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