1 /* $NetBSD: if.c,v 1.17 2003/09/23 18:15:50 itojun Exp $ */
2 /* $KAME: if.c,v 1.36 2004/11/30 22:32:01 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>
35 #include <sys/sysctl.h>
36 #include <sys/ioctl.h>
38 #include <net/if_types.h>
40 #include <net/if_ether.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 struct ifaddrs
*ifap
, *ifa
;
89 struct sockaddr_dl
*sdl
;
91 if (getifaddrs(&ifap
) != 0)
94 for (ifa
= ifap
; ifa
; ifa
= ifa
->ifa_next
) {
95 if (strcmp(ifa
->ifa_name
, name
) != 0)
97 if (ifa
->ifa_addr
->sa_family
!= AF_LINK
)
100 sdl
= malloc(ifa
->ifa_addr
->sa_len
);
104 memcpy(sdl
, ifa
->ifa_addr
, ifa
->ifa_addr
->sa_len
);
114 if_getmtu(char *name
)
116 struct ifaddrs
*ifap
, *ifa
;
120 if (getifaddrs(&ifap
) < 0)
122 for (ifa
= ifap
; ifa
; ifa
= ifa
->ifa_next
) {
123 if (strcmp(ifa
->ifa_name
, name
) == 0) {
132 #ifdef SIOCGIFMTU /* XXX: this ifdef may not be necessary */
137 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0)
140 ifr
.ifr_addr
.sa_family
= AF_INET6
;
141 strncpy(ifr
.ifr_name
, name
,
142 sizeof(ifr
.ifr_name
));
143 if (ioctl(s
, SIOCGIFMTU
, (caddr_t
)&ifr
) < 0) {
156 /* give interface index and its old flags, then new flags returned */
158 if_getflags(int ifindex
, int oifflags
)
163 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0) {
164 syslog(LOG_ERR
, "<%s> socket: %s", __func__
,
166 return (oifflags
& ~IFF_UP
);
169 if_indextoname(ifindex
, ifr
.ifr_name
);
170 if (ioctl(s
, SIOCGIFFLAGS
, (caddr_t
)&ifr
) < 0) {
171 syslog(LOG_ERR
, "<%s> ioctl:SIOCGIFFLAGS: failed for %s",
172 __func__
, ifr
.ifr_name
);
174 return (oifflags
& ~IFF_UP
);
177 return (ifr
.ifr_flags
);
180 #define ROUNDUP8(a) (1 + (((a) - 1) | 7))
182 lladdropt_length(struct sockaddr_dl
*sdl
)
184 switch (sdl
->sdl_type
) {
187 return(ROUNDUP8(ETHER_ADDR_LEN
+ 2));
194 lladdropt_fill(struct sockaddr_dl
*sdl
, struct nd_opt_hdr
*ndopt
)
198 ndopt
->nd_opt_type
= ND_OPT_SOURCE_LINKADDR
; /* fixed */
200 switch (sdl
->sdl_type
) {
203 ndopt
->nd_opt_len
= (ROUNDUP8(ETHER_ADDR_LEN
+ 2)) >> 3;
204 addr
= (char *)(ndopt
+ 1);
205 memcpy(addr
, LLADDR(sdl
), ETHER_ADDR_LEN
);
208 syslog(LOG_ERR
, "<%s> unsupported link type(%d)",
209 __func__
, sdl
->sdl_type
);
216 #define FILTER_MATCH(type, filter) ((0x1 << type) & filter)
217 #define SIN6(s) ((struct sockaddr_in6 *)(s))
218 #define SDL(s) ((struct sockaddr_dl *)(s))
220 get_next_msg(char *buf
, char *lim
, int ifindex
, size_t *lenp
, int filter
)
222 struct rt_msghdr
*rtm
;
223 struct ifa_msghdr
*ifam
;
224 struct sockaddr
*sa
, *dst
, *gw
, *ifa
, *rti_info
[RTAX_MAX
];
227 for (rtm
= (struct rt_msghdr
*)buf
;
228 rtm
< (struct rt_msghdr
*)lim
;
229 rtm
= (struct rt_msghdr
*)(((char *)rtm
) + rtm
->rtm_msglen
)) {
230 /* just for safety */
231 if (!rtm
->rtm_msglen
) {
232 syslog(LOG_WARNING
, "<%s> rtm_msglen is 0 "
233 "(buf=%p lim=%p rtm=%p)", __func__
,
237 if (FILTER_MATCH(rtm
->rtm_type
, filter
) == 0) {
241 switch (rtm
->rtm_type
) {
245 /* address related checks */
246 sa
= (struct sockaddr
*)(rtm
+ 1);
247 get_rtaddrs(rtm
->rtm_addrs
, sa
, rti_info
);
248 if ((dst
= rti_info
[RTAX_DST
]) == NULL
||
249 dst
->sa_family
!= AF_INET6
)
252 if (IN6_IS_ADDR_LINKLOCAL(&SIN6(dst
)->sin6_addr
) ||
253 IN6_IS_ADDR_MULTICAST(&SIN6(dst
)->sin6_addr
))
256 if ((gw
= rti_info
[RTAX_GATEWAY
]) == NULL
||
257 gw
->sa_family
!= AF_LINK
)
259 if (ifindex
&& SDL(gw
)->sdl_index
!= ifindex
)
262 if (rti_info
[RTAX_NETMASK
] == NULL
)
266 *lenp
= rtm
->rtm_msglen
;
271 ifam
= (struct ifa_msghdr
*)rtm
;
273 /* address related checks */
274 sa
= (struct sockaddr
*)(ifam
+ 1);
275 get_rtaddrs(ifam
->ifam_addrs
, sa
, rti_info
);
276 if ((ifa
= rti_info
[RTAX_IFA
]) == NULL
||
277 (ifa
->sa_family
!= AF_INET
&&
278 ifa
->sa_family
!= AF_INET6
))
281 if (ifa
->sa_family
== AF_INET6
&&
282 (IN6_IS_ADDR_LINKLOCAL(&SIN6(ifa
)->sin6_addr
) ||
283 IN6_IS_ADDR_MULTICAST(&SIN6(ifa
)->sin6_addr
)))
286 if (ifindex
&& ifam
->ifam_index
!= ifindex
)
290 *lenp
= ifam
->ifam_msglen
;
295 *lenp
= rtm
->rtm_msglen
;
308 struct rt_msghdr
*rtm
= (struct rt_msghdr
*)buf
;
309 struct sockaddr
*sa
, *rti_info
[RTAX_MAX
];
311 sa
= (struct sockaddr
*)(rtm
+ 1);
312 get_rtaddrs(rtm
->rtm_addrs
, sa
, rti_info
);
314 return(&SIN6(rti_info
[RTAX_DST
])->sin6_addr
);
318 get_rtm_ifindex(char *buf
)
320 struct rt_msghdr
*rtm
= (struct rt_msghdr
*)buf
;
321 struct sockaddr
*sa
, *rti_info
[RTAX_MAX
];
323 sa
= (struct sockaddr
*)(rtm
+ 1);
324 get_rtaddrs(rtm
->rtm_addrs
, sa
, rti_info
);
326 return(((struct sockaddr_dl
*)rti_info
[RTAX_GATEWAY
])->sdl_index
);
330 get_ifm_ifindex(char *buf
)
332 struct if_msghdr
*ifm
= (struct if_msghdr
*)buf
;
334 return ((int)ifm
->ifm_index
);
338 get_ifam_ifindex(char *buf
)
340 struct ifa_msghdr
*ifam
= (struct ifa_msghdr
*)buf
;
342 return ((int)ifam
->ifam_index
);
346 get_ifm_flags(char *buf
)
348 struct if_msghdr
*ifm
= (struct if_msghdr
*)buf
;
350 return (ifm
->ifm_flags
);
354 get_prefixlen(char *buf
)
356 struct rt_msghdr
*rtm
= (struct rt_msghdr
*)buf
;
357 struct sockaddr
*sa
, *rti_info
[RTAX_MAX
];
360 sa
= (struct sockaddr
*)(rtm
+ 1);
361 get_rtaddrs(rtm
->rtm_addrs
, sa
, rti_info
);
362 sa
= rti_info
[RTAX_NETMASK
];
364 p
= (u_char
*)(&SIN6(sa
)->sin6_addr
);
365 lim
= (u_char
*)sa
+ sa
->sa_len
;
366 return prefixlen(p
, lim
);
370 prefixlen(u_char
*p
, u_char
*lim
)
374 for (masklen
= 0; p
< lim
; p
++) {
411 rtmsg_type(char *buf
)
413 struct rt_msghdr
*rtm
= (struct rt_msghdr
*)buf
;
415 return(rtm
->rtm_type
);
421 struct rt_msghdr
*rtm
= (struct rt_msghdr
*)buf
;
423 return(rtm
->rtm_msglen
);
429 struct if_msghdr
*ifm
= (struct if_msghdr
*)buf
;
431 return(ifm
->ifm_msglen
);
435 * alloc buffer and get if_msghdrs block from kernel,
436 * and put them into the buffer
439 get_iflist(char **buf
, size_t *size
)
447 mib
[4] = NET_RT_IFLIST
;
450 if (sysctl(mib
, 6, NULL
, size
, NULL
, 0) < 0) {
451 syslog(LOG_ERR
, "<%s> sysctl: iflist size get failed",
455 if ((*buf
= malloc(*size
)) == NULL
) {
456 syslog(LOG_ERR
, "<%s> malloc failed", __func__
);
459 if (sysctl(mib
, 6, *buf
, size
, NULL
, 0) < 0) {
460 syslog(LOG_ERR
, "<%s> sysctl: iflist get failed",
468 * alloc buffer and parse if_msghdrs block passed as arg,
469 * and init the buffer as list of pointers ot each of the if_msghdr.
472 parse_iflist(struct if_msghdr
***ifmlist_p
, char *buf
, size_t bufsize
)
474 int iflentry_size
, malloc_size
;
475 struct if_msghdr
*ifm
;
476 struct ifa_msghdr
*ifam
;
480 * Estimate least size of an iflist entry, to be obtained from kernel.
481 * Should add sizeof(sockaddr) ??
483 iflentry_size
= sizeof(struct if_msghdr
);
484 /* roughly estimate max list size of pointers to each if_msghdr */
485 malloc_size
= (bufsize
/iflentry_size
) * sizeof(void *);
486 if ((*ifmlist_p
= (struct if_msghdr
**)malloc(malloc_size
)) == NULL
) {
487 syslog(LOG_ERR
, "<%s> malloc failed", __func__
);
492 for (ifm
= (struct if_msghdr
*)buf
; ifm
< (struct if_msghdr
*)lim
;) {
493 if (ifm
->ifm_msglen
== 0) {
494 syslog(LOG_WARNING
, "<%s> ifm_msglen is 0 "
495 "(buf=%p lim=%p ifm=%p)", __func__
,
500 if (ifm
->ifm_type
== RTM_IFINFO
) {
501 (*ifmlist_p
)[ifm
->ifm_index
] = ifm
;
503 syslog(LOG_ERR
, "out of sync parsing NET_RT_IFLIST\n"
504 "expected %d, got %d\n msglen = %d\n"
505 "buf:%p, ifm:%p, lim:%p\n",
506 RTM_IFINFO
, ifm
->ifm_type
, ifm
->ifm_msglen
,
510 for (ifam
= (struct ifa_msghdr
*)
511 ((char *)ifm
+ ifm
->ifm_msglen
);
512 ifam
< (struct ifa_msghdr
*)lim
;
513 ifam
= (struct ifa_msghdr
*)
514 ((char *)ifam
+ ifam
->ifam_msglen
)) {
515 /* just for safety */
516 if (!ifam
->ifam_msglen
) {
517 syslog(LOG_WARNING
, "<%s> ifa_msglen is 0 "
518 "(buf=%p lim=%p ifam=%p)", __func__
,
522 if (ifam
->ifam_type
!= RTM_NEWADDR
)
525 ifm
= (struct if_msghdr
*)ifam
;
538 /* get iflist block from kernel */
539 get_iflist(&ifblock
, &ifblock_size
);
541 /* make list of pointers to each if_msghdr */
542 parse_iflist(&iflist
, ifblock
, ifblock_size
);