Home | History | Annotate | Line # | Download | only in rtadvd
config.c revision 1.10
      1 /*	$NetBSD: config.c,v 1.10 2001/01/23 15:35:54 itojun Exp $	*/
      2 /*	$KAME: config.c,v 1.29 2001/01/23 14:13:08 jinmei 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 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
     41 #include <net/if_var.h>
     42 #endif /* __FreeBSD__ >= 3 */
     43 #include <net/route.h>
     44 #include <net/if_dl.h>
     45 
     46 #include <netinet/in.h>
     47 #include <netinet/in_var.h>
     48 #include <netinet/ip6.h>
     49 #include <netinet6/ip6_var.h>
     50 #include <netinet/icmp6.h>
     51 #ifdef MIP6
     52 #include <netinet6/mip6.h>
     53 #endif
     54 
     55 #include <arpa/inet.h>
     56 
     57 #include <stdio.h>
     58 #include <syslog.h>
     59 #include <errno.h>
     60 #include <string.h>
     61 #include <stdlib.h>
     62 #if defined(__NetBSD__) || defined(__OpenBSD__)
     63 #include <search.h>
     64 #endif
     65 #include <unistd.h>
     66 #include <ifaddrs.h>
     67 
     68 #include "rtadvd.h"
     69 #include "advcap.h"
     70 #include "timer.h"
     71 #include "if.h"
     72 #include "config.h"
     73 
     74 static void makeentry __P((char *, int, char *, int));
     75 static void get_prefix __P((struct rainfo *));
     76 static int getinet6sysctl __P((int));
     77 
     78 extern struct rainfo *ralist;
     79 
     80 void
     81 getconfig(intface)
     82 	char *intface;
     83 {
     84 	int stat, pfxs, i;
     85 	char tbuf[BUFSIZ];
     86 	struct rainfo *tmp;
     87 	long val;
     88 	char buf[BUFSIZ];
     89 	char *bp = buf;
     90 	char *addr;
     91 	static int forwarding = -1;
     92 
     93 #define MUSTHAVE(var, cap)	\
     94     do {								\
     95 	int t;								\
     96 	if ((t = agetnum(cap)) < 0) {					\
     97 		fprintf(stderr, "rtadvd: need %s for interface %s\n",	\
     98 			cap, intface);					\
     99 		exit(1);						\
    100 	}								\
    101 	var = t;							\
    102      } while (0)
    103 #define MAYHAVE(var, cap, def)	\
    104      do {								\
    105 	if ((var = agetnum(cap)) < 0)					\
    106 		var = def;						\
    107      } while (0)
    108 
    109 	if ((stat = agetent(tbuf, intface)) <= 0) {
    110 		memset(tbuf, 0, sizeof(tbuf));
    111 		syslog(LOG_INFO,
    112 		       "<%s> %s isn't defined in the configuration file"
    113 		       " or the configuration file doesn't exist."
    114 		       " Treat it as default",
    115 		        __FUNCTION__, intface);
    116 	}
    117 
    118 	tmp = (struct rainfo *)malloc(sizeof(*ralist));
    119 	memset(tmp, 0, sizeof(*tmp));
    120 	tmp->prefix.next = tmp->prefix.prev = &tmp->prefix;
    121 
    122 	/* check if we are allowed to forward packets (if not determined) */
    123 	if (forwarding < 0) {
    124 		if ((forwarding = getinet6sysctl(IPV6CTL_FORWARDING)) < 0)
    125 			exit(1);
    126 	}
    127 
    128 	/* get interface information */
    129 	if (agetflag("nolladdr"))
    130 		tmp->advlinkopt = 0;
    131 	else
    132 		tmp->advlinkopt = 1;
    133 	if (tmp->advlinkopt) {
    134 		if ((tmp->sdl = if_nametosdl(intface)) == NULL) {
    135 			syslog(LOG_ERR,
    136 			       "<%s> can't get information of %s",
    137 			       __FUNCTION__, intface);
    138 			exit(1);
    139 		}
    140 		tmp->ifindex = tmp->sdl->sdl_index;
    141 	} else
    142 		tmp->ifindex = if_nametoindex(intface);
    143 	strncpy(tmp->ifname, intface, sizeof(tmp->ifname));
    144 	if ((tmp->phymtu = if_getmtu(intface)) == 0) {
    145 		tmp->phymtu = IPV6_MMTU;
    146 		syslog(LOG_WARNING,
    147 		       "<%s> can't get interface mtu of %s. Treat as %d",
    148 		       __FUNCTION__, intface, IPV6_MMTU);
    149 	}
    150 
    151 	/*
    152 	 * set router configuration variables.
    153 	 */
    154 	MAYHAVE(val, "maxinterval", DEF_MAXRTRADVINTERVAL);
    155 	if (val < MIN_MAXINTERVAL || val > MAX_MAXINTERVAL) {
    156 		syslog(LOG_ERR,
    157 		       "<%s> maxinterval must be between %e and %u",
    158 		       __FUNCTION__, MIN_MAXINTERVAL, MAX_MAXINTERVAL);
    159 		exit(1);
    160 	}
    161 	tmp->maxinterval = (u_int)val;
    162 	MAYHAVE(val, "mininterval", tmp->maxinterval/3);
    163 	if (val < MIN_MININTERVAL || val > (tmp->maxinterval * 3) / 4) {
    164 		syslog(LOG_ERR,
    165 		       "<%s> mininterval must be between %e and %d",
    166 		       __FUNCTION__,
    167 		       MIN_MININTERVAL,
    168 		       (tmp->maxinterval * 3) / 4);
    169 		exit(1);
    170 	}
    171 	tmp->mininterval = (u_int)val;
    172 
    173 	MAYHAVE(val, "chlim", DEF_ADVCURHOPLIMIT);
    174 	tmp->hoplimit = val & 0xff;
    175 
    176 	MAYHAVE(val, "raflags", 0);
    177 	tmp->managedflg= val & ND_RA_FLAG_MANAGED;
    178 	tmp->otherflg = val & ND_RA_FLAG_OTHER;
    179 #ifdef MIP6
    180 	if (mobileip6)
    181 		tmp->haflg = val & ND_RA_FLAG_HA;
    182 #endif
    183 
    184 	MAYHAVE(val, "rltime", tmp->maxinterval * 3);
    185 	if (val && (val < tmp->maxinterval || val > MAXROUTERLIFETIME)) {
    186 		syslog(LOG_ERR,
    187 		       "<%s> router lifetime on %s must be 0 or"
    188 		       " between %d and %d",
    189 		       __FUNCTION__, 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_WARNING,
    203 		       "<%s> non zero router lifetime is specified for %s, "
    204 		       "which must not be allowed for hosts.",
    205 		       __FUNCTION__, intface);
    206 		exit(1);
    207 	}
    208 	tmp->lifetime = val & 0xffff;
    209 
    210 	MAYHAVE(val, "rtime", DEF_ADVREACHABLETIME);
    211 	if (val > MAXREACHABLETIME) {
    212 		syslog(LOG_ERR,
    213 		       "<%s> reachable time must be no greater than %d",
    214 		       __FUNCTION__, MAXREACHABLETIME);
    215 		exit(1);
    216 	}
    217 	tmp->reachabletime = (u_int32_t)val;
    218 
    219 	MAYHAVE(val, "retrans", DEF_ADVRETRANSTIMER);
    220 	if (val < 0 || val > 0xffffffff) {
    221 		syslog(LOG_ERR,
    222 		       "<%s> retrans time out of range", __FUNCTION__);
    223 		exit(1);
    224 	}
    225 	tmp->retranstimer = (u_int32_t)val;
    226 
    227 #ifdef MIP6
    228 	if (!mobileip6)
    229 #else
    230 	if (1)
    231 #endif
    232 	{
    233 		if (agetstr("hapref", &bp) || agetstr("hatime", &bp)) {
    234 			syslog(LOG_ERR,
    235 			       "<%s> mobile-ip6 configuration without "
    236 			       "proper command line option",
    237 			       __FUNCTION__);
    238 			exit(1);
    239 		}
    240 	}
    241 #ifdef MIP6
    242 	else {
    243 		tmp->hapref = 0;
    244 		if ((val = agetnum("hapref")) >= 0)
    245 			tmp->hapref = (int16_t)val;
    246 		if (tmp->hapref != 0) {
    247 			tmp->hatime = 0;
    248 			MUSTHAVE(val, "hatime");
    249 			tmp->hatime = (u_int16_t)val;
    250 			if (tmp->hatime <= 0) {
    251 				syslog(LOG_ERR,
    252 				       "<%s> home agent lifetime must be greater than 0",
    253 				       __FUNCTION__);
    254 				exit(1);
    255 			}
    256 		}
    257 	}
    258 #endif
    259 
    260 	/* prefix information */
    261 
    262 	/*
    263 	 * This is an implementation specific parameter to consinder
    264 	 * link propagation delays and poorly synchronized clocks when
    265 	 * checking consistency of advertised lifetimes.
    266 	 */
    267 	MAYHAVE(val, "clockskew", 0);
    268 	tmp->clockskew = val;
    269 
    270 	if ((pfxs = agetnum("addrs")) < 0) {
    271 		/* auto configure prefix information */
    272 		if (agetstr("addr", &bp) || agetstr("addr1", &bp)) {
    273 			syslog(LOG_ERR,
    274 			       "<%s> conflicting prefix configuration for %s: "
    275 			       "automatic and manual config at the same time",
    276 			       __FUNCTION__, intface);
    277 			exit(1);
    278 		}
    279 		get_prefix(tmp);
    280 	}
    281 	else {
    282 		tmp->pfxs = pfxs;
    283 		for (i = 0; i < pfxs; i++) {
    284 			struct prefix *pfx;
    285 			char entbuf[256];
    286 			int added = (pfxs > 1) ? 1 : 0;
    287 
    288 			/* allocate memory to store prefix information */
    289 			if ((pfx = malloc(sizeof(struct prefix))) == NULL) {
    290 				syslog(LOG_ERR,
    291 				       "<%s> can't allocate enough memory",
    292 				       __FUNCTION__);
    293 				exit(1);
    294 			}
    295 			memset(pfx, 0, sizeof(*pfx));
    296 
    297 			/* link into chain */
    298 			insque(pfx, &tmp->prefix);
    299 
    300 			pfx->origin = PREFIX_FROM_CONFIG;
    301 
    302 			makeentry(entbuf, i, "prefixlen", added);
    303 			MAYHAVE(val, entbuf, 64);
    304 			if (val < 0 || val > 128) {
    305 				syslog(LOG_ERR,
    306 				       "<%s> prefixlen out of range",
    307 				       __FUNCTION__);
    308 				exit(1);
    309 			}
    310 			pfx->prefixlen = (int)val;
    311 
    312 			makeentry(entbuf, i, "pinfoflags", added);
    313 #ifdef MIP6
    314 			if (mobileip6)
    315 			{
    316 				MAYHAVE(val, entbuf,
    317 				    (ND_OPT_PI_FLAG_ONLINK|ND_OPT_PI_FLAG_AUTO|
    318 					 ND_OPT_PI_FLAG_RTADDR));
    319 			} else
    320 #endif
    321 			{
    322 				MAYHAVE(val, entbuf,
    323 				    (ND_OPT_PI_FLAG_ONLINK|ND_OPT_PI_FLAG_AUTO));
    324 			}
    325 			pfx->onlinkflg = val & ND_OPT_PI_FLAG_ONLINK;
    326 			pfx->autoconfflg = val & ND_OPT_PI_FLAG_AUTO;
    327 #ifdef MIP6
    328 			pfx->routeraddr = val & ND_OPT_PI_FLAG_RTADDR;
    329 #endif
    330 
    331 			makeentry(entbuf, i, "vltime", added);
    332 			MAYHAVE(val, entbuf, DEF_ADVVALIDLIFETIME);
    333 			if (val < 0 || val > 0xffffffff) {
    334 				syslog(LOG_ERR,
    335 				       "<%s> vltime out of range",
    336 				       __FUNCTION__);
    337 				exit(1);
    338 			}
    339 			pfx->validlifetime = (u_int32_t)val;
    340 
    341 			makeentry(entbuf, i, "vltimedecr", added);
    342 			if (agetflag(entbuf)) {
    343 				struct timeval now;
    344 				gettimeofday(&now, 0);
    345 				pfx->vltimeexpire =
    346 					now.tv_sec + pfx->validlifetime;
    347 			}
    348 
    349 			makeentry(entbuf, i, "pltime", added);
    350 			MAYHAVE(val, entbuf, DEF_ADVPREFERREDLIFETIME);
    351 			if (val < 0 || val > 0xffffffff) {
    352 				syslog(LOG_ERR,
    353 				       "<%s> pltime out of range",
    354 				       __FUNCTION__);
    355 				exit(1);
    356 			}
    357 			pfx->preflifetime = (u_int32_t)val;
    358 
    359 			makeentry(entbuf, i, "pltimedecr", added);
    360 			if (agetflag(entbuf)) {
    361 				struct timeval now;
    362 				gettimeofday(&now, 0);
    363 				pfx->pltimeexpire =
    364 					now.tv_sec + pfx->preflifetime;
    365 			}
    366 
    367 			makeentry(entbuf, i, "addr", added);
    368 			addr = (char *)agetstr(entbuf, &bp);
    369 			if (addr == NULL) {
    370 				syslog(LOG_ERR,
    371 				       "<%s> need %s as an prefix for "
    372 				       "interface %s",
    373 				       __FUNCTION__, entbuf, intface);
    374 				exit(1);
    375 			}
    376 			if (inet_pton(AF_INET6, addr,
    377 				      &pfx->prefix) != 1) {
    378 				syslog(LOG_ERR,
    379 				       "<%s> inet_pton failed for %s",
    380 				       __FUNCTION__, addr);
    381 				exit(1);
    382 			}
    383 			if (IN6_IS_ADDR_MULTICAST(&pfx->prefix)) {
    384 				syslog(LOG_ERR,
    385 				       "<%s> multicast prefix(%s) must "
    386 				       "not be advertised (IF=%s)",
    387 				       __FUNCTION__, addr, intface);
    388 				exit(1);
    389 			}
    390 			if (IN6_IS_ADDR_LINKLOCAL(&pfx->prefix))
    391 				syslog(LOG_NOTICE,
    392 				       "<%s> link-local prefix(%s) will be"
    393 				       " advertised on %s",
    394 				       __FUNCTION__, addr, intface);
    395 		}
    396 	}
    397 
    398 	MAYHAVE(val, "mtu", 0);
    399 	if (val < 0 || val > 0xffffffff) {
    400 		syslog(LOG_ERR,
    401 		       "<%s> mtu out of range", __FUNCTION__);
    402 		exit(1);
    403 	}
    404 	tmp->linkmtu = (u_int32_t)val;
    405 	if (tmp->linkmtu == 0) {
    406 		char *mtustr;
    407 
    408 		if ((mtustr = (char *)agetstr("mtu", &bp)) &&
    409 		    strcmp(mtustr, "auto") == 0)
    410 			tmp->linkmtu = tmp->phymtu;
    411 	}
    412 	else if (tmp->linkmtu < IPV6_MMTU || tmp->linkmtu > tmp->phymtu) {
    413 		syslog(LOG_ERR,
    414 		       "<%s> advertised link mtu must be between"
    415 		       " least MTU and physical link MTU",
    416 		       __FUNCTION__);
    417 		exit(1);
    418 	}
    419 
    420 	/* okey */
    421 	tmp->next = ralist;
    422 	ralist = tmp;
    423 
    424 	/* construct the sending packet */
    425 	make_packet(tmp);
    426 
    427 	/* set timer */
    428 	tmp->timer = rtadvd_add_timer(ra_timeout, ra_timer_update,
    429 				      tmp, tmp);
    430 	ra_timer_update((void *)tmp, &tmp->timer->tm);
    431 	rtadvd_set_timer(&tmp->timer->tm, tmp->timer);
    432 }
    433 
    434 static void
    435 get_prefix(struct rainfo *rai)
    436 {
    437 	struct ifaddrs *ifap, *ifa;
    438 	struct prefix *pp;
    439 	struct in6_addr *a;
    440 	u_char *p, *ep, *m, *lim;
    441 	u_char ntopbuf[INET6_ADDRSTRLEN];
    442 
    443 	if (getifaddrs(&ifap) < 0) {
    444 		syslog(LOG_ERR,
    445 		       "<%s> can't get interface addresses",
    446 		       __FUNCTION__);
    447 		exit(1);
    448 	}
    449 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
    450 		if (strcmp(ifa->ifa_name, rai->ifname) != 0)
    451 			continue;
    452 		if (ifa->ifa_addr->sa_family != AF_INET6)
    453 			continue;
    454 		a = &((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr;
    455 		if (IN6_IS_ADDR_LINKLOCAL(a))
    456 			continue;
    457 
    458 		/* allocate memory to store prefix info. */
    459 		if ((pp = malloc(sizeof(*pp))) == NULL) {
    460 			syslog(LOG_ERR,
    461 			       "<%s> can't get allocate buffer for prefix",
    462 			       __FUNCTION__);
    463 			exit(1);
    464 		}
    465 		memset(pp, 0, sizeof(*pp));
    466 
    467 		/* set prefix length */
    468 		m = (u_char *)&((struct sockaddr_in6 *)ifa->ifa_netmask)->sin6_addr;
    469 		lim = (u_char *)(ifa->ifa_netmask) + ifa->ifa_netmask->sa_len;
    470 		pp->prefixlen = prefixlen(m, lim);
    471 		if (pp->prefixlen < 0 || pp->prefixlen > 128) {
    472 			syslog(LOG_ERR,
    473 			       "<%s> failed to get prefixlen "
    474 			       "or prefix is invalid",
    475 			       __FUNCTION__);
    476 			exit(1);
    477 		}
    478 
    479 		/* set prefix, sweep bits outside of prefixlen */
    480 		memcpy(&pp->prefix, a, sizeof(*a));
    481 		p = (u_char *)&pp->prefix;
    482 		ep = (u_char *)(&pp->prefix + 1);
    483 		while (m < lim)
    484 			*p++ &= *m++;
    485 		while (p < ep)
    486 			*p++ = 0x00;
    487 
    488 	        if (!inet_ntop(AF_INET6, &pp->prefix, ntopbuf,
    489 	            sizeof(ntopbuf))) {
    490 			syslog(LOG_ERR, "<%s> inet_ntop failed", __FUNCTION__);
    491 			exit(1);
    492 		}
    493 		syslog(LOG_DEBUG,
    494 		       "<%s> add %s/%d to prefix list on %s",
    495 		       __FUNCTION__, ntopbuf, pp->prefixlen, rai->ifname);
    496 
    497 		/* set other fields with protocol defaults */
    498 		pp->validlifetime = DEF_ADVVALIDLIFETIME;
    499 		pp->preflifetime = DEF_ADVPREFERREDLIFETIME;
    500 		pp->onlinkflg = 1;
    501 		pp->autoconfflg = 1;
    502 		pp->origin = PREFIX_FROM_KERNEL;
    503 
    504 		/* link into chain */
    505 		insque(pp, &rai->prefix);
    506 
    507 		/* counter increment */
    508 		rai->pfxs++;
    509 	}
    510 
    511 	freeifaddrs(ifap);
    512 }
    513 
    514 static void
    515 makeentry(buf, id, string, add)
    516     char *buf, *string;
    517     int id, add;
    518 {
    519 	strcpy(buf, string);
    520 	if (add) {
    521 		char *cp;
    522 
    523 		cp = (char *)index(buf, '\0');
    524 		cp += sprintf(cp, "%d", id);
    525 		*cp = '\0';
    526 	}
    527 }
    528 
    529 /*
    530  * Add a prefix to the list of specified interface and reconstruct
    531  * the outgoing packet.
    532  * The prefix must not be in the list.
    533  * XXX: other parameter of the prefix(e.g. lifetime) shoule be
    534  * able to be specified.
    535  */
    536 static void
    537 add_prefix(struct rainfo *rai, struct in6_prefixreq *ipr)
    538 {
    539 	struct prefix *prefix;
    540 	u_char ntopbuf[INET6_ADDRSTRLEN];
    541 
    542 	if ((prefix = malloc(sizeof(*prefix))) == NULL) {
    543 		syslog(LOG_ERR, "<%s> memory allocation failed",
    544 		       __FUNCTION__);
    545 		return;		/* XXX: error or exit? */
    546 	}
    547 	memset(prefix, 0, sizeof(*prefix));
    548 	prefix->prefix = ipr->ipr_prefix.sin6_addr;
    549 	prefix->prefixlen = ipr->ipr_plen;
    550 	prefix->validlifetime = ipr->ipr_vltime;
    551 	prefix->preflifetime = ipr->ipr_pltime;
    552 	prefix->onlinkflg = ipr->ipr_raf_onlink;
    553 	prefix->autoconfflg = ipr->ipr_raf_auto;
    554 	prefix->origin = PREFIX_FROM_DYNAMIC;
    555 
    556 	insque(prefix, &rai->prefix);
    557 
    558 	syslog(LOG_DEBUG, "<%s> new prefix %s/%d was added on %s",
    559 	       __FUNCTION__, inet_ntop(AF_INET6, &ipr->ipr_prefix.sin6_addr,
    560 				       ntopbuf, INET6_ADDRSTRLEN),
    561 	       ipr->ipr_plen, rai->ifname);
    562 
    563 	/* free the previous packet */
    564 	free(rai->ra_data);
    565 	rai->ra_data = 0;
    566 
    567 	/* reconstruct the packet */
    568 	rai->pfxs++;
    569 	make_packet(rai);
    570 
    571 	/*
    572 	 * reset the timer so that the new prefix will be advertised quickly.
    573 	 */
    574 	rai->initcounter = 0;
    575 	ra_timer_update((void *)rai, &rai->timer->tm);
    576 	rtadvd_set_timer(&rai->timer->tm, rai->timer);
    577 }
    578 
    579 /*
    580  * Delete a prefix to the list of specified interface and reconstruct
    581  * the outgoing packet.
    582  * The prefix must be in the list.
    583  */
    584 void
    585 delete_prefix(struct rainfo *rai, struct prefix *prefix)
    586 {
    587 	u_char ntopbuf[INET6_ADDRSTRLEN];
    588 
    589 	remque(prefix);
    590 	syslog(LOG_DEBUG, "<%s> prefix %s/%d was deleted on %s",
    591 	       __FUNCTION__, inet_ntop(AF_INET6, &prefix->prefix,
    592 				       ntopbuf, INET6_ADDRSTRLEN),
    593 	       prefix->prefixlen, rai->ifname);
    594 	free(prefix);
    595 	rai->pfxs--;
    596 	make_packet(rai);
    597 }
    598 
    599 /*
    600  * Try to get an in6_prefixreq contents for a prefix which matches
    601  * ipr->ipr_prefix and ipr->ipr_plen and belongs to
    602  * the interface whose name is ipr->ipr_name[].
    603  */
    604 static int
    605 init_prefix(struct in6_prefixreq *ipr)
    606 {
    607 	int s;
    608 
    609 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
    610 		syslog(LOG_ERR, "<%s> socket: %s", __FUNCTION__,
    611 		       strerror(errno));
    612 		exit(1);
    613 	}
    614 
    615 	if (ioctl(s, SIOCGIFPREFIX_IN6, (caddr_t)ipr) < 0) {
    616 		syslog(LOG_INFO, "<%s> ioctl:SIOCGIFPREFIX %s", __FUNCTION__,
    617 		       strerror(errno));
    618 
    619 		ipr->ipr_vltime = DEF_ADVVALIDLIFETIME;
    620 		ipr->ipr_pltime = DEF_ADVPREFERREDLIFETIME;
    621 		ipr->ipr_raf_onlink = 1;
    622 		ipr->ipr_raf_auto = 1;
    623 		/* omit other field initialization */
    624 	}
    625 	else if (ipr->ipr_origin < PR_ORIG_RR) {
    626 		u_char ntopbuf[INET6_ADDRSTRLEN];
    627 
    628 		syslog(LOG_WARNING, "<%s> Added prefix(%s)'s origin %d is"
    629 		       "lower than PR_ORIG_RR(router renumbering)."
    630 		       "This should not happen if I am router", __FUNCTION__,
    631 		       inet_ntop(AF_INET6, &ipr->ipr_prefix.sin6_addr, ntopbuf,
    632 				 sizeof(ntopbuf)), ipr->ipr_origin);
    633 		close(s);
    634 		return 1;
    635 	}
    636 
    637 	close(s);
    638 	return 0;
    639 }
    640 
    641 void
    642 make_prefix(struct rainfo *rai, int ifindex, struct in6_addr *addr, int plen)
    643 {
    644 	struct in6_prefixreq ipr;
    645 
    646 	memset(&ipr, 0, sizeof(ipr));
    647 	if (if_indextoname(ifindex, ipr.ipr_name) == NULL) {
    648 		syslog(LOG_ERR, "<%s> Prefix added interface No.%d doesn't"
    649 		       "exist. This should not happen! %s", __FUNCTION__,
    650 		       ifindex, strerror(errno));
    651 		exit(1);
    652 	}
    653 	ipr.ipr_prefix.sin6_len = sizeof(ipr.ipr_prefix);
    654 	ipr.ipr_prefix.sin6_family = AF_INET6;
    655 	ipr.ipr_prefix.sin6_addr = *addr;
    656 	ipr.ipr_plen = plen;
    657 
    658 	if (init_prefix(&ipr))
    659 		return; /* init failed by some error */
    660 	add_prefix(rai, &ipr);
    661 }
    662 
    663 void
    664 make_packet(struct rainfo *rainfo)
    665 {
    666 	size_t packlen, lladdroptlen = 0;
    667 	char *buf;
    668 	struct nd_router_advert *ra;
    669 	struct nd_opt_prefix_info *ndopt_pi;
    670 	struct nd_opt_mtu *ndopt_mtu;
    671 #ifdef MIP6
    672 	struct nd_opt_advint *ndopt_advint;
    673 	struct nd_opt_hai *ndopt_hai;
    674 #endif
    675 	struct prefix *pfx;
    676 
    677 	/* calculate total length */
    678 	packlen = sizeof(struct nd_router_advert);
    679 	if (rainfo->advlinkopt) {
    680 		if ((lladdroptlen = lladdropt_length(rainfo->sdl)) == 0) {
    681 			syslog(LOG_INFO,
    682 			       "<%s> link-layer address option has"
    683 			       " null length on %s."
    684 			       " Treat as not included.",
    685 			       __FUNCTION__, rainfo->ifname);
    686 			rainfo->advlinkopt = 0;
    687 		}
    688 		packlen += lladdroptlen;
    689 	}
    690 	if (rainfo->pfxs)
    691 		packlen += sizeof(struct nd_opt_prefix_info) * rainfo->pfxs;
    692 	if (rainfo->linkmtu)
    693 		packlen += sizeof(struct nd_opt_mtu);
    694 #ifdef MIP6
    695 	if (mobileip6 && rainfo->maxinterval)
    696 		packlen += sizeof(struct nd_opt_advint);
    697 	if (mobileip6 && rainfo->hatime)
    698 		packlen += sizeof(struct nd_opt_hai);
    699 #endif
    700 
    701 	/* allocate memory for the packet */
    702 	if ((buf = malloc(packlen)) == NULL) {
    703 		syslog(LOG_ERR,
    704 		       "<%s> can't get enough memory for an RA packet",
    705 		       __FUNCTION__);
    706 		exit(1);
    707 	}
    708 	rainfo->ra_data = buf;
    709 	/* XXX: what if packlen > 576? */
    710 	rainfo->ra_datalen = packlen;
    711 
    712 	/*
    713 	 * construct the packet
    714 	 */
    715 	ra = (struct nd_router_advert *)buf;
    716 	ra->nd_ra_type = ND_ROUTER_ADVERT;
    717 	ra->nd_ra_code = 0;
    718 	ra->nd_ra_cksum = 0;
    719 	ra->nd_ra_curhoplimit = (u_int8_t)(0xff & rainfo->hoplimit);
    720 	ra->nd_ra_flags_reserved = 0;
    721 	ra->nd_ra_flags_reserved |=
    722 		rainfo->managedflg ? ND_RA_FLAG_MANAGED : 0;
    723 	ra->nd_ra_flags_reserved |=
    724 		rainfo->otherflg ? ND_RA_FLAG_OTHER : 0;
    725 #ifdef MIP6
    726 	ra->nd_ra_flags_reserved |=
    727 		rainfo->haflg ? ND_RA_FLAG_HA : 0;
    728 #endif
    729 	ra->nd_ra_router_lifetime = htons(rainfo->lifetime);
    730 	ra->nd_ra_reachable = htonl(rainfo->reachabletime);
    731 	ra->nd_ra_retransmit = htonl(rainfo->retranstimer);
    732 	buf += sizeof(*ra);
    733 
    734 	if (rainfo->advlinkopt) {
    735 		lladdropt_fill(rainfo->sdl, (struct nd_opt_hdr *)buf);
    736 		buf += lladdroptlen;
    737 	}
    738 
    739 	if (rainfo->linkmtu) {
    740 		ndopt_mtu = (struct nd_opt_mtu *)buf;
    741 		ndopt_mtu->nd_opt_mtu_type = ND_OPT_MTU;
    742 		ndopt_mtu->nd_opt_mtu_len = 1;
    743 		ndopt_mtu->nd_opt_mtu_reserved = 0;
    744 		ndopt_mtu->nd_opt_mtu_mtu = ntohl(rainfo->linkmtu);
    745 		buf += sizeof(struct nd_opt_mtu);
    746 	}
    747 
    748 #ifdef MIP6
    749 	if (mobileip6 && rainfo->maxinterval) {
    750 		ndopt_advint = (struct nd_opt_advint *)buf;
    751 		ndopt_advint->nd_opt_int_type = ND_OPT_ADV_INTERVAL;
    752 		ndopt_advint->nd_opt_int_len = 1;
    753 		ndopt_advint->nd_opt_int_reserved = 0;
    754 		ndopt_advint->nd_opt_int_interval = ntohl(rainfo->maxinterval *
    755 							  1000);
    756 		buf += sizeof(struct nd_opt_advint);
    757 	}
    758 #endif
    759 
    760 #ifdef MIP6
    761 	if (rainfo->hatime) {
    762 		ndopt_hai = (struct nd_opt_hai *)buf;
    763 		ndopt_hai->nd_opt_hai_type = ND_OPT_HA_INFORMATION;
    764 		ndopt_hai->nd_opt_hai_len = 1;
    765 		ndopt_hai->nd_opt_hai_reserved = 0;
    766 		ndopt_hai->nd_opt_hai_pref = ntohs(rainfo->hapref);
    767 		ndopt_hai->nd_opt_hai_lifetime = ntohs(rainfo->hatime);
    768 		buf += sizeof(struct nd_opt_hai);
    769 	}
    770 #endif
    771 
    772 	for (pfx = rainfo->prefix.next;
    773 	     pfx != &rainfo->prefix; pfx = pfx->next) {
    774 		u_int32_t vltime, pltime;
    775 		struct timeval now;
    776 
    777 		ndopt_pi = (struct nd_opt_prefix_info *)buf;
    778 		ndopt_pi->nd_opt_pi_type = ND_OPT_PREFIX_INFORMATION;
    779 		ndopt_pi->nd_opt_pi_len = 4;
    780 		ndopt_pi->nd_opt_pi_prefix_len = pfx->prefixlen;
    781 		ndopt_pi->nd_opt_pi_flags_reserved = 0;
    782 		if (pfx->onlinkflg)
    783 			ndopt_pi->nd_opt_pi_flags_reserved |=
    784 				ND_OPT_PI_FLAG_ONLINK;
    785 		if (pfx->autoconfflg)
    786 			ndopt_pi->nd_opt_pi_flags_reserved |=
    787 				ND_OPT_PI_FLAG_AUTO;
    788 #ifdef MIP6
    789 		if (pfx->routeraddr)
    790 			ndopt_pi->nd_opt_pi_flags_reserved |=
    791 				ND_OPT_PI_FLAG_RTADDR;
    792 #endif
    793 		if (pfx->vltimeexpire || pfx->pltimeexpire)
    794 			gettimeofday(&now, NULL);
    795 		if (pfx->vltimeexpire == 0)
    796 			vltime = pfx->validlifetime;
    797 		else
    798 			vltime = (pfx->vltimeexpire > now.tv_sec) ?
    799 				pfx->vltimeexpire - now.tv_sec : 0;
    800 		if (pfx->pltimeexpire == 0)
    801 			pltime = pfx->preflifetime;
    802 		else
    803 			pltime = (pfx->pltimeexpire > now.tv_sec) ?
    804 				pfx->pltimeexpire - now.tv_sec : 0;
    805 		if (vltime < pltime) {
    806 			/*
    807 			 * this can happen if vltime is decrement but pltime
    808 			 * is not.
    809 			 */
    810 			pltime = vltime;
    811 		}
    812 		ndopt_pi->nd_opt_pi_valid_time = ntohl(vltime);
    813 		ndopt_pi->nd_opt_pi_preferred_time = ntohl(pltime);
    814 		ndopt_pi->nd_opt_pi_reserved2 = 0;
    815 		ndopt_pi->nd_opt_pi_prefix = pfx->prefix;
    816 
    817 		buf += sizeof(struct nd_opt_prefix_info);
    818 	}
    819 
    820 	return;
    821 }
    822 
    823 static int
    824 getinet6sysctl(int code)
    825 {
    826 	int mib[] = { CTL_NET, PF_INET6, IPPROTO_IPV6, 0 };
    827 	int value;
    828 	size_t size;
    829 
    830 	mib[3] = code;
    831 	size = sizeof(value);
    832 	if (sysctl(mib, sizeof(mib)/sizeof(mib[0]), &value, &size, NULL, 0)
    833 	    < 0) {
    834 		syslog(LOG_ERR, "<%s>: failed to get ip6 sysctl(%d): %s",
    835 		       __FUNCTION__, code,
    836 		       strerror(errno));
    837 		return(-1);
    838 	}
    839 	else
    840 		return(value);
    841 }
    842