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