1 /* $KAME: config.c,v 1.84 2003/08/05 12:34:23 itojun Exp $ */ 2 3 /*- 4 * SPDX-License-Identifier: BSD-3-Clause 5 * 6 * Copyright (C) 1998 WIDE Project. 7 * Copyright (C) 2011 Hiroki Sato <hrs@FreeBSD.org> 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 35 #include <sys/param.h> 36 #include <sys/ioctl.h> 37 #include <sys/socket.h> 38 39 #include <net/if.h> 40 #include <net/route.h> 41 #include <net/if_dl.h> 42 43 #include <netinet/in.h> 44 #include <netinet/in_var.h> 45 #include <netinet/ip6.h> 46 #include <netinet6/ip6_var.h> 47 #include <netinet/icmp6.h> 48 #include <netinet6/nd6.h> 49 50 #include <arpa/inet.h> 51 52 #include <stdio.h> 53 #include <syslog.h> 54 #include <errno.h> 55 #include <inttypes.h> 56 #include <netdb.h> 57 #include <string.h> 58 #include <search.h> 59 #include <stdlib.h> 60 #include <time.h> 61 #include <unistd.h> 62 #include <ifaddrs.h> 63 64 #include "rtadvd.h" 65 #include "advcap.h" 66 #include "timer.h" 67 #include "if.h" 68 #include "config.h" 69 70 /* label of tcapcode + number + domain name + zero octet */ 71 static char entbuf[10 + 3 + NI_MAXHOST + 1]; 72 static char oentbuf[10 + 3 + NI_MAXHOST + 1]; 73 static char abuf[DNAME_LABELENC_MAXLEN]; 74 75 static time_t prefix_timo = (60 * 120); /* 2 hours. 76 * XXX: should be configurable. */ 77 78 static struct rtadvd_timer *prefix_timeout(void *); 79 static void makeentry(char *, size_t, int, const char *); 80 static ssize_t dname_labelenc(char *, const char *); 81 82 /* Encode domain name label encoding in RFC 1035 Section 3.1 */ 83 static ssize_t 84 dname_labelenc(char *dst, const char *src) 85 { 86 char *dst_origin; 87 char *p; 88 size_t len; 89 90 dst_origin = dst; 91 len = strlen(src); 92 93 if (len + len / 64 + 1 + 1 > DNAME_LABELENC_MAXLEN) 94 return (-1); 95 /* Length fields per 63 octets + '\0' (<= DNAME_LABELENC_MAXLEN) */ 96 memset(dst, 0, len + len / 64 + 1 + 1); 97 98 syslog(LOG_DEBUG, "<%s> labelenc = %s", __func__, src); 99 while (src && (len = strlen(src)) != 0) { 100 /* Put a length field with 63 octet limitation first. */ 101 p = strchr(src, '.'); 102 if (p == NULL) 103 *dst = len = MIN(63, len); 104 else 105 *dst = len = MIN(63, p - src); 106 if (dst + 1 + len < dst_origin + DNAME_LABELENC_MAXLEN) 107 dst++; 108 else 109 return (-1); 110 /* Copy 63 octets at most. */ 111 memcpy(dst, src, len); 112 dst += len; 113 if (p == NULL) /* the last label */ 114 break; 115 src = p + 1; 116 } 117 /* Always need a 0-length label at the tail. */ 118 *dst++ = '\0'; 119 120 syslog(LOG_DEBUG, "<%s> labellen = %td", __func__, dst - dst_origin); 121 return (dst - dst_origin); 122 } 123 124 #define MUSTHAVE(var, cap) \ 125 do { \ 126 int64_t t; \ 127 if ((t = agetnum(cap)) < 0) { \ 128 fprintf(stderr, "rtadvd: need %s for interface %s\n", \ 129 cap, intface); \ 130 exit(1); \ 131 } \ 132 var = t; \ 133 } while (0) 134 135 #define MAYHAVE(var, cap, def) \ 136 do { \ 137 if ((var = agetnum(cap)) < 0) \ 138 var = def; \ 139 } while (0) 140 141 int 142 loadconfig_index(int idx) 143 { 144 char ifname[IFNAMSIZ]; 145 146 syslog(LOG_DEBUG, "<%s> enter", __func__); 147 148 if (if_indextoname(idx, ifname) != NULL) 149 return (loadconfig_ifname(ifname)); 150 else 151 return (1); 152 } 153 154 int 155 loadconfig_ifname(char *ifname) 156 { 157 struct ifinfo *ifi; 158 159 syslog(LOG_DEBUG, "<%s> enter", __func__); 160 161 update_ifinfo(&ifilist, UPDATE_IFINFO_ALL); 162 TAILQ_FOREACH(ifi, &ifilist, ifi_next) { 163 /* NULL means all IFs will be processed. */ 164 if (ifname != NULL && 165 strcmp(ifi->ifi_ifname, ifname) != 0) 166 continue; 167 168 if (!ifi->ifi_persist) { 169 syslog(LOG_INFO, 170 "<%s> %s is not a target interface. " 171 "Ignored at this moment.", __func__, 172 ifi->ifi_ifname); 173 continue; 174 175 } 176 if (ifi->ifi_ifindex == 0) { 177 syslog(LOG_ERR, 178 "<%s> %s not found. " 179 "Ignored at this moment.", __func__, 180 ifi->ifi_ifname); 181 continue; 182 } 183 if (getconfig(ifi) == NULL) { 184 syslog(LOG_ERR, 185 "<%s> invalid configuration for %s. " 186 "Ignored at this moment.", __func__, 187 ifi->ifi_ifname); 188 continue; 189 } 190 } 191 return (0); 192 } 193 194 int 195 rm_ifinfo_index(int idx) 196 { 197 struct ifinfo *ifi; 198 199 ifi = if_indextoifinfo(idx); 200 if (ifi == NULL) { 201 syslog(LOG_ERR, "<%s>: ifinfo not found (idx=%d)", 202 __func__, idx); 203 return (-1); 204 } 205 206 return (rm_ifinfo(ifi)); 207 } 208 209 int 210 rm_ifinfo(struct ifinfo *ifi) 211 { 212 int error; 213 214 syslog(LOG_DEBUG, "<%s> enter (%s).", __func__, ifi->ifi_ifname); 215 switch (ifi->ifi_state) { 216 case IFI_STATE_UNCONFIGURED: 217 return (0); 218 break; 219 default: 220 ifi->ifi_state = IFI_STATE_UNCONFIGURED; 221 syslog(LOG_DEBUG, 222 "<%s> ifname=%s marked as UNCONFIGURED.", 223 __func__, ifi->ifi_ifname); 224 225 /* XXX: No MC leaving here because index is disappeared */ 226 227 /* Inactivate timer */ 228 rtadvd_remove_timer(ifi->ifi_ra_timer); 229 ifi->ifi_ra_timer = NULL; 230 break; 231 } 232 233 /* clean up ifi */ 234 if (!ifi->ifi_persist) { 235 TAILQ_REMOVE(&ifilist, ifi, ifi_next); 236 syslog(LOG_DEBUG, "<%s>: ifinfo (idx=%d) removed.", 237 __func__, ifi->ifi_ifindex); 238 } else { 239 /* recreate an empty entry */ 240 update_persist_ifinfo(&ifilist, ifi->ifi_ifname); 241 syslog(LOG_DEBUG, "<%s>: ifname=%s is persistent.", 242 __func__, ifi->ifi_ifname); 243 } 244 245 /* clean up rai if any */ 246 switch (ifi->ifi_state) { 247 case IFI_STATE_CONFIGURED: 248 if (ifi->ifi_rainfo != NULL) { 249 error = rm_rainfo(ifi->ifi_rainfo); 250 if (error) 251 return (error); 252 ifi->ifi_rainfo = NULL; 253 } 254 break; 255 case IFI_STATE_TRANSITIVE: 256 if (ifi->ifi_rainfo == ifi->ifi_rainfo_trans) { 257 if (ifi->ifi_rainfo != NULL) { 258 error = rm_rainfo(ifi->ifi_rainfo); 259 if (error) 260 return (error); 261 ifi->ifi_rainfo = NULL; 262 ifi->ifi_rainfo_trans = NULL; 263 } 264 } else { 265 if (ifi->ifi_rainfo != NULL) { 266 error = rm_rainfo(ifi->ifi_rainfo); 267 if (error) 268 return (error); 269 ifi->ifi_rainfo = NULL; 270 } 271 if (ifi->ifi_rainfo_trans != NULL) { 272 error = rm_rainfo(ifi->ifi_rainfo_trans); 273 if (error) 274 return (error); 275 ifi->ifi_rainfo_trans = NULL; 276 } 277 } 278 } 279 280 syslog(LOG_DEBUG, "<%s> leave (%s).", __func__, ifi->ifi_ifname); 281 if (!ifi->ifi_persist) 282 free(ifi); 283 return (0); 284 } 285 286 int 287 rm_rainfo(struct rainfo *rai) 288 { 289 struct prefix *pfx; 290 struct soliciter *sol; 291 struct rdnss *rdn; 292 struct rdnss_addr *rdna; 293 struct dnssl *dns; 294 struct pref64 *prf64; 295 struct rtinfo *rti; 296 297 syslog(LOG_DEBUG, "<%s>: enter", __func__); 298 299 TAILQ_REMOVE(&railist, rai, rai_next); 300 if (rai->rai_ifinfo != NULL) 301 syslog(LOG_DEBUG, "<%s>: rainfo (idx=%d) removed.", 302 __func__, rai->rai_ifinfo->ifi_ifindex); 303 304 if (rai->rai_ra_data != NULL) 305 free(rai->rai_ra_data); 306 307 while ((pfx = TAILQ_FIRST(&rai->rai_prefix)) != NULL) 308 delete_prefix(pfx); 309 while ((sol = TAILQ_FIRST(&rai->rai_soliciter)) != NULL) { 310 TAILQ_REMOVE(&rai->rai_soliciter, sol, sol_next); 311 free(sol); 312 } 313 while ((rdn = TAILQ_FIRST(&rai->rai_rdnss)) != NULL) { 314 TAILQ_REMOVE(&rai->rai_rdnss, rdn, rd_next); 315 while ((rdna = TAILQ_FIRST(&rdn->rd_list)) != NULL) { 316 TAILQ_REMOVE(&rdn->rd_list, rdna, ra_next); 317 free(rdna); 318 } 319 free(rdn); 320 } 321 while ((dns = TAILQ_FIRST(&rai->rai_dnssl)) != NULL) { 322 TAILQ_REMOVE(&rai->rai_dnssl, dns, dn_next); 323 free(dns); 324 } 325 while ((rti = TAILQ_FIRST(&rai->rai_route)) != NULL) { 326 TAILQ_REMOVE(&rai->rai_route, rti, rti_next); 327 free(rti); 328 } 329 while ((prf64 = TAILQ_FIRST(&rai->rai_pref64)) != NULL) { 330 TAILQ_REMOVE(&rai->rai_pref64, prf64, p64_next); 331 free(prf64); 332 } 333 free(rai); 334 syslog(LOG_DEBUG, "<%s>: leave", __func__); 335 336 return (0); 337 } 338 339 struct ifinfo * 340 getconfig(struct ifinfo *ifi) 341 { 342 int stat, i; 343 int error; 344 char tbuf[BUFSIZ]; 345 struct rainfo *rai; 346 struct rainfo *rai_old; 347 int32_t val; 348 int64_t val64; 349 char buf[BUFSIZ]; 350 char *bp = buf; 351 char *addr, *flagstr; 352 353 if (ifi == NULL) /* if does not exist */ 354 return (NULL); 355 356 if (ifi->ifi_state == IFI_STATE_TRANSITIVE && 357 ifi->ifi_rainfo == NULL) { 358 syslog(LOG_INFO, "<%s> %s is shutting down. Skipped.", 359 __func__, ifi->ifi_ifname); 360 return (NULL); 361 } 362 363 if ((stat = agetent(tbuf, ifi->ifi_ifname)) <= 0) { 364 memset(tbuf, 0, sizeof(tbuf)); 365 syslog(LOG_INFO, 366 "<%s> %s isn't defined in the configuration file" 367 " or the configuration file doesn't exist." 368 " Treat it as default", 369 __func__, ifi->ifi_ifname); 370 } 371 372 ELM_MALLOC(rai, exit(1)); 373 TAILQ_INIT(&rai->rai_prefix); 374 TAILQ_INIT(&rai->rai_route); 375 TAILQ_INIT(&rai->rai_rdnss); 376 TAILQ_INIT(&rai->rai_dnssl); 377 TAILQ_INIT(&rai->rai_pref64); 378 TAILQ_INIT(&rai->rai_soliciter); 379 rai->rai_ifinfo = ifi; 380 381 /* gather on-link prefixes from the network interfaces. */ 382 if (agetflag("noifprefix")) 383 rai->rai_advifprefix = 0; 384 else 385 rai->rai_advifprefix = 1; 386 387 /* get interface information */ 388 if (agetflag("nolladdr")) 389 rai->rai_advlinkopt = 0; 390 else 391 rai->rai_advlinkopt = 1; 392 if (rai->rai_advlinkopt) { 393 if (ifi->ifi_sdl.sdl_type == 0) { 394 syslog(LOG_ERR, 395 "<%s> can't get information of %s", 396 __func__, ifi->ifi_ifname); 397 goto getconfig_free_rai; 398 } 399 } 400 401 /* 402 * set router configuration variables. 403 */ 404 MAYHAVE(val, "maxinterval", DEF_MAXRTRADVINTERVAL); 405 if (val < MIN_MAXINTERVAL || val > MAX_MAXINTERVAL) { 406 syslog(LOG_ERR, 407 "<%s> maxinterval (%" PRIu32 ") on %s is invalid " 408 "(must be between %u and %u)", __func__, val, 409 ifi->ifi_ifname, MIN_MAXINTERVAL, MAX_MAXINTERVAL); 410 goto getconfig_free_rai; 411 } 412 rai->rai_maxinterval = (uint16_t)val; 413 414 MAYHAVE(val, "mininterval", rai->rai_maxinterval/3); 415 if ((uint16_t)val < MIN_MININTERVAL || 416 (uint16_t)val > (rai->rai_maxinterval * 3) / 4) { 417 syslog(LOG_ERR, 418 "<%s> mininterval (%" PRIu32 ") on %s is invalid " 419 "(must be between %d and %d)", 420 __func__, val, ifi->ifi_ifname, MIN_MININTERVAL, 421 (rai->rai_maxinterval * 3) / 4); 422 goto getconfig_free_rai; 423 } 424 rai->rai_mininterval = (uint16_t)val; 425 426 MAYHAVE(val, "chlim", DEF_ADVCURHOPLIMIT); 427 rai->rai_hoplimit = val & 0xff; 428 429 if ((flagstr = (char *)agetstr("raflags", &bp))) { 430 val = 0; 431 if (strchr(flagstr, 'm')) 432 val |= ND_RA_FLAG_MANAGED; 433 if (strchr(flagstr, 'o')) 434 val |= ND_RA_FLAG_OTHER; 435 if (strchr(flagstr, 'h')) 436 val |= ND_RA_FLAG_RTPREF_HIGH; 437 if (strchr(flagstr, 'l')) { 438 if ((val & ND_RA_FLAG_RTPREF_HIGH)) { 439 syslog(LOG_ERR, "<%s> the \'h\' and \'l\'" 440 " router flags are exclusive", __func__); 441 goto getconfig_free_rai; 442 } 443 val |= ND_RA_FLAG_RTPREF_LOW; 444 } 445 #ifdef DRAFT_IETF_6MAN_IPV6ONLY_FLAG 446 if (strchr(flagstr, 'S')) 447 val |= ND_RA_FLAG_IPV6_ONLY; 448 #endif 449 } else 450 MAYHAVE(val, "raflags", 0); 451 452 rai->rai_managedflg = val & ND_RA_FLAG_MANAGED; 453 rai->rai_otherflg = val & ND_RA_FLAG_OTHER; 454 #ifndef ND_RA_FLAG_RTPREF_MASK 455 #define ND_RA_FLAG_RTPREF_MASK 0x18 /* 00011000 */ 456 #define ND_RA_FLAG_RTPREF_RSV 0x10 /* 00010000 */ 457 #endif 458 rai->rai_rtpref = val & ND_RA_FLAG_RTPREF_MASK; 459 if (rai->rai_rtpref == ND_RA_FLAG_RTPREF_RSV) { 460 syslog(LOG_ERR, "<%s> invalid router preference (%02x) on %s", 461 __func__, rai->rai_rtpref, ifi->ifi_ifname); 462 goto getconfig_free_rai; 463 } 464 #ifdef DRAFT_IETF_6MAN_IPV6ONLY_FLAG 465 rai->rai_ipv6onlyflg = val & ND_RA_FLAG_IPV6_ONLY; 466 #endif 467 468 MAYHAVE(val, "rltime", rai->rai_maxinterval * 3); 469 if ((uint16_t)val && ((uint16_t)val < rai->rai_maxinterval || 470 (uint16_t)val > MAXROUTERLIFETIME)) { 471 syslog(LOG_ERR, 472 "<%s> router lifetime (%" PRIu32 ") on %s is invalid " 473 "(must be 0 or between %d and %d)", 474 __func__, val, ifi->ifi_ifname, rai->rai_maxinterval, 475 MAXROUTERLIFETIME); 476 goto getconfig_free_rai; 477 } 478 rai->rai_lifetime = val & 0xffff; 479 480 MAYHAVE(val, "rtime", DEF_ADVREACHABLETIME); 481 if (val < 0 || val > MAXREACHABLETIME) { 482 syslog(LOG_ERR, 483 "<%s> reachable time (%" PRIu32 ") on %s is invalid " 484 "(must be no greater than %d)", 485 __func__, val, ifi->ifi_ifname, MAXREACHABLETIME); 486 goto getconfig_free_rai; 487 } 488 rai->rai_reachabletime = (uint32_t)val; 489 490 MAYHAVE(val64, "retrans", DEF_ADVRETRANSTIMER); 491 if (val64 < 0 || val64 > 0xffffffff) { 492 syslog(LOG_ERR, "<%s> retrans time (%" PRIu64 ") on %s out of range", 493 __func__, val64, ifi->ifi_ifname); 494 goto getconfig_free_rai; 495 } 496 rai->rai_retranstimer = (uint32_t)val64; 497 498 if (agetnum("hapref") != -1 || agetnum("hatime") != -1) { 499 syslog(LOG_ERR, 500 "<%s> mobile-ip6 configuration not supported", 501 __func__); 502 goto getconfig_free_rai; 503 } 504 /* prefix information */ 505 506 /* 507 * This is an implementation specific parameter to consider 508 * link propagation delays and poorly synchronized clocks when 509 * checking consistency of advertised lifetimes. 510 */ 511 MAYHAVE(val, "clockskew", 0); 512 rai->rai_clockskew = val; 513 514 rai->rai_pfxs = 0; 515 for (i = -1; i < MAXPREFIX; i++) { 516 struct prefix *pfx; 517 518 makeentry(entbuf, sizeof(entbuf), i, "addr"); 519 addr = (char *)agetstr(entbuf, &bp); 520 if (addr == NULL) 521 continue; 522 523 /* allocate memory to store prefix information */ 524 ELM_MALLOC(pfx, exit(1)); 525 pfx->pfx_rainfo = rai; 526 pfx->pfx_origin = PREFIX_FROM_CONFIG; 527 528 if (inet_pton(AF_INET6, addr, &pfx->pfx_prefix) != 1) { 529 syslog(LOG_ERR, 530 "<%s> inet_pton failed for %s", 531 __func__, addr); 532 goto getconfig_free_pfx; 533 } 534 if (IN6_IS_ADDR_MULTICAST(&pfx->pfx_prefix)) { 535 syslog(LOG_ERR, 536 "<%s> multicast prefix (%s) must " 537 "not be advertised on %s", 538 __func__, addr, ifi->ifi_ifname); 539 goto getconfig_free_pfx; 540 } 541 if (IN6_IS_ADDR_LINKLOCAL(&pfx->pfx_prefix)) 542 syslog(LOG_NOTICE, 543 "<%s> link-local prefix (%s) will be" 544 " advertised on %s", 545 __func__, addr, ifi->ifi_ifname); 546 547 makeentry(entbuf, sizeof(entbuf), i, "prefixlen"); 548 MAYHAVE(val, entbuf, 64); 549 if (val < 0 || val > 128) { 550 syslog(LOG_ERR, "<%s> prefixlen (%" PRIu32 ") for %s " 551 "on %s out of range", 552 __func__, val, addr, ifi->ifi_ifname); 553 goto getconfig_free_pfx; 554 } 555 pfx->pfx_prefixlen = (int)val; 556 557 makeentry(entbuf, sizeof(entbuf), i, "pinfoflags"); 558 if ((flagstr = (char *)agetstr(entbuf, &bp))) { 559 val = 0; 560 if (strchr(flagstr, 'l')) 561 val |= ND_OPT_PI_FLAG_ONLINK; 562 if (strchr(flagstr, 'a')) 563 val |= ND_OPT_PI_FLAG_AUTO; 564 } else { 565 MAYHAVE(val, entbuf, 566 (ND_OPT_PI_FLAG_ONLINK|ND_OPT_PI_FLAG_AUTO)); 567 } 568 pfx->pfx_onlinkflg = val & ND_OPT_PI_FLAG_ONLINK; 569 pfx->pfx_autoconfflg = val & ND_OPT_PI_FLAG_AUTO; 570 571 makeentry(entbuf, sizeof(entbuf), i, "vltime"); 572 MAYHAVE(val64, entbuf, DEF_ADVVALIDLIFETIME); 573 if (val64 < 0 || val64 > 0xffffffff) { 574 syslog(LOG_ERR, "<%s> vltime (%" PRIu64 ") for " 575 "%s/%d on %s is out of range", 576 __func__, val64, 577 addr, pfx->pfx_prefixlen, ifi->ifi_ifname); 578 goto getconfig_free_pfx; 579 } 580 pfx->pfx_validlifetime = (uint32_t)val64; 581 582 makeentry(entbuf, sizeof(entbuf), i, "vltimedecr"); 583 if (agetflag(entbuf)) { 584 struct timespec now; 585 586 clock_gettime(CLOCK_MONOTONIC_FAST, &now); 587 pfx->pfx_vltimeexpire = 588 now.tv_sec + pfx->pfx_validlifetime; 589 } 590 591 makeentry(entbuf, sizeof(entbuf), i, "pltime"); 592 MAYHAVE(val64, entbuf, DEF_ADVPREFERREDLIFETIME); 593 if (val64 < 0 || val64 > 0xffffffff) { 594 syslog(LOG_ERR, 595 "<%s> pltime (%" PRIu64 ") for %s/%d on %s " 596 "is out of range", 597 __func__, val64, 598 addr, pfx->pfx_prefixlen, ifi->ifi_ifname); 599 goto getconfig_free_pfx; 600 } 601 pfx->pfx_preflifetime = (uint32_t)val64; 602 603 makeentry(entbuf, sizeof(entbuf), i, "pltimedecr"); 604 if (agetflag(entbuf)) { 605 struct timespec now; 606 607 clock_gettime(CLOCK_MONOTONIC_FAST, &now); 608 pfx->pfx_pltimeexpire = 609 now.tv_sec + pfx->pfx_preflifetime; 610 } 611 /* link into chain */ 612 TAILQ_INSERT_TAIL(&rai->rai_prefix, pfx, pfx_next); 613 rai->rai_pfxs++; 614 continue; 615 getconfig_free_pfx: 616 free(pfx); 617 } 618 if (rai->rai_advifprefix && rai->rai_pfxs == 0) 619 get_prefix(rai); 620 621 MAYHAVE(val64, "mtu", 0); 622 if (val < 0 || val64 > 0xffffffff) { 623 syslog(LOG_ERR, 624 "<%s> mtu (%" PRIu64 ") on %s out of range", 625 __func__, val64, ifi->ifi_ifname); 626 goto getconfig_free_rai; 627 } 628 rai->rai_linkmtu = (uint32_t)val64; 629 if (rai->rai_linkmtu == 0) { 630 char *mtustr; 631 632 if ((mtustr = (char *)agetstr("mtu", &bp)) && 633 strcmp(mtustr, "auto") == 0) 634 rai->rai_linkmtu = ifi->ifi_phymtu; 635 } 636 else if (rai->rai_linkmtu < IPV6_MMTU || 637 rai->rai_linkmtu > ifi->ifi_phymtu) { 638 syslog(LOG_ERR, 639 "<%s> advertised link mtu (%" PRIu32 ") on %s is invalid (must " 640 "be between least MTU (%d) and physical link MTU (%d)", 641 __func__, rai->rai_linkmtu, ifi->ifi_ifname, 642 IPV6_MMTU, ifi->ifi_phymtu); 643 goto getconfig_free_rai; 644 } 645 646 #ifdef SIOCSIFINFO_IN6 647 { 648 struct in6_ndireq ndi; 649 int s; 650 651 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) { 652 syslog(LOG_ERR, "<%s> socket: %s", __func__, 653 strerror(errno)); 654 exit(1); 655 } 656 memset(&ndi, 0, sizeof(ndi)); 657 strlcpy(ndi.ifname, ifi->ifi_ifname, sizeof(ndi.ifname)); 658 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&ndi) < 0) 659 syslog(LOG_INFO, "<%s> ioctl:SIOCGIFINFO_IN6 at %s: %s", 660 __func__, ifi->ifi_ifname, strerror(errno)); 661 662 /* reflect the RA info to the host variables in kernel */ 663 ndi.ndi.chlim = rai->rai_hoplimit; 664 ndi.ndi.retrans = rai->rai_retranstimer; 665 ndi.ndi.basereachable = rai->rai_reachabletime; 666 if (ioctl(s, SIOCSIFINFO_IN6, (caddr_t)&ndi) < 0) 667 syslog(LOG_INFO, "<%s> ioctl:SIOCSIFINFO_IN6 at %s: %s", 668 __func__, ifi->ifi_ifname, strerror(errno)); 669 670 close(s); 671 } 672 #endif 673 674 /* route information */ 675 rai->rai_routes = 0; 676 for (i = -1; i < MAXROUTE; i++) { 677 struct rtinfo *rti; 678 679 makeentry(entbuf, sizeof(entbuf), i, "rtprefix"); 680 addr = (char *)agetstr(entbuf, &bp); 681 if (addr == NULL) { 682 makeentry(oentbuf, sizeof(oentbuf), i, "rtrprefix"); 683 addr = (char *)agetstr(oentbuf, &bp); 684 if (addr) 685 fprintf(stderr, "%s was obsoleted. Use %s.\n", 686 oentbuf, entbuf); 687 } 688 if (addr == NULL) 689 continue; 690 691 /* allocate memory to store prefix information */ 692 ELM_MALLOC(rti, exit(1)); 693 694 if (inet_pton(AF_INET6, addr, &rti->rti_prefix) != 1) { 695 syslog(LOG_ERR, "<%s> inet_pton failed for %s", 696 __func__, addr); 697 goto getconfig_free_rti; 698 } 699 #if 0 700 /* 701 * XXX: currently there's no restriction in route information 702 * prefix according to 703 * draft-ietf-ipngwg-router-selection-00.txt. 704 * However, I think the similar restriction be necessary. 705 */ 706 MAYHAVE(val64, entbuf, DEF_ADVVALIDLIFETIME); 707 if (IN6_IS_ADDR_MULTICAST(&rti->prefix)) { 708 syslog(LOG_ERR, 709 "<%s> multicast route (%s) must " 710 "not be advertised on %s", 711 __func__, addr, ifi->ifi_ifname); 712 goto getconfig_free_rti; 713 } 714 if (IN6_IS_ADDR_LINKLOCAL(&rti->prefix)) { 715 syslog(LOG_NOTICE, 716 "<%s> link-local route (%s) will " 717 "be advertised on %s", 718 __func__, addr, ifi->ifi_ifname); 719 goto getconfig_free_rti; 720 } 721 #endif 722 723 makeentry(entbuf, sizeof(entbuf), i, "rtplen"); 724 /* XXX: 256 is a magic number for compatibility check. */ 725 MAYHAVE(val, entbuf, 256); 726 if (val == 256) { 727 makeentry(oentbuf, sizeof(oentbuf), i, "rtrplen"); 728 MAYHAVE(val, oentbuf, 256); 729 if (val != 256) 730 fprintf(stderr, "%s was obsoleted. Use %s.\n", 731 oentbuf, entbuf); 732 else 733 val = 64; 734 } 735 if (val < 0 || val > 128) { 736 syslog(LOG_ERR, "<%s> prefixlen (%" PRIu32 ") for %s on %s " 737 "out of range", 738 __func__, val, addr, ifi->ifi_ifname); 739 goto getconfig_free_rti; 740 } 741 rti->rti_prefixlen = (int)val; 742 743 makeentry(entbuf, sizeof(entbuf), i, "rtflags"); 744 if ((flagstr = (char *)agetstr(entbuf, &bp))) { 745 val = 0; 746 if (strchr(flagstr, 'h')) 747 val |= ND_RA_FLAG_RTPREF_HIGH; 748 if (strchr(flagstr, 'l')) { 749 if ((val & ND_RA_FLAG_RTPREF_HIGH)) { 750 syslog(LOG_ERR, 751 "<%s> the \'h\' and \'l\' route" 752 " preferences are exclusive", 753 __func__); 754 goto getconfig_free_rti; 755 } 756 val |= ND_RA_FLAG_RTPREF_LOW; 757 } 758 } else 759 MAYHAVE(val, entbuf, 256); /* XXX */ 760 if (val == 256) { 761 makeentry(oentbuf, sizeof(oentbuf), i, "rtrflags"); 762 MAYHAVE(val, oentbuf, 256); 763 if (val != 256) { 764 fprintf(stderr, "%s was obsoleted. Use %s.\n", 765 oentbuf, entbuf); 766 } else 767 val = 0; 768 } 769 rti->rti_rtpref = val & ND_RA_FLAG_RTPREF_MASK; 770 if (rti->rti_rtpref == ND_RA_FLAG_RTPREF_RSV) { 771 syslog(LOG_ERR, "<%s> invalid route preference (%02x) " 772 "for %s/%d on %s", 773 __func__, rti->rti_rtpref, addr, 774 rti->rti_prefixlen, ifi->ifi_ifname); 775 goto getconfig_free_rti; 776 } 777 778 /* 779 * Since the spec does not a default value, we should make 780 * this entry mandatory. However, FreeBSD 4.4 has shipped 781 * with this field being optional, we use the router lifetime 782 * as an ad-hoc default value with a warning message. 783 */ 784 makeentry(entbuf, sizeof(entbuf), i, "rtltime"); 785 MAYHAVE(val64, entbuf, -1); 786 if (val64 == -1) { 787 makeentry(oentbuf, sizeof(oentbuf), i, "rtrltime"); 788 MAYHAVE(val64, oentbuf, -1); 789 if (val64 != -1) 790 fprintf(stderr, "%s was obsoleted. Use %s.\n", 791 oentbuf, entbuf); 792 else { 793 fprintf(stderr, "%s should be specified " 794 "for interface %s.\n", entbuf, 795 ifi->ifi_ifname); 796 val64 = rai->rai_lifetime; 797 } 798 } 799 if (val64 < 0 || val64 > 0xffffffff) { 800 syslog(LOG_ERR, "<%s> route lifetime (%" PRIu64 ") for " 801 "%s/%d on %s out of range", __func__, 802 val64, addr, rti->rti_prefixlen, 803 ifi->ifi_ifname); 804 goto getconfig_free_rti; 805 } 806 rti->rti_ltime = (uint32_t)val64; 807 808 /* link into chain */ 809 TAILQ_INSERT_TAIL(&rai->rai_route, rti, rti_next); 810 rai->rai_routes++; 811 continue; 812 getconfig_free_rti: 813 free(rti); 814 } 815 816 /* DNS server and DNS search list information */ 817 for (i = -1; i < MAXRDNSSENT ; i++) { 818 struct rdnss *rdn; 819 struct rdnss_addr *rdna; 820 char *ap; 821 int c; 822 823 makeentry(entbuf, sizeof(entbuf), i, "rdnss"); 824 addr = (char *)agetstr(entbuf, &bp); 825 if (addr == NULL) 826 continue; 827 ELM_MALLOC(rdn, exit(1)); 828 829 TAILQ_INIT(&rdn->rd_list); 830 831 for (ap = addr; ap - addr < (ssize_t)strlen(addr); ap += c+1) { 832 c = strcspn(ap, ","); 833 strncpy(abuf, ap, c); 834 abuf[c] = '\0'; 835 ELM_MALLOC(rdna, goto getconfig_free_rdn); 836 if (inet_pton(AF_INET6, abuf, &rdna->ra_dns) != 1) { 837 syslog(LOG_ERR, "<%s> inet_pton failed for %s", 838 __func__, abuf); 839 free(rdna); 840 goto getconfig_free_rdn; 841 } 842 TAILQ_INSERT_TAIL(&rdn->rd_list, rdna, ra_next); 843 } 844 845 makeentry(entbuf, sizeof(entbuf), i, "rdnssltime"); 846 MAYHAVE(val, entbuf, (rai->rai_maxinterval * 3 / 2)); 847 if ((uint16_t)val < rai->rai_maxinterval || 848 (uint16_t)val > rai->rai_maxinterval * 2) { 849 syslog(LOG_ERR, "%s (%" PRIu16 ") on %s is invalid " 850 "(must be between %d and %d)", 851 entbuf, val, ifi->ifi_ifname, rai->rai_maxinterval, 852 rai->rai_maxinterval * 2); 853 goto getconfig_free_rdn; 854 } 855 rdn->rd_ltime = val; 856 857 /* link into chain */ 858 TAILQ_INSERT_TAIL(&rai->rai_rdnss, rdn, rd_next); 859 continue; 860 getconfig_free_rdn: 861 while ((rdna = TAILQ_FIRST(&rdn->rd_list)) != NULL) { 862 TAILQ_REMOVE(&rdn->rd_list, rdna, ra_next); 863 free(rdna); 864 } 865 free(rdn); 866 } 867 868 for (i = -1; i < MAXDNSSLENT ; i++) { 869 struct dnssl *dns; 870 struct dnssl_addr *dnsa; 871 char *ap; 872 int c; 873 874 makeentry(entbuf, sizeof(entbuf), i, "dnssl"); 875 addr = (char *)agetstr(entbuf, &bp); 876 if (addr == NULL) 877 continue; 878 879 ELM_MALLOC(dns, exit(1)); 880 881 TAILQ_INIT(&dns->dn_list); 882 883 for (ap = addr; ap - addr < (ssize_t)strlen(addr); ap += c+1) { 884 c = strcspn(ap, ","); 885 strncpy(abuf, ap, c); 886 abuf[c] = '\0'; 887 ELM_MALLOC(dnsa, goto getconfig_free_dns); 888 dnsa->da_len = dname_labelenc(dnsa->da_dom, abuf); 889 if (dnsa->da_len < 0) { 890 syslog(LOG_ERR, "Invalid dnssl entry: %s", 891 abuf); 892 goto getconfig_free_dns; 893 } 894 syslog(LOG_DEBUG, "<%s>: dnsa->da_len = %d", __func__, 895 dnsa->da_len); 896 TAILQ_INSERT_TAIL(&dns->dn_list, dnsa, da_next); 897 } 898 899 makeentry(entbuf, sizeof(entbuf), i, "dnsslltime"); 900 MAYHAVE(val, entbuf, (rai->rai_maxinterval * 3 / 2)); 901 if ((uint16_t)val < rai->rai_maxinterval || 902 (uint16_t)val > rai->rai_maxinterval * 2) { 903 syslog(LOG_ERR, "%s (%" PRIu16 ") on %s is invalid " 904 "(must be between %d and %d)", 905 entbuf, val, ifi->ifi_ifname, rai->rai_maxinterval, 906 rai->rai_maxinterval * 2); 907 goto getconfig_free_dns; 908 } 909 dns->dn_ltime = val; 910 911 /* link into chain */ 912 TAILQ_INSERT_TAIL(&rai->rai_dnssl, dns, dn_next); 913 continue; 914 getconfig_free_dns: 915 while ((dnsa = TAILQ_FIRST(&dns->dn_list)) != NULL) { 916 TAILQ_REMOVE(&dns->dn_list, dnsa, da_next); 917 free(dnsa); 918 } 919 free(dns); 920 } 921 922 /* 923 * handle pref64 924 */ 925 for (i = -1; i < MAXPREF64 ; i++) { 926 struct pref64 *prf64; 927 928 makeentry(entbuf, sizeof(entbuf), i, "pref64"); 929 addr = (char *)agetstr(entbuf, &bp); 930 if (addr == NULL) 931 continue; 932 ELM_MALLOC(prf64, exit(1)); 933 934 if (inet_pton(AF_INET6, addr, &prf64->p64_prefix) != 1) { 935 syslog(LOG_ERR, "<%s> inet_pton failed for %s", 936 __func__, addr); 937 goto getconfig_free_prf64; 938 } 939 940 makeentry(entbuf, sizeof(entbuf), i, "pref64len"); 941 MAYHAVE(val64, entbuf, 96); 942 switch (val64) { 943 case 96: 944 prf64->p64_plc = 0; 945 break; 946 case 64: 947 prf64->p64_plc = 1; 948 break; 949 case 56: 950 prf64->p64_plc = 2; 951 break; 952 case 48: 953 prf64->p64_plc = 3; 954 break; 955 case 40: 956 prf64->p64_plc = 4; 957 break; 958 case 32: 959 prf64->p64_plc = 5; 960 break; 961 default: 962 syslog(LOG_ERR, "PREF64 prefix length %" PRIi64 963 "on %s is invalid; skipping", 964 val64, ifi->ifi_ifname); 965 goto getconfig_free_prf64; 966 } 967 968 makeentry(entbuf, sizeof(entbuf), i, "pref64lifetime"); 969 MAYHAVE(val64, entbuf, (rai->rai_lifetime * 3)); 970 /* This logic is from RFC 8781 section 4.1. */ 971 if (val64 > 65528) 972 val64 = 65528; 973 val64 = (val64 + 7) / 8; 974 prf64->p64_sl = (uint16_t)val64; 975 976 /* link into chain */ 977 TAILQ_INSERT_TAIL(&rai->rai_pref64, prf64, p64_next); 978 continue; 979 getconfig_free_prf64: 980 free(prf64); 981 } 982 983 /* construct the sending packet */ 984 make_packet(rai); 985 986 /* 987 * If an entry with the same ifindex exists, remove it first. 988 * Before the removal, RDNSS and DNSSL options with 989 * zero-lifetime will be sent. 990 */ 991 switch (ifi->ifi_state) { 992 case IFI_STATE_UNCONFIGURED: 993 /* UNCONFIGURED -> TRANSITIVE */ 994 995 error = sock_mc_join(&sock, ifi->ifi_ifindex); 996 if (error) 997 exit(1); 998 999 ifi->ifi_state = IFI_STATE_TRANSITIVE; 1000 ifi->ifi_burstcount = MAX_INITIAL_RTR_ADVERTISEMENTS; 1001 ifi->ifi_burstinterval = MAX_INITIAL_RTR_ADVERT_INTERVAL; 1002 1003 /* The same two rai mean initial burst */ 1004 ifi->ifi_rainfo = rai; 1005 ifi->ifi_rainfo_trans = rai; 1006 TAILQ_INSERT_TAIL(&railist, rai, rai_next); 1007 1008 if (ifi->ifi_ra_timer == NULL) 1009 ifi->ifi_ra_timer = rtadvd_add_timer(ra_timeout, 1010 ra_timer_update, ifi, ifi); 1011 ra_timer_update(ifi, &ifi->ifi_ra_timer->rat_tm); 1012 rtadvd_set_timer(&ifi->ifi_ra_timer->rat_tm, 1013 ifi->ifi_ra_timer); 1014 1015 syslog(LOG_DEBUG, 1016 "<%s> ifname=%s marked as TRANSITIVE (initial burst).", 1017 __func__, ifi->ifi_ifname); 1018 break; 1019 case IFI_STATE_CONFIGURED: 1020 /* CONFIGURED -> TRANSITIVE */ 1021 rai_old = ifi->ifi_rainfo; 1022 if (rai_old == NULL) { 1023 syslog(LOG_ERR, 1024 "<%s> ifi_rainfo is NULL" 1025 " in IFI_STATE_CONFIGURED.", __func__); 1026 ifi = NULL; 1027 break; 1028 } else { 1029 struct rdnss *rdn; 1030 struct dnssl *dns; 1031 1032 rai_old->rai_lifetime = 0; 1033 TAILQ_FOREACH(rdn, &rai_old->rai_rdnss, rd_next) 1034 rdn->rd_ltime = 0; 1035 TAILQ_FOREACH(dns, &rai_old->rai_dnssl, dn_next) 1036 dns->dn_ltime = 0; 1037 1038 ifi->ifi_rainfo_trans = rai_old; 1039 ifi->ifi_state = IFI_STATE_TRANSITIVE; 1040 ifi->ifi_burstcount = MAX_FINAL_RTR_ADVERTISEMENTS; 1041 ifi->ifi_burstinterval = MIN_DELAY_BETWEEN_RAS; 1042 1043 ra_timer_update(ifi, &ifi->ifi_ra_timer->rat_tm); 1044 rtadvd_set_timer(&ifi->ifi_ra_timer->rat_tm, 1045 ifi->ifi_ra_timer); 1046 1047 syslog(LOG_DEBUG, 1048 "<%s> ifname=%s marked as TRANSITIVE" 1049 " (transitional burst)", 1050 __func__, ifi->ifi_ifname); 1051 } 1052 ifi->ifi_rainfo = rai; 1053 TAILQ_INSERT_TAIL(&railist, rai, rai_next); 1054 break; 1055 case IFI_STATE_TRANSITIVE: 1056 if (ifi->ifi_rainfo != NULL) { 1057 if (ifi->ifi_rainfo == ifi->ifi_rainfo_trans) { 1058 /* Reinitialize initial burst */ 1059 rm_rainfo(ifi->ifi_rainfo); 1060 ifi->ifi_rainfo = rai; 1061 ifi->ifi_rainfo_trans = rai; 1062 ifi->ifi_burstcount = 1063 MAX_INITIAL_RTR_ADVERTISEMENTS; 1064 ifi->ifi_burstinterval = 1065 MAX_INITIAL_RTR_ADVERT_INTERVAL; 1066 } else { 1067 /* Replace ifi_rainfo with the new one */ 1068 rm_rainfo(ifi->ifi_rainfo); 1069 ifi->ifi_rainfo = rai; 1070 } 1071 TAILQ_INSERT_TAIL(&railist, rai, rai_next); 1072 1073 ra_timer_update(ifi, &ifi->ifi_ra_timer->rat_tm); 1074 rtadvd_set_timer(&ifi->ifi_ra_timer->rat_tm, 1075 ifi->ifi_ra_timer); 1076 } else { 1077 /* XXX: NOTREACHED. Being shut down. */ 1078 syslog(LOG_ERR, 1079 "<%s> %s is shutting down. Skipped.", 1080 __func__, ifi->ifi_ifname); 1081 rm_rainfo(rai); 1082 1083 return (NULL); 1084 } 1085 break; 1086 } 1087 1088 return (ifi); 1089 1090 getconfig_free_rai: 1091 free(rai); 1092 return (NULL); 1093 } 1094 1095 void 1096 get_prefix(struct rainfo *rai) 1097 { 1098 struct ifaddrs *ifap, *ifa; 1099 struct prefix *pfx; 1100 struct in6_addr *a; 1101 struct ifinfo *ifi; 1102 char *p, *ep, *m, *lim; 1103 char ntopbuf[INET6_ADDRSTRLEN]; 1104 1105 if (getifaddrs(&ifap) < 0) { 1106 syslog(LOG_ERR, 1107 "<%s> can't get interface addresses", 1108 __func__); 1109 exit(1); 1110 } 1111 ifi = rai->rai_ifinfo; 1112 1113 for (ifa = ifap; ifa; ifa = ifa->ifa_next) { 1114 int plen; 1115 1116 if (strcmp(ifa->ifa_name, ifi->ifi_ifname) != 0) 1117 continue; 1118 if (ifa->ifa_addr->sa_family != AF_INET6) 1119 continue; 1120 a = &((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr; 1121 if (IN6_IS_ADDR_LINKLOCAL(a)) 1122 continue; 1123 1124 /* get prefix length */ 1125 m = (char *)&((struct sockaddr_in6 *)ifa->ifa_netmask)->sin6_addr; 1126 lim = (char *)(ifa->ifa_netmask) + ifa->ifa_netmask->sa_len; 1127 plen = prefixlen(m, lim); 1128 if (plen <= 0 || plen > 128) { 1129 syslog(LOG_ERR, "<%s> failed to get prefixlen " 1130 "or prefix is invalid", 1131 __func__); 1132 exit(1); 1133 } 1134 if (plen == 128) /* XXX */ 1135 continue; 1136 if (find_prefix(rai, a, plen)) { 1137 /* ignore a duplicated prefix. */ 1138 continue; 1139 } 1140 1141 /* allocate memory to store prefix info. */ 1142 ELM_MALLOC(pfx, exit(1)); 1143 1144 /* set prefix, sweep bits outside of prefixlen */ 1145 pfx->pfx_prefixlen = plen; 1146 memcpy(&pfx->pfx_prefix, a, sizeof(*a)); 1147 p = (char *)&pfx->pfx_prefix; 1148 ep = (char *)(&pfx->pfx_prefix + 1); 1149 while (m < lim && p < ep) 1150 *p++ &= *m++; 1151 while (p < ep) 1152 *p++ = 0x00; 1153 if (!inet_ntop(AF_INET6, &pfx->pfx_prefix, ntopbuf, 1154 sizeof(ntopbuf))) { 1155 syslog(LOG_ERR, "<%s> inet_ntop failed", __func__); 1156 exit(1); 1157 } 1158 syslog(LOG_DEBUG, 1159 "<%s> add %s/%d to prefix list on %s", 1160 __func__, ntopbuf, pfx->pfx_prefixlen, ifi->ifi_ifname); 1161 1162 /* set other fields with protocol defaults */ 1163 pfx->pfx_validlifetime = DEF_ADVVALIDLIFETIME; 1164 pfx->pfx_preflifetime = DEF_ADVPREFERREDLIFETIME; 1165 pfx->pfx_onlinkflg = 1; 1166 pfx->pfx_autoconfflg = 1; 1167 pfx->pfx_origin = PREFIX_FROM_KERNEL; 1168 pfx->pfx_rainfo = rai; 1169 1170 /* link into chain */ 1171 TAILQ_INSERT_TAIL(&rai->rai_prefix, pfx, pfx_next); 1172 1173 /* counter increment */ 1174 rai->rai_pfxs++; 1175 } 1176 1177 freeifaddrs(ifap); 1178 } 1179 1180 static void 1181 makeentry(char *buf, size_t len, int id, const char *string) 1182 { 1183 1184 if (id < 0) 1185 strlcpy(buf, string, len); 1186 else 1187 snprintf(buf, len, "%s%d", string, id); 1188 } 1189 1190 /* 1191 * Add a prefix to the list of specified interface and reconstruct 1192 * the outgoing packet. 1193 * The prefix must not be in the list. 1194 * XXX: other parameters of the prefix (e.g. lifetime) should be 1195 * able to be specified. 1196 */ 1197 static void 1198 add_prefix(struct rainfo *rai, struct in6_prefixreq *ipr) 1199 { 1200 struct prefix *pfx; 1201 struct ifinfo *ifi; 1202 char ntopbuf[INET6_ADDRSTRLEN]; 1203 1204 ifi = rai->rai_ifinfo; 1205 ELM_MALLOC(pfx, return); 1206 pfx->pfx_prefix = ipr->ipr_prefix.sin6_addr; 1207 pfx->pfx_prefixlen = ipr->ipr_plen; 1208 pfx->pfx_validlifetime = ipr->ipr_vltime; 1209 pfx->pfx_preflifetime = ipr->ipr_pltime; 1210 pfx->pfx_onlinkflg = ipr->ipr_raf_onlink; 1211 pfx->pfx_autoconfflg = ipr->ipr_raf_auto; 1212 pfx->pfx_origin = PREFIX_FROM_DYNAMIC; 1213 pfx->pfx_rainfo = rai; 1214 1215 TAILQ_INSERT_TAIL(&rai->rai_prefix, pfx, pfx_next); 1216 1217 syslog(LOG_DEBUG, "<%s> new prefix %s/%d was added on %s", 1218 __func__, 1219 inet_ntop(AF_INET6, &ipr->ipr_prefix.sin6_addr, ntopbuf, 1220 sizeof(ntopbuf)), ipr->ipr_plen, ifi->ifi_ifname); 1221 1222 rai->rai_pfxs++; 1223 } 1224 1225 /* 1226 * Delete a prefix to the list of specified interface and reconstruct 1227 * the outgoing packet. 1228 * The prefix must be in the list. 1229 */ 1230 void 1231 delete_prefix(struct prefix *pfx) 1232 { 1233 struct rainfo *rai; 1234 struct ifinfo *ifi; 1235 char ntopbuf[INET6_ADDRSTRLEN]; 1236 1237 rai = pfx->pfx_rainfo; 1238 ifi = rai->rai_ifinfo; 1239 TAILQ_REMOVE(&rai->rai_prefix, pfx, pfx_next); 1240 syslog(LOG_DEBUG, "<%s> prefix %s/%d was deleted on %s", 1241 __func__, 1242 inet_ntop(AF_INET6, &pfx->pfx_prefix, ntopbuf, 1243 sizeof(ntopbuf)), pfx->pfx_prefixlen, ifi->ifi_ifname); 1244 if (pfx->pfx_timer) 1245 rtadvd_remove_timer(pfx->pfx_timer); 1246 free(pfx); 1247 1248 rai->rai_pfxs--; 1249 } 1250 1251 void 1252 invalidate_prefix(struct prefix *pfx) 1253 { 1254 struct timespec timo; 1255 struct rainfo *rai; 1256 struct ifinfo *ifi; 1257 char ntopbuf[INET6_ADDRSTRLEN]; 1258 1259 rai = pfx->pfx_rainfo; 1260 ifi = rai->rai_ifinfo; 1261 if (pfx->pfx_timer) { /* sanity check */ 1262 syslog(LOG_ERR, 1263 "<%s> assumption failure: timer already exists", 1264 __func__); 1265 exit(1); 1266 } 1267 1268 syslog(LOG_DEBUG, "<%s> prefix %s/%d was invalidated on %s, " 1269 "will expire in %ld seconds", __func__, 1270 inet_ntop(AF_INET6, &pfx->pfx_prefix, ntopbuf, sizeof(ntopbuf)), 1271 pfx->pfx_prefixlen, ifi->ifi_ifname, (long)prefix_timo); 1272 1273 /* set the expiration timer */ 1274 pfx->pfx_timer = rtadvd_add_timer(prefix_timeout, NULL, pfx, NULL); 1275 if (pfx->pfx_timer == NULL) { 1276 syslog(LOG_ERR, "<%s> failed to add a timer for a prefix. " 1277 "remove the prefix", __func__); 1278 delete_prefix(pfx); 1279 } 1280 timo.tv_sec = prefix_timo; 1281 timo.tv_nsec = 0; 1282 rtadvd_set_timer(&timo, pfx->pfx_timer); 1283 } 1284 1285 static struct rtadvd_timer * 1286 prefix_timeout(void *arg) 1287 { 1288 1289 delete_prefix((struct prefix *)arg); 1290 1291 return (NULL); 1292 } 1293 1294 void 1295 update_prefix(struct prefix *pfx) 1296 { 1297 struct rainfo *rai; 1298 struct ifinfo *ifi; 1299 char ntopbuf[INET6_ADDRSTRLEN]; 1300 1301 rai = pfx->pfx_rainfo; 1302 ifi = rai->rai_ifinfo; 1303 if (pfx->pfx_timer == NULL) { /* sanity check */ 1304 syslog(LOG_ERR, 1305 "<%s> assumption failure: timer does not exist", 1306 __func__); 1307 exit(1); 1308 } 1309 1310 syslog(LOG_DEBUG, "<%s> prefix %s/%d was re-enabled on %s", 1311 __func__, inet_ntop(AF_INET6, &pfx->pfx_prefix, ntopbuf, 1312 sizeof(ntopbuf)), pfx->pfx_prefixlen, ifi->ifi_ifname); 1313 1314 /* stop the expiration timer */ 1315 rtadvd_remove_timer(pfx->pfx_timer); 1316 pfx->pfx_timer = NULL; 1317 } 1318 1319 /* 1320 * Try to get an in6_prefixreq contents for a prefix which matches 1321 * ipr->ipr_prefix and ipr->ipr_plen and belongs to 1322 * the interface whose name is ipr->ipr_name[]. 1323 */ 1324 static int 1325 init_prefix(struct in6_prefixreq *ipr) 1326 { 1327 #if 0 1328 int s; 1329 1330 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) { 1331 syslog(LOG_ERR, "<%s> socket: %s", __func__, 1332 strerror(errno)); 1333 exit(1); 1334 } 1335 1336 if (ioctl(s, SIOCGIFPREFIX_IN6, (caddr_t)ipr) < 0) { 1337 syslog(LOG_INFO, "<%s> ioctl:SIOCGIFPREFIX %s", __func__, 1338 strerror(errno)); 1339 1340 ipr->ipr_vltime = DEF_ADVVALIDLIFETIME; 1341 ipr->ipr_pltime = DEF_ADVPREFERREDLIFETIME; 1342 ipr->ipr_raf_onlink = 1; 1343 ipr->ipr_raf_auto = 1; 1344 /* omit other field initialization */ 1345 } 1346 else if (ipr->ipr_origin < PR_ORIG_RR) { 1347 char ntopbuf[INET6_ADDRSTRLEN]; 1348 1349 syslog(LOG_WARNING, "<%s> Added prefix(%s)'s origin %d is" 1350 "lower than PR_ORIG_RR(router renumbering)." 1351 "This should not happen if I am router", __func__, 1352 inet_ntop(AF_INET6, &ipr->ipr_prefix.sin6_addr, ntopbuf, 1353 sizeof(ntopbuf)), ipr->ipr_origin); 1354 close(s); 1355 return (1); 1356 } 1357 1358 close(s); 1359 return (0); 1360 #else 1361 ipr->ipr_vltime = DEF_ADVVALIDLIFETIME; 1362 ipr->ipr_pltime = DEF_ADVPREFERREDLIFETIME; 1363 ipr->ipr_raf_onlink = 1; 1364 ipr->ipr_raf_auto = 1; 1365 return (0); 1366 #endif 1367 } 1368 1369 void 1370 make_prefix(struct rainfo *rai, int ifindex, struct in6_addr *addr, int plen) 1371 { 1372 struct in6_prefixreq ipr; 1373 1374 memset(&ipr, 0, sizeof(ipr)); 1375 if (if_indextoname(ifindex, ipr.ipr_name) == NULL) { 1376 syslog(LOG_ERR, "<%s> Prefix added interface No.%d doesn't " 1377 "exist. This should not happen! %s", __func__, 1378 ifindex, strerror(errno)); 1379 exit(1); 1380 } 1381 ipr.ipr_prefix.sin6_len = sizeof(ipr.ipr_prefix); 1382 ipr.ipr_prefix.sin6_family = AF_INET6; 1383 ipr.ipr_prefix.sin6_addr = *addr; 1384 ipr.ipr_plen = plen; 1385 1386 if (init_prefix(&ipr)) 1387 return; /* init failed by some error */ 1388 add_prefix(rai, &ipr); 1389 } 1390 1391 void 1392 make_packet(struct rainfo *rai) 1393 { 1394 size_t packlen, lladdroptlen = 0; 1395 char *buf; 1396 struct nd_router_advert *ra; 1397 struct nd_opt_prefix_info *ndopt_pi; 1398 struct nd_opt_mtu *ndopt_mtu; 1399 struct nd_opt_route_info *ndopt_rti; 1400 struct rtinfo *rti; 1401 struct nd_opt_rdnss *ndopt_rdnss; 1402 struct rdnss *rdn; 1403 struct nd_opt_dnssl *ndopt_dnssl; 1404 struct dnssl *dns; 1405 struct pref64 *prf64; 1406 struct nd_opt_pref64 *ndopt_pref64; 1407 size_t len; 1408 struct prefix *pfx; 1409 struct ifinfo *ifi; 1410 1411 ifi = rai->rai_ifinfo; 1412 /* calculate total length */ 1413 packlen = sizeof(struct nd_router_advert); 1414 if (rai->rai_advlinkopt) { 1415 if ((lladdroptlen = lladdropt_length(&ifi->ifi_sdl)) == 0) { 1416 syslog(LOG_INFO, 1417 "<%s> link-layer address option has" 1418 " null length on %s. Treat as not included.", 1419 __func__, ifi->ifi_ifname); 1420 rai->rai_advlinkopt = 0; 1421 } 1422 packlen += lladdroptlen; 1423 } 1424 if (rai->rai_pfxs) 1425 packlen += sizeof(struct nd_opt_prefix_info) * rai->rai_pfxs; 1426 if (rai->rai_linkmtu) 1427 packlen += sizeof(struct nd_opt_mtu); 1428 1429 TAILQ_FOREACH(rti, &rai->rai_route, rti_next) 1430 packlen += sizeof(struct nd_opt_route_info) + 1431 ((rti->rti_prefixlen + 0x3f) >> 6) * 8; 1432 1433 TAILQ_FOREACH(rdn, &rai->rai_rdnss, rd_next) { 1434 struct rdnss_addr *rdna; 1435 1436 packlen += sizeof(struct nd_opt_rdnss); 1437 TAILQ_FOREACH(rdna, &rdn->rd_list, ra_next) 1438 packlen += sizeof(rdna->ra_dns); 1439 } 1440 TAILQ_FOREACH(dns, &rai->rai_dnssl, dn_next) { 1441 struct dnssl_addr *dnsa; 1442 1443 packlen += sizeof(struct nd_opt_dnssl); 1444 len = 0; 1445 TAILQ_FOREACH(dnsa, &dns->dn_list, da_next) 1446 len += dnsa->da_len; 1447 1448 /* A zero octet and 8 octet boundary */ 1449 len++; 1450 len += (len % 8) ? 8 - len % 8 : 0; 1451 1452 packlen += len; 1453 } 1454 TAILQ_FOREACH(prf64, &rai->rai_pref64, p64_next) 1455 packlen += sizeof(struct nd_opt_pref64); 1456 1457 /* allocate memory for the packet */ 1458 if ((buf = malloc(packlen)) == NULL) { 1459 syslog(LOG_ERR, 1460 "<%s> can't get enough memory for an RA packet", 1461 __func__); 1462 exit(1); 1463 } 1464 memset(buf, 0, packlen); 1465 if (rai->rai_ra_data) /* Free old data if any. */ 1466 free(rai->rai_ra_data); 1467 rai->rai_ra_data = buf; 1468 /* XXX: what if packlen > 576? */ 1469 rai->rai_ra_datalen = packlen; 1470 1471 /* 1472 * construct the packet 1473 */ 1474 ra = (struct nd_router_advert *)buf; 1475 ra->nd_ra_type = ND_ROUTER_ADVERT; 1476 ra->nd_ra_code = 0; 1477 ra->nd_ra_cksum = 0; 1478 ra->nd_ra_curhoplimit = (uint8_t)(0xff & rai->rai_hoplimit); 1479 /* 1480 * XXX: the router preference field, which is a 2-bit field, should be 1481 * initialized before other fields. 1482 */ 1483 ra->nd_ra_flags_reserved = 0xff & rai->rai_rtpref; 1484 ra->nd_ra_flags_reserved |= 1485 rai->rai_managedflg ? ND_RA_FLAG_MANAGED : 0; 1486 ra->nd_ra_flags_reserved |= 1487 rai->rai_otherflg ? ND_RA_FLAG_OTHER : 0; 1488 #ifdef DRAFT_IETF_6MAN_IPV6ONLY_FLAG 1489 ra->nd_ra_flags_reserved |= 1490 rai->rai_ipv6onlyflg ? ND_RA_FLAG_IPV6_ONLY : 0; 1491 #endif 1492 ra->nd_ra_router_lifetime = htons(rai->rai_lifetime); 1493 ra->nd_ra_reachable = htonl(rai->rai_reachabletime); 1494 ra->nd_ra_retransmit = htonl(rai->rai_retranstimer); 1495 buf += sizeof(*ra); 1496 1497 if (rai->rai_advlinkopt) { 1498 lladdropt_fill(&ifi->ifi_sdl, (struct nd_opt_hdr *)buf); 1499 buf += lladdroptlen; 1500 } 1501 1502 if (rai->rai_linkmtu) { 1503 ndopt_mtu = (struct nd_opt_mtu *)buf; 1504 ndopt_mtu->nd_opt_mtu_type = ND_OPT_MTU; 1505 ndopt_mtu->nd_opt_mtu_len = 1; 1506 ndopt_mtu->nd_opt_mtu_reserved = 0; 1507 ndopt_mtu->nd_opt_mtu_mtu = htonl(rai->rai_linkmtu); 1508 buf += sizeof(struct nd_opt_mtu); 1509 } 1510 1511 TAILQ_FOREACH(pfx, &rai->rai_prefix, pfx_next) { 1512 uint32_t vltime, pltime; 1513 struct timespec now; 1514 1515 ndopt_pi = (struct nd_opt_prefix_info *)buf; 1516 ndopt_pi->nd_opt_pi_type = ND_OPT_PREFIX_INFORMATION; 1517 ndopt_pi->nd_opt_pi_len = 4; 1518 ndopt_pi->nd_opt_pi_prefix_len = pfx->pfx_prefixlen; 1519 ndopt_pi->nd_opt_pi_flags_reserved = 0; 1520 if (pfx->pfx_onlinkflg) 1521 ndopt_pi->nd_opt_pi_flags_reserved |= 1522 ND_OPT_PI_FLAG_ONLINK; 1523 if (pfx->pfx_autoconfflg) 1524 ndopt_pi->nd_opt_pi_flags_reserved |= 1525 ND_OPT_PI_FLAG_AUTO; 1526 if (pfx->pfx_timer) 1527 vltime = 0; 1528 else { 1529 if (pfx->pfx_vltimeexpire || pfx->pfx_pltimeexpire) 1530 clock_gettime(CLOCK_MONOTONIC_FAST, &now); 1531 if (pfx->pfx_vltimeexpire == 0) 1532 vltime = pfx->pfx_validlifetime; 1533 else 1534 vltime = ((time_t)pfx->pfx_vltimeexpire > now.tv_sec) ? 1535 pfx->pfx_vltimeexpire - now.tv_sec : 0; 1536 } 1537 if (pfx->pfx_timer) 1538 pltime = 0; 1539 else { 1540 if (pfx->pfx_pltimeexpire == 0) 1541 pltime = pfx->pfx_preflifetime; 1542 else 1543 pltime = ((time_t)pfx->pfx_pltimeexpire > now.tv_sec) ? 1544 pfx->pfx_pltimeexpire - now.tv_sec : 0; 1545 } 1546 if (vltime < pltime) { 1547 /* 1548 * this can happen if vltime is decrement but pltime 1549 * is not. 1550 */ 1551 pltime = vltime; 1552 } 1553 ndopt_pi->nd_opt_pi_valid_time = htonl(vltime); 1554 ndopt_pi->nd_opt_pi_preferred_time = htonl(pltime); 1555 ndopt_pi->nd_opt_pi_reserved2 = 0; 1556 ndopt_pi->nd_opt_pi_prefix = pfx->pfx_prefix; 1557 1558 buf += sizeof(struct nd_opt_prefix_info); 1559 } 1560 1561 TAILQ_FOREACH(rti, &rai->rai_route, rti_next) { 1562 uint8_t psize = (rti->rti_prefixlen + 0x3f) >> 6; 1563 1564 ndopt_rti = (struct nd_opt_route_info *)buf; 1565 ndopt_rti->nd_opt_rti_type = ND_OPT_ROUTE_INFO; 1566 ndopt_rti->nd_opt_rti_len = 1 + psize; 1567 ndopt_rti->nd_opt_rti_prefixlen = rti->rti_prefixlen; 1568 ndopt_rti->nd_opt_rti_flags = 0xff & rti->rti_rtpref; 1569 ndopt_rti->nd_opt_rti_lifetime = htonl(rti->rti_ltime); 1570 memcpy(ndopt_rti + 1, &rti->rti_prefix, psize * 8); 1571 buf += sizeof(struct nd_opt_route_info) + psize * 8; 1572 } 1573 1574 TAILQ_FOREACH(rdn, &rai->rai_rdnss, rd_next) { 1575 struct rdnss_addr *rdna; 1576 1577 ndopt_rdnss = (struct nd_opt_rdnss *)buf; 1578 ndopt_rdnss->nd_opt_rdnss_type = ND_OPT_RDNSS; 1579 ndopt_rdnss->nd_opt_rdnss_len = 0; 1580 ndopt_rdnss->nd_opt_rdnss_reserved = 0; 1581 ndopt_rdnss->nd_opt_rdnss_lifetime = htonl(rdn->rd_ltime); 1582 buf += sizeof(struct nd_opt_rdnss); 1583 1584 TAILQ_FOREACH(rdna, &rdn->rd_list, ra_next) { 1585 memcpy(buf, &rdna->ra_dns, sizeof(rdna->ra_dns)); 1586 buf += sizeof(rdna->ra_dns); 1587 } 1588 /* Length field should be in 8 octets */ 1589 ndopt_rdnss->nd_opt_rdnss_len = (buf - (char *)ndopt_rdnss) / 8; 1590 1591 syslog(LOG_DEBUG, "<%s>: nd_opt_dnss_len = %d", __func__, 1592 ndopt_rdnss->nd_opt_rdnss_len); 1593 } 1594 1595 TAILQ_FOREACH(dns, &rai->rai_dnssl, dn_next) { 1596 struct dnssl_addr *dnsa; 1597 1598 ndopt_dnssl = (struct nd_opt_dnssl *)buf; 1599 ndopt_dnssl->nd_opt_dnssl_type = ND_OPT_DNSSL; 1600 ndopt_dnssl->nd_opt_dnssl_len = 0; 1601 ndopt_dnssl->nd_opt_dnssl_reserved = 0; 1602 ndopt_dnssl->nd_opt_dnssl_lifetime = htonl(dns->dn_ltime); 1603 buf += sizeof(*ndopt_dnssl); 1604 1605 TAILQ_FOREACH(dnsa, &dns->dn_list, da_next) { 1606 memcpy(buf, dnsa->da_dom, dnsa->da_len); 1607 buf += dnsa->da_len; 1608 } 1609 1610 /* A zero octet after encoded DNS server list. */ 1611 *buf++ = '\0'; 1612 1613 /* Padding to next 8 octets boundary */ 1614 len = buf - (char *)ndopt_dnssl; 1615 len += (len % 8) ? 8 - len % 8 : 0; 1616 buf = (char *)ndopt_dnssl + len; 1617 1618 /* Length field must be in 8 octets */ 1619 ndopt_dnssl->nd_opt_dnssl_len = len / 8; 1620 1621 syslog(LOG_DEBUG, "<%s>: nd_opt_dnssl_len = %d", __func__, 1622 ndopt_dnssl->nd_opt_dnssl_len); 1623 } 1624 1625 TAILQ_FOREACH(prf64, &rai->rai_pref64, p64_next) { 1626 ndopt_pref64 = (struct nd_opt_pref64 *)buf; 1627 ndopt_pref64->nd_opt_pref64_type = ND_OPT_PREF64; 1628 ndopt_pref64->nd_opt_pref64_len = 2; 1629 ndopt_pref64->nd_opt_pref64_sl_plc = 1630 (htons(prf64->p64_sl << 3)) | 1631 htons((prf64->p64_plc & 0x7)); 1632 memcpy(&ndopt_pref64->nd_opt_prefix[0], 1633 &prf64->p64_prefix, 1634 sizeof(ndopt_pref64->nd_opt_prefix)); 1635 buf += sizeof(struct nd_opt_pref64); 1636 } 1637 } 1638