1 /* SPDX-License-Identifier: BSD-2-Clause */
3 * dhcpcd - DHCP client daemon
4 * Copyright (c) 2006-2023 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-2023 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>
79 #include <openssl/crypto.h>
86 const int dhcpcd_signals
[] = {
95 const size_t dhcpcd_signals_len
= __arraycount(dhcpcd_signals
);
97 const int dhcpcd_signals_ignore
[] = {
100 const size_t dhcpcd_signals_ignore_len
= __arraycount(dhcpcd_signals_ignore
);
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_MANAGER
)) {
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 #ifndef THERE_IS_NO_FORK
334 dhcpcd_daemonised(struct dhcpcd_ctx
*ctx
)
336 unsigned int logopts
= loggetopts();
339 * Stop writing to stderr.
340 * On the happy path, only the manager process writes to stderr,
341 * so this just stops wasting fprintf calls to nowhere.
343 logopts
&= ~LOGERR_ERR
;
347 * We need to do something with stdout/stderr to avoid SIGPIPE.
348 * We know that stdin is already mapped to /dev/null.
349 * TODO: Capture script output and log it to the logfile and/or syslog.
351 dup2(STDIN_FILENO
, STDOUT_FILENO
);
352 dup2(STDIN_FILENO
, STDERR_FILENO
);
354 ctx
->options
|= DHCPCD_DAEMONISED
;
358 /* Returns the pid of the child, otherwise 0. */
360 dhcpcd_daemonise(struct dhcpcd_ctx
*ctx
)
362 #ifdef THERE_IS_NO_FORK
363 eloop_timeout_delete(ctx
->eloop
, handle_exit_timeout
, ctx
);
369 if (ctx
->options
& DHCPCD_DAEMONISE
&&
370 !(ctx
->options
& (DHCPCD_DAEMONISED
| DHCPCD_NOWAITIP
)))
372 if (!dhcpcd_ipwaited(ctx
))
376 if (ctx
->options
& DHCPCD_ONESHOT
) {
377 loginfox("exiting due to oneshot");
378 eloop_exit(ctx
->eloop
, EXIT_SUCCESS
);
382 eloop_timeout_delete(ctx
->eloop
, handle_exit_timeout
, ctx
);
383 if (ctx
->options
& DHCPCD_DAEMONISED
||
384 !(ctx
->options
& DHCPCD_DAEMONISE
))
392 dhcpcd_daemonised(ctx
);
394 eloop_event_delete(ctx
->eloop
, ctx
->fork_fd
);
395 exit_code
= EXIT_SUCCESS
;
396 if (write(ctx
->fork_fd
, &exit_code
, sizeof(exit_code
)) == -1)
404 dhcpcd_drop_af(struct interface
*ifp
, int stop
, int af
)
407 if (af
== AF_UNSPEC
|| af
== AF_INET6
) {
409 dhcp6_drop(ifp
, stop
? NULL
: "EXPIRE6");
417 if (af
== AF_UNSPEC
|| af
== AF_INET
) {
422 dhcp_drop(ifp
, stop
? "STOP" : "EXPIRE");
429 #if !defined(DHCP6) && !defined(DHCP)
435 dhcpcd_drop(struct interface
*ifp
, int stop
)
438 dhcpcd_drop_af(ifp
, stop
, AF_UNSPEC
);
442 stop_interface(struct interface
*ifp
, const char *reason
)
444 struct dhcpcd_ctx
*ctx
;
447 loginfox("%s: removing interface", ifp
->name
);
448 ifp
->options
->options
|= DHCPCD_STOPPING
;
451 script_runreason(ifp
, reason
== NULL
? "STOPPED" : reason
);
453 /* Delete all timeouts for the interfaces */
454 eloop_q_timeout_delete(ctx
->eloop
, ELOOP_QUEUE_ALL
, NULL
, ifp
);
456 /* De-activate the interface */
457 ifp
->active
= IF_INACTIVE
;
458 ifp
->options
->options
&= ~DHCPCD_STOPPING
;
460 if (!(ctx
->options
& (DHCPCD_MANAGER
| DHCPCD_TEST
)))
461 eloop_exit(ctx
->eloop
, EXIT_FAILURE
);
465 configure_interface1(struct interface
*ifp
)
467 struct if_options
*ifo
= ifp
->options
;
469 /* Do any platform specific configuration */
472 /* If we want to release a lease, we can't really persist the
474 if (ifo
->options
& DHCPCD_RELEASE
)
475 ifo
->options
&= ~DHCPCD_PERSISTENT
;
477 if (ifp
->flags
& (IFF_POINTOPOINT
| IFF_LOOPBACK
)) {
478 ifo
->options
&= ~DHCPCD_ARP
;
479 if (!(ifp
->flags
& IFF_MULTICAST
))
480 ifo
->options
&= ~DHCPCD_IPV6RS
;
481 if (!(ifo
->options
& (DHCPCD_INFORM
| DHCPCD_WANTDHCP
)))
482 ifo
->options
|= DHCPCD_STATIC
;
485 if (ifo
->metric
!= -1)
486 ifp
->metric
= (unsigned int)ifo
->metric
;
489 /* We want to setup INET6 on the interface as soon as possible. */
490 if (ifp
->active
== IF_ACTIVE_USER
&&
491 ifo
->options
& DHCPCD_IPV6
&&
492 !(ifp
->ctx
->options
& (DHCPCD_DUMPLEASE
| DHCPCD_TEST
)))
494 /* If not doing any DHCP, disable the RDNSS requirement. */
495 if (!(ifo
->options
& (DHCPCD_DHCP
| DHCPCD_DHCP6
)))
496 ifo
->options
&= ~DHCPCD_IPV6RA_REQRDNSS
;
501 if (!(ifo
->options
& DHCPCD_IAID
)) {
503 * An IAID is for identifying a unqiue interface within
504 * the client. It is 4 bytes long. Working out a default
505 * value is problematic.
507 * Interface name and number are not stable
508 * between different OS's. Some OS's also cannot make
509 * up their mind what the interface should be called
510 * (yes, udev, I'm looking at you).
511 * Also, the name could be longer than 4 bytes.
512 * Also, with pluggable interfaces the name and index
513 * could easily get swapped per actual interface.
515 * The MAC address is 6 bytes long, the final 3
516 * being unique to the manufacturer and the initial 3
517 * being unique to the organisation which makes it.
518 * We could use the last 4 bytes of the MAC address
519 * as the IAID as it's the most stable part given the
520 * above, but equally it's not guaranteed to be
523 * Given the above, and our need to reliably work
524 * between reboots without persitent storage,
525 * generating the IAID from the MAC address is the only
527 * Saying that, if a VLANID has been specified then we
528 * can use that. It's possible that different interfaces
529 * can have the same VLANID, but this is no worse than
530 * generating the IAID from the duplicate MAC address.
532 * dhclient uses the last 4 bytes of the MAC address.
533 * dibbler uses an increamenting counter.
534 * wide-dhcpv6 uses 0 or a configured value.
536 * Windows 7 uses the first 3 bytes of the MAC address
537 * and an unknown byte.
538 * dhcpcd-6.1.0 and earlier used the interface name,
539 * falling back to interface index if name > 4.
541 if (ifp
->vlanid
!= 0) {
544 /* Maximal VLANID is 4095, so prefix with 0xff
545 * so we don't conflict with an interface index. */
546 vlanid
= htonl(ifp
->vlanid
| 0xff000000);
547 memcpy(ifo
->iaid
, &vlanid
, sizeof(vlanid
));
548 } else if (ifo
->options
& DHCPCD_ANONYMOUS
)
549 memset(ifo
->iaid
, 0, sizeof(ifo
->iaid
));
550 else if (ifp
->hwlen
>= sizeof(ifo
->iaid
)) {
552 ifp
->hwaddr
+ ifp
->hwlen
- sizeof(ifo
->iaid
),
557 len
= (uint32_t)strlen(ifp
->name
);
558 if (len
<= sizeof(ifo
->iaid
)) {
559 memcpy(ifo
->iaid
, ifp
->name
, len
);
560 if (len
< sizeof(ifo
->iaid
))
561 memset(ifo
->iaid
+ len
, 0,
562 sizeof(ifo
->iaid
) - len
);
564 /* IAID is the same size as a uint32_t */
565 len
= htonl(ifp
->index
);
566 memcpy(ifo
->iaid
, &len
, sizeof(ifo
->iaid
));
569 ifo
->options
|= DHCPCD_IAID
;
573 if (ifo
->ia_len
== 0 && ifo
->options
& DHCPCD_IPV6
&&
574 ifp
->name
[0] != '\0')
576 ifo
->ia
= malloc(sizeof(*ifo
->ia
));
581 ifo
->ia
->ia_type
= D6_OPTION_IA_NA
;
582 memcpy(ifo
->ia
->iaid
, ifo
->iaid
, sizeof(ifo
->iaid
));
583 memset(&ifo
->ia
->addr
, 0, sizeof(ifo
->ia
->addr
));
586 ifo
->ia
->sla_len
= 0;
592 for (i
= 0; i
< ifo
->ia_len
; i
++) {
593 if (!ifo
->ia
[i
].iaid_set
) {
594 memcpy(&ifo
->ia
[i
].iaid
, ifo
->iaid
,
595 sizeof(ifo
->ia
[i
].iaid
));
596 ifo
->ia
[i
].iaid_set
= 1;
602 /* If root is network mounted, we don't want to kill the connection
603 * if the DHCP server goes the way of the dodo OR dhcpcd is rebooting
604 * and the lease file has expired. */
605 if (is_root_local() == 0)
606 ifo
->options
|= DHCPCD_LASTLEASE_EXTEND
;
610 dhcpcd_selectprofile(struct interface
*ifp
, const char *profile
)
612 struct if_options
*ifo
;
613 char pssid
[PROFILE_LEN
];
618 r
= print_string(pssid
, sizeof(pssid
), OT_ESCSTRING
,
619 ifp
->ssid
, ifp
->ssid_len
);
626 ifo
= read_config(ifp
->ctx
, ifp
->name
, pssid
, profile
);
628 logdebugx("%s: no profile %s", ifp
->name
, profile
);
631 if (profile
!= NULL
) {
632 strlcpy(ifp
->profile
, profile
, sizeof(ifp
->profile
));
633 loginfox("%s: selected profile %s", ifp
->name
, profile
);
635 *ifp
->profile
= '\0';
637 free_options(ifp
->ctx
, ifp
->options
);
640 add_options(ifp
->ctx
, ifp
->name
, ifp
->options
,
641 ifp
->ctx
->argc
, ifp
->ctx
->argv
);
642 configure_interface1(ifp
);
648 configure_interface(struct interface
*ifp
, int argc
, char **argv
,
649 unsigned long long options
)
653 old
= ifp
->options
? ifp
->options
->mtime
: 0;
654 dhcpcd_selectprofile(ifp
, NULL
);
655 if (ifp
->options
== NULL
) {
656 /* dhcpcd cannot continue with this interface. */
657 ifp
->active
= IF_INACTIVE
;
660 add_options(ifp
->ctx
, ifp
->name
, ifp
->options
, argc
, argv
);
661 ifp
->options
->options
|= options
;
662 configure_interface1(ifp
);
664 /* If the mtime has changed drop any old lease */
665 if (old
!= 0 && ifp
->options
->mtime
!= old
) {
666 logwarnx("%s: config file changed, expiring leases",
673 dhcpcd_initstate1(struct interface
*ifp
, int argc
, char **argv
,
674 unsigned long long options
)
676 struct if_options
*ifo
;
678 configure_interface(ifp
, argc
, argv
, options
);
683 ifo
->options
|= options
;
686 if (ifo
->options
& DHCPCD_IPV6
&& ipv6_init(ifp
->ctx
) == -1) {
688 ifo
->options
&= ~DHCPCD_IPV6
;
694 dhcpcd_initstate(struct interface
*ifp
, unsigned long long options
)
697 dhcpcd_initstate1(ifp
, ifp
->ctx
->argc
, ifp
->ctx
->argv
, options
);
701 dhcpcd_reportssid(struct interface
*ifp
)
703 char pssid
[IF_SSIDLEN
* 4];
705 if (print_string(pssid
, sizeof(pssid
), OT_ESCSTRING
,
706 ifp
->ssid
, ifp
->ssid_len
) == -1)
712 loginfox("%s: connected to Access Point: %s", ifp
->name
, pssid
);
716 dhcpcd_nocarrier_roaming(struct interface
*ifp
)
719 loginfox("%s: carrier lost - roaming", ifp
->name
);
731 rt_build(ifp
->ctx
, AF_UNSPEC
);
732 script_runreason(ifp
, "NOCARRIER_ROAMING");
736 dhcpcd_handlecarrier(struct interface
*ifp
, int carrier
, unsigned int flags
)
738 bool was_link_up
= if_is_link_up(ifp
);
739 bool was_roaming
= if_roaming(ifp
);
741 ifp
->carrier
= carrier
;
744 if (!if_is_link_up(ifp
)) {
745 if (!ifp
->active
|| (!was_link_up
&& !was_roaming
))
749 * If the interface is roaming (generally on wireless)
750 * then while we are not up, we are not down either.
751 * Preserve the network state until we either disconnect
754 if (!ifp
->options
->randomise_hwaddr
&& if_roaming(ifp
)) {
755 dhcpcd_nocarrier_roaming(ifp
);
759 loginfox("%s: carrier lost", ifp
->name
);
760 script_runreason(ifp
, "NOCARRIER");
763 if (ifp
->options
->randomise_hwaddr
) {
764 bool is_up
= ifp
->flags
& IFF_UP
;
768 if (if_randomisemac(ifp
) == -1 && errno
!= ENXIO
)
778 * At this point carrier is NOT DOWN and we have IFF_UP.
779 * We should treat LINK_UNKNOWN as up as the driver may not support
780 * link state changes.
781 * The consideration of any other information about carrier should
782 * be handled in the OS specific if_carrier() function.
788 if (carrier
== LINK_UNKNOWN
)
789 loginfox("%s: carrier unknown, assuming up", ifp
->name
);
791 loginfox("%s: carrier acquired", ifp
->name
);
794 #if !defined(__linux__) && !defined(__NetBSD__)
795 /* BSD does not emit RTM_NEWADDR or RTM_CHGADDR when the
796 * hardware address changes so we have to go
797 * through the disovery process to work it out. */
798 dhcpcd_handleinterface(ifp
->ctx
, 0, ifp
->name
);
802 uint8_t ossid
[IF_SSIDLEN
];
805 olen
= ifp
->ssid_len
;
806 memcpy(ossid
, ifp
->ssid
, ifp
->ssid_len
);
809 /* If we changed SSID network, drop leases */
810 if ((ifp
->ssid_len
!= olen
||
811 memcmp(ifp
->ssid
, ossid
, ifp
->ssid_len
)) && ifp
->active
)
813 dhcpcd_reportssid(ifp
);
824 dhcpcd_initstate(ifp
, 0);
825 script_runreason(ifp
, "CARRIER");
828 /* Set any IPv6 Routers we remembered to expire faster than they
829 * would normally as we maybe on a new network. */
830 ipv6nd_startexpire(ifp
);
831 #ifdef IPV6_MANAGETEMPADDR
832 /* RFC4941 Section 3.5 */
833 ipv6_regentempaddrs(ifp
);
837 dhcpcd_startinterface(ifp
);
841 warn_iaid_conflict(struct interface
*ifp
, uint16_t ia_type
, uint8_t *iaid
)
843 struct interface
*ifn
;
849 TAILQ_FOREACH(ifn
, ifp
->ctx
->ifaces
, next
) {
850 if (ifn
== ifp
|| !ifn
->active
)
852 if (ifn
->options
->options
& DHCPCD_ANONYMOUS
)
855 memcmp(ifn
->options
->iaid
, iaid
,
856 sizeof(ifn
->options
->iaid
)) == 0)
859 for (i
= 0; i
< ifn
->options
->ia_len
; i
++) {
860 ia
= &ifn
->options
->ia
[i
];
861 if (ia
->ia_type
== ia_type
&&
862 memcmp(ia
->iaid
, iaid
, sizeof(ia
->iaid
)) == 0)
868 /* This is only a problem if the interfaces are on the same network. */
870 logerrx("%s: IAID conflicts with one assigned to %s",
871 ifp
->name
, ifn
->name
);
875 dhcpcd_initduid(struct dhcpcd_ctx
*ctx
, struct interface
*ifp
)
877 char buf
[DUID_LEN
* 3];
879 if (ctx
->duid
!= NULL
) {
886 if (ctx
->duid
== NULL
)
891 hwaddr_ntoa(ctx
->duid
, ctx
->duid_len
, buf
, sizeof(buf
)));
895 dhcpcd_startinterface(void *arg
)
897 struct interface
*ifp
= arg
;
898 struct if_options
*ifo
= ifp
->options
;
900 if (ifo
->options
& DHCPCD_LINK
&& !if_is_link_up(ifp
)) {
901 loginfox("%s: waiting for carrier", ifp
->name
);
905 if (ifo
->options
& (DHCPCD_DUID
| DHCPCD_IPV6
) &&
906 !(ifo
->options
& DHCPCD_ANONYMOUS
))
908 char buf
[sizeof(ifo
->iaid
) * 3];
914 /* Try and init DUID from the interface hardware address */
915 dhcpcd_initduid(ifp
->ctx
, ifp
);
918 loginfox("%s: IAID %s", ifp
->name
,
919 hwaddr_ntoa(ifo
->iaid
, sizeof(ifo
->iaid
),
921 warn_iaid_conflict(ifp
, 0, ifo
->iaid
);
924 for (i
= 0; i
< ifo
->ia_len
; i
++) {
926 if (memcmp(ifo
->iaid
, ia
->iaid
, sizeof(ifo
->iaid
))) {
927 loginfox("%s: IA type %u IAID %s",
928 ifp
->name
, ia
->ia_type
,
929 hwaddr_ntoa(ia
->iaid
, sizeof(ia
->iaid
),
931 warn_iaid_conflict(ifp
, ia
->ia_type
, ia
->iaid
);
938 if (ifo
->options
& DHCPCD_IPV6
&& ipv6_start(ifp
) == -1) {
939 logerr("%s: ipv6_start", ifp
->name
);
940 ifo
->options
&= ~DHCPCD_IPV6
;
943 if (ifo
->options
& DHCPCD_IPV6
) {
944 if (ifp
->active
== IF_ACTIVE_USER
) {
945 ipv6_startstatic(ifp
);
947 if (ifo
->options
& DHCPCD_IPV6RS
)
952 /* DHCPv6 could be turned off, but the interface
953 * is still delegated to. */
955 dhcp6_find_delegates(ifp
);
957 if (ifo
->options
& DHCPCD_DHCP6
) {
958 if (ifp
->active
== IF_ACTIVE_USER
) {
961 if (ifo
->options
& DHCPCD_IA_FORCED
)
962 d6_state
= DH6S_INIT
;
963 else if (ifo
->options
& DHCPCD_INFORM6
)
964 d6_state
= DH6S_INFORM
;
966 d6_state
= DH6S_CONFIRM
;
967 if (dhcp6_start(ifp
, d6_state
) == -1)
968 logerr("%s: dhcp6_start", ifp
->name
);
976 if (ifo
->options
& DHCPCD_IPV4
&& ifp
->active
== IF_ACTIVE_USER
) {
977 /* Ensure we have an IPv4 state before starting DHCP */
978 if (ipv4_getstate(ifp
) != NULL
)
985 dhcpcd_prestartinterface(void *arg
)
987 struct interface
*ifp
= arg
;
988 struct dhcpcd_ctx
*ctx
= ifp
->ctx
;
991 if (ifp
->carrier
<= LINK_DOWN
&&
992 ifp
->options
->randomise_hwaddr
&&
998 randmac_down
= false;
1000 if ((!(ctx
->options
& DHCPCD_MANAGER
) ||
1001 ifp
->options
->options
& DHCPCD_IF_UP
|| randmac_down
) &&
1002 !(ifp
->flags
& IFF_UP
))
1004 if (ifp
->options
->randomise_hwaddr
&&
1005 if_randomisemac(ifp
) == -1)
1007 if (if_up(ifp
) == -1)
1011 dhcpcd_startinterface(ifp
);
1015 run_preinit(struct interface
*ifp
)
1018 if (ifp
->ctx
->options
& DHCPCD_TEST
)
1021 script_runreason(ifp
, "PREINIT");
1022 if (ifp
->wireless
&& if_is_link_up(ifp
))
1023 dhcpcd_reportssid(ifp
);
1024 if (ifp
->options
->options
& DHCPCD_LINK
&& ifp
->carrier
!= LINK_UNKNOWN
)
1025 script_runreason(ifp
,
1026 ifp
->carrier
== LINK_UP
? "CARRIER" : "NOCARRIER");
1030 dhcpcd_activateinterface(struct interface
*ifp
, unsigned long long options
)
1036 /* IF_ACTIVE_USER will start protocols when the interface is started.
1037 * IF_ACTIVE will ask the protocols for setup,
1038 * such as any delegated prefixes. */
1039 ifp
->active
= IF_ACTIVE
;
1040 dhcpcd_initstate(ifp
, options
);
1042 /* It's possible we might not have been able to load
1048 dhcpcd_prestartinterface(ifp
);
1052 dhcpcd_handleinterface(void *arg
, int action
, const char *ifname
)
1054 struct dhcpcd_ctx
*ctx
= arg
;
1055 struct ifaddrs
*ifaddrs
;
1056 struct if_head
*ifs
;
1057 struct interface
*ifp
, *iff
;
1058 const char * const argv
[] = { ifname
};
1062 ifp
= if_find(ctx
->ifaces
, ifname
);
1068 logdebugx("%s: interface departed", ifp
->name
);
1069 stop_interface(ifp
, "DEPARTED");
1071 TAILQ_REMOVE(ctx
->ifaces
, ifp
, next
);
1076 ifs
= if_discover(ctx
, &ifaddrs
, -1, UNCONST(argv
));
1082 ifp
= if_find(ifs
, ifname
);
1084 /* This can happen if an interface is quickly added
1085 * and then removed. */
1092 /* Check if we already have the interface */
1093 iff
= if_find(ctx
->ifaces
, ifp
->name
);
1097 logdebugx("%s: interface updated", iff
->name
);
1098 /* The flags and hwaddr could have changed */
1099 iff
->flags
= ifp
->flags
;
1100 iff
->hwlen
= ifp
->hwlen
;
1101 if (ifp
->hwlen
!= 0)
1102 memcpy(iff
->hwaddr
, ifp
->hwaddr
, iff
->hwlen
);
1104 TAILQ_REMOVE(ifs
, ifp
, next
);
1105 TAILQ_INSERT_TAIL(ctx
->ifaces
, ifp
, next
);
1107 logdebugx("%s: interface added", ifp
->name
);
1108 dhcpcd_initstate(ifp
, 0);
1115 if_learnaddrs(ctx
, ifs
, &ifaddrs
);
1117 dhcpcd_prestartinterface(iff
);
1121 /* Free our discovered list */
1122 while ((ifp
= TAILQ_FIRST(ifs
))) {
1123 TAILQ_REMOVE(ifs
, ifp
, next
);
1127 if_freeifaddrs(ctx
, &ifaddrs
);
1133 dhcpcd_handlelink(void *arg
, unsigned short events
)
1135 struct dhcpcd_ctx
*ctx
= arg
;
1137 if (events
!= ELE_READ
)
1138 logerrx("%s: unexpected event 0x%04x", __func__
, events
);
1140 if (if_handlelink(ctx
) == -1) {
1141 if (errno
== ENOBUFS
|| errno
== ENOMEM
) {
1142 dhcpcd_linkoverflow(ctx
);
1145 if (errno
!= ENOTSUP
)
1151 dhcpcd_checkcarrier(void *arg
)
1153 struct interface
*ifp0
= arg
, *ifp
;
1155 ifp
= if_find(ifp0
->ctx
->ifaces
, ifp0
->name
);
1156 if (ifp
== NULL
|| ifp
->carrier
== ifp0
->carrier
)
1159 dhcpcd_handlecarrier(ifp
, ifp0
->carrier
, ifp0
->flags
);
1165 dhcpcd_setlinkrcvbuf(struct dhcpcd_ctx
*ctx
)
1169 if (ctx
->link_rcvbuf
== 0)
1172 logdebugx("setting route socket receive buffer size to %d bytes",
1175 socklen
= sizeof(ctx
->link_rcvbuf
);
1176 if (setsockopt(ctx
->link_fd
, SOL_SOCKET
,
1177 SO_RCVBUF
, &ctx
->link_rcvbuf
, socklen
) == -1)
1183 dhcpcd_runprestartinterface(void *arg
)
1185 struct interface
*ifp
= arg
;
1188 dhcpcd_prestartinterface(ifp
);
1192 dhcpcd_linkoverflow(struct dhcpcd_ctx
*ctx
)
1199 struct if_head
*ifaces
;
1200 struct ifaddrs
*ifaddrs
;
1201 struct interface
*ifp
, *ifn
, *ifp1
;
1203 socklen
= sizeof(rcvbuflen
);
1204 if (getsockopt(ctx
->link_fd
, SOL_SOCKET
,
1205 SO_RCVBUF
, &rcvbuflen
, &socklen
) == -1) {
1206 logerr("%s: getsockopt", __func__
);
1214 logerrx("route socket overflowed (rcvbuflen %d)"
1215 " - learning interface state", rcvbuflen
);
1217 /* Drain the socket.
1218 * We cannot open a new one due to privsep. */
1221 rlen
= read(ctx
->link_fd
, buf
, sizeof(buf
));
1222 if (++rcnt
% 1000 == 0)
1223 logwarnx("drained %zu messages", rcnt
);
1224 } while (rlen
!= -1 || errno
== ENOBUFS
|| errno
== ENOMEM
);
1225 if (rcnt
% 1000 != 0)
1226 logwarnx("drained %zu messages", rcnt
);
1228 /* Work out the current interfaces. */
1229 ifaces
= if_discover(ctx
, &ifaddrs
, ctx
->ifc
, ctx
->ifv
);
1230 if (ifaces
== NULL
) {
1235 /* Punt departed interfaces */
1236 TAILQ_FOREACH_SAFE(ifp
, ctx
->ifaces
, next
, ifn
) {
1237 if (if_find(ifaces
, ifp
->name
) != NULL
)
1239 dhcpcd_handleinterface(ctx
, -1, ifp
->name
);
1242 /* Add new interfaces */
1243 while ((ifp
= TAILQ_FIRST(ifaces
)) != NULL
) {
1244 TAILQ_REMOVE(ifaces
, ifp
, next
);
1245 ifp1
= if_find(ctx
->ifaces
, ifp
->name
);
1247 /* If the interface already exists,
1248 * check carrier state.
1249 * dhcpcd_checkcarrier will free ifp. */
1250 eloop_timeout_add_sec(ctx
->eloop
, 0,
1251 dhcpcd_checkcarrier
, ifp
);
1254 TAILQ_INSERT_TAIL(ctx
->ifaces
, ifp
, next
);
1256 dhcpcd_initstate(ifp
, 0);
1257 eloop_timeout_add_sec(ctx
->eloop
, 0,
1258 dhcpcd_runprestartinterface
, ifp
);
1263 /* Update address state. */
1264 if_markaddrsstale(ctx
->ifaces
);
1265 if_learnaddrs(ctx
, ctx
->ifaces
, &ifaddrs
);
1266 if_deletestaleaddrs(ctx
->ifaces
);
1267 if_freeifaddrs(ctx
, &ifaddrs
);
1271 dhcpcd_handlehwaddr(struct interface
*ifp
,
1272 uint16_t hwtype
, const void *hwaddr
, uint8_t hwlen
)
1274 char buf
[sizeof(ifp
->hwaddr
) * 3];
1276 if (hwaddr
== NULL
|| !if_valid_hwaddr(hwaddr
, hwlen
))
1279 if (hwlen
> sizeof(ifp
->hwaddr
)) {
1281 logerr("%s: %s", __func__
, ifp
->name
);
1285 if (ifp
->hwtype
!= hwtype
) {
1287 loginfox("%s: hardware address type changed"
1288 " from %d to %d", ifp
->name
, ifp
->hwtype
, hwtype
);
1289 ifp
->hwtype
= hwtype
;
1292 if (ifp
->hwlen
== hwlen
&&
1293 (hwlen
== 0 || memcmp(ifp
->hwaddr
, hwaddr
, hwlen
) == 0))
1297 loginfox("%s: old hardware address: %s", ifp
->name
,
1298 hwaddr_ntoa(ifp
->hwaddr
, ifp
->hwlen
, buf
, sizeof(buf
)));
1299 loginfox("%s: new hardware address: %s", ifp
->name
,
1300 hwaddr_ntoa(hwaddr
, hwlen
, buf
, sizeof(buf
)));
1304 memcpy(ifp
->hwaddr
, hwaddr
, hwlen
);
1308 if_reboot(struct interface
*ifp
, int argc
, char **argv
)
1311 unsigned long long oldopts
;
1313 oldopts
= ifp
->options
->options
;
1315 script_runreason(ifp
, "RECONFIGURE");
1316 dhcpcd_initstate1(ifp
, argc
, argv
, 0);
1318 dhcp_reboot_newopts(ifp
, oldopts
);
1323 dhcpcd_prestartinterface(ifp
);
1327 reload_config(struct dhcpcd_ctx
*ctx
)
1329 struct if_options
*ifo
;
1332 if ((ifo
= read_config(ctx
, NULL
, NULL
, NULL
)) == NULL
)
1334 add_options(ctx
, NULL
, ifo
, ctx
->argc
, ctx
->argv
);
1335 /* We need to preserve these options. */
1336 if (ctx
->options
& DHCPCD_STARTED
)
1337 ifo
->options
|= DHCPCD_STARTED
;
1338 if (ctx
->options
& DHCPCD_MANAGER
)
1339 ifo
->options
|= DHCPCD_MANAGER
;
1340 if (ctx
->options
& DHCPCD_DAEMONISED
)
1341 ifo
->options
|= DHCPCD_DAEMONISED
;
1342 if (ctx
->options
& DHCPCD_PRIVSEP
)
1343 ifo
->options
|= DHCPCD_PRIVSEP
;
1344 ctx
->options
= ifo
->options
;
1345 free_options(ctx
, ifo
);
1349 reconf_reboot(struct dhcpcd_ctx
*ctx
, int action
, int argc
, char **argv
, int oi
)
1352 struct interface
*ifp
;
1354 TAILQ_FOREACH(ifp
, ctx
->ifaces
, next
) {
1355 for (i
= oi
; i
< argc
; i
++) {
1356 if (strcmp(ifp
->name
, argv
[i
]) == 0)
1359 if (oi
!= argc
&& i
== argc
)
1361 if (ifp
->active
== IF_ACTIVE_USER
) {
1363 if_reboot(ifp
, argc
, argv
);
1366 ipv4_applyaddr(ifp
);
1368 } else if (i
!= argc
) {
1369 ifp
->active
= IF_ACTIVE_USER
;
1370 dhcpcd_initstate1(ifp
, argc
, argv
, 0);
1372 dhcpcd_prestartinterface(ifp
);
1378 stop_all_interfaces(struct dhcpcd_ctx
*ctx
, unsigned long long opts
)
1380 struct interface
*ifp
;
1382 ctx
->options
|= DHCPCD_EXITING
;
1383 if (ctx
->ifaces
== NULL
)
1386 /* Drop the last interface first */
1387 TAILQ_FOREACH_REVERSE(ifp
, ctx
->ifaces
, if_head
, next
) {
1390 ifp
->options
->options
|= opts
;
1391 if (ifp
->options
->options
& DHCPCD_RELEASE
)
1392 ifp
->options
->options
&= ~DHCPCD_PERSISTENT
;
1393 ifp
->options
->options
|= DHCPCD_EXITING
;
1394 stop_interface(ifp
, NULL
);
1399 dhcpcd_ifrenew(struct interface
*ifp
)
1405 if (ifp
->options
->options
& DHCPCD_LINK
&& !if_is_link_up(ifp
))
1412 #define DHCPCD_RARENEW (DHCPCD_IPV6 | DHCPCD_IPV6RS)
1413 if ((ifp
->options
->options
& DHCPCD_RARENEW
) == DHCPCD_RARENEW
)
1414 ipv6nd_startrs(ifp
);
1422 dhcpcd_renew(struct dhcpcd_ctx
*ctx
)
1424 struct interface
*ifp
;
1426 TAILQ_FOREACH(ifp
, ctx
->ifaces
, next
) {
1427 dhcpcd_ifrenew(ifp
);
1432 #define sigmsg "received %s, %s"
1433 static volatile bool dhcpcd_exiting
= false;
1435 dhcpcd_signal_cb(int sig
, void *arg
)
1437 struct dhcpcd_ctx
*ctx
= arg
;
1438 unsigned long long opts
;
1441 if (ctx
->options
& DHCPCD_DUMPLEASE
) {
1442 eloop_exit(ctx
->eloop
, EXIT_FAILURE
);
1446 if (sig
!= SIGCHLD
&& ctx
->options
& DHCPCD_FORKED
) {
1447 if (sig
!= SIGHUP
&&
1448 write(ctx
->fork_fd
, &sig
, sizeof(sig
)) == -1)
1449 logerr("%s: write", __func__
);
1454 exit_code
= EXIT_FAILURE
;
1457 loginfox(sigmsg
, "SIGINT", "stopping");
1460 loginfox(sigmsg
, "SIGTERM", "stopping");
1461 exit_code
= EXIT_SUCCESS
;
1464 loginfox(sigmsg
, "SIGALRM", "releasing");
1465 opts
|= DHCPCD_RELEASE
;
1466 exit_code
= EXIT_SUCCESS
;
1469 loginfox(sigmsg
, "SIGHUP", "rebinding");
1471 /* Preserve any options passed on the commandline
1472 * when we were started. */
1473 reconf_reboot(ctx
, 1, ctx
->argc
, ctx
->argv
,
1474 ctx
->argc
- ctx
->ifc
);
1477 loginfox(sigmsg
, "SIGUSR1", "renewing");
1481 loginfox(sigmsg
, "SIGUSR2", "reopening log");
1483 if (IN_PRIVSEP(ctx
)) {
1484 if (ps_root_logreopen(ctx
) == -1)
1485 logerr("ps_root_logreopen");
1489 if (logopen(ctx
->logfile
) == -1)
1494 ps_root_signalcb(sig
, ctx
);
1496 while (waitpid(-1, NULL
, WNOHANG
) > 0)
1501 logerrx("received signal %d but don't know what to do with it",
1507 * Privsep has a mini-eloop for reading data from other processes.
1508 * This mini-eloop processes signals as well so we can reap children.
1509 * During teardown we don't want to process SIGTERM or SIGINT again,
1510 * as that could trigger memory issues.
1515 dhcpcd_exiting
= true;
1516 if (!(ctx
->options
& DHCPCD_TEST
))
1517 stop_all_interfaces(ctx
, opts
);
1518 eloop_exit(ctx
->eloop
, exit_code
);
1519 dhcpcd_exiting
= false;
1524 dhcpcd_handleargs(struct dhcpcd_ctx
*ctx
, struct fd_list
*fd
,
1525 int argc
, char **argv
)
1527 struct interface
*ifp
;
1528 struct if_options
*ifo
;
1529 unsigned long long opts
, orig_opts
;
1530 int opt
, oi
, oifind
, do_reboot
, do_renew
, af
= AF_UNSPEC
;
1531 size_t len
, l
, nifaces
;
1534 /* Special commands for our control socket
1535 * as the other end should be blocking until it gets the
1536 * expected reply we should be safely able just to change the
1537 * write callback on the fd */
1538 /* Make any change here in privsep-control.c as well. */
1539 if (strcmp(*argv
, "--version") == 0) {
1540 return control_queue(fd
, UNCONST(VERSION
),
1541 strlen(VERSION
) + 1);
1542 } else if (strcmp(*argv
, "--getconfigfile") == 0) {
1543 return control_queue(fd
, UNCONST(fd
->ctx
->cffile
),
1544 strlen(fd
->ctx
->cffile
) + 1);
1545 } else if (strcmp(*argv
, "--getinterfaces") == 0) {
1548 } else if (strcmp(*argv
, "--listen") == 0) {
1549 fd
->flags
|= FD_LISTEN
;
1553 /* Log the command */
1555 for (opt
= 0; opt
< argc
; opt
++)
1556 len
+= strlen(argv
[opt
]) + 1;
1561 for (opt
= 0; opt
< argc
; opt
++) {
1562 l
= strlen(argv
[opt
]);
1563 strlcpy(p
, argv
[opt
], len
);
1569 loginfox("control command: %s", tmp
);
1575 do_reboot
= do_renew
= 0;
1576 while ((opt
= getopt_long(argc
, argv
, IF_OPTS
, cf_options
, &oi
)) != -1)
1580 /* Assumed if below not set */
1583 opts
|= DHCPCD_RELEASE
;
1589 opts
|= DHCPCD_PERSISTENT
;
1592 opts
|= DHCPCD_EXITING
;
1598 opts
|= DHCPCD_DUMPLEASE
;
1609 /* store the index; the optind will change when a getopt get called */
1612 if (opts
& DHCPCD_DUMPLEASE
) {
1613 ctx
->options
|= DHCPCD_DUMPLEASE
;
1616 TAILQ_FOREACH(ifp
, ctx
->ifaces
, next
) {
1619 for (oi
= oifind
; oi
< argc
; oi
++) {
1620 if (strcmp(ifp
->name
, argv
[oi
]) == 0)
1623 if (oifind
== argc
|| oi
< argc
) {
1624 opt
= send_interface(NULL
, ifp
, af
);
1627 nifaces
+= (size_t)opt
;
1630 if (write(fd
->fd
, &nifaces
, sizeof(nifaces
)) != sizeof(nifaces
))
1632 TAILQ_FOREACH(ifp
, ctx
->ifaces
, next
) {
1635 for (oi
= oifind
; oi
< argc
; oi
++) {
1636 if (strcmp(ifp
->name
, argv
[oi
]) == 0)
1639 if (oifind
== argc
|| oi
< argc
) {
1640 if (send_interface(fd
, ifp
, af
) == -1)
1644 ctx
->options
&= ~DHCPCD_DUMPLEASE
;
1647 ctx
->options
&= ~DHCPCD_DUMPLEASE
;
1651 /* Only privileged users can control dhcpcd via the socket. */
1652 if (fd
->flags
& FD_UNPRIV
) {
1657 if (opts
& (DHCPCD_EXITING
| DHCPCD_RELEASE
)) {
1658 if (oifind
== argc
&& af
== AF_UNSPEC
) {
1659 stop_all_interfaces(ctx
, opts
);
1660 eloop_exit(ctx
->eloop
, EXIT_SUCCESS
);
1664 TAILQ_FOREACH(ifp
, ctx
->ifaces
, next
) {
1667 for (oi
= oifind
; oi
< argc
; oi
++) {
1668 if (strcmp(ifp
->name
, argv
[oi
]) == 0)
1675 orig_opts
= ifo
->options
;
1676 ifo
->options
|= opts
;
1677 if (opts
& DHCPCD_RELEASE
)
1678 ifo
->options
&= ~DHCPCD_PERSISTENT
;
1681 ifo
->options
&= ~DHCPCD_IPV4
;
1684 ifo
->options
&= ~DHCPCD_IPV6
;
1687 if (af
!= AF_UNSPEC
)
1688 dhcpcd_drop_af(ifp
, 1, af
);
1690 stop_interface(ifp
, NULL
);
1691 ifo
->options
= orig_opts
;
1697 if (oifind
== argc
) {
1701 for (oi
= oifind
; oi
< argc
; oi
++) {
1702 if ((ifp
= if_find(ctx
->ifaces
, argv
[oi
])) == NULL
)
1704 dhcpcd_ifrenew(ifp
);
1710 /* XXX: Respect initial commandline options? */
1711 reconf_reboot(ctx
, do_reboot
, argc
, argv
, oifind
);
1715 static void dhcpcd_readdump1(void *, unsigned short);
1718 dhcpcd_readdump2(void *arg
, unsigned short events
)
1720 struct dhcpcd_ctx
*ctx
= arg
;
1722 int exit_code
= EXIT_FAILURE
;
1724 if (events
!= ELE_READ
)
1725 logerrx("%s: unexpected event 0x%04x", __func__
, events
);
1727 len
= read(ctx
->control_fd
, ctx
->ctl_buf
+ ctx
->ctl_bufpos
,
1728 ctx
->ctl_buflen
- ctx
->ctl_bufpos
);
1732 } else if (len
== 0)
1734 if ((size_t)len
+ ctx
->ctl_bufpos
!= ctx
->ctl_buflen
) {
1735 ctx
->ctl_bufpos
+= (size_t)len
;
1739 if (ctx
->ctl_buf
[ctx
->ctl_buflen
- 1] != '\0') /* unlikely */
1740 ctx
->ctl_buf
[ctx
->ctl_buflen
- 1] = '\0';
1741 script_dump(ctx
->ctl_buf
, ctx
->ctl_buflen
);
1743 if (--ctx
->ctl_extra
!= 0) {
1745 if (eloop_event_add(ctx
->eloop
, ctx
->control_fd
, ELE_READ
,
1746 dhcpcd_readdump1
, ctx
) == -1)
1747 logerr("%s: eloop_event_add", __func__
);
1750 exit_code
= EXIT_SUCCESS
;
1753 shutdown(ctx
->control_fd
, SHUT_RDWR
);
1754 eloop_exit(ctx
->eloop
, exit_code
);
1758 dhcpcd_readdump1(void *arg
, unsigned short events
)
1760 struct dhcpcd_ctx
*ctx
= arg
;
1763 if (events
!= ELE_READ
)
1764 logerrx("%s: unexpected event 0x%04x", __func__
, events
);
1766 len
= read(ctx
->control_fd
, &ctx
->ctl_buflen
, sizeof(ctx
->ctl_buflen
));
1767 if (len
!= sizeof(ctx
->ctl_buflen
)) {
1772 if (ctx
->ctl_buflen
> SSIZE_MAX
) {
1778 ctx
->ctl_buf
= malloc(ctx
->ctl_buflen
);
1779 if (ctx
->ctl_buf
== NULL
)
1782 ctx
->ctl_bufpos
= 0;
1783 if (eloop_event_add(ctx
->eloop
, ctx
->control_fd
, ELE_READ
,
1784 dhcpcd_readdump2
, ctx
) == -1)
1785 logerr("%s: eloop_event_add", __func__
);
1790 eloop_exit(ctx
->eloop
, EXIT_FAILURE
);
1794 dhcpcd_readdump0(void *arg
, unsigned short events
)
1796 struct dhcpcd_ctx
*ctx
= arg
;
1799 if (events
!= ELE_READ
)
1800 logerrx("%s: unexpected event 0x%04x", __func__
, events
);
1802 len
= read(ctx
->control_fd
, &ctx
->ctl_extra
, sizeof(ctx
->ctl_extra
));
1803 if (len
!= sizeof(ctx
->ctl_extra
)) {
1807 eloop_exit(ctx
->eloop
, EXIT_FAILURE
);
1811 if (ctx
->ctl_extra
== 0) {
1812 eloop_exit(ctx
->eloop
, EXIT_SUCCESS
);
1816 if (eloop_event_add(ctx
->eloop
, ctx
->control_fd
, ELE_READ
,
1817 dhcpcd_readdump1
, ctx
) == -1)
1818 logerr("%s: eloop_event_add", __func__
);
1822 dhcpcd_readdumptimeout(void *arg
)
1824 struct dhcpcd_ctx
*ctx
= arg
;
1827 eloop_exit(ctx
->eloop
, EXIT_FAILURE
);
1831 dhcpcd_readdump(struct dhcpcd_ctx
*ctx
)
1834 ctx
->options
|= DHCPCD_FORKED
;
1835 if (eloop_timeout_add_sec(ctx
->eloop
, 5,
1836 dhcpcd_readdumptimeout
, ctx
) == -1)
1838 return eloop_event_add(ctx
->eloop
, ctx
->control_fd
, ELE_READ
,
1839 dhcpcd_readdump0
, ctx
);
1843 dhcpcd_fork_cb(void *arg
, unsigned short events
)
1845 struct dhcpcd_ctx
*ctx
= arg
;
1849 if (!(events
& ELE_READ
))
1850 logerrx("%s: unexpected event 0x%04x", __func__
, events
);
1852 len
= read(ctx
->fork_fd
, &exit_code
, sizeof(exit_code
));
1855 eloop_exit(ctx
->eloop
, EXIT_FAILURE
);
1859 if (ctx
->options
& DHCPCD_FORKED
) {
1860 logerrx("%s: dhcpcd manager hungup", __func__
);
1861 eloop_exit(ctx
->eloop
, EXIT_FAILURE
);
1864 eloop_event_delete(ctx
->eloop
, ctx
->fork_fd
);
1865 close(ctx
->fork_fd
);
1870 if ((size_t)len
< sizeof(exit_code
)) {
1871 logerrx("%s: truncated read %zd (expected %zu)",
1872 __func__
, len
, sizeof(exit_code
));
1873 eloop_exit(ctx
->eloop
, EXIT_FAILURE
);
1877 if (ctx
->options
& DHCPCD_FORKED
) {
1878 if (exit_code
== EXIT_SUCCESS
)
1879 logdebugx("forked to background");
1880 eloop_exit(ctx
->eloop
, exit_code
);
1882 dhcpcd_signal_cb(exit_code
, ctx
);
1886 dhcpcd_pidfile_timeout(void *arg
)
1888 struct dhcpcd_ctx
*ctx
= arg
;
1891 pid
= pidfile_read(ctx
->pidfile
);
1894 eloop_exit(ctx
->eloop
, EXIT_SUCCESS
);
1895 else if (++ctx
->duid_len
>= 100) { /* overload duid_len */
1896 logerrx("pid %d failed to exit", pid
);
1897 eloop_exit(ctx
->eloop
, EXIT_FAILURE
);
1899 eloop_timeout_add_msec(ctx
->eloop
, 100,
1900 dhcpcd_pidfile_timeout
, ctx
);
1903 static int dup_null(int fd
)
1905 int fd_null
= open(_PATH_DEVNULL
, O_WRONLY
);
1908 if (fd_null
== -1) {
1909 logwarn("open %s", _PATH_DEVNULL
);
1913 if ((err
= dup2(fd_null
, fd
)) == -1)
1914 logwarn("dup2 %d", fd
);
1920 main(int argc
, char **argv
, char **envp
)
1922 struct dhcpcd_ctx ctx
;
1923 struct ifaddrs
*ifaddrs
= NULL
;
1924 struct if_options
*ifo
;
1925 struct interface
*ifp
;
1926 sa_family_t family
= AF_UNSPEC
;
1928 unsigned int logopts
, t
;
1930 #if defined(USE_SIGNALS) || !defined(THERE_IS_NO_FORK)
1936 const char *siga
= NULL
;
1940 #ifdef SETPROCTITLE_H
1941 setproctitle_init(argc
, argv
, envp
);
1946 /* Test for --help and --version */
1948 if (strcmp(argv
[1], "--help") == 0) {
1950 return EXIT_SUCCESS
;
1951 } else if (strcmp(argv
[1], "--version") == 0) {
1952 printf(""PACKAGE
" "VERSION
"\n%s\n", dhcpcd_copyright
);
1953 printf("Compiled in features:"
1979 return EXIT_SUCCESS
;
1983 memset(&ctx
, 0, sizeof(ctx
));
1984 closefrom(STDERR_FILENO
+ 1);
1987 ctx
.cffile
= CONFIG
;
1988 ctx
.script
= UNCONST(dhcpcd_default_script
);
1989 ctx
.control_fd
= ctx
.control_unpriv_fd
= ctx
.link_fd
= -1;
1990 ctx
.pf_inet_fd
= -1;
1992 ctx
.pf_link_fd
= -1;
1995 TAILQ_INIT(&ctx
.control_fds
);
2006 #if defined(INET6) && !defined(__sun)
2014 ctx
.ps_log_fd
= ctx
.ps_log_root_fd
= -1;
2015 TAILQ_INIT(&ctx
.ps_processes
);
2018 logopts
= LOGERR_LOG
| LOGERR_LOG_DATE
| LOGERR_LOG_PID
;
2020 /* Ensure we have stdin, stdout and stderr file descriptors.
2021 * This is important as we do run scripts which expect these. */
2022 if (fcntl(STDIN_FILENO
, F_GETFD
) == -1)
2023 dup_null(STDIN_FILENO
);
2024 if (fcntl(STDOUT_FILENO
, F_GETFD
) == -1)
2025 dup_null(STDOUT_FILENO
);
2026 if (fcntl(STDERR_FILENO
, F_GETFD
) == -1)
2027 dup_null(STDERR_FILENO
);
2029 logopts
|= LOGERR_ERR
;
2033 while ((opt
= getopt_long(argc
, argv
,
2034 ctx
.options
& DHCPCD_PRINT_PIDFILE
? NOERR_IF_OPTS
: IF_OPTS
,
2035 cf_options
, &oi
)) != -1)
2045 ctx
.cffile
= optarg
;
2049 ctx
.logfile
= strdup(optarg
);
2061 /* -qq disables console output entirely.
2062 * This is important for systemd because it logs
2063 * both console AND syslog to the same log
2064 * resulting in untold confusion. */
2065 if (logopts
& LOGERR_QUIET
)
2066 logopts
&= ~LOGERR_ERR
;
2068 logopts
|= LOGERR_QUIET
;
2080 ctx
.options
|= DHCPCD_PRINT_PIDFILE
;
2081 logopts
&= ~(LOGERR_LOG
| LOGERR_ERR
);
2085 logopts
&= ~LOGERR_LOG
;
2094 if (ctx
.options
& DHCPCD_PRINT_PIDFILE
)
2101 if (optind
!= argc
- 1)
2102 ctx
.options
|= DHCPCD_MANAGER
;
2104 logsetopts(logopts
);
2105 logopen(ctx
.logfile
);
2109 ctx
.ifc
= argc
- optind
;
2110 ctx
.ifv
= argv
+ optind
;
2114 ifo
= read_config(&ctx
, NULL
, NULL
, NULL
);
2116 if (ctx
.options
& DHCPCD_PRINT_PIDFILE
)
2121 opt
= add_options(&ctx
, NULL
, ifo
, argc
, argv
);
2123 if (ctx
.options
& DHCPCD_PRINT_PIDFILE
)
2130 printf("Interface options:\n");
2131 if (optind
== argc
- 1) {
2132 free_options(&ctx
, ifo
);
2133 ifo
= read_config(&ctx
, argv
[optind
], NULL
, NULL
);
2136 add_options(&ctx
, NULL
, ifo
, argc
, argv
);
2140 if (family
== 0 || family
== AF_INET
) {
2141 printf("\nDHCPv4 options:\n");
2142 dhcp_printoptions(&ctx
,
2143 ifo
->dhcp_override
, ifo
->dhcp_override_len
);
2147 if (family
== 0 || family
== AF_INET6
) {
2148 printf("\nND options:\n");
2149 ipv6nd_printoptions(&ctx
,
2150 ifo
->nd_override
, ifo
->nd_override_len
);
2152 printf("\nDHCPv6 options:\n");
2153 dhcp6_printoptions(&ctx
,
2154 ifo
->dhcp6_override
, ifo
->dhcp6_override_len
);
2160 ctx
.options
|= ifo
->options
;
2162 if (i
== 1 || i
== 3) {
2164 ctx
.options
|= DHCPCD_TEST
;
2166 ctx
.options
|= DHCPCD_DUMPLEASE
;
2167 ctx
.options
|= DHCPCD_PERSISTENT
;
2168 ctx
.options
&= ~DHCPCD_DAEMONISE
;
2171 #ifdef THERE_IS_NO_FORK
2172 ctx
.options
&= ~DHCPCD_DAEMONISE
;
2175 if (ctx
.options
& DHCPCD_DEBUG
)
2176 logsetopts(logopts
| LOGERR_DEBUG
);
2178 if (!(ctx
.options
& (DHCPCD_TEST
| DHCPCD_DUMPLEASE
))) {
2180 /* If we have any other args, we should run as a single dhcpcd
2181 * instance for that interface. */
2182 if (optind
== argc
- 1 && !(ctx
.options
& DHCPCD_MANAGER
)) {
2187 if (ifname
== NULL
|| strlen(ifname
) > IF_NAMESIZE
) {
2188 errno
= ifname
== NULL
? EINVAL
: E2BIG
;
2189 logerr("%s: ", ifname
);
2192 /* Allow a dhcpcd interface per address family */
2203 snprintf(ctx
.pidfile
, sizeof(ctx
.pidfile
),
2204 PIDFILE
, ifname
, per
, ".");
2206 snprintf(ctx
.pidfile
, sizeof(ctx
.pidfile
),
2207 PIDFILE
, "", "", "");
2208 ctx
.options
|= DHCPCD_MANAGER
;
2211 * If we are given any interfaces or a family, we
2212 * cannot send a signal as that would impact
2215 if (optind
!= argc
|| family
!= AF_UNSPEC
)
2218 if (ctx
.options
& DHCPCD_PRINT_PIDFILE
) {
2219 printf("%s\n", ctx
.pidfile
);
2224 if (chdir("/") == -1)
2225 logerr("%s: chdir: /", __func__
);
2227 /* Freeing allocated addresses from dumping leases can trigger
2228 * eloop removals as well, so init here. */
2229 if ((ctx
.eloop
= eloop_new()) == NULL
) {
2230 logerr("%s: eloop_init", __func__
);
2235 for (si
= 0; si
< dhcpcd_signals_ignore_len
; si
++)
2236 signal(dhcpcd_signals_ignore
[si
], SIG_IGN
);
2238 /* Save signal mask, block and redirect signals to our handler */
2239 eloop_signal_set_cb(ctx
.eloop
,
2240 dhcpcd_signals
, dhcpcd_signals_len
,
2241 dhcpcd_signal_cb
, &ctx
);
2242 if (eloop_signal_mask(ctx
.eloop
, &ctx
.sigset
) == -1) {
2243 logerr("%s: eloop_signal_mask", __func__
);
2248 pid
= pidfile_read(ctx
.pidfile
);
2249 if (pid
!= 0 && pid
!= -1)
2250 loginfox("sending signal %s to pid %d", siga
, pid
);
2251 if (pid
== 0 || pid
== -1 || kill(pid
, sig
) != 0) {
2252 if (pid
!= 0 && pid
!= -1 && errno
!= ESRCH
) {
2256 unlink(ctx
.pidfile
);
2257 /* We can still continue and send the command
2258 * via the control socket. */
2260 if (sig
== SIGHUP
|| sig
== SIGUSR1
)
2262 /* Spin until it exits */
2263 loginfox("waiting for pid %d to exit", pid
);
2264 dhcpcd_pidfile_timeout(&ctx
);
2271 OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS
|
2272 OPENSSL_INIT_ADD_ALL_DIGESTS
| OPENSSL_INIT_LOAD_CONFIG
, NULL
);
2280 if (ctx
.options
& DHCPCD_DUMPLEASE
&&
2281 ioctl(fileno(stdin
), FIONREAD
, &i
, sizeof(i
)) == 0 &&
2284 ctx
.options
|= DHCPCD_FORKED
; /* pretend child process */
2286 if (IN_PRIVSEP(&ctx
) && ps_managersandbox(&ctx
, NULL
) == -1)
2289 ifp
= calloc(1, sizeof(*ifp
));
2299 if (dhcp_dump(ifp
) == -1)
2303 logerrx("No DHCP support");
2308 if (dhcp6_dump(ifp
) == -1)
2312 logerrx("No DHCP6 support");
2316 logerrx("Family not specified. Please use -4 or -6.");
2323 /* Try and contact the manager process to send the instruction. */
2324 if (!(ctx
.options
& DHCPCD_TEST
)) {
2325 ctx
.options
|= DHCPCD_FORKED
; /* avoid socket unlink */
2326 if (!(ctx
.options
& DHCPCD_MANAGER
))
2327 ctx
.control_fd
= control_open(argv
[optind
], family
,
2328 ctx
.options
& DHCPCD_DUMPLEASE
);
2329 if (!(ctx
.options
& DHCPCD_MANAGER
) && ctx
.control_fd
== -1)
2330 ctx
.control_fd
= control_open(argv
[optind
], AF_UNSPEC
,
2331 ctx
.options
& DHCPCD_DUMPLEASE
);
2332 if (ctx
.control_fd
== -1)
2333 ctx
.control_fd
= control_open(NULL
, AF_UNSPEC
,
2334 ctx
.options
& DHCPCD_DUMPLEASE
);
2335 if (ctx
.control_fd
!= -1) {
2337 if (IN_PRIVSEP(&ctx
) &&
2338 ps_managersandbox(&ctx
, NULL
) == -1)
2341 if (!(ctx
.options
& DHCPCD_DUMPLEASE
))
2342 loginfox("sending commands to dhcpcd process");
2343 len
= control_send(&ctx
, argc
, argv
);
2345 logdebugx("send OK");
2347 logerr("%s: control_send", __func__
);
2350 if (ctx
.options
& DHCPCD_DUMPLEASE
) {
2351 if (dhcpcd_readdump(&ctx
) == -1) {
2352 logerr("%s: dhcpcd_readdump", __func__
);
2359 if (errno
!= ENOENT
)
2360 logerr("%s: control_open", __func__
);
2361 /* If asking dhcpcd to exit and we failed to
2362 * send a signal or a message then we
2363 * don't proceed past here. */
2364 if (ctx
.options
& DHCPCD_DUMPLEASE
||
2365 sig
== SIGTERM
|| sig
== SIGALRM
)
2367 if (errno
== ENOENT
)
2368 logerrx(PACKAGE
" is not running");
2371 if (errno
== EPERM
|| errno
== EACCES
)
2374 ctx
.options
&= ~DHCPCD_FORKED
;
2377 if (!(ctx
.options
& DHCPCD_TEST
)) {
2378 /* Ensure we have the needed directories */
2379 if (mkdir(DBDIR
, 0750) == -1 && errno
!= EEXIST
)
2380 logerr("%s: mkdir: %s", __func__
, DBDIR
);
2381 if (mkdir(RUNDIR
, 0755) == -1 && errno
!= EEXIST
)
2382 logerr("%s: mkdir: %s", __func__
, RUNDIR
);
2383 if ((pid
= pidfile_lock(ctx
.pidfile
)) != 0) {
2385 logerr("%s: pidfile_lock: %s",
2386 __func__
, ctx
.pidfile
);
2389 " already running on pid %d (%s)",
2395 loginfox(PACKAGE
"-" VERSION
" starting");
2397 // We don't need stdin past this point
2398 dup_null(STDIN_FILENO
);
2400 #if defined(USE_SIGNALS) && !defined(THERE_IS_NO_FORK)
2401 if (!(ctx
.options
& DHCPCD_DAEMONISE
))
2404 if (xsocketpair(AF_UNIX
, SOCK_SEQPACKET
|SOCK_CXNB
, 0, fork_fd
) == -1) {
2405 logerr("socketpair");
2408 switch (pid
= fork()) {
2413 ctx
.fork_fd
= fork_fd
[1];
2415 #ifdef PRIVSEP_RIGHTS
2416 if (ps_rights_limit_fd(ctx
.fork_fd
) == -1) {
2417 logerr("ps_rights_limit_fdpair");
2421 if (eloop_event_add(ctx
.eloop
, ctx
.fork_fd
, ELE_READ
,
2422 dhcpcd_fork_cb
, &ctx
) == -1)
2423 logerr("%s: eloop_event_add", __func__
);
2425 if (setsid() == -1) {
2426 logerr("%s: setsid", __func__
);
2429 /* Ensure we can never get a controlling terminal */
2430 switch (pid
= fork()) {
2435 eloop_forked(ctx
.eloop
);
2438 ctx
.options
|= DHCPCD_FORKED
; /* A lie */
2444 setproctitle("[launcher]");
2445 ctx
.options
|= DHCPCD_FORKED
| DHCPCD_LAUNCHER
;
2446 ctx
.fork_fd
= fork_fd
[0];
2448 #ifdef PRIVSEP_RIGHTS
2449 if (ps_rights_limit_fd(ctx
.fork_fd
) == -1) {
2450 logerr("ps_rights_limit_fd");
2454 if (eloop_event_add(ctx
.eloop
, ctx
.fork_fd
, ELE_READ
,
2455 dhcpcd_fork_cb
, &ctx
) == -1)
2456 logerr("%s: eloop_event_add", __func__
);
2459 if (IN_PRIVSEP(&ctx
) && ps_managersandbox(&ctx
, NULL
) == -1)
2466 loginfox("forkfd %d", ctx
.fork_fd
);
2469 /* We have now forked, setsid, forked once more.
2470 * From this point on, we are the controlling daemon. */
2471 logdebugx("spawned manager process on PID %d", getpid());
2474 ctx
.options
|= DHCPCD_STARTED
;
2475 if ((pid
= pidfile_lock(ctx
.pidfile
)) != 0) {
2476 logerr("%s: pidfile_lock %d", __func__
, pid
);
2478 /* privsep has not started ... */
2479 ctx
.options
&= ~DHCPCD_PRIVSEP
;
2487 #if defined(BSD) && defined(INET6)
2488 /* Disable the kernel RTADV sysctl as early as possible. */
2489 if (ctx
.options
& DHCPCD_IPV6
&& ctx
.options
& DHCPCD_IPV6RS
)
2494 if (IN_PRIVSEP(&ctx
) && ps_start(&ctx
) == -1) {
2498 if (ctx
.options
& DHCPCD_FORKED
)
2502 if (!(ctx
.options
& DHCPCD_TEST
)) {
2503 if (control_start(&ctx
,
2504 ctx
.options
& DHCPCD_MANAGER
?
2505 NULL
: argv
[optind
], family
) == -1)
2507 logerr("%s: control_start", __func__
);
2513 /* Start any dev listening plugin which may want to
2514 * change the interface name provided by the kernel */
2515 if (!IN_PRIVSEP(&ctx
) &&
2516 (ctx
.options
& (DHCPCD_MANAGER
| DHCPCD_DEV
)) ==
2517 (DHCPCD_MANAGER
| DHCPCD_DEV
))
2518 dev_start(&ctx
, dhcpcd_handleinterface
);
2521 setproctitle("%s%s%s",
2522 ctx
.options
& DHCPCD_MANAGER
? "[manager]" : argv
[optind
],
2523 ctx
.options
& DHCPCD_IPV4
? " [ip4]" : "",
2524 ctx
.options
& DHCPCD_IPV6
? " [ip6]" : "");
2526 if (if_opensockets(&ctx
) == -1) {
2527 logerr("%s: if_opensockets", __func__
);
2531 dhcpcd_setlinkrcvbuf(&ctx
);
2534 /* Try and create DUID from the machine UUID. */
2535 dhcpcd_initduid(&ctx
, NULL
);
2537 /* Cache the default vendor option. */
2538 if (dhcp_vendor(ctx
.vendor
, sizeof(ctx
.vendor
)) == -1)
2539 logerr("dhcp_vendor");
2541 /* Start handling kernel messages for interfaces, addresses and
2543 if (eloop_event_add(ctx
.eloop
, ctx
.link_fd
, ELE_READ
,
2544 dhcpcd_handlelink
, &ctx
) == -1)
2545 logerr("%s: eloop_event_add", __func__
);
2548 if (IN_PRIVSEP(&ctx
) && ps_managersandbox(&ctx
, "stdio route") == -1)
2552 /* When running dhcpcd against a single interface, we need to retain
2553 * the old behaviour of waiting for an IP address */
2554 if (ctx
.ifc
== 1 && !(ctx
.options
& DHCPCD_BACKGROUND
))
2555 ctx
.options
|= DHCPCD_WAITIP
;
2557 ctx
.ifaces
= if_discover(&ctx
, &ifaddrs
, ctx
.ifc
, ctx
.ifv
);
2558 if (ctx
.ifaces
== NULL
) {
2559 logerr("%s: if_discover", __func__
);
2562 for (i
= 0; i
< ctx
.ifc
; i
++) {
2563 if ((ifp
= if_find(ctx
.ifaces
, ctx
.ifv
[i
])) == NULL
)
2564 logerrx("%s: interface not found",
2566 else if (!ifp
->active
)
2567 logerrx("%s: interface has an invalid configuration",
2570 TAILQ_FOREACH(ifp
, ctx
.ifaces
, next
) {
2571 if (ifp
->active
== IF_ACTIVE_USER
)
2579 loglevel
= ctx
.options
& DHCPCD_INACTIVE
?
2580 LOG_DEBUG
: LOG_ERR
;
2581 logmessage(loglevel
, "no valid interfaces found");
2582 dhcpcd_daemonise(&ctx
);
2585 if (!(ctx
.options
& DHCPCD_LINK
)) {
2586 logerrx("aborting as link detection is disabled");
2591 TAILQ_FOREACH(ifp
, ctx
.ifaces
, next
) {
2593 dhcpcd_initstate1(ifp
, argc
, argv
, 0);
2595 if_learnaddrs(&ctx
, ctx
.ifaces
, &ifaddrs
);
2596 if_freeifaddrs(&ctx
, &ifaddrs
);
2599 if (ctx
.options
& DHCPCD_BACKGROUND
)
2600 dhcpcd_daemonise(&ctx
);
2603 TAILQ_FOREACH(ifp
, ctx
.ifaces
, next
) {
2606 if (if_is_link_up(ifp
))
2611 if (!(ctx
.options
& DHCPCD_BACKGROUND
)) {
2612 if (ctx
.options
& DHCPCD_MANAGER
)
2616 TAILQ_FOREACH(ifp
, ctx
.ifaces
, next
) {
2618 t
= ifp
->options
->timeout
;
2624 ctx
.options
& DHCPCD_LINK
&&
2625 !(ctx
.options
& DHCPCD_WAITIP
))
2629 loglevel
= ctx
.options
& DHCPCD_INACTIVE
?
2630 LOG_DEBUG
: LOG_WARNING
;
2631 logmessage(loglevel
, "no interfaces have a carrier");
2632 dhcpcd_daemonise(&ctx
);
2634 /* Test mode removes the daemonise bit, so check for both */
2635 ctx
.options
& (DHCPCD_DAEMONISE
| DHCPCD_TEST
))
2637 eloop_timeout_add_sec(ctx
.eloop
, t
,
2638 handle_exit_timeout
, &ctx
);
2641 free_options(&ctx
, ifo
);
2644 TAILQ_FOREACH(ifp
, ctx
.ifaces
, next
) {
2646 eloop_timeout_add_sec(ctx
.eloop
, 0,
2647 dhcpcd_prestartinterface
, ifp
);
2651 i
= eloop_start(ctx
.eloop
, &ctx
.sigset
);
2653 logerr("%s: eloop_start", __func__
);
2666 if (!(ctx
.options
& DHCPCD_TEST
) && control_stop(&ctx
) == -1)
2667 logerr("%s: control_stop", __func__
);
2668 if_freeifaddrs(&ctx
, &ifaddrs
);
2672 /* Free memory and close fd's */
2674 while ((ifp
= TAILQ_FIRST(ctx
.ifaces
))) {
2675 TAILQ_REMOVE(ctx
.ifaces
, ifp
, next
);
2681 free_options(&ctx
, ifo
);
2682 #ifdef HAVE_OPEN_MEMSTREAM
2684 fclose(ctx
.script_fp
);
2686 free(ctx
.script_buf
);
2687 free(ctx
.script_env
);
2690 if_closesockets(&ctx
);
2698 if (ctx
.script
!= dhcpcd_default_script
)
2701 if (ps_stopwait(&ctx
) != EXIT_SUCCESS
)
2704 if (ctx
.options
& DHCPCD_STARTED
&& !(ctx
.options
& DHCPCD_FORKED
))
2705 loginfox(PACKAGE
" exited");
2707 if (ps_root_stop(&ctx
) == -1)
2709 eloop_free(ctx
.ps_eloop
);
2713 /* If still attached, detach from the launcher */
2714 if (ctx
.options
& DHCPCD_STARTED
&& ctx
.fork_fd
!= -1) {
2715 if (write(ctx
.fork_fd
, &i
, sizeof(i
)) == -1)
2716 logerr("%s: write", __func__
);
2720 eloop_free(ctx
.eloop
);
2724 #ifdef SETPROCTITLE_H
2725 setproctitle_fini();
2729 if (ctx
.options
& (DHCPCD_FORKED
| DHCPCD_PRIVSEP
))
2730 _exit(i
); /* so atexit won't remove our pidfile */