xref: /minix/external/bsd/dhcpcd/dist/if-bsd.c (revision 9f20bfa6)
1 #include <sys/cdefs.h>
2  __RCSID("$NetBSD: if-bsd.c,v 1.24 2015/08/21 13:24:47 roy Exp $");
3 
4 /*
5  * dhcpcd - DHCP client daemon
6  * Copyright (c) 2006-2015 Roy Marples <roy@marples.name>
7  * All rights reserved
8 
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include <sys/ioctl.h>
32 #include <sys/param.h>
33 #include <sys/socket.h>
34 #include <sys/stat.h>
35 #include <sys/sysctl.h>
36 #include <sys/time.h>
37 #include <sys/types.h>
38 #include <sys/uio.h>
39 #include <sys/utsname.h>
40 
41 #include <arpa/inet.h>
42 #include <net/bpf.h>
43 #include <net/if.h>
44 #include <net/if_dl.h>
45 #ifdef __FreeBSD__ /* Needed so that including netinet6/in6_var.h works */
46 #  include <net/if_var.h>
47 #endif
48 #include <net/if_media.h>
49 #include <net/route.h>
50 #include <netinet/if_ether.h>
51 #include <netinet/in.h>
52 #include <netinet/in_var.h>
53 #include <netinet6/in6_var.h>
54 #include <netinet6/nd6.h>
55 #ifdef __DragonFly__
56 #  include <netproto/802_11/ieee80211_ioctl.h>
57 #elif __APPLE__
58   /* FIXME: Add apple includes so we can work out SSID */
59 #else
60 #  include <net80211/ieee80211.h>
61 #  include <net80211/ieee80211_ioctl.h>
62 #endif
63 
64 #include <errno.h>
65 #include <fcntl.h>
66 #include <fnmatch.h>
67 #include <paths.h>
68 #include <stddef.h>
69 #include <stdio.h>
70 #include <stdlib.h>
71 #include <string.h>
72 #include <unistd.h>
73 
74 #if defined(OpenBSD) && OpenBSD >= 201411
75 /* OpenBSD dropped the global setting from sysctl but left the #define
76  * which causes a EPERM error when trying to use it.
77  * I think both the error and keeping the define are wrong, so we #undef it. */
78 #undef IPV6CTL_ACCEPT_RTADV
79 #endif
80 
81 #include "config.h"
82 #include "common.h"
83 #include "dhcp.h"
84 #include "if.h"
85 #include "if-options.h"
86 #include "ipv4.h"
87 #include "ipv4ll.h"
88 #include "ipv6.h"
89 #include "ipv6nd.h"
90 
91 #include "bpf-filter.h"
92 
93 #ifndef RT_ROUNDUP
94 #define RT_ROUNDUP(a)							      \
95 	((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
96 #define RT_ADVANCE(x, n) (x += RT_ROUNDUP((n)->sa_len))
97 #endif
98 
99 #define COPYOUT(sin, sa) do {						      \
100 	if ((sa) && ((sa)->sa_family == AF_INET || (sa)->sa_family == 255))   \
101 		(sin) = ((struct sockaddr_in*)(void *)(sa))->sin_addr;	      \
102 	} while (0)
103 
104 #define COPYOUT6(sin, sa) do {						      \
105 	if ((sa) && ((sa)->sa_family == AF_INET6 || (sa)->sa_family == 255))  \
106 		(sin) = ((struct sockaddr_in6*)(void *)(sa))->sin6_addr;      \
107 	} while (0)
108 
109 #ifndef CLLADDR
110 #  define CLLADDR(s) ((const char *)((s)->sdl_data + (s)->sdl_nlen))
111 #endif
112 
113 int
if_init(__unused struct interface * iface)114 if_init(__unused struct interface *iface)
115 {
116 	/* BSD promotes secondary address by default */
117 	return 0;
118 }
119 
120 int
if_conf(__unused struct interface * iface)121 if_conf(__unused struct interface *iface)
122 {
123 	/* No extra checks needed on BSD */
124 	return 0;
125 }
126 
127 int
if_openlinksocket(void)128 if_openlinksocket(void)
129 {
130 
131 	return xsocket(PF_ROUTE, SOCK_RAW, 0, O_NONBLOCK|O_CLOEXEC);
132 }
133 
134 #if defined(INET) || defined(INET6)
135 static void
if_linkaddr(struct sockaddr_dl * sdl,const struct interface * ifp)136 if_linkaddr(struct sockaddr_dl *sdl, const struct interface *ifp)
137 {
138 
139 	memset(sdl, 0, sizeof(*sdl));
140 	sdl->sdl_family = AF_LINK;
141 	sdl->sdl_len = sizeof(*sdl);
142 	sdl->sdl_nlen = sdl->sdl_alen = sdl->sdl_slen = 0;
143 	sdl->sdl_index = (unsigned short)ifp->index;
144 }
145 #endif
146 
147 static int
if_getssid1(int s,const char * ifname,uint8_t * ssid)148 if_getssid1(int s, const char *ifname, uint8_t *ssid)
149 {
150 	int retval = -1;
151 #if defined(SIOCG80211NWID)
152 	struct ifreq ifr;
153 	struct ieee80211_nwid nwid;
154 #elif defined(IEEE80211_IOC_SSID)
155 	struct ieee80211req ireq;
156 	char nwid[IEEE80211_NWID_LEN + 1];
157 #endif
158 
159 #if defined(SIOCG80211NWID) /* NetBSD */
160 	memset(&ifr, 0, sizeof(ifr));
161 	strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
162 	memset(&nwid, 0, sizeof(nwid));
163 	ifr.ifr_data = (void *)&nwid;
164 	if (ioctl(s, SIOCG80211NWID, &ifr) == 0) {
165 		if (ssid == NULL)
166 			retval = nwid.i_len;
167 		else if (nwid.i_len > IF_SSIDSIZE) {
168 			errno = ENOBUFS;
169 			retval = -1;
170 		} else {
171 			retval = nwid.i_len;
172 			memcpy(ssid, nwid.i_nwid, nwid.i_len);
173 			ssid[nwid.i_len] = '\0';
174 		}
175 	}
176 #elif defined(IEEE80211_IOC_SSID) /* FreeBSD */
177 	memset(&ireq, 0, sizeof(ireq));
178 	strlcpy(ireq.i_name, ifname, sizeof(ireq.i_name));
179 	ireq.i_type = IEEE80211_IOC_SSID;
180 	ireq.i_val = -1;
181 	memset(nwid, 0, sizeof(nwid));
182 	ireq.i_data = &nwid;
183 	if (ioctl(s, SIOCG80211, &ireq) == 0) {
184 		if (ssid == NULL)
185 			retval = ireq.i_len;
186 		else if (ireq.i_len > IF_SSIDSIZE) {
187 			errno = ENOBUFS;
188 			retval = -1;
189 		} else  {
190 			retval = ireq.i_len;
191 			memcpy(ssid, nwid, ireq.i_len);
192 			ssid[ireq.i_len] = '\0';
193 		}
194 	}
195 #endif
196 
197 	return retval;
198 }
199 
200 int
if_getssid(struct interface * ifp)201 if_getssid(struct interface *ifp)
202 {
203 	int r;
204 
205 	r = if_getssid1(ifp->ctx->pf_inet_fd, ifp->name, ifp->ssid);
206 	if (r != -1)
207 		ifp->ssid_len = (unsigned int)r;
208 	return r;
209 }
210 
211 /*
212  * FreeBSD allows for Virtual Access Points
213  * We need to check if the interface is a Virtual Interface Master
214  * and if so, don't use it.
215  * This check is made by virtue of being a IEEE80211 device but
216  * returning the SSID gives an error.
217  */
218 int
if_vimaster(const struct dhcpcd_ctx * ctx,const char * ifname)219 if_vimaster(const struct dhcpcd_ctx *ctx, const char *ifname)
220 {
221 	int r;
222 	struct ifmediareq ifmr;
223 
224 	memset(&ifmr, 0, sizeof(ifmr));
225 	strlcpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name));
226 	r = ioctl(ctx->pf_inet_fd, SIOCGIFMEDIA, &ifmr);
227 	if (r == -1)
228 		return -1;
229 	if (ifmr.ifm_status & IFM_AVALID &&
230 	    IFM_TYPE(ifmr.ifm_active) == IFM_IEEE80211)
231 	{
232 		if (if_getssid1(ctx->pf_inet_fd, ifname, NULL) == -1)
233 			return 1;
234 	}
235 	return 0;
236 }
237 
238 static void
get_addrs(int type,char * cp,struct sockaddr ** sa)239 get_addrs(int type, char *cp, struct sockaddr **sa)
240 {
241 	int i;
242 
243 	for (i = 0; i < RTAX_MAX; i++) {
244 		if (type & (1 << i)) {
245 			sa[i] = (struct sockaddr *)cp;
246 			RT_ADVANCE(cp, sa[i]);
247 		} else
248 			sa[i] = NULL;
249 	}
250 }
251 
252 #if defined(INET) || defined(INET6)
253 static struct interface *
if_findsdl(struct dhcpcd_ctx * ctx,struct sockaddr_dl * sdl)254 if_findsdl(struct dhcpcd_ctx *ctx, struct sockaddr_dl *sdl)
255 {
256 
257 	if (sdl->sdl_nlen) {
258 		char ifname[IF_NAMESIZE];
259 		memcpy(ifname, sdl->sdl_data, sdl->sdl_nlen);
260 		ifname[sdl->sdl_nlen] = '\0';
261 		return if_find(ctx->ifaces, ifname);
262 	}
263 	return NULL;
264 }
265 #endif
266 
267 #ifdef INET
268 const char *if_pfname = "Berkley Packet Filter";
269 
270 int
if_openrawsocket(struct interface * ifp,uint16_t protocol)271 if_openrawsocket(struct interface *ifp, uint16_t protocol)
272 {
273 	struct ipv4_state *state;
274 	int fd = -1;
275 	struct ifreq ifr;
276 	int ibuf_len = 0;
277 	size_t buf_len;
278 	struct bpf_version pv;
279 	struct bpf_program pf;
280 #ifdef BIOCIMMEDIATE
281 	int flags;
282 #endif
283 #ifdef _PATH_BPF
284 	fd = open(_PATH_BPF, O_RDWR | O_CLOEXEC | O_NONBLOCK);
285 #else
286 	char device[32];
287 	int n = 0;
288 
289 	do {
290 		snprintf(device, sizeof(device), "/dev/bpf%d", n++);
291 		fd = open(device, O_RDWR | O_CLOEXEC | O_NONBLOCK);
292 	} while (fd == -1 && errno == EBUSY);
293 #endif
294 
295 	if (fd == -1)
296 		return -1;
297 
298 	state = IPV4_STATE(ifp);
299 	memset(&pv, 0, sizeof(pv));
300 	if (ioctl(fd, BIOCVERSION, &pv) == -1)
301 		goto eexit;
302 	if (pv.bv_major != BPF_MAJOR_VERSION ||
303 	    pv.bv_minor < BPF_MINOR_VERSION) {
304 		logger(ifp->ctx, LOG_ERR, "BPF version mismatch - recompile");
305 		goto eexit;
306 	}
307 
308 	memset(&ifr, 0, sizeof(ifr));
309 	strlcpy(ifr.ifr_name, ifp->name, sizeof(ifr.ifr_name));
310 	if (ioctl(fd, BIOCSETIF, &ifr) == -1)
311 		goto eexit;
312 
313 	/* Get the required BPF buffer length from the kernel. */
314 	if (ioctl(fd, BIOCGBLEN, &ibuf_len) == -1)
315 		goto eexit;
316 	buf_len = (size_t)ibuf_len;
317 	if (state->buffer_size != buf_len) {
318 		free(state->buffer);
319 		state->buffer = malloc(buf_len);
320 		if (state->buffer == NULL)
321 			goto eexit;
322 		state->buffer_size = buf_len;
323 		state->buffer_len = state->buffer_pos = 0;
324 	}
325 
326 #ifdef BIOCIMMEDIATE
327 	flags = 1;
328 	if (ioctl(fd, BIOCIMMEDIATE, &flags) == -1)
329 		goto eexit;
330 #endif
331 
332 	/* Install the DHCP filter */
333 	memset(&pf, 0, sizeof(pf));
334 	if (protocol == ETHERTYPE_ARP) {
335 		pf.bf_insns = UNCONST(arp_bpf_filter);
336 		pf.bf_len = arp_bpf_filter_len;
337 	} else {
338 		pf.bf_insns = UNCONST(dhcp_bpf_filter);
339 		pf.bf_len = dhcp_bpf_filter_len;
340 	}
341 	if (ioctl(fd, BIOCSETF, &pf) == -1)
342 		goto eexit;
343 
344 	return fd;
345 
346 eexit:
347 	free(state->buffer);
348 	state->buffer = NULL;
349 	close(fd);
350 	return -1;
351 }
352 
353 ssize_t
if_sendrawpacket(const struct interface * ifp,uint16_t protocol,const void * data,size_t len)354 if_sendrawpacket(const struct interface *ifp, uint16_t protocol,
355     const void *data, size_t len)
356 {
357 	struct iovec iov[2];
358 	struct ether_header hw;
359 	int fd;
360 
361 	memset(&hw, 0, ETHER_HDR_LEN);
362 	memset(&hw.ether_dhost, 0xff, ETHER_ADDR_LEN);
363 	hw.ether_type = htons(protocol);
364 	iov[0].iov_base = &hw;
365 	iov[0].iov_len = ETHER_HDR_LEN;
366 	iov[1].iov_base = UNCONST(data);
367 	iov[1].iov_len = len;
368 	fd = ipv4_protocol_fd(ifp, protocol);
369 	return writev(fd, iov, 2);
370 }
371 
372 /* BPF requires that we read the entire buffer.
373  * So we pass the buffer in the API so we can loop on >1 packet. */
374 ssize_t
if_readrawpacket(struct interface * ifp,uint16_t protocol,void * data,size_t len,int * flags)375 if_readrawpacket(struct interface *ifp, uint16_t protocol,
376     void *data, size_t len, int *flags)
377 {
378 	int fd;
379 	struct bpf_hdr packet;
380 	ssize_t bytes;
381 	const unsigned char *payload;
382 	struct ipv4_state *state;
383 
384 	state = IPV4_STATE(ifp);
385 	fd = ipv4_protocol_fd(ifp, protocol);
386 
387 	*flags = 0;
388 	for (;;) {
389 		if (state->buffer_len == 0) {
390 			bytes = read(fd, state->buffer, state->buffer_size);
391 			if (bytes == -1 || bytes == 0)
392 				return bytes;
393 			state->buffer_len = (size_t)bytes;
394 			state->buffer_pos = 0;
395 		}
396 		bytes = -1;
397 		memcpy(&packet, state->buffer + state->buffer_pos,
398 		    sizeof(packet));
399 		if (packet.bh_caplen != packet.bh_datalen)
400 			goto next; /* Incomplete packet, drop. */
401 		if (state->buffer_pos + packet.bh_caplen + packet.bh_hdrlen >
402 		    state->buffer_len)
403 			goto next; /* Packet beyond buffer, drop. */
404 		payload = state->buffer + state->buffer_pos +
405 		    packet.bh_hdrlen + ETHER_HDR_LEN;
406 		bytes = (ssize_t)packet.bh_caplen - ETHER_HDR_LEN;
407 		if ((size_t)bytes > len)
408 			bytes = (ssize_t)len;
409 		memcpy(data, payload, (size_t)bytes);
410 next:
411 		state->buffer_pos += BPF_WORDALIGN(packet.bh_hdrlen +
412 		    packet.bh_caplen);
413 		if (state->buffer_pos >= state->buffer_len) {
414 			state->buffer_len = state->buffer_pos = 0;
415 			*flags |= RAW_EOF;
416 		}
417 		if (bytes != -1)
418 			return bytes;
419 	}
420 }
421 
422 int
if_address(const struct interface * ifp,const struct in_addr * address,const struct in_addr * netmask,const struct in_addr * broadcast,int action)423 if_address(const struct interface *ifp, const struct in_addr *address,
424     const struct in_addr *netmask, const struct in_addr *broadcast,
425     int action)
426 {
427 	int r;
428 	struct in_aliasreq ifra;
429 
430 	memset(&ifra, 0, sizeof(ifra));
431 	strlcpy(ifra.ifra_name, ifp->name, sizeof(ifra.ifra_name));
432 
433 #define ADDADDR(var, addr) do {						      \
434 		(var)->sin_family = AF_INET;				      \
435 		(var)->sin_len = sizeof(*(var));			      \
436 		(var)->sin_addr = *(addr);				      \
437 	} while (/*CONSTCOND*/0)
438 	ADDADDR(&ifra.ifra_addr, address);
439 	ADDADDR(&ifra.ifra_mask, netmask);
440 	if (action >= 0 && broadcast)
441 		ADDADDR(&ifra.ifra_broadaddr, broadcast);
442 #undef ADDADDR
443 
444 	r = ioctl(ifp->ctx->pf_inet_fd,
445 	    action < 0 ? SIOCDIFADDR : SIOCAIFADDR, &ifra);
446 	return r;
447 }
448 
449 static int
if_copyrt(struct dhcpcd_ctx * ctx,struct rt * rt,struct rt_msghdr * rtm)450 if_copyrt(struct dhcpcd_ctx *ctx, struct rt *rt, struct rt_msghdr *rtm)
451 {
452 	char *cp;
453 	struct sockaddr *sa, *rti_info[RTAX_MAX];
454 
455 	cp = (char *)(void *)(rtm + 1);
456 	sa = (struct sockaddr *)(void *)cp;
457 	if (sa->sa_family != AF_INET)
458 		return -1;
459 	if (~rtm->rtm_addrs & (RTA_DST | RTA_GATEWAY))
460 		return -1;
461 #ifdef RTF_CLONED
462 	if (rtm->rtm_flags & RTF_CLONED)
463 		return -1;
464 #endif
465 #ifdef RTF_LOCAL
466 	if (rtm->rtm_flags & RTF_LOCAL)
467 		return -1;
468 #endif
469 #ifdef RTF_BROADCAST
470 	if (rtm->rtm_flags & RTF_BROADCAST)
471 		return -1;
472 #endif
473 
474 	get_addrs(rtm->rtm_addrs, cp, rti_info);
475 	memset(rt, 0, sizeof(*rt));
476 	rt->flags = (unsigned int)rtm->rtm_flags;
477 	COPYOUT(rt->dest, rti_info[RTAX_DST]);
478 	if (rtm->rtm_addrs & RTA_NETMASK)
479 		COPYOUT(rt->net, rti_info[RTAX_NETMASK]);
480 	else
481 		rt->net.s_addr = INADDR_BROADCAST;
482 	COPYOUT(rt->gate, rti_info[RTAX_GATEWAY]);
483 	COPYOUT(rt->src, rti_info[RTAX_IFA]);
484 
485 	if (rtm->rtm_inits & RTV_MTU)
486 		rt->mtu = (unsigned int)rtm->rtm_rmx.rmx_mtu;
487 
488 	if (rtm->rtm_index)
489 		rt->iface = if_findindex(ctx->ifaces, rtm->rtm_index);
490 	else if (rtm->rtm_addrs & RTA_IFP) {
491 		struct sockaddr_dl *sdl;
492 
493 		sdl = (struct sockaddr_dl *)(void *)rti_info[RTAX_IFP];
494 		rt->iface = if_findsdl(ctx, sdl);
495 	}
496 
497 	/* If we don't have an interface and it's a host route, it maybe
498 	 * to a local ip via the loopback interface. */
499 	if (rt->iface == NULL &&
500 	    !(~rtm->rtm_flags & (RTF_HOST | RTF_GATEWAY)))
501 	{
502 		struct ipv4_addr *ia;
503 
504 		if ((ia = ipv4_findaddr(ctx, &rt->dest)))
505 			rt->iface = ia->iface;
506 	}
507 
508 	return 0;
509 }
510 
511 int
if_route(unsigned char cmd,const struct rt * rt)512 if_route(unsigned char cmd, const struct rt *rt)
513 {
514 	const struct dhcp_state *state;
515 	const struct ipv4ll_state *istate;
516 	union sockunion {
517 		struct sockaddr sa;
518 		struct sockaddr_in sin;
519 		struct sockaddr_dl sdl;
520 	} su;
521 	struct rtm
522 	{
523 		struct rt_msghdr hdr;
524 		char buffer[sizeof(su) * RTAX_MAX];
525 	} rtm;
526 	char *bp = rtm.buffer;
527 	size_t l;
528 
529 #define ADDSU {								      \
530 		l = RT_ROUNDUP(su.sa.sa_len);				      \
531 		memcpy(bp, &su, l);					      \
532 		bp += l;						      \
533 	}
534 #define ADDADDR(addr) {							      \
535 		memset(&su, 0, sizeof(su));				      \
536 		su.sin.sin_family = AF_INET;				      \
537 		su.sin.sin_len = sizeof(su.sin);			      \
538 		(&su.sin)->sin_addr = *(addr);				      \
539 		ADDSU;							      \
540 	}
541 
542 #if defined(__minix)
543 	/*
544 	 * It seems that when dhcpcd(8) is invoked on a single interface, which
545 	 * is what netconf(8) configures interfaces to do right now, dhcpcd
546 	 * sometimes tries to remove routes for interfaces it does not manage.
547 	 * These routes therefore do not have an associated "iface", causing
548 	 * dhcpcd to crash on a NULL pointer dereference in this function.
549 	 * This quick hack prevents it from attempting to perform such
550 	 * dereferences.  The affected scenario seems to be mainly that dhcpcd
551 	 * is unable to replace a preexisting default route because of this,
552 	 * but arguably if it didn't set it, it shouldn't remove it either..
553 	 * Better solutions will have to come from upstream.
554 	 */
555 	if (rt->iface == NULL) {
556 		errno = EINVAL;
557 		return -1;
558 	}
559 #endif /* defined(__minix) */
560 
561 	if (cmd != RTM_DELETE) {
562 		state = D_CSTATE(rt->iface);
563 		istate = IPV4LL_CSTATE(rt->iface);
564 	} else {
565 		/* appease GCC */
566 		state = NULL;
567 		istate = NULL;
568 	}
569 	memset(&rtm, 0, sizeof(rtm));
570 	rtm.hdr.rtm_version = RTM_VERSION;
571 	rtm.hdr.rtm_seq = 1;
572 	rtm.hdr.rtm_type = cmd;
573 	rtm.hdr.rtm_addrs = RTA_DST;
574 	if (cmd == RTM_ADD || cmd == RTM_CHANGE)
575 		rtm.hdr.rtm_addrs |= RTA_GATEWAY;
576 	rtm.hdr.rtm_flags = RTF_UP;
577 #ifdef RTF_PINNED
578 	if (cmd != RTM_ADD)
579 		rtm.hdr.rtm_flags |= RTF_PINNED;
580 #endif
581 
582 	if (cmd != RTM_DELETE) {
583 		rtm.hdr.rtm_addrs |= RTA_IFA | RTA_IFP;
584 		/* None interface subnet routes are static. */
585 		if ((rt->gate.s_addr != INADDR_ANY ||
586 		    rt->net.s_addr != state->net.s_addr ||
587 		    rt->dest.s_addr !=
588 		    (state->addr.s_addr & state->net.s_addr)) &&
589 		    (istate == NULL ||
590 		    rt->dest.s_addr !=
591 		    (istate->addr.s_addr & inaddr_llmask.s_addr) ||
592 		    rt->net.s_addr != inaddr_llmask.s_addr))
593 			rtm.hdr.rtm_flags |= RTF_STATIC;
594 		else {
595 #ifdef RTF_CLONING
596 			rtm.hdr.rtm_flags |= RTF_CLONING;
597 #endif
598 #ifdef RTP_CONNECTED
599 			rtm.hdr.rtm_priority = RTP_CONNECTED;
600 #endif
601 		}
602 	}
603 	if (rt->net.s_addr == htonl(INADDR_BROADCAST) &&
604 	    rt->gate.s_addr == htonl(INADDR_ANY))
605 	{
606 #ifdef RTF_CLONING
607 		/* We add a cloning network route for a single host.
608 		 * Traffic to the host will generate a cloned route and the
609 		 * hardware address will resolve correctly.
610 		 * It might be more correct to use RTF_HOST instead of
611 		 * RTF_CLONING, and that does work, but some OS generate
612 		 * an arp warning diagnostic which we don't want to do. */
613 		rtm.hdr.rtm_flags |= RTF_CLONING;
614 		rtm.hdr.rtm_addrs |= RTA_NETMASK;
615 #else
616 		rtm.hdr.rtm_flags |= RTF_HOST;
617 #endif
618 	} else if (rt->gate.s_addr == htonl(INADDR_LOOPBACK) &&
619 	    rt->net.s_addr == htonl(INADDR_BROADCAST))
620 	{
621 		rtm.hdr.rtm_flags |= RTF_HOST | RTF_GATEWAY;
622 		/* Going via lo0 so remove the interface flags */
623 		if (cmd == RTM_ADD)
624 			rtm.hdr.rtm_addrs &= ~(RTA_IFA | RTA_IFP);
625 	} else {
626 		rtm.hdr.rtm_addrs |= RTA_NETMASK;
627 		if (rtm.hdr.rtm_flags & RTF_STATIC)
628 			rtm.hdr.rtm_flags |= RTF_GATEWAY;
629 	}
630 	if ((cmd == RTM_ADD || cmd == RTM_CHANGE) &&
631 	    !(rtm.hdr.rtm_flags & RTF_GATEWAY))
632 		rtm.hdr.rtm_addrs |= RTA_IFA | RTA_IFP;
633 
634 	ADDADDR(&rt->dest);
635 	if (rtm.hdr.rtm_addrs & RTA_GATEWAY) {
636 #ifdef RTF_CLONING
637 		if ((rtm.hdr.rtm_flags & (RTF_HOST | RTF_CLONING) &&
638 #else
639 		if ((rtm.hdr.rtm_flags & RTF_HOST &&
640 #endif
641 		    rt->gate.s_addr != htonl(INADDR_LOOPBACK)) ||
642 		    !(rtm.hdr.rtm_flags & RTF_STATIC))
643 		{
644 			if_linkaddr(&su.sdl, rt->iface);
645 			ADDSU;
646 		} else
647 			ADDADDR(&rt->gate);
648 	}
649 
650 	if (rtm.hdr.rtm_addrs & RTA_NETMASK)
651 		ADDADDR(&rt->net);
652 
653 	if ((cmd == RTM_ADD || cmd == RTM_CHANGE) &&
654 	    (rtm.hdr.rtm_addrs & (RTA_IFP | RTA_IFA)))
655 	{
656 		rtm.hdr.rtm_index = (unsigned short)rt->iface->index;
657 		if (rtm.hdr.rtm_addrs & RTA_IFP) {
658 			if_linkaddr(&su.sdl, rt->iface);
659 			ADDSU;
660 		}
661 
662 		if (rtm.hdr.rtm_addrs & RTA_IFA)
663 			ADDADDR(istate == NULL ? &state->addr : &istate->addr);
664 
665 		if (rt->mtu) {
666 			rtm.hdr.rtm_inits |= RTV_MTU;
667 			rtm.hdr.rtm_rmx.rmx_mtu = rt->mtu;
668 		}
669 	}
670 
671 #undef ADDADDR
672 #undef ADDSU
673 
674 	rtm.hdr.rtm_msglen = (unsigned short)(bp - (char *)&rtm);
675 	return write(rt->iface->ctx->link_fd,
676 	    &rtm, rtm.hdr.rtm_msglen) == -1 ? -1 : 0;
677 }
678 
679 int
if_initrt(struct interface * ifp)680 if_initrt(struct interface *ifp)
681 {
682 	struct rt_msghdr *rtm;
683 	int mib[6];
684 	size_t needed;
685 	char *buf, *p, *end;
686 	struct rt rt;
687 
688 	ipv4_freerts(ifp->ctx->ipv4_kroutes);
689 
690 	mib[0] = CTL_NET;
691 	mib[1] = PF_ROUTE;
692 	mib[2] = 0;
693 	mib[3] = AF_INET;
694 	mib[4] = NET_RT_DUMP;
695 	mib[5] = 0;
696 
697 	if (sysctl(mib, 6, NULL, &needed, NULL, 0) == -1)
698 		return -1;
699 	if (needed == 0)
700 		return 0;
701 	if ((buf = malloc(needed)) == NULL)
702 		return -1;
703 	if (sysctl(mib, 6, buf, &needed, NULL, 0) == -1)
704 		return -1;
705 
706 	end = buf + needed;
707 	for (p = buf; p < end; p += rtm->rtm_msglen) {
708 		rtm = (struct rt_msghdr *)(void *)p;
709 		if (if_copyrt(ifp->ctx, &rt, rtm) == 0)
710 			ipv4_handlert(ifp->ctx, RTM_ADD, &rt);
711 	}
712 	free(buf);
713 	return 0;
714 }
715 
716 #ifdef SIOCGIFAFLAG_IN
717 int
if_addrflags(const struct in_addr * addr,const struct interface * ifp)718 if_addrflags(const struct in_addr *addr, const struct interface *ifp)
719 {
720 	struct ifreq ifr;
721 	struct sockaddr_in *sin;
722 
723 	memset(&ifr, 0, sizeof(ifr));
724 	strlcpy(ifr.ifr_name, ifp->name, sizeof(ifr.ifr_name));
725 	sin = (struct sockaddr_in *)(void *)&ifr.ifr_addr;
726 	sin->sin_family = AF_INET;
727 	sin->sin_addr = *addr;
728 	if (ioctl(ifp->ctx->pf_inet_fd, SIOCGIFAFLAG_IN, &ifr) == -1)
729 		return -1;
730 	return ifr.ifr_addrflags;
731 }
732 #else
733 int
if_addrflags(__unused const struct in_addr * addr,__unused const struct interface * ifp)734 if_addrflags(__unused const struct in_addr *addr,
735     __unused const struct interface *ifp)
736 {
737 
738 	errno = ENOTSUP;
739 	return 0;
740 }
741 #endif
742 #endif /* INET */
743 
744 #ifdef INET6
745 static void
ifa_scope(struct sockaddr_in6 * sin,unsigned int ifindex)746 ifa_scope(struct sockaddr_in6 *sin, unsigned int ifindex)
747 {
748 
749 #ifdef __KAME__
750 	/* KAME based systems want to store the scope inside the sin6_addr
751 	 * for link local addreses */
752 	if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) {
753 		uint16_t scope = htons((uint16_t)ifindex);
754 		memcpy(&sin->sin6_addr.s6_addr[2], &scope,
755 		    sizeof(scope));
756 	}
757 	sin->sin6_scope_id = 0;
758 #else
759 	if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr))
760 		sin->sin6_scope_id = ifindex;
761 	else
762 		sin->sin6_scope_id = 0;
763 #endif
764 }
765 
766 #ifdef __KAME__
767 #define DESCOPE(ia6) do {						      \
768 	if (IN6_IS_ADDR_LINKLOCAL((ia6)))				      \
769 		(ia6)->s6_addr[2] = (ia6)->s6_addr[3] = '\0';		      \
770 	} while (/*CONSTCOND */0)
771 #else
772 #define DESCOPE(ia6)
773 #endif
774 
775 int
if_address6(const struct ipv6_addr * ia,int action)776 if_address6(const struct ipv6_addr *ia, int action)
777 {
778 	struct in6_aliasreq ifa;
779 	struct in6_addr mask;
780 
781 	memset(&ifa, 0, sizeof(ifa));
782 	strlcpy(ifa.ifra_name, ia->iface->name, sizeof(ifa.ifra_name));
783 	/*
784 	 * We should not set IN6_IFF_TENTATIVE as the kernel should be
785 	 * able to work out if it's a new address or not.
786 	 *
787 	 * We should set IN6_IFF_AUTOCONF, but the kernel won't let us.
788 	 * This is probably a safety measure, but still it's not entirely right
789 	 * either.
790 	 */
791 #if 0
792 	if (ia->autoconf)
793 		ifa.ifra_flags |= IN6_IFF_AUTOCONF;
794 #endif
795 #if defined(__minix)
796 	/*
797 	 * On MINIX 3, do set the IN6_IFF_AUTOCONF flag: this tells the TCP/IP
798 	 * service that the address does not come with an implied subnet.
799 	 */
800 	if (ia->flags & IPV6_AF_AUTOCONF)
801 		ifa.ifra_flags |= IN6_IFF_AUTOCONF;
802 #endif
803 #ifdef IPV6_MANAGETEMPADDR /* XXX typo fix on MINIX3: "MANGE" -> "MANAGE" */
804 	if (ia->flags & IPV6_AF_TEMPORARY)
805 		ifa.ifra_flags |= IN6_IFF_TEMPORARY;
806 #endif
807 
808 #define ADDADDR(v, addr) {						      \
809 		(v)->sin6_family = AF_INET6;				      \
810 		(v)->sin6_len = sizeof(*v);				      \
811 		(v)->sin6_addr = *(addr);				      \
812 	}
813 
814 	ADDADDR(&ifa.ifra_addr, &ia->addr);
815 	ifa_scope(&ifa.ifra_addr, ia->iface->index);
816 	ipv6_mask(&mask, ia->prefix_len);
817 	ADDADDR(&ifa.ifra_prefixmask, &mask);
818 	ifa.ifra_lifetime.ia6t_vltime = ia->prefix_vltime;
819 	ifa.ifra_lifetime.ia6t_pltime = ia->prefix_pltime;
820 #undef ADDADDR
821 
822 	return ioctl(ia->iface->ctx->pf_inet6_fd,
823 	    action < 0 ? SIOCDIFADDR_IN6 : SIOCAIFADDR_IN6, &ifa);
824 }
825 
826 
827 static int
if_copyrt6(struct dhcpcd_ctx * ctx,struct rt6 * rt,struct rt_msghdr * rtm)828 if_copyrt6(struct dhcpcd_ctx *ctx, struct rt6 *rt, struct rt_msghdr *rtm)
829 {
830 	char *cp;
831 	struct sockaddr *sa, *rti_info[RTAX_MAX];
832 
833 	cp = (char *)(void *)(rtm + 1);
834 	sa = (struct sockaddr *)(void *)cp;
835 	if (sa->sa_family != AF_INET6)
836 		return -1;
837 	if (~rtm->rtm_addrs & (RTA_DST | RTA_GATEWAY))
838 		return -1;
839 #ifdef RTF_CLONED
840 	if (rtm->rtm_flags & (RTF_CLONED | RTF_HOST))
841 		return -1;
842 #else
843 	if (rtm->rtm_flags & RTF_HOST)
844 		return -1;
845 #endif
846 #ifdef RTF_LOCAL
847 	if (rtm->rtm_flags & RTF_LOCAL)
848 		return -1;
849 #endif
850 
851 	get_addrs(rtm->rtm_addrs, cp, rti_info);
852 	memset(rt, 0, sizeof(*rt));
853 	rt->flags = (unsigned int)rtm->rtm_flags;
854 	COPYOUT6(rt->dest, rti_info[RTAX_DST]);
855 	if (rtm->rtm_addrs & RTA_NETMASK) {
856 		/*
857 		 * We need to zero out the struct beyond sin6_len and
858 		 * ensure it's valid.
859 		 * I have no idea what the invalid data is for, could be
860 		 * a kernel bug or actually used for something.
861 		 * Either way it needs to be zeroed out.
862 		 */
863 		struct sockaddr_in6 *sin6;
864 		size_t e, i, len = 0, final = 0;
865 
866 		sin6 = (struct sockaddr_in6 *)(void *)rti_info[RTAX_NETMASK];
867 		rt->net = sin6->sin6_addr;
868 		e = sin6->sin6_len - offsetof(struct sockaddr_in6, sin6_addr);
869 		if (e > sizeof(struct in6_addr))
870 			e = sizeof(struct in6_addr);
871 		for (i = 0; i < e; i++) {
872 			switch (rt->net.s6_addr[i] & 0xff) {
873 			case 0xff:
874 				/* We don't really want the length,
875 				 * just that it's valid */
876 				len++;
877 				break;
878 			case 0xfe:
879 			case 0xfc:
880 			case 0xf8:
881 			case 0xf0:
882 			case 0xe0:
883 			case 0xc0:
884 			case 0x80:
885 				len++;
886 				final = 1;
887 				break;
888 			default:
889 				rt->net.s6_addr[i] = 0x00;
890 				final = 1;
891 				break;
892 			}
893 			if (final)
894 				break;
895 		}
896 		if (len == 0)
897 			i = 0;
898 		while (i < sizeof(rt->net.s6_addr))
899 			rt->net.s6_addr[i++] = 0x00;
900 	} else
901 		ipv6_mask(&rt->net, 128);
902 	COPYOUT6(rt->gate, rti_info[RTAX_GATEWAY]);
903 
904 	if (rtm->rtm_inits & RTV_MTU)
905 		rt->mtu = (unsigned int)rtm->rtm_rmx.rmx_mtu;
906 
907 	if (rtm->rtm_index)
908 		rt->iface = if_findindex(ctx->ifaces, rtm->rtm_index);
909 	else if (rtm->rtm_addrs & RTA_IFP) {
910 		struct sockaddr_dl *sdl;
911 
912 		sdl = (struct sockaddr_dl *)(void *)rti_info[RTAX_IFP];
913 		rt->iface = if_findsdl(ctx, sdl);
914 	}
915 	/* If we don't have an interface and it's a host route, it maybe
916 	 * to a local ip via the loopback interface. */
917 	if (rt->iface == NULL &&
918 	    !(~rtm->rtm_flags & (RTF_HOST | RTF_GATEWAY)))
919 	{
920 		struct ipv6_addr *ia;
921 
922 		if ((ia = ipv6_findaddr(ctx, &rt->dest, 0)))
923 			rt->iface = ia->iface;
924 	}
925 
926 	return 0;
927 }
928 
929 int
if_route6(unsigned char cmd,const struct rt6 * rt)930 if_route6(unsigned char cmd, const struct rt6 *rt)
931 {
932 	union sockunion {
933 		struct sockaddr sa;
934 		struct sockaddr_in6 sin;
935 		struct sockaddr_dl sdl;
936 	} su;
937 	struct rtm
938 	{
939 		struct rt_msghdr hdr;
940 		char buffer[sizeof(su) * RTAX_MAX];
941 	} rtm;
942 	char *bp = rtm.buffer;
943 	size_t l;
944 
945 #define ADDSU {								      \
946 		l = RT_ROUNDUP(su.sa.sa_len);				      \
947 		memcpy(bp, &su, l);					      \
948 		bp += l;						      \
949 	}
950 #define ADDADDRS(addr, scope) {						      \
951 		memset(&su, 0, sizeof(su));				      \
952 		su.sin.sin6_family = AF_INET6;				      \
953 		su.sin.sin6_len = sizeof(su.sin);			      \
954 		(&su.sin)->sin6_addr = *addr;				      \
955 		if (scope)						      \
956 			ifa_scope(&su.sin, scope);			      \
957 		ADDSU;							      \
958 	}
959 #define ADDADDR(addr) ADDADDRS(addr, 0)
960 
961 	memset(&rtm, 0, sizeof(rtm));
962 	rtm.hdr.rtm_version = RTM_VERSION;
963 	rtm.hdr.rtm_seq = 1;
964 	rtm.hdr.rtm_type = cmd;
965 	rtm.hdr.rtm_flags = RTF_UP | (int)rt->flags;
966 #ifdef RTF_PINNED
967 	if (rtm.hdr.rtm_type != RTM_ADD)
968 		rtm.hdr.rtm_flags |= RTF_PINNED;
969 #endif
970 	rtm.hdr.rtm_addrs = RTA_DST | RTA_NETMASK;
971 	/* None interface subnet routes are static. */
972 	if (IN6_IS_ADDR_UNSPECIFIED(&rt->gate)) {
973 #ifdef RTF_CLONING
974 		rtm.hdr.rtm_flags |= RTF_CLONING;
975 #endif
976 #ifdef RTP_CONNECTED
977 		rtm.hdr.rtm_priority = RTP_CONNECTED;
978 #endif
979 	} else
980 		rtm.hdr.rtm_flags |= RTF_GATEWAY | RTF_STATIC;
981 
982 	if (cmd == RTM_ADD)
983 		rtm.hdr.rtm_addrs |= RTA_GATEWAY;
984 	if (cmd == RTM_ADD && !(rtm.hdr.rtm_flags & RTF_REJECT))
985 		rtm.hdr.rtm_addrs |= RTA_IFP | RTA_IFA;
986 
987 	ADDADDR(&rt->dest);
988 	if (rtm.hdr.rtm_addrs & RTA_GATEWAY) {
989 		if (IN6_IS_ADDR_UNSPECIFIED(&rt->gate)) {
990 			if_linkaddr(&su.sdl, rt->iface);
991 			ADDSU;
992 		} else {
993 			ADDADDRS(&rt->gate, rt->iface->index);
994 		}
995 	}
996 
997 	if (rtm.hdr.rtm_addrs & RTA_NETMASK)
998 		ADDADDR(&rt->net);
999 
1000 	if ((cmd == RTM_ADD || cmd == RTM_CHANGE) &&
1001 	    (rtm.hdr.rtm_addrs & (RTA_IFP | RTA_IFA)))
1002 	{
1003 		rtm.hdr.rtm_index = (unsigned short)rt->iface->index;
1004 		if (rtm.hdr.rtm_addrs & RTA_IFP) {
1005 			if_linkaddr(&su.sdl, rt->iface);
1006 			ADDSU;
1007 		}
1008 
1009 		if (rtm.hdr.rtm_addrs & RTA_IFA) {
1010 			const struct ipv6_addr *lla;
1011 
1012 			lla = ipv6_linklocal(rt->iface);
1013 			if (lla == NULL) /* unlikely */
1014 					return -1;
1015 			ADDADDRS(&lla->addr, rt->iface->index);
1016 		}
1017 
1018 		if (rt->mtu) {
1019 			rtm.hdr.rtm_inits |= RTV_MTU;
1020 			rtm.hdr.rtm_rmx.rmx_mtu = rt->mtu;
1021 		}
1022 	}
1023 
1024 #undef ADDADDR
1025 #undef ADDSU
1026 
1027 	rtm.hdr.rtm_msglen = (unsigned short)(bp - (char *)&rtm);
1028 	return write(rt->iface->ctx->link_fd,
1029 	    &rtm, rtm.hdr.rtm_msglen) == -1 ? -1 : 0;
1030 }
1031 
1032 int
if_initrt6(struct interface * ifp)1033 if_initrt6(struct interface *ifp)
1034 {
1035 	struct rt_msghdr *rtm;
1036 	int mib[6];
1037 	size_t needed;
1038 	char *buf, *p, *end;
1039 	struct rt6 rt;
1040 
1041 	ipv6_freerts(&ifp->ctx->ipv6->kroutes);
1042 
1043 	mib[0] = CTL_NET;
1044 	mib[1] = PF_ROUTE;
1045 	mib[2] = 0;
1046 	mib[3] = AF_INET6;
1047 	mib[4] = NET_RT_DUMP;
1048 	mib[5] = 0;
1049 
1050 	if (sysctl(mib, 6, NULL, &needed, NULL, 0) == -1)
1051 		return -1;
1052 	if (needed == 0)
1053 		return 0;
1054 	if ((buf = malloc(needed)) == NULL)
1055 		return -1;
1056 	if (sysctl(mib, 6, buf, &needed, NULL, 0) == -1)
1057 		return -1;
1058 
1059 	end = buf + needed;
1060 	for (p = buf; p < end; p += rtm->rtm_msglen) {
1061 		rtm = (struct rt_msghdr *)(void *)p;
1062 		if (if_copyrt6(ifp->ctx, &rt, rtm) == 0)
1063 			ipv6_handlert(ifp->ctx, RTM_ADD, &rt);
1064 	}
1065 	free(buf);
1066 	return 0;
1067 }
1068 
1069 int
if_addrflags6(const struct in6_addr * addr,const struct interface * ifp)1070 if_addrflags6(const struct in6_addr *addr, const struct interface *ifp)
1071 {
1072 	int flags;
1073 	struct in6_ifreq ifr6;
1074 
1075 	memset(&ifr6, 0, sizeof(ifr6));
1076 	strlcpy(ifr6.ifr_name, ifp->name, sizeof(ifr6.ifr_name));
1077 	ifr6.ifr_addr.sin6_family = AF_INET6;
1078 	ifr6.ifr_addr.sin6_addr = *addr;
1079 	ifa_scope(&ifr6.ifr_addr, ifp->index);
1080 	if (ioctl(ifp->ctx->pf_inet6_fd, SIOCGIFAFLAG_IN6, &ifr6) != -1)
1081 		flags = ifr6.ifr_ifru.ifru_flags6;
1082 	else
1083 		flags = -1;
1084 	return flags;
1085 }
1086 
1087 int
if_getlifetime6(struct ipv6_addr * ia)1088 if_getlifetime6(struct ipv6_addr *ia)
1089 {
1090 	struct in6_ifreq ifr6;
1091 	time_t t;
1092 	struct in6_addrlifetime *lifetime;
1093 
1094 	memset(&ifr6, 0, sizeof(ifr6));
1095 	strlcpy(ifr6.ifr_name, ia->iface->name, sizeof(ifr6.ifr_name));
1096 	ifr6.ifr_addr.sin6_family = AF_INET6;
1097 	ifr6.ifr_addr.sin6_addr = ia->addr;
1098 	ifa_scope(&ifr6.ifr_addr, ia->iface->index);
1099 	if (ioctl(ia->iface->ctx->pf_inet6_fd,
1100 	    SIOCGIFALIFETIME_IN6, &ifr6) == -1)
1101 		return -1;
1102 
1103 	t = time(NULL);
1104 	lifetime = &ifr6.ifr_ifru.ifru_lifetime;
1105 
1106 	if (lifetime->ia6t_preferred)
1107 		ia->prefix_pltime = (uint32_t)(lifetime->ia6t_preferred -
1108 		    MIN(t, lifetime->ia6t_preferred));
1109 	else
1110 		ia->prefix_pltime = ND6_INFINITE_LIFETIME;
1111 	if (lifetime->ia6t_expire) {
1112 		ia->prefix_vltime = (uint32_t)(lifetime->ia6t_expire -
1113 		    MIN(t, lifetime->ia6t_expire));
1114 		/* Calculate the created time */
1115 		clock_gettime(CLOCK_MONOTONIC, &ia->created);
1116 		ia->created.tv_sec -= lifetime->ia6t_vltime - ia->prefix_vltime;
1117 	} else
1118 		ia->prefix_vltime = ND6_INFINITE_LIFETIME;
1119 	return 0;
1120 }
1121 #endif
1122 
1123 int
if_managelink(struct dhcpcd_ctx * ctx)1124 if_managelink(struct dhcpcd_ctx *ctx)
1125 {
1126 	/* route and ifwatchd like a msg buf size of 2048 */
1127 	char msg[2048], *p, *e, *cp;
1128 	ssize_t bytes;
1129 	struct rt_msghdr *rtm;
1130 	struct if_announcemsghdr *ifan;
1131 	struct if_msghdr *ifm;
1132 	struct ifa_msghdr *ifam;
1133 	struct sockaddr *sa, *rti_info[RTAX_MAX];
1134 	int len;
1135 	struct sockaddr_dl sdl;
1136 	struct interface *ifp;
1137 #ifdef INET
1138 	struct rt rt;
1139 #endif
1140 #ifdef INET6
1141 	struct rt6 rt6;
1142 	struct in6_addr ia6, net6;
1143 	struct sockaddr_in6 *sin6;
1144 #endif
1145 #if (defined(INET) && defined(IN_IFF_TENTATIVE)) || defined(INET6)
1146 	int ifa_flags;
1147 #elif defined(__minix)
1148 	int ifa_flags;	/* compilation fix for USE_INET6=no */
1149 #endif
1150 
1151 	if ((bytes = read(ctx->link_fd, msg, sizeof(msg))) == -1)
1152 		return -1;
1153 	e = msg + bytes;
1154 	for (p = msg; p < e; p += rtm->rtm_msglen) {
1155 		rtm = (struct rt_msghdr *)(void *)p;
1156 		// Ignore messages generated by us
1157 		if (rtm->rtm_pid == getpid())
1158 			break;
1159 		switch(rtm->rtm_type) {
1160 #ifdef RTM_IFANNOUNCE
1161 		case RTM_IFANNOUNCE:
1162 			ifan = (struct if_announcemsghdr *)(void *)p;
1163 			switch(ifan->ifan_what) {
1164 			case IFAN_ARRIVAL:
1165 				dhcpcd_handleinterface(ctx, 1,
1166 				    ifan->ifan_name);
1167 				break;
1168 			case IFAN_DEPARTURE:
1169 				dhcpcd_handleinterface(ctx, -1,
1170 				    ifan->ifan_name);
1171 				break;
1172 			}
1173 			break;
1174 #endif
1175 		case RTM_IFINFO:
1176 			ifm = (struct if_msghdr *)(void *)p;
1177 			ifp = if_findindex(ctx->ifaces, ifm->ifm_index);
1178 			if (ifp == NULL)
1179 				break;
1180 			switch (ifm->ifm_data.ifi_link_state) {
1181 			case LINK_STATE_DOWN:
1182 				len = LINK_DOWN;
1183 				break;
1184 			case LINK_STATE_UP:
1185 				len = LINK_UP;
1186 				break;
1187 			default:
1188 				/* handle_carrier will re-load
1189 				 * the interface flags and check for
1190 				 * IFF_RUNNING as some drivers that
1191 				 * don't handle link state also don't
1192 				 * set IFF_RUNNING when this routing
1193 				 * message is generated.
1194 				 * As such, it is a race ...*/
1195 				len = LINK_UNKNOWN;
1196 				break;
1197 			}
1198 			dhcpcd_handlecarrier(ctx, len,
1199 			    (unsigned int)ifm->ifm_flags, ifp->name);
1200 			break;
1201 		case RTM_ADD:
1202 		case RTM_CHANGE:
1203 		case RTM_DELETE:
1204 			cp = (char *)(void *)(rtm + 1);
1205 			sa = (struct sockaddr *)(void *)cp;
1206 			switch (sa->sa_family) {
1207 #ifdef INET
1208 			case AF_INET:
1209 				if (if_copyrt(ctx, &rt, rtm) == 0)
1210 					ipv4_handlert(ctx, rtm->rtm_type, &rt);
1211 				break;
1212 #endif
1213 #ifdef INET6
1214 			case AF_INET6:
1215 				if (~rtm->rtm_addrs & (RTA_DST | RTA_GATEWAY))
1216 					break;
1217 				/*
1218 				 * BSD caches host routes in the
1219 				 * routing table.
1220 				 * As such, we should be notified of
1221 				 * reachability by its existance
1222 				 * with a hardware address
1223 				 */
1224 				if (rtm->rtm_flags & (RTF_HOST)) {
1225 					get_addrs(rtm->rtm_addrs, cp, rti_info);
1226 					COPYOUT6(ia6, rti_info[RTAX_DST]);
1227 					DESCOPE(&ia6);
1228 					if (rti_info[RTAX_GATEWAY]->sa_family
1229 					    == AF_LINK)
1230 						memcpy(&sdl,
1231 						    rti_info[RTAX_GATEWAY],
1232 						    sizeof(sdl));
1233 					else
1234 						sdl.sdl_alen = 0;
1235 					ipv6nd_neighbour(ctx, &ia6,
1236 					    rtm->rtm_type != RTM_DELETE &&
1237 					    sdl.sdl_alen ?
1238 					    IPV6ND_REACHABLE : 0);
1239 					break;
1240 				}
1241 
1242 				if (if_copyrt6(ctx, &rt6, rtm) == 0)
1243 					ipv6_handlert(ctx, rtm->rtm_type, &rt6);
1244 				break;
1245 #endif
1246 			}
1247 			break;
1248 #ifdef RTM_CHGADDR
1249 		case RTM_CHGADDR:	/* FALLTHROUGH */
1250 #endif
1251 		case RTM_DELADDR:	/* FALLTHROUGH */
1252 		case RTM_NEWADDR:
1253 			ifam = (struct ifa_msghdr *)(void *)p;
1254 			ifp = if_findindex(ctx->ifaces, ifam->ifam_index);
1255 			if (ifp == NULL)
1256 				break;
1257 			cp = (char *)(void *)(ifam + 1);
1258 			get_addrs(ifam->ifam_addrs, cp, rti_info);
1259 			if (rti_info[RTAX_IFA] == NULL)
1260 				break;
1261 			switch (rti_info[RTAX_IFA]->sa_family) {
1262 			case AF_LINK:
1263 #ifdef RTM_CHGADDR
1264 				if (rtm->rtm_type != RTM_CHGADDR)
1265 					break;
1266 #else
1267 				if (rtm->rtm_type != RTM_NEWADDR)
1268 					break;
1269 #endif
1270 				memcpy(&sdl, rti_info[RTAX_IFA],
1271 				    rti_info[RTAX_IFA]->sa_len);
1272 				dhcpcd_handlehwaddr(ctx, ifp->name,
1273 				    (const unsigned char*)CLLADDR(&sdl),
1274 				    sdl.sdl_alen);
1275 				break;
1276 #ifdef INET
1277 			case AF_INET:
1278 			case 255: /* FIXME: Why 255? */
1279 				COPYOUT(rt.dest, rti_info[RTAX_IFA]);
1280 				COPYOUT(rt.net, rti_info[RTAX_NETMASK]);
1281 				COPYOUT(rt.gate, rti_info[RTAX_BRD]);
1282 				if (rtm->rtm_type == RTM_NEWADDR) {
1283 					ifa_flags = if_addrflags(&rt.dest, ifp);
1284 					if (ifa_flags == -1)
1285 						break;
1286 				} else
1287 					ifa_flags = 0;
1288 				ipv4_handleifa(ctx, rtm->rtm_type,
1289 				    NULL, ifp->name,
1290 				    &rt.dest, &rt.net, &rt.gate, ifa_flags);
1291 				break;
1292 #endif
1293 #ifdef INET6
1294 			case AF_INET6:
1295 				sin6 = (struct sockaddr_in6*)(void *)
1296 				    rti_info[RTAX_IFA];
1297 				ia6 = sin6->sin6_addr;
1298 				DESCOPE(&ia6);
1299 				sin6 = (struct sockaddr_in6*)(void *)
1300 				    rti_info[RTAX_NETMASK];
1301 				net6 = sin6->sin6_addr;
1302 				DESCOPE(&net6);
1303 				if (rtm->rtm_type == RTM_NEWADDR) {
1304 					ifa_flags = if_addrflags6(&ia6, ifp);
1305 					if (ifa_flags == -1)
1306 						break;
1307 				} else
1308 					ifa_flags = 0;
1309 				ipv6_handleifa(ctx, rtm->rtm_type, NULL,
1310 				    ifp->name, &ia6, ipv6_prefixlen(&net6),
1311 				    ifa_flags);
1312 				break;
1313 #endif
1314 			}
1315 			break;
1316 		}
1317 	}
1318 	return 0;
1319 }
1320 
1321 #ifndef SYS_NMLN	/* OSX */
1322 #  define SYS_NMLN 256
1323 #endif
1324 #ifndef HW_MACHINE_ARCH
1325 #  ifdef HW_MODEL	/* OpenBSD */
1326 #    define HW_MACHINE_ARCH HW_MODEL
1327 #  endif
1328 #endif
1329 int
if_machinearch(char * str,size_t len)1330 if_machinearch(char *str, size_t len)
1331 {
1332 	int mib[2] = { CTL_HW, HW_MACHINE_ARCH };
1333 	char march[SYS_NMLN];
1334 	size_t marchlen = sizeof(march);
1335 
1336 	if (sysctl(mib, sizeof(mib) / sizeof(mib[0]),
1337 	    march, &marchlen, NULL, 0) != 0)
1338 		return -1;
1339 	return snprintf(str, len, ":%s", march);
1340 }
1341 
1342 #ifdef INET6
1343 #ifdef IPV6CTL_ACCEPT_RTADV
1344 #define get_inet6_sysctl(code) inet6_sysctl(code, 0, 0)
1345 #define set_inet6_sysctl(code, val) inet6_sysctl(code, val, 1)
1346 static int
inet6_sysctl(int code,int val,int action)1347 inet6_sysctl(int code, int val, int action)
1348 {
1349 	int mib[] = { CTL_NET, PF_INET6, IPPROTO_IPV6, 0 };
1350 	size_t size;
1351 
1352 	mib[3] = code;
1353 	size = sizeof(val);
1354 	if (action) {
1355 		if (sysctl(mib, sizeof(mib)/sizeof(mib[0]),
1356 		    NULL, 0, &val, size) == -1)
1357 			return -1;
1358 		return 0;
1359 	}
1360 	if (sysctl(mib, sizeof(mib)/sizeof(mib[0]), &val, &size, NULL, 0) == -1)
1361 		return -1;
1362 	return val;
1363 }
1364 #endif
1365 
1366 #ifdef IPV6_MANAGETEMPADDR
1367 #ifndef IPV6CTL_TEMPVLTIME
1368 #define get_inet6_sysctlbyname(code) inet6_sysctlbyname(code, 0, 0)
1369 #define set_inet6_sysctlbyname(code, val) inet6_sysctlbyname(code, val, 1)
1370 static int
inet6_sysctlbyname(const char * name,int val,int action)1371 inet6_sysctlbyname(const char *name, int val, int action)
1372 {
1373 	size_t size;
1374 
1375 	size = sizeof(val);
1376 	if (action) {
1377 		if (sysctlbyname(name, NULL, 0, &val, size) == -1)
1378 			return -1;
1379 		return 0;
1380 	}
1381 	if (sysctlbyname(name, &val, &size, NULL, 0) == -1)
1382 		return -1;
1383 	return val;
1384 }
1385 #endif
1386 
1387 int
ip6_use_tempaddr(__unused const char * ifname)1388 ip6_use_tempaddr(__unused const char *ifname)
1389 {
1390 	int val;
1391 
1392 #ifdef IPV6CTL_USETEMPADDR
1393 	val = get_inet6_sysctl(IPV6CTL_USETEMPADDR);
1394 #else
1395 	val = get_inet6_sysctlbyname("net.inet6.ip6.use_tempaddr");
1396 #endif
1397 	return val == -1 ? 0 : val;
1398 }
1399 
1400 int
ip6_temp_preferred_lifetime(__unused const char * ifname)1401 ip6_temp_preferred_lifetime(__unused const char *ifname)
1402 {
1403 	int val;
1404 
1405 #ifdef IPV6CTL_TEMPPLTIME
1406 	val = get_inet6_sysctl(IPV6CTL_TEMPPLTIME);
1407 #else
1408 	val = get_inet6_sysctlbyname("net.inet6.ip6.temppltime");
1409 #endif
1410 	return val < 0 ? TEMP_PREFERRED_LIFETIME : val;
1411 }
1412 
1413 int
ip6_temp_valid_lifetime(__unused const char * ifname)1414 ip6_temp_valid_lifetime(__unused const char *ifname)
1415 {
1416 	int val;
1417 
1418 #ifdef IPV6CTL_TEMPVLTIME
1419 	val = get_inet6_sysctl(IPV6CTL_TEMPVLTIME);
1420 #else
1421 	val = get_inet6_sysctlbyname("net.inet6.ip6.tempvltime");
1422 #endif
1423 	return val < 0 ? TEMP_VALID_LIFETIME : val;
1424 }
1425 #endif
1426 
1427 #define del_if_nd6_flag(s, ifname, flag) if_nd6_flag((s), (ifp), (flag), -1)
1428 #define get_if_nd6_flag(s, ifname, flag) if_nd6_flag((s), (ifp), (flag),  0)
1429 #define set_if_nd6_flag(s, ifname, flag) if_nd6_flag((s), (ifp), (flag),  1)
1430 static int
if_nd6_flag(int s,const struct interface * ifp,unsigned int flag,int set)1431 if_nd6_flag(int s, const struct interface *ifp, unsigned int flag, int set)
1432 {
1433 	struct in6_ndireq nd;
1434 	unsigned int oflags;
1435 
1436 	memset(&nd, 0, sizeof(nd));
1437 	strlcpy(nd.ifname, ifp->name, sizeof(nd.ifname));
1438 	if (ioctl(s, SIOCGIFINFO_IN6, &nd) == -1)
1439 		return -1;
1440 	if (set == 0)
1441 		return nd.ndi.flags & flag ? 1 : 0;
1442 
1443 	oflags = nd.ndi.flags;
1444 	if (set == -1)
1445 		nd.ndi.flags &= ~flag;
1446 	else
1447 		nd.ndi.flags |= flag;
1448 	if (oflags == nd.ndi.flags)
1449 		return 0;
1450 	return ioctl(s, SIOCSIFINFO_FLAGS, &nd);
1451 }
1452 
1453 static int
if_raflush(int s)1454 if_raflush(int s)
1455 {
1456 	char dummy[IFNAMSIZ + 8];
1457 
1458 	strlcpy(dummy, "lo0", sizeof(dummy));
1459 	if (ioctl(s, SIOCSRTRFLUSH_IN6, (void *)&dummy) == -1 ||
1460 	    ioctl(s, SIOCSPFXFLUSH_IN6, (void *)&dummy) == -1)
1461 		return -1;
1462 	return 0;
1463 }
1464 
1465 #ifdef SIOCIFAFATTACH
1466 static int
af_attach(int s,const struct interface * ifp,int af)1467 af_attach(int s, const struct interface *ifp, int af)
1468 {
1469 	struct if_afreq ifar;
1470 
1471 	strlcpy(ifar.ifar_name, ifp->name, sizeof(ifar.ifar_name));
1472 	ifar.ifar_af = af;
1473 	return ioctl(s, SIOCIFAFATTACH, (void *)&ifar);
1474 }
1475 #endif
1476 
1477 #ifdef SIOCGIFXFLAGS
1478 static int
set_ifxflags(int s,const struct interface * ifp,int own)1479 set_ifxflags(int s, const struct interface *ifp, int own)
1480 {
1481 	struct ifreq ifr;
1482 	int flags;
1483 
1484 #ifndef IFXF_NOINET6
1485 	/* No point in removing the no inet6 flag if it doesn't
1486 	 * exist and we're not owning inet6. */
1487 	if (! own)
1488 		return 0;
1489 #endif
1490 
1491 	strlcpy(ifr.ifr_name, ifp->name, sizeof(ifr.ifr_name));
1492 	if (ioctl(s, SIOCGIFXFLAGS, (void *)&ifr) == -1)
1493 		return -1;
1494 	flags = ifr.ifr_flags;
1495 #ifdef IFXF_NOINET6
1496 	flags &= ~IFXF_NOINET6;
1497 #endif
1498 	if (own)
1499 		flags &= ~IFXF_AUTOCONF6;
1500 	if (ifr.ifr_flags == flags)
1501 		return 0;
1502 	ifr.ifr_flags = flags;
1503 	return ioctl(s, SIOCSIFXFLAGS, (void *)&ifr);
1504 }
1505 #endif
1506 
1507 static int
_if_checkipv6(int s,struct dhcpcd_ctx * ctx,const struct interface * ifp,int own)1508 _if_checkipv6(int s, struct dhcpcd_ctx *ctx,
1509     const struct interface *ifp, int own)
1510 {
1511 	int ra;
1512 
1513 	if (ifp) {
1514 #ifdef ND6_IFF_OVERRIDE_RTADV
1515 		int override;
1516 #endif
1517 
1518 #ifdef ND6_IFF_IFDISABLED
1519 		if (del_if_nd6_flag(s, ifp, ND6_IFF_IFDISABLED) == -1) {
1520 			logger(ifp->ctx, LOG_ERR,
1521 			    "%s: del_if_nd6_flag: ND6_IFF_IFDISABLED: %m",
1522 			    ifp->name);
1523 			return -1;
1524 		}
1525 #endif
1526 
1527 #ifdef ND6_IFF_PERFORMNUD
1528 		if (set_if_nd6_flag(s, ifp, ND6_IFF_PERFORMNUD) == -1) {
1529 			logger(ifp->ctx, LOG_ERR,
1530 			    "%s: set_if_nd6_flag: ND6_IFF_PERFORMNUD: %m",
1531 			    ifp->name);
1532 			return -1;
1533 		}
1534 #endif
1535 
1536 #ifdef ND6_IFF_AUTO_LINKLOCAL
1537 		if (own) {
1538 			int all;
1539 
1540 			all = get_if_nd6_flag(s, ifp, ND6_IFF_AUTO_LINKLOCAL);
1541 			if (all == -1)
1542 				logger(ifp->ctx, LOG_ERR,
1543 				    "%s: get_if_nd6_flag: "
1544 				    "ND6_IFF_AUTO_LINKLOCAL: %m",
1545 				    ifp->name);
1546 			else if (all != 0) {
1547 				logger(ifp->ctx, LOG_DEBUG,
1548 				    "%s: disabling Kernel IPv6 "
1549 				    "auto link-local support",
1550 				    ifp->name);
1551 				if (del_if_nd6_flag(s, ifp,
1552 				    ND6_IFF_AUTO_LINKLOCAL) == -1)
1553 				{
1554 					logger(ifp->ctx, LOG_ERR,
1555 					    "%s: del_if_nd6_flag: "
1556 					    "ND6_IFF_AUTO_LINKLOCAL: %m",
1557 					    ifp->name);
1558 					return -1;
1559 				}
1560 			}
1561 		}
1562 #endif
1563 
1564 #ifdef SIOCIFAFATTACH
1565 		if (af_attach(s, ifp, AF_INET6) == -1) {
1566 			logger(ifp->ctx, LOG_ERR,
1567 			    "%s: af_attach: %m", ifp->name);
1568 			return 1;
1569 		}
1570 #endif
1571 
1572 #ifdef SIOCGIFXFLAGS
1573 		if (set_ifxflags(s, ifp, own) == -1) {
1574 			logger(ifp->ctx, LOG_ERR,
1575 			    "%s: set_ifxflags: %m", ifp->name);
1576 			return -1;
1577 		}
1578 #endif
1579 
1580 #ifdef ND6_IFF_OVERRIDE_RTADV
1581 		override = get_if_nd6_flag(s, ifp, ND6_IFF_OVERRIDE_RTADV);
1582 		if (override == -1)
1583 			logger(ifp->ctx, LOG_ERR,
1584 			    "%s: get_if_nd6_flag: ND6_IFF_OVERRIDE_RTADV: %m",
1585 			    ifp->name);
1586 		else if (override == 0 && own) {
1587 			if (set_if_nd6_flag(s, ifp, ND6_IFF_OVERRIDE_RTADV)
1588 			    == -1)
1589 				logger(ifp->ctx, LOG_ERR,
1590 				    "%s: set_if_nd6_flag: "
1591 				    "ND6_IFF_OVERRIDE_RTADV: %m",
1592 				    ifp->name);
1593 			else
1594 				override = 1;
1595 		}
1596 #endif
1597 
1598 #ifdef ND6_IFF_ACCEPT_RTADV
1599 		ra = get_if_nd6_flag(s, ifp, ND6_IFF_ACCEPT_RTADV);
1600 		if (ra == -1)
1601 			logger(ifp->ctx, LOG_ERR,
1602 			    "%s: get_if_nd6_flag: ND6_IFF_ACCEPT_RTADV: %m",
1603 			    ifp->name);
1604 		else if (ra != 0 && own) {
1605 			logger(ifp->ctx, LOG_DEBUG,
1606 			    "%s: disabling Kernel IPv6 RA support",
1607 			    ifp->name);
1608 			if (del_if_nd6_flag(s, ifp, ND6_IFF_ACCEPT_RTADV)
1609 			    == -1)
1610 				logger(ifp->ctx, LOG_ERR,
1611 				    "%s: del_if_nd6_flag: "
1612 				    "ND6_IFF_ACCEPT_RTADV: %m",
1613 				    ifp->name);
1614 			else
1615 				ra = 0;
1616 		} else if (ra == 0 && !own)
1617 			logger(ifp->ctx, LOG_WARNING,
1618 			    "%s: IPv6 kernel autoconf disabled", ifp->name);
1619 #ifdef ND6_IFF_OVERRIDE_RTADV
1620 		if (override == 0 && ra)
1621 			return ctx->ra_global;
1622 #endif
1623 		return ra;
1624 #else
1625 		return ctx->ra_global;
1626 #endif
1627 	}
1628 
1629 #ifdef IPV6CTL_ACCEPT_RTADV
1630 	ra = get_inet6_sysctl(IPV6CTL_ACCEPT_RTADV);
1631 	if (ra == -1)
1632 		/* The sysctl probably doesn't exist, but this isn't an
1633 		 * error as such so just log it and continue */
1634 		logger(ifp->ctx, errno == ENOENT ? LOG_DEBUG : LOG_WARNING,
1635 		    "IPV6CTL_ACCEPT_RTADV: %m");
1636 	else if (ra != 0 && own) {
1637 		logger(ifp->ctx, LOG_DEBUG, "disabling Kernel IPv6 RA support");
1638 		if (set_inet6_sysctl(IPV6CTL_ACCEPT_RTADV, 0) == -1) {
1639 			logger(ifp->ctx, LOG_ERR, "IPV6CTL_ACCEPT_RTADV: %m");
1640 			return ra;
1641 		}
1642 		ra = 0;
1643 #else
1644 	ra = 0;
1645 	if (own) {
1646 #endif
1647 		/* Flush the kernel knowledge of advertised routers
1648 		 * and prefixes so the kernel does not expire prefixes
1649 		 * and default routes we are trying to own. */
1650 		if (if_raflush(s) == -1)
1651 			logger(ctx, LOG_WARNING, "if_raflush: %m");
1652 	}
1653 
1654 	ctx->ra_global = ra;
1655 	return ra;
1656 }
1657 
1658 int
1659 if_checkipv6(struct dhcpcd_ctx *ctx, const struct interface *ifp, int own)
1660 {
1661 
1662 	return _if_checkipv6(ctx->pf_inet6_fd, ctx, ifp, own);
1663 }
1664 #endif
1665