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