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