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 9 * are met: 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. 18 * 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 29 * SUCH DAMAGE. 30 * 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 $ 33 */ 34 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> 40 41 #include <net/if.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> 49 #ifdef __DragonFly__ 50 # include <net/ethernet.h> 51 #endif 52 #ifdef __NetBSD__ 53 #include <net/if_ether.h> 54 #endif 55 #if defined(__bsdi__) || defined(__OpenBSD__) 56 # include <netinet/in.h> 57 # include <netinet/if_ether.h> 58 #endif 59 #include <netinet/in.h> 60 #include <netinet/icmp6.h> 61 62 #include <netinet6/in6_var.h> 63 64 #include <stdio.h> 65 #include <unistd.h> 66 #include <stdlib.h> 67 #include <syslog.h> 68 #include <string.h> 69 #include <fcntl.h> 70 #include <errno.h> 71 #include <limits.h> 72 #ifdef HAVE_GETIFADDRS 73 #include <ifaddrs.h> 74 #endif 75 76 #include "rtsold.h" 77 78 extern int rssock; 79 static int ifsock; 80 81 static int get_llflag(const char *name); 82 #ifndef HAVE_GETIFADDRS 83 static unsigned int if_maxindex(void); 84 #endif 85 static void get_rtaddrs(int addrs, struct sockaddr *sa, 86 struct sockaddr **rti_info); 87 88 int 89 ifinit(void) 90 { 91 ifsock = rssock; 92 93 return(0); 94 } 95 96 int 97 interface_up(char *name) 98 { 99 struct ifreq ifr; 100 int llflag; 101 102 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); 103 104 if (ioctl(ifsock, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) { 105 warnmsg(LOG_WARNING, __func__, "ioctl(SIOCGIFFLAGS): %s", 106 strerror(errno)); 107 return(-1); 108 } 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)); 114 } 115 return(-1); 116 } 117 118 warnmsg(LOG_DEBUG, __func__, "checking if %s is ready...", name); 119 120 llflag = get_llflag(name); 121 if (llflag < 0) { 122 warnmsg(LOG_WARNING, __func__, 123 "get_llflag() failed, anyway I'll try"); 124 return 0; 125 } 126 127 if (!(llflag & IN6_IFF_NOTREADY)) { 128 warnmsg(LOG_DEBUG, __func__, 129 "%s is ready", name); 130 return(0); 131 } else { 132 if (llflag & IN6_IFF_TENTATIVE) { 133 warnmsg(LOG_DEBUG, __func__, "%s is tentative", 134 name); 135 return IFS_TENTATIVE; 136 } 137 if (llflag & IN6_IFF_DUPLICATED) 138 warnmsg(LOG_DEBUG, __func__, "%s is duplicated", 139 name); 140 return -1; 141 } 142 } 143 144 int 145 interface_status(struct ifinfo *ifinfo) 146 { 147 char *ifname = ifinfo->ifname; 148 struct ifreq ifr; 149 struct ifmediareq ifmr; 150 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)); 157 return(-1); 158 } 159 /* 160 * if one of UP and RUNNING flags is dropped, 161 * the interface is not active. 162 */ 163 if ((ifr.ifr_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) { 164 goto inactive; 165 } 166 167 /* Next, check carrier on the interface, if possible */ 168 if (!ifinfo->mediareqok) 169 goto active; 170 memset(&ifmr, 0, sizeof(ifmr)); 171 strncpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name)); 172 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)); 178 return(-1); 179 } 180 /* 181 * EINVAL simply means that the interface does not support 182 * the SIOCGIFMEDIA ioctl. We regard it alive. 183 */ 184 ifinfo->mediareqok = 0; 185 goto active; 186 } 187 188 if (ifmr.ifm_status & IFM_AVALID) { 189 switch(ifmr.ifm_active & IFM_NMASK) { 190 case IFM_ETHER: 191 if (ifmr.ifm_status & IFM_ACTIVE) 192 goto active; 193 else 194 goto inactive; 195 break; 196 default: 197 goto inactive; 198 } 199 } 200 201 inactive: 202 return(0); 203 204 active: 205 return(1); 206 } 207 208 #define ROUNDUP(a, size) \ 209 (((a) & ((size)-1)) ? (1 + ((a) | ((size)-1))) : (a)) 210 211 #define NEXT_SA(ap) (ap) = (struct sockaddr *) \ 212 ((caddr_t)(ap) + ((ap)->sa_len ? ROUNDUP((ap)->sa_len,\ 213 sizeof(u_long)) :\ 214 sizeof(u_long))) 215 #define ROUNDUP8(a) (1 + (((a) - 1) | 7)) 216 217 int 218 lladdropt_length(struct sockaddr_dl *sdl) 219 { 220 switch(sdl->sdl_type) { 221 case IFT_ETHER: 222 #ifdef IFT_IEEE80211 223 case IFT_IEEE80211: 224 #endif 225 return(ROUNDUP8(ETHER_ADDR_LEN + 2)); 226 default: 227 return(0); 228 } 229 } 230 231 void 232 lladdropt_fill(struct sockaddr_dl *sdl, struct nd_opt_hdr *ndopt) 233 { 234 char *addr; 235 236 ndopt->nd_opt_type = ND_OPT_SOURCE_LINKADDR; /* fixed */ 237 238 switch(sdl->sdl_type) { 239 case IFT_ETHER: 240 #ifdef IFT_IEEE80211 241 case IFT_IEEE80211: 242 #endif 243 ndopt->nd_opt_len = (ROUNDUP8(ETHER_ADDR_LEN + 2)) >> 3; 244 addr = (char *)(ndopt + 1); 245 memcpy(addr, LLADDR(sdl), ETHER_ADDR_LEN); 246 break; 247 default: 248 warnmsg(LOG_ERR, __func__, 249 "unsupported link type(%d)", sdl->sdl_type); 250 exit(1); 251 } 252 253 return; 254 } 255 256 struct sockaddr_dl * 257 if_nametosdl(char *name) 258 { 259 int mib[6] = {CTL_NET, AF_ROUTE, 0, 0, NET_RT_IFLIST, 0}; 260 char *buf, *next, *lim; 261 size_t len; 262 struct if_msghdr *ifm; 263 struct sockaddr *sa, *rti_info[RTAX_MAX]; 264 struct sockaddr_dl *sdl = NULL, *ret_sdl; 265 266 if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) 267 return(NULL); 268 if ((buf = malloc(len)) == NULL) 269 return(NULL); 270 if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) { 271 free(buf); 272 return(NULL); 273 } 274 275 lim = buf + len; 276 for (next = buf; next < lim; next += ifm->ifm_msglen) { 277 ifm = (struct if_msghdr *)next; 278 if (ifm->ifm_type == RTM_IFINFO) { 279 sa = (struct sockaddr *)(ifm + 1); 280 get_rtaddrs(ifm->ifm_addrs, sa, rti_info); 281 if ((sa = rti_info[RTAX_IFP]) != NULL) { 282 if (sa->sa_family == AF_LINK) { 283 sdl = (struct sockaddr_dl *)sa; 284 if (strlen(name) != sdl->sdl_nlen) 285 continue; /* not same len */ 286 if (strncmp(&sdl->sdl_data[0], 287 name, 288 sdl->sdl_nlen) == 0) { 289 break; 290 } 291 } 292 } 293 } 294 } 295 if (next == lim) { 296 /* search failed */ 297 free(buf); 298 return(NULL); 299 } 300 301 if ((ret_sdl = malloc(sdl->sdl_len)) == NULL) 302 return(NULL); 303 memcpy((caddr_t)ret_sdl, (caddr_t)sdl, sdl->sdl_len); 304 305 free(buf); 306 return(ret_sdl); 307 } 308 309 int 310 getinet6sysctl(int code) 311 { 312 int mib[] = { CTL_NET, PF_INET6, IPPROTO_IPV6, 0 }; 313 int value; 314 size_t size; 315 316 mib[3] = code; 317 size = sizeof(value); 318 if (sysctl(mib, sizeof(mib)/sizeof(mib[0]), &value, &size, NULL, 0) < 0) 319 return -1; 320 else 321 return value; 322 } 323 324 /*------------------------------------------------------------*/ 325 326 /* get ia6_flags for link-local addr on if. returns -1 on error. */ 327 static int 328 get_llflag(const char *name) 329 { 330 #ifdef HAVE_GETIFADDRS 331 struct ifaddrs *ifap, *ifa; 332 struct in6_ifreq ifr6; 333 struct sockaddr_in6 *sin6; 334 int s; 335 336 if ((s = socket(PF_INET6, SOCK_DGRAM, 0)) < 0) { 337 warnmsg(LOG_ERR, __func__, "socket(SOCK_DGRAM): %s", 338 strerror(errno)); 339 exit(1); 340 } 341 if (getifaddrs(&ifap) != 0) { 342 warnmsg(LOG_ERR, __func__, "etifaddrs: %s", 343 strerror(errno)); 344 exit(1); 345 } 346 347 for (ifa = ifap; ifa; ifa = ifa->ifa_next) { 348 if (strlen(ifa->ifa_name) != strlen(name) 349 || strncmp(ifa->ifa_name, name, strlen(name)) != 0) 350 continue; 351 if (ifa->ifa_addr->sa_family != AF_INET6) 352 continue; 353 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr; 354 if (!IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) 355 continue; 356 357 memset(&ifr6, 0, sizeof(ifr6)); 358 strcpy(ifr6.ifr_name, name); 359 memcpy(&ifr6.ifr_ifru.ifru_addr, sin6, sin6->sin6_len); 360 if (ioctl(s, SIOCGIFAFLAG_IN6, &ifr6) < 0) { 361 warnmsg(LOG_ERR, __func__, 362 "ioctl(SIOCGIFAFLAG_IN6): %s", strerror(errno)); 363 exit(1); 364 } 365 366 freeifaddrs(ifap); 367 close(s); 368 return ifr6.ifr_ifru.ifru_flags6; 369 } 370 371 freeifaddrs(ifap); 372 close(s); 373 return -1; 374 #else 375 int s; 376 unsigned int maxif; 377 struct ifreq *iflist; 378 struct ifconf ifconf; 379 struct ifreq *ifr, *ifr_end; 380 struct sockaddr_in6 *sin6; 381 struct in6_ifreq ifr6; 382 383 maxif = if_maxindex() + 1; 384 iflist = (struct ifreq *)malloc(maxif * BUFSIZ); /* XXX */ 385 if (iflist == NULL) { 386 warnmsg(LOG_ERR, __func__, "not enough core"); 387 exit(1); 388 } 389 390 if ((s = socket(PF_INET6, SOCK_DGRAM, 0)) < 0) { 391 warnmsg(LOG_ERR, __func__, "socket(SOCK_DGRAM): %s", 392 strerror(errno)); 393 exit(1); 394 } 395 memset(&ifconf, 0, sizeof(ifconf)); 396 ifconf.ifc_req = iflist; 397 ifconf.ifc_len = maxif * BUFSIZ; /* XXX */ 398 if (ioctl(s, SIOCGIFCONF, &ifconf) < 0) { 399 warnmsg(LOG_ERR, __func__, "ioctl(SIOCGIFCONF): %s", 400 strerror(errno)); 401 exit(1); 402 } 403 404 /* Look for this interface in the list */ 405 ifr_end = (struct ifreq *) (ifconf.ifc_buf + ifconf.ifc_len); 406 for (ifr = ifconf.ifc_req; 407 ifr < ifr_end; 408 ifr = (struct ifreq *) ((char *) &ifr->ifr_addr 409 + ifr->ifr_addr.sa_len)) { 410 if (strlen(ifr->ifr_name) != strlen(name) 411 || strncmp(ifr->ifr_name, name, strlen(name)) != 0) 412 continue; 413 if (ifr->ifr_addr.sa_family != AF_INET6) 414 continue; 415 sin6 = (struct sockaddr_in6 *)&ifr->ifr_addr; 416 if (!IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) 417 continue; 418 419 memset(&ifr6, 0, sizeof(ifr6)); 420 strcpy(ifr6.ifr_name, name); 421 memcpy(&ifr6.ifr_ifru.ifru_addr, sin6, sin6->sin6_len); 422 if (ioctl(s, SIOCGIFAFLAG_IN6, &ifr6) < 0) { 423 warnmsg(LOG_ERR, __func__, 424 "ioctl(SIOCGIFAFLAG_IN6): %s", strerror(errno)); 425 exit(1); 426 } 427 428 free(iflist); 429 close(s); 430 return ifr6.ifr_ifru.ifru_flags6; 431 } 432 433 free(iflist); 434 close(s); 435 return -1; 436 #endif 437 } 438 439 #ifndef HAVE_GETIFADDRS 440 static unsigned int 441 if_maxindex(void) 442 { 443 struct if_nameindex *p, *p0; 444 unsigned int max = 0; 445 446 p0 = if_nameindex(); 447 for (p = p0; p && p->if_index && p->if_name; p++) { 448 if (max < p->if_index) 449 max = p->if_index; 450 } 451 if_freenameindex(p0); 452 return max; 453 } 454 #endif 455 456 static void 457 get_rtaddrs(int addrs, struct sockaddr *sa, struct sockaddr **rti_info) 458 { 459 int i; 460 461 for (i = 0; i < RTAX_MAX; i++) { 462 if (addrs & (1 << i)) { 463 rti_info[i] = sa; 464 NEXT_SA(sa); 465 } 466 else 467 rti_info[i] = NULL; 468 } 469 } 470