if.c (78064) | if.c (118660) |
---|---|
1/* $KAME: if.c,v 1.15 2001/05/22 06:04:17 jinmei Exp $ */ 2 3/* 4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions --- 14 unchanged lines hidden (view full) --- 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 29 * SUCH DAMAGE. 30 * | 1/* $KAME: if.c,v 1.15 2001/05/22 06:04:17 jinmei Exp $ */ 2 3/* 4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions --- 14 unchanged lines hidden (view full) --- 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 29 * SUCH DAMAGE. 30 * |
31 * $FreeBSD: head/usr.sbin/rtsold/if.c 78064 2001-06-11 12:39:29Z ume $ | 31 * $FreeBSD: head/usr.sbin/rtsold/if.c 118660 2003-08-08 16:38:23Z ume $ |
32 */ 33 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> 39 --- 56 unchanged lines hidden (view full) --- 96interface_up(char *name) 97{ 98 struct ifreq ifr; 99 int llflag; 100 101 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); 102 103 if (ioctl(ifsock, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) { | 32 */ 33 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> 39 --- 56 unchanged lines hidden (view full) --- 96interface_up(char *name) 97{ 98 struct ifreq ifr; 99 int llflag; 100 101 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); 102 103 if (ioctl(ifsock, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) { |
104 warnmsg(LOG_WARNING, __FUNCTION__, "ioctl(SIOCGIFFLAGS): %s", | 104 warnmsg(LOG_WARNING, __func__, "ioctl(SIOCGIFFLAGS): %s", |
105 strerror(errno)); 106 return(-1); 107 } 108 if (!(ifr.ifr_flags & IFF_UP)) { 109 ifr.ifr_flags |= IFF_UP; 110 if (ioctl(ifsock, SIOCSIFFLAGS, (caddr_t)&ifr) < 0) { | 105 strerror(errno)); 106 return(-1); 107 } 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__, | 111 warnmsg(LOG_ERR, __func__, |
112 "ioctl(SIOCSIFFLAGS): %s", strerror(errno)); 113 } 114 return(-1); 115 } 116 | 112 "ioctl(SIOCSIFFLAGS): %s", strerror(errno)); 113 } 114 return(-1); 115 } 116 |
117 warnmsg(LOG_DEBUG, __FUNCTION__, "checking if %s is ready...", name); | 117 warnmsg(LOG_DEBUG, __func__, "checking if %s is ready...", name); |
118 119 llflag = get_llflag(name); 120 if (llflag < 0) { | 118 119 llflag = get_llflag(name); 120 if (llflag < 0) { |
121 warnmsg(LOG_WARNING, __FUNCTION__, | 121 warnmsg(LOG_WARNING, __func__, |
122 "get_llflag() failed, anyway I'll try"); 123 return 0; 124 } 125 126 if (!(llflag & IN6_IFF_NOTREADY)) { | 122 "get_llflag() failed, anyway I'll try"); 123 return 0; 124 } 125 126 if (!(llflag & IN6_IFF_NOTREADY)) { |
127 warnmsg(LOG_DEBUG, __FUNCTION__, | 127 warnmsg(LOG_DEBUG, __func__, |
128 "%s is ready", name); 129 return(0); 130 } else { 131 if (llflag & IN6_IFF_TENTATIVE) { | 128 "%s is ready", name); 129 return(0); 130 } else { 131 if (llflag & IN6_IFF_TENTATIVE) { |
132 warnmsg(LOG_DEBUG, __FUNCTION__, "%s is tentative", | 132 warnmsg(LOG_DEBUG, __func__, "%s is tentative", |
133 name); 134 return IFS_TENTATIVE; 135 } 136 if (llflag & IN6_IFF_DUPLICATED) | 133 name); 134 return IFS_TENTATIVE; 135 } 136 if (llflag & IN6_IFF_DUPLICATED) |
137 warnmsg(LOG_DEBUG, __FUNCTION__, "%s is duplicated", | 137 warnmsg(LOG_DEBUG, __func__, "%s is duplicated", |
138 name); 139 return -1; 140 } 141} 142 143int 144interface_status(struct ifinfo *ifinfo) 145{ 146 char *ifname = ifinfo->ifname; 147 struct ifreq ifr; 148 struct ifmediareq ifmr; 149 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) { | 138 name); 139 return -1; 140 } 141} 142 143int 144interface_status(struct ifinfo *ifinfo) 145{ 146 char *ifname = ifinfo->ifname; 147 struct ifreq ifr; 148 struct ifmediareq ifmr; 149 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", | 154 warnmsg(LOG_ERR, __func__, "ioctl(SIOCGIFFLAGS) on %s: %s", |
155 ifname, strerror(errno)); 156 return(-1); 157 } 158 /* 159 * if one of UP and RUNNING flags is dropped, 160 * the interface is not active. 161 */ 162 if ((ifr.ifr_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) { 163 goto inactive; 164 } 165 166 /* Next, check carrier on the interface, if possible */ 167 if (!ifinfo->mediareqok) 168 goto active; 169 memset(&ifmr, 0, sizeof(ifmr)); 170 strncpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name)); 171 172 if (ioctl(ifsock, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) { 173 if (errno != EINVAL) { | 155 ifname, strerror(errno)); 156 return(-1); 157 } 158 /* 159 * if one of UP and RUNNING flags is dropped, 160 * the interface is not active. 161 */ 162 if ((ifr.ifr_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) { 163 goto inactive; 164 } 165 166 /* Next, check carrier on the interface, if possible */ 167 if (!ifinfo->mediareqok) 168 goto active; 169 memset(&ifmr, 0, sizeof(ifmr)); 170 strncpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name)); 171 172 if (ioctl(ifsock, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) { 173 if (errno != EINVAL) { |
174 warnmsg(LOG_DEBUG, __FUNCTION__, | 174 warnmsg(LOG_DEBUG, __func__, |
175 "ioctl(SIOCGIFMEDIA) on %s: %s", 176 ifname, strerror(errno)); 177 return(-1); 178 } 179 /* 180 * EINVAL simply means that the interface does not support 181 * the SIOCGIFMEDIA ioctl. We regard it alive. 182 */ --- 56 unchanged lines hidden (view full) --- 239#ifdef IFT_IEEE80211 240 case IFT_IEEE80211: 241#endif 242 ndopt->nd_opt_len = (ROUNDUP8(ETHER_ADDR_LEN + 2)) >> 3; 243 addr = (char *)(ndopt + 1); 244 memcpy(addr, LLADDR(sdl), ETHER_ADDR_LEN); 245 break; 246 default: | 175 "ioctl(SIOCGIFMEDIA) on %s: %s", 176 ifname, strerror(errno)); 177 return(-1); 178 } 179 /* 180 * EINVAL simply means that the interface does not support 181 * the SIOCGIFMEDIA ioctl. We regard it alive. 182 */ --- 56 unchanged lines hidden (view full) --- 239#ifdef IFT_IEEE80211 240 case IFT_IEEE80211: 241#endif 242 ndopt->nd_opt_len = (ROUNDUP8(ETHER_ADDR_LEN + 2)) >> 3; 243 addr = (char *)(ndopt + 1); 244 memcpy(addr, LLADDR(sdl), ETHER_ADDR_LEN); 245 break; 246 default: |
247 warnmsg(LOG_ERR, __FUNCTION__, | 247 warnmsg(LOG_ERR, __func__, |
248 "unsupported link type(%d)", sdl->sdl_type); 249 exit(1); 250 } 251 252 return; 253} 254 255struct sockaddr_dl * --- 72 unchanged lines hidden (view full) --- 328{ 329#ifdef HAVE_GETIFADDRS 330 struct ifaddrs *ifap, *ifa; 331 struct in6_ifreq ifr6; 332 struct sockaddr_in6 *sin6; 333 int s; 334 335 if ((s = socket(PF_INET6, SOCK_DGRAM, 0)) < 0) { | 248 "unsupported link type(%d)", sdl->sdl_type); 249 exit(1); 250 } 251 252 return; 253} 254 255struct sockaddr_dl * --- 72 unchanged lines hidden (view full) --- 328{ 329#ifdef HAVE_GETIFADDRS 330 struct ifaddrs *ifap, *ifa; 331 struct in6_ifreq ifr6; 332 struct sockaddr_in6 *sin6; 333 int s; 334 335 if ((s = socket(PF_INET6, SOCK_DGRAM, 0)) < 0) { |
336 warnmsg(LOG_ERR, __FUNCTION__, "socket(SOCK_DGRAM): %s", | 336 warnmsg(LOG_ERR, __func__, "socket(SOCK_DGRAM): %s", |
337 strerror(errno)); 338 exit(1); 339 } 340 if (getifaddrs(&ifap) != 0) { | 337 strerror(errno)); 338 exit(1); 339 } 340 if (getifaddrs(&ifap) != 0) { |
341 warnmsg(LOG_ERR, __FUNCTION__, "etifaddrs: %s", | 341 warnmsg(LOG_ERR, __func__, "etifaddrs: %s", |
342 strerror(errno)); 343 exit(1); 344 } 345 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) 349 continue; 350 if (ifa->ifa_addr->sa_family != AF_INET6) 351 continue; 352 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr; 353 if (!IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) 354 continue; 355 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) { | 342 strerror(errno)); 343 exit(1); 344 } 345 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) 349 continue; 350 if (ifa->ifa_addr->sa_family != AF_INET6) 351 continue; 352 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr; 353 if (!IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) 354 continue; 355 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__, | 360 warnmsg(LOG_ERR, __func__, |
361 "ioctl(SIOCGIFAFLAG_IN6): %s", strerror(errno)); 362 exit(1); 363 } 364 365 freeifaddrs(ifap); 366 close(s); 367 return ifr6.ifr_ifru.ifru_flags6; 368 } --- 8 unchanged lines hidden (view full) --- 377 struct ifconf ifconf; 378 struct ifreq *ifr, *ifr_end; 379 struct sockaddr_in6 *sin6; 380 struct in6_ifreq ifr6; 381 382 maxif = if_maxindex() + 1; 383 iflist = (struct ifreq *)malloc(maxif * BUFSIZ); /* XXX */ 384 if (iflist == NULL) { | 361 "ioctl(SIOCGIFAFLAG_IN6): %s", strerror(errno)); 362 exit(1); 363 } 364 365 freeifaddrs(ifap); 366 close(s); 367 return ifr6.ifr_ifru.ifru_flags6; 368 } --- 8 unchanged lines hidden (view full) --- 377 struct ifconf ifconf; 378 struct ifreq *ifr, *ifr_end; 379 struct sockaddr_in6 *sin6; 380 struct in6_ifreq ifr6; 381 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"); | 385 warnmsg(LOG_ERR, __func__, "not enough core"); |
386 exit(1); 387 } 388 389 if ((s = socket(PF_INET6, SOCK_DGRAM, 0)) < 0) { | 386 exit(1); 387 } 388 389 if ((s = socket(PF_INET6, SOCK_DGRAM, 0)) < 0) { |
390 warnmsg(LOG_ERR, __FUNCTION__, "socket(SOCK_DGRAM): %s", | 390 warnmsg(LOG_ERR, __func__, "socket(SOCK_DGRAM): %s", |
391 strerror(errno)); 392 exit(1); 393 } 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) { | 391 strerror(errno)); 392 exit(1); 393 } 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", | 398 warnmsg(LOG_ERR, __func__, "ioctl(SIOCGIFCONF): %s", |
399 strerror(errno)); 400 exit(1); 401 } 402 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; 406 ifr < ifr_end; --- 7 unchanged lines hidden (view full) --- 414 sin6 = (struct sockaddr_in6 *)&ifr->ifr_addr; 415 if (!IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) 416 continue; 417 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) { | 399 strerror(errno)); 400 exit(1); 401 } 402 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; 406 ifr < ifr_end; --- 7 unchanged lines hidden (view full) --- 414 sin6 = (struct sockaddr_in6 *)&ifr->ifr_addr; 415 if (!IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) 416 continue; 417 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__, | 422 warnmsg(LOG_ERR, __func__, |
423 "ioctl(SIOCGIFAFLAG_IN6): %s", strerror(errno)); 424 exit(1); 425 } 426 427 free(iflist); 428 close(s); 429 return ifr6.ifr_ifru.ifru_flags6; 430 } --- 38 unchanged lines hidden --- | 423 "ioctl(SIOCGIFAFLAG_IN6): %s", strerror(errno)); 424 exit(1); 425 } 426 427 free(iflist); 428 close(s); 429 return ifr6.ifr_ifru.ifru_flags6; 430 } --- 38 unchanged lines hidden --- |