2 * dhcpcd - DHCP client daemon
3 * Copyright (c) 2006-2018 Roy Marples <roy@marples.name>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/param.h>
29 #include <sys/types.h>
30 #include <sys/ioctl.h>
31 #include <sys/socket.h>
36 #include <net/if_arp.h>
37 #include <netinet/in.h>
39 # include <net/if_dl.h>
40 # include <net/if_types.h>
41 # include <netinet/in_var.h>
42 # undef AF_PACKET /* Newer Illumos defines this */
45 # include <netpacket/packet.h>
48 # include <net/if_media.h>
50 #include <net/route.h>
69 #include "if-options.h"
76 if_free(struct interface *ifp)
88 free_options(ifp->ctx, ifp->options);
93 if_opensockets(struct dhcpcd_ctx *ctx)
96 if (if_opensockets_os(ctx) == -1)
99 /* We use this socket for some operations without INET. */
100 ctx->pf_inet_fd = xsocket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
101 if (ctx->pf_inet_fd == -1)
105 ctx->pf_link_fd = xsocket(PF_LINK, SOCK_DGRAM | SOCK_CLOEXEC, 0);
106 if (ctx->pf_link_fd == -1)
114 if_closesockets(struct dhcpcd_ctx *ctx)
117 if (ctx->pf_inet_fd != -1)
118 close(ctx->pf_inet_fd);
120 if (ctx->pf_link_fd != -1)
121 close(ctx->pf_link_fd);
125 if_closesockets_os(ctx);
131 if_carrier(struct interface *ifp)
136 struct ifmediareq ifmr;
139 memset(&ifr, 0, sizeof(ifr));
140 strlcpy(ifr.ifr_name, ifp->name, sizeof(ifr.ifr_name));
141 if (ioctl(ifp->ctx->pf_inet_fd, SIOCGIFFLAGS, &ifr) == -1)
143 ifp->flags = (unsigned int)ifr.ifr_flags;
146 memset(&ifmr, 0, sizeof(ifmr));
147 strlcpy(ifmr.ifm_name, ifp->name, sizeof(ifmr.ifm_name));
148 if (ioctl(ifp->ctx->pf_inet_fd, SIOCGIFMEDIA, &ifmr) != -1 &&
149 ifmr.ifm_status & IFM_AVALID)
150 r = (ifmr.ifm_status & IFM_ACTIVE) ? LINK_UP : LINK_DOWN;
152 r = ifr.ifr_flags & IFF_RUNNING ? LINK_UP : LINK_UNKNOWN;
154 r = ifr.ifr_flags & IFF_RUNNING ? LINK_UP : LINK_DOWN;
160 if_setflag(struct interface *ifp, short flag)
165 memset(&ifr, 0, sizeof(ifr));
166 strlcpy(ifr.ifr_name, ifp->name, sizeof(ifr.ifr_name));
168 if (ioctl(ifp->ctx->pf_inet_fd, SIOCGIFFLAGS, &ifr) == 0) {
169 if (flag == 0 || (ifr.ifr_flags & flag) == flag)
172 ifr.ifr_flags |= flag;
173 if (ioctl(ifp->ctx->pf_inet_fd, SIOCSIFFLAGS, &ifr) ==0)
176 ifp->flags = (unsigned int)ifr.ifr_flags;
182 if_hasconf(struct dhcpcd_ctx *ctx, const char *ifname)
186 for (i = 0; i < ctx->ifcc; i++) {
187 if (strcmp(ctx->ifcv[i], ifname) == 0)
194 if_markaddrsstale(struct if_head *ifs)
196 struct interface *ifp;
198 TAILQ_FOREACH(ifp, ifs, next) {
200 ipv4_markaddrsstale(ifp);
203 ipv6_markaddrsstale(ifp, 0);
209 if_learnaddrs(struct dhcpcd_ctx *ctx, struct if_head *ifs,
210 struct ifaddrs **ifaddrs)
213 struct interface *ifp;
215 const struct sockaddr_in *addr, *net, *brd;
218 struct sockaddr_in6 *sin6, *net6;
222 for (ifa = *ifaddrs; ifa; ifa = ifa->ifa_next) {
223 if (ifa->ifa_addr == NULL)
225 if ((ifp = if_find(ifs, ifa->ifa_name)) == NULL)
227 #ifdef HAVE_IFADDRS_ADDRFLAGS
228 addrflags = (int)ifa->ifa_addrflags;
230 switch(ifa->ifa_addr->sa_family) {
233 addr = (void *)ifa->ifa_addr;
234 net = (void *)ifa->ifa_netmask;
235 if (ifa->ifa_flags & IFF_POINTOPOINT)
236 brd = (void *)ifa->ifa_dstaddr;
238 brd = (void *)ifa->ifa_broadaddr;
239 #ifndef HAVE_IFADDRS_ADDRFLAGS
240 addrflags = if_addrflags(ifp, &addr->sin_addr,
242 if (addrflags == -1) {
243 if (errno != EEXIST && errno != EADDRNOTAVAIL)
244 logerr("%s: if_addrflags", __func__);
248 ipv4_handleifa(ctx, RTM_NEWADDR, ifs, ifa->ifa_name,
249 &addr->sin_addr, &net->sin_addr,
250 brd ? &brd->sin_addr : NULL, addrflags, 0);
255 sin6 = (void *)ifa->ifa_addr;
256 net6 = (void *)ifa->ifa_netmask;
258 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
259 /* Remove the scope from the address */
260 sin6->sin6_addr.s6_addr[2] =
261 sin6->sin6_addr.s6_addr[3] = '\0';
263 #ifndef HAVE_IFADDRS_ADDRFLAGS
264 addrflags = if_addrflags6(ifp, &sin6->sin6_addr,
266 if (addrflags == -1) {
267 if (errno != EEXIST && errno != EADDRNOTAVAIL)
268 logerr("%s: if_addrflags6", __func__);
272 ipv6_handleifa(ctx, RTM_NEWADDR, ifs,
273 ifa->ifa_name, &sin6->sin6_addr,
274 ipv6_prefixlen(&net6->sin6_addr), addrflags, 0);
280 freeifaddrs(*ifaddrs);
285 if_deletestaleaddrs(struct if_head *ifs)
287 struct interface *ifp;
289 TAILQ_FOREACH(ifp, ifs, next) {
291 ipv4_deletestaleaddrs(ifp);
294 ipv6_deletestaleaddrs(ifp);
300 if_valid_hwaddr(const uint8_t *hwaddr, size_t hwlen)
303 bool all_zeros, all_ones;
305 all_zeros = all_ones = true;
306 for (i = 0; i < hwlen; i++) {
307 if (hwaddr[i] != 0x00)
309 if (hwaddr[i] != 0xff)
311 if (!all_zeros && !all_ones)
318 if_discover(struct dhcpcd_ctx *ctx, struct ifaddrs **ifaddrs,
319 int argc, char * const *argv)
325 struct interface *ifp;
328 const struct sockaddr_dl *sdl;
329 #ifdef SIOCGIFPRIORITY
333 struct if_laddrreq iflr;
337 memset(&iflr, 0, sizeof(iflr));
340 const struct sockaddr_ll *sll;
343 if ((ifs = malloc(sizeof(*ifs))) == NULL) {
348 if (getifaddrs(ifaddrs) == -1) {
353 for (ifa = *ifaddrs; ifa; ifa = ifa->ifa_next) {
354 if (ifa->ifa_addr != NULL) {
356 if (ifa->ifa_addr->sa_family != AF_LINK)
359 if (ifa->ifa_addr->sa_family != AF_PACKET)
363 if (if_nametospec(ifa->ifa_name, &spec) != 0)
366 /* It's possible for an interface to have >1 AF_LINK.
367 * For our purposes, we use the first one. */
368 TAILQ_FOREACH(ifp, ifs, next) {
369 if (strcmp(ifp->name, spec.devname) == 0)
376 for (i = 0; i < argc; i++) {
377 if (strcmp(argv[i], spec.devname) == 0)
380 active = (i == argc) ? IF_INACTIVE : IF_ACTIVE_USER;
382 /* -1 means we're discovering against a specific
383 * interface, but we still need the below rules
385 if (argc == -1 && strcmp(argv[0], spec.devname) != 0)
387 active = ctx->options & DHCPCD_INACTIVE ?
388 IF_INACTIVE: IF_ACTIVE_USER;
391 for (i = 0; i < ctx->ifdc; i++)
392 if (fnmatch(ctx->ifdv[i], spec.devname, 0) == 0)
395 active = IF_INACTIVE;
396 for (i = 0; i < ctx->ifc; i++)
397 if (fnmatch(ctx->ifv[i], spec.devname, 0) == 0)
399 if (ctx->ifc && i == ctx->ifc)
400 active = IF_INACTIVE;
401 for (i = 0; i < ctx->ifac; i++)
402 if (fnmatch(ctx->ifav[i], spec.devname, 0) == 0)
404 if (ctx->ifac && i == ctx->ifac)
405 active = IF_INACTIVE;
408 /* Ensure that the interface name has settled */
409 if (!dev_initialized(ctx, spec.devname))
413 /* Don't allow loopback or pointopoint unless explicit */
414 if (ifa->ifa_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) {
415 if ((argc == 0 || argc == -1) &&
416 ctx->ifac == 0 && !if_hasconf(ctx, spec.devname))
417 active = IF_INACTIVE;
420 if (if_vimaster(ctx, spec.devname) == 1) {
421 logfunc_t *logfunc = argc != 0 ? logerrx : logdebugx;
422 logfunc("%s: is a Virtual Interface Master, skipping",
427 ifp = calloc(1, sizeof(*ifp));
433 strlcpy(ifp->name, spec.devname, sizeof(ifp->name));
434 ifp->flags = ifa->ifa_flags;
436 if (ifa->ifa_addr != NULL) {
438 sdl = (const void *)ifa->ifa_addr;
441 /* We need to check for active address */
442 strlcpy(iflr.iflr_name, ifp->name,
443 sizeof(iflr.iflr_name));
444 memcpy(&iflr.addr, ifa->ifa_addr,
445 MIN(ifa->ifa_addr->sa_len, sizeof(iflr.addr)));
446 iflr.flags = IFLR_PREFIX;
447 iflr.prefixlen = (unsigned int)sdl->sdl_alen * NBBY;
448 if (ioctl(ctx->pf_link_fd, SIOCGLIFADDR, &iflr) == -1 ||
449 !(iflr.flags & IFLR_ACTIVE))
456 ifp->index = sdl->sdl_index;
457 switch(sdl->sdl_type) {
459 case IFT_BRIDGE: /* FALLTHROUGH */
462 case IFT_PPP: /* FALLTHROUGH */
464 #ifdef IFT_PROPVIRTUAL
465 case IFT_PROPVIRTUAL: /* FALLTHROUGH */
467 #if defined(IFT_BRIDGE) || defined(IFT_PPP) || defined(IFT_PROPVIRTUAL)
468 /* Don't allow unless explicit */
469 if ((argc == 0 || argc == -1) &&
470 ctx->ifac == 0 && active &&
471 !if_hasconf(ctx, ifp->name))
473 logdebugx("%s: ignoring due to"
474 " interface type and"
477 active = IF_INACTIVE;
483 case IFT_L2VLAN: /* FALLTHROUGH */
486 case IFT_L3IPVLAN: /* FALLTHROUGH */
489 ifp->family = ARPHRD_ETHER;
493 ifp->family = ARPHRD_IEEE1394;
496 #ifdef IFT_INFINIBAND
498 ifp->family = ARPHRD_INFINIBAND;
502 /* Don't allow unless explicit */
503 if ((argc == 0 || argc == -1) &&
505 !if_hasconf(ctx, ifp->name))
506 active = IF_INACTIVE;
508 logwarnx("%s: unsupported"
509 " interface type %.2x",
510 ifp->name, sdl->sdl_type);
511 /* Pretend it's ethernet */
512 ifp->family = ARPHRD_ETHER;
515 ifp->hwlen = sdl->sdl_alen;
516 memcpy(ifp->hwaddr, CLLADDR(sdl), ifp->hwlen);
518 sll = (const void *)ifa->ifa_addr;
519 ifp->index = (unsigned int)sll->sll_ifindex;
520 ifp->family = sll->sll_hatype;
521 ifp->hwlen = sll->sll_halen;
523 memcpy(ifp->hwaddr, sll->sll_addr, ifp->hwlen);
527 /* PPP addresses on Linux don't have hardware addresses */
529 ifp->index = if_nametoindex(ifp->name);
532 /* Ensure hardware address is valid. */
533 if (!if_valid_hwaddr(ifp->hwaddr, ifp->hwlen))
536 /* We only work on ethernet by default */
537 if (ifp->family != ARPHRD_ETHER) {
538 if ((argc == 0 || argc == -1) &&
539 ctx->ifac == 0 && !if_hasconf(ctx, ifp->name))
540 active = IF_INACTIVE;
541 switch (ifp->family) {
542 case ARPHRD_IEEE1394:
543 case ARPHRD_INFINIBAND:
544 #ifdef ARPHRD_LOOPBACK
545 case ARPHRD_LOOPBACK:
550 /* We don't warn for supported families */
553 /* IFT already checked */
557 logwarnx("%s: unsupported"
558 " interface family %.2x",
559 ifp->name, ifp->family);
565 if (!(ctx->options & (DHCPCD_DUMPLEASE | DHCPCD_TEST))) {
566 /* Handle any platform init for the interface */
567 if (active != IF_INACTIVE && if_init(ifp) == -1) {
568 logerr("%s: if_init", ifp->name);
574 ifp->vlanid = if_vlanid(ifp);
576 #ifdef SIOCGIFPRIORITY
577 /* Respect the interface priority */
578 memset(&ifr, 0, sizeof(ifr));
579 strlcpy(ifr.ifr_name, ifp->name, sizeof(ifr.ifr_name));
580 if (ioctl(ctx->pf_inet_fd, SIOCGIFPRIORITY, &ifr) == 0)
581 ifp->metric = (unsigned int)ifr.ifr_metric;
584 /* We reserve the 100 range for virtual interfaces, if and when
585 * we can work them out. */
586 ifp->metric = 200 + ifp->index;
587 if (if_getssid(ifp) != -1) {
593 ifp->active = active;
595 ifp->carrier = if_carrier(ifp);
597 ifp->carrier = LINK_UNKNOWN;
598 TAILQ_INSERT_TAIL(ifs, ifp, next);
605 /* Decode bge0:1 as dev = bge, ppa = 0 and lun = 1 */
607 if_nametospec(const char *ifname, struct if_spec *spec)
612 if (ifname == NULL || *ifname == '\0' ||
613 strlcpy(spec->ifname, ifname, sizeof(spec->ifname)) >=
614 sizeof(spec->ifname) ||
615 strlcpy(spec->drvname, ifname, sizeof(spec->drvname)) >=
616 sizeof(spec->drvname))
621 ep = strchr(spec->drvname, ':');
623 spec->lun = (int)strtoi(ep + 1, NULL, 10, 0, INT_MAX, &e);
631 ep = spec->drvname + strlen(spec->drvname) - 1;
633 strlcpy(spec->devname, spec->drvname, sizeof(spec->devname));
634 while (ep > spec->drvname && isdigit((int)*ep))
640 spec->ppa = (int)strtoi(ep, NULL, 10, 0, INT_MAX, &e);
648 static struct interface *
649 if_findindexname(struct if_head *ifaces, unsigned int idx, const char *name)
652 if (ifaces != NULL) {
654 struct interface *ifp;
656 if (name && if_nametospec(name, &spec) == -1)
659 TAILQ_FOREACH(ifp, ifaces, next) {
660 if ((name && strcmp(ifp->name, spec.devname) == 0) ||
661 (!name && ifp->index == idx))
671 if_find(struct if_head *ifaces, const char *name)
674 return if_findindexname(ifaces, 0, name);
678 if_findindex(struct if_head *ifaces, unsigned int idx)
681 return if_findindexname(ifaces, idx, NULL);
685 if_loopback(struct dhcpcd_ctx *ctx)
687 struct interface *ifp;
689 TAILQ_FOREACH(ifp, ctx->ifaces, next) {
690 if (ifp->flags & IFF_LOOPBACK)
697 if_domtu(const struct interface *ifp, short int mtu)
702 memset(&ifr, 0, sizeof(ifr));
703 strlcpy(ifr.ifr_name, ifp->name, sizeof(ifr.ifr_name));
705 r = ioctl(ifp->ctx->pf_inet_fd, mtu ? SIOCSIFMTU : SIOCGIFMTU, &ifr);
711 /* Interface comparer for working out ordering. */
713 if_cmp(const struct interface *si, const struct interface *ti)
719 /* Check active first */
720 if (si->active > ti->active)
722 if (si->active < ti->active)
725 /* Check carrier status next */
726 if (si->carrier > ti->carrier)
728 if (si->carrier < ti->carrier)
731 if (D_STATE_RUNNING(si) && !D_STATE_RUNNING(ti))
733 if (!D_STATE_RUNNING(si) && D_STATE_RUNNING(ti))
735 if (RS_STATE_RUNNING(si) && !RS_STATE_RUNNING(ti))
737 if (!RS_STATE_RUNNING(si) && RS_STATE_RUNNING(ti))
739 if (D6_STATE_RUNNING(si) && !D6_STATE_RUNNING(ti))
741 if (!D6_STATE_RUNNING(si) && D6_STATE_RUNNING(ti))
745 /* Special attention needed here due to states and IPv4LL. */
746 if ((r = ipv4_ifcmp(si, ti)) != 0)
750 /* Finally, metric */
751 if (si->metric < ti->metric)
753 if (si->metric > ti->metric)
758 /* Sort the interfaces into a preferred order - best first, worst last. */
760 if_sortinterfaces(struct dhcpcd_ctx *ctx)
762 struct if_head sorted;
763 struct interface *ifp, *ift;
765 if (ctx->ifaces == NULL ||
766 (ifp = TAILQ_FIRST(ctx->ifaces)) == NULL ||
767 TAILQ_NEXT(ifp, next) == NULL)
771 TAILQ_REMOVE(ctx->ifaces, ifp, next);
772 TAILQ_INSERT_HEAD(&sorted, ifp, next);
773 while ((ifp = TAILQ_FIRST(ctx->ifaces))) {
774 TAILQ_REMOVE(ctx->ifaces, ifp, next);
775 TAILQ_FOREACH(ift, &sorted, next) {
776 if (if_cmp(ifp, ift) == -1) {
777 TAILQ_INSERT_BEFORE(ift, ifp, next);
782 TAILQ_INSERT_TAIL(&sorted, ifp, next);
784 TAILQ_CONCAT(ctx->ifaces, &sorted, next);
788 xsocket(int domain, int type, int protocol)
791 #if !defined(HAVE_SOCK_CLOEXEC) || !defined(HAVE_SOCK_NONBLOCK)
792 int xflags, xtype = type;
795 #ifndef HAVE_SOCK_CLOEXEC
796 if (xtype & SOCK_CLOEXEC)
797 type &= ~SOCK_CLOEXEC;
799 #ifndef HAVE_SOCK_NONBLOCK
800 if (xtype & SOCK_NONBLOCK)
801 type &= ~SOCK_NONBLOCK;
804 if ((s = socket(domain, type, protocol)) == -1)
807 #ifndef HAVE_SOCK_CLOEXEC
808 if ((xtype & SOCK_CLOEXEC) && ((xflags = fcntl(s, F_GETFD)) == -1 ||
809 fcntl(s, F_SETFD, xflags | FD_CLOEXEC) == -1))
812 #ifndef HAVE_SOCK_NONBLOCK
813 if ((xtype & SOCK_NONBLOCK) && ((xflags = fcntl(s, F_GETFL)) == -1 ||
814 fcntl(s, F_SETFL, xflags | O_NONBLOCK) == -1))
820 #if !defined(HAVE_SOCK_CLOEXEC) || !defined(HAVE_SOCK_NONBLOCK)