2 * Copyright (c) 2020 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: rtadvd.c,v 1.82 2003/08/05 12:34:23 itojun Exp $ */
32 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
33 * Copyright (C) 2011 Hiroki Sato <hrs@FreeBSD.org>
34 * All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. Neither the name of the project nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 #include <sys/param.h>
62 #include <sys/socket.h>
65 #include <sys/queue.h>
68 #include <net/route.h>
69 #include <net/if_dl.h>
70 #include <netinet/in.h>
71 #include <netinet/ip6.h>
72 #include <netinet6/ip6_var.h>
73 #include <netinet/icmp6.h>
75 #include <arpa/inet.h>
94 struct msghdr rcvmhdr
;
95 static u_char
*rcvcmsgbuf
;
96 static size_t rcvcmsgbuflen
;
97 static u_char
*sndcmsgbuf
= NULL
;
98 static size_t sndcmsgbuflen
;
99 volatile sig_atomic_t do_dump
;
100 volatile sig_atomic_t do_die
;
101 struct msghdr sndmhdr
;
102 struct iovec rcviov
[2];
103 struct iovec sndiov
[2];
104 struct sockaddr_in6 rcvfrom
;
105 struct sockaddr_in6 sin6_allnodes
= {sizeof(sin6_allnodes
), AF_INET6
};
106 struct in6_addr in6a_site_allrouters
;
107 static char *dumpfilename
= "/var/run/rtadvd.dump";
108 static char *pidfilename
= "/var/run/rtadvd.pid";
109 static struct pidfh
*pfh
;
110 static char *mcastif
;
114 int dflag
= 0, sflag
= 0;
115 int so_traffic_class
= SO_TC_CTL
; /* use control class, by default */
116 char *conffile
= NULL
;
118 struct rainfo
*ralist
= NULL
;
121 struct nd_optlist
*next
;
122 struct nd_opt_hdr
*opt
;
125 struct nd_opt_hdr
*nd_opt_array
[9];
127 struct nd_opt_hdr
*zero
;
128 struct nd_opt_hdr
*src_lladdr
;
129 struct nd_opt_hdr
*tgt_lladdr
;
130 struct nd_opt_prefix_info
*pi
;
131 struct nd_opt_rd_hdr
*rh
;
132 struct nd_opt_mtu
*mtu
;
133 struct nd_optlist
*list
;
136 #define nd_opts_src_lladdr nd_opt_each.src_lladdr
137 #define nd_opts_tgt_lladdr nd_opt_each.tgt_lladdr
138 #define nd_opts_pi nd_opt_each.pi
139 #define nd_opts_rh nd_opt_each.rh
140 #define nd_opts_mtu nd_opt_each.mtu
141 #define nd_opts_list nd_opt_each.list
143 #define NDOPT_FLAG_SRCLINKADDR 0x1
144 #define NDOPT_FLAG_TGTLINKADDR 0x2
145 #define NDOPT_FLAG_PREFIXINFO 0x4
146 #define NDOPT_FLAG_RDHDR 0x8
147 #define NDOPT_FLAG_MTU 0x10
149 u_int32_t ndopt_flags
[] = {
150 0, NDOPT_FLAG_SRCLINKADDR
, NDOPT_FLAG_TGTLINKADDR
,
151 NDOPT_FLAG_PREFIXINFO
, NDOPT_FLAG_RDHDR
, NDOPT_FLAG_MTU
,
154 int main(int, char *[]);
155 static void set_die(int);
156 static void die(void);
157 static void sock_open(void);
158 static void rtsock_open(void);
159 static void rtadvd_input(void);
160 static void rs_input(int, struct nd_router_solicit
*,
161 struct in6_pktinfo
*, struct sockaddr_in6
*);
162 static void ra_input(int, struct nd_router_advert
*,
163 struct in6_pktinfo
*, struct sockaddr_in6
*);
164 static int prefix_check(struct nd_opt_prefix_info
*, struct rainfo
*,
165 struct sockaddr_in6
*);
166 static int nd6_options(struct nd_opt_hdr
*, int,
167 union nd_opts
*, u_int32_t
);
168 static void free_ndopts(union nd_opts
*);
169 static void ra_output(struct rainfo
*);
170 static void rtmsg_input(void);
171 static void rtadvd_set_dump_file(int);
172 static void set_short_delay(struct rainfo
*);
179 fd_set
*fdsetp
, *selectfdp
;
182 struct timeval
*timeout
;
187 /* get command line options and arguments */
188 while ((ch
= getopt(argc
, argv
, "c:dDF:fMp:Rs")) != -1) {
206 fprintf(stderr
, "rtadvd: "
207 "the -R option is currently ignored.\n");
215 pidfilename
= optarg
;
218 dumpfilename
= optarg
;
226 "usage: rtadvd [-dDfMRs] [-c conffile] "
227 "[-F dumpfile] [-p pidfile] interfaces...\n");
231 /* timer initialization */
234 /* random value initialization */
235 srandom((u_long
)time(NULL
));
237 /* get iflist block from kernel */
243 if (inet_pton(AF_INET6
, ALLNODES
, &sin6_allnodes
.sin6_addr
) != 1) {
244 fprintf(stderr
, "fatal: inet_pton failed\n");
248 pfh
= pidfile_open(pidfilename
, 0600, &otherpid
);
251 errx(1, "%s already running, pid: %d",
252 getprogname(), otherpid
);
253 errorlog("<%s> failed to open the pid log file, run anyway.",
262 /* record the current PID */
274 fdmasks
= howmany(maxfd
+ 1, NFDBITS
) * sizeof(fd_mask
);
275 if ((fdsetp
= malloc(fdmasks
)) == NULL
) {
279 if ((selectfdp
= malloc(fdmasks
)) == NULL
) {
283 memset(fdsetp
, 0, fdmasks
);
284 FD_SET(sock
, fdsetp
);
286 FD_SET(rtsock
, fdsetp
);
288 signal(SIGTERM
, set_die
);
289 signal(SIGUSR1
, rtadvd_set_dump_file
);
292 memcpy(selectfdp
, fdsetp
, fdmasks
); /* reinitialize */
294 if (do_dump
) { /* SIGUSR1 */
296 rtadvd_dump_file(dumpfilename
);
304 /* timer expiration check and reset the timer */
305 timeout
= rtadvd_check_timer();
307 if (timeout
!= NULL
) {
308 debuglog("<%s> set timer to %ld:%ld. waiting for "
309 "inputs or timeout", __func__
,
310 (long int)timeout
->tv_sec
,
311 (long int)timeout
->tv_usec
);
313 debuglog("<%s> there's no timer. waiting for inputs",
317 if ((i
= select(maxfd
+ 1, selectfdp
, NULL
, NULL
,
319 /* EINTR would occur upon SIGUSR1 for status dump */
321 errorlog( "<%s> select: %s",
322 __func__
, strerror(errno
));
325 if (i
== 0) /* timeout */
327 if (rtsock
!= -1 && FD_ISSET(rtsock
, selectfdp
))
329 if (FD_ISSET(sock
, selectfdp
))
332 exit(0); /* NOTREACHED */
336 rtadvd_set_dump_file(sig
)
354 const int retrans
= MAX_FINAL_RTR_ADVERTISEMENTS
;
357 debuglog("<%s> cease to be an advertising router\n",
361 for (ra
= ralist
; ra
; ra
= ra
->next
) {
365 for (i
= 0; i
< retrans
; i
++) {
366 for (ra
= ralist
; ra
; ra
= ra
->next
)
370 sleep(MIN_DELAY_BETWEEN_RAS
);
380 int n
, type
, ifindex
= 0, plen
;
382 char msg
[2048], *next
, *lim
;
383 char ifname
[IF_NAMESIZE
];
384 struct prefix
*prefix
;
386 struct in6_addr
*addr
;
387 char addrbuf
[INET6_ADDRSTRLEN
];
388 int prefixchange
= 0;
390 n
= read(rtsock
, msg
, sizeof(msg
));
392 debuglog( "<%s> received a routing message "
393 "(type = %d, len = %d)", __func__
, rtmsg_type(msg
), n
);
395 if (n
> rtmsg_len(msg
)) {
397 * This usually won't happen for messages received on
401 debuglog("<%s> received data length is larger than "
402 "1st routing message len. multiple messages? "
403 "read %d bytes, but 1st msg len = %d",
404 __func__
, n
, rtmsg_len(msg
));
412 for (next
= msg
; next
< lim
; next
+= len
) {
413 struct if_msghdr
* ifm
= NULL
;
416 next
= get_next_msg(next
, lim
, 0, &len
,
417 RTADV_TYPE2BITMASK(RTM_ADD
) |
418 RTADV_TYPE2BITMASK(RTM_DELETE
) |
419 RTADV_TYPE2BITMASK(RTM_NEWADDR
) |
420 RTADV_TYPE2BITMASK(RTM_DELADDR
) |
421 RTADV_TYPE2BITMASK(RTM_IFINFO
));
424 type
= rtmsg_type(next
);
428 ifindex
= get_rtm_ifindex(next
);
432 ifindex
= get_ifam_ifindex(next
);
435 ifindex
= get_ifm_ifindex(next
);
438 /* should not reach here */
440 debuglog("<%s:%d> unknown rtmsg %d on %s",
441 __func__
, __LINE__
, type
,
442 if_indextoname(ifindex
, ifname
));
447 if ((rai
= if_indextorainfo(ifindex
)) == NULL
) {
449 debuglog("<%s> route changed on "
450 "non advertising interface(%s)",
452 if_indextoname(ifindex
, ifname
));
456 ifm
= get_interface_entry(ifindex
);
458 debuglog("Couldn't find interface entry for %d. Skipping.", ifindex
);
461 oldifflags
= ifm
->ifm_flags
;
465 /* init ifflags because it may have changed */
467 if_getflags(ifindex
, ifm
->ifm_flags
);
470 break; /* we aren't interested in prefixes */
472 addr
= get_addr(msg
);
473 plen
= get_prefixlen(msg
);
474 /* sanity check for plen */
475 /* as RFC2373, prefixlen is at least 4 */
476 if (plen
< 4 || plen
> 127) {
477 infolog("<%s> new interface route's"
478 "plen %d is invalid for a prefix",
482 prefix
= find_prefix(rai
, addr
, plen
);
486 * If the prefix has been invalidated,
487 * make it available again.
489 update_prefix(prefix
);
491 } else if (dflag
> 1) {
492 debuglog("<%s> new prefix(%s/%d) "
494 "but it was already in list",
496 inet_ntop(AF_INET6
, addr
,
497 (char *)addrbuf
, INET6_ADDRSTRLEN
),
502 make_prefix(rai
, ifindex
, addr
, plen
);
506 /* init ifflags because it may have changed */
507 ifm
->ifm_flags
= if_getflags(ifindex
, ifm
->ifm_flags
);
512 addr
= get_addr(msg
);
513 plen
= get_prefixlen(msg
);
514 /* sanity check for plen */
515 /* as RFC2373, prefixlen is at least 4 */
516 if (plen
< 4 || plen
> 127) {
517 infolog("<%s> deleted interface route's "
518 "plen %d is invalid for a prefix",
522 prefix
= find_prefix(rai
, addr
, plen
);
523 if (prefix
== NULL
) {
525 debuglog("<%s> prefix(%s/%d) was "
527 "but it was not in list",
529 inet_ntop(AF_INET6
, addr
,
530 (char *)addrbuf
, INET6_ADDRSTRLEN
),
535 invalidate_prefix(prefix
);
540 /* init ifflags because it may have changed */
541 ifm
->ifm_flags
= if_getflags(ifindex
, ifm
->ifm_flags
);
544 ifm
->ifm_flags
= get_ifm_flags(next
);
547 /* should not reach here */
549 debuglog("<%s:%d> unknown rtmsg %d on %s",
550 __func__
, __LINE__
, type
,
551 if_indextoname(ifindex
, ifname
));
556 /* check if an interface flag is changed */
557 if ((oldifflags
& IFF_UP
) && /* UP to DOWN */
558 !(ifm
->ifm_flags
& IFF_UP
)) {
559 infolog("<%s> interface %s becomes down. stop timer.",
560 __func__
, rai
->ifname
);
561 rtadvd_remove_timer(&rai
->timer
);
562 } else if (!(oldifflags
& IFF_UP
) && /* DOWN to UP */
563 (ifm
->ifm_flags
& IFF_UP
)) {
564 infolog("<%s> interface %s becomes up. restart timer.",
565 __func__
, rai
->ifname
);
567 rai
->initcounter
= 0; /* reset the counter */
568 rai
->waiting
= 0; /* XXX */
569 rai
->timer
= rtadvd_add_timer(ra_timeout
,
570 ra_timer_update
, rai
, rai
);
571 ra_timer_update((void *)rai
, &rai
->timer
->tm
);
572 rtadvd_set_timer(&rai
->timer
->tm
, rai
->timer
);
573 } else if (prefixchange
&&
574 (ifm
->ifm_flags
& IFF_UP
)) {
576 * An advertised prefix has been added or invalidated.
577 * Will notice the change in a short delay.
579 rai
->initcounter
= 0;
580 set_short_delay(rai
);
595 struct icmp6_hdr
*icp
;
598 struct in6_pktinfo
*pi
= NULL
;
599 char ntopbuf
[INET6_ADDRSTRLEN
], ifnamebuf
[IFNAMSIZ
];
600 struct in6_addr dst
= in6addr_any
;
601 struct if_msghdr
*ifm
= NULL
;
604 * Get message. We reset msg_controllen since the field could
605 * be modified if we had received a message before setting
608 rcvmhdr
.msg_controllen
= rcvcmsgbuflen
;
609 if ((i
= recvmsg(sock
, &rcvmhdr
, 0)) < 0)
612 /* extract optional information via Advanced API */
613 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(&rcvmhdr
);
615 cm
= (struct cmsghdr
*)CMSG_NXTHDR(&rcvmhdr
, cm
)) {
616 if (cm
->cmsg_level
== IPPROTO_IPV6
&&
617 cm
->cmsg_type
== IPV6_PKTINFO
&&
618 cm
->cmsg_len
== CMSG_LEN(sizeof(struct in6_pktinfo
))) {
619 pi
= (struct in6_pktinfo
*)(CMSG_DATA(cm
));
620 ifindex
= pi
->ipi6_ifindex
;
623 if (cm
->cmsg_level
== IPPROTO_IPV6
&&
624 cm
->cmsg_type
== IPV6_HOPLIMIT
&&
625 cm
->cmsg_len
== CMSG_LEN(sizeof(int)))
626 hlimp
= (int *)CMSG_DATA(cm
);
629 errorlog("<%s> failed to get receiving interface",
634 errorlog("<%s> failed to get receiving hop limit",
639 ifm
= get_interface_entry(pi
->ipi6_ifindex
);
641 * If we happen to receive data on an interface which is now gone
642 * or down, just discard the data.
645 (ifm
->ifm_flags
& IFF_UP
) == 0) {
646 infolog("<%s> received data on a disabled interface (%s)",
648 (ifm
== NULL
) ? "[gone]" :
649 if_indextoname(pi
->ipi6_ifindex
, ifnamebuf
));
654 if (i
< sizeof(struct ip6_hdr
) + sizeof(struct icmp6_hdr
)) {
655 errorlog("<%s> packet size(%d) is too short",
660 ip
= (struct ip6_hdr
*)rcvmhdr
.msg_iov
[0].iov_base
;
661 icp
= (struct icmp6_hdr
*)(ip
+ 1); /* XXX: ext. hdr? */
663 if (i
< sizeof(struct icmp6_hdr
)) {
664 errorlog("<%s> packet size(%d) is too short",
669 icp
= (struct icmp6_hdr
*)rcvmhdr
.msg_iov
[0].iov_base
;
672 switch (icp
->icmp6_type
) {
673 case ND_ROUTER_SOLICIT
:
675 * Message verification - RFC-2461 6.1.1
676 * XXX: these checks must be done in the kernel as well,
677 * but we can't completely rely on them.
680 noticelog("<%s> RS with invalid hop limit(%d) "
681 "received from %s on %s",
683 inet_ntop(AF_INET6
, &rcvfrom
.sin6_addr
, ntopbuf
,
685 if_indextoname(pi
->ipi6_ifindex
, ifnamebuf
));
688 if (icp
->icmp6_code
) {
689 noticelog("<%s> RS with invalid ICMP6 code(%d) "
690 "received from %s on %s",
691 __func__
, icp
->icmp6_code
,
692 inet_ntop(AF_INET6
, &rcvfrom
.sin6_addr
, ntopbuf
,
694 if_indextoname(pi
->ipi6_ifindex
, ifnamebuf
));
697 if (i
< sizeof(struct nd_router_solicit
)) {
698 noticelog("<%s> RS from %s on %s does not have enough "
701 inet_ntop(AF_INET6
, &rcvfrom
.sin6_addr
, ntopbuf
,
703 if_indextoname(pi
->ipi6_ifindex
, ifnamebuf
), i
);
706 rs_input(i
, (struct nd_router_solicit
*)icp
, pi
, &rcvfrom
);
708 case ND_ROUTER_ADVERT
:
710 * Message verification - RFC-2461 6.1.2
711 * XXX: there's a same dilemma as above...
714 noticelog("<%s> RA with invalid hop limit(%d) "
715 "received from %s on %s",
717 inet_ntop(AF_INET6
, &rcvfrom
.sin6_addr
, ntopbuf
,
719 if_indextoname(pi
->ipi6_ifindex
, ifnamebuf
));
722 if (icp
->icmp6_code
) {
723 noticelog("<%s> RA with invalid ICMP6 code(%d) "
724 "received from %s on %s",
725 __func__
, icp
->icmp6_code
,
726 inet_ntop(AF_INET6
, &rcvfrom
.sin6_addr
, ntopbuf
,
728 if_indextoname(pi
->ipi6_ifindex
, ifnamebuf
));
731 if (i
< sizeof(struct nd_router_advert
)) {
732 noticelog("<%s> RA from %s on %s does not have enough "
735 inet_ntop(AF_INET6
, &rcvfrom
.sin6_addr
, ntopbuf
,
737 if_indextoname(pi
->ipi6_ifindex
, ifnamebuf
), i
);
740 ra_input(i
, (struct nd_router_advert
*)icp
, pi
, &rcvfrom
);
742 case ICMP6_ROUTER_RENUMBERING
:
743 if (accept_rr
== 0) {
744 errorlog("<%s> received a router renumbering "
745 "message, but not allowed to be accepted",
749 rr_input(i
, (struct icmp6_router_renum
*)icp
, pi
, &rcvfrom
,
754 * Note that this case is POSSIBLE, especially just
755 * after invocation of the daemon. This is because we
756 * could receive message after opening the socket and
757 * before setting ICMP6 type filter(see sock_open()).
759 errorlog("<%s> invalid icmp type(%d)",
760 __func__
, icp
->icmp6_type
);
768 rs_input(int len
, struct nd_router_solicit
*rs
,
769 struct in6_pktinfo
*pi
, struct sockaddr_in6
*from
)
771 char ntopbuf
[INET6_ADDRSTRLEN
], ifnamebuf
[IFNAMSIZ
];
772 union nd_opts ndopts
;
774 struct soliciter
*sol
;
777 "<%s> RS received from %s on %s",
779 inet_ntop(AF_INET6
, &from
->sin6_addr
,
780 ntopbuf
, INET6_ADDRSTRLEN
),
781 if_indextoname(pi
->ipi6_ifindex
, ifnamebuf
));
783 /* ND option check */
784 memset(&ndopts
, 0, sizeof(ndopts
));
785 if (nd6_options((struct nd_opt_hdr
*)(rs
+ 1),
786 len
- sizeof(struct nd_router_solicit
),
787 &ndopts
, NDOPT_FLAG_SRCLINKADDR
)) {
788 infolog("<%s> ND option check failed for an RS from %s on %s",
790 inet_ntop(AF_INET6
, &from
->sin6_addr
,
791 ntopbuf
, INET6_ADDRSTRLEN
),
792 if_indextoname(pi
->ipi6_ifindex
, ifnamebuf
));
797 * If the IP source address is the unspecified address, there
798 * must be no source link-layer address option in the message.
801 if (IN6_IS_ADDR_UNSPECIFIED(&from
->sin6_addr
) &&
802 ndopts
.nd_opts_src_lladdr
) {
803 infolog("<%s> RS from unspecified src on %s has a link-layer"
806 if_indextoname(pi
->ipi6_ifindex
, ifnamebuf
));
812 if (pi
->ipi6_ifindex
== ra
->ifindex
)
817 infolog("<%s> RS received on non advertising interface(%s)",
819 if_indextoname(pi
->ipi6_ifindex
, ifnamebuf
));
823 ra
->rsinput
++; /* increment statistics */
826 * Decide whether to send RA according to the rate-limit
830 /* record sockaddr waiting for RA, if possible */
831 sol
= (struct soliciter
*)malloc(sizeof(*sol
));
834 /* XXX RFC2553 need clarification on flowinfo */
835 sol
->addr
.sin6_flowinfo
= 0;
836 sol
->next
= ra
->soliciter
;
841 * If there is already a waiting RS packet, don't
850 free_ndopts(&ndopts
);
858 long delay
; /* must not be greater than 1000000 */
859 struct timeval interval
, now
, min_delay
, tm_tmp
, *rest
;
861 if (rai
->timer
== NULL
)
864 * Compute a random delay. If the computed value
865 * corresponds to a time later than the time the next
866 * multicast RA is scheduled to be sent, ignore the random
867 * delay and send the advertisement at the
868 * already-scheduled time. RFC-2461 6.2.6
870 #ifdef HAVE_ARC4RANDOM
871 delay
= arc4random_uniform(MAX_RA_DELAY_TIME
);
873 delay
= random() % MAX_RA_DELAY_TIME
;
876 interval
.tv_usec
= delay
;
877 rest
= rtadvd_timer_rest(rai
->timer
);
878 if (TIMEVAL_LT(*rest
, interval
)) {
879 debuglog("<%s> random delay is larger than "
880 "the rest of the current timer", __func__
);
885 * If we sent a multicast Router Advertisement within
886 * the last MIN_DELAY_BETWEEN_RAS seconds, schedule
887 * the advertisement to be sent at a time corresponding to
888 * MIN_DELAY_BETWEEN_RAS plus the random value after the
889 * previous advertisement was sent.
891 gettimeofday(&now
, NULL
);
892 TIMEVAL_SUB(&now
, &rai
->lastsent
, &tm_tmp
);
893 min_delay
.tv_sec
= MIN_DELAY_BETWEEN_RAS
;
894 min_delay
.tv_usec
= 0;
895 if (TIMEVAL_LT(tm_tmp
, min_delay
)) {
896 TIMEVAL_SUB(&min_delay
, &tm_tmp
, &min_delay
);
897 TIMEVAL_ADD(&min_delay
, &interval
, &interval
);
899 rtadvd_set_timer(&interval
, rai
->timer
);
903 ra_input(int len
, struct nd_router_advert
*ra
,
904 struct in6_pktinfo
*pi
, struct sockaddr_in6
*from
)
907 char ntopbuf
[INET6_ADDRSTRLEN
], ifnamebuf
[IFNAMSIZ
];
908 union nd_opts ndopts
;
909 char *on_off
[] = {"OFF", "ON"};
910 u_int32_t reachabletime
, retranstimer
, mtu
;
911 int inconsistent
= 0;
913 debuglog("<%s> RA received from %s on %s",
915 inet_ntop(AF_INET6
, &from
->sin6_addr
,
916 ntopbuf
, INET6_ADDRSTRLEN
),
917 if_indextoname(pi
->ipi6_ifindex
, ifnamebuf
));
919 /* ND option check */
920 memset(&ndopts
, 0, sizeof(ndopts
));
921 if (nd6_options((struct nd_opt_hdr
*)(ra
+ 1),
922 len
- sizeof(struct nd_router_advert
),
923 &ndopts
, NDOPT_FLAG_SRCLINKADDR
|
924 NDOPT_FLAG_PREFIXINFO
| NDOPT_FLAG_MTU
)) {
925 infolog("<%s> ND option check failed for an RA from %s on %s",
927 inet_ntop(AF_INET6
, &from
->sin6_addr
,
928 ntopbuf
, INET6_ADDRSTRLEN
),
929 if_indextoname(pi
->ipi6_ifindex
, ifnamebuf
));
934 * RA consistency check according to RFC-2461 6.2.7
936 if ((rai
= if_indextorainfo(pi
->ipi6_ifindex
)) == 0) {
937 infolog("<%s> received RA from %s on non-advertising"
940 inet_ntop(AF_INET6
, &from
->sin6_addr
,
941 ntopbuf
, INET6_ADDRSTRLEN
),
942 if_indextoname(pi
->ipi6_ifindex
, ifnamebuf
));
945 rai
->rainput
++; /* increment statistics */
947 /* Cur Hop Limit value */
948 if (ra
->nd_ra_curhoplimit
&& rai
->hoplimit
&&
949 ra
->nd_ra_curhoplimit
!= rai
->hoplimit
) {
950 infolog("<%s> CurHopLimit inconsistent on %s:"
951 " %d from %s, %d from us",
954 ra
->nd_ra_curhoplimit
,
955 inet_ntop(AF_INET6
, &from
->sin6_addr
,
956 ntopbuf
, INET6_ADDRSTRLEN
),
961 if ((ra
->nd_ra_flags_reserved
& ND_RA_FLAG_MANAGED
) !=
963 infolog("<%s> M flag inconsistent on %s:"
964 " %s from %s, %s from us",
967 on_off
[!rai
->managedflg
],
968 inet_ntop(AF_INET6
, &from
->sin6_addr
,
969 ntopbuf
, INET6_ADDRSTRLEN
),
970 on_off
[rai
->managedflg
]);
974 if ((ra
->nd_ra_flags_reserved
& ND_RA_FLAG_OTHER
) !=
976 infolog("<%s> O flag inconsistent on %s:"
977 " %s from %s, %s from us",
980 on_off
[!rai
->otherflg
],
981 inet_ntop(AF_INET6
, &from
->sin6_addr
,
982 ntopbuf
, INET6_ADDRSTRLEN
),
983 on_off
[rai
->otherflg
]);
987 reachabletime
= ntohl(ra
->nd_ra_reachable
);
988 if (reachabletime
&& rai
->reachabletime
&&
989 reachabletime
!= rai
->reachabletime
) {
990 infolog("<%s> ReachableTime inconsistent on %s:"
991 " %d from %s, %d from us",
995 inet_ntop(AF_INET6
, &from
->sin6_addr
,
996 ntopbuf
, INET6_ADDRSTRLEN
),
1001 retranstimer
= ntohl(ra
->nd_ra_retransmit
);
1002 if (retranstimer
&& rai
->retranstimer
&&
1003 retranstimer
!= rai
->retranstimer
) {
1004 infolog("<%s> RetranceTimer inconsistent on %s:"
1005 " %d from %s, %d from us",
1009 inet_ntop(AF_INET6
, &from
->sin6_addr
,
1010 ntopbuf
, INET6_ADDRSTRLEN
),
1014 /* Values in the MTU options */
1015 if (ndopts
.nd_opts_mtu
) {
1016 mtu
= ntohl(ndopts
.nd_opts_mtu
->nd_opt_mtu_mtu
);
1017 if (mtu
&& rai
->linkmtu
&& mtu
!= rai
->linkmtu
) {
1018 infolog("<%s> MTU option value inconsistent on %s:"
1019 " %d from %s, %d from us",
1022 inet_ntop(AF_INET6
, &from
->sin6_addr
,
1023 ntopbuf
, INET6_ADDRSTRLEN
),
1028 /* Preferred and Valid Lifetimes for prefixes */
1030 struct nd_optlist
*optp
= ndopts
.nd_opts_list
;
1032 if (ndopts
.nd_opts_pi
) {
1033 if (prefix_check(ndopts
.nd_opts_pi
, rai
, from
))
1037 if (prefix_check((struct nd_opt_prefix_info
*)optp
->opt
,
1045 rai
->rainconsistent
++;
1048 free_ndopts(&ndopts
);
1052 /* return a non-zero value if the received prefix is inconsitent with ours */
1054 prefix_check(struct nd_opt_prefix_info
*pinfo
,
1055 struct rainfo
*rai
, struct sockaddr_in6
*from
)
1057 u_int32_t preferred_time
, valid_time
;
1059 int inconsistent
= 0;
1060 char ntopbuf
[INET6_ADDRSTRLEN
], prefixbuf
[INET6_ADDRSTRLEN
];
1063 #if 0 /* impossible */
1064 if (pinfo
->nd_opt_pi_type
!= ND_OPT_PREFIX_INFORMATION
)
1069 * log if the adveritsed prefix has link-local scope(sanity check?)
1071 if (IN6_IS_ADDR_LINKLOCAL(&pinfo
->nd_opt_pi_prefix
)) {
1072 infolog("<%s> link-local prefix %s/%d is advertised "
1075 inet_ntop(AF_INET6
, &pinfo
->nd_opt_pi_prefix
,
1076 prefixbuf
, INET6_ADDRSTRLEN
),
1077 pinfo
->nd_opt_pi_prefix_len
,
1078 inet_ntop(AF_INET6
, &from
->sin6_addr
,
1079 ntopbuf
, INET6_ADDRSTRLEN
),
1083 if ((pp
= find_prefix(rai
, &pinfo
->nd_opt_pi_prefix
,
1084 pinfo
->nd_opt_pi_prefix_len
)) == NULL
) {
1085 infolog("<%s> prefix %s/%d from %s on %s is not in our list",
1087 inet_ntop(AF_INET6
, &pinfo
->nd_opt_pi_prefix
,
1088 prefixbuf
, INET6_ADDRSTRLEN
),
1089 pinfo
->nd_opt_pi_prefix_len
,
1090 inet_ntop(AF_INET6
, &from
->sin6_addr
,
1091 ntopbuf
, INET6_ADDRSTRLEN
),
1096 preferred_time
= ntohl(pinfo
->nd_opt_pi_preferred_time
);
1097 if (pp
->pltimeexpire
) {
1099 * The lifetime is decremented in real time, so we should
1100 * compare the expiration time.
1101 * (RFC 2461 Section 6.2.7.)
1102 * XXX: can we really expect that all routers on the link
1103 * have synchronized clocks?
1105 gettimeofday(&now
, NULL
);
1106 preferred_time
+= now
.tv_sec
;
1108 if (!pp
->timer
&& rai
->clockskew
&&
1109 preferred_time
- pp
->pltimeexpire
> rai
->clockskew
) {
1110 infolog("<%s> preferred lifetime for %s/%d"
1111 " (decr. in real time) inconsistent on %s:"
1112 " %d from %s, %ld from us",
1114 inet_ntop(AF_INET6
, &pinfo
->nd_opt_pi_prefix
,
1115 prefixbuf
, INET6_ADDRSTRLEN
),
1116 pinfo
->nd_opt_pi_prefix_len
,
1117 rai
->ifname
, preferred_time
,
1118 inet_ntop(AF_INET6
, &from
->sin6_addr
,
1119 ntopbuf
, INET6_ADDRSTRLEN
),
1123 } else if (!pp
->timer
&& preferred_time
!= pp
->preflifetime
) {
1124 infolog("<%s> preferred lifetime for %s/%d"
1125 " inconsistent on %s:"
1126 " %d from %s, %d from us",
1128 inet_ntop(AF_INET6
, &pinfo
->nd_opt_pi_prefix
,
1129 prefixbuf
, INET6_ADDRSTRLEN
),
1130 pinfo
->nd_opt_pi_prefix_len
,
1131 rai
->ifname
, preferred_time
,
1132 inet_ntop(AF_INET6
, &from
->sin6_addr
,
1133 ntopbuf
, INET6_ADDRSTRLEN
),
1137 valid_time
= ntohl(pinfo
->nd_opt_pi_valid_time
);
1138 if (pp
->vltimeexpire
) {
1139 gettimeofday(&now
, NULL
);
1140 valid_time
+= now
.tv_sec
;
1142 if (!pp
->timer
&& rai
->clockskew
&&
1143 valid_time
- pp
->vltimeexpire
> rai
->clockskew
) {
1144 infolog("<%s> valid lifetime for %s/%d"
1145 " (decr. in real time) inconsistent on %s:"
1146 " %d from %s, %ld from us",
1148 inet_ntop(AF_INET6
, &pinfo
->nd_opt_pi_prefix
,
1149 prefixbuf
, INET6_ADDRSTRLEN
),
1150 pinfo
->nd_opt_pi_prefix_len
,
1151 rai
->ifname
, preferred_time
,
1152 inet_ntop(AF_INET6
, &from
->sin6_addr
,
1153 ntopbuf
, INET6_ADDRSTRLEN
),
1157 } else if (!pp
->timer
&& valid_time
!= pp
->validlifetime
) {
1158 infolog("<%s> valid lifetime for %s/%d"
1159 " inconsistent on %s:"
1160 " %d from %s, %d from us",
1162 inet_ntop(AF_INET6
, &pinfo
->nd_opt_pi_prefix
,
1163 prefixbuf
, INET6_ADDRSTRLEN
),
1164 pinfo
->nd_opt_pi_prefix_len
,
1165 rai
->ifname
, valid_time
,
1166 inet_ntop(AF_INET6
, &from
->sin6_addr
,
1167 ntopbuf
, INET6_ADDRSTRLEN
),
1172 return(inconsistent
);
1176 find_prefix(struct rainfo
*rai
, struct in6_addr
*prefix
, int plen
)
1179 int bytelen
, bitlen
;
1182 for (pp
= rai
->prefix
.next
; pp
!= &rai
->prefix
; pp
= pp
->next
) {
1183 if (plen
!= pp
->prefixlen
)
1187 bitmask
= 0xff << (8 - bitlen
);
1188 if (memcmp((void *)prefix
, (void *)&pp
->prefix
, bytelen
))
1191 ((prefix
->s6_addr
[bytelen
] & bitmask
) ==
1192 (pp
->prefix
.s6_addr
[bytelen
] & bitmask
))) {
1200 /* check if p0/plen0 matches p1/plen1; return 1 if matches, otherwise 0. */
1202 prefix_match(struct in6_addr
*p0
, int plen0
,
1203 struct in6_addr
*p1
, int plen1
)
1205 int bytelen
, bitlen
;
1210 bytelen
= plen1
/ 8;
1212 bitmask
= 0xff << (8 - bitlen
);
1213 if (memcmp((void *)p0
, (void *)p1
, bytelen
))
1216 ((p0
->s6_addr
[bytelen
] & bitmask
) ==
1217 (p1
->s6_addr
[bytelen
] & bitmask
))) {
1225 nd6_options(struct nd_opt_hdr
*hdr
, int limit
,
1226 union nd_opts
*ndopts
, u_int32_t optflags
)
1230 for (; limit
> 0; limit
-= optlen
) {
1231 if (limit
< sizeof(struct nd_opt_hdr
)) {
1232 infolog("<%s> short option header", __func__
);
1236 hdr
= (struct nd_opt_hdr
*)((caddr_t
)hdr
+ optlen
);
1237 if (hdr
->nd_opt_len
== 0) {
1238 infolog("<%s> bad ND option length(0) (type = %d)",
1239 __func__
, hdr
->nd_opt_type
);
1242 optlen
= hdr
->nd_opt_len
<< 3;
1243 if (optlen
> limit
) {
1244 infolog("<%s> short option", __func__
);
1248 if (hdr
->nd_opt_type
> ND_OPT_MTU
) {
1249 infolog("<%s> unknown ND option(type %d)",
1250 __func__
, hdr
->nd_opt_type
);
1254 if ((ndopt_flags
[hdr
->nd_opt_type
] & optflags
) == 0) {
1255 infolog("<%s> unexpected ND option(type %d)",
1256 __func__
, hdr
->nd_opt_type
);
1261 * Option length check. Do it here for all fixed-length
1264 if ((hdr
->nd_opt_type
== ND_OPT_MTU
&&
1265 (optlen
!= sizeof(struct nd_opt_mtu
))) ||
1266 ((hdr
->nd_opt_type
== ND_OPT_PREFIX_INFORMATION
&&
1267 optlen
!= sizeof(struct nd_opt_prefix_info
)))) {
1268 infolog("<%s> invalid option length",
1273 switch (hdr
->nd_opt_type
) {
1274 case ND_OPT_TARGET_LINKADDR
:
1275 case ND_OPT_REDIRECTED_HEADER
:
1276 break; /* we don't care about these options */
1277 case ND_OPT_SOURCE_LINKADDR
:
1279 if (ndopts
->nd_opt_array
[hdr
->nd_opt_type
]) {
1280 infolog("<%s> duplicated ND option (type = %d)",
1281 __func__
, hdr
->nd_opt_type
);
1283 ndopts
->nd_opt_array
[hdr
->nd_opt_type
] = hdr
;
1285 case ND_OPT_PREFIX_INFORMATION
:
1287 struct nd_optlist
*pfxlist
;
1289 if (ndopts
->nd_opts_pi
== 0) {
1290 ndopts
->nd_opts_pi
=
1291 (struct nd_opt_prefix_info
*)hdr
;
1294 if ((pfxlist
= malloc(sizeof(*pfxlist
))) == NULL
) {
1295 errorlog("<%s> can't allocate memory",
1299 pfxlist
->next
= ndopts
->nd_opts_list
;
1301 ndopts
->nd_opts_list
= pfxlist
;
1305 default: /* impossible */
1313 free_ndopts(ndopts
);
1319 free_ndopts(union nd_opts
*ndopts
)
1321 struct nd_optlist
*opt
= ndopts
->nd_opts_list
, *next
;
1333 struct icmp6_filter filt
;
1334 struct ipv6_mreq mreq
;
1335 struct rainfo
*ra
= ralist
;
1337 /* XXX: should be max MTU attached to the node */
1338 static u_char answer
[1500];
1340 rcvcmsgbuflen
= CMSG_SPACE(sizeof(struct in6_pktinfo
)) +
1341 CMSG_SPACE(sizeof(int));
1342 rcvcmsgbuf
= (u_char
*)malloc(rcvcmsgbuflen
);
1343 if (rcvcmsgbuf
== NULL
) {
1344 errorlog("<%s> not enough core", __func__
);
1348 sndcmsgbuflen
= CMSG_SPACE(sizeof(struct in6_pktinfo
)) +
1349 CMSG_SPACE(sizeof(int));
1350 sndcmsgbuf
= (u_char
*)malloc(sndcmsgbuflen
);
1351 if (sndcmsgbuf
== NULL
) {
1352 errorlog("<%s> not enough core", __func__
);
1356 if ((sock
= socket(AF_INET6
, SOCK_RAW
, IPPROTO_ICMPV6
)) < 0) {
1357 errorlog("<%s> socket: %s", __func__
,
1362 (void) setsockopt(sock
, SOL_SOCKET
, SO_TRAFFIC_CLASS
,
1363 (void *)&so_traffic_class
, sizeof (so_traffic_class
));
1365 /* specify to tell receiving interface */
1367 #ifdef IPV6_RECVPKTINFO
1368 if (setsockopt(sock
, IPPROTO_IPV6
, IPV6_RECVPKTINFO
, &on
,
1370 errorlog("<%s> IPV6_RECVPKTINFO: %s",
1371 __func__
, strerror(errno
));
1374 #else /* old adv. API */
1375 if (setsockopt(sock
, IPPROTO_IPV6
, IPV6_PKTINFO
, &on
,
1377 errorlog("<%s> IPV6_PKTINFO: %s",
1378 __func__
, strerror(errno
));
1384 /* specify to tell value of hoplimit field of received IP6 hdr */
1385 #ifdef IPV6_RECVHOPLIMIT
1386 if (setsockopt(sock
, IPPROTO_IPV6
, IPV6_RECVHOPLIMIT
, &on
,
1388 errorlog( "<%s> IPV6_RECVHOPLIMIT: %s",
1389 __func__
, strerror(errno
));
1392 #else /* old adv. API */
1393 if (setsockopt(sock
, IPPROTO_IPV6
, IPV6_HOPLIMIT
, &on
,
1395 errorlog("<%s> IPV6_HOPLIMIT: %s",
1396 __func__
, strerror(errno
));
1402 if (setsockopt(sock
, IPPROTO_IPV6
, IPV6_DONTFRAG
, &on
,
1404 errorlog("<%s> IPV6_DONTFRAG: %s",
1405 __func__
, strerror(errno
));
1409 ICMP6_FILTER_SETBLOCKALL(&filt
);
1410 ICMP6_FILTER_SETPASS(ND_ROUTER_SOLICIT
, &filt
);
1411 ICMP6_FILTER_SETPASS(ND_ROUTER_ADVERT
, &filt
);
1413 ICMP6_FILTER_SETPASS(ICMP6_ROUTER_RENUMBERING
, &filt
);
1414 if (setsockopt(sock
, IPPROTO_ICMPV6
, ICMP6_FILTER
, &filt
,
1415 sizeof(filt
)) < 0) {
1416 errorlog("<%s> IICMP6_FILTER: %s",
1417 __func__
, strerror(errno
));
1422 * join all routers multicast address on each advertising interface.
1424 if (inet_pton(AF_INET6
, ALLROUTERS_LINK
,
1425 &mreq
.ipv6mr_multiaddr
.s6_addr
)
1427 errorlog("<%s> inet_pton failed(library bug?)",
1432 mreq
.ipv6mr_interface
= ra
->ifindex
;
1433 if (setsockopt(sock
, IPPROTO_IPV6
, IPV6_JOIN_GROUP
, &mreq
,
1434 sizeof(mreq
)) < 0) {
1435 errorlog("<%s> IPV6_JOIN_GROUP(link) on %s: %s",
1436 __func__
, ra
->ifname
, strerror(errno
));
1443 * When attending router renumbering, join all-routers site-local
1447 if (inet_pton(AF_INET6
, ALLROUTERS_SITE
,
1448 &in6a_site_allrouters
) != 1) {
1449 errorlog("<%s> inet_pton failed(library bug?)",
1453 mreq
.ipv6mr_multiaddr
= in6a_site_allrouters
;
1455 if ((mreq
.ipv6mr_interface
= if_nametoindex(mcastif
))
1457 errorlog("<%s> invalid interface: %s",
1462 mreq
.ipv6mr_interface
= ralist
->ifindex
;
1463 if (setsockopt(sock
, IPPROTO_IPV6
, IPV6_JOIN_GROUP
,
1464 &mreq
, sizeof(mreq
)) < 0) {
1465 errorlog("<%s> IPV6_JOIN_GROUP(site) on %s: %s",
1467 mcastif
? mcastif
: ralist
->ifname
,
1473 /* initialize msghdr for receiving packets */
1474 rcviov
[0].iov_base
= (caddr_t
)answer
;
1475 rcviov
[0].iov_len
= sizeof(answer
);
1476 rcvmhdr
.msg_name
= (caddr_t
)&rcvfrom
;
1477 rcvmhdr
.msg_namelen
= sizeof(rcvfrom
);
1478 rcvmhdr
.msg_iov
= rcviov
;
1479 rcvmhdr
.msg_iovlen
= 1;
1480 rcvmhdr
.msg_control
= (caddr_t
) rcvcmsgbuf
;
1481 rcvmhdr
.msg_controllen
= rcvcmsgbuflen
;
1483 /* initialize msghdr for sending packets */
1484 sndmhdr
.msg_namelen
= sizeof(struct sockaddr_in6
);
1485 sndmhdr
.msg_iov
= sndiov
;
1486 sndmhdr
.msg_iovlen
= 1;
1487 sndmhdr
.msg_control
= (caddr_t
)sndcmsgbuf
;
1488 sndmhdr
.msg_controllen
= sndcmsgbuflen
;
1493 /* open a routing socket to watch the routing table */
1497 if ((rtsock
= socket(PF_ROUTE
, SOCK_RAW
, 0)) < 0) {
1498 errorlog("<%s> socket: %s", __func__
, strerror(errno
));
1504 if_indextorainfo(int idx
)
1506 struct rainfo
*rai
= ralist
;
1508 for (rai
= ralist
; rai
; rai
= rai
->next
) {
1509 if (rai
->ifindex
== idx
)
1513 return(NULL
); /* search failed */
1518 struct rainfo
*rainfo
;
1522 struct in6_pktinfo
*pi
;
1523 struct soliciter
*sol
, *nextsol
;
1524 struct if_msghdr
*ifm
= get_interface_entry(rainfo
->ifindex
);
1527 (ifm
->ifm_flags
& IFF_UP
) == 0) {
1528 debuglog("<%s> %s is not up, skip sending RA",
1529 __func__
, rainfo
->ifname
);
1533 make_packet(rainfo
); /* XXX: inefficient */
1535 sndmhdr
.msg_name
= (caddr_t
)&sin6_allnodes
;
1536 sndmhdr
.msg_iov
[0].iov_base
= (caddr_t
)rainfo
->ra_data
;
1537 sndmhdr
.msg_iov
[0].iov_len
= rainfo
->ra_datalen
;
1539 cm
= CMSG_FIRSTHDR(&sndmhdr
);
1540 /* specify the outgoing interface */
1541 cm
->cmsg_level
= IPPROTO_IPV6
;
1542 cm
->cmsg_type
= IPV6_PKTINFO
;
1543 cm
->cmsg_len
= CMSG_LEN(sizeof(struct in6_pktinfo
));
1544 pi
= (struct in6_pktinfo
*)CMSG_DATA(cm
);
1545 memset(&pi
->ipi6_addr
, 0, sizeof(pi
->ipi6_addr
)); /*XXX*/
1546 pi
->ipi6_ifindex
= rainfo
->ifindex
;
1548 /* specify the hop limit of the packet */
1552 cm
= CMSG_NXTHDR(&sndmhdr
, cm
);
1553 cm
->cmsg_level
= IPPROTO_IPV6
;
1554 cm
->cmsg_type
= IPV6_HOPLIMIT
;
1555 cm
->cmsg_len
= CMSG_LEN(sizeof(int));
1556 memcpy(CMSG_DATA(cm
), &hoplimit
, sizeof(int));
1559 debuglog("<%s> send RA on %s, # of waitings = %d",
1560 __func__
, rainfo
->ifname
, rainfo
->waiting
);
1562 i
= sendmsg(sock
, &sndmhdr
, 0);
1564 if (i
< 0 || i
!= rainfo
->ra_datalen
) {
1566 errorlog("<%s> sendmsg on %s: %s",
1567 __func__
, rainfo
->ifname
,
1571 /* update counter */
1572 if (rainfo
->initcounter
< MAX_INITIAL_RTR_ADVERTISEMENTS
)
1573 rainfo
->initcounter
++;
1577 * unicast advertisements
1578 * XXX commented out. reason: though spec does not forbit it, unicast
1579 * advert does not really help
1581 for (sol
= rainfo
->soliciter
; sol
; sol
= nextsol
) {
1582 nextsol
= sol
->next
;
1587 rainfo
->soliciter
= NULL
;
1589 /* update timestamp */
1590 gettimeofday(&rainfo
->lastsent
, NULL
);
1592 /* reset waiting conter */
1593 rainfo
->waiting
= 0;
1596 /* process RA timer */
1597 struct rtadvd_timer
*
1598 ra_timeout(void *data
)
1600 struct rainfo
*rai
= (struct rainfo
*)data
;
1603 /* if necessary, reconstruct the packet. */
1606 debuglog("<%s> RA timer on %s is expired",
1607 __func__
, rai
->ifname
);
1614 /* update RA timer */
1616 ra_timer_update(void *data
, struct timeval
*tm
)
1618 struct rainfo
*rai
= (struct rainfo
*)data
;
1622 * Whenever a multicast advertisement is sent from an interface,
1623 * the timer is reset to a uniformly-distributed random value
1624 * between the interface's configured MinRtrAdvInterval and
1625 * MaxRtrAdvInterval (RFC2461 6.2.4).
1627 interval
= rai
->mininterval
;
1628 interval
+= random() % (rai
->maxinterval
- rai
->mininterval
);
1631 * The first advertisement is sent as soon as rtadvd starts up
1632 * and for the next few advertisements (up to
1633 * MAX_INITIAL_RTR_ADVERTISEMENTS), if the randomly chosen interval
1634 * is greater than MAX_INITIAL_RTR_ADVERT_INTERVAL, the timer
1635 * SHOULD be set to MAX_INITIAL_RTR_ADVERT_INTERVAL instead.
1638 if (rai
->initcounter
< MAX_INITIAL_RTR_ADVERTISEMENTS
&&
1639 interval
> MAX_INITIAL_RTR_ADVERT_INTERVAL
)
1640 interval
= MAX_INITIAL_RTR_ADVERT_INTERVAL
;
1642 tm
->tv_sec
= rai
->initcounter
== 0 ? 0 : interval
;
1645 debuglog("<%s> RA timer on %s is set to %ld:%ld",
1646 __func__
, rai
->ifname
,
1647 (long int)tm
->tv_sec
, (long int)tm
->tv_usec
);