2 * Copyright (c) 2009 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 /* $KAME: config.c,v 1.37 2001/05/25 07:34:00 itojun Exp $ */
32 * Copyright (C) 1998 WIDE Project.
33 * All rights reserved.
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. Neither the name of the project nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
47 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * $FreeBSD: src/usr.sbin/rtadvd/config.c,v 1.3.2.3 2001/07/03 11:02:14 ume Exp $
62 #include <sys/param.h>
63 #include <sys/ioctl.h>
64 #include <sys/socket.h>
66 #include <sys/sysctl.h>
69 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
70 #include <net/if_var.h>
71 #endif /* __FreeBSD__ >= 3 */
72 #include <net/route.h>
73 #include <net/if_dl.h>
75 #include <netinet/in.h>
76 #include <netinet/in_var.h>
77 #include <netinet/ip6.h>
78 #include <netinet6/ip6_var.h>
79 #include <netinet/icmp6.h>
81 #include <netinet6/mip6.h>
84 #include <arpa/inet.h>
91 #if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__APPLE__)
103 static void makeentry (char *, size_t, int, char *, int);
104 static void get_prefix (struct rainfo
*);
105 static int getinet6sysctl (int);
107 extern struct rainfo
*ralist
;
110 getconfig(char *intface
)
121 static int forwarding
= -1;
123 #define MUSTHAVE(var, cap) \
126 if ((t = agetnum(cap)) < 0) { \
127 fprintf(stderr, "rtadvd: need %s for interface %s\n", \
133 #define MAYHAVE(var, cap, def) \
135 if ((var = agetnum(cap)) < 0) \
139 if ((stat
= agetent(tbuf
, intface
)) <= 0) {
140 memset(tbuf
, 0, sizeof(tbuf
));
142 "<%s> %s isn't defined in the configuration file"
143 " or the configuration file doesn't exist."
144 " Treat it as default",
145 __FUNCTION__
, intface
);
148 tmp
= (struct rainfo
*)malloc(sizeof(*ralist
));
149 memset(tmp
, 0, sizeof(*tmp
));
150 tmp
->prefix
.next
= tmp
->prefix
.prev
= &tmp
->prefix
;
151 tmp
->route
.next
= tmp
->route
.prev
= &tmp
->route
;
152 tmp
->rdnss_list
.next
= tmp
->rdnss_list
.prev
= &tmp
->rdnss_list
;
154 /* check if we are allowed to forward packets (if not determined) */
155 if (forwarding
< 0) {
156 if ((forwarding
= getinet6sysctl(IPV6CTL_FORWARDING
)) < 0)
160 /* get interface information */
161 if (agetflag("nolladdr"))
165 if (tmp
->advlinkopt
) {
166 if ((tmp
->sdl
= if_nametosdl(intface
)) == NULL
) {
168 "<%s> can't get information of %s",
169 __FUNCTION__
, intface
);
172 tmp
->ifindex
= tmp
->sdl
->sdl_index
;
174 tmp
->ifindex
= if_nametoindex(intface
);
175 strncpy(tmp
->ifname
, intface
, sizeof(tmp
->ifname
));
176 if ((tmp
->phymtu
= if_getmtu(intface
)) == 0) {
177 tmp
->phymtu
= IPV6_MMTU
;
179 "<%s> can't get interface mtu of %s. Treat as %d",
180 __FUNCTION__
, intface
, IPV6_MMTU
);
184 * set router configuration variables.
186 MAYHAVE(val
, "maxinterval", DEF_MAXRTRADVINTERVAL
);
187 if (val
< MIN_MAXINTERVAL
|| val
> MAX_MAXINTERVAL
) {
189 "<%s> maxinterval must be between %e and %u",
190 __FUNCTION__
, MIN_MAXINTERVAL
, MAX_MAXINTERVAL
);
193 tmp
->maxinterval
= (u_int
)val
;
194 MAYHAVE(val
, "mininterval", tmp
->maxinterval
/3);
195 if (val
< MIN_MININTERVAL
|| val
> (tmp
->maxinterval
* 3) / 4) {
197 "<%s> mininterval must be between %e and %d",
200 (tmp
->maxinterval
* 3) / 4);
203 tmp
->mininterval
= (u_int
)val
;
205 MAYHAVE(val
, "chlim", DEF_ADVCURHOPLIMIT
);
206 tmp
->hoplimit
= val
& 0xff;
208 MAYHAVE(val
, "raflags", 0);
209 tmp
->managedflg
= val
& ND_RA_FLAG_MANAGED
;
210 tmp
->otherflg
= val
& ND_RA_FLAG_OTHER
;
213 tmp
->haflg
= val
& ND_RA_FLAG_HA
;
215 #ifndef ND_RA_FLAG_RTPREF_MASK
216 #define ND_RA_FLAG_RTPREF_MASK 0x18 /* 00011000 */
217 #define ND_RA_FLAG_RTPREF_RSV 0x10 /* 00010000 */
219 tmp
->rtpref
= val
& ND_RA_FLAG_RTPREF_MASK
;
220 if (tmp
->rtpref
== ND_RA_FLAG_RTPREF_RSV
) {
221 syslog(LOG_ERR
, "<%s> invalid router preference on %s",
222 __FUNCTION__
, intface
);
226 MAYHAVE(val
, "rltime", tmp
->maxinterval
* 3);
227 if (val
&& (val
< tmp
->maxinterval
|| val
> MAXROUTERLIFETIME
)) {
229 "<%s> router lifetime on %s must be 0 or"
230 " between %d and %d",
231 __FUNCTION__
, intface
,
232 tmp
->maxinterval
, MAXROUTERLIFETIME
);
236 * Basically, hosts MUST NOT send Router Advertisement messages at any
237 * time (RFC 2461, Section 6.2.3). However, it would sometimes be
238 * useful to allow hosts to advertise some parameters such as prefix
239 * information and link MTU. Thus, we allow hosts to invoke rtadvd
240 * only when router lifetime (on every advertising interface) is
241 * explicitly set zero. (see also the above section)
243 if (val
&& forwarding
== 0) {
245 "<%s> non zero router lifetime is specified for %s, "
246 "which must not be allowed for hosts.",
247 __FUNCTION__
, intface
);
250 tmp
->lifetime
= val
& 0xffff;
252 MAYHAVE(val
, "rtime", DEF_ADVREACHABLETIME
);
253 if (val
> MAXREACHABLETIME
) {
255 "<%s> reachable time must be no greater than %d",
256 __FUNCTION__
, MAXREACHABLETIME
);
259 tmp
->reachabletime
= (u_int32_t
)val
;
261 MAYHAVE(val64
, "retrans", DEF_ADVRETRANSTIMER
);
262 if (val64
< 0 || val64
> 0xffffffff) {
264 "<%s> retrans time out of range", __FUNCTION__
);
267 tmp
->retranstimer
= (u_int32_t
)val64
;
270 if (agetstr("hapref", &bp
) || agetstr("hatime", &bp
)) {
272 "<%s> mobile-ip6 configuration not supported",
278 if (agetstr("hapref", &bp
) || agetstr("hatime", &bp
)) {
280 "<%s> mobile-ip6 configuration without "
281 "proper command line option",
287 if ((val
= agetnum("hapref")) >= 0)
288 tmp
->hapref
= (int16_t)val
;
289 if (tmp
->hapref
!= 0) {
291 MUSTHAVE(val
, "hatime");
292 tmp
->hatime
= (u_int16_t
)val
;
293 if (tmp
->hatime
<= 0) {
295 "<%s> home agent lifetime must be greater than 0",
303 /* prefix information */
306 * This is an implementation specific parameter to consinder
307 * link propagation delays and poorly synchronized clocks when
308 * checking consistency of advertised lifetimes.
310 MAYHAVE(val
, "clockskew", 0);
311 tmp
->clockskew
= val
;
313 if ((pfxs
= agetnum("addrs")) < 0) {
314 /* auto configure prefix information */
315 if (agetstr("addr", &bp
) || agetstr("addr1", &bp
)) {
317 "<%s> conflicting prefix configuration for %s: "
318 "automatic and manual config at the same time",
319 __FUNCTION__
, intface
);
326 for (i
= 0; i
< pfxs
; i
++) {
329 int added
= (pfxs
> 1) ? 1 : 0;
331 /* allocate memory to store prefix information */
332 if ((pfx
= malloc(sizeof(struct prefix
))) == NULL
) {
334 "<%s> can't allocate enough memory",
338 memset(pfx
, 0, sizeof(*pfx
));
340 /* link into chain */
341 insque(pfx
, &tmp
->prefix
);
343 pfx
->origin
= PREFIX_FROM_CONFIG
;
345 makeentry(entbuf
, sizeof(entbuf
), i
, "prefixlen", added
);
346 MAYHAVE(val
, entbuf
, 64);
347 if (val
< 0 || val
> 128) {
349 "<%s> prefixlen out of range",
353 pfx
->prefixlen
= (int)val
;
355 makeentry(entbuf
, sizeof(entbuf
), i
, "pinfoflags", added
);
360 (ND_OPT_PI_FLAG_ONLINK
|ND_OPT_PI_FLAG_AUTO
|
361 ND_OPT_PI_FLAG_ROUTER
));
366 (ND_OPT_PI_FLAG_ONLINK
|ND_OPT_PI_FLAG_AUTO
));
368 pfx
->onlinkflg
= val
& ND_OPT_PI_FLAG_ONLINK
;
369 pfx
->autoconfflg
= val
& ND_OPT_PI_FLAG_AUTO
;
371 pfx
->routeraddr
= val
& ND_OPT_PI_FLAG_ROUTER
;
374 makeentry(entbuf
, sizeof(entbuf
), i
, "vltime", added
);
375 MAYHAVE(val64
, entbuf
, DEF_ADVVALIDLIFETIME
);
376 if (val64
< 0 || val64
> 0xffffffff) {
378 "<%s> vltime out of range",
382 pfx
->validlifetime
= (u_int32_t
)val64
;
384 makeentry(entbuf
, sizeof(entbuf
), i
, "vltimedecr", added
);
385 if (agetflag(entbuf
)) {
387 gettimeofday(&now
, 0);
389 now
.tv_sec
+ pfx
->validlifetime
;
392 makeentry(entbuf
, sizeof(entbuf
), i
, "pltime", added
);
393 MAYHAVE(val64
, entbuf
, DEF_ADVPREFERREDLIFETIME
);
394 if (val64
< 0 || val64
> 0xffffffff) {
396 "<%s> pltime out of range",
400 pfx
->preflifetime
= (u_int32_t
)val64
;
402 makeentry(entbuf
, sizeof(entbuf
), i
, "pltimedecr", added
);
403 if (agetflag(entbuf
)) {
405 gettimeofday(&now
, 0);
407 now
.tv_sec
+ pfx
->preflifetime
;
410 makeentry(entbuf
, sizeof(entbuf
), i
, "addr", added
);
411 addr
= (char *)agetstr(entbuf
, &bp
);
414 "<%s> need %s as an prefix for "
416 __FUNCTION__
, entbuf
, intface
);
419 if (inet_pton(AF_INET6
, addr
,
420 &pfx
->prefix
) != 1) {
422 "<%s> inet_pton failed for %s",
426 if (IN6_IS_ADDR_MULTICAST(&pfx
->prefix
)) {
428 "<%s> multicast prefix(%s) must "
429 "not be advertised (IF=%s)",
430 __FUNCTION__
, addr
, intface
);
433 if (IN6_IS_ADDR_LINKLOCAL(&pfx
->prefix
))
435 "<%s> link-local prefix(%s) will be"
437 __FUNCTION__
, addr
, intface
);
441 MAYHAVE(val
, "mtu", 0);
442 if (val
< 0 || val
> 0xffffffff) {
444 "<%s> mtu out of range", __FUNCTION__
);
447 tmp
->linkmtu
= (u_int32_t
)val
;
448 if (tmp
->linkmtu
== 0) {
451 if ((mtustr
= (char *)agetstr("mtu", &bp
)) &&
452 strcmp(mtustr
, "auto") == 0)
453 tmp
->linkmtu
= tmp
->phymtu
;
455 else if (tmp
->linkmtu
< IPV6_MMTU
|| tmp
->linkmtu
> tmp
->phymtu
) {
457 "<%s> advertised link mtu must be between"
458 " least MTU and physical link MTU",
463 /* route information */
465 MAYHAVE(val
, "routes", 0);
466 if (val
< 0 || val
> 0xffffffff) {
468 "<%s> number of route information improper", __FUNCTION__
);
472 for (i
= 0; i
< tmp
->routes
; i
++) {
475 int added
= (tmp
->routes
> 1) ? 1 : 0;
477 /* allocate memory to store prefix information */
478 if ((rti
= malloc(sizeof(struct rtinfo
))) == NULL
) {
480 "<%s> can't allocate enough memory",
484 memset(rti
, 0, sizeof(*rti
));
486 /* link into chain */
487 insque(rti
, &tmp
->route
);
489 makeentry(entbuf
, sizeof(entbuf
), i
, "rtrplen", added
);
490 MAYHAVE(val
, entbuf
, 64);
491 if (val
< 0 || val
> 128) {
493 "<%s> prefixlen out of range",
497 rti
->prefixlen
= (int)val
;
499 makeentry(entbuf
, sizeof(entbuf
), i
, "rtrflags", added
);
500 MAYHAVE(val
, entbuf
, 0);
501 rti
->rtpref
= val
& ND_RA_FLAG_RTPREF_MASK
;
502 if (rti
->rtpref
== ND_RA_FLAG_RTPREF_RSV
) {
503 syslog(LOG_ERR
, "<%s> invalid router preference",
508 makeentry(entbuf
, sizeof(entbuf
), i
, "rtrltime", added
);
510 * XXX: since default value of route lifetime is not defined in
511 * draft-draves-route-selection-01.txt, I took the default
512 * value of valid lifetime of prefix as its default.
513 * It need be much considered.
515 MAYHAVE(val64
, entbuf
, DEF_ADVVALIDLIFETIME
);
516 if (val64
< 0 || val64
> 0xffffffff) {
518 "<%s> rtrltime out of range",
522 rti
->ltime
= (u_int32_t
)val64
;
524 makeentry(entbuf
, sizeof(entbuf
), i
, "rtrprefix", added
);
525 addr
= (char *)agetstr(entbuf
, &bp
);
528 "<%s> need %s as an route for "
530 __FUNCTION__
, entbuf
, intface
);
533 if (inet_pton(AF_INET6
, addr
, &rti
->prefix
) != 1) {
535 "<%s> inet_pton failed for %s",
541 * XXX: currently there's no restriction in route information
542 * prefix according to draft-draves-route-selection-01.txt,
543 * however I think the similar restriction be necessary.
545 MAYHAVE(val64
, entbuf
, DEF_ADVVALIDLIFETIME
);
546 if (IN6_IS_ADDR_MULTICAST(&rti
->prefix
)) {
548 "<%s> multicast route (%s) must "
549 "not be advertised (IF=%s)",
550 __FUNCTION__
, addr
, intface
);
553 if (IN6_IS_ADDR_LINKLOCAL(&rti
->prefix
)) {
555 "<%s> link-local route (%s) must "
556 "not be advertised on %s",
557 __FUNCTION__
, addr
, intface
);
563 /* RDNSS option (RFC5006) */
564 MAYHAVE(val
, "rdnsslifetime", 2 * tmp
->maxinterval
);
565 if (val
< tmp
->maxinterval
|| val
> (2 * tmp
->maxinterval
)) {
567 "<%s> rdnsslifetime (%lu) on %s SHOULD "
568 "be between %u and %u", __FUNCTION__
, val
,
569 intface
, tmp
->maxinterval
, 2 * tmp
->maxinterval
);
571 tmp
->rdnss_lifetime
= val
;
572 if ((rdnss_length
= agetnum("rdnssaddrs")) < 0) {
573 tmp
->rdnss_length
= 0;
576 tmp
->rdnss_length
= rdnss_length
;
578 /* traverse in reverse order so that the queue has correct order */
579 for (i
= (rdnss_length
- 1); i
>= 0; i
--) {
582 int added
= (rdnss_length
> 1) ? 1 : 0;
584 /* allocate memory to store server address information */
585 if ((rdnss
= malloc(sizeof(struct rdnss
))) == NULL
) {
587 "<%s> can't allocate enough memory",
591 memset(rdnss
, 0, sizeof(*rdnss
));
593 /* link into chain */
594 insque(rdnss
, &tmp
->rdnss_list
);
596 makeentry(entbuf
, sizeof(entbuf
), i
, "rdnssaddr", added
);
597 addr
= (char *)agetstr(entbuf
, &bp
);
600 "<%s> need %s as a DNS server address for "
602 __FUNCTION__
, entbuf
, intface
);
606 if (inet_pton(AF_INET6
, addr
, &rdnss
->addr
) != 1) {
608 "<%s> inet_pton failed for %s",
612 if (IN6_IS_ADDR_MULTICAST(&rdnss
->addr
)) {
614 "<%s> multicast address (%s) must "
615 "not be advertised as recursive DNS server",
626 /* construct the sending packet */
630 tmp
->timer
= rtadvd_add_timer(ra_timeout
, ra_timer_update
,
632 ra_timer_update((void *)tmp
, &tmp
->timer
->tm
);
633 rtadvd_set_timer(&tmp
->timer
->tm
, tmp
->timer
);
637 get_prefix(struct rainfo
*rai
)
639 struct ifaddrs
*ifap
, *ifa
;
642 u_char
*p
, *ep
, *m
, *lim
;
643 u_char ntopbuf
[INET6_ADDRSTRLEN
];
645 if (getifaddrs(&ifap
) < 0) {
647 "<%s> can't get interface addresses",
651 for (ifa
= ifap
; ifa
; ifa
= ifa
->ifa_next
) {
652 if (strcmp(ifa
->ifa_name
, rai
->ifname
) != 0)
654 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
656 a
= &((struct sockaddr_in6
*)ifa
->ifa_addr
)->sin6_addr
;
657 if (IN6_IS_ADDR_LINKLOCAL(a
))
660 /* allocate memory to store prefix info. */
661 if ((pp
= malloc(sizeof(*pp
))) == NULL
) {
663 "<%s> can't get allocate buffer for prefix",
667 memset(pp
, 0, sizeof(*pp
));
669 /* set prefix length */
670 m
= (u_char
*)&((struct sockaddr_in6
*)ifa
->ifa_netmask
)->sin6_addr
;
671 lim
= (u_char
*)(ifa
->ifa_netmask
) + ifa
->ifa_netmask
->sa_len
;
672 pp
->prefixlen
= prefixlen(m
, lim
);
673 if (pp
->prefixlen
< 0 || pp
->prefixlen
> 128) {
675 "<%s> failed to get prefixlen "
676 "or prefix is invalid",
681 /* set prefix, sweep bits outside of prefixlen */
682 memcpy(&pp
->prefix
, a
, sizeof(*a
));
683 p
= (u_char
*)&pp
->prefix
;
684 ep
= (u_char
*)(&pp
->prefix
+ 1);
690 if (!inet_ntop(AF_INET6
, &pp
->prefix
, (char *)ntopbuf
,
692 syslog(LOG_ERR
, "<%s> inet_ntop failed", __FUNCTION__
);
696 "<%s> add %s/%d to prefix list on %s",
697 __FUNCTION__
, ntopbuf
, pp
->prefixlen
, rai
->ifname
);
699 /* set other fields with protocol defaults */
700 pp
->validlifetime
= DEF_ADVVALIDLIFETIME
;
701 pp
->preflifetime
= DEF_ADVPREFERREDLIFETIME
;
704 pp
->origin
= PREFIX_FROM_KERNEL
;
706 /* link into chain */
707 insque(pp
, &rai
->prefix
);
709 /* counter increment */
717 makeentry(char *buf
, size_t len
, int id
, char *string
, int add
)
719 strlcpy(buf
, string
, len
);
723 cp
= (char *)index(buf
, '\0');
724 cp
+= snprintf(cp
, len
- (cp
- buf
), "%d", id
);
730 * Add a prefix to the list of specified interface and reconstruct
731 * the outgoing packet.
732 * The prefix must not be in the list.
733 * XXX: other parameter of the prefix(e.g. lifetime) shoule be
734 * able to be specified.
737 add_prefix(struct rainfo
*rai
, struct in6_prefixreq
*ipr
)
739 struct prefix
*prefix
;
740 u_char ntopbuf
[INET6_ADDRSTRLEN
];
742 if ((prefix
= malloc(sizeof(*prefix
))) == NULL
) {
743 syslog(LOG_ERR
, "<%s> memory allocation failed",
745 return; /* XXX: error or exit? */
747 memset(prefix
, 0, sizeof(*prefix
));
748 prefix
->prefix
= ipr
->ipr_prefix
.sin6_addr
;
749 prefix
->prefixlen
= ipr
->ipr_plen
;
750 prefix
->validlifetime
= ipr
->ipr_vltime
;
751 prefix
->preflifetime
= ipr
->ipr_pltime
;
752 prefix
->onlinkflg
= ipr
->ipr_raf_onlink
;
753 prefix
->autoconfflg
= ipr
->ipr_raf_auto
;
754 prefix
->origin
= PREFIX_FROM_DYNAMIC
;
756 insque(prefix
, &rai
->prefix
);
758 syslog(LOG_DEBUG
, "<%s> new prefix %s/%d was added on %s",
759 __FUNCTION__
, inet_ntop(AF_INET6
, &ipr
->ipr_prefix
.sin6_addr
,
760 (char *)ntopbuf
, INET6_ADDRSTRLEN
),
761 ipr
->ipr_plen
, rai
->ifname
);
763 /* free the previous packet */
767 /* reconstruct the packet */
772 * reset the timer so that the new prefix will be advertised quickly.
774 rai
->initcounter
= 0;
775 ra_timer_update((void *)rai
, &rai
->timer
->tm
);
776 rtadvd_set_timer(&rai
->timer
->tm
, rai
->timer
);
780 * Delete a prefix to the list of specified interface and reconstruct
781 * the outgoing packet.
782 * The prefix must be in the list.
785 delete_prefix(struct rainfo
*rai
, struct prefix
*prefix
)
787 u_char ntopbuf
[INET6_ADDRSTRLEN
];
790 syslog(LOG_DEBUG
, "<%s> prefix %s/%d was deleted on %s",
791 __FUNCTION__
, inet_ntop(AF_INET6
, &prefix
->prefix
,
792 (char *)ntopbuf
, INET6_ADDRSTRLEN
),
793 prefix
->prefixlen
, rai
->ifname
);
800 * Try to get an in6_prefixreq contents for a prefix which matches
801 * ipr->ipr_prefix and ipr->ipr_plen and belongs to
802 * the interface whose name is ipr->ipr_name[].
805 init_prefix(struct in6_prefixreq
*ipr
)
809 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0) {
810 syslog(LOG_ERR
, "<%s> socket: %s", __FUNCTION__
,
815 if (ioctl(s
, SIOCGIFPREFIX_IN6
, (caddr_t
)ipr
) < 0) {
816 syslog(LOG_INFO
, "<%s> ioctl:SIOCGIFPREFIX %s", __FUNCTION__
,
819 ipr
->ipr_vltime
= DEF_ADVVALIDLIFETIME
;
820 ipr
->ipr_pltime
= DEF_ADVPREFERREDLIFETIME
;
821 ipr
->ipr_raf_onlink
= 1;
822 ipr
->ipr_raf_auto
= 1;
823 /* omit other field initialization */
825 else if (ipr
->ipr_origin
< PR_ORIG_RR
) {
826 u_char ntopbuf
[INET6_ADDRSTRLEN
];
828 syslog(LOG_WARNING
, "<%s> Added prefix(%s)'s origin %d is"
829 "lower than PR_ORIG_RR(router renumbering)."
830 "This should not happen if I am router", __FUNCTION__
,
831 inet_ntop(AF_INET6
, &ipr
->ipr_prefix
.sin6_addr
, (char *)ntopbuf
,
832 sizeof(ntopbuf
)), ipr
->ipr_origin
);
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", __FUNCTION__
,
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 __FUNCTION__
, 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;
907 if (rainfo
->rdnss_length
> 0)
908 packlen
+= 8 + sizeof(struct in6_addr
) * rainfo
->rdnss_length
;
910 /* allocate memory for the packet */
911 if ((buf
= malloc(packlen
)) == NULL
) {
913 "<%s> can't get enough memory for an RA packet",
917 if (rainfo
->ra_data
) {
918 /* free the previous packet */
919 free(rainfo
->ra_data
);
920 rainfo
->ra_data
= NULL
;
922 rainfo
->ra_data
= (u_char
*)buf
;
923 /* XXX: what if packlen > 576? */
924 rainfo
->ra_datalen
= packlen
;
927 * construct the packet
929 ra
= (struct nd_router_advert
*)buf
;
930 ra
->nd_ra_type
= ND_ROUTER_ADVERT
;
933 ra
->nd_ra_curhoplimit
= (u_int8_t
)(0xff & rainfo
->hoplimit
);
934 ra
->nd_ra_flags_reserved
= 0; /* just in case */
936 * XXX: the router preference field, which is a 2-bit field, should be
937 * initialized before other fields.
939 ra
->nd_ra_flags_reserved
= 0xff & rainfo
->rtpref
;
940 ra
->nd_ra_flags_reserved
|=
941 rainfo
->managedflg
? ND_RA_FLAG_MANAGED
: 0;
942 ra
->nd_ra_flags_reserved
|=
943 rainfo
->otherflg
? ND_RA_FLAG_OTHER
: 0;
945 ra
->nd_ra_flags_reserved
|=
946 rainfo
->haflg
? ND_RA_FLAG_HA
: 0;
948 ra
->nd_ra_router_lifetime
= htons(rainfo
->lifetime
);
949 ra
->nd_ra_reachable
= htonl(rainfo
->reachabletime
);
950 ra
->nd_ra_retransmit
= htonl(rainfo
->retranstimer
);
953 if (rainfo
->advlinkopt
) {
954 lladdropt_fill(rainfo
->sdl
, (struct nd_opt_hdr
*)buf
);
958 if (rainfo
->linkmtu
) {
959 ndopt_mtu
= (struct nd_opt_mtu
*)buf
;
960 ndopt_mtu
->nd_opt_mtu_type
= ND_OPT_MTU
;
961 ndopt_mtu
->nd_opt_mtu_len
= 1;
962 ndopt_mtu
->nd_opt_mtu_reserved
= 0;
963 ndopt_mtu
->nd_opt_mtu_mtu
= htonl(rainfo
->linkmtu
);
964 buf
+= sizeof(struct nd_opt_mtu
);
968 if (mobileip6
&& rainfo
->maxinterval
) {
969 ndopt_advint
= (struct nd_opt_advinterval
*)buf
;
970 ndopt_advint
->nd_opt_adv_type
= ND_OPT_ADVINTERVAL
;
971 ndopt_advint
->nd_opt_adv_len
= 1;
972 ndopt_advint
->nd_opt_adv_reserved
= 0;
973 ndopt_advint
->nd_opt_adv_interval
= htonl(rainfo
->maxinterval
*
975 buf
+= sizeof(struct nd_opt_advinterval
);
980 if (rainfo
->hatime
) {
981 ndopt_hai
= (struct nd_opt_homeagent_info
*)buf
;
982 ndopt_hai
->nd_opt_hai_type
= ND_OPT_HOMEAGENT_INFO
;
983 ndopt_hai
->nd_opt_hai_len
= 1;
984 ndopt_hai
->nd_opt_hai_reserved
= 0;
985 ndopt_hai
->nd_opt_hai_preference
= htons(rainfo
->hapref
);
986 ndopt_hai
->nd_opt_hai_lifetime
= htons(rainfo
->hatime
);
987 buf
+= sizeof(struct nd_opt_homeagent_info
);
991 for (pfx
= rainfo
->prefix
.next
;
992 pfx
!= &rainfo
->prefix
; pfx
= pfx
->next
) {
993 u_int32_t vltime
, pltime
;
996 ndopt_pi
= (struct nd_opt_prefix_info
*)buf
;
997 ndopt_pi
->nd_opt_pi_type
= ND_OPT_PREFIX_INFORMATION
;
998 ndopt_pi
->nd_opt_pi_len
= 4;
999 ndopt_pi
->nd_opt_pi_prefix_len
= pfx
->prefixlen
;
1000 ndopt_pi
->nd_opt_pi_flags_reserved
= 0;
1002 ndopt_pi
->nd_opt_pi_flags_reserved
|=
1003 ND_OPT_PI_FLAG_ONLINK
;
1004 if (pfx
->autoconfflg
)
1005 ndopt_pi
->nd_opt_pi_flags_reserved
|=
1006 ND_OPT_PI_FLAG_AUTO
;
1008 if (pfx
->routeraddr
)
1009 ndopt_pi
->nd_opt_pi_flags_reserved
|=
1010 ND_OPT_PI_FLAG_ROUTER
;
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;
1019 if (pfx
->pltimeexpire
== 0)
1020 pltime
= pfx
->preflifetime
;
1022 pltime
= (pfx
->pltimeexpire
> now
.tv_sec
) ?
1023 pfx
->pltimeexpire
- now
.tv_sec
: 0;
1024 if (vltime
< pltime
) {
1026 * this can happen if vltime is decrement but pltime
1031 ndopt_pi
->nd_opt_pi_valid_time
= htonl(vltime
);
1032 ndopt_pi
->nd_opt_pi_preferred_time
= htonl(pltime
);
1033 ndopt_pi
->nd_opt_pi_reserved2
= 0;
1034 ndopt_pi
->nd_opt_pi_prefix
= pfx
->prefix
;
1036 buf
+= sizeof(struct nd_opt_prefix_info
);
1039 #ifdef ND_OPT_ROUTE_INFO
1040 for (rti
= rainfo
->route
.next
; rti
!= &rainfo
->route
; rti
= rti
->next
) {
1041 u_int8_t psize
= (rti
->prefixlen
+ 0x3f) >> 6;
1043 ndopt_rti
= (struct nd_opt_route_info
*)buf
;
1044 ndopt_rti
->nd_opt_rti_type
= ND_OPT_ROUTE_INFO
;
1045 ndopt_rti
->nd_opt_rti_len
= 1 + psize
;
1046 ndopt_rti
->nd_opt_rti_prefixlen
= rti
->prefixlen
;
1047 ndopt_rti
->nd_opt_rti_flags
= 0xff & rti
->rtpref
;
1048 ndopt_rti
->nd_opt_rti_lifetime
= rti
->ltime
;
1049 memcpy(ndopt_rti
+ 1, &rti
->prefix
, psize
* 8);
1050 buf
+= sizeof(struct nd_opt_route_info
) + psize
* 8;
1054 if (rainfo
->rdnss_length
> 0) {
1055 struct nd_opt_rdnss
* ndopt_rdnss
;
1056 struct rdnss
* rdnss
;
1058 ndopt_rdnss
= (struct nd_opt_rdnss
*) buf
;
1059 ndopt_rdnss
->nd_opt_rdnss_type
= ND_OPT_RDNSS
;
1060 ndopt_rdnss
->nd_opt_rdnss_len
= 1 + (rainfo
->rdnss_length
* 2);
1061 ndopt_rdnss
->nd_opt_rdnss_reserved
= 0;
1062 ndopt_rdnss
->nd_opt_rdnss_lifetime
= htonl(rainfo
->rdnss_lifetime
);
1065 for (rdnss
= rainfo
->rdnss_list
.next
;
1066 rdnss
!= &rainfo
->rdnss_list
;
1067 rdnss
= rdnss
->next
)
1069 struct in6_addr
* addr6
= (struct in6_addr
*) buf
;
1070 *addr6
= rdnss
->addr
;
1071 buf
+= sizeof *addr6
;
1079 getinet6sysctl(int code
)
1081 int mib
[] = { CTL_NET
, PF_INET6
, IPPROTO_IPV6
, 0 };
1086 size
= sizeof(value
);
1087 if (sysctl(mib
, sizeof(mib
)/sizeof(mib
[0]), &value
, &size
, NULL
, 0)
1089 syslog(LOG_ERR
, "<%s>: failed to get ip6 sysctl(%d): %s",