1 /* $KAME: if.c,v 1.15 2001/05/22 06:04:17 jinmei 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/rtsold/if.c,v 1.2.2.3 2001/07/03 11:02:16 ume Exp $
32 * $DragonFly: src/usr.sbin/rtsold/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>
39 #include <sys/queue.h>
42 #if defined(__DragonFly__)
43 #include <net/if_var.h>
44 #endif /* __DragonFly__ >= 3 */
45 #include <net/if_types.h>
46 #include <net/route.h>
47 #include <net/if_dl.h>
48 #include <net/if_media.h>
50 # include <net/ethernet.h>
53 #include <net/if_ether.h>
55 #if defined(__bsdi__) || defined(__OpenBSD__)
56 # include <netinet/in.h>
57 # include <netinet/if_ether.h>
59 #include <netinet/in.h>
60 #include <netinet/icmp6.h>
62 #include <netinet6/in6_var.h>
72 #ifdef HAVE_GETIFADDRS
81 static int get_llflag(const char *name
);
82 #ifndef HAVE_GETIFADDRS
83 static unsigned int if_maxindex(void);
85 static void get_rtaddrs(int addrs
, struct sockaddr
*sa
,
86 struct sockaddr
**rti_info
);
97 interface_up(char *name
)
102 strncpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
104 if (ioctl(ifsock
, SIOCGIFFLAGS
, (caddr_t
)&ifr
) < 0) {
105 warnmsg(LOG_WARNING
, __func__
, "ioctl(SIOCGIFFLAGS): %s",
109 if (!(ifr
.ifr_flags
& IFF_UP
)) {
110 ifr
.ifr_flags
|= IFF_UP
;
111 if (ioctl(ifsock
, SIOCSIFFLAGS
, (caddr_t
)&ifr
) < 0) {
112 warnmsg(LOG_ERR
, __func__
,
113 "ioctl(SIOCSIFFLAGS): %s", strerror(errno
));
118 warnmsg(LOG_DEBUG
, __func__
, "checking if %s is ready...", name
);
120 llflag
= get_llflag(name
);
122 warnmsg(LOG_WARNING
, __func__
,
123 "get_llflag() failed, anyway I'll try");
127 if (!(llflag
& IN6_IFF_NOTREADY
)) {
128 warnmsg(LOG_DEBUG
, __func__
,
129 "%s is ready", name
);
132 if (llflag
& IN6_IFF_TENTATIVE
) {
133 warnmsg(LOG_DEBUG
, __func__
, "%s is tentative",
135 return IFS_TENTATIVE
;
137 if (llflag
& IN6_IFF_DUPLICATED
)
138 warnmsg(LOG_DEBUG
, __func__
, "%s is duplicated",
145 interface_status(struct ifinfo
*ifinfo
)
147 char *ifname
= ifinfo
->ifname
;
149 struct ifmediareq ifmr
;
151 /* get interface flags */
152 memset(&ifr
, 0, sizeof(ifr
));
153 strncpy(ifr
.ifr_name
, ifname
, sizeof(ifr
.ifr_name
));
154 if (ioctl(ifsock
, SIOCGIFFLAGS
, &ifr
) < 0) {
155 warnmsg(LOG_ERR
, __func__
, "ioctl(SIOCGIFFLAGS) on %s: %s",
156 ifname
, strerror(errno
));
160 * if one of UP and RUNNING flags is dropped,
161 * the interface is not active.
163 if ((ifr
.ifr_flags
& (IFF_UP
|IFF_RUNNING
)) != (IFF_UP
|IFF_RUNNING
)) {
167 /* Next, check carrier on the interface, if possible */
168 if (!ifinfo
->mediareqok
)
170 memset(&ifmr
, 0, sizeof(ifmr
));
171 strncpy(ifmr
.ifm_name
, ifname
, sizeof(ifmr
.ifm_name
));
173 if (ioctl(ifsock
, SIOCGIFMEDIA
, (caddr_t
)&ifmr
) < 0) {
174 if (errno
!= EINVAL
) {
175 warnmsg(LOG_DEBUG
, __func__
,
176 "ioctl(SIOCGIFMEDIA) on %s: %s",
177 ifname
, strerror(errno
));
181 * EINVAL simply means that the interface does not support
182 * the SIOCGIFMEDIA ioctl. We regard it alive.
184 ifinfo
->mediareqok
= 0;
188 if (ifmr
.ifm_status
& IFM_AVALID
) {
189 switch(ifmr
.ifm_active
& IFM_NMASK
) {
191 if (ifmr
.ifm_status
& IFM_ACTIVE
)
208 #define ROUNDUP8(a) (1 + (((a) - 1) | 7))
211 lladdropt_length(struct sockaddr_dl
*sdl
)
213 switch(sdl
->sdl_type
) {
218 return(ROUNDUP8(ETHER_ADDR_LEN
+ 2));
225 lladdropt_fill(struct sockaddr_dl
*sdl
, struct nd_opt_hdr
*ndopt
)
229 ndopt
->nd_opt_type
= ND_OPT_SOURCE_LINKADDR
; /* fixed */
231 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
);
241 warnmsg(LOG_ERR
, __func__
,
242 "unsupported link type(%d)", sdl
->sdl_type
);
250 if_nametosdl(char *name
)
252 int mib
[6] = {CTL_NET
, AF_ROUTE
, 0, 0, NET_RT_IFLIST
, 0};
253 char *buf
, *next
, *lim
;
255 struct if_msghdr
*ifm
;
256 struct sockaddr
*sa
, *rti_info
[RTAX_MAX
];
257 struct sockaddr_dl
*sdl
= NULL
, *ret_sdl
;
259 if (sysctl(mib
, 6, NULL
, &len
, NULL
, 0) < 0)
261 if ((buf
= malloc(len
)) == NULL
)
263 if (sysctl(mib
, 6, buf
, &len
, NULL
, 0) < 0) {
269 for (next
= buf
; next
< lim
; next
+= ifm
->ifm_msglen
) {
270 ifm
= (struct if_msghdr
*)next
;
271 if (ifm
->ifm_type
== RTM_IFINFO
) {
272 sa
= (struct sockaddr
*)(ifm
+ 1);
273 get_rtaddrs(ifm
->ifm_addrs
, sa
, rti_info
);
274 if ((sa
= rti_info
[RTAX_IFP
]) != NULL
) {
275 if (sa
->sa_family
== AF_LINK
) {
276 sdl
= (struct sockaddr_dl
*)sa
;
277 if (strlen(name
) != sdl
->sdl_nlen
)
278 continue; /* not same len */
279 if (strncmp(&sdl
->sdl_data
[0],
281 sdl
->sdl_nlen
) == 0) {
294 if ((ret_sdl
= malloc(sdl
->sdl_len
)) == NULL
)
296 memcpy((caddr_t
)ret_sdl
, (caddr_t
)sdl
, sdl
->sdl_len
);
303 getinet6sysctl(int code
)
305 int mib
[] = { CTL_NET
, PF_INET6
, IPPROTO_IPV6
, 0 };
310 size
= sizeof(value
);
311 if (sysctl(mib
, sizeof(mib
)/sizeof(mib
[0]), &value
, &size
, NULL
, 0) < 0)
317 /*------------------------------------------------------------*/
319 /* get ia6_flags for link-local addr on if. returns -1 on error. */
321 get_llflag(const char *name
)
323 #ifdef HAVE_GETIFADDRS
324 struct ifaddrs
*ifap
, *ifa
;
325 struct in6_ifreq ifr6
;
326 struct sockaddr_in6
*sin6
;
329 if ((s
= socket(PF_INET6
, SOCK_DGRAM
, 0)) < 0) {
330 warnmsg(LOG_ERR
, __func__
, "socket(SOCK_DGRAM): %s",
334 if (getifaddrs(&ifap
) != 0) {
335 warnmsg(LOG_ERR
, __func__
, "etifaddrs: %s",
340 for (ifa
= ifap
; ifa
; ifa
= ifa
->ifa_next
) {
341 if (strlen(ifa
->ifa_name
) != strlen(name
)
342 || strncmp(ifa
->ifa_name
, name
, strlen(name
)) != 0)
344 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
346 sin6
= (struct sockaddr_in6
*)ifa
->ifa_addr
;
347 if (!IN6_IS_ADDR_LINKLOCAL(&sin6
->sin6_addr
))
350 memset(&ifr6
, 0, sizeof(ifr6
));
351 strcpy(ifr6
.ifr_name
, name
);
352 memcpy(&ifr6
.ifr_ifru
.ifru_addr
, sin6
, sin6
->sin6_len
);
353 if (ioctl(s
, SIOCGIFAFLAG_IN6
, &ifr6
) < 0) {
354 warnmsg(LOG_ERR
, __func__
,
355 "ioctl(SIOCGIFAFLAG_IN6): %s", strerror(errno
));
361 return ifr6
.ifr_ifru
.ifru_flags6
;
370 struct ifreq
*iflist
;
371 struct ifconf ifconf
;
372 struct ifreq
*ifr
, *ifr_end
;
373 struct sockaddr_in6
*sin6
;
374 struct in6_ifreq ifr6
;
376 maxif
= if_maxindex() + 1;
377 iflist
= (struct ifreq
*)malloc(maxif
* BUFSIZ
); /* XXX */
378 if (iflist
== NULL
) {
379 warnmsg(LOG_ERR
, __func__
, "not enough core");
383 if ((s
= socket(PF_INET6
, SOCK_DGRAM
, 0)) < 0) {
384 warnmsg(LOG_ERR
, __func__
, "socket(SOCK_DGRAM): %s",
388 memset(&ifconf
, 0, sizeof(ifconf
));
389 ifconf
.ifc_req
= iflist
;
390 ifconf
.ifc_len
= maxif
* BUFSIZ
; /* XXX */
391 if (ioctl(s
, SIOCGIFCONF
, &ifconf
) < 0) {
392 warnmsg(LOG_ERR
, __func__
, "ioctl(SIOCGIFCONF): %s",
397 /* Look for this interface in the list */
398 ifr_end
= (struct ifreq
*) (ifconf
.ifc_buf
+ ifconf
.ifc_len
);
399 for (ifr
= ifconf
.ifc_req
;
401 ifr
= (struct ifreq
*) ((char *) &ifr
->ifr_addr
402 + ifr
->ifr_addr
.sa_len
)) {
403 if (strlen(ifr
->ifr_name
) != strlen(name
)
404 || strncmp(ifr
->ifr_name
, name
, strlen(name
)) != 0)
406 if (ifr
->ifr_addr
.sa_family
!= AF_INET6
)
408 sin6
= (struct sockaddr_in6
*)&ifr
->ifr_addr
;
409 if (!IN6_IS_ADDR_LINKLOCAL(&sin6
->sin6_addr
))
412 memset(&ifr6
, 0, sizeof(ifr6
));
413 strcpy(ifr6
.ifr_name
, name
);
414 memcpy(&ifr6
.ifr_ifru
.ifru_addr
, sin6
, sin6
->sin6_len
);
415 if (ioctl(s
, SIOCGIFAFLAG_IN6
, &ifr6
) < 0) {
416 warnmsg(LOG_ERR
, __func__
,
417 "ioctl(SIOCGIFAFLAG_IN6): %s", strerror(errno
));
423 return ifr6
.ifr_ifru
.ifru_flags6
;
432 #ifndef HAVE_GETIFADDRS
436 struct if_nameindex
*p
, *p0
;
437 unsigned int max
= 0;
440 for (p
= p0
; p
&& p
->if_index
&& p
->if_name
; p
++) {
441 if (max
< p
->if_index
)
444 if_freenameindex(p0
);
450 get_rtaddrs(int addrs
, struct sockaddr
*sa
, struct sockaddr
**rti_info
)
454 for (i
= 0; i
< RTAX_MAX
; i
++) {
455 if (addrs
& (1 << i
)) {