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