2 * dhcpcd - DHCP client daemon
3 * Copyright (c) 2006-2009 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-2009 Roy Marples";
31 #include <sys/socket.h>
34 #include <sys/types.h>
37 #include <arpa/inet.h>
38 #include <net/route.h>
41 # include <linux/rtnetlink.h>
61 #include "configure.h"
66 #include "if-options.h"
72 /* We should define a maximum for the NAK exponential backoff */
75 /* Wait N nanoseconds between sending a RELEASE and dropping the address.
76 * This gives the kernel enough time to actually send it. */
77 #define RELEASE_DELAY_S 0
78 #define RELEASE_DELAY_NS 10000000
82 struct interface
*ifaces
= NULL
;
94 static int linkfd
= -1;
101 static const struct dhcp_op dhcp_ops
[] = {
102 { DHCP_DISCOVER
, "DHCP_DISCOVER" },
103 { DHCP_OFFER
, "DHCP_OFFER" },
104 { DHCP_REQUEST
, "DHCP_REQUEST" },
105 { DHCP_DECLINE
, "DHCP_DECLINE" },
106 { DHCP_ACK
, "DHCP_ACK" },
107 { DHCP_NAK
, "DHCP_NAK" },
108 { DHCP_RELEASE
, "DHCP_RELEASE" },
109 { DHCP_INFORM
, "DHCP_INFORM" },
113 static void send_release(struct interface
*);
116 get_dhcp_op(uint8_t type
)
118 const struct dhcp_op
*d
;
120 for (d
= dhcp_ops
; d
->name
; d
++)
121 if (d
->value
== type
)
132 if ((fp
= fopen(pidfile
, "r")) == NULL
) {
136 if (fscanf(fp
, "%d", &pid
) != 1)
145 printf("usage: "PACKAGE
" [-dgknpqwxyADEGHKLOTV] [-c script] [-f file]"
147 " [-h hostname] [-i classID ] [-l leasetime]"
148 " [-m metric] [-o option]\n"
149 " [-r ipaddr] [-s ipaddr] [-t timeout]"
151 " [-F none|ptr|both] [-I clientID] [-C hookscript]"
153 " [-X ipaddr] <interface>\n");
160 struct interface
*iface
;
165 ifaces
= iface
->next
;
166 free_interface(iface
);
169 for (i
= 0; i
< ifac
; i
++)
172 for (i
= 0; i
< ifdc
; i
++)
180 if (options
& DHCPCD_MASTER
) {
181 if (stop_control() == -1)
182 syslog(LOG_ERR
, "stop_control: %m");
194 handle_exit_timeout(_unused
void *arg
)
198 syslog(LOG_ERR
, "timed out");
199 if (!(options
& DHCPCD_TIMEOUT_IPV4LL
))
201 options
&= ~DHCPCD_TIMEOUT_IPV4LL
;
202 timeout
= (PROBE_NUM
* PROBE_MAX
) + PROBE_WAIT
+ 1;
203 syslog(LOG_WARNING
, "allowing %d seconds for IPv4LL timeout", timeout
);
204 add_timeout_sec(timeout
, handle_exit_timeout
, NULL
);
208 drop_config(struct interface
*iface
, const char *reason
)
210 free(iface
->state
->old
);
211 iface
->state
->old
= iface
->state
->new;
212 iface
->state
->new = NULL
;
213 iface
->state
->reason
= reason
;
215 free(iface
->state
->old
);
216 iface
->state
->old
= NULL
;
217 iface
->state
->lease
.addr
.s_addr
= 0;
221 close_sockets(struct interface
*iface
)
223 if (iface
->arp_fd
!= -1) {
224 delete_event(iface
->arp_fd
);
225 close(iface
->arp_fd
);
228 if (iface
->raw_fd
!= -1) {
229 delete_event(iface
->raw_fd
);
230 close(iface
->raw_fd
);
233 if (iface
->udp_fd
!= -1) {
234 close(iface
->udp_fd
);
240 find_interface(const char *ifname
)
242 struct interface
*ifp
;
244 for (ifp
= ifaces
; ifp
; ifp
= ifp
->next
)
245 if (strcmp(ifp
->name
, ifname
) == 0)
251 stop_interface(struct interface
*iface
)
253 struct interface
*ifp
, *ifl
= NULL
;
255 syslog(LOG_INFO
, "%s: removing interface", iface
->name
);
256 if (strcmp(iface
->state
->reason
, "RELEASE") != 0)
257 drop_config(iface
, "STOP");
258 close_sockets(iface
);
259 delete_timeout(NULL
, iface
);
260 for (ifp
= ifaces
; ifp
; ifp
= ifp
->next
) {
266 ifl
->next
= ifp
->next
;
270 if (!(options
& (DHCPCD_MASTER
| DHCPCD_TEST
)))
275 send_message(struct interface
*iface
, int type
,
276 void (*callback
)(void *))
278 struct if_state
*state
= iface
->state
;
279 struct if_options
*ifo
= state
->options
;
280 struct dhcp_message
*dhcp
;
283 struct in_addr from
, to
;
288 syslog(LOG_DEBUG
, "%s: sending %s with xid 0x%x",
289 iface
->name
, get_dhcp_op(type
), state
->xid
);
291 if (state
->interval
== 0)
294 state
->interval
*= 2;
295 if (state
->interval
> 64)
296 state
->interval
= 64;
298 tv
.tv_sec
= state
->interval
+ DHCP_RAND_MIN
;
299 tv
.tv_usec
= arc4random() % (DHCP_RAND_MAX_U
- DHCP_RAND_MIN_U
);
301 "%s: sending %s (xid 0x%x), next in %0.2f seconds",
302 iface
->name
, get_dhcp_op(type
), state
->xid
,
303 timeval_to_double(&tv
));
305 /* If we couldn't open a UDP port for our IP address
306 * then we cannot renew.
307 * This could happen if our IP was pulled out from underneath us.
308 * Also, we should not unicast from a BOOTP lease. */
309 if (iface
->udp_fd
== -1 ||
310 (!(ifo
->options
& DHCPCD_INFORM
) && is_bootp(iface
->state
->new)))
312 a
= iface
->addr
.s_addr
;
313 iface
->addr
.s_addr
= 0;
315 len
= make_message(&dhcp
, iface
, type
);
317 iface
->addr
.s_addr
= a
;
318 from
.s_addr
= dhcp
->ciaddr
;
320 to
.s_addr
= state
->lease
.server
.s_addr
;
323 if (to
.s_addr
&& to
.s_addr
!= INADDR_BROADCAST
) {
324 r
= send_packet(iface
, to
, (uint8_t *)dhcp
, len
);
326 syslog(LOG_ERR
, "%s: send_packet: %m", iface
->name
);
328 len
= make_udp_packet(&udp
, (uint8_t *)dhcp
, len
, from
, to
);
329 r
= send_raw_packet(iface
, ETHERTYPE_IP
, udp
, len
);
331 /* If we failed to send a raw packet this normally means
332 * we don't have the ability to work beneath the IP layer
333 * for this interface.
334 * As such we remove it from consideration without actually
335 * stopping the interface. */
337 syslog(LOG_ERR
, "%s: send_raw_packet: %m", iface
->name
);
338 if (!(options
& DHCPCD_TEST
))
339 drop_config(iface
, "FAIL");
340 close_sockets(iface
);
341 delete_timeout(NULL
, iface
);
346 /* Even if we fail to send a packet we should continue as we are
347 * as our failure timeouts will change out codepath when needed. */
349 add_timeout_tv(&tv
, callback
, iface
);
353 send_inform(void *arg
)
355 send_message((struct interface
*)arg
, DHCP_INFORM
, send_inform
);
359 send_discover(void *arg
)
361 send_message((struct interface
*)arg
, DHCP_DISCOVER
, send_discover
);
365 send_request(void *arg
)
367 send_message((struct interface
*)arg
, DHCP_REQUEST
, send_request
);
371 send_renew(void *arg
)
373 send_message((struct interface
*)arg
, DHCP_REQUEST
, send_renew
);
377 send_rebind(void *arg
)
379 send_message((struct interface
*)arg
, DHCP_REQUEST
, send_rebind
);
383 start_expire(void *arg
)
385 struct interface
*iface
= arg
;
387 iface
->state
->interval
= 0;
388 if (iface
->addr
.s_addr
== 0) {
389 /* We failed to reboot, so enter discovery. */
390 start_discover(iface
);
394 syslog(LOG_ERR
, "%s: lease expired", iface
->name
);
395 delete_timeout(NULL
, iface
);
396 drop_config(iface
, "EXPIRE");
397 unlink(iface
->leasefile
);
398 if (iface
->carrier
!= LINK_DOWN
)
399 start_interface(iface
);
403 log_dhcp(int lvl
, const char *msg
,
404 const struct interface
*iface
, const struct dhcp_message
*dhcp
)
410 if (strcmp(msg
, "NAK:") == 0)
411 a
= get_option_string(dhcp
, DHO_MESSAGE
);
412 else if (dhcp
->yiaddr
!= 0) {
413 addr
.s_addr
= dhcp
->yiaddr
;
414 a
= xstrdup(inet_ntoa(addr
));
417 r
= get_option_addr(&addr
, dhcp
, DHO_SERVERID
);
418 if (dhcp
->servername
[0] && r
== 0)
419 syslog(lvl
, "%s: %s %s from %s `%s'", iface
->name
, msg
, a
,
420 inet_ntoa(addr
), dhcp
->servername
);
423 syslog(lvl
, "%s: %s from %s",
424 iface
->name
, msg
, inet_ntoa(addr
));
426 syslog(lvl
, "%s: %s %s from %s",
427 iface
->name
, msg
, a
, inet_ntoa(addr
));
428 } else if (a
!= NULL
)
429 syslog(lvl
, "%s: %s %s", iface
->name
, msg
, a
);
431 syslog(lvl
, "%s: %s", iface
->name
, msg
);
436 blacklisted_ip(const struct if_options
*ifo
, in_addr_t addr
)
440 for (i
= 0; i
< ifo
->blacklist_len
; i
+= 2)
441 if (ifo
->blacklist
[i
] == (addr
& ifo
->blacklist
[i
+ 1]))
447 whitelisted_ip(const struct if_options
*ifo
, in_addr_t addr
)
451 if (ifo
->whitelist_len
== 0)
453 for (i
= 0; i
< ifo
->whitelist_len
; i
+= 2)
454 if (ifo
->whitelist
[i
] == (addr
& ifo
->whitelist
[i
+ 1]))
460 handle_dhcp(struct interface
*iface
, struct dhcp_message
**dhcpp
)
462 struct if_state
*state
= iface
->state
;
463 struct if_options
*ifo
= state
->options
;
464 struct dhcp_message
*dhcp
= *dhcpp
;
465 struct dhcp_lease
*lease
= &state
->lease
;
470 /* reset the message counter */
473 /* We may have found a BOOTP server */
474 if (get_option_uint8(&type
, dhcp
, DHO_MESSAGETYPE
) == -1)
477 if (type
== DHCP_NAK
) {
478 /* For NAK, only check if we require the ServerID */
479 if (has_option_mask(ifo
->requiremask
, DHO_SERVERID
) &&
480 get_option_addr(&addr
, dhcp
, DHO_SERVERID
) == -1)
482 log_dhcp(LOG_WARNING
, "reject NAK", iface
, dhcp
);
485 /* We should restart on a NAK */
486 log_dhcp(LOG_WARNING
, "NAK:", iface
, dhcp
);
487 if (!(options
& DHCPCD_TEST
)) {
488 drop_config(iface
, "NAK");
489 unlink(iface
->leasefile
);
491 delete_event(iface
->raw_fd
);
492 close(iface
->raw_fd
);
494 close(iface
->udp_fd
);
496 /* If we constantly get NAKS then we should slowly back off */
497 add_timeout_sec(state
->nakoff
, start_interface
, iface
);
499 if (state
->nakoff
> NAKOFF_MAX
)
500 state
->nakoff
= NAKOFF_MAX
;
504 /* Ensure that all required options are present */
505 for (i
= 1; i
< 255; i
++) {
506 if (has_option_mask(ifo
->requiremask
, i
) &&
507 get_option_uint8(&tmp
, dhcp
, i
) != 0)
509 /* If we are bootp, then ignore the need for serverid.
510 * To ignore bootp, require dhcp_message_type instead. */
511 if (type
== 0 && i
== DHO_SERVERID
)
513 log_dhcp(LOG_WARNING
, "reject DHCP", iface
, dhcp
);
518 /* No NAK, so reset the backoff */
521 if ((type
== 0 || type
== DHCP_OFFER
) &&
522 state
->state
== DHS_DISCOVER
)
525 lease
->addr
.s_addr
= dhcp
->yiaddr
;
527 get_option_addr(&lease
->server
, dhcp
, DHO_SERVERID
) != 0)
528 lease
->server
.s_addr
= INADDR_ANY
;
529 log_dhcp(LOG_INFO
, "offered", iface
, dhcp
);
533 if (options
& DHCPCD_TEST
) {
535 state
->old
= state
->new;
536 state
->new = state
->offer
;
538 state
->reason
= "TEST";
542 delete_timeout(send_discover
, iface
);
543 /* We don't request BOOTP addresses */
545 /* We used to ARP check here, but that seems to be in
546 * violation of RFC2131 where it only describes
547 * DECLINE after REQUEST.
548 * It also seems that some MS DHCP servers actually
549 * ignore DECLINE if no REQUEST, ie we decline a
551 start_request(iface
);
557 if (type
== DHCP_OFFER
) {
558 log_dhcp(LOG_INFO
, "ignoring offer of", iface
, dhcp
);
562 /* We should only be dealing with acks */
563 if (type
!= DHCP_ACK
) {
564 log_dhcp(LOG_ERR
, "not ACK or OFFER", iface
, dhcp
);
568 if (!(ifo
->options
& DHCPCD_INFORM
))
569 log_dhcp(LOG_INFO
, "acknowledged", iface
, dhcp
);
572 /* BOOTP could have already assigned this above, so check we still
581 delete_timeout(NULL
, iface
);
583 /* We now have an offer, so close the DHCP sockets.
584 * This allows us to safely ARP when broken DHCP servers send an ACK
585 * follows by an invalid NAK. */
586 close_sockets(iface
);
588 if (ifo
->options
& DHCPCD_ARP
&&
589 iface
->addr
.s_addr
!= state
->offer
->yiaddr
)
591 /* If the interface already has the address configured
592 * then we can't ARP for duplicate detection. */
593 addr
.s_addr
= state
->offer
->yiaddr
;
594 if (has_address(iface
->name
, &addr
, NULL
) != 1) {
597 state
->conflicts
= 0;
598 state
->state
= DHS_PROBE
;
599 send_arp_probe(iface
);
604 bind_interface(iface
);
608 handle_dhcp_packet(void *arg
)
610 struct interface
*iface
= arg
;
612 struct dhcp_message
*dhcp
= NULL
;
618 /* We loop through until our buffer is empty.
619 * The benefit is that if we get >1 DHCP packet in our buffer and
620 * the first one fails for any reason, we can use the next. */
621 packet
= xmalloc(udp_dhcp_len
);
623 bytes
= get_raw_packet(iface
, ETHERTYPE_IP
,
624 packet
, udp_dhcp_len
);
625 if (bytes
== 0 || bytes
== -1)
627 if (valid_udp_packet(packet
, bytes
, &from
) == -1) {
628 syslog(LOG_ERR
, "%s: invalid UDP packet from %s",
629 iface
->name
, inet_ntoa(from
));
632 i
= whitelisted_ip(iface
->state
->options
, from
.s_addr
);
635 "%s: non whitelisted DHCP packet from %s",
636 iface
->name
, inet_ntoa(from
));
639 blacklisted_ip(iface
->state
->options
, from
.s_addr
) == 1)
642 "%s: blacklisted DHCP packet from %s",
643 iface
->name
, inet_ntoa(from
));
646 if (iface
->flags
& IFF_POINTOPOINT
&&
647 iface
->dst
.s_addr
!= from
.s_addr
)
650 "%s: server %s is not destination",
651 iface
->name
, inet_ntoa(from
));
653 bytes
= get_udp_data(&pp
, packet
);
654 if ((size_t)bytes
> sizeof(*dhcp
)) {
656 "%s: packet greater than DHCP size from %s",
657 iface
->name
, inet_ntoa(from
));
661 dhcp
= xzalloc(sizeof(*dhcp
));
662 memcpy(dhcp
, pp
, bytes
);
663 if (dhcp
->cookie
!= htonl(MAGIC_COOKIE
)) {
664 syslog(LOG_DEBUG
, "%s: bogus cookie from %s",
665 iface
->name
, inet_ntoa(from
));
668 /* Ensure it's the right transaction */
669 if (iface
->state
->xid
!= dhcp
->xid
) {
671 "%s: wrong xid 0x%x (expecting 0x%x) from %s",
672 iface
->name
, dhcp
->xid
, iface
->state
->xid
,
676 /* Ensure packet is for us */
677 if (iface
->hwlen
<= sizeof(dhcp
->chaddr
) &&
678 memcmp(dhcp
->chaddr
, iface
->hwaddr
, iface
->hwlen
))
680 syslog(LOG_DEBUG
, "%s: xid 0x%x is not for hwaddr %s",
681 iface
->name
, dhcp
->xid
,
682 hwaddr_ntoa(dhcp
->chaddr
, sizeof(dhcp
->chaddr
)));
685 handle_dhcp(iface
, &dhcp
);
686 if (iface
->raw_fd
== -1)
694 open_sockets(struct interface
*iface
)
696 if (iface
->udp_fd
!= -1)
697 close(iface
->udp_fd
);
698 if (open_udp_socket(iface
) == -1 &&
699 (errno
!= EADDRINUSE
|| iface
->addr
.s_addr
!= 0))
700 syslog(LOG_ERR
, "%s: open_udp_socket: %m", iface
->name
);
701 if (iface
->raw_fd
!= -1)
702 delete_event(iface
->raw_fd
);
703 if (open_socket(iface
, ETHERTYPE_IP
) == -1)
704 syslog(LOG_ERR
, "%s: open_socket: %m", iface
->name
);
705 if (iface
->raw_fd
!= -1)
706 add_event(iface
->raw_fd
, handle_dhcp_packet
, iface
);
710 send_release(struct interface
*iface
)
714 if (iface
->state
->lease
.addr
.s_addr
&&
715 !IN_LINKLOCAL(htonl(iface
->state
->lease
.addr
.s_addr
)))
717 syslog(LOG_INFO
, "%s: releasing lease of %s",
718 iface
->name
, inet_ntoa(iface
->state
->lease
.addr
));
720 iface
->state
->xid
= arc4random();
721 send_message(iface
, DHCP_RELEASE
, NULL
);
722 /* Give the packet a chance to go before dropping the ip */
723 ts
.tv_sec
= RELEASE_DELAY_S
;
724 ts
.tv_nsec
= RELEASE_DELAY_NS
;
725 nanosleep(&ts
, NULL
);
726 drop_config(iface
, "RELEASE");
728 unlink(iface
->leasefile
);
732 send_decline(struct interface
*iface
)
735 send_message(iface
, DHCP_DECLINE
, NULL
);
739 configure_interface1(struct interface
*iface
)
741 struct if_state
*ifs
= iface
->state
;
742 struct if_options
*ifo
= ifs
->options
;
746 if (iface
->flags
& IFF_POINTOPOINT
&& !(ifo
->options
& DHCPCD_INFORM
))
747 ifo
->options
|= DHCPCD_STATIC
;
748 if (iface
->flags
& IFF_NOARP
||
749 ifo
->options
& (DHCPCD_INFORM
| DHCPCD_STATIC
))
750 ifo
->options
&= ~(DHCPCD_ARP
| DHCPCD_IPV4LL
);
751 if (ifo
->options
& DHCPCD_LINK
&& carrier_status(iface
) == -1)
752 ifo
->options
&= ~DHCPCD_LINK
;
754 if (ifo
->metric
!= -1)
755 iface
->metric
= ifo
->metric
;
757 /* If we haven't specified a ClientID and our hardware address
758 * length is greater than DHCP_CHADDR_LEN then we enforce a ClientID
759 * of the hardware address family and the hardware address. */
760 if (iface
->hwlen
> DHCP_CHADDR_LEN
)
761 ifo
->options
|= DHCPCD_CLIENTID
;
763 free(iface
->clientid
);
764 if (*ifo
->clientid
) {
765 iface
->clientid
= xmalloc(ifo
->clientid
[0] + 1);
766 memcpy(iface
->clientid
, ifo
->clientid
, ifo
->clientid
[0] + 1);
767 } else if (ifo
->options
& DHCPCD_CLIENTID
) {
768 if (ifo
->options
& DHCPCD_DUID
) {
769 duid
= xmalloc(DUID_LEN
);
770 if ((len
= get_duid(duid
, iface
)) == 0)
771 syslog(LOG_ERR
, "get_duid: %m");
774 iface
->clientid
= xmalloc(len
+ 6);
775 iface
->clientid
[0] = len
+ 5;
776 iface
->clientid
[1] = 255; /* RFC 4361 */
777 ifl
= strlen(iface
->name
);
779 memcpy(iface
->clientid
+ 2, iface
->name
, ifl
);
781 memset(iface
->clientid
+ 2 + ifl
,
784 ifl
= htonl(if_nametoindex(iface
->name
));
785 memcpy(iface
->clientid
+ 2, &ifl
, 4);
787 } else if (len
== 0) {
788 len
= iface
->hwlen
+ 1;
789 iface
->clientid
= xmalloc(len
+ 1);
790 iface
->clientid
[0] = len
;
791 iface
->clientid
[1] = iface
->family
;
792 memcpy(iface
->clientid
+ 2, iface
->hwaddr
,
796 if (ifo
->options
& DHCPCD_CLIENTID
)
797 syslog(LOG_DEBUG
, "%s: using ClientID %s", iface
->name
,
798 hwaddr_ntoa(iface
->clientid
+ 1, *iface
->clientid
));
802 select_profile(struct interface
*iface
, const char *profile
)
804 struct if_options
*ifo
;
806 ifo
= read_config(cffile
, iface
->name
, iface
->ssid
, profile
);
808 syslog(LOG_DEBUG
, "%s: no profile %s", iface
->name
, profile
);
811 if (profile
!= NULL
) {
812 strlcpy(iface
->state
->profile
, profile
,
813 sizeof(iface
->state
->profile
));
814 syslog(LOG_INFO
, "%s: selected profile %s",
815 iface
->name
, profile
);
817 *iface
->state
->profile
= '\0';
818 free_options(iface
->state
->options
);
819 iface
->state
->options
= ifo
;
820 configure_interface1(iface
);
825 start_fallback(void *arg
)
827 struct interface
*iface
;
829 iface
= (struct interface
*)arg
;
830 select_profile(iface
, iface
->state
->options
->fallback
);
831 configure_interface1(iface
);
832 start_interface(iface
);
836 configure_interface(struct interface
*iface
, int argc
, char **argv
)
838 select_profile(iface
, NULL
);
839 add_options(iface
->state
->options
, argc
, argv
);
840 configure_interface1(iface
);
844 handle_carrier(const char *ifname
)
846 struct interface
*iface
;
849 if (!(options
& DHCPCD_LINK
))
851 for (iface
= ifaces
; iface
; iface
= iface
->next
)
852 if (strcmp(iface
->name
, ifname
) == 0)
854 if (!iface
|| !(iface
->state
->options
->options
& DHCPCD_LINK
))
856 carrier
= carrier_status(iface
);
858 syslog(LOG_ERR
, "%s: carrier_status: %m", ifname
);
859 else if (carrier
== 0 || !(iface
->flags
& IFF_RUNNING
)) {
860 if (iface
->carrier
!= LINK_DOWN
) {
861 iface
->carrier
= LINK_DOWN
;
862 syslog(LOG_INFO
, "%s: carrier lost", iface
->name
);
863 close_sockets(iface
);
864 delete_timeouts(iface
, start_expire
, NULL
);
865 drop_config(iface
, "NOCARRIER");
867 } else if (carrier
== 1 && (iface
->flags
& IFF_RUNNING
)) {
868 if (iface
->carrier
!= LINK_UP
) {
869 iface
->carrier
= LINK_UP
;
870 syslog(LOG_INFO
, "%s: carrier acquired", iface
->name
);
872 getifssid(iface
->name
, iface
->ssid
);
873 configure_interface(iface
, margc
, margv
);
874 iface
->state
->interval
= 0;
875 iface
->state
->reason
= "CARRIER";
877 start_interface(iface
);
883 start_discover(void *arg
)
885 struct interface
*iface
= arg
;
886 struct if_options
*ifo
= iface
->state
->options
;
888 iface
->state
->state
= DHS_DISCOVER
;
889 iface
->state
->xid
= arc4random();
891 delete_timeout(NULL
, iface
);
893 add_timeout_sec(ifo
->timeout
, start_fallback
, iface
);
894 else if (ifo
->options
& DHCPCD_IPV4LL
&&
895 !IN_LINKLOCAL(htonl(iface
->addr
.s_addr
)))
897 if (IN_LINKLOCAL(htonl(iface
->state
->fail
.s_addr
)))
898 add_timeout_sec(RATE_LIMIT_INTERVAL
, start_ipv4ll
, iface
);
900 add_timeout_sec(ifo
->timeout
, start_ipv4ll
, iface
);
902 syslog(LOG_INFO
, "%s: broadcasting for a lease", iface
->name
);
903 send_discover(iface
);
907 start_request(void *arg
)
909 struct interface
*iface
= arg
;
911 iface
->state
->state
= DHS_REQUEST
;
916 start_renew(void *arg
)
918 struct interface
*iface
= arg
;
920 syslog(LOG_INFO
, "%s: renewing lease of %s",
921 iface
->name
, inet_ntoa(iface
->state
->lease
.addr
));
922 iface
->state
->state
= DHS_RENEW
;
923 iface
->state
->xid
= arc4random();
929 start_rebind(void *arg
)
931 struct interface
*iface
= arg
;
933 syslog(LOG_ERR
, "%s: failed to renew, attempting to rebind",
935 iface
->state
->state
= DHS_REBIND
;
936 delete_timeout(send_renew
, iface
);
937 iface
->state
->lease
.server
.s_addr
= 0;
938 if (iface
->raw_fd
== -1)
944 start_timeout(void *arg
)
946 struct interface
*iface
= arg
;
948 bind_interface(iface
);
949 iface
->state
->interval
= 0;
950 start_discover(iface
);
953 static struct dhcp_message
*
954 dhcp_message_new(struct in_addr
*addr
, struct in_addr
*mask
)
956 struct dhcp_message
*dhcp
;
959 dhcp
= xzalloc(sizeof(*dhcp
));
960 dhcp
->yiaddr
= addr
->s_addr
;
962 if (mask
&& mask
->s_addr
!= INADDR_ANY
) {
963 *p
++ = DHO_SUBNETMASK
;
964 *p
++ = sizeof(mask
->s_addr
);
965 memcpy(p
, &mask
->s_addr
, sizeof(mask
->s_addr
));
966 p
+= sizeof(mask
->s_addr
);
973 handle_3rdparty(struct interface
*iface
)
975 struct if_options
*ifo
;
976 struct in_addr addr
, net
, dst
;
978 ifo
= iface
->state
->options
;
979 if (ifo
->req_addr
.s_addr
!= INADDR_ANY
)
982 if (get_address(iface
->name
, &addr
, &net
, &dst
) == 1)
983 handle_ifa(RTM_NEWADDR
, iface
->name
, &addr
, &net
, &dst
);
986 "%s: waiting for 3rd party to configure IP address",
988 iface
->state
->reason
= "3RDPARTY";
995 start_static(struct interface
*iface
)
997 struct if_options
*ifo
;
999 if (handle_3rdparty(iface
))
1001 ifo
= iface
->state
->options
;
1002 iface
->state
->offer
=
1003 dhcp_message_new(&ifo
->req_addr
, &ifo
->req_mask
);
1004 delete_timeout(NULL
, iface
);
1005 bind_interface(iface
);
1009 start_inform(struct interface
*iface
)
1011 if (handle_3rdparty(iface
))
1014 if (options
& DHCPCD_TEST
) {
1015 iface
->addr
.s_addr
= iface
->state
->options
->req_addr
.s_addr
;
1016 iface
->net
.s_addr
= iface
->state
->options
->req_mask
.s_addr
;
1018 iface
->state
->options
->options
|= DHCPCD_STATIC
;
1019 start_static(iface
);
1022 iface
->state
->state
= DHS_INFORM
;
1023 iface
->state
->xid
= arc4random();
1024 open_sockets(iface
);
1029 start_reboot(struct interface
*iface
)
1031 struct if_options
*ifo
= iface
->state
->options
;
1033 if (ifo
->options
& DHCPCD_LINK
&& iface
->carrier
== LINK_DOWN
) {
1034 syslog(LOG_INFO
, "%s: waiting for carrier", iface
->name
);
1037 if (ifo
->options
& DHCPCD_STATIC
) {
1038 start_static(iface
);
1041 if (ifo
->reboot
== 0) {
1042 start_discover(iface
);
1045 if (IN_LINKLOCAL(htonl(iface
->state
->lease
.addr
.s_addr
))) {
1046 if (ifo
->options
& DHCPCD_IPV4LL
) {
1047 iface
->state
->claims
= 0;
1048 send_arp_announce(iface
);
1050 start_discover(iface
);
1053 if (ifo
->options
& DHCPCD_INFORM
) {
1054 syslog(LOG_INFO
, "%s: informing address of %s",
1055 iface
->name
, inet_ntoa(iface
->state
->lease
.addr
));
1057 syslog(LOG_INFO
, "%s: rebinding lease of %s",
1058 iface
->name
, inet_ntoa(iface
->state
->lease
.addr
));
1060 iface
->state
->state
= DHS_REBOOT
;
1061 iface
->state
->xid
= arc4random();
1062 iface
->state
->lease
.server
.s_addr
= 0;
1063 delete_timeout(NULL
, iface
);
1065 add_timeout_sec(ifo
->reboot
, start_fallback
, iface
);
1066 else if (ifo
->options
& DHCPCD_LASTLEASE
&&
1067 iface
->state
->lease
.frominfo
)
1068 add_timeout_sec(ifo
->reboot
, start_timeout
, iface
);
1069 else if (!(ifo
->options
& DHCPCD_INFORM
&&
1070 options
& (DHCPCD_MASTER
| DHCPCD_DAEMONISED
)))
1071 add_timeout_sec(ifo
->reboot
, start_expire
, iface
);
1072 open_sockets(iface
);
1073 /* Don't bother ARP checking as the server could NAK us first. */
1074 if (ifo
->options
& DHCPCD_INFORM
)
1077 send_request(iface
);
1081 start_interface(void *arg
)
1083 struct interface
*iface
= arg
;
1084 struct if_options
*ifo
= iface
->state
->options
;
1089 handle_carrier(iface
->name
);
1090 if (iface
->carrier
== LINK_DOWN
) {
1091 syslog(LOG_INFO
, "%s: waiting for carrier", iface
->name
);
1095 iface
->start_uptime
= uptime();
1096 free(iface
->state
->offer
);
1097 iface
->state
->offer
= NULL
;
1099 if (iface
->state
->arping_index
< ifo
->arping_len
) {
1100 start_arping(iface
);
1103 if (ifo
->options
& DHCPCD_STATIC
) {
1104 start_static(iface
);
1107 if (ifo
->options
& DHCPCD_INFORM
) {
1108 start_inform(iface
);
1111 if (iface
->hwlen
== 0 && ifo
->clientid
[0] == '\0') {
1112 syslog(LOG_WARNING
, "%s: needs a clientid to configure",
1114 drop_config(iface
, "FAIL");
1115 close_sockets(iface
);
1116 delete_timeout(NULL
, iface
);
1119 if (ifo
->req_addr
.s_addr
) {
1120 iface
->state
->offer
=
1121 dhcp_message_new(&ifo
->req_addr
, &ifo
->req_mask
);
1122 if (ifo
->options
& DHCPCD_REQUEST
)
1123 ifo
->req_addr
.s_addr
= 0;
1125 iface
->state
->reason
= "STATIC";
1126 iface
->state
->new = iface
->state
->offer
;
1127 iface
->state
->offer
= NULL
;
1128 get_lease(&iface
->state
->lease
, iface
->state
->new);
1130 start_inform(iface
);
1134 iface
->state
->offer
= read_lease(iface
);
1135 if (iface
->state
->offer
) {
1136 get_lease(&iface
->state
->lease
, iface
->state
->offer
);
1137 iface
->state
->lease
.frominfo
= 1;
1138 if (IN_LINKLOCAL(htonl(iface
->state
->offer
->yiaddr
))) {
1139 if (iface
->state
->offer
->yiaddr
==
1142 free(iface
->state
->offer
);
1143 iface
->state
->offer
= NULL
;
1145 } else if (stat(iface
->leasefile
, &st
) == 0 &&
1146 get_option_uint32(&l
, iface
->state
->offer
,
1147 DHO_LEASETIME
) == 0)
1149 /* Offset lease times and check expiry */
1150 gettimeofday(&now
, NULL
);
1151 if ((time_t)l
< now
.tv_sec
- st
.st_mtime
) {
1152 free(iface
->state
->offer
);
1153 iface
->state
->offer
= NULL
;
1155 l
= now
.tv_sec
- st
.st_mtime
;
1156 iface
->state
->lease
.leasetime
-= l
;
1157 iface
->state
->lease
.renewaltime
-= l
;
1158 iface
->state
->lease
.rebindtime
-= l
;
1162 if (!iface
->state
->offer
)
1163 start_discover(iface
);
1164 else if (IN_LINKLOCAL(htonl(iface
->state
->lease
.addr
.s_addr
)) &&
1165 iface
->state
->options
->options
& DHCPCD_IPV4LL
)
1166 start_ipv4ll(iface
);
1168 start_reboot(iface
);
1172 init_state(struct interface
*iface
, int argc
, char **argv
)
1174 struct if_state
*ifs
;
1179 ifs
= iface
->state
= xzalloc(sizeof(*ifs
));
1181 ifs
->state
= DHS_INIT
;
1182 ifs
->reason
= "PREINIT";
1184 configure_interface(iface
, argc
, argv
);
1185 if (!(options
& DHCPCD_TEST
))
1188 if (ifs
->options
->options
& DHCPCD_LINK
) {
1189 switch (carrier_status(iface
)) {
1191 iface
->carrier
= LINK_DOWN
;
1192 ifs
->reason
= "NOCARRIER";
1195 iface
->carrier
= LINK_UP
;
1196 ifs
->reason
= "CARRIER";
1199 iface
->carrier
= LINK_UNKNOWN
;
1202 if (!(options
& DHCPCD_TEST
))
1205 iface
->carrier
= LINK_UNKNOWN
;
1209 handle_interface(int action
, const char *ifname
)
1211 struct interface
*ifs
, *ifp
, *ifn
, *ifl
= NULL
;
1212 const char * const argv
[] = { ifname
};
1216 ifp
= find_interface(ifname
);
1218 stop_interface(ifp
);
1220 } else if (action
== 0) {
1221 handle_carrier(ifname
);
1225 /* If running off an interface list, check it's in it. */
1227 for (i
= 0; i
< ifc
; i
++)
1228 if (strcmp(ifv
[i
], ifname
) == 0)
1234 ifs
= discover_interfaces(-1, UNCONST(argv
));
1235 for (ifp
= ifs
; ifp
; ifp
= ifp
->next
) {
1236 if (strcmp(ifp
->name
, ifname
) != 0)
1238 /* Check if we already have the interface */
1239 for (ifn
= ifaces
; ifn
; ifn
= ifn
->next
) {
1240 if (strcmp(ifn
->name
, ifp
->name
) == 0)
1245 /* The flags and hwaddr could have changed */
1246 ifn
->flags
= ifp
->flags
;
1247 ifn
->hwlen
= ifp
->hwlen
;
1248 if (ifp
->hwlen
!= 0)
1249 memcpy(ifn
->hwaddr
, ifp
->hwaddr
, ifn
->hwlen
);
1256 init_state(ifp
, 0, NULL
);
1257 start_interface(ifp
);
1262 handle_ifa(int type
, const char *ifname
,
1263 struct in_addr
*addr
, struct in_addr
*net
, struct in_addr
*dst
)
1265 struct interface
*ifp
;
1266 struct if_options
*ifo
;
1269 if (addr
->s_addr
== INADDR_ANY
)
1271 for (ifp
= ifaces
; ifp
; ifp
= ifp
->next
)
1272 if (strcmp(ifp
->name
, ifname
) == 0)
1276 ifo
= ifp
->state
->options
;
1277 if ((ifo
->options
& (DHCPCD_INFORM
| DHCPCD_STATIC
)) == 0 ||
1278 ifo
->req_addr
.s_addr
!= INADDR_ANY
)
1283 if (ifp
->state
->new &&
1284 ifp
->state
->new->yiaddr
== addr
->s_addr
)
1285 drop_config(ifp
, "EXPIRE");
1288 free(ifp
->state
->old
);
1289 ifp
->state
->old
= ifp
->state
->new;
1290 ifp
->state
->new = dhcp_message_new(addr
, net
);
1291 ifp
->dst
.s_addr
= dst
? dst
->s_addr
: INADDR_ANY
;
1293 for (i
= 1; i
< 255; i
++)
1294 if (i
!= DHO_ROUTER
&&
1295 has_option_mask(ifo
->dstmask
, i
))
1296 dhcp_message_add_addr(
1300 ifp
->state
->reason
= "STATIC";
1303 if (ifo
->options
& DHCPCD_INFORM
) {
1304 ifp
->state
->state
= DHS_INFORM
;
1305 ifp
->state
->xid
= arc4random();
1306 ifp
->state
->lease
.server
.s_addr
=
1307 dst
? dst
->s_addr
: INADDR_ANY
;
1319 handle_link(_unused
void *arg
)
1321 if (manage_link(linkfd
) == -1)
1322 syslog(LOG_ERR
, "manage_link: %m");
1327 handle_signal(_unused
void *arg
)
1329 struct interface
*iface
, *ifl
;
1330 int sig
= signal_read();
1335 syslog(LOG_INFO
, "received SIGINT, stopping");
1338 syslog(LOG_INFO
, "received SIGTERM, stopping");
1341 syslog(LOG_INFO
, "received SIGALRM, rebinding lease");
1342 for (iface
= ifaces
; iface
; iface
= iface
->next
)
1343 start_interface(iface
);
1346 syslog(LOG_INFO
, "received SIGHUP, releasing lease");
1350 syslog(LOG_INFO
, "received SIGUSR, reconfiguring");
1351 for (iface
= ifaces
; iface
; iface
= iface
->next
)
1352 if (iface
->state
->new)
1356 syslog(LOG_WARNING
, "received SIGPIPE");
1360 "received signal %d, but don't know what to do with it",
1365 if (options
& DHCPCD_TEST
)
1368 /* As drop_config could re-arrange the order, we do it like this. */
1370 /* Be sane and drop the last config first */
1372 for (iface
= ifaces
; iface
; iface
= iface
->next
) {
1373 if (iface
->next
== NULL
)
1379 if (iface
->carrier
!= LINK_DOWN
&&
1381 iface
->state
->options
->options
& DHCPCD_RELEASE
))
1382 send_release(iface
);
1383 stop_interface(iface
);
1389 reconf_reboot(struct interface
*iface
, int argc
, char **argv
)
1391 const struct if_options
*ifo
;
1394 ifo
= iface
->state
->options
;
1396 configure_interface(iface
, argc
, argv
);
1397 ifo
= iface
->state
->options
;
1398 iface
->state
->interval
= 0;
1399 if ((ifo
->options
& (DHCPCD_INFORM
| DHCPCD_STATIC
) &&
1400 iface
->addr
.s_addr
!= ifo
->req_addr
.s_addr
) ||
1401 (opt
& (DHCPCD_INFORM
| DHCPCD_STATIC
) &&
1402 !(ifo
->options
& (DHCPCD_INFORM
| DHCPCD_STATIC
))))
1404 drop_config(iface
, "EXPIRE");
1406 free(iface
->state
->offer
);
1407 iface
->state
->offer
= NULL
;
1409 start_interface(iface
);
1413 handle_args(struct fd_list
*fd
, int argc
, char **argv
)
1415 struct interface
*ifs
, *ifp
, *ifl
, *ifn
, *ift
;
1416 int do_exit
= 0, do_release
= 0, do_reboot
= 0, do_reconf
= 0;
1420 struct iovec iov
[2];
1424 /* Special commands for our control socket */
1425 if (strcmp(*argv
, "--version") == 0) {
1426 len
= strlen(VERSION
) + 1;
1427 iov
[0].iov_base
= &len
;
1428 iov
[0].iov_len
= sizeof(ssize_t
);
1429 iov
[1].iov_base
= UNCONST(VERSION
);
1430 iov
[1].iov_len
= len
;
1431 if (writev(fd
->fd
, iov
, 2) == -1) {
1432 syslog(LOG_ERR
, "writev: %m");
1436 } else if (strcmp(*argv
, "--getconfigfile") == 0) {
1437 len
= strlen(cffile
? cffile
: CONFIG
) + 1;
1438 iov
[0].iov_base
= &len
;
1439 iov
[0].iov_len
= sizeof(ssize_t
);
1440 iov
[1].iov_base
= cffile
? cffile
: UNCONST(CONFIG
);
1441 iov
[1].iov_len
= len
;
1442 if (writev(fd
->fd
, iov
, 2) == -1) {
1443 syslog(LOG_ERR
, "writev: %m");
1447 } else if (strcmp(*argv
, "--getinterfaces") == 0) {
1450 for (ifp
= ifaces
; ifp
; ifp
= ifp
->next
)
1452 len
= write(fd
->fd
, &len
, sizeof(len
));
1453 if (len
!= sizeof(len
))
1455 for (ifp
= ifaces
; ifp
; ifp
= ifp
->next
)
1456 send_interface(fd
->fd
, ifp
);
1460 while (argv
[++opt
] != NULL
) {
1461 for (ifp
= ifaces
; ifp
; ifp
= ifp
->next
)
1462 if (strcmp(argv
[opt
], ifp
->name
) == 0)
1465 len
= write(fd
->fd
, &len
, sizeof(len
));
1466 if (len
!= sizeof(len
))
1469 while (argv
[++opt
] != NULL
) {
1470 for (ifp
= ifaces
; ifp
; ifp
= ifp
->next
)
1471 if (strcmp(argv
[opt
], ifp
->name
) == 0)
1472 send_interface(fd
->fd
, ifp
);
1475 } else if (strcmp(*argv
, "--listen") == 0) {
1481 /* Log the command */
1483 for (opt
= 0; opt
< argc
; opt
++)
1484 len
+= strlen(argv
[opt
]) + 1;
1485 tmp
= p
= xmalloc(len
+ 1);
1486 for (opt
= 0; opt
< argc
; opt
++) {
1487 l
= strlen(argv
[opt
]);
1488 strlcpy(p
, argv
[opt
], l
+ 1);
1493 syslog(LOG_INFO
, "control command: %s", tmp
);
1497 while ((opt
= getopt_long(argc
, argv
, IF_OPTS
, cf_options
, &oi
)) != -1)
1515 /* We need at least one interface */
1516 if (optind
== argc
) {
1517 syslog(LOG_ERR
, "handle_args: no interface");
1521 if (do_release
|| do_exit
) {
1522 for (oi
= optind
; oi
< argc
; oi
++) {
1523 for (ifp
= ifaces
; ifp
; ifp
= ifp
->next
)
1524 if (strcmp(ifp
->name
, argv
[oi
]) == 0)
1529 ifp
->state
->options
->options
|= DHCPCD_RELEASE
;
1530 if (ifp
->state
->options
->options
& DHCPCD_RELEASE
&&
1531 ifp
->carrier
!= LINK_DOWN
)
1533 stop_interface(ifp
);
1538 if ((ifs
= discover_interfaces(argc
- optind
, argv
+ optind
))) {
1539 for (ifp
= ifs
; ifp
&& (ift
= ifp
->next
, 1); ifp
= ift
) {
1541 for (ifn
= ifaces
; ifn
; ifn
= ifn
->next
) {
1542 if (strcmp(ifn
->name
, ifp
->name
) == 0)
1548 reconf_reboot(ifn
, argc
, argv
);
1549 else if (do_reconf
&& ifn
->state
->new)
1551 free_interface(ifp
);
1554 init_state(ifp
, argc
, argv
);
1555 start_interface(ifp
);
1568 main(int argc
, char **argv
)
1570 struct if_options
*ifo
;
1571 struct interface
*iface
;
1572 int opt
, oi
= 0, signal_fd
, sig
= 0, i
, control_fd
;
1578 openlog(PACKAGE
, LOG_PERROR
, LOG_DAEMON
);
1579 setlogmask(LOG_UPTO(LOG_INFO
));
1581 /* Test for --help and --version */
1583 if (strcmp(argv
[1], "--help") == 0) {
1586 } else if (strcmp(argv
[1], "--version") == 0) {
1587 printf(""PACKAGE
" "VERSION
"\n%s\n", copyright
);
1593 while ((opt
= getopt_long(argc
, argv
, IF_OPTS
, cf_options
, &oi
)) != -1)
1625 ifo
= read_config(cffile
, NULL
, NULL
, NULL
);
1626 opt
= add_options(ifo
, argc
, argv
);
1632 options
= ifo
->options
;
1634 options
|= DHCPCD_TEST
| DHCPCD_PERSISTENT
;
1635 options
&= ~DHCPCD_DAEMONISE
;
1638 #ifdef THERE_IS_NO_FORK
1639 options
&= ~DHCPCD_DAEMONISE
;
1642 if (options
& DHCPCD_DEBUG
)
1643 setlogmask(LOG_UPTO(LOG_DEBUG
));
1644 else if (options
& DHCPCD_QUIET
)
1645 close(STDERR_FILENO
);
1647 if (!(options
& DHCPCD_TEST
)) {
1648 /* If we have any other args, we should run as a single dhcpcd
1649 * instance for that interface. */
1650 len
= strlen(PIDFILE
) + IF_NAMESIZE
+ 2;
1651 pidfile
= xmalloc(len
);
1652 if (optind
== argc
- 1)
1653 snprintf(pidfile
, len
, PIDFILE
, "-", argv
[optind
]);
1655 snprintf(pidfile
, len
, PIDFILE
, "", "");
1656 options
|= DHCPCD_MASTER
;
1660 if (chdir("/") == -1)
1661 syslog(LOG_ERR
, "chdir `/': %m");
1664 if (!(options
& (DHCPCD_MASTER
| DHCPCD_TEST
))) {
1665 control_fd
= open_control();
1666 if (control_fd
!= -1) {
1668 "sending commands to master dhcpcd process");
1669 i
= send_control(argc
, argv
);
1671 syslog(LOG_DEBUG
, "send OK");
1674 syslog(LOG_ERR
, "failed to send commands");
1678 if (errno
!= ENOENT
)
1679 syslog(LOG_ERR
, "open_control: %m");
1685 PACKAGE
" will not work correctly unless run as root");
1690 syslog(LOG_INFO
, "sending signal %d to pid %d",
1692 if (pid
== 0 || kill(pid
, sig
) != 0) {
1694 syslog(LOG_ERR
, ""PACKAGE
" not running");
1695 if (pid
!= 0 && errno
!= ESRCH
) {
1696 syslog(LOG_ERR
, "kill: %m");
1705 /* Spin until it exits */
1706 syslog(LOG_INFO
, "waiting for pid %d to exit", pid
);
1708 ts
.tv_nsec
= 100000000; /* 10th of a second */
1709 for(i
= 0; i
< 100; i
++) {
1710 nanosleep(&ts
, NULL
);
1711 if (read_pid() == 0)
1714 syslog(LOG_ERR
, "pid %d failed to exit", pid
);
1719 if (!(options
& DHCPCD_TEST
)) {
1720 if ((pid
= read_pid()) > 0 &&
1723 syslog(LOG_ERR
, ""PACKAGE
1724 " already running on pid %d (%s)",
1729 /* Ensure we have the needed directories */
1730 if (mkdir(RUNDIR
, 0755) == -1 && errno
!= EEXIST
) {
1731 syslog(LOG_ERR
, "mkdir `%s': %m", RUNDIR
);
1734 if (mkdir(DBDIR
, 0755) == -1 && errno
!= EEXIST
) {
1735 syslog(LOG_ERR
, "mkdir `%s': %m", DBDIR
);
1739 pidfd
= open(pidfile
, O_WRONLY
| O_CREAT
| O_NONBLOCK
, 0664);
1741 syslog(LOG_ERR
, "open `%s': %m", pidfile
);
1744 /* Lock the file so that only one instance of dhcpcd runs
1745 * on an interface */
1746 if (flock(pidfd
, LOCK_EX
| LOCK_NB
) == -1) {
1747 syslog(LOG_ERR
, "flock `%s': %m", pidfile
);
1750 if (set_cloexec(pidfd
) == -1)
1752 writepid(pidfd
, getpid());
1755 syslog(LOG_INFO
, "version " VERSION
" starting");
1757 if ((signal_fd
= signal_init()) == -1)
1759 if (signal_setup() == -1)
1761 add_event(signal_fd
, handle_signal
, NULL
);
1763 if (options
& DHCPCD_MASTER
) {
1764 if (start_control() == -1) {
1765 syslog(LOG_ERR
, "start_control: %m");
1770 if (init_sockets() == -1) {
1771 syslog(LOG_ERR
, "init_socket: %m");
1774 if (ifo
->options
& DHCPCD_LINK
) {
1775 linkfd
= open_link_socket();
1777 syslog(LOG_ERR
, "open_link_socket: %m");
1779 add_event(linkfd
, handle_link
, NULL
);
1782 ifc
= argc
- optind
;
1783 ifv
= argv
+ optind
;
1785 /* When running dhcpcd against a single interface, we need to retain
1786 * the old behaviour of waiting for an IP address */
1788 options
|= DHCPCD_WAITIP
;
1790 ifaces
= discover_interfaces(ifc
, ifv
);
1791 for (i
= 0; i
< ifc
; i
++) {
1792 for (iface
= ifaces
; iface
; iface
= iface
->next
)
1793 if (strcmp(iface
->name
, ifv
[i
]) == 0)
1796 syslog(LOG_ERR
, "%s: interface not found or invalid",
1801 syslog(LOG_ERR
, "no valid interfaces found");
1804 if (!(options
& DHCPCD_LINK
)) {
1806 "aborting as link detection is disabled");
1811 if (options
& DHCPCD_BACKGROUND
)
1815 for (iface
= ifaces
; iface
; iface
= iface
->next
) {
1816 init_state(iface
, argc
, argv
);
1817 if (iface
->carrier
!= LINK_DOWN
)
1821 if (!(options
& DHCPCD_BACKGROUND
)) {
1822 /* If we don't have a carrier, we may have to wait for a second
1823 * before one becomes available if we brought an interface up. */
1825 options
& DHCPCD_LINK
&&
1826 options
& DHCPCD_WAITUP
&&
1827 !(options
& DHCPCD_WAITIP
))
1831 nanosleep(&ts
, NULL
);
1832 for (iface
= ifaces
; iface
; iface
= iface
->next
) {
1833 handle_carrier(iface
->name
);
1834 if (iface
->carrier
!= LINK_DOWN
) {
1841 options
& DHCPCD_LINK
&&
1842 !(options
& DHCPCD_WAITIP
))
1844 syslog(LOG_WARNING
, "no interfaces have a carrier");
1846 } else if (options
& DHCPCD_DAEMONISE
&& ifo
->timeout
> 0) {
1847 if (options
& DHCPCD_IPV4LL
)
1848 options
|= DHCPCD_TIMEOUT_IPV4LL
;
1849 add_timeout_sec(ifo
->timeout
, handle_exit_timeout
, NULL
);
1856 for (iface
= ifaces
; iface
; iface
= iface
->next
)
1857 add_timeout_sec(0, start_interface
, iface
);