xref: /dflybsd-src/contrib/dhcpcd/src/if-bsd.c (revision 959366edc6ceee964a31f71baf4f38d75e0f167c)
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * BSD interface driver for dhcpcd
4  * Copyright (c) 2006-2020 Roy Marples <roy@marples.name>
5  * All rights reserved
6 
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/ioctl.h>
30 #include <sys/param.h>
31 #include <sys/socket.h>
32 #include <sys/stat.h>
33 #include <sys/sysctl.h>
34 #include <sys/time.h>
35 #include <sys/types.h>
36 #include <sys/uio.h>
37 #include <sys/utsname.h>
38 
39 #include "config.h"
40 
41 #include <arpa/inet.h>
42 #include <net/bpf.h>
43 #include <net/if.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>
52 #ifdef __NetBSD__
53 #include <net/if_vlanvar.h> /* Needs netinet/if_ether.h */
54 #elif defined(__DragonFly__)
55 #include <net/vlan/if_vlan_var.h>
56 #else
57 #include <net/if_vlan_var.h>
58 #endif
59 #ifdef __DragonFly__
60 #  include <netproto/802_11/ieee80211_ioctl.h>
61 #else
62 #  include <net80211/ieee80211.h>
63 #  include <net80211/ieee80211_ioctl.h>
64 #endif
65 
66 #include <assert.h>
67 #include <errno.h>
68 #include <fcntl.h>
69 #include <fnmatch.h>
70 #include <paths.h>
71 #include <stddef.h>
72 #include <stdio.h>
73 #include <stdlib.h>
74 #include <string.h>
75 #include <unistd.h>
76 
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
82 #endif
83 
84 #include "common.h"
85 #include "dhcp.h"
86 #include "if.h"
87 #include "if-options.h"
88 #include "ipv4.h"
89 #include "ipv4ll.h"
90 #include "ipv6.h"
91 #include "ipv6nd.h"
92 #include "logerr.h"
93 #include "privsep.h"
94 #include "route.h"
95 #include "sa.h"
96 
97 #ifndef RT_ROUNDUP
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))
101 #endif
102 
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[] = {
106 	"bridge",
107 	"fwe",		/* Firewire */
108 	"fwip",		/* Firewire */
109 	"tap",
110 	"xvif",		/* XEN DOM0 -> guest interface */
111 	NULL
112 };
113 
114 struct priv {
115 	int pf_inet6_fd;
116 };
117 
118 struct rtm
119 {
120 	struct rt_msghdr hdr;
121 	char buffer[sizeof(struct sockaddr_storage) * RTAX_MAX];
122 };
123 
124 int
125 if_init(__unused struct interface *iface)
126 {
127 	/* BSD promotes secondary address by default */
128 	return 0;
129 }
130 
131 int
132 if_conf(__unused struct interface *iface)
133 {
134 	/* No extra checks needed on BSD */
135 	return 0;
136 }
137 
138 int
139 if_opensockets_os(struct dhcpcd_ctx *ctx)
140 {
141 	struct priv *priv;
142 	int n;
143 #if defined(RO_MSGFILTER) || defined(ROUTE_MSGFILTER)
144 	unsigned char msgfilter[] = {
145 	    RTM_IFINFO,
146 #ifdef RTM_IFANNOUNCE
147 	    RTM_IFANNOUNCE,
148 #endif
149 	    RTM_ADD, RTM_CHANGE, RTM_DELETE, RTM_MISS,
150 #ifdef RTM_CHGADDR
151 	    RTM_CHGADDR,
152 #endif
153 	    RTM_NEWADDR, RTM_DELADDR
154 	};
155 #ifdef ROUTE_MSGFILTER
156 	unsigned int i, msgfilter_mask;
157 #endif
158 #endif
159 
160 	if ((priv = malloc(sizeof(*priv))) == NULL)
161 		return -1;
162 	ctx->priv = priv;
163 
164 #ifdef INET6
165 	priv->pf_inet6_fd = xsocket(PF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0);
166 #ifdef PRIVSEP_RIGHTS
167 	if (IN_PRIVSEP(ctx))
168 		ps_rights_limit_ioctl(priv->pf_inet6_fd);
169 #endif
170 	/* Don't return an error so we at least work on kernels witout INET6
171 	 * even though we expect INET6 support.
172 	 * We will fail noisily elsewhere anyway. */
173 #else
174 	priv->pf_inet6_fd = -1;
175 #endif
176 
177 	ctx->link_fd = xsocket(PF_ROUTE, SOCK_RAW | SOCK_CXNB, AF_UNSPEC);
178 	if (ctx->link_fd == -1)
179 		return -1;
180 
181 #ifdef SO_RERROR
182 	n = 1;
183 	if (setsockopt(ctx->link_fd, SOL_SOCKET, SO_RERROR, &n,sizeof(n)) == -1)
184 		logerr("%s: SO_RERROR", __func__);
185 #endif
186 
187 	/* Ignore our own route(4) messages.
188 	 * Sadly there is no way of doing this for route(4) messages
189 	 * generated from addresses we add/delete. */
190 	n = 0;
191 	if (setsockopt(ctx->link_fd, SOL_SOCKET, SO_USELOOPBACK,
192 	    &n, sizeof(n)) == -1)
193 		logerr("%s: SO_USELOOPBACK", __func__);
194 
195 #if defined(RO_MSGFILTER)
196 	if (setsockopt(ctx->link_fd, PF_ROUTE, RO_MSGFILTER,
197 	    &msgfilter, sizeof(msgfilter)) == -1)
198 		logerr(__func__);
199 #elif defined(ROUTE_MSGFILTER)
200 	/* Convert the array into a bitmask. */
201 	msgfilter_mask = 0;
202 	for (i = 0; i < __arraycount(msgfilter); i++)
203 		msgfilter_mask |= ROUTE_FILTER(msgfilter[i]);
204 	if (setsockopt(ctx->link_fd, PF_ROUTE, ROUTE_MSGFILTER,
205 	    &msgfilter_mask, sizeof(msgfilter_mask)) == -1)
206 		logerr(__func__);
207 #else
208 #warning kernel does not support route message filtering
209 #endif
210 
211 	return 0;
212 }
213 
214 void
215 if_closesockets_os(struct dhcpcd_ctx *ctx)
216 {
217 	struct priv *priv;
218 
219 	priv = (struct priv *)ctx->priv;
220 	if (priv->pf_inet6_fd != -1)
221 		close(priv->pf_inet6_fd);
222 	free(priv);
223 	ctx->priv = NULL;
224 }
225 
226 #if defined(SIOCALIFADDR) && defined(IFLR_ACTIVE) /*NetBSD */
227 static int
228 if_ioctllink(struct dhcpcd_ctx *ctx, unsigned long req, void *data, size_t len)
229 {
230 	int s;
231 	int retval;
232 
233 #ifdef PRIVSEP
234 	if (ctx->options & DHCPCD_PRIVSEP)
235 		return (int)ps_root_ioctllink(ctx, req, data, len);
236 #else
237 	UNUSED(ctx);
238 #endif
239 
240 	s = socket(PF_LINK, SOCK_DGRAM, 0);
241 	if (s == -1)
242 		return -1;
243 	retval = ioctl(s, req, data, len);
244 	close(s);
245 	return retval;
246 }
247 #endif
248 
249 int
250 if_setmac(struct interface *ifp, void *mac, uint8_t maclen)
251 {
252 
253 	if (ifp->hwlen != maclen) {
254 		errno = EINVAL;
255 		return -1;
256 	}
257 
258 #if defined(SIOCALIFADDR) && defined(IFLR_ACTIVE) /*NetBSD */
259 	struct if_laddrreq iflr = { .flags = IFLR_ACTIVE };
260 	struct sockaddr_dl *sdl = satosdl(&iflr.addr);
261 	int retval;
262 
263 	strlcpy(iflr.iflr_name, ifp->name, sizeof(iflr.iflr_name));
264 	sdl->sdl_family = AF_LINK;
265 	sdl->sdl_len = sizeof(*sdl);
266 	sdl->sdl_alen = maclen;
267 	memcpy(LLADDR(sdl), mac, maclen);
268 	retval = if_ioctllink(ifp->ctx, SIOCALIFADDR, &iflr, sizeof(iflr));
269 
270 	/* Try and remove the old address */
271 	memcpy(LLADDR(sdl), ifp->hwaddr, ifp->hwlen);
272 	if_ioctllink(ifp->ctx, SIOCDLIFADDR, &iflr, sizeof(iflr));
273 
274 	return retval;
275 #else
276 	struct ifreq ifr = {
277 		.ifr_addr.sa_family = AF_LINK,
278 		.ifr_addr.sa_len = maclen,
279 	};
280 
281 	strlcpy(ifr.ifr_name, ifp->name, sizeof(ifr.ifr_name));
282 	memcpy(ifr.ifr_addr.sa_data, mac, maclen);
283 	return if_ioctl(ifp->ctx, SIOCSIFLLADDR, &ifr, sizeof(ifr));
284 #endif
285 }
286 
287 static bool
288 if_ignore1(const char *drvname)
289 {
290 	const char * const *p;
291 
292 	for (p = ifnames_ignore; *p; p++) {
293 		if (strcmp(*p, drvname) == 0)
294 			return true;
295 	}
296 	return false;
297 }
298 
299 #ifdef SIOCGIFGROUP
300 int
301 if_ignoregroup(int s, const char *ifname)
302 {
303 	struct ifgroupreq ifgr = { .ifgr_len = 0 };
304 	struct ifg_req *ifg;
305 	size_t ifg_len;
306 
307 	/* Sadly it is possible to remove the device name
308 	 * from the interface groups, but hopefully this
309 	 * will be very unlikely.... */
310 
311 	strlcpy(ifgr.ifgr_name, ifname, sizeof(ifgr.ifgr_name));
312 	if (ioctl(s, SIOCGIFGROUP, &ifgr) == -1 ||
313 	    (ifgr.ifgr_groups = malloc(ifgr.ifgr_len)) == NULL ||
314 	    ioctl(s, SIOCGIFGROUP, &ifgr) == -1)
315 	{
316 		logerr(__func__);
317 		return -1;
318 	}
319 
320 	for (ifg = ifgr.ifgr_groups, ifg_len = ifgr.ifgr_len;
321 	     ifg && ifg_len >= sizeof(*ifg);
322 	     ifg++, ifg_len -= sizeof(*ifg))
323 	{
324 		if (if_ignore1(ifg->ifgrq_group))
325 			return 1;
326 	}
327 	return 0;
328 }
329 #endif
330 
331 bool
332 if_ignore(struct dhcpcd_ctx *ctx, const char *ifname)
333 {
334 	struct if_spec spec;
335 
336 	if (if_nametospec(ifname, &spec) != 0)
337 		return false;
338 
339 	if (if_ignore1(spec.drvname))
340 		return true;
341 
342 #ifdef SIOCGIFGROUP
343 #if defined(PRIVSEP) && defined(HAVE_PLEDGE)
344 	if (IN_PRIVSEP(ctx))
345 		return ps_root_ifignoregroup(ctx, ifname) == 1 ? true : false;
346 #endif
347 	else
348 		return if_ignoregroup(ctx->pf_inet_fd, ifname) == 1 ?
349 		    true : false;
350 #else
351 	UNUSED(ctx);
352 	return false;
353 #endif
354 }
355 
356 int
357 if_carrier(struct interface *ifp)
358 {
359 	struct ifmediareq ifmr = { .ifm_status = 0 };
360 
361 	/* Not really needed, but the other OS update flags here also */
362 	if (if_getflags(ifp) == -1)
363 		return LINK_UNKNOWN;
364 
365 	strlcpy(ifmr.ifm_name, ifp->name, sizeof(ifmr.ifm_name));
366 	if (ioctl(ifp->ctx->pf_inet_fd, SIOCGIFMEDIA, &ifmr) == -1 ||
367 	    !(ifmr.ifm_status & IFM_AVALID))
368 		return LINK_UNKNOWN;
369 
370 	return (ifmr.ifm_status & IFM_ACTIVE) ? LINK_UP : LINK_DOWN;
371 }
372 
373 static void
374 if_linkaddr(struct sockaddr_dl *sdl, const struct interface *ifp)
375 {
376 
377 	memset(sdl, 0, sizeof(*sdl));
378 	sdl->sdl_family = AF_LINK;
379 	sdl->sdl_len = sizeof(*sdl);
380 	sdl->sdl_nlen = sdl->sdl_alen = sdl->sdl_slen = 0;
381 	sdl->sdl_index = (unsigned short)ifp->index;
382 }
383 
384 #if defined(SIOCG80211NWID) || defined(SIOCGETVLAN)
385 static int if_indirect_ioctl(struct dhcpcd_ctx *ctx,
386     const char *ifname, unsigned long cmd, void *data, size_t len)
387 {
388 	struct ifreq ifr = { .ifr_flags = 0 };
389 
390 #if defined(PRIVSEP) && defined(HAVE_PLEDGE)
391 	if (IN_PRIVSEP(ctx))
392 		return (int)ps_root_indirectioctl(ctx, cmd, ifname, data, len);
393 #else
394 	UNUSED(len);
395 #endif
396 
397 	strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
398 	ifr.ifr_data = data;
399 	return ioctl(ctx->pf_inet_fd, cmd, &ifr);
400 }
401 #endif
402 
403 static int
404 if_getssid1(struct dhcpcd_ctx *ctx, const char *ifname, void *ssid)
405 {
406 	int retval = -1;
407 #if defined(SIOCG80211NWID)
408 	struct ieee80211_nwid nwid;
409 #elif defined(IEEE80211_IOC_SSID)
410 	struct ieee80211req ireq;
411 	char nwid[IEEE80211_NWID_LEN];
412 #endif
413 
414 #if defined(SIOCG80211NWID) /* NetBSD */
415 	memset(&nwid, 0, sizeof(nwid));
416 	if (if_indirect_ioctl(ctx, ifname, SIOCG80211NWID,
417 	    &nwid, sizeof(nwid)) == 0)
418 	{
419 		if (ssid == NULL)
420 			retval = nwid.i_len;
421 		else if (nwid.i_len > IF_SSIDLEN)
422 			errno = ENOBUFS;
423 		else {
424 			retval = nwid.i_len;
425 			memcpy(ssid, nwid.i_nwid, nwid.i_len);
426 		}
427 	}
428 #elif defined(IEEE80211_IOC_SSID) /* FreeBSD */
429 	memset(&ireq, 0, sizeof(ireq));
430 	strlcpy(ireq.i_name, ifname, sizeof(ireq.i_name));
431 	ireq.i_type = IEEE80211_IOC_SSID;
432 	ireq.i_val = -1;
433 	memset(nwid, 0, sizeof(nwid));
434 	ireq.i_data = &nwid;
435 	if (ioctl(ctx->pf_inet_fd, SIOCG80211, &ireq) == 0) {
436 		if (ssid == NULL)
437 			retval = ireq.i_len;
438 		else if (ireq.i_len > IF_SSIDLEN)
439 			errno = ENOBUFS;
440 		else  {
441 			retval = ireq.i_len;
442 			memcpy(ssid, nwid, ireq.i_len);
443 		}
444 	}
445 #else
446 	errno = ENOSYS;
447 #endif
448 
449 	return retval;
450 }
451 
452 int
453 if_getssid(struct interface *ifp)
454 {
455 	int r;
456 
457 	r = if_getssid1(ifp->ctx, ifp->name, ifp->ssid);
458 	if (r != -1)
459 		ifp->ssid_len = (unsigned int)r;
460 	else
461 		ifp->ssid_len = 0;
462 	ifp->ssid[ifp->ssid_len] = '\0';
463 	return r;
464 }
465 
466 /*
467  * FreeBSD allows for Virtual Access Points
468  * We need to check if the interface is a Virtual Interface Master
469  * and if so, don't use it.
470  * This check is made by virtue of being a IEEE80211 device but
471  * returning the SSID gives an error.
472  */
473 int
474 if_vimaster(struct dhcpcd_ctx *ctx, const char *ifname)
475 {
476 	int r;
477 	struct ifmediareq ifmr = { .ifm_active = 0 };
478 
479 	strlcpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name));
480 	r = ioctl(ctx->pf_inet_fd, SIOCGIFMEDIA, &ifmr);
481 	if (r == -1)
482 		return -1;
483 	if (ifmr.ifm_status & IFM_AVALID &&
484 	    IFM_TYPE(ifmr.ifm_active) == IFM_IEEE80211)
485 	{
486 		if (if_getssid1(ctx, ifname, NULL) == -1)
487 			return 1;
488 	}
489 	return 0;
490 }
491 
492 unsigned short
493 if_vlanid(const struct interface *ifp)
494 {
495 #ifdef SIOCGETVLAN
496 	struct vlanreq vlr = { .vlr_tag = 0 };
497 
498 	if (if_indirect_ioctl(ifp->ctx, ifp->name, SIOCGETVLAN,
499 	    &vlr, sizeof(vlr)) != 0)
500 		return 0; /* 0 means no VLANID */
501 	return vlr.vlr_tag;
502 #elif defined(SIOCGVNETID)
503 	struct ifreq ifr = { .ifr_vnetid = 0 };
504 
505 	strlcpy(ifr.ifr_name, ifp->name, sizeof(ifr.ifr_name));
506 	if (ioctl(ifp->ctx->pf_inet_fd, SIOCGVNETID, &ifr) != 0)
507 		return 0; /* 0 means no VLANID */
508 	return ifr.ifr_vnetid;
509 #else
510 	UNUSED(ifp);
511 	return 0; /* 0 means no VLANID */
512 #endif
513 }
514 
515 static int
516 get_addrs(int type, const void *data, size_t data_len,
517     const struct sockaddr **sa)
518 {
519 	const char *cp, *ep;
520 	int i;
521 
522 	cp = data;
523 	ep = cp + data_len;
524 	for (i = 0; i < RTAX_MAX; i++) {
525 		if (type & (1 << i)) {
526 			if (cp >= ep) {
527 				errno = EINVAL;
528 				return -1;
529 			}
530 			sa[i] = (const struct sockaddr *)cp;
531 			RT_ADVANCE(cp, sa[i]);
532 		} else
533 			sa[i] = NULL;
534 	}
535 
536 	return 0;
537 }
538 
539 static struct interface *
540 if_findsdl(struct dhcpcd_ctx *ctx, const struct sockaddr_dl *sdl)
541 {
542 
543 	if (sdl->sdl_index)
544 		return if_findindex(ctx->ifaces, sdl->sdl_index);
545 
546 	if (sdl->sdl_nlen) {
547 		char ifname[IF_NAMESIZE];
548 
549 		memcpy(ifname, sdl->sdl_data, sdl->sdl_nlen);
550 		ifname[sdl->sdl_nlen] = '\0';
551 		return if_find(ctx->ifaces, ifname);
552 	}
553 	if (sdl->sdl_alen) {
554 		struct interface *ifp;
555 
556 		TAILQ_FOREACH(ifp, ctx->ifaces, next) {
557 			if (ifp->hwlen == sdl->sdl_alen &&
558 			    memcmp(ifp->hwaddr,
559 			    sdl->sdl_data, sdl->sdl_alen) == 0)
560 				return ifp;
561 		}
562 	}
563 
564 	errno = ENOENT;
565 	return NULL;
566 }
567 
568 static struct interface *
569 if_findsa(struct dhcpcd_ctx *ctx, const struct sockaddr *sa)
570 {
571 	if (sa == NULL) {
572 		errno = EINVAL;
573 		return NULL;
574 	}
575 
576 	switch (sa->sa_family) {
577 	case AF_LINK:
578 	{
579 		const struct sockaddr_dl *sdl;
580 
581 		sdl = (const void *)sa;
582 		return if_findsdl(ctx, sdl);
583 	}
584 #ifdef INET
585 	case AF_INET:
586 	{
587 		const struct sockaddr_in *sin;
588 		struct ipv4_addr *ia;
589 
590 		sin = (const void *)sa;
591 		if ((ia = ipv4_findmaskaddr(ctx, &sin->sin_addr)))
592 			return ia->iface;
593 		break;
594 	}
595 #endif
596 #ifdef INET6
597 	case AF_INET6:
598 	{
599 		const struct sockaddr_in6 *sin;
600 		unsigned int scope;
601 		struct ipv6_addr *ia;
602 
603 		sin = (const void *)sa;
604 		scope = ipv6_getscope(sin);
605 		if (scope != 0)
606 			return if_findindex(ctx->ifaces, scope);
607 		if ((ia = ipv6_findmaskaddr(ctx, &sin->sin6_addr)))
608 			return ia->iface;
609 		break;
610 	}
611 #endif
612 	default:
613 		errno = EAFNOSUPPORT;
614 		return NULL;
615 	}
616 
617 	errno = ENOENT;
618 	return NULL;
619 }
620 
621 static void
622 if_copysa(struct sockaddr *dst, const struct sockaddr *src)
623 {
624 
625 	assert(dst != NULL);
626 	assert(src != NULL);
627 
628 	memcpy(dst, src, src->sa_len);
629 #if defined(INET6) && defined(__KAME__)
630 	if (dst->sa_family == AF_INET6) {
631 		struct in6_addr *in6;
632 
633 		in6 = &satosin6(dst)->sin6_addr;
634 		if (IN6_IS_ADDR_LINKLOCAL(in6))
635 			in6->s6_addr[2] = in6->s6_addr[3] = '\0';
636 	}
637 #endif
638 }
639 
640 int
641 if_route(unsigned char cmd, const struct rt *rt)
642 {
643 	struct dhcpcd_ctx *ctx;
644 	struct rtm rtmsg;
645 	struct rt_msghdr *rtm = &rtmsg.hdr;
646 	char *bp = rtmsg.buffer;
647 	struct sockaddr_dl sdl;
648 	bool gateway_unspec;
649 
650 	assert(rt != NULL);
651 	assert(rt->rt_ifp != NULL);
652 	assert(rt->rt_ifp->ctx != NULL);
653 	ctx = rt->rt_ifp->ctx;
654 
655 #define ADDSA(sa) do {							      \
656 		memcpy(bp, (sa), (sa)->sa_len);				      \
657 		bp += RT_ROUNDUP((sa)->sa_len);				      \
658 	}  while (0 /* CONSTCOND */)
659 
660 	memset(&rtmsg, 0, sizeof(rtmsg));
661 	rtm->rtm_version = RTM_VERSION;
662 	rtm->rtm_type = cmd;
663 #ifdef __OpenBSD__
664 	rtm->rtm_pid = getpid();
665 #endif
666 	rtm->rtm_seq = ++ctx->seq;
667 	rtm->rtm_flags = (int)rt->rt_flags;
668 	rtm->rtm_addrs = RTA_DST;
669 #ifdef RTF_PINNED
670 	if (cmd != RTM_ADD)
671 		rtm->rtm_flags |= RTF_PINNED;
672 #endif
673 
674 	gateway_unspec = sa_is_unspecified(&rt->rt_gateway);
675 
676 	if (cmd == RTM_ADD || cmd == RTM_CHANGE) {
677 		bool netmask_bcast = sa_is_allones(&rt->rt_netmask);
678 
679 		rtm->rtm_flags |= RTF_UP;
680 		rtm->rtm_addrs |= RTA_GATEWAY;
681 		if (!(rtm->rtm_flags & RTF_REJECT) &&
682 		    !sa_is_loopback(&rt->rt_gateway))
683 		{
684 			rtm->rtm_index = (unsigned short)rt->rt_ifp->index;
685 /*
686  * OpenBSD rejects the message for on-link routes.
687  * FreeBSD-12 kernel apparently panics.
688  * I can't replicate the panic, but better safe than sorry!
689  * https://roy.marples.name/archives/dhcpcd-discuss/0002286.html
690  *
691  * Neither OS currently allows IPv6 address sharing anyway, so let's
692  * try to encourage someone to fix that by logging a waring during compile.
693  */
694 #if defined(__FreeBSD__) || defined(__OpenBSD__)
695 #warning kernel does not allow IPv6 address sharing
696 			if (!gateway_unspec || rt->rt_dest.sa_family!=AF_INET6)
697 #endif
698 			rtm->rtm_addrs |= RTA_IFP;
699 			if (!sa_is_unspecified(&rt->rt_ifa))
700 				rtm->rtm_addrs |= RTA_IFA;
701 		}
702 		if (netmask_bcast)
703 			rtm->rtm_flags |= RTF_HOST;
704 		/* Network routes are cloning or connected if supported.
705 		 * All other routes are static. */
706 		if (gateway_unspec) {
707 #ifdef RTF_CLONING
708 			rtm->rtm_flags |= RTF_CLONING;
709 #endif
710 #ifdef RTF_CONNECTED
711 			rtm->rtm_flags |= RTF_CONNECTED;
712 #endif
713 #ifdef RTP_CONNECTED
714 			rtm->rtm_priority = RTP_CONNECTED;
715 #endif
716 #ifdef RTF_CLONING
717 			if (netmask_bcast) {
718 				/*
719 				 * We add a cloning network route for a single
720 				 * host. Traffic to the host will generate a
721 				 * cloned route and the hardware address will
722 				 * resolve correctly.
723 				 * It might be more correct to use RTF_HOST
724 				 * instead of RTF_CLONING, and that does work,
725 				 * but some OS generate an arp warning
726 				 * diagnostic which we don't want to do.
727 				 */
728 				rtm->rtm_flags &= ~RTF_HOST;
729 			}
730 #endif
731 		} else
732 			rtm->rtm_flags |= RTF_GATEWAY;
733 
734 		if (rt->rt_dflags & RTDF_STATIC)
735 			rtm->rtm_flags |= RTF_STATIC;
736 
737 		if (rt->rt_mtu != 0) {
738 			rtm->rtm_inits |= RTV_MTU;
739 			rtm->rtm_rmx.rmx_mtu = rt->rt_mtu;
740 		}
741 	}
742 
743 	if (!(rtm->rtm_flags & RTF_HOST))
744 		rtm->rtm_addrs |= RTA_NETMASK;
745 
746 	if_linkaddr(&sdl, rt->rt_ifp);
747 
748 	ADDSA(&rt->rt_dest);
749 
750 	if (rtm->rtm_addrs & RTA_GATEWAY) {
751 		if (gateway_unspec)
752 			ADDSA((struct sockaddr *)&sdl);
753 		else {
754 			union sa_ss gateway;
755 
756 			if_copysa(&gateway.sa, &rt->rt_gateway);
757 #ifdef INET6
758 			if (gateway.sa.sa_family == AF_INET6)
759 				ipv6_setscope(&gateway.sin6, rt->rt_ifp->index);
760 #endif
761 			ADDSA(&gateway.sa);
762 		}
763 	}
764 
765 	if (rtm->rtm_addrs & RTA_NETMASK)
766 		ADDSA(&rt->rt_netmask);
767 
768 	if (rtm->rtm_addrs & RTA_IFP)
769 		ADDSA((struct sockaddr *)&sdl);
770 
771 	if (rtm->rtm_addrs & RTA_IFA)
772 		ADDSA(&rt->rt_ifa);
773 
774 #undef ADDSA
775 
776 	rtm->rtm_msglen = (unsigned short)(bp - (char *)rtm);
777 
778 #ifdef PRIVSEP
779 	if (ctx->options & DHCPCD_PRIVSEP) {
780 		if (ps_root_route(ctx, rtm, rtm->rtm_msglen) == -1)
781 			return -1;
782 		return 0;
783 	}
784 #endif
785 	if (write(ctx->link_fd, rtm, rtm->rtm_msglen) == -1)
786 		return -1;
787 	return 0;
788 }
789 
790 static bool
791 if_realroute(const struct rt_msghdr *rtm)
792 {
793 
794 #ifdef RTF_CLONED
795 	if (rtm->rtm_flags & RTF_CLONED)
796 		return false;
797 #endif
798 #ifdef RTF_WASCLONED
799 	if (rtm->rtm_flags & RTF_WASCLONED)
800 		return false;
801 #endif
802 #ifdef RTF_LOCAL
803 	if (rtm->rtm_flags & RTF_LOCAL)
804 		return false;
805 #endif
806 #ifdef RTF_BROADCAST
807 	if (rtm->rtm_flags & RTF_BROADCAST)
808 		return false;
809 #endif
810 	return true;
811 }
812 
813 static int
814 if_copyrt(struct dhcpcd_ctx *ctx, struct rt *rt, const struct rt_msghdr *rtm)
815 {
816 	const struct sockaddr *rti_info[RTAX_MAX];
817 
818 	if (!(rtm->rtm_addrs & RTA_DST)) {
819 		errno = EINVAL;
820 		return -1;
821 	}
822 	if (rtm->rtm_type != RTM_MISS && !(rtm->rtm_addrs & RTA_GATEWAY)) {
823 		errno = EINVAL;
824 		return -1;
825 	}
826 
827 	if (get_addrs(rtm->rtm_addrs, (const char *)rtm + sizeof(*rtm),
828 	              rtm->rtm_msglen - sizeof(*rtm), rti_info) == -1)
829 		return -1;
830 	memset(rt, 0, sizeof(*rt));
831 
832 	rt->rt_flags = (unsigned int)rtm->rtm_flags;
833 	if_copysa(&rt->rt_dest, rti_info[RTAX_DST]);
834 	if (rtm->rtm_addrs & RTA_NETMASK) {
835 		if_copysa(&rt->rt_netmask, rti_info[RTAX_NETMASK]);
836 		if (rt->rt_netmask.sa_family == 255) /* Why? */
837 			rt->rt_netmask.sa_family = rt->rt_dest.sa_family;
838 	}
839 
840 	/* dhcpcd likes an unspecified gateway to indicate via the link.
841 	 * However we need to know if gateway was a link with an address. */
842 	if (rtm->rtm_addrs & RTA_GATEWAY) {
843 		if (rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) {
844 			const struct sockaddr_dl *sdl;
845 
846 			sdl = (const struct sockaddr_dl*)
847 			    (const void *)rti_info[RTAX_GATEWAY];
848 			if (sdl->sdl_alen != 0)
849 				rt->rt_dflags |= RTDF_GATELINK;
850 		} else if (rtm->rtm_flags & RTF_GATEWAY)
851 			if_copysa(&rt->rt_gateway, rti_info[RTAX_GATEWAY]);
852 	}
853 
854 	if (rtm->rtm_addrs & RTA_IFA)
855 		if_copysa(&rt->rt_ifa, rti_info[RTAX_IFA]);
856 
857 	rt->rt_mtu = (unsigned int)rtm->rtm_rmx.rmx_mtu;
858 
859 	if (rtm->rtm_index)
860 		rt->rt_ifp = if_findindex(ctx->ifaces, rtm->rtm_index);
861 	else if (rtm->rtm_addrs & RTA_IFP)
862 		rt->rt_ifp = if_findsa(ctx, rti_info[RTAX_IFP]);
863 	else if (rtm->rtm_addrs & RTA_GATEWAY)
864 		rt->rt_ifp = if_findsa(ctx, rti_info[RTAX_GATEWAY]);
865 	else
866 		rt->rt_ifp = if_findsa(ctx, rti_info[RTAX_DST]);
867 
868 	if (rt->rt_ifp == NULL && rtm->rtm_type == RTM_MISS)
869 		rt->rt_ifp = if_find(ctx->ifaces, "lo0");
870 
871 	if (rt->rt_ifp == NULL) {
872 		errno = ESRCH;
873 		return -1;
874 	}
875 	return 0;
876 }
877 
878 int
879 if_initrt(struct dhcpcd_ctx *ctx, rb_tree_t *kroutes, int af)
880 {
881 	struct rt_msghdr *rtm;
882 	int mib[6];
883 	size_t needed;
884 	char *buf, *p, *end;
885 	struct rt rt, *rtn;
886 
887 	mib[0] = CTL_NET;
888 	mib[1] = PF_ROUTE;
889 	mib[2] = 0;
890 	mib[3] = af;
891 	mib[4] = NET_RT_DUMP;
892 	mib[5] = 0;
893 
894 	if (sysctl(mib, 6, NULL, &needed, NULL, 0) == -1)
895 		return -1;
896 	if (needed == 0)
897 		return 0;
898 	if ((buf = malloc(needed)) == NULL)
899 		return -1;
900 	if (sysctl(mib, 6, buf, &needed, NULL, 0) == -1) {
901 		free(buf);
902 		return -1;
903 	}
904 
905 	end = buf + needed;
906 	for (p = buf; p < end; p += rtm->rtm_msglen) {
907 		rtm = (void *)p;
908 		if (p + rtm->rtm_msglen >= end) {
909 			errno = EINVAL;
910 			break;
911 		}
912 		if (!if_realroute(rtm))
913 			continue;
914 		if (if_copyrt(ctx, &rt, rtm) != 0)
915 			continue;
916 		if ((rtn = rt_new(rt.rt_ifp)) == NULL) {
917 			logerr(__func__);
918 			break;
919 		}
920 		memcpy(rtn, &rt, sizeof(*rtn));
921 		if (rb_tree_insert_node(kroutes, rtn) != rtn)
922 			rt_free(rtn);
923 	}
924 	free(buf);
925 	return p == end ? 0 : -1;
926 }
927 
928 #ifdef INET
929 int
930 if_address(unsigned char cmd, const struct ipv4_addr *ia)
931 {
932 	int r;
933 	struct in_aliasreq ifra;
934 	struct dhcpcd_ctx *ctx = ia->iface->ctx;
935 
936 	memset(&ifra, 0, sizeof(ifra));
937 	strlcpy(ifra.ifra_name, ia->iface->name, sizeof(ifra.ifra_name));
938 
939 #define ADDADDR(var, addr) do {						      \
940 		(var)->sin_family = AF_INET;				      \
941 		(var)->sin_len = sizeof(*(var));			      \
942 		(var)->sin_addr = *(addr);				      \
943 	} while (/*CONSTCOND*/0)
944 	ADDADDR(&ifra.ifra_addr, &ia->addr);
945 	ADDADDR(&ifra.ifra_mask, &ia->mask);
946 	if (cmd == RTM_NEWADDR && ia->brd.s_addr != INADDR_ANY)
947 		ADDADDR(&ifra.ifra_broadaddr, &ia->brd);
948 #undef ADDADDR
949 
950 	r = if_ioctl(ctx,
951 	    cmd == RTM_DELADDR ? SIOCDIFADDR : SIOCAIFADDR, &ifra,sizeof(ifra));
952 	return r;
953 }
954 
955 #if !(defined(HAVE_IFADDRS_ADDRFLAGS) && defined(HAVE_IFAM_ADDRFLAGS))
956 int
957 if_addrflags(const struct interface *ifp, const struct in_addr *addr,
958     __unused const char *alias)
959 {
960 #ifdef SIOCGIFAFLAG_IN
961 	struct ifreq ifr;
962 	struct sockaddr_in *sin;
963 
964 	memset(&ifr, 0, sizeof(ifr));
965 	strlcpy(ifr.ifr_name, ifp->name, sizeof(ifr.ifr_name));
966 	sin = (void *)&ifr.ifr_addr;
967 	sin->sin_family = AF_INET;
968 	sin->sin_addr = *addr;
969 	if (ioctl(ifp->ctx->pf_inet_fd, SIOCGIFAFLAG_IN, &ifr) == -1)
970 		return -1;
971 	return ifr.ifr_addrflags;
972 #else
973 	UNUSED(ifp);
974 	UNUSED(addr);
975 	return 0;
976 #endif
977 }
978 #endif
979 #endif /* INET */
980 
981 #ifdef INET6
982 static int
983 if_ioctl6(struct dhcpcd_ctx *ctx, unsigned long req, void *data, size_t len)
984 {
985 	struct priv *priv;
986 
987 #ifdef PRIVSEP
988 	if (ctx->options & DHCPCD_PRIVSEP)
989 		return (int)ps_root_ioctl6(ctx, req, data, len);
990 #endif
991 
992 	priv = ctx->priv;
993 	return ioctl(priv->pf_inet6_fd, req, data, len);
994 }
995 
996 int
997 if_address6(unsigned char cmd, const struct ipv6_addr *ia)
998 {
999 	struct in6_aliasreq ifa = { .ifra_flags = 0 };
1000 	struct in6_addr mask;
1001 	struct dhcpcd_ctx *ctx = ia->iface->ctx;
1002 
1003 	strlcpy(ifa.ifra_name, ia->iface->name, sizeof(ifa.ifra_name));
1004 #if defined(__FreeBSD__) || defined(__DragonFly__)
1005 	/* This is a bug - the kernel should work this out. */
1006 	if (ia->addr_flags & IN6_IFF_TENTATIVE)
1007 		ifa.ifra_flags |= IN6_IFF_TENTATIVE;
1008 #endif
1009 #if (defined(__NetBSD__) || defined(__OpenBSD__)) && \
1010     (defined(IPV6CTL_ACCEPT_RTADV) || defined(ND6_IFF_ACCEPT_RTADV))
1011 	/* These kernels don't accept userland setting IN6_IFF_AUTOCONF */
1012 #else
1013 	if (ia->flags & IPV6_AF_AUTOCONF)
1014 		ifa.ifra_flags |= IN6_IFF_AUTOCONF;
1015 #endif
1016 #ifdef IPV6_MANAGETEMPADDR
1017 	if (ia->flags & IPV6_AF_TEMPORARY)
1018 		ifa.ifra_flags |= IN6_IFF_TEMPORARY;
1019 #endif
1020 
1021 #define ADDADDR(v, addr) {						      \
1022 		(v)->sin6_family = AF_INET6;				      \
1023 		(v)->sin6_len = sizeof(*v);				      \
1024 		(v)->sin6_addr = *(addr);				      \
1025 	}
1026 
1027 	ADDADDR(&ifa.ifra_addr, &ia->addr);
1028 	ipv6_setscope(&ifa.ifra_addr, ia->iface->index);
1029 	ipv6_mask(&mask, ia->prefix_len);
1030 	ADDADDR(&ifa.ifra_prefixmask, &mask);
1031 
1032 #undef ADDADDR
1033 
1034 	/*
1035 	 * Every BSD kernel wants to add the prefix of the address to it's
1036 	 * list of RA received prefixes.
1037 	 * THIS IS WRONG because there (as the comments in the kernel state)
1038 	 * is no API for managing prefix lifetime and the kernel should not
1039 	 * pretend it's from a RA either.
1040 	 *
1041 	 * The issue is that the very first assigned prefix will inherit the
1042 	 * lifetime of the address, but any subsequent alteration of the
1043 	 * address OR it's lifetime will not affect the prefix lifetime.
1044 	 * As such, we cannot stop the prefix from timing out and then
1045 	 * constantly removing the prefix route dhcpcd is capable of adding
1046 	 * in it's absense.
1047 	 *
1048 	 * What we can do to mitigate the issue is to add the address with
1049 	 * infinite lifetimes, so the prefix route will never time out.
1050 	 * Once done, we can then set lifetimes on the address and all is good.
1051 	 * The downside of this approach is that we need to manually remove
1052 	 * the kernel route because it has no lifetime, but this is OK as
1053 	 * dhcpcd will handle this too.
1054 	 *
1055 	 * This issue is discussed on the NetBSD mailing lists here:
1056 	 * http://mail-index.netbsd.org/tech-net/2016/08/05/msg006044.html
1057 	 *
1058 	 * Fixed in NetBSD-7.99.36
1059 	 * NOT fixed in FreeBSD - bug 195197
1060 	 * Fixed in OpenBSD-5.9
1061 	 */
1062 
1063 #if !((defined(__NetBSD_Version__) && __NetBSD_Version__ >= 799003600) || \
1064       (defined(__OpenBSD__) && OpenBSD >= 201605))
1065 	if (cmd == RTM_NEWADDR && !(ia->flags & IPV6_AF_ADDED)) {
1066 		ifa.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
1067 		ifa.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
1068 		(void)if_ioctl6(ctx, SIOCAIFADDR_IN6, &ifa, sizeof(ifa));
1069 	}
1070 #endif
1071 
1072 #if defined(__OpenBSD__) && OpenBSD <= 201705
1073 	/* BUT OpenBSD older than 6.2 does not reset the address lifetime
1074 	 * for subsequent calls...
1075 	 * Luckily dhcpcd will remove the lease when it expires so
1076 	 * just set an infinite lifetime, unless a temporary address. */
1077 	if (ifa.ifra_flags & IN6_IFF_PRIVACY) {
1078 		ifa.ifra_lifetime.ia6t_vltime = ia->prefix_vltime;
1079 		ifa.ifra_lifetime.ia6t_pltime = ia->prefix_pltime;
1080 	} else {
1081 		ifa.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
1082 		ifa.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
1083 	}
1084 #else
1085 	ifa.ifra_lifetime.ia6t_vltime = ia->prefix_vltime;
1086 	ifa.ifra_lifetime.ia6t_pltime = ia->prefix_pltime;
1087 #endif
1088 
1089 	return if_ioctl6(ctx,
1090 	    cmd == RTM_DELADDR ? SIOCDIFADDR_IN6 : SIOCAIFADDR_IN6,
1091 	    &ifa, sizeof(ifa));
1092 }
1093 
1094 int
1095 if_addrflags6(const struct interface *ifp, const struct in6_addr *addr,
1096     __unused const char *alias)
1097 {
1098 	int flags;
1099 	struct in6_ifreq ifr6;
1100 	struct priv *priv;
1101 
1102 	memset(&ifr6, 0, sizeof(ifr6));
1103 	strlcpy(ifr6.ifr_name, ifp->name, sizeof(ifr6.ifr_name));
1104 	ifr6.ifr_addr.sin6_family = AF_INET6;
1105 	ifr6.ifr_addr.sin6_addr = *addr;
1106 	ipv6_setscope(&ifr6.ifr_addr, ifp->index);
1107 	priv = (struct priv *)ifp->ctx->priv;
1108 	if (ioctl(priv->pf_inet6_fd, SIOCGIFAFLAG_IN6, &ifr6) != -1)
1109 		flags = ifr6.ifr_ifru.ifru_flags6;
1110 	else
1111 		flags = -1;
1112 	return flags;
1113 }
1114 
1115 int
1116 if_getlifetime6(struct ipv6_addr *ia)
1117 {
1118 	struct in6_ifreq ifr6;
1119 	time_t t;
1120 	struct in6_addrlifetime *lifetime;
1121 	struct priv *priv;
1122 
1123 	memset(&ifr6, 0, sizeof(ifr6));
1124 	strlcpy(ifr6.ifr_name, ia->iface->name, sizeof(ifr6.ifr_name));
1125 	ifr6.ifr_addr.sin6_family = AF_INET6;
1126 	ifr6.ifr_addr.sin6_addr = ia->addr;
1127 	ipv6_setscope(&ifr6.ifr_addr, ia->iface->index);
1128 	priv = (struct priv *)ia->iface->ctx->priv;
1129 	if (ioctl(priv->pf_inet6_fd, SIOCGIFALIFETIME_IN6, &ifr6) == -1)
1130 		return -1;
1131 	clock_gettime(CLOCK_MONOTONIC, &ia->created);
1132 
1133 #if defined(__FreeBSD__) || defined(__DragonFly__)
1134 	t = ia->created.tv_sec;
1135 #else
1136 	t = time(NULL);
1137 #endif
1138 
1139 	lifetime = &ifr6.ifr_ifru.ifru_lifetime;
1140 	if (lifetime->ia6t_preferred)
1141 		ia->prefix_pltime = (uint32_t)(lifetime->ia6t_preferred -
1142 		    MIN(t, lifetime->ia6t_preferred));
1143 	else
1144 		ia->prefix_pltime = ND6_INFINITE_LIFETIME;
1145 	if (lifetime->ia6t_expire) {
1146 		ia->prefix_vltime = (uint32_t)(lifetime->ia6t_expire -
1147 		    MIN(t, lifetime->ia6t_expire));
1148 		/* Calculate the created time */
1149 		ia->created.tv_sec -= lifetime->ia6t_vltime - ia->prefix_vltime;
1150 	} else
1151 		ia->prefix_vltime = ND6_INFINITE_LIFETIME;
1152 	return 0;
1153 }
1154 #endif
1155 
1156 static int
1157 if_announce(struct dhcpcd_ctx *ctx, const struct if_announcemsghdr *ifan)
1158 {
1159 
1160 	if (ifan->ifan_msglen < sizeof(*ifan)) {
1161 		errno = EINVAL;
1162 		return -1;
1163 	}
1164 
1165 	switch(ifan->ifan_what) {
1166 	case IFAN_ARRIVAL:
1167 		return dhcpcd_handleinterface(ctx, 1, ifan->ifan_name);
1168 	case IFAN_DEPARTURE:
1169 		return dhcpcd_handleinterface(ctx, -1, ifan->ifan_name);
1170 	}
1171 
1172 	return 0;
1173 }
1174 
1175 static int
1176 if_ifinfo(struct dhcpcd_ctx *ctx, const struct if_msghdr *ifm)
1177 {
1178 	struct interface *ifp;
1179 	int link_state;
1180 
1181 	if (ifm->ifm_msglen < sizeof(*ifm)) {
1182 		errno = EINVAL;
1183 		return -1;
1184 	}
1185 
1186 	if ((ifp = if_findindex(ctx->ifaces, ifm->ifm_index)) == NULL)
1187 		return 0;
1188 
1189 	switch (ifm->ifm_data.ifi_link_state) {
1190 	case LINK_STATE_UNKNOWN:
1191 		link_state = LINK_UNKNOWN;
1192 		break;
1193 #ifdef LINK_STATE_FULL_DUPLEX
1194 	case LINK_STATE_HALF_DUPLEX:	/* FALLTHROUGH */
1195 	case LINK_STATE_FULL_DUPLEX:	/* FALLTHROUGH */
1196 #endif
1197 	case LINK_STATE_UP:
1198 		link_state = LINK_UP;
1199 		break;
1200 	default:
1201 		link_state = LINK_DOWN;
1202 		break;
1203 	}
1204 
1205 	dhcpcd_handlecarrier(ctx, link_state,
1206 	    (unsigned int)ifm->ifm_flags, ifp->name);
1207 	return 0;
1208 }
1209 
1210 static int
1211 if_rtm(struct dhcpcd_ctx *ctx, const struct rt_msghdr *rtm)
1212 {
1213 	struct rt rt;
1214 
1215 	if (rtm->rtm_msglen < sizeof(*rtm)) {
1216 		errno = EINVAL;
1217 		return -1;
1218 	}
1219 
1220 	/* Ignore errors. */
1221 	if (rtm->rtm_errno != 0)
1222 		return 0;
1223 
1224 	/* Ignore messages from ourself. */
1225 #ifdef PRIVSEP
1226 	if (ctx->ps_root_pid != 0) {
1227 		if (rtm->rtm_pid == ctx->ps_root_pid)
1228 			return 0;
1229 	}
1230 #endif
1231 
1232 	if (if_copyrt(ctx, &rt, rtm) == -1)
1233 		return errno == ENOTSUP ? 0 : -1;
1234 
1235 #ifdef INET6
1236 	/*
1237 	 * BSD announces host routes.
1238 	 * As such, we should be notified of reachability by its
1239 	 * existance with a hardware address.
1240 	 * Ensure we don't call this for a newly incomplete state.
1241 	 */
1242 	if (rt.rt_dest.sa_family == AF_INET6 &&
1243 	    (rt.rt_flags & RTF_HOST || rtm->rtm_type == RTM_MISS) &&
1244 	    !(rtm->rtm_type == RTM_ADD && !(rt.rt_dflags & RTDF_GATELINK)))
1245 	{
1246 		bool reachable;
1247 
1248 		reachable = (rtm->rtm_type == RTM_ADD ||
1249 		    rtm->rtm_type == RTM_CHANGE) &&
1250 		    rt.rt_dflags & RTDF_GATELINK;
1251 		ipv6nd_neighbour(ctx, &rt.rt_ss_dest.sin6.sin6_addr, reachable);
1252 	}
1253 #endif
1254 
1255 	if (rtm->rtm_type != RTM_MISS && if_realroute(rtm))
1256 		rt_recvrt(rtm->rtm_type, &rt, rtm->rtm_pid);
1257 	return 0;
1258 }
1259 
1260 static int
1261 if_ifa(struct dhcpcd_ctx *ctx, const struct ifa_msghdr *ifam)
1262 {
1263 	struct interface *ifp;
1264 	const struct sockaddr *rti_info[RTAX_MAX];
1265 	int flags;
1266 	pid_t pid;
1267 
1268 	if (ifam->ifam_msglen < sizeof(*ifam)) {
1269 		errno = EINVAL;
1270 		return -1;
1271 	}
1272 
1273 #ifdef HAVE_IFAM_PID
1274 	/* Ignore address deletions from ourself.
1275 	 * We need to process address flag changes though. */
1276 	if (ifam->ifam_type == RTM_DELADDR) {
1277 #ifdef PRIVSEP
1278 		if (ctx->ps_root_pid != 0) {
1279 			if (ifam->ifam_pid == ctx->ps_root_pid)
1280 				return 0;
1281 		} else
1282 #endif
1283 			/* address management is done via ioctl,
1284 			 * so SO_USELOOPBACK has no effect,
1285 			 * so we do need to check the pid. */
1286 			if (ifam->ifam_pid == getpid())
1287 				return 0;
1288 	}
1289 	pid = ifam->ifam_pid;
1290 #else
1291 	pid = 0;
1292 #endif
1293 
1294 	if (~ifam->ifam_addrs & RTA_IFA)
1295 		return 0;
1296 	if ((ifp = if_findindex(ctx->ifaces, ifam->ifam_index)) == NULL)
1297 		return 0;
1298 
1299 	if (get_addrs(ifam->ifam_addrs, (const char *)ifam + sizeof(*ifam),
1300 		      ifam->ifam_msglen - sizeof(*ifam), rti_info) == -1)
1301 		return -1;
1302 
1303 	switch (rti_info[RTAX_IFA]->sa_family) {
1304 	case AF_LINK:
1305 	{
1306 		struct sockaddr_dl sdl;
1307 
1308 #ifdef RTM_CHGADDR
1309 		if (ifam->ifam_type != RTM_CHGADDR)
1310 			break;
1311 #else
1312 		if (ifam->ifam_type != RTM_NEWADDR)
1313 			break;
1314 #endif
1315 		memcpy(&sdl, rti_info[RTAX_IFA], rti_info[RTAX_IFA]->sa_len);
1316 		dhcpcd_handlehwaddr(ifp, ifp->hwtype,
1317 		    CLLADDR(&sdl), sdl.sdl_alen);
1318 		break;
1319 	}
1320 #ifdef INET
1321 	case AF_INET:
1322 	case 255: /* FIXME: Why 255? */
1323 	{
1324 		const struct sockaddr_in *sin;
1325 		struct in_addr addr, mask, bcast;
1326 
1327 		sin = (const void *)rti_info[RTAX_IFA];
1328 		addr.s_addr = sin != NULL && sin->sin_family == AF_INET ?
1329 		    sin->sin_addr.s_addr : INADDR_ANY;
1330 		sin = (const void *)rti_info[RTAX_NETMASK];
1331 		mask.s_addr = sin != NULL && sin->sin_family == AF_INET ?
1332 		    sin->sin_addr.s_addr : INADDR_ANY;
1333 		sin = (const void *)rti_info[RTAX_BRD];
1334 		bcast.s_addr = sin != NULL && sin->sin_family == AF_INET ?
1335 		    sin->sin_addr.s_addr : INADDR_ANY;
1336 
1337 		/*
1338 		 * NetBSD-7 and older send an invalid broadcast address.
1339 		 * So we need to query the actual address to get
1340 		 * the right one.
1341 		 * We can also use this to test if the address
1342 		 * has really been added or deleted.
1343 		 */
1344 #ifdef SIOCGIFALIAS
1345 		struct in_aliasreq ifra;
1346 
1347 		memset(&ifra, 0, sizeof(ifra));
1348 		strlcpy(ifra.ifra_name, ifp->name, sizeof(ifra.ifra_name));
1349 		ifra.ifra_addr.sin_family = AF_INET;
1350 		ifra.ifra_addr.sin_len = sizeof(ifra.ifra_addr);
1351 		ifra.ifra_addr.sin_addr = addr;
1352 		if (ioctl(ctx->pf_inet_fd, SIOCGIFALIAS, &ifra) == -1) {
1353 			if (errno != ENXIO && errno != EADDRNOTAVAIL)
1354 				logerr("%s: SIOCGIFALIAS", __func__);
1355 			if (ifam->ifam_type != RTM_DELADDR)
1356 				break;
1357 		} else {
1358 			if (ifam->ifam_type == RTM_DELADDR)
1359 				break;
1360 #if defined(__NetBSD_Version__) && __NetBSD_Version__ < 800000000
1361 			bcast = ifra.ifra_broadaddr.sin_addr;
1362 #endif
1363 		}
1364 #else
1365 #warning No SIOCGIFALIAS support
1366 		/*
1367 		 * No SIOCGIFALIAS? That sucks!
1368 		 * This makes this call very heavy weight, but we
1369 		 * really need to know if the message is late or not.
1370 		 */
1371 		const struct sockaddr *sa;
1372 		struct ifaddrs *ifaddrs = NULL, *ifa;
1373 
1374 		sa = rti_info[RTAX_IFA];
1375 #ifdef PRIVSEP_GETIFADDRS
1376 		if (IN_PRIVSEP(ctx)) {
1377 			if (ps_root_getifaddrs(ctx, &ifaddrs) == -1) {
1378 				logerr("ps_root_getifaddrs");
1379 				break;
1380 			}
1381 		} else
1382 #endif
1383 		if (getifaddrs(&ifaddrs) == -1) {
1384 			logerr("getifaddrs");
1385 			break;
1386 		}
1387 		for (ifa = ifaddrs; ifa; ifa = ifa->ifa_next) {
1388 			if (ifa->ifa_addr == NULL)
1389 				continue;
1390 			if (sa_cmp(ifa->ifa_addr, sa) == 0 &&
1391 			    strcmp(ifa->ifa_name, ifp->name) == 0)
1392 				break;
1393 		}
1394 #ifdef PRIVSEP_GETIFADDRS
1395 		if (IN_PRIVSEP(ctx))
1396 			free(ifaddrs);
1397 		else
1398 #endif
1399 		freeifaddrs(ifaddrs);
1400 		if (ifam->ifam_type == RTM_DELADDR) {
1401 			if (ifa != NULL)
1402 				break;
1403 		} else {
1404 			if (ifa == NULL)
1405 				break;
1406 		}
1407 #endif
1408 
1409 #ifdef HAVE_IFAM_ADDRFLAGS
1410 		flags = ifam->ifam_addrflags;
1411 #else
1412 		flags = 0;
1413 #endif
1414 
1415 		ipv4_handleifa(ctx, ifam->ifam_type, NULL, ifp->name,
1416 		    &addr, &mask, &bcast, flags, pid);
1417 		break;
1418 	}
1419 #endif
1420 #ifdef INET6
1421 	case AF_INET6:
1422 	{
1423 		struct in6_addr addr6, mask6;
1424 		const struct sockaddr_in6 *sin6;
1425 
1426 		sin6 = (const void *)rti_info[RTAX_IFA];
1427 		addr6 = sin6->sin6_addr;
1428 		sin6 = (const void *)rti_info[RTAX_NETMASK];
1429 		mask6 = sin6->sin6_addr;
1430 
1431 		/*
1432 		 * If the address was deleted, lets check if it's
1433 		 * a late message and it still exists (maybe modified).
1434 		 * If so, ignore it as deleting an address causes
1435 		 * dhcpcd to drop any lease to which it belongs.
1436 		 * Also check an added address was really added.
1437 		 */
1438 		flags = if_addrflags6(ifp, &addr6, NULL);
1439 		if (flags == -1) {
1440 			if (errno != ENXIO && errno != EADDRNOTAVAIL)
1441 				logerr("%s: if_addrflags6", __func__);
1442 			if (ifam->ifam_type != RTM_DELADDR)
1443 				break;
1444 			flags = 0;
1445 		} else if (ifam->ifam_type == RTM_DELADDR)
1446 			break;
1447 
1448 #ifdef __KAME__
1449 		if (IN6_IS_ADDR_LINKLOCAL(&addr6))
1450 			/* Remove the scope from the address */
1451 			addr6.s6_addr[2] = addr6.s6_addr[3] = '\0';
1452 #endif
1453 
1454 		ipv6_handleifa(ctx, ifam->ifam_type, NULL,
1455 		    ifp->name, &addr6, ipv6_prefixlen(&mask6), flags, pid);
1456 		break;
1457 	}
1458 #endif
1459 	}
1460 
1461 	return 0;
1462 }
1463 
1464 static int
1465 if_dispatch(struct dhcpcd_ctx *ctx, const struct rt_msghdr *rtm)
1466 {
1467 
1468 	if (rtm->rtm_version != RTM_VERSION)
1469 		return 0;
1470 
1471 	switch(rtm->rtm_type) {
1472 #ifdef RTM_IFANNOUNCE
1473 	case RTM_IFANNOUNCE:
1474 		return if_announce(ctx, (const void *)rtm);
1475 #endif
1476 	case RTM_IFINFO:
1477 		return if_ifinfo(ctx, (const void *)rtm);
1478 	case RTM_ADD:		/* FALLTHROUGH */
1479 	case RTM_CHANGE:	/* FALLTHROUGH */
1480 	case RTM_DELETE:	/* FALLTHROUGH */
1481 	case RTM_MISS:
1482 		return if_rtm(ctx, (const void *)rtm);
1483 #ifdef RTM_CHGADDR
1484 	case RTM_CHGADDR:	/* FALLTHROUGH */
1485 #endif
1486 	case RTM_DELADDR:	/* FALLTHROUGH */
1487 	case RTM_NEWADDR:
1488 		return if_ifa(ctx, (const void *)rtm);
1489 #ifdef RTM_DESYNC
1490 	case RTM_DESYNC:
1491 		dhcpcd_linkoverflow(ctx);
1492 #elif !defined(SO_RERROR)
1493 #warning cannot detect route socket overflow within kernel
1494 #endif
1495 	}
1496 
1497 	return 0;
1498 }
1499 
1500 static int
1501 if_missfilter0(struct dhcpcd_ctx *ctx, struct interface *ifp,
1502     struct sockaddr *sa)
1503 {
1504 	size_t salen = (size_t)RT_ROUNDUP(sa->sa_len);
1505 	size_t newlen = ctx->rt_missfilterlen + salen;
1506 	size_t diff = salen - (sa->sa_len);
1507 	uint8_t *cp;
1508 
1509 	if (ctx->rt_missfiltersize < newlen) {
1510 		void *n = realloc(ctx->rt_missfilter, newlen);
1511 		if (n == NULL)
1512 			return -1;
1513 		ctx->rt_missfilter = n;
1514 		ctx->rt_missfiltersize = newlen;
1515 	}
1516 
1517 #ifdef INET6
1518 	if (sa->sa_family == AF_INET6)
1519 		ipv6_setscope(satosin6(sa), ifp->index);
1520 #else
1521 	UNUSED(ifp);
1522 #endif
1523 
1524 	cp = ctx->rt_missfilter + ctx->rt_missfilterlen;
1525 	memcpy(cp, sa, sa->sa_len);
1526 	if (diff != 0)
1527 		memset(cp + sa->sa_len, 0, diff);
1528 	ctx->rt_missfilterlen += salen;
1529 
1530 #ifdef INET6
1531 	if (sa->sa_family == AF_INET6)
1532 		ipv6_setscope(satosin6(sa), 0);
1533 #endif
1534 
1535 	return 0;
1536 }
1537 
1538 int
1539 if_missfilter(struct interface *ifp, struct sockaddr *sa)
1540 {
1541 
1542 	return if_missfilter0(ifp->ctx, ifp, sa);
1543 }
1544 
1545 int
1546 if_missfilter_apply(struct dhcpcd_ctx *ctx)
1547 {
1548 #ifdef RO_MISSFILTER
1549 	if (ctx->rt_missfilterlen == 0) {
1550 		struct sockaddr sa = {
1551 		    .sa_family = AF_UNSPEC,
1552 		    .sa_len = sizeof(sa),
1553 		};
1554 
1555 		if (if_missfilter0(ctx, NULL, &sa) == -1)
1556 			return -1;
1557 	}
1558 
1559 	return setsockopt(ctx->link_fd, PF_ROUTE, RO_MISSFILTER,
1560 	    ctx->rt_missfilter, (socklen_t)ctx->rt_missfilterlen);
1561 #else
1562 #warning kernel does not support RTM_MISS DST filtering
1563 	UNUSED(ctx);
1564 	errno = ENOTSUP;
1565 	return -1;
1566 #endif
1567 }
1568 
1569 __CTASSERT(offsetof(struct rt_msghdr, rtm_msglen) == 0);
1570 int
1571 if_handlelink(struct dhcpcd_ctx *ctx)
1572 {
1573 	struct rtm rtm;
1574 	ssize_t len;
1575 
1576 	len = read(ctx->link_fd, &rtm, sizeof(rtm));
1577 	if (len == -1)
1578 		return -1;
1579 	if (len == 0)
1580 		return 0;
1581 	if ((size_t)len < sizeof(rtm.hdr.rtm_msglen) ||
1582 	    len != rtm.hdr.rtm_msglen)
1583 	{
1584 		errno = EINVAL;
1585 		return -1;
1586 	}
1587 	/*
1588 	 * Coverity thinks that the data could be tainted from here.
1589 	 * I have no idea how because the length of the data we read
1590 	 * is guarded by len and checked to match rtm_msglen.
1591 	 * The issue seems to be related to extracting the addresses
1592 	 * at the end of the header, but seems to have no issues with the
1593 	 * equivalent call in if_initrt.
1594 	 */
1595 	/* coverity[tainted_data] */
1596 	return if_dispatch(ctx, &rtm.hdr);
1597 }
1598 
1599 #ifndef SYS_NMLN	/* OSX */
1600 #  define SYS_NMLN __SYS_NAMELEN
1601 #endif
1602 #ifndef HW_MACHINE_ARCH
1603 #  ifdef HW_MODEL	/* OpenBSD */
1604 #    define HW_MACHINE_ARCH HW_MODEL
1605 #  endif
1606 #endif
1607 int
1608 if_machinearch(char *str, size_t len)
1609 {
1610 	int mib[2] = { CTL_HW, HW_MACHINE_ARCH };
1611 
1612 	return sysctl(mib, sizeof(mib) / sizeof(mib[0]), str, &len, NULL, 0);
1613 }
1614 
1615 #ifdef INET6
1616 #if (defined(IPV6CTL_ACCEPT_RTADV) && !defined(ND6_IFF_ACCEPT_RTADV)) || \
1617     defined(IPV6CTL_FORWARDING)
1618 #define get_inet6_sysctl(code) inet6_sysctl(code, 0, 0)
1619 #define set_inet6_sysctl(code, val) inet6_sysctl(code, val, 1)
1620 static int
1621 inet6_sysctl(int code, int val, int action)
1622 {
1623 	int mib[] = { CTL_NET, PF_INET6, IPPROTO_IPV6, 0 };
1624 	size_t size;
1625 
1626 	mib[3] = code;
1627 	size = sizeof(val);
1628 	if (action) {
1629 		if (sysctl(mib, sizeof(mib)/sizeof(mib[0]),
1630 		    NULL, 0, &val, size) == -1)
1631 			return -1;
1632 		return 0;
1633 	}
1634 	if (sysctl(mib, sizeof(mib)/sizeof(mib[0]), &val, &size, NULL, 0) == -1)
1635 		return -1;
1636 	return val;
1637 }
1638 #endif
1639 
1640 int
1641 if_applyra(const struct ra *rap)
1642 {
1643 #ifdef SIOCSIFINFO_IN6
1644 	struct in6_ndireq nd = { .ndi.chlim = 0 };
1645 	struct dhcpcd_ctx *ctx = rap->iface->ctx;
1646 	int error;
1647 
1648 	strlcpy(nd.ifname, rap->iface->name, sizeof(nd.ifname));
1649 
1650 #ifdef IPV6CTL_ACCEPT_RTADV
1651 	struct priv *priv = ctx->priv;
1652 
1653 	/*
1654 	 * NetBSD changed SIOCSIFINFO_IN6 to NOT set flags when kernel
1655 	 * RA was removed, however both FreeBSD and DragonFlyBSD still do.
1656 	 * linkmtu was also removed.
1657 	 * Hopefully this guard will still work if either remove kernel RA.
1658 	 */
1659 	if (ioctl(priv->pf_inet6_fd, SIOCGIFINFO_IN6, &nd, sizeof(nd)) == -1)
1660 		return -1;
1661 
1662 	nd.ndi.linkmtu = rap->mtu;
1663 #endif
1664 
1665 	nd.ndi.chlim = rap->hoplimit;
1666 	nd.ndi.retrans = rap->retrans;
1667 	nd.ndi.basereachable = rap->reachable;
1668 	error = if_ioctl6(ctx, SIOCSIFINFO_IN6, &nd, sizeof(nd));
1669 #ifdef IPV6CTL_ACCEPT_RTADV
1670 	if (error == -1 && errno == EINVAL) {
1671 		/*
1672 		 * Very likely that this is caused by a dodgy MTU
1673 		 * setting specific to the interface.
1674 		 * Let's set it to "unspecified" and try again.
1675 		 * Doesn't really matter as we fix the MTU against the
1676 		 * routes we add as not all OS support SIOCSIFINFO_IN6.
1677 		 */
1678 		nd.ndi.linkmtu = 0;
1679 		error = if_ioctl6(ctx, SIOCSIFINFO_IN6, &nd, sizeof(nd));
1680 	}
1681 #endif
1682 	return error;
1683 #else
1684 #warning OS does not allow setting of RA bits hoplimit, retrans or reachable
1685 	UNUSED(rap);
1686 	return 0;
1687 #endif
1688 }
1689 
1690 #ifndef IPV6CTL_FORWARDING
1691 #define get_inet6_sysctlbyname(code) inet6_sysctlbyname(code, 0, 0)
1692 #define set_inet6_sysctlbyname(code, val) inet6_sysctlbyname(code, val, 1)
1693 static int
1694 inet6_sysctlbyname(const char *name, int val, int action)
1695 {
1696 	size_t size;
1697 
1698 	size = sizeof(val);
1699 	if (action) {
1700 		if (sysctlbyname(name, NULL, 0, &val, size) == -1)
1701 			return -1;
1702 		return 0;
1703 	}
1704 	if (sysctlbyname(name, &val, &size, NULL, 0) == -1)
1705 		return -1;
1706 	return val;
1707 }
1708 #endif
1709 
1710 int
1711 ip6_forwarding(__unused const char *ifname)
1712 {
1713 	int val;
1714 
1715 #ifdef IPV6CTL_FORWARDING
1716 	val = get_inet6_sysctl(IPV6CTL_FORWARDING);
1717 #else
1718 	val = get_inet6_sysctlbyname("net.inet6.ip6.forwarding");
1719 #endif
1720 	return val < 0 ? 0 : val;
1721 }
1722 
1723 #ifdef SIOCIFAFATTACH
1724 static int
1725 if_af_attach(const struct interface *ifp, int af)
1726 {
1727 	struct if_afreq ifar;
1728 
1729 	strlcpy(ifar.ifar_name, ifp->name, sizeof(ifar.ifar_name));
1730 	ifar.ifar_af = af;
1731 	return if_ioctl6(ifp->ctx, SIOCIFAFATTACH, &ifar, sizeof(ifar));
1732 }
1733 #endif
1734 
1735 #ifdef SIOCGIFXFLAGS
1736 static int
1737 if_set_ifxflags(const struct interface *ifp)
1738 {
1739 	struct ifreq ifr;
1740 	int flags;
1741 	struct priv *priv = ifp->ctx->priv;
1742 
1743 	strlcpy(ifr.ifr_name, ifp->name, sizeof(ifr.ifr_name));
1744 	if (ioctl(priv->pf_inet6_fd, SIOCGIFXFLAGS, &ifr) == -1)
1745 		return -1;
1746 	flags = ifr.ifr_flags;
1747 #ifdef IFXF_NOINET6
1748 	flags &= ~IFXF_NOINET6;
1749 #endif
1750 	/*
1751 	 * If not doing autoconf, don't disable the kernel from doing it.
1752 	 * If we need to, we should have another option actively disable it.
1753 	 *
1754 	 * OpenBSD moved from kernel based SLAAC to userland via slaacd(8).
1755 	 * It has a similar featureset to dhcpcd such as stable private
1756 	 * addresses, but lacks the ability to handle DNS inside the RA
1757 	 * which is a serious shortfall in this day and age.
1758 	 * Appease their user base by working alongside slaacd(8) if
1759 	 * dhcpcd is instructed not to do auto configuration of addresses.
1760 	 */
1761 #if defined(ND6_IFF_ACCEPT_RTADV)
1762 #define	BSD_AUTOCONF	DHCPCD_IPV6RS
1763 #else
1764 #define	BSD_AUTOCONF	DHCPCD_IPV6RA_AUTOCONF
1765 #endif
1766 	if (ifp->options->options & BSD_AUTOCONF)
1767 		flags &= ~IFXF_AUTOCONF6;
1768 	if (ifr.ifr_flags == flags)
1769 		return 0;
1770 	ifr.ifr_flags = flags;
1771 	return if_ioctl6(ifp->ctx, SIOCSIFXFLAGS, &ifr, sizeof(ifr));
1772 }
1773 #endif
1774 
1775 /* OpenBSD removed ND6 flags entirely, so we need to check for their
1776  * existance. */
1777 #if defined(ND6_IFF_AUTO_LINKLOCAL) || \
1778     defined(ND6_IFF_PERFORMNUD) || \
1779     defined(ND6_IFF_ACCEPT_RTADV) || \
1780     defined(ND6_IFF_OVERRIDE_RTADV) || \
1781     defined(ND6_IFF_IFDISABLED)
1782 #define	ND6_NDI_FLAGS
1783 #endif
1784 
1785 void
1786 if_disable_rtadv(void)
1787 {
1788 #if defined(IPV6CTL_ACCEPT_RTADV) && !defined(ND6_IFF_ACCEPT_RTADV)
1789 	int ra = get_inet6_sysctl(IPV6CTL_ACCEPT_RTADV);
1790 
1791 	if (ra == -1) {
1792 		if (errno != ENOENT)
1793 			logerr("IPV6CTL_ACCEPT_RTADV");
1794 	else if (ra != 0)
1795 		if (set_inet6_sysctl(IPV6CTL_ACCEPT_RTADV, 0) == -1)
1796 			logerr("IPV6CTL_ACCEPT_RTADV");
1797 	}
1798 #endif
1799 }
1800 
1801 void
1802 if_setup_inet6(const struct interface *ifp)
1803 {
1804 	struct priv *priv;
1805 	int s;
1806 #ifdef ND6_NDI_FLAGS
1807 	struct in6_ndireq nd;
1808 	int flags;
1809 #endif
1810 
1811 	priv = (struct priv *)ifp->ctx->priv;
1812 	s = priv->pf_inet6_fd;
1813 
1814 #ifdef ND6_NDI_FLAGS
1815 	memset(&nd, 0, sizeof(nd));
1816 	strlcpy(nd.ifname, ifp->name, sizeof(nd.ifname));
1817 	if (ioctl(s, SIOCGIFINFO_IN6, &nd) == -1)
1818 		logerr("%s: SIOCGIFINFO_FLAGS", ifp->name);
1819 	flags = (int)nd.ndi.flags;
1820 #endif
1821 
1822 #ifdef ND6_IFF_AUTO_LINKLOCAL
1823 	/* Unlike the kernel, dhcpcd make make a stable private address. */
1824 	flags &= ~ND6_IFF_AUTO_LINKLOCAL;
1825 #endif
1826 
1827 #ifdef ND6_IFF_PERFORMNUD
1828 	/* NUD is kind of essential. */
1829 	flags |= ND6_IFF_PERFORMNUD;
1830 #endif
1831 
1832 #ifdef ND6_IFF_IFDISABLED
1833 	/* Ensure the interface is not disabled. */
1834 	flags &= ~ND6_IFF_IFDISABLED;
1835 #endif
1836 
1837 	/*
1838 	 * If not doing autoconf, don't disable the kernel from doing it.
1839 	 * If we need to, we should have another option actively disable it.
1840 	 */
1841 #ifdef ND6_IFF_ACCEPT_RTADV
1842 	if (ifp->options->options & DHCPCD_IPV6RS)
1843 		flags &= ~ND6_IFF_ACCEPT_RTADV;
1844 #ifdef ND6_IFF_OVERRIDE_RTADV
1845 	if (ifp->options->options & DHCPCD_IPV6RS)
1846 		flags |= ND6_IFF_OVERRIDE_RTADV;
1847 #endif
1848 #endif
1849 
1850 #ifdef ND6_NDI_FLAGS
1851 	if (nd.ndi.flags != (uint32_t)flags) {
1852 		nd.ndi.flags = (uint32_t)flags;
1853 		if (if_ioctl6(ifp->ctx, SIOCSIFINFO_FLAGS,
1854 		    &nd, sizeof(nd)) == -1)
1855 			logerr("%s: SIOCSIFINFO_FLAGS", ifp->name);
1856 	}
1857 #endif
1858 
1859 	/* Enabling IPv6 by whatever means must be the
1860 	 * last action undertaken to ensure kernel RS and
1861 	 * LLADDR auto configuration are disabled where applicable. */
1862 #ifdef SIOCIFAFATTACH
1863 	if (if_af_attach(ifp, AF_INET6) == -1)
1864 		logerr("%s: if_af_attach", ifp->name);
1865 #endif
1866 
1867 #ifdef SIOCGIFXFLAGS
1868 	if (if_set_ifxflags(ifp) == -1)
1869 		logerr("%s: set_ifxflags", ifp->name);
1870 #endif
1871 
1872 #ifdef SIOCSRTRFLUSH_IN6
1873 	/* Flush the kernel knowledge of advertised routers
1874 	 * and prefixes so the kernel does not expire prefixes
1875 	 * and default routes we are trying to own. */
1876 	if (ifp->options->options & DHCPCD_IPV6RS) {
1877 		struct in6_ifreq ifr;
1878 
1879 		memset(&ifr, 0, sizeof(ifr));
1880 		strlcpy(ifr.ifr_name, ifp->name, sizeof(ifr.ifr_name));
1881 		if (if_ioctl6(ifp->ctx, SIOCSRTRFLUSH_IN6,
1882 		    &ifr, sizeof(ifr)) == -1 &&
1883 		    errno != ENOTSUP && errno != ENOTTY)
1884 			logwarn("SIOCSRTRFLUSH_IN6 %d", errno);
1885 #ifdef SIOCSPFXFLUSH_IN6
1886 		if (if_ioctl6(ifp->ctx, SIOCSPFXFLUSH_IN6,
1887 		    &ifr, sizeof(ifr)) == -1 &&
1888 		    errno != ENOTSUP && errno != ENOTTY)
1889 			logwarn("SIOCSPFXFLUSH_IN6");
1890 #endif
1891 	}
1892 #endif
1893 }
1894 #endif
1895