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 } else 446 MAYHAVE(val, "raflags", 0); 447 448 rai->rai_managedflg = val & ND_RA_FLAG_MANAGED; 449 rai->rai_otherflg = val & ND_RA_FLAG_OTHER; 450 #ifndef ND_RA_FLAG_RTPREF_MASK 451 #define ND_RA_FLAG_RTPREF_MASK 0x18 /* 00011000 */ 452 #define ND_RA_FLAG_RTPREF_RSV 0x10 /* 00010000 */ 453 #endif 454 rai->rai_rtpref = val & ND_RA_FLAG_RTPREF_MASK; 455 if (rai->rai_rtpref == ND_RA_FLAG_RTPREF_RSV) { 456 syslog(LOG_ERR, "<%s> invalid router preference (%02x) on %s", 457 __func__, rai->rai_rtpref, ifi->ifi_ifname); 458 goto getconfig_free_rai; 459 } 460 461 MAYHAVE(val, "rltime", rai->rai_maxinterval * 3); 462 if ((uint16_t)val && ((uint16_t)val < rai->rai_maxinterval || 463 (uint16_t)val > MAXROUTERLIFETIME)) { 464 syslog(LOG_ERR, 465 "<%s> router lifetime (%" PRIu32 ") on %s is invalid " 466 "(must be 0 or between %d and %d)", 467 __func__, val, ifi->ifi_ifname, rai->rai_maxinterval, 468 MAXROUTERLIFETIME); 469 goto getconfig_free_rai; 470 } 471 rai->rai_lifetime = val & 0xffff; 472 473 MAYHAVE(val, "rtime", DEF_ADVREACHABLETIME); 474 if (val < 0 || val > MAXREACHABLETIME) { 475 syslog(LOG_ERR, 476 "<%s> reachable time (%" PRIu32 ") on %s is invalid " 477 "(must be no greater than %d)", 478 __func__, val, ifi->ifi_ifname, MAXREACHABLETIME); 479 goto getconfig_free_rai; 480 } 481 rai->rai_reachabletime = (uint32_t)val; 482 483 MAYHAVE(val64, "retrans", DEF_ADVRETRANSTIMER); 484 if (val64 < 0 || val64 > 0xffffffff) { 485 syslog(LOG_ERR, "<%s> retrans time (%" PRIu64 ") on %s out of range", 486 __func__, val64, ifi->ifi_ifname); 487 goto getconfig_free_rai; 488 } 489 rai->rai_retranstimer = (uint32_t)val64; 490 491 if (agetnum("hapref") != -1 || agetnum("hatime") != -1) { 492 syslog(LOG_ERR, 493 "<%s> mobile-ip6 configuration not supported", 494 __func__); 495 goto getconfig_free_rai; 496 } 497 /* prefix information */ 498 499 /* 500 * This is an implementation specific parameter to consider 501 * link propagation delays and poorly synchronized clocks when 502 * checking consistency of advertised lifetimes. 503 */ 504 MAYHAVE(val, "clockskew", 0); 505 rai->rai_clockskew = val; 506 507 rai->rai_pfxs = 0; 508 for (i = -1; i < MAXPREFIX; i++) { 509 struct prefix *pfx; 510 511 makeentry(entbuf, sizeof(entbuf), i, "addr"); 512 addr = (char *)agetstr(entbuf, &bp); 513 if (addr == NULL) 514 continue; 515 516 /* allocate memory to store prefix information */ 517 ELM_MALLOC(pfx, exit(1)); 518 pfx->pfx_rainfo = rai; 519 pfx->pfx_origin = PREFIX_FROM_CONFIG; 520 521 if (inet_pton(AF_INET6, addr, &pfx->pfx_prefix) != 1) { 522 syslog(LOG_ERR, 523 "<%s> inet_pton failed for %s", 524 __func__, addr); 525 goto getconfig_free_pfx; 526 } 527 if (IN6_IS_ADDR_MULTICAST(&pfx->pfx_prefix)) { 528 syslog(LOG_ERR, 529 "<%s> multicast prefix (%s) must " 530 "not be advertised on %s", 531 __func__, addr, ifi->ifi_ifname); 532 goto getconfig_free_pfx; 533 } 534 if (IN6_IS_ADDR_LINKLOCAL(&pfx->pfx_prefix)) 535 syslog(LOG_NOTICE, 536 "<%s> link-local prefix (%s) will be" 537 " advertised on %s", 538 __func__, addr, ifi->ifi_ifname); 539 540 makeentry(entbuf, sizeof(entbuf), i, "prefixlen"); 541 MAYHAVE(val, entbuf, 64); 542 if (val < 0 || val > 128) { 543 syslog(LOG_ERR, "<%s> prefixlen (%" PRIu32 ") for %s " 544 "on %s out of range", 545 __func__, val, addr, ifi->ifi_ifname); 546 goto getconfig_free_pfx; 547 } 548 pfx->pfx_prefixlen = (int)val; 549 550 makeentry(entbuf, sizeof(entbuf), i, "pinfoflags"); 551 if ((flagstr = (char *)agetstr(entbuf, &bp))) { 552 val = 0; 553 if (strchr(flagstr, 'l')) 554 val |= ND_OPT_PI_FLAG_ONLINK; 555 if (strchr(flagstr, 'a')) 556 val |= ND_OPT_PI_FLAG_AUTO; 557 } else { 558 MAYHAVE(val, entbuf, 559 (ND_OPT_PI_FLAG_ONLINK|ND_OPT_PI_FLAG_AUTO)); 560 } 561 pfx->pfx_onlinkflg = val & ND_OPT_PI_FLAG_ONLINK; 562 pfx->pfx_autoconfflg = val & ND_OPT_PI_FLAG_AUTO; 563 564 makeentry(entbuf, sizeof(entbuf), i, "vltime"); 565 MAYHAVE(val64, entbuf, DEF_ADVVALIDLIFETIME); 566 if (val64 < 0 || val64 > 0xffffffff) { 567 syslog(LOG_ERR, "<%s> vltime (%" PRIu64 ") for " 568 "%s/%d on %s is out of range", 569 __func__, val64, 570 addr, pfx->pfx_prefixlen, ifi->ifi_ifname); 571 goto getconfig_free_pfx; 572 } 573 pfx->pfx_validlifetime = (uint32_t)val64; 574 575 makeentry(entbuf, sizeof(entbuf), i, "vltimedecr"); 576 if (agetflag(entbuf)) { 577 struct timespec now; 578 579 clock_gettime(CLOCK_MONOTONIC_FAST, &now); 580 pfx->pfx_vltimeexpire = 581 now.tv_sec + pfx->pfx_validlifetime; 582 } 583 584 makeentry(entbuf, sizeof(entbuf), i, "pltime"); 585 MAYHAVE(val64, entbuf, DEF_ADVPREFERREDLIFETIME); 586 if (val64 < 0 || val64 > 0xffffffff) { 587 syslog(LOG_ERR, 588 "<%s> pltime (%" PRIu64 ") for %s/%d on %s " 589 "is out of range", 590 __func__, val64, 591 addr, pfx->pfx_prefixlen, ifi->ifi_ifname); 592 goto getconfig_free_pfx; 593 } 594 pfx->pfx_preflifetime = (uint32_t)val64; 595 596 makeentry(entbuf, sizeof(entbuf), i, "pltimedecr"); 597 if (agetflag(entbuf)) { 598 struct timespec now; 599 600 clock_gettime(CLOCK_MONOTONIC_FAST, &now); 601 pfx->pfx_pltimeexpire = 602 now.tv_sec + pfx->pfx_preflifetime; 603 } 604 /* link into chain */ 605 TAILQ_INSERT_TAIL(&rai->rai_prefix, pfx, pfx_next); 606 rai->rai_pfxs++; 607 continue; 608 getconfig_free_pfx: 609 free(pfx); 610 } 611 if (rai->rai_advifprefix && rai->rai_pfxs == 0) 612 get_prefix(rai); 613 614 MAYHAVE(val64, "mtu", 0); 615 if (val64 < 0 || val64 > 0xffffffff) { 616 syslog(LOG_ERR, 617 "<%s> mtu (%" PRIu64 ") on %s out of range", 618 __func__, val64, ifi->ifi_ifname); 619 goto getconfig_free_rai; 620 } 621 rai->rai_linkmtu = (uint32_t)val64; 622 if (rai->rai_linkmtu == 0) { 623 char *mtustr; 624 625 if ((mtustr = (char *)agetstr("mtu", &bp)) && 626 strcmp(mtustr, "auto") == 0) 627 rai->rai_linkmtu = ifi->ifi_phymtu; 628 } 629 else if (rai->rai_linkmtu < IPV6_MMTU || 630 rai->rai_linkmtu > ifi->ifi_phymtu) { 631 syslog(LOG_ERR, 632 "<%s> advertised link mtu (%" PRIu32 ") on %s is invalid (must " 633 "be between least MTU (%d) and physical link MTU (%d)", 634 __func__, rai->rai_linkmtu, ifi->ifi_ifname, 635 IPV6_MMTU, ifi->ifi_phymtu); 636 goto getconfig_free_rai; 637 } 638 639 #ifdef SIOCSIFINFO_IN6 640 { 641 struct in6_ndireq ndi; 642 int s; 643 644 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) { 645 syslog(LOG_ERR, "<%s> socket: %s", __func__, 646 strerror(errno)); 647 exit(1); 648 } 649 memset(&ndi, 0, sizeof(ndi)); 650 strlcpy(ndi.ifname, ifi->ifi_ifname, sizeof(ndi.ifname)); 651 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&ndi) < 0) 652 syslog(LOG_INFO, "<%s> ioctl:SIOCGIFINFO_IN6 at %s: %s", 653 __func__, ifi->ifi_ifname, strerror(errno)); 654 655 /* reflect the RA info to the host variables in kernel */ 656 ndi.ndi.chlim = rai->rai_hoplimit; 657 ndi.ndi.retrans = rai->rai_retranstimer; 658 ndi.ndi.basereachable = rai->rai_reachabletime; 659 if (ioctl(s, SIOCSIFINFO_IN6, (caddr_t)&ndi) < 0) 660 syslog(LOG_INFO, "<%s> ioctl:SIOCSIFINFO_IN6 at %s: %s", 661 __func__, ifi->ifi_ifname, strerror(errno)); 662 663 close(s); 664 } 665 #endif 666 667 /* route information */ 668 rai->rai_routes = 0; 669 for (i = -1; i < MAXROUTE; i++) { 670 struct rtinfo *rti; 671 672 makeentry(entbuf, sizeof(entbuf), i, "rtprefix"); 673 addr = (char *)agetstr(entbuf, &bp); 674 if (addr == NULL) { 675 makeentry(oentbuf, sizeof(oentbuf), i, "rtrprefix"); 676 addr = (char *)agetstr(oentbuf, &bp); 677 if (addr) 678 fprintf(stderr, "%s was obsoleted. Use %s.\n", 679 oentbuf, entbuf); 680 } 681 if (addr == NULL) 682 continue; 683 684 /* allocate memory to store prefix information */ 685 ELM_MALLOC(rti, exit(1)); 686 687 if (inet_pton(AF_INET6, addr, &rti->rti_prefix) != 1) { 688 syslog(LOG_ERR, "<%s> inet_pton failed for %s", 689 __func__, addr); 690 goto getconfig_free_rti; 691 } 692 #if 0 693 /* 694 * XXX: currently there's no restriction in route information 695 * prefix according to 696 * draft-ietf-ipngwg-router-selection-00.txt. 697 * However, I think the similar restriction be necessary. 698 */ 699 MAYHAVE(val64, entbuf, DEF_ADVVALIDLIFETIME); 700 if (IN6_IS_ADDR_MULTICAST(&rti->prefix)) { 701 syslog(LOG_ERR, 702 "<%s> multicast route (%s) must " 703 "not be advertised on %s", 704 __func__, addr, ifi->ifi_ifname); 705 goto getconfig_free_rti; 706 } 707 if (IN6_IS_ADDR_LINKLOCAL(&rti->prefix)) { 708 syslog(LOG_NOTICE, 709 "<%s> link-local route (%s) will " 710 "be advertised on %s", 711 __func__, addr, ifi->ifi_ifname); 712 goto getconfig_free_rti; 713 } 714 #endif 715 716 makeentry(entbuf, sizeof(entbuf), i, "rtplen"); 717 /* XXX: 256 is a magic number for compatibility check. */ 718 MAYHAVE(val, entbuf, 256); 719 if (val == 256) { 720 makeentry(oentbuf, sizeof(oentbuf), i, "rtrplen"); 721 MAYHAVE(val, oentbuf, 256); 722 if (val != 256) 723 fprintf(stderr, "%s was obsoleted. Use %s.\n", 724 oentbuf, entbuf); 725 else 726 val = 64; 727 } 728 if (val < 0 || val > 128) { 729 syslog(LOG_ERR, "<%s> prefixlen (%" PRIu32 ") for %s on %s " 730 "out of range", 731 __func__, val, addr, ifi->ifi_ifname); 732 goto getconfig_free_rti; 733 } 734 rti->rti_prefixlen = (int)val; 735 736 makeentry(entbuf, sizeof(entbuf), i, "rtflags"); 737 if ((flagstr = (char *)agetstr(entbuf, &bp))) { 738 val = 0; 739 if (strchr(flagstr, 'h')) 740 val |= ND_RA_FLAG_RTPREF_HIGH; 741 if (strchr(flagstr, 'l')) { 742 if ((val & ND_RA_FLAG_RTPREF_HIGH)) { 743 syslog(LOG_ERR, 744 "<%s> the \'h\' and \'l\' route" 745 " preferences are exclusive", 746 __func__); 747 goto getconfig_free_rti; 748 } 749 val |= ND_RA_FLAG_RTPREF_LOW; 750 } 751 } else 752 MAYHAVE(val, entbuf, 256); /* XXX */ 753 if (val == 256) { 754 makeentry(oentbuf, sizeof(oentbuf), i, "rtrflags"); 755 MAYHAVE(val, oentbuf, 256); 756 if (val != 256) { 757 fprintf(stderr, "%s was obsoleted. Use %s.\n", 758 oentbuf, entbuf); 759 } else 760 val = 0; 761 } 762 rti->rti_rtpref = val & ND_RA_FLAG_RTPREF_MASK; 763 if (rti->rti_rtpref == ND_RA_FLAG_RTPREF_RSV) { 764 syslog(LOG_ERR, "<%s> invalid route preference (%02x) " 765 "for %s/%d on %s", 766 __func__, rti->rti_rtpref, addr, 767 rti->rti_prefixlen, ifi->ifi_ifname); 768 goto getconfig_free_rti; 769 } 770 771 /* 772 * Since the spec does not a default value, we should make 773 * this entry mandatory. However, FreeBSD 4.4 has shipped 774 * with this field being optional, we use the router lifetime 775 * as an ad-hoc default value with a warning message. 776 */ 777 makeentry(entbuf, sizeof(entbuf), i, "rtltime"); 778 MAYHAVE(val64, entbuf, -1); 779 if (val64 == -1) { 780 makeentry(oentbuf, sizeof(oentbuf), i, "rtrltime"); 781 MAYHAVE(val64, oentbuf, -1); 782 if (val64 != -1) 783 fprintf(stderr, "%s was obsoleted. Use %s.\n", 784 oentbuf, entbuf); 785 else { 786 fprintf(stderr, "%s should be specified " 787 "for interface %s.\n", entbuf, 788 ifi->ifi_ifname); 789 val64 = rai->rai_lifetime; 790 } 791 } 792 if (val64 < 0 || val64 > 0xffffffff) { 793 syslog(LOG_ERR, "<%s> route lifetime (%" PRIu64 ") for " 794 "%s/%d on %s out of range", __func__, 795 val64, addr, rti->rti_prefixlen, 796 ifi->ifi_ifname); 797 goto getconfig_free_rti; 798 } 799 rti->rti_ltime = (uint32_t)val64; 800 801 /* link into chain */ 802 TAILQ_INSERT_TAIL(&rai->rai_route, rti, rti_next); 803 rai->rai_routes++; 804 continue; 805 getconfig_free_rti: 806 free(rti); 807 } 808 809 /* DNS server and DNS search list information */ 810 for (i = -1; i < MAXRDNSSENT ; i++) { 811 struct rdnss *rdn; 812 struct rdnss_addr *rdna; 813 char *ap; 814 int c; 815 816 makeentry(entbuf, sizeof(entbuf), i, "rdnss"); 817 addr = (char *)agetstr(entbuf, &bp); 818 if (addr == NULL) 819 continue; 820 ELM_MALLOC(rdn, exit(1)); 821 822 TAILQ_INIT(&rdn->rd_list); 823 824 for (ap = addr; ap - addr < (ssize_t)strlen(addr); ap += c+1) { 825 c = strcspn(ap, ","); 826 strncpy(abuf, ap, c); 827 abuf[c] = '\0'; 828 ELM_MALLOC(rdna, goto getconfig_free_rdn); 829 if (inet_pton(AF_INET6, abuf, &rdna->ra_dns) != 1) { 830 syslog(LOG_ERR, "<%s> inet_pton failed for %s", 831 __func__, abuf); 832 free(rdna); 833 goto getconfig_free_rdn; 834 } 835 TAILQ_INSERT_TAIL(&rdn->rd_list, rdna, ra_next); 836 } 837 838 makeentry(entbuf, sizeof(entbuf), i, "rdnssltime"); 839 MAYHAVE(val, entbuf, (rai->rai_maxinterval * 3 / 2)); 840 if ((uint16_t)val < rai->rai_maxinterval || 841 (uint16_t)val > rai->rai_maxinterval * 2) { 842 syslog(LOG_ERR, "%s (%" PRIu16 ") on %s is invalid " 843 "(must be between %d and %d)", 844 entbuf, val, ifi->ifi_ifname, rai->rai_maxinterval, 845 rai->rai_maxinterval * 2); 846 goto getconfig_free_rdn; 847 } 848 rdn->rd_ltime = val; 849 850 /* link into chain */ 851 TAILQ_INSERT_TAIL(&rai->rai_rdnss, rdn, rd_next); 852 continue; 853 getconfig_free_rdn: 854 while ((rdna = TAILQ_FIRST(&rdn->rd_list)) != NULL) { 855 TAILQ_REMOVE(&rdn->rd_list, rdna, ra_next); 856 free(rdna); 857 } 858 free(rdn); 859 } 860 861 for (i = -1; i < MAXDNSSLENT ; i++) { 862 struct dnssl *dns; 863 struct dnssl_addr *dnsa; 864 char *ap; 865 int c; 866 867 makeentry(entbuf, sizeof(entbuf), i, "dnssl"); 868 addr = (char *)agetstr(entbuf, &bp); 869 if (addr == NULL) 870 continue; 871 872 ELM_MALLOC(dns, exit(1)); 873 874 TAILQ_INIT(&dns->dn_list); 875 876 for (ap = addr; ap - addr < (ssize_t)strlen(addr); ap += c+1) { 877 c = strcspn(ap, ","); 878 strncpy(abuf, ap, c); 879 abuf[c] = '\0'; 880 ELM_MALLOC(dnsa, goto getconfig_free_dns); 881 dnsa->da_len = dname_labelenc(dnsa->da_dom, abuf); 882 if (dnsa->da_len < 0) { 883 syslog(LOG_ERR, "Invalid dnssl entry: %s", 884 abuf); 885 goto getconfig_free_dns; 886 } 887 syslog(LOG_DEBUG, "<%s>: dnsa->da_len = %d", __func__, 888 dnsa->da_len); 889 TAILQ_INSERT_TAIL(&dns->dn_list, dnsa, da_next); 890 } 891 892 makeentry(entbuf, sizeof(entbuf), i, "dnsslltime"); 893 MAYHAVE(val, entbuf, (rai->rai_maxinterval * 3 / 2)); 894 if ((uint16_t)val < rai->rai_maxinterval || 895 (uint16_t)val > rai->rai_maxinterval * 2) { 896 syslog(LOG_ERR, "%s (%" PRIu16 ") on %s is invalid " 897 "(must be between %d and %d)", 898 entbuf, val, ifi->ifi_ifname, rai->rai_maxinterval, 899 rai->rai_maxinterval * 2); 900 goto getconfig_free_dns; 901 } 902 dns->dn_ltime = val; 903 904 /* link into chain */ 905 TAILQ_INSERT_TAIL(&rai->rai_dnssl, dns, dn_next); 906 continue; 907 getconfig_free_dns: 908 while ((dnsa = TAILQ_FIRST(&dns->dn_list)) != NULL) { 909 TAILQ_REMOVE(&dns->dn_list, dnsa, da_next); 910 free(dnsa); 911 } 912 free(dns); 913 } 914 915 /* 916 * handle pref64 917 */ 918 for (i = -1; i < MAXPREF64 ; i++) { 919 struct pref64 *prf64; 920 921 makeentry(entbuf, sizeof(entbuf), i, "pref64"); 922 addr = (char *)agetstr(entbuf, &bp); 923 if (addr == NULL) 924 continue; 925 ELM_MALLOC(prf64, exit(1)); 926 927 if (inet_pton(AF_INET6, addr, &prf64->p64_prefix) != 1) { 928 syslog(LOG_ERR, "<%s> inet_pton failed for %s", 929 __func__, addr); 930 goto getconfig_free_prf64; 931 } 932 933 makeentry(entbuf, sizeof(entbuf), i, "pref64len"); 934 MAYHAVE(val64, entbuf, 96); 935 switch (val64) { 936 case 96: 937 prf64->p64_plc = 0; 938 break; 939 case 64: 940 prf64->p64_plc = 1; 941 break; 942 case 56: 943 prf64->p64_plc = 2; 944 break; 945 case 48: 946 prf64->p64_plc = 3; 947 break; 948 case 40: 949 prf64->p64_plc = 4; 950 break; 951 case 32: 952 prf64->p64_plc = 5; 953 break; 954 default: 955 syslog(LOG_ERR, "PREF64 prefix length %" PRIi64 956 "on %s is invalid; skipping", 957 val64, ifi->ifi_ifname); 958 goto getconfig_free_prf64; 959 } 960 961 makeentry(entbuf, sizeof(entbuf), i, "pref64lifetime"); 962 MAYHAVE(val64, entbuf, (rai->rai_lifetime * 3)); 963 /* This logic is from RFC 8781 section 4.1. */ 964 if (val64 > 65528) 965 val64 = 65528; 966 val64 = (val64 + 7) / 8; 967 prf64->p64_sl = (uint16_t)val64; 968 969 /* link into chain */ 970 TAILQ_INSERT_TAIL(&rai->rai_pref64, prf64, p64_next); 971 continue; 972 getconfig_free_prf64: 973 free(prf64); 974 } 975 976 /* construct the sending packet */ 977 make_packet(rai); 978 979 /* 980 * If an entry with the same ifindex exists, remove it first. 981 * Before the removal, RDNSS and DNSSL options with 982 * zero-lifetime will be sent. 983 */ 984 switch (ifi->ifi_state) { 985 case IFI_STATE_UNCONFIGURED: 986 /* UNCONFIGURED -> TRANSITIVE */ 987 988 error = sock_mc_join(&sock, ifi->ifi_ifindex); 989 if (error) 990 exit(1); 991 992 ifi->ifi_state = IFI_STATE_TRANSITIVE; 993 ifi->ifi_burstcount = MAX_INITIAL_RTR_ADVERTISEMENTS; 994 ifi->ifi_burstinterval = MAX_INITIAL_RTR_ADVERT_INTERVAL; 995 996 /* The same two rai mean initial burst */ 997 ifi->ifi_rainfo = rai; 998 ifi->ifi_rainfo_trans = rai; 999 TAILQ_INSERT_TAIL(&railist, rai, rai_next); 1000 1001 if (ifi->ifi_ra_timer == NULL) 1002 ifi->ifi_ra_timer = rtadvd_add_timer(ra_timeout, 1003 ra_timer_update, ifi, ifi); 1004 ra_timer_update(ifi, &ifi->ifi_ra_timer->rat_tm); 1005 rtadvd_set_timer(&ifi->ifi_ra_timer->rat_tm, 1006 ifi->ifi_ra_timer); 1007 1008 syslog(LOG_DEBUG, 1009 "<%s> ifname=%s marked as TRANSITIVE (initial burst).", 1010 __func__, ifi->ifi_ifname); 1011 break; 1012 case IFI_STATE_CONFIGURED: 1013 /* CONFIGURED -> TRANSITIVE */ 1014 rai_old = ifi->ifi_rainfo; 1015 if (rai_old == NULL) { 1016 syslog(LOG_ERR, 1017 "<%s> ifi_rainfo is NULL" 1018 " in IFI_STATE_CONFIGURED.", __func__); 1019 ifi = NULL; 1020 break; 1021 } else { 1022 struct rdnss *rdn; 1023 struct dnssl *dns; 1024 struct rtinfo *rti; 1025 1026 rai_old->rai_lifetime = 0; 1027 TAILQ_FOREACH(rdn, &rai_old->rai_rdnss, rd_next) 1028 rdn->rd_ltime = 0; 1029 TAILQ_FOREACH(dns, &rai_old->rai_dnssl, dn_next) 1030 dns->dn_ltime = 0; 1031 TAILQ_FOREACH(rti, &rai_old->rai_route, rti_next) 1032 rti->rti_ltime = 0; 1033 1034 ifi->ifi_rainfo_trans = rai_old; 1035 ifi->ifi_state = IFI_STATE_TRANSITIVE; 1036 ifi->ifi_burstcount = MAX_FINAL_RTR_ADVERTISEMENTS; 1037 ifi->ifi_burstinterval = MIN_DELAY_BETWEEN_RAS; 1038 1039 ra_timer_update(ifi, &ifi->ifi_ra_timer->rat_tm); 1040 rtadvd_set_timer(&ifi->ifi_ra_timer->rat_tm, 1041 ifi->ifi_ra_timer); 1042 1043 syslog(LOG_DEBUG, 1044 "<%s> ifname=%s marked as TRANSITIVE" 1045 " (transitional burst)", 1046 __func__, ifi->ifi_ifname); 1047 } 1048 ifi->ifi_rainfo = rai; 1049 TAILQ_INSERT_TAIL(&railist, rai, rai_next); 1050 break; 1051 case IFI_STATE_TRANSITIVE: 1052 if (ifi->ifi_rainfo != NULL) { 1053 if (ifi->ifi_rainfo == ifi->ifi_rainfo_trans) { 1054 /* Reinitialize initial burst */ 1055 rm_rainfo(ifi->ifi_rainfo); 1056 ifi->ifi_rainfo = rai; 1057 ifi->ifi_rainfo_trans = rai; 1058 ifi->ifi_burstcount = 1059 MAX_INITIAL_RTR_ADVERTISEMENTS; 1060 ifi->ifi_burstinterval = 1061 MAX_INITIAL_RTR_ADVERT_INTERVAL; 1062 } else { 1063 /* Replace ifi_rainfo with the new one */ 1064 rm_rainfo(ifi->ifi_rainfo); 1065 ifi->ifi_rainfo = rai; 1066 } 1067 TAILQ_INSERT_TAIL(&railist, rai, rai_next); 1068 1069 ra_timer_update(ifi, &ifi->ifi_ra_timer->rat_tm); 1070 rtadvd_set_timer(&ifi->ifi_ra_timer->rat_tm, 1071 ifi->ifi_ra_timer); 1072 } else { 1073 /* XXX: NOTREACHED. Being shut down. */ 1074 syslog(LOG_ERR, 1075 "<%s> %s is shutting down. Skipped.", 1076 __func__, ifi->ifi_ifname); 1077 rm_rainfo(rai); 1078 1079 return (NULL); 1080 } 1081 break; 1082 } 1083 1084 return (ifi); 1085 1086 getconfig_free_rai: 1087 free(rai); 1088 return (NULL); 1089 } 1090 1091 void 1092 get_prefix(struct rainfo *rai) 1093 { 1094 struct ifaddrs *ifap, *ifa; 1095 struct prefix *pfx; 1096 struct in6_addr *a; 1097 struct ifinfo *ifi; 1098 char *p, *ep, *m, *lim; 1099 char ntopbuf[INET6_ADDRSTRLEN]; 1100 1101 if (getifaddrs(&ifap) < 0) { 1102 syslog(LOG_ERR, 1103 "<%s> can't get interface addresses", 1104 __func__); 1105 exit(1); 1106 } 1107 ifi = rai->rai_ifinfo; 1108 1109 for (ifa = ifap; ifa; ifa = ifa->ifa_next) { 1110 int plen; 1111 1112 if (strcmp(ifa->ifa_name, ifi->ifi_ifname) != 0) 1113 continue; 1114 if (ifa->ifa_addr->sa_family != AF_INET6) 1115 continue; 1116 a = &((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr; 1117 if (IN6_IS_ADDR_LINKLOCAL(a)) 1118 continue; 1119 1120 /* get prefix length */ 1121 m = (char *)&((struct sockaddr_in6 *)ifa->ifa_netmask)->sin6_addr; 1122 lim = (char *)(ifa->ifa_netmask) + ifa->ifa_netmask->sa_len; 1123 plen = prefixlen(m, lim); 1124 if (plen <= 0 || plen > 128) { 1125 syslog(LOG_ERR, "<%s> failed to get prefixlen " 1126 "or prefix is invalid", 1127 __func__); 1128 exit(1); 1129 } 1130 if (plen == 128) /* XXX */ 1131 continue; 1132 if (find_prefix(rai, a, plen)) { 1133 /* ignore a duplicated prefix. */ 1134 continue; 1135 } 1136 1137 /* allocate memory to store prefix info. */ 1138 ELM_MALLOC(pfx, exit(1)); 1139 1140 /* set prefix, sweep bits outside of prefixlen */ 1141 pfx->pfx_prefixlen = plen; 1142 memcpy(&pfx->pfx_prefix, a, sizeof(*a)); 1143 p = (char *)&pfx->pfx_prefix; 1144 ep = (char *)(&pfx->pfx_prefix + 1); 1145 while (m < lim && p < ep) 1146 *p++ &= *m++; 1147 while (p < ep) 1148 *p++ = 0x00; 1149 if (!inet_ntop(AF_INET6, &pfx->pfx_prefix, ntopbuf, 1150 sizeof(ntopbuf))) { 1151 syslog(LOG_ERR, "<%s> inet_ntop failed", __func__); 1152 exit(1); 1153 } 1154 syslog(LOG_DEBUG, 1155 "<%s> add %s/%d to prefix list on %s", 1156 __func__, ntopbuf, pfx->pfx_prefixlen, ifi->ifi_ifname); 1157 1158 /* set other fields with protocol defaults */ 1159 pfx->pfx_validlifetime = DEF_ADVVALIDLIFETIME; 1160 pfx->pfx_preflifetime = DEF_ADVPREFERREDLIFETIME; 1161 pfx->pfx_onlinkflg = 1; 1162 pfx->pfx_autoconfflg = 1; 1163 pfx->pfx_origin = PREFIX_FROM_KERNEL; 1164 pfx->pfx_rainfo = rai; 1165 1166 /* link into chain */ 1167 TAILQ_INSERT_TAIL(&rai->rai_prefix, pfx, pfx_next); 1168 1169 /* counter increment */ 1170 rai->rai_pfxs++; 1171 } 1172 1173 freeifaddrs(ifap); 1174 } 1175 1176 static void 1177 makeentry(char *buf, size_t len, int id, const char *string) 1178 { 1179 1180 if (id < 0) 1181 strlcpy(buf, string, len); 1182 else 1183 snprintf(buf, len, "%s%d", string, id); 1184 } 1185 1186 /* 1187 * Add a prefix to the list of specified interface and reconstruct 1188 * the outgoing packet. 1189 * The prefix must not be in the list. 1190 * XXX: other parameters of the prefix (e.g. lifetime) should be 1191 * able to be specified. 1192 */ 1193 static void 1194 add_prefix(struct rainfo *rai, struct in6_prefixreq *ipr) 1195 { 1196 struct prefix *pfx; 1197 struct ifinfo *ifi; 1198 char ntopbuf[INET6_ADDRSTRLEN]; 1199 1200 ifi = rai->rai_ifinfo; 1201 ELM_MALLOC(pfx, return); 1202 pfx->pfx_prefix = ipr->ipr_prefix.sin6_addr; 1203 pfx->pfx_prefixlen = ipr->ipr_plen; 1204 pfx->pfx_validlifetime = ipr->ipr_vltime; 1205 pfx->pfx_preflifetime = ipr->ipr_pltime; 1206 pfx->pfx_onlinkflg = ipr->ipr_raf_onlink; 1207 pfx->pfx_autoconfflg = ipr->ipr_raf_auto; 1208 pfx->pfx_origin = PREFIX_FROM_DYNAMIC; 1209 pfx->pfx_rainfo = rai; 1210 1211 TAILQ_INSERT_TAIL(&rai->rai_prefix, pfx, pfx_next); 1212 1213 syslog(LOG_DEBUG, "<%s> new prefix %s/%d was added on %s", 1214 __func__, 1215 inet_ntop(AF_INET6, &ipr->ipr_prefix.sin6_addr, ntopbuf, 1216 sizeof(ntopbuf)), ipr->ipr_plen, ifi->ifi_ifname); 1217 1218 rai->rai_pfxs++; 1219 } 1220 1221 /* 1222 * Delete a prefix to the list of specified interface and reconstruct 1223 * the outgoing packet. 1224 * The prefix must be in the list. 1225 */ 1226 void 1227 delete_prefix(struct prefix *pfx) 1228 { 1229 struct rainfo *rai; 1230 struct ifinfo *ifi; 1231 char ntopbuf[INET6_ADDRSTRLEN]; 1232 1233 rai = pfx->pfx_rainfo; 1234 ifi = rai->rai_ifinfo; 1235 TAILQ_REMOVE(&rai->rai_prefix, pfx, pfx_next); 1236 syslog(LOG_DEBUG, "<%s> prefix %s/%d was deleted on %s", 1237 __func__, 1238 inet_ntop(AF_INET6, &pfx->pfx_prefix, ntopbuf, 1239 sizeof(ntopbuf)), pfx->pfx_prefixlen, ifi->ifi_ifname); 1240 if (pfx->pfx_timer) 1241 rtadvd_remove_timer(pfx->pfx_timer); 1242 free(pfx); 1243 1244 rai->rai_pfxs--; 1245 } 1246 1247 void 1248 invalidate_prefix(struct prefix *pfx) 1249 { 1250 struct timespec timo; 1251 struct rainfo *rai; 1252 struct ifinfo *ifi; 1253 char ntopbuf[INET6_ADDRSTRLEN]; 1254 1255 rai = pfx->pfx_rainfo; 1256 ifi = rai->rai_ifinfo; 1257 if (pfx->pfx_timer) { /* sanity check */ 1258 syslog(LOG_ERR, 1259 "<%s> assumption failure: timer already exists", 1260 __func__); 1261 exit(1); 1262 } 1263 1264 syslog(LOG_DEBUG, "<%s> prefix %s/%d was invalidated on %s, " 1265 "will expire in %ld seconds", __func__, 1266 inet_ntop(AF_INET6, &pfx->pfx_prefix, ntopbuf, sizeof(ntopbuf)), 1267 pfx->pfx_prefixlen, ifi->ifi_ifname, (long)prefix_timo); 1268 1269 /* set the expiration timer */ 1270 pfx->pfx_timer = rtadvd_add_timer(prefix_timeout, NULL, pfx, NULL); 1271 if (pfx->pfx_timer == NULL) { 1272 syslog(LOG_ERR, "<%s> failed to add a timer for a prefix. " 1273 "remove the prefix", __func__); 1274 delete_prefix(pfx); 1275 } 1276 timo.tv_sec = prefix_timo; 1277 timo.tv_nsec = 0; 1278 rtadvd_set_timer(&timo, pfx->pfx_timer); 1279 } 1280 1281 static struct rtadvd_timer * 1282 prefix_timeout(void *arg) 1283 { 1284 1285 delete_prefix((struct prefix *)arg); 1286 1287 return (NULL); 1288 } 1289 1290 void 1291 update_prefix(struct prefix *pfx) 1292 { 1293 struct rainfo *rai; 1294 struct ifinfo *ifi; 1295 char ntopbuf[INET6_ADDRSTRLEN]; 1296 1297 rai = pfx->pfx_rainfo; 1298 ifi = rai->rai_ifinfo; 1299 if (pfx->pfx_timer == NULL) { /* sanity check */ 1300 syslog(LOG_ERR, 1301 "<%s> assumption failure: timer does not exist", 1302 __func__); 1303 exit(1); 1304 } 1305 1306 syslog(LOG_DEBUG, "<%s> prefix %s/%d was re-enabled on %s", 1307 __func__, inet_ntop(AF_INET6, &pfx->pfx_prefix, ntopbuf, 1308 sizeof(ntopbuf)), pfx->pfx_prefixlen, ifi->ifi_ifname); 1309 1310 /* stop the expiration timer */ 1311 rtadvd_remove_timer(pfx->pfx_timer); 1312 pfx->pfx_timer = NULL; 1313 } 1314 1315 /* 1316 * Try to get an in6_prefixreq contents for a prefix which matches 1317 * ipr->ipr_prefix and ipr->ipr_plen and belongs to 1318 * the interface whose name is ipr->ipr_name[]. 1319 */ 1320 static int 1321 init_prefix(struct in6_prefixreq *ipr) 1322 { 1323 #if 0 1324 int s; 1325 1326 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) { 1327 syslog(LOG_ERR, "<%s> socket: %s", __func__, 1328 strerror(errno)); 1329 exit(1); 1330 } 1331 1332 if (ioctl(s, SIOCGIFPREFIX_IN6, (caddr_t)ipr) < 0) { 1333 syslog(LOG_INFO, "<%s> ioctl:SIOCGIFPREFIX %s", __func__, 1334 strerror(errno)); 1335 1336 ipr->ipr_vltime = DEF_ADVVALIDLIFETIME; 1337 ipr->ipr_pltime = DEF_ADVPREFERREDLIFETIME; 1338 ipr->ipr_raf_onlink = 1; 1339 ipr->ipr_raf_auto = 1; 1340 /* omit other field initialization */ 1341 } 1342 else if (ipr->ipr_origin < PR_ORIG_RR) { 1343 char ntopbuf[INET6_ADDRSTRLEN]; 1344 1345 syslog(LOG_WARNING, "<%s> Added prefix(%s)'s origin %d is" 1346 "lower than PR_ORIG_RR(router renumbering)." 1347 "This should not happen if I am router", __func__, 1348 inet_ntop(AF_INET6, &ipr->ipr_prefix.sin6_addr, ntopbuf, 1349 sizeof(ntopbuf)), ipr->ipr_origin); 1350 close(s); 1351 return (1); 1352 } 1353 1354 close(s); 1355 return (0); 1356 #else 1357 ipr->ipr_vltime = DEF_ADVVALIDLIFETIME; 1358 ipr->ipr_pltime = DEF_ADVPREFERREDLIFETIME; 1359 ipr->ipr_raf_onlink = 1; 1360 ipr->ipr_raf_auto = 1; 1361 return (0); 1362 #endif 1363 } 1364 1365 void 1366 make_prefix(struct rainfo *rai, int ifindex, struct in6_addr *addr, int plen) 1367 { 1368 struct in6_prefixreq ipr; 1369 1370 memset(&ipr, 0, sizeof(ipr)); 1371 if (if_indextoname(ifindex, ipr.ipr_name) == NULL) { 1372 syslog(LOG_ERR, "<%s> Prefix added interface No.%d doesn't " 1373 "exist. This should not happen! %s", __func__, 1374 ifindex, strerror(errno)); 1375 exit(1); 1376 } 1377 ipr.ipr_prefix.sin6_len = sizeof(ipr.ipr_prefix); 1378 ipr.ipr_prefix.sin6_family = AF_INET6; 1379 ipr.ipr_prefix.sin6_addr = *addr; 1380 ipr.ipr_plen = plen; 1381 1382 if (init_prefix(&ipr)) 1383 return; /* init failed by some error */ 1384 add_prefix(rai, &ipr); 1385 } 1386 1387 void 1388 make_packet(struct rainfo *rai) 1389 { 1390 size_t packlen, lladdroptlen = 0; 1391 char *buf; 1392 struct nd_router_advert *ra; 1393 struct nd_opt_prefix_info *ndopt_pi; 1394 struct nd_opt_mtu *ndopt_mtu; 1395 struct nd_opt_route_info *ndopt_rti; 1396 struct rtinfo *rti; 1397 struct nd_opt_rdnss *ndopt_rdnss; 1398 struct rdnss *rdn; 1399 struct nd_opt_dnssl *ndopt_dnssl; 1400 struct dnssl *dns; 1401 struct pref64 *prf64; 1402 struct nd_opt_pref64 *ndopt_pref64; 1403 size_t len; 1404 struct prefix *pfx; 1405 struct ifinfo *ifi; 1406 1407 ifi = rai->rai_ifinfo; 1408 /* calculate total length */ 1409 packlen = sizeof(struct nd_router_advert); 1410 if (rai->rai_advlinkopt) { 1411 if ((lladdroptlen = lladdropt_length(&ifi->ifi_sdl)) == 0) { 1412 syslog(LOG_INFO, 1413 "<%s> link-layer address option has" 1414 " null length on %s. Treat as not included.", 1415 __func__, ifi->ifi_ifname); 1416 rai->rai_advlinkopt = 0; 1417 } 1418 packlen += lladdroptlen; 1419 } 1420 if (rai->rai_pfxs) 1421 packlen += sizeof(struct nd_opt_prefix_info) * rai->rai_pfxs; 1422 if (rai->rai_linkmtu) 1423 packlen += sizeof(struct nd_opt_mtu); 1424 1425 TAILQ_FOREACH(rti, &rai->rai_route, rti_next) 1426 packlen += sizeof(struct nd_opt_route_info) + 1427 ((rti->rti_prefixlen + 0x3f) >> 6) * 8; 1428 1429 TAILQ_FOREACH(rdn, &rai->rai_rdnss, rd_next) { 1430 struct rdnss_addr *rdna; 1431 1432 packlen += sizeof(struct nd_opt_rdnss); 1433 TAILQ_FOREACH(rdna, &rdn->rd_list, ra_next) 1434 packlen += sizeof(rdna->ra_dns); 1435 } 1436 TAILQ_FOREACH(dns, &rai->rai_dnssl, dn_next) { 1437 struct dnssl_addr *dnsa; 1438 1439 packlen += sizeof(struct nd_opt_dnssl); 1440 len = 0; 1441 TAILQ_FOREACH(dnsa, &dns->dn_list, da_next) 1442 len += dnsa->da_len; 1443 1444 /* A zero octet and 8 octet boundary */ 1445 len++; 1446 len += (len % 8) ? 8 - len % 8 : 0; 1447 1448 packlen += len; 1449 } 1450 TAILQ_FOREACH(prf64, &rai->rai_pref64, p64_next) 1451 packlen += sizeof(struct nd_opt_pref64); 1452 1453 /* allocate memory for the packet */ 1454 if ((buf = malloc(packlen)) == NULL) { 1455 syslog(LOG_ERR, 1456 "<%s> can't get enough memory for an RA packet", 1457 __func__); 1458 exit(1); 1459 } 1460 memset(buf, 0, packlen); 1461 if (rai->rai_ra_data) /* Free old data if any. */ 1462 free(rai->rai_ra_data); 1463 rai->rai_ra_data = buf; 1464 /* XXX: what if packlen > 576? */ 1465 rai->rai_ra_datalen = packlen; 1466 1467 /* 1468 * construct the packet 1469 */ 1470 ra = (struct nd_router_advert *)buf; 1471 ra->nd_ra_type = ND_ROUTER_ADVERT; 1472 ra->nd_ra_code = 0; 1473 ra->nd_ra_cksum = 0; 1474 ra->nd_ra_curhoplimit = (uint8_t)(0xff & rai->rai_hoplimit); 1475 /* 1476 * XXX: the router preference field, which is a 2-bit field, should be 1477 * initialized before other fields. 1478 */ 1479 ra->nd_ra_flags_reserved = 0xff & rai->rai_rtpref; 1480 ra->nd_ra_flags_reserved |= 1481 rai->rai_managedflg ? ND_RA_FLAG_MANAGED : 0; 1482 ra->nd_ra_flags_reserved |= 1483 rai->rai_otherflg ? ND_RA_FLAG_OTHER : 0; 1484 ra->nd_ra_router_lifetime = htons(rai->rai_lifetime); 1485 ra->nd_ra_reachable = htonl(rai->rai_reachabletime); 1486 ra->nd_ra_retransmit = htonl(rai->rai_retranstimer); 1487 buf += sizeof(*ra); 1488 1489 if (rai->rai_advlinkopt) { 1490 lladdropt_fill(&ifi->ifi_sdl, (struct nd_opt_hdr *)buf); 1491 buf += lladdroptlen; 1492 } 1493 1494 if (rai->rai_linkmtu) { 1495 ndopt_mtu = (struct nd_opt_mtu *)buf; 1496 ndopt_mtu->nd_opt_mtu_type = ND_OPT_MTU; 1497 ndopt_mtu->nd_opt_mtu_len = 1; 1498 ndopt_mtu->nd_opt_mtu_reserved = 0; 1499 ndopt_mtu->nd_opt_mtu_mtu = htonl(rai->rai_linkmtu); 1500 buf += sizeof(struct nd_opt_mtu); 1501 } 1502 1503 TAILQ_FOREACH(pfx, &rai->rai_prefix, pfx_next) { 1504 uint32_t vltime, pltime; 1505 struct timespec now; 1506 1507 ndopt_pi = (struct nd_opt_prefix_info *)buf; 1508 ndopt_pi->nd_opt_pi_type = ND_OPT_PREFIX_INFORMATION; 1509 ndopt_pi->nd_opt_pi_len = 4; 1510 ndopt_pi->nd_opt_pi_prefix_len = pfx->pfx_prefixlen; 1511 ndopt_pi->nd_opt_pi_flags_reserved = 0; 1512 if (pfx->pfx_onlinkflg) 1513 ndopt_pi->nd_opt_pi_flags_reserved |= 1514 ND_OPT_PI_FLAG_ONLINK; 1515 if (pfx->pfx_autoconfflg) 1516 ndopt_pi->nd_opt_pi_flags_reserved |= 1517 ND_OPT_PI_FLAG_AUTO; 1518 if (pfx->pfx_timer) 1519 vltime = 0; 1520 else { 1521 if (pfx->pfx_vltimeexpire || pfx->pfx_pltimeexpire) 1522 clock_gettime(CLOCK_MONOTONIC_FAST, &now); 1523 if (pfx->pfx_vltimeexpire == 0) 1524 vltime = pfx->pfx_validlifetime; 1525 else 1526 vltime = ((time_t)pfx->pfx_vltimeexpire > now.tv_sec) ? 1527 pfx->pfx_vltimeexpire - now.tv_sec : 0; 1528 } 1529 if (pfx->pfx_timer) 1530 pltime = 0; 1531 else { 1532 if (pfx->pfx_pltimeexpire == 0) 1533 pltime = pfx->pfx_preflifetime; 1534 else 1535 pltime = ((time_t)pfx->pfx_pltimeexpire > now.tv_sec) ? 1536 pfx->pfx_pltimeexpire - now.tv_sec : 0; 1537 } 1538 if (vltime < pltime) { 1539 /* 1540 * this can happen if vltime is decrement but pltime 1541 * is not. 1542 */ 1543 pltime = vltime; 1544 } 1545 ndopt_pi->nd_opt_pi_valid_time = htonl(vltime); 1546 ndopt_pi->nd_opt_pi_preferred_time = htonl(pltime); 1547 ndopt_pi->nd_opt_pi_reserved2 = 0; 1548 ndopt_pi->nd_opt_pi_prefix = pfx->pfx_prefix; 1549 1550 buf += sizeof(struct nd_opt_prefix_info); 1551 } 1552 1553 TAILQ_FOREACH(rti, &rai->rai_route, rti_next) { 1554 uint8_t psize = (rti->rti_prefixlen + 0x3f) >> 6; 1555 1556 ndopt_rti = (struct nd_opt_route_info *)buf; 1557 ndopt_rti->nd_opt_rti_type = ND_OPT_ROUTE_INFO; 1558 ndopt_rti->nd_opt_rti_len = 1 + psize; 1559 ndopt_rti->nd_opt_rti_prefixlen = rti->rti_prefixlen; 1560 ndopt_rti->nd_opt_rti_flags = 0xff & rti->rti_rtpref; 1561 ndopt_rti->nd_opt_rti_lifetime = htonl(rti->rti_ltime); 1562 memcpy(ndopt_rti + 1, &rti->rti_prefix, psize * 8); 1563 buf += sizeof(struct nd_opt_route_info) + psize * 8; 1564 } 1565 1566 TAILQ_FOREACH(rdn, &rai->rai_rdnss, rd_next) { 1567 struct rdnss_addr *rdna; 1568 1569 ndopt_rdnss = (struct nd_opt_rdnss *)buf; 1570 ndopt_rdnss->nd_opt_rdnss_type = ND_OPT_RDNSS; 1571 ndopt_rdnss->nd_opt_rdnss_len = 0; 1572 ndopt_rdnss->nd_opt_rdnss_reserved = 0; 1573 ndopt_rdnss->nd_opt_rdnss_lifetime = htonl(rdn->rd_ltime); 1574 buf += sizeof(struct nd_opt_rdnss); 1575 1576 TAILQ_FOREACH(rdna, &rdn->rd_list, ra_next) { 1577 memcpy(buf, &rdna->ra_dns, sizeof(rdna->ra_dns)); 1578 buf += sizeof(rdna->ra_dns); 1579 } 1580 /* Length field should be in 8 octets */ 1581 ndopt_rdnss->nd_opt_rdnss_len = (buf - (char *)ndopt_rdnss) / 8; 1582 1583 syslog(LOG_DEBUG, "<%s>: nd_opt_dnss_len = %d", __func__, 1584 ndopt_rdnss->nd_opt_rdnss_len); 1585 } 1586 1587 TAILQ_FOREACH(dns, &rai->rai_dnssl, dn_next) { 1588 struct dnssl_addr *dnsa; 1589 1590 ndopt_dnssl = (struct nd_opt_dnssl *)buf; 1591 ndopt_dnssl->nd_opt_dnssl_type = ND_OPT_DNSSL; 1592 ndopt_dnssl->nd_opt_dnssl_len = 0; 1593 ndopt_dnssl->nd_opt_dnssl_reserved = 0; 1594 ndopt_dnssl->nd_opt_dnssl_lifetime = htonl(dns->dn_ltime); 1595 buf += sizeof(*ndopt_dnssl); 1596 1597 TAILQ_FOREACH(dnsa, &dns->dn_list, da_next) { 1598 memcpy(buf, dnsa->da_dom, dnsa->da_len); 1599 buf += dnsa->da_len; 1600 } 1601 1602 /* A zero octet after encoded DNS server list. */ 1603 *buf++ = '\0'; 1604 1605 /* Padding to next 8 octets boundary */ 1606 len = buf - (char *)ndopt_dnssl; 1607 len += (len % 8) ? 8 - len % 8 : 0; 1608 buf = (char *)ndopt_dnssl + len; 1609 1610 /* Length field must be in 8 octets */ 1611 ndopt_dnssl->nd_opt_dnssl_len = len / 8; 1612 1613 syslog(LOG_DEBUG, "<%s>: nd_opt_dnssl_len = %d", __func__, 1614 ndopt_dnssl->nd_opt_dnssl_len); 1615 } 1616 1617 TAILQ_FOREACH(prf64, &rai->rai_pref64, p64_next) { 1618 ndopt_pref64 = (struct nd_opt_pref64 *)buf; 1619 ndopt_pref64->nd_opt_pref64_type = ND_OPT_PREF64; 1620 ndopt_pref64->nd_opt_pref64_len = 2; 1621 ndopt_pref64->nd_opt_pref64_sl_plc = 1622 (htons(prf64->p64_sl << 3)) | 1623 htons((prf64->p64_plc & 0x7)); 1624 memcpy(&ndopt_pref64->nd_opt_prefix[0], 1625 &prf64->p64_prefix, 1626 sizeof(ndopt_pref64->nd_opt_prefix)); 1627 buf += sizeof(struct nd_opt_pref64); 1628 } 1629 } 1630