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