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
34 #include <sys/param.h>
35 #include <sys/socket.h>
36 #include <sys/sysctl.h>
37 #include <sys/ioctl.h>
38 #include <sys/queue.h>
41 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
42 #include <net/if_var.h>
43 #endif /* __FreeBSD__ >= 3 */
44 #include <net/if_types.h>
45 #include <net/route.h>
46 #include <net/if_dl.h>
47 #include <net/if_media.h>
49 # include <net/ethernet.h>
52 #include <net/if_ether.h>
54 #if defined(__bsdi__) || defined(__OpenBSD__)
55 # include <netinet/in.h>
56 # include <netinet/if_ether.h>
58 #include <netinet/in.h>
59 #include <netinet/icmp6.h>
61 #include <netinet6/in6_var.h>
71 #ifdef HAVE_GETIFADDRS
80 static int get_llflag __P((const char *name));
81 #ifndef HAVE_GETIFADDRS
82 static unsigned int if_maxindex __P((void));
84 static void get_rtaddrs __P((int addrs, struct sockaddr *sa,
85 struct sockaddr **rti_info));
96 interface_up(char *name)
101 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
103 if (ioctl(ifsock, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) {
104 warnmsg(LOG_WARNING, __FUNCTION__, "ioctl(SIOCGIFFLAGS): %s",
108 if (!(ifr.ifr_flags & IFF_UP)) {
109 ifr.ifr_flags |= IFF_UP;
110 if (ioctl(ifsock, SIOCSIFFLAGS, (caddr_t)&ifr) < 0) {
111 warnmsg(LOG_ERR, __FUNCTION__,
112 "ioctl(SIOCSIFFLAGS): %s", strerror(errno));
117 warnmsg(LOG_DEBUG, __FUNCTION__, "checking if %s is ready...", name);
119 llflag = get_llflag(name);
121 warnmsg(LOG_WARNING, __FUNCTION__,
122 "get_llflag() failed, anyway I'll try");
126 if (!(llflag & IN6_IFF_NOTREADY)) {
127 warnmsg(LOG_DEBUG, __FUNCTION__,
128 "%s is ready", name);
131 if (llflag & IN6_IFF_TENTATIVE) {
132 warnmsg(LOG_DEBUG, __FUNCTION__, "%s is tentative",
134 return IFS_TENTATIVE;
136 if (llflag & IN6_IFF_DUPLICATED)
137 warnmsg(LOG_DEBUG, __FUNCTION__, "%s is duplicated",
144 interface_status(struct ifinfo *ifinfo)
146 char *ifname = ifinfo->ifname;
148 struct ifmediareq ifmr;
150 /* get interface flags */
151 memset(&ifr, 0, sizeof(ifr));
152 strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
153 if (ioctl(ifsock, SIOCGIFFLAGS, &ifr) < 0) {
154 warnmsg(LOG_ERR, __FUNCTION__, "ioctl(SIOCGIFFLAGS) on %s: %s",
155 ifname, strerror(errno));
159 * if one of UP and RUNNING flags is dropped,
160 * the interface is not active.
162 if ((ifr.ifr_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
166 /* Next, check carrier on the interface, if possible */
167 if (!ifinfo->mediareqok)
169 memset(&ifmr, 0, sizeof(ifmr));
170 strncpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name));
172 if (ioctl(ifsock, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
173 if (errno != EINVAL) {
174 warnmsg(LOG_DEBUG, __FUNCTION__,
175 "ioctl(SIOCGIFMEDIA) on %s: %s",
176 ifname, strerror(errno));
180 * EINVAL simply means that the interface does not support
181 * the SIOCGIFMEDIA ioctl. We regard it alive.
183 ifinfo->mediareqok = 0;
187 if (ifmr.ifm_status & IFM_AVALID) {
188 switch(ifmr.ifm_active & IFM_NMASK) {
190 if (ifmr.ifm_status & IFM_ACTIVE)
207 #define ROUNDUP(a, size) \
208 (((a) & ((size)-1)) ? (1 + ((a) | ((size)-1))) : (a))
210 #define NEXT_SA(ap) (ap) = (struct sockaddr *) \
211 ((caddr_t)(ap) + ((ap)->sa_len ? ROUNDUP((ap)->sa_len,\
214 #define ROUNDUP8(a) (1 + (((a) - 1) | 7))
217 lladdropt_length(struct sockaddr_dl *sdl)
219 switch(sdl->sdl_type) {
224 return(ROUNDUP8(ETHER_ADDR_LEN + 2));
231 lladdropt_fill(struct sockaddr_dl *sdl, struct nd_opt_hdr *ndopt)
235 ndopt->nd_opt_type = ND_OPT_SOURCE_LINKADDR; /* fixed */
237 switch(sdl->sdl_type) {
242 ndopt->nd_opt_len = (ROUNDUP8(ETHER_ADDR_LEN + 2)) >> 3;
243 addr = (char *)(ndopt + 1);
244 memcpy(addr, LLADDR(sdl), ETHER_ADDR_LEN);
247 warnmsg(LOG_ERR, __FUNCTION__,
248 "unsupported link type(%d)", sdl->sdl_type);
256 if_nametosdl(char *name)
258 int mib[6] = {CTL_NET, AF_ROUTE, 0, 0, NET_RT_IFLIST, 0};
259 char *buf, *next, *lim;
261 struct if_msghdr *ifm;
262 struct sockaddr *sa, *rti_info[RTAX_MAX];
263 struct sockaddr_dl *sdl = NULL, *ret_sdl;
265 if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0)
267 if ((buf = malloc(len)) == NULL)
269 if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
275 for (next = buf; next < lim; next += ifm->ifm_msglen) {
276 ifm = (struct if_msghdr *)next;
277 if (ifm->ifm_type == RTM_IFINFO) {
278 sa = (struct sockaddr *)(ifm + 1);
279 get_rtaddrs(ifm->ifm_addrs, sa, rti_info);
280 if ((sa = rti_info[RTAX_IFP]) != NULL) {
281 if (sa->sa_family == AF_LINK) {
282 sdl = (struct sockaddr_dl *)sa;
283 if (strlen(name) != sdl->sdl_nlen)
284 continue; /* not same len */
285 if (strncmp(&sdl->sdl_data[0],
287 sdl->sdl_nlen) == 0) {
300 if ((ret_sdl = malloc(sdl->sdl_len)) == NULL)
302 memcpy((caddr_t)ret_sdl, (caddr_t)sdl, sdl->sdl_len);
309 getinet6sysctl(int code)
311 int mib[] = { CTL_NET, PF_INET6, IPPROTO_IPV6, 0 };
316 size = sizeof(value);
317 if (sysctl(mib, sizeof(mib)/sizeof(mib[0]), &value, &size, NULL, 0) < 0)
323 /*------------------------------------------------------------*/
325 /* get ia6_flags for link-local addr on if. returns -1 on error. */
327 get_llflag(const char *name)
329 #ifdef HAVE_GETIFADDRS
330 struct ifaddrs *ifap, *ifa;
331 struct in6_ifreq ifr6;
332 struct sockaddr_in6 *sin6;
335 if ((s = socket(PF_INET6, SOCK_DGRAM, 0)) < 0) {
336 warnmsg(LOG_ERR, __FUNCTION__, "socket(SOCK_DGRAM): %s",
340 if (getifaddrs(&ifap) != 0) {
341 warnmsg(LOG_ERR, __FUNCTION__, "etifaddrs: %s",
346 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
347 if (strlen(ifa->ifa_name) != strlen(name)
348 || strncmp(ifa->ifa_name, name, strlen(name)) != 0)
350 if (ifa->ifa_addr->sa_family != AF_INET6)
352 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
353 if (!IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
356 memset(&ifr6, 0, sizeof(ifr6));
357 strcpy(ifr6.ifr_name, name);
358 memcpy(&ifr6.ifr_ifru.ifru_addr, sin6, sin6->sin6_len);
359 if (ioctl(s, SIOCGIFAFLAG_IN6, &ifr6) < 0) {
360 warnmsg(LOG_ERR, __FUNCTION__,
361 "ioctl(SIOCGIFAFLAG_IN6): %s", strerror(errno));
367 return ifr6.ifr_ifru.ifru_flags6;
376 struct ifreq *iflist;
377 struct ifconf ifconf;
378 struct ifreq *ifr, *ifr_end;
379 struct sockaddr_in6 *sin6;
380 struct in6_ifreq ifr6;
382 maxif = if_maxindex() + 1;
383 iflist = (struct ifreq *)malloc(maxif * BUFSIZ); /* XXX */
384 if (iflist == NULL) {
385 warnmsg(LOG_ERR, __FUNCTION__, "not enough core");
389 if ((s = socket(PF_INET6, SOCK_DGRAM, 0)) < 0) {
390 warnmsg(LOG_ERR, __FUNCTION__, "socket(SOCK_DGRAM): %s",
394 memset(&ifconf, 0, sizeof(ifconf));
395 ifconf.ifc_req = iflist;
396 ifconf.ifc_len = maxif * BUFSIZ; /* XXX */
397 if (ioctl(s, SIOCGIFCONF, &ifconf) < 0) {
398 warnmsg(LOG_ERR, __FUNCTION__, "ioctl(SIOCGIFCONF): %s",
403 /* Look for this interface in the list */
404 ifr_end = (struct ifreq *) (ifconf.ifc_buf + ifconf.ifc_len);
405 for (ifr = ifconf.ifc_req;
407 ifr = (struct ifreq *) ((char *) &ifr->ifr_addr
408 + ifr->ifr_addr.sa_len)) {
409 if (strlen(ifr->ifr_name) != strlen(name)
410 || strncmp(ifr->ifr_name, name, strlen(name)) != 0)
412 if (ifr->ifr_addr.sa_family != AF_INET6)
414 sin6 = (struct sockaddr_in6 *)&ifr->ifr_addr;
415 if (!IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
418 memset(&ifr6, 0, sizeof(ifr6));
419 strcpy(ifr6.ifr_name, name);
420 memcpy(&ifr6.ifr_ifru.ifru_addr, sin6, sin6->sin6_len);
421 if (ioctl(s, SIOCGIFAFLAG_IN6, &ifr6) < 0) {
422 warnmsg(LOG_ERR, __FUNCTION__,
423 "ioctl(SIOCGIFAFLAG_IN6): %s", strerror(errno));
429 return ifr6.ifr_ifru.ifru_flags6;
438 #ifndef HAVE_GETIFADDRS
442 struct if_nameindex *p, *p0;
443 unsigned int max = 0;
446 for (p = p0; p && p->if_index && p->if_name; p++) {
447 if (max < p->if_index)
450 if_freenameindex(p0);
456 get_rtaddrs(int addrs, struct sockaddr *sa, struct sockaddr **rti_info)
460 for (i = 0; i < RTAX_MAX; i++) {
461 if (addrs & (1 << i)) {