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