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.
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>
35 #include <sys/sysctl.h>
36 #include <sys/ioctl.h>
38 #include <net/if_types.h>
39 #include <net/ethernet.h>
41 #include <net/route.h>
42 #include <net/if_dl.h>
43 #include <netinet/in.h>
44 #include <netinet/icmp6.h>
53 #define ROUNDUP(a, size) \
54 (((a) & ((size)-1)) ? (1 + ((a) | ((size)-1))) : (a))
56 #define NEXT_SA(ap) (ap) = (struct sockaddr *) \
57 ((caddr_t)(ap) + ((ap)->sa_len ? ROUNDUP((ap)->sa_len,\
61 struct if_msghdr **iflist;
66 static void get_iflist __P((char **buf, size_t *size));
67 static void parse_iflist __P((struct if_msghdr ***ifmlist_p, char *buf,
71 get_rtaddrs(int addrs, struct sockaddr *sa, struct sockaddr **rti_info)
75 for (i = 0; i < RTAX_MAX; i++) {
76 if (addrs & (1 << i)) {
86 if_nametosdl(char *name)
88 int mib[6] = {CTL_NET, AF_ROUTE, 0, 0, NET_RT_IFLIST, 0};
89 char *buf, *next, *lim;
91 struct if_msghdr *ifm;
92 struct sockaddr *sa, *rti_info[RTAX_MAX];
93 struct sockaddr_dl *sdl = NULL, *ret_sdl;
95 if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0)
97 if ((buf = malloc(len)) == NULL)
99 if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
105 for (next = buf; next < lim; next += ifm->ifm_msglen) {
106 ifm = (struct if_msghdr *)next;
107 if (ifm->ifm_type == RTM_IFINFO) {
108 sa = (struct sockaddr *)(ifm + 1);
109 get_rtaddrs(ifm->ifm_addrs, sa, rti_info);
110 if ((sa = rti_info[RTAX_IFP]) != NULL) {
111 if (sa->sa_family == AF_LINK) {
112 sdl = (struct sockaddr_dl *)sa;
113 if (strlen(name) != sdl->sdl_nlen)
114 continue; /* not same len */
115 if (strncmp(&sdl->sdl_data[0],
117 sdl->sdl_nlen) == 0) {
130 if ((ret_sdl = malloc(sdl->sdl_len)) == NULL)
132 memcpy((caddr_t)ret_sdl, (caddr_t)sdl, sdl->sdl_len);
140 if_getmtu(char *name)
142 struct ifaddrs *ifap, *ifa;
146 if (getifaddrs(&ifap) < 0)
148 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
149 if (strcmp(ifa->ifa_name, name) == 0) {
158 #ifdef SIOCGIFMTU /* XXX: this ifdef may not be necessary */
163 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
166 ifr.ifr_addr.sa_family = AF_INET6;
167 strncpy(ifr.ifr_name, name,
168 sizeof(ifr.ifr_name));
169 if (ioctl(s, SIOCGIFMTU, (caddr_t)&ifr) < 0) {
182 /* give interface index and its old flags, then new flags returned */
184 if_getflags(int ifindex, int oifflags)
189 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
190 syslog(LOG_ERR, "<%s> socket: %s", __func__,
192 return (oifflags & ~IFF_UP);
195 if_indextoname(ifindex, ifr.ifr_name);
196 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) {
197 syslog(LOG_ERR, "<%s> ioctl:SIOCGIFFLAGS: failed for %s",
198 __func__, ifr.ifr_name);
200 return (oifflags & ~IFF_UP);
203 return (ifr.ifr_flags);
206 #define ROUNDUP8(a) (1 + (((a) - 1) | 7))
208 lladdropt_length(struct sockaddr_dl *sdl)
210 switch (sdl->sdl_type) {
212 return(ROUNDUP8(ETHER_ADDR_LEN + 2));
219 lladdropt_fill(struct sockaddr_dl *sdl, struct nd_opt_hdr *ndopt)
223 ndopt->nd_opt_type = ND_OPT_SOURCE_LINKADDR; /* fixed */
225 switch (sdl->sdl_type) {
227 ndopt->nd_opt_len = (ROUNDUP8(ETHER_ADDR_LEN + 2)) >> 3;
228 addr = (char *)(ndopt + 1);
229 memcpy(addr, LLADDR(sdl), ETHER_ADDR_LEN);
232 syslog(LOG_ERR, "<%s> unsupported link type(%d)",
233 __func__, sdl->sdl_type);
245 int mib[6] = {CTL_NET, AF_ROUTE, 0, AF_INET6, NET_RT_DUMP, 0};
247 if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0)
253 #define FILTER_MATCH(type, filter) ((0x1 << type) & filter)
254 #define SIN6(s) ((struct sockaddr_in6 *)(s))
255 #define SDL(s) ((struct sockaddr_dl *)(s))
257 get_next_msg(char *buf, char *lim, int ifindex, size_t *lenp, int filter)
259 struct rt_msghdr *rtm;
260 struct ifa_msghdr *ifam;
261 struct sockaddr *sa, *dst, *gw, *ifa, *rti_info[RTAX_MAX];
264 for (rtm = (struct rt_msghdr *)buf;
265 rtm < (struct rt_msghdr *)lim;
266 rtm = (struct rt_msghdr *)(((char *)rtm) + rtm->rtm_msglen)) {
267 /* just for safety */
268 if (!rtm->rtm_msglen) {
269 syslog(LOG_WARNING, "<%s> rtm_msglen is 0 "
270 "(buf=%p lim=%p rtm=%p)", __func__,
274 if (FILTER_MATCH(rtm->rtm_type, filter) == 0) {
278 switch (rtm->rtm_type) {
282 /* address related checks */
283 sa = (struct sockaddr *)(rtm + 1);
284 get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
285 if ((dst = rti_info[RTAX_DST]) == NULL ||
286 dst->sa_family != AF_INET6)
289 if (IN6_IS_ADDR_LINKLOCAL(&SIN6(dst)->sin6_addr) ||
290 IN6_IS_ADDR_MULTICAST(&SIN6(dst)->sin6_addr))
293 if ((gw = rti_info[RTAX_GATEWAY]) == NULL ||
294 gw->sa_family != AF_LINK)
296 if (ifindex && SDL(gw)->sdl_index != ifindex)
299 if (rti_info[RTAX_NETMASK] == NULL)
303 *lenp = rtm->rtm_msglen;
308 ifam = (struct ifa_msghdr *)rtm;
310 /* address related checks */
311 sa = (struct sockaddr *)(ifam + 1);
312 get_rtaddrs(ifam->ifam_addrs, sa, rti_info);
313 if ((ifa = rti_info[RTAX_IFA]) == NULL ||
314 (ifa->sa_family != AF_INET &&
315 ifa->sa_family != AF_INET6))
318 if (ifa->sa_family == AF_INET6 &&
319 (IN6_IS_ADDR_LINKLOCAL(&SIN6(ifa)->sin6_addr) ||
320 IN6_IS_ADDR_MULTICAST(&SIN6(ifa)->sin6_addr)))
323 if (ifindex && ifam->ifam_index != ifindex)
327 *lenp = ifam->ifam_msglen;
332 *lenp = rtm->rtm_msglen;
345 struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
346 struct sockaddr *sa, *rti_info[RTAX_MAX];
348 sa = (struct sockaddr *)(rtm + 1);
349 get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
351 return(&SIN6(rti_info[RTAX_DST])->sin6_addr);
355 get_rtm_ifindex(char *buf)
357 struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
358 struct sockaddr *sa, *rti_info[RTAX_MAX];
360 sa = (struct sockaddr *)(rtm + 1);
361 get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
363 return(((struct sockaddr_dl *)rti_info[RTAX_GATEWAY])->sdl_index);
367 get_ifm_ifindex(char *buf)
369 struct if_msghdr *ifm = (struct if_msghdr *)buf;
371 return ((int)ifm->ifm_index);
375 get_ifam_ifindex(char *buf)
377 struct ifa_msghdr *ifam = (struct ifa_msghdr *)buf;
379 return ((int)ifam->ifam_index);
383 get_ifm_flags(char *buf)
385 struct if_msghdr *ifm = (struct if_msghdr *)buf;
387 return (ifm->ifm_flags);
391 get_prefixlen(char *buf)
393 struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
394 struct sockaddr *sa, *rti_info[RTAX_MAX];
397 sa = (struct sockaddr *)(rtm + 1);
398 get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
399 sa = rti_info[RTAX_NETMASK];
401 p = (u_char *)(&SIN6(sa)->sin6_addr);
402 lim = (u_char *)sa + sa->sa_len;
403 return prefixlen(p, lim);
407 prefixlen(u_char *p, u_char *lim)
411 for (masklen = 0; p < lim; p++) {
448 rtmsg_type(char *buf)
450 struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
452 return(rtm->rtm_type);
458 struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
460 return(rtm->rtm_msglen);
466 struct if_msghdr *ifm = (struct if_msghdr *)buf;
468 return(ifm->ifm_msglen);
472 * alloc buffer and get if_msghdrs block from kernel,
473 * and put them into the buffer
476 get_iflist(char **buf, size_t *size)
484 mib[4] = NET_RT_IFLIST;
487 if (sysctl(mib, 6, NULL, size, NULL, 0) < 0) {
488 syslog(LOG_ERR, "<%s> sysctl: iflist size get failed",
492 if ((*buf = malloc(*size)) == NULL) {
493 syslog(LOG_ERR, "<%s> malloc failed", __func__);
496 if (sysctl(mib, 6, *buf, size, NULL, 0) < 0) {
497 syslog(LOG_ERR, "<%s> sysctl: iflist get failed",
505 * alloc buffer and parse if_msghdrs block passed as arg,
506 * and init the buffer as list of pointers ot each of the if_msghdr.
509 parse_iflist(struct if_msghdr ***ifmlist_p, char *buf, size_t bufsize)
511 int iflentry_size, malloc_size;
512 struct if_msghdr *ifm;
513 struct ifa_msghdr *ifam;
517 * Estimate least size of an iflist entry, to be obtained from kernel.
518 * Should add sizeof(sockaddr) ??
520 iflentry_size = sizeof(struct if_msghdr);
521 /* roughly estimate max list size of pointers to each if_msghdr */
522 malloc_size = (bufsize/iflentry_size) * sizeof(size_t);
523 if ((*ifmlist_p = (struct if_msghdr **)malloc(malloc_size)) == NULL) {
524 syslog(LOG_ERR, "<%s> malloc failed", __func__);
529 for (ifm = (struct if_msghdr *)buf; ifm < (struct if_msghdr *)lim;) {
530 if (ifm->ifm_msglen == 0) {
531 syslog(LOG_WARNING, "<%s> ifm_msglen is 0 "
532 "(buf=%p lim=%p ifm=%p)", __func__,
537 if (ifm->ifm_type == RTM_IFINFO) {
538 (*ifmlist_p)[ifm->ifm_index] = ifm;
540 syslog(LOG_ERR, "out of sync parsing NET_RT_IFLIST\n"
541 "expected %d, got %d\n msglen = %d\n"
542 "buf:%p, ifm:%p, lim:%p\n",
543 RTM_IFINFO, ifm->ifm_type, ifm->ifm_msglen,
547 for (ifam = (struct ifa_msghdr *)
548 ((char *)ifm + ifm->ifm_msglen);
549 ifam < (struct ifa_msghdr *)lim;
550 ifam = (struct ifa_msghdr *)
551 ((char *)ifam + ifam->ifam_msglen)) {
552 /* just for safety */
553 if (!ifam->ifam_msglen) {
554 syslog(LOG_WARNING, "<%s> ifa_msglen is 0 "
555 "(buf=%p lim=%p ifam=%p)", __func__,
559 if (ifam->ifam_type != RTM_NEWADDR)
562 ifm = (struct if_msghdr *)ifam;
575 /* get iflist block from kernel */
576 get_iflist(&ifblock, &ifblock_size);
578 /* make list of pointers to each if_msghdr */
579 parse_iflist(&iflist, ifblock, ifblock_size);