1/*
2 * dhcpcd - DHCP client daemon
3 * Copyright (c) 2006-2012 Roy Marples <roy@marples.name>
4 * All rights reserved
5
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.
14 *
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.
26 */
27
28const char copyright[] = "Copyright (c) 2006-2012 Roy Marples";
29
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>
36
37#include <arpa/inet.h>
38#include <net/route.h>
39
40#ifdef __linux__
41#  include <asm/types.h> /* for systems with broken headers */
42#  include <linux/rtnetlink.h>
43#endif
44
45#include <ctype.h>
46#include <errno.h>
47#include <getopt.h>
48#include <limits.h>
49#include <paths.h>
50#include <signal.h>
51#include <stdio.h>
52#include <stdlib.h>
53#include <string.h>
54#include <syslog.h>
55#include <unistd.h>
56#include <time.h>
57
58#include "arp.h"
59#include "bind.h"
60#include "config.h"
61#include "common.h"
62#include "configure.h"
63#include "control.h"
64#include "dhcpcd.h"
65#include "duid.h"
66#include "eloop.h"
67#include "if-options.h"
68#include "if-pref.h"
69#include "ipv4ll.h"
70#include "ipv6.h"
71#include "ipv6ns.h"
72#include "ipv6rs.h"
73#include "net.h"
74#include "platform.h"
75#include "signals.h"
76
77/* We should define a maximum for the NAK exponential backoff */
78#define NAKOFF_MAX              60
79
80/* Wait N nanoseconds between sending a RELEASE and dropping the address.
81 * This gives the kernel enough time to actually send it. */
82#define RELEASE_DELAY_S		0
83#define RELEASE_DELAY_NS	10000000
84
85int pidfd = -1;
86struct interface *ifaces = NULL;
87int ifac = 0;
88char **ifav = NULL;
89int ifdc = 0;
90char **ifdv = NULL;
91
92static char **margv;
93static int margc;
94static struct if_options *if_options;
95static char **ifv;
96static int ifc;
97static char *cffile;
98static char *pidfile;
99static int linkfd = -1, ipv6rsfd = -1, ipv6nsfd = -1;
100
101struct dhcp_op {
102	uint8_t value;
103	const char *name;
104};
105
106static const struct dhcp_op dhcp_ops[] = {
107	{ DHCP_DISCOVER, "DISCOVER" },
108	{ DHCP_OFFER,    "OFFER" },
109	{ DHCP_REQUEST,  "REQUEST" },
110	{ DHCP_DECLINE,  "DECLINE" },
111	{ DHCP_ACK,      "ACK" },
112	{ DHCP_NAK,      "NAK" },
113	{ DHCP_RELEASE,  "RELEASE" },
114	{ DHCP_INFORM,   "INFORM" },
115	{ 0, NULL }
116};
117
118static void send_release(struct interface *);
119
120static const char *
121get_dhcp_op(uint8_t type)
122{
123	const struct dhcp_op *d;
124
125	for (d = dhcp_ops; d->name; d++)
126		if (d->value == type)
127			return d->name;
128	return NULL;
129}
130
131static pid_t
132read_pid(void)
133{
134	FILE *fp;
135	pid_t pid;
136
137	if ((fp = fopen(pidfile, "r")) == NULL) {
138		errno = ENOENT;
139		return 0;
140	}
141	if (fscanf(fp, "%d", &pid) != 1)
142		pid = 0;
143	fclose(fp);
144	return pid;
145}
146
147static void
148usage(void)
149{
150
151printf("usage: "PACKAGE"\t[-ABbDdEGgHJKkLnpqTVw]\n"
152	"\t\t[-C, --nohook hook] [-c, --script script]\n"
153	"\t\t[-e, --env value] [-F, --fqdn FQDN] [-f, --config file]\n"
154	"\t\t[-h, --hostname hostname] [-I, --clientid clientid]\n"
155	"\t\t[-i, --vendorclassid vendorclassid] [-l, --leasetime seconds]\n"
156	"\t\t[-m, --metric metric] [-O, --nooption option]\n"
157	"\t\t[-o, --option option] [-Q, --require option]\n"
158	"\t\t[-r, --request address] [-S, --static value]\n"
159	"\t\t[-s, --inform address[/cidr]] [-t, --timeout seconds]\n"
160	"\t\t[-u, --userclass class] [-v, --vendor code, value]\n"
161	"\t\t[-W, --whitelist address[/cidr]] [-y, --reboot seconds]\n"
162	"\t\t[-X, --blacklist address[/cidr]] [-Z, --denyinterfaces pattern]\n"
163	"\t\t[-z, --allowinterfaces pattern] [interface] [...]\n"
164	"       "PACKAGE"\t-k, --release [interface]\n"
165	"       "PACKAGE"\t-U, --dumplease interface\n"
166	"       "PACKAGE"\t--version\n"
167	"       "PACKAGE"\t-x, --exit [interface]\n");
168}
169
170static void
171cleanup(void)
172{
173#ifdef DEBUG_MEMORY
174	struct interface *iface;
175	int i;
176
177	free_options(if_options);
178
179	while (ifaces) {
180		iface = ifaces;
181		ifaces = iface->next;
182		free_interface(iface);
183	}
184
185	for (i = 0; i < ifac; i++)
186		free(ifav[i]);
187	free(ifav);
188	for (i = 0; i < ifdc; i++)
189		free(ifdv[i]);
190	free(ifdv);
191#endif
192
193	if (linkfd != -1)
194		close(linkfd);
195	if (pidfd > -1) {
196		if (options & DHCPCD_MASTER) {
197			if (stop_control() == -1)
198				syslog(LOG_ERR, "stop_control: %m");
199		}
200		close(pidfd);
201		unlink(pidfile);
202	}
203#ifdef DEBUG_MEMORY
204	free(pidfile);
205#endif
206}
207
208/* ARGSUSED */
209void
210handle_exit_timeout(_unused void *arg)
211{
212	int timeout;
213
214	syslog(LOG_ERR, "timed out");
215	if (!(options & DHCPCD_TIMEOUT_IPV4LL)) {
216		if (options & DHCPCD_MASTER) {
217			daemonise();
218			return;
219		} else
220			exit(EXIT_FAILURE);
221	}
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	add_timeout_sec(timeout, handle_exit_timeout, NULL);
226}
227
228void
229drop_dhcp(struct interface *iface, const char *reason)
230{
231	free(iface->state->old);
232	iface->state->old = iface->state->new;
233	iface->state->new = NULL;
234	iface->state->reason = reason;
235	configure(iface);
236	free(iface->state->old);
237	iface->state->old = NULL;
238	iface->state->lease.addr.s_addr = 0;
239}
240
241struct interface *
242find_interface(const char *ifname)
243{
244	struct interface *ifp;
245
246	for (ifp = ifaces; ifp; ifp = ifp->next)
247		if (strcmp(ifp->name, ifname) == 0)
248			return ifp;
249	return NULL;
250}
251
252static void
253stop_interface(struct interface *iface)
254{
255	struct interface *ifp, *ifl = NULL;
256
257	syslog(LOG_INFO, "%s: removing interface", iface->name);
258
259	// Remove the interface from our list
260	for (ifp = ifaces; ifp; ifp = ifp->next) {
261		if (ifp == iface)
262			break;
263		ifl = ifp;
264	}
265	if (ifl)
266		ifl->next = ifp->next;
267	else
268		ifaces = ifp->next;
269
270	ipv6rs_drop(iface);
271	if (strcmp(iface->state->reason, "RELEASE") != 0)
272		drop_dhcp(iface, "STOP");
273	close_sockets(iface);
274	delete_timeout(NULL, iface);
275	free_interface(ifp);
276	if (!(options & (DHCPCD_MASTER | DHCPCD_TEST)))
277		exit(EXIT_FAILURE);
278}
279
280static uint32_t
281dhcp_xid(struct interface *iface)
282{
283	uint32_t xid;
284
285	if (iface->state->options->options & DHCPCD_XID_HWADDR &&
286	    iface->hwlen >= sizeof(xid))
287		/* The lower bits are probably more unique on the network */
288		memcpy(&xid, (iface->hwaddr + iface->hwlen) - sizeof(xid),
289		    sizeof(xid));
290	else
291		xid = arc4random();
292
293	return xid;
294}
295
296static void
297send_message(struct interface *iface, int type,
298    void (*callback)(void *))
299{
300	struct if_state *state = iface->state;
301	struct if_options *ifo = state->options;
302	struct dhcp_message *dhcp;
303	uint8_t *udp;
304	ssize_t len, r;
305	struct in_addr from, to;
306	in_addr_t a = 0;
307	struct timeval tv;
308
309	if (!callback)
310		syslog(LOG_DEBUG, "%s: sending %s with xid 0x%x",
311		    iface->name, get_dhcp_op(type), state->xid);
312	else {
313		if (state->interval == 0)
314			state->interval = 4;
315		else {
316			state->interval *= 2;
317			if (state->interval > 64)
318				state->interval = 64;
319		}
320		tv.tv_sec = state->interval + DHCP_RAND_MIN;
321		tv.tv_usec = arc4random() % (DHCP_RAND_MAX_U - DHCP_RAND_MIN_U);
322		syslog(LOG_DEBUG,
323		    "%s: sending %s (xid 0x%x), next in %0.2f seconds",
324		    iface->name, get_dhcp_op(type), state->xid,
325		    timeval_to_double(&tv));
326	}
327
328	/* Ensure sockets are open. */
329	if (open_sockets(iface) == -1) {
330		if (!(options & DHCPCD_TEST))
331			drop_dhcp(iface, "FAIL");
332		return;
333	}
334
335	/* If we couldn't open a UDP port for our IP address
336	 * then we cannot renew.
337	 * This could happen if our IP was pulled out from underneath us.
338	 * Also, we should not unicast from a BOOTP lease. */
339	if (iface->udp_fd == -1 ||
340	    (!(ifo->options & DHCPCD_INFORM) && is_bootp(iface->state->new)))
341	{
342		a = iface->addr.s_addr;
343		iface->addr.s_addr = 0;
344	}
345	len = make_message(&dhcp, iface, type);
346	if (a)
347		iface->addr.s_addr = a;
348	from.s_addr = dhcp->ciaddr;
349	if (from.s_addr)
350		to.s_addr = state->lease.server.s_addr;
351	else
352		to.s_addr = 0;
353	if (to.s_addr && to.s_addr != INADDR_BROADCAST) {
354		r = send_packet(iface, to, (uint8_t *)dhcp, len);
355		if (r == -1) {
356			syslog(LOG_ERR, "%s: send_packet: %m", iface->name);
357			close_sockets(iface);
358		}
359	} else {
360		len = make_udp_packet(&udp, (uint8_t *)dhcp, len, from, to);
361		r = send_raw_packet(iface, ETHERTYPE_IP, udp, len);
362		free(udp);
363		/* If we failed to send a raw packet this normally means
364		 * we don't have the ability to work beneath the IP layer
365		 * for this interface.
366		 * As such we remove it from consideration without actually
367		 * stopping the interface. */
368		if (r == -1) {
369			syslog(LOG_ERR, "%s: send_raw_packet: %m", iface->name);
370			if (!(options & DHCPCD_TEST))
371				drop_dhcp(iface, "FAIL");
372			close_sockets(iface);
373			delete_timeout(NULL, iface);
374			callback = NULL;
375		}
376	}
377	free(dhcp);
378
379	/* Even if we fail to send a packet we should continue as we are
380	 * as our failure timeouts will change out codepath when needed. */
381	if (callback)
382		add_timeout_tv(&tv, callback, iface);
383}
384
385static void
386send_inform(void *arg)
387{
388	send_message((struct interface *)arg, DHCP_INFORM, send_inform);
389}
390
391static void
392send_discover(void *arg)
393{
394	send_message((struct interface *)arg, DHCP_DISCOVER, send_discover);
395}
396
397static void
398send_request(void *arg)
399{
400	send_message((struct interface *)arg, DHCP_REQUEST, send_request);
401}
402
403static void
404send_renew(void *arg)
405{
406	send_message((struct interface *)arg, DHCP_REQUEST, send_renew);
407}
408
409static void
410send_rebind(void *arg)
411{
412	send_message((struct interface *)arg, DHCP_REQUEST, send_rebind);
413}
414
415void
416start_expire(void *arg)
417{
418	struct interface *iface = arg;
419
420	iface->state->interval = 0;
421	if (iface->addr.s_addr == 0) {
422		/* We failed to reboot, so enter discovery. */
423		iface->state->lease.addr.s_addr = 0;
424		start_discover(iface);
425		return;
426	}
427
428	syslog(LOG_ERR, "%s: lease expired", iface->name);
429	delete_timeout(NULL, iface);
430	drop_dhcp(iface, "EXPIRE");
431	unlink(iface->leasefile);
432	if (iface->carrier != LINK_DOWN)
433		start_interface(iface);
434}
435
436static void
437log_dhcp(int lvl, const char *msg,
438    const struct interface *iface, const struct dhcp_message *dhcp,
439    const struct in_addr *from)
440{
441	const char *tfrom;
442	char *a;
443	struct in_addr addr;
444	int r;
445
446	if (strcmp(msg, "NAK:") == 0)
447		a = get_option_string(dhcp, DHO_MESSAGE);
448	else if (dhcp->yiaddr != 0) {
449		addr.s_addr = dhcp->yiaddr;
450		a = xstrdup(inet_ntoa(addr));
451	} else
452		a = NULL;
453
454	tfrom = "from";
455	r = get_option_addr(&addr, dhcp, DHO_SERVERID);
456	if (dhcp->servername[0] && r == 0)
457		syslog(lvl, "%s: %s %s %s %s `%s'", iface->name, msg, a,
458		    tfrom, inet_ntoa(addr), dhcp->servername);
459	else {
460		if (r != 0) {
461			tfrom = "via";
462			addr = *from;
463		}
464		if (a == NULL)
465			syslog(lvl, "%s: %s %s %s",
466			    iface->name, msg, tfrom, inet_ntoa(addr));
467		else
468			syslog(lvl, "%s: %s %s %s %s",
469			    iface->name, msg, a, tfrom, inet_ntoa(addr));
470	}
471	free(a);
472}
473
474static int
475blacklisted_ip(const struct if_options *ifo, in_addr_t addr)
476{
477	size_t i;
478
479	for (i = 0; i < ifo->blacklist_len; i += 2)
480		if (ifo->blacklist[i] == (addr & ifo->blacklist[i + 1]))
481			return 1;
482	return 0;
483}
484
485static int
486whitelisted_ip(const struct if_options *ifo, in_addr_t addr)
487{
488	size_t i;
489
490	if (ifo->whitelist_len == 0)
491		return -1;
492	for (i = 0; i < ifo->whitelist_len; i += 2)
493		if (ifo->whitelist[i] == (addr & ifo->whitelist[i + 1]))
494			return 1;
495	return 0;
496}
497
498static void
499handle_dhcp(struct interface *iface, struct dhcp_message **dhcpp, const struct in_addr *from)
500{
501	struct if_state *state = iface->state;
502	struct if_options *ifo = state->options;
503	struct dhcp_message *dhcp = *dhcpp;
504	struct dhcp_lease *lease = &state->lease;
505	uint8_t type, tmp;
506	struct in_addr addr;
507	size_t i;
508
509	/* reset the message counter */
510	state->interval = 0;
511
512	/* We may have found a BOOTP server */
513	if (get_option_uint8(&type, dhcp, DHO_MESSAGETYPE) == -1)
514		type = 0;
515
516	if (type == DHCP_NAK) {
517		/* For NAK, only check if we require the ServerID */
518		if (has_option_mask(ifo->requiremask, DHO_SERVERID) &&
519		    get_option_addr(&addr, dhcp, DHO_SERVERID) == -1)
520		{
521			log_dhcp(LOG_WARNING, "reject NAK", iface, dhcp, from);
522			return;
523		}
524		/* We should restart on a NAK */
525		log_dhcp(LOG_WARNING, "NAK:", iface, dhcp, from);
526		if (!(options & DHCPCD_TEST)) {
527			drop_dhcp(iface, "NAK");
528			unlink(iface->leasefile);
529		}
530		close_sockets(iface);
531		/* If we constantly get NAKS then we should slowly back off */
532		add_timeout_sec(state->nakoff, start_interface, iface);
533		state->nakoff *= 2;
534		if (state->nakoff > NAKOFF_MAX)
535			state->nakoff = NAKOFF_MAX;
536		return;
537	}
538
539	/* Ensure that all required options are present */
540	for (i = 1; i < 255; i++) {
541		if (has_option_mask(ifo->requiremask, i) &&
542		    get_option_uint8(&tmp, dhcp, i) != 0)
543		{
544			/* If we are bootp, then ignore the need for serverid.
545			 * To ignore bootp, require dhcp_message_type instead. */
546			if (type == 0 && i == DHO_SERVERID)
547				continue;
548			log_dhcp(LOG_WARNING, "reject DHCP", iface, dhcp, from);
549			return;
550		}
551	}
552
553	/* Ensure that the address offered is valid */
554	if ((type == 0 || type == DHCP_OFFER || type == DHCP_ACK) &&
555	    (dhcp->ciaddr == INADDR_ANY || dhcp->ciaddr == INADDR_BROADCAST) &&
556	    (dhcp->yiaddr == INADDR_ANY || dhcp->yiaddr == INADDR_BROADCAST))
557	{
558		log_dhcp(LOG_WARNING, "reject invalid address",
559		    iface, dhcp, from);
560		return;
561	}
562
563	/* No NAK, so reset the backoff */
564	state->nakoff = 1;
565
566	if ((type == 0 || type == DHCP_OFFER) &&
567	    state->state == DHS_DISCOVER)
568	{
569		lease->frominfo = 0;
570		lease->addr.s_addr = dhcp->yiaddr;
571		lease->cookie = dhcp->cookie;
572		if (type == 0 ||
573		    get_option_addr(&lease->server, dhcp, DHO_SERVERID) != 0)
574			lease->server.s_addr = INADDR_ANY;
575		log_dhcp(LOG_INFO, "offered", iface, dhcp, from);
576		free(state->offer);
577		state->offer = dhcp;
578		*dhcpp = NULL;
579		if (options & DHCPCD_TEST) {
580			free(state->old);
581			state->old = state->new;
582			state->new = state->offer;
583			state->offer = NULL;
584			state->reason = "TEST";
585			run_script(iface);
586			exit(EXIT_SUCCESS);
587		}
588		delete_timeout(send_discover, iface);
589		/* We don't request BOOTP addresses */
590		if (type) {
591			/* We used to ARP check here, but that seems to be in
592			 * violation of RFC2131 where it only describes
593			 * DECLINE after REQUEST.
594			 * It also seems that some MS DHCP servers actually
595			 * ignore DECLINE if no REQUEST, ie we decline a
596			 * DISCOVER. */
597			start_request(iface);
598			return;
599		}
600	}
601
602	if (type) {
603		if (type == DHCP_OFFER) {
604			log_dhcp(LOG_INFO, "ignoring offer of",
605			    iface, dhcp, from);
606			return;
607		}
608
609		/* We should only be dealing with acks */
610		if (type != DHCP_ACK) {
611			log_dhcp(LOG_ERR, "not ACK or OFFER",
612			    iface, dhcp, from);
613			return;
614		}
615
616		if (!(ifo->options & DHCPCD_INFORM))
617			log_dhcp(LOG_INFO, "acknowledged", iface, dhcp, from);
618	}
619
620	/* BOOTP could have already assigned this above, so check we still
621	 * have a pointer. */
622	if (*dhcpp) {
623		free(state->offer);
624		state->offer = dhcp;
625		*dhcpp = NULL;
626	}
627
628	lease->frominfo = 0;
629	delete_timeout(NULL, iface);
630
631	/* We now have an offer, so close the DHCP sockets.
632	 * This allows us to safely ARP when broken DHCP servers send an ACK
633	 * follows by an invalid NAK. */
634	close_sockets(iface);
635
636	if (ifo->options & DHCPCD_ARP &&
637	    iface->addr.s_addr != state->offer->yiaddr)
638	{
639		/* If the interface already has the address configured
640		 * then we can't ARP for duplicate detection. */
641		addr.s_addr = state->offer->yiaddr;
642		if (has_address(iface->name, &addr, NULL) != 1) {
643			state->claims = 0;
644			state->probes = 0;
645			state->conflicts = 0;
646			state->state = DHS_PROBE;
647			send_arp_probe(iface);
648			return;
649		}
650	}
651
652	bind_interface(iface);
653}
654
655static void
656handle_dhcp_packet(void *arg)
657{
658	struct interface *iface = arg;
659	uint8_t *packet;
660	struct dhcp_message *dhcp = NULL;
661	const uint8_t *pp;
662	ssize_t bytes;
663	struct in_addr from;
664	int i, partialcsum = 0;
665
666	/* We loop through until our buffer is empty.
667	 * The benefit is that if we get >1 DHCP packet in our buffer and
668	 * the first one fails for any reason, we can use the next. */
669	packet = xmalloc(udp_dhcp_len);
670	for(;;) {
671		bytes = get_raw_packet(iface, ETHERTYPE_IP,
672		    packet, udp_dhcp_len, &partialcsum);
673		if (bytes == 0 || bytes == -1)
674			break;
675		if (valid_udp_packet(packet, bytes, &from, partialcsum) == -1) {
676			syslog(LOG_ERR, "%s: invalid UDP packet from %s",
677			    iface->name, inet_ntoa(from));
678			continue;
679		}
680		i = whitelisted_ip(iface->state->options, from.s_addr);
681		if (i == 0) {
682			syslog(LOG_WARNING,
683			    "%s: non whitelisted DHCP packet from %s",
684			    iface->name, inet_ntoa(from));
685			continue;
686		} else if (i != 1 &&
687		    blacklisted_ip(iface->state->options, from.s_addr) == 1)
688		{
689			syslog(LOG_WARNING,
690			    "%s: blacklisted DHCP packet from %s",
691			    iface->name, inet_ntoa(from));
692			continue;
693		}
694		if (iface->flags & IFF_POINTOPOINT &&
695		    iface->dst.s_addr != from.s_addr)
696		{
697			syslog(LOG_WARNING,
698			    "%s: server %s is not destination",
699			    iface->name, inet_ntoa(from));
700		}
701		bytes = get_udp_data(&pp, packet);
702		if ((size_t)bytes > sizeof(*dhcp)) {
703			syslog(LOG_ERR,
704			    "%s: packet greater than DHCP size from %s",
705			    iface->name, inet_ntoa(from));
706			continue;
707		}
708		if (!dhcp)
709			dhcp = xzalloc(sizeof(*dhcp));
710		memcpy(dhcp, pp, bytes);
711		if (dhcp->cookie != htonl(MAGIC_COOKIE)) {
712			syslog(LOG_DEBUG, "%s: bogus cookie from %s",
713			    iface->name, inet_ntoa(from));
714			continue;
715		}
716		/* Ensure it's the right transaction */
717		if (iface->state->xid != dhcp->xid) {
718			syslog(LOG_DEBUG,
719			    "%s: wrong xid 0x%x (expecting 0x%x) from %s",
720			    iface->name, dhcp->xid, iface->state->xid,
721			    inet_ntoa(from));
722			continue;
723		}
724		/* Ensure packet is for us */
725		if (iface->hwlen <= sizeof(dhcp->chaddr) &&
726		    memcmp(dhcp->chaddr, iface->hwaddr, iface->hwlen))
727		{
728			syslog(LOG_DEBUG, "%s: xid 0x%x is not for hwaddr %s",
729			    iface->name, dhcp->xid,
730			    hwaddr_ntoa(dhcp->chaddr, sizeof(dhcp->chaddr)));
731			continue;
732		}
733		handle_dhcp(iface, &dhcp, &from);
734		if (iface->raw_fd == -1)
735			break;
736	}
737	free(packet);
738	free(dhcp);
739}
740
741static void
742send_release(struct interface *iface)
743{
744	struct timespec ts;
745
746	if (iface->state->new != NULL &&
747	    iface->state->new->cookie == htonl(MAGIC_COOKIE))
748	{
749		syslog(LOG_INFO, "%s: releasing lease of %s",
750		    iface->name, inet_ntoa(iface->state->lease.addr));
751		iface->state->xid = dhcp_xid(iface);
752		send_message(iface, DHCP_RELEASE, NULL);
753		/* Give the packet a chance to go before dropping the ip */
754		ts.tv_sec = RELEASE_DELAY_S;
755		ts.tv_nsec = RELEASE_DELAY_NS;
756		nanosleep(&ts, NULL);
757		drop_dhcp(iface, "RELEASE");
758	}
759	unlink(iface->leasefile);
760}
761
762void
763send_decline(struct interface *iface)
764{
765	send_message(iface, DHCP_DECLINE, NULL);
766}
767
768static void
769configure_interface1(struct interface *iface)
770{
771	struct if_state *ifs = iface->state;
772	struct if_options *ifo = ifs->options;
773	uint8_t *duid;
774	size_t len = 0, ifl;
775
776	/* Do any platform specific configuration */
777	if_conf(iface);
778
779	if (iface->flags & IFF_POINTOPOINT && !(ifo->options & DHCPCD_INFORM))
780		ifo->options |= DHCPCD_STATIC;
781	if (iface->flags & IFF_NOARP ||
782	    ifo->options & (DHCPCD_INFORM | DHCPCD_STATIC))
783		ifo->options &= ~(DHCPCD_ARP | DHCPCD_IPV4LL);
784	if (!(iface->flags & (IFF_POINTOPOINT | IFF_LOOPBACK | IFF_MULTICAST)))
785		ifo->options &= ~DHCPCD_IPV6RS;
786	if (ifo->options & DHCPCD_LINK && carrier_status(iface) == -1)
787		ifo->options &= ~DHCPCD_LINK;
788
789	if (ifo->metric != -1)
790		iface->metric = ifo->metric;
791
792	/* We want to disable kernel interface RA as early as possible. */
793	if (options & DHCPCD_IPV6RS && ifo->options & DHCPCD_IPV6RS) {
794		if (check_ipv6(iface->name) != 1)
795			ifo->options &= ~DHCPCD_IPV6RS;
796	}
797
798	/* If we haven't specified a ClientID and our hardware address
799	 * length is greater than DHCP_CHADDR_LEN then we enforce a ClientID
800	 * of the hardware address family and the hardware address. */
801	if (iface->hwlen > DHCP_CHADDR_LEN)
802		ifo->options |= DHCPCD_CLIENTID;
803
804	/* Firewire and InfiniBand interfaces require ClientID and
805	 * the broadcast option being set. */
806	switch (iface->family) {
807	case ARPHRD_IEEE1394:	/* FALLTHROUGH */
808	case ARPHRD_INFINIBAND:
809		ifo->options |= DHCPCD_CLIENTID | DHCPCD_BROADCAST;
810		break;
811	}
812
813	free(iface->clientid);
814	iface->clientid = NULL;
815	if (*ifo->clientid) {
816		iface->clientid = xmalloc(ifo->clientid[0] + 1);
817		memcpy(iface->clientid, ifo->clientid, ifo->clientid[0] + 1);
818	} else if (ifo->options & DHCPCD_CLIENTID) {
819		if (ifo->options & DHCPCD_DUID) {
820			duid = xmalloc(DUID_LEN);
821			if ((len = get_duid(duid, iface)) == 0)
822				syslog(LOG_ERR, "get_duid: %m");
823		}
824		if (len > 0) {
825			iface->clientid = xmalloc(len + 6);
826			iface->clientid[0] = len + 5;
827			iface->clientid[1] = 255; /* RFC 4361 */
828			ifl = strlen(iface->name);
829			if (ifl < 5) {
830				memcpy(iface->clientid + 2, iface->name, ifl);
831				if (ifl < 4)
832					memset(iface->clientid + 2 + ifl,
833					    0, 4 - ifl);
834			} else {
835				ifl = htonl(iface->index);
836				memcpy(iface->clientid + 2, &ifl, 4);
837			}
838		} else if (len == 0) {
839			len = iface->hwlen + 1;
840			iface->clientid = xmalloc(len + 1);
841			iface->clientid[0] = len;
842			iface->clientid[1] = iface->family;
843			memcpy(iface->clientid + 2, iface->hwaddr,
844			    iface->hwlen);
845		}
846	}
847	if (ifo->options & DHCPCD_CLIENTID)
848		syslog(LOG_DEBUG, "%s: using ClientID %s", iface->name,
849		    hwaddr_ntoa(iface->clientid + 1, *iface->clientid));
850	else if (iface->hwlen)
851		syslog(LOG_DEBUG, "%s: using hwaddr %s", iface->name,
852		    hwaddr_ntoa(iface->hwaddr, iface->hwlen));
853}
854
855int
856select_profile(struct interface *iface, const char *profile)
857{
858	struct if_options *ifo;
859	int ret;
860
861	ret = 0;
862	ifo = read_config(cffile, iface->name, iface->ssid, profile);
863	if (ifo == NULL) {
864		syslog(LOG_DEBUG, "%s: no profile %s", iface->name, profile);
865		ret = -1;
866		goto exit;
867	}
868	if (profile != NULL) {
869		strlcpy(iface->state->profile, profile,
870		    sizeof(iface->state->profile));
871		syslog(LOG_INFO, "%s: selected profile %s",
872		    iface->name, profile);
873	} else
874		*iface->state->profile = '\0';
875	free_options(iface->state->options);
876	iface->state->options = ifo;
877
878exit:
879	if (profile)
880		configure_interface1(iface);
881	return ret;
882}
883
884static void
885start_fallback(void *arg)
886{
887	struct interface *iface;
888
889	iface = (struct interface *)arg;
890	select_profile(iface, iface->state->options->fallback);
891	start_interface(iface);
892}
893
894static void
895configure_interface(struct interface *iface, int argc, char **argv)
896{
897	select_profile(iface, NULL);
898	add_options(iface->state->options, argc, argv);
899	configure_interface1(iface);
900}
901
902void
903handle_carrier(int action, int flags, const char *ifname)
904{
905	struct interface *iface;
906	int carrier;
907
908	if (!(options & DHCPCD_LINK))
909		return;
910	for (iface = ifaces; iface; iface = iface->next)
911		if (strcmp(iface->name, ifname) == 0)
912			break;
913	if (!iface) {
914		if (options & DHCPCD_LINK)
915			handle_interface(1, ifname);
916		return;
917	}
918	if (!(iface->state->options->options & DHCPCD_LINK))
919		return;
920
921	if (action) {
922		carrier = action == 1 ? 1 : 0;
923		iface->flags = flags;
924	} else
925		carrier = carrier_status(iface);
926
927	if (carrier == -1)
928		syslog(LOG_ERR, "%s: carrier_status: %m", ifname);
929	else if (carrier == 0 || ~iface->flags & IFF_UP) {
930		if (iface->carrier != LINK_DOWN) {
931			iface->carrier = LINK_DOWN;
932			syslog(LOG_INFO, "%s: carrier lost", iface->name);
933			close_sockets(iface);
934			delete_timeouts(iface, start_expire, NULL);
935			ipv6rs_drop(iface);
936			drop_dhcp(iface, "NOCARRIER");
937		}
938	} else if (carrier == 1 && !(~iface->flags & IFF_UP)) {
939		if (iface->carrier != LINK_UP) {
940			iface->carrier = LINK_UP;
941			syslog(LOG_INFO, "%s: carrier acquired", iface->name);
942			if (iface->wireless)
943				getifssid(iface->name, iface->ssid);
944			configure_interface(iface, margc, margv);
945			iface->state->interval = 0;
946			iface->state->reason = "CARRIER";
947			run_script(iface);
948			start_interface(iface);
949		}
950	}
951}
952
953void
954start_discover(void *arg)
955{
956	struct interface *iface = arg;
957	struct if_options *ifo = iface->state->options;
958	int timeout = ifo->timeout;
959
960	/* If we're rebooting and we're not daemonised then we need
961	 * to shorten the normal timeout to ensure we try correctly
962	 * for a fallback or IPv4LL address. */
963	if (iface->state->state == DHS_REBOOT &&
964	    !(options & DHCPCD_DAEMONISED))
965	{
966		timeout -= ifo->reboot;
967		if (timeout <= 0)
968			timeout = 2;
969	}
970
971	iface->state->state = DHS_DISCOVER;
972	iface->state->xid = dhcp_xid(iface);
973	delete_timeout(NULL, iface);
974	if (ifo->fallback)
975		add_timeout_sec(timeout, start_fallback, iface);
976	else if (ifo->options & DHCPCD_IPV4LL &&
977	    !IN_LINKLOCAL(htonl(iface->addr.s_addr)))
978	{
979		if (IN_LINKLOCAL(htonl(iface->state->fail.s_addr)))
980			add_timeout_sec(RATE_LIMIT_INTERVAL, start_ipv4ll, iface);
981		else
982			add_timeout_sec(timeout, start_ipv4ll, iface);
983	}
984	if (ifo->options & DHCPCD_REQUEST)
985		syslog(LOG_INFO, "%s: broadcasting for a lease (requesting %s)",
986		    iface->name, inet_ntoa(ifo->req_addr));
987	else
988		syslog(LOG_INFO, "%s: broadcasting for a lease", iface->name);
989	send_discover(iface);
990}
991
992void
993start_request(void *arg)
994{
995	struct interface *iface = arg;
996
997	iface->state->state = DHS_REQUEST;
998	send_request(iface);
999}
1000
1001void
1002start_renew(void *arg)
1003{
1004	struct interface *iface = arg;
1005
1006	syslog(LOG_INFO, "%s: renewing lease of %s",
1007	    iface->name, inet_ntoa(iface->state->lease.addr));
1008	iface->state->state = DHS_RENEW;
1009	iface->state->xid = dhcp_xid(iface);
1010	send_renew(iface);
1011}
1012
1013void
1014start_rebind(void *arg)
1015{
1016	struct interface *iface = arg;
1017
1018	syslog(LOG_ERR, "%s: failed to renew, attempting to rebind",
1019	    iface->name);
1020	iface->state->state = DHS_REBIND;
1021	delete_timeout(send_renew, iface);
1022	iface->state->lease.server.s_addr = 0;
1023	send_rebind(iface);
1024}
1025
1026static void
1027start_timeout(void *arg)
1028{
1029	struct interface *iface = arg;
1030
1031	bind_interface(iface);
1032	iface->state->interval = 0;
1033	start_discover(iface);
1034}
1035
1036static struct dhcp_message *
1037dhcp_message_new(struct in_addr *addr, struct in_addr *mask)
1038{
1039	struct dhcp_message *dhcp;
1040	uint8_t *p;
1041
1042	dhcp = xzalloc(sizeof(*dhcp));
1043	dhcp->yiaddr = addr->s_addr;
1044	p = dhcp->options;
1045	if (mask && mask->s_addr != INADDR_ANY) {
1046		*p++ = DHO_SUBNETMASK;
1047		*p++ = sizeof(mask->s_addr);
1048		memcpy(p, &mask->s_addr, sizeof(mask->s_addr));
1049		p+= sizeof(mask->s_addr);
1050	}
1051	*p++ = DHO_END;
1052	return dhcp;
1053}
1054
1055static int
1056handle_3rdparty(struct interface *iface)
1057{
1058	struct if_options *ifo;
1059	struct in_addr addr, net, dst;
1060
1061	ifo = iface->state->options;
1062	if (ifo->req_addr.s_addr != INADDR_ANY)
1063		return 0;
1064
1065	if (get_address(iface->name, &addr, &net, &dst) == 1)
1066		handle_ifa(RTM_NEWADDR, iface->name, &addr, &net, &dst);
1067	else {
1068		syslog(LOG_INFO,
1069		    "%s: waiting for 3rd party to configure IP address",
1070		    iface->name);
1071		iface->state->reason = "3RDPARTY";
1072		run_script(iface);
1073	}
1074	return 1;
1075}
1076
1077static void
1078start_static(struct interface *iface)
1079{
1080	struct if_options *ifo;
1081
1082	if (handle_3rdparty(iface))
1083		return;
1084	ifo = iface->state->options;
1085	iface->state->offer =
1086	    dhcp_message_new(&ifo->req_addr, &ifo->req_mask);
1087	delete_timeout(NULL, iface);
1088	bind_interface(iface);
1089}
1090
1091static void
1092start_inform(struct interface *iface)
1093{
1094	if (handle_3rdparty(iface))
1095		return;
1096
1097	if (options & DHCPCD_TEST) {
1098		iface->addr.s_addr = iface->state->options->req_addr.s_addr;
1099		iface->net.s_addr = iface->state->options->req_mask.s_addr;
1100	} else {
1101		iface->state->options->options |= DHCPCD_STATIC;
1102		start_static(iface);
1103	}
1104
1105	iface->state->state = DHS_INFORM;
1106	iface->state->xid = dhcp_xid(iface);
1107	send_inform(iface);
1108}
1109
1110void
1111start_reboot(struct interface *iface)
1112{
1113	struct if_options *ifo = iface->state->options;
1114
1115	if (ifo->options & DHCPCD_LINK && iface->carrier == LINK_DOWN) {
1116		syslog(LOG_INFO, "%s: waiting for carrier", iface->name);
1117		return;
1118	}
1119	if (ifo->options & DHCPCD_STATIC) {
1120		start_static(iface);
1121		return;
1122	}
1123	if (ifo->reboot == 0 || iface->state->offer == NULL) {
1124		start_discover(iface);
1125		return;
1126	}
1127	if (ifo->options & DHCPCD_INFORM) {
1128		syslog(LOG_INFO, "%s: informing address of %s",
1129		    iface->name, inet_ntoa(iface->state->lease.addr));
1130	} else if (iface->state->offer->cookie == 0) {
1131		if (ifo->options & DHCPCD_IPV4LL) {
1132			iface->state->claims = 0;
1133			send_arp_announce(iface);
1134		} else
1135			start_discover(iface);
1136		return;
1137	} else {
1138		syslog(LOG_INFO, "%s: rebinding lease of %s",
1139		    iface->name, inet_ntoa(iface->state->lease.addr));
1140	}
1141	iface->state->state = DHS_REBOOT;
1142	iface->state->xid = dhcp_xid(iface);
1143	iface->state->lease.server.s_addr = 0;
1144	delete_timeout(NULL, iface);
1145	if (ifo->fallback)
1146		add_timeout_sec(ifo->reboot, start_fallback, iface);
1147	else if (ifo->options & DHCPCD_LASTLEASE &&
1148	    iface->state->lease.frominfo)
1149		add_timeout_sec(ifo->reboot, start_timeout, iface);
1150	else if (!(ifo->options & DHCPCD_INFORM &&
1151		options & (DHCPCD_MASTER | DHCPCD_DAEMONISED)))
1152		add_timeout_sec(ifo->reboot, start_expire, iface);
1153	/* Don't bother ARP checking as the server could NAK us first. */
1154	if (ifo->options & DHCPCD_INFORM)
1155		send_inform(iface);
1156	else
1157		send_request(iface);
1158}
1159
1160void
1161start_interface(void *arg)
1162{
1163	struct interface *iface = arg;
1164	struct if_options *ifo = iface->state->options;
1165	struct stat st;
1166	struct timeval now;
1167	uint32_t l;
1168	int nolease;
1169
1170	handle_carrier(0, 0, iface->name);
1171	if (iface->carrier == LINK_DOWN) {
1172		syslog(LOG_INFO, "%s: waiting for carrier", iface->name);
1173		return;
1174	}
1175
1176	iface->start_uptime = uptime();
1177	free(iface->state->offer);
1178	iface->state->offer = NULL;
1179
1180	if (options & DHCPCD_IPV6RS && ifo->options & DHCPCD_IPV6RS)
1181		ipv6rs_start(iface);
1182
1183	if (iface->state->arping_index < ifo->arping_len) {
1184		start_arping(iface);
1185		return;
1186	}
1187	if (ifo->options & DHCPCD_STATIC) {
1188		start_static(iface);
1189		return;
1190	}
1191	if (ifo->options & DHCPCD_INFORM) {
1192		start_inform(iface);
1193		return;
1194	}
1195	if (iface->hwlen == 0 && ifo->clientid[0] == '\0') {
1196		syslog(LOG_WARNING, "%s: needs a clientid to configure",
1197		    iface->name);
1198		drop_dhcp(iface, "FAIL");
1199		close_sockets(iface);
1200		delete_timeout(NULL, iface);
1201		return;
1202	}
1203	/* We don't want to read the old lease if we NAK an old test */
1204	nolease = iface->state->offer && options & DHCPCD_TEST;
1205	if (!nolease)
1206		iface->state->offer = read_lease(iface);
1207	if (iface->state->offer) {
1208		get_lease(&iface->state->lease, iface->state->offer);
1209		iface->state->lease.frominfo = 1;
1210		if (iface->state->offer->cookie == 0) {
1211			if (iface->state->offer->yiaddr ==
1212			    iface->addr.s_addr)
1213			{
1214				free(iface->state->offer);
1215				iface->state->offer = NULL;
1216			}
1217		} else if (iface->state->lease.leasetime != ~0U &&
1218		    stat(iface->leasefile, &st) == 0)
1219		{
1220			/* Offset lease times and check expiry */
1221			gettimeofday(&now, NULL);
1222			if ((time_t)iface->state->lease.leasetime <
1223			    now.tv_sec - st.st_mtime)
1224			{
1225				syslog(LOG_DEBUG,
1226				    "%s: discarding expired lease",
1227				    iface->name);
1228				free(iface->state->offer);
1229				iface->state->offer = NULL;
1230				iface->state->lease.addr.s_addr = 0;
1231			} else {
1232				l = now.tv_sec - st.st_mtime;
1233				iface->state->lease.leasetime -= l;
1234				iface->state->lease.renewaltime -= l;
1235				iface->state->lease.rebindtime -= l;
1236			}
1237		}
1238	}
1239	if (iface->state->offer == NULL)
1240		start_discover(iface);
1241	else if (iface->state->offer->cookie == 0 &&
1242	    iface->state->options->options & DHCPCD_IPV4LL)
1243		start_ipv4ll(iface);
1244	else
1245		start_reboot(iface);
1246}
1247
1248static void
1249init_state(struct interface *iface, int argc, char **argv)
1250{
1251	struct if_state *ifs;
1252
1253	if (iface->state)
1254		ifs = iface->state;
1255	else
1256		ifs = iface->state = xzalloc(sizeof(*ifs));
1257
1258	ifs->state = DHS_INIT;
1259	ifs->reason = "PREINIT";
1260	ifs->nakoff = 1;
1261	configure_interface(iface, argc, argv);
1262	if (!(options & DHCPCD_TEST))
1263		run_script(iface);
1264	/* We need to drop the leasefile so that start_interface
1265	 * doesn't load it. */
1266	if (ifs->options->options & DHCPCD_REQUEST)
1267		unlink(iface->leasefile);
1268
1269	if (ifs->options->options & DHCPCD_LINK) {
1270		switch (carrier_status(iface)) {
1271		case 0:
1272			iface->carrier = LINK_DOWN;
1273			ifs->reason = "NOCARRIER";
1274			break;
1275		case 1:
1276			iface->carrier = LINK_UP;
1277			ifs->reason = "CARRIER";
1278			break;
1279		default:
1280			iface->carrier = LINK_UNKNOWN;
1281			return;
1282		}
1283		if (!(options & DHCPCD_TEST))
1284			run_script(iface);
1285	} else
1286		iface->carrier = LINK_UNKNOWN;
1287}
1288
1289void
1290handle_interface(int action, const char *ifname)
1291{
1292	struct interface *ifs, *ifp, *ifn, *ifl = NULL;
1293	const char * const argv[] = { ifname };
1294	int i;
1295
1296	if (action == -1) {
1297		ifp = find_interface(ifname);
1298		if (ifp != NULL)
1299			stop_interface(ifp);
1300		return;
1301	}
1302
1303	/* If running off an interface list, check it's in it. */
1304	if (ifc) {
1305		for (i = 0; i < ifc; i++)
1306			if (strcmp(ifv[i], ifname) == 0)
1307				break;
1308		if (i >= ifc)
1309			return;
1310	}
1311
1312	ifs = discover_interfaces(-1, UNCONST(argv));
1313	for (ifp = ifs; ifp; ifp = ifp->next) {
1314		if (strcmp(ifp->name, ifname) != 0)
1315			continue;
1316		/* Check if we already have the interface */
1317		for (ifn = ifaces; ifn; ifn = ifn->next) {
1318			if (strcmp(ifn->name, ifp->name) == 0)
1319				break;
1320			ifl = ifn;
1321		}
1322		if (ifn) {
1323			/* The flags and hwaddr could have changed */
1324			ifn->flags = ifp->flags;
1325			ifn->hwlen = ifp->hwlen;
1326			if (ifp->hwlen != 0)
1327				memcpy(ifn->hwaddr, ifp->hwaddr, ifn->hwlen);
1328		} else {
1329			if (ifl)
1330				ifl->next = ifp;
1331			else
1332				ifaces = ifp;
1333		}
1334		init_state(ifp, 0, NULL);
1335		start_interface(ifp);
1336	}
1337}
1338
1339#ifdef RTM_CHGADDR
1340void
1341handle_hwaddr(const char *ifname, unsigned char *hwaddr, size_t hwlen)
1342{
1343	struct interface *ifp;
1344	struct if_options *ifo;
1345
1346	for (ifp = ifaces; ifp; ifp = ifp->next)
1347		if (strcmp(ifp->name, ifname) == 0 && ifp->hwlen <= hwlen) {
1348			ifo = ifp->state->options;
1349			if (!(ifo->options &
1350			    (DHCPCD_INFORM | DHCPCD_STATIC | DHCPCD_CLIENTID))
1351	    		    && ifp->state->new != NULL &&
1352			    ifp->state->new->cookie == htonl(MAGIC_COOKIE))
1353			{
1354				syslog(LOG_INFO,
1355				    "%s: expiring for new hardware address",
1356				    ifp->name);
1357				drop_dhcp(ifp, "EXPIRE");
1358			}
1359			memcpy(ifp->hwaddr, hwaddr, hwlen);
1360			ifp->hwlen = hwlen;
1361			if (!(ifo->options &
1362			    (DHCPCD_INFORM | DHCPCD_STATIC | DHCPCD_CLIENTID)))
1363			{
1364				syslog(LOG_DEBUG, "%s: using hwaddr %s",
1365				    ifp->name,
1366		    		    hwaddr_ntoa(ifp->hwaddr, ifp->hwlen));
1367				ifp->state->interval = 0;
1368				ifp->state->nakoff = 1;
1369				start_interface(ifp);
1370			}
1371		}
1372	free(hwaddr);
1373}
1374#endif
1375
1376void
1377handle_ifa(int type, const char *ifname,
1378    struct in_addr *addr, struct in_addr *net, struct in_addr *dst)
1379{
1380	struct interface *ifp;
1381	struct if_options *ifo;
1382	int i;
1383
1384	if (addr->s_addr == INADDR_ANY)
1385		return;
1386	for (ifp = ifaces; ifp; ifp = ifp->next)
1387		if (strcmp(ifp->name, ifname) == 0)
1388			break;
1389	if (ifp == NULL)
1390		return;
1391
1392	if (type == RTM_DELADDR) {
1393		if (ifp->state->new &&
1394		    ifp->state->new->yiaddr == addr->s_addr)
1395			syslog(LOG_INFO, "%s: removing IP address %s/%d",
1396			    ifp->name, inet_ntoa(ifp->state->lease.addr),
1397			    inet_ntocidr(ifp->state->lease.net));
1398		return;
1399	}
1400
1401	if (type != RTM_NEWADDR)
1402		return;
1403
1404	ifo = ifp->state->options;
1405	if ((ifo->options & (DHCPCD_INFORM | DHCPCD_STATIC)) == 0 ||
1406	    ifo->req_addr.s_addr != INADDR_ANY)
1407		return;
1408
1409	free(ifp->state->old);
1410	ifp->state->old = ifp->state->new;
1411	ifp->state->new = dhcp_message_new(addr, net);
1412	ifp->dst.s_addr = dst ? dst->s_addr : INADDR_ANY;
1413	if (dst) {
1414		for (i = 1; i < 255; i++)
1415			if (i != DHO_ROUTER && has_option_mask(ifo->dstmask,i))
1416				dhcp_message_add_addr(ifp->state->new, i, *dst);
1417	}
1418	ifp->state->reason = "STATIC";
1419	build_routes();
1420	run_script(ifp);
1421	if (ifo->options & DHCPCD_INFORM) {
1422		ifp->state->state = DHS_INFORM;
1423		ifp->state->xid = dhcp_xid(ifp);
1424		ifp->state->lease.server.s_addr =
1425		    dst ? dst->s_addr : INADDR_ANY;
1426		ifp->addr = *addr;
1427		ifp->net = *net;
1428		send_inform(ifp);
1429	}
1430}
1431
1432/* ARGSUSED */
1433static void
1434handle_link(_unused void *arg)
1435{
1436	if (manage_link(linkfd) == -1)
1437		syslog(LOG_ERR, "manage_link: %m");
1438}
1439
1440static void
1441if_reboot(struct interface *iface, int argc, char **argv)
1442{
1443	const struct if_options *ifo;
1444	int opt;
1445
1446	ifo = iface->state->options;
1447	opt = ifo->options;
1448	configure_interface(iface, argc, argv);
1449	ifo = iface->state->options;
1450	iface->state->interval = 0;
1451	if ((ifo->options & (DHCPCD_INFORM | DHCPCD_STATIC) &&
1452		iface->addr.s_addr != ifo->req_addr.s_addr) ||
1453	    (opt & (DHCPCD_INFORM | DHCPCD_STATIC) &&
1454		!(ifo->options & (DHCPCD_INFORM | DHCPCD_STATIC))))
1455	{
1456		drop_dhcp(iface, "EXPIRE");
1457	} else {
1458		free(iface->state->offer);
1459		iface->state->offer = NULL;
1460	}
1461	start_interface(iface);
1462}
1463
1464static void
1465reconf_reboot(int action, int argc, char **argv, int oi)
1466{
1467	struct interface *ifl, *ifn, *ifp, *ifs, *ift;
1468
1469	ifs = discover_interfaces(argc - oi, argv + oi);
1470	if (ifs == NULL)
1471		return;
1472
1473	for (ifp = ifs; ifp && (ift = ifp->next, 1); ifp = ift) {
1474		ifl = NULL;
1475		for (ifn = ifaces; ifn; ifn = ifn->next) {
1476			if (strcmp(ifn->name, ifp->name) == 0)
1477				break;
1478			ifl = ifn;
1479		}
1480		if (ifn) {
1481			if (action)
1482				if_reboot(ifn, argc, argv);
1483			else if (ifn->state->new)
1484				configure(ifn);
1485			free_interface(ifp);
1486		} else {
1487			ifp->next = NULL;
1488			init_state(ifp, argc, argv);
1489			start_interface(ifp);
1490			if (ifl)
1491				ifl->next = ifp;
1492			else
1493				ifaces = ifp;
1494		}
1495	}
1496
1497	sort_interfaces();
1498}
1499
1500/* ARGSUSED */
1501static void
1502handle_signal(_unused void *arg)
1503{
1504	struct interface *ifp;
1505	struct if_options *ifo;
1506	int sig = signal_read();
1507	int do_release, i;
1508
1509	do_release = 0;
1510	switch (sig) {
1511	case SIGINT:
1512		syslog(LOG_INFO, "received SIGINT, stopping");
1513		break;
1514	case SIGTERM:
1515		syslog(LOG_INFO, "received SIGTERM, stopping");
1516		break;
1517	case SIGALRM:
1518		syslog(LOG_INFO, "received SIGALRM, rebinding");
1519		for (i = 0; i < ifac; i++)
1520			free(ifav[i]);
1521		free(ifav);
1522		ifav = NULL;
1523		ifac = 0;
1524		for (i = 0; i < ifdc; i++)
1525			free(ifdv[i]);
1526		free(ifdv);
1527		ifdc = 0;
1528		ifdv = NULL;
1529		ifo = read_config(cffile, NULL, NULL, NULL);
1530		add_options(ifo, margc, margv);
1531		/* We need to preserve these two options. */
1532		if (options & DHCPCD_MASTER)
1533			ifo->options |= DHCPCD_MASTER;
1534		if (options & DHCPCD_DAEMONISED)
1535			ifo->options |= DHCPCD_DAEMONISED;
1536		options = ifo->options;
1537		free_options(ifo);
1538		reconf_reboot(1, ifc, ifv, 0);
1539		return;
1540	case SIGHUP:
1541		syslog(LOG_INFO, "received SIGHUP, releasing");
1542		do_release = 1;
1543		break;
1544	case SIGUSR1:
1545		syslog(LOG_INFO, "received SIGUSR, reconfiguring");
1546		for (ifp = ifaces; ifp; ifp = ifp->next)
1547			if (ifp->state->new)
1548				configure(ifp);
1549		return;
1550	case SIGPIPE:
1551		syslog(LOG_WARNING, "received SIGPIPE");
1552		return;
1553	default:
1554		syslog(LOG_ERR,
1555		    "received signal %d, but don't know what to do with it",
1556		    sig);
1557		return;
1558	}
1559
1560	if (options & DHCPCD_TEST)
1561		exit(EXIT_FAILURE);
1562
1563	/* As drop_dhcp could re-arrange the order, we do it like this. */
1564	for (;;) {
1565		/* Be sane and drop the last config first */
1566		for (ifp = ifaces; ifp; ifp = ifp->next) {
1567			if (ifp->next == NULL)
1568				break;
1569		}
1570		if (ifp == NULL)
1571			break;
1572		if (ifp->carrier != LINK_DOWN &&
1573		    (do_release ||
1574			ifp->state->options->options & DHCPCD_RELEASE))
1575			send_release(ifp);
1576		stop_interface(ifp);
1577	}
1578	exit(EXIT_FAILURE);
1579}
1580
1581int
1582handle_args(struct fd_list *fd, int argc, char **argv)
1583{
1584	struct interface *ifp;
1585	int do_exit = 0, do_release = 0, do_reboot = 0;
1586	int opt, oi = 0;
1587	ssize_t len;
1588	size_t l;
1589	struct iovec iov[2];
1590	char *tmp, *p;
1591
1592	if (fd != NULL) {
1593		/* Special commands for our control socket */
1594		if (strcmp(*argv, "--version") == 0) {
1595			len = strlen(VERSION) + 1;
1596			iov[0].iov_base = &len;
1597			iov[0].iov_len = sizeof(ssize_t);
1598			iov[1].iov_base = UNCONST(VERSION);
1599			iov[1].iov_len = len;
1600			if (writev(fd->fd, iov, 2) == -1) {
1601				syslog(LOG_ERR, "writev: %m");
1602				return -1;
1603			}
1604			return 0;
1605		} else if (strcmp(*argv, "--getconfigfile") == 0) {
1606			len = strlen(cffile ? cffile : CONFIG) + 1;
1607			iov[0].iov_base = &len;
1608			iov[0].iov_len = sizeof(ssize_t);
1609			iov[1].iov_base = cffile ? cffile : UNCONST(CONFIG);
1610			iov[1].iov_len = len;
1611			if (writev(fd->fd, iov, 2) == -1) {
1612				syslog(LOG_ERR, "writev: %m");
1613				return -1;
1614			}
1615			return 0;
1616		} else if (strcmp(*argv, "--getinterfaces") == 0) {
1617			len = 0;
1618			if (argc == 1) {
1619				for (ifp = ifaces; ifp; ifp = ifp->next) {
1620					len++;
1621					if (ipv6rs_has_ra(ifp))
1622						len++;
1623				}
1624				len = write(fd->fd, &len, sizeof(len));
1625				if (len != sizeof(len))
1626					return -1;
1627				for (ifp = ifaces; ifp; ifp = ifp->next)
1628					send_interface(fd->fd, ifp);
1629				return 0;
1630			}
1631			opt = 0;
1632			while (argv[++opt] != NULL) {
1633				for (ifp = ifaces; ifp; ifp = ifp->next)
1634					if (strcmp(argv[opt], ifp->name) == 0) {
1635						len++;
1636						if (ipv6rs_has_ra(ifp))
1637							len++;
1638					}
1639			}
1640			len = write(fd->fd, &len, sizeof(len));
1641			if (len != sizeof(len))
1642				return -1;
1643			opt = 0;
1644			while (argv[++opt] != NULL) {
1645				for (ifp = ifaces; ifp; ifp = ifp->next)
1646					if (strcmp(argv[opt], ifp->name) == 0)
1647						send_interface(fd->fd, ifp);
1648			}
1649			return 0;
1650		} else if (strcmp(*argv, "--listen") == 0) {
1651			fd->listener = 1;
1652			return 0;
1653		}
1654	}
1655
1656	/* Log the command */
1657	len = 0;
1658	for (opt = 0; opt < argc; opt++)
1659		len += strlen(argv[opt]) + 1;
1660	tmp = p = xmalloc(len + 1);
1661	for (opt = 0; opt < argc; opt++) {
1662		l = strlen(argv[opt]);
1663		strlcpy(p, argv[opt], l + 1);
1664		p += l;
1665		*p++ = ' ';
1666	}
1667	*--p = '\0';
1668	syslog(LOG_INFO, "control command: %s", tmp);
1669	free(tmp);
1670
1671	optind = 0;
1672	while ((opt = getopt_long(argc, argv, IF_OPTS, cf_options, &oi)) != -1)
1673	{
1674		switch (opt) {
1675		case 'g':
1676			/* Assumed if below not set */
1677			break;
1678		case 'k':
1679			do_release = 1;
1680			break;
1681		case 'n':
1682			do_reboot = 1;
1683			break;
1684		case 'x':
1685			do_exit = 1;
1686			break;
1687		}
1688	}
1689
1690	/* We need at least one interface */
1691	if (optind == argc) {
1692		syslog(LOG_ERR, "handle_args: no interface");
1693		return -1;
1694	}
1695
1696	if (do_release || do_exit) {
1697		for (oi = optind; oi < argc; oi++) {
1698			for (ifp = ifaces; ifp; ifp = ifp->next)
1699				if (strcmp(ifp->name, argv[oi]) == 0)
1700					break;
1701			if (!ifp)
1702				continue;
1703			if (do_release)
1704				ifp->state->options->options |= DHCPCD_RELEASE;
1705			if (ifp->state->options->options & DHCPCD_RELEASE &&
1706			    ifp->carrier != LINK_DOWN)
1707				send_release(ifp);
1708			stop_interface(ifp);
1709		}
1710		return 0;
1711	}
1712
1713	reconf_reboot(do_reboot, argc, argv, optind);
1714	return 0;
1715}
1716
1717int
1718open_sockets(struct interface *iface)
1719{
1720	int r = 0;
1721
1722	if (iface->raw_fd == -1) {
1723		if ((r = open_socket(iface, ETHERTYPE_IP)) == -1)
1724			syslog(LOG_ERR, "%s: open_socket: %m", iface->name);
1725		else
1726			add_event(iface->raw_fd, handle_dhcp_packet, iface);
1727	}
1728	if (iface->udp_fd == -1 &&
1729	    iface->addr.s_addr != 0 &&
1730	    iface->state->new != NULL &&
1731	    (iface->state->new->cookie == htonl(MAGIC_COOKIE) ||
1732	    iface->state->options->options & DHCPCD_INFORM))
1733	{
1734		if (open_udp_socket(iface) == -1 && errno != EADDRINUSE) {
1735			syslog(LOG_ERR, "%s: open_udp_socket: %m", iface->name);
1736			r = -1;
1737		}
1738	}
1739	return r;
1740}
1741
1742void
1743close_sockets(struct interface *iface)
1744{
1745	if (iface->arp_fd != -1) {
1746		delete_event(iface->arp_fd);
1747		close(iface->arp_fd);
1748		iface->arp_fd = -1;
1749	}
1750	if (iface->raw_fd != -1) {
1751		delete_event(iface->raw_fd);
1752		close(iface->raw_fd);
1753		iface->raw_fd = -1;
1754	}
1755	if (iface->udp_fd != -1) {
1756		/* we don't listen to events on the udp */
1757		close(iface->udp_fd);
1758		iface->udp_fd = -1;
1759	}
1760}
1761
1762int
1763main(int argc, char **argv)
1764{
1765	struct interface *iface;
1766	int opt, oi = 0, signal_fd, sig = 0, i, control_fd;
1767	size_t len;
1768	pid_t pid;
1769	struct timespec ts;
1770
1771	closefrom(3);
1772	openlog(PACKAGE, LOG_PERROR | LOG_PID, LOG_DAEMON);
1773	setlogmask(LOG_UPTO(LOG_INFO));
1774
1775	/* Test for --help and --version */
1776	if (argc > 1) {
1777		if (strcmp(argv[1], "--help") == 0) {
1778			usage();
1779			exit(EXIT_SUCCESS);
1780		} else if (strcmp(argv[1], "--version") == 0) {
1781			printf(""PACKAGE" "VERSION"\n%s\n", copyright);
1782			exit(EXIT_SUCCESS);
1783		}
1784	}
1785
1786	i = 0;
1787	while ((opt = getopt_long(argc, argv, IF_OPTS, cf_options, &oi)) != -1)
1788	{
1789		switch (opt) {
1790		case 'f':
1791			cffile = optarg;
1792			break;
1793		case 'g':
1794			sig = SIGUSR1;
1795			break;
1796		case 'k':
1797			sig = SIGHUP;
1798			break;
1799		case 'n':
1800			sig = SIGALRM;
1801			break;
1802		case 'x':
1803			sig = SIGTERM;
1804			break;
1805		case 'T':
1806			i = 1;
1807			break;
1808		case 'U':
1809			i = 2;
1810			break;
1811		case 'V':
1812			print_options();
1813			exit(EXIT_SUCCESS);
1814		case '?':
1815			usage();
1816			exit(EXIT_FAILURE);
1817		}
1818	}
1819
1820	margv = argv;
1821	margc = argc;
1822	if_options = read_config(cffile, NULL, NULL, NULL);
1823	opt = add_options(if_options, argc, argv);
1824	if (opt != 1) {
1825		if (opt == 0)
1826			usage();
1827		exit(EXIT_FAILURE);
1828	}
1829	options = if_options->options;
1830	if (i != 0) {
1831		if (i == 1)
1832			options |= DHCPCD_TEST;
1833		else
1834			options |= DHCPCD_DUMPLEASE;
1835		options |= DHCPCD_PERSISTENT;
1836		options &= ~DHCPCD_DAEMONISE;
1837	}
1838
1839#ifdef THERE_IS_NO_FORK
1840	options &= ~DHCPCD_DAEMONISE;
1841#endif
1842
1843	if (options & DHCPCD_DEBUG)
1844		setlogmask(LOG_UPTO(LOG_DEBUG));
1845	if (options & DHCPCD_QUIET)
1846		close(STDERR_FILENO);
1847
1848	if (!(options & (DHCPCD_TEST | DHCPCD_DUMPLEASE))) {
1849		/* If we have any other args, we should run as a single dhcpcd
1850		 *  instance for that interface. */
1851		len = strlen(PIDFILE) + IF_NAMESIZE + 2;
1852		pidfile = xmalloc(len);
1853		if (optind == argc - 1)
1854			snprintf(pidfile, len, PIDFILE, "-", argv[optind]);
1855		else {
1856			snprintf(pidfile, len, PIDFILE, "", "");
1857			options |= DHCPCD_MASTER;
1858		}
1859	}
1860
1861	if (chdir("/") == -1)
1862		syslog(LOG_ERR, "chdir `/': %m");
1863	atexit(cleanup);
1864
1865	if (options & DHCPCD_DUMPLEASE) {
1866		if (optind != argc - 1) {
1867			syslog(LOG_ERR, "dumplease requires an interface");
1868			exit(EXIT_FAILURE);
1869		}
1870		ifaces = iface = xzalloc(sizeof(*iface));
1871		strlcpy(iface->name, argv[optind], sizeof(iface->name));
1872		snprintf(iface->leasefile, sizeof(iface->leasefile),
1873		    LEASEFILE, iface->name);
1874		iface->state = xzalloc(sizeof(*iface->state));
1875		iface->state->options = xzalloc(sizeof(*iface->state->options));
1876		strlcpy(iface->state->options->script, if_options->script,
1877		    sizeof(iface->state->options->script));
1878		iface->state->new = read_lease(iface);
1879		if (iface->state->new == NULL && errno == ENOENT) {
1880			strlcpy(iface->leasefile, argv[optind],
1881			    sizeof(iface->leasefile));
1882			iface->state->new = read_lease(iface);
1883		}
1884		if (iface->state->new == NULL) {
1885			if (errno == ENOENT)
1886				syslog(LOG_ERR, "%s: no lease to dump",
1887				    iface->name);
1888			exit(EXIT_FAILURE);
1889		}
1890		iface->state->reason = "DUMP";
1891		run_script(iface);
1892		exit(EXIT_SUCCESS);
1893	}
1894
1895	if (!(options & (DHCPCD_MASTER | DHCPCD_TEST))) {
1896		control_fd = open_control();
1897		if (control_fd != -1) {
1898			syslog(LOG_INFO,
1899			    "sending commands to master dhcpcd process");
1900			i = send_control(argc, argv);
1901			if (i > 0) {
1902				syslog(LOG_DEBUG, "send OK");
1903				exit(EXIT_SUCCESS);
1904			} else {
1905				syslog(LOG_ERR, "failed to send commands");
1906				exit(EXIT_FAILURE);
1907			}
1908		} else {
1909			if (errno != ENOENT)
1910				syslog(LOG_ERR, "open_control: %m");
1911		}
1912	}
1913
1914	if (geteuid())
1915		syslog(LOG_WARNING,
1916		    PACKAGE " will not work correctly unless run as root");
1917
1918	if (sig != 0) {
1919		pid = read_pid();
1920		if (pid != 0)
1921			syslog(LOG_INFO, "sending signal %d to pid %d",
1922			    sig, pid);
1923		if (pid == 0 || kill(pid, sig) != 0) {
1924			if (sig != SIGALRM && errno != EPERM)
1925				syslog(LOG_ERR, ""PACKAGE" not running");
1926			if (pid != 0 && errno != ESRCH) {
1927				syslog(LOG_ERR, "kill: %m");
1928				exit(EXIT_FAILURE);
1929			}
1930			unlink(pidfile);
1931			if (sig != SIGALRM)
1932				exit(EXIT_FAILURE);
1933		} else {
1934			if (sig == SIGALRM || sig == SIGUSR1)
1935				exit(EXIT_SUCCESS);
1936			/* Spin until it exits */
1937			syslog(LOG_INFO, "waiting for pid %d to exit", pid);
1938			ts.tv_sec = 0;
1939			ts.tv_nsec = 100000000; /* 10th of a second */
1940			for(i = 0; i < 100; i++) {
1941				nanosleep(&ts, NULL);
1942				if (read_pid() == 0)
1943					exit(EXIT_SUCCESS);
1944			}
1945			syslog(LOG_ERR, "pid %d failed to exit", pid);
1946			exit(EXIT_FAILURE);
1947		}
1948	}
1949
1950	if (!(options & DHCPCD_TEST)) {
1951		if ((pid = read_pid()) > 0 &&
1952		    kill(pid, 0) == 0)
1953		{
1954			syslog(LOG_ERR, ""PACKAGE
1955			    " already running on pid %d (%s)",
1956			    pid, pidfile);
1957			exit(EXIT_FAILURE);
1958		}
1959
1960		/* Ensure we have the needed directories */
1961		if (mkdir(RUNDIR, 0755) == -1 && errno != EEXIST)
1962			syslog(LOG_ERR, "mkdir `%s': %m", RUNDIR);
1963		if (mkdir(DBDIR, 0755) == -1 && errno != EEXIST)
1964			syslog(LOG_ERR, "mkdir `%s': %m", DBDIR);
1965
1966		pidfd = open(pidfile, O_WRONLY | O_CREAT | O_NONBLOCK, 0664);
1967		if (pidfd == -1)
1968			syslog(LOG_ERR, "open `%s': %m", pidfile);
1969		else {
1970			/* Lock the file so that only one instance of dhcpcd
1971			 * runs on an interface */
1972			if (flock(pidfd, LOCK_EX | LOCK_NB) == -1) {
1973				syslog(LOG_ERR, "flock `%s': %m", pidfile);
1974				exit(EXIT_FAILURE);
1975			}
1976			if (set_cloexec(pidfd) == -1)
1977				exit(EXIT_FAILURE);
1978			writepid(pidfd, getpid());
1979		}
1980	}
1981
1982	syslog(LOG_INFO, "version " VERSION " starting");
1983
1984	if ((signal_fd = signal_init()) == -1)
1985		exit(EXIT_FAILURE);
1986	if (signal_setup() == -1)
1987		exit(EXIT_FAILURE);
1988	add_event(signal_fd, handle_signal, NULL);
1989
1990	if (options & DHCPCD_MASTER) {
1991		if (start_control() == -1)
1992			syslog(LOG_ERR, "start_control: %m");
1993	}
1994
1995	if (init_sockets() == -1) {
1996		syslog(LOG_ERR, "init_socket: %m");
1997		exit(EXIT_FAILURE);
1998	}
1999	if (if_options->options & DHCPCD_LINK) {
2000		linkfd = open_link_socket();
2001		if (linkfd == -1)
2002			syslog(LOG_ERR, "open_link_socket: %m");
2003		else
2004			add_event(linkfd, handle_link, NULL);
2005	}
2006
2007#if 0
2008	if (options & DHCPCD_IPV6RS && disable_rtadv() == -1) {
2009		syslog(LOG_ERR, "ipv6rs: %m");
2010		options &= ~DHCPCD_IPV6RS;
2011	}
2012#endif
2013
2014	if (options & DHCPCD_IPV6RS && !check_ipv6(NULL))
2015		options &= ~DHCPCD_IPV6RS;
2016	if (options & DHCPCD_IPV6RS && ipv6_open() == -1) {
2017		options &= ~DHCPCD_IPV6RS;
2018		syslog(LOG_ERR, "ipv6_open: %m");
2019	}
2020	if (options & DHCPCD_IPV6RS) {
2021		ipv6rsfd = ipv6rs_open();
2022		if (ipv6rsfd == -1) {
2023			syslog(LOG_ERR, "ipv6rs: %m");
2024			options &= ~DHCPCD_IPV6RS;
2025		} else {
2026			add_event(ipv6rsfd, ipv6rs_handledata, NULL);
2027//			atexit(restore_rtadv);
2028		}
2029		if (options & DHCPCD_IPV6RA_OWN ||
2030		    options & DHCPCD_IPV6RA_OWN_DEFAULT)
2031		{
2032			ipv6nsfd = ipv6ns_open();
2033			if (ipv6nsfd == -1)
2034				syslog(LOG_ERR, "ipv6nd: %m");
2035			else
2036				add_event(ipv6nsfd, ipv6ns_handledata, NULL);
2037		}
2038	}
2039
2040	ifc = argc - optind;
2041	ifv = argv + optind;
2042
2043	/* When running dhcpcd against a single interface, we need to retain
2044	 * the old behaviour of waiting for an IP address */
2045	if (ifc == 1)
2046		options |= DHCPCD_WAITIP;
2047
2048	ifaces = discover_interfaces(ifc, ifv);
2049	for (i = 0; i < ifc; i++) {
2050		for (iface = ifaces; iface; iface = iface->next)
2051			if (strcmp(iface->name, ifv[i]) == 0)
2052				break;
2053		if (!iface)
2054			syslog(LOG_ERR, "%s: interface not found or invalid",
2055			    ifv[i]);
2056	}
2057	if (!ifaces) {
2058		if (ifc == 0)
2059			syslog(LOG_ERR, "no valid interfaces found");
2060		else
2061			exit(EXIT_FAILURE);
2062		if (!(options & DHCPCD_LINK)) {
2063			syslog(LOG_ERR,
2064			    "aborting as link detection is disabled");
2065			exit(EXIT_FAILURE);
2066		}
2067	}
2068
2069	if (options & DHCPCD_BACKGROUND)
2070		daemonise();
2071
2072	opt = 0;
2073	for (iface = ifaces; iface; iface = iface->next) {
2074		init_state(iface, argc, argv);
2075		if (iface->carrier != LINK_DOWN)
2076			opt = 1;
2077	}
2078
2079	if (!(options & DHCPCD_BACKGROUND)) {
2080		/* If we don't have a carrier, we may have to wait for a second
2081		 * before one becomes available if we brought an interface up */
2082		if (opt == 0 &&
2083		    options & DHCPCD_LINK &&
2084		    options & DHCPCD_WAITUP &&
2085		    !(options & DHCPCD_WAITIP))
2086		{
2087			ts.tv_sec = 1;
2088			ts.tv_nsec = 0;
2089			nanosleep(&ts, NULL);
2090			for (iface = ifaces; iface; iface = iface->next) {
2091				handle_carrier(0, 0, iface->name);
2092				if (iface->carrier != LINK_DOWN) {
2093					opt = 1;
2094					break;
2095				}
2096			}
2097		}
2098		if (options & DHCPCD_MASTER)
2099			i = if_options->timeout;
2100		else
2101			i = ifaces->state->options->timeout;
2102		if (opt == 0 &&
2103		    options & DHCPCD_LINK &&
2104		    !(options & DHCPCD_WAITIP))
2105		{
2106			syslog(LOG_WARNING, "no interfaces have a carrier");
2107			daemonise();
2108		} else if (i > 0) {
2109			if (options & DHCPCD_IPV4LL)
2110				options |= DHCPCD_TIMEOUT_IPV4LL;
2111			add_timeout_sec(i, handle_exit_timeout, NULL);
2112		}
2113	}
2114	free_options(if_options);
2115	if_options = NULL;
2116
2117	sort_interfaces();
2118	for (iface = ifaces; iface; iface = iface->next)
2119		add_timeout_sec(0, start_interface, iface);
2120
2121	start_eloop();
2122	exit(EXIT_SUCCESS);
2123}
2124