]>
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
32 #include <sys/param.h>
33 #include <sys/socket.h>
34 #include <sys/sysctl.h>
35 #include <sys/ioctl.h>
37 #include <net/if_types.h>
38 #include <net/ethernet.h>
40 #include <net/route.h>
41 #include <net/if_dl.h>
42 #include <netinet/in.h>
43 #include <netinet/icmp6.h>
52 #define ROUNDUP(a, size) \
53 (((a) & ((size)-1)) ? (1 + ((a) | ((size)-1))) : (a))
55 #define NEXT_SA(ap) (ap) = (struct sockaddr *) \
56 ((caddr_t)(ap) + ((ap)->sa_len ? ROUNDUP((ap)->sa_len,\
60 struct if_msghdr
**iflist
;
65 static void get_iflist(char **buf
, size_t *size
);
66 static void parse_iflist(struct if_msghdr
***ifmlist_p
, char *buf
,
70 get_rtaddrs(int addrs
, struct sockaddr
*sa
, struct sockaddr
**rti_info
)
74 for (i
= 0; i
< RTAX_MAX
; i
++) {
75 if (addrs
& (1 << i
)) {
85 if_nametosdl(char *name
)
87 int mib
[6] = {CTL_NET
, AF_ROUTE
, 0, 0, NET_RT_IFLIST
, 0};
88 char *buf
, *next
, *lim
;
90 struct if_msghdr
*ifm
;
91 struct sockaddr
*sa
, *rti_info
[RTAX_MAX
];
92 struct sockaddr_dl
*sdl
= NULL
, *ret_sdl
= NULL
;
94 if (sysctl(mib
, 6, NULL
, &len
, NULL
, 0) < 0)
96 if ((buf
= malloc(len
)) == NULL
)
98 if (sysctl(mib
, 6, buf
, &len
, NULL
, 0) < 0) {
104 for (next
= buf
; next
< lim
; next
+= ifm
->ifm_msglen
) {
105 ifm
= (struct if_msghdr
*)next
;
106 if (ifm
->ifm_type
== RTM_IFINFO
) {
107 sa
= (struct sockaddr
*)(ifm
+ 1);
108 get_rtaddrs(ifm
->ifm_addrs
, sa
, rti_info
);
109 if ((sa
= rti_info
[RTAX_IFP
]) != NULL
) {
110 if (sa
->sa_family
== AF_LINK
) {
111 sdl
= (struct sockaddr_dl
*)sa
;
112 if (strlen(name
) != sdl
->sdl_nlen
)
113 continue; /* not same len */
114 if (strncmp(&sdl
->sdl_data
[0],
116 sdl
->sdl_nlen
) == 0) {
129 if (sdl
== NULL
|| (ret_sdl
= malloc(sdl
->sdl_len
)) == NULL
)
131 memcpy((caddr_t
)ret_sdl
, (caddr_t
)sdl
, sdl
->sdl_len
);
139 if_getmtu(char *name
)
141 struct ifaddrs
*ifap
, *ifa
;
145 if (getifaddrs(&ifap
) < 0)
147 for (ifa
= ifap
; ifa
; ifa
= ifa
->ifa_next
) {
148 if (strcmp(ifa
->ifa_name
, name
) == 0) {
157 #ifdef SIOCGIFMTU /* XXX: this ifdef may not be necessary */
162 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0)
165 ifr
.ifr_addr
.sa_family
= AF_INET6
;
166 strncpy(ifr
.ifr_name
, name
,
167 sizeof(ifr
.ifr_name
));
168 if (ioctl(s
, SIOCGIFMTU
, (caddr_t
)&ifr
) < 0) {
181 /* give interface index and its old flags, then new flags returned */
183 if_getflags(int ifindex
, int oifflags
)
188 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0) {
189 syslog(LOG_ERR
, "<%s> socket: %s", __func__
,
191 return (oifflags
& ~IFF_UP
);
194 if_indextoname(ifindex
, ifr
.ifr_name
);
195 if (ioctl(s
, SIOCGIFFLAGS
, (caddr_t
)&ifr
) < 0) {
196 syslog(LOG_ERR
, "<%s> ioctl:SIOCGIFFLAGS: failed for %s",
197 __func__
, ifr
.ifr_name
);
199 return (oifflags
& ~IFF_UP
);
202 return (ifr
.ifr_flags
);
205 #define ROUNDUP8(a) (1 + (((a) - 1) | 7))
207 lladdropt_length(struct sockaddr_dl
*sdl
)
209 switch (sdl
->sdl_type
) {
211 return(ROUNDUP8(ETHER_ADDR_LEN
+ 2));
218 lladdropt_fill(struct sockaddr_dl
*sdl
, struct nd_opt_hdr
*ndopt
)
222 ndopt
->nd_opt_type
= ND_OPT_SOURCE_LINKADDR
; /* fixed */
224 switch (sdl
->sdl_type
) {
226 ndopt
->nd_opt_len
= (ROUNDUP8(ETHER_ADDR_LEN
+ 2)) >> 3;
227 addr
= (char *)(ndopt
+ 1);
228 memcpy(addr
, LLADDR(sdl
), ETHER_ADDR_LEN
);
231 syslog(LOG_ERR
, "<%s> unsupported link type(%d)",
232 __func__
, sdl
->sdl_type
);
244 int mib
[6] = {CTL_NET
, AF_ROUTE
, 0, AF_INET6
, NET_RT_DUMP
, 0};
246 if (sysctl(mib
, 6, NULL
, &len
, NULL
, 0) < 0)
252 #define FILTER_MATCH(type, filter) ((0x1 << type) & filter)
253 #define SIN6(s) ((struct sockaddr_in6 *)(s))
254 #define SDL(s) ((struct sockaddr_dl *)(s))
256 get_next_msg(char *buf
, char *lim
, int ifindex
, size_t *lenp
, int filter
)
258 struct rt_msghdr
*rtm
;
259 struct ifa_msghdr
*ifam
;
260 struct sockaddr
*sa
, *dst
, *gw
, *ifa
, *rti_info
[RTAX_MAX
];
263 for (rtm
= (struct rt_msghdr
*)buf
;
264 rtm
< (struct rt_msghdr
*)lim
;
265 rtm
= (struct rt_msghdr
*)(((char *)rtm
) + rtm
->rtm_msglen
)) {
266 /* just for safety */
267 if (!rtm
->rtm_msglen
) {
268 syslog(LOG_WARNING
, "<%s> rtm_msglen is 0 "
269 "(buf=%p lim=%p rtm=%p)", __func__
,
273 if (FILTER_MATCH(rtm
->rtm_type
, filter
) == 0) {
277 switch (rtm
->rtm_type
) {
281 /* address related checks */
282 sa
= (struct sockaddr
*)(rtm
+ 1);
283 get_rtaddrs(rtm
->rtm_addrs
, sa
, rti_info
);
284 if ((dst
= rti_info
[RTAX_DST
]) == NULL
||
285 dst
->sa_family
!= AF_INET6
)
288 if (IN6_IS_ADDR_LINKLOCAL(&SIN6(dst
)->sin6_addr
) ||
289 IN6_IS_ADDR_MULTICAST(&SIN6(dst
)->sin6_addr
))
292 if ((gw
= rti_info
[RTAX_GATEWAY
]) == NULL
||
293 gw
->sa_family
!= AF_LINK
)
295 if (ifindex
&& SDL(gw
)->sdl_index
!= ifindex
)
298 if (rti_info
[RTAX_NETMASK
] == NULL
)
302 *lenp
= rtm
->rtm_msglen
;
307 ifam
= (struct ifa_msghdr
*)rtm
;
309 /* address related checks */
310 sa
= (struct sockaddr
*)(ifam
+ 1);
311 get_rtaddrs(ifam
->ifam_addrs
, sa
, rti_info
);
312 if ((ifa
= rti_info
[RTAX_IFA
]) == NULL
||
313 (ifa
->sa_family
!= AF_INET
&&
314 ifa
->sa_family
!= AF_INET6
))
317 if (ifa
->sa_family
== AF_INET6
&&
318 (IN6_IS_ADDR_LINKLOCAL(&SIN6(ifa
)->sin6_addr
) ||
319 IN6_IS_ADDR_MULTICAST(&SIN6(ifa
)->sin6_addr
)))
322 if (ifindex
&& ifam
->ifam_index
!= ifindex
)
326 *lenp
= ifam
->ifam_msglen
;
331 *lenp
= rtm
->rtm_msglen
;
344 struct rt_msghdr
*rtm
= (struct rt_msghdr
*)buf
;
345 struct sockaddr
*sa
, *rti_info
[RTAX_MAX
];
347 sa
= (struct sockaddr
*)(rtm
+ 1);
348 get_rtaddrs(rtm
->rtm_addrs
, sa
, rti_info
);
350 return(&SIN6(rti_info
[RTAX_DST
])->sin6_addr
);
354 get_rtm_ifindex(char *buf
)
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(((struct sockaddr_dl
*)rti_info
[RTAX_GATEWAY
])->sdl_index
);
366 get_ifm_ifindex(char *buf
)
368 struct if_msghdr
*ifm
= (struct if_msghdr
*)buf
;
370 return ((int)ifm
->ifm_index
);
374 get_ifam_ifindex(char *buf
)
376 struct ifa_msghdr
*ifam
= (struct ifa_msghdr
*)buf
;
378 return ((int)ifam
->ifam_index
);
382 get_ifm_flags(char *buf
)
384 struct if_msghdr
*ifm
= (struct if_msghdr
*)buf
;
386 return (ifm
->ifm_flags
);
390 get_prefixlen(char *buf
)
392 struct rt_msghdr
*rtm
= (struct rt_msghdr
*)buf
;
393 struct sockaddr
*sa
, *rti_info
[RTAX_MAX
];
396 sa
= (struct sockaddr
*)(rtm
+ 1);
397 get_rtaddrs(rtm
->rtm_addrs
, sa
, rti_info
);
398 sa
= rti_info
[RTAX_NETMASK
];
400 p
= (u_char
*)(&SIN6(sa
)->sin6_addr
);
401 lim
= (u_char
*)sa
+ sa
->sa_len
;
402 return prefixlen(p
, lim
);
406 prefixlen(u_char
*p
, u_char
*lim
)
410 for (masklen
= 0; p
< lim
; p
++) {
447 rtmsg_type(char *buf
)
449 struct rt_msghdr
*rtm
= (struct rt_msghdr
*)buf
;
451 return(rtm
->rtm_type
);
457 struct rt_msghdr
*rtm
= (struct rt_msghdr
*)buf
;
459 return(rtm
->rtm_msglen
);
465 struct if_msghdr
*ifm
= (struct if_msghdr
*)buf
;
467 return(ifm
->ifm_msglen
);
471 * alloc buffer and get if_msghdrs block from kernel,
472 * and put them into the buffer
475 get_iflist(char **buf
, size_t *size
)
483 mib
[4] = NET_RT_IFLIST
;
486 if (sysctl(mib
, 6, NULL
, size
, NULL
, 0) < 0) {
487 syslog(LOG_ERR
, "<%s> sysctl: iflist size get failed",
491 if ((*buf
= malloc(*size
)) == NULL
) {
492 syslog(LOG_ERR
, "<%s> malloc failed", __func__
);
495 if (sysctl(mib
, 6, *buf
, size
, NULL
, 0) < 0) {
496 syslog(LOG_ERR
, "<%s> sysctl: iflist get failed",
504 * alloc buffer and parse if_msghdrs block passed as arg,
505 * and init the buffer as list of pointers ot each of the if_msghdr.
508 parse_iflist(struct if_msghdr
***ifmlist_p
, char *buf
, size_t bufsize
)
510 int iflentry_size
, malloc_size
;
511 struct if_msghdr
*ifm
;
512 struct ifa_msghdr
*ifam
;
516 * Estimate least size of an iflist entry, to be obtained from kernel.
517 * Should add sizeof(sockaddr) ??
519 iflentry_size
= sizeof(struct if_msghdr
);
520 /* roughly estimate max list size of pointers to each if_msghdr */
521 malloc_size
= (bufsize
/iflentry_size
) * sizeof(size_t);
522 if ((*ifmlist_p
= (struct if_msghdr
**)malloc(malloc_size
)) == NULL
) {
523 syslog(LOG_ERR
, "<%s> malloc failed", __func__
);
528 for (ifm
= (struct if_msghdr
*)buf
; ifm
< (struct if_msghdr
*)lim
;) {
529 if (ifm
->ifm_msglen
== 0) {
530 syslog(LOG_WARNING
, "<%s> ifm_msglen is 0 "
531 "(buf=%p lim=%p ifm=%p)", __func__
,
536 if (ifm
->ifm_type
== RTM_IFINFO
) {
537 (*ifmlist_p
)[ifm
->ifm_index
] = ifm
;
539 syslog(LOG_ERR
, "out of sync parsing NET_RT_IFLIST\n"
540 "expected %d, got %d\n msglen = %d\n"
541 "buf:%p, ifm:%p, lim:%p\n",
542 RTM_IFINFO
, ifm
->ifm_type
, ifm
->ifm_msglen
,
546 for (ifam
= (struct ifa_msghdr
*)
547 ((char *)ifm
+ ifm
->ifm_msglen
);
548 ifam
< (struct ifa_msghdr
*)lim
;
549 ifam
= (struct ifa_msghdr
*)
550 ((char *)ifam
+ ifam
->ifam_msglen
)) {
551 /* just for safety */
552 if (!ifam
->ifam_msglen
) {
553 syslog(LOG_WARNING
, "<%s> ifa_msglen is 0 "
554 "(buf=%p lim=%p ifam=%p)", __func__
,
558 if (ifam
->ifam_type
!= RTM_NEWADDR
)
561 ifm
= (struct if_msghdr
*)ifam
;
574 /* get iflist block from kernel */
575 get_iflist(&ifblock
, &ifblock_size
);
577 /* make list of pointers to each if_msghdr */
578 parse_iflist(&iflist
, ifblock
, ifblock_size
);