1 /* SPDX-License-Identifier: BSD-2-Clause */
3 * BSD interface driver for dhcpcd
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/ioctl.h>
30 #include <sys/param.h>
31 #include <sys/socket.h>
33 #include <sys/sysctl.h>
35 #include <sys/types.h>
37 #include <sys/utsname.h>
41 #include <arpa/inet.h>
44 #include <net/if_dl.h>
45 #include <net/if_media.h>
46 #include <net/route.h>
47 #include <netinet/if_ether.h>
48 #include <netinet/in.h>
49 #include <netinet/in_var.h>
50 #include <netinet6/in6_var.h>
51 #include <netinet6/nd6.h>
53 #include <net/if_vlanvar.h> /* Needs netinet/if_ether.h */
54 #elif defined(__DragonFly__)
55 #include <net/vlan/if_vlan_var.h>
57 #include <net/if_vlan_var.h>
60 # include <netproto/802_11/ieee80211_ioctl.h>
62 # include <net80211/ieee80211.h>
63 # include <net80211/ieee80211_ioctl.h>
77 #if defined(OpenBSD) && OpenBSD >= 201411
78 /* OpenBSD dropped the global setting from sysctl but left the #define
79 * which causes a EPERM error when trying to use it.
80 * I think both the error and keeping the define are wrong, so we #undef it. */
81 #undef IPV6CTL_ACCEPT_RTADV
87 #include "if-options.h"
98 #define RT_ROUNDUP(a) \
99 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
100 #define RT_ADVANCE(x, n) (x += RT_ROUNDUP((n)->sa_len))
103 /* Ignore these interface names which look like ethernet but are virtual or
104 * just won't work without explicit configuration. */
105 static const char * const ifnames_ignore[] = {
107 "fwe", /* Firewire */
108 "fwip", /* Firewire */
110 "xvif", /* XEN DOM0 -> guest interface */
120 struct rt_msghdr hdr;
121 char buffer[sizeof(struct sockaddr_storage) * RTAX_MAX];
125 if_init(__unused struct interface *iface)
127 /* BSD promotes secondary address by default */
132 if_conf(__unused struct interface *iface)
134 /* No extra checks needed on BSD */
139 if_opensockets_os(struct dhcpcd_ctx *ctx)
143 #if defined(RO_MSGFILTER) || defined(ROUTE_MSGFILTER)
144 unsigned char msgfilter[] = {
146 #ifdef RTM_IFANNOUNCE
149 RTM_ADD, RTM_CHANGE, RTM_DELETE, RTM_MISS,
153 RTM_NEWADDR, RTM_DELADDR
155 #ifdef ROUTE_MSGFILTER
156 unsigned int i, msgfilter_mask;
160 if ((priv = malloc(sizeof(*priv))) == NULL)
165 priv->pf_inet6_fd = xsocket(PF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0);
166 /* Don't return an error so we at least work on kernels witout INET6
167 * even though we expect INET6 support.
168 * We will fail noisily elsewhere anyway. */
170 priv->pf_inet6_fd = -1;
173 ctx->link_fd = xsocket(PF_ROUTE, SOCK_RAW | SOCK_CXNB, AF_UNSPEC);
174 if (ctx->link_fd == -1)
179 if (setsockopt(ctx->link_fd, SOL_SOCKET, SO_RERROR, &n,sizeof(n)) == -1)
180 logerr("%s: SO_RERROR", __func__);
183 /* Ignore our own route(4) messages.
184 * Sadly there is no way of doing this for route(4) messages
185 * generated from addresses we add/delete. */
187 if (setsockopt(ctx->link_fd, SOL_SOCKET, SO_USELOOPBACK,
188 &n, sizeof(n)) == -1)
189 logerr("%s: SO_USELOOPBACK", __func__);
191 #if defined(RO_MSGFILTER)
192 if (setsockopt(ctx->link_fd, PF_ROUTE, RO_MSGFILTER,
193 &msgfilter, sizeof(msgfilter)) == -1)
195 #elif defined(ROUTE_MSGFILTER)
196 /* Convert the array into a bitmask. */
198 for (i = 0; i < __arraycount(msgfilter); i++)
199 msgfilter_mask |= ROUTE_FILTER(msgfilter[i]);
200 if (setsockopt(ctx->link_fd, PF_ROUTE, ROUTE_MSGFILTER,
201 &msgfilter_mask, sizeof(msgfilter_mask)) == -1)
204 #warning kernel does not support route message filtering
211 if_closesockets_os(struct dhcpcd_ctx *ctx)
215 priv = (struct priv *)ctx->priv;
216 if (priv->pf_inet6_fd != -1)
217 close(priv->pf_inet6_fd);
222 #if defined(SIOCALIFADDR) && defined(IFLR_ACTIVE) /*NetBSD */
224 if_ioctllink(struct dhcpcd_ctx *ctx, unsigned long req, void *data, size_t len)
230 if (ctx->options & DHCPCD_PRIVSEP)
231 return (int)ps_root_ioctllink(ctx, req, data, len);
236 s = socket(PF_LINK, SOCK_DGRAM, 0);
239 retval = ioctl(s, req, data, len);
246 if_setmac(struct interface *ifp, void *mac, uint8_t maclen)
249 if (ifp->hwlen != maclen) {
254 #if defined(SIOCALIFADDR) && defined(IFLR_ACTIVE) /*NetBSD */
255 struct if_laddrreq iflr = { .flags = IFLR_ACTIVE };
256 struct sockaddr_dl *sdl = satosdl(&iflr.addr);
259 strlcpy(iflr.iflr_name, ifp->name, sizeof(iflr.iflr_name));
260 sdl->sdl_family = AF_LINK;
261 sdl->sdl_len = sizeof(*sdl);
262 sdl->sdl_alen = maclen;
263 memcpy(LLADDR(sdl), mac, maclen);
264 retval = if_ioctllink(ifp->ctx, SIOCALIFADDR, &iflr, sizeof(iflr));
266 /* Try and remove the old address */
267 memcpy(LLADDR(sdl), ifp->hwaddr, ifp->hwlen);
268 if_ioctllink(ifp->ctx, SIOCDLIFADDR, &iflr, sizeof(iflr));
273 .ifr_addr.sa_family = AF_LINK,
274 .ifr_addr.sa_len = maclen,
277 strlcpy(ifr.ifr_name, ifp->name, sizeof(ifr.ifr_name));
278 memcpy(ifr.ifr_addr.sa_data, mac, maclen);
279 return if_ioctl(ifp->ctx, SIOCSIFLLADDR, &ifr, sizeof(ifr));
284 if_ignore1(const char *drvname)
286 const char * const *p;
288 for (p = ifnames_ignore; *p; p++) {
289 if (strcmp(*p, drvname) == 0)
296 if_ignore(struct dhcpcd_ctx *ctx, const char *ifname)
300 if (if_nametospec(ifname, &spec) != 0)
303 if (if_ignore1(spec.drvname))
308 #warning Fix SIOCGIFGROUP to use privsep to remove inet pledge requirement
310 struct ifgroupreq ifgr = { .ifgr_len = 0 };
314 /* Sadly it is possible to remove the device name
315 * from the interface groups, but hopefully this
316 * will be very unlikely.... */
318 strlcpy(ifgr.ifgr_name, ifname, sizeof(ifgr.ifgr_name));
319 if (ioctl(ctx->pf_inet_fd, SIOCGIFGROUP, &ifgr) == -1 ||
320 (ifgr.ifgr_groups = malloc(ifgr.ifgr_len)) == NULL ||
321 ioctl(ctx->pf_inet_fd, SIOCGIFGROUP, &ifgr) == -1)
327 for (ifg = ifgr.ifgr_groups, ifg_len = ifgr.ifgr_len;
328 ifg && ifg_len >= sizeof(*ifg);
329 ifg++, ifg_len -= sizeof(*ifg))
331 if (if_ignore1(ifg->ifgrq_group))
342 if_carrier(struct interface *ifp)
344 struct ifmediareq ifmr = { .ifm_status = 0 };
346 strlcpy(ifmr.ifm_name, ifp->name, sizeof(ifmr.ifm_name));
347 if (ioctl(ifp->ctx->pf_inet_fd, SIOCGIFMEDIA, &ifmr) == -1 ||
348 !(ifmr.ifm_status & IFM_AVALID))
351 return (ifmr.ifm_status & IFM_ACTIVE) ? LINK_UP : LINK_DOWN;
355 if_linkaddr(struct sockaddr_dl *sdl, const struct interface *ifp)
358 memset(sdl, 0, sizeof(*sdl));
359 sdl->sdl_family = AF_LINK;
360 sdl->sdl_len = sizeof(*sdl);
361 sdl->sdl_nlen = sdl->sdl_alen = sdl->sdl_slen = 0;
362 sdl->sdl_index = (unsigned short)ifp->index;
365 #if defined(SIOCG80211NWID) || defined(SIOCGETVLAN)
366 static int if_indirect_ioctl(struct dhcpcd_ctx *ctx,
367 const char *ifname, unsigned long cmd, void *data, size_t len)
369 struct ifreq ifr = { .ifr_flags = 0 };
371 #if defined(PRIVSEP) && defined(HAVE_PLEDGE)
373 return (int)ps_root_indirectioctl(ctx, cmd, ifname, data, len);
378 strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
380 return ioctl(ctx->pf_inet_fd, cmd, &ifr);
385 if_getssid1(struct dhcpcd_ctx *ctx, const char *ifname, void *ssid)
388 #if defined(SIOCG80211NWID)
389 struct ieee80211_nwid nwid;
390 #elif defined(IEEE80211_IOC_SSID)
391 struct ieee80211req ireq;
392 char nwid[IEEE80211_NWID_LEN];
395 #if defined(SIOCG80211NWID) /* NetBSD */
396 memset(&nwid, 0, sizeof(nwid));
397 if (if_indirect_ioctl(ctx, ifname, SIOCG80211NWID,
398 &nwid, sizeof(nwid)) == 0)
402 else if (nwid.i_len > IF_SSIDLEN)
406 memcpy(ssid, nwid.i_nwid, nwid.i_len);
409 #elif defined(IEEE80211_IOC_SSID) /* FreeBSD */
410 memset(&ireq, 0, sizeof(ireq));
411 strlcpy(ireq.i_name, ifname, sizeof(ireq.i_name));
412 ireq.i_type = IEEE80211_IOC_SSID;
414 memset(nwid, 0, sizeof(nwid));
416 if (ioctl(ctx->pf_inet_fd, SIOCG80211, &ireq) == 0) {
419 else if (ireq.i_len > IF_SSIDLEN)
423 memcpy(ssid, nwid, ireq.i_len);
434 if_getssid(struct interface *ifp)
438 r = if_getssid1(ifp->ctx, ifp->name, ifp->ssid);
440 ifp->ssid_len = (unsigned int)r;
443 ifp->ssid[ifp->ssid_len] = '\0';
448 * FreeBSD allows for Virtual Access Points
449 * We need to check if the interface is a Virtual Interface Master
450 * and if so, don't use it.
451 * This check is made by virtue of being a IEEE80211 device but
452 * returning the SSID gives an error.
455 if_vimaster(struct dhcpcd_ctx *ctx, const char *ifname)
458 struct ifmediareq ifmr = { .ifm_active = 0 };
460 strlcpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name));
461 r = ioctl(ctx->pf_inet_fd, SIOCGIFMEDIA, &ifmr);
464 if (ifmr.ifm_status & IFM_AVALID &&
465 IFM_TYPE(ifmr.ifm_active) == IFM_IEEE80211)
467 if (if_getssid1(ctx, ifname, NULL) == -1)
474 if_vlanid(const struct interface *ifp)
477 struct vlanreq vlr = { .vlr_tag = 0 };
479 if (if_indirect_ioctl(ifp->ctx, ifp->name, SIOCGETVLAN,
480 &vlr, sizeof(vlr)) != 0)
481 return 0; /* 0 means no VLANID */
483 #elif defined(SIOCGVNETID)
484 struct ifreq ifr = { .ifr_vnetid = 0 };
486 strlcpy(ifr.ifr_name, ifp->name, sizeof(ifr.ifr_name));
487 if (ioctl(ifp->ctx->pf_inet_fd, SIOCGVNETID, &ifr) != 0)
488 return 0; /* 0 means no VLANID */
489 return ifr.ifr_vnetid;
492 return 0; /* 0 means no VLANID */
497 get_addrs(int type, const void *data, size_t data_len,
498 const struct sockaddr **sa)
505 for (i = 0; i < RTAX_MAX; i++) {
506 if (type & (1 << i)) {
511 sa[i] = (const struct sockaddr *)cp;
512 RT_ADVANCE(cp, sa[i]);
520 static struct interface *
521 if_findsdl(struct dhcpcd_ctx *ctx, const struct sockaddr_dl *sdl)
525 return if_findindex(ctx->ifaces, sdl->sdl_index);
528 char ifname[IF_NAMESIZE];
530 memcpy(ifname, sdl->sdl_data, sdl->sdl_nlen);
531 ifname[sdl->sdl_nlen] = '\0';
532 return if_find(ctx->ifaces, ifname);
535 struct interface *ifp;
537 TAILQ_FOREACH(ifp, ctx->ifaces, next) {
538 if (ifp->hwlen == sdl->sdl_alen &&
540 sdl->sdl_data, sdl->sdl_alen) == 0)
549 static struct interface *
550 if_findsa(struct dhcpcd_ctx *ctx, const struct sockaddr *sa)
557 switch (sa->sa_family) {
560 const struct sockaddr_dl *sdl;
562 sdl = (const void *)sa;
563 return if_findsdl(ctx, sdl);
568 const struct sockaddr_in *sin;
569 struct ipv4_addr *ia;
571 sin = (const void *)sa;
572 if ((ia = ipv4_findmaskaddr(ctx, &sin->sin_addr)))
580 const struct sockaddr_in6 *sin;
582 struct ipv6_addr *ia;
584 sin = (const void *)sa;
585 scope = ipv6_getscope(sin);
587 return if_findindex(ctx->ifaces, scope);
588 if ((ia = ipv6_findmaskaddr(ctx, &sin->sin6_addr)))
594 errno = EAFNOSUPPORT;
603 if_copysa(struct sockaddr *dst, const struct sockaddr *src)
609 memcpy(dst, src, src->sa_len);
610 #if defined(INET6) && defined(__KAME__)
611 if (dst->sa_family == AF_INET6) {
612 struct in6_addr *in6;
614 in6 = &satosin6(dst)->sin6_addr;
615 if (IN6_IS_ADDR_LINKLOCAL(in6))
616 in6->s6_addr[2] = in6->s6_addr[3] = '\0';
622 if_route(unsigned char cmd, const struct rt *rt)
624 struct dhcpcd_ctx *ctx;
626 struct rt_msghdr *rtm = &rtmsg.hdr;
627 char *bp = rtmsg.buffer;
628 struct sockaddr_dl sdl;
632 assert(rt->rt_ifp != NULL);
633 assert(rt->rt_ifp->ctx != NULL);
634 ctx = rt->rt_ifp->ctx;
636 #define ADDSA(sa) do { \
637 memcpy(bp, (sa), (sa)->sa_len); \
638 bp += RT_ROUNDUP((sa)->sa_len); \
639 } while (0 /* CONSTCOND */)
641 memset(&rtmsg, 0, sizeof(rtmsg));
642 rtm->rtm_version = RTM_VERSION;
645 rtm->rtm_pid = getpid();
647 rtm->rtm_seq = ++ctx->seq;
648 rtm->rtm_flags = (int)rt->rt_flags;
649 rtm->rtm_addrs = RTA_DST;
652 rtm->rtm_flags |= RTF_PINNED;
655 gateway_unspec = sa_is_unspecified(&rt->rt_gateway);
657 if (cmd == RTM_ADD || cmd == RTM_CHANGE) {
658 bool netmask_bcast = sa_is_allones(&rt->rt_netmask);
660 rtm->rtm_flags |= RTF_UP;
661 rtm->rtm_addrs |= RTA_GATEWAY;
662 if (!(rtm->rtm_flags & RTF_REJECT) &&
663 !sa_is_loopback(&rt->rt_gateway))
665 rtm->rtm_index = (unsigned short)rt->rt_ifp->index;
667 * OpenBSD rejects the message for on-link routes.
668 * FreeBSD-12 kernel apparently panics.
669 * I can't replicate the panic, but better safe than sorry!
670 * https://roy.marples.name/archives/dhcpcd-discuss/0002286.html
672 * Neither OS currently allows IPv6 address sharing anyway, so let's
673 * try to encourage someone to fix that by logging a waring during compile.
675 #if defined(__FreeBSD__) || defined(__OpenBSD__)
676 #warning kernel does not allow IPv6 address sharing
677 if (!gateway_unspec || rt->rt_dest.sa_family!=AF_INET6)
679 rtm->rtm_addrs |= RTA_IFP;
680 if (!sa_is_unspecified(&rt->rt_ifa))
681 rtm->rtm_addrs |= RTA_IFA;
684 rtm->rtm_flags |= RTF_HOST;
685 /* Network routes are cloning or connected if supported.
686 * All other routes are static. */
687 if (gateway_unspec) {
689 rtm->rtm_flags |= RTF_CLONING;
692 rtm->rtm_flags |= RTF_CONNECTED;
695 rtm->rtm_priority = RTP_CONNECTED;
700 * We add a cloning network route for a single
701 * host. Traffic to the host will generate a
702 * cloned route and the hardware address will
704 * It might be more correct to use RTF_HOST
705 * instead of RTF_CLONING, and that does work,
706 * but some OS generate an arp warning
707 * diagnostic which we don't want to do.
709 rtm->rtm_flags &= ~RTF_HOST;
713 rtm->rtm_flags |= RTF_GATEWAY;
715 /* Emulate the kernel by marking address generated
716 * network routes non-static. */
717 if (!(rt->rt_dflags & RTDF_IFA_ROUTE))
718 rtm->rtm_flags |= RTF_STATIC;
720 if (rt->rt_mtu != 0) {
721 rtm->rtm_inits |= RTV_MTU;
722 rtm->rtm_rmx.rmx_mtu = rt->rt_mtu;
726 if (!(rtm->rtm_flags & RTF_HOST))
727 rtm->rtm_addrs |= RTA_NETMASK;
729 if_linkaddr(&sdl, rt->rt_ifp);
733 if (rtm->rtm_addrs & RTA_GATEWAY) {
735 ADDSA((struct sockaddr *)&sdl);
739 if_copysa(&gateway.sa, &rt->rt_gateway);
741 if (gateway.sa.sa_family == AF_INET6)
742 ipv6_setscope(&gateway.sin6, rt->rt_ifp->index);
748 if (rtm->rtm_addrs & RTA_NETMASK)
749 ADDSA(&rt->rt_netmask);
751 if (rtm->rtm_addrs & RTA_IFP)
752 ADDSA((struct sockaddr *)&sdl);
754 if (rtm->rtm_addrs & RTA_IFA)
759 rtm->rtm_msglen = (unsigned short)(bp - (char *)rtm);
762 if (ctx->options & DHCPCD_PRIVSEP) {
763 if (ps_root_route(ctx, rtm, rtm->rtm_msglen) == -1)
768 if (write(ctx->link_fd, rtm, rtm->rtm_msglen) == -1)
774 if_realroute(const struct rt_msghdr *rtm)
778 if (rtm->rtm_flags & RTF_CLONED)
782 if (rtm->rtm_flags & RTF_WASCLONED)
786 if (rtm->rtm_flags & RTF_LOCAL)
790 if (rtm->rtm_flags & RTF_BROADCAST)
797 if_copyrt(struct dhcpcd_ctx *ctx, struct rt *rt, const struct rt_msghdr *rtm)
799 const struct sockaddr *rti_info[RTAX_MAX];
801 if (!(rtm->rtm_addrs & RTA_DST)) {
805 if (rtm->rtm_type != RTM_MISS && !(rtm->rtm_addrs & RTA_GATEWAY)) {
810 if (get_addrs(rtm->rtm_addrs, (const char *)rtm + sizeof(*rtm),
811 rtm->rtm_msglen - sizeof(*rtm), rti_info) == -1)
813 memset(rt, 0, sizeof(*rt));
815 rt->rt_flags = (unsigned int)rtm->rtm_flags;
816 if_copysa(&rt->rt_dest, rti_info[RTAX_DST]);
817 if (rtm->rtm_addrs & RTA_NETMASK) {
818 if_copysa(&rt->rt_netmask, rti_info[RTAX_NETMASK]);
819 if (rt->rt_netmask.sa_family == 255) /* Why? */
820 rt->rt_netmask.sa_family = rt->rt_dest.sa_family;
823 /* dhcpcd likes an unspecified gateway to indicate via the link.
824 * However we need to know if gateway was a link with an address. */
825 if (rtm->rtm_addrs & RTA_GATEWAY) {
826 if (rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) {
827 const struct sockaddr_dl *sdl;
829 sdl = (const struct sockaddr_dl*)
830 (const void *)rti_info[RTAX_GATEWAY];
831 if (sdl->sdl_alen != 0)
832 rt->rt_dflags |= RTDF_GATELINK;
833 } else if (rtm->rtm_flags & RTF_GATEWAY)
834 if_copysa(&rt->rt_gateway, rti_info[RTAX_GATEWAY]);
837 if (rtm->rtm_addrs & RTA_IFA)
838 if_copysa(&rt->rt_ifa, rti_info[RTAX_IFA]);
840 rt->rt_mtu = (unsigned int)rtm->rtm_rmx.rmx_mtu;
843 rt->rt_ifp = if_findindex(ctx->ifaces, rtm->rtm_index);
844 else if (rtm->rtm_addrs & RTA_IFP)
845 rt->rt_ifp = if_findsa(ctx, rti_info[RTAX_IFP]);
846 else if (rtm->rtm_addrs & RTA_GATEWAY)
847 rt->rt_ifp = if_findsa(ctx, rti_info[RTAX_GATEWAY]);
849 rt->rt_ifp = if_findsa(ctx, rti_info[RTAX_DST]);
851 if (rt->rt_ifp == NULL && rtm->rtm_type == RTM_MISS)
852 rt->rt_ifp = if_find(ctx->ifaces, "lo0");
854 if (rt->rt_ifp == NULL) {
862 if_initrt(struct dhcpcd_ctx *ctx, rb_tree_t *kroutes, int af)
864 struct rt_msghdr *rtm;
874 mib[4] = NET_RT_DUMP;
877 if (sysctl(mib, 6, NULL, &needed, NULL, 0) == -1)
881 if ((buf = malloc(needed)) == NULL)
883 if (sysctl(mib, 6, buf, &needed, NULL, 0) == -1) {
889 for (p = buf; p < end; p += rtm->rtm_msglen) {
891 if (p + rtm->rtm_msglen >= end) {
895 if (!if_realroute(rtm))
897 if (if_copyrt(ctx, &rt, rtm) != 0)
899 if ((rtn = rt_new(rt.rt_ifp)) == NULL) {
903 memcpy(rtn, &rt, sizeof(*rtn));
904 if (rb_tree_insert_node(kroutes, rtn) != rtn)
908 return p == end ? 0 : -1;
913 if_address(unsigned char cmd, const struct ipv4_addr *ia)
916 struct in_aliasreq ifra;
917 struct dhcpcd_ctx *ctx = ia->iface->ctx;
919 memset(&ifra, 0, sizeof(ifra));
920 strlcpy(ifra.ifra_name, ia->iface->name, sizeof(ifra.ifra_name));
922 #define ADDADDR(var, addr) do { \
923 (var)->sin_family = AF_INET; \
924 (var)->sin_len = sizeof(*(var)); \
925 (var)->sin_addr = *(addr); \
926 } while (/*CONSTCOND*/0)
927 ADDADDR(&ifra.ifra_addr, &ia->addr);
928 ADDADDR(&ifra.ifra_mask, &ia->mask);
929 if (cmd == RTM_NEWADDR && ia->brd.s_addr != INADDR_ANY)
930 ADDADDR(&ifra.ifra_broadaddr, &ia->brd);
934 cmd == RTM_DELADDR ? SIOCDIFADDR : SIOCAIFADDR, &ifra,sizeof(ifra));
938 #if !(defined(HAVE_IFADDRS_ADDRFLAGS) && defined(HAVE_IFAM_ADDRFLAGS))
940 if_addrflags(const struct interface *ifp, const struct in_addr *addr,
941 __unused const char *alias)
943 #ifdef SIOCGIFAFLAG_IN
945 struct sockaddr_in *sin;
947 memset(&ifr, 0, sizeof(ifr));
948 strlcpy(ifr.ifr_name, ifp->name, sizeof(ifr.ifr_name));
949 sin = (void *)&ifr.ifr_addr;
950 sin->sin_family = AF_INET;
951 sin->sin_addr = *addr;
952 if (ioctl(ifp->ctx->pf_inet_fd, SIOCGIFAFLAG_IN, &ifr) == -1)
954 return ifr.ifr_addrflags;
966 if_ioctl6(struct dhcpcd_ctx *ctx, unsigned long req, void *data, size_t len)
971 if (ctx->options & DHCPCD_PRIVSEP)
972 return (int)ps_root_ioctl6(ctx, req, data, len);
976 return ioctl(priv->pf_inet6_fd, req, data, len);
980 if_address6(unsigned char cmd, const struct ipv6_addr *ia)
982 struct in6_aliasreq ifa = { .ifra_flags = 0 };
983 struct in6_addr mask;
984 struct dhcpcd_ctx *ctx = ia->iface->ctx;
986 strlcpy(ifa.ifra_name, ia->iface->name, sizeof(ifa.ifra_name));
987 #if defined(__FreeBSD__) || defined(__DragonFly__)
988 /* This is a bug - the kernel should work this out. */
989 if (ia->addr_flags & IN6_IFF_TENTATIVE)
990 ifa.ifra_flags |= IN6_IFF_TENTATIVE;
992 // #if (defined(__NetBSD__) && __NetBSD_Version__ >= 999005700) ||
993 #if (defined(__OpenBSD__) && OpenBSD >= 201605)
994 if (ia->flags & IPV6_AF_AUTOCONF)
995 ifa.ifra_flags |= IN6_IFF_AUTOCONF;
997 #ifdef IPV6_MANAGETEMPADDR
998 if (ia->flags & IPV6_AF_TEMPORARY)
999 ifa.ifra_flags |= IN6_IFF_TEMPORARY;
1002 #define ADDADDR(v, addr) { \
1003 (v)->sin6_family = AF_INET6; \
1004 (v)->sin6_len = sizeof(*v); \
1005 (v)->sin6_addr = *(addr); \
1008 ADDADDR(&ifa.ifra_addr, &ia->addr);
1009 ipv6_setscope(&ifa.ifra_addr, ia->iface->index);
1010 ipv6_mask(&mask, ia->prefix_len);
1011 ADDADDR(&ifa.ifra_prefixmask, &mask);
1016 * Every BSD kernel wants to add the prefix of the address to it's
1017 * list of RA received prefixes.
1018 * THIS IS WRONG because there (as the comments in the kernel state)
1019 * is no API for managing prefix lifetime and the kernel should not
1020 * pretend it's from a RA either.
1022 * The issue is that the very first assigned prefix will inherit the
1023 * lifetime of the address, but any subsequent alteration of the
1024 * address OR it's lifetime will not affect the prefix lifetime.
1025 * As such, we cannot stop the prefix from timing out and then
1026 * constantly removing the prefix route dhcpcd is capable of adding
1029 * What we can do to mitigate the issue is to add the address with
1030 * infinite lifetimes, so the prefix route will never time out.
1031 * Once done, we can then set lifetimes on the address and all is good.
1032 * The downside of this approach is that we need to manually remove
1033 * the kernel route because it has no lifetime, but this is OK as
1034 * dhcpcd will handle this too.
1036 * This issue is discussed on the NetBSD mailing lists here:
1037 * http://mail-index.netbsd.org/tech-net/2016/08/05/msg006044.html
1039 * Fixed in NetBSD-7.99.36
1040 * NOT fixed in FreeBSD - bug 195197
1041 * Fixed in OpenBSD-5.9
1044 #if !((defined(__NetBSD_Version__) && __NetBSD_Version__ >= 799003600) || \
1045 (defined(__OpenBSD__) && OpenBSD >= 201605))
1046 if (cmd == RTM_NEWADDR && !(ia->flags & IPV6_AF_ADDED)) {
1047 ifa.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
1048 ifa.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
1049 (void)if_ioctl6(ctx, SIOCAIFADDR_IN6, &ifa, sizeof(ifa));
1053 #if defined(__OpenBSD__) && OpenBSD <= 201705
1054 /* BUT OpenBSD older than 6.2 does not reset the address lifetime
1055 * for subsequent calls...
1056 * Luckily dhcpcd will remove the lease when it expires so
1057 * just set an infinite lifetime, unless a temporary address. */
1058 if (ifa.ifra_flags & IN6_IFF_PRIVACY) {
1059 ifa.ifra_lifetime.ia6t_vltime = ia->prefix_vltime;
1060 ifa.ifra_lifetime.ia6t_pltime = ia->prefix_pltime;
1062 ifa.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
1063 ifa.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
1066 ifa.ifra_lifetime.ia6t_vltime = ia->prefix_vltime;
1067 ifa.ifra_lifetime.ia6t_pltime = ia->prefix_pltime;
1070 return if_ioctl6(ctx,
1071 cmd == RTM_DELADDR ? SIOCDIFADDR_IN6 : SIOCAIFADDR_IN6,
1076 if_addrflags6(const struct interface *ifp, const struct in6_addr *addr,
1077 __unused const char *alias)
1080 struct in6_ifreq ifr6;
1083 memset(&ifr6, 0, sizeof(ifr6));
1084 strlcpy(ifr6.ifr_name, ifp->name, sizeof(ifr6.ifr_name));
1085 ifr6.ifr_addr.sin6_family = AF_INET6;
1086 ifr6.ifr_addr.sin6_addr = *addr;
1087 ipv6_setscope(&ifr6.ifr_addr, ifp->index);
1088 priv = (struct priv *)ifp->ctx->priv;
1089 if (ioctl(priv->pf_inet6_fd, SIOCGIFAFLAG_IN6, &ifr6) != -1)
1090 flags = ifr6.ifr_ifru.ifru_flags6;
1097 if_getlifetime6(struct ipv6_addr *ia)
1099 struct in6_ifreq ifr6;
1101 struct in6_addrlifetime *lifetime;
1104 memset(&ifr6, 0, sizeof(ifr6));
1105 strlcpy(ifr6.ifr_name, ia->iface->name, sizeof(ifr6.ifr_name));
1106 ifr6.ifr_addr.sin6_family = AF_INET6;
1107 ifr6.ifr_addr.sin6_addr = ia->addr;
1108 ipv6_setscope(&ifr6.ifr_addr, ia->iface->index);
1109 priv = (struct priv *)ia->iface->ctx->priv;
1110 if (ioctl(priv->pf_inet6_fd, SIOCGIFALIFETIME_IN6, &ifr6) == -1)
1112 clock_gettime(CLOCK_MONOTONIC, &ia->created);
1114 #if defined(__FreeBSD__) || defined(__DragonFly__)
1115 t = ia->created.tv_sec;
1120 lifetime = &ifr6.ifr_ifru.ifru_lifetime;
1121 if (lifetime->ia6t_preferred)
1122 ia->prefix_pltime = (uint32_t)(lifetime->ia6t_preferred -
1123 MIN(t, lifetime->ia6t_preferred));
1125 ia->prefix_pltime = ND6_INFINITE_LIFETIME;
1126 if (lifetime->ia6t_expire) {
1127 ia->prefix_vltime = (uint32_t)(lifetime->ia6t_expire -
1128 MIN(t, lifetime->ia6t_expire));
1129 /* Calculate the created time */
1130 ia->created.tv_sec -= lifetime->ia6t_vltime - ia->prefix_vltime;
1132 ia->prefix_vltime = ND6_INFINITE_LIFETIME;
1138 if_announce(struct dhcpcd_ctx *ctx, const struct if_announcemsghdr *ifan)
1141 if (ifan->ifan_msglen < sizeof(*ifan)) {
1146 switch(ifan->ifan_what) {
1148 return dhcpcd_handleinterface(ctx, 1, ifan->ifan_name);
1149 case IFAN_DEPARTURE:
1150 return dhcpcd_handleinterface(ctx, -1, ifan->ifan_name);
1157 if_ifinfo(struct dhcpcd_ctx *ctx, const struct if_msghdr *ifm)
1159 struct interface *ifp;
1162 if (ifm->ifm_msglen < sizeof(*ifm)) {
1167 if ((ifp = if_findindex(ctx->ifaces, ifm->ifm_index)) == NULL)
1170 switch (ifm->ifm_data.ifi_link_state) {
1171 case LINK_STATE_UNKNOWN:
1172 link_state = LINK_UNKNOWN;
1174 #ifdef LINK_STATE_FULL_DUPLEX
1175 case LINK_STATE_HALF_DUPLEX: /* FALLTHROUGH */
1176 case LINK_STATE_FULL_DUPLEX: /* FALLTHROUGH */
1179 link_state = LINK_UP;
1182 link_state = LINK_DOWN;
1186 dhcpcd_handlecarrier(ctx, link_state,
1187 (unsigned int)ifm->ifm_flags, ifp->name);
1192 if_rtm(struct dhcpcd_ctx *ctx, const struct rt_msghdr *rtm)
1196 if (rtm->rtm_msglen < sizeof(*rtm)) {
1201 /* Ignore errors. */
1202 if (rtm->rtm_errno != 0)
1205 /* Ignore messages from ourself. */
1207 if (ctx->ps_root_pid != 0) {
1208 if (rtm->rtm_pid == ctx->ps_root_pid)
1213 if (if_copyrt(ctx, &rt, rtm) == -1)
1214 return errno == ENOTSUP ? 0 : -1;
1218 * BSD announces host routes.
1219 * As such, we should be notified of reachability by its
1220 * existance with a hardware address.
1221 * Ensure we don't call this for a newly incomplete state.
1223 if (rt.rt_dest.sa_family == AF_INET6 &&
1224 (rt.rt_flags & RTF_HOST || rtm->rtm_type == RTM_MISS) &&
1225 !(rtm->rtm_type == RTM_ADD && !(rt.rt_dflags & RTDF_GATELINK)))
1229 reachable = (rtm->rtm_type == RTM_ADD ||
1230 rtm->rtm_type == RTM_CHANGE) &&
1231 rt.rt_dflags & RTDF_GATELINK;
1232 ipv6nd_neighbour(ctx, &rt.rt_ss_dest.sin6.sin6_addr, reachable);
1236 if (rtm->rtm_type != RTM_MISS && if_realroute(rtm))
1237 rt_recvrt(rtm->rtm_type, &rt, rtm->rtm_pid);
1242 if_ifa(struct dhcpcd_ctx *ctx, const struct ifa_msghdr *ifam)
1244 struct interface *ifp;
1245 const struct sockaddr *rti_info[RTAX_MAX];
1249 if (ifam->ifam_msglen < sizeof(*ifam)) {
1254 #ifdef HAVE_IFAM_PID
1255 /* Ignore address deletions from ourself.
1256 * We need to process address flag changes though. */
1257 if (ifam->ifam_type == RTM_DELADDR) {
1259 if (ctx->ps_root_pid != 0) {
1260 if (ifam->ifam_pid == ctx->ps_root_pid)
1264 /* address management is done via ioctl,
1265 * so SO_USELOOPBACK has no effect,
1266 * so we do need to check the pid. */
1267 if (ifam->ifam_pid == getpid())
1270 pid = ifam->ifam_pid;
1275 if (~ifam->ifam_addrs & RTA_IFA)
1277 if ((ifp = if_findindex(ctx->ifaces, ifam->ifam_index)) == NULL)
1280 if (get_addrs(ifam->ifam_addrs, (const char *)ifam + sizeof(*ifam),
1281 ifam->ifam_msglen - sizeof(*ifam), rti_info) == -1)
1284 switch (rti_info[RTAX_IFA]->sa_family) {
1287 struct sockaddr_dl sdl;
1290 if (ifam->ifam_type != RTM_CHGADDR)
1293 if (ifam->ifam_type != RTM_NEWADDR)
1296 memcpy(&sdl, rti_info[RTAX_IFA], rti_info[RTAX_IFA]->sa_len);
1297 dhcpcd_handlehwaddr(ifp, ifp->hwtype,
1298 CLLADDR(&sdl), sdl.sdl_alen);
1303 case 255: /* FIXME: Why 255? */
1305 const struct sockaddr_in *sin;
1306 struct in_addr addr, mask, bcast;
1308 sin = (const void *)rti_info[RTAX_IFA];
1309 addr.s_addr = sin != NULL && sin->sin_family == AF_INET ?
1310 sin->sin_addr.s_addr : INADDR_ANY;
1311 sin = (const void *)rti_info[RTAX_NETMASK];
1312 mask.s_addr = sin != NULL && sin->sin_family == AF_INET ?
1313 sin->sin_addr.s_addr : INADDR_ANY;
1314 sin = (const void *)rti_info[RTAX_BRD];
1315 bcast.s_addr = sin != NULL && sin->sin_family == AF_INET ?
1316 sin->sin_addr.s_addr : INADDR_ANY;
1319 * NetBSD-7 and older send an invalid broadcast address.
1320 * So we need to query the actual address to get
1322 * We can also use this to test if the address
1323 * has really been added or deleted.
1326 struct in_aliasreq ifra;
1328 memset(&ifra, 0, sizeof(ifra));
1329 strlcpy(ifra.ifra_name, ifp->name, sizeof(ifra.ifra_name));
1330 ifra.ifra_addr.sin_family = AF_INET;
1331 ifra.ifra_addr.sin_len = sizeof(ifra.ifra_addr);
1332 ifra.ifra_addr.sin_addr = addr;
1333 if (ioctl(ctx->pf_inet_fd, SIOCGIFALIAS, &ifra) == -1) {
1334 if (errno != ENXIO && errno != EADDRNOTAVAIL)
1335 logerr("%s: SIOCGIFALIAS", __func__);
1336 if (ifam->ifam_type != RTM_DELADDR)
1339 if (ifam->ifam_type == RTM_DELADDR)
1341 #if defined(__NetBSD_Version__) && __NetBSD_Version__ < 800000000
1342 bcast = ifra.ifra_broadaddr.sin_addr;
1346 #warning No SIOCGIFALIAS support
1348 * No SIOCGIFALIAS? That sucks!
1349 * This makes this call very heavy weight, but we
1350 * really need to know if the message is late or not.
1352 const struct sockaddr *sa;
1353 struct ifaddrs *ifaddrs = NULL, *ifa;
1355 sa = rti_info[RTAX_IFA];
1356 getifaddrs(&ifaddrs);
1357 for (ifa = ifaddrs; ifa; ifa = ifa->ifa_next) {
1358 if (ifa->ifa_addr == NULL)
1360 if (sa_cmp(ifa->ifa_addr, sa) == 0 &&
1361 strcmp(ifa->ifa_name, ifp->name) == 0)
1364 freeifaddrs(ifaddrs);
1365 if (ifam->ifam_type == RTM_DELADDR) {
1374 #ifdef HAVE_IFAM_ADDRFLAGS
1375 flags = ifam->ifam_addrflags;
1380 ipv4_handleifa(ctx, ifam->ifam_type, NULL, ifp->name,
1381 &addr, &mask, &bcast, flags, pid);
1388 struct in6_addr addr6, mask6;
1389 const struct sockaddr_in6 *sin6;
1391 sin6 = (const void *)rti_info[RTAX_IFA];
1392 addr6 = sin6->sin6_addr;
1393 sin6 = (const void *)rti_info[RTAX_NETMASK];
1394 mask6 = sin6->sin6_addr;
1397 * If the address was deleted, lets check if it's
1398 * a late message and it still exists (maybe modified).
1399 * If so, ignore it as deleting an address causes
1400 * dhcpcd to drop any lease to which it belongs.
1401 * Also check an added address was really added.
1403 flags = if_addrflags6(ifp, &addr6, NULL);
1405 if (errno != ENXIO && errno != EADDRNOTAVAIL)
1406 logerr("%s: if_addrflags6", __func__);
1407 if (ifam->ifam_type != RTM_DELADDR)
1410 } else if (ifam->ifam_type == RTM_DELADDR)
1414 if (IN6_IS_ADDR_LINKLOCAL(&addr6))
1415 /* Remove the scope from the address */
1416 addr6.s6_addr[2] = addr6.s6_addr[3] = '\0';
1419 ipv6_handleifa(ctx, ifam->ifam_type, NULL,
1420 ifp->name, &addr6, ipv6_prefixlen(&mask6), flags, pid);
1430 if_dispatch(struct dhcpcd_ctx *ctx, const struct rt_msghdr *rtm)
1433 if (rtm->rtm_version != RTM_VERSION)
1436 switch(rtm->rtm_type) {
1437 #ifdef RTM_IFANNOUNCE
1438 case RTM_IFANNOUNCE:
1439 return if_announce(ctx, (const void *)rtm);
1442 return if_ifinfo(ctx, (const void *)rtm);
1443 case RTM_ADD: /* FALLTHROUGH */
1444 case RTM_CHANGE: /* FALLTHROUGH */
1445 case RTM_DELETE: /* FALLTHROUGH */
1447 return if_rtm(ctx, (const void *)rtm);
1449 case RTM_CHGADDR: /* FALLTHROUGH */
1451 case RTM_DELADDR: /* FALLTHROUGH */
1453 return if_ifa(ctx, (const void *)rtm);
1456 dhcpcd_linkoverflow(ctx);
1457 #elif !defined(SO_RERROR)
1458 #warning cannot detect route socket overflow within kernel
1466 if_missfilter0(struct dhcpcd_ctx *ctx, struct interface *ifp,
1467 struct sockaddr *sa)
1469 size_t salen = (size_t)RT_ROUNDUP(sa->sa_len);
1470 size_t newlen = ctx->rt_missfilterlen + salen;
1471 size_t diff = salen - (sa->sa_len);
1474 if (ctx->rt_missfiltersize < newlen) {
1475 void *n = realloc(ctx->rt_missfilter, newlen);
1478 ctx->rt_missfilter = n;
1479 ctx->rt_missfiltersize = newlen;
1483 if (sa->sa_family == AF_INET6)
1484 ipv6_setscope(satosin6(sa), ifp->index);
1489 cp = ctx->rt_missfilter + ctx->rt_missfilterlen;
1490 memcpy(cp, sa, sa->sa_len);
1492 memset(cp + sa->sa_len, 0, diff);
1493 ctx->rt_missfilterlen += salen;
1496 if (sa->sa_family == AF_INET6)
1497 ipv6_setscope(satosin6(sa), 0);
1504 if_missfilter(struct interface *ifp, struct sockaddr *sa)
1507 return if_missfilter0(ifp->ctx, ifp, sa);
1511 if_missfilter_apply(struct dhcpcd_ctx *ctx)
1513 #ifdef RO_MISSFILTER
1514 if (ctx->rt_missfilterlen == 0) {
1515 struct sockaddr sa = {
1516 .sa_family = AF_UNSPEC,
1517 .sa_len = sizeof(sa),
1520 if (if_missfilter0(ctx, NULL, &sa) == -1)
1524 return setsockopt(ctx->link_fd, PF_ROUTE, RO_MISSFILTER,
1525 ctx->rt_missfilter, (socklen_t)ctx->rt_missfilterlen);
1527 #warning kernel does not support RTM_MISS DST filtering
1534 __CTASSERT(offsetof(struct rt_msghdr, rtm_msglen) == 0);
1536 if_handlelink(struct dhcpcd_ctx *ctx)
1541 len = read(ctx->link_fd, &rtm, sizeof(rtm));
1546 if ((size_t)len < sizeof(rtm.hdr.rtm_msglen) ||
1547 len != rtm.hdr.rtm_msglen)
1553 * Coverity thinks that the data could be tainted from here.
1554 * I have no idea how because the length of the data we read
1555 * is guarded by len and checked to match rtm_msglen.
1556 * The issue seems to be related to extracting the addresses
1557 * at the end of the header, but seems to have no issues with the
1558 * equivalent call in if_initrt.
1560 /* coverity[tainted_data] */
1561 return if_dispatch(ctx, &rtm.hdr);
1564 #ifndef SYS_NMLN /* OSX */
1565 # define SYS_NMLN __SYS_NAMELEN
1567 #ifndef HW_MACHINE_ARCH
1568 # ifdef HW_MODEL /* OpenBSD */
1569 # define HW_MACHINE_ARCH HW_MODEL
1573 if_machinearch(char *str, size_t len)
1575 int mib[2] = { CTL_HW, HW_MACHINE_ARCH };
1577 return sysctl(mib, sizeof(mib) / sizeof(mib[0]), str, &len, NULL, 0);
1581 #if (defined(IPV6CTL_ACCEPT_RTADV) && !defined(ND6_IFF_ACCEPT_RTADV)) || \
1582 defined(IPV6CTL_FORWARDING)
1583 #define get_inet6_sysctl(code) inet6_sysctl(code, 0, 0)
1584 #define set_inet6_sysctl(code, val) inet6_sysctl(code, val, 1)
1586 inet6_sysctl(int code, int val, int action)
1588 int mib[] = { CTL_NET, PF_INET6, IPPROTO_IPV6, 0 };
1594 if (sysctl(mib, sizeof(mib)/sizeof(mib[0]),
1595 NULL, 0, &val, size) == -1)
1599 if (sysctl(mib, sizeof(mib)/sizeof(mib[0]), &val, &size, NULL, 0) == -1)
1606 if_applyra(const struct ra *rap)
1608 #ifdef SIOCSIFINFO_IN6
1609 struct in6_ndireq nd = { .ndi.chlim = 0 };
1610 struct dhcpcd_ctx *ctx = rap->iface->ctx;
1613 strlcpy(nd.ifname, rap->iface->name, sizeof(nd.ifname));
1615 #ifdef IPV6CTL_ACCEPT_RTADV
1616 struct priv *priv = ctx->priv;
1619 * NetBSD changed SIOCSIFINFO_IN6 to NOT set flags when kernel
1620 * RA was removed, however both FreeBSD and DragonFlyBSD still do.
1621 * linkmtu was also removed.
1622 * Hopefully this guard will still work if either remove kernel RA.
1624 if (ioctl(priv->pf_inet6_fd, SIOCGIFINFO_IN6, &nd, sizeof(nd)) == -1)
1627 nd.ndi.linkmtu = rap->mtu;
1630 nd.ndi.chlim = rap->hoplimit;
1631 nd.ndi.retrans = rap->retrans;
1632 nd.ndi.basereachable = rap->reachable;
1633 error = if_ioctl6(ctx, SIOCSIFINFO_IN6, &nd, sizeof(nd));
1634 #ifdef IPV6CTL_ACCEPT_RTADV
1635 if (error == -1 && errno == EINVAL) {
1637 * Very likely that this is caused by a dodgy MTU
1638 * setting specific to the interface.
1639 * Let's set it to "unspecified" and try again.
1640 * Doesn't really matter as we fix the MTU against the
1641 * routes we add as not all OS support SIOCSIFINFO_IN6.
1644 error = if_ioctl6(ctx, SIOCSIFINFO_IN6, &nd, sizeof(nd));
1649 #warning OS does not allow setting of RA bits hoplimit, retrans or reachable
1655 #ifndef IPV6CTL_FORWARDING
1656 #define get_inet6_sysctlbyname(code) inet6_sysctlbyname(code, 0, 0)
1657 #define set_inet6_sysctlbyname(code, val) inet6_sysctlbyname(code, val, 1)
1659 inet6_sysctlbyname(const char *name, int val, int action)
1665 if (sysctlbyname(name, NULL, 0, &val, size) == -1)
1669 if (sysctlbyname(name, &val, &size, NULL, 0) == -1)
1676 ip6_forwarding(__unused const char *ifname)
1680 #ifdef IPV6CTL_FORWARDING
1681 val = get_inet6_sysctl(IPV6CTL_FORWARDING);
1683 val = get_inet6_sysctlbyname("net.inet6.ip6.forwarding");
1685 return val < 0 ? 0 : val;
1688 #ifdef SIOCIFAFATTACH
1690 if_af_attach(const struct interface *ifp, int af)
1692 struct if_afreq ifar;
1694 strlcpy(ifar.ifar_name, ifp->name, sizeof(ifar.ifar_name));
1696 return if_ioctl6(ifp->ctx, SIOCIFAFATTACH, &ifar, sizeof(ifar));
1700 #ifdef SIOCGIFXFLAGS
1702 if_set_ifxflags(const struct interface *ifp)
1706 struct priv *priv = ifp->ctx->priv;
1708 strlcpy(ifr.ifr_name, ifp->name, sizeof(ifr.ifr_name));
1709 if (ioctl(priv->pf_inet6_fd, SIOCGIFXFLAGS, &ifr) == -1)
1711 flags = ifr.ifr_flags;
1713 flags &= ~IFXF_NOINET6;
1716 * If not doing autoconf, don't disable the kernel from doing it.
1717 * If we need to, we should have another option actively disable it.
1719 * OpenBSD moved from kernel based SLAAC to userland via slaacd(8).
1720 * It has a similar featureset to dhcpcd such as stable private
1721 * addresses, but lacks the ability to handle DNS inside the RA
1722 * which is a serious shortfall in this day and age.
1723 * Appease their user base by working alongside slaacd(8) if
1724 * dhcpcd is instructed not to do auto configuration of addresses.
1726 #if defined(ND6_IFF_ACCEPT_RTADV)
1727 #define BSD_AUTOCONF DHCPCD_IPV6RS
1729 #define BSD_AUTOCONF DHCPCD_IPV6RA_AUTOCONF
1731 if (ifp->options->options & BSD_AUTOCONF)
1732 flags &= ~IFXF_AUTOCONF6;
1733 if (ifr.ifr_flags == flags)
1735 ifr.ifr_flags = flags;
1736 return if_ioctl6(ifp->ctx, SIOCSIFXFLAGS, &ifr, sizeof(ifr));
1740 /* OpenBSD removed ND6 flags entirely, so we need to check for their
1742 #if defined(ND6_IFF_AUTO_LINKLOCAL) || \
1743 defined(ND6_IFF_PERFORMNUD) || \
1744 defined(ND6_IFF_ACCEPT_RTADV) || \
1745 defined(ND6_IFF_OVERRIDE_RTADV) || \
1746 defined(ND6_IFF_IFDISABLED)
1747 #define ND6_NDI_FLAGS
1751 if_disable_rtadv(void)
1753 #if defined(IPV6CTL_ACCEPT_RTADV) && !defined(ND6_IFF_ACCEPT_RTADV)
1754 int ra = get_inet6_sysctl(IPV6CTL_ACCEPT_RTADV);
1757 if (errno != ENOENT)
1758 logerr("IPV6CTL_ACCEPT_RTADV");
1760 if (set_inet6_sysctl(IPV6CTL_ACCEPT_RTADV, 0) == -1)
1761 logerr("IPV6CTL_ACCEPT_RTADV");
1767 if_setup_inet6(const struct interface *ifp)
1771 #ifdef ND6_NDI_FLAGS
1772 struct in6_ndireq nd;
1776 priv = (struct priv *)ifp->ctx->priv;
1777 s = priv->pf_inet6_fd;
1779 #ifdef ND6_NDI_FLAGS
1780 memset(&nd, 0, sizeof(nd));
1781 strlcpy(nd.ifname, ifp->name, sizeof(nd.ifname));
1782 if (ioctl(s, SIOCGIFINFO_IN6, &nd) == -1)
1783 logerr("%s: SIOCGIFINFO_FLAGS", ifp->name);
1784 flags = (int)nd.ndi.flags;
1787 #ifdef ND6_IFF_AUTO_LINKLOCAL
1788 /* Unlike the kernel, dhcpcd make make a stable private address. */
1789 flags &= ~ND6_IFF_AUTO_LINKLOCAL;
1792 #ifdef ND6_IFF_PERFORMNUD
1793 /* NUD is kind of essential. */
1794 flags |= ND6_IFF_PERFORMNUD;
1797 #ifdef ND6_IFF_IFDISABLED
1798 /* Ensure the interface is not disabled. */
1799 flags &= ~ND6_IFF_IFDISABLED;
1803 * If not doing autoconf, don't disable the kernel from doing it.
1804 * If we need to, we should have another option actively disable it.
1806 #ifdef ND6_IFF_ACCEPT_RTADV
1807 if (ifp->options->options & DHCPCD_IPV6RS)
1808 flags &= ~ND6_IFF_ACCEPT_RTADV;
1809 #ifdef ND6_IFF_OVERRIDE_RTADV
1810 if (ifp->options->options & DHCPCD_IPV6RS)
1811 flags |= ND6_IFF_OVERRIDE_RTADV;
1815 #ifdef ND6_NDI_FLAGS
1816 if (nd.ndi.flags != (uint32_t)flags) {
1817 nd.ndi.flags = (uint32_t)flags;
1818 if (if_ioctl6(ifp->ctx, SIOCSIFINFO_FLAGS,
1819 &nd, sizeof(nd)) == -1)
1820 logerr("%s: SIOCSIFINFO_FLAGS", ifp->name);
1824 /* Enabling IPv6 by whatever means must be the
1825 * last action undertaken to ensure kernel RS and
1826 * LLADDR auto configuration are disabled where applicable. */
1827 #ifdef SIOCIFAFATTACH
1828 if (if_af_attach(ifp, AF_INET6) == -1)
1829 logerr("%s: if_af_attach", ifp->name);
1832 #ifdef SIOCGIFXFLAGS
1833 if (if_set_ifxflags(ifp) == -1)
1834 logerr("%s: set_ifxflags", ifp->name);
1837 #ifdef SIOCSRTRFLUSH_IN6
1838 /* Flush the kernel knowledge of advertised routers
1839 * and prefixes so the kernel does not expire prefixes
1840 * and default routes we are trying to own. */
1841 if (ifp->options->options & DHCPCD_IPV6RS) {
1842 struct in6_ifreq ifr;
1844 memset(&ifr, 0, sizeof(ifr));
1845 strlcpy(ifr.ifr_name, ifp->name, sizeof(ifr.ifr_name));
1846 if (if_ioctl6(ifp->ctx, SIOCSRTRFLUSH_IN6,
1847 &ifr, sizeof(ifr)) == -1 &&
1848 errno != ENOTSUP && errno != ENOTTY)
1849 logwarn("SIOCSRTRFLUSH_IN6 %d", errno);
1850 #ifdef SIOCSPFXFLUSH_IN6
1851 if (if_ioctl6(ifp->ctx, SIOCSPFXFLUSH_IN6,
1852 &ifr, sizeof(ifr)) == -1 &&
1853 errno != ENOTSUP && errno != ENOTTY)
1854 logwarn("SIOCSPFXFLUSH_IN6");