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  /*
1776  * If it wasn't specified by the server, calculate it. Also use expiry
1777  * instead of renewal time when it is shorter. This better follows
1778  * RFC 2131 (section 4.4.5) when dealing with some DHCP servers.
1779  */
1780 
1781  if (!client -> new -> renewal ||
1782  client -> new -> renewal > client -> new -> expiry)
1783  client -> new -> renewal = client -> new -> expiry / 2 + 1;
1784 
1785  if (client -> new -> renewal <= 0)
1786  client -> new -> renewal = TIME_MAX;
1787 
1788  /* Now introduce some randomness to the renewal time: */
1789  if (client->new->renewal <= ((TIME_MAX / 3) - 3))
1790  client->new->renewal = (((client->new->renewal * 3) + 3) / 4) +
1791  (((random() % client->new->renewal) + 3) / 4);
1792 
1793  /* Same deal with the rebind time. */
1794  oc = lookup_option (&dhcp_universe, client -> new -> options,
1796  if (oc &&
1797  evaluate_option_cache (&ds, packet, (struct lease *)0, client,
1798  packet -> options, client -> new -> options,
1799  &global_scope, oc, MDL)) {
1800  if (ds.len > 3)
1801  client -> new -> rebind = getULong (ds.data);
1802  else
1803  client -> new -> rebind = 0;
1804  data_string_forget (&ds, MDL);
1805  } else
1806  client -> new -> rebind = 0;
1807 
1808  /* Rebinding time must not be longer than expiry. */
1809  if (client -> new -> rebind <= 0 ||
1810  client -> new -> rebind > client -> new -> expiry) {
1811  if (client -> new -> expiry <= TIME_MAX / 7)
1812  client -> new -> rebind =
1813  client -> new -> expiry * 7 / 8;
1814  else
1815  client -> new -> rebind =
1816  client -> new -> expiry / 8 * 7;
1817  }
1818 
1819  /* Make sure our randomness didn't run the renewal time past the
1820  rebind time. */
1821  if (client -> new -> renewal > client -> new -> rebind) {
1822  if (client -> new -> rebind <= TIME_MAX / 3)
1823  client -> new -> renewal =
1824  client -> new -> rebind * 3 / 4;
1825  else
1826  client -> new -> renewal =
1827  client -> new -> rebind / 4 * 3;
1828  }
1829 
1830  client -> new -> expiry += cur_time;
1831  /* Lease lengths can never be negative. */
1832  if (client -> new -> expiry < cur_time)
1833  client -> new -> expiry = TIME_MAX;
1834  client -> new -> renewal += cur_time;
1835  if (client -> new -> renewal < cur_time)
1836  client -> new -> renewal = TIME_MAX;
1837  client -> new -> rebind += cur_time;
1838  if (client -> new -> rebind < cur_time)
1839  client -> new -> rebind = TIME_MAX;
1840 
1841  bind_lease (client);
1842 }
1843 
1844 void bind_lease (client)
1845  struct client_state *client;
1846 {
1847  struct timeval tv;
1848 
1849  /* Remember the medium. */
1850  client->new->medium = client->medium;
1851 
1852  /* Run the client script with the new parameters. */
1853  script_init(client, (client->state == S_REQUESTING ? "BOUND" :
1854  (client->state == S_RENEWING ? "RENEW" :
1855  (client->state == S_REBOOTING ? "REBOOT" :
1856  "REBIND"))),
1857  client->new->medium);
1858  if (client->active && client->state != S_REBOOTING)
1859  script_write_params(client, "old_", client->active);
1860  script_write_params(client, "new_", client->new);
1861  script_write_requested(client);
1862  if (client->alias)
1863  script_write_params(client, "alias_", client->alias);
1864 
1865  /* If the BOUND/RENEW code detects another machine using the
1866  offered address, it exits nonzero. We need to send a
1867  DHCPDECLINE and toss the lease. */
1868  if (script_go(client)) {
1869  make_decline(client, client->new);
1870  send_decline(client);
1871  destroy_client_lease(client->new);
1872  client->new = NULL;
1873  if (onetry) {
1874  if (!quiet) {
1875  log_info("Unable to obtain a lease on first "
1876  "try (declined). Exiting.");
1877  }
1878 
1879 #if defined (CALL_SCRIPT_ON_ONETRY_FAIL)
1880  /* Let's call a script and we're done */
1881  script_init(client, "FAIL", (struct string_list *)0);
1882  script_go(client);
1883 #endif
1884  exit(2);
1885  } else {
1886  client -> state = S_DECLINED;
1887  state_init(client);
1888  return;
1889  }
1890  }
1891 
1892  /* Write out the new lease if it has been long enough. */
1893  if (!client->last_write ||
1894  (cur_time - client->last_write) >= MIN_LEASE_WRITE)
1895  write_client_lease(client, client->new, 0, 1);
1896 
1897  /* Replace the old active lease with the new one. */
1898  if (client->active)
1899  destroy_client_lease(client->active);
1900  client->active = client->new;
1901  client->new = NULL;
1902 
1903  /* Set up a timeout to start the renewal process. */
1904  tv.tv_sec = client->active->renewal;
1905  tv.tv_usec = ((client->active->renewal - cur_tv.tv_sec) > 1) ?
1906  random() % 1000000 : cur_tv.tv_usec;
1907  add_timeout(&tv, state_bound, client, 0, 0);
1908 
1909  log_info("bound to %s -- renewal in %ld seconds.",
1910  piaddr(client->active->address),
1911  (long)(client->active->renewal - cur_time));
1912  client->state = S_BOUND;
1914  go_daemon();
1915 #if defined (NSUPDATE)
1916  if (client->config->do_forward_update)
1917  dhclient_schedule_updates(client, &client->active->address, 1);
1918 #endif
1919 }
1920 
1921 /* state_bound is called when we've successfully bound to a particular
1922  lease, but the renewal time on that lease has expired. We are
1923  expected to unicast a DHCPREQUEST to the server that gave us our
1924  original lease. */
1925 
1926 void state_bound (cpp)
1927  void *cpp;
1928 {
1929  struct client_state *client = cpp;
1930  struct option_cache *oc;
1931  struct data_string ds;
1932 
1933  ASSERT_STATE(state, S_BOUND);
1934 
1935  /* T1 has expired. */
1936  make_request (client, client -> active);
1937  client -> xid = client -> packet.xid;
1938 
1939  memset (&ds, 0, sizeof ds);
1940  oc = lookup_option (&dhcp_universe, client -> active -> options,
1942  if (oc &&
1943  evaluate_option_cache (&ds, (struct packet *)0, (struct lease *)0,
1944  client, (struct option_state *)0,
1945  client -> active -> options,
1946  &global_scope, oc, MDL)) {
1947  if (ds.len > 3) {
1948  memcpy (client -> destination.iabuf, ds.data, 4);
1949  client -> destination.len = 4;
1950  } else
1951  client -> destination = iaddr_broadcast;
1952 
1953  data_string_forget (&ds, MDL);
1954  } else
1955  client -> destination = iaddr_broadcast;
1956 
1957  client -> first_sending = cur_time;
1958  client -> interval = client -> config -> initial_interval;
1959  client -> state = S_RENEWING;
1960 
1961  /* Send the first packet immediately. */
1962  send_request (client);
1963 }
1964 
1965 /* state_stop is called when we've been told to shut down. We unconfigure
1966  the interfaces, and then stop operating until told otherwise. */
1967 
1968 void state_stop (cpp)
1969  void *cpp;
1970 {
1971  struct client_state *client = cpp;
1972 
1973  client->pending = P_NONE;
1974 
1975  /* Cancel all timeouts. */
1977  cancel_timeout(send_discover, client);
1978  cancel_timeout(send_request, client);
1979  cancel_timeout(state_bound, client);
1980 
1981  /* If we have an address, unconfigure it. */
1982  if (client->active) {
1983  script_init(client, "STOP", client->active->medium);
1984  script_write_params(client, "old_", client->active);
1985  script_write_requested(client);
1986  if (client->alias)
1987  script_write_params(client, "alias_", client->alias);
1988  script_go(client);
1989  }
1990 }
1991 
1993 {
1994  return 0;
1995 }
1996 
1998  struct lease *lease;
1999 {
2000  return 0;
2001 }
2002 
2004  struct host_decl *host;
2005 {
2006  return 0;
2007 }
2008 
2009 void db_startup (testp)
2010  int testp;
2011 {
2012 }
2013 
2015  struct packet *packet;
2016 {
2017  struct iaddrmatchlist *ap;
2018  char addrbuf[4*16];
2019  char maskbuf[4*16];
2020 
2021  if (packet -> raw -> op != BOOTREPLY)
2022  return;
2023 
2024  /* If there's a reject list, make sure this packet's sender isn't
2025  on it. */
2026  for (ap = packet -> interface -> client -> config -> reject_list;
2027  ap; ap = ap -> next) {
2028  if (addr_match(&packet->client_addr, &ap->match)) {
2029 
2030  /* piaddr() returns its result in a static
2031  buffer sized 4*16 (see common/inet.c). */
2032 
2033  strcpy(addrbuf, piaddr(ap->match.addr));
2034  strcpy(maskbuf, piaddr(ap->match.mask));
2035 
2036  log_info("BOOTREPLY from %s rejected by rule %s "
2037  "mask %s.", piaddr(packet->client_addr),
2038  addrbuf, maskbuf);
2039  return;
2040  }
2041  }
2042 
2043  dhcpoffer (packet);
2044 
2045 }
2046 
2047 void dhcp (packet)
2048  struct packet *packet;
2049 {
2050  struct iaddrmatchlist *ap;
2051  void (*handler) (struct packet *);
2052  const char *type;
2053  char addrbuf[4*16];
2054  char maskbuf[4*16];
2055 
2056  switch (packet -> packet_type) {
2057  case DHCPOFFER:
2058  handler = dhcpoffer;
2059  type = "DHCPOFFER";
2060  break;
2061 
2062  case DHCPNAK:
2063  handler = dhcpnak;
2064  type = "DHCPNACK";
2065  break;
2066 
2067  case DHCPACK:
2068  handler = dhcpack;
2069  type = "DHCPACK";
2070  break;
2071 
2072  default:
2073  return;
2074  }
2075 
2076  /* If there's a reject list, make sure this packet's sender isn't
2077  on it. */
2078  for (ap = packet -> interface -> client -> config -> reject_list;
2079  ap; ap = ap -> next) {
2080  if (addr_match(&packet->client_addr, &ap->match)) {
2081 
2082  /* piaddr() returns its result in a static
2083  buffer sized 4*16 (see common/inet.c). */
2084 
2085  strcpy(addrbuf, piaddr(ap->match.addr));
2086  strcpy(maskbuf, piaddr(ap->match.mask));
2087 
2088  log_info("%s from %s rejected by rule %s mask %s.",
2089  type, piaddr(packet->client_addr),
2090  addrbuf, maskbuf);
2091  return;
2092  }
2093  }
2094  (*handler) (packet);
2095 }
2096 
2097 #ifdef DHCPv6
2098 void
2099 dhcpv6(struct packet *packet) {
2100  struct iaddrmatchlist *ap;
2101  struct client_state *client;
2102  char addrbuf[sizeof("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")];
2103 
2104  /* Silently drop bogus messages. */
2106  return;
2107 
2108  /* Discard, with log, packets from quenched sources. */
2109  for (ap = packet->interface->client->config->reject_list ;
2110  ap ; ap = ap->next) {
2111  if (addr_match(&packet->client_addr, &ap->match)) {
2112  strcpy(addrbuf, piaddr(packet->client_addr));
2113  log_info("%s from %s rejected by rule %s",
2115  addrbuf,
2116  piaddrmask(&ap->match.addr, &ap->match.mask));
2117  return;
2118  }
2119  }
2120 
2121  /* Screen out nonsensical messages. */
2122  switch(packet->dhcpv6_msg_type) {
2123 #ifdef DHCP4o6
2125  if (dhcpv4_over_dhcpv6) {
2126  log_info("RCV: %s message on %s from %s.",
2128  packet->interface->name,
2130  forw_dhcpv4_response(packet);
2131  }
2132  return;
2133 #endif
2134  case DHCPV6_ADVERTISE:
2135  case DHCPV6_RECONFIGURE:
2136  if (stateless)
2137  return;
2138  /* Falls through */
2139  case DHCPV6_REPLY:
2140  log_info("RCV: %s message on %s from %s.",
2143  break;
2144 
2145  default:
2146  return;
2147  }
2148 
2149  /* Find a client state that matches the incoming XID. */
2150  for (client = packet->interface->client ; client ;
2151  client = client->next) {
2152  if (memcmp(&client->dhcpv6_transaction_id,
2153  packet->dhcpv6_transaction_id, 3) == 0) {
2154  client->v6_handler(packet, client);
2155  return;
2156  }
2157  }
2158 
2159  /* XXX: temporary log for debugging */
2160  log_info("Packet received, but nothing done with it.");
2161 }
2162 
2163 #ifdef DHCP4o6
2164 /*
2165  * \brief Forward a DHCPv4-response to the DHCPv4 client.
2166  * (DHCPv6 client function)
2167  *
2168  * The DHCPv6 client receives a DHCPv4-response which is forwarded
2169  * to the DHCPv4 client.
2170  * Format: address:16 + DHCPv4 message content
2171  * (we have no state to keep the address so it is transported in
2172  * DHCPv6 <-> DHCPv6 inter-process messages)
2173  *
2174  * \param packet the DHCPv4-response packet
2175  */
2176 static void forw_dhcpv4_response(struct packet *packet)
2177 {
2178  struct option_cache *oc;
2179  struct data_string enc_opt_data;
2180  struct data_string ds;
2181  int cc;
2182 
2183  /*
2184  * Discard if relay is not ready.
2185  */
2186  if (dhcp4o6_state == -1) {
2187  log_info("forw_dhcpv4_response: not ready.");
2188  return;
2189  }
2190 
2191  if (packet->client_addr.len != 16) {
2192  log_error("forw_dhcpv4_response: bad address");
2193  return;
2194  }
2195 
2196  /*
2197  * Get our encapsulated DHCPv4 message.
2198  */
2200  if (oc == NULL) {
2201  log_info("DHCPv4-response from %s missing "
2202  "DHCPv4 Message option.",
2204  return;
2205  }
2206 
2207  memset(&enc_opt_data, 0, sizeof(enc_opt_data));
2208  if (!evaluate_option_cache(&enc_opt_data, NULL, NULL, NULL,
2209  NULL, NULL, &global_scope, oc, MDL)) {
2210  log_error("forw_dhcpv4_response: error evaluating "
2211  "DHCPv4 message.");
2212  data_string_forget(&enc_opt_data, MDL);
2213  return;
2214  }
2215 
2216  if (enc_opt_data.len < DHCP_FIXED_NON_UDP) {
2217  log_error("forw_dhcpv4_response: "
2218  "no memory for encapsulated packet.");
2219  data_string_forget(&enc_opt_data, MDL);
2220  return;
2221  }
2222 
2223  /*
2224  * Append address.
2225  */
2226  memset(&ds, 0, sizeof(ds));
2227  if (!buffer_allocate(&ds.buffer, enc_opt_data.len + 16, MDL)) {
2228  log_error("forw_dhcpv4_response: no memory buffer.");
2229  data_string_forget(&enc_opt_data, MDL);
2230  return;
2231  }
2232  ds.data = ds.buffer->data;
2233  ds.len = enc_opt_data.len + 16;
2234  memcpy(ds.buffer->data, enc_opt_data.data, enc_opt_data.len);
2235  memcpy(ds.buffer->data + enc_opt_data.len,
2236  packet->client_addr.iabuf, 16);
2237  data_string_forget(&enc_opt_data, MDL);
2238 
2239  /*
2240  * Forward them.
2241  */
2242  cc = send(dhcp4o6_fd, ds.data, ds.len, 0);
2243  if (cc < 0)
2244  log_error("forw_dhcpv4_response: send(): %m");
2245 
2246  data_string_forget(&ds, MDL);
2247 }
2248 
2249 /*
2250  * \brief Receive a DHCPv4-response from the DHCPv6 client.
2251  * (DHCPv4 client function)
2252  *
2253  * The DHCPv4 client receives a DHCPv4-response forwarded
2254  * by the DHCPv6 client (using \ref forw_dhcpv4_response())
2255  *
2256  * \param raw the DHCPv4-response raw packet
2257  */
2258 static void recv_dhcpv4_response(struct data_string *raw)
2259 {
2260  struct packet *packet;
2261  struct iaddr from;
2262 
2263  if (interfaces == NULL) {
2264  log_error("recv_dhcpv4_response: no interfaces.");
2265  return;
2266  }
2267 
2268  from.len = 16;
2269  memcpy(from.iabuf, raw->data + (raw->len - 16), 16);
2270 
2271  /*
2272  * Build a packet structure.
2273  */
2274  packet = NULL;
2275  if (!packet_allocate(&packet, MDL)) {
2276  log_error("recv_dhcpv4_response: no memory for packet.");
2277  return;
2278  }
2279 
2280  packet->raw = (struct dhcp_packet *) raw->data;
2281  packet->packet_length = raw->len - 16;
2283  packet->client_addr = from;
2284  interface_reference(&packet->interface, interfaces, MDL);
2285 
2286  /* Allocate packet->options now so it is non-null for all packets */
2288  log_error("recv_dhcpv4_response: no memory for options.");
2290  return;
2291  }
2292 
2293  /* If there's an option buffer, try to parse it. */
2294  if (packet->packet_length >= DHCP_FIXED_NON_UDP + 4) {
2295  struct option_cache *op;
2296  if (!parse_options(packet)) {
2297  if (packet->options)
2299  (&packet->options, MDL);
2301  return;
2302  }
2303 
2304  if (packet->options_valid &&
2306  packet->options,
2308  struct data_string dp;
2309  memset(&dp, 0, sizeof dp);
2310  evaluate_option_cache(&dp, packet, NULL, NULL,
2311  packet->options, NULL,
2312  NULL, op, MDL);
2313  if (dp.len > 0)
2314  packet->packet_type = dp.data[0];
2315  else
2316  packet->packet_type = 0;
2317  data_string_forget(&dp, MDL);
2318  }
2319  }
2320 
2321  if (validate_packet(packet) != 0) {
2322  if (packet->packet_type)
2323  dhcp(packet);
2324  else
2325  bootp(packet);
2326  }
2327 
2328  /* If the caller kept the packet, they'll have upped the refcnt. */
2330 }
2331 #endif /* DHCP4o6 */
2332 #endif /* DHCPv6 */
2333 
2335  struct packet *packet;
2336 {
2337  struct interface_info *ip = packet -> interface;
2338  struct client_state *client;
2339  struct client_lease *lease, *lp;
2340  struct option **req;
2341  int i;
2342  int stop_selecting;
2343  const char *name = packet -> packet_type ? "DHCPOFFER" : "BOOTREPLY";
2344  char obuf [1024];
2345  struct timeval tv;
2346 
2347 #ifdef DEBUG_PACKET
2348  dump_packet (packet);
2349 #endif
2350 
2351  /* Find a client state that matches the xid... */
2352  for (client = ip -> client; client; client = client -> next)
2353  if (client -> xid == packet -> raw -> xid)
2354  break;
2355 
2356  /* If we're not receptive to an offer right now, or if the offer
2357  has an unrecognizable transaction id, then just drop it. */
2358  if (!client ||
2359  client -> state != S_SELECTING ||
2360  (packet -> interface -> hw_address.hlen - 1 !=
2361  packet -> raw -> hlen) ||
2362  (memcmp (&packet -> interface -> hw_address.hbuf [1],
2363  packet -> raw -> chaddr, packet -> raw -> hlen))) {
2364 #if defined (DEBUG)
2365  log_debug ("%s in wrong transaction.", name);
2366 #endif
2367  return;
2368  }
2369 
2370  sprintf (obuf, "%s from %s", name, piaddr (packet -> client_addr));
2371 
2372 
2373  /* If this lease doesn't supply the minimum required DHCPv4 parameters,
2374  * ignore it.
2375  */
2376  req = client->config->required_options;
2377  if (req != NULL) {
2378  for (i = 0 ; req[i] != NULL ; i++) {
2379  if ((req[i]->universe == &dhcp_universe) &&
2381  req[i]->code)) {
2382  struct option *option = NULL;
2383  unsigned code = req[i]->code;
2384 
2385  option_code_hash_lookup(&option,
2387  &code, 0, MDL);
2388 
2389  if (option)
2390  log_info("%s: no %s option.", obuf,
2391  option->name);
2392  else
2393  log_info("%s: no unknown-%u option.",
2394  obuf, code);
2395 
2397 
2398  return;
2399  }
2400  }
2401  }
2402 
2403  /* If we've already seen this lease, don't record it again. */
2404  for (lease = client -> offered_leases; lease; lease = lease -> next) {
2405  if (lease -> address.len == sizeof packet -> raw -> yiaddr &&
2406  !memcmp (lease -> address.iabuf,
2407  &packet -> raw -> yiaddr, lease -> address.len)) {
2408  log_debug ("%s: already seen.", obuf);
2409  return;
2410  }
2411  }
2412 
2413  lease = packet_to_lease (packet, client);
2414  if (!lease) {
2415  log_info ("%s: packet_to_lease failed.", obuf);
2416  return;
2417  }
2418 
2419  /* If this lease was acquired through a BOOTREPLY, record that
2420  fact. */
2421  if (!packet -> options_valid || !packet -> packet_type)
2422  lease -> is_bootp = 1;
2423 
2424  /* Record the medium under which this lease was offered. */
2425  lease -> medium = client -> medium;
2426 
2427  /* Figure out when we're supposed to stop selecting. */
2428  stop_selecting = (client -> first_sending +
2429  client -> config -> select_interval);
2430 
2431  /* If this is the lease we asked for, put it at the head of the
2432  list, and don't mess with the arp request timeout. */
2433  if (lease -> address.len == client -> requested_address.len &&
2434  !memcmp (lease -> address.iabuf,
2435  client -> requested_address.iabuf,
2436  client -> requested_address.len)) {
2437  lease -> next = client -> offered_leases;
2438  client -> offered_leases = lease;
2439  } else {
2440  /* Put the lease at the end of the list. */
2441  lease -> next = (struct client_lease *)0;
2442  if (!client -> offered_leases)
2443  client -> offered_leases = lease;
2444  else {
2445  for (lp = client -> offered_leases; lp -> next;
2446  lp = lp -> next)
2447  ;
2448  lp -> next = lease;
2449  }
2450  }
2451 
2452  /* If the selecting interval has expired, go immediately to
2453  state_selecting(). Otherwise, time out into
2454  state_selecting at the select interval. */
2455  if (stop_selecting <= cur_tv.tv_sec)
2456  state_selecting (client);
2457  else {
2458  tv.tv_sec = stop_selecting;
2459  tv.tv_usec = cur_tv.tv_usec;
2460  add_timeout(&tv, state_selecting, client, 0, 0);
2461  cancel_timeout(send_discover, client);
2462  }
2463  log_info("%s", obuf);
2464 }
2465 
2466 /* Allocate a client_lease structure and initialize it from the parameters
2467  in the specified packet. */
2468 
2470  struct packet *packet;
2471  struct client_state *client;
2472 {
2473  struct client_lease *lease;
2474  unsigned i;
2475  struct option_cache *oc;
2476  struct option *option = NULL;
2477  struct data_string data;
2478 
2479  lease = (struct client_lease *)new_client_lease (MDL);
2480 
2481  if (!lease) {
2482  log_error("packet_to_lease: no memory to record lease.\n");
2483  return NULL;
2484  }
2485 
2486  memset(lease, 0, sizeof(*lease));
2487 
2488  /* Copy the lease options. */
2490 
2491  lease->address.len = sizeof(packet->raw->yiaddr);
2492  memcpy(lease->address.iabuf, &packet->raw->yiaddr,
2493  lease->address.len);
2494 
2495  lease->next_srv_addr.len = sizeof(packet->raw->siaddr);
2496  memcpy(lease->next_srv_addr.iabuf, &packet->raw->siaddr,
2497  lease->next_srv_addr.len);
2498 
2499  memset(&data, 0, sizeof(data));
2500 
2501  if (client -> config -> vendor_space_name) {
2503 
2504  /* See if there was a vendor encapsulation option. */
2505  oc = lookup_option (&dhcp_universe, lease -> options, i);
2506  if (oc &&
2507  client -> config -> vendor_space_name &&
2508  evaluate_option_cache (&data, packet,
2509  (struct lease *)0, client,
2510  packet -> options, lease -> options,
2511  &global_scope, oc, MDL)) {
2512  if (data.len) {
2513  if (!option_code_hash_lookup(&option,
2515  &i, 0, MDL))
2516  log_fatal("Unable to find VENDOR "
2517  "option (%s:%d).", MDL);
2519  (packet -> options, option,
2520  data.data, data.len, &dhcp_universe,
2521  client -> config -> vendor_space_name
2522  );
2523 
2525  }
2526  data_string_forget (&data, MDL);
2527  }
2528  } else
2529  i = 0;
2530 
2531  /* Figure out the overload flag. */
2534  if (oc &&
2535  evaluate_option_cache (&data, packet, (struct lease *)0, client,
2536  packet -> options, lease -> options,
2537  &global_scope, oc, MDL)) {
2538  if (data.len > 0)
2539  i = data.data [0];
2540  else
2541  i = 0;
2542  data_string_forget (&data, MDL);
2543  } else
2544  i = 0;
2545 
2546  /* If the server name was filled out, copy it. */
2547  if (!(i & 2) && packet -> raw -> sname [0]) {
2548  unsigned len;
2549  /* Don't count on the NUL terminator. */
2550  for (len = 0; len < DHCP_SNAME_LEN; len++)
2551  if (!packet -> raw -> sname [len])
2552  break;
2553  lease -> server_name = dmalloc (len + 1, MDL);
2554  if (!lease -> server_name) {
2555  log_error ("dhcpoffer: no memory for server name.\n");
2557  return (struct client_lease *)0;
2558  } else {
2559  memcpy (lease -> server_name,
2560  packet -> raw -> sname, len);
2561  lease -> server_name [len] = 0;
2562  }
2563  }
2564 
2565  /* Ditto for the filename. */
2566  if (!(i & 1) && packet -> raw -> file [0]) {
2567  unsigned len;
2568  /* Don't count on the NUL terminator. */
2569  for (len = 0; len < DHCP_FILE_LEN; len++)
2570  if (!packet -> raw -> file [len])
2571  break;
2572  lease -> filename = dmalloc (len + 1, MDL);
2573  if (!lease -> filename) {
2574  log_error ("dhcpoffer: no memory for filename.\n");
2576  return (struct client_lease *)0;
2577  } else {
2578  memcpy (lease -> filename,
2579  packet -> raw -> file, len);
2580  lease -> filename [len] = 0;
2581  }
2582  }
2583 
2584  execute_statements_in_scope(NULL, (struct packet *)packet, NULL,
2585  client, lease->options, lease->options,
2586  &global_scope, client->config->on_receipt,
2587  NULL, NULL);
2588 
2589  return lease;
2590 }
2591 
2593  struct packet *packet;
2594 {
2595  struct interface_info *ip = packet -> interface;
2596  struct client_state *client;
2597 
2598  /* Find a client state that matches the xid... */
2599  for (client = ip -> client; client; client = client -> next)
2600  if (client -> xid == packet -> raw -> xid)
2601  break;
2602 
2603  /* If we're not receptive to an offer right now, or if the offer
2604  has an unrecognizable transaction id, then just drop it. */
2605  if (!client ||
2606  (packet -> interface -> hw_address.hlen - 1 !=
2607  packet -> raw -> hlen) ||
2608  (memcmp (&packet -> interface -> hw_address.hbuf [1],
2609  packet -> raw -> chaddr, packet -> raw -> hlen))) {
2610 #if defined (DEBUG)
2611  log_debug ("DHCPNAK in wrong transaction.");
2612 #endif
2613  return;
2614  }
2615 
2616  if (client -> state != S_REBOOTING &&
2617  client -> state != S_REQUESTING &&
2618  client -> state != S_RENEWING &&
2619  client -> state != S_REBINDING) {
2620 #if defined (DEBUG)
2621  log_debug ("DHCPNAK in wrong state.");
2622 #endif
2623  return;
2624  }
2625 
2626  log_info ("DHCPNAK from %s (xid=0x%x)", piaddr (packet -> client_addr), ntohl(client -> xid));
2627 
2628  if (!client -> active) {
2629 #if defined (DEBUG)
2630  log_info ("DHCPNAK with no active lease.\n");
2631 #endif
2632  return;
2633  }
2634 
2635  /* If we get a DHCPNAK, we use the EXPIRE dhclient-script state
2636  * to indicate that we want all old bindings to be removed. (It
2637  * is possible that we may get a NAK while in the RENEW state,
2638  * so we might have bindings active at that time)
2639  */
2640  script_init(client, "EXPIRE", NULL);
2641  script_write_params(client, "old_", client->active);
2642  script_write_requested(client);
2643  if (client->alias)
2644  script_write_params(client, "alias_", client->alias);
2645  script_go(client);
2646 
2647  destroy_client_lease (client -> active);
2648  client -> active = (struct client_lease *)0;
2649 
2650  /* Stop sending DHCPREQUEST packets... */
2651  cancel_timeout (send_request, client);
2652 
2653  /* On some scripts, 'EXPIRE' causes the interface to be ifconfig'd
2654  * down (this expunges any routes and arp cache). This makes the
2655  * interface unusable by state_init(), which we call next. So, we
2656  * need to 'PREINIT' the interface to bring it back up.
2657  */
2658  script_init(client, "PREINIT", NULL);
2659  if (client->alias)
2660  script_write_params(client, "alias_", client->alias);
2661  script_go(client);
2662 
2663  client -> state = S_INIT;
2664  state_init (client);
2665 }
2666 
2667 /* Send out a DHCPDISCOVER packet, and set a timeout to send out another
2668  one after the right interval has expired. If we don't get an offer by
2669  the time we reach the panic interval, call the panic function. */
2670 
2671 void send_discover (cpp)
2672  void *cpp;
2673 {
2674  struct client_state *client = cpp;
2675 
2676  int result;
2677  int interval;
2678  int increase = 1;
2679  struct timeval tv;
2680 
2681  /* Figure out how long it's been since we started transmitting. */
2682  interval = cur_time - client -> first_sending;
2683 
2684  /* If we're past the panic timeout, call the script and tell it
2685  we haven't found anything for this interface yet. */
2686  if (interval > client -> config -> timeout) {
2687  state_panic (client);
2688  return;
2689  }
2690 
2691  /* If we're selecting media, try the whole list before doing
2692  the exponential backoff, but if we've already received an
2693  offer, stop looping, because we obviously have it right. */
2694  if (!client -> offered_leases &&
2695  client -> config -> media) {
2696  int fail = 0;
2697  again:
2698  if (client -> medium) {
2699  client -> medium = client -> medium -> next;
2700  increase = 0;
2701  }
2702  if (!client -> medium) {
2703  if (fail)
2704  log_fatal ("No valid media types for %s!",
2705  client -> interface -> name);
2706  client -> medium =
2707  client -> config -> media;
2708  increase = 1;
2709  }
2710 
2711  log_info ("Trying medium \"%s\" %d",
2712  client -> medium -> string, increase);
2713  script_init(client, "MEDIUM", client -> medium);
2714  if (script_go(client)) {
2715  fail = 1;
2716  goto again;
2717  }
2718  }
2719 
2720  /* If we're supposed to increase the interval, do so. If it's
2721  currently zero (i.e., we haven't sent any packets yet), set
2722  it to initial_interval; otherwise, add to it a random number
2723  between zero and two times itself. On average, this means
2724  that it will double with every transmission. */
2725  if (increase) {
2726  if (!client->interval)
2727  client->interval = client->config->initial_interval;
2728  else
2729  client->interval += random() % (2 * client->interval);
2730 
2731  /* Don't backoff past cutoff. */
2732  if (client->interval > client->config->backoff_cutoff)
2733  client->interval = (client->config->backoff_cutoff / 2)
2734  + (random() % client->config->backoff_cutoff);
2735  } else if (!client->interval)
2736  client->interval = client->config->initial_interval;
2737 
2738  /* If the backoff would take us to the panic timeout, just use that
2739  as the interval. */
2740  if (cur_time + client -> interval >
2741  client -> first_sending + client -> config -> timeout)
2742  client -> interval =
2743  (client -> first_sending +
2744  client -> config -> timeout) - cur_time + 1;
2745 
2746  /* Record the number of seconds since we started sending. */
2747  if (interval < 65536)
2748  client -> packet.secs = htons (interval);
2749  else
2750  client -> packet.secs = htons (65535);
2751  client -> secs = client -> packet.secs;
2752 
2753 #if defined(DHCPv6) && defined(DHCP4o6)
2754  if (dhcpv4_over_dhcpv6) {
2755  log_info ("DHCPDISCOVER interval %ld",
2756  (long)(client -> interval));
2757  } else
2758 #endif
2759  log_info ("DHCPDISCOVER on %s to %s port %d interval %ld (xid=0x%x)",
2760  client -> name ? client -> name : client -> interface -> name,
2761  inet_ntoa (sockaddr_broadcast.sin_addr),
2762  ntohs (sockaddr_broadcast.sin_port), (long)(client -> interval), ntohl(client -> xid));
2763 
2764  /* Send out a packet. */
2765 #if defined(DHCPv6) && defined(DHCP4o6)
2766  if (dhcpv4_over_dhcpv6) {
2767  result = send_dhcpv4_query(client, 1);
2768  } else
2769 #endif
2770  result = send_packet(client->interface, NULL, &client->packet,
2771  client->packet_length, inaddr_any,
2772  &sockaddr_broadcast, NULL);
2773  if (result < 0) {
2774 #if defined(DHCPv6) && defined(DHCP4o6)
2775  if (dhcpv4_over_dhcpv6) {
2776  log_error("%s:%d: Failed to send %d byte long packet.",
2777  MDL, client->packet_length);
2778  } else
2779 #endif
2780  log_error("%s:%d: Failed to send %d byte long packet over %s "
2781  "interface.", MDL, client->packet_length,
2782  client->interface->name);
2783  }
2784 
2785  /*
2786  * If we used 0 microseconds here, and there were other clients on the
2787  * same network with a synchronized local clock (ntp), and a similar
2788  * zero-microsecond-scheduler behavior, then we could be participating
2789  * in a sub-second DOS ttck.
2790  */
2791  tv.tv_sec = cur_tv.tv_sec + client->interval;
2792  tv.tv_usec = client->interval > 1 ? random() % 1000000 : cur_tv.tv_usec;
2793  add_timeout(&tv, send_discover, client, 0, 0);
2794 }
2795 
2796 /* state_panic gets called if we haven't received any offers in a preset
2797  amount of time. When this happens, we try to use existing leases that
2798  haven't yet expired, and failing that, we call the client script and
2799  hope it can do something. */
2800 
2801 void state_panic (cpp)
2802  void *cpp;
2803 {
2804  struct client_state *client = cpp;
2805  struct client_lease *loop;
2806  struct client_lease *lp;
2807  struct timeval tv;
2808 
2809  loop = lp = client -> active;
2810 
2811  log_info ("No DHCPOFFERS received.");
2812 
2813  /* We may not have an active lease, but we may have some
2814  predefined leases that we can try. */
2815  if (!client -> active && client -> leases)
2816  goto activate_next;
2817 
2818  /* Run through the list of leases and see if one can be used. */
2819  while (client -> active) {
2820  if (client -> active -> expiry > cur_time) {
2821  log_info ("Trying recorded lease %s",
2822  piaddr (client -> active -> address));
2823  /* Run the client script with the existing
2824  parameters. */
2825  script_init(client, "TIMEOUT",
2826  client -> active -> medium);
2827  script_write_params(client, "new_", client -> active);
2828  script_write_requested(client);
2829  if (client -> alias)
2830  script_write_params(client, "alias_",
2831  client -> alias);
2832 
2833  /* If the old lease is still good and doesn't
2834  yet need renewal, go into BOUND state and
2835  timeout at the renewal time. */
2836  if (!script_go(client)) {
2837  if (cur_time < client -> active -> renewal) {
2838  client -> state = S_BOUND;
2839  log_info ("bound: renewal in %ld %s.",
2840  (long)(client -> active -> renewal -
2841  cur_time), "seconds");
2842  tv.tv_sec = client->active->renewal;
2843  tv.tv_usec = ((client->active->renewal -
2844  cur_time) > 1) ?
2845  random() % 1000000 :
2846  cur_tv.tv_usec;
2847  add_timeout(&tv, state_bound, client, 0, 0);
2848  } else {
2849  client -> state = S_BOUND;
2850  log_info ("bound: immediate renewal.");
2851  state_bound (client);
2852  }
2854  go_daemon ();
2855  return;
2856  }
2857  }
2858 
2859  /* If there are no other leases, give up. */
2860  if (!client -> leases) {
2861  client -> leases = client -> active;
2862  client -> active = (struct client_lease *)0;
2863  break;
2864  }
2865 
2866  activate_next:
2867  /* Otherwise, put the active lease at the end of the
2868  lease list, and try another lease.. */
2869  for (lp = client -> leases; lp -> next; lp = lp -> next)
2870  ;
2871  lp -> next = client -> active;
2872  if (lp -> next) {
2873  lp -> next -> next = (struct client_lease *)0;
2874  }
2875  client -> active = client -> leases;
2876  client -> leases = client -> leases -> next;
2877 
2878  /* If we already tried this lease, we've exhausted the
2879  set of leases, so we might as well give up for
2880  now. */
2881  if (client -> active == loop)
2882  break;
2883  else if (!loop)
2884  loop = client -> active;
2885  }
2886 
2887  /* No leases were available, or what was available didn't work, so
2888  tell the shell script that we failed to allocate an address,
2889  and try again later. */
2890  if (onetry) {
2891  if (!quiet) {
2892  log_info ("Unable to obtain a lease on first try.%s",
2893  " Exiting.");
2894  }
2895 
2896 #if defined (CALL_SCRIPT_ON_ONETRY_FAIL)
2897  /* Let's call a script and we're done */
2898  script_init(client, "FAIL", (struct string_list *)0);
2899  script_go(client);
2900 #endif
2901  exit (2);
2902  }
2903 
2904  log_info ("No working leases in persistent database - sleeping.");
2905  script_init(client, "FAIL", (struct string_list *)0);
2906  if (client -> alias)
2907  script_write_params(client, "alias_", client -> alias);
2908  script_go(client);
2909  client -> state = S_INIT;
2910  tv.tv_sec = cur_tv.tv_sec + ((client->config->retry_interval + 1) / 2 +
2911  (random() % client->config->retry_interval));
2912  tv.tv_usec = ((tv.tv_sec - cur_tv.tv_sec) > 1) ?
2913  random() % 1000000 : cur_tv.tv_usec;
2914  add_timeout(&tv, state_init, client, 0, 0);
2915  go_daemon ();
2916 }
2917 
2918 void send_request (cpp)
2919  void *cpp;
2920 {
2921  struct client_state *client = cpp;
2922 
2923  int result;
2924  int interval;
2925  struct sockaddr_in destination;
2926  struct in_addr from;
2927  struct timeval tv;
2928 
2929  /* Figure out how long it's been since we started transmitting. */
2930  interval = cur_time - client -> first_sending;
2931 
2932  /* If we're in the INIT-REBOOT or REQUESTING state and we're
2933  past the reboot timeout, go to INIT and see if we can
2934  DISCOVER an address... */
2935  /* XXX In the INIT-REBOOT state, if we don't get an ACK, it
2936  means either that we're on a network with no DHCP server,
2937  or that our server is down. In the latter case, assuming
2938  that there is a backup DHCP server, DHCPDISCOVER will get
2939  us a new address, but we could also have successfully
2940  reused our old address. In the former case, we're hosed
2941  anyway. This is not a win-prone situation. */
2942  if ((client -> state == S_REBOOTING ||
2943  client -> state == S_REQUESTING) &&
2944  interval > client -> config -> reboot_timeout) {
2945  cancel:
2946  client -> state = S_INIT;
2947  cancel_timeout (send_request, client);
2948  state_init (client);
2949  return;
2950  }
2951 
2952  /* If we're in the reboot state, make sure the media is set up
2953  correctly. */
2954  if (client -> state == S_REBOOTING &&
2955  !client -> medium &&
2956  client -> active -> medium ) {
2957  script_init(client, "MEDIUM", client -> active -> medium);
2958 
2959  /* If the medium we chose won't fly, go to INIT state. */
2960  if (script_go(client))
2961  goto cancel;
2962 
2963  /* Record the medium. */
2964  client -> medium = client -> active -> medium;
2965  }
2966 
2967  /* If the lease has expired, relinquish the address and go back
2968  to the INIT state. */
2969  if (client -> state != S_REQUESTING &&
2970  cur_time > client -> active -> expiry) {
2971  /* Run the client script with the new parameters. */
2972  script_init(client, "EXPIRE", (struct string_list *)0);
2973  script_write_params(client, "old_", client -> active);
2974  script_write_requested(client);
2975  if (client -> alias)
2976  script_write_params(client, "alias_",
2977  client -> alias);
2978  script_go(client);
2979 
2980  /* Now do a preinit on the interface so that we can
2981  discover a new address. */
2982  script_init(client, "PREINIT", (struct string_list *)0);
2983  if (client -> alias)
2984  script_write_params(client, "alias_",
2985  client -> alias);
2986  script_go(client);
2987 
2988  client -> state = S_INIT;
2989  state_init (client);
2990  return;
2991  }
2992 
2993  /* Do the exponential backoff... */
2994  if (!client -> interval)
2995  client -> interval = client -> config -> initial_interval;
2996  else {
2997  client -> interval += ((random () >> 2) %
2998  (2 * client -> interval));
2999  }
3000 
3001  /* Don't backoff past cutoff. */
3002  if (client -> interval >
3003  client -> config -> backoff_cutoff)
3004  client -> interval =
3005  ((client -> config -> backoff_cutoff / 2)
3006  + ((random () >> 2) %
3007  client -> config -> backoff_cutoff));
3008 
3009  /* If the backoff would take us to the expiry time, just set the
3010  timeout to the expiry time. */
3011  if (client -> state != S_REQUESTING &&
3012  cur_time + client -> interval > client -> active -> expiry)
3013  client -> interval =
3014  client -> active -> expiry - cur_time + 1;
3015 
3016  /* If the lease T2 time has elapsed, or if we're not yet bound,
3017  broadcast the DHCPREQUEST rather than unicasting. */
3018  if (client -> state == S_REQUESTING ||
3019  client -> state == S_REBOOTING ||
3020  cur_time > client -> active -> rebind)
3021  destination.sin_addr = sockaddr_broadcast.sin_addr;
3022  else
3023  memcpy (&destination.sin_addr.s_addr,
3024  client -> destination.iabuf,
3025  sizeof destination.sin_addr.s_addr);
3026  destination.sin_port = remote_port;
3027  destination.sin_family = AF_INET;
3028 #ifdef HAVE_SA_LEN
3029  destination.sin_len = sizeof destination;
3030 #endif
3031 
3032  if (client -> state == S_RENEWING ||
3033  client -> state == S_REBINDING)
3034  memcpy (&from, client -> active -> address.iabuf,
3035  sizeof from);
3036  else
3037  from.s_addr = INADDR_ANY;
3038 
3039  /* Record the number of seconds since we started sending. */
3040  if (client -> state == S_REQUESTING)
3041  client -> packet.secs = client -> secs;
3042  else {
3043  if (interval < 65536)
3044  client -> packet.secs = htons (interval);
3045  else
3046  client -> packet.secs = htons (65535);
3047  }
3048 
3049 #if defined(DHCPv6) && defined(DHCP4o6)
3050  if (dhcpv4_over_dhcpv6) {
3051  log_info ("DHCPREQUEST");
3052  } else
3053 #endif
3054  log_info ("DHCPREQUEST on %s to %s port %d (xid=0x%x)",
3055  client -> name ? client -> name : client -> interface -> name,
3056  inet_ntoa (destination.sin_addr),
3057  ntohs (destination.sin_port), ntohl(client -> xid));
3058 
3059 #if defined(DHCPv6) && defined(DHCP4o6)
3060  if (dhcpv4_over_dhcpv6) {
3061  int broadcast = 0;
3062  if (destination.sin_addr.s_addr == INADDR_BROADCAST)
3063  broadcast = 1;
3064  result = send_dhcpv4_query(client, broadcast);
3065  if (result < 0) {
3066  log_error("%s:%d: Failed to send %d byte long packet.",
3067  MDL, client->packet_length);
3068  }
3069  } else
3070 #endif
3071  if (destination.sin_addr.s_addr != INADDR_BROADCAST &&
3073 #if defined(SO_BINDTODEVICE)
3074  if (setsockopt(fallback_interface -> wfdesc, SOL_SOCKET,
3075  SO_BINDTODEVICE, client->interface->name,
3076  strlen(client->interface->name)) < 0) {
3077  log_error("%s:%d: Failed to bind fallback interface"
3078  " to %s: %m", MDL, client->interface->name);
3079  }
3080 #endif
3081  result = send_packet(fallback_interface, NULL, &client->packet,
3082  client->packet_length, from, &destination,
3083  NULL);
3084  if (result < 0) {
3085  log_error("%s:%d: Failed to send %d byte long packet "
3086  "over %s interface.", MDL,
3087  client->packet_length,
3089  }
3090 #if defined(SO_BINDTODEVICE)
3091  if (setsockopt(fallback_interface -> wfdesc, SOL_SOCKET,
3092  SO_BINDTODEVICE, NULL, 0) < 0) {
3093  log_fatal("%s:%d: Failed to unbind fallback interface:"
3094  " %m", MDL);
3095  }
3096 #endif
3097  }
3098  else {
3099  /* Send out a packet. */
3100  result = send_packet(client->interface, NULL, &client->packet,
3101  client->packet_length, from, &destination,
3102  NULL);
3103  if (result < 0) {
3104  log_error("%s:%d: Failed to send %d byte long packet"
3105  " over %s interface.", MDL,
3106  client->packet_length,
3107  client->interface->name);
3108  }
3109  }
3110 
3111  tv.tv_sec = cur_tv.tv_sec + client->interval;
3112  tv.tv_usec = ((tv.tv_sec - cur_tv.tv_sec) > 1) ?
3113  random() % 1000000 : cur_tv.tv_usec;
3114  add_timeout(&tv, send_request, client, 0, 0);
3115 }
3116 
3117 void send_decline (cpp)
3118  void *cpp;
3119 {
3120  struct client_state *client = cpp;
3121 
3122  int result;
3123 
3124 #if defined(DHCPv6) && defined(DHCP4o6)
3125  if (dhcpv4_over_dhcpv6) {
3126  log_info ("DHCPDECLINE");
3127  } else
3128 #endif
3129  log_info ("DHCPDECLINE on %s to %s port %d (xid=0x%x)",
3130  client->name ? client->name : client->interface->name,
3131  inet_ntoa(sockaddr_broadcast.sin_addr),
3132  ntohs(sockaddr_broadcast.sin_port), ntohl(client -> xid));
3133 
3134  /* Send out a packet. */
3135 #if defined(DHCPv6) && defined(DHCP4o6)
3136  if (dhcpv4_over_dhcpv6) {
3137  result = send_dhcpv4_query(client, 1);
3138  } else
3139 #endif
3140  result = send_packet(client->interface, NULL, &client->packet,
3141  client->packet_length, inaddr_any,
3142  &sockaddr_broadcast, NULL);
3143  if (result < 0) {
3144 #if defined(DHCPv6) && defined(DHCP4o6)
3145  if (dhcpv4_over_dhcpv6) {
3146  log_error("%s:%d: Failed to send %d byte long packet.",
3147  MDL, client->packet_length);
3148  } else
3149 #endif
3150  log_error("%s:%d: Failed to send %d byte long packet over %s"
3151  " interface.", MDL, client->packet_length,
3152  client->interface->name);
3153  }
3154 }
3155 
3156 void send_release (cpp)
3157  void *cpp;
3158 {
3159  struct client_state *client = cpp;
3160 
3161  int result;
3162  struct sockaddr_in destination;
3163  struct in_addr from;
3164 
3165  memcpy (&from, client -> active -> address.iabuf,
3166  sizeof from);
3167  memcpy (&destination.sin_addr.s_addr,
3168  client -> destination.iabuf,
3169  sizeof destination.sin_addr.s_addr);
3170  destination.sin_port = remote_port;
3171  destination.sin_family = AF_INET;
3172 #ifdef HAVE_SA_LEN
3173  destination.sin_len = sizeof destination;
3174 #endif
3175 
3176  /* Set the lease to end now, so that we don't accidentally
3177  reuse it if we restart before the old expiry time. */
3178  client -> active -> expiry =
3179  client -> active -> renewal =
3180  client -> active -> rebind = cur_time;
3181  if (!write_client_lease (client, client -> active, 1, 1)) {
3182  log_error ("Can't release lease: lease write failed.");
3183  return;
3184  }
3185 
3186 #if defined(DHCPv6) && defined(DHCP4o6)
3187  if (dhcpv4_over_dhcpv6) {
3188  log_info ("DHCPRELEASE");
3189  } else
3190 #endif
3191  log_info ("DHCPRELEASE on %s to %s port %d (xid=0x%x)",
3192  client -> name ? client -> name : client -> interface -> name,
3193  inet_ntoa (destination.sin_addr),
3194  ntohs (destination.sin_port), ntohl(client -> xid));
3195 
3196 #if defined(DHCPv6) && defined(DHCP4o6)
3197  if (dhcpv4_over_dhcpv6) {
3198  int broadcast = 0;
3199  if (destination.sin_addr.s_addr == INADDR_BROADCAST)
3200  broadcast = 1;
3201  result = send_dhcpv4_query(client, broadcast);
3202  if (result < 0) {
3203  log_error("%s:%d: Failed to send %d byte long packet.",
3204  MDL, client->packet_length);
3205  }
3206  } else
3207 #endif
3208  if (fallback_interface) {
3209 #if defined(SO_BINDTODEVICE)
3210  if (setsockopt(fallback_interface -> wfdesc, SOL_SOCKET,
3211  SO_BINDTODEVICE, client->interface->name,
3212  strlen(client->interface->name)) < 0) {
3213  log_error("%s:%d: Failed to bind fallback interface"
3214  " to %s: %m", MDL, client->interface->name);
3215  }
3216 #endif
3217  result = send_packet(fallback_interface, NULL, &client->packet,
3218  client->packet_length, from, &destination,
3219  NULL);
3220  if (result < 0) {
3221  log_error("%s:%d: Failed to send %d byte long packet"
3222  " over %s interface.", MDL,
3223  client->packet_length,
3225  }
3226 #if defined(SO_BINDTODEVICE)
3227  if (setsockopt(fallback_interface -> wfdesc, SOL_SOCKET,
3228  SO_BINDTODEVICE, NULL, 0) < 0) {
3229  log_fatal("%s:%d: Failed to unbind fallback interface:"
3230  " %m", MDL);
3231  }
3232 #endif
3233  } else {
3234  /* Send out a packet. */
3235  result = send_packet(client->interface, NULL, &client->packet,
3236  client->packet_length, from, &destination,
3237  NULL);
3238  if (result < 0) {
3239  log_error ("%s:%d: Failed to send %d byte long packet"
3240  " over %s interface.", MDL,
3241  client->packet_length,
3242  client->interface->name);
3243  }
3244 
3245  }
3246 }
3247 
3248 #if defined(DHCPv6) && defined(DHCP4o6)
3249 /*
3250  * \brief Send a DHCPv4-query to the DHCPv6 client
3251  * (DHCPv4 client function)
3252  *
3253  * The DHCPv4 client sends a DHCPv4-query to the DHCPv6 client over
3254  * the inter-process communication socket.
3255  *
3256  * \param client the DHCPv4 client state
3257  * \param broadcast the broadcast flag
3258  * \return the sent byte count (-1 on error)
3259  */
3260 static int send_dhcpv4_query(struct client_state *client, int broadcast) {
3261  struct data_string ds;
3262  struct dhcpv4_over_dhcpv6_packet *query;
3263  int ofs, len, cc;
3264 
3265  if (dhcp4o6_state <= 0) {
3266  log_info("send_dhcpv4_query: not ready.");
3267  return -1;
3268  }
3269 
3270  /*
3271  * Compute buffer length and allocate it.
3272  */
3273  len = ofs = (int)(offsetof(struct dhcpv4_over_dhcpv6_packet, options));
3275  len += client->packet_length;
3276  memset(&ds, 0, sizeof(ds));
3277  if (!buffer_allocate(&ds.buffer, len, MDL)) {
3278  log_error("Unable to allocate memory for DHCPv4-query.");
3279  return -1;
3280  }
3281  ds.data = ds.buffer->data;
3282  ds.len = len;
3283 
3284  /*
3285  * Fill header.
3286  */
3287  query = (struct dhcpv4_over_dhcpv6_packet *)ds.data;
3288  query->msg_type = DHCPV6_DHCPV4_QUERY;
3289  query->flags[0] = query->flags[1] = query->flags[2] = 0;
3290  if (!broadcast)
3291  query->flags[0] |= DHCP4O6_QUERY_UNICAST;
3292 
3293  /*
3294  * Append DHCPv4 message.
3295  */
3296  dhcpv6_universe.store_tag(ds.buffer->data + ofs, D6O_DHCPV4_MSG);
3297  ofs += dhcpv6_universe.tag_size;
3298  dhcpv6_universe.store_length(ds.buffer->data + ofs,
3299  client->packet_length);
3301  memcpy(ds.buffer->data + ofs, &client->packet, client->packet_length);
3302 
3303  /*
3304  * Send DHCPv6 message.
3305  */
3306  cc = send(dhcp4o6_fd, ds.data, ds.len, 0);
3307  if (cc < 0)
3308  log_error("send_dhcpv4_query: send(): %m");
3309 
3310  data_string_forget(&ds, MDL);
3311 
3312  return cc;
3313 }
3314 
3315 /*
3316  * \brief Forward a DHCPv4-query to all DHCPv4 over DHCPv6 server addresses.
3317  * (DHCPv6 client function)
3318  *
3319  * \param raw the DHCPv6 DHCPv4-query message raw content
3320  */
3321 static void forw_dhcpv4_query(struct data_string *raw) {
3322  struct interface_info *ip;
3323  struct client_state *client;
3324  struct dhc6_lease *lease;
3325  struct option_cache *oc;
3326  struct data_string addrs;
3327  struct sockaddr_in6 sin6;
3328  int i, send_ret, attempt, success;
3329 
3330  attempt = success = 0;
3331  memset(&sin6, 0, sizeof(sin6));
3332  sin6.sin6_family = AF_INET6;
3333  sin6.sin6_port = remote_port;
3334 #ifdef HAVE_SA_LEN
3335  sin6.sin6_len = sizeof(sin6);
3336 #endif
3337  memset(&addrs, 0, sizeof(addrs));
3338  for (ip = interfaces; ip != NULL; ip = ip->next) {
3339  for (client = ip->client; client != NULL;
3340  client = client->next) {
3341  if ((client->state != S_BOUND) &&
3342  (client->state != S_RENEWING) &&
3343  (client->state != S_REBINDING))
3344  continue;
3345  lease = client->active_lease;
3346  if ((lease == NULL) || lease->released)
3347  continue;
3349  lease->options,
3351  if ((oc == NULL) ||
3352  !evaluate_option_cache(&addrs, NULL, NULL, NULL,
3353  lease->options, NULL,
3354  &global_scope, oc, MDL) ||
3355  ((addrs.len % sizeof(sin6.sin6_addr)) != 0)) {
3356  data_string_forget(&addrs, MDL);
3357  continue;
3358  }
3359  if (addrs.len == 0) {
3360  /* note there is nothing to forget */
3361  inet_pton(AF_INET6,
3363  &sin6.sin6_addr);
3364  attempt++;
3365  send_ret = send_packet6(ip, raw->data,
3366  raw->len, &sin6);
3367  if (send_ret == raw->len)
3368  success++;
3369  continue;
3370  }
3371  for (i = 0; i < addrs.len;
3372  i += sizeof(sin6.sin6_addr)) {
3373  memcpy(&sin6.sin6_addr, addrs.data + i,
3374  sizeof(sin6.sin6_addr));
3375  attempt++;
3376  send_ret = send_packet6(ip, raw->data,
3377  raw->len, &sin6);
3378  if (send_ret == raw->len)
3379  success++;
3380  }
3381  data_string_forget(&addrs, MDL);
3382  }
3383  }
3384 
3385  log_info("forw_dhcpv4_query: sent(%d): %d/%d",
3386  raw->len, success, attempt);
3387 
3388  if (attempt == 0)
3389  dhcp4o6_stop();
3390 }
3391 #endif
3392 
3393 void
3395  u_int8_t *type, struct option_cache *sid,
3396  struct iaddr *rip, struct option **prl,
3397  struct option_state **op)
3398 {
3399  unsigned i;
3400  struct option_cache *oc;
3401  struct option *option = NULL;
3402  struct buffer *bp = NULL;
3403 
3404  /* If there are any leftover options, get rid of them. */
3405  if (*op)
3407 
3408  /* Allocate space for options. */
3410 
3411  /* Send the server identifier if provided. */
3412  if (sid)
3413  save_option(&dhcp_universe, *op, sid);
3414 
3415  oc = NULL;
3416 
3417  /* Send the requested address if provided. */
3418  if (rip) {
3419  client->requested_address = *rip;
3421  if (!(option_code_hash_lookup(&option, dhcp_universe.code_hash,
3422  &i, 0, MDL) &&
3423  make_const_option_cache(&oc, NULL, rip->iabuf, rip->len,
3424  option, MDL)))
3425  log_error ("can't make requested address cache.");
3426  else {
3427  save_option(&dhcp_universe, *op, oc);
3429  }
3431  } else {
3432  client->requested_address.len = 0;
3433  }
3434 
3436  if (!(option_code_hash_lookup(&option, dhcp_universe.code_hash, &i, 0,
3437  MDL) &&
3438  make_const_option_cache(&oc, NULL, type, 1, option, MDL)))
3439  log_error("can't make message type.");
3440  else {
3441  save_option(&dhcp_universe, *op, oc);
3443  }
3445 
3446  if (prl) {
3447  int len;
3448 
3449  /* Probe the length of the list. */
3450  len = 0;
3451  for (i = 0 ; prl[i] != NULL ; i++)
3452  if (prl[i]->universe == &dhcp_universe)
3453  len++;
3454 
3455  if (!buffer_allocate(&bp, len, MDL))
3456  log_error("can't make parameter list buffer.");
3457  else {
3458  unsigned code = DHO_DHCP_PARAMETER_REQUEST_LIST;
3459 
3460  len = 0;
3461  for (i = 0 ; prl[i] != NULL ; i++)
3462  if (prl[i]->universe == &dhcp_universe)
3463  bp->data[len++] = prl[i]->code;
3464 
3465  if (!(option_code_hash_lookup(&option,
3467  &code, 0, MDL) &&
3468  make_const_option_cache(&oc, &bp, NULL, len,
3469  option, MDL))) {
3470  if (bp != NULL)
3471  buffer_dereference(&bp, MDL);
3472  log_error ("can't make option cache");
3473  } else {
3474  save_option(&dhcp_universe, *op, oc);
3476  }
3478  }
3479  }
3480 
3481  /*
3482  * If requested (duid_v4 == 1) add an RFC4361 compliant client-identifier
3483  * This can be overridden by including a client id in the configuration
3484  * file.
3485  */
3486  if (duid_v4 == 1) {
3487  struct data_string client_identifier;
3488  int hw_idx, hw_len;
3489 
3490  memset(&client_identifier, 0, sizeof(client_identifier));
3491  client_identifier.len = 1 + 4 + default_duid.len;
3492  if (!buffer_allocate(&client_identifier.buffer,
3493  client_identifier.len, MDL))
3494  log_fatal("no memory for default DUID!");
3495  client_identifier.data = client_identifier.buffer->data;
3496 
3498 
3499  /* Client-identifier type : 1 byte */
3500  *client_identifier.buffer->data = 255;
3501 
3502  /* IAID : 4 bytes
3503  * we use the low 4 bytes from the interface address
3504  */
3505  if (client->interface->hw_address.hlen > 4) {
3506  hw_idx = client->interface->hw_address.hlen - 4;
3507  hw_len = 4;
3508  } else {
3509  hw_idx = 0;
3510  hw_len = client->interface->hw_address.hlen;
3511  }
3512  memcpy(&client_identifier.buffer->data + 5 - hw_len,
3513  client->interface->hw_address.hbuf + hw_idx,
3514  hw_len);
3515 
3516  /* Add the default duid */
3517  memcpy(&client_identifier.buffer->data+(1+4),
3519 
3520  /* And save the option */
3521  if (!(option_code_hash_lookup(&option, dhcp_universe.code_hash,
3522  &i, 0, MDL) &&
3523  make_const_option_cache(&oc, NULL,
3524  (u_int8_t *)client_identifier.data,
3525  client_identifier.len,
3526  option, MDL)))
3527  log_error ("can't make requested client id cache..");
3528  else {
3529  save_option (&dhcp_universe, *op, oc);
3531  }
3533  }
3534 
3535  /* Run statements that need to be run on transmission. */
3536  if (client->config->on_transmission)
3537  execute_statements_in_scope(NULL, NULL, NULL, client,
3538  (lease ? lease->options : NULL),
3539  *op, &global_scope,
3540  client->config->on_transmission,
3541  NULL, NULL);
3542 }
3543 
3544 void make_discover (client, lease)
3545  struct client_state *client;
3546  struct client_lease *lease;
3547 {
3548  unsigned char discover = DHCPDISCOVER;
3549  struct option_state *options = (struct option_state *)0;
3550 
3551  memset (&client -> packet, 0, sizeof (client -> packet));
3552 
3553  make_client_options (client,
3554  lease, &discover, (struct option_cache *)0,
3555  lease ? &lease -> address : (struct iaddr *)0,
3556  client -> config -> requested_options,
3557  &options);
3558 
3559  /* Set up the option buffer... */
3560  client -> packet_length =
3561  cons_options ((struct packet *)0, &client -> packet,
3562  (struct lease *)0, client,
3563  /* maximum packet size */1500,
3564  (struct option_state *)0,
3565  options,
3566  /* scope */ &global_scope,
3567  /* overload */ 0,
3568  /* terminate */0,
3569  /* bootpp */0,
3570  (struct data_string *)0,
3571  client -> config -> vendor_space_name);
3572 
3573  option_state_dereference (&options, MDL);
3574  if (client -> packet_length < BOOTP_MIN_LEN)
3575  client -> packet_length = BOOTP_MIN_LEN;
3576 
3577  client -> packet.op = BOOTREQUEST;
3578  client -> packet.htype = client -> interface -> hw_address.hbuf [0];
3579  /* Assumes hw_address is known, otherwise a random value may result */
3580  client -> packet.hlen = client -> interface -> hw_address.hlen - 1;
3581  client -> packet.hops = 0;
3582  client -> packet.xid = random ();
3583  client -> packet.secs = 0; /* filled in by send_discover. */
3584 
3587  client -> packet.flags = 0;
3588  else
3589  client -> packet.flags = htons (BOOTP_BROADCAST);
3590 
3591  memset (&(client -> packet.ciaddr),
3592  0, sizeof client -> packet.ciaddr);
3593  memset (&(client -> packet.yiaddr),
3594  0, sizeof client -> packet.yiaddr);
3595  memset (&(client -> packet.siaddr),
3596  0, sizeof client -> packet.siaddr);
3597  client -> packet.giaddr = giaddr;
3598  if (client -> interface -> hw_address.hlen > 0)
3599  memcpy (client -> packet.chaddr,
3600  &client -> interface -> hw_address.hbuf [1],
3601  (unsigned)(client -> interface -> hw_address.hlen - 1));
3602 
3603 #ifdef DEBUG_PACKET
3604  dump_raw ((unsigned char *)&client -> packet, client -> packet_length);
3605 #endif
3606 }
3607 
3608 
3609 void make_request (client, lease)
3610  struct client_state *client;
3611  struct client_lease *lease;
3612 {
3613  unsigned char request = DHCPREQUEST;
3614  struct option_cache *oc;
3615 
3616  memset (&client -> packet, 0, sizeof (client -> packet));
3617 
3618  if (client -> state == S_REQUESTING)
3619  oc = lookup_option (&dhcp_universe, lease -> options,
3621  else
3622  oc = (struct option_cache *)0;
3623 
3624  if (client -> sent_options)
3625  option_state_dereference (&client -> sent_options, MDL);
3626 
3627  make_client_options (client, lease, &request, oc,
3628  ((client -> state == S_REQUESTING ||
3629  client -> state == S_REBOOTING)
3630  ? &lease -> address
3631  : (struct iaddr *)0),
3632  client -> config -> requested_options,
3633  &client -> sent_options);
3634 
3635  /* Set up the option buffer... */
3636  client -> packet_length =
3637  cons_options ((struct packet *)0, &client -> packet,
3638  (struct lease *)0, client,
3639  /* maximum packet size */1500,
3640  (struct option_state *)0,
3641  client -> sent_options,
3642  /* scope */ &global_scope,
3643  /* overload */ 0,
3644  /* terminate */0,
3645  /* bootpp */0,
3646  (struct data_string *)0,
3647  client -> config -> vendor_space_name);
3648 
3649  if (client -> packet_length < BOOTP_MIN_LEN)
3650  client -> packet_length = BOOTP_MIN_LEN;
3651 
3652  client -> packet.op = BOOTREQUEST;
3653  client -> packet.htype = client -> interface -> hw_address.hbuf [0];
3654  /* Assumes hw_address is known, otherwise a random value may result */
3655  client -> packet.hlen = client -> interface -> hw_address.hlen - 1;
3656  client -> packet.hops = 0;
3657  client -> packet.xid = client -> xid;
3658  client -> packet.secs = 0; /* Filled in by send_request. */
3659 
3660  /* If we own the address we're requesting, put it in ciaddr;
3661  otherwise set ciaddr to zero. */
3662  if (client -> state == S_BOUND ||
3663  client -> state == S_RENEWING ||
3664  client -> state == S_REBINDING) {
3665  memcpy (&client -> packet.ciaddr,
3666  lease -> address.iabuf, lease -> address.len);
3667  client -> packet.flags = 0;
3668  } else {
3669  memset (&client -> packet.ciaddr, 0,
3670  sizeof client -> packet.ciaddr);
3671  if ((!(bootp_broadcast_always ||
3672  client ->config->bootp_broadcast_always)) &&
3673  can_receive_unicast_unconfigured (client -> interface))
3674  client -> packet.flags = 0;
3675  else
3676  client -> packet.flags = htons (BOOTP_BROADCAST);
3677  }
3678 
3679  memset (&client -> packet.yiaddr, 0,
3680  sizeof client -> packet.yiaddr);
3681  memset (&client -> packet.siaddr, 0,
3682  sizeof client -> packet.siaddr);
3683  if (client -> state != S_BOUND &&
3684  client -> state != S_RENEWING)
3685  client -> packet.giaddr = giaddr;
3686  else
3687  memset (&client -> packet.giaddr, 0,
3688  sizeof client -> packet.giaddr);
3689  if (client -> interface -> hw_address.hlen > 0)
3690  memcpy (client -> packet.chaddr,
3691  &client -> interface -> hw_address.hbuf [1],
3692  (unsigned)(client -> interface -> hw_address.hlen - 1));
3693 
3694 #ifdef DEBUG_PACKET
3695  dump_raw ((unsigned char *)&client -> packet, client -> packet_length);
3696 #endif
3697 }
3698 
3699 void make_decline (client, lease)
3700  struct client_state *client;
3701  struct client_lease *lease;
3702 {
3703  unsigned char decline = DHCPDECLINE;
3704  struct option_cache *oc;
3705 
3706  struct option_state *options = (struct option_state *)0;
3707 
3708  /* Create the options cache. */
3709  oc = lookup_option (&dhcp_universe, lease -> options,
3711  make_client_options(client, lease, &decline, oc, &lease->address,
3712  NULL, &options);
3713 
3714  /* Consume the options cache into the option buffer. */
3715  memset (&client -> packet, 0, sizeof (client -> packet));
3716  client -> packet_length =
3717  cons_options ((struct packet *)0, &client -> packet,
3718  (struct lease *)0, client, 0,
3719  (struct option_state *)0, options,
3720  &global_scope, 0, 0, 0, (struct data_string *)0,
3721  client -> config -> vendor_space_name);
3722 
3723  /* Destroy the options cache. */
3724  option_state_dereference (&options, MDL);
3725 
3726  if (client -> packet_length < BOOTP_MIN_LEN)
3727  client -> packet_length = BOOTP_MIN_LEN;
3728 
3729  client -> packet.op = BOOTREQUEST;
3730  client -> packet.htype = client -> interface -> hw_address.hbuf [0];
3731  /* Assumes hw_address is known, otherwise a random value may result */
3732  client -> packet.hlen = client -> interface -> hw_address.hlen - 1;
3733  client -> packet.hops = 0;
3734  client -> packet.xid = client -> xid;
3735  client -> packet.secs = 0; /* Filled in by send_request. */
3738  client -> packet.flags = 0;
3739  else
3740  client -> packet.flags = htons (BOOTP_BROADCAST);
3741 
3742  /* ciaddr must always be zero. */
3743  memset (&client -> packet.ciaddr, 0,
3744  sizeof client -> packet.ciaddr);
3745  memset (&client -> packet.yiaddr, 0,
3746  sizeof client -> packet.yiaddr);
3747  memset (&client -> packet.siaddr, 0,
3748  sizeof client -> packet.siaddr);
3749  client -> packet.giaddr = giaddr;
3750  memcpy (client -> packet.chaddr,
3751  &client -> interface -> hw_address.hbuf [1],
3752  client -> interface -> hw_address.hlen);
3753 
3754 #ifdef DEBUG_PACKET
3755  dump_raw ((unsigned char *)&client -> packet, client -> packet_length);
3756 #endif
3757 }
3758 
3759 void make_release (client, lease)
3760  struct client_state *client;
3761  struct client_lease *lease;
3762 {
3763  unsigned char request = DHCPRELEASE;
3764  struct option_cache *oc;
3765 
3766  struct option_state *options = (struct option_state *)0;
3767 
3768  memset (&client -> packet, 0, sizeof (client -> packet));
3769 
3770  oc = lookup_option (&dhcp_universe, lease -> options,
3772  make_client_options(client, lease, &request, oc, NULL, NULL, &options);
3773 
3774  /* Set up the option buffer... */
3775  client -> packet_length =
3776  cons_options ((struct packet *)0, &client -> packet,
3777  (struct lease *)0, client,
3778  /* maximum packet size */1500,
3779  (struct option_state *)0,
3780  options,
3781  /* scope */ &global_scope,
3782  /* overload */ 0,
3783  /* terminate */0,
3784  /* bootpp */0,
3785  (struct data_string *)0,
3786  client -> config -> vendor_space_name);
3787 
3788  if (client -> packet_length < BOOTP_MIN_LEN)
3789  client -> packet_length = BOOTP_MIN_LEN;
3790  option_state_dereference (&options, MDL);
3791 
3792  client -> packet.op = BOOTREQUEST;
3793  client -> packet.htype = client -> interface -> hw_address.hbuf [0];
3794  /* Assumes hw_address is known, otherwise a random value may result */
3795  client -> packet.hlen = client -> interface -> hw_address.hlen - 1;
3796  client -> packet.hops = 0;
3797  client -> packet.xid = random ();
3798  client -> packet.secs = 0;
3799  client -> packet.flags = 0;
3800  memcpy (&client -> packet.ciaddr,
3801  lease -> address.iabuf, lease -> address.len);
3802  memset (&client -> packet.yiaddr, 0,
3803  sizeof client -> packet.yiaddr);
3804  memset (&client -> packet.siaddr, 0,
3805  sizeof client -> packet.siaddr);
3806  client -> packet.giaddr = giaddr;
3807  memcpy (client -> packet.chaddr,
3808  &client -> interface -> hw_address.hbuf [1],
3809  client -> interface -> hw_address.hlen);
3810 
3811 #ifdef DEBUG_PACKET
3812  dump_raw ((unsigned char *)&client -> packet, client -> packet_length);
3813 #endif
3814 }
3815 
3817  struct client_lease *lease;
3818 {
3819  if (lease -> server_name)
3820  dfree (lease -> server_name, MDL);
3821  if (lease -> filename)
3822  dfree (lease -> filename, MDL);
3825 }
3826 
3827 FILE *leaseFile = NULL;
3829 
3831 {
3832  struct interface_info *ip;
3833  struct client_state *client;
3834  struct client_lease *lp;
3835 
3836  if (leaseFile != NULL)
3837  fclose (leaseFile);
3838  leaseFile = fopen (path_dhclient_db, "we");
3839  if (leaseFile == NULL) {
3840  log_error ("can't create %s: %m", path_dhclient_db);
3841  return;
3842  }
3843 
3844  /* If there is a default duid, write it out. */
3845  if (default_duid.len != 0)
3846  write_duid(&default_duid);
3847 
3848  /* Write out all the leases attached to configured interfaces that
3849  we know about. */
3850  for (ip = interfaces; ip; ip = ip -> next) {
3851  for (client = ip -> client; client; client = client -> next) {
3852  for (lp = client -> leases; lp; lp = lp -> next) {
3853  write_client_lease (client, lp, 1, 0);
3854  }
3855  if (client -> active)
3856  write_client_lease (client,
3857  client -> active, 1, 0);
3858 
3859  if (client->active_lease != NULL)
3860  write_client6_lease(client,
3861  client->active_lease,
3862  1, 0);
3863 
3864  /* Reset last_write after rewrites. */
3865  client->last_write = 0;
3866  }
3867  }
3868 
3869  /* Write out any leases that are attached to interfaces that aren't
3870  currently configured. */
3871  for (ip = dummy_interfaces; ip; ip = ip -> next) {
3872  for (client = ip -> client; client; client = client -> next) {
3873  for (lp = client -> leases; lp; lp = lp -> next) {
3874  write_client_lease (client, lp, 1, 0);
3875  }
3876  if (client -> active)
3877  write_client_lease (client,
3878  client -> active, 1, 0);
3879 
3880  if (client->active_lease != NULL)
3881  write_client6_lease(client,
3882  client->active_lease,
3883  1, 0);
3884 
3885  /* Reset last_write after rewrites. */
3886  client->last_write = 0;
3887  }
3888  }
3889  fflush (leaseFile);
3890 }
3891 
3893  struct packet *packet, struct lease *lease,
3894  struct client_state *client_state,
3895  struct option_state *in_options,
3896  struct option_state *cfg_options,
3897  struct binding_scope **scope,
3898  struct universe *u, void *stuff)
3899 {
3900  const char *name, *dot;
3901  struct data_string ds;
3902  char *preamble = stuff;
3903 
3904  memset (&ds, 0, sizeof ds);
3905 
3906  if (u != &dhcp_universe) {
3907  name = u -> name;
3908  dot = ".";
3909  } else {
3910  name = "";
3911  dot = "";
3912  }
3914  in_options, cfg_options, scope, oc, MDL)) {
3915  /* The option name */
3916  fprintf(leaseFile, "%soption %s%s%s", preamble,
3917  name, dot, oc->option->name);
3918 
3919  /* The option value if there is one */
3920  if ((oc->option->format == NULL) ||
3921  (oc->option->format[0] != 'Z')) {
3922  fprintf(leaseFile, " %s",
3924  ds.len, 1, 1));
3925  }
3926 
3927  /* The closing semi-colon and newline */
3928  fprintf(leaseFile, ";\n");
3929 
3930  data_string_forget (&ds, MDL);
3931  }
3932 }
3933 
3934 /* Write an option cache to the lease store. */
3935 static void
3936 write_options(struct client_state *client, struct option_state *options,
3937  const char *preamble)
3938 {
3939  int i;
3940 
3941  for (i = 0; i < options->universe_count; i++) {
3942  option_space_foreach(NULL, NULL, client, NULL, options,
3943  &global_scope, universes[i],
3944  (char *)preamble, write_lease_option);
3945  }
3946 }
3947 
3948 int unhexchar(char c) {
3949 
3950  if (c >= '0' && c <= '9')
3951  return c - '0';
3952 
3953  if (c >= 'a' && c <= 'f')
3954  return c - 'a' + 10;
3955 
3956  if (c >= 'A' && c <= 'F')
3957  return c - 'A' + 10;
3958 
3959  return -1;
3960 }
3961 
3962 isc_result_t
3963 read_uuid(u_int8_t* uuid) {
3964  const char *id_fname = "/etc/machine-id";
3965  char id[32];
3966  size_t nread;
3967  FILE * file = fopen( id_fname , "r");
3968  if (!file) {
3969  log_debug("Cannot open %s", id_fname);
3970  return ISC_R_IOERROR;
3971  }
3972  nread = fread(id, 1, sizeof id, file);
3973  fclose(file);
3974 
3975  if (nread < 32) {
3976  log_debug("Not enough data in %s", id_fname);
3977  return ISC_R_IOERROR;
3978  }
3979  int j;
3980  for (j = 0; j < 16; j++) {
3981  int a, b;
3982 
3983  a = unhexchar(id[j*2]);
3984  b = unhexchar(id[j*2+1]);
3985 
3986  if (a < 0 || b < 0) {
3987  log_debug("Wrong data in %s", id_fname);
3988  return ISC_R_IOERROR;
3989  }
3990  uuid[j] = a << 4 | b;
3991  }
3992 
3993  /* Set UUID version to 4 --- truly random generation */
3994  uuid[6] = (uuid[6] & 0x0F) | 0x40;
3995  /* Set the UUID variant to DCE */
3996  uuid[8] = (uuid[8] & 0x3F) | 0x80;
3997 
3998  return ISC_R_SUCCESS;
3999 }
4000 
4001 /*
4002  * The "best" default DUID, since we cannot predict any information
4003  * about the system (such as whether or not the hardware addresses are
4004  * integrated into the motherboard or similar), is the "LLT", link local
4005  * plus time, DUID. For real stateless "LL" is better.
4006  *
4007  * Once generated, this duid is stored into the state database, and
4008  * retained across restarts.
4009  *
4010  * For the time being, there is probably a different state database for
4011  * every daemon, so this winds up being a per-interface identifier...which
4012  * is not how it is intended. Upcoming rearchitecting the client should
4013  * address this "one daemon model."
4014  */
4015 isc_result_t
4016 form_duid(struct data_string *duid, const char *file, int line)
4017 {
4018  struct interface_info *ip;
4019  int len;
4020  char *str;
4021  u_int8_t uuid[16];
4022 
4023  /* For now, just use the first interface on the list. */
4024  ip = interfaces;
4025 
4026  if (ip == NULL)
4027  log_fatal("Impossible condition at %s:%d.", MDL);
4028 
4029  while (ip && ip->hw_address.hbuf[0] == HTYPE_RESERVED) {
4030  /* Try the other interfaces */
4031  log_debug("Cannot form default DUID from interface %s.", ip->name);
4032  ip = ip->next;
4033  }
4034  if (ip == NULL) {
4035  return ISC_R_UNEXPECTED;
4036  }
4037 
4038  if ((ip->hw_address.hlen == 0) ||
4039  (ip->hw_address.hlen > sizeof(ip->hw_address.hbuf)))
4040  log_fatal("Impossible hardware address length at %s:%d.", MDL);
4041 
4042  if (duid_type == 0) {
4043  if (read_uuid(uuid) == ISC_R_SUCCESS)
4044  duid_type = DUID_UUID;
4045  else
4047  }
4048 
4049  if (duid_type == DUID_UUID)
4050  len = 2 + sizeof (uuid);
4051  else {
4052  /*
4053  * 2 bytes for the 'duid type' field.
4054  * 2 bytes for the 'htype' field.
4055  * (DUID_LLT) 4 bytes for the 'current time'.
4056  * enough bytes for the hardware address (note that hw_address has
4057  * the 'htype' on byte zero).
4058  */
4059  len = 4 + (ip->hw_address.hlen - 1);
4060  if (duid_type == DUID_LLT)
4061  len += 4;
4062  }
4063  if (!buffer_allocate(&duid->buffer, len, MDL))
4064  log_fatal("no memory for default DUID!");
4065  duid->data = duid->buffer->data;
4066  duid->len = len;
4067 
4068  if (duid_type == DUID_UUID) {
4069  putUShort(duid->buffer->data, DUID_UUID);
4070  memcpy(duid->buffer->data + 2, uuid, sizeof(uuid));
4071  }
4072  /* Basic Link Local Address type of DUID. */
4073  else if (duid_type == DUID_LLT) {
4074  putUShort(duid->buffer->data, DUID_LLT);
4075  putUShort(duid->buffer->data + 2, ip->hw_address.hbuf[0]);
4076  putULong(duid->buffer->data + 4, cur_time - DUID_TIME_EPOCH);
4077  memcpy(duid->buffer->data + 8, ip->hw_address.hbuf + 1,
4078  ip->hw_address.hlen - 1);
4079  } else {
4080  putUShort(duid->buffer->data, DUID_LL);
4081  putUShort(duid->buffer->data + 2, ip->hw_address.hbuf[0]);
4082  memcpy(duid->buffer->data + 4, ip->hw_address.hbuf + 1,
4083  ip->hw_address.hlen - 1);
4084  }
4085 
4086  /* Now format the output based on lease-id-format */
4087  str = format_lease_id(duid->data, duid->len,
4089  if (str == NULL) {
4090  log_info("form_duid: Couldn't allocate memory to log duid!");
4091  } else {
4092  log_info("Created duid %s.", str);
4093  dfree(str, MDL);
4094  }
4095 
4096  return ISC_R_SUCCESS;
4097 }
4098 
4099 /* Write the default DUID to the lease store. */
4100 static isc_result_t
4101 write_duid(struct data_string *duid)
4102 {
4103  char *str;
4104  int stat;
4105 
4106  if ((duid == NULL) || (duid->len <= 2))
4107  return DHCP_R_INVALIDARG;
4108 
4109  if (leaseFile == NULL) { /* XXX? */
4110  leaseFile = fopen(path_dhclient_db, "we");
4111  if (leaseFile == NULL) {
4112  log_error("can't create %s: %m", path_dhclient_db);
4113  return ISC_R_IOERROR;
4114  }
4115  }
4116 
4117  /* Generate a formatted duid string per lease-id-format */
4118  str = format_lease_id(duid->data, duid->len,
4120  if (str == NULL)
4121  return ISC_R_NOMEMORY;
4122 
4123  stat = fprintf(leaseFile, "default-duid %s;\n", str);
4124  dfree(str, MDL);
4125  if (stat <= 0)
4126  return ISC_R_IOERROR;
4127 
4128  if (fflush(leaseFile) != 0)
4129  return ISC_R_IOERROR;
4130 
4131  return ISC_R_SUCCESS;
4132 }
4133 
4134 /* Write a DHCPv6 lease to the store. */
4135 isc_result_t
4137  int rewrite, int sync)
4138 {
4139  struct dhc6_ia *ia;
4140  struct dhc6_addr *addr;
4141  int stat;
4142  const char *ianame;
4143 
4144  /* This should include the current lease. */
4145  if (!rewrite && (leases_written++ > 20)) {
4147  leases_written = 0;
4148  return ISC_R_SUCCESS;
4149  }
4150 
4151  if (client == NULL || lease == NULL)
4152  return DHCP_R_INVALIDARG;
4153 
4154  if (leaseFile == NULL) { /* XXX? */
4155  leaseFile = fopen(path_dhclient_db, "w");
4156  if (leaseFile == NULL) {
4157  log_error("can't create %s: %m", path_dhclient_db);
4158  return ISC_R_IOERROR;
4159  }
4160  }
4161 
4162  stat = fprintf(leaseFile, "lease6 {\n");
4163  if (stat <= 0)
4164  return ISC_R_IOERROR;
4165 
4166  stat = fprintf(leaseFile, " interface \"%s\";\n",
4167  client->interface->name);
4168  if (stat <= 0)
4169  return ISC_R_IOERROR;
4170 
4171  for (ia = lease->bindings ; ia != NULL ; ia = ia->next) {
4172  switch (ia->ia_type) {
4173  case D6O_IA_NA:
4174  default:
4175  ianame = "ia-na";
4176  break;
4177  case D6O_IA_TA:
4178  ianame = "ia-ta";
4179  break;
4180  case D6O_IA_PD:
4181  ianame = "ia-pd";
4182  break;
4183  }
4184 
4185  /* For some reason IAID was never octal or hex, but string or
4186  * hex. Go figure. So for compatibilty's sake we will either
4187  * do hex or "legacy" i.e string rather than octal. What a
4188  * cluster. */
4190  case TOKEN_HEX: {
4191  char* iaid_str = format_lease_id(
4192  (const unsigned char *) &ia->iaid, 4,
4194 
4195  if (!iaid_str) {
4196  log_error("Can't format iaid");
4197  return ISC_R_IOERROR;
4198  }
4199 
4200  stat = fprintf(leaseFile, " %s %s {\n",
4201  ianame, iaid_str);
4202  dfree(iaid_str, MDL);
4203  break;
4204  }
4205 
4206  case TOKEN_OCTAL:
4207  default:
4208  stat = fprintf(leaseFile, " %s %s {\n", ianame,
4209  print_hex_1(4, ia->iaid, 12));
4210  break;
4211  }
4212 
4213  if (stat <= 0)
4214  return ISC_R_IOERROR;
4215 
4216  if (ia->ia_type != D6O_IA_TA)
4217  stat = fprintf(leaseFile, " starts %d;\n"
4218  " renew %u;\n"
4219  " rebind %u;\n",
4220  (int)ia->starts, ia->renew, ia->rebind);
4221  else
4222  stat = fprintf(leaseFile, " starts %d;\n",
4223  (int)ia->starts);
4224  if (stat <= 0)
4225  return ISC_R_IOERROR;
4226 
4227  for (addr = ia->addrs ; addr != NULL ; addr = addr->next) {
4228  if (ia->ia_type != D6O_IA_PD)
4229  stat = fprintf(leaseFile,
4230  " iaaddr %s {\n",
4231  piaddr(addr->address));
4232  else
4233  stat = fprintf(leaseFile,
4234  " iaprefix %s/%d {\n",
4235  piaddr(addr->address),
4236  (int)addr->plen);
4237  if (stat <= 0)
4238  return ISC_R_IOERROR;
4239 
4240  stat = fprintf(leaseFile, " starts %d;\n"
4241  " preferred-life %u;\n"
4242  " max-life %u;\n",
4243  (int)addr->starts, addr->preferred_life,
4244  addr->max_life);
4245  if (stat <= 0)
4246  return ISC_R_IOERROR;
4247 
4248  if (addr->options != NULL)
4249  write_options(client, addr->options, " ");
4250 
4251  stat = fprintf(leaseFile, " }\n");
4252  if (stat <= 0)
4253  return ISC_R_IOERROR;
4254  }
4255 
4256  if (ia->options != NULL)
4257  write_options(client, ia->options, " ");
4258 
4259  stat = fprintf(leaseFile, " }\n");
4260  if (stat <= 0)
4261  return ISC_R_IOERROR;
4262  }
4263 
4264  if (lease->released) {
4265  stat = fprintf(leaseFile, " released;\n");
4266  if (stat <= 0)
4267  return ISC_R_IOERROR;
4268  }
4269 
4270  if (lease->options != NULL)
4271  write_options(client, lease->options, " ");
4272 
4273  stat = fprintf(leaseFile, "}\n");
4274  if (stat <= 0)
4275  return ISC_R_IOERROR;
4276 
4277  if (fflush(leaseFile) != 0)
4278  return ISC_R_IOERROR;
4279 
4280  if (sync) {
4281  if (fsync(fileno(leaseFile)) < 0) {
4282  log_error("write_client_lease: fsync(): %m");
4283  return ISC_R_IOERROR;
4284  }
4285  }
4286 
4287  return ISC_R_SUCCESS;
4288 }
4289 
4290 int write_client_lease (client, lease, rewrite, makesure)
4291  struct client_state *client;
4292  struct client_lease *lease;
4293  int rewrite;
4294  int makesure;
4295 {
4296  struct data_string ds;
4297  int errors = 0;
4298  char *s;
4299  const char *tval;
4300 
4301  if (!rewrite) {
4302  if (leases_written++ > 20) {
4304  leases_written = 0;
4305  }
4306  }
4307 
4308  /* If the lease came from the config file, we don't need to stash
4309  a copy in the lease database. */
4310  if (lease -> is_static)
4311  return 1;
4312 
4313  if (leaseFile == NULL) { /* XXX */
4314  leaseFile = fopen (path_dhclient_db, "we");
4315  if (leaseFile == NULL) {
4316  log_error ("can't create %s: %m", path_dhclient_db);
4317  return 0;
4318  }
4319  }
4320 
4321  errno = 0;
4322  fprintf (leaseFile, "lease {\n");
4323  if (lease -> is_bootp) {
4324  fprintf (leaseFile, " bootp;\n");
4325  if (errno) {
4326  ++errors;
4327  errno = 0;
4328  }
4329  }
4330  fprintf (leaseFile, " interface \"%s\";\n",
4331  client -> interface -> name);
4332  if (errno) {
4333  ++errors;
4334  errno = 0;
4335  }
4336  if (client -> name) {
4337  fprintf (leaseFile, " name \"%s\";\n", client -> name);
4338  if (errno) {
4339  ++errors;
4340  errno = 0;
4341  }
4342  }
4343  fprintf (leaseFile, " fixed-address %s;\n",
4344  piaddr (lease -> address));
4345  if (errno) {
4346  ++errors;
4347  errno = 0;
4348  }
4349  if (lease -> filename) {
4350  s = quotify_string (lease -> filename, MDL);
4351  if (s) {
4352  fprintf (leaseFile, " filename \"%s\";\n", s);
4353  if (errno) {
4354  ++errors;
4355  errno = 0;
4356  }
4357  dfree (s, MDL);
4358  } else
4359  errors++;
4360 
4361  }
4362  if (lease->server_name != NULL) {
4363  s = quotify_string(lease->server_name, MDL);
4364  if (s != NULL) {
4365  fprintf(leaseFile, " server-name \"%s\";\n", s);
4366  if (errno) {
4367  ++errors;
4368  errno = 0;
4369  }
4370  dfree(s, MDL);
4371  } else
4372  ++errors;
4373  }
4374  if (lease -> medium) {
4375  s = quotify_string (lease -> medium -> string, MDL);
4376  if (s) {
4377  fprintf (leaseFile, " medium \"%s\";\n", s);
4378  if (errno) {
4379  ++errors;
4380  errno = 0;
4381  }
4382  dfree (s, MDL);
4383  } else
4384  errors++;
4385  }
4386  if (errno != 0) {
4387  errors++;
4388  errno = 0;
4389  }
4390 
4391  memset (&ds, 0, sizeof ds);
4392 
4393  write_options(client, lease->options, " ");
4394 
4395  tval = print_time(lease->renewal);
4396  if (tval == NULL ||
4397  fprintf(leaseFile, " renew %s\n", tval) < 0)
4398  errors++;
4399 
4400  tval = print_time(lease->rebind);
4401  if (tval == NULL ||
4402  fprintf(leaseFile, " rebind %s\n", tval) < 0)
4403  errors++;
4404 
4405  tval = print_time(lease->expiry);
4406  if (tval == NULL ||
4407  fprintf(leaseFile, " expire %s\n", tval) < 0)
4408  errors++;
4409 
4410  if (fprintf(leaseFile, "}\n") < 0)
4411  errors++;
4412 
4413  if (fflush(leaseFile) != 0)
4414  errors++;
4415 
4416  client->last_write = cur_time;
4417 
4418  if (!errors && makesure) {
4419  if (fsync (fileno (leaseFile)) < 0) {
4420  log_info ("write_client_lease: %m");
4421  return 0;
4422  }
4423  }
4424 
4425  return errors ? 0 : 1;
4426 }
4427 
4428 /* Variables holding name of script and file pointer for writing to
4429  script. Needless to say, this is not reentrant - only one script
4430  can be invoked at a time. */
4431 char scriptName [256];
4433 
4446 void script_init(struct client_state *client, const char *reason,
4447  struct string_list *medium)
4448 {
4449  struct string_list *sl, *next;
4450 
4451  if (client) {
4452  for (sl = client -> env; sl; sl = next) {
4453  next = sl -> next;
4454  dfree (sl, MDL);
4455  }
4456  client -> env = (struct string_list *)0;
4457  client -> envc = 0;
4458 
4459  if (client -> interface) {
4460  client_envadd (client, "", "interface", "%s",
4461  client -> interface -> name);
4462  }
4463  if (client -> name)
4464  client_envadd (client,
4465  "", "client", "%s", client -> name);
4466  if (medium)
4467  client_envadd (client,
4468  "", "medium", "%s", medium -> string);
4469 
4470  client_envadd (client, "", "reason", "%s", reason);
4471  client_envadd (client, "", "pid", "%ld", (long int)getpid ());
4472  client_envadd (client, "", "dad_wait_time", "%ld",
4473  (long int)dad_wait_time);
4474  }
4475 }
4476 
4478  struct packet *packet, struct lease *lease,
4479  struct client_state *client_state,
4480  struct option_state *in_options,
4481  struct option_state *cfg_options,
4482  struct binding_scope **scope,
4483  struct universe *u, void *stuff)
4484 {
4485  struct envadd_state *es = stuff;
4486  struct data_string data;
4487  memset (&data, 0, sizeof data);
4488 
4490  in_options, cfg_options, scope, oc, MDL)) {
4491  if (data.len) {
4492  char name [256];
4493  if (dhcp_option_ev_name (name, sizeof name,
4494  oc->option)) {
4495  const char *value;
4496  size_t length;
4497  value = pretty_print_option(oc->option,
4498  data.data,
4499  data.len, 0, 0);
4500  length = strlen(value);
4501 
4502  if (check_option_values(oc->option->universe,
4503  oc->option->code,
4504  value, length) == 0) {
4505  client_envadd(es->client, es->prefix,
4506  name, "%s", value);
4507  } else {
4508  log_error("suspect value in %s "
4509  "option - discarded",
4510  name);
4511  }
4513  }
4514  }
4515  }
4516 }
4517 
4537 void script_write_params(struct client_state *client, const char *prefix,
4538  struct client_lease *lease)
4539 {
4540  int i;
4541  struct data_string data;
4542  struct option_cache *oc;
4543  struct envadd_state es;
4544 
4545  es.client = client;
4546  es.prefix = prefix;
4547 
4549  prefix, "ip_address", "%s", piaddr (lease -> address));
4550 
4551  /* If we've set the next server address in the lease structure
4552  put it into an environment variable for the script */
4553  if (lease->next_srv_addr.len != 0) {
4554  client_envadd(client, prefix, "next_server", "%s",
4555  piaddr(lease->next_srv_addr));
4556  }
4557 
4558  /* For the benefit of Linux (and operating systems which may
4559  have similar needs), compute the network address based on
4560  the supplied ip address and netmask, if provided. Also
4561  compute the broadcast address (the host address all ones
4562  broadcast address, not the host address all zeroes
4563  broadcast address). */
4564 
4565  memset (&data, 0, sizeof data);
4566  oc = lookup_option (&dhcp_universe, lease -> options, DHO_SUBNET_MASK);
4567  if (oc && evaluate_option_cache (&data, (struct packet *)0,
4568  (struct lease *)0, client,
4569  (struct option_state *)0,
4570  lease -> options,
4571  &global_scope, oc, MDL)) {
4572  if (data.len > 3) {
4573  struct iaddr netmask, subnet, broadcast;
4574 
4575  /*
4576  * No matter the length of the subnet-mask option,
4577  * use only the first four octets. Note that
4578  * subnet-mask options longer than 4 octets are not
4579  * in conformance with RFC 2132, but servers with this
4580  * flaw do exist.
4581  */
4582  memcpy(netmask.iabuf, data.data, 4);
4583  netmask.len = 4;
4584  data_string_forget (&data, MDL);
4585 
4586  subnet = subnet_number (lease -> address, netmask);
4587  if (subnet.len) {
4588  client_envadd (client, prefix, "network_number",
4589  "%s", piaddr (subnet));
4590 
4592  lease -> options,
4594  if (!oc ||
4596  (&data, (struct packet *)0,
4597  (struct lease *)0, client,
4598  (struct option_state *)0,
4599  lease -> options,
4600  &global_scope, oc, MDL))) {
4601  broadcast = broadcast_addr (subnet, netmask);
4602  if (broadcast.len) {
4603  client_envadd (client,
4604  prefix, "broadcast_address",
4605  "%s", piaddr (broadcast));
4606  }
4607  }
4608  }
4609  }
4610  data_string_forget (&data, MDL);
4611  }
4612 
4613  if (lease->filename) {
4614  if (check_option_values(NULL, DHO_ROOT_PATH,
4615  lease->filename,
4616  strlen(lease->filename)) == 0) {
4617  client_envadd(client, prefix, "filename",
4618  "%s", lease->filename);
4619  } else {
4620  log_error("suspect value in %s "
4621  "option - discarded",
4622  lease->filename);
4623  }
4624  }
4625 
4626  if (lease->server_name) {
4627  if (check_option_values(NULL, DHO_HOST_NAME,
4628  lease->server_name,
4629  strlen(lease->server_name)) == 0 ) {
4630  client_envadd (client, prefix, "server_name",
4631  "%s", lease->server_name);
4632  } else {
4633  log_error("suspect value in %s "
4634  "option - discarded",
4635  lease->server_name);
4636  }
4637  }
4638 
4639  for (i = 0; i < lease -> options -> universe_count; i++) {
4640  option_space_foreach ((struct packet *)0, (struct lease *)0,
4641  client, (struct option_state *)0,
4642  lease -> options, &global_scope,
4643  universes [i],
4644  &es, client_option_envadd);
4645  }
4646 
4647  client_envadd (client, prefix, "expiry", "%lu",
4648  (unsigned long)(lease -> expiry));
4649 }
4650 
4661 {
4662  int i;
4663  struct option **req;
4664  char name[256];
4665  req = client->config->requested_options;
4666 
4667  if (req == NULL)
4668  return;
4669 
4670  for (i = 0 ; req[i] != NULL ; i++) {
4671  if ((req[i]->universe == &dhcp_universe) &&
4672  dhcp_option_ev_name(name, sizeof(name), req[i])) {
4673  client_envadd(client, "requested_", name, "%d", 1);
4674  }
4675  }
4676 }
4677 
4690 int script_go(struct client_state *client)
4691 {
4692  char *scriptName;
4693  char *argv [2];
4694  char **envp;
4695  char reason [] = "REASON=NBI";
4696  static char client_path [] = CLIENT_PATH;
4697  int i;
4698  struct string_list *sp, *next;
4699  int pid, wpid, wstatus;
4700 
4701  if (client)
4702  scriptName = client -> config -> script_name;
4703  else
4705 
4706  envp = dmalloc (((client ? client -> envc : 2) +
4707  client_env_count + 2) * sizeof (char *), MDL);
4708  if (!envp) {
4709  log_error ("No memory for client script environment.");
4710  return 0;
4711  }
4712  i = 0;
4713  /* Copy out the environment specified on the command line,
4714  if any. */
4715  for (sp = client_env; sp; sp = sp -> next) {
4716  envp [i++] = sp -> string;
4717  }
4718  /* Copy out the environment specified by dhclient. */
4719  if (client) {
4720  for (sp = client -> env; sp; sp = sp -> next) {
4721  envp [i++] = sp -> string;
4722  }
4723  } else {
4724  envp [i++] = reason;
4725  }
4726  /* Set $PATH. */
4727  envp [i++] = client_path;
4728  envp [i] = (char *)0;
4729 
4730  argv [0] = scriptName;
4731  argv [1] = (char *)0;
4732 
4733  pid = fork ();
4734  if (pid < 0) {
4735  log_error ("fork: %m");
4736  wstatus = 0;
4737  } else if (pid) {
4738  do {
4739  wpid = wait (&wstatus);
4740  } while (wpid != pid && wpid > 0);
4741  if (wpid < 0) {
4742  log_error ("wait: %m");
4743  wstatus = 0;
4744  }
4745  } else {
4746  /* We don't want to pass an open file descriptor for
4747  * dhclient.leases when executing dhclient-script.
4748  */
4749  if (leaseFile != NULL)
4750  fclose(leaseFile);
4751  execve (scriptName, argv, envp);
4752  log_error ("execve (%s, ...): %m", scriptName);
4753  exit (0);
4754  }
4755 
4756  if (client) {
4757  for (sp = client -> env; sp; sp = next) {
4758  next = sp -> next;
4759  dfree (sp, MDL);
4760  }
4761  client -> env = (struct string_list *)0;
4762  client -> envc = 0;
4763  }
4764  dfree (envp, MDL);
4765  gettimeofday(&cur_tv, NULL);
4766  return (WIFEXITED (wstatus) ?
4767  WEXITSTATUS (wstatus) : -WTERMSIG (wstatus));
4768 }
4769 
4770 void client_envadd (struct client_state *client,
4771  const char *prefix, const char *name, const char *fmt, ...)
4772 {
4773  char spbuf [1024];
4774  char *s;
4775  unsigned len;
4776  struct string_list *val;
4777  va_list list;
4778 
4779  va_start (list, fmt);
4780  len = vsnprintf (spbuf, sizeof spbuf, fmt, list);
4781  va_end (list);
4782 
4783  val = dmalloc (strlen (prefix) + strlen (name) + 1 /* = */ +
4784  len + sizeof *val, MDL);
4785  if (!val) {
4786  log_error ("client_envadd: cannot allocate space for variable");
4787  return;
4788  }
4789 
4790  s = val -> string;
4791  strcpy (s, prefix);
4792  strcat (s, name);
4793  s += strlen (s);
4794  *s++ = '=';
4795  if (len >= sizeof spbuf) {
4796  va_start (list, fmt);
4797  vsnprintf (s, len + 1, fmt, list);
4798  va_end (list);
4799  } else {
4800  strcpy (s, spbuf);
4801  }
4802 
4803  val -> next = client -> env;
4804  client -> env = val;
4805  client -> envc++;
4806 }
4807 
4808 int dhcp_option_ev_name (buf, buflen, option)
4809  char *buf;
4810  size_t buflen;
4811  struct option *option;
4812 {
4813  int i, j;
4814  const char *s;
4815 
4816  j = 0;
4817  if (option -> universe != &dhcp_universe) {
4818  s = option -> universe -> name;
4819  i = 0;
4820  } else {
4821  s = option -> name;
4822  i = 1;
4823  }
4824 
4825  do {
4826  while (*s) {
4827  if (j + 1 == buflen)
4828  return 0;
4829  if (*s == '-')
4830  buf [j++] = '_';
4831  else
4832  buf [j++] = *s;
4833  ++s;
4834  }
4835  if (!i) {
4836  s = option -> name;
4837  if (j + 1 == buflen)
4838  return 0;
4839  buf [j++] = '_';
4840  }
4841  ++i;
4842  } while (i != 2);
4843 
4844  buf [j] = 0;
4845  return 1;
4846 }
4847 
4848 void go_daemon ()
4849 {
4850  static int state = 0;
4851  int pid;
4852 
4853  /* Don't become a daemon if the user requested otherwise. */
4854  if (no_daemon) {
4856  return;
4857  }
4858 
4859  /* Only do it once. */
4860  if (state)
4861  return;
4862  state = 1;
4863 
4864  /* Stop logging to stderr... */
4865  log_perror = 0;
4866 
4867  /* Become a daemon... */
4868  if ((pid = fork ()) < 0)
4869  log_fatal ("Can't fork daemon: %m");
4870  else if (pid)
4871  exit (0);
4872  /* Become session leader and get pid... */
4873  (void) setsid ();
4874 
4875  /* Close standard I/O descriptors. */
4876  (void) close(0);
4877  (void) close(1);
4878  (void) close(2);
4879 
4880  /* Reopen them on /dev/null. */
4881  (void) open("/dev/null", O_RDWR | O_CLOEXEC);
4882  (void) open("/dev/null", O_RDWR | O_CLOEXEC);
4883  (void) open("/dev/null", O_RDWR | O_CLOEXEC);
4884 
4886 
4887  IGNORE_RET (chdir("/"));
4888 }
4889 
4891 {
4892  FILE *pf;
4893  int pfdesc;
4894 
4895  /* nothing to do if the user doesn't want a pid file */
4896  if (no_pid_file == ISC_TRUE) {
4897  return;
4898  }
4899 
4900  pfdesc = open (path_dhclient_pid, O_CREAT | O_TRUNC | O_WRONLY | O_CLOEXEC, 0644);
4901 
4902  if (pfdesc < 0) {
4903  log_error ("Can't create %s: %m", path_dhclient_pid);
4904  return;
4905  }
4906 
4907  pf = fdopen (pfdesc, "we");
4908  if (!pf) {
4909  close(pfdesc);
4910  log_error ("Can't fdopen %s: %m", path_dhclient_pid);
4911  } else {
4912  fprintf (pf, "%ld\n", (long)getpid ());
4913  fclose (pf);
4914  }
4915 }
4916 
4918 {
4919  struct interface_info *ip;
4920  struct client_state *client;
4921 
4922  for (ip = interfaces; ip; ip = ip -> next) {
4923  for (client = ip -> client; client; client = client -> next) {
4924  switch (client -> state) {
4925  case S_SELECTING:
4926  cancel_timeout (send_discover, client);
4927  break;
4928 
4929  case S_BOUND:
4930  cancel_timeout (state_bound, client);
4931  break;
4932 
4933  case S_REBOOTING:
4934  case S_REQUESTING:
4935  case S_RENEWING:
4936  cancel_timeout (send_request, client);
4937  break;
4938 
4939  case S_INIT:
4940  case S_REBINDING:
4941  case S_STOPPED:
4942  case S_DECLINED:
4943  break;
4944  }
4945  client -> state = S_INIT;
4946  state_reboot (client);
4947  }
4948  }
4949 }
4950 
4951 void do_release(client)
4952  struct client_state *client;
4953 {
4954  struct data_string ds;
4955  struct option_cache *oc;
4956 
4957 #if defined(DHCPv6) && defined(DHCP4o6)
4958  if (dhcpv4_over_dhcpv6 && (dhcp4o6_state <= 0)) {
4959  if (dhcp4o6_state < 0)
4960  dhcp4o6_poll(NULL);
4961  client->pending = P_RELEASE;
4962  return;
4963  }
4964 #endif
4965 
4966  /* Pick a random xid. */
4967  client -> xid = random ();
4968 
4969  /* is there even a lease to release? */
4970  if (client -> active) {
4971  /* Make a DHCPRELEASE packet, and set appropriate per-interface
4972  flags. */
4973  make_release (client, client -> active);
4974 
4975  memset (&ds, 0, sizeof ds);
4977  client -> active -> options,
4979  if (oc &&
4980  evaluate_option_cache (&ds, (struct packet *)0,
4981  (struct lease *)0, client,
4982  (struct option_state *)0,
4983  client -> active -> options,
4984  &global_scope, oc, MDL)) {
4985  if (ds.len > 3) {
4986  memcpy (client -> destination.iabuf,
4987  ds.data, 4);
4988  client -> destination.len = 4;
4989  } else
4990  client -> destination = iaddr_broadcast;
4991 
4992  data_string_forget (&ds, MDL);
4993  } else
4994  client -> destination = iaddr_broadcast;
4995  client -> first_sending = cur_time;
4996  client -> interval = client -> config -> initial_interval;
4997 
4998  /* Zap the medium list... */
4999  client -> medium = (struct string_list *)0;
5000 
5001  /* Send out the first and only DHCPRELEASE packet. */
5002  send_release (client);
5003 
5004  /* Do the client script RELEASE operation. */
5005  script_init (client,
5006  "RELEASE", (struct string_list *)0);
5007  if (client -> alias)
5008  script_write_params(client, "alias_",
5009  client -> alias);
5010  script_write_params(client, "old_", client -> active);
5011  script_write_requested(client);
5012  script_go(client);
5013  }
5014 
5015  /* Cancel any timeouts. */
5016  cancel_timeout (state_bound, client);
5017  cancel_timeout (send_discover, client);
5018  cancel_timeout (state_init, client);
5019  cancel_timeout (send_request, client);
5020  cancel_timeout (state_reboot, client);
5021  client -> state = S_STOPPED;
5022 
5023 #if defined(DHCPv6) && defined(DHCP4o6)
5024  if (dhcpv4_over_dhcpv6)
5025  exit(0);
5026 #endif
5027 }
5028 
5030 {
5031  do_release (interface -> client);
5032 
5033  return 1;
5034 }
5035 
5037 {
5038  struct interface_info *last, *ip;
5039  /* See if we can find the client from dummy_interfaces */
5040  last = 0;
5041  for (ip = dummy_interfaces; ip; ip = ip -> next) {
5042  if (!strcmp (ip -> name, tmp -> name)) {
5043  /* Remove from dummy_interfaces */
5044  if (last) {
5045  ip = (struct interface_info *)0;
5046  interface_reference (&ip, last -> next, MDL);
5047  interface_dereference (&last -> next, MDL);
5048  if (ip -> next) {
5049  interface_reference (&last -> next,
5050  ip -> next, MDL);
5051  interface_dereference (&ip -> next,
5052  MDL);
5053  }
5054  } else {
5055  ip = (struct interface_info *)0;
5056  interface_reference (&ip,
5058  interface_dereference (&dummy_interfaces, MDL);
5059  if (ip -> next) {
5060  interface_reference (&dummy_interfaces,
5061  ip -> next, MDL);
5062  interface_dereference (&ip -> next,
5063  MDL);
5064  }
5065  }
5066  /* Copy "client" to tmp */
5067  if (ip -> client) {
5068  tmp -> client = ip -> client;
5069  tmp -> client -> interface = tmp;
5070  }
5071  interface_dereference (&ip, MDL);
5072  break;
5073  }
5074  last = ip;
5075  }
5076  return 1;
5077 }
5078 
5079 isc_result_t dhclient_interface_startup_hook (struct interface_info *interface)
5080 {
5081  struct interface_info *ip;
5082  struct client_state *client;
5083 
5084  /* This code needs some rethinking. It doesn't test against
5085  a signal name, and it just kind of bulls into doing something
5086  that may or may not be appropriate. */
5087 
5088  if (interfaces) {
5089  interface_reference (&interface -> next, interfaces, MDL);
5090  interface_dereference (&interfaces, MDL);
5091  }
5092  interface_reference (&interfaces, interface, MDL);
5093 
5095 
5096  for (ip = interfaces; ip; ip = ip -> next) {
5097  /* If interfaces were specified, don't configure
5098  interfaces that weren't specified! */
5099  if (ip -> flags & INTERFACE_RUNNING ||
5100  (ip -> flags & (INTERFACE_REQUESTED |
5101  INTERFACE_AUTOMATIC)) !=
5103  continue;
5104  script_init (ip -> client,
5105  "PREINIT", (struct string_list *)0);
5106  if (ip -> client -> alias)
5107  script_write_params(ip -> client, "alias_",
5108  ip -> client -> alias);
5109  script_go(ip -> client);
5110  }
5111 
5114  : DISCOVER_RUNNING);
5115 
5116  for (ip = interfaces; ip; ip = ip -> next) {
5117  if (ip -> flags & INTERFACE_RUNNING)
5118  continue;
5119  ip -> flags |= INTERFACE_RUNNING;
5120  for (client = ip->client ; client ; client = client->next) {
5121  client->state = S_INIT;
5122  state_reboot(client);
5123  }
5124  }
5125  return ISC_R_SUCCESS;
5126 }
5127 
5128 /* The client should never receive a relay agent information option,
5129  so if it does, log it and discard it. */
5130 
5132  struct packet *packet;
5133  int len;
5134  u_int8_t *data;
5135 {
5136  return 1;
5137 }
5138 
5139 /* The client never sends relay agent information options. */
5140 
5141 unsigned cons_agent_information_options (cfg_options, outpacket,
5142  agentix, length)
5143  struct option_state *cfg_options;
5144  struct dhcp_packet *outpacket;
5145  unsigned agentix;
5146  unsigned length;
5147 {
5148  return length;
5149 }
5150 
5151 static void shutdown_exit (void *foo)
5152 {
5153  /* get rid of the pid if we can */
5154  if (no_pid_file == ISC_FALSE)
5155  (void) unlink(path_dhclient_pid);
5156  exit (0);
5157 }
5158 
5159 #if defined (NSUPDATE)
5160 /*
5161  * If the first query fails, the updater MUST NOT delete the DNS name. It
5162  * may be that the host whose lease on the server has expired has moved
5163  * to another network and obtained a lease from a different server,
5164  * which has caused the client's A RR to be replaced. It may also be
5165  * that some other client has been configured with a name that matches
5166  * the name of the DHCP client, and the policy was that the last client
5167  * to specify the name would get the name. In this case, the DHCID RR
5168  * will no longer match the updater's notion of the client-identity of
5169  * the host pointed to by the DNS name.
5170  * -- "Interaction between DHCP and DNS"
5171  */
5172 
5173 /* The first and second stages are pretty similar so we combine them */
5174 void
5175 client_dns_remove_action(dhcp_ddns_cb_t *ddns_cb,
5176  isc_result_t eresult)
5177 {
5178 
5179  isc_result_t result;
5180 
5181  if ((eresult == ISC_R_SUCCESS) &&
5182  (ddns_cb->state == DDNS_STATE_REM_FW_YXDHCID)) {
5183  /* Do the second stage of the FWD removal */
5184  ddns_cb->state = DDNS_STATE_REM_FW_NXRR;
5185 
5186  result = ddns_modify_fwd(ddns_cb, MDL);
5187  if (result == ISC_R_SUCCESS) {
5188  return;
5189  }
5190  }
5191 
5192  /* If we are done or have an error clean up */
5193  dhclient_ddns_cb_free(ddns_cb, MDL);
5194  return;
5195 }
5196 
5197 void
5198 client_dns_remove(struct client_state *client,
5199  struct iaddr *addr)
5200 {
5201  dhcp_ddns_cb_t *ddns_cb;
5202  isc_result_t result;
5203 
5204  /* if we have an old ddns request for this client, cancel it */
5205  if (client->ddns_cb != NULL) {
5206  ddns_cancel(client->ddns_cb, MDL);
5207  client->ddns_cb = NULL;
5208  }
5209 
5210  ddns_cb = ddns_cb_alloc(MDL);
5211  if (ddns_cb != NULL) {
5212  ddns_cb->address = *addr;
5213  ddns_cb->timeout = 0;
5214 
5215  ddns_cb->state = DDNS_STATE_REM_FW_YXDHCID;
5216  ddns_cb->flags = DDNS_UPDATE_ADDR;
5217  ddns_cb->cur_func = client_dns_remove_action;
5218 
5219  result = client_dns_update(client, ddns_cb);
5220 
5221  if (result != ISC_R_TIMEDOUT) {
5222  dhclient_ddns_cb_free(ddns_cb, MDL);
5223  }
5224  }
5225 }
5226 #endif
5227 
5229  control_object_state_t newstate)
5230 {
5231  struct interface_info *ip;
5232  struct client_state *client;
5233  struct timeval tv;
5234 
5235  if (newstate == server_shutdown) {
5236  /* Re-entry */
5237  if (shutdown_signal == SIGUSR1)
5238  return ISC_R_SUCCESS;
5239  /* Log shutdown on signal. */
5240  if ((shutdown_signal == SIGINT) ||
5241  (shutdown_signal == SIGTERM)) {
5242  log_info("Received signal %d, initiating shutdown.",
5243  shutdown_signal);
5244  }
5245  /* Mark it was called. */
5246  shutdown_signal = SIGUSR1;
5247  }
5248 
5249  /* Do the right thing for each interface. */
5250  for (ip = interfaces; ip; ip = ip -> next) {
5251  for (client = ip -> client; client; client = client -> next) {
5252  switch (newstate) {
5253  case server_startup:
5254  return ISC_R_SUCCESS;
5255 
5256  case server_running:
5257  return ISC_R_SUCCESS;
5258 
5259  case server_shutdown:
5260  if (client -> active &&
5261  client -> active -> expiry > cur_time) {
5262 #if defined (NSUPDATE)
5263  if (client->config->do_forward_update) {
5264  client_dns_remove(client,
5265  &client->active->address);
5266  }
5267 #endif
5268  do_release (client);
5269  }
5270  break;
5271 
5272  case server_hibernate:
5273  state_stop (client);
5274  break;
5275 
5276  case server_awaken:
5277  state_reboot (client);
5278  break;
5279  }
5280  }
5281  }
5282 
5283  if (newstate == server_shutdown) {
5284  tv.tv_sec = cur_tv.tv_sec;
5285  tv.tv_usec = cur_tv.tv_usec + 1;
5286  add_timeout(&tv, shutdown_exit, 0, 0, 0);
5287  }
5288  return ISC_R_SUCCESS;
5289 }
5290 
5291 #if defined (NSUPDATE)
5292 /*
5293  * Called after a timeout if the DNS update failed on the previous try.
5294  * Starts the retry process. If the retry times out it will schedule
5295  * this routine to run again after a 10x wait.
5296  */
5297 void
5298 client_dns_update_timeout (void *cp)
5299 {
5300  dhcp_ddns_cb_t *ddns_cb = (dhcp_ddns_cb_t *)cp;
5301  struct client_state *client = (struct client_state *)ddns_cb->lease;
5302  isc_result_t status = ISC_R_FAILURE;
5303 
5304  if ((client != NULL) &&
5305  ((client->active != NULL) ||
5306  (client->active_lease != NULL)))
5307  status = client_dns_update(client, ddns_cb);
5308 
5309  /*
5310  * A status of timedout indicates that we started the update and
5311  * have released control of the control block. Any other status
5312  * indicates that we should clean up the control block. We either
5313  * got a success which indicates that we didn't really need to
5314  * send an update or some other error in which case we weren't able
5315  * to start the update process. In both cases we still own
5316  * the control block and should free it.
5317  */
5318  if (status != ISC_R_TIMEDOUT) {
5319  dhclient_ddns_cb_free(ddns_cb, MDL);
5320  }
5321 }
5322 
5323 /*
5324  * If the first query succeeds, the updater can conclude that it
5325  * has added a new name whose only RRs are the A and DHCID RR records.
5326  * The A RR update is now complete (and a client updater is finished,
5327  * while a server might proceed to perform a PTR RR update).
5328  * -- "Interaction between DHCP and DNS"
5329  *
5330  * If the second query succeeds, the updater can conclude that the current
5331  * client was the last client associated with the domain name, and that
5332  * the name now contains the updated A RR. The A RR update is now
5333  * complete (and a client updater is finished, while a server would
5334  * then proceed to perform a PTR RR update).
5335  * -- "Interaction between DHCP and DNS"
5336  *
5337  * If the second query fails with NXRRSET, the updater must conclude
5338  * that the client's desired name is in use by another host. At this
5339  * juncture, the updater can decide (based on some administrative
5340  * configuration outside of the scope of this document) whether to let
5341  * the existing owner of the name keep that name, and to (possibly)
5342  * perform some name disambiguation operation on behalf of the current
5343  * client, or to replace the RRs on the name with RRs that represent
5344  * the current client. If the configured policy allows replacement of
5345  * existing records, the updater submits a query that deletes the
5346  * existing A RR and the existing DHCID RR, adding A and DHCID RRs that
5347  * represent the IP address and client-identity of the new client.
5348  * -- "Interaction between DHCP and DNS"
5349  */
5350 
5351 /* The first and second stages are pretty similar so we combine them */
5352 void
5353 client_dns_update_action(dhcp_ddns_cb_t *ddns_cb,
5354  isc_result_t eresult)
5355 {
5356  isc_result_t result;
5357  struct timeval tv;
5358 
5359  switch(eresult) {
5360  case ISC_R_SUCCESS:
5361  default:
5362  /* Either we succeeded or broke in a bad way, clean up */
5363  break;
5364 
5365  case DNS_R_YXRRSET:
5366  /*
5367  * This is the only difference between the two stages,
5368  * check to see if it is the first stage, in which case
5369  * start the second stage
5370  */
5371  if (ddns_cb->state == DDNS_STATE_ADD_FW_NXDOMAIN) {
5372  ddns_cb->state = DDNS_STATE_ADD_FW_YXDHCID;
5373  ddns_cb->cur_func = client_dns_update_action;
5374 
5375  result = ddns_modify_fwd(ddns_cb, MDL);
5376  if (result == ISC_R_SUCCESS) {
5377  return;
5378  }
5379  }
5380  break;
5381 
5382  case ISC_R_TIMEDOUT:
5383  /*
5384  * We got a timeout response from the DNS module. Schedule
5385  * another attempt for later. We forget the name, dhcid and
5386  * zone so if it gets changed we will get the new information.
5387  */
5388  data_string_forget(&ddns_cb->fwd_name, MDL);
5389  data_string_forget(&ddns_cb->dhcid, MDL);
5390  if (ddns_cb->zone != NULL) {
5391  forget_zone((struct dns_zone **)&ddns_cb->zone);
5392  }
5393 
5394  /* Reset to doing the first stage */
5395  ddns_cb->state = DDNS_STATE_ADD_FW_NXDOMAIN;
5396  ddns_cb->cur_func = client_dns_update_action;
5397 
5398  /* and update our timer */
5399  if (ddns_cb->timeout < 3600)
5400  ddns_cb->timeout *= 10;
5401  tv.tv_sec = cur_tv.tv_sec + ddns_cb->timeout;
5402  tv.tv_usec = cur_tv.tv_usec;
5404  ddns_cb, NULL, NULL);
5405  return;
5406  }
5407 
5408  dhclient_ddns_cb_free(ddns_cb, MDL);
5409  return;
5410 }
5411 
5412 /* See if we should do a DNS update, and if so, do it. */
5413 
5414 isc_result_t
5415 client_dns_update(struct client_state *client, dhcp_ddns_cb_t *ddns_cb)
5416 {
5417  struct data_string client_identifier;
5418  struct option_cache *oc;
5419  int ignorep;
5420  int result;
5421  int ddns_v4_type;
5422  isc_result_t rcode;
5423 
5424  /* If we didn't send an FQDN option, we certainly aren't going to
5425  be doing an update. */
5426  if (!client -> sent_options)
5427  return ISC_R_SUCCESS;
5428 
5429  /* If we don't have a lease, we can't do an update. */
5430  if ((client->active == NULL) && (client->active_lease == NULL))
5431  return ISC_R_SUCCESS;
5432 
5433  /* If we set the no client update flag, don't do the update. */
5434  if ((oc = lookup_option (&fqdn_universe, client -> sent_options,
5436  evaluate_boolean_option_cache (&ignorep, (struct packet *)0,
5437  (struct lease *)0, client,
5438  client -> sent_options,
5439  (struct option_state *)0,
5440  &global_scope, oc, MDL))
5441  return ISC_R_SUCCESS;
5442 
5443  /* If we set the "server, please update" flag, or didn't set it
5444  to false, don't do the update. */
5445  if (!(oc = lookup_option (&fqdn_universe, client -> sent_options,
5446  FQDN_SERVER_UPDATE)) ||
5447  evaluate_boolean_option_cache (&ignorep, (struct packet *)0,
5448  (struct lease *)0, client,
5449  client -> sent_options,
5450  (struct option_state *)0,
5451  &global_scope, oc, MDL))
5452  return ISC_R_SUCCESS;
5453 
5454  /* If no FQDN option was supplied, don't do the update. */
5455  if (!(oc = lookup_option (&fqdn_universe, client -> sent_options,
5456  FQDN_FQDN)) ||
5457  !evaluate_option_cache (&ddns_cb->fwd_name, (struct packet *)0,
5458  (struct lease *)0, client,
5459  client -> sent_options,
5460  (struct option_state *)0,
5461  &global_scope, oc, MDL))
5462  return ISC_R_SUCCESS;
5463 
5464  /*
5465  * Construct the DHCID value for use in the DDNS update process
5466  * We have the newer standard version and the older interim version
5467  * chosen by the '-I' option. The interim version is left as is
5468  * for backwards compatibility. The standard version is based on
5469  * RFC 4701 section 3.3
5470  */
5471 
5472  result = 0;
5473  POST(result);
5474  memset(&client_identifier, 0, sizeof(client_identifier));
5475 
5476  if (std_dhcid == 1) {
5477  /* standard style */
5478  ddns_cb->dhcid_class = dns_rdatatype_dhcid;
5479  ddns_v4_type = 1;
5480  } else {
5481  /* interim style */
5482  ddns_cb->dhcid_class = dns_rdatatype_txt;
5483  /* for backwards compatibility */
5484  ddns_v4_type = DHO_DHCP_CLIENT_IDENTIFIER;
5485  }
5486  if (client->active_lease != NULL) {
5487  /* V6 request, get the client identifier, then
5488  * construct the dhcid for either standard
5489  * or interim */
5490  if (((oc = lookup_option(&dhcpv6_universe,
5491  client->sent_options,
5492  D6O_CLIENTID)) != NULL) &&
5493  evaluate_option_cache(&client_identifier, NULL,
5494  NULL, client,
5495  client->sent_options, NULL,
5496  &global_scope, oc, MDL)) {
5497  result = get_dhcid(ddns_cb, 2,
5498  client_identifier.data,
5499  client_identifier.len);
5500  data_string_forget(&client_identifier, MDL);
5501  } else
5502  log_fatal("Impossible condition at %s:%d.", MDL);
5503  } else {
5504  /*
5505  * V4 request, use the client id if there is one or the
5506  * mac address if there isn't. If we have a client id
5507  * we check to see if it is an embedded DUID.
5508  */
5509  if (((oc = lookup_option(&dhcp_universe,
5510  client->sent_options,
5511  DHO_DHCP_CLIENT_IDENTIFIER)) != NULL) &&
5512  evaluate_option_cache(&client_identifier, NULL,
5513  NULL, client,
5514  client->sent_options, NULL,
5515  &global_scope, oc, MDL)) {
5516  if ((std_dhcid == 1) && (duid_v4 == 1) &&
5517  (client_identifier.data[0] == 255)) {
5518  /*
5519  * This appears to be an embedded DUID,
5520  * extract it and treat it as such
5521  */
5522  if (client_identifier.len <= 5)
5523  log_fatal("Impossible condition at %s:%d.",
5524  MDL);
5525  result = get_dhcid(ddns_cb, 2,
5526  client_identifier.data + 5,
5527  client_identifier.len - 5);
5528  } else {
5529  result = get_dhcid(ddns_cb, ddns_v4_type,
5530  client_identifier.data,
5531  client_identifier.len);
5532  }
5533  data_string_forget(&client_identifier, MDL);
5534  } else
5535  result = get_dhcid(ddns_cb, 0,
5536  client->interface->hw_address.hbuf,
5537  client->interface->hw_address.hlen);
5538  }
5539 
5540  if (!result) {
5541  return ISC_R_SUCCESS;
5542  }
5543 
5544  /*
5545  * Perform updates.
5546  */
5547  if (ddns_cb->fwd_name.len && ddns_cb->dhcid.len) {
5548  rcode = ddns_modify_fwd(ddns_cb, MDL);
5549  } else
5550  rcode = ISC_R_FAILURE;
5551 
5552  /*
5553  * A success from the modify routine means we are performing
5554  * async processing, for which we use the timedout error message.
5555  */
5556  if (rcode == ISC_R_SUCCESS) {
5557  rcode = ISC_R_TIMEDOUT;
5558  }
5559 
5560  return rcode;
5561 }
5562 
5563 
5564 /*
5565  * Schedule the first update. They will continue to retry occasionally
5566  * until they no longer time out (or fail).
5567  */
5568 void
5570  struct iaddr *addr,
5571  int offset)
5572 {
5573  dhcp_ddns_cb_t *ddns_cb;
5574  struct timeval tv;
5575 
5576  if (!client->config->do_forward_update)
5577  return;
5578 
5579  /* cancel any outstanding ddns requests */
5580  if (client->ddns_cb != NULL) {
5581  ddns_cancel(client->ddns_cb, MDL);
5582  client->ddns_cb = NULL;
5583  }
5584 
5585  ddns_cb = ddns_cb_alloc(MDL);
5586 
5587  if (ddns_cb != NULL) {
5588  ddns_cb->lease = (void *)client;
5589  ddns_cb->address = *addr;
5590  ddns_cb->timeout = 1;
5591 
5592  /*
5593  * XXX: DNS TTL is a problem we need to solve properly.
5594  * Until that time, 300 is a placeholder default for
5595  * something that is less insane than a value scaled
5596  * by lease timeout.
5597  */
5598  ddns_cb->ttl = 300;
5599 
5600  ddns_cb->state = DDNS_STATE_ADD_FW_NXDOMAIN;
5601  ddns_cb->cur_func = client_dns_update_action;
5603 
5604  client->ddns_cb = ddns_cb;
5605  tv.tv_sec = cur_tv.tv_sec + offset;
5606  tv.tv_usec = cur_tv.tv_usec;
5608  ddns_cb, NULL, NULL);
5609  } else {
5610  log_error("Unable to allocate dns update state for %s",
5611  piaddr(*addr));
5612  }
5613 }
5614 #endif
5615 
5616 void
5618 {
5619  struct servent *ent;
5620 
5621  if (path_dhclient_pid == NULL)
5623  if (path_dhclient_db == NULL)
5625 
5626  /* Default to the DHCP/BOOTP port. */
5627  if (!local_port) {
5628  /* If we're faking a relay agent, and we're not using loopback,
5629  use the server port, not the client port. */
5630  if (mockup_relay && giaddr.s_addr != htonl(INADDR_LOOPBACK)) {
5631  local_port = htons(67);
5632  } else {
5633  ent = getservbyname("dhcpc", "udp");
5634  if (ent == NULL)
5635  ent = getservbyname("bootpc", "udp");
5636  if (ent == NULL)
5637  local_port = htons(68);
5638  else
5639  local_port = ent->s_port;
5640 #ifndef __CYGWIN32__
5641  endservent ();
5642 #endif
5643  }
5644  }
5645 
5646  /* If we're faking a relay agent, and we're not using loopback,
5647  we're using the server port, not the client port. */
5648  if (mockup_relay && giaddr.s_addr != htonl(INADDR_LOOPBACK)) {
5650  } else
5651  remote_port = htons(ntohs(local_port) - 1); /* XXX */
5652 }
5653 
5654 /*
5655  * The following routines are used to check that certain
5656  * strings are reasonable before we pass them to the scripts.
5657  * This avoids some problems with scripts treating the strings
5658  * as commands - see ticket 23722
5659  * The domain checking code should be done as part of assembling
5660  * the string but we are doing it here for now due to time
5661  * constraints.
5662  */
5663 
5664 static int check_domain_name(const char *ptr, size_t len, int dots)
5665 {
5666  const char *p;
5667 
5668  /* not empty or complete length not over 255 characters */
5669  if ((len == 0) || (len > 256))
5670  return(-1);
5671 
5672  /* consists of [[:alnum:]-]+ labels separated by [.] */
5673  /* a [_] is against RFC but seems to be "widely used"... */
5674  for (p=ptr; (*p != 0) && (len-- > 0); p++) {
5675  if ((*p == '-') || (*p == '_')) {
5676  /* not allowed at begin or end of a label */
5677  if (((p - ptr) == 0) || (len == 0) || (p[1] == '.'))
5678  return(-1);
5679  } else if (*p == '.') {
5680  /* each label has to be 1-63 characters;
5681  we allow [.] at the end ('foo.bar.') */
5682  size_t d = p - ptr;
5683  if ((d <= 0) || (d >= 64))
5684  return(-1);
5685  ptr = p + 1; /* jump to the next label */
5686  if ((dots > 0) && (len > 0))
5687  dots--;
5688  } else if (isalnum((unsigned char)*p) == 0) {
5689  /* also numbers at the begin are fine */
5690  return(-1);
5691  }
5692  }
5693  return(dots ? -1 : 0);
5694 }
5695 
5696 static int check_domain_name_list(const char *ptr, size_t len, int dots)
5697 {
5698  const char *p;
5699  int ret = -1; /* at least one needed */
5700 
5701  if ((ptr == NULL) || (len == 0))
5702  return(-1);
5703 
5704  for (p=ptr; (*p != 0) && (len > 0); p++, len--) {
5705  if (*p != ' ')
5706  continue;
5707  if (p > ptr) {
5708  if (check_domain_name(ptr, p - ptr, dots) != 0)
5709  return(-1);
5710  ret = 0;
5711  }
5712  ptr = p + 1;
5713  }
5714  if (p > ptr)
5715  return(check_domain_name(ptr, p - ptr, dots));
5716  else
5717  return(ret);
5718 }
5719 
5720 static int check_option_values(struct universe *universe,
5721  unsigned int opt,
5722  const char *ptr,
5723  size_t len)
5724 {
5725  if (ptr == NULL)
5726  return(-1);
5727 
5728  /* just reject options we want to protect, will be escaped anyway */
5729  if ((universe == NULL) || (universe == &dhcp_universe)) {
5730  switch(opt) {
5731  case DHO_DOMAIN_NAME:
5732 #ifdef ACCEPT_LIST_IN_DOMAIN_NAME
5733  return check_domain_name_list(ptr, len, 0);
5734 #else
5735  return check_domain_name(ptr, len, 0);
5736 #endif
5737  case DHO_HOST_NAME:
5738  case DHO_NIS_DOMAIN:
5739  case DHO_NETBIOS_SCOPE:
5740  return check_domain_name(ptr, len, 0);
5741  break;
5742  case DHO_DOMAIN_SEARCH:
5743  return check_domain_name_list(ptr, len, 0);
5744  break;
5745  case DHO_ROOT_PATH:
5746  if (len == 0)
5747  return(-1);
5748  for (; (*ptr != 0) && (len-- > 0); ptr++) {
5749  if(!(isalnum((unsigned char)*ptr) ||
5750  *ptr == '#' || *ptr == '%' ||
5751  *ptr == '+' || *ptr == '-' ||
5752  *ptr == '_' || *ptr == ':' ||
5753  *ptr == '.' || *ptr == ',' ||
5754  *ptr == '@' || *ptr == '~' ||
5755  *ptr == '\\' || *ptr == '/' ||
5756  *ptr == '[' || *ptr == ']' ||
5757  *ptr == '=' || *ptr == ' '))
5758  return(-1);
5759  }
5760  return(0);
5761  break;
5762  }
5763  }
5764 
5765 #ifdef DHCPv6
5766  if (universe == &dhcpv6_universe) {
5767  switch(opt) {
5768  case D6O_SIP_SERVERS_DNS:
5769  case D6O_DOMAIN_SEARCH:
5770  case D6O_NIS_DOMAIN_NAME:
5771  case D6O_NISP_DOMAIN_NAME:
5772  return check_domain_name_list(ptr, len, 0);
5773  break;
5774  }
5775  }
5776 #endif
5777 
5778  return(0);
5779 }
5780 
5781 static void
5782 add_reject(struct packet *packet) {
5783  struct iaddrmatchlist *list;
5784 
5785  list = dmalloc(sizeof(struct iaddrmatchlist), MDL);
5786  if (!list)
5787  log_fatal ("no memory for reject list!");
5788 
5789  /*
5790  * client_addr is misleading - it is set to source address in common
5791  * code.
5792  */
5793  list->match.addr = packet->client_addr;
5794  /* Set mask to indicate host address. */
5795  list->match.mask.len = list->match.addr.len;
5796  memset(list->match.mask.iabuf, 0xff, sizeof(list->match.mask.iabuf));
5797 
5798  /* Append to reject list for the source interface. */
5801 
5802  /*
5803  * We should inform user that we won't be accepting this server
5804  * anymore.
5805  */
5806  log_info("Server added to list of rejected servers.");
5807 }
5808 
5809 /* Wrapper function around common ddns_cb_free function that ensures
5810  * we set the client_state pointer to the control block to NULL. */
5811 static void
5812 dhclient_ddns_cb_free(dhcp_ddns_cb_t *ddns_cb, char* file, int line) {
5813  if (ddns_cb) {
5814  struct client_state *client = (struct client_state *)ddns_cb->lease;
5815  if (client != NULL) {
5816  client->ddns_cb = NULL;
5817  }
5818 
5820  }
5821 }
5822 
5823 #if defined(DHCPv6) && defined(DHCP4o6)
5824 /*
5825  * \brief Omapi I/O handler
5826  *
5827  * The inter-process communication receive handler.
5828  *
5829  * On the DHCPv6 side, the message is either a POLL (which is answered
5830  * by a START or a STOP) or a DHCPv4-QUERY (which is forwarded to
5831  * DHCPv4 over DHCPv6 servers by forw_dhcpv4_query()).
5832  *
5833  * On the DHCPv4 side, the message is either a START, a STOP
5834  * (both for the DHCP4 over DHCPv6 state machine) or a DHCPv4-RESPONSE
5835  * (which is processed by recv_dhcpv4_response()).
5836  *
5837  * \param h the OMAPI object
5838  * \return a result for I/O success or error (used by the I/O subsystem)
5839  */
5840 isc_result_t dhcpv4o6_handler(omapi_object_t *h) {
5841  char buf[65536];
5842  char start_msg[5] = { 'S', 'T', 'A', 'R', 'T' };
5843  char stop_msg[4] = { 'S', 'T', 'O', 'P' };
5844  char poll_msg[4] = { 'P', 'O', 'L', 'L' };
5845  struct data_string raw;
5846  int cc;
5847 
5848  if (h->type != dhcp4o6_type)
5849  return DHCP_R_INVALIDARG;
5850 
5851  cc = recv(dhcp4o6_fd, buf, sizeof(buf), 0);
5852  if (cc <= 0)
5853  return ISC_R_UNEXPECTED;
5854 
5855  if (local_family == AF_INET6) {
5856  if ((cc == 4) &&
5857  (memcmp(buf, poll_msg, sizeof(poll_msg)) == 0)) {
5858  log_info("RCV: POLL");
5859  if (dhcp4o6_state < 0)
5860  cc = send(dhcp4o6_fd, stop_msg,
5861  sizeof(stop_msg), 0);
5862  else
5863  cc = send(dhcp4o6_fd, start_msg,
5864  sizeof(start_msg), 0);
5865  if (cc < 0) {
5866  log_error("dhcpv4o6_handler: send(): %m");
5867  return ISC_R_IOERROR;
5868  }
5869  } else {
5870  if (cc < DHCP_FIXED_NON_UDP + 8)
5871  return ISC_R_UNEXPECTED;
5872  memset(&raw, 0, sizeof(raw));
5873  if (!buffer_allocate(&raw.buffer, cc, MDL)) {
5874  log_error("dhcpv4o6_handler: "
5875  "no memory buffer.");
5876  return ISC_R_NOMEMORY;
5877  }
5878  raw.data = raw.buffer->data;
5879  raw.len = cc;
5880  memcpy(raw.buffer->data, buf, cc);
5881 
5882  forw_dhcpv4_query(&raw);
5883 
5884  data_string_forget(&raw, MDL);
5885  }
5886  } else {
5887  if ((cc == 4) &&
5888  (memcmp(buf, stop_msg, sizeof(stop_msg)) == 0)) {
5889  log_info("RCV: STOP");
5890  if (dhcp4o6_state > 0) {
5891  dhcp4o6_state = 0;
5892  dhcp4o6_poll(NULL);
5893  }
5894  } else if ((cc == 5) &&
5895  (memcmp(buf, start_msg, sizeof(start_msg)) == 0)) {
5896  log_info("RCV: START");
5897  if (dhcp4o6_state == 0)
5898  cancel_timeout(dhcp4o6_poll, NULL);
5899  dhcp4o6_state = 1;
5900  dhcp4o6_resume();
5901  } else {
5902  if (cc < DHCP_FIXED_NON_UDP + 16)
5903  return ISC_R_UNEXPECTED;
5904  memset(&raw, 0, sizeof(raw));
5905  if (!buffer_allocate(&raw.buffer, cc, MDL)) {
5906  log_error("dhcpv4o6_handler: "
5907  "no memory buffer.");
5908  return ISC_R_NOMEMORY;
5909  }
5910  raw.data = raw.buffer->data;
5911  raw.len = cc;
5912  memcpy(raw.buffer->data, buf, cc);
5913 
5914  recv_dhcpv4_response(&raw);
5915 
5916  data_string_forget(&raw, MDL);
5917  }
5918  }
5919 
5920  return ISC_R_SUCCESS;
5921 }
5922 
5923 /*
5924  * \brief Poll the DHCPv6 client
5925  * (DHCPv4 client function)
5926  *
5927  * A POLL message is sent to the DHCPv6 client periodically to check
5928  * if the DHCPv6 is ready (i.e., has a valid DHCPv4-over-DHCPv6 server
5929  * address option).
5930  */
5931 static void dhcp4o6_poll(void *dummy) {
5932  char msg[4] = { 'P', 'O', 'L', 'L' };
5933  struct timeval tv;
5934  int cc;
5935 
5936  IGNORE_UNUSED(dummy);
5937 
5938  if (dhcp4o6_state < 0)
5939  dhcp4o6_state = 0;
5940 
5941  log_info("POLL");
5942 
5943  cc = send(dhcp4o6_fd, msg, sizeof(msg), 0);
5944  if (cc < 0)
5945  log_error("dhcp4o6_poll: send(): %m");
5946 
5947  tv.tv_sec = cur_time + 60;
5948  tv.tv_usec = random() % 1000000;
5949 
5950  add_timeout(&tv, dhcp4o6_poll, NULL, 0, 0);
5951 }
5952 
5953 /*
5954  * \brief Resume pending operations
5955  * (DHCPv4 client function)
5956  *
5957  * A START message was received from the DHCPv6 client so pending
5958  * operations (RELEASE or REBOOT) must be resumed.
5959  */
5960 static void dhcp4o6_resume() {
5961  struct interface_info *ip;
5962  struct client_state *client;
5963 
5964  for (ip = interfaces; ip != NULL; ip = ip->next) {
5965  for (client = ip->client; client != NULL;
5966  client = client->next) {
5967  if (client->pending == P_RELEASE)
5968  do_release(client);
5969  else if (client->pending == P_REBOOT)
5970  state_reboot(client);
5971  }
5972  }
5973 }
5974 
5975 /*
5976  * \brief Send a START to the DHCPv4 client
5977  * (DHCPv6 client function)
5978  *
5979  * First check if there is a valid DHCPv4-over-DHCPv6 server address option,
5980  * and when found go UP and on a transition from another state send
5981  * a START message to the DHCPv4 client.
5982  */
5983 void dhcp4o6_start() {
5984  struct interface_info *ip;
5985  struct client_state *client;
5986  struct dhc6_lease *lease;
5987  struct option_cache *oc;
5988  struct data_string addrs;
5989  char msg[5] = { 'S', 'T', 'A', 'R', 'T' };
5990  int cc;
5991 
5992  memset(&addrs, 0, sizeof(addrs));
5993  for (ip = interfaces; ip != NULL; ip = ip->next) {
5994  for (client = ip->client; client != NULL;
5995  client = client->next) {
5996  if ((client->state != S_BOUND) &&
5997  (client->state != S_RENEWING) &&
5998  (client->state != S_REBINDING))
5999  continue;
6000  lease = client->active_lease;
6001  if ((lease == NULL) || lease->released)
6002  continue;
6004  lease->options,
6006  if ((oc == NULL) ||
6007  !evaluate_option_cache(&addrs, NULL, NULL, NULL,
6008  lease->options, NULL,
6009  &global_scope, oc, MDL))
6010  continue;
6011  if ((addrs.len % 16) != 0) {
6012  data_string_forget(&addrs, MDL);
6013  continue;
6014  }
6015  data_string_forget(&addrs, MDL);
6016  goto found;
6017  }
6018  }
6019  log_info("dhcp4o6_start: failed");
6020  dhcp4o6_stop();
6021  return;
6022 
6023 found:
6024  if (dhcp4o6_state == 1)
6025  return;
6026  log_info("dhcp4o6_start: go to UP");
6027  dhcp4o6_state = 1;
6028 
6029  cc = send(dhcp4o6_fd, msg, sizeof(msg), 0);
6030  if (cc < 0)
6031  log_info("dhcp4o6_start: send(): %m");
6032 }
6033 
6034 /*
6035  * Send a STOP to the DHCPv4 client
6036  * (DHCPv6 client function)
6037  *
6038  * Go DOWN and on a transition from another state send a STOP message
6039  * to the DHCPv4 client.
6040  */
6041 static void dhcp4o6_stop() {
6042  char msg[4] = { 'S', 'T', 'O', 'P' };
6043  int cc;
6044 
6045  if (dhcp4o6_state == -1)
6046  return;
6047 
6048  log_info("dhcp4o6_stop: go to DOWN");
6049  dhcp4o6_state = -1;
6050 
6051  cc = send(dhcp4o6_fd, msg, sizeof(msg), 0);
6052  if (cc < 0)
6053  log_error("dhcp4o6_stop: send(): %m");
6054 }
6055 #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:2671
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:3723
u_int8_t plen
Definition: dhcpd.h:1132
struct binding_scope * global_scope
Definition: tree.c:38
struct dns_zone * zone
Definition: dhcpd.h:1785
#define _PATH_DHCLIENT_PID
Definition: config.h:250
struct universe * universe
Definition: tree.h:349
int interfaces_requested
Definition: dhclient.c: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:3394
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:4951
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:3830
#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:2334
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:2592
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:3117
#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:3827
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:2469
#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:4690
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:4290
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:1024
FILE * scriptFile
Definition: dhclient.c:4432
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:5141
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:2014
#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:3156
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:4770
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:4431
#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:4890
#define D6O_CLIENTID
Definition: dhcp6.h:30
void state_panic(void *cpp)
Definition: dhclient.c:2801
#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:4016
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:3609
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:5079
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:4477
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:2003
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:4446
#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:3699
#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:1844
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:3963
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:1997
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:2918
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:5036
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:3948
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:3816
void db_startup(int testp)
Definition: dhclient.c:2009
int parse_agent_information_option(struct packet *packet, int len, u_int8_t *data)
Definition: dhclient.c:5131
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:3544
int leases_written
Definition: dhclient.c:3828
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:1926
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:3759
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:4660
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:1992
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:5617
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:4808
#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:4848
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:2047
#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:3723
#define DHO_DHCP_CLIENT_IDENTIFIER
Definition: dhcp.h:153
struct dhcp_ddns_cb * ddns_cb
Definition: dhcpd.h:1332
void putUShort(unsigned char *, u_int32_t)
Definition: convert.c:86
TIME default_lease_time
Definition: dhclient.c: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:5228
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:3892
#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:5029
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:4537
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:4917
void state_stop(void *cpp)
Definition: dhclient.c:1968
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:4136
int bootp_broadcast_always
Definition: dhcpd.h:1259