1 /* SPDX-License-Identifier: BSD-2-Clause */
3 * dhcpcd - DHCP client daemon
4 * Copyright (c) 2006-2019 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 const char dhcpcd_copyright[] = "Copyright (c) 2006-2019 Roy Marples";
32 #include <sys/socket.h>
35 #include <sys/types.h>
56 #include "dhcp-common.h"
63 #include "if-options.h"
76 const int dhcpcd_signals[] = {
85 const size_t dhcpcd_signals_len = __arraycount(dhcpcd_signals);
88 #define IF_UPANDRUNNING(a) \
89 (((a)->flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
95 printf("usage: "PACKAGE"\t[-146ABbDdEGgHJKLMNPpqTV]\n"
96 "\t\t[-C, --nohook hook] [-c, --script script]\n"
97 "\t\t[-e, --env value] [-F, --fqdn FQDN] [-f, --config file]\n"
98 "\t\t[-h, --hostname hostname] [-I, --clientid clientid]\n"
99 "\t\t[-i, --vendorclassid vendorclassid] [-j, --logfile logfile]\n"
100 "\t\t[-l, --leasetime seconds] [-m, --metric metric]\n"
101 "\t\t[-O, --nooption option] [-o, --option option]\n"
102 "\t\t[-Q, --require option] [-r, --request address]\n"
103 "\t\t[-S, --static value]\n"
104 "\t\t[-s, --inform address[/cidr[/broadcast_address]]]\n [--inform6]"
105 "\t\t[-t, --timeout seconds] [-u, --userclass class]\n"
106 "\t\t[-v, --vendor code, value] [-W, --whitelist address[/cidr]] [-w]\n"
107 "\t\t[--waitip [4 | 6]] [-y, --reboot seconds]\n"
108 "\t\t[-X, --blacklist address[/cidr]] [-Z, --denyinterfaces pattern]\n"
109 "\t\t[-z, --allowinterfaces pattern] [--inactive] [interface] [...]\n"
110 " "PACKAGE"\t-n, --rebind [interface]\n"
111 " "PACKAGE"\t-k, --release [interface]\n"
112 " "PACKAGE"\t-U, --dumplease interface\n"
113 " "PACKAGE"\t--version\n"
114 " "PACKAGE"\t-x, --exit [interface]\n");
118 free_globals(struct dhcpcd_ctx *ctx)
120 struct dhcp_opt *opt;
123 for (; ctx->ifac > 0; ctx->ifac--)
124 free(ctx->ifav[ctx->ifac - 1]);
129 for (; ctx->ifdc > 0; ctx->ifdc--)
130 free(ctx->ifdv[ctx->ifdc - 1]);
135 for (; ctx->ifcc > 0; ctx->ifcc--)
136 free(ctx->ifcv[ctx->ifcc - 1]);
142 if (ctx->dhcp_opts) {
143 for (opt = ctx->dhcp_opts;
144 ctx->dhcp_opts_len > 0;
145 opt++, ctx->dhcp_opts_len--)
146 free_dhcp_opt_embenc(opt);
147 free(ctx->dhcp_opts);
148 ctx->dhcp_opts = NULL;
153 for (opt = ctx->nd_opts;
154 ctx->nd_opts_len > 0;
155 opt++, ctx->nd_opts_len--)
156 free_dhcp_opt_embenc(opt);
161 if (ctx->dhcp6_opts) {
162 for (opt = ctx->dhcp6_opts;
163 ctx->dhcp6_opts_len > 0;
164 opt++, ctx->dhcp6_opts_len--)
165 free_dhcp_opt_embenc(opt);
166 free(ctx->dhcp6_opts);
167 ctx->dhcp6_opts = NULL;
172 for (opt = ctx->vivso;
174 opt++, ctx->vivso_len--)
175 free_dhcp_opt_embenc(opt);
182 handle_exit_timeout(void *arg)
184 struct dhcpcd_ctx *ctx;
187 logerrx("timed out");
188 if (!(ctx->options & DHCPCD_MASTER)) {
189 eloop_exit(ctx->eloop, EXIT_FAILURE);
192 ctx->options |= DHCPCD_NOWAITIP;
193 dhcpcd_daemonise(ctx);
213 dhcpcd_ifafwaiting(const struct interface *ifp)
215 unsigned long long opts;
216 bool foundany = false;
218 if (ifp->active != IF_ACTIVE_USER)
221 #define DHCPCD_WAITALL (DHCPCD_WAITIP4 | DHCPCD_WAITIP6)
222 opts = ifp->options->options;
224 if (opts & DHCPCD_WAITIP4 ||
225 (opts & DHCPCD_WAITIP && !(opts & DHCPCD_WAITALL)))
227 bool foundaddr = ipv4_hasaddr(ifp);
229 if (opts & DHCPCD_WAITIP4 && !foundaddr)
236 if (opts & DHCPCD_WAITIP6 ||
237 (opts & DHCPCD_WAITIP && !(opts & DHCPCD_WAITALL)))
239 bool foundaddr = ipv6_hasaddr(ifp);
241 if (opts & DHCPCD_WAITIP6 && !foundaddr)
248 if (opts & DHCPCD_WAITIP && !(opts & DHCPCD_WAITALL) && !foundany)
254 dhcpcd_afwaiting(const struct dhcpcd_ctx *ctx)
256 unsigned long long opts;
257 const struct interface *ifp;
260 if (!(ctx->options & DHCPCD_WAITOPTS))
264 TAILQ_FOREACH(ifp, ctx->ifaces, next) {
266 if (opts & (DHCPCD_WAITIP | DHCPCD_WAITIP4) &&
268 opts &= ~(DHCPCD_WAITIP | DHCPCD_WAITIP4);
271 if (opts & (DHCPCD_WAITIP | DHCPCD_WAITIP6) &&
273 opts &= ~(DHCPCD_WAITIP | DHCPCD_WAITIP6);
275 if (!(opts & DHCPCD_WAITOPTS))
278 if (opts & DHCPCD_WAITIP)
280 else if (opts & DHCPCD_WAITIP4)
282 else if (opts & DHCPCD_WAITIP6)
290 dhcpcd_ipwaited(struct dhcpcd_ctx *ctx)
292 struct interface *ifp;
295 TAILQ_FOREACH(ifp, ctx->ifaces, next) {
296 if ((af = dhcpcd_ifafwaiting(ifp)) != AF_MAX) {
297 logdebugx("%s: waiting for an %s address",
298 ifp->name, dhcpcd_af(af));
303 if ((af = dhcpcd_afwaiting(ctx)) != AF_MAX) {
304 logdebugx("waiting for an %s address",
312 /* Returns the pid of the child, otherwise 0. */
314 dhcpcd_daemonise(struct dhcpcd_ctx *ctx)
316 #ifdef THERE_IS_NO_FORK
317 eloop_timeout_delete(ctx->eloop, handle_exit_timeout, ctx);
325 if (ctx->options & DHCPCD_DAEMONISE &&
326 !(ctx->options & (DHCPCD_DAEMONISED | DHCPCD_NOWAITIP)))
328 if (!dhcpcd_ipwaited(ctx))
332 if (ctx->options & DHCPCD_ONESHOT) {
333 loginfox("exiting due to oneshot");
334 eloop_exit(ctx->eloop, EXIT_SUCCESS);
338 eloop_timeout_delete(ctx->eloop, handle_exit_timeout, ctx);
339 if (ctx->options & DHCPCD_DAEMONISED ||
340 !(ctx->options & DHCPCD_DAEMONISE))
342 logdebugx("forking to background");
344 /* Setup a signal pipe so parent knows when to exit. */
345 if (pipe(sidpipe) == -1) {
346 logerr("%s: pipe", __func__);
350 switch (pid = fork()) {
352 logerr("%s: fork", __func__);
355 if ((lpid = pidfile_lock(ctx->pidfile)) != 0)
356 logerr("%s: pidfile_lock %d", __func__, lpid);
358 /* Notify parent it's safe to exit as we've detached. */
360 if (write(sidpipe[1], &buf, 1) == -1)
361 logerr("%s: write", __func__);
363 /* Some polling methods don't survive after forking,
364 * so ensure we can requeue all our events. */
365 if (eloop_requeue(ctx->eloop) == -1) {
366 logerr("%s: eloop_requeue", __func__);
367 eloop_exit(ctx->eloop, EXIT_FAILURE);
369 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
370 dup2(fd, STDIN_FILENO);
371 dup2(fd, STDOUT_FILENO);
372 dup2(fd, STDERR_FILENO);
375 ctx->options |= DHCPCD_DAEMONISED;
378 /* Wait for child to detach */
380 if (read(sidpipe[0], &buf, 1) == -1)
381 logerr("%s: read", __func__);
383 loginfox("forked to background, child pid %d", pid);
384 ctx->options |= DHCPCD_FORKED;
385 eloop_exit(ctx->eloop, EXIT_SUCCESS);
392 dhcpcd_drop(struct interface *ifp, int stop)
396 dhcp6_drop(ifp, stop ? NULL : "EXPIRE6");
406 dhcp_drop(ifp, stop ? "STOP" : "EXPIRE");
411 #if !defined(DHCP6) && !defined(DHCP)
417 stop_interface(struct interface *ifp)
419 struct dhcpcd_ctx *ctx;
422 loginfox("%s: removing interface", ifp->name);
423 ifp->options->options |= DHCPCD_STOPPING;
426 if (ifp->options->options & DHCPCD_DEPARTED)
427 script_runreason(ifp, "DEPARTED");
429 script_runreason(ifp, "STOPPED");
431 /* Delete all timeouts for the interfaces */
432 eloop_q_timeout_delete(ctx->eloop, 0, NULL, ifp);
434 /* De-activate the interface */
435 ifp->active = IF_INACTIVE;
436 ifp->options->options &= ~DHCPCD_STOPPING;
437 /* Set the link state to unknown as we're no longer tracking it. */
438 ifp->carrier = LINK_UNKNOWN;
440 if (!(ctx->options & (DHCPCD_MASTER | DHCPCD_TEST)))
441 eloop_exit(ctx->eloop, EXIT_FAILURE);
445 configure_interface1(struct interface *ifp)
447 struct if_options *ifo = ifp->options;
449 /* Do any platform specific configuration */
452 /* If we want to release a lease, we can't really persist the
454 if (ifo->options & DHCPCD_RELEASE)
455 ifo->options &= ~DHCPCD_PERSISTENT;
457 if (ifp->flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) {
458 ifo->options &= ~DHCPCD_ARP;
459 if (!(ifp->flags & IFF_MULTICAST))
460 ifo->options &= ~DHCPCD_IPV6RS;
461 if (!(ifo->options & (DHCPCD_INFORM | DHCPCD_WANTDHCP)))
462 ifo->options |= DHCPCD_STATIC;
464 if (!(ifo->options & DHCPCD_ARP) ||
465 ifo->options & (DHCPCD_INFORM | DHCPCD_STATIC))
466 ifo->options &= ~DHCPCD_IPV4LL;
468 if (ifo->metric != -1)
469 ifp->metric = (unsigned int)ifo->metric;
471 if (!(ifo->options & DHCPCD_IPV4))
472 ifo->options &= ~(DHCPCD_DHCP | DHCPCD_IPV4LL | DHCPCD_WAITIP4);
475 if (!(ifo->options & DHCPCD_IPV6))
477 ~(DHCPCD_IPV6RS | DHCPCD_DHCP6 | DHCPCD_WAITIP6);
479 if (!(ifo->options & DHCPCD_IPV6RS))
481 ~(DHCPCD_IPV6RA_AUTOCONF | DHCPCD_IPV6RA_REQRDNSS);
483 /* We want to setup INET6 on the interface as soon as possible. */
484 if (ifp->active == IF_ACTIVE_USER &&
485 ifo->options & DHCPCD_IPV6 &&
486 !(ifp->ctx->options & (DHCPCD_DUMPLEASE | DHCPCD_TEST)))
488 /* If not doing any DHCP, disable the RDNSS requirement. */
489 if (!(ifo->options & (DHCPCD_DHCP | DHCPCD_DHCP6)))
490 ifo->options &= ~DHCPCD_IPV6RA_REQRDNSS;
495 if (!(ifo->options & DHCPCD_IAID)) {
497 * An IAID is for identifying a unqiue interface within
498 * the client. It is 4 bytes long. Working out a default
499 * value is problematic.
501 * Interface name and number are not stable
502 * between different OS's. Some OS's also cannot make
503 * up their mind what the interface should be called
504 * (yes, udev, I'm looking at you).
505 * Also, the name could be longer than 4 bytes.
506 * Also, with pluggable interfaces the name and index
507 * could easily get swapped per actual interface.
509 * The MAC address is 6 bytes long, the final 3
510 * being unique to the manufacturer and the initial 3
511 * being unique to the organisation which makes it.
512 * We could use the last 4 bytes of the MAC address
513 * as the IAID as it's the most stable part given the
514 * above, but equally it's not guaranteed to be
517 * Given the above, and our need to reliably work
518 * between reboots without persitent storage,
519 * generating the IAID from the MAC address is the only
521 * Saying that, if a VLANID has been specified then we
522 * can use that. It's possible that different interfaces
523 * can have the same VLANID, but this is no worse than
524 * generating the IAID from the duplicate MAC address.
526 * dhclient uses the last 4 bytes of the MAC address.
527 * dibbler uses an increamenting counter.
528 * wide-dhcpv6 uses 0 or a configured value.
530 * Windows 7 uses the first 3 bytes of the MAC address
531 * and an unknown byte.
532 * dhcpcd-6.1.0 and earlier used the interface name,
533 * falling back to interface index if name > 4.
535 if (ifp->vlanid != 0) {
538 /* Maximal VLANID is 4095, so prefix with 0xff
539 * so we don't conflict with an interface index. */
540 vlanid = htonl(ifp->vlanid | 0xff000000);
541 memcpy(ifo->iaid, &vlanid, sizeof(vlanid));
542 } else if (ifp->hwlen >= sizeof(ifo->iaid)) {
544 ifp->hwaddr + ifp->hwlen - sizeof(ifo->iaid),
549 len = (uint32_t)strlen(ifp->name);
550 if (len <= sizeof(ifo->iaid)) {
551 memcpy(ifo->iaid, ifp->name, len);
552 if (len < sizeof(ifo->iaid))
553 memset(ifo->iaid + len, 0,
554 sizeof(ifo->iaid) - len);
556 /* IAID is the same size as a uint32_t */
557 len = htonl(ifp->index);
558 memcpy(ifo->iaid, &len, sizeof(ifo->iaid));
561 ifo->options |= DHCPCD_IAID;
565 if (ifo->ia_len == 0 && ifo->options & DHCPCD_IPV6 &&
566 ifp->name[0] != '\0')
568 ifo->ia = malloc(sizeof(*ifo->ia));
573 ifo->ia->ia_type = D6_OPTION_IA_NA;
574 memcpy(ifo->ia->iaid, ifo->iaid, sizeof(ifo->iaid));
575 memset(&ifo->ia->addr, 0, sizeof(ifo->ia->addr));
578 ifo->ia->sla_len = 0;
584 for (i = 0; i < ifo->ia_len; i++) {
585 if (!ifo->ia[i].iaid_set) {
586 memcpy(&ifo->ia[i].iaid, ifo->iaid,
587 sizeof(ifo->ia[i].iaid));
588 ifo->ia[i].iaid_set = 1;
596 dhcpcd_selectprofile(struct interface *ifp, const char *profile)
598 struct if_options *ifo;
599 char pssid[PROFILE_LEN];
604 r = print_string(pssid, sizeof(pssid), OT_ESCSTRING,
605 ifp->ssid, ifp->ssid_len);
612 ifo = read_config(ifp->ctx, ifp->name, pssid, profile);
614 logdebugx("%s: no profile %s", ifp->name, profile);
617 if (profile != NULL) {
618 strlcpy(ifp->profile, profile, sizeof(ifp->profile));
619 loginfox("%s: selected profile %s", ifp->name, profile);
621 *ifp->profile = '\0';
623 free_options(ifp->ctx, ifp->options);
626 add_options(ifp->ctx, ifp->name, ifp->options,
627 ifp->ctx->argc, ifp->ctx->argv);
628 configure_interface1(ifp);
634 configure_interface(struct interface *ifp, int argc, char **argv,
635 unsigned long long options)
639 old = ifp->options ? ifp->options->mtime : 0;
640 dhcpcd_selectprofile(ifp, NULL);
641 if (ifp->options == NULL) {
642 /* dhcpcd cannot continue with this interface. */
643 ifp->active = IF_INACTIVE;
646 add_options(ifp->ctx, ifp->name, ifp->options, argc, argv);
647 ifp->options->options |= options;
648 configure_interface1(ifp);
650 /* If the mtime has changed drop any old lease */
651 if (old != 0 && ifp->options->mtime != old) {
652 logwarnx("%s: confile file changed, expiring leases",
659 dhcpcd_initstate2(struct interface *ifp, unsigned long long options)
661 struct if_options *ifo;
664 if ((ifo = default_config(ifp->ctx)) == NULL) {
668 ifo->options |= options;
675 if (ifo->options & DHCPCD_IPV6 && ipv6_init(ifp->ctx) == -1) {
677 ifo->options &= ~DHCPCD_IPV6;
683 dhcpcd_initstate1(struct interface *ifp, int argc, char **argv,
684 unsigned long long options)
687 configure_interface(ifp, argc, argv, options);
689 dhcpcd_initstate2(ifp, 0);
693 dhcpcd_initstate(struct interface *ifp, unsigned long long options)
696 dhcpcd_initstate1(ifp, ifp->ctx->argc, ifp->ctx->argv, options);
700 dhcpcd_handlecarrier(struct dhcpcd_ctx *ctx, int carrier, unsigned int flags,
703 struct interface *ifp;
705 ifp = if_find(ctx->ifaces, ifname);
707 ifp->options == NULL || !(ifp->options->options & DHCPCD_LINK) ||
711 if (carrier == LINK_UNKNOWN) {
716 carrier = if_carrier(ifp);
719 if (carrier == LINK_UNKNOWN)
720 carrier = IF_UPANDRUNNING(ifp) ? LINK_UP : LINK_DOWN;
722 if (carrier == LINK_DOWN || (ifp->flags & IFF_UP) == 0) {
723 if (ifp->carrier != LINK_DOWN) {
724 if (ifp->carrier == LINK_UP)
725 loginfox("%s: carrier lost", ifp->name);
726 #ifdef NOCARRIER_PRESERVE_IP
727 if (ifp->flags & IFF_UP)
728 ifp->carrier = LINK_DOWN_IFFUP;
731 ifp->carrier = LINK_DOWN;
732 script_runreason(ifp, "NOCARRIER");
733 #ifdef NOCARRIER_PRESERVE_IP
734 if (ifp->flags & IFF_UP) {
748 } else if (carrier == LINK_UP && ifp->flags & IFF_UP) {
749 if (ifp->carrier != LINK_UP) {
750 loginfox("%s: carrier acquired", ifp->name);
751 ifp->carrier = LINK_UP;
752 #if !defined(__linux__) && !defined(__NetBSD__)
753 /* BSD does not emit RTM_NEWADDR or RTM_CHGADDR when the
754 * hardware address changes so we have to go
755 * through the disovery process to work it out. */
756 dhcpcd_handleinterface(ctx, 0, ifp->name);
759 uint8_t ossid[IF_SSIDLEN];
762 olen = ifp->ssid_len;
763 memcpy(ossid, ifp->ssid, ifp->ssid_len);
766 /* If we changed SSID network, drop leases */
767 if (ifp->ssid_len != olen ||
768 memcmp(ifp->ssid, ossid, ifp->ssid_len))
770 #ifdef NOCARRIER_PRESERVE_IP
778 dhcpcd_initstate(ifp, 0);
779 script_runreason(ifp, "CARRIER");
781 #ifdef NOCARRIER_PRESERVE_IP
782 /* Set any IPv6 Routers we remembered to expire
783 * faster than they would normally as we
784 * maybe on a new network. */
785 ipv6nd_startexpire(ifp);
787 /* RFC4941 Section 3.5 */
788 ipv6_gentempifid(ifp);
790 dhcpcd_startinterface(ifp);
796 warn_iaid_conflict(struct interface *ifp, uint16_t ia_type, uint8_t *iaid)
798 struct interface *ifn;
804 TAILQ_FOREACH(ifn, ifp->ctx->ifaces, next) {
805 if (ifn == ifp || !ifn->active)
808 memcmp(ifn->options->iaid, iaid,
809 sizeof(ifn->options->iaid)) == 0)
812 for (i = 0; i < ifn->options->ia_len; i++) {
813 ia = &ifn->options->ia[i];
814 if (ia->ia_type == ia_type &&
815 memcmp(ia->iaid, iaid, sizeof(ia->iaid)) == 0)
821 /* This is only a problem if the interfaces are on the same network. */
823 logerrx("%s: IAID conflicts with one assigned to %s",
824 ifp->name, ifn->name);
828 dhcpcd_startinterface(void *arg)
830 struct interface *ifp = arg;
831 struct if_options *ifo = ifp->options;
832 char buf[DUID_LEN * 3];
835 if (ifo->options & DHCPCD_LINK) {
836 switch (ifp->carrier) {
840 loginfox("%s: waiting for carrier", ifp->name);
843 /* No media state available.
844 * Loop until both IFF_UP and IFF_RUNNING are set */
845 carrier = if_carrier(ifp);
846 if (carrier == LINK_UNKNOWN) {
847 if (IF_UPANDRUNNING(ifp))
853 tv.tv_nsec = IF_POLL_UP * NSEC_PER_MSEC;
854 eloop_timeout_add_tv(ifp->ctx->eloop,
855 &tv, dhcpcd_startinterface, ifp);
859 dhcpcd_handlecarrier(ifp->ctx, carrier,
860 ifp->flags, ifp->name);
865 if (ifo->options & (DHCPCD_DUID | DHCPCD_IPV6)) {
866 /* Report client DUID */
867 if (ifp->ctx->duid == NULL) {
868 if (duid_init(ifp) == 0)
871 hwaddr_ntoa(ifp->ctx->duid,
877 if (ifo->options & (DHCPCD_DUID | DHCPCD_IPV6)) {
884 loginfox("%s: IAID %s", ifp->name,
885 hwaddr_ntoa(ifo->iaid, sizeof(ifo->iaid),
887 warn_iaid_conflict(ifp, 0, ifo->iaid);
889 for (i = 0; i < ifo->ia_len; i++) {
891 if (memcmp(ifo->iaid, ia->iaid, sizeof(ifo->iaid))) {
892 loginfox("%s: IA type %u IAID %s",
893 ifp->name, ia->ia_type,
894 hwaddr_ntoa(ia->iaid, sizeof(ia->iaid),
896 warn_iaid_conflict(ifp, ia->ia_type, ia->iaid);
903 if (ifo->options & DHCPCD_IPV6 && ipv6_start(ifp) == -1) {
904 logerr("%s: ipv6_start", ifp->name);
905 ifo->options &= ~DHCPCD_IPV6;
908 if (ifo->options & DHCPCD_IPV6) {
909 if (ifp->active == IF_ACTIVE_USER) {
910 ipv6_startstatic(ifp);
912 if (ifo->options & DHCPCD_IPV6RS)
917 if (ifo->options & DHCPCD_DHCP6) {
918 dhcp6_find_delegates(ifp);
920 if (ifp->active == IF_ACTIVE_USER) {
923 if (ifo->options & DHCPCD_IA_FORCED)
924 d6_state = DH6S_INIT;
925 else if (ifo->options & DHCPCD_INFORM6)
926 d6_state = DH6S_INFORM;
928 d6_state = DH6S_CONFIRM;
929 if (dhcp6_start(ifp, d6_state) == -1)
930 logerr("%s: dhcp6_start", ifp->name);
938 if (ifo->options & DHCPCD_IPV4 && ifp->active == IF_ACTIVE_USER) {
939 /* Ensure we have an IPv4 state before starting DHCP */
940 if (ipv4_getstate(ifp) != NULL)
947 dhcpcd_prestartinterface(void *arg)
949 struct interface *ifp = arg;
951 if ((!(ifp->ctx->options & DHCPCD_MASTER) ||
952 ifp->options->options & DHCPCD_IF_UP) &&
954 logerr("%s: %s", __func__, ifp->name);
956 dhcpcd_startinterface(ifp);
960 run_preinit(struct interface *ifp)
963 if (ifp->ctx->options & DHCPCD_TEST)
966 script_runreason(ifp, "PREINIT");
968 if (ifp->options->options & DHCPCD_LINK && ifp->carrier != LINK_UNKNOWN)
969 script_runreason(ifp,
970 ifp->carrier == LINK_UP ? "CARRIER" : "NOCARRIER");
974 dhcpcd_activateinterface(struct interface *ifp, unsigned long long options)
978 ifp->active = IF_ACTIVE;
979 dhcpcd_initstate2(ifp, options);
980 /* It's possible we might not have been able to load
983 configure_interface1(ifp);
985 dhcpcd_prestartinterface(ifp);
991 dhcpcd_handleinterface(void *arg, int action, const char *ifname)
993 struct dhcpcd_ctx *ctx;
994 struct ifaddrs *ifaddrs;
996 struct interface *ifp, *iff;
997 const char * const argv[] = { ifname };
1002 ifp = if_find(ctx->ifaces, ifname);
1008 logdebugx("%s: interface departed", ifp->name);
1009 ifp->options->options |= DHCPCD_DEPARTED;
1010 stop_interface(ifp);
1012 TAILQ_REMOVE(ctx->ifaces, ifp, next);
1017 ifs = if_discover(ctx, &ifaddrs, -1, UNCONST(argv));
1023 ifp = if_find(ifs, ifname);
1025 /* This can happen if an interface is quickly added
1026 * and then removed. */
1033 /* Check if we already have the interface */
1034 iff = if_find(ctx->ifaces, ifp->name);
1038 logdebugx("%s: interface updated", iff->name);
1039 /* The flags and hwaddr could have changed */
1040 iff->flags = ifp->flags;
1041 iff->hwlen = ifp->hwlen;
1042 if (ifp->hwlen != 0)
1043 memcpy(iff->hwaddr, ifp->hwaddr, iff->hwlen);
1045 TAILQ_REMOVE(ifs, ifp, next);
1046 TAILQ_INSERT_TAIL(ctx->ifaces, ifp, next);
1048 logdebugx("%s: interface added", ifp->name);
1049 dhcpcd_initstate(ifp, 0);
1056 if_learnaddrs(ctx, ifs, &ifaddrs);
1058 dhcpcd_prestartinterface(iff);
1062 /* Free our discovered list */
1063 while ((ifp = TAILQ_FIRST(ifs))) {
1064 TAILQ_REMOVE(ifs, ifp, next);
1073 dhcpcd_handlelink(void *arg)
1075 struct dhcpcd_ctx *ctx = arg;
1077 if (if_handlelink(ctx) == -1) {
1078 if (errno == ENOBUFS || errno == ENOMEM) {
1079 dhcpcd_linkoverflow(ctx);
1082 if (errno != ENOTSUP)
1088 dhcpcd_checkcarrier(void *arg)
1090 struct interface *ifp = arg;
1092 dhcpcd_handlecarrier(ifp->ctx, LINK_UNKNOWN, ifp->flags, ifp->name);
1097 dhcpcd_setlinkrcvbuf(struct dhcpcd_ctx *ctx)
1101 if (ctx->link_rcvbuf == 0)
1104 socklen = sizeof(ctx->link_rcvbuf);
1105 if (setsockopt(ctx->link_fd, SOL_SOCKET,
1106 SO_RCVBUF, &ctx->link_rcvbuf, socklen) == -1)
1112 dhcpcd_linkoverflow(struct dhcpcd_ctx *ctx)
1114 struct if_head *ifaces;
1115 struct ifaddrs *ifaddrs;
1116 struct interface *ifp, *ifn, *ifp1;
1118 logerrx("route socket overflowed - learning interface state");
1120 /* Close the existing socket and open a new one.
1121 * This is easier than draining the kernel buffer of an
1122 * in-determinate size. */
1123 eloop_event_delete(ctx->eloop, ctx->link_fd);
1124 close(ctx->link_fd);
1125 if_closesockets_os(ctx);
1126 if (if_opensockets_os(ctx) == -1) {
1127 logerr("%s: if_opensockets", __func__);
1128 eloop_exit(ctx->eloop, EXIT_FAILURE);
1132 dhcpcd_setlinkrcvbuf(ctx);
1134 eloop_event_add(ctx->eloop, ctx->link_fd, dhcpcd_handlelink, ctx);
1136 /* Work out the current interfaces. */
1137 ifaces = if_discover(ctx, &ifaddrs, ctx->ifc, ctx->ifv);
1138 if (ifaces == NULL) {
1143 /* Punt departed interfaces */
1144 TAILQ_FOREACH_SAFE(ifp, ctx->ifaces, next, ifn) {
1145 if (if_find(ifaces, ifp->name) != NULL)
1147 dhcpcd_handleinterface(ctx, -1, ifp->name);
1150 /* Add new interfaces */
1151 while ((ifp = TAILQ_FIRST(ifaces)) != NULL ) {
1152 TAILQ_REMOVE(ifaces, ifp, next);
1153 ifp1 = if_find(ctx->ifaces, ifp->name);
1155 /* If the interface already exists,
1156 * check carrier state. */
1157 eloop_timeout_add_sec(ctx->eloop, 0,
1158 dhcpcd_checkcarrier, ifp1);
1162 TAILQ_INSERT_TAIL(ctx->ifaces, ifp, next);
1164 eloop_timeout_add_sec(ctx->eloop, 0,
1165 dhcpcd_prestartinterface, ifp);
1169 /* Update address state. */
1170 if_markaddrsstale(ctx->ifaces);
1171 if_learnaddrs(ctx, ctx->ifaces, &ifaddrs);
1172 if_deletestaleaddrs(ctx->ifaces);
1176 dhcpcd_handlehwaddr(struct dhcpcd_ctx *ctx, const char *ifname,
1177 const void *hwaddr, uint8_t hwlen)
1179 struct interface *ifp;
1180 char buf[sizeof(ifp->hwaddr) * 3];
1182 ifp = if_find(ctx->ifaces, ifname);
1186 if (!if_valid_hwaddr(hwaddr, hwlen))
1189 if (hwlen > sizeof(ifp->hwaddr)) {
1191 logerr("%s: %s", __func__, ifp->name);
1195 if (ifp->hwlen == hwlen && memcmp(ifp->hwaddr, hwaddr, hwlen) == 0)
1198 loginfox("%s: new hardware address: %s", ifp->name,
1199 hwaddr_ntoa(hwaddr, hwlen, buf, sizeof(buf)));
1201 memcpy(ifp->hwaddr, hwaddr, hwlen);
1205 if_reboot(struct interface *ifp, int argc, char **argv)
1208 unsigned long long oldopts;
1210 oldopts = ifp->options->options;
1212 script_runreason(ifp, "RECONFIGURE");
1213 dhcpcd_initstate1(ifp, argc, argv, 0);
1215 dhcp_reboot_newopts(ifp, oldopts);
1220 dhcpcd_prestartinterface(ifp);
1224 reload_config(struct dhcpcd_ctx *ctx)
1226 struct if_options *ifo;
1229 if ((ifo = read_config(ctx, NULL, NULL, NULL)) == NULL)
1231 add_options(ctx, NULL, ifo, ctx->argc, ctx->argv);
1232 /* We need to preserve these two options. */
1233 if (ctx->options & DHCPCD_MASTER)
1234 ifo->options |= DHCPCD_MASTER;
1235 if (ctx->options & DHCPCD_DAEMONISED)
1236 ifo->options |= DHCPCD_DAEMONISED;
1237 ctx->options = ifo->options;
1238 free_options(ctx, ifo);
1242 reconf_reboot(struct dhcpcd_ctx *ctx, int action, int argc, char **argv, int oi)
1245 struct interface *ifp;
1247 TAILQ_FOREACH(ifp, ctx->ifaces, next) {
1248 for (i = oi; i < argc; i++) {
1249 if (strcmp(ifp->name, argv[i]) == 0)
1252 if (oi != argc && i == argc)
1254 if (ifp->active == IF_ACTIVE_USER) {
1256 if_reboot(ifp, argc, argv);
1259 ipv4_applyaddr(ifp);
1261 } else if (i != argc) {
1262 ifp->active = IF_ACTIVE_USER;
1263 dhcpcd_initstate1(ifp, argc, argv, 0);
1265 dhcpcd_prestartinterface(ifp);
1271 stop_all_interfaces(struct dhcpcd_ctx *ctx, unsigned long long opts)
1273 struct interface *ifp;
1275 ctx->options |= DHCPCD_EXITING;
1276 /* Drop the last interface first */
1277 TAILQ_FOREACH_REVERSE(ifp, ctx->ifaces, if_head, next) {
1279 ifp->options->options |= opts;
1280 if (ifp->options->options & DHCPCD_RELEASE)
1281 ifp->options->options &= ~DHCPCD_PERSISTENT;
1282 ifp->options->options |= DHCPCD_EXITING;
1283 stop_interface(ifp);
1289 dhcpcd_ifrenew(struct interface *ifp)
1295 if (ifp->options->options & DHCPCD_LINK &&
1296 ifp->carrier == LINK_DOWN)
1303 #define DHCPCD_RARENEW (DHCPCD_IPV6 | DHCPCD_IPV6RS)
1304 if ((ifp->options->options & DHCPCD_RARENEW) == DHCPCD_RARENEW)
1305 ipv6nd_startrs(ifp);
1313 dhcpcd_renew(struct dhcpcd_ctx *ctx)
1315 struct interface *ifp;
1317 TAILQ_FOREACH(ifp, ctx->ifaces, next) {
1318 dhcpcd_ifrenew(ifp);
1323 #define sigmsg "received %s, %s"
1325 signal_cb(int sig, void *arg)
1327 struct dhcpcd_ctx *ctx = arg;
1328 unsigned long long opts;
1332 exit_code = EXIT_FAILURE;
1335 loginfox(sigmsg, "SIGINT", "stopping");
1338 loginfox(sigmsg, "SIGTERM", "stopping");
1339 exit_code = EXIT_SUCCESS;
1342 loginfox(sigmsg, "SIGALRM", "releasing");
1343 opts |= DHCPCD_RELEASE;
1344 exit_code = EXIT_SUCCESS;
1347 loginfox(sigmsg, "SIGHUP", "rebinding");
1349 /* Preserve any options passed on the commandline
1350 * when we were started. */
1351 reconf_reboot(ctx, 1, ctx->argc, ctx->argv,
1352 ctx->argc - ctx->ifc);
1355 loginfox(sigmsg, "SIGUSR1", "renewing");
1359 loginfox(sigmsg, "SIGUSR2", "reopening log");
1361 if (logopen(ctx->logfile) == -1)
1365 logwarnx("received SIGPIPE");
1368 logerrx("received signal %d but don't know what to do with it",
1373 if (!(ctx->options & DHCPCD_TEST))
1374 stop_all_interfaces(ctx, opts);
1375 eloop_exit(ctx->eloop, exit_code);
1380 dhcpcd_getinterfaces(void *arg)
1382 struct fd_list *fd = arg;
1383 struct interface *ifp;
1387 TAILQ_FOREACH(ifp, fd->ctx->ifaces, next) {
1392 if (D_STATE_RUNNING(ifp))
1396 if (IPV4LL_STATE_RUNNING(ifp))
1400 if (IPV6_STATE_RUNNING(ifp))
1402 if (RS_STATE_RUNNING(ifp))
1406 if (D6_STATE_RUNNING(ifp))
1410 if (write(fd->fd, &len, sizeof(len)) != sizeof(len))
1412 eloop_event_remove_writecb(fd->ctx->eloop, fd->fd);
1413 TAILQ_FOREACH(ifp, fd->ctx->ifaces, next) {
1416 if (send_interface(fd, ifp) == -1)
1422 dhcpcd_handleargs(struct dhcpcd_ctx *ctx, struct fd_list *fd,
1423 int argc, char **argv)
1425 struct interface *ifp;
1426 unsigned long long opts;
1427 int opt, oi, do_reboot, do_renew;
1431 /* Special commands for our control socket
1432 * as the other end should be blocking until it gets the
1433 * expected reply we should be safely able just to change the
1434 * write callback on the fd */
1435 if (strcmp(*argv, "--version") == 0) {
1436 return control_queue(fd, UNCONST(VERSION),
1437 strlen(VERSION) + 1, false);
1438 } else if (strcmp(*argv, "--getconfigfile") == 0) {
1439 return control_queue(fd, UNCONST(fd->ctx->cffile),
1440 strlen(fd->ctx->cffile) + 1, false);
1441 } else if (strcmp(*argv, "--getinterfaces") == 0) {
1442 eloop_event_add_w(fd->ctx->eloop, fd->fd,
1443 dhcpcd_getinterfaces, fd);
1445 } else if (strcmp(*argv, "--listen") == 0) {
1446 fd->flags |= FD_LISTEN;
1450 /* Only priviledged users can control dhcpcd via the socket. */
1451 if (fd->flags & FD_UNPRIV) {
1456 /* Log the command */
1458 for (opt = 0; opt < argc; opt++)
1459 len += strlen(argv[opt]) + 1;
1464 for (opt = 0; opt < argc; opt++) {
1465 l = strlen(argv[opt]);
1466 strlcpy(p, argv[opt], len);
1472 loginfox("control command: %s", tmp);
1478 do_reboot = do_renew = 0;
1479 while ((opt = getopt_long(argc, argv, IF_OPTS, cf_options, &oi)) != -1)
1483 /* Assumed if below not set */
1486 opts |= DHCPCD_RELEASE;
1492 opts |= DHCPCD_PERSISTENT;
1495 opts |= DHCPCD_EXITING;
1503 if (opts & (DHCPCD_EXITING | DHCPCD_RELEASE)) {
1504 if (optind == argc) {
1505 stop_all_interfaces(ctx, opts);
1506 eloop_exit(ctx->eloop, EXIT_SUCCESS);
1509 for (oi = optind; oi < argc; oi++) {
1510 if ((ifp = if_find(ctx->ifaces, argv[oi])) == NULL)
1514 ifp->options->options |= opts;
1515 if (opts & DHCPCD_RELEASE)
1516 ifp->options->options &= ~DHCPCD_PERSISTENT;
1517 stop_interface(ifp);
1523 if (optind == argc) {
1527 for (oi = optind; oi < argc; oi++) {
1528 if ((ifp = if_find(ctx->ifaces, argv[oi])) == NULL)
1530 dhcpcd_ifrenew(ifp);
1536 /* XXX: Respect initial commandline options? */
1537 reconf_reboot(ctx, do_reboot, argc, argv, optind - 1);
1542 main(int argc, char **argv)
1544 struct dhcpcd_ctx ctx;
1545 struct ifaddrs *ifaddrs = NULL;
1546 struct if_options *ifo;
1547 struct interface *ifp;
1548 uint16_t family = 0;
1550 unsigned int logopts;
1553 #if defined(USE_SIGNALS) || !defined(THERE_IS_NO_FORK)
1558 const char *siga = NULL;
1561 /* Test for --help and --version */
1563 if (strcmp(argv[1], "--help") == 0) {
1565 return EXIT_SUCCESS;
1566 } else if (strcmp(argv[1], "--version") == 0) {
1567 printf(""PACKAGE" "VERSION"\n%s\n", dhcpcd_copyright);
1568 printf("Compiled in features:"
1591 return EXIT_SUCCESS;
1595 memset(&ctx, 0, sizeof(ctx));
1598 ctx.cffile = CONFIG;
1599 ctx.control_fd = ctx.control_unpriv_fd = ctx.link_fd = -1;
1600 ctx.pf_inet_fd = -1;
1602 TAILQ_INIT(&ctx.control_fds);
1611 logopts = LOGERR_ERR|LOGERR_LOG|LOGERR_LOG_DATE|LOGERR_LOG_PID;
1613 while ((opt = getopt_long(argc, argv,
1614 ctx.options & DHCPCD_PRINT_PIDFILE ? NOERR_IF_OPTS : IF_OPTS,
1615 cf_options, &oi)) != -1)
1625 ctx.cffile = optarg;
1629 ctx.logfile = strdup(optarg);
1642 /* Force going via command socket as we're
1643 * out of user definable signals. */
1647 logopts |= LOGERR_QUIET;
1659 ctx.options |= DHCPCD_PRINT_PIDFILE;
1660 logopts &= ~(LOGERR_LOG | LOGERR_ERR);
1664 logopts &= ~LOGERR_LOG;
1673 if (ctx.options & DHCPCD_PRINT_PIDFILE)
1680 logsetopts(logopts);
1681 logopen(ctx.logfile);
1685 ctx.ifc = argc - optind;
1686 ctx.ifv = argv + optind;
1688 ifo = read_config(&ctx, NULL, NULL, NULL);
1690 if (ctx.options & DHCPCD_PRINT_PIDFILE)
1694 opt = add_options(&ctx, NULL, ifo, argc, argv);
1696 if (ctx.options & DHCPCD_PRINT_PIDFILE)
1703 printf("Interface options:\n");
1704 if (optind == argc - 1) {
1705 free_options(&ctx, ifo);
1706 ifo = read_config(&ctx, argv[optind], NULL, NULL);
1709 add_options(&ctx, NULL, ifo, argc, argv);
1713 if (family == 0 || family == AF_INET) {
1714 printf("\nDHCPv4 options:\n");
1715 dhcp_printoptions(&ctx,
1716 ifo->dhcp_override, ifo->dhcp_override_len);
1720 if (family == 0 || family == AF_INET6) {
1721 printf("\nND options:\n");
1722 ipv6nd_printoptions(&ctx,
1723 ifo->nd_override, ifo->nd_override_len);
1725 printf("\nDHCPv6 options:\n");
1726 dhcp6_printoptions(&ctx,
1727 ifo->dhcp6_override, ifo->dhcp6_override_len);
1733 ctx.options |= ifo->options;
1734 if (i == 1 || i == 3) {
1736 ctx.options |= DHCPCD_TEST;
1738 ctx.options |= DHCPCD_DUMPLEASE;
1739 ctx.options |= DHCPCD_PERSISTENT;
1740 ctx.options &= ~DHCPCD_DAEMONISE;
1743 #ifdef THERE_IS_NO_FORK
1744 ctx.options &= ~DHCPCD_DAEMONISE;
1747 if (ctx.options & DHCPCD_DEBUG)
1748 logsetopts(logopts | LOGERR_DEBUG);
1750 if (!(ctx.options & (DHCPCD_TEST | DHCPCD_DUMPLEASE))) {
1752 /* If we have any other args, we should run as a single dhcpcd
1753 * instance for that interface. */
1754 if (optind == argc - 1 && !(ctx.options & DHCPCD_MASTER)) {
1759 if (ifname == NULL || strlen(ifname) > IF_NAMESIZE) {
1760 errno = ifname == NULL ? EINVAL : E2BIG;
1761 logerr("%s: ", ifname);
1764 /* Allow a dhcpcd interface per address family */
1775 snprintf(ctx.pidfile, sizeof(ctx.pidfile),
1776 PIDFILE, "-", ifname, per);
1778 snprintf(ctx.pidfile, sizeof(ctx.pidfile),
1779 PIDFILE, "", "", "");
1780 ctx.options |= DHCPCD_MASTER;
1782 if (ctx.options & DHCPCD_PRINT_PIDFILE) {
1783 printf("%s\n", ctx.pidfile);
1788 if (chdir("/") == -1)
1789 logerr("%s: chdir `/'", __func__);
1791 /* Freeing allocated addresses from dumping leases can trigger
1792 * eloop removals as well, so init here. */
1793 if ((ctx.eloop = eloop_new()) == NULL) {
1794 logerr("%s: eloop_init", __func__);
1798 /* Save signal mask, block and redirect signals to our handler */
1799 if (eloop_signal_set_cb(ctx.eloop,
1800 dhcpcd_signals, dhcpcd_signals_len,
1801 signal_cb, &ctx) == -1)
1803 logerr("%s: eloop_signal_set_cb", __func__);
1806 if (eloop_signal_mask(ctx.eloop, &ctx.sigset) == -1) {
1807 logerr("%s: eloop_signal_mask", __func__);
1812 if (ctx.options & DHCPCD_DUMPLEASE) {
1813 /* Open sockets so we can dump something about
1814 * valid interfaces. */
1815 if (if_opensockets(&ctx) == -1) {
1816 logerr("%s: if_opensockets", __func__);
1819 if (optind != argc) {
1820 /* We need to try and find the interface so we can load
1821 * the hardware address to compare automated IAID */
1822 ctx.ifaces = if_discover(&ctx, &ifaddrs,
1823 argc - optind, argv + optind);
1825 if ((ctx.ifaces = malloc(sizeof(*ctx.ifaces))) != NULL)
1826 TAILQ_INIT(ctx.ifaces);
1828 if (ctx.ifaces == NULL) {
1829 logerr("%s: if_discover", __func__);
1832 ifp = if_find(ctx.ifaces, argv[optind]);
1834 ifp = calloc(1, sizeof(*ifp));
1840 strlcpy(ctx.pidfile, argv[optind],
1841 sizeof(ctx.pidfile));
1843 TAILQ_INSERT_HEAD(ctx.ifaces, ifp, next);
1845 if (ctx.pidfile[0] != '\0' &&
1846 ctx.pidfile[strlen(ctx.pidfile) - 1] == '6')
1852 configure_interface(ifp, ctx.argc, ctx.argv, 0);
1854 if (family == 0 || family == AF_INET) {
1856 if (dhcp_dump(ifp) == -1)
1859 if (family == AF_INET)
1860 logerrx("No INET support");
1863 if (family == 0 || family == AF_INET6) {
1865 if (dhcp6_dump(ifp) == -1)
1868 if (family == AF_INET6)
1869 logerrx("No DHCP6 support");
1878 /* Test against siga instead of sig to avoid gcc
1879 * warning about a bogus potential signed overflow.
1880 * The end result will be the same. */
1881 if ((siga == NULL || i == 4 || ctx.ifc != 0) &&
1882 !(ctx.options & DHCPCD_TEST))
1885 if (!(ctx.options & DHCPCD_MASTER))
1886 ctx.control_fd = control_open(argv[optind]);
1887 if (ctx.control_fd == -1)
1888 ctx.control_fd = control_open(NULL);
1889 if (ctx.control_fd != -1) {
1890 loginfox("sending commands to master dhcpcd process");
1891 len = control_send(&ctx, argc, argv);
1892 control_close(&ctx);
1894 logdebugx("send OK");
1897 logerr("%s: control_send", __func__);
1901 if (errno != ENOENT)
1902 logerr("%s: control_open", __func__);
1910 pid = pidfile_read(ctx.pidfile);
1911 if (pid != 0 && pid != -1)
1912 loginfox("sending signal %s to pid %d", siga, pid);
1913 if (pid == 0 || pid == -1 || kill(pid, sig) != 0) {
1914 if (sig != SIGHUP && sig != SIGUSR1 && errno != EPERM)
1915 logerrx(PACKAGE" not running");
1916 if (pid != 0 && pid != -1 && errno != ESRCH) {
1920 unlink(ctx.pidfile);
1921 if (sig != SIGHUP && sig != SIGUSR1)
1926 if (sig == SIGHUP || sig == SIGUSR1)
1928 /* Spin until it exits */
1929 loginfox("waiting for pid %d to exit", pid);
1931 ts.tv_nsec = 100000000; /* 10th of a second */
1932 for(i = 0; i < 100; i++) {
1933 nanosleep(&ts, NULL);
1934 if (pidfile_read(ctx.pidfile) == -1)
1937 logerrx("pid %d failed to exit", pid);
1942 if (!(ctx.options & DHCPCD_TEST)) {
1943 /* Ensure we have the needed directories */
1944 if (mkdir(RUNDIR, 0755) == -1 && errno != EEXIST)
1945 logerr("%s: mkdir `%s'", __func__, RUNDIR);
1946 if (mkdir(DBDIR, 0755) == -1 && errno != EEXIST)
1947 logerr("%s: mkdir `%s'", __func__, DBDIR);
1949 if ((pid = pidfile_lock(ctx.pidfile)) != 0) {
1951 logerr("%s: pidfile_lock", __func__);
1954 " already running on pid %d (%s)",
1960 if (ctx.options & DHCPCD_MASTER) {
1961 if (control_start(&ctx, NULL) == -1)
1962 logerr("%s: control_start", __func__);
1965 if (control_start(&ctx,
1966 ctx.options & DHCPCD_MASTER ? NULL : argv[optind]) == -1)
1968 logerr("%s: control_start", __func__);
1973 logdebugx(PACKAGE "-" VERSION " starting");
1974 ctx.options |= DHCPCD_STARTED;
1976 #ifdef HAVE_SETPROCTITLE
1977 setproctitle("%s%s%s",
1978 ctx.options & DHCPCD_MASTER ? "[master]" : argv[optind],
1979 ctx.options & DHCPCD_IPV4 ? " [ip4]" : "",
1980 ctx.options & DHCPCD_IPV6 ? " [ip6]" : "");
1983 if (if_opensockets(&ctx) == -1) {
1984 logerr("%s: if_opensockets", __func__);
1988 dhcpcd_setlinkrcvbuf(&ctx);
1991 /* When running dhcpcd against a single interface, we need to retain
1992 * the old behaviour of waiting for an IP address */
1993 if (ctx.ifc == 1 && !(ctx.options & DHCPCD_BACKGROUND))
1994 ctx.options |= DHCPCD_WAITIP;
1996 /* Start handling kernel messages for interfaces, addresses and
1998 eloop_event_add(ctx.eloop, ctx.link_fd, dhcpcd_handlelink, &ctx);
2000 /* Start any dev listening plugin which may want to
2001 * change the interface name provided by the kernel */
2002 if ((ctx.options & (DHCPCD_MASTER | DHCPCD_DEV)) ==
2003 (DHCPCD_MASTER | DHCPCD_DEV))
2006 ctx.ifaces = if_discover(&ctx, &ifaddrs, ctx.ifc, ctx.ifv);
2007 if (ctx.ifaces == NULL) {
2008 logerr("%s: if_discover", __func__);
2011 for (i = 0; i < ctx.ifc; i++) {
2012 if ((ifp = if_find(ctx.ifaces, ctx.ifv[i])) == NULL ||
2014 logerrx("%s: interface not found or invalid",
2017 TAILQ_FOREACH(ifp, ctx.ifaces, next) {
2018 if (ifp->active == IF_ACTIVE_USER)
2025 logfunc = ctx.options & DHCPCD_INACTIVE ?
2026 logdebugx : logerrx;
2027 logfunc("no valid interfaces found");
2030 if (!(ctx.options & DHCPCD_LINK)) {
2031 logerrx("aborting as link detection is disabled");
2036 TAILQ_FOREACH(ifp, ctx.ifaces, next) {
2038 dhcpcd_initstate1(ifp, argc, argv, 0);
2040 if_learnaddrs(&ctx, ctx.ifaces, &ifaddrs);
2042 if (ctx.options & DHCPCD_BACKGROUND && dhcpcd_daemonise(&ctx))
2046 TAILQ_FOREACH(ifp, ctx.ifaces, next) {
2049 if (!(ifp->options->options & DHCPCD_LINK) ||
2050 ifp->carrier != LINK_DOWN)
2055 if (!(ctx.options & DHCPCD_BACKGROUND)) {
2056 if (ctx.options & DHCPCD_MASTER)
2060 TAILQ_FOREACH(ifp, ctx.ifaces, next) {
2062 t = ifp->options->timeout;
2068 ctx.options & DHCPCD_LINK &&
2069 !(ctx.options & DHCPCD_WAITIP))
2073 logfunc = ctx.options & DHCPCD_INACTIVE ?
2074 logdebugx : logwarnx;
2075 logfunc("no interfaces have a carrier");
2076 if (dhcpcd_daemonise(&ctx))
2079 /* Test mode removes the daemonise bit, so check for both */
2080 ctx.options & (DHCPCD_DAEMONISE | DHCPCD_TEST))
2082 eloop_timeout_add_sec(ctx.eloop, t,
2083 handle_exit_timeout, &ctx);
2086 free_options(&ctx, ifo);
2089 TAILQ_FOREACH(ifp, ctx.ifaces, next) {
2091 eloop_timeout_add_sec(ctx.eloop, 0,
2092 dhcpcd_prestartinterface, ifp);
2095 i = eloop_start(ctx.eloop, &ctx.sigset);
2097 logerr("%s: eloop_start", __func__);
2110 if (ifaddrs != NULL)
2111 freeifaddrs(ifaddrs);
2112 if (control_stop(&ctx) == -1)
2113 logerr("%s: control_stop", __func__);
2114 /* Free memory and close fd's */
2116 while ((ifp = TAILQ_FIRST(ctx.ifaces))) {
2117 TAILQ_REMOVE(ctx.ifaces, ifp, next);
2122 free_options(&ctx, ifo);
2123 #ifdef HAVE_OPEN_MEMSTREAM
2125 fclose(ctx.script_fp);
2127 free(ctx.script_buf);
2128 free(ctx.script_env);
2131 if (ctx.link_fd != -1) {
2132 eloop_event_delete(ctx.eloop, ctx.link_fd);
2135 if_closesockets(&ctx);
2141 eloop_free(ctx.eloop);
2143 if (ctx.options & DHCPCD_STARTED && !(ctx.options & DHCPCD_FORKED))
2144 loginfox(PACKAGE " exited");
2148 if (ctx.options & DHCPCD_FORKED)
2149 _exit(i); /* so atexit won't remove our pidfile */