Import dhcpcd-10.0.2 with the following changes:
[dragonfly.git] / contrib / dhcpcd / src / dhcpcd.c
blobe06733d318bc30ad6f5af7af849a2067224b9d4f
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3 * dhcpcd - DHCP client daemon
4 * Copyright (c) 2006-2023 Roy Marples <roy@marples.name>
5 * All rights reserved
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
29 static const char dhcpcd_copyright[] = "Copyright (c) 2006-2023 Roy Marples";
31 #include <sys/file.h>
32 #include <sys/ioctl.h>
33 #include <sys/socket.h>
34 #include <sys/stat.h>
35 #include <sys/time.h>
36 #include <sys/types.h>
37 #include <sys/uio.h>
38 #include <sys/wait.h>
40 #include <ctype.h>
41 #include <errno.h>
42 #include <fcntl.h>
43 #include <getopt.h>
44 #include <limits.h>
45 #include <paths.h>
46 #include <signal.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <syslog.h>
51 #include <unistd.h>
52 #include <time.h>
54 #include "config.h"
55 #include "arp.h"
56 #include "common.h"
57 #include "control.h"
58 #include "dev.h"
59 #include "dhcp-common.h"
60 #include "dhcpcd.h"
61 #include "dhcp.h"
62 #include "dhcp6.h"
63 #include "duid.h"
64 #include "eloop.h"
65 #include "if.h"
66 #include "if-options.h"
67 #include "ipv4.h"
68 #include "ipv4ll.h"
69 #include "ipv6.h"
70 #include "ipv6nd.h"
71 #include "logerr.h"
72 #include "privsep.h"
73 #include "script.h"
75 #ifdef HAVE_CAPSICUM
76 #include <sys/capsicum.h>
77 #endif
78 #ifdef HAVE_UTIL_H
79 #include <util.h>
80 #endif
82 #ifdef USE_SIGNALS
83 const int dhcpcd_signals[] = {
84 SIGTERM,
85 SIGINT,
86 SIGALRM,
87 SIGHUP,
88 SIGUSR1,
89 SIGUSR2,
90 SIGCHLD,
92 const size_t dhcpcd_signals_len = __arraycount(dhcpcd_signals);
94 const int dhcpcd_signals_ignore[] = {
95 SIGPIPE,
97 const size_t dhcpcd_signals_ignore_len = __arraycount(dhcpcd_signals_ignore);
98 #endif
100 const char *dhcpcd_default_script = SCRIPT;
102 static void
103 usage(void)
106 printf("usage: "PACKAGE"\t[-146ABbDdEGgHJKLMNPpqTV]\n"
107 "\t\t[-C, --nohook hook] [-c, --script script]\n"
108 "\t\t[-e, --env value] [-F, --fqdn FQDN] [-f, --config file]\n"
109 "\t\t[-h, --hostname hostname] [-I, --clientid clientid]\n"
110 "\t\t[-i, --vendorclassid vendorclassid] [-j, --logfile logfile]\n"
111 "\t\t[-l, --leasetime seconds] [-m, --metric metric]\n"
112 "\t\t[-O, --nooption option] [-o, --option option]\n"
113 "\t\t[-Q, --require option] [-r, --request address]\n"
114 "\t\t[-S, --static value]\n"
115 "\t\t[-s, --inform address[/cidr[/broadcast_address]]]\n [--inform6]"
116 "\t\t[-t, --timeout seconds] [-u, --userclass class]\n"
117 "\t\t[-v, --vendor code, value] [-W, --whitelist address[/cidr]] [-w]\n"
118 "\t\t[--waitip [4 | 6]] [-y, --reboot seconds]\n"
119 "\t\t[-X, --blacklist address[/cidr]] [-Z, --denyinterfaces pattern]\n"
120 "\t\t[-z, --allowinterfaces pattern] [--inactive] [interface] [...]\n"
121 " "PACKAGE"\t-n, --rebind [interface]\n"
122 " "PACKAGE"\t-k, --release [interface]\n"
123 " "PACKAGE"\t-U, --dumplease interface\n"
124 " "PACKAGE"\t--version\n"
125 " "PACKAGE"\t-x, --exit [interface]\n");
128 static void
129 free_globals(struct dhcpcd_ctx *ctx)
131 struct dhcp_opt *opt;
133 if (ctx->ifac) {
134 for (; ctx->ifac > 0; ctx->ifac--)
135 free(ctx->ifav[ctx->ifac - 1]);
136 free(ctx->ifav);
137 ctx->ifav = NULL;
139 if (ctx->ifdc) {
140 for (; ctx->ifdc > 0; ctx->ifdc--)
141 free(ctx->ifdv[ctx->ifdc - 1]);
142 free(ctx->ifdv);
143 ctx->ifdv = NULL;
145 if (ctx->ifcc) {
146 for (; ctx->ifcc > 0; ctx->ifcc--)
147 free(ctx->ifcv[ctx->ifcc - 1]);
148 free(ctx->ifcv);
149 ctx->ifcv = NULL;
152 #ifdef INET
153 if (ctx->dhcp_opts) {
154 for (opt = ctx->dhcp_opts;
155 ctx->dhcp_opts_len > 0;
156 opt++, ctx->dhcp_opts_len--)
157 free_dhcp_opt_embenc(opt);
158 free(ctx->dhcp_opts);
159 ctx->dhcp_opts = NULL;
161 #endif
162 #ifdef INET6
163 if (ctx->nd_opts) {
164 for (opt = ctx->nd_opts;
165 ctx->nd_opts_len > 0;
166 opt++, ctx->nd_opts_len--)
167 free_dhcp_opt_embenc(opt);
168 free(ctx->nd_opts);
169 ctx->nd_opts = NULL;
171 #ifdef DHCP6
172 if (ctx->dhcp6_opts) {
173 for (opt = ctx->dhcp6_opts;
174 ctx->dhcp6_opts_len > 0;
175 opt++, ctx->dhcp6_opts_len--)
176 free_dhcp_opt_embenc(opt);
177 free(ctx->dhcp6_opts);
178 ctx->dhcp6_opts = NULL;
180 #endif
181 #endif
182 if (ctx->vivso) {
183 for (opt = ctx->vivso;
184 ctx->vivso_len > 0;
185 opt++, ctx->vivso_len--)
186 free_dhcp_opt_embenc(opt);
187 free(ctx->vivso);
188 ctx->vivso = NULL;
192 static void
193 handle_exit_timeout(void *arg)
195 struct dhcpcd_ctx *ctx;
197 ctx = arg;
198 logerrx("timed out");
199 if (!(ctx->options & DHCPCD_MANAGER)) {
200 struct interface *ifp;
202 TAILQ_FOREACH(ifp, ctx->ifaces, next) {
203 if (ifp->active == IF_ACTIVE_USER)
204 script_runreason(ifp, "STOPPED");
206 eloop_exit(ctx->eloop, EXIT_FAILURE);
207 return;
209 ctx->options |= DHCPCD_NOWAITIP;
210 dhcpcd_daemonise(ctx);
213 static const char *
214 dhcpcd_af(int af)
217 switch (af) {
218 case AF_UNSPEC:
219 return "IP";
220 case AF_INET:
221 return "IPv4";
222 case AF_INET6:
223 return "IPv6";
224 default:
225 return NULL;
230 dhcpcd_ifafwaiting(const struct interface *ifp)
232 unsigned long long opts;
233 bool foundany = false;
235 if (ifp->active != IF_ACTIVE_USER)
236 return AF_MAX;
238 #define DHCPCD_WAITALL (DHCPCD_WAITIP4 | DHCPCD_WAITIP6)
239 opts = ifp->options->options;
240 #ifdef INET
241 if (opts & DHCPCD_WAITIP4 ||
242 (opts & DHCPCD_WAITIP && !(opts & DHCPCD_WAITALL)))
244 bool foundaddr = ipv4_hasaddr(ifp);
246 if (opts & DHCPCD_WAITIP4 && !foundaddr)
247 return AF_INET;
248 if (foundaddr)
249 foundany = true;
251 #endif
252 #ifdef INET6
253 if (opts & DHCPCD_WAITIP6 ||
254 (opts & DHCPCD_WAITIP && !(opts & DHCPCD_WAITALL)))
256 bool foundaddr = ipv6_hasaddr(ifp);
258 if (opts & DHCPCD_WAITIP6 && !foundaddr)
259 return AF_INET6;
260 if (foundaddr)
261 foundany = true;
263 #endif
265 if (opts & DHCPCD_WAITIP && !(opts & DHCPCD_WAITALL) && !foundany)
266 return AF_UNSPEC;
267 return AF_MAX;
271 dhcpcd_afwaiting(const struct dhcpcd_ctx *ctx)
273 unsigned long long opts;
274 const struct interface *ifp;
275 int af;
277 if (!(ctx->options & DHCPCD_WAITOPTS))
278 return AF_MAX;
280 opts = ctx->options;
281 TAILQ_FOREACH(ifp, ctx->ifaces, next) {
282 #ifdef INET
283 if (opts & (DHCPCD_WAITIP | DHCPCD_WAITIP4) &&
284 ipv4_hasaddr(ifp))
285 opts &= ~(DHCPCD_WAITIP | DHCPCD_WAITIP4);
286 #endif
287 #ifdef INET6
288 if (opts & (DHCPCD_WAITIP | DHCPCD_WAITIP6) &&
289 ipv6_hasaddr(ifp))
290 opts &= ~(DHCPCD_WAITIP | DHCPCD_WAITIP6);
291 #endif
292 if (!(opts & DHCPCD_WAITOPTS))
293 break;
295 if (opts & DHCPCD_WAITIP)
296 af = AF_UNSPEC;
297 else if (opts & DHCPCD_WAITIP4)
298 af = AF_INET;
299 else if (opts & DHCPCD_WAITIP6)
300 af = AF_INET6;
301 else
302 return AF_MAX;
303 return af;
306 static int
307 dhcpcd_ipwaited(struct dhcpcd_ctx *ctx)
309 struct interface *ifp;
310 int af;
312 TAILQ_FOREACH(ifp, ctx->ifaces, next) {
313 if ((af = dhcpcd_ifafwaiting(ifp)) != AF_MAX) {
314 logdebugx("%s: waiting for an %s address",
315 ifp->name, dhcpcd_af(af));
316 return 0;
320 if ((af = dhcpcd_afwaiting(ctx)) != AF_MAX) {
321 logdebugx("waiting for an %s address",
322 dhcpcd_af(af));
323 return 0;
326 return 1;
329 /* Returns the pid of the child, otherwise 0. */
330 void
331 dhcpcd_daemonise(struct dhcpcd_ctx *ctx)
333 #ifdef THERE_IS_NO_FORK
334 eloop_timeout_delete(ctx->eloop, handle_exit_timeout, ctx);
335 errno = ENOSYS;
336 return;
337 #else
338 int i;
339 unsigned int logopts = loggetopts();
341 if (ctx->options & DHCPCD_DAEMONISE &&
342 !(ctx->options & (DHCPCD_DAEMONISED | DHCPCD_NOWAITIP)))
344 if (!dhcpcd_ipwaited(ctx))
345 return;
348 if (ctx->options & DHCPCD_ONESHOT) {
349 loginfox("exiting due to oneshot");
350 eloop_exit(ctx->eloop, EXIT_SUCCESS);
351 return;
354 eloop_timeout_delete(ctx->eloop, handle_exit_timeout, ctx);
355 if (ctx->options & DHCPCD_DAEMONISED ||
356 !(ctx->options & DHCPCD_DAEMONISE))
357 return;
359 /* Don't use loginfo because this makes no sense in a log. */
360 if (!(logopts & LOGERR_QUIET) && ctx->stderr_valid)
361 (void)fprintf(stderr,
362 "forked to background, child pid %d\n", getpid());
363 i = EXIT_SUCCESS;
364 if (write(ctx->fork_fd, &i, sizeof(i)) == -1)
365 logerr("write");
366 ctx->options |= DHCPCD_DAEMONISED;
367 eloop_event_delete(ctx->eloop, ctx->fork_fd);
368 close(ctx->fork_fd);
369 ctx->fork_fd = -1;
372 * Stop writing to stderr.
373 * On the happy path, only the manager process writes to stderr,
374 * so this just stops wasting fprintf calls to nowhere.
375 * All other calls - ie errors in privsep processes or script output,
376 * will error when printing.
377 * If we *really* want to fix that, then we need to suck
378 * stderr/stdout in the manager process and either disacrd it or pass
379 * it to the launcher process and then to stderr.
381 logopts &= ~LOGERR_ERR;
382 logsetopts(logopts);
383 #endif
386 static void
387 dhcpcd_drop(struct interface *ifp, int stop)
390 #ifdef DHCP6
391 dhcp6_drop(ifp, stop ? NULL : "EXPIRE6");
392 #endif
393 #ifdef INET6
394 ipv6nd_drop(ifp);
395 ipv6_drop(ifp);
396 #endif
397 #ifdef IPV4LL
398 ipv4ll_drop(ifp);
399 #endif
400 #ifdef INET
401 dhcp_drop(ifp, stop ? "STOP" : "EXPIRE");
402 #endif
403 #ifdef ARP
404 arp_drop(ifp);
405 #endif
406 #if !defined(DHCP6) && !defined(DHCP)
407 UNUSED(stop);
408 #endif
411 static void
412 stop_interface(struct interface *ifp, const char *reason)
414 struct dhcpcd_ctx *ctx;
416 ctx = ifp->ctx;
417 loginfox("%s: removing interface", ifp->name);
418 ifp->options->options |= DHCPCD_STOPPING;
420 dhcpcd_drop(ifp, 1);
421 script_runreason(ifp, reason == NULL ? "STOPPED" : reason);
423 /* Delete all timeouts for the interfaces */
424 eloop_q_timeout_delete(ctx->eloop, ELOOP_QUEUE_ALL, NULL, ifp);
426 /* De-activate the interface */
427 ifp->active = IF_INACTIVE;
428 ifp->options->options &= ~DHCPCD_STOPPING;
430 if (!(ctx->options & (DHCPCD_MANAGER | DHCPCD_TEST)))
431 eloop_exit(ctx->eloop, EXIT_FAILURE);
434 static void
435 configure_interface1(struct interface *ifp)
437 struct if_options *ifo = ifp->options;
439 /* Do any platform specific configuration */
440 if_conf(ifp);
442 /* If we want to release a lease, we can't really persist the
443 * address either. */
444 if (ifo->options & DHCPCD_RELEASE)
445 ifo->options &= ~DHCPCD_PERSISTENT;
447 if (ifp->flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) {
448 ifo->options &= ~DHCPCD_ARP;
449 if (!(ifp->flags & IFF_MULTICAST))
450 ifo->options &= ~DHCPCD_IPV6RS;
451 if (!(ifo->options & (DHCPCD_INFORM | DHCPCD_WANTDHCP)))
452 ifo->options |= DHCPCD_STATIC;
455 if (ifo->metric != -1)
456 ifp->metric = (unsigned int)ifo->metric;
458 #ifdef INET6
459 /* We want to setup INET6 on the interface as soon as possible. */
460 if (ifp->active == IF_ACTIVE_USER &&
461 ifo->options & DHCPCD_IPV6 &&
462 !(ifp->ctx->options & (DHCPCD_DUMPLEASE | DHCPCD_TEST)))
464 /* If not doing any DHCP, disable the RDNSS requirement. */
465 if (!(ifo->options & (DHCPCD_DHCP | DHCPCD_DHCP6)))
466 ifo->options &= ~DHCPCD_IPV6RA_REQRDNSS;
467 if_setup_inet6(ifp);
469 #endif
471 if (!(ifo->options & DHCPCD_IAID)) {
473 * An IAID is for identifying a unqiue interface within
474 * the client. It is 4 bytes long. Working out a default
475 * value is problematic.
477 * Interface name and number are not stable
478 * between different OS's. Some OS's also cannot make
479 * up their mind what the interface should be called
480 * (yes, udev, I'm looking at you).
481 * Also, the name could be longer than 4 bytes.
482 * Also, with pluggable interfaces the name and index
483 * could easily get swapped per actual interface.
485 * The MAC address is 6 bytes long, the final 3
486 * being unique to the manufacturer and the initial 3
487 * being unique to the organisation which makes it.
488 * We could use the last 4 bytes of the MAC address
489 * as the IAID as it's the most stable part given the
490 * above, but equally it's not guaranteed to be
491 * unique.
493 * Given the above, and our need to reliably work
494 * between reboots without persitent storage,
495 * generating the IAID from the MAC address is the only
496 * logical default.
497 * Saying that, if a VLANID has been specified then we
498 * can use that. It's possible that different interfaces
499 * can have the same VLANID, but this is no worse than
500 * generating the IAID from the duplicate MAC address.
502 * dhclient uses the last 4 bytes of the MAC address.
503 * dibbler uses an increamenting counter.
504 * wide-dhcpv6 uses 0 or a configured value.
505 * odhcp6c uses 1.
506 * Windows 7 uses the first 3 bytes of the MAC address
507 * and an unknown byte.
508 * dhcpcd-6.1.0 and earlier used the interface name,
509 * falling back to interface index if name > 4.
511 if (ifp->vlanid != 0) {
512 uint32_t vlanid;
514 /* Maximal VLANID is 4095, so prefix with 0xff
515 * so we don't conflict with an interface index. */
516 vlanid = htonl(ifp->vlanid | 0xff000000);
517 memcpy(ifo->iaid, &vlanid, sizeof(vlanid));
518 } else if (ifo->options & DHCPCD_ANONYMOUS)
519 memset(ifo->iaid, 0, sizeof(ifo->iaid));
520 else if (ifp->hwlen >= sizeof(ifo->iaid)) {
521 memcpy(ifo->iaid,
522 ifp->hwaddr + ifp->hwlen - sizeof(ifo->iaid),
523 sizeof(ifo->iaid));
524 } else {
525 uint32_t len;
527 len = (uint32_t)strlen(ifp->name);
528 if (len <= sizeof(ifo->iaid)) {
529 memcpy(ifo->iaid, ifp->name, len);
530 if (len < sizeof(ifo->iaid))
531 memset(ifo->iaid + len, 0,
532 sizeof(ifo->iaid) - len);
533 } else {
534 /* IAID is the same size as a uint32_t */
535 len = htonl(ifp->index);
536 memcpy(ifo->iaid, &len, sizeof(ifo->iaid));
539 ifo->options |= DHCPCD_IAID;
542 #ifdef DHCP6
543 if (ifo->ia_len == 0 && ifo->options & DHCPCD_IPV6 &&
544 ifp->name[0] != '\0')
546 ifo->ia = malloc(sizeof(*ifo->ia));
547 if (ifo->ia == NULL)
548 logerr(__func__);
549 else {
550 ifo->ia_len = 1;
551 ifo->ia->ia_type = D6_OPTION_IA_NA;
552 memcpy(ifo->ia->iaid, ifo->iaid, sizeof(ifo->iaid));
553 memset(&ifo->ia->addr, 0, sizeof(ifo->ia->addr));
554 #ifndef SMALL
555 ifo->ia->sla = NULL;
556 ifo->ia->sla_len = 0;
557 #endif
559 } else {
560 size_t i;
562 for (i = 0; i < ifo->ia_len; i++) {
563 if (!ifo->ia[i].iaid_set) {
564 memcpy(&ifo->ia[i].iaid, ifo->iaid,
565 sizeof(ifo->ia[i].iaid));
566 ifo->ia[i].iaid_set = 1;
570 #endif
572 /* If root is network mounted, we don't want to kill the connection
573 * if the DHCP server goes the way of the dodo OR dhcpcd is rebooting
574 * and the lease file has expired. */
575 if (is_root_local() == 0)
576 ifo->options |= DHCPCD_LASTLEASE_EXTEND;
580 dhcpcd_selectprofile(struct interface *ifp, const char *profile)
582 struct if_options *ifo;
583 char pssid[PROFILE_LEN];
585 if (ifp->ssid_len) {
586 ssize_t r;
588 r = print_string(pssid, sizeof(pssid), OT_ESCSTRING,
589 ifp->ssid, ifp->ssid_len);
590 if (r == -1) {
591 logerr(__func__);
592 pssid[0] = '\0';
594 } else
595 pssid[0] = '\0';
596 ifo = read_config(ifp->ctx, ifp->name, pssid, profile);
597 if (ifo == NULL) {
598 logdebugx("%s: no profile %s", ifp->name, profile);
599 return -1;
601 if (profile != NULL) {
602 strlcpy(ifp->profile, profile, sizeof(ifp->profile));
603 loginfox("%s: selected profile %s", ifp->name, profile);
604 } else
605 *ifp->profile = '\0';
607 free_options(ifp->ctx, ifp->options);
608 ifp->options = ifo;
609 if (profile) {
610 add_options(ifp->ctx, ifp->name, ifp->options,
611 ifp->ctx->argc, ifp->ctx->argv);
612 configure_interface1(ifp);
614 return 1;
617 static void
618 configure_interface(struct interface *ifp, int argc, char **argv,
619 unsigned long long options)
621 time_t old;
623 old = ifp->options ? ifp->options->mtime : 0;
624 dhcpcd_selectprofile(ifp, NULL);
625 if (ifp->options == NULL) {
626 /* dhcpcd cannot continue with this interface. */
627 ifp->active = IF_INACTIVE;
628 return;
630 add_options(ifp->ctx, ifp->name, ifp->options, argc, argv);
631 ifp->options->options |= options;
632 configure_interface1(ifp);
634 /* If the mtime has changed drop any old lease */
635 if (old != 0 && ifp->options->mtime != old) {
636 logwarnx("%s: config file changed, expiring leases",
637 ifp->name);
638 dhcpcd_drop(ifp, 0);
642 static void
643 dhcpcd_initstate2(struct interface *ifp, unsigned long long options)
645 struct if_options *ifo;
647 if (options) {
648 if ((ifo = default_config(ifp->ctx)) == NULL) {
649 logerr(__func__);
650 return;
652 ifo->options |= options;
653 free(ifp->options);
654 ifp->options = ifo;
655 } else
656 ifo = ifp->options;
658 #ifdef INET6
659 if (ifo->options & DHCPCD_IPV6 && ipv6_init(ifp->ctx) == -1) {
660 logerr(__func__);
661 ifo->options &= ~DHCPCD_IPV6;
663 #endif
666 static void
667 dhcpcd_initstate1(struct interface *ifp, int argc, char **argv,
668 unsigned long long options)
671 configure_interface(ifp, argc, argv, options);
672 if (ifp->active)
673 dhcpcd_initstate2(ifp, 0);
676 static void
677 dhcpcd_initstate(struct interface *ifp, unsigned long long options)
680 dhcpcd_initstate1(ifp, ifp->ctx->argc, ifp->ctx->argv, options);
683 static void
684 dhcpcd_reportssid(struct interface *ifp)
686 char pssid[IF_SSIDLEN * 4];
688 if (print_string(pssid, sizeof(pssid), OT_ESCSTRING,
689 ifp->ssid, ifp->ssid_len) == -1)
691 logerr(__func__);
692 return;
695 loginfox("%s: connected to Access Point: %s", ifp->name, pssid);
698 static void
699 dhcpcd_nocarrier_roaming(struct interface *ifp)
702 loginfox("%s: carrier lost - roaming", ifp->name);
704 #ifdef ARP
705 arp_drop(ifp);
706 #endif
707 #ifdef INET
708 dhcp_abort(ifp);
709 #endif
710 #ifdef DHCP6
711 dhcp6_abort(ifp);
712 #endif
714 rt_build(ifp->ctx, AF_UNSPEC);
715 script_runreason(ifp, "NOCARRIER_ROAMING");
718 void
719 dhcpcd_handlecarrier(struct interface *ifp, int carrier, unsigned int flags)
721 bool was_link_up = if_is_link_up(ifp);
722 bool was_roaming = if_roaming(ifp);
724 ifp->carrier = carrier;
725 ifp->flags = flags;
727 if (!if_is_link_up(ifp)) {
728 if (!ifp->active || (!was_link_up && !was_roaming))
729 return;
732 * If the interface is roaming (generally on wireless)
733 * then while we are not up, we are not down either.
734 * Preserve the network state until we either disconnect
735 * or re-connect.
737 if (!ifp->options->randomise_hwaddr && if_roaming(ifp)) {
738 dhcpcd_nocarrier_roaming(ifp);
739 return;
742 loginfox("%s: carrier lost", ifp->name);
743 script_runreason(ifp, "NOCARRIER");
744 dhcpcd_drop(ifp, 0);
746 if (ifp->options->randomise_hwaddr) {
747 bool is_up = ifp->flags & IFF_UP;
749 if (is_up)
750 if_down(ifp);
751 if (if_randomisemac(ifp) == -1 && errno != ENXIO)
752 logerr(__func__);
753 if (is_up)
754 if_up(ifp);
757 return;
761 * At this point carrier is NOT DOWN and we have IFF_UP.
762 * We should treat LINK_UNKNOWN as up as the driver may not support
763 * link state changes.
764 * The consideration of any other information about carrier should
765 * be handled in the OS specific if_carrier() function.
767 if (was_link_up)
768 return;
770 if (ifp->active) {
771 if (carrier == LINK_UNKNOWN)
772 loginfox("%s: carrier unknown, assuming up", ifp->name);
773 else
774 loginfox("%s: carrier acquired", ifp->name);
777 #if !defined(__linux__) && !defined(__NetBSD__)
778 /* BSD does not emit RTM_NEWADDR or RTM_CHGADDR when the
779 * hardware address changes so we have to go
780 * through the disovery process to work it out. */
781 dhcpcd_handleinterface(ifp->ctx, 0, ifp->name);
782 #endif
784 if (ifp->wireless) {
785 uint8_t ossid[IF_SSIDLEN];
786 size_t olen;
788 olen = ifp->ssid_len;
789 memcpy(ossid, ifp->ssid, ifp->ssid_len);
790 if_getssid(ifp);
792 /* If we changed SSID network, drop leases */
793 if ((ifp->ssid_len != olen ||
794 memcmp(ifp->ssid, ossid, ifp->ssid_len)) && ifp->active)
796 dhcpcd_reportssid(ifp);
797 dhcpcd_drop(ifp, 0);
798 #ifdef IPV4LL
799 ipv4ll_reset(ifp);
800 #endif
804 if (!ifp->active)
805 return;
807 dhcpcd_initstate(ifp, 0);
808 script_runreason(ifp, "CARRIER");
810 #ifdef INET6
811 /* Set any IPv6 Routers we remembered to expire faster than they
812 * would normally as we maybe on a new network. */
813 ipv6nd_startexpire(ifp);
814 #ifdef IPV6_MANAGETEMPADDR
815 /* RFC4941 Section 3.5 */
816 ipv6_regentempaddrs(ifp);
817 #endif
818 #endif
820 dhcpcd_startinterface(ifp);
823 static void
824 warn_iaid_conflict(struct interface *ifp, uint16_t ia_type, uint8_t *iaid)
826 struct interface *ifn;
827 #ifdef INET6
828 size_t i;
829 struct if_ia *ia;
830 #endif
832 TAILQ_FOREACH(ifn, ifp->ctx->ifaces, next) {
833 if (ifn == ifp || !ifn->active)
834 continue;
835 if (ifn->options->options & DHCPCD_ANONYMOUS)
836 continue;
837 if (ia_type == 0 &&
838 memcmp(ifn->options->iaid, iaid,
839 sizeof(ifn->options->iaid)) == 0)
840 break;
841 #ifdef INET6
842 for (i = 0; i < ifn->options->ia_len; i++) {
843 ia = &ifn->options->ia[i];
844 if (ia->ia_type == ia_type &&
845 memcmp(ia->iaid, iaid, sizeof(ia->iaid)) == 0)
846 break;
848 #endif
851 /* This is only a problem if the interfaces are on the same network. */
852 if (ifn)
853 logerrx("%s: IAID conflicts with one assigned to %s",
854 ifp->name, ifn->name);
857 static void
858 dhcpcd_initduid(struct dhcpcd_ctx *ctx, struct interface *ifp)
860 char buf[DUID_LEN * 3];
862 if (ctx->duid != NULL) {
863 if (ifp == NULL)
864 goto log;
865 return;
868 duid_init(ctx, ifp);
869 if (ctx->duid == NULL)
870 return;
872 log:
873 loginfox("DUID %s",
874 hwaddr_ntoa(ctx->duid, ctx->duid_len, buf, sizeof(buf)));
877 void
878 dhcpcd_startinterface(void *arg)
880 struct interface *ifp = arg;
881 struct if_options *ifo = ifp->options;
883 if (ifo->options & DHCPCD_LINK && !if_is_link_up(ifp)) {
884 loginfox("%s: waiting for carrier", ifp->name);
885 return;
888 if (ifo->options & (DHCPCD_DUID | DHCPCD_IPV6) &&
889 !(ifo->options & DHCPCD_ANONYMOUS))
891 char buf[sizeof(ifo->iaid) * 3];
892 #ifdef INET6
893 size_t i;
894 struct if_ia *ia;
895 #endif
897 /* Try and init DUID from the interface hardware address */
898 dhcpcd_initduid(ifp->ctx, ifp);
900 /* Report IAIDs */
901 loginfox("%s: IAID %s", ifp->name,
902 hwaddr_ntoa(ifo->iaid, sizeof(ifo->iaid),
903 buf, sizeof(buf)));
904 warn_iaid_conflict(ifp, 0, ifo->iaid);
906 #ifdef INET6
907 for (i = 0; i < ifo->ia_len; i++) {
908 ia = &ifo->ia[i];
909 if (memcmp(ifo->iaid, ia->iaid, sizeof(ifo->iaid))) {
910 loginfox("%s: IA type %u IAID %s",
911 ifp->name, ia->ia_type,
912 hwaddr_ntoa(ia->iaid, sizeof(ia->iaid),
913 buf, sizeof(buf)));
914 warn_iaid_conflict(ifp, ia->ia_type, ia->iaid);
917 #endif
920 #ifdef INET6
921 if (ifo->options & DHCPCD_IPV6 && ipv6_start(ifp) == -1) {
922 logerr("%s: ipv6_start", ifp->name);
923 ifo->options &= ~DHCPCD_IPV6;
926 if (ifo->options & DHCPCD_IPV6) {
927 if (ifp->active == IF_ACTIVE_USER) {
928 ipv6_startstatic(ifp);
930 if (ifo->options & DHCPCD_IPV6RS)
931 ipv6nd_startrs(ifp);
934 #ifdef DHCP6
935 /* DHCPv6 could be turned off, but the interface
936 * is still delegated to. */
937 if (ifp->active)
938 dhcp6_find_delegates(ifp);
940 if (ifo->options & DHCPCD_DHCP6) {
941 if (ifp->active == IF_ACTIVE_USER) {
942 enum DH6S d6_state;
944 if (ifo->options & DHCPCD_IA_FORCED)
945 d6_state = DH6S_INIT;
946 else if (ifo->options & DHCPCD_INFORM6)
947 d6_state = DH6S_INFORM;
948 else
949 d6_state = DH6S_CONFIRM;
950 if (dhcp6_start(ifp, d6_state) == -1)
951 logerr("%s: dhcp6_start", ifp->name);
954 #endif
956 #endif
958 #ifdef INET
959 if (ifo->options & DHCPCD_IPV4 && ifp->active == IF_ACTIVE_USER) {
960 /* Ensure we have an IPv4 state before starting DHCP */
961 if (ipv4_getstate(ifp) != NULL)
962 dhcp_start(ifp);
964 #endif
967 static void
968 dhcpcd_prestartinterface(void *arg)
970 struct interface *ifp = arg;
971 struct dhcpcd_ctx *ctx = ifp->ctx;
972 bool randmac_down;
974 if (ifp->carrier <= LINK_DOWN &&
975 ifp->options->randomise_hwaddr &&
976 ifp->flags & IFF_UP)
978 if_down(ifp);
979 randmac_down = true;
980 } else
981 randmac_down = false;
983 if ((!(ctx->options & DHCPCD_MANAGER) ||
984 ifp->options->options & DHCPCD_IF_UP || randmac_down) &&
985 !(ifp->flags & IFF_UP))
987 if (ifp->options->randomise_hwaddr &&
988 if_randomisemac(ifp) == -1)
989 logerr(__func__);
990 if (if_up(ifp) == -1)
991 logerr(__func__);
994 dhcpcd_startinterface(ifp);
997 static void
998 run_preinit(struct interface *ifp)
1001 if (ifp->ctx->options & DHCPCD_TEST)
1002 return;
1004 script_runreason(ifp, "PREINIT");
1005 if (ifp->wireless && if_is_link_up(ifp))
1006 dhcpcd_reportssid(ifp);
1007 if (ifp->options->options & DHCPCD_LINK && ifp->carrier != LINK_UNKNOWN)
1008 script_runreason(ifp,
1009 ifp->carrier == LINK_UP ? "CARRIER" : "NOCARRIER");
1012 void
1013 dhcpcd_activateinterface(struct interface *ifp, unsigned long long options)
1016 if (ifp->active)
1017 return;
1019 ifp->active = IF_ACTIVE;
1020 dhcpcd_initstate2(ifp, options);
1022 /* It's possible we might not have been able to load
1023 * a config. */
1024 if (!ifp->active)
1025 return;
1027 configure_interface1(ifp);
1028 run_preinit(ifp);
1029 dhcpcd_prestartinterface(ifp);
1033 dhcpcd_handleinterface(void *arg, int action, const char *ifname)
1035 struct dhcpcd_ctx *ctx = arg;
1036 struct ifaddrs *ifaddrs;
1037 struct if_head *ifs;
1038 struct interface *ifp, *iff;
1039 const char * const argv[] = { ifname };
1040 int e;
1042 if (action == -1) {
1043 ifp = if_find(ctx->ifaces, ifname);
1044 if (ifp == NULL) {
1045 errno = ESRCH;
1046 return -1;
1048 if (ifp->active) {
1049 logdebugx("%s: interface departed", ifp->name);
1050 stop_interface(ifp, "DEPARTED");
1052 TAILQ_REMOVE(ctx->ifaces, ifp, next);
1053 if_free(ifp);
1054 return 0;
1057 ifs = if_discover(ctx, &ifaddrs, -1, UNCONST(argv));
1058 if (ifs == NULL) {
1059 logerr(__func__);
1060 return -1;
1063 ifp = if_find(ifs, ifname);
1064 if (ifp == NULL) {
1065 /* This can happen if an interface is quickly added
1066 * and then removed. */
1067 errno = ENOENT;
1068 e = -1;
1069 goto out;
1071 e = 1;
1073 /* Check if we already have the interface */
1074 iff = if_find(ctx->ifaces, ifp->name);
1076 if (iff != NULL) {
1077 if (iff->active)
1078 logdebugx("%s: interface updated", iff->name);
1079 /* The flags and hwaddr could have changed */
1080 iff->flags = ifp->flags;
1081 iff->hwlen = ifp->hwlen;
1082 if (ifp->hwlen != 0)
1083 memcpy(iff->hwaddr, ifp->hwaddr, iff->hwlen);
1084 } else {
1085 TAILQ_REMOVE(ifs, ifp, next);
1086 TAILQ_INSERT_TAIL(ctx->ifaces, ifp, next);
1087 if (ifp->active) {
1088 logdebugx("%s: interface added", ifp->name);
1089 dhcpcd_initstate(ifp, 0);
1090 run_preinit(ifp);
1092 iff = ifp;
1095 if (action > 0) {
1096 if_learnaddrs(ctx, ifs, &ifaddrs);
1097 if (iff->active)
1098 dhcpcd_prestartinterface(iff);
1101 out:
1102 /* Free our discovered list */
1103 while ((ifp = TAILQ_FIRST(ifs))) {
1104 TAILQ_REMOVE(ifs, ifp, next);
1105 if_free(ifp);
1107 free(ifs);
1108 if_freeifaddrs(ctx, &ifaddrs);
1110 return e;
1113 static void
1114 dhcpcd_handlelink(void *arg, unsigned short events)
1116 struct dhcpcd_ctx *ctx = arg;
1118 if (events != ELE_READ)
1119 logerrx("%s: unexpected event 0x%04x", __func__, events);
1121 if (if_handlelink(ctx) == -1) {
1122 if (errno == ENOBUFS || errno == ENOMEM) {
1123 dhcpcd_linkoverflow(ctx);
1124 return;
1126 if (errno != ENOTSUP)
1127 logerr(__func__);
1131 static void
1132 dhcpcd_checkcarrier(void *arg)
1134 struct interface *ifp0 = arg, *ifp;
1136 ifp = if_find(ifp0->ctx->ifaces, ifp0->name);
1137 if (ifp == NULL || ifp->carrier == ifp0->carrier)
1138 return;
1140 dhcpcd_handlecarrier(ifp, ifp0->carrier, ifp0->flags);
1141 if_free(ifp0);
1144 #ifndef SMALL
1145 static void
1146 dhcpcd_setlinkrcvbuf(struct dhcpcd_ctx *ctx)
1148 socklen_t socklen;
1150 if (ctx->link_rcvbuf == 0)
1151 return;
1153 logdebugx("setting route socket receive buffer size to %d bytes",
1154 ctx->link_rcvbuf);
1156 socklen = sizeof(ctx->link_rcvbuf);
1157 if (setsockopt(ctx->link_fd, SOL_SOCKET,
1158 SO_RCVBUF, &ctx->link_rcvbuf, socklen) == -1)
1159 logerr(__func__);
1161 #endif
1163 static void
1164 dhcpcd_runprestartinterface(void *arg)
1166 struct interface *ifp = arg;
1168 run_preinit(ifp);
1169 dhcpcd_prestartinterface(ifp);
1172 void
1173 dhcpcd_linkoverflow(struct dhcpcd_ctx *ctx)
1175 socklen_t socklen;
1176 int rcvbuflen;
1177 char buf[2048];
1178 ssize_t rlen;
1179 size_t rcnt;
1180 struct if_head *ifaces;
1181 struct ifaddrs *ifaddrs;
1182 struct interface *ifp, *ifn, *ifp1;
1184 socklen = sizeof(rcvbuflen);
1185 if (getsockopt(ctx->link_fd, SOL_SOCKET,
1186 SO_RCVBUF, &rcvbuflen, &socklen) == -1) {
1187 logerr("%s: getsockopt", __func__);
1188 rcvbuflen = 0;
1190 #ifdef __linux__
1191 else
1192 rcvbuflen /= 2;
1193 #endif
1195 logerrx("route socket overflowed (rcvbuflen %d)"
1196 " - learning interface state", rcvbuflen);
1198 /* Drain the socket.
1199 * We cannot open a new one due to privsep. */
1200 rcnt = 0;
1201 do {
1202 rlen = read(ctx->link_fd, buf, sizeof(buf));
1203 if (++rcnt % 1000 == 0)
1204 logwarnx("drained %zu messages", rcnt);
1205 } while (rlen != -1 || errno == ENOBUFS || errno == ENOMEM);
1206 if (rcnt % 1000 != 0)
1207 logwarnx("drained %zu messages", rcnt);
1209 /* Work out the current interfaces. */
1210 ifaces = if_discover(ctx, &ifaddrs, ctx->ifc, ctx->ifv);
1211 if (ifaces == NULL) {
1212 logerr(__func__);
1213 return;
1216 /* Punt departed interfaces */
1217 TAILQ_FOREACH_SAFE(ifp, ctx->ifaces, next, ifn) {
1218 if (if_find(ifaces, ifp->name) != NULL)
1219 continue;
1220 dhcpcd_handleinterface(ctx, -1, ifp->name);
1223 /* Add new interfaces */
1224 while ((ifp = TAILQ_FIRST(ifaces)) != NULL ) {
1225 TAILQ_REMOVE(ifaces, ifp, next);
1226 ifp1 = if_find(ctx->ifaces, ifp->name);
1227 if (ifp1 != NULL) {
1228 /* If the interface already exists,
1229 * check carrier state.
1230 * dhcpcd_checkcarrier will free ifp. */
1231 eloop_timeout_add_sec(ctx->eloop, 0,
1232 dhcpcd_checkcarrier, ifp);
1233 continue;
1235 TAILQ_INSERT_TAIL(ctx->ifaces, ifp, next);
1236 if (ifp->active) {
1237 dhcpcd_initstate(ifp, 0);
1238 eloop_timeout_add_sec(ctx->eloop, 0,
1239 dhcpcd_runprestartinterface, ifp);
1242 free(ifaces);
1244 /* Update address state. */
1245 if_markaddrsstale(ctx->ifaces);
1246 if_learnaddrs(ctx, ctx->ifaces, &ifaddrs);
1247 if_deletestaleaddrs(ctx->ifaces);
1248 if_freeifaddrs(ctx, &ifaddrs);
1251 void
1252 dhcpcd_handlehwaddr(struct interface *ifp,
1253 uint16_t hwtype, const void *hwaddr, uint8_t hwlen)
1255 char buf[sizeof(ifp->hwaddr) * 3];
1257 if (hwaddr == NULL || !if_valid_hwaddr(hwaddr, hwlen))
1258 hwlen = 0;
1260 if (hwlen > sizeof(ifp->hwaddr)) {
1261 errno = ENOBUFS;
1262 logerr("%s: %s", __func__, ifp->name);
1263 return;
1266 if (ifp->hwtype != hwtype) {
1267 if (ifp->active)
1268 loginfox("%s: hardware address type changed"
1269 " from %d to %d", ifp->name, ifp->hwtype, hwtype);
1270 ifp->hwtype = hwtype;
1273 if (ifp->hwlen == hwlen &&
1274 (hwlen == 0 || memcmp(ifp->hwaddr, hwaddr, hwlen) == 0))
1275 return;
1277 if (ifp->active) {
1278 loginfox("%s: old hardware address: %s", ifp->name,
1279 hwaddr_ntoa(ifp->hwaddr, ifp->hwlen, buf, sizeof(buf)));
1280 loginfox("%s: new hardware address: %s", ifp->name,
1281 hwaddr_ntoa(hwaddr, hwlen, buf, sizeof(buf)));
1283 ifp->hwlen = hwlen;
1284 if (hwaddr != NULL)
1285 memcpy(ifp->hwaddr, hwaddr, hwlen);
1288 static void
1289 if_reboot(struct interface *ifp, int argc, char **argv)
1291 #ifdef INET
1292 unsigned long long oldopts;
1294 oldopts = ifp->options->options;
1295 #endif
1296 script_runreason(ifp, "RECONFIGURE");
1297 dhcpcd_initstate1(ifp, argc, argv, 0);
1298 #ifdef INET
1299 dhcp_reboot_newopts(ifp, oldopts);
1300 #endif
1301 #ifdef DHCP6
1302 dhcp6_reboot(ifp);
1303 #endif
1304 dhcpcd_prestartinterface(ifp);
1307 static void
1308 reload_config(struct dhcpcd_ctx *ctx)
1310 struct if_options *ifo;
1312 free_globals(ctx);
1313 if ((ifo = read_config(ctx, NULL, NULL, NULL)) == NULL)
1314 return;
1315 add_options(ctx, NULL, ifo, ctx->argc, ctx->argv);
1316 /* We need to preserve these options. */
1317 if (ctx->options & DHCPCD_STARTED)
1318 ifo->options |= DHCPCD_STARTED;
1319 if (ctx->options & DHCPCD_MANAGER)
1320 ifo->options |= DHCPCD_MANAGER;
1321 if (ctx->options & DHCPCD_DAEMONISED)
1322 ifo->options |= DHCPCD_DAEMONISED;
1323 if (ctx->options & DHCPCD_PRIVSEP)
1324 ifo->options |= DHCPCD_PRIVSEP;
1325 ctx->options = ifo->options;
1326 free_options(ctx, ifo);
1329 static void
1330 reconf_reboot(struct dhcpcd_ctx *ctx, int action, int argc, char **argv, int oi)
1332 int i;
1333 struct interface *ifp;
1335 TAILQ_FOREACH(ifp, ctx->ifaces, next) {
1336 for (i = oi; i < argc; i++) {
1337 if (strcmp(ifp->name, argv[i]) == 0)
1338 break;
1340 if (oi != argc && i == argc)
1341 continue;
1342 if (ifp->active == IF_ACTIVE_USER) {
1343 if (action)
1344 if_reboot(ifp, argc, argv);
1345 #ifdef INET
1346 else
1347 ipv4_applyaddr(ifp);
1348 #endif
1349 } else if (i != argc) {
1350 ifp->active = IF_ACTIVE_USER;
1351 dhcpcd_initstate1(ifp, argc, argv, 0);
1352 run_preinit(ifp);
1353 dhcpcd_prestartinterface(ifp);
1358 static void
1359 stop_all_interfaces(struct dhcpcd_ctx *ctx, unsigned long long opts)
1361 struct interface *ifp;
1363 ctx->options |= DHCPCD_EXITING;
1364 if (ctx->ifaces == NULL)
1365 return;
1367 /* Drop the last interface first */
1368 TAILQ_FOREACH_REVERSE(ifp, ctx->ifaces, if_head, next) {
1369 if (!ifp->active)
1370 continue;
1371 ifp->options->options |= opts;
1372 if (ifp->options->options & DHCPCD_RELEASE)
1373 ifp->options->options &= ~DHCPCD_PERSISTENT;
1374 ifp->options->options |= DHCPCD_EXITING;
1375 stop_interface(ifp, NULL);
1379 static void
1380 dhcpcd_ifrenew(struct interface *ifp)
1383 if (!ifp->active)
1384 return;
1386 if (ifp->options->options & DHCPCD_LINK && !if_is_link_up(ifp))
1387 return;
1389 #ifdef INET
1390 dhcp_renew(ifp);
1391 #endif
1392 #ifdef INET6
1393 #define DHCPCD_RARENEW (DHCPCD_IPV6 | DHCPCD_IPV6RS)
1394 if ((ifp->options->options & DHCPCD_RARENEW) == DHCPCD_RARENEW)
1395 ipv6nd_startrs(ifp);
1396 #endif
1397 #ifdef DHCP6
1398 dhcp6_renew(ifp);
1399 #endif
1402 static void
1403 dhcpcd_renew(struct dhcpcd_ctx *ctx)
1405 struct interface *ifp;
1407 TAILQ_FOREACH(ifp, ctx->ifaces, next) {
1408 dhcpcd_ifrenew(ifp);
1412 #ifdef USE_SIGNALS
1413 #define sigmsg "received %s, %s"
1414 void
1415 dhcpcd_signal_cb(int sig, void *arg)
1417 struct dhcpcd_ctx *ctx = arg;
1418 unsigned long long opts;
1419 int exit_code;
1421 if (ctx->options & DHCPCD_DUMPLEASE) {
1422 eloop_exit(ctx->eloop, EXIT_FAILURE);
1423 return;
1426 if (sig != SIGCHLD && ctx->options & DHCPCD_FORKED) {
1427 if (sig != SIGHUP &&
1428 write(ctx->fork_fd, &sig, sizeof(sig)) == -1)
1429 logerr("%s: write", __func__);
1430 return;
1433 opts = 0;
1434 exit_code = EXIT_FAILURE;
1435 switch (sig) {
1436 case SIGINT:
1437 loginfox(sigmsg, "SIGINT", "stopping");
1438 break;
1439 case SIGTERM:
1440 loginfox(sigmsg, "SIGTERM", "stopping");
1441 exit_code = EXIT_SUCCESS;
1442 break;
1443 case SIGALRM:
1444 loginfox(sigmsg, "SIGALRM", "releasing");
1445 opts |= DHCPCD_RELEASE;
1446 exit_code = EXIT_SUCCESS;
1447 break;
1448 case SIGHUP:
1449 loginfox(sigmsg, "SIGHUP", "rebinding");
1450 reload_config(ctx);
1451 /* Preserve any options passed on the commandline
1452 * when we were started. */
1453 reconf_reboot(ctx, 1, ctx->argc, ctx->argv,
1454 ctx->argc - ctx->ifc);
1455 return;
1456 case SIGUSR1:
1457 loginfox(sigmsg, "SIGUSR1", "renewing");
1458 dhcpcd_renew(ctx);
1459 return;
1460 case SIGUSR2:
1461 loginfox(sigmsg, "SIGUSR2", "reopening log");
1462 #ifdef PRIVSEP
1463 if (IN_PRIVSEP(ctx)) {
1464 if (ps_root_logreopen(ctx) == -1)
1465 logerr("ps_root_logreopen");
1466 return;
1468 #endif
1469 if (logopen(ctx->logfile) == -1)
1470 logerr("logopen");
1471 return;
1472 case SIGCHLD:
1473 #ifdef PRIVSEP
1474 ps_root_signalcb(sig, ctx);
1475 #else
1476 while (waitpid(-1, NULL, WNOHANG) > 0)
1478 #endif
1479 return;
1480 default:
1481 logerrx("received signal %d but don't know what to do with it",
1482 sig);
1483 return;
1486 if (!(ctx->options & DHCPCD_TEST))
1487 stop_all_interfaces(ctx, opts);
1488 eloop_exit(ctx->eloop, exit_code);
1490 #endif
1493 dhcpcd_handleargs(struct dhcpcd_ctx *ctx, struct fd_list *fd,
1494 int argc, char **argv)
1496 struct interface *ifp;
1497 unsigned long long opts;
1498 int opt, oi, do_reboot, do_renew, af = AF_UNSPEC;
1499 size_t len, l, nifaces;
1500 char *tmp, *p;
1502 /* Special commands for our control socket
1503 * as the other end should be blocking until it gets the
1504 * expected reply we should be safely able just to change the
1505 * write callback on the fd */
1506 /* Make any change here in privsep-control.c as well. */
1507 if (strcmp(*argv, "--version") == 0) {
1508 return control_queue(fd, UNCONST(VERSION),
1509 strlen(VERSION) + 1);
1510 } else if (strcmp(*argv, "--getconfigfile") == 0) {
1511 return control_queue(fd, UNCONST(fd->ctx->cffile),
1512 strlen(fd->ctx->cffile) + 1);
1513 } else if (strcmp(*argv, "--getinterfaces") == 0) {
1514 optind = argc = 0;
1515 goto dumplease;
1516 } else if (strcmp(*argv, "--listen") == 0) {
1517 fd->flags |= FD_LISTEN;
1518 return 0;
1521 /* Log the command */
1522 len = 1;
1523 for (opt = 0; opt < argc; opt++)
1524 len += strlen(argv[opt]) + 1;
1525 tmp = malloc(len);
1526 if (tmp == NULL)
1527 return -1;
1528 p = tmp;
1529 for (opt = 0; opt < argc; opt++) {
1530 l = strlen(argv[opt]);
1531 strlcpy(p, argv[opt], len);
1532 len -= l + 1;
1533 p += l;
1534 *p++ = ' ';
1536 *--p = '\0';
1537 loginfox("control command: %s", tmp);
1538 free(tmp);
1540 optind = 0;
1541 oi = 0;
1542 opts = 0;
1543 do_reboot = do_renew = 0;
1544 while ((opt = getopt_long(argc, argv, IF_OPTS, cf_options, &oi)) != -1)
1546 switch (opt) {
1547 case 'g':
1548 /* Assumed if below not set */
1549 break;
1550 case 'k':
1551 opts |= DHCPCD_RELEASE;
1552 break;
1553 case 'n':
1554 do_reboot = 1;
1555 break;
1556 case 'p':
1557 opts |= DHCPCD_PERSISTENT;
1558 break;
1559 case 'x':
1560 opts |= DHCPCD_EXITING;
1561 break;
1562 case 'N':
1563 do_renew = 1;
1564 break;
1565 case 'U':
1566 opts |= DHCPCD_DUMPLEASE;
1567 break;
1568 case '4':
1569 af = AF_INET;
1570 break;
1571 case '6':
1572 af = AF_INET6;
1573 break;
1577 if (opts & DHCPCD_DUMPLEASE) {
1578 ctx->options |= DHCPCD_DUMPLEASE;
1579 dumplease:
1580 nifaces = 0;
1581 TAILQ_FOREACH(ifp, ctx->ifaces, next) {
1582 if (!ifp->active)
1583 continue;
1584 for (oi = optind; oi < argc; oi++) {
1585 if (strcmp(ifp->name, argv[oi]) == 0)
1586 break;
1588 if (optind == argc || oi < argc) {
1589 opt = send_interface(NULL, ifp, af);
1590 if (opt == -1)
1591 goto dumperr;
1592 nifaces += (size_t)opt;
1595 if (write(fd->fd, &nifaces, sizeof(nifaces)) != sizeof(nifaces))
1596 goto dumperr;
1597 TAILQ_FOREACH(ifp, ctx->ifaces, next) {
1598 if (!ifp->active)
1599 continue;
1600 for (oi = optind; oi < argc; oi++) {
1601 if (strcmp(ifp->name, argv[oi]) == 0)
1602 break;
1604 if (optind == argc || oi < argc) {
1605 if (send_interface(fd, ifp, af) == -1)
1606 goto dumperr;
1609 ctx->options &= ~DHCPCD_DUMPLEASE;
1610 return 0;
1611 dumperr:
1612 ctx->options &= ~DHCPCD_DUMPLEASE;
1613 return -1;
1616 /* Only privileged users can control dhcpcd via the socket. */
1617 if (fd->flags & FD_UNPRIV) {
1618 errno = EPERM;
1619 return -1;
1622 if (opts & (DHCPCD_EXITING | DHCPCD_RELEASE)) {
1623 if (optind == argc) {
1624 stop_all_interfaces(ctx, opts);
1625 eloop_exit(ctx->eloop, EXIT_SUCCESS);
1626 return 0;
1628 for (oi = optind; oi < argc; oi++) {
1629 if ((ifp = if_find(ctx->ifaces, argv[oi])) == NULL)
1630 continue;
1631 if (!ifp->active)
1632 continue;
1633 ifp->options->options |= opts;
1634 if (opts & DHCPCD_RELEASE)
1635 ifp->options->options &= ~DHCPCD_PERSISTENT;
1636 stop_interface(ifp, NULL);
1638 return 0;
1641 if (do_renew) {
1642 if (optind == argc) {
1643 dhcpcd_renew(ctx);
1644 return 0;
1646 for (oi = optind; oi < argc; oi++) {
1647 if ((ifp = if_find(ctx->ifaces, argv[oi])) == NULL)
1648 continue;
1649 dhcpcd_ifrenew(ifp);
1651 return 0;
1654 reload_config(ctx);
1655 /* XXX: Respect initial commandline options? */
1656 reconf_reboot(ctx, do_reboot, argc, argv, optind - 1);
1657 return 0;
1660 static void dhcpcd_readdump1(void *, unsigned short);
1662 static void
1663 dhcpcd_readdump2(void *arg, unsigned short events)
1665 struct dhcpcd_ctx *ctx = arg;
1666 ssize_t len;
1667 int exit_code = EXIT_FAILURE;
1669 if (events != ELE_READ)
1670 logerrx("%s: unexpected event 0x%04x", __func__, events);
1672 len = read(ctx->control_fd, ctx->ctl_buf + ctx->ctl_bufpos,
1673 ctx->ctl_buflen - ctx->ctl_bufpos);
1674 if (len == -1) {
1675 logerr(__func__);
1676 goto finished;
1677 } else if (len == 0)
1678 goto finished;
1679 if ((size_t)len + ctx->ctl_bufpos != ctx->ctl_buflen) {
1680 ctx->ctl_bufpos += (size_t)len;
1681 return;
1684 if (ctx->ctl_buf[ctx->ctl_buflen - 1] != '\0') /* unlikely */
1685 ctx->ctl_buf[ctx->ctl_buflen - 1] = '\0';
1686 script_dump(ctx->ctl_buf, ctx->ctl_buflen);
1687 fflush(stdout);
1688 if (--ctx->ctl_extra != 0) {
1689 putchar('\n');
1690 if (eloop_event_add(ctx->eloop, ctx->control_fd, ELE_READ,
1691 dhcpcd_readdump1, ctx) == -1)
1692 logerr("%s: eloop_event_add", __func__);
1693 return;
1695 exit_code = EXIT_SUCCESS;
1697 finished:
1698 shutdown(ctx->control_fd, SHUT_RDWR);
1699 eloop_exit(ctx->eloop, exit_code);
1702 static void
1703 dhcpcd_readdump1(void *arg, unsigned short events)
1705 struct dhcpcd_ctx *ctx = arg;
1706 ssize_t len;
1708 if (events != ELE_READ)
1709 logerrx("%s: unexpected event 0x%04x", __func__, events);
1711 len = read(ctx->control_fd, &ctx->ctl_buflen, sizeof(ctx->ctl_buflen));
1712 if (len != sizeof(ctx->ctl_buflen)) {
1713 if (len != -1)
1714 errno = EINVAL;
1715 goto err;
1717 if (ctx->ctl_buflen > SSIZE_MAX) {
1718 errno = ENOBUFS;
1719 goto err;
1722 free(ctx->ctl_buf);
1723 ctx->ctl_buf = malloc(ctx->ctl_buflen);
1724 if (ctx->ctl_buf == NULL)
1725 goto err;
1727 ctx->ctl_bufpos = 0;
1728 if (eloop_event_add(ctx->eloop, ctx->control_fd, ELE_READ,
1729 dhcpcd_readdump2, ctx) == -1)
1730 logerr("%s: eloop_event_add", __func__);
1731 return;
1733 err:
1734 logerr(__func__);
1735 eloop_exit(ctx->eloop, EXIT_FAILURE);
1738 static void
1739 dhcpcd_readdump0(void *arg, unsigned short events)
1741 struct dhcpcd_ctx *ctx = arg;
1742 ssize_t len;
1744 if (events != ELE_READ)
1745 logerrx("%s: unexpected event 0x%04x", __func__, events);
1747 len = read(ctx->control_fd, &ctx->ctl_extra, sizeof(ctx->ctl_extra));
1748 if (len != sizeof(ctx->ctl_extra)) {
1749 if (len != -1)
1750 errno = EINVAL;
1751 logerr(__func__);
1752 eloop_exit(ctx->eloop, EXIT_FAILURE);
1753 return;
1756 if (ctx->ctl_extra == 0) {
1757 eloop_exit(ctx->eloop, EXIT_SUCCESS);
1758 return;
1761 if (eloop_event_add(ctx->eloop, ctx->control_fd, ELE_READ,
1762 dhcpcd_readdump1, ctx) == -1)
1763 logerr("%s: eloop_event_add", __func__);
1766 static void
1767 dhcpcd_readdumptimeout(void *arg)
1769 struct dhcpcd_ctx *ctx = arg;
1771 logerrx(__func__);
1772 eloop_exit(ctx->eloop, EXIT_FAILURE);
1775 static int
1776 dhcpcd_readdump(struct dhcpcd_ctx *ctx)
1779 ctx->options |= DHCPCD_FORKED;
1780 if (eloop_timeout_add_sec(ctx->eloop, 5,
1781 dhcpcd_readdumptimeout, ctx) == -1)
1782 return -1;
1783 return eloop_event_add(ctx->eloop, ctx->control_fd, ELE_READ,
1784 dhcpcd_readdump0, ctx);
1787 static void
1788 dhcpcd_fork_cb(void *arg, unsigned short events)
1790 struct dhcpcd_ctx *ctx = arg;
1791 int exit_code;
1792 ssize_t len;
1794 if (!(events & ELE_READ))
1795 logerrx("%s: unexpected event 0x%04x", __func__, events);
1797 len = read(ctx->fork_fd, &exit_code, sizeof(exit_code));
1798 if (len == -1) {
1799 logerr(__func__);
1800 exit_code = EXIT_FAILURE;
1801 } else if ((size_t)len < sizeof(exit_code)) {
1802 logerrx("%s: truncated read %zd (expected %zu)",
1803 __func__, len, sizeof(exit_code));
1804 exit_code = EXIT_FAILURE;
1806 if (ctx->options & DHCPCD_FORKED)
1807 eloop_exit(ctx->eloop, exit_code);
1808 else
1809 dhcpcd_signal_cb(exit_code, ctx);
1812 static void
1813 dhcpcd_stderr_cb(void *arg, unsigned short events)
1815 struct dhcpcd_ctx *ctx = arg;
1816 char log[BUFSIZ];
1817 ssize_t len;
1819 if (events & ELE_HANGUP)
1820 eloop_exit(ctx->eloop, EXIT_SUCCESS);
1822 if (!(events & ELE_READ))
1823 return;
1825 len = read(ctx->stderr_fd, log, sizeof(log));
1826 if (len == -1) {
1827 if (errno != ECONNRESET)
1828 logerr(__func__);
1829 return;
1832 log[len] = '\0';
1833 fprintf(stderr, "%s", log);
1836 static void
1837 dhcpcd_pidfile_timeout(void *arg)
1839 struct dhcpcd_ctx *ctx = arg;
1840 pid_t pid;
1842 pid = pidfile_read(ctx->pidfile);
1844 if(pid == -1)
1845 eloop_exit(ctx->eloop, EXIT_SUCCESS);
1846 else if (++ctx->duid_len >= 100) { /* overload duid_len */
1847 logerrx("pid %d failed to exit", pid);
1848 eloop_exit(ctx->eloop, EXIT_FAILURE);
1849 } else
1850 eloop_timeout_add_msec(ctx->eloop, 100,
1851 dhcpcd_pidfile_timeout, ctx);
1855 main(int argc, char **argv, char **envp)
1857 struct dhcpcd_ctx ctx;
1858 struct ifaddrs *ifaddrs = NULL;
1859 struct if_options *ifo;
1860 struct interface *ifp;
1861 sa_family_t family = AF_UNSPEC;
1862 int opt, oi = 0, i;
1863 unsigned int logopts, t;
1864 ssize_t len;
1865 #if defined(USE_SIGNALS) || !defined(THERE_IS_NO_FORK)
1866 pid_t pid;
1867 int fork_fd[2], stderr_fd[2];
1868 #endif
1869 #ifdef USE_SIGNALS
1870 int sig = 0;
1871 const char *siga = NULL;
1872 size_t si;
1873 #endif
1875 #ifdef SETPROCTITLE_H
1876 setproctitle_init(argc, argv, envp);
1877 #else
1878 UNUSED(envp);
1879 #endif
1881 /* Test for --help and --version */
1882 if (argc > 1) {
1883 if (strcmp(argv[1], "--help") == 0) {
1884 usage();
1885 return EXIT_SUCCESS;
1886 } else if (strcmp(argv[1], "--version") == 0) {
1887 printf(""PACKAGE" "VERSION"\n%s\n", dhcpcd_copyright);
1888 printf("Compiled in features:"
1889 #ifdef INET
1890 " INET"
1891 #endif
1892 #ifdef ARP
1893 " ARP"
1894 #endif
1895 #ifdef ARPING
1896 " ARPing"
1897 #endif
1898 #ifdef IPV4LL
1899 " IPv4LL"
1900 #endif
1901 #ifdef INET6
1902 " INET6"
1903 #endif
1904 #ifdef DHCP6
1905 " DHCPv6"
1906 #endif
1907 #ifdef AUTH
1908 " AUTH"
1909 #endif
1910 #ifdef PRIVSEP
1911 " PRIVSEP"
1912 #endif
1913 "\n");
1914 return EXIT_SUCCESS;
1918 memset(&ctx, 0, sizeof(ctx));
1920 ifo = NULL;
1921 ctx.cffile = CONFIG;
1922 ctx.script = UNCONST(dhcpcd_default_script);
1923 ctx.control_fd = ctx.control_unpriv_fd = ctx.link_fd = -1;
1924 ctx.pf_inet_fd = -1;
1925 #ifdef PF_LINK
1926 ctx.pf_link_fd = -1;
1927 #endif
1929 TAILQ_INIT(&ctx.control_fds);
1930 #ifdef USE_SIGNALS
1931 ctx.fork_fd = -1;
1932 #endif
1933 #ifdef PLUGIN_DEV
1934 ctx.dev_fd = -1;
1935 #endif
1936 #ifdef INET
1937 ctx.udp_rfd = -1;
1938 ctx.udp_wfd = -1;
1939 #endif
1940 #if defined(INET6) && !defined(__sun)
1941 ctx.nd_fd = -1;
1942 #endif
1943 #ifdef DHCP6
1944 ctx.dhcp6_rfd = -1;
1945 ctx.dhcp6_wfd = -1;
1946 #endif
1947 #ifdef PRIVSEP
1948 ctx.ps_log_fd = -1;
1949 TAILQ_INIT(&ctx.ps_processes);
1950 #endif
1952 /* Check our streams for validity */
1953 ctx.stdin_valid = fcntl(STDIN_FILENO, F_GETFD) != -1;
1954 ctx.stdout_valid = fcntl(STDOUT_FILENO, F_GETFD) != -1;
1955 ctx.stderr_valid = fcntl(STDERR_FILENO, F_GETFD) != -1;
1957 logopts = LOGERR_LOG | LOGERR_LOG_DATE | LOGERR_LOG_PID;
1958 if (ctx.stderr_valid)
1959 logopts |= LOGERR_ERR;
1961 i = 0;
1963 while ((opt = getopt_long(argc, argv,
1964 ctx.options & DHCPCD_PRINT_PIDFILE ? NOERR_IF_OPTS : IF_OPTS,
1965 cf_options, &oi)) != -1)
1967 switch (opt) {
1968 case '4':
1969 family = AF_INET;
1970 break;
1971 case '6':
1972 family = AF_INET6;
1973 break;
1974 case 'f':
1975 ctx.cffile = optarg;
1976 break;
1977 case 'j':
1978 free(ctx.logfile);
1979 ctx.logfile = strdup(optarg);
1980 break;
1981 #ifdef USE_SIGNALS
1982 case 'k':
1983 sig = SIGALRM;
1984 siga = "ALRM";
1985 break;
1986 case 'n':
1987 sig = SIGHUP;
1988 siga = "HUP";
1989 break;
1990 case 'q':
1991 /* -qq disables console output entirely.
1992 * This is important for systemd because it logs
1993 * both console AND syslog to the same log
1994 * resulting in untold confusion. */
1995 if (logopts & LOGERR_QUIET)
1996 logopts &= ~LOGERR_ERR;
1997 else
1998 logopts |= LOGERR_QUIET;
1999 break;
2000 case 'x':
2001 sig = SIGTERM;
2002 siga = "TERM";
2003 break;
2004 case 'N':
2005 sig = SIGUSR1;
2006 siga = "USR1";
2007 break;
2008 #endif
2009 case 'P':
2010 ctx.options |= DHCPCD_PRINT_PIDFILE;
2011 logopts &= ~(LOGERR_LOG | LOGERR_ERR);
2012 break;
2013 case 'T':
2014 i = 1;
2015 logopts &= ~LOGERR_LOG;
2016 break;
2017 case 'U':
2018 i = 3;
2019 break;
2020 case 'V':
2021 i = 2;
2022 break;
2023 case '?':
2024 if (ctx.options & DHCPCD_PRINT_PIDFILE)
2025 continue;
2026 usage();
2027 goto exit_failure;
2031 if (optind != argc - 1)
2032 ctx.options |= DHCPCD_MANAGER;
2034 logsetopts(logopts);
2035 logopen(ctx.logfile);
2037 ctx.argv = argv;
2038 ctx.argc = argc;
2039 ctx.ifc = argc - optind;
2040 ctx.ifv = argv + optind;
2042 rt_init(&ctx);
2044 ifo = read_config(&ctx, NULL, NULL, NULL);
2045 if (ifo == NULL) {
2046 if (ctx.options & DHCPCD_PRINT_PIDFILE)
2047 goto printpidfile;
2048 goto exit_failure;
2051 opt = add_options(&ctx, NULL, ifo, argc, argv);
2052 if (opt != 1) {
2053 if (ctx.options & DHCPCD_PRINT_PIDFILE)
2054 goto printpidfile;
2055 if (opt == 0)
2056 usage();
2057 goto exit_failure;
2059 if (i == 2) {
2060 printf("Interface options:\n");
2061 if (optind == argc - 1) {
2062 free_options(&ctx, ifo);
2063 ifo = read_config(&ctx, argv[optind], NULL, NULL);
2064 if (ifo == NULL)
2065 goto exit_failure;
2066 add_options(&ctx, NULL, ifo, argc, argv);
2068 if_printoptions();
2069 #ifdef INET
2070 if (family == 0 || family == AF_INET) {
2071 printf("\nDHCPv4 options:\n");
2072 dhcp_printoptions(&ctx,
2073 ifo->dhcp_override, ifo->dhcp_override_len);
2075 #endif
2076 #ifdef INET6
2077 if (family == 0 || family == AF_INET6) {
2078 printf("\nND options:\n");
2079 ipv6nd_printoptions(&ctx,
2080 ifo->nd_override, ifo->nd_override_len);
2081 #ifdef DHCP6
2082 printf("\nDHCPv6 options:\n");
2083 dhcp6_printoptions(&ctx,
2084 ifo->dhcp6_override, ifo->dhcp6_override_len);
2085 #endif
2087 #endif
2088 goto exit_success;
2090 ctx.options |= ifo->options;
2092 if (i == 1 || i == 3) {
2093 if (i == 1)
2094 ctx.options |= DHCPCD_TEST;
2095 else
2096 ctx.options |= DHCPCD_DUMPLEASE;
2097 ctx.options |= DHCPCD_PERSISTENT;
2098 ctx.options &= ~DHCPCD_DAEMONISE;
2101 #ifdef THERE_IS_NO_FORK
2102 ctx.options &= ~DHCPCD_DAEMONISE;
2103 #endif
2105 if (ctx.options & DHCPCD_DEBUG)
2106 logsetopts(logopts | LOGERR_DEBUG);
2108 if (!(ctx.options & (DHCPCD_TEST | DHCPCD_DUMPLEASE))) {
2109 printpidfile:
2110 /* If we have any other args, we should run as a single dhcpcd
2111 * instance for that interface. */
2112 if (optind == argc - 1 && !(ctx.options & DHCPCD_MANAGER)) {
2113 const char *per;
2114 const char *ifname;
2116 ifname = *ctx.ifv;
2117 if (ifname == NULL || strlen(ifname) > IF_NAMESIZE) {
2118 errno = ifname == NULL ? EINVAL : E2BIG;
2119 logerr("%s: ", ifname);
2120 goto exit_failure;
2122 /* Allow a dhcpcd interface per address family */
2123 switch(family) {
2124 case AF_INET:
2125 per = "-4";
2126 break;
2127 case AF_INET6:
2128 per = "-6";
2129 break;
2130 default:
2131 per = "";
2133 snprintf(ctx.pidfile, sizeof(ctx.pidfile),
2134 PIDFILE, ifname, per, ".");
2135 } else {
2136 snprintf(ctx.pidfile, sizeof(ctx.pidfile),
2137 PIDFILE, "", "", "");
2138 ctx.options |= DHCPCD_MANAGER;
2141 * If we are given any interfaces, we
2142 * cannot send a signal as that would impact
2143 * other interfaces.
2145 if (optind != argc)
2146 sig = 0;
2148 if (ctx.options & DHCPCD_PRINT_PIDFILE) {
2149 printf("%s\n", ctx.pidfile);
2150 goto exit_success;
2154 if (chdir("/") == -1)
2155 logerr("%s: chdir: /", __func__);
2157 /* Freeing allocated addresses from dumping leases can trigger
2158 * eloop removals as well, so init here. */
2159 if ((ctx.eloop = eloop_new()) == NULL) {
2160 logerr("%s: eloop_init", __func__);
2161 goto exit_failure;
2164 #ifdef USE_SIGNALS
2165 for (si = 0; si < dhcpcd_signals_ignore_len; si++)
2166 signal(dhcpcd_signals_ignore[si], SIG_IGN);
2168 /* Save signal mask, block and redirect signals to our handler */
2169 eloop_signal_set_cb(ctx.eloop,
2170 dhcpcd_signals, dhcpcd_signals_len,
2171 dhcpcd_signal_cb, &ctx);
2172 if (eloop_signal_mask(ctx.eloop, &ctx.sigset) == -1) {
2173 logerr("%s: eloop_signal_mask", __func__);
2174 goto exit_failure;
2177 if (sig != 0) {
2178 pid = pidfile_read(ctx.pidfile);
2179 if (pid != 0 && pid != -1)
2180 loginfox("sending signal %s to pid %d", siga, pid);
2181 if (pid == 0 || pid == -1 || kill(pid, sig) != 0) {
2182 if (pid != 0 && pid != -1 && errno != ESRCH) {
2183 logerr("kill");
2184 goto exit_failure;
2186 unlink(ctx.pidfile);
2187 /* We can still continue and send the command
2188 * via the control socket. */
2189 } else {
2190 if (sig == SIGHUP || sig == SIGUSR1)
2191 goto exit_success;
2192 /* Spin until it exits */
2193 loginfox("waiting for pid %d to exit", pid);
2194 dhcpcd_pidfile_timeout(&ctx);
2195 goto run_loop;
2198 #endif
2200 #ifdef PRIVSEP
2201 ps_init(&ctx);
2202 #endif
2204 #ifndef SMALL
2205 if (ctx.options & DHCPCD_DUMPLEASE &&
2206 ioctl(fileno(stdin), FIONREAD, &i, sizeof(i)) == 0 &&
2207 i > 0)
2209 ctx.options |= DHCPCD_FORKED; /* pretend child process */
2210 #ifdef PRIVSEP
2211 if (IN_PRIVSEP(&ctx) && ps_managersandbox(&ctx, NULL) == -1)
2212 goto exit_failure;
2213 #endif
2214 ifp = calloc(1, sizeof(*ifp));
2215 if (ifp == NULL) {
2216 logerr(__func__);
2217 goto exit_failure;
2219 ifp->ctx = &ctx;
2220 ifp->options = ifo;
2221 switch (family) {
2222 case AF_INET:
2223 #ifdef INET
2224 if (dhcp_dump(ifp) == -1)
2225 goto exit_failure;
2226 break;
2227 #else
2228 logerrx("No DHCP support");
2229 goto exit_failure;
2230 #endif
2231 case AF_INET6:
2232 #ifdef DHCP6
2233 if (dhcp6_dump(ifp) == -1)
2234 goto exit_failure;
2235 break;
2236 #else
2237 logerrx("No DHCP6 support");
2238 goto exit_failure;
2239 #endif
2240 default:
2241 logerrx("Family not specified. Please use -4 or -6.");
2242 goto exit_failure;
2244 goto exit_success;
2246 #endif
2248 /* Try and contact the manager process to send the instruction. */
2249 if (!(ctx.options & DHCPCD_TEST)) {
2250 ctx.options |= DHCPCD_FORKED; /* avoid socket unlink */
2251 if (!(ctx.options & DHCPCD_MANAGER))
2252 ctx.control_fd = control_open(argv[optind], family,
2253 ctx.options & DHCPCD_DUMPLEASE);
2254 if (!(ctx.options & DHCPCD_MANAGER) && ctx.control_fd == -1)
2255 ctx.control_fd = control_open(argv[optind], AF_UNSPEC,
2256 ctx.options & DHCPCD_DUMPLEASE);
2257 if (ctx.control_fd == -1)
2258 ctx.control_fd = control_open(NULL, AF_UNSPEC,
2259 ctx.options & DHCPCD_DUMPLEASE);
2260 if (ctx.control_fd != -1) {
2261 #ifdef PRIVSEP
2262 if (IN_PRIVSEP(&ctx) &&
2263 ps_managersandbox(&ctx, NULL) == -1)
2264 goto exit_failure;
2265 #endif
2266 if (!(ctx.options & DHCPCD_DUMPLEASE))
2267 loginfox("sending commands to dhcpcd process");
2268 len = control_send(&ctx, argc, argv);
2269 if (len > 0)
2270 logdebugx("send OK");
2271 else {
2272 logerr("%s: control_send", __func__);
2273 goto exit_failure;
2275 if (ctx.options & DHCPCD_DUMPLEASE) {
2276 if (dhcpcd_readdump(&ctx) == -1) {
2277 logerr("%s: dhcpcd_readdump", __func__);
2278 goto exit_failure;
2280 goto run_loop;
2282 goto exit_success;
2283 } else {
2284 if (errno != ENOENT)
2285 logerr("%s: control_open", __func__);
2286 /* If asking dhcpcd to exit and we failed to
2287 * send a signal or a message then we
2288 * don't proceed past here. */
2289 if (ctx.options & DHCPCD_DUMPLEASE ||
2290 sig == SIGTERM || sig == SIGALRM)
2292 if (errno == ENOENT)
2293 logerrx(PACKAGE" is not running");
2294 goto exit_failure;
2296 if (errno == EPERM || errno == EACCES)
2297 goto exit_failure;
2299 ctx.options &= ~DHCPCD_FORKED;
2302 if (!(ctx.options & DHCPCD_TEST)) {
2303 /* Ensure we have the needed directories */
2304 if (mkdir(DBDIR, 0750) == -1 && errno != EEXIST)
2305 logerr("%s: mkdir: %s", __func__, DBDIR);
2306 if (mkdir(RUNDIR, 0755) == -1 && errno != EEXIST)
2307 logerr("%s: mkdir: %s", __func__, RUNDIR);
2308 if ((pid = pidfile_lock(ctx.pidfile)) != 0) {
2309 if (pid == -1)
2310 logerr("%s: pidfile_lock: %s",
2311 __func__, ctx.pidfile);
2312 else
2313 logerrx(PACKAGE
2314 " already running on pid %d (%s)",
2315 pid, ctx.pidfile);
2316 goto exit_failure;
2320 loginfox(PACKAGE "-" VERSION " starting");
2321 if (ctx.stdin_valid && freopen(_PATH_DEVNULL, "w", stdin) == NULL)
2322 logwarn("freopen stdin");
2324 #if defined(USE_SIGNALS) && !defined(THERE_IS_NO_FORK)
2325 if (!(ctx.options & DHCPCD_DAEMONISE))
2326 goto start_manager;
2328 if (xsocketpair(AF_UNIX, SOCK_SEQPACKET|SOCK_CXNB, 0, fork_fd) == -1 ||
2329 (ctx.stderr_valid &&
2330 xsocketpair(AF_UNIX, SOCK_SEQPACKET|SOCK_CXNB, 0, stderr_fd) == -1))
2332 logerr("socketpair");
2333 goto exit_failure;
2335 switch (pid = fork()) {
2336 case -1:
2337 logerr("fork");
2338 goto exit_failure;
2339 case 0:
2340 ctx.fork_fd = fork_fd[1];
2341 close(fork_fd[0]);
2342 #ifdef PRIVSEP_RIGHTS
2343 if (ps_rights_limit_fd(ctx.fork_fd) == -1) {
2344 logerr("ps_rights_limit_fdpair");
2345 goto exit_failure;
2347 #endif
2348 if (eloop_event_add(ctx.eloop, ctx.fork_fd, ELE_READ,
2349 dhcpcd_fork_cb, &ctx) == -1)
2350 logerr("%s: eloop_event_add", __func__);
2353 * Redirect stderr to the stderr socketpair.
2354 * Redirect stdout as well.
2355 * dhcpcd doesn't output via stdout, but something in
2356 * a called script might.
2358 if (ctx.stderr_valid) {
2359 if (dup2(stderr_fd[1], STDERR_FILENO) == -1 ||
2360 (ctx.stdout_valid &&
2361 dup2(stderr_fd[1], STDOUT_FILENO) == -1))
2362 logerr("dup2");
2363 close(stderr_fd[0]);
2364 close(stderr_fd[1]);
2365 } else if (ctx.stdout_valid) {
2366 if (freopen(_PATH_DEVNULL, "w", stdout) == NULL)
2367 logerr("freopen stdout");
2369 if (setsid() == -1) {
2370 logerr("%s: setsid", __func__);
2371 goto exit_failure;
2373 /* Ensure we can never get a controlling terminal */
2374 switch (pid = fork()) {
2375 case -1:
2376 logerr("fork");
2377 goto exit_failure;
2378 case 0:
2379 eloop_forked(ctx.eloop);
2380 break;
2381 default:
2382 ctx.options |= DHCPCD_FORKED; /* A lie */
2383 i = EXIT_SUCCESS;
2384 goto exit1;
2386 break;
2387 default:
2388 setproctitle("[launcher]");
2389 ctx.options |= DHCPCD_FORKED | DHCPCD_LAUNCHER;
2390 ctx.fork_fd = fork_fd[0];
2391 close(fork_fd[1]);
2392 #ifdef PRIVSEP_RIGHTS
2393 if (ps_rights_limit_fd(ctx.fork_fd) == -1) {
2394 logerr("ps_rights_limit_fd");
2395 goto exit_failure;
2397 #endif
2398 if (eloop_event_add(ctx.eloop, ctx.fork_fd, ELE_READ,
2399 dhcpcd_fork_cb, &ctx) == -1)
2400 logerr("%s: eloop_event_add", __func__);
2402 if (ctx.stderr_valid) {
2403 ctx.stderr_fd = stderr_fd[0];
2404 close(stderr_fd[1]);
2405 #ifdef PRIVSEP_RIGHTS
2406 if (ps_rights_limit_fd(ctx.stderr_fd) == 1) {
2407 logerr("ps_rights_limit_fd");
2408 goto exit_failure;
2410 #endif
2411 if (eloop_event_add(ctx.eloop, ctx.stderr_fd, ELE_READ,
2412 dhcpcd_stderr_cb, &ctx) == -1)
2413 logerr("%s: eloop_event_add", __func__);
2415 #ifdef PRIVSEP
2416 if (IN_PRIVSEP(&ctx) && ps_managersandbox(&ctx, NULL) == -1)
2417 goto exit_failure;
2418 #endif
2419 goto run_loop;
2422 /* We have now forked, setsid, forked once more.
2423 * From this point on, we are the controlling daemon. */
2424 logdebugx("spawned manager process on PID %d", getpid());
2425 start_manager:
2426 ctx.options |= DHCPCD_STARTED;
2427 if ((pid = pidfile_lock(ctx.pidfile)) != 0) {
2428 logerr("%s: pidfile_lock %d", __func__, pid);
2429 #ifdef PRIVSEP
2430 /* privsep has not started ... */
2431 ctx.options &= ~DHCPCD_PRIVSEP;
2432 #endif
2433 goto exit_failure;
2435 #endif
2437 os_init();
2439 #if defined(BSD) && defined(INET6)
2440 /* Disable the kernel RTADV sysctl as early as possible. */
2441 if (ctx.options & DHCPCD_IPV6 && ctx.options & DHCPCD_IPV6RS)
2442 if_disable_rtadv();
2443 #endif
2445 #ifdef PRIVSEP
2446 if (IN_PRIVSEP(&ctx) && ps_start(&ctx) == -1) {
2447 logerr("ps_start");
2448 goto exit_failure;
2450 if (ctx.options & DHCPCD_FORKED)
2451 goto run_loop;
2452 #endif
2454 if (!(ctx.options & DHCPCD_TEST)) {
2455 if (control_start(&ctx,
2456 ctx.options & DHCPCD_MANAGER ?
2457 NULL : argv[optind], family) == -1)
2459 logerr("%s: control_start", __func__);
2460 goto exit_failure;
2464 #ifdef PLUGIN_DEV
2465 /* Start any dev listening plugin which may want to
2466 * change the interface name provided by the kernel */
2467 if (!IN_PRIVSEP(&ctx) &&
2468 (ctx.options & (DHCPCD_MANAGER | DHCPCD_DEV)) ==
2469 (DHCPCD_MANAGER | DHCPCD_DEV))
2470 dev_start(&ctx, dhcpcd_handleinterface);
2471 #endif
2473 setproctitle("%s%s%s",
2474 ctx.options & DHCPCD_MANAGER ? "[manager]" : argv[optind],
2475 ctx.options & DHCPCD_IPV4 ? " [ip4]" : "",
2476 ctx.options & DHCPCD_IPV6 ? " [ip6]" : "");
2478 if (if_opensockets(&ctx) == -1) {
2479 logerr("%s: if_opensockets", __func__);
2480 goto exit_failure;
2482 #ifndef SMALL
2483 dhcpcd_setlinkrcvbuf(&ctx);
2484 #endif
2486 /* Try and create DUID from the machine UUID. */
2487 dhcpcd_initduid(&ctx, NULL);
2489 /* Cache the default vendor option. */
2490 if (dhcp_vendor(ctx.vendor, sizeof(ctx.vendor)) == -1)
2491 logerr("dhcp_vendor");
2493 /* Start handling kernel messages for interfaces, addresses and
2494 * routes. */
2495 if (eloop_event_add(ctx.eloop, ctx.link_fd, ELE_READ,
2496 dhcpcd_handlelink, &ctx) == -1)
2497 logerr("%s: eloop_event_add", __func__);
2499 #ifdef PRIVSEP
2500 if (IN_PRIVSEP(&ctx) && ps_managersandbox(&ctx, "stdio route") == -1)
2501 goto exit_failure;
2502 #endif
2504 /* When running dhcpcd against a single interface, we need to retain
2505 * the old behaviour of waiting for an IP address */
2506 if (ctx.ifc == 1 && !(ctx.options & DHCPCD_BACKGROUND))
2507 ctx.options |= DHCPCD_WAITIP;
2509 ctx.ifaces = if_discover(&ctx, &ifaddrs, ctx.ifc, ctx.ifv);
2510 if (ctx.ifaces == NULL) {
2511 logerr("%s: if_discover", __func__);
2512 goto exit_failure;
2514 for (i = 0; i < ctx.ifc; i++) {
2515 if ((ifp = if_find(ctx.ifaces, ctx.ifv[i])) == NULL)
2516 logerrx("%s: interface not found",
2517 ctx.ifv[i]);
2518 else if (!ifp->active)
2519 logerrx("%s: interface has an invalid configuration",
2520 ctx.ifv[i]);
2522 TAILQ_FOREACH(ifp, ctx.ifaces, next) {
2523 if (ifp->active == IF_ACTIVE_USER)
2524 break;
2526 if (ifp == NULL) {
2527 if (ctx.ifc == 0) {
2528 int loglevel;
2530 loglevel = ctx.options & DHCPCD_INACTIVE ?
2531 LOG_DEBUG : LOG_ERR;
2532 logmessage(loglevel, "no valid interfaces found");
2533 dhcpcd_daemonise(&ctx);
2534 } else
2535 goto exit_failure;
2536 if (!(ctx.options & DHCPCD_LINK)) {
2537 logerrx("aborting as link detection is disabled");
2538 goto exit_failure;
2542 TAILQ_FOREACH(ifp, ctx.ifaces, next) {
2543 if (ifp->active)
2544 dhcpcd_initstate1(ifp, argc, argv, 0);
2546 if_learnaddrs(&ctx, ctx.ifaces, &ifaddrs);
2547 if_freeifaddrs(&ctx, &ifaddrs);
2548 ifaddrs = NULL;
2550 if (ctx.options & DHCPCD_BACKGROUND)
2551 dhcpcd_daemonise(&ctx);
2553 opt = 0;
2554 TAILQ_FOREACH(ifp, ctx.ifaces, next) {
2555 if (ifp->active) {
2556 run_preinit(ifp);
2557 if (if_is_link_up(ifp))
2558 opt = 1;
2562 if (!(ctx.options & DHCPCD_BACKGROUND)) {
2563 if (ctx.options & DHCPCD_MANAGER)
2564 t = ifo->timeout;
2565 else {
2566 t = 0;
2567 TAILQ_FOREACH(ifp, ctx.ifaces, next) {
2568 if (ifp->active) {
2569 t = ifp->options->timeout;
2570 break;
2574 if (opt == 0 &&
2575 ctx.options & DHCPCD_LINK &&
2576 !(ctx.options & DHCPCD_WAITIP))
2578 int loglevel;
2580 loglevel = ctx.options & DHCPCD_INACTIVE ?
2581 LOG_DEBUG : LOG_WARNING;
2582 logmessage(loglevel, "no interfaces have a carrier");
2583 dhcpcd_daemonise(&ctx);
2584 } else if (t > 0 &&
2585 /* Test mode removes the daemonise bit, so check for both */
2586 ctx.options & (DHCPCD_DAEMONISE | DHCPCD_TEST))
2588 eloop_timeout_add_sec(ctx.eloop, t,
2589 handle_exit_timeout, &ctx);
2592 free_options(&ctx, ifo);
2593 ifo = NULL;
2595 TAILQ_FOREACH(ifp, ctx.ifaces, next) {
2596 if (ifp->active)
2597 eloop_timeout_add_sec(ctx.eloop, 0,
2598 dhcpcd_prestartinterface, ifp);
2601 run_loop:
2602 i = eloop_start(ctx.eloop, &ctx.sigset);
2603 if (i < 0) {
2604 logerr("%s: eloop_start", __func__);
2605 goto exit_failure;
2607 goto exit1;
2609 exit_success:
2610 i = EXIT_SUCCESS;
2611 goto exit1;
2613 exit_failure:
2614 i = EXIT_FAILURE;
2616 exit1:
2617 if (!(ctx.options & DHCPCD_TEST) && control_stop(&ctx) == -1)
2618 logerr("%s: control_stop", __func__);
2619 if_freeifaddrs(&ctx, &ifaddrs);
2620 #ifdef PRIVSEP
2621 ps_stop(&ctx);
2622 #endif
2623 /* Free memory and close fd's */
2624 if (ctx.ifaces) {
2625 while ((ifp = TAILQ_FIRST(ctx.ifaces))) {
2626 TAILQ_REMOVE(ctx.ifaces, ifp, next);
2627 if_free(ifp);
2629 free(ctx.ifaces);
2630 ctx.ifaces = NULL;
2632 free_options(&ctx, ifo);
2633 #ifdef HAVE_OPEN_MEMSTREAM
2634 if (ctx.script_fp)
2635 fclose(ctx.script_fp);
2636 #endif
2637 free(ctx.script_buf);
2638 free(ctx.script_env);
2639 rt_dispose(&ctx);
2640 free(ctx.duid);
2641 if (ctx.link_fd != -1) {
2642 eloop_event_delete(ctx.eloop, ctx.link_fd);
2643 close(ctx.link_fd);
2645 if_closesockets(&ctx);
2646 free_globals(&ctx);
2647 #ifdef INET6
2648 ipv6_ctxfree(&ctx);
2649 #endif
2650 #ifdef PLUGIN_DEV
2651 dev_stop(&ctx);
2652 #endif
2653 if (ctx.script != dhcpcd_default_script)
2654 free(ctx.script);
2655 #ifdef PRIVSEP
2656 if (ps_stopwait(&ctx) != EXIT_SUCCESS)
2657 i = EXIT_FAILURE;
2658 #endif
2659 if (ctx.options & DHCPCD_STARTED && !(ctx.options & DHCPCD_FORKED))
2660 loginfox(PACKAGE " exited");
2661 #ifdef PRIVSEP
2662 if (ps_root_stop(&ctx) == -1)
2663 i = EXIT_FAILURE;
2664 eloop_free(ctx.ps_eloop);
2665 #endif
2666 eloop_free(ctx.eloop);
2667 logclose();
2668 free(ctx.logfile);
2669 free(ctx.ctl_buf);
2670 #ifdef SETPROCTITLE_H
2671 setproctitle_fini();
2672 #endif
2673 #ifdef USE_SIGNALS
2674 if (ctx.options & DHCPCD_STARTED) {
2675 /* Try to detach from the launch process. */
2676 if (ctx.fork_fd != -1 &&
2677 write(ctx.fork_fd, &i, sizeof(i)) == -1)
2678 logerr("%s: write", __func__);
2680 if (ctx.options & (DHCPCD_FORKED | DHCPCD_PRIVSEP))
2681 _exit(i); /* so atexit won't remove our pidfile */
2682 #endif
2683 return i;