1 /* $KAME: if.c,v 1.17 2001/01/21 15:27:30 itojun Exp $ */
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * $FreeBSD: src/usr.sbin/rtadvd/if.c,v 1.2.2.3 2001/07/03 11:02:14 ume Exp $
32 * $DragonFly: src/usr.sbin/rtadvd/if.c,v 1.6 2005/12/05 00:56:37 swildner Exp $
35 #include <sys/param.h>
36 #include <sys/socket.h>
37 #include <sys/sysctl.h>
38 #include <sys/ioctl.h>
40 #include <net/if_types.h>
42 # include <net/ethernet.h>
46 #include <net/if_ether.h>
48 #include <net/route.h>
49 #include <net/if_dl.h>
50 #include <netinet/in.h>
51 #include <netinet/icmp6.h>
53 # include <netinet/if_ether.h>
56 #include <netinet/if_ether.h>
66 #define ROUNDUP(a, size) \
67 (((a) & ((size)-1)) ? (1 + ((a) | ((size)-1))) : (a))
69 #define NEXT_SA(ap) (ap) = (struct sockaddr *) \
70 ((caddr_t)(ap) + ((ap)->sa_len ? ROUNDUP((ap)->sa_len,\
74 struct if_msghdr
**iflist
;
79 static void get_iflist(char **buf
, size_t *size
);
80 static void parse_iflist(struct if_msghdr
***ifmlist_p
, char *buf
,
84 get_rtaddrs(int addrs
, struct sockaddr
*sa
, struct sockaddr
**rti_info
)
88 for (i
= 0; i
< RTAX_MAX
; i
++) {
89 if (addrs
& (1 << i
)) {
99 if_nametosdl(char *name
)
101 int mib
[6] = {CTL_NET
, AF_ROUTE
, 0, 0, NET_RT_IFLIST
, 0};
102 char *buf
, *next
, *lim
;
104 struct if_msghdr
*ifm
;
105 struct sockaddr
*sa
, *rti_info
[RTAX_MAX
];
106 struct sockaddr_dl
*sdl
= NULL
, *ret_sdl
;
108 if (sysctl(mib
, 6, NULL
, &len
, NULL
, 0) < 0)
110 if ((buf
= malloc(len
)) == NULL
)
112 if (sysctl(mib
, 6, buf
, &len
, NULL
, 0) < 0) {
118 for (next
= buf
; next
< lim
; next
+= ifm
->ifm_msglen
) {
119 ifm
= (struct if_msghdr
*)next
;
120 if (ifm
->ifm_type
== RTM_IFINFO
) {
121 sa
= (struct sockaddr
*)(ifm
+ 1);
122 get_rtaddrs(ifm
->ifm_addrs
, sa
, rti_info
);
123 if ((sa
= rti_info
[RTAX_IFP
]) != NULL
) {
124 if (sa
->sa_family
== AF_LINK
) {
125 sdl
= (struct sockaddr_dl
*)sa
;
126 if (strlen(name
) != sdl
->sdl_nlen
)
127 continue; /* not same len */
128 if (strncmp(&sdl
->sdl_data
[0],
130 sdl
->sdl_nlen
) == 0) {
143 if ((ret_sdl
= malloc(sdl
->sdl_len
)) == NULL
)
145 memcpy((caddr_t
)ret_sdl
, (caddr_t
)sdl
, sdl
->sdl_len
);
150 if_getmtu(char *name
)
152 struct ifaddrs
*ifap
, *ifa
;
156 if (getifaddrs(&ifap
) < 0)
158 for (ifa
= ifap
; ifa
; ifa
= ifa
->ifa_next
) {
159 if (strcmp(ifa
->ifa_name
, name
) == 0) {
168 #ifdef SIOCGIFMTU /* XXX: this ifdef may not be necessary */
173 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0)
176 ifr
.ifr_addr
.sa_family
= AF_INET6
;
177 strncpy(ifr
.ifr_name
, name
,
178 sizeof(ifr
.ifr_name
));
179 if (ioctl(s
, SIOCGIFMTU
, (caddr_t
)&ifr
) < 0) {
192 /* give interface index and its old flags, then new flags returned */
194 if_getflags(int ifindex
, int oifflags
)
199 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0) {
200 syslog(LOG_ERR
, "<%s> socket: %s", __func__
,
202 return (oifflags
& ~IFF_UP
);
205 if_indextoname(ifindex
, ifr
.ifr_name
);
206 if (ioctl(s
, SIOCGIFFLAGS
, (caddr_t
)&ifr
) < 0) {
207 syslog(LOG_ERR
, "<%s> ioctl:SIOCGIFFLAGS: failed for %s",
208 __func__
, ifr
.ifr_name
);
210 return (oifflags
& ~IFF_UP
);
213 return (ifr
.ifr_flags
);
216 #define ROUNDUP8(a) (1 + (((a) - 1) | 7))
218 lladdropt_length(struct sockaddr_dl
*sdl
)
220 switch(sdl
->sdl_type
) {
222 return(ROUNDUP8(ETHER_ADDR_LEN
+ 2));
229 lladdropt_fill(struct sockaddr_dl
*sdl
, struct nd_opt_hdr
*ndopt
)
233 ndopt
->nd_opt_type
= ND_OPT_SOURCE_LINKADDR
; /* fixed */
235 switch(sdl
->sdl_type
) {
237 ndopt
->nd_opt_len
= (ROUNDUP8(ETHER_ADDR_LEN
+ 2)) >> 3;
238 addr
= (char *)(ndopt
+ 1);
239 memcpy(addr
, LLADDR(sdl
), ETHER_ADDR_LEN
);
243 "<%s> unsupported link type(%d)",
244 __func__
, sdl
->sdl_type
);
256 int mib
[6] = {CTL_NET
, AF_ROUTE
, 0, AF_INET6
, NET_RT_DUMP
, 0};
258 if (sysctl(mib
, 6, NULL
, &len
, NULL
, 0) < 0)
264 #define FILTER_MATCH(type, filter) ((0x1 << type) & filter)
265 #define SIN6(s) ((struct sockaddr_in6 *)(s))
266 #define SDL(s) ((struct sockaddr_dl *)(s))
268 get_next_msg(char *buf
, char *lim
, int ifindex
, size_t *lenp
, int filter
)
270 struct rt_msghdr
*rtm
;
271 struct ifa_msghdr
*ifam
;
272 struct sockaddr
*sa
, *dst
, *gw
, *ifa
, *rti_info
[RTAX_MAX
];
275 for (rtm
= (struct rt_msghdr
*)buf
;
276 rtm
< (struct rt_msghdr
*)lim
;
277 rtm
= (struct rt_msghdr
*)(((char *)rtm
) + rtm
->rtm_msglen
)) {
278 /* just for safety */
279 if (!rtm
->rtm_msglen
) {
280 syslog(LOG_WARNING
, "<%s> rtm_msglen is 0 "
281 "(buf=%p lim=%p rtm=%p)", __func__
,
285 if (FILTER_MATCH(rtm
->rtm_type
, filter
) == 0) {
289 switch (rtm
->rtm_type
) {
293 /* address related checks */
294 sa
= (struct sockaddr
*)(rtm
+ 1);
295 get_rtaddrs(rtm
->rtm_addrs
, sa
, rti_info
);
296 if ((dst
= rti_info
[RTAX_DST
]) == NULL
||
297 dst
->sa_family
!= AF_INET6
)
300 if (IN6_IS_ADDR_LINKLOCAL(&SIN6(dst
)->sin6_addr
) ||
301 IN6_IS_ADDR_MULTICAST(&SIN6(dst
)->sin6_addr
))
304 if ((gw
= rti_info
[RTAX_GATEWAY
]) == NULL
||
305 gw
->sa_family
!= AF_LINK
)
307 if (ifindex
&& SDL(gw
)->sdl_index
!= ifindex
)
310 if (rti_info
[RTAX_NETMASK
] == NULL
)
314 *lenp
= rtm
->rtm_msglen
;
319 ifam
= (struct ifa_msghdr
*)rtm
;
321 /* address related checks */
322 sa
= (struct sockaddr
*)(ifam
+ 1);
323 get_rtaddrs(ifam
->ifam_addrs
, sa
, rti_info
);
324 if ((ifa
= rti_info
[RTAX_IFA
]) == NULL
||
325 (ifa
->sa_family
!= AF_INET
&&
326 ifa
->sa_family
!= AF_INET6
))
329 if (ifa
->sa_family
== AF_INET6
&&
330 (IN6_IS_ADDR_LINKLOCAL(&SIN6(ifa
)->sin6_addr
) ||
331 IN6_IS_ADDR_MULTICAST(&SIN6(ifa
)->sin6_addr
)))
334 if (ifindex
&& ifam
->ifam_index
!= ifindex
)
338 *lenp
= ifam
->ifam_msglen
;
343 *lenp
= rtm
->rtm_msglen
;
356 struct rt_msghdr
*rtm
= (struct rt_msghdr
*)buf
;
357 struct sockaddr
*sa
, *rti_info
[RTAX_MAX
];
359 sa
= (struct sockaddr
*)(rtm
+ 1);
360 get_rtaddrs(rtm
->rtm_addrs
, sa
, rti_info
);
362 return(&SIN6(rti_info
[RTAX_DST
])->sin6_addr
);
366 get_rtm_ifindex(char *buf
)
368 struct rt_msghdr
*rtm
= (struct rt_msghdr
*)buf
;
369 struct sockaddr
*sa
, *rti_info
[RTAX_MAX
];
371 sa
= (struct sockaddr
*)(rtm
+ 1);
372 get_rtaddrs(rtm
->rtm_addrs
, sa
, rti_info
);
374 return(((struct sockaddr_dl
*)rti_info
[RTAX_GATEWAY
])->sdl_index
);
378 get_ifm_ifindex(char *buf
)
380 struct if_msghdr
*ifm
= (struct if_msghdr
*)buf
;
382 return ((int)ifm
->ifm_index
);
386 get_ifam_ifindex(char *buf
)
388 struct ifa_msghdr
*ifam
= (struct ifa_msghdr
*)buf
;
390 return ((int)ifam
->ifam_index
);
394 get_ifm_flags(char *buf
)
396 struct if_msghdr
*ifm
= (struct if_msghdr
*)buf
;
398 return (ifm
->ifm_flags
);
402 get_prefixlen(char *buf
)
404 struct rt_msghdr
*rtm
= (struct rt_msghdr
*)buf
;
405 struct sockaddr
*sa
, *rti_info
[RTAX_MAX
];
408 sa
= (struct sockaddr
*)(rtm
+ 1);
409 get_rtaddrs(rtm
->rtm_addrs
, sa
, rti_info
);
410 sa
= rti_info
[RTAX_NETMASK
];
412 p
= (u_char
*)(&SIN6(sa
)->sin6_addr
);
413 lim
= (u_char
*)sa
+ sa
->sa_len
;
414 return prefixlen(p
, lim
);
418 prefixlen(u_char
*p
, u_char
*lim
)
422 for (masklen
= 0; p
< lim
; p
++) {
459 rtmsg_type(char *buf
)
461 struct rt_msghdr
*rtm
= (struct rt_msghdr
*)buf
;
463 return(rtm
->rtm_type
);
469 struct rt_msghdr
*rtm
= (struct rt_msghdr
*)buf
;
471 return(rtm
->rtm_msglen
);
477 struct if_msghdr
*ifm
= (struct if_msghdr
*)buf
;
479 return(ifm
->ifm_msglen
);
483 * alloc buffer and get if_msghdrs block from kernel,
484 * and put them into the buffer
487 get_iflist(char **buf
, size_t *size
)
495 mib
[4] = NET_RT_IFLIST
;
498 if (sysctl(mib
, 6, NULL
, size
, NULL
, 0) < 0) {
499 syslog(LOG_ERR
, "<%s> sysctl: iflist size get failed",
503 if ((*buf
= malloc(*size
)) == NULL
) {
504 syslog(LOG_ERR
, "<%s> malloc failed", __func__
);
507 if (sysctl(mib
, 6, *buf
, size
, NULL
, 0) < 0) {
508 syslog(LOG_ERR
, "<%s> sysctl: iflist get failed",
516 * alloc buffer and parse if_msghdrs block passed as arg,
517 * and init the buffer as list of pointers ot each of the if_msghdr.
520 parse_iflist(struct if_msghdr
***ifmlist_p
, char *buf
, size_t bufsize
)
522 int iflentry_size
, malloc_size
;
523 struct if_msghdr
*ifm
;
524 struct ifa_msghdr
*ifam
;
528 * Estimate least size of an iflist entry, to be obtained from kernel.
529 * Should add sizeof(sockaddr) ??
531 iflentry_size
= sizeof(struct if_msghdr
);
532 /* roughly estimate max list size of pointers to each if_msghdr */
533 malloc_size
= (bufsize
/iflentry_size
) * sizeof(size_t);
534 if ((*ifmlist_p
= (struct if_msghdr
**)malloc(malloc_size
)) == NULL
) {
535 syslog(LOG_ERR
, "<%s> malloc failed", __func__
);
540 for (ifm
= (struct if_msghdr
*)buf
; ifm
< (struct if_msghdr
*)lim
;) {
541 if (ifm
->ifm_msglen
== 0) {
542 syslog(LOG_WARNING
, "<%s> ifm_msglen is 0 "
543 "(buf=%p lim=%p ifm=%p)", __func__
,
548 if (ifm
->ifm_type
== RTM_IFINFO
) {
549 (*ifmlist_p
)[ifm
->ifm_index
] = ifm
;
551 syslog(LOG_ERR
, "out of sync parsing NET_RT_IFLIST\n"
552 "expected %d, got %d\n msglen = %d\n"
553 "buf:%p, ifm:%p, lim:%p\n",
554 RTM_IFINFO
, ifm
->ifm_type
, ifm
->ifm_msglen
,
558 for (ifam
= (struct ifa_msghdr
*)
559 ((char *)ifm
+ ifm
->ifm_msglen
);
560 ifam
< (struct ifa_msghdr
*)lim
;
561 ifam
= (struct ifa_msghdr
*)
562 ((char *)ifam
+ ifam
->ifam_msglen
)) {
563 /* just for safety */
564 if (!ifam
->ifam_msglen
) {
565 syslog(LOG_WARNING
, "<%s> ifa_msglen is 0 "
566 "(buf=%p lim=%p ifam=%p)", __func__
,
570 if (ifam
->ifam_type
!= RTM_NEWADDR
)
573 ifm
= (struct if_msghdr
*)ifam
;
586 /* get iflist block from kernel */
587 get_iflist(&ifblock
, &ifblock_size
);
589 /* make list of pointers to each if_msghdr */
590 parse_iflist(&iflist
, ifblock
, ifblock_size
);