1 /* $KAME: config.c,v 1.37 2001/05/25 07:34:00 itojun Exp $ */
4 * Copyright (C) 1998 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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>
39 #include <sys/sysctl.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>
54 #include <netinet6/mip6.h>
57 #include <arpa/inet.h>
64 #if defined(__NetBSD__) || defined(__OpenBSD__)
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);
86 getconfig(char *intface
)
96 static int forwarding
= -1;
98 #define MUSTHAVE(var, cap) \
101 if ((t = agetnum(cap)) < 0) { \
102 fprintf(stderr, "rtadvd: need %s for interface %s\n", \
108 #define MAYHAVE(var, cap, def) \
110 if ((var = agetnum(cap)) < 0) \
114 if ((stat
= agetent(tbuf
, intface
)) <= 0) {
115 memset(tbuf
, 0, sizeof(tbuf
));
117 "<%s> %s isn't defined in the configuration file"
118 " or the configuration file doesn't exist."
119 " Treat it as default",
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)
134 /* get interface information */
135 if (agetflag("nolladdr"))
139 if (tmp
->advlinkopt
) {
140 if ((tmp
->sdl
= if_nametosdl(intface
)) == NULL
) {
142 "<%s> can't get information of %s",
146 tmp
->ifindex
= tmp
->sdl
->sdl_index
;
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
;
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
) {
163 "<%s> maxinterval must be between %e and %u",
164 __func__
, 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 must be between %e and %d",
174 (tmp
->maxinterval
* 3) / 4);
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
;
187 tmp
->haflg
= val
& ND_RA_FLAG_HA
;
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 */
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",
200 MAYHAVE(val
, "rltime", tmp
->maxinterval
* 3);
201 if (val
&& (val
< tmp
->maxinterval
|| val
> MAXROUTERLIFETIME
)) {
203 "<%s> router lifetime on %s must be 0 or"
204 " between %d and %d",
206 tmp
->maxinterval
, MAXROUTERLIFETIME
);
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) {
219 "<%s> non zero router lifetime is specified for %s, "
220 "which must not be allowed for hosts.",
224 tmp
->lifetime
= val
& 0xffff;
226 MAYHAVE(val
, "rtime", DEF_ADVREACHABLETIME
);
227 if (val
> MAXREACHABLETIME
) {
229 "<%s> reachable time must be no greater than %d",
230 __func__
, MAXREACHABLETIME
);
233 tmp
->reachabletime
= (u_int32_t
)val
;
235 MAYHAVE(val64
, "retrans", DEF_ADVRETRANSTIMER
);
236 if (val64
< 0 || val64
> 0xffffffff) {
238 "<%s> retrans time out of range", __func__
);
241 tmp
->retranstimer
= (u_int32_t
)val64
;
244 if (agetstr("hapref", &bp
) || agetstr("hatime", &bp
)) {
246 "<%s> mobile-ip6 configuration not supported",
252 if (agetstr("hapref", &bp
) || agetstr("hatime", &bp
)) {
254 "<%s> mobile-ip6 configuration without "
255 "proper command line option",
261 if ((val
= agetnum("hapref")) >= 0)
262 tmp
->hapref
= (int16_t)val
;
263 if (tmp
->hapref
!= 0) {
265 MUSTHAVE(val
, "hatime");
266 tmp
->hatime
= (u_int16_t
)val
;
267 if (tmp
->hatime
<= 0) {
269 "<%s> home agent lifetime must be greater than 0",
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
)) {
291 "<%s> conflicting prefix configuration for %s: "
292 "automatic and manual config at the same time",
300 for (i
= 0; i
< pfxs
; i
++) {
303 int added
= (pfxs
> 1) ? 1 : 0;
305 /* allocate memory to store prefix information */
306 if ((pfx
= malloc(sizeof(struct prefix
))) == NULL
) {
308 "<%s> can't allocate enough memory",
312 memset(pfx
, 0, sizeof(*pfx
));
314 /* link into chain */
315 insque(pfx
, &tmp
->prefix
);
318 pfx
->origin
= PREFIX_FROM_CONFIG
;
320 makeentry(entbuf
, sizeof(entbuf
), i
, "prefixlen",
322 MAYHAVE(val
, entbuf
, 64);
323 if (val
< 0 || val
> 128) {
325 "<%s> prefixlen out of range",
329 pfx
->prefixlen
= (int)val
;
331 makeentry(entbuf
, sizeof(entbuf
), i
, "pinfoflags",
337 (ND_OPT_PI_FLAG_ONLINK
|ND_OPT_PI_FLAG_AUTO
|
338 ND_OPT_PI_FLAG_ROUTER
));
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
;
348 pfx
->routeraddr
= val
& ND_OPT_PI_FLAG_ROUTER
;
351 makeentry(entbuf
, sizeof(entbuf
), i
, "vltime", added
);
352 MAYHAVE(val64
, entbuf
, DEF_ADVVALIDLIFETIME
);
353 if (val64
< 0 || val64
> 0xffffffff) {
355 "<%s> vltime out of range",
359 pfx
->validlifetime
= (u_int32_t
)val64
;
361 makeentry(entbuf
, sizeof(entbuf
), i
, "vltimedecr",
363 if (agetflag(entbuf
)) {
365 gettimeofday(&now
, 0);
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) {
374 "<%s> pltime out of range",
378 pfx
->preflifetime
= (u_int32_t
)val64
;
380 makeentry(entbuf
, sizeof(entbuf
), i
, "pltimedecr",
382 if (agetflag(entbuf
)) {
384 gettimeofday(&now
, 0);
386 now
.tv_sec
+ pfx
->preflifetime
;
389 makeentry(entbuf
, sizeof(entbuf
), i
, "addr", added
);
390 addr
= (char *)agetstr(entbuf
, &bp
);
393 "<%s> need %s as an prefix for "
395 __func__
, entbuf
, intface
);
398 if (inet_pton(AF_INET6
, addr
,
399 &pfx
->prefix
) != 1) {
401 "<%s> inet_pton failed for %s",
405 if (IN6_IS_ADDR_MULTICAST(&pfx
->prefix
)) {
407 "<%s> multicast prefix(%s) must "
408 "not be advertised (IF=%s)",
409 __func__
, addr
, intface
);
412 if (IN6_IS_ADDR_LINKLOCAL(&pfx
->prefix
))
414 "<%s> link-local prefix(%s) will be"
416 __func__
, addr
, intface
);
420 MAYHAVE(val
, "mtu", 0);
421 if (val
< 0 || val
> 0xffffffff) {
423 "<%s> mtu out of range", __func__
);
426 tmp
->linkmtu
= (u_int32_t
)val
;
427 if (tmp
->linkmtu
== 0) {
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
) {
436 "<%s> advertised link mtu must be between"
437 " least MTU and physical link MTU",
442 /* route information */
444 MAYHAVE(val
, "routes", 0);
445 if (val
< 0 || val
> 0xffffffff) {
447 "<%s> number of route information improper", __func__
);
451 for (i
= 0; i
< tmp
->routes
; i
++) {
454 int added
= (tmp
->routes
> 1) ? 1 : 0;
456 /* allocate memory to store prefix information */
457 if ((rti
= malloc(sizeof(struct rtinfo
))) == NULL
) {
459 "<%s> can't allocate enough memory",
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) {
472 "<%s> prefixlen out of range",
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",
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) {
497 "<%s> rtrltime out of range",
501 rti
->ltime
= (u_int32_t
)val64
;
503 makeentry(entbuf
, sizeof(entbuf
), i
, "rtrprefix", added
);
504 addr
= (char *)agetstr(entbuf
, &bp
);
507 "<%s> need %s as an route for "
509 __func__
, entbuf
, intface
);
512 if (inet_pton(AF_INET6
, addr
, &rti
->prefix
) != 1) {
514 "<%s> inet_pton failed for %s",
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
)) {
527 "<%s> multicast route (%s) must "
528 "not be advertised (IF=%s)",
529 __func__
, addr
, intface
);
532 if (IN6_IS_ADDR_LINKLOCAL(&rti
->prefix
)) {
534 "<%s> link-local route (%s) must "
535 "not be advertised on %s",
536 __func__
, addr
, intface
);
546 /* construct the sending packet */
550 tmp
->timer
= rtadvd_add_timer(ra_timeout
, ra_timer_update
,
552 ra_timer_update((void *)tmp
, &tmp
->timer
->tm
);
553 rtadvd_set_timer(&tmp
->timer
->tm
, tmp
->timer
);
557 get_prefix(struct rainfo
*rai
)
559 struct ifaddrs
*ifap
, *ifa
;
562 u_char
*p
, *ep
, *m
, *lim
;
563 u_char ntopbuf
[INET6_ADDRSTRLEN
];
565 if (getifaddrs(&ifap
) < 0) {
567 "<%s> can't get interface addresses",
571 for (ifa
= ifap
; ifa
; ifa
= ifa
->ifa_next
) {
574 if (strcmp(ifa
->ifa_name
, rai
->ifname
) != 0)
576 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
578 a
= &((struct sockaddr_in6
*)ifa
->ifa_addr
)->sin6_addr
;
579 if (IN6_IS_ADDR_LINKLOCAL(a
))
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",
592 if (find_prefix(rai
, a
, plen
)) {
593 /* ignore a duplicated prefix. */
597 /* allocate memory to store prefix info. */
598 if ((pp
= malloc(sizeof(*pp
))) == NULL
) {
600 "<%s> can't get allocate buffer for prefix",
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);
616 if (!inet_ntop(AF_INET6
, &pp
->prefix
, ntopbuf
,
618 syslog(LOG_ERR
, "<%s> inet_ntop failed", __func__
);
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
;
630 pp
->origin
= PREFIX_FROM_KERNEL
;
632 /* link into chain */
633 insque(pp
, &rai
->prefix
);
635 /* counter increment */
643 makeentry(char *buf
, size_t len
, int id
, char *string
, int add
)
645 char *ep
= buf
+ len
;
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.
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",
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 */
695 /* reconstruct the packet */
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.
713 delete_prefix(struct prefix
*prefix
)
715 u_char ntopbuf
[INET6_ADDRSTRLEN
];
716 struct rainfo
*rai
= prefix
->rainfo
;
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
);
724 rtadvd_remove_timer(&prefix
->timer
);
730 invalidate_prefix(struct prefix
*prefix
)
732 u_char ntopbuf
[INET6_ADDRSTRLEN
];
734 struct rainfo
*rai
= prefix
->rainfo
;
736 if (prefix
->timer
) { /* sanity check */
738 "<%s> assumption failure: timer already exists",
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
;
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
);
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 */
778 "<%s> assumption failure: timer does not exist",
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[].
797 init_prefix(struct in6_prefixreq
*ipr
)
802 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0) {
803 syslog(LOG_ERR
, "<%s> socket: %s", __func__
,
808 if (ioctl(s
, SIOCGIFPREFIX_IN6
, (caddr_t
)ipr
) < 0) {
809 syslog(LOG_INFO
, "<%s> ioctl:SIOCGIFPREFIX %s", __func__
,
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
);
833 ipr
->ipr_vltime
= DEF_ADVVALIDLIFETIME
;
834 ipr
->ipr_pltime
= DEF_ADVPREFERREDLIFETIME
;
835 ipr
->ipr_raf_onlink
= 1;
836 ipr
->ipr_raf_auto
= 1;
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
));
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
;
858 if (init_prefix(&ipr
))
859 return; /* init failed by some error */
860 add_prefix(rai
, &ipr
);
864 make_packet(struct rainfo
*rainfo
)
866 size_t packlen
, lladdroptlen
= 0;
868 struct nd_router_advert
*ra
;
869 struct nd_opt_prefix_info
*ndopt_pi
;
870 struct nd_opt_mtu
*ndopt_mtu
;
872 struct nd_opt_advinterval
*ndopt_advint
;
873 struct nd_opt_homeagent_info
*ndopt_hai
;
875 struct nd_opt_route_info
*ndopt_rti
;
879 /* calculate total length */
880 packlen
= sizeof(struct nd_router_advert
);
881 if (rainfo
->advlinkopt
) {
882 if ((lladdroptlen
= lladdropt_length(rainfo
->sdl
)) == 0) {
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
;
893 packlen
+= sizeof(struct nd_opt_prefix_info
) * rainfo
->pfxs
;
895 packlen
+= sizeof(struct nd_opt_mtu
);
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
);
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;
908 /* allocate memory for the packet */
909 if ((buf
= malloc(packlen
)) == NULL
) {
911 "<%s> can't get enough memory for an RA packet",
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
;
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;
943 ra
->nd_ra_flags_reserved
|=
944 rainfo
->haflg
? ND_RA_FLAG_HA
: 0;
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
);
951 if (rainfo
->advlinkopt
) {
952 lladdropt_fill(rainfo
->sdl
, (struct nd_opt_hdr
*)buf
);
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
);
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
*
973 buf
+= sizeof(struct nd_opt_advinterval
);
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
);
989 for (pfx
= rainfo
->prefix
.next
;
990 pfx
!= &rainfo
->prefix
; pfx
= pfx
->next
) {
991 u_int32_t vltime
, pltime
;
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;
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
;
1006 if (pfx
->routeraddr
)
1007 ndopt_pi
->nd_opt_pi_flags_reserved
|=
1008 ND_OPT_PI_FLAG_ROUTER
;
1013 if (pfx
->vltimeexpire
|| pfx
->pltimeexpire
)
1014 gettimeofday(&now
, NULL
);
1015 if (pfx
->vltimeexpire
== 0)
1016 vltime
= pfx
->validlifetime
;
1018 vltime
= (pfx
->vltimeexpire
> now
.tv_sec
) ?
1019 pfx
->vltimeexpire
- now
.tv_sec
: 0;
1024 if (pfx
->pltimeexpire
== 0)
1025 pltime
= pfx
->preflifetime
;
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
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;
1064 getinet6sysctl(int code
)
1066 int mib
[] = { CTL_NET
, PF_INET6
, IPPROTO_IPV6
, 0 };
1071 size
= sizeof(value
);
1072 if (sysctl(mib
, sizeof(mib
)/sizeof(mib
[0]), &value
, &size
, NULL
, 0)
1074 syslog(LOG_ERR
, "<%s>: failed to get ip6 sysctl(%d): %s",