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