1 /* $FreeBSD: stable/10/usr.sbin/rtadvd/if.c 255156 2013-09-02 20:44:19Z hrs $ */
2 /* $KAME: if.c,v 1.17 2001/01/21 15:27:30 itojun Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * Copyright (C) 2011 Hiroki Sato <hrs@FreeBSD.org>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/param.h>
35 #include <sys/socket.h>
36 #include <sys/sysctl.h>
37 #include <sys/ioctl.h>
39 #include <net/if_dl.h>
40 #include <net/if_types.h>
41 #include <net/if_var.h>
42 #include <net/ethernet.h>
43 #include <net/route.h>
44 #include <netinet/in.h>
45 #include <netinet/in_var.h>
46 #include <netinet/ip6.h>
47 #include <netinet/icmp6.h>
48 #include <netinet6/nd6.h>
56 #include "pathnames.h"
60 #define ROUNDUP(a, size) \
61 (((a) & ((size)-1)) ? (1 + ((a) | ((size)-1))) : (a))
64 (ap) = (struct sockaddr *)((caddr_t)(ap) + \
65 ((ap)->sa_len ? ROUNDUP((ap)->sa_len, sizeof(u_long)) : \
68 struct sockaddr_in6 sin6_linklocal_allnodes
= {
69 .sin6_len
= sizeof(sin6_linklocal_allnodes
),
70 .sin6_family
= AF_INET6
,
71 .sin6_addr
= IN6ADDR_LINKLOCAL_ALLNODES_INIT
,
74 struct sockaddr_in6 sin6_linklocal_allrouters
= {
75 .sin6_len
= sizeof(sin6_linklocal_allrouters
),
76 .sin6_family
= AF_INET6
,
77 .sin6_addr
= IN6ADDR_LINKLOCAL_ALLROUTERS_INIT
,
80 struct sockaddr_in6 sin6_sitelocal_allrouters
= {
81 .sin6_len
= sizeof(sin6_sitelocal_allrouters
),
82 .sin6_family
= AF_INET6
,
83 .sin6_addr
= IN6ADDR_SITELOCAL_ALLROUTERS_INIT
,
86 struct sockinfo sock
= { .si_fd
= -1, .si_name
= NULL
};
87 struct sockinfo rtsock
= { .si_fd
= -1, .si_name
= NULL
};
88 struct sockinfo ctrlsock
= { .si_fd
= -1, .si_name
= _PATH_CTRL_SOCK
};
92 static void get_rtaddrs(int, struct sockaddr
*,
94 static struct if_msghdr
*get_next_msghdr(struct if_msghdr
*,
98 get_rtaddrs(int addrs
, struct sockaddr
*sa
, struct sockaddr
**rti_info
)
102 for (i
= 0; i
< RTAX_MAX
; i
++) {
103 if (addrs
& (1 << i
)) {
112 #define ROUNDUP8(a) (1 + (((a) - 1) | 7))
114 lladdropt_length(struct sockaddr_dl
*sdl
)
116 switch (sdl
->sdl_type
) {
118 return (ROUNDUP8(ETHER_ADDR_LEN
+ 2));
125 lladdropt_fill(struct sockaddr_dl
*sdl
, struct nd_opt_hdr
*ndopt
)
129 ndopt
->nd_opt_type
= ND_OPT_SOURCE_LINKADDR
; /* fixed */
131 switch (sdl
->sdl_type
) {
133 ndopt
->nd_opt_len
= (ROUNDUP8(ETHER_ADDR_LEN
+ 2)) >> 3;
134 addr
= (char *)(ndopt
+ 1);
135 memcpy(addr
, LLADDR(sdl
), ETHER_ADDR_LEN
);
138 syslog(LOG_ERR
, "<%s> unsupported link type(%d)",
139 __func__
, sdl
->sdl_type
);
150 int mib
[6] = {CTL_NET
, AF_ROUTE
, 0, AF_INET6
, NET_RT_DUMP
, 0};
152 if (sysctl(mib
, 6, NULL
, &len
, NULL
, 0) < 0)
158 #define FILTER_MATCH(type, filter) ((0x1 << type) & filter)
159 #define SIN6(s) ((struct sockaddr_in6 *)(s))
160 #define SDL(s) ((struct sockaddr_dl *)(s))
162 get_next_msg(char *buf
, char *lim
, int ifindex
, size_t *lenp
, int filter
)
164 struct rt_msghdr
*rtm
;
165 struct ifa_msghdr
*ifam
;
166 struct sockaddr
*sa
, *dst
, *gw
, *ifa
, *rti_info
[RTAX_MAX
];
169 for (rtm
= (struct rt_msghdr
*)buf
;
170 rtm
< (struct rt_msghdr
*)lim
;
171 rtm
= (struct rt_msghdr
*)(((char *)rtm
) + rtm
->rtm_msglen
)) {
172 /* just for safety */
173 if (!rtm
->rtm_msglen
) {
174 syslog(LOG_WARNING
, "<%s> rtm_msglen is 0 "
175 "(buf=%p lim=%p rtm=%p)", __func__
,
179 if (((struct rt_msghdr
*)buf
)->rtm_version
!= RTM_VERSION
) {
181 "<%s> routing message version mismatch "
182 "(buf=%p lim=%p rtm=%p)", __func__
,
187 if (FILTER_MATCH(rtm
->rtm_type
, filter
) == 0)
190 switch (rtm
->rtm_type
) {
194 /* address related checks */
195 sa
= (struct sockaddr
*)(rtm
+ 1);
196 get_rtaddrs(rtm
->rtm_addrs
, sa
, rti_info
);
197 if ((dst
= rti_info
[RTAX_DST
]) == NULL
||
198 dst
->sa_family
!= AF_INET6
)
201 if (IN6_IS_ADDR_LINKLOCAL(&SIN6(dst
)->sin6_addr
) ||
202 IN6_IS_ADDR_MULTICAST(&SIN6(dst
)->sin6_addr
))
205 if ((gw
= rti_info
[RTAX_GATEWAY
]) == NULL
||
206 gw
->sa_family
!= AF_LINK
)
208 if (ifindex
&& SDL(gw
)->sdl_index
!= ifindex
)
211 if (rti_info
[RTAX_NETMASK
] == NULL
)
215 *lenp
= rtm
->rtm_msglen
;
220 ifam
= (struct ifa_msghdr
*)rtm
;
222 /* address related checks */
223 sa
= (struct sockaddr
*)(ifam
+ 1);
224 get_rtaddrs(ifam
->ifam_addrs
, sa
, rti_info
);
225 if ((ifa
= rti_info
[RTAX_IFA
]) == NULL
||
226 (ifa
->sa_family
!= AF_INET
&&
227 ifa
->sa_family
!= AF_INET6
))
230 if (ifa
->sa_family
== AF_INET6
&&
231 (IN6_IS_ADDR_LINKLOCAL(&SIN6(ifa
)->sin6_addr
) ||
232 IN6_IS_ADDR_MULTICAST(&SIN6(ifa
)->sin6_addr
)))
235 if (ifindex
&& ifam
->ifam_index
!= ifindex
)
239 *lenp
= ifam
->ifam_msglen
;
245 *lenp
= rtm
->rtm_msglen
;
251 return ((char *)rtm
);
258 struct rt_msghdr
*rtm
= (struct rt_msghdr
*)buf
;
259 struct sockaddr
*sa
, *rti_info
[RTAX_MAX
];
261 sa
= (struct sockaddr
*)(rtm
+ 1);
262 get_rtaddrs(rtm
->rtm_addrs
, sa
, rti_info
);
264 return (&SIN6(rti_info
[RTAX_DST
])->sin6_addr
);
268 get_rtm_ifindex(char *buf
)
270 struct rt_msghdr
*rtm
= (struct rt_msghdr
*)buf
;
271 struct sockaddr
*sa
, *rti_info
[RTAX_MAX
];
273 sa
= (struct sockaddr
*)(rtm
+ 1);
274 get_rtaddrs(rtm
->rtm_addrs
, sa
, rti_info
);
276 return (((struct sockaddr_dl
*)rti_info
[RTAX_GATEWAY
])->sdl_index
);
280 get_prefixlen(char *buf
)
282 struct rt_msghdr
*rtm
= (struct rt_msghdr
*)buf
;
283 struct sockaddr
*sa
, *rti_info
[RTAX_MAX
];
286 sa
= (struct sockaddr
*)(rtm
+ 1);
287 get_rtaddrs(rtm
->rtm_addrs
, sa
, rti_info
);
288 sa
= rti_info
[RTAX_NETMASK
];
290 p
= (char *)(&SIN6(sa
)->sin6_addr
);
291 lim
= (char *)sa
+ sa
->sa_len
;
292 return prefixlen(p
, lim
);
296 prefixlen(unsigned char *p
, unsigned char *lim
)
300 for (masklen
= 0; p
< lim
; p
++) {
337 update_persist_ifinfo(struct ifilist_head_t
*ifi_head
, const char *ifname
)
343 ifindex
= if_nametoindex(ifname
);
344 TAILQ_FOREACH(ifi
, ifi_head
, ifi_next
) {
346 if (ifindex
== ifi
->ifi_ifindex
)
349 if (strncmp(ifname
, ifi
->ifi_ifname
,
350 sizeof(ifi
->ifi_ifname
)) == 0)
356 /* A new ifinfo element is needed. */
357 syslog(LOG_DEBUG
, "<%s> new entry: %s", __func__
,
360 ELM_MALLOC(ifi
, exit(1));
361 ifi
->ifi_ifindex
= 0;
362 strncpy(ifi
->ifi_ifname
, ifname
, sizeof(ifi
->ifi_ifname
)-1);
363 ifi
->ifi_ifname
[sizeof(ifi
->ifi_ifname
)-1] = '\0';
364 ifi
->ifi_rainfo
= NULL
;
365 ifi
->ifi_state
= IFI_STATE_UNCONFIGURED
;
366 TAILQ_INSERT_TAIL(ifi_head
, ifi
, ifi_next
);
369 ifi
->ifi_persist
= 1;
371 syslog(LOG_DEBUG
, "<%s> %s is marked PERSIST", __func__
,
373 syslog(LOG_DEBUG
, "<%s> %s is state = %d", __func__
,
374 ifi
->ifi_ifname
, ifi
->ifi_state
);
379 update_ifinfo_nd_flags(struct ifinfo
*ifi
)
381 struct in6_ndireq nd
;
385 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0) {
387 "<%s> socket() failed.", __func__
);
391 memset(&nd
, 0, sizeof(nd
));
392 strncpy(nd
.ifname
, ifi
->ifi_ifname
,
394 error
= ioctl(s
, SIOCGIFINFO_IN6
, (caddr_t
)&nd
);
397 if (errno
!= EPFNOSUPPORT
)
398 syslog(LOG_ERR
, "<%s> ioctl() failed.", __func__
);
401 ifi
->ifi_nd_flags
= nd
.ndi
.flags
;
408 update_ifinfo(struct ifilist_head_t
*ifi_head
, int ifindex
)
410 struct if_msghdr
*ifm
;
411 struct ifinfo
*ifi
= NULL
;
413 struct sockaddr
*rti_info
[RTAX_MAX
];
417 int mib
[] = { CTL_NET
, PF_ROUTE
, 0, AF_INET6
, NET_RT_IFLIST
, 0 };
420 syslog(LOG_DEBUG
, "<%s> enter", __func__
);
422 if (sysctl(mib
, NELEM(mib
), NULL
, &len
, NULL
, 0) <
425 "<%s> sysctl: NET_RT_IFLIST size get failed", __func__
);
428 if ((msg
= malloc(len
)) == NULL
) {
429 syslog(LOG_ERR
, "<%s> malloc failed", __func__
);
432 if (sysctl(mib
, NELEM(mib
), msg
, &len
, NULL
, 0) <
435 "<%s> sysctl: NET_RT_IFLIST get failed", __func__
);
440 for (ifm
= (struct if_msghdr
*)msg
;
441 ifm
!= NULL
&& ifm
< (struct if_msghdr
*)lim
;
442 ifm
= get_next_msghdr(ifm
,(struct if_msghdr
*)lim
)) {
445 syslog(LOG_DEBUG
, "<%s> ifm = %p, lim = %p, diff = %zu",
446 __func__
, ifm
, lim
, (char *)lim
- (char *)ifm
);
448 if (ifm
->ifm_version
!= RTM_VERSION
) {
450 "<%s> ifm_vesrion mismatch", __func__
);
453 if (ifm
->ifm_msglen
== 0) {
455 "<%s> ifm_msglen is 0", __func__
);
461 if (ifm
->ifm_type
== RTM_IFINFO
) {
464 char ifname
[IFNAMSIZ
];
466 syslog(LOG_DEBUG
, "<%s> RTM_IFINFO found. "
467 "ifm_index = %d, ifindex = %d",
468 __func__
, ifm
->ifm_index
, ifindex
);
470 /* when ifindex is specified */
471 if (ifindex
!= UPDATE_IFINFO_ALL
&&
472 ifindex
!= ifm
->ifm_index
)
475 /* lookup an entry with the same ifindex */
476 TAILQ_FOREACH(ifi
, ifi_head
, ifi_next
) {
477 if (ifm
->ifm_index
== ifi
->ifi_ifindex
)
479 if_indextoname(ifm
->ifm_index
, ifname
);
480 if (strncmp(ifname
, ifi
->ifi_ifname
,
481 sizeof(ifname
)) == 0)
486 "<%s> new entry for idx=%d",
487 __func__
, ifm
->ifm_index
);
488 ELM_MALLOC(ifi
, exit(1));
489 ifi
->ifi_rainfo
= NULL
;
490 ifi
->ifi_state
= IFI_STATE_UNCONFIGURED
;
491 ifi
->ifi_persist
= 0;
495 ifi
->ifi_ifindex
= ifm
->ifm_index
;
498 if_indextoname(ifm
->ifm_index
, ifi
->ifi_ifname
);
499 if (ifi
->ifi_ifname
== NULL
) {
501 "<%s> ifname not found (idx=%d)",
502 __func__
, ifm
->ifm_index
);
508 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0) {
510 "<%s> socket() failed.", __func__
);
517 ifi
->ifi_phymtu
= ifm
->ifm_data
.ifi_mtu
;
518 if (ifi
->ifi_phymtu
== 0) {
519 memset(&ifr
, 0, sizeof(ifr
));
520 ifr
.ifr_addr
.sa_family
= AF_INET6
;
521 strncpy(ifr
.ifr_name
, ifi
->ifi_ifname
,
522 sizeof(ifr
.ifr_name
));
523 error
= ioctl(s
, SIOCGIFMTU
, (caddr_t
)&ifr
);
527 "<%s> ioctl() failed.",
533 ifi
->ifi_phymtu
= ifr
.ifr_mtu
;
534 if (ifi
->ifi_phymtu
== 0) {
536 "<%s> no interface mtu info"
537 " on %s. %d will be used.",
538 __func__
, ifi
->ifi_ifname
,
540 ifi
->ifi_phymtu
= IPV6_MMTU
;
546 error
= update_ifinfo_nd_flags(ifi
);
554 sa
= (struct sockaddr
*)(ifm
+ 1);
555 get_rtaddrs(ifm
->ifm_addrs
, sa
, rti_info
);
556 if ((sa
= rti_info
[RTAX_IFP
]) != NULL
) {
557 if (sa
->sa_family
== AF_LINK
) {
558 memcpy(&ifi
->ifi_sdl
,
559 (struct sockaddr_dl
*)sa
,
560 sizeof(ifi
->ifi_sdl
));
563 memset(&ifi
->ifi_sdl
, 0,
564 sizeof(ifi
->ifi_sdl
));
567 ifi
->ifi_flags
= ifm
->ifm_flags
;
570 ifi
->ifi_type
= ifm
->ifm_type
;
573 "out of sync parsing NET_RT_IFLIST\n"
574 "expected %d, got %d\n msglen = %d\n",
575 RTM_IFINFO
, ifm
->ifm_type
, ifm
->ifm_msglen
);
581 "<%s> adding %s(idx=%d) to ifilist",
582 __func__
, ifi
->ifi_ifname
, ifi
->ifi_ifindex
);
583 TAILQ_INSERT_TAIL(ifi_head
, ifi
, ifi_next
);
588 if (mcastif
!= NULL
) {
589 error
= sock_mc_rr_update(&sock
, mcastif
);
597 static struct if_msghdr
*
598 get_next_msghdr(struct if_msghdr
*ifm
, struct if_msghdr
*lim
)
600 struct ifa_msghdr
*ifam
;
602 for (ifam
= (struct ifa_msghdr
*)((char *)ifm
+ ifm
->ifm_msglen
);
603 ifam
< (struct ifa_msghdr
*)lim
;
604 ifam
= (struct ifa_msghdr
*)((char *)ifam
+ ifam
->ifam_msglen
)) {
605 if (!ifam
->ifam_msglen
) {
607 "<%s> ifa_msglen is 0", __func__
);
610 if (ifam
->ifam_type
!= RTM_NEWADDR
)
614 return ((struct if_msghdr
*)ifam
);
618 getinet6sysctl(int code
)
620 int mib
[] = { CTL_NET
, PF_INET6
, IPPROTO_IPV6
, 0 };
625 size
= sizeof(value
);
626 if (sysctl(mib
, NELEM(mib
), &value
, &size
, NULL
, 0)
628 syslog(LOG_ERR
, "<%s>: failed to get ip6 sysctl(%d): %s",
639 sock_mc_join(struct sockinfo
*s
, int ifindex
)
641 struct ipv6_mreq mreq
;
642 char ifname
[IFNAMSIZ
];
644 syslog(LOG_DEBUG
, "<%s> enter", __func__
);
650 * join all routers multicast address on each advertising
653 memset(&mreq
, 0, sizeof(mreq
));
655 memcpy(&mreq
.ipv6mr_multiaddr
.s6_addr
,
656 &sin6_linklocal_allrouters
.sin6_addr
,
657 sizeof(mreq
.ipv6mr_multiaddr
.s6_addr
));
659 mreq
.ipv6mr_interface
= ifindex
;
660 if (setsockopt(s
->si_fd
, IPPROTO_IPV6
, IPV6_JOIN_GROUP
, &mreq
,
663 "<%s> IPV6_JOIN_GROUP(link) on %s: %s",
664 __func__
, if_indextoname(ifindex
, ifname
),
669 "<%s> %s: join link-local all-routers MC group",
670 __func__
, if_indextoname(ifindex
, ifname
));
676 sock_mc_leave(struct sockinfo
*s
, int ifindex
)
678 struct ipv6_mreq mreq
;
679 char ifname
[IFNAMSIZ
];
681 syslog(LOG_DEBUG
, "<%s> enter", __func__
);
687 * join all routers multicast address on each advertising
691 memset(&mreq
, 0, sizeof(mreq
));
693 memcpy(&mreq
.ipv6mr_multiaddr
.s6_addr
,
694 &sin6_linklocal_allrouters
.sin6_addr
,
695 sizeof(mreq
.ipv6mr_multiaddr
.s6_addr
));
697 mreq
.ipv6mr_interface
= ifindex
;
698 if (setsockopt(s
->si_fd
, IPPROTO_IPV6
, IPV6_LEAVE_GROUP
, &mreq
,
701 "<%s> IPV6_JOIN_LEAVE(link) on %s: %s",
702 __func__
, if_indextoname(ifindex
, ifname
),
707 "<%s> %s: leave link-local all-routers MC group",
708 __func__
, if_indextoname(ifindex
, ifname
));
714 sock_mc_rr_update(struct sockinfo
*s
, char *mif
)
716 struct ipv6_mreq mreq
;
718 syslog(LOG_DEBUG
, "<%s> enter", __func__
);
723 * When attending router renumbering, join all-routers site-local
727 memcpy(&mreq
.ipv6mr_multiaddr
.s6_addr
,
728 &sin6_sitelocal_allrouters
.sin6_addr
,
729 sizeof(mreq
.ipv6mr_multiaddr
.s6_addr
));
730 if ((mreq
.ipv6mr_interface
= if_nametoindex(mif
)) == 0) {
732 "<%s> invalid interface: %s",
737 if (setsockopt(s
->si_fd
, IPPROTO_IPV6
, IPV6_JOIN_GROUP
,
738 &mreq
, sizeof(mreq
)) < 0) {
740 "<%s> IPV6_JOIN_GROUP(site) on %s: %s",
741 __func__
, mif
, strerror(errno
));
746 "<%s> %s: join site-local all-routers MC group",