ISC DHCP  4.3.3
A reference DHCPv4 and DHCPv6 implementation
dhclient.c
Go to the documentation of this file.
1 /* dhclient.c
2 
3  DHCP Client. */
4 
5 /*
6  * Copyright (c) 2004-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  /* Discover all the network interfaces. */
696 
697  /* Parse the dhclient.conf file. */
699 
700  /* Stateless special case. */
701  if (stateless) {
702  if (release_mode || (wanted_ia_na > 0) ||
704  (interfaces_requested != 1)) {
705  usage();
706  }
707  run_stateless(exit_mode);
708  return 0;
709  }
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  struct interface_info *ip;
1173 
1174  if (!interfaces)
1175  usage();
1176 
1177  for (ip = interfaces; ip; ip = ip->next) {
1178  if ((interfaces_requested > 0) &&
1179  ((ip->flags & (INTERFACE_REQUESTED |
1180  INTERFACE_AUTOMATIC)) !=
1182  continue;
1183  script_init(ip->client, "PREINIT6", NULL);
1184  script_go(ip->client);
1185  }
1186 
1187  /* Discover the network interface. */
1189 
1190  /* Parse the lease database. */
1192 
1193  /* If desired parse the secondary lease database for a DUID */
1194  if ((default_duid.len == 0) && (path_dhclient_duid != NULL)) {
1195  read_client_duid();
1196  }
1197 
1198  /* Establish a default DUID. */
1199  if (default_duid.len == 0) {
1200  if (default_duid.buffer != NULL)
1202 
1203  if (form_duid(&default_duid, MDL) == ISC_R_SUCCESS &&
1204  duid_type == DUID_LLT)
1205  write_duid(&default_duid);
1206  }
1207 
1208  /* Start a configuration state machine. */
1209  for (client = interfaces->client ;
1210  client != NULL ;
1211  client = client->next) {
1212  if (exit_mode) {
1213  unconfigure6(client, "STOP6");
1214  continue;
1215  }
1216  start_info_request6(client);
1217  }
1218  if (exit_mode)
1219  return;
1220 
1221  /* Start up a listener for the object management API protocol. */
1222  if (top_level_config.omapi_port != -1) {
1223  listener = NULL;
1224  result = omapi_generic_new(&listener, MDL);
1225  if (result != ISC_R_SUCCESS)
1226  log_fatal("Can't allocate new generic object: %s\n",
1227  isc_result_totext(result));
1228  result = omapi_protocol_listen(listener,
1229  (unsigned)
1231  1);
1232  if (result != ISC_R_SUCCESS)
1233  log_fatal("Can't start OMAPI protocol: %s",
1234  isc_result_totext(result));
1235  }
1236 
1237  /* Set up the packet handler... */
1239 
1240 #if defined(DEBUG_MEMORY_LEAKAGE) || defined(DEBUG_MALLOC_POOL) || \
1241  defined(DEBUG_MEMORY_LEAKAGE_ON_EXIT)
1242  dmalloc_cutoff_generation = dmalloc_generation;
1243  dmalloc_longterm = dmalloc_outstanding;
1244  dmalloc_outstanding = 0;
1245 #endif
1246 
1247  /* If we're not supposed to wait before getting the address,
1248  don't. */
1249  if (nowait)
1250  go_daemon();
1251 
1252  /* If we're not going to daemonize, write the pid file
1253  now. */
1254  if (no_daemon || nowait)
1256 
1257  /* Start dispatching packets and timeouts... */
1258  dispatch();
1259 
1260 #endif /* DHCPv6 */
1261  return;
1262 }
1263 
1264 isc_result_t find_class (struct class **c,
1265  const char *s, const char *file, int line)
1266 {
1267  return 0;
1268 }
1269 
1271  struct packet *packet;
1272  struct lease *lease;
1273  struct collection *collection;
1274 {
1275  return 0;
1276 }
1277 
1278 void classify (packet, class)
1279  struct packet *packet;
1280  struct class *class;
1281 {
1282 }
1283 
1285  struct lease *lease;
1286 {
1287 }
1288 
1289 int find_subnet (struct subnet **sp,
1290  struct iaddr addr, const char *file, int line)
1291 {
1292  return 0;
1293 }
1294 
1295 static void setup_ib_interface(struct interface_info *ip)
1296 {
1297  struct group *g;
1298 
1299  /* Set the broadcast flag */
1301 
1302  /*
1303  * Find out if a dhcp-client-identifier option was specified either
1304  * in the config file or on the command line
1305  */
1306  for (g = ip->client->config->on_transmission; g != NULL; g = g->next) {
1307  if ((g->statements != NULL) &&
1308  (strcmp(g->statements->data.option->option->name,
1309  "dhcp-client-identifier") == 0)) {
1310  return;
1311  }
1312  }
1313 
1314  /* No client ID specified */
1315  //log_fatal("dhcp-client-identifier must be specified for InfiniBand");
1316 }
1317 
1318 /* Individual States:
1319  *
1320  * Each routine is called from the dhclient_state_machine() in one of
1321  * these conditions:
1322  * -> entering INIT state
1323  * -> recvpacket_flag == 0: timeout in this state
1324  * -> otherwise: received a packet in this state
1325  *
1326  * Return conditions as handled by dhclient_state_machine():
1327  * Returns 1, sendpacket_flag = 1: send packet, reset timer.
1328  * Returns 1, sendpacket_flag = 0: just reset the timer (wait for a milestone).
1329  * Returns 0: finish the nap which was interrupted for no good reason.
1330  *
1331  * Several per-interface variables are used to keep track of the process:
1332  * active_lease: the lease that is being used on the interface
1333  * (null pointer if not configured yet).
1334  * offered_leases: leases corresponding to DHCPOFFER messages that have
1335  * been sent to us by DHCP servers.
1336  * acked_leases: leases corresponding to DHCPACK messages that have been
1337  * sent to us by DHCP servers.
1338  * sendpacket: DHCP packet we're trying to send.
1339  * destination: IP address to send sendpacket to
1340  * In addition, there are several relevant per-lease variables.
1341  * T1_expiry, T2_expiry, lease_expiry: lease milestones
1342  * In the active lease, these control the process of renewing the lease;
1343  * In leases on the acked_leases list, this simply determines when we
1344  * can no longer legitimately use the lease.
1345  */
1346 
1347 void state_reboot (cpp)
1348  void *cpp;
1349 {
1350  struct client_state *client = cpp;
1351 
1352  /* If we don't remember an active lease, go straight to INIT. */
1353  if (!client -> active ||
1354  client -> active -> is_bootp ||
1355  client -> active -> expiry <= cur_time) {
1356  state_init (client);
1357  return;
1358  }
1359 
1360  /* We are in the rebooting state. */
1361  client -> state = S_REBOOTING;
1362 
1363  /*
1364  * make_request doesn't initialize xid because it normally comes
1365  * from the DHCPDISCOVER, but we haven't sent a DHCPDISCOVER,
1366  * so pick an xid now.
1367  */
1368  client -> xid = random ();
1369 
1370  /*
1371  * Make a DHCPREQUEST packet, and set
1372  * appropriate per-interface flags.
1373  */
1374  make_request (client, client -> active);
1375  client -> destination = iaddr_broadcast;
1376  client -> first_sending = cur_time;
1377  client -> interval = client -> config -> initial_interval;
1378 
1379  /* Zap the medium list... */
1380  client -> medium = NULL;
1381 
1382  /* Send out the first DHCPREQUEST packet. */
1383  send_request (client);
1384 }
1385 
1386 /* Called when a lease has completely expired and we've been unable to
1387  renew it. */
1388 
1389 void state_init (cpp)
1390  void *cpp;
1391 {
1392  struct client_state *client = cpp;
1393  enum dhcp_state init_state = client->state;
1394  struct timeval tv;
1395 
1396  ASSERT_STATE(state, S_INIT);
1397 
1398  /* Make a DHCPDISCOVER packet, and set appropriate per-interface
1399  flags. */
1400  make_discover (client, client -> active);
1401  client -> xid = client -> packet.xid;
1402  client -> destination = iaddr_broadcast;
1403  client -> state = S_SELECTING;
1404  client -> first_sending = cur_time;
1405  client -> interval = client -> config -> initial_interval;
1406 
1407  if (init_state != S_DECLINED) {
1408  /* Add an immediate timeout to cause the first DHCPDISCOVER packet
1409  to go out. */
1410  send_discover(client);
1411  } else {
1412  /* We've received an OFFER and it has been DECLINEd by dhclient-script.
1413  * wait for a random time between 1 and backoff_cutoff seconds before
1414  * trying again. */
1415  tv . tv_sec = cur_time + ((1 + (random() >> 2)) % client->config->backoff_cutoff);
1416  tv . tv_usec = 0;
1417  add_timeout(&tv, send_discover, client, 0, 0);
1418  }
1419 }
1420 
1421 /*
1422  * state_selecting is called when one or more DHCPOFFER packets have been
1423  * received and a configurable period of time has passed.
1424  */
1425 
1427  void *cpp;
1428 {
1429  struct client_state *client = cpp;
1430  struct client_lease *lp, *next, *picked;
1431 
1432 
1433  ASSERT_STATE(state, S_SELECTING);
1434 
1435  /*
1436  * Cancel state_selecting and send_discover timeouts, since either
1437  * one could have got us here.
1438  */
1439  cancel_timeout (state_selecting, client);
1440  cancel_timeout (send_discover, client);
1441 
1442  /*
1443  * We have received one or more DHCPOFFER packets. Currently,
1444  * the only criterion by which we judge leases is whether or
1445  * not we get a response when we arp for them.
1446  */
1447  picked = NULL;
1448  for (lp = client -> offered_leases; lp; lp = next) {
1449  next = lp -> next;
1450 
1451  /*
1452  * Check to see if we got an ARPREPLY for the address
1453  * in this particular lease.
1454  */
1455  if (!picked) {
1456  picked = lp;
1457  picked -> next = NULL;
1458  } else {
1459  destroy_client_lease (lp);
1460  }
1461  }
1462  client -> offered_leases = NULL;
1463 
1464  /*
1465  * If we just tossed all the leases we were offered, go back
1466  * to square one.
1467  */
1468  if (!picked) {
1469  client -> state = S_INIT;
1470  state_init (client);
1471  return;
1472  }
1473 
1474  /* If it was a BOOTREPLY, we can just take the address right now. */
1475  if (picked -> is_bootp) {
1476  client -> new = picked;
1477 
1478  /* Make up some lease expiry times
1479  XXX these should be configurable. */
1480  client -> new -> expiry = cur_time + 12000;
1481  client -> new -> renewal += cur_time + 8000;
1482  client -> new -> rebind += cur_time + 10000;
1483 
1484  client -> state = S_REQUESTING;
1485 
1486  /* Bind to the address we received. */
1487  bind_lease (client);
1488  return;
1489  }
1490 
1491  /* Go to the REQUESTING state. */
1492  client -> destination = iaddr_broadcast;
1493  client -> state = S_REQUESTING;
1494  client -> first_sending = cur_time;
1495  client -> interval = client -> config -> initial_interval;
1496 
1497  /* Make a DHCPREQUEST packet from the lease we picked. */
1498  make_request (client, picked);
1499  client -> xid = client -> packet.xid;
1500 
1501  /* Toss the lease we picked - we'll get it back in a DHCPACK. */
1502  destroy_client_lease (picked);
1503 
1504  /* Add an immediate timeout to send the first DHCPREQUEST packet. */
1505  send_request (client);
1506 }
1507 
1508 /* state_requesting is called when we receive a DHCPACK message after
1509  having sent out one or more DHCPREQUEST packets. */
1510 
1512  struct packet *packet;
1513 {
1514  struct interface_info *ip = packet -> interface;
1515  struct client_state *client;
1516  struct client_lease *lease;
1517  struct option_cache *oc;
1518  struct data_string ds;
1519 
1520  /* If we're not receptive to an offer right now, or if the offer
1521  has an unrecognizable transaction id, then just drop it. */
1522  for (client = ip -> client; client; client = client -> next) {
1523  if (client -> xid == packet -> raw -> xid)
1524  break;
1525  }
1526  if (!client ||
1527  (packet -> interface -> hw_address.hlen - 1 !=
1528  packet -> raw -> hlen) ||
1529  (memcmp (&packet -> interface -> hw_address.hbuf [1],
1530  packet -> raw -> chaddr, packet -> raw -> hlen))) {
1531 #if defined (DEBUG)
1532  log_debug ("DHCPACK in wrong transaction.");
1533 #endif
1534  return;
1535  }
1536 
1537  if (client -> state != S_REBOOTING &&
1538  client -> state != S_REQUESTING &&
1539  client -> state != S_RENEWING &&
1540  client -> state != S_REBINDING) {
1541 #if defined (DEBUG)
1542  log_debug ("DHCPACK in wrong state.");
1543 #endif
1544  return;
1545  }
1546 
1547  log_info ("DHCPACK from %s (xid=0x%x)", piaddr (packet -> client_addr), ntohl(client -> xid));
1548 
1549  lease = packet_to_lease (packet, client);
1550  if (!lease) {
1551  log_info ("packet_to_lease failed.");
1552  return;
1553  }
1554 
1555  client -> new = lease;
1556 
1557  /* Stop resending DHCPREQUEST. */
1558  cancel_timeout (send_request, client);
1559 
1560  /* Figure out the lease time. */
1561  oc = lookup_option (&dhcp_universe, client -> new -> options,
1563  memset (&ds, 0, sizeof ds);
1564  if (oc &&
1565  evaluate_option_cache (&ds, packet, (struct lease *)0, client,
1566  packet -> options, client -> new -> options,
1567  &global_scope, oc, MDL)) {
1568  if (ds.len > 3)
1569  client -> new -> expiry = getULong (ds.data);
1570  else
1571  client -> new -> expiry = 0;
1572  data_string_forget (&ds, MDL);
1573  } else
1574  client -> new -> expiry = 0;
1575 
1576  if (client->new->expiry == 0) {
1577  struct timeval tv;
1578 
1579  log_error ("no expiry time on offered lease.");
1580 
1581  /* Quench this (broken) server. Return to INIT to reselect. */
1582  add_reject(packet);
1583 
1584  /* 1/2 second delay to restart at INIT. */
1585  tv.tv_sec = cur_tv.tv_sec;
1586  tv.tv_usec = cur_tv.tv_usec + 500000;
1587 
1588  if (tv.tv_usec >= 1000000) {
1589  tv.tv_sec++;
1590  tv.tv_usec -= 1000000;
1591  }
1592 
1593  add_timeout(&tv, state_init, client, 0, 0);
1594  return;
1595  }
1596 
1597  /*
1598  * A number that looks negative here is really just very large,
1599  * because the lease expiry offset is unsigned.
1600  */
1601  if (client->new->expiry < 0)
1602  client->new->expiry = TIME_MAX;
1603 
1604  /* Take the server-provided renewal time if there is one. */
1605  oc = lookup_option (&dhcp_universe, client -> new -> options,
1607  if (oc &&
1608  evaluate_option_cache (&ds, packet, (struct lease *)0, client,
1609  packet -> options, client -> new -> options,
1610  &global_scope, oc, MDL)) {
1611  if (ds.len > 3)
1612  client -> new -> renewal = getULong (ds.data);
1613  else
1614  client -> new -> renewal = 0;
1615  data_string_forget (&ds, MDL);
1616  } else
1617  client -> new -> renewal = 0;
1618 
1619  /* If it wasn't specified by the server, calculate it. */
1620  if (!client -> new -> renewal)
1621  client -> new -> renewal = client -> new -> expiry / 2 + 1;
1622 
1623  if (client -> new -> renewal <= 0)
1624  client -> new -> renewal = TIME_MAX;
1625 
1626  /* Now introduce some randomness to the renewal time: */
1627  if (client->new->renewal <= ((TIME_MAX / 3) - 3))
1628  client->new->renewal = (((client->new->renewal * 3) + 3) / 4) +
1629  (((random() % client->new->renewal) + 3) / 4);
1630 
1631  /* Same deal with the rebind time. */
1632  oc = lookup_option (&dhcp_universe, client -> new -> options,
1634  if (oc &&
1635  evaluate_option_cache (&ds, packet, (struct lease *)0, client,
1636  packet -> options, client -> new -> options,
1637  &global_scope, oc, MDL)) {
1638  if (ds.len > 3)
1639  client -> new -> rebind = getULong (ds.data);
1640  else
1641  client -> new -> rebind = 0;
1642  data_string_forget (&ds, MDL);
1643  } else
1644  client -> new -> rebind = 0;
1645 
1646  if (client -> new -> rebind <= 0) {
1647  if (client -> new -> expiry <= TIME_MAX / 7)
1648  client -> new -> rebind =
1649  client -> new -> expiry * 7 / 8;
1650  else
1651  client -> new -> rebind =
1652  client -> new -> expiry / 8 * 7;
1653  }
1654 
1655  /* Make sure our randomness didn't run the renewal time past the
1656  rebind time. */
1657  if (client -> new -> renewal > client -> new -> rebind) {
1658  if (client -> new -> rebind <= TIME_MAX / 3)
1659  client -> new -> renewal =
1660  client -> new -> rebind * 3 / 4;
1661  else
1662  client -> new -> renewal =
1663  client -> new -> rebind / 4 * 3;
1664  }
1665 
1666  client -> new -> expiry += cur_time;
1667  /* Lease lengths can never be negative. */
1668  if (client -> new -> expiry < cur_time)
1669  client -> new -> expiry = TIME_MAX;
1670  client -> new -> renewal += cur_time;
1671  if (client -> new -> renewal < cur_time)
1672  client -> new -> renewal = TIME_MAX;
1673  client -> new -> rebind += cur_time;
1674  if (client -> new -> rebind < cur_time)
1675  client -> new -> rebind = TIME_MAX;
1676 
1677  bind_lease (client);
1678 }
1679 
1680 void bind_lease (client)
1681  struct client_state *client;
1682 {
1683  struct timeval tv;
1684 
1685  /* Remember the medium. */
1686  client->new->medium = client->medium;
1687 
1688  /* Run the client script with the new parameters. */
1689  script_init(client, (client->state == S_REQUESTING ? "BOUND" :
1690  (client->state == S_RENEWING ? "RENEW" :
1691  (client->state == S_REBOOTING ? "REBOOT" :
1692  "REBIND"))),
1693  client->new->medium);
1694  if (client->active && client->state != S_REBOOTING)
1695  script_write_params(client, "old_", client->active);
1696  script_write_params (client, "new_", client->new);
1697  script_write_requested(client);
1698  if (client->alias)
1699  script_write_params(client, "alias_", client->alias);
1700 
1701  /* If the BOUND/RENEW code detects another machine using the
1702  offered address, it exits nonzero. We need to send a
1703  DHCPDECLINE and toss the lease. */
1704  if (script_go(client)) {
1705  make_decline(client, client->new);
1706  send_decline(client);
1707  destroy_client_lease(client->new);
1708  client->new = NULL;
1709  if (onetry) {
1710  if (!quiet)
1711  log_info("Unable to obtain a lease on first "
1712  "try (declined). Exiting.");
1713  exit(2);
1714  } else {
1715  client -> state = S_DECLINED;
1716  state_init(client);
1717  return;
1718  }
1719  }
1720 
1721  /* Write out the new lease if it has been long enough. */
1722  if (!client->last_write ||
1723  (cur_time - client->last_write) >= MIN_LEASE_WRITE)
1724  write_client_lease(client, client->new, 0, 1);
1725 
1726  /* Replace the old active lease with the new one. */
1727  if (client->active)
1728  destroy_client_lease(client->active);
1729  client->active = client->new;
1730  client->new = NULL;
1731 
1732  /* Set up a timeout to start the renewal process. */
1733  tv.tv_sec = client->active->renewal;
1734  tv.tv_usec = ((client->active->renewal - cur_tv.tv_sec) > 1) ?
1735  random() % 1000000 : cur_tv.tv_usec;
1736  add_timeout(&tv, state_bound, client, 0, 0);
1737 
1738  log_info("bound to %s -- renewal in %ld seconds.",
1739  piaddr(client->active->address),
1740  (long)(client->active->renewal - cur_time));
1741  client->state = S_BOUND;
1743  go_daemon();
1744 #if defined (NSUPDATE)
1745  if (client->config->do_forward_update)
1746  dhclient_schedule_updates(client, &client->active->address, 1);
1747 #endif
1748 }
1749 
1750 /* state_bound is called when we've successfully bound to a particular
1751  lease, but the renewal time on that lease has expired. We are
1752  expected to unicast a DHCPREQUEST to the server that gave us our
1753  original lease. */
1754 
1755 void state_bound (cpp)
1756  void *cpp;
1757 {
1758  struct client_state *client = cpp;
1759  struct option_cache *oc;
1760  struct data_string ds;
1761 
1762  ASSERT_STATE(state, S_BOUND);
1763 
1764  /* T1 has expired. */
1765  make_request (client, client -> active);
1766  client -> xid = client -> packet.xid;
1767 
1768  memset (&ds, 0, sizeof ds);
1769  oc = lookup_option (&dhcp_universe, client -> active -> options,
1771  if (oc &&
1772  evaluate_option_cache (&ds, (struct packet *)0, (struct lease *)0,
1773  client, (struct option_state *)0,
1774  client -> active -> options,
1775  &global_scope, oc, MDL)) {
1776  if (ds.len > 3) {
1777  memcpy (client -> destination.iabuf, ds.data, 4);
1778  client -> destination.len = 4;
1779  } else
1780  client -> destination = iaddr_broadcast;
1781 
1782  data_string_forget (&ds, MDL);
1783  } else
1784  client -> destination = iaddr_broadcast;
1785 
1786  client -> first_sending = cur_time;
1787  client -> interval = client -> config -> initial_interval;
1788  client -> state = S_RENEWING;
1789 
1790  /* Send the first packet immediately. */
1791  send_request (client);
1792 }
1793 
1794 /* state_stop is called when we've been told to shut down. We unconfigure
1795  the interfaces, and then stop operating until told otherwise. */
1796 
1797 void state_stop (cpp)
1798  void *cpp;
1799 {
1800  struct client_state *client = cpp;
1801 
1802  /* Cancel all timeouts. */
1804  cancel_timeout(send_discover, client);
1805  cancel_timeout(send_request, client);
1806  cancel_timeout(state_bound, client);
1807 
1808  /* If we have an address, unconfigure it. */
1809  if (client->active) {
1810  script_init(client, "STOP", client->active->medium);
1811  script_write_params(client, "old_", client->active);
1812  script_write_requested(client);
1813  if (client->alias)
1814  script_write_params(client, "alias_", client->alias);
1815  script_go(client);
1816  }
1817 }
1818 
1820 {
1821  return 0;
1822 }
1823 
1825  struct lease *lease;
1826 {
1827  return 0;
1828 }
1829 
1830 int write_host (host)
1831  struct host_decl *host;
1832 {
1833  return 0;
1834 }
1835 
1836 void db_startup (testp)
1837  int testp;
1838 {
1839 }
1840 
1842  struct packet *packet;
1843 {
1844  struct iaddrmatchlist *ap;
1845  char addrbuf[4*16];
1846  char maskbuf[4*16];
1847 
1848  if (packet -> raw -> op != BOOTREPLY)
1849  return;
1850 
1851  /* If there's a reject list, make sure this packet's sender isn't
1852  on it. */
1853  for (ap = packet -> interface -> client -> config -> reject_list;
1854  ap; ap = ap -> next) {
1855  if (addr_match(&packet->client_addr, &ap->match)) {
1856 
1857  /* piaddr() returns its result in a static
1858  buffer sized 4*16 (see common/inet.c). */
1859 
1860  strcpy(addrbuf, piaddr(ap->match.addr));
1861  strcpy(maskbuf, piaddr(ap->match.mask));
1862 
1863  log_info("BOOTREPLY from %s rejected by rule %s "
1864  "mask %s.", piaddr(packet->client_addr),
1865  addrbuf, maskbuf);
1866  return;
1867  }
1868  }
1869 
1870  dhcpoffer (packet);
1871 
1872 }
1873 
1874 void dhcp (packet)
1875  struct packet *packet;
1876 {
1877  struct iaddrmatchlist *ap;
1878  void (*handler) (struct packet *);
1879  const char *type;
1880  char addrbuf[4*16];
1881  char maskbuf[4*16];
1882 
1883  switch (packet -> packet_type) {
1884  case DHCPOFFER:
1885  handler = dhcpoffer;
1886  type = "DHCPOFFER";
1887  break;
1888 
1889  case DHCPNAK:
1890  handler = dhcpnak;
1891  type = "DHCPNACK";
1892  break;
1893 
1894  case DHCPACK:
1895  handler = dhcpack;
1896  type = "DHCPACK";
1897  break;
1898 
1899  default:
1900  return;
1901  }
1902 
1903  /* If there's a reject list, make sure this packet's sender isn't
1904  on it. */
1905  for (ap = packet -> interface -> client -> config -> reject_list;
1906  ap; ap = ap -> next) {
1907  if (addr_match(&packet->client_addr, &ap->match)) {
1908 
1909  /* piaddr() returns its result in a static
1910  buffer sized 4*16 (see common/inet.c). */
1911 
1912  strcpy(addrbuf, piaddr(ap->match.addr));
1913  strcpy(maskbuf, piaddr(ap->match.mask));
1914 
1915  log_info("%s from %s rejected by rule %s mask %s.",
1916  type, piaddr(packet->client_addr),
1917  addrbuf, maskbuf);
1918  return;
1919  }
1920  }
1921  (*handler) (packet);
1922 }
1923 
1924 #ifdef DHCPv6
1925 void
1926 dhcpv6(struct packet *packet) {
1927  struct iaddrmatchlist *ap;
1928  struct client_state *client;
1929  char addrbuf[sizeof("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")];
1930 
1931  /* Silently drop bogus messages. */
1932  if (packet->dhcpv6_msg_type >= dhcpv6_type_name_max)
1933  return;
1934 
1935  /* Discard, with log, packets from quenched sources. */
1936  for (ap = packet->interface->client->config->reject_list ;
1937  ap ; ap = ap->next) {
1938  if (addr_match(&packet->client_addr, &ap->match)) {
1939  strcpy(addrbuf, piaddr(packet->client_addr));
1940  log_info("%s from %s rejected by rule %s",
1942  addrbuf,
1943  piaddrmask(&ap->match.addr, &ap->match.mask));
1944  return;
1945  }
1946  }
1947 
1948  /* Screen out nonsensical messages. */
1949  switch(packet->dhcpv6_msg_type) {
1950  case DHCPV6_ADVERTISE:
1951  case DHCPV6_RECONFIGURE:
1952  if (stateless)
1953  return;
1954  /* Falls through */
1955  case DHCPV6_REPLY:
1956  log_info("RCV: %s message on %s from %s.",
1958  packet->interface->name, piaddr(packet->client_addr));
1959  break;
1960 
1961  default:
1962  return;
1963  }
1964 
1965  /* Find a client state that matches the incoming XID. */
1966  for (client = packet->interface->client ; client ;
1967  client = client->next) {
1968  if (memcmp(&client->dhcpv6_transaction_id,
1969  packet->dhcpv6_transaction_id, 3) == 0) {
1970  client->v6_handler(packet, client);
1971  return;
1972  }
1973  }
1974 
1975  /* XXX: temporary log for debugging */
1976  log_info("Packet received, but nothing done with it.");
1977 }
1978 #endif /* DHCPv6 */
1979 
1980 void dhcpoffer (packet)
1981  struct packet *packet;
1982 {
1983  struct interface_info *ip = packet -> interface;
1984  struct client_state *client;
1985  struct client_lease *lease, *lp;
1986  struct option **req;
1987  int i;
1988  int stop_selecting;
1989  const char *name = packet -> packet_type ? "DHCPOFFER" : "BOOTREPLY";
1990  char obuf [1024];
1991  struct timeval tv;
1992 
1993 #ifdef DEBUG_PACKET
1994  dump_packet (packet);
1995 #endif
1996 
1997  /* Find a client state that matches the xid... */
1998  for (client = ip -> client; client; client = client -> next)
1999  if (client -> xid == packet -> raw -> xid)
2000  break;
2001 
2002  /* If we're not receptive to an offer right now, or if the offer
2003  has an unrecognizable transaction id, then just drop it. */
2004  if (!client ||
2005  client -> state != S_SELECTING ||
2006  (packet -> interface -> hw_address.hlen - 1 !=
2007  packet -> raw -> hlen) ||
2008  (memcmp (&packet -> interface -> hw_address.hbuf [1],
2009  packet -> raw -> chaddr, packet -> raw -> hlen))) {
2010 #if defined (DEBUG)
2011  log_debug ("%s in wrong transaction.", name);
2012 #endif
2013  return;
2014  }
2015 
2016  sprintf (obuf, "%s from %s", name, piaddr (packet -> client_addr));
2017 
2018 
2019  /* If this lease doesn't supply the minimum required DHCPv4 parameters,
2020  * ignore it.
2021  */
2022  req = client->config->required_options;
2023  if (req != NULL) {
2024  for (i = 0 ; req[i] != NULL ; i++) {
2025  if ((req[i]->universe == &dhcp_universe) &&
2026  !lookup_option(&dhcp_universe, packet->options,
2027  req[i]->code)) {
2028  struct option *option = NULL;
2029  unsigned code = req[i]->code;
2030 
2031  option_code_hash_lookup(&option,
2033  &code, 0, MDL);
2034 
2035  if (option)
2036  log_info("%s: no %s option.", obuf,
2037  option->name);
2038  else
2039  log_info("%s: no unknown-%u option.",
2040  obuf, code);
2041 
2042  option_dereference(&option, MDL);
2043 
2044  return;
2045  }
2046  }
2047  }
2048 
2049  /* If we've already seen this lease, don't record it again. */
2050  for (lease = client -> offered_leases; lease; lease = lease -> next) {
2051  if (lease -> address.len == sizeof packet -> raw -> yiaddr &&
2052  !memcmp (lease -> address.iabuf,
2053  &packet -> raw -> yiaddr, lease -> address.len)) {
2054  log_debug ("%s: already seen.", obuf);
2055  return;
2056  }
2057  }
2058 
2059  lease = packet_to_lease (packet, client);
2060  if (!lease) {
2061  log_info ("%s: packet_to_lease failed.", obuf);
2062  return;
2063  }
2064 
2065  /* If this lease was acquired through a BOOTREPLY, record that
2066  fact. */
2067  if (!packet -> options_valid || !packet -> packet_type)
2068  lease -> is_bootp = 1;
2069 
2070  /* Record the medium under which this lease was offered. */
2071  lease -> medium = client -> medium;
2072 
2073  /* Figure out when we're supposed to stop selecting. */
2074  stop_selecting = (client -> first_sending +
2075  client -> config -> select_interval);
2076 
2077  /* If this is the lease we asked for, put it at the head of the
2078  list, and don't mess with the arp request timeout. */
2079  if (lease -> address.len == client -> requested_address.len &&
2080  !memcmp (lease -> address.iabuf,
2081  client -> requested_address.iabuf,
2082  client -> requested_address.len)) {
2083  lease -> next = client -> offered_leases;
2084  client -> offered_leases = lease;
2085  } else {
2086  /* Put the lease at the end of the list. */
2087  lease -> next = (struct client_lease *)0;
2088  if (!client -> offered_leases)
2089  client -> offered_leases = lease;
2090  else {
2091  for (lp = client -> offered_leases; lp -> next;
2092  lp = lp -> next)
2093  ;
2094  lp -> next = lease;
2095  }
2096  }
2097 
2098  /* If the selecting interval has expired, go immediately to
2099  state_selecting(). Otherwise, time out into
2100  state_selecting at the select interval. */
2101  if (stop_selecting <= cur_tv.tv_sec)
2102  state_selecting (client);
2103  else {
2104  tv.tv_sec = stop_selecting;
2105  tv.tv_usec = cur_tv.tv_usec;
2106  add_timeout(&tv, state_selecting, client, 0, 0);
2107  cancel_timeout(send_discover, client);
2108  }
2109  log_info("%s", obuf);
2110 }
2111 
2112 /* Allocate a client_lease structure and initialize it from the parameters
2113  in the specified packet. */
2114 
2115 struct client_lease *packet_to_lease (packet, client)
2116  struct packet *packet;
2117  struct client_state *client;
2118 {
2119  struct client_lease *lease;
2120  unsigned i;
2121  struct option_cache *oc;
2122  struct option *option = NULL;
2123  struct data_string data;
2124 
2125  lease = (struct client_lease *)new_client_lease (MDL);
2126 
2127  if (!lease) {
2128  log_error("packet_to_lease: no memory to record lease.\n");
2129  return NULL;
2130  }
2131 
2132  memset(lease, 0, sizeof(*lease));
2133 
2134  /* Copy the lease options. */
2135  option_state_reference(&lease->options, packet->options, MDL);
2136 
2137  lease->address.len = sizeof(packet->raw->yiaddr);
2138  memcpy(lease->address.iabuf, &packet->raw->yiaddr,
2139  lease->address.len);
2140 
2141  lease->next_srv_addr.len = sizeof(packet->raw->siaddr);
2142  memcpy(lease->next_srv_addr.iabuf, &packet->raw->siaddr,
2143  lease->next_srv_addr.len);
2144 
2145  memset(&data, 0, sizeof(data));
2146 
2147  if (client -> config -> vendor_space_name) {
2149 
2150  /* See if there was a vendor encapsulation option. */
2151  oc = lookup_option (&dhcp_universe, lease -> options, i);
2152  if (oc &&
2153  client -> config -> vendor_space_name &&
2154  evaluate_option_cache (&data, packet,
2155  (struct lease *)0, client,
2156  packet -> options, lease -> options,
2157  &global_scope, oc, MDL)) {
2158  if (data.len) {
2159  if (!option_code_hash_lookup(&option,
2161  &i, 0, MDL))
2162  log_fatal("Unable to find VENDOR "
2163  "option (%s:%d).", MDL);
2165  (packet -> options, option,
2166  data.data, data.len, &dhcp_universe,
2167  client -> config -> vendor_space_name
2168  );
2169 
2170  option_dereference(&option, MDL);
2171  }
2172  data_string_forget (&data, MDL);
2173  }
2174  } else
2175  i = 0;
2176 
2177  /* Figure out the overload flag. */
2178  oc = lookup_option (&dhcp_universe, lease -> options,
2180  if (oc &&
2181  evaluate_option_cache (&data, packet, (struct lease *)0, client,
2182  packet -> options, lease -> options,
2183  &global_scope, oc, MDL)) {
2184  if (data.len > 0)
2185  i = data.data [0];
2186  else
2187  i = 0;
2188  data_string_forget (&data, MDL);
2189  } else
2190  i = 0;
2191 
2192  /* If the server name was filled out, copy it. */
2193  if (!(i & 2) && packet -> raw -> sname [0]) {
2194  unsigned len;
2195  /* Don't count on the NUL terminator. */
2196  for (len = 0; len < DHCP_SNAME_LEN; len++)
2197  if (!packet -> raw -> sname [len])
2198  break;
2199  lease -> server_name = dmalloc (len + 1, MDL);
2200  if (!lease -> server_name) {
2201  log_error ("dhcpoffer: no memory for server name.\n");
2202  destroy_client_lease (lease);
2203  return (struct client_lease *)0;
2204  } else {
2205  memcpy (lease -> server_name,
2206  packet -> raw -> sname, len);
2207  lease -> server_name [len] = 0;
2208  }
2209  }
2210 
2211  /* Ditto for the filename. */
2212  if (!(i & 1) && packet -> raw -> file [0]) {
2213  unsigned len;
2214  /* Don't count on the NUL terminator. */
2215  for (len = 0; len < DHCP_FILE_LEN; len++)
2216  if (!packet -> raw -> file [len])
2217  break;
2218  lease -> filename = dmalloc (len + 1, MDL);
2219  if (!lease -> filename) {
2220  log_error ("dhcpoffer: no memory for filename.\n");
2221  destroy_client_lease (lease);
2222  return (struct client_lease *)0;
2223  } else {
2224  memcpy (lease -> filename,
2225  packet -> raw -> file, len);
2226  lease -> filename [len] = 0;
2227  }
2228  }
2229 
2230  execute_statements_in_scope(NULL, (struct packet *)packet, NULL,
2231  client, lease->options, lease->options,
2232  &global_scope, client->config->on_receipt,
2233  NULL, NULL);
2234 
2235  return lease;
2236 }
2237 
2238 void dhcpnak (packet)
2239  struct packet *packet;
2240 {
2241  struct interface_info *ip = packet -> interface;
2242  struct client_state *client;
2243 
2244  /* Find a client state that matches the xid... */
2245  for (client = ip -> client; client; client = client -> next)
2246  if (client -> xid == packet -> raw -> xid)
2247  break;
2248 
2249  /* If we're not receptive to an offer right now, or if the offer
2250  has an unrecognizable transaction id, then just drop it. */
2251  if (!client ||
2252  (packet -> interface -> hw_address.hlen - 1 !=
2253  packet -> raw -> hlen) ||
2254  (memcmp (&packet -> interface -> hw_address.hbuf [1],
2255  packet -> raw -> chaddr, packet -> raw -> hlen))) {
2256 #if defined (DEBUG)
2257  log_debug ("DHCPNAK in wrong transaction.");
2258 #endif
2259  return;
2260  }
2261 
2262  if (client -> state != S_REBOOTING &&
2263  client -> state != S_REQUESTING &&
2264  client -> state != S_RENEWING &&
2265  client -> state != S_REBINDING) {
2266 #if defined (DEBUG)
2267  log_debug ("DHCPNAK in wrong state.");
2268 #endif
2269  return;
2270  }
2271 
2272  log_info ("DHCPNAK from %s (xid=0x%x)", piaddr (packet -> client_addr), ntohl(client -> xid));
2273 
2274  if (!client -> active) {
2275 #if defined (DEBUG)
2276  log_info ("DHCPNAK with no active lease.\n");
2277 #endif
2278  return;
2279  }
2280 
2281  /* If we get a DHCPNAK, we use the EXPIRE dhclient-script state
2282  * to indicate that we want all old bindings to be removed. (It
2283  * is possible that we may get a NAK while in the RENEW state,
2284  * so we might have bindings active at that time)
2285  */
2286  script_init(client, "EXPIRE", NULL);
2287  script_write_params(client, "old_", client->active);
2288  script_write_requested(client);
2289  if (client->alias)
2290  script_write_params(client, "alias_", client->alias);
2291  script_go(client);
2292 
2293  destroy_client_lease (client -> active);
2294  client -> active = (struct client_lease *)0;
2295 
2296  /* Stop sending DHCPREQUEST packets... */
2297  cancel_timeout (send_request, client);
2298 
2299  /* On some scripts, 'EXPIRE' causes the interface to be ifconfig'd
2300  * down (this expunges any routes and arp cache). This makes the
2301  * interface unusable by state_init(), which we call next. So, we
2302  * need to 'PREINIT' the interface to bring it back up.
2303  */
2304  script_init(client, "PREINIT", NULL);
2305  if (client->alias)
2306  script_write_params(client, "alias_", client->alias);
2307  script_go(client);
2308 
2309  client -> state = S_INIT;
2310  state_init (client);
2311 }
2312 
2313 /* Send out a DHCPDISCOVER packet, and set a timeout to send out another
2314  one after the right interval has expired. If we don't get an offer by
2315  the time we reach the panic interval, call the panic function. */
2316 
2317 void send_discover (cpp)
2318  void *cpp;
2319 {
2320  struct client_state *client = cpp;
2321 
2322  int result;
2323  int interval;
2324  int increase = 1;
2325  struct timeval tv;
2326 
2327  /* Figure out how long it's been since we started transmitting. */
2328  interval = cur_time - client -> first_sending;
2329 
2330  /* If we're past the panic timeout, call the script and tell it
2331  we haven't found anything for this interface yet. */
2332  if (interval > client -> config -> timeout) {
2333  state_panic (client);
2334  return;
2335  }
2336 
2337  /* If we're selecting media, try the whole list before doing
2338  the exponential backoff, but if we've already received an
2339  offer, stop looping, because we obviously have it right. */
2340  if (!client -> offered_leases &&
2341  client -> config -> media) {
2342  int fail = 0;
2343  again:
2344  if (client -> medium) {
2345  client -> medium = client -> medium -> next;
2346  increase = 0;
2347  }
2348  if (!client -> medium) {
2349  if (fail)
2350  log_fatal ("No valid media types for %s!",
2351  client -> interface -> name);
2352  client -> medium =
2353  client -> config -> media;
2354  increase = 1;
2355  }
2356 
2357  log_info ("Trying medium \"%s\" %d",
2358  client -> medium -> string, increase);
2359  script_init (client, "MEDIUM", client -> medium);
2360  if (script_go (client)) {
2361  fail = 1;
2362  goto again;
2363  }
2364  }
2365 
2366  /* If we're supposed to increase the interval, do so. If it's
2367  currently zero (i.e., we haven't sent any packets yet), set
2368  it to initial_interval; otherwise, add to it a random number
2369  between zero and two times itself. On average, this means
2370  that it will double with every transmission. */
2371  if (increase) {
2372  if (!client->interval)
2373  client->interval = client->config->initial_interval;
2374  else
2375  client->interval += random() % (2 * client->interval);
2376 
2377  /* Don't backoff past cutoff. */
2378  if (client->interval > client->config->backoff_cutoff)
2379  client->interval = (client->config->backoff_cutoff / 2)
2380  + (random() % client->config->backoff_cutoff);
2381  } else if (!client->interval)
2382  client->interval = client->config->initial_interval;
2383 
2384  /* If the backoff would take us to the panic timeout, just use that
2385  as the interval. */
2386  if (cur_time + client -> interval >
2387  client -> first_sending + client -> config -> timeout)
2388  client -> interval =
2389  (client -> first_sending +
2390  client -> config -> timeout) - cur_time + 1;
2391 
2392  /* Record the number of seconds since we started sending. */
2393  if (interval < 65536)
2394  client -> packet.secs = htons (interval);
2395  else
2396  client -> packet.secs = htons (65535);
2397  client -> secs = client -> packet.secs;
2398 
2399  log_info ("DHCPDISCOVER on %s to %s port %d interval %ld (xid=0x%x)",
2400  client -> name ? client -> name : client -> interface -> name,
2401  inet_ntoa (sockaddr_broadcast.sin_addr),
2402  ntohs (sockaddr_broadcast.sin_port), (long)(client -> interval), ntohl(client -> xid));
2403 
2404  /* Send out a packet. */
2405  result = send_packet(client->interface, NULL, &client->packet,
2406  client->packet_length, inaddr_any,
2407  &sockaddr_broadcast, NULL);
2408  if (result < 0) {
2409  log_error("%s:%d: Failed to send %d byte long packet over %s "
2410  "interface.", MDL, client->packet_length,
2411  client->interface->name);
2412  }
2413 
2414  /*
2415  * If we used 0 microseconds here, and there were other clients on the
2416  * same network with a synchronized local clock (ntp), and a similar
2417  * zero-microsecond-scheduler behavior, then we could be participating
2418  * in a sub-second DOS ttck.
2419  */
2420  tv.tv_sec = cur_tv.tv_sec + client->interval;
2421  tv.tv_usec = client->interval > 1 ? random() % 1000000 : cur_tv.tv_usec;
2422  add_timeout(&tv, send_discover, client, 0, 0);
2423 }
2424 
2425 /* state_panic gets called if we haven't received any offers in a preset
2426  amount of time. When this happens, we try to use existing leases that
2427  haven't yet expired, and failing that, we call the client script and
2428  hope it can do something. */
2429 
2430 void state_panic (cpp)
2431  void *cpp;
2432 {
2433  struct client_state *client = cpp;
2434  struct client_lease *loop;
2435  struct client_lease *lp;
2436  struct timeval tv;
2437 
2438  loop = lp = client -> active;
2439 
2440  log_info ("No DHCPOFFERS received.");
2441 
2442  /* We may not have an active lease, but we may have some
2443  predefined leases that we can try. */
2444  if (!client -> active && client -> leases)
2445  goto activate_next;
2446 
2447  /* Run through the list of leases and see if one can be used. */
2448  while (client -> active) {
2449  if (client -> active -> expiry > cur_time) {
2450  log_info ("Trying recorded lease %s",
2451  piaddr (client -> active -> address));
2452  /* Run the client script with the existing
2453  parameters. */
2454  script_init (client, "TIMEOUT",
2455  client -> active -> medium);
2456  script_write_params (client, "new_", client -> active);
2457  script_write_requested(client);
2458  if (client -> alias)
2459  script_write_params (client, "alias_",
2460  client -> alias);
2461 
2462  /* If the old lease is still good and doesn't
2463  yet need renewal, go into BOUND state and
2464  timeout at the renewal time. */
2465  if (!script_go (client)) {
2466  if (cur_time < client -> active -> renewal) {
2467  client -> state = S_BOUND;
2468  log_info ("bound: renewal in %ld %s.",
2469  (long)(client -> active -> renewal -
2470  cur_time), "seconds");
2471  tv.tv_sec = client->active->renewal;
2472  tv.tv_usec = ((client->active->renewal -
2473  cur_time) > 1) ?
2474  random() % 1000000 :
2475  cur_tv.tv_usec;
2476  add_timeout(&tv, state_bound, client, 0, 0);
2477  } else {
2478  client -> state = S_BOUND;
2479  log_info ("bound: immediate renewal.");
2480  state_bound (client);
2481  }
2483  go_daemon ();
2484  return;
2485  }
2486  }
2487 
2488  /* If there are no other leases, give up. */
2489  if (!client -> leases) {
2490  client -> leases = client -> active;
2491  client -> active = (struct client_lease *)0;
2492  break;
2493  }
2494 
2495  activate_next:
2496  /* Otherwise, put the active lease at the end of the
2497  lease list, and try another lease.. */
2498  for (lp = client -> leases; lp -> next; lp = lp -> next)
2499  ;
2500  lp -> next = client -> active;
2501  if (lp -> next) {
2502  lp -> next -> next = (struct client_lease *)0;
2503  }
2504  client -> active = client -> leases;
2505  client -> leases = client -> leases -> next;
2506 
2507  /* If we already tried this lease, we've exhausted the
2508  set of leases, so we might as well give up for
2509  now. */
2510  if (client -> active == loop)
2511  break;
2512  else if (!loop)
2513  loop = client -> active;
2514  }
2515 
2516  /* No leases were available, or what was available didn't work, so
2517  tell the shell script that we failed to allocate an address,
2518  and try again later. */
2519  if (onetry) {
2520  if (!quiet)
2521  log_info ("Unable to obtain a lease on first try.%s",
2522  " Exiting.");
2523  exit (2);
2524  }
2525 
2526  log_info ("No working leases in persistent database - sleeping.");
2527  script_init (client, "FAIL", (struct string_list *)0);
2528  if (client -> alias)
2529  script_write_params (client, "alias_", client -> alias);
2530  script_go (client);
2531  client -> state = S_INIT;
2532  tv.tv_sec = cur_tv.tv_sec + ((client->config->retry_interval + 1) / 2 +
2533  (random() % client->config->retry_interval));
2534  tv.tv_usec = ((tv.tv_sec - cur_tv.tv_sec) > 1) ?
2535  random() % 1000000 : cur_tv.tv_usec;
2536  add_timeout(&tv, state_init, client, 0, 0);
2537  go_daemon ();
2538 }
2539 
2540 void send_request (cpp)
2541  void *cpp;
2542 {
2543  struct client_state *client = cpp;
2544 
2545  int result;
2546  int interval;
2547  struct sockaddr_in destination;
2548  struct in_addr from;
2549  struct timeval tv;
2550 
2551  /* Figure out how long it's been since we started transmitting. */
2552  interval = cur_time - client -> first_sending;
2553 
2554  /* If we're in the INIT-REBOOT or REQUESTING state and we're
2555  past the reboot timeout, go to INIT and see if we can
2556  DISCOVER an address... */
2557  /* XXX In the INIT-REBOOT state, if we don't get an ACK, it
2558  means either that we're on a network with no DHCP server,
2559  or that our server is down. In the latter case, assuming
2560  that there is a backup DHCP server, DHCPDISCOVER will get
2561  us a new address, but we could also have successfully
2562  reused our old address. In the former case, we're hosed
2563  anyway. This is not a win-prone situation. */
2564  if ((client -> state == S_REBOOTING ||
2565  client -> state == S_REQUESTING) &&
2566  interval > client -> config -> reboot_timeout) {
2567  cancel:
2568  client -> state = S_INIT;
2569  cancel_timeout (send_request, client);
2570  state_init (client);
2571  return;
2572  }
2573 
2574  /* If we're in the reboot state, make sure the media is set up
2575  correctly. */
2576  if (client -> state == S_REBOOTING &&
2577  !client -> medium &&
2578  client -> active -> medium ) {
2579  script_init (client, "MEDIUM", client -> active -> medium);
2580 
2581  /* If the medium we chose won't fly, go to INIT state. */
2582  if (script_go (client))
2583  goto cancel;
2584 
2585  /* Record the medium. */
2586  client -> medium = client -> active -> medium;
2587  }
2588 
2589  /* If the lease has expired, relinquish the address and go back
2590  to the INIT state. */
2591  if (client -> state != S_REQUESTING &&
2592  cur_time > client -> active -> expiry) {
2593  /* Run the client script with the new parameters. */
2594  script_init (client, "EXPIRE", (struct string_list *)0);
2595  script_write_params (client, "old_", client -> active);
2596  script_write_requested(client);
2597  if (client -> alias)
2598  script_write_params (client, "alias_",
2599  client -> alias);
2600  script_go (client);
2601 
2602  /* Now do a preinit on the interface so that we can
2603  discover a new address. */
2604  script_init (client, "PREINIT", (struct string_list *)0);
2605  if (client -> alias)
2606  script_write_params (client, "alias_",
2607  client -> alias);
2608  script_go (client);
2609 
2610  client -> state = S_INIT;
2611  state_init (client);
2612  return;
2613  }
2614 
2615  /* Do the exponential backoff... */
2616  if (!client -> interval)
2617  client -> interval = client -> config -> initial_interval;
2618  else {
2619  client -> interval += ((random () >> 2) %
2620  (2 * client -> interval));
2621  }
2622 
2623  /* Don't backoff past cutoff. */
2624  if (client -> interval >
2625  client -> config -> backoff_cutoff)
2626  client -> interval =
2627  ((client -> config -> backoff_cutoff / 2)
2628  + ((random () >> 2) %
2629  client -> config -> backoff_cutoff));
2630 
2631  /* If the backoff would take us to the expiry time, just set the
2632  timeout to the expiry time. */
2633  if (client -> state != S_REQUESTING &&
2634  cur_time + client -> interval > client -> active -> expiry)
2635  client -> interval =
2636  client -> active -> expiry - cur_time + 1;
2637 
2638  /* If the lease T2 time has elapsed, or if we're not yet bound,
2639  broadcast the DHCPREQUEST rather than unicasting. */
2640  if (client -> state == S_REQUESTING ||
2641  client -> state == S_REBOOTING ||
2642  cur_time > client -> active -> rebind)
2643  destination.sin_addr = sockaddr_broadcast.sin_addr;
2644  else
2645  memcpy (&destination.sin_addr.s_addr,
2646  client -> destination.iabuf,
2647  sizeof destination.sin_addr.s_addr);
2648  destination.sin_port = remote_port;
2649  destination.sin_family = AF_INET;
2650 #ifdef HAVE_SA_LEN
2651  destination.sin_len = sizeof destination;
2652 #endif
2653 
2654  if (client -> state == S_RENEWING ||
2655  client -> state == S_REBINDING)
2656  memcpy (&from, client -> active -> address.iabuf,
2657  sizeof from);
2658  else
2659  from.s_addr = INADDR_ANY;
2660 
2661  /* Record the number of seconds since we started sending. */
2662  if (client -> state == S_REQUESTING)
2663  client -> packet.secs = client -> secs;
2664  else {
2665  if (interval < 65536)
2666  client -> packet.secs = htons (interval);
2667  else
2668  client -> packet.secs = htons (65535);
2669  }
2670 
2671  log_info ("DHCPREQUEST on %s to %s port %d (xid=0x%x)",
2672  client -> name ? client -> name : client -> interface -> name,
2673  inet_ntoa (destination.sin_addr),
2674  ntohs (destination.sin_port), ntohl(client -> xid));
2675 
2676  if (destination.sin_addr.s_addr != INADDR_BROADCAST &&
2678 #if defined(SO_BINDTODEVICE)
2679  if (setsockopt(fallback_interface -> wfdesc, SOL_SOCKET,
2680  SO_BINDTODEVICE, client->interface->name,
2681  strlen(client->interface->name)) < 0) {
2682  log_error("%s:%d: Failed to bind fallback interface"
2683  " to %s: %m", MDL, client->interface->name);
2684  }
2685 #endif
2686  result = send_packet(fallback_interface, NULL, &client->packet,
2687  client->packet_length, from, &destination,
2688  NULL);
2689  if (result < 0) {
2690  log_error("%s:%d: Failed to send %d byte long packet "
2691  "over %s interface.", MDL,
2692  client->packet_length,
2694  }
2695 #if defined(SO_BINDTODEVICE)
2696  if (setsockopt(fallback_interface -> wfdesc, SOL_SOCKET,
2697  SO_BINDTODEVICE, NULL, 0) < 0) {
2698  log_fatal("%s:%d: Failed to unbind fallback interface:"
2699  " %m", MDL);
2700  }
2701 #endif
2702  }
2703  else {
2704  /* Send out a packet. */
2705  result = send_packet(client->interface, NULL, &client->packet,
2706  client->packet_length, from, &destination,
2707  NULL);
2708  if (result < 0) {
2709  log_error("%s:%d: Failed to send %d byte long packet"
2710  " over %s interface.", MDL,
2711  client->packet_length,
2712  client->interface->name);
2713  }
2714  }
2715 
2716  tv.tv_sec = cur_tv.tv_sec + client->interval;
2717  tv.tv_usec = ((tv.tv_sec - cur_tv.tv_sec) > 1) ?
2718  random() % 1000000 : cur_tv.tv_usec;
2719  add_timeout(&tv, send_request, client, 0, 0);
2720 }
2721 
2722 void send_decline (cpp)
2723  void *cpp;
2724 {
2725  struct client_state *client = cpp;
2726 
2727  int result;
2728 
2729  log_info ("DHCPDECLINE on %s to %s port %d (xid=0x%x)",
2730  client->name ? client->name : client->interface->name,
2731  inet_ntoa(sockaddr_broadcast.sin_addr),
2732  ntohs(sockaddr_broadcast.sin_port), ntohl(client -> xid));
2733 
2734  /* Send out a packet. */
2735  result = send_packet(client->interface, NULL, &client->packet,
2736  client->packet_length, inaddr_any,
2737  &sockaddr_broadcast, NULL);
2738  if (result < 0) {
2739  log_error("%s:%d: Failed to send %d byte long packet over %s"
2740  " interface.", MDL, client->packet_length,
2741  client->interface->name);
2742  }
2743 }
2744 
2745 void send_release (cpp)
2746  void *cpp;
2747 {
2748  struct client_state *client = cpp;
2749 
2750  int result;
2751  struct sockaddr_in destination;
2752  struct in_addr from;
2753 
2754  memcpy (&from, client -> active -> address.iabuf,
2755  sizeof from);
2756  memcpy (&destination.sin_addr.s_addr,
2757  client -> destination.iabuf,
2758  sizeof destination.sin_addr.s_addr);
2759  destination.sin_port = remote_port;
2760  destination.sin_family = AF_INET;
2761 #ifdef HAVE_SA_LEN
2762  destination.sin_len = sizeof destination;
2763 #endif
2764 
2765  /* Set the lease to end now, so that we don't accidentally
2766  reuse it if we restart before the old expiry time. */
2767  client -> active -> expiry =
2768  client -> active -> renewal =
2769  client -> active -> rebind = cur_time;
2770  if (!write_client_lease (client, client -> active, 1, 1)) {
2771  log_error ("Can't release lease: lease write failed.");
2772  return;
2773  }
2774 
2775  log_info ("DHCPRELEASE on %s to %s port %d (xid=0x%x)",
2776  client -> name ? client -> name : client -> interface -> name,
2777  inet_ntoa (destination.sin_addr),
2778  ntohs (destination.sin_port), ntohl(client -> xid));
2779 
2780  if (fallback_interface) {
2781 #if defined(SO_BINDTODEVICE)
2782  if (setsockopt(fallback_interface -> wfdesc, SOL_SOCKET,
2783  SO_BINDTODEVICE, client->interface->name,
2784  strlen(client->interface->name)) < 0) {
2785  log_error("%s:%d: Failed to bind fallback interface"
2786  " to %s: %m", MDL, client->interface->name);
2787  }
2788 #endif
2789  result = send_packet(fallback_interface, NULL, &client->packet,
2790  client->packet_length, from, &destination,
2791  NULL);
2792  if (result < 0) {
2793  log_error("%s:%d: Failed to send %d byte long packet"
2794  " over %s interface.", MDL,
2795  client->packet_length,
2797  }
2798 #if defined(SO_BINDTODEVICE)
2799  if (setsockopt(fallback_interface -> wfdesc, SOL_SOCKET,
2800  SO_BINDTODEVICE, NULL, 0) < 0) {
2801  log_fatal("%s:%d: Failed to unbind fallback interface:"
2802  " %m", MDL);
2803  }
2804 #endif
2805  } else {
2806  /* Send out a packet. */
2807  result = send_packet(client->interface, NULL, &client->packet,
2808  client->packet_length, from, &destination,
2809  NULL);
2810  if (result < 0) {
2811  log_error ("%s:%d: Failed to send %d byte long packet"
2812  " over %s interface.", MDL,
2813  client->packet_length,
2814  client->interface->name);
2815  }
2816 
2817  }
2818 }
2819 
2820 void
2822  u_int8_t *type, struct option_cache *sid,
2823  struct iaddr *rip, struct option **prl,
2824  struct option_state **op)
2825 {
2826  unsigned i;
2827  struct option_cache *oc;
2828  struct option *option = NULL;
2829  struct buffer *bp = NULL;
2830 
2831  /* If there are any leftover options, get rid of them. */
2832  if (*op)
2834 
2835  /* Allocate space for options. */
2837 
2838  /* Send the server identifier if provided. */
2839  if (sid)
2840  save_option(&dhcp_universe, *op, sid);
2841 
2842  oc = NULL;
2843 
2844  /* Send the requested address if provided. */
2845  if (rip) {
2846  client->requested_address = *rip;
2848  if (!(option_code_hash_lookup(&option, dhcp_universe.code_hash,
2849  &i, 0, MDL) &&
2850  make_const_option_cache(&oc, NULL, rip->iabuf, rip->len,
2851  option, MDL)))
2852  log_error ("can't make requested address cache.");
2853  else {
2854  save_option(&dhcp_universe, *op, oc);
2856  }
2857  option_dereference(&option, MDL);
2858  } else {
2859  client->requested_address.len = 0;
2860  }
2861 
2863  if (!(option_code_hash_lookup(&option, dhcp_universe.code_hash, &i, 0,
2864  MDL) &&
2865  make_const_option_cache(&oc, NULL, type, 1, option, MDL)))
2866  log_error("can't make message type.");
2867  else {
2868  save_option(&dhcp_universe, *op, oc);
2870  }
2871  option_dereference(&option, MDL);
2872 
2873  if (prl) {
2874  int len;
2875 
2876  /* Probe the length of the list. */
2877  len = 0;
2878  for (i = 0 ; prl[i] != NULL ; i++)
2879  if (prl[i]->universe == &dhcp_universe)
2880  len++;
2881 
2882  if (!buffer_allocate(&bp, len, MDL))
2883  log_error("can't make parameter list buffer.");
2884  else {
2885  unsigned code = DHO_DHCP_PARAMETER_REQUEST_LIST;
2886 
2887  len = 0;
2888  for (i = 0 ; prl[i] != NULL ; i++)
2889  if (prl[i]->universe == &dhcp_universe)
2890  bp->data[len++] = prl[i]->code;
2891 
2892  if (!(option_code_hash_lookup(&option,
2894  &code, 0, MDL) &&
2895  make_const_option_cache(&oc, &bp, NULL, len,
2896  option, MDL))) {
2897  if (bp != NULL)
2898  buffer_dereference(&bp, MDL);
2899  log_error ("can't make option cache");
2900  } else {
2901  save_option(&dhcp_universe, *op, oc);
2903  }
2904  option_dereference(&option, MDL);
2905  }
2906  }
2907 
2908  /*
2909  * If requested (duid_v4 == 1) add an RFC4361 compliant client-identifier
2910  * This can be overridden by including a client id in the configuration
2911  * file.
2912  */
2913  if (duid_v4 == 1) {
2914  struct data_string client_identifier;
2915  int hw_idx, hw_len;
2916 
2917  memset(&client_identifier, 0, sizeof(client_identifier));
2918  client_identifier.len = 1 + 4 + default_duid.len;
2919  if (!buffer_allocate(&client_identifier.buffer,
2920  client_identifier.len, MDL))
2921  log_fatal("no memory for default DUID!");
2922  client_identifier.data = client_identifier.buffer->data;
2923 
2925 
2926  /* Client-identifier type : 1 byte */
2927  *client_identifier.buffer->data = 255;
2928 
2929  /* IAID : 4 bytes
2930  * we use the low 4 bytes from the interface address
2931  */
2932  if (client->interface->hw_address.hlen > 4) {
2933  hw_idx = client->interface->hw_address.hlen - 4;
2934  hw_len = 4;
2935  } else {
2936  hw_idx = 0;
2937  hw_len = client->interface->hw_address.hlen;
2938  }
2939  memcpy(&client_identifier.buffer->data + 5 - hw_len,
2940  client->interface->hw_address.hbuf + hw_idx,
2941  hw_len);
2942 
2943  /* Add the default duid */
2944  memcpy(&client_identifier.buffer->data+(1+4),
2946 
2947  /* And save the option */
2948  if (!(option_code_hash_lookup(&option, dhcp_universe.code_hash,
2949  &i, 0, MDL) &&
2950  make_const_option_cache(&oc, NULL,
2951  (u_int8_t *)client_identifier.data,
2952  client_identifier.len,
2953  option, MDL)))
2954  log_error ("can't make requested client id cache..");
2955  else {
2956  save_option (&dhcp_universe, *op, oc);
2958  }
2959  option_dereference(&option, MDL);
2960  }
2961 
2962  /* Run statements that need to be run on transmission. */
2963  if (client->config->on_transmission)
2964  execute_statements_in_scope(NULL, NULL, NULL, client,
2965  (lease ? lease->options : NULL),
2966  *op, &global_scope,
2967  client->config->on_transmission,
2968  NULL, NULL);
2969 }
2970 
2971 void make_discover (client, lease)
2972  struct client_state *client;
2973  struct client_lease *lease;
2974 {
2975  unsigned char discover = DHCPDISCOVER;
2976  struct option_state *options = (struct option_state *)0;
2977 
2978  memset (&client -> packet, 0, sizeof (client -> packet));
2979 
2980  make_client_options (client,
2981  lease, &discover, (struct option_cache *)0,
2982  lease ? &lease -> address : (struct iaddr *)0,
2983  client -> config -> requested_options,
2984  &options);
2985 
2986  /* Set up the option buffer... */
2987  client -> packet_length =
2988  cons_options ((struct packet *)0, &client -> packet,
2989  (struct lease *)0, client,
2990  /* maximum packet size */1500,
2991  (struct option_state *)0,
2992  options,
2993  /* scope */ &global_scope,
2994  /* overload */ 0,
2995  /* terminate */0,
2996  /* bootpp */0,
2997  (struct data_string *)0,
2998  client -> config -> vendor_space_name);
2999 
3000  option_state_dereference (&options, MDL);
3001  if (client -> packet_length < BOOTP_MIN_LEN)
3002  client -> packet_length = BOOTP_MIN_LEN;
3003 
3004  client -> packet.op = BOOTREQUEST;
3005  client -> packet.htype = client -> interface -> hw_address.hbuf [0];
3006  client -> packet.hlen = client -> interface -> hw_address.hlen - 1;
3007  client -> packet.hops = 0;
3008  client -> packet.xid = random ();
3009  client -> packet.secs = 0; /* filled in by send_discover. */
3010 
3011  if ((!(bootp_broadcast_always || client->config->bootp_broadcast_always))
3012  && can_receive_unicast_unconfigured(client->interface))
3013  client -> packet.flags = 0;
3014  else
3015  client -> packet.flags = htons (BOOTP_BROADCAST);
3016 
3017  memset (&(client -> packet.ciaddr),
3018  0, sizeof client -> packet.ciaddr);
3019  memset (&(client -> packet.yiaddr),
3020  0, sizeof client -> packet.yiaddr);
3021  memset (&(client -> packet.siaddr),
3022  0, sizeof client -> packet.siaddr);
3023  client -> packet.giaddr = giaddr;
3024  if (client -> interface -> hw_address.hlen > 0)
3025  memcpy (client -> packet.chaddr,
3026  &client -> interface -> hw_address.hbuf [1],
3027  (unsigned)(client -> interface -> hw_address.hlen - 1));
3028 
3029 #ifdef DEBUG_PACKET
3030  dump_raw ((unsigned char *)&client -> packet, client -> packet_length);
3031 #endif
3032 }
3033 
3034 
3035 void make_request (client, lease)
3036  struct client_state *client;
3037  struct client_lease *lease;
3038 {
3039  unsigned char request = DHCPREQUEST;
3040  struct option_cache *oc;
3041 
3042  memset (&client -> packet, 0, sizeof (client -> packet));
3043 
3044  if (client -> state == S_REQUESTING)
3045  oc = lookup_option (&dhcp_universe, lease -> options,
3047  else
3048  oc = (struct option_cache *)0;
3049 
3050  if (client -> sent_options)
3051  option_state_dereference (&client -> sent_options, MDL);
3052 
3053  make_client_options (client, lease, &request, oc,
3054  ((client -> state == S_REQUESTING ||
3055  client -> state == S_REBOOTING)
3056  ? &lease -> address
3057  : (struct iaddr *)0),
3058  client -> config -> requested_options,
3059  &client -> sent_options);
3060 
3061  /* Set up the option buffer... */
3062  client -> packet_length =
3063  cons_options ((struct packet *)0, &client -> packet,
3064  (struct lease *)0, client,
3065  /* maximum packet size */1500,
3066  (struct option_state *)0,
3067  client -> sent_options,
3068  /* scope */ &global_scope,
3069  /* overload */ 0,
3070  /* terminate */0,
3071  /* bootpp */0,
3072  (struct data_string *)0,
3073  client -> config -> vendor_space_name);
3074 
3075  if (client -> packet_length < BOOTP_MIN_LEN)
3076  client -> packet_length = BOOTP_MIN_LEN;
3077 
3078  client -> packet.op = BOOTREQUEST;
3079  client -> packet.htype = client -> interface -> hw_address.hbuf [0];
3080  client -> packet.hlen = client -> interface -> hw_address.hlen - 1;
3081  client -> packet.hops = 0;
3082  client -> packet.xid = client -> xid;
3083  client -> packet.secs = 0; /* Filled in by send_request. */
3084 
3085  /* If we own the address we're requesting, put it in ciaddr;
3086  otherwise set ciaddr to zero. */
3087  if (client -> state == S_BOUND ||
3088  client -> state == S_RENEWING ||
3089  client -> state == S_REBINDING) {
3090  memcpy (&client -> packet.ciaddr,
3091  lease -> address.iabuf, lease -> address.len);
3092  client -> packet.flags = 0;
3093  } else {
3094  memset (&client -> packet.ciaddr, 0,
3095  sizeof client -> packet.ciaddr);
3096  if ((!(bootp_broadcast_always ||
3097  client ->config->bootp_broadcast_always)) &&
3098  can_receive_unicast_unconfigured (client -> interface))
3099  client -> packet.flags = 0;
3100  else
3101  client -> packet.flags = htons (BOOTP_BROADCAST);
3102  }
3103 
3104  memset (&client -> packet.yiaddr, 0,
3105  sizeof client -> packet.yiaddr);
3106  memset (&client -> packet.siaddr, 0,
3107  sizeof client -> packet.siaddr);
3108  if (client -> state != S_BOUND &&
3109  client -> state != S_RENEWING)
3110  client -> packet.giaddr = giaddr;
3111  else
3112  memset (&client -> packet.giaddr, 0,
3113  sizeof client -> packet.giaddr);
3114  if (client -> interface -> hw_address.hlen > 0)
3115  memcpy (client -> packet.chaddr,
3116  &client -> interface -> hw_address.hbuf [1],
3117  (unsigned)(client -> interface -> hw_address.hlen - 1));
3118 
3119 #ifdef DEBUG_PACKET
3120  dump_raw ((unsigned char *)&client -> packet, client -> packet_length);
3121 #endif
3122 }
3123 
3124 void make_decline (client, lease)
3125  struct client_state *client;
3126  struct client_lease *lease;
3127 {
3128  unsigned char decline = DHCPDECLINE;
3129  struct option_cache *oc;
3130 
3131  struct option_state *options = (struct option_state *)0;
3132 
3133  /* Create the options cache. */
3134  oc = lookup_option (&dhcp_universe, lease -> options,
3136  make_client_options(client, lease, &decline, oc, &lease->address,
3137  NULL, &options);
3138 
3139  /* Consume the options cache into the option buffer. */
3140  memset (&client -> packet, 0, sizeof (client -> packet));
3141  client -> packet_length =
3142  cons_options ((struct packet *)0, &client -> packet,
3143  (struct lease *)0, client, 0,
3144  (struct option_state *)0, options,
3145  &global_scope, 0, 0, 0, (struct data_string *)0,
3146  client -> config -> vendor_space_name);
3147 
3148  /* Destroy the options cache. */
3149  option_state_dereference (&options, MDL);
3150 
3151  if (client -> packet_length < BOOTP_MIN_LEN)
3152  client -> packet_length = BOOTP_MIN_LEN;
3153 
3154  client -> packet.op = BOOTREQUEST;
3155  client -> packet.htype = client -> interface -> hw_address.hbuf [0];
3156  client -> packet.hlen = client -> interface -> hw_address.hlen - 1;
3157  client -> packet.hops = 0;
3158  client -> packet.xid = client -> xid;
3159  client -> packet.secs = 0; /* Filled in by send_request. */
3160  if ((!(bootp_broadcast_always || client->config-> bootp_broadcast_always))
3161  && can_receive_unicast_unconfigured (client->interface))
3162  client -> packet.flags = 0;
3163  else
3164  client -> packet.flags = htons (BOOTP_BROADCAST);
3165 
3166  /* ciaddr must always be zero. */
3167  memset (&client -> packet.ciaddr, 0,
3168  sizeof client -> packet.ciaddr);
3169  memset (&client -> packet.yiaddr, 0,
3170  sizeof client -> packet.yiaddr);
3171  memset (&client -> packet.siaddr, 0,
3172  sizeof client -> packet.siaddr);
3173  client -> packet.giaddr = giaddr;
3174  memcpy (client -> packet.chaddr,
3175  &client -> interface -> hw_address.hbuf [1],
3176  client -> interface -> hw_address.hlen);
3177 
3178 #ifdef DEBUG_PACKET
3179  dump_raw ((unsigned char *)&client -> packet, client -> packet_length);
3180 #endif
3181 }
3182 
3183 void make_release (client, lease)
3184  struct client_state *client;
3185  struct client_lease *lease;
3186 {
3187  unsigned char request = DHCPRELEASE;
3188  struct option_cache *oc;
3189 
3190  struct option_state *options = (struct option_state *)0;
3191 
3192  memset (&client -> packet, 0, sizeof (client -> packet));
3193 
3194  oc = lookup_option (&dhcp_universe, lease -> options,
3196  make_client_options(client, lease, &request, oc, NULL, NULL, &options);
3197 
3198  /* Set up the option buffer... */
3199  client -> packet_length =
3200  cons_options ((struct packet *)0, &client -> packet,
3201  (struct lease *)0, client,
3202  /* maximum packet size */1500,
3203  (struct option_state *)0,
3204  options,
3205  /* scope */ &global_scope,
3206  /* overload */ 0,
3207  /* terminate */0,
3208  /* bootpp */0,
3209  (struct data_string *)0,
3210  client -> config -> vendor_space_name);
3211 
3212  if (client -> packet_length < BOOTP_MIN_LEN)
3213  client -> packet_length = BOOTP_MIN_LEN;
3214  option_state_dereference (&options, MDL);
3215 
3216  client -> packet.op = BOOTREQUEST;
3217  client -> packet.htype = client -> interface -> hw_address.hbuf [0];
3218  client -> packet.hlen = client -> interface -> hw_address.hlen - 1;
3219  client -> packet.hops = 0;
3220  client -> packet.xid = random ();
3221  client -> packet.secs = 0;
3222  client -> packet.flags = 0;
3223  memcpy (&client -> packet.ciaddr,
3224  lease -> address.iabuf, lease -> address.len);
3225  memset (&client -> packet.yiaddr, 0,
3226  sizeof client -> packet.yiaddr);
3227  memset (&client -> packet.siaddr, 0,
3228  sizeof client -> packet.siaddr);
3229  client -> packet.giaddr = giaddr;
3230  memcpy (client -> packet.chaddr,
3231  &client -> interface -> hw_address.hbuf [1],
3232  client -> interface -> hw_address.hlen);
3233 
3234 #ifdef DEBUG_PACKET
3235  dump_raw ((unsigned char *)&client -> packet, client -> packet_length);
3236 #endif
3237 }
3238 
3240  struct client_lease *lease;
3241 {
3242  if (lease -> server_name)
3243  dfree (lease -> server_name, MDL);
3244  if (lease -> filename)
3245  dfree (lease -> filename, MDL);
3246  option_state_dereference (&lease -> options, MDL);
3247  free_client_lease (lease, MDL);
3248 }
3249 
3250 FILE *leaseFile = NULL;
3252 
3254 {
3255  struct interface_info *ip;
3256  struct client_state *client;
3257  struct client_lease *lp;
3258 
3259  if (leaseFile != NULL)
3260  fclose (leaseFile);
3261  leaseFile = fopen (path_dhclient_db, "we");
3262  if (leaseFile == NULL) {
3263  log_error ("can't create %s: %m", path_dhclient_db);
3264  return;
3265  }
3266 
3267  /* If there is a default duid, write it out. */
3268  if (default_duid.len != 0)
3269  write_duid(&default_duid);
3270 
3271  /* Write out all the leases attached to configured interfaces that
3272  we know about. */
3273  for (ip = interfaces; ip; ip = ip -> next) {
3274  for (client = ip -> client; client; client = client -> next) {
3275  for (lp = client -> leases; lp; lp = lp -> next) {
3276  write_client_lease (client, lp, 1, 0);
3277  }
3278  if (client -> active)
3279  write_client_lease (client,
3280  client -> active, 1, 0);
3281 
3282  if (client->active_lease != NULL)
3283  write_client6_lease(client,
3284  client->active_lease,
3285  1, 0);
3286 
3287  /* Reset last_write after rewrites. */
3288  client->last_write = 0;
3289  }
3290  }
3291 
3292  /* Write out any leases that are attached to interfaces that aren't
3293  currently configured. */
3294  for (ip = dummy_interfaces; ip; ip = ip -> next) {
3295  for (client = ip -> client; client; client = client -> next) {
3296  for (lp = client -> leases; lp; lp = lp -> next) {
3297  write_client_lease (client, lp, 1, 0);
3298  }
3299  if (client -> active)
3300  write_client_lease (client,
3301  client -> active, 1, 0);
3302 
3303  if (client->active_lease != NULL)
3304  write_client6_lease(client,
3305  client->active_lease,
3306  1, 0);
3307 
3308  /* Reset last_write after rewrites. */
3309  client->last_write = 0;
3310  }
3311  }
3312  fflush (leaseFile);
3313 }
3314 
3316  struct packet *packet, struct lease *lease,
3317  struct client_state *client_state,
3318  struct option_state *in_options,
3319  struct option_state *cfg_options,
3320  struct binding_scope **scope,
3321  struct universe *u, void *stuff)
3322 {
3323  const char *name, *dot;
3324  struct data_string ds;
3325  char *preamble = stuff;
3326 
3327  memset (&ds, 0, sizeof ds);
3328 
3329  if (u != &dhcp_universe) {
3330  name = u -> name;
3331  dot = ".";
3332  } else {
3333  name = "";
3334  dot = "";
3335  }
3336  if (evaluate_option_cache (&ds, packet, lease, client_state,
3337  in_options, cfg_options, scope, oc, MDL)) {
3338  /* The option name */
3339  fprintf(leaseFile, "%soption %s%s%s", preamble,
3340  name, dot, oc->option->name);
3341 
3342  /* The option value if there is one */
3343  if ((oc->option->format == NULL) ||
3344  (oc->option->format[0] != 'Z')) {
3345  fprintf(leaseFile, " %s",
3347  ds.len, 1, 1));
3348  }
3349 
3350  /* The closing semi-colon and newline */
3351  fprintf(leaseFile, ";\n");
3352 
3353  data_string_forget (&ds, MDL);
3354  }
3355 }
3356 
3357 /* Write an option cache to the lease store. */
3358 static void
3359 write_options(struct client_state *client, struct option_state *options,
3360  const char *preamble)
3361 {
3362  int i;
3363 
3364  for (i = 0; i < options->universe_count; i++) {
3365  option_space_foreach(NULL, NULL, client, NULL, options,
3366  &global_scope, universes[i],
3367  (char *)preamble, write_lease_option);
3368  }
3369 }
3370 
3371 /*
3372  * The "best" default DUID, since we cannot predict any information
3373  * about the system (such as whether or not the hardware addresses are
3374  * integrated into the motherboard or similar), is the "LLT", link local
3375  * plus time, DUID. For real stateless "LL" is better.
3376  *
3377  * Once generated, this duid is stored into the state database, and
3378  * retained across restarts.
3379  *
3380  * For the time being, there is probably a different state database for
3381  * every daemon, so this winds up being a per-interface identifier...which
3382  * is not how it is intended. Upcoming rearchitecting the client should
3383  * address this "one daemon model."
3384  */
3385 isc_result_t
3386 form_duid(struct data_string *duid, const char *file, int line)
3387 {
3388  struct interface_info *ip;
3389  int len;
3390  char *str;
3391 
3392  /* For now, just use the first interface on the list. */
3393  ip = interfaces;
3394 
3395  if (ip == NULL)
3396  log_fatal("Impossible condition at %s:%d.", MDL);
3397 
3398  while (ip && ip->hw_address.hbuf[0] == HTYPE_RESERVED) {
3399  /* Try the other interfaces */
3400  log_debug("Cannot form default DUID from interface %s.", ip->name);
3401  ip = ip->next;
3402  }
3403  if (ip == NULL) {
3404  return ISC_R_UNEXPECTED;
3405  }
3406 
3407  if ((ip->hw_address.hlen == 0) ||
3408  (ip->hw_address.hlen > sizeof(ip->hw_address.hbuf)))
3409  log_fatal("Impossible hardware address length at %s:%d.", MDL);
3410 
3411  if (duid_type == 0)
3413 
3414  /*
3415  * 2 bytes for the 'duid type' field.
3416  * 2 bytes for the 'htype' field.
3417  * (DUID_LLT) 4 bytes for the 'current time'.
3418  * enough bytes for the hardware address (note that hw_address has
3419  * the 'htype' on byte zero).
3420  */
3421  len = 4 + (ip->hw_address.hlen - 1);
3422  if (duid_type == DUID_LLT)
3423  len += 4;
3424  if (!buffer_allocate(&duid->buffer, len, MDL))
3425  log_fatal("no memory for default DUID!");
3426  duid->data = duid->buffer->data;
3427  duid->len = len;
3428 
3429  /* Basic Link Local Address type of DUID. */
3430  if (duid_type == DUID_LLT) {
3431  putUShort(duid->buffer->data, DUID_LLT);
3432  putUShort(duid->buffer->data + 2, ip->hw_address.hbuf[0]);
3433  putULong(duid->buffer->data + 4, cur_time - DUID_TIME_EPOCH);
3434  memcpy(duid->buffer->data + 8, ip->hw_address.hbuf + 1,
3435  ip->hw_address.hlen - 1);
3436  } else {
3437  putUShort(duid->buffer->data, DUID_LL);
3438  putUShort(duid->buffer->data + 2, ip->hw_address.hbuf[0]);
3439  memcpy(duid->buffer->data + 4, ip->hw_address.hbuf + 1,
3440  ip->hw_address.hlen - 1);
3441  }
3442 
3443  str = quotify_buf(duid->data, duid->len, MDL);
3444  if (str == NULL)
3445  log_info("Created duid.");
3446  else {
3447  log_info("Created duid %s.", str);
3448  dfree(str, MDL);
3449  }
3450 
3451  return ISC_R_SUCCESS;
3452 }
3453 
3454 /* Write the default DUID to the lease store. */
3455 static isc_result_t
3456 write_duid(struct data_string *duid)
3457 {
3458  char *str;
3459  int stat;
3460 
3461  if ((duid == NULL) || (duid->len <= 2))
3462  return DHCP_R_INVALIDARG;
3463 
3464  if (leaseFile == NULL) { /* XXX? */
3465  leaseFile = fopen(path_dhclient_db, "we");
3466  if (leaseFile == NULL) {
3467  log_error("can't create %s: %m", path_dhclient_db);
3468  return ISC_R_IOERROR;
3469  }
3470  }
3471 
3472  /* It would make more sense to write this as a hex string,
3473  * but our function to do that (print_hex_n) uses a fixed
3474  * length buffer...and we can't guarantee a duid would be
3475  * less than the fixed length.
3476  */
3477  str = quotify_buf(duid->data, duid->len, MDL);
3478  if (str == NULL)
3479  return ISC_R_NOMEMORY;
3480 
3481  stat = fprintf(leaseFile, "default-duid \"%s\";\n", str);
3482  dfree(str, MDL);
3483  if (stat <= 0)
3484  return ISC_R_IOERROR;
3485 
3486  if (fflush(leaseFile) != 0)
3487  return ISC_R_IOERROR;
3488 
3489  return ISC_R_SUCCESS;
3490 }
3491 
3492 /* Write a DHCPv6 lease to the store. */
3493 isc_result_t
3495  int rewrite, int sync)
3496 {
3497  struct dhc6_ia *ia;
3498  struct dhc6_addr *addr;
3499  int stat;
3500  const char *ianame;
3501 
3502  /* This should include the current lease. */
3503  if (!rewrite && (leases_written++ > 20)) {
3505  leases_written = 0;
3506  return ISC_R_SUCCESS;
3507  }
3508 
3509  if (client == NULL || lease == NULL)
3510  return DHCP_R_INVALIDARG;
3511 
3512  if (leaseFile == NULL) { /* XXX? */
3513  leaseFile = fopen(path_dhclient_db, "w");
3514  if (leaseFile == NULL) {
3515  log_error("can't create %s: %m", path_dhclient_db);
3516  return ISC_R_IOERROR;
3517  }
3518  }
3519 
3520  stat = fprintf(leaseFile, "lease6 {\n");
3521  if (stat <= 0)
3522  return ISC_R_IOERROR;
3523 
3524  stat = fprintf(leaseFile, " interface \"%s\";\n",
3525  client->interface->name);
3526  if (stat <= 0)
3527  return ISC_R_IOERROR;
3528 
3529  for (ia = lease->bindings ; ia != NULL ; ia = ia->next) {
3530  switch (ia->ia_type) {
3531  case D6O_IA_NA:
3532  default:
3533  ianame = "ia-na";
3534  break;
3535  case D6O_IA_TA:
3536  ianame = "ia-ta";
3537  break;
3538  case D6O_IA_PD:
3539  ianame = "ia-pd";
3540  break;
3541  }
3542  stat = fprintf(leaseFile, " %s %s {\n",
3543  ianame, print_hex_1(4, ia->iaid, 12));
3544  if (stat <= 0)
3545  return ISC_R_IOERROR;
3546 
3547  if (ia->ia_type != D6O_IA_TA)
3548  stat = fprintf(leaseFile, " starts %d;\n"
3549  " renew %u;\n"
3550  " rebind %u;\n",
3551  (int)ia->starts, ia->renew, ia->rebind);
3552  else
3553  stat = fprintf(leaseFile, " starts %d;\n",
3554  (int)ia->starts);
3555  if (stat <= 0)
3556  return ISC_R_IOERROR;
3557 
3558  for (addr = ia->addrs ; addr != NULL ; addr = addr->next) {
3559  if (ia->ia_type != D6O_IA_PD)
3560  stat = fprintf(leaseFile,
3561  " iaaddr %s {\n",
3562  piaddr(addr->address));
3563  else
3564  stat = fprintf(leaseFile,
3565  " iaprefix %s/%d {\n",
3566  piaddr(addr->address),
3567  (int)addr->plen);
3568  if (stat <= 0)
3569  return ISC_R_IOERROR;
3570 
3571  stat = fprintf(leaseFile, " starts %d;\n"
3572  " preferred-life %u;\n"
3573  " max-life %u;\n",
3574  (int)addr->starts, addr->preferred_life,
3575  addr->max_life);
3576  if (stat <= 0)
3577  return ISC_R_IOERROR;
3578 
3579  if (addr->options != NULL)
3580  write_options(client, addr->options, " ");
3581 
3582  stat = fprintf(leaseFile, " }\n");
3583  if (stat <= 0)
3584  return ISC_R_IOERROR;
3585  }
3586 
3587  if (ia->options != NULL)
3588  write_options(client, ia->options, " ");
3589 
3590  stat = fprintf(leaseFile, " }\n");
3591  if (stat <= 0)
3592  return ISC_R_IOERROR;
3593  }
3594 
3595  if (lease->released) {
3596  stat = fprintf(leaseFile, " released;\n");
3597  if (stat <= 0)
3598  return ISC_R_IOERROR;
3599  }
3600 
3601  if (lease->options != NULL)
3602  write_options(client, lease->options, " ");
3603 
3604  stat = fprintf(leaseFile, "}\n");
3605  if (stat <= 0)
3606  return ISC_R_IOERROR;
3607 
3608  if (fflush(leaseFile) != 0)
3609  return ISC_R_IOERROR;
3610 
3611  if (sync) {
3612  if (fsync(fileno(leaseFile)) < 0) {
3613  log_error("write_client_lease: fsync(): %m");
3614  return ISC_R_IOERROR;
3615  }
3616  }
3617 
3618  return ISC_R_SUCCESS;
3619 }
3620 
3621 int write_client_lease (client, lease, rewrite, makesure)
3622  struct client_state *client;
3623  struct client_lease *lease;
3624  int rewrite;
3625  int makesure;
3626 {
3627  struct data_string ds;
3628  int errors = 0;
3629  char *s;
3630  const char *tval;
3631 
3632  if (!rewrite) {
3633  if (leases_written++ > 20) {
3635  leases_written = 0;
3636  }
3637  }
3638 
3639  /* If the lease came from the config file, we don't need to stash
3640  a copy in the lease database. */
3641  if (lease -> is_static)
3642  return 1;
3643 
3644  if (leaseFile == NULL) { /* XXX */
3645  leaseFile = fopen (path_dhclient_db, "we");
3646  if (leaseFile == NULL) {
3647  log_error ("can't create %s: %m", path_dhclient_db);
3648  return 0;
3649  }
3650  }
3651 
3652  errno = 0;
3653  fprintf (leaseFile, "lease {\n");
3654  if (lease -> is_bootp) {
3655  fprintf (leaseFile, " bootp;\n");
3656  if (errno) {
3657  ++errors;
3658  errno = 0;
3659  }
3660  }
3661  fprintf (leaseFile, " interface \"%s\";\n",
3662  client -> interface -> name);
3663  if (errno) {
3664  ++errors;
3665  errno = 0;
3666  }
3667  if (client -> name) {
3668  fprintf (leaseFile, " name \"%s\";\n", client -> name);
3669  if (errno) {
3670  ++errors;
3671  errno = 0;
3672  }
3673  }
3674  fprintf (leaseFile, " fixed-address %s;\n",
3675  piaddr (lease -> address));
3676  if (errno) {
3677  ++errors;
3678  errno = 0;
3679  }
3680  if (lease -> filename) {
3681  s = quotify_string (lease -> filename, MDL);
3682  if (s) {
3683  fprintf (leaseFile, " filename \"%s\";\n", s);
3684  if (errno) {
3685  ++errors;
3686  errno = 0;
3687  }
3688  dfree (s, MDL);
3689  } else
3690  errors++;
3691 
3692  }
3693  if (lease->server_name != NULL) {
3694  s = quotify_string(lease->server_name, MDL);
3695  if (s != NULL) {
3696  fprintf(leaseFile, " server-name \"%s\";\n", s);
3697  if (errno) {
3698  ++errors;
3699  errno = 0;
3700  }
3701  dfree(s, MDL);
3702  } else
3703  ++errors;
3704  }
3705  if (lease -> medium) {
3706  s = quotify_string (lease -> medium -> string, MDL);
3707  if (s) {
3708  fprintf (leaseFile, " medium \"%s\";\n", s);
3709  if (errno) {
3710  ++errors;
3711  errno = 0;
3712  }
3713  dfree (s, MDL);
3714  } else
3715  errors++;
3716  }
3717  if (errno != 0) {
3718  errors++;
3719  errno = 0;
3720  }
3721 
3722  memset (&ds, 0, sizeof ds);
3723 
3724  write_options(client, lease->options, " ");
3725 
3726  tval = print_time(lease->renewal);
3727  if (tval == NULL ||
3728  fprintf(leaseFile, " renew %s\n", tval) < 0)
3729  errors++;
3730 
3731  tval = print_time(lease->rebind);
3732  if (tval == NULL ||
3733  fprintf(leaseFile, " rebind %s\n", tval) < 0)
3734  errors++;
3735 
3736  tval = print_time(lease->expiry);
3737  if (tval == NULL ||
3738  fprintf(leaseFile, " expire %s\n", tval) < 0)
3739  errors++;
3740 
3741  if (fprintf(leaseFile, "}\n") < 0)
3742  errors++;
3743 
3744  if (fflush(leaseFile) != 0)
3745  errors++;
3746 
3747  client->last_write = cur_time;
3748 
3749  if (!errors && makesure) {
3750  if (fsync (fileno (leaseFile)) < 0) {
3751  log_info ("write_client_lease: %m");
3752  return 0;
3753  }
3754  }
3755 
3756  return errors ? 0 : 1;
3757 }
3758 
3759 /* Variables holding name of script and file pointer for writing to
3760  script. Needless to say, this is not reentrant - only one script
3761  can be invoked at a time. */
3762 char scriptName [256];
3764 
3765 void script_init (client, reason, medium)
3766  struct client_state *client;
3767  const char *reason;
3768  struct string_list *medium;
3769 {
3770  struct string_list *sl, *next;
3771 
3772  if (client) {
3773  for (sl = client -> env; sl; sl = next) {
3774  next = sl -> next;
3775  dfree (sl, MDL);
3776  }
3777  client -> env = (struct string_list *)0;
3778  client -> envc = 0;
3779 
3780  if (client -> interface) {
3781  client_envadd (client, "", "interface", "%s",
3782  client -> interface -> name);
3783  }
3784  if (client -> name)
3785  client_envadd (client,
3786  "", "client", "%s", client -> name);
3787  if (medium)
3788  client_envadd (client,
3789  "", "medium", "%s", medium -> string);
3790 
3791  client_envadd (client, "", "reason", "%s", reason);
3792  client_envadd (client, "", "pid", "%ld", (long int)getpid ());
3793  }
3794 }
3795 
3797  struct packet *packet, struct lease *lease,
3798  struct client_state *client_state,
3799  struct option_state *in_options,
3800  struct option_state *cfg_options,
3801  struct binding_scope **scope,
3802  struct universe *u, void *stuff)
3803 {
3804  struct envadd_state *es = stuff;
3805  struct data_string data;
3806  memset (&data, 0, sizeof data);
3807 
3808  if (evaluate_option_cache (&data, packet, lease, client_state,
3809  in_options, cfg_options, scope, oc, MDL)) {
3810  if (data.len) {
3811  char name [256];
3812  if (dhcp_option_ev_name (name, sizeof name,
3813  oc->option)) {
3814  const char *value;
3815  size_t length;
3816  value = pretty_print_option(oc->option,
3817  data.data,
3818  data.len, 0, 0);
3819  length = strlen(value);
3820 
3821  if (check_option_values(oc->option->universe,
3822  oc->option->code,
3823  value, length) == 0) {
3824  client_envadd(es->client, es->prefix,
3825  name, "%s", value);
3826  } else {
3827  log_error("suspect value in %s "
3828  "option - discarded",
3829  name);
3830  }
3831  data_string_forget (&data, MDL);
3832  }
3833  }
3834  }
3835 }
3836 
3837 void script_write_params (client, prefix, lease)
3838  struct client_state *client;
3839  const char *prefix;
3840  struct client_lease *lease;
3841 {
3842  int i;
3843  struct data_string data;
3844  struct option_cache *oc;
3845  struct envadd_state es;
3846 
3847  es.client = client;
3848  es.prefix = prefix;
3849 
3850  client_envadd (client,
3851  prefix, "ip_address", "%s", piaddr (lease -> address));
3852 
3853  /* If we've set the next server address in the lease structure
3854  put it into an environment variable for the script */
3855  if (lease->next_srv_addr.len != 0) {
3856  client_envadd(client, prefix, "next_server", "%s",
3857  piaddr(lease->next_srv_addr));
3858  }
3859 
3860  /* For the benefit of Linux (and operating systems which may
3861  have similar needs), compute the network address based on
3862  the supplied ip address and netmask, if provided. Also
3863  compute the broadcast address (the host address all ones
3864  broadcast address, not the host address all zeroes
3865  broadcast address). */
3866 
3867  memset (&data, 0, sizeof data);
3868  oc = lookup_option (&dhcp_universe, lease -> options, DHO_SUBNET_MASK);
3869  if (oc && evaluate_option_cache (&data, (struct packet *)0,
3870  (struct lease *)0, client,
3871  (struct option_state *)0,
3872  lease -> options,
3873  &global_scope, oc, MDL)) {
3874  if (data.len > 3) {
3875  struct iaddr netmask, subnet, broadcast;
3876 
3877  /*
3878  * No matter the length of the subnet-mask option,
3879  * use only the first four octets. Note that
3880  * subnet-mask options longer than 4 octets are not
3881  * in conformance with RFC 2132, but servers with this
3882  * flaw do exist.
3883  */
3884  memcpy(netmask.iabuf, data.data, 4);
3885  netmask.len = 4;
3886  data_string_forget (&data, MDL);
3887 
3888  subnet = subnet_number (lease -> address, netmask);
3889  if (subnet.len) {
3890  client_envadd (client, prefix, "network_number",
3891  "%s", piaddr (subnet));
3892 
3894  lease -> options,
3896  if (!oc ||
3898  (&data, (struct packet *)0,
3899  (struct lease *)0, client,
3900  (struct option_state *)0,
3901  lease -> options,
3902  &global_scope, oc, MDL))) {
3903  broadcast = broadcast_addr (subnet, netmask);
3904  if (broadcast.len) {
3905  client_envadd (client,
3906  prefix, "broadcast_address",
3907  "%s", piaddr (broadcast));
3908  }
3909  }
3910  }
3911  }
3912  data_string_forget (&data, MDL);
3913  }
3914 
3915  if (lease->filename) {
3916  if (check_option_values(NULL, DHO_ROOT_PATH,
3917  lease->filename,
3918  strlen(lease->filename)) == 0) {
3919  client_envadd(client, prefix, "filename",
3920  "%s", lease->filename);
3921  } else {
3922  log_error("suspect value in %s "
3923  "option - discarded",
3924  lease->filename);
3925  }
3926  }
3927 
3928  if (lease->server_name) {
3929  if (check_option_values(NULL, DHO_HOST_NAME,
3930  lease->server_name,
3931  strlen(lease->server_name)) == 0 ) {
3932  client_envadd (client, prefix, "server_name",
3933  "%s", lease->server_name);
3934  } else {
3935  log_error("suspect value in %s "
3936  "option - discarded",
3937  lease->server_name);
3938  }
3939  }
3940 
3941  for (i = 0; i < lease -> options -> universe_count; i++) {
3942  option_space_foreach ((struct packet *)0, (struct lease *)0,
3943  client, (struct option_state *)0,
3944  lease -> options, &global_scope,
3945  universes [i],
3946  &es, client_option_envadd);
3947  }
3948  client_envadd (client, prefix, "expiry", "%d", (int)(lease -> expiry));
3949 }
3950 
3951 /*
3952  * Write out the environment variables for the objects that the
3953  * client requested. If the object was requested the variable will be:
3954  * requested_<option_name>=1
3955  * If it wasn't requested there won't be a variable.
3956  */
3958  struct client_state *client;
3959 {
3960  int i;
3961  struct option **req;
3962  char name[256];
3963  req = client->config->requested_options;
3964 
3965  if (req == NULL)
3966  return;
3967 
3968  for (i = 0 ; req[i] != NULL ; i++) {
3969  if ((req[i]->universe == &dhcp_universe) &&
3970  dhcp_option_ev_name(name, sizeof(name), req[i])) {
3971  client_envadd(client, "requested_", name, "%d", 1);
3972  }
3973  }
3974 }
3975 
3976 int script_go (client)
3977  struct client_state *client;
3978 {
3979  char *scriptName;
3980  char *argv [2];
3981  char **envp;
3982  char reason [] = "REASON=NBI";
3983  static char client_path [] = CLIENT_PATH;
3984  int i;
3985  struct string_list *sp, *next;
3986  int pid, wpid, wstatus;
3987 
3988  if (client)
3989  scriptName = client -> config -> script_name;
3990  else
3991  scriptName = top_level_config.script_name;
3992 
3993  envp = dmalloc (((client ? client -> envc : 2) +
3994  client_env_count + 2) * sizeof (char *), MDL);
3995  if (!envp) {
3996  log_error ("No memory for client script environment.");
3997  return 0;
3998  }
3999  i = 0;
4000  /* Copy out the environment specified on the command line,
4001  if any. */
4002  for (sp = client_env; sp; sp = sp -> next) {
4003  envp [i++] = sp -> string;
4004  }
4005  /* Copy out the environment specified by dhclient. */
4006  if (client) {
4007  for (sp = client -> env; sp; sp = sp -> next) {
4008  envp [i++] = sp -> string;
4009  }
4010  } else {
4011  envp [i++] = reason;
4012  }
4013  /* Set $PATH. */
4014  envp [i++] = client_path;
4015  envp [i] = (char *)0;
4016 
4017  argv [0] = scriptName;
4018  argv [1] = (char *)0;
4019 
4020  pid = fork ();
4021  if (pid < 0) {
4022  log_error ("fork: %m");
4023  wstatus = 0;
4024  } else if (pid) {
4025  do {
4026  wpid = wait (&wstatus);
4027  } while (wpid != pid && wpid > 0);
4028  if (wpid < 0) {
4029  log_error ("wait: %m");
4030  wstatus = 0;
4031  }
4032  } else {
4033  /* We don't want to pass an open file descriptor for
4034  * dhclient.leases when executing dhclient-script.
4035  */
4036  if (leaseFile != NULL)
4037  fclose(leaseFile);
4038  execve (scriptName, argv, envp);
4039  log_error ("execve (%s, ...): %m", scriptName);
4040  exit (0);
4041  }
4042 
4043  if (client) {
4044  for (sp = client -> env; sp; sp = next) {
4045  next = sp -> next;
4046  dfree (sp, MDL);
4047  }
4048  client -> env = (struct string_list *)0;
4049  client -> envc = 0;
4050  }
4051  dfree (envp, MDL);
4052  gettimeofday(&cur_tv, NULL);
4053  return (WIFEXITED (wstatus) ?
4054  WEXITSTATUS (wstatus) : -WTERMSIG (wstatus));
4055 }
4056 
4057 void client_envadd (struct client_state *client,
4058  const char *prefix, const char *name, const char *fmt, ...)
4059 {
4060  char spbuf [1024];
4061  char *s;
4062  unsigned len;
4063  struct string_list *val;
4064  va_list list;
4065 
4066  va_start (list, fmt);
4067  len = vsnprintf (spbuf, sizeof spbuf, fmt, list);
4068  va_end (list);
4069 
4070  val = dmalloc (strlen (prefix) + strlen (name) + 1 /* = */ +
4071  len + sizeof *val, MDL);
4072  if (!val)
4073  return;
4074  s = val -> string;
4075  strcpy (s, prefix);
4076  strcat (s, name);
4077  s += strlen (s);
4078  *s++ = '=';
4079  if (len >= sizeof spbuf) {
4080  va_start (list, fmt);
4081  vsnprintf (s, len + 1, fmt, list);
4082  va_end (list);
4083  } else
4084  strcpy (s, spbuf);
4085  val -> next = client -> env;
4086  client -> env = val;
4087  client -> envc++;
4088 }
4089 
4090 int dhcp_option_ev_name (buf, buflen, option)
4091  char *buf;
4092  size_t buflen;
4093  struct option *option;
4094 {
4095  int i, j;
4096  const char *s;
4097 
4098  j = 0;
4099  if (option -> universe != &dhcp_universe) {
4100  s = option -> universe -> name;
4101  i = 0;
4102  } else {
4103  s = option -> name;
4104  i = 1;
4105  }
4106 
4107  do {
4108  while (*s) {
4109  if (j + 1 == buflen)
4110  return 0;
4111  if (*s == '-')
4112  buf [j++] = '_';
4113  else
4114  buf [j++] = *s;
4115  ++s;
4116  }
4117  if (!i) {
4118  s = option -> name;
4119  if (j + 1 == buflen)
4120  return 0;
4121  buf [j++] = '_';
4122  }
4123  ++i;
4124  } while (i != 2);
4125 
4126  buf [j] = 0;
4127  return 1;
4128 }
4129 
4130 void go_daemon ()
4131 {
4132  static int state = 0;
4133  int pid;
4134 
4135  /* Don't become a daemon if the user requested otherwise. */
4136  if (no_daemon) {
4138  return;
4139  }
4140 
4141  /* Only do it once. */
4142  if (state)
4143  return;
4144  state = 1;
4145 
4146  /* Stop logging to stderr... */
4147  log_perror = 0;
4148 
4149  /* Become a daemon... */
4150  if ((pid = fork ()) < 0)
4151  log_fatal ("Can't fork daemon: %m");
4152  else if (pid)
4153  exit (0);
4154  /* Become session leader and get pid... */
4155  (void) setsid ();
4156 
4157  /* Close standard I/O descriptors. */
4158  (void) close(0);
4159  (void) close(1);
4160  (void) close(2);
4161 
4162  /* Reopen them on /dev/null. */
4163  (void) open("/dev/null", O_RDWR | O_CLOEXEC);
4164  (void) open("/dev/null", O_RDWR | O_CLOEXEC);
4165  (void) open("/dev/null", O_RDWR | O_CLOEXEC);
4166 
4168 
4169  IGNORE_RET (chdir("/"));
4170 }
4171 
4173 {
4174  FILE *pf;
4175  int pfdesc;
4176 
4177  /* nothing to do if the user doesn't want a pid file */
4178  if (no_pid_file == ISC_TRUE) {
4179  return;
4180  }
4181 
4182  pfdesc = open (path_dhclient_pid, O_CREAT | O_TRUNC | O_WRONLY | O_CLOEXEC, 0644);
4183 
4184  if (pfdesc < 0) {
4185  log_error ("Can't create %s: %m", path_dhclient_pid);
4186  return;
4187  }
4188 
4189  pf = fdopen (pfdesc, "we");
4190  if (!pf) {
4191  close(pfdesc);
4192  log_error ("Can't fdopen %s: %m", path_dhclient_pid);
4193  } else {
4194  fprintf (pf, "%ld\n", (long)getpid ());
4195  fclose (pf);
4196  }
4197 }
4198 
4200 {
4201  struct interface_info *ip;
4202  struct client_state *client;
4203 
4204  for (ip = interfaces; ip; ip = ip -> next) {
4205  for (client = ip -> client; client; client = client -> next) {
4206  switch (client -> state) {
4207  case S_SELECTING:
4208  cancel_timeout (send_discover, client);
4209  break;
4210 
4211  case S_BOUND:
4212  cancel_timeout (state_bound, client);
4213  break;
4214 
4215  case S_REBOOTING:
4216  case S_REQUESTING:
4217  case S_RENEWING:
4218  cancel_timeout (send_request, client);
4219  break;
4220 
4221  case S_INIT:
4222  case S_REBINDING:
4223  case S_STOPPED:
4224  case S_DECLINED:
4225  break;
4226  }
4227  client -> state = S_INIT;
4228  state_reboot (client);
4229  }
4230  }
4231 }
4232 
4233 void do_release(client)
4234  struct client_state *client;
4235 {
4236  struct data_string ds;
4237  struct option_cache *oc;
4238 
4239  /* Pick a random xid. */
4240  client -> xid = random ();
4241 
4242  /* is there even a lease to release? */
4243  if (client -> active) {
4244  /* Make a DHCPRELEASE packet, and set appropriate per-interface
4245  flags. */
4246  make_release (client, client -> active);
4247 
4248  memset (&ds, 0, sizeof ds);
4250  client -> active -> options,
4252  if (oc &&
4253  evaluate_option_cache (&ds, (struct packet *)0,
4254  (struct lease *)0, client,
4255  (struct option_state *)0,
4256  client -> active -> options,
4257  &global_scope, oc, MDL)) {
4258  if (ds.len > 3) {
4259  memcpy (client -> destination.iabuf,
4260  ds.data, 4);
4261  client -> destination.len = 4;
4262  } else
4263  client -> destination = iaddr_broadcast;
4264 
4265  data_string_forget (&ds, MDL);
4266  } else
4267  client -> destination = iaddr_broadcast;
4268  client -> first_sending = cur_time;
4269  client -> interval = client -> config -> initial_interval;
4270 
4271  /* Zap the medium list... */
4272  client -> medium = (struct string_list *)0;
4273 
4274  /* Send out the first and only DHCPRELEASE packet. */
4275  send_release (client);
4276 
4277  /* Do the client script RELEASE operation. */
4278  script_init (client,
4279  "RELEASE", (struct string_list *)0);
4280  if (client -> alias)
4281  script_write_params (client, "alias_",
4282  client -> alias);
4283  script_write_params (client, "old_", client -> active);
4284  script_write_requested(client);
4285  script_go (client);
4286  }
4287 
4288  /* Cancel any timeouts. */
4289  cancel_timeout (state_bound, client);
4290  cancel_timeout (send_discover, client);
4291  cancel_timeout (state_init, client);
4292  cancel_timeout (send_request, client);
4293  cancel_timeout (state_reboot, client);
4294  client -> state = S_STOPPED;
4295 }
4296 
4298 {
4299  do_release (interface -> client);
4300 
4301  return 1;
4302 }
4303 
4305 {
4306  struct interface_info *last, *ip;
4307  /* See if we can find the client from dummy_interfaces */
4308  last = 0;
4309  for (ip = dummy_interfaces; ip; ip = ip -> next) {
4310  if (!strcmp (ip -> name, tmp -> name)) {
4311  /* Remove from dummy_interfaces */
4312  if (last) {
4313  ip = (struct interface_info *)0;
4314  interface_reference (&ip, last -> next, MDL);
4315  interface_dereference (&last -> next, MDL);
4316  if (ip -> next) {
4317  interface_reference (&last -> next,
4318  ip -> next, MDL);
4319  interface_dereference (&ip -> next,
4320  MDL);
4321  }
4322  } else {
4323  ip = (struct interface_info *)0;
4324  interface_reference (&ip,
4326  interface_dereference (&dummy_interfaces, MDL);
4327  if (ip -> next) {
4328  interface_reference (&dummy_interfaces,
4329  ip -> next, MDL);
4330  interface_dereference (&ip -> next,
4331  MDL);
4332  }
4333  }
4334  /* Copy "client" to tmp */
4335  if (ip -> client) {
4336  tmp -> client = ip -> client;
4337  tmp -> client -> interface = tmp;
4338  }
4339  interface_dereference (&ip, MDL);
4340  break;
4341  }
4342  last = ip;
4343  }
4344  return 1;
4345 }
4346 
4347 isc_result_t dhclient_interface_startup_hook (struct interface_info *interface)
4348 {
4349  struct interface_info *ip;
4350  struct client_state *client;
4351 
4352  /* This code needs some rethinking. It doesn't test against
4353  a signal name, and it just kind of bulls into doing something
4354  that may or may not be appropriate. */
4355 
4356  if (interfaces) {
4357  interface_reference (&interface -> next, interfaces, MDL);
4358  interface_dereference (&interfaces, MDL);
4359  }
4360  interface_reference (&interfaces, interface, MDL);
4361 
4363 
4364  for (ip = interfaces; ip; ip = ip -> next) {
4365  /* If interfaces were specified, don't configure
4366  interfaces that weren't specified! */
4367  if (ip -> flags & INTERFACE_RUNNING ||
4368  (ip -> flags & (INTERFACE_REQUESTED |
4369  INTERFACE_AUTOMATIC)) !=
4371  continue;
4372  script_init (ip -> client,
4373  "PREINIT", (struct string_list *)0);
4374  if (ip -> client -> alias)
4375  script_write_params (ip -> client, "alias_",
4376  ip -> client -> alias);
4377  script_go (ip -> client);
4378  }
4379 
4382  : DISCOVER_RUNNING);
4383 
4384  for (ip = interfaces; ip; ip = ip -> next) {
4385  if (ip -> flags & INTERFACE_RUNNING)
4386  continue;
4387  ip -> flags |= INTERFACE_RUNNING;
4388  for (client = ip->client ; client ; client = client->next) {
4389  client->state = S_INIT;
4390  state_reboot(client);
4391  }
4392  }
4393  return ISC_R_SUCCESS;
4394 }
4395 
4396 /* The client should never receive a relay agent information option,
4397  so if it does, log it and discard it. */
4398 
4399 int parse_agent_information_option (packet, len, data)
4400  struct packet *packet;
4401  int len;
4402  u_int8_t *data;
4403 {
4404  return 1;
4405 }
4406 
4407 /* The client never sends relay agent information options. */
4408 
4409 unsigned cons_agent_information_options (cfg_options, outpacket,
4410  agentix, length)
4411  struct option_state *cfg_options;
4412  struct dhcp_packet *outpacket;
4413  unsigned agentix;
4414  unsigned length;
4415 {
4416  return length;
4417 }
4418 
4419 static void shutdown_exit (void *foo)
4420 {
4421  /* get rid of the pid if we can */
4422  if (no_pid_file == ISC_FALSE)
4423  (void) unlink(path_dhclient_pid);
4424  exit (0);
4425 }
4426 
4427 #if defined (NSUPDATE)
4428 /*
4429  * If the first query fails, the updater MUST NOT delete the DNS name. It
4430  * may be that the host whose lease on the server has expired has moved
4431  * to another network and obtained a lease from a different server,
4432  * which has caused the client's A RR to be replaced. It may also be
4433  * that some other client has been configured with a name that matches
4434  * the name of the DHCP client, and the policy was that the last client
4435  * to specify the name would get the name. In this case, the DHCID RR
4436  * will no longer match the updater's notion of the client-identity of
4437  * the host pointed to by the DNS name.
4438  * -- "Interaction between DHCP and DNS"
4439  */
4440 
4441 /* The first and second stages are pretty similar so we combine them */
4442 void
4443 client_dns_remove_action(dhcp_ddns_cb_t *ddns_cb,
4444  isc_result_t eresult)
4445 {
4446 
4447  isc_result_t result;
4448 
4449  if ((eresult == ISC_R_SUCCESS) &&
4450  (ddns_cb->state == DDNS_STATE_REM_FW_YXDHCID)) {
4451  /* Do the second stage of the FWD removal */
4452  ddns_cb->state = DDNS_STATE_REM_FW_NXRR;
4453 
4454  result = ddns_modify_fwd(ddns_cb, MDL);
4455  if (result == ISC_R_SUCCESS) {
4456  return;
4457  }
4458  }
4459 
4460  /* If we are done or have an error clean up */
4461  dhclient_ddns_cb_free(ddns_cb, MDL);
4462  return;
4463 }
4464 
4465 void
4466 client_dns_remove(struct client_state *client,
4467  struct iaddr *addr)
4468 {
4470  isc_result_t result;
4471 
4472  /* if we have an old ddns request for this client, cancel it */
4473  if (client->ddns_cb != NULL) {
4474  ddns_cancel(client->ddns_cb, MDL);
4475  client->ddns_cb = NULL;
4476  }
4477 
4478  ddns_cb = ddns_cb_alloc(MDL);
4479  if (ddns_cb != NULL) {
4480  ddns_cb->address = *addr;
4481  ddns_cb->timeout = 0;
4482 
4483  ddns_cb->state = DDNS_STATE_REM_FW_YXDHCID;
4484  ddns_cb->flags = DDNS_UPDATE_ADDR;
4485  ddns_cb->cur_func = client_dns_remove_action;
4486 
4487  result = client_dns_update(client, ddns_cb);
4488 
4489  if (result != ISC_R_TIMEDOUT) {
4490  dhclient_ddns_cb_free(ddns_cb, MDL);
4491  }
4492  }
4493 }
4494 #endif
4495 
4497  control_object_state_t newstate)
4498 {
4499  struct interface_info *ip;
4500  struct client_state *client;
4501  struct timeval tv;
4502 
4503  if (newstate == server_shutdown) {
4504  /* Re-entry */
4505  if (shutdown_signal == SIGUSR1)
4506  return ISC_R_SUCCESS;
4507  /* Log shutdown on signal. */
4508  if ((shutdown_signal == SIGINT) ||
4509  (shutdown_signal == SIGTERM)) {
4510  log_info("Received signal %d, initiating shutdown.",
4511  shutdown_signal);
4512  }
4513  /* Mark it was called. */
4514  shutdown_signal = SIGUSR1;
4515  }
4516 
4517  /* Do the right thing for each interface. */
4518  for (ip = interfaces; ip; ip = ip -> next) {
4519  for (client = ip -> client; client; client = client -> next) {
4520  switch (newstate) {
4521  case server_startup:
4522  return ISC_R_SUCCESS;
4523 
4524  case server_running:
4525  return ISC_R_SUCCESS;
4526 
4527  case server_shutdown:
4528  if (client -> active &&
4529  client -> active -> expiry > cur_time) {
4530 #if defined (NSUPDATE)
4531  if (client->config->do_forward_update) {
4532  client_dns_remove(client,
4533  &client->active->address);
4534  }
4535 #endif
4536  do_release (client);
4537  }
4538  break;
4539 
4540  case server_hibernate:
4541  state_stop (client);
4542  break;
4543 
4544  case server_awaken:
4545  state_reboot (client);
4546  break;
4547  }
4548  }
4549  }
4550 
4551  if (newstate == server_shutdown) {
4552  tv.tv_sec = cur_tv.tv_sec;
4553  tv.tv_usec = cur_tv.tv_usec + 1;
4554  add_timeout(&tv, shutdown_exit, 0, 0, 0);
4555  }
4556  return ISC_R_SUCCESS;
4557 }
4558 
4559 #if defined (NSUPDATE)
4560 /*
4561  * Called after a timeout if the DNS update failed on the previous try.
4562  * Starts the retry process. If the retry times out it will schedule
4563  * this routine to run again after a 10x wait.
4564  */
4565 void
4566 client_dns_update_timeout (void *cp)
4567 {
4568  dhcp_ddns_cb_t *ddns_cb = (dhcp_ddns_cb_t *)cp;
4569  struct client_state *client = (struct client_state *)ddns_cb->lease;
4570  isc_result_t status = ISC_R_FAILURE;
4571 
4572  if ((client != NULL) &&
4573  ((client->active != NULL) ||
4574  (client->active_lease != NULL)))
4575  status = client_dns_update(client, ddns_cb);
4576 
4577  /*
4578  * A status of timedout indicates that we started the update and
4579  * have released control of the control block. Any other status
4580  * indicates that we should clean up the control block. We either
4581  * got a success which indicates that we didn't really need to
4582  * send an update or some other error in which case we weren't able
4583  * to start the update process. In both cases we still own
4584  * the control block and should free it.
4585  */
4586  if (status != ISC_R_TIMEDOUT) {
4587  dhclient_ddns_cb_free(ddns_cb, MDL);
4588  }
4589 }
4590 
4591 /*
4592  * If the first query succeeds, the updater can conclude that it
4593  * has added a new name whose only RRs are the A and DHCID RR records.
4594  * The A RR update is now complete (and a client updater is finished,
4595  * while a server might proceed to perform a PTR RR update).
4596  * -- "Interaction between DHCP and DNS"
4597  *
4598  * If the second query succeeds, the updater can conclude that the current
4599  * client was the last client associated with the domain name, and that
4600  * the name now contains the updated A RR. The A RR update is now
4601  * complete (and a client updater is finished, while a server would
4602  * then proceed to perform a PTR RR update).
4603  * -- "Interaction between DHCP and DNS"
4604  *
4605  * If the second query fails with NXRRSET, the updater must conclude
4606  * that the client's desired name is in use by another host. At this
4607  * juncture, the updater can decide (based on some administrative
4608  * configuration outside of the scope of this document) whether to let
4609  * the existing owner of the name keep that name, and to (possibly)
4610  * perform some name disambiguation operation on behalf of the current
4611  * client, or to replace the RRs on the name with RRs that represent
4612  * the current client. If the configured policy allows replacement of
4613  * existing records, the updater submits a query that deletes the
4614  * existing A RR and the existing DHCID RR, adding A and DHCID RRs that
4615  * represent the IP address and client-identity of the new client.
4616  * -- "Interaction between DHCP and DNS"
4617  */
4618 
4619 /* The first and second stages are pretty similar so we combine them */
4620 void
4621 client_dns_update_action(dhcp_ddns_cb_t *ddns_cb,
4622  isc_result_t eresult)
4623 {
4624  isc_result_t result;
4625  struct timeval tv;
4626 
4627  switch(eresult) {
4628  case ISC_R_SUCCESS:
4629  default:
4630  /* Either we succeeded or broke in a bad way, clean up */
4631  break;
4632 
4633  case DNS_R_YXRRSET:
4634  /*
4635  * This is the only difference between the two stages,
4636  * check to see if it is the first stage, in which case
4637  * start the second stage
4638  */
4639  if (ddns_cb->state == DDNS_STATE_ADD_FW_NXDOMAIN) {
4640  ddns_cb->state = DDNS_STATE_ADD_FW_YXDHCID;
4641  ddns_cb->cur_func = client_dns_update_action;
4642 
4643  result = ddns_modify_fwd(ddns_cb, MDL);
4644  if (result == ISC_R_SUCCESS) {
4645  return;
4646  }
4647  }
4648  break;
4649 
4650  case ISC_R_TIMEDOUT:
4651  /*
4652  * We got a timeout response from the DNS module. Schedule
4653  * another attempt for later. We forget the name, dhcid and
4654  * zone so if it gets changed we will get the new information.
4655  */
4656  data_string_forget(&ddns_cb->fwd_name, MDL);
4657  data_string_forget(&ddns_cb->dhcid, MDL);
4658  if (ddns_cb->zone != NULL) {
4659  forget_zone((struct dns_zone **)&ddns_cb->zone);
4660  }
4661 
4662  /* Reset to doing the first stage */
4663  ddns_cb->state = DDNS_STATE_ADD_FW_NXDOMAIN;
4664  ddns_cb->cur_func = client_dns_update_action;
4665 
4666  /* and update our timer */
4667  if (ddns_cb->timeout < 3600)
4668  ddns_cb->timeout *= 10;
4669  tv.tv_sec = cur_tv.tv_sec + ddns_cb->timeout;
4670  tv.tv_usec = cur_tv.tv_usec;
4672  ddns_cb, NULL, NULL);
4673  return;
4674  }
4675 
4676  dhclient_ddns_cb_free(ddns_cb, MDL);
4677  return;
4678 }
4679 
4680 /* See if we should do a DNS update, and if so, do it. */
4681 
4682 isc_result_t
4683 client_dns_update(struct client_state *client, dhcp_ddns_cb_t *ddns_cb)
4684 {
4685  struct data_string client_identifier;
4686  struct option_cache *oc;
4687  int ignorep;
4688  int result;
4689  int ddns_v4_type;
4690  isc_result_t rcode;
4691 
4692  /* If we didn't send an FQDN option, we certainly aren't going to
4693  be doing an update. */
4694  if (!client -> sent_options)
4695  return ISC_R_SUCCESS;
4696 
4697  /* If we don't have a lease, we can't do an update. */
4698  if ((client->active == NULL) && (client->active_lease == NULL))
4699  return ISC_R_SUCCESS;
4700 
4701  /* If we set the no client update flag, don't do the update. */
4702  if ((oc = lookup_option (&fqdn_universe, client -> sent_options,
4704  evaluate_boolean_option_cache (&ignorep, (struct packet *)0,
4705  (struct lease *)0, client,
4706  client -> sent_options,
4707  (struct option_state *)0,
4708  &global_scope, oc, MDL))
4709  return ISC_R_SUCCESS;
4710 
4711  /* If we set the "server, please update" flag, or didn't set it
4712  to false, don't do the update. */
4713  if (!(oc = lookup_option (&fqdn_universe, client -> sent_options,
4714  FQDN_SERVER_UPDATE)) ||
4715  evaluate_boolean_option_cache (&ignorep, (struct packet *)0,
4716  (struct lease *)0, client,
4717  client -> sent_options,
4718  (struct option_state *)0,
4719  &global_scope, oc, MDL))
4720  return ISC_R_SUCCESS;
4721 
4722  /* If no FQDN option was supplied, don't do the update. */
4723  if (!(oc = lookup_option (&fqdn_universe, client -> sent_options,
4724  FQDN_FQDN)) ||
4725  !evaluate_option_cache (&ddns_cb->fwd_name, (struct packet *)0,
4726  (struct lease *)0, client,
4727  client -> sent_options,
4728  (struct option_state *)0,
4729  &global_scope, oc, MDL))
4730  return ISC_R_SUCCESS;
4731 
4732  /*
4733  * Construct the DHCID value for use in the DDNS update process
4734  * We have the newer standard version and the older interim version
4735  * chosen by the '-I' option. The interim version is left as is
4736  * for backwards compatibility. The standard version is based on
4737  * RFC 4701 section 3.3
4738  */
4739 
4740  result = 0;
4741  POST(result);
4742  memset(&client_identifier, 0, sizeof(client_identifier));
4743 
4744  if (std_dhcid == 1) {
4745  /* standard style */
4746  ddns_cb->dhcid_class = dns_rdatatype_dhcid;
4747  ddns_v4_type = 1;
4748  } else {
4749  /* interim style */
4750  ddns_cb->dhcid_class = dns_rdatatype_txt;
4751  /* for backwards compatibility */
4752  ddns_v4_type = DHO_DHCP_CLIENT_IDENTIFIER;
4753  }
4754  if (client->active_lease != NULL) {
4755  /* V6 request, get the client identifier, then
4756  * construct the dhcid for either standard
4757  * or interim */
4758  if (((oc = lookup_option(&dhcpv6_universe,
4759  client->sent_options,
4760  D6O_CLIENTID)) != NULL) &&
4761  evaluate_option_cache(&client_identifier, NULL,
4762  NULL, client,
4763  client->sent_options, NULL,
4764  &global_scope, oc, MDL)) {
4765  result = get_dhcid(ddns_cb, 2,
4766  client_identifier.data,
4767  client_identifier.len);
4768  data_string_forget(&client_identifier, MDL);
4769  } else
4770  log_fatal("Impossible condition at %s:%d.", MDL);
4771  } else {
4772  /*
4773  * V4 request, use the client id if there is one or the
4774  * mac address if there isn't. If we have a client id
4775  * we check to see if it is an embedded DUID.
4776  */
4777  if (((oc = lookup_option(&dhcp_universe,
4778  client->sent_options,
4779  DHO_DHCP_CLIENT_IDENTIFIER)) != NULL) &&
4780  evaluate_option_cache(&client_identifier, NULL,
4781  NULL, client,
4782  client->sent_options, NULL,
4783  &global_scope, oc, MDL)) {
4784  if ((std_dhcid == 1) && (duid_v4 == 1) &&
4785  (client_identifier.data[0] == 255)) {
4786  /*
4787  * This appears to be an embedded DUID,
4788  * extract it and treat it as such
4789  */
4790  if (client_identifier.len <= 5)
4791  log_fatal("Impossible condition at %s:%d.",
4792  MDL);
4793  result = get_dhcid(ddns_cb, 2,
4794  client_identifier.data + 5,
4795  client_identifier.len - 5);
4796  } else {
4797  result = get_dhcid(ddns_cb, ddns_v4_type,
4798  client_identifier.data,
4799  client_identifier.len);
4800  }
4801  data_string_forget(&client_identifier, MDL);
4802  } else
4803  result = get_dhcid(ddns_cb, 0,
4804  client->interface->hw_address.hbuf,
4805  client->interface->hw_address.hlen);
4806  }
4807 
4808  if (!result) {
4809  return ISC_R_SUCCESS;
4810  }
4811 
4812  /*
4813  * Perform updates.
4814  */
4815  if (ddns_cb->fwd_name.len && ddns_cb->dhcid.len) {
4816  rcode = ddns_modify_fwd(ddns_cb, MDL);
4817  } else
4818  rcode = ISC_R_FAILURE;
4819 
4820  /*
4821  * A success from the modify routine means we are performing
4822  * async processing, for which we use the timedout error message.
4823  */
4824  if (rcode == ISC_R_SUCCESS) {
4825  rcode = ISC_R_TIMEDOUT;
4826  }
4827 
4828  return rcode;
4829 }
4830 
4831 
4832 /*
4833  * Schedule the first update. They will continue to retry occasionally
4834  * until they no longer time out (or fail).
4835  */
4836 void
4838  struct iaddr *addr,
4839  int offset)
4840 {
4841  dhcp_ddns_cb_t *ddns_cb;
4842  struct timeval tv;
4843 
4844  if (!client->config->do_forward_update)
4845  return;
4846 
4847  /* cancel any outstanding ddns requests */
4848  if (client->ddns_cb != NULL) {
4849  ddns_cancel(client->ddns_cb, MDL);
4850  client->ddns_cb = NULL;
4851  }
4852 
4853  ddns_cb = ddns_cb_alloc(MDL);
4854 
4855  if (ddns_cb != NULL) {
4856  ddns_cb->lease = (void *)client;
4857  ddns_cb->address = *addr;
4858  ddns_cb->timeout = 1;
4859 
4860  /*
4861  * XXX: DNS TTL is a problem we need to solve properly.
4862  * Until that time, 300 is a placeholder default for
4863  * something that is less insane than a value scaled
4864  * by lease timeout.
4865  */
4866  ddns_cb->ttl = 300;
4867 
4868  ddns_cb->state = DDNS_STATE_ADD_FW_NXDOMAIN;
4869  ddns_cb->cur_func = client_dns_update_action;
4871 
4872  client->ddns_cb = ddns_cb;
4873  tv.tv_sec = cur_tv.tv_sec + offset;
4874  tv.tv_usec = cur_tv.tv_usec;
4876  ddns_cb, NULL, NULL);
4877  } else {
4878  log_error("Unable to allocate dns update state for %s",
4879  piaddr(*addr));
4880  }
4881 }
4882 #endif
4883 
4884 void
4886 {
4887  struct servent *ent;
4888 
4889  if (path_dhclient_pid == NULL)
4891  if (path_dhclient_db == NULL)
4893 
4894  /* Default to the DHCP/BOOTP port. */
4895  if (!local_port) {
4896  /* If we're faking a relay agent, and we're not using loopback,
4897  use the server port, not the client port. */
4898  if (mockup_relay && giaddr.s_addr != htonl(INADDR_LOOPBACK)) {
4899  local_port = htons(67);
4900  } else {
4901  ent = getservbyname("dhcpc", "udp");
4902  if (ent == NULL)
4903  ent = getservbyname("bootpc", "udp");
4904  if (ent == NULL)
4905  local_port = htons(68);
4906  else
4907  local_port = ent->s_port;
4908 #ifndef __CYGWIN32__
4909  endservent ();
4910 #endif
4911  }
4912  }
4913 
4914  /* If we're faking a relay agent, and we're not using loopback,
4915  we're using the server port, not the client port. */
4916  if (mockup_relay && giaddr.s_addr != htonl(INADDR_LOOPBACK)) {
4918  } else
4919  remote_port = htons(ntohs(local_port) - 1); /* XXX */
4920 }
4921 
4922 /*
4923  * The following routines are used to check that certain
4924  * strings are reasonable before we pass them to the scripts.
4925  * This avoids some problems with scripts treating the strings
4926  * as commands - see ticket 23722
4927  * The domain checking code should be done as part of assembling
4928  * the string but we are doing it here for now due to time
4929  * constraints.
4930  */
4931 
4932 static int check_domain_name(const char *ptr, size_t len, int dots)
4933 {
4934  const char *p;
4935 
4936  /* not empty or complete length not over 255 characters */
4937  if ((len == 0) || (len > 256))
4938  return(-1);
4939 
4940  /* consists of [[:alnum:]-]+ labels separated by [.] */
4941  /* a [_] is against RFC but seems to be "widely used"... */
4942  for (p=ptr; (*p != 0) && (len-- > 0); p++) {
4943  if ((*p == '-') || (*p == '_')) {
4944  /* not allowed at begin or end of a label */
4945  if (((p - ptr) == 0) || (len == 0) || (p[1] == '.'))
4946  return(-1);
4947  } else if (*p == '.') {
4948  /* each label has to be 1-63 characters;
4949  we allow [.] at the end ('foo.bar.') */
4950  size_t d = p - ptr;
4951  if ((d <= 0) || (d >= 64))
4952  return(-1);
4953  ptr = p + 1; /* jump to the next label */
4954  if ((dots > 0) && (len > 0))
4955  dots--;
4956  } else if (isalnum((unsigned char)*p) == 0) {
4957  /* also numbers at the begin are fine */
4958  return(-1);
4959  }
4960  }
4961  return(dots ? -1 : 0);
4962 }
4963 
4964 static int check_domain_name_list(const char *ptr, size_t len, int dots)
4965 {
4966  const char *p;
4967  int ret = -1; /* at least one needed */
4968 
4969  if ((ptr == NULL) || (len == 0))
4970  return(-1);
4971 
4972  for (p=ptr; (*p != 0) && (len > 0); p++, len--) {
4973  if (*p != ' ')
4974  continue;
4975  if (p > ptr) {
4976  if (check_domain_name(ptr, p - ptr, dots) != 0)
4977  return(-1);
4978  ret = 0;
4979  }
4980  ptr = p + 1;
4981  }
4982  if (p > ptr)
4983  return(check_domain_name(ptr, p - ptr, dots));
4984  else
4985  return(ret);
4986 }
4987 
4988 static int check_option_values(struct universe *universe,
4989  unsigned int opt,
4990  const char *ptr,
4991  size_t len)
4992 {
4993  if (ptr == NULL)
4994  return(-1);
4995 
4996  /* just reject options we want to protect, will be escaped anyway */
4997  if ((universe == NULL) || (universe == &dhcp_universe)) {
4998  switch(opt) {
4999  case DHO_DOMAIN_NAME:
5000 #ifdef ACCEPT_LIST_IN_DOMAIN_NAME
5001  return check_domain_name_list(ptr, len, 0);
5002 #else
5003  return check_domain_name(ptr, len, 0);
5004 #endif
5005  case DHO_HOST_NAME:
5006  case DHO_NIS_DOMAIN:
5007  case DHO_NETBIOS_SCOPE:
5008  return check_domain_name(ptr, len, 0);
5009  break;
5010  case DHO_DOMAIN_SEARCH:
5011  return check_domain_name_list(ptr, len, 0);
5012  break;
5013  case DHO_ROOT_PATH:
5014  if (len == 0)
5015  return(-1);
5016  for (; (*ptr != 0) && (len-- > 0); ptr++) {
5017  if(!(isalnum((unsigned char)*ptr) ||
5018  *ptr == '#' || *ptr == '%' ||
5019  *ptr == '+' || *ptr == '-' ||
5020  *ptr == '_' || *ptr == ':' ||
5021  *ptr == '.' || *ptr == ',' ||
5022  *ptr == '@' || *ptr == '~' ||
5023  *ptr == '\\' || *ptr == '/' ||
5024  *ptr == '[' || *ptr == ']' ||
5025  *ptr == '=' || *ptr == ' '))
5026  return(-1);
5027  }
5028  return(0);
5029  break;
5030  }
5031  }
5032 
5033 #ifdef DHCPv6
5034  if (universe == &dhcpv6_universe) {
5035  switch(opt) {
5036  case D6O_SIP_SERVERS_DNS:
5037  case D6O_DOMAIN_SEARCH:
5038  case D6O_NIS_DOMAIN_NAME:
5039  case D6O_NISP_DOMAIN_NAME:
5040  return check_domain_name_list(ptr, len, 0);
5041  break;
5042  }
5043  }
5044 #endif
5045 
5046  return(0);
5047 }
5048 
5049 static void
5050 add_reject(struct packet *packet) {
5051  struct iaddrmatchlist *list;
5052 
5053  list = dmalloc(sizeof(struct iaddrmatchlist), MDL);
5054  if (!list)
5055  log_fatal ("no memory for reject list!");
5056 
5057  /*
5058  * client_addr is misleading - it is set to source address in common
5059  * code.
5060  */
5061  list->match.addr = packet->client_addr;
5062  /* Set mask to indicate host address. */
5063  list->match.mask.len = list->match.addr.len;
5064  memset(list->match.mask.iabuf, 0xff, sizeof(list->match.mask.iabuf));
5065 
5066  /* Append to reject list for the source interface. */
5067  list->next = packet->interface->client->config->reject_list;
5068  packet->interface->client->config->reject_list = list;
5069 
5070  /*
5071  * We should inform user that we won't be accepting this server
5072  * anymore.
5073  */
5074  log_info("Server added to list of rejected servers.");
5075 }
5076 
5077 /* Wrapper function around common ddns_cb_free function that ensures
5078  * we set the client_state pointer to the control block to NULL. */
5079 static void
5080 dhclient_ddns_cb_free(dhcp_ddns_cb_t *ddns_cb, char* file, int line) {
5081  if (ddns_cb) {
5082  struct client_state *client = (struct client_state *)ddns_cb->lease;
5083  if (client != NULL) {
5084  client->ddns_cb = NULL;
5085  }
5086 
5087  ddns_cb_free(ddns_cb, file, line);
5088  }
5089 }
#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:1426
#define _PATH_DHCLIENT_CONF
Definition: dhcpd.h:1533
void(* dhcpv6_packet_handler)(struct interface_info *, const char *, int, int, const struct iaddr *, isc_boolean_t)
void send_discover(void *cpp)
Definition: dhclient.c:2317
void unbill_class(struct lease *lease)
Definition: dhclient.c:1284
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 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:2821
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:4233
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:3253
#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:1980
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:2238
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
void(* bootp_packet_handler)(struct interface_info *, struct dhcp_packet *, unsigned, unsigned int, struct iaddr, struct hardware *)
Definition: discover.c:58
#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:2722
#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:3250
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:2115
#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:3976
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:3621
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:3763
#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:1511
unsigned cons_agent_information_options(struct option_state *cfg_options, struct dhcp_packet *outpacket, unsigned agentix, unsigned length)
Definition: dhclient.c:4409
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:1841
#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:2745
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:4057
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:3762
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
struct data_string fwd_name
Definition: dhcpd.h:1748
void write_client_pid_file()
Definition: dhclient.c:4172
#define D6O_CLIENTID
Definition: dhcp6.h:31
void state_panic(void *cpp)
Definition: dhclient.c:2430
#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__
void(* v6_handler)(struct packet *, struct client_state *)
Definition: dhcpd.h:1300
#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:3386
struct executable_statement * statements
Definition: dhcpd.h:926
void state_init(void *cpp)
Definition: dhclient.c:1389
#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:1289
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:3035
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:4347
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:3796
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:1830
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:1278
void script_init(struct client_state *client, const char *reason, struct string_list *medium)
Definition: dhclient.c:3765
#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:3124
#define HTYPE_INFINIBAND
Definition: dhcp.h:79
void bind_lease(struct client_state *client)
Definition: dhclient.c:1680
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:1824
struct group * next
Definition: dhcpd.h:919
void putULong(unsigned char *, u_int32_t)
Definition: convert.c:70
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
void send_request(void *cpp)
Definition: dhclient.c:2540
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:4304
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:1347
int check_collection(struct packet *packet, struct lease *lease, struct collection *collection)
Definition: dhclient.c:1270
void destroy_client_lease(struct client_lease *lease)
Definition: dhclient.c:3239
void db_startup(int testp)
Definition: dhclient.c:1836
int parse_agent_information_option(struct packet *packet, int len, u_int8_t *data)
Definition: dhclient.c:4399
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
isc_result_t(* dhcp_interface_startup_hook)(struct interface_info *)
Definition: discover.c:50
#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
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:2971
int leases_written
Definition: dhclient.c:3251
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:1755
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:3183
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:1264
void script_write_requested(struct client_state *client)
Definition: dhclient.c:3957
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:1819
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:4885
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:4090
#define D6O_IA_PD
Definition: dhcp6.h:55
int(* dhcp_interface_discovery_hook)(struct interface_info *)
Definition: discover.c:49
void go_daemon()
Definition: dhclient.c:4130
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:1874
#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:4496
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:3315
#define DDNS_STATE_REM_FW_NXRR
Definition: dhcpd.h:1733
int(* dhcp_interface_shutdown_hook)(struct interface_info *)
Definition: discover.c:51
#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:4297
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:3837
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:4199
void state_stop(void *cpp)
Definition: dhclient.c:1797
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:3494
int bootp_broadcast_always
Definition: dhcpd.h:1236