1 /* $FreeBSD$ */ 2 /* $KAME: if.c,v 1.17 2001/01/21 15:27:30 itojun Exp $ */ 3 4 /* 5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the project nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #include <sys/param.h> 34 #include <sys/socket.h> 35 #include <sys/sysctl.h> 36 #include <sys/ioctl.h> 37 #include <net/if.h> 38 #include <net/if_types.h> 39 #ifdef __FreeBSD__ 40 # include <net/ethernet.h> 41 #endif 42 #include <ifaddrs.h> 43 #ifdef __NetBSD__ 44 #include <net/if_ether.h> 45 #endif 46 #include <net/route.h> 47 #include <net/if_dl.h> 48 #include <netinet/in.h> 49 #include <netinet/icmp6.h> 50 #ifdef __bsdi__ 51 # include <netinet/if_ether.h> 52 #endif 53 #ifdef __OpenBSD__ 54 #include <netinet/if_ether.h> 55 #endif 56 #include <unistd.h> 57 #include <errno.h> 58 #include <stdlib.h> 59 #include <string.h> 60 #include <syslog.h> 61 #include "rtadvd.h" 62 #include "if.h" 63 64 #define ROUNDUP(a, size) \ 65 (((a) & ((size)-1)) ? (1 + ((a) | ((size)-1))) : (a)) 66 67 #define NEXT_SA(ap) (ap) = (struct sockaddr *) \ 68 ((caddr_t)(ap) + ((ap)->sa_len ? ROUNDUP((ap)->sa_len,\ 69 sizeof(u_long)) :\ 70 sizeof(u_long))) 71 72 struct if_msghdr **iflist; 73 int iflist_init_ok; 74 size_t ifblock_size; 75 char *ifblock; 76 77 static void get_iflist __P((char **buf, size_t *size)); 78 static void parse_iflist __P((struct if_msghdr ***ifmlist_p, char *buf, 79 size_t bufsize)); 80 81 static void 82 get_rtaddrs(int addrs, struct sockaddr *sa, struct sockaddr **rti_info) 83 { 84 int i; 85 86 for (i = 0; i < RTAX_MAX; i++) { 87 if (addrs & (1 << i)) { 88 rti_info[i] = sa; 89 NEXT_SA(sa); 90 } 91 else 92 rti_info[i] = NULL; 93 } 94 } 95 96 struct sockaddr_dl * 97 if_nametosdl(char *name) 98 { 99 int mib[6] = {CTL_NET, AF_ROUTE, 0, 0, NET_RT_IFLIST, 0}; 100 char *buf, *next, *lim; 101 size_t len; 102 struct if_msghdr *ifm; 103 struct sockaddr *sa, *rti_info[RTAX_MAX]; 104 struct sockaddr_dl *sdl = NULL, *ret_sdl; 105 106 if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) 107 return(NULL); 108 if ((buf = malloc(len)) == NULL) 109 return(NULL); 110 if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) { 111 free(buf); 112 return(NULL); 113 } 114 115 lim = buf + len; 116 for (next = buf; next < lim; next += ifm->ifm_msglen) { 117 ifm = (struct if_msghdr *)next; 118 if (ifm->ifm_type == RTM_IFINFO) { 119 sa = (struct sockaddr *)(ifm + 1); 120 get_rtaddrs(ifm->ifm_addrs, sa, rti_info); 121 if ((sa = rti_info[RTAX_IFP]) != NULL) { 122 if (sa->sa_family == AF_LINK) { 123 sdl = (struct sockaddr_dl *)sa; 124 if (strlen(name) != sdl->sdl_nlen) 125 continue; /* not same len */ 126 if (strncmp(&sdl->sdl_data[0], 127 name, 128 sdl->sdl_nlen) == 0) { 129 break; 130 } 131 } 132 } 133 } 134 } 135 if (next == lim) { 136 /* search failed */ 137 free(buf); 138 return(NULL); 139 } 140 141 if ((ret_sdl = malloc(sdl->sdl_len)) == NULL) 142 return(NULL); 143 memcpy((caddr_t)ret_sdl, (caddr_t)sdl, sdl->sdl_len); 144 return(ret_sdl); 145 } 146 147 int 148 if_getmtu(char *name) 149 { 150 struct ifaddrs *ifap, *ifa; 151 struct if_data *ifd; 152 u_long mtu = 0; 153 154 if (getifaddrs(&ifap) < 0) 155 return(0); 156 for (ifa = ifap; ifa; ifa = ifa->ifa_next) { 157 if (strcmp(ifa->ifa_name, name) == 0) { 158 ifd = ifa->ifa_data; 159 if (ifd) 160 mtu = ifd->ifi_mtu; 161 break; 162 } 163 } 164 freeifaddrs(ifap); 165 166 #ifdef SIOCGIFMTU /* XXX: this ifdef may not be necessary */ 167 if (mtu == 0) { 168 struct ifreq ifr; 169 int s; 170 171 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 172 return(0); 173 174 ifr.ifr_addr.sa_family = AF_INET6; 175 strncpy(ifr.ifr_name, name, 176 sizeof(ifr.ifr_name)); 177 if (ioctl(s, SIOCGIFMTU, (caddr_t)&ifr) < 0) { 178 close(s); 179 return(0); 180 } 181 close(s); 182 183 mtu = ifr.ifr_mtu; 184 } 185 #endif 186 187 return(mtu); 188 } 189 190 /* give interface index and its old flags, then new flags returned */ 191 int 192 if_getflags(int ifindex, int oifflags) 193 { 194 struct ifreq ifr; 195 int s; 196 197 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) { 198 syslog(LOG_ERR, "<%s> socket: %s", __FUNCTION__, 199 strerror(errno)); 200 return (oifflags & ~IFF_UP); 201 } 202 203 if_indextoname(ifindex, ifr.ifr_name); 204 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) { 205 syslog(LOG_ERR, "<%s> ioctl:SIOCGIFFLAGS: failed for %s", 206 __FUNCTION__, ifr.ifr_name); 207 close(s); 208 return (oifflags & ~IFF_UP); 209 } 210 close(s); 211 return (ifr.ifr_flags); 212 } 213 214 #define ROUNDUP8(a) (1 + (((a) - 1) | 7)) 215 int 216 lladdropt_length(struct sockaddr_dl *sdl) 217 { 218 switch(sdl->sdl_type) { 219 case IFT_ETHER: 220 return(ROUNDUP8(ETHER_ADDR_LEN + 2)); 221 default: 222 return(0); 223 } 224 } 225 226 void 227 lladdropt_fill(struct sockaddr_dl *sdl, struct nd_opt_hdr *ndopt) 228 { 229 char *addr; 230 231 ndopt->nd_opt_type = ND_OPT_SOURCE_LINKADDR; /* fixed */ 232 233 switch(sdl->sdl_type) { 234 case IFT_ETHER: 235 ndopt->nd_opt_len = (ROUNDUP8(ETHER_ADDR_LEN + 2)) >> 3; 236 addr = (char *)(ndopt + 1); 237 memcpy(addr, LLADDR(sdl), ETHER_ADDR_LEN); 238 break; 239 default: 240 syslog(LOG_ERR, 241 "<%s> unsupported link type(%d)", 242 __FUNCTION__, sdl->sdl_type); 243 exit(1); 244 } 245 246 return; 247 } 248 249 int 250 rtbuf_len() 251 { 252 size_t len; 253 254 int mib[6] = {CTL_NET, AF_ROUTE, 0, AF_INET6, NET_RT_DUMP, 0}; 255 256 if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) 257 return(-1); 258 259 return(len); 260 } 261 262 #define FILTER_MATCH(type, filter) ((0x1 << type) & filter) 263 #define SIN6(s) ((struct sockaddr_in6 *)(s)) 264 #define SDL(s) ((struct sockaddr_dl *)(s)) 265 char * 266 get_next_msg(char *buf, char *lim, int ifindex, size_t *lenp, int filter) 267 { 268 struct rt_msghdr *rtm; 269 struct ifa_msghdr *ifam; 270 struct sockaddr *sa, *dst, *gw, *ifa, *rti_info[RTAX_MAX]; 271 272 *lenp = 0; 273 for (rtm = (struct rt_msghdr *)buf; 274 rtm < (struct rt_msghdr *)lim; 275 rtm = (struct rt_msghdr *)(((char *)rtm) + rtm->rtm_msglen)) { 276 /* just for safety */ 277 if (!rtm->rtm_msglen) { 278 syslog(LOG_WARNING, "<%s> rtm_msglen is 0 " 279 "(buf=%p lim=%p rtm=%p)", __FUNCTION__, 280 buf, lim, rtm); 281 break; 282 } 283 if (FILTER_MATCH(rtm->rtm_type, filter) == 0) { 284 continue; 285 } 286 287 switch (rtm->rtm_type) { 288 case RTM_GET: 289 case RTM_ADD: 290 case RTM_DELETE: 291 /* address related checks */ 292 sa = (struct sockaddr *)(rtm + 1); 293 get_rtaddrs(rtm->rtm_addrs, sa, rti_info); 294 if ((dst = rti_info[RTAX_DST]) == NULL || 295 dst->sa_family != AF_INET6) 296 continue; 297 298 if (IN6_IS_ADDR_LINKLOCAL(&SIN6(dst)->sin6_addr) || 299 IN6_IS_ADDR_MULTICAST(&SIN6(dst)->sin6_addr)) 300 continue; 301 302 if ((gw = rti_info[RTAX_GATEWAY]) == NULL || 303 gw->sa_family != AF_LINK) 304 continue; 305 if (ifindex && SDL(gw)->sdl_index != ifindex) 306 continue; 307 308 if (rti_info[RTAX_NETMASK] == NULL) 309 continue; 310 311 /* found */ 312 *lenp = rtm->rtm_msglen; 313 return (char *)rtm; 314 /* NOTREACHED */ 315 case RTM_NEWADDR: 316 case RTM_DELADDR: 317 ifam = (struct ifa_msghdr *)rtm; 318 319 /* address related checks */ 320 sa = (struct sockaddr *)(ifam + 1); 321 get_rtaddrs(ifam->ifam_addrs, sa, rti_info); 322 if ((ifa = rti_info[RTAX_IFA]) == NULL || 323 (ifa->sa_family != AF_INET && 324 ifa->sa_family != AF_INET6)) 325 continue; 326 327 if (ifa->sa_family == AF_INET6 && 328 (IN6_IS_ADDR_LINKLOCAL(&SIN6(ifa)->sin6_addr) || 329 IN6_IS_ADDR_MULTICAST(&SIN6(ifa)->sin6_addr))) 330 continue; 331 332 if (ifindex && ifam->ifam_index != ifindex) 333 continue; 334 335 /* found */ 336 *lenp = ifam->ifam_msglen; 337 return (char *)rtm; 338 /* NOTREACHED */ 339 case RTM_IFINFO: 340 /* found */ 341 *lenp = rtm->rtm_msglen; 342 return (char *)rtm; 343 /* NOTREACHED */ 344 } 345 } 346 347 return (char *)rtm; 348 } 349 #undef FILTER_MATCH 350 351 struct in6_addr * 352 get_addr(char *buf) 353 { 354 struct rt_msghdr *rtm = (struct rt_msghdr *)buf; 355 struct sockaddr *sa, *rti_info[RTAX_MAX]; 356 357 sa = (struct sockaddr *)(rtm + 1); 358 get_rtaddrs(rtm->rtm_addrs, sa, rti_info); 359 360 return(&SIN6(rti_info[RTAX_DST])->sin6_addr); 361 } 362 363 int 364 get_rtm_ifindex(char *buf) 365 { 366 struct rt_msghdr *rtm = (struct rt_msghdr *)buf; 367 struct sockaddr *sa, *rti_info[RTAX_MAX]; 368 369 sa = (struct sockaddr *)(rtm + 1); 370 get_rtaddrs(rtm->rtm_addrs, sa, rti_info); 371 372 return(((struct sockaddr_dl *)rti_info[RTAX_GATEWAY])->sdl_index); 373 } 374 375 int 376 get_ifm_ifindex(char *buf) 377 { 378 struct if_msghdr *ifm = (struct if_msghdr *)buf; 379 380 return ((int)ifm->ifm_index); 381 } 382 383 int 384 get_ifam_ifindex(char *buf) 385 { 386 struct ifa_msghdr *ifam = (struct ifa_msghdr *)buf; 387 388 return ((int)ifam->ifam_index); 389 } 390 391 int 392 get_ifm_flags(char *buf) 393 { 394 struct if_msghdr *ifm = (struct if_msghdr *)buf; 395 396 return (ifm->ifm_flags); 397 } 398 399 int 400 get_prefixlen(char *buf) 401 { 402 struct rt_msghdr *rtm = (struct rt_msghdr *)buf; 403 struct sockaddr *sa, *rti_info[RTAX_MAX]; 404 u_char *p, *lim; 405 406 sa = (struct sockaddr *)(rtm + 1); 407 get_rtaddrs(rtm->rtm_addrs, sa, rti_info); 408 sa = rti_info[RTAX_NETMASK]; 409 410 p = (u_char *)(&SIN6(sa)->sin6_addr); 411 lim = (u_char *)sa + sa->sa_len; 412 return prefixlen(p, lim); 413 } 414 415 int 416 prefixlen(u_char *p, u_char *lim) 417 { 418 int masklen; 419 420 for (masklen = 0; p < lim; p++) { 421 switch (*p) { 422 case 0xff: 423 masklen += 8; 424 break; 425 case 0xfe: 426 masklen += 7; 427 break; 428 case 0xfc: 429 masklen += 6; 430 break; 431 case 0xf8: 432 masklen += 5; 433 break; 434 case 0xf0: 435 masklen += 4; 436 break; 437 case 0xe0: 438 masklen += 3; 439 break; 440 case 0xc0: 441 masklen += 2; 442 break; 443 case 0x80: 444 masklen += 1; 445 break; 446 case 0x00: 447 break; 448 default: 449 return(-1); 450 } 451 } 452 453 return(masklen); 454 } 455 456 int 457 rtmsg_type(char *buf) 458 { 459 struct rt_msghdr *rtm = (struct rt_msghdr *)buf; 460 461 return(rtm->rtm_type); 462 } 463 464 int 465 rtmsg_len(char *buf) 466 { 467 struct rt_msghdr *rtm = (struct rt_msghdr *)buf; 468 469 return(rtm->rtm_msglen); 470 } 471 472 int 473 ifmsg_len(char *buf) 474 { 475 struct if_msghdr *ifm = (struct if_msghdr *)buf; 476 477 return(ifm->ifm_msglen); 478 } 479 480 /* 481 * alloc buffer and get if_msghdrs block from kernel, 482 * and put them into the buffer 483 */ 484 static void 485 get_iflist(char **buf, size_t *size) 486 { 487 int mib[6]; 488 489 mib[0] = CTL_NET; 490 mib[1] = PF_ROUTE; 491 mib[2] = 0; 492 mib[3] = AF_INET6; 493 mib[4] = NET_RT_IFLIST; 494 mib[5] = 0; 495 496 if (sysctl(mib, 6, NULL, size, NULL, 0) < 0) { 497 syslog(LOG_ERR, "<%s> sysctl: iflist size get failed", 498 __FUNCTION__); 499 exit(1); 500 } 501 if ((*buf = malloc(*size)) == NULL) { 502 syslog(LOG_ERR, "<%s> malloc failed", __FUNCTION__); 503 exit(1); 504 } 505 if (sysctl(mib, 6, *buf, size, NULL, 0) < 0) { 506 syslog(LOG_ERR, "<%s> sysctl: iflist get failed", 507 __FUNCTION__); 508 exit(1); 509 } 510 return; 511 } 512 513 /* 514 * alloc buffer and parse if_msghdrs block passed as arg, 515 * and init the buffer as list of pointers ot each of the if_msghdr. 516 */ 517 static void 518 parse_iflist(struct if_msghdr ***ifmlist_p, char *buf, size_t bufsize) 519 { 520 int iflentry_size, malloc_size; 521 struct if_msghdr *ifm; 522 struct ifa_msghdr *ifam; 523 char *lim; 524 525 /* 526 * Estimate least size of an iflist entry, to be obtained from kernel. 527 * Should add sizeof(sockaddr) ?? 528 */ 529 iflentry_size = sizeof(struct if_msghdr); 530 /* roughly estimate max list size of pointers to each if_msghdr */ 531 malloc_size = (bufsize/iflentry_size) * sizeof(size_t); 532 if ((*ifmlist_p = (struct if_msghdr **)malloc(malloc_size)) == NULL) { 533 syslog(LOG_ERR, "<%s> malloc failed", __FUNCTION__); 534 exit(1); 535 } 536 537 lim = buf + bufsize; 538 for (ifm = (struct if_msghdr *)buf; ifm < (struct if_msghdr *)lim;) { 539 if (ifm->ifm_msglen == 0) { 540 syslog(LOG_WARNING, "<%s> ifm_msglen is 0 " 541 "(buf=%p lim=%p ifm=%p)", __FUNCTION__, 542 buf, lim, ifm); 543 return; 544 } 545 546 if (ifm->ifm_type == RTM_IFINFO) { 547 (*ifmlist_p)[ifm->ifm_index] = ifm; 548 } else { 549 syslog(LOG_ERR, "out of sync parsing NET_RT_IFLIST\n" 550 "expected %d, got %d\n msglen = %d\n" 551 "buf:%p, ifm:%p, lim:%p\n", 552 RTM_IFINFO, ifm->ifm_type, ifm->ifm_msglen, 553 buf, ifm, lim); 554 exit (1); 555 } 556 for (ifam = (struct ifa_msghdr *) 557 ((char *)ifm + ifm->ifm_msglen); 558 ifam < (struct ifa_msghdr *)lim; 559 ifam = (struct ifa_msghdr *) 560 ((char *)ifam + ifam->ifam_msglen)) { 561 /* just for safety */ 562 if (!ifam->ifam_msglen) { 563 syslog(LOG_WARNING, "<%s> ifa_msglen is 0 " 564 "(buf=%p lim=%p ifam=%p)", __FUNCTION__, 565 buf, lim, ifam); 566 return; 567 } 568 if (ifam->ifam_type != RTM_NEWADDR) 569 break; 570 } 571 ifm = (struct if_msghdr *)ifam; 572 } 573 } 574 575 void 576 init_iflist() 577 { 578 if (ifblock) { 579 free(ifblock); 580 ifblock_size = 0; 581 } 582 if (iflist) 583 free(iflist); 584 /* get iflist block from kernel */ 585 get_iflist(&ifblock, &ifblock_size); 586 587 /* make list of pointers to each if_msghdr */ 588 parse_iflist(&iflist, ifblock, ifblock_size); 589 } 590