Expand PMF_FN_* macros.
[netbsd-mini2440.git] / external / bsd / dhcpcd / dist / dhcpcd.c
blob4e095e05c81f656563aca91d246ac59ea59ccf24
1 /*
2 * dhcpcd - DHCP client daemon
3 * Copyright (c) 2006-2009 Roy Marples <roy@marples.name>
4 * All rights reserved
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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
25 * SUCH DAMAGE.
28 const char copyright[] = "Copyright (c) 2006-2009 Roy Marples";
30 #include <sys/file.h>
31 #include <sys/socket.h>
32 #include <sys/stat.h>
33 #include <sys/time.h>
34 #include <sys/types.h>
35 #include <sys/uio.h>
37 #include <arpa/inet.h>
38 #include <net/route.h>
40 #ifdef __linux__
41 # include <linux/rtnetlink.h>
42 #endif
44 #include <ctype.h>
45 #include <errno.h>
46 #include <getopt.h>
47 #include <limits.h>
48 #include <paths.h>
49 #include <signal.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <syslog.h>
54 #include <unistd.h>
55 #include <time.h>
57 #include "arp.h"
58 #include "bind.h"
59 #include "config.h"
60 #include "common.h"
61 #include "configure.h"
62 #include "control.h"
63 #include "dhcpcd.h"
64 #include "duid.h"
65 #include "eloop.h"
66 #include "if-options.h"
67 #include "if-pref.h"
68 #include "ipv4ll.h"
69 #include "net.h"
70 #include "signals.h"
72 /* We should define a maximum for the NAK exponential backoff */
73 #define NAKOFF_MAX 60
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
80 int options = 0;
81 int pidfd = -1;
82 struct interface *ifaces = NULL;
83 int ifac = 0;
84 char **ifav = NULL;
85 int ifdc = 0;
86 char **ifdv = NULL;
88 static char **margv;
89 static int margc;
90 static char **ifv;
91 static int ifc;
92 static char *cffile;
93 static char *pidfile;
94 static int linkfd = -1;
96 struct dhcp_op {
97 uint8_t value;
98 const char *name;
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" },
110 { 0, NULL }
113 static void send_release(struct interface *);
115 static const char *
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)
122 return d->name;
123 return NULL;
126 static pid_t
127 read_pid(void)
129 FILE *fp;
130 pid_t pid;
132 if ((fp = fopen(pidfile, "r")) == NULL) {
133 errno = ENOENT;
134 return 0;
136 if (fscanf(fp, "%d", &pid) != 1)
137 pid = 0;
138 fclose(fp);
139 return pid;
142 static void
143 usage(void)
145 printf("usage: "PACKAGE" [-dgknpqwxyADEGHKLOTV] [-c script] [-f file]"
146 " [-e var=val]\n"
147 " [-h hostname] [-i classID ] [-l leasetime]"
148 " [-m metric] [-o option]\n"
149 " [-r ipaddr] [-s ipaddr] [-t timeout]"
150 " [-u userclass]\n"
151 " [-F none|ptr|both] [-I clientID] [-C hookscript]"
152 " [-Q option]\n"
153 " [-X ipaddr] <interface>\n");
156 static void
157 cleanup(void)
159 #ifdef DEBUG_MEMORY
160 struct interface *iface;
161 int i;
163 while (ifaces) {
164 iface = ifaces;
165 ifaces = iface->next;
166 free_interface(iface);
169 for (i = 0; i < ifac; i++)
170 free(ifav[i]);
171 free(ifav);
172 for (i = 0; i < ifdc; i++)
173 free(ifdv[i]);
174 free(ifdv);
175 #endif
177 if (linkfd != -1)
178 close(linkfd);
179 if (pidfd > -1) {
180 if (options & DHCPCD_MASTER) {
181 if (stop_control() == -1)
182 syslog(LOG_ERR, "stop_control: %m");
184 close(pidfd);
185 unlink(pidfile);
187 #ifdef DEBUG_MEMORY
188 free(pidfile);
189 #endif
192 /* ARGSUSED */
193 void
194 handle_exit_timeout(_unused void *arg)
196 int timeout;
198 syslog(LOG_ERR, "timed out");
199 if (!(options & DHCPCD_TIMEOUT_IPV4LL))
200 exit(EXIT_FAILURE);
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);
207 void
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;
214 configure(iface);
215 free(iface->state->old);
216 iface->state->old = NULL;
217 iface->state->lease.addr.s_addr = 0;
220 void
221 close_sockets(struct interface *iface)
223 if (iface->arp_fd != -1) {
224 delete_event(iface->arp_fd);
225 close(iface->arp_fd);
226 iface->arp_fd = -1;
228 if (iface->raw_fd != -1) {
229 delete_event(iface->raw_fd);
230 close(iface->raw_fd);
231 iface->raw_fd = -1;
233 if (iface->udp_fd != -1) {
234 close(iface->udp_fd);
235 iface->udp_fd = -1;
239 struct interface *
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)
246 return ifp;
247 return NULL;
250 static void
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) {
261 if (ifp == iface)
262 break;
263 ifl = ifp;
265 if (ifl)
266 ifl->next = ifp->next;
267 else
268 ifaces = ifp->next;
269 free_interface(ifp);
270 if (!(options & (DHCPCD_MASTER | DHCPCD_TEST)))
271 exit(EXIT_FAILURE);
274 static void
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;
281 uint8_t *udp;
282 ssize_t len, r;
283 struct in_addr from, to;
284 in_addr_t a = 0;
285 struct timeval tv;
287 if (!callback)
288 syslog(LOG_DEBUG, "%s: sending %s with xid 0x%x",
289 iface->name, get_dhcp_op(type), state->xid);
290 else {
291 if (state->interval == 0)
292 state->interval = 4;
293 else {
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);
300 syslog(LOG_DEBUG,
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);
316 if (a)
317 iface->addr.s_addr = a;
318 from.s_addr = dhcp->ciaddr;
319 if (from.s_addr)
320 to.s_addr = state->lease.server.s_addr;
321 else
322 to.s_addr = 0;
323 if (to.s_addr && to.s_addr != INADDR_BROADCAST) {
324 r = send_packet(iface, to, (uint8_t *)dhcp, len);
325 if (r == -1)
326 syslog(LOG_ERR, "%s: send_packet: %m", iface->name);
327 } else {
328 len = make_udp_packet(&udp, (uint8_t *)dhcp, len, from, to);
329 r = send_raw_packet(iface, ETHERTYPE_IP, udp, len);
330 free(udp);
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. */
336 if (r == -1) {
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);
342 callback = NULL;
345 free(dhcp);
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. */
348 if (callback)
349 add_timeout_tv(&tv, callback, iface);
352 static void
353 send_inform(void *arg)
355 send_message((struct interface *)arg, DHCP_INFORM, send_inform);
358 static void
359 send_discover(void *arg)
361 send_message((struct interface *)arg, DHCP_DISCOVER, send_discover);
364 static void
365 send_request(void *arg)
367 send_message((struct interface *)arg, DHCP_REQUEST, send_request);
370 static void
371 send_renew(void *arg)
373 send_message((struct interface *)arg, DHCP_REQUEST, send_renew);
376 static void
377 send_rebind(void *arg)
379 send_message((struct interface *)arg, DHCP_REQUEST, send_rebind);
382 void
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);
391 return;
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);
402 static void
403 log_dhcp(int lvl, const char *msg,
404 const struct interface *iface, const struct dhcp_message *dhcp)
406 char *a;
407 struct in_addr addr;
408 int r;
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));
415 } else
416 a = NULL;
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);
421 else if (r == 0) {
422 if (a == NULL)
423 syslog(lvl, "%s: %s from %s",
424 iface->name, msg, inet_ntoa(addr));
425 else
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);
430 else
431 syslog(lvl, "%s: %s", iface->name, msg);
432 free(a);
435 static int
436 blacklisted_ip(const struct if_options *ifo, in_addr_t addr)
438 size_t i;
440 for (i = 0; i < ifo->blacklist_len; i += 2)
441 if (ifo->blacklist[i] == (addr & ifo->blacklist[i + 1]))
442 return 1;
443 return 0;
446 static int
447 whitelisted_ip(const struct if_options *ifo, in_addr_t addr)
449 size_t i;
451 if (ifo->whitelist_len == 0)
452 return -1;
453 for (i = 0; i < ifo->whitelist_len; i += 2)
454 if (ifo->whitelist[i] == (addr & ifo->whitelist[i + 1]))
455 return 1;
456 return 0;
459 static void
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;
466 uint8_t type, tmp;
467 struct in_addr addr;
468 size_t i;
470 /* reset the message counter */
471 state->interval = 0;
473 /* We may have found a BOOTP server */
474 if (get_option_uint8(&type, dhcp, DHO_MESSAGETYPE) == -1)
475 type = 0;
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);
483 return;
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);
493 iface->raw_fd = -1;
494 close(iface->udp_fd);
495 iface->udp_fd = -1;
496 /* If we constantly get NAKS then we should slowly back off */
497 add_timeout_sec(state->nakoff, start_interface, iface);
498 state->nakoff *= 2;
499 if (state->nakoff > NAKOFF_MAX)
500 state->nakoff = NAKOFF_MAX;
501 return;
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)
512 continue;
513 log_dhcp(LOG_WARNING, "reject DHCP", iface, dhcp);
514 return;
518 /* No NAK, so reset the backoff */
519 state->nakoff = 1;
521 if ((type == 0 || type == DHCP_OFFER) &&
522 state->state == DHS_DISCOVER)
524 lease->frominfo = 0;
525 lease->addr.s_addr = dhcp->yiaddr;
526 if (type == 0 ||
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);
530 free(state->offer);
531 state->offer = dhcp;
532 *dhcpp = NULL;
533 if (options & DHCPCD_TEST) {
534 free(state->old);
535 state->old = state->new;
536 state->new = state->offer;
537 state->offer = NULL;
538 state->reason = "TEST";
539 run_script(iface);
540 exit(EXIT_SUCCESS);
542 delete_timeout(send_discover, iface);
543 /* We don't request BOOTP addresses */
544 if (type) {
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
550 * DISCOVER. */
551 start_request(iface);
552 return;
556 if (type) {
557 if (type == DHCP_OFFER) {
558 log_dhcp(LOG_INFO, "ignoring offer of", iface, dhcp);
559 return;
562 /* We should only be dealing with acks */
563 if (type != DHCP_ACK) {
564 log_dhcp(LOG_ERR, "not ACK or OFFER", iface, dhcp);
565 return;
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
573 * have a pointer. */
574 if (*dhcpp) {
575 free(state->offer);
576 state->offer = dhcp;
577 *dhcpp = NULL;
580 lease->frominfo = 0;
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) {
595 state->claims = 0;
596 state->probes = 0;
597 state->conflicts = 0;
598 state->state = DHS_PROBE;
599 send_arp_probe(iface);
600 return;
604 bind_interface(iface);
607 static void
608 handle_dhcp_packet(void *arg)
610 struct interface *iface = arg;
611 uint8_t *packet;
612 struct dhcp_message *dhcp = NULL;
613 const uint8_t *pp;
614 ssize_t bytes;
615 struct in_addr from;
616 int i;
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);
622 for(;;) {
623 bytes = get_raw_packet(iface, ETHERTYPE_IP,
624 packet, udp_dhcp_len);
625 if (bytes == 0 || bytes == -1)
626 break;
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));
630 continue;
632 i = whitelisted_ip(iface->state->options, from.s_addr);
633 if (i == 0) {
634 syslog(LOG_WARNING,
635 "%s: non whitelisted DHCP packet from %s",
636 iface->name, inet_ntoa(from));
637 continue;
638 } else if (i != 1 &&
639 blacklisted_ip(iface->state->options, from.s_addr) == 1)
641 syslog(LOG_WARNING,
642 "%s: blacklisted DHCP packet from %s",
643 iface->name, inet_ntoa(from));
644 continue;
646 if (iface->flags & IFF_POINTOPOINT &&
647 iface->dst.s_addr != from.s_addr)
649 syslog(LOG_WARNING,
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)) {
655 syslog(LOG_ERR,
656 "%s: packet greater than DHCP size from %s",
657 iface->name, inet_ntoa(from));
658 continue;
660 if (!dhcp)
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));
666 continue;
668 /* Ensure it's the right transaction */
669 if (iface->state->xid != dhcp->xid) {
670 syslog(LOG_DEBUG,
671 "%s: wrong xid 0x%x (expecting 0x%x) from %s",
672 iface->name, dhcp->xid, iface->state->xid,
673 inet_ntoa(from));
674 continue;
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)));
683 continue;
685 handle_dhcp(iface, &dhcp);
686 if (iface->raw_fd == -1)
687 break;
689 free(packet);
690 free(dhcp);
693 static void
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);
709 static void
710 send_release(struct interface *iface)
712 struct timespec ts;
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));
719 open_sockets(iface);
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);
731 void
732 send_decline(struct interface *iface)
734 open_sockets(iface);
735 send_message(iface, DHCP_DECLINE, NULL);
738 static void
739 configure_interface1(struct interface *iface)
741 struct if_state *ifs = iface->state;
742 struct if_options *ifo = ifs->options;
743 uint8_t *duid;
744 size_t len = 0, ifl;
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");
773 if (len > 0) {
774 iface->clientid = xmalloc(len + 6);
775 iface->clientid[0] = len + 5;
776 iface->clientid[1] = 255; /* RFC 4361 */
777 ifl = strlen(iface->name);
778 if (ifl < 5) {
779 memcpy(iface->clientid + 2, iface->name, ifl);
780 if (ifl < 4)
781 memset(iface->clientid + 2 + ifl,
782 0, 4 - ifl);
783 } else {
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,
793 iface->hwlen);
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);
807 if (ifo == NULL) {
808 syslog(LOG_DEBUG, "%s: no profile %s", iface->name, profile);
809 return -1;
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);
816 } else
817 *iface->state->profile = '\0';
818 free_options(iface->state->options);
819 iface->state->options = ifo;
820 configure_interface1(iface);
821 return 0;
824 static void
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);
835 static void
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);
843 static void
844 handle_carrier(const char *ifname)
846 struct interface *iface;
847 int carrier;
849 if (!(options & DHCPCD_LINK))
850 return;
851 for (iface = ifaces; iface; iface = iface->next)
852 if (strcmp(iface->name, ifname) == 0)
853 break;
854 if (!iface || !(iface->state->options->options & DHCPCD_LINK))
855 return;
856 carrier = carrier_status(iface);
857 if (carrier == -1)
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);
871 if (iface->wireless)
872 getifssid(iface->name, iface->ssid);
873 configure_interface(iface, margc, margv);
874 iface->state->interval = 0;
875 iface->state->reason = "CARRIER";
876 run_script(iface);
877 start_interface(iface);
882 void
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();
890 open_sockets(iface);
891 delete_timeout(NULL, iface);
892 if (ifo->fallback)
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);
899 else
900 add_timeout_sec(ifo->timeout, start_ipv4ll, iface);
902 syslog(LOG_INFO, "%s: broadcasting for a lease", iface->name);
903 send_discover(iface);
906 void
907 start_request(void *arg)
909 struct interface *iface = arg;
911 iface->state->state = DHS_REQUEST;
912 send_request(iface);
915 void
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();
924 open_sockets(iface);
925 send_renew(iface);
928 void
929 start_rebind(void *arg)
931 struct interface *iface = arg;
933 syslog(LOG_ERR, "%s: failed to renew, attempting to rebind",
934 iface->name);
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)
939 open_sockets(iface);
940 send_rebind(iface);
943 static void
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;
957 uint8_t *p;
959 dhcp = xzalloc(sizeof(*dhcp));
960 dhcp->yiaddr = addr->s_addr;
961 p = dhcp->options;
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);
968 *p++ = DHO_END;
969 return dhcp;
972 static int
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)
980 return 0;
982 if (get_address(iface->name, &addr, &net, &dst) == 1)
983 handle_ifa(RTM_NEWADDR, iface->name, &addr, &net, &dst);
984 else {
985 syslog(LOG_INFO,
986 "%s: waiting for 3rd party to configure IP address",
987 iface->name);
988 iface->state->reason = "3RDPARTY";
989 run_script(iface);
991 return 1;
994 static void
995 start_static(struct interface *iface)
997 struct if_options *ifo;
999 if (handle_3rdparty(iface))
1000 return;
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);
1008 static void
1009 start_inform(struct interface *iface)
1011 if (handle_3rdparty(iface))
1012 return;
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;
1017 } else {
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);
1025 send_inform(iface);
1028 void
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);
1035 return;
1037 if (ifo->options & DHCPCD_STATIC) {
1038 start_static(iface);
1039 return;
1041 if (ifo->reboot == 0) {
1042 start_discover(iface);
1043 return;
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);
1049 } else
1050 start_discover(iface);
1051 return;
1053 if (ifo->options & DHCPCD_INFORM) {
1054 syslog(LOG_INFO, "%s: informing address of %s",
1055 iface->name, inet_ntoa(iface->state->lease.addr));
1056 } else {
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);
1064 if (ifo->fallback)
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)
1075 send_inform(iface);
1076 else
1077 send_request(iface);
1080 void
1081 start_interface(void *arg)
1083 struct interface *iface = arg;
1084 struct if_options *ifo = iface->state->options;
1085 struct stat st;
1086 struct timeval now;
1087 uint32_t l;
1089 handle_carrier(iface->name);
1090 if (iface->carrier == LINK_DOWN) {
1091 syslog(LOG_INFO, "%s: waiting for carrier", iface->name);
1092 return;
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);
1101 return;
1103 if (ifo->options & DHCPCD_STATIC) {
1104 start_static(iface);
1105 return;
1107 if (ifo->options & DHCPCD_INFORM) {
1108 start_inform(iface);
1109 return;
1111 if (iface->hwlen == 0 && ifo->clientid[0] == '\0') {
1112 syslog(LOG_WARNING, "%s: needs a clientid to configure",
1113 iface->name);
1114 drop_config(iface, "FAIL");
1115 close_sockets(iface);
1116 delete_timeout(NULL, iface);
1117 return;
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;
1124 else {
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);
1129 configure(iface);
1130 start_inform(iface);
1131 return;
1133 } else
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 ==
1140 iface->addr.s_addr)
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;
1154 } else {
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);
1167 else
1168 start_reboot(iface);
1171 static void
1172 init_state(struct interface *iface, int argc, char **argv)
1174 struct if_state *ifs;
1176 if (iface->state)
1177 ifs = iface->state;
1178 else
1179 ifs = iface->state = xzalloc(sizeof(*ifs));
1181 ifs->state = DHS_INIT;
1182 ifs->reason = "PREINIT";
1183 ifs->nakoff = 1;
1184 configure_interface(iface, argc, argv);
1185 if (!(options & DHCPCD_TEST))
1186 run_script(iface);
1188 if (ifs->options->options & DHCPCD_LINK) {
1189 switch (carrier_status(iface)) {
1190 case 0:
1191 iface->carrier = LINK_DOWN;
1192 ifs->reason = "NOCARRIER";
1193 break;
1194 case 1:
1195 iface->carrier = LINK_UP;
1196 ifs->reason = "CARRIER";
1197 break;
1198 default:
1199 iface->carrier = LINK_UNKNOWN;
1200 return;
1202 if (!(options & DHCPCD_TEST))
1203 run_script(iface);
1204 } else
1205 iface->carrier = LINK_UNKNOWN;
1208 void
1209 handle_interface(int action, const char *ifname)
1211 struct interface *ifs, *ifp, *ifn, *ifl = NULL;
1212 const char * const argv[] = { ifname };
1213 int i;
1215 if (action == -1) {
1216 ifp = find_interface(ifname);
1217 if (ifp != NULL)
1218 stop_interface(ifp);
1219 return;
1220 } else if (action == 0) {
1221 handle_carrier(ifname);
1222 return;
1225 /* If running off an interface list, check it's in it. */
1226 if (ifc) {
1227 for (i = 0; i < ifc; i++)
1228 if (strcmp(ifv[i], ifname) == 0)
1229 break;
1230 if (i >= ifc)
1231 return;
1234 ifs = discover_interfaces(-1, UNCONST(argv));
1235 for (ifp = ifs; ifp; ifp = ifp->next) {
1236 if (strcmp(ifp->name, ifname) != 0)
1237 continue;
1238 /* Check if we already have the interface */
1239 for (ifn = ifaces; ifn; ifn = ifn->next) {
1240 if (strcmp(ifn->name, ifp->name) == 0)
1241 break;
1242 ifl = ifn;
1244 if (ifn) {
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);
1250 } else {
1251 if (ifl)
1252 ifl->next = ifp;
1253 else
1254 ifaces = ifp;
1256 init_state(ifp, 0, NULL);
1257 start_interface(ifp);
1261 void
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;
1267 int i;
1269 if (addr->s_addr == INADDR_ANY)
1270 return;
1271 for (ifp = ifaces; ifp; ifp = ifp->next)
1272 if (strcmp(ifp->name, ifname) == 0)
1273 break;
1274 if (ifp == NULL)
1275 return;
1276 ifo = ifp->state->options;
1277 if ((ifo->options & (DHCPCD_INFORM | DHCPCD_STATIC)) == 0 ||
1278 ifo->req_addr.s_addr != INADDR_ANY)
1279 return;
1281 switch (type) {
1282 case RTM_DELADDR:
1283 if (ifp->state->new &&
1284 ifp->state->new->yiaddr == addr->s_addr)
1285 drop_config(ifp, "EXPIRE");
1286 break;
1287 case RTM_NEWADDR:
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;
1292 if (dst) {
1293 for (i = 1; i < 255; i++)
1294 if (i != DHO_ROUTER &&
1295 has_option_mask(ifo->dstmask, i))
1296 dhcp_message_add_addr(
1297 ifp->state->new,
1298 i, *dst);
1300 ifp->state->reason = "STATIC";
1301 build_routes();
1302 run_script(ifp);
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;
1308 ifp->addr = *addr;
1309 ifp->net = *net;
1310 open_sockets(ifp);
1311 send_inform(ifp);
1313 break;
1317 /* ARGSUSED */
1318 static void
1319 handle_link(_unused void *arg)
1321 if (manage_link(linkfd) == -1)
1322 syslog(LOG_ERR, "manage_link: %m");
1325 /* ARGSUSED */
1326 static void
1327 handle_signal(_unused void *arg)
1329 struct interface *iface, *ifl;
1330 int sig = signal_read();
1331 int do_release = 0;
1333 switch (sig) {
1334 case SIGINT:
1335 syslog(LOG_INFO, "received SIGINT, stopping");
1336 break;
1337 case SIGTERM:
1338 syslog(LOG_INFO, "received SIGTERM, stopping");
1339 break;
1340 case SIGALRM:
1341 syslog(LOG_INFO, "received SIGALRM, rebinding lease");
1342 for (iface = ifaces; iface; iface = iface->next)
1343 start_interface(iface);
1344 return;
1345 case SIGHUP:
1346 syslog(LOG_INFO, "received SIGHUP, releasing lease");
1347 do_release = 1;
1348 break;
1349 case SIGUSR1:
1350 syslog(LOG_INFO, "received SIGUSR, reconfiguring");
1351 for (iface = ifaces; iface; iface = iface->next)
1352 if (iface->state->new)
1353 configure(iface);
1354 return;
1355 case SIGPIPE:
1356 syslog(LOG_WARNING, "received SIGPIPE");
1357 return;
1358 default:
1359 syslog(LOG_ERR,
1360 "received signal %d, but don't know what to do with it",
1361 sig);
1362 return;
1365 if (options & DHCPCD_TEST)
1366 exit(EXIT_FAILURE);
1368 /* As drop_config could re-arrange the order, we do it like this. */
1369 for (;;) {
1370 /* Be sane and drop the last config first */
1371 ifl = NULL;
1372 for (iface = ifaces; iface; iface = iface->next) {
1373 if (iface->next == NULL)
1374 break;
1375 ifl = iface;
1377 if (iface == NULL)
1378 break;
1379 if (iface->carrier != LINK_DOWN &&
1380 (do_release ||
1381 iface->state->options->options & DHCPCD_RELEASE))
1382 send_release(iface);
1383 stop_interface(iface);
1385 exit(EXIT_FAILURE);
1388 static void
1389 reconf_reboot(struct interface *iface, int argc, char **argv)
1391 const struct if_options *ifo;
1392 int opt;
1394 ifo = iface->state->options;
1395 opt = ifo->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");
1405 } else {
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;
1417 int opt, oi = 0;
1418 ssize_t len;
1419 size_t l;
1420 struct iovec iov[2];
1421 char *tmp, *p;
1423 if (fd != NULL) {
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");
1433 return -1;
1435 return 0;
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");
1444 return -1;
1446 return 0;
1447 } else if (strcmp(*argv, "--getinterfaces") == 0) {
1448 len = 0;
1449 if (argc == 1) {
1450 for (ifp = ifaces; ifp; ifp = ifp->next)
1451 len++;
1452 len = write(fd->fd, &len, sizeof(len));
1453 if (len != sizeof(len))
1454 return -1;
1455 for (ifp = ifaces; ifp; ifp = ifp->next)
1456 send_interface(fd->fd, ifp);
1457 return 0;
1459 opt = 0;
1460 while (argv[++opt] != NULL) {
1461 for (ifp = ifaces; ifp; ifp = ifp->next)
1462 if (strcmp(argv[opt], ifp->name) == 0)
1463 len++;
1465 len = write(fd->fd, &len, sizeof(len));
1466 if (len != sizeof(len))
1467 return -1;
1468 opt = 0;
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);
1474 return 0;
1475 } else if (strcmp(*argv, "--listen") == 0) {
1476 fd->listener = 1;
1477 return 0;
1481 /* Log the command */
1482 len = 0;
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);
1489 p += l;
1490 *p++ = ' ';
1492 *--p = '\0';
1493 syslog(LOG_INFO, "control command: %s", tmp);
1494 free(tmp);
1496 optind = 0;
1497 while ((opt = getopt_long(argc, argv, IF_OPTS, cf_options, &oi)) != -1)
1499 switch (opt) {
1500 case 'g':
1501 do_reconf = 1;
1502 break;
1503 case 'k':
1504 do_release = 1;
1505 break;
1506 case 'n':
1507 do_reboot = 1;
1508 break;
1509 case 'x':
1510 do_exit = 1;
1511 break;
1515 /* We need at least one interface */
1516 if (optind == argc) {
1517 syslog(LOG_ERR, "handle_args: no interface");
1518 return -1;
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)
1525 break;
1526 if (!ifp)
1527 continue;
1528 if (do_release)
1529 ifp->state->options->options |= DHCPCD_RELEASE;
1530 if (ifp->state->options->options & DHCPCD_RELEASE &&
1531 ifp->carrier != LINK_DOWN)
1532 send_release(ifp);
1533 stop_interface(ifp);
1535 return 0;
1538 if ((ifs = discover_interfaces(argc - optind, argv + optind))) {
1539 for (ifp = ifs; ifp && (ift = ifp->next, 1); ifp = ift) {
1540 ifl = NULL;
1541 for (ifn = ifaces; ifn; ifn = ifn->next) {
1542 if (strcmp(ifn->name, ifp->name) == 0)
1543 break;
1544 ifl = ifn;
1546 if (ifn) {
1547 if (do_reboot)
1548 reconf_reboot(ifn, argc, argv);
1549 else if (do_reconf && ifn->state->new)
1550 configure(ifn);
1551 free_interface(ifp);
1552 } else {
1553 ifp->next = NULL;
1554 init_state(ifp, argc, argv);
1555 start_interface(ifp);
1556 if (ifl)
1557 ifl->next = ifp;
1558 else
1559 ifaces = ifp;
1562 sort_interfaces();
1564 return 0;
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;
1573 size_t len;
1574 pid_t pid;
1575 struct timespec ts;
1577 closefrom(3);
1578 openlog(PACKAGE, LOG_PERROR, LOG_DAEMON);
1579 setlogmask(LOG_UPTO(LOG_INFO));
1581 /* Test for --help and --version */
1582 if (argc > 1) {
1583 if (strcmp(argv[1], "--help") == 0) {
1584 usage();
1585 exit(EXIT_SUCCESS);
1586 } else if (strcmp(argv[1], "--version") == 0) {
1587 printf(""PACKAGE" "VERSION"\n%s\n", copyright);
1588 exit(EXIT_SUCCESS);
1592 i = 0;
1593 while ((opt = getopt_long(argc, argv, IF_OPTS, cf_options, &oi)) != -1)
1595 switch (opt) {
1596 case 'f':
1597 cffile = optarg;
1598 break;
1599 case 'g':
1600 sig = SIGUSR1;
1601 break;
1602 case 'k':
1603 sig = SIGHUP;
1604 break;
1605 case 'n':
1606 sig = SIGALRM;
1607 break;
1608 case 'x':
1609 sig = SIGTERM;
1610 break;
1611 case 'T':
1612 i = 1;
1613 break;
1614 case 'V':
1615 print_options();
1616 exit(EXIT_SUCCESS);
1617 case '?':
1618 usage();
1619 exit(EXIT_FAILURE);
1623 margv = argv;
1624 margc = argc;
1625 ifo = read_config(cffile, NULL, NULL, NULL);
1626 opt = add_options(ifo, argc, argv);
1627 if (opt != 1) {
1628 if (opt == 0)
1629 usage();
1630 exit(EXIT_FAILURE);
1632 options = ifo->options;
1633 if (i != 0) {
1634 options |= DHCPCD_TEST | DHCPCD_PERSISTENT;
1635 options &= ~DHCPCD_DAEMONISE;
1638 #ifdef THERE_IS_NO_FORK
1639 options &= ~DHCPCD_DAEMONISE;
1640 #endif
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]);
1654 else {
1655 snprintf(pidfile, len, PIDFILE, "", "");
1656 options |= DHCPCD_MASTER;
1660 if (chdir("/") == -1)
1661 syslog(LOG_ERR, "chdir `/': %m");
1662 atexit(cleanup);
1664 if (!(options & (DHCPCD_MASTER | DHCPCD_TEST))) {
1665 control_fd = open_control();
1666 if (control_fd != -1) {
1667 syslog(LOG_INFO,
1668 "sending commands to master dhcpcd process");
1669 i = send_control(argc, argv);
1670 if (i > 0) {
1671 syslog(LOG_DEBUG, "send OK");
1672 exit(EXIT_SUCCESS);
1673 } else {
1674 syslog(LOG_ERR, "failed to send commands");
1675 exit(EXIT_FAILURE);
1677 } else {
1678 if (errno != ENOENT)
1679 syslog(LOG_ERR, "open_control: %m");
1683 if (geteuid())
1684 syslog(LOG_WARNING,
1685 PACKAGE " will not work correctly unless run as root");
1687 if (sig != 0) {
1688 pid = read_pid();
1689 if (pid != 0)
1690 syslog(LOG_INFO, "sending signal %d to pid %d",
1691 sig, pid);
1692 if (pid == 0 || kill(pid, sig) != 0) {
1693 if (sig != SIGALRM)
1694 syslog(LOG_ERR, ""PACKAGE" not running");
1695 if (pid != 0 && errno != ESRCH) {
1696 syslog(LOG_ERR, "kill: %m");
1697 exit(EXIT_FAILURE);
1699 unlink(pidfile);
1700 if (sig != SIGALRM)
1701 exit(EXIT_FAILURE);
1702 } else {
1703 if (sig == SIGALRM)
1704 exit(EXIT_SUCCESS);
1705 /* Spin until it exits */
1706 syslog(LOG_INFO, "waiting for pid %d to exit", pid);
1707 ts.tv_sec = 0;
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)
1712 exit(EXIT_SUCCESS);
1714 syslog(LOG_ERR, "pid %d failed to exit", pid);
1715 exit(EXIT_FAILURE);
1719 if (!(options & DHCPCD_TEST)) {
1720 if ((pid = read_pid()) > 0 &&
1721 kill(pid, 0) == 0)
1723 syslog(LOG_ERR, ""PACKAGE
1724 " already running on pid %d (%s)",
1725 pid, pidfile);
1726 exit(EXIT_FAILURE);
1729 /* Ensure we have the needed directories */
1730 if (mkdir(RUNDIR, 0755) == -1 && errno != EEXIST) {
1731 syslog(LOG_ERR, "mkdir `%s': %m", RUNDIR);
1732 exit(EXIT_FAILURE);
1734 if (mkdir(DBDIR, 0755) == -1 && errno != EEXIST) {
1735 syslog(LOG_ERR, "mkdir `%s': %m", DBDIR);
1736 exit(EXIT_FAILURE);
1739 pidfd = open(pidfile, O_WRONLY | O_CREAT | O_NONBLOCK, 0664);
1740 if (pidfd == -1) {
1741 syslog(LOG_ERR, "open `%s': %m", pidfile);
1742 exit(EXIT_FAILURE);
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);
1748 exit(EXIT_FAILURE);
1750 if (set_cloexec(pidfd) == -1)
1751 exit(EXIT_FAILURE);
1752 writepid(pidfd, getpid());
1755 syslog(LOG_INFO, "version " VERSION " starting");
1757 if ((signal_fd = signal_init()) == -1)
1758 exit(EXIT_FAILURE);
1759 if (signal_setup() == -1)
1760 exit(EXIT_FAILURE);
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");
1766 exit(EXIT_FAILURE);
1770 if (init_sockets() == -1) {
1771 syslog(LOG_ERR, "init_socket: %m");
1772 exit(EXIT_FAILURE);
1774 if (ifo->options & DHCPCD_LINK) {
1775 linkfd = open_link_socket();
1776 if (linkfd == -1)
1777 syslog(LOG_ERR, "open_link_socket: %m");
1778 else
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 */
1787 if (ifc == 1)
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)
1794 break;
1795 if (!iface)
1796 syslog(LOG_ERR, "%s: interface not found or invalid",
1797 ifv[i]);
1799 if (!ifaces) {
1800 if (ifc == 0)
1801 syslog(LOG_ERR, "no valid interfaces found");
1802 else
1803 exit(EXIT_FAILURE);
1804 if (!(options & DHCPCD_LINK)) {
1805 syslog(LOG_ERR,
1806 "aborting as link detection is disabled");
1807 exit(EXIT_FAILURE);
1811 if (options & DHCPCD_BACKGROUND)
1812 daemonise();
1814 opt = 0;
1815 for (iface = ifaces; iface; iface = iface->next) {
1816 init_state(iface, argc, argv);
1817 if (iface->carrier != LINK_DOWN)
1818 opt = 1;
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. */
1824 if (opt == 0 &&
1825 options & DHCPCD_LINK &&
1826 options & DHCPCD_WAITUP &&
1827 !(options & DHCPCD_WAITIP))
1829 ts.tv_sec = 1;
1830 ts.tv_nsec = 0;
1831 nanosleep(&ts, NULL);
1832 for (iface = ifaces; iface; iface = iface->next) {
1833 handle_carrier(iface->name);
1834 if (iface->carrier != LINK_DOWN) {
1835 opt = 1;
1836 break;
1840 if (opt == 0 &&
1841 options & DHCPCD_LINK &&
1842 !(options & DHCPCD_WAITIP))
1844 syslog(LOG_WARNING, "no interfaces have a carrier");
1845 daemonise();
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);
1853 free_options(ifo);
1855 sort_interfaces();
1856 for (iface = ifaces; iface; iface = iface->next)
1857 add_timeout_sec(0, start_interface, iface);
1859 start_eloop();
1860 exit(EXIT_SUCCESS);