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