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