ISC DHCP  4.3.3
A reference DHCPv4 and DHCPv6 implementation
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
mdb6.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007-2015 by Internet Systems Consortium, Inc. ("ISC")
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
9  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10  * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
11  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
13  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14  * PERFORMANCE OF THIS SOFTWARE.
15  */
16 
168 #include "config.h"
169 
170 #include <sys/types.h>
171 #include <time.h>
172 #include <netinet/in.h>
173 
174 #include <stdarg.h>
175 #include "dhcpd.h"
176 #include "omapip/omapip.h"
177 #include "omapip/hash.h"
178 #include <isc/md5.h>
179 
180 HASH_FUNCTIONS(ia, unsigned char *, struct ia_xx, ia_hash_t,
182 
186 
187 HASH_FUNCTIONS(iasubopt, struct in6_addr *, struct iasubopt, iasubopt_hash_t,
189 
190 struct ipv6_pool **pools;
191 int num_pools;
192 
193 /*
194  * Create a new IAADDR/PREFIX structure.
195  *
196  * - iasubopt must be a pointer to a (struct iasubopt *) pointer previously
197  * initialized to NULL
198  */
199 isc_result_t
200 iasubopt_allocate(struct iasubopt **iasubopt, const char *file, int line) {
201  struct iasubopt *tmp;
202 
203  if (iasubopt == NULL) {
204  log_error("%s(%d): NULL pointer reference", file, line);
205  return DHCP_R_INVALIDARG;
206  }
207  if (*iasubopt != NULL) {
208  log_error("%s(%d): non-NULL pointer", file, line);
209  return DHCP_R_INVALIDARG;
210  }
211 
212  tmp = dmalloc(sizeof(*tmp), file, line);
213  if (tmp == NULL) {
214  return ISC_R_NOMEMORY;
215  }
216 
217  tmp->refcnt = 1;
218  tmp->state = FTS_FREE;
219  tmp->heap_index = -1;
220  tmp->plen = 255;
221 
222  *iasubopt = tmp;
223  return ISC_R_SUCCESS;
224 }
225 
226 /*
227  * Reference an IAADDR/PREFIX structure.
228  *
229  * - iasubopt must be a pointer to a (struct iasubopt *) pointer previously
230  * initialized to NULL
231  */
232 isc_result_t
233 iasubopt_reference(struct iasubopt **iasubopt, struct iasubopt *src,
234  const char *file, int line) {
235  if (iasubopt == NULL) {
236  log_error("%s(%d): NULL pointer reference", file, line);
237  return DHCP_R_INVALIDARG;
238  }
239  if (*iasubopt != NULL) {
240  log_error("%s(%d): non-NULL pointer", file, line);
241  return DHCP_R_INVALIDARG;
242  }
243  if (src == NULL) {
244  log_error("%s(%d): NULL pointer reference", file, line);
245  return DHCP_R_INVALIDARG;
246  }
247  *iasubopt = src;
248  src->refcnt++;
249  return ISC_R_SUCCESS;
250 }
251 
252 
253 /*
254  * Dereference an IAADDR/PREFIX structure.
255  *
256  * If it is the last reference, then the memory for the
257  * structure is freed.
258  */
259 isc_result_t
260 iasubopt_dereference(struct iasubopt **iasubopt, const char *file, int line) {
261  struct iasubopt *tmp;
262 
263  if ((iasubopt == NULL) || (*iasubopt == NULL)) {
264  log_error("%s(%d): NULL pointer", file, line);
265  return DHCP_R_INVALIDARG;
266  }
267 
268  tmp = *iasubopt;
269  *iasubopt = NULL;
270 
271  tmp->refcnt--;
272  if (tmp->refcnt < 0) {
273  log_error("%s(%d): negative refcnt", file, line);
274  tmp->refcnt = 0;
275  }
276  if (tmp->refcnt == 0) {
277  if (tmp->ia != NULL) {
278  ia_dereference(&(tmp->ia), file, line);
279  }
280  if (tmp->ipv6_pool != NULL) {
281  ipv6_pool_dereference(&(tmp->ipv6_pool), file, line);
282  }
283  if (tmp->scope != NULL) {
284  binding_scope_dereference(&tmp->scope, file, line);
285  }
286 
287  if (tmp->on_star.on_expiry != NULL) {
289  (&tmp->on_star.on_expiry, MDL);
290  }
291  if (tmp->on_star.on_commit != NULL) {
293  (&tmp->on_star.on_commit, MDL);
294  }
295  if (tmp->on_star.on_release != NULL) {
297  (&tmp->on_star.on_release, MDL);
298  }
299 
300  dfree(tmp, file, line);
301  }
302 
303  return ISC_R_SUCCESS;
304 }
305 
306 /*
307  * Make the key that we use for IA.
308  */
309 isc_result_t
310 ia_make_key(struct data_string *key, u_int32_t iaid,
311  const char *duid, unsigned int duid_len,
312  const char *file, int line) {
313 
314  memset(key, 0, sizeof(*key));
315  key->len = duid_len + sizeof(iaid);
316  if (!buffer_allocate(&(key->buffer), key->len, file, line)) {
317  return ISC_R_NOMEMORY;
318  }
319  key->data = key->buffer->data;
320  memcpy((char *)key->data, &iaid, sizeof(iaid));
321  memcpy((char *)key->data + sizeof(iaid), duid, duid_len);
322 
323  return ISC_R_SUCCESS;
324 }
325 
326 /*
327  * Create a new IA structure.
328  *
329  * - ia must be a pointer to a (struct ia_xx *) pointer previously
330  * initialized to NULL
331  * - iaid and duid are values from the client
332  *
333  * XXXsk: we don't concern ourself with the byte order of the IAID,
334  * which might be a problem if we transfer this structure
335  * between machines of different byte order
336  */
337 isc_result_t
338 ia_allocate(struct ia_xx **ia, u_int32_t iaid,
339  const char *duid, unsigned int duid_len,
340  const char *file, int line) {
341  struct ia_xx *tmp;
342 
343  if (ia == NULL) {
344  log_error("%s(%d): NULL pointer reference", file, line);
345  return DHCP_R_INVALIDARG;
346  }
347  if (*ia != NULL) {
348  log_error("%s(%d): non-NULL pointer", file, line);
349  return DHCP_R_INVALIDARG;
350  }
351 
352  tmp = dmalloc(sizeof(*tmp), file, line);
353  if (tmp == NULL) {
354  return ISC_R_NOMEMORY;
355  }
356 
357  if (ia_make_key(&tmp->iaid_duid, iaid,
358  duid, duid_len, file, line) != ISC_R_SUCCESS) {
359  dfree(tmp, file, line);
360  return ISC_R_NOMEMORY;
361  }
362 
363  tmp->refcnt = 1;
364 
365  *ia = tmp;
366  return ISC_R_SUCCESS;
367 }
368 
369 /*
370  * Reference an IA structure.
371  *
372  * - ia must be a pointer to a (struct ia_xx *) pointer previously
373  * initialized to NULL
374  */
375 isc_result_t
376 ia_reference(struct ia_xx **ia, struct ia_xx *src,
377  const char *file, int line) {
378  if (ia == NULL) {
379  log_error("%s(%d): NULL pointer reference", file, line);
380  return DHCP_R_INVALIDARG;
381  }
382  if (*ia != NULL) {
383  log_error("%s(%d): non-NULL pointer", file, line);
384  return DHCP_R_INVALIDARG;
385  }
386  if (src == NULL) {
387  log_error("%s(%d): NULL pointer reference", file, line);
388  return DHCP_R_INVALIDARG;
389  }
390  *ia = src;
391  src->refcnt++;
392  return ISC_R_SUCCESS;
393 }
394 
395 /*
396  * Dereference an IA structure.
397  *
398  * If it is the last reference, then the memory for the
399  * structure is freed.
400  */
401 isc_result_t
402 ia_dereference(struct ia_xx **ia, const char *file, int line) {
403  struct ia_xx *tmp;
404  int i;
405 
406  if ((ia == NULL) || (*ia == NULL)) {
407  log_error("%s(%d): NULL pointer", file, line);
408  return DHCP_R_INVALIDARG;
409  }
410 
411  tmp = *ia;
412  *ia = NULL;
413 
414  tmp->refcnt--;
415  if (tmp->refcnt < 0) {
416  log_error("%s(%d): negative refcnt", file, line);
417  tmp->refcnt = 0;
418  }
419  if (tmp->refcnt == 0) {
420  if (tmp->iasubopt != NULL) {
421  for (i=0; i<tmp->num_iasubopt; i++) {
422  iasubopt_dereference(&(tmp->iasubopt[i]),
423  file, line);
424  }
425  dfree(tmp->iasubopt, file, line);
426  }
427  data_string_forget(&(tmp->iaid_duid), file, line);
428  dfree(tmp, file, line);
429  }
430  return ISC_R_SUCCESS;
431 }
432 
433 
434 /*
435  * Add an IAADDR/PREFIX entry to an IA structure.
436  */
437 isc_result_t
438 ia_add_iasubopt(struct ia_xx *ia, struct iasubopt *iasubopt,
439  const char *file, int line) {
440  int max;
441  struct iasubopt **new;
442 
443  /*
444  * Grow our array if we need to.
445  *
446  * Note: we pick 4 as the increment, as that seems a reasonable
447  * guess as to how many addresses/prefixes we might expect
448  * on an interface.
449  */
450  if (ia->max_iasubopt <= ia->num_iasubopt) {
451  max = ia->max_iasubopt + 4;
452  new = dmalloc(max * sizeof(struct iasubopt *), file, line);
453  if (new == NULL) {
454  return ISC_R_NOMEMORY;
455  }
456  memcpy(new, ia->iasubopt,
457  ia->num_iasubopt * sizeof(struct iasubopt *));
458  ia->iasubopt = new;
459  ia->max_iasubopt = max;
460  }
461 
462  iasubopt_reference(&(ia->iasubopt[ia->num_iasubopt]), iasubopt,
463  file, line);
464  ia->num_iasubopt++;
465 
466  return ISC_R_SUCCESS;
467 }
468 
469 /*
470  * Remove an IAADDR/PREFIX entry to an IA structure.
471  *
472  * Note: if a suboption appears more than once, then only ONE will be removed.
473  */
474 void
476  const char *file, int line) {
477  int i, j;
478  if (ia == NULL || iasubopt == NULL)
479  return;
480 
481  for (i=0; i<ia->num_iasubopt; i++) {
482  if (ia->iasubopt[i] == iasubopt) {
483  /* remove this sub option */
484  iasubopt_dereference(&(ia->iasubopt[i]), file, line);
485  /* move remaining suboption pointers down one */
486  for (j=i+1; j < ia->num_iasubopt; j++) {
487  ia->iasubopt[j-1] = ia->iasubopt[j];
488  }
489  /* decrease our total count */
490  /* remove the back-reference in the suboption itself */
491  ia_dereference(&iasubopt->ia, file, line);
492  ia->num_iasubopt--;
493  return;
494  }
495  }
496  log_error("%s(%d): IAADDR/PREFIX not in IA", file, line);
497 }
498 
499 /*
500  * Remove all addresses/prefixes from an IA.
501  */
502 void
503 ia_remove_all_lease(struct ia_xx *ia, const char *file, int line) {
504  int i;
505 
506  for (i=0; i<ia->num_iasubopt; i++) {
507  ia_dereference(&(ia->iasubopt[i]->ia), file, line);
508  iasubopt_dereference(&(ia->iasubopt[i]), file, line);
509  }
510  ia->num_iasubopt = 0;
511 }
512 
513 /*
514  * Compare two IA.
515  */
516 isc_boolean_t
517 ia_equal(const struct ia_xx *a, const struct ia_xx *b)
518 {
519  isc_boolean_t found;
520  int i, j;
521 
522  /*
523  * Handle cases where one or both of the inputs is NULL.
524  */
525  if (a == NULL) {
526  if (b == NULL) {
527  return ISC_TRUE;
528  } else {
529  return ISC_FALSE;
530  }
531  }
532 
533  /*
534  * Check the type is the same.
535  */
536  if (a->ia_type != b->ia_type) {
537  return ISC_FALSE;
538  }
539 
540  /*
541  * Check the DUID is the same.
542  */
543  if (a->iaid_duid.len != b->iaid_duid.len) {
544  return ISC_FALSE;
545  }
546  if (memcmp(a->iaid_duid.data,
547  b->iaid_duid.data, a->iaid_duid.len) != 0) {
548  return ISC_FALSE;
549  }
550 
551  /*
552  * Make sure we have the same number of addresses/prefixes in each.
553  */
554  if (a->num_iasubopt != b->num_iasubopt) {
555  return ISC_FALSE;
556  }
557 
558  /*
559  * Check that each address/prefix is present in both.
560  */
561  for (i=0; i<a->num_iasubopt; i++) {
562  found = ISC_FALSE;
563  for (j=0; j<a->num_iasubopt; j++) {
564  if (a->iasubopt[i]->plen != b->iasubopt[i]->plen)
565  continue;
566  if (memcmp(&(a->iasubopt[i]->addr),
567  &(b->iasubopt[j]->addr),
568  sizeof(struct in6_addr)) == 0) {
569  found = ISC_TRUE;
570  break;
571  }
572  }
573  if (!found) {
574  return ISC_FALSE;
575  }
576  }
577 
578  /*
579  * These are the same in every way we care about.
580  */
581  return ISC_TRUE;
582 }
583 
584 /*
585  * Helper function for lease heaps.
586  * Makes the top of the heap the oldest lease.
587  */
588 static isc_boolean_t
589 lease_older(void *a, void *b) {
590  struct iasubopt *la = (struct iasubopt *)a;
591  struct iasubopt *lb = (struct iasubopt *)b;
592 
594  return difftime(la->soft_lifetime_end_time,
595  lb->soft_lifetime_end_time) < 0;
596  } else {
597  return difftime(la->hard_lifetime_end_time,
598  lb->hard_lifetime_end_time) < 0;
599  }
600 }
601 
602 /*
603  * Helper function for lease address/prefix heaps.
604  * Callback when an address's position in the heap changes.
605  */
606 static void
607 lease_index_changed(void *iasubopt, unsigned int new_heap_index) {
608  ((struct iasubopt *)iasubopt)-> heap_index = new_heap_index;
609 }
610 
611 
634 isc_result_t
635 ipv6_pool_allocate(struct ipv6_pool **pool, u_int16_t type,
636  const struct in6_addr *start_addr, int bits,
637  int units, const char *file, int line) {
638  struct ipv6_pool *tmp;
639 
640  if (pool == NULL) {
641  log_error("%s(%d): NULL pointer reference", file, line);
642  return DHCP_R_INVALIDARG;
643  }
644  if (*pool != NULL) {
645  log_error("%s(%d): non-NULL pointer", file, line);
646  return DHCP_R_INVALIDARG;
647  }
648 
649  tmp = dmalloc(sizeof(*tmp), file, line);
650  if (tmp == NULL) {
651  return ISC_R_NOMEMORY;
652  }
653 
654  tmp->refcnt = 1;
655  tmp->pool_type = type;
656  tmp->start_addr = *start_addr;
657  tmp->bits = bits;
658  tmp->units = units;
659  if (!iasubopt_new_hash(&tmp->leases, DEFAULT_HASH_SIZE, file, line)) {
660  dfree(tmp, file, line);
661  return ISC_R_NOMEMORY;
662  }
663  if (isc_heap_create(dhcp_gbl_ctx.mctx, lease_older, lease_index_changed,
664  0, &(tmp->active_timeouts)) != ISC_R_SUCCESS) {
665  iasubopt_free_hash_table(&(tmp->leases), file, line);
666  dfree(tmp, file, line);
667  return ISC_R_NOMEMORY;
668  }
669  if (isc_heap_create(dhcp_gbl_ctx.mctx, lease_older, lease_index_changed,
670  0, &(tmp->inactive_timeouts)) != ISC_R_SUCCESS) {
672  iasubopt_free_hash_table(&(tmp->leases), file, line);
673  dfree(tmp, file, line);
674  return ISC_R_NOMEMORY;
675  }
676 
677  *pool = tmp;
678  return ISC_R_SUCCESS;
679 }
680 
700 isc_result_t
702  const char *file, int line) {
703  if (pool == NULL) {
704  log_error("%s(%d): NULL pointer reference", file, line);
705  return DHCP_R_INVALIDARG;
706  }
707  if (*pool != NULL) {
708  log_error("%s(%d): non-NULL pointer", file, line);
709  return DHCP_R_INVALIDARG;
710  }
711  if (src == NULL) {
712  log_error("%s(%d): NULL pointer reference", file, line);
713  return DHCP_R_INVALIDARG;
714  }
715  *pool = src;
716  src->refcnt++;
717  return ISC_R_SUCCESS;
718 }
719 
720 /*
721  * Note: Each IAADDR/PREFIX in a pool is referenced by the pool. This is needed
722  * to prevent the lease from being garbage collected out from under the
723  * pool.
724  *
725  * The references are made from the hash and from the heap. The following
726  * helper functions dereference these when a pool is destroyed.
727  */
728 
729 /*
730  * Helper function for pool cleanup.
731  * Dereference each of the hash entries in a pool.
732  */
733 static isc_result_t
734 dereference_hash_entry(const void *name, unsigned len, void *value) {
735  struct iasubopt *iasubopt = (struct iasubopt *)value;
736 
737  iasubopt_dereference(&iasubopt, MDL);
738  return ISC_R_SUCCESS;
739 }
740 
741 /*
742  * Helper function for pool cleanup.
743  * Dereference each of the heap entries in a pool.
744  */
745 static void
746 dereference_heap_entry(void *value, void *dummy) {
747  struct iasubopt *iasubopt = (struct iasubopt *)value;
748 
749  iasubopt_dereference(&iasubopt, MDL);
750 }
751 
771 isc_result_t
772 ipv6_pool_dereference(struct ipv6_pool **pool, const char *file, int line) {
773  struct ipv6_pool *tmp;
774 
775  if ((pool == NULL) || (*pool == NULL)) {
776  log_error("%s(%d): NULL pointer", file, line);
777  return DHCP_R_INVALIDARG;
778  }
779 
780  tmp = *pool;
781  *pool = NULL;
782 
783  tmp->refcnt--;
784  if (tmp->refcnt < 0) {
785  log_error("%s(%d): negative refcnt", file, line);
786  tmp->refcnt = 0;
787  }
788  if (tmp->refcnt == 0) {
789  iasubopt_hash_foreach(tmp->leases, dereference_hash_entry);
790  iasubopt_free_hash_table(&(tmp->leases), file, line);
792  dereference_heap_entry, NULL);
795  dereference_heap_entry, NULL);
797  dfree(tmp, file, line);
798  }
799 
800  return ISC_R_SUCCESS;
801 }
802 
803 /*
804  * Create an address by hashing the input, and using that for
805  * the non-network part.
806  */
807 static void
808 build_address6(struct in6_addr *addr,
809  const struct in6_addr *net_start_addr, int net_bits,
810  const struct data_string *input) {
811  isc_md5_t ctx;
812  int net_bytes;
813  int i;
814  char *str;
815  const char *net_str;
816 
817  /*
818  * Use MD5 to get a nice 128 bit hash of the input.
819  * Yes, we know MD5 isn't cryptographically sound.
820  * No, we don't care.
821  */
822  isc_md5_init(&ctx);
823  isc_md5_update(&ctx, input->data, input->len);
824  isc_md5_final(&ctx, (unsigned char *)addr);
825 
826  /*
827  * Copy the [0..128] network bits over.
828  */
829  str = (char *)addr;
830  net_str = (const char *)net_start_addr;
831  net_bytes = net_bits / 8;
832  for (i = 0; i < net_bytes; i++) {
833  str[i] = net_str[i];
834  }
835  switch (net_bits % 8) {
836  case 1: str[i] = (str[i] & 0x7F) | (net_str[i] & 0x80); break;
837  case 2: str[i] = (str[i] & 0x3F) | (net_str[i] & 0xC0); break;
838  case 3: str[i] = (str[i] & 0x1F) | (net_str[i] & 0xE0); break;
839  case 4: str[i] = (str[i] & 0x0F) | (net_str[i] & 0xF0); break;
840  case 5: str[i] = (str[i] & 0x07) | (net_str[i] & 0xF8); break;
841  case 6: str[i] = (str[i] & 0x03) | (net_str[i] & 0xFC); break;
842  case 7: str[i] = (str[i] & 0x01) | (net_str[i] & 0xFE); break;
843  }
844 
845  /*
846  * Set the universal/local bit ("u bit") to zero for /64s. The
847  * individual/group bit ("g bit") is unchanged, because the g-bit
848  * has no meaning when the u-bit is cleared.
849  */
850  if (net_bits == 64)
851  str[8] &= ~0x02;
852 }
853 
854 /*
855  * Create a temporary address by a variant of RFC 4941 algo.
856  * Note: this should not be used for prefixes shorter than 64 bits.
857  */
858 static void
859 build_temporary6(struct in6_addr *addr,
860  const struct in6_addr *net_start_addr, int net_bits,
861  const struct data_string *input) {
862  static u_int32_t history[2];
863  static u_int32_t counter = 0;
864  isc_md5_t ctx;
865  unsigned char md[16];
866 
867  /*
868  * First time/time to reseed.
869  * Please use a good pseudo-random generator here!
870  */
871  if (counter == 0) {
872  isc_random_get(&history[0]);
873  isc_random_get(&history[1]);
874  }
875 
876  /*
877  * Use MD5 as recommended by RFC 4941.
878  */
879  isc_md5_init(&ctx);
880  isc_md5_update(&ctx, (unsigned char *)&history[0], 8UL);
881  isc_md5_update(&ctx, input->data, input->len);
882  isc_md5_final(&ctx, md);
883 
884  /*
885  * Build the address.
886  */
887  if (net_bits == 64) {
888  memcpy(&addr->s6_addr[0], &net_start_addr->s6_addr[0], 8);
889  memcpy(&addr->s6_addr[8], md, 8);
890  addr->s6_addr[8] &= ~0x02;
891  } else {
892  int net_bytes;
893  int i;
894  char *str;
895  const char *net_str;
896 
897  /*
898  * Copy the [0..128] network bits over.
899  */
900  str = (char *)addr;
901  net_str = (const char *)net_start_addr;
902  net_bytes = net_bits / 8;
903  for (i = 0; i < net_bytes; i++) {
904  str[i] = net_str[i];
905  }
906  memcpy(str + net_bytes, md, 16 - net_bytes);
907  switch (net_bits % 8) {
908  case 1: str[i] = (str[i] & 0x7F) | (net_str[i] & 0x80); break;
909  case 2: str[i] = (str[i] & 0x3F) | (net_str[i] & 0xC0); break;
910  case 3: str[i] = (str[i] & 0x1F) | (net_str[i] & 0xE0); break;
911  case 4: str[i] = (str[i] & 0x0F) | (net_str[i] & 0xF0); break;
912  case 5: str[i] = (str[i] & 0x07) | (net_str[i] & 0xF8); break;
913  case 6: str[i] = (str[i] & 0x03) | (net_str[i] & 0xFC); break;
914  case 7: str[i] = (str[i] & 0x01) | (net_str[i] & 0xFE); break;
915  }
916  }
917 
918 
919  /*
920  * Save history for the next call.
921  */
922  memcpy((unsigned char *)&history[0], md + 8, 8);
923  counter++;
924 }
925 
926 /* Reserved Subnet Router Anycast ::0:0:0:0. */
927 static struct in6_addr rtany;
928 /* Reserved Subnet Anycasts ::fdff:ffff:ffff:ff80-::fdff:ffff:ffff:ffff. */
929 static struct in6_addr resany;
930 
931 /*
932  * Create a lease for the given address and client duid.
933  *
934  * - pool must be a pointer to a (struct ipv6_pool *) pointer previously
935  * initialized to NULL
936  *
937  * Right now we simply hash the DUID, and if we get a collision, we hash
938  * again until we find a free address. We try this a fixed number of times,
939  * to avoid getting stuck in a loop (this is important on small pools
940  * where we can run out of space).
941  *
942  * We return the number of attempts that it took to find an available
943  * lease. This tells callers when a pool is are filling up, as
944  * well as an indication of how full the pool is; statistically the
945  * more full a pool is the more attempts must be made before finding
946  * a free lease. Realistically this will only happen in very full
947  * pools.
948  *
949  * We probably want different algorithms depending on the network size, in
950  * the long term.
951  */
952 isc_result_t
953 create_lease6(struct ipv6_pool *pool, struct iasubopt **addr,
954  unsigned int *attempts,
955  const struct data_string *uid, time_t soft_lifetime_end_time) {
956  struct data_string ds;
957  struct in6_addr tmp;
958  struct iasubopt *test_iaaddr;
959  struct data_string new_ds;
960  struct iasubopt *iaaddr;
961  isc_result_t result;
962  isc_boolean_t reserved_iid;
963  static isc_boolean_t init_resiid = ISC_FALSE;
964 
965  /*
966  * Fill the reserved IIDs.
967  */
968  if (!init_resiid) {
969  memset(&rtany, 0, 16);
970  memset(&resany, 0, 8);
971  resany.s6_addr[8] = 0xfd;
972  memset(&resany.s6_addr[9], 0xff, 6);
973  init_resiid = ISC_TRUE;
974  }
975 
976  /*
977  * Use the UID as our initial seed for the hash
978  */
979  memset(&ds, 0, sizeof(ds));
980  data_string_copy(&ds, (struct data_string *)uid, MDL);
981 
982  *attempts = 0;
983  for (;;) {
984  /*
985  * Give up at some point.
986  */
987  if (++(*attempts) > 100) {
988  data_string_forget(&ds, MDL);
989  return ISC_R_NORESOURCES;
990  }
991 
992  /*
993  * Build a resource.
994  */
995  switch (pool->pool_type) {
996  case D6O_IA_NA:
997  /* address */
998  build_address6(&tmp, &pool->start_addr,
999  pool->bits, &ds);
1000  break;
1001  case D6O_IA_TA:
1002  /* temporary address */
1003  build_temporary6(&tmp, &pool->start_addr,
1004  pool->bits, &ds);
1005  break;
1006  case D6O_IA_PD:
1007  /* prefix */
1008  log_error("create_lease6: prefix pool.");
1009  return DHCP_R_INVALIDARG;
1010  default:
1011  log_error("create_lease6: untyped pool.");
1012  return DHCP_R_INVALIDARG;
1013  }
1014 
1015  /*
1016  * Avoid reserved interface IDs. (cf. RFC 5453)
1017  */
1018  reserved_iid = ISC_FALSE;
1019  if (memcmp(&tmp.s6_addr[8], &rtany.s6_addr[8], 8) == 0) {
1020  reserved_iid = ISC_TRUE;
1021  }
1022  if (!reserved_iid &&
1023  (memcmp(&tmp.s6_addr[8], &resany.s6_addr[8], 7) == 0) &&
1024  ((tmp.s6_addr[15] & 0x80) == 0x80)) {
1025  reserved_iid = ISC_TRUE;
1026  }
1027 
1028  /*
1029  * If this address is not in use, we're happy with it
1030  */
1031  test_iaaddr = NULL;
1032  if (!reserved_iid &&
1033  (iasubopt_hash_lookup(&test_iaaddr, pool->leases,
1034  &tmp, sizeof(tmp), MDL) == 0)) {
1035  break;
1036  }
1037  if (test_iaaddr != NULL)
1038  iasubopt_dereference(&test_iaaddr, MDL);
1039 
1040  /*
1041  * Otherwise, we create a new input, adding the address
1042  */
1043  memset(&new_ds, 0, sizeof(new_ds));
1044  new_ds.len = ds.len + sizeof(tmp);
1045  if (!buffer_allocate(&new_ds.buffer, new_ds.len, MDL)) {
1046  data_string_forget(&ds, MDL);
1047  return ISC_R_NOMEMORY;
1048  }
1049  new_ds.data = new_ds.buffer->data;
1050  memcpy(new_ds.buffer->data, ds.data, ds.len);
1051  memcpy(new_ds.buffer->data + ds.len, &tmp, sizeof(tmp));
1052  data_string_forget(&ds, MDL);
1053  data_string_copy(&ds, &new_ds, MDL);
1054  data_string_forget(&new_ds, MDL);
1055  }
1056 
1057  data_string_forget(&ds, MDL);
1058 
1059  /*
1060  * We're happy with the address, create an IAADDR
1061  * to hold it.
1062  */
1063  iaaddr = NULL;
1064  result = iasubopt_allocate(&iaaddr, MDL);
1065  if (result != ISC_R_SUCCESS) {
1066  return result;
1067  }
1068  iaaddr->plen = 0;
1069  memcpy(&iaaddr->addr, &tmp, sizeof(iaaddr->addr));
1070 
1071  /*
1072  * Add the lease to the pool (note state is free, not active?!).
1073  */
1074  result = add_lease6(pool, iaaddr, soft_lifetime_end_time);
1075  if (result == ISC_R_SUCCESS) {
1076  iasubopt_reference(addr, iaaddr, MDL);
1077  }
1078  iasubopt_dereference(&iaaddr, MDL);
1079  return result;
1080 }
1081 
1082 
1123 isc_result_t
1125  struct ipv6_pool *pool,
1126  struct iasubopt *lease,
1127  struct ia_xx *ia) {
1128 
1129  struct iasubopt *test_iasubopt, *tmp_iasubopt;
1130  struct ia_xx *old_ia;
1131  isc_result_t status = ISC_R_SUCCESS;
1132 
1133  test_iasubopt = NULL;
1134  old_ia = NULL;
1135 
1136  /*
1137  * Look up the address - if we don't find a lease
1138  * we don't need to do anything.
1139  */
1140  if (iasubopt_hash_lookup(&test_iasubopt, pool->leases,
1141  &lease->addr, sizeof(lease->addr),
1142  MDL) == 0) {
1143  return (ISC_R_SUCCESS);
1144  }
1145 
1146  if (test_iasubopt->ia == NULL) {
1147  /* no old ia, no work to do */
1148  iasubopt_dereference(&test_iasubopt, MDL);
1149  return (status);
1150  }
1151 
1152  ia_reference(&old_ia, test_iasubopt->ia, MDL);
1153 
1154  if ((old_ia->iaid_duid.len == ia->iaid_duid.len) &&
1155  (memcmp((unsigned char *)ia->iaid_duid.data,
1156  (unsigned char *)old_ia->iaid_duid.data,
1157  ia->iaid_duid.len) == 0)) {
1158  /* same IA */
1159  if ((lease->state == FTS_ACTIVE) ||
1160  (lease->state == FTS_ABANDONED)) {
1161  /* still active, no need to delete */
1162  goto cleanup;
1163  }
1164  } else {
1165  /* different IA */
1166  if ((lease->state != FTS_ACTIVE) &&
1167  (lease->state != FTS_ABANDONED)) {
1168  /* new lease isn't active, no work */
1169  goto cleanup;
1170  }
1171 
1172  /*
1173  * We appear to have two active leases, this shouldn't happen.
1174  * Before a second lease can be set to active the first lease
1175  * should be set to inactive (released, expired etc). For now
1176  * delete the previous lease and indicate a failure to the
1177  * caller so it can generate a warning.
1178  * In the future we may try and determine which is the better
1179  * lease to keep.
1180  */
1181 
1182  status = ISC_R_FAILURE;
1183  }
1184 
1185  /*
1186  * Remove the old lease from the active heap and from the hash table
1187  * then remove the lease from the IA and clean up the IA if necessary.
1188  */
1189  isc_heap_delete(pool->active_timeouts, test_iasubopt->heap_index);
1190  pool->num_active--;
1191  if (pool->ipv6_pond)
1192  pool->ipv6_pond->num_active--;
1193 
1194  if (lease->state == FTS_ABANDONED) {
1195  pool->num_abandoned--;
1196  if (pool->ipv6_pond)
1197  pool->ipv6_pond->num_abandoned--;
1198  }
1199 
1200  iasubopt_hash_delete(pool->leases, &test_iasubopt->addr,
1201  sizeof(test_iasubopt->addr), MDL);
1202  ia_remove_iasubopt(old_ia, test_iasubopt, MDL);
1203  if (old_ia->num_iasubopt <= 0) {
1204  ia_hash_delete(ia_table,
1205  (unsigned char *)old_ia->iaid_duid.data,
1206  old_ia->iaid_duid.len, MDL);
1207  }
1208 
1209  /*
1210  * We derefenrece the subopt here as we've just removed it from
1211  * the hash table in the pool. We need to make a copy as we
1212  * need to derefernece it again later.
1213  */
1214  tmp_iasubopt = test_iasubopt;
1215  iasubopt_dereference(&tmp_iasubopt, MDL);
1216 
1217  cleanup:
1218  ia_dereference(&old_ia, MDL);
1219 
1220  /*
1221  * Clean up the reference, this is in addition to the deference
1222  * above after removing the entry from the hash table
1223  */
1224  iasubopt_dereference(&test_iasubopt, MDL);
1225 
1226  return (status);
1227 }
1228 
1229 /*
1230  * Put a lease in the pool directly. This is intended to be used when
1231  * loading leases from the file.
1232  */
1233 isc_result_t
1235  time_t valid_lifetime_end_time) {
1236  isc_result_t insert_result;
1237  struct iasubopt *test_iasubopt;
1238  struct iasubopt *tmp_iasubopt;
1239 
1240  /* If a state was not assigned by the caller, assume active. */
1241  if (lease->state == 0)
1242  lease->state = FTS_ACTIVE;
1243 
1244  ipv6_pool_reference(&lease->ipv6_pool, pool, MDL);
1245 
1246  /*
1247  * If this IAADDR/PREFIX is already in our structures, remove the
1248  * old one.
1249  */
1250  test_iasubopt = NULL;
1251  if (iasubopt_hash_lookup(&test_iasubopt, pool->leases,
1252  &lease->addr, sizeof(lease->addr), MDL)) {
1253  /* XXX: we should probably ask the lease what heap it is on
1254  * (as a consistency check).
1255  * XXX: we should probably have one function to "put this lease
1256  * on its heap" rather than doing these if's everywhere. If
1257  * you add more states to this list, don't.
1258  */
1259  if ((test_iasubopt->state == FTS_ACTIVE) ||
1260  (test_iasubopt->state == FTS_ABANDONED)) {
1262  test_iasubopt->heap_index);
1263  pool->num_active--;
1264  if (pool->ipv6_pond)
1265  pool->ipv6_pond->num_active--;
1266 
1267  if (test_iasubopt->state == FTS_ABANDONED) {
1268  pool->num_abandoned--;
1269  if (pool->ipv6_pond)
1270  pool->ipv6_pond->num_abandoned--;
1271  }
1272  } else {
1274  test_iasubopt->heap_index);
1275  pool->num_inactive--;
1276  }
1277 
1278  iasubopt_hash_delete(pool->leases, &test_iasubopt->addr,
1279  sizeof(test_iasubopt->addr), MDL);
1280 
1281  /*
1282  * We're going to do a bit of evil trickery here.
1283  *
1284  * We need to dereference the entry once to remove our
1285  * current reference (in test_iasubopt), and then one
1286  * more time to remove the reference left when the
1287  * address was added to the pool before.
1288  */
1289  tmp_iasubopt = test_iasubopt;
1290  iasubopt_dereference(&test_iasubopt, MDL);
1291  iasubopt_dereference(&tmp_iasubopt, MDL);
1292  }
1293 
1294  /*
1295  * Add IAADDR/PREFIX to our structures.
1296  */
1297  tmp_iasubopt = NULL;
1298  iasubopt_reference(&tmp_iasubopt, lease, MDL);
1299  if ((tmp_iasubopt->state == FTS_ACTIVE) ||
1300  (tmp_iasubopt->state == FTS_ABANDONED)) {
1301  tmp_iasubopt->hard_lifetime_end_time = valid_lifetime_end_time;
1302  iasubopt_hash_add(pool->leases, &tmp_iasubopt->addr,
1303  sizeof(tmp_iasubopt->addr), lease, MDL);
1304  insert_result = isc_heap_insert(pool->active_timeouts,
1305  tmp_iasubopt);
1306  if (insert_result == ISC_R_SUCCESS) {
1307  pool->num_active++;
1308  if (pool->ipv6_pond)
1309  pool->ipv6_pond->num_active++;
1310 
1311  if (tmp_iasubopt->state == FTS_ABANDONED) {
1312  pool->num_abandoned++;
1313  if (pool->ipv6_pond)
1314  pool->ipv6_pond->num_abandoned++;
1315  }
1316  }
1317 
1318  } else {
1319  tmp_iasubopt->soft_lifetime_end_time = valid_lifetime_end_time;
1320  insert_result = isc_heap_insert(pool->inactive_timeouts,
1321  tmp_iasubopt);
1322  if (insert_result == ISC_R_SUCCESS)
1323  pool->num_inactive++;
1324  }
1325  if (insert_result != ISC_R_SUCCESS) {
1326  iasubopt_hash_delete(pool->leases, &lease->addr,
1327  sizeof(lease->addr), MDL);
1328  iasubopt_dereference(&tmp_iasubopt, MDL);
1329  return insert_result;
1330  }
1331 
1332  /*
1333  * Note: we intentionally leave tmp_iasubopt referenced; there
1334  * is a reference in the heap/hash, after all.
1335  */
1336 
1337  return ISC_R_SUCCESS;
1338 }
1339 
1340 /*
1341  * Determine if an address is present in a pool or not.
1342  */
1343 isc_boolean_t
1344 lease6_exists(const struct ipv6_pool *pool, const struct in6_addr *addr) {
1345  struct iasubopt *test_iaaddr;
1346 
1347  test_iaaddr = NULL;
1348  if (iasubopt_hash_lookup(&test_iaaddr, pool->leases,
1349  (void *)addr, sizeof(*addr), MDL)) {
1350  iasubopt_dereference(&test_iaaddr, MDL);
1351  return ISC_TRUE;
1352  } else {
1353  return ISC_FALSE;
1354  }
1355 }
1356 
1371 isc_boolean_t
1373  struct iasubopt *test_iaaddr;
1374  isc_boolean_t status = ISC_TRUE;
1375 
1376  test_iaaddr = NULL;
1377  if (iasubopt_hash_lookup(&test_iaaddr, lease->ipv6_pool->leases,
1378  (void *)&lease->addr,
1379  sizeof(lease->addr), MDL)) {
1380  if (test_iaaddr != lease) {
1381  status = ISC_FALSE;
1382  }
1383  iasubopt_dereference(&test_iaaddr, MDL);
1384  }
1385 
1386  return (status);
1387 }
1388 
1389 /*
1390  * Put the lease on our active pool.
1391  */
1392 static isc_result_t
1393 move_lease_to_active(struct ipv6_pool *pool, struct iasubopt *lease) {
1394  isc_result_t insert_result;
1395  int old_heap_index;
1396 
1397  old_heap_index = lease->heap_index;
1398  insert_result = isc_heap_insert(pool->active_timeouts, lease);
1399  if (insert_result == ISC_R_SUCCESS) {
1400  iasubopt_hash_add(pool->leases, &lease->addr,
1401  sizeof(lease->addr), lease, MDL);
1402  isc_heap_delete(pool->inactive_timeouts, old_heap_index);
1403  pool->num_active++;
1404  pool->num_inactive--;
1405  lease->state = FTS_ACTIVE;
1406  if (pool->ipv6_pond)
1407  pool->ipv6_pond->num_active++;
1408 
1409  }
1410  return insert_result;
1411 }
1412 
1443 isc_result_t
1444 renew_lease6(struct ipv6_pool *pool, struct iasubopt *lease) {
1445  time_t old_end_time = lease->hard_lifetime_end_time;
1447  lease->soft_lifetime_end_time = 0;
1448 
1449  if (lease->state == FTS_ACTIVE) {
1450  if (old_end_time <= lease->hard_lifetime_end_time) {
1452  lease->heap_index);
1453  } else {
1455  lease->heap_index);
1456  }
1457  return ISC_R_SUCCESS;
1458  } else if (lease->state == FTS_ABANDONED) {
1459  char tmp_addr[INET6_ADDRSTRLEN];
1460  lease->state = FTS_ACTIVE;
1462  log_info("Reclaiming previously abandoned address %s",
1463  inet_ntop(AF_INET6, &(lease->addr), tmp_addr,
1464  sizeof(tmp_addr)));
1465 
1466  pool->num_abandoned--;
1467  if (pool->ipv6_pond)
1468  pool->ipv6_pond->num_abandoned--;
1469 
1470  return ISC_R_SUCCESS;
1471  } else {
1472  return move_lease_to_active(pool, lease);
1473  }
1474 }
1475 
1476 /*
1477  * Put the lease on our inactive pool, with the specified state.
1478  */
1479 static isc_result_t
1480 move_lease_to_inactive(struct ipv6_pool *pool, struct iasubopt *lease,
1482  isc_result_t insert_result;
1483  int old_heap_index;
1484 
1485  old_heap_index = lease->heap_index;
1486  insert_result = isc_heap_insert(pool->inactive_timeouts, lease);
1487  if (insert_result == ISC_R_SUCCESS) {
1488  /*
1489  * Handle expire and release statements
1490  * To get here we must be active and have done a commit so
1491  * we should run the proper statements if they exist, though
1492  * that will change when we remove the inactive heap.
1493  * In addition we get rid of the references for both as we
1494  * can only do one (expire or release) on a lease
1495  */
1496  if (lease->on_star.on_expiry != NULL) {
1497  if (state == FTS_EXPIRED) {
1498  execute_statements(NULL, NULL, NULL,
1499  NULL, NULL, NULL,
1500  &lease->scope,
1501  lease->on_star.on_expiry,
1502  &lease->on_star);
1503  }
1505  (&lease->on_star.on_expiry, MDL);
1506  }
1507 
1508  if (lease->on_star.on_release != NULL) {
1509  if (state == FTS_RELEASED) {
1510  execute_statements(NULL, NULL, NULL,
1511  NULL, NULL, NULL,
1512  &lease->scope,
1513  lease->on_star.on_release,
1514  &lease->on_star);
1515  }
1517  (&lease->on_star.on_release, MDL);
1518  }
1519 
1520 #if defined (NSUPDATE)
1521  /* Process events upon expiration. */
1522  if (pool->pool_type != D6O_IA_PD) {
1523  (void) ddns_removals(NULL, lease, NULL, ISC_FALSE);
1524  }
1525 #endif
1526 
1527  /* Binding scopes are no longer valid after expiry or
1528  * release.
1529  */
1530  if (lease->scope != NULL) {
1532  }
1533 
1534  iasubopt_hash_delete(pool->leases,
1535  &lease->addr, sizeof(lease->addr), MDL);
1536  isc_heap_delete(pool->active_timeouts, old_heap_index);
1537  lease->state = state;
1538  pool->num_active--;
1539  pool->num_inactive++;
1540  if (pool->ipv6_pond)
1541  pool->ipv6_pond->num_active--;
1542 
1543  if (lease->state == FTS_ABANDONED) {
1544  pool->num_abandoned--;
1545  if (pool->ipv6_pond)
1546  pool->ipv6_pond->num_abandoned--;
1547  }
1548  }
1549  return insert_result;
1550 }
1551 
1552 /*
1553  * Expire the oldest lease if it's lifetime_end_time is
1554  * older than the given time.
1555  *
1556  * - leasep must be a pointer to a (struct iasubopt *) pointer previously
1557  * initialized to NULL
1558  *
1559  * On return leasep has a reference to the removed entry. It is left
1560  * pointing to NULL if the oldest lease has not expired.
1561  */
1562 isc_result_t
1563 expire_lease6(struct iasubopt **leasep, struct ipv6_pool *pool, time_t now) {
1564  struct iasubopt *tmp;
1565  isc_result_t result;
1566 
1567  if (leasep == NULL) {
1568  log_error("%s(%d): NULL pointer reference", MDL);
1569  return DHCP_R_INVALIDARG;
1570  }
1571  if (*leasep != NULL) {
1572  log_error("%s(%d): non-NULL pointer", MDL);
1573  return DHCP_R_INVALIDARG;
1574  }
1575 
1576  if (pool->num_active > 0) {
1577  tmp = (struct iasubopt *)
1579  if (now > tmp->hard_lifetime_end_time) {
1580  result = move_lease_to_inactive(pool, tmp,
1581  FTS_EXPIRED);
1582  if (result == ISC_R_SUCCESS) {
1583  iasubopt_reference(leasep, tmp, MDL);
1584  }
1585  return result;
1586  }
1587  }
1588  return ISC_R_SUCCESS;
1589 }
1590 
1591 
1592 /*
1593  * For a declined lease, leave it on the "active" pool, but mark
1594  * it as declined. Give it an infinite (well, really long) life.
1595  */
1596 isc_result_t
1597 decline_lease6(struct ipv6_pool *pool, struct iasubopt *lease) {
1598  isc_result_t result;
1599 
1600  if ((lease->state != FTS_ACTIVE) &&
1601  (lease->state != FTS_ABANDONED)) {
1602  result = move_lease_to_active(pool, lease);
1603  if (result != ISC_R_SUCCESS) {
1604  return result;
1605  }
1606  }
1607  lease->state = FTS_ABANDONED;
1608 
1609  pool->num_abandoned++;
1610  if (pool->ipv6_pond)
1611  pool->ipv6_pond->num_abandoned++;
1612 
1615  return ISC_R_SUCCESS;
1616 }
1617 
1618 /*
1619  * Put the returned lease on our inactive pool.
1620  */
1621 isc_result_t
1622 release_lease6(struct ipv6_pool *pool, struct iasubopt *lease) {
1623  if (lease->state == FTS_ACTIVE) {
1624  return move_lease_to_inactive(pool, lease, FTS_RELEASED);
1625  } else {
1626  return ISC_R_SUCCESS;
1627  }
1628 }
1629 
1630 /*
1631  * Create a prefix by hashing the input, and using that for
1632  * the part subject to allocation.
1633  */
1634 void
1635 build_prefix6(struct in6_addr *pref,
1636  const struct in6_addr *net_start_pref,
1637  int pool_bits, int pref_bits,
1638  const struct data_string *input) {
1639  isc_md5_t ctx;
1640  int net_bytes;
1641  int i;
1642  char *str;
1643  const char *net_str;
1644 
1645  /*
1646  * Use MD5 to get a nice 128 bit hash of the input.
1647  * Yes, we know MD5 isn't cryptographically sound.
1648  * No, we don't care.
1649  */
1650  isc_md5_init(&ctx);
1651  isc_md5_update(&ctx, input->data, input->len);
1652  isc_md5_final(&ctx, (unsigned char *)pref);
1653 
1654  /*
1655  * Copy the network bits over.
1656  */
1657  str = (char *)pref;
1658  net_str = (const char *)net_start_pref;
1659  net_bytes = pool_bits / 8;
1660  for (i = 0; i < net_bytes; i++) {
1661  str[i] = net_str[i];
1662  }
1663  i = net_bytes;
1664  switch (pool_bits % 8) {
1665  case 1: str[i] = (str[i] & 0x7F) | (net_str[i] & 0x80); break;
1666  case 2: str[i] = (str[i] & 0x3F) | (net_str[i] & 0xC0); break;
1667  case 3: str[i] = (str[i] & 0x1F) | (net_str[i] & 0xE0); break;
1668  case 4: str[i] = (str[i] & 0x0F) | (net_str[i] & 0xF0); break;
1669  case 5: str[i] = (str[i] & 0x07) | (net_str[i] & 0xF8); break;
1670  case 6: str[i] = (str[i] & 0x03) | (net_str[i] & 0xFC); break;
1671  case 7: str[i] = (str[i] & 0x01) | (net_str[i] & 0xFE); break;
1672  }
1673  /*
1674  * Zero the remaining bits.
1675  */
1676  net_bytes = pref_bits / 8;
1677  for (i=net_bytes+1; i<16; i++) {
1678  str[i] = 0;
1679  }
1680  i = net_bytes;
1681  switch (pref_bits % 8) {
1682  case 0: str[i] &= 0; break;
1683  case 1: str[i] &= 0x80; break;
1684  case 2: str[i] &= 0xC0; break;
1685  case 3: str[i] &= 0xE0; break;
1686  case 4: str[i] &= 0xF0; break;
1687  case 5: str[i] &= 0xF8; break;
1688  case 6: str[i] &= 0xFC; break;
1689  case 7: str[i] &= 0xFE; break;
1690  }
1691 }
1692 
1693 /*
1694  * Create a lease for the given prefix and client duid.
1695  *
1696  * - pool must be a pointer to a (struct ipv6_pool *) pointer previously
1697  * initialized to NULL
1698  *
1699  * Right now we simply hash the DUID, and if we get a collision, we hash
1700  * again until we find a free prefix. We try this a fixed number of times,
1701  * to avoid getting stuck in a loop (this is important on small pools
1702  * where we can run out of space).
1703  *
1704  * We return the number of attempts that it took to find an available
1705  * prefix. This tells callers when a pool is are filling up, as
1706  * well as an indication of how full the pool is; statistically the
1707  * more full a pool is the more attempts must be made before finding
1708  * a free prefix. Realistically this will only happen in very full
1709  * pools.
1710  *
1711  * We probably want different algorithms depending on the network size, in
1712  * the long term.
1713  */
1714 isc_result_t
1715 create_prefix6(struct ipv6_pool *pool, struct iasubopt **pref,
1716  unsigned int *attempts,
1717  const struct data_string *uid,
1718  time_t soft_lifetime_end_time) {
1719  struct data_string ds;
1720  struct in6_addr tmp;
1721  struct iasubopt *test_iapref;
1722  struct data_string new_ds;
1723  struct iasubopt *iapref;
1724  isc_result_t result;
1725 
1726  /*
1727  * Use the UID as our initial seed for the hash
1728  */
1729  memset(&ds, 0, sizeof(ds));
1730  data_string_copy(&ds, (struct data_string *)uid, MDL);
1731 
1732  *attempts = 0;
1733  for (;;) {
1734  /*
1735  * Give up at some point.
1736  */
1737  if (++(*attempts) > 10) {
1738  data_string_forget(&ds, MDL);
1739  return ISC_R_NORESOURCES;
1740  }
1741 
1742  /*
1743  * Build a prefix
1744  */
1745  build_prefix6(&tmp, &pool->start_addr,
1746  pool->bits, pool->units, &ds);
1747 
1748  /*
1749  * If this prefix is not in use, we're happy with it
1750  */
1751  test_iapref = NULL;
1752  if (iasubopt_hash_lookup(&test_iapref, pool->leases,
1753  &tmp, sizeof(tmp), MDL) == 0) {
1754  break;
1755  }
1756  iasubopt_dereference(&test_iapref, MDL);
1757 
1758  /*
1759  * Otherwise, we create a new input, adding the prefix
1760  */
1761  memset(&new_ds, 0, sizeof(new_ds));
1762  new_ds.len = ds.len + sizeof(tmp);
1763  if (!buffer_allocate(&new_ds.buffer, new_ds.len, MDL)) {
1764  data_string_forget(&ds, MDL);
1765  return ISC_R_NOMEMORY;
1766  }
1767  new_ds.data = new_ds.buffer->data;
1768  memcpy(new_ds.buffer->data, ds.data, ds.len);
1769  memcpy(new_ds.buffer->data + ds.len, &tmp, sizeof(tmp));
1770  data_string_forget(&ds, MDL);
1771  data_string_copy(&ds, &new_ds, MDL);
1772  data_string_forget(&new_ds, MDL);
1773  }
1774 
1775  data_string_forget(&ds, MDL);
1776 
1777  /*
1778  * We're happy with the prefix, create an IAPREFIX
1779  * to hold it.
1780  */
1781  iapref = NULL;
1782  result = iasubopt_allocate(&iapref, MDL);
1783  if (result != ISC_R_SUCCESS) {
1784  return result;
1785  }
1786  iapref->plen = (u_int8_t)pool->units;
1787  memcpy(&iapref->addr, &tmp, sizeof(iapref->addr));
1788 
1789  /*
1790  * Add the prefix to the pool (note state is free, not active?!).
1791  */
1792  result = add_lease6(pool, iapref, soft_lifetime_end_time);
1793  if (result == ISC_R_SUCCESS) {
1794  iasubopt_reference(pref, iapref, MDL);
1795  }
1796  iasubopt_dereference(&iapref, MDL);
1797  return result;
1798 }
1799 
1800 /*
1801  * Determine if a prefix is present in a pool or not.
1802  */
1803 isc_boolean_t
1804 prefix6_exists(const struct ipv6_pool *pool,
1805  const struct in6_addr *pref, u_int8_t plen) {
1806  struct iasubopt *test_iapref;
1807 
1808  if ((int)plen != pool->units)
1809  return ISC_FALSE;
1810 
1811  test_iapref = NULL;
1812  if (iasubopt_hash_lookup(&test_iapref, pool->leases,
1813  (void *)pref, sizeof(*pref), MDL)) {
1814  iasubopt_dereference(&test_iapref, MDL);
1815  return ISC_TRUE;
1816  } else {
1817  return ISC_FALSE;
1818  }
1819 }
1820 
1821 /*
1822  * Mark an IPv6 address/prefix as unavailable from a pool.
1823  *
1824  * This is used for host entries and the addresses of the server itself.
1825  */
1826 isc_result_t
1827 mark_lease_unavailable(struct ipv6_pool *pool, const struct in6_addr *addr) {
1828  struct iasubopt *dummy_iasubopt;
1829  isc_result_t result;
1830 
1831  dummy_iasubopt = NULL;
1832  result = iasubopt_allocate(&dummy_iasubopt, MDL);
1833  if (result == ISC_R_SUCCESS) {
1834  dummy_iasubopt->addr = *addr;
1835  iasubopt_hash_add(pool->leases, &dummy_iasubopt->addr,
1836  sizeof(*addr), dummy_iasubopt, MDL);
1837  }
1838  return result;
1839 }
1840 
1841 /*
1842  * Add a pool.
1843  */
1844 isc_result_t
1845 add_ipv6_pool(struct ipv6_pool *pool) {
1846  struct ipv6_pool **new_pools;
1847 
1848  new_pools = dmalloc(sizeof(struct ipv6_pool *) * (num_pools+1), MDL);
1849  if (new_pools == NULL) {
1850  return ISC_R_NOMEMORY;
1851  }
1852 
1853  if (num_pools > 0) {
1854  memcpy(new_pools, pools,
1855  sizeof(struct ipv6_pool *) * num_pools);
1856  dfree(pools, MDL);
1857  }
1858  pools = new_pools;
1859 
1860  pools[num_pools] = NULL;
1862  num_pools++;
1863  return ISC_R_SUCCESS;
1864 }
1865 
1866 static void
1867 cleanup_old_expired(struct ipv6_pool *pool) {
1868  struct iasubopt *tmp;
1869  struct ia_xx *ia;
1870  struct ia_xx *ia_active;
1871  unsigned char *tmpd;
1872  time_t timeout;
1873 
1874  while (pool->num_inactive > 0) {
1875  tmp = (struct iasubopt *)
1877  if (tmp->hard_lifetime_end_time != 0) {
1878  timeout = tmp->hard_lifetime_end_time;
1879  timeout += EXPIRED_IPV6_CLEANUP_TIME;
1880  } else {
1881  timeout = tmp->soft_lifetime_end_time;
1882  }
1883  if (cur_time < timeout) {
1884  break;
1885  }
1886 
1888  pool->num_inactive--;
1889 
1890  if (tmp->ia != NULL) {
1891  /*
1892  * Check to see if this IA is in an active list,
1893  * but has no remaining resources. If so, remove it
1894  * from the active list.
1895  */
1896  ia = NULL;
1897  ia_reference(&ia, tmp->ia, MDL);
1898  ia_remove_iasubopt(ia, tmp, MDL);
1899  ia_active = NULL;
1900  tmpd = (unsigned char *)ia->iaid_duid.data;
1901  if ((ia->ia_type == D6O_IA_NA) &&
1902  (ia->num_iasubopt <= 0) &&
1903  (ia_hash_lookup(&ia_active, ia_na_active, tmpd,
1904  ia->iaid_duid.len, MDL) == 0) &&
1905  (ia_active == ia)) {
1906  ia_hash_delete(ia_na_active, tmpd,
1907  ia->iaid_duid.len, MDL);
1908  }
1909  if ((ia->ia_type == D6O_IA_TA) &&
1910  (ia->num_iasubopt <= 0) &&
1911  (ia_hash_lookup(&ia_active, ia_ta_active, tmpd,
1912  ia->iaid_duid.len, MDL) == 0) &&
1913  (ia_active == ia)) {
1914  ia_hash_delete(ia_ta_active, tmpd,
1915  ia->iaid_duid.len, MDL);
1916  }
1917  if ((ia->ia_type == D6O_IA_PD) &&
1918  (ia->num_iasubopt <= 0) &&
1919  (ia_hash_lookup(&ia_active, ia_pd_active, tmpd,
1920  ia->iaid_duid.len, MDL) == 0) &&
1921  (ia_active == ia)) {
1922  ia_hash_delete(ia_pd_active, tmpd,
1923  ia->iaid_duid.len, MDL);
1924  }
1925  ia_dereference(&ia, MDL);
1926  }
1927  iasubopt_dereference(&tmp, MDL);
1928  }
1929 }
1930 
1931 static void
1932 lease_timeout_support(void *vpool) {
1933  struct ipv6_pool *pool;
1934  struct iasubopt *lease;
1935 
1936  pool = (struct ipv6_pool *)vpool;
1937  for (;;) {
1938  /*
1939  * Get the next lease scheduled to expire.
1940  *
1941  * Note that if there are no leases in the pool,
1942  * expire_lease6() will return ISC_R_SUCCESS with
1943  * a NULL lease.
1944  *
1945  * expire_lease6() will call move_lease_to_inactive() which
1946  * calls ddns_removals() do we want that on the standard
1947  * expiration timer or a special 'depref' timer? Original
1948  * query from DH, moved here by SAR.
1949  */
1950  lease = NULL;
1951  if (expire_lease6(&lease, pool, cur_time) != ISC_R_SUCCESS) {
1952  break;
1953  }
1954  if (lease == NULL) {
1955  break;
1956  }
1957 
1958  write_ia(lease->ia);
1959 
1960  iasubopt_dereference(&lease, MDL);
1961  }
1962 
1963  /*
1964  * If appropriate commit and rotate the lease file
1965  * As commit_leases_timed() checks to see if we've done any writes
1966  * we don't bother tracking if this function called write _ia
1967  */
1968  (void) commit_leases_timed();
1969 
1970  /*
1971  * Do some cleanup of our expired leases.
1972  */
1973  cleanup_old_expired(pool);
1974 
1975  /*
1976  * Schedule next round of expirations.
1977  */
1978  schedule_lease_timeout(pool);
1979 }
1980 
1981 /*
1982  * For a given pool, add a timer that will remove the next
1983  * lease to expire.
1984  */
1985 void
1987  struct iasubopt *tmp;
1988  time_t timeout;
1989  time_t next_timeout;
1990  struct timeval tv;
1991 
1992  next_timeout = MAX_TIME;
1993 
1994  if (pool->num_active > 0) {
1995  tmp = (struct iasubopt *)
1997  if (tmp->hard_lifetime_end_time < next_timeout) {
1998  next_timeout = tmp->hard_lifetime_end_time + 1;
1999  }
2000  }
2001 
2002  if (pool->num_inactive > 0) {
2003  tmp = (struct iasubopt *)
2005  if (tmp->hard_lifetime_end_time != 0) {
2006  timeout = tmp->hard_lifetime_end_time;
2007  timeout += EXPIRED_IPV6_CLEANUP_TIME;
2008  } else {
2009  timeout = tmp->soft_lifetime_end_time + 1;
2010  }
2011  if (timeout < next_timeout) {
2012  next_timeout = timeout;
2013  }
2014  }
2015 
2016  if (next_timeout < MAX_TIME) {
2017  tv.tv_sec = next_timeout;
2018  tv.tv_usec = 0;
2019  add_timeout(&tv, lease_timeout_support, pool,
2022  }
2023 }
2024 
2025 /*
2026  * Schedule timeouts across all pools.
2027  */
2028 void
2030  int i;
2031 
2032  for (i=0; i<num_pools; i++) {
2034  }
2035 }
2036 
2037 /*
2038  * Given an address and the length of the network mask, return
2039  * only the network portion.
2040  *
2041  * Examples:
2042  *
2043  * "fe80::216:6fff:fe49:7d9b", length 64 = "fe80::"
2044  * "2001:888:1936:2:216:6fff:fe49:7d9b", length 48 = "2001:888:1936::"
2045  */
2046 static void
2047 ipv6_network_portion(struct in6_addr *result,
2048  const struct in6_addr *addr, int bits) {
2049  unsigned char *addrp;
2050  int mask_bits;
2051  int bytes;
2052  int extra_bits;
2053  int i;
2054 
2055  static const unsigned char bitmasks[] = {
2056  0x00, 0xFE, 0xFC, 0xF8,
2057  0xF0, 0xE0, 0xC0, 0x80,
2058  };
2059 
2060  /*
2061  * Sanity check our bits. ;)
2062  */
2063  if ((bits < 0) || (bits > 128)) {
2064  log_fatal("ipv6_network_portion: bits %d not between 0 and 128",
2065  bits);
2066  }
2067 
2068  /*
2069  * Copy our address portion.
2070  */
2071  *result = *addr;
2072  addrp = ((unsigned char *)result) + 15;
2073 
2074  /*
2075  * Zero out masked portion.
2076  */
2077  mask_bits = 128 - bits;
2078  bytes = mask_bits / 8;
2079  extra_bits = mask_bits % 8;
2080 
2081  for (i=0; i<bytes; i++) {
2082  *addrp = 0;
2083  addrp--;
2084  }
2085  if (extra_bits) {
2086  *addrp &= bitmasks[extra_bits];
2087  }
2088 }
2089 
2090 /*
2091  * Determine if the given address/prefix is in the pool.
2092  */
2093 isc_boolean_t
2094 ipv6_in_pool(const struct in6_addr *addr, const struct ipv6_pool *pool) {
2095  struct in6_addr tmp;
2096 
2097  ipv6_network_portion(&tmp, addr, pool->bits);
2098  if (memcmp(&tmp, &pool->start_addr, sizeof(tmp)) == 0) {
2099  return ISC_TRUE;
2100  } else {
2101  return ISC_FALSE;
2102  }
2103 }
2104 
2105 /*
2106  * Find the pool that contains the given address.
2107  *
2108  * - pool must be a pointer to a (struct ipv6_pool *) pointer previously
2109  * initialized to NULL
2110  */
2111 isc_result_t
2112 find_ipv6_pool(struct ipv6_pool **pool, u_int16_t type,
2113  const struct in6_addr *addr) {
2114  int i;
2115 
2116  if (pool == NULL) {
2117  log_error("%s(%d): NULL pointer reference", MDL);
2118  return DHCP_R_INVALIDARG;
2119  }
2120  if (*pool != NULL) {
2121  log_error("%s(%d): non-NULL pointer", MDL);
2122  return DHCP_R_INVALIDARG;
2123  }
2124 
2125  for (i=0; i<num_pools; i++) {
2126  if (pools[i]->pool_type != type)
2127  continue;
2128  if (ipv6_in_pool(addr, pools[i])) {
2129  ipv6_pool_reference(pool, pools[i], MDL);
2130  return ISC_R_SUCCESS;
2131  }
2132  }
2133  return ISC_R_NOTFOUND;
2134 }
2135 
2136 /*
2137  * Helper function for the various functions that act across all
2138  * pools.
2139  */
2140 static isc_result_t
2141 change_leases(struct ia_xx *ia,
2142  isc_result_t (*change_func)(struct ipv6_pool *,
2143  struct iasubopt *)) {
2144  isc_result_t retval;
2145  isc_result_t renew_retval;
2146  struct ipv6_pool *pool;
2147  struct in6_addr *addr;
2148  int i;
2149 
2150  retval = ISC_R_SUCCESS;
2151  for (i=0; i<ia->num_iasubopt; i++) {
2152  pool = NULL;
2153  addr = &ia->iasubopt[i]->addr;
2154  if (find_ipv6_pool(&pool, ia->ia_type,
2155  addr) == ISC_R_SUCCESS) {
2156  renew_retval = change_func(pool, ia->iasubopt[i]);
2157  if (renew_retval != ISC_R_SUCCESS) {
2158  retval = renew_retval;
2159  }
2160  }
2161  /* XXXsk: should we warn if we don't find a pool? */
2162  }
2163  return retval;
2164 }
2165 
2166 /*
2167  * Renew all leases in an IA from all pools.
2168  *
2169  * The new lifetime should be in the soft_lifetime_end_time
2170  * and will be moved to hard_lifetime_end_time by renew_lease6.
2171  */
2172 isc_result_t
2173 renew_leases(struct ia_xx *ia) {
2174  return change_leases(ia, renew_lease6);
2175 }
2176 
2177 /*
2178  * Release all leases in an IA from all pools.
2179  */
2180 isc_result_t
2181 release_leases(struct ia_xx *ia) {
2182  return change_leases(ia, release_lease6);
2183 }
2184 
2185 /*
2186  * Decline all leases in an IA from all pools.
2187  */
2188 isc_result_t
2189 decline_leases(struct ia_xx *ia) {
2190  return change_leases(ia, decline_lease6);
2191 }
2192 
2193 #ifdef DHCPv6
2194 /*
2195  * Helper function to output leases.
2196  */
2197 static int write_error;
2198 
2199 static isc_result_t
2200 write_ia_leases(const void *name, unsigned len, void *value) {
2201  struct ia_xx *ia = (struct ia_xx *)value;
2202 
2203  if (!write_error) {
2204  if (!write_ia(ia)) {
2205  write_error = 1;
2206  }
2207  }
2208  return ISC_R_SUCCESS;
2209 }
2210 
2211 /*
2212  * Write all DHCPv6 information.
2213  */
2214 int
2215 write_leases6(void) {
2216  int nas, tas, pds;
2217 
2218  write_error = 0;
2220  nas = ia_hash_foreach(ia_na_active, write_ia_leases);
2221  if (write_error) {
2222  return 0;
2223  }
2224  tas = ia_hash_foreach(ia_ta_active, write_ia_leases);
2225  if (write_error) {
2226  return 0;
2227  }
2228  pds = ia_hash_foreach(ia_pd_active, write_ia_leases);
2229  if (write_error) {
2230  return 0;
2231  }
2232 
2233  log_info("Wrote %d NA, %d TA, %d PD leases to lease file.",
2234  nas, tas, pds);
2235  return 1;
2236 }
2237 #endif /* DHCPv6 */
2238 
2239 static isc_result_t
2240 mark_hosts_unavailable_support(const void *name, unsigned len, void *value) {
2241  struct host_decl *h;
2242  struct data_string fixed_addr;
2243  struct in6_addr addr;
2244  struct ipv6_pool *p;
2245 
2246  h = (struct host_decl *)value;
2247 
2248  /*
2249  * If the host has no address, we don't need to mark anything.
2250  */
2251  if (h->fixed_addr == NULL) {
2252  return ISC_R_SUCCESS;
2253  }
2254 
2255  /*
2256  * Evaluate the fixed address.
2257  */
2258  memset(&fixed_addr, 0, sizeof(fixed_addr));
2259  if (!evaluate_option_cache(&fixed_addr, NULL, NULL, NULL, NULL, NULL,
2260  &global_scope, h->fixed_addr, MDL)) {
2261  log_error("mark_hosts_unavailable: "
2262  "error evaluating host address.");
2263  return ISC_R_SUCCESS;
2264  }
2265  if (fixed_addr.len != 16) {
2266  log_error("mark_hosts_unavailable: "
2267  "host address is not 128 bits.");
2268  return ISC_R_SUCCESS;
2269  }
2270  memcpy(&addr, fixed_addr.data, 16);
2272 
2273  /*
2274  * Find the pool holding this host, and mark the address.
2275  * (I suppose it is arguably valid to have a host that does not
2276  * sit in any pool.)
2277  */
2278  p = NULL;
2279  if (find_ipv6_pool(&p, D6O_IA_NA, &addr) == ISC_R_SUCCESS) {
2280  mark_lease_unavailable(p, &addr);
2282  }
2283  if (find_ipv6_pool(&p, D6O_IA_TA, &addr) == ISC_R_SUCCESS) {
2284  mark_lease_unavailable(p, &addr);
2286  }
2287 
2288  return ISC_R_SUCCESS;
2289 }
2290 
2291 void
2293  hash_foreach(host_name_hash, mark_hosts_unavailable_support);
2294 }
2295 
2296 static isc_result_t
2297 mark_phosts_unavailable_support(const void *name, unsigned len, void *value) {
2298  struct host_decl *h;
2299  struct iaddrcidrnetlist *l;
2300  struct in6_addr pref;
2301  struct ipv6_pool *p;
2302 
2303  h = (struct host_decl *)value;
2304 
2305  /*
2306  * If the host has no prefix, we don't need to mark anything.
2307  */
2308  if (h->fixed_prefix == NULL) {
2309  return ISC_R_SUCCESS;
2310  }
2311 
2312  /*
2313  * Get the fixed prefixes.
2314  */
2315  for (l = h->fixed_prefix; l != NULL; l = l->next) {
2316  if (l->cidrnet.lo_addr.len != 16) {
2317  continue;
2318  }
2319  memcpy(&pref, l->cidrnet.lo_addr.iabuf, 16);
2320 
2321  /*
2322  * Find the pool holding this host, and mark the prefix.
2323  * (I suppose it is arguably valid to have a host that does not
2324  * sit in any pool.)
2325  */
2326  p = NULL;
2327  if (find_ipv6_pool(&p, D6O_IA_PD, &pref) != ISC_R_SUCCESS) {
2328  continue;
2329  }
2330  if (l->cidrnet.bits != p->units) {
2332  continue;
2333  }
2334  mark_lease_unavailable(p, &pref);
2336  }
2337 
2338  return ISC_R_SUCCESS;
2339 }
2340 
2341 void
2343  hash_foreach(host_name_hash, mark_phosts_unavailable_support);
2344 }
2345 
2346 void
2348  struct interface_info *ip;
2349  int i;
2350  struct ipv6_pool *p;
2351 
2352  ip = interfaces;
2353  while (ip != NULL) {
2354  for (i=0; i<ip->v6address_count; i++) {
2355  p = NULL;
2356  if (find_ipv6_pool(&p, D6O_IA_NA, &ip->v6addresses[i])
2357  == ISC_R_SUCCESS) {
2359  &ip->v6addresses[i]);
2361  }
2362  if (find_ipv6_pool(&p, D6O_IA_TA, &ip->v6addresses[i])
2363  == ISC_R_SUCCESS) {
2365  &ip->v6addresses[i]);
2367  }
2368  }
2369  ip = ip->next;
2370  }
2371 }
2372 
2390 isc_result_t
2391 ipv6_pond_allocate(struct ipv6_pond **pond, const char *file, int line) {
2392  struct ipv6_pond *tmp;
2393 
2394  if (pond == NULL) {
2395  log_error("%s(%d): NULL pointer reference", file, line);
2396  return DHCP_R_INVALIDARG;
2397  }
2398  if (*pond != NULL) {
2399  log_error("%s(%d): non-NULL pointer", file, line);
2400  return DHCP_R_INVALIDARG;
2401  }
2402 
2403  tmp = dmalloc(sizeof(*tmp), file, line);
2404  if (tmp == NULL) {
2405  return ISC_R_NOMEMORY;
2406  }
2407 
2408  tmp->refcnt = 1;
2409 
2410  *pond = tmp;
2411  return ISC_R_SUCCESS;
2412 }
2413 
2433 isc_result_t
2434 ipv6_pond_reference(struct ipv6_pond **pond, struct ipv6_pond *src,
2435  const char *file, int line) {
2436  if (pond == NULL) {
2437  log_error("%s(%d): NULL pointer reference", file, line);
2438  return DHCP_R_INVALIDARG;
2439  }
2440  if (*pond != NULL) {
2441  log_error("%s(%d): non-NULL pointer", file, line);
2442  return DHCP_R_INVALIDARG;
2443  }
2444  if (src == NULL) {
2445  log_error("%s(%d): NULL pointer reference", file, line);
2446  return DHCP_R_INVALIDARG;
2447  }
2448  *pond = src;
2449  src->refcnt++;
2450  return ISC_R_SUCCESS;
2451 }
2452 
2473 isc_result_t
2474 ipv6_pond_dereference(struct ipv6_pond **pond, const char *file, int line) {
2475  struct ipv6_pond *tmp;
2476 
2477  if ((pond == NULL) || (*pond == NULL)) {
2478  log_error("%s(%d): NULL pointer", file, line);
2479  return DHCP_R_INVALIDARG;
2480  }
2481 
2482  tmp = *pond;
2483  *pond = NULL;
2484 
2485  tmp->refcnt--;
2486  if (tmp->refcnt < 0) {
2487  log_error("%s(%d): negative refcnt", file, line);
2488  tmp->refcnt = 0;
2489  }
2490  if (tmp->refcnt == 0) {
2491  dfree(tmp, file, line);
2492  }
2493 
2494  return ISC_R_SUCCESS;
2495 }
2496 
2497 /*
2498  * Emits a log for each pond that has been flagged as being a "jumbo range"
2499  * A pond is considered a "jumbo range" when the total number of elements
2500  * exceeds the maximum value of POND_TRACK_MAX (currently maximum value
2501  * that can be stored by ipv6_pond.num_total). Since we disable threshold
2502  * logging for jumbo ranges, we need to report this to the user. This
2503  * function allows us to report jumbo ponds after config parsing, so the
2504  * logs can be seen both on the console (-T) and the log facility (i.e syslog).
2505  *
2506  * Note, threshold logging is done at the pond level, so we need emit a list
2507  * of the addresses ranges of the pools in the pond affected.
2508  */
2509 void
2511  struct shared_network* s;
2512  char log_buf[1084];
2513 
2514  /* Loop thru all the networks looking for jumbo range ponds */
2515  for (s = shared_networks; s; s = s -> next) {
2516  struct ipv6_pond* pond = s->ipv6_pond;
2517  while (pond) {
2518  /* if its a jumbo and has pools(sanity check) */
2519  if (pond->jumbo_range == 1 && (pond->ipv6_pools)) {
2520  struct ipv6_pool* pool;
2521  char *bufptr = log_buf;
2522  size_t space_left = sizeof(log_buf) - 1;
2523  int i = 0;
2524  int used = 0;
2525 
2526  /* Build list containing the start-address/CIDR
2527  * of each pool */
2528  *bufptr = '\0';
2529  while ((pool = pond->ipv6_pools[i++]) &&
2530  (space_left > (INET6_ADDRSTRLEN + 6))) {
2531  /* more than one so add a comma */
2532  if (i > 1) {
2533  *bufptr++ = ',';
2534  *bufptr++ = ' ';
2535  *bufptr = '\0';
2536  space_left -= 2;
2537  }
2538 
2539  /* add the address */
2540  inet_ntop(AF_INET6, &pool->start_addr,
2541  bufptr, INET6_ADDRSTRLEN);
2542 
2543  used = strlen(bufptr);
2544  bufptr += used;
2545  space_left -= used;
2546 
2547  /* add the CIDR */
2548  sprintf (bufptr, "/%d",pool->bits);
2549  used = strlen(bufptr);
2550  bufptr += used;
2551  space_left -= used;
2552  *bufptr = '\0';
2553  }
2554 
2555  log_info("Threshold logging disabled for shared"
2556  " subnet of ranges: %s", log_buf);
2557  }
2558  pond = pond->next;
2559  }
2560  }
2561 }
2562 
2563 /* unittest moved to server/tests/mdb6_unittest.c */
#define FTS_ABANDONED
Definition: dhcpd.h:531
struct iaddrcidrnet cidrnet
Definition: inet.h:77
void mark_interfaces_unavailable(void)
Definition: mdb6.c:2347
ia_hash_t * ia_ta_active
struct ipv6_pond * next
Definition: dhcpd.h:1686
isc_boolean_t lease6_usable(struct iasubopt *lease)
Check if address is available to a lease.
Definition: mdb6.c:1372
const char int line
Definition: dhcpd.h:3676
isc_result_t mark_lease_unavailable(struct ipv6_pool *pool, const struct in6_addr *addr)
Definition: mdb6.c:1827
struct binding_scope * global_scope
Definition: tree.c:39
isc_boolean_t prefix6_exists(const struct ipv6_pool *pool, const struct in6_addr *pref, u_int8_t plen)
Definition: mdb6.c:1804
void report_jumbo_ranges()
Definition: mdb6.c:2510
Definition: dhcpd.h:550
unsigned len
Definition: tree.h:80
int executable_statement_dereference(struct executable_statement **ptr, const char *file, int line)
Definition: execute.c:623
isc_uint64_t num_active
Definition: dhcpd.h:1699
int bits
Definition: inet.h:72
#define FTS_FREE
Definition: dhcpd.h:527
struct shared_network * shared_networks
Definition: mdb.c:34
Definition: dhcpd.h:1620
isc_result_t create_prefix6(struct ipv6_pool *pool, struct iasubopt **pref, unsigned int *attempts, const struct data_string *uid, time_t soft_lifetime_end_time)
Definition: mdb6.c:1715
int units
Definition: dhcpd.h:1659
int max_iasubopt
Definition: dhcpd.h:1625
void * dmalloc(unsigned, const char *, int)
Definition: alloc.c:56
isc_result_t renew_lease6(struct ipv6_pool *pool, struct iasubopt *lease)
Renew a lease in the pool.
Definition: mdb6.c:1444
int execute_statements(struct binding_value **result, struct packet *packet, struct lease *lease, struct client_state *client_state, struct option_state *in_options, struct option_state *out_options, struct binding_scope **scope, struct executable_statement *statements, struct on_star *on_star)
Definition: execute.c:35
isc_result_t ia_make_key(struct data_string *key, u_int32_t iaid, const char *duid, unsigned int duid_len, const char *file, int line)
Definition: mdb6.c:310
struct ipv6_pond * ipv6_pond
Definition: dhcpd.h:1024
isc_result_t iasubopt_dereference(struct iasubopt **iasubopt, const char *file, int line)
Definition: mdb6.c:260
#define MDL
Definition: omapip.h:568
int heap_index
Definition: dhcpd.h:1605
unsigned char iabuf[16]
Definition: inet.h:33
dhcp_context_t dhcp_gbl_ctx
Definition: isclib.c:33
#define DHCP_R_INVALIDARG
Definition: result.h:48
isc_result_t find_ipv6_pool(struct ipv6_pool **pool, u_int16_t type, const struct in6_addr *addr)
Definition: mdb6.c:2112
#define FTS_RELEASED
Definition: dhcpd.h:530
void build_prefix6(struct in6_addr *pref, const struct in6_addr *net_start_pref, int pool_bits, int pref_bits, const struct data_string *input)
Definition: mdb6.c:1635
struct executable_statement * on_release
Definition: dhcpd.h:546
isc_result_t ia_dereference(struct ia_xx **ia, const char *file, int line)
Definition: mdb6.c:402
void data_string_forget(struct data_string *data, const char *file, int line)
Definition: alloc.c:1340
isc_result_t ia_add_iasubopt(struct ia_xx *ia, struct iasubopt *iasubopt, const char *file, int line)
Definition: mdb6.c:438
struct in6_addr start_addr
Definition: dhcpd.h:1657
struct option_cache * fixed_addr
Definition: dhcpd.h:942
int log_error(const char *,...) __attribute__((__format__(__printf__
isc_result_t release_leases(struct ia_xx *ia)
Definition: mdb6.c:2181
#define FTS_EXPIRED
Definition: dhcpd.h:529
int binding_scope_dereference(struct binding_scope **ptr, const char *file, int line)
Definition: tree.c:3775
int num_inactive
Definition: dhcpd.h:1664
struct on_star on_star
Definition: dhcpd.h:1617
struct binding_scope * scope
Definition: dhcpd.h:1592
struct ipv6_pond * ipv6_pond
Definition: dhcpd.h:1670
void add_timeout(struct timeval *when, void(*)(void *) where, void *what, tvref_t ref, tvunref_t unref)
Definition: dispatch.c:198
void ia_remove_all_lease(struct ia_xx *ia, const char *file, int line)
Definition: mdb6.c:503
unsigned len
Definition: inet.h:32
int refcnt
Definition: dhcpd.h:1685
isc_result_t ipv6_pool_allocate(struct ipv6_pool **pool, u_int16_t type, const struct in6_addr *start_addr, int bits, int units, const char *file, int line)
Create a new IPv6 lease pool structure.
Definition: mdb6.c:635
#define EXPIRED_IPV6_CLEANUP_TIME
Definition: dhcpd.h:1603
isc_result_t isc_heap_create(isc_heapcompare_t compare, isc_heapindex_t index, unsigned int size_increment, isc_heap_t **heapp)
Create a new heap. The heap is implemented using a space-efficient storage method. When the heap elements are deleted space is not freed but will be reused when new elements are inserted.
isc_result_t ia_allocate(struct ia_xx **ia, u_int32_t iaid, const char *duid, unsigned int duid_len, const char *file, int line)
Definition: mdb6.c:338
int write_leases6(void)
void log_fatal(const char *,...) __attribute__((__format__(__printf__
isc_result_t create_lease6(struct ipv6_pool *pool, struct iasubopt **addr, unsigned int *attempts, const struct data_string *uid, time_t soft_lifetime_end_time)
Definition: mdb6.c:953
#define D6O_IA_TA
Definition: dhcp6.h:34
isc_mem_t * mctx
Definition: isclib.h:92
isc_boolean_t lease6_exists(const struct ipv6_pool *pool, const struct in6_addr *addr)
Definition: mdb6.c:1344
void isc_heap_decreased(isc_heap_t *heap, unsigned int index)
Indicates to the heap that an element's priority has decreased. This function MUST be called whenever...
time_t hard_lifetime_end_time
Definition: dhcpd.h:1593
int evaluate_option_cache(struct data_string *result, struct packet *packet, struct lease *lease, struct client_state *client_state, struct option_state *in_options, struct option_state *cfg_options, struct binding_scope **scope, struct option_cache *oc, const char *file, int line)
Definition: tree.c:2688
host_hash_t * host_name_hash
Definition: mdb.c:37
Definition: dhcpd.h:985
unsigned do_string_hash(const void *, unsigned, unsigned)
Definition: hash.c:267
ia_hash_t * ia_na_active
struct ipv6_pool * ipv6_pool
Definition: dhcpd.h:1598
int buffer_allocate(struct buffer **ptr, unsigned len, const char *file, int line)
Definition: alloc.c:680
isc_result_t ipv6_pond_allocate(struct ipv6_pond **pond, const char *file, int line)
Create a new IPv6 pond structure.
Definition: mdb6.c:2391
int write_server_duid(void)
struct iaddrcidrnetlist * next
Definition: inet.h:76
isc_boolean_t ia_equal(const struct ia_xx *a, const struct ia_xx *b)
Definition: mdb6.c:517
u_int8_t plen
Definition: dhcpd.h:1590
struct data_string iaid_duid
Definition: dhcpd.h:1622
#define cur_time
Definition: dhcpd.h:2041
Definition: ip.h:47
void(* tvref_t)(void *, void *, const char *, int)
Definition: dhcpd.h:1395
int refcnt
Definition: dhcpd.h:1621
void dfree(void *, const char *, int)
Definition: alloc.c:131
void isc_heap_foreach(isc_heap_t *heap, isc_heapaction_t action, void *uap)
Iterate over the heap, calling an action for each element. The order of iteration is not sorted...
int bits
Definition: dhcpd.h:1658
int jumbo_range
Definition: dhcpd.h:1703
isc_result_t renew_leases(struct ia_xx *ia)
Definition: mdb6.c:2173
int refcnt
Definition: dhcpd.h:1588
isc_result_t decline_leases(struct ia_xx *ia)
Definition: mdb6.c:2189
iasubopt_hash_t * leases
Definition: dhcpd.h:1660
int num_iasubopt
Definition: dhcpd.h:1624
int int log_info(const char *,...) __attribute__((__format__(__printf__
struct ipv6_pool ** ipv6_pools
Definition: dhcpd.h:1695
u_int16_t ia_type
Definition: dhcpd.h:1623
binding_state_t state
Definition: dhcpd.h:1591
struct interface_info * interfaces
Definition: discover.c:43
isc_result_t ipv6_pool_dereference(struct ipv6_pool **pool, const char *file, int line)
de-reference an IPv6 pool structure.
Definition: mdb6.c:772
int v6address_count
Definition: dhcpd.h:1338
void(* tvunref_t)(void *, const char *, int)
Definition: dhcpd.h:1396
isc_uint64_t num_active
Definition: dhcpd.h:1661
void cleanup(void)
isc_result_t ipv6_pool_reference(struct ipv6_pool **pool, struct ipv6_pool *src, const char *file, int line)
reference an IPv6 pool structure.
Definition: mdb6.c:701
#define DEFAULT_HASH_SIZE
Definition: hash.h:33
ipv6_pool structure
Definition: dhcpd.h:1654
void isc_heap_destroy(isc_heap_t **heapp)
Destroys a heap.
int refcnt
Definition: dhcpd.h:1655
struct iaddrcidrnetlist * fixed_prefix
Definition: dhcpd.h:943
ia_hash_t * ia_pd_active
isc_result_t ddns_removals(struct lease *, struct iasubopt *, struct dhcp_ddns_cb *, isc_boolean_t)
int commit_leases_timed(void)
Definition: db.c:1039
void isc_heap_increased(isc_heap_t *heap, unsigned int index)
Indicates to the heap that an element's priority has increased. This function MUST be called whenever...
void isc_heap_delete(isc_heap_t *heap, unsigned int index)
Deletes an element from a heap, by element index.
int hash_foreach(struct hash_table *, hash_foreach_func)
Definition: hash.c:512
isc_result_t add_lease6(struct ipv6_pool *pool, struct iasubopt *lease, time_t valid_lifetime_end_time)
Definition: mdb6.c:1234
struct interface_info * next
Definition: dhcpd.h:1326
isc_heap_t * inactive_timeouts
Definition: dhcpd.h:1665
#define D6O_IA_NA
Definition: dhcp6.h:33
isc_result_t iasubopt_reference(struct iasubopt **iasubopt, struct iasubopt *src, const char *file, int line)
Definition: mdb6.c:233
isc_uint64_t num_abandoned
Definition: dhcpd.h:1700
HASH_FUNCTIONS(ia, unsigned char *, struct ia_xx, ia_hash_t, ia_reference, ia_dereference, do_string_hash)
Definition: mdb6.c:180
unsigned char data[1]
Definition: tree.h:63
isc_uint64_t num_abandoned
Definition: dhcpd.h:1662
isc_heap_t * active_timeouts
Definition: dhcpd.h:1663
isc_result_t decline_lease6(struct ipv6_pool *pool, struct iasubopt *lease)
Definition: mdb6.c:1597
void schedule_lease_timeout(struct ipv6_pool *pool)
Definition: mdb6.c:1986
time_t soft_lifetime_end_time
Definition: dhcpd.h:1594
struct iaddr lo_addr
Definition: inet.h:71
isc_result_t ipv6_pond_dereference(struct ipv6_pond **pond, const char *file, int line)
de-reference an IPv6 pond structure.
Definition: mdb6.c:2474
isc_boolean_t ipv6_in_pool(const struct in6_addr *addr, const struct ipv6_pool *pool)
Definition: mdb6.c:2094
void mark_phosts_unavailable(void)
Definition: mdb6.c:2342
isc_result_t expire_lease6(struct iasubopt **leasep, struct ipv6_pool *pool, time_t now)
Definition: mdb6.c:1563
isc_result_t release_lease6(struct ipv6_pool *pool, struct iasubopt *lease)
Definition: mdb6.c:1622
#define MAX_TIME
Definition: dhcpd.h:1572
struct data_string data
Definition: dhcpd.h:390
ipv6_pond structure
Definition: dhcpd.h:1684
void * isc_heap_element(isc_heap_t *heap, unsigned int index)
Returns the element for a specific element index.
#define D6O_IA_PD
Definition: dhcp6.h:55
isc_result_t ipv6_pond_reference(struct ipv6_pond **pond, struct ipv6_pond *src, const char *file, int line)
reference an IPv6 pond structure.
Definition: mdb6.c:2434
struct ipv6_pool ** pools
struct iasubopt ** iasubopt
Definition: dhcpd.h:1627
int write_ia(const struct ia_xx *)
Definition: db.c:515
struct ia_xx * ia
Definition: dhcpd.h:1597
struct executable_statement * on_expiry
Definition: dhcpd.h:544
struct shared_network * next
Definition: dhcpd.h:1015
const char * file
Definition: dhcpd.h:3676
isc_result_t ia_reference(struct ia_xx **ia, struct ia_xx *src, const char *file, int line)
Definition: mdb6.c:376
struct in6_addr addr
Definition: dhcpd.h:1589
struct executable_statement * on_commit
Definition: dhcpd.h:545
const unsigned char * data
Definition: tree.h:79
isc_result_t add_ipv6_pool(struct ipv6_pool *pool)
Definition: mdb6.c:1845
void data_string_copy(struct data_string *dest, const struct data_string *src, const char *file, int line)
Definition: alloc.c:1324
void mark_hosts_unavailable(void)
Definition: mdb6.c:2292
u_int16_t pool_type
Definition: dhcpd.h:1656
isc_result_t isc_heap_insert(isc_heap_t *heap, void *elt)
Inserts a new element into a heap.
void ia_remove_iasubopt(struct ia_xx *ia, struct iasubopt *iasubopt, const char *file, int line)
Definition: mdb6.c:475
isc_result_t cleanup_lease6(ia_hash_t *ia_table, struct ipv6_pool *pool, struct iasubopt *lease, struct ia_xx *ia)
Cleans up leases when reading from a lease file.
Definition: mdb6.c:1124
u_int8_t binding_state_t
Definition: dhcpd.h:534
void schedule_all_ipv6_lease_timeouts(void)
Definition: mdb6.c:2029
struct buffer * buffer
Definition: tree.h:78
#define FTS_ACTIVE
Definition: dhcpd.h:528
struct in6_addr * v6addresses
Definition: dhcpd.h:1336
int num_pools