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