Home | History | Annotate | Line # | Download | only in rtadvd
config.c revision 1.18
      1 /*	$NetBSD: config.c,v 1.18 2002/07/10 21:11:43 itojun Exp $	*/
      2 /*	$KAME: config.c,v 1.62 2002/05/29 10:13:10 itojun Exp $	*/
      3 
      4 /*
      5  * Copyright (C) 1998 WIDE Project.
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. Neither the name of the project nor the names of its contributors
     17  *    may be used to endorse or promote products derived from this software
     18  *    without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30  * SUCH DAMAGE.
     31  */
     32 
     33 #include <sys/param.h>
     34 #include <sys/ioctl.h>
     35 #include <sys/socket.h>
     36 #include <sys/time.h>
     37 #include <sys/sysctl.h>
     38 
     39 #include <net/if.h>
     40 #include <net/route.h>
     41 #include <net/if_dl.h>
     42 
     43 #include <netinet/in.h>
     44 #include <netinet/in_var.h>
     45 #include <netinet/ip6.h>
     46 #include <netinet6/ip6_var.h>
     47 #include <netinet/icmp6.h>
     48 
     49 #include <arpa/inet.h>
     50 
     51 #include <stdio.h>
     52 #include <syslog.h>
     53 #include <errno.h>
     54 #include <string.h>
     55 #include <stdlib.h>
     56 #include <search.h>
     57 #include <unistd.h>
     58 #include <ifaddrs.h>
     59 
     60 #include "rtadvd.h"
     61 #include "advcap.h"
     62 #include "timer.h"
     63 #include "if.h"
     64 #include "config.h"
     65 
     66 static void makeentry __P((char *, size_t, int, char *, int));
     67 static void get_prefix __P((struct rainfo *));
     68 static int getinet6sysctl __P((int));
     69 
     70 extern struct rainfo *ralist;
     71 
     72 void
     73 getconfig(intface)
     74 	char *intface;
     75 {
     76 	int stat, pfxs, i;
     77 	char tbuf[BUFSIZ];
     78 	struct rainfo *tmp;
     79 	long val;
     80 	int64_t val64;
     81 	char buf[BUFSIZ];
     82 	char *bp = buf;
     83 	char *addr;
     84 	static int forwarding = -1;
     85 
     86 #define MUSTHAVE(var, cap)	\
     87     do {								\
     88 	int64_t t;							\
     89 	if ((t = agetnum(cap)) < 0) {					\
     90 		fprintf(stderr, "rtadvd: need %s for interface %s\n",	\
     91 			cap, intface);					\
     92 		exit(1);						\
     93 	}								\
     94 	var = t;							\
     95      } while (0)
     96 #define MAYHAVE(var, cap, def)	\
     97      do {								\
     98 	if ((var = agetnum(cap)) < 0)					\
     99 		var = def;						\
    100      } while (0)
    101 
    102 	if ((stat = agetent(tbuf, intface)) <= 0) {
    103 		memset(tbuf, 0, sizeof(tbuf));
    104 		syslog(LOG_INFO,
    105 		       "<%s> %s isn't defined in the configuration file"
    106 		       " or the configuration file doesn't exist."
    107 		       " Treat it as default",
    108 		        __func__, intface);
    109 	}
    110 
    111 	tmp = (struct rainfo *)malloc(sizeof(*ralist));
    112 	memset(tmp, 0, sizeof(*tmp));
    113 	tmp->prefix.next = tmp->prefix.prev = &tmp->prefix;
    114 
    115 	/* check if we are allowed to forward packets (if not determined) */
    116 	if (forwarding < 0) {
    117 		if ((forwarding = getinet6sysctl(IPV6CTL_FORWARDING)) < 0)
    118 			exit(1);
    119 	}
    120 
    121 	/* get interface information */
    122 	if (agetflag("nolladdr"))
    123 		tmp->advlinkopt = 0;
    124 	else
    125 		tmp->advlinkopt = 1;
    126 	if (tmp->advlinkopt) {
    127 		if ((tmp->sdl = if_nametosdl(intface)) == NULL) {
    128 			syslog(LOG_ERR,
    129 			       "<%s> can't get information of %s",
    130 			       __func__, intface);
    131 			exit(1);
    132 		}
    133 		tmp->ifindex = tmp->sdl->sdl_index;
    134 	} else
    135 		tmp->ifindex = if_nametoindex(intface);
    136 	strncpy(tmp->ifname, intface, sizeof(tmp->ifname));
    137 	if ((tmp->phymtu = if_getmtu(intface)) == 0) {
    138 		tmp->phymtu = IPV6_MMTU;
    139 		syslog(LOG_WARNING,
    140 		       "<%s> can't get interface mtu of %s. Treat as %d",
    141 		       __func__, intface, IPV6_MMTU);
    142 	}
    143 
    144 	/*
    145 	 * set router configuration variables.
    146 	 */
    147 	MAYHAVE(val, "maxinterval", DEF_MAXRTRADVINTERVAL);
    148 	if (val < MIN_MAXINTERVAL || val > MAX_MAXINTERVAL) {
    149 		syslog(LOG_ERR,
    150 		       "<%s> maxinterval (%ld) on %s is invalid "
    151 		       "(must be between %e and %u)", __func__, val,
    152 		       intface, MIN_MAXINTERVAL, MAX_MAXINTERVAL);
    153 		exit(1);
    154 	}
    155 	tmp->maxinterval = (u_int)val;
    156 	MAYHAVE(val, "mininterval", tmp->maxinterval/3);
    157 	if (val < MIN_MININTERVAL || val > (tmp->maxinterval * 3) / 4) {
    158 		syslog(LOG_ERR,
    159 		       "<%s> mininterval (%ld) on %s is invalid "
    160 		       "(must be between %e and %d)",
    161 		       __func__, val, intface, MIN_MININTERVAL,
    162 		       (tmp->maxinterval * 3) / 4);
    163 		exit(1);
    164 	}
    165 	tmp->mininterval = (u_int)val;
    166 
    167 	MAYHAVE(val, "chlim", DEF_ADVCURHOPLIMIT);
    168 	tmp->hoplimit = val & 0xff;
    169 
    170 	MAYHAVE(val, "raflags", 0);
    171 	tmp->managedflg = val & ND_RA_FLAG_MANAGED;
    172 	tmp->otherflg = val & ND_RA_FLAG_OTHER;
    173 #ifndef ND_RA_FLAG_RTPREF_MASK
    174 #define ND_RA_FLAG_RTPREF_MASK	0x18 /* 00011000 */
    175 #define ND_RA_FLAG_RTPREF_RSV	0x10 /* 00010000 */
    176 #endif
    177 	tmp->rtpref = val & ND_RA_FLAG_RTPREF_MASK;
    178 	if (tmp->rtpref == ND_RA_FLAG_RTPREF_RSV) {
    179 		syslog(LOG_ERR, "<%s> invalid router preference (%02x) on %s",
    180 		       __func__, tmp->rtpref, intface);
    181 		exit(1);
    182 	}
    183 
    184 	MAYHAVE(val, "rltime", tmp->maxinterval * 3);
    185 	if (val && (val < tmp->maxinterval || val > MAXROUTERLIFETIME)) {
    186 		syslog(LOG_ERR,
    187 		       "<%s> router lifetime (%ld) on %s is invalid "
    188 		       "(must be 0 or between %d and %d)",
    189 		       __func__, val, intface,
    190 		       tmp->maxinterval, MAXROUTERLIFETIME);
    191 		exit(1);
    192 	}
    193 	/*
    194 	 * Basically, hosts MUST NOT send Router Advertisement messages at any
    195 	 * time (RFC 2461, Section 6.2.3). However, it would sometimes be
    196 	 * useful to allow hosts to advertise some parameters such as prefix
    197 	 * information and link MTU. Thus, we allow hosts to invoke rtadvd
    198 	 * only when router lifetime (on every advertising interface) is
    199 	 * explicitly set zero. (see also the above section)
    200 	 */
    201 	if (val && forwarding == 0) {
    202 		syslog(LOG_ERR,
    203 		       "<%s> non zero router lifetime is specified for %s, "
    204 		       "which must not be allowed for hosts.  you must "
    205 		       "change router lifetime or enable IPv6 forwarding.",
    206 		       __func__, intface);
    207 		exit(1);
    208 	}
    209 	tmp->lifetime = val & 0xffff;
    210 
    211 	MAYHAVE(val, "rtime", DEF_ADVREACHABLETIME);
    212 	if (val < 0 || val > MAXREACHABLETIME) {
    213 		syslog(LOG_ERR,
    214 		       "<%s> reachable time (%ld) on %s is invalid "
    215 		       "(must be no greater than %d)",
    216 		       __func__, val, intface, MAXREACHABLETIME);
    217 		exit(1);
    218 	}
    219 	tmp->reachabletime = (u_int32_t)val;
    220 
    221 	MAYHAVE(val64, "retrans", DEF_ADVRETRANSTIMER);
    222 	if (val64 < 0 || val64 > 0xffffffff) {
    223 		syslog(LOG_ERR, "<%s> retrans time (%lld) on %s out of range",
    224 		       __func__, (long long)val64, intface);
    225 		exit(1);
    226 	}
    227 	tmp->retranstimer = (u_int32_t)val64;
    228 
    229 	if (agetnum("hapref") != -1 || agetnum("hatime") != -1) {
    230 		syslog(LOG_ERR,
    231 		       "<%s> mobile-ip6 configuration not supported",
    232 		       __func__);
    233 		exit(1);
    234 	}
    235 	/* prefix information */
    236 
    237 	/*
    238 	 * This is an implementation specific parameter to consinder
    239 	 * link propagation delays and poorly synchronized clocks when
    240 	 * checking consistency of advertised lifetimes.
    241 	 */
    242 	MAYHAVE(val, "clockskew", 0);
    243 	tmp->clockskew = val;
    244 
    245 	if ((pfxs = agetnum("addrs")) <= 0) {
    246 		/* auto configure prefix information */
    247 		if (agetstr("addr", &bp) || agetstr("addr1", &bp)) {
    248 			syslog(LOG_ERR,
    249 			       "<%s> conflicting prefix configuration for %s: "
    250 			       "automatic and manual config at the same time",
    251 			       __func__, intface);
    252 			exit(1);
    253 		}
    254 		get_prefix(tmp);
    255 	} else {
    256 		tmp->pfxs = pfxs;
    257 		for (i = 0; i < pfxs; i++) {
    258 			struct prefix *pfx;
    259 			char entbuf[256];
    260 			int added = (pfxs > 1) ? 1 : 0;
    261 
    262 			/* allocate memory to store prefix information */
    263 			if ((pfx = malloc(sizeof(struct prefix))) == NULL) {
    264 				syslog(LOG_ERR,
    265 				       "<%s> can't allocate enough memory",
    266 				       __func__);
    267 				exit(1);
    268 			}
    269 			memset(pfx, 0, sizeof(*pfx));
    270 
    271 			/* link into chain */
    272 			insque(pfx, &tmp->prefix);
    273 
    274 			pfx->origin = PREFIX_FROM_CONFIG;
    275 
    276 
    277 			makeentry(entbuf, sizeof(entbuf), i, "addr", added);
    278 			addr = (char *)agetstr(entbuf, &bp);
    279 			if (addr == NULL) {
    280 				syslog(LOG_ERR,
    281 				       "<%s> need %s as a prefix for "
    282 				       "interface %s",
    283 				       __func__, entbuf, intface);
    284 				exit(1);
    285 			}
    286 			if (inet_pton(AF_INET6, addr,
    287 				      &pfx->prefix) != 1) {
    288 				syslog(LOG_ERR,
    289 				       "<%s> inet_pton failed for %s",
    290 				       __func__, addr);
    291 				exit(1);
    292 			}
    293 			if (IN6_IS_ADDR_MULTICAST(&pfx->prefix)) {
    294 				syslog(LOG_ERR,
    295 				       "<%s> multicast prefix (%s) must "
    296 				       "not be advertised on %s",
    297 				       __func__, addr, intface);
    298 				exit(1);
    299 			}
    300 			if (IN6_IS_ADDR_LINKLOCAL(&pfx->prefix))
    301 				syslog(LOG_NOTICE,
    302 				       "<%s> link-local prefix (%s) will be"
    303 				       " advertised on %s",
    304 				       __func__, addr, intface);
    305 
    306 			makeentry(entbuf, sizeof(entbuf), i, "prefixlen",
    307 			    added);
    308 			MAYHAVE(val, entbuf, 64);
    309 			if (val < 0 || val > 128) {
    310 				syslog(LOG_ERR, "<%s> prefixlen (%ld) for %s "
    311 				       "on %s out of range",
    312 				       __func__, val, addr, intface);
    313 				exit(1);
    314 			}
    315 			pfx->prefixlen = (int)val;
    316 
    317 			makeentry(entbuf, sizeof(entbuf), i, "pinfoflags",
    318 			    added);
    319 			MAYHAVE(val, entbuf,
    320 				(ND_OPT_PI_FLAG_ONLINK|ND_OPT_PI_FLAG_AUTO));
    321 			pfx->onlinkflg = val & ND_OPT_PI_FLAG_ONLINK;
    322 			pfx->autoconfflg = val & ND_OPT_PI_FLAG_AUTO;
    323 
    324 			makeentry(entbuf, sizeof(entbuf), i, "vltime", added);
    325 			MAYHAVE(val64, entbuf, DEF_ADVVALIDLIFETIME);
    326 			if (val64 < 0 || val64 > 0xffffffff) {
    327 				syslog(LOG_ERR, "<%s> vltime (%lld) for "
    328 				    "%s/%d on %s is out of range",
    329 				    __func__, (long long)val64,
    330 				    addr, pfx->prefixlen, intface);
    331 				exit(1);
    332 			}
    333 			pfx->validlifetime = (u_int32_t)val64;
    334 
    335 			makeentry(entbuf, sizeof(entbuf), i, "vltimedecr", added);
    336 			if (agetflag(entbuf)) {
    337 				struct timeval now;
    338 				gettimeofday(&now, 0);
    339 				pfx->vltimeexpire =
    340 					now.tv_sec + pfx->validlifetime;
    341 			}
    342 
    343 			makeentry(entbuf, sizeof(entbuf), i, "pltime", added);
    344 			MAYHAVE(val64, entbuf, DEF_ADVPREFERREDLIFETIME);
    345 			if (val64 < 0 || val64 > 0xffffffff) {
    346 				syslog(LOG_ERR,
    347 				    "<%s> pltime (%lld) for %s/%d on %s "
    348 				    "is out of range",
    349 				    __func__, (long long)val64,
    350 				    addr, pfx->prefixlen, intface);
    351 				exit(1);
    352 			}
    353 			pfx->preflifetime = (u_int32_t)val64;
    354 
    355 			makeentry(entbuf, sizeof(entbuf), i, "pltimedecr", added);
    356 			if (agetflag(entbuf)) {
    357 				struct timeval now;
    358 				gettimeofday(&now, 0);
    359 				pfx->pltimeexpire =
    360 					now.tv_sec + pfx->preflifetime;
    361 			}
    362 		}
    363 	}
    364 
    365 	MAYHAVE(val, "mtu", 0);
    366 	if (val < 0 || val > 0xffffffff) {
    367 		syslog(LOG_ERR,
    368 		       "<%s> mtu (%ld) on %s out of range",
    369 		       __func__, val, intface);
    370 		exit(1);
    371 	}
    372 	tmp->linkmtu = (u_int32_t)val;
    373 	if (tmp->linkmtu == 0) {
    374 		char *mtustr;
    375 
    376 		if ((mtustr = (char *)agetstr("mtu", &bp)) &&
    377 		    strcmp(mtustr, "auto") == 0)
    378 			tmp->linkmtu = tmp->phymtu;
    379 	}
    380 	else if (tmp->linkmtu < IPV6_MMTU || tmp->linkmtu > tmp->phymtu) {
    381 		syslog(LOG_ERR,
    382 		       "<%s> advertised link mtu (%lu) on %s is invalid (must "
    383 		       "be between least MTU (%d) and physical link MTU (%d)",
    384 		       __func__, (unsigned long)tmp->linkmtu, intface,
    385 		       IPV6_MMTU, tmp->phymtu);
    386 		exit(1);
    387 	}
    388 
    389 	/* route information */
    390 	MAYHAVE(val, "routes", -1);
    391 	if (val != -1)
    392 		syslog(LOG_INFO, "route information option is not available");
    393 
    394 	/* okey */
    395 	tmp->next = ralist;
    396 	ralist = tmp;
    397 
    398 	/* construct the sending packet */
    399 	make_packet(tmp);
    400 
    401 	/* set timer */
    402 	tmp->timer = rtadvd_add_timer(ra_timeout, ra_timer_update,
    403 				      tmp, tmp);
    404 	ra_timer_update((void *)tmp, &tmp->timer->tm);
    405 	rtadvd_set_timer(&tmp->timer->tm, tmp->timer);
    406 }
    407 
    408 static void
    409 get_prefix(struct rainfo *rai)
    410 {
    411 	struct ifaddrs *ifap, *ifa;
    412 	struct prefix *pp;
    413 	struct in6_addr *a;
    414 	u_char *p, *ep, *m, *lim;
    415 	u_char ntopbuf[INET6_ADDRSTRLEN];
    416 
    417 	if (getifaddrs(&ifap) < 0) {
    418 		syslog(LOG_ERR,
    419 		       "<%s> can't get interface addresses",
    420 		       __func__);
    421 		exit(1);
    422 	}
    423 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
    424 		int plen;
    425 
    426 		if (strcmp(ifa->ifa_name, rai->ifname) != 0)
    427 			continue;
    428 		if (ifa->ifa_addr->sa_family != AF_INET6)
    429 			continue;
    430 		a = &((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr;
    431 		if (IN6_IS_ADDR_LINKLOCAL(a))
    432 			continue;
    433 		/* get prefix length */
    434 		m = (u_char *)&((struct sockaddr_in6 *)ifa->ifa_netmask)->sin6_addr;
    435 		lim = (u_char *)(ifa->ifa_netmask) + ifa->ifa_netmask->sa_len;
    436 		plen = prefixlen(m, lim);
    437 		if (plen <= 0 || plen > 128) {
    438 			syslog(LOG_ERR, "<%s> failed to get prefixlen "
    439 			       "or prefix is invalid",
    440 			       __func__);
    441 			exit(1);
    442 		}
    443 		if (plen == 128)	/* XXX */
    444 			continue;
    445 		if (find_prefix(rai, a, plen)) {
    446 			/* ignore a duplicated prefix. */
    447 			continue;
    448 		}
    449 
    450 		/* allocate memory to store prefix info. */
    451 		if ((pp = malloc(sizeof(*pp))) == NULL) {
    452 			syslog(LOG_ERR,
    453 			       "<%s> can't get allocate buffer for prefix",
    454 			       __func__);
    455 			exit(1);
    456 		}
    457 		memset(pp, 0, sizeof(*pp));
    458 
    459 		/* set prefix, sweep bits outside of prefixlen */
    460 		pp->prefixlen = plen;
    461 		memcpy(&pp->prefix, a, sizeof(*a));
    462 		{
    463 			p = (u_char *)&pp->prefix;
    464 			ep = (u_char *)(&pp->prefix + 1);
    465 			while (m < lim)
    466 				*p++ &= *m++;
    467 			while (p < ep)
    468 				*p++ = 0x00;
    469 		}
    470 	        if (!inet_ntop(AF_INET6, &pp->prefix, ntopbuf,
    471 	            sizeof(ntopbuf))) {
    472 			syslog(LOG_ERR, "<%s> inet_ntop failed", __func__);
    473 			exit(1);
    474 		}
    475 		syslog(LOG_DEBUG,
    476 		       "<%s> add %s/%d to prefix list on %s",
    477 		       __func__, ntopbuf, pp->prefixlen, rai->ifname);
    478 
    479 		/* set other fields with protocol defaults */
    480 		pp->validlifetime = DEF_ADVVALIDLIFETIME;
    481 		pp->preflifetime = DEF_ADVPREFERREDLIFETIME;
    482 		pp->onlinkflg = 1;
    483 		pp->autoconfflg = 1;
    484 		pp->origin = PREFIX_FROM_KERNEL;
    485 
    486 		/* link into chain */
    487 		insque(pp, &rai->prefix);
    488 
    489 		/* counter increment */
    490 		rai->pfxs++;
    491 	}
    492 
    493 	freeifaddrs(ifap);
    494 }
    495 
    496 static void
    497 makeentry(buf, len, id, string, add)
    498 	char *buf;
    499 	size_t len;
    500 	int id;
    501 	char *string;
    502 	int add;
    503 {
    504 	char *ep = buf + len;
    505 
    506 	strlcpy(buf, string, len);
    507 	if (add) {
    508 		char *cp;
    509 
    510 		cp = (char *)strchr(buf, '\0');
    511 		snprintf(cp, ep - cp, "%d", id);
    512 	}
    513 }
    514 
    515 /*
    516  * Add a prefix to the list of specified interface and reconstruct
    517  * the outgoing packet.
    518  * The prefix must not be in the list.
    519  * XXX: other parameter of the prefix(e.g. lifetime) shoule be
    520  * able to be specified.
    521  */
    522 static void
    523 add_prefix(struct rainfo *rai, struct in6_prefixreq *ipr)
    524 {
    525 	struct prefix *prefix;
    526 	u_char ntopbuf[INET6_ADDRSTRLEN];
    527 
    528 	if ((prefix = malloc(sizeof(*prefix))) == NULL) {
    529 		syslog(LOG_ERR, "<%s> memory allocation failed",
    530 		       __func__);
    531 		return;		/* XXX: error or exit? */
    532 	}
    533 	memset(prefix, 0, sizeof(*prefix));
    534 	prefix->prefix = ipr->ipr_prefix.sin6_addr;
    535 	prefix->prefixlen = ipr->ipr_plen;
    536 	prefix->validlifetime = ipr->ipr_vltime;
    537 	prefix->preflifetime = ipr->ipr_pltime;
    538 	prefix->onlinkflg = ipr->ipr_raf_onlink;
    539 	prefix->autoconfflg = ipr->ipr_raf_auto;
    540 	prefix->origin = PREFIX_FROM_DYNAMIC;
    541 
    542 	insque(prefix, &rai->prefix);
    543 
    544 	syslog(LOG_DEBUG, "<%s> new prefix %s/%d was added on %s",
    545 	       __func__, inet_ntop(AF_INET6, &ipr->ipr_prefix.sin6_addr,
    546 				       ntopbuf, INET6_ADDRSTRLEN),
    547 	       ipr->ipr_plen, rai->ifname);
    548 
    549 	/* free the previous packet */
    550 	free(rai->ra_data);
    551 	rai->ra_data = NULL;
    552 
    553 	/* reconstruct the packet */
    554 	rai->pfxs++;
    555 	make_packet(rai);
    556 
    557 	/*
    558 	 * reset the timer so that the new prefix will be advertised quickly.
    559 	 */
    560 	rai->initcounter = 0;
    561 	ra_timer_update((void *)rai, &rai->timer->tm);
    562 	rtadvd_set_timer(&rai->timer->tm, rai->timer);
    563 }
    564 
    565 /*
    566  * Delete a prefix to the list of specified interface and reconstruct
    567  * the outgoing packet.
    568  * The prefix must be in the list.
    569  */
    570 void
    571 delete_prefix(struct rainfo *rai, struct prefix *prefix)
    572 {
    573 	u_char ntopbuf[INET6_ADDRSTRLEN];
    574 
    575 	remque(prefix);
    576 	syslog(LOG_DEBUG, "<%s> prefix %s/%d was deleted on %s",
    577 	       __func__, inet_ntop(AF_INET6, &prefix->prefix,
    578 				       ntopbuf, INET6_ADDRSTRLEN),
    579 	       prefix->prefixlen, rai->ifname);
    580 	free(prefix);
    581 	rai->pfxs--;
    582 	make_packet(rai);
    583 }
    584 
    585 /*
    586  * Try to get an in6_prefixreq contents for a prefix which matches
    587  * ipr->ipr_prefix and ipr->ipr_plen and belongs to
    588  * the interface whose name is ipr->ipr_name[].
    589  */
    590 static int
    591 init_prefix(struct in6_prefixreq *ipr)
    592 {
    593 #if 0
    594 	int s;
    595 
    596 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
    597 		syslog(LOG_ERR, "<%s> socket: %s", __func__,
    598 		       strerror(errno));
    599 		exit(1);
    600 	}
    601 
    602 	if (ioctl(s, SIOCGIFPREFIX_IN6, (caddr_t)ipr) < 0) {
    603 		syslog(LOG_INFO, "<%s> ioctl:SIOCGIFPREFIX %s", __func__,
    604 		       strerror(errno));
    605 
    606 		ipr->ipr_vltime = DEF_ADVVALIDLIFETIME;
    607 		ipr->ipr_pltime = DEF_ADVPREFERREDLIFETIME;
    608 		ipr->ipr_raf_onlink = 1;
    609 		ipr->ipr_raf_auto = 1;
    610 		/* omit other field initialization */
    611 	}
    612 	else if (ipr->ipr_origin < PR_ORIG_RR) {
    613 		u_char ntopbuf[INET6_ADDRSTRLEN];
    614 
    615 		syslog(LOG_WARNING, "<%s> Added prefix(%s)'s origin %d is"
    616 		       "lower than PR_ORIG_RR(router renumbering)."
    617 		       "This should not happen if I am router", __func__,
    618 		       inet_ntop(AF_INET6, &ipr->ipr_prefix.sin6_addr, ntopbuf,
    619 				 sizeof(ntopbuf)), ipr->ipr_origin);
    620 		close(s);
    621 		return 1;
    622 	}
    623 
    624 	close(s);
    625 	return 0;
    626 #else
    627 	ipr->ipr_vltime = DEF_ADVVALIDLIFETIME;
    628 	ipr->ipr_pltime = DEF_ADVPREFERREDLIFETIME;
    629 	ipr->ipr_raf_onlink = 1;
    630 	ipr->ipr_raf_auto = 1;
    631 	return 0;
    632 #endif
    633 }
    634 
    635 void
    636 make_prefix(struct rainfo *rai, int ifindex, struct in6_addr *addr, int plen)
    637 {
    638 	struct in6_prefixreq ipr;
    639 
    640 	memset(&ipr, 0, sizeof(ipr));
    641 	if (if_indextoname(ifindex, ipr.ipr_name) == NULL) {
    642 		syslog(LOG_ERR, "<%s> Prefix added interface No.%d doesn't"
    643 		       "exist. This should not happen! %s", __func__,
    644 		       ifindex, strerror(errno));
    645 		exit(1);
    646 	}
    647 	ipr.ipr_prefix.sin6_len = sizeof(ipr.ipr_prefix);
    648 	ipr.ipr_prefix.sin6_family = AF_INET6;
    649 	ipr.ipr_prefix.sin6_addr = *addr;
    650 	ipr.ipr_plen = plen;
    651 
    652 	if (init_prefix(&ipr))
    653 		return; /* init failed by some error */
    654 	add_prefix(rai, &ipr);
    655 }
    656 
    657 void
    658 make_packet(struct rainfo *rainfo)
    659 {
    660 	size_t packlen, lladdroptlen = 0;
    661 	char *buf;
    662 	struct nd_router_advert *ra;
    663 	struct nd_opt_prefix_info *ndopt_pi;
    664 	struct nd_opt_mtu *ndopt_mtu;
    665 	struct prefix *pfx;
    666 
    667 	/* calculate total length */
    668 	packlen = sizeof(struct nd_router_advert);
    669 	if (rainfo->advlinkopt) {
    670 		if ((lladdroptlen = lladdropt_length(rainfo->sdl)) == 0) {
    671 			syslog(LOG_INFO,
    672 			       "<%s> link-layer address option has"
    673 			       " null length on %s.  Treat as not included.",
    674 			       __func__, rainfo->ifname);
    675 			rainfo->advlinkopt = 0;
    676 		}
    677 		packlen += lladdroptlen;
    678 	}
    679 	if (rainfo->pfxs)
    680 		packlen += sizeof(struct nd_opt_prefix_info) * rainfo->pfxs;
    681 	if (rainfo->linkmtu)
    682 		packlen += sizeof(struct nd_opt_mtu);
    683 
    684 	/* allocate memory for the packet */
    685 	if ((buf = malloc(packlen)) == NULL) {
    686 		syslog(LOG_ERR,
    687 		       "<%s> can't get enough memory for an RA packet",
    688 		       __func__);
    689 		exit(1);
    690 	}
    691 	if (rainfo->ra_data) {
    692 		/* free the previous packet */
    693 		free(rainfo->ra_data);
    694 		rainfo->ra_data = NULL;
    695 	}
    696 	rainfo->ra_data = buf;
    697 	/* XXX: what if packlen > 576? */
    698 	rainfo->ra_datalen = packlen;
    699 
    700 	/*
    701 	 * construct the packet
    702 	 */
    703 	ra = (struct nd_router_advert *)buf;
    704 	ra->nd_ra_type = ND_ROUTER_ADVERT;
    705 	ra->nd_ra_code = 0;
    706 	ra->nd_ra_cksum = 0;
    707 	ra->nd_ra_curhoplimit = (u_int8_t)(0xff & rainfo->hoplimit);
    708 	ra->nd_ra_flags_reserved = 0; /* just in case */
    709 	/*
    710 	 * XXX: the router preference field, which is a 2-bit field, should be
    711 	 * initialized before other fields.
    712 	 */
    713 	ra->nd_ra_flags_reserved = 0xff & rainfo->rtpref;
    714 	ra->nd_ra_flags_reserved |=
    715 		rainfo->managedflg ? ND_RA_FLAG_MANAGED : 0;
    716 	ra->nd_ra_flags_reserved |=
    717 		rainfo->otherflg ? ND_RA_FLAG_OTHER : 0;
    718 	ra->nd_ra_router_lifetime = htons(rainfo->lifetime);
    719 	ra->nd_ra_reachable = htonl(rainfo->reachabletime);
    720 	ra->nd_ra_retransmit = htonl(rainfo->retranstimer);
    721 	buf += sizeof(*ra);
    722 
    723 	if (rainfo->advlinkopt) {
    724 		lladdropt_fill(rainfo->sdl, (struct nd_opt_hdr *)buf);
    725 		buf += lladdroptlen;
    726 	}
    727 
    728 	if (rainfo->linkmtu) {
    729 		ndopt_mtu = (struct nd_opt_mtu *)buf;
    730 		ndopt_mtu->nd_opt_mtu_type = ND_OPT_MTU;
    731 		ndopt_mtu->nd_opt_mtu_len = 1;
    732 		ndopt_mtu->nd_opt_mtu_reserved = 0;
    733 		ndopt_mtu->nd_opt_mtu_mtu = htonl(rainfo->linkmtu);
    734 		buf += sizeof(struct nd_opt_mtu);
    735 	}
    736 
    737 
    738 
    739 	for (pfx = rainfo->prefix.next;
    740 	     pfx != &rainfo->prefix; pfx = pfx->next) {
    741 		u_int32_t vltime, pltime;
    742 		struct timeval now;
    743 
    744 		ndopt_pi = (struct nd_opt_prefix_info *)buf;
    745 		ndopt_pi->nd_opt_pi_type = ND_OPT_PREFIX_INFORMATION;
    746 		ndopt_pi->nd_opt_pi_len = 4;
    747 		ndopt_pi->nd_opt_pi_prefix_len = pfx->prefixlen;
    748 		ndopt_pi->nd_opt_pi_flags_reserved = 0;
    749 		if (pfx->onlinkflg)
    750 			ndopt_pi->nd_opt_pi_flags_reserved |=
    751 				ND_OPT_PI_FLAG_ONLINK;
    752 		if (pfx->autoconfflg)
    753 			ndopt_pi->nd_opt_pi_flags_reserved |=
    754 				ND_OPT_PI_FLAG_AUTO;
    755 		if (pfx->vltimeexpire || pfx->pltimeexpire)
    756 			gettimeofday(&now, NULL);
    757 		if (pfx->vltimeexpire == 0)
    758 			vltime = pfx->validlifetime;
    759 		else
    760 			vltime = (pfx->vltimeexpire > now.tv_sec) ?
    761 				pfx->vltimeexpire - now.tv_sec : 0;
    762 		if (pfx->pltimeexpire == 0)
    763 			pltime = pfx->preflifetime;
    764 		else
    765 			pltime = (pfx->pltimeexpire > now.tv_sec) ?
    766 				pfx->pltimeexpire - now.tv_sec : 0;
    767 		if (vltime < pltime) {
    768 			/*
    769 			 * this can happen if vltime is decrement but pltime
    770 			 * is not.
    771 			 */
    772 			pltime = vltime;
    773 		}
    774 		ndopt_pi->nd_opt_pi_valid_time = htonl(vltime);
    775 		ndopt_pi->nd_opt_pi_preferred_time = htonl(pltime);
    776 		ndopt_pi->nd_opt_pi_reserved2 = 0;
    777 		ndopt_pi->nd_opt_pi_prefix = pfx->prefix;
    778 
    779 		buf += sizeof(struct nd_opt_prefix_info);
    780 	}
    781 
    782 	return;
    783 }
    784 
    785 static int
    786 getinet6sysctl(int code)
    787 {
    788 	int mib[] = { CTL_NET, PF_INET6, IPPROTO_IPV6, 0 };
    789 	int value;
    790 	size_t size;
    791 
    792 	mib[3] = code;
    793 	size = sizeof(value);
    794 	if (sysctl(mib, sizeof(mib)/sizeof(mib[0]), &value, &size, NULL, 0)
    795 	    < 0) {
    796 		syslog(LOG_ERR, "<%s>: failed to get ip6 sysctl(%d): %s",
    797 		       __func__, code,
    798 		       strerror(errno));
    799 		return(-1);
    800 	}
    801 	else
    802 		return(value);
    803 }
    804