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