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