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