]>
git.ipfire.org Git - thirdparty/dhcpcd.git/blob - dhcpcd.c
2 * dhcpcd - DHCP client daemon
3 * Copyright (c) 2006-2013 Roy Marples <roy@marples.name>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 const char copyright
[] = "Copyright (c) 2006-2013 Roy Marples";
31 #include <sys/socket.h>
34 #include <sys/types.h>
36 #include <sys/utsname.h>
38 #include <net/route.h> /* For RTM_CHGADDR */
62 #include "if-options.h"
71 struct if_head
*ifaces
= NULL
;
72 char vendor
[VENDORCLASSID_MAX_LEN
];
74 struct if_options
*if_options
= NULL
;
80 sigset_t dhcpcd_sigset
;
81 const int handle_sigs
[] = {
93 static int linkfd
= -1;
105 if ((fp
= fopen(pidfile
, "r")) == NULL
) {
109 if (fscanf(fp
, "%d", &pid
) != 1)
119 printf("usage: "PACKAGE
"\t[-46ABbDdEGgHJKkLnpqTVw]\n"
120 "\t\t[-C, --nohook hook] [-c, --script script]\n"
121 "\t\t[-e, --env value] [-F, --fqdn FQDN] [-f, --config file]\n"
122 "\t\t[-h, --hostname hostname] [-I, --clientid clientid]\n"
123 "\t\t[-i, --vendorclassid vendorclassid] [-l, --leasetime seconds]\n"
124 "\t\t[-m, --metric metric] [-O, --nooption option]\n"
125 "\t\t[-o, --option option] [-Q, --require option]\n"
126 "\t\t[-r, --request address] [-S, --static value]\n"
127 "\t\t[-s, --inform address[/cidr]] [-t, --timeout seconds]\n"
128 "\t\t[-u, --userclass class] [-v, --vendor code, value]\n"
129 "\t\t[-W, --whitelist address[/cidr]] [-y, --reboot seconds]\n"
130 "\t\t[-X, --blacklist address[/cidr]] [-Z, --denyinterfaces pattern]\n"
131 "\t\t[-z, --allowinterfaces pattern] [interface] [...]\n"
132 " "PACKAGE
"\t-k, --release [interface]\n"
133 " "PACKAGE
"\t-U, --dumplease interface\n"
134 " "PACKAGE
"\t--version\n"
135 " "PACKAGE
"\t-x, --exit [interface]\n");
144 for (i
= 0; i
< ifac
; i
++)
147 for (i
= 0; i
< ifdc
; i
++)
152 for (n
= 0; n
< dhcp_eopts_len
; n
++)
153 free_dhcp_opt_embenc(&dhcp_eopts
[n
]);
157 for (n
= 0; n
< dhcp6_eopts_len
; n
++)
158 free_dhcp_opt_embenc(&dhcp6_eopts
[n
]);
167 struct interface
*ifp
;
170 free_options(if_options
);
173 while ((ifp
= TAILQ_FIRST(ifaces
))) {
174 TAILQ_REMOVE(ifaces
, ifp
, next
);
183 if (!(options
& DHCPCD_FORKED
))
188 if (options
& DHCPCD_MASTER
) {
189 if (control_stop() == -1)
190 syslog(LOG_ERR
, "control_stop: %m");
199 if (options
& DHCPCD_STARTED
&& !(options
& DHCPCD_FORKED
))
200 syslog(LOG_INFO
, "exited");
205 handle_exit_timeout(__unused
void *arg
)
209 syslog(LOG_ERR
, "timed out");
210 if (!(options
& DHCPCD_IPV4
) || !(options
& DHCPCD_TIMEOUT_IPV4LL
)) {
211 if (options
& DHCPCD_MASTER
) {
212 /* We've timed out, so remove the waitip requirements.
213 * If the user doesn't like this they can always set
214 * an infinite timeout. */
216 ~(DHCPCD_WAITIP
| DHCPCD_WAITIP4
| DHCPCD_WAITIP6
);
222 options
&= ~DHCPCD_TIMEOUT_IPV4LL
;
223 timeout
= (PROBE_NUM
* PROBE_MAX
) + (PROBE_WAIT
* 2);
224 syslog(LOG_WARNING
, "allowing %d seconds for IPv4LL timeout", timeout
);
225 eloop_timeout_add_sec(timeout
, handle_exit_timeout
, NULL
);
231 #ifdef THERE_IS_NO_FORK
238 if (options
& DHCPCD_DAEMONISE
&& !(options
& DHCPCD_DAEMONISED
)) {
239 if (options
& DHCPCD_WAITIP4
&&
240 !ipv4_addrexists(NULL
))
242 if (options
& DHCPCD_WAITIP6
&&
243 !ipv6nd_addrexists(NULL
) &&
244 !dhcp6_addrexists(NULL
))
247 (DHCPCD_WAITIP
| DHCPCD_WAITIP4
| DHCPCD_WAITIP6
)) ==
249 !ipv4_addrexists(NULL
) &&
250 !ipv6nd_addrexists(NULL
) &&
251 !dhcp6_addrexists(NULL
))
255 eloop_timeout_delete(handle_exit_timeout
, NULL
);
256 if (options
& DHCPCD_DAEMONISED
|| !(options
& DHCPCD_DAEMONISE
))
258 /* Setup a signal pipe so parent knows when to exit. */
259 if (pipe(sidpipe
) == -1) {
260 syslog(LOG_ERR
, "pipe: %m");
263 syslog(LOG_DEBUG
, "forking to background");
264 switch (pid
= fork()) {
266 syslog(LOG_ERR
, "fork: %m");
271 /* Notify parent it's safe to exit as we've detached. */
273 if (write(sidpipe
[1], &buf
, 1) == -1)
274 syslog(LOG_ERR
, "failed to notify parent: %m");
276 if ((fd
= open(_PATH_DEVNULL
, O_RDWR
, 0)) != -1) {
277 dup2(fd
, STDIN_FILENO
);
278 dup2(fd
, STDOUT_FILENO
);
279 dup2(fd
, STDERR_FILENO
);
284 /* Wait for child to detach */
286 if (read(sidpipe
[0], &buf
, 1) == -1)
287 syslog(LOG_ERR
, "failed to read child: %m");
291 /* Done with the fd now */
293 syslog(LOG_INFO
, "forked to background, child pid %d",pid
);
294 writepid(pidfd
, pid
);
297 options
|= DHCPCD_FORKED
;
300 options
|= DHCPCD_DAEMONISED
;
306 find_interface(const char *ifname
)
308 struct interface
*ifp
;
310 TAILQ_FOREACH(ifp
, ifaces
, next
) {
311 if (strcmp(ifp
->name
, ifname
) == 0)
318 stop_interface(struct interface
*ifp
)
321 syslog(LOG_INFO
, "%s: removing interface", ifp
->name
);
322 ifp
->options
->options
|= DHCPCD_STOPPING
;
324 // Remove the interface from our list
325 TAILQ_REMOVE(ifaces
, ifp
, next
);
326 dhcp6_drop(ifp
, NULL
);
328 dhcp_drop(ifp
, "STOP");
330 eloop_timeout_delete(NULL
, ifp
);
331 if (ifp
->options
->options
& DHCPCD_DEPARTED
)
332 script_runreason(ifp
, "DEPARTED");
334 if (!(options
& (DHCPCD_MASTER
| DHCPCD_TEST
)))
339 configure_interface1(struct interface
*ifp
)
341 struct if_options
*ifo
= ifp
->options
;
342 int ra_global
, ra_iface
;
344 /* Do any platform specific configuration */
347 /* If we want to release a lease, we can't really persist the
349 if (ifo
->options
& DHCPCD_RELEASE
)
350 ifo
->options
&= ~DHCPCD_PERSISTENT
;
352 if (ifp
->flags
& IFF_POINTOPOINT
&& !(ifo
->options
& DHCPCD_INFORM
))
353 ifo
->options
|= DHCPCD_STATIC
;
354 if (ifp
->flags
& IFF_NOARP
||
355 ifo
->options
& (DHCPCD_INFORM
| DHCPCD_STATIC
))
356 ifo
->options
&= ~(DHCPCD_ARP
| DHCPCD_IPV4LL
);
357 if (!(ifp
->flags
& (IFF_POINTOPOINT
| IFF_LOOPBACK
| IFF_MULTICAST
)))
358 ifo
->options
&= ~DHCPCD_IPV6RS
;
359 if (ifo
->options
& DHCPCD_LINK
&& carrier_status(ifp
) == LINK_UNKNOWN
)
360 ifo
->options
&= ~DHCPCD_LINK
;
362 if (ifo
->metric
!= -1)
363 ifp
->metric
= ifo
->metric
;
365 if (!(ifo
->options
& DHCPCD_IPV6
))
366 ifo
->options
&= ~DHCPCD_IPV6RS
;
368 /* We want to disable kernel interface RA as early as possible. */
369 if (ifo
->options
& DHCPCD_IPV6RS
) {
370 ra_global
= check_ipv6(NULL
, options
& DHCPCD_IPV6RA_OWN
? 1:0);
371 ra_iface
= check_ipv6(ifp
->name
,
372 ifp
->options
->options
& DHCPCD_IPV6RA_OWN
? 1 : 0);
373 if (ra_global
== -1 || ra_iface
== -1)
374 ifo
->options
&= ~DHCPCD_IPV6RS
;
375 else if (ra_iface
== 0)
376 ifo
->options
|= DHCPCD_IPV6RA_OWN
;
379 /* If we haven't specified a ClientID and our hardware address
380 * length is greater than DHCP_CHADDR_LEN then we enforce a ClientID
381 * of the hardware address family and the hardware address. */
382 if (ifp
->hwlen
> DHCP_CHADDR_LEN
)
383 ifo
->options
|= DHCPCD_CLIENTID
;
385 /* Firewire and InfiniBand interfaces require ClientID and
386 * the broadcast option being set. */
387 switch (ifp
->family
) {
388 case ARPHRD_IEEE1394
: /* FALLTHROUGH */
389 case ARPHRD_INFINIBAND
:
390 ifo
->options
|= DHCPCD_CLIENTID
| DHCPCD_BROADCAST
;
394 if (!(ifo
->options
& DHCPCD_IAID
)) {
396 * An IAID is for identifying a unqiue interface within
397 * the client. It is 4 bytes long. Working out a default
398 * value is problematic.
400 * Interface name and number are not stable
401 * between different OS's. Some OS's also cannot make
402 * up their mind what the interface should be called
403 * (yes, udev, I'm looking at you).
404 * Also, the name could be longer than 4 bytes.
405 * Also, with pluggable interfaces the name and index
406 * could easily get swapped per actual interface.
408 * The MAC address is 6 bytes long, the final 3
409 * being unique to the manufacturer and the initial 3
410 * being unique to the organisation which makes it.
411 * We could use the last 4 bytes of the MAC address
412 * as the IAID as it's the most stable part given the
413 * above, but equally it's not guaranteed to be
416 * Given the above, and our need to reliably work
417 * between reboots without persitent storage,
418 * generating the IAID from the MAC address is the only
421 * dhclient uses the last 4 bytes of the MAC address.
422 * dibbler uses an increamenting counter.
423 * wide-dhcpv6 uses 0 or a configured value.
425 * Windows 7 uses the first 3 bytes of the MAC address
426 * and an unknown byte.
427 * dhcpcd-6.1.0 and earlier used the interface name,
428 * falling back to interface index if name > 4.
430 memcpy(ifo
->iaid
, ifp
->hwaddr
+ ifp
->hwlen
- sizeof(ifo
->iaid
),
433 len
= strlen(ifp
->name
);
434 if (len
<= sizeof(ifo
->iaid
)) {
435 memcpy(ifo
->iaid
, ifp
->name
, len
);
436 memset(ifo
->iaid
+ len
, 0, sizeof(ifo
->iaid
) - len
);
438 /* IAID is the same size as a uint32_t */
439 len
= htonl(ifp
->index
);
440 memcpy(ifo
->iaid
, &len
, sizeof(len
));
443 ifo
->options
|= DHCPCD_IAID
;
447 if (ifo
->ia
== NULL
&& ifo
->options
& DHCPCD_IPV6
) {
448 ifo
->ia
= malloc(sizeof(*ifo
->ia
));
450 syslog(LOG_ERR
, "%s: %m", __func__
);
452 if (ifo
->ia_type
== 0)
453 ifo
->ia_type
= D6_OPTION_IA_NA
;
454 memcpy(ifo
->ia
->iaid
, ifo
->iaid
, sizeof(ifo
->iaid
));
457 ifo
->ia
->sla_len
= 0;
464 select_profile(struct interface
*ifp
, const char *profile
)
466 struct if_options
*ifo
;
470 ifo
= read_config(cffile
, ifp
->name
, ifp
->ssid
, profile
);
472 syslog(LOG_DEBUG
, "%s: no profile %s", ifp
->name
, profile
);
476 if (profile
!= NULL
) {
477 strlcpy(ifp
->profile
, profile
, sizeof(ifp
->profile
));
478 syslog(LOG_INFO
, "%s: selected profile %s",
481 *ifp
->profile
= '\0';
482 free_options(ifp
->options
);
487 configure_interface1(ifp
);
492 configure_interface(struct interface
*ifp
, int argc
, char **argv
)
495 select_profile(ifp
, NULL
);
496 add_options(ifp
->options
, argc
, argv
);
497 configure_interface1(ifp
);
501 handle_carrier(int carrier
, int flags
, const char *ifname
)
503 struct interface
*ifp
;
505 ifp
= find_interface(ifname
);
506 if (ifp
== NULL
|| !(ifp
->options
->options
& DHCPCD_LINK
))
509 if (carrier
== LINK_UNKNOWN
)
510 carrier
= carrier_status(ifp
); /* will set ifp->flags */
514 if (carrier
== LINK_UNKNOWN
)
515 syslog(LOG_ERR
, "%s: carrier_status: %m", ifname
);
516 /* IFF_RUNNING is checked, if needed, earlier and is OS dependant */
517 else if (carrier
== LINK_DOWN
|| (ifp
->flags
& IFF_UP
) == 0) {
518 if (ifp
->carrier
!= LINK_DOWN
) {
519 if (ifp
->carrier
== LINK_UP
)
520 syslog(LOG_INFO
, "%s: carrier lost", ifp
->name
);
521 ifp
->carrier
= LINK_DOWN
;
523 dhcp6_drop(ifp
, "EXPIRE6");
525 /* Don't blindly delete our knowledge of LL addresses.
526 * We need to listen to what the kernel does with
527 * them as some OS's will remove, mark tentative or
529 ipv6_free_ll_callbacks(ifp
);
530 dhcp_drop(ifp
, "NOCARRIER");
532 } else if (carrier
== LINK_UP
&& ifp
->flags
& IFF_UP
) {
533 if (ifp
->carrier
!= LINK_UP
) {
534 syslog(LOG_INFO
, "%s: carrier acquired", ifp
->name
);
535 ifp
->carrier
= LINK_UP
;
536 #if !defined(__linux__) && !defined(__NetBSD__)
537 /* BSD does not emit RTM_NEWADDR or RTM_CHGADDR when the
538 * hardware address changes so we have to go
539 * through the disovery process to work it out. */
540 handle_interface(0, ifp
->name
);
543 getifssid(ifp
->name
, ifp
->ssid
);
544 configure_interface(ifp
, margc
, margv
);
545 script_runreason(ifp
, "CARRIER");
546 start_interface(ifp
);
552 warn_iaid_conflict(struct interface
*ifp
, uint8_t *iaid
)
554 struct interface
*ifn
;
557 TAILQ_FOREACH(ifn
, ifaces
, next
) {
560 if (memcmp(ifn
->options
->iaid
, iaid
,
561 sizeof(ifn
->options
->iaid
)) == 0)
563 for (i
= 0; i
< ifn
->options
->ia_len
; i
++) {
564 if (memcmp(&ifn
->options
->ia
[i
].iaid
, iaid
,
565 sizeof(ifn
->options
->ia
[i
].iaid
)) == 0)
570 /* This is only a problem if the interfaces are on the same network. */
573 "%s: IAID conflicts with one assigned to %s",
574 ifp
->name
, ifn
->name
);
578 start_interface(void *arg
)
580 struct interface
*ifp
= arg
;
581 struct if_options
*ifo
= ifp
->options
;
585 handle_carrier(LINK_UNKNOWN
, 0, ifp
->name
);
586 if (ifp
->carrier
== LINK_DOWN
) {
587 syslog(LOG_INFO
, "%s: waiting for carrier", ifp
->name
);
591 if (ifo
->options
& (DHCPCD_DUID
| DHCPCD_IPV6
)) {
592 /* Report client DUID */
594 if (duid_init(ifp
) == 0)
596 syslog(LOG_INFO
, "DUID %s",
597 hwaddr_ntoa(duid
, duid_len
));
601 syslog(LOG_INFO
, "%s: IAID %s", ifp
->name
,
602 hwaddr_ntoa(ifo
->iaid
, sizeof(ifo
->iaid
)));
603 warn_iaid_conflict(ifp
, ifo
->iaid
);
604 for (i
= 0; i
< ifo
->ia_len
; i
++) {
605 if (memcmp(ifo
->iaid
, ifo
->ia
[i
].iaid
,
608 syslog(LOG_INFO
, "%s: IAID %s", ifp
->name
,
609 hwaddr_ntoa(ifo
->ia
[i
].iaid
,
610 sizeof(ifo
->ia
[i
].iaid
)));
611 warn_iaid_conflict(ifp
, ifo
->ia
[i
].iaid
);
616 if (ifo
->options
& DHCPCD_IPV6
) {
617 if (ifo
->options
& DHCPCD_IPV6RS
&&
618 !(ifo
->options
& DHCPCD_INFORM
))
621 if (!(ifo
->options
& DHCPCD_IPV6RS
)) {
622 if (ifo
->options
& DHCPCD_IA_FORCED
)
623 nolease
= dhcp6_start(ifp
, DH6S_INIT
);
625 nolease
= dhcp6_find_delegates(ifp
);
626 /* Enabling the below doesn't really make
627 * sense as there is currently no standard
628 * to push routes via DHCPv6.
629 * (There is an expired working draft,
631 * You can also get it to work by forcing
632 * an IA as shown above. */
634 /* With no RS or delegates we might
635 * as well try and solicit a DHCPv6 address */
637 nolease
= dhcp6_start(ifp
, DH6S_INIT
);
642 "%s: dhcp6_start: %m", ifp
->name
);
646 if (ifo
->options
& DHCPCD_IPV4
)
652 handle_link(__unused
void *arg
)
655 if (manage_link(linkfd
) == -1 && errno
!= ENXIO
&& errno
!= ENODEV
)
656 syslog(LOG_ERR
, "manage_link: %m");
660 init_state(struct interface
*ifp
, int argc
, char **argv
)
662 struct if_options
*ifo
;
663 const char *reason
= NULL
;
665 configure_interface(ifp
, argc
, argv
);
668 if (ifo
->options
& DHCPCD_IPV4
&& ipv4_init() == -1) {
669 syslog(LOG_ERR
, "ipv4_init: %m");
670 ifo
->options
&= ~DHCPCD_IPV4
;
672 if (ifo
->options
& DHCPCD_IPV6
&& ipv6_init() == -1) {
673 syslog(LOG_ERR
, "ipv6_init: %m");
674 ifo
->options
&= ~DHCPCD_IPV6RS
;
677 if (!(options
& DHCPCD_TEST
))
678 script_runreason(ifp
, "PREINIT");
680 if (ifo
->options
& DHCPCD_LINK
) {
681 switch (carrier_status(ifp
)) {
683 ifp
->carrier
= LINK_DOWN
;
684 reason
= "NOCARRIER";
687 ifp
->carrier
= LINK_UP
;
691 ifp
->carrier
= LINK_UNKNOWN
;
694 if (reason
&& !(options
& DHCPCD_TEST
))
695 script_runreason(ifp
, reason
);
697 ifp
->carrier
= LINK_UNKNOWN
;
701 handle_interface(int action
, const char *ifname
)
704 struct interface
*ifp
, *ifn
, *ifl
= NULL
;
705 const char * const argv
[] = { ifname
};
709 ifp
= find_interface(ifname
);
711 ifp
->options
->options
|= DHCPCD_DEPARTED
;
717 /* If running off an interface list, check it's in it. */
719 for (i
= 0; i
< ifc
; i
++)
720 if (strcmp(ifv
[i
], ifname
) == 0)
726 ifs
= discover_interfaces(-1, UNCONST(argv
));
727 TAILQ_FOREACH_SAFE(ifp
, ifs
, next
, ifn
) {
728 if (strcmp(ifp
->name
, ifname
) != 0)
730 /* Check if we already have the interface */
731 ifl
= find_interface(ifp
->name
);
733 /* The flags and hwaddr could have changed */
734 ifl
->flags
= ifp
->flags
;
735 ifl
->hwlen
= ifp
->hwlen
;
737 memcpy(ifl
->hwaddr
, ifp
->hwaddr
, ifl
->hwlen
);
739 TAILQ_REMOVE(ifs
, ifp
, next
);
740 TAILQ_INSERT_TAIL(ifaces
, ifp
, next
);
743 init_state(ifp
, margc
, margv
);
744 start_interface(ifp
);
748 /* Free our discovered list */
749 while ((ifp
= TAILQ_FIRST(ifs
))) {
750 TAILQ_REMOVE(ifs
, ifp
, next
);
757 handle_hwaddr(const char *ifname
, const uint8_t *hwaddr
, size_t hwlen
)
759 struct interface
*ifp
;
761 ifp
= find_interface(ifname
);
765 if (hwlen
> sizeof(ifp
->hwaddr
)) {
767 syslog(LOG_ERR
, "%s: %s: %m", ifp
->name
, __func__
);
771 if (ifp
->hwlen
== hwlen
&& memcmp(ifp
->hwaddr
, hwaddr
, hwlen
) == 0)
774 syslog(LOG_INFO
, "%s: new hardware address: %s", ifp
->name
,
775 hwaddr_ntoa(hwaddr
, hwlen
));
777 memcpy(ifp
->hwaddr
, hwaddr
, hwlen
);
781 if_reboot(struct interface
*ifp
, int argc
, char **argv
)
785 oldopts
= ifp
->options
->options
;
786 script_runreason(ifp
, "RECONFIGURE");
787 configure_interface(ifp
, argc
, argv
);
788 dhcp_reboot_newopts(ifp
, oldopts
);
790 start_interface(ifp
);
794 reconf_reboot(int action
, int argc
, char **argv
, int oi
)
797 struct interface
*ifn
, *ifp
;
799 ifs
= discover_interfaces(argc
- oi
, argv
+ oi
);
803 while ((ifp
= TAILQ_FIRST(ifs
))) {
804 TAILQ_REMOVE(ifs
, ifp
, next
);
805 ifn
= find_interface(ifp
->name
);
808 if_reboot(ifn
, argc
, argv
);
813 init_state(ifp
, argc
, argv
);
814 TAILQ_INSERT_TAIL(ifaces
, ifp
, next
);
815 start_interface(ifp
);
825 sig_reboot(void *arg
)
827 siginfo_t
*siginfo
= arg
;
828 struct if_options
*ifo
;
830 syslog(LOG_INFO
, "received SIGALRM from PID %d, rebinding",
831 (int)siginfo
->si_pid
);
839 ifo
= read_config(cffile
, NULL
, NULL
, NULL
);
840 add_options(ifo
, margc
, margv
);
841 /* We need to preserve these two options. */
842 if (options
& DHCPCD_MASTER
)
843 ifo
->options
|= DHCPCD_MASTER
;
844 if (options
& DHCPCD_DAEMONISED
)
845 ifo
->options
|= DHCPCD_DAEMONISED
;
846 options
= ifo
->options
;
848 reconf_reboot(1, ifc
, ifv
, 0);
852 sig_reconf(void *arg
)
854 siginfo_t
*siginfo
= arg
;
855 struct interface
*ifp
;
857 syslog(LOG_INFO
, "received SIGUSR from PID %d, reconfiguring",
858 (int)siginfo
->si_pid
);
859 TAILQ_FOREACH(ifp
, ifaces
, next
) {
865 handle_signal(int sig
, siginfo_t
*siginfo
, __unused
void *context
)
867 struct interface
*ifp
;
873 syslog(LOG_INFO
, "received SIGINT from PID %d, stopping",
874 (int)siginfo
->si_pid
);
877 syslog(LOG_INFO
, "received SIGTERM from PID %d, stopping",
878 (int)siginfo
->si_pid
);
881 eloop_timeout_add_now(sig_reboot
, siginfo
);
884 syslog(LOG_INFO
, "received SIGHUP from PID %d, releasing",
885 (int)siginfo
->si_pid
);
889 eloop_timeout_add_now(sig_reconf
, siginfo
);
892 syslog(LOG_WARNING
, "received SIGPIPE");
896 "received signal %d from PID %d, "
897 "but don't know what to do with it",
898 sig
, (int)siginfo
->si_pid
);
902 if (options
& DHCPCD_TEST
)
905 /* As drop_dhcp could re-arrange the order, we do it like this. */
907 /* Be sane and drop the last config first */
908 ifp
= TAILQ_LAST(ifaces
, if_head
);
912 ifp
->options
->options
|= DHCPCD_RELEASE
;
913 ifp
->options
->options
&= ~DHCPCD_PERSISTENT
;
915 ifp
->options
->options
|= DHCPCD_EXITING
;
922 handle_args(struct fd_list
*fd
, int argc
, char **argv
)
924 struct interface
*ifp
;
925 int do_exit
= 0, do_release
= 0, do_reboot
= 0;
933 /* Special commands for our control socket */
934 if (strcmp(*argv
, "--version") == 0) {
935 len
= strlen(VERSION
) + 1;
936 iov
[0].iov_base
= &len
;
937 iov
[0].iov_len
= sizeof(ssize_t
);
938 iov
[1].iov_base
= UNCONST(VERSION
);
939 iov
[1].iov_len
= len
;
940 if (writev(fd
->fd
, iov
, 2) == -1) {
941 syslog(LOG_ERR
, "writev: %m");
945 } else if (strcmp(*argv
, "--getconfigfile") == 0) {
946 len
= strlen(cffile
? cffile
: CONFIG
) + 1;
947 iov
[0].iov_base
= &len
;
948 iov
[0].iov_len
= sizeof(ssize_t
);
949 iov
[1].iov_base
= cffile
? cffile
: UNCONST(CONFIG
);
950 iov
[1].iov_len
= len
;
951 if (writev(fd
->fd
, iov
, 2) == -1) {
952 syslog(LOG_ERR
, "writev: %m");
956 } else if (strcmp(*argv
, "--getinterfaces") == 0) {
959 TAILQ_FOREACH(ifp
, ifaces
, next
) {
961 if (D6_STATE_RUNNING(ifp
))
963 if (ipv6nd_has_ra(ifp
))
966 len
= write(fd
->fd
, &len
, sizeof(len
));
967 if (len
!= sizeof(len
))
969 TAILQ_FOREACH(ifp
, ifaces
, next
) {
970 send_interface(fd
->fd
, ifp
);
975 while (argv
[++opt
] != NULL
) {
976 TAILQ_FOREACH(ifp
, ifaces
, next
) {
977 if (strcmp(argv
[opt
], ifp
->name
) == 0) {
979 if (D6_STATE_RUNNING(ifp
))
981 if (ipv6nd_has_ra(ifp
))
986 len
= write(fd
->fd
, &len
, sizeof(len
));
987 if (len
!= sizeof(len
))
990 while (argv
[++opt
] != NULL
) {
991 TAILQ_FOREACH(ifp
, ifaces
, next
) {
992 if (strcmp(argv
[opt
], ifp
->name
) == 0)
993 send_interface(fd
->fd
, ifp
);
997 } else if (strcmp(*argv
, "--listen") == 0) {
1003 /* Log the command */
1005 for (opt
= 0; opt
< argc
; opt
++)
1006 len
+= strlen(argv
[opt
]) + 1;
1007 tmp
= p
= malloc(len
+ 1);
1009 syslog(LOG_ERR
, "%s: %m", __func__
);
1012 for (opt
= 0; opt
< argc
; opt
++) {
1013 l
= strlen(argv
[opt
]);
1014 strlcpy(p
, argv
[opt
], l
+ 1);
1019 syslog(LOG_INFO
, "control command: %s", tmp
);
1023 while ((opt
= getopt_long(argc
, argv
, IF_OPTS
, cf_options
, &oi
)) != -1)
1027 /* Assumed if below not set */
1041 /* We need at least one interface */
1042 if (optind
== argc
) {
1043 syslog(LOG_ERR
, "%s: no interface", __func__
);
1047 if (do_release
|| do_exit
) {
1048 for (oi
= optind
; oi
< argc
; oi
++) {
1049 if ((ifp
= find_interface(argv
[oi
])) == NULL
)
1052 ifp
->options
->options
|= DHCPCD_RELEASE
;
1053 ifp
->options
->options
&= ~DHCPCD_PERSISTENT
;
1055 ifp
->options
->options
|= DHCPCD_EXITING
;
1056 stop_interface(ifp
);
1061 reconf_reboot(do_reboot
, argc
, argv
, optind
);
1066 signal_init(void (*func
)(int, siginfo_t
*, void *), sigset_t
*oldset
)
1069 struct sigaction sa
;
1072 sigfillset(&newset
);
1073 if (sigprocmask(SIG_SETMASK
, &newset
, oldset
) == -1)
1076 memset(&sa
, 0, sizeof(sa
));
1077 sa
.sa_sigaction
= func
;
1078 sa
.sa_flags
= SA_SIGINFO
;
1079 sigemptyset(&sa
.sa_mask
);
1081 for (i
= 0; handle_sigs
[i
]; i
++) {
1082 if (sigaction(handle_sigs
[i
], &sa
, NULL
) == -1)
1089 main(int argc
, char **argv
)
1091 struct interface
*ifp
;
1092 uint16_t family
= 0;
1093 int opt
, oi
= 0, sig
= 0, i
, control_fd
;
1098 const char *platform
;
1101 openlog(PACKAGE
, LOG_PERROR
| LOG_PID
, LOG_DAEMON
);
1102 setlogmask(LOG_UPTO(LOG_INFO
));
1104 /* Test for --help and --version */
1106 if (strcmp(argv
[1], "--help") == 0) {
1109 } else if (strcmp(argv
[1], "--version") == 0) {
1110 printf(""PACKAGE
" "VERSION
"\n%s\n", copyright
);
1115 platform
= hardware_platform();
1116 if (uname(&utn
) == 0)
1117 snprintf(vendor
, VENDORCLASSID_MAX_LEN
,
1118 "%s-%s:%s-%s:%s%s%s", PACKAGE
, VERSION
,
1119 utn
.sysname
, utn
.release
, utn
.machine
,
1120 platform
? ":" : "", platform
? platform
: "");
1122 snprintf(vendor
, VENDORCLASSID_MAX_LEN
,
1123 "%s-%s", PACKAGE
, VERSION
);
1126 while ((opt
= getopt_long(argc
, argv
, IF_OPTS
, cf_options
, &oi
)) != -1)
1157 printf("Interface options:\n");
1160 if (family
== 0 || family
== AF_INET
) {
1161 printf("\nDHCPv4 options:\n");
1162 dhcp_printoptions();
1166 if (family
== 0 || family
== AF_INET6
) {
1167 printf("\nDHCPv6 options:\n");
1168 dhcp6_printoptions();
1180 if_options
= read_config(cffile
, NULL
, NULL
, NULL
);
1181 opt
= add_options(if_options
, argc
, argv
);
1187 options
= if_options
->options
;
1190 options
|= DHCPCD_TEST
;
1192 options
|= DHCPCD_DUMPLEASE
;
1193 options
|= DHCPCD_PERSISTENT
;
1194 options
&= ~DHCPCD_DAEMONISE
;
1197 #ifdef THERE_IS_NO_FORK
1198 options
&= ~DHCPCD_DAEMONISE
;
1201 if (options
& DHCPCD_DEBUG
)
1202 setlogmask(LOG_UPTO(LOG_DEBUG
));
1203 if (options
& DHCPCD_QUIET
) {
1204 i
= open(_PATH_DEVNULL
, O_RDWR
);
1206 syslog(LOG_ERR
, "%s: open: %m", __func__
);
1208 dup2(i
, STDERR_FILENO
);
1213 if (!(options
& (DHCPCD_TEST
| DHCPCD_DUMPLEASE
))) {
1214 /* If we have any other args, we should run as a single dhcpcd
1215 * instance for that interface. */
1216 len
= strlen(PIDFILE
) + IF_NAMESIZE
+ 2;
1217 pidfile
= malloc(len
);
1218 if (pidfile
== NULL
) {
1219 syslog(LOG_ERR
, "%s: %m", __func__
);
1222 if (optind
== argc
- 1)
1223 snprintf(pidfile
, len
, PIDFILE
, "-", argv
[optind
]);
1225 snprintf(pidfile
, len
, PIDFILE
, "", "");
1226 options
|= DHCPCD_MASTER
;
1230 if (chdir("/") == -1)
1231 syslog(LOG_ERR
, "chdir `/': %m");
1234 if (options
& DHCPCD_DUMPLEASE
) {
1235 if (optind
!= argc
- 1) {
1236 syslog(LOG_ERR
, "dumplease requires an interface");
1239 if (dhcp_dump(argv
[optind
]) == -1)
1244 if (!(options
& (DHCPCD_MASTER
| DHCPCD_TEST
))) {
1245 control_fd
= control_open();
1246 if (control_fd
!= -1) {
1248 "sending commands to master dhcpcd process");
1249 i
= control_send(argc
, argv
);
1251 syslog(LOG_DEBUG
, "send OK");
1254 syslog(LOG_ERR
, "failed to send commands");
1258 if (errno
!= ENOENT
)
1259 syslog(LOG_ERR
, "control_open: %m");
1265 PACKAGE
" will not work correctly unless run as root");
1270 syslog(LOG_INFO
, "sending signal %d to pid %d",
1272 if (pid
== 0 || kill(pid
, sig
) != 0) {
1273 if (sig
!= SIGALRM
&& errno
!= EPERM
)
1274 syslog(LOG_ERR
, ""PACKAGE
" not running");
1275 if (pid
!= 0 && errno
!= ESRCH
) {
1276 syslog(LOG_ERR
, "kill: %m");
1283 if (sig
== SIGALRM
|| sig
== SIGUSR1
)
1285 /* Spin until it exits */
1286 syslog(LOG_INFO
, "waiting for pid %d to exit", pid
);
1288 ts
.tv_nsec
= 100000000; /* 10th of a second */
1289 for(i
= 0; i
< 100; i
++) {
1290 nanosleep(&ts
, NULL
);
1291 if (read_pid() == 0)
1294 syslog(LOG_ERR
, "pid %d failed to exit", pid
);
1299 if (!(options
& DHCPCD_TEST
)) {
1300 if ((pid
= read_pid()) > 0 &&
1303 syslog(LOG_ERR
, ""PACKAGE
1304 " already running on pid %d (%s)",
1309 /* Ensure we have the needed directories */
1310 if (mkdir(RUNDIR
, 0755) == -1 && errno
!= EEXIST
)
1311 syslog(LOG_ERR
, "mkdir `%s': %m", RUNDIR
);
1312 if (mkdir(DBDIR
, 0755) == -1 && errno
!= EEXIST
)
1313 syslog(LOG_ERR
, "mkdir `%s': %m", DBDIR
);
1315 pidfd
= open(pidfile
, O_WRONLY
| O_CREAT
| O_NONBLOCK
, 0664);
1317 syslog(LOG_ERR
, "open `%s': %m", pidfile
);
1319 /* Lock the file so that only one instance of dhcpcd
1320 * runs on an interface */
1321 if (flock(pidfd
, LOCK_EX
| LOCK_NB
) == -1) {
1322 syslog(LOG_ERR
, "flock `%s': %m", pidfile
);
1325 if (set_cloexec(pidfd
) == -1)
1327 writepid(pidfd
, getpid());
1331 syslog(LOG_INFO
, "version " VERSION
" starting");
1332 options
|= DHCPCD_STARTED
;
1338 /* Save signal mask, block and redirect signals to our handler */
1339 if (signal_init(handle_signal
, &dhcpcd_sigset
) == -1) {
1340 syslog(LOG_ERR
, "signal_setup: %m");
1344 if (options
& DHCPCD_MASTER
) {
1345 if (control_start() == -1)
1346 syslog(LOG_ERR
, "control_start: %m");
1349 if (open_sockets() == -1) {
1350 syslog(LOG_ERR
, "open_sockets: %m");
1355 if (options
& DHCPCD_IPV6RS
&& disable_rtadv() == -1) {
1356 syslog(LOG_ERR
, "disable_rtadvd: %m");
1357 options
&= ~DHCPCD_IPV6RS
;
1361 ifc
= argc
- optind
;
1362 ifv
= argv
+ optind
;
1364 /* When running dhcpcd against a single interface, we need to retain
1365 * the old behaviour of waiting for an IP address */
1366 if (ifc
== 1 && !(options
& DHCPCD_BACKGROUND
))
1367 options
|= DHCPCD_WAITIP
;
1369 /* RTM_NEWADDR goes through the link socket as well which we
1370 * need for IPv6 DAD, so we check for DHCPCD_LINK in handle_carrier
1372 * We also need to open this before checking for interfaces below
1373 * so that we pickup any new addresses during the discover phase. */
1375 linkfd
= open_link_socket();
1377 syslog(LOG_ERR
, "open_link_socket: %m");
1379 eloop_event_add(linkfd
, handle_link
, NULL
);
1382 /* Start any dev listening plugin which may want to
1383 * change the interface name provided by the kernel */
1384 if ((options
& (DHCPCD_MASTER
| DHCPCD_DEV
)) ==
1385 (DHCPCD_MASTER
| DHCPCD_DEV
))
1386 dev_start(dev_load
);
1388 ifaces
= discover_interfaces(ifc
, ifv
);
1389 for (i
= 0; i
< ifc
; i
++) {
1390 if (find_interface(ifv
[i
]) == NULL
)
1391 syslog(LOG_ERR
, "%s: interface not found or invalid",
1394 if (ifaces
== NULL
|| TAILQ_FIRST(ifaces
) == NULL
) {
1396 syslog(LOG_ERR
, "no valid interfaces found");
1399 if (!(options
& DHCPCD_LINK
)) {
1401 "aborting as link detection is disabled");
1406 if (options
& DHCPCD_BACKGROUND
)
1410 TAILQ_FOREACH(ifp
, ifaces
, next
) {
1411 init_state(ifp
, argc
, argv
);
1412 if (ifp
->carrier
!= LINK_DOWN
)
1416 if (!(options
& DHCPCD_BACKGROUND
)) {
1417 /* If we don't have a carrier, we may have to wait for a second
1418 * before one becomes available if we brought an interface up */
1420 options
& DHCPCD_LINK
&&
1421 options
& DHCPCD_WAITUP
&&
1422 !(options
& DHCPCD_WAITIP
))
1426 nanosleep(&ts
, NULL
);
1427 TAILQ_FOREACH(ifp
, ifaces
, next
) {
1428 handle_carrier(LINK_UNKNOWN
, 0, ifp
->name
);
1429 if (ifp
->carrier
!= LINK_DOWN
) {
1435 if (options
& DHCPCD_MASTER
)
1436 i
= if_options
->timeout
;
1437 else if ((ifp
= TAILQ_FIRST(ifaces
)))
1438 i
= ifp
->options
->timeout
;
1442 options
& DHCPCD_LINK
&&
1443 !(options
& DHCPCD_WAITIP
))
1445 syslog(LOG_WARNING
, "no interfaces have a carrier");
1448 if (options
& DHCPCD_IPV4LL
)
1449 options
|= DHCPCD_TIMEOUT_IPV4LL
;
1450 eloop_timeout_add_sec(i
, handle_exit_timeout
, NULL
);
1453 free_options(if_options
);
1457 TAILQ_FOREACH(ifp
, ifaces
, next
) {
1458 eloop_timeout_add_sec(0, start_interface
, ifp
);
1461 eloop_start(&dhcpcd_sigset
);