1 /* $NetBSD: if.c,v 1.23 2015/06/05 15:41:59 roy 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/queue.h>
35 #include <sys/socket.h>
36 #include <sys/sysctl.h>
37 #include <sys/ioctl.h>
39 #include <net/if_types.h>
42 #include <net/ethernet.h>
44 #include <net/if_ether.h>
46 #include <net/route.h>
47 #include <net/if_dl.h>
48 #include <netinet/in.h>
49 #include <netinet/icmp6.h>
60 #define RT_ROUNDUP(a) \
61 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
62 #define RT_ADVANCE(x, n) (x += RT_ROUNDUP((n)->sa_len))
66 get_rtaddrs(int addrs
, struct sockaddr
*sa
, struct sockaddr
**rti_info
)
70 for (i
= 0; i
< RTAX_MAX
; i
++) {
71 if (addrs
& (1 << i
)) {
81 if_nametosdl(const char *name
)
83 struct ifaddrs
*ifap
, *ifa
;
84 struct sockaddr_dl
*sdl
;
86 if (getifaddrs(&ifap
) != 0)
89 for (ifa
= ifap
; ifa
; ifa
= ifa
->ifa_next
) {
90 if (strcmp(ifa
->ifa_name
, name
) != 0)
92 if (ifa
->ifa_addr
->sa_family
!= AF_LINK
)
95 sdl
= malloc(ifa
->ifa_addr
->sa_len
);
99 memcpy(sdl
, ifa
->ifa_addr
, ifa
->ifa_addr
->sa_len
);
109 if_getmtu(const char *name
)
114 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0)
117 memset(&ifr
, 0, sizeof(ifr
));
118 ifr
.ifr_addr
.sa_family
= AF_INET6
;
119 strncpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
120 if (ioctl(s
, SIOCGIFMTU
, &ifr
) != -1)
129 /* give interface index and its old flags, then new flags returned */
131 if_getflags(int ifindex
, int oifflags
)
136 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0) {
137 syslog(LOG_ERR
, "<%s> socket: %m", __func__
);
138 return (oifflags
& ~IFF_UP
);
141 memset(&ifr
, 0, sizeof(ifr
));
142 if_indextoname(ifindex
, ifr
.ifr_name
);
143 if (ioctl(s
, SIOCGIFFLAGS
, &ifr
) < 0) {
144 syslog(LOG_ERR
, "<%s> ioctl:SIOCGIFFLAGS: failed for %s",
145 __func__
, ifr
.ifr_name
);
147 return (oifflags
& ~IFF_UP
);
150 return (ifr
.ifr_flags
);
153 #define ROUNDUP8(a) (1 + (((a) - 1) | 7))
155 lladdropt_length(struct sockaddr_dl
*sdl
)
157 switch (sdl
->sdl_type
) {
160 return(ROUNDUP8(ETHER_ADDR_LEN
+ 2));
167 lladdropt_fill(struct sockaddr_dl
*sdl
, struct nd_opt_hdr
*ndopt
)
171 ndopt
->nd_opt_type
= ND_OPT_SOURCE_LINKADDR
; /* fixed */
173 switch (sdl
->sdl_type
) {
176 ndopt
->nd_opt_len
= (ROUNDUP8(ETHER_ADDR_LEN
+ 2)) >> 3;
177 addr
= (char *)(ndopt
+ 1);
178 memcpy(addr
, LLADDR(sdl
), ETHER_ADDR_LEN
);
181 syslog(LOG_ERR
, "<%s> unsupported link type(%d)",
182 __func__
, sdl
->sdl_type
);
189 #define FILTER_MATCH(type, filter) ((0x1 << type) & filter)
190 #define SIN6(s) ((struct sockaddr_in6 *)(s))
191 #define SDL(s) ((struct sockaddr_dl *)(s))
193 get_next_msg(char *buf
, char *lim
, int ifindex
, size_t *lenp
, int filter
)
195 struct rt_msghdr
*rtm
;
196 struct ifa_msghdr
*ifam
;
197 struct sockaddr
*sa
, *dst
, *gw
, *ifa
, *rti_info
[RTAX_MAX
];
200 for (rtm
= (struct rt_msghdr
*)buf
;
201 rtm
< (struct rt_msghdr
*)lim
;
202 rtm
= (struct rt_msghdr
*)(((char *)rtm
) + rtm
->rtm_msglen
)) {
203 /* just for safety */
204 if (!rtm
->rtm_msglen
) {
205 syslog(LOG_WARNING
, "<%s> rtm_msglen is 0 "
206 "(buf=%p lim=%p rtm=%p)", __func__
,
210 if (FILTER_MATCH(rtm
->rtm_type
, filter
) == 0) {
214 switch (rtm
->rtm_type
) {
218 /* address related checks */
219 sa
= (struct sockaddr
*)(rtm
+ 1);
220 get_rtaddrs(rtm
->rtm_addrs
, sa
, rti_info
);
221 if ((dst
= rti_info
[RTAX_DST
]) == NULL
||
222 dst
->sa_family
!= AF_INET6
)
225 if (IN6_IS_ADDR_LINKLOCAL(&SIN6(dst
)->sin6_addr
) ||
226 IN6_IS_ADDR_MULTICAST(&SIN6(dst
)->sin6_addr
))
229 if ((gw
= rti_info
[RTAX_GATEWAY
]) == NULL
||
230 gw
->sa_family
!= AF_LINK
)
232 if (ifindex
&& SDL(gw
)->sdl_index
!= ifindex
)
235 if (rti_info
[RTAX_NETMASK
] == NULL
)
239 *lenp
= rtm
->rtm_msglen
;
244 ifam
= (struct ifa_msghdr
*)rtm
;
246 /* address related checks */
247 sa
= (struct sockaddr
*)(ifam
+ 1);
248 get_rtaddrs(ifam
->ifam_addrs
, sa
, rti_info
);
249 if ((ifa
= rti_info
[RTAX_IFA
]) == NULL
||
250 (ifa
->sa_family
!= AF_INET
&&
251 ifa
->sa_family
!= AF_INET6
))
254 if (ifa
->sa_family
== AF_INET6
&&
255 (IN6_IS_ADDR_LINKLOCAL(&SIN6(ifa
)->sin6_addr
) ||
256 IN6_IS_ADDR_MULTICAST(&SIN6(ifa
)->sin6_addr
)))
259 if (ifindex
&& ifam
->ifam_index
!= ifindex
)
263 *lenp
= ifam
->ifam_msglen
;
266 #ifdef RTM_IFANNOUNCE
271 *lenp
= rtm
->rtm_msglen
;
284 struct rt_msghdr
*rtm
= (struct rt_msghdr
*)buf
;
285 struct sockaddr
*sa
, *rti_info
[RTAX_MAX
];
287 sa
= (struct sockaddr
*)(rtm
+ 1);
288 get_rtaddrs(rtm
->rtm_addrs
, sa
, rti_info
);
290 return(&SIN6(rti_info
[RTAX_DST
])->sin6_addr
);
294 get_rtm_ifindex(char *buf
)
296 struct rt_msghdr
*rtm
= (struct rt_msghdr
*)buf
;
297 struct sockaddr
*sa
, *rti_info
[RTAX_MAX
];
299 sa
= (struct sockaddr
*)(rtm
+ 1);
300 get_rtaddrs(rtm
->rtm_addrs
, sa
, rti_info
);
302 return(((struct sockaddr_dl
*)rti_info
[RTAX_GATEWAY
])->sdl_index
);
306 get_ifm_ifindex(char *buf
)
308 struct if_msghdr
*ifm
= (struct if_msghdr
*)buf
;
310 return ((int)ifm
->ifm_index
);
314 get_ifam_ifindex(char *buf
)
316 struct ifa_msghdr
*ifam
= (struct ifa_msghdr
*)buf
;
318 return ((int)ifam
->ifam_index
);
322 get_ifm_flags(char *buf
)
324 struct if_msghdr
*ifm
= (struct if_msghdr
*)buf
;
326 return (ifm
->ifm_flags
);
329 #ifdef RTM_IFANNOUNCE
331 get_ifan_ifindex(char *buf
)
333 struct if_announcemsghdr
*ifan
= (struct if_announcemsghdr
*)buf
;
335 return ((int)ifan
->ifan_index
);
339 get_ifan_what(char *buf
)
341 struct if_announcemsghdr
*ifan
= (struct if_announcemsghdr
*)buf
;
343 return ((int)ifan
->ifan_what
);
348 get_prefixlen(char *buf
)
350 struct rt_msghdr
*rtm
= (struct rt_msghdr
*)buf
;
351 struct sockaddr
*sa
, *rti_info
[RTAX_MAX
];
352 unsigned char *p
, *lim
;
354 sa
= (struct sockaddr
*)(rtm
+ 1);
355 get_rtaddrs(rtm
->rtm_addrs
, sa
, rti_info
);
356 sa
= rti_info
[RTAX_NETMASK
];
358 p
= (unsigned char *)(&SIN6(sa
)->sin6_addr
);
359 lim
= (unsigned char *)sa
+ sa
->sa_len
;
360 return prefixlen(p
, lim
);
364 prefixlen(const unsigned char *p
, const unsigned char *lim
)
368 for (masklen
= 0; p
< lim
; p
++) {
405 rtmsg_type(char *buf
)
407 struct rt_msghdr
*rtm
= (struct rt_msghdr
*)buf
;
409 return(rtm
->rtm_type
);
415 struct rt_msghdr
*rtm
= (struct rt_msghdr
*)buf
;
417 return(rtm
->rtm_msglen
);