1 1.47 rillig /* $NetBSD: config.c,v 1.47 2021/11/27 22:30:26 rillig Exp $ */ 2 1.22 rpaulo /* $KAME: config.c,v 1.93 2005/10/17 14:40:02 suz Exp $ */ 3 1.3 itojun 4 1.1 itojun /* 5 1.1 itojun * Copyright (C) 1998 WIDE Project. 6 1.1 itojun * All rights reserved. 7 1.38 roy * 8 1.1 itojun * Redistribution and use in source and binary forms, with or without 9 1.1 itojun * modification, are permitted provided that the following conditions 10 1.1 itojun * are met: 11 1.1 itojun * 1. Redistributions of source code must retain the above copyright 12 1.1 itojun * notice, this list of conditions and the following disclaimer. 13 1.1 itojun * 2. Redistributions in binary form must reproduce the above copyright 14 1.1 itojun * notice, this list of conditions and the following disclaimer in the 15 1.1 itojun * documentation and/or other materials provided with the distribution. 16 1.1 itojun * 3. Neither the name of the project nor the names of its contributors 17 1.1 itojun * may be used to endorse or promote products derived from this software 18 1.1 itojun * without specific prior written permission. 19 1.38 roy * 20 1.1 itojun * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 21 1.1 itojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 1.1 itojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 1.1 itojun * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 24 1.1 itojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 1.1 itojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 1.1 itojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 1.1 itojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 1.1 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 1.1 itojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 1.1 itojun * SUCH DAMAGE. 31 1.1 itojun */ 32 1.1 itojun 33 1.1 itojun #include <sys/param.h> 34 1.1 itojun #include <sys/ioctl.h> 35 1.1 itojun #include <sys/socket.h> 36 1.1 itojun #include <sys/time.h> 37 1.8 itojun #include <sys/sysctl.h> 38 1.1 itojun 39 1.1 itojun #include <net/if.h> 40 1.1 itojun #include <net/route.h> 41 1.1 itojun #include <net/if_dl.h> 42 1.30 roy #ifdef __FreeBSD__ 43 1.30 roy #include <net/if_var.h> 44 1.30 roy #endif 45 1.1 itojun 46 1.1 itojun #include <netinet/in.h> 47 1.1 itojun #include <netinet/in_var.h> 48 1.5 itojun #include <netinet/ip6.h> 49 1.1 itojun #include <netinet6/ip6_var.h> 50 1.5 itojun #include <netinet/icmp6.h> 51 1.22 rpaulo #include <netinet6/nd6.h> 52 1.1 itojun 53 1.1 itojun #include <arpa/inet.h> 54 1.1 itojun 55 1.1 itojun #include <stdio.h> 56 1.1 itojun #include <syslog.h> 57 1.1 itojun #include <errno.h> 58 1.1 itojun #include <string.h> 59 1.1 itojun #include <stdlib.h> 60 1.1 itojun #include <search.h> 61 1.1 itojun #include <unistd.h> 62 1.8 itojun #include <ifaddrs.h> 63 1.30 roy #include <inttypes.h> 64 1.1 itojun 65 1.1 itojun #include "rtadvd.h" 66 1.1 itojun #include "advcap.h" 67 1.1 itojun #include "timer.h" 68 1.1 itojun #include "if.h" 69 1.1 itojun #include "config.h" 70 1.37 christos #include "logit.h" 71 1.35 ozaki #include "prog_ops.h" 72 1.1 itojun 73 1.30 roy #ifndef __arraycount 74 1.30 roy #define __arraycount(__x) (sizeof(__x) / sizeof(__x[0])) 75 1.30 roy #endif 76 1.30 roy 77 1.22 rpaulo static time_t prefix_timo = (60 * 120); /* 2 hours. 78 1.22 rpaulo * XXX: should be configurable. */ 79 1.26 roy static struct rtadvd_timer *prefix_timeout(void *); 80 1.26 roy static void makeentry(char *, size_t, int, const char *); 81 1.26 roy static int getinet6sysctl(int); 82 1.22 rpaulo 83 1.26 roy static size_t 84 1.26 roy encode_domain(char *dst, const char *src) 85 1.26 roy { 86 1.26 roy ssize_t len; 87 1.26 roy char *odst, *p; 88 1.26 roy 89 1.26 roy odst = dst; 90 1.26 roy while (src && (len = strlen(src)) != 0) { 91 1.26 roy p = strchr(src, '.'); 92 1.26 roy *dst++ = len = MIN(63, p == NULL ? len : p - src); 93 1.26 roy memcpy(dst, src, len); 94 1.26 roy dst += len; 95 1.26 roy if (p == NULL) 96 1.26 roy break; 97 1.26 roy src = p + 1; 98 1.26 roy } 99 1.26 roy *dst++ = '\0'; 100 1.38 roy 101 1.26 roy return dst - odst; 102 1.26 roy } 103 1.1 itojun 104 1.1 itojun void 105 1.30 roy free_rainfo(struct rainfo *rai) 106 1.30 roy { 107 1.39 roy struct soliciter *sol; 108 1.30 roy struct prefix *pfx; 109 1.30 roy struct rtinfo *rti; 110 1.30 roy struct rdnss *rdnss; 111 1.30 roy struct rdnss_addr *rdnsa; 112 1.30 roy struct dnssl *dnssl; 113 1.30 roy struct dnssl_domain *dnsd; 114 1.30 roy 115 1.30 roy rtadvd_remove_timer(&rai->timer); 116 1.40 roy rtadvd_remove_timer(&rai->timer_sol); 117 1.30 roy 118 1.39 roy while ((sol = TAILQ_FIRST(&rai->soliciter))) { 119 1.39 roy TAILQ_REMOVE(&rai->soliciter, sol, next); 120 1.39 roy free(sol); 121 1.39 roy } 122 1.39 roy 123 1.30 roy while ((pfx = TAILQ_FIRST(&rai->prefix))) { 124 1.30 roy TAILQ_REMOVE(&rai->prefix, pfx, next); 125 1.30 roy free(pfx); 126 1.30 roy } 127 1.30 roy 128 1.30 roy while ((rti = TAILQ_FIRST(&rai->route))) { 129 1.30 roy TAILQ_REMOVE(&rai->route, rti, next); 130 1.30 roy free(rti); 131 1.30 roy } 132 1.30 roy 133 1.30 roy while ((rdnss = TAILQ_FIRST(&rai->rdnss))) { 134 1.30 roy TAILQ_REMOVE(&rai->rdnss, rdnss, next); 135 1.30 roy while ((rdnsa = TAILQ_FIRST(&rdnss->list))) { 136 1.30 roy TAILQ_REMOVE(&rdnss->list, rdnsa, next); 137 1.30 roy free(rdnsa); 138 1.30 roy } 139 1.30 roy free(rdnss); 140 1.30 roy } 141 1.30 roy 142 1.30 roy while ((dnssl = TAILQ_FIRST(&rai->dnssl))) { 143 1.30 roy TAILQ_REMOVE(&rai->dnssl, dnssl, next); 144 1.30 roy while ((dnsd = TAILQ_FIRST(&dnssl->list))) { 145 1.30 roy TAILQ_REMOVE(&dnssl->list, dnsd, next); 146 1.30 roy free(dnsd); 147 1.30 roy } 148 1.30 roy free(dnssl); 149 1.30 roy } 150 1.30 roy 151 1.30 roy free(rai->sdl); 152 1.30 roy free(rai->ra_data); 153 1.30 roy free(rai); 154 1.30 roy } 155 1.30 roy 156 1.30 roy void 157 1.30 roy getconfig(const char *intface, int exithard) 158 1.1 itojun { 159 1.26 roy int stat, c, i; 160 1.1 itojun char tbuf[BUFSIZ]; 161 1.30 roy struct rainfo *tmp, *rai; 162 1.26 roy int32_t val; 163 1.14 itojun int64_t val64; 164 1.1 itojun char buf[BUFSIZ]; 165 1.1 itojun char *bp = buf; 166 1.26 roy char *addr, *flagstr, *ap; 167 1.8 itojun static int forwarding = -1; 168 1.26 roy char entbuf[256], abuf[256]; 169 1.30 roy struct rdnss *rdnss; 170 1.30 roy struct dnssl *dnssl; 171 1.1 itojun 172 1.46 christos #define MUSTHAVE(var, cap) \ 173 1.6 itojun do { \ 174 1.14 itojun int64_t t; \ 175 1.1 itojun if ((t = agetnum(cap)) < 0) { \ 176 1.46 christos logit(LOG_ERR, "%s: need %s for interface %s", \ 177 1.46 christos __func__, cap, intface); \ 178 1.30 roy goto errexit; \ 179 1.1 itojun } \ 180 1.1 itojun var = t; \ 181 1.47 rillig } while (0) 182 1.46 christos 183 1.46 christos #define MAYHAVE(var, cap, def) \ 184 1.6 itojun do { \ 185 1.1 itojun if ((var = agetnum(cap)) < 0) \ 186 1.1 itojun var = def; \ 187 1.47 rillig } while (0) 188 1.46 christos 189 1.46 christos #define ELM_MALLOC(p) \ 190 1.26 roy do { \ 191 1.27 christos p = calloc(1, sizeof(*p)); \ 192 1.26 roy if (p == NULL) { \ 193 1.46 christos logit(LOG_ERR, "%s: calloc failed: %m", \ 194 1.27 christos __func__); \ 195 1.30 roy goto errexit; \ 196 1.26 roy } \ 197 1.27 christos } while(/*CONSTCOND*/0) 198 1.26 roy 199 1.30 roy if (if_nametoindex(intface) == 0) { 200 1.46 christos logit(LOG_INFO, "%s: interface %s not found, ignoring", 201 1.30 roy __func__, intface); 202 1.30 roy return; 203 1.30 roy } 204 1.30 roy 205 1.46 christos logit(LOG_DEBUG, "%s: loading configuration for interface %s", 206 1.30 roy __func__, intface); 207 1.1 itojun 208 1.1 itojun if ((stat = agetent(tbuf, intface)) <= 0) { 209 1.1 itojun memset(tbuf, 0, sizeof(tbuf)); 210 1.37 christos logit(LOG_INFO, 211 1.46 christos "%s: %s isn't defined in the configuration file" 212 1.1 itojun " or the configuration file doesn't exist." 213 1.1 itojun " Treat it as default", 214 1.18 itojun __func__, intface); 215 1.1 itojun } 216 1.1 itojun 217 1.30 roy ELM_MALLOC(tmp); 218 1.39 roy TAILQ_INIT(&tmp->soliciter); 219 1.30 roy TAILQ_INIT(&tmp->prefix); 220 1.30 roy TAILQ_INIT(&tmp->route); 221 1.30 roy TAILQ_INIT(&tmp->rdnss); 222 1.30 roy TAILQ_INIT(&tmp->dnssl); 223 1.1 itojun 224 1.8 itojun /* check if we are allowed to forward packets (if not determined) */ 225 1.8 itojun if (forwarding < 0) { 226 1.8 itojun if ((forwarding = getinet6sysctl(IPV6CTL_FORWARDING)) < 0) 227 1.46 christos exit(EXIT_FAILURE); 228 1.8 itojun } 229 1.8 itojun 230 1.1 itojun /* get interface information */ 231 1.1 itojun if (agetflag("nolladdr")) 232 1.1 itojun tmp->advlinkopt = 0; 233 1.1 itojun else 234 1.1 itojun tmp->advlinkopt = 1; 235 1.1 itojun if (tmp->advlinkopt) { 236 1.1 itojun if ((tmp->sdl = if_nametosdl(intface)) == NULL) { 237 1.37 christos logit(LOG_ERR, 238 1.46 christos "%s: can't get information of %s", 239 1.18 itojun __func__, intface); 240 1.30 roy goto errexit; 241 1.1 itojun } 242 1.1 itojun tmp->ifindex = tmp->sdl->sdl_index; 243 1.30 roy } else { 244 1.1 itojun tmp->ifindex = if_nametoindex(intface); 245 1.30 roy if (tmp->ifindex == 0) { 246 1.37 christos logit(LOG_ERR, 247 1.46 christos "%s: can't get information of %s", 248 1.30 roy __func__, intface); 249 1.30 roy goto errexit; 250 1.30 roy } 251 1.30 roy } 252 1.30 roy tmp->ifflags = if_getflags(tmp->ifindex, 0); 253 1.20 itojun strlcpy(tmp->ifname, intface, sizeof(tmp->ifname)); 254 1.1 itojun if ((tmp->phymtu = if_getmtu(intface)) == 0) { 255 1.1 itojun tmp->phymtu = IPV6_MMTU; 256 1.37 christos logit(LOG_WARNING, 257 1.46 christos "%s: can't get interface mtu of %s. Treat as %d", 258 1.18 itojun __func__, intface, IPV6_MMTU); 259 1.1 itojun } 260 1.1 itojun 261 1.1 itojun /* 262 1.1 itojun * set router configuration variables. 263 1.1 itojun */ 264 1.24 rpaulo MAYHAVE(val, "maxinterval", DEF_MAXRTRADVINTERVAL); 265 1.2 itojun if (val < MIN_MAXINTERVAL || val > MAX_MAXINTERVAL) { 266 1.37 christos logit(LOG_ERR, 267 1.46 christos "%s: maxinterval (%d) on %s is invalid " 268 1.22 rpaulo "(must be between %u and %u)", __func__, val, 269 1.14 itojun intface, MIN_MAXINTERVAL, MAX_MAXINTERVAL); 270 1.30 roy goto errexit; 271 1.1 itojun } 272 1.26 roy tmp->maxinterval = val; 273 1.2 itojun MAYHAVE(val, "mininterval", tmp->maxinterval/3); 274 1.2 itojun if (val < MIN_MININTERVAL || val > (tmp->maxinterval * 3) / 4) { 275 1.37 christos logit(LOG_ERR, 276 1.46 christos "%s: mininterval (%d) on %s is invalid " 277 1.22 rpaulo "(must be between %u and %d)", 278 1.18 itojun __func__, val, intface, MIN_MININTERVAL, 279 1.1 itojun (tmp->maxinterval * 3) / 4); 280 1.30 roy goto errexit; 281 1.1 itojun } 282 1.26 roy tmp->mininterval = val; 283 1.1 itojun 284 1.2 itojun MAYHAVE(val, "chlim", DEF_ADVCURHOPLIMIT); 285 1.2 itojun tmp->hoplimit = val & 0xff; 286 1.1 itojun 287 1.46 christos if ((flagstr = agetstr("raflags", &bp))) { 288 1.22 rpaulo val = 0; 289 1.22 rpaulo if (strchr(flagstr, 'm')) 290 1.22 rpaulo val |= ND_RA_FLAG_MANAGED; 291 1.22 rpaulo if (strchr(flagstr, 'o')) 292 1.22 rpaulo val |= ND_RA_FLAG_OTHER; 293 1.22 rpaulo if (strchr(flagstr, 'h')) 294 1.22 rpaulo val |= ND_RA_FLAG_RTPREF_HIGH; 295 1.22 rpaulo if (strchr(flagstr, 'l')) { 296 1.22 rpaulo if ((val & ND_RA_FLAG_RTPREF_HIGH)) { 297 1.46 christos logit(LOG_ERR, "%s: the \'h\' and \'l\'" 298 1.22 rpaulo " router flags are exclusive", __func__); 299 1.30 roy goto errexit; 300 1.22 rpaulo } 301 1.22 rpaulo val |= ND_RA_FLAG_RTPREF_LOW; 302 1.22 rpaulo } 303 1.22 rpaulo } else { 304 1.22 rpaulo MAYHAVE(val, "raflags", 0); 305 1.22 rpaulo } 306 1.14 itojun tmp->managedflg = val & ND_RA_FLAG_MANAGED; 307 1.2 itojun tmp->otherflg = val & ND_RA_FLAG_OTHER; 308 1.14 itojun #ifndef ND_RA_FLAG_RTPREF_MASK 309 1.14 itojun #define ND_RA_FLAG_RTPREF_MASK 0x18 /* 00011000 */ 310 1.14 itojun #define ND_RA_FLAG_RTPREF_RSV 0x10 /* 00010000 */ 311 1.14 itojun #endif 312 1.14 itojun tmp->rtpref = val & ND_RA_FLAG_RTPREF_MASK; 313 1.14 itojun if (tmp->rtpref == ND_RA_FLAG_RTPREF_RSV) { 314 1.46 christos logit(LOG_ERR, "%s: invalid router preference (%02x) on %s", 315 1.18 itojun __func__, tmp->rtpref, intface); 316 1.30 roy goto errexit; 317 1.14 itojun } 318 1.2 itojun 319 1.36 ozaki MAYHAVE(val, "rltime", DEF_ADVROUTERLIFETIME); 320 1.2 itojun if (val && (val < tmp->maxinterval || val > MAXROUTERLIFETIME)) { 321 1.46 christos logit(LOG_ERR, "%s: router lifetime (%d) on %s is invalid " 322 1.14 itojun "(must be 0 or between %d and %d)", 323 1.18 itojun __func__, val, intface, 324 1.1 itojun tmp->maxinterval, MAXROUTERLIFETIME); 325 1.30 roy goto errexit; 326 1.1 itojun } 327 1.8 itojun /* 328 1.8 itojun * Basically, hosts MUST NOT send Router Advertisement messages at any 329 1.8 itojun * time (RFC 2461, Section 6.2.3). However, it would sometimes be 330 1.8 itojun * useful to allow hosts to advertise some parameters such as prefix 331 1.8 itojun * information and link MTU. Thus, we allow hosts to invoke rtadvd 332 1.8 itojun * only when router lifetime (on every advertising interface) is 333 1.8 itojun * explicitly set zero. (see also the above section) 334 1.8 itojun */ 335 1.8 itojun if (val && forwarding == 0) { 336 1.37 christos logit(LOG_ERR, 337 1.46 christos "%s: non zero router lifetime is specified for %s, " 338 1.14 itojun "which must not be allowed for hosts. you must " 339 1.14 itojun "change router lifetime or enable IPv6 forwarding.", 340 1.18 itojun __func__, intface); 341 1.30 roy goto errexit; 342 1.8 itojun } 343 1.2 itojun tmp->lifetime = val & 0xffff; 344 1.1 itojun 345 1.2 itojun MAYHAVE(val, "rtime", DEF_ADVREACHABLETIME); 346 1.14 itojun if (val < 0 || val > MAXREACHABLETIME) { 347 1.37 christos logit(LOG_ERR, 348 1.46 christos "%s: reachable time (%d) on %s is invalid " 349 1.14 itojun "(must be no greater than %d)", 350 1.18 itojun __func__, val, intface, MAXREACHABLETIME); 351 1.30 roy goto errexit; 352 1.1 itojun } 353 1.26 roy tmp->reachabletime = (uint32_t)val; 354 1.1 itojun 355 1.11 itojun MAYHAVE(val64, "retrans", DEF_ADVRETRANSTIMER); 356 1.11 itojun if (val64 < 0 || val64 > 0xffffffff) { 357 1.46 christos logit(LOG_ERR, "%s: retrans time (%lld) on %s out of range", 358 1.18 itojun __func__, (long long)val64, intface); 359 1.30 roy goto errexit; 360 1.2 itojun } 361 1.26 roy tmp->retranstimer = (uint32_t)val64; 362 1.1 itojun 363 1.14 itojun if (agetnum("hapref") != -1 || agetnum("hatime") != -1) { 364 1.37 christos logit(LOG_ERR, 365 1.46 christos "%s: mobile-ip6 configuration not supported", 366 1.18 itojun __func__); 367 1.30 roy goto errexit; 368 1.11 itojun } 369 1.1 itojun /* prefix information */ 370 1.8 itojun 371 1.8 itojun /* 372 1.21 itojun * This is an implementation specific parameter to consider 373 1.8 itojun * link propagation delays and poorly synchronized clocks when 374 1.8 itojun * checking consistency of advertised lifetimes. 375 1.8 itojun */ 376 1.8 itojun MAYHAVE(val, "clockskew", 0); 377 1.8 itojun tmp->clockskew = val; 378 1.8 itojun 379 1.28 msaitoh tmp->pfxs = 0; 380 1.21 itojun for (i = -1; i < MAXPREFIX; i++) { 381 1.21 itojun struct prefix *pfx; 382 1.21 itojun 383 1.21 itojun makeentry(entbuf, sizeof(entbuf), i, "addr"); 384 1.21 itojun addr = (char *)agetstr(entbuf, &bp); 385 1.21 itojun if (addr == NULL) 386 1.21 itojun continue; 387 1.21 itojun 388 1.21 itojun /* allocate memory to store prefix information */ 389 1.27 christos if ((pfx = calloc(1, sizeof(*pfx))) == NULL) { 390 1.37 christos logit(LOG_ERR, 391 1.46 christos "%s: can't allocate memory: %m", 392 1.21 itojun __func__); 393 1.30 roy goto errexit; 394 1.21 itojun } 395 1.21 itojun 396 1.26 roy TAILQ_INSERT_TAIL(&tmp->prefix, pfx, next); 397 1.26 roy tmp->pfxs++; 398 1.22 rpaulo pfx->rainfo = tmp; 399 1.21 itojun 400 1.21 itojun pfx->origin = PREFIX_FROM_CONFIG; 401 1.21 itojun 402 1.21 itojun if (inet_pton(AF_INET6, addr, &pfx->prefix) != 1) { 403 1.37 christos logit(LOG_ERR, 404 1.46 christos "%s: inet_pton failed for %s", 405 1.21 itojun __func__, addr); 406 1.30 roy goto errexit; 407 1.21 itojun } 408 1.21 itojun if (IN6_IS_ADDR_MULTICAST(&pfx->prefix)) { 409 1.37 christos logit(LOG_ERR, 410 1.46 christos "%s: multicast prefix (%s) must " 411 1.21 itojun "not be advertised on %s", 412 1.21 itojun __func__, addr, intface); 413 1.30 roy goto errexit; 414 1.21 itojun } 415 1.21 itojun if (IN6_IS_ADDR_LINKLOCAL(&pfx->prefix)) 416 1.37 christos logit(LOG_NOTICE, 417 1.46 christos "%s: link-local prefix (%s) will be" 418 1.21 itojun " advertised on %s", 419 1.21 itojun __func__, addr, intface); 420 1.21 itojun 421 1.21 itojun makeentry(entbuf, sizeof(entbuf), i, "prefixlen"); 422 1.21 itojun MAYHAVE(val, entbuf, 64); 423 1.21 itojun if (val < 0 || val > 128) { 424 1.46 christos logit(LOG_ERR, "%s: prefixlen (%d) for %s " 425 1.21 itojun "on %s out of range", 426 1.21 itojun __func__, val, addr, intface); 427 1.30 roy goto errexit; 428 1.21 itojun } 429 1.21 itojun pfx->prefixlen = (int)val; 430 1.21 itojun 431 1.21 itojun makeentry(entbuf, sizeof(entbuf), i, "pinfoflags"); 432 1.22 rpaulo if ((flagstr = (char *)agetstr(entbuf, &bp))) { 433 1.22 rpaulo val = 0; 434 1.22 rpaulo if (strchr(flagstr, 'l')) 435 1.22 rpaulo val |= ND_OPT_PI_FLAG_ONLINK; 436 1.22 rpaulo if (strchr(flagstr, 'a')) 437 1.22 rpaulo val |= ND_OPT_PI_FLAG_AUTO; 438 1.22 rpaulo } else { 439 1.22 rpaulo MAYHAVE(val, entbuf, 440 1.22 rpaulo (ND_OPT_PI_FLAG_ONLINK|ND_OPT_PI_FLAG_AUTO)); 441 1.22 rpaulo } 442 1.21 itojun pfx->onlinkflg = val & ND_OPT_PI_FLAG_ONLINK; 443 1.21 itojun pfx->autoconfflg = val & ND_OPT_PI_FLAG_AUTO; 444 1.21 itojun 445 1.21 itojun makeentry(entbuf, sizeof(entbuf), i, "vltime"); 446 1.21 itojun MAYHAVE(val64, entbuf, DEF_ADVVALIDLIFETIME); 447 1.21 itojun if (val64 < 0 || val64 > 0xffffffff) { 448 1.46 christos logit(LOG_ERR, "%s: vltime (%lld) for " 449 1.21 itojun "%s/%d on %s is out of range", 450 1.21 itojun __func__, (long long)val64, 451 1.21 itojun addr, pfx->prefixlen, intface); 452 1.30 roy goto errexit; 453 1.21 itojun } 454 1.26 roy pfx->validlifetime = (uint32_t)val64; 455 1.21 itojun 456 1.21 itojun makeentry(entbuf, sizeof(entbuf), i, "vltimedecr"); 457 1.21 itojun if (agetflag(entbuf)) { 458 1.34 roy struct timespec now; 459 1.35 ozaki prog_clock_gettime(CLOCK_MONOTONIC, &now); 460 1.21 itojun pfx->vltimeexpire = 461 1.21 itojun now.tv_sec + pfx->validlifetime; 462 1.21 itojun } 463 1.21 itojun 464 1.21 itojun makeentry(entbuf, sizeof(entbuf), i, "pltime"); 465 1.21 itojun MAYHAVE(val64, entbuf, DEF_ADVPREFERREDLIFETIME); 466 1.21 itojun if (val64 < 0 || val64 > 0xffffffff) { 467 1.37 christos logit(LOG_ERR, 468 1.46 christos "%s: pltime (%lld) for %s/%d on %s " 469 1.21 itojun "is out of range", 470 1.21 itojun __func__, (long long)val64, 471 1.21 itojun addr, pfx->prefixlen, intface); 472 1.30 roy goto errexit; 473 1.1 itojun } 474 1.26 roy pfx->preflifetime = (uint32_t)val64; 475 1.21 itojun 476 1.21 itojun makeentry(entbuf, sizeof(entbuf), i, "pltimedecr"); 477 1.21 itojun if (agetflag(entbuf)) { 478 1.34 roy struct timespec now; 479 1.35 ozaki prog_clock_gettime(CLOCK_MONOTONIC, &now); 480 1.21 itojun pfx->pltimeexpire = 481 1.21 itojun now.tv_sec + pfx->preflifetime; 482 1.1 itojun } 483 1.1 itojun } 484 1.29 roy if (TAILQ_FIRST(&tmp->prefix) == NULL && !agetflag("noifprefix")) 485 1.21 itojun get_prefix(tmp); 486 1.1 itojun 487 1.26 roy MAYHAVE(val64, "mtu", 0); 488 1.26 roy if (val64 < 0 || val64 > 0xffffffff) { 489 1.37 christos logit(LOG_ERR, 490 1.46 christos "%s: mtu (%" PRIi64 ") on %s out of range", 491 1.26 roy __func__, val64, intface); 492 1.30 roy goto errexit; 493 1.2 itojun } 494 1.26 roy tmp->linkmtu = (uint32_t)val64; 495 1.1 itojun if (tmp->linkmtu == 0) { 496 1.1 itojun char *mtustr; 497 1.1 itojun 498 1.1 itojun if ((mtustr = (char *)agetstr("mtu", &bp)) && 499 1.1 itojun strcmp(mtustr, "auto") == 0) 500 1.1 itojun tmp->linkmtu = tmp->phymtu; 501 1.1 itojun } 502 1.1 itojun else if (tmp->linkmtu < IPV6_MMTU || tmp->linkmtu > tmp->phymtu) { 503 1.37 christos logit(LOG_ERR, 504 1.46 christos "%s: advertised link mtu (%d) on %s is invalid (must " 505 1.14 itojun "be between least MTU (%d) and physical link MTU (%d)", 506 1.26 roy __func__, tmp->linkmtu, intface, 507 1.14 itojun IPV6_MMTU, tmp->phymtu); 508 1.30 roy goto errexit; 509 1.1 itojun } 510 1.1 itojun 511 1.14 itojun /* route information */ 512 1.22 rpaulo for (i = -1; i < MAXROUTE; i++) { 513 1.22 rpaulo struct rtinfo *rti; 514 1.26 roy char oentbuf[256]; 515 1.22 rpaulo 516 1.22 rpaulo makeentry(entbuf, sizeof(entbuf), i, "rtprefix"); 517 1.22 rpaulo addr = (char *)agetstr(entbuf, &bp); 518 1.22 rpaulo if (addr == NULL) { 519 1.22 rpaulo makeentry(oentbuf, sizeof(oentbuf), i, "rtrprefix"); 520 1.22 rpaulo addr = (char *)agetstr(oentbuf, &bp); 521 1.22 rpaulo if (addr) { 522 1.22 rpaulo fprintf(stderr, "%s was obsoleted. Use %s.\n", 523 1.22 rpaulo oentbuf, entbuf); 524 1.22 rpaulo } 525 1.22 rpaulo } 526 1.22 rpaulo if (addr == NULL) 527 1.22 rpaulo continue; 528 1.22 rpaulo 529 1.30 roy ELM_MALLOC(rti); 530 1.22 rpaulo memset(rti, 0, sizeof(*rti)); 531 1.22 rpaulo 532 1.22 rpaulo /* link into chain */ 533 1.26 roy TAILQ_INSERT_TAIL(&tmp->route, rti, next); 534 1.22 rpaulo 535 1.22 rpaulo if (inet_pton(AF_INET6, addr, &rti->prefix) != 1) { 536 1.46 christos logit(LOG_ERR, "%s: inet_pton failed for %s", 537 1.22 rpaulo __func__, addr); 538 1.30 roy goto errexit; 539 1.22 rpaulo } 540 1.22 rpaulo #if 0 541 1.22 rpaulo /* 542 1.22 rpaulo * XXX: currently there's no restriction in route information 543 1.22 rpaulo * prefix according to 544 1.22 rpaulo * draft-ietf-ipngwg-router-selection-00.txt. 545 1.22 rpaulo * However, I think the similar restriction be necessary. 546 1.22 rpaulo */ 547 1.22 rpaulo MAYHAVE(val64, entbuf, DEF_ADVVALIDLIFETIME); 548 1.22 rpaulo if (IN6_IS_ADDR_MULTICAST(&rti->prefix)) { 549 1.37 christos logit(LOG_ERR, 550 1.46 christos "%s: multicast route (%s) must " 551 1.22 rpaulo "not be advertised on %s", 552 1.22 rpaulo __func__, addr, intface); 553 1.30 roy goto errexit; 554 1.22 rpaulo } 555 1.22 rpaulo if (IN6_IS_ADDR_LINKLOCAL(&rti->prefix)) { 556 1.37 christos logit(LOG_NOTICE, 557 1.46 christos "%s: link-local route (%s) will " 558 1.22 rpaulo "be advertised on %s", 559 1.22 rpaulo __func__, addr, intface); 560 1.30 roy goto errexit; 561 1.22 rpaulo } 562 1.22 rpaulo #endif 563 1.22 rpaulo 564 1.22 rpaulo makeentry(entbuf, sizeof(entbuf), i, "rtplen"); 565 1.22 rpaulo /* XXX: 256 is a magic number for compatibility check. */ 566 1.22 rpaulo MAYHAVE(val, entbuf, 256); 567 1.22 rpaulo if (val == 256) { 568 1.22 rpaulo makeentry(oentbuf, sizeof(oentbuf), i, "rtrplen"); 569 1.22 rpaulo MAYHAVE(val, oentbuf, 256); 570 1.22 rpaulo if (val != 256) { 571 1.22 rpaulo fprintf(stderr, "%s was obsoleted. Use %s.\n", 572 1.22 rpaulo oentbuf, entbuf); 573 1.22 rpaulo } else 574 1.22 rpaulo val = 64; 575 1.22 rpaulo } 576 1.22 rpaulo if (val < 0 || val > 128) { 577 1.46 christos logit(LOG_ERR, "%s: prefixlen (%d) for %s on %s " 578 1.22 rpaulo "out of range", 579 1.22 rpaulo __func__, val, addr, intface); 580 1.30 roy goto errexit; 581 1.22 rpaulo } 582 1.22 rpaulo rti->prefixlen = (int)val; 583 1.22 rpaulo 584 1.22 rpaulo makeentry(entbuf, sizeof(entbuf), i, "rtflags"); 585 1.22 rpaulo if ((flagstr = (char *)agetstr(entbuf, &bp))) { 586 1.22 rpaulo val = 0; 587 1.22 rpaulo if (strchr(flagstr, 'h')) 588 1.22 rpaulo val |= ND_RA_FLAG_RTPREF_HIGH; 589 1.22 rpaulo if (strchr(flagstr, 'l')) { 590 1.22 rpaulo if ((val & ND_RA_FLAG_RTPREF_HIGH)) { 591 1.37 christos logit(LOG_ERR, 592 1.46 christos "%s: the \'h\' and \'l\' route" 593 1.22 rpaulo " preferences are exclusive", 594 1.22 rpaulo __func__); 595 1.30 roy goto errexit; 596 1.22 rpaulo } 597 1.22 rpaulo val |= ND_RA_FLAG_RTPREF_LOW; 598 1.22 rpaulo } 599 1.22 rpaulo } else 600 1.22 rpaulo MAYHAVE(val, entbuf, 256); /* XXX */ 601 1.22 rpaulo if (val == 256) { 602 1.22 rpaulo makeentry(oentbuf, sizeof(oentbuf), i, "rtrflags"); 603 1.22 rpaulo MAYHAVE(val, oentbuf, 256); 604 1.22 rpaulo if (val != 256) { 605 1.22 rpaulo fprintf(stderr, "%s was obsoleted. Use %s.\n", 606 1.22 rpaulo oentbuf, entbuf); 607 1.22 rpaulo } else 608 1.22 rpaulo val = 0; 609 1.22 rpaulo } 610 1.22 rpaulo rti->rtpref = val & ND_RA_FLAG_RTPREF_MASK; 611 1.22 rpaulo if (rti->rtpref == ND_RA_FLAG_RTPREF_RSV) { 612 1.46 christos logit(LOG_ERR, "%s: invalid route preference (%02x) " 613 1.22 rpaulo "for %s/%d on %s", 614 1.22 rpaulo __func__, rti->rtpref, addr, 615 1.22 rpaulo rti->prefixlen, intface); 616 1.30 roy goto errexit; 617 1.22 rpaulo } 618 1.22 rpaulo 619 1.22 rpaulo /* 620 1.22 rpaulo * Since the spec does not a default value, we should make 621 1.22 rpaulo * this entry mandatory. However, FreeBSD 4.4 has shipped 622 1.22 rpaulo * with this field being optional, we use the router lifetime 623 1.22 rpaulo * as an ad-hoc default value with a warning message. 624 1.22 rpaulo */ 625 1.22 rpaulo makeentry(entbuf, sizeof(entbuf), i, "rtltime"); 626 1.22 rpaulo MAYHAVE(val64, entbuf, -1); 627 1.22 rpaulo if (val64 == -1) { 628 1.22 rpaulo makeentry(oentbuf, sizeof(oentbuf), i, "rtrltime"); 629 1.22 rpaulo MAYHAVE(val64, oentbuf, -1); 630 1.22 rpaulo if (val64 != -1) { 631 1.22 rpaulo fprintf(stderr, "%s was obsoleted. Use %s.\n", 632 1.22 rpaulo oentbuf, entbuf); 633 1.22 rpaulo } else { 634 1.22 rpaulo fprintf(stderr, "%s should be specified " 635 1.22 rpaulo "for interface %s.\n", 636 1.22 rpaulo entbuf, intface); 637 1.22 rpaulo val64 = tmp->lifetime; 638 1.22 rpaulo } 639 1.22 rpaulo } 640 1.22 rpaulo if (val64 < 0 || val64 > 0xffffffff) { 641 1.46 christos logit(LOG_ERR, "%s: route lifetime (%lld) for " 642 1.22 rpaulo "%s/%d on %s out of range", __func__, 643 1.22 rpaulo (long long)val64, addr, rti->prefixlen, intface); 644 1.30 roy goto errexit; 645 1.22 rpaulo } 646 1.26 roy rti->ltime = (uint32_t)val64; 647 1.26 roy } 648 1.26 roy 649 1.26 roy /* RDNSS */ 650 1.26 roy for (i = -1; i < MAXRDNSS; i++) { 651 1.26 roy struct rdnss_addr *rdnsa; 652 1.26 roy 653 1.26 roy makeentry(entbuf, sizeof(entbuf), i, "rdnss"); 654 1.26 roy addr = (char *)agetstr(entbuf, &bp); 655 1.26 roy if (addr == NULL) 656 1.26 roy continue; 657 1.26 roy 658 1.30 roy ELM_MALLOC(rdnss); 659 1.30 roy TAILQ_INSERT_TAIL(&tmp->rdnss, rdnss, next); 660 1.26 roy TAILQ_INIT(&rdnss->list); 661 1.26 roy 662 1.26 roy for (ap = addr; ap - addr < (ssize_t)strlen(addr); ap += c+1) { 663 1.26 roy c = strcspn(ap, ","); 664 1.26 roy strncpy(abuf, ap, c); 665 1.26 roy abuf[c] = '\0'; 666 1.30 roy ELM_MALLOC(rdnsa); 667 1.30 roy TAILQ_INSERT_TAIL(&rdnss->list, rdnsa, next); 668 1.26 roy if (inet_pton(AF_INET6, abuf, &rdnsa->addr) != 1) { 669 1.46 christos logit(LOG_ERR, "%s: inet_pton failed for %s", 670 1.26 roy __func__, addr); 671 1.30 roy goto errexit; 672 1.26 roy } 673 1.26 roy } 674 1.26 roy 675 1.26 roy makeentry(entbuf, sizeof(entbuf), i, "rdnssltime"); 676 1.26 roy MAYHAVE(val64, entbuf, tmp->maxinterval * 3 / 2); 677 1.41 roy if (val64 < 0 || val64 > 0xffffffff) { 678 1.46 christos logit(LOG_ERR, "%s: %s (%lld) on %s is invalid", 679 1.38 roy __func__, entbuf, (long long)val64, intface); 680 1.30 roy goto errexit; 681 1.26 roy } 682 1.26 roy rdnss->lifetime = (uint32_t)val64; 683 1.26 roy 684 1.26 roy } 685 1.26 roy 686 1.26 roy /* DNSSL */ 687 1.26 roy TAILQ_INIT(&tmp->dnssl); 688 1.26 roy for (i = -1; i < MAXDNSSL; i++) { 689 1.26 roy struct dnssl_domain *dnsd; 690 1.26 roy 691 1.26 roy makeentry(entbuf, sizeof(entbuf), i, "dnssl"); 692 1.26 roy addr = (char *)agetstr(entbuf, &bp); 693 1.26 roy if (addr == NULL) 694 1.26 roy continue; 695 1.26 roy 696 1.30 roy ELM_MALLOC(dnssl); 697 1.30 roy TAILQ_INSERT_TAIL(&tmp->dnssl, dnssl, next); 698 1.26 roy TAILQ_INIT(&dnssl->list); 699 1.26 roy 700 1.26 roy for (ap = addr; ap - addr < (ssize_t)strlen(addr); ap += c+1) { 701 1.26 roy c = strcspn(ap, ","); 702 1.26 roy strncpy(abuf, ap, c); 703 1.26 roy abuf[c] = '\0'; 704 1.30 roy ELM_MALLOC(dnsd); 705 1.30 roy TAILQ_INSERT_TAIL(&dnssl->list, dnsd, next); 706 1.26 roy dnsd->len = encode_domain(dnsd->domain, abuf); 707 1.26 roy } 708 1.26 roy 709 1.26 roy makeentry(entbuf, sizeof(entbuf), i, "dnsslltime"); 710 1.26 roy MAYHAVE(val64, entbuf, tmp->maxinterval * 3 / 2); 711 1.41 roy if (val64 < 0 || val64 > 0xffffffff) { 712 1.46 christos logit(LOG_ERR, "%s: %s (%lld) on %s is invalid", 713 1.38 roy __func__, entbuf, (long long)val64, intface); 714 1.30 roy goto errexit; 715 1.26 roy } 716 1.26 roy dnssl->lifetime = (uint32_t)val64; 717 1.26 roy 718 1.30 roy } 719 1.30 roy 720 1.30 roy TAILQ_FOREACH(rai, &ralist, next) { 721 1.30 roy if (rai->ifindex == tmp->ifindex) { 722 1.30 roy TAILQ_REMOVE(&ralist, rai, next); 723 1.43 roy if (Cflag) { 724 1.43 roy free_rainfo(rai); 725 1.43 roy rai = NULL; 726 1.43 roy break; 727 1.43 roy } 728 1.30 roy /* If we already have a leaving RA use that 729 1.30 roy * as this config hasn't been advertised */ 730 1.30 roy if (rai->leaving) { 731 1.30 roy tmp->leaving = rai->leaving; 732 1.30 roy free_rainfo(rai); 733 1.30 roy rai = tmp->leaving; 734 1.30 roy rai->leaving_for = tmp; 735 1.30 roy break; 736 1.30 roy } 737 1.30 roy rai->lifetime = 0; 738 1.30 roy TAILQ_FOREACH(rdnss, &rai->rdnss, next) 739 1.30 roy rdnss->lifetime = 0; 740 1.30 roy TAILQ_FOREACH(dnssl, &rai->dnssl, next) 741 1.30 roy dnssl->lifetime = 0; 742 1.30 roy rai->leaving_for = tmp; 743 1.30 roy tmp->leaving = rai; 744 1.31 roy rai->initcounter = MAX_INITIAL_RTR_ADVERTISEMENTS; 745 1.30 roy rai->mininterval = MIN_DELAY_BETWEEN_RAS; 746 1.30 roy rai->maxinterval = MIN_DELAY_BETWEEN_RAS; 747 1.30 roy rai->leaving_adv = MAX_FINAL_RTR_ADVERTISEMENTS; 748 1.30 roy if (rai->timer == NULL) 749 1.30 roy rai->timer = rtadvd_add_timer(ra_timeout, 750 1.30 roy ra_timer_update, 751 1.30 roy rai, rai); 752 1.30 roy ra_timer_update((void *)rai, &rai->timer->tm); 753 1.30 roy rtadvd_set_timer(&rai->timer->tm, rai->timer); 754 1.30 roy break; 755 1.30 roy } 756 1.22 rpaulo } 757 1.14 itojun 758 1.1 itojun /* okey */ 759 1.26 roy TAILQ_INSERT_TAIL(&ralist, tmp, next); 760 1.1 itojun 761 1.1 itojun /* construct the sending packet */ 762 1.1 itojun make_packet(tmp); 763 1.1 itojun 764 1.1 itojun /* set timer */ 765 1.30 roy if (rai) 766 1.30 roy return; 767 1.1 itojun tmp->timer = rtadvd_add_timer(ra_timeout, ra_timer_update, 768 1.1 itojun tmp, tmp); 769 1.40 roy ra_timer_set_short_delay(tmp, tmp->timer); 770 1.40 roy tmp->timer_sol = rtadvd_add_timer(ra_timeout_sol, NULL, tmp, NULL); 771 1.30 roy 772 1.30 roy return; 773 1.30 roy 774 1.30 roy errexit: 775 1.30 roy if (exithard) 776 1.46 christos exit(EXIT_FAILURE); 777 1.30 roy free_rainfo(tmp); 778 1.1 itojun } 779 1.1 itojun 780 1.21 itojun void 781 1.1 itojun get_prefix(struct rainfo *rai) 782 1.1 itojun { 783 1.8 itojun struct ifaddrs *ifap, *ifa; 784 1.8 itojun struct prefix *pp; 785 1.8 itojun struct in6_addr *a; 786 1.26 roy unsigned char *p, *ep, *m, *lim; 787 1.25 mrg char ntopbuf[INET6_ADDRSTRLEN]; 788 1.8 itojun 789 1.8 itojun if (getifaddrs(&ifap) < 0) { 790 1.37 christos logit(LOG_ERR, 791 1.46 christos "%s: can't get interface addresses", 792 1.18 itojun __func__); 793 1.46 christos exit(EXIT_FAILURE); 794 1.8 itojun } 795 1.21 itojun 796 1.8 itojun for (ifa = ifap; ifa; ifa = ifa->ifa_next) { 797 1.14 itojun int plen; 798 1.14 itojun 799 1.8 itojun if (strcmp(ifa->ifa_name, rai->ifname) != 0) 800 1.8 itojun continue; 801 1.8 itojun if (ifa->ifa_addr->sa_family != AF_INET6) 802 1.8 itojun continue; 803 1.8 itojun a = &((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr; 804 1.8 itojun if (IN6_IS_ADDR_LINKLOCAL(a)) 805 1.8 itojun continue; 806 1.14 itojun /* get prefix length */ 807 1.26 roy m = (unsigned char *)&((struct sockaddr_in6 *)ifa->ifa_netmask)->sin6_addr; 808 1.26 roy lim = (unsigned char *)(ifa->ifa_netmask) + ifa->ifa_netmask->sa_len; 809 1.14 itojun plen = prefixlen(m, lim); 810 1.14 itojun if (plen <= 0 || plen > 128) { 811 1.46 christos logit(LOG_ERR, "%s: failed to get prefixlen " 812 1.14 itojun "or prefix is invalid", 813 1.18 itojun __func__); 814 1.46 christos exit(EXIT_FAILURE); 815 1.14 itojun } 816 1.14 itojun if (plen == 128) /* XXX */ 817 1.14 itojun continue; 818 1.14 itojun if (find_prefix(rai, a, plen)) { 819 1.14 itojun /* ignore a duplicated prefix. */ 820 1.14 itojun continue; 821 1.14 itojun } 822 1.8 itojun 823 1.8 itojun /* allocate memory to store prefix info. */ 824 1.27 christos if ((pp = calloc(1, sizeof(*pp))) == NULL) { 825 1.37 christos logit(LOG_ERR, 826 1.46 christos "%s: can't get allocate buffer for prefix", 827 1.18 itojun __func__); 828 1.46 christos exit(EXIT_FAILURE); 829 1.8 itojun } 830 1.8 itojun 831 1.8 itojun /* set prefix, sweep bits outside of prefixlen */ 832 1.14 itojun pp->prefixlen = plen; 833 1.8 itojun memcpy(&pp->prefix, a, sizeof(*a)); 834 1.21 itojun if (1) 835 1.14 itojun { 836 1.26 roy p = (unsigned char *)&pp->prefix; 837 1.26 roy ep = (unsigned char *)(&pp->prefix + 1); 838 1.23 itojun while (m < lim && p < ep) 839 1.14 itojun *p++ &= *m++; 840 1.14 itojun while (p < ep) 841 1.14 itojun *p++ = 0x00; 842 1.14 itojun } 843 1.8 itojun if (!inet_ntop(AF_INET6, &pp->prefix, ntopbuf, 844 1.8 itojun sizeof(ntopbuf))) { 845 1.46 christos logit(LOG_ERR, "%s: inet_ntop failed", __func__); 846 1.46 christos exit(EXIT_FAILURE); 847 1.8 itojun } 848 1.37 christos logit(LOG_DEBUG, 849 1.46 christos "%s: add %s/%d to prefix list on %s", 850 1.18 itojun __func__, ntopbuf, pp->prefixlen, rai->ifname); 851 1.8 itojun 852 1.8 itojun /* set other fields with protocol defaults */ 853 1.8 itojun pp->validlifetime = DEF_ADVVALIDLIFETIME; 854 1.8 itojun pp->preflifetime = DEF_ADVPREFERREDLIFETIME; 855 1.8 itojun pp->onlinkflg = 1; 856 1.8 itojun pp->autoconfflg = 1; 857 1.8 itojun pp->origin = PREFIX_FROM_KERNEL; 858 1.22 rpaulo pp->rainfo = rai; 859 1.8 itojun 860 1.8 itojun /* link into chain */ 861 1.26 roy TAILQ_INSERT_TAIL(&rai->prefix, pp, next); 862 1.28 msaitoh rai->pfxs++; 863 1.8 itojun } 864 1.8 itojun 865 1.8 itojun freeifaddrs(ifap); 866 1.1 itojun } 867 1.1 itojun 868 1.1 itojun static void 869 1.26 roy makeentry(char *buf, size_t len, int id, const char *string) 870 1.1 itojun { 871 1.14 itojun 872 1.21 itojun if (id < 0) 873 1.21 itojun strlcpy(buf, string, len); 874 1.21 itojun else 875 1.21 itojun snprintf(buf, len, "%s%d", string, id); 876 1.1 itojun } 877 1.1 itojun 878 1.1 itojun /* 879 1.1 itojun * Delete a prefix to the list of specified interface and reconstruct 880 1.1 itojun * the outgoing packet. 881 1.7 itojun * The prefix must be in the list. 882 1.1 itojun */ 883 1.1 itojun void 884 1.22 rpaulo delete_prefix(struct prefix *prefix) 885 1.1 itojun { 886 1.25 mrg char ntopbuf[INET6_ADDRSTRLEN]; 887 1.22 rpaulo struct rainfo *rai = prefix->rainfo; 888 1.1 itojun 889 1.26 roy TAILQ_REMOVE(&rai->prefix, prefix, next); 890 1.26 roy rai->pfxs--; 891 1.46 christos logit(LOG_DEBUG, "%s: prefix %s/%d was deleted on %s", 892 1.18 itojun __func__, inet_ntop(AF_INET6, &prefix->prefix, 893 1.1 itojun ntopbuf, INET6_ADDRSTRLEN), 894 1.1 itojun prefix->prefixlen, rai->ifname); 895 1.30 roy rtadvd_remove_timer(&prefix->timer); 896 1.1 itojun free(prefix); 897 1.22 rpaulo } 898 1.22 rpaulo 899 1.22 rpaulo void 900 1.22 rpaulo invalidate_prefix(struct prefix *prefix) 901 1.22 rpaulo { 902 1.25 mrg char ntopbuf[INET6_ADDRSTRLEN]; 903 1.34 roy struct timespec timo; 904 1.22 rpaulo struct rainfo *rai = prefix->rainfo; 905 1.22 rpaulo 906 1.22 rpaulo if (prefix->timer) { /* sanity check */ 907 1.37 christos logit(LOG_ERR, 908 1.46 christos "%s: assumption failure: timer already exists", 909 1.22 rpaulo __func__); 910 1.46 christos exit(EXIT_FAILURE); 911 1.22 rpaulo } 912 1.22 rpaulo 913 1.46 christos logit(LOG_DEBUG, "%s: prefix %s/%d was invalidated on %s, " 914 1.22 rpaulo "will expire in %ld seconds", __func__, 915 1.22 rpaulo inet_ntop(AF_INET6, &prefix->prefix, ntopbuf, INET6_ADDRSTRLEN), 916 1.22 rpaulo prefix->prefixlen, rai->ifname, (long)prefix_timo); 917 1.22 rpaulo 918 1.22 rpaulo /* set the expiration timer */ 919 1.22 rpaulo prefix->timer = rtadvd_add_timer(prefix_timeout, NULL, prefix, NULL); 920 1.22 rpaulo if (prefix->timer == NULL) { 921 1.46 christos logit(LOG_ERR, "%s: failed to add a timer for a prefix. " 922 1.22 rpaulo "remove the prefix", __func__); 923 1.22 rpaulo delete_prefix(prefix); 924 1.22 rpaulo } 925 1.22 rpaulo timo.tv_sec = prefix_timo; 926 1.34 roy timo.tv_nsec = 0; 927 1.22 rpaulo rtadvd_set_timer(&timo, prefix->timer); 928 1.22 rpaulo } 929 1.22 rpaulo 930 1.22 rpaulo static struct rtadvd_timer * 931 1.22 rpaulo prefix_timeout(void *arg) 932 1.22 rpaulo { 933 1.22 rpaulo struct prefix *prefix = (struct prefix *)arg; 934 1.22 rpaulo 935 1.22 rpaulo delete_prefix(prefix); 936 1.22 rpaulo 937 1.22 rpaulo return(NULL); 938 1.22 rpaulo } 939 1.22 rpaulo 940 1.22 rpaulo void 941 1.22 rpaulo update_prefix(struct prefix * prefix) 942 1.22 rpaulo { 943 1.25 mrg char ntopbuf[INET6_ADDRSTRLEN]; 944 1.22 rpaulo struct rainfo *rai = prefix->rainfo; 945 1.22 rpaulo 946 1.22 rpaulo if (prefix->timer == NULL) { /* sanity check */ 947 1.37 christos logit(LOG_ERR, 948 1.46 christos "%s: assumption failure: timer does not exist", 949 1.22 rpaulo __func__); 950 1.46 christos exit(EXIT_FAILURE); 951 1.22 rpaulo } 952 1.22 rpaulo 953 1.46 christos logit(LOG_DEBUG, "%s: prefix %s/%d was re-enabled on %s", 954 1.22 rpaulo __func__, inet_ntop(AF_INET6, &prefix->prefix, ntopbuf, 955 1.22 rpaulo INET6_ADDRSTRLEN), prefix->prefixlen, rai->ifname); 956 1.22 rpaulo 957 1.22 rpaulo /* stop the expiration timer */ 958 1.22 rpaulo rtadvd_remove_timer(&prefix->timer); 959 1.1 itojun } 960 1.1 itojun 961 1.1 itojun /* 962 1.44 roy * Add a prefix to the list of specified interface and reconstruct 963 1.44 roy * the outgoing packet. 964 1.44 roy * The prefix must not be in the list. 965 1.44 roy * XXX: other parameters of the prefix(e.g. lifetime) should be 966 1.44 roy * able to be specified. 967 1.1 itojun */ 968 1.44 roy void 969 1.45 christos add_prefix(struct rainfo *rai, int ifindex, const struct in6_addr *addr, 970 1.45 christos int plen) 971 1.1 itojun { 972 1.44 roy struct prefix *prefix; 973 1.44 roy char ntopbuf[INET6_ADDRSTRLEN]; 974 1.1 itojun 975 1.44 roy if ((prefix = calloc(1, sizeof(*prefix))) == NULL) { 976 1.46 christos logit(LOG_ERR, "%s: memory allocation failed", 977 1.44 roy __func__); 978 1.44 roy return; /* XXX: error or exit? */ 979 1.1 itojun } 980 1.44 roy prefix->prefix = *addr; 981 1.44 roy prefix->prefixlen = plen; 982 1.44 roy prefix->validlifetime = DEF_ADVVALIDLIFETIME; 983 1.44 roy prefix->preflifetime = DEF_ADVPREFERREDLIFETIME; 984 1.44 roy prefix->onlinkflg = 1; 985 1.44 roy prefix->autoconfflg = 0; 986 1.44 roy prefix->origin = PREFIX_FROM_DYNAMIC; 987 1.1 itojun 988 1.44 roy prefix->rainfo = rai; 989 1.44 roy TAILQ_INSERT_TAIL(&rai->prefix, prefix, next); 990 1.44 roy rai->pfxs++; 991 1.1 itojun 992 1.46 christos logit(LOG_DEBUG, "%s: new prefix %s/%d was added on %s", 993 1.44 roy __func__, inet_ntop(AF_INET6, addr, ntopbuf, INET6_ADDRSTRLEN), 994 1.44 roy plen, rai->ifname); 995 1.1 itojun 996 1.44 roy /* free the previous packet */ 997 1.44 roy free(rai->ra_data); 998 1.44 roy rai->ra_data = NULL; 999 1.1 itojun 1000 1.44 roy /* reconstruct the packet */ 1001 1.44 roy make_packet(rai); 1002 1.1 itojun } 1003 1.1 itojun 1004 1.4 itojun void 1005 1.1 itojun make_packet(struct rainfo *rainfo) 1006 1.1 itojun { 1007 1.1 itojun size_t packlen, lladdroptlen = 0; 1008 1.26 roy char *buf; 1009 1.1 itojun struct nd_router_advert *ra; 1010 1.1 itojun struct nd_opt_prefix_info *ndopt_pi; 1011 1.1 itojun struct nd_opt_mtu *ndopt_mtu; 1012 1.1 itojun struct prefix *pfx; 1013 1.22 rpaulo struct nd_opt_route_info *ndopt_rti; 1014 1.22 rpaulo struct rtinfo *rti; 1015 1.26 roy struct nd_opt_rdnss *ndopt_rdnss; 1016 1.26 roy struct rdnss *rdns; 1017 1.26 roy struct rdnss_addr *rdnsa; 1018 1.26 roy struct nd_opt_dnssl *ndopt_dnssl; 1019 1.26 roy struct dnssl *dnsl; 1020 1.26 roy struct dnssl_domain *dnsd; 1021 1.26 roy size_t len, plen; 1022 1.1 itojun 1023 1.1 itojun /* calculate total length */ 1024 1.1 itojun packlen = sizeof(struct nd_router_advert); 1025 1.1 itojun if (rainfo->advlinkopt) { 1026 1.1 itojun if ((lladdroptlen = lladdropt_length(rainfo->sdl)) == 0) { 1027 1.37 christos logit(LOG_INFO, 1028 1.46 christos "%s: link-layer address option has" 1029 1.14 itojun " null length on %s. Treat as not included.", 1030 1.18 itojun __func__, rainfo->ifname); 1031 1.1 itojun rainfo->advlinkopt = 0; 1032 1.1 itojun } 1033 1.1 itojun packlen += lladdroptlen; 1034 1.1 itojun } 1035 1.26 roy if (TAILQ_FIRST(&rainfo->prefix) != NULL) 1036 1.1 itojun packlen += sizeof(struct nd_opt_prefix_info) * rainfo->pfxs; 1037 1.1 itojun if (rainfo->linkmtu) 1038 1.1 itojun packlen += sizeof(struct nd_opt_mtu); 1039 1.38 roy TAILQ_FOREACH(rti, &rainfo->route, next) 1040 1.38 roy packlen += sizeof(struct nd_opt_route_info) + 1041 1.22 rpaulo ((rti->prefixlen + 0x3f) >> 6) * 8; 1042 1.26 roy 1043 1.26 roy TAILQ_FOREACH(rdns, &rainfo->rdnss, next) { 1044 1.26 roy packlen += sizeof(struct nd_opt_rdnss); 1045 1.26 roy TAILQ_FOREACH(rdnsa, &rdns->list, next) 1046 1.26 roy packlen += sizeof(rdnsa->addr); 1047 1.26 roy } 1048 1.26 roy TAILQ_FOREACH(dnsl, &rainfo->dnssl, next) { 1049 1.26 roy packlen += sizeof(struct nd_opt_dnssl); 1050 1.26 roy len = 0; 1051 1.26 roy TAILQ_FOREACH(dnsd, &dnsl->list, next) 1052 1.26 roy len += dnsd->len; 1053 1.26 roy len += len % 8 ? 8 - len % 8 : 0; 1054 1.26 roy packlen += len; 1055 1.26 roy } 1056 1.1 itojun 1057 1.1 itojun /* allocate memory for the packet */ 1058 1.27 christos if ((buf = realloc(rainfo->ra_data, packlen)) == NULL) { 1059 1.37 christos logit(LOG_ERR, 1060 1.46 christos "%s: can't get enough memory for an RA packet %m", 1061 1.18 itojun __func__); 1062 1.46 christos exit(EXIT_FAILURE); 1063 1.1 itojun } 1064 1.1 itojun rainfo->ra_data = buf; 1065 1.1 itojun /* XXX: what if packlen > 576? */ 1066 1.1 itojun rainfo->ra_datalen = packlen; 1067 1.27 christos #define CHECKLEN(size) \ 1068 1.27 christos do { \ 1069 1.27 christos if (buf + size > rainfo->ra_data + packlen) { \ 1070 1.37 christos logit(LOG_ERR, \ 1071 1.46 christos "%s: @%d RA packet does not fit in %zu",\ 1072 1.27 christos __func__, __LINE__, packlen); \ 1073 1.46 christos exit(EXIT_FAILURE); \ 1074 1.27 christos } \ 1075 1.47 rillig } while (0) 1076 1.1 itojun /* 1077 1.1 itojun * construct the packet 1078 1.1 itojun */ 1079 1.27 christos CHECKLEN(sizeof(*ra)); 1080 1.1 itojun ra = (struct nd_router_advert *)buf; 1081 1.1 itojun ra->nd_ra_type = ND_ROUTER_ADVERT; 1082 1.1 itojun ra->nd_ra_code = 0; 1083 1.1 itojun ra->nd_ra_cksum = 0; 1084 1.26 roy ra->nd_ra_curhoplimit = (uint8_t)(0xff & rainfo->hoplimit); 1085 1.14 itojun ra->nd_ra_flags_reserved = 0; /* just in case */ 1086 1.14 itojun /* 1087 1.14 itojun * XXX: the router preference field, which is a 2-bit field, should be 1088 1.14 itojun * initialized before other fields. 1089 1.14 itojun */ 1090 1.14 itojun ra->nd_ra_flags_reserved = 0xff & rainfo->rtpref; 1091 1.1 itojun ra->nd_ra_flags_reserved |= 1092 1.1 itojun rainfo->managedflg ? ND_RA_FLAG_MANAGED : 0; 1093 1.1 itojun ra->nd_ra_flags_reserved |= 1094 1.1 itojun rainfo->otherflg ? ND_RA_FLAG_OTHER : 0; 1095 1.1 itojun ra->nd_ra_router_lifetime = htons(rainfo->lifetime); 1096 1.1 itojun ra->nd_ra_reachable = htonl(rainfo->reachabletime); 1097 1.1 itojun ra->nd_ra_retransmit = htonl(rainfo->retranstimer); 1098 1.1 itojun buf += sizeof(*ra); 1099 1.1 itojun 1100 1.1 itojun if (rainfo->advlinkopt) { 1101 1.27 christos CHECKLEN(sizeof(struct nd_opt_hdr)); 1102 1.1 itojun lladdropt_fill(rainfo->sdl, (struct nd_opt_hdr *)buf); 1103 1.1 itojun buf += lladdroptlen; 1104 1.1 itojun } 1105 1.1 itojun 1106 1.1 itojun if (rainfo->linkmtu) { 1107 1.27 christos CHECKLEN(sizeof(*ndopt_mtu)); 1108 1.1 itojun ndopt_mtu = (struct nd_opt_mtu *)buf; 1109 1.1 itojun ndopt_mtu->nd_opt_mtu_type = ND_OPT_MTU; 1110 1.1 itojun ndopt_mtu->nd_opt_mtu_len = 1; 1111 1.1 itojun ndopt_mtu->nd_opt_mtu_reserved = 0; 1112 1.12 itojun ndopt_mtu->nd_opt_mtu_mtu = htonl(rainfo->linkmtu); 1113 1.1 itojun buf += sizeof(struct nd_opt_mtu); 1114 1.1 itojun } 1115 1.1 itojun 1116 1.38 roy TAILQ_FOREACH(pfx, &rainfo->prefix, next) { 1117 1.26 roy uint32_t vltime, pltime; 1118 1.34 roy struct timespec now; 1119 1.8 itojun 1120 1.27 christos CHECKLEN(sizeof(*ndopt_pi)); 1121 1.1 itojun ndopt_pi = (struct nd_opt_prefix_info *)buf; 1122 1.1 itojun ndopt_pi->nd_opt_pi_type = ND_OPT_PREFIX_INFORMATION; 1123 1.1 itojun ndopt_pi->nd_opt_pi_len = 4; 1124 1.1 itojun ndopt_pi->nd_opt_pi_prefix_len = pfx->prefixlen; 1125 1.1 itojun ndopt_pi->nd_opt_pi_flags_reserved = 0; 1126 1.1 itojun if (pfx->onlinkflg) 1127 1.1 itojun ndopt_pi->nd_opt_pi_flags_reserved |= 1128 1.1 itojun ND_OPT_PI_FLAG_ONLINK; 1129 1.1 itojun if (pfx->autoconfflg) 1130 1.1 itojun ndopt_pi->nd_opt_pi_flags_reserved |= 1131 1.1 itojun ND_OPT_PI_FLAG_AUTO; 1132 1.22 rpaulo if (pfx->timer) 1133 1.22 rpaulo vltime = 0; 1134 1.22 rpaulo else { 1135 1.22 rpaulo if (pfx->vltimeexpire || pfx->pltimeexpire) 1136 1.35 ozaki prog_clock_gettime(CLOCK_MONOTONIC, &now); 1137 1.22 rpaulo if (pfx->vltimeexpire == 0) 1138 1.22 rpaulo vltime = pfx->validlifetime; 1139 1.22 rpaulo else 1140 1.22 rpaulo vltime = (pfx->vltimeexpire > now.tv_sec) ? 1141 1.22 rpaulo pfx->vltimeexpire - now.tv_sec : 0; 1142 1.22 rpaulo } 1143 1.22 rpaulo if (pfx->timer) 1144 1.22 rpaulo pltime = 0; 1145 1.22 rpaulo else { 1146 1.22 rpaulo if (pfx->pltimeexpire == 0) 1147 1.22 rpaulo pltime = pfx->preflifetime; 1148 1.22 rpaulo else 1149 1.22 rpaulo pltime = (pfx->pltimeexpire > now.tv_sec) ? 1150 1.22 rpaulo pfx->pltimeexpire - now.tv_sec : 0; 1151 1.22 rpaulo } 1152 1.8 itojun if (vltime < pltime) { 1153 1.8 itojun /* 1154 1.8 itojun * this can happen if vltime is decrement but pltime 1155 1.8 itojun * is not. 1156 1.8 itojun */ 1157 1.8 itojun pltime = vltime; 1158 1.8 itojun } 1159 1.12 itojun ndopt_pi->nd_opt_pi_valid_time = htonl(vltime); 1160 1.12 itojun ndopt_pi->nd_opt_pi_preferred_time = htonl(pltime); 1161 1.1 itojun ndopt_pi->nd_opt_pi_reserved2 = 0; 1162 1.1 itojun ndopt_pi->nd_opt_pi_prefix = pfx->prefix; 1163 1.1 itojun 1164 1.1 itojun buf += sizeof(struct nd_opt_prefix_info); 1165 1.1 itojun } 1166 1.1 itojun 1167 1.26 roy TAILQ_FOREACH(rti, &rainfo->route, next) { 1168 1.26 roy uint8_t psize = (rti->prefixlen + 0x3f) >> 6; 1169 1.22 rpaulo 1170 1.27 christos CHECKLEN(sizeof(*ndopt_rti)); 1171 1.22 rpaulo ndopt_rti = (struct nd_opt_route_info *)buf; 1172 1.22 rpaulo ndopt_rti->nd_opt_rti_type = ND_OPT_ROUTE_INFO; 1173 1.22 rpaulo ndopt_rti->nd_opt_rti_len = 1 + psize; 1174 1.22 rpaulo ndopt_rti->nd_opt_rti_prefixlen = rti->prefixlen; 1175 1.22 rpaulo ndopt_rti->nd_opt_rti_flags = 0xff & rti->rtpref; 1176 1.22 rpaulo ndopt_rti->nd_opt_rti_lifetime = htonl(rti->ltime); 1177 1.22 rpaulo memcpy(ndopt_rti + 1, &rti->prefix, psize * 8); 1178 1.22 rpaulo buf += sizeof(struct nd_opt_route_info) + psize * 8; 1179 1.22 rpaulo } 1180 1.22 rpaulo 1181 1.26 roy TAILQ_FOREACH(rdns, &rainfo->rdnss, next) { 1182 1.27 christos CHECKLEN(sizeof(*ndopt_rdnss)); 1183 1.26 roy ndopt_rdnss = (struct nd_opt_rdnss *)buf; 1184 1.26 roy ndopt_rdnss->nd_opt_rdnss_type = ND_OPT_RDNSS; 1185 1.26 roy ndopt_rdnss->nd_opt_rdnss_len = 1; 1186 1.26 roy ndopt_rdnss->nd_opt_rdnss_reserved = 0; 1187 1.26 roy ndopt_rdnss->nd_opt_rdnss_lifetime = htonl(rdns->lifetime); 1188 1.26 roy buf += sizeof(*ndopt_rdnss); 1189 1.38 roy 1190 1.26 roy TAILQ_FOREACH(rdnsa, &rdns->list, next) { 1191 1.27 christos CHECKLEN(sizeof(rdnsa->addr)); 1192 1.26 roy memcpy(buf, &rdnsa->addr, sizeof(rdnsa->addr)); 1193 1.26 roy ndopt_rdnss->nd_opt_rdnss_len += 2; 1194 1.26 roy buf += sizeof(rdnsa->addr); 1195 1.26 roy } 1196 1.26 roy } 1197 1.26 roy 1198 1.26 roy TAILQ_FOREACH(dnsl, &rainfo->dnssl, next) { 1199 1.27 christos CHECKLEN(sizeof(*ndopt_dnssl)); 1200 1.26 roy ndopt_dnssl = (struct nd_opt_dnssl *)buf; 1201 1.26 roy ndopt_dnssl->nd_opt_dnssl_type = ND_OPT_DNSSL; 1202 1.26 roy ndopt_dnssl->nd_opt_dnssl_len = 0; 1203 1.26 roy ndopt_dnssl->nd_opt_dnssl_reserved = 0; 1204 1.26 roy ndopt_dnssl->nd_opt_dnssl_lifetime = htonl(dnsl->lifetime); 1205 1.26 roy buf += sizeof(*ndopt_dnssl); 1206 1.38 roy 1207 1.26 roy TAILQ_FOREACH(dnsd, &dnsl->list, next) { 1208 1.27 christos CHECKLEN(dnsd->len); 1209 1.26 roy memcpy(buf, dnsd->domain, dnsd->len); 1210 1.26 roy buf += dnsd->len; 1211 1.26 roy } 1212 1.26 roy /* Ensure our length is padded correctly */ 1213 1.26 roy len = buf - (char *)ndopt_dnssl; 1214 1.26 roy plen = len % 8 ? 8 - len % 8 : 0; 1215 1.27 christos CHECKLEN(plen); 1216 1.26 roy memset(buf, 0, plen); 1217 1.26 roy buf += plen; 1218 1.26 roy ndopt_dnssl->nd_opt_dnssl_len = (len + plen) / 8; 1219 1.26 roy } 1220 1.27 christos memset(buf, 0, packlen - (buf - rainfo->ra_data)); 1221 1.8 itojun } 1222 1.8 itojun 1223 1.8 itojun static int 1224 1.8 itojun getinet6sysctl(int code) 1225 1.8 itojun { 1226 1.33 christos const int mib[] = { CTL_NET, PF_INET6, IPPROTO_IPV6, code }; 1227 1.8 itojun int value; 1228 1.8 itojun size_t size; 1229 1.8 itojun 1230 1.8 itojun size = sizeof(value); 1231 1.46 christos if (prog_sysctl(mib, __arraycount(mib), &value, &size, NULL, 0) == -1) { 1232 1.46 christos logit(LOG_ERR, "%s: failed to get ip6 sysctl(%d): %m", 1233 1.27 christos __func__, code); 1234 1.27 christos return -1; 1235 1.8 itojun } 1236 1.46 christos return value; 1237 1.1 itojun } 1238