1 /* SPDX-License-Identifier: BSD-2-Clause */
3 * dhcpcd - DHCP client daemon
4 * Copyright (c) 2006-2019 Roy Marples <roy@marples.name>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/param.h>
30 #include <sys/types.h>
31 #include <sys/ioctl.h>
32 #include <sys/socket.h>
37 #include <net/if_arp.h>
38 #include <netinet/in.h>
40 # include <net/if_dl.h>
41 # include <net/if_types.h>
42 # include <netinet/in_var.h>
43 # undef AF_PACKET /* Newer Illumos defines this */
46 # include <netpacket/packet.h>
49 # include <net/if_media.h>
51 #include <net/route.h>
69 #include "if-options.h"
76 /* It has the ioctl, but the member is missing from the struct?
77 * No matter, our getifaddrs foo in if-sun.c will DTRT. */
82 if_free(struct interface *ifp)
102 free_options(ifp->ctx, ifp->options);
107 if_opensockets(struct dhcpcd_ctx *ctx)
110 if (if_opensockets_os(ctx) == -1)
113 /* We use this socket for some operations without INET. */
114 ctx->pf_inet_fd = xsocket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
115 if (ctx->pf_inet_fd == -1)
122 if_closesockets(struct dhcpcd_ctx *ctx)
125 if (ctx->pf_inet_fd != -1)
126 close(ctx->pf_inet_fd);
129 if_closesockets_os(ctx);
135 if_getflags(struct interface *ifp)
137 struct ifreq ifr = { .ifr_flags = 0 };
139 strlcpy(ifr.ifr_name, ifp->name, sizeof(ifr.ifr_name));
140 if (ioctl(ifp->ctx->pf_inet_fd, SIOCGIFFLAGS, &ifr) == -1)
142 ifp->flags = (unsigned int)ifr.ifr_flags;
147 if_setflag(struct interface *ifp, short flag)
149 struct ifreq ifr = { .ifr_flags = 0 };
152 if (if_getflags(ifp) == -1)
155 f = (short)ifp->flags;
156 if ((f & flag) == flag)
159 strlcpy(ifr.ifr_name, ifp->name, sizeof(ifr.ifr_name));
160 ifr.ifr_flags = f | flag;
161 if (ioctl(ifp->ctx->pf_inet_fd, SIOCSIFFLAGS, &ifr) == -1)
164 ifp->flags = (unsigned int)ifr.ifr_flags;
169 if_hasconf(struct dhcpcd_ctx *ctx, const char *ifname)
173 for (i = 0; i < ctx->ifcc; i++) {
174 if (strcmp(ctx->ifcv[i], ifname) == 0)
181 if_markaddrsstale(struct if_head *ifs)
183 struct interface *ifp;
185 TAILQ_FOREACH(ifp, ifs, next) {
187 ipv4_markaddrsstale(ifp);
190 ipv6_markaddrsstale(ifp, 0);
196 if_learnaddrs(struct dhcpcd_ctx *ctx, struct if_head *ifs,
197 struct ifaddrs **ifaddrs)
200 struct interface *ifp;
202 const struct sockaddr_in *addr, *net, *brd;
205 struct sockaddr_in6 *sin6, *net6;
209 for (ifa = *ifaddrs; ifa; ifa = ifa->ifa_next) {
210 if (ifa->ifa_addr == NULL)
212 if ((ifp = if_find(ifs, ifa->ifa_name)) == NULL)
214 #ifdef HAVE_IFADDRS_ADDRFLAGS
215 addrflags = (int)ifa->ifa_addrflags;
217 switch(ifa->ifa_addr->sa_family) {
220 addr = (void *)ifa->ifa_addr;
221 net = (void *)ifa->ifa_netmask;
222 if (ifa->ifa_flags & IFF_POINTOPOINT)
223 brd = (void *)ifa->ifa_dstaddr;
225 brd = (void *)ifa->ifa_broadaddr;
226 #ifndef HAVE_IFADDRS_ADDRFLAGS
227 addrflags = if_addrflags(ifp, &addr->sin_addr,
229 if (addrflags == -1) {
230 if (errno != EEXIST && errno != EADDRNOTAVAIL)
231 logerr("%s: if_addrflags", __func__);
235 ipv4_handleifa(ctx, RTM_NEWADDR, ifs, ifa->ifa_name,
236 &addr->sin_addr, &net->sin_addr,
237 brd ? &brd->sin_addr : NULL, addrflags, 0);
242 sin6 = (void *)ifa->ifa_addr;
243 net6 = (void *)ifa->ifa_netmask;
246 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
247 /* Remove the scope from the address */
248 sin6->sin6_addr.s6_addr[2] =
249 sin6->sin6_addr.s6_addr[3] = '\0';
251 #ifndef HAVE_IFADDRS_ADDRFLAGS
252 addrflags = if_addrflags6(ifp, &sin6->sin6_addr,
254 if (addrflags == -1) {
255 if (errno != EEXIST && errno != EADDRNOTAVAIL)
256 logerr("%s: if_addrflags6", __func__);
260 ipv6_handleifa(ctx, RTM_NEWADDR, ifs,
261 ifa->ifa_name, &sin6->sin6_addr,
262 ipv6_prefixlen(&net6->sin6_addr), addrflags, 0);
268 freeifaddrs(*ifaddrs);
273 if_deletestaleaddrs(struct if_head *ifs)
275 struct interface *ifp;
277 TAILQ_FOREACH(ifp, ifs, next) {
279 ipv4_deletestaleaddrs(ifp);
282 ipv6_deletestaleaddrs(ifp);
288 if_valid_hwaddr(const uint8_t *hwaddr, size_t hwlen)
291 bool all_zeros, all_ones;
293 all_zeros = all_ones = true;
294 for (i = 0; i < hwlen; i++) {
295 if (hwaddr[i] != 0x00)
297 if (hwaddr[i] != 0xff)
299 if (!all_zeros && !all_ones)
306 if_discover(struct dhcpcd_ctx *ctx, struct ifaddrs **ifaddrs,
307 int argc, char * const *argv)
313 struct interface *ifp;
316 const struct sockaddr_dl *sdl;
318 struct if_laddrreq iflr = { .flags = IFLR_PREFIX };
322 const struct sockaddr_ll *sll;
324 #if defined(SIOCGIFPRIORITY) || defined(SIOCGIFHWADDR)
328 if ((ifs = malloc(sizeof(*ifs))) == NULL) {
332 if (getifaddrs(ifaddrs) == -1) {
340 link_fd = xsocket(PF_LINK, SOCK_DGRAM | SOCK_CLOEXEC, 0);
348 for (ifa = *ifaddrs; ifa; ifa = ifa->ifa_next) {
349 if (ifa->ifa_addr != NULL) {
351 if (ifa->ifa_addr->sa_family != AF_LINK)
354 if (ifa->ifa_addr->sa_family != AF_PACKET)
358 if (if_nametospec(ifa->ifa_name, &spec) != 0)
361 /* It's possible for an interface to have >1 AF_LINK.
362 * For our purposes, we use the first one. */
363 TAILQ_FOREACH(ifp, ifs, next) {
364 if (strcmp(ifp->name, spec.devname) == 0)
371 for (i = 0; i < argc; i++) {
372 if (strcmp(argv[i], spec.devname) == 0)
375 active = (i == argc) ? IF_INACTIVE : IF_ACTIVE_USER;
377 /* -1 means we're discovering against a specific
378 * interface, but we still need the below rules
380 if (argc == -1 && strcmp(argv[0], spec.devname) != 0)
382 active = ctx->options & DHCPCD_INACTIVE ?
383 IF_INACTIVE: IF_ACTIVE_USER;
386 for (i = 0; i < ctx->ifdc; i++)
387 if (fnmatch(ctx->ifdv[i], spec.devname, 0) == 0)
390 active = IF_INACTIVE;
391 for (i = 0; i < ctx->ifc; i++)
392 if (fnmatch(ctx->ifv[i], spec.devname, 0) == 0)
394 if (ctx->ifc && i == ctx->ifc)
395 active = IF_INACTIVE;
396 for (i = 0; i < ctx->ifac; i++)
397 if (fnmatch(ctx->ifav[i], spec.devname, 0) == 0)
399 if (ctx->ifac && i == ctx->ifac)
400 active = IF_INACTIVE;
403 /* Ensure that the interface name has settled */
404 if (!dev_initialized(ctx, spec.devname))
408 /* Don't allow loopback or pointopoint unless explicit */
409 if (ifa->ifa_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) {
410 if ((argc == 0 || argc == -1) &&
411 ctx->ifac == 0 && !if_hasconf(ctx, spec.devname))
412 active = IF_INACTIVE;
415 if (if_vimaster(ctx, spec.devname) == 1) {
416 logfunc_t *logfunc = argc != 0 ? logerrx : logdebugx;
417 logfunc("%s: is a Virtual Interface Master, skipping",
422 ifp = calloc(1, sizeof(*ifp));
428 strlcpy(ifp->name, spec.devname, sizeof(ifp->name));
429 ifp->flags = ifa->ifa_flags;
431 if (ifa->ifa_addr != NULL) {
433 sdl = (const void *)ifa->ifa_addr;
436 /* We need to check for active address */
437 strlcpy(iflr.iflr_name, ifp->name,
438 sizeof(iflr.iflr_name));
439 memcpy(&iflr.addr, ifa->ifa_addr,
440 MIN(ifa->ifa_addr->sa_len, sizeof(iflr.addr)));
441 iflr.flags = IFLR_PREFIX;
442 iflr.prefixlen = (unsigned int)sdl->sdl_alen * NBBY;
443 if (ioctl(link_fd, SIOCGLIFADDR, &iflr) == -1 ||
444 !(iflr.flags & IFLR_ACTIVE))
451 ifp->index = sdl->sdl_index;
452 switch(sdl->sdl_type) {
454 case IFT_BRIDGE: /* FALLTHROUGH */
457 case IFT_PPP: /* FALLTHROUGH */
459 #ifdef IFT_PROPVIRTUAL
460 case IFT_PROPVIRTUAL:
462 #if defined(IFT_BRIDGE) || defined(IFT_PPP) || defined(IFT_PROPVIRTUAL)
463 /* Don't allow unless explicit */
464 if ((argc == 0 || argc == -1) &&
465 ctx->ifac == 0 && active &&
466 !if_hasconf(ctx, ifp->name))
468 logdebugx("%s: ignoring due to"
469 " interface type and"
472 active = IF_INACTIVE;
474 __fallthrough; /* Appease gcc-7 */
479 case IFT_L2VLAN: /* FALLTHROUGH */
482 case IFT_L3IPVLAN: /* FALLTHROUGH */
485 ifp->family = ARPHRD_ETHER;
489 ifp->family = ARPHRD_IEEE1394;
492 #ifdef IFT_INFINIBAND
494 ifp->family = ARPHRD_INFINIBAND;
498 /* Don't allow unless explicit */
499 if ((argc == 0 || argc == -1) &&
501 !if_hasconf(ctx, ifp->name))
502 active = IF_INACTIVE;
504 logwarnx("%s: unsupported"
505 " interface type %.2x",
506 ifp->name, sdl->sdl_type);
507 /* Pretend it's ethernet */
508 ifp->family = ARPHRD_ETHER;
511 ifp->hwlen = sdl->sdl_alen;
512 memcpy(ifp->hwaddr, CLLADDR(sdl), ifp->hwlen);
514 sll = (const void *)ifa->ifa_addr;
515 ifp->index = (unsigned int)sll->sll_ifindex;
516 ifp->family = sll->sll_hatype;
517 ifp->hwlen = sll->sll_halen;
519 memcpy(ifp->hwaddr, sll->sll_addr, ifp->hwlen);
524 /* This is a huge bug in getifaddrs(3) as there
525 * is no reason why this can't be returned in
527 memset(&ifr, 0, sizeof(ifr));
528 strlcpy(ifr.ifr_name, ifa->ifa_name,
529 sizeof(ifr.ifr_name));
530 if (ioctl(ctx->pf_inet_fd, SIOCGIFHWADDR, &ifr) == -1)
531 logerr("%s: SIOCGIFHWADDR", ifa->ifa_name);
532 ifp->family = ifr.ifr_hwaddr.sa_family;
533 if (ioctl(ctx->pf_inet_fd, SIOCGIFINDEX, &ifr) == -1)
534 logerr("%s: SIOCGIFINDEX", ifa->ifa_name);
535 ifp->index = (unsigned int)ifr.ifr_ifindex;
539 /* Ensure hardware address is valid. */
540 if (!if_valid_hwaddr(ifp->hwaddr, ifp->hwlen))
543 /* We only work on ethernet by default */
544 if (ifp->family != ARPHRD_ETHER) {
545 if ((argc == 0 || argc == -1) &&
546 ctx->ifac == 0 && !if_hasconf(ctx, ifp->name))
547 active = IF_INACTIVE;
548 switch (ifp->family) {
549 case ARPHRD_IEEE1394:
550 case ARPHRD_INFINIBAND:
551 #ifdef ARPHRD_LOOPBACK
552 case ARPHRD_LOOPBACK:
560 /* We don't warn for supported families */
563 /* IFT already checked */
567 logwarnx("%s: unsupported"
568 " interface family %.2x",
569 ifp->name, ifp->family);
575 if (!(ctx->options & (DHCPCD_DUMPLEASE | DHCPCD_TEST))) {
576 /* Handle any platform init for the interface */
577 if (active != IF_INACTIVE && if_init(ifp) == -1) {
578 logerr("%s: if_init", ifp->name);
584 ifp->vlanid = if_vlanid(ifp);
586 #ifdef SIOCGIFPRIORITY
587 /* Respect the interface priority */
588 memset(&ifr, 0, sizeof(ifr));
589 strlcpy(ifr.ifr_name, ifp->name, sizeof(ifr.ifr_name));
590 if (ioctl(ctx->pf_inet_fd, SIOCGIFPRIORITY, &ifr) == 0)
591 ifp->metric = (unsigned int)ifr.ifr_metric;
594 /* We reserve the 100 range for virtual interfaces, if and when
595 * we can work them out. */
596 ifp->metric = 200 + ifp->index;
597 if (if_getssid(ifp) != -1) {
598 ifp->wireless = true;
603 ifp->active = active;
605 ifp->carrier = if_carrier(ifp);
607 ifp->carrier = LINK_UNKNOWN;
608 TAILQ_INSERT_TAIL(ifs, ifp, next);
617 /* Decode bge0:1 as dev = bge, ppa = 0 and lun = 1 */
619 if_nametospec(const char *ifname, struct if_spec *spec)
624 if (ifname == NULL || *ifname == '\0' ||
625 strlcpy(spec->ifname, ifname, sizeof(spec->ifname)) >=
626 sizeof(spec->ifname) ||
627 strlcpy(spec->drvname, ifname, sizeof(spec->drvname)) >=
628 sizeof(spec->drvname))
633 ep = strchr(spec->drvname, ':');
635 spec->lun = (int)strtoi(ep + 1, NULL, 10, 0, INT_MAX, &e);
643 ep = spec->drvname + strlen(spec->drvname) - 1;
645 strlcpy(spec->devname, spec->drvname, sizeof(spec->devname));
646 while (ep > spec->drvname && isdigit((int)*ep))
652 spec->ppa = (int)strtoi(ep, NULL, 10, 0, INT_MAX, &e);
660 static struct interface *
661 if_findindexname(struct if_head *ifaces, unsigned int idx, const char *name)
664 if (ifaces != NULL) {
666 struct interface *ifp;
668 if (name && if_nametospec(name, &spec) == -1)
671 TAILQ_FOREACH(ifp, ifaces, next) {
672 if ((name && strcmp(ifp->name, spec.devname) == 0) ||
673 (!name && ifp->index == idx))
683 if_find(struct if_head *ifaces, const char *name)
686 return if_findindexname(ifaces, 0, name);
690 if_findindex(struct if_head *ifaces, unsigned int idx)
693 return if_findindexname(ifaces, idx, NULL);
697 if_loopback(struct dhcpcd_ctx *ctx)
699 struct interface *ifp;
701 TAILQ_FOREACH(ifp, ctx->ifaces, next) {
702 if (ifp->flags & IFF_LOOPBACK)
709 if_domtu(const struct interface *ifp, short int mtu)
716 return if_mtu_os(ifp);
719 memset(&ifr, 0, sizeof(ifr));
720 strlcpy(ifr.ifr_name, ifp->name, sizeof(ifr.ifr_name));
722 r = ioctl(ifp->ctx->pf_inet_fd, mtu ? SIOCSIFMTU : SIOCGIFMTU, &ifr);
730 if_makealias(char *alias, size_t alias_len, const char *ifname, int lun)
734 return strlcpy(alias, ifname, alias_len);
735 return snprintf(alias, alias_len, "%s:%u", ifname, lun);
740 if_findifpfromcmsg(struct dhcpcd_ctx *ctx, struct msghdr *msg, int *hoplimit)
743 unsigned int ifindex = 0;
744 struct interface *ifp;
745 #if defined(INET) && defined(IP_PKTINFO)
746 struct in_pktinfo ipi;
749 struct in6_pktinfo ipi6;
754 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(msg);
756 cm = (struct cmsghdr *)CMSG_NXTHDR(msg, cm))
758 #if defined(INET) && defined(IP_PKTINFO)
759 if (cm->cmsg_level == IPPROTO_IP) {
760 switch(cm->cmsg_type) {
762 if (cm->cmsg_len != CMSG_LEN(sizeof(ipi)))
764 memcpy(&ipi, CMSG_DATA(cm), sizeof(ipi));
765 ifindex = (unsigned int)ipi.ipi_ifindex;
771 if (cm->cmsg_level == IPPROTO_IPV6) {
772 switch(cm->cmsg_type) {
774 if (cm->cmsg_len != CMSG_LEN(sizeof(ipi6)))
776 memcpy(&ipi6, CMSG_DATA(cm), sizeof(ipi6));
777 ifindex = (unsigned int)ipi6.ipi6_ifindex;
780 if (cm->cmsg_len != CMSG_LEN(sizeof(int)))
782 if (hoplimit == NULL)
784 memcpy(hoplimit, CMSG_DATA(cm), sizeof(int));
791 /* Find the receiving interface */
792 TAILQ_FOREACH(ifp, ctx->ifaces, next) {
793 if (ifp->index == ifindex)
802 xsocket(int domain, int type, int protocol)
805 #if !defined(HAVE_SOCK_CLOEXEC) || !defined(HAVE_SOCK_NONBLOCK)
806 int xflags, xtype = type;
812 #ifndef HAVE_SOCK_CLOEXEC
813 if (xtype & SOCK_CLOEXEC)
814 type &= ~SOCK_CLOEXEC;
816 #ifndef HAVE_SOCK_NONBLOCK
817 if (xtype & SOCK_NONBLOCK)
818 type &= ~SOCK_NONBLOCK;
821 if ((s = socket(domain, type, protocol)) == -1)
824 #ifndef HAVE_SOCK_CLOEXEC
825 if ((xtype & SOCK_CLOEXEC) && ((xflags = fcntl(s, F_GETFD)) == -1 ||
826 fcntl(s, F_SETFD, xflags | FD_CLOEXEC) == -1))
829 #ifndef HAVE_SOCK_NONBLOCK
830 if ((xtype & SOCK_NONBLOCK) && ((xflags = fcntl(s, F_GETFL)) == -1 ||
831 fcntl(s, F_SETFL, xflags | O_NONBLOCK) == -1))
836 /* Tell recvmsg(2) to return ENOBUFS if the receiving socket overflows. */
838 if (setsockopt(s, SOL_SOCKET, SO_RERROR, &on, sizeof(on)) == -1)
839 logerr("%s: SO_RERROR", __func__);
844 #if !defined(HAVE_SOCK_CLOEXEC) || !defined(HAVE_SOCK_NONBLOCK)