xref: /freebsd/usr.sbin/rtsold/rtsol.c (revision e6bfd18d21b225af6a0ed67ceeaf1293b7b9eba5)
1 /*	$KAME: rtsol.c,v 1.27 2003/10/05 00:09:36 itojun Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7  * Copyright (C) 2011 Hiroki Sato
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the project nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  * $FreeBSD$
35  */
36 
37 #include <sys/param.h>
38 #include <sys/capsicum.h>
39 #include <sys/queue.h>
40 #include <sys/socket.h>
41 #include <sys/stat.h>
42 #include <sys/uio.h>
43 #include <sys/wait.h>
44 
45 #include <net/if.h>
46 #include <net/route.h>
47 #include <net/if_dl.h>
48 
49 #define	__BSD_VISIBLE	1	/* IN6ADDR_LINKLOCAL_ALLROUTERS_INIT */
50 #include <netinet/in.h>
51 #undef 	__BSD_VISIBLE
52 #include <netinet/ip6.h>
53 #include <netinet6/ip6_var.h>
54 #include <netinet/icmp6.h>
55 
56 #include <arpa/inet.h>
57 
58 #include <capsicum_helpers.h>
59 #include <netdb.h>
60 #include <time.h>
61 #include <fcntl.h>
62 #include <unistd.h>
63 #include <stdio.h>
64 #include <time.h>
65 #include <err.h>
66 #include <errno.h>
67 #include <string.h>
68 #include <stdlib.h>
69 #include <syslog.h>
70 #include "rtsold.h"
71 
72 static char rsid[IFNAMSIZ + 1 + sizeof(DNSINFO_ORIGIN_LABEL) + 1 + NI_MAXHOST];
73 struct ifinfo_head_t ifinfo_head = TAILQ_HEAD_INITIALIZER(ifinfo_head);
74 
75 static void call_script(const char *const *, struct script_msg_head_t *);
76 static size_t dname_labeldec(char *, size_t, const char *);
77 static struct ra_opt *find_raopt(struct rainfo *, int, void *, size_t);
78 static int ra_opt_rdnss_dispatch(struct ifinfo *, struct rainfo *,
79     struct script_msg_head_t *, struct script_msg_head_t *);
80 static char *make_rsid(const char *, const char *, struct rainfo *);
81 
82 #define	_ARGS_MANAGED	managedconf_script, ifi->ifname, rasender
83 #define	_ARGS_OTHER	otherconf_script, ifi->ifname, rasender
84 #define	_ARGS_ALWAYS	alwaysconf_script, ifi->ifname, rasender
85 #define	_ARGS_RESADD	resolvconf_script, "-a", rsid
86 #define	_ARGS_RESDEL	resolvconf_script, "-d", rsid
87 
88 #define	CALL_SCRIPT(name, sm_head) do {				\
89 	const char *const sarg[] = { _ARGS_##name, NULL };	\
90 	call_script(sarg, sm_head);				\
91 } while (0)
92 
93 #define	ELM_MALLOC(p, error_action) do {			\
94 	p = malloc(sizeof(*p));					\
95 	if (p == NULL) {					\
96 		warnmsg(LOG_ERR, __func__, "malloc failed: %s", \
97 		    strerror(errno));				\
98 		error_action;					\
99 	}							\
100 	memset(p, 0, sizeof(*p));				\
101 } while (0)
102 
103 int
104 recvsockopen(void)
105 {
106 	struct icmp6_filter filt;
107 	cap_rights_t rights;
108 	int on, sock;
109 
110 	if ((sock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0) {
111 		warnmsg(LOG_ERR, __func__, "socket: %s", strerror(errno));
112 		goto fail;
113 	}
114 
115 	/* Provide info about the receiving interface. */
116 	on = 1;
117 	if (setsockopt(sock, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on,
118 	    sizeof(on)) < 0) {
119 		warnmsg(LOG_ERR, __func__, "setsockopt(IPV6_RECVPKTINFO): %s",
120 		    strerror(errno));
121 		goto fail;
122 	}
123 
124 	/* Include the hop limit from the received header. */
125 	on = 1;
126 	if (setsockopt(sock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &on,
127 	    sizeof(on)) < 0) {
128 		warnmsg(LOG_ERR, __func__, "setsockopt(IPV6_RECVHOPLIMIT): %s",
129 		    strerror(errno));
130 		goto fail;
131 	}
132 
133 	/* Filter out everything except for Router Advertisements. */
134 	ICMP6_FILTER_SETBLOCKALL(&filt);
135 	ICMP6_FILTER_SETPASS(ND_ROUTER_ADVERT, &filt);
136 	if (setsockopt(sock, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
137 	    sizeof(filt)) == -1) {
138 		warnmsg(LOG_ERR, __func__, "setsockopt(ICMP6_FILTER): %s",
139 		    strerror(errno));
140 		goto fail;
141 	}
142 
143 	cap_rights_init(&rights, CAP_EVENT, CAP_RECV);
144 	if (caph_rights_limit(sock, &rights) < 0) {
145 		warnmsg(LOG_ERR, __func__, "caph_rights_limit(): %s",
146 		    strerror(errno));
147 		goto fail;
148 	}
149 
150 	return (sock);
151 
152 fail:
153 	if (sock >= 0)
154 		(void)close(sock);
155 	return (-1);
156 }
157 
158 void
159 rtsol_input(int sock)
160 {
161 	uint8_t cmsg[CMSG_SPACE(sizeof(struct in6_pktinfo)) +
162 	    CMSG_SPACE(sizeof(int))];
163 	struct iovec iov;
164 	struct msghdr hdr;
165 	struct sockaddr_in6 from;
166 	char answer[1500], ntopbuf[INET6_ADDRSTRLEN], ifnamebuf[IFNAMSIZ];
167 	int l, ifindex = 0, *hlimp = NULL;
168 	ssize_t msglen;
169 	struct in6_pktinfo *pi = NULL;
170 	struct ifinfo *ifi = NULL;
171 	struct ra_opt *rao = NULL;
172 	struct icmp6_hdr *icp;
173 	struct nd_router_advert *nd_ra;
174 	struct cmsghdr *cm;
175 	struct rainfo *rai;
176 	char *p, *raoptp;
177 	struct in6_addr *addr;
178 	struct nd_opt_hdr *ndo;
179 	struct nd_opt_rdnss *rdnss;
180 	struct nd_opt_dnssl *dnssl;
181 	size_t len;
182 	char nsbuf[INET6_ADDRSTRLEN + 1 + IFNAMSIZ + 1];
183 	char dname[NI_MAXHOST];
184 	struct timespec lifetime, now;
185 	int newent_rai, newent_rao;
186 
187 	memset(&hdr, 0, sizeof(hdr));
188 	hdr.msg_iov = &iov;
189 	hdr.msg_iovlen = 1;
190 	hdr.msg_name = &from;
191 	hdr.msg_namelen = sizeof(from);
192 	hdr.msg_control = cmsg;
193 	hdr.msg_controllen = sizeof(cmsg);
194 
195 	iov.iov_base = (caddr_t)answer;
196 	iov.iov_len = sizeof(answer);
197 
198 	if ((msglen = recvmsg(sock, &hdr, 0)) < 0) {
199 		warnmsg(LOG_ERR, __func__, "recvmsg: %s", strerror(errno));
200 		return;
201 	}
202 
203 	/* Extract control message info. */
204 	for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(&hdr); cm != NULL;
205 	    cm = (struct cmsghdr *)CMSG_NXTHDR(&hdr, cm)) {
206 		if (cm->cmsg_level == IPPROTO_IPV6 &&
207 		    cm->cmsg_type == IPV6_PKTINFO &&
208 		    cm->cmsg_len == CMSG_LEN(sizeof(struct in6_pktinfo))) {
209 			pi = (struct in6_pktinfo *)(void *)(CMSG_DATA(cm));
210 			ifindex = pi->ipi6_ifindex;
211 		}
212 		if (cm->cmsg_level == IPPROTO_IPV6 &&
213 		    cm->cmsg_type == IPV6_HOPLIMIT &&
214 		    cm->cmsg_len == CMSG_LEN(sizeof(int)))
215 			hlimp = (int *)(void *)CMSG_DATA(cm);
216 	}
217 
218 	if (ifindex == 0) {
219 		warnmsg(LOG_ERR, __func__,
220 		    "failed to get receiving interface");
221 		return;
222 	}
223 	if (hlimp == NULL) {
224 		warnmsg(LOG_ERR, __func__,
225 		    "failed to get receiving hop limit");
226 		return;
227 	}
228 
229 	if ((size_t)msglen < sizeof(struct nd_router_advert)) {
230 		warnmsg(LOG_INFO, __func__,
231 		    "packet size(%zd) is too short", msglen);
232 		return;
233 	}
234 
235 	icp = (struct icmp6_hdr *)iov.iov_base;
236 	if (icp->icmp6_type != ND_ROUTER_ADVERT) {
237 		/*
238 		 * this should not happen because we configured a filter
239 		 * that only passes RAs on the receiving socket.
240 		 */
241 		warnmsg(LOG_ERR, __func__,
242 		    "invalid icmp type(%d) from %s on %s", icp->icmp6_type,
243 		    inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
244 			sizeof(ntopbuf)),
245 		    if_indextoname(pi->ipi6_ifindex, ifnamebuf));
246 		return;
247 	}
248 
249 	if (icp->icmp6_code != 0) {
250 		warnmsg(LOG_INFO, __func__,
251 		    "invalid icmp code(%d) from %s on %s", icp->icmp6_code,
252 		    inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
253 			sizeof(ntopbuf)),
254 		    if_indextoname(pi->ipi6_ifindex, ifnamebuf));
255 		return;
256 	}
257 
258 	if (*hlimp != 255) {
259 		warnmsg(LOG_INFO, __func__,
260 		    "invalid RA with hop limit(%d) from %s on %s",
261 		    *hlimp,
262 		    inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
263 			sizeof(ntopbuf)),
264 		    if_indextoname(pi->ipi6_ifindex, ifnamebuf));
265 		return;
266 	}
267 
268 	if (pi && !IN6_IS_ADDR_LINKLOCAL(&from.sin6_addr)) {
269 		warnmsg(LOG_INFO, __func__,
270 		    "invalid RA with non link-local source from %s on %s",
271 		    inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
272 			sizeof(ntopbuf)),
273 		    if_indextoname(pi->ipi6_ifindex, ifnamebuf));
274 		return;
275 	}
276 
277 	/* xxx: more validation? */
278 
279 	if ((ifi = find_ifinfo(pi->ipi6_ifindex)) == NULL) {
280 		warnmsg(LOG_DEBUG, __func__,
281 		    "received RA from %s on an unexpected IF(%s)",
282 		    inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
283 			sizeof(ntopbuf)),
284 		    if_indextoname(pi->ipi6_ifindex, ifnamebuf));
285 		return;
286 	}
287 
288 	warnmsg(LOG_DEBUG, __func__,
289 	    "received RA from %s on %s, state is %d",
290 	    inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf, sizeof(ntopbuf)),
291 	    ifi->ifname, ifi->state);
292 
293 	nd_ra = (struct nd_router_advert *)icp;
294 
295 	/*
296 	 * Process the "M bit."
297 	 * If the value of ManagedConfigFlag changes from FALSE to TRUE, the
298 	 * host should invoke the stateful autoconfiguration protocol,
299 	 * requesting information.
300 	 * [RFC 4861 Section 4.2]
301 	 * XXX ??? [draft-ietf-v6ops-dhcpv6-slaac-problem-07]
302 	 */
303 	if (((nd_ra->nd_ra_flags_reserved) & ND_RA_FLAG_MANAGED) &&
304 	    !ifi->managedconfig) {
305 		const char *rasender = inet_ntop(AF_INET6, &from.sin6_addr,
306 		    ntopbuf, sizeof(ntopbuf));
307 		warnmsg(LOG_DEBUG, __func__,
308 		    "ManagedConfigFlag on %s is turned on", ifi->ifname);
309 		ifi->managedconfig = 1;
310 		CALL_SCRIPT(MANAGED, NULL);
311 	}
312 
313 	/*
314 	 * Process the "O bit."
315 	 * If the value of OtherConfigFlag changes from FALSE to TRUE, the
316 	 * host should invoke the stateful autoconfiguration protocol,
317 	 * requesting information unless the "M bit" was set as well in
318 	 * which case the "O bit" is redundant.
319 	 * [RFC 4861 Section 4.2]
320 	 */
321 	if (((nd_ra->nd_ra_flags_reserved) & ND_RA_FLAG_OTHER) &&
322 	    !ifi->otherconfig) {
323 		const char *rasender = inet_ntop(AF_INET6, &from.sin6_addr,
324 		    ntopbuf, sizeof(ntopbuf));
325 		warnmsg(LOG_DEBUG, __func__,
326 		    "OtherConfigFlag on %s is turned on", ifi->ifname);
327 		ifi->otherconfig = 1;
328 		if (!ifi->managedconfig)
329 			CALL_SCRIPT(OTHER, NULL);
330 	}
331 
332 	/*
333 	 * "Always" script.
334 	 */
335 	if (!ifi->alwaysconfig) {
336 		const char *rasender = inet_ntop(AF_INET6, &from.sin6_addr,
337 		    ntopbuf, sizeof(ntopbuf));
338 		ifi->alwaysconfig = 1;
339 		CALL_SCRIPT(ALWAYS, NULL);
340 	}
341 
342 	clock_gettime(CLOCK_MONOTONIC_FAST, &now);
343 	newent_rai = 0;
344 	rai = find_rainfo(ifi, &from);
345 	if (rai == NULL) {
346 		ELM_MALLOC(rai, exit(1));
347 		rai->rai_ifinfo = ifi;
348 		TAILQ_INIT(&rai->rai_ra_opt);
349 		rai->rai_saddr.sin6_family = AF_INET6;
350 		rai->rai_saddr.sin6_len = sizeof(rai->rai_saddr);
351 		memcpy(&rai->rai_saddr.sin6_addr, &from.sin6_addr,
352 		    sizeof(rai->rai_saddr.sin6_addr));
353 		newent_rai = 1;
354 	}
355 
356 #define	RA_OPT_NEXT_HDR(x)	(struct nd_opt_hdr *)((char *)(x) + \
357 				(((struct nd_opt_hdr *)(x))->nd_opt_len * 8))
358 	/* Process RA options. */
359 	warnmsg(LOG_DEBUG, __func__, "Processing RA");
360 	raoptp = (char *)icp + sizeof(struct nd_router_advert);
361 	while (raoptp < (char *)icp + msglen) {
362 		ndo = (struct nd_opt_hdr *)raoptp;
363 		warnmsg(LOG_DEBUG, __func__, "ndo = %p", raoptp);
364 		warnmsg(LOG_DEBUG, __func__, "ndo->nd_opt_type = %d",
365 		    ndo->nd_opt_type);
366 		warnmsg(LOG_DEBUG, __func__, "ndo->nd_opt_len = %d",
367 		    ndo->nd_opt_len);
368 
369 		if (ndo->nd_opt_len == 0) {
370 			warnmsg(LOG_INFO, __func__, "invalid option length 0.");
371 			break;
372 		}
373 		if ((char *)RA_OPT_NEXT_HDR(raoptp) > (char *)icp + msglen) {
374 			warnmsg(LOG_INFO, __func__, "option length overflow.");
375 			break;
376 		}
377 
378 		switch (ndo->nd_opt_type) {
379 		case ND_OPT_RDNSS:
380 			rdnss = (struct nd_opt_rdnss *)raoptp;
381 
382 			/*
383 			 * The option header is 8 bytes long and each address
384 			 * occupies 16 bytes, so the option length must be
385 			 * greater than or equal to 24 bytes and an odd multiple
386 			 * of 8 bytes.  See section 5.1 in RFC 6106.
387 			 */
388 			if (rdnss->nd_opt_rdnss_len < 3 ||
389 			    rdnss->nd_opt_rdnss_len % 2 == 0) {
390 				warnmsg(LOG_INFO, __func__,
391 				    "too short RDNSS option in RA from %s "
392 				    "was ignored.",
393 				inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
394 				    sizeof(ntopbuf)));
395 				break;
396 			}
397 
398 			addr = (struct in6_addr *)(void *)(raoptp + sizeof(*rdnss));
399 			while ((char *)addr < (char *)RA_OPT_NEXT_HDR(raoptp)) {
400 				if (inet_ntop(AF_INET6, addr, ntopbuf,
401 					sizeof(ntopbuf)) == NULL) {
402 					warnmsg(LOG_INFO, __func__,
403 		    			    "an invalid address in RDNSS option"
404 					    " in RA from %s was ignored.",
405 					    inet_ntop(AF_INET6, &from.sin6_addr,
406 						ntopbuf, sizeof(ntopbuf)));
407 					addr++;
408 					continue;
409 				}
410 				if (IN6_IS_ADDR_LINKLOCAL(addr))
411 					/* XXX: % has to be escaped here */
412 					l = snprintf(nsbuf, sizeof(nsbuf),
413 					    "%s%c%s", ntopbuf,
414 					    SCOPE_DELIMITER,
415 					    ifi->ifname);
416 				else
417 					l = snprintf(nsbuf, sizeof(nsbuf),
418 					    "%s", ntopbuf);
419 				if (l < 0 || (size_t)l >= sizeof(nsbuf)) {
420 					warnmsg(LOG_ERR, __func__,
421 					    "address copying error in "
422 					    "RDNSS option: %d.", l);
423 					addr++;
424 					continue;
425 				}
426 				warnmsg(LOG_DEBUG, __func__, "nsbuf = %s",
427 				    nsbuf);
428 
429 				newent_rao = 0;
430 				rao = find_raopt(rai, ndo->nd_opt_type, nsbuf,
431 				    strlen(nsbuf));
432 				if (rao == NULL) {
433 					ELM_MALLOC(rao, break);
434 					rao->rao_type = ndo->nd_opt_type;
435 					rao->rao_len = strlen(nsbuf);
436 					rao->rao_msg = strdup(nsbuf);
437 					if (rao->rao_msg == NULL) {
438 						warnmsg(LOG_ERR, __func__,
439 						    "strdup failed: %s",
440 						    strerror(errno));
441 						free(rao);
442 						addr++;
443 						continue;
444 					}
445 					newent_rao = 1;
446 				}
447 				/* Set expiration timer */
448 				memset(&rao->rao_expire, 0,
449 				    sizeof(rao->rao_expire));
450 				memset(&lifetime, 0, sizeof(lifetime));
451 				lifetime.tv_sec =
452 				    ntohl(rdnss->nd_opt_rdnss_lifetime);
453 				TS_ADD(&now, &lifetime, &rao->rao_expire);
454 
455 				if (newent_rao)
456 					TAILQ_INSERT_TAIL(&rai->rai_ra_opt,
457 					    rao, rao_next);
458 				addr++;
459 			}
460 			break;
461 		case ND_OPT_DNSSL:
462 			dnssl = (struct nd_opt_dnssl *)raoptp;
463 
464 			/* Optlen sanity check (Section 5.3.1 in RFC 6106) */
465 			if (dnssl->nd_opt_dnssl_len < 2) {
466 				warnmsg(LOG_INFO, __func__,
467 		    			"too short DNSSL option"
468 					"in RA from %s was ignored.",
469 					inet_ntop(AF_INET6, &from.sin6_addr,
470 					    ntopbuf, sizeof(ntopbuf)));
471 				break;
472 			}
473 
474 			/*
475 			 * Ensure NUL-termination in DNSSL in case of
476 			 * malformed field.
477 			 */
478 			p = (char *)RA_OPT_NEXT_HDR(raoptp);
479 			*(p - 1) = '\0';
480 
481 			p = raoptp + sizeof(*dnssl);
482 			while (1 < (len = dname_labeldec(dname, sizeof(dname),
483 			    p))) {
484 				/* length == 1 means empty string */
485 				warnmsg(LOG_DEBUG, __func__, "dname = %s",
486 				    dname);
487 
488 				newent_rao = 0;
489 				rao = find_raopt(rai, ndo->nd_opt_type, dname,
490 				    strlen(dname));
491 				if (rao == NULL) {
492 					ELM_MALLOC(rao, break);
493 					rao->rao_type = ndo->nd_opt_type;
494 					rao->rao_len = strlen(dname);
495 					rao->rao_msg = strdup(dname);
496 					if (rao->rao_msg == NULL) {
497 						warnmsg(LOG_ERR, __func__,
498 						    "strdup failed: %s",
499 						    strerror(errno));
500 						free(rao);
501 						addr++;
502 						continue;
503 					}
504 					newent_rao = 1;
505 				}
506 				/* Set expiration timer */
507 				memset(&rao->rao_expire, 0,
508 				    sizeof(rao->rao_expire));
509 				memset(&lifetime, 0, sizeof(lifetime));
510 				lifetime.tv_sec =
511 				    ntohl(dnssl->nd_opt_dnssl_lifetime);
512 				TS_ADD(&now, &lifetime, &rao->rao_expire);
513 
514 				if (newent_rao)
515 					TAILQ_INSERT_TAIL(&rai->rai_ra_opt,
516 					    rao, rao_next);
517 				p += len;
518 			}
519 			break;
520 		default:
521 			/* nothing to do for other options */
522 			break;
523 		}
524 		raoptp = (char *)RA_OPT_NEXT_HDR(raoptp);
525 	}
526 	if (newent_rai)
527 		TAILQ_INSERT_TAIL(&ifi->ifi_rainfo, rai, rai_next);
528 
529 	ra_opt_handler(ifi);
530 	ifi->racnt++;
531 
532 	switch (ifi->state) {
533 	case IFS_IDLE:		/* should be ignored */
534 	case IFS_DELAY:		/* right? */
535 		break;
536 	case IFS_PROBE:
537 		ifi->state = IFS_IDLE;
538 		ifi->probes = 0;
539 		rtsol_timer_update(ifi);
540 		break;
541 	}
542 }
543 
544 static char resstr_ns_prefix[] = "nameserver ";
545 static char resstr_sh_prefix[] = "search ";
546 static char resstr_nl[] = "\n";
547 static char resstr_sp[] = " ";
548 
549 int
550 ra_opt_handler(struct ifinfo *ifi)
551 {
552 	struct ra_opt *rao;
553 	struct rainfo *rai;
554 	struct script_msg *smp1, *smp2, *smp3;
555 	struct timespec now;
556 	struct script_msg_head_t sm_rdnss_head =
557 	    TAILQ_HEAD_INITIALIZER(sm_rdnss_head);
558 	struct script_msg_head_t sm_dnssl_head =
559 	    TAILQ_HEAD_INITIALIZER(sm_dnssl_head);
560 
561 	int dcount, dlen;
562 
563 	dcount = 0;
564 	dlen = strlen(resstr_sh_prefix) + strlen(resstr_nl);
565 	clock_gettime(CLOCK_MONOTONIC_FAST, &now);
566 
567 	/*
568 	 * All options from multiple RAs with the same or different
569 	 * source addresses on a single interface will be gathered and
570 	 * handled, not overridden.  [RFC 4861 6.3.4]
571 	 */
572 	TAILQ_FOREACH(rai, &ifi->ifi_rainfo, rai_next) {
573 		TAILQ_FOREACH(rao, &rai->rai_ra_opt, rao_next) {
574 			switch (rao->rao_type) {
575 			case ND_OPT_RDNSS:
576 				if (TS_CMP(&now, &rao->rao_expire, >)) {
577 					warnmsg(LOG_INFO, __func__,
578 					    "expired rdnss entry: %s",
579 					    (char *)rao->rao_msg);
580 					break;
581 				}
582 				ELM_MALLOC(smp1, continue);
583 				ELM_MALLOC(smp2, goto free1);
584 				ELM_MALLOC(smp3, goto free2);
585 				smp1->sm_msg = resstr_ns_prefix;
586 				TAILQ_INSERT_TAIL(&sm_rdnss_head, smp1,
587 				    sm_next);
588 				smp2->sm_msg = rao->rao_msg;
589 				TAILQ_INSERT_TAIL(&sm_rdnss_head, smp2,
590 				    sm_next);
591 				smp3->sm_msg = resstr_nl;
592 				TAILQ_INSERT_TAIL(&sm_rdnss_head, smp3,
593 				    sm_next);
594 				ifi->ifi_rdnss = IFI_DNSOPT_STATE_RECEIVED;
595 				break;
596 			case ND_OPT_DNSSL:
597 				if (TS_CMP(&now, &rao->rao_expire, >)) {
598 					warnmsg(LOG_INFO, __func__,
599 					    "expired dnssl entry: %s",
600 					    (char *)rao->rao_msg);
601 					break;
602 				}
603 				dcount++;
604 				/* Check resolv.conf(5) restrictions. */
605 				if (dcount > 6) {
606 					warnmsg(LOG_INFO, __func__,
607 					    "dnssl entry exceeding maximum count (%d>6)"
608 					    ": %s", dcount, (char *)rao->rao_msg);
609 					break;
610 				}
611 				if (256 < dlen + strlen(rao->rao_msg) +
612 				    strlen(resstr_sp)) {
613 					warnmsg(LOG_INFO, __func__,
614 					    "dnssl entry exceeding maximum length "
615 					    "(>256): %s", (char *)rao->rao_msg);
616 					break;
617 				}
618 				ELM_MALLOC(smp1, continue);
619 				ELM_MALLOC(smp2, goto free1);
620 				if (TAILQ_EMPTY(&sm_dnssl_head)) {
621 					ELM_MALLOC(smp3, goto free2);
622 					smp3->sm_msg = resstr_sh_prefix;
623 					TAILQ_INSERT_TAIL(&sm_dnssl_head, smp3,
624 					    sm_next);
625 				}
626 				smp1->sm_msg = rao->rao_msg;
627 				TAILQ_INSERT_TAIL(&sm_dnssl_head, smp1,
628 				    sm_next);
629 				smp2->sm_msg = resstr_sp;
630 				TAILQ_INSERT_TAIL(&sm_dnssl_head, smp2,
631 				    sm_next);
632 				dlen += strlen(rao->rao_msg) +
633 				    strlen(resstr_sp);
634 				ifi->ifi_dnssl = IFI_DNSOPT_STATE_RECEIVED;
635 				break;
636 			}
637 			continue;
638 free2:
639 			free(smp2);
640 free1:
641 			free(smp1);
642 		}
643 		/* Call the script for each information source. */
644 		if (uflag)
645 			ra_opt_rdnss_dispatch(ifi, rai, &sm_rdnss_head,
646 			    &sm_dnssl_head);
647 	}
648 	/* Call the script for each interface. */
649 	if (!uflag)
650 		ra_opt_rdnss_dispatch(ifi, NULL, &sm_rdnss_head,
651 		    &sm_dnssl_head);
652 	return (0);
653 }
654 
655 char *
656 make_rsid(const char *ifname, const char *origin, struct rainfo *rai)
657 {
658 	char hbuf[NI_MAXHOST];
659 
660 	if (rai == NULL)
661 		sprintf(rsid, "%s:%s", ifname, origin);
662 	else {
663 		if (!IN6_IS_ADDR_LINKLOCAL(&rai->rai_saddr.sin6_addr))
664 			return (NULL);
665 		if (getnameinfo((struct sockaddr *)&rai->rai_saddr,
666 			rai->rai_saddr.sin6_len, hbuf, sizeof(hbuf), NULL, 0,
667 			NI_NUMERICHOST) != 0)
668 			return (NULL);
669 		sprintf(rsid, "%s:%s:[%s]", ifname, origin, hbuf);
670 	}
671 	warnmsg(LOG_DEBUG, __func__, "rsid = [%s]", rsid);
672 	return (rsid);
673 }
674 
675 int
676 ra_opt_rdnss_dispatch(struct ifinfo *ifi, struct rainfo *rai,
677     struct script_msg_head_t *sm_rdnss_head,
678     struct script_msg_head_t *sm_dnssl_head)
679 {
680 	struct script_msg *smp1;
681 	const char *r;
682 	int error;
683 
684 	error = 0;
685 	/* Add \n for DNSSL list. */
686 	if (!TAILQ_EMPTY(sm_dnssl_head)) {
687 		ELM_MALLOC(smp1, goto ra_opt_rdnss_freeit);
688 		smp1->sm_msg = resstr_nl;
689 		TAILQ_INSERT_TAIL(sm_dnssl_head, smp1, sm_next);
690 	}
691 	TAILQ_CONCAT(sm_rdnss_head, sm_dnssl_head, sm_next);
692 
693 	r = make_rsid(ifi->ifname, DNSINFO_ORIGIN_LABEL, uflag ? rai : NULL);
694 	if (r == NULL) {
695 		warnmsg(LOG_ERR, __func__, "make_rsid() failed.  "
696 		    "Script was not invoked.");
697 		error = 1;
698 		goto ra_opt_rdnss_freeit;
699 	}
700 	if (!TAILQ_EMPTY(sm_rdnss_head))
701 		CALL_SCRIPT(RESADD, sm_rdnss_head);
702 	else if (ifi->ifi_rdnss == IFI_DNSOPT_STATE_RECEIVED ||
703 	    ifi->ifi_dnssl == IFI_DNSOPT_STATE_RECEIVED) {
704 		CALL_SCRIPT(RESDEL, NULL);
705 		ifi->ifi_rdnss = IFI_DNSOPT_STATE_NOINFO;
706 		ifi->ifi_dnssl = IFI_DNSOPT_STATE_NOINFO;
707 	}
708 
709 ra_opt_rdnss_freeit:
710 	/* Clear script message queue. */
711 	if (!TAILQ_EMPTY(sm_rdnss_head)) {
712 		while ((smp1 = TAILQ_FIRST(sm_rdnss_head)) != NULL) {
713 			TAILQ_REMOVE(sm_rdnss_head, smp1, sm_next);
714 			free(smp1);
715 		}
716 	}
717 	if (!TAILQ_EMPTY(sm_dnssl_head)) {
718 		while ((smp1 = TAILQ_FIRST(sm_dnssl_head)) != NULL) {
719 			TAILQ_REMOVE(sm_dnssl_head, smp1, sm_next);
720 			free(smp1);
721 		}
722 	}
723 	return (error);
724 }
725 
726 static struct ra_opt *
727 find_raopt(struct rainfo *rai, int type, void *msg, size_t len)
728 {
729 	struct ra_opt *rao;
730 
731 	TAILQ_FOREACH(rao, &rai->rai_ra_opt, rao_next) {
732 		if (rao->rao_type == type &&
733 		    rao->rao_len == strlen(msg) &&
734 		    memcmp(rao->rao_msg, msg, len) == 0)
735 			break;
736 	}
737 
738 	return (rao);
739 }
740 
741 static void
742 call_script(const char *const argv[], struct script_msg_head_t *sm_head)
743 {
744 	struct script_msg *smp;
745 	ssize_t len;
746 	int status, wfd;
747 
748 	if (argv[0] == NULL)
749 		return;
750 
751 	wfd = cap_script_run(capscript, argv);
752 	if (wfd == -1) {
753 		warnmsg(LOG_ERR, __func__,
754 		    "failed to run %s: %s", argv[0], strerror(errno));
755 		return;
756 	}
757 
758 	if (sm_head != NULL) {
759 		TAILQ_FOREACH(smp, sm_head, sm_next) {
760 			len = strlen(smp->sm_msg);
761 			warnmsg(LOG_DEBUG, __func__, "write to child = %s(%zd)",
762 			    smp->sm_msg, len);
763 			if (write(wfd, smp->sm_msg, len) != len) {
764 				warnmsg(LOG_ERR, __func__,
765 				    "write to child failed: %s",
766 				    strerror(errno));
767 				break;
768 			}
769 		}
770 	}
771 
772 	(void)close(wfd);
773 
774 	if (cap_script_wait(capscript, &status) != 0)
775 		warnmsg(LOG_ERR, __func__, "wait(): %s", strerror(errno));
776 	else
777 		warnmsg(LOG_DEBUG, __func__, "script \"%s\" status %d",
778 		    argv[0], status);
779 }
780 
781 /* Decode domain name label encoding in RFC 1035 Section 3.1 */
782 static size_t
783 dname_labeldec(char *dst, size_t dlen, const char *src)
784 {
785 	size_t len;
786 	const char *src_origin;
787 	const char *src_last;
788 	const char *dst_origin;
789 
790 	src_origin = src;
791 	src_last = strchr(src, '\0');
792 	dst_origin = dst;
793 	memset(dst, '\0', dlen);
794 	while ((len = (*src++) & 0x3f) &&
795 	    src + len <= src_last &&
796 	    len + (dst == dst_origin ? 0 : 1) < dlen) {
797 		if (dst != dst_origin) {
798 			*dst++ = '.';
799 			dlen--;
800 		}
801 		warnmsg(LOG_DEBUG, __func__, "labellen = %zd", len);
802 		memcpy(dst, src, len);
803 		src += len;
804 		dst += len;
805 		dlen -= len;
806 	}
807 	*dst = '\0';
808 
809 	/*
810 	 * XXX validate that domain name only contains valid characters
811 	 * for two reasons: 1) correctness, 2) we do not want to pass
812 	 * possible malicious, unescaped characters like `` to a script
813 	 * or program that could be exploited that way.
814 	 */
815 
816 	return (src - src_origin);
817 }
818