2 /* $KAME: config.c,v 1.84 2003/08/05 12:34:23 itojun Exp $ */
5 * Copyright (C) 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
33 #include <sys/param.h>
34 #include <sys/ioctl.h>
35 #include <sys/socket.h>
37 #include <sys/sysctl.h>
40 #include <net/if_var.h>
41 #include <net/route.h>
42 #include <net/if_dl.h>
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>
51 #include <arpa/inet.h>
68 static time_t prefix_timo
= (60 * 120); /* 2 hours.
69 * XXX: should be configurable. */
70 extern struct rainfo
*ralist
;
72 static struct rtadvd_timer
*prefix_timeout(void *);
73 static void makeentry(char *, size_t, int, char *);
74 static int getinet6sysctl(int);
88 static int forwarding
= -1;
90 #define MUSTHAVE(var, cap) \
93 if ((t = agetnum(cap)) < 0) { \
94 fprintf(stderr, "rtadvd: need %s for interface %s\n", \
100 #define MAYHAVE(var, cap, def) \
102 if ((var = agetnum(cap)) < 0) \
106 if ((stat
= agetent(tbuf
, intface
)) <= 0) {
107 memset(tbuf
, 0, sizeof(tbuf
));
109 "<%s> %s isn't defined in the configuration file"
110 " or the configuration file doesn't exist."
111 " Treat it as default",
115 tmp
= (struct rainfo
*)malloc(sizeof(*ralist
));
117 syslog(LOG_INFO
, "<%s> %s: can't allocate enough memory",
121 memset(tmp
, 0, sizeof(*tmp
));
122 tmp
->prefix
.next
= tmp
->prefix
.prev
= &tmp
->prefix
;
124 tmp
->route
.next
= tmp
->route
.prev
= &tmp
->route
;
127 /* check if we are allowed to forward packets (if not determined) */
128 if (forwarding
< 0) {
129 if ((forwarding
= getinet6sysctl(IPV6CTL_FORWARDING
)) < 0)
133 /* get interface information */
134 if (agetflag("nolladdr"))
138 if (tmp
->advlinkopt
) {
139 if ((tmp
->sdl
= if_nametosdl(intface
)) == NULL
) {
141 "<%s> can't get information of %s",
145 tmp
->ifindex
= tmp
->sdl
->sdl_index
;
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
;
152 "<%s> can't get interface mtu of %s. Treat as %d",
153 __func__
, intface
, IPV6_MMTU
);
157 * set router configuration variables.
159 MAYHAVE(val
, "maxinterval", DEF_MAXRTRADVINTERVAL
);
160 if (val
< MIN_MAXINTERVAL
|| val
> MAX_MAXINTERVAL
) {
162 "<%s> maxinterval (%ld) on %s is invalid "
163 "(must be between %u and %u)", __func__
, val
,
164 intface
, MIN_MAXINTERVAL
, MAX_MAXINTERVAL
);
167 tmp
->maxinterval
= (u_int
)val
;
168 MAYHAVE(val
, "mininterval", tmp
->maxinterval
/3);
169 if (val
< MIN_MININTERVAL
|| val
> (tmp
->maxinterval
* 3) / 4) {
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);
177 tmp
->mininterval
= (u_int
)val
;
179 MAYHAVE(val
, "chlim", DEF_ADVCURHOPLIMIT
);
180 tmp
->hoplimit
= val
& 0xff;
182 if ((flagstr
= (char *)agetstr("raflags", &bp
))) {
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__
);
196 val
|= ND_RA_FLAG_RTPREF_LOW
;
199 MAYHAVE(val
, "raflags", 0);
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 */
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
);
214 MAYHAVE(val
, "rltime", tmp
->maxinterval
* 3);
215 if (val
&& (val
< tmp
->maxinterval
|| val
> MAXROUTERLIFETIME
)) {
217 "<%s> router lifetime (%ld) on %s is invalid "
218 "(must be 0 or between %d and %d)",
219 __func__
, val
, intface
,
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)
232 if (val
&& forwarding
== 0) {
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.",
240 tmp
->lifetime
= val
& 0xffff;
242 MAYHAVE(val
, "rtime", DEF_ADVREACHABLETIME
);
243 if (val
< 0 || val
> MAXREACHABLETIME
) {
245 "<%s> reachable time (%ld) on %s is invalid "
246 "(must be no greater than %d)",
247 __func__
, val
, intface
, MAXREACHABLETIME
);
250 tmp
->reachabletime
= (u_int32_t
)val
;
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
);
258 tmp
->retranstimer
= (u_int32_t
)val64
;
260 if (agetnum("hapref") != -1 || agetnum("hatime") != -1) {
262 "<%s> mobile-ip6 configuration not supported",
266 /* prefix information */
269 * This is an implementation specific parameter to consider
270 * link propagation delays and poorly synchronized clocks when
271 * checking consistency of advertised lifetimes.
273 MAYHAVE(val
, "clockskew", 0);
274 tmp
->clockskew
= val
;
277 for (i
= -1; i
< MAXPREFIX
; i
++) {
281 makeentry(entbuf
, sizeof(entbuf
), i
, "addr");
282 addr
= (char *)agetstr(entbuf
, &bp
);
286 /* allocate memory to store prefix information */
287 if ((pfx
= malloc(sizeof(struct prefix
))) == NULL
) {
289 "<%s> can't allocate enough memory",
293 memset(pfx
, 0, sizeof(*pfx
));
295 /* link into chain */
296 insque(pfx
, &tmp
->prefix
);
300 pfx
->origin
= PREFIX_FROM_CONFIG
;
302 if (inet_pton(AF_INET6
, addr
, &pfx
->prefix
) != 1) {
304 "<%s> inet_pton failed for %s",
308 if (IN6_IS_ADDR_MULTICAST(&pfx
->prefix
)) {
310 "<%s> multicast prefix (%s) must "
311 "not be advertised on %s",
312 __func__
, addr
, intface
);
315 if (IN6_IS_ADDR_LINKLOCAL(&pfx
->prefix
))
317 "<%s> link-local prefix (%s) will be"
319 __func__
, addr
, intface
);
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
);
329 pfx
->prefixlen
= (int)val
;
331 makeentry(entbuf
, sizeof(entbuf
), i
, "pinfoflags");
332 if ((flagstr
= (char *)agetstr(entbuf
, &bp
))) {
334 if (strchr(flagstr
, 'l'))
335 val
|= ND_OPT_PI_FLAG_ONLINK
;
336 if (strchr(flagstr
, 'a'))
337 val
|= ND_OPT_PI_FLAG_AUTO
;
340 (ND_OPT_PI_FLAG_ONLINK
|ND_OPT_PI_FLAG_AUTO
));
342 pfx
->onlinkflg
= val
& ND_OPT_PI_FLAG_ONLINK
;
343 pfx
->autoconfflg
= val
& ND_OPT_PI_FLAG_AUTO
;
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
);
354 pfx
->validlifetime
= (u_int32_t
)val64
;
356 makeentry(entbuf
, sizeof(entbuf
), i
, "vltimedecr");
357 if (agetflag(entbuf
)) {
359 gettimeofday(&now
, 0);
361 now
.tv_sec
+ pfx
->validlifetime
;
364 makeentry(entbuf
, sizeof(entbuf
), i
, "pltime");
365 MAYHAVE(val64
, entbuf
, DEF_ADVPREFERREDLIFETIME
);
366 if (val64
< 0 || val64
> 0xffffffff) {
368 "<%s> pltime (%lld) for %s/%d on %s "
370 __func__
, (long long)val64
,
371 addr
, pfx
->prefixlen
, intface
);
374 pfx
->preflifetime
= (u_int32_t
)val64
;
376 makeentry(entbuf
, sizeof(entbuf
), i
, "pltimedecr");
377 if (agetflag(entbuf
)) {
379 gettimeofday(&now
, 0);
381 now
.tv_sec
+ pfx
->preflifetime
;
387 MAYHAVE(val
, "mtu", 0);
388 if (val
< 0 || val
> 0xffffffff) {
390 "<%s> mtu (%ld) on %s out of range",
391 __func__
, val
, intface
);
394 tmp
->linkmtu
= (u_int32_t
)val
;
395 if (tmp
->linkmtu
== 0) {
398 if ((mtustr
= (char *)agetstr("mtu", &bp
)) &&
399 strcmp(mtustr
, "auto") == 0)
400 tmp
->linkmtu
= tmp
->phymtu
;
402 else if (tmp
->linkmtu
< IPV6_MMTU
|| tmp
->linkmtu
> tmp
->phymtu
) {
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
);
411 #ifdef SIOCSIFINFO_IN6
413 struct in6_ndireq ndi
;
416 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0) {
417 syslog(LOG_ERR
, "<%s> socket: %s", __func__
,
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
));
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
));
440 /* route information */
443 for (i
= -1; i
< MAXROUTE
; i
++) {
445 char entbuf
[256], oentbuf
[256];
447 makeentry(entbuf
, sizeof(entbuf
), i
, "rtprefix");
448 addr
= (char *)agetstr(entbuf
, &bp
);
450 makeentry(oentbuf
, sizeof(oentbuf
), i
, "rtrprefix");
451 addr
= (char *)agetstr(oentbuf
, &bp
);
453 fprintf(stderr
, "%s was obsoleted. Use %s.\n",
460 /* allocate memory to store prefix information */
461 if ((rti
= malloc(sizeof(struct rtinfo
))) == NULL
) {
463 "<%s> can't allocate enough memory",
467 memset(rti
, 0, sizeof(*rti
));
469 /* link into chain */
470 insque(rti
, &tmp
->route
);
473 if (inet_pton(AF_INET6
, addr
, &rti
->prefix
) != 1) {
474 syslog(LOG_ERR
, "<%s> inet_pton failed for %s",
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.
485 MAYHAVE(val64
, entbuf
, DEF_ADVVALIDLIFETIME
);
486 if (IN6_IS_ADDR_MULTICAST(&rti
->prefix
)) {
488 "<%s> multicast route (%s) must "
489 "not be advertised on %s",
490 __func__
, addr
, intface
);
493 if (IN6_IS_ADDR_LINKLOCAL(&rti
->prefix
)) {
495 "<%s> link-local route (%s) will "
496 "be advertised on %s",
497 __func__
, addr
, intface
);
502 makeentry(entbuf
, sizeof(entbuf
), i
, "rtplen");
503 /* XXX: 256 is a magic number for compatibility check. */
504 MAYHAVE(val
, entbuf
, 256);
506 makeentry(oentbuf
, sizeof(oentbuf
), i
, "rtrplen");
507 MAYHAVE(val
, oentbuf
, 256);
509 fprintf(stderr
, "%s was obsoleted. Use %s.\n",
514 if (val
< 0 || val
> 128) {
515 syslog(LOG_ERR
, "<%s> prefixlen (%ld) for %s on %s "
517 __func__
, val
, addr
, intface
);
520 rti
->prefixlen
= (int)val
;
522 makeentry(entbuf
, sizeof(entbuf
), i
, "rtflags");
523 if ((flagstr
= (char *)agetstr(entbuf
, &bp
))) {
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
)) {
530 "<%s> the \'h\' and \'l\' route"
531 " preferences are exclusive",
535 val
|= ND_RA_FLAG_RTPREF_LOW
;
538 MAYHAVE(val
, entbuf
, 256); /* XXX */
540 makeentry(oentbuf
, sizeof(oentbuf
), i
, "rtrflags");
541 MAYHAVE(val
, oentbuf
, 256);
543 fprintf(stderr
, "%s was obsoleted. Use %s.\n",
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) "
552 __func__
, rti
->rtpref
, addr
,
553 rti
->prefixlen
, intface
);
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.
563 makeentry(entbuf
, sizeof(entbuf
), i
, "rtltime");
564 MAYHAVE(val64
, entbuf
, -1);
566 makeentry(oentbuf
, sizeof(oentbuf
), i
, "rtrltime");
567 MAYHAVE(val64
, oentbuf
, -1);
569 fprintf(stderr
, "%s was obsoleted. Use %s.\n",
572 fprintf(stderr
, "%s should be specified "
573 "for interface %s.\n",
575 val64
= tmp
->lifetime
;
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
);
584 rti
->ltime
= (u_int32_t
)val64
;
592 /* construct the sending packet */
596 tmp
->timer
= rtadvd_add_timer(ra_timeout
, ra_timer_update
,
598 ra_timer_update((void *)tmp
, &tmp
->timer
->tm
);
599 rtadvd_set_timer(&tmp
->timer
->tm
, tmp
->timer
);
603 get_prefix(struct rainfo
*rai
)
605 struct ifaddrs
*ifap
, *ifa
;
608 u_char
*p
, *ep
, *m
, *lim
;
609 u_char ntopbuf
[INET6_ADDRSTRLEN
];
611 if (getifaddrs(&ifap
) < 0) {
613 "<%s> can't get interface addresses",
618 for (ifa
= ifap
; ifa
; ifa
= ifa
->ifa_next
) {
621 if (strcmp(ifa
->ifa_name
, rai
->ifname
) != 0)
623 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
625 a
= &((struct sockaddr_in6
*)ifa
->ifa_addr
)->sin6_addr
;
626 if (IN6_IS_ADDR_LINKLOCAL(a
))
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",
638 if (plen
== 128) /* XXX */
640 if (find_prefix(rai
, a
, plen
)) {
641 /* ignore a duplicated prefix. */
645 /* allocate memory to store prefix info. */
646 if ((pp
= malloc(sizeof(*pp
))) == NULL
) {
648 "<%s> can't get allocate buffer for prefix",
652 memset(pp
, 0, sizeof(*pp
));
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
)
663 if (!inet_ntop(AF_INET6
, &pp
->prefix
, ntopbuf
,
665 syslog(LOG_ERR
, "<%s> inet_ntop failed", __func__
);
669 "<%s> add %s/%d to prefix list on %s",
670 __func__
, ntopbuf
, pp
->prefixlen
, rai
->ifname
);
672 /* set other fields with protocol defaults */
673 pp
->validlifetime
= DEF_ADVVALIDLIFETIME
;
674 pp
->preflifetime
= DEF_ADVPREFERREDLIFETIME
;
677 pp
->origin
= PREFIX_FROM_KERNEL
;
680 /* link into chain */
681 insque(pp
, &rai
->prefix
);
683 /* counter increment */
691 makeentry(buf
, len
, id
, string
)
699 strlcpy(buf
, string
, len
);
701 snprintf(buf
, len
, "%s%d", string
, id
);
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.
712 add_prefix(struct rainfo
*rai
, struct in6_prefixreq
*ipr
)
714 struct prefix
*prefix
;
715 u_char ntopbuf
[INET6_ADDRSTRLEN
];
717 if ((prefix
= malloc(sizeof(*prefix
))) == NULL
) {
718 syslog(LOG_ERR
, "<%s> memory allocation failed",
720 return; /* XXX: error or exit? */
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
;
731 insque(prefix
, &rai
->prefix
);
732 prefix
->rainfo
= rai
;
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
);
739 /* free the previous packet */
743 /* reconstruct the packet */
749 * Delete a prefix to the list of specified interface and reconstruct
750 * the outgoing packet.
751 * The prefix must be in the list.
754 delete_prefix(struct prefix
*prefix
)
756 u_char ntopbuf
[INET6_ADDRSTRLEN
];
757 struct rainfo
*rai
= prefix
->rainfo
;
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
);
765 rtadvd_remove_timer(&prefix
->timer
);
771 invalidate_prefix(struct prefix
*prefix
)
773 u_char ntopbuf
[INET6_ADDRSTRLEN
];
775 struct rainfo
*rai
= prefix
->rainfo
;
777 if (prefix
->timer
) { /* sanity check */
779 "<%s> assumption failure: timer already exists",
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
);
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
);
796 timo
.tv_sec
= prefix_timo
;
798 rtadvd_set_timer(&timo
, prefix
->timer
);
801 static struct rtadvd_timer
*
802 prefix_timeout(void *arg
)
804 struct prefix
*prefix
= (struct prefix
*)arg
;
806 delete_prefix(prefix
);
812 update_prefix(struct prefix
* prefix
)
814 u_char ntopbuf
[INET6_ADDRSTRLEN
];
815 struct rainfo
*rai
= prefix
->rainfo
;
817 if (prefix
->timer
== NULL
) { /* sanity check */
819 "<%s> assumption failure: timer does not exist",
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
);
828 /* stop the expiration timer */
829 rtadvd_remove_timer(&prefix
->timer
);
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[].
838 init_prefix(struct in6_prefixreq
*ipr
)
843 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0) {
844 syslog(LOG_ERR
, "<%s> socket: %s", __func__
,
849 if (ioctl(s
, SIOCGIFPREFIX_IN6
, (caddr_t
)ipr
) < 0) {
850 syslog(LOG_INFO
, "<%s> ioctl:SIOCGIFPREFIX %s", __func__
,
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 */
859 else if (ipr
->ipr_origin
< PR_ORIG_RR
) {
860 u_char ntopbuf
[INET6_ADDRSTRLEN
];
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
);
874 ipr
->ipr_vltime
= DEF_ADVVALIDLIFETIME
;
875 ipr
->ipr_pltime
= DEF_ADVPREFERREDLIFETIME
;
876 ipr
->ipr_raf_onlink
= 1;
877 ipr
->ipr_raf_auto
= 1;
883 make_prefix(struct rainfo
*rai
, int ifindex
, struct in6_addr
*addr
, int plen
)
885 struct in6_prefixreq ipr
;
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
));
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
;
899 if (init_prefix(&ipr
))
900 return; /* init failed by some error */
901 add_prefix(rai
, &ipr
);
905 make_packet(struct rainfo
*rainfo
)
907 size_t packlen
, lladdroptlen
= 0;
909 struct nd_router_advert
*ra
;
910 struct nd_opt_prefix_info
*ndopt_pi
;
911 struct nd_opt_mtu
*ndopt_mtu
;
913 struct nd_opt_route_info
*ndopt_rti
;
918 /* calculate total length */
919 packlen
= sizeof(struct nd_router_advert
);
920 if (rainfo
->advlinkopt
) {
921 if ((lladdroptlen
= lladdropt_length(rainfo
->sdl
)) == 0) {
923 "<%s> link-layer address option has"
924 " null length on %s. Treat as not included.",
925 __func__
, rainfo
->ifname
);
926 rainfo
->advlinkopt
= 0;
928 packlen
+= lladdroptlen
;
931 packlen
+= sizeof(struct nd_opt_prefix_info
) * rainfo
->pfxs
;
933 packlen
+= sizeof(struct nd_opt_mtu
);
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;
940 /* allocate memory for the packet */
941 if ((buf
= malloc(packlen
)) == NULL
) {
943 "<%s> can't get enough memory for an RA packet",
947 if (rainfo
->ra_data
) {
948 /* free the previous packet */
949 free(rainfo
->ra_data
);
950 rainfo
->ra_data
= NULL
;
952 rainfo
->ra_data
= buf
;
953 /* XXX: what if packlen > 576? */
954 rainfo
->ra_datalen
= packlen
;
957 * construct the packet
959 ra
= (struct nd_router_advert
*)buf
;
960 ra
->nd_ra_type
= ND_ROUTER_ADVERT
;
963 ra
->nd_ra_curhoplimit
= (u_int8_t
)(0xff & rainfo
->hoplimit
);
964 ra
->nd_ra_flags_reserved
= 0; /* just in case */
966 * XXX: the router preference field, which is a 2-bit field, should be
967 * initialized before other fields.
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
);
979 if (rainfo
->advlinkopt
) {
980 lladdropt_fill(rainfo
->sdl
, (struct nd_opt_hdr
*)buf
);
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
);
993 for (pfx
= rainfo
->prefix
.next
;
994 pfx
!= &rainfo
->prefix
; pfx
= pfx
->next
) {
995 u_int32_t vltime
, pltime
;
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;
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
;
1012 if (pfx
->vltimeexpire
|| pfx
->pltimeexpire
)
1013 gettimeofday(&now
, NULL
);
1014 if (pfx
->vltimeexpire
== 0)
1015 vltime
= pfx
->validlifetime
;
1017 vltime
= (pfx
->vltimeexpire
> now
.tv_sec
) ?
1018 pfx
->vltimeexpire
- now
.tv_sec
: 0;
1023 if (pfx
->pltimeexpire
== 0)
1024 pltime
= pfx
->preflifetime
;
1026 pltime
= (pfx
->pltimeexpire
> now
.tv_sec
) ?
1027 pfx
->pltimeexpire
- now
.tv_sec
: 0;
1029 if (vltime
< pltime
) {
1031 * this can happen if vltime is decrement but pltime
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
;
1041 buf
+= sizeof(struct nd_opt_prefix_info
);
1045 for (rti
= rainfo
->route
.next
; rti
!= &rainfo
->route
; rti
= rti
->next
) {
1046 u_int8_t psize
= (rti
->prefixlen
+ 0x3f) >> 6;
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;
1063 getinet6sysctl(int code
)
1065 int mib
[] = { CTL_NET
, PF_INET6
, IPPROTO_IPV6
, 0 };
1070 size
= sizeof(value
);
1071 if (sysctl(mib
, sizeof(mib
)/sizeof(mib
[0]), &value
, &size
, NULL
, 0)
1073 syslog(LOG_ERR
, "<%s>: failed to get ip6 sysctl(%d): %s",