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