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 */
478 case IFT_L2VLAN
: /* FALLTHROUGH */
481 case IFT_L3IPVLAN
: /* FALLTHROUGH */
484 ifp
->family
= ARPHRD_ETHER
;
488 ifp
->family
= ARPHRD_IEEE1394
;
491 #ifdef IFT_INFINIBAND
493 ifp
->family
= ARPHRD_INFINIBAND
;
497 /* Don't allow unless explicit */
498 if ((argc
== 0 || argc
== -1) &&
500 !if_hasconf(ctx
, ifp
->name
))
501 active
= IF_INACTIVE
;
503 logwarnx("%s: unsupported"
504 " interface type %.2x",
505 ifp
->name
, sdl
->sdl_type
);
506 /* Pretend it's ethernet */
507 ifp
->family
= ARPHRD_ETHER
;
510 ifp
->hwlen
= sdl
->sdl_alen
;
511 memcpy(ifp
->hwaddr
, CLLADDR(sdl
), ifp
->hwlen
);
513 sll
= (const void *)ifa
->ifa_addr
;
514 ifp
->index
= (unsigned int)sll
->sll_ifindex
;
515 ifp
->family
= sll
->sll_hatype
;
516 ifp
->hwlen
= sll
->sll_halen
;
518 memcpy(ifp
->hwaddr
, sll
->sll_addr
, ifp
->hwlen
);
523 /* This is a huge bug in getifaddrs(3) as there
524 * is no reason why this can't be returned in
526 memset(&ifr
, 0, sizeof(ifr
));
527 strlcpy(ifr
.ifr_name
, ifa
->ifa_name
,
528 sizeof(ifr
.ifr_name
));
529 if (ioctl(ctx
->pf_inet_fd
, SIOCGIFHWADDR
, &ifr
) == -1)
530 logerr("%s: SIOCGIFHWADDR", ifa
->ifa_name
);
531 ifp
->family
= ifr
.ifr_hwaddr
.sa_family
;
532 if (ioctl(ctx
->pf_inet_fd
, SIOCGIFINDEX
, &ifr
) == -1)
533 logerr("%s: SIOCGIFINDEX", ifa
->ifa_name
);
534 ifp
->index
= (unsigned int)ifr
.ifr_ifindex
;
538 /* Ensure hardware address is valid. */
539 if (!if_valid_hwaddr(ifp
->hwaddr
, ifp
->hwlen
))
542 /* We only work on ethernet by default */
543 if (ifp
->family
!= ARPHRD_ETHER
) {
544 if ((argc
== 0 || argc
== -1) &&
545 ctx
->ifac
== 0 && !if_hasconf(ctx
, ifp
->name
))
546 active
= IF_INACTIVE
;
547 switch (ifp
->family
) {
548 case ARPHRD_IEEE1394
:
549 case ARPHRD_INFINIBAND
:
550 #ifdef ARPHRD_LOOPBACK
551 case ARPHRD_LOOPBACK
:
559 /* We don't warn for supported families */
562 /* IFT already checked */
566 logwarnx("%s: unsupported"
567 " interface family %.2x",
568 ifp
->name
, ifp
->family
);
574 if (!(ctx
->options
& (DHCPCD_DUMPLEASE
| DHCPCD_TEST
))) {
575 /* Handle any platform init for the interface */
576 if (active
!= IF_INACTIVE
&& if_init(ifp
) == -1) {
577 logerr("%s: if_init", ifp
->name
);
583 ifp
->vlanid
= if_vlanid(ifp
);
585 #ifdef SIOCGIFPRIORITY
586 /* Respect the interface priority */
587 memset(&ifr
, 0, sizeof(ifr
));
588 strlcpy(ifr
.ifr_name
, ifp
->name
, sizeof(ifr
.ifr_name
));
589 if (ioctl(ctx
->pf_inet_fd
, SIOCGIFPRIORITY
, &ifr
) == 0)
590 ifp
->metric
= (unsigned int)ifr
.ifr_metric
;
593 /* We reserve the 100 range for virtual interfaces, if and when
594 * we can work them out. */
595 ifp
->metric
= 200 + ifp
->index
;
596 if (if_getssid(ifp
) != -1) {
597 ifp
->wireless
= true;
602 ifp
->active
= active
;
604 ifp
->carrier
= if_carrier(ifp
);
606 ifp
->carrier
= LINK_UNKNOWN
;
607 TAILQ_INSERT_TAIL(ifs
, ifp
, next
);
616 /* Decode bge0:1 as dev = bge, ppa = 0 and lun = 1 */
618 if_nametospec(const char *ifname
, struct if_spec
*spec
)
623 if (ifname
== NULL
|| *ifname
== '\0' ||
624 strlcpy(spec
->ifname
, ifname
, sizeof(spec
->ifname
)) >=
625 sizeof(spec
->ifname
) ||
626 strlcpy(spec
->drvname
, ifname
, sizeof(spec
->drvname
)) >=
627 sizeof(spec
->drvname
))
632 ep
= strchr(spec
->drvname
, ':');
634 spec
->lun
= (int)strtoi(ep
+ 1, NULL
, 10, 0, INT_MAX
, &e
);
642 ep
= spec
->drvname
+ strlen(spec
->drvname
) - 1;
644 strlcpy(spec
->devname
, spec
->drvname
, sizeof(spec
->devname
));
645 while (ep
> spec
->drvname
&& isdigit((int)*ep
))
651 spec
->ppa
= (int)strtoi(ep
, NULL
, 10, 0, INT_MAX
, &e
);
659 static struct interface
*
660 if_findindexname(struct if_head
*ifaces
, unsigned int idx
, const char *name
)
663 if (ifaces
!= NULL
) {
665 struct interface
*ifp
;
667 if (name
&& if_nametospec(name
, &spec
) == -1)
670 TAILQ_FOREACH(ifp
, ifaces
, next
) {
671 if ((name
&& strcmp(ifp
->name
, spec
.devname
) == 0) ||
672 (!name
&& ifp
->index
== idx
))
682 if_find(struct if_head
*ifaces
, const char *name
)
685 return if_findindexname(ifaces
, 0, name
);
689 if_findindex(struct if_head
*ifaces
, unsigned int idx
)
692 return if_findindexname(ifaces
, idx
, NULL
);
696 if_loopback(struct dhcpcd_ctx
*ctx
)
698 struct interface
*ifp
;
700 TAILQ_FOREACH(ifp
, ctx
->ifaces
, next
) {
701 if (ifp
->flags
& IFF_LOOPBACK
)
708 if_domtu(const struct interface
*ifp
, short int mtu
)
715 return if_mtu_os(ifp
);
718 memset(&ifr
, 0, sizeof(ifr
));
719 strlcpy(ifr
.ifr_name
, ifp
->name
, sizeof(ifr
.ifr_name
));
721 r
= ioctl(ifp
->ctx
->pf_inet_fd
, mtu
? SIOCSIFMTU
: SIOCGIFMTU
, &ifr
);
729 if_makealias(char *alias
, size_t alias_len
, const char *ifname
, int lun
)
733 return strlcpy(alias
, ifname
, alias_len
);
734 return snprintf(alias
, alias_len
, "%s:%u", ifname
, lun
);
739 if_findifpfromcmsg(struct dhcpcd_ctx
*ctx
, struct msghdr
*msg
, int *hoplimit
)
742 unsigned int ifindex
= 0;
743 struct interface
*ifp
;
744 #if defined(INET) && defined(IP_PKTINFO)
745 struct in_pktinfo ipi
;
748 struct in6_pktinfo ipi6
;
753 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(msg
);
755 cm
= (struct cmsghdr
*)CMSG_NXTHDR(msg
, cm
))
757 #if defined(INET) && defined(IP_PKTINFO)
758 if (cm
->cmsg_level
== IPPROTO_IP
) {
759 switch(cm
->cmsg_type
) {
761 if (cm
->cmsg_len
!= CMSG_LEN(sizeof(ipi
)))
763 memcpy(&ipi
, CMSG_DATA(cm
), sizeof(ipi
));
764 ifindex
= (unsigned int)ipi
.ipi_ifindex
;
770 if (cm
->cmsg_level
== IPPROTO_IPV6
) {
771 switch(cm
->cmsg_type
) {
773 if (cm
->cmsg_len
!= CMSG_LEN(sizeof(ipi6
)))
775 memcpy(&ipi6
, CMSG_DATA(cm
), sizeof(ipi6
));
776 ifindex
= (unsigned int)ipi6
.ipi6_ifindex
;
779 if (cm
->cmsg_len
!= CMSG_LEN(sizeof(int)))
781 if (hoplimit
== NULL
)
783 memcpy(hoplimit
, CMSG_DATA(cm
), sizeof(int));
790 /* Find the receiving interface */
791 TAILQ_FOREACH(ifp
, ctx
->ifaces
, next
) {
792 if (ifp
->index
== ifindex
)
801 xsocket(int domain
, int type
, int protocol
)
804 #if !defined(HAVE_SOCK_CLOEXEC) || !defined(HAVE_SOCK_NONBLOCK)
805 int xflags
, xtype
= type
;
811 #ifndef HAVE_SOCK_CLOEXEC
812 if (xtype
& SOCK_CLOEXEC
)
813 type
&= ~SOCK_CLOEXEC
;
815 #ifndef HAVE_SOCK_NONBLOCK
816 if (xtype
& SOCK_NONBLOCK
)
817 type
&= ~SOCK_NONBLOCK
;
820 if ((s
= socket(domain
, type
, protocol
)) == -1)
823 #ifndef HAVE_SOCK_CLOEXEC
824 if ((xtype
& SOCK_CLOEXEC
) && ((xflags
= fcntl(s
, F_GETFD
)) == -1 ||
825 fcntl(s
, F_SETFD
, xflags
| FD_CLOEXEC
) == -1))
828 #ifndef HAVE_SOCK_NONBLOCK
829 if ((xtype
& SOCK_NONBLOCK
) && ((xflags
= fcntl(s
, F_GETFL
)) == -1 ||
830 fcntl(s
, F_SETFL
, xflags
| O_NONBLOCK
) == -1))
835 /* Tell recvmsg(2) to return ENOBUFS if the receiving socket overflows. */
837 if (setsockopt(s
, SOL_SOCKET
, SO_RERROR
, &on
, sizeof(on
)) == -1)
838 logerr("%s: SO_RERROR", __func__
);
843 #if !defined(HAVE_SOCK_CLOEXEC) || !defined(HAVE_SOCK_NONBLOCK)