Fix bug in as-of mount date specification.
[dragonfly.git] / usr.sbin / rtadvd / config.c
blob1bb2191c1b3226aeef97d53a3c057e4e00b7d525
1 /* $KAME: config.c,v 1.37 2001/05/25 07:34:00 itojun Exp $ */
3 /*
4 * Copyright (C) 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
31 * $FreeBSD: src/usr.sbin/rtadvd/config.c,v 1.3.2.5 2003/04/22 09:40:57 suz Exp $
32 * $DragonFly: src/usr.sbin/rtadvd/config.c,v 1.6 2005/12/05 00:56:37 swildner Exp $
35 #include <sys/param.h>
36 #include <sys/ioctl.h>
37 #include <sys/socket.h>
38 #include <sys/time.h>
39 #include <sys/sysctl.h>
41 #include <net/if.h>
42 #if defined(__DragonFly__)
43 #include <net/if_var.h>
44 #endif /* __DragonFly__ */
45 #include <net/route.h>
46 #include <net/if_dl.h>
48 #include <netinet/in.h>
49 #include <netinet/in_var.h>
50 #include <netinet/ip6.h>
51 #include <netinet6/ip6_var.h>
52 #include <netinet/icmp6.h>
53 #ifdef MIP6
54 #include <netinet6/mip6.h>
55 #endif
57 #include <arpa/inet.h>
59 #include <stdio.h>
60 #include <syslog.h>
61 #include <errno.h>
62 #include <string.h>
63 #include <stdlib.h>
64 #if defined(__NetBSD__) || defined(__OpenBSD__)
65 #include <search.h>
66 #endif
67 #include <unistd.h>
68 #include <ifaddrs.h>
70 #include "rtadvd.h"
71 #include "advcap.h"
72 #include "timer.h"
73 #include "if.h"
74 #include "config.h"
76 static time_t prefix_timo = (60 * 120); /* 2 hours.
77 * XXX: should be configurable. */
78 extern struct rainfo *ralist;
80 static struct rtadvd_timer *prefix_timeout(void *);
81 static void makeentry(char *, size_t, int, char *, int);
82 static void get_prefix(struct rainfo *);
83 static int getinet6sysctl(int);
85 void
86 getconfig(char *intface)
88 int stat, pfxs, i;
89 char tbuf[BUFSIZ];
90 struct rainfo *tmp;
91 long val;
92 long long val64;
93 char buf[BUFSIZ];
94 char *bp = buf;
95 char *addr;
96 static int forwarding = -1;
98 #define MUSTHAVE(var, cap) \
99 do { \
100 int t; \
101 if ((t = agetnum(cap)) < 0) { \
102 fprintf(stderr, "rtadvd: need %s for interface %s\n", \
103 cap, intface); \
104 exit(1); \
106 var = t; \
107 } while (0)
108 #define MAYHAVE(var, cap, def) \
109 do { \
110 if ((var = agetnum(cap)) < 0) \
111 var = def; \
112 } while (0)
114 if ((stat = agetent(tbuf, intface)) <= 0) {
115 memset(tbuf, 0, sizeof(tbuf));
116 syslog(LOG_INFO,
117 "<%s> %s isn't defined in the configuration file"
118 " or the configuration file doesn't exist."
119 " Treat it as default",
120 __func__, intface);
123 tmp = (struct rainfo *)malloc(sizeof(*ralist));
124 memset(tmp, 0, sizeof(*tmp));
125 tmp->prefix.next = tmp->prefix.prev = &tmp->prefix;
126 tmp->route.next = tmp->route.prev = &tmp->route;
128 /* check if we are allowed to forward packets (if not determined) */
129 if (forwarding < 0) {
130 if ((forwarding = getinet6sysctl(IPV6CTL_FORWARDING)) < 0)
131 exit(1);
134 /* get interface information */
135 if (agetflag("nolladdr"))
136 tmp->advlinkopt = 0;
137 else
138 tmp->advlinkopt = 1;
139 if (tmp->advlinkopt) {
140 if ((tmp->sdl = if_nametosdl(intface)) == NULL) {
141 syslog(LOG_ERR,
142 "<%s> can't get information of %s",
143 __func__, intface);
144 exit(1);
146 tmp->ifindex = tmp->sdl->sdl_index;
147 } else
148 tmp->ifindex = if_nametoindex(intface);
149 strncpy(tmp->ifname, intface, sizeof(tmp->ifname));
150 if ((tmp->phymtu = if_getmtu(intface)) == 0) {
151 tmp->phymtu = IPV6_MMTU;
152 syslog(LOG_WARNING,
153 "<%s> can't get interface mtu of %s. Treat as %d",
154 __func__, intface, IPV6_MMTU);
158 * set router configuration variables.
160 MAYHAVE(val, "maxinterval", DEF_MAXRTRADVINTERVAL);
161 if (val < MIN_MAXINTERVAL || val > MAX_MAXINTERVAL) {
162 syslog(LOG_ERR,
163 "<%s> maxinterval must be between %e and %u",
164 __func__, MIN_MAXINTERVAL, MAX_MAXINTERVAL);
165 exit(1);
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 must be between %e and %d",
172 __func__,
173 MIN_MININTERVAL,
174 (tmp->maxinterval * 3) / 4);
175 exit(1);
177 tmp->mininterval = (u_int)val;
179 MAYHAVE(val, "chlim", DEF_ADVCURHOPLIMIT);
180 tmp->hoplimit = val & 0xff;
182 MAYHAVE(val, "raflags", 0);
183 tmp->managedflg = val & ND_RA_FLAG_MANAGED;
184 tmp->otherflg = val & ND_RA_FLAG_OTHER;
185 #ifdef MIP6
186 if (mobileip6)
187 tmp->haflg = val & ND_RA_FLAG_HA;
188 #endif
189 #ifndef ND_RA_FLAG_RTPREF_MASK
190 #define ND_RA_FLAG_RTPREF_MASK 0x18 /* 00011000 */
191 #define ND_RA_FLAG_RTPREF_RSV 0x10 /* 00010000 */
192 #endif
193 tmp->rtpref = val & ND_RA_FLAG_RTPREF_MASK;
194 if (tmp->rtpref == ND_RA_FLAG_RTPREF_RSV) {
195 syslog(LOG_ERR, "<%s> invalid router preference on %s",
196 __func__, intface);
197 exit(1);
200 MAYHAVE(val, "rltime", tmp->maxinterval * 3);
201 if (val && (val < tmp->maxinterval || val > MAXROUTERLIFETIME)) {
202 syslog(LOG_ERR,
203 "<%s> router lifetime on %s must be 0 or"
204 " between %d and %d",
205 __func__, intface,
206 tmp->maxinterval, MAXROUTERLIFETIME);
207 exit(1);
210 * Basically, hosts MUST NOT send Router Advertisement messages at any
211 * time (RFC 2461, Section 6.2.3). However, it would sometimes be
212 * useful to allow hosts to advertise some parameters such as prefix
213 * information and link MTU. Thus, we allow hosts to invoke rtadvd
214 * only when router lifetime (on every advertising interface) is
215 * explicitly set zero. (see also the above section)
217 if (val && forwarding == 0) {
218 syslog(LOG_WARNING,
219 "<%s> non zero router lifetime is specified for %s, "
220 "which must not be allowed for hosts.",
221 __func__, intface);
222 exit(1);
224 tmp->lifetime = val & 0xffff;
226 MAYHAVE(val, "rtime", DEF_ADVREACHABLETIME);
227 if (val > MAXREACHABLETIME) {
228 syslog(LOG_ERR,
229 "<%s> reachable time must be no greater than %d",
230 __func__, MAXREACHABLETIME);
231 exit(1);
233 tmp->reachabletime = (u_int32_t)val;
235 MAYHAVE(val64, "retrans", DEF_ADVRETRANSTIMER);
236 if (val64 < 0 || val64 > 0xffffffff) {
237 syslog(LOG_ERR,
238 "<%s> retrans time out of range", __func__);
239 exit(1);
241 tmp->retranstimer = (u_int32_t)val64;
243 #ifndef MIP6
244 if (agetstr("hapref", &bp) || agetstr("hatime", &bp)) {
245 syslog(LOG_ERR,
246 "<%s> mobile-ip6 configuration not supported",
247 __func__);
248 exit(1);
250 #else
251 if (!mobileip6) {
252 if (agetstr("hapref", &bp) || agetstr("hatime", &bp)) {
253 syslog(LOG_ERR,
254 "<%s> mobile-ip6 configuration without "
255 "proper command line option",
256 __func__);
257 exit(1);
259 } else {
260 tmp->hapref = 0;
261 if ((val = agetnum("hapref")) >= 0)
262 tmp->hapref = (int16_t)val;
263 if (tmp->hapref != 0) {
264 tmp->hatime = 0;
265 MUSTHAVE(val, "hatime");
266 tmp->hatime = (u_int16_t)val;
267 if (tmp->hatime <= 0) {
268 syslog(LOG_ERR,
269 "<%s> home agent lifetime must be greater than 0",
270 __func__);
271 exit(1);
275 #endif
277 /* prefix information */
280 * This is an implementation specific parameter to consinder
281 * link propagation delays and poorly synchronized clocks when
282 * checking consistency of advertised lifetimes.
284 MAYHAVE(val, "clockskew", 0);
285 tmp->clockskew = val;
287 if ((pfxs = agetnum("addrs")) < 0) {
288 /* auto configure prefix information */
289 if (agetstr("addr", &bp) || agetstr("addr1", &bp)) {
290 syslog(LOG_ERR,
291 "<%s> conflicting prefix configuration for %s: "
292 "automatic and manual config at the same time",
293 __func__, intface);
294 exit(1);
296 get_prefix(tmp);
298 else {
299 tmp->pfxs = pfxs;
300 for (i = 0; i < pfxs; i++) {
301 struct prefix *pfx;
302 char entbuf[256];
303 int added = (pfxs > 1) ? 1 : 0;
305 /* allocate memory to store prefix information */
306 if ((pfx = malloc(sizeof(struct prefix))) == NULL) {
307 syslog(LOG_ERR,
308 "<%s> can't allocate enough memory",
309 __func__);
310 exit(1);
312 memset(pfx, 0, sizeof(*pfx));
314 /* link into chain */
315 insque(pfx, &tmp->prefix);
316 pfx->rainfo = tmp;
318 pfx->origin = PREFIX_FROM_CONFIG;
320 makeentry(entbuf, sizeof(entbuf), i, "prefixlen",
321 added);
322 MAYHAVE(val, entbuf, 64);
323 if (val < 0 || val > 128) {
324 syslog(LOG_ERR,
325 "<%s> prefixlen out of range",
326 __func__);
327 exit(1);
329 pfx->prefixlen = (int)val;
331 makeentry(entbuf, sizeof(entbuf), i, "pinfoflags",
332 added);
333 #ifdef MIP6
334 if (mobileip6)
336 MAYHAVE(val, entbuf,
337 (ND_OPT_PI_FLAG_ONLINK|ND_OPT_PI_FLAG_AUTO|
338 ND_OPT_PI_FLAG_ROUTER));
339 } else
340 #endif
342 MAYHAVE(val, entbuf,
343 (ND_OPT_PI_FLAG_ONLINK|ND_OPT_PI_FLAG_AUTO));
345 pfx->onlinkflg = val & ND_OPT_PI_FLAG_ONLINK;
346 pfx->autoconfflg = val & ND_OPT_PI_FLAG_AUTO;
347 #ifdef MIP6
348 pfx->routeraddr = val & ND_OPT_PI_FLAG_ROUTER;
349 #endif
351 makeentry(entbuf, sizeof(entbuf), i, "vltime", added);
352 MAYHAVE(val64, entbuf, DEF_ADVVALIDLIFETIME);
353 if (val64 < 0 || val64 > 0xffffffff) {
354 syslog(LOG_ERR,
355 "<%s> vltime out of range",
356 __func__);
357 exit(1);
359 pfx->validlifetime = (u_int32_t)val64;
361 makeentry(entbuf, sizeof(entbuf), i, "vltimedecr",
362 added);
363 if (agetflag(entbuf)) {
364 struct timeval now;
365 gettimeofday(&now, 0);
366 pfx->vltimeexpire =
367 now.tv_sec + pfx->validlifetime;
370 makeentry(entbuf, sizeof(entbuf), i, "pltime", added);
371 MAYHAVE(val64, entbuf, DEF_ADVPREFERREDLIFETIME);
372 if (val64 < 0 || val64 > 0xffffffff) {
373 syslog(LOG_ERR,
374 "<%s> pltime out of range",
375 __func__);
376 exit(1);
378 pfx->preflifetime = (u_int32_t)val64;
380 makeentry(entbuf, sizeof(entbuf), i, "pltimedecr",
381 added);
382 if (agetflag(entbuf)) {
383 struct timeval now;
384 gettimeofday(&now, 0);
385 pfx->pltimeexpire =
386 now.tv_sec + pfx->preflifetime;
389 makeentry(entbuf, sizeof(entbuf), i, "addr", added);
390 addr = (char *)agetstr(entbuf, &bp);
391 if (addr == NULL) {
392 syslog(LOG_ERR,
393 "<%s> need %s as an prefix for "
394 "interface %s",
395 __func__, entbuf, intface);
396 exit(1);
398 if (inet_pton(AF_INET6, addr,
399 &pfx->prefix) != 1) {
400 syslog(LOG_ERR,
401 "<%s> inet_pton failed for %s",
402 __func__, addr);
403 exit(1);
405 if (IN6_IS_ADDR_MULTICAST(&pfx->prefix)) {
406 syslog(LOG_ERR,
407 "<%s> multicast prefix(%s) must "
408 "not be advertised (IF=%s)",
409 __func__, addr, intface);
410 exit(1);
412 if (IN6_IS_ADDR_LINKLOCAL(&pfx->prefix))
413 syslog(LOG_NOTICE,
414 "<%s> link-local prefix(%s) will be"
415 " advertised on %s",
416 __func__, addr, intface);
420 MAYHAVE(val, "mtu", 0);
421 if (val < 0 || val > 0xffffffff) {
422 syslog(LOG_ERR,
423 "<%s> mtu out of range", __func__);
424 exit(1);
426 tmp->linkmtu = (u_int32_t)val;
427 if (tmp->linkmtu == 0) {
428 char *mtustr;
430 if ((mtustr = (char *)agetstr("mtu", &bp)) &&
431 strcmp(mtustr, "auto") == 0)
432 tmp->linkmtu = tmp->phymtu;
434 else if (tmp->linkmtu < IPV6_MMTU || tmp->linkmtu > tmp->phymtu) {
435 syslog(LOG_ERR,
436 "<%s> advertised link mtu must be between"
437 " least MTU and physical link MTU",
438 __func__);
439 exit(1);
442 /* route information */
444 MAYHAVE(val, "routes", 0);
445 if (val < 0 || val > 0xffffffff) {
446 syslog(LOG_ERR,
447 "<%s> number of route information improper", __func__);
448 exit(1);
450 tmp->routes = val;
451 for (i = 0; i < tmp->routes; i++) {
452 struct rtinfo *rti;
453 char entbuf[256];
454 int added = (tmp->routes > 1) ? 1 : 0;
456 /* allocate memory to store prefix information */
457 if ((rti = malloc(sizeof(struct rtinfo))) == NULL) {
458 syslog(LOG_ERR,
459 "<%s> can't allocate enough memory",
460 __func__);
461 exit(1);
463 memset(rti, 0, sizeof(*rti));
465 /* link into chain */
466 insque(rti, &tmp->route);
468 makeentry(entbuf, sizeof(entbuf), i, "rtrplen", added);
469 MAYHAVE(val, entbuf, 64);
470 if (val < 0 || val > 128) {
471 syslog(LOG_ERR,
472 "<%s> prefixlen out of range",
473 __func__);
474 exit(1);
476 rti->prefixlen = (int)val;
478 makeentry(entbuf, sizeof(entbuf), i, "rtrflags", added);
479 MAYHAVE(val, entbuf, 0);
480 rti->rtpref = val & ND_RA_FLAG_RTPREF_MASK;
481 if (rti->rtpref == ND_RA_FLAG_RTPREF_RSV) {
482 syslog(LOG_ERR, "<%s> invalid route preference",
483 __func__);
484 exit(1);
487 makeentry(entbuf, sizeof(entbuf), i, "rtrltime", added);
489 * XXX: since default value of route lifetime is not defined in
490 * draft-draves-route-selection-01.txt, I took the default
491 * value of valid lifetime of prefix as its default.
492 * It need be much considered.
494 MAYHAVE(val64, entbuf, DEF_ADVVALIDLIFETIME);
495 if (val64 < 0 || val64 > 0xffffffff) {
496 syslog(LOG_ERR,
497 "<%s> rtrltime out of range",
498 __func__);
499 exit(1);
501 rti->ltime = (u_int32_t)val64;
503 makeentry(entbuf, sizeof(entbuf), i, "rtrprefix", added);
504 addr = (char *)agetstr(entbuf, &bp);
505 if (addr == NULL) {
506 syslog(LOG_ERR,
507 "<%s> need %s as an route for "
508 "interface %s",
509 __func__, entbuf, intface);
510 exit(1);
512 if (inet_pton(AF_INET6, addr, &rti->prefix) != 1) {
513 syslog(LOG_ERR,
514 "<%s> inet_pton failed for %s",
515 __func__, addr);
516 exit(1);
518 #if 0
520 * XXX: currently there's no restriction in route information
521 * prefix according to draft-draves-route-selection-01.txt,
522 * however I think the similar restriction be necessary.
524 MAYHAVE(val64, entbuf, DEF_ADVVALIDLIFETIME);
525 if (IN6_IS_ADDR_MULTICAST(&rti->prefix)) {
526 syslog(LOG_ERR,
527 "<%s> multicast route (%s) must "
528 "not be advertised (IF=%s)",
529 __func__, addr, intface);
530 exit(1);
532 if (IN6_IS_ADDR_LINKLOCAL(&rti->prefix)) {
533 syslog(LOG_NOTICE,
534 "<%s> link-local route (%s) must "
535 "not be advertised on %s",
536 __func__, addr, intface);
537 exit(1);
539 #endif
542 /* okey */
543 tmp->next = ralist;
544 ralist = tmp;
546 /* construct the sending packet */
547 make_packet(tmp);
549 /* set timer */
550 tmp->timer = rtadvd_add_timer(ra_timeout, ra_timer_update,
551 tmp, tmp);
552 ra_timer_update((void *)tmp, &tmp->timer->tm);
553 rtadvd_set_timer(&tmp->timer->tm, tmp->timer);
556 static void
557 get_prefix(struct rainfo *rai)
559 struct ifaddrs *ifap, *ifa;
560 struct prefix *pp;
561 struct in6_addr *a;
562 u_char *p, *ep, *m, *lim;
563 u_char ntopbuf[INET6_ADDRSTRLEN];
565 if (getifaddrs(&ifap) < 0) {
566 syslog(LOG_ERR,
567 "<%s> can't get interface addresses",
568 __func__);
569 exit(1);
571 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
572 int plen;
574 if (strcmp(ifa->ifa_name, rai->ifname) != 0)
575 continue;
576 if (ifa->ifa_addr->sa_family != AF_INET6)
577 continue;
578 a = &((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr;
579 if (IN6_IS_ADDR_LINKLOCAL(a))
580 continue;
582 /* get prefix length */
583 m = (u_char *)&((struct sockaddr_in6 *)ifa->ifa_netmask)->sin6_addr;
584 lim = (u_char *)(ifa->ifa_netmask) + ifa->ifa_netmask->sa_len;
585 plen = prefixlen(m, lim);
586 if (plen < 0 || plen > 128) {
587 syslog(LOG_ERR, "<%s> failed to get prefixlen "
588 "or prefix is invalid",
589 __func__);
590 exit(1);
592 if (find_prefix(rai, a, plen)) {
593 /* ignore a duplicated prefix. */
594 continue;
597 /* allocate memory to store prefix info. */
598 if ((pp = malloc(sizeof(*pp))) == NULL) {
599 syslog(LOG_ERR,
600 "<%s> can't get allocate buffer for prefix",
601 __func__);
602 exit(1);
604 memset(pp, 0, sizeof(*pp));
606 /* set prefix, sweep bits outside of prefixlen */
607 pp->prefixlen = plen;
608 memcpy(&pp->prefix, a, sizeof(*a));
609 p = (u_char *)&pp->prefix;
610 ep = (u_char *)(&pp->prefix + 1);
611 while (m < lim)
612 *p++ &= *m++;
613 while (p < ep)
614 *p++ = 0x00;
616 if (!inet_ntop(AF_INET6, &pp->prefix, ntopbuf,
617 sizeof(ntopbuf))) {
618 syslog(LOG_ERR, "<%s> inet_ntop failed", __func__);
619 exit(1);
621 syslog(LOG_DEBUG,
622 "<%s> add %s/%d to prefix list on %s",
623 __func__, ntopbuf, pp->prefixlen, rai->ifname);
625 /* set other fields with protocol defaults */
626 pp->validlifetime = DEF_ADVVALIDLIFETIME;
627 pp->preflifetime = DEF_ADVPREFERREDLIFETIME;
628 pp->onlinkflg = 1;
629 pp->autoconfflg = 1;
630 pp->origin = PREFIX_FROM_KERNEL;
632 /* link into chain */
633 insque(pp, &rai->prefix);
635 /* counter increment */
636 rai->pfxs++;
639 freeifaddrs(ifap);
642 static void
643 makeentry(char *buf, size_t len, int id, char *string, int add)
645 char *ep = buf + len;
647 strcpy(buf, string);
648 if (add) {
649 char *cp;
651 cp = (char *)index(buf, '\0');
652 snprintf(cp, ep - cp, "%d", id);
657 * Add a prefix to the list of specified interface and reconstruct
658 * the outgoing packet.
659 * The prefix must not be in the list.
660 * XXX: other parameter of the prefix(e.g. lifetime) shoule be
661 * able to be specified.
663 static void
664 add_prefix(struct rainfo *rai, struct in6_prefixreq *ipr)
666 struct prefix *prefix;
667 u_char ntopbuf[INET6_ADDRSTRLEN];
669 if ((prefix = malloc(sizeof(*prefix))) == NULL) {
670 syslog(LOG_ERR, "<%s> memory allocation failed",
671 __func__);
672 return; /* XXX: error or exit? */
674 memset(prefix, 0, sizeof(*prefix));
675 prefix->prefix = ipr->ipr_prefix.sin6_addr;
676 prefix->prefixlen = ipr->ipr_plen;
677 prefix->validlifetime = ipr->ipr_vltime;
678 prefix->preflifetime = ipr->ipr_pltime;
679 prefix->onlinkflg = ipr->ipr_raf_onlink;
680 prefix->autoconfflg = ipr->ipr_raf_auto;
681 prefix->origin = PREFIX_FROM_DYNAMIC;
683 insque(prefix, &rai->prefix);
684 prefix->rainfo = rai;
686 syslog(LOG_DEBUG, "<%s> new prefix %s/%d was added on %s",
687 __func__, inet_ntop(AF_INET6, &ipr->ipr_prefix.sin6_addr,
688 ntopbuf, INET6_ADDRSTRLEN),
689 ipr->ipr_plen, rai->ifname);
691 /* free the previous packet */
692 free(rai->ra_data);
693 rai->ra_data = NULL;
695 /* reconstruct the packet */
696 rai->pfxs++;
697 make_packet(rai);
700 * reset the timer so that the new prefix will be advertised quickly.
702 rai->initcounter = 0;
703 ra_timer_update((void *)rai, &rai->timer->tm);
704 rtadvd_set_timer(&rai->timer->tm, rai->timer);
708 * Delete a prefix to the list of specified interface and reconstruct
709 * the outgoing packet.
710 * The prefix must be in the list.
712 void
713 delete_prefix(struct prefix *prefix)
715 u_char ntopbuf[INET6_ADDRSTRLEN];
716 struct rainfo *rai = prefix->rainfo;
718 remque(prefix);
719 syslog(LOG_DEBUG, "<%s> prefix %s/%d was deleted on %s",
720 __func__, inet_ntop(AF_INET6, &prefix->prefix,
721 ntopbuf, INET6_ADDRSTRLEN),
722 prefix->prefixlen, rai->ifname);
723 if (prefix->timer)
724 rtadvd_remove_timer(&prefix->timer);
725 free(prefix);
726 rai->pfxs--;
729 void
730 invalidate_prefix(struct prefix *prefix)
732 u_char ntopbuf[INET6_ADDRSTRLEN];
733 struct timeval timo;
734 struct rainfo *rai = prefix->rainfo;
736 if (prefix->timer) { /* sanity check */
737 syslog(LOG_ERR,
738 "<%s> assumption failure: timer already exists",
739 __func__);
740 exit(1);
743 syslog(LOG_DEBUG, "<%s> prefix %s/%d was invalidated on %s, "
744 "will expire in %ld seconds", __func__,
745 inet_ntop(AF_INET6, &prefix->prefix, ntopbuf, INET6_ADDRSTRLEN),
746 prefix->prefixlen, rai->ifname, (long)prefix_timo);
748 /* set the expiration timer */
749 prefix->timer = rtadvd_add_timer(prefix_timeout, NULL, prefix, NULL);
750 if (prefix->timer == NULL) {
751 syslog(LOG_ERR, "<%s> failed to add a timer for a prefix. "
752 "remove the prefix", __func__);
753 delete_prefix(prefix);
755 timo.tv_sec = prefix_timo;
756 timo.tv_usec = 0;
757 rtadvd_set_timer(&timo, prefix->timer);
760 static struct rtadvd_timer *
761 prefix_timeout(void *arg)
763 struct prefix *prefix = (struct prefix *)arg;
765 delete_prefix(prefix);
767 return(NULL);
770 void
771 update_prefix(struct prefix * prefix)
773 u_char ntopbuf[INET6_ADDRSTRLEN];
774 struct rainfo *rai = prefix->rainfo;
776 if (prefix->timer == NULL) { /* sanity check */
777 syslog(LOG_ERR,
778 "<%s> assumption failure: timer does not exist",
779 __func__);
780 exit(1);
783 syslog(LOG_DEBUG, "<%s> prefix %s/%d was re-enabled on %s",
784 __func__, inet_ntop(AF_INET6, &prefix->prefix, ntopbuf,
785 INET6_ADDRSTRLEN), prefix->prefixlen, rai->ifname);
787 /* stop the expiration timer */
788 rtadvd_remove_timer(&prefix->timer);
792 * Try to get an in6_prefixreq contents for a prefix which matches
793 * ipr->ipr_prefix and ipr->ipr_plen and belongs to
794 * the interface whose name is ipr->ipr_name[].
796 static int
797 init_prefix(struct in6_prefixreq *ipr)
799 #if 0
800 int s;
802 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
803 syslog(LOG_ERR, "<%s> socket: %s", __func__,
804 strerror(errno));
805 exit(1);
808 if (ioctl(s, SIOCGIFPREFIX_IN6, (caddr_t)ipr) < 0) {
809 syslog(LOG_INFO, "<%s> ioctl:SIOCGIFPREFIX %s", __func__,
810 strerror(errno));
812 ipr->ipr_vltime = DEF_ADVVALIDLIFETIME;
813 ipr->ipr_pltime = DEF_ADVPREFERREDLIFETIME;
814 ipr->ipr_raf_onlink = 1;
815 ipr->ipr_raf_auto = 1;
816 /* omit other field initialization */
818 else if (ipr->ipr_origin < PR_ORIG_RR) {
819 u_char ntopbuf[INET6_ADDRSTRLEN];
821 syslog(LOG_WARNING, "<%s> Added prefix(%s)'s origin %d is"
822 "lower than PR_ORIG_RR(router renumbering)."
823 "This should not happen if I am router", __func__,
824 inet_ntop(AF_INET6, &ipr->ipr_prefix.sin6_addr, ntopbuf,
825 sizeof(ntopbuf)), ipr->ipr_origin);
826 close(s);
827 return 1;
830 close(s);
831 return 0;
832 #else
833 ipr->ipr_vltime = DEF_ADVVALIDLIFETIME;
834 ipr->ipr_pltime = DEF_ADVPREFERREDLIFETIME;
835 ipr->ipr_raf_onlink = 1;
836 ipr->ipr_raf_auto = 1;
837 return 0;
838 #endif
841 void
842 make_prefix(struct rainfo *rai, int ifindex, struct in6_addr *addr, int plen)
844 struct in6_prefixreq ipr;
846 memset(&ipr, 0, sizeof(ipr));
847 if (if_indextoname(ifindex, ipr.ipr_name) == NULL) {
848 syslog(LOG_ERR, "<%s> Prefix added interface No.%d doesn't"
849 "exist. This should not happen! %s", __func__,
850 ifindex, strerror(errno));
851 exit(1);
853 ipr.ipr_prefix.sin6_len = sizeof(ipr.ipr_prefix);
854 ipr.ipr_prefix.sin6_family = AF_INET6;
855 ipr.ipr_prefix.sin6_addr = *addr;
856 ipr.ipr_plen = plen;
858 if (init_prefix(&ipr))
859 return; /* init failed by some error */
860 add_prefix(rai, &ipr);
863 void
864 make_packet(struct rainfo *rainfo)
866 size_t packlen, lladdroptlen = 0;
867 char *buf;
868 struct nd_router_advert *ra;
869 struct nd_opt_prefix_info *ndopt_pi;
870 struct nd_opt_mtu *ndopt_mtu;
871 #ifdef MIP6
872 struct nd_opt_advinterval *ndopt_advint;
873 struct nd_opt_homeagent_info *ndopt_hai;
874 #endif
875 struct nd_opt_route_info *ndopt_rti;
876 struct prefix *pfx;
877 struct rtinfo *rti;
879 /* calculate total length */
880 packlen = sizeof(struct nd_router_advert);
881 if (rainfo->advlinkopt) {
882 if ((lladdroptlen = lladdropt_length(rainfo->sdl)) == 0) {
883 syslog(LOG_INFO,
884 "<%s> link-layer address option has"
885 " null length on %s."
886 " Treat as not included.",
887 __func__, rainfo->ifname);
888 rainfo->advlinkopt = 0;
890 packlen += lladdroptlen;
892 if (rainfo->pfxs)
893 packlen += sizeof(struct nd_opt_prefix_info) * rainfo->pfxs;
894 if (rainfo->linkmtu)
895 packlen += sizeof(struct nd_opt_mtu);
896 #ifdef MIP6
897 if (mobileip6 && rainfo->maxinterval)
898 packlen += sizeof(struct nd_opt_advinterval);
899 if (mobileip6 && rainfo->hatime)
900 packlen += sizeof(struct nd_opt_homeagent_info);
901 #endif
902 #ifdef ND_OPT_ROUTE_INFO
903 for (rti = rainfo->route.next; rti != &rainfo->route; rti = rti->next)
904 packlen += sizeof(struct nd_opt_route_info) +
905 ((rti->prefixlen + 0x3f) >> 6) * 8;
906 #endif
908 /* allocate memory for the packet */
909 if ((buf = malloc(packlen)) == NULL) {
910 syslog(LOG_ERR,
911 "<%s> can't get enough memory for an RA packet",
912 __func__);
913 exit(1);
915 if (rainfo->ra_data) {
916 /* free the previous packet */
917 free(rainfo->ra_data);
918 rainfo->ra_data = NULL;
920 rainfo->ra_data = buf;
921 /* XXX: what if packlen > 576? */
922 rainfo->ra_datalen = packlen;
925 * construct the packet
927 ra = (struct nd_router_advert *)buf;
928 ra->nd_ra_type = ND_ROUTER_ADVERT;
929 ra->nd_ra_code = 0;
930 ra->nd_ra_cksum = 0;
931 ra->nd_ra_curhoplimit = (u_int8_t)(0xff & rainfo->hoplimit);
932 ra->nd_ra_flags_reserved = 0; /* just in case */
934 * XXX: the router preference field, which is a 2-bit field, should be
935 * initialized before other fields.
937 ra->nd_ra_flags_reserved = 0xff & rainfo->rtpref;
938 ra->nd_ra_flags_reserved |=
939 rainfo->managedflg ? ND_RA_FLAG_MANAGED : 0;
940 ra->nd_ra_flags_reserved |=
941 rainfo->otherflg ? ND_RA_FLAG_OTHER : 0;
942 #ifdef MIP6
943 ra->nd_ra_flags_reserved |=
944 rainfo->haflg ? ND_RA_FLAG_HA : 0;
945 #endif
946 ra->nd_ra_router_lifetime = htons(rainfo->lifetime);
947 ra->nd_ra_reachable = htonl(rainfo->reachabletime);
948 ra->nd_ra_retransmit = htonl(rainfo->retranstimer);
949 buf += sizeof(*ra);
951 if (rainfo->advlinkopt) {
952 lladdropt_fill(rainfo->sdl, (struct nd_opt_hdr *)buf);
953 buf += lladdroptlen;
956 if (rainfo->linkmtu) {
957 ndopt_mtu = (struct nd_opt_mtu *)buf;
958 ndopt_mtu->nd_opt_mtu_type = ND_OPT_MTU;
959 ndopt_mtu->nd_opt_mtu_len = 1;
960 ndopt_mtu->nd_opt_mtu_reserved = 0;
961 ndopt_mtu->nd_opt_mtu_mtu = htonl(rainfo->linkmtu);
962 buf += sizeof(struct nd_opt_mtu);
965 #ifdef MIP6
966 if (mobileip6 && rainfo->maxinterval) {
967 ndopt_advint = (struct nd_opt_advinterval *)buf;
968 ndopt_advint->nd_opt_adv_type = ND_OPT_ADVINTERVAL;
969 ndopt_advint->nd_opt_adv_len = 1;
970 ndopt_advint->nd_opt_adv_reserved = 0;
971 ndopt_advint->nd_opt_adv_interval = htonl(rainfo->maxinterval *
972 1000);
973 buf += sizeof(struct nd_opt_advinterval);
975 #endif
977 #ifdef MIP6
978 if (rainfo->hatime) {
979 ndopt_hai = (struct nd_opt_homeagent_info *)buf;
980 ndopt_hai->nd_opt_hai_type = ND_OPT_HOMEAGENT_INFO;
981 ndopt_hai->nd_opt_hai_len = 1;
982 ndopt_hai->nd_opt_hai_reserved = 0;
983 ndopt_hai->nd_opt_hai_preference = htons(rainfo->hapref);
984 ndopt_hai->nd_opt_hai_lifetime = htons(rainfo->hatime);
985 buf += sizeof(struct nd_opt_homeagent_info);
987 #endif
989 for (pfx = rainfo->prefix.next;
990 pfx != &rainfo->prefix; pfx = pfx->next) {
991 u_int32_t vltime, pltime;
992 struct timeval now;
994 ndopt_pi = (struct nd_opt_prefix_info *)buf;
995 ndopt_pi->nd_opt_pi_type = ND_OPT_PREFIX_INFORMATION;
996 ndopt_pi->nd_opt_pi_len = 4;
997 ndopt_pi->nd_opt_pi_prefix_len = pfx->prefixlen;
998 ndopt_pi->nd_opt_pi_flags_reserved = 0;
999 if (pfx->onlinkflg)
1000 ndopt_pi->nd_opt_pi_flags_reserved |=
1001 ND_OPT_PI_FLAG_ONLINK;
1002 if (pfx->autoconfflg)
1003 ndopt_pi->nd_opt_pi_flags_reserved |=
1004 ND_OPT_PI_FLAG_AUTO;
1005 #ifdef MIP6
1006 if (pfx->routeraddr)
1007 ndopt_pi->nd_opt_pi_flags_reserved |=
1008 ND_OPT_PI_FLAG_ROUTER;
1009 #endif
1010 if (pfx->timer)
1011 vltime = 0;
1012 else {
1013 if (pfx->vltimeexpire || pfx->pltimeexpire)
1014 gettimeofday(&now, NULL);
1015 if (pfx->vltimeexpire == 0)
1016 vltime = pfx->validlifetime;
1017 else
1018 vltime = (pfx->vltimeexpire > now.tv_sec) ?
1019 pfx->vltimeexpire - now.tv_sec : 0;
1021 if (pfx->timer)
1022 pltime = 0;
1023 else {
1024 if (pfx->pltimeexpire == 0)
1025 pltime = pfx->preflifetime;
1026 else
1027 pltime = (pfx->pltimeexpire > now.tv_sec) ?
1028 pfx->pltimeexpire - now.tv_sec : 0;
1030 if (vltime < pltime) {
1032 * this can happen if vltime is decrement but pltime
1033 * is not.
1035 pltime = vltime;
1037 ndopt_pi->nd_opt_pi_valid_time = htonl(vltime);
1038 ndopt_pi->nd_opt_pi_preferred_time = htonl(pltime);
1039 ndopt_pi->nd_opt_pi_reserved2 = 0;
1040 ndopt_pi->nd_opt_pi_prefix = pfx->prefix;
1042 buf += sizeof(struct nd_opt_prefix_info);
1045 #ifdef ND_OPT_ROUTE_INFO
1046 for (rti = rainfo->route.next; rti != &rainfo->route; rti = rti->next) {
1047 u_int8_t psize = (rti->prefixlen + 0x3f) >> 6;
1049 ndopt_rti = (struct nd_opt_route_info *)buf;
1050 ndopt_rti->nd_opt_rti_type = ND_OPT_ROUTE_INFO;
1051 ndopt_rti->nd_opt_rti_len = 1 + psize;
1052 ndopt_rti->nd_opt_rti_prefixlen = rti->prefixlen;
1053 ndopt_rti->nd_opt_rti_flags = 0xff & rti->rtpref;
1054 ndopt_rti->nd_opt_rti_lifetime = htonl(rti->ltime);
1055 memcpy(ndopt_rti + 1, &rti->prefix, psize * 8);
1056 buf += sizeof(struct nd_opt_route_info) + psize * 8;
1058 #endif
1060 return;
1063 static int
1064 getinet6sysctl(int code)
1066 int mib[] = { CTL_NET, PF_INET6, IPPROTO_IPV6, 0 };
1067 int value;
1068 size_t size;
1070 mib[3] = code;
1071 size = sizeof(value);
1072 if (sysctl(mib, sizeof(mib)/sizeof(mib[0]), &value, &size, NULL, 0)
1073 < 0) {
1074 syslog(LOG_ERR, "<%s>: failed to get ip6 sysctl(%d): %s",
1075 __func__, code,
1076 strerror(errno));
1077 return(-1);
1079 else
1080 return(value);