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