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