1 /* $NetBSD: rtadvd.c,v 1.50 2015/06/15 04:15:33 ozaki-r Exp $ */
2 /* $KAME: rtadvd.c,v 1.92 2005/10/17 14:40:02 suz Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 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/socket.h>
37 #include <sys/queue.h>
40 #include <net/route.h>
41 #include <net/if_dl.h>
42 #include <netinet/in.h>
43 #include <netinet/ip6.h>
44 #include <netinet6/ip6_var.h>
45 #include <netinet/icmp6.h>
47 #include <arpa/inet.h>
57 #if defined(__NetBSD__) || defined(__minix)
71 struct msghdr rcvmhdr
;
72 static unsigned char *rcvcmsgbuf
;
73 static size_t rcvcmsgbuflen
;
74 static unsigned char *sndcmsgbuf
;
75 static size_t sndcmsgbuflen
;
76 volatile sig_atomic_t do_dump
;
77 volatile sig_atomic_t do_reconf
;
78 volatile sig_atomic_t do_die
;
79 struct msghdr sndmhdr
;
80 struct iovec rcviov
[2];
81 struct iovec sndiov
[2];
82 struct sockaddr_in6 rcvfrom
;
83 static const char *dumpfilename
= "/var/run/rtadvd.dump"; /* XXX configurable */
88 int dflag
= 0, sflag
= 0;
90 static char **if_argv
;
93 char *conffile
= NULL
;
95 struct ralist_head_t ralist
= TAILQ_HEAD_INITIALIZER(ralist
);
98 TAILQ_ENTRY(nd_optlist
) next
;
99 struct nd_opt_hdr
*opt
;
102 struct nd_opt_hdr
*nd_opt_array
[9];
104 struct nd_opt_hdr
*zero
;
105 struct nd_opt_hdr
*src_lladdr
;
106 struct nd_opt_hdr
*tgt_lladdr
;
107 struct nd_opt_prefix_info
*pi
;
108 struct nd_opt_rd_hdr
*rh
;
109 struct nd_opt_mtu
*mtu
;
110 TAILQ_HEAD(, nd_optlist
) list
;
113 #define nd_opts_src_lladdr nd_opt_each.src_lladdr
114 #define nd_opts_tgt_lladdr nd_opt_each.tgt_lladdr
115 #define nd_opts_pi nd_opt_each.pi
116 #define nd_opts_rh nd_opt_each.rh
117 #define nd_opts_mtu nd_opt_each.mtu
118 #define nd_opts_list nd_opt_each.list
120 #define NDOPT_FLAG_SRCLINKADDR (1 << 0)
121 #define NDOPT_FLAG_TGTLINKADDR (1 << 1)
122 #define NDOPT_FLAG_PREFIXINFO (1 << 2)
123 #define NDOPT_FLAG_RDHDR (1 << 3)
124 #define NDOPT_FLAG_MTU (1 << 4)
125 #define NDOPT_FLAG_RDNSS (1 << 5)
126 #define NDOPT_FLAG_DNSSL (1 << 6)
128 uint32_t ndopt_flags
[] = {
129 [ND_OPT_SOURCE_LINKADDR
] = NDOPT_FLAG_SRCLINKADDR
,
130 [ND_OPT_TARGET_LINKADDR
] = NDOPT_FLAG_TGTLINKADDR
,
131 [ND_OPT_PREFIX_INFORMATION
] = NDOPT_FLAG_PREFIXINFO
,
132 [ND_OPT_REDIRECTED_HEADER
] = NDOPT_FLAG_RDHDR
,
133 [ND_OPT_MTU
] = NDOPT_FLAG_MTU
,
134 [ND_OPT_RDNSS
] = NDOPT_FLAG_RDNSS
,
135 [ND_OPT_DNSSL
] = NDOPT_FLAG_DNSSL
,
138 struct sockaddr_in6 sin6_linklocal_allnodes
= {
139 .sin6_len
= sizeof(sin6_linklocal_allnodes
),
140 .sin6_family
= AF_INET6
,
141 .sin6_addr
= IN6ADDR_LINKLOCAL_ALLNODES_INIT
,
144 struct sockaddr_in6 sin6_linklocal_allrouters
= {
145 .sin6_len
= sizeof(sin6_linklocal_allrouters
),
146 .sin6_family
= AF_INET6
,
147 .sin6_addr
= IN6ADDR_LINKLOCAL_ALLROUTERS_INIT
,
150 struct sockaddr_in6 sin6_sitelocal_allrouters
= {
151 .sin6_len
= sizeof(sin6_sitelocal_allrouters
),
152 .sin6_family
= AF_INET6
,
153 .sin6_addr
= IN6ADDR_SITELOCAL_ALLROUTERS_INIT
,
156 static void set_die(int);
157 static void die(void);
158 static void set_reconf(int);
159 static void sock_open(void);
160 static void rtsock_open(void);
161 static void rtadvd_input(void);
162 static void rs_input(int, struct nd_router_solicit
*,
163 struct in6_pktinfo
*, struct sockaddr_in6
*);
164 static void ra_input(int, struct nd_router_advert
*,
165 struct in6_pktinfo
*, struct sockaddr_in6
*);
166 static struct rainfo
*ra_output(struct rainfo
*);
167 static int prefix_check(struct nd_opt_prefix_info
*, struct rainfo
*,
168 struct sockaddr_in6
*);
169 static int nd6_options(struct nd_opt_hdr
*, int, union nd_opts
*, uint32_t);
170 static void free_ndopts(union nd_opts
*);
171 static void rtmsg_input(void);
172 static void rtadvd_set_dump_file(int);
175 main(int argc
, char *argv
[])
177 struct pollfd set
[2];
178 struct timespec
*timeout
;
180 int fflag
= 0, logopt
;
183 /* get command line options and arguments */
184 #define OPTIONS "c:dDfM:Rs"
185 while ((ch
= getopt(argc
, argv
, OPTIONS
)) != -1) {
204 fprintf(stderr
, "rtadvd: "
205 "the -R option is currently ignored.\n");
218 "usage: rtadvd [-DdfRs] [-c conffile]"
219 " [-M ifname] interface ...\n");
223 logopt
= LOG_NDELAY
| LOG_PID
;
225 logopt
|= LOG_PERROR
;
226 openlog("rtadvd", logopt
, LOG_DAEMON
);
230 (void)setlogmask(LOG_UPTO(LOG_ERR
));
232 (void)setlogmask(LOG_UPTO(LOG_INFO
));
234 errno
= 0; /* Ensure errno is 0 so we know if getpwnam errors or not */
235 if ((pw
= getpwnam(RTADVD_USER
)) == NULL
) {
238 "user %s does not exist, aborting",
241 syslog(LOG_ERR
, "getpwnam: %s: %m", RTADVD_USER
);
245 /* timer initialization */
251 getconfig(*argv
++, 1);
258 #if defined(__NetBSD__) || defined(__minix)
259 /* record the current PID */
260 if (pidfile(NULL
) < 0) {
262 "<%s> failed to open the pid log file, run anyway.",
268 set
[0].events
= POLLIN
;
272 set
[1].events
= POLLIN
;
276 syslog(LOG_INFO
, "dropping privileges to %s", RTADVD_USER
);
277 if (chroot(pw
->pw_dir
) == -1) {
278 syslog(LOG_ERR
, "chroot: %s: %m", pw
->pw_dir
);
281 if (chdir("/") == -1) {
282 syslog(LOG_ERR
, "chdir: /: %m");
285 if (setgroups(1, &pw
->pw_gid
) == -1 ||
286 setgid(pw
->pw_gid
) == -1 ||
287 setuid(pw
->pw_uid
) == -1)
289 syslog(LOG_ERR
, "failed to drop privileges: %m");
293 signal(SIGINT
, set_die
);
294 signal(SIGTERM
, set_die
);
295 signal(SIGHUP
, set_reconf
);
296 signal(SIGUSR1
, rtadvd_set_dump_file
);
299 if (do_dump
) { /* SIGUSR1 */
301 rtadvd_dump_file(dumpfilename
);
304 if (do_reconf
) { /* SIGHUP */
306 syslog(LOG_INFO
, "<%s> reloading config on SIGHUP",
311 getconfig(*argv
++, 0);
314 /* timer expiration check and reset the timer */
315 timeout
= rtadvd_check_timer();
322 if (timeout
!= NULL
) {
324 "<%s> set timer to %ld:%ld. waiting for "
325 "inputs or timeout", __func__
,
326 (long int)timeout
->tv_sec
,
327 (long int)timeout
->tv_nsec
);
330 "<%s> there's no timer. waiting for inputs",
334 if ((i
= poll(set
, 2, timeout
? (timeout
->tv_sec
* 1000 +
335 (timeout
->tv_nsec
+ 999999) / 1000000) : INFTIM
)) < 0)
337 /* EINTR would occur upon SIGUSR1 for status dump */
339 syslog(LOG_ERR
, "<%s> poll: %m", __func__
);
342 if (i
== 0) /* timeout */
344 if (rtsock
!= -1 && set
[1].revents
& POLLIN
)
346 if (set
[0].revents
& POLLIN
)
349 exit(0); /* NOTREACHED */
353 rtadvd_set_dump_file(__unused
int sig
)
360 set_reconf(__unused
int sig
)
367 set_die(__unused
int sig
)
377 struct rainfo
*rai
, *ran
;
382 if (TAILQ_FIRST(&ralist
)) {
384 "<%s> waiting for expiration of all RA timers",
388 syslog(LOG_NOTICE
, "<%s> gracefully terminated", __func__
);
395 if (TAILQ_FIRST(&ralist
) == NULL
) {
396 syslog(LOG_NOTICE
, "<%s> gracefully terminated", __func__
);
402 syslog(LOG_NOTICE
, "<%s> final RA transmission started", __func__
);
404 TAILQ_FOREACH_SAFE(rai
, &ralist
, next
, ran
) {
406 TAILQ_REMOVE(&ralist
, rai
, next
);
407 TAILQ_INSERT_HEAD(&ralist
, rai
->leaving
, next
);
408 rai
->leaving
->leaving
= rai
->leaving
;
409 rai
->leaving
->leaving_for
= rai
->leaving
;
414 TAILQ_FOREACH(rdnss
, &rai
->rdnss
, next
)
416 TAILQ_FOREACH(dnssl
, &rai
->dnssl
, next
)
420 rai
->leaving_for
= rai
;
421 rai
->initcounter
= MAX_INITIAL_RTR_ADVERTISEMENTS
;
422 rai
->mininterval
= MIN_DELAY_BETWEEN_RAS
;
423 rai
->maxinterval
= MIN_DELAY_BETWEEN_RAS
;
424 rai
->leaving_adv
= MAX_FINAL_RTR_ADVERTISEMENTS
;
426 ra_timer_update((void *)rai
, &rai
->timer
->tm
);
427 rtadvd_set_timer(&rai
->timer
->tm
, rai
->timer
);
434 int n
, type
, ifindex
= 0, plen
;
436 union rt_msghdr_buf
{
437 struct rt_msghdr rt_msghdr
;
440 char *msg
, *next
, *lim
, **argv
;
441 char ifname
[IF_NAMESIZE
];
442 struct prefix
*prefix
;
444 struct in6_addr
*addr
;
445 char addrbuf
[INET6_ADDRSTRLEN
];
446 int prefixchange
= 0, argc
;
448 memset(&buffer
, 0, sizeof(buffer
));
449 n
= read(rtsock
, &buffer
, sizeof(buffer
));
451 /* We read the buffer first to clear the FD */
457 syslog(LOG_DEBUG
, "<%s> received a routing message "
458 "(type = %d, len = %d)", __func__
, rtmsg_type(msg
),
461 if (n
> rtmsg_len(msg
)) {
463 * This usually won't happen for messages received on
468 "<%s> received data length is larger than "
469 "1st routing message len. multiple messages? "
470 "read %d bytes, but 1st msg len = %d",
471 __func__
, n
, rtmsg_len(msg
));
479 for (next
= msg
; next
< lim
; next
+= len
) {
482 next
= get_next_msg(next
, lim
, 0, &len
,
483 RTADV_TYPE2BITMASK(RTM_ADD
) |
484 RTADV_TYPE2BITMASK(RTM_DELETE
) |
485 RTADV_TYPE2BITMASK(RTM_NEWADDR
) |
486 RTADV_TYPE2BITMASK(RTM_DELADDR
) |
487 #ifdef RTM_IFANNOUNCE
488 RTADV_TYPE2BITMASK(RTM_IFANNOUNCE
) |
490 RTADV_TYPE2BITMASK(RTM_IFINFO
));
493 type
= rtmsg_type(next
);
497 ifindex
= get_rtm_ifindex(next
);
501 ifindex
= get_ifam_ifindex(next
);
503 #ifdef RTM_IFANNOUNCE
505 ifindex
= get_ifan_ifindex(next
);
506 if (get_ifan_what(next
) == IFAN_ARRIVAL
) {
508 "<%s> interface %s arrived",
510 if_indextoname(ifindex
, ifname
));
512 getconfig(ifname
, 0);
518 if (strcmp(ifname
, *argv
++) == 0) {
519 getconfig(ifname
, 0);
528 ifindex
= get_ifm_ifindex(next
);
531 /* should not reach here */
534 "<%s:%d> unknown rtmsg %d on %s",
535 __func__
, __LINE__
, type
,
536 if_indextoname(ifindex
, ifname
));
541 if ((rai
= if_indextorainfo(ifindex
)) == NULL
) {
544 "<%s> route changed on "
545 "non advertising interface %s (%d)",
547 if_indextoname(ifindex
, ifname
),
552 oldifflags
= rai
->ifflags
;
556 /* init ifflags because it may have changed */
557 rai
->ifflags
= if_getflags(ifindex
, rai
->ifflags
);
560 break; /* we aren't interested in prefixes */
562 addr
= get_addr(msg
);
563 plen
= get_prefixlen(msg
);
564 /* sanity check for plen */
565 /* as RFC2373, prefixlen is at least 4 */
566 if (plen
< 4 || plen
> 127) {
567 syslog(LOG_INFO
, "<%s> new interface route's"
568 "plen %d is invalid for a prefix",
572 prefix
= find_prefix(rai
, addr
, plen
);
576 * If the prefix has been invalidated,
577 * make it available again.
579 update_prefix(prefix
);
581 } else if (dflag
> 1) {
583 "<%s> new prefix(%s/%d) "
585 "but it was already in list",
587 inet_ntop(AF_INET6
, addr
,
588 (char *)addrbuf
, INET6_ADDRSTRLEN
),
593 make_prefix(rai
, ifindex
, addr
, plen
);
597 /* init ifflags because it may have changed */
598 rai
->ifflags
= if_getflags(ifindex
, rai
->ifflags
);
603 addr
= get_addr(msg
);
604 plen
= get_prefixlen(msg
);
605 /* sanity check for plen */
606 /* as RFC2373, prefixlen is at least 4 */
607 if (plen
< 4 || plen
> 127) {
609 "<%s> deleted interface route's "
610 "plen %d is invalid for a prefix",
614 prefix
= find_prefix(rai
, addr
, plen
);
615 if (prefix
== NULL
) {
618 "<%s> prefix(%s/%d) was "
620 "but it was not in list",
622 inet_ntop(AF_INET6
, addr
,
623 (char *)addrbuf
, INET6_ADDRSTRLEN
),
628 invalidate_prefix(prefix
);
633 /* init ifflags because it may have changed */
634 rai
->ifflags
= if_getflags(ifindex
, rai
->ifflags
);
637 rai
->ifflags
= get_ifm_flags(next
);
639 #ifdef RTM_IFANNOUNCE
641 if (get_ifan_what(next
) == IFAN_DEPARTURE
) {
643 "<%s> interface %s departed",
644 __func__
, rai
->ifname
);
645 TAILQ_REMOVE(&ralist
, rai
, next
);
647 free_rainfo(rai
->leaving
);
654 /* should not reach here */
657 "<%s:%d> unknown rtmsg %d on %s",
658 __func__
, __LINE__
, type
,
659 if_indextoname(ifindex
, ifname
));
664 /* check if an interface flag is changed */
665 if ((oldifflags
& IFF_UP
) != 0 && /* UP to DOWN */
666 (rai
->ifflags
& IFF_UP
) == 0) {
668 "<%s> interface %s becomes down. stop timer.",
669 __func__
, rai
->ifname
);
670 rtadvd_remove_timer(&rai
->timer
);
671 } else if ((oldifflags
& IFF_UP
) == 0 && /* DOWN to UP */
672 (rai
->ifflags
& IFF_UP
) != 0) {
674 "<%s> interface %s becomes up. restart timer.",
675 __func__
, rai
->ifname
);
677 rai
->initcounter
= 0; /* reset the counter */
678 rai
->waiting
= 0; /* XXX */
679 rtadvd_remove_timer(&rai
->timer
);
680 rai
->timer
= rtadvd_add_timer(ra_timeout
,
681 ra_timer_update
, rai
, rai
);
682 ra_timer_update((void *)rai
, &rai
->timer
->tm
);
683 rtadvd_set_timer(&rai
->timer
->tm
, rai
->timer
);
684 } else if (prefixchange
&& rai
->ifflags
& IFF_UP
) {
686 * An advertised prefix has been added or invalidated.
687 * Will notice the change in a short delay.
689 rai
->initcounter
= 0;
690 ra_timer_set_short_delay(rai
);
705 struct icmp6_hdr
*icp
;
708 struct in6_pktinfo
*pi
= NULL
;
709 char ntopbuf
[INET6_ADDRSTRLEN
], ifnamebuf
[IFNAMSIZ
];
710 struct in6_addr dst
= in6addr_any
;
714 * Get message. We reset msg_controllen since the field could
715 * be modified if we had received a message before setting
718 rcvmhdr
.msg_controllen
= rcvcmsgbuflen
;
719 if ((i
= recvmsg(sock
, &rcvmhdr
, 0)) < 0)
722 /* We read the buffer first to clear the FD */
726 /* extract optional information via Advanced API */
727 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(&rcvmhdr
);
729 cm
= (struct cmsghdr
*)CMSG_NXTHDR(&rcvmhdr
, cm
)) {
730 if (cm
->cmsg_level
== IPPROTO_IPV6
&&
731 cm
->cmsg_type
== IPV6_PKTINFO
&&
732 cm
->cmsg_len
== CMSG_LEN(sizeof(struct in6_pktinfo
))) {
733 pi
= (struct in6_pktinfo
*)(CMSG_DATA(cm
));
734 ifindex
= pi
->ipi6_ifindex
;
737 if (cm
->cmsg_level
== IPPROTO_IPV6
&&
738 cm
->cmsg_type
== IPV6_HOPLIMIT
&&
739 cm
->cmsg_len
== CMSG_LEN(sizeof(int)))
740 hlimp
= (int *)CMSG_DATA(cm
);
744 "<%s> failed to get receiving interface",
750 "<%s> failed to get receiving hop limit",
755 if ((rai
= if_indextorainfo(pi
->ipi6_ifindex
)) == NULL
) {
758 "<%s> received data for non advertising "
761 if_indextoname(pi
->ipi6_ifindex
, ifnamebuf
));
766 * If we happen to receive data on an interface which is now down,
767 * just discard the data.
769 if ((rai
->ifflags
& IFF_UP
) == 0) {
771 "<%s> received data on a disabled interface (%s)",
773 if_indextoname(pi
->ipi6_ifindex
, ifnamebuf
));
778 if ((size_t)i
< sizeof(struct ip6_hdr
) + sizeof(struct icmp6_hdr
)) {
780 "<%s> packet size(%d) is too short",
785 ip
= (struct ip6_hdr
*)rcvmhdr
.msg_iov
[0].iov_base
;
786 icp
= (struct icmp6_hdr
*)(ip
+ 1); /* XXX: ext. hdr? */
788 if ((size_t)i
< sizeof(struct icmp6_hdr
)) {
790 "<%s> packet size(%zd) is too short",
795 icp
= (struct icmp6_hdr
*)rcvmhdr
.msg_iov
[0].iov_base
;
798 switch (icp
->icmp6_type
) {
799 case ND_ROUTER_SOLICIT
:
801 * Message verification - RFC-2461 6.1.1
802 * XXX: these checks must be done in the kernel as well,
803 * but we can't completely rely on them.
807 "<%s> RS with invalid hop limit(%d) "
808 "received from %s on %s",
810 inet_ntop(AF_INET6
, &rcvfrom
.sin6_addr
, ntopbuf
,
812 if_indextoname(pi
->ipi6_ifindex
, ifnamebuf
));
815 if (icp
->icmp6_code
) {
817 "<%s> RS with invalid ICMP6 code(%d) "
818 "received from %s on %s",
819 __func__
, icp
->icmp6_code
,
820 inet_ntop(AF_INET6
, &rcvfrom
.sin6_addr
, ntopbuf
,
822 if_indextoname(pi
->ipi6_ifindex
, ifnamebuf
));
825 if ((size_t)i
< sizeof(struct nd_router_solicit
)) {
827 "<%s> RS from %s on %s does not have enough "
828 "length (len = %zd)",
830 inet_ntop(AF_INET6
, &rcvfrom
.sin6_addr
, ntopbuf
,
832 if_indextoname(pi
->ipi6_ifindex
, ifnamebuf
), i
);
835 rs_input(i
, (struct nd_router_solicit
*)icp
, pi
, &rcvfrom
);
837 case ND_ROUTER_ADVERT
:
839 * Message verification - RFC-2461 6.1.2
840 * XXX: there's a same dilemma as above...
844 "<%s> RA with invalid hop limit(%d) "
845 "received from %s on %s",
847 inet_ntop(AF_INET6
, &rcvfrom
.sin6_addr
, ntopbuf
,
849 if_indextoname(pi
->ipi6_ifindex
, ifnamebuf
));
852 if (icp
->icmp6_code
) {
854 "<%s> RA with invalid ICMP6 code(%d) "
855 "received from %s on %s",
856 __func__
, icp
->icmp6_code
,
857 inet_ntop(AF_INET6
, &rcvfrom
.sin6_addr
, ntopbuf
,
859 if_indextoname(pi
->ipi6_ifindex
, ifnamebuf
));
862 if ((size_t)i
< sizeof(struct nd_router_advert
)) {
864 "<%s> RA from %s on %s does not have enough "
865 "length (len = %zd)",
867 inet_ntop(AF_INET6
, &rcvfrom
.sin6_addr
, ntopbuf
,
869 if_indextoname(pi
->ipi6_ifindex
, ifnamebuf
), i
);
872 ra_input(i
, (struct nd_router_advert
*)icp
, pi
, &rcvfrom
);
874 case ICMP6_ROUTER_RENUMBERING
:
875 if (accept_rr
== 0) {
876 syslog(LOG_ERR
, "<%s> received a router renumbering "
877 "message, but not allowed to be accepted",
881 rr_input(i
, (struct icmp6_router_renum
*)icp
, pi
, &rcvfrom
,
886 * Note that this case is POSSIBLE, especially just
887 * after invocation of the daemon. This is because we
888 * could receive message after opening the socket and
889 * before setting ICMP6 type filter(see sock_open()).
891 syslog(LOG_ERR
, "<%s> invalid icmp type(%d)",
892 __func__
, icp
->icmp6_type
);
900 rs_input(int len
, struct nd_router_solicit
*rs
,
901 struct in6_pktinfo
*pi
, struct sockaddr_in6
*from
)
903 char ntopbuf
[INET6_ADDRSTRLEN
], ifnamebuf
[IFNAMSIZ
];
904 union nd_opts ndopts
;
906 struct soliciter
*sol
;
909 "<%s> RS received from %s on %s",
911 inet_ntop(AF_INET6
, &from
->sin6_addr
,
912 ntopbuf
, INET6_ADDRSTRLEN
),
913 if_indextoname(pi
->ipi6_ifindex
, ifnamebuf
));
915 /* ND option check */
916 memset(&ndopts
, 0, sizeof(ndopts
));
917 TAILQ_INIT(&ndopts
.nd_opts_list
);
918 if (nd6_options((struct nd_opt_hdr
*)(rs
+ 1),
919 len
- sizeof(struct nd_router_solicit
),
920 &ndopts
, NDOPT_FLAG_SRCLINKADDR
)) {
922 "<%s> ND option check failed for an RS from %s on %s",
924 inet_ntop(AF_INET6
, &from
->sin6_addr
,
925 ntopbuf
, INET6_ADDRSTRLEN
),
926 if_indextoname(pi
->ipi6_ifindex
, ifnamebuf
));
931 * If the IP source address is the unspecified address, there
932 * must be no source link-layer address option in the message.
935 if (IN6_IS_ADDR_UNSPECIFIED(&from
->sin6_addr
) &&
936 ndopts
.nd_opts_src_lladdr
) {
938 "<%s> RS from unspecified src on %s has a link-layer"
941 if_indextoname(pi
->ipi6_ifindex
, ifnamebuf
));
945 if ((rai
= if_indextorainfo(pi
->ipi6_ifindex
)) == NULL
) {
947 "<%s> RS received on non advertising interface(%s)",
949 if_indextoname(pi
->ipi6_ifindex
, ifnamebuf
));
955 "<%s> RS received on reconfiguring advertising interface(%s)",
956 __func__
, rai
->ifname
);
960 rai
->rsinput
++; /* increment statistics */
963 * Decide whether to send RA according to the rate-limit
967 /* record sockaddr waiting for RA, if possible */
968 sol
= malloc(sizeof(*sol
));
971 /* XXX RFC2553 need clarification on flowinfo */
972 sol
->addr
.sin6_flowinfo
= 0;
973 TAILQ_INSERT_HEAD(&rai
->soliciter
, sol
, next
);
977 * If there is already a waiting RS packet, don't
983 ra_timer_set_short_delay(rai
);
986 free_ndopts(&ndopts
);
991 ra_timer_set_short_delay(struct rainfo
*rai
)
993 long delay
; /* must not be greater than 1000000 */
994 struct timespec interval
, now
, min_delay
, tm_tmp
, *rest
;
997 * Compute a random delay. If the computed value
998 * corresponds to a time later than the time the next
999 * multicast RA is scheduled to be sent, ignore the random
1000 * delay and send the advertisement at the
1001 * already-scheduled time. RFC2461 6.2.6
1003 delay
= arc4random() % MAX_RA_DELAY_TIME
;
1004 interval
.tv_sec
= 0;
1005 interval
.tv_nsec
= delay
;
1006 rest
= rtadvd_timer_rest(rai
->timer
);
1007 if (timespeccmp(rest
, &interval
, <)) {
1008 syslog(LOG_DEBUG
, "<%s> random delay is larger than "
1009 "the rest of current timer", __func__
);
1014 * If we sent a multicast Router Advertisement within
1015 * the last MIN_DELAY_BETWEEN_RAS seconds, schedule
1016 * the advertisement to be sent at a time corresponding to
1017 * MIN_DELAY_BETWEEN_RAS plus the random value after the
1018 * previous advertisement was sent.
1020 clock_gettime(CLOCK_MONOTONIC
, &now
);
1021 timespecsub(&now
, &rai
->lastsent
, &tm_tmp
);
1022 min_delay
.tv_sec
= MIN_DELAY_BETWEEN_RAS
;
1023 min_delay
.tv_nsec
= 0;
1024 if (timespeccmp(&tm_tmp
, &min_delay
, <)) {
1025 timespecsub(&min_delay
, &tm_tmp
, &min_delay
);
1026 timespecadd(&min_delay
, &interval
, &interval
);
1028 rtadvd_set_timer(&interval
, rai
->timer
);
1032 ra_input(int len
, struct nd_router_advert
*ra
,
1033 struct in6_pktinfo
*pi
, struct sockaddr_in6
*from
)
1036 char ntopbuf
[INET6_ADDRSTRLEN
], ifnamebuf
[IFNAMSIZ
];
1037 union nd_opts ndopts
;
1038 const char *on_off
[] = {"OFF", "ON"};
1039 uint32_t reachabletime
, retranstimer
, mtu
;
1040 struct nd_optlist
*optp
;
1041 int inconsistent
= 0;
1044 "<%s> RA received from %s on %s",
1046 inet_ntop(AF_INET6
, &from
->sin6_addr
,
1047 ntopbuf
, INET6_ADDRSTRLEN
),
1048 if_indextoname(pi
->ipi6_ifindex
, ifnamebuf
));
1050 /* ND option check */
1051 memset(&ndopts
, 0, sizeof(ndopts
));
1052 TAILQ_INIT(&ndopts
.nd_opts_list
);
1053 if (nd6_options((struct nd_opt_hdr
*)(ra
+ 1),
1054 len
- sizeof(struct nd_router_advert
),
1055 &ndopts
, NDOPT_FLAG_SRCLINKADDR
|
1056 NDOPT_FLAG_PREFIXINFO
| NDOPT_FLAG_MTU
|
1057 NDOPT_FLAG_RDNSS
| NDOPT_FLAG_DNSSL
))
1060 "<%s> ND option check failed for an RA from %s on %s",
1062 inet_ntop(AF_INET6
, &from
->sin6_addr
,
1063 ntopbuf
, INET6_ADDRSTRLEN
),
1064 if_indextoname(pi
->ipi6_ifindex
, ifnamebuf
));
1069 * RA consistency check according to RFC-2461 6.2.7
1071 if ((rai
= if_indextorainfo(pi
->ipi6_ifindex
)) == 0) {
1073 "<%s> received RA from %s on non-advertising"
1076 inet_ntop(AF_INET6
, &from
->sin6_addr
,
1077 ntopbuf
, INET6_ADDRSTRLEN
),
1078 if_indextoname(pi
->ipi6_ifindex
, ifnamebuf
));
1083 "<%s> received RA on re-configuring interface (%s)",
1084 __func__
, rai
->ifname
);
1087 rai
->rainput
++; /* increment statistics */
1089 /* Cur Hop Limit value */
1090 if (ra
->nd_ra_curhoplimit
&& rai
->hoplimit
&&
1091 ra
->nd_ra_curhoplimit
!= rai
->hoplimit
) {
1093 "<%s> CurHopLimit inconsistent on %s:"
1094 " %d from %s, %d from us",
1097 ra
->nd_ra_curhoplimit
,
1098 inet_ntop(AF_INET6
, &from
->sin6_addr
,
1099 ntopbuf
, INET6_ADDRSTRLEN
),
1104 if ((ra
->nd_ra_flags_reserved
& ND_RA_FLAG_MANAGED
) !=
1107 "<%s> M flag inconsistent on %s:"
1108 " %s from %s, %s from us",
1111 on_off
[!rai
->managedflg
],
1112 inet_ntop(AF_INET6
, &from
->sin6_addr
,
1113 ntopbuf
, INET6_ADDRSTRLEN
),
1114 on_off
[rai
->managedflg
]);
1118 if ((ra
->nd_ra_flags_reserved
& ND_RA_FLAG_OTHER
) !=
1121 "<%s> O flag inconsistent on %s:"
1122 " %s from %s, %s from us",
1125 on_off
[!rai
->otherflg
],
1126 inet_ntop(AF_INET6
, &from
->sin6_addr
,
1127 ntopbuf
, INET6_ADDRSTRLEN
),
1128 on_off
[rai
->otherflg
]);
1131 /* Reachable Time */
1132 reachabletime
= ntohl(ra
->nd_ra_reachable
);
1133 if (reachabletime
&& rai
->reachabletime
&&
1134 reachabletime
!= rai
->reachabletime
) {
1136 "<%s> ReachableTime inconsistent on %s:"
1137 " %d from %s, %d from us",
1141 inet_ntop(AF_INET6
, &from
->sin6_addr
,
1142 ntopbuf
, INET6_ADDRSTRLEN
),
1143 rai
->reachabletime
);
1147 retranstimer
= ntohl(ra
->nd_ra_retransmit
);
1148 if (retranstimer
&& rai
->retranstimer
&&
1149 retranstimer
!= rai
->retranstimer
) {
1151 "<%s> RetranceTimer inconsistent on %s:"
1152 " %d from %s, %d from us",
1156 inet_ntop(AF_INET6
, &from
->sin6_addr
,
1157 ntopbuf
, INET6_ADDRSTRLEN
),
1161 /* Values in the MTU options */
1162 if (ndopts
.nd_opts_mtu
) {
1163 mtu
= ntohl(ndopts
.nd_opts_mtu
->nd_opt_mtu_mtu
);
1164 if (mtu
&& rai
->linkmtu
&& mtu
!= rai
->linkmtu
) {
1166 "<%s> MTU option value inconsistent on %s:"
1167 " %d from %s, %d from us",
1170 inet_ntop(AF_INET6
, &from
->sin6_addr
,
1171 ntopbuf
, INET6_ADDRSTRLEN
),
1176 /* Preferred and Valid Lifetimes for prefixes */
1177 if (ndopts
.nd_opts_pi
)
1178 if (prefix_check(ndopts
.nd_opts_pi
, rai
, from
))
1180 TAILQ_FOREACH(optp
, &ndopts
.nd_opts_list
, next
)
1181 if (prefix_check((struct nd_opt_prefix_info
*)optp
->opt
,
1186 rai
->rainconsistent
++;
1189 free_ndopts(&ndopts
);
1193 /* return a non-zero value if the received prefix is inconsitent with ours */
1195 prefix_check(struct nd_opt_prefix_info
*pinfo
,
1196 struct rainfo
*rai
, struct sockaddr_in6
*from
)
1198 uint32_t preferred_time
, valid_time
;
1200 int inconsistent
= 0;
1201 char ntopbuf
[INET6_ADDRSTRLEN
], prefixbuf
[INET6_ADDRSTRLEN
];
1202 struct timespec now
;
1204 #if 0 /* impossible */
1205 if (pinfo
->nd_opt_pi_type
!= ND_OPT_PREFIX_INFORMATION
)
1210 * log if the adveritsed prefix has link-local scope(sanity check?)
1212 if (IN6_IS_ADDR_LINKLOCAL(&pinfo
->nd_opt_pi_prefix
)) {
1214 "<%s> link-local prefix %s/%d is advertised "
1217 inet_ntop(AF_INET6
, &pinfo
->nd_opt_pi_prefix
,
1218 prefixbuf
, INET6_ADDRSTRLEN
),
1219 pinfo
->nd_opt_pi_prefix_len
,
1220 inet_ntop(AF_INET6
, &from
->sin6_addr
,
1221 ntopbuf
, INET6_ADDRSTRLEN
),
1225 if ((pp
= find_prefix(rai
, &pinfo
->nd_opt_pi_prefix
,
1226 pinfo
->nd_opt_pi_prefix_len
)) == NULL
) {
1228 "<%s> prefix %s/%d from %s on %s is not in our list",
1230 inet_ntop(AF_INET6
, &pinfo
->nd_opt_pi_prefix
,
1231 prefixbuf
, INET6_ADDRSTRLEN
),
1232 pinfo
->nd_opt_pi_prefix_len
,
1233 inet_ntop(AF_INET6
, &from
->sin6_addr
,
1234 ntopbuf
, INET6_ADDRSTRLEN
),
1239 preferred_time
= ntohl(pinfo
->nd_opt_pi_preferred_time
);
1240 if (pp
->pltimeexpire
) {
1242 * The lifetime is decremented in real time, so we should
1243 * compare the expiration time.
1244 * (RFC 2461 Section 6.2.7.)
1245 * XXX: can we really expect that all routers on the link
1246 * have synchronized clocks?
1248 clock_gettime(CLOCK_MONOTONIC
, &now
);
1249 preferred_time
+= now
.tv_sec
;
1251 if (!pp
->timer
&& rai
->clockskew
&&
1252 llabs((long long)preferred_time
- pp
->pltimeexpire
) > rai
->clockskew
) {
1254 "<%s> preferred lifetime for %s/%d"
1255 " (decr. in real time) inconsistent on %s:"
1256 " %d from %s, %ld from us",
1258 inet_ntop(AF_INET6
, &pinfo
->nd_opt_pi_prefix
,
1259 prefixbuf
, INET6_ADDRSTRLEN
),
1260 pinfo
->nd_opt_pi_prefix_len
,
1261 rai
->ifname
, preferred_time
,
1262 inet_ntop(AF_INET6
, &from
->sin6_addr
,
1263 ntopbuf
, INET6_ADDRSTRLEN
),
1267 } else if (!pp
->timer
&& preferred_time
!= pp
->preflifetime
) {
1269 "<%s> preferred lifetime for %s/%d"
1270 " inconsistent on %s:"
1271 " %d from %s, %d from us",
1273 inet_ntop(AF_INET6
, &pinfo
->nd_opt_pi_prefix
,
1274 prefixbuf
, INET6_ADDRSTRLEN
),
1275 pinfo
->nd_opt_pi_prefix_len
,
1276 rai
->ifname
, preferred_time
,
1277 inet_ntop(AF_INET6
, &from
->sin6_addr
,
1278 ntopbuf
, INET6_ADDRSTRLEN
),
1282 valid_time
= ntohl(pinfo
->nd_opt_pi_valid_time
);
1283 if (pp
->vltimeexpire
) {
1284 clock_gettime(CLOCK_MONOTONIC
, &now
);
1285 valid_time
+= now
.tv_sec
;
1287 if (!pp
->timer
&& rai
->clockskew
&&
1288 llabs((long long)valid_time
- pp
->vltimeexpire
) > rai
->clockskew
) {
1290 "<%s> valid lifetime for %s/%d"
1291 " (decr. in real time) inconsistent on %s:"
1292 " %d from %s, %ld from us",
1294 inet_ntop(AF_INET6
, &pinfo
->nd_opt_pi_prefix
,
1295 prefixbuf
, INET6_ADDRSTRLEN
),
1296 pinfo
->nd_opt_pi_prefix_len
,
1297 rai
->ifname
, preferred_time
,
1298 inet_ntop(AF_INET6
, &from
->sin6_addr
,
1299 ntopbuf
, INET6_ADDRSTRLEN
),
1303 } else if (!pp
->timer
&& valid_time
!= pp
->validlifetime
) {
1305 "<%s> valid lifetime for %s/%d"
1306 " inconsistent on %s:"
1307 " %d from %s, %d from us",
1309 inet_ntop(AF_INET6
, &pinfo
->nd_opt_pi_prefix
,
1310 prefixbuf
, INET6_ADDRSTRLEN
),
1311 pinfo
->nd_opt_pi_prefix_len
,
1312 rai
->ifname
, valid_time
,
1313 inet_ntop(AF_INET6
, &from
->sin6_addr
,
1314 ntopbuf
, INET6_ADDRSTRLEN
),
1319 return(inconsistent
);
1323 find_prefix(struct rainfo
*rai
, struct in6_addr
*prefix
, int plen
)
1326 int bytelen
, bitlen
;
1327 unsigned char bitmask
;
1329 TAILQ_FOREACH(pp
, &rai
->prefix
, next
) {
1330 if (plen
!= pp
->prefixlen
)
1334 bitmask
= 0xff << (8 - bitlen
);
1335 if (memcmp((void *)prefix
, (void *)&pp
->prefix
, bytelen
))
1338 ((prefix
->s6_addr
[bytelen
] & bitmask
) ==
1339 (pp
->prefix
.s6_addr
[bytelen
] & bitmask
))) {
1347 /* check if p0/plen0 matches p1/plen1; return 1 if matches, otherwise 0. */
1349 prefix_match(struct in6_addr
*p0
, int plen0
,
1350 struct in6_addr
*p1
, int plen1
)
1352 int bytelen
, bitlen
;
1353 unsigned char bitmask
;
1357 bytelen
= plen1
/ 8;
1359 bitmask
= 0xff << (8 - bitlen
);
1360 if (memcmp((void *)p0
, (void *)p1
, bytelen
))
1363 ((p0
->s6_addr
[bytelen
] & bitmask
) ==
1364 (p1
->s6_addr
[bytelen
] & bitmask
))) {
1372 nd6_options(struct nd_opt_hdr
*hdr
, int limit
,
1373 union nd_opts
*ndopts
, uint32_t optflags
)
1377 for (; limit
> 0; limit
-= optlen
) {
1378 if ((size_t)limit
< sizeof(struct nd_opt_hdr
)) {
1379 syslog(LOG_INFO
, "<%s> short option header", __func__
);
1383 hdr
= (struct nd_opt_hdr
*)((char *)hdr
+ optlen
);
1384 if (hdr
->nd_opt_len
== 0) {
1386 "<%s> bad ND option length(0) (type = %d)",
1387 __func__
, hdr
->nd_opt_type
);
1390 optlen
= hdr
->nd_opt_len
<< 3;
1391 if (optlen
> limit
) {
1392 syslog(LOG_INFO
, "<%s> short option", __func__
);
1396 if (hdr
->nd_opt_type
> ND_OPT_MTU
&&
1397 hdr
->nd_opt_type
!= ND_OPT_RDNSS
&&
1398 hdr
->nd_opt_type
!= ND_OPT_DNSSL
)
1400 syslog(LOG_INFO
, "<%s> unknown ND option(type %d)",
1401 __func__
, hdr
->nd_opt_type
);
1405 if ((ndopt_flags
[hdr
->nd_opt_type
] & optflags
) == 0) {
1406 syslog(LOG_INFO
, "<%s> unexpected ND option(type %d)",
1407 __func__
, hdr
->nd_opt_type
);
1412 * Option length check. Do it here for all fixed-length
1415 if ((hdr
->nd_opt_type
== ND_OPT_MTU
&&
1416 (optlen
!= sizeof(struct nd_opt_mtu
))) ||
1417 ((hdr
->nd_opt_type
== ND_OPT_PREFIX_INFORMATION
&&
1418 optlen
!= sizeof(struct nd_opt_prefix_info
))) ||
1419 (hdr
->nd_opt_type
== ND_OPT_RDNSS
&&
1420 ((optlen
< (int)sizeof(struct nd_opt_rdnss
) ||
1421 (optlen
- sizeof(struct nd_opt_rdnss
)) % 16 != 0))) ||
1422 (hdr
->nd_opt_type
== ND_OPT_DNSSL
&&
1423 optlen
< (int)sizeof(struct nd_opt_dnssl
)))
1425 syslog(LOG_INFO
, "<%s> invalid option length",
1430 switch (hdr
->nd_opt_type
) {
1431 case ND_OPT_TARGET_LINKADDR
:
1432 case ND_OPT_REDIRECTED_HEADER
:
1435 break; /* we don't care about these options */
1436 case ND_OPT_SOURCE_LINKADDR
:
1438 if (ndopts
->nd_opt_array
[hdr
->nd_opt_type
]) {
1440 "<%s> duplicated ND option (type = %d)",
1441 __func__
, hdr
->nd_opt_type
);
1443 ndopts
->nd_opt_array
[hdr
->nd_opt_type
] = hdr
;
1445 case ND_OPT_PREFIX_INFORMATION
:
1447 struct nd_optlist
*pfxlist
;
1449 if (ndopts
->nd_opts_pi
== 0) {
1450 ndopts
->nd_opts_pi
=
1451 (struct nd_opt_prefix_info
*)hdr
;
1454 if ((pfxlist
= malloc(sizeof(*pfxlist
))) == NULL
) {
1455 syslog(LOG_ERR
, "<%s> can't allocate memory",
1460 TAILQ_INSERT_TAIL(&ndopts
->nd_opts_list
, pfxlist
, next
);
1464 default: /* impossible */
1472 free_ndopts(ndopts
);
1478 free_ndopts(union nd_opts
*ndopts
)
1480 struct nd_optlist
*opt
;
1482 while ((opt
= TAILQ_FIRST(&ndopts
->nd_opts_list
)) != NULL
) {
1483 TAILQ_REMOVE(&ndopts
->nd_opts_list
, opt
, next
);
1491 struct icmp6_filter filt
;
1492 struct ipv6_mreq mreq
;
1495 /* XXX: should be max MTU attached to the node */
1496 static unsigned char answer
[1500];
1498 rcvcmsgbuflen
= CMSG_SPACE(sizeof(struct in6_pktinfo
)) +
1499 CMSG_SPACE(sizeof(int));
1500 rcvcmsgbuf
= malloc(rcvcmsgbuflen
);
1501 if (rcvcmsgbuf
== NULL
) {
1502 syslog(LOG_ERR
, "<%s> malloc: %m", __func__
);
1506 sndcmsgbuflen
= CMSG_SPACE(sizeof(struct in6_pktinfo
));
1507 sndcmsgbuf
= malloc(sndcmsgbuflen
);
1508 if (sndcmsgbuf
== NULL
) {
1509 syslog(LOG_ERR
, "<%s> malloc: %m", __func__
);
1513 if ((sock
= socket(AF_INET6
, SOCK_RAW
, IPPROTO_ICMPV6
)) < 0) {
1514 syslog(LOG_ERR
, "<%s> socket: %m", __func__
);
1518 /* RFC 4861 Section 4.2 */
1520 if (setsockopt(sock
, IPPROTO_IPV6
, IPV6_MULTICAST_HOPS
, &on
,
1521 sizeof(on
)) == -1) {
1522 syslog(LOG_ERR
, "<%s> IPV6_MULTICAST_HOPS: %m", __func__
);
1526 /* specify to tell receiving interface */
1528 #ifdef IPV6_RECVPKTINFO
1529 if (setsockopt(sock
, IPPROTO_IPV6
, IPV6_RECVPKTINFO
, &on
,
1531 syslog(LOG_ERR
, "<%s> IPV6_RECVPKTINFO: %m", __func__
);
1534 #else /* old adv. API */
1535 if (setsockopt(sock
, IPPROTO_IPV6
, IPV6_PKTINFO
, &on
,
1537 syslog(LOG_ERR
, "<%s> IPV6_PKTINFO: %m", __func__
);
1543 /* specify to tell value of hoplimit field of received IP6 hdr */
1544 #ifdef IPV6_RECVHOPLIMIT
1545 if (setsockopt(sock
, IPPROTO_IPV6
, IPV6_RECVHOPLIMIT
, &on
,
1547 syslog(LOG_ERR
, "<%s> IPV6_RECVHOPLIMIT: %m", __func__
);
1550 #else /* old adv. API */
1551 if (setsockopt(sock
, IPPROTO_IPV6
, IPV6_HOPLIMIT
, &on
,
1553 syslog(LOG_ERR
, "<%s> IPV6_HOPLIMIT: %m", __func__
);
1558 ICMP6_FILTER_SETBLOCKALL(&filt
);
1559 ICMP6_FILTER_SETPASS(ND_ROUTER_SOLICIT
, &filt
);
1560 ICMP6_FILTER_SETPASS(ND_ROUTER_ADVERT
, &filt
);
1562 ICMP6_FILTER_SETPASS(ICMP6_ROUTER_RENUMBERING
, &filt
);
1563 if (setsockopt(sock
, IPPROTO_ICMPV6
, ICMP6_FILTER
, &filt
,
1564 sizeof(filt
)) < 0) {
1565 syslog(LOG_ERR
, "<%s> IICMP6_FILTER: %m", __func__
);
1570 * join all routers multicast address on each advertising interface.
1572 if (inet_pton(AF_INET6
, ALLROUTERS_LINK
,
1573 mreq
.ipv6mr_multiaddr
.s6_addr
) != 1)
1575 syslog(LOG_ERR
, "<%s> inet_pton failed(library bug?)",
1579 TAILQ_FOREACH(ra
, &ralist
, next
) {
1580 mreq
.ipv6mr_interface
= ra
->ifindex
;
1581 if (setsockopt(sock
, IPPROTO_IPV6
, IPV6_JOIN_GROUP
, &mreq
,
1582 sizeof(mreq
)) < 0) {
1583 syslog(LOG_ERR
, "<%s> IPV6_JOIN_GROUP(link) on %s: %m",
1584 __func__
, ra
->ifname
);
1590 * When attending router renumbering, join all-routers site-local
1594 if (inet_pton(AF_INET6
, ALLROUTERS_SITE
,
1595 mreq
.ipv6mr_multiaddr
.s6_addr
) != 1)
1597 syslog(LOG_ERR
, "<%s> inet_pton failed(library bug?)",
1601 ra
= TAILQ_FIRST(&ralist
);
1603 if ((mreq
.ipv6mr_interface
= if_nametoindex(mcastif
))
1606 "<%s> invalid interface: %s",
1611 mreq
.ipv6mr_interface
= ra
->ifindex
;
1612 if (setsockopt(sock
, IPPROTO_IPV6
, IPV6_JOIN_GROUP
,
1613 &mreq
, sizeof(mreq
)) < 0) {
1615 "<%s> IPV6_JOIN_GROUP(site) on %s: %m",
1617 mcastif
? mcastif
: ra
->ifname
);
1622 /* initialize msghdr for receiving packets */
1623 rcviov
[0].iov_base
= answer
;
1624 rcviov
[0].iov_len
= sizeof(answer
);
1625 rcvmhdr
.msg_name
= &rcvfrom
;
1626 rcvmhdr
.msg_namelen
= sizeof(rcvfrom
);
1627 rcvmhdr
.msg_iov
= rcviov
;
1628 rcvmhdr
.msg_iovlen
= 1;
1629 rcvmhdr
.msg_control
= rcvcmsgbuf
;
1630 rcvmhdr
.msg_controllen
= rcvcmsgbuflen
;
1632 /* initialize msghdr for sending packets */
1633 sndmhdr
.msg_namelen
= sizeof(struct sockaddr_in6
);
1634 sndmhdr
.msg_iov
= sndiov
;
1635 sndmhdr
.msg_iovlen
= 1;
1636 sndmhdr
.msg_control
= (void *)sndcmsgbuf
;
1637 sndmhdr
.msg_controllen
= sndcmsgbuflen
;
1642 /* open a routing socket to watch the routing table */
1646 if ((rtsock
= socket(PF_ROUTE
, SOCK_RAW
, 0)) < 0) {
1647 syslog(LOG_ERR
, "<%s> socket: %m", __func__
);
1653 if_indextorainfo(unsigned int idx
)
1657 TAILQ_FOREACH(rai
, &ralist
, next
) {
1658 if (rai
->ifindex
== idx
)
1662 return(NULL
); /* search failed */
1666 ra_output(struct rainfo
*rai
)
1670 struct in6_pktinfo
*pi
;
1671 struct soliciter
*sol
;
1673 if ((rai
->ifflags
& IFF_UP
) == 0) {
1674 syslog(LOG_DEBUG
, "<%s> %s is not up, skip sending RA",
1675 __func__
, rai
->ifname
);
1679 make_packet(rai
); /* XXX: inefficient */
1681 sndmhdr
.msg_name
= (void *)&sin6_linklocal_allnodes
;
1682 sndmhdr
.msg_iov
[0].iov_base
= (void *)rai
->ra_data
;
1683 sndmhdr
.msg_iov
[0].iov_len
= rai
->ra_datalen
;
1685 cm
= CMSG_FIRSTHDR(&sndmhdr
);
1686 /* specify the outgoing interface */
1687 cm
->cmsg_level
= IPPROTO_IPV6
;
1688 cm
->cmsg_type
= IPV6_PKTINFO
;
1689 cm
->cmsg_len
= CMSG_LEN(sizeof(struct in6_pktinfo
));
1690 pi
= (struct in6_pktinfo
*)CMSG_DATA(cm
);
1691 memset(&pi
->ipi6_addr
, 0, sizeof(pi
->ipi6_addr
)); /*XXX*/
1692 pi
->ipi6_ifindex
= rai
->ifindex
;
1695 "<%s> send RA on %s, # of waitings = %d",
1696 __func__
, rai
->ifname
, rai
->waiting
);
1698 i
= sendmsg(sock
, &sndmhdr
, 0);
1700 if (i
< 0 || (size_t)i
!= rai
->ra_datalen
) {
1702 syslog(LOG_ERR
, "<%s> sendmsg on %s: %m",
1703 __func__
, rai
->ifname
);
1708 * unicast advertisements
1709 * XXX commented out. reason: though spec does not forbit it, unicast
1710 * advert does not really help
1712 while ((sol
= TAILQ_FIRST(&rai
->soliciter
)) != NULL
) {
1714 sndmhdr
.msg_name
= (void *)&sol
->addr
;
1715 i
= sendmsg(sock
, &sndmhdr
, 0);
1716 if (i
< 0 || i
!= rai
->ra_datalen
) {
1719 "<%s> unicast sendmsg on %s: %m",
1720 __func__
, rai
->ifname
);
1724 TAILQ_REMOVE(&rai
->soliciter
, sol
, next
);
1728 if (rai
->leaving_adv
> 0) {
1729 if (--(rai
->leaving_adv
) == 0) {
1730 /* leaving for ourself means we're shutting down */
1731 if (rai
->leaving_for
== rai
) {
1732 TAILQ_REMOVE(&ralist
, rai
, next
);
1738 " new config active for interface (%s)",
1739 __func__
, rai
->ifname
);
1740 rai
->leaving_for
->timer
= rtadvd_add_timer(ra_timeout
,
1742 rai
->leaving_for
, rai
->leaving_for
);
1743 ra_timer_set_short_delay(rai
->leaving_for
);
1744 rai
->leaving_for
->leaving
= NULL
;
1750 /* update counter */
1751 if (rai
->initcounter
< MAX_INITIAL_RTR_ADVERTISEMENTS
)
1755 /* update timestamp */
1756 clock_gettime(CLOCK_MONOTONIC
, &rai
->lastsent
);
1758 /* reset waiting conter */
1764 /* process RA timer */
1765 struct rtadvd_timer
*
1766 ra_timeout(void *data
)
1768 struct rainfo
*rai
= (struct rainfo
*)data
;
1771 /* if necessary, reconstruct the packet. */
1775 "<%s> RA timer on %s is expired",
1776 __func__
, rai
->ifname
);
1783 /* update RA timer */
1785 ra_timer_update(void *data
, struct timespec
*tm
)
1787 struct rainfo
*rai
= (struct rainfo
*)data
;
1791 * Whenever a multicast advertisement is sent from an interface,
1792 * the timer is reset to a uniformly-distributed random value
1793 * between the interface's configured MinRtrAdvInterval and
1794 * MaxRtrAdvInterval (RFC2461 6.2.4).
1796 interval
= rai
->mininterval
;
1797 if (rai
->mininterval
!= rai
->maxinterval
)
1798 interval
+= arc4random() % (rai
->maxinterval
-rai
->mininterval
);
1801 * For the first few advertisements (up to
1802 * MAX_INITIAL_RTR_ADVERTISEMENTS), if the randomly chosen interval
1803 * is greater than MAX_INITIAL_RTR_ADVERT_INTERVAL, the timer
1804 * SHOULD be set to MAX_INITIAL_RTR_ADVERT_INTERVAL instead.
1807 if (rai
->initcounter
< MAX_INITIAL_RTR_ADVERTISEMENTS
&&
1808 interval
> MAX_INITIAL_RTR_ADVERT_INTERVAL
)
1809 interval
= MAX_INITIAL_RTR_ADVERT_INTERVAL
;
1811 tm
->tv_sec
= interval
;
1815 "<%s> RA timer on %s is set to %ld:%ld",
1816 __func__
, rai
->ifname
,
1817 (long int)tm
->tv_sec
, (long int)tm
->tv_nsec
);