]>
git.saurik.com Git - apple/network_cmds.git/blob - rtadvd.tproj/if.c
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 $
34 #include <sys/param.h>
35 #include <sys/socket.h>
36 #include <sys/sysctl.h>
37 #include <sys/ioctl.h>
39 #include <net/if_types.h>
40 #if defined(__FreeBSD__) || defined (__APPLE__)
41 # include <net/ethernet.h>
45 #include <net/if_ether.h>
47 #include <net/route.h>
48 #include <net/if_dl.h>
49 #include <netinet/in.h>
50 #include <netinet/icmp6.h>
52 # include <netinet/if_ether.h>
55 #include <netinet/if_ether.h>
65 #define ROUNDUP(a, size) \
66 (((a) & ((size)-1)) ? (1 + ((a) | ((size)-1))) : (a))
68 #define NEXT_SA(ap) (ap) = (struct sockaddr *) \
69 ((caddr_t)(ap) + ((ap)->sa_len ? ROUNDUP((ap)->sa_len,\
73 struct if_msghdr
**iflist
;
78 static void get_iflist
__P((char **buf
, size_t *size
));
79 static void parse_iflist
__P((struct if_msghdr
***ifmlist_p
, char *buf
,
83 get_rtaddrs(int addrs
, struct sockaddr
*sa
, struct sockaddr
**rti_info
)
87 for (i
= 0; i
< RTAX_MAX
; i
++) {
88 if (addrs
& (1 << i
)) {
98 if_nametosdl(char *name
)
100 int mib
[6] = {CTL_NET
, AF_ROUTE
, 0, 0, NET_RT_IFLIST
, 0};
101 char *buf
, *next
, *lim
;
103 struct if_msghdr
*ifm
;
104 struct sockaddr
*sa
, *rti_info
[RTAX_MAX
];
105 struct sockaddr_dl
*sdl
= NULL
, *ret_sdl
;
107 if (sysctl(mib
, 6, NULL
, &len
, NULL
, 0) < 0)
109 if ((buf
= malloc(len
)) == NULL
)
111 if (sysctl(mib
, 6, buf
, &len
, NULL
, 0) < 0) {
117 for (next
= buf
; next
< lim
; next
+= ifm
->ifm_msglen
) {
118 ifm
= (struct if_msghdr
*)next
;
119 if (ifm
->ifm_type
== RTM_IFINFO
) {
120 sa
= (struct sockaddr
*)(ifm
+ 1);
121 get_rtaddrs(ifm
->ifm_addrs
, sa
, rti_info
);
122 if ((sa
= rti_info
[RTAX_IFP
]) != NULL
) {
123 if (sa
->sa_family
== AF_LINK
) {
124 sdl
= (struct sockaddr_dl
*)sa
;
125 if (strlen(name
) != sdl
->sdl_nlen
)
126 continue; /* not same len */
127 if (strncmp(&sdl
->sdl_data
[0],
129 sdl
->sdl_nlen
) == 0) {
142 if ((ret_sdl
= malloc(sdl
->sdl_len
)) == NULL
)
144 memcpy((caddr_t
)ret_sdl
, (caddr_t
)sdl
, sdl
->sdl_len
);
149 if_getmtu(char *name
)
151 struct ifaddrs
*ifap
, *ifa
;
155 if (getifaddrs(&ifap
) < 0)
157 for (ifa
= ifap
; ifa
; ifa
= ifa
->ifa_next
) {
158 if (strcmp(ifa
->ifa_name
, name
) == 0) {
167 #ifdef SIOCGIFMTU /* XXX: this ifdef may not be necessary */
172 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0)
175 ifr
.ifr_addr
.sa_family
= AF_INET6
;
176 strncpy(ifr
.ifr_name
, name
,
177 sizeof(ifr
.ifr_name
));
178 if (ioctl(s
, SIOCGIFMTU
, (caddr_t
)&ifr
) < 0) {
191 /* give interface index and its old flags, then new flags returned */
193 if_getflags(int ifindex
, int oifflags
)
198 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0) {
199 syslog(LOG_ERR
, "<%s> socket: %s", __FUNCTION__
,
201 return (oifflags
& ~IFF_UP
);
204 if_indextoname(ifindex
, ifr
.ifr_name
);
205 if (ioctl(s
, SIOCGIFFLAGS
, (caddr_t
)&ifr
) < 0) {
206 syslog(LOG_ERR
, "<%s> ioctl:SIOCGIFFLAGS: failed for %s",
207 __FUNCTION__
, ifr
.ifr_name
);
209 return (oifflags
& ~IFF_UP
);
212 return (ifr
.ifr_flags
);
215 #define ROUNDUP8(a) (1 + (((a) - 1) | 7))
217 lladdropt_length(struct sockaddr_dl
*sdl
)
219 switch(sdl
->sdl_type
) {
221 return(ROUNDUP8(ETHER_ADDR_LEN
+ 2));
228 lladdropt_fill(struct sockaddr_dl
*sdl
, struct nd_opt_hdr
*ndopt
)
232 ndopt
->nd_opt_type
= ND_OPT_SOURCE_LINKADDR
; /* fixed */
234 switch(sdl
->sdl_type
) {
236 ndopt
->nd_opt_len
= (ROUNDUP8(ETHER_ADDR_LEN
+ 2)) >> 3;
237 addr
= (char *)(ndopt
+ 1);
238 memcpy(addr
, LLADDR(sdl
), ETHER_ADDR_LEN
);
242 "<%s> unsupported link type(%d)",
243 __FUNCTION__
, sdl
->sdl_type
);
255 int mib
[6] = {CTL_NET
, AF_ROUTE
, 0, AF_INET6
, NET_RT_DUMP
, 0};
257 if (sysctl(mib
, 6, NULL
, &len
, NULL
, 0) < 0)
263 #define FILTER_MATCH(type, filter) ((0x1 << type) & filter)
264 #define SIN6(s) ((struct sockaddr_in6 *)(s))
265 #define SDL(s) ((struct sockaddr_dl *)(s))
267 get_next_msg(char *buf
, char *lim
, int ifindex
, size_t *lenp
, int filter
)
269 struct rt_msghdr
*rtm
;
270 struct ifa_msghdr
*ifam
;
271 struct sockaddr
*sa
, *dst
, *gw
, *ifa
, *rti_info
[RTAX_MAX
];
274 for (rtm
= (struct rt_msghdr
*)buf
;
275 rtm
< (struct rt_msghdr
*)lim
;
276 rtm
= (struct rt_msghdr
*)(((char *)rtm
) + rtm
->rtm_msglen
)) {
277 /* just for safety */
278 if (!rtm
->rtm_msglen
) {
279 syslog(LOG_WARNING
, "<%s> rtm_msglen is 0 "
280 "(buf=%p lim=%p rtm=%p)", __FUNCTION__
,
284 if (FILTER_MATCH(rtm
->rtm_type
, filter
) == 0) {
288 switch (rtm
->rtm_type
) {
292 /* address related checks */
293 sa
= (struct sockaddr
*)(rtm
+ 1);
294 get_rtaddrs(rtm
->rtm_addrs
, sa
, rti_info
);
295 if ((dst
= rti_info
[RTAX_DST
]) == NULL
||
296 dst
->sa_family
!= AF_INET6
)
299 if (IN6_IS_ADDR_LINKLOCAL(&SIN6(dst
)->sin6_addr
) ||
300 IN6_IS_ADDR_MULTICAST(&SIN6(dst
)->sin6_addr
))
303 if ((gw
= rti_info
[RTAX_GATEWAY
]) == NULL
||
304 gw
->sa_family
!= AF_LINK
)
306 if (ifindex
&& SDL(gw
)->sdl_index
!= ifindex
)
309 if (rti_info
[RTAX_NETMASK
] == NULL
)
313 *lenp
= rtm
->rtm_msglen
;
318 ifam
= (struct ifa_msghdr
*)rtm
;
320 /* address related checks */
321 sa
= (struct sockaddr
*)(ifam
+ 1);
322 get_rtaddrs(ifam
->ifam_addrs
, sa
, rti_info
);
323 if ((ifa
= rti_info
[RTAX_IFA
]) == NULL
||
324 (ifa
->sa_family
!= AF_INET
&&
325 ifa
->sa_family
!= AF_INET6
))
328 if (ifa
->sa_family
== AF_INET6
&&
329 (IN6_IS_ADDR_LINKLOCAL(&SIN6(ifa
)->sin6_addr
) ||
330 IN6_IS_ADDR_MULTICAST(&SIN6(ifa
)->sin6_addr
)))
333 if (ifindex
&& ifam
->ifam_index
!= ifindex
)
337 *lenp
= ifam
->ifam_msglen
;
342 *lenp
= rtm
->rtm_msglen
;
355 struct rt_msghdr
*rtm
= (struct rt_msghdr
*)buf
;
356 struct sockaddr
*sa
, *rti_info
[RTAX_MAX
];
358 sa
= (struct sockaddr
*)(rtm
+ 1);
359 get_rtaddrs(rtm
->rtm_addrs
, sa
, rti_info
);
361 return(&SIN6(rti_info
[RTAX_DST
])->sin6_addr
);
365 get_rtm_ifindex(char *buf
)
367 struct rt_msghdr
*rtm
= (struct rt_msghdr
*)buf
;
368 struct sockaddr
*sa
, *rti_info
[RTAX_MAX
];
370 sa
= (struct sockaddr
*)(rtm
+ 1);
371 get_rtaddrs(rtm
->rtm_addrs
, sa
, rti_info
);
373 return(((struct sockaddr_dl
*)rti_info
[RTAX_GATEWAY
])->sdl_index
);
377 get_ifm_ifindex(char *buf
)
379 struct if_msghdr
*ifm
= (struct if_msghdr
*)buf
;
381 return ((int)ifm
->ifm_index
);
385 get_ifam_ifindex(char *buf
)
387 struct ifa_msghdr
*ifam
= (struct ifa_msghdr
*)buf
;
389 return ((int)ifam
->ifam_index
);
393 get_ifm_flags(char *buf
)
395 struct if_msghdr
*ifm
= (struct if_msghdr
*)buf
;
397 return (ifm
->ifm_flags
);
401 get_prefixlen(char *buf
)
403 struct rt_msghdr
*rtm
= (struct rt_msghdr
*)buf
;
404 struct sockaddr
*sa
, *rti_info
[RTAX_MAX
];
407 sa
= (struct sockaddr
*)(rtm
+ 1);
408 get_rtaddrs(rtm
->rtm_addrs
, sa
, rti_info
);
409 sa
= rti_info
[RTAX_NETMASK
];
411 p
= (u_char
*)(&SIN6(sa
)->sin6_addr
);
412 lim
= (u_char
*)sa
+ sa
->sa_len
;
413 return prefixlen(p
, lim
);
417 prefixlen(u_char
*p
, u_char
*lim
)
421 for (masklen
= 0; p
< lim
; p
++) {
458 rtmsg_type(char *buf
)
460 struct rt_msghdr
*rtm
= (struct rt_msghdr
*)buf
;
462 return(rtm
->rtm_type
);
468 struct rt_msghdr
*rtm
= (struct rt_msghdr
*)buf
;
470 return(rtm
->rtm_msglen
);
476 struct if_msghdr
*ifm
= (struct if_msghdr
*)buf
;
478 return(ifm
->ifm_msglen
);
482 * alloc buffer and get if_msghdrs block from kernel,
483 * and put them into the buffer
486 get_iflist(char **buf
, size_t *size
)
494 mib
[4] = NET_RT_IFLIST
;
497 if (sysctl(mib
, 6, NULL
, size
, NULL
, 0) < 0) {
498 syslog(LOG_ERR
, "<%s> sysctl: iflist size get failed",
502 if ((*buf
= malloc(*size
)) == NULL
) {
503 syslog(LOG_ERR
, "<%s> malloc failed", __FUNCTION__
);
506 if (sysctl(mib
, 6, *buf
, size
, NULL
, 0) < 0) {
507 syslog(LOG_ERR
, "<%s> sysctl: iflist get failed",
515 * alloc buffer and parse if_msghdrs block passed as arg,
516 * and init the buffer as list of pointers ot each of the if_msghdr.
519 parse_iflist(struct if_msghdr
***ifmlist_p
, char *buf
, size_t bufsize
)
521 int iflentry_size
, malloc_size
;
522 struct if_msghdr
*ifm
;
523 struct ifa_msghdr
*ifam
;
527 * Estimate least size of an iflist entry, to be obtained from kernel.
528 * Should add sizeof(sockaddr) ??
530 iflentry_size
= sizeof(struct if_msghdr
);
531 /* roughly estimate max list size of pointers to each if_msghdr */
532 malloc_size
= (bufsize
/iflentry_size
) * sizeof(size_t);
533 if ((*ifmlist_p
= (struct if_msghdr
**)malloc(malloc_size
)) == NULL
) {
534 syslog(LOG_ERR
, "<%s> malloc failed", __FUNCTION__
);
539 for (ifm
= (struct if_msghdr
*)buf
; ifm
< (struct if_msghdr
*)lim
;) {
540 if (ifm
->ifm_msglen
== 0) {
541 syslog(LOG_WARNING
, "<%s> ifm_msglen is 0 "
542 "(buf=%p lim=%p ifm=%p)", __FUNCTION__
,
547 if (ifm
->ifm_type
== RTM_IFINFO
) {
548 (*ifmlist_p
)[ifm
->ifm_index
] = ifm
;
550 syslog(LOG_ERR
, "out of sync parsing NET_RT_IFLIST\n"
551 "expected %d, got %d\n msglen = %d\n"
552 "buf:%p, ifm:%p, lim:%p\n",
553 RTM_IFINFO
, ifm
->ifm_type
, ifm
->ifm_msglen
,
557 for (ifam
= (struct ifa_msghdr
*)
558 ((char *)ifm
+ ifm
->ifm_msglen
);
559 ifam
< (struct ifa_msghdr
*)lim
;
560 ifam
= (struct ifa_msghdr
*)
561 ((char *)ifam
+ ifam
->ifam_msglen
)) {
562 /* just for safety */
563 if (!ifam
->ifam_msglen
) {
564 syslog(LOG_WARNING
, "<%s> ifa_msglen is 0 "
565 "(buf=%p lim=%p ifam=%p)", __FUNCTION__
,
569 if (ifam
->ifam_type
!= RTM_NEWADDR
)
572 ifm
= (struct if_msghdr
*)ifam
;
585 /* get iflist block from kernel */
586 get_iflist(&ifblock
, &ifblock_size
);
588 /* make list of pointers to each if_msghdr */
589 parse_iflist(&iflist
, ifblock
, ifblock_size
);