1 /* $NetBSD: arp.c,v 1.55 2016/04/04 07:37:08 ozaki-r Exp $ */ 2 3 /* 4 * Copyright (c) 1984, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Sun Microsystems, Inc. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 #include <sys/cdefs.h> 36 #ifndef lint 37 __COPYRIGHT("@(#) Copyright (c) 1984, 1993\ 38 The Regents of the University of California. All rights reserved."); 39 #endif /* not lint */ 40 41 #ifndef lint 42 #if 0 43 static char sccsid[] = "@(#)arp.c 8.3 (Berkeley) 4/28/95"; 44 #else 45 __RCSID("$NetBSD: arp.c,v 1.55 2016/04/04 07:37:08 ozaki-r Exp $"); 46 #endif 47 #endif /* not lint */ 48 49 /* 50 * arp - display, set, and delete arp table entries 51 */ 52 53 #include <sys/param.h> 54 #include <sys/file.h> 55 #include <sys/socket.h> 56 #include <sys/sysctl.h> 57 #include <sys/ioctl.h> 58 59 #include <net/if.h> 60 #include <net/if_dl.h> 61 #include <net/if_ether.h> 62 #include <net/if_types.h> 63 #include <net/route.h> 64 #include <netinet/in.h> 65 #include <netinet/if_inarp.h> 66 #include <arpa/inet.h> 67 68 #include <err.h> 69 #include <errno.h> 70 #include <netdb.h> 71 #include <nlist.h> 72 #include <paths.h> 73 #include <stdio.h> 74 #include <stdlib.h> 75 #include <string.h> 76 #include <unistd.h> 77 #include <ifaddrs.h> 78 79 #include "prog_ops.h" 80 81 static int is_llinfo(const struct sockaddr_dl *, int); 82 static int delete(const char *, const char *); 83 static void dump(uint32_t); 84 static void delete_all(void); 85 static void sdl_print(const struct sockaddr_dl *); 86 static int getifname(u_int16_t, char *, size_t); 87 static int atosdl(const char *s, struct sockaddr_dl *sdl); 88 static int file(const char *); 89 static void get(const char *); 90 static int getinetaddr(const char *, struct in_addr *); 91 static int getsocket(void); 92 static struct rt_msghdr * 93 rtmsg(const int, const int, const struct sockaddr_inarp *, 94 const struct sockaddr_dl *); 95 static int set(int, char **); 96 static void usage(void) __dead; 97 98 static int aflag, nflag, vflag; 99 static struct sockaddr_in so_mask = { 100 .sin_len = 8, 101 .sin_addr = { 102 .s_addr = 0xffffffff 103 } 104 }; 105 static struct sockaddr_inarp blank_sin = { 106 .sin_len = sizeof(blank_sin), 107 .sin_family = AF_INET 108 }; 109 static struct sockaddr_dl blank_sdl = { 110 .sdl_len = sizeof(blank_sdl), 111 .sdl_family = AF_LINK 112 }; 113 114 static int expire_time, flags, export_only, doing_proxy, found_entry; 115 116 int 117 main(int argc, char **argv) 118 { 119 int ch; 120 int op = 0; 121 122 setprogname(argv[0]); 123 124 while ((ch = getopt(argc, argv, "andsfv")) != -1) 125 switch((char)ch) { 126 case 'a': 127 aflag = 1; 128 break; 129 case 'd': 130 case 's': 131 case 'f': 132 if (op) 133 usage(); 134 op = ch; 135 break; 136 case 'n': 137 nflag = 1; 138 break; 139 case 'v': 140 vflag = 1; 141 break; 142 default: 143 usage(); 144 } 145 argc -= optind; 146 argv += optind; 147 148 if (!op && aflag) 149 op = 'a'; 150 151 if (prog_init && prog_init() == -1) 152 err(1, "init failed"); 153 154 switch((char)op) { 155 case 'a': 156 dump(0); 157 break; 158 case 'd': 159 if (aflag && argc == 0) 160 delete_all(); 161 else { 162 if (aflag || argc < 1 || argc > 2) 163 usage(); 164 (void)delete(argv[0], argv[1]); 165 } 166 break; 167 case 's': 168 if (argc < 2 || argc > 7) 169 usage(); 170 return (set(argc, argv) ? 1 : 0); 171 case 'f': 172 if (argc != 1) 173 usage(); 174 return (file(argv[0])); 175 default: 176 if (argc != 1) 177 usage(); 178 get(argv[0]); 179 break; 180 } 181 return (0); 182 } 183 184 /* 185 * Process a file to set standard arp entries 186 */ 187 static int 188 file(const char *name) 189 { 190 char *line, *argv[5]; 191 int i, retval; 192 FILE *fp; 193 194 if (!strcmp(name, "-")) { 195 fp = stdin; 196 } else { 197 fp = fopen(name, "r"); 198 if (fp == NULL) { 199 err(1, "Cannot open %s", name); 200 } 201 } 202 retval = 0; 203 for (; (line = fparseln(fp, NULL, NULL, NULL, 0)) != NULL; free(line)) { 204 char **ap, *inputstring; 205 206 inputstring = line; 207 for (ap = argv; ap < &argv[sizeof(argv) / sizeof(argv[0])] && 208 (*ap = stresep(&inputstring, " \t", '\\')) != NULL;) { 209 if (**ap != '\0') 210 ap++; 211 } 212 i = ap - argv; 213 if (i < 2) { 214 warnx("bad line: %s", line); 215 retval = 1; 216 continue; 217 } 218 if (set(i, argv)) 219 retval = 1; 220 } 221 if (fp != stdin) 222 (void)fclose(fp); 223 return retval; 224 } 225 226 static int 227 getsocket(void) 228 { 229 int s; 230 s = prog_socket(PF_ROUTE, SOCK_RAW, 0); 231 if (s < 0) 232 err(1, "socket"); 233 return s; 234 } 235 236 static int 237 getlink(const char *name, struct sockaddr_dl *sdl) 238 { 239 struct ifaddrs *ifap, *ifa; 240 241 if (getifaddrs(&ifap) != 0) { 242 warn("getifaddrs"); 243 return 0; 244 } 245 246 for (ifa = ifap; ifa; ifa = ifa->ifa_next) { 247 if (ifa->ifa_addr->sa_family != AF_LINK) 248 continue; 249 if (strcmp(ifa->ifa_name, name) != 0) 250 continue; 251 memcpy(sdl, ifa->ifa_addr, sizeof(*sdl)); 252 freeifaddrs(ifap); 253 return 1; 254 } 255 freeifaddrs(ifap); 256 return 0; 257 } 258 259 /* 260 * Set an individual arp entry 261 */ 262 static int 263 set(int argc, char **argv) 264 { 265 struct sockaddr_inarp *sina; 266 struct sockaddr_dl *sdl = NULL; 267 struct rt_msghdr *rtm; 268 char *host = argv[0], *eaddr; 269 struct sockaddr_inarp sin_m = blank_sin; /* struct copy */ 270 struct sockaddr_dl sdl_m = blank_sdl; /* struct copy */ 271 int s; 272 273 eaddr = argv[1]; 274 275 s = getsocket(); 276 argc -= 2; 277 argv += 2; 278 279 if (getinetaddr(host, &sin_m.sin_addr) == -1) 280 return (1); 281 if (atosdl(eaddr, &sdl_m)) 282 warnx("invalid link-level address '%s'", eaddr); 283 doing_proxy = flags = export_only = expire_time = 0; 284 for (; argc-- > 0; argv++) { 285 if (strncmp(argv[0], "temp", 4) == 0) { 286 struct timeval timev; 287 (void)gettimeofday(&timev, 0); 288 expire_time = timev.tv_sec + 20 * 60; 289 } 290 else if (strncmp(argv[0], "pub", 3) == 0) { 291 flags |= RTF_ANNOUNCE; 292 doing_proxy = 1; 293 if (argc && strncmp(argv[1], "pro", 3) == 0) { 294 export_only = 1; 295 argc--; argv++; 296 } 297 } else if (strncmp(argv[0], "trail", 5) == 0) { 298 warnx("%s: Sending trailers is no longer supported", 299 host); 300 } else if (strcmp(argv[0], "ifscope") == 0) { 301 if (argc == 0) { 302 warnx("missing interface for ifscope"); 303 continue; 304 } 305 argc--; 306 argv++; 307 if (!getlink(argv[0], &sdl_m)) 308 warnx("cannot get link address for %s", argv[0]); 309 } 310 311 } 312 tryagain: 313 rtm = rtmsg(s, RTM_GET, &sin_m, &sdl_m); 314 if (rtm == NULL) { 315 warn("%s", host); 316 return (1); 317 } 318 sina = (struct sockaddr_inarp *)(void *)(rtm + 1); 319 sdl = (struct sockaddr_dl *)(void *)(RT_ROUNDUP(sina->sin_len) + 320 (char *)(void *)sina); 321 if (sina->sin_addr.s_addr == sin_m.sin_addr.s_addr) { 322 if (is_llinfo(sdl, rtm->rtm_flags)) 323 goto overwrite; 324 if (doing_proxy == 0) { 325 warnx("set: can only proxy for %s", host); 326 return (1); 327 } 328 if (sin_m.sin_other & SIN_PROXY) { 329 warnx("set: proxy entry exists for non 802 device"); 330 return (1); 331 } 332 sin_m.sin_other = SIN_PROXY; 333 export_only = 1; 334 goto tryagain; 335 } 336 overwrite: 337 if (sdl->sdl_family != AF_LINK) { 338 warnx("cannot intuit interface index and type for %s", 339 host); 340 return (1); 341 } 342 sdl_m.sdl_type = sdl->sdl_type; 343 sdl_m.sdl_index = sdl->sdl_index; 344 sin_m.sin_other = 0; 345 if (doing_proxy && export_only) 346 sin_m.sin_other = SIN_PROXY; 347 rtm = rtmsg(s, RTM_ADD, &sin_m, &sdl_m); 348 if (vflag) 349 (void)printf("%s (%s) added\n", host, eaddr); 350 return (rtm == NULL) ? 1 : 0; 351 } 352 353 /* 354 * Display an individual arp entry 355 */ 356 static void 357 get(const char *host) 358 { 359 struct sockaddr_inarp sin = blank_sin; /* struct copy */ 360 361 if (getinetaddr(host, &sin.sin_addr) == -1) 362 exit(1); 363 dump(sin.sin_addr.s_addr); 364 if (found_entry == 0) 365 errx(1, "%s (%s) -- no entry", host, inet_ntoa(sin.sin_addr)); 366 } 367 368 369 static int 370 is_llinfo(const struct sockaddr_dl *sdl, int rtflags) 371 { 372 if (sdl->sdl_family != AF_LINK || 373 (rtflags & (RTF_LLDATA|RTF_GATEWAY)) != RTF_LLDATA) 374 return 0; 375 376 switch (sdl->sdl_type) { 377 case IFT_ETHER: 378 case IFT_FDDI: 379 case IFT_ISO88023: 380 case IFT_ISO88024: 381 case IFT_ISO88025: 382 case IFT_ARCNET: 383 return 1; 384 default: 385 return 0; 386 } 387 } 388 389 /* 390 * Delete an arp entry 391 */ 392 int 393 delete(const char *host, const char *info) 394 { 395 struct sockaddr_inarp *sina; 396 struct sockaddr_dl *sdl; 397 struct rt_msghdr *rtm; 398 struct sockaddr_inarp sin_m = blank_sin; /* struct copy */ 399 struct sockaddr_dl sdl_m = blank_sdl; /* struct copy */ 400 int s; 401 402 s = getsocket(); 403 if (info && strncmp(info, "pro", 3) == 0) 404 sin_m.sin_other = SIN_PROXY; 405 if (getinetaddr(host, &sin_m.sin_addr) == -1) 406 return (1); 407 tryagain: 408 rtm = rtmsg(s, RTM_GET, &sin_m, &sdl_m); 409 if (rtm == NULL) { 410 warn("%s", host); 411 return (1); 412 } 413 sina = (struct sockaddr_inarp *)(void *)(rtm + 1); 414 sdl = (struct sockaddr_dl *)(void *)(RT_ROUNDUP(sina->sin_len) + 415 (char *)(void *)sina); 416 if (sina->sin_addr.s_addr == sin_m.sin_addr.s_addr && 417 is_llinfo(sdl, rtm->rtm_flags)) 418 goto delete; 419 if (sin_m.sin_other & SIN_PROXY) { 420 warnx("delete: can't locate %s", host); 421 return (1); 422 } else { 423 sin_m.sin_other = SIN_PROXY; 424 goto tryagain; 425 } 426 delete: 427 if (sdl->sdl_family != AF_LINK) { 428 (void)warnx("cannot locate %s", host); 429 return (1); 430 } 431 rtm = rtmsg(s, RTM_DELETE, &sin_m, sdl); 432 if (rtm == NULL) 433 return (1); 434 if (vflag) 435 (void)printf("%s (%s) deleted\n", host, 436 inet_ntoa(sin_m.sin_addr)); 437 return (0); 438 } 439 440 /* 441 * Dump the entire arp table 442 */ 443 void 444 dump(uint32_t addr) 445 { 446 int mib[6]; 447 size_t needed; 448 char ifname[IFNAMSIZ]; 449 char *lim, *buf, *next; 450 const char *host; 451 struct rt_msghdr *rtm; 452 struct sockaddr_inarp *sina; 453 struct sockaddr_dl *sdl; 454 struct hostent *hp; 455 456 mib[0] = CTL_NET; 457 mib[1] = PF_ROUTE; 458 mib[2] = 0; 459 mib[3] = AF_INET; 460 mib[4] = NET_RT_FLAGS; 461 mib[5] = 0; 462 if (prog_sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) 463 err(1, "route-sysctl-estimate"); 464 if (needed == 0) 465 return; 466 if ((buf = malloc(needed)) == NULL) 467 err(1, "malloc"); 468 if (prog_sysctl(mib, 6, buf, &needed, NULL, 0) < 0) 469 err(1, "actual retrieval of routing table"); 470 lim = buf + needed; 471 for (next = buf; next < lim; next += rtm->rtm_msglen) { 472 rtm = (struct rt_msghdr *)(void *)next; 473 sina = (struct sockaddr_inarp *)(void *)(rtm + 1); 474 sdl = (struct sockaddr_dl *)(void *) 475 (RT_ROUNDUP(sina->sin_len) + (char *)(void *)sina); 476 if (addr) { 477 if (addr != sina->sin_addr.s_addr) 478 continue; 479 found_entry = 1; 480 } 481 if (nflag == 0) 482 hp = gethostbyaddr((const char *)(void *) 483 &(sina->sin_addr), 484 sizeof sina->sin_addr, AF_INET); 485 else 486 hp = NULL; 487 488 host = hp ? hp->h_name : "?"; 489 490 (void)printf("%s (%s) at ", host, inet_ntoa(sina->sin_addr)); 491 if (sdl->sdl_alen) 492 sdl_print(sdl); 493 else 494 (void)printf("(incomplete)"); 495 496 if (sdl->sdl_index) { 497 if (getifname(sdl->sdl_index, ifname, sizeof(ifname)) == 0) 498 (void)printf(" on %s", ifname); 499 } 500 501 if (rtm->rtm_rmx.rmx_expire == 0) 502 (void)printf(" permanent"); 503 if (sina->sin_other & SIN_PROXY) 504 (void)printf(" published (proxy only)"); 505 if (rtm->rtm_addrs & RTA_NETMASK) { 506 sina = (struct sockaddr_inarp *)(void *) 507 (RT_ROUNDUP(sdl->sdl_len) + (char *)(void *)sdl); 508 if (sina->sin_addr.s_addr == 0xffffffff) 509 (void)printf(" published"); 510 if (sina->sin_len != 8) 511 (void)printf("(weird)"); 512 } 513 (void)printf("\n"); 514 } 515 free(buf); 516 } 517 518 /* 519 * Delete the entire arp table 520 */ 521 void 522 delete_all(void) 523 { 524 int mib[6]; 525 size_t needed; 526 char addr[sizeof("000.000.000.000\0")]; 527 char *lim, *buf, *next; 528 struct rt_msghdr *rtm; 529 struct sockaddr_inarp *sina; 530 531 mib[0] = CTL_NET; 532 mib[1] = PF_ROUTE; 533 mib[2] = 0; 534 mib[3] = AF_INET; 535 mib[4] = NET_RT_FLAGS; 536 mib[5] = 0; 537 if (prog_sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) 538 err(1, "route-sysctl-estimate"); 539 if (needed == 0) 540 return; 541 if ((buf = malloc(needed)) == NULL) 542 err(1, "malloc"); 543 if (prog_sysctl(mib, 6, buf, &needed, NULL, 0) < 0) 544 err(1, "actual retrieval of routing table"); 545 lim = buf + needed; 546 for (next = buf; next < lim; next += rtm->rtm_msglen) { 547 rtm = (struct rt_msghdr *)(void *)next; 548 sina = (struct sockaddr_inarp *)(void *)(rtm + 1); 549 (void)snprintf(addr, sizeof(addr), "%s", 550 inet_ntoa(sina->sin_addr)); 551 (void)delete(addr, NULL); 552 } 553 free(buf); 554 } 555 556 void 557 sdl_print(const struct sockaddr_dl *sdl) 558 { 559 char hbuf[NI_MAXHOST]; 560 561 if (getnameinfo((const struct sockaddr *)(const void *)sdl, 562 (socklen_t)sdl->sdl_len, 563 hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0) 564 (void)printf("<invalid>"); 565 else 566 (void)printf("%s", hbuf); 567 } 568 569 static int 570 atosdl(const char *ss, struct sockaddr_dl *sdl) 571 { 572 int i; 573 unsigned long b; 574 char *endp; 575 char *p; 576 char *t, *r; 577 578 p = LLADDR(sdl); 579 endp = ((char *)(void *)sdl) + sdl->sdl_len; 580 i = 0; 581 582 b = strtoul(ss, &t, 16); 583 if (b > 255 || t == ss) 584 return 1; 585 586 *p++ = (char)b; 587 ++i; 588 while ((p < endp) && (*t++ == ':')) { 589 b = strtoul(t, &r, 16); 590 if (b > 255 || r == t) 591 break; 592 *p++ = (char)b; 593 ++i; 594 t = r; 595 } 596 sdl->sdl_alen = i; 597 598 return 0; 599 } 600 601 static void 602 usage(void) 603 { 604 const char *progname; 605 606 progname = getprogname(); 607 (void)fprintf(stderr, "Usage: %s [-n] hostname\n", progname); 608 (void)fprintf(stderr, " %s [-nv] -a\n", progname); 609 (void)fprintf(stderr, " %s [-v] -d [-a|hostname [pub [proxy]]]\n", 610 progname); 611 (void)fprintf(stderr, " %s -s hostname ether_addr [temp] [pub [proxy]]\n", 612 progname); 613 (void)fprintf(stderr, " %s -f filename\n", progname); 614 exit(1); 615 } 616 617 static struct rt_msghdr * 618 rtmsg(const int s, const int cmd, const struct sockaddr_inarp *sin, 619 const struct sockaddr_dl *sdl) 620 { 621 static int seq; 622 struct rt_msghdr *rtm; 623 char *cp; 624 int l; 625 static struct { 626 struct rt_msghdr m_rtm; 627 char m_space[512]; 628 } m_rtmsg; 629 pid_t pid; 630 631 rtm = &m_rtmsg.m_rtm; 632 cp = m_rtmsg.m_space; 633 errno = 0; 634 635 /* XXX depends on rtm is filled by RTM_GET */ 636 if (cmd == RTM_DELETE) { 637 rtm->rtm_flags |= RTF_LLDATA; 638 goto doit; 639 } 640 (void)memset(&m_rtmsg, 0, sizeof(m_rtmsg)); 641 rtm->rtm_flags = flags; 642 rtm->rtm_version = RTM_VERSION; 643 644 switch (cmd) { 645 default: 646 errx(1, "internal wrong cmd"); 647 /*NOTREACHED*/ 648 case RTM_ADD: 649 rtm->rtm_addrs |= RTA_GATEWAY; 650 rtm->rtm_rmx.rmx_expire = expire_time; 651 rtm->rtm_inits = RTV_EXPIRE; 652 rtm->rtm_flags |= (RTF_HOST | RTF_STATIC | RTF_LLDATA); 653 if (doing_proxy) { 654 if (!export_only) { 655 rtm->rtm_addrs |= RTA_NETMASK; 656 rtm->rtm_flags &= ~RTF_HOST; 657 } 658 } 659 rtm->rtm_addrs |= RTA_DST; 660 break; 661 case RTM_GET: 662 rtm->rtm_flags |= RTF_LLDATA; 663 rtm->rtm_addrs |= RTA_DST | RTA_IFP; 664 } 665 666 #define NEXTADDR(w, s) \ 667 if (rtm->rtm_addrs & (w)) { \ 668 (void)memcpy(cp, &s, \ 669 (size_t)((const struct sockaddr *)&s)->sa_len); \ 670 RT_ADVANCE(cp, ((const struct sockaddr *)&s)); \ 671 } 672 673 NEXTADDR(RTA_DST, *sin); 674 NEXTADDR(RTA_GATEWAY, *sdl); 675 NEXTADDR(RTA_NETMASK, so_mask); 676 677 rtm->rtm_msglen = cp - (char *)(void *)&m_rtmsg; 678 doit: 679 l = rtm->rtm_msglen; 680 rtm->rtm_seq = ++seq; 681 rtm->rtm_type = cmd; 682 if (prog_write(s, &m_rtmsg, (size_t)l) < 0) { 683 if (errno != ESRCH || cmd != RTM_DELETE) { 684 warn("writing to routing socket"); 685 return NULL; 686 } 687 } 688 689 pid = prog_getpid(); 690 do { 691 l = prog_read(s, &m_rtmsg, sizeof(m_rtmsg)); 692 } while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid)); 693 if (l < 0) 694 warn("read from routing socket"); 695 return rtm; 696 } 697 698 static int 699 getinetaddr(const char *host, struct in_addr *inap) 700 { 701 struct hostent *hp; 702 703 if (inet_aton(host, inap) == 1) 704 return (0); 705 if ((hp = gethostbyname(host)) == NULL) { 706 warnx("%s: %s", host, hstrerror(h_errno)); 707 return (-1); 708 } 709 (void)memcpy(inap, hp->h_addr, sizeof(*inap)); 710 return (0); 711 } 712 713 static int 714 getifname(u_int16_t ifindex, char *ifname, size_t l) 715 { 716 int i; 717 struct ifaddrs *addr; 718 const struct sockaddr_dl *sdl = NULL; 719 static struct ifaddrs* ifaddrs = NULL; 720 721 if (ifaddrs == NULL) { 722 i = getifaddrs(&ifaddrs); 723 if (i != 0) 724 err(1, "getifaddrs"); 725 } 726 727 for (addr = ifaddrs; addr; addr = addr->ifa_next) { 728 if (addr->ifa_addr == NULL || 729 addr->ifa_addr->sa_family != AF_LINK) 730 continue; 731 732 sdl = (const struct sockaddr_dl *)(void *)addr->ifa_addr; 733 if (sdl && sdl->sdl_index == ifindex) { 734 (void) strlcpy(ifname, addr->ifa_name, l); 735 return 0; 736 } 737 } 738 739 return -1; 740 } 741