1 /* SPDX-License-Identifier: BSD-2-Clause */
3 * dhcpcd - DHCP client daemon
4 * Copyright (c) 2006-2020 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-2020 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 #define IF_UPANDRUNNING(a) \
101 (((a)->flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
103 const char *dhcpcd_default_script
= SCRIPT
;
109 printf("usage: "PACKAGE
"\t[-146ABbDdEGgHJKLMNPpqTV]\n"
110 "\t\t[-C, --nohook hook] [-c, --script script]\n"
111 "\t\t[-e, --env value] [-F, --fqdn FQDN] [-f, --config file]\n"
112 "\t\t[-h, --hostname hostname] [-I, --clientid clientid]\n"
113 "\t\t[-i, --vendorclassid vendorclassid] [-j, --logfile logfile]\n"
114 "\t\t[-l, --leasetime seconds] [-m, --metric metric]\n"
115 "\t\t[-O, --nooption option] [-o, --option option]\n"
116 "\t\t[-Q, --require option] [-r, --request address]\n"
117 "\t\t[-S, --static value]\n"
118 "\t\t[-s, --inform address[/cidr[/broadcast_address]]]\n [--inform6]"
119 "\t\t[-t, --timeout seconds] [-u, --userclass class]\n"
120 "\t\t[-v, --vendor code, value] [-W, --whitelist address[/cidr]] [-w]\n"
121 "\t\t[--waitip [4 | 6]] [-y, --reboot seconds]\n"
122 "\t\t[-X, --blacklist address[/cidr]] [-Z, --denyinterfaces pattern]\n"
123 "\t\t[-z, --allowinterfaces pattern] [--inactive] [interface] [...]\n"
124 " "PACKAGE
"\t-n, --rebind [interface]\n"
125 " "PACKAGE
"\t-k, --release [interface]\n"
126 " "PACKAGE
"\t-U, --dumplease interface\n"
127 " "PACKAGE
"\t--version\n"
128 " "PACKAGE
"\t-x, --exit [interface]\n");
132 free_globals(struct dhcpcd_ctx
*ctx
)
134 struct dhcp_opt
*opt
;
137 for (; ctx
->ifac
> 0; ctx
->ifac
--)
138 free(ctx
->ifav
[ctx
->ifac
- 1]);
143 for (; ctx
->ifdc
> 0; ctx
->ifdc
--)
144 free(ctx
->ifdv
[ctx
->ifdc
- 1]);
149 for (; ctx
->ifcc
> 0; ctx
->ifcc
--)
150 free(ctx
->ifcv
[ctx
->ifcc
- 1]);
156 if (ctx
->dhcp_opts
) {
157 for (opt
= ctx
->dhcp_opts
;
158 ctx
->dhcp_opts_len
> 0;
159 opt
++, ctx
->dhcp_opts_len
--)
160 free_dhcp_opt_embenc(opt
);
161 free(ctx
->dhcp_opts
);
162 ctx
->dhcp_opts
= NULL
;
167 for (opt
= ctx
->nd_opts
;
168 ctx
->nd_opts_len
> 0;
169 opt
++, ctx
->nd_opts_len
--)
170 free_dhcp_opt_embenc(opt
);
175 if (ctx
->dhcp6_opts
) {
176 for (opt
= ctx
->dhcp6_opts
;
177 ctx
->dhcp6_opts_len
> 0;
178 opt
++, ctx
->dhcp6_opts_len
--)
179 free_dhcp_opt_embenc(opt
);
180 free(ctx
->dhcp6_opts
);
181 ctx
->dhcp6_opts
= NULL
;
186 for (opt
= ctx
->vivso
;
188 opt
++, ctx
->vivso_len
--)
189 free_dhcp_opt_embenc(opt
);
196 handle_exit_timeout(void *arg
)
198 struct dhcpcd_ctx
*ctx
;
201 logerrx("timed out");
202 if (!(ctx
->options
& DHCPCD_MASTER
)) {
203 struct interface
*ifp
;
205 TAILQ_FOREACH(ifp
, ctx
->ifaces
, next
) {
206 if (ifp
->active
== IF_ACTIVE_USER
)
207 script_runreason(ifp
, "STOPPED");
209 eloop_exit(ctx
->eloop
, EXIT_FAILURE
);
212 ctx
->options
|= DHCPCD_NOWAITIP
;
213 dhcpcd_daemonise(ctx
);
233 dhcpcd_ifafwaiting(const struct interface
*ifp
)
235 unsigned long long opts
;
236 bool foundany
= false;
238 if (ifp
->active
!= IF_ACTIVE_USER
)
241 #define DHCPCD_WAITALL (DHCPCD_WAITIP4 | DHCPCD_WAITIP6)
242 opts
= ifp
->options
->options
;
244 if (opts
& DHCPCD_WAITIP4
||
245 (opts
& DHCPCD_WAITIP
&& !(opts
& DHCPCD_WAITALL
)))
247 bool foundaddr
= ipv4_hasaddr(ifp
);
249 if (opts
& DHCPCD_WAITIP4
&& !foundaddr
)
256 if (opts
& DHCPCD_WAITIP6
||
257 (opts
& DHCPCD_WAITIP
&& !(opts
& DHCPCD_WAITALL
)))
259 bool foundaddr
= ipv6_hasaddr(ifp
);
261 if (opts
& DHCPCD_WAITIP6
&& !foundaddr
)
268 if (opts
& DHCPCD_WAITIP
&& !(opts
& DHCPCD_WAITALL
) && !foundany
)
274 dhcpcd_afwaiting(const struct dhcpcd_ctx
*ctx
)
276 unsigned long long opts
;
277 const struct interface
*ifp
;
280 if (!(ctx
->options
& DHCPCD_WAITOPTS
))
284 TAILQ_FOREACH(ifp
, ctx
->ifaces
, next
) {
286 if (opts
& (DHCPCD_WAITIP
| DHCPCD_WAITIP4
) &&
288 opts
&= ~(DHCPCD_WAITIP
| DHCPCD_WAITIP4
);
291 if (opts
& (DHCPCD_WAITIP
| DHCPCD_WAITIP6
) &&
293 opts
&= ~(DHCPCD_WAITIP
| DHCPCD_WAITIP6
);
295 if (!(opts
& DHCPCD_WAITOPTS
))
298 if (opts
& DHCPCD_WAITIP
)
300 else if (opts
& DHCPCD_WAITIP4
)
302 else if (opts
& DHCPCD_WAITIP6
)
310 dhcpcd_ipwaited(struct dhcpcd_ctx
*ctx
)
312 struct interface
*ifp
;
315 TAILQ_FOREACH(ifp
, ctx
->ifaces
, next
) {
316 if ((af
= dhcpcd_ifafwaiting(ifp
)) != AF_MAX
) {
317 logdebugx("%s: waiting for an %s address",
318 ifp
->name
, dhcpcd_af(af
));
323 if ((af
= dhcpcd_afwaiting(ctx
)) != AF_MAX
) {
324 logdebugx("waiting for an %s address",
332 /* Returns the pid of the child, otherwise 0. */
334 dhcpcd_daemonise(struct dhcpcd_ctx
*ctx
)
336 #ifdef THERE_IS_NO_FORK
337 eloop_timeout_delete(ctx
->eloop
, handle_exit_timeout
, ctx
);
342 unsigned int logopts
= loggetopts();
344 if (ctx
->options
& DHCPCD_DAEMONISE
&&
345 !(ctx
->options
& (DHCPCD_DAEMONISED
| DHCPCD_NOWAITIP
)))
347 if (!dhcpcd_ipwaited(ctx
))
351 if (ctx
->options
& DHCPCD_ONESHOT
) {
352 loginfox("exiting due to oneshot");
353 eloop_exit(ctx
->eloop
, EXIT_SUCCESS
);
357 eloop_timeout_delete(ctx
->eloop
, handle_exit_timeout
, ctx
);
358 if (ctx
->options
& DHCPCD_DAEMONISED
||
359 !(ctx
->options
& DHCPCD_DAEMONISE
))
362 /* Don't use loginfo because this makes no sense in a log. */
363 if (!(logopts
& LOGERR_QUIET
))
364 (void)fprintf(stderr
,
365 "forked to background, child pid %d\n", getpid());
367 if (write(ctx
->fork_fd
, &i
, sizeof(i
)) == -1)
369 ctx
->options
|= DHCPCD_DAEMONISED
;
370 eloop_event_delete(ctx
->eloop
, ctx
->fork_fd
);
375 * Stop writing to stderr.
376 * On the happy path, only the master process writes to stderr,
377 * so this just stops wasting fprintf calls to nowhere.
378 * All other calls - ie errors in privsep processes or script output,
379 * will error when printing.
380 * If we *really* want to fix that, then we need to suck
381 * stderr/stdout in the master process and either disacrd it or pass
382 * it to the launcher process and then to stderr.
384 logopts
&= ~LOGERR_ERR
;
390 dhcpcd_drop(struct interface
*ifp
, int stop
)
394 dhcp6_drop(ifp
, stop
? NULL
: "EXPIRE6");
404 dhcp_drop(ifp
, stop
? "STOP" : "EXPIRE");
409 #if !defined(DHCP6) && !defined(DHCP)
415 stop_interface(struct interface
*ifp
)
417 struct dhcpcd_ctx
*ctx
;
420 loginfox("%s: removing interface", ifp
->name
);
421 ifp
->options
->options
|= DHCPCD_STOPPING
;
424 if (ifp
->options
->options
& DHCPCD_DEPARTED
)
425 script_runreason(ifp
, "DEPARTED");
427 script_runreason(ifp
, "STOPPED");
429 /* Delete all timeouts for the interfaces */
430 eloop_q_timeout_delete(ctx
->eloop
, ELOOP_QUEUE_ALL
, NULL
, ifp
);
432 /* De-activate the interface */
433 ifp
->active
= IF_INACTIVE
;
434 ifp
->options
->options
&= ~DHCPCD_STOPPING
;
435 /* Set the link state to unknown as we're no longer tracking it. */
436 ifp
->carrier
= LINK_UNKNOWN
;
438 if (!(ctx
->options
& (DHCPCD_MASTER
| DHCPCD_TEST
)))
439 eloop_exit(ctx
->eloop
, EXIT_FAILURE
);
443 configure_interface1(struct interface
*ifp
)
445 struct if_options
*ifo
= ifp
->options
;
447 /* Do any platform specific configuration */
450 /* If we want to release a lease, we can't really persist the
452 if (ifo
->options
& DHCPCD_RELEASE
)
453 ifo
->options
&= ~DHCPCD_PERSISTENT
;
455 if (ifp
->flags
& (IFF_POINTOPOINT
| IFF_LOOPBACK
)) {
456 ifo
->options
&= ~DHCPCD_ARP
;
457 if (!(ifp
->flags
& IFF_MULTICAST
))
458 ifo
->options
&= ~DHCPCD_IPV6RS
;
459 if (!(ifo
->options
& (DHCPCD_INFORM
| DHCPCD_WANTDHCP
)))
460 ifo
->options
|= DHCPCD_STATIC
;
463 if (ifo
->metric
!= -1)
464 ifp
->metric
= (unsigned int)ifo
->metric
;
467 /* We want to setup INET6 on the interface as soon as possible. */
468 if (ifp
->active
== IF_ACTIVE_USER
&&
469 ifo
->options
& DHCPCD_IPV6
&&
470 !(ifp
->ctx
->options
& (DHCPCD_DUMPLEASE
| DHCPCD_TEST
)))
472 /* If not doing any DHCP, disable the RDNSS requirement. */
473 if (!(ifo
->options
& (DHCPCD_DHCP
| DHCPCD_DHCP6
)))
474 ifo
->options
&= ~DHCPCD_IPV6RA_REQRDNSS
;
479 if (!(ifo
->options
& DHCPCD_IAID
)) {
481 * An IAID is for identifying a unqiue interface within
482 * the client. It is 4 bytes long. Working out a default
483 * value is problematic.
485 * Interface name and number are not stable
486 * between different OS's. Some OS's also cannot make
487 * up their mind what the interface should be called
488 * (yes, udev, I'm looking at you).
489 * Also, the name could be longer than 4 bytes.
490 * Also, with pluggable interfaces the name and index
491 * could easily get swapped per actual interface.
493 * The MAC address is 6 bytes long, the final 3
494 * being unique to the manufacturer and the initial 3
495 * being unique to the organisation which makes it.
496 * We could use the last 4 bytes of the MAC address
497 * as the IAID as it's the most stable part given the
498 * above, but equally it's not guaranteed to be
501 * Given the above, and our need to reliably work
502 * between reboots without persitent storage,
503 * generating the IAID from the MAC address is the only
505 * Saying that, if a VLANID has been specified then we
506 * can use that. It's possible that different interfaces
507 * can have the same VLANID, but this is no worse than
508 * generating the IAID from the duplicate MAC address.
510 * dhclient uses the last 4 bytes of the MAC address.
511 * dibbler uses an increamenting counter.
512 * wide-dhcpv6 uses 0 or a configured value.
514 * Windows 7 uses the first 3 bytes of the MAC address
515 * and an unknown byte.
516 * dhcpcd-6.1.0 and earlier used the interface name,
517 * falling back to interface index if name > 4.
519 if (ifp
->vlanid
!= 0) {
522 /* Maximal VLANID is 4095, so prefix with 0xff
523 * so we don't conflict with an interface index. */
524 vlanid
= htonl(ifp
->vlanid
| 0xff000000);
525 memcpy(ifo
->iaid
, &vlanid
, sizeof(vlanid
));
526 } else if (ifo
->options
& DHCPCD_ANONYMOUS
)
527 memset(ifo
->iaid
, 0, sizeof(ifo
->iaid
));
528 else if (ifp
->hwlen
>= sizeof(ifo
->iaid
)) {
530 ifp
->hwaddr
+ ifp
->hwlen
- sizeof(ifo
->iaid
),
535 len
= (uint32_t)strlen(ifp
->name
);
536 if (len
<= sizeof(ifo
->iaid
)) {
537 memcpy(ifo
->iaid
, ifp
->name
, len
);
538 if (len
< sizeof(ifo
->iaid
))
539 memset(ifo
->iaid
+ len
, 0,
540 sizeof(ifo
->iaid
) - len
);
542 /* IAID is the same size as a uint32_t */
543 len
= htonl(ifp
->index
);
544 memcpy(ifo
->iaid
, &len
, sizeof(ifo
->iaid
));
547 ifo
->options
|= DHCPCD_IAID
;
551 if (ifo
->ia_len
== 0 && ifo
->options
& DHCPCD_IPV6
&&
552 ifp
->name
[0] != '\0')
554 ifo
->ia
= malloc(sizeof(*ifo
->ia
));
559 ifo
->ia
->ia_type
= D6_OPTION_IA_NA
;
560 memcpy(ifo
->ia
->iaid
, ifo
->iaid
, sizeof(ifo
->iaid
));
561 memset(&ifo
->ia
->addr
, 0, sizeof(ifo
->ia
->addr
));
564 ifo
->ia
->sla_len
= 0;
570 for (i
= 0; i
< ifo
->ia_len
; i
++) {
571 if (!ifo
->ia
[i
].iaid_set
) {
572 memcpy(&ifo
->ia
[i
].iaid
, ifo
->iaid
,
573 sizeof(ifo
->ia
[i
].iaid
));
574 ifo
->ia
[i
].iaid_set
= 1;
580 /* If root is network mounted, we don't want to kill the connection
581 * if the DHCP server goes the way of the dodo OR dhcpcd is rebooting
582 * and the lease file has expired. */
583 if (is_root_local() == 0)
584 ifo
->options
|= DHCPCD_LASTLEASE_EXTEND
;
588 dhcpcd_selectprofile(struct interface
*ifp
, const char *profile
)
590 struct if_options
*ifo
;
591 char pssid
[PROFILE_LEN
];
596 r
= print_string(pssid
, sizeof(pssid
), OT_ESCSTRING
,
597 ifp
->ssid
, ifp
->ssid_len
);
604 ifo
= read_config(ifp
->ctx
, ifp
->name
, pssid
, profile
);
606 logdebugx("%s: no profile %s", ifp
->name
, profile
);
609 if (profile
!= NULL
) {
610 strlcpy(ifp
->profile
, profile
, sizeof(ifp
->profile
));
611 loginfox("%s: selected profile %s", ifp
->name
, profile
);
613 *ifp
->profile
= '\0';
615 free_options(ifp
->ctx
, ifp
->options
);
618 add_options(ifp
->ctx
, ifp
->name
, ifp
->options
,
619 ifp
->ctx
->argc
, ifp
->ctx
->argv
);
620 configure_interface1(ifp
);
626 configure_interface(struct interface
*ifp
, int argc
, char **argv
,
627 unsigned long long options
)
631 old
= ifp
->options
? ifp
->options
->mtime
: 0;
632 dhcpcd_selectprofile(ifp
, NULL
);
633 if (ifp
->options
== NULL
) {
634 /* dhcpcd cannot continue with this interface. */
635 ifp
->active
= IF_INACTIVE
;
638 add_options(ifp
->ctx
, ifp
->name
, ifp
->options
, argc
, argv
);
639 ifp
->options
->options
|= options
;
640 configure_interface1(ifp
);
642 /* If the mtime has changed drop any old lease */
643 if (old
!= 0 && ifp
->options
->mtime
!= old
) {
644 logwarnx("%s: config file changed, expiring leases",
651 dhcpcd_initstate2(struct interface
*ifp
, unsigned long long options
)
653 struct if_options
*ifo
;
656 if ((ifo
= default_config(ifp
->ctx
)) == NULL
) {
660 ifo
->options
|= options
;
667 if (ifo
->options
& DHCPCD_IPV6
&& ipv6_init(ifp
->ctx
) == -1) {
669 ifo
->options
&= ~DHCPCD_IPV6
;
675 dhcpcd_initstate1(struct interface
*ifp
, int argc
, char **argv
,
676 unsigned long long options
)
679 configure_interface(ifp
, argc
, argv
, options
);
681 dhcpcd_initstate2(ifp
, 0);
685 dhcpcd_initstate(struct interface
*ifp
, unsigned long long options
)
688 dhcpcd_initstate1(ifp
, ifp
->ctx
->argc
, ifp
->ctx
->argv
, options
);
692 dhcpcd_reportssid(struct interface
*ifp
)
694 char pssid
[IF_SSIDLEN
* 4];
696 if (print_string(pssid
, sizeof(pssid
), OT_ESCSTRING
,
697 ifp
->ssid
, ifp
->ssid_len
) == -1)
703 loginfox("%s: connected to Access Point `%s'", ifp
->name
, pssid
);
707 dhcpcd_handlecarrier(struct dhcpcd_ctx
*ctx
, int carrier
, unsigned int flags
,
710 struct interface
*ifp
;
712 ifp
= if_find(ctx
->ifaces
, ifname
);
714 ifp
->options
== NULL
|| !(ifp
->options
->options
& DHCPCD_LINK
) ||
718 if (carrier
== LINK_UNKNOWN
) {
723 carrier
= if_carrier(ifp
);
726 if (carrier
== LINK_UNKNOWN
)
727 carrier
= IF_UPANDRUNNING(ifp
) ? LINK_UP
: LINK_DOWN
;
729 if (carrier
== LINK_DOWN
|| (ifp
->flags
& IFF_UP
) == 0) {
730 if (ifp
->carrier
!= LINK_DOWN
) {
731 if (ifp
->carrier
== LINK_UP
)
732 loginfox("%s: carrier lost", ifp
->name
);
733 #ifdef NOCARRIER_PRESERVE_IP
734 if (ifp
->flags
& IFF_UP
&&
735 !(ifp
->options
->options
& DHCPCD_ANONYMOUS
))
736 ifp
->carrier
= LINK_DOWN_IFFUP
;
739 ifp
->carrier
= LINK_DOWN
;
740 script_runreason(ifp
, "NOCARRIER");
741 #ifdef NOCARRIER_PRESERVE_IP
742 if (ifp
->flags
& IFF_UP
&&
743 !(ifp
->options
->options
& DHCPCD_ANONYMOUS
))
757 if (ifp
->options
->options
& DHCPCD_ANONYMOUS
) {
758 bool was_up
= ifp
->flags
& IFF_UP
;
762 if (if_randomisemac(ifp
) == -1 && errno
!= ENXIO
)
768 } else if (carrier
== LINK_UP
&& ifp
->flags
& IFF_UP
) {
769 if (ifp
->carrier
!= LINK_UP
) {
770 loginfox("%s: carrier acquired", ifp
->name
);
771 ifp
->carrier
= LINK_UP
;
772 #if !defined(__linux__) && !defined(__NetBSD__)
773 /* BSD does not emit RTM_NEWADDR or RTM_CHGADDR when the
774 * hardware address changes so we have to go
775 * through the disovery process to work it out. */
776 dhcpcd_handleinterface(ctx
, 0, ifp
->name
);
779 uint8_t ossid
[IF_SSIDLEN
];
782 olen
= ifp
->ssid_len
;
783 memcpy(ossid
, ifp
->ssid
, ifp
->ssid_len
);
786 /* If we changed SSID network, drop leases */
787 if (ifp
->ssid_len
!= olen
||
788 memcmp(ifp
->ssid
, ossid
, ifp
->ssid_len
))
790 dhcpcd_reportssid(ifp
);
791 #ifdef NOCARRIER_PRESERVE_IP
799 dhcpcd_initstate(ifp
, 0);
800 script_runreason(ifp
, "CARRIER");
802 #ifdef NOCARRIER_PRESERVE_IP
803 /* Set any IPv6 Routers we remembered to expire
804 * faster than they would normally as we
805 * maybe on a new network. */
806 ipv6nd_startexpire(ifp
);
808 #ifdef IPV6_MANAGETEMPADDR
809 /* RFC4941 Section 3.5 */
810 ipv6_regentempaddrs(ifp
);
813 dhcpcd_startinterface(ifp
);
819 warn_iaid_conflict(struct interface
*ifp
, uint16_t ia_type
, uint8_t *iaid
)
821 struct interface
*ifn
;
827 TAILQ_FOREACH(ifn
, ifp
->ctx
->ifaces
, next
) {
828 if (ifn
== ifp
|| !ifn
->active
)
830 if (ifn
->options
->options
& DHCPCD_ANONYMOUS
)
833 memcmp(ifn
->options
->iaid
, iaid
,
834 sizeof(ifn
->options
->iaid
)) == 0)
837 for (i
= 0; i
< ifn
->options
->ia_len
; i
++) {
838 ia
= &ifn
->options
->ia
[i
];
839 if (ia
->ia_type
== ia_type
&&
840 memcmp(ia
->iaid
, iaid
, sizeof(ia
->iaid
)) == 0)
846 /* This is only a problem if the interfaces are on the same network. */
848 logerrx("%s: IAID conflicts with one assigned to %s",
849 ifp
->name
, ifn
->name
);
853 dhcpcd_initduid(struct dhcpcd_ctx
*ctx
, struct interface
*ifp
)
855 char buf
[DUID_LEN
* 3];
857 if (ctx
->duid
!= NULL
)
861 if (ctx
->duid
== NULL
)
865 hwaddr_ntoa(ctx
->duid
, ctx
->duid_len
, buf
, sizeof(buf
)));
869 dhcpcd_startinterface(void *arg
)
871 struct interface
*ifp
= arg
;
872 struct if_options
*ifo
= ifp
->options
;
874 if (ifo
->options
& DHCPCD_LINK
) {
875 switch (ifp
->carrier
) {
879 loginfox("%s: waiting for carrier", ifp
->name
);
882 /* No media state available.
883 * Loop until both IFF_UP and IFF_RUNNING are set */
890 if (ifo
->options
& (DHCPCD_DUID
| DHCPCD_IPV6
) &&
891 !(ifo
->options
& DHCPCD_ANONYMOUS
))
893 char buf
[sizeof(ifo
->iaid
) * 3];
899 /* Try and init DUID from the interface hardware address */
900 dhcpcd_initduid(ifp
->ctx
, ifp
);
903 loginfox("%s: IAID %s", ifp
->name
,
904 hwaddr_ntoa(ifo
->iaid
, sizeof(ifo
->iaid
),
906 warn_iaid_conflict(ifp
, 0, ifo
->iaid
);
909 for (i
= 0; i
< ifo
->ia_len
; i
++) {
911 if (memcmp(ifo
->iaid
, ia
->iaid
, sizeof(ifo
->iaid
))) {
912 loginfox("%s: IA type %u IAID %s",
913 ifp
->name
, ia
->ia_type
,
914 hwaddr_ntoa(ia
->iaid
, sizeof(ia
->iaid
),
916 warn_iaid_conflict(ifp
, ia
->ia_type
, ia
->iaid
);
923 if (ifo
->options
& DHCPCD_IPV6
&& ipv6_start(ifp
) == -1) {
924 logerr("%s: ipv6_start", ifp
->name
);
925 ifo
->options
&= ~DHCPCD_IPV6
;
928 if (ifo
->options
& DHCPCD_IPV6
) {
929 if (ifp
->active
== IF_ACTIVE_USER
) {
930 ipv6_startstatic(ifp
);
932 if (ifo
->options
& DHCPCD_IPV6RS
)
937 /* DHCPv6 could be turned off, but the interface
938 * is still delegated to. */
940 dhcp6_find_delegates(ifp
);
942 if (ifo
->options
& DHCPCD_DHCP6
) {
943 if (ifp
->active
== IF_ACTIVE_USER
) {
946 if (ifo
->options
& DHCPCD_IA_FORCED
)
947 d6_state
= DH6S_INIT
;
948 else if (ifo
->options
& DHCPCD_INFORM6
)
949 d6_state
= DH6S_INFORM
;
951 d6_state
= DH6S_CONFIRM
;
952 if (dhcp6_start(ifp
, d6_state
) == -1)
953 logerr("%s: dhcp6_start", ifp
->name
);
961 if (ifo
->options
& DHCPCD_IPV4
&& ifp
->active
== IF_ACTIVE_USER
) {
962 /* Ensure we have an IPv4 state before starting DHCP */
963 if (ipv4_getstate(ifp
) != NULL
)
970 dhcpcd_prestartinterface(void *arg
)
972 struct interface
*ifp
= arg
;
973 struct dhcpcd_ctx
*ctx
= ifp
->ctx
;
976 if (ifp
->carrier
== LINK_DOWN
&&
977 ifp
->options
->options
& DHCPCD_ANONYMOUS
&&
985 if ((!(ctx
->options
& DHCPCD_MASTER
) ||
986 ifp
->options
->options
& DHCPCD_IF_UP
|| anondown
) &&
987 !(ifp
->flags
& IFF_UP
))
989 if (ifp
->options
->options
& DHCPCD_ANONYMOUS
&&
990 if_randomisemac(ifp
) == -1)
992 if (if_up(ifp
) == -1)
996 if (ifp
->options
->poll
!= 0)
999 dhcpcd_startinterface(ifp
);
1003 run_preinit(struct interface
*ifp
)
1006 if (ifp
->ctx
->options
& DHCPCD_TEST
)
1009 script_runreason(ifp
, "PREINIT");
1010 if (ifp
->wireless
&& ifp
->carrier
== LINK_UP
)
1011 dhcpcd_reportssid(ifp
);
1012 if (ifp
->options
->options
& DHCPCD_LINK
&& ifp
->carrier
!= LINK_UNKNOWN
)
1013 script_runreason(ifp
,
1014 ifp
->carrier
== LINK_UP
? "CARRIER" : "NOCARRIER");
1018 dhcpcd_activateinterface(struct interface
*ifp
, unsigned long long options
)
1022 ifp
->active
= IF_ACTIVE
;
1023 dhcpcd_initstate2(ifp
, options
);
1024 /* It's possible we might not have been able to load
1027 configure_interface1(ifp
);
1029 dhcpcd_prestartinterface(ifp
);
1035 dhcpcd_handleinterface(void *arg
, int action
, const char *ifname
)
1037 struct dhcpcd_ctx
*ctx
;
1038 struct ifaddrs
*ifaddrs
;
1039 struct if_head
*ifs
;
1040 struct interface
*ifp
, *iff
;
1041 const char * const argv
[] = { ifname
};
1046 ifp
= if_find(ctx
->ifaces
, ifname
);
1052 logdebugx("%s: interface departed", ifp
->name
);
1053 ifp
->options
->options
|= DHCPCD_DEPARTED
;
1054 stop_interface(ifp
);
1056 TAILQ_REMOVE(ctx
->ifaces
, ifp
, next
);
1061 ifs
= if_discover(ctx
, &ifaddrs
, -1, UNCONST(argv
));
1067 ifp
= if_find(ifs
, ifname
);
1069 /* This can happen if an interface is quickly added
1070 * and then removed. */
1077 /* Check if we already have the interface */
1078 iff
= if_find(ctx
->ifaces
, ifp
->name
);
1082 logdebugx("%s: interface updated", iff
->name
);
1083 /* The flags and hwaddr could have changed */
1084 iff
->flags
= ifp
->flags
;
1085 iff
->hwlen
= ifp
->hwlen
;
1086 if (ifp
->hwlen
!= 0)
1087 memcpy(iff
->hwaddr
, ifp
->hwaddr
, iff
->hwlen
);
1089 TAILQ_REMOVE(ifs
, ifp
, next
);
1090 TAILQ_INSERT_TAIL(ctx
->ifaces
, ifp
, next
);
1092 logdebugx("%s: interface added", ifp
->name
);
1093 dhcpcd_initstate(ifp
, 0);
1100 if_learnaddrs(ctx
, ifs
, &ifaddrs
);
1102 dhcpcd_prestartinterface(iff
);
1106 /* Free our discovered list */
1107 while ((ifp
= TAILQ_FIRST(ifs
))) {
1108 TAILQ_REMOVE(ifs
, ifp
, next
);
1117 dhcpcd_handlelink(void *arg
)
1119 struct dhcpcd_ctx
*ctx
= arg
;
1121 if (if_handlelink(ctx
) == -1) {
1122 if (errno
== ENOBUFS
|| errno
== ENOMEM
) {
1123 dhcpcd_linkoverflow(ctx
);
1126 if (errno
!= ENOTSUP
)
1132 dhcpcd_checkcarrier(void *arg
)
1134 struct interface
*ifp
= arg
;
1137 /* Check carrier here rather than setting LINK_UNKNOWN.
1138 * This is because we force LINK_UNKNOWN as down for wireless which
1139 * we do not want when dealing with a route socket overflow. */
1140 carrier
= if_carrier(ifp
);
1141 dhcpcd_handlecarrier(ifp
->ctx
, carrier
, ifp
->flags
, ifp
->name
);
1146 dhcpcd_setlinkrcvbuf(struct dhcpcd_ctx
*ctx
)
1150 if (ctx
->link_rcvbuf
== 0)
1153 logdebugx("setting route socket receive buffer size to %d bytes",
1156 socklen
= sizeof(ctx
->link_rcvbuf
);
1157 if (setsockopt(ctx
->link_fd
, SOL_SOCKET
,
1158 SO_RCVBUF
, &ctx
->link_rcvbuf
, socklen
) == -1)
1164 dhcpcd_runprestartinterface(void *arg
)
1166 struct interface
*ifp
= arg
;
1169 dhcpcd_prestartinterface(ifp
);
1173 dhcpcd_linkoverflow(struct dhcpcd_ctx
*ctx
)
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)
1193 logerrx("route socket overflowed (rcvbuflen %d)"
1194 " - learning interface state", rcvbuflen
);
1196 /* Drain the socket.
1197 * We cannot open a new one due to privsep. */
1200 rlen
= read(ctx
->link_fd
, buf
, sizeof(buf
));
1201 if (++rcnt
% 1000 == 0)
1202 logwarnx("drained %zu messages", rcnt
);
1203 } while (rlen
!= -1 || errno
== ENOBUFS
|| errno
== ENOMEM
);
1204 if (rcnt
% 1000 != 0)
1205 logwarnx("drained %zu messages", rcnt
);
1207 /* Work out the current interfaces. */
1208 ifaces
= if_discover(ctx
, &ifaddrs
, ctx
->ifc
, ctx
->ifv
);
1209 if (ifaces
== NULL
) {
1214 /* Punt departed interfaces */
1215 TAILQ_FOREACH_SAFE(ifp
, ctx
->ifaces
, next
, ifn
) {
1216 if (if_find(ifaces
, ifp
->name
) != NULL
)
1218 dhcpcd_handleinterface(ctx
, -1, ifp
->name
);
1221 /* Add new interfaces */
1222 while ((ifp
= TAILQ_FIRST(ifaces
)) != NULL
) {
1223 TAILQ_REMOVE(ifaces
, ifp
, next
);
1224 ifp1
= if_find(ctx
->ifaces
, ifp
->name
);
1226 /* If the interface already exists,
1227 * check carrier state. */
1228 eloop_timeout_add_sec(ctx
->eloop
, 0,
1229 dhcpcd_checkcarrier
, ifp1
);
1233 TAILQ_INSERT_TAIL(ctx
->ifaces
, ifp
, next
);
1235 dhcpcd_initstate(ifp
, 0);
1236 eloop_timeout_add_sec(ctx
->eloop
, 0,
1237 dhcpcd_runprestartinterface
, ifp
);
1242 /* Update address state. */
1243 if_markaddrsstale(ctx
->ifaces
);
1244 if_learnaddrs(ctx
, ctx
->ifaces
, &ifaddrs
);
1245 if_deletestaleaddrs(ctx
->ifaces
);
1249 dhcpcd_handlehwaddr(struct interface
*ifp
,
1250 uint16_t hwtype
, const void *hwaddr
, uint8_t hwlen
)
1252 char buf
[sizeof(ifp
->hwaddr
) * 3];
1254 if (hwaddr
== NULL
|| !if_valid_hwaddr(hwaddr
, hwlen
))
1257 if (hwlen
> sizeof(ifp
->hwaddr
)) {
1259 logerr("%s: %s", __func__
, ifp
->name
);
1263 if (ifp
->hwtype
!= hwtype
) {
1264 loginfox("%s: hardware address type changed from %d to %d",
1265 ifp
->name
, ifp
->hwtype
, hwtype
);
1266 ifp
->hwtype
= hwtype
;
1269 if (ifp
->hwlen
== hwlen
&&
1270 (hwlen
== 0 || memcmp(ifp
->hwaddr
, hwaddr
, hwlen
) == 0))
1273 loginfox("%s: new hardware address: %s", ifp
->name
,
1274 hwaddr_ntoa(hwaddr
, hwlen
, buf
, sizeof(buf
)));
1277 memcpy(ifp
->hwaddr
, hwaddr
, hwlen
);
1281 if_reboot(struct interface
*ifp
, int argc
, char **argv
)
1284 unsigned long long oldopts
;
1286 oldopts
= ifp
->options
->options
;
1288 script_runreason(ifp
, "RECONFIGURE");
1289 dhcpcd_initstate1(ifp
, argc
, argv
, 0);
1291 dhcp_reboot_newopts(ifp
, oldopts
);
1296 dhcpcd_prestartinterface(ifp
);
1300 reload_config(struct dhcpcd_ctx
*ctx
)
1302 struct if_options
*ifo
;
1305 if ((ifo
= read_config(ctx
, NULL
, NULL
, NULL
)) == NULL
)
1307 add_options(ctx
, NULL
, ifo
, ctx
->argc
, ctx
->argv
);
1308 /* We need to preserve these options. */
1309 if (ctx
->options
& DHCPCD_STARTED
)
1310 ifo
->options
|= DHCPCD_STARTED
;
1311 if (ctx
->options
& DHCPCD_MASTER
)
1312 ifo
->options
|= DHCPCD_MASTER
;
1313 if (ctx
->options
& DHCPCD_DAEMONISED
)
1314 ifo
->options
|= DHCPCD_DAEMONISED
;
1315 if (ctx
->options
& DHCPCD_PRIVSEP
)
1316 ifo
->options
|= DHCPCD_PRIVSEP
;
1317 ctx
->options
= ifo
->options
;
1318 free_options(ctx
, ifo
);
1322 reconf_reboot(struct dhcpcd_ctx
*ctx
, int action
, int argc
, char **argv
, int oi
)
1325 struct interface
*ifp
;
1327 TAILQ_FOREACH(ifp
, ctx
->ifaces
, next
) {
1328 for (i
= oi
; i
< argc
; i
++) {
1329 if (strcmp(ifp
->name
, argv
[i
]) == 0)
1332 if (oi
!= argc
&& i
== argc
)
1334 if (ifp
->active
== IF_ACTIVE_USER
) {
1336 if_reboot(ifp
, argc
, argv
);
1339 ipv4_applyaddr(ifp
);
1341 } else if (i
!= argc
) {
1342 ifp
->active
= IF_ACTIVE_USER
;
1343 dhcpcd_initstate1(ifp
, argc
, argv
, 0);
1345 dhcpcd_prestartinterface(ifp
);
1351 stop_all_interfaces(struct dhcpcd_ctx
*ctx
, unsigned long long opts
)
1353 struct interface
*ifp
;
1355 ctx
->options
|= DHCPCD_EXITING
;
1356 if (ctx
->ifaces
== NULL
)
1359 /* Drop the last interface first */
1360 TAILQ_FOREACH_REVERSE(ifp
, ctx
->ifaces
, if_head
, next
) {
1363 ifp
->options
->options
|= opts
;
1364 if (ifp
->options
->options
& DHCPCD_RELEASE
)
1365 ifp
->options
->options
&= ~DHCPCD_PERSISTENT
;
1366 ifp
->options
->options
|= DHCPCD_EXITING
;
1367 stop_interface(ifp
);
1372 dhcpcd_ifrenew(struct interface
*ifp
)
1378 if (ifp
->options
->options
& DHCPCD_LINK
&&
1379 ifp
->carrier
== LINK_DOWN
)
1386 #define DHCPCD_RARENEW (DHCPCD_IPV6 | DHCPCD_IPV6RS)
1387 if ((ifp
->options
->options
& DHCPCD_RARENEW
) == DHCPCD_RARENEW
)
1388 ipv6nd_startrs(ifp
);
1396 dhcpcd_renew(struct dhcpcd_ctx
*ctx
)
1398 struct interface
*ifp
;
1400 TAILQ_FOREACH(ifp
, ctx
->ifaces
, next
) {
1401 dhcpcd_ifrenew(ifp
);
1406 #define sigmsg "received %s, %s"
1408 dhcpcd_signal_cb(int sig
, void *arg
)
1410 struct dhcpcd_ctx
*ctx
= arg
;
1411 unsigned long long opts
;
1414 if (ctx
->options
& DHCPCD_DUMPLEASE
) {
1415 eloop_exit(ctx
->eloop
, EXIT_FAILURE
);
1419 if (sig
!= SIGCHLD
&& ctx
->options
& DHCPCD_FORKED
) {
1423 pid_t pid
= pidfile_read(ctx
->pidfile
);
1425 if (errno
!= ENOENT
)
1426 logerr("%s: pidfile_read",__func__
);
1427 } else if (pid
== 0)
1428 logerr("%s: pid cannot be zero", __func__
);
1429 else if (kill(pid
, sig
) == -1)
1430 logerr("%s: kill", __func__
);
1435 exit_code
= EXIT_FAILURE
;
1438 loginfox(sigmsg
, "SIGINT", "stopping");
1441 loginfox(sigmsg
, "SIGTERM", "stopping");
1442 exit_code
= EXIT_SUCCESS
;
1445 loginfox(sigmsg
, "SIGALRM", "releasing");
1446 opts
|= DHCPCD_RELEASE
;
1447 exit_code
= EXIT_SUCCESS
;
1450 loginfox(sigmsg
, "SIGHUP", "rebinding");
1452 /* Preserve any options passed on the commandline
1453 * when we were started. */
1454 reconf_reboot(ctx
, 1, ctx
->argc
, ctx
->argv
,
1455 ctx
->argc
- ctx
->ifc
);
1458 loginfox(sigmsg
, "SIGUSR1", "renewing");
1462 loginfox(sigmsg
, "SIGUSR2", "reopening log");
1463 /* XXX This may not work that well in a chroot */
1465 if (logopen(ctx
->logfile
) == -1)
1469 while (waitpid(-1, NULL
, WNOHANG
) > 0)
1473 logerrx("received signal %d but don't know what to do with it",
1478 if (!(ctx
->options
& DHCPCD_TEST
))
1479 stop_all_interfaces(ctx
, opts
);
1480 eloop_exit(ctx
->eloop
, exit_code
);
1485 dhcpcd_handleargs(struct dhcpcd_ctx
*ctx
, struct fd_list
*fd
,
1486 int argc
, char **argv
)
1488 struct interface
*ifp
;
1489 unsigned long long opts
;
1490 int opt
, oi
, do_reboot
, do_renew
, af
= AF_UNSPEC
;
1491 size_t len
, l
, nifaces
;
1494 /* Special commands for our control socket
1495 * as the other end should be blocking until it gets the
1496 * expected reply we should be safely able just to change the
1497 * write callback on the fd */
1498 /* Make any change here in privsep-control.c as well. */
1499 if (strcmp(*argv
, "--version") == 0) {
1500 return control_queue(fd
, UNCONST(VERSION
),
1501 strlen(VERSION
) + 1);
1502 } else if (strcmp(*argv
, "--getconfigfile") == 0) {
1503 return control_queue(fd
, UNCONST(fd
->ctx
->cffile
),
1504 strlen(fd
->ctx
->cffile
) + 1);
1505 } else if (strcmp(*argv
, "--getinterfaces") == 0) {
1508 } else if (strcmp(*argv
, "--listen") == 0) {
1509 fd
->flags
|= FD_LISTEN
;
1513 /* Log the command */
1515 for (opt
= 0; opt
< argc
; opt
++)
1516 len
+= strlen(argv
[opt
]) + 1;
1521 for (opt
= 0; opt
< argc
; opt
++) {
1522 l
= strlen(argv
[opt
]);
1523 strlcpy(p
, argv
[opt
], len
);
1529 loginfox("control command: %s", tmp
);
1535 do_reboot
= do_renew
= 0;
1536 while ((opt
= getopt_long(argc
, argv
, IF_OPTS
, cf_options
, &oi
)) != -1)
1540 /* Assumed if below not set */
1543 opts
|= DHCPCD_RELEASE
;
1549 opts
|= DHCPCD_PERSISTENT
;
1552 opts
|= DHCPCD_EXITING
;
1558 opts
|= DHCPCD_DUMPLEASE
;
1569 if (opts
& DHCPCD_DUMPLEASE
) {
1570 ctx
->options
|= DHCPCD_DUMPLEASE
;
1573 TAILQ_FOREACH(ifp
, ctx
->ifaces
, next
) {
1576 for (oi
= optind
; oi
< argc
; oi
++) {
1577 if (strcmp(ifp
->name
, argv
[oi
]) == 0)
1580 if (optind
== argc
|| oi
< argc
) {
1581 opt
= send_interface(NULL
, ifp
, af
);
1584 nifaces
+= (size_t)opt
;
1587 if (write(fd
->fd
, &nifaces
, sizeof(nifaces
)) != sizeof(nifaces
))
1589 TAILQ_FOREACH(ifp
, ctx
->ifaces
, next
) {
1592 for (oi
= optind
; oi
< argc
; oi
++) {
1593 if (strcmp(ifp
->name
, argv
[oi
]) == 0)
1596 if (optind
== argc
|| oi
< argc
) {
1597 if (send_interface(fd
, ifp
, af
) == -1)
1601 ctx
->options
&= ~DHCPCD_DUMPLEASE
;
1604 ctx
->options
&= ~DHCPCD_DUMPLEASE
;
1608 /* Only privileged users can control dhcpcd via the socket. */
1609 if (fd
->flags
& FD_UNPRIV
) {
1614 if (opts
& (DHCPCD_EXITING
| DHCPCD_RELEASE
)) {
1615 if (optind
== argc
) {
1616 stop_all_interfaces(ctx
, opts
);
1617 eloop_exit(ctx
->eloop
, EXIT_SUCCESS
);
1620 for (oi
= optind
; oi
< argc
; oi
++) {
1621 if ((ifp
= if_find(ctx
->ifaces
, argv
[oi
])) == NULL
)
1625 ifp
->options
->options
|= opts
;
1626 if (opts
& DHCPCD_RELEASE
)
1627 ifp
->options
->options
&= ~DHCPCD_PERSISTENT
;
1628 stop_interface(ifp
);
1634 if (optind
== argc
) {
1638 for (oi
= optind
; oi
< argc
; oi
++) {
1639 if ((ifp
= if_find(ctx
->ifaces
, argv
[oi
])) == NULL
)
1641 dhcpcd_ifrenew(ifp
);
1647 /* XXX: Respect initial commandline options? */
1648 reconf_reboot(ctx
, do_reboot
, argc
, argv
, optind
- 1);
1652 static void dhcpcd_readdump1(void *);
1655 dhcpcd_readdump2(void *arg
)
1657 struct dhcpcd_ctx
*ctx
= arg
;
1659 int exit_code
= EXIT_FAILURE
;
1661 len
= read(ctx
->control_fd
, ctx
->ctl_buf
+ ctx
->ctl_bufpos
,
1662 ctx
->ctl_buflen
- ctx
->ctl_bufpos
);
1666 } else if (len
== 0)
1668 if ((size_t)len
+ ctx
->ctl_bufpos
!= ctx
->ctl_buflen
) {
1669 ctx
->ctl_bufpos
+= (size_t)len
;
1673 if (ctx
->ctl_buf
[ctx
->ctl_buflen
- 1] != '\0') /* unlikely */
1674 ctx
->ctl_buf
[ctx
->ctl_buflen
- 1] = '\0';
1675 script_dump(ctx
->ctl_buf
, ctx
->ctl_buflen
);
1677 if (--ctx
->ctl_extra
!= 0) {
1679 eloop_event_add(ctx
->eloop
, ctx
->control_fd
,
1680 dhcpcd_readdump1
, ctx
);
1683 exit_code
= EXIT_SUCCESS
;
1686 shutdown(ctx
->control_fd
, SHUT_RDWR
);
1687 eloop_exit(ctx
->eloop
, exit_code
);
1691 dhcpcd_readdump1(void *arg
)
1693 struct dhcpcd_ctx
*ctx
= arg
;
1696 len
= read(ctx
->control_fd
, &ctx
->ctl_buflen
, sizeof(ctx
->ctl_buflen
));
1697 if (len
!= sizeof(ctx
->ctl_buflen
)) {
1702 if (ctx
->ctl_buflen
> SSIZE_MAX
) {
1708 ctx
->ctl_buf
= malloc(ctx
->ctl_buflen
);
1709 if (ctx
->ctl_buf
== NULL
)
1712 ctx
->ctl_bufpos
= 0;
1713 eloop_event_add(ctx
->eloop
, ctx
->control_fd
,
1714 dhcpcd_readdump2
, ctx
);
1719 eloop_exit(ctx
->eloop
, EXIT_FAILURE
);
1723 dhcpcd_readdump0(void *arg
)
1725 struct dhcpcd_ctx
*ctx
= arg
;
1728 len
= read(ctx
->control_fd
, &ctx
->ctl_extra
, sizeof(ctx
->ctl_extra
));
1729 if (len
!= sizeof(ctx
->ctl_extra
)) {
1733 eloop_exit(ctx
->eloop
, EXIT_FAILURE
);
1737 if (ctx
->ctl_extra
== 0) {
1738 eloop_exit(ctx
->eloop
, EXIT_SUCCESS
);
1742 eloop_event_add(ctx
->eloop
, ctx
->control_fd
,
1743 dhcpcd_readdump1
, ctx
);
1747 dhcpcd_readdumptimeout(void *arg
)
1749 struct dhcpcd_ctx
*ctx
= arg
;
1752 eloop_exit(ctx
->eloop
, EXIT_FAILURE
);
1756 dhcpcd_readdump(struct dhcpcd_ctx
*ctx
)
1759 ctx
->options
|= DHCPCD_FORKED
;
1760 if (eloop_timeout_add_sec(ctx
->eloop
, 5,
1761 dhcpcd_readdumptimeout
, ctx
) == -1)
1763 return eloop_event_add(ctx
->eloop
, ctx
->control_fd
,
1764 dhcpcd_readdump0
, ctx
);
1768 dhcpcd_fork_cb(void *arg
)
1770 struct dhcpcd_ctx
*ctx
= arg
;
1774 len
= read(ctx
->fork_fd
, &exit_code
, sizeof(exit_code
));
1777 exit_code
= EXIT_FAILURE
;
1778 } else if ((size_t)len
< sizeof(exit_code
)) {
1779 logerrx("%s: truncated read %zd (expected %zu)",
1780 __func__
, len
, sizeof(exit_code
));
1781 exit_code
= EXIT_FAILURE
;
1783 eloop_exit(ctx
->eloop
, exit_code
);
1787 dhcpcd_stderr_cb(void *arg
)
1789 struct dhcpcd_ctx
*ctx
= arg
;
1793 len
= read(ctx
->stderr_fd
, log
, sizeof(log
));
1795 if (errno
!= ECONNRESET
)
1801 fprintf(stderr
, "%s", log
);
1805 main(int argc
, char **argv
)
1807 struct dhcpcd_ctx ctx
;
1808 struct ifaddrs
*ifaddrs
= NULL
;
1809 struct if_options
*ifo
;
1810 struct interface
*ifp
;
1811 sa_family_t family
= AF_UNSPEC
;
1813 unsigned int logopts
, t
;
1815 #if defined(USE_SIGNALS) || !defined(THERE_IS_NO_FORK)
1817 int fork_fd
[2], stderr_fd
[2];
1821 const char *siga
= NULL
;
1825 /* Test for --help and --version */
1827 if (strcmp(argv
[1], "--help") == 0) {
1829 return EXIT_SUCCESS
;
1830 } else if (strcmp(argv
[1], "--version") == 0) {
1831 printf(""PACKAGE
" "VERSION
"\n%s\n", dhcpcd_copyright
);
1832 printf("Compiled in features:"
1858 return EXIT_SUCCESS
;
1862 memset(&ctx
, 0, sizeof(ctx
));
1865 ctx
.cffile
= CONFIG
;
1866 ctx
.script
= UNCONST(dhcpcd_default_script
);
1867 ctx
.control_fd
= ctx
.control_unpriv_fd
= ctx
.link_fd
= -1;
1868 ctx
.pf_inet_fd
= -1;
1870 ctx
.pf_link_fd
= -1;
1873 TAILQ_INIT(&ctx
.control_fds
);
1884 #if defined(INET6) && !defined(__sun)
1892 ctx
.ps_root_fd
= ctx
.ps_data_fd
= -1;
1893 ctx
.ps_inet_fd
= ctx
.ps_control_fd
= -1;
1894 TAILQ_INIT(&ctx
.ps_processes
);
1898 logopts
= LOGERR_ERR
|LOGERR_LOG
|LOGERR_LOG_DATE
|LOGERR_LOG_PID
;
1900 while ((opt
= getopt_long(argc
, argv
,
1901 ctx
.options
& DHCPCD_PRINT_PIDFILE
? NOERR_IF_OPTS
: IF_OPTS
,
1902 cf_options
, &oi
)) != -1)
1912 ctx
.cffile
= optarg
;
1916 ctx
.logfile
= strdup(optarg
);
1929 /* Force going via command socket as we're
1930 * out of user definable signals. */
1934 /* -qq disables console output entirely.
1935 * This is important for systemd because it logs
1936 * both console AND syslog to the same log
1937 * resulting in untold confusion. */
1938 if (logopts
& LOGERR_QUIET
)
1939 logopts
&= ~LOGERR_ERR
;
1941 logopts
|= LOGERR_QUIET
;
1953 ctx
.options
|= DHCPCD_PRINT_PIDFILE
;
1954 logopts
&= ~(LOGERR_LOG
| LOGERR_ERR
);
1958 logopts
&= ~LOGERR_LOG
;
1967 if (ctx
.options
& DHCPCD_PRINT_PIDFILE
)
1974 logsetopts(logopts
);
1975 logopen(ctx
.logfile
);
1979 ctx
.ifc
= argc
- optind
;
1980 ctx
.ifv
= argv
+ optind
;
1982 ifo
= read_config(&ctx
, NULL
, NULL
, NULL
);
1984 if (ctx
.options
& DHCPCD_PRINT_PIDFILE
)
1988 opt
= add_options(&ctx
, NULL
, ifo
, argc
, argv
);
1990 if (ctx
.options
& DHCPCD_PRINT_PIDFILE
)
1997 printf("Interface options:\n");
1998 if (optind
== argc
- 1) {
1999 free_options(&ctx
, ifo
);
2000 ifo
= read_config(&ctx
, argv
[optind
], NULL
, NULL
);
2003 add_options(&ctx
, NULL
, ifo
, argc
, argv
);
2007 if (family
== 0 || family
== AF_INET
) {
2008 printf("\nDHCPv4 options:\n");
2009 dhcp_printoptions(&ctx
,
2010 ifo
->dhcp_override
, ifo
->dhcp_override_len
);
2014 if (family
== 0 || family
== AF_INET6
) {
2015 printf("\nND options:\n");
2016 ipv6nd_printoptions(&ctx
,
2017 ifo
->nd_override
, ifo
->nd_override_len
);
2019 printf("\nDHCPv6 options:\n");
2020 dhcp6_printoptions(&ctx
,
2021 ifo
->dhcp6_override
, ifo
->dhcp6_override_len
);
2027 ctx
.options
|= ifo
->options
;
2028 if (i
== 1 || i
== 3) {
2030 ctx
.options
|= DHCPCD_TEST
;
2032 ctx
.options
|= DHCPCD_DUMPLEASE
;
2033 ctx
.options
|= DHCPCD_PERSISTENT
;
2034 ctx
.options
&= ~DHCPCD_DAEMONISE
;
2037 #ifdef THERE_IS_NO_FORK
2038 ctx
.options
&= ~DHCPCD_DAEMONISE
;
2041 if (ctx
.options
& DHCPCD_DEBUG
)
2042 logsetopts(logopts
| LOGERR_DEBUG
);
2044 if (!(ctx
.options
& (DHCPCD_TEST
| DHCPCD_DUMPLEASE
))) {
2046 /* If we have any other args, we should run as a single dhcpcd
2047 * instance for that interface. */
2048 if (optind
== argc
- 1 && !(ctx
.options
& DHCPCD_MASTER
)) {
2053 if (ifname
== NULL
|| strlen(ifname
) > IF_NAMESIZE
) {
2054 errno
= ifname
== NULL
? EINVAL
: E2BIG
;
2055 logerr("%s: ", ifname
);
2058 /* Allow a dhcpcd interface per address family */
2069 snprintf(ctx
.pidfile
, sizeof(ctx
.pidfile
),
2070 PIDFILE
, ifname
, per
, ".");
2072 snprintf(ctx
.pidfile
, sizeof(ctx
.pidfile
),
2073 PIDFILE
, "", "", "");
2074 ctx
.options
|= DHCPCD_MASTER
;
2076 if (ctx
.options
& DHCPCD_PRINT_PIDFILE
) {
2077 printf("%s\n", ctx
.pidfile
);
2082 if (chdir("/") == -1)
2083 logerr("%s: chdir `/'", __func__
);
2085 /* Freeing allocated addresses from dumping leases can trigger
2086 * eloop removals as well, so init here. */
2087 if ((ctx
.eloop
= eloop_new()) == NULL
) {
2088 logerr("%s: eloop_init", __func__
);
2093 for (si
= 0; si
< dhcpcd_signals_ignore_len
; si
++)
2094 signal(dhcpcd_signals_ignore
[si
], SIG_IGN
);
2096 /* Save signal mask, block and redirect signals to our handler */
2097 eloop_signal_set_cb(ctx
.eloop
,
2098 dhcpcd_signals
, dhcpcd_signals_len
,
2099 dhcpcd_signal_cb
, &ctx
);
2100 if (eloop_signal_mask(ctx
.eloop
, &ctx
.sigset
) == -1) {
2101 logerr("%s: eloop_signal_mask", __func__
);
2106 pid
= pidfile_read(ctx
.pidfile
);
2107 if (pid
!= 0 && pid
!= -1)
2108 loginfox("sending signal %s to pid %d", siga
, pid
);
2109 if (pid
== 0 || pid
== -1 || kill(pid
, sig
) != 0) {
2110 if (sig
!= SIGHUP
&& sig
!= SIGUSR1
&& errno
!= EPERM
)
2111 logerrx(PACKAGE
" not running");
2112 if (pid
!= 0 && pid
!= -1 && errno
!= ESRCH
) {
2116 unlink(ctx
.pidfile
);
2117 if (sig
!= SIGHUP
&& sig
!= SIGUSR1
)
2122 if (sig
== SIGHUP
|| sig
== SIGUSR1
)
2124 /* Spin until it exits */
2125 loginfox("waiting for pid %d to exit", pid
);
2127 ts
.tv_nsec
= 100000000; /* 10th of a second */
2128 for(i
= 0; i
< 100; i
++) {
2129 nanosleep(&ts
, NULL
);
2130 if (pidfile_read(ctx
.pidfile
) == -1)
2133 logerrx("pid %d failed to exit", pid
);
2144 if (ctx
.options
& DHCPCD_DUMPLEASE
&&
2145 ioctl(fileno(stdin
), FIONREAD
, &i
, sizeof(i
)) == 0 &&
2148 ctx
.options
|= DHCPCD_FORKED
; /* pretend child process */
2150 if (IN_PRIVSEP(&ctx
) && ps_mastersandbox(&ctx
) == -1)
2153 ifp
= calloc(1, sizeof(*ifp
));
2163 if (dhcp_dump(ifp
) == -1)
2167 logerrx("No DHCP support");
2172 if (dhcp6_dump(ifp
) == -1)
2176 logerrx("No DHCP6 support");
2180 logerrx("Family not specified. Please use -4 or -6.");
2187 /* Test against siga instead of sig to avoid gcc
2188 * warning about a bogus potential signed overflow.
2189 * The end result will be the same. */
2190 if ((siga
== NULL
|| i
== 4 || ctx
.ifc
!= 0) &&
2191 !(ctx
.options
& DHCPCD_TEST
))
2193 ctx
.options
|= DHCPCD_FORKED
; /* avoid socket unlink */
2194 if (!(ctx
.options
& DHCPCD_MASTER
))
2195 ctx
.control_fd
= control_open(argv
[optind
], family
,
2196 ctx
.options
& DHCPCD_DUMPLEASE
);
2197 if (ctx
.control_fd
== -1)
2198 ctx
.control_fd
= control_open(NULL
, AF_UNSPEC
,
2199 ctx
.options
& DHCPCD_DUMPLEASE
);
2200 if (ctx
.control_fd
!= -1) {
2202 ctx
.options
&= ~DHCPCD_FORKED
;
2203 if (IN_PRIVSEP(&ctx
) && ps_mastersandbox(&ctx
) == -1) {
2204 ctx
.options
|= DHCPCD_FORKED
;
2207 ctx
.options
|= DHCPCD_FORKED
;
2209 if (!(ctx
.options
& DHCPCD_DUMPLEASE
))
2210 loginfox("sending commands to dhcpcd process");
2211 len
= control_send(&ctx
, argc
, argv
);
2213 logdebugx("send OK");
2215 logerr("%s: control_send", __func__
);
2218 if (ctx
.options
& DHCPCD_DUMPLEASE
) {
2219 if (dhcpcd_readdump(&ctx
) == -1) {
2220 logerr("%s: dhcpcd_readdump", __func__
);
2227 if (errno
!= ENOENT
)
2228 logerr("%s: control_open", __func__
);
2229 if (ctx
.options
& DHCPCD_DUMPLEASE
) {
2230 if (errno
== ENOENT
)
2231 logerrx("dhcpcd is not running");
2234 if (errno
== EPERM
|| errno
== EACCES
)
2237 ctx
.options
&= ~DHCPCD_FORKED
;
2240 if (!(ctx
.options
& DHCPCD_TEST
)) {
2241 /* Ensure we have the needed directories */
2242 if (mkdir(DBDIR
, 0750) == -1 && errno
!= EEXIST
)
2243 logerr("%s: mkdir `%s'", __func__
, DBDIR
);
2244 if (mkdir(RUNDIR
, 0755) == -1 && errno
!= EEXIST
)
2245 logerr("%s: mkdir `%s'", __func__
, RUNDIR
);
2246 if ((pid
= pidfile_lock(ctx
.pidfile
)) != 0) {
2248 logerr("%s: pidfile_lock: %s",
2249 __func__
, ctx
.pidfile
);
2252 " already running on pid %d (%s)",
2258 loginfox(PACKAGE
"-" VERSION
" starting");
2259 if (freopen(_PATH_DEVNULL
, "r", stdin
) == NULL
)
2260 logerr("%s: freopen stdin", __func__
);
2262 #if defined(USE_SIGNALS) && !defined(THERE_IS_NO_FORK)
2263 if (xsocketpair(AF_UNIX
, SOCK_DGRAM
| SOCK_CXNB
, 0, fork_fd
) == -1 ||
2264 xsocketpair(AF_UNIX
, SOCK_DGRAM
| SOCK_CXNB
, 0, stderr_fd
) == -1)
2266 logerr("socketpair");
2269 switch (pid
= fork()) {
2274 ctx
.fork_fd
= fork_fd
[1];
2276 #ifdef PRIVSEP_RIGHTS
2277 if (ps_rights_limit_fd(fork_fd
[1]) == -1) {
2278 logerr("ps_rights_limit_fdpair");
2283 * Redirect stderr to the stderr socketpair.
2284 * Redirect stdout as well.
2285 * dhcpcd doesn't output via stdout, but something in
2286 * a called script might.
2288 * Do NOT rights limit this fd as it will affect scripts.
2289 * For example, cmp reports insufficient caps on FreeBSD.
2291 if (dup2(stderr_fd
[1], STDERR_FILENO
) == -1 ||
2292 dup2(stderr_fd
[1], STDOUT_FILENO
) == -1)
2294 close(stderr_fd
[0]);
2295 close(stderr_fd
[1]);
2296 if (setsid() == -1) {
2297 logerr("%s: setsid", __func__
);
2300 /* Ensure we can never get a controlling terminal */
2301 switch (pid
= fork()) {
2308 ctx
.options
|= DHCPCD_FORKED
; /* A lie */
2314 ctx
.options
|= DHCPCD_FORKED
; /* A lie */
2315 ctx
.fork_fd
= fork_fd
[0];
2317 ctx
.stderr_fd
= stderr_fd
[0];
2318 close(stderr_fd
[1]);
2319 #ifdef PRIVSEP_RIGHTS
2320 if (ps_rights_limit_fd(fork_fd
[0]) == -1 ||
2321 ps_rights_limit_fd(stderr_fd
[0]) == 1)
2323 logerr("ps_rights_limit_fdpair");
2327 setproctitle("[launcher]");
2328 eloop_event_add(ctx
.eloop
, ctx
.fork_fd
, dhcpcd_fork_cb
, &ctx
);
2329 eloop_event_add(ctx
.eloop
, ctx
.stderr_fd
, dhcpcd_stderr_cb
,
2334 /* We have now forked, setsid, forked once more.
2335 * From this point on, we are the controlling daemon. */
2336 ctx
.options
|= DHCPCD_STARTED
;
2337 if ((pid
= pidfile_lock(ctx
.pidfile
)) != 0) {
2338 logerr("%s: pidfile_lock %d", __func__
, pid
);
2343 #if defined(BSD) && defined(INET6)
2344 /* Disable the kernel RTADV sysctl as early as possible. */
2345 if (ctx
.options
& DHCPCD_IPV6
&& ctx
.options
& DHCPCD_IPV6RS
)
2350 if (IN_PRIVSEP(&ctx
) && ps_start(&ctx
) == -1) {
2354 if (ctx
.options
& DHCPCD_FORKED
)
2358 if (!(ctx
.options
& DHCPCD_TEST
)) {
2359 if (control_start(&ctx
,
2360 ctx
.options
& DHCPCD_MASTER
?
2361 NULL
: argv
[optind
], family
) == -1)
2363 logerr("%s: control_start", __func__
);
2369 /* Start any dev listening plugin which may want to
2370 * change the interface name provided by the kernel */
2371 if (!IN_PRIVSEP(&ctx
) &&
2372 (ctx
.options
& (DHCPCD_MASTER
| DHCPCD_DEV
)) ==
2373 (DHCPCD_MASTER
| DHCPCD_DEV
))
2374 dev_start(&ctx
, dhcpcd_handleinterface
);
2377 setproctitle("%s%s%s",
2378 ctx
.options
& DHCPCD_MASTER
? "[master]" : argv
[optind
],
2379 ctx
.options
& DHCPCD_IPV4
? " [ip4]" : "",
2380 ctx
.options
& DHCPCD_IPV6
? " [ip6]" : "");
2382 if (if_opensockets(&ctx
) == -1) {
2383 logerr("%s: if_opensockets", __func__
);
2387 dhcpcd_setlinkrcvbuf(&ctx
);
2390 /* Try and create DUID from the machine UUID. */
2391 dhcpcd_initduid(&ctx
, NULL
);
2393 /* Cache the default vendor option. */
2394 if (dhcp_vendor(ctx
.vendor
, sizeof(ctx
.vendor
)) == -1)
2395 logerr("dhcp_vendor");
2397 /* Start handling kernel messages for interfaces, addresses and
2399 eloop_event_add(ctx
.eloop
, ctx
.link_fd
, dhcpcd_handlelink
, &ctx
);
2402 if (IN_PRIVSEP(&ctx
) && ps_mastersandbox(&ctx
) == -1)
2406 /* When running dhcpcd against a single interface, we need to retain
2407 * the old behaviour of waiting for an IP address */
2408 if (ctx
.ifc
== 1 && !(ctx
.options
& DHCPCD_BACKGROUND
))
2409 ctx
.options
|= DHCPCD_WAITIP
;
2411 ctx
.ifaces
= if_discover(&ctx
, &ifaddrs
, ctx
.ifc
, ctx
.ifv
);
2412 if (ctx
.ifaces
== NULL
) {
2413 logerr("%s: if_discover", __func__
);
2416 for (i
= 0; i
< ctx
.ifc
; i
++) {
2417 if ((ifp
= if_find(ctx
.ifaces
, ctx
.ifv
[i
])) == NULL
)
2418 logerrx("%s: interface not found",
2420 else if (!ifp
->active
)
2421 logerrx("%s: interface has an invalid configuration",
2424 TAILQ_FOREACH(ifp
, ctx
.ifaces
, next
) {
2425 if (ifp
->active
== IF_ACTIVE_USER
)
2432 loglevel
= ctx
.options
& DHCPCD_INACTIVE
?
2433 LOG_DEBUG
: LOG_ERR
;
2434 logmessage(loglevel
, "no valid interfaces found");
2435 dhcpcd_daemonise(&ctx
);
2438 if (!(ctx
.options
& DHCPCD_LINK
)) {
2439 logerrx("aborting as link detection is disabled");
2444 TAILQ_FOREACH(ifp
, ctx
.ifaces
, next
) {
2446 dhcpcd_initstate1(ifp
, argc
, argv
, 0);
2448 if_learnaddrs(&ctx
, ctx
.ifaces
, &ifaddrs
);
2450 if (ctx
.options
& DHCPCD_BACKGROUND
)
2451 dhcpcd_daemonise(&ctx
);
2454 TAILQ_FOREACH(ifp
, ctx
.ifaces
, next
) {
2457 if (!(ifp
->options
->options
& DHCPCD_LINK
) ||
2458 ifp
->carrier
!= LINK_DOWN
)
2463 if (!(ctx
.options
& DHCPCD_BACKGROUND
)) {
2464 if (ctx
.options
& DHCPCD_MASTER
)
2468 TAILQ_FOREACH(ifp
, ctx
.ifaces
, next
) {
2470 t
= ifp
->options
->timeout
;
2476 ctx
.options
& DHCPCD_LINK
&&
2477 !(ctx
.options
& DHCPCD_WAITIP
))
2481 loglevel
= ctx
.options
& DHCPCD_INACTIVE
?
2482 LOG_DEBUG
: LOG_WARNING
;
2483 logmessage(loglevel
, "no interfaces have a carrier");
2484 dhcpcd_daemonise(&ctx
);
2486 /* Test mode removes the daemonise bit, so check for both */
2487 ctx
.options
& (DHCPCD_DAEMONISE
| DHCPCD_TEST
))
2489 eloop_timeout_add_sec(ctx
.eloop
, t
,
2490 handle_exit_timeout
, &ctx
);
2493 free_options(&ctx
, ifo
);
2496 TAILQ_FOREACH(ifp
, ctx
.ifaces
, next
) {
2498 eloop_timeout_add_sec(ctx
.eloop
, 0,
2499 dhcpcd_prestartinterface
, ifp
);
2503 i
= eloop_start(ctx
.eloop
, &ctx
.sigset
);
2505 logerr("%s: eloop_start", __func__
);
2518 if (control_stop(&ctx
) == -1)
2519 logerr("%s: control_stop", __func__
);
2520 if (ifaddrs
!= NULL
) {
2521 #ifdef PRIVSEP_GETIFADDRS
2522 if (IN_PRIVSEP(&ctx
))
2526 freeifaddrs(ifaddrs
);
2531 /* Free memory and close fd's */
2533 while ((ifp
= TAILQ_FIRST(ctx
.ifaces
))) {
2534 TAILQ_REMOVE(ctx
.ifaces
, ifp
, next
);
2540 free_options(&ctx
, ifo
);
2541 #ifdef HAVE_OPEN_MEMSTREAM
2543 fclose(ctx
.script_fp
);
2545 free(ctx
.script_buf
);
2546 free(ctx
.script_env
);
2549 if (ctx
.link_fd
!= -1) {
2550 eloop_event_delete(ctx
.eloop
, ctx
.link_fd
);
2553 if_closesockets(&ctx
);
2562 eloop_free(ctx
.ps_eloop
);
2564 eloop_free(ctx
.eloop
);
2565 if (ctx
.script
!= dhcpcd_default_script
)
2567 if (ctx
.options
& DHCPCD_STARTED
&& !(ctx
.options
& DHCPCD_FORKED
))
2568 loginfox(PACKAGE
" exited");
2572 #ifdef SETPROCTITLE_H
2573 setproctitle_free();
2576 if (ctx
.options
& DHCPCD_FORKED
)
2577 _exit(i
); /* so atexit won't remove our pidfile */
2578 else if (ctx
.options
& DHCPCD_STARTED
) {
2579 /* Try to detach from the launch process. */
2580 if (ctx
.fork_fd
!= -1 &&
2581 write(ctx
.fork_fd
, &i
, sizeof(i
)) == -1)
2582 logerr("%s: write", __func__
);