1 /* SPDX-License-Identifier: BSD-2-Clause */
3 * dhcpcd - DHCP client daemon
4 * Copyright (c) 2006-2021 Roy Marples <roy@marples.name>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
29 static const char dhcpcd_copyright[] = "Copyright (c) 2006-2021 Roy Marples";
32 #include <sys/ioctl.h>
33 #include <sys/socket.h>
36 #include <sys/types.h>
59 #include "dhcp-common.h"
66 #include "if-options.h"
76 #include <sys/capsicum.h>
83 const int dhcpcd_signals[] = {
92 const size_t dhcpcd_signals_len = __arraycount(dhcpcd_signals);
94 const int dhcpcd_signals_ignore[] = {
97 const size_t dhcpcd_signals_ignore_len = __arraycount(dhcpcd_signals_ignore);
100 const char *dhcpcd_default_script = SCRIPT;
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");
129 free_globals(struct dhcpcd_ctx *ctx)
131 struct dhcp_opt *opt;
134 for (; ctx->ifac > 0; ctx->ifac--)
135 free(ctx->ifav[ctx->ifac - 1]);
140 for (; ctx->ifdc > 0; ctx->ifdc--)
141 free(ctx->ifdv[ctx->ifdc - 1]);
146 for (; ctx->ifcc > 0; ctx->ifcc--)
147 free(ctx->ifcv[ctx->ifcc - 1]);
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;
164 for (opt = ctx->nd_opts;
165 ctx->nd_opts_len > 0;
166 opt++, ctx->nd_opts_len--)
167 free_dhcp_opt_embenc(opt);
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;
183 for (opt = ctx->vivso;
185 opt++, ctx->vivso_len--)
186 free_dhcp_opt_embenc(opt);
193 handle_exit_timeout(void *arg)
195 struct dhcpcd_ctx *ctx;
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);
209 ctx->options |= DHCPCD_NOWAITIP;
210 dhcpcd_daemonise(ctx);
230 dhcpcd_ifafwaiting(const struct interface *ifp)
232 unsigned long long opts;
233 bool foundany = false;
235 if (ifp->active != IF_ACTIVE_USER)
238 #define DHCPCD_WAITALL (DHCPCD_WAITIP4 | DHCPCD_WAITIP6)
239 opts = ifp->options->options;
241 if (opts & DHCPCD_WAITIP4 ||
242 (opts & DHCPCD_WAITIP && !(opts & DHCPCD_WAITALL)))
244 bool foundaddr = ipv4_hasaddr(ifp);
246 if (opts & DHCPCD_WAITIP4 && !foundaddr)
253 if (opts & DHCPCD_WAITIP6 ||
254 (opts & DHCPCD_WAITIP && !(opts & DHCPCD_WAITALL)))
256 bool foundaddr = ipv6_hasaddr(ifp);
258 if (opts & DHCPCD_WAITIP6 && !foundaddr)
265 if (opts & DHCPCD_WAITIP && !(opts & DHCPCD_WAITALL) && !foundany)
271 dhcpcd_afwaiting(const struct dhcpcd_ctx *ctx)
273 unsigned long long opts;
274 const struct interface *ifp;
277 if (!(ctx->options & DHCPCD_WAITOPTS))
281 TAILQ_FOREACH(ifp, ctx->ifaces, next) {
283 if (opts & (DHCPCD_WAITIP | DHCPCD_WAITIP4) &&
285 opts &= ~(DHCPCD_WAITIP | DHCPCD_WAITIP4);
288 if (opts & (DHCPCD_WAITIP | DHCPCD_WAITIP6) &&
290 opts &= ~(DHCPCD_WAITIP | DHCPCD_WAITIP6);
292 if (!(opts & DHCPCD_WAITOPTS))
295 if (opts & DHCPCD_WAITIP)
297 else if (opts & DHCPCD_WAITIP4)
299 else if (opts & DHCPCD_WAITIP6)
307 dhcpcd_ipwaited(struct dhcpcd_ctx *ctx)
309 struct interface *ifp;
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));
320 if ((af = dhcpcd_afwaiting(ctx)) != AF_MAX) {
321 logdebugx("waiting for an %s address",
329 /* Returns the pid of the child, otherwise 0. */
331 dhcpcd_daemonise(struct dhcpcd_ctx *ctx)
333 #ifdef THERE_IS_NO_FORK
334 eloop_timeout_delete(ctx->eloop, handle_exit_timeout, ctx);
339 unsigned int logopts = loggetopts();
341 if (ctx->options & DHCPCD_DAEMONISE &&
342 !(ctx->options & (DHCPCD_DAEMONISED | DHCPCD_NOWAITIP)))
344 if (!dhcpcd_ipwaited(ctx))
348 if (ctx->options & DHCPCD_ONESHOT) {
349 loginfox("exiting due to oneshot");
350 eloop_exit(ctx->eloop, EXIT_SUCCESS);
354 eloop_timeout_delete(ctx->eloop, handle_exit_timeout, ctx);
355 if (ctx->options & DHCPCD_DAEMONISED ||
356 !(ctx->options & DHCPCD_DAEMONISE))
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());
364 if (write(ctx->fork_fd, &i, sizeof(i)) == -1)
366 ctx->options |= DHCPCD_DAEMONISED;
367 eloop_event_delete(ctx->eloop, ctx->fork_fd);
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;
387 dhcpcd_drop(struct interface *ifp, int stop)
391 dhcp6_drop(ifp, stop ? NULL : "EXPIRE6");
401 dhcp_drop(ifp, stop ? "STOP" : "EXPIRE");
406 #if !defined(DHCP6) && !defined(DHCP)
412 stop_interface(struct interface *ifp, const char *reason)
414 struct dhcpcd_ctx *ctx;
417 loginfox("%s: removing interface", ifp->name);
418 ifp->options->options |= DHCPCD_STOPPING;
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);
435 configure_interface1(struct interface *ifp)
437 struct if_options *ifo = ifp->options;
439 /* Do any platform specific configuration */
442 /* If we want to release a lease, we can't really persist the
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;
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;
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
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
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.
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) {
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)) {
522 ifp->hwaddr + ifp->hwlen - sizeof(ifo->iaid),
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);
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;
543 if (ifo->ia_len == 0 && ifo->options & DHCPCD_IPV6 &&
544 ifp->name[0] != '\0')
546 ifo->ia = malloc(sizeof(*ifo->ia));
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));
556 ifo->ia->sla_len = 0;
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;
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];
588 r = print_string(pssid, sizeof(pssid), OT_ESCSTRING,
589 ifp->ssid, ifp->ssid_len);
596 ifo = read_config(ifp->ctx, ifp->name, pssid, profile);
598 logdebugx("%s: no profile %s", ifp->name, profile);
601 if (profile != NULL) {
602 strlcpy(ifp->profile, profile, sizeof(ifp->profile));
603 loginfox("%s: selected profile %s", ifp->name, profile);
605 *ifp->profile = '\0';
607 free_options(ifp->ctx, ifp->options);
610 add_options(ifp->ctx, ifp->name, ifp->options,
611 ifp->ctx->argc, ifp->ctx->argv);
612 configure_interface1(ifp);
618 configure_interface(struct interface *ifp, int argc, char **argv,
619 unsigned long long options)
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;
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",
643 dhcpcd_initstate2(struct interface *ifp, unsigned long long options)
645 struct if_options *ifo;
648 if ((ifo = default_config(ifp->ctx)) == NULL) {
652 ifo->options |= options;
659 if (ifo->options & DHCPCD_IPV6 && ipv6_init(ifp->ctx) == -1) {
661 ifo->options &= ~DHCPCD_IPV6;
667 dhcpcd_initstate1(struct interface *ifp, int argc, char **argv,
668 unsigned long long options)
671 configure_interface(ifp, argc, argv, options);
673 dhcpcd_initstate2(ifp, 0);
677 dhcpcd_initstate(struct interface *ifp, unsigned long long options)
680 dhcpcd_initstate1(ifp, ifp->ctx->argc, ifp->ctx->argv, options);
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)
695 loginfox("%s: connected to Access Point: %s", ifp->name, pssid);
699 dhcpcd_nocarrier_roaming(struct interface *ifp)
702 loginfox("%s: carrier lost - roaming", ifp->name);
714 rt_build(ifp->ctx, AF_UNSPEC);
715 script_runreason(ifp, "NOCARRIER_ROAMING");
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;
727 if (!if_is_link_up(ifp)) {
728 if (!ifp->active || (!was_link_up && !was_roaming))
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
737 if (!ifp->options->randomise_hwaddr && if_roaming(ifp)) {
738 dhcpcd_nocarrier_roaming(ifp);
742 loginfox("%s: carrier lost", ifp->name);
743 script_runreason(ifp, "NOCARRIER");
746 if (ifp->options->randomise_hwaddr) {
747 bool is_up = ifp->flags & IFF_UP;
751 if (if_randomisemac(ifp) == -1 && errno != ENXIO)
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.
771 if (carrier == LINK_UNKNOWN)
772 loginfox("%s: carrier unknown, assuming up", ifp->name);
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);
785 uint8_t ossid[IF_SSIDLEN];
788 olen = ifp->ssid_len;
789 memcpy(ossid, ifp->ssid, ifp->ssid_len);
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);
807 dhcpcd_initstate(ifp, 0);
808 script_runreason(ifp, "CARRIER");
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);
820 dhcpcd_startinterface(ifp);
824 warn_iaid_conflict(struct interface *ifp, uint16_t ia_type, uint8_t *iaid)
826 struct interface *ifn;
832 TAILQ_FOREACH(ifn, ifp->ctx->ifaces, next) {
833 if (ifn == ifp || !ifn->active)
835 if (ifn->options->options & DHCPCD_ANONYMOUS)
838 memcmp(ifn->options->iaid, iaid,
839 sizeof(ifn->options->iaid)) == 0)
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)
851 /* This is only a problem if the interfaces are on the same network. */
853 logerrx("%s: IAID conflicts with one assigned to %s",
854 ifp->name, ifn->name);
858 dhcpcd_initduid(struct dhcpcd_ctx *ctx, struct interface *ifp)
860 char buf[DUID_LEN * 3];
862 if (ctx->duid != NULL) {
869 if (ctx->duid == NULL)
874 hwaddr_ntoa(ctx->duid, ctx->duid_len, buf, sizeof(buf)));
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);
888 if (ifo->options & (DHCPCD_DUID | DHCPCD_IPV6) &&
889 !(ifo->options & DHCPCD_ANONYMOUS))
891 char buf[sizeof(ifo->iaid) * 3];
897 /* Try and init DUID from the interface hardware address */
898 dhcpcd_initduid(ifp->ctx, ifp);
901 loginfox("%s: IAID %s", ifp->name,
902 hwaddr_ntoa(ifo->iaid, sizeof(ifo->iaid),
904 warn_iaid_conflict(ifp, 0, ifo->iaid);
907 for (i = 0; i < ifo->ia_len; 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),
914 warn_iaid_conflict(ifp, ia->ia_type, ia->iaid);
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)
935 /* DHCPv6 could be turned off, but the interface
936 * is still delegated to. */
938 dhcp6_find_delegates(ifp);
940 if (ifo->options & DHCPCD_DHCP6) {
941 if (ifp->active == IF_ACTIVE_USER) {
944 if (ifo->options & DHCPCD_IA_FORCED)
945 d6_state = DH6S_INIT;
946 else if (ifo->options & DHCPCD_INFORM6)
947 d6_state = DH6S_INFORM;
949 d6_state = DH6S_CONFIRM;
950 if (dhcp6_start(ifp, d6_state) == -1)
951 logerr("%s: dhcp6_start", ifp->name);
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)
968 dhcpcd_prestartinterface(void *arg)
970 struct interface *ifp = arg;
971 struct dhcpcd_ctx *ctx = ifp->ctx;
974 if (ifp->carrier <= LINK_DOWN &&
975 ifp->options->randomise_hwaddr &&
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)
990 if (if_up(ifp) == -1)
994 dhcpcd_startinterface(ifp);
998 run_preinit(struct interface *ifp)
1001 if (ifp->ctx->options & DHCPCD_TEST)
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");
1013 dhcpcd_activateinterface(struct interface *ifp, unsigned long long options)
1019 ifp->active = IF_ACTIVE;
1020 dhcpcd_initstate2(ifp, options);
1022 /* It's possible we might not have been able to load
1027 configure_interface1(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 };
1043 ifp = if_find(ctx->ifaces, ifname);
1049 logdebugx("%s: interface departed", ifp->name);
1050 stop_interface(ifp, "DEPARTED");
1052 TAILQ_REMOVE(ctx->ifaces, ifp, next);
1057 ifs = if_discover(ctx, &ifaddrs, -1, UNCONST(argv));
1063 ifp = if_find(ifs, ifname);
1065 /* This can happen if an interface is quickly added
1066 * and then removed. */
1073 /* Check if we already have the interface */
1074 iff = if_find(ctx->ifaces, ifp->name);
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);
1085 TAILQ_REMOVE(ifs, ifp, next);
1086 TAILQ_INSERT_TAIL(ctx->ifaces, ifp, next);
1088 logdebugx("%s: interface added", ifp->name);
1089 dhcpcd_initstate(ifp, 0);
1096 if_learnaddrs(ctx, ifs, &ifaddrs);
1098 dhcpcd_prestartinterface(iff);
1102 /* Free our discovered list */
1103 while ((ifp = TAILQ_FIRST(ifs))) {
1104 TAILQ_REMOVE(ifs, ifp, next);
1113 dhcpcd_handlelink(void *arg)
1115 struct dhcpcd_ctx *ctx = arg;
1117 if (if_handlelink(ctx) == -1) {
1118 if (errno == ENOBUFS || errno == ENOMEM) {
1119 dhcpcd_linkoverflow(ctx);
1122 if (errno != ENOTSUP)
1128 dhcpcd_checkcarrier(void *arg)
1130 struct interface *ifp0 = arg, *ifp;
1132 ifp = if_find(ifp0->ctx->ifaces, ifp0->name);
1133 if (ifp == NULL || ifp->carrier == ifp0->carrier)
1136 dhcpcd_handlecarrier(ifp, ifp0->carrier, ifp0->flags);
1142 dhcpcd_setlinkrcvbuf(struct dhcpcd_ctx *ctx)
1146 if (ctx->link_rcvbuf == 0)
1149 logdebugx("setting route socket receive buffer size to %d bytes",
1152 socklen = sizeof(ctx->link_rcvbuf);
1153 if (setsockopt(ctx->link_fd, SOL_SOCKET,
1154 SO_RCVBUF, &ctx->link_rcvbuf, socklen) == -1)
1160 dhcpcd_runprestartinterface(void *arg)
1162 struct interface *ifp = arg;
1165 dhcpcd_prestartinterface(ifp);
1169 dhcpcd_linkoverflow(struct dhcpcd_ctx *ctx)
1176 struct if_head *ifaces;
1177 struct ifaddrs *ifaddrs;
1178 struct interface *ifp, *ifn, *ifp1;
1180 socklen = sizeof(rcvbuflen);
1181 if (getsockopt(ctx->link_fd, SOL_SOCKET,
1182 SO_RCVBUF, &rcvbuflen, &socklen) == -1) {
1183 logerr("%s: getsockopt", __func__);
1191 logerrx("route socket overflowed (rcvbuflen %d)"
1192 " - learning interface state", rcvbuflen);
1194 /* Drain the socket.
1195 * We cannot open a new one due to privsep. */
1198 rlen = read(ctx->link_fd, buf, sizeof(buf));
1199 if (++rcnt % 1000 == 0)
1200 logwarnx("drained %zu messages", rcnt);
1201 } while (rlen != -1 || errno == ENOBUFS || errno == ENOMEM);
1202 if (rcnt % 1000 != 0)
1203 logwarnx("drained %zu messages", rcnt);
1205 /* Work out the current interfaces. */
1206 ifaces = if_discover(ctx, &ifaddrs, ctx->ifc, ctx->ifv);
1207 if (ifaces == NULL) {
1212 /* Punt departed interfaces */
1213 TAILQ_FOREACH_SAFE(ifp, ctx->ifaces, next, ifn) {
1214 if (if_find(ifaces, ifp->name) != NULL)
1216 dhcpcd_handleinterface(ctx, -1, ifp->name);
1219 /* Add new interfaces */
1220 while ((ifp = TAILQ_FIRST(ifaces)) != NULL ) {
1221 TAILQ_REMOVE(ifaces, ifp, next);
1222 ifp1 = if_find(ctx->ifaces, ifp->name);
1224 /* If the interface already exists,
1225 * check carrier state.
1226 * dhcpcd_checkcarrier will free ifp. */
1227 eloop_timeout_add_sec(ctx->eloop, 0,
1228 dhcpcd_checkcarrier, ifp);
1231 TAILQ_INSERT_TAIL(ctx->ifaces, ifp, next);
1233 dhcpcd_initstate(ifp, 0);
1234 eloop_timeout_add_sec(ctx->eloop, 0,
1235 dhcpcd_runprestartinterface, ifp);
1240 /* Update address state. */
1241 if_markaddrsstale(ctx->ifaces);
1242 if_learnaddrs(ctx, ctx->ifaces, &ifaddrs);
1243 if_deletestaleaddrs(ctx->ifaces);
1247 dhcpcd_handlehwaddr(struct interface *ifp,
1248 uint16_t hwtype, const void *hwaddr, uint8_t hwlen)
1250 char buf[sizeof(ifp->hwaddr) * 3];
1252 if (hwaddr == NULL || !if_valid_hwaddr(hwaddr, hwlen))
1255 if (hwlen > sizeof(ifp->hwaddr)) {
1257 logerr("%s: %s", __func__, ifp->name);
1261 if (ifp->hwtype != hwtype) {
1263 loginfox("%s: hardware address type changed"
1264 " from %d to %d", ifp->name, ifp->hwtype, hwtype);
1265 ifp->hwtype = hwtype;
1268 if (ifp->hwlen == hwlen &&
1269 (hwlen == 0 || memcmp(ifp->hwaddr, hwaddr, hwlen) == 0))
1273 loginfox("%s: old hardware address: %s", ifp->name,
1274 hwaddr_ntoa(ifp->hwaddr, ifp->hwlen, buf, sizeof(buf)));
1275 loginfox("%s: new hardware address: %s", ifp->name,
1276 hwaddr_ntoa(hwaddr, hwlen, buf, sizeof(buf)));
1280 memcpy(ifp->hwaddr, hwaddr, hwlen);
1284 if_reboot(struct interface *ifp, int argc, char **argv)
1287 unsigned long long oldopts;
1289 oldopts = ifp->options->options;
1291 script_runreason(ifp, "RECONFIGURE");
1292 dhcpcd_initstate1(ifp, argc, argv, 0);
1294 dhcp_reboot_newopts(ifp, oldopts);
1299 dhcpcd_prestartinterface(ifp);
1303 reload_config(struct dhcpcd_ctx *ctx)
1305 struct if_options *ifo;
1308 if ((ifo = read_config(ctx, NULL, NULL, NULL)) == NULL)
1310 add_options(ctx, NULL, ifo, ctx->argc, ctx->argv);
1311 /* We need to preserve these options. */
1312 if (ctx->options & DHCPCD_STARTED)
1313 ifo->options |= DHCPCD_STARTED;
1314 if (ctx->options & DHCPCD_MANAGER)
1315 ifo->options |= DHCPCD_MANAGER;
1316 if (ctx->options & DHCPCD_DAEMONISED)
1317 ifo->options |= DHCPCD_DAEMONISED;
1318 if (ctx->options & DHCPCD_PRIVSEP)
1319 ifo->options |= DHCPCD_PRIVSEP;
1320 ctx->options = ifo->options;
1321 free_options(ctx, ifo);
1325 reconf_reboot(struct dhcpcd_ctx *ctx, int action, int argc, char **argv, int oi)
1328 struct interface *ifp;
1330 TAILQ_FOREACH(ifp, ctx->ifaces, next) {
1331 for (i = oi; i < argc; i++) {
1332 if (strcmp(ifp->name, argv[i]) == 0)
1335 if (oi != argc && i == argc)
1337 if (ifp->active == IF_ACTIVE_USER) {
1339 if_reboot(ifp, argc, argv);
1342 ipv4_applyaddr(ifp);
1344 } else if (i != argc) {
1345 ifp->active = IF_ACTIVE_USER;
1346 dhcpcd_initstate1(ifp, argc, argv, 0);
1348 dhcpcd_prestartinterface(ifp);
1354 stop_all_interfaces(struct dhcpcd_ctx *ctx, unsigned long long opts)
1356 struct interface *ifp;
1358 ctx->options |= DHCPCD_EXITING;
1359 if (ctx->ifaces == NULL)
1362 /* Drop the last interface first */
1363 TAILQ_FOREACH_REVERSE(ifp, ctx->ifaces, if_head, next) {
1366 ifp->options->options |= opts;
1367 if (ifp->options->options & DHCPCD_RELEASE)
1368 ifp->options->options &= ~DHCPCD_PERSISTENT;
1369 ifp->options->options |= DHCPCD_EXITING;
1370 stop_interface(ifp, NULL);
1375 dhcpcd_ifrenew(struct interface *ifp)
1381 if (ifp->options->options & DHCPCD_LINK && !if_is_link_up(ifp))
1388 #define DHCPCD_RARENEW (DHCPCD_IPV6 | DHCPCD_IPV6RS)
1389 if ((ifp->options->options & DHCPCD_RARENEW) == DHCPCD_RARENEW)
1390 ipv6nd_startrs(ifp);
1398 dhcpcd_renew(struct dhcpcd_ctx *ctx)
1400 struct interface *ifp;
1402 TAILQ_FOREACH(ifp, ctx->ifaces, next) {
1403 dhcpcd_ifrenew(ifp);
1408 #define sigmsg "received %s, %s"
1410 dhcpcd_signal_cb(int sig, void *arg)
1412 struct dhcpcd_ctx *ctx = arg;
1413 unsigned long long opts;
1416 if (ctx->options & DHCPCD_DUMPLEASE) {
1417 eloop_exit(ctx->eloop, EXIT_FAILURE);
1421 if (sig != SIGCHLD && ctx->options & DHCPCD_FORKED) {
1422 if (sig != SIGHUP &&
1423 write(ctx->fork_fd, &sig, sizeof(sig)) == -1)
1424 logerr("%s: write", __func__);
1429 exit_code = EXIT_FAILURE;
1432 loginfox(sigmsg, "SIGINT", "stopping");
1435 loginfox(sigmsg, "SIGTERM", "stopping");
1436 exit_code = EXIT_SUCCESS;
1439 loginfox(sigmsg, "SIGALRM", "releasing");
1440 opts |= DHCPCD_RELEASE;
1441 exit_code = EXIT_SUCCESS;
1444 loginfox(sigmsg, "SIGHUP", "rebinding");
1446 /* Preserve any options passed on the commandline
1447 * when we were started. */
1448 reconf_reboot(ctx, 1, ctx->argc, ctx->argv,
1449 ctx->argc - ctx->ifc);
1452 loginfox(sigmsg, "SIGUSR1", "renewing");
1456 loginfox(sigmsg, "SIGUSR2", "reopening log");
1458 if (IN_PRIVSEP(ctx)) {
1459 if (ps_root_logreopen(ctx) == -1)
1460 logerr("ps_root_logreopen");
1464 if (logopen(ctx->logfile) == -1)
1468 while (waitpid(-1, NULL, WNOHANG) > 0)
1472 logerrx("received signal %d but don't know what to do with it",
1477 if (!(ctx->options & DHCPCD_TEST))
1478 stop_all_interfaces(ctx, opts);
1479 eloop_exit(ctx->eloop, exit_code);
1484 dhcpcd_handleargs(struct dhcpcd_ctx *ctx, struct fd_list *fd,
1485 int argc, char **argv)
1487 struct interface *ifp;
1488 unsigned long long opts;
1489 int opt, oi, do_reboot, do_renew, af = AF_UNSPEC;
1490 size_t len, l, nifaces;
1493 /* Special commands for our control socket
1494 * as the other end should be blocking until it gets the
1495 * expected reply we should be safely able just to change the
1496 * write callback on the fd */
1497 /* Make any change here in privsep-control.c as well. */
1498 if (strcmp(*argv, "--version") == 0) {
1499 return control_queue(fd, UNCONST(VERSION),
1500 strlen(VERSION) + 1);
1501 } else if (strcmp(*argv, "--getconfigfile") == 0) {
1502 return control_queue(fd, UNCONST(fd->ctx->cffile),
1503 strlen(fd->ctx->cffile) + 1);
1504 } else if (strcmp(*argv, "--getinterfaces") == 0) {
1507 } else if (strcmp(*argv, "--listen") == 0) {
1508 fd->flags |= FD_LISTEN;
1512 /* Log the command */
1514 for (opt = 0; opt < argc; opt++)
1515 len += strlen(argv[opt]) + 1;
1520 for (opt = 0; opt < argc; opt++) {
1521 l = strlen(argv[opt]);
1522 strlcpy(p, argv[opt], len);
1528 loginfox("control command: %s", tmp);
1534 do_reboot = do_renew = 0;
1535 while ((opt = getopt_long(argc, argv, IF_OPTS, cf_options, &oi)) != -1)
1539 /* Assumed if below not set */
1542 opts |= DHCPCD_RELEASE;
1548 opts |= DHCPCD_PERSISTENT;
1551 opts |= DHCPCD_EXITING;
1557 opts |= DHCPCD_DUMPLEASE;
1568 if (opts & DHCPCD_DUMPLEASE) {
1569 ctx->options |= DHCPCD_DUMPLEASE;
1572 TAILQ_FOREACH(ifp, ctx->ifaces, next) {
1575 for (oi = optind; oi < argc; oi++) {
1576 if (strcmp(ifp->name, argv[oi]) == 0)
1579 if (optind == argc || oi < argc) {
1580 opt = send_interface(NULL, ifp, af);
1583 nifaces += (size_t)opt;
1586 if (write(fd->fd, &nifaces, sizeof(nifaces)) != sizeof(nifaces))
1588 TAILQ_FOREACH(ifp, ctx->ifaces, next) {
1591 for (oi = optind; oi < argc; oi++) {
1592 if (strcmp(ifp->name, argv[oi]) == 0)
1595 if (optind == argc || oi < argc) {
1596 if (send_interface(fd, ifp, af) == -1)
1600 ctx->options &= ~DHCPCD_DUMPLEASE;
1603 ctx->options &= ~DHCPCD_DUMPLEASE;
1607 /* Only privileged users can control dhcpcd via the socket. */
1608 if (fd->flags & FD_UNPRIV) {
1613 if (opts & (DHCPCD_EXITING | DHCPCD_RELEASE)) {
1614 if (optind == argc) {
1615 stop_all_interfaces(ctx, opts);
1616 eloop_exit(ctx->eloop, EXIT_SUCCESS);
1619 for (oi = optind; oi < argc; oi++) {
1620 if ((ifp = if_find(ctx->ifaces, argv[oi])) == NULL)
1624 ifp->options->options |= opts;
1625 if (opts & DHCPCD_RELEASE)
1626 ifp->options->options &= ~DHCPCD_PERSISTENT;
1627 stop_interface(ifp, NULL);
1633 if (optind == argc) {
1637 for (oi = optind; oi < argc; oi++) {
1638 if ((ifp = if_find(ctx->ifaces, argv[oi])) == NULL)
1640 dhcpcd_ifrenew(ifp);
1646 /* XXX: Respect initial commandline options? */
1647 reconf_reboot(ctx, do_reboot, argc, argv, optind - 1);
1651 static void dhcpcd_readdump1(void *);
1654 dhcpcd_readdump2(void *arg)
1656 struct dhcpcd_ctx *ctx = arg;
1658 int exit_code = EXIT_FAILURE;
1660 len = read(ctx->control_fd, ctx->ctl_buf + ctx->ctl_bufpos,
1661 ctx->ctl_buflen - ctx->ctl_bufpos);
1665 } else if (len == 0)
1667 if ((size_t)len + ctx->ctl_bufpos != ctx->ctl_buflen) {
1668 ctx->ctl_bufpos += (size_t)len;
1672 if (ctx->ctl_buf[ctx->ctl_buflen - 1] != '\0') /* unlikely */
1673 ctx->ctl_buf[ctx->ctl_buflen - 1] = '\0';
1674 script_dump(ctx->ctl_buf, ctx->ctl_buflen);
1676 if (--ctx->ctl_extra != 0) {
1678 eloop_event_add(ctx->eloop, ctx->control_fd,
1679 dhcpcd_readdump1, ctx);
1682 exit_code = EXIT_SUCCESS;
1685 shutdown(ctx->control_fd, SHUT_RDWR);
1686 eloop_exit(ctx->eloop, exit_code);
1690 dhcpcd_readdump1(void *arg)
1692 struct dhcpcd_ctx *ctx = arg;
1695 len = read(ctx->control_fd, &ctx->ctl_buflen, sizeof(ctx->ctl_buflen));
1696 if (len != sizeof(ctx->ctl_buflen)) {
1701 if (ctx->ctl_buflen > SSIZE_MAX) {
1707 ctx->ctl_buf = malloc(ctx->ctl_buflen);
1708 if (ctx->ctl_buf == NULL)
1711 ctx->ctl_bufpos = 0;
1712 eloop_event_add(ctx->eloop, ctx->control_fd,
1713 dhcpcd_readdump2, ctx);
1718 eloop_exit(ctx->eloop, EXIT_FAILURE);
1722 dhcpcd_readdump0(void *arg)
1724 struct dhcpcd_ctx *ctx = arg;
1727 len = read(ctx->control_fd, &ctx->ctl_extra, sizeof(ctx->ctl_extra));
1728 if (len != sizeof(ctx->ctl_extra)) {
1732 eloop_exit(ctx->eloop, EXIT_FAILURE);
1736 if (ctx->ctl_extra == 0) {
1737 eloop_exit(ctx->eloop, EXIT_SUCCESS);
1741 eloop_event_add(ctx->eloop, ctx->control_fd,
1742 dhcpcd_readdump1, ctx);
1746 dhcpcd_readdumptimeout(void *arg)
1748 struct dhcpcd_ctx *ctx = arg;
1751 eloop_exit(ctx->eloop, EXIT_FAILURE);
1755 dhcpcd_readdump(struct dhcpcd_ctx *ctx)
1758 ctx->options |= DHCPCD_FORKED;
1759 if (eloop_timeout_add_sec(ctx->eloop, 5,
1760 dhcpcd_readdumptimeout, ctx) == -1)
1762 return eloop_event_add(ctx->eloop, ctx->control_fd,
1763 dhcpcd_readdump0, ctx);
1767 dhcpcd_fork_cb(void *arg)
1769 struct dhcpcd_ctx *ctx = arg;
1773 len = read(ctx->fork_fd, &exit_code, sizeof(exit_code));
1776 exit_code = EXIT_FAILURE;
1777 } else if ((size_t)len < sizeof(exit_code)) {
1778 logerrx("%s: truncated read %zd (expected %zu)",
1779 __func__, len, sizeof(exit_code));
1780 exit_code = EXIT_FAILURE;
1782 if (ctx->options & DHCPCD_FORKED)
1783 eloop_exit(ctx->eloop, exit_code);
1785 dhcpcd_signal_cb(exit_code, ctx);
1789 dhcpcd_stderr_cb(void *arg)
1791 struct dhcpcd_ctx *ctx = arg;
1795 len = read(ctx->stderr_fd, log, sizeof(log));
1797 if (errno != ECONNRESET)
1803 fprintf(stderr, "%s", log);
1807 main(int argc, char **argv, char **envp)
1809 struct dhcpcd_ctx ctx;
1810 struct ifaddrs *ifaddrs = NULL;
1811 struct if_options *ifo;
1812 struct interface *ifp;
1813 sa_family_t family = AF_UNSPEC;
1815 unsigned int logopts, t;
1817 #if defined(USE_SIGNALS) || !defined(THERE_IS_NO_FORK)
1819 int fork_fd[2], stderr_fd[2];
1823 const char *siga = NULL;
1827 #ifdef SETPROCTITLE_H
1828 setproctitle_init(argc, argv, envp);
1833 /* Test for --help and --version */
1835 if (strcmp(argv[1], "--help") == 0) {
1837 return EXIT_SUCCESS;
1838 } else if (strcmp(argv[1], "--version") == 0) {
1839 printf(""PACKAGE" "VERSION"\n%s\n", dhcpcd_copyright);
1840 printf("Compiled in features:"
1866 return EXIT_SUCCESS;
1870 memset(&ctx, 0, sizeof(ctx));
1873 ctx.cffile = CONFIG;
1874 ctx.script = UNCONST(dhcpcd_default_script);
1875 ctx.control_fd = ctx.control_unpriv_fd = ctx.link_fd = -1;
1876 ctx.pf_inet_fd = -1;
1878 ctx.pf_link_fd = -1;
1881 TAILQ_INIT(&ctx.control_fds);
1892 #if defined(INET6) && !defined(__sun)
1900 ctx.ps_root_fd = ctx.ps_log_fd = ctx.ps_data_fd = -1;
1901 ctx.ps_inet_fd = ctx.ps_control_fd = -1;
1902 TAILQ_INIT(&ctx.ps_processes);
1905 /* Check our streams for validity */
1906 ctx.stdin_valid = fcntl(STDIN_FILENO, F_GETFD) != -1;
1907 ctx.stdout_valid = fcntl(STDOUT_FILENO, F_GETFD) != -1;
1908 ctx.stderr_valid = fcntl(STDERR_FILENO, F_GETFD) != -1;
1910 logopts = LOGERR_LOG | LOGERR_LOG_DATE | LOGERR_LOG_PID;
1911 if (ctx.stderr_valid)
1912 logopts |= LOGERR_ERR;
1916 while ((opt = getopt_long(argc, argv,
1917 ctx.options & DHCPCD_PRINT_PIDFILE ? NOERR_IF_OPTS : IF_OPTS,
1918 cf_options, &oi)) != -1)
1928 ctx.cffile = optarg;
1932 ctx.logfile = strdup(optarg);
1945 /* Force going via command socket as we're
1946 * out of user definable signals. */
1950 /* -qq disables console output entirely.
1951 * This is important for systemd because it logs
1952 * both console AND syslog to the same log
1953 * resulting in untold confusion. */
1954 if (logopts & LOGERR_QUIET)
1955 logopts &= ~LOGERR_ERR;
1957 logopts |= LOGERR_QUIET;
1969 ctx.options |= DHCPCD_PRINT_PIDFILE;
1970 logopts &= ~(LOGERR_LOG | LOGERR_ERR);
1974 logopts &= ~LOGERR_LOG;
1983 if (ctx.options & DHCPCD_PRINT_PIDFILE)
1990 if (optind != argc - 1)
1991 ctx.options |= DHCPCD_MANAGER;
1993 logsetopts(logopts);
1994 logopen(ctx.logfile);
1998 ctx.ifc = argc - optind;
1999 ctx.ifv = argv + optind;
2003 ifo = read_config(&ctx, NULL, NULL, NULL);
2005 if (ctx.options & DHCPCD_PRINT_PIDFILE)
2010 opt = add_options(&ctx, NULL, ifo, argc, argv);
2012 if (ctx.options & DHCPCD_PRINT_PIDFILE)
2019 printf("Interface options:\n");
2020 if (optind == argc - 1) {
2021 free_options(&ctx, ifo);
2022 ifo = read_config(&ctx, argv[optind], NULL, NULL);
2025 add_options(&ctx, NULL, ifo, argc, argv);
2029 if (family == 0 || family == AF_INET) {
2030 printf("\nDHCPv4 options:\n");
2031 dhcp_printoptions(&ctx,
2032 ifo->dhcp_override, ifo->dhcp_override_len);
2036 if (family == 0 || family == AF_INET6) {
2037 printf("\nND options:\n");
2038 ipv6nd_printoptions(&ctx,
2039 ifo->nd_override, ifo->nd_override_len);
2041 printf("\nDHCPv6 options:\n");
2042 dhcp6_printoptions(&ctx,
2043 ifo->dhcp6_override, ifo->dhcp6_override_len);
2049 ctx.options |= ifo->options;
2051 if (i == 1 || i == 3) {
2053 ctx.options |= DHCPCD_TEST;
2055 ctx.options |= DHCPCD_DUMPLEASE;
2056 ctx.options |= DHCPCD_PERSISTENT;
2057 ctx.options &= ~DHCPCD_DAEMONISE;
2060 #ifdef THERE_IS_NO_FORK
2061 ctx.options &= ~DHCPCD_DAEMONISE;
2064 if (ctx.options & DHCPCD_DEBUG)
2065 logsetopts(logopts | LOGERR_DEBUG);
2067 if (!(ctx.options & (DHCPCD_TEST | DHCPCD_DUMPLEASE))) {
2069 /* If we have any other args, we should run as a single dhcpcd
2070 * instance for that interface. */
2071 if (optind == argc - 1 && !(ctx.options & DHCPCD_MANAGER)) {
2076 if (ifname == NULL || strlen(ifname) > IF_NAMESIZE) {
2077 errno = ifname == NULL ? EINVAL : E2BIG;
2078 logerr("%s: ", ifname);
2081 /* Allow a dhcpcd interface per address family */
2092 snprintf(ctx.pidfile, sizeof(ctx.pidfile),
2093 PIDFILE, ifname, per, ".");
2095 snprintf(ctx.pidfile, sizeof(ctx.pidfile),
2096 PIDFILE, "", "", "");
2097 ctx.options |= DHCPCD_MANAGER;
2099 if (ctx.options & DHCPCD_PRINT_PIDFILE) {
2100 printf("%s\n", ctx.pidfile);
2105 if (chdir("/") == -1)
2106 logerr("%s: chdir: /", __func__);
2108 /* Freeing allocated addresses from dumping leases can trigger
2109 * eloop removals as well, so init here. */
2110 if ((ctx.eloop = eloop_new()) == NULL) {
2111 logerr("%s: eloop_init", __func__);
2116 for (si = 0; si < dhcpcd_signals_ignore_len; si++)
2117 signal(dhcpcd_signals_ignore[si], SIG_IGN);
2119 /* Save signal mask, block and redirect signals to our handler */
2120 eloop_signal_set_cb(ctx.eloop,
2121 dhcpcd_signals, dhcpcd_signals_len,
2122 dhcpcd_signal_cb, &ctx);
2123 if (eloop_signal_mask(ctx.eloop, &ctx.sigset) == -1) {
2124 logerr("%s: eloop_signal_mask", __func__);
2129 pid = pidfile_read(ctx.pidfile);
2130 if (pid != 0 && pid != -1)
2131 loginfox("sending signal %s to pid %d", siga, pid);
2132 if (pid == 0 || pid == -1 || kill(pid, sig) != 0) {
2133 if (sig != SIGHUP && sig != SIGUSR1 && errno != EPERM)
2134 logerrx(PACKAGE" not running");
2135 if (pid != 0 && pid != -1 && errno != ESRCH) {
2139 unlink(ctx.pidfile);
2140 if (sig != SIGHUP && sig != SIGUSR1)
2145 if (sig == SIGHUP || sig == SIGUSR1)
2147 /* Spin until it exits */
2148 loginfox("waiting for pid %d to exit", pid);
2150 ts.tv_nsec = 100000000; /* 10th of a second */
2151 for(i = 0; i < 100; i++) {
2152 nanosleep(&ts, NULL);
2153 if (pidfile_read(ctx.pidfile) == -1)
2156 logerrx("pid %d failed to exit", pid);
2167 if (ctx.options & DHCPCD_DUMPLEASE &&
2168 ioctl(fileno(stdin), FIONREAD, &i, sizeof(i)) == 0 &&
2171 ctx.options |= DHCPCD_FORKED; /* pretend child process */
2173 if (IN_PRIVSEP(&ctx) && ps_managersandbox(&ctx, NULL) == -1)
2176 ifp = calloc(1, sizeof(*ifp));
2186 if (dhcp_dump(ifp) == -1)
2190 logerrx("No DHCP support");
2195 if (dhcp6_dump(ifp) == -1)
2199 logerrx("No DHCP6 support");
2203 logerrx("Family not specified. Please use -4 or -6.");
2210 /* Test against siga instead of sig to avoid gcc
2211 * warning about a bogus potential signed overflow.
2212 * The end result will be the same. */
2213 if ((siga == NULL || i == 4 || ctx.ifc != 0) &&
2214 !(ctx.options & DHCPCD_TEST))
2216 ctx.options |= DHCPCD_FORKED; /* avoid socket unlink */
2217 if (!(ctx.options & DHCPCD_MANAGER))
2218 ctx.control_fd = control_open(argv[optind], family,
2219 ctx.options & DHCPCD_DUMPLEASE);
2220 if (!(ctx.options & DHCPCD_MANAGER) && ctx.control_fd == -1)
2221 ctx.control_fd = control_open(argv[optind], AF_UNSPEC,
2222 ctx.options & DHCPCD_DUMPLEASE);
2223 if (ctx.control_fd == -1)
2224 ctx.control_fd = control_open(NULL, AF_UNSPEC,
2225 ctx.options & DHCPCD_DUMPLEASE);
2226 if (ctx.control_fd != -1) {
2228 if (IN_PRIVSEP(&ctx) &&
2229 ps_managersandbox(&ctx, NULL) == -1)
2232 if (!(ctx.options & DHCPCD_DUMPLEASE))
2233 loginfox("sending commands to dhcpcd process");
2234 len = control_send(&ctx, argc, argv);
2236 logdebugx("send OK");
2238 logerr("%s: control_send", __func__);
2241 if (ctx.options & DHCPCD_DUMPLEASE) {
2242 if (dhcpcd_readdump(&ctx) == -1) {
2243 logerr("%s: dhcpcd_readdump", __func__);
2250 if (errno != ENOENT)
2251 logerr("%s: control_open", __func__);
2252 if (ctx.options & DHCPCD_DUMPLEASE) {
2253 if (errno == ENOENT)
2254 logerrx("dhcpcd is not running");
2257 if (errno == EPERM || errno == EACCES)
2260 ctx.options &= ~DHCPCD_FORKED;
2263 if (!(ctx.options & DHCPCD_TEST)) {
2264 /* Ensure we have the needed directories */
2265 if (mkdir(DBDIR, 0750) == -1 && errno != EEXIST)
2266 logerr("%s: mkdir: %s", __func__, DBDIR);
2267 if (mkdir(RUNDIR, 0755) == -1 && errno != EEXIST)
2268 logerr("%s: mkdir: %s", __func__, RUNDIR);
2269 if ((pid = pidfile_lock(ctx.pidfile)) != 0) {
2271 logerr("%s: pidfile_lock: %s",
2272 __func__, ctx.pidfile);
2275 " already running on pid %d (%s)",
2281 loginfox(PACKAGE "-" VERSION " starting");
2282 if (ctx.stdin_valid && freopen(_PATH_DEVNULL, "w", stdin) == NULL)
2283 logwarn("freopen stdin");
2285 #if defined(USE_SIGNALS) && !defined(THERE_IS_NO_FORK)
2286 if (!(ctx.options & DHCPCD_DAEMONISE))
2289 if (xsocketpair(AF_UNIX, SOCK_DGRAM | SOCK_CXNB, 0, fork_fd) == -1 ||
2290 (ctx.stderr_valid &&
2291 xsocketpair(AF_UNIX, SOCK_DGRAM | SOCK_CXNB, 0, stderr_fd) == -1))
2293 logerr("socketpair");
2296 switch (pid = fork()) {
2301 ctx.fork_fd = fork_fd[1];
2303 #ifdef PRIVSEP_RIGHTS
2304 if (ps_rights_limit_fd(ctx.fork_fd) == -1) {
2305 logerr("ps_rights_limit_fdpair");
2309 eloop_event_add(ctx.eloop, ctx.fork_fd, dhcpcd_fork_cb, &ctx);
2312 * Redirect stderr to the stderr socketpair.
2313 * Redirect stdout as well.
2314 * dhcpcd doesn't output via stdout, but something in
2315 * a called script might.
2317 if (ctx.stderr_valid) {
2318 if (dup2(stderr_fd[1], STDERR_FILENO) == -1 ||
2319 (ctx.stdout_valid &&
2320 dup2(stderr_fd[1], STDOUT_FILENO) == -1))
2322 close(stderr_fd[0]);
2323 close(stderr_fd[1]);
2324 } else if (ctx.stdout_valid) {
2325 if (freopen(_PATH_DEVNULL, "w", stdout) == NULL)
2326 logerr("freopen stdout");
2328 if (setsid() == -1) {
2329 logerr("%s: setsid", __func__);
2332 /* Ensure we can never get a controlling terminal */
2333 switch (pid = fork()) {
2340 ctx.options |= DHCPCD_FORKED; /* A lie */
2346 setproctitle("[launcher]");
2347 ctx.options |= DHCPCD_FORKED | DHCPCD_LAUNCHER;
2348 ctx.fork_fd = fork_fd[0];
2350 #ifdef PRIVSEP_RIGHTS
2351 if (ps_rights_limit_fd(ctx.fork_fd) == -1) {
2352 logerr("ps_rights_limit_fd");
2356 eloop_event_add(ctx.eloop, ctx.fork_fd, dhcpcd_fork_cb, &ctx);
2358 if (ctx.stderr_valid) {
2359 ctx.stderr_fd = stderr_fd[0];
2360 close(stderr_fd[1]);
2361 #ifdef PRIVSEP_RIGHTS
2362 if (ps_rights_limit_fd(ctx.stderr_fd) == 1) {
2363 logerr("ps_rights_limit_fd");
2367 eloop_event_add(ctx.eloop, ctx.stderr_fd,
2368 dhcpcd_stderr_cb, &ctx);
2371 if (IN_PRIVSEP(&ctx) && ps_managersandbox(&ctx, NULL) == -1)
2377 /* We have now forked, setsid, forked once more.
2378 * From this point on, we are the controlling daemon. */
2379 logdebugx("spawned manager process on PID %d", getpid());
2381 ctx.options |= DHCPCD_STARTED;
2382 if ((pid = pidfile_lock(ctx.pidfile)) != 0) {
2383 logerr("%s: pidfile_lock %d", __func__, pid);
2385 /* privsep has not started ... */
2386 ctx.options &= ~DHCPCD_PRIVSEP;
2394 #if defined(BSD) && defined(INET6)
2395 /* Disable the kernel RTADV sysctl as early as possible. */
2396 if (ctx.options & DHCPCD_IPV6 && ctx.options & DHCPCD_IPV6RS)
2401 if (IN_PRIVSEP(&ctx) && ps_start(&ctx) == -1) {
2405 if (ctx.options & DHCPCD_FORKED)
2409 if (!(ctx.options & DHCPCD_TEST)) {
2410 if (control_start(&ctx,
2411 ctx.options & DHCPCD_MANAGER ?
2412 NULL : argv[optind], family) == -1)
2414 logerr("%s: control_start", __func__);
2420 /* Start any dev listening plugin which may want to
2421 * change the interface name provided by the kernel */
2422 if (!IN_PRIVSEP(&ctx) &&
2423 (ctx.options & (DHCPCD_MANAGER | DHCPCD_DEV)) ==
2424 (DHCPCD_MANAGER | DHCPCD_DEV))
2425 dev_start(&ctx, dhcpcd_handleinterface);
2428 setproctitle("%s%s%s",
2429 ctx.options & DHCPCD_MANAGER ? "[manager]" : argv[optind],
2430 ctx.options & DHCPCD_IPV4 ? " [ip4]" : "",
2431 ctx.options & DHCPCD_IPV6 ? " [ip6]" : "");
2433 if (if_opensockets(&ctx) == -1) {
2434 logerr("%s: if_opensockets", __func__);
2438 dhcpcd_setlinkrcvbuf(&ctx);
2441 /* Try and create DUID from the machine UUID. */
2442 dhcpcd_initduid(&ctx, NULL);
2444 /* Cache the default vendor option. */
2445 if (dhcp_vendor(ctx.vendor, sizeof(ctx.vendor)) == -1)
2446 logerr("dhcp_vendor");
2448 /* Start handling kernel messages for interfaces, addresses and
2450 eloop_event_add(ctx.eloop, ctx.link_fd, dhcpcd_handlelink, &ctx);
2453 if (IN_PRIVSEP(&ctx) && ps_managersandbox(&ctx, "stdio route") == -1)
2457 /* When running dhcpcd against a single interface, we need to retain
2458 * the old behaviour of waiting for an IP address */
2459 if (ctx.ifc == 1 && !(ctx.options & DHCPCD_BACKGROUND))
2460 ctx.options |= DHCPCD_WAITIP;
2462 ctx.ifaces = if_discover(&ctx, &ifaddrs, ctx.ifc, ctx.ifv);
2463 if (ctx.ifaces == NULL) {
2464 logerr("%s: if_discover", __func__);
2467 for (i = 0; i < ctx.ifc; i++) {
2468 if ((ifp = if_find(ctx.ifaces, ctx.ifv[i])) == NULL)
2469 logerrx("%s: interface not found",
2471 else if (!ifp->active)
2472 logerrx("%s: interface has an invalid configuration",
2475 TAILQ_FOREACH(ifp, ctx.ifaces, next) {
2476 if (ifp->active == IF_ACTIVE_USER)
2483 loglevel = ctx.options & DHCPCD_INACTIVE ?
2484 LOG_DEBUG : LOG_ERR;
2485 logmessage(loglevel, "no valid interfaces found");
2486 dhcpcd_daemonise(&ctx);
2489 if (!(ctx.options & DHCPCD_LINK)) {
2490 logerrx("aborting as link detection is disabled");
2495 TAILQ_FOREACH(ifp, ctx.ifaces, next) {
2497 dhcpcd_initstate1(ifp, argc, argv, 0);
2499 if_learnaddrs(&ctx, ctx.ifaces, &ifaddrs);
2501 if (ctx.options & DHCPCD_BACKGROUND)
2502 dhcpcd_daemonise(&ctx);
2505 TAILQ_FOREACH(ifp, ctx.ifaces, next) {
2508 if (if_is_link_up(ifp))
2513 if (!(ctx.options & DHCPCD_BACKGROUND)) {
2514 if (ctx.options & DHCPCD_MANAGER)
2518 TAILQ_FOREACH(ifp, ctx.ifaces, next) {
2520 t = ifp->options->timeout;
2526 ctx.options & DHCPCD_LINK &&
2527 !(ctx.options & DHCPCD_WAITIP))
2531 loglevel = ctx.options & DHCPCD_INACTIVE ?
2532 LOG_DEBUG : LOG_WARNING;
2533 logmessage(loglevel, "no interfaces have a carrier");
2534 dhcpcd_daemonise(&ctx);
2536 /* Test mode removes the daemonise bit, so check for both */
2537 ctx.options & (DHCPCD_DAEMONISE | DHCPCD_TEST))
2539 eloop_timeout_add_sec(ctx.eloop, t,
2540 handle_exit_timeout, &ctx);
2543 free_options(&ctx, ifo);
2546 TAILQ_FOREACH(ifp, ctx.ifaces, next) {
2548 eloop_timeout_add_sec(ctx.eloop, 0,
2549 dhcpcd_prestartinterface, ifp);
2553 i = eloop_start(ctx.eloop, &ctx.sigset);
2555 logerr("%s: eloop_start", __func__);
2568 if (!(ctx.options & DHCPCD_TEST) && control_stop(&ctx) == -1)
2569 logerr("%s: control_stop", __func__);
2570 if (ifaddrs != NULL) {
2571 #ifdef PRIVSEP_GETIFADDRS
2572 if (IN_PRIVSEP(&ctx))
2576 freeifaddrs(ifaddrs);
2578 /* ps_stop will clear DHCPCD_PRIVSEP but we need to
2579 * remember it to avoid attemping to remove the pidfile */
2580 oi = ctx.options & DHCPCD_PRIVSEP ? 1 : 0;
2584 /* Free memory and close fd's */
2586 while ((ifp = TAILQ_FIRST(ctx.ifaces))) {
2587 TAILQ_REMOVE(ctx.ifaces, ifp, next);
2593 free_options(&ctx, ifo);
2594 #ifdef HAVE_OPEN_MEMSTREAM
2596 fclose(ctx.script_fp);
2598 free(ctx.script_buf);
2599 free(ctx.script_env);
2602 if (ctx.link_fd != -1) {
2603 eloop_event_delete(ctx.eloop, ctx.link_fd);
2606 if_closesockets(&ctx);
2615 eloop_free(ctx.ps_eloop);
2617 eloop_free(ctx.eloop);
2618 if (ctx.script != dhcpcd_default_script)
2620 if (ctx.options & DHCPCD_STARTED && !(ctx.options & DHCPCD_FORKED))
2621 loginfox(PACKAGE " exited");
2625 #ifdef SETPROCTITLE_H
2626 setproctitle_fini();
2629 if (ctx.options & DHCPCD_STARTED) {
2630 /* Try to detach from the launch process. */
2631 if (ctx.fork_fd != -1 &&
2632 write(ctx.fork_fd, &i, sizeof(i)) == -1)
2633 logerr("%s: write", __func__);
2635 if (ctx.options & DHCPCD_FORKED || oi != 0)
2636 _exit(i); /* so atexit won't remove our pidfile */