Home | History | Annotate | Line # | Download | only in rtadvd
config.c revision 1.3
      1 /*	$NetBSD: config.c,v 1.3 1999/07/06 13:02:09 itojun Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 1998 WIDE Project.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the project nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/param.h>
     33 #include <sys/ioctl.h>
     34 #include <sys/socket.h>
     35 #include <sys/time.h>
     36 
     37 #include <net/if.h>
     38 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
     39 #include <net/if_var.h>
     40 #endif /* __FreeBSD__ >= 3 */
     41 #include <net/route.h>
     42 #include <net/if_dl.h>
     43 
     44 #include <netinet/in.h>
     45 #include <netinet/in_var.h>
     46 #include <netinet6/ip6.h>
     47 #include <netinet6/ip6_var.h>
     48 #include <netinet6/icmp6.h>
     49 
     50 #include <arpa/inet.h>
     51 
     52 #include <stdio.h>
     53 #include <syslog.h>
     54 #include <errno.h>
     55 #include <string.h>
     56 #include <stdlib.h>
     57 #ifdef __NetBSD__
     58 #include <search.h>
     59 #endif
     60 #include <unistd.h>
     61 
     62 #include "rtadvd.h"
     63 #include "advcap.h"
     64 #include "timer.h"
     65 #include "if.h"
     66 #include "config.h"
     67 
     68 static void makeentry __P((char *, int, char *, int));
     69 static void make_packet __P((struct rainfo *));
     70 static void get_prefix __P((struct rainfo *));
     71 
     72 extern struct rainfo *ralist;
     73 
     74 void
     75 getconfig(intface)
     76 	char *intface;
     77 {
     78 	int stat, pfxs, i;
     79 	char tbuf[BUFSIZ];
     80 	struct rainfo *tmp;
     81 	long val;
     82 	char buf[BUFSIZ];
     83 	char *bp = buf;
     84 	char *addr;
     85 
     86 #define MUSTHAVE(var, cap)	\
     87     {									\
     88 	int 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      }
     96 #define MAYHAVE(var, cap, def)	\
     97      {									\
     98 	if ((var = agetnum(cap)) < 0)					\
     99 		var = def;						\
    100      }
    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 		        __FUNCTION__, 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 	/* get interface information */
    116 	if (agetflag("nolladdr"))
    117 		tmp->advlinkopt = 0;
    118 	else
    119 		tmp->advlinkopt = 1;
    120 	if (tmp->advlinkopt) {
    121 		if ((tmp->sdl = if_nametosdl(intface)) == NULL) {
    122 			syslog(LOG_ERR,
    123 			       "<%s> can't get information of %s",
    124 			       __FUNCTION__, intface);
    125 			exit(1);
    126 		}
    127 		tmp->ifindex = tmp->sdl->sdl_index;
    128 	} else
    129 		tmp->ifindex = if_nametoindex(intface);
    130 	strncpy(tmp->ifname, intface, sizeof(tmp->ifname));
    131 	if ((tmp->phymtu = if_getmtu(intface)) == 0) {
    132 		tmp->phymtu = IPV6_MMTU;
    133 		syslog(LOG_WARNING,
    134 		       "<%s> can't get interface mtu of %s. Treat as %d",
    135 		       __FUNCTION__, intface, IPV6_MMTU);
    136 	}
    137 
    138 	/*
    139 	 * set router configuration variables.
    140 	 */
    141 	MAYHAVE(val, "maxinterval", DEF_MAXRTRADVINTERVAL);
    142 	if (val < MIN_MAXINTERVAL || val > MAX_MAXINTERVAL) {
    143 		syslog(LOG_ERR,
    144 		       "<%s> maxinterval must be between %d and %d",
    145 		       __FUNCTION__, MIN_MAXINTERVAL, MAX_MAXINTERVAL);
    146 		exit(1);
    147 	}
    148 	tmp->maxinterval = (u_int)val;
    149 	MAYHAVE(val, "mininterval", tmp->maxinterval/3);
    150 	if (val < MIN_MININTERVAL || val > (tmp->maxinterval * 3) / 4) {
    151 		syslog(LOG_ERR,
    152 		       "<%s> mininterval must be between %d and %d",
    153 		       __FUNCTION__,
    154 		       MIN_MININTERVAL,
    155 		       (tmp->maxinterval * 3) / 4);
    156 		exit(1);
    157 	}
    158 	tmp->mininterval = (u_int)val;
    159 
    160 	MAYHAVE(val, "chlim", DEF_ADVCURHOPLIMIT);
    161 	tmp->hoplimit = val & 0xff;
    162 
    163 	MAYHAVE(val, "raflags", 0);
    164 	tmp->managedflg= val & ND_RA_FLAG_MANAGED;
    165 	tmp->otherflg = val & ND_RA_FLAG_OTHER;
    166 
    167 	MAYHAVE(val, "rltime", tmp->maxinterval * 3);
    168 	if (val && (val < tmp->maxinterval || val > MAXROUTERLIFETIME)) {
    169 		syslog(LOG_ERR,
    170 		       "<%s> router lifetime on %s must be 0 or"
    171 		       " between %d and %d",
    172 		       __FUNCTION__, intface,
    173 		       tmp->maxinterval, MAXROUTERLIFETIME);
    174 		exit(1);
    175 	}
    176 	tmp->lifetime = val & 0xffff;
    177 
    178 	MAYHAVE(val, "rtime", DEF_ADVREACHABLETIME);
    179 	if (val > MAXREACHABLETIME) {
    180 		syslog(LOG_ERR,
    181 		       "<%s> reachable time must be no greater than %d",
    182 		       __FUNCTION__, MAXREACHABLETIME);
    183 		exit(1);
    184 	}
    185 	tmp->reachabletime = (u_int32_t)val;
    186 
    187 	MAYHAVE(val, "retrans", DEF_ADVRETRANSTIMER);
    188 	if (val < 0 || val > 0xffffffff) {
    189 		syslog(LOG_ERR,
    190 		       "<%s> retrans time out of range", __FUNCTION__);
    191 		exit(1);
    192 	}
    193 	tmp->retranstimer = (u_int32_t)val;
    194 
    195 	/* prefix information */
    196 	if ((pfxs = agetnum("addrs")) < 0) {
    197 		/* auto configure prefix information */
    198 		if (agetstr("addr", &bp) || agetstr("addr1", &bp)) {
    199 			syslog(LOG_ERR,
    200 			       "<%s> conflicting prefix configuration for %s: "
    201 			       "automatic and manual config at the same time",
    202 			       __FUNCTION__, intface);
    203 			exit(1);
    204 		}
    205 		get_prefix(tmp);
    206 	}
    207 	else {
    208 		tmp->pfxs = pfxs;
    209 		for (i = 0; i < pfxs; i++) {
    210 			struct prefix *pfx;
    211 			char entbuf[256];
    212 			int added = (pfxs > 1) ? 1 : 0;
    213 
    214 			/* allocate memory to store prefix information */
    215 			if ((pfx = malloc(sizeof(struct prefix))) == NULL) {
    216 				syslog(LOG_ERR,
    217 				       "<%s> can't allocate enough memory",
    218 				       __FUNCTION__);
    219 				exit(1);
    220 			}
    221 			/* link into chain */
    222 			insque(pfx, &tmp->prefix);
    223 
    224 			makeentry(entbuf, i, "prefixlen", added);
    225 			MAYHAVE(val, entbuf, 64);
    226 			if (val < 0 || val > 128) {
    227 				syslog(LOG_ERR,
    228 				       "<%s> prefixlen out of range",
    229 				       __FUNCTION__);
    230 				exit(1);
    231 			}
    232 			pfx->prefixlen = (int)val;
    233 
    234 			makeentry(entbuf, i, "pinfoflags", added);
    235 			MAYHAVE(val, entbuf,
    236 				(ND_OPT_PI_FLAG_ONLINK|ND_OPT_PI_FLAG_AUTO));
    237 			pfx->onlinkflg = val & ND_OPT_PI_FLAG_ONLINK;
    238 			pfx->autoconfflg = val & ND_OPT_PI_FLAG_AUTO;
    239 
    240 			makeentry(entbuf, i, "vltime", added);
    241 			MAYHAVE(val, entbuf, DEF_ADVVALIDLIFETIME);
    242 			if (val < 0 || val > 0xffffffff) {
    243 				syslog(LOG_ERR,
    244 				       "<%s> vltime out of range",
    245 				       __FUNCTION__);
    246 				exit(1);
    247 			}
    248 			pfx->validlifetime = (u_int32_t)val;
    249 
    250 			makeentry(entbuf, i, "pltime", added);
    251 			MAYHAVE(val, entbuf, DEF_ADVPREFERREDLIFETIME);
    252 			if (val < 0 || val > 0xffffffff) {
    253 				syslog(LOG_ERR,
    254 				       "<%s> pltime out of range",
    255 				       __FUNCTION__);
    256 				exit(1);
    257 			}
    258 			pfx->preflifetime = (u_int32_t)val;
    259 
    260 			makeentry(entbuf, i, "addr", added);
    261 			addr = (char *)agetstr(entbuf, &bp);
    262 			if (addr == NULL) {
    263 				syslog(LOG_ERR,
    264 				       "<%s> need %s as an prefix for "
    265 				       "interface %s",
    266 				       __FUNCTION__, entbuf, intface);
    267 				exit(1);
    268 			}
    269 			if (inet_pton(AF_INET6, addr,
    270 				      &pfx->prefix) != 1) {
    271 				syslog(LOG_ERR,
    272 				       "<%s> inet_pton failed for %s",
    273 				       __FUNCTION__, addr);
    274 				exit(1);
    275 			}
    276 			if (IN6_IS_ADDR_MULTICAST(&pfx->prefix)) {
    277 				syslog(LOG_ERR,
    278 				       "<%s> multicast prefix(%s) must "
    279 				       "not be advertised (IF=%s)",
    280 				       __FUNCTION__, addr, intface);
    281 				exit(1);
    282 			}
    283 			if (IN6_IS_ADDR_LINKLOCAL(&pfx->prefix))
    284 				syslog(LOG_NOTICE,
    285 				       "<%s> link-local prefix(%s) will be"
    286 				       " advertised on %s",
    287 				       __FUNCTION__, addr, intface);
    288 		}
    289 	}
    290 
    291 	MAYHAVE(val, "mtu", 0);
    292 	if (val < 0 || val > 0xffffffff) {
    293 		syslog(LOG_ERR,
    294 		       "<%s> mtu out of range", __FUNCTION__);
    295 		exit(1);
    296 	}
    297 	tmp->linkmtu = (u_int32_t)val;
    298 	if (tmp->linkmtu == 0) {
    299 		char *mtustr;
    300 
    301 		if ((mtustr = (char *)agetstr("mtu", &bp)) &&
    302 		    strcmp(mtustr, "auto") == 0)
    303 			tmp->linkmtu = tmp->phymtu;
    304 	}
    305 	else if (tmp->linkmtu < IPV6_MMTU || tmp->linkmtu > tmp->phymtu) {
    306 		syslog(LOG_ERR,
    307 		       "<%s> advertised link mtu must be between"
    308 		       " least MTU and physical link MTU",
    309 		       __FUNCTION__);
    310 		exit(1);
    311 	}
    312 
    313 	/* okey */
    314 	tmp->next = ralist;
    315 	ralist = tmp;
    316 
    317 	/* construct the sending packet */
    318 	make_packet(tmp);
    319 
    320 	/* set timer */
    321 	tmp->timer = rtadvd_add_timer(ra_timeout, ra_timer_update,
    322 				      tmp, tmp);
    323 	ra_timer_update((void *)tmp, &tmp->timer->tm);
    324 	rtadvd_set_timer(&tmp->timer->tm, tmp->timer);
    325 }
    326 
    327 static void
    328 get_prefix(struct rainfo *rai)
    329 {
    330 	size_t len;
    331 	u_char *buf, *lim, *next;
    332 	u_char ntopbuf[INET6_ADDRSTRLEN];
    333 
    334 	if ((len = rtbuf_len()) < 0) {
    335 		syslog(LOG_ERR,
    336 		       "<%s> can't get buffer length for routing info",
    337 		       __FUNCTION__);
    338 		exit(1);
    339 	}
    340 	if ((buf = malloc(len)) == NULL) {
    341 		syslog(LOG_ERR,
    342 		       "<%s> can't allocate buffer", __FUNCTION__);
    343 		exit(1);
    344 	}
    345 	if (get_rtinfo(buf, &len) < 0) {
    346 		syslog(LOG_ERR,
    347 		       "<%s> can't get routing inforamtion", __FUNCTION__);
    348 		exit(1);
    349 	}
    350 
    351 	lim = buf + len;
    352 	next = get_next_msg(buf, lim, rai->ifindex, &len,
    353 			    RTADV_TYPE2BITMASK(RTM_GET));
    354 	while (next < lim) {
    355 		struct prefix *pp;
    356 		struct in6_addr *a;
    357 
    358 		/* allocate memory to store prefix info. */
    359 		if ((pp = malloc(sizeof(*pp))) == NULL) {
    360 			syslog(LOG_ERR,
    361 			       "<%s> can't get allocate buffer for prefix",
    362 			       __FUNCTION__);
    363 			exit(1);
    364 		}
    365 		memset(pp, 0, sizeof(*pp));
    366 
    367 		/* set prefix and its length */
    368 		a = get_addr(next);
    369 		memcpy(&pp->prefix, a, sizeof(*a));
    370 		if ((pp->prefixlen = get_prefixlen(next)) < 0) {
    371 			syslog(LOG_ERR,
    372 			       "<%s> failed to get prefixlen "
    373 			       "or prefixl is invalid",
    374 			       __FUNCTION__);
    375 			exit(1);
    376 		}
    377 		syslog(LOG_DEBUG,
    378 		       "<%s> add %s/%d to prefix list on %s",
    379 		       __FUNCTION__,
    380 		       inet_ntop(AF_INET6, a, ntopbuf, INET6_ADDRSTRLEN),
    381 		       pp->prefixlen, rai->ifname);
    382 
    383 		/* set other fields with protocol defaults */
    384 		pp->validlifetime = DEF_ADVVALIDLIFETIME;
    385 		pp->preflifetime = DEF_ADVPREFERREDLIFETIME;
    386 		pp->onlinkflg = 1;
    387 		pp->autoconfflg = 1;
    388 
    389 		/* link into chain */
    390 		insque(pp, &rai->prefix);
    391 
    392 		/* counter increment */
    393 		rai->pfxs++;
    394 
    395 		/* forward pointer and get next prefix(if any) */
    396 		next += len;
    397 		next = get_next_msg(next, lim, rai->ifindex,
    398 				    &len, RTADV_TYPE2BITMASK(RTM_GET));
    399 	}
    400 
    401 	free(buf);
    402 }
    403 
    404 static void
    405 makeentry(buf, id, string, add)
    406     char *buf, *string;
    407     int id, add;
    408 {
    409 	strcpy(buf, string);
    410 	if (add) {
    411 		char *cp;
    412 
    413 		cp = (char *)index(buf, '\0');
    414 		cp += sprintf(cp, "%d", id);
    415 		*cp = '\0';
    416 	}
    417 }
    418 
    419 /*
    420  * Add a prefix to the list of specified interface and reconstruct
    421  * the outgoing packet.
    422  * The prefix must not be in the list.
    423  * XXX: other parameter of the prefix(e.g. lifetime) shoule be
    424  * able to be specified.
    425  */
    426 static void
    427 add_prefix(struct rainfo *rai, struct in6_prefixreq *ipr)
    428 {
    429 	struct prefix *prefix;
    430 	u_char ntopbuf[INET6_ADDRSTRLEN];
    431 
    432 	if ((prefix = malloc(sizeof(*prefix))) == NULL) {
    433 		syslog(LOG_ERR, "<%s> memory allocation failed",
    434 		       __FUNCTION__);
    435 		return;		/* XXX: error or exit? */
    436 	}
    437 	prefix->prefix = ipr->ipr_prefix.sin6_addr;
    438 	prefix->prefixlen = ipr->ipr_plen;
    439 	prefix->validlifetime = ipr->ipr_vltime;
    440 	prefix->preflifetime = ipr->ipr_pltime;
    441 	prefix->onlinkflg = ipr->ipr_raf_onlink;
    442 	prefix->autoconfflg = ipr->ipr_raf_auto;
    443 
    444 	insque(prefix, &rai->prefix);
    445 
    446 	syslog(LOG_DEBUG, "<%s> new prefix %s/%d was added on %s",
    447 	       __FUNCTION__, inet_ntop(AF_INET6, &ipr->ipr_prefix.sin6_addr,
    448 				       ntopbuf, INET6_ADDRSTRLEN),
    449 	       ipr->ipr_plen, rai->ifname);
    450 
    451 	/* free the previous packet */
    452 	free(rai->ra_data);
    453 	rai->ra_data = 0;
    454 
    455 	/* reconstruct the packet */
    456 	rai->pfxs++;
    457 	make_packet(rai);
    458 
    459 	/*
    460 	 * reset the timer so that the new prefix will be advertised quickly.
    461 	 */
    462 	rai->initcounter = 0;
    463 	ra_timer_update((void *)rai, &rai->timer->tm);
    464 	rtadvd_set_timer(&rai->timer->tm, rai->timer);
    465 }
    466 
    467 /*
    468  * Delete a prefix to the list of specified interface and reconstruct
    469  * the outgoing packet.
    470  * The prefix must be in the list
    471  */
    472 void
    473 delete_prefix(struct rainfo *rai, struct prefix *prefix)
    474 {
    475 	u_char ntopbuf[INET6_ADDRSTRLEN];
    476 
    477 	remque(prefix);
    478 	syslog(LOG_DEBUG, "<%s> prefix %s/%d was deleted on %s",
    479 	       __FUNCTION__, inet_ntop(AF_INET6, &prefix->prefix,
    480 				       ntopbuf, INET6_ADDRSTRLEN),
    481 	       prefix->prefixlen, rai->ifname);
    482 	free(prefix);
    483 	rai->pfxs--;
    484 	make_packet(rai);
    485 }
    486 
    487 /*
    488  * Try to get an in6_prefixreq contents for a prefix which matches
    489  * ipr->ipr_prefix and ipr->ipr_plen and belongs to
    490  * the interface whose name is ipr->ipr_name[].
    491  */
    492 static int
    493 init_prefix(struct in6_prefixreq *ipr)
    494 {
    495 	int s;
    496 
    497 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
    498 		syslog(LOG_ERR, "<%s> socket: %s", __FUNCTION__,
    499 		       strerror(errno));
    500 		exit(1);
    501 	}
    502 
    503 	if (ioctl(s, SIOCGIFPREFIX_IN6, (caddr_t)ipr) < 0) {
    504 		syslog(LOG_INFO, "<%s> ioctl:SIOCGIFPREFIX %s", __FUNCTION__,
    505 		       strerror(errno));
    506 
    507 		ipr->ipr_vltime = DEF_ADVVALIDLIFETIME;
    508 		ipr->ipr_pltime = DEF_ADVPREFERREDLIFETIME;
    509 		ipr->ipr_raf_onlink = 1;
    510 		ipr->ipr_raf_auto = 1;
    511 		/* omit other field initialization */
    512 	}
    513 	else if (ipr->ipr_origin < PR_ORIG_RR) {
    514 		u_char ntopbuf[INET6_ADDRSTRLEN];
    515 
    516 		syslog(LOG_WARNING, "<%s> Added prefix(%s)'s origin %d is"
    517 		       "lower than PR_ORIG_RR(router renumbering)."
    518 		       "This should not happen if I am router", __FUNCTION__,
    519 		       inet_ntop(AF_INET6, &ipr->ipr_prefix.sin6_addr, ntopbuf,
    520 				 sizeof(ntopbuf)), ipr->ipr_origin);
    521 		return 1;
    522 	}
    523 
    524 	close(s);
    525 	return 0;
    526 }
    527 
    528 void
    529 make_prefix(struct rainfo *rai, int ifindex, struct in6_addr *addr, int plen)
    530 {
    531 	struct in6_prefixreq ipr;
    532 
    533 	memset(&ipr, 0, sizeof(ipr));
    534 	if (if_indextoname(ifindex, ipr.ipr_name) == NULL) {
    535 		syslog(LOG_ERR, "<%s> Prefix added interface No.%d doesn't"
    536 		       "exist. This should not happen! %s", __FUNCTION__,
    537 		       ifindex, strerror(errno));
    538 		exit(1);
    539 	}
    540 	ipr.ipr_prefix.sin6_len = sizeof(ipr.ipr_prefix);
    541 	ipr.ipr_prefix.sin6_family = AF_INET6;
    542 	ipr.ipr_prefix.sin6_addr = *addr;
    543 	ipr.ipr_plen = plen;
    544 
    545 	if (init_prefix(&ipr))
    546 		return; /* init failed by some error */
    547 	add_prefix(rai, &ipr);
    548 }
    549 
    550 static void
    551 make_packet(struct rainfo *rainfo)
    552 {
    553 	size_t packlen, lladdroptlen = 0;
    554 	char *buf;
    555 	struct nd_router_advert *ra;
    556 	struct nd_opt_prefix_info *ndopt_pi;
    557 	struct nd_opt_mtu *ndopt_mtu;
    558 	struct prefix *pfx;
    559 
    560 	/* calculate total length */
    561 	packlen = sizeof(struct nd_router_advert);
    562 	if (rainfo->advlinkopt) {
    563 		if ((lladdroptlen = lladdropt_length(rainfo->sdl)) == 0) {
    564 			syslog(LOG_INFO,
    565 			       "<%s> link-layer address option has"
    566 			       " null length on %s."
    567 			       " Treat as not included.",
    568 			       __FUNCTION__, rainfo->ifname);
    569 			rainfo->advlinkopt = 0;
    570 		}
    571 		packlen += lladdroptlen;
    572 	}
    573 	if (rainfo->pfxs)
    574 		packlen += sizeof(struct nd_opt_prefix_info) * rainfo->pfxs;
    575 	if (rainfo->linkmtu)
    576 		packlen += sizeof(struct nd_opt_mtu);
    577 
    578 	/* allocate memory for the packet */
    579 	if ((buf = malloc(packlen)) == NULL) {
    580 		syslog(LOG_ERR,
    581 		       "<%s> can't get enough memory for an RA packet",
    582 		       __FUNCTION__);
    583 		exit(1);
    584 	}
    585 	rainfo->ra_data = buf;
    586 	/* XXX: what if packlen > 576? */
    587 	rainfo->ra_datalen = packlen;
    588 
    589 	/*
    590 	 * construct the packet
    591 	 */
    592 	ra = (struct nd_router_advert *)buf;
    593 	ra->nd_ra_type = ND_ROUTER_ADVERT;
    594 	ra->nd_ra_code = 0;
    595 	ra->nd_ra_cksum = 0;
    596 	ra->nd_ra_curhoplimit = (u_int8_t)(0xff & rainfo->hoplimit);
    597 	ra->nd_ra_flags_reserved = 0;
    598 	ra->nd_ra_flags_reserved |=
    599 		rainfo->managedflg ? ND_RA_FLAG_MANAGED : 0;
    600 	ra->nd_ra_flags_reserved |=
    601 		rainfo->otherflg ? ND_RA_FLAG_OTHER : 0;
    602 	ra->nd_ra_router_lifetime = htons(rainfo->lifetime);
    603 	ra->nd_ra_reachable = htonl(rainfo->reachabletime);
    604 	ra->nd_ra_retransmit = htonl(rainfo->retranstimer);
    605 	buf += sizeof(*ra);
    606 
    607 	if (rainfo->advlinkopt) {
    608 		lladdropt_fill(rainfo->sdl, (struct nd_opt_hdr *)buf);
    609 		buf += lladdroptlen;
    610 	}
    611 
    612 	if (rainfo->linkmtu) {
    613 		ndopt_mtu = (struct nd_opt_mtu *)buf;
    614 		ndopt_mtu->nd_opt_mtu_type = ND_OPT_MTU;
    615 		ndopt_mtu->nd_opt_mtu_len = 1;
    616 		ndopt_mtu->nd_opt_mtu_reserved = 0;
    617 		ndopt_mtu->nd_opt_mtu_mtu = ntohl(rainfo->linkmtu);
    618 		buf += sizeof(struct nd_opt_mtu);
    619 	}
    620 
    621 	for (pfx = rainfo->prefix.next;
    622 	     pfx != &rainfo->prefix; pfx = pfx->next) {
    623 		ndopt_pi = (struct nd_opt_prefix_info *)buf;
    624 		ndopt_pi->nd_opt_pi_type = ND_OPT_PREFIX_INFORMATION;
    625 		ndopt_pi->nd_opt_pi_len = 4;
    626 		ndopt_pi->nd_opt_pi_prefix_len = pfx->prefixlen;
    627 		ndopt_pi->nd_opt_pi_flags_reserved = 0;
    628 		if (pfx->onlinkflg)
    629 			ndopt_pi->nd_opt_pi_flags_reserved |=
    630 				ND_OPT_PI_FLAG_ONLINK;
    631 		if (pfx->autoconfflg)
    632 			ndopt_pi->nd_opt_pi_flags_reserved |=
    633 				ND_OPT_PI_FLAG_AUTO;
    634 		ndopt_pi->nd_opt_pi_valid_time = ntohl(pfx->validlifetime);
    635 		ndopt_pi->nd_opt_pi_preferred_time =
    636 			ntohl(pfx->preflifetime);
    637 		ndopt_pi->nd_opt_pi_reserved2 = 0;
    638 		ndopt_pi->nd_opt_pi_prefix = pfx->prefix;
    639 
    640 		buf += sizeof(struct nd_opt_prefix_info);
    641 	}
    642 
    643 	return;
    644 }
    645