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