xref: /freebsd/usr.sbin/rtadvd/config.c (revision 39beb93c3f8bdbf72a61fda42300b5ebed7390c8)
1 /*	$FreeBSD$	*/
2 /*	$KAME: config.c,v 1.84 2003/08/05 12:34:23 itojun Exp $	*/
3 
4 /*
5  * Copyright (C) 1998 WIDE Project.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the project nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #include <sys/param.h>
34 #include <sys/ioctl.h>
35 #include <sys/socket.h>
36 #include <sys/time.h>
37 #include <sys/sysctl.h>
38 
39 #include <net/if.h>
40 #include <net/if_var.h>
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 #include <netinet6/nd6.h>
50 
51 #include <arpa/inet.h>
52 
53 #include <stdio.h>
54 #include <syslog.h>
55 #include <errno.h>
56 #include <string.h>
57 #include <search.h>
58 #include <stdlib.h>
59 #include <unistd.h>
60 #include <ifaddrs.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 time_t prefix_timo = (60 * 120);	/* 2 hours.
69 					 * XXX: should be configurable. */
70 extern struct rainfo *ralist;
71 
72 static struct rtadvd_timer *prefix_timeout(void *);
73 static void makeentry(char *, size_t, int, char *);
74 static int getinet6sysctl(int);
75 
76 void
77 getconfig(intface)
78 	char *intface;
79 {
80 	int stat, i;
81 	char tbuf[BUFSIZ];
82 	struct rainfo *tmp;
83 	long val;
84 	int64_t val64;
85 	char buf[BUFSIZ];
86 	char *bp = buf;
87 	char *addr, *flagstr;
88 	static int forwarding = -1;
89 
90 #define MUSTHAVE(var, cap)	\
91     do {								\
92 	int64_t t;							\
93 	if ((t = agetnum(cap)) < 0) {					\
94 		fprintf(stderr, "rtadvd: need %s for interface %s\n",	\
95 			cap, intface);					\
96 		exit(1);						\
97 	}								\
98 	var = t;							\
99      } while (0)
100 #define MAYHAVE(var, cap, def)	\
101      do {								\
102 	if ((var = agetnum(cap)) < 0)					\
103 		var = def;						\
104      } while (0)
105 
106 	if ((stat = agetent(tbuf, intface)) <= 0) {
107 		memset(tbuf, 0, sizeof(tbuf));
108 		syslog(LOG_INFO,
109 		       "<%s> %s isn't defined in the configuration file"
110 		       " or the configuration file doesn't exist."
111 		       " Treat it as default",
112 		        __func__, intface);
113 	}
114 
115 	tmp = (struct rainfo *)malloc(sizeof(*ralist));
116 	if (tmp == NULL) {
117 		syslog(LOG_INFO, "<%s> %s: can't allocate enough memory",
118 		    __func__, intface);
119 		exit(1);
120 	}
121 	memset(tmp, 0, sizeof(*tmp));
122 	tmp->prefix.next = tmp->prefix.prev = &tmp->prefix;
123 #ifdef ROUTEINFO
124 	tmp->route.next = tmp->route.prev = &tmp->route;
125 #endif
126 
127 	/* check if we are allowed to forward packets (if not determined) */
128 	if (forwarding < 0) {
129 		if ((forwarding = getinet6sysctl(IPV6CTL_FORWARDING)) < 0)
130 			exit(1);
131 	}
132 
133 	/* get interface information */
134 	if (agetflag("nolladdr"))
135 		tmp->advlinkopt = 0;
136 	else
137 		tmp->advlinkopt = 1;
138 	if (tmp->advlinkopt) {
139 		if ((tmp->sdl = if_nametosdl(intface)) == NULL) {
140 			syslog(LOG_ERR,
141 			       "<%s> can't get information of %s",
142 			       __func__, intface);
143 			exit(1);
144 		}
145 		tmp->ifindex = tmp->sdl->sdl_index;
146 	} else
147 		tmp->ifindex = if_nametoindex(intface);
148 	strncpy(tmp->ifname, intface, sizeof(tmp->ifname));
149 	if ((tmp->phymtu = if_getmtu(intface)) == 0) {
150 		tmp->phymtu = IPV6_MMTU;
151 		syslog(LOG_WARNING,
152 		       "<%s> can't get interface mtu of %s. Treat as %d",
153 		       __func__, intface, IPV6_MMTU);
154 	}
155 
156 	/*
157 	 * set router configuration variables.
158 	 */
159 	MAYHAVE(val, "maxinterval", DEF_MAXRTRADVINTERVAL);
160 	if (val < MIN_MAXINTERVAL || val > MAX_MAXINTERVAL) {
161 		syslog(LOG_ERR,
162 		       "<%s> maxinterval (%ld) on %s is invalid "
163 		       "(must be between %u and %u)", __func__, val,
164 		       intface, MIN_MAXINTERVAL, MAX_MAXINTERVAL);
165 		exit(1);
166 	}
167 	tmp->maxinterval = (u_int)val;
168 	MAYHAVE(val, "mininterval", tmp->maxinterval/3);
169 	if (val < MIN_MININTERVAL || val > (tmp->maxinterval * 3) / 4) {
170 		syslog(LOG_ERR,
171 		       "<%s> mininterval (%ld) on %s is invalid "
172 		       "(must be between %d and %d)",
173 		       __func__, val, intface, MIN_MININTERVAL,
174 		       (tmp->maxinterval * 3) / 4);
175 		exit(1);
176 	}
177 	tmp->mininterval = (u_int)val;
178 
179 	MAYHAVE(val, "chlim", DEF_ADVCURHOPLIMIT);
180 	tmp->hoplimit = val & 0xff;
181 
182 	if ((flagstr = (char *)agetstr("raflags", &bp))) {
183 		val = 0;
184 		if (strchr(flagstr, 'm'))
185 			val |= ND_RA_FLAG_MANAGED;
186 		if (strchr(flagstr, 'o'))
187 			val |= ND_RA_FLAG_OTHER;
188 		if (strchr(flagstr, 'h'))
189 			val |= ND_RA_FLAG_RTPREF_HIGH;
190 		if (strchr(flagstr, 'l')) {
191 			if ((val & ND_RA_FLAG_RTPREF_HIGH)) {
192 				syslog(LOG_ERR, "<%s> the \'h\' and \'l\'"
193 				    " router flags are exclusive", __func__);
194 				exit(1);
195 			}
196 			val |= ND_RA_FLAG_RTPREF_LOW;
197 		}
198 	} else {
199 		MAYHAVE(val, "raflags", 0);
200 	}
201 	tmp->managedflg = val & ND_RA_FLAG_MANAGED;
202 	tmp->otherflg = val & ND_RA_FLAG_OTHER;
203 #ifndef ND_RA_FLAG_RTPREF_MASK
204 #define ND_RA_FLAG_RTPREF_MASK	0x18 /* 00011000 */
205 #define ND_RA_FLAG_RTPREF_RSV	0x10 /* 00010000 */
206 #endif
207 	tmp->rtpref = val & ND_RA_FLAG_RTPREF_MASK;
208 	if (tmp->rtpref == ND_RA_FLAG_RTPREF_RSV) {
209 		syslog(LOG_ERR, "<%s> invalid router preference (%02x) on %s",
210 		       __func__, tmp->rtpref, intface);
211 		exit(1);
212 	}
213 
214 	MAYHAVE(val, "rltime", tmp->maxinterval * 3);
215 	if (val && (val < tmp->maxinterval || val > MAXROUTERLIFETIME)) {
216 		syslog(LOG_ERR,
217 		       "<%s> router lifetime (%ld) on %s is invalid "
218 		       "(must be 0 or between %d and %d)",
219 		       __func__, val, intface,
220 		       tmp->maxinterval,
221 		       MAXROUTERLIFETIME);
222 		exit(1);
223 	}
224 	/*
225 	 * Basically, hosts MUST NOT send Router Advertisement messages at any
226 	 * time (RFC 2461, Section 6.2.3). However, it would sometimes be
227 	 * useful to allow hosts to advertise some parameters such as prefix
228 	 * information and link MTU. Thus, we allow hosts to invoke rtadvd
229 	 * only when router lifetime (on every advertising interface) is
230 	 * explicitly set zero. (see also the above section)
231 	 */
232 	if (val && forwarding == 0) {
233 		syslog(LOG_ERR,
234 		       "<%s> non zero router lifetime is specified for %s, "
235 		       "which must not be allowed for hosts.  you must "
236 		       "change router lifetime or enable IPv6 forwarding.",
237 		       __func__, intface);
238 		exit(1);
239 	}
240 	tmp->lifetime = val & 0xffff;
241 
242 	MAYHAVE(val, "rtime", DEF_ADVREACHABLETIME);
243 	if (val < 0 || val > MAXREACHABLETIME) {
244 		syslog(LOG_ERR,
245 		       "<%s> reachable time (%ld) on %s is invalid "
246 		       "(must be no greater than %d)",
247 		       __func__, val, intface, MAXREACHABLETIME);
248 		exit(1);
249 	}
250 	tmp->reachabletime = (u_int32_t)val;
251 
252 	MAYHAVE(val64, "retrans", DEF_ADVRETRANSTIMER);
253 	if (val64 < 0 || val64 > 0xffffffff) {
254 		syslog(LOG_ERR, "<%s> retrans time (%lld) on %s out of range",
255 		       __func__, (long long)val64, intface);
256 		exit(1);
257 	}
258 	tmp->retranstimer = (u_int32_t)val64;
259 
260 	if (agetnum("hapref") != -1 || agetnum("hatime") != -1) {
261 		syslog(LOG_ERR,
262 		       "<%s> mobile-ip6 configuration not supported",
263 		       __func__);
264 		exit(1);
265 	}
266 	/* prefix information */
267 
268 	/*
269 	 * This is an implementation specific parameter to consider
270 	 * link propagation delays and poorly synchronized clocks when
271 	 * checking consistency of advertised lifetimes.
272 	 */
273 	MAYHAVE(val, "clockskew", 0);
274 	tmp->clockskew = val;
275 
276 	tmp->pfxs = 0;
277 	for (i = -1; i < MAXPREFIX; i++) {
278 		struct prefix *pfx;
279 		char entbuf[256];
280 
281 		makeentry(entbuf, sizeof(entbuf), i, "addr");
282 		addr = (char *)agetstr(entbuf, &bp);
283 		if (addr == NULL)
284 			continue;
285 
286 		/* allocate memory to store prefix information */
287 		if ((pfx = malloc(sizeof(struct prefix))) == NULL) {
288 			syslog(LOG_ERR,
289 			       "<%s> can't allocate enough memory",
290 			       __func__);
291 			exit(1);
292 		}
293 		memset(pfx, 0, sizeof(*pfx));
294 
295 		/* link into chain */
296 		insque(pfx, &tmp->prefix);
297 		tmp->pfxs++;
298 		pfx->rainfo = tmp;
299 
300 		pfx->origin = PREFIX_FROM_CONFIG;
301 
302 		if (inet_pton(AF_INET6, addr, &pfx->prefix) != 1) {
303 			syslog(LOG_ERR,
304 			       "<%s> inet_pton failed for %s",
305 			       __func__, addr);
306 			exit(1);
307 		}
308 		if (IN6_IS_ADDR_MULTICAST(&pfx->prefix)) {
309 			syslog(LOG_ERR,
310 			       "<%s> multicast prefix (%s) must "
311 			       "not be advertised on %s",
312 			       __func__, addr, intface);
313 			exit(1);
314 		}
315 		if (IN6_IS_ADDR_LINKLOCAL(&pfx->prefix))
316 			syslog(LOG_NOTICE,
317 			       "<%s> link-local prefix (%s) will be"
318 			       " advertised on %s",
319 			       __func__, addr, intface);
320 
321 		makeentry(entbuf, sizeof(entbuf), i, "prefixlen");
322 		MAYHAVE(val, entbuf, 64);
323 		if (val < 0 || val > 128) {
324 			syslog(LOG_ERR, "<%s> prefixlen (%ld) for %s "
325 			       "on %s out of range",
326 			       __func__, val, addr, intface);
327 			exit(1);
328 		}
329 		pfx->prefixlen = (int)val;
330 
331 		makeentry(entbuf, sizeof(entbuf), i, "pinfoflags");
332 		if ((flagstr = (char *)agetstr(entbuf, &bp))) {
333 			val = 0;
334 			if (strchr(flagstr, 'l'))
335 				val |= ND_OPT_PI_FLAG_ONLINK;
336 			if (strchr(flagstr, 'a'))
337 				val |= ND_OPT_PI_FLAG_AUTO;
338 		} else {
339 			MAYHAVE(val, entbuf,
340 			    (ND_OPT_PI_FLAG_ONLINK|ND_OPT_PI_FLAG_AUTO));
341 		}
342 		pfx->onlinkflg = val & ND_OPT_PI_FLAG_ONLINK;
343 		pfx->autoconfflg = val & ND_OPT_PI_FLAG_AUTO;
344 
345 		makeentry(entbuf, sizeof(entbuf), i, "vltime");
346 		MAYHAVE(val64, entbuf, DEF_ADVVALIDLIFETIME);
347 		if (val64 < 0 || val64 > 0xffffffff) {
348 			syslog(LOG_ERR, "<%s> vltime (%lld) for "
349 			    "%s/%d on %s is out of range",
350 			    __func__, (long long)val64,
351 			    addr, pfx->prefixlen, intface);
352 			exit(1);
353 		}
354 		pfx->validlifetime = (u_int32_t)val64;
355 
356 		makeentry(entbuf, sizeof(entbuf), i, "vltimedecr");
357 		if (agetflag(entbuf)) {
358 			struct timeval now;
359 			gettimeofday(&now, 0);
360 			pfx->vltimeexpire =
361 				now.tv_sec + pfx->validlifetime;
362 		}
363 
364 		makeentry(entbuf, sizeof(entbuf), i, "pltime");
365 		MAYHAVE(val64, entbuf, DEF_ADVPREFERREDLIFETIME);
366 		if (val64 < 0 || val64 > 0xffffffff) {
367 			syslog(LOG_ERR,
368 			    "<%s> pltime (%lld) for %s/%d on %s "
369 			    "is out of range",
370 			    __func__, (long long)val64,
371 			    addr, pfx->prefixlen, intface);
372 			exit(1);
373 		}
374 		pfx->preflifetime = (u_int32_t)val64;
375 
376 		makeentry(entbuf, sizeof(entbuf), i, "pltimedecr");
377 		if (agetflag(entbuf)) {
378 			struct timeval now;
379 			gettimeofday(&now, 0);
380 			pfx->pltimeexpire =
381 				now.tv_sec + pfx->preflifetime;
382 		}
383 	}
384 	if (tmp->pfxs == 0)
385 		get_prefix(tmp);
386 
387 	MAYHAVE(val, "mtu", 0);
388 	if (val < 0 || val > 0xffffffff) {
389 		syslog(LOG_ERR,
390 		       "<%s> mtu (%ld) on %s out of range",
391 		       __func__, val, intface);
392 		exit(1);
393 	}
394 	tmp->linkmtu = (u_int32_t)val;
395 	if (tmp->linkmtu == 0) {
396 		char *mtustr;
397 
398 		if ((mtustr = (char *)agetstr("mtu", &bp)) &&
399 		    strcmp(mtustr, "auto") == 0)
400 			tmp->linkmtu = tmp->phymtu;
401 	}
402 	else if (tmp->linkmtu < IPV6_MMTU || tmp->linkmtu > tmp->phymtu) {
403 		syslog(LOG_ERR,
404 		       "<%s> advertised link mtu (%lu) on %s is invalid (must "
405 		       "be between least MTU (%d) and physical link MTU (%d)",
406 		       __func__, (unsigned long)tmp->linkmtu, intface,
407 		       IPV6_MMTU, tmp->phymtu);
408 		exit(1);
409 	}
410 
411 #ifdef SIOCSIFINFO_IN6
412 	{
413 		struct in6_ndireq ndi;
414 		int s;
415 
416 		if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
417 			syslog(LOG_ERR, "<%s> socket: %s", __func__,
418 			       strerror(errno));
419 			exit(1);
420 		}
421 		memset(&ndi, 0, sizeof(ndi));
422 		strncpy(ndi.ifname, intface, IFNAMSIZ);
423 		if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&ndi) < 0) {
424 			syslog(LOG_INFO, "<%s> ioctl:SIOCGIFINFO_IN6 at %s: %s",
425 			     __func__, intface, strerror(errno));
426 		}
427 
428 		/* reflect the RA info to the host variables in kernel */
429 		ndi.ndi.chlim = tmp->hoplimit;
430 		ndi.ndi.retrans = tmp->retranstimer;
431 		ndi.ndi.basereachable = tmp->reachabletime;
432 		if (ioctl(s, SIOCSIFINFO_IN6, (caddr_t)&ndi) < 0) {
433 			syslog(LOG_INFO, "<%s> ioctl:SIOCSIFINFO_IN6 at %s: %s",
434 			     __func__, intface, strerror(errno));
435 		}
436 		close(s);
437 	}
438 #endif
439 
440 	/* route information */
441 #ifdef ROUTEINFO
442 	tmp->routes = 0;
443 	for (i = -1; i < MAXROUTE; i++) {
444 		struct rtinfo *rti;
445 		char entbuf[256], oentbuf[256];
446 
447 		makeentry(entbuf, sizeof(entbuf), i, "rtprefix");
448 		addr = (char *)agetstr(entbuf, &bp);
449 		if (addr == NULL) {
450 			makeentry(oentbuf, sizeof(oentbuf), i, "rtrprefix");
451 			addr = (char *)agetstr(oentbuf, &bp);
452 			if (addr) {
453 				fprintf(stderr, "%s was obsoleted.  Use %s.\n",
454 					oentbuf, entbuf);
455 			}
456 		}
457 		if (addr == NULL)
458 			continue;
459 
460 		/* allocate memory to store prefix information */
461 		if ((rti = malloc(sizeof(struct rtinfo))) == NULL) {
462 			syslog(LOG_ERR,
463 			       "<%s> can't allocate enough memory",
464 			       __func__);
465 			exit(1);
466 		}
467 		memset(rti, 0, sizeof(*rti));
468 
469 		/* link into chain */
470 		insque(rti, &tmp->route);
471 		tmp->routes++;
472 
473 		if (inet_pton(AF_INET6, addr, &rti->prefix) != 1) {
474 			syslog(LOG_ERR, "<%s> inet_pton failed for %s",
475 			       __func__, addr);
476 			exit(1);
477 		}
478 #if 0
479 		/*
480 		 * XXX: currently there's no restriction in route information
481 		 * prefix according to
482 		 * draft-ietf-ipngwg-router-selection-00.txt.
483 		 * However, I think the similar restriction be necessary.
484 		 */
485 		MAYHAVE(val64, entbuf, DEF_ADVVALIDLIFETIME);
486 		if (IN6_IS_ADDR_MULTICAST(&rti->prefix)) {
487 			syslog(LOG_ERR,
488 			       "<%s> multicast route (%s) must "
489 			       "not be advertised on %s",
490 			       __func__, addr, intface);
491 			exit(1);
492 		}
493 		if (IN6_IS_ADDR_LINKLOCAL(&rti->prefix)) {
494 			syslog(LOG_NOTICE,
495 			       "<%s> link-local route (%s) will "
496 			       "be advertised on %s",
497 			       __func__, addr, intface);
498 			exit(1);
499 		}
500 #endif
501 
502 		makeentry(entbuf, sizeof(entbuf), i, "rtplen");
503 		/* XXX: 256 is a magic number for compatibility check. */
504 		MAYHAVE(val, entbuf, 256);
505 		if (val == 256) {
506 			makeentry(oentbuf, sizeof(oentbuf), i, "rtrplen");
507 			MAYHAVE(val, oentbuf, 256);
508 			if (val != 256) {
509 				fprintf(stderr, "%s was obsoleted.  Use %s.\n",
510 					oentbuf, entbuf);
511 			} else
512 				val = 64;
513 		}
514 		if (val < 0 || val > 128) {
515 			syslog(LOG_ERR, "<%s> prefixlen (%ld) for %s on %s "
516 			       "out of range",
517 			       __func__, val, addr, intface);
518 			exit(1);
519 		}
520 		rti->prefixlen = (int)val;
521 
522 		makeentry(entbuf, sizeof(entbuf), i, "rtflags");
523 		if ((flagstr = (char *)agetstr(entbuf, &bp))) {
524 			val = 0;
525 			if (strchr(flagstr, 'h'))
526 				val |= ND_RA_FLAG_RTPREF_HIGH;
527 			if (strchr(flagstr, 'l')) {
528 				if ((val & ND_RA_FLAG_RTPREF_HIGH)) {
529 					syslog(LOG_ERR,
530 					    "<%s> the \'h\' and \'l\' route"
531 					    " preferences are exclusive",
532 					    __func__);
533 					exit(1);
534 				}
535 				val |= ND_RA_FLAG_RTPREF_LOW;
536 			}
537 		} else
538 			MAYHAVE(val, entbuf, 256); /* XXX */
539 		if (val == 256) {
540 			makeentry(oentbuf, sizeof(oentbuf), i, "rtrflags");
541 			MAYHAVE(val, oentbuf, 256);
542 			if (val != 256) {
543 				fprintf(stderr, "%s was obsoleted.  Use %s.\n",
544 					oentbuf, entbuf);
545 			} else
546 				val = 0;
547 		}
548 		rti->rtpref = val & ND_RA_FLAG_RTPREF_MASK;
549 		if (rti->rtpref == ND_RA_FLAG_RTPREF_RSV) {
550 			syslog(LOG_ERR, "<%s> invalid route preference (%02x) "
551 			       "for %s/%d on %s",
552 			       __func__, rti->rtpref, addr,
553 			       rti->prefixlen, intface);
554 			exit(1);
555 		}
556 
557 		/*
558 		 * Since the spec does not a default value, we should make
559 		 * this entry mandatory.  However, FreeBSD 4.4 has shipped
560 		 * with this field being optional, we use the router lifetime
561 		 * as an ad-hoc default value with a warning message.
562 		 */
563 		makeentry(entbuf, sizeof(entbuf), i, "rtltime");
564 		MAYHAVE(val64, entbuf, -1);
565 		if (val64 == -1) {
566 			makeentry(oentbuf, sizeof(oentbuf), i, "rtrltime");
567 			MAYHAVE(val64, oentbuf, -1);
568 			if (val64 != -1) {
569 				fprintf(stderr, "%s was obsoleted.  Use %s.\n",
570 					oentbuf, entbuf);
571 			} else {
572 				fprintf(stderr, "%s should be specified "
573 					"for interface %s.\n",
574 					entbuf, intface);
575 				val64 = tmp->lifetime;
576 			}
577 		}
578 		if (val64 < 0 || val64 > 0xffffffff) {
579 			syslog(LOG_ERR, "<%s> route lifetime (%lld) for "
580 			    "%s/%d on %s out of range", __func__,
581 			    (long long)val64, addr, rti->prefixlen, intface);
582 			exit(1);
583 		}
584 		rti->ltime = (u_int32_t)val64;
585 	}
586 #endif
587 
588 	/* okey */
589 	tmp->next = ralist;
590 	ralist = tmp;
591 
592 	/* construct the sending packet */
593 	make_packet(tmp);
594 
595 	/* set timer */
596 	tmp->timer = rtadvd_add_timer(ra_timeout, ra_timer_update,
597 				      tmp, tmp);
598 	ra_timer_update((void *)tmp, &tmp->timer->tm);
599 	rtadvd_set_timer(&tmp->timer->tm, tmp->timer);
600 }
601 
602 void
603 get_prefix(struct rainfo *rai)
604 {
605 	struct ifaddrs *ifap, *ifa;
606 	struct prefix *pp;
607 	struct in6_addr *a;
608 	u_char *p, *ep, *m, *lim;
609 	u_char ntopbuf[INET6_ADDRSTRLEN];
610 
611 	if (getifaddrs(&ifap) < 0) {
612 		syslog(LOG_ERR,
613 		       "<%s> can't get interface addresses",
614 		       __func__);
615 		exit(1);
616 	}
617 
618 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
619 		int plen;
620 
621 		if (strcmp(ifa->ifa_name, rai->ifname) != 0)
622 			continue;
623 		if (ifa->ifa_addr->sa_family != AF_INET6)
624 			continue;
625 		a = &((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr;
626 		if (IN6_IS_ADDR_LINKLOCAL(a))
627 			continue;
628 		/* get prefix length */
629 		m = (u_char *)&((struct sockaddr_in6 *)ifa->ifa_netmask)->sin6_addr;
630 		lim = (u_char *)(ifa->ifa_netmask) + ifa->ifa_netmask->sa_len;
631 		plen = prefixlen(m, lim);
632 		if (plen <= 0 || plen > 128) {
633 			syslog(LOG_ERR, "<%s> failed to get prefixlen "
634 			       "or prefix is invalid",
635 			       __func__);
636 			exit(1);
637 		}
638 		if (plen == 128)	/* XXX */
639 			continue;
640 		if (find_prefix(rai, a, plen)) {
641 			/* ignore a duplicated prefix. */
642 			continue;
643 		}
644 
645 		/* allocate memory to store prefix info. */
646 		if ((pp = malloc(sizeof(*pp))) == NULL) {
647 			syslog(LOG_ERR,
648 			       "<%s> can't get allocate buffer for prefix",
649 			       __func__);
650 			exit(1);
651 		}
652 		memset(pp, 0, sizeof(*pp));
653 
654 		/* set prefix, sweep bits outside of prefixlen */
655 		pp->prefixlen = plen;
656 		memcpy(&pp->prefix, a, sizeof(*a));
657 		p = (u_char *)&pp->prefix;
658 		ep = (u_char *)(&pp->prefix + 1);
659 		while (m < lim && p < ep)
660 			*p++ &= *m++;
661 		while (p < ep)
662 			*p++ = 0x00;
663 	        if (!inet_ntop(AF_INET6, &pp->prefix, ntopbuf,
664 	            sizeof(ntopbuf))) {
665 			syslog(LOG_ERR, "<%s> inet_ntop failed", __func__);
666 			exit(1);
667 		}
668 		syslog(LOG_DEBUG,
669 		       "<%s> add %s/%d to prefix list on %s",
670 		       __func__, ntopbuf, pp->prefixlen, rai->ifname);
671 
672 		/* set other fields with protocol defaults */
673 		pp->validlifetime = DEF_ADVVALIDLIFETIME;
674 		pp->preflifetime = DEF_ADVPREFERREDLIFETIME;
675 		pp->onlinkflg = 1;
676 		pp->autoconfflg = 1;
677 		pp->origin = PREFIX_FROM_KERNEL;
678 		pp->rainfo = rai;
679 
680 		/* link into chain */
681 		insque(pp, &rai->prefix);
682 
683 		/* counter increment */
684 		rai->pfxs++;
685 	}
686 
687 	freeifaddrs(ifap);
688 }
689 
690 static void
691 makeentry(buf, len, id, string)
692 	char *buf;
693 	size_t len;
694 	int id;
695 	char *string;
696 {
697 
698 	if (id < 0)
699 		strlcpy(buf, string, len);
700 	else
701 		snprintf(buf, len, "%s%d", string, id);
702 }
703 
704 /*
705  * Add a prefix to the list of specified interface and reconstruct
706  * the outgoing packet.
707  * The prefix must not be in the list.
708  * XXX: other parameters of the prefix (e.g. lifetime) should be
709  * able to be specified.
710  */
711 static void
712 add_prefix(struct rainfo *rai, struct in6_prefixreq *ipr)
713 {
714 	struct prefix *prefix;
715 	u_char ntopbuf[INET6_ADDRSTRLEN];
716 
717 	if ((prefix = malloc(sizeof(*prefix))) == NULL) {
718 		syslog(LOG_ERR, "<%s> memory allocation failed",
719 		       __func__);
720 		return;		/* XXX: error or exit? */
721 	}
722 	memset(prefix, 0, sizeof(*prefix));
723 	prefix->prefix = ipr->ipr_prefix.sin6_addr;
724 	prefix->prefixlen = ipr->ipr_plen;
725 	prefix->validlifetime = ipr->ipr_vltime;
726 	prefix->preflifetime = ipr->ipr_pltime;
727 	prefix->onlinkflg = ipr->ipr_raf_onlink;
728 	prefix->autoconfflg = ipr->ipr_raf_auto;
729 	prefix->origin = PREFIX_FROM_DYNAMIC;
730 
731 	insque(prefix, &rai->prefix);
732 	prefix->rainfo = rai;
733 
734 	syslog(LOG_DEBUG, "<%s> new prefix %s/%d was added on %s",
735 	       __func__, inet_ntop(AF_INET6, &ipr->ipr_prefix.sin6_addr,
736 				       ntopbuf, INET6_ADDRSTRLEN),
737 	       ipr->ipr_plen, rai->ifname);
738 
739 	/* free the previous packet */
740 	free(rai->ra_data);
741 	rai->ra_data = NULL;
742 
743 	/* reconstruct the packet */
744 	rai->pfxs++;
745 	make_packet(rai);
746 }
747 
748 /*
749  * Delete a prefix to the list of specified interface and reconstruct
750  * the outgoing packet.
751  * The prefix must be in the list.
752  */
753 void
754 delete_prefix(struct prefix *prefix)
755 {
756 	u_char ntopbuf[INET6_ADDRSTRLEN];
757 	struct rainfo *rai = prefix->rainfo;
758 
759 	remque(prefix);
760 	syslog(LOG_DEBUG, "<%s> prefix %s/%d was deleted on %s",
761 	       __func__, inet_ntop(AF_INET6, &prefix->prefix,
762 				       ntopbuf, INET6_ADDRSTRLEN),
763 	       prefix->prefixlen, rai->ifname);
764 	if (prefix->timer)
765 		rtadvd_remove_timer(&prefix->timer);
766 	free(prefix);
767 	rai->pfxs--;
768 }
769 
770 void
771 invalidate_prefix(struct prefix *prefix)
772 {
773 	u_char ntopbuf[INET6_ADDRSTRLEN];
774 	struct timeval timo;
775 	struct rainfo *rai = prefix->rainfo;
776 
777 	if (prefix->timer) {	/* sanity check */
778 		syslog(LOG_ERR,
779 		    "<%s> assumption failure: timer already exists",
780 		    __func__);
781 		exit(1);
782 	}
783 
784 	syslog(LOG_DEBUG, "<%s> prefix %s/%d was invalidated on %s, "
785 	    "will expire in %ld seconds", __func__,
786 	    inet_ntop(AF_INET6, &prefix->prefix, ntopbuf, INET6_ADDRSTRLEN),
787 	    prefix->prefixlen, rai->ifname, (long)prefix_timo);
788 
789 	/* set the expiration timer */
790 	prefix->timer = rtadvd_add_timer(prefix_timeout, NULL, prefix, NULL);
791 	if (prefix->timer == NULL) {
792 		syslog(LOG_ERR, "<%s> failed to add a timer for a prefix. "
793 		    "remove the prefix", __func__);
794 		delete_prefix(prefix);
795 	}
796 	timo.tv_sec = prefix_timo;
797 	timo.tv_usec = 0;
798 	rtadvd_set_timer(&timo, prefix->timer);
799 }
800 
801 static struct rtadvd_timer *
802 prefix_timeout(void *arg)
803 {
804 	struct prefix *prefix = (struct prefix *)arg;
805 
806 	delete_prefix(prefix);
807 
808 	return(NULL);
809 }
810 
811 void
812 update_prefix(struct prefix * prefix)
813 {
814 	u_char ntopbuf[INET6_ADDRSTRLEN];
815 	struct rainfo *rai = prefix->rainfo;
816 
817 	if (prefix->timer == NULL) { /* sanity check */
818 		syslog(LOG_ERR,
819 		    "<%s> assumption failure: timer does not exist",
820 		    __func__);
821 		exit(1);
822 	}
823 
824 	syslog(LOG_DEBUG, "<%s> prefix %s/%d was re-enabled on %s",
825 	    __func__, inet_ntop(AF_INET6, &prefix->prefix, ntopbuf,
826 	    INET6_ADDRSTRLEN), prefix->prefixlen, rai->ifname);
827 
828 	/* stop the expiration timer */
829 	rtadvd_remove_timer(&prefix->timer);
830 }
831 
832 /*
833  * Try to get an in6_prefixreq contents for a prefix which matches
834  * ipr->ipr_prefix and ipr->ipr_plen and belongs to
835  * the interface whose name is ipr->ipr_name[].
836  */
837 static int
838 init_prefix(struct in6_prefixreq *ipr)
839 {
840 #if 0
841 	int s;
842 
843 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
844 		syslog(LOG_ERR, "<%s> socket: %s", __func__,
845 		       strerror(errno));
846 		exit(1);
847 	}
848 
849 	if (ioctl(s, SIOCGIFPREFIX_IN6, (caddr_t)ipr) < 0) {
850 		syslog(LOG_INFO, "<%s> ioctl:SIOCGIFPREFIX %s", __func__,
851 		       strerror(errno));
852 
853 		ipr->ipr_vltime = DEF_ADVVALIDLIFETIME;
854 		ipr->ipr_pltime = DEF_ADVPREFERREDLIFETIME;
855 		ipr->ipr_raf_onlink = 1;
856 		ipr->ipr_raf_auto = 1;
857 		/* omit other field initialization */
858 	}
859 	else if (ipr->ipr_origin < PR_ORIG_RR) {
860 		u_char ntopbuf[INET6_ADDRSTRLEN];
861 
862 		syslog(LOG_WARNING, "<%s> Added prefix(%s)'s origin %d is"
863 		       "lower than PR_ORIG_RR(router renumbering)."
864 		       "This should not happen if I am router", __func__,
865 		       inet_ntop(AF_INET6, &ipr->ipr_prefix.sin6_addr, ntopbuf,
866 				 sizeof(ntopbuf)), ipr->ipr_origin);
867 		close(s);
868 		return 1;
869 	}
870 
871 	close(s);
872 	return 0;
873 #else
874 	ipr->ipr_vltime = DEF_ADVVALIDLIFETIME;
875 	ipr->ipr_pltime = DEF_ADVPREFERREDLIFETIME;
876 	ipr->ipr_raf_onlink = 1;
877 	ipr->ipr_raf_auto = 1;
878 	return 0;
879 #endif
880 }
881 
882 void
883 make_prefix(struct rainfo *rai, int ifindex, struct in6_addr *addr, int plen)
884 {
885 	struct in6_prefixreq ipr;
886 
887 	memset(&ipr, 0, sizeof(ipr));
888 	if (if_indextoname(ifindex, ipr.ipr_name) == NULL) {
889 		syslog(LOG_ERR, "<%s> Prefix added interface No.%d doesn't"
890 		       "exist. This should not happen! %s", __func__,
891 		       ifindex, strerror(errno));
892 		exit(1);
893 	}
894 	ipr.ipr_prefix.sin6_len = sizeof(ipr.ipr_prefix);
895 	ipr.ipr_prefix.sin6_family = AF_INET6;
896 	ipr.ipr_prefix.sin6_addr = *addr;
897 	ipr.ipr_plen = plen;
898 
899 	if (init_prefix(&ipr))
900 		return; /* init failed by some error */
901 	add_prefix(rai, &ipr);
902 }
903 
904 void
905 make_packet(struct rainfo *rainfo)
906 {
907 	size_t packlen, lladdroptlen = 0;
908 	char *buf;
909 	struct nd_router_advert *ra;
910 	struct nd_opt_prefix_info *ndopt_pi;
911 	struct nd_opt_mtu *ndopt_mtu;
912 #ifdef ROUTEINFO
913 	struct nd_opt_route_info *ndopt_rti;
914 	struct rtinfo *rti;
915 #endif
916 	struct prefix *pfx;
917 
918 	/* calculate total length */
919 	packlen = sizeof(struct nd_router_advert);
920 	if (rainfo->advlinkopt) {
921 		if ((lladdroptlen = lladdropt_length(rainfo->sdl)) == 0) {
922 			syslog(LOG_INFO,
923 			       "<%s> link-layer address option has"
924 			       " null length on %s.  Treat as not included.",
925 			       __func__, rainfo->ifname);
926 			rainfo->advlinkopt = 0;
927 		}
928 		packlen += lladdroptlen;
929 	}
930 	if (rainfo->pfxs)
931 		packlen += sizeof(struct nd_opt_prefix_info) * rainfo->pfxs;
932 	if (rainfo->linkmtu)
933 		packlen += sizeof(struct nd_opt_mtu);
934 #ifdef ROUTEINFO
935 	for (rti = rainfo->route.next; rti != &rainfo->route; rti = rti->next)
936 		packlen += sizeof(struct nd_opt_route_info) +
937 			   ((rti->prefixlen + 0x3f) >> 6) * 8;
938 #endif
939 
940 	/* allocate memory for the packet */
941 	if ((buf = malloc(packlen)) == NULL) {
942 		syslog(LOG_ERR,
943 		       "<%s> can't get enough memory for an RA packet",
944 		       __func__);
945 		exit(1);
946 	}
947 	if (rainfo->ra_data) {
948 		/* free the previous packet */
949 		free(rainfo->ra_data);
950 		rainfo->ra_data = NULL;
951 	}
952 	rainfo->ra_data = buf;
953 	/* XXX: what if packlen > 576? */
954 	rainfo->ra_datalen = packlen;
955 
956 	/*
957 	 * construct the packet
958 	 */
959 	ra = (struct nd_router_advert *)buf;
960 	ra->nd_ra_type = ND_ROUTER_ADVERT;
961 	ra->nd_ra_code = 0;
962 	ra->nd_ra_cksum = 0;
963 	ra->nd_ra_curhoplimit = (u_int8_t)(0xff & rainfo->hoplimit);
964 	ra->nd_ra_flags_reserved = 0; /* just in case */
965 	/*
966 	 * XXX: the router preference field, which is a 2-bit field, should be
967 	 * initialized before other fields.
968 	 */
969 	ra->nd_ra_flags_reserved = 0xff & rainfo->rtpref;
970 	ra->nd_ra_flags_reserved |=
971 		rainfo->managedflg ? ND_RA_FLAG_MANAGED : 0;
972 	ra->nd_ra_flags_reserved |=
973 		rainfo->otherflg ? ND_RA_FLAG_OTHER : 0;
974 	ra->nd_ra_router_lifetime = htons(rainfo->lifetime);
975 	ra->nd_ra_reachable = htonl(rainfo->reachabletime);
976 	ra->nd_ra_retransmit = htonl(rainfo->retranstimer);
977 	buf += sizeof(*ra);
978 
979 	if (rainfo->advlinkopt) {
980 		lladdropt_fill(rainfo->sdl, (struct nd_opt_hdr *)buf);
981 		buf += lladdroptlen;
982 	}
983 
984 	if (rainfo->linkmtu) {
985 		ndopt_mtu = (struct nd_opt_mtu *)buf;
986 		ndopt_mtu->nd_opt_mtu_type = ND_OPT_MTU;
987 		ndopt_mtu->nd_opt_mtu_len = 1;
988 		ndopt_mtu->nd_opt_mtu_reserved = 0;
989 		ndopt_mtu->nd_opt_mtu_mtu = htonl(rainfo->linkmtu);
990 		buf += sizeof(struct nd_opt_mtu);
991 	}
992 
993 	for (pfx = rainfo->prefix.next;
994 	     pfx != &rainfo->prefix; pfx = pfx->next) {
995 		u_int32_t vltime, pltime;
996 		struct timeval now;
997 
998 		ndopt_pi = (struct nd_opt_prefix_info *)buf;
999 		ndopt_pi->nd_opt_pi_type = ND_OPT_PREFIX_INFORMATION;
1000 		ndopt_pi->nd_opt_pi_len = 4;
1001 		ndopt_pi->nd_opt_pi_prefix_len = pfx->prefixlen;
1002 		ndopt_pi->nd_opt_pi_flags_reserved = 0;
1003 		if (pfx->onlinkflg)
1004 			ndopt_pi->nd_opt_pi_flags_reserved |=
1005 				ND_OPT_PI_FLAG_ONLINK;
1006 		if (pfx->autoconfflg)
1007 			ndopt_pi->nd_opt_pi_flags_reserved |=
1008 				ND_OPT_PI_FLAG_AUTO;
1009 		if (pfx->timer)
1010 			vltime = 0;
1011 		else {
1012 			if (pfx->vltimeexpire || pfx->pltimeexpire)
1013 				gettimeofday(&now, NULL);
1014 			if (pfx->vltimeexpire == 0)
1015 				vltime = pfx->validlifetime;
1016 			else
1017 				vltime = (pfx->vltimeexpire > now.tv_sec) ?
1018 				    pfx->vltimeexpire - now.tv_sec : 0;
1019 		}
1020 		if (pfx->timer)
1021 			pltime = 0;
1022 		else {
1023 			if (pfx->pltimeexpire == 0)
1024 				pltime = pfx->preflifetime;
1025 			else
1026 				pltime = (pfx->pltimeexpire > now.tv_sec) ?
1027 				    pfx->pltimeexpire - now.tv_sec : 0;
1028 		}
1029 		if (vltime < pltime) {
1030 			/*
1031 			 * this can happen if vltime is decrement but pltime
1032 			 * is not.
1033 			 */
1034 			pltime = vltime;
1035 		}
1036 		ndopt_pi->nd_opt_pi_valid_time = htonl(vltime);
1037 		ndopt_pi->nd_opt_pi_preferred_time = htonl(pltime);
1038 		ndopt_pi->nd_opt_pi_reserved2 = 0;
1039 		ndopt_pi->nd_opt_pi_prefix = pfx->prefix;
1040 
1041 		buf += sizeof(struct nd_opt_prefix_info);
1042 	}
1043 
1044 #ifdef ROUTEINFO
1045 	for (rti = rainfo->route.next; rti != &rainfo->route; rti = rti->next) {
1046 		u_int8_t psize = (rti->prefixlen + 0x3f) >> 6;
1047 
1048 		ndopt_rti = (struct nd_opt_route_info *)buf;
1049 		ndopt_rti->nd_opt_rti_type = ND_OPT_ROUTE_INFO;
1050 		ndopt_rti->nd_opt_rti_len = 1 + psize;
1051 		ndopt_rti->nd_opt_rti_prefixlen = rti->prefixlen;
1052 		ndopt_rti->nd_opt_rti_flags = 0xff & rti->rtpref;
1053 		ndopt_rti->nd_opt_rti_lifetime = htonl(rti->ltime);
1054 		memcpy(ndopt_rti + 1, &rti->prefix, psize * 8);
1055 		buf += sizeof(struct nd_opt_route_info) + psize * 8;
1056 	}
1057 #endif
1058 
1059 	return;
1060 }
1061 
1062 static int
1063 getinet6sysctl(int code)
1064 {
1065 	int mib[] = { CTL_NET, PF_INET6, IPPROTO_IPV6, 0 };
1066 	int value;
1067 	size_t size;
1068 
1069 	mib[3] = code;
1070 	size = sizeof(value);
1071 	if (sysctl(mib, sizeof(mib)/sizeof(mib[0]), &value, &size, NULL, 0)
1072 	    < 0) {
1073 		syslog(LOG_ERR, "<%s>: failed to get ip6 sysctl(%d): %s",
1074 		       __func__, code,
1075 		       strerror(errno));
1076 		return(-1);
1077 	}
1078 	else
1079 		return(value);
1080 }
1081