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