ISC DHCP  4.3.0
A reference DHCPv4 and DHCPv6 implementation
mdb6.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007-2013 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 
1192  iasubopt_hash_delete(pool->leases, &test_iasubopt->addr,
1193  sizeof(test_iasubopt->addr), MDL);
1194  ia_remove_iasubopt(old_ia, test_iasubopt, MDL);
1195  if (old_ia->num_iasubopt <= 0) {
1196  ia_hash_delete(ia_table,
1197  (unsigned char *)old_ia->iaid_duid.data,
1198  old_ia->iaid_duid.len, MDL);
1199  }
1200 
1201  /*
1202  * We derefenrece the subopt here as we've just removed it from
1203  * the hash table in the pool. We need to make a copy as we
1204  * need to derefernece it again later.
1205  */
1206  tmp_iasubopt = test_iasubopt;
1207  iasubopt_dereference(&tmp_iasubopt, MDL);
1208 
1209  cleanup:
1210  ia_dereference(&old_ia, MDL);
1211 
1212  /*
1213  * Clean up the reference, this is in addition to the deference
1214  * above after removing the entry from the hash table
1215  */
1216  iasubopt_dereference(&test_iasubopt, MDL);
1217 
1218  return (status);
1219 }
1220 
1221 /*
1222  * Put a lease in the pool directly. This is intended to be used when
1223  * loading leases from the file.
1224  */
1225 isc_result_t
1227  time_t valid_lifetime_end_time) {
1228  isc_result_t insert_result;
1229  struct iasubopt *test_iasubopt;
1230  struct iasubopt *tmp_iasubopt;
1231 
1232  /* If a state was not assigned by the caller, assume active. */
1233  if (lease->state == 0)
1234  lease->state = FTS_ACTIVE;
1235 
1236  ipv6_pool_reference(&lease->ipv6_pool, pool, MDL);
1237 
1238  /*
1239  * If this IAADDR/PREFIX is already in our structures, remove the
1240  * old one.
1241  */
1242  test_iasubopt = NULL;
1243  if (iasubopt_hash_lookup(&test_iasubopt, pool->leases,
1244  &lease->addr, sizeof(lease->addr), MDL)) {
1245  /* XXX: we should probably ask the lease what heap it is on
1246  * (as a consistency check).
1247  * XXX: we should probably have one function to "put this lease
1248  * on its heap" rather than doing these if's everywhere. If
1249  * you add more states to this list, don't.
1250  */
1251  if ((test_iasubopt->state == FTS_ACTIVE) ||
1252  (test_iasubopt->state == FTS_ABANDONED)) {
1254  test_iasubopt->heap_index);
1255  pool->num_active--;
1256  } else {
1258  test_iasubopt->heap_index);
1259  pool->num_inactive--;
1260  }
1261 
1262  iasubopt_hash_delete(pool->leases, &test_iasubopt->addr,
1263  sizeof(test_iasubopt->addr), MDL);
1264 
1265  /*
1266  * We're going to do a bit of evil trickery here.
1267  *
1268  * We need to dereference the entry once to remove our
1269  * current reference (in test_iasubopt), and then one
1270  * more time to remove the reference left when the
1271  * address was added to the pool before.
1272  */
1273  tmp_iasubopt = test_iasubopt;
1274  iasubopt_dereference(&test_iasubopt, MDL);
1275  iasubopt_dereference(&tmp_iasubopt, MDL);
1276  }
1277 
1278  /*
1279  * Add IAADDR/PREFIX to our structures.
1280  */
1281  tmp_iasubopt = NULL;
1282  iasubopt_reference(&tmp_iasubopt, lease, MDL);
1283  if ((tmp_iasubopt->state == FTS_ACTIVE) ||
1284  (tmp_iasubopt->state == FTS_ABANDONED)) {
1285  tmp_iasubopt->hard_lifetime_end_time = valid_lifetime_end_time;
1286  iasubopt_hash_add(pool->leases, &tmp_iasubopt->addr,
1287  sizeof(tmp_iasubopt->addr), lease, MDL);
1288  insert_result = isc_heap_insert(pool->active_timeouts,
1289  tmp_iasubopt);
1290  if (insert_result == ISC_R_SUCCESS)
1291  pool->num_active++;
1292  } else {
1293  tmp_iasubopt->soft_lifetime_end_time = valid_lifetime_end_time;
1294  insert_result = isc_heap_insert(pool->inactive_timeouts,
1295  tmp_iasubopt);
1296  if (insert_result == ISC_R_SUCCESS)
1297  pool->num_inactive++;
1298  }
1299  if (insert_result != ISC_R_SUCCESS) {
1300  iasubopt_hash_delete(pool->leases, &lease->addr,
1301  sizeof(lease->addr), MDL);
1302  iasubopt_dereference(&tmp_iasubopt, MDL);
1303  return insert_result;
1304  }
1305 
1306  /*
1307  * Note: we intentionally leave tmp_iasubopt referenced; there
1308  * is a reference in the heap/hash, after all.
1309  */
1310 
1311  return ISC_R_SUCCESS;
1312 }
1313 
1314 /*
1315  * Determine if an address is present in a pool or not.
1316  */
1317 isc_boolean_t
1318 lease6_exists(const struct ipv6_pool *pool, const struct in6_addr *addr) {
1319  struct iasubopt *test_iaaddr;
1320 
1321  test_iaaddr = NULL;
1322  if (iasubopt_hash_lookup(&test_iaaddr, pool->leases,
1323  (void *)addr, sizeof(*addr), MDL)) {
1324  iasubopt_dereference(&test_iaaddr, MDL);
1325  return ISC_TRUE;
1326  } else {
1327  return ISC_FALSE;
1328  }
1329 }
1330 
1345 isc_boolean_t
1347  struct iasubopt *test_iaaddr;
1348  isc_boolean_t status = ISC_TRUE;
1349 
1350  test_iaaddr = NULL;
1351  if (iasubopt_hash_lookup(&test_iaaddr, lease->ipv6_pool->leases,
1352  (void *)&lease->addr,
1353  sizeof(lease->addr), MDL)) {
1354  if (test_iaaddr != lease) {
1355  status = ISC_FALSE;
1356  }
1357  iasubopt_dereference(&test_iaaddr, MDL);
1358  }
1359 
1360  return (status);
1361 }
1362 
1363 /*
1364  * Put the lease on our active pool.
1365  */
1366 static isc_result_t
1367 move_lease_to_active(struct ipv6_pool *pool, struct iasubopt *lease) {
1368  isc_result_t insert_result;
1369  int old_heap_index;
1370 
1371  old_heap_index = lease->heap_index;
1372  insert_result = isc_heap_insert(pool->active_timeouts, lease);
1373  if (insert_result == ISC_R_SUCCESS) {
1374  iasubopt_hash_add(pool->leases, &lease->addr,
1375  sizeof(lease->addr), lease, MDL);
1376  isc_heap_delete(pool->inactive_timeouts, old_heap_index);
1377  pool->num_active++;
1378  pool->num_inactive--;
1379  lease->state = FTS_ACTIVE;
1380  }
1381  return insert_result;
1382 }
1383 
1414 isc_result_t
1415 renew_lease6(struct ipv6_pool *pool, struct iasubopt *lease) {
1416  time_t old_end_time = lease->hard_lifetime_end_time;
1418  lease->soft_lifetime_end_time = 0;
1419 
1420  if (lease->state == FTS_ACTIVE) {
1421  if (old_end_time <= lease->hard_lifetime_end_time) {
1423  lease->heap_index);
1424  } else {
1426  lease->heap_index);
1427  }
1428  return ISC_R_SUCCESS;
1429  } else if (lease->state == FTS_ABANDONED) {
1430  char tmp_addr[INET6_ADDRSTRLEN];
1431  lease->state = FTS_ACTIVE;
1433  log_info("Reclaiming previously abandoned address %s",
1434  inet_ntop(AF_INET6, &(lease->addr), tmp_addr,
1435  sizeof(tmp_addr)));
1436  return ISC_R_SUCCESS;
1437  } else {
1438  return move_lease_to_active(pool, lease);
1439  }
1440 }
1441 
1442 /*
1443  * Put the lease on our inactive pool, with the specified state.
1444  */
1445 static isc_result_t
1446 move_lease_to_inactive(struct ipv6_pool *pool, struct iasubopt *lease,
1448  isc_result_t insert_result;
1449  int old_heap_index;
1450 
1451  old_heap_index = lease->heap_index;
1452  insert_result = isc_heap_insert(pool->inactive_timeouts, lease);
1453  if (insert_result == ISC_R_SUCCESS) {
1454  /*
1455  * Handle expire and release statements
1456  * To get here we must be active and have done a commit so
1457  * we should run the proper statements if they exist, though
1458  * that will change when we remove the inactive heap.
1459  * In addition we get rid of the references for both as we
1460  * can only do one (expire or release) on a lease
1461  */
1462  if (lease->on_star.on_expiry != NULL) {
1463  if (state == FTS_EXPIRED) {
1464  execute_statements(NULL, NULL, NULL,
1465  NULL, NULL, NULL,
1466  &lease->scope,
1467  lease->on_star.on_expiry,
1468  &lease->on_star);
1469  }
1471  (&lease->on_star.on_expiry, MDL);
1472  }
1473 
1474  if (lease->on_star.on_release != NULL) {
1475  if (state == FTS_RELEASED) {
1476  execute_statements(NULL, NULL, NULL,
1477  NULL, NULL, NULL,
1478  &lease->scope,
1479  lease->on_star.on_release,
1480  &lease->on_star);
1481  }
1483  (&lease->on_star.on_release, MDL);
1484  }
1485 
1486 #if defined (NSUPDATE)
1487  /* Process events upon expiration. */
1488  if (pool->pool_type != D6O_IA_PD) {
1489  (void) ddns_removals(NULL, lease, NULL, ISC_FALSE);
1490  }
1491 #endif
1492 
1493  /* Binding scopes are no longer valid after expiry or
1494  * release.
1495  */
1496  if (lease->scope != NULL) {
1498  }
1499 
1500  iasubopt_hash_delete(pool->leases,
1501  &lease->addr, sizeof(lease->addr), MDL);
1502  isc_heap_delete(pool->active_timeouts, old_heap_index);
1503  lease->state = state;
1504  pool->num_active--;
1505  pool->num_inactive++;
1506  }
1507  return insert_result;
1508 }
1509 
1510 /*
1511  * Expire the oldest lease if it's lifetime_end_time is
1512  * older than the given time.
1513  *
1514  * - leasep must be a pointer to a (struct iasubopt *) pointer previously
1515  * initialized to NULL
1516  *
1517  * On return leasep has a reference to the removed entry. It is left
1518  * pointing to NULL if the oldest lease has not expired.
1519  */
1520 isc_result_t
1521 expire_lease6(struct iasubopt **leasep, struct ipv6_pool *pool, time_t now) {
1522  struct iasubopt *tmp;
1523  isc_result_t result;
1524 
1525  if (leasep == NULL) {
1526  log_error("%s(%d): NULL pointer reference", MDL);
1527  return DHCP_R_INVALIDARG;
1528  }
1529  if (*leasep != NULL) {
1530  log_error("%s(%d): non-NULL pointer", MDL);
1531  return DHCP_R_INVALIDARG;
1532  }
1533 
1534  if (pool->num_active > 0) {
1535  tmp = (struct iasubopt *)
1537  if (now > tmp->hard_lifetime_end_time) {
1538  result = move_lease_to_inactive(pool, tmp,
1539  FTS_EXPIRED);
1540  if (result == ISC_R_SUCCESS) {
1541  iasubopt_reference(leasep, tmp, MDL);
1542  }
1543  return result;
1544  }
1545  }
1546  return ISC_R_SUCCESS;
1547 }
1548 
1549 
1550 /*
1551  * For a declined lease, leave it on the "active" pool, but mark
1552  * it as declined. Give it an infinite (well, really long) life.
1553  */
1554 isc_result_t
1555 decline_lease6(struct ipv6_pool *pool, struct iasubopt *lease) {
1556  isc_result_t result;
1557 
1558  if ((lease->state != FTS_ACTIVE) &&
1559  (lease->state != FTS_ABANDONED)) {
1560  result = move_lease_to_active(pool, lease);
1561  if (result != ISC_R_SUCCESS) {
1562  return result;
1563  }
1564  }
1565  lease->state = FTS_ABANDONED;
1568  return ISC_R_SUCCESS;
1569 }
1570 
1571 /*
1572  * Put the returned lease on our inactive pool.
1573  */
1574 isc_result_t
1575 release_lease6(struct ipv6_pool *pool, struct iasubopt *lease) {
1576  if (lease->state == FTS_ACTIVE) {
1577  return move_lease_to_inactive(pool, lease, FTS_RELEASED);
1578  } else {
1579  return ISC_R_SUCCESS;
1580  }
1581 }
1582 
1583 /*
1584  * Create a prefix by hashing the input, and using that for
1585  * the part subject to allocation.
1586  */
1587 void
1588 build_prefix6(struct in6_addr *pref,
1589  const struct in6_addr *net_start_pref,
1590  int pool_bits, int pref_bits,
1591  const struct data_string *input) {
1592  isc_md5_t ctx;
1593  int net_bytes;
1594  int i;
1595  char *str;
1596  const char *net_str;
1597 
1598  /*
1599  * Use MD5 to get a nice 128 bit hash of the input.
1600  * Yes, we know MD5 isn't cryptographically sound.
1601  * No, we don't care.
1602  */
1603  isc_md5_init(&ctx);
1604  isc_md5_update(&ctx, input->data, input->len);
1605  isc_md5_final(&ctx, (unsigned char *)pref);
1606 
1607  /*
1608  * Copy the network bits over.
1609  */
1610  str = (char *)pref;
1611  net_str = (const char *)net_start_pref;
1612  net_bytes = pool_bits / 8;
1613  for (i = 0; i < net_bytes; i++) {
1614  str[i] = net_str[i];
1615  }
1616  i = net_bytes;
1617  switch (pool_bits % 8) {
1618  case 1: str[i] = (str[i] & 0x7F) | (net_str[i] & 0x80); break;
1619  case 2: str[i] = (str[i] & 0x3F) | (net_str[i] & 0xC0); break;
1620  case 3: str[i] = (str[i] & 0x1F) | (net_str[i] & 0xE0); break;
1621  case 4: str[i] = (str[i] & 0x0F) | (net_str[i] & 0xF0); break;
1622  case 5: str[i] = (str[i] & 0x07) | (net_str[i] & 0xF8); break;
1623  case 6: str[i] = (str[i] & 0x03) | (net_str[i] & 0xFC); break;
1624  case 7: str[i] = (str[i] & 0x01) | (net_str[i] & 0xFE); break;
1625  }
1626  /*
1627  * Zero the remaining bits.
1628  */
1629  net_bytes = pref_bits / 8;
1630  for (i=net_bytes+1; i<16; i++) {
1631  str[i] = 0;
1632  }
1633  i = net_bytes;
1634  switch (pref_bits % 8) {
1635  case 0: str[i] &= 0; break;
1636  case 1: str[i] &= 0x80; break;
1637  case 2: str[i] &= 0xC0; break;
1638  case 3: str[i] &= 0xE0; break;
1639  case 4: str[i] &= 0xF0; break;
1640  case 5: str[i] &= 0xF8; break;
1641  case 6: str[i] &= 0xFC; break;
1642  case 7: str[i] &= 0xFE; break;
1643  }
1644 }
1645 
1646 /*
1647  * Create a lease for the given prefix and client duid.
1648  *
1649  * - pool must be a pointer to a (struct ipv6_pool *) pointer previously
1650  * initialized to NULL
1651  *
1652  * Right now we simply hash the DUID, and if we get a collision, we hash
1653  * again until we find a free prefix. We try this a fixed number of times,
1654  * to avoid getting stuck in a loop (this is important on small pools
1655  * where we can run out of space).
1656  *
1657  * We return the number of attempts that it took to find an available
1658  * prefix. This tells callers when a pool is are filling up, as
1659  * well as an indication of how full the pool is; statistically the
1660  * more full a pool is the more attempts must be made before finding
1661  * a free prefix. Realistically this will only happen in very full
1662  * pools.
1663  *
1664  * We probably want different algorithms depending on the network size, in
1665  * the long term.
1666  */
1667 isc_result_t
1668 create_prefix6(struct ipv6_pool *pool, struct iasubopt **pref,
1669  unsigned int *attempts,
1670  const struct data_string *uid,
1671  time_t soft_lifetime_end_time) {
1672  struct data_string ds;
1673  struct in6_addr tmp;
1674  struct iasubopt *test_iapref;
1675  struct data_string new_ds;
1676  struct iasubopt *iapref;
1677  isc_result_t result;
1678 
1679  /*
1680  * Use the UID as our initial seed for the hash
1681  */
1682  memset(&ds, 0, sizeof(ds));
1683  data_string_copy(&ds, (struct data_string *)uid, MDL);
1684 
1685  *attempts = 0;
1686  for (;;) {
1687  /*
1688  * Give up at some point.
1689  */
1690  if (++(*attempts) > 10) {
1691  data_string_forget(&ds, MDL);
1692  return ISC_R_NORESOURCES;
1693  }
1694 
1695  /*
1696  * Build a prefix
1697  */
1698  build_prefix6(&tmp, &pool->start_addr,
1699  pool->bits, pool->units, &ds);
1700 
1701  /*
1702  * If this prefix is not in use, we're happy with it
1703  */
1704  test_iapref = NULL;
1705  if (iasubopt_hash_lookup(&test_iapref, pool->leases,
1706  &tmp, sizeof(tmp), MDL) == 0) {
1707  break;
1708  }
1709  iasubopt_dereference(&test_iapref, MDL);
1710 
1711  /*
1712  * Otherwise, we create a new input, adding the prefix
1713  */
1714  memset(&new_ds, 0, sizeof(new_ds));
1715  new_ds.len = ds.len + sizeof(tmp);
1716  if (!buffer_allocate(&new_ds.buffer, new_ds.len, MDL)) {
1717  data_string_forget(&ds, MDL);
1718  return ISC_R_NOMEMORY;
1719  }
1720  new_ds.data = new_ds.buffer->data;
1721  memcpy(new_ds.buffer->data, ds.data, ds.len);
1722  memcpy(new_ds.buffer->data + ds.len, &tmp, sizeof(tmp));
1723  data_string_forget(&ds, MDL);
1724  data_string_copy(&ds, &new_ds, MDL);
1725  data_string_forget(&new_ds, MDL);
1726  }
1727 
1728  data_string_forget(&ds, MDL);
1729 
1730  /*
1731  * We're happy with the prefix, create an IAPREFIX
1732  * to hold it.
1733  */
1734  iapref = NULL;
1735  result = iasubopt_allocate(&iapref, MDL);
1736  if (result != ISC_R_SUCCESS) {
1737  return result;
1738  }
1739  iapref->plen = (u_int8_t)pool->units;
1740  memcpy(&iapref->addr, &tmp, sizeof(iapref->addr));
1741 
1742  /*
1743  * Add the prefix to the pool (note state is free, not active?!).
1744  */
1745  result = add_lease6(pool, iapref, soft_lifetime_end_time);
1746  if (result == ISC_R_SUCCESS) {
1747  iasubopt_reference(pref, iapref, MDL);
1748  }
1749  iasubopt_dereference(&iapref, MDL);
1750  return result;
1751 }
1752 
1753 /*
1754  * Determine if a prefix is present in a pool or not.
1755  */
1756 isc_boolean_t
1757 prefix6_exists(const struct ipv6_pool *pool,
1758  const struct in6_addr *pref, u_int8_t plen) {
1759  struct iasubopt *test_iapref;
1760 
1761  if ((int)plen != pool->units)
1762  return ISC_FALSE;
1763 
1764  test_iapref = NULL;
1765  if (iasubopt_hash_lookup(&test_iapref, pool->leases,
1766  (void *)pref, sizeof(*pref), MDL)) {
1767  iasubopt_dereference(&test_iapref, MDL);
1768  return ISC_TRUE;
1769  } else {
1770  return ISC_FALSE;
1771  }
1772 }
1773 
1774 /*
1775  * Mark an IPv6 address/prefix as unavailable from a pool.
1776  *
1777  * This is used for host entries and the addresses of the server itself.
1778  */
1779 isc_result_t
1780 mark_lease_unavailable(struct ipv6_pool *pool, const struct in6_addr *addr) {
1781  struct iasubopt *dummy_iasubopt;
1782  isc_result_t result;
1783 
1784  dummy_iasubopt = NULL;
1785  result = iasubopt_allocate(&dummy_iasubopt, MDL);
1786  if (result == ISC_R_SUCCESS) {
1787  dummy_iasubopt->addr = *addr;
1788  iasubopt_hash_add(pool->leases, &dummy_iasubopt->addr,
1789  sizeof(*addr), dummy_iasubopt, MDL);
1790  }
1791  return result;
1792 }
1793 
1794 /*
1795  * Add a pool.
1796  */
1797 isc_result_t
1798 add_ipv6_pool(struct ipv6_pool *pool) {
1799  struct ipv6_pool **new_pools;
1800 
1801  new_pools = dmalloc(sizeof(struct ipv6_pool *) * (num_pools+1), MDL);
1802  if (new_pools == NULL) {
1803  return ISC_R_NOMEMORY;
1804  }
1805 
1806  if (num_pools > 0) {
1807  memcpy(new_pools, pools,
1808  sizeof(struct ipv6_pool *) * num_pools);
1809  dfree(pools, MDL);
1810  }
1811  pools = new_pools;
1812 
1813  pools[num_pools] = NULL;
1814  ipv6_pool_reference(&pools[num_pools], pool, MDL);
1815  num_pools++;
1816  return ISC_R_SUCCESS;
1817 }
1818 
1819 static void
1820 cleanup_old_expired(struct ipv6_pool *pool) {
1821  struct iasubopt *tmp;
1822  struct ia_xx *ia;
1823  struct ia_xx *ia_active;
1824  unsigned char *tmpd;
1825  time_t timeout;
1826 
1827  while (pool->num_inactive > 0) {
1828  tmp = (struct iasubopt *)
1830  if (tmp->hard_lifetime_end_time != 0) {
1831  timeout = tmp->hard_lifetime_end_time;
1832  timeout += EXPIRED_IPV6_CLEANUP_TIME;
1833  } else {
1834  timeout = tmp->soft_lifetime_end_time;
1835  }
1836  if (cur_time < timeout) {
1837  break;
1838  }
1839 
1841  pool->num_inactive--;
1842 
1843  if (tmp->ia != NULL) {
1844  /*
1845  * Check to see if this IA is in an active list,
1846  * but has no remaining resources. If so, remove it
1847  * from the active list.
1848  */
1849  ia = NULL;
1850  ia_reference(&ia, tmp->ia, MDL);
1851  ia_remove_iasubopt(ia, tmp, MDL);
1852  ia_active = NULL;
1853  tmpd = (unsigned char *)ia->iaid_duid.data;
1854  if ((ia->ia_type == D6O_IA_NA) &&
1855  (ia->num_iasubopt <= 0) &&
1856  (ia_hash_lookup(&ia_active, ia_na_active, tmpd,
1857  ia->iaid_duid.len, MDL) == 0) &&
1858  (ia_active == ia)) {
1859  ia_hash_delete(ia_na_active, tmpd,
1860  ia->iaid_duid.len, MDL);
1861  }
1862  if ((ia->ia_type == D6O_IA_TA) &&
1863  (ia->num_iasubopt <= 0) &&
1864  (ia_hash_lookup(&ia_active, ia_ta_active, tmpd,
1865  ia->iaid_duid.len, MDL) == 0) &&
1866  (ia_active == ia)) {
1867  ia_hash_delete(ia_ta_active, tmpd,
1868  ia->iaid_duid.len, MDL);
1869  }
1870  if ((ia->ia_type == D6O_IA_PD) &&
1871  (ia->num_iasubopt <= 0) &&
1872  (ia_hash_lookup(&ia_active, ia_pd_active, tmpd,
1873  ia->iaid_duid.len, MDL) == 0) &&
1874  (ia_active == ia)) {
1875  ia_hash_delete(ia_pd_active, tmpd,
1876  ia->iaid_duid.len, MDL);
1877  }
1878  ia_dereference(&ia, MDL);
1879  }
1880  iasubopt_dereference(&tmp, MDL);
1881  }
1882 }
1883 
1884 static void
1885 lease_timeout_support(void *vpool) {
1886  struct ipv6_pool *pool;
1887  struct iasubopt *lease;
1888 
1889  pool = (struct ipv6_pool *)vpool;
1890  for (;;) {
1891  /*
1892  * Get the next lease scheduled to expire.
1893  *
1894  * Note that if there are no leases in the pool,
1895  * expire_lease6() will return ISC_R_SUCCESS with
1896  * a NULL lease.
1897  *
1898  * expire_lease6() will call move_lease_to_inactive() which
1899  * calls ddns_removals() do we want that on the standard
1900  * expiration timer or a special 'depref' timer? Original
1901  * query from DH, moved here by SAR.
1902  */
1903  lease = NULL;
1904  if (expire_lease6(&lease, pool, cur_time) != ISC_R_SUCCESS) {
1905  break;
1906  }
1907  if (lease == NULL) {
1908  break;
1909  }
1910 
1911  write_ia(lease->ia);
1912 
1913  iasubopt_dereference(&lease, MDL);
1914  }
1915 
1916  /*
1917  * If appropriate commit and rotate the lease file
1918  * As commit_leases_timed() checks to see if we've done any writes
1919  * we don't bother tracking if this function called write _ia
1920  */
1921  (void) commit_leases_timed();
1922 
1923  /*
1924  * Do some cleanup of our expired leases.
1925  */
1926  cleanup_old_expired(pool);
1927 
1928  /*
1929  * Schedule next round of expirations.
1930  */
1931  schedule_lease_timeout(pool);
1932 }
1933 
1934 /*
1935  * For a given pool, add a timer that will remove the next
1936  * lease to expire.
1937  */
1938 void
1940  struct iasubopt *tmp;
1941  time_t timeout;
1942  time_t next_timeout;
1943  struct timeval tv;
1944 
1945  next_timeout = MAX_TIME;
1946 
1947  if (pool->num_active > 0) {
1948  tmp = (struct iasubopt *)
1950  if (tmp->hard_lifetime_end_time < next_timeout) {
1951  next_timeout = tmp->hard_lifetime_end_time + 1;
1952  }
1953  }
1954 
1955  if (pool->num_inactive > 0) {
1956  tmp = (struct iasubopt *)
1958  if (tmp->hard_lifetime_end_time != 0) {
1959  timeout = tmp->hard_lifetime_end_time;
1960  timeout += EXPIRED_IPV6_CLEANUP_TIME;
1961  } else {
1962  timeout = tmp->soft_lifetime_end_time + 1;
1963  }
1964  if (timeout < next_timeout) {
1965  next_timeout = timeout;
1966  }
1967  }
1968 
1969  if (next_timeout < MAX_TIME) {
1970  tv.tv_sec = next_timeout;
1971  tv.tv_usec = 0;
1972  add_timeout(&tv, lease_timeout_support, pool,
1975  }
1976 }
1977 
1978 /*
1979  * Schedule timeouts across all pools.
1980  */
1981 void
1983  int i;
1984 
1985  for (i=0; i<num_pools; i++) {
1986  schedule_lease_timeout(pools[i]);
1987  }
1988 }
1989 
1990 /*
1991  * Given an address and the length of the network mask, return
1992  * only the network portion.
1993  *
1994  * Examples:
1995  *
1996  * "fe80::216:6fff:fe49:7d9b", length 64 = "fe80::"
1997  * "2001:888:1936:2:216:6fff:fe49:7d9b", length 48 = "2001:888:1936::"
1998  */
1999 static void
2000 ipv6_network_portion(struct in6_addr *result,
2001  const struct in6_addr *addr, int bits) {
2002  unsigned char *addrp;
2003  int mask_bits;
2004  int bytes;
2005  int extra_bits;
2006  int i;
2007 
2008  static const unsigned char bitmasks[] = {
2009  0x00, 0xFE, 0xFC, 0xF8,
2010  0xF0, 0xE0, 0xC0, 0x80,
2011  };
2012 
2013  /*
2014  * Sanity check our bits. ;)
2015  */
2016  if ((bits < 0) || (bits > 128)) {
2017  log_fatal("ipv6_network_portion: bits %d not between 0 and 128",
2018  bits);
2019  }
2020 
2021  /*
2022  * Copy our address portion.
2023  */
2024  *result = *addr;
2025  addrp = ((unsigned char *)result) + 15;
2026 
2027  /*
2028  * Zero out masked portion.
2029  */
2030  mask_bits = 128 - bits;
2031  bytes = mask_bits / 8;
2032  extra_bits = mask_bits % 8;
2033 
2034  for (i=0; i<bytes; i++) {
2035  *addrp = 0;
2036  addrp--;
2037  }
2038  if (extra_bits) {
2039  *addrp &= bitmasks[extra_bits];
2040  }
2041 }
2042 
2043 /*
2044  * Determine if the given address/prefix is in the pool.
2045  */
2046 isc_boolean_t
2047 ipv6_in_pool(const struct in6_addr *addr, const struct ipv6_pool *pool) {
2048  struct in6_addr tmp;
2049 
2050  ipv6_network_portion(&tmp, addr, pool->bits);
2051  if (memcmp(&tmp, &pool->start_addr, sizeof(tmp)) == 0) {
2052  return ISC_TRUE;
2053  } else {
2054  return ISC_FALSE;
2055  }
2056 }
2057 
2058 /*
2059  * Find the pool that contains the given address.
2060  *
2061  * - pool must be a pointer to a (struct ipv6_pool *) pointer previously
2062  * initialized to NULL
2063  */
2064 isc_result_t
2065 find_ipv6_pool(struct ipv6_pool **pool, u_int16_t type,
2066  const struct in6_addr *addr) {
2067  int i;
2068 
2069  if (pool == NULL) {
2070  log_error("%s(%d): NULL pointer reference", MDL);
2071  return DHCP_R_INVALIDARG;
2072  }
2073  if (*pool != NULL) {
2074  log_error("%s(%d): non-NULL pointer", MDL);
2075  return DHCP_R_INVALIDARG;
2076  }
2077 
2078  for (i=0; i<num_pools; i++) {
2079  if (pools[i]->pool_type != type)
2080  continue;
2081  if (ipv6_in_pool(addr, pools[i])) {
2082  ipv6_pool_reference(pool, pools[i], MDL);
2083  return ISC_R_SUCCESS;
2084  }
2085  }
2086  return ISC_R_NOTFOUND;
2087 }
2088 
2089 /*
2090  * Helper function for the various functions that act across all
2091  * pools.
2092  */
2093 static isc_result_t
2094 change_leases(struct ia_xx *ia,
2095  isc_result_t (*change_func)(struct ipv6_pool *,
2096  struct iasubopt *)) {
2097  isc_result_t retval;
2098  isc_result_t renew_retval;
2099  struct ipv6_pool *pool;
2100  struct in6_addr *addr;
2101  int i;
2102 
2103  retval = ISC_R_SUCCESS;
2104  for (i=0; i<ia->num_iasubopt; i++) {
2105  pool = NULL;
2106  addr = &ia->iasubopt[i]->addr;
2107  if (find_ipv6_pool(&pool, ia->ia_type,
2108  addr) == ISC_R_SUCCESS) {
2109  renew_retval = change_func(pool, ia->iasubopt[i]);
2110  if (renew_retval != ISC_R_SUCCESS) {
2111  retval = renew_retval;
2112  }
2113  }
2114  /* XXXsk: should we warn if we don't find a pool? */
2115  }
2116  return retval;
2117 }
2118 
2119 /*
2120  * Renew all leases in an IA from all pools.
2121  *
2122  * The new lifetime should be in the soft_lifetime_end_time
2123  * and will be moved to hard_lifetime_end_time by renew_lease6.
2124  */
2125 isc_result_t
2126 renew_leases(struct ia_xx *ia) {
2127  return change_leases(ia, renew_lease6);
2128 }
2129 
2130 /*
2131  * Release all leases in an IA from all pools.
2132  */
2133 isc_result_t
2134 release_leases(struct ia_xx *ia) {
2135  return change_leases(ia, release_lease6);
2136 }
2137 
2138 /*
2139  * Decline all leases in an IA from all pools.
2140  */
2141 isc_result_t
2142 decline_leases(struct ia_xx *ia) {
2143  return change_leases(ia, decline_lease6);
2144 }
2145 
2146 #ifdef DHCPv6
2147 /*
2148  * Helper function to output leases.
2149  */
2150 static int write_error;
2151 
2152 static isc_result_t
2153 write_ia_leases(const void *name, unsigned len, void *value) {
2154  struct ia_xx *ia = (struct ia_xx *)value;
2155 
2156  if (!write_error) {
2157  if (!write_ia(ia)) {
2158  write_error = 1;
2159  }
2160  }
2161  return ISC_R_SUCCESS;
2162 }
2163 
2164 /*
2165  * Write all DHCPv6 information.
2166  */
2167 int
2168 write_leases6(void) {
2169  int nas, tas, pds;
2170 
2171  write_error = 0;
2173  nas = ia_hash_foreach(ia_na_active, write_ia_leases);
2174  if (write_error) {
2175  return 0;
2176  }
2177  tas = ia_hash_foreach(ia_ta_active, write_ia_leases);
2178  if (write_error) {
2179  return 0;
2180  }
2181  pds = ia_hash_foreach(ia_pd_active, write_ia_leases);
2182  if (write_error) {
2183  return 0;
2184  }
2185 
2186  log_info("Wrote %d NA, %d TA, %d PD leases to lease file.",
2187  nas, tas, pds);
2188  return 1;
2189 }
2190 #endif /* DHCPv6 */
2191 
2192 static isc_result_t
2193 mark_hosts_unavailable_support(const void *name, unsigned len, void *value) {
2194  struct host_decl *h;
2195  struct data_string fixed_addr;
2196  struct in6_addr addr;
2197  struct ipv6_pool *p;
2198 
2199  h = (struct host_decl *)value;
2200 
2201  /*
2202  * If the host has no address, we don't need to mark anything.
2203  */
2204  if (h->fixed_addr == NULL) {
2205  return ISC_R_SUCCESS;
2206  }
2207 
2208  /*
2209  * Evaluate the fixed address.
2210  */
2211  memset(&fixed_addr, 0, sizeof(fixed_addr));
2212  if (!evaluate_option_cache(&fixed_addr, NULL, NULL, NULL, NULL, NULL,
2213  &global_scope, h->fixed_addr, MDL)) {
2214  log_error("mark_hosts_unavailable: "
2215  "error evaluating host address.");
2216  return ISC_R_SUCCESS;
2217  }
2218  if (fixed_addr.len != 16) {
2219  log_error("mark_hosts_unavailable: "
2220  "host address is not 128 bits.");
2221  return ISC_R_SUCCESS;
2222  }
2223  memcpy(&addr, fixed_addr.data, 16);
2224  data_string_forget(&fixed_addr, MDL);
2225 
2226  /*
2227  * Find the pool holding this host, and mark the address.
2228  * (I suppose it is arguably valid to have a host that does not
2229  * sit in any pool.)
2230  */
2231  p = NULL;
2232  if (find_ipv6_pool(&p, D6O_IA_NA, &addr) == ISC_R_SUCCESS) {
2233  mark_lease_unavailable(p, &addr);
2235  }
2236  if (find_ipv6_pool(&p, D6O_IA_TA, &addr) == ISC_R_SUCCESS) {
2237  mark_lease_unavailable(p, &addr);
2239  }
2240 
2241  return ISC_R_SUCCESS;
2242 }
2243 
2244 void
2246  hash_foreach(host_name_hash, mark_hosts_unavailable_support);
2247 }
2248 
2249 static isc_result_t
2250 mark_phosts_unavailable_support(const void *name, unsigned len, void *value) {
2251  struct host_decl *h;
2252  struct iaddrcidrnetlist *l;
2253  struct in6_addr pref;
2254  struct ipv6_pool *p;
2255 
2256  h = (struct host_decl *)value;
2257 
2258  /*
2259  * If the host has no prefix, we don't need to mark anything.
2260  */
2261  if (h->fixed_prefix == NULL) {
2262  return ISC_R_SUCCESS;
2263  }
2264 
2265  /*
2266  * Get the fixed prefixes.
2267  */
2268  for (l = h->fixed_prefix; l != NULL; l = l->next) {
2269  if (l->cidrnet.lo_addr.len != 16) {
2270  continue;
2271  }
2272  memcpy(&pref, l->cidrnet.lo_addr.iabuf, 16);
2273 
2274  /*
2275  * Find the pool holding this host, and mark the prefix.
2276  * (I suppose it is arguably valid to have a host that does not
2277  * sit in any pool.)
2278  */
2279  p = NULL;
2280  if (find_ipv6_pool(&p, D6O_IA_PD, &pref) != ISC_R_SUCCESS) {
2281  continue;
2282  }
2283  if (l->cidrnet.bits != p->units) {
2285  continue;
2286  }
2287  mark_lease_unavailable(p, &pref);
2289  }
2290 
2291  return ISC_R_SUCCESS;
2292 }
2293 
2294 void
2296  hash_foreach(host_name_hash, mark_phosts_unavailable_support);
2297 }
2298 
2299 void
2301  struct interface_info *ip;
2302  int i;
2303  struct ipv6_pool *p;
2304 
2305  ip = interfaces;
2306  while (ip != NULL) {
2307  for (i=0; i<ip->v6address_count; i++) {
2308  p = NULL;
2309  if (find_ipv6_pool(&p, D6O_IA_NA, &ip->v6addresses[i])
2310  == ISC_R_SUCCESS) {
2312  &ip->v6addresses[i]);
2314  }
2315  if (find_ipv6_pool(&p, D6O_IA_TA, &ip->v6addresses[i])
2316  == ISC_R_SUCCESS) {
2318  &ip->v6addresses[i]);
2320  }
2321  }
2322  ip = ip->next;
2323  }
2324 }
2325 
2343 isc_result_t
2344 ipv6_pond_allocate(struct ipv6_pond **pond, const char *file, int line) {
2345  struct ipv6_pond *tmp;
2346 
2347  if (pond == NULL) {
2348  log_error("%s(%d): NULL pointer reference", file, line);
2349  return DHCP_R_INVALIDARG;
2350  }
2351  if (*pond != NULL) {
2352  log_error("%s(%d): non-NULL pointer", file, line);
2353  return DHCP_R_INVALIDARG;
2354  }
2355 
2356  tmp = dmalloc(sizeof(*tmp), file, line);
2357  if (tmp == NULL) {
2358  return ISC_R_NOMEMORY;
2359  }
2360 
2361  tmp->refcnt = 1;
2362 
2363  *pond = tmp;
2364  return ISC_R_SUCCESS;
2365 }
2366 
2386 isc_result_t
2387 ipv6_pond_reference(struct ipv6_pond **pond, struct ipv6_pond *src,
2388  const char *file, int line) {
2389  if (pond == NULL) {
2390  log_error("%s(%d): NULL pointer reference", file, line);
2391  return DHCP_R_INVALIDARG;
2392  }
2393  if (*pond != NULL) {
2394  log_error("%s(%d): non-NULL pointer", file, line);
2395  return DHCP_R_INVALIDARG;
2396  }
2397  if (src == NULL) {
2398  log_error("%s(%d): NULL pointer reference", file, line);
2399  return DHCP_R_INVALIDARG;
2400  }
2401  *pond = src;
2402  src->refcnt++;
2403  return ISC_R_SUCCESS;
2404 }
2405 
2426 isc_result_t
2427 ipv6_pond_dereference(struct ipv6_pond **pond, const char *file, int line) {
2428  struct ipv6_pond *tmp;
2429 
2430  if ((pond == NULL) || (*pond == NULL)) {
2431  log_error("%s(%d): NULL pointer", file, line);
2432  return DHCP_R_INVALIDARG;
2433  }
2434 
2435  tmp = *pond;
2436  *pond = NULL;
2437 
2438  tmp->refcnt--;
2439  if (tmp->refcnt < 0) {
2440  log_error("%s(%d): negative refcnt", file, line);
2441  tmp->refcnt = 0;
2442  }
2443  if (tmp->refcnt == 0) {
2444  dfree(tmp, file, line);
2445  }
2446 
2447  return ISC_R_SUCCESS;
2448 }
2449 
2450 /* unittest moved to server/tests/mdb6_unittest.c */
#define FTS_ABANDONED
Definition: dhcpd.h:488
struct iaddrcidrnet cidrnet
Definition: inet.h:77
void mark_interfaces_unavailable(void)
Definition: mdb6.c:2300
ia_hash_t * ia_ta_active
isc_boolean_t lease6_usable(struct iasubopt *lease)
Check if address is available to a lease.
Definition: mdb6.c:1346
const char int line
Definition: dhcpd.h:3535
isc_result_t mark_lease_unavailable(struct ipv6_pool *pool, const struct in6_addr *addr)
Definition: mdb6.c:1780
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:1757
Definition: dhcpd.h:507
unsigned len
Definition: tree.h:80
int executable_statement_dereference(struct executable_statement **ptr, const char *file, int line)
Definition: execute.c:615
int bits
Definition: inet.h:72
#define FTS_FREE
Definition: dhcpd.h:484
Definition: dhcpd.h:1530
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:1668
int units
Definition: dhcpd.h:1569
int max_iasubopt
Definition: dhcpd.h:1535
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:1415
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
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:1515
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:2065
#define FTS_RELEASED
Definition: dhcpd.h:487
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:1588
struct executable_statement * on_release
Definition: dhcpd.h:503
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:1276
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:1567
struct option_cache * fixed_addr
Definition: dhcpd.h:873
int log_error(const char *,...) __attribute__((__format__(__printf__
isc_result_t release_leases(struct ia_xx *ia)
Definition: mdb6.c:2134
#define FTS_EXPIRED
Definition: dhcpd.h:486
int binding_scope_dereference(struct binding_scope **ptr, const char *file, int line)
Definition: tree.c:3722
int num_inactive
Definition: dhcpd.h:1573
struct on_star on_star
Definition: dhcpd.h:1527
struct binding_scope * scope
Definition: dhcpd.h:1502
void add_timeout(struct timeval *when, void(*)(void *) where, void *what, tvref_t ref, tvunref_t unref)
Definition: dispatch.c:198
void(* tvunref_t)(void *, const char *, int)
Definition: dhcpd.h:1312
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:1594
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:1513
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.
void(* tvref_t)(void *, void *, const char *, int)
Definition: dhcpd.h:1311
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:1318
void isc_heap_decreased(isc_heap_t *heap, unsigned int index)
Indicates to the heap that an element&#39;s priority has decreased. This function MUST be called whenever...
time_t hard_lifetime_end_time
Definition: dhcpd.h:1503
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:2643
host_hash_t * host_name_hash
Definition: mdb.c:37
Definition: dhcpd.h:904
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:1508
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:2344
int write_server_duid(void)
struct iaddrcidrnetlist * next
Definition: inet.h:76
char * name
Definition: dhcpd.h:865
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:1500
struct data_string iaid_duid
Definition: dhcpd.h:1532
#define cur_time
Definition: dhcpd.h:1926
Definition: ip.h:47
int refcnt
Definition: dhcpd.h:1531
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:1568
isc_result_t renew_leases(struct ia_xx *ia)
Definition: mdb6.c:2126
int refcnt
Definition: dhcpd.h:1498
isc_result_t decline_leases(struct ia_xx *ia)
Definition: mdb6.c:2142
iasubopt_hash_t * leases
Definition: dhcpd.h:1570
int num_iasubopt
Definition: dhcpd.h:1534
int int log_info(const char *,...) __attribute__((__format__(__printf__
u_int16_t ia_type
Definition: dhcpd.h:1533
binding_state_t state
Definition: dhcpd.h:1501
struct interface_info * interfaces
Definition: discover.c:40
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:1254
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:1564
void isc_heap_destroy(isc_heap_t **heapp)
Destroys a heap.
int refcnt
Definition: dhcpd.h:1565
struct iaddrcidrnetlist * fixed_prefix
Definition: dhcpd.h:874
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:1046
void isc_heap_increased(isc_heap_t *heap, unsigned int index)
Indicates to the heap that an element&#39;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:1226
struct interface_info * next
Definition: dhcpd.h:1242
isc_heap_t * inactive_timeouts
Definition: dhcpd.h:1574
#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
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_heap_t * active_timeouts
Definition: dhcpd.h:1572
isc_result_t decline_lease6(struct ipv6_pool *pool, struct iasubopt *lease)
Definition: mdb6.c:1555
void schedule_lease_timeout(struct ipv6_pool *pool)
Definition: mdb6.c:1939
time_t soft_lifetime_end_time
Definition: dhcpd.h:1504
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:2427
isc_boolean_t ipv6_in_pool(const struct in6_addr *addr, const struct ipv6_pool *pool)
Definition: mdb6.c:2047
void mark_phosts_unavailable(void)
Definition: mdb6.c:2295
isc_result_t expire_lease6(struct iasubopt **leasep, struct ipv6_pool *pool, time_t now)
Definition: mdb6.c:1521
isc_result_t release_lease6(struct ipv6_pool *pool, struct iasubopt *lease)
Definition: mdb6.c:1575
#define MAX_TIME
Definition: dhcpd.h:1488
ipv6_pond structure
Definition: dhcpd.h:1593
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:2387
struct ipv6_pool ** pools
struct iasubopt ** iasubopt
Definition: dhcpd.h:1537
int write_ia(const struct ia_xx *)
Definition: db.c:519
struct ia_xx * ia
Definition: dhcpd.h:1507
struct executable_statement * on_expiry
Definition: dhcpd.h:501
const char * file
Definition: dhcpd.h:3535
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:1499
struct executable_statement * on_commit
Definition: dhcpd.h:502
const unsigned char * data
Definition: tree.h:79
isc_result_t add_ipv6_pool(struct ipv6_pool *pool)
Definition: mdb6.c:1798
void data_string_copy(struct data_string *dest, const struct data_string *src, const char *file, int line)
Definition: alloc.c:1260
void mark_hosts_unavailable(void)
Definition: mdb6.c:2245
u_int16_t pool_type
Definition: dhcpd.h:1566
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:491
void schedule_all_ipv6_lease_timeouts(void)
Definition: mdb6.c:1982
struct buffer * buffer
Definition: tree.h:78
int num_active
Definition: dhcpd.h:1571
#define FTS_ACTIVE
Definition: dhcpd.h:485
struct in6_addr * v6addresses
Definition: dhcpd.h:1252
int num_pools