ISC DHCP  4.3.5
A reference DHCPv4 and DHCPv6 implementation
dhc6.c
Go to the documentation of this file.
1 /* dhc6.c - DHCPv6 client routines. */
2 
3 /*
4  * Copyright (c) 2012-2016 by Internet Systems Consortium, Inc. ("ISC")
5  * Copyright (c) 2006-2010 by Internet Systems Consortium, Inc. ("ISC")
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  *
19  * Internet Systems Consortium, Inc.
20  * 950 Charter Street
21  * Redwood City, CA 94063
22  * <info@isc.org>
23  * https://www.isc.org/
24  */
25 
26 #include "dhcpd.h"
27 
28 #ifdef DHCPv6
29 
30 struct sockaddr_in6 DHCPv6DestAddr;
31 
32 /*
33  * Option definition structures that are used by the software - declared
34  * here once and assigned at startup to save lookups.
35  */
36 struct option *clientid_option = NULL;
37 struct option *elapsed_option = NULL;
38 struct option *ia_na_option = NULL;
39 struct option *ia_ta_option = NULL;
40 struct option *ia_pd_option = NULL;
41 struct option *iaaddr_option = NULL;
42 struct option *iaprefix_option = NULL;
43 struct option *oro_option = NULL;
44 struct option *irt_option = NULL;
45 
46 static struct dhc6_lease *dhc6_dup_lease(struct dhc6_lease *lease,
47  const char *file, int line);
48 static struct dhc6_ia *dhc6_dup_ia(struct dhc6_ia *ia,
49  const char *file, int line);
50 static struct dhc6_addr *dhc6_dup_addr(struct dhc6_addr *addr,
51  const char *file, int line);
52 static void dhc6_ia_destroy(struct dhc6_ia **src, const char *file, int line);
53 static isc_result_t dhc6_parse_ia_na(struct dhc6_ia **pia,
54  struct packet *packet,
55  struct option_state *options,
56  unsigned code);
57 static isc_result_t dhc6_parse_ia_ta(struct dhc6_ia **pia,
58  struct packet *packet,
59  struct option_state *options,
60  unsigned code);
61 static isc_result_t dhc6_parse_ia_pd(struct dhc6_ia **pia,
62  struct packet *packet,
63  struct option_state *options,
64  unsigned code);
65 static isc_result_t dhc6_parse_addrs(struct dhc6_addr **paddr,
66  struct packet *packet,
67  struct option_state *options);
68 static isc_result_t dhc6_parse_prefixes(struct dhc6_addr **ppref,
69  struct packet *packet,
70  struct option_state *options);
71 static struct dhc6_ia *find_ia(struct dhc6_ia *head,
72  u_int16_t type, const char *id);
73 static struct dhc6_addr *find_addr(struct dhc6_addr *head,
74  struct iaddr *address);
75 static struct dhc6_addr *find_pref(struct dhc6_addr *head,
76  struct iaddr *prefix, u_int8_t plen);
77 void init_handler(struct packet *packet, struct client_state *client);
78 void info_request_handler(struct packet *packet, struct client_state *client);
79 void rapid_commit_handler(struct packet *packet, struct client_state *client);
80 void do_init6(void *input);
81 void do_info_request6(void *input);
82 void do_confirm6(void *input);
83 void reply_handler(struct packet *packet, struct client_state *client);
84 static isc_result_t dhc6_create_iaid(struct client_state *client,
85  struct data_string *ia,
86  int idx,
87  unsigned len);
88 static int dhc6_count_ia(struct dhc6_lease *lease,
89  u_int16_t ia_type);
90 static isc_result_t dhc6_bare_ia_xx(struct client_state *client,
91  struct data_string *packet,
92  int wanted,
93  u_int16_t ia_type);
94 static isc_result_t dhc6_add_ia_na(struct client_state *client,
95  struct data_string *packet,
96  struct dhc6_lease *lease,
97  u_int8_t message,
98  int wanted,
99  int *added);
100 static isc_result_t dhc6_add_ia_ta(struct client_state *client,
101  struct data_string *packet,
102  struct dhc6_lease *lease,
103  u_int8_t message,
104  int wanted,
105  int *added);
106 static isc_result_t dhc6_add_ia_pd(struct client_state *client,
107  struct data_string *packet,
108  struct dhc6_lease *lease,
109  u_int8_t message,
110  int wanted,
111  int *added);
112 static isc_boolean_t stopping_finished(void);
113 static void dhc6_merge_lease(struct dhc6_lease *src, struct dhc6_lease *dst);
114 void do_select6(void *input);
115 void do_refresh6(void *input);
116 static void do_release6(void *input);
117 static void start_bound(struct client_state *client);
118 static void start_decline6(struct client_state *client);
119 static void do_decline6(void *input);
120 static void start_informed(struct client_state *client);
121 void informed_handler(struct packet *packet, struct client_state *client);
122 void bound_handler(struct packet *packet, struct client_state *client);
123 void start_renew6(void *input);
124 void start_rebind6(void *input);
125 void do_depref(void *input);
126 void do_expire(void *input);
127 static void make_client6_options(struct client_state *client,
128  struct option_state **op,
129  struct dhc6_lease *lease, u_int8_t message);
130 static void script_write_params6(struct client_state *client,
131  const char *prefix,
132  struct option_state *options);
133 static void script_write_requested6(struct client_state *client);
134 static isc_boolean_t active_prefix(struct client_state *client);
135 
136 static int check_timing6(struct client_state *client, u_int8_t msg_type,
137  char *msg_str, struct dhc6_lease *lease,
138  struct data_string *ds);
139 static isc_result_t dhc6_get_status_code(struct option_state *options,
140  unsigned *code,
141  struct data_string *msg);
142 static isc_result_t dhc6_check_status(isc_result_t rval,
143  struct option_state *options,
144  const char *scope,
145  unsigned *code);
146 
147 extern int onetry;
148 extern int stateless;
149 
150 /*
151  * Assign DHCPv6 port numbers as a client.
152  */
153 void
155 {
156  struct servent *ent;
157  unsigned code;
158 
159  if (path_dhclient_pid == NULL)
161  if (path_dhclient_db == NULL)
163 
164  if (local_port == 0) {
165  ent = getservbyname("dhcpv6-client", "udp");
166  if (ent == NULL)
167  local_port = htons(546);
168  else
169  local_port = ent->s_port;
170  }
171 
172  if (remote_port == 0) {
173  ent = getservbyname("dhcpv6-server", "udp");
174  if (ent == NULL)
175  remote_port = htons(547);
176  else
177  remote_port = ent->s_port;
178  }
179 
180  memset(&DHCPv6DestAddr, 0, sizeof(DHCPv6DestAddr));
181  DHCPv6DestAddr.sin6_family = AF_INET6;
182  DHCPv6DestAddr.sin6_port = remote_port;
183  if (inet_pton(AF_INET6, All_DHCP_Relay_Agents_and_Servers,
184  &DHCPv6DestAddr.sin6_addr) <= 0) {
185  log_fatal("Bad address %s", All_DHCP_Relay_Agents_and_Servers);
186  }
187 
188  code = D6O_CLIENTID;
189  if (!option_code_hash_lookup(&clientid_option,
190  dhcpv6_universe.code_hash, &code, 0, MDL))
191  log_fatal("Unable to find the CLIENTID option definition.");
192 
193  code = D6O_ELAPSED_TIME;
194  if (!option_code_hash_lookup(&elapsed_option,
195  dhcpv6_universe.code_hash, &code, 0, MDL))
196  log_fatal("Unable to find the ELAPSED_TIME option definition.");
197 
198  code = D6O_IA_NA;
199  if (!option_code_hash_lookup(&ia_na_option, dhcpv6_universe.code_hash,
200  &code, 0, MDL))
201  log_fatal("Unable to find the IA_NA option definition.");
202 
203  code = D6O_IA_TA;
204  if (!option_code_hash_lookup(&ia_ta_option, dhcpv6_universe.code_hash,
205  &code, 0, MDL))
206  log_fatal("Unable to find the IA_TA option definition.");
207 
208  code = D6O_IA_PD;
209  if (!option_code_hash_lookup(&ia_pd_option, dhcpv6_universe.code_hash,
210  &code, 0, MDL))
211  log_fatal("Unable to find the IA_PD option definition.");
212 
213  code = D6O_IAADDR;
214  if (!option_code_hash_lookup(&iaaddr_option, dhcpv6_universe.code_hash,
215  &code, 0, MDL))
216  log_fatal("Unable to find the IAADDR option definition.");
217 
218  code = D6O_IAPREFIX;
219  if (!option_code_hash_lookup(&iaprefix_option,
221  &code, 0, MDL))
222  log_fatal("Unable to find the IAPREFIX option definition.");
223 
224  code = D6O_ORO;
225  if (!option_code_hash_lookup(&oro_option, dhcpv6_universe.code_hash,
226  &code, 0, MDL))
227  log_fatal("Unable to find the ORO option definition.");
228 
230  if (!option_code_hash_lookup(&irt_option, dhcpv6_universe.code_hash,
231  &code, 0, MDL))
232  log_fatal("Unable to find the IRT option definition.");
233 
234 #ifndef __CYGWIN32__ /* XXX */
235  endservent();
236 #endif
237 }
238 
239 /*
240  * Instead of implementing RFC3315 RAND (section 14) as a float "between"
241  * -0.1 and 0.1 non-inclusive, we implement it as an integer.
242  *
243  * The result is expected to follow this table:
244  *
245  * split range answer
246  * - ERROR - base <= 0
247  * 0 1 0..0 1 <= base <= 10
248  * 1 3 -1..1 11 <= base <= 20
249  * 2 5 -2..2 21 <= base <= 30
250  * 3 7 -3..3 31 <= base <= 40
251  * ...
252  *
253  * XXX: For this to make sense, we really need to do timing on a
254  * XXX: usec scale...we currently can assume zero for any value less than
255  * XXX: 11, which are very common in early stages of transmission for most
256  * XXX: messages.
257  */
258 static TIME
259 dhc6_rand(TIME base)
260 {
261  TIME rval;
262  TIME range;
263  TIME split;
264 
265  /*
266  * A zero or less timeout is a bad thing...we don't want to
267  * DHCP-flood anyone.
268  */
269  if (base <= 0)
270  log_fatal("Impossible condition at %s:%d.", MDL);
271 
272  /*
273  * The first thing we do is count how many random integers we want
274  * in either direction (best thought of as the maximum negative
275  * integer, as we will subtract this potentially from a random 0).
276  */
277  split = (base - 1) / 10;
278 
279  /* Don't bother with the rest of the math if we know we'll get 0. */
280  if (split == 0)
281  return 0;
282 
283  /*
284  * Then we count the total number of integers in this set. This
285  * is twice the number of integers in positive and negative
286  * directions, plus zero (-1, 0, 1 is 3, -2..2 adds 2 to 5, so forth).
287  */
288  range = (split * 2) + 1;
289 
290  /* Take a random number from [0..(range-1)]. */
291  rval = random();
292  rval %= range;
293 
294  /* Offset it to uncover potential negative values. */
295  rval -= split;
296 
297  return rval;
298 }
299 
300 /* Initialize message exchange timers (set RT from Initial-RT). */
301 static void
302 dhc6_retrans_init(struct client_state *client)
303 {
304  int xid;
305 
306  /* Initialize timers. */
307  client->txcount = 0;
308  client->RT = client->IRT + dhc6_rand(client->IRT);
309 
310  /* Generate a new random 24-bit transaction ID for this exchange. */
311 
312 #if (RAND_MAX >= 0x00ffffff)
313  xid = random();
314 #elif (RAND_MAX >= 0x0000ffff)
315  xid = (random() << 16) ^ random();
316 #elif (RAND_MAX >= 0x000000ff)
317  xid = (random() << 16) ^ (random() << 8) ^ random();
318 #else
319 # error "Random number generator of less than 8 bits not supported."
320 #endif
321 
322  client->dhcpv6_transaction_id[0] = (xid >> 16) & 0xff;
323  client->dhcpv6_transaction_id[1] = (xid >> 8) & 0xff;
324  client->dhcpv6_transaction_id[2] = xid & 0xff;
325 }
326 
327 /* Advance the DHCPv6 retransmission state once. */
328 static void
329 dhc6_retrans_advance(struct client_state *client)
330 {
331  struct timeval elapsed, elapsed_plus_rt;
332 
333  /* elapsed = cur - start */
334  elapsed.tv_sec = cur_tv.tv_sec - client->start_time.tv_sec;
335  elapsed.tv_usec = cur_tv.tv_usec - client->start_time.tv_usec;
336  if (elapsed.tv_usec < 0) {
337  elapsed.tv_sec -= 1;
338  elapsed.tv_usec += 1000000;
339  }
340  /* retrans_advance is called after consuming client->RT. */
341  /* elapsed += RT */
342  elapsed.tv_sec += client->RT / 100;
343  elapsed.tv_usec += (client->RT % 100) * 10000;
344  if (elapsed.tv_usec >= 1000000) {
345  elapsed.tv_sec += 1;
346  elapsed.tv_usec -= 1000000;
347  }
348  /*
349  * Save what the time will be after the current RT to determine
350  * what the delta to MRD will be.
351  */
352  elapsed_plus_rt.tv_sec = elapsed.tv_sec;
353  elapsed_plus_rt.tv_usec = elapsed.tv_usec;
354 
355  /*
356  * RT for each subsequent message transmission is based on the previous
357  * value of RT:
358  *
359  * RT = 2*RTprev + RAND*RTprev
360  */
361  client->RT += client->RT + dhc6_rand(client->RT);
362 
363  /*
364  * MRT specifies an upper bound on the value of RT (disregarding the
365  * randomization added by the use of RAND). If MRT has a value of 0,
366  * there is no upper limit on the value of RT. Otherwise:
367  *
368  * if (RT > MRT)
369  * RT = MRT + RAND*MRT
370  */
371  if ((client->MRT != 0) && (client->RT > client->MRT))
372  client->RT = client->MRT + dhc6_rand(client->MRT);
373 
374  /*
375  * Further, if there's an MRD, we should wake up upon reaching
376  * the MRD rather than at some point after it.
377  */
378  if (client->MRD == 0) {
379  /* Done. */
380  client->txcount++;
381  return;
382  }
383  /* elapsed += client->RT */
384  elapsed.tv_sec += client->RT / 100;
385  elapsed.tv_usec += (client->RT % 100) * 10000;
386  if (elapsed.tv_usec >= 1000000) {
387  elapsed.tv_sec += 1;
388  elapsed.tv_usec -= 1000000;
389  }
390  if (elapsed.tv_sec >= client->MRD) {
391  /*
392  * The desired RT is the time that will be remaining in MRD
393  * when the current timeout finishes. We then have
394  * desired RT = MRD - (elapsed time + previous RT); or
395  * desired RT = MRD - elapsed_plut_rt;
396  */
397  client->RT = client->MRD - elapsed_plus_rt.tv_sec;
398  client->RT = (client->RT * 100) -
399  (elapsed_plus_rt.tv_usec / 10000);
400  if (client->RT < 0)
401  client->RT = 0;
402  }
403  client->txcount++;
404 }
405 
406 /* Quick validation of DHCPv6 ADVERTISE packet contents. */
407 static int
408 valid_reply(struct packet *packet, struct client_state *client)
409 {
410  struct data_string sid, cid;
411  struct option_cache *oc;
412  int rval = ISC_TRUE;
413 
414  memset(&sid, 0, sizeof(sid));
415  memset(&cid, 0, sizeof(cid));
416 
418  log_error("Response without a server identifier received.");
419  rval = ISC_FALSE;
420  }
421 
423  if (!oc ||
424  !evaluate_option_cache(&sid, packet, NULL, client, packet->options,
425  client->sent_options, &global_scope, oc,
426  MDL)) {
427  log_error("Response without a client identifier.");
428  rval = ISC_FALSE;
429  }
430 
432  D6O_CLIENTID);
433  if (!oc ||
434  !evaluate_option_cache(&cid, packet, NULL, client,
435  client->sent_options, NULL, &global_scope,
436  oc, MDL)) {
437  log_error("Local client identifier is missing!");
438  rval = ISC_FALSE;
439  }
440 
441  if (sid.len == 0 ||
442  sid.len != cid.len ||
443  memcmp(sid.data, cid.data, sid.len)) {
444  log_error("Advertise with matching transaction ID, but "
445  "mismatching client id.");
446  rval = ISC_FALSE;
447  }
448 
449  /* clean up pointers to the strings */
450  if (sid.data != NULL)
451  data_string_forget(&sid, MDL);
452  if (cid.data != NULL)
453  data_string_forget(&cid, MDL);
454 
455  return rval;
456 }
457 
458 /*
459  * Create a complete copy of a DHCPv6 lease structure.
460  */
461 static struct dhc6_lease *
462 dhc6_dup_lease(struct dhc6_lease *lease, const char *file, int line)
463 {
464  struct dhc6_lease *copy;
465  struct dhc6_ia **insert_ia, *ia;
466 
467  copy = dmalloc(sizeof(*copy), file, line);
468  if (copy == NULL) {
469  log_error("Out of memory for v6 lease structure.");
470  return NULL;
471  }
472 
473  data_string_copy(&copy->server_id, &lease->server_id, file, line);
474  copy->pref = lease->pref;
475 
476  memcpy(copy->dhcpv6_transaction_id, lease->dhcpv6_transaction_id,
477  sizeof(copy->dhcpv6_transaction_id));
478 
479  option_state_reference(&copy->options, lease->options, file, line);
480 
481  insert_ia = &copy->bindings;
482  for (ia = lease->bindings ; ia != NULL ; ia = ia->next) {
483  *insert_ia = dhc6_dup_ia(ia, file, line);
484 
485  if (*insert_ia == NULL) {
486  dhc6_lease_destroy(&copy, file, line);
487  return NULL;
488  }
489 
490  insert_ia = &(*insert_ia)->next;
491  }
492 
493  return copy;
494 }
495 
496 /*
497  * Duplicate an IA structure.
498  */
499 static struct dhc6_ia *
500 dhc6_dup_ia(struct dhc6_ia *ia, const char *file, int line)
501 {
502  struct dhc6_ia *copy;
503  struct dhc6_addr **insert_addr, *addr;
504 
505  copy = dmalloc(sizeof(*ia), file, line);
506  if (copy == NULL) {
507  log_error("Out of memory for v6 duplicate IA structure.");
508  return NULL;
509  }
510 
511  memcpy(copy->iaid, ia->iaid, sizeof(copy->iaid));
512 
513  copy->ia_type = ia->ia_type;
514  copy->starts = ia->starts;
515  copy->renew = ia->renew;
516  copy->rebind = ia->rebind;
517 
518  insert_addr = &copy->addrs;
519  for (addr = ia->addrs ; addr != NULL ; addr = addr->next) {
520  *insert_addr = dhc6_dup_addr(addr, file, line);
521 
522  if (*insert_addr == NULL) {
523  dhc6_ia_destroy(&copy, file, line);
524  return NULL;
525  }
526 
527  insert_addr = &(*insert_addr)->next;
528  }
529 
530  if (ia->options != NULL)
532  file, line);
533 
534  return copy;
535 }
536 
537 /*
538  * Duplicate an IAADDR or IAPREFIX structure.
539  */
540 static struct dhc6_addr *
541 dhc6_dup_addr(struct dhc6_addr *addr, const char *file, int line)
542 {
543  struct dhc6_addr *copy;
544 
545  copy = dmalloc(sizeof(*addr), file, line);
546 
547  if (copy == NULL)
548  return NULL;
549 
550  memcpy(&copy->address, &addr->address, sizeof(copy->address));
551 
552  copy->plen = addr->plen;
553  copy->flags = addr->flags;
554  copy->starts = addr->starts;
555  copy->preferred_life = addr->preferred_life;
556  copy->max_life = addr->max_life;
557 
558  if (addr->options != NULL)
559  option_state_reference(&copy->options, addr->options,
560  file, line);
561 
562  return copy;
563 }
564 
565 /*
566  * Form a DHCPv6 lease structure based upon packet contents. Creates and
567  * populates IA's and any IAADDR/IAPREFIX's they contain.
568  * Parsed options are deleted in order to not save them in the lease file.
569  *
570  * If we get a status code of NoAddrs or NoPrefix we toss the affected
571  * IAs. If it as at the top level we toss all IAs of that type. If it
572  * is in an IA we only toss that one. According to the spec we shouldn't
573  * get a NoPrefix status at the top level but we will allow it.
574  *
575  */
576 static struct dhc6_lease *
577 dhc6_leaseify(struct packet *packet, struct client_state* client)
578 {
579  struct data_string ds;
580  struct dhc6_lease *lease;
581  struct option_cache *oc;
582  unsigned code;
583 
584  lease = dmalloc(sizeof(*lease), MDL);
585  if (lease == NULL) {
586  log_error("Out of memory for v6 lease structure.");
587  return NULL;
588  }
589 
590  memcpy(lease->dhcpv6_transaction_id, packet->dhcpv6_transaction_id, 3);
591  option_state_reference(&lease->options, packet->options, MDL);
592 
593  memset(&ds, 0, sizeof(ds));
594 
595  /* Determine preference (default zero). */
597  if (oc &&
598  evaluate_option_cache(&ds, packet, NULL, NULL, lease->options,
599  NULL, &global_scope, oc, MDL)) {
600  if (ds.len != 1) {
601  log_error("Invalid length of DHCPv6 Preference option "
602  "(%d != 1)", ds.len);
603  data_string_forget(&ds, MDL);
604  dhc6_lease_destroy(&lease, MDL);
605  return NULL;
606  } else {
607  lease->pref = ds.data[0];
608  log_debug("RCV: X-- Preference %u.",
609  (unsigned)lease->pref);
610  }
611 
612  data_string_forget(&ds, MDL);
613  }
615 
616  /* Get the top level status code. If the code is NoAddrsAvail
617  * or NoPrefixAvail strip it from the options as we don't
618  * want it to show up in check_[advertise reply]. We
619  * pass it along to the parse_ia_xx routines and they
620  * will drop the affected IAs for NoAddrs or NoPrefix,
621  * other status codes will be ignored and handled by
622  * the check_[advertise reply] routines.
623  */
624  code = STATUS_Success;
625  if ((dhc6_get_status_code(lease->options, &code, NULL) == ISC_R_SUCCESS)
626  &&
627  ((code == STATUS_NoAddrsAvail) || (code == STATUS_NoPrefixAvail))) {
630  }
631 
632  /*
633  * Dig into recursive DHCPv6 pockets for IA_NA and contained IAADDR
634  * options.
635  */
636  if (dhc6_parse_ia_na(&lease->bindings, packet,
637  lease->options, code) != ISC_R_SUCCESS) {
638  /* Error conditions are logged by the caller. */
639  dhc6_lease_destroy(&lease, MDL);
640  return NULL;
641  }
642  /*
643  * Dig into recursive DHCPv6 pockets for IA_TA and contained IAADDR
644  * options.
645  */
646  if (dhc6_parse_ia_ta(&lease->bindings, packet,
647  lease->options, code) != ISC_R_SUCCESS) {
648  /* Error conditions are logged by the caller. */
649  dhc6_lease_destroy(&lease, MDL);
650  return NULL;
651  }
652  /*
653  * Dig into recursive DHCPv6 pockets for IA_PD and contained IAPREFIX
654  * options.
655  */
656  if (dhc6_parse_ia_pd(&lease->bindings, packet,
657  lease->options, code) != ISC_R_SUCCESS) {
658  /* Error conditions are logged by the caller. */
659  dhc6_lease_destroy(&lease, MDL);
660  return NULL;
661  }
662 
663  /*
664  * This is last because in the future we may want to make a different
665  * key based upon additional information from the packet (we may need
666  * to allow multiple leases in one client state per server, but we're
667  * not sure based on what additional keys now).
668  */
670  if ((oc == NULL) ||
671  !evaluate_option_cache(&lease->server_id, packet, NULL, NULL,
672  lease->options, NULL, &global_scope,
673  oc, MDL) ||
674  lease->server_id.len == 0) {
675  /* This should be impossible due to validation checks earlier.
676  */
677  log_error("Invalid SERVERID option cache.");
678  dhc6_lease_destroy(&lease, MDL);
679  return NULL;
680  } else {
681  log_debug("RCV: X-- Server ID: %s",
682  print_hex_1(lease->server_id.len,
683  lease->server_id.data, 52));
684  }
685 
686  execute_statements_in_scope(NULL, (struct packet *)packet, NULL,
687  client, lease->options, lease->options,
688  &global_scope, client->config->on_receipt,
689  NULL, NULL);
690 
691  return lease;
692 }
693 
694 static isc_result_t
695 dhc6_parse_ia_na(struct dhc6_ia **pia, struct packet *packet,
696  struct option_state *options, unsigned code)
697 {
698  struct data_string ds;
699  struct dhc6_ia *ia;
700  struct option_cache *oc;
701  isc_result_t result;
702  unsigned ia_code;
703 
704  memset(&ds, 0, sizeof(ds));
705 
706  oc = lookup_option(&dhcpv6_universe, options, D6O_IA_NA);
707  for ( ; oc != NULL ; oc = oc->next) {
708  ia = dmalloc(sizeof(*ia), MDL);
709  if (ia == NULL) {
710  log_error("Out of memory allocating IA_NA structure.");
711  return ISC_R_NOMEMORY;
712  } else if (evaluate_option_cache(&ds, packet, NULL, NULL,
713  options, NULL,
714  &global_scope, oc, MDL) &&
715  ds.len >= 12) {
716  memcpy(ia->iaid, ds.data, 4);
717  ia->ia_type = D6O_IA_NA;
718  ia->starts = cur_time;
719  ia->renew = getULong(ds.data + 4);
720  ia->rebind = getULong(ds.data + 8);
721 
722  log_debug("RCV: X-- IA_NA %s",
723  print_hex_1(4, ia->iaid, 59));
724  /* XXX: This should be the printed time I think. */
725  log_debug("RCV: | X-- starts %u",
726  (unsigned)ia->starts);
727  log_debug("RCV: | X-- t1 - renew +%u", ia->renew);
728  log_debug("RCV: | X-- t2 - rebind +%u", ia->rebind);
729 
730  /*
731  * RFC3315 section 22.4, discard IA_NA's that
732  * have t1 greater than t2, and both not zero.
733  * Since RFC3315 defines this behaviour, it is not
734  * an error - just normal operation.
735  *
736  * Note that RFC3315 says we MUST honor these values
737  * if they are not zero. So insane values are
738  * totally OK.
739  */
740  if ((ia->renew > 0) && (ia->rebind > 0) &&
741  (ia->renew > ia->rebind)) {
742  log_debug("RCV: | !-- INVALID renew/rebind "
743  "times, IA_NA discarded.");
744  dfree(ia, MDL);
745  data_string_forget(&ds, MDL);
746  continue;
747  }
748 
749  if (ds.len > 12) {
750  log_debug("RCV: | X-- [Options]");
751 
752  if (!option_state_allocate(&ia->options,
753  MDL)) {
754  log_error("Out of memory allocating "
755  "IA_NA option state.");
756  dfree(ia, MDL);
757  data_string_forget(&ds, MDL);
758  return ISC_R_NOMEMORY;
759  }
760 
761  if (!parse_option_buffer(ia->options,
762  ds.data + 12,
763  ds.len - 12,
764  &dhcpv6_universe)) {
765  log_error("Corrupt IA_NA options.");
767  MDL);
768  dfree(ia, MDL);
769  data_string_forget(&ds, MDL);
770  return DHCP_R_BADPARSE;
771  }
772  }
773  data_string_forget(&ds, MDL);
774 
775  if (ia->options != NULL) {
776  result = dhc6_parse_addrs(&ia->addrs, packet,
777  ia->options);
778  if (result != ISC_R_SUCCESS) {
780  MDL);
781  dfree(ia, MDL);
782  return result;
783  }
784  }
785 
786  /* If we have no addresses or the top level status code
787  * or the status code in this IA indicate no addresses
788  * toss the IA.
789  */
790  ia_code = STATUS_Success;
791  if ((ia->addrs == NULL) ||
792  (code == STATUS_NoAddrsAvail) ||
793  ((ia->options != NULL) &&
794  (dhc6_get_status_code(ia->options, &ia_code, NULL)
795  == ISC_R_SUCCESS) &&
796  (ia_code == STATUS_NoAddrsAvail))) {
797  log_debug("RCV: | !-- Status code of "
798  "no addrs, IA_NA discarded.");
799  dhc6_ia_destroy(&ia, MDL);
800  continue;
801  }
802 
803  while (*pia != NULL)
804  pia = &(*pia)->next;
805  *pia = ia;
806  pia = &ia->next;
807  } else {
808  log_error("Invalid IA_NA option cache.");
809  dfree(ia, MDL);
810  if (ds.len != 0)
811  data_string_forget(&ds, MDL);
812  return ISC_R_UNEXPECTED;
813  }
814  }
816 
817  return ISC_R_SUCCESS;
818 }
819 
820 static isc_result_t
821 dhc6_parse_ia_ta(struct dhc6_ia **pia, struct packet *packet,
822  struct option_state *options, unsigned code)
823 {
824  struct data_string ds;
825  struct dhc6_ia *ia;
826  struct option_cache *oc;
827  isc_result_t result;
828  unsigned ia_code;
829 
830  memset(&ds, 0, sizeof(ds));
831 
832  oc = lookup_option(&dhcpv6_universe, options, D6O_IA_TA);
833  for ( ; oc != NULL ; oc = oc->next) {
834  ia = dmalloc(sizeof(*ia), MDL);
835  if (ia == NULL) {
836  log_error("Out of memory allocating IA_TA structure.");
837  return ISC_R_NOMEMORY;
838  } else if (evaluate_option_cache(&ds, packet, NULL, NULL,
839  options, NULL,
840  &global_scope, oc, MDL) &&
841  ds.len >= 4) {
842  memcpy(ia->iaid, ds.data, 4);
843  ia->ia_type = D6O_IA_TA;
844  ia->starts = cur_time;
845 
846  log_debug("RCV: X-- IA_TA %s",
847  print_hex_1(4, ia->iaid, 59));
848  /* XXX: This should be the printed time I think. */
849  log_debug("RCV: | X-- starts %u",
850  (unsigned)ia->starts);
851 
852  if (ds.len > 4) {
853  log_debug("RCV: | X-- [Options]");
854 
855  if (!option_state_allocate(&ia->options,
856  MDL)) {
857  log_error("Out of memory allocating "
858  "IA_TA option state.");
859  dfree(ia, MDL);
860  data_string_forget(&ds, MDL);
861  return ISC_R_NOMEMORY;
862  }
863 
864  if (!parse_option_buffer(ia->options,
865  ds.data + 4,
866  ds.len - 4,
867  &dhcpv6_universe)) {
868  log_error("Corrupt IA_TA options.");
870  MDL);
871  dfree(ia, MDL);
872  data_string_forget(&ds, MDL);
873  return DHCP_R_BADPARSE;
874  }
875  }
876  data_string_forget(&ds, MDL);
877 
878  if (ia->options != NULL) {
879  result = dhc6_parse_addrs(&ia->addrs, packet,
880  ia->options);
881  if (result != ISC_R_SUCCESS) {
883  MDL);
884  dfree(ia, MDL);
885  return result;
886  }
887  }
888 
889  /* If we have no addresses or the top level status code
890  * or the status code in this IA indicate no addresses
891  * toss the IA.
892  */
893  ia_code = STATUS_Success;
894  if ((ia->addrs == NULL) ||
895  (code == STATUS_NoAddrsAvail) ||
896  ((ia->options != NULL) &&
897  (dhc6_get_status_code(ia->options, &ia_code, NULL)
898  == ISC_R_SUCCESS) &&
899  (ia_code == STATUS_NoAddrsAvail))) {
900  log_debug("RCV: | !-- Status code of "
901  "no addrs, IA_TA discarded.");
902  dhc6_ia_destroy(&ia, MDL);
903  continue;
904  }
905 
906  while (*pia != NULL)
907  pia = &(*pia)->next;
908  *pia = ia;
909  pia = &ia->next;
910  } else {
911  log_error("Invalid IA_TA option cache.");
912  dfree(ia, MDL);
913  if (ds.len != 0)
914  data_string_forget(&ds, MDL);
915  return ISC_R_UNEXPECTED;
916  }
917  }
919 
920  return ISC_R_SUCCESS;
921 }
922 
923 static isc_result_t
924 dhc6_parse_ia_pd(struct dhc6_ia **pia, struct packet *packet,
925  struct option_state *options, unsigned code)
926 {
927  struct data_string ds;
928  struct dhc6_ia *ia;
929  struct option_cache *oc;
930  isc_result_t result;
931  unsigned ia_code;
932 
933  memset(&ds, 0, sizeof(ds));
934 
935  oc = lookup_option(&dhcpv6_universe, options, D6O_IA_PD);
936  for ( ; oc != NULL ; oc = oc->next) {
937  ia = dmalloc(sizeof(*ia), MDL);
938  if (ia == NULL) {
939  log_error("Out of memory allocating IA_PD structure.");
940  return ISC_R_NOMEMORY;
941  } else if (evaluate_option_cache(&ds, packet, NULL, NULL,
942  options, NULL,
943  &global_scope, oc, MDL) &&
944  ds.len >= 12) {
945  memcpy(ia->iaid, ds.data, 4);
946  ia->ia_type = D6O_IA_PD;
947  ia->starts = cur_time;
948  ia->renew = getULong(ds.data + 4);
949  ia->rebind = getULong(ds.data + 8);
950 
951  log_debug("RCV: X-- IA_PD %s",
952  print_hex_1(4, ia->iaid, 59));
953  /* XXX: This should be the printed time I think. */
954  log_debug("RCV: | X-- starts %u",
955  (unsigned)ia->starts);
956  log_debug("RCV: | X-- t1 - renew +%u", ia->renew);
957  log_debug("RCV: | X-- t2 - rebind +%u", ia->rebind);
958 
959  /*
960  * RFC3633 section 9, discard IA_PD's that
961  * have t1 greater than t2, and both not zero.
962  * Since RFC3633 defines this behaviour, it is not
963  * an error - just normal operation.
964  */
965  if ((ia->renew > 0) && (ia->rebind > 0) &&
966  (ia->renew > ia->rebind)) {
967  log_debug("RCV: | !-- INVALID renew/rebind "
968  "times, IA_PD discarded.");
969  dfree(ia, MDL);
970  data_string_forget(&ds, MDL);
971  continue;
972  }
973 
974  if (ds.len > 12) {
975  log_debug("RCV: | X-- [Options]");
976 
977  if (!option_state_allocate(&ia->options,
978  MDL)) {
979  log_error("Out of memory allocating "
980  "IA_PD option state.");
981  dfree(ia, MDL);
982  data_string_forget(&ds, MDL);
983  return ISC_R_NOMEMORY;
984  }
985 
986  if (!parse_option_buffer(ia->options,
987  ds.data + 12,
988  ds.len - 12,
989  &dhcpv6_universe)) {
990  log_error("Corrupt IA_PD options.");
992  MDL);
993  dfree(ia, MDL);
994  data_string_forget(&ds, MDL);
995  return DHCP_R_BADPARSE;
996  }
997  }
998  data_string_forget(&ds, MDL);
999 
1000  if (ia->options != NULL) {
1001  result = dhc6_parse_prefixes(&ia->addrs,
1002  packet,
1003  ia->options);
1004  if (result != ISC_R_SUCCESS) {
1006  MDL);
1007  dfree(ia, MDL);
1008  return result;
1009  }
1010  }
1011 
1012  /* If we have no prefixes or the top level status code
1013  * or the status code in this IA indicate no prefixes
1014  * toss the IA.
1015  */
1016  ia_code = STATUS_Success;
1017  if ((ia->addrs == NULL) ||
1018  (code == STATUS_NoPrefixAvail) ||
1019  ((ia->options != NULL) &&
1020  (dhc6_get_status_code(ia->options, &ia_code, NULL)
1021  == ISC_R_SUCCESS) &&
1022  (ia_code == STATUS_NoPrefixAvail))) {
1023  log_debug("RCV: | !-- Status code of "
1024  "no prefix, IA_PD discarded.");
1025  dhc6_ia_destroy(&ia, MDL);
1026  continue;
1027  }
1028 
1029  while (*pia != NULL)
1030  pia = &(*pia)->next;
1031  *pia = ia;
1032  pia = &ia->next;
1033  } else {
1034  log_error("Invalid IA_PD option cache.");
1035  dfree(ia, MDL);
1036  if (ds.len != 0)
1037  data_string_forget(&ds, MDL);
1038  return ISC_R_UNEXPECTED;
1039  }
1040  }
1042 
1043  return ISC_R_SUCCESS;
1044 }
1045 
1046 
1047 static isc_result_t
1048 dhc6_parse_addrs(struct dhc6_addr **paddr, struct packet *packet,
1049  struct option_state *options)
1050 {
1051  struct data_string ds;
1052  struct option_cache *oc;
1053  struct dhc6_addr *addr;
1054  isc_result_t rval = ISC_R_SUCCESS;
1055  unsigned code;
1056 
1057  memset(&ds, 0, sizeof(ds));
1058 
1059  oc = lookup_option(&dhcpv6_universe, options, D6O_IAADDR);
1060  for ( ; oc != NULL ; oc = oc->next) {
1061  addr = dmalloc(sizeof(*addr), MDL);
1062  if (addr == NULL) {
1063  log_error("Out of memory allocating "
1064  "address structure.");
1065  return ISC_R_NOMEMORY;
1066  } else if (evaluate_option_cache(&ds, packet, NULL, NULL,
1067  options, NULL, &global_scope,
1068  oc, MDL) &&
1069  (ds.len >= 24)) {
1070 
1071  addr->address.len = 16;
1072  memcpy(addr->address.iabuf, ds.data, 16);
1073  addr->starts = cur_time;
1074  addr->preferred_life = getULong(ds.data + 16);
1075  addr->max_life = getULong(ds.data + 20);
1076 
1077  log_debug("RCV: | | X-- IAADDR %s",
1078  piaddr(addr->address));
1079  log_debug("RCV: | | | X-- Preferred lifetime %u.",
1080  addr->preferred_life);
1081  log_debug("RCV: | | | X-- Max lifetime %u.",
1082  addr->max_life);
1083 
1084  /*
1085  * RFC 3315 section 22.6 says we must discard
1086  * addresses whose pref is later than valid.
1087  */
1088  if ((addr->preferred_life > addr->max_life)) {
1089  log_debug("RCV: | | | !-- INVALID lifetimes, "
1090  "IAADDR discarded. Check your "
1091  "server configuration.");
1092  dfree(addr, MDL);
1093  data_string_forget(&ds, MDL);
1094  continue;
1095  }
1096 
1097  /*
1098  * Fortunately this is the last recursion in the
1099  * protocol.
1100  */
1101  if (ds.len > 24) {
1102  if (!option_state_allocate(&addr->options,
1103  MDL)) {
1104  log_error("Out of memory allocating "
1105  "IAADDR option state.");
1106  dfree(addr, MDL);
1107  data_string_forget(&ds, MDL);
1108  return ISC_R_NOMEMORY;
1109  }
1110 
1111  if (!parse_option_buffer(addr->options,
1112  ds.data + 24,
1113  ds.len - 24,
1114  &dhcpv6_universe)) {
1115  log_error("Corrupt IAADDR options.");
1117  MDL);
1118  dfree(addr, MDL);
1119  data_string_forget(&ds, MDL);
1120  return DHCP_R_BADPARSE;
1121  }
1122  }
1123 
1124  data_string_forget(&ds, MDL);
1125 
1126  if (addr->options != NULL) {
1127  log_debug("RCV: | | | X-- [Options]");
1128 
1129  /* Get the status code if the return value
1130  * indicates an error or the status code
1131  * indicates no address toss the address
1132  */
1133  code = STATUS_Success;
1134  rval = dhc6_check_status(ISC_R_SUCCESS,
1135  addr->options,
1136  "IAADDR", &code);
1137  if (rval != ISC_R_SUCCESS) {
1138  log_debug("RCV: | | | X-- Status code"
1139  " issue, IAADDR discarded.");
1141  MDL);
1142  dfree(addr, MDL);
1143  continue;
1144  }
1145  }
1146 
1147  *paddr = addr;
1148  paddr = &addr->next;
1149  } else {
1150  log_error("Invalid IAADDR option cache.");
1151  dfree(addr, MDL);
1152  if (ds.len != 0)
1153  data_string_forget(&ds, MDL);
1154  return ISC_R_UNEXPECTED;
1155  }
1156  }
1158 
1159  return ISC_R_SUCCESS;
1160 }
1161 
1162 static isc_result_t
1163 dhc6_parse_prefixes(struct dhc6_addr **ppfx, struct packet *packet,
1164  struct option_state *options)
1165 {
1166  struct data_string ds;
1167  struct option_cache *oc;
1168  struct dhc6_addr *pfx;
1169  isc_result_t rval = ISC_R_SUCCESS;
1170  unsigned code;
1171 
1172  memset(&ds, 0, sizeof(ds));
1173 
1174  oc = lookup_option(&dhcpv6_universe, options, D6O_IAPREFIX);
1175  for ( ; oc != NULL ; oc = oc->next) {
1176  pfx = dmalloc(sizeof(*pfx), MDL);
1177  if (pfx == NULL) {
1178  log_error("Out of memory allocating "
1179  "prefix structure.");
1180  return ISC_R_NOMEMORY;
1181  } else if (evaluate_option_cache(&ds, packet, NULL, NULL,
1182  options, NULL, &global_scope,
1183  oc, MDL) &&
1184  (ds.len >= 25)) {
1185 
1186  pfx->preferred_life = getULong(ds.data);
1187  pfx->max_life = getULong(ds.data + 4);
1188  pfx->plen = getUChar(ds.data + 8);
1189  pfx->address.len = 16;
1190  memcpy(pfx->address.iabuf, ds.data + 9, 16);
1191  pfx->starts = cur_time;
1192 
1193  log_debug("RCV: | | X-- IAPREFIX %s/%d",
1194  piaddr(pfx->address), (int)pfx->plen);
1195  log_debug("RCV: | | | X-- Preferred lifetime %u.",
1196  pfx->preferred_life);
1197  log_debug("RCV: | | | X-- Max lifetime %u.",
1198  pfx->max_life);
1199 
1200  /* Sanity check over the prefix length */
1201  if ((pfx->plen < 4) || (pfx->plen > 128)) {
1202  log_debug("RCV: | | | !-- INVALID prefix "
1203  "length, IAPREFIX discarded. "
1204  "Check your server configuration.");
1205  dfree(pfx, MDL);
1206  data_string_forget(&ds, MDL);
1207  continue;
1208  }
1209  /*
1210  * RFC 3633 section 10 says we must discard
1211  * prefixes whose pref is later than valid.
1212  */
1213  if ((pfx->preferred_life > pfx->max_life)) {
1214  log_debug("RCV: | | | !-- INVALID lifetimes, "
1215  "IAPREFIX discarded. Check your "
1216  "server configuration.");
1217  dfree(pfx, MDL);
1218  data_string_forget(&ds, MDL);
1219  continue;
1220  }
1221 
1222  /*
1223  * Fortunately this is the last recursion in the
1224  * protocol.
1225  */
1226  if (ds.len > 25) {
1227  if (!option_state_allocate(&pfx->options,
1228  MDL)) {
1229  log_error("Out of memory allocating "
1230  "IAPREFIX option state.");
1231  dfree(pfx, MDL);
1232  data_string_forget(&ds, MDL);
1233  return ISC_R_NOMEMORY;
1234  }
1235 
1236  if (!parse_option_buffer(pfx->options,
1237  ds.data + 25,
1238  ds.len - 25,
1239  &dhcpv6_universe)) {
1240  log_error("Corrupt IAPREFIX options.");
1242  MDL);
1243  dfree(pfx, MDL);
1244  data_string_forget(&ds, MDL);
1245  return DHCP_R_BADPARSE;
1246  }
1247  }
1248 
1249  data_string_forget(&ds, MDL);
1250 
1251  if (pfx->options != NULL) {
1252  log_debug("RCV: | | | X-- [Options]");
1253 
1254  /* Get the status code if the return value
1255  * indicates an error or the status code
1256  * indicates no prefix toss the prefix
1257  */
1258  code = STATUS_Success;
1259  rval = dhc6_check_status(ISC_R_SUCCESS,
1260  pfx->options,
1261  "IAPREFIX", &code);
1262  if (rval != ISC_R_SUCCESS) {
1263  log_debug("RCV: | | | X-- Status code"
1264  " issue IAPREFIX discarded.");
1266  MDL);
1267  dfree(pfx, MDL);
1268  continue;
1269  }
1270  }
1271 
1272  *ppfx = pfx;
1273  ppfx = &pfx->next;
1274  } else {
1275  log_error("Invalid IAPREFIX option cache.");
1276  dfree(pfx, MDL);
1277  if (ds.len != 0)
1278  data_string_forget(&ds, MDL);
1279  return ISC_R_UNEXPECTED;
1280  }
1281  }
1283 
1284  return ISC_R_SUCCESS;
1285 }
1286 
1287 /* Clean up a lease object, deallocate all its parts, and set it to NULL. */
1288 void
1289 dhc6_lease_destroy(struct dhc6_lease **src, const char *file, int line)
1290 {
1291  struct dhc6_ia *ia, *nia;
1292  struct dhc6_lease *lease;
1293 
1294  if (src == NULL || *src == NULL) {
1295  log_error("Attempt to destroy null lease.");
1296  return;
1297  }
1298  lease = *src;
1299 
1300  if (lease->server_id.len != 0)
1301  data_string_forget(&lease->server_id, file, line);
1302 
1303  for (ia = lease->bindings ; ia != NULL ; ia = nia) {
1304  nia = ia->next;
1305 
1306  dhc6_ia_destroy(&ia, file, line);
1307  }
1308 
1309  if (lease->options != NULL)
1310  option_state_dereference(&lease->options, file, line);
1311 
1312  dfree(lease, file, line);
1313  *src = NULL;
1314 }
1315 
1316 /*
1317  * Traverse the addresses list, and destroy their contents, and NULL the
1318  * list pointer.
1319  */
1320 static void
1321 dhc6_ia_destroy(struct dhc6_ia **src, const char *file, int line)
1322 {
1323  struct dhc6_addr *addr, *naddr;
1324  struct dhc6_ia *ia;
1325 
1326  if (src == NULL || *src == NULL) {
1327  log_error("Attempt to destroy null IA.");
1328  return;
1329  }
1330  ia = *src;
1331 
1332  for (addr = ia->addrs ; addr != NULL ; addr = naddr) {
1333  naddr = addr->next;
1334 
1335  if (addr->options != NULL)
1336  option_state_dereference(&addr->options, file, line);
1337 
1338  dfree(addr, file, line);
1339  }
1340 
1341  if (ia->options != NULL)
1342  option_state_dereference(&ia->options, file, line);
1343 
1344  dfree(ia, file, line);
1345  *src = NULL;
1346 }
1347 
1348 /*
1349  * For a given lease, insert it into the tail of the lease list. Upon
1350  * finding a duplicate by server id, remove it and take over its position.
1351  */
1352 static void
1353 insert_lease(struct dhc6_lease **head, struct dhc6_lease *new)
1354 {
1355  while (*head != NULL) {
1356  if ((*head)->server_id.len == new->server_id.len &&
1357  memcmp((*head)->server_id.data, new->server_id.data,
1358  new->server_id.len) == 0) {
1359  new->next = (*head)->next;
1360  dhc6_lease_destroy(head, MDL);
1361  break;
1362  }
1363 
1364  head= &(*head)->next;
1365  }
1366 
1367  *head = new;
1368  return;
1369 }
1370 
1392 /* The scores for individual items. */
1393 #ifdef USE_ORIGINAL_CLIENT_LEASE_WEIGHTS
1394 #define SCORE_BINDING 50
1395 #define SCORE_ADDRESS 100
1396 #else
1397 #define SCORE_BINDING 10000
1398 #define SCORE_ADDRESS 100
1399 #endif
1400 
1401 #define SCORE_OPTION 1
1402 /* We need a lease with at least 1 binding and 1 address */
1403 #define SCORE_MIN (SCORE_BINDING + SCORE_ADDRESS)
1404 
1405 static int
1406 dhc6_score_lease(struct client_state *client, struct dhc6_lease *lease)
1407 {
1408  struct dhc6_ia *ia;
1409  struct dhc6_addr *addr;
1410  struct option **req;
1411  int i;
1412 
1413  if (lease->score)
1414  return lease->score;
1415 
1416  lease->score = SCORE_OPTION;
1417 
1418  /* If this lease lacks a required option, dump it. */
1419  /* XXX: we should be able to cache the failure... */
1420  req = client->config->required_options;
1421  if (req != NULL) {
1422  for (i = 0 ; req[i] != NULL ; i++) {
1423  if (lookup_option(&dhcpv6_universe, lease->options,
1424  req[i]->code) == NULL) {
1425  lease->score = 0;
1426  return lease->score;
1427  }
1428  }
1429  }
1430 
1431  /* If this lease contains a requested option, improve its score. */
1432  req = client->config->requested_options;
1433  if (req != NULL) {
1434  for (i = 0 ; req[i] != NULL ; i++) {
1435  if (lookup_option(&dhcpv6_universe, lease->options,
1436  req[i]->code) != NULL)
1437  lease->score += SCORE_OPTION;
1438  }
1439  }
1440 
1441  for (ia = lease->bindings ; ia != NULL ; ia = ia->next) {
1442  lease->score += SCORE_BINDING;
1443 
1444  for (addr = ia->addrs ; addr != NULL ; addr = addr->next) {
1445  lease->score += SCORE_ADDRESS;
1446  }
1447  }
1448 
1449  return lease->score;
1450 }
1451 
1452 /*
1453  * start_init6() kicks off the process, transmitting a packet and
1454  * scheduling a retransmission event.
1455  */
1456 void
1457 start_init6(struct client_state *client)
1458 {
1459  struct timeval tv;
1460 
1461  log_debug("PRC: Soliciting for leases (INIT).");
1462  client->state = S_INIT;
1463 
1464  /* Initialize timers, RFC3315 section 17.1.2. */
1465  client->IRT = SOL_TIMEOUT * 100;
1466  client->MRT = SOL_MAX_RT * 100;
1467  client->MRC = 0;
1468  /* Default is 0 (no max) but -1 changes this. */
1469  if (!onetry)
1470  client->MRD = 0;
1471  else
1472  client->MRD = client->config->timeout;
1473 
1474  dhc6_retrans_init(client);
1475 
1476  /*
1477  * RFC3315 section 17.1.2 goes out of its way:
1478  * Also, the first RT MUST be selected to be strictly greater than IRT
1479  * by choosing RAND to be strictly greater than 0.
1480  */
1481  /* if RAND < 0 then RAND = -RAND */
1482  if (client->RT <= client->IRT)
1483  client->RT = client->IRT + (client->IRT - client->RT);
1484  /* if RAND == 0 then RAND = 1 */
1485  if (client->RT <= client->IRT)
1486  client->RT = client->IRT + 1;
1487 
1488  client->v6_handler = init_handler;
1489 
1490  /*
1491  * RFC3315 section 17.1.2 says we MUST start the first packet
1492  * between 0 and SOL_MAX_DELAY seconds. The good news is
1493  * SOL_MAX_DELAY is 1.
1494  */
1495  tv.tv_sec = cur_tv.tv_sec;
1496  tv.tv_usec = cur_tv.tv_usec;
1497  tv.tv_usec += (random() % (SOL_MAX_DELAY * 100)) * 10000;
1498  if (tv.tv_usec >= 1000000) {
1499  tv.tv_sec += 1;
1500  tv.tv_usec -= 1000000;
1501  }
1502  add_timeout(&tv, do_init6, client, NULL, NULL);
1503 
1504  if (nowait)
1505  go_daemon();
1506 }
1507 
1508 /*
1509  * start_info_request6() kicks off the process, transmitting an info
1510  * request packet and scheduling a retransmission event.
1511  */
1512 void
1513 start_info_request6(struct client_state *client)
1514 {
1515  struct timeval tv;
1516 
1517  log_debug("PRC: Requesting information (INIT).");
1518  client->state = S_INIT;
1519 
1520  /* Initialize timers, RFC3315 section 18.1.5. */
1521  client->IRT = INF_TIMEOUT * 100;
1522  client->MRT = INF_MAX_RT * 100;
1523  client->MRC = 0;
1524  /* Default is 0 (no max) but -1 changes this. */
1525  if (!onetry)
1526  client->MRD = 0;
1527  else
1528  client->MRD = client->config->timeout;
1529 
1530  dhc6_retrans_init(client);
1531 
1532  client->v6_handler = info_request_handler;
1533 
1534  /*
1535  * RFC3315 section 18.1.5 says we MUST start the first packet
1536  * between 0 and INF_MAX_DELAY seconds. The good news is
1537  * INF_MAX_DELAY is 1.
1538  */
1539  tv.tv_sec = cur_tv.tv_sec;
1540  tv.tv_usec = cur_tv.tv_usec;
1541  tv.tv_usec += (random() % (INF_MAX_DELAY * 100)) * 10000;
1542  if (tv.tv_usec >= 1000000) {
1543  tv.tv_sec += 1;
1544  tv.tv_usec -= 1000000;
1545  }
1546  add_timeout(&tv, do_info_request6, client, NULL, NULL);
1547 
1548  if (nowait)
1549  go_daemon();
1550 }
1551 
1552 /* Run through the addresses in lease and return true if there's any unexpired.
1553  * Return false otherwise.
1554  */
1555 isc_boolean_t
1556 unexpired_address_in_lease(struct dhc6_lease *lease)
1557 {
1558  struct dhc6_ia *ia;
1559  struct dhc6_addr *addr;
1560 
1561  for (ia = lease->bindings ; ia != NULL ; ia = ia->next) {
1562  for (addr = ia->addrs ; addr != NULL ; addr = addr->next) {
1563  if (addr->flags & DHC6_ADDR_EXPIRED)
1564  continue;
1565 
1566  if (addr->starts + addr->max_life > cur_time) {
1567  return ISC_TRUE;
1568  }
1569  }
1570  }
1571 
1572  log_info("PRC: Previous lease is devoid of active addresses."
1573  " Re-initializing.");
1574 
1575  return ISC_FALSE;
1576 }
1577 
1578 /*
1579  * start_confirm6() kicks off an "init-reboot" version of the process, at
1580  * startup to find out if old bindings are 'fair' and at runtime whenever
1581  * a link cycles state we'll eventually want to do this.
1582  */
1583 void
1584 start_confirm6(struct client_state *client)
1585 {
1586  struct timeval tv;
1587 
1588  /* If there is no active lease, there is nothing to check. */
1589  if ((client->active_lease == NULL) ||
1590  !active_prefix(client) ||
1591  client->active_lease->released ||
1592  !unexpired_address_in_lease(client->active_lease)) {
1593  dhc6_lease_destroy(&client->active_lease, MDL);
1594  start_init6(client);
1595  return;
1596  }
1597 
1598  log_debug("PRC: Confirming active lease (INIT-REBOOT).");
1599  client->state = S_REBOOTING;
1600 
1601  /* Initialize timers, RFC3315 section 17.1.3. */
1602  client->IRT = CNF_TIMEOUT * 100;
1603  client->MRT = CNF_MAX_RT * 100;
1604  client->MRC = 0;
1605  client->MRD = CNF_MAX_RD;
1606 
1607  dhc6_retrans_init(client);
1608 
1609  client->v6_handler = reply_handler;
1610 
1611  /*
1612  * RFC3315 section 18.1.2 says we MUST start the first packet
1613  * between 0 and CNF_MAX_DELAY seconds. The good news is
1614  * CNF_MAX_DELAY is 1.
1615  */
1616  tv.tv_sec = cur_tv.tv_sec;
1617  tv.tv_usec = cur_tv.tv_usec;
1618  tv.tv_usec += (random() % (CNF_MAX_DELAY * 100)) * 10000;
1619  if (tv.tv_usec >= 1000000) {
1620  tv.tv_sec += 1;
1621  tv.tv_usec -= 1000000;
1622  }
1623 
1624  /* We do a rebind instead of a confirm if the user
1625  * is requesting PDs or previously requesed PDs or
1626  * increased the number of NAs or TAs they want
1627  * Confirms don't tell us if PDs are still on-link and
1628  * we won't add new IAs on a confirm.
1629  */
1630 
1631  if ((wanted_ia_pd != 0) ||
1632  (dhc6_count_ia(client->active_lease, D6O_IA_PD) != 0) ||
1633  (dhc6_count_ia(client->active_lease, D6O_IA_NA) < wanted_ia_na) ||
1634  (dhc6_count_ia(client->active_lease, D6O_IA_TA) < wanted_ia_ta)) {
1635  client->state = S_REBINDING;
1636  client->refresh_type = DHCPV6_REBIND;
1637  add_timeout(&tv, do_refresh6, client, NULL, NULL);
1638  } else
1639  add_timeout(&tv, do_confirm6, client, NULL, NULL);
1640 }
1641 
1642 /*
1643  * check_timing6() check on the timing for sending a v6 message
1644  * and then do the basic initialization for a v6 message.
1645  */
1646 #define CHK_TIM_SUCCESS 0
1647 #define CHK_TIM_MRC_EXCEEDED 1
1648 #define CHK_TIM_MRD_EXCEEDED 2
1649 #define CHK_TIM_ALLOC_FAILURE 3
1650 
1651 int
1652 check_timing6 (struct client_state *client, u_int8_t msg_type,
1653  char *msg_str, struct dhc6_lease *lease,
1654  struct data_string *ds)
1655 {
1656  struct timeval elapsed;
1657 
1658  /*
1659  * Start_time starts at the first transmission.
1660  */
1661  if (client->txcount == 0) {
1662  client->start_time.tv_sec = cur_tv.tv_sec;
1663  client->start_time.tv_usec = cur_tv.tv_usec;
1664  } else if ((client->MRC != 0) && (client->txcount > client->MRC)) {
1665  log_info("Max retransmission count exceeded.");
1666  return(CHK_TIM_MRC_EXCEEDED);
1667  }
1668 
1669  /* elapsed = cur - start */
1670  elapsed.tv_sec = cur_tv.tv_sec - client->start_time.tv_sec;
1671  elapsed.tv_usec = cur_tv.tv_usec - client->start_time.tv_usec;
1672  if (elapsed.tv_usec < 0) {
1673  elapsed.tv_sec -= 1;
1674  elapsed.tv_usec += 1000000;
1675  }
1676 
1677  /* Check if finished (-1 argument). */
1678  if ((client->MRD != 0) && (elapsed.tv_sec >= client->MRD)) {
1679  log_info("Max retransmission duration exceeded.");
1680  return(CHK_TIM_MRD_EXCEEDED);
1681  }
1682 
1683  memset(ds, 0, sizeof(*ds));
1684  if (!buffer_allocate(&(ds->buffer), 4, MDL)) {
1685  log_error("Unable to allocate memory for %s.", msg_str);
1686  return(CHK_TIM_ALLOC_FAILURE);
1687  }
1688  ds->data = ds->buffer->data;
1689  ds->len = 4;
1690 
1691  ds->buffer->data[0] = msg_type;
1692  memcpy(ds->buffer->data + 1, client->dhcpv6_transaction_id, 3);
1693 
1694  /* Form an elapsed option. */
1695  /* Maximum value is 65535 1/100s coded as 0xffff. */
1696  if ((elapsed.tv_sec < 0) || (elapsed.tv_sec > 655) ||
1697  ((elapsed.tv_sec == 655) && (elapsed.tv_usec > 350000))) {
1698  client->elapsed = 0xffff;
1699  } else {
1700  client->elapsed = elapsed.tv_sec * 100;
1701  client->elapsed += elapsed.tv_usec / 10000;
1702  }
1703 
1704  if (client->elapsed == 0)
1705  log_debug("XMT: Forming %s, 0 ms elapsed.", msg_str);
1706  else
1707  log_debug("XMT: Forming %s, %u0 ms elapsed.", msg_str,
1708  (unsigned)client->elapsed);
1709 
1710  client->elapsed = htons(client->elapsed);
1711 
1712  make_client6_options(client, &client->sent_options, lease, msg_type);
1713 
1714  return(CHK_TIM_SUCCESS);
1715 }
1716 
1730 static isc_result_t
1731 dhc6_create_iaid(struct client_state *client,
1732  struct data_string *ia,
1733  int idx,
1734  unsigned len)
1735 {
1736  int start_idx, copy_len;
1737 
1738  memset(ia, 0, sizeof(*ia));
1739  if (!buffer_allocate(&ia->buffer, 12, MDL)) {
1740  return (ISC_R_NOMEMORY);
1741  }
1742  ia->data = ia->buffer->data;
1743  ia->len = len;
1744 
1745  /*
1746  * A simple IAID is the last 4 bytes
1747  * of the hardware address.
1748  */
1749  if (client->interface->hw_address.hlen > 4) {
1750  start_idx = client->interface->hw_address.hlen - 4;
1751  copy_len = 4;
1752  } else {
1753  start_idx = 0;
1754  copy_len = client->interface->hw_address.hlen;
1755  }
1756  memcpy(ia->buffer->data,
1757  client->interface->hw_address.hbuf + start_idx,
1758  copy_len);
1759  if (idx)
1760  ia->buffer->data[3] += idx;
1761 
1762  return (ISC_R_SUCCESS);
1763 }
1764 
1783 static isc_result_t
1784 dhc6_bare_ia_xx(struct client_state *client,
1785  struct data_string *packet,
1786  int wanted,
1787  u_int16_t ia_type)
1788 {
1789  struct dhc6_ia *old_ia;
1790  struct data_string ia;
1791  u_int32_t t1, t2;
1792  int i, len;
1793  isc_result_t rval;
1794  char *type_string;
1795  struct option *type_option;
1796 
1797  /* figure out what type of option we are working with */
1798  switch (ia_type) {
1799  case D6O_IA_NA:
1800  type_string = "IA_NA";
1801  type_option = ia_na_option;
1802  len = 12;
1803  break;
1804  case D6O_IA_TA:
1805  type_string = "IA_TA";
1806  type_option = ia_ta_option;
1807  len = 4;
1808  break;
1809  case D6O_IA_PD:
1810  type_string = "IA_PD";
1811  type_option = ia_pd_option;
1812  len = 12;
1813  break;
1814  default:
1815  return (ISC_R_FAILURE);
1816  }
1817 
1818  for (i = 0; wanted != 0; i++) {
1819  rval = dhc6_create_iaid(client, &ia, i, len);
1820  if (rval != ISC_R_SUCCESS) {
1821  log_error("Unable to allocate memory for %s.",
1822  type_string);
1823  return (rval);
1824  }
1825 
1826  /* If we are already using this IAID, skip it and try again */
1827  if ((client->active_lease != NULL) &&
1828  ((old_ia = find_ia(client->active_lease->bindings,
1829  ia_type,
1830  (char *)ia.buffer->data)) != NULL)) {
1831  data_string_forget(&ia, MDL);
1832  continue;
1833  }
1834 
1835  /* We have a good IAID, log it */
1836  log_debug("XMT: X-- %s %s",
1837  type_string, print_hex_1(4, ia.buffer->data, 55));
1838 
1839  /* If we are requesting an NA or a PD we also want to add
1840  * the renew and rebind times we are requesting.
1841  */
1842  if (len == 12) {
1843  t1 = client->config->requested_lease / 2;
1844  t2 = t1 + (t1 / 2);
1845  putULong(ia.buffer->data + 4, t1);
1846  putULong(ia.buffer->data + 8, t2);
1847 
1848  log_debug("XMT: | X-- Request renew in +%u",
1849  (unsigned)t1);
1850  log_debug("XMT: | X-- Request rebind in +%u",
1851  (unsigned)t2);
1852  }
1853 
1854  /* and append it to the packet */
1855  append_option(packet, &dhcpv6_universe, type_option, &ia);
1856  data_string_forget(&ia, MDL);
1857 
1858  /* decrement the number of IAs we want */
1859  wanted--;
1860  }
1861 
1862  return (ISC_R_SUCCESS);
1863 }
1864 
1865 /*
1866  * do_init6() marshals and transmits a solicit.
1867  */
1868 void
1869 do_init6(void *input)
1870 {
1871  struct client_state *client;
1872  struct dhc6_ia *old_ia;
1873  struct dhc6_addr *old_addr;
1874  struct data_string ds;
1875  struct data_string ia;
1876  struct data_string addr;
1877  struct timeval tv;
1878  u_int32_t t1, t2;
1879  int i, send_ret;
1880 
1881  client = input;
1882 
1883  /*
1884  * In RFC3315 section 17.1.2, the retransmission timer is
1885  * used as the selecting timer.
1886  */
1887  if (client->advertised_leases != NULL) {
1888  start_selecting6(client);
1889  return;
1890  }
1891 
1892  switch(check_timing6(client, DHCPV6_SOLICIT, "Solicit", NULL, &ds)) {
1893  case CHK_TIM_MRC_EXCEEDED:
1894  case CHK_TIM_ALLOC_FAILURE:
1895  return;
1896  case CHK_TIM_MRD_EXCEEDED:
1897  client->state = S_STOPPED;
1898  if (client->active_lease != NULL) {
1899  dhc6_lease_destroy(&client->active_lease, MDL);
1900  client->active_lease = NULL;
1901  }
1902  /* Stop if and only if this is the last client. */
1903  if (stopping_finished())
1904  exit(2);
1905  return;
1906  }
1907 
1908  /*
1909  * Fetch any configured 'sent' options (includes DUID) in wire format.
1910  */
1911  dhcpv6_universe.encapsulate(&ds, NULL, NULL, client,
1912  NULL, client->sent_options, &global_scope,
1913  &dhcpv6_universe);
1914 
1915  /* Use a specific handler with rapid-commit. */
1917  D6O_RAPID_COMMIT) != NULL) {
1918  client->v6_handler = rapid_commit_handler;
1919  }
1920 
1921  /* Append IA_NA. */
1922  for (i = 0; i < wanted_ia_na; i++) {
1923  /*
1924  * XXX: maybe the IA_NA('s) should be put into the sent_options
1925  * cache. They'd have to be pulled down as they also contain
1926  * different option caches in the same universe...
1927  */
1928  if (dhc6_create_iaid(client, &ia, i, 12) != ISC_R_SUCCESS) {
1929  log_error("Unable to allocate memory for IA_NA.");
1930  data_string_forget(&ds, MDL);
1931  return;
1932  }
1933 
1934  t1 = client->config->requested_lease / 2;
1935  t2 = t1 + (t1 / 2);
1936  putULong(ia.buffer->data + 4, t1);
1937  putULong(ia.buffer->data + 8, t2);
1938 
1939  log_debug("XMT: X-- IA_NA %s",
1940  print_hex_1(4, ia.buffer->data, 55));
1941  log_debug("XMT: | X-- Request renew in +%u", (unsigned)t1);
1942  log_debug("XMT: | X-- Request rebind in +%u", (unsigned)t2);
1943 
1944  if ((client->active_lease != NULL) &&
1945  ((old_ia = find_ia(client->active_lease->bindings,
1946  D6O_IA_NA,
1947  (char *)ia.buffer->data)) != NULL)) {
1948  /*
1949  * For each address in the old IA_NA,
1950  * request a binding.
1951  */
1952  memset(&addr, 0, sizeof(addr));
1953  for (old_addr = old_ia->addrs ; old_addr != NULL ;
1954  old_addr = old_addr->next) {
1955  if (old_addr->address.len != 16) {
1956  log_error("Invalid IPv6 address "
1957  "length %d. "
1958  "Ignoring. (%s:%d)",
1959  old_addr->address.len,
1960  MDL);
1961  continue;
1962  }
1963 
1964  if (!buffer_allocate(&addr.buffer, 24, MDL)) {
1965  log_error("Unable to allocate memory "
1966  "for IAADDR.");
1967  data_string_forget(&ia, MDL);
1968  data_string_forget(&ds, MDL);
1969  return;
1970  }
1971  addr.data = addr.buffer->data;
1972  addr.len = 24;
1973 
1974  memcpy(addr.buffer->data,
1975  old_addr->address.iabuf,
1976  16);
1977 
1978  t1 = client->config->requested_lease;
1979  t2 = t1 + (t1 / 2);
1980  putULong(addr.buffer->data + 16, t1);
1981  putULong(addr.buffer->data + 20, t2);
1982 
1983  log_debug("XMT: | X-- Request address %s.",
1984  piaddr(old_addr->address));
1985  log_debug("XMT: | | X-- Request "
1986  "preferred in +%u",
1987  (unsigned)t1);
1988  log_debug("XMT: | | X-- Request valid "
1989  "in +%u",
1990  (unsigned)t2);
1991 
1993  iaaddr_option,
1994  &addr);
1995 
1996  data_string_forget(&addr, MDL);
1997  }
1998  }
1999 
2000  append_option(&ds, &dhcpv6_universe, ia_na_option, &ia);
2001  data_string_forget(&ia, MDL);
2002  }
2003 
2004  /* Append IA_TA. */
2005  for (i = 0; i < wanted_ia_ta; i++) {
2006  /*
2007  * XXX: maybe the IA_TA('s) should be put into the sent_options
2008  * cache. They'd have to be pulled down as they also contain
2009  * different option caches in the same universe...
2010  */
2011  if (dhc6_create_iaid(client, &ia, i, 4) != ISC_R_SUCCESS) {
2012  log_error("Unable to allocate memory for IA_TA.");
2013  data_string_forget(&ds, MDL);
2014  return;
2015  }
2016 
2017  log_debug("XMT: X-- IA_TA %s",
2018  print_hex_1(4, ia.buffer->data, 55));
2019 
2020  if ((client->active_lease != NULL) &&
2021  ((old_ia = find_ia(client->active_lease->bindings,
2022  D6O_IA_TA,
2023  (char *)ia.buffer->data)) != NULL)) {
2024  /*
2025  * For each address in the old IA_TA,
2026  * request a binding.
2027  */
2028  memset(&addr, 0, sizeof(addr));
2029  for (old_addr = old_ia->addrs ; old_addr != NULL ;
2030  old_addr = old_addr->next) {
2031  if (old_addr->address.len != 16) {
2032  log_error("Invalid IPv6 address "
2033  "length %d. "
2034  "Ignoring. (%s:%d)",
2035  old_addr->address.len,
2036  MDL);
2037  continue;
2038  }
2039 
2040  if (!buffer_allocate(&addr.buffer, 24, MDL)) {
2041  log_error("Unable to allocate memory "
2042  "for IAADDR.");
2043  data_string_forget(&ia, MDL);
2044  data_string_forget(&ds, MDL);
2045  return;
2046  }
2047  addr.data = addr.buffer->data;
2048  addr.len = 24;
2049 
2050  memcpy(addr.buffer->data,
2051  old_addr->address.iabuf,
2052  16);
2053 
2054  t1 = client->config->requested_lease;
2055  t2 = t1 + (t1 / 2);
2056  putULong(addr.buffer->data + 16, t1);
2057  putULong(addr.buffer->data + 20, t2);
2058 
2059  log_debug("XMT: | X-- Request address %s.",
2060  piaddr(old_addr->address));
2061  log_debug("XMT: | | X-- Request "
2062  "preferred in +%u",
2063  (unsigned)t1);
2064  log_debug("XMT: | | X-- Request valid "
2065  "in +%u",
2066  (unsigned)t2);
2067 
2069  iaaddr_option,
2070  &addr);
2071 
2072  data_string_forget(&addr, MDL);
2073  }
2074  }
2075 
2076  append_option(&ds, &dhcpv6_universe, ia_ta_option, &ia);
2077  data_string_forget(&ia, MDL);
2078  }
2079 
2080  /* Append IA_PD. */
2081  for (i = 0; i < wanted_ia_pd; i++) {
2082  /*
2083  * XXX: maybe the IA_PD('s) should be put into the sent_options
2084  * cache. They'd have to be pulled down as they also contain
2085  * different option caches in the same universe...
2086  */
2087  memset(&ia, 0, sizeof(ia));
2088  if (dhc6_create_iaid(client, &ia, i, 12) != ISC_R_SUCCESS) {
2089  log_error("Unable to allocate memory for IA_PD.");
2090  data_string_forget(&ds, MDL);
2091  return;
2092  }
2093 
2094  t1 = client->config->requested_lease / 2;
2095  t2 = t1 + (t1 / 2);
2096  putULong(ia.buffer->data + 4, t1);
2097  putULong(ia.buffer->data + 8, t2);
2098 
2099  log_debug("XMT: X-- IA_PD %s",
2100  print_hex_1(4, ia.buffer->data, 55));
2101  log_debug("XMT: | X-- Request renew in +%u", (unsigned)t1);
2102  log_debug("XMT: | X-- Request rebind in +%u", (unsigned)t2);
2103 
2104  if ((client->active_lease != NULL) &&
2105  ((old_ia = find_ia(client->active_lease->bindings,
2106  D6O_IA_PD,
2107  (char *)ia.buffer->data)) != NULL)) {
2108  /*
2109  * For each prefix in the old IA_PD,
2110  * request a binding.
2111  */
2112  memset(&addr, 0, sizeof(addr));
2113  for (old_addr = old_ia->addrs ; old_addr != NULL ;
2114  old_addr = old_addr->next) {
2115  if (old_addr->address.len != 16) {
2116  log_error("Invalid IPv6 prefix, "
2117  "Ignoring. (%s:%d)",
2118  MDL);
2119  continue;
2120  }
2121 
2122  if (!buffer_allocate(&addr.buffer, 25, MDL)) {
2123  log_error("Unable to allocate memory "
2124  "for IAPREFIX.");
2125  data_string_forget(&ia, MDL);
2126  data_string_forget(&ds, MDL);
2127  return;
2128  }
2129  addr.data = addr.buffer->data;
2130  addr.len = 25;
2131 
2132  t1 = client->config->requested_lease;
2133  t2 = t1 + (t1 / 2);
2134  putULong(addr.buffer->data, t1);
2135  putULong(addr.buffer->data + 4, t2);
2136 
2137  putUChar(addr.buffer->data + 8,
2138  old_addr->plen);
2139  memcpy(addr.buffer->data + 9,
2140  old_addr->address.iabuf,
2141  16);
2142 
2143  log_debug("XMT: | X-- Request prefix %s/%u.",
2144  piaddr(old_addr->address),
2145  (unsigned) old_addr->plen);
2146  log_debug("XMT: | | X-- Request "
2147  "preferred in +%u",
2148  (unsigned)t1);
2149  log_debug("XMT: | | X-- Request valid "
2150  "in +%u",
2151  (unsigned)t2);
2152 
2154  iaprefix_option,
2155  &addr);
2156 
2157  data_string_forget(&addr, MDL);
2158  }
2159  }
2160 
2161  append_option(&ds, &dhcpv6_universe, ia_pd_option, &ia);
2162  data_string_forget(&ia, MDL);
2163  }
2164 
2165  /* Transmit and wait. */
2166 
2167  log_info("XMT: Solicit on %s, interval %ld0ms.",
2168  client->name ? client->name : client->interface->name,
2169  (long int)client->RT);
2170 
2171  send_ret = send_packet6(client->interface,
2172  ds.data, ds.len, &DHCPv6DestAddr);
2173  if (send_ret != ds.len) {
2174  log_error("dhc6: send_packet6() sent %d of %d bytes",
2175  send_ret, ds.len);
2176  }
2177 
2178  data_string_forget(&ds, MDL);
2179 
2180  /* Wait RT */
2181  tv.tv_sec = cur_tv.tv_sec + client->RT / 100;
2182  tv.tv_usec = cur_tv.tv_usec + (client->RT % 100) * 10000;
2183  if (tv.tv_usec >= 1000000) {
2184  tv.tv_sec += 1;
2185  tv.tv_usec -= 1000000;
2186  }
2187  add_timeout(&tv, do_init6, client, NULL, NULL);
2188 
2189  dhc6_retrans_advance(client);
2190 }
2191 
2192 /* do_info_request6() marshals and transmits an information-request. */
2193 void
2194 do_info_request6(void *input)
2195 {
2196  struct client_state *client;
2197  struct data_string ds;
2198  struct timeval tv;
2199  int send_ret;
2200 
2201  client = input;
2202 
2203  switch(check_timing6(client, DHCPV6_INFORMATION_REQUEST,
2204  "Info-Request", NULL, &ds)) {
2205  case CHK_TIM_MRC_EXCEEDED:
2206  case CHK_TIM_ALLOC_FAILURE:
2207  return;
2208  case CHK_TIM_MRD_EXCEEDED:
2209  exit(2);
2210  case CHK_TIM_SUCCESS:
2211  break;
2212  }
2213 
2214  /* Fetch any configured 'sent' options (includes DUID) in wire format.
2215  */
2216  dhcpv6_universe.encapsulate(&ds, NULL, NULL, client,
2217  NULL, client->sent_options, &global_scope,
2218  &dhcpv6_universe);
2219 
2220  /* Transmit and wait. */
2221 
2222  log_info("XMT: Info-Request on %s, interval %ld0ms.",
2223  client->name ? client->name : client->interface->name,
2224  (long int)client->RT);
2225 
2226  send_ret = send_packet6(client->interface,
2227  ds.data, ds.len, &DHCPv6DestAddr);
2228  if (send_ret != ds.len) {
2229  log_error("dhc6: send_packet6() sent %d of %d bytes",
2230  send_ret, ds.len);
2231  }
2232 
2233  data_string_forget(&ds, MDL);
2234 
2235  /* Wait RT */
2236  tv.tv_sec = cur_tv.tv_sec + client->RT / 100;
2237  tv.tv_usec = cur_tv.tv_usec + (client->RT % 100) * 10000;
2238  if (tv.tv_usec >= 1000000) {
2239  tv.tv_sec += 1;
2240  tv.tv_usec -= 1000000;
2241  }
2242  add_timeout(&tv, do_info_request6, client, NULL, NULL);
2243 
2244  dhc6_retrans_advance(client);
2245 }
2246 
2247 /* do_confirm6() creates a Confirm packet and transmits it. This function
2248  * is called on every timeout to (re)transmit.
2249  */
2250 void
2251 do_confirm6(void *input)
2252 {
2253  struct client_state *client;
2254  struct data_string ds;
2255  int send_ret, added;
2256  struct timeval tv;
2257 
2258  client = input;
2259 
2260  if (client->active_lease == NULL)
2261  log_fatal("Impossible condition at %s:%d.", MDL);
2262 
2263  /* In section 17.1.3, it is said:
2264  *
2265  * If the client receives no responses before the message
2266  * transmission process terminates, as described in section 14,
2267  * the client SHOULD continue to use any IP addresses, using the
2268  * last known lifetimes for those addresses, and SHOULD continue
2269  * to use any other previously obtained configuration parameters.
2270  *
2271  * So if confirm times out, we go active.
2272  *
2273  * XXX: Should we reduce all IA's t1 to 0, so that we renew and
2274  * stick there until we get a reply?
2275  */
2276 
2277  switch(check_timing6(client, DHCPV6_CONFIRM, "Confirm",
2278  client->active_lease, &ds)) {
2279  case CHK_TIM_MRC_EXCEEDED:
2280  case CHK_TIM_MRD_EXCEEDED:
2281  start_bound(client);
2282  return;
2283  case CHK_TIM_ALLOC_FAILURE:
2284  return;
2285  case CHK_TIM_SUCCESS:
2286  break;
2287  }
2288 
2289  /* Fetch any configured 'sent' options (includes DUID') in wire format.
2290  */
2291  dhcpv6_universe.encapsulate(&ds, NULL, NULL, client, NULL,
2292  client->sent_options, &global_scope,
2293  &dhcpv6_universe);
2294 
2295  /* Append IA's. */
2296  if (wanted_ia_na &&
2297  dhc6_add_ia_na(client, &ds, client->active_lease,
2298  DHCPV6_CONFIRM, 0, &added) != ISC_R_SUCCESS) {
2299  data_string_forget(&ds, MDL);
2300  return;
2301  }
2302  if (wanted_ia_ta &&
2303  dhc6_add_ia_ta(client, &ds, client->active_lease,
2304  DHCPV6_CONFIRM, 0, &added) != ISC_R_SUCCESS) {
2305  data_string_forget(&ds, MDL);
2306  return;
2307  }
2308 
2309  /* Transmit and wait. */
2310 
2311  log_info("XMT: Confirm on %s, interval %ld0ms.",
2312  client->name ? client->name : client->interface->name,
2313  (long int)client->RT);
2314 
2315  send_ret = send_packet6(client->interface, ds.data, ds.len,
2316  &DHCPv6DestAddr);
2317  if (send_ret != ds.len) {
2318  log_error("dhc6: sendpacket6() sent %d of %d bytes",
2319  send_ret, ds.len);
2320  }
2321 
2322  data_string_forget(&ds, MDL);
2323 
2324  /* Wait RT */
2325  tv.tv_sec = cur_tv.tv_sec + client->RT / 100;
2326  tv.tv_usec = cur_tv.tv_usec + (client->RT % 100) * 10000;
2327  if (tv.tv_usec >= 1000000) {
2328  tv.tv_sec += 1;
2329  tv.tv_usec -= 1000000;
2330  }
2331  add_timeout(&tv, do_confirm6, client, NULL, NULL);
2332 
2333  dhc6_retrans_advance(client);
2334 }
2335 
2336 /*
2337  * Release addresses.
2338  */
2339 void
2340 start_release6(struct client_state *client)
2341 {
2342  /* Cancel any pending transmissions */
2343  cancel_timeout(do_confirm6, client);
2344  cancel_timeout(do_select6, client);
2345  cancel_timeout(do_refresh6, client);
2346  cancel_timeout(do_release6, client);
2347  cancel_timeout(do_decline6, client);
2348  client->state = S_STOPPED;
2349 
2350  /*
2351  * It is written: "The client MUST NOT use any of the addresses it
2352  * is releasing as the source address in the Release message or in
2353  * any subsequently transmitted message." So unconfigure now.
2354  */
2355  unconfigure6(client, "RELEASE6");
2356 
2357  /* Note this in the lease file. */
2358  if (client->active_lease == NULL)
2359  return;
2360  client->active_lease->released = ISC_TRUE;
2361  write_client6_lease(client, client->active_lease, 0, 1);
2362 
2363  /* Set timers per RFC3315 section 18.1.6. */
2364  client->IRT = REL_TIMEOUT * 100;
2365  client->MRT = 0;
2366  client->MRC = REL_MAX_RC;
2367  client->MRD = 0;
2368 
2369  dhc6_retrans_init(client);
2370  client->v6_handler = reply_handler;
2371 
2372  do_release6(client);
2373 }
2374 /*
2375  * do_release6() creates a Release packet and transmits it.
2376  */
2377 static void
2378 do_release6(void *input)
2379 {
2380  struct client_state *client;
2381  struct data_string ds;
2382  int send_ret, added;
2383  struct timeval tv;
2384 
2385  client = input;
2386 
2387  if ((client->active_lease == NULL) || !active_prefix(client))
2388  return;
2389 
2390  switch(check_timing6(client, DHCPV6_RELEASE, "Release",
2391  client->active_lease, &ds)) {
2392  case CHK_TIM_MRC_EXCEEDED:
2393  case CHK_TIM_ALLOC_FAILURE:
2394  case CHK_TIM_MRD_EXCEEDED:
2395  goto release_done;
2396  case CHK_TIM_SUCCESS:
2397  break;
2398  }
2399 
2400  /*
2401  * Don't use unicast as we don't know if we still have an
2402  * available address with enough scope.
2403  */
2404 
2405  dhcpv6_universe.encapsulate(&ds, NULL, NULL, client, NULL,
2406  client->sent_options, &global_scope,
2407  &dhcpv6_universe);
2408 
2409  /* Append IA's (but don't release temporary addresses). */
2410  if (wanted_ia_na &&
2411  dhc6_add_ia_na(client, &ds, client->active_lease,
2412  DHCPV6_RELEASE, 0, &added) != ISC_R_SUCCESS) {
2413  data_string_forget(&ds, MDL);
2414  goto release_done;
2415  }
2416  if (wanted_ia_pd &&
2417  dhc6_add_ia_pd(client, &ds, client->active_lease,
2418  DHCPV6_RELEASE, 0, &added) != ISC_R_SUCCESS) {
2419  data_string_forget(&ds, MDL);
2420  goto release_done;
2421  }
2422 
2423  /* Transmit and wait. */
2424  log_info("XMT: Release on %s, interval %ld0ms.",
2425  client->name ? client->name : client->interface->name,
2426  (long int)client->RT);
2427 
2428  send_ret = send_packet6(client->interface, ds.data, ds.len,
2429  &DHCPv6DestAddr);
2430  if (send_ret != ds.len) {
2431  log_error("dhc6: sendpacket6() sent %d of %d bytes",
2432  send_ret, ds.len);
2433  }
2434 
2435  data_string_forget(&ds, MDL);
2436 
2437  /* Wait RT */
2438  tv.tv_sec = cur_tv.tv_sec + client->RT / 100;
2439  tv.tv_usec = cur_tv.tv_usec + (client->RT % 100) * 10000;
2440  if (tv.tv_usec >= 1000000) {
2441  tv.tv_sec += 1;
2442  tv.tv_usec -= 1000000;
2443  }
2444  add_timeout(&tv, do_release6, client, NULL, NULL);
2445  dhc6_retrans_advance(client);
2446  return;
2447 
2448  release_done:
2449  dhc6_lease_destroy(&client->active_lease, MDL);
2450  client->active_lease = NULL;
2451  if (stopping_finished())
2452  exit(0);
2453 }
2454 
2455 /* status_log() just puts a status code into displayable form and logs it
2456  * to info level.
2457  */
2458 static void
2459 status_log(int code, const char *scope, const char *additional, int len)
2460 {
2461  const char *msg = NULL;
2462 
2463  switch(code) {
2464  case STATUS_Success:
2465  msg = "Success";
2466  break;
2467 
2468  case STATUS_UnspecFail:
2469  msg = "UnspecFail";
2470  break;
2471 
2472  case STATUS_NoAddrsAvail:
2473  msg = "NoAddrsAvail";
2474  break;
2475 
2476  case STATUS_NoBinding:
2477  msg = "NoBinding";
2478  break;
2479 
2480  case STATUS_NotOnLink:
2481  msg = "NotOnLink";
2482  break;
2483 
2484  case STATUS_UseMulticast:
2485  msg = "UseMulticast";
2486  break;
2487 
2488  case STATUS_NoPrefixAvail:
2489  msg = "NoPrefixAvail";
2490  break;
2491 
2492  default:
2493  msg = "UNKNOWN";
2494  break;
2495  }
2496 
2497  if (len > 0)
2498  log_info("%s status code %s: %s", scope, msg,
2499  print_hex_1(len,
2500  (const unsigned char *)additional, 50));
2501  else
2502  log_info("%s status code %s.", scope, msg);
2503 }
2504 
2505 /* Acquire a status code.
2506  */
2507 static isc_result_t
2508 dhc6_get_status_code(struct option_state *options, unsigned *code,
2509  struct data_string *msg)
2510 {
2511  struct option_cache *oc;
2512  struct data_string ds;
2513  isc_result_t rval = ISC_R_SUCCESS;
2514 
2515  if ((options == NULL) || (code == NULL))
2516  return DHCP_R_INVALIDARG;
2517 
2518  if ((msg != NULL) && (msg->len != 0))
2519  return DHCP_R_INVALIDARG;
2520 
2521  memset(&ds, 0, sizeof(ds));
2522 
2523  /* Assume success if there is no option. */
2524  *code = STATUS_Success;
2525 
2527  if ((oc != NULL) &&
2528  evaluate_option_cache(&ds, NULL, NULL, NULL, options,
2529  NULL, &global_scope, oc, MDL)) {
2530  if (ds.len < 2) {
2531  log_error("Invalid status code length %d.", ds.len);
2532  rval = DHCP_R_FORMERR;
2533  } else
2534  *code = getUShort(ds.data);
2535 
2536  if ((msg != NULL) && (ds.len > 2)) {
2537  data_string_copy(msg, &ds, MDL);
2538  msg->data += 2;
2539  msg->len -= 2;
2540  }
2541 
2542  data_string_forget(&ds, MDL);
2543  return rval;
2544  }
2545 
2546  return ISC_R_NOTFOUND;
2547 }
2548 
2549 /* Look at status codes in an advertise, and reform the return value.
2550  */
2551 static isc_result_t
2552 dhc6_check_status(isc_result_t rval, struct option_state *options,
2553  const char *scope, unsigned *code)
2554 {
2555  struct data_string msg;
2556  isc_result_t status;
2557 
2558  if ((scope == NULL) || (code == NULL))
2559  return DHCP_R_INVALIDARG;
2560 
2561  /* If we don't find a code, we assume success. */
2562  *code = STATUS_Success;
2563 
2564  /* If there is no options cache, then there is no code. */
2565  if (options != NULL) {
2566  memset(&msg, 0, sizeof(msg));
2567  status = dhc6_get_status_code(options, code, &msg);
2568 
2569  if (status == ISC_R_SUCCESS) {
2570  status_log(*code, scope, (char *)msg.data, msg.len);
2571  data_string_forget(&msg, MDL);
2572 
2573  if (*code != STATUS_Success)
2574  rval = ISC_R_FAILURE;
2575 
2576  } else if (status != ISC_R_NOTFOUND)
2577  rval = status;
2578  }
2579 
2580  return rval;
2581 }
2582 
2583 /* Determine if this packet could provide usable information.
2584  * We check the status codes at the top level and at the IA level,
2585  * IAADDRS have already been checked in the leaseify step and any with
2586  * a bad format or status code that wasn't success have been dropped.
2587  *
2588  * leaseify has also already removed any IAs for which the top level status
2589  * code or the IA status code indicated no addresses or prefixes were
2590  * available.
2591  */
2592 static isc_result_t
2593 dhc6_check_advertise(struct dhc6_lease *lease)
2594 {
2595  struct dhc6_ia *ia;
2596  isc_result_t rval = ISC_R_SUCCESS;
2597  int have_addrs = ISC_FALSE;
2598  unsigned code;
2599  const char *scope;
2600  int got_na = 0, got_ta = 0, got_pd = 0;
2601 
2602  rval = dhc6_check_status(rval, lease->options, "message", &code);
2603 
2604  for (ia = lease->bindings ; ia != NULL ; ia = ia->next) {
2605  switch (ia->ia_type) {
2606  case D6O_IA_NA:
2607  scope = "IA_NA";
2608  got_na++;
2609  break;
2610  case D6O_IA_TA:
2611  scope = "IA_TA";
2612  got_ta++;
2613  break;
2614  case D6O_IA_PD:
2615  scope = "IA_PD";
2616  got_pd++;
2617  break;
2618  default:
2619  log_error("dhc6_check_advertise: no type.");
2620  return ISC_R_FAILURE;
2621  }
2622  /* Currently we toss packets if we have an error getting a
2623  * status code or if the status code isn't success, so
2624  * no need to loop through the addresses */
2625  rval = dhc6_check_status(rval, ia->options, scope, &code);
2626  if (rval != ISC_R_SUCCESS)
2627  continue;
2628 
2629  /* We don't need to check status on IAADDRS here as we already
2630  * did it as part of the leaseify step and tossed bad IAADDRS.
2631  * We are just checking to see if we have any addrs.
2632  * Should we check the addr itself for usability?
2633  */
2634  if (ia->addrs != NULL) {
2635  have_addrs = ISC_TRUE;
2636  }
2637  }
2638 
2639  /* If we didn't get some addrs or the user required us to
2640  * get all of the requested IAs and we didn't return an error
2641  */
2642  if ((have_addrs != ISC_TRUE) ||
2643  ((require_all_ias != 0) &&
2644  ((got_na < wanted_ia_na) ||
2645  (got_ta < wanted_ia_ta) ||
2646  (got_pd < wanted_ia_pd))))
2647  rval = ISC_R_ADDRNOTAVAIL;
2648 
2649  return rval;
2650 }
2651 
2652 /* status code <-> action matrix for the client in INIT state
2653  * (rapid/commit). Returns always false as no action is defined.
2654  */
2655 static isc_boolean_t
2656 dhc6_init_action(struct client_state *client, isc_result_t *rvalp,
2657  unsigned code)
2658 {
2659  if (rvalp == NULL)
2660  log_fatal("Impossible condition at %s:%d.", MDL);
2661 
2662  if (client == NULL) {
2663  *rvalp = DHCP_R_INVALIDARG;
2664  return ISC_FALSE;
2665  }
2666 
2667  if (*rvalp == ISC_R_SUCCESS)
2668  return ISC_FALSE;
2669 
2670  /* No possible action in any case... */
2671  return ISC_FALSE;
2672 }
2673 
2674 /* status code <-> action matrix for the client in SELECT state
2675  * (request/reply). Returns true if action was taken (and the
2676  * packet should be ignored), or false if no action was taken.
2677  */
2678 static isc_boolean_t
2679 dhc6_select_action(struct client_state *client, isc_result_t *rvalp,
2680  unsigned code)
2681 {
2682  struct dhc6_lease *lease;
2683  isc_result_t rval;
2684 
2685  if (rvalp == NULL)
2686  log_fatal("Impossible condition at %s:%d.", MDL);
2687 
2688  if (client == NULL) {
2689  *rvalp = DHCP_R_INVALIDARG;
2690  return ISC_FALSE;
2691  }
2692  rval = *rvalp;
2693 
2694  if (rval == ISC_R_SUCCESS)
2695  return ISC_FALSE;
2696 
2697  switch (code) {
2698  /* We may have an earlier failure status code (so no
2699  * success rval), and a success code now. This
2700  * doesn't upgrade the rval to success, but it does
2701  * mean we take no action here.
2702  */
2703  case STATUS_Success:
2704  /* Gimpy server, or possibly an attacker. */
2705  case STATUS_NoBinding:
2706  case STATUS_UseMulticast:
2707  /* Take no action. */
2708  return ISC_FALSE;
2709 
2710  /* If the server can't deal with us, either try the
2711  * next advertised server, or continue retrying if there
2712  * weren't any.
2713  */
2714  default:
2715  case STATUS_UnspecFail:
2716  if (client->advertised_leases != NULL) {
2718  client->selected_lease = NULL;
2719 
2720  start_selecting6(client);
2721 
2722  break;
2723  } else /* Take no action - continue to retry. */
2724  return ISC_FALSE;
2725 
2726  /* If the server has no addresses, try other servers if
2727  * we got some, otherwise go to INIT to hope for more
2728  * servers.
2729  */
2730  case STATUS_NoAddrsAvail:
2731  case STATUS_NoPrefixAvail:
2732  if (client->state == S_REBOOTING)
2733  return ISC_FALSE;
2734 
2735  if (client->selected_lease == NULL)
2736  log_fatal("Impossible case at %s:%d.", MDL);
2737 
2739  client->selected_lease = NULL;
2740 
2741  if (client->advertised_leases != NULL)
2742  start_selecting6(client);
2743  else
2744  start_init6(client);
2745 
2746  break;
2747 
2748  /* If we got a NotOnLink from a Confirm, then we're not
2749  * on link. Kill the old-active binding and start over.
2750  *
2751  * If we got a NotOnLink from our Request, something weird
2752  * happened. Start over from scratch anyway.
2753  */
2754  case STATUS_NotOnLink:
2755  if (client->state == S_REBOOTING) {
2756  if (client->active_lease == NULL)
2757  log_fatal("Impossible case at %s:%d.", MDL);
2758 
2759  dhc6_lease_destroy(&client->active_lease, MDL);
2760  } else {
2761  if (client->selected_lease == NULL)
2762  log_fatal("Impossible case at %s:%d.", MDL);
2763 
2765  client->selected_lease = NULL;
2766 
2767  while (client->advertised_leases != NULL) {
2768  lease = client->advertised_leases;
2769  client->advertised_leases = lease->next;
2770 
2771  dhc6_lease_destroy(&lease, MDL);
2772  }
2773  }
2774 
2775  start_init6(client);
2776  break;
2777  }
2778 
2779  return ISC_TRUE;
2780 }
2781 
2782 static void
2783 dhc6_withdraw_lease(struct client_state *client)
2784 {
2785  struct dhc6_ia *ia;
2786  struct dhc6_addr *addr;
2787 
2788  if ((client == NULL) || (client->active_lease == NULL))
2789  return;
2790 
2791  for (ia = client->active_lease->bindings ; ia != NULL ;
2792  ia = ia->next) {
2793  for (addr = ia->addrs ; addr != NULL ; addr = addr->next) {
2794  addr->max_life = addr->preferred_life = 0;
2795  }
2796  }
2797 
2798  /* Perform expiry. */
2799  do_expire(client);
2800 }
2801 
2802 /* status code <-> action matrix for the client in BOUND state
2803  * (request/reply). Returns true if action was taken (and the
2804  * packet should be ignored), or false if no action was taken.
2805  */
2806 static isc_boolean_t
2807 dhc6_reply_action(struct client_state *client, isc_result_t *rvalp,
2808  unsigned code)
2809 {
2810  isc_result_t rval;
2811 
2812  if (rvalp == NULL)
2813  log_fatal("Impossible condition at %s:%d.", MDL);
2814 
2815  if (client == NULL) {
2816  *rvalp = DHCP_R_INVALIDARG;
2817  return ISC_FALSE;
2818  }
2819  rval = *rvalp;
2820 
2821  if (rval == ISC_R_SUCCESS)
2822  return ISC_FALSE;
2823 
2824  switch (code) {
2825  /* It's possible an earlier status code set rval to a failure
2826  * code, and we've encountered a later success.
2827  */
2828  case STATUS_Success:
2829  /* In "refreshes" (where we get replies), we probably
2830  * still have a valid lease. So "take no action" and
2831  * the upper levels will keep retrying until the lease
2832  * expires (or we rebind).
2833  */
2834  case STATUS_UnspecFail:
2835  /* For unknown codes...it's a soft (retryable) error. */
2836  default:
2837  return ISC_FALSE;
2838 
2839  /* The server is telling us to use a multicast address, so
2840  * we have to delete the unicast option from the active
2841  * lease, then allow retransmission to occur normally.
2842  * (XXX: It might be preferable in this case to retransmit
2843  * sooner than the current interval, but for now we don't.)
2844  */
2845  case STATUS_UseMulticast:
2846  if (client->active_lease != NULL)
2848  client->active_lease->options,
2849  D6O_UNICAST);
2850  return ISC_FALSE;
2851 
2852  /* "When the client receives a NotOnLink status from the
2853  * server in response to a Request, the client can either
2854  * re-issue the Request without specifying any addresses
2855  * or restart the DHCP server discovery process."
2856  *
2857  * This is strange. If competing server evaluation is
2858  * useful (and therefore in the protocol), then why would
2859  * a client's first reaction be to request from the same
2860  * server on a different link? Surely you'd want to
2861  * re-evaluate your server selection.
2862  *
2863  * Well, I guess that's the answer.
2864  */
2865  case STATUS_NotOnLink:
2866  /* In this case, we need to rescind all current active
2867  * bindings (just 'expire' them all normally, if early).
2868  * They're no use to us on the wrong link. Then head back
2869  * to init, redo server selection and get new addresses.
2870  */
2871  dhc6_withdraw_lease(client);
2872  break;
2873 
2874  /* "If the status code is NoAddrsAvail, the client has
2875  * received no usable addresses in the IA and may choose
2876  * to try obtaining addresses for the IA from another
2877  * server."
2878  */
2879  case STATUS_NoAddrsAvail:
2880  case STATUS_NoPrefixAvail:
2881  /* Head back to init, keeping any active bindings (!). */
2882  start_init6(client);
2883  break;
2884 
2885  /* - sends a Request message if the IA contained a Status
2886  * Code option with the NoBinding status (and does not
2887  * send any additional Renew/Rebind messages)
2888  */
2889  case STATUS_NoBinding:
2890  if (client->advertised_leases != NULL)
2891  log_fatal("Impossible condition at %s:%d.", MDL);
2892 
2893  client->advertised_leases =
2894  dhc6_dup_lease(client->active_lease, MDL);
2895  start_selecting6(client);
2896  break;
2897  }
2898 
2899  return ISC_TRUE;
2900 }
2901 
2902 /* status code <-> action matrix for the client in STOPPED state
2903  * (release/decline). Returns true if action was taken (and the
2904  * packet should be ignored), or false if no action was taken.
2905  * NoBinding is translated into Success.
2906  */
2907 static isc_boolean_t
2908 dhc6_stop_action(struct client_state *client, isc_result_t *rvalp,
2909  unsigned code)
2910 {
2911  isc_result_t rval;
2912 
2913  if (rvalp == NULL)
2914  log_fatal("Impossible condition at %s:%d.", MDL);
2915 
2916  if (client == NULL) {
2917  *rvalp = DHCP_R_INVALIDARG;
2918  return ISC_FALSE;
2919  }
2920  rval = *rvalp;
2921 
2922  if (rval == ISC_R_SUCCESS)
2923  return ISC_FALSE;
2924 
2925  switch (code) {
2926  /* It's possible an earlier status code set rval to a failure
2927  * code, and we've encountered a later success.
2928  */
2929  case STATUS_Success:
2930  /* For unknown codes...it's a soft (retryable) error. */
2931  case STATUS_UnspecFail:
2932  default:
2933  return ISC_FALSE;
2934 
2935  /* NoBinding is not an error */
2936  case STATUS_NoBinding:
2937  if (rval == ISC_R_FAILURE)
2938  *rvalp = ISC_R_SUCCESS;
2939  return ISC_FALSE;
2940 
2941  /* Should not happen */
2942  case STATUS_NoAddrsAvail:
2943  case STATUS_NoPrefixAvail:
2944  break;
2945 
2946  /* Give up on it */
2947  case STATUS_NotOnLink:
2948  break;
2949 
2950  /* The server is telling us to use a multicast address, so
2951  * we have to delete the unicast option from the active
2952  * lease, then allow retransmission to occur normally.
2953  * (XXX: It might be preferable in this case to retransmit
2954  * sooner than the current interval, but for now we don't.)
2955  */
2956  case STATUS_UseMulticast:
2957  if (client->active_lease != NULL)
2959  client->active_lease->options,
2960  D6O_UNICAST);
2961  return ISC_FALSE;
2962  }
2963 
2964  return ISC_TRUE;
2965 }
2966 
2967 /* Look at a new and old lease, and make sure the new information is not
2968  * losing us any state.
2969  */
2970 static isc_result_t
2971 dhc6_check_reply(struct client_state *client, struct dhc6_lease *new)
2972 {
2973  isc_boolean_t (*action)(struct client_state *,
2974  isc_result_t *, unsigned);
2975  struct dhc6_ia *ia;
2976  isc_result_t rval = ISC_R_SUCCESS;
2977  unsigned code;
2978  const char *scope;
2979  int nscore, sscore;
2980  int have_addrs = ISC_FALSE;
2981  int got_na = 0, got_ta = 0, got_pd = 0;
2982 
2983  if ((client == NULL) || (new == NULL))
2984  return DHCP_R_INVALIDARG;
2985 
2986  switch (client->state) {
2987  case S_INIT:
2988  action = dhc6_init_action;
2989  break;
2990 
2991  case S_SELECTING:
2992  case S_REBOOTING:
2993  action = dhc6_select_action;
2994  break;
2995 
2996  case S_RENEWING:
2997  case S_REBINDING:
2998  action = dhc6_reply_action;
2999  break;
3000 
3001  case S_STOPPED:
3002  case S_DECLINED:
3003  action = dhc6_stop_action;
3004  break;
3005 
3006  default:
3007  log_fatal("Impossible condition at %s:%d.", MDL);
3008  return ISC_R_CANCELED;
3009  }
3010 
3011  /* If there is a code to extract, and if there is some
3012  * action to take based on that code, then take the action
3013  * and do not continue.
3014  */
3015  rval = dhc6_check_status(rval, new->options, "message", &code);
3016  if (action(client, &rval, code))
3017  return ISC_R_CANCELED;
3018 
3019  for (ia = new->bindings ; ia != NULL ; ia = ia->next) {
3020  switch (ia->ia_type) {
3021  case D6O_IA_NA:
3022  scope = "IA_NA";
3023  got_na++;
3024  break;
3025  case D6O_IA_TA:
3026  scope = "IA_TA";
3027  got_ta++;
3028  break;
3029  case D6O_IA_PD:
3030  scope = "IA_PD";
3031  got_pd++;
3032  break;
3033  default:
3034  log_error("dhc6_check_reply: no type.");
3035  return DHCP_R_INVALIDARG;
3036  }
3037  rval = dhc6_check_status(rval, ia->options, scope, &code);
3038 
3039  if (action(client, &rval, code))
3040  return ISC_R_CANCELED;
3041 
3042  if (ia->addrs != NULL) {
3043  have_addrs = ISC_TRUE;
3044  }
3045  }
3046 
3047  /* A Confirm->Reply is unsuitable for comparison to the old lease. */
3048  if (client->state == S_REBOOTING)
3049  return rval;
3050 
3051  /* We expect the lease to have at least one address and if
3052  * required all of the requested IAs if not flag it as
3053  * NoAddrs and call the action routine to try again.
3054  *
3055  * Currently we don't completely handle TAs in all cases
3056  * so we don't check them for requires. I've left the
3057  * check in and commented it as I eventually do want
3058  * us to check for TAs as well. SAR
3059  */
3060  if ((have_addrs != ISC_TRUE) ||
3061  ((require_all_ias != 0) &&
3062  ((got_na < wanted_ia_na) ||
3063  /*(got_ta < wanted_ia_ta) ||*/
3064  (got_pd < wanted_ia_pd)))) {
3065  rval = ISC_R_FAILURE;
3066  if (action(client, &rval, STATUS_NoAddrsAvail) == ISC_TRUE) {
3067  return ISC_R_CANCELED;
3068  }
3069  }
3070 
3071  /* No old lease in rapid-commit. */
3072  if (client->state == S_INIT)
3073  return rval;
3074 
3075  switch (client->state) {
3076  case S_SELECTING:
3077  /* Compare the new lease with the selected lease to make
3078  * sure there is no risky business.
3079  */
3080  nscore = dhc6_score_lease(client, new);
3081  sscore = dhc6_score_lease(client, client->selected_lease);
3082  if ((client->advertised_leases != NULL) &&
3083  (nscore < (sscore / 2))) {
3084  /* XXX: An attacker might reply this way to make
3085  * XXX: sure we latch onto their configuration.
3086  * XXX: We might want to ignore the packet and
3087  * XXX: schedule re-selection at the next timeout?
3088  */
3089  log_error("PRC: BAIT AND SWITCH detected. Score of "
3090  "supplied lease (%d) is substantially "
3091  "smaller than the advertised score (%d). "
3092  "Trying other servers.",
3093  nscore, sscore);
3094 
3096  client->selected_lease = NULL;
3097 
3098  start_selecting6(client);
3099 
3100  return ISC_R_CANCELED;
3101  }
3102  break;
3103 
3104  case S_RENEWING:
3105  case S_REBINDING:
3106  /* This leaves one RFC3315 status check unimplemented:
3107  *
3108  * - sends a Renew/Rebind if the IA is not in the Reply
3109  * message
3110  *
3111  * We rely on the scheduling system to note that the IA has
3112  * not left Renewal/Rebinding/whatever since it still carries
3113  * old times from the last successful binding. So this is
3114  * implemented actually, just not explicitly.
3115  */
3116  break;
3117 
3118  case S_STOPPED:
3119  case S_DECLINED:
3120  /* Nothing critical to do at this stage. */
3121  break;
3122 
3123  default:
3124  log_fatal("REALLY impossible condition at %s:%d.", MDL);
3125  return ISC_R_CANCELED;
3126  }
3127 
3128  return rval;
3129 }
3130 
3131 /* While in init state, we only collect advertisements. If there happens
3132  * to be an advertisement with a preference option of 255, that's an
3133  * automatic exit. Otherwise, we collect advertisements until our timeout
3134  * expires (client->RT).
3135  */
3136 void
3137 init_handler(struct packet *packet, struct client_state *client)
3138 {
3139  struct dhc6_lease *lease;
3140 
3141  /* In INIT state, we send solicits, we only expect to get
3142  * advertises (rapid commit has its own handler).
3143  */
3144  if (packet->dhcpv6_msg_type != DHCPV6_ADVERTISE)
3145  return;
3146 
3147  /* RFC3315 section 15.3 validation (same as 15.10 since we
3148  * always include a client id).
3149  */
3150  if (!valid_reply(packet, client)) {
3151  log_error("Invalid Advertise - rejecting.");
3152  return;
3153  }
3154 
3155  lease = dhc6_leaseify(packet, client);
3156 
3157  /* Out of memory or corrupt packet condition...hopefully a temporary
3158  * problem. Returning now makes us try to retransmit later.
3159  */
3160  if (lease == NULL)
3161  return;
3162 
3163  if (dhc6_check_advertise(lease) != ISC_R_SUCCESS) {
3164  log_debug("PRC: Lease failed to satisfy.");
3165  dhc6_lease_destroy(&lease, MDL);
3166  return;
3167  }
3168 
3169  insert_lease(&client->advertised_leases, lease);
3170 
3171  /* According to RFC3315 section 17.1.2, the client MUST wait for
3172  * the first RT before selecting a lease. But on the 400th RT,
3173  * we dont' want to wait the full timeout if we finally get an
3174  * advertise. We could probably wait a second, but ohwell,
3175  * RFC3315 doesn't say so.
3176  *
3177  * If the lease is highest possible preference, 255, RFC3315 claims
3178  * we should continue immediately even on the first RT. We probably
3179  * should not if the advertise contains less than one IA and address.
3180  */
3181  if ((client->txcount > 1) ||
3182  ((lease->pref == 255) &&
3183  (dhc6_score_lease(client, lease) > SCORE_MIN))) {
3184  log_debug("RCV: Advertisement immediately selected.");
3185  cancel_timeout(do_init6, client);
3186  start_selecting6(client);
3187  } else
3188  log_debug("RCV: Advertisement recorded.");
3189 }
3190 
3191 /* info_request_handler() accepts a Reply to an Info-request.
3192  */
3193 void
3194 info_request_handler(struct packet *packet, struct client_state *client)
3195 {
3196  isc_result_t check_status;
3197  unsigned code;
3198 
3199  if (packet->dhcpv6_msg_type != DHCPV6_REPLY)
3200  return;
3201 
3202  /* RFC3315 section 15.10 validation (same as 15.3 since we
3203  * always include a client id).
3204  */
3205  if (!valid_reply(packet, client)) {
3206  log_error("Invalid Reply - rejecting.");
3207  return;
3208  }
3209 
3210  check_status = dhc6_check_status(ISC_R_SUCCESS, packet->options,
3211  "message", &code);
3212 
3213  if (check_status != ISC_R_SUCCESS) {
3214  /* If no action was taken, but there is an error, then
3215  * we wait for a retransmission.
3216  */
3217  if (check_status != ISC_R_CANCELED)
3218  return;
3219  }
3220 
3221  /* We're done retransmitting at this point. */
3222  cancel_timeout(do_info_request6, client);
3223 
3224  /* Action was taken, so now that we've torn down our scheduled
3225  * retransmissions, return.
3226  */
3227  if (check_status == ISC_R_CANCELED)
3228  return;
3229 
3230  /* Cleanup if a previous attempt to go bound failed. */
3231  if (client->old_lease != NULL) {
3232  dhc6_lease_destroy(&client->old_lease, MDL);
3233  client->old_lease = NULL;
3234  }
3235 
3236  /* Cache options in the active_lease. */
3237  if (client->active_lease != NULL)
3238  client->old_lease = client->active_lease;
3239  client->active_lease = dmalloc(sizeof(struct dhc6_lease), MDL);
3240  if (client->active_lease == NULL)
3241  log_fatal("Out of memory for v6 lease structure.");
3243  packet->options, MDL);
3244 
3245  execute_statements_in_scope(NULL, (struct packet *)packet, NULL, client,
3246  client->active_lease->options,
3247  client->active_lease->options,
3248  &global_scope, client->config->on_receipt,
3249  NULL, NULL);
3250 
3251  start_informed(client);
3252 }
3253 
3254 /* Specific version of init_handler() for rapid-commit.
3255  */
3256 void
3257 rapid_commit_handler(struct packet *packet, struct client_state *client)
3258 {
3259  struct dhc6_lease *lease;
3260  isc_result_t check_status;
3261 
3262  /* On ADVERTISE just fall back to the init_handler().
3263  */
3264  if (packet->dhcpv6_msg_type == DHCPV6_ADVERTISE) {
3265  init_handler(packet, client);
3266  return;
3267  } else if (packet->dhcpv6_msg_type != DHCPV6_REPLY)
3268  return;
3269 
3270  /* RFC3315 section 15.10 validation (same as 15.3 since we
3271  * always include a client id).
3272  */
3273  if (!valid_reply(packet, client)) {
3274  log_error("Invalid Reply - rejecting.");
3275  return;
3276  }
3277 
3278  /* A rapid-commit option MUST be here. */
3279  if (lookup_option(&dhcpv6_universe, packet->options,
3280  D6O_RAPID_COMMIT) == 0) {
3281  log_error("Reply without Rapid-Commit - rejecting.");
3282  return;
3283  }
3284 
3285  lease = dhc6_leaseify(packet, client);
3286 
3287  /* Out of memory or corrupt packet condition...hopefully a temporary
3288  * problem. Returning now makes us try to retransmit later.
3289  */
3290  if (lease == NULL)
3291  return;
3292 
3293  check_status = dhc6_check_reply(client, lease);
3294  if (check_status != ISC_R_SUCCESS) {
3295  dhc6_lease_destroy(&lease, MDL);
3296  return;
3297  }
3298 
3299  /* Jump to the selecting state. */
3300  cancel_timeout(do_init6, client);
3301  client->state = S_SELECTING;
3302 
3303  /* Merge any bindings in the active lease (if there is one) into
3304  * the new active lease.
3305  */
3306  dhc6_merge_lease(client->active_lease, lease);
3307 
3308  /* Cleanup if a previous attempt to go bound failed. */
3309  if (client->old_lease != NULL) {
3310  dhc6_lease_destroy(&client->old_lease, MDL);
3311  client->old_lease = NULL;
3312  }
3313 
3314  /* Make this lease active and BIND to it. */
3315  if (client->active_lease != NULL)
3316  client->old_lease = client->active_lease;
3317  client->active_lease = lease;
3318 
3319  /* We're done with the ADVERTISEd leases, if any. */
3320  while(client->advertised_leases != NULL) {
3321  lease = client->advertised_leases;
3322  client->advertised_leases = lease->next;
3323 
3324  dhc6_lease_destroy(&lease, MDL);
3325  }
3326 
3327  start_bound(client);
3328 }
3329 
3330 /* Find the 'best' lease in the cache of advertised leases (usually). From
3331  * RFC3315 Section 17.1.3:
3332  *
3333  * Upon receipt of one or more valid Advertise messages, the client
3334  * selects one or more Advertise messages based upon the following
3335  * criteria.
3336  *
3337  * - Those Advertise messages with the highest server preference value
3338  * are preferred over all other Advertise messages.
3339  *
3340  * - Within a group of Advertise messages with the same server
3341  * preference value, a client MAY select those servers whose
3342  * Advertise messages advertise information of interest to the
3343  * client. For example, the client may choose a server that returned
3344  * an advertisement with configuration options of interest to the
3345  * client.
3346  *
3347  * - The client MAY choose a less-preferred server if that server has a
3348  * better set of advertised parameters, such as the available
3349  * addresses advertised in IAs.
3350  *
3351  * Note that the first and third contradict each other. The third should
3352  * probably be taken to mean that the client should prefer answers that
3353  * offer bindings, even if that violates the preference rule.
3354  *
3355  * The above also isn't deterministic where there are ties. So the final
3356  * tiebreaker we add, if all other values are equal, is to compare the
3357  * server identifiers and to select the numerically lower one.
3358  */
3359 static struct dhc6_lease *
3360 dhc6_best_lease(struct client_state *client, struct dhc6_lease **head)
3361 {
3362  struct dhc6_lease **rpos, *rval, **candp, *cand;
3363  int cscore, rscore;
3364 
3365  if (head == NULL || *head == NULL)
3366  return NULL;
3367 
3368  rpos = head;
3369  rval = *rpos;
3370  rscore = dhc6_score_lease(client, rval);
3371  candp = &rval->next;
3372  cand = *candp;
3373 
3374  log_debug("PRC: Considering best lease.");
3375  log_debug("PRC: X-- Initial candidate %s (s: %d, p: %u).",
3376  print_hex_1(rval->server_id.len,
3377  rval->server_id.data, 48),
3378  rscore, (unsigned)rval->pref);
3379 
3380  for (; cand != NULL ; candp = &cand->next, cand = *candp) {
3381  cscore = dhc6_score_lease(client, cand);
3382 
3383  log_debug("PRC: X-- Candidate %s (s: %d, p: %u).",
3384  print_hex_1(cand->server_id.len,
3385  cand->server_id.data, 48),
3386  cscore, (unsigned)cand->pref);
3387 
3388  /* Above you'll find quoted RFC3315 Section 17.1.3.
3389  *
3390  * The third clause tells us to give up on leases that
3391  * have no bindings even if their preference is better.
3392  * So where our 'selected' lease's score is less than
3393  * SCORE_MIN (1 ia + 1 addr), choose any candidate >= SCORE_MIN.
3394  *
3395  * The first clause tells us to make preference the primary
3396  * deciding factor. So if it's lower, reject, if it's
3397  * higher, select.
3398  *
3399  * The second clause tells us where the preference is
3400  * equal, we should use 'our judgement' of what we like
3401  * to see in an advertisement primarily.
3402  *
3403  * But there can still be a tie. To make this deterministic,
3404  * we compare the server identifiers and select the binary
3405  * lowest.
3406  *
3407  * Since server id's are unique in this list, there is
3408  * no further tie to break.
3409  */
3410  if ((rscore < SCORE_MIN) && (cscore >= SCORE_MIN)) {
3411  log_debug("PRC: | X-- Selected, has bindings.");
3412  } else if (cand->pref < rval->pref) {
3413  log_debug("PRC: | X-- Rejected, lower preference.");
3414  continue;
3415  } else if (cand->pref > rval->pref) {
3416  log_debug("PRC: | X-- Selected, higher preference.");
3417  } else if (cscore > rscore) {
3418  log_debug("PRC: | X-- Selected, equal preference, "
3419  "higher score.");
3420  } else if (cscore < rscore) {
3421  log_debug("PRC: | X-- Rejected, equal preference, "
3422  "lower score.");
3423  continue;
3424  } else if ((cand->server_id.len < rval->server_id.len) ||
3425  ((cand->server_id.len == rval->server_id.len) &&
3426  (memcmp(cand->server_id.data,
3427  rval->server_id.data,
3428  cand->server_id.len) < 0))) {
3429  log_debug("PRC: | X-- Selected, equal preference, "
3430  "equal score, binary lesser server ID.");
3431  } else {
3432  log_debug("PRC: | X-- Rejected, equal preference, "
3433  "equal score, binary greater server ID.");
3434  continue;
3435  }
3436 
3437  rpos = candp;
3438  rval = cand;
3439  rscore = cscore;
3440  }
3441 
3442  /* Remove the selected lease from the chain. */
3443  *rpos = rval->next;
3444 
3445  return rval;
3446 }
3447 
3448 /* Select a lease out of the advertised leases and setup state to try and
3449  * acquire that lease.
3450  */
3451 void
3452 start_selecting6(struct client_state *client)
3453 {
3454  struct dhc6_lease *lease;
3455 
3456  if (client->advertised_leases == NULL) {
3457  log_error("Can not enter DHCPv6 SELECTING state with no "
3458  "leases to select from!");
3459  return;
3460  }
3461 
3462  log_debug("PRC: Selecting best advertised lease.");
3463  client->state = S_SELECTING;
3464 
3465  lease = dhc6_best_lease(client, &client->advertised_leases);
3466 
3467  if (lease == NULL)
3468  log_fatal("Impossible error at %s:%d.", MDL);
3469 
3470  client->selected_lease = lease;
3471 
3472  /* Set timers per RFC3315 section 18.1.1. */
3473  client->IRT = REQ_TIMEOUT * 100;
3474  client->MRT = REQ_MAX_RT * 100;
3475  client->MRC = REQ_MAX_RC;
3476  client->MRD = 0;
3477 
3478  dhc6_retrans_init(client);
3479 
3480  client->v6_handler = reply_handler;
3481 
3482  /* ("re")transmit the first packet. */
3483  do_select6(client);
3484 }
3485 
3486 /* Transmit a Request to select a lease offered in Advertisements. In
3487  * the event of failure, either move on to the next-best advertised lease,
3488  * or head back to INIT state if there are none.
3489  */
3490 void
3491 do_select6(void *input)
3492 {
3493  struct client_state *client;
3494  struct dhc6_lease *lease;
3495  struct data_string ds;
3496  struct timeval tv;
3497  int send_ret, added;
3498 
3499  client = input;
3500 
3501  /* 'lease' is fewer characters to type. */
3502  lease = client->selected_lease;
3503  if (lease == NULL || lease->bindings == NULL) {
3504  log_error("Illegal to attempt selection without selecting "
3505  "a lease.");
3506  return;
3507  }
3508 
3509  switch(check_timing6(client, DHCPV6_REQUEST, "Request", lease, &ds)) {
3510  case CHK_TIM_MRC_EXCEEDED:
3511  case CHK_TIM_MRD_EXCEEDED:
3512  log_debug("PRC: Lease %s failed.",
3513  print_hex_1(lease->server_id.len,
3514  lease->server_id.data, 56));
3515 
3516  /* Get rid of the lease that timed/counted out. */
3517  dhc6_lease_destroy(&lease, MDL);
3518  client->selected_lease = NULL;
3519 
3520  /* If there are more leases great. If not, get more. */
3521  if (client->advertised_leases != NULL)
3522  start_selecting6(client);
3523  else
3524  start_init6(client);
3525  return;
3526  case CHK_TIM_ALLOC_FAILURE:
3527  return;
3528  case CHK_TIM_SUCCESS:
3529  break;
3530  }
3531 
3532  /* Now make a packet that looks suspiciously like the one we
3533  * got from the server. But different.
3534  *
3535  * XXX: I guess IAID is supposed to be something the client
3536  * indicates and uses as a key to its internal state. It is
3537  * kind of odd to ask the server for IA's whose IAID the client
3538  * did not manufacture. We first need a formal dhclient.conf
3539  * construct for the iaid, then we can delve into this matter
3540  * more properly. In the time being, this will work.
3541  */
3542 
3543  /* Fetch any configured 'sent' options (includes DUID) in wire format.
3544  */
3545  dhcpv6_universe.encapsulate(&ds, NULL, NULL, client,
3546  NULL, client->sent_options, &global_scope,
3547  &dhcpv6_universe);
3548 
3549  /* Now append any IA's, and within them any IAADDR/IAPREFIXs.
3550  * For each type of IA (na, ta, pd) we start with the ones for
3551  * which we already have addresses (dhc6_add_ia_xx) and then
3552  * if we still want more we add aditional IAs (dhc6_bare_ia_xx)
3553  */
3554  if (wanted_ia_na &&
3555  ((dhc6_add_ia_na(client, &ds, lease, DHCPV6_REQUEST,
3556  wanted_ia_na, &added) != ISC_R_SUCCESS) ||
3557  (dhc6_bare_ia_xx(client, &ds, wanted_ia_na - added,
3558  D6O_IA_NA) != ISC_R_SUCCESS))) {
3559  data_string_forget(&ds, MDL);
3560  return;
3561  }
3562  if (wanted_ia_ta &&
3563  ((dhc6_add_ia_ta(client, &ds, lease, DHCPV6_REQUEST,
3564  wanted_ia_ta, &added) != ISC_R_SUCCESS) ||
3565  (dhc6_bare_ia_xx(client, &ds, wanted_ia_ta - added,
3566  D6O_IA_TA) != ISC_R_SUCCESS))) {
3567  data_string_forget(&ds, MDL);
3568  return;
3569  }
3570  if (wanted_ia_pd &&
3571  ((dhc6_add_ia_pd(client, &ds, lease, DHCPV6_REQUEST,
3572  wanted_ia_pd, &added) != ISC_R_SUCCESS) ||
3573  (dhc6_bare_ia_xx(client, &ds, wanted_ia_pd - added,
3574  D6O_IA_PD) != ISC_R_SUCCESS))) {
3575  data_string_forget(&ds, MDL);
3576  return;
3577  }
3578 
3579  log_info("XMT: Request on %s, interval %ld0ms.",
3580  client->name ? client->name : client->interface->name,
3581  (long int)client->RT);
3582 
3583  send_ret = send_packet6(client->interface,
3584  ds.data, ds.len, &DHCPv6DestAddr);
3585  if (send_ret != ds.len) {
3586  log_error("dhc6: send_packet6() sent %d of %d bytes",
3587  send_ret, ds.len);
3588  }
3589 
3590  data_string_forget(&ds, MDL);
3591 
3592  /* Wait RT */
3593  tv.tv_sec = cur_tv.tv_sec + client->RT / 100;
3594  tv.tv_usec = cur_tv.tv_usec + (client->RT % 100) * 10000;
3595  if (tv.tv_usec >= 1000000) {
3596  tv.tv_sec += 1;
3597  tv.tv_usec -= 1000000;
3598  }
3599  add_timeout(&tv, do_select6, client, NULL, NULL);
3600 
3601  dhc6_retrans_advance(client);
3602 }
3603 
3613 static int
3614 dhc6_count_ia(struct dhc6_lease *lease, u_int16_t ia_type)
3615 {
3616  struct dhc6_ia *ia;
3617  int i = 0;
3618 
3619  for (ia = lease->bindings; ia != NULL; ia = ia->next) {
3620  if (ia->ia_type == ia_type)
3621  /* bump the counter for the correct types */
3622  i++;
3623  }
3624 
3625  return (i);
3626 }
3627 
3653 static isc_result_t
3654 dhc6_add_ia_na(struct client_state *client, struct data_string *packet,
3655  struct dhc6_lease *lease, u_int8_t message,
3656  int wanted, int *added)
3657 {
3658  struct data_string iads;
3659  struct data_string addrds;
3660  struct dhc6_addr *addr;
3661  struct dhc6_ia *ia;
3662  isc_result_t rval = ISC_R_SUCCESS;
3663  TIME t1, t2;
3664  int i;
3665 
3666  *added = 0;
3667  memset(&iads, 0, sizeof(iads));
3668  memset(&addrds, 0, sizeof(addrds));
3669  for (ia = lease->bindings, i = 0;
3670  ia != NULL && rval == ISC_R_SUCCESS && (wanted == 0 || i < wanted);
3671  ia = ia->next) {
3672  if (ia->ia_type != D6O_IA_NA)
3673  continue;
3674 
3675  /* Now that we know this is an NA bump the counter */
3676  i++;
3677 
3678  if (!buffer_allocate(&iads.buffer, 12, MDL)) {
3679  log_error("Unable to allocate memory for IA_NA.");
3680  rval = ISC_R_NOMEMORY;
3681  break;
3682  }
3683 
3684  /* Copy the IAID into the packet buffer. */
3685  memcpy(iads.buffer->data, ia->iaid, 4);
3686  iads.data = iads.buffer->data;
3687  iads.len = 12;
3688 
3689  switch (message) {
3690  case DHCPV6_REQUEST:
3691  case DHCPV6_RENEW:
3692  case DHCPV6_REBIND:
3693 
3694  t1 = client->config->requested_lease / 2;
3695  t2 = t1 + (t1 / 2);
3696 #if MAX_TIME > 0xffffffff
3697  if (t1 > 0xffffffff)
3698  t1 = 0xffffffff;
3699  if (t2 > 0xffffffff)
3700  t2 = 0xffffffff;
3701 #endif
3702  putULong(iads.buffer->data + 4, t1);
3703  putULong(iads.buffer->data + 8, t2);
3704 
3705  log_debug("XMT: X-- IA_NA %s",
3706  print_hex_1(4, iads.data, 59));
3707  log_debug("XMT: | X-- Requested renew +%u",
3708  (unsigned) t1);
3709  log_debug("XMT: | X-- Requested rebind +%u",
3710  (unsigned) t2);
3711  break;
3712 
3713  case DHCPV6_CONFIRM:
3714  case DHCPV6_RELEASE:
3715  case DHCPV6_DECLINE:
3716  /* Set t1 and t2 to zero; server will ignore them */
3717  memset(iads.buffer->data + 4, 0, 8);
3718  log_debug("XMT: X-- IA_NA %s",
3719  print_hex_1(4, iads.buffer->data, 55));
3720 
3721  break;
3722 
3723  default:
3724  log_fatal("Impossible condition at %s:%d.", MDL);
3725  }
3726 
3727  for (addr = ia->addrs ; addr != NULL ; addr = addr->next) {
3728  /*
3729  * Do not confirm expired addresses, do not request
3730  * expired addresses (but we keep them around for
3731  * solicit).
3732  */
3733  if (addr->flags & DHC6_ADDR_EXPIRED)
3734  continue;
3735 
3736  if (addr->address.len != 16) {
3737  log_error("Illegal IPv6 address length (%d), "
3738  "ignoring. (%s:%d)",
3739  addr->address.len, MDL);
3740  continue;
3741  }
3742 
3743  if (!buffer_allocate(&addrds.buffer, 24, MDL)) {
3744  log_error("Unable to allocate memory for "
3745  "IAADDR.");
3746  rval = ISC_R_NOMEMORY;
3747  break;
3748  }
3749 
3750  addrds.data = addrds.buffer->data;
3751  addrds.len = 24;
3752 
3753  /* Copy the address into the packet buffer. */
3754  memcpy(addrds.buffer->data, addr->address.iabuf, 16);
3755 
3756  /* Copy in additional information as appropriate */
3757  switch (message) {
3758  case DHCPV6_REQUEST:
3759  case DHCPV6_RENEW:
3760  case DHCPV6_REBIND:
3761  t1 = client->config->requested_lease;
3762  t2 = t1 + 300;
3763  putULong(addrds.buffer->data + 16, t1);
3764  putULong(addrds.buffer->data + 20, t2);
3765 
3766  log_debug("XMT: | | X-- IAADDR %s",
3767  piaddr(addr->address));
3768  log_debug("XMT: | | | X-- Preferred "
3769  "lifetime +%u", (unsigned)t1);
3770  log_debug("XMT: | | | X-- Max lifetime +%u",
3771  (unsigned)t2);
3772 
3773  break;
3774 
3775  case DHCPV6_CONFIRM:
3776  /*
3777  * Set preferred and max life to zero,
3778  * per 17.1.3.
3779  */
3780  memset(addrds.buffer->data + 16, 0, 8);
3781  log_debug("XMT: | X-- Confirm Address %s",
3782  piaddr(addr->address));
3783  break;
3784 
3785  case DHCPV6_RELEASE:
3786  /* Preferred and max life are irrelevant */
3787  memset(addrds.buffer->data + 16, 0, 8);
3788  log_debug("XMT: | X-- Release Address %s",
3789  piaddr(addr->address));
3790  break;
3791 
3792  case DHCPV6_DECLINE:
3793  /* Preferred and max life are irrelevant */
3794  memset(addrds.buffer->data + 16, 0, 8);
3795  log_debug("XMT: | X-- Decline Address %s",
3796  piaddr(addr->address));
3797  break;
3798 
3799  default:
3800  log_fatal("Impossible condition at %s:%d.",
3801  MDL);
3802  }
3803 
3804  append_option(&iads, &dhcpv6_universe, iaaddr_option,
3805  &addrds);
3806  data_string_forget(&addrds, MDL);
3807  }
3808 
3809  /*
3810  * It doesn't make sense to make a request without an
3811  * address.
3812  */
3813  if (ia->addrs == NULL) {
3814  log_debug("!!!: V IA_NA has no IAADDRs - removed.");
3815  rval = ISC_R_FAILURE;
3816  } else if (rval == ISC_R_SUCCESS) {
3817  log_debug("XMT: V IA_NA appended.");
3818  append_option(packet, &dhcpv6_universe, ia_na_option,
3819  &iads);
3820  }
3821 
3822  data_string_forget(&iads, MDL);
3823  }
3824 
3825  if (rval == ISC_R_SUCCESS)
3826  *added = i;
3827 
3828  return (rval);
3829 }
3830 
3856 static isc_result_t
3857 dhc6_add_ia_ta(struct client_state *client, struct data_string *packet,
3858  struct dhc6_lease *lease, u_int8_t message,
3859  int wanted, int *added)
3860 {
3861  struct data_string iads;
3862  struct data_string addrds;
3863  struct dhc6_addr *addr;
3864  struct dhc6_ia *ia;
3865  isc_result_t rval = ISC_R_SUCCESS;
3866  TIME t1, t2;
3867  int i;
3868 
3869  *added = 0;
3870  memset(&iads, 0, sizeof(iads));
3871  memset(&addrds, 0, sizeof(addrds));
3872  for (ia = lease->bindings, i = 0;
3873  ia != NULL && rval == ISC_R_SUCCESS && (wanted == 0 || i < wanted);
3874  ia = ia->next) {
3875  if (ia->ia_type != D6O_IA_TA)
3876  continue;
3877 
3878  /* Now that we know this is an TA bump the counter */
3879  i++;
3880 
3881  if (!buffer_allocate(&iads.buffer, 4, MDL)) {
3882  log_error("Unable to allocate memory for IA_TA.");
3883  rval = ISC_R_NOMEMORY;
3884  break;
3885  }
3886 
3887  /* Copy the IAID into the packet buffer. */
3888  memcpy(iads.buffer->data, ia->iaid, 4);
3889  iads.data = iads.buffer->data;
3890  iads.len = 4;
3891 
3892  log_debug("XMT: X-- IA_TA %s",
3893  print_hex_1(4, iads.buffer->data, 55));
3894 
3895  for (addr = ia->addrs ; addr != NULL ; addr = addr->next) {
3896  /*
3897  * Do not confirm expired addresses, do not request
3898  * expired addresses (but we keep them around for
3899  * solicit).
3900  */
3901  if (addr->flags & DHC6_ADDR_EXPIRED)
3902  continue;
3903 
3904  if (addr->address.len != 16) {
3905  log_error("Illegal IPv6 address length (%d), "
3906  "ignoring. (%s:%d)",
3907  addr->address.len, MDL);
3908  continue;
3909  }
3910 
3911  if (!buffer_allocate(&addrds.buffer, 24, MDL)) {
3912  log_error("Unable to allocate memory for "
3913  "IAADDR.");
3914  rval = ISC_R_NOMEMORY;
3915  break;
3916  }
3917 
3918  addrds.data = addrds.buffer->data;
3919  addrds.len = 24;
3920 
3921  /* Copy the address into the packet buffer. */
3922  memcpy(addrds.buffer->data, addr->address.iabuf, 16);
3923 
3924  /* Copy in additional information as appropriate */
3925  switch (message) {
3926  case DHCPV6_REQUEST:
3927  case DHCPV6_RENEW:
3928  case DHCPV6_REBIND:
3929  t1 = client->config->requested_lease;
3930  t2 = t1 + 300;
3931  putULong(addrds.buffer->data + 16, t1);
3932  putULong(addrds.buffer->data + 20, t2);
3933 
3934  log_debug("XMT: | | X-- IAADDR %s",
3935  piaddr(addr->address));
3936  log_debug("XMT: | | | X-- Preferred "
3937  "lifetime +%u", (unsigned)t1);
3938  log_debug("XMT: | | | X-- Max lifetime +%u",
3939  (unsigned)t2);
3940 
3941  break;
3942 
3943  case DHCPV6_CONFIRM:
3944  /*
3945  * Set preferred and max life to zero,
3946  * per 17.1.3.
3947  */
3948  memset(addrds.buffer->data + 16, 0, 8);
3949  log_debug("XMT: | X-- Confirm Address %s",
3950  piaddr(addr->address));
3951  break;
3952 
3953  case DHCPV6_RELEASE:
3954  /* Preferred and max life are irrelevant */
3955  memset(addrds.buffer->data + 16, 0, 8);
3956  log_debug("XMT: | X-- Release Address %s",
3957  piaddr(addr->address));
3958  break;
3959 
3960  default:
3961  log_fatal("Impossible condition at %s:%d.",
3962  MDL);
3963  }
3964 
3965  append_option(&iads, &dhcpv6_universe, iaaddr_option,
3966  &addrds);
3967  data_string_forget(&addrds, MDL);
3968  }
3969 
3970  /*
3971  * It doesn't make sense to make a request without an
3972  * address.
3973  */
3974  if (ia->addrs == NULL) {
3975  log_debug("!!!: V IA_TA has no IAADDRs - removed.");
3976  rval = ISC_R_FAILURE;
3977  } else if (rval == ISC_R_SUCCESS) {
3978  log_debug("XMT: V IA_TA appended.");
3979  append_option(packet, &dhcpv6_universe, ia_ta_option,
3980  &iads);
3981  }
3982 
3983  data_string_forget(&iads, MDL);
3984  }
3985 
3986  if (rval == ISC_R_SUCCESS)
3987  *added = i;
3988 
3989  return (rval);
3990 }
3991 
4017 static isc_result_t
4018 dhc6_add_ia_pd(struct client_state *client, struct data_string *packet,
4019  struct dhc6_lease *lease, u_int8_t message,
4020  int wanted, int *added)
4021 {
4022  struct data_string iads;
4023  struct data_string prefds;
4024  struct dhc6_addr *pref;
4025  struct dhc6_ia *ia;
4026  isc_result_t rval = ISC_R_SUCCESS;
4027  TIME t1, t2;
4028  int i;
4029 
4030  *added = 0;
4031  memset(&iads, 0, sizeof(iads));
4032  memset(&prefds, 0, sizeof(prefds));
4033  for (ia = lease->bindings, i = 0;
4034  ia != NULL && rval == ISC_R_SUCCESS && (wanted == 0 || i < wanted);
4035  ia = ia->next) {
4036  if (ia->ia_type != D6O_IA_PD)
4037  continue;
4038 
4039  /* Now that we know this is an PD bump the counter */
4040  i++;
4041 
4042  if (!buffer_allocate(&iads.buffer, 12, MDL)) {
4043  log_error("Unable to allocate memory for IA_PD.");
4044  rval = ISC_R_NOMEMORY;
4045  break;
4046  }
4047 
4048  /* Copy the IAID into the packet buffer. */
4049  memcpy(iads.buffer->data, ia->iaid, 4);
4050  iads.data = iads.buffer->data;
4051  iads.len = 12;
4052 
4053  switch (message) {
4054  case DHCPV6_REQUEST:
4055  case DHCPV6_RENEW:
4056  case DHCPV6_REBIND:
4057 
4058  t1 = client->config->requested_lease / 2;
4059  t2 = t1 + (t1 / 2);
4060 #if MAX_TIME > 0xffffffff
4061  if (t1 > 0xffffffff)
4062  t1 = 0xffffffff;
4063  if (t2 > 0xffffffff)
4064  t2 = 0xffffffff;
4065 #endif
4066  putULong(iads.buffer->data + 4, t1);
4067  putULong(iads.buffer->data + 8, t2);
4068 
4069  log_debug("XMT: X-- IA_PD %s",
4070  print_hex_1(4, iads.data, 59));
4071  log_debug("XMT: | X-- Requested renew +%u",
4072  (unsigned) t1);
4073  log_debug("XMT: | X-- Requested rebind +%u",
4074  (unsigned) t2);
4075  break;
4076 
4077  case DHCPV6_RELEASE:
4078  /* Set t1 and t2 to zero; server will ignore them */
4079  memset(iads.buffer->data + 4, 0, 8);
4080  log_debug("XMT: X-- IA_PD %s",
4081  print_hex_1(4, iads.buffer->data, 55));
4082 
4083  break;
4084 
4085  default:
4086  log_fatal("Impossible condition at %s:%d.", MDL);
4087  }
4088 
4089  for (pref = ia->addrs ; pref != NULL ; pref = pref->next) {
4090  /*
4091  * Do not confirm expired prefixes, do not request
4092  * expired prefixes (but we keep them around for
4093  * solicit).
4094  */
4095  if (pref->flags & DHC6_ADDR_EXPIRED)
4096  continue;
4097 
4098  if (pref->address.len != 16) {
4099  log_error("Illegal IPv6 prefix "
4100  "ignoring. (%s:%d)",
4101  MDL);
4102  continue;
4103  }
4104 
4105  if (pref->plen == 0) {
4106  log_info("Null IPv6 prefix, "
4107  "ignoring. (%s:%d)",
4108  MDL);
4109  }
4110 
4111  if (!buffer_allocate(&prefds.buffer, 25, MDL)) {
4112  log_error("Unable to allocate memory for "
4113  "IAPREFIX.");
4114  rval = ISC_R_NOMEMORY;
4115  break;
4116  }
4117 
4118  prefds.data = prefds.buffer->data;
4119  prefds.len = 25;
4120 
4121  /* Copy the prefix into the packet buffer. */
4122  putUChar(prefds.buffer->data + 8, pref->plen);
4123  memcpy(prefds.buffer->data + 9,
4124  pref->address.iabuf,
4125  16);
4126 
4127  /* Copy in additional information as appropriate */
4128  switch (message) {
4129  case DHCPV6_REQUEST:
4130  case DHCPV6_RENEW:
4131  case DHCPV6_REBIND:
4132  t1 = client->config->requested_lease;
4133  t2 = t1 + 300;
4134  putULong(prefds.buffer->data, t1);
4135  putULong(prefds.buffer->data + 4, t2);
4136 
4137  log_debug("XMT: | | X-- IAPREFIX %s/%u",
4138  piaddr(pref->address),
4139  (unsigned) pref->plen);
4140  log_debug("XMT: | | | X-- Preferred "
4141  "lifetime +%u", (unsigned)t1);
4142  log_debug("XMT: | | | X-- Max lifetime +%u",
4143  (unsigned)t2);
4144 
4145  break;
4146 
4147  case DHCPV6_RELEASE:
4148  /* Preferred and max life are irrelevant */
4149  memset(prefds.buffer->data, 0, 8);
4150  log_debug("XMT: | X-- Release Prefix %s/%u",
4151  piaddr(pref->address),
4152  (unsigned) pref->plen);
4153  break;
4154 
4155  default:
4156  log_fatal("Impossible condition at %s:%d.",
4157  MDL);
4158  }
4159 
4161  iaprefix_option, &prefds);
4162  data_string_forget(&prefds, MDL);
4163  }
4164 
4165  /*
4166  * It doesn't make sense to make a request without an
4167  * address.
4168  */
4169  if (ia->addrs == NULL) {
4170  log_debug("!!!: V IA_PD has no IAPREFIXs - removed.");
4171  rval = ISC_R_FAILURE;
4172  } else if (rval == ISC_R_SUCCESS) {
4173  log_debug("XMT: V IA_PD appended.");
4174  append_option(packet, &dhcpv6_universe,
4175  ia_pd_option, &iads);
4176  }
4177 
4178  data_string_forget(&iads, MDL);
4179  }
4180 
4181  if (rval == ISC_R_SUCCESS)
4182  *added = i;
4183 
4184  return (rval);
4185 }
4186 
4187 /* stopping_finished() checks if there is a remaining work to do.
4188  */
4189 static isc_boolean_t
4190 stopping_finished(void)
4191 {
4192  struct interface_info *ip;
4193  struct client_state *client;
4194 
4195  for (ip = interfaces; ip; ip = ip -> next) {
4196  for (client = ip -> client; client; client = client -> next) {
4197  if (client->state != S_STOPPED)
4198  return ISC_FALSE;
4199  if (client->active_lease != NULL)
4200  return ISC_FALSE;
4201  }
4202  }
4203  return ISC_TRUE;
4204 }
4205 
4206 /* reply_handler() accepts a Reply while we're attempting Select or Renew or
4207  * Rebind. Basically any Reply packet.
4208  */
4209 void
4210 reply_handler(struct packet *packet, struct client_state *client)
4211 {
4212  struct dhc6_lease *lease;
4213  isc_result_t check_status;
4214 
4215  if (packet->dhcpv6_msg_type != DHCPV6_REPLY)
4216  return;
4217 
4218  /* RFC3315 section 15.10 validation (same as 15.3 since we
4219  * always include a client id).
4220  */
4221  if (!valid_reply(packet, client)) {
4222  log_error("Invalid Reply - rejecting.");
4223  return;
4224  }
4225 
4226  lease = dhc6_leaseify(packet, client);
4227 
4228  /* Out of memory or corrupt packet condition...hopefully a temporary
4229  * problem. Returning now makes us try to retransmit later.
4230  */
4231  if (lease == NULL)
4232  return;
4233 
4234  check_status = dhc6_check_reply(client, lease);
4235  if (check_status != ISC_R_SUCCESS) {
4236  dhc6_lease_destroy(&lease, MDL);
4237 
4238  /* If no action was taken, but there is an error, then
4239  * we wait for a retransmission.
4240  */
4241  if (check_status != ISC_R_CANCELED)
4242  return;
4243  }
4244 
4245  /* We're done retransmitting at this point. */
4246  cancel_timeout(do_confirm6, client);
4247  cancel_timeout(do_select6, client);
4248  cancel_timeout(do_refresh6, client);
4249  cancel_timeout(do_release6, client);
4250  cancel_timeout(do_decline6, client);
4251 
4252  /* If this is in response to a Release/Decline, clean up and return. */
4253  if ((client->state == S_STOPPED) ||
4254  (client->state == S_DECLINED)) {
4255 
4256  if (client->active_lease != NULL) {
4257  dhc6_lease_destroy(&client->active_lease, MDL);
4258  client->active_lease = NULL;
4259  /* We should never wait for nothing!? */
4260  if (stopping_finished())
4261  exit(0);
4262  }
4263 
4264  if (client->state == S_DECLINED)
4265  start_init6(client);
4266 
4267  return;
4268  }
4269 
4270  /* Action was taken, so now that we've torn down our scheduled
4271  * retransmissions, return.
4272  */
4273  if (check_status == ISC_R_CANCELED)
4274  return;
4275 
4276  if (client->selected_lease != NULL) {
4278  client->selected_lease = NULL;
4279  }
4280 
4281  /* If this is in response to a confirm, we use the lease we've
4282  * already got, not the reply we were sent.
4283  */
4284  if (client->state == S_REBOOTING) {
4285  if (client->active_lease == NULL)
4286  log_fatal("Impossible condition at %s:%d.", MDL);
4287 
4288  dhc6_lease_destroy(&lease, MDL);
4289  start_bound(client);
4290  return;
4291  }
4292 
4293  /* Merge any bindings in the active lease (if there is one) into
4294  * the new active lease.
4295  */
4296  dhc6_merge_lease(client->active_lease, lease);
4297 
4298  /* Cleanup if a previous attempt to go bound failed. */
4299  if (client->old_lease != NULL) {
4300  dhc6_lease_destroy(&client->old_lease, MDL);
4301  client->old_lease = NULL;
4302  }
4303 
4304  /* Make this lease active and BIND to it. */
4305  if (client->active_lease != NULL)
4306  client->old_lease = client->active_lease;
4307  client->active_lease = lease;
4308 
4309  /* We're done with the ADVERTISEd leases, if any. */
4310  while(client->advertised_leases != NULL) {
4311  lease = client->advertised_leases;
4312  client->advertised_leases = lease->next;
4313 
4314  dhc6_lease_destroy(&lease, MDL);
4315  }
4316 
4317  start_bound(client);
4318 }
4319 
4320 /* DHCPv6 packets are a little sillier than they needed to be - the root
4321  * packet contains options, then IA's which contain options, then within
4322  * that IAADDR's which contain options.
4323  *
4324  * To sort this out at dhclient-script time (which fetches config parameters
4325  * in environment variables), start_bound() iterates over each IAADDR, and
4326  * calls this function to marshall an environment variable set that includes
4327  * the most-specific option values related to that IAADDR in particular.
4328  *
4329  * To achieve this, we load environment variables for the root options space,
4330  * then the IA, then the IAADDR. Any duplicate option names will be
4331  * over-written by the later versions.
4332  */
4333 static void
4334 dhc6_marshall_values(const char *prefix, struct client_state *client,
4335  struct dhc6_lease *lease, struct dhc6_ia *ia,
4336  struct dhc6_addr *addr)
4337 {
4338  /* Option cache contents, in descending order of
4339  * scope.
4340  */
4341  if ((lease != NULL) && (lease->options != NULL))
4342  script_write_params6(client, prefix, lease->options);
4343  if ((ia != NULL) && (ia->options != NULL))
4344  script_write_params6(client, prefix, ia->options);
4345  if ((addr != NULL) && (addr->options != NULL))
4346  script_write_params6(client, prefix, addr->options);
4347 
4348  /* addr fields. */
4349  if (addr != NULL) {
4350  if ((ia != NULL) && (ia->ia_type == D6O_IA_PD)) {
4351  client_envadd(client, prefix,
4352  "ip6_prefix", "%s/%u",
4353  piaddr(addr->address),
4354  (unsigned) addr->plen);
4355  } else {
4356  client_envadd(client, prefix, "ip6_prefixlen",
4358  client_envadd(client, prefix, "ip6_address",
4359  "%s", piaddr(addr->address));
4360  }
4361  if ((ia != NULL) && (ia->ia_type == D6O_IA_TA)) {
4362  client_envadd(client, prefix,
4363  "ip6_type", "temporary");
4364  }
4365  client_envadd(client, prefix, "life_starts", "%d",
4366  (int)(addr->starts));
4367  client_envadd(client, prefix, "preferred_life", "%u",
4368  addr->preferred_life);
4369  client_envadd(client, prefix, "max_life", "%u",
4370  addr->max_life);
4371  }
4372 
4373  /* ia fields. */
4374  if (ia != NULL) {
4375  client_envadd(client, prefix, "iaid", "%s",
4376  print_hex_1(4, ia->iaid, 12));
4377  client_envadd(client, prefix, "starts", "%d",
4378  (int)(ia->starts));
4379  client_envadd(client, prefix, "renew", "%u", ia->renew);
4380  client_envadd(client, prefix, "rebind", "%u", ia->rebind);
4381  }
4382 }
4383 
4384 /* Look at where the client's active lease is sitting. If it's looking to
4385  * time out on renew, rebind, depref, or expiration, do those things.
4386  */
4387 static void
4388 dhc6_check_times(struct client_state *client)
4389 {
4390  struct dhc6_lease *lease;
4391  struct dhc6_ia *ia;
4392  struct dhc6_addr *addr;
4393  TIME renew=MAX_TIME, rebind=MAX_TIME, depref=MAX_TIME,
4394  lo_expire=MAX_TIME, hi_expire=0, max_ia_starts = 0, tmp;
4395  int has_addrs = ISC_FALSE;
4396  struct timeval tv;
4397 
4398  lease = client->active_lease;
4399 
4400  /* Bit spammy. We should probably keep record of scheduled
4401  * events instead.
4402  */
4403  cancel_timeout(start_renew6, client);
4404  cancel_timeout(start_rebind6, client);
4405  cancel_timeout(do_depref, client);
4406  cancel_timeout(do_expire, client);
4407 
4408  for(ia = lease->bindings ; ia != NULL ; ia = ia->next) {
4409  TIME this_ia_lo_expire, this_ia_hi_expire, use_expire;
4410 
4411  this_ia_lo_expire = MAX_TIME;
4412  this_ia_hi_expire = 0;
4413 
4414  for (addr = ia->addrs ; addr != NULL ; addr = addr->next) {
4415  if(!(addr->flags & DHC6_ADDR_DEPREFFED)) {
4416  if (addr->preferred_life == 0xffffffff)
4417  tmp = MAX_TIME;
4418  else
4419  tmp = addr->starts +
4420  addr->preferred_life;
4421 
4422  if (tmp < depref)
4423  depref = tmp;
4424  }
4425 
4426  if (!(addr->flags & DHC6_ADDR_EXPIRED)) {
4427  /* Find EPOCH-relative expiration. */
4428  if (addr->max_life == 0xffffffff)
4429  tmp = MAX_TIME;
4430  else
4431  tmp = addr->starts + addr->max_life;
4432 
4433  /* Make the times ia->starts relative. */
4434  tmp -= ia->starts;
4435 
4436  if (tmp > this_ia_hi_expire)
4437  this_ia_hi_expire = tmp;
4438  if (tmp < this_ia_lo_expire)
4439  this_ia_lo_expire = tmp;
4440 
4441  has_addrs = ISC_TRUE;
4442  }
4443  }
4444 
4445  /* These times are ia->starts relative. */
4446  if (this_ia_lo_expire <= (this_ia_hi_expire / 2))
4447  use_expire = this_ia_hi_expire;
4448  else
4449  use_expire = this_ia_lo_expire;
4450 
4451  /*
4452  * If the auto-selected expiration time is "infinite", or
4453  * zero, assert a reasonable default.
4454  */
4455  if ((use_expire == MAX_TIME) || (use_expire <= 1))
4456  use_expire = client->config->requested_lease / 2;
4457  else
4458  use_expire /= 2;
4459 
4460  /* Don't renew/rebind temporary addresses. */
4461  /* For NA and PD we find the most recent IA and the smallest
4462  * values for the renew and rebind then base the timer on
4463  * the sum of the them.
4464  * Normally all the IAs will have the same time as they
4465  * are requested and served as a group but in some cases the
4466  * client isn't asking for all of the IAs (for example
4467  * restarted with a different set of arguments) or the server
4468  * isn't updating the client on all of them (probably a
4469  * broken server).
4470  */
4471  if (ia->ia_type != D6O_IA_TA) {
4472  if (ia->starts > max_ia_starts)
4473  max_ia_starts = ia->starts;
4474 
4475  if (ia->renew == 0) {
4476  tmp = use_expire;
4477  } else if (ia->renew == 0xffffffff)
4478  tmp = MAX_TIME;
4479  else
4480  tmp = ia->renew;
4481 
4482  if (tmp < renew)
4483  renew = tmp;
4484 
4485  if (ia->rebind == 0) {
4486  /* Set rebind to 3/4 expiration interval. */
4487  tmp = use_expire + (use_expire / 2);
4488  } else if (ia->rebind == 0xffffffff)
4489  tmp = MAX_TIME;
4490  else
4491  tmp = ia->rebind;
4492 
4493  if (tmp < rebind)
4494  rebind = tmp;
4495  }
4496 
4497  /*
4498  * Return expiration ranges to EPOCH relative for event
4499  * scheduling (add_timeout()).
4500  */
4501  this_ia_hi_expire += ia->starts;
4502  this_ia_lo_expire += ia->starts;
4503 
4504  if (this_ia_hi_expire > hi_expire)
4505  hi_expire = this_ia_hi_expire;
4506  if (this_ia_lo_expire < lo_expire)
4507  lo_expire = this_ia_lo_expire;
4508  }
4509 
4510  /* If there are no addresses, give up, go to INIT.
4511  * Note that if an address is unexpired with a date in the past,
4512  * we're scheduling an expiration event to ocurr in the past. We
4513  * could probably optimize this to expire now (but then there's
4514  * recursion).
4515  *
4516  * In the future, we may decide that we're done here, or to
4517  * schedule a future request (using 4-pkt info-request model).
4518  */
4519  if (has_addrs == ISC_FALSE) {
4520  dhc6_lease_destroy(&client->active_lease, MDL);
4521  client->active_lease = NULL;
4522 
4523  /* Go back to the beginning. */
4524  start_init6(client);
4525  return;
4526  }
4527 
4528  /* Second part of calculating the renew and rebind times.
4529  * We have the start time and the desired periods for renew
4530  * and rebind, just add them to get the desired end time.
4531  */
4532  if (renew != MAX_TIME)
4533  renew += max_ia_starts;
4534  if (rebind != MAX_TIME)
4535  rebind += max_ia_starts;
4536 
4537  switch(client->state) {
4538  case S_BOUND:
4539  /* We'd like to hit renewing, but if rebinding has already
4540  * passed (time warp), head straight there.
4541  */
4542  if ((rebind > cur_time) && (renew < rebind)) {
4543  log_debug("PRC: Renewal event scheduled in %d seconds, "
4544  "to run for %u seconds.",
4545  (int)(renew - cur_time),
4546  (unsigned)(rebind - renew));
4547  client->next_MRD = rebind;
4548  tv.tv_sec = renew;
4549  tv.tv_usec = 0;
4550  add_timeout(&tv, start_renew6, client, NULL, NULL);
4551 
4552  break;
4553  }
4554  /* FALL THROUGH */
4555  case S_RENEWING:
4556  /* While actively renewing, MRD is bounded by the time
4557  * we stop renewing and start rebinding. This helps us
4558  * process the state change on time.
4559  */
4560  client->MRD = rebind - cur_time;
4561  if (rebind != MAX_TIME) {
4562  log_debug("PRC: Rebind event scheduled in %d seconds, "
4563  "to run for %d seconds.",
4564  (int)(rebind - cur_time),
4565  (int)(hi_expire - rebind));
4566  client->next_MRD = hi_expire;
4567  tv.tv_sec = rebind;
4568  tv.tv_usec = 0;
4569  add_timeout(&tv, start_rebind6, client, NULL, NULL);
4570  }
4571  break;
4572 
4573  case S_REBINDING:
4574  /* For now, we rebind up until the last lease expires. In
4575  * the future, we might want to start SOLICITing when we've
4576  * depreffed an address.
4577  */
4578  client->MRD = hi_expire - cur_time;
4579  break;
4580 
4581  default:
4582  log_fatal("Impossible condition at %s:%d.", MDL);
4583  }
4584 
4585  /* Separately, set a time at which we will depref and expire
4586  * leases. This might happen with multiple addresses while we
4587  * keep trying to refresh.
4588  */
4589  if (depref != MAX_TIME) {
4590  log_debug("PRC: Depreference scheduled in %d seconds.",
4591  (int)(depref - cur_time));
4592  tv.tv_sec = depref;
4593  tv.tv_usec = 0;
4594  add_timeout(&tv, do_depref, client, NULL, NULL);
4595  }
4596  if (lo_expire != MAX_TIME) {
4597  log_debug("PRC: Expiration scheduled in %d seconds.",
4598  (int)(lo_expire - cur_time));
4599  tv.tv_sec = lo_expire;
4600  tv.tv_usec = 0;
4601  add_timeout(&tv, do_expire, client, NULL, NULL);
4602  }
4603 }
4604 
4605 /* In a given IA chain, find the IA with the same type and 'iaid'. */
4606 static struct dhc6_ia *
4607 find_ia(struct dhc6_ia *head, u_int16_t type, const char *id)
4608 {
4609  struct dhc6_ia *ia;
4610 
4611  for (ia = head ; ia != NULL ; ia = ia->next) {
4612  if (ia->ia_type != type)
4613  continue;
4614  if (memcmp(ia->iaid, id, 4) == 0)
4615  return ia;
4616  }
4617 
4618  return NULL;
4619 }
4620 
4621 /* In a given address chain, find a matching address. */
4622 static struct dhc6_addr *
4623 find_addr(struct dhc6_addr *head, struct iaddr *address)
4624 {
4625  struct dhc6_addr *addr;
4626 
4627  for (addr = head ; addr != NULL ; addr = addr->next) {
4628  if ((addr->address.len == address->len) &&
4629  (memcmp(addr->address.iabuf, address->iabuf,
4630  address->len) == 0))
4631  return addr;
4632  }
4633 
4634  return NULL;
4635 }
4636 
4637 /* In a given prefix chain, find a matching prefix. */
4638 static struct dhc6_addr *
4639 find_pref(struct dhc6_addr *head, struct iaddr *prefix, u_int8_t plen)
4640 {
4641  struct dhc6_addr *pref;
4642 
4643  for (pref = head ; pref != NULL ; pref = pref->next) {
4644  if ((pref->address.len == prefix->len) &&
4645  (pref->plen == plen) &&
4646  (memcmp(pref->address.iabuf, prefix->iabuf,
4647  prefix->len) == 0))
4648  return pref;
4649  }
4650 
4651  return NULL;
4652 }
4653 
4654 /*
4655  *
4656  * \brief Merge the bindings from the source lease into the destination
4657  * lease structure, where they are missing.
4658  *
4659  * This is used to merge any extra information we have in the current
4660  * (older, src) lease into the lease we have just received. For example
4661  * the src lease might include a binding for an NA that is still usable
4662  * but that we didn't request or that the server is no longer serving.
4663  * We want to keep that information until we toss the binding (expire,
4664  * release) so we move it to the new lease.
4665  *
4666  * We have to copy the stateful objects rather than move them over,
4667  * because later code needs to be able to compare new versus old if
4668  * they contain any bindings.
4669  *
4670  * \param src The older lease to copy the objects from
4671  * \param dst The newer lease to copy the objects to
4672  */
4673 static void
4674 dhc6_merge_lease(struct dhc6_lease *src, struct dhc6_lease *dst)
4675 {
4676  struct dhc6_ia *sia, *dia, *tia, **eia;
4677  struct dhc6_addr *saddr, *daddr, *taddr;
4678  int changes = 0;
4679 
4680  if ((dst == NULL) || (src == NULL))
4681  return;
4682 
4683  for (sia = src->bindings ; sia != NULL ; sia = sia->next) {
4684  dia = find_ia(dst->bindings, sia->ia_type, (char *)sia->iaid);
4685 
4686  if (dia == NULL) {
4687  tia = dhc6_dup_ia(sia, MDL);
4688 
4689  if (tia == NULL)
4690  log_fatal("Out of memory merging lease - "
4691  "Unable to continue without losing "
4692  "state! (%s:%d)", MDL);
4693 
4694  /* Put any bindings that aren't in the new lease at the
4695  * end of the list. If the user or server reduces the
4696  * number of IAs the ones in use will be at the front
4697  * and will be used when building the next requests
4698  * We could be more efficient by finding the end
4699  * of the list once but we don't expect to do this
4700  * often.
4701  */
4702  for (eia = &dst->bindings;
4703  *eia != NULL;
4704  eia = &(*eia)->next) {
4705  ; /* no work just find the end */
4706  }
4707  *eia = tia;
4708  changes = 1;
4709  } else {
4710  for (saddr = sia->addrs ; saddr != NULL ;
4711  saddr = saddr->next) {
4712  if (sia->ia_type != D6O_IA_PD)
4713  daddr = find_addr(dia->addrs,
4714  &saddr->address);
4715  else
4716  daddr = find_pref(dia->addrs,
4717  &saddr->address,
4718  saddr->plen);
4719 
4720  if (daddr == NULL) {
4721  taddr = dhc6_dup_addr(saddr, MDL);
4722 
4723  if (taddr == NULL)
4724  log_fatal("Out of memory "
4725  "merging lease - "
4726  "Unable to continue "
4727  "without losing "
4728  "state! (%s:%d)",
4729  MDL);
4730 
4731  /* XXX: consider sorting? */
4732  taddr->next = dia->addrs;
4733  dia->addrs = taddr;
4734  changes = 1;
4735  }
4736  }
4737  }
4738  }
4739 
4740  /* If we made changes, reset the score to 0 so it is recalculated. */
4741  if (changes)
4742  dst->score = 0;
4743 }
4744 
4745 /* We've either finished selecting or succeeded in Renew or Rebinding our
4746  * lease. In all cases we got a Reply. Give dhclient-script a tickle
4747  * to inform it about the new values, and then lay in wait for the next
4748  * event.
4749  */
4750 static void
4751 start_bound(struct client_state *client)
4752 {
4753  struct dhc6_ia *ia, *oldia;
4754  struct dhc6_addr *addr, *oldaddr;
4755  struct dhc6_lease *lease, *old;
4756  const char *reason;
4757 #if defined (NSUPDATE)
4758  TIME dns_update_offset = 1;
4759 #endif
4760 
4761  lease = client->active_lease;
4762  if (lease == NULL) {
4763  log_error("Cannot enter bound state unless an active lease "
4764  "is selected.");
4765  return;
4766  }
4767  lease->released = ISC_FALSE;
4768  old = client->old_lease;
4769 
4770  client->v6_handler = bound_handler;
4771 
4772  switch (client->state) {
4773  case S_SELECTING:
4774  case S_REBOOTING: /* Pretend we got bound. */
4775  reason = "BOUND6";
4776  break;
4777 
4778  case S_RENEWING:
4779  reason = "RENEW6";
4780  break;
4781 
4782  case S_REBINDING:
4783  reason = "REBIND6";
4784  break;
4785 
4786  default:
4787  log_fatal("Impossible condition at %s:%d.", MDL);
4788  /* Silence compiler warnings. */
4789  return;
4790  }
4791 
4792  log_debug("PRC: Bound to lease %s.",
4794  client->active_lease->server_id.data, 55));
4795  client->state = S_BOUND;
4796 
4797  write_client6_lease(client, lease, 0, 1);
4798 
4799  oldia = NULL;
4800  for (ia = lease->bindings ; ia != NULL ; ia = ia->next) {
4801  if (old != NULL)
4802  oldia = find_ia(old->bindings,
4803  ia->ia_type,
4804  (char *)ia->iaid);
4805  else
4806  oldia = NULL;
4807 
4808  for (addr = ia->addrs ; addr != NULL ; addr = addr->next) {
4809  /* Don't try to use the address if it's already expired */
4810  if (addr->flags & DHC6_ADDR_EXPIRED)
4811  continue;
4812 
4813  if (oldia != NULL) {
4814  if (ia->ia_type != D6O_IA_PD)
4815  oldaddr = find_addr(oldia->addrs,
4816  &addr->address);
4817  else
4818  oldaddr = find_pref(oldia->addrs,
4819  &addr->address,
4820  addr->plen);
4821  } else
4822  oldaddr = NULL;
4823 
4824 #if defined (NSUPDATE)
4825  if ((oldaddr == NULL) && (ia->ia_type == D6O_IA_NA))
4827  &addr->address,
4828  dns_update_offset++);
4829 #endif
4830 
4831  /* Shell out to setup the new binding. */
4832  script_init(client, reason, NULL);
4833 
4834  if (old != NULL)
4835  dhc6_marshall_values("old_", client, old,
4836  oldia, oldaddr);
4837  dhc6_marshall_values("new_", client, lease, ia, addr);
4838  script_write_requested6(client);
4839 
4840  // when script returns 3, DAD failed
4841  if (script_go(client) == 3) {
4842  start_decline6(client);
4843  return;
4844  }
4845  }
4846 
4847  /* XXX: maybe we should loop on the old values instead? */
4848  if (ia->addrs == NULL) {
4849  script_init(client, reason, NULL);
4850 
4851  if (old != NULL)
4852  dhc6_marshall_values("old_", client, old,
4853  oldia,
4854  oldia != NULL ?
4855  oldia->addrs : NULL);
4856 
4857  dhc6_marshall_values("new_", client, lease, ia,
4858  NULL);
4859  script_write_requested6(client);
4860 
4861  script_go(client);
4862  }
4863  }
4864 
4865  /* XXX: maybe we should loop on the old values instead? */
4866  if (lease->bindings == NULL) {
4867  script_init(client, reason, NULL);
4868 
4869  if (old != NULL)
4870  dhc6_marshall_values("old_", client, old,
4871  old->bindings,
4872  (old->bindings != NULL) ?
4873  old->bindings->addrs : NULL);
4874 
4875  dhc6_marshall_values("new_", client, lease, NULL, NULL);
4876  script_write_requested6(client);
4877 
4878  script_go(client);
4879  }
4880 
4881 #ifdef DHCP4o6
4882  if (dhcpv4_over_dhcpv6)
4883  dhcp4o6_start();
4884 #endif
4885 
4886  go_daemon();
4887 
4888  if (client->old_lease != NULL) {
4889  dhc6_lease_destroy(&client->old_lease, MDL);
4890  client->old_lease = NULL;
4891  }
4892 
4893  /* Schedule events. */
4894  dhc6_check_times(client);
4895 }
4896 
4897 /*
4898  * Decline addresses.
4899  */
4900 void
4901 start_decline6(struct client_state *client)
4902 {
4903  /* Cancel any pending transmissions */
4904  cancel_timeout(do_confirm6, client);
4905  cancel_timeout(do_select6, client);
4906  cancel_timeout(do_refresh6, client);
4907  cancel_timeout(do_release6, client);
4908  cancel_timeout(do_decline6, client);
4909  client->state = S_DECLINED;
4910 
4911  if (client->active_lease == NULL)
4912  return;
4913 
4914  /* Set timers per RFC3315 section 18.1.7. */
4915  client->IRT = DEC_TIMEOUT * 100;
4916  client->MRT = 0;
4917  client->MRC = DEC_MAX_RC;
4918  client->MRD = 0;
4919 
4920  dhc6_retrans_init(client);
4921  client->v6_handler = reply_handler;
4922 
4923  client->refresh_type = DHCPV6_DECLINE;
4924  do_decline6(client);
4925 }
4926 
4927 /*
4928  * do_decline6() creates a Decline packet and transmits it.
4929  */
4930 static void
4931 do_decline6(void *input)
4932 {
4933  struct client_state *client;
4934  struct data_string ds;
4935  struct timeval elapsed, tv;
4936  int send_ret, added;
4937 
4938  client = input;
4939 
4940  if ((client->active_lease == NULL) || !active_prefix(client))
4941  return;
4942 
4943  if ((client->MRC != 0) && (client->txcount > client->MRC)) {
4944  log_info("Max retransmission count exceeded.");
4945  goto decline_done;
4946  }
4947 
4948  /*
4949  * Start_time starts at the first transmission.
4950  */
4951  if (client->txcount == 0) {
4952  client->start_time.tv_sec = cur_tv.tv_sec;
4953  client->start_time.tv_usec = cur_tv.tv_usec;
4954  }
4955 
4956  /* elapsed = cur - start */
4957  elapsed.tv_sec = cur_tv.tv_sec - client->start_time.tv_sec;
4958  elapsed.tv_usec = cur_tv.tv_usec - client->start_time.tv_usec;
4959  if (elapsed.tv_usec < 0) {
4960  elapsed.tv_sec -= 1;
4961  elapsed.tv_usec += 1000000;
4962  }
4963 
4964  memset(&ds, 0, sizeof(ds));
4965  if (!buffer_allocate(&ds.buffer, 4, MDL)) {
4966  log_error("Unable to allocate memory for Decline.");
4967  goto decline_done;
4968  }
4969 
4970  ds.data = ds.buffer->data;
4971  ds.len = 4;
4972  ds.buffer->data[0] = DHCPV6_DECLINE;
4973  memcpy(ds.buffer->data + 1, client->dhcpv6_transaction_id, 3);
4974 
4975  /* Form an elapsed option. */
4976  /* Maximum value is 65535 1/100s coded as 0xffff. */
4977  if ((elapsed.tv_sec < 0) || (elapsed.tv_sec > 655) ||
4978  ((elapsed.tv_sec == 655) && (elapsed.tv_usec > 350000))) {
4979  client->elapsed = 0xffff;
4980  } else {
4981  client->elapsed = elapsed.tv_sec * 100;
4982  client->elapsed += elapsed.tv_usec / 10000;
4983  }
4984 
4985  client->elapsed = htons(client->elapsed);
4986 
4987  log_debug("XMT: Forming Decline.");
4988  make_client6_options(client, &client->sent_options,
4989  client->active_lease, DHCPV6_DECLINE);
4990  dhcpv6_universe.encapsulate(&ds, NULL, NULL, client, NULL,
4991  client->sent_options, &global_scope,
4992  &dhcpv6_universe);
4993 
4994  /* Append IA's (but don't release temporary addresses). */
4995  if (wanted_ia_na &&
4996  dhc6_add_ia_na(client, &ds, client->active_lease,
4997  DHCPV6_DECLINE, 0, &added) != ISC_R_SUCCESS) {
4998  data_string_forget(&ds, MDL);
4999  goto decline_done;
5000  }
5001  if (wanted_ia_pd &&
5002  dhc6_add_ia_pd(client, &ds, client->active_lease,
5003  DHCPV6_DECLINE, 0, &added) != ISC_R_SUCCESS) {
5004  data_string_forget(&ds, MDL);
5005  goto decline_done;
5006  }
5007 
5008  /* Transmit and wait. */
5009  log_info("XMT: Decline on %s, interval %ld0ms.",
5010  client->name ? client->name : client->interface->name,
5011  (long int)client->RT);
5012 
5013  send_ret = send_packet6(client->interface, ds.data, ds.len,
5014  &DHCPv6DestAddr);
5015  if (send_ret != ds.len) {
5016  log_error("dhc6: sendpacket6() sent %d of %d bytes",
5017  send_ret, ds.len);
5018  }
5019 
5020  data_string_forget(&ds, MDL);
5021 
5022  /* Wait RT */
5023  tv.tv_sec = cur_tv.tv_sec + client->RT / 100;
5024  tv.tv_usec = cur_tv.tv_usec + (client->RT % 100) * 10000;
5025  if (tv.tv_usec >= 1000000) {
5026  tv.tv_sec += 1;
5027  tv.tv_usec -= 1000000;
5028  }
5029  add_timeout(&tv, do_decline6, client, NULL, NULL);
5030  dhc6_retrans_advance(client);
5031  return;
5032 
5033 decline_done:
5034  dhc6_lease_destroy(&client->active_lease, MDL);
5035  client->active_lease = NULL;
5036  start_init6(client);
5037  return;
5038 }
5039 
5040 /* While bound, ignore packets. In the future we'll want to answer
5041  * Reconfigure-Request messages and the like.
5042  */
5043 void
5044 bound_handler(struct packet *packet, struct client_state *client)
5045 {
5046  log_debug("RCV: Input packets are ignored once bound.");
5047 }
5048 
5049 /* start_renew6() gets us all ready to go to start transmitting Renew packets.
5050  * Note that client->next_MRD must be set before entering this function -
5051  * it must be set to the time at which the client should start Rebinding.
5052  */
5053 void
5054 start_renew6(void *input)
5055 {
5056  struct client_state *client;
5057 
5058  client = (struct client_state *)input;
5059 
5060  log_info("PRC: Renewing lease on %s.",
5061  client->name ? client->name : client->interface->name);
5062  client->state = S_RENEWING;
5063 
5064  client->v6_handler = reply_handler;
5065 
5066  /* Times per RFC3315 section 18.1.3. */
5067  client->IRT = REN_TIMEOUT * 100;
5068  client->MRT = REN_MAX_RT * 100;
5069  client->MRC = 0;
5070  /* MRD is special in renew - we need to set it by checking timer
5071  * state.
5072  */
5073  client->MRD = client->next_MRD - cur_time;
5074 
5075  dhc6_retrans_init(client);
5076 
5077  client->refresh_type = DHCPV6_RENEW;
5078  do_refresh6(client);
5079 }
5080 
5081 /* do_refresh6() transmits one DHCPv6 packet, be it a Renew or Rebind, and
5082  * gives the retransmission state a bump for the next time. Note that
5083  * client->refresh_type must be set before entering this function.
5084  */
5085 void
5086 do_refresh6(void *input)
5087 {
5088  struct option_cache *oc;
5089  struct sockaddr_in6 unicast, *dest_addr = &DHCPv6DestAddr;
5090  struct data_string ds;
5091  struct client_state *client;
5092  struct dhc6_lease *lease;
5093  struct timeval elapsed, tv;
5094  int send_ret, added;
5095 
5096  client = (struct client_state *)input;
5097  memset(&ds, 0, sizeof(ds));
5098 
5099  lease = client->active_lease;
5100  if (lease == NULL) {
5101  log_error("Cannot renew without an active binding.");
5102  return;
5103  }
5104 
5105  /* Ensure we're emitting a valid message type. */
5106  switch (client->refresh_type) {
5107  case DHCPV6_RENEW:
5108  case DHCPV6_REBIND:
5109  break;
5110 
5111  default:
5112  log_fatal("Internal inconsistency (%d) at %s:%d.",
5113  client->refresh_type, MDL);
5114  }
5115 
5116  /*
5117  * Start_time starts at the first transmission.
5118  */
5119  if (client->txcount == 0) {
5120  client->start_time.tv_sec = cur_tv.tv_sec;
5121  client->start_time.tv_usec = cur_tv.tv_usec;
5122  }
5123 
5124  /* elapsed = cur - start */
5125  elapsed.tv_sec = cur_tv.tv_sec - client->start_time.tv_sec;
5126  elapsed.tv_usec = cur_tv.tv_usec - client->start_time.tv_usec;
5127  if (elapsed.tv_usec < 0) {
5128  elapsed.tv_sec -= 1;
5129  elapsed.tv_usec += 1000000;
5130  }
5131  if (((client->MRC != 0) && (client->txcount > client->MRC)) ||
5132  ((client->MRD != 0) && (elapsed.tv_sec >= client->MRD))) {
5133  /* We're done. Move on to the next phase, if any. */
5134  dhc6_check_times(client);
5135  return;
5136  }
5137 
5138  /*
5139  * Check whether the server has sent a unicast option; if so, we can
5140  * use the address it specified for RENEWs.
5141  */
5143  if (oc && evaluate_option_cache(&ds, NULL, NULL, NULL,
5144  lease->options, NULL, &global_scope,
5145  oc, MDL)) {
5146  if (ds.len < 16) {
5147  log_error("Invalid unicast option length %d.", ds.len);
5148  } else {
5149  memset(&unicast, 0, sizeof(DHCPv6DestAddr));
5150  unicast.sin6_family = AF_INET6;
5151  unicast.sin6_port = remote_port;
5152  memcpy(&unicast.sin6_addr, ds.data, 16);
5153  if (client->refresh_type == DHCPV6_RENEW) {
5154  dest_addr = &unicast;
5155  }
5156  }
5157 
5158  data_string_forget(&ds, MDL);
5159  }
5160 
5161  /* Commence forming a renew packet. */
5162  memset(&ds, 0, sizeof(ds));
5163  if (!buffer_allocate(&ds.buffer, 4, MDL)) {
5164  log_error("Unable to allocate memory for packet.");
5165  return;
5166  }
5167  ds.data = ds.buffer->data;
5168  ds.len = 4;
5169 
5170  ds.buffer->data[0] = client->refresh_type;
5171  memcpy(ds.buffer->data + 1, client->dhcpv6_transaction_id, 3);
5172 
5173  /* Form an elapsed option. */
5174  /* Maximum value is 65535 1/100s coded as 0xffff. */
5175  if ((elapsed.tv_sec < 0) || (elapsed.tv_sec > 655) ||
5176  ((elapsed.tv_sec == 655) && (elapsed.tv_usec > 350000))) {
5177  client->elapsed = 0xffff;
5178  } else {
5179  client->elapsed = elapsed.tv_sec * 100;
5180  client->elapsed += elapsed.tv_usec / 10000;
5181  }
5182 
5183  if (client->elapsed == 0)
5184  log_debug("XMT: Forming %s, 0 ms elapsed.",
5185  dhcpv6_type_names[client->refresh_type]);
5186  else
5187  log_debug("XMT: Forming %s, %u0 ms elapsed.",
5189  (unsigned)client->elapsed);
5190 
5191  client->elapsed = htons(client->elapsed);
5192 
5193  make_client6_options(client, &client->sent_options, lease,
5194  client->refresh_type);
5195 
5196  /* Put in any options from the sent cache. */
5197  dhcpv6_universe.encapsulate(&ds, NULL, NULL, client, NULL,
5198  client->sent_options, &global_scope,
5199  &dhcpv6_universe);
5200 
5201  /* Now append any IA's, and within them any IAADDR/IAPREFIXs.
5202  * For each type of IA (na, ta, pd) we start with the ones for
5203  * which we already have addresses (dhc6_add_ia_xx) and then
5204  * if we still want more we add aditional IAs (dhc6_bare_ia_xx)
5205  */
5206  if (wanted_ia_na &&
5207  ((dhc6_add_ia_na(client, &ds, lease, client->refresh_type,
5208  wanted_ia_na, &added) != ISC_R_SUCCESS) ||
5209  (dhc6_bare_ia_xx(client, &ds, wanted_ia_na - added,
5210  D6O_IA_NA) != ISC_R_SUCCESS))) {
5211  data_string_forget(&ds, MDL);
5212  return;
5213  }
5214  if (wanted_ia_pd &&
5215  ((dhc6_add_ia_pd(client, &ds, lease, client->refresh_type,
5216  wanted_ia_pd, &added) != ISC_R_SUCCESS) ||
5217  (dhc6_bare_ia_xx(client, &ds, wanted_ia_pd - added,
5218  D6O_IA_PD) != ISC_R_SUCCESS))) {
5219  data_string_forget(&ds, MDL);
5220  return;
5221  }
5222 
5223  log_info("XMT: %s on %s, interval %ld0ms.",
5225  client->name ? client->name : client->interface->name,
5226  (long int)client->RT);
5227 
5228  send_ret = send_packet6(client->interface, ds.data, ds.len, dest_addr);
5229 
5230  if (send_ret != ds.len) {
5231  log_error("dhc6: send_packet6() sent %d of %d bytes",
5232  send_ret, ds.len);
5233  }
5234 
5235  data_string_forget(&ds, MDL);
5236 
5237  /* Wait RT */
5238  tv.tv_sec = cur_tv.tv_sec + client->RT / 100;
5239  tv.tv_usec = cur_tv.tv_usec + (client->RT % 100) * 10000;
5240  if (tv.tv_usec >= 1000000) {
5241  tv.tv_sec += 1;
5242  tv.tv_usec -= 1000000;
5243  }
5244  add_timeout(&tv, do_refresh6, client, NULL, NULL);
5245 
5246  dhc6_retrans_advance(client);
5247 }
5248 
5249 /* start_rebind6() gets us all set up to go and rebind a lease. Note that
5250  * client->next_MRD must be set before entering this function. In this case,
5251  * MRD must be set to the maximum time any address in the packet will
5252  * expire.
5253  */
5254 void
5255 start_rebind6(void *input)
5256 {
5257  struct client_state *client;
5258 
5259  client = (struct client_state *)input;
5260 
5261  log_info("PRC: Rebinding lease on %s.",
5262  client->name ? client->name : client->interface->name);
5263  client->state = S_REBINDING;
5264 
5265  client->v6_handler = reply_handler;
5266 
5267  /* Times per RFC3315 section 18.1.4. */
5268  client->IRT = REB_TIMEOUT * 100;
5269  client->MRT = REB_MAX_RT * 100;
5270  client->MRC = 0;
5271  /* MRD is special in rebind - it's determined by the timer
5272  * state.
5273  */
5274  client->MRD = client->next_MRD - cur_time;
5275 
5276  dhc6_retrans_init(client);
5277 
5278  client->refresh_type = DHCPV6_REBIND;
5279  do_refresh6(client);
5280 }
5281 
5282 /* do_depref() runs through a given lease's addresses, for each that has
5283  * not yet been depreffed, shells out to the dhclient-script to inform it
5284  * of the status change. The dhclient-script should then do...something...
5285  * to encourage applications to move off the address and onto one of the
5286  * remaining 'preferred' addresses.
5287  */
5288 void
5289 do_depref(void *input)
5290 {
5291  struct client_state *client;
5292  struct dhc6_lease *lease;
5293  struct dhc6_ia *ia;
5294  struct dhc6_addr *addr;
5295 
5296  client = (struct client_state *)input;
5297 
5298  lease = client->active_lease;
5299  if (lease == NULL)
5300  return;
5301 
5302  for (ia = lease->bindings ; ia != NULL ; ia = ia->next) {
5303  for (addr = ia->addrs ; addr != NULL ; addr = addr->next) {
5304  if (addr->flags & DHC6_ADDR_DEPREFFED)
5305  continue;
5306 
5307  if (addr->starts + addr->preferred_life <= cur_time) {
5308  script_init(client, "DEPREF6", NULL);
5309  dhc6_marshall_values("cur_", client, lease,
5310  ia, addr);
5311  script_write_requested6(client);
5312  script_go(client);
5313 
5314  addr->flags |= DHC6_ADDR_DEPREFFED;
5315 
5316  if (ia->ia_type != D6O_IA_PD)
5317  log_info("PRC: Address %s depreferred.",
5318  piaddr(addr->address));
5319  else
5320  log_info("PRC: Prefix %s/%u depreferred.",
5321  piaddr(addr->address),
5322  (unsigned) addr->plen);
5323 
5324 #if defined (NSUPDATE)
5325  /* Remove DDNS bindings at depref time. */
5326  if ((ia->ia_type == D6O_IA_NA) &&
5327  client->config->do_forward_update)
5328  client_dns_remove(client,
5329  &addr->address);
5330 #endif
5331  }
5332  }
5333  }
5334 
5335  dhc6_check_times(client);
5336 }
5337 
5338 /* do_expire() searches through all the addresses on a given lease, and
5339  * expires/removes any addresses that are no longer valid.
5340  */
5341 void
5342 do_expire(void *input)
5343 {
5344  struct client_state *client;
5345  struct dhc6_lease *lease;
5346  struct dhc6_ia *ia, **tia;
5347  struct dhc6_addr *addr;
5348  int has_addrs = ISC_FALSE;
5349  int ia_has_addrs = ISC_FALSE;
5350 
5351  client = (struct client_state *)input;
5352 
5353  lease = client->active_lease;
5354  if (lease == NULL)
5355  return;
5356 
5357  for (ia = lease->bindings, tia = &lease->bindings; ia != NULL ; ) {
5358  ia_has_addrs = ISC_FALSE;
5359  for (addr = ia->addrs ; addr != NULL ; addr = addr->next) {
5360  if (addr->flags & DHC6_ADDR_EXPIRED)
5361  continue;
5362 
5363  if (addr->starts + addr->max_life <= cur_time) {
5364  script_init(client, "EXPIRE6", NULL);
5365  dhc6_marshall_values("old_", client, lease,
5366  ia, addr);
5367  script_write_requested6(client);
5368  script_go(client);
5369 
5370  addr->flags |= DHC6_ADDR_EXPIRED;
5371 
5372  if (ia->ia_type != D6O_IA_PD)
5373  log_info("PRC: Address %s expired.",
5374  piaddr(addr->address));
5375  else
5376  log_info("PRC: Prefix %s/%u expired.",
5377  piaddr(addr->address),
5378  (unsigned) addr->plen);
5379 
5380 #if defined (NSUPDATE)
5381  /* We remove DNS records at depref time, but
5382  * it is possible that we might get here
5383  * without depreffing.
5384  */
5385  if ((ia->ia_type == D6O_IA_NA) &&
5386  client->config->do_forward_update &&
5387  !(addr->flags & DHC6_ADDR_DEPREFFED))
5388  client_dns_remove(client,
5389  &addr->address);
5390 #endif
5391 
5392  continue;
5393  }
5394 
5395  ia_has_addrs = ISC_TRUE;
5396  has_addrs = ISC_TRUE;
5397  }
5398 
5399  /* Update to the next ia and git rid of this ia
5400  * if it doesn't have any leases.
5401  */
5402  if (ia_has_addrs == ISC_TRUE) {
5403  /* leases, just advance the list pointer */
5404  tia = &(*tia)->next;
5405  } else {
5406  /* no leases, update the list pointer
5407  * and free the ia
5408  */
5409  *tia = ia->next;
5410  dhc6_ia_destroy(&ia, MDL);
5411  }
5412  /* lastly update the ia pointer to our new ia */
5413  ia = *tia;
5414  }
5415 
5416  /* Clean up empty leases. */
5417  if (has_addrs == ISC_FALSE) {
5418  log_info("PRC: Bound lease is devoid of active addresses."
5419  " Re-initializing.");
5420 
5421  dhc6_lease_destroy(&lease, MDL);
5422  client->active_lease = NULL;
5423 
5424  start_init6(client);
5425  return;
5426  }
5427 
5428  /* Schedule the next run through. */
5429  dhc6_check_times(client);
5430 }
5431 
5432 /*
5433  * Run client script to unconfigure interface.
5434  * Called with reason STOP6 when dhclient -x is run, or with reason
5435  * RELEASE6 when server has replied to a Release message.
5436  * Stateless is a special case.
5437  */
5438 void
5439 unconfigure6(struct client_state *client, const char *reason)
5440 {
5441  struct dhc6_ia *ia;
5442  struct dhc6_addr *addr;
5443 
5444  if (stateless) {
5445  script_init(client, reason, NULL);
5446  if (client->active_lease != NULL)
5447  script_write_params6(client, "old_",
5448  client->active_lease->options);
5449  script_write_requested6(client);
5450  script_go(client);
5451  return;
5452  }
5453 
5454  if (client->active_lease == NULL)
5455  return;
5456 
5457  for (ia = client->active_lease->bindings ; ia != NULL ; ia = ia->next) {
5458  if (ia->ia_type == D6O_IA_TA)
5459  continue;
5460 
5461  for (addr = ia->addrs ; addr != NULL ; addr = addr->next) {
5462  script_init(client, reason, NULL);
5463  dhc6_marshall_values("old_", client,
5464  client->active_lease, ia, addr);
5465  script_write_requested6(client);
5466  script_go(client);
5467 
5468 #if defined (NSUPDATE)
5469  if ((ia->ia_type == D6O_IA_NA) &&
5470  client->config->do_forward_update)
5471  client_dns_remove(client, &addr->address);
5472 #endif
5473  }
5474  }
5475 }
5476 
5477 void
5478 refresh_info_request6(void *input)
5479 {
5480  struct client_state *client;
5481 
5482  client = (struct client_state *)input;
5483  start_info_request6(client);
5484 }
5485 
5486 /* Timeout for Information-Request (using the IRT option).
5487  */
5488 static void
5489 dhc6_check_irt(struct client_state *client)
5490 {
5491  struct option **req;
5492  struct option_cache *oc;
5493  TIME expire = MAX_TIME;
5494  struct timeval tv;
5495  int i;
5496  isc_boolean_t found = ISC_FALSE;
5497 
5498  cancel_timeout(refresh_info_request6, client);
5499 
5500  req = client->config->requested_options;
5501  for (i = 0; req[i] != NULL; i++) {
5502  if (req[i] == irt_option) {
5503  found = ISC_TRUE;
5504  break;
5505  }
5506  }
5507  /* Simply return gives a endless loop waiting for nothing. */
5508  if (!found) {
5509 #ifdef DHCP4o6
5510  if (!dhcpv4_over_dhcpv6)
5511 #endif
5512  exit(0);
5513  }
5514 
5517  if (oc != NULL) {
5518  struct data_string irt;
5519 
5520  memset(&irt, 0, sizeof(irt));
5521  if (!evaluate_option_cache(&irt, NULL, NULL, client,
5522  client->active_lease->options,
5523  NULL, &global_scope, oc, MDL) ||
5524  (irt.len < 4)) {
5525  log_error("Can't evaluate IRT.");
5526  } else {
5527  expire = getULong(irt.data);
5528  if (expire < IRT_MINIMUM)
5529  expire = IRT_MINIMUM;
5530  if (expire == 0xffffffff)
5531  expire = MAX_TIME;
5532  }
5533  data_string_forget(&irt, MDL);
5534  } else
5535  expire = IRT_DEFAULT;
5536 
5537  if (expire != MAX_TIME) {
5538  log_debug("PRC: Refresh event scheduled in %u seconds.",
5539  (unsigned) expire);
5540  tv.tv_sec = cur_time + expire;
5541  tv.tv_usec = 0;
5542  add_timeout(&tv, refresh_info_request6, client, NULL, NULL);
5543  }
5544 }
5545 
5546 /* We got a Reply. Give dhclient-script a tickle to inform it about
5547  * the new values, and then lay in wait for the next event.
5548  */
5549 static void
5550 start_informed(struct client_state *client)
5551 {
5552  client->v6_handler = informed_handler;
5553 
5554  log_debug("PRC: Done.");
5555 
5556  client->state = S_BOUND;
5557 
5558  script_init(client, "RENEW6", NULL);
5559  if (client->old_lease != NULL)
5560  script_write_params6(client, "old_",
5561  client->old_lease->options);
5562  script_write_params6(client, "new_", client->active_lease->options);
5563  script_write_requested6(client);
5564  script_go(client);
5565 
5566 #ifdef DHCP4o6
5567  if (dhcpv4_over_dhcpv6)
5568  dhcp4o6_start();
5569 #endif
5570 
5571  go_daemon();
5572 
5573  if (client->old_lease != NULL) {
5574  dhc6_lease_destroy(&client->old_lease, MDL);
5575  client->old_lease = NULL;
5576  }
5577 
5578  /* Schedule events. */
5579  dhc6_check_irt(client);
5580 }
5581 
5582 /* While informed, ignore packets.
5583  */
5584 void
5585 informed_handler(struct packet *packet, struct client_state *client)
5586 {
5587  log_debug("RCV: Input packets are ignored once bound.");
5588 }
5589 
5590 /* make_client6_options() fetches option caches relevant to the client's
5591  * scope and places them into the sent_options cache. This cache is later
5592  * used to populate DHCPv6 output packets with options.
5593  */
5594 static void
5595 make_client6_options(struct client_state *client, struct option_state **op,
5596  struct dhc6_lease *lease, u_int8_t message)
5597 {
5598  struct option_cache *oc;
5599  struct option **req;
5600  struct buffer *buffer;
5601  int buflen, i, oro_len;
5602 
5603  if ((op == NULL) || (client == NULL))
5604  return;
5605 
5606  if (*op)
5608 
5609  /* Create a cache to carry options to transmission. */
5611 
5612  /* Create and store an 'elapsed time' option in the cache. */
5613  oc = NULL;
5614  if (option_cache_allocate(&oc, MDL)) {
5615  const unsigned char *cdata;
5616 
5617  cdata = (unsigned char *)&client->elapsed;
5618 
5619  if (make_const_data(&oc->expression, cdata, 2, 0, 0, MDL)) {
5620  option_reference(&oc->option, elapsed_option, MDL);
5621  save_option(&dhcpv6_universe, *op, oc);
5622  }
5623 
5625  }
5626 
5627  /* Bring in any configured options to send. */
5628  if (client->config->on_transmission)
5629  execute_statements_in_scope(NULL, NULL, NULL, client,
5630  lease ? lease->options : NULL,
5631  *op, &global_scope,
5632  client->config->on_transmission,
5633  NULL, NULL);
5634 
5635  /* Rapid-commit is only for SOLICITs. */
5636  if (message != DHCPV6_SOLICIT)
5638 
5639  /* See if the user configured a DUID in a relevant scope. If not,
5640  * introduce our default manufactured id.
5641  */
5642  if ((oc = lookup_option(&dhcpv6_universe, *op,
5643  D6O_CLIENTID)) == NULL) {
5644  if (default_duid.len == 0 ||
5645  !option_cache(&oc, &default_duid, NULL, clientid_option,
5646  MDL))
5647  log_fatal("Failure assembling a DUID.");
5648 
5649  save_option(&dhcpv6_universe, *op, oc);
5651  }
5652 
5653  /* In cases where we're responding to a single server, put the
5654  * server's id in the response.
5655  *
5656  * Note that lease is NULL for SOLICIT or INFO request messages,
5657  * and otherwise MUST be present.
5658  */
5659  if (lease == NULL) {
5660  if ((message != DHCPV6_SOLICIT) &&
5661  (message != DHCPV6_INFORMATION_REQUEST))
5662  log_fatal("Impossible condition at %s:%d.", MDL);
5663  } else if ((message != DHCPV6_REBIND) &&
5664  (message != DHCPV6_CONFIRM)) {
5665  oc = lookup_option(&dhcpv6_universe, lease->options,
5666  D6O_SERVERID);
5667  if (oc != NULL)
5668  save_option(&dhcpv6_universe, *op, oc);
5669  }
5670 
5671  /* 'send dhcp6.oro foo;' syntax we used in 4.0.0a1/a2 has been
5672  * deprecated by adjustments to the 'request' syntax also used for
5673  * DHCPv4.
5674  */
5675  if (lookup_option(&dhcpv6_universe, *op, D6O_ORO) != NULL)
5676  log_error("'send dhcp6.oro' syntax is deprecated, please "
5677  "use the 'request' syntax (\"man dhclient.conf\").");
5678 
5679  /* Construct and store an ORO (Option Request Option). It is a
5680  * fatal error to fail to send an ORO (of at least zero length).
5681  *
5682  * Discussion: RFC3315 appears to be inconsistent in its statements
5683  * of whether or not the ORO is mandatory. In section 18.1.1
5684  * ("Creation and Transmission of Request Messages"):
5685  *
5686  * The client MUST include an Option Request option (see section
5687  * 22.7) to indicate the options the client is interested in
5688  * receiving. The client MAY include options with data values as
5689  * hints to the server about parameter values the client would like
5690  * to have returned.
5691  *
5692  * This MUST is missing from the creation/transmission of other
5693  * messages (such as Renew and Rebind), and the section 22.7 ("Option
5694  * Request Option" format and definition):
5695  *
5696  * A client MAY include an Option Request option in a Solicit,
5697  * Request, Renew, Rebind, Confirm or Information-request message to
5698  * inform the server about options the client wants the server to
5699  * send to the client. A server MAY include an Option Request
5700  * option in a Reconfigure option to indicate which options the
5701  * client should request from the server.
5702  *
5703  * seems to relax the requirement from MUST to MAY (and still other
5704  * language in RFC3315 supports this).
5705  *
5706  * In lieu of a clarification of RFC3315, we will conform with the
5707  * MUST. Instead of an absent ORO, we will if there are no options
5708  * to request supply an empty ORO. Theoretically, an absent ORO is
5709  * difficult to interpret (does the client want all options or no
5710  * options?). A zero-length ORO is intuitively clear: requesting
5711  * nothing.
5712  */
5713  buffer = NULL;
5714  oro_len = 0;
5715  buflen = 32;
5716  if (!buffer_allocate(&buffer, buflen, MDL))
5717  log_fatal("Out of memory constructing DHCPv6 ORO.");
5718  req = client->config->requested_options;
5719  if (req != NULL) {
5720  for (i = 0 ; req[i] != NULL ; i++) {
5721  if (buflen == oro_len) {
5722  struct buffer *tmpbuf = NULL;
5723 
5724  buflen += 32;
5725 
5726  /* Shell game. */
5727  buffer_reference(&tmpbuf, buffer, MDL);
5728  buffer_dereference(&buffer, MDL);
5729 
5730  if (!buffer_allocate(&buffer, buflen, MDL))
5731  log_fatal("Out of memory resizing "
5732  "DHCPv6 ORO buffer.");
5733 
5734  memcpy(buffer->data, tmpbuf->data, oro_len);
5735 
5736  buffer_dereference(&tmpbuf, MDL);
5737  }
5738 
5739  if (req[i]->universe == &dhcpv6_universe) {
5740  /* Append the code to the ORO. */
5741  putUShort(buffer->data + oro_len,
5742  req[i]->code);
5743  oro_len += 2;
5744  }
5745  }
5746  }
5747 
5748  oc = NULL;
5749  if (make_const_option_cache(&oc, &buffer, NULL, oro_len,
5750  oro_option, MDL)) {
5751  save_option(&dhcpv6_universe, *op, oc);
5752  } else {
5753  log_fatal("Unable to create ORO option cache.");
5754  }
5755 
5756  /*
5757  * Note: make_const_option_cache() consumes the buffer, we do not
5758  * need to dereference it (XXX).
5759  */
5761 }
5762 
5763 /* A clone of the DHCPv4 script_write_params() minus the DHCPv4-specific
5764  * filename, server-name, etc specifics.
5765  *
5766  * Simply, store all values present in all universes of the option state
5767  * (probably derived from a DHCPv6 packet) into environment variables
5768  * named after the option names (and universe names) but with the 'prefix'
5769  * prepended.
5770  *
5771  * Later, dhclient-script may compare for example "new_time_servers" and
5772  * "old_time_servers" for differences, and only upon detecting a change
5773  * bother to rewrite ntp.conf and restart it. Or something along those
5774  * generic lines.
5775  */
5776 static void
5777 script_write_params6(struct client_state *client, const char *prefix,
5778  struct option_state *options)
5779 {
5780  struct envadd_state es;
5781  int i;
5782 
5783  if (options == NULL)
5784  return;
5785 
5786  es.client = client;
5787  es.prefix = prefix;
5788 
5789  for (i = 0 ; i < options->universe_count ; i++) {
5790  option_space_foreach(NULL, NULL, client, NULL, options,
5791  &global_scope, universes[i], &es,
5793  }
5794 }
5795 
5796 /*
5797  * A clone of the DHCPv4 routine.
5798  * Write out the environment variables for the objects that the
5799  * client requested. If the object was requested the variable will be:
5800  * requested_<option_name>=1
5801  * If it wasn't requested there won't be a variable.
5802  */
5803 static void script_write_requested6(client)
5804  struct client_state *client;
5805 {
5806  int i;
5807  struct option **req;
5808  char name[256];
5809  req = client->config->requested_options;
5810 
5811  if (req == NULL)
5812  return;
5813 
5814  for (i = 0 ; req[i] != NULL ; i++) {
5815  if ((req[i]->universe == &dhcpv6_universe) &&
5816  dhcp_option_ev_name (name, sizeof(name), req[i])) {
5817  client_envadd(client, "requested_", name, "%d", 1);
5818  }
5819  }
5820 }
5821 
5822 /*
5823  * Check if there is something not fully defined in the active lease.
5824  */
5825 static isc_boolean_t
5826 active_prefix(struct client_state *client)
5827 {
5828  struct dhc6_lease *lease;
5829  struct dhc6_ia *ia;
5830  struct dhc6_addr *pref;
5831  char zeros[16];
5832 
5833  lease = client->active_lease;
5834  if (lease == NULL)
5835  return ISC_FALSE;
5836  memset(zeros, 0, 16);
5837  for (ia = lease->bindings; ia != NULL; ia = ia->next) {
5838  if (ia->ia_type != D6O_IA_PD)
5839  continue;
5840  for (pref = ia->addrs; pref != NULL; pref = pref->next) {
5841  if (pref->plen == 0)
5842  return ISC_FALSE;
5843  if (pref->address.len != 16)
5844  return ISC_FALSE;
5845  if (memcmp(pref->address.iabuf, zeros, 16) == 0)
5846  return ISC_FALSE;
5847  }
5848  }
5849  return ISC_TRUE;
5850 }
5851 #endif /* DHCPv6 */
int txcount
Definition: dhcpd.h:1309
struct timeval start_time
Definition: dhcpd.h:1307
#define REB_MAX_RT
Definition: dhcp6.h:207
TIME IRT
Definition: dhcpd.h:1313
#define REQ_TIMEOUT
Definition: dhcp6.h:197
TIME RT
Definition: dhcpd.h:1312
const char int line
Definition: dhcpd.h:3723
void start_selecting6(struct client_state *client)
int score
Definition: dhcpd.h:1164
#define D6O_IAADDR
Definition: dhcp6.h:34
u_int8_t plen
Definition: dhcpd.h:1132
struct binding_scope * global_scope
Definition: tree.c:38
#define STATUS_NoBinding
Definition: dhcp6.h:125
#define DHCPV6_RELEASE
Definition: dhcp6.h:145
unsigned char dhcpv6_transaction_id[3]
Definition: dhcpd.h:1167
struct group * on_receipt
Definition: dhcpd.h:1203
unsigned char dhcpv6_transaction_id[3]
Definition: dhcpd.h:1298
Definition: dhcpd.h:556
void save_option(struct universe *universe, struct option_state *options, struct option_cache *oc)
Definition: options.c:2753
unsigned len
Definition: tree.h:80
const char * piaddr(const struct iaddr addr)
Definition: inet.c:579
u_int8_t hlen
Definition: dhcpd.h:489
int do_forward_update
Definition: dhcpd.h:1252
#define D6O_STATUS_CODE
Definition: dhcp6.h:42
u_int32_t renew
Definition: dhcpd.h:1152
unsigned char dhcpv6_transaction_id[3]
Definition: dhcpd.h:414
char name[IFNAMSIZ]
Definition: dhcpd.h:1375
int append_option(struct data_string *dst, struct universe *universe, struct option *option, struct data_string *src)
Definition: options.c:2976
int make_const_option_cache(struct option_cache **oc, struct buffer **buffer, u_int8_t *data, unsigned len, struct option *option, const char *file, int line)
Definition: tree.c:149
#define CNF_MAX_DELAY
Definition: dhcp6.h:200
u_int8_t pref
Definition: dhcpd.h:1165
#define IRT_MINIMUM
Definition: dhcp6.h:278
const char * path_dhclient_db
Definition: dhclient.c:57
#define All_DHCP_Relay_Agents_and_Servers
Definition: dhcp6.h:187
void start_release6(struct client_state *client)
int option_cache_dereference(struct option_cache **ptr, const char *file, int line)
Definition: options.c:2888
int stateless
Definition: dhclient.c:102
void start_info_request6(struct client_state *client)
#define REN_MAX_RT
Definition: dhcp6.h:205
#define SOL_TIMEOUT
Definition: dhcp6.h:195
#define MDL
Definition: omapip.h:568
void cancel_timeout(void(*)(void *) where, void *what)
Definition: dispatch.c:390
#define D6O_PREFERENCE
Definition: dhcp6.h:36
unsigned char iabuf[16]
Definition: inet.h:33
#define print_hex_1(len, data, limit)
Definition: dhcpd.h:2574
Definition: dhcpd.h:1176
#define DHCP_R_INVALIDARG
Definition: result.h:48
#define STATUS_NoAddrsAvail
Definition: dhcp6.h:124
struct group * on_transmission
Definition: dhcpd.h:1208
#define INF_MAX_RT
Definition: dhcp6.h:210
int script_go(struct client_state *client)
Definition: dhclient.c:4611
const char * dhcpv6_type_names[]
Definition: tables.c:656
int int int log_debug(const char *,...) __attribute__((__format__(__printf__
#define DHCPV6_REPLY
Definition: dhcp6.h:144
#define DHCPV6_REQUEST
Definition: dhcp6.h:140
struct client_state * next
Definition: dhcpd.h:1266
u_int16_t elapsed
Definition: dhcpd.h:1308
int option_reference(struct option **dest, struct option *src, const char *file, int line)
Definition: tables.c:980
#define D6O_RAPID_COMMIT
Definition: dhcp6.h:43
struct universe dhcp_universe
int wanted_ia_pd
Definition: dhclient.c:105
#define DHCP_R_FORMERR
Definition: result.h:58
struct option_state * options
Definition: dhcpd.h:1143
int dhcpv4_over_dhcpv6
Definition: discover.c:47
#define D6O_SERVERID
Definition: dhcp6.h:31
#define STATUS_NotOnLink
Definition: dhcp6.h:126
void data_string_forget(struct data_string *data, const char *file, int line)
Definition: alloc.c:1339
struct option_cache * next
Definition: dhcpd.h:387
struct dhc6_ia * next
Definition: dhcpd.h:1147
void delete_option(struct universe *universe, struct option_state *options, int code)
Definition: options.c:2841
int log_error(const char *,...) __attribute__((__format__(__printf__
#define STATUS_UnspecFail
Definition: dhcp6.h:123
#define REQ_MAX_RT
Definition: dhcp6.h:198
void client_envadd(struct client_state *client, const char *prefix, const char *name, const char *fmt,...)
Definition: dhclient.c:4692
void add_timeout(struct timeval *when, void(*)(void *) where, void *what, tvref_t ref, tvunref_t unref)
Definition: dispatch.c:198
#define D6O_INFORMATION_REFRESH_TIME
Definition: dhcp6.h:61
#define STATUS_Success
Definition: dhcp6.h:122
unsigned len
Definition: inet.h:32
void dhcp4o6_start(void)
struct dhc6_ia * bindings
Definition: dhcpd.h:1168
TIME next_MRD
Definition: dhcpd.h:1317
u_int8_t flags
Definition: dhcpd.h:1137
struct expression * expression
Definition: dhcpd.h:388
#define D6O_CLIENTID
Definition: dhcp6.h:30
u_int8_t refresh_type
Definition: dhcpd.h:1299
struct data_string default_duid
Definition: dhclient.c:75
struct option_state * options
Definition: dhcpd.h:449
Definition: tree.h:302
unsigned char dhcpv6_msg_type
Definition: dhcpd.h:411
unsigned char iaid[4]
Definition: dhcpd.h:1148
TIME MRT
Definition: dhcpd.h:1315
void log_fatal(const char *,...) __attribute__((__format__(__printf__
void(* v6_handler)(struct packet *, struct client_state *)
Definition: dhcpd.h:1324
#define D6O_IA_TA
Definition: dhcp6.h:33
int parse_option_buffer(struct option_state *options, const unsigned char *buffer, unsigned length, struct universe *universe)
Definition: options.c:117
int buffer_reference(struct buffer **ptr, struct buffer *bp, const char *file, int line)
Definition: alloc.c:698
#define DHCP_R_BADPARSE
Definition: result.h:53
int option_cache_allocate(struct option_cache **cptr, const char *file, int line)
Definition: alloc.c:630
int option_state_reference(struct option_state **ptr, struct option_state *bp, const char *file, int line)
Definition: alloc.c:883
#define D6O_UNICAST
Definition: dhcp6.h:41
struct option_state * options
Definition: dhcpd.h:1170
#define REL_MAX_RC
Definition: dhcp6.h:212
#define D6O_IAPREFIX
Definition: dhcp6.h:55
void execute_statements_in_scope(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 group *group, struct group *limiting_group, struct on_star *on_star)
Definition: execute.c:562
char * name
Definition: dhcpd.h:1268
int option_state_allocate(struct option_state **ptr, const char *file, int line)
Definition: alloc.c:846
const char * path_dhclient_pid
Definition: dhclient.c:58
void client_option_envadd(struct option_cache *oc, 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 universe *u, void *stuff)
Definition: dhclient.c:4431
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:2699
#define STATUS_NoPrefixAvail
Definition: dhcp6.h:128
Definition: tree.h:346
void dhc6_lease_destroy(struct dhc6_lease **src, const char *file, int line)
struct option ** requested_options
Definition: dhcpd.h:1211
int require_all_ias
Definition: dhclient.c:106
void script_init(struct client_state *client, const char *reason, struct string_list *medium)
Definition: dhclient.c:4400
#define DHCPV6_RENEW
Definition: dhcp6.h:142
#define CNF_MAX_RD
Definition: dhcp6.h:203
#define REN_TIMEOUT
Definition: dhcp6.h:204
int buffer_allocate(struct buffer **ptr, unsigned len, const char *file, int line)
Definition: alloc.c:679
struct data_string server_id
Definition: dhcpd.h:1161
unsigned code
Definition: tree.h:350
ssize_t send_packet6(struct interface_info *, const unsigned char *, size_t, struct sockaddr_in6 *)
void putULong(unsigned char *, u_int32_t)
Definition: convert.c:70
const char * prefix
Definition: dhcpd.h:1337
#define DHCPV6_REBIND
Definition: dhcp6.h:143
u_int16_t local_port
Definition: dhclient.c:91
Definition: dhcpd.h:405
#define SOL_MAX_DELAY
Definition: dhcp6.h:194
#define cur_time
Definition: dhcpd.h:2076
Definition: ip.h:47
struct dhc6_lease * advertised_leases
Definition: dhcpd.h:1303
u_int32_t getUShort(const unsigned char *)
void start_confirm6(struct client_state *client)
void dfree(void *, const char *, int)
Definition: alloc.c:131
u_int32_t max_life
Definition: dhcpd.h:1141
const char * name
Definition: tree.h:347
struct option_state * sent_options
Definition: dhcpd.h:1274
struct hardware hw_address
Definition: dhcpd.h:1353
struct option_cache * lookup_option(struct universe *universe, struct option_state *options, unsigned code)
Definition: options.c:2438
struct client_state * client
Definition: dhcpd.h:1336
struct option_state * options
Definition: dhcpd.h:1156
struct option * option
Definition: dhcpd.h:389
struct dhc6_lease * selected_lease
Definition: dhcpd.h:1304
#define _PATH_DHCLIENT6_DB
Definition: config.h:241
int int log_info(const char *,...) __attribute__((__format__(__printf__
enum dhcp_state state
Definition: dhcpd.h:1275
void * dmalloc(size_t, const char *, int)
Definition: alloc.c:56
struct interface_info * interfaces
Definition: discover.c:42
u_int32_t getULong(const unsigned char *)
#define INF_TIMEOUT
Definition: dhcp6.h:209
u_int32_t rebind
Definition: dhcpd.h:1153
struct option ** required_options
Definition: dhcpd.h:1210
struct dhc6_addr * addrs
Definition: dhcpd.h:1154
void putUChar(unsigned char *, u_int32_t)
Definition: convert.c:102
#define _PATH_DHCLIENT6_PID
Definition: config.h:244
struct universe ** universes
Definition: tables.c:963
Definition: inet.h:31
#define DEC_MAX_RC
Definition: dhcp6.h:214
u_int32_t getUChar(const unsigned char *)
u_int32_t preferred_life
Definition: dhcpd.h:1140
int option_state_dereference(struct option_state **ptr, const char *file, int line)
Definition: alloc.c:911
void start_init6(struct client_state *client)
void option_space_foreach(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 universe *u, void *stuff, void(*func)(struct option_cache *, struct packet *, struct lease *, struct client_state *, struct option_state *, struct option_state *, struct binding_scope **, struct universe *, void *))
Definition: options.c:3722
struct dhc6_addr * next
Definition: dhcpd.h:1130
struct timeval cur_tv
Definition: dispatch.c:35
void unconfigure6(struct client_state *client, const char *reason)
void client_dns_remove(struct client_state *client, struct iaddr *addr)
struct universe dhcpv6_universe
Definition: tables.c:343
#define D6O_IA_NA
Definition: dhcp6.h:32
#define DHCLIENT_DEFAULT_PREFIX_LEN
Definition: site.h:282
int make_const_data(struct expression **expr, const unsigned char *data, unsigned len, int terminated, int allocate, const char *file, int line)
Definition: tree.c:219
#define DHCPV6_ADVERTISE
Definition: dhcp6.h:139
int onetry
Definition: dhclient.c:99
int universe_count
Definition: dhcpd.h:398
time_t TIME
Definition: dhcpd.h:85
#define D6O_ORO
Definition: dhcp6.h:35
#define REL_TIMEOUT
Definition: dhcp6.h:211
unsigned char data[1]
Definition: tree.h:63
Definition: tree.h:61
#define REB_TIMEOUT
Definition: dhcp6.h:206
struct iaddr address
Definition: dhcpd.h:1131
int(* encapsulate)(struct data_string *, struct packet *, struct lease *, struct client_state *, struct option_state *, struct option_state *, struct binding_scope **, struct universe *)
Definition: tree.h:326
#define DEC_TIMEOUT
Definition: dhcp6.h:213
void dhcpv6_client_assignments(void)
u_int8_t hbuf[HARDWARE_ADDR_LEN+1]
Definition: dhcpd.h:490
int wanted_ia_na
Definition: dhclient.c:103
struct client_config * config
Definition: dhcpd.h:1271
int wanted_ia_ta
Definition: dhclient.c:104
#define MAX_TIME
Definition: dhcpd.h:1596
#define CNF_MAX_RT
Definition: dhcp6.h:202
#define DHCPV6_CONFIRM
Definition: dhcp6.h:141
void dhclient_schedule_updates(struct client_state *client, struct iaddr *addr, int offset)
int dhcp_option_ev_name(char *buf, size_t buflen, struct option *option)
Definition: dhclient.c:4725
#define SOL_MAX_RT
Definition: dhcp6.h:196
#define D6O_IA_PD
Definition: dhcp6.h:54
void go_daemon()
Definition: dhclient.c:4765
struct dhc6_lease * next
Definition: dhcpd.h:1160
option_code_hash_t * code_hash
Definition: tree.h:338
int nowait
Definition: dhclient.c:101
u_int16_t remote_port
Definition: dhclient.c:92
TIME timeout
Definition: dhcpd.h:1213
const char * file
Definition: dhcpd.h:3723
#define IRT_DEFAULT
Definition: dhcp6.h:277
void putUShort(unsigned char *, u_int32_t)
Definition: convert.c:86
#define INF_MAX_DELAY
Definition: dhcp6.h:208
const unsigned char * data
Definition: tree.h:79
struct interface_info * interface
Definition: dhcpd.h:1267
#define DHC6_ADDR_EXPIRED
Definition: dhcpd.h:1136
u_int16_t ia_type
Definition: dhcpd.h:1149
void data_string_copy(struct data_string *dest, const struct data_string *src, const char *file, int line)
Definition: alloc.c:1323
isc_boolean_t released
Definition: dhcpd.h:1163
#define CNF_TIMEOUT
Definition: dhcp6.h:201
#define DHCPV6_INFORMATION_REQUEST
Definition: dhcp6.h:148
#define DHCPV6_DECLINE
Definition: dhcp6.h:146
struct dhc6_lease * old_lease
Definition: dhcpd.h:1302
u_int32_t requested_lease
Definition: dhcpd.h:1233
#define REQ_MAX_RC
Definition: dhcp6.h:199
#define DHC6_ADDR_DEPREFFED
Definition: dhcpd.h:1135
TIME MRD
Definition: dhcpd.h:1316
TIME starts
Definition: dhcpd.h:1151
#define DHCPV6_SOLICIT
Definition: dhcp6.h:138
struct dhc6_lease * active_lease
Definition: dhcpd.h:1301
TIME starts
Definition: dhcpd.h:1139
struct buffer * buffer
Definition: tree.h:78
TIME MRC
Definition: dhcpd.h:1314
int buffer_dereference(struct buffer **ptr, const char *file, int line)
Definition: alloc.c:726
#define STATUS_UseMulticast
Definition: dhcp6.h:127
isc_result_t write_client6_lease(struct client_state *client, struct dhc6_lease *lease, int rewrite, int sync)
Definition: dhclient.c:4102
#define D6O_ELAPSED_TIME
Definition: dhcp6.h:37