ISC DHCP  4.3.6
A reference DHCPv4 and DHCPv6 implementation
dhclient.c
Go to the documentation of this file.
1 /* dhclient.c
2 
3  DHCP Client. */
4 
5 /*
6  * Copyright (c) 2004-2017 by Internet Systems Consortium, Inc. ("ISC")
7  * Copyright (c) 1995-2003 by Internet Software Consortium
8  *
9  * Permission to use, copy, modify, and distribute this software for any
10  * purpose with or without fee is hereby granted, provided that the above
11  * copyright notice and this permission notice appear in all copies.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
14  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
16  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
19  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20  *
21  * Internet Systems Consortium, Inc.
22  * 950 Charter Street
23  * Redwood City, CA 94063
24  * <info@isc.org>
25  * https://www.isc.org/
26  *
27  * This code is based on the original client state machine that was
28  * written by Elliot Poger. The code has been extensively hacked on
29  * by Ted Lemon since then, so any mistakes you find are probably his
30  * fault and not Elliot's.
31  */
32 
33 #include "dhcpd.h"
34 #include <syslog.h>
35 #include <signal.h>
36 #include <errno.h>
37 #include <sys/time.h>
38 #include <sys/wait.h>
39 #include <limits.h>
40 #include <isc/file.h>
41 #include <dns/result.h>
42 
43 #ifdef HAVE_LIBCAP_NG
44 #include <cap-ng.h>
45 #endif
46 
47 /*
48  * Defined in stdio.h when _GNU_SOURCE is set, but we don't want to define
49  * that when building ISC code.
50  */
51 extern int asprintf(char **strp, const char *fmt, ...);
52 
53 TIME default_lease_time = 43200; /* 12 hours... */
54 TIME max_lease_time = 86400; /* 24 hours... */
55 
57 const char *path_dhclient_db = NULL;
58 const char *path_dhclient_pid = NULL;
59 static char path_dhclient_script_array[] = _PATH_DHCLIENT_SCRIPT;
60 char *path_dhclient_script = path_dhclient_script_array;
61 const char *path_dhclient_duid = NULL;
62 
63 /* False (default) => we write and use a pid file */
64 isc_boolean_t no_pid_file = ISC_FALSE;
65 
67 
69 
70 struct iaddr iaddr_broadcast = { 4, { 255, 255, 255, 255 } };
71 struct iaddr iaddr_any = { 4, { 0, 0, 0, 0 } };
72 struct in_addr inaddr_any;
73 struct sockaddr_in sockaddr_broadcast;
74 struct in_addr giaddr;
76 int duid_type = 0;
77 int duid_v4 = 1;
78 int std_dhcid = 0;
79 
80 /* ASSERT_STATE() does nothing now; it used to be
81  assert (state_is == state_shouldbe). */
82 #define ASSERT_STATE(state_is, state_shouldbe) {}
83 
84 #ifndef UNIT_TEST
85 static const char copyright[] = "Copyright 2004-2017 Internet Systems Consortium.";
86 static const char arr [] = "All rights reserved.";
87 static const char message [] = "Internet Systems Consortium DHCP Client";
88 static const char url [] = "For info, please visit https://www.isc.org/software/dhcp/";
89 #endif /* UNIT_TEST */
90 
91 u_int16_t local_port = 0;
92 u_int16_t remote_port = 0;
93 #if defined(DHCPv6) && defined(DHCP4o6)
94 int dhcp4o6_state = -1; /* -1 = stopped, 0 = polling, 1 = started */
95 #endif
96 int no_daemon = 0;
97 struct string_list *client_env = NULL;
99 int onetry = 0;
100 int quiet = 1;
101 int nowait = 0;
102 int stateless = 0;
103 int wanted_ia_na = -1; /* the absolute value is the real one. */
104 int wanted_ia_ta = 0;
105 int wanted_ia_pd = 0;
106 int require_all_ias = 0; /* If the user requires all of the IAs to
107  be available before accepting a lease
108  0 = no, 1 = requries */
110 char *mockup_relay = NULL;
111 
112 char *progname = NULL;
113 
115 
116 extern struct option *default_requested_options[];
117 
118 void run_stateless(int exit_mode, u_int16_t port);
119 
120 static isc_result_t write_duid(struct data_string *duid);
121 static void add_reject(struct packet *packet);
122 
123 static int check_domain_name(const char *ptr, size_t len, int dots);
124 static int check_domain_name_list(const char *ptr, size_t len, int dots);
125 static int check_option_values(struct universe *universe, unsigned int opt,
126  const char *ptr, size_t len);
127 
128 static void dhclient_ddns_cb_free(dhcp_ddns_cb_t *ddns_cb,
129  char* file, int line);
130 
147 #if defined(DHCPv6) && defined(DHCP4o6)
148 static void dhcp4o6_poll(void *dummy);
149 static void dhcp4o6_resume(void);
150 static void recv_dhcpv4_response(struct data_string *raw);
151 static int send_dhcpv4_query(struct client_state *client, int broadcast);
152 
153 static void dhcp4o6_stop(void);
154 static void forw_dhcpv4_response(struct packet *packet);
155 static void forw_dhcpv4_query(struct data_string *raw);
156 #endif
157 
158 #ifndef UNIT_TEST
159 /* These are only used when we call usage() from the main routine
160  * which isn't compiled when building for unit tests
161  */
162 static const char use_noarg[] = "No argument for command: %s";
163 #ifdef DHCPv6
164 static const char use_v6command[] = "Command not used for DHCPv4: %s";
165 #endif
166 
167 static void setup_ib_interface(struct interface_info *ip);
168 
169 static void
170 usage(const char *sfmt, const char *sarg)
171 {
172  log_info("%s %s", message, PACKAGE_VERSION);
173  log_info(copyright);
174  log_info(arr);
175  log_info(url);
176 
177  /* If desired print out the specific error message */
178 #ifdef PRINT_SPECIFIC_CL_ERRORS
179  if (sfmt != NULL)
180  log_error(sfmt, sarg);
181 #endif
182 
183  log_fatal("Usage: %s "
184 #ifdef DHCPv6
185 #ifdef DHCP4o6
186  "[-4|-6] [-SNTPRI1dvrxi] [-nw] -4o6 <port>] [-p <port>]\n"
187  " [-D LL|LLT] [--dad-wait-time seconds]\n"
188 #else /* DHCP4o6 */
189  "[-4|-6] [-SNTPRI1dvrxi] [-nw] [-p <port>]\n"
190  " [-D LL|LLT] [--dad-wait-time seconds]\n"
191 #endif
192 #else /* DHCPv6 */
193  "[-I1dvrxi] [-nw] [-p <port>] [-D LL|LLT] \n"
194 #endif /* DHCPv6 */
195  " [-s server-addr] [-cf config-file]\n"
196  " [-df duid-file] [-lf lease-file]\n"
197  " [-pf pid-file] [--no-pid] [-e VAR=val]\n"
198  " [-sf script-file] [interface]*\n"
199  " [-C <dhcp-client-identifier>] [-B]\n"
200  " [-H <host-name> | -F <fqdn.fqdn>] [--timeout <timeout>]\n"
201  " [-V <vendor-class-identifier>]\n"
202  " [--request-options <request option list>]",
203  isc_file_basename(progname));
204 }
205 
206 int
207 main(int argc, char **argv) {
208  int fd;
209  int i;
210  struct interface_info *ip;
211  struct client_state *client;
212  unsigned seed;
213  char *server = NULL;
214  isc_result_t status;
215  int exit_mode = 0;
216  int release_mode = 0;
217  struct timeval tv;
218  omapi_object_t *listener;
219  isc_result_t result;
220  int persist = 0;
221  int no_dhclient_conf = 0;
222  int no_dhclient_db = 0;
223  int no_dhclient_pid = 0;
224  int no_dhclient_script = 0;
225 #ifdef DHCPv6
226  int local_family_set = 0;
227 #ifdef DHCP4o6
228  u_int16_t dhcp4o6_port = 0;
229 #endif /* DHCP4o6 */
230 #endif /* DHCPv6 */
231  char *s;
232 
233 #ifdef OLD_LOG_NAME
234  progname = "dhclient";
235 #else
236  progname = argv[0];
237 #endif
238 
239  char *dhcp_client_identifier_arg = NULL;
240  char *dhcp_host_name_arg = NULL;
241  char *dhcp_fqdn_arg = NULL;
242  char *dhcp_vendor_class_identifier_arg = NULL;
243  char *dhclient_request_options = NULL;
244 
245  int timeout_arg = 0;
246  char *arg_conf = NULL;
247  int arg_conf_len = 0;
248 #ifdef HAVE_LIBCAP_NG
249  int keep_capabilities = 0;
250 #endif
251 
252  /* Initialize client globals. */
253  memset(&default_duid, 0, sizeof(default_duid));
254 
255  /* Make sure that file descriptors 0 (stdin), 1, (stdout), and
256  2 (stderr) are open. To do this, we assume that when we
257  open a file the lowest available file descriptor is used. */
258  fd = open("/dev/null", O_RDWR | O_CLOEXEC);
259  if (fd == 0)
260  fd = open("/dev/null", O_RDWR | O_CLOEXEC);
261  if (fd == 1)
262  fd = open("/dev/null", O_RDWR | O_CLOEXEC);
263  if (fd == 2)
264  log_perror = 0; /* No sense logging to /dev/null. */
265  else if (fd != -1)
266  close(fd);
267 
268  openlog(isc_file_basename(progname), DHCP_LOG_OPTIONS, LOG_DAEMON);
269 
270 #if !(defined(DEBUG) || defined(__CYGWIN32__))
271  setlogmask(LOG_UPTO(LOG_INFO));
272 #endif
273 
274  /* Set up the isc and dns library managers */
276  | DHCP_DNS_CLIENT_LAZY_INIT, NULL, NULL);
277  if (status != ISC_R_SUCCESS)
278  log_fatal("Can't initialize context: %s",
279  isc_result_totext(status));
280 
281  /* Set up the OMAPI. */
282  status = omapi_init();
283  if (status != ISC_R_SUCCESS)
284  log_fatal("Can't initialize OMAPI: %s",
285  isc_result_totext(status));
286 
287  /* Set up the OMAPI wrappers for various server database internal
288  objects. */
290 
294 
295  for (i = 1; i < argc; i++) {
296  if (!strcmp(argv[i], "-r")) {
297  release_mode = 1;
298  no_daemon = 1;
299 #ifdef DHCPv6
300  } else if (!strcmp(argv[i], "-4")) {
301  if (local_family_set && local_family != AF_INET)
302  log_fatal("Client can only do v4 or v6, not "
303  "both.");
304  local_family_set = 1;
305  local_family = AF_INET;
306  } else if (!strcmp(argv[i], "-6")) {
307  if (local_family_set && local_family != AF_INET6)
308  log_fatal("Client can only do v4 or v6, not "
309  "both.");
310  local_family_set = 1;
311  local_family = AF_INET6;
312 #ifdef DHCP4o6
313  } else if (!strcmp(argv[i], "-4o6")) {
314  if (++i == argc)
315  usage(use_noarg, argv[i-1]);
316  dhcp4o6_port = validate_port_pair(argv[i]);
317 
318  log_debug("DHCPv4 over DHCPv6 over ::1 port %d and %d",
319  ntohs(dhcp4o6_port),
320  ntohs(dhcp4o6_port) + 1);
321  dhcpv4_over_dhcpv6 = 1;
322 #endif /* DHCP4o6 */
323 #endif /* DHCPv6 */
324  } else if (!strcmp(argv[i], "-x")) { /* eXit, no release */
325  release_mode = 0;
326  no_daemon = 0;
327  exit_mode = 1;
328  } else if (!strcmp(argv[i], "-p")) {
329  if (++i == argc)
330  usage(use_noarg, argv[i-1]);
331  local_port = validate_port(argv[i]);
332  log_debug("binding to user-specified port %d",
333  ntohs(local_port));
334  } else if (!strcmp(argv[i], "-d")) {
335  no_daemon = 1;
336  quiet = 0;
337  } else if (!strcmp(argv[i], "-pf")) {
338  if (++i == argc)
339  usage(use_noarg, argv[i-1]);
340  path_dhclient_pid = argv[i];
341  no_dhclient_pid = 1;
342  } else if (!strcmp(argv[i], "--no-pid")) {
343  no_pid_file = ISC_TRUE;
344  } else if (!strcmp(argv[i], "-cf")) {
345  if (++i == argc)
346  usage(use_noarg, argv[i-1]);
347  path_dhclient_conf = argv[i];
348  no_dhclient_conf = 1;
349  } else if (!strcmp(argv[i], "-df")) {
350  if (++i == argc)
351  usage(use_noarg, argv[i-1]);
352  path_dhclient_duid = argv[i];
353  } else if (!strcmp(argv[i], "-lf")) {
354  if (++i == argc)
355  usage(use_noarg, argv[i-1]);
356  path_dhclient_db = argv[i];
357  no_dhclient_db = 1;
358  } else if (!strcmp(argv[i], "-sf")) {
359  if (++i == argc)
360  usage(use_noarg, argv[i-1]);
361  path_dhclient_script = argv[i];
362  no_dhclient_script = 1;
363  } else if (!strcmp(argv[i], "-1")) {
364  onetry = 1;
365  } else if (!strcmp(argv[i], "-q")) {
366  quiet = 1;
367  } else if (!strcmp(argv[i], "-s")) {
368  if (++i == argc)
369  usage(use_noarg, argv[i-1]);
370  server = argv[i];
371  } else if (!strcmp(argv[i], "-g")) {
372  if (++i == argc)
373  usage(use_noarg, argv[i-1]);
374  mockup_relay = argv[i];
375  } else if (!strcmp(argv[i], "-nw")) {
376  nowait = 1;
377  } else if (!strcmp(argv[i], "-n")) {
378  /* do not start up any interfaces */
380  } else if (!strcmp(argv[i], "-w")) {
381  /* do not exit if there are no broadcast interfaces. */
382  persist = 1;
383  } else if (!strcmp(argv[i], "-e")) {
384  struct string_list *tmp;
385  if (++i == argc)
386  usage(use_noarg, argv[i-1]);
387  tmp = dmalloc(strlen(argv[i]) + sizeof *tmp, MDL);
388  if (!tmp)
389  log_fatal("No memory for %s", argv[i]);
390  strcpy(tmp->string, argv[i]);
391  tmp->next = client_env;
392  client_env = tmp;
394 #ifdef DHCPv6
395  } else if (!strcmp(argv[i], "-S")) {
396  if (local_family_set && (local_family == AF_INET)) {
397  usage(use_v6command, argv[i]);
398  }
399  local_family_set = 1;
400  local_family = AF_INET6;
401  wanted_ia_na = 0;
402  stateless = 1;
403  } else if (!strcmp(argv[i], "-N")) {
404  if (local_family_set && (local_family == AF_INET)) {
405  usage(use_v6command, argv[i]);
406  }
407  local_family_set = 1;
408  local_family = AF_INET6;
409  if (wanted_ia_na < 0) {
410  wanted_ia_na = 0;
411  }
412  wanted_ia_na++;
413  } else if (!strcmp(argv[i], "-T")) {
414  if (local_family_set && (local_family == AF_INET)) {
415  usage(use_v6command, argv[i]);
416  }
417  local_family_set = 1;
418  local_family = AF_INET6;
419  if (wanted_ia_na < 0) {
420  wanted_ia_na = 0;
421  }
422  wanted_ia_ta++;
423  } else if (!strcmp(argv[i], "-P")) {
424  if (local_family_set && (local_family == AF_INET)) {
425  usage(use_v6command, argv[i]);
426  }
427  local_family_set = 1;
428  local_family = AF_INET6;
429  if (wanted_ia_na < 0) {
430  wanted_ia_na = 0;
431  }
432  wanted_ia_pd++;
433  } else if (!strcmp(argv[i], "-R")) {
434  if (local_family_set && (local_family == AF_INET)) {
435  usage(use_v6command, argv[i]);
436  }
437  local_family_set = 1;
438  local_family = AF_INET6;
439  require_all_ias = 1;
440  } else if (!strcmp(argv[i], "--dad-wait-time")) {
441  if (++i == argc) {
442  usage(use_noarg, argv[i-1]);
443  }
444  dad_wait_time = (int)strtol(argv[i], &s, 10);
445  if (errno || (*s != '\0') || (dad_wait_time < 0)) {
446  usage("Invalid value for --dad-wait-time: %s", argv[i]);
447  }
448 
449 #endif /* DHCPv6 */
450  } else if (!strcmp(argv[i], "-D")) {
451  duid_v4 = 1;
452  if (++i == argc)
453  usage(use_noarg, argv[i-1]);
454  if (!strcasecmp(argv[i], "LL")) {
455  duid_type = DUID_LL;
456  } else if (!strcasecmp(argv[i], "LLT")) {
458  } else {
459  usage("Unknown argument to -D: %s", argv[i]);
460  }
461  } else if (!strcmp(argv[i], "-i")) {
462  /* enable DUID support for DHCPv4 clients */
463  duid_v4 = 1;
464  } else if (!strcmp(argv[i], "-I")) {
465  /* enable standard DHCID support for DDNS updates */
466  std_dhcid = 1;
467  } else if (!strcmp(argv[i], "-v")) {
468  quiet = 0;
469  } else if (!strcmp(argv[i], "--version")) {
470  const char vstring[] = "isc-dhclient-";
471  IGNORE_RET(write(STDERR_FILENO, vstring,
472  strlen(vstring)));
475  strlen(PACKAGE_VERSION)));
476  IGNORE_RET(write(STDERR_FILENO, "\n", 1));
477  exit(0);
478  } else if (!strcmp(argv[i], "-C")) {
479  if ((++i == argc) || (argv[i] == NULL) || (*(argv[i])=='\0')) {
480  usage(use_noarg, argv[i-1]);
481  exit(1);
482  }
483 
484  if (strlen(argv[i]) >= DHCP_MAX_OPTION_LEN) {
485  log_error("-C option dhcp-client-identifier string \"%s\" is too long - maximum length is: %d", argv[i], DHCP_MAX_OPTION_LEN-1);
486  exit(1);
487  }
488 
489  dhcp_client_identifier_arg = argv[i];
490  } else if (!strcmp(argv[i], "-B")) {
492  } else if (!strcmp(argv[i], "-H")) {
493  if ((++i == argc) || (argv[i] == NULL) || (*(argv[i])=='\0')) {
494  usage(use_noarg, argv[i-1]);
495  exit(1);
496  }
497 
498  if (strlen(argv[i]) >= DHCP_MAX_OPTION_LEN) {
499  log_error("-H option host-name string \"%s\" is too long - maximum length is: %d", argv[i], DHCP_MAX_OPTION_LEN-1);
500  exit(1);
501  }
502 
503  if (dhcp_host_name_arg != NULL) {
504  log_error("The -H <host-name> and -F <fqdn> arguments are mutually exclusive");
505  exit(1);
506  }
507 
508  dhcp_host_name_arg = argv[i];
509  } else if (!strcmp(argv[i], "-F")) {
510  if ((++i == argc) || (argv[i] == NULL) || (*(argv[i])=='\0')) {
511  usage(use_noarg, argv[i-1]);
512  exit(1);
513  }
514 
515  if (strlen(argv[i]) >= DHCP_MAX_OPTION_LEN) {
516  log_error("-F option fqdn.fqdn string \"%s\" is too long - maximum length is: %d", argv[i], DHCP_MAX_OPTION_LEN-1);
517  exit(1);
518  }
519 
520  if (dhcp_fqdn_arg != NULL) {
521  log_error("Only one -F <fqdn> argument can be specified");
522  exit(1);
523  }
524 
525  if (dhcp_host_name_arg != NULL) {
526  log_error("The -F <fqdn> and -H <host-name> arguments are mutually exclusive");
527  exit(1);
528  }
529 
530  dhcp_fqdn_arg = argv[i];
531  } else if (!strcmp(argv[i], "--timeout")) {
532  if ((++i == argc) || (argv[i] == NULL) || (*(argv[i])=='\0')) {
533  usage(use_noarg, argv[i-1]);
534  exit(1);
535  }
536 
537  if ((timeout_arg = atoi(argv[i])) <= 0) {
538  log_error("timeout option must be > 0 - bad value: %s",argv[i]);
539  exit(1);
540  }
541  } else if (!strcmp(argv[i], "-V")) {
542  if ((++i == argc) || (argv[i] == NULL) || (*(argv[i])=='\0')) {
543  usage(use_noarg, argv[i-1]);
544  exit(1);
545  }
546 
547  if (strlen(argv[i]) >= DHCP_MAX_OPTION_LEN) {
548  log_error("-V option vendor-class-identifier string \"%s\" is too long - maximum length is: %d", argv[i], DHCP_MAX_OPTION_LEN-1);
549  exit(1);
550  }
551 
552  dhcp_vendor_class_identifier_arg = argv[i];
553  } else if (!strcmp(argv[i], "--request-options")) {
554  if ((++i == argc) || (argv[i] == NULL) || (*(argv[i])=='\0')) {
555  usage(use_noarg, argv[i-1]);
556  exit(1);
557  }
558 
559  dhclient_request_options = argv[i];
560  } else if (!strcmp(argv[i], "-nc")) {
561 #ifdef HAVE_LIBCAP_NG
562  keep_capabilities = 1;
563 #endif
564  } else if (argv[i][0] == '-') {
565  usage("Unknown command: %s", argv[i]);
566  } else if (interfaces_requested < 0) {
567  usage("No interfaces comamnd -n and "
568  " requested interface %s", argv[i]);
569  } else {
570  struct interface_info *tmp = NULL;
571 
572  status = interface_allocate(&tmp, MDL);
573  if (status != ISC_R_SUCCESS)
574  log_fatal("Can't record interface %s:%s",
575  argv[i], isc_result_totext(status));
576  if (strlen(argv[i]) >= sizeof(tmp->name))
577  log_fatal("%s: interface name too long (is %ld)",
578  argv[i], (long)strlen(argv[i]));
579  strcpy(tmp->name, argv[i]);
580  if (interfaces) {
581  interface_reference(&tmp->next,
582  interfaces, MDL);
583  interface_dereference(&interfaces, MDL);
584  }
585  interface_reference(&interfaces, tmp, MDL);
586  tmp->flags = INTERFACE_REQUESTED;
588  }
589  }
590 
591  if (wanted_ia_na < 0) {
592  wanted_ia_na = 1;
593  }
594 
595  /* Support only one (requested) interface for Prefix Delegation. */
596  if (wanted_ia_pd && (interfaces_requested != 1)) {
597  usage("PD %s only supports one requested interface", "-P");
598  }
599 
600 #if defined(DHCPv6) && defined(DHCP4o6)
601  if ((local_family == AF_INET6) && dhcpv4_over_dhcpv6 &&
602  (exit_mode || release_mode))
603  log_error("Can't relay DHCPv4-over-DHCPv6 "
604  "without a persistent DHCPv6 client");
605  if ((local_family == AF_INET) && dhcpv4_over_dhcpv6 &&
606  (interfaces_requested != 1))
607  log_fatal("DHCPv4-over-DHCPv6 requires an explicit "
608  "interface on which to be applied");
609 #endif
610 
611  if (!no_dhclient_conf && (s = getenv("PATH_DHCLIENT_CONF"))) {
612  path_dhclient_conf = s;
613  }
614  if (!no_dhclient_db && (s = getenv("PATH_DHCLIENT_DB"))) {
615  path_dhclient_db = s;
616  }
617  if (!no_dhclient_pid && (s = getenv("PATH_DHCLIENT_PID"))) {
618  path_dhclient_pid = s;
619  }
620  if (!no_dhclient_script && (s = getenv("PATH_DHCLIENT_SCRIPT"))) {
622  }
623 
624 #ifdef HAVE_LIBCAP_NG
625  /* Drop capabilities */
626  if (!keep_capabilities) {
627  capng_clear(CAPNG_SELECT_CAPS);
628  capng_update(CAPNG_ADD, CAPNG_EFFECTIVE|CAPNG_PERMITTED,
629  CAP_DAC_OVERRIDE); // Drop this someday
630  capng_updatev(CAPNG_ADD, CAPNG_EFFECTIVE|CAPNG_PERMITTED,
631  CAP_NET_ADMIN, CAP_NET_RAW,
632  CAP_NET_BIND_SERVICE, CAP_SYS_ADMIN, -1);
633  capng_apply(CAPNG_SELECT_CAPS);
634  }
635 #endif
636 
637  /* Set up the initial dhcp option universe. */
639 
640  /* Assign v4 or v6 specific running parameters. */
641  if (local_family == AF_INET)
643 #ifdef DHCPv6
644  else if (local_family == AF_INET6)
646 #endif /* DHCPv6 */
647  else
648  log_fatal("Impossible condition at %s:%d.", MDL);
649 
650  /*
651  * convert relative path names to absolute, for files that need
652  * to be reopened after chdir() has been called
653  */
654  if (path_dhclient_db[0] != '/') {
655  const char *old_path = path_dhclient_db;
656  path_dhclient_db = realpath(path_dhclient_db, NULL);
657  if (path_dhclient_db == NULL)
658  log_fatal("Failed to get realpath for %s: %s", old_path, strerror(errno));
659  }
660 
661  if (path_dhclient_script[0] != '/') {
662  const char *old_path = path_dhclient_script;
663  path_dhclient_script = realpath(path_dhclient_script, NULL);
664  if (path_dhclient_script == NULL)
665  log_fatal("Failed to get realpath for %s: %s", old_path, strerror(errno));
666  }
667 
668  /*
669  * See if we should kill off any currently running client
670  * we don't try to kill it off if the user told us not
671  * to write a pid file - we assume they are controlling
672  * the process in some other fashion.
673  */
674  if ((release_mode || exit_mode) && (no_pid_file == ISC_FALSE)) {
675  FILE *pidfd;
676  pid_t oldpid;
677  long temp;
678  int e;
679 
680  if ((pidfd = fopen(path_dhclient_pid, "re")) != NULL) {
681  e = fscanf(pidfd, "%ld\n", &temp);
682  oldpid = (pid_t)temp;
683 
684  if (e != 0 && e != EOF && oldpid) {
685  if (kill(oldpid, SIGTERM) == 0) {
686  log_info("Killed old client process");
687  (void) unlink(path_dhclient_pid);
688  /*
689  * wait for the old process to
690  * cleanly terminate.
691  * Note kill() with sig=0 could
692  * detect termination but only
693  * the parent can be signaled...
694  */
695  sleep(1);
696  } else if (errno == ESRCH) {
697  log_info("Removed stale PID file");
698  (void) unlink(path_dhclient_pid);
699  }
700  }
701  fclose(pidfd);
702  } else {
703  /* handle release for interfaces requested with Red Hat
704  * /sbin/ifup - pidfile will be /var/run/dhclient-$interface.pid
705  */
706 
707  if ((path_dhclient_pid == NULL) || (*path_dhclient_pid == '\0'))
708  path_dhclient_pid = "/var/run/dhclient.pid";
709 
710  char *new_path_dhclient_pid;
711  struct interface_info *ip;
712  int pdp_len = strlen(path_dhclient_pid), pfx, dpfx;
713 
714  /* find append point: beginning of any trailing '.pid'
715  * or '-$IF.pid' */
716  for (pfx=pdp_len; (pfx >= 0) && (path_dhclient_pid[pfx] != '.') && (path_dhclient_pid[pfx] != '/'); pfx--);
717  if (pfx == -1)
718  pfx = pdp_len;
719 
720  if (path_dhclient_pid[pfx] == '/')
721  pfx += 1;
722 
723  for (dpfx=pfx; (dpfx >= 0) && (path_dhclient_pid[dpfx] != '-') && (path_dhclient_pid[dpfx] != '/'); dpfx--);
724  if ((dpfx > -1) && (path_dhclient_pid[dpfx] != '/'))
725  pfx = dpfx;
726 
727  for (ip = interfaces; ip; ip = ip->next) {
729  int n_len = strlen(ip->name);
730 
731  new_path_dhclient_pid = (char*) malloc(pfx + n_len + 6);
732  strncpy(new_path_dhclient_pid, path_dhclient_pid, pfx);
733  sprintf(new_path_dhclient_pid + pfx, "-%s.pid", ip->name);
734 
735  if ((pidfd = fopen(new_path_dhclient_pid, "re")) != NULL) {
736  e = fscanf(pidfd, "%ld\n", &temp);
737  oldpid = (pid_t)temp;
738 
739  if (e != 0 && e != EOF) {
740  if (oldpid) {
741  if (kill(oldpid, SIGTERM) == 0)
742  unlink(path_dhclient_pid);
743  }
744  }
745 
746  fclose(pidfd);
747  }
748 
749  free(new_path_dhclient_pid);
750  }
751  }
752  }
753  } else {
754  FILE *pidfp = NULL;
755  long temp = 0;
756  pid_t dhcpid = 0;
757  int dhc_running = 0;
758  char procfn[256] = "";
759 
760  if ((pidfp = fopen(path_dhclient_pid, "re")) != NULL) {
761  if ((fscanf(pidfp, "%ld", &temp)==1) && ((dhcpid=(pid_t)temp) > 0)) {
762  snprintf(procfn,256,"/proc/%u",dhcpid);
763  dhc_running = (access(procfn, F_OK) == 0);
764  }
765 
766  fclose(pidfp);
767  }
768 
769  if (dhc_running) {
770  log_fatal("dhclient(%u) is already running - exiting. ", dhcpid);
771  return(1);
772  }
773  }
774 
776 
777  if (!quiet) {
778  log_info("%s %s", message, PACKAGE_VERSION);
779  log_info(copyright);
780  log_info(arr);
781  log_info(url);
782  log_info("%s", "");
783  } else {
784  log_perror = 0;
786  }
787 
788  /* If we're given a relay agent address to insert, for testing
789  purposes, figure out what it is. */
790  if (mockup_relay) {
791  if (!inet_aton(mockup_relay, &giaddr)) {
792  struct hostent *he;
793  he = gethostbyname(mockup_relay);
794  if (he) {
795  memcpy(&giaddr, he->h_addr_list[0],
796  sizeof giaddr);
797  } else {
798  log_fatal("%s: no such host", mockup_relay);
799  }
800  }
801  }
802 
803  /* Get the current time... */
804  gettimeofday(&cur_tv, NULL);
805 
806  sockaddr_broadcast.sin_family = AF_INET;
807  sockaddr_broadcast.sin_port = remote_port;
808  if (server) {
809  if (!inet_aton(server, &sockaddr_broadcast.sin_addr)) {
810  struct hostent *he;
811  he = gethostbyname(server);
812  if (he) {
813  memcpy(&sockaddr_broadcast.sin_addr,
814  he->h_addr_list[0],
815  sizeof sockaddr_broadcast.sin_addr);
816  } else
817  sockaddr_broadcast.sin_addr.s_addr =
818  INADDR_BROADCAST;
819  }
820  } else {
821  sockaddr_broadcast.sin_addr.s_addr = INADDR_BROADCAST;
822  }
823 
824  inaddr_any.s_addr = INADDR_ANY;
825 
826  /* Discover all the network interfaces. */
828 
829  /* Parse the dhclient.conf file. */
831 
832  /* Stateless special case. */
833  if (stateless) {
834  if (release_mode || (wanted_ia_na > 0) ||
836  (interfaces_requested != 1)) {
837  usage("Stateless commnad: %s incompatibile with "
838  "other commands", "-S");
839  }
840 #if defined(DHCPv6) && defined(DHCP4o6)
841  run_stateless(exit_mode, dhcp4o6_port);
842 #else
843  run_stateless(exit_mode, 0);
844 #endif
845  return 0;
846  }
847 
848  /* Parse any extra command line configuration arguments: */
849  if ((dhcp_client_identifier_arg != NULL) && (*dhcp_client_identifier_arg != '\0')) {
850  arg_conf_len = asprintf(&arg_conf, "send dhcp-client-identifier \"%s\";", dhcp_client_identifier_arg);
851 
852  if ((arg_conf == 0) || (arg_conf_len <= 0))
853  log_fatal("Unable to send -C option dhcp-client-identifier");
854  }
855 
856  if ((dhcp_host_name_arg != NULL) && (*dhcp_host_name_arg != '\0')) {
857  if (arg_conf == 0) {
858  arg_conf_len = asprintf(&arg_conf, "send host-name \"%s\";", dhcp_host_name_arg);
859 
860  if ((arg_conf == 0) || (arg_conf_len <= 0))
861  log_fatal("Unable to send -H option host-name");
862  } else {
863  char *last_arg_conf = arg_conf;
864  arg_conf = NULL;
865  arg_conf_len = asprintf(&arg_conf, "%s\nsend host-name \"%s\";", last_arg_conf, dhcp_host_name_arg);
866 
867  if ((arg_conf == 0) || (arg_conf_len <= 0))
868  log_fatal("Unable to send -H option host-name");
869 
870  free(last_arg_conf);
871  }
872  }
873 
874  if ((dhcp_fqdn_arg != NULL) && (*dhcp_fqdn_arg != '\0')) {
875  if (arg_conf == 0) {
876  arg_conf_len = asprintf(&arg_conf, "send fqdn.fqdn \"%s\";", dhcp_fqdn_arg);
877 
878  if ((arg_conf == 0) || (arg_conf_len <= 0))
879  log_fatal("Unable to send -F option fqdn.fqdn");
880  } else {
881  char *last_arg_conf = arg_conf;
882  arg_conf = NULL;
883  arg_conf_len = asprintf(&arg_conf, "%s\nsend fqdn.fqdn \"%s\";", last_arg_conf, dhcp_fqdn_arg);
884 
885  if ((arg_conf == 0) || (arg_conf_len <= 0))
886  log_fatal("Unable to send -F option fqdn.fqdn");
887 
888  free(last_arg_conf);
889  }
890  }
891 
892  if (timeout_arg) {
893  if (arg_conf == 0) {
894  arg_conf_len = asprintf(&arg_conf, "timeout %d;", timeout_arg);
895 
896  if ((arg_conf == 0) || (arg_conf_len <= 0))
897  log_fatal("Unable to process --timeout timeout argument");
898  } else {
899  char *last_arg_conf = arg_conf;
900  arg_conf = NULL;
901  arg_conf_len = asprintf(&arg_conf, "%s\ntimeout %d;", last_arg_conf, timeout_arg);
902 
903  if ((arg_conf == 0) || (arg_conf_len == 0))
904  log_fatal("Unable to process --timeout timeout argument");
905 
906  free(last_arg_conf);
907  }
908  }
909 
910  if ((dhcp_vendor_class_identifier_arg != NULL) && (*dhcp_vendor_class_identifier_arg != '\0')) {
911  if (arg_conf == 0) {
912  arg_conf_len = asprintf(&arg_conf, "send vendor-class-identifier \"%s\";", dhcp_vendor_class_identifier_arg);
913 
914  if ((arg_conf == 0) || (arg_conf_len <= 0))
915  log_fatal("Unable to send -V option vendor-class-identifier");
916  } else {
917  char *last_arg_conf = arg_conf;
918  arg_conf = NULL;
919  arg_conf_len = asprintf(&arg_conf, "%s\nsend vendor-class-identifier \"%s\";", last_arg_conf, dhcp_vendor_class_identifier_arg);
920 
921  if ((arg_conf == 0) || (arg_conf_len <= 0))
922  log_fatal("Unable to send -V option vendor-class-identifier");
923 
924  free(last_arg_conf);
925  }
926  }
927 
928  if (dhclient_request_options != NULL) {
929  if (arg_conf == 0) {
930  arg_conf_len = asprintf(&arg_conf, "request %s;", dhclient_request_options);
931 
932  if ((arg_conf == 0) || (arg_conf_len <= 0))
933  log_fatal("Unable to parse --request-options <request options list> argument");
934  } else {
935  char *last_arg_conf = arg_conf;
936  arg_conf = NULL;
937  arg_conf_len = asprintf(&arg_conf, "%s\nrequest %s;", last_arg_conf, dhclient_request_options);
938 
939  if ((arg_conf == 0) || (arg_conf_len <= 0))
940  log_fatal("Unable to parse --request-options <request options list> argument");
941 
942  free(last_arg_conf);
943  }
944  }
945 
946  if (arg_conf) {
947  if (arg_conf_len == 0)
948  if ((arg_conf_len = strlen(arg_conf)) == 0)
949  /* huh ? cannot happen ! */
950  log_fatal("Unable to process -C/-H/-F/--timeout/-V/--request-options configuration arguments");
951 
952  /* parse the extra dhclient.conf configuration arguments
953  * into top level config: */
954  struct parse *cfile = (struct parse *)0;
955  const char *val = NULL;
956  int token;
957 
958  status = new_parse(&cfile, -1, arg_conf, arg_conf_len, "extra dhclient -C/-H/-F/--timeout/-V/--request-options configuration arguments", 0);
959 
960  if ((status != ISC_R_SUCCESS) || (cfile -> warnings_occurred))
961  log_fatal("Cannot parse -C/-H/-F/--timeout/-V/--request-options configuration arguments !");
962  /* more detailed parse failures will be logged */
963 
964  do {
965  token = peek_token(&val, (unsigned *)0, cfile);
966  if (token == END_OF_FILE)
967  break;
968 
970  } while (1);
971 
972  if (cfile -> warnings_occurred)
973  log_fatal("Cannot parse -C/-H/-F/--timeout/-V/--request-options configuration arguments !");
974  end_parse(&cfile);
975 
976  if (timeout_arg) {
977  /* we just set the toplevel timeout, but per-client
978  * timeouts may still be at defaults.
979  */
980  for (ip=interfaces; ip; ip = ip->next) {
981  if (ip->client->config->timeout == 60)
982  ip->client->config->timeout = timeout_arg;
983  }
984  }
985 
986  if ((dhclient_request_options != 0) && (top_level_config.requested_options != default_requested_options)) {
987  for (ip=interfaces; ip; ip = ip->next) {
988  if (ip->client->config->requested_options == default_requested_options)
990  }
991  }
992 
993  free(arg_conf);
994  arg_conf = NULL;
995  arg_conf_len = 0;
996  }
997 
998  /* Parse the lease database. */
1000 
1001  /* If desired parse the secondary lease database for a DUID */
1002  if ((default_duid.len == 0) && (path_dhclient_duid != NULL)) {
1003  read_client_duid();
1004  }
1005 
1006  /* Rewrite the lease database... */
1008 
1009  /* XXX */
1010 /* config_counter(&snd_counter, &rcv_counter); */
1011 
1012  /*
1013  * If no broadcast interfaces were discovered, call the script
1014  * and tell it so.
1015  */
1016  if (!interfaces) {
1017  /*
1018  * Call dhclient-script with the NBI flag,
1019  * in case somebody cares.
1020  */
1021  script_init(NULL, "NBI", NULL);
1022  script_go(NULL);
1023 
1024  /*
1025  * If we haven't been asked to persist, waiting for new
1026  * interfaces, then just exit.
1027  */
1028  if (!persist) {
1029  /* Nothing more to do. */
1030  log_info("No broadcast interfaces found - exiting.");
1031  exit(0);
1032  }
1033  } else if (!release_mode && !exit_mode) {
1034  /* Call the script with the list of interfaces. */
1035  for (ip = interfaces; ip; ip = ip->next) {
1036  /*
1037  * If interfaces were specified, don't configure
1038  * interfaces that weren't specified!
1039  */
1040  if ((interfaces_requested > 0) &&
1041  ((ip->flags & (INTERFACE_REQUESTED |
1042  INTERFACE_AUTOMATIC)) !=
1044  continue;
1045 
1046  if (local_family == AF_INET6) {
1047  script_init(ip->client, "PREINIT6", NULL);
1048  } else {
1049  script_init(ip->client, "PREINIT", NULL);
1050  if (ip->client->alias != NULL)
1052  "alias_",
1053  ip->client->alias);
1054  }
1055  script_go(ip->client);
1056  }
1057  }
1058 
1059  /* We create a backup seed before rediscovering interfaces in order to
1060  have a seed built using all of the available interfaces
1061  It's interesting if required interfaces doesn't let us defined
1062  a really unique seed due to a lack of valid HW addr later
1063  (this is the case with DHCP over IB)
1064  We only use the last device as using a sum could broke the
1065  uniqueness of the seed among multiple nodes
1066  */
1067  unsigned backup_seed = 0;
1068  for (ip = interfaces; ip; ip = ip -> next) {
1069  int junk;
1070  if ( ip -> hw_address.hlen <= sizeof seed )
1071  continue;
1072  memcpy (&junk,
1073  &ip -> hw_address.hbuf [ip -> hw_address.hlen -
1074  sizeof seed], sizeof seed);
1075  backup_seed = junk;
1076  }
1077 
1078 
1079  /* At this point, all the interfaces that the script thinks
1080  are relevant should be running, so now we once again call
1081  discover_interfaces(), and this time ask it to actually set
1082  up the interfaces. */
1085  : DISCOVER_RUNNING);
1086 
1087  /* Make up a seed for the random number generator from current
1088  time plus the sum of the last four bytes of each
1089  interface's hardware address interpreted as an integer.
1090  Not much entropy, but we're booting, so we're not likely to
1091  find anything better. */
1092  seed = 0;
1093  int seed_flag = 0;
1094  for (ip = interfaces; ip; ip = ip->next) {
1095  int junk;
1096  if ( ip -> hw_address.hlen <= sizeof seed )
1097  continue;
1098  memcpy(&junk,
1099  &ip->hw_address.hbuf[ip->hw_address.hlen -
1100  sizeof seed], sizeof seed);
1101  seed += junk;
1102  seed_flag = 1;
1103  }
1104  if ( seed_flag == 0 ) {
1105  if ( backup_seed != 0 ) {
1106  seed = backup_seed;
1107  log_info ("xid: rand init seed (0x%x) built using all"
1108  " available interfaces",seed);
1109  }
1110  else {
1111  seed = cur_time^((unsigned) gethostid()) ;
1112  log_info ("xid: warning: no netdev with useable HWADDR found"
1113  " for seed's uniqueness enforcement");
1114  log_info ("xid: rand init seed (0x%x) built using gethostid",
1115  seed);
1116  }
1117  /* we only use seed and no current time as a broadcast reply */
1118  /* will certainly be used by the hwaddrless interface */
1119  srandom(seed + ((unsigned)(cur_tv.tv_usec * 1000000)) + (unsigned)getpid());
1120  }
1121  else
1122  srandom(seed + ((unsigned)(cur_tv.tv_usec * 1000000)) + (unsigned)getpid());
1123 
1124  /* Setup specific Infiniband options */
1125  for (ip = interfaces; ip; ip = ip->next) {
1126  if (ip->client &&
1127  (ip->hw_address.hbuf[0] == HTYPE_INFINIBAND)) {
1128  setup_ib_interface(ip);
1129  }
1130  }
1131 
1132  /*
1133  * Establish a default DUID. We always do so for v6 and
1134  * do so if desired for v4 via the -D or -i options
1135  */
1136  if ((local_family == AF_INET6) ||
1137  ((local_family == AF_INET) && (duid_v4 == 1))) {
1138  if (default_duid.len == 0) {
1139  if (default_duid.buffer != NULL)
1141 
1142  if (form_duid(&default_duid, MDL) == ISC_R_SUCCESS)
1143  write_duid(&default_duid);
1144  }
1145  }
1146 
1147 #if defined(DHCPv6) && defined(DHCP4o6)
1148  if (dhcpv4_over_dhcpv6 && !exit_mode)
1149  dhcp4o6_setup(dhcp4o6_port);
1150 #endif
1151 
1152  /* Start a configuration state machine for each interface. */
1153 #ifdef DHCPv6
1154  if (local_family == AF_INET6) {
1155  for (ip = interfaces ; ip != NULL ; ip = ip->next) {
1156  for (client = ip->client ; client != NULL ;
1157  client = client->next) {
1158  if (release_mode) {
1159  start_release6(client);
1160  continue;
1161  } else if (exit_mode) {
1162  unconfigure6(client, "STOP6");
1163  continue;
1164  }
1165 
1166  /* If we have a previous binding, Confirm
1167  * that we can (or can't) still use it.
1168  */
1169  if ((client->active_lease != NULL) &&
1170  !client->active_lease->released)
1171  start_confirm6(client);
1172  else
1173  start_init6(client);
1174  }
1175  }
1176  } else
1177 #endif /* DHCPv6 */
1178  {
1179  for (ip = interfaces ; ip ; ip = ip->next) {
1180  ip->flags |= INTERFACE_RUNNING;
1181  for (client = ip->client ; client ;
1182  client = client->next) {
1183  if (exit_mode)
1184  state_stop(client);
1185  if (release_mode)
1186  do_release(client);
1187  else {
1188  client->state = S_INIT;
1189 
1191  {
1192  tv.tv_sec = 0;
1193  if (top_level_config.
1194  initial_delay>1)
1195  tv.tv_sec = cur_time
1196  + random()
1197  % (top_level_config.
1198  initial_delay-1);
1199  tv.tv_usec = random()
1200  % 1000000;
1201  /*
1202  * this gives better
1203  * distribution than just
1204  *whole seconds
1205  */
1207  client, 0, 0);
1208  } else {
1209  state_reboot(client);
1210  }
1211  }
1212  }
1213  }
1214  }
1215 
1216  if (exit_mode)
1217  return 0;
1218  if (release_mode) {
1219 #ifndef DHCPv6
1220  return 0;
1221 #else
1222  if ((local_family == AF_INET6) || dhcpv4_over_dhcpv6) {
1223  if (onetry)
1224  return 0;
1225  } else
1226  return 0;
1227 #endif /* DHCPv6 */
1228  }
1229 
1230  /* Start up a listener for the object management API protocol. */
1231  if (top_level_config.omapi_port != -1) {
1232  listener = NULL;
1233  result = omapi_generic_new(&listener, MDL);
1234  if (result != ISC_R_SUCCESS)
1235  log_fatal("Can't allocate new generic object: %s\n",
1236  isc_result_totext(result));
1237  result = omapi_protocol_listen(listener,
1238  (unsigned)
1240  1);
1241  if (result != ISC_R_SUCCESS)
1242  log_fatal("Can't start OMAPI protocol: %s",
1243  isc_result_totext (result));
1244  }
1245 
1246  /* Set up the bootp packet handler... */
1248 #ifdef DHCPv6
1250 #endif /* DHCPv6 */
1251 
1252 #if defined(DEBUG_MEMORY_LEAKAGE) || defined(DEBUG_MALLOC_POOL) || \
1253  defined(DEBUG_MEMORY_LEAKAGE_ON_EXIT)
1254  dmalloc_cutoff_generation = dmalloc_generation;
1255  dmalloc_longterm = dmalloc_outstanding;
1256  dmalloc_outstanding = 0;
1257 #endif
1258 
1259 #if defined(ENABLE_GENTLE_SHUTDOWN)
1260  /* no signal handlers until we deal with the side effects */
1261  /* install signal handlers */
1262  signal(SIGINT, dhcp_signal_handler); /* control-c */
1263  signal(SIGTERM, dhcp_signal_handler); /* kill */
1264 #endif
1265 
1266  /* If we're not supposed to wait before getting the address,
1267  don't. */
1268  if (nowait)
1269  go_daemon();
1270 
1271  /* If we're not going to daemonize, write the pid file
1272  now. */
1273  if (no_daemon || nowait)
1275 
1276  /* Start dispatching packets and timeouts... */
1277  dispatch();
1278 
1279  /* In fact dispatch() never returns. */
1280  return 0;
1281 }
1282 
1283 /*
1284  * \brief Run the DHCPv6 stateless client (dhclient -6 -S)
1285  *
1286  * \param exist_mode set to 1 when dhclient was called with -x
1287  * \param port DHCPv4-over-DHCPv6 client inter-process communication
1288  * UDP port pair (port,port+1 with port in network byte order)
1289  */
1290 
1291 void run_stateless(int exit_mode, u_int16_t port)
1292 {
1293 #ifdef DHCPv6
1294  struct client_state *client;
1295  omapi_object_t *listener;
1296  isc_result_t result;
1297 
1298 #ifndef DHCP4o6
1299  IGNORE_UNUSED(port);
1300 #endif
1301 
1302  struct interface_info *ip;
1303 
1304  if (!interfaces)
1305  usage("No interfaces available for stateless command: %s", "-S");
1306 
1307 #ifdef DHCP4o6
1308  if (dhcpv4_over_dhcpv6) {
1309  /* Mark we want to request IRT too! */
1311  }
1312 #endif
1313 
1314  for (ip = interfaces; ip; ip = ip->next) {
1315  if ((interfaces_requested > 0) &&
1316  ((ip->flags & (INTERFACE_REQUESTED |
1317  INTERFACE_AUTOMATIC)) !=
1319  continue;
1320  script_init(ip->client, "PREINIT6", NULL);
1321  script_go(ip->client);
1322  }
1323 
1324  /* Discover the network interface. */
1326 
1327  /* Parse the lease database. */
1329 
1330  /* If desired parse the secondary lease database for a DUID */
1331  if ((default_duid.len == 0) && (path_dhclient_duid != NULL)) {
1332  read_client_duid();
1333  }
1334 
1335  /* Establish a default DUID. */
1336  if (default_duid.len == 0) {
1337  if (default_duid.buffer != NULL)
1339 
1341  if (form_duid(&default_duid, MDL) == ISC_R_SUCCESS &&
1342  duid_type == DUID_LLT)
1343  write_duid(&default_duid);
1344  }
1345 
1346 #ifdef DHCP4o6
1347  if (dhcpv4_over_dhcpv6 && !exit_mode)
1348  dhcp4o6_setup(port);
1349 #endif
1350 
1351  /* Start a configuration state machine. */
1352  for (client = interfaces->client ;
1353  client != NULL ;
1354  client = client->next) {
1355  if (exit_mode) {
1356  unconfigure6(client, "STOP6");
1357  continue;
1358  }
1359  start_info_request6(client);
1360  }
1361  if (exit_mode)
1362  return;
1363 
1364  /* Start up a listener for the object management API protocol. */
1365  if (top_level_config.omapi_port != -1) {
1366  listener = NULL;
1367  result = omapi_generic_new(&listener, MDL);
1368  if (result != ISC_R_SUCCESS)
1369  log_fatal("Can't allocate new generic object: %s\n",
1370  isc_result_totext(result));
1371  result = omapi_protocol_listen(listener,
1372  (unsigned)
1374  1);
1375  if (result != ISC_R_SUCCESS)
1376  log_fatal("Can't start OMAPI protocol: %s",
1377  isc_result_totext(result));
1378  }
1379 
1380  /* Set up the packet handler... */
1382 
1383 #if defined(DEBUG_MEMORY_LEAKAGE) || defined(DEBUG_MALLOC_POOL) || \
1384  defined(DEBUG_MEMORY_LEAKAGE_ON_EXIT)
1385  dmalloc_cutoff_generation = dmalloc_generation;
1386  dmalloc_longterm = dmalloc_outstanding;
1387  dmalloc_outstanding = 0;
1388 #endif
1389 
1390  /* If we're not supposed to wait before getting the address,
1391  don't. */
1392  if (nowait)
1393  go_daemon();
1394 
1395  /* If we're not going to daemonize, write the pid file
1396  now. */
1397  if (no_daemon || nowait)
1399 
1400  /* Start dispatching packets and timeouts... */
1401  dispatch();
1402 
1403 #endif /* DHCPv6 */
1404  return;
1405 }
1406 #endif /* !UNIT_TEST */
1407 
1408 isc_result_t find_class (struct class **c,
1409  const char *s, const char *file, int line)
1410 {
1411  return 0;
1412 }
1413 
1415  struct packet *packet;
1416  struct lease *lease;
1417  struct collection *collection;
1418 {
1419  return 0;
1420 }
1421 
1422 void classify (packet, class)
1423  struct packet *packet;
1424  struct class *class;
1425 {
1426 }
1427 
1428 void unbill_class (lease)
1429  struct lease *lease;
1430 {
1431 }
1432 
1433 int find_subnet (struct subnet **sp,
1434  struct iaddr addr, const char *file, int line)
1435 {
1436  return 0;
1437 }
1438 
1439 static void setup_ib_interface(struct interface_info *ip)
1440 {
1441  struct group *g;
1442 
1443  /* Set the broadcast flag */
1445 
1446  /*
1447  * Find out if a dhcp-client-identifier option was specified either
1448  * in the config file or on the command line
1449  */
1450  for (g = ip->client->config->on_transmission; g != NULL; g = g->next) {
1451  if ((g->statements != NULL) &&
1452  (strcmp(g->statements->data.option->option->name,
1453  "dhcp-client-identifier") == 0)) {
1454  return;
1455  }
1456  }
1457 
1458  /* No client ID specified */
1459  //log_fatal("dhcp-client-identifier must be specified for InfiniBand");
1460 }
1461 
1462 /* Individual States:
1463  *
1464  * Each routine is called from the dhclient_state_machine() in one of
1465  * these conditions:
1466  * -> entering INIT state
1467  * -> recvpacket_flag == 0: timeout in this state
1468  * -> otherwise: received a packet in this state
1469  *
1470  * Return conditions as handled by dhclient_state_machine():
1471  * Returns 1, sendpacket_flag = 1: send packet, reset timer.
1472  * Returns 1, sendpacket_flag = 0: just reset the timer (wait for a milestone).
1473  * Returns 0: finish the nap which was interrupted for no good reason.
1474  *
1475  * Several per-interface variables are used to keep track of the process:
1476  * active_lease: the lease that is being used on the interface
1477  * (null pointer if not configured yet).
1478  * offered_leases: leases corresponding to DHCPOFFER messages that have
1479  * been sent to us by DHCP servers.
1480  * acked_leases: leases corresponding to DHCPACK messages that have been
1481  * sent to us by DHCP servers.
1482  * sendpacket: DHCP packet we're trying to send.
1483  * destination: IP address to send sendpacket to
1484  * In addition, there are several relevant per-lease variables.
1485  * T1_expiry, T2_expiry, lease_expiry: lease milestones
1486  * In the active lease, these control the process of renewing the lease;
1487  * In leases on the acked_leases list, this simply determines when we
1488  * can no longer legitimately use the lease.
1489  */
1490 
1491 void state_reboot (cpp)
1492  void *cpp;
1493 {
1494  struct client_state *client = cpp;
1495 
1496 #if defined(DHCPv6) && defined(DHCP4o6)
1497  if (dhcpv4_over_dhcpv6 && (dhcp4o6_state <= 0)) {
1498  if (dhcp4o6_state < 0)
1499  dhcp4o6_poll(NULL);
1500  client->pending = P_REBOOT;
1501  return;
1502  }
1503 #endif
1504 
1505  client->pending= P_NONE;
1506 
1507  /* If we don't remember an active lease, go straight to INIT. */
1508  if (!client -> active ||
1509  client -> active -> is_bootp ||
1510  client -> active -> expiry <= cur_time) {
1511  state_init (client);
1512  return;
1513  }
1514 
1515  /* We are in the rebooting state. */
1516  client -> state = S_REBOOTING;
1517 
1518  /*
1519  * make_request doesn't initialize xid because it normally comes
1520  * from the DHCPDISCOVER, but we haven't sent a DHCPDISCOVER,
1521  * so pick an xid now.
1522  */
1523  client -> xid = random ();
1524 
1525  /*
1526  * Make a DHCPREQUEST packet, and set
1527  * appropriate per-interface flags.
1528  */
1529  make_request (client, client -> active);
1530  client -> destination = iaddr_broadcast;
1531  client -> first_sending = cur_time;
1532  client -> interval = client -> config -> initial_interval;
1533 
1534  /* Zap the medium list... */
1535  client -> medium = NULL;
1536 
1537  /* Send out the first DHCPREQUEST packet. */
1538  send_request (client);
1539 }
1540 
1541 /* Called when a lease has completely expired and we've been unable to
1542  renew it. */
1543 
1544 void state_init (cpp)
1545  void *cpp;
1546 {
1547  struct client_state *client = cpp;
1548  enum dhcp_state init_state = client->state;
1549  struct timeval tv;
1550 
1551  ASSERT_STATE(state, S_INIT);
1552 
1553  /* Make a DHCPDISCOVER packet, and set appropriate per-interface
1554  flags. */
1555  make_discover (client, client -> active);
1556  client -> xid = client -> packet.xid;
1557  client -> destination = iaddr_broadcast;
1558  client -> state = S_SELECTING;
1559  client -> first_sending = cur_time;
1560  client -> interval = client -> config -> initial_interval;
1561 
1562  if (init_state != S_DECLINED) {
1563  /* Add an immediate timeout to cause the first DHCPDISCOVER packet
1564  to go out. */
1565  send_discover(client);
1566  } else {
1567  /* We've received an OFFER and it has been DECLINEd by dhclient-script.
1568  * wait for a random time between 1 and backoff_cutoff seconds before
1569  * trying again. */
1570  tv . tv_sec = cur_time + ((1 + (random() >> 2)) % client->config->backoff_cutoff);
1571  tv . tv_usec = 0;
1572  add_timeout(&tv, send_discover, client, 0, 0);
1573  }
1574 }
1575 
1576 /*
1577  * state_selecting is called when one or more DHCPOFFER packets have been
1578  * received and a configurable period of time has passed.
1579  */
1580 
1582  void *cpp;
1583 {
1584  struct client_state *client = cpp;
1585  struct client_lease *lp, *next, *picked;
1586 
1587 
1588  ASSERT_STATE(state, S_SELECTING);
1589 
1590  /*
1591  * Cancel state_selecting and send_discover timeouts, since either
1592  * one could have got us here.
1593  */
1594  cancel_timeout (state_selecting, client);
1595  cancel_timeout (send_discover, client);
1596 
1597  /*
1598  * We have received one or more DHCPOFFER packets. Currently,
1599  * the only criterion by which we judge leases is whether or
1600  * not we get a response when we arp for them.
1601  */
1602  picked = NULL;
1603  for (lp = client -> offered_leases; lp; lp = next) {
1604  next = lp -> next;
1605 
1606  /*
1607  * Check to see if we got an ARPREPLY for the address
1608  * in this particular lease.
1609  */
1610  if (!picked) {
1611  picked = lp;
1612  picked -> next = NULL;
1613  } else {
1614  destroy_client_lease (lp);
1615  }
1616  }
1617  client -> offered_leases = NULL;
1618 
1619  /*
1620  * If we just tossed all the leases we were offered, go back
1621  * to square one.
1622  */
1623  if (!picked) {
1624  client -> state = S_INIT;
1625  state_init (client);
1626  return;
1627  }
1628 
1629  /* If it was a BOOTREPLY, we can just take the address right now. */
1630  if (picked -> is_bootp) {
1631  client -> new = picked;
1632 
1633  /* Make up some lease expiry times
1634  XXX these should be configurable. */
1635  client -> new -> expiry = cur_time + 12000;
1636  client -> new -> renewal += cur_time + 8000;
1637  client -> new -> rebind += cur_time + 10000;
1638 
1639  client -> state = S_REQUESTING;
1640 
1641  /* Bind to the address we received. */
1642  bind_lease (client);
1643  return;
1644  }
1645 
1646  /* Go to the REQUESTING state. */
1647  client -> destination = iaddr_broadcast;
1648  client -> state = S_REQUESTING;
1649  client -> first_sending = cur_time;
1650  client -> interval = client -> config -> initial_interval;
1651 
1652  /* Make a DHCPREQUEST packet from the lease we picked. */
1653  make_request (client, picked);
1654  client -> xid = client -> packet.xid;
1655 
1656  /* Toss the lease we picked - we'll get it back in a DHCPACK. */
1657  destroy_client_lease (picked);
1658 
1659  /* Add an immediate timeout to send the first DHCPREQUEST packet. */
1660  send_request (client);
1661 }
1662 
1663 /* state_requesting is called when we receive a DHCPACK message after
1664  having sent out one or more DHCPREQUEST packets. */
1665 
1666 void dhcpack (packet)
1667  struct packet *packet;
1668 {
1669  struct interface_info *ip = packet -> interface;
1670  struct client_state *client;
1671  struct client_lease *lease;
1672  struct option_cache *oc;
1673  struct data_string ds;
1674 
1675  /* If we're not receptive to an offer right now, or if the offer
1676  has an unrecognizable transaction id, then just drop it. */
1677  for (client = ip -> client; client; client = client -> next) {
1678  if (client -> xid == packet -> raw -> xid)
1679  break;
1680  }
1681  if (!client ||
1682  (packet -> interface -> hw_address.hlen - 1 !=
1683  packet -> raw -> hlen) ||
1684  (memcmp (&packet -> interface -> hw_address.hbuf [1],
1685  packet -> raw -> chaddr, packet -> raw -> hlen))) {
1686 #if defined (DEBUG)
1687  log_debug ("DHCPACK in wrong transaction.");
1688 #endif
1689  return;
1690  }
1691 
1692  if (client -> state != S_REBOOTING &&
1693  client -> state != S_REQUESTING &&
1694  client -> state != S_RENEWING &&
1695  client -> state != S_REBINDING) {
1696 #if defined (DEBUG)
1697  log_debug ("DHCPACK in wrong state.");
1698 #endif
1699  return;
1700  }
1701 
1702  log_info ("DHCPACK from %s (xid=0x%x)", piaddr (packet -> client_addr), ntohl(client -> xid));
1703 
1704  lease = packet_to_lease (packet, client);
1705  if (!lease) {
1706  log_info ("packet_to_lease failed.");
1707  return;
1708  }
1709 
1710  client -> new = lease;
1711 
1712  /* Stop resending DHCPREQUEST. */
1713  cancel_timeout (send_request, client);
1714 
1715  /* Figure out the lease time. */
1716  oc = lookup_option (&dhcp_universe, client -> new -> options,
1718  memset (&ds, 0, sizeof ds);
1719  if (oc &&
1720  evaluate_option_cache (&ds, packet, (struct lease *)0, client,
1721  packet -> options, client -> new -> options,
1722  &global_scope, oc, MDL)) {
1723  if (ds.len > 3)
1724  client -> new -> expiry = getULong (ds.data);
1725  else
1726  client -> new -> expiry = 0;
1727  data_string_forget (&ds, MDL);
1728  } else
1729  client -> new -> expiry = 0;
1730 
1731  if (client->new->expiry == 0) {
1732  struct timeval tv;
1733 
1734  log_error ("no expiry time on offered lease.");
1735 
1736  /* Quench this (broken) server. Return to INIT to reselect. */
1737  add_reject(packet);
1738 
1739  /* 1/2 second delay to restart at INIT. */
1740  tv.tv_sec = cur_tv.tv_sec;
1741  tv.tv_usec = cur_tv.tv_usec + 500000;
1742 
1743  if (tv.tv_usec >= 1000000) {
1744  tv.tv_sec++;
1745  tv.tv_usec -= 1000000;
1746  }
1747 
1748  add_timeout(&tv, state_init, client, 0, 0);
1749  return;
1750  }
1751 
1752  /*
1753  * A number that looks negative here is really just very large,
1754  * because the lease expiry offset is unsigned.
1755  */
1756  if (client->new->expiry < 0)
1757  client->new->expiry = TIME_MAX;
1758 
1759  /* Take the server-provided renewal time if there is one. */
1760  oc = lookup_option (&dhcp_universe, client -> new -> options,
1762  if (oc &&
1763  evaluate_option_cache (&ds, packet, (struct lease *)0, client,
1764  packet -> options, client -> new -> options,
1765  &global_scope, oc, MDL)) {
1766  if (ds.len > 3)
1767  client -> new -> renewal = getULong (ds.data);
1768  else
1769  client -> new -> renewal = 0;
1770  data_string_forget (&ds, MDL);
1771  } else
1772  client -> new -> renewal = 0;
1773 
1774  /* If it wasn't specified by the server, calculate it. */
1775  if (!client -> new -> renewal)
1776  client -> new -> renewal = client -> new -> expiry / 2 + 1;
1777 
1778  if (client -> new -> renewal <= 0)
1779  client -> new -> renewal = TIME_MAX;
1780 
1781  /* Now introduce some randomness to the renewal time: */
1782  if (client->new->renewal <= ((TIME_MAX / 3) - 3))
1783  client->new->renewal = (((client->new->renewal * 3) + 3) / 4) +
1784  (((random() % client->new->renewal) + 3) / 4);
1785 
1786  /* Same deal with the rebind time. */
1787  oc = lookup_option (&dhcp_universe, client -> new -> options,
1789  if (oc &&
1790  evaluate_option_cache (&ds, packet, (struct lease *)0, client,
1791  packet -> options, client -> new -> options,
1792  &global_scope, oc, MDL)) {
1793  if (ds.len > 3)
1794  client -> new -> rebind = getULong (ds.data);
1795  else
1796  client -> new -> rebind = 0;
1797  data_string_forget (&ds, MDL);
1798  } else
1799  client -> new -> rebind = 0;
1800 
1801  if (client -> new -> rebind <= 0) {
1802  if (client -> new -> expiry <= TIME_MAX / 7)
1803  client -> new -> rebind =
1804  client -> new -> expiry * 7 / 8;
1805  else
1806  client -> new -> rebind =
1807  client -> new -> expiry / 8 * 7;
1808  }
1809 
1810  /* Make sure our randomness didn't run the renewal time past the
1811  rebind time. */
1812  if (client -> new -> renewal > client -> new -> rebind) {
1813  if (client -> new -> rebind <= TIME_MAX / 3)
1814  client -> new -> renewal =
1815  client -> new -> rebind * 3 / 4;
1816  else
1817  client -> new -> renewal =
1818  client -> new -> rebind / 4 * 3;
1819  }
1820 
1821  client -> new -> expiry += cur_time;
1822  /* Lease lengths can never be negative. */
1823  if (client -> new -> expiry < cur_time)
1824  client -> new -> expiry = TIME_MAX;
1825  client -> new -> renewal += cur_time;
1826  if (client -> new -> renewal < cur_time)
1827  client -> new -> renewal = TIME_MAX;
1828  client -> new -> rebind += cur_time;
1829  if (client -> new -> rebind < cur_time)
1830  client -> new -> rebind = TIME_MAX;
1831 
1832  bind_lease (client);
1833 }
1834 
1835 void bind_lease (client)
1836  struct client_state *client;
1837 {
1838  struct timeval tv;
1839 
1840  /* Remember the medium. */
1841  client->new->medium = client->medium;
1842 
1843  /* Run the client script with the new parameters. */
1844  script_init(client, (client->state == S_REQUESTING ? "BOUND" :
1845  (client->state == S_RENEWING ? "RENEW" :
1846  (client->state == S_REBOOTING ? "REBOOT" :
1847  "REBIND"))),
1848  client->new->medium);
1849  if (client->active && client->state != S_REBOOTING)
1850  script_write_params(client, "old_", client->active);
1851  script_write_params(client, "new_", client->new);
1852  script_write_requested(client);
1853  if (client->alias)
1854  script_write_params(client, "alias_", client->alias);
1855 
1856  /* If the BOUND/RENEW code detects another machine using the
1857  offered address, it exits nonzero. We need to send a
1858  DHCPDECLINE and toss the lease. */
1859  if (script_go(client)) {
1860  make_decline(client, client->new);
1861  send_decline(client);
1862  destroy_client_lease(client->new);
1863  client->new = NULL;
1864  if (onetry) {
1865  if (!quiet) {
1866  log_info("Unable to obtain a lease on first "
1867  "try (declined). Exiting.");
1868  }
1869 
1870 #if defined (CALL_SCRIPT_ON_ONETRY_FAIL)
1871  /* Let's call a script and we're done */
1872  script_init(client, "FAIL", (struct string_list *)0);
1873  script_go(client);
1874 #endif
1875  exit(2);
1876  } else {
1877  client -> state = S_DECLINED;
1878  state_init(client);
1879  return;
1880  }
1881  }
1882 
1883  /* Write out the new lease if it has been long enough. */
1884  if (!client->last_write ||
1885  (cur_time - client->last_write) >= MIN_LEASE_WRITE)
1886  write_client_lease(client, client->new, 0, 1);
1887 
1888  /* Replace the old active lease with the new one. */
1889  if (client->active)
1890  destroy_client_lease(client->active);
1891  client->active = client->new;
1892  client->new = NULL;
1893 
1894  /* Set up a timeout to start the renewal process. */
1895  tv.tv_sec = client->active->renewal;
1896  tv.tv_usec = ((client->active->renewal - cur_tv.tv_sec) > 1) ?
1897  random() % 1000000 : cur_tv.tv_usec;
1898  add_timeout(&tv, state_bound, client, 0, 0);
1899 
1900  log_info("bound to %s -- renewal in %ld seconds.",
1901  piaddr(client->active->address),
1902  (long)(client->active->renewal - cur_time));
1903  client->state = S_BOUND;
1905  go_daemon();
1906 #if defined (NSUPDATE)
1907  if (client->config->do_forward_update)
1908  dhclient_schedule_updates(client, &client->active->address, 1);
1909 #endif
1910 }
1911 
1912 /* state_bound is called when we've successfully bound to a particular
1913  lease, but the renewal time on that lease has expired. We are
1914  expected to unicast a DHCPREQUEST to the server that gave us our
1915  original lease. */
1916 
1917 void state_bound (cpp)
1918  void *cpp;
1919 {
1920  struct client_state *client = cpp;
1921  struct option_cache *oc;
1922  struct data_string ds;
1923 
1924  ASSERT_STATE(state, S_BOUND);
1925 
1926  /* T1 has expired. */
1927  make_request (client, client -> active);
1928  client -> xid = client -> packet.xid;
1929 
1930  memset (&ds, 0, sizeof ds);
1931  oc = lookup_option (&dhcp_universe, client -> active -> options,
1933  if (oc &&
1934  evaluate_option_cache (&ds, (struct packet *)0, (struct lease *)0,
1935  client, (struct option_state *)0,
1936  client -> active -> options,
1937  &global_scope, oc, MDL)) {
1938  if (ds.len > 3) {
1939  memcpy (client -> destination.iabuf, ds.data, 4);
1940  client -> destination.len = 4;
1941  } else
1942  client -> destination = iaddr_broadcast;
1943 
1944  data_string_forget (&ds, MDL);
1945  } else
1946  client -> destination = iaddr_broadcast;
1947 
1948  client -> first_sending = cur_time;
1949  client -> interval = client -> config -> initial_interval;
1950  client -> state = S_RENEWING;
1951 
1952  /* Send the first packet immediately. */
1953  send_request (client);
1954 }
1955 
1956 /* state_stop is called when we've been told to shut down. We unconfigure
1957  the interfaces, and then stop operating until told otherwise. */
1958 
1959 void state_stop (cpp)
1960  void *cpp;
1961 {
1962  struct client_state *client = cpp;
1963 
1964  client->pending = P_NONE;
1965 
1966  /* Cancel all timeouts. */
1968  cancel_timeout(send_discover, client);
1969  cancel_timeout(send_request, client);
1970  cancel_timeout(state_bound, client);
1971 
1972  /* If we have an address, unconfigure it. */
1973  if (client->active) {
1974  script_init(client, "STOP", client->active->medium);
1975  script_write_params(client, "old_", client->active);
1976  script_write_requested(client);
1977  if (client->alias)
1978  script_write_params(client, "alias_", client->alias);
1979  script_go(client);
1980  }
1981 }
1982 
1984 {
1985  return 0;
1986 }
1987 
1988 int write_lease (lease)
1989  struct lease *lease;
1990 {
1991  return 0;
1992 }
1993 
1995  struct host_decl *host;
1996 {
1997  return 0;
1998 }
1999 
2000 void db_startup (testp)
2001  int testp;
2002 {
2003 }
2004 
2005 void bootp (packet)
2006  struct packet *packet;
2007 {
2008  struct iaddrmatchlist *ap;
2009  char addrbuf[4*16];
2010  char maskbuf[4*16];
2011 
2012  if (packet -> raw -> op != BOOTREPLY)
2013  return;
2014 
2015  /* If there's a reject list, make sure this packet's sender isn't
2016  on it. */
2017  for (ap = packet -> interface -> client -> config -> reject_list;
2018  ap; ap = ap -> next) {
2019  if (addr_match(&packet->client_addr, &ap->match)) {
2020 
2021  /* piaddr() returns its result in a static
2022  buffer sized 4*16 (see common/inet.c). */
2023 
2024  strcpy(addrbuf, piaddr(ap->match.addr));
2025  strcpy(maskbuf, piaddr(ap->match.mask));
2026 
2027  log_info("BOOTREPLY from %s rejected by rule %s "
2028  "mask %s.", piaddr(packet->client_addr),
2029  addrbuf, maskbuf);
2030  return;
2031  }
2032  }
2033 
2034  dhcpoffer (packet);
2035 
2036 }
2037 
2038 void dhcp (packet)
2039  struct packet *packet;
2040 {
2041  struct iaddrmatchlist *ap;
2042  void (*handler) (struct packet *);
2043  const char *type;
2044  char addrbuf[4*16];
2045  char maskbuf[4*16];
2046 
2047  switch (packet -> packet_type) {
2048  case DHCPOFFER:
2049  handler = dhcpoffer;
2050  type = "DHCPOFFER";
2051  break;
2052 
2053  case DHCPNAK:
2054  handler = dhcpnak;
2055  type = "DHCPNACK";
2056  break;
2057 
2058  case DHCPACK:
2059  handler = dhcpack;
2060  type = "DHCPACK";
2061  break;
2062 
2063  default:
2064  return;
2065  }
2066 
2067  /* If there's a reject list, make sure this packet's sender isn't
2068  on it. */
2069  for (ap = packet -> interface -> client -> config -> reject_list;
2070  ap; ap = ap -> next) {
2071  if (addr_match(&packet->client_addr, &ap->match)) {
2072 
2073  /* piaddr() returns its result in a static
2074  buffer sized 4*16 (see common/inet.c). */
2075 
2076  strcpy(addrbuf, piaddr(ap->match.addr));
2077  strcpy(maskbuf, piaddr(ap->match.mask));
2078 
2079  log_info("%s from %s rejected by rule %s mask %s.",
2080  type, piaddr(packet->client_addr),
2081  addrbuf, maskbuf);
2082  return;
2083  }
2084  }
2085  (*handler) (packet);
2086 }
2087 
2088 #ifdef DHCPv6
2089 void
2090 dhcpv6(struct packet *packet) {
2091  struct iaddrmatchlist *ap;
2092  struct client_state *client;
2093  char addrbuf[sizeof("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")];
2094 
2095  /* Silently drop bogus messages. */
2096  if (packet->dhcpv6_msg_type >= dhcpv6_type_name_max)
2097  return;
2098 
2099  /* Discard, with log, packets from quenched sources. */
2100  for (ap = packet->interface->client->config->reject_list ;
2101  ap ; ap = ap->next) {
2102  if (addr_match(&packet->client_addr, &ap->match)) {
2103  strcpy(addrbuf, piaddr(packet->client_addr));
2104  log_info("%s from %s rejected by rule %s",
2106  addrbuf,
2107  piaddrmask(&ap->match.addr, &ap->match.mask));
2108  return;
2109  }
2110  }
2111 
2112  /* Screen out nonsensical messages. */
2113  switch(packet->dhcpv6_msg_type) {
2114 #ifdef DHCP4o6
2116  if (dhcpv4_over_dhcpv6) {
2117  log_info("RCV: %s message on %s from %s.",
2119  packet->interface->name,
2120  piaddr(packet->client_addr));
2121  forw_dhcpv4_response(packet);
2122  }
2123  return;
2124 #endif
2125  case DHCPV6_ADVERTISE:
2126  case DHCPV6_RECONFIGURE:
2127  if (stateless)
2128  return;
2129  /* Falls through */
2130  case DHCPV6_REPLY:
2131  log_info("RCV: %s message on %s from %s.",
2133  packet->interface->name, piaddr(packet->client_addr));
2134  break;
2135 
2136  default:
2137  return;
2138  }
2139 
2140  /* Find a client state that matches the incoming XID. */
2141  for (client = packet->interface->client ; client ;
2142  client = client->next) {
2143  if (memcmp(&client->dhcpv6_transaction_id,
2144  packet->dhcpv6_transaction_id, 3) == 0) {
2145  client->v6_handler(packet, client);
2146  return;
2147  }
2148  }
2149 
2150  /* XXX: temporary log for debugging */
2151  log_info("Packet received, but nothing done with it.");
2152 }
2153 
2154 #ifdef DHCP4o6
2155 /*
2156  * \brief Forward a DHCPv4-response to the DHCPv4 client.
2157  * (DHCPv6 client function)
2158  *
2159  * The DHCPv6 client receives a DHCPv4-response which is forwarded
2160  * to the DHCPv4 client.
2161  * Format: address:16 + DHCPv4 message content
2162  * (we have no state to keep the address so it is transported in
2163  * DHCPv6 <-> DHCPv6 inter-process messages)
2164  *
2165  * \param packet the DHCPv4-response packet
2166  */
2167 static void forw_dhcpv4_response(struct packet *packet)
2168 {
2169  struct option_cache *oc;
2170  struct data_string enc_opt_data;
2171  struct data_string ds;
2172  int cc;
2173 
2174  /*
2175  * Discard if relay is not ready.
2176  */
2177  if (dhcp4o6_state == -1) {
2178  log_info("forw_dhcpv4_response: not ready.");
2179  return;
2180  }
2181 
2182  if (packet->client_addr.len != 16) {
2183  log_error("forw_dhcpv4_response: bad address");
2184  return;
2185  }
2186 
2187  /*
2188  * Get our encapsulated DHCPv4 message.
2189  */
2191  if (oc == NULL) {
2192  log_info("DHCPv4-response from %s missing "
2193  "DHCPv4 Message option.",
2194  piaddr(packet->client_addr));
2195  return;
2196  }
2197 
2198  memset(&enc_opt_data, 0, sizeof(enc_opt_data));
2199  if (!evaluate_option_cache(&enc_opt_data, NULL, NULL, NULL,
2200  NULL, NULL, &global_scope, oc, MDL)) {
2201  log_error("forw_dhcpv4_response: error evaluating "
2202  "DHCPv4 message.");
2203  data_string_forget(&enc_opt_data, MDL);
2204  return;
2205  }
2206 
2207  if (enc_opt_data.len < DHCP_FIXED_NON_UDP) {
2208  log_error("forw_dhcpv4_response: "
2209  "no memory for encapsulated packet.");
2210  data_string_forget(&enc_opt_data, MDL);
2211  return;
2212  }
2213 
2214  /*
2215  * Append address.
2216  */
2217  memset(&ds, 0, sizeof(ds));
2218  if (!buffer_allocate(&ds.buffer, enc_opt_data.len + 16, MDL)) {
2219  log_error("forw_dhcpv4_response: no memory buffer.");
2220  data_string_forget(&enc_opt_data, MDL);
2221  return;
2222  }
2223  ds.data = ds.buffer->data;
2224  ds.len = enc_opt_data.len + 16;
2225  memcpy(ds.buffer->data, enc_opt_data.data, enc_opt_data.len);
2226  memcpy(ds.buffer->data + enc_opt_data.len,
2227  packet->client_addr.iabuf, 16);
2228  data_string_forget(&enc_opt_data, MDL);
2229 
2230  /*
2231  * Forward them.
2232  */
2233  cc = send(dhcp4o6_fd, ds.data, ds.len, 0);
2234  if (cc < 0)
2235  log_error("forw_dhcpv4_response: send(): %m");
2236 
2237  data_string_forget(&ds, MDL);
2238 }
2239 
2240 /*
2241  * \brief Receive a DHCPv4-response from the DHCPv6 client.
2242  * (DHCPv4 client function)
2243  *
2244  * The DHCPv4 client receives a DHCPv4-response forwarded
2245  * by the DHCPv6 client (using \ref forw_dhcpv4_response())
2246  *
2247  * \param raw the DHCPv4-response raw packet
2248  */
2249 static void recv_dhcpv4_response(struct data_string *raw)
2250 {
2251  struct packet *packet;
2252  struct iaddr from;
2253 
2254  if (interfaces == NULL) {
2255  log_error("recv_dhcpv4_response: no interfaces.");
2256  return;
2257  }
2258 
2259  from.len = 16;
2260  memcpy(from.iabuf, raw->data + (raw->len - 16), 16);
2261 
2262  /*
2263  * Build a packet structure.
2264  */
2265  packet = NULL;
2266  if (!packet_allocate(&packet, MDL)) {
2267  log_error("recv_dhcpv4_response: no memory for packet.");
2268  return;
2269  }
2270 
2271  packet->raw = (struct dhcp_packet *) raw->data;
2272  packet->packet_length = raw->len - 16;
2273  packet->client_port = remote_port;
2274  packet->client_addr = from;
2275  interface_reference(&packet->interface, interfaces, MDL);
2276 
2277  /* Allocate packet->options now so it is non-null for all packets */
2278  if (!option_state_allocate (&packet->options, MDL)) {
2279  log_error("recv_dhcpv4_response: no memory for options.");
2280  packet_dereference (&packet, MDL);
2281  return;
2282  }
2283 
2284  /* If there's an option buffer, try to parse it. */
2285  if (packet->packet_length >= DHCP_FIXED_NON_UDP + 4) {
2286  struct option_cache *op;
2287  if (!parse_options(packet)) {
2288  if (packet->options)
2290  (&packet->options, MDL);
2291  packet_dereference (&packet, MDL);
2292  return;
2293  }
2294 
2295  if (packet->options_valid &&
2297  packet->options,
2299  struct data_string dp;
2300  memset(&dp, 0, sizeof dp);
2301  evaluate_option_cache(&dp, packet, NULL, NULL,
2302  packet->options, NULL,
2303  NULL, op, MDL);
2304  if (dp.len > 0)
2305  packet->packet_type = dp.data[0];
2306  else
2307  packet->packet_type = 0;
2308  data_string_forget(&dp, MDL);
2309  }
2310  }
2311 
2312  if (validate_packet(packet) != 0) {
2313  if (packet->packet_type)
2314  dhcp(packet);
2315  else
2316  bootp(packet);
2317  }
2318 
2319  /* If the caller kept the packet, they'll have upped the refcnt. */
2320  packet_dereference(&packet, MDL);
2321 }
2322 #endif /* DHCP4o6 */
2323 #endif /* DHCPv6 */
2324 
2325 void dhcpoffer (packet)
2326  struct packet *packet;
2327 {
2328  struct interface_info *ip = packet -> interface;
2329  struct client_state *client;
2330  struct client_lease *lease, *lp;
2331  struct option **req;
2332  int i;
2333  int stop_selecting;
2334  const char *name = packet -> packet_type ? "DHCPOFFER" : "BOOTREPLY";
2335  char obuf [1024];
2336  struct timeval tv;
2337 
2338 #ifdef DEBUG_PACKET
2339  dump_packet (packet);
2340 #endif
2341 
2342  /* Find a client state that matches the xid... */
2343  for (client = ip -> client; client; client = client -> next)
2344  if (client -> xid == packet -> raw -> xid)
2345  break;
2346 
2347  /* If we're not receptive to an offer right now, or if the offer
2348  has an unrecognizable transaction id, then just drop it. */
2349  if (!client ||
2350  client -> state != S_SELECTING ||
2351  (packet -> interface -> hw_address.hlen - 1 !=
2352  packet -> raw -> hlen) ||
2353  (memcmp (&packet -> interface -> hw_address.hbuf [1],
2354  packet -> raw -> chaddr, packet -> raw -> hlen))) {
2355 #if defined (DEBUG)
2356  log_debug ("%s in wrong transaction.", name);
2357 #endif
2358  return;
2359  }
2360 
2361  sprintf (obuf, "%s from %s", name, piaddr (packet -> client_addr));
2362 
2363 
2364  /* If this lease doesn't supply the minimum required DHCPv4 parameters,
2365  * ignore it.
2366  */
2367  req = client->config->required_options;
2368  if (req != NULL) {
2369  for (i = 0 ; req[i] != NULL ; i++) {
2370  if ((req[i]->universe == &dhcp_universe) &&
2371  !lookup_option(&dhcp_universe, packet->options,
2372  req[i]->code)) {
2373  struct option *option = NULL;
2374  unsigned code = req[i]->code;
2375 
2376  option_code_hash_lookup(&option,
2378  &code, 0, MDL);
2379 
2380  if (option)
2381  log_info("%s: no %s option.", obuf,
2382  option->name);
2383  else
2384  log_info("%s: no unknown-%u option.",
2385  obuf, code);
2386 
2387  option_dereference(&option, MDL);
2388 
2389  return;
2390  }
2391  }
2392  }
2393 
2394  /* If we've already seen this lease, don't record it again. */
2395  for (lease = client -> offered_leases; lease; lease = lease -> next) {
2396  if (lease -> address.len == sizeof packet -> raw -> yiaddr &&
2397  !memcmp (lease -> address.iabuf,
2398  &packet -> raw -> yiaddr, lease -> address.len)) {
2399  log_debug ("%s: already seen.", obuf);
2400  return;
2401  }
2402  }
2403 
2404  lease = packet_to_lease (packet, client);
2405  if (!lease) {
2406  log_info ("%s: packet_to_lease failed.", obuf);
2407  return;
2408  }
2409 
2410  /* If this lease was acquired through a BOOTREPLY, record that
2411  fact. */
2412  if (!packet -> options_valid || !packet -> packet_type)
2413  lease -> is_bootp = 1;
2414 
2415  /* Record the medium under which this lease was offered. */
2416  lease -> medium = client -> medium;
2417 
2418  /* Figure out when we're supposed to stop selecting. */
2419  stop_selecting = (client -> first_sending +
2420  client -> config -> select_interval);
2421 
2422  /* If this is the lease we asked for, put it at the head of the
2423  list, and don't mess with the arp request timeout. */
2424  if (lease -> address.len == client -> requested_address.len &&
2425  !memcmp (lease -> address.iabuf,
2426  client -> requested_address.iabuf,
2427  client -> requested_address.len)) {
2428  lease -> next = client -> offered_leases;
2429  client -> offered_leases = lease;
2430  } else {
2431  /* Put the lease at the end of the list. */
2432  lease -> next = (struct client_lease *)0;
2433  if (!client -> offered_leases)
2434  client -> offered_leases = lease;
2435  else {
2436  for (lp = client -> offered_leases; lp -> next;
2437  lp = lp -> next)
2438  ;
2439  lp -> next = lease;
2440  }
2441  }
2442 
2443  /* If the selecting interval has expired, go immediately to
2444  state_selecting(). Otherwise, time out into
2445  state_selecting at the select interval. */
2446  if (stop_selecting <= cur_tv.tv_sec)
2447  state_selecting (client);
2448  else {
2449  tv.tv_sec = stop_selecting;
2450  tv.tv_usec = cur_tv.tv_usec;
2451  add_timeout(&tv, state_selecting, client, 0, 0);
2452  cancel_timeout(send_discover, client);
2453  }
2454  log_info("%s", obuf);
2455 }
2456 
2457 /* Allocate a client_lease structure and initialize it from the parameters
2458  in the specified packet. */
2459 
2460 struct client_lease *packet_to_lease (packet, client)
2461  struct packet *packet;
2462  struct client_state *client;
2463 {
2464  struct client_lease *lease;
2465  unsigned i;
2466  struct option_cache *oc;
2467  struct option *option = NULL;
2468  struct data_string data;
2469 
2470  lease = (struct client_lease *)new_client_lease (MDL);
2471 
2472  if (!lease) {
2473  log_error("packet_to_lease: no memory to record lease.\n");
2474  return NULL;
2475  }
2476 
2477  memset(lease, 0, sizeof(*lease));
2478 
2479  /* Copy the lease options. */
2480  option_state_reference(&lease->options, packet->options, MDL);
2481 
2482  lease->address.len = sizeof(packet->raw->yiaddr);
2483  memcpy(lease->address.iabuf, &packet->raw->yiaddr,
2484  lease->address.len);
2485 
2486  lease->next_srv_addr.len = sizeof(packet->raw->siaddr);
2487  memcpy(lease->next_srv_addr.iabuf, &packet->raw->siaddr,
2488  lease->next_srv_addr.len);
2489 
2490  memset(&data, 0, sizeof(data));
2491 
2492  if (client -> config -> vendor_space_name) {
2494 
2495  /* See if there was a vendor encapsulation option. */
2496  oc = lookup_option (&dhcp_universe, lease -> options, i);
2497  if (oc &&
2498  client -> config -> vendor_space_name &&
2499  evaluate_option_cache (&data, packet,
2500  (struct lease *)0, client,
2501  packet -> options, lease -> options,
2502  &global_scope, oc, MDL)) {
2503  if (data.len) {
2504  if (!option_code_hash_lookup(&option,
2506  &i, 0, MDL))
2507  log_fatal("Unable to find VENDOR "
2508  "option (%s:%d).", MDL);
2510  (packet -> options, option,
2511  data.data, data.len, &dhcp_universe,
2512  client -> config -> vendor_space_name
2513  );
2514 
2515  option_dereference(&option, MDL);
2516  }
2517  data_string_forget (&data, MDL);
2518  }
2519  } else
2520  i = 0;
2521 
2522  /* Figure out the overload flag. */
2523  oc = lookup_option (&dhcp_universe, lease -> options,
2525  if (oc &&
2526  evaluate_option_cache (&data, packet, (struct lease *)0, client,
2527  packet -> options, lease -> options,
2528  &global_scope, oc, MDL)) {
2529  if (data.len > 0)
2530  i = data.data [0];
2531  else
2532  i = 0;
2533  data_string_forget (&data, MDL);
2534  } else
2535  i = 0;
2536 
2537  /* If the server name was filled out, copy it. */
2538  if (!(i & 2) && packet -> raw -> sname [0]) {
2539  unsigned len;
2540  /* Don't count on the NUL terminator. */
2541  for (len = 0; len < DHCP_SNAME_LEN; len++)
2542  if (!packet -> raw -> sname [len])
2543  break;
2544  lease -> server_name = dmalloc (len + 1, MDL);
2545  if (!lease -> server_name) {
2546  log_error ("dhcpoffer: no memory for server name.\n");
2547  destroy_client_lease (lease);
2548  return (struct client_lease *)0;
2549  } else {
2550  memcpy (lease -> server_name,
2551  packet -> raw -> sname, len);
2552  lease -> server_name [len] = 0;
2553  }
2554  }
2555 
2556  /* Ditto for the filename. */
2557  if (!(i & 1) && packet -> raw -> file [0]) {
2558  unsigned len;
2559  /* Don't count on the NUL terminator. */
2560  for (len = 0; len < DHCP_FILE_LEN; len++)
2561  if (!packet -> raw -> file [len])
2562  break;
2563  lease -> filename = dmalloc (len + 1, MDL);
2564  if (!lease -> filename) {
2565  log_error ("dhcpoffer: no memory for filename.\n");
2566  destroy_client_lease (lease);
2567  return (struct client_lease *)0;
2568  } else {
2569  memcpy (lease -> filename,
2570  packet -> raw -> file, len);
2571  lease -> filename [len] = 0;
2572  }
2573  }
2574 
2575  execute_statements_in_scope(NULL, (struct packet *)packet, NULL,
2576  client, lease->options, lease->options,
2577  &global_scope, client->config->on_receipt,
2578  NULL, NULL);
2579 
2580  return lease;
2581 }
2582 
2583 void dhcpnak (packet)
2584  struct packet *packet;
2585 {
2586  struct interface_info *ip = packet -> interface;
2587  struct client_state *client;
2588 
2589  /* Find a client state that matches the xid... */
2590  for (client = ip -> client; client; client = client -> next)
2591  if (client -> xid == packet -> raw -> xid)
2592  break;
2593 
2594  /* If we're not receptive to an offer right now, or if the offer
2595  has an unrecognizable transaction id, then just drop it. */
2596  if (!client ||
2597  (packet -> interface -> hw_address.hlen - 1 !=
2598  packet -> raw -> hlen) ||
2599  (memcmp (&packet -> interface -> hw_address.hbuf [1],
2600  packet -> raw -> chaddr, packet -> raw -> hlen))) {
2601 #if defined (DEBUG)
2602  log_debug ("DHCPNAK in wrong transaction.");
2603 #endif
2604  return;
2605  }
2606 
2607  if (client -> state != S_REBOOTING &&
2608  client -> state != S_REQUESTING &&
2609  client -> state != S_RENEWING &&
2610  client -> state != S_REBINDING) {
2611 #if defined (DEBUG)
2612  log_debug ("DHCPNAK in wrong state.");
2613 #endif
2614  return;
2615  }
2616 
2617  log_info ("DHCPNAK from %s (xid=0x%x)", piaddr (packet -> client_addr), ntohl(client -> xid));
2618 
2619  if (!client -> active) {
2620 #if defined (DEBUG)
2621  log_info ("DHCPNAK with no active lease.\n");
2622 #endif
2623  return;
2624  }
2625 
2626  /* If we get a DHCPNAK, we use the EXPIRE dhclient-script state
2627  * to indicate that we want all old bindings to be removed. (It
2628  * is possible that we may get a NAK while in the RENEW state,
2629  * so we might have bindings active at that time)
2630  */
2631  script_init(client, "EXPIRE", NULL);
2632  script_write_params(client, "old_", client->active);
2633  script_write_requested(client);
2634  if (client->alias)
2635  script_write_params(client, "alias_", client->alias);
2636  script_go(client);
2637 
2638  destroy_client_lease (client -> active);
2639  client -> active = (struct client_lease *)0;
2640 
2641  /* Stop sending DHCPREQUEST packets... */
2642  cancel_timeout (send_request, client);
2643 
2644  /* On some scripts, 'EXPIRE' causes the interface to be ifconfig'd
2645  * down (this expunges any routes and arp cache). This makes the
2646  * interface unusable by state_init(), which we call next. So, we
2647  * need to 'PREINIT' the interface to bring it back up.
2648  */
2649  script_init(client, "PREINIT", NULL);
2650  if (client->alias)
2651  script_write_params(client, "alias_", client->alias);
2652  script_go(client);
2653 
2654  client -> state = S_INIT;
2655  state_init (client);
2656 }
2657 
2658 /* Send out a DHCPDISCOVER packet, and set a timeout to send out another
2659  one after the right interval has expired. If we don't get an offer by
2660  the time we reach the panic interval, call the panic function. */
2661 
2662 void send_discover (cpp)
2663  void *cpp;
2664 {
2665  struct client_state *client = cpp;
2666 
2667  int result;
2668  int interval;
2669  int increase = 1;
2670  struct timeval tv;
2671 
2672  /* Figure out how long it's been since we started transmitting. */
2673  interval = cur_time - client -> first_sending;
2674 
2675  /* If we're past the panic timeout, call the script and tell it
2676  we haven't found anything for this interface yet. */
2677  if (interval > client -> config -> timeout) {
2678  state_panic (client);
2679  return;
2680  }
2681 
2682  /* If we're selecting media, try the whole list before doing
2683  the exponential backoff, but if we've already received an
2684  offer, stop looping, because we obviously have it right. */
2685  if (!client -> offered_leases &&
2686  client -> config -> media) {
2687  int fail = 0;
2688  again:
2689  if (client -> medium) {
2690  client -> medium = client -> medium -> next;
2691  increase = 0;
2692  }
2693  if (!client -> medium) {
2694  if (fail)
2695  log_fatal ("No valid media types for %s!",
2696  client -> interface -> name);
2697  client -> medium =
2698  client -> config -> media;
2699  increase = 1;
2700  }
2701 
2702  log_info ("Trying medium \"%s\" %d",
2703  client -> medium -> string, increase);
2704  script_init(client, "MEDIUM", client -> medium);
2705  if (script_go(client)) {
2706  fail = 1;
2707  goto again;
2708  }
2709  }
2710 
2711  /* If we're supposed to increase the interval, do so. If it's
2712  currently zero (i.e., we haven't sent any packets yet), set
2713  it to initial_interval; otherwise, add to it a random number
2714  between zero and two times itself. On average, this means
2715  that it will double with every transmission. */
2716  if (increase) {
2717  if (!client->interval)
2718  client->interval = client->config->initial_interval;
2719  else
2720  client->interval += random() % (2 * client->interval);
2721 
2722  /* Don't backoff past cutoff. */
2723  if (client->interval > client->config->backoff_cutoff)
2724  client->interval = (client->config->backoff_cutoff / 2)
2725  + (random() % client->config->backoff_cutoff);
2726  } else if (!client->interval)
2727  client->interval = client->config->initial_interval;
2728 
2729  /* If the backoff would take us to the panic timeout, just use that
2730  as the interval. */
2731  if (cur_time + client -> interval >
2732  client -> first_sending + client -> config -> timeout)
2733  client -> interval =
2734  (client -> first_sending +
2735  client -> config -> timeout) - cur_time + 1;
2736 
2737  /* Record the number of seconds since we started sending. */
2738  if (interval < 65536)
2739  client -> packet.secs = htons (interval);
2740  else
2741  client -> packet.secs = htons (65535);
2742  client -> secs = client -> packet.secs;
2743 
2744 #if defined(DHCPv6) && defined(DHCP4o6)
2745  if (dhcpv4_over_dhcpv6) {
2746  log_info ("DHCPDISCOVER interval %ld",
2747  (long)(client -> interval));
2748  } else
2749 #endif
2750  log_info ("DHCPDISCOVER on %s to %s port %d interval %ld (xid=0x%x)",
2751  client -> name ? client -> name : client -> interface -> name,
2752  inet_ntoa (sockaddr_broadcast.sin_addr),
2753  ntohs (sockaddr_broadcast.sin_port), (long)(client -> interval), ntohl(client -> xid));
2754 
2755  /* Send out a packet. */
2756 #if defined(DHCPv6) && defined(DHCP4o6)
2757  if (dhcpv4_over_dhcpv6) {
2758  result = send_dhcpv4_query(client, 1);
2759  } else
2760 #endif
2761  result = send_packet(client->interface, NULL, &client->packet,
2762  client->packet_length, inaddr_any,
2763  &sockaddr_broadcast, NULL);
2764  if (result < 0) {
2765 #if defined(DHCPv6) && defined(DHCP4o6)
2766  if (dhcpv4_over_dhcpv6) {
2767  log_error("%s:%d: Failed to send %d byte long packet.",
2768  MDL, client->packet_length);
2769  } else
2770 #endif
2771  log_error("%s:%d: Failed to send %d byte long packet over %s "
2772  "interface.", MDL, client->packet_length,
2773  client->interface->name);
2774  }
2775 
2776  /*
2777  * If we used 0 microseconds here, and there were other clients on the
2778  * same network with a synchronized local clock (ntp), and a similar
2779  * zero-microsecond-scheduler behavior, then we could be participating
2780  * in a sub-second DOS ttck.
2781  */
2782  tv.tv_sec = cur_tv.tv_sec + client->interval;
2783  tv.tv_usec = client->interval > 1 ? random() % 1000000 : cur_tv.tv_usec;
2784  add_timeout(&tv, send_discover, client, 0, 0);
2785 }
2786 
2787 /* state_panic gets called if we haven't received any offers in a preset
2788  amount of time. When this happens, we try to use existing leases that
2789  haven't yet expired, and failing that, we call the client script and
2790  hope it can do something. */
2791 
2792 void state_panic (cpp)
2793  void *cpp;
2794 {
2795  struct client_state *client = cpp;
2796  struct client_lease *loop;
2797  struct client_lease *lp;
2798  struct timeval tv;
2799 
2800  loop = lp = client -> active;
2801 
2802  log_info ("No DHCPOFFERS received.");
2803 
2804  /* We may not have an active lease, but we may have some
2805  predefined leases that we can try. */
2806  if (!client -> active && client -> leases)
2807  goto activate_next;
2808 
2809  /* Run through the list of leases and see if one can be used. */
2810  while (client -> active) {
2811  if (client -> active -> expiry > cur_time) {
2812  log_info ("Trying recorded lease %s",
2813  piaddr (client -> active -> address));
2814  /* Run the client script with the existing
2815  parameters. */
2816  script_init(client, "TIMEOUT",
2817  client -> active -> medium);
2818  script_write_params(client, "new_", client -> active);
2819  script_write_requested(client);
2820  if (client -> alias)
2821  script_write_params(client, "alias_",
2822  client -> alias);
2823 
2824  /* If the old lease is still good and doesn't
2825  yet need renewal, go into BOUND state and
2826  timeout at the renewal time. */
2827  if (!script_go(client)) {
2828  if (cur_time < client -> active -> renewal) {
2829  client -> state = S_BOUND;
2830  log_info ("bound: renewal in %ld %s.",
2831  (long)(client -> active -> renewal -
2832  cur_time), "seconds");
2833  tv.tv_sec = client->active->renewal;
2834  tv.tv_usec = ((client->active->renewal -
2835  cur_time) > 1) ?
2836  random() % 1000000 :
2837  cur_tv.tv_usec;
2838  add_timeout(&tv, state_bound, client, 0, 0);
2839  } else {
2840  client -> state = S_BOUND;
2841  log_info ("bound: immediate renewal.");
2842  state_bound (client);
2843  }
2845  go_daemon ();
2846  return;
2847  }
2848  }
2849 
2850  /* If there are no other leases, give up. */
2851  if (!client -> leases) {
2852  client -> leases = client -> active;
2853  client -> active = (struct client_lease *)0;
2854  break;
2855  }
2856 
2857  activate_next:
2858  /* Otherwise, put the active lease at the end of the
2859  lease list, and try another lease.. */
2860  for (lp = client -> leases; lp -> next; lp = lp -> next)
2861  ;
2862  lp -> next = client -> active;
2863  if (lp -> next) {
2864  lp -> next -> next = (struct client_lease *)0;
2865  }
2866  client -> active = client -> leases;
2867  client -> leases = client -> leases -> next;
2868 
2869  /* If we already tried this lease, we've exhausted the
2870  set of leases, so we might as well give up for
2871  now. */
2872  if (client -> active == loop)
2873  break;
2874  else if (!loop)
2875  loop = client -> active;
2876  }
2877 
2878  /* No leases were available, or what was available didn't work, so
2879  tell the shell script that we failed to allocate an address,
2880  and try again later. */
2881  if (onetry) {
2882  if (!quiet) {
2883  log_info ("Unable to obtain a lease on first try.%s",
2884  " Exiting.");
2885  }
2886 
2887 #if defined (CALL_SCRIPT_ON_ONETRY_FAIL)
2888  /* Let's call a script and we're done */
2889  script_init(client, "FAIL", (struct string_list *)0);
2890  script_go(client);
2891 #endif
2892  exit (2);
2893  }
2894 
2895  log_info ("No working leases in persistent database - sleeping.");
2896  script_init(client, "FAIL", (struct string_list *)0);
2897  if (client -> alias)
2898  script_write_params(client, "alias_", client -> alias);
2899  script_go(client);
2900  client -> state = S_INIT;
2901  tv.tv_sec = cur_tv.tv_sec + ((client->config->retry_interval + 1) / 2 +
2902  (random() % client->config->retry_interval));
2903  tv.tv_usec = ((tv.tv_sec - cur_tv.tv_sec) > 1) ?
2904  random() % 1000000 : cur_tv.tv_usec;
2905  add_timeout(&tv, state_init, client, 0, 0);
2906  go_daemon ();
2907 }
2908 
2909 void send_request (cpp)
2910  void *cpp;
2911 {
2912  struct client_state *client = cpp;
2913 
2914  int result;
2915  int interval;
2916  struct sockaddr_in destination;
2917  struct in_addr from;
2918  struct timeval tv;
2919 
2920  /* Figure out how long it's been since we started transmitting. */
2921  interval = cur_time - client -> first_sending;
2922 
2923  /* If we're in the INIT-REBOOT or REQUESTING state and we're
2924  past the reboot timeout, go to INIT and see if we can
2925  DISCOVER an address... */
2926  /* XXX In the INIT-REBOOT state, if we don't get an ACK, it
2927  means either that we're on a network with no DHCP server,
2928  or that our server is down. In the latter case, assuming
2929  that there is a backup DHCP server, DHCPDISCOVER will get
2930  us a new address, but we could also have successfully
2931  reused our old address. In the former case, we're hosed
2932  anyway. This is not a win-prone situation. */
2933  if ((client -> state == S_REBOOTING ||
2934  client -> state == S_REQUESTING) &&
2935  interval > client -> config -> reboot_timeout) {
2936  cancel:
2937  client -> state = S_INIT;
2938  cancel_timeout (send_request, client);
2939  state_init (client);
2940  return;
2941  }
2942 
2943  /* If we're in the reboot state, make sure the media is set up
2944  correctly. */
2945  if (client -> state == S_REBOOTING &&
2946  !client -> medium &&
2947  client -> active -> medium ) {
2948  script_init(client, "MEDIUM", client -> active -> medium);
2949 
2950  /* If the medium we chose won't fly, go to INIT state. */
2951  if (script_go(client))
2952  goto cancel;
2953 
2954  /* Record the medium. */
2955  client -> medium = client -> active -> medium;
2956  }
2957 
2958  /* If the lease has expired, relinquish the address and go back
2959  to the INIT state. */
2960  if (client -> state != S_REQUESTING &&
2961  cur_time > client -> active -> expiry) {
2962  /* Run the client script with the new parameters. */
2963  script_init(client, "EXPIRE", (struct string_list *)0);
2964  script_write_params(client, "old_", client -> active);
2965  script_write_requested(client);
2966  if (client -> alias)
2967  script_write_params(client, "alias_",
2968  client -> alias);
2969  script_go(client);
2970 
2971  /* Now do a preinit on the interface so that we can
2972  discover a new address. */
2973  script_init(client, "PREINIT", (struct string_list *)0);
2974  if (client -> alias)
2975  script_write_params(client, "alias_",
2976  client -> alias);
2977  script_go(client);
2978 
2979  client -> state = S_INIT;
2980  state_init (client);
2981  return;
2982  }
2983 
2984  /* Do the exponential backoff... */
2985  if (!client -> interval)
2986  client -> interval = client -> config -> initial_interval;
2987  else {
2988  client -> interval += ((random () >> 2) %
2989  (2 * client -> interval));
2990  }
2991 
2992  /* Don't backoff past cutoff. */
2993  if (client -> interval >
2994  client -> config -> backoff_cutoff)
2995  client -> interval =
2996  ((client -> config -> backoff_cutoff / 2)
2997  + ((random () >> 2) %
2998  client -> config -> backoff_cutoff));
2999 
3000  /* If the backoff would take us to the expiry time, just set the
3001  timeout to the expiry time. */
3002  if (client -> state != S_REQUESTING &&
3003  cur_time + client -> interval > client -> active -> expiry)
3004  client -> interval =
3005  client -> active -> expiry - cur_time + 1;
3006 
3007  /* If the lease T2 time has elapsed, or if we're not yet bound,
3008  broadcast the DHCPREQUEST rather than unicasting. */
3009  if (client -> state == S_REQUESTING ||
3010  client -> state == S_REBOOTING ||
3011  cur_time > client -> active -> rebind)
3012  destination.sin_addr = sockaddr_broadcast.sin_addr;
3013  else
3014  memcpy (&destination.sin_addr.s_addr,
3015  client -> destination.iabuf,
3016  sizeof destination.sin_addr.s_addr);
3017  destination.sin_port = remote_port;
3018  destination.sin_family = AF_INET;
3019 #ifdef HAVE_SA_LEN
3020  destination.sin_len = sizeof destination;
3021 #endif
3022 
3023  if (client -> state == S_RENEWING ||
3024  client -> state == S_REBINDING)
3025  memcpy (&from, client -> active -> address.iabuf,
3026  sizeof from);
3027  else
3028  from.s_addr = INADDR_ANY;
3029 
3030  /* Record the number of seconds since we started sending. */
3031  if (client -> state == S_REQUESTING)
3032  client -> packet.secs = client -> secs;
3033  else {
3034  if (interval < 65536)
3035  client -> packet.secs = htons (interval);
3036  else
3037  client -> packet.secs = htons (65535);
3038  }
3039 
3040 #if defined(DHCPv6) && defined(DHCP4o6)
3041  if (dhcpv4_over_dhcpv6) {
3042  log_info ("DHCPREQUEST");
3043  } else
3044 #endif
3045  log_info ("DHCPREQUEST on %s to %s port %d (xid=0x%x)",
3046  client -> name ? client -> name : client -> interface -> name,
3047  inet_ntoa (destination.sin_addr),
3048  ntohs (destination.sin_port), ntohl(client -> xid));
3049 
3050 #if defined(DHCPv6) && defined(DHCP4o6)
3051  if (dhcpv4_over_dhcpv6) {
3052  int broadcast = 0;
3053  if (destination.sin_addr.s_addr == INADDR_BROADCAST)
3054  broadcast = 1;
3055  result = send_dhcpv4_query(client, broadcast);
3056  if (result < 0) {
3057  log_error("%s:%d: Failed to send %d byte long packet.",
3058  MDL, client->packet_length);
3059  }
3060  } else
3061 #endif
3062  if (destination.sin_addr.s_addr != INADDR_BROADCAST &&
3064 #if defined(SO_BINDTODEVICE)
3065  if (setsockopt(fallback_interface -> wfdesc, SOL_SOCKET,
3066  SO_BINDTODEVICE, client->interface->name,
3067  strlen(client->interface->name)) < 0) {
3068  log_error("%s:%d: Failed to bind fallback interface"
3069  " to %s: %m", MDL, client->interface->name);
3070  }
3071 #endif
3072  result = send_packet(fallback_interface, NULL, &client->packet,
3073  client->packet_length, from, &destination,
3074  NULL);
3075  if (result < 0) {
3076  log_error("%s:%d: Failed to send %d byte long packet "
3077  "over %s interface.", MDL,
3078  client->packet_length,
3080  }
3081 #if defined(SO_BINDTODEVICE)
3082  if (setsockopt(fallback_interface -> wfdesc, SOL_SOCKET,
3083  SO_BINDTODEVICE, NULL, 0) < 0) {
3084  log_fatal("%s:%d: Failed to unbind fallback interface:"
3085  " %m", MDL);
3086  }
3087 #endif
3088  }
3089  else {
3090  /* Send out a packet. */
3091  result = send_packet(client->interface, NULL, &client->packet,
3092  client->packet_length, from, &destination,
3093  NULL);
3094  if (result < 0) {
3095  log_error("%s:%d: Failed to send %d byte long packet"
3096  " over %s interface.", MDL,
3097  client->packet_length,
3098  client->interface->name);
3099  }
3100  }
3101 
3102  tv.tv_sec = cur_tv.tv_sec + client->interval;
3103  tv.tv_usec = ((tv.tv_sec - cur_tv.tv_sec) > 1) ?
3104  random() % 1000000 : cur_tv.tv_usec;
3105  add_timeout(&tv, send_request, client, 0, 0);
3106 }
3107 
3108 void send_decline (cpp)
3109  void *cpp;
3110 {
3111  struct client_state *client = cpp;
3112 
3113  int result;
3114 
3115 #if defined(DHCPv6) && defined(DHCP4o6)
3116  if (dhcpv4_over_dhcpv6) {
3117  log_info ("DHCPDECLINE");
3118  } else
3119 #endif
3120  log_info ("DHCPDECLINE on %s to %s port %d (xid=0x%x)",
3121  client->name ? client->name : client->interface->name,
3122  inet_ntoa(sockaddr_broadcast.sin_addr),
3123  ntohs(sockaddr_broadcast.sin_port), ntohl(client -> xid));
3124 
3125  /* Send out a packet. */
3126 #if defined(DHCPv6) && defined(DHCP4o6)
3127  if (dhcpv4_over_dhcpv6) {
3128  result = send_dhcpv4_query(client, 1);
3129  } else
3130 #endif
3131  result = send_packet(client->interface, NULL, &client->packet,
3132  client->packet_length, inaddr_any,
3133  &sockaddr_broadcast, NULL);
3134  if (result < 0) {
3135 #if defined(DHCPv6) && defined(DHCP4o6)
3136  if (dhcpv4_over_dhcpv6) {
3137  log_error("%s:%d: Failed to send %d byte long packet.",
3138  MDL, client->packet_length);
3139  } else
3140 #endif
3141  log_error("%s:%d: Failed to send %d byte long packet over %s"
3142  " interface.", MDL, client->packet_length,
3143  client->interface->name);
3144  }
3145 }
3146 
3147 void send_release (cpp)
3148  void *cpp;
3149 {
3150  struct client_state *client = cpp;
3151 
3152  int result;
3153  struct sockaddr_in destination;
3154  struct in_addr from;
3155 
3156  memcpy (&from, client -> active -> address.iabuf,
3157  sizeof from);
3158  memcpy (&destination.sin_addr.s_addr,
3159  client -> destination.iabuf,
3160  sizeof destination.sin_addr.s_addr);
3161  destination.sin_port = remote_port;
3162  destination.sin_family = AF_INET;
3163 #ifdef HAVE_SA_LEN
3164  destination.sin_len = sizeof destination;
3165 #endif
3166 
3167  /* Set the lease to end now, so that we don't accidentally
3168  reuse it if we restart before the old expiry time. */
3169  client -> active -> expiry =
3170  client -> active -> renewal =
3171  client -> active -> rebind = cur_time;
3172  if (!write_client_lease (client, client -> active, 1, 1)) {
3173  log_error ("Can't release lease: lease write failed.");
3174  return;
3175  }
3176 
3177 #if defined(DHCPv6) && defined(DHCP4o6)
3178  if (dhcpv4_over_dhcpv6) {
3179  log_info ("DHCPRELEASE");
3180  } else
3181 #endif
3182  log_info ("DHCPRELEASE on %s to %s port %d (xid=0x%x)",
3183  client -> name ? client -> name : client -> interface -> name,
3184  inet_ntoa (destination.sin_addr),
3185  ntohs (destination.sin_port), ntohl(client -> xid));
3186 
3187 #if defined(DHCPv6) && defined(DHCP4o6)
3188  if (dhcpv4_over_dhcpv6) {
3189  int broadcast = 0;
3190  if (destination.sin_addr.s_addr == INADDR_BROADCAST)
3191  broadcast = 1;
3192  result = send_dhcpv4_query(client, broadcast);
3193  if (result < 0) {
3194  log_error("%s:%d: Failed to send %d byte long packet.",
3195  MDL, client->packet_length);
3196  }
3197  } else
3198 #endif
3199  if (fallback_interface) {
3200 #if defined(SO_BINDTODEVICE)
3201  if (setsockopt(fallback_interface -> wfdesc, SOL_SOCKET,
3202  SO_BINDTODEVICE, client->interface->name,
3203  strlen(client->interface->name)) < 0) {
3204  log_error("%s:%d: Failed to bind fallback interface"
3205  " to %s: %m", MDL, client->interface->name);
3206  }
3207 #endif
3208  result = send_packet(fallback_interface, NULL, &client->packet,
3209  client->packet_length, from, &destination,
3210  NULL);
3211  if (result < 0) {
3212  log_error("%s:%d: Failed to send %d byte long packet"
3213  " over %s interface.", MDL,
3214  client->packet_length,
3216  }
3217 #if defined(SO_BINDTODEVICE)
3218  if (setsockopt(fallback_interface -> wfdesc, SOL_SOCKET,
3219  SO_BINDTODEVICE, NULL, 0) < 0) {
3220  log_fatal("%s:%d: Failed to unbind fallback interface:"
3221  " %m", MDL);
3222  }
3223 #endif
3224  } else {
3225  /* Send out a packet. */
3226  result = send_packet(client->interface, NULL, &client->packet,
3227  client->packet_length, from, &destination,
3228  NULL);
3229  if (result < 0) {
3230  log_error ("%s:%d: Failed to send %d byte long packet"
3231  " over %s interface.", MDL,
3232  client->packet_length,
3233  client->interface->name);
3234  }
3235 
3236  }
3237 }
3238 
3239 #if defined(DHCPv6) && defined(DHCP4o6)
3240 /*
3241  * \brief Send a DHCPv4-query to the DHCPv6 client
3242  * (DHCPv4 client function)
3243  *
3244  * The DHCPv4 client sends a DHCPv4-query to the DHCPv6 client over
3245  * the inter-process communication socket.
3246  *
3247  * \param client the DHCPv4 client state
3248  * \param broadcast the broadcast flag
3249  * \return the sent byte count (-1 on error)
3250  */
3251 static int send_dhcpv4_query(struct client_state *client, int broadcast) {
3252  struct data_string ds;
3253  struct dhcpv4_over_dhcpv6_packet *query;
3254  int ofs, len, cc;
3255 
3256  if (dhcp4o6_state <= 0) {
3257  log_info("send_dhcpv4_query: not ready.");
3258  return -1;
3259  }
3260 
3261  /*
3262  * Compute buffer length and allocate it.
3263  */
3264  len = ofs = (int)(offsetof(struct dhcpv4_over_dhcpv6_packet, options));
3266  len += client->packet_length;
3267  memset(&ds, 0, sizeof(ds));
3268  if (!buffer_allocate(&ds.buffer, len, MDL)) {
3269  log_error("Unable to allocate memory for DHCPv4-query.");
3270  return -1;
3271  }
3272  ds.data = ds.buffer->data;
3273  ds.len = len;
3274 
3275  /*
3276  * Fill header.
3277  */
3278  query = (struct dhcpv4_over_dhcpv6_packet *)ds.data;
3279  query->msg_type = DHCPV6_DHCPV4_QUERY;
3280  query->flags[0] = query->flags[1] = query->flags[2] = 0;
3281  if (!broadcast)
3282  query->flags[0] |= DHCP4O6_QUERY_UNICAST;
3283 
3284  /*
3285  * Append DHCPv4 message.
3286  */
3288  ofs += dhcpv6_universe.tag_size;
3290  client->packet_length);
3292  memcpy(ds.buffer->data + ofs, &client->packet, client->packet_length);
3293 
3294  /*
3295  * Send DHCPv6 message.
3296  */
3297  cc = send(dhcp4o6_fd, ds.data, ds.len, 0);
3298  if (cc < 0)
3299  log_error("send_dhcpv4_query: send(): %m");
3300 
3301  data_string_forget(&ds, MDL);
3302 
3303  return cc;
3304 }
3305 
3306 /*
3307  * \brief Forward a DHCPv4-query to all DHCPv4 over DHCPv6 server addresses.
3308  * (DHCPv6 client function)
3309  *
3310  * \param raw the DHCPv6 DHCPv4-query message raw content
3311  */
3312 static void forw_dhcpv4_query(struct data_string *raw) {
3313  struct interface_info *ip;
3314  struct client_state *client;
3315  struct dhc6_lease *lease;
3316  struct option_cache *oc;
3317  struct data_string addrs;
3318  struct sockaddr_in6 sin6;
3319  int i, send_ret, attempt, success;
3320 
3321  attempt = success = 0;
3322  memset(&sin6, 0, sizeof(sin6));
3323  sin6.sin6_family = AF_INET6;
3324  sin6.sin6_port = remote_port;
3325 #ifdef HAVE_SA_LEN
3326  sin6.sin6_len = sizeof(sin6);
3327 #endif
3328  memset(&addrs, 0, sizeof(addrs));
3329  for (ip = interfaces; ip != NULL; ip = ip->next) {
3330  for (client = ip->client; client != NULL;
3331  client = client->next) {
3332  if ((client->state != S_BOUND) &&
3333  (client->state != S_RENEWING) &&
3334  (client->state != S_REBINDING))
3335  continue;
3336  lease = client->active_lease;
3337  if ((lease == NULL) || lease->released)
3338  continue;
3340  lease->options,
3342  if ((oc == NULL) ||
3343  !evaluate_option_cache(&addrs, NULL, NULL, NULL,
3344  lease->options, NULL,
3345  &global_scope, oc, MDL) ||
3346  ((addrs.len % sizeof(sin6.sin6_addr)) != 0)) {
3347  data_string_forget(&addrs, MDL);
3348  continue;
3349  }
3350  if (addrs.len == 0) {
3351  /* note there is nothing to forget */
3352  inet_pton(AF_INET6,
3354  &sin6.sin6_addr);
3355  attempt++;
3356  send_ret = send_packet6(ip, raw->data,
3357  raw->len, &sin6);
3358  if (send_ret == raw->len)
3359  success++;
3360  continue;
3361  }
3362  for (i = 0; i < addrs.len;
3363  i += sizeof(sin6.sin6_addr)) {
3364  memcpy(&sin6.sin6_addr, addrs.data + i,
3365  sizeof(sin6.sin6_addr));
3366  attempt++;
3367  send_ret = send_packet6(ip, raw->data,
3368  raw->len, &sin6);
3369  if (send_ret == raw->len)
3370  success++;
3371  }
3372  data_string_forget(&addrs, MDL);
3373  }
3374  }
3375 
3376  log_info("forw_dhcpv4_query: sent(%d): %d/%d",
3377  raw->len, success, attempt);
3378 
3379  if (attempt == 0)
3380  dhcp4o6_stop();
3381 }
3382 #endif
3383 
3384 void
3385 make_client_options(struct client_state *client, struct client_lease *lease,
3386  u_int8_t *type, struct option_cache *sid,
3387  struct iaddr *rip, struct option **prl,
3388  struct option_state **op)
3389 {
3390  unsigned i;
3391  struct option_cache *oc;
3392  struct option *option = NULL;
3393  struct buffer *bp = NULL;
3394 
3395  /* If there are any leftover options, get rid of them. */
3396  if (*op)
3398 
3399  /* Allocate space for options. */
3401 
3402  /* Send the server identifier if provided. */
3403  if (sid)
3404  save_option(&dhcp_universe, *op, sid);
3405 
3406  oc = NULL;
3407 
3408  /* Send the requested address if provided. */
3409  if (rip) {
3410  client->requested_address = *rip;
3412  if (!(option_code_hash_lookup(&option, dhcp_universe.code_hash,
3413  &i, 0, MDL) &&
3414  make_const_option_cache(&oc, NULL, rip->iabuf, rip->len,
3415  option, MDL)))
3416  log_error ("can't make requested address cache.");
3417  else {
3418  save_option(&dhcp_universe, *op, oc);
3420  }
3421  option_dereference(&option, MDL);
3422  } else {
3423  client->requested_address.len = 0;
3424  }
3425 
3427  if (!(option_code_hash_lookup(&option, dhcp_universe.code_hash, &i, 0,
3428  MDL) &&
3429  make_const_option_cache(&oc, NULL, type, 1, option, MDL)))
3430  log_error("can't make message type.");
3431  else {
3432  save_option(&dhcp_universe, *op, oc);
3434  }
3435  option_dereference(&option, MDL);
3436 
3437  if (prl) {
3438  int len;
3439 
3440  /* Probe the length of the list. */
3441  len = 0;
3442  for (i = 0 ; prl[i] != NULL ; i++)
3443  if (prl[i]->universe == &dhcp_universe)
3444  len++;
3445 
3446  if (!buffer_allocate(&bp, len, MDL))
3447  log_error("can't make parameter list buffer.");
3448  else {
3449  unsigned code = DHO_DHCP_PARAMETER_REQUEST_LIST;
3450 
3451  len = 0;
3452  for (i = 0 ; prl[i] != NULL ; i++)
3453  if (prl[i]->universe == &dhcp_universe)
3454  bp->data[len++] = prl[i]->code;
3455 
3456  if (!(option_code_hash_lookup(&option,
3458  &code, 0, MDL) &&
3459  make_const_option_cache(&oc, &bp, NULL, len,
3460  option, MDL))) {
3461  if (bp != NULL)
3462  buffer_dereference(&bp, MDL);
3463  log_error ("can't make option cache");
3464  } else {
3465  save_option(&dhcp_universe, *op, oc);
3467  }
3468  option_dereference(&option, MDL);
3469  }
3470  }
3471 
3472  /*
3473  * If requested (duid_v4 == 1) add an RFC4361 compliant client-identifier
3474  * This can be overridden by including a client id in the configuration
3475  * file.
3476  */
3477  if (duid_v4 == 1) {
3478  struct data_string client_identifier;
3479  int hw_idx, hw_len;
3480 
3481  memset(&client_identifier, 0, sizeof(client_identifier));
3482  client_identifier.len = 1 + 4 + default_duid.len;
3483  if (!buffer_allocate(&client_identifier.buffer,
3484  client_identifier.len, MDL))
3485  log_fatal("no memory for default DUID!");
3486  client_identifier.data = client_identifier.buffer->data;
3487 
3489 
3490  /* Client-identifier type : 1 byte */
3491  *client_identifier.buffer->data = 255;
3492 
3493  /* IAID : 4 bytes
3494  * we use the low 4 bytes from the interface address
3495  */
3496  if (client->interface->hw_address.hlen > 4) {
3497  hw_idx = client->interface->hw_address.hlen - 4;
3498  hw_len = 4;
3499  } else {
3500  hw_idx = 0;
3501  hw_len = client->interface->hw_address.hlen;
3502  }
3503  memcpy(&client_identifier.buffer->data + 5 - hw_len,
3504  client->interface->hw_address.hbuf + hw_idx,
3505  hw_len);
3506 
3507  /* Add the default duid */
3508  memcpy(&client_identifier.buffer->data+(1+4),
3510 
3511  /* And save the option */
3512  if (!(option_code_hash_lookup(&option, dhcp_universe.code_hash,
3513  &i, 0, MDL) &&
3514  make_const_option_cache(&oc, NULL,
3515  (u_int8_t *)client_identifier.data,
3516  client_identifier.len,
3517  option, MDL)))
3518  log_error ("can't make requested client id cache..");
3519  else {
3520  save_option (&dhcp_universe, *op, oc);
3522  }
3523  option_dereference(&option, MDL);
3524  }
3525 
3526  /* Run statements that need to be run on transmission. */
3527  if (client->config->on_transmission)
3528  execute_statements_in_scope(NULL, NULL, NULL, client,
3529  (lease ? lease->options : NULL),
3530  *op, &global_scope,
3531  client->config->on_transmission,
3532  NULL, NULL);
3533 }
3534 
3535 void make_discover (client, lease)
3536  struct client_state *client;
3537  struct client_lease *lease;
3538 {
3539  unsigned char discover = DHCPDISCOVER;
3540  struct option_state *options = (struct option_state *)0;
3541 
3542  memset (&client -> packet, 0, sizeof (client -> packet));
3543 
3544  make_client_options (client,
3545  lease, &discover, (struct option_cache *)0,
3546  lease ? &lease -> address : (struct iaddr *)0,
3547  client -> config -> requested_options,
3548  &options);
3549 
3550  /* Set up the option buffer... */
3551  client -> packet_length =
3552  cons_options ((struct packet *)0, &client -> packet,
3553  (struct lease *)0, client,
3554  /* maximum packet size */1500,
3555  (struct option_state *)0,
3556  options,
3557  /* scope */ &global_scope,
3558  /* overload */ 0,
3559  /* terminate */0,
3560  /* bootpp */0,
3561  (struct data_string *)0,
3562  client -> config -> vendor_space_name);
3563 
3564  option_state_dereference (&options, MDL);
3565  if (client -> packet_length < BOOTP_MIN_LEN)
3566  client -> packet_length = BOOTP_MIN_LEN;
3567 
3568  client -> packet.op = BOOTREQUEST;
3569  client -> packet.htype = client -> interface -> hw_address.hbuf [0];
3570  /* Assumes hw_address is known, otherwise a random value may result */
3571  client -> packet.hlen = client -> interface -> hw_address.hlen - 1;
3572  client -> packet.hops = 0;
3573  client -> packet.xid = random ();
3574  client -> packet.secs = 0; /* filled in by send_discover. */
3575 
3578  client -> packet.flags = 0;
3579  else
3580  client -> packet.flags = htons (BOOTP_BROADCAST);
3581 
3582  memset (&(client -> packet.ciaddr),
3583  0, sizeof client -> packet.ciaddr);
3584  memset (&(client -> packet.yiaddr),
3585  0, sizeof client -> packet.yiaddr);
3586  memset (&(client -> packet.siaddr),
3587  0, sizeof client -> packet.siaddr);
3588  client -> packet.giaddr = giaddr;
3589  if (client -> interface -> hw_address.hlen > 0)
3590  memcpy (client -> packet.chaddr,
3591  &client -> interface -> hw_address.hbuf [1],
3592  (unsigned)(client -> interface -> hw_address.hlen - 1));
3593 
3594 #ifdef DEBUG_PACKET
3595  dump_raw ((unsigned char *)&client -> packet, client -> packet_length);
3596 #endif
3597 }
3598 
3599 
3600 void make_request (client, lease)
3601  struct client_state *client;
3602  struct client_lease *lease;
3603 {
3604  unsigned char request = DHCPREQUEST;
3605  struct option_cache *oc;
3606 
3607  memset (&client -> packet, 0, sizeof (client -> packet));
3608 
3609  if (client -> state == S_REQUESTING)
3610  oc = lookup_option (&dhcp_universe, lease -> options,
3612  else
3613  oc = (struct option_cache *)0;
3614 
3615  if (client -> sent_options)
3616  option_state_dereference (&client -> sent_options, MDL);
3617 
3618  make_client_options (client, lease, &request, oc,
3619  ((client -> state == S_REQUESTING ||
3620  client -> state == S_REBOOTING)
3621  ? &lease -> address
3622  : (struct iaddr *)0),
3623  client -> config -> requested_options,
3624  &client -> sent_options);
3625 
3626  /* Set up the option buffer... */
3627  client -> packet_length =
3628  cons_options ((struct packet *)0, &client -> packet,
3629  (struct lease *)0, client,
3630  /* maximum packet size */1500,
3631  (struct option_state *)0,
3632  client -> sent_options,
3633  /* scope */ &global_scope,
3634  /* overload */ 0,
3635  /* terminate */0,
3636  /* bootpp */0,
3637  (struct data_string *)0,
3638  client -> config -> vendor_space_name);
3639 
3640  if (client -> packet_length < BOOTP_MIN_LEN)
3641  client -> packet_length = BOOTP_MIN_LEN;
3642 
3643  client -> packet.op = BOOTREQUEST;
3644  client -> packet.htype = client -> interface -> hw_address.hbuf [0];
3645  /* Assumes hw_address is known, otherwise a random value may result */
3646  client -> packet.hlen = client -> interface -> hw_address.hlen - 1;
3647  client -> packet.hops = 0;
3648  client -> packet.xid = client -> xid;
3649  client -> packet.secs = 0; /* Filled in by send_request. */
3650 
3651  /* If we own the address we're requesting, put it in ciaddr;
3652  otherwise set ciaddr to zero. */
3653  if (client -> state == S_BOUND ||
3654  client -> state == S_RENEWING ||
3655  client -> state == S_REBINDING) {
3656  memcpy (&client -> packet.ciaddr,
3657  lease -> address.iabuf, lease -> address.len);
3658  client -> packet.flags = 0;
3659  } else {
3660  memset (&client -> packet.ciaddr, 0,
3661  sizeof client -> packet.ciaddr);
3662  if ((!(bootp_broadcast_always ||
3663  client ->config->bootp_broadcast_always)) &&
3664  can_receive_unicast_unconfigured (client -> interface))
3665  client -> packet.flags = 0;
3666  else
3667  client -> packet.flags = htons (BOOTP_BROADCAST);
3668  }
3669 
3670  memset (&client -> packet.yiaddr, 0,
3671  sizeof client -> packet.yiaddr);
3672  memset (&client -> packet.siaddr, 0,
3673  sizeof client -> packet.siaddr);
3674  if (client -> state != S_BOUND &&
3675  client -> state != S_RENEWING)
3676  client -> packet.giaddr = giaddr;
3677  else
3678  memset (&client -> packet.giaddr, 0,
3679  sizeof client -> packet.giaddr);
3680  if (client -> interface -> hw_address.hlen > 0)
3681  memcpy (client -> packet.chaddr,
3682  &client -> interface -> hw_address.hbuf [1],
3683  (unsigned)(client -> interface -> hw_address.hlen - 1));
3684 
3685 #ifdef DEBUG_PACKET
3686  dump_raw ((unsigned char *)&client -> packet, client -> packet_length);
3687 #endif
3688 }
3689 
3690 void make_decline (client, lease)
3691  struct client_state *client;
3692  struct client_lease *lease;
3693 {
3694  unsigned char decline = DHCPDECLINE;
3695  struct option_cache *oc;
3696 
3697  struct option_state *options = (struct option_state *)0;
3698 
3699  /* Create the options cache. */
3700  oc = lookup_option (&dhcp_universe, lease -> options,
3702  make_client_options(client, lease, &decline, oc, &lease->address,
3703  NULL, &options);
3704 
3705  /* Consume the options cache into the option buffer. */
3706  memset (&client -> packet, 0, sizeof (client -> packet));
3707  client -> packet_length =
3708  cons_options ((struct packet *)0, &client -> packet,
3709  (struct lease *)0, client, 0,
3710  (struct option_state *)0, options,
3711  &global_scope, 0, 0, 0, (struct data_string *)0,
3712  client -> config -> vendor_space_name);
3713 
3714  /* Destroy the options cache. */
3715  option_state_dereference (&options, MDL);
3716 
3717  if (client -> packet_length < BOOTP_MIN_LEN)
3718  client -> packet_length = BOOTP_MIN_LEN;
3719 
3720  client -> packet.op = BOOTREQUEST;
3721  client -> packet.htype = client -> interface -> hw_address.hbuf [0];
3722  /* Assumes hw_address is known, otherwise a random value may result */
3723  client -> packet.hlen = client -> interface -> hw_address.hlen - 1;
3724  client -> packet.hops = 0;
3725  client -> packet.xid = client -> xid;
3726  client -> packet.secs = 0; /* Filled in by send_request. */
3729  client -> packet.flags = 0;
3730  else
3731  client -> packet.flags = htons (BOOTP_BROADCAST);
3732 
3733  /* ciaddr must always be zero. */
3734  memset (&client -> packet.ciaddr, 0,
3735  sizeof client -> packet.ciaddr);
3736  memset (&client -> packet.yiaddr, 0,
3737  sizeof client -> packet.yiaddr);
3738  memset (&client -> packet.siaddr, 0,
3739  sizeof client -> packet.siaddr);
3740  client -> packet.giaddr = giaddr;
3741  memcpy (client -> packet.chaddr,
3742  &client -> interface -> hw_address.hbuf [1],
3743  client -> interface -> hw_address.hlen);
3744 
3745 #ifdef DEBUG_PACKET
3746  dump_raw ((unsigned char *)&client -> packet, client -> packet_length);
3747 #endif
3748 }
3749 
3750 void make_release (client, lease)
3751  struct client_state *client;
3752  struct client_lease *lease;
3753 {
3754  unsigned char request = DHCPRELEASE;
3755  struct option_cache *oc;
3756 
3757  struct option_state *options = (struct option_state *)0;
3758 
3759  memset (&client -> packet, 0, sizeof (client -> packet));
3760 
3761  oc = lookup_option (&dhcp_universe, lease -> options,
3763  make_client_options(client, lease, &request, oc, NULL, NULL, &options);
3764 
3765  /* Set up the option buffer... */
3766  client -> packet_length =
3767  cons_options ((struct packet *)0, &client -> packet,
3768  (struct lease *)0, client,
3769  /* maximum packet size */1500,
3770  (struct option_state *)0,
3771  options,
3772  /* scope */ &global_scope,
3773  /* overload */ 0,
3774  /* terminate */0,
3775  /* bootpp */0,
3776  (struct data_string *)0,
3777  client -> config -> vendor_space_name);
3778 
3779  if (client -> packet_length < BOOTP_MIN_LEN)
3780  client -> packet_length = BOOTP_MIN_LEN;
3781  option_state_dereference (&options, MDL);
3782 
3783  client -> packet.op = BOOTREQUEST;
3784  client -> packet.htype = client -> interface -> hw_address.hbuf [0];
3785  /* Assumes hw_address is known, otherwise a random value may result */
3786  client -> packet.hlen = client -> interface -> hw_address.hlen - 1;
3787  client -> packet.hops = 0;
3788  client -> packet.xid = random ();
3789  client -> packet.secs = 0;
3790  client -> packet.flags = 0;
3791  memcpy (&client -> packet.ciaddr,
3792  lease -> address.iabuf, lease -> address.len);
3793  memset (&client -> packet.yiaddr, 0,
3794  sizeof client -> packet.yiaddr);
3795  memset (&client -> packet.siaddr, 0,
3796  sizeof client -> packet.siaddr);
3797  client -> packet.giaddr = giaddr;
3798  memcpy (client -> packet.chaddr,
3799  &client -> interface -> hw_address.hbuf [1],
3800  client -> interface -> hw_address.hlen);
3801 
3802 #ifdef DEBUG_PACKET
3803  dump_raw ((unsigned char *)&client -> packet, client -> packet_length);
3804 #endif
3805 }
3806 
3808  struct client_lease *lease;
3809 {
3810  if (lease -> server_name)
3811  dfree (lease -> server_name, MDL);
3812  if (lease -> filename)
3813  dfree (lease -> filename, MDL);
3814  option_state_dereference (&lease -> options, MDL);
3815  free_client_lease (lease, MDL);
3816 }
3817 
3818 FILE *leaseFile = NULL;
3820 
3822 {
3823  struct interface_info *ip;
3824  struct client_state *client;
3825  struct client_lease *lp;
3826 
3827  if (leaseFile != NULL)
3828  fclose (leaseFile);
3829  leaseFile = fopen (path_dhclient_db, "we");
3830  if (leaseFile == NULL) {
3831  log_error ("can't create %s: %m", path_dhclient_db);
3832  return;
3833  }
3834 
3835  /* If there is a default duid, write it out. */
3836  if (default_duid.len != 0)
3837  write_duid(&default_duid);
3838 
3839  /* Write out all the leases attached to configured interfaces that
3840  we know about. */
3841  for (ip = interfaces; ip; ip = ip -> next) {
3842  for (client = ip -> client; client; client = client -> next) {
3843  for (lp = client -> leases; lp; lp = lp -> next) {
3844  write_client_lease (client, lp, 1, 0);
3845  }
3846  if (client -> active)
3847  write_client_lease (client,
3848  client -> active, 1, 0);
3849 
3850  if (client->active_lease != NULL)
3851  write_client6_lease(client,
3852  client->active_lease,
3853  1, 0);
3854 
3855  /* Reset last_write after rewrites. */
3856  client->last_write = 0;
3857  }
3858  }
3859 
3860  /* Write out any leases that are attached to interfaces that aren't
3861  currently configured. */
3862  for (ip = dummy_interfaces; ip; ip = ip -> next) {
3863  for (client = ip -> client; client; client = client -> next) {
3864  for (lp = client -> leases; lp; lp = lp -> next) {
3865  write_client_lease (client, lp, 1, 0);
3866  }
3867  if (client -> active)
3868  write_client_lease (client,
3869  client -> active, 1, 0);
3870 
3871  if (client->active_lease != NULL)
3872  write_client6_lease(client,
3873  client->active_lease,
3874  1, 0);
3875 
3876  /* Reset last_write after rewrites. */
3877  client->last_write = 0;
3878  }
3879  }
3880  fflush (leaseFile);
3881 }
3882 
3884  struct packet *packet, struct lease *lease,
3885  struct client_state *client_state,
3886  struct option_state *in_options,
3887  struct option_state *cfg_options,
3888  struct binding_scope **scope,
3889  struct universe *u, void *stuff)
3890 {
3891  const char *name, *dot;
3892  struct data_string ds;
3893  char *preamble = stuff;
3894 
3895  memset (&ds, 0, sizeof ds);
3896 
3897  if (u != &dhcp_universe) {
3898  name = u -> name;
3899  dot = ".";
3900  } else {
3901  name = "";
3902  dot = "";
3903  }
3904  if (evaluate_option_cache (&ds, packet, lease, client_state,
3905  in_options, cfg_options, scope, oc, MDL)) {
3906  /* The option name */
3907  fprintf(leaseFile, "%soption %s%s%s", preamble,
3908  name, dot, oc->option->name);
3909 
3910  /* The option value if there is one */
3911  if ((oc->option->format == NULL) ||
3912  (oc->option->format[0] != 'Z')) {
3913  fprintf(leaseFile, " %s",
3915  ds.len, 1, 1));
3916  }
3917 
3918  /* The closing semi-colon and newline */
3919  fprintf(leaseFile, ";\n");
3920 
3921  data_string_forget (&ds, MDL);
3922  }
3923 }
3924 
3925 /* Write an option cache to the lease store. */
3926 static void
3927 write_options(struct client_state *client, struct option_state *options,
3928  const char *preamble)
3929 {
3930  int i;
3931 
3932  for (i = 0; i < options->universe_count; i++) {
3933  option_space_foreach(NULL, NULL, client, NULL, options,
3934  &global_scope, universes[i],
3935  (char *)preamble, write_lease_option);
3936  }
3937 }
3938 
3939 int unhexchar(char c) {
3940 
3941  if (c >= '0' && c <= '9')
3942  return c - '0';
3943 
3944  if (c >= 'a' && c <= 'f')
3945  return c - 'a' + 10;
3946 
3947  if (c >= 'A' && c <= 'F')
3948  return c - 'A' + 10;
3949 
3950  return -1;
3951 }
3952 
3953 isc_result_t
3954 read_uuid(u_int8_t* uuid) {
3955  const char *id_fname = "/etc/machine-id";
3956  char id[32];
3957  size_t nread;
3958  FILE * file = fopen( id_fname , "r");
3959  if (!file) {
3960  log_debug("Cannot open %s", id_fname);
3961  return ISC_R_IOERROR;
3962  }
3963  nread = fread(id, 1, sizeof id, file);
3964  fclose(file);
3965 
3966  if (nread < 32) {
3967  log_debug("Not enough data in %s", id_fname);
3968  return ISC_R_IOERROR;
3969  }
3970 
3971  for (int j = 0; j < 16; j++) {
3972  int a, b;
3973 
3974  a = unhexchar(id[j*2]);
3975  b = unhexchar(id[j*2+1]);
3976 
3977  if (a < 0 || b < 0) {
3978  log_debug("Wrong data in %s", id_fname);
3979  return ISC_R_IOERROR;
3980  }
3981  uuid[j] = a << 4 | b;
3982  }
3983 
3984  /* Set UUID version to 4 --- truly random generation */
3985  uuid[6] = (uuid[6] & 0x0F) | 0x40;
3986  /* Set the UUID variant to DCE */
3987  uuid[8] = (uuid[8] & 0x3F) | 0x80;
3988 
3989  return ISC_R_SUCCESS;
3990 }
3991 
3992 /*
3993  * The "best" default DUID, since we cannot predict any information
3994  * about the system (such as whether or not the hardware addresses are
3995  * integrated into the motherboard or similar), is the "LLT", link local
3996  * plus time, DUID. For real stateless "LL" is better.
3997  *
3998  * Once generated, this duid is stored into the state database, and
3999  * retained across restarts.
4000  *
4001  * For the time being, there is probably a different state database for
4002  * every daemon, so this winds up being a per-interface identifier...which
4003  * is not how it is intended. Upcoming rearchitecting the client should
4004  * address this "one daemon model."
4005  */
4006 isc_result_t
4007 form_duid(struct data_string *duid, const char *file, int line)
4008 {
4009  struct interface_info *ip;
4010  int len;
4011  char *str;
4012  u_int8_t uuid[16];
4013 
4014  /* For now, just use the first interface on the list. */
4015  ip = interfaces;
4016 
4017  if (ip == NULL)
4018  log_fatal("Impossible condition at %s:%d.", MDL);
4019 
4020  while (ip && ip->hw_address.hbuf[0] == HTYPE_RESERVED) {
4021  /* Try the other interfaces */
4022  log_debug("Cannot form default DUID from interface %s.", ip->name);
4023  ip = ip->next;
4024  }
4025  if (ip == NULL) {
4026  return ISC_R_UNEXPECTED;
4027  }
4028 
4029  if ((ip->hw_address.hlen == 0) ||
4030  (ip->hw_address.hlen > sizeof(ip->hw_address.hbuf)))
4031  log_fatal("Impossible hardware address length at %s:%d.", MDL);
4032 
4033  if (duid_type == 0) {
4034  if (read_uuid(uuid) == ISC_R_SUCCESS)
4035  duid_type = DUID_UUID;
4036  else
4038  }
4039 
4040  if (duid_type == DUID_UUID)
4041  len = 2 + sizeof (uuid);
4042  else {
4043  /*
4044  * 2 bytes for the 'duid type' field.
4045  * 2 bytes for the 'htype' field.
4046  * (DUID_LLT) 4 bytes for the 'current time'.
4047  * enough bytes for the hardware address (note that hw_address has
4048  * the 'htype' on byte zero).
4049  */
4050  len = 4 + (ip->hw_address.hlen - 1);
4051  if (duid_type == DUID_LLT)
4052  len += 4;
4053  }
4054  if (!buffer_allocate(&duid->buffer, len, MDL))
4055  log_fatal("no memory for default DUID!");
4056  duid->data = duid->buffer->data;
4057  duid->len = len;
4058 
4059  if (duid_type == DUID_UUID) {
4060  putUShort(duid->buffer->data, DUID_UUID);
4061  memcpy(duid->buffer->data + 2, uuid, sizeof(uuid));
4062  }
4063  /* Basic Link Local Address type of DUID. */
4064  else if (duid_type == DUID_LLT) {
4065  putUShort(duid->buffer->data, DUID_LLT);
4066  putUShort(duid->buffer->data + 2, ip->hw_address.hbuf[0]);
4067  putULong(duid->buffer->data + 4, cur_time - DUID_TIME_EPOCH);
4068  memcpy(duid->buffer->data + 8, ip->hw_address.hbuf + 1,
4069  ip->hw_address.hlen - 1);
4070  } else {
4071  putUShort(duid->buffer->data, DUID_LL);
4072  putUShort(duid->buffer->data + 2, ip->hw_address.hbuf[0]);
4073  memcpy(duid->buffer->data + 4, ip->hw_address.hbuf + 1,
4074  ip->hw_address.hlen - 1);
4075  }
4076 
4077  /* Now format the output based on lease-id-format */
4078  str = format_lease_id(duid->data, duid->len,
4080  if (str == NULL) {
4081  log_info("form_duid: Couldn't allocate memory to log duid!");
4082  } else {
4083  log_info("Created duid %s.", str);
4084  dfree(str, MDL);
4085  }
4086 
4087  return ISC_R_SUCCESS;
4088 }
4089 
4090 /* Write the default DUID to the lease store. */
4091 static isc_result_t
4092 write_duid(struct data_string *duid)
4093 {
4094  char *str;
4095  int stat;
4096 
4097  if ((duid == NULL) || (duid->len <= 2))
4098  return DHCP_R_INVALIDARG;
4099 
4100  if (leaseFile == NULL) { /* XXX? */
4101  leaseFile = fopen(path_dhclient_db, "we");
4102  if (leaseFile == NULL) {
4103  log_error("can't create %s: %m", path_dhclient_db);
4104  return ISC_R_IOERROR;
4105  }
4106  }
4107 
4108  /* Generate a formatted duid string per lease-id-format */
4109  str = format_lease_id(duid->data, duid->len,
4111  if (str == NULL)
4112  return ISC_R_NOMEMORY;
4113 
4114  stat = fprintf(leaseFile, "default-duid %s;\n", str);
4115  dfree(str, MDL);
4116  if (stat <= 0)
4117  return ISC_R_IOERROR;
4118 
4119  if (fflush(leaseFile) != 0)
4120  return ISC_R_IOERROR;
4121 
4122  return ISC_R_SUCCESS;
4123 }
4124 
4125 /* Write a DHCPv6 lease to the store. */
4126 isc_result_t
4127 write_client6_lease(struct client_state *client, struct dhc6_lease *lease,
4128  int rewrite, int sync)
4129 {
4130  struct dhc6_ia *ia;
4131  struct dhc6_addr *addr;
4132  int stat;
4133  const char *ianame;
4134 
4135  /* This should include the current lease. */
4136  if (!rewrite && (leases_written++ > 20)) {
4138  leases_written = 0;
4139  return ISC_R_SUCCESS;
4140  }
4141 
4142  if (client == NULL || lease == NULL)
4143  return DHCP_R_INVALIDARG;
4144 
4145  if (leaseFile == NULL) { /* XXX? */
4146  leaseFile = fopen(path_dhclient_db, "w");
4147  if (leaseFile == NULL) {
4148  log_error("can't create %s: %m", path_dhclient_db);
4149  return ISC_R_IOERROR;
4150  }
4151  }
4152 
4153  stat = fprintf(leaseFile, "lease6 {\n");
4154  if (stat <= 0)
4155  return ISC_R_IOERROR;
4156 
4157  stat = fprintf(leaseFile, " interface \"%s\";\n",
4158  client->interface->name);
4159  if (stat <= 0)
4160  return ISC_R_IOERROR;
4161 
4162  for (ia = lease->bindings ; ia != NULL ; ia = ia->next) {
4163  switch (ia->ia_type) {
4164  case D6O_IA_NA:
4165  default:
4166  ianame = "ia-na";
4167  break;
4168  case D6O_IA_TA:
4169  ianame = "ia-ta";
4170  break;
4171  case D6O_IA_PD:
4172  ianame = "ia-pd";
4173  break;
4174  }
4175 
4176  /* For some reason IAID was never octal or hex, but string or
4177  * hex. Go figure. So for compatibilty's sake we will either
4178  * do hex or "legacy" i.e string rather than octal. What a
4179  * cluster. */
4181  case TOKEN_HEX: {
4182  char* iaid_str = format_lease_id(
4183  (const unsigned char *) &ia->iaid, 4,
4185 
4186  if (!iaid_str) {
4187  log_error("Can't format iaid");
4188  return ISC_R_IOERROR;
4189  }
4190 
4191  stat = fprintf(leaseFile, " %s %s {\n",
4192  ianame, iaid_str);
4193  dfree(iaid_str, MDL);
4194  break;
4195  }
4196 
4197  case TOKEN_OCTAL:
4198  default:
4199  stat = fprintf(leaseFile, " %s %s {\n", ianame,
4200  print_hex_1(4, ia->iaid, 12));
4201  break;
4202  }
4203 
4204  if (stat <= 0)
4205  return ISC_R_IOERROR;
4206 
4207  if (ia->ia_type != D6O_IA_TA)
4208  stat = fprintf(leaseFile, " starts %d;\n"
4209  " renew %u;\n"
4210  " rebind %u;\n",
4211  (int)ia->starts, ia->renew, ia->rebind);
4212  else
4213  stat = fprintf(leaseFile, " starts %d;\n",
4214  (int)ia->starts);
4215  if (stat <= 0)
4216  return ISC_R_IOERROR;
4217 
4218  for (addr = ia->addrs ; addr != NULL ; addr = addr->next) {
4219  if (ia->ia_type != D6O_IA_PD)
4220  stat = fprintf(leaseFile,
4221  " iaaddr %s {\n",
4222  piaddr(addr->address));
4223  else
4224  stat = fprintf(leaseFile,
4225  " iaprefix %s/%d {\n",
4226  piaddr(addr->address),
4227  (int)addr->plen);
4228  if (stat <= 0)
4229  return ISC_R_IOERROR;
4230 
4231  stat = fprintf(leaseFile, " starts %d;\n"
4232  " preferred-life %u;\n"
4233  " max-life %u;\n",
4234  (int)addr->starts, addr->preferred_life,
4235  addr->max_life);
4236  if (stat <= 0)
4237  return ISC_R_IOERROR;
4238 
4239  if (addr->options != NULL)
4240  write_options(client, addr->options, " ");
4241 
4242  stat = fprintf(leaseFile, " }\n");
4243  if (stat <= 0)
4244  return ISC_R_IOERROR;
4245  }
4246 
4247  if (ia->options != NULL)
4248  write_options(client, ia->options, " ");
4249 
4250  stat = fprintf(leaseFile, " }\n");
4251  if (stat <= 0)
4252  return ISC_R_IOERROR;
4253  }
4254 
4255  if (lease->released) {
4256  stat = fprintf(leaseFile, " released;\n");
4257  if (stat <= 0)
4258  return ISC_R_IOERROR;
4259  }
4260 
4261  if (lease->options != NULL)
4262  write_options(client, lease->options, " ");
4263 
4264  stat = fprintf(leaseFile, "}\n");
4265  if (stat <= 0)
4266  return ISC_R_IOERROR;
4267 
4268  if (fflush(leaseFile) != 0)
4269  return ISC_R_IOERROR;
4270 
4271  if (sync) {
4272  if (fsync(fileno(leaseFile)) < 0) {
4273  log_error("write_client_lease: fsync(): %m");
4274  return ISC_R_IOERROR;
4275  }
4276  }
4277 
4278  return ISC_R_SUCCESS;
4279 }
4280 
4281 int write_client_lease (client, lease, rewrite, makesure)
4282  struct client_state *client;
4283  struct client_lease *lease;
4284  int rewrite;
4285  int makesure;
4286 {
4287  struct data_string ds;
4288  int errors = 0;
4289  char *s;
4290  const char *tval;
4291 
4292  if (!rewrite) {
4293  if (leases_written++ > 20) {
4295  leases_written = 0;
4296  }
4297  }
4298 
4299  /* If the lease came from the config file, we don't need to stash
4300  a copy in the lease database. */
4301  if (lease -> is_static)
4302  return 1;
4303 
4304  if (leaseFile == NULL) { /* XXX */
4305  leaseFile = fopen (path_dhclient_db, "we");
4306  if (leaseFile == NULL) {
4307  log_error ("can't create %s: %m", path_dhclient_db);
4308  return 0;
4309  }
4310  }
4311 
4312  errno = 0;
4313  fprintf (leaseFile, "lease {\n");
4314  if (lease -> is_bootp) {
4315  fprintf (leaseFile, " bootp;\n");
4316  if (errno) {
4317  ++errors;
4318  errno = 0;
4319  }
4320  }
4321  fprintf (leaseFile, " interface \"%s\";\n",
4322  client -> interface -> name);
4323  if (errno) {
4324  ++errors;
4325  errno = 0;
4326  }
4327  if (client -> name) {
4328  fprintf (leaseFile, " name \"%s\";\n", client -> name);
4329  if (errno) {
4330  ++errors;
4331  errno = 0;
4332  }
4333  }
4334  fprintf (leaseFile, " fixed-address %s;\n",
4335  piaddr (lease -> address));
4336  if (errno) {
4337  ++errors;
4338  errno = 0;
4339  }
4340  if (lease -> filename) {
4341  s = quotify_string (lease -> filename, MDL);
4342  if (s) {
4343  fprintf (leaseFile, " filename \"%s\";\n", s);
4344  if (errno) {
4345  ++errors;
4346  errno = 0;
4347  }
4348  dfree (s, MDL);
4349  } else
4350  errors++;
4351 
4352  }
4353  if (lease->server_name != NULL) {
4354  s = quotify_string(lease->server_name, MDL);
4355  if (s != NULL) {
4356  fprintf(leaseFile, " server-name \"%s\";\n", s);
4357  if (errno) {
4358  ++errors;
4359  errno = 0;
4360  }
4361  dfree(s, MDL);
4362  } else
4363  ++errors;
4364  }
4365  if (lease -> medium) {
4366  s = quotify_string (lease -> medium -> string, MDL);
4367  if (s) {
4368  fprintf (leaseFile, " medium \"%s\";\n", s);
4369  if (errno) {
4370  ++errors;
4371  errno = 0;
4372  }
4373  dfree (s, MDL);
4374  } else
4375  errors++;
4376  }
4377  if (errno != 0) {
4378  errors++;
4379  errno = 0;
4380  }
4381 
4382  memset (&ds, 0, sizeof ds);
4383 
4384  write_options(client, lease->options, " ");
4385 
4386  tval = print_time(lease->renewal);
4387  if (tval == NULL ||
4388  fprintf(leaseFile, " renew %s\n", tval) < 0)
4389  errors++;
4390 
4391  tval = print_time(lease->rebind);
4392  if (tval == NULL ||
4393  fprintf(leaseFile, " rebind %s\n", tval) < 0)
4394  errors++;
4395 
4396  tval = print_time(lease->expiry);
4397  if (tval == NULL ||
4398  fprintf(leaseFile, " expire %s\n", tval) < 0)
4399  errors++;
4400 
4401  if (fprintf(leaseFile, "}\n") < 0)
4402  errors++;
4403 
4404  if (fflush(leaseFile) != 0)
4405  errors++;
4406 
4407  client->last_write = cur_time;
4408 
4409  if (!errors && makesure) {
4410  if (fsync (fileno (leaseFile)) < 0) {
4411  log_info ("write_client_lease: %m");
4412  return 0;
4413  }
4414  }
4415 
4416  return errors ? 0 : 1;
4417 }
4418 
4419 /* Variables holding name of script and file pointer for writing to
4420  script. Needless to say, this is not reentrant - only one script
4421  can be invoked at a time. */
4422 char scriptName [256];
4424 
4437 void script_init(struct client_state *client, const char *reason,
4438  struct string_list *medium)
4439 {
4440  struct string_list *sl, *next;
4441 
4442  if (client) {
4443  for (sl = client -> env; sl; sl = next) {
4444  next = sl -> next;
4445  dfree (sl, MDL);
4446  }
4447  client -> env = (struct string_list *)0;
4448  client -> envc = 0;
4449 
4450  if (client -> interface) {
4451  client_envadd (client, "", "interface", "%s",
4452  client -> interface -> name);
4453  }
4454  if (client -> name)
4455  client_envadd (client,
4456  "", "client", "%s", client -> name);
4457  if (medium)
4458  client_envadd (client,
4459  "", "medium", "%s", medium -> string);
4460 
4461  client_envadd (client, "", "reason", "%s", reason);
4462  client_envadd (client, "", "pid", "%ld", (long int)getpid ());
4463  client_envadd (client, "", "dad_wait_time", "%ld",
4464  (long int)dad_wait_time);
4465  }
4466 }
4467 
4469  struct packet *packet, struct lease *lease,
4470  struct client_state *client_state,
4471  struct option_state *in_options,
4472  struct option_state *cfg_options,
4473  struct binding_scope **scope,
4474  struct universe *u, void *stuff)
4475 {
4476  struct envadd_state *es = stuff;
4477  struct data_string data;
4478  memset (&data, 0, sizeof data);
4479 
4480  if (evaluate_option_cache (&data, packet, lease, client_state,
4481  in_options, cfg_options, scope, oc, MDL)) {
4482  if (data.len) {
4483  char name [256];
4484  if (dhcp_option_ev_name (name, sizeof name,
4485  oc->option)) {
4486  const char *value;
4487  size_t length;
4488  value = pretty_print_option(oc->option,
4489  data.data,
4490  data.len, 0, 0);
4491  length = strlen(value);
4492 
4493  if (check_option_values(oc->option->universe,
4494  oc->option->code,
4495  value, length) == 0) {
4496  client_envadd(es->client, es->prefix,
4497  name, "%s", value);
4498  } else {
4499  log_error("suspect value in %s "
4500  "option - discarded",
4501  name);
4502  }
4503  data_string_forget (&data, MDL);
4504  }
4505  }
4506  }
4507 }
4508 
4528 void script_write_params(struct client_state *client, const char *prefix,
4529  struct client_lease *lease)
4530 {
4531  int i;
4532  struct data_string data;
4533  struct option_cache *oc;
4534  struct envadd_state es;
4535 
4536  es.client = client;
4537  es.prefix = prefix;
4538 
4539  client_envadd (client,
4540  prefix, "ip_address", "%s", piaddr (lease -> address));
4541 
4542  /* If we've set the next server address in the lease structure
4543  put it into an environment variable for the script */
4544  if (lease->next_srv_addr.len != 0) {
4545  client_envadd(client, prefix, "next_server", "%s",
4546  piaddr(lease->next_srv_addr));
4547  }
4548 
4549  /* For the benefit of Linux (and operating systems which may
4550  have similar needs), compute the network address based on
4551  the supplied ip address and netmask, if provided. Also
4552  compute the broadcast address (the host address all ones
4553  broadcast address, not the host address all zeroes
4554  broadcast address). */
4555 
4556  memset (&data, 0, sizeof data);
4557  oc = lookup_option (&dhcp_universe, lease -> options, DHO_SUBNET_MASK);
4558  if (oc && evaluate_option_cache (&data, (struct packet *)0,
4559  (struct lease *)0, client,
4560  (struct option_state *)0,
4561  lease -> options,
4562  &global_scope, oc, MDL)) {
4563  if (data.len > 3) {
4564  struct iaddr netmask, subnet, broadcast;
4565 
4566  /*
4567  * No matter the length of the subnet-mask option,
4568  * use only the first four octets. Note that
4569  * subnet-mask options longer than 4 octets are not
4570  * in conformance with RFC 2132, but servers with this
4571  * flaw do exist.
4572  */
4573  memcpy(netmask.iabuf, data.data, 4);
4574  netmask.len = 4;
4575  data_string_forget (&data, MDL);
4576 
4577  subnet = subnet_number (lease -> address, netmask);
4578  if (subnet.len) {
4579  client_envadd (client, prefix, "network_number",
4580  "%s", piaddr (subnet));
4581 
4583  lease -> options,
4585  if (!oc ||
4587  (&data, (struct packet *)0,
4588  (struct lease *)0, client,
4589  (struct option_state *)0,
4590  lease -> options,
4591  &global_scope, oc, MDL))) {
4592  broadcast = broadcast_addr (subnet, netmask);
4593  if (broadcast.len) {
4594  client_envadd (client,
4595  prefix, "broadcast_address",
4596  "%s", piaddr (broadcast));
4597  }
4598  }
4599  }
4600  }
4601  data_string_forget (&data, MDL);
4602  }
4603 
4604  if (lease->filename) {
4605  if (check_option_values(NULL, DHO_ROOT_PATH,
4606  lease->filename,
4607  strlen(lease->filename)) == 0) {
4608  client_envadd(client, prefix, "filename",
4609  "%s", lease->filename);
4610  } else {
4611  log_error("suspect value in %s "
4612  "option - discarded",
4613  lease->filename);
4614  }
4615  }
4616 
4617  if (lease->server_name) {
4618  if (check_option_values(NULL, DHO_HOST_NAME,
4619  lease->server_name,
4620  strlen(lease->server_name)) == 0 ) {
4621  client_envadd (client, prefix, "server_name",
4622  "%s", lease->server_name);
4623  } else {
4624  log_error("suspect value in %s "
4625  "option - discarded",
4626  lease->server_name);
4627  }
4628  }
4629 
4630  for (i = 0; i < lease -> options -> universe_count; i++) {
4631  option_space_foreach ((struct packet *)0, (struct lease *)0,
4632  client, (struct option_state *)0,
4633  lease -> options, &global_scope,
4634  universes [i],
4635  &es, client_option_envadd);
4636  }
4637 
4638  client_envadd (client, prefix, "expiry", "%lu",
4639  (unsigned long)(lease -> expiry));
4640 }
4641 
4652 {
4653  int i;
4654  struct option **req;
4655  char name[256];
4656  req = client->config->requested_options;
4657 
4658  if (req == NULL)
4659  return;
4660 
4661  for (i = 0 ; req[i] != NULL ; i++) {
4662  if ((req[i]->universe == &dhcp_universe) &&
4663  dhcp_option_ev_name(name, sizeof(name), req[i])) {
4664  client_envadd(client, "requested_", name, "%d", 1);
4665  }
4666  }
4667 }
4668 
4681 int script_go(struct client_state *client)
4682 {
4683  char *scriptName;
4684  char *argv [2];
4685  char **envp;
4686  char reason [] = "REASON=NBI";
4687  static char client_path [] = CLIENT_PATH;
4688  int i;
4689  struct string_list *sp, *next;
4690  int pid, wpid, wstatus;
4691 
4692  if (client)
4693  scriptName = client -> config -> script_name;
4694  else
4695  scriptName = top_level_config.script_name;
4696 
4697  envp = dmalloc (((client ? client -> envc : 2) +
4698  client_env_count + 2) * sizeof (char *), MDL);
4699  if (!envp) {
4700  log_error ("No memory for client script environment.");
4701  return 0;
4702  }
4703  i = 0;
4704  /* Copy out the environment specified on the command line,
4705  if any. */
4706  for (sp = client_env; sp; sp = sp -> next) {
4707  envp [i++] = sp -> string;
4708  }
4709  /* Copy out the environment specified by dhclient. */
4710  if (client) {
4711  for (sp = client -> env; sp; sp = sp -> next) {
4712  envp [i++] = sp -> string;
4713  }
4714  } else {
4715  envp [i++] = reason;
4716  }
4717  /* Set $PATH. */
4718  envp [i++] = client_path;
4719  envp [i] = (char *)0;
4720 
4721  argv [0] = scriptName;
4722  argv [1] = (char *)0;
4723 
4724  pid = fork ();
4725  if (pid < 0) {
4726  log_error ("fork: %m");
4727  wstatus = 0;
4728  } else if (pid) {
4729  do {
4730  wpid = wait (&wstatus);
4731  } while (wpid != pid && wpid > 0);
4732  if (wpid < 0) {
4733  log_error ("wait: %m");
4734  wstatus = 0;
4735  }
4736  } else {
4737  /* We don't want to pass an open file descriptor for
4738  * dhclient.leases when executing dhclient-script.
4739  */
4740  if (leaseFile != NULL)
4741  fclose(leaseFile);
4742  execve (scriptName, argv, envp);
4743  log_error ("execve (%s, ...): %m", scriptName);
4744  exit (0);
4745  }
4746 
4747  if (client) {
4748  for (sp = client -> env; sp; sp = next) {
4749  next = sp -> next;
4750  dfree (sp, MDL);
4751  }
4752  client -> env = (struct string_list *)0;
4753  client -> envc = 0;
4754  }
4755  dfree (envp, MDL);
4756  gettimeofday(&cur_tv, NULL);
4757  return (WIFEXITED (wstatus) ?
4758  WEXITSTATUS (wstatus) : -WTERMSIG (wstatus));
4759 }
4760 
4761 void client_envadd (struct client_state *client,
4762  const char *prefix, const char *name, const char *fmt, ...)
4763 {
4764  char spbuf [1024];
4765  char *s;
4766  unsigned len;
4767  struct string_list *val;
4768  va_list list;
4769 
4770  va_start (list, fmt);
4771  len = vsnprintf (spbuf, sizeof spbuf, fmt, list);
4772  va_end (list);
4773 
4774  val = dmalloc (strlen (prefix) + strlen (name) + 1 /* = */ +
4775  len + sizeof *val, MDL);
4776  if (!val) {
4777  log_error ("client_envadd: cannot allocate space for variable");
4778  return;
4779  }
4780 
4781  s = val -> string;
4782  strcpy (s, prefix);
4783  strcat (s, name);
4784  s += strlen (s);
4785  *s++ = '=';
4786  if (len >= sizeof spbuf) {
4787  va_start (list, fmt);
4788  vsnprintf (s, len + 1, fmt, list);
4789  va_end (list);
4790  } else {
4791  strcpy (s, spbuf);
4792  }
4793 
4794  val -> next = client -> env;
4795  client -> env = val;
4796  client -> envc++;
4797 }
4798 
4799 int dhcp_option_ev_name (buf, buflen, option)
4800  char *buf;
4801  size_t buflen;
4802  struct option *option;
4803 {
4804  int i, j;
4805  const char *s;
4806 
4807  j = 0;
4808  if (option -> universe != &dhcp_universe) {
4809  s = option -> universe -> name;
4810  i = 0;
4811  } else {
4812  s = option -> name;
4813  i = 1;
4814  }
4815 
4816  do {
4817  while (*s) {
4818  if (j + 1 == buflen)
4819  return 0;
4820  if (*s == '-')
4821  buf [j++] = '_';
4822  else
4823  buf [j++] = *s;
4824  ++s;
4825  }
4826  if (!i) {
4827  s = option -> name;
4828  if (j + 1 == buflen)
4829  return 0;
4830  buf [j++] = '_';
4831  }
4832  ++i;
4833  } while (i != 2);
4834 
4835  buf [j] = 0;
4836  return 1;
4837 }
4838 
4839 void go_daemon ()
4840 {
4841  static int state = 0;
4842  int pid;
4843 
4844  /* Don't become a daemon if the user requested otherwise. */
4845  if (no_daemon) {
4847  return;
4848  }
4849 
4850  /* Only do it once. */
4851  if (state)
4852  return;
4853  state = 1;
4854 
4855  /* Stop logging to stderr... */
4856  log_perror = 0;
4857 
4858  /* Become a daemon... */
4859  if ((pid = fork ()) < 0)
4860  log_fatal ("Can't fork daemon: %m");
4861  else if (pid)
4862  exit (0);
4863  /* Become session leader and get pid... */
4864  (void) setsid ();
4865 
4866  /* Close standard I/O descriptors. */
4867  (void) close(0);
4868  (void) close(1);
4869  (void) close(2);
4870 
4871  /* Reopen them on /dev/null. */
4872  (void) open("/dev/null", O_RDWR | O_CLOEXEC);
4873  (void) open("/dev/null", O_RDWR | O_CLOEXEC);
4874  (void) open("/dev/null", O_RDWR | O_CLOEXEC);
4875 
4877 
4878  IGNORE_RET (chdir("/"));
4879 }
4880 
4882 {
4883  FILE *pf;
4884  int pfdesc;
4885 
4886  /* nothing to do if the user doesn't want a pid file */
4887  if (no_pid_file == ISC_TRUE) {
4888  return;
4889  }
4890 
4891  pfdesc = open (path_dhclient_pid, O_CREAT | O_TRUNC | O_WRONLY | O_CLOEXEC, 0644);
4892 
4893  if (pfdesc < 0) {
4894  log_error ("Can't create %s: %m", path_dhclient_pid);
4895  return;
4896  }
4897 
4898  pf = fdopen (pfdesc, "we");
4899  if (!pf) {
4900  close(pfdesc);
4901  log_error ("Can't fdopen %s: %m", path_dhclient_pid);
4902  } else {
4903  fprintf (pf, "%ld\n", (long)getpid ());
4904  fclose (pf);
4905  }
4906 }
4907 
4909 {
4910  struct interface_info *ip;
4911  struct client_state *client;
4912 
4913  for (ip = interfaces; ip; ip = ip -> next) {
4914  for (client = ip -> client; client; client = client -> next) {
4915  switch (client -> state) {
4916  case S_SELECTING:
4917  cancel_timeout (send_discover, client);
4918  break;
4919 
4920  case S_BOUND:
4921  cancel_timeout (state_bound, client);
4922  break;
4923 
4924  case S_REBOOTING:
4925  case S_REQUESTING:
4926  case S_RENEWING:
4927  cancel_timeout (send_request, client);
4928  break;
4929 
4930  case S_INIT:
4931  case S_REBINDING:
4932  case S_STOPPED:
4933  case S_DECLINED:
4934  break;
4935  }
4936  client -> state = S_INIT;
4937  state_reboot (client);
4938  }
4939  }
4940 }
4941 
4942 void do_release(client)
4943  struct client_state *client;
4944 {
4945  struct data_string ds;
4946  struct option_cache *oc;
4947 
4948 #if defined(DHCPv6) && defined(DHCP4o6)
4949  if (dhcpv4_over_dhcpv6 && (dhcp4o6_state <= 0)) {
4950  if (dhcp4o6_state < 0)
4951  dhcp4o6_poll(NULL);
4952  client->pending = P_RELEASE;
4953  return;
4954  }
4955 #endif
4956 
4957  /* Pick a random xid. */
4958  client -> xid = random ();
4959 
4960  /* is there even a lease to release? */
4961  if (client -> active) {
4962  /* Make a DHCPRELEASE packet, and set appropriate per-interface
4963  flags. */
4964  make_release (client, client -> active);
4965 
4966  memset (&ds, 0, sizeof ds);
4968  client -> active -> options,
4970  if (oc &&
4971  evaluate_option_cache (&ds, (struct packet *)0,
4972  (struct lease *)0, client,
4973  (struct option_state *)0,
4974  client -> active -> options,
4975  &global_scope, oc, MDL)) {
4976  if (ds.len > 3) {
4977  memcpy (client -> destination.iabuf,
4978  ds.data, 4);
4979  client -> destination.len = 4;
4980  } else
4981  client -> destination = iaddr_broadcast;
4982 
4983  data_string_forget (&ds, MDL);
4984  } else
4985  client -> destination = iaddr_broadcast;
4986  client -> first_sending = cur_time;
4987  client -> interval = client -> config -> initial_interval;
4988 
4989  /* Zap the medium list... */
4990  client -> medium = (struct string_list *)0;
4991 
4992  /* Send out the first and only DHCPRELEASE packet. */
4993  send_release (client);
4994 
4995  /* Do the client script RELEASE operation. */
4996  script_init (client,
4997  "RELEASE", (struct string_list *)0);
4998  if (client -> alias)
4999  script_write_params(client, "alias_",
5000  client -> alias);
5001  script_write_params(client, "old_", client -> active);
5002  script_write_requested(client);
5003  script_go(client);
5004  }
5005 
5006  /* Cancel any timeouts. */
5007  cancel_timeout (state_bound, client);
5008  cancel_timeout (send_discover, client);
5009  cancel_timeout (state_init, client);
5010  cancel_timeout (send_request, client);
5011  cancel_timeout (state_reboot, client);
5012  client -> state = S_STOPPED;
5013 
5014 #if defined(DHCPv6) && defined(DHCP4o6)
5015  if (dhcpv4_over_dhcpv6)
5016  exit(0);
5017 #endif
5018 }
5019 
5021 {
5022  do_release (interface -> client);
5023 
5024  return 1;
5025 }
5026 
5028 {
5029  struct interface_info *last, *ip;
5030  /* See if we can find the client from dummy_interfaces */
5031  last = 0;
5032  for (ip = dummy_interfaces; ip; ip = ip -> next) {
5033  if (!strcmp (ip -> name, tmp -> name)) {
5034  /* Remove from dummy_interfaces */
5035  if (last) {
5036  ip = (struct interface_info *)0;
5037  interface_reference (&ip, last -> next, MDL);
5038  interface_dereference (&last -> next, MDL);
5039  if (ip -> next) {
5040  interface_reference (&last -> next,
5041  ip -> next, MDL);
5042  interface_dereference (&ip -> next,
5043  MDL);
5044  }
5045  } else {
5046  ip = (struct interface_info *)0;
5047  interface_reference (&ip,
5049  interface_dereference (&dummy_interfaces, MDL);
5050  if (ip -> next) {
5051  interface_reference (&dummy_interfaces,
5052  ip -> next, MDL);
5053  interface_dereference (&ip -> next,
5054  MDL);
5055  }
5056  }
5057  /* Copy "client" to tmp */
5058  if (ip -> client) {
5059  tmp -> client = ip -> client;
5060  tmp -> client -> interface = tmp;
5061  }
5062  interface_dereference (&ip, MDL);
5063  break;
5064  }
5065  last = ip;
5066  }
5067  return 1;
5068 }
5069 
5070 isc_result_t dhclient_interface_startup_hook (struct interface_info *interface)
5071 {
5072  struct interface_info *ip;
5073  struct client_state *client;
5074 
5075  /* This code needs some rethinking. It doesn't test against
5076  a signal name, and it just kind of bulls into doing something
5077  that may or may not be appropriate. */
5078 
5079  if (interfaces) {
5080  interface_reference (&interface -> next, interfaces, MDL);
5081  interface_dereference (&interfaces, MDL);
5082  }
5083  interface_reference (&interfaces, interface, MDL);
5084 
5086 
5087  for (ip = interfaces; ip; ip = ip -> next) {
5088  /* If interfaces were specified, don't configure
5089  interfaces that weren't specified! */
5090  if (ip -> flags & INTERFACE_RUNNING ||
5091  (ip -> flags & (INTERFACE_REQUESTED |
5092  INTERFACE_AUTOMATIC)) !=
5094  continue;
5095  script_init (ip -> client,
5096  "PREINIT", (struct string_list *)0);
5097  if (ip -> client -> alias)
5098  script_write_params(ip -> client, "alias_",
5099  ip -> client -> alias);
5100  script_go(ip -> client);
5101  }
5102 
5105  : DISCOVER_RUNNING);
5106 
5107  for (ip = interfaces; ip; ip = ip -> next) {
5108  if (ip -> flags & INTERFACE_RUNNING)
5109  continue;
5110  ip -> flags |= INTERFACE_RUNNING;
5111  for (client = ip->client ; client ; client = client->next) {
5112  client->state = S_INIT;
5113  state_reboot(client);
5114  }
5115  }
5116  return ISC_R_SUCCESS;
5117 }
5118 
5119 /* The client should never receive a relay agent information option,
5120  so if it does, log it and discard it. */
5121 
5122 int parse_agent_information_option (packet, len, data)
5123  struct packet *packet;
5124  int len;
5125  u_int8_t *data;
5126 {
5127  return 1;
5128 }
5129 
5130 /* The client never sends relay agent information options. */
5131 
5132 unsigned cons_agent_information_options (cfg_options, outpacket,
5133  agentix, length)
5134  struct option_state *cfg_options;
5135  struct dhcp_packet *outpacket;
5136  unsigned agentix;
5137  unsigned length;
5138 {
5139  return length;
5140 }
5141 
5142 static void shutdown_exit (void *foo)
5143 {
5144  /* get rid of the pid if we can */
5145  if (no_pid_file == ISC_FALSE)
5146  (void) unlink(path_dhclient_pid);
5147  exit (0);
5148 }
5149 
5150 #if defined (NSUPDATE)
5151 /*
5152  * If the first query fails, the updater MUST NOT delete the DNS name. It
5153  * may be that the host whose lease on the server has expired has moved
5154  * to another network and obtained a lease from a different server,
5155  * which has caused the client's A RR to be replaced. It may also be
5156  * that some other client has been configured with a name that matches
5157  * the name of the DHCP client, and the policy was that the last client
5158  * to specify the name would get the name. In this case, the DHCID RR
5159  * will no longer match the updater's notion of the client-identity of
5160  * the host pointed to by the DNS name.
5161  * -- "Interaction between DHCP and DNS"
5162  */
5163 
5164 /* The first and second stages are pretty similar so we combine them */
5165 void
5166 client_dns_remove_action(dhcp_ddns_cb_t *ddns_cb,
5167  isc_result_t eresult)
5168 {
5169 
5170  isc_result_t result;
5171 
5172  if ((eresult == ISC_R_SUCCESS) &&
5173  (ddns_cb->state == DDNS_STATE_REM_FW_YXDHCID)) {
5174  /* Do the second stage of the FWD removal */
5175  ddns_cb->state = DDNS_STATE_REM_FW_NXRR;
5176 
5177  result = ddns_modify_fwd(ddns_cb, MDL);
5178  if (result == ISC_R_SUCCESS) {
5179  return;
5180  }
5181  }
5182 
5183  /* If we are done or have an error clean up */
5184  dhclient_ddns_cb_free(ddns_cb, MDL);
5185  return;
5186 }
5187 
5188 void
5189 client_dns_remove(struct client_state *client,
5190  struct iaddr *addr)
5191 {
5192  dhcp_ddns_cb_t *ddns_cb;
5193  isc_result_t result;
5194 
5195  /* if we have an old ddns request for this client, cancel it */
5196  if (client->ddns_cb != NULL) {
5197  ddns_cancel(client->ddns_cb, MDL);
5198  client->ddns_cb = NULL;
5199  }
5200 
5201  ddns_cb = ddns_cb_alloc(MDL);
5202  if (ddns_cb != NULL) {
5203  ddns_cb->address = *addr;
5204  ddns_cb->timeout = 0;
5205 
5206  ddns_cb->state = DDNS_STATE_REM_FW_YXDHCID;
5207  ddns_cb->flags = DDNS_UPDATE_ADDR;
5208  ddns_cb->cur_func = client_dns_remove_action;
5209 
5210  result = client_dns_update(client, ddns_cb);
5211 
5212  if (result != ISC_R_TIMEDOUT) {
5213  dhclient_ddns_cb_free(ddns_cb, MDL);
5214  }
5215  }
5216 }
5217 #endif
5218 
5220  control_object_state_t newstate)
5221 {
5222  struct interface_info *ip;
5223  struct client_state *client;
5224  struct timeval tv;
5225 
5226  if (newstate == server_shutdown) {
5227  /* Re-entry */
5228  if (shutdown_signal == SIGUSR1)
5229  return ISC_R_SUCCESS;
5230  /* Log shutdown on signal. */
5231  if ((shutdown_signal == SIGINT) ||
5232  (shutdown_signal == SIGTERM)) {
5233  log_info("Received signal %d, initiating shutdown.",
5234  shutdown_signal);
5235  }
5236  /* Mark it was called. */
5237  shutdown_signal = SIGUSR1;
5238  }
5239 
5240  /* Do the right thing for each interface. */
5241  for (ip = interfaces; ip; ip = ip -> next) {
5242  for (client = ip -> client; client; client = client -> next) {
5243  switch (newstate) {
5244  case server_startup:
5245  return ISC_R_SUCCESS;
5246 
5247  case server_running:
5248  return ISC_R_SUCCESS;
5249 
5250  case server_shutdown:
5251  if (client -> active &&
5252  client -> active -> expiry > cur_time) {
5253 #if defined (NSUPDATE)
5254  if (client->config->do_forward_update) {
5255  client_dns_remove(client,
5256  &client->active->address);
5257  }
5258 #endif
5259  do_release (client);
5260  }
5261  break;
5262 
5263  case server_hibernate:
5264  state_stop (client);
5265  break;
5266 
5267  case server_awaken:
5268  state_reboot (client);
5269  break;
5270  }
5271  }
5272  }
5273 
5274  if (newstate == server_shutdown) {
5275  tv.tv_sec = cur_tv.tv_sec;
5276  tv.tv_usec = cur_tv.tv_usec + 1;
5277  add_timeout(&tv, shutdown_exit, 0, 0, 0);
5278  }
5279  return ISC_R_SUCCESS;
5280 }
5281 
5282 #if defined (NSUPDATE)
5283 /*
5284  * Called after a timeout if the DNS update failed on the previous try.
5285  * Starts the retry process. If the retry times out it will schedule
5286  * this routine to run again after a 10x wait.
5287  */
5288 void
5289 client_dns_update_timeout (void *cp)
5290 {
5291  dhcp_ddns_cb_t *ddns_cb = (dhcp_ddns_cb_t *)cp;
5292  struct client_state *client = (struct client_state *)ddns_cb->lease;
5293  isc_result_t status = ISC_R_FAILURE;
5294 
5295  if ((client != NULL) &&
5296  ((client->active != NULL) ||
5297  (client->active_lease != NULL)))
5298  status = client_dns_update(client, ddns_cb);
5299 
5300  /*
5301  * A status of timedout indicates that we started the update and
5302  * have released control of the control block. Any other status
5303  * indicates that we should clean up the control block. We either
5304  * got a success which indicates that we didn't really need to
5305  * send an update or some other error in which case we weren't able
5306  * to start the update process. In both cases we still own
5307  * the control block and should free it.
5308  */
5309  if (status != ISC_R_TIMEDOUT) {
5310  dhclient_ddns_cb_free(ddns_cb, MDL);
5311  }
5312 }
5313 
5314 /*
5315  * If the first query succeeds, the updater can conclude that it
5316  * has added a new name whose only RRs are the A and DHCID RR records.
5317  * The A RR update is now complete (and a client updater is finished,
5318  * while a server might proceed to perform a PTR RR update).
5319  * -- "Interaction between DHCP and DNS"
5320  *
5321  * If the second query succeeds, the updater can conclude that the current
5322  * client was the last client associated with the domain name, and that
5323  * the name now contains the updated A RR. The A RR update is now
5324  * complete (and a client updater is finished, while a server would
5325  * then proceed to perform a PTR RR update).
5326  * -- "Interaction between DHCP and DNS"
5327  *
5328  * If the second query fails with NXRRSET, the updater must conclude
5329  * that the client's desired name is in use by another host. At this
5330  * juncture, the updater can decide (based on some administrative
5331  * configuration outside of the scope of this document) whether to let
5332  * the existing owner of the name keep that name, and to (possibly)
5333  * perform some name disambiguation operation on behalf of the current
5334  * client, or to replace the RRs on the name with RRs that represent
5335  * the current client. If the configured policy allows replacement of
5336  * existing records, the updater submits a query that deletes the
5337  * existing A RR and the existing DHCID RR, adding A and DHCID RRs that
5338  * represent the IP address and client-identity of the new client.
5339  * -- "Interaction between DHCP and DNS"
5340  */
5341 
5342 /* The first and second stages are pretty similar so we combine them */
5343 void
5344 client_dns_update_action(dhcp_ddns_cb_t *ddns_cb,
5345  isc_result_t eresult)
5346 {
5347  isc_result_t result;
5348  struct timeval tv;
5349 
5350  switch(eresult) {
5351  case ISC_R_SUCCESS:
5352  default:
5353  /* Either we succeeded or broke in a bad way, clean up */
5354  break;
5355 
5356  case DNS_R_YXRRSET:
5357  /*
5358  * This is the only difference between the two stages,
5359  * check to see if it is the first stage, in which case
5360  * start the second stage
5361  */
5362  if (ddns_cb->state == DDNS_STATE_ADD_FW_NXDOMAIN) {
5363  ddns_cb->state = DDNS_STATE_ADD_FW_YXDHCID;
5364  ddns_cb->cur_func = client_dns_update_action;
5365 
5366  result = ddns_modify_fwd(ddns_cb, MDL);
5367  if (result == ISC_R_SUCCESS) {
5368  return;
5369  }
5370  }
5371  break;
5372 
5373  case ISC_R_TIMEDOUT:
5374  /*
5375  * We got a timeout response from the DNS module. Schedule
5376  * another attempt for later. We forget the name, dhcid and
5377  * zone so if it gets changed we will get the new information.
5378  */
5379  data_string_forget(&ddns_cb->fwd_name, MDL);
5380  data_string_forget(&ddns_cb->dhcid, MDL);
5381  if (ddns_cb->zone != NULL) {
5382  forget_zone((struct dns_zone **)&ddns_cb->zone);
5383  }
5384 
5385  /* Reset to doing the first stage */
5386  ddns_cb->state = DDNS_STATE_ADD_FW_NXDOMAIN;
5387  ddns_cb->cur_func = client_dns_update_action;
5388 
5389  /* and update our timer */
5390  if (ddns_cb->timeout < 3600)
5391  ddns_cb->timeout *= 10;
5392  tv.tv_sec = cur_tv.tv_sec + ddns_cb->timeout;
5393  tv.tv_usec = cur_tv.tv_usec;
5395  ddns_cb, NULL, NULL);
5396  return;
5397  }
5398 
5399  dhclient_ddns_cb_free(ddns_cb, MDL);
5400  return;
5401 }
5402 
5403 /* See if we should do a DNS update, and if so, do it. */
5404 
5405 isc_result_t
5406 client_dns_update(struct client_state *client, dhcp_ddns_cb_t *ddns_cb)
5407 {
5408  struct data_string client_identifier;
5409  struct option_cache *oc;
5410  int ignorep;
5411  int result;
5412  int ddns_v4_type;
5413  isc_result_t rcode;
5414 
5415  /* If we didn't send an FQDN option, we certainly aren't going to
5416  be doing an update. */
5417  if (!client -> sent_options)
5418  return ISC_R_SUCCESS;
5419 
5420  /* If we don't have a lease, we can't do an update. */
5421  if ((client->active == NULL) && (client->active_lease == NULL))
5422  return ISC_R_SUCCESS;
5423 
5424  /* If we set the no client update flag, don't do the update. */
5425  if ((oc = lookup_option (&fqdn_universe, client -> sent_options,
5427  evaluate_boolean_option_cache (&ignorep, (struct packet *)0,
5428  (struct lease *)0, client,
5429  client -> sent_options,
5430  (struct option_state *)0,
5431  &global_scope, oc, MDL))
5432  return ISC_R_SUCCESS;
5433 
5434  /* If we set the "server, please update" flag, or didn't set it
5435  to false, don't do the update. */
5436  if (!(oc = lookup_option (&fqdn_universe, client -> sent_options,
5437  FQDN_SERVER_UPDATE)) ||
5438  evaluate_boolean_option_cache (&ignorep, (struct packet *)0,
5439  (struct lease *)0, client,
5440  client -> sent_options,
5441  (struct option_state *)0,
5442  &global_scope, oc, MDL))
5443  return ISC_R_SUCCESS;
5444 
5445  /* If no FQDN option was supplied, don't do the update. */
5446  if (!(oc = lookup_option (&fqdn_universe, client -> sent_options,
5447  FQDN_FQDN)) ||
5448  !evaluate_option_cache (&ddns_cb->fwd_name, (struct packet *)0,
5449  (struct lease *)0, client,
5450  client -> sent_options,
5451  (struct option_state *)0,
5452  &global_scope, oc, MDL))
5453  return ISC_R_SUCCESS;
5454 
5455  /*
5456  * Construct the DHCID value for use in the DDNS update process
5457  * We have the newer standard version and the older interim version
5458  * chosen by the '-I' option. The interim version is left as is
5459  * for backwards compatibility. The standard version is based on
5460  * RFC 4701 section 3.3
5461  */
5462 
5463  result = 0;
5464  POST(result);
5465  memset(&client_identifier, 0, sizeof(client_identifier));
5466 
5467  if (std_dhcid == 1) {
5468  /* standard style */
5469  ddns_cb->dhcid_class = dns_rdatatype_dhcid;
5470  ddns_v4_type = 1;
5471  } else {
5472  /* interim style */
5473  ddns_cb->dhcid_class = dns_rdatatype_txt;
5474  /* for backwards compatibility */
5475  ddns_v4_type = DHO_DHCP_CLIENT_IDENTIFIER;
5476  }
5477  if (client->active_lease != NULL) {
5478  /* V6 request, get the client identifier, then
5479  * construct the dhcid for either standard
5480  * or interim */
5481  if (((oc = lookup_option(&dhcpv6_universe,
5482  client->sent_options,
5483  D6O_CLIENTID)) != NULL) &&
5484  evaluate_option_cache(&client_identifier, NULL,
5485  NULL, client,
5486  client->sent_options, NULL,
5487  &global_scope, oc, MDL)) {
5488  result = get_dhcid(ddns_cb, 2,
5489  client_identifier.data,
5490  client_identifier.len);
5491  data_string_forget(&client_identifier, MDL);
5492  } else
5493  log_fatal("Impossible condition at %s:%d.", MDL);
5494  } else {
5495  /*
5496  * V4 request, use the client id if there is one or the
5497  * mac address if there isn't. If we have a client id
5498  * we check to see if it is an embedded DUID.
5499  */
5500  if (((oc = lookup_option(&dhcp_universe,
5501  client->sent_options,
5502  DHO_DHCP_CLIENT_IDENTIFIER)) != NULL) &&
5503  evaluate_option_cache(&client_identifier, NULL,
5504  NULL, client,
5505  client->sent_options, NULL,
5506  &global_scope, oc, MDL)) {
5507  if ((std_dhcid == 1) && (duid_v4 == 1) &&
5508  (client_identifier.data[0] == 255)) {
5509  /*
5510  * This appears to be an embedded DUID,
5511  * extract it and treat it as such
5512  */
5513  if (client_identifier.len <= 5)
5514  log_fatal("Impossible condition at %s:%d.",
5515  MDL);
5516  result = get_dhcid(ddns_cb, 2,
5517  client_identifier.data + 5,
5518  client_identifier.len - 5);
5519  } else {
5520  result = get_dhcid(ddns_cb, ddns_v4_type,
5521  client_identifier.data,
5522  client_identifier.len);
5523  }
5524  data_string_forget(&client_identifier, MDL);
5525  } else
5526  result = get_dhcid(ddns_cb, 0,
5527  client->interface->hw_address.hbuf,
5528  client->interface->hw_address.hlen);
5529  }
5530 
5531  if (!result) {
5532  return ISC_R_SUCCESS;
5533  }
5534 
5535  /*
5536  * Perform updates.
5537  */
5538  if (ddns_cb->fwd_name.len && ddns_cb->dhcid.len) {
5539  rcode = ddns_modify_fwd(ddns_cb, MDL);
5540  } else
5541  rcode = ISC_R_FAILURE;
5542 
5543  /*
5544  * A success from the modify routine means we are performing
5545  * async processing, for which we use the timedout error message.
5546  */
5547  if (rcode == ISC_R_SUCCESS) {
5548  rcode = ISC_R_TIMEDOUT;
5549  }
5550 
5551  return rcode;
5552 }
5553 
5554 
5555 /*
5556  * Schedule the first update. They will continue to retry occasionally
5557  * until they no longer time out (or fail).
5558  */
5559 void
5561  struct iaddr *addr,
5562  int offset)
5563 {
5564  dhcp_ddns_cb_t *ddns_cb;
5565  struct timeval tv;
5566 
5567  if (!client->config->do_forward_update)
5568  return;
5569 
5570  /* cancel any outstanding ddns requests */
5571  if (client->ddns_cb != NULL) {
5572  ddns_cancel(client->ddns_cb, MDL);
5573  client->ddns_cb = NULL;
5574  }
5575 
5576  ddns_cb = ddns_cb_alloc(MDL);
5577 
5578  if (ddns_cb != NULL) {
5579  ddns_cb->lease = (void *)client;
5580  ddns_cb->address = *addr;
5581  ddns_cb->timeout = 1;
5582 
5583  /*
5584  * XXX: DNS TTL is a problem we need to solve properly.
5585  * Until that time, 300 is a placeholder default for
5586  * something that is less insane than a value scaled
5587  * by lease timeout.
5588  */
5589  ddns_cb->ttl = 300;
5590 
5591  ddns_cb->state = DDNS_STATE_ADD_FW_NXDOMAIN;
5592  ddns_cb->cur_func = client_dns_update_action;
5594 
5595  client->ddns_cb = ddns_cb;
5596  tv.tv_sec = cur_tv.tv_sec + offset;
5597  tv.tv_usec = cur_tv.tv_usec;
5599  ddns_cb, NULL, NULL);
5600  } else {
5601  log_error("Unable to allocate dns update state for %s",
5602  piaddr(*addr));
5603  }
5604 }
5605 #endif
5606 
5607 void
5609 {
5610  struct servent *ent;
5611 
5612  if (path_dhclient_pid == NULL)
5614  if (path_dhclient_db == NULL)
5616 
5617  /* Default to the DHCP/BOOTP port. */
5618  if (!local_port) {
5619  /* If we're faking a relay agent, and we're not using loopback,
5620  use the server port, not the client port. */
5621  if (mockup_relay && giaddr.s_addr != htonl(INADDR_LOOPBACK)) {
5622  local_port = htons(67);
5623  } else {
5624  ent = getservbyname("dhcpc", "udp");
5625  if (ent == NULL)
5626  ent = getservbyname("bootpc", "udp");
5627  if (ent == NULL)
5628  local_port = htons(68);
5629  else
5630  local_port = ent->s_port;
5631 #ifndef __CYGWIN32__
5632  endservent ();
5633 #endif
5634  }
5635  }
5636 
5637  /* If we're faking a relay agent, and we're not using loopback,
5638  we're using the server port, not the client port. */
5639  if (mockup_relay && giaddr.s_addr != htonl(INADDR_LOOPBACK)) {
5641  } else
5642  remote_port = htons(ntohs(local_port) - 1); /* XXX */
5643 }
5644 
5645 /*
5646  * The following routines are used to check that certain
5647  * strings are reasonable before we pass them to the scripts.
5648  * This avoids some problems with scripts treating the strings
5649  * as commands - see ticket 23722
5650  * The domain checking code should be done as part of assembling
5651  * the string but we are doing it here for now due to time
5652  * constraints.
5653  */
5654 
5655 static int check_domain_name(const char *ptr, size_t len, int dots)
5656 {
5657  const char *p;
5658 
5659  /* not empty or complete length not over 255 characters */
5660  if ((len == 0) || (len > 256))
5661  return(-1);
5662 
5663  /* consists of [[:alnum:]-]+ labels separated by [.] */
5664  /* a [_] is against RFC but seems to be "widely used"... */
5665  for (p=ptr; (*p != 0) && (len-- > 0); p++) {
5666  if ((*p == '-') || (*p == '_')) {
5667  /* not allowed at begin or end of a label */
5668  if (((p - ptr) == 0) || (len == 0) || (p[1] == '.'))
5669  return(-1);
5670  } else if (*p == '.') {
5671  /* each label has to be 1-63 characters;
5672  we allow [.] at the end ('foo.bar.') */
5673  size_t d = p - ptr;
5674  if ((d <= 0) || (d >= 64))
5675  return(-1);
5676  ptr = p + 1; /* jump to the next label */
5677  if ((dots > 0) && (len > 0))
5678  dots--;
5679  } else if (isalnum((unsigned char)*p) == 0) {
5680  /* also numbers at the begin are fine */
5681  return(-1);
5682  }
5683  }
5684  return(dots ? -1 : 0);
5685 }
5686 
5687 static int check_domain_name_list(const char *ptr, size_t len, int dots)
5688 {
5689  const char *p;
5690  int ret = -1; /* at least one needed */
5691 
5692  if ((ptr == NULL) || (len == 0))
5693  return(-1);
5694 
5695  for (p=ptr; (*p != 0) && (len > 0); p++, len--) {
5696  if (*p != ' ')
5697  continue;
5698  if (p > ptr) {
5699  if (check_domain_name(ptr, p - ptr, dots) != 0)
5700  return(-1);
5701  ret = 0;
5702  }
5703  ptr = p + 1;
5704  }
5705  if (p > ptr)
5706  return(check_domain_name(ptr, p - ptr, dots));
5707  else
5708  return(ret);
5709 }
5710 
5711 static int check_option_values(struct universe *universe,
5712  unsigned int opt,
5713  const char *ptr,
5714  size_t len)
5715 {
5716  if (ptr == NULL)
5717  return(-1);
5718 
5719  /* just reject options we want to protect, will be escaped anyway */
5720  if ((universe == NULL) || (universe == &dhcp_universe)) {
5721  switch(opt) {
5722  case DHO_DOMAIN_NAME:
5723 #ifdef ACCEPT_LIST_IN_DOMAIN_NAME
5724  return check_domain_name_list(ptr, len, 0);
5725 #else
5726  return check_domain_name(ptr, len, 0);
5727 #endif
5728  case DHO_HOST_NAME:
5729  case DHO_NIS_DOMAIN:
5730  case DHO_NETBIOS_SCOPE:
5731  return check_domain_name(ptr, len, 0);
5732  break;
5733  case DHO_DOMAIN_SEARCH:
5734  return check_domain_name_list(ptr, len, 0);
5735  break;
5736  case DHO_ROOT_PATH:
5737  if (len == 0)
5738  return(-1);
5739  for (; (*ptr != 0) && (len-- > 0); ptr++) {
5740  if(!(isalnum((unsigned char)*ptr) ||
5741  *ptr == '#' || *ptr == '%' ||
5742  *ptr == '+' || *ptr == '-' ||
5743  *ptr == '_' || *ptr == ':' ||
5744  *ptr == '.' || *ptr == ',' ||
5745  *ptr == '@' || *ptr == '~' ||
5746  *ptr == '\\' || *ptr == '/' ||
5747  *ptr == '[' || *ptr == ']' ||
5748  *ptr == '=' || *ptr == ' '))
5749  return(-1);
5750  }
5751  return(0);
5752  break;
5753  }
5754  }
5755 
5756 #ifdef DHCPv6
5757  if (universe == &dhcpv6_universe) {
5758  switch(opt) {
5759  case D6O_SIP_SERVERS_DNS:
5760  case D6O_DOMAIN_SEARCH:
5761  case D6O_NIS_DOMAIN_NAME:
5762  case D6O_NISP_DOMAIN_NAME:
5763  return check_domain_name_list(ptr, len, 0);
5764  break;
5765  }
5766  }
5767 #endif
5768 
5769  return(0);
5770 }
5771 
5772 static void
5773 add_reject(struct packet *packet) {
5774  struct iaddrmatchlist *list;
5775 
5776  list = dmalloc(sizeof(struct iaddrmatchlist), MDL);
5777  if (!list)
5778  log_fatal ("no memory for reject list!");
5779 
5780  /*
5781  * client_addr is misleading - it is set to source address in common
5782  * code.
5783  */
5784  list->match.addr = packet->client_addr;
5785  /* Set mask to indicate host address. */
5786  list->match.mask.len = list->match.addr.len;
5787  memset(list->match.mask.iabuf, 0xff, sizeof(list->match.mask.iabuf));
5788 
5789  /* Append to reject list for the source interface. */
5790  list->next = packet->interface->client->config->reject_list;
5791  packet->interface->client->config->reject_list = list;
5792 
5793  /*
5794  * We should inform user that we won't be accepting this server
5795  * anymore.
5796  */
5797  log_info("Server added to list of rejected servers.");
5798 }
5799 
5800 /* Wrapper function around common ddns_cb_free function that ensures
5801  * we set the client_state pointer to the control block to NULL. */
5802 static void
5803 dhclient_ddns_cb_free(dhcp_ddns_cb_t *ddns_cb, char* file, int line) {
5804  if (ddns_cb) {
5805  struct client_state *client = (struct client_state *)ddns_cb->lease;
5806  if (client != NULL) {
5807  client->ddns_cb = NULL;
5808  }
5809 
5810  ddns_cb_free(ddns_cb, file, line);
5811  }
5812 }
5813 
5814 #if defined(DHCPv6) && defined(DHCP4o6)
5815 /*
5816  * \brief Omapi I/O handler
5817  *
5818  * The inter-process communication receive handler.
5819  *
5820  * On the DHCPv6 side, the message is either a POLL (which is answered
5821  * by a START or a STOP) or a DHCPv4-QUERY (which is forwarded to
5822  * DHCPv4 over DHCPv6 servers by forw_dhcpv4_query()).
5823  *
5824  * On the DHCPv4 side, the message is either a START, a STOP
5825  * (both for the DHCP4 over DHCPv6 state machine) or a DHCPv4-RESPONSE
5826  * (which is processed by recv_dhcpv4_response()).
5827  *
5828  * \param h the OMAPI object
5829  * \return a result for I/O success or error (used by the I/O subsystem)
5830  */
5831 isc_result_t dhcpv4o6_handler(omapi_object_t *h) {
5832  char buf[65536];
5833  char start_msg[5] = { 'S', 'T', 'A', 'R', 'T' };
5834  char stop_msg[4] = { 'S', 'T', 'O', 'P' };
5835  char poll_msg[4] = { 'P', 'O', 'L', 'L' };
5836  struct data_string raw;
5837  int cc;
5838 
5839  if (h->type != dhcp4o6_type)
5840  return DHCP_R_INVALIDARG;
5841 
5842  cc = recv(dhcp4o6_fd, buf, sizeof(buf), 0);
5843  if (cc <= 0)
5844  return ISC_R_UNEXPECTED;
5845 
5846  if (local_family == AF_INET6) {
5847  if ((cc == 4) &&
5848  (memcmp(buf, poll_msg, sizeof(poll_msg)) == 0)) {
5849  log_info("RCV: POLL");
5850  if (dhcp4o6_state < 0)
5851  cc = send(dhcp4o6_fd, stop_msg,
5852  sizeof(stop_msg), 0);
5853  else
5854  cc = send(dhcp4o6_fd, start_msg,
5855  sizeof(start_msg), 0);
5856  if (cc < 0) {
5857  log_error("dhcpv4o6_handler: send(): %m");
5858  return ISC_R_IOERROR;
5859  }
5860  } else {
5861  if (cc < DHCP_FIXED_NON_UDP + 8)
5862  return ISC_R_UNEXPECTED;
5863  memset(&raw, 0, sizeof(raw));
5864  if (!buffer_allocate(&raw.buffer, cc, MDL)) {
5865  log_error("dhcpv4o6_handler: "
5866  "no memory buffer.");
5867  return ISC_R_NOMEMORY;
5868  }
5869  raw.data = raw.buffer->data;
5870  raw.len = cc;
5871  memcpy(raw.buffer->data, buf, cc);
5872 
5873  forw_dhcpv4_query(&raw);
5874 
5875  data_string_forget(&raw, MDL);
5876  }
5877  } else {
5878  if ((cc == 4) &&
5879  (memcmp(buf, stop_msg, sizeof(stop_msg)) == 0)) {
5880  log_info("RCV: STOP");
5881  if (dhcp4o6_state > 0) {
5882  dhcp4o6_state = 0;
5883  dhcp4o6_poll(NULL);
5884  }
5885  } else if ((cc == 5) &&
5886  (memcmp(buf, start_msg, sizeof(start_msg)) == 0)) {
5887  log_info("RCV: START");
5888  if (dhcp4o6_state == 0)
5889  cancel_timeout(dhcp4o6_poll, NULL);
5890  dhcp4o6_state = 1;
5891  dhcp4o6_resume();
5892  } else {
5893  if (cc < DHCP_FIXED_NON_UDP + 16)
5894  return ISC_R_UNEXPECTED;
5895  memset(&raw, 0, sizeof(raw));
5896  if (!buffer_allocate(&raw.buffer, cc, MDL)) {
5897  log_error("dhcpv4o6_handler: "
5898  "no memory buffer.");
5899  return ISC_R_NOMEMORY;
5900  }
5901  raw.data = raw.buffer->data;
5902  raw.len = cc;
5903  memcpy(raw.buffer->data, buf, cc);
5904 
5905  recv_dhcpv4_response(&raw);
5906 
5907  data_string_forget(&raw, MDL);
5908  }
5909  }
5910 
5911  return ISC_R_SUCCESS;
5912 }
5913 
5914 /*
5915  * \brief Poll the DHCPv6 client
5916  * (DHCPv4 client function)
5917  *
5918  * A POLL message is sent to the DHCPv6 client periodically to check
5919  * if the DHCPv6 is ready (i.e., has a valid DHCPv4-over-DHCPv6 server
5920  * address option).
5921  */
5922 static void dhcp4o6_poll(void *dummy) {
5923  char msg[4] = { 'P', 'O', 'L', 'L' };
5924  struct timeval tv;
5925  int cc;
5926 
5927  IGNORE_UNUSED(dummy);
5928 
5929  if (dhcp4o6_state < 0)
5930  dhcp4o6_state = 0;
5931 
5932  log_info("POLL");
5933 
5934  cc = send(dhcp4o6_fd, msg, sizeof(msg), 0);
5935  if (cc < 0)
5936  log_error("dhcp4o6_poll: send(): %m");
5937 
5938  tv.tv_sec = cur_time + 60;
5939  tv.tv_usec = random() % 1000000;
5940 
5941  add_timeout(&tv, dhcp4o6_poll, NULL, 0, 0);
5942 }
5943 
5944 /*
5945  * \brief Resume pending operations
5946  * (DHCPv4 client function)
5947  *
5948  * A START message was received from the DHCPv6 client so pending
5949  * operations (RELEASE or REBOOT) must be resumed.
5950  */
5951 static void dhcp4o6_resume() {
5952  struct interface_info *ip;
5953  struct client_state *client;
5954 
5955  for (ip = interfaces; ip != NULL; ip = ip->next) {
5956  for (client = ip->client; client != NULL;
5957  client = client->next) {
5958  if (client->pending == P_RELEASE)
5959  do_release(client);
5960  else if (client->pending == P_REBOOT)
5961  state_reboot(client);
5962  }
5963  }
5964 }
5965 
5966 /*
5967  * \brief Send a START to the DHCPv4 client
5968  * (DHCPv6 client function)
5969  *
5970  * First check if there is a valid DHCPv4-over-DHCPv6 server address option,
5971  * and when found go UP and on a transition from another state send
5972  * a START message to the DHCPv4 client.
5973  */
5974 void dhcp4o6_start() {
5975  struct interface_info *ip;
5976  struct client_state *client;
5977  struct dhc6_lease *lease;
5978  struct option_cache *oc;
5979  struct data_string addrs;
5980  char msg[5] = { 'S', 'T', 'A', 'R', 'T' };
5981  int cc;
5982 
5983  memset(&addrs, 0, sizeof(addrs));
5984  for (ip = interfaces; ip != NULL; ip = ip->next) {
5985  for (client = ip->client; client != NULL;
5986  client = client->next) {
5987  if ((client->state != S_BOUND) &&
5988  (client->state != S_RENEWING) &&
5989  (client->state != S_REBINDING))
5990  continue;
5991  lease = client->active_lease;
5992  if ((lease == NULL) || lease->released)
5993  continue;
5995  lease->options,
5997  if ((oc == NULL) ||
5998  !evaluate_option_cache(&addrs, NULL, NULL, NULL,
5999  lease->options, NULL,
6000  &global_scope, oc, MDL))
6001  continue;
6002  if ((addrs.len % 16) != 0) {
6003  data_string_forget(&addrs, MDL);
6004  continue;
6005  }
6006  data_string_forget(&addrs, MDL);
6007  goto found;
6008  }
6009  }
6010  log_info("dhcp4o6_start: failed");
6011  dhcp4o6_stop();
6012  return;
6013 
6014 found:
6015  if (dhcp4o6_state == 1)
6016  return;
6017  log_info("dhcp4o6_start: go to UP");
6018  dhcp4o6_state = 1;
6019 
6020  cc = send(dhcp4o6_fd, msg, sizeof(msg), 0);
6021  if (cc < 0)
6022  log_info("dhcp4o6_start: send(): %m");
6023 }
6024 
6025 /*
6026  * Send a STOP to the DHCPv4 client
6027  * (DHCPv6 client function)
6028  *
6029  * Go DOWN and on a transition from another state send a STOP message
6030  * to the DHCPv4 client.
6031  */
6032 static void dhcp4o6_stop() {
6033  char msg[4] = { 'S', 'T', 'O', 'P' };
6034  int cc;
6035 
6036  if (dhcp4o6_state == -1)
6037  return;
6038 
6039  log_info("dhcp4o6_stop: go to DOWN");
6040  dhcp4o6_state = -1;
6041 
6042  cc = send(dhcp4o6_fd, msg, sizeof(msg), 0);
6043  if (cc < 0)
6044  log_error("dhcp4o6_stop: send(): %m");
6045 }
6046 #endif /* DHCPv6 && DHCP4o6 */
#define BOOTREPLY
Definition: dhcp.h:70
void do_packet6(struct interface_info *, const char *, int, int, const struct iaddr *, isc_boolean_t)
void state_selecting(void *cpp)
Definition: dhclient.c:1581
#define DHCP_FIXED_NON_UDP
Definition: dhcp.h:37
#define _PATH_DHCLIENT_CONF
Definition: dhcpd.h:1557
void(* dhcpv6_packet_handler)(struct interface_info *, const char *, int, int, const struct iaddr *, isc_boolean_t)
void send_discover(void *cpp)
Definition: dhclient.c:2662
void unbill_class(struct lease *lease)
Definition: dhclient.c:1428
struct client_lease * alias
Definition: dhcpd.h:1284
#define IGNORE_UNUSED(x)
Definition: cdefs.h:68
int parse_encapsulated_suboptions(struct option_state *options, struct option *eopt, const unsigned char *buffer, unsigned len, struct universe *eu, const char *uname)
Definition: options.c:318
isc_result_t omapi_protocol_listen(omapi_object_t *, unsigned, int)
Definition: protocol.c:998
TIME interval
Definition: dhcpd.h:1290
const char int line
Definition: dhcpd.h:3723
u_int8_t plen
Definition: dhcpd.h:1132
struct binding_scope * global_scope
Definition: tree.c:38
struct dns_zone * zone
Definition: dhcpd.h:1785
#define _PATH_DHCLIENT_PID
Definition: config.h:250
struct universe * universe
Definition: tree.h:349
int interfaces_requested
Definition: dhclient.c:68
void make_client_options(struct client_state *client, struct client_lease *lease, u_int8_t *type, struct option_cache *sid, struct iaddr *rip, struct option **prl, struct option_state **op)
Definition: dhclient.c:3385
struct group * on_receipt
Definition: dhcpd.h:1203
unsigned char dhcpv6_transaction_id[3]
Definition: dhcpd.h:1298
Definition: dhcpd.h:556
#define _PATH_DHCLIENT_SCRIPT
Definition: dhcpd.h:1561
void save_option(struct universe *universe, struct option_state *options, struct option_cache *oc)
Definition: options.c:2764
unsigned len
Definition: tree.h:80
struct client_lease * new
Definition: dhcpd.h:1281
void do_release(struct client_state *client)
Definition: dhclient.c:4942
const char * piaddr(const struct iaddr addr)
Definition: inet.c:579
u_int8_t hlen
Definition: dhcpd.h:489
void rewrite_client_leases()
Definition: dhclient.c:3821
#define FQDN_NO_CLIENT_UPDATE
Definition: dhcp.h:193
#define DHO_DOMAIN_SEARCH
Definition: dhcp.h:164
int do_forward_update
Definition: dhcpd.h:1252
#define DDNS_STATE_ADD_FW_NXDOMAIN
Definition: dhcpd.h:1753
dhcp_state
Definition: dhcpd.h:1174
void dhcpoffer(struct packet *packet)
Definition: dhclient.c:2325
int no_daemon
Definition: dhclient.c:96
u_int32_t renew
Definition: dhcpd.h:1152
unsigned char dhcpv6_transaction_id[3]
Definition: dhcpd.h:414
char name[IFNAMSIZ]
Definition: dhcpd.h:1375
int 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
void dhcpnak(struct packet *packet)
Definition: dhclient.c:2583
int get_dhcid(dhcp_ddns_cb_t *, int, const u_int8_t *, unsigned)
isc_result_t end_parse(struct parse **cfile)
Definition: conflex.c:103
const char * path_dhclient_db
Definition: dhclient.c:57
void(* bootp_packet_handler)(struct interface_info *, struct dhcp_packet *, unsigned, unsigned int, struct iaddr, struct hardware *)
Definition: discover.c:58
#define All_DHCP_Relay_Agents_and_Servers
Definition: dhcp6.h:187
Definition: dhcpd.h:1188
#define DDNS_UPDATE_ADDR
Definition: dhcpd.h:1738
int tag_size
Definition: tree.h:335
void start_release6(struct client_state *client)
char * piaddrmask(struct iaddr *addr, struct iaddr *mask)
Definition: inet.c:606
struct iaddr next_srv_addr
Definition: dhcpd.h:1125
int option_cache_dereference(struct option_cache **ptr, const char *file, int line)
Definition: options.c:2899
enum dhcp_token token
Definition: dhcpd.h:320
int stateless
Definition: dhclient.c:102
int duid_type
Definition: dhclient.c:76
void start_info_request6(struct client_state *client)
Definition: dhcpd.h:1044
TIME first_sending
Definition: dhcpd.h:1289
void send_decline(void *cpp)
Definition: dhclient.c:3108
#define STDERR_FILENO
Definition: osdep.h:288
#define HTYPE_RESERVED
Definition: dhcp.h:84
void client_dns_update_timeout(void *cp)
#define MDL
Definition: omapip.h:568
void cancel_timeout(void(*)(void *) where, void *what)
Definition: dispatch.c:381
unsigned char iabuf[16]
Definition: inet.h:33
#define print_hex_1(len, data, limit)
Definition: dhcpd.h:2575
Definition: dhcpd.h:1176
#define DHO_DHCP_PARAMETER_REQUEST_LIST
Definition: dhcp.h:147
FILE * leaseFile
Definition: dhclient.c:3818
int lease_id_format
Definition: dhcpd.h:1256
int dhcp_max_agent_option_packet_length
Definition: dhclient.c:66
struct client_lease * packet_to_lease(struct packet *packet, struct client_state *client)
Definition: dhclient.c:2460
#define DHCP_R_INVALIDARG
Definition: result.h:48
struct group * on_transmission
Definition: dhcpd.h:1208
#define DISCOVER_REQUESTED
Definition: dhcpd.h:697
#define DHCP_SNAME_LEN
Definition: dhcp.h:35
#define DHO_NIS_DOMAIN
Definition: dhcp.h:132
int script_go(struct client_state *client)
Calls external script.
Definition: dhclient.c:4681
struct iaddr requested_address
Definition: dhcpd.h:1295
const char * dhcpv6_type_names[]
Definition: tables.c:656
int int int log_debug(const char *,...) __attribute__((__format__(__printf__
int write_client_lease(struct client_state *client, struct client_lease *lease, int rewrite, int makesure)
Definition: dhclient.c:4281
struct client_state * client
Definition: dhcpd.h:1398
#define DHCPV6_REPLY
Definition: dhcp6.h:144
int can_receive_unicast_unconfigured(struct interface_info *)
struct iaddr iaddr_broadcast
Definition: dhclient.c:70
void reinitialize_interfaces()
Definition: discover.c:1024
FILE * scriptFile
Definition: dhclient.c:4423
unsigned char msg_type
Definition: dhcp6.h:250
#define DHCPV6_RECONFIGURE
Definition: dhcp6.h:147
struct client_state * next
Definition: dhcpd.h:1266
#define DHCP_CONTEXT_PRE_DB
Definition: isclib.h:130
#define DHO_DHCP_LEASE_TIME
Definition: dhcp.h:143
void dhcpack(struct packet *packet)
Definition: dhclient.c:1666
unsigned cons_agent_information_options(struct option_state *cfg_options, struct dhcp_packet *outpacket, unsigned agentix, unsigned length)
Definition: dhclient.c:5132
struct universe dhcp_universe
int wanted_ia_pd
Definition: dhclient.c:105
struct option_state * options
Definition: dhcpd.h:1143
int dhcpv4_over_dhcpv6
Definition: discover.c:47
dhcp_ddns_cb_t * ddns_cb_alloc(const char *file, int line)
void data_string_forget(struct data_string *data, const char *file, int line)
Definition: alloc.c:1339
void bootp(struct packet *packet)
Definition: dhclient.c:2005
#define DHCPACK
Definition: dhcp.h:176
int duid_v4
Definition: dhclient.c:77
const char * pretty_print_option(struct option *option, const unsigned char *data, unsigned len, int emit_commas, int emit_quotes)
Definition: options.c:1769
#define DHO_SUBNET_MASK
Definition: dhcp.h:93
#define INTERFACE_RUNNING
Definition: dhcpd.h:1392
struct dhc6_ia * next
Definition: dhcpd.h:1147
#define DHO_ROOT_PATH
Definition: dhcp.h:109
#define DUID_LL
Definition: dhcp6.h:167
#define BOOTP_BROADCAST
Definition: dhcp.h:73
TIME last_write
Definition: dhcpd.h:1276
void send_release(void *cpp)
Definition: dhclient.c:3147
int log_error(const char *,...) __attribute__((__format__(__printf__
struct string_list * client_env
Definition: dhclient.c:97
#define DDNS_INCLUDE_RRSET
Definition: dhcpd.h:1740
struct in_addr siaddr
Definition: dhcp.h:58
void client_envadd(struct client_state *client, const char *prefix, const char *name, const char *fmt,...)
Definition: dhclient.c:4761
void add_timeout(struct timeval *when, void(*)(void *) where, void *what, tvref_t ref, tvunref_t unref)
Definition: dispatch.c:197
void dump_packet(struct packet *)
TIME initial_delay
Definition: dhcpd.h:1216
#define DDNS_STATE_REM_FW_YXDHCID
Definition: dhcpd.h:1757
#define DHO_DHCP_REBINDING_TIME
Definition: dhcp.h:151
#define DHO_NETBIOS_SCOPE
Definition: dhcp.h:139
unsigned len
Definition: inet.h:32
struct iaddr destination
Definition: dhcpd.h:1286
TIME backoff_cutoff
Definition: dhcpd.h:1230
#define DHCPV6_DHCPV4_QUERY
Definition: dhcp6.h:157
char scriptName[256]
Definition: dhclient.c:4422
#define D6O_DHCPV4_MSG
Definition: dhcp6.h:116
void dhcp4o6_start(void)
unsigned char flags[3]
Definition: dhcp6.h:251
struct dhc6_ia * bindings
Definition: dhcpd.h:1168
const char * path_dhclient_duid
Definition: dhclient.c:61
enum dhcp_token peek_token(const char **rval, unsigned *rlen, struct parse *cfile)
Definition: conflex.c:443
struct data_string fwd_name
Definition: dhcpd.h:1773
void write_client_pid_file()
Definition: dhclient.c:4881
#define D6O_CLIENTID
Definition: dhcp6.h:30
void state_panic(void *cpp)
Definition: dhclient.c:2792
#define DHO_DOMAIN_NAME
Definition: dhcp.h:107
#define DHCPRELEASE
Definition: dhcp.h:178
void forget_zone(struct dns_zone **)
struct data_string default_duid
Definition: dhclient.c:75
char * filename
Definition: dhcpd.h:1117
struct option_state * options
Definition: dhcpd.h:449
#define BOOTP_MIN_LEN
Definition: dhcp.h:40
Definition: dhcpd.h:288
void ddns_cb_free(dhcp_ddns_cb_t *ddns_cb, const char *file, int line)
struct iaddr address
Definition: dhcpd.h:1776
unsigned long ttl
Definition: dhcpd.h:1779
Definition: tree.h:302
void do_packet(struct interface_info *interface, struct dhcp_packet *packet, unsigned len, unsigned int from_port, struct iaddr from, struct hardware *hfrom)
Definition: options.c:3993
void dispatch(void)
Definition: dispatch.c:109
#define DHCP_LOG_OPTIONS
Definition: dhcpd.h:1602
#define D6O_NIS_DOMAIN_NAME
Definition: dhcp6.h:58
void * lease
Definition: dhcpd.h:1795
unsigned char dhcpv6_msg_type
Definition: dhcpd.h:411
unsigned char iaid[4]
Definition: dhcpd.h:1148
#define DHO_DHCP_SERVER_IDENTIFIER
Definition: dhcp.h:146
void log_fatal(const char *,...) __attribute__((__format__(__printf__
int client_port
Definition: dhcpd.h:431
void(* v6_handler)(struct packet *, struct client_state *)
Definition: dhcpd.h:1324
#define D6O_IA_TA
Definition: dhcp6.h:33
#define DHCP_CONTEXT_POST_DB
Definition: isclib.h:131
enum dhcp_pending pending
Definition: dhcpd.h:1277
isc_result_t form_duid(struct data_string *duid, const char *file, int line)
Definition: dhclient.c:4007
struct executable_statement * statements
Definition: dhcpd.h:939
void state_init(void *cpp)
Definition: dhclient.c:1544
#define INTERFACE_AUTOMATIC
Definition: dhcpd.h:1391
struct data_string dhcid
Definition: dhcpd.h:1775
int option_state_reference(struct option_state **ptr, struct option_state *bp, const char *file, int line)
Definition: alloc.c:883
struct dhcp_packet * raw
Definition: dhcpd.h:406
#define MIN_LEASE_WRITE
Definition: dhcpd.h:850
struct option * default_requested_options[]
Definition: clparse.c:36
void read_client_leases()
Definition: clparse.c:366
struct option_state * options
Definition: dhcpd.h:1170
struct iaddr subnet_number(struct iaddr addr, struct iaddr mask)
Definition: inet.c:34
u_int16_t validate_port(char *port)
Definition: inet.c:659
void dhcp_signal_handler(int signal)
Definition: isclib.c:332
int find_subnet(struct subnet **sp, struct iaddr addr, const char *file, int line)
Definition: dhclient.c:1433
void execute_statements_in_scope(struct binding_value **result, struct packet *packet, struct lease *lease, struct client_state *client_state, struct option_state *in_options, struct option_state *out_options, struct binding_scope **scope, struct group *group, struct group *limiting_group, struct on_star *on_star)
Definition: execute.c:562
void make_request(struct client_state *client, struct client_lease *lease)
Definition: dhclient.c:3600
char * name
Definition: dhcpd.h:1268
#define DUID_TIME_EPOCH
Definition: dhcp6.h:273
struct interface_info * fallback_interface
Definition: discover.c:42
isc_result_t dhclient_interface_startup_hook(struct interface_info *interface)
Definition: dhclient.c:5070
unsigned packet_length
Definition: dhcpd.h:1293
int option_state_allocate(struct option_state **ptr, const char *file, int line)
Definition: alloc.c:846
const char * path_dhclient_pid
Definition: dhclient.c:58
struct iaddrmatchlist * next
Definition: inet.h:61
void client_option_envadd(struct option_cache *oc, struct packet *packet, struct lease *lease, struct client_state *client_state, struct option_state *in_options, struct option_state *cfg_options, struct binding_scope **scope, struct universe *u, void *stuff)
Definition: dhclient.c:4468
isc_result_t dhcp_context_create(int flags, struct in_addr *local4, struct in6_addr *local6)
Definition: isclib.c:138
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
TIME expiry
Definition: dhcpd.h:1114
#define DHCPV6_DHCPV4_RESPONSE
Definition: dhcp6.h:158
Definition: tree.h:346
int write_host(struct host_decl *host)
Definition: dhclient.c:1994
struct option_state * options
Definition: dhcpd.h:1124
#define DHCPNAK
Definition: dhcp.h:177
struct option ** requested_options
Definition: dhcpd.h:1211
void classify(struct packet *packet, struct class *class)
Definition: dhclient.c:1422
int require_all_ias
Definition: dhclient.c:106
void script_init(struct client_state *client, const char *reason, struct string_list *medium)
Initializes basic variables for a script.
Definition: dhclient.c:4437
#define DHCP_MAX_OPTION_LEN
Definition: dhcp.h:45
int main(int argc, char **argv)
Definition: dhclient.c:207
int options_valid
Definition: dhcpd.h:430
void(* store_length)(unsigned char *, u_int32_t)
Definition: tree.h:334
dns_rdataclass_t dhcid_class
Definition: dhcpd.h:1801
void make_decline(struct client_state *client, struct client_lease *lease)
Definition: dhclient.c:3690
#define HTYPE_INFINIBAND
Definition: dhcp.h:79
#define DHCP_DNS_CLIENT_LAZY_INIT
Definition: isclib.h:132
void bind_lease(struct client_state *client)
Definition: dhclient.c:1835
int buffer_allocate(struct buffer **ptr, unsigned len, const char *file, int line)
Definition: alloc.c:679
#define DHO_BROADCAST_ADDRESS
Definition: dhcp.h:120
TIME timeout
Definition: dhcpd.h:1788
struct option_cache * option
Definition: statement.h:66
struct interface_info * interface
Definition: dhcpd.h:433
isc_result_t read_uuid(u_int8_t *uuid)
Definition: dhclient.c:3954
unsigned code
Definition: tree.h:350
ssize_t send_packet6(struct interface_info *, const unsigned char *, size_t, struct sockaddr_in6 *)
int write_lease(struct lease *lease)
Definition: dhclient.c:1988
struct group * next
Definition: dhcpd.h:932
void putULong(unsigned char *, u_int32_t)
Definition: convert.c:70
const char * prefix
Definition: dhcpd.h:1337
u_int16_t local_port
Definition: dhclient.c:91
Definition: dhcpd.h:405
void run_stateless(int exit_mode, u_int16_t port)
Definition: dhclient.c:1291
void send_request(void *cpp)
Definition: dhclient.c:2909
isc_result_t omapi_generic_new(omapi_object_t **, const char *, int)
#define D6O_DOMAIN_SEARCH
Definition: dhcp6.h:53
struct in_addr yiaddr
Definition: dhcp.h:57
#define cur_time
Definition: dhcpd.h:2077
struct iaddr iaddr_any
Definition: dhclient.c:71
int quiet
Definition: dhclient.c:100
Definition: ip.h:47
ssize_t send_packet(struct interface_info *, struct packet *, struct dhcp_packet *, size_t, struct in_addr, struct sockaddr_in *, struct hardware *)
int cons_options(struct packet *inpacket, struct dhcp_packet *outpacket, struct lease *lease, struct client_state *client_state, int mms, struct option_state *in_options, struct option_state *cfg_options, struct binding_scope **scope, int overload_avail, int terminate, int bootpp, struct data_string *prl, const char *vuname)
Definition: options.c:519
void start_confirm6(struct client_state *client)
struct in_addr giaddr
Definition: dhclient.c:74
void dfree(void *, const char *, int)
Definition: alloc.c:145
isc_boolean_t no_pid_file
Definition: dhclient.c:64
u_int32_t max_life
Definition: dhcpd.h:1141
struct client_lease * next
Definition: dhcpd.h:1113
void ddns_cancel(dhcp_ddns_cb_t *ddns_cb, const char *file, int line)
#define FQDN_FQDN
Definition: dhcp.h:200
const char * name
Definition: tree.h:347
struct option_state * sent_options
Definition: dhcpd.h:1274
#define DISCOVER_RUNNING
Definition: dhcpd.h:692
const char * path_dhclient_conf
Definition: dhclient.c:56
#define BOOTREQUEST
Definition: dhcp.h:69
struct hardware hw_address
Definition: dhcpd.h:1353
int packet_type
Definition: dhcpd.h:409
char * mockup_relay
Definition: dhclient.c:110
struct option_cache * lookup_option(struct universe *universe, struct option_state *options, unsigned code)
Definition: options.c:2449
#define DHCPDECLINE
Definition: dhcp.h:175
int dhclient_interface_discovery_hook(struct interface_info *tmp)
Definition: dhclient.c:5027
struct client_state * client
Definition: dhcpd.h:1336
struct option_state * options
Definition: dhcpd.h:1156
int omapi_port
Definition: dhcpd.h:1249
struct option * option
Definition: dhcpd.h:389
int unhexchar(char c)
Definition: dhclient.c:3939
int asprintf(char **strp, const char *fmt,...)
control_object_state_t
Definition: dhcpd.h:519
int int log_info(const char *,...) __attribute__((__format__(__printf__
int bootp_broadcast_always
Definition: dhclient.c:114
enum dhcp_state state
Definition: dhcpd.h:1275
u_int16_t validate_port_pair(char *port)
Definition: inet.c:685
void * dmalloc(size_t, const char *, int)
Definition: alloc.c:57
int parse_options(struct packet *packet)
Definition: options.c:47
struct interface_info * interfaces
Definition: discover.c:42
void free_client_lease(struct client_lease *lease, const char *file, int line)
Definition: alloc.c:369
#define _PATH_DHCLIENT_DB
Definition: config.h:247
u_int32_t flags
Definition: dhcpd.h:1389
u_int32_t getULong(const unsigned char *)
int validate_packet(struct packet *packet)
Definition: options.c:4429
struct client_config top_level_config
Definition: clparse.c:32
struct iaddr broadcast_addr(struct iaddr subnet, struct iaddr mask)
Definition: inet.c:112
#define DHCPDISCOVER
Definition: dhcp.h:172
u_int32_t rebind
Definition: dhcpd.h:1153
struct option ** required_options
Definition: dhcpd.h:1210
struct dhc6_addr * addrs
Definition: dhcpd.h:1154
union executable_statement::@7 data
void state_reboot(void *cpp)
Definition: dhclient.c:1491
int check_collection(struct packet *packet, struct lease *lease, struct collection *collection)
Definition: dhclient.c:1414
void destroy_client_lease(struct client_lease *lease)
Definition: dhclient.c:3807
void db_startup(int testp)
Definition: dhclient.c:2000
int parse_agent_information_option(struct packet *packet, int len, u_int8_t *data)
Definition: dhclient.c:5122
TIME retry_interval
Definition: dhcpd.h:1220
#define ASSERT_STATE(state_is, state_shouldbe)
Definition: dhclient.c:82
int std_dhcid
Definition: dhclient.c:78
char * path_dhclient_script
Definition: dhclient.c:60
void parse_client_statement(struct parse *cfile, struct interface_info *ip, struct client_config *config)
Definition: clparse.c:435
struct universe ** universes
Definition: tables.c:963
Definition: inet.h:31
#define DHCP4O6_QUERY_UNICAST
Definition: dhcp6.h:254
TIME max_lease_time
Definition: dhclient.c:54
int local_family
Definition: discover.c:55
int shutdown_signal
Definition: isclib.c:34
int quiet_interface_discovery
Definition: discover.c:44
isc_result_t(* dhcp_interface_startup_hook)(struct interface_info *)
Definition: discover.c:50
#define DISCOVER_UNCONFIGURED
Definition: dhcpd.h:694
struct client_lease * active
Definition: dhcpd.h:1280
isc_result_t client_dns_update(struct client_state *client, dhcp_ddns_cb_t *ddns_cb)
const char * format
Definition: tree.h:348
u_int32_t preferred_life
Definition: dhcpd.h:1140
int option_state_dereference(struct option_state **ptr, const char *file, int line)
Definition: alloc.c:911
void start_init6(struct client_state *client)
void option_space_foreach(struct packet *packet, struct lease *lease, struct client_state *client_state, struct option_state *in_options, struct option_state *cfg_options, struct binding_scope **scope, struct universe *u, void *stuff, void(*func)(struct option_cache *, struct packet *, struct lease *, struct client_state *, struct option_state *, struct option_state *, struct binding_scope **, struct universe *, void *))
Definition: options.c:3737
Definition: dhcpd.h:931
struct dhc6_addr * next
Definition: dhcpd.h:1130
void initialize_common_option_spaces()
Definition: tables.c:1049
void make_discover(struct client_state *client, struct client_lease *lease)
Definition: dhclient.c:3535
int leases_written
Definition: dhclient.c:3819
void dhcpv6(struct packet *)
struct timeval cur_tv
Definition: dispatch.c:35
ddns_action_t cur_func
Definition: dhcpd.h:1790
void unconfigure6(struct client_state *client, const char *reason)
void client_dns_remove(struct client_state *client, struct iaddr *addr)
const int dhcpv6_type_name_max
Definition: tables.c:680
int addr_match(struct iaddr *addr, struct iaddrmatch *match)
Definition: inet.c:184
struct dhcp_packet packet
Definition: dhcpd.h:1292
void state_bound(void *cpp)
Definition: dhclient.c:1917
struct interface_info * next
Definition: dhcpd.h:1350
struct universe dhcpv6_universe
Definition: tables.c:343
struct iaddr addr
Definition: inet.h:54
int evaluate_boolean_option_cache(int *ignorep, 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:2733
#define D6O_IA_NA
Definition: dhcp6.h:32
#define TIME_MAX
Definition: osdep.h:83
int packet_dereference(struct packet **ptr, const char *file, int line)
Definition: alloc.c:1081
int packet_allocate(struct packet **ptr, const char *file, int line)
Definition: alloc.c:1015
int warnings_occurred
Definition: dhcpd.h:326
struct host_decl * host
Definition: dhcpd.h:572
void make_release(struct client_state *client, struct client_lease *lease)
Definition: dhclient.c:3750
struct string_list * next
Definition: dhcpd.h:348
TIME initial_interval
Definition: dhcpd.h:1218
const char int
Definition: omapip.h:443
#define DHCPV6_ADVERTISE
Definition: dhcp6.h:139
int onetry
Definition: dhclient.c:99
void read_client_duid()
Definition: clparse.c:330
isc_result_t read_client_conf()
Definition: clparse.c:55
struct interface_info * dummy_interfaces
Definition: discover.c:42
isc_result_t find_class(struct class **c, const char *s, const char *file, int line)
Definition: dhclient.c:1408
void script_write_requested(struct client_state *client)
Write out the environent variable the client requested. Write out the environment variables for the o...
Definition: dhclient.c:4651
int universe_count
Definition: dhcpd.h:398
char * progname
Definition: dhclient.c:112
time_t TIME
Definition: dhcpd.h:85
char string[1]
Definition: dhcpd.h:349
#define FQDN_SERVER_UPDATE
Definition: dhcp.h:194
char * script_name
Definition: dhcpd.h:1236
struct client_lease * new_client_lease(char *file, int line) const
Definition: alloc.c:361
int commit_leases()
Definition: dhclient.c:1983
unsigned char data[1]
Definition: tree.h:63
Definition: tree.h:61
#define DHCP_FILE_LEN
Definition: dhcp.h:36
u_int32_t xid
Definition: dhcpd.h:1287
int length_size
Definition: tree.h:335
int state
Definition: dhcpd.h:1789
#define DDNS_STATE_ADD_FW_YXDHCID
Definition: dhcpd.h:1754
#define D6O_DHCP4_O_DHCP6_SERVER
Definition: dhcp6.h:117
void dhcpv4_client_assignments(void)
Definition: dhclient.c:5608
TIME renewal
Definition: dhcpd.h:1114
struct iaddr address
Definition: dhcpd.h:1131
struct iaddr mask
Definition: inet.h:55
void dhcpv6_client_assignments(void)
u_int8_t hbuf[HARDWARE_ADDR_LEN+1]
Definition: dhcpd.h:490
struct iaddrmatchlist * reject_list
Definition: dhcpd.h:1247
struct string_list * medium
Definition: dhcpd.h:1118
int wanted_ia_na
Definition: dhclient.c:103
struct client_config * config
Definition: dhcpd.h:1271
int wanted_ia_ta
Definition: dhclient.c:104
u_int16_t flags
Definition: dhcpd.h:1787
struct iaddrmatch match
Definition: inet.h:62
#define D6O_SIP_SERVERS_DNS
Definition: dhcp6.h:50
struct sockaddr_in sockaddr_broadcast
Definition: dhclient.c:73
struct iaddr client_addr
Definition: dhcpd.h:432
void dhclient_schedule_updates(struct client_state *client, struct iaddr *addr, int offset)
#define DHCPREQUEST
Definition: dhcp.h:174
TIME rebind
Definition: dhcpd.h:1114
#define PACKAGE_VERSION
Definition: config.h:168
int dhcp_option_ev_name(char *buf, size_t buflen, struct option *option)
Definition: dhclient.c:4799
#define D6O_IA_PD
Definition: dhcp6.h:54
int(* dhcp_interface_discovery_hook)(struct interface_info *)
Definition: discover.c:49
void go_daemon()
Definition: dhclient.c:4839
struct in_addr inaddr_any
Definition: dhclient.c:72
struct universe fqdn_universe
Definition: tables.c:310
#define DUID_LLT
Definition: dhcp6.h:165
void dhcp(struct packet *packet)
Definition: dhclient.c:2038
#define DHO_DHCP_OPTION_OVERLOAD
Definition: dhcp.h:144
#define D6O_NISP_DOMAIN_NAME
Definition: dhcp6.h:59
option_code_hash_t * code_hash
Definition: tree.h:338
int nowait
Definition: dhclient.c:101
u_int16_t remote_port
Definition: dhclient.c:92
#define DHO_DHCP_RENEWAL_TIME
Definition: dhcp.h:150
struct iaddr address
Definition: dhcpd.h:1115
TIME timeout
Definition: dhcpd.h:1213
struct string_list * medium
Definition: dhcpd.h:1291
unsigned int is_bootp
Definition: dhcpd.h:1122
const char * file
Definition: dhcpd.h:3723
#define DHO_DHCP_CLIENT_IDENTIFIER
Definition: dhcp.h:153
struct dhcp_ddns_cb * ddns_cb
Definition: dhcpd.h:1332
void putUShort(unsigned char *, u_int32_t)
Definition: convert.c:86
TIME default_lease_time
Definition: dhclient.c:53
int client_env_count
Definition: dhclient.c:98
isc_result_t dhcp_set_control_state(control_object_state_t oldstate, control_object_state_t newstate)
Definition: dhclient.c:5219
unsigned char options[FLEXIBLE_ARRAY_MEMBER]
Definition: dhcp6.h:252
Definition: dhcpd.h:1071
const unsigned char * data
Definition: tree.h:79
struct interface_info * interface
Definition: dhcpd.h:1267
#define DHO_DHCP_MESSAGE_TYPE
Definition: dhcp.h:145
void dhcp_common_objects_setup(void)
#define DHCPv6
Definition: config.h:24
u_int16_t ia_type
Definition: dhcpd.h:1149
isc_boolean_t released
Definition: dhcpd.h:1163
unsigned packet_length
Definition: dhcpd.h:408
void(* store_tag)(unsigned char *, u_int32_t)
Definition: tree.h:332
void write_lease_option(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:3883
#define DDNS_STATE_REM_FW_NXRR
Definition: dhcpd.h:1758
int(* dhcp_interface_shutdown_hook)(struct interface_info *)
Definition: discover.c:51
#define DHCPOFFER
Definition: dhcp.h:173
TIME starts
Definition: dhcpd.h:1151
void discover_interfaces(int state)
Definition: discover.c:559
isc_result_t new_parse(struct parse **cfile, int file, char *inbuf, unsigned buflen, const char *name, int eolp)
Definition: conflex.c:41
#define DUID_UUID
Definition: dhcp6.h:168
struct dhc6_lease * active_lease
Definition: dhcpd.h:1301
#define INTERFACE_REQUESTED
Definition: dhcpd.h:1390
#define DHO_HOST_NAME
Definition: dhcp.h:104
int universe_count
Definition: tables.c:964
int dhclient_interface_shutdown_hook(struct interface_info *interface)
Definition: dhclient.c:5020
TIME starts
Definition: dhcpd.h:1139
struct buffer * buffer
Definition: tree.h:78
void script_write_params(struct client_state *client, const char *prefix, struct client_lease *lease)
Adds parameters to environment variables for a script.
Definition: dhclient.c:4528
int option_dereference(struct option **dest, const char *file, int line)
Definition: tables.c:1002
#define DHO_VENDOR_ENCAPSULATED_OPTIONS
Definition: dhcp.h:135
void client_location_changed()
Definition: dhclient.c:4908
void state_stop(void *cpp)
Definition: dhclient.c:1959
int dad_wait_time
Definition: dhclient.c:109
isc_result_t omapi_init(void)
Definition: support.c:62
#define DHO_DHCP_REQUESTED_ADDRESS
Definition: dhcp.h:142
int buffer_dereference(struct buffer **ptr, const char *file, int line)
Definition: alloc.c:726
#define IGNORE_RET(x)
Definition: cdefs.h:55
int log_perror
Definition: errwarn.c:43
char * server_name
Definition: dhcpd.h:1116
isc_result_t ddns_modify_fwd(dhcp_ddns_cb_t *ddns_cb, const char *file, int line)
isc_result_t write_client6_lease(struct client_state *client, struct dhc6_lease *lease, int rewrite, int sync)
Definition: dhclient.c:4127
int bootp_broadcast_always
Definition: dhcpd.h:1259