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