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