1 /* SPDX-License-Identifier: BSD-2-Clause */
3 * dhcpcd - DHCP client daemon
4 * Copyright (c) 2006-2020 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>
34 #include <fcntl.h> /* Needs to be here for old Linux */
39 #include <net/if_arp.h>
40 #include <netinet/in.h>
42 # include <net/if_dl.h>
43 # include <net/if_types.h>
44 # include <netinet/in_var.h>
45 # undef AF_PACKET /* Newer Illumos defines this */
48 # include <netpacket/packet.h>
51 # include <net/if_media.h>
53 #include <net/route.h>
72 #include "if-options.h"
80 if_free(struct interface *ifp)
100 free_options(ifp->ctx, ifp->options);
105 if_opensockets(struct dhcpcd_ctx *ctx)
108 if (if_opensockets_os(ctx) == -1)
112 ctx->pf_link_fd = xsocket(PF_LINK, SOCK_DGRAM | SOCK_CLOEXEC, 0);
113 if (ctx->pf_link_fd == -1)
116 if (ps_rights_limit_ioctl(ctx->pf_link_fd) == -1)
121 /* We use this socket for some operations without INET. */
122 ctx->pf_inet_fd = xsocket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
123 if (ctx->pf_inet_fd == -1)
130 if_closesockets(struct dhcpcd_ctx *ctx)
133 if (ctx->pf_inet_fd != -1)
134 close(ctx->pf_inet_fd);
136 if (ctx->pf_link_fd != -1)
137 close(ctx->pf_link_fd);
141 if_closesockets_os(ctx);
147 if_ioctl(struct dhcpcd_ctx *ctx, ioctl_request_t req, void *data, size_t len)
151 if (ctx->options & DHCPCD_PRIVSEP)
152 return (int)ps_root_ioctl(ctx, req, data, len);
154 return ioctl(ctx->pf_inet_fd, req, data, len);
158 if_getflags(struct interface *ifp)
160 struct ifreq ifr = { .ifr_flags = 0 };
162 strlcpy(ifr.ifr_name, ifp->name, sizeof(ifr.ifr_name));
163 if (ioctl(ifp->ctx->pf_inet_fd, SIOCGIFFLAGS, &ifr) == -1)
165 ifp->flags = (unsigned int)ifr.ifr_flags;
170 if_setflag(struct interface *ifp, short setflag, short unsetflag)
172 struct ifreq ifr = { .ifr_flags = 0 };
175 strlcpy(ifr.ifr_name, ifp->name, sizeof(ifr.ifr_name));
176 if (ioctl(ifp->ctx->pf_inet_fd, SIOCGIFFLAGS, &ifr) == -1)
179 oflags = ifr.ifr_flags;
180 ifr.ifr_flags |= setflag;
181 ifr.ifr_flags &= (short)~unsetflag;
182 if (ifr.ifr_flags != oflags &&
183 if_ioctl(ifp->ctx, SIOCSIFFLAGS, &ifr, sizeof(ifr)) == -1)
186 ifp->flags = (unsigned int)ifr.ifr_flags;
191 if_randomisemac(struct interface *ifp)
194 size_t hwlen = ifp->hwlen, rlen = 0;
195 uint8_t buf[HWADDR_LEN], *bp = buf, *rp = (uint8_t *)&randnum;
196 char sbuf[HWADDR_LEN * 3];
203 if (hwlen > sizeof(buf)) {
208 for (; hwlen != 0; hwlen--) {
210 randnum = arc4random();
211 rp = (uint8_t *)&randnum;
212 rlen = sizeof(randnum);
218 /* Unicast address and locally administered. */
222 logdebugx("%s: hardware address randomised to %s",
224 hwaddr_ntoa(buf, ifp->hwlen, sbuf, sizeof(sbuf)));
225 retval = if_setmac(ifp, buf, ifp->hwlen);
227 memcpy(ifp->hwaddr, buf, ifp->hwlen);
232 if_hasconf(struct dhcpcd_ctx *ctx, const char *ifname)
236 for (i = 0; i < ctx->ifcc; i++) {
237 if (strcmp(ctx->ifcv[i], ifname) == 0)
244 if_markaddrsstale(struct if_head *ifs)
246 struct interface *ifp;
248 TAILQ_FOREACH(ifp, ifs, next) {
250 ipv4_markaddrsstale(ifp);
253 ipv6_markaddrsstale(ifp, 0);
259 if_learnaddrs(struct dhcpcd_ctx *ctx, struct if_head *ifs,
260 struct ifaddrs **ifaddrs)
263 struct interface *ifp;
265 const struct sockaddr_in *addr, *net, *brd;
268 struct sockaddr_in6 *sin6, *net6;
272 for (ifa = *ifaddrs; ifa; ifa = ifa->ifa_next) {
273 if (ifa->ifa_addr == NULL)
275 if ((ifp = if_find(ifs, ifa->ifa_name)) == NULL)
277 #ifdef HAVE_IFADDRS_ADDRFLAGS
278 addrflags = (int)ifa->ifa_addrflags;
280 switch(ifa->ifa_addr->sa_family) {
283 addr = (void *)ifa->ifa_addr;
284 net = (void *)ifa->ifa_netmask;
285 if (ifa->ifa_flags & IFF_POINTOPOINT)
286 brd = (void *)ifa->ifa_dstaddr;
288 brd = (void *)ifa->ifa_broadaddr;
289 #ifndef HAVE_IFADDRS_ADDRFLAGS
290 addrflags = if_addrflags(ifp, &addr->sin_addr,
292 if (addrflags == -1) {
293 if (errno != EEXIST && errno != EADDRNOTAVAIL) {
294 char dbuf[INET_ADDRSTRLEN];
297 dbp = inet_ntop(AF_INET, &addr->sin_addr,
299 logerr("%s: if_addrflags: %s%%%s",
300 __func__, dbp, ifp->name);
305 ipv4_handleifa(ctx, RTM_NEWADDR, ifs, ifa->ifa_name,
306 &addr->sin_addr, &net->sin_addr,
307 brd ? &brd->sin_addr : NULL, addrflags, 0);
312 sin6 = (void *)ifa->ifa_addr;
313 net6 = (void *)ifa->ifa_netmask;
316 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
317 /* Remove the scope from the address */
318 sin6->sin6_addr.s6_addr[2] =
319 sin6->sin6_addr.s6_addr[3] = '\0';
321 #ifndef HAVE_IFADDRS_ADDRFLAGS
322 addrflags = if_addrflags6(ifp, &sin6->sin6_addr,
324 if (addrflags == -1) {
325 if (errno != EEXIST && errno != EADDRNOTAVAIL) {
326 char dbuf[INET6_ADDRSTRLEN];
329 dbp = inet_ntop(AF_INET6, &sin6->sin6_addr,
331 logerr("%s: if_addrflags6: %s%%%s",
332 __func__, dbp, ifp->name);
337 ipv6_handleifa(ctx, RTM_NEWADDR, ifs,
338 ifa->ifa_name, &sin6->sin6_addr,
339 ipv6_prefixlen(&net6->sin6_addr), addrflags, 0);
345 #ifdef PRIVSEP_GETIFADDRS
350 freeifaddrs(*ifaddrs);
355 if_deletestaleaddrs(struct if_head *ifs)
357 struct interface *ifp;
359 TAILQ_FOREACH(ifp, ifs, next) {
361 ipv4_deletestaleaddrs(ifp);
364 ipv6_deletestaleaddrs(ifp);
370 if_valid_hwaddr(const uint8_t *hwaddr, size_t hwlen)
373 bool all_zeros, all_ones;
375 all_zeros = all_ones = true;
376 for (i = 0; i < hwlen; i++) {
377 if (hwaddr[i] != 0x00)
379 if (hwaddr[i] != 0xff)
381 if (!all_zeros && !all_ones)
388 if_discover(struct dhcpcd_ctx *ctx, struct ifaddrs **ifaddrs,
389 int argc, char * const *argv)
395 struct interface *ifp;
399 const struct sockaddr_dl *sdl;
401 struct if_laddrreq iflr = { .flags = IFLR_PREFIX };
403 #elif defined(AF_PACKET)
404 const struct sockaddr_ll *sll;
406 #if defined(SIOCGIFPRIORITY)
410 if ((ifs = malloc(sizeof(*ifs))) == NULL) {
416 #ifdef PRIVSEP_GETIFADDRS
417 if (ctx->options & DHCPCD_PRIVSEP) {
418 if (ps_root_getifaddrs(ctx, ifaddrs) == -1) {
419 logerr("ps_root_getifaddrs");
425 if (getifaddrs(ifaddrs) == -1) {
426 logerr("getifaddrs");
431 for (ifa = *ifaddrs; ifa; ifa = ifa->ifa_next) {
432 if (ifa->ifa_addr != NULL) {
434 if (ifa->ifa_addr->sa_family != AF_LINK)
436 #elif defined(AF_PACKET)
437 if (ifa->ifa_addr->sa_family != AF_PACKET)
441 if (if_nametospec(ifa->ifa_name, &spec) != 0)
444 /* It's possible for an interface to have >1 AF_LINK.
445 * For our purposes, we use the first one. */
446 TAILQ_FOREACH(ifp, ifs, next) {
447 if (strcmp(ifp->name, spec.devname) == 0)
454 for (i = 0; i < argc; i++) {
455 if (strcmp(argv[i], spec.devname) == 0)
458 active = (i == argc) ? IF_INACTIVE : IF_ACTIVE_USER;
460 /* -1 means we're discovering against a specific
461 * interface, but we still need the below rules
463 if (argc == -1 && strcmp(argv[0], spec.devname) != 0)
465 active = ctx->options & DHCPCD_INACTIVE ?
466 IF_INACTIVE: IF_ACTIVE_USER;
469 for (i = 0; i < ctx->ifdc; i++)
470 if (fnmatch(ctx->ifdv[i], spec.devname, 0) == 0)
473 active = IF_INACTIVE;
474 for (i = 0; i < ctx->ifc; i++)
475 if (fnmatch(ctx->ifv[i], spec.devname, 0) == 0)
477 if (ctx->ifc && i == ctx->ifc)
478 active = IF_INACTIVE;
479 for (i = 0; i < ctx->ifac; i++)
480 if (fnmatch(ctx->ifav[i], spec.devname, 0) == 0)
482 if (ctx->ifac && i == ctx->ifac)
483 active = IF_INACTIVE;
486 /* Ensure that the interface name has settled */
487 if (!dev_initialized(ctx, spec.devname))
491 if (if_vimaster(ctx, spec.devname) == 1) {
492 int loglevel = argc != 0 ? LOG_ERR : LOG_DEBUG;
494 "%s: is a Virtual Interface Master, skipping",
499 if_noconf = ((argc == 0 || argc == -1) && ctx->ifac == 0 &&
500 !if_hasconf(ctx, spec.devname));
502 /* Don't allow some reserved interface names unless explicit. */
503 if (if_noconf && if_ignore(ctx, spec.devname))
504 active = IF_INACTIVE;
506 ifp = calloc(1, sizeof(*ifp));
512 strlcpy(ifp->name, spec.devname, sizeof(ifp->name));
513 ifp->flags = ifa->ifa_flags;
515 if (ifa->ifa_addr != NULL) {
517 sdl = (const void *)ifa->ifa_addr;
520 /* We need to check for active address */
521 strlcpy(iflr.iflr_name, ifp->name,
522 sizeof(iflr.iflr_name));
523 memcpy(&iflr.addr, ifa->ifa_addr,
524 MIN(ifa->ifa_addr->sa_len, sizeof(iflr.addr)));
525 iflr.flags = IFLR_PREFIX;
526 iflr.prefixlen = (unsigned int)sdl->sdl_alen * NBBY;
527 if (ioctl(ctx->pf_link_fd, SIOCGLIFADDR, &iflr) == -1 ||
528 !(iflr.flags & IFLR_ACTIVE))
535 ifp->index = sdl->sdl_index;
536 switch(sdl->sdl_type) {
538 case IFT_BRIDGE: /* FALLTHROUGH */
540 #ifdef IFT_PROPVIRTUAL
541 case IFT_PROPVIRTUAL: /* FALLTHROUGH */
544 case IFT_TUNNEL: /* FALLTHROUGH */
546 case IFT_LOOP: /* FALLTHROUGH */
548 /* Don't allow unless explicit */
550 logdebugx("%s: ignoring due to"
551 " interface type and"
554 active = IF_INACTIVE;
556 __fallthrough; /* appease gcc */
559 case IFT_L2VLAN: /* FALLTHROUGH */
562 case IFT_L3IPVLAN: /* FALLTHROUGH */
565 ifp->hwtype = ARPHRD_ETHER;
569 ifp->hwtype = ARPHRD_IEEE1394;
572 #ifdef IFT_INFINIBAND
574 ifp->hwtype = ARPHRD_INFINIBAND;
578 /* Don't allow unless explicit */
580 active = IF_INACTIVE;
582 logwarnx("%s: unsupported"
583 " interface type 0x%.2x",
584 ifp->name, sdl->sdl_type);
585 /* Pretend it's ethernet */
586 ifp->hwtype = ARPHRD_ETHER;
589 ifp->hwlen = sdl->sdl_alen;
590 memcpy(ifp->hwaddr, CLLADDR(sdl), ifp->hwlen);
591 #elif defined(AF_PACKET)
592 sll = (const void *)ifa->ifa_addr;
593 ifp->index = (unsigned int)sll->sll_ifindex;
594 ifp->hwtype = sll->sll_hatype;
595 ifp->hwlen = sll->sll_halen;
597 memcpy(ifp->hwaddr, sll->sll_addr, ifp->hwlen);
599 switch(ifp->hwtype) {
600 case ARPHRD_ETHER: /* FALLTHROUGH */
601 case ARPHRD_IEEE1394: /* FALLTHROUGH */
602 case ARPHRD_INFINIBAND: /* FALLTHROUGH */
603 case ARPHRD_NONE: /* FALLTHROUGH */
605 case ARPHRD_LOOPBACK:
608 logdebugx("%s: ignoring due to"
609 " interface type and"
612 active = IF_INACTIVE;
617 logwarnx("%s: unsupported"
618 " interface type 0x%.2x",
619 ifp->name, ifp->hwtype);
625 if (!(ctx->options & (DHCPCD_DUMPLEASE | DHCPCD_TEST))) {
626 /* Handle any platform init for the interface */
627 if (active != IF_INACTIVE && if_init(ifp) == -1) {
628 logerr("%s: if_init", ifp->name);
634 ifp->vlanid = if_vlanid(ifp);
636 #ifdef SIOCGIFPRIORITY
637 /* Respect the interface priority */
638 memset(&ifr, 0, sizeof(ifr));
639 strlcpy(ifr.ifr_name, ifp->name, sizeof(ifr.ifr_name));
640 if (pioctl(ctx, SIOCGIFPRIORITY, &ifr, sizeof(ifr)) == 0)
641 ifp->metric = (unsigned int)ifr.ifr_metric;
644 /* We reserve the 100 range for virtual interfaces, if and when
645 * we can work them out. */
646 ifp->metric = 200 + ifp->index;
647 if (if_getssid(ifp) != -1) {
648 ifp->wireless = true;
653 ifp->active = active;
655 ifp->carrier = if_carrier(ifp);
657 ifp->carrier = LINK_UNKNOWN;
658 TAILQ_INSERT_TAIL(ifs, ifp, next);
665 * eth0.100:2 OR eth0i100:2 (seems to be NetBSD xvif(4) only)
668 * devname == eth0.100 OR eth0i100
673 if_nametospec(const char *ifname, struct if_spec *spec)
678 if (ifname == NULL || *ifname == '\0' ||
679 strlcpy(spec->ifname, ifname, sizeof(spec->ifname)) >=
680 sizeof(spec->ifname) ||
681 strlcpy(spec->drvname, ifname, sizeof(spec->drvname)) >=
682 sizeof(spec->drvname))
689 ep = strchr(spec->drvname, ':');
691 spec->lun = (int)strtoi(ep + 1, NULL, 10, 0, INT_MAX, &e);
703 ep = spec->drvname + strlen(spec->drvname) - 1;
707 strlcpy(spec->devname, spec->drvname, sizeof(spec->devname));
709 /* Solaris has numbers in the driver name, such as e1000g */
710 while (ep > spec->drvname && isdigit((int)*ep))
717 /* BSD and Linux no not have numbers in the driver name */
718 for (ep = spec->drvname; *ep != '\0' && !isdigit((int)*ep); ep++) {
725 spec->ppa = (int)strtoi(ep, &pp, 10, 0, INT_MAX, &e);
730 * . is used for VLAN style names
731 * i is used on NetBSD for xvif interfaces
733 if (pp != NULL && (*pp == '.' || *pp == 'i')) {
734 spec->vlid = (int)strtoi(pp + 1, NULL, 10, 0, INT_MAX, &e);
744 static struct interface *
745 if_findindexname(struct if_head *ifaces, unsigned int idx, const char *name)
748 if (ifaces != NULL) {
750 struct interface *ifp;
752 if (name && if_nametospec(name, &spec) == -1)
755 TAILQ_FOREACH(ifp, ifaces, next) {
756 if ((name && strcmp(ifp->name, spec.devname) == 0) ||
757 (!name && ifp->index == idx))
767 if_find(struct if_head *ifaces, const char *name)
770 return if_findindexname(ifaces, 0, name);
774 if_findindex(struct if_head *ifaces, unsigned int idx)
777 return if_findindexname(ifaces, idx, NULL);
781 if_loopback(struct dhcpcd_ctx *ctx)
783 struct interface *ifp;
785 TAILQ_FOREACH(ifp, ctx->ifaces, next) {
786 if (ifp->flags & IFF_LOOPBACK)
793 if_domtu(const struct interface *ifp, short int mtu)
800 return if_mtu_os(ifp);
803 memset(&ifr, 0, sizeof(ifr));
804 strlcpy(ifr.ifr_name, ifp->name, sizeof(ifr.ifr_name));
807 r = if_ioctl(ifp->ctx, SIOCSIFMTU, &ifr, sizeof(ifr));
809 r = pioctl(ifp->ctx, SIOCGIFMTU, &ifr, sizeof(ifr));
818 if_makealias(char *alias, size_t alias_len, const char *ifname, int lun)
822 return strlcpy(alias, ifname, alias_len);
823 return snprintf(alias, alias_len, "%s:%u", ifname, lun);
828 if_findifpfromcmsg(struct dhcpcd_ctx *ctx, struct msghdr *msg, int *hoplimit)
831 unsigned int ifindex = 0;
832 struct interface *ifp;
835 struct sockaddr_dl sdl;
837 struct in_pktinfo ipi;
841 struct in6_pktinfo ipi6;
846 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(msg);
848 cm = (struct cmsghdr *)CMSG_NXTHDR(msg, cm))
851 if (cm->cmsg_level == IPPROTO_IP) {
852 switch(cm->cmsg_type) {
856 offsetof(struct sockaddr_dl, sdl_index) +
857 sizeof(sdl.sdl_index))
859 memcpy(&sdl, CMSG_DATA(cm),
860 MIN(sizeof(sdl), cm->cmsg_len));
861 ifindex = sdl.sdl_index;
865 if (cm->cmsg_len != CMSG_LEN(sizeof(ipi)))
867 memcpy(&ipi, CMSG_DATA(cm), sizeof(ipi));
868 ifindex = (unsigned int)ipi.ipi_ifindex;
875 if (cm->cmsg_level == IPPROTO_IPV6) {
876 switch(cm->cmsg_type) {
878 if (cm->cmsg_len != CMSG_LEN(sizeof(ipi6)))
880 memcpy(&ipi6, CMSG_DATA(cm), sizeof(ipi6));
881 ifindex = (unsigned int)ipi6.ipi6_ifindex;
884 if (cm->cmsg_len != CMSG_LEN(sizeof(int)))
886 if (hoplimit == NULL)
888 memcpy(hoplimit, CMSG_DATA(cm), sizeof(int));
895 /* Find the receiving interface */
896 TAILQ_FOREACH(ifp, ctx->ifaces, next) {
897 if (ifp->index == ifindex)
906 xsocket(int domain, int type, int protocol)
909 #if !defined(HAVE_SOCK_CLOEXEC) || !defined(HAVE_SOCK_NONBLOCK)
910 int xflags, xtype = type;
913 #ifndef HAVE_SOCK_CLOEXEC
914 if (xtype & SOCK_CLOEXEC)
915 type &= ~SOCK_CLOEXEC;
917 #ifndef HAVE_SOCK_NONBLOCK
918 if (xtype & SOCK_NONBLOCK)
919 type &= ~SOCK_NONBLOCK;
922 if ((s = socket(domain, type, protocol)) == -1)
925 #ifndef HAVE_SOCK_CLOEXEC
926 if ((xtype & SOCK_CLOEXEC) && ((xflags = fcntl(s, F_GETFD)) == -1 ||
927 fcntl(s, F_SETFD, xflags | FD_CLOEXEC) == -1))
930 #ifndef HAVE_SOCK_NONBLOCK
931 if ((xtype & SOCK_NONBLOCK) && ((xflags = fcntl(s, F_GETFL)) == -1 ||
932 fcntl(s, F_SETFL, xflags | O_NONBLOCK) == -1))
938 #if !defined(HAVE_SOCK_CLOEXEC) || !defined(HAVE_SOCK_NONBLOCK)