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