1 /* $KAME: rtsol.c,v 1.27 2003/10/05 00:09:36 itojun Exp $ */
4 * SPDX-License-Identifier: BSD-3-Clause
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * Copyright (C) 2011 Hiroki Sato
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
37 #include <sys/param.h>
38 #include <sys/socket.h>
40 #include <sys/queue.h>
45 #include <net/route.h>
46 #include <net/if_dl.h>
48 #define __BSD_VISIBLE 1 /* IN6ADDR_LINKLOCAL_ALLROUTERS_INIT */
49 #include <netinet/in.h>
51 #include <netinet/ip6.h>
52 #include <netinet6/ip6_var.h>
53 #include <netinet/icmp6.h>
55 #include <arpa/inet.h>
70 static struct msghdr rcvmhdr;
71 static struct msghdr sndmhdr;
72 static struct iovec rcviov[2];
73 static struct iovec sndiov[2];
74 static struct sockaddr_in6 from;
75 static int rcvcmsglen;
78 static char rsid[IFNAMSIZ + 1 + sizeof(DNSINFO_ORIGIN_LABEL) + 1 + NI_MAXHOST];
79 struct ifinfo_head_t ifinfo_head =
80 TAILQ_HEAD_INITIALIZER(ifinfo_head);
82 static const struct sockaddr_in6 sin6_allrouters = {
83 .sin6_len = sizeof(sin6_allrouters),
84 .sin6_family = AF_INET6,
85 .sin6_addr = IN6ADDR_LINKLOCAL_ALLROUTERS_INIT,
88 static void call_script(const int, const char *const *,
89 struct script_msg_head_t *);
90 static size_t dname_labeldec(char *, size_t, const char *);
91 static int safefile(const char *);
92 static struct ra_opt *find_raopt(struct rainfo *, int, void *, size_t);
93 static int ra_opt_rdnss_dispatch(struct ifinfo *, struct rainfo *,
94 struct script_msg_head_t *, struct script_msg_head_t *);
95 static char *make_rsid(const char *, const char *, struct rainfo *);
97 #define _ARGS_OTHER otherconf_script, ifi->ifname
98 #define _ARGS_RESADD resolvconf_script, "-a", rsid
99 #define _ARGS_RESDEL resolvconf_script, "-d", rsid
101 #define CALL_SCRIPT(name, sm_head) \
103 const char *const sarg[] = { _ARGS_##name, NULL }; \
104 call_script(sizeof(sarg), sarg, sm_head); \
107 #define ELM_MALLOC(p,error_action) \
109 p = malloc(sizeof(*p)); \
111 warnmsg(LOG_ERR, __func__, "malloc failed: %s", \
115 memset(p, 0, sizeof(*p)); \
121 static u_char *rcvcmsgbuf = NULL, *sndcmsgbuf = NULL;
123 static u_char answer[1500];
124 struct icmp6_filter filt;
126 sndcmsglen = rcvcmsglen = CMSG_SPACE(sizeof(struct in6_pktinfo)) +
127 CMSG_SPACE(sizeof(int));
128 if (rcvcmsgbuf == NULL && (rcvcmsgbuf = malloc(rcvcmsglen)) == NULL) {
129 warnmsg(LOG_ERR, __func__,
130 "malloc for receive msghdr failed");
133 if (sndcmsgbuf == NULL && (sndcmsgbuf = malloc(sndcmsglen)) == NULL) {
134 warnmsg(LOG_ERR, __func__,
135 "malloc for send msghdr failed");
138 if ((rssock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0) {
139 warnmsg(LOG_ERR, __func__, "socket: %s", strerror(errno));
143 /* specify to tell receiving interface */
145 if (setsockopt(rssock, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on,
147 warnmsg(LOG_ERR, __func__, "IPV6_RECVPKTINFO: %s",
152 /* specify to tell value of hoplimit field of received IP6 hdr */
154 if (setsockopt(rssock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &on,
156 warnmsg(LOG_ERR, __func__, "IPV6_RECVHOPLIMIT: %s",
161 /* specfiy to accept only router advertisements on the socket */
162 ICMP6_FILTER_SETBLOCKALL(&filt);
163 ICMP6_FILTER_SETPASS(ND_ROUTER_ADVERT, &filt);
164 if (setsockopt(rssock, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
165 sizeof(filt)) == -1) {
166 warnmsg(LOG_ERR, __func__, "setsockopt(ICMP6_FILTER): %s",
171 /* initialize msghdr for receiving packets */
172 rcviov[0].iov_base = (caddr_t)answer;
173 rcviov[0].iov_len = sizeof(answer);
174 rcvmhdr.msg_name = (caddr_t)&from;
175 rcvmhdr.msg_iov = rcviov;
176 rcvmhdr.msg_iovlen = 1;
177 rcvmhdr.msg_control = (caddr_t) rcvcmsgbuf;
179 /* initialize msghdr for sending packets */
180 sndmhdr.msg_namelen = sizeof(struct sockaddr_in6);
181 sndmhdr.msg_iov = sndiov;
182 sndmhdr.msg_iovlen = 1;
183 sndmhdr.msg_control = (caddr_t)sndcmsgbuf;
184 sndmhdr.msg_controllen = sndcmsglen;
190 sendpacket(struct ifinfo *ifi)
192 struct in6_pktinfo *pi;
196 struct sockaddr_in6 dst;
198 dst = sin6_allrouters;
199 dst.sin6_scope_id = ifi->linkid;
201 sndmhdr.msg_name = (caddr_t)&dst;
202 sndmhdr.msg_iov[0].iov_base = (caddr_t)ifi->rs_data;
203 sndmhdr.msg_iov[0].iov_len = ifi->rs_datalen;
205 cm = CMSG_FIRSTHDR(&sndmhdr);
206 /* specify the outgoing interface */
207 cm->cmsg_level = IPPROTO_IPV6;
208 cm->cmsg_type = IPV6_PKTINFO;
209 cm->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
210 pi = (struct in6_pktinfo *)(void *)CMSG_DATA(cm);
211 memset(&pi->ipi6_addr, 0, sizeof(pi->ipi6_addr)); /*XXX*/
212 pi->ipi6_ifindex = ifi->sdl->sdl_index;
214 /* specify the hop limit of the packet */
215 cm = CMSG_NXTHDR(&sndmhdr, cm);
216 cm->cmsg_level = IPPROTO_IPV6;
217 cm->cmsg_type = IPV6_HOPLIMIT;
218 cm->cmsg_len = CMSG_LEN(sizeof(int));
219 memcpy(CMSG_DATA(cm), &hoplimit, sizeof(int));
221 warnmsg(LOG_DEBUG, __func__,
222 "send RS on %s, whose state is %d",
223 ifi->ifname, ifi->state);
224 i = sendmsg(rssock, &sndmhdr, 0);
225 if (i < 0 || (size_t)i != ifi->rs_datalen) {
227 * ENETDOWN is not so serious, especially when using several
228 * network cards on a mobile node. We ignore it.
230 if (errno != ENETDOWN || dflag > 0)
231 warnmsg(LOG_ERR, __func__, "sendmsg on %s: %s",
232 ifi->ifname, strerror(errno));
242 char ntopbuf[INET6_ADDRSTRLEN], ifnamebuf[IFNAMSIZ];
243 int l, ifindex = 0, *hlimp = NULL;
245 struct in6_pktinfo *pi = NULL;
246 struct ifinfo *ifi = NULL;
247 struct ra_opt *rao = NULL;
248 struct icmp6_hdr *icp;
249 struct nd_router_advert *nd_ra;
254 struct in6_addr *addr;
255 struct nd_opt_hdr *ndo;
256 struct nd_opt_rdnss *rdnss;
257 struct nd_opt_dnssl *dnssl;
259 char nsbuf[INET6_ADDRSTRLEN + 1 + IFNAMSIZ + 1];
260 char dname[NI_MAXHOST];
262 struct timespec lifetime;
266 /* get message. namelen and controllen must always be initialized. */
267 rcvmhdr.msg_namelen = sizeof(from);
268 rcvmhdr.msg_controllen = rcvcmsglen;
269 if ((msglen = recvmsg(s, &rcvmhdr, 0)) < 0) {
270 warnmsg(LOG_ERR, __func__, "recvmsg: %s", strerror(errno));
274 /* extract optional information via Advanced API */
275 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(&rcvmhdr); cm;
276 cm = (struct cmsghdr *)CMSG_NXTHDR(&rcvmhdr, cm)) {
277 if (cm->cmsg_level == IPPROTO_IPV6 &&
278 cm->cmsg_type == IPV6_PKTINFO &&
279 cm->cmsg_len == CMSG_LEN(sizeof(struct in6_pktinfo))) {
280 pi = (struct in6_pktinfo *)(void *)(CMSG_DATA(cm));
281 ifindex = pi->ipi6_ifindex;
283 if (cm->cmsg_level == IPPROTO_IPV6 &&
284 cm->cmsg_type == IPV6_HOPLIMIT &&
285 cm->cmsg_len == CMSG_LEN(sizeof(int)))
286 hlimp = (int *)(void *)CMSG_DATA(cm);
290 warnmsg(LOG_ERR, __func__,
291 "failed to get receiving interface");
295 warnmsg(LOG_ERR, __func__,
296 "failed to get receiving hop limit");
300 if ((size_t)msglen < sizeof(struct nd_router_advert)) {
301 warnmsg(LOG_INFO, __func__,
302 "packet size(%zd) is too short", msglen);
306 icp = (struct icmp6_hdr *)rcvmhdr.msg_iov[0].iov_base;
308 if (icp->icmp6_type != ND_ROUTER_ADVERT) {
310 * this should not happen because we configured a filter
311 * that only passes RAs on the receiving socket.
313 warnmsg(LOG_ERR, __func__,
314 "invalid icmp type(%d) from %s on %s", icp->icmp6_type,
315 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
317 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
321 if (icp->icmp6_code != 0) {
322 warnmsg(LOG_INFO, __func__,
323 "invalid icmp code(%d) from %s on %s", icp->icmp6_code,
324 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
326 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
331 warnmsg(LOG_INFO, __func__,
332 "invalid RA with hop limit(%d) from %s on %s",
334 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
336 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
340 if (pi && !IN6_IS_ADDR_LINKLOCAL(&from.sin6_addr)) {
341 warnmsg(LOG_INFO, __func__,
342 "invalid RA with non link-local source from %s on %s",
343 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
345 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
349 /* xxx: more validation? */
351 if ((ifi = find_ifinfo(pi->ipi6_ifindex)) == NULL) {
352 warnmsg(LOG_DEBUG, __func__,
353 "received RA from %s on an unexpected IF(%s)",
354 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
356 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
360 warnmsg(LOG_DEBUG, __func__,
361 "received RA from %s on %s, state is %d",
362 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf, sizeof(ntopbuf)),
363 ifi->ifname, ifi->state);
365 nd_ra = (struct nd_router_advert *)icp;
368 * Process the "O bit."
369 * If the value of OtherConfigFlag changes from FALSE to TRUE, the
370 * host should invoke the stateful autoconfiguration protocol,
371 * requesting information.
372 * [RFC 2462 Section 5.5.3]
374 if (((nd_ra->nd_ra_flags_reserved) & ND_RA_FLAG_OTHER) &&
376 warnmsg(LOG_DEBUG, __func__,
377 "OtherConfigFlag on %s is turned on", ifi->ifname);
378 ifi->otherconfig = 1;
379 CALL_SCRIPT(OTHER, NULL);
381 clock_gettime(CLOCK_MONOTONIC_FAST, &now);
383 rai = find_rainfo(ifi, &from);
385 ELM_MALLOC(rai, exit(1));
386 rai->rai_ifinfo = ifi;
387 TAILQ_INIT(&rai->rai_ra_opt);
388 rai->rai_saddr.sin6_family = AF_INET6;
389 rai->rai_saddr.sin6_len = sizeof(rai->rai_saddr);
390 memcpy(&rai->rai_saddr.sin6_addr, &from.sin6_addr,
391 sizeof(rai->rai_saddr.sin6_addr));
395 #define RA_OPT_NEXT_HDR(x) (struct nd_opt_hdr *)((char *)x + \
396 (((struct nd_opt_hdr *)x)->nd_opt_len * 8))
397 /* Process RA options. */
398 warnmsg(LOG_DEBUG, __func__, "Processing RA");
399 raoptp = (char *)icp + sizeof(struct nd_router_advert);
400 while (raoptp < (char *)icp + msglen) {
401 ndo = (struct nd_opt_hdr *)raoptp;
402 warnmsg(LOG_DEBUG, __func__, "ndo = %p", raoptp);
403 warnmsg(LOG_DEBUG, __func__, "ndo->nd_opt_type = %d",
405 warnmsg(LOG_DEBUG, __func__, "ndo->nd_opt_len = %d",
408 switch (ndo->nd_opt_type) {
410 rdnss = (struct nd_opt_rdnss *)raoptp;
412 /* Optlen sanity check (Section 5.3.1 in RFC 6106) */
413 if (rdnss->nd_opt_rdnss_len < 3) {
414 warnmsg(LOG_INFO, __func__,
415 "too short RDNSS option"
416 "in RA from %s was ignored.",
417 inet_ntop(AF_INET6, &from.sin6_addr,
418 ntopbuf, sizeof(ntopbuf)));
422 addr = (struct in6_addr *)(void *)(raoptp + sizeof(*rdnss));
423 while ((char *)addr < (char *)RA_OPT_NEXT_HDR(raoptp)) {
424 if (inet_ntop(AF_INET6, addr, ntopbuf,
425 sizeof(ntopbuf)) == NULL) {
426 warnmsg(LOG_INFO, __func__,
427 "an invalid address in RDNSS option"
428 " in RA from %s was ignored.",
429 inet_ntop(AF_INET6, &from.sin6_addr,
430 ntopbuf, sizeof(ntopbuf)));
434 if (IN6_IS_ADDR_LINKLOCAL(addr))
435 /* XXX: % has to be escaped here */
436 l = snprintf(nsbuf, sizeof(nsbuf),
441 l = snprintf(nsbuf, sizeof(nsbuf),
443 if (l < 0 || (size_t)l >= sizeof(nsbuf)) {
444 warnmsg(LOG_ERR, __func__,
445 "address copying error in "
446 "RDNSS option: %d.", l);
450 warnmsg(LOG_DEBUG, __func__, "nsbuf = %s",
454 rao = find_raopt(rai, ndo->nd_opt_type, nsbuf,
457 ELM_MALLOC(rao, break);
458 rao->rao_type = ndo->nd_opt_type;
459 rao->rao_len = strlen(nsbuf);
460 rao->rao_msg = strdup(nsbuf);
461 if (rao->rao_msg == NULL) {
462 warnmsg(LOG_ERR, __func__,
471 /* Set expiration timer */
472 memset(&rao->rao_expire, 0,
473 sizeof(rao->rao_expire));
474 memset(&lifetime, 0, sizeof(lifetime));
476 ntohl(rdnss->nd_opt_rdnss_lifetime);
477 TS_ADD(&now, &lifetime, &rao->rao_expire);
480 TAILQ_INSERT_TAIL(&rai->rai_ra_opt,
486 dnssl = (struct nd_opt_dnssl *)raoptp;
488 /* Optlen sanity check (Section 5.3.1 in RFC 6106) */
489 if (dnssl->nd_opt_dnssl_len < 2) {
490 warnmsg(LOG_INFO, __func__,
491 "too short DNSSL option"
492 "in RA from %s was ignored.",
493 inet_ntop(AF_INET6, &from.sin6_addr,
494 ntopbuf, sizeof(ntopbuf)));
499 * Ensure NUL-termination in DNSSL in case of
502 p = (char *)RA_OPT_NEXT_HDR(raoptp);
505 p = raoptp + sizeof(*dnssl);
506 while (1 < (len = dname_labeldec(dname, sizeof(dname),
508 /* length == 1 means empty string */
509 warnmsg(LOG_DEBUG, __func__, "dname = %s",
513 rao = find_raopt(rai, ndo->nd_opt_type, dname,
516 ELM_MALLOC(rao, break);
517 rao->rao_type = ndo->nd_opt_type;
518 rao->rao_len = strlen(dname);
519 rao->rao_msg = strdup(dname);
520 if (rao->rao_msg == NULL) {
521 warnmsg(LOG_ERR, __func__,
530 /* Set expiration timer */
531 memset(&rao->rao_expire, 0,
532 sizeof(rao->rao_expire));
533 memset(&lifetime, 0, sizeof(lifetime));
535 ntohl(dnssl->nd_opt_dnssl_lifetime);
536 TS_ADD(&now, &lifetime, &rao->rao_expire);
539 TAILQ_INSERT_TAIL(&rai->rai_ra_opt,
545 /* nothing to do for other options */
548 raoptp = (char *)RA_OPT_NEXT_HDR(raoptp);
551 TAILQ_INSERT_TAIL(&ifi->ifi_rainfo, rai, rai_next);
556 switch (ifi->state) {
557 case IFS_IDLE: /* should be ignored */
558 case IFS_DELAY: /* right? */
561 ifi->state = IFS_IDLE;
563 rtsol_timer_update(ifi);
568 static char resstr_ns_prefix[] = "nameserver ";
569 static char resstr_sh_prefix[] = "search ";
570 static char resstr_nl[] = "\n";
571 static char resstr_sp[] = " ";
574 ra_opt_handler(struct ifinfo *ifi)
578 struct script_msg *smp1, *smp2, *smp3;
580 struct script_msg_head_t sm_rdnss_head =
581 TAILQ_HEAD_INITIALIZER(sm_rdnss_head);
582 struct script_msg_head_t sm_dnssl_head =
583 TAILQ_HEAD_INITIALIZER(sm_dnssl_head);
588 dlen = strlen(resstr_sh_prefix) + strlen(resstr_nl);
589 clock_gettime(CLOCK_MONOTONIC_FAST, &now);
592 * All options from multiple RAs with the same or different
593 * source addresses on a single interface will be gathered and
594 * handled, not overridden. [RFC 4861 6.3.4]
596 TAILQ_FOREACH(rai, &ifi->ifi_rainfo, rai_next) {
597 TAILQ_FOREACH(rao, &rai->rai_ra_opt, rao_next) {
598 switch (rao->rao_type) {
600 if (TS_CMP(&now, &rao->rao_expire, >)) {
601 warnmsg(LOG_INFO, __func__,
602 "expired rdnss entry: %s",
603 (char *)rao->rao_msg);
606 ELM_MALLOC(smp1, continue);
607 ELM_MALLOC(smp2, goto free1);
608 ELM_MALLOC(smp3, goto free2);
609 smp1->sm_msg = resstr_ns_prefix;
610 TAILQ_INSERT_TAIL(&sm_rdnss_head, smp1,
612 smp2->sm_msg = rao->rao_msg;
613 TAILQ_INSERT_TAIL(&sm_rdnss_head, smp2,
615 smp3->sm_msg = resstr_nl;
616 TAILQ_INSERT_TAIL(&sm_rdnss_head, smp3,
618 ifi->ifi_rdnss = IFI_DNSOPT_STATE_RECEIVED;
622 if (TS_CMP(&now, &rao->rao_expire, >)) {
623 warnmsg(LOG_INFO, __func__,
624 "expired dnssl entry: %s",
625 (char *)rao->rao_msg);
629 /* Check resolv.conf(5) restrictions. */
631 warnmsg(LOG_INFO, __func__,
632 "dnssl entry exceeding maximum count (%d>6)"
633 ": %s", dcount, (char *)rao->rao_msg);
636 if (256 < dlen + strlen(rao->rao_msg) +
638 warnmsg(LOG_INFO, __func__,
639 "dnssl entry exceeding maximum length "
640 "(>256): %s", (char *)rao->rao_msg);
643 ELM_MALLOC(smp1, continue);
644 ELM_MALLOC(smp2, goto free1);
645 if (TAILQ_EMPTY(&sm_dnssl_head)) {
646 ELM_MALLOC(smp3, goto free2);
647 smp3->sm_msg = resstr_sh_prefix;
648 TAILQ_INSERT_TAIL(&sm_dnssl_head, smp3,
651 smp1->sm_msg = rao->rao_msg;
652 TAILQ_INSERT_TAIL(&sm_dnssl_head, smp1,
654 smp2->sm_msg = resstr_sp;
655 TAILQ_INSERT_TAIL(&sm_dnssl_head, smp2,
657 dlen += strlen(rao->rao_msg) +
661 ifi->ifi_dnssl = IFI_DNSOPT_STATE_RECEIVED;
671 /* Call the script for each information source. */
673 ra_opt_rdnss_dispatch(ifi, rai, &sm_rdnss_head,
676 /* Call the script for each interface. */
678 ra_opt_rdnss_dispatch(ifi, NULL, &sm_rdnss_head,
684 make_rsid(const char *ifname, const char *origin, struct rainfo *rai)
686 char hbuf[NI_MAXHOST];
689 sprintf(rsid, "%s:%s", ifname, origin);
691 if (!IN6_IS_ADDR_LINKLOCAL(&rai->rai_saddr.sin6_addr))
693 if (getnameinfo((struct sockaddr *)&rai->rai_saddr,
694 rai->rai_saddr.sin6_len, hbuf, sizeof(hbuf), NULL, 0,
695 NI_NUMERICHOST) != 0)
697 sprintf(rsid, "%s:%s:[%s]", ifname, origin, hbuf);
699 warnmsg(LOG_DEBUG, __func__, "rsid = [%s]", rsid);
704 ra_opt_rdnss_dispatch(struct ifinfo *ifi,
706 struct script_msg_head_t *sm_rdnss_head,
707 struct script_msg_head_t *sm_dnssl_head)
710 struct script_msg *smp1;
714 /* Add \n for DNSSL list. */
715 if (!TAILQ_EMPTY(sm_dnssl_head)) {
716 ELM_MALLOC(smp1, goto ra_opt_rdnss_freeit);
717 smp1->sm_msg = resstr_nl;
718 TAILQ_INSERT_TAIL(sm_dnssl_head, smp1, sm_next);
720 TAILQ_CONCAT(sm_rdnss_head, sm_dnssl_head, sm_next);
722 if (rai != NULL && uflag)
723 r = make_rsid(ifi->ifname, DNSINFO_ORIGIN_LABEL, rai);
725 r = make_rsid(ifi->ifname, DNSINFO_ORIGIN_LABEL, NULL);
727 warnmsg(LOG_ERR, __func__, "make_rsid() failed. "
728 "Script was not invoked.");
730 goto ra_opt_rdnss_freeit;
732 if (!TAILQ_EMPTY(sm_rdnss_head))
733 CALL_SCRIPT(RESADD, sm_rdnss_head);
734 else if (ifi->ifi_rdnss == IFI_DNSOPT_STATE_RECEIVED ||
735 ifi->ifi_dnssl == IFI_DNSOPT_STATE_RECEIVED) {
736 CALL_SCRIPT(RESDEL, NULL);
737 ifi->ifi_rdnss = IFI_DNSOPT_STATE_NOINFO;
738 ifi->ifi_dnssl = IFI_DNSOPT_STATE_NOINFO;
742 /* Clear script message queue. */
743 if (!TAILQ_EMPTY(sm_rdnss_head)) {
744 while ((smp1 = TAILQ_FIRST(sm_rdnss_head)) != NULL) {
745 TAILQ_REMOVE(sm_rdnss_head, smp1, sm_next);
749 if (!TAILQ_EMPTY(sm_dnssl_head)) {
750 while ((smp1 = TAILQ_FIRST(sm_dnssl_head)) != NULL) {
751 TAILQ_REMOVE(sm_dnssl_head, smp1, sm_next);
758 static struct ra_opt *
759 find_raopt(struct rainfo *rai, int type, void *msg, size_t len)
763 TAILQ_FOREACH(rao, &rai->rai_ra_opt, rao_next) {
764 if (rao->rao_type == type &&
765 rao->rao_len == strlen(msg) &&
766 memcmp(rao->rao_msg, msg, len) == 0)
774 call_script(const int argc, const char *const argv[],
775 struct script_msg_head_t *sm_head)
777 const char *scriptpath;
782 if ((scriptpath = argv[0]) == NULL)
786 if (sm_head != NULL && !TAILQ_EMPTY(sm_head)) {
789 warnmsg(LOG_ERR, __func__,
790 "failed to create a pipe: %s", strerror(errno));
795 /* launch the script */
798 warnmsg(LOG_ERR, __func__,
799 "failed to fork: %s", strerror(errno));
801 } else if (pid) { /* parent */
804 if (fd[0] != -1) { /* Send message to the child if any. */
806 struct script_msg *smp;
809 TAILQ_FOREACH(smp, sm_head, sm_next) {
810 len = strlen(smp->sm_msg);
811 warnmsg(LOG_DEBUG, __func__,
812 "write to child = %s(%zd)",
814 if (write(fd[1], smp->sm_msg, len) != len) {
815 warnmsg(LOG_ERR, __func__,
816 "write to child failed: %s",
824 wpid = wait(&wstatus);
825 } while (wpid != pid && wpid > 0);
828 warnmsg(LOG_ERR, __func__,
829 "wait: %s", strerror(errno));
831 warnmsg(LOG_DEBUG, __func__,
832 "script \"%s\" terminated", scriptpath);
837 if (safefile(scriptpath)) {
838 warnmsg(LOG_ERR, __func__,
839 "script \"%s\" cannot be executed safely",
843 nullfd = open("/dev/null", O_RDWR);
845 warnmsg(LOG_ERR, __func__,
846 "open /dev/null: %s", strerror(errno));
849 if (fd[0] != -1) { /* Receive message from STDIN if any. */
851 if (fd[0] != STDIN_FILENO) {
852 /* Connect a pipe read-end to child's STDIN. */
853 if (dup2(fd[0], STDIN_FILENO) != STDIN_FILENO) {
854 warnmsg(LOG_ERR, __func__,
855 "dup2 STDIN: %s", strerror(errno));
861 dup2(nullfd, STDIN_FILENO);
863 dup2(nullfd, STDOUT_FILENO);
864 dup2(nullfd, STDERR_FILENO);
865 if (nullfd > STDERR_FILENO)
868 _argv = malloc(sizeof(*_argv) * argc);
870 warnmsg(LOG_ERR, __func__,
871 "malloc: %s", strerror(errno));
874 memcpy(_argv, argv, (size_t)argc);
875 execv(scriptpath, (char *const *)_argv);
876 warnmsg(LOG_ERR, __func__, "child: exec failed: %s",
885 safefile(const char *path)
891 if (getuid() != geteuid()) {
892 warnmsg(LOG_NOTICE, __func__,
893 "setuid'ed execution not allowed\n");
897 if (lstat(path, &s) != 0) {
898 warnmsg(LOG_NOTICE, __func__, "lstat failed: %s",
903 /* the file must be owned by the running uid */
905 if (s.st_uid != myuid) {
906 warnmsg(LOG_NOTICE, __func__,
907 "%s has invalid owner uid\n", path);
911 switch (s.st_mode & S_IFMT) {
915 warnmsg(LOG_NOTICE, __func__,
916 "%s is an invalid file type 0x%o\n",
917 path, (s.st_mode & S_IFMT));
924 /* Decode domain name label encoding in RFC 1035 Section 3.1 */
926 dname_labeldec(char *dst, size_t dlen, const char *src)
929 const char *src_origin;
930 const char *src_last;
931 const char *dst_origin;
934 src_last = strchr(src, '\0');
936 memset(dst, '\0', dlen);
937 while (src && (len = (uint8_t)(*src++) & 0x3f) &&
938 (src + len) <= src_last &&
939 (dst - dst_origin < (ssize_t)dlen)) {
940 if (dst != dst_origin)
942 warnmsg(LOG_DEBUG, __func__, "labellen = %zd", len);
943 memcpy(dst, src, len);
950 * XXX validate that domain name only contains valid characters
951 * for two reasons: 1) correctness, 2) we do not want to pass
952 * possible malicious, unescaped characters like `` to a script
953 * or program that could be exploited that way.
956 return (src - src_origin);