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