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