]> git.ipfire.org Git - thirdparty/dhcpcd.git/blob - dhcpcd.c
Allow string and binhex types to have lengths.
[thirdparty/dhcpcd.git] / dhcpcd.c
1 /*
2 * dhcpcd - DHCP client daemon
3 * Copyright (c) 2006-2013 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-2013 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 #include <sys/utsname.h>
37
38 #include <net/route.h> /* For RTM_CHGADDR */
39
40 #include <ctype.h>
41 #include <errno.h>
42 #include <getopt.h>
43 #include <limits.h>
44 #include <paths.h>
45 #include <signal.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <syslog.h>
50 #include <unistd.h>
51 #include <time.h>
52
53 #include "arp.h"
54 #include "config.h"
55 #include "common.h"
56 #include "control.h"
57 #include "dev.h"
58 #include "dhcpcd.h"
59 #include "dhcp6.h"
60 #include "duid.h"
61 #include "eloop.h"
62 #include "if-options.h"
63 #include "if-pref.h"
64 #include "ipv4.h"
65 #include "ipv6.h"
66 #include "ipv6nd.h"
67 #include "net.h"
68 #include "platform.h"
69 #include "script.h"
70
71 struct if_head *ifaces = NULL;
72 char vendor[VENDORCLASSID_MAX_LEN];
73 int pidfd = -1;
74 struct if_options *if_options = NULL;
75 int ifac = 0;
76 char **ifav = NULL;
77 int ifdc = 0;
78 char **ifdv = NULL;
79
80 sigset_t dhcpcd_sigset;
81 const int handle_sigs[] = {
82 SIGALRM,
83 SIGHUP,
84 SIGINT,
85 SIGPIPE,
86 SIGTERM,
87 SIGUSR1,
88 0
89 };
90
91 static char *cffile;
92 static char *pidfile;
93 static int linkfd = -1;
94 static char **ifv;
95 static int ifc;
96 static char **margv;
97 static int margc;
98
99 static pid_t
100 read_pid(void)
101 {
102 FILE *fp;
103 pid_t pid;
104
105 if ((fp = fopen(pidfile, "r")) == NULL) {
106 errno = ENOENT;
107 return 0;
108 }
109 if (fscanf(fp, "%d", &pid) != 1)
110 pid = 0;
111 fclose(fp);
112 return pid;
113 }
114
115 static void
116 usage(void)
117 {
118
119 printf("usage: "PACKAGE"\t[-46ABbDdEGgHJKkLnpqTVw]\n"
120 "\t\t[-C, --nohook hook] [-c, --script script]\n"
121 "\t\t[-e, --env value] [-F, --fqdn FQDN] [-f, --config file]\n"
122 "\t\t[-h, --hostname hostname] [-I, --clientid clientid]\n"
123 "\t\t[-i, --vendorclassid vendorclassid] [-l, --leasetime seconds]\n"
124 "\t\t[-m, --metric metric] [-O, --nooption option]\n"
125 "\t\t[-o, --option option] [-Q, --require option]\n"
126 "\t\t[-r, --request address] [-S, --static value]\n"
127 "\t\t[-s, --inform address[/cidr]] [-t, --timeout seconds]\n"
128 "\t\t[-u, --userclass class] [-v, --vendor code, value]\n"
129 "\t\t[-W, --whitelist address[/cidr]] [-y, --reboot seconds]\n"
130 "\t\t[-X, --blacklist address[/cidr]] [-Z, --denyinterfaces pattern]\n"
131 "\t\t[-z, --allowinterfaces pattern] [interface] [...]\n"
132 " "PACKAGE"\t-k, --release [interface]\n"
133 " "PACKAGE"\t-U, --dumplease interface\n"
134 " "PACKAGE"\t--version\n"
135 " "PACKAGE"\t-x, --exit [interface]\n");
136 }
137
138 static void
139 free_globals(void)
140 {
141 int i;
142 size_t n;
143
144 for (i = 0; i < ifac; i++)
145 free(ifav[i]);
146 free(ifav);
147 for (i = 0; i < ifdc; i++)
148 free(ifdv[i]);
149 free(ifdv);
150
151 #ifdef INET
152 for (n = 0; n < dhcp_eopts_len; n++)
153 free_dhcp_opt_embenc(&dhcp_eopts[n]);
154 free(dhcp_eopts);
155 #endif
156 #ifdef INET6
157 for (n = 0; n < dhcp6_eopts_len; n++)
158 free_dhcp_opt_embenc(&dhcp6_eopts[n]);
159 free(dhcp6_eopts);
160 #endif
161 }
162
163 static void
164 cleanup(void)
165 {
166 #ifdef DEBUG_MEMORY
167 struct interface *ifp;
168
169 free(duid);
170 free_options(if_options);
171
172 if (ifaces) {
173 while ((ifp = TAILQ_FIRST(ifaces))) {
174 TAILQ_REMOVE(ifaces, ifp, next);
175 free_interface(ifp);
176 }
177 free(ifaces);
178 }
179
180 free_globals();
181 #endif
182
183 if (!(options & DHCPCD_FORKED))
184 dev_stop();
185 if (linkfd != -1)
186 close(linkfd);
187 if (pidfd > -1) {
188 if (options & DHCPCD_MASTER) {
189 if (control_stop() == -1)
190 syslog(LOG_ERR, "control_stop: %m");
191 }
192 close(pidfd);
193 unlink(pidfile);
194 }
195 #ifdef DEBUG_MEMORY
196 free(pidfile);
197 #endif
198
199 if (options & DHCPCD_STARTED && !(options & DHCPCD_FORKED))
200 syslog(LOG_INFO, "exited");
201 }
202
203 /* ARGSUSED */
204 static void
205 handle_exit_timeout(__unused void *arg)
206 {
207 int timeout;
208
209 syslog(LOG_ERR, "timed out");
210 if (!(options & DHCPCD_IPV4) || !(options & DHCPCD_TIMEOUT_IPV4LL)) {
211 if (options & DHCPCD_MASTER) {
212 /* We've timed out, so remove the waitip requirements.
213 * If the user doesn't like this they can always set
214 * an infinite timeout. */
215 options &=
216 ~(DHCPCD_WAITIP | DHCPCD_WAITIP4 | DHCPCD_WAITIP6);
217 daemonise();
218 return;
219 } else
220 exit(EXIT_FAILURE);
221 }
222 options &= ~DHCPCD_TIMEOUT_IPV4LL;
223 timeout = (PROBE_NUM * PROBE_MAX) + (PROBE_WAIT * 2);
224 syslog(LOG_WARNING, "allowing %d seconds for IPv4LL timeout", timeout);
225 eloop_timeout_add_sec(timeout, handle_exit_timeout, NULL);
226 }
227
228 pid_t
229 daemonise(void)
230 {
231 #ifdef THERE_IS_NO_FORK
232 return -1;
233 #else
234 pid_t pid;
235 char buf = '\0';
236 int sidpipe[2], fd;
237
238 if (options & DHCPCD_DAEMONISE && !(options & DHCPCD_DAEMONISED)) {
239 if (options & DHCPCD_WAITIP4 &&
240 !ipv4_addrexists(NULL))
241 return -1;
242 if (options & DHCPCD_WAITIP6 &&
243 !ipv6nd_addrexists(NULL) &&
244 !dhcp6_addrexists(NULL))
245 return -1;
246 if ((options &
247 (DHCPCD_WAITIP | DHCPCD_WAITIP4 | DHCPCD_WAITIP6)) ==
248 DHCPCD_WAITIP &&
249 !ipv4_addrexists(NULL) &&
250 !ipv6nd_addrexists(NULL) &&
251 !dhcp6_addrexists(NULL))
252 return -1;
253 }
254
255 eloop_timeout_delete(handle_exit_timeout, NULL);
256 if (options & DHCPCD_DAEMONISED || !(options & DHCPCD_DAEMONISE))
257 return 0;
258 /* Setup a signal pipe so parent knows when to exit. */
259 if (pipe(sidpipe) == -1) {
260 syslog(LOG_ERR, "pipe: %m");
261 return -1;
262 }
263 syslog(LOG_DEBUG, "forking to background");
264 switch (pid = fork()) {
265 case -1:
266 syslog(LOG_ERR, "fork: %m");
267 exit(EXIT_FAILURE);
268 /* NOTREACHED */
269 case 0:
270 setsid();
271 /* Notify parent it's safe to exit as we've detached. */
272 close(sidpipe[0]);
273 if (write(sidpipe[1], &buf, 1) == -1)
274 syslog(LOG_ERR, "failed to notify parent: %m");
275 close(sidpipe[1]);
276 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
277 dup2(fd, STDIN_FILENO);
278 dup2(fd, STDOUT_FILENO);
279 dup2(fd, STDERR_FILENO);
280 close(fd);
281 }
282 break;
283 default:
284 /* Wait for child to detach */
285 close(sidpipe[1]);
286 if (read(sidpipe[0], &buf, 1) == -1)
287 syslog(LOG_ERR, "failed to read child: %m");
288 close(sidpipe[0]);
289 break;
290 }
291 /* Done with the fd now */
292 if (pid != 0) {
293 syslog(LOG_INFO, "forked to background, child pid %d",pid);
294 writepid(pidfd, pid);
295 close(pidfd);
296 pidfd = -1;
297 options |= DHCPCD_FORKED;
298 exit(EXIT_SUCCESS);
299 }
300 options |= DHCPCD_DAEMONISED;
301 return pid;
302 #endif
303 }
304
305 struct interface *
306 find_interface(const char *ifname)
307 {
308 struct interface *ifp;
309
310 TAILQ_FOREACH(ifp, ifaces, next) {
311 if (strcmp(ifp->name, ifname) == 0)
312 return ifp;
313 }
314 return NULL;
315 }
316
317 static void
318 stop_interface(struct interface *ifp)
319 {
320
321 syslog(LOG_INFO, "%s: removing interface", ifp->name);
322 ifp->options->options |= DHCPCD_STOPPING;
323
324 // Remove the interface from our list
325 TAILQ_REMOVE(ifaces, ifp, next);
326 dhcp6_drop(ifp, NULL);
327 ipv6nd_drop(ifp);
328 dhcp_drop(ifp, "STOP");
329 dhcp_close(ifp);
330 eloop_timeout_delete(NULL, ifp);
331 if (ifp->options->options & DHCPCD_DEPARTED)
332 script_runreason(ifp, "DEPARTED");
333 free_interface(ifp);
334 if (!(options & (DHCPCD_MASTER | DHCPCD_TEST)))
335 exit(EXIT_FAILURE);
336 }
337
338 static void
339 configure_interface1(struct interface *ifp)
340 {
341 struct if_options *ifo = ifp->options;
342 int ra_global, ra_iface;
343
344 /* Do any platform specific configuration */
345 if_conf(ifp);
346
347 /* If we want to release a lease, we can't really persist the
348 * address either. */
349 if (ifo->options & DHCPCD_RELEASE)
350 ifo->options &= ~DHCPCD_PERSISTENT;
351
352 if (ifp->flags & IFF_POINTOPOINT && !(ifo->options & DHCPCD_INFORM))
353 ifo->options |= DHCPCD_STATIC;
354 if (ifp->flags & IFF_NOARP ||
355 ifo->options & (DHCPCD_INFORM | DHCPCD_STATIC))
356 ifo->options &= ~(DHCPCD_ARP | DHCPCD_IPV4LL);
357 if (!(ifp->flags & (IFF_POINTOPOINT | IFF_LOOPBACK | IFF_MULTICAST)))
358 ifo->options &= ~DHCPCD_IPV6RS;
359 if (ifo->options & DHCPCD_LINK && carrier_status(ifp) == LINK_UNKNOWN)
360 ifo->options &= ~DHCPCD_LINK;
361
362 if (ifo->metric != -1)
363 ifp->metric = ifo->metric;
364
365 if (!(ifo->options & DHCPCD_IPV6))
366 ifo->options &= ~DHCPCD_IPV6RS;
367
368 /* We want to disable kernel interface RA as early as possible. */
369 if (ifo->options & DHCPCD_IPV6RS) {
370 ra_global = check_ipv6(NULL, options & DHCPCD_IPV6RA_OWN ? 1:0);
371 ra_iface = check_ipv6(ifp->name,
372 ifp->options->options & DHCPCD_IPV6RA_OWN ? 1 : 0);
373 if (ra_global == -1 || ra_iface == -1)
374 ifo->options &= ~DHCPCD_IPV6RS;
375 else if (ra_iface == 0)
376 ifo->options |= DHCPCD_IPV6RA_OWN;
377 }
378
379 /* If we haven't specified a ClientID and our hardware address
380 * length is greater than DHCP_CHADDR_LEN then we enforce a ClientID
381 * of the hardware address family and the hardware address. */
382 if (ifp->hwlen > DHCP_CHADDR_LEN)
383 ifo->options |= DHCPCD_CLIENTID;
384
385 /* Firewire and InfiniBand interfaces require ClientID and
386 * the broadcast option being set. */
387 switch (ifp->family) {
388 case ARPHRD_IEEE1394: /* FALLTHROUGH */
389 case ARPHRD_INFINIBAND:
390 ifo->options |= DHCPCD_CLIENTID | DHCPCD_BROADCAST;
391 break;
392 }
393
394 if (!(ifo->options & DHCPCD_IAID)) {
395 /*
396 * An IAID is for identifying a unqiue interface within
397 * the client. It is 4 bytes long. Working out a default
398 * value is problematic.
399 *
400 * Interface name and number are not stable
401 * between different OS's. Some OS's also cannot make
402 * up their mind what the interface should be called
403 * (yes, udev, I'm looking at you).
404 * Also, the name could be longer than 4 bytes.
405 * Also, with pluggable interfaces the name and index
406 * could easily get swapped per actual interface.
407 *
408 * The MAC address is 6 bytes long, the final 3
409 * being unique to the manufacturer and the initial 3
410 * being unique to the organisation which makes it.
411 * We could use the last 4 bytes of the MAC address
412 * as the IAID as it's the most stable part given the
413 * above, but equally it's not guaranteed to be
414 * unique.
415 *
416 * Given the above, and our need to reliably work
417 * between reboots without persitent storage,
418 * generating the IAID from the MAC address is the only
419 * logical default.
420 *
421 * dhclient uses the last 4 bytes of the MAC address.
422 * dibbler uses an increamenting counter.
423 * wide-dhcpv6 uses 0 or a configured value.
424 * odhcp6c uses 1.
425 * Windows 7 uses the first 3 bytes of the MAC address
426 * and an unknown byte.
427 * dhcpcd-6.1.0 and earlier used the interface name,
428 * falling back to interface index if name > 4.
429 */
430 memcpy(ifo->iaid, ifp->hwaddr + ifp->hwlen - sizeof(ifo->iaid),
431 sizeof(ifo->iaid));
432 #if 0
433 len = strlen(ifp->name);
434 if (len <= sizeof(ifo->iaid)) {
435 memcpy(ifo->iaid, ifp->name, len);
436 memset(ifo->iaid + len, 0, sizeof(ifo->iaid) - len);
437 } else {
438 /* IAID is the same size as a uint32_t */
439 len = htonl(ifp->index);
440 memcpy(ifo->iaid, &len, sizeof(len));
441 }
442 #endif
443 ifo->options |= DHCPCD_IAID;
444 }
445
446 #ifdef INET6
447 if (ifo->ia == NULL && ifo->options & DHCPCD_IPV6) {
448 ifo->ia = malloc(sizeof(*ifo->ia));
449 if (ifo->ia == NULL)
450 syslog(LOG_ERR, "%s: %m", __func__);
451 else {
452 if (ifo->ia_type == 0)
453 ifo->ia_type = D6_OPTION_IA_NA;
454 memcpy(ifo->ia->iaid, ifo->iaid, sizeof(ifo->iaid));
455 ifo->ia_len = 1;
456 ifo->ia->sla = NULL;
457 ifo->ia->sla_len = 0;
458 }
459 }
460 #endif
461 }
462
463 int
464 select_profile(struct interface *ifp, const char *profile)
465 {
466 struct if_options *ifo;
467 int ret;
468
469 ret = 0;
470 ifo = read_config(cffile, ifp->name, ifp->ssid, profile);
471 if (ifo == NULL) {
472 syslog(LOG_DEBUG, "%s: no profile %s", ifp->name, profile);
473 ret = -1;
474 goto exit;
475 }
476 if (profile != NULL) {
477 strlcpy(ifp->profile, profile, sizeof(ifp->profile));
478 syslog(LOG_INFO, "%s: selected profile %s",
479 ifp->name, profile);
480 } else
481 *ifp->profile = '\0';
482 free_options(ifp->options);
483 ifp->options = ifo;
484
485 exit:
486 if (profile)
487 configure_interface1(ifp);
488 return ret;
489 }
490
491 static void
492 configure_interface(struct interface *ifp, int argc, char **argv)
493 {
494
495 select_profile(ifp, NULL);
496 add_options(ifp->options, argc, argv);
497 configure_interface1(ifp);
498 }
499
500 void
501 handle_carrier(int carrier, int flags, const char *ifname)
502 {
503 struct interface *ifp;
504
505 ifp = find_interface(ifname);
506 if (ifp == NULL || !(ifp->options->options & DHCPCD_LINK))
507 return;
508
509 if (carrier == LINK_UNKNOWN)
510 carrier = carrier_status(ifp); /* will set ifp->flags */
511 else
512 ifp->flags = flags;
513
514 if (carrier == LINK_UNKNOWN)
515 syslog(LOG_ERR, "%s: carrier_status: %m", ifname);
516 /* IFF_RUNNING is checked, if needed, earlier and is OS dependant */
517 else if (carrier == LINK_DOWN || (ifp->flags & IFF_UP) == 0) {
518 if (ifp->carrier != LINK_DOWN) {
519 if (ifp->carrier == LINK_UP)
520 syslog(LOG_INFO, "%s: carrier lost", ifp->name);
521 ifp->carrier = LINK_DOWN;
522 dhcp_close(ifp);
523 dhcp6_drop(ifp, "EXPIRE6");
524 ipv6nd_drop(ifp);
525 /* Don't blindly delete our knowledge of LL addresses.
526 * We need to listen to what the kernel does with
527 * them as some OS's will remove, mark tentative or
528 * do nothing. */
529 ipv6_free_ll_callbacks(ifp);
530 dhcp_drop(ifp, "NOCARRIER");
531 }
532 } else if (carrier == LINK_UP && ifp->flags & IFF_UP) {
533 if (ifp->carrier != LINK_UP) {
534 syslog(LOG_INFO, "%s: carrier acquired", ifp->name);
535 ifp->carrier = LINK_UP;
536 #if !defined(__linux__) && !defined(__NetBSD__)
537 /* BSD does not emit RTM_NEWADDR or RTM_CHGADDR when the
538 * hardware address changes so we have to go
539 * through the disovery process to work it out. */
540 handle_interface(0, ifp->name);
541 #endif
542 if (ifp->wireless)
543 getifssid(ifp->name, ifp->ssid);
544 configure_interface(ifp, margc, margv);
545 script_runreason(ifp, "CARRIER");
546 start_interface(ifp);
547 }
548 }
549 }
550
551 static void
552 warn_iaid_conflict(struct interface *ifp, uint8_t *iaid)
553 {
554 struct interface *ifn;
555 size_t i;
556
557 TAILQ_FOREACH(ifn, ifaces, next) {
558 if (ifn == ifp)
559 continue;
560 if (memcmp(ifn->options->iaid, iaid,
561 sizeof(ifn->options->iaid)) == 0)
562 break;
563 for (i = 0; i < ifn->options->ia_len; i++) {
564 if (memcmp(&ifn->options->ia[i].iaid, iaid,
565 sizeof(ifn->options->ia[i].iaid)) == 0)
566 break;
567 }
568 }
569
570 /* This is only a problem if the interfaces are on the same network. */
571 if (ifn)
572 syslog(LOG_ERR,
573 "%s: IAID conflicts with one assigned to %s",
574 ifp->name, ifn->name);
575 }
576
577 void
578 start_interface(void *arg)
579 {
580 struct interface *ifp = arg;
581 struct if_options *ifo = ifp->options;
582 int nolease;
583 size_t i;
584
585 handle_carrier(LINK_UNKNOWN, 0, ifp->name);
586 if (ifp->carrier == LINK_DOWN) {
587 syslog(LOG_INFO, "%s: waiting for carrier", ifp->name);
588 return;
589 }
590
591 if (ifo->options & (DHCPCD_DUID | DHCPCD_IPV6)) {
592 /* Report client DUID */
593 if (duid == NULL) {
594 if (duid_init(ifp) == 0)
595 return;
596 syslog(LOG_INFO, "DUID %s",
597 hwaddr_ntoa(duid, duid_len));
598 }
599
600 /* Report IAIDs */
601 syslog(LOG_INFO, "%s: IAID %s", ifp->name,
602 hwaddr_ntoa(ifo->iaid, sizeof(ifo->iaid)));
603 warn_iaid_conflict(ifp, ifo->iaid);
604 for (i = 0; i < ifo->ia_len; i++) {
605 if (memcmp(ifo->iaid, ifo->ia[i].iaid,
606 sizeof(ifo->iaid)))
607 {
608 syslog(LOG_INFO, "%s: IAID %s", ifp->name,
609 hwaddr_ntoa(ifo->ia[i].iaid,
610 sizeof(ifo->ia[i].iaid)));
611 warn_iaid_conflict(ifp, ifo->ia[i].iaid);
612 }
613 }
614 }
615
616 if (ifo->options & DHCPCD_IPV6) {
617 if (ifo->options & DHCPCD_IPV6RS &&
618 !(ifo->options & DHCPCD_INFORM))
619 ipv6nd_startrs(ifp);
620
621 if (!(ifo->options & DHCPCD_IPV6RS)) {
622 if (ifo->options & DHCPCD_IA_FORCED)
623 nolease = dhcp6_start(ifp, DH6S_INIT);
624 else {
625 nolease = dhcp6_find_delegates(ifp);
626 /* Enabling the below doesn't really make
627 * sense as there is currently no standard
628 * to push routes via DHCPv6.
629 * (There is an expired working draft,
630 * maybe abandoned?)
631 * You can also get it to work by forcing
632 * an IA as shown above. */
633 #if 0
634 /* With no RS or delegates we might
635 * as well try and solicit a DHCPv6 address */
636 if (nolease == 0)
637 nolease = dhcp6_start(ifp, DH6S_INIT);
638 #endif
639 }
640 if (nolease == -1)
641 syslog(LOG_ERR,
642 "%s: dhcp6_start: %m", ifp->name);
643 }
644 }
645
646 if (ifo->options & DHCPCD_IPV4)
647 dhcp_start(ifp);
648 }
649
650 /* ARGSUSED */
651 static void
652 handle_link(__unused void *arg)
653 {
654
655 if (manage_link(linkfd) == -1 && errno != ENXIO && errno != ENODEV)
656 syslog(LOG_ERR, "manage_link: %m");
657 }
658
659 static void
660 init_state(struct interface *ifp, int argc, char **argv)
661 {
662 struct if_options *ifo;
663 const char *reason = NULL;
664
665 configure_interface(ifp, argc, argv);
666 ifo = ifp->options;
667
668 if (ifo->options & DHCPCD_IPV4 && ipv4_init() == -1) {
669 syslog(LOG_ERR, "ipv4_init: %m");
670 ifo->options &= ~DHCPCD_IPV4;
671 }
672 if (ifo->options & DHCPCD_IPV6 && ipv6_init() == -1) {
673 syslog(LOG_ERR, "ipv6_init: %m");
674 ifo->options &= ~DHCPCD_IPV6RS;
675 }
676
677 if (!(options & DHCPCD_TEST))
678 script_runreason(ifp, "PREINIT");
679
680 if (ifo->options & DHCPCD_LINK) {
681 switch (carrier_status(ifp)) {
682 case LINK_DOWN:
683 ifp->carrier = LINK_DOWN;
684 reason = "NOCARRIER";
685 break;
686 case LINK_UP:
687 ifp->carrier = LINK_UP;
688 reason = "CARRIER";
689 break;
690 default:
691 ifp->carrier = LINK_UNKNOWN;
692 return;
693 }
694 if (reason && !(options & DHCPCD_TEST))
695 script_runreason(ifp, reason);
696 } else
697 ifp->carrier = LINK_UNKNOWN;
698 }
699
700 void
701 handle_interface(int action, const char *ifname)
702 {
703 struct if_head *ifs;
704 struct interface *ifp, *ifn, *ifl = NULL;
705 const char * const argv[] = { ifname };
706 int i;
707
708 if (action == -1) {
709 ifp = find_interface(ifname);
710 if (ifp != NULL) {
711 ifp->options->options |= DHCPCD_DEPARTED;
712 stop_interface(ifp);
713 }
714 return;
715 }
716
717 /* If running off an interface list, check it's in it. */
718 if (ifc) {
719 for (i = 0; i < ifc; i++)
720 if (strcmp(ifv[i], ifname) == 0)
721 break;
722 if (i >= ifc)
723 return;
724 }
725
726 ifs = discover_interfaces(-1, UNCONST(argv));
727 TAILQ_FOREACH_SAFE(ifp, ifs, next, ifn) {
728 if (strcmp(ifp->name, ifname) != 0)
729 continue;
730 /* Check if we already have the interface */
731 ifl = find_interface(ifp->name);
732 if (ifl) {
733 /* The flags and hwaddr could have changed */
734 ifl->flags = ifp->flags;
735 ifl->hwlen = ifp->hwlen;
736 if (ifp->hwlen != 0)
737 memcpy(ifl->hwaddr, ifp->hwaddr, ifl->hwlen);
738 } else {
739 TAILQ_REMOVE(ifs, ifp, next);
740 TAILQ_INSERT_TAIL(ifaces, ifp, next);
741 }
742 if (action == 1) {
743 init_state(ifp, margc, margv);
744 start_interface(ifp);
745 }
746 }
747
748 /* Free our discovered list */
749 while ((ifp = TAILQ_FIRST(ifs))) {
750 TAILQ_REMOVE(ifs, ifp, next);
751 free_interface(ifp);
752 }
753 free(ifs);
754 }
755
756 void
757 handle_hwaddr(const char *ifname, const uint8_t *hwaddr, size_t hwlen)
758 {
759 struct interface *ifp;
760
761 ifp = find_interface(ifname);
762 if (ifp == NULL)
763 return;
764
765 if (hwlen > sizeof(ifp->hwaddr)) {
766 errno = ENOBUFS;
767 syslog(LOG_ERR, "%s: %s: %m", ifp->name, __func__);
768 return;
769 }
770
771 if (ifp->hwlen == hwlen && memcmp(ifp->hwaddr, hwaddr, hwlen) == 0)
772 return;
773
774 syslog(LOG_INFO, "%s: new hardware address: %s", ifp->name,
775 hwaddr_ntoa(hwaddr, hwlen));
776 ifp->hwlen = hwlen;
777 memcpy(ifp->hwaddr, hwaddr, hwlen);
778 }
779
780 static void
781 if_reboot(struct interface *ifp, int argc, char **argv)
782 {
783 int oldopts;
784
785 oldopts = ifp->options->options;
786 script_runreason(ifp, "RECONFIGURE");
787 configure_interface(ifp, argc, argv);
788 dhcp_reboot_newopts(ifp, oldopts);
789 dhcp6_reboot(ifp);
790 start_interface(ifp);
791 }
792
793 static void
794 reconf_reboot(int action, int argc, char **argv, int oi)
795 {
796 struct if_head *ifs;
797 struct interface *ifn, *ifp;
798
799 ifs = discover_interfaces(argc - oi, argv + oi);
800 if (ifs == NULL)
801 return;
802
803 while ((ifp = TAILQ_FIRST(ifs))) {
804 TAILQ_REMOVE(ifs, ifp, next);
805 ifn = find_interface(ifp->name);
806 if (ifn) {
807 if (action)
808 if_reboot(ifn, argc, argv);
809 else
810 ipv4_applyaddr(ifn);
811 free_interface(ifp);
812 } else {
813 init_state(ifp, argc, argv);
814 TAILQ_INSERT_TAIL(ifaces, ifp, next);
815 start_interface(ifp);
816 }
817 }
818 free(ifs);
819
820 sort_interfaces();
821 }
822
823 /* ARGSUSED */
824 static void
825 sig_reboot(void *arg)
826 {
827 siginfo_t *siginfo = arg;
828 struct if_options *ifo;
829
830 syslog(LOG_INFO, "received SIGALRM from PID %d, rebinding",
831 (int)siginfo->si_pid);
832
833 free_globals();
834 ifav = NULL;
835 ifac = 0;
836 ifdc = 0;
837 ifdv = NULL;
838
839 ifo = read_config(cffile, NULL, NULL, NULL);
840 add_options(ifo, margc, margv);
841 /* We need to preserve these two options. */
842 if (options & DHCPCD_MASTER)
843 ifo->options |= DHCPCD_MASTER;
844 if (options & DHCPCD_DAEMONISED)
845 ifo->options |= DHCPCD_DAEMONISED;
846 options = ifo->options;
847 free_options(ifo);
848 reconf_reboot(1, ifc, ifv, 0);
849 }
850
851 static void
852 sig_reconf(void *arg)
853 {
854 siginfo_t *siginfo = arg;
855 struct interface *ifp;
856
857 syslog(LOG_INFO, "received SIGUSR from PID %d, reconfiguring",
858 (int)siginfo->si_pid);
859 TAILQ_FOREACH(ifp, ifaces, next) {
860 ipv4_applyaddr(ifp);
861 }
862 }
863
864 static void
865 handle_signal(int sig, siginfo_t *siginfo, __unused void *context)
866 {
867 struct interface *ifp;
868 int do_release;
869
870 do_release = 0;
871 switch (sig) {
872 case SIGINT:
873 syslog(LOG_INFO, "received SIGINT from PID %d, stopping",
874 (int)siginfo->si_pid);
875 break;
876 case SIGTERM:
877 syslog(LOG_INFO, "received SIGTERM from PID %d, stopping",
878 (int)siginfo->si_pid);
879 break;
880 case SIGALRM:
881 eloop_timeout_add_now(sig_reboot, siginfo);
882 return;
883 case SIGHUP:
884 syslog(LOG_INFO, "received SIGHUP from PID %d, releasing",
885 (int)siginfo->si_pid);
886 do_release = 1;
887 break;
888 case SIGUSR1:
889 eloop_timeout_add_now(sig_reconf, siginfo);
890 return;
891 case SIGPIPE:
892 syslog(LOG_WARNING, "received SIGPIPE");
893 return;
894 default:
895 syslog(LOG_ERR,
896 "received signal %d from PID %d, "
897 "but don't know what to do with it",
898 sig, (int)siginfo->si_pid);
899 return;
900 }
901
902 if (options & DHCPCD_TEST)
903 exit(EXIT_FAILURE);
904
905 /* As drop_dhcp could re-arrange the order, we do it like this. */
906 for (;;) {
907 /* Be sane and drop the last config first */
908 ifp = TAILQ_LAST(ifaces, if_head);
909 if (ifp == NULL)
910 break;
911 if (do_release) {
912 ifp->options->options |= DHCPCD_RELEASE;
913 ifp->options->options &= ~DHCPCD_PERSISTENT;
914 }
915 ifp->options->options |= DHCPCD_EXITING;
916 stop_interface(ifp);
917 }
918 exit(EXIT_FAILURE);
919 }
920
921 int
922 handle_args(struct fd_list *fd, int argc, char **argv)
923 {
924 struct interface *ifp;
925 int do_exit = 0, do_release = 0, do_reboot = 0;
926 int opt, oi = 0;
927 ssize_t len;
928 size_t l;
929 struct iovec iov[2];
930 char *tmp, *p;
931
932 if (fd != NULL) {
933 /* Special commands for our control socket */
934 if (strcmp(*argv, "--version") == 0) {
935 len = strlen(VERSION) + 1;
936 iov[0].iov_base = &len;
937 iov[0].iov_len = sizeof(ssize_t);
938 iov[1].iov_base = UNCONST(VERSION);
939 iov[1].iov_len = len;
940 if (writev(fd->fd, iov, 2) == -1) {
941 syslog(LOG_ERR, "writev: %m");
942 return -1;
943 }
944 return 0;
945 } else if (strcmp(*argv, "--getconfigfile") == 0) {
946 len = strlen(cffile ? cffile : CONFIG) + 1;
947 iov[0].iov_base = &len;
948 iov[0].iov_len = sizeof(ssize_t);
949 iov[1].iov_base = cffile ? cffile : UNCONST(CONFIG);
950 iov[1].iov_len = len;
951 if (writev(fd->fd, iov, 2) == -1) {
952 syslog(LOG_ERR, "writev: %m");
953 return -1;
954 }
955 return 0;
956 } else if (strcmp(*argv, "--getinterfaces") == 0) {
957 len = 0;
958 if (argc == 1) {
959 TAILQ_FOREACH(ifp, ifaces, next) {
960 len++;
961 if (D6_STATE_RUNNING(ifp))
962 len++;
963 if (ipv6nd_has_ra(ifp))
964 len++;
965 }
966 len = write(fd->fd, &len, sizeof(len));
967 if (len != sizeof(len))
968 return -1;
969 TAILQ_FOREACH(ifp, ifaces, next) {
970 send_interface(fd->fd, ifp);
971 }
972 return 0;
973 }
974 opt = 0;
975 while (argv[++opt] != NULL) {
976 TAILQ_FOREACH(ifp, ifaces, next) {
977 if (strcmp(argv[opt], ifp->name) == 0) {
978 len++;
979 if (D6_STATE_RUNNING(ifp))
980 len++;
981 if (ipv6nd_has_ra(ifp))
982 len++;
983 }
984 }
985 }
986 len = write(fd->fd, &len, sizeof(len));
987 if (len != sizeof(len))
988 return -1;
989 opt = 0;
990 while (argv[++opt] != NULL) {
991 TAILQ_FOREACH(ifp, ifaces, next) {
992 if (strcmp(argv[opt], ifp->name) == 0)
993 send_interface(fd->fd, ifp);
994 }
995 }
996 return 0;
997 } else if (strcmp(*argv, "--listen") == 0) {
998 fd->listener = 1;
999 return 0;
1000 }
1001 }
1002
1003 /* Log the command */
1004 len = 0;
1005 for (opt = 0; opt < argc; opt++)
1006 len += strlen(argv[opt]) + 1;
1007 tmp = p = malloc(len + 1);
1008 if (tmp == NULL) {
1009 syslog(LOG_ERR, "%s: %m", __func__);
1010 return -1;
1011 }
1012 for (opt = 0; opt < argc; opt++) {
1013 l = strlen(argv[opt]);
1014 strlcpy(p, argv[opt], l + 1);
1015 p += l;
1016 *p++ = ' ';
1017 }
1018 *--p = '\0';
1019 syslog(LOG_INFO, "control command: %s", tmp);
1020 free(tmp);
1021
1022 optind = 0;
1023 while ((opt = getopt_long(argc, argv, IF_OPTS, cf_options, &oi)) != -1)
1024 {
1025 switch (opt) {
1026 case 'g':
1027 /* Assumed if below not set */
1028 break;
1029 case 'k':
1030 do_release = 1;
1031 break;
1032 case 'n':
1033 do_reboot = 1;
1034 break;
1035 case 'x':
1036 do_exit = 1;
1037 break;
1038 }
1039 }
1040
1041 /* We need at least one interface */
1042 if (optind == argc) {
1043 syslog(LOG_ERR, "%s: no interface", __func__);
1044 return -1;
1045 }
1046
1047 if (do_release || do_exit) {
1048 for (oi = optind; oi < argc; oi++) {
1049 if ((ifp = find_interface(argv[oi])) == NULL)
1050 continue;
1051 if (do_release) {
1052 ifp->options->options |= DHCPCD_RELEASE;
1053 ifp->options->options &= ~DHCPCD_PERSISTENT;
1054 }
1055 ifp->options->options |= DHCPCD_EXITING;
1056 stop_interface(ifp);
1057 }
1058 return 0;
1059 }
1060
1061 reconf_reboot(do_reboot, argc, argv, optind);
1062 return 0;
1063 }
1064
1065 static int
1066 signal_init(void (*func)(int, siginfo_t *, void *), sigset_t *oldset)
1067 {
1068 unsigned int i;
1069 struct sigaction sa;
1070 sigset_t newset;
1071
1072 sigfillset(&newset);
1073 if (sigprocmask(SIG_SETMASK, &newset, oldset) == -1)
1074 return -1;
1075
1076 memset(&sa, 0, sizeof(sa));
1077 sa.sa_sigaction = func;
1078 sa.sa_flags = SA_SIGINFO;
1079 sigemptyset(&sa.sa_mask);
1080
1081 for (i = 0; handle_sigs[i]; i++) {
1082 if (sigaction(handle_sigs[i], &sa, NULL) == -1)
1083 return -1;
1084 }
1085 return 0;
1086 }
1087
1088 int
1089 main(int argc, char **argv)
1090 {
1091 struct interface *ifp;
1092 uint16_t family = 0;
1093 int opt, oi = 0, sig = 0, i, control_fd;
1094 size_t len;
1095 pid_t pid;
1096 struct timespec ts;
1097 struct utsname utn;
1098 const char *platform;
1099
1100 closefrom(3);
1101 openlog(PACKAGE, LOG_PERROR | LOG_PID, LOG_DAEMON);
1102 setlogmask(LOG_UPTO(LOG_INFO));
1103
1104 /* Test for --help and --version */
1105 if (argc > 1) {
1106 if (strcmp(argv[1], "--help") == 0) {
1107 usage();
1108 exit(EXIT_SUCCESS);
1109 } else if (strcmp(argv[1], "--version") == 0) {
1110 printf(""PACKAGE" "VERSION"\n%s\n", copyright);
1111 exit(EXIT_SUCCESS);
1112 }
1113 }
1114
1115 platform = hardware_platform();
1116 if (uname(&utn) == 0)
1117 snprintf(vendor, VENDORCLASSID_MAX_LEN,
1118 "%s-%s:%s-%s:%s%s%s", PACKAGE, VERSION,
1119 utn.sysname, utn.release, utn.machine,
1120 platform ? ":" : "", platform ? platform : "");
1121 else
1122 snprintf(vendor, VENDORCLASSID_MAX_LEN,
1123 "%s-%s", PACKAGE, VERSION);
1124
1125 i = 0;
1126 while ((opt = getopt_long(argc, argv, IF_OPTS, cf_options, &oi)) != -1)
1127 {
1128 switch (opt) {
1129 case '4':
1130 family = AF_INET;
1131 break;
1132 case '6':
1133 family = AF_INET6;
1134 break;
1135 case 'f':
1136 cffile = optarg;
1137 break;
1138 case 'g':
1139 sig = SIGUSR1;
1140 break;
1141 case 'k':
1142 sig = SIGHUP;
1143 break;
1144 case 'n':
1145 sig = SIGALRM;
1146 break;
1147 case 'x':
1148 sig = SIGTERM;
1149 break;
1150 case 'T':
1151 i = 1;
1152 break;
1153 case 'U':
1154 i = 2;
1155 break;
1156 case 'V':
1157 printf("Interface options:\n");
1158 if_printoptions();
1159 #ifdef INET
1160 if (family == 0 || family == AF_INET) {
1161 printf("\nDHCPv4 options:\n");
1162 dhcp_printoptions();
1163 }
1164 #endif
1165 #ifdef INET6
1166 if (family == 0 || family == AF_INET6) {
1167 printf("\nDHCPv6 options:\n");
1168 dhcp6_printoptions();
1169 }
1170 #endif
1171 exit(EXIT_SUCCESS);
1172 case '?':
1173 usage();
1174 exit(EXIT_FAILURE);
1175 }
1176 }
1177
1178 margv = argv;
1179 margc = argc;
1180 if_options = read_config(cffile, NULL, NULL, NULL);
1181 opt = add_options(if_options, argc, argv);
1182 if (opt != 1) {
1183 if (opt == 0)
1184 usage();
1185 exit(EXIT_FAILURE);
1186 }
1187 options = if_options->options;
1188 if (i != 0) {
1189 if (i == 1)
1190 options |= DHCPCD_TEST;
1191 else
1192 options |= DHCPCD_DUMPLEASE;
1193 options |= DHCPCD_PERSISTENT;
1194 options &= ~DHCPCD_DAEMONISE;
1195 }
1196
1197 #ifdef THERE_IS_NO_FORK
1198 options &= ~DHCPCD_DAEMONISE;
1199 #endif
1200
1201 if (options & DHCPCD_DEBUG)
1202 setlogmask(LOG_UPTO(LOG_DEBUG));
1203 if (options & DHCPCD_QUIET) {
1204 i = open(_PATH_DEVNULL, O_RDWR);
1205 if (i == -1)
1206 syslog(LOG_ERR, "%s: open: %m", __func__);
1207 else {
1208 dup2(i, STDERR_FILENO);
1209 close(i);
1210 }
1211 }
1212
1213 if (!(options & (DHCPCD_TEST | DHCPCD_DUMPLEASE))) {
1214 /* If we have any other args, we should run as a single dhcpcd
1215 * instance for that interface. */
1216 len = strlen(PIDFILE) + IF_NAMESIZE + 2;
1217 pidfile = malloc(len);
1218 if (pidfile == NULL) {
1219 syslog(LOG_ERR, "%s: %m", __func__);
1220 exit(EXIT_FAILURE);
1221 }
1222 if (optind == argc - 1)
1223 snprintf(pidfile, len, PIDFILE, "-", argv[optind]);
1224 else {
1225 snprintf(pidfile, len, PIDFILE, "", "");
1226 options |= DHCPCD_MASTER;
1227 }
1228 }
1229
1230 if (chdir("/") == -1)
1231 syslog(LOG_ERR, "chdir `/': %m");
1232 atexit(cleanup);
1233
1234 if (options & DHCPCD_DUMPLEASE) {
1235 if (optind != argc - 1) {
1236 syslog(LOG_ERR, "dumplease requires an interface");
1237 exit(EXIT_FAILURE);
1238 }
1239 if (dhcp_dump(argv[optind]) == -1)
1240 exit(EXIT_FAILURE);
1241 exit(EXIT_SUCCESS);
1242 }
1243
1244 if (!(options & (DHCPCD_MASTER | DHCPCD_TEST))) {
1245 control_fd = control_open();
1246 if (control_fd != -1) {
1247 syslog(LOG_INFO,
1248 "sending commands to master dhcpcd process");
1249 i = control_send(argc, argv);
1250 if (i > 0) {
1251 syslog(LOG_DEBUG, "send OK");
1252 exit(EXIT_SUCCESS);
1253 } else {
1254 syslog(LOG_ERR, "failed to send commands");
1255 exit(EXIT_FAILURE);
1256 }
1257 } else {
1258 if (errno != ENOENT)
1259 syslog(LOG_ERR, "control_open: %m");
1260 }
1261 }
1262
1263 if (geteuid())
1264 syslog(LOG_WARNING,
1265 PACKAGE " will not work correctly unless run as root");
1266
1267 if (sig != 0) {
1268 pid = read_pid();
1269 if (pid != 0)
1270 syslog(LOG_INFO, "sending signal %d to pid %d",
1271 sig, pid);
1272 if (pid == 0 || kill(pid, sig) != 0) {
1273 if (sig != SIGALRM && errno != EPERM)
1274 syslog(LOG_ERR, ""PACKAGE" not running");
1275 if (pid != 0 && errno != ESRCH) {
1276 syslog(LOG_ERR, "kill: %m");
1277 exit(EXIT_FAILURE);
1278 }
1279 unlink(pidfile);
1280 if (sig != SIGALRM)
1281 exit(EXIT_FAILURE);
1282 } else {
1283 if (sig == SIGALRM || sig == SIGUSR1)
1284 exit(EXIT_SUCCESS);
1285 /* Spin until it exits */
1286 syslog(LOG_INFO, "waiting for pid %d to exit", pid);
1287 ts.tv_sec = 0;
1288 ts.tv_nsec = 100000000; /* 10th of a second */
1289 for(i = 0; i < 100; i++) {
1290 nanosleep(&ts, NULL);
1291 if (read_pid() == 0)
1292 exit(EXIT_SUCCESS);
1293 }
1294 syslog(LOG_ERR, "pid %d failed to exit", pid);
1295 exit(EXIT_FAILURE);
1296 }
1297 }
1298
1299 if (!(options & DHCPCD_TEST)) {
1300 if ((pid = read_pid()) > 0 &&
1301 kill(pid, 0) == 0)
1302 {
1303 syslog(LOG_ERR, ""PACKAGE
1304 " already running on pid %d (%s)",
1305 pid, pidfile);
1306 exit(EXIT_FAILURE);
1307 }
1308
1309 /* Ensure we have the needed directories */
1310 if (mkdir(RUNDIR, 0755) == -1 && errno != EEXIST)
1311 syslog(LOG_ERR, "mkdir `%s': %m", RUNDIR);
1312 if (mkdir(DBDIR, 0755) == -1 && errno != EEXIST)
1313 syslog(LOG_ERR, "mkdir `%s': %m", DBDIR);
1314
1315 pidfd = open(pidfile, O_WRONLY | O_CREAT | O_NONBLOCK, 0664);
1316 if (pidfd == -1)
1317 syslog(LOG_ERR, "open `%s': %m", pidfile);
1318 else {
1319 /* Lock the file so that only one instance of dhcpcd
1320 * runs on an interface */
1321 if (flock(pidfd, LOCK_EX | LOCK_NB) == -1) {
1322 syslog(LOG_ERR, "flock `%s': %m", pidfile);
1323 exit(EXIT_FAILURE);
1324 }
1325 if (set_cloexec(pidfd) == -1)
1326 exit(EXIT_FAILURE);
1327 writepid(pidfd, getpid());
1328 }
1329 }
1330
1331 syslog(LOG_INFO, "version " VERSION " starting");
1332 options |= DHCPCD_STARTED;
1333
1334 #ifdef DEBUG_MEMORY
1335 eloop_init();
1336 #endif
1337
1338 /* Save signal mask, block and redirect signals to our handler */
1339 if (signal_init(handle_signal, &dhcpcd_sigset) == -1) {
1340 syslog(LOG_ERR, "signal_setup: %m");
1341 exit(EXIT_FAILURE);
1342 }
1343
1344 if (options & DHCPCD_MASTER) {
1345 if (control_start() == -1)
1346 syslog(LOG_ERR, "control_start: %m");
1347 }
1348
1349 if (open_sockets() == -1) {
1350 syslog(LOG_ERR, "open_sockets: %m");
1351 exit(EXIT_FAILURE);
1352 }
1353
1354 #if 0
1355 if (options & DHCPCD_IPV6RS && disable_rtadv() == -1) {
1356 syslog(LOG_ERR, "disable_rtadvd: %m");
1357 options &= ~DHCPCD_IPV6RS;
1358 }
1359 #endif
1360
1361 ifc = argc - optind;
1362 ifv = argv + optind;
1363
1364 /* When running dhcpcd against a single interface, we need to retain
1365 * the old behaviour of waiting for an IP address */
1366 if (ifc == 1 && !(options & DHCPCD_BACKGROUND))
1367 options |= DHCPCD_WAITIP;
1368
1369 /* RTM_NEWADDR goes through the link socket as well which we
1370 * need for IPv6 DAD, so we check for DHCPCD_LINK in handle_carrier
1371 * instead.
1372 * We also need to open this before checking for interfaces below
1373 * so that we pickup any new addresses during the discover phase. */
1374 if (linkfd == -1) {
1375 linkfd = open_link_socket();
1376 if (linkfd == -1)
1377 syslog(LOG_ERR, "open_link_socket: %m");
1378 else
1379 eloop_event_add(linkfd, handle_link, NULL);
1380 }
1381
1382 /* Start any dev listening plugin which may want to
1383 * change the interface name provided by the kernel */
1384 if ((options & (DHCPCD_MASTER | DHCPCD_DEV)) ==
1385 (DHCPCD_MASTER | DHCPCD_DEV))
1386 dev_start(dev_load);
1387
1388 ifaces = discover_interfaces(ifc, ifv);
1389 for (i = 0; i < ifc; i++) {
1390 if (find_interface(ifv[i]) == NULL)
1391 syslog(LOG_ERR, "%s: interface not found or invalid",
1392 ifv[i]);
1393 }
1394 if (ifaces == NULL || TAILQ_FIRST(ifaces) == NULL) {
1395 if (ifc == 0)
1396 syslog(LOG_ERR, "no valid interfaces found");
1397 else
1398 exit(EXIT_FAILURE);
1399 if (!(options & DHCPCD_LINK)) {
1400 syslog(LOG_ERR,
1401 "aborting as link detection is disabled");
1402 exit(EXIT_FAILURE);
1403 }
1404 }
1405
1406 if (options & DHCPCD_BACKGROUND)
1407 daemonise();
1408
1409 opt = 0;
1410 TAILQ_FOREACH(ifp, ifaces, next) {
1411 init_state(ifp, argc, argv);
1412 if (ifp->carrier != LINK_DOWN)
1413 opt = 1;
1414 }
1415
1416 if (!(options & DHCPCD_BACKGROUND)) {
1417 /* If we don't have a carrier, we may have to wait for a second
1418 * before one becomes available if we brought an interface up */
1419 if (opt == 0 &&
1420 options & DHCPCD_LINK &&
1421 options & DHCPCD_WAITUP &&
1422 !(options & DHCPCD_WAITIP))
1423 {
1424 ts.tv_sec = 1;
1425 ts.tv_nsec = 0;
1426 nanosleep(&ts, NULL);
1427 TAILQ_FOREACH(ifp, ifaces, next) {
1428 handle_carrier(LINK_UNKNOWN, 0, ifp->name);
1429 if (ifp->carrier != LINK_DOWN) {
1430 opt = 1;
1431 break;
1432 }
1433 }
1434 }
1435 if (options & DHCPCD_MASTER)
1436 i = if_options->timeout;
1437 else if ((ifp = TAILQ_FIRST(ifaces)))
1438 i = ifp->options->timeout;
1439 else
1440 i = 0;
1441 if (opt == 0 &&
1442 options & DHCPCD_LINK &&
1443 !(options & DHCPCD_WAITIP))
1444 {
1445 syslog(LOG_WARNING, "no interfaces have a carrier");
1446 daemonise();
1447 } else if (i > 0) {
1448 if (options & DHCPCD_IPV4LL)
1449 options |= DHCPCD_TIMEOUT_IPV4LL;
1450 eloop_timeout_add_sec(i, handle_exit_timeout, NULL);
1451 }
1452 }
1453 free_options(if_options);
1454 if_options = NULL;
1455
1456 sort_interfaces();
1457 TAILQ_FOREACH(ifp, ifaces, next) {
1458 eloop_timeout_add_sec(0, start_interface, ifp);
1459 }
1460
1461 eloop_start(&dhcpcd_sigset);
1462 exit(EXIT_SUCCESS);
1463 }