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