1 /* 2 * Copyright (c) 1984, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Sun Microsystems, Inc. 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 * 4. Neither the name of the University 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 REGENTS 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 REGENTS 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 #if 0 34 #ifndef lint 35 static char const copyright[] = 36 "@(#) Copyright (c) 1984, 1993\n\ 37 The Regents of the University of California. All rights reserved.\n"; 38 #endif /* not lint */ 39 40 #ifndef lint 41 static char const sccsid[] = "@(#)from: arp.c 8.2 (Berkeley) 1/2/94"; 42 #endif /* not lint */ 43 #endif 44 #include <sys/cdefs.h> 45 __FBSDID("$FreeBSD$"); 46 47 /* 48 * arp - display, set, and delete arp table entries 49 */ 50 51 52 #include <sys/param.h> 53 #include <sys/file.h> 54 #include <sys/socket.h> 55 #include <sys/sockio.h> 56 #include <sys/sysctl.h> 57 #include <sys/ioctl.h> 58 #include <sys/time.h> 59 60 #include <net/if.h> 61 #include <net/if_dl.h> 62 #include <net/if_types.h> 63 #include <net/route.h> 64 #include <net/iso88025.h> 65 66 #include <netinet/in.h> 67 #include <netinet/if_ether.h> 68 69 #include <arpa/inet.h> 70 71 #include <ctype.h> 72 #include <err.h> 73 #include <errno.h> 74 #include <netdb.h> 75 #include <nlist.h> 76 #include <paths.h> 77 #include <stdio.h> 78 #include <stdlib.h> 79 #include <string.h> 80 #include <strings.h> 81 #include <unistd.h> 82 83 typedef void (action_fn)(struct sockaddr_dl *sdl, 84 struct sockaddr_inarp *s_in, struct rt_msghdr *rtm); 85 86 static int search(u_long addr, action_fn *action); 87 static action_fn print_entry; 88 static action_fn nuke_entry; 89 90 static int delete(char *host, int do_proxy); 91 static void usage(void); 92 static int set(int argc, char **argv); 93 static void get(char *host); 94 static int file(char *name); 95 static struct rt_msghdr *rtmsg(int cmd, 96 struct sockaddr_inarp *dst, struct sockaddr_dl *sdl); 97 static int get_ether_addr(u_int32_t ipaddr, struct ether_addr *hwaddr); 98 99 static int nflag; /* no reverse dns lookups */ 100 static char *rifname; 101 102 static int expire_time, flags, doing_proxy, proxy_only; 103 104 /* which function we're supposed to do */ 105 #define F_GET 1 106 #define F_SET 2 107 #define F_FILESET 3 108 #define F_REPLACE 4 109 #define F_DELETE 5 110 111 #define SETFUNC(f) { if (func) usage(); func = (f); } 112 113 int 114 main(int argc, char *argv[]) 115 { 116 int ch, func = 0; 117 int rtn = 0; 118 int aflag = 0; /* do it for all entries */ 119 120 while ((ch = getopt(argc, argv, "andfsSi:")) != -1) 121 switch((char)ch) { 122 case 'a': 123 aflag = 1; 124 break; 125 case 'd': 126 SETFUNC(F_DELETE); 127 break; 128 case 'n': 129 nflag = 1; 130 break; 131 case 'S': 132 SETFUNC(F_REPLACE); 133 break; 134 case 's': 135 SETFUNC(F_SET); 136 break; 137 case 'f' : 138 SETFUNC(F_FILESET); 139 break; 140 case 'i': 141 rifname = optarg; 142 break; 143 case '?': 144 default: 145 usage(); 146 } 147 argc -= optind; 148 argv += optind; 149 150 if (!func) 151 func = F_GET; 152 if (rifname) { 153 if (func != F_GET) 154 errx(1, "-i not applicable to this operation"); 155 if (if_nametoindex(rifname) == 0) { 156 if (errno == ENXIO) 157 errx(1, "interface %s does not exist", rifname); 158 else 159 err(1, "if_nametoindex(%s)", rifname); 160 } 161 } 162 switch (func) { 163 case F_GET: 164 if (aflag) { 165 if (argc != 0) 166 usage(); 167 search(0, print_entry); 168 } else { 169 if (argc != 1) 170 usage(); 171 get(argv[0]); 172 } 173 break; 174 case F_SET: 175 case F_REPLACE: 176 if (argc < 2 || argc > 6) 177 usage(); 178 if (func == F_REPLACE) 179 delete(argv[0], 0); 180 rtn = set(argc, argv) ? 1 : 0; 181 break; 182 case F_DELETE: 183 if (aflag) { 184 if (argc != 0) 185 usage(); 186 search(0, nuke_entry); 187 } else { 188 if (argc == 2 && strncmp(argv[1], "pub", 3) == 0) 189 ch = SIN_PROXY; 190 else if (argc == 1) 191 ch = 0; 192 else 193 usage(); 194 rtn = delete(argv[0], ch); 195 } 196 break; 197 case F_FILESET: 198 if (argc != 1) 199 usage(); 200 rtn = file(argv[0]); 201 break; 202 } 203 204 return(rtn); 205 } 206 207 /* 208 * Process a file to set standard arp entries 209 */ 210 static int 211 file(char *name) 212 { 213 FILE *fp; 214 int i, retval; 215 char line[100], arg[5][50], *args[5], *p; 216 217 if ((fp = fopen(name, "r")) == NULL) 218 err(1, "cannot open %s", name); 219 args[0] = &arg[0][0]; 220 args[1] = &arg[1][0]; 221 args[2] = &arg[2][0]; 222 args[3] = &arg[3][0]; 223 args[4] = &arg[4][0]; 224 retval = 0; 225 while(fgets(line, 100, fp) != NULL) { 226 if ((p = strchr(line, '#')) != NULL) 227 *p = '\0'; 228 for (p = line; isblank(*p); p++); 229 if (*p == '\n' || *p == '\0') 230 continue; 231 i = sscanf(p, "%49s %49s %49s %49s %49s", arg[0], arg[1], 232 arg[2], arg[3], arg[4]); 233 if (i < 2) { 234 warnx("bad line: %s", line); 235 retval = 1; 236 continue; 237 } 238 if (set(i, args)) 239 retval = 1; 240 } 241 fclose(fp); 242 return (retval); 243 } 244 245 /* 246 * Given a hostname, fills up a (static) struct sockaddr_inarp with 247 * the address of the host and returns a pointer to the 248 * structure. 249 */ 250 static struct sockaddr_inarp * 251 getaddr(char *host) 252 { 253 struct hostent *hp; 254 static struct sockaddr_inarp reply; 255 256 bzero(&reply, sizeof(reply)); 257 reply.sin_len = sizeof(reply); 258 reply.sin_family = AF_INET; 259 reply.sin_addr.s_addr = inet_addr(host); 260 if (reply.sin_addr.s_addr == INADDR_NONE) { 261 if (!(hp = gethostbyname(host))) { 262 warnx("%s: %s", host, hstrerror(h_errno)); 263 return NULL; 264 } 265 bcopy((char *)hp->h_addr, (char *)&reply.sin_addr, 266 sizeof reply.sin_addr); 267 } 268 return &reply; 269 } 270 271 /* 272 * returns true if the type is a valid one for ARP 273 */ 274 static int 275 valid_type(int type) 276 { 277 switch (type) { 278 default: 279 return 0; 280 case IFT_ETHER: 281 case IFT_FDDI: 282 case IFT_ISO88023: 283 case IFT_ISO88024: 284 case IFT_ISO88025: 285 case IFT_L2VLAN: 286 return 1; 287 } 288 } 289 290 /* 291 * Set an individual arp entry 292 */ 293 static int 294 set(int argc, char **argv) 295 { 296 struct sockaddr_inarp *addr; 297 struct sockaddr_inarp *dst; /* what are we looking for */ 298 struct sockaddr_dl *sdl; 299 struct rt_msghdr *rtm; 300 struct ether_addr *ea; 301 char *host = argv[0], *eaddr = argv[1]; 302 struct sockaddr_dl sdl_m; 303 304 argc -= 2; 305 argv += 2; 306 307 bzero(&sdl_m, sizeof(sdl_m)); 308 sdl_m.sdl_len = sizeof(sdl_m); 309 sdl_m.sdl_family = AF_LINK; 310 311 dst = getaddr(host); 312 if (dst == NULL) 313 return (1); 314 doing_proxy = flags = proxy_only = expire_time = 0; 315 while (argc-- > 0) { 316 if (strncmp(argv[0], "temp", 4) == 0) { 317 struct timeval tv; 318 gettimeofday(&tv, 0); 319 expire_time = tv.tv_sec + 20 * 60; 320 } 321 else if (strncmp(argv[0], "pub", 3) == 0) { 322 flags |= RTF_ANNOUNCE; 323 doing_proxy = 1; 324 if (argc && strncmp(argv[1], "only", 3) == 0) { 325 proxy_only = 1; 326 dst->sin_other = SIN_PROXY; 327 argc--; argv++; 328 } 329 } else if (strncmp(argv[0], "trail", 5) == 0) { 330 /* XXX deprecated and undocumented feature */ 331 printf("%s: Sending trailers is no longer supported\n", 332 host); 333 } 334 argv++; 335 } 336 ea = (struct ether_addr *)LLADDR(&sdl_m); 337 if (doing_proxy && !strcmp(eaddr, "auto")) { 338 if (!get_ether_addr(dst->sin_addr.s_addr, ea)) { 339 printf("no interface found for %s\n", 340 inet_ntoa(dst->sin_addr)); 341 return (1); 342 } 343 sdl_m.sdl_alen = ETHER_ADDR_LEN; 344 } else { 345 struct ether_addr *ea1 = ether_aton(eaddr); 346 347 if (ea1 == NULL) 348 warnx("invalid Ethernet address '%s'", eaddr); 349 else { 350 *ea = *ea1; 351 sdl_m.sdl_alen = ETHER_ADDR_LEN; 352 } 353 } 354 for (;;) { /* try at most twice */ 355 rtm = rtmsg(RTM_GET, dst, &sdl_m); 356 if (rtm == NULL) { 357 warn("%s", host); 358 return (1); 359 } 360 addr = (struct sockaddr_inarp *)(rtm + 1); 361 sdl = (struct sockaddr_dl *)(SA_SIZE(addr) + (char *)addr); 362 if (addr->sin_addr.s_addr != dst->sin_addr.s_addr) 363 break; 364 if (sdl->sdl_family == AF_LINK && 365 (rtm->rtm_flags & RTF_LLINFO) && 366 !(rtm->rtm_flags & RTF_GATEWAY) && 367 valid_type(sdl->sdl_type) ) 368 break; 369 if (doing_proxy == 0) { 370 printf("set: can only proxy for %s\n", host); 371 return (1); 372 } 373 if (dst->sin_other & SIN_PROXY) { 374 printf("set: proxy entry exists for non 802 device\n"); 375 return(1); 376 } 377 dst->sin_other = SIN_PROXY; 378 proxy_only = 1; 379 } 380 381 if (sdl->sdl_family != AF_LINK) { 382 printf("cannot intuit interface index and type for %s\n", host); 383 return (1); 384 } 385 sdl_m.sdl_type = sdl->sdl_type; 386 sdl_m.sdl_index = sdl->sdl_index; 387 return (rtmsg(RTM_ADD, dst, &sdl_m) != NULL); 388 } 389 390 /* 391 * Display an individual arp entry 392 */ 393 static void 394 get(char *host) 395 { 396 struct sockaddr_inarp *addr; 397 398 addr = getaddr(host); 399 if (addr == NULL) 400 exit(1); 401 if (0 == search(addr->sin_addr.s_addr, print_entry)) { 402 printf("%s (%s) -- no entry", 403 host, inet_ntoa(addr->sin_addr)); 404 if (rifname) 405 printf(" on %s", rifname); 406 printf("\n"); 407 } 408 } 409 410 /* 411 * Delete an arp entry 412 */ 413 static int 414 delete(char *host, int do_proxy) 415 { 416 struct sockaddr_inarp *addr, *dst; 417 struct rt_msghdr *rtm; 418 struct sockaddr_dl *sdl; 419 420 dst = getaddr(host); 421 if (dst == NULL) 422 return 1; 423 dst->sin_other = do_proxy; 424 for (;;) { /* try twice */ 425 rtm = rtmsg(RTM_GET, dst, NULL); 426 if (rtm == NULL) { 427 warn("%s", host); 428 return (1); 429 } 430 addr = (struct sockaddr_inarp *)(rtm + 1); 431 sdl = (struct sockaddr_dl *)(SA_SIZE(addr) + (char *)addr); 432 if (addr->sin_addr.s_addr == dst->sin_addr.s_addr && 433 sdl->sdl_family == AF_LINK && 434 (rtm->rtm_flags & RTF_LLINFO) && 435 !(rtm->rtm_flags & RTF_GATEWAY) && 436 valid_type(sdl->sdl_type) ) 437 break; /* found it */ 438 if (dst->sin_other & SIN_PROXY) { 439 fprintf(stderr, "delete: cannot locate %s\n",host); 440 return (1); 441 } 442 dst->sin_other = SIN_PROXY; 443 } 444 if (rtmsg(RTM_DELETE, dst, NULL) != NULL) { 445 printf("%s (%s) deleted\n", host, inet_ntoa(addr->sin_addr)); 446 return (0); 447 } 448 return (1); 449 } 450 451 /* 452 * Search the arp table and do some action on matching entries 453 */ 454 static int 455 search(u_long addr, action_fn *action) 456 { 457 int mib[6]; 458 size_t needed; 459 char *lim, *buf, *next; 460 struct rt_msghdr *rtm; 461 struct sockaddr_inarp *sin2; 462 struct sockaddr_dl *sdl; 463 char ifname[IF_NAMESIZE]; 464 int found_entry = 0; 465 466 mib[0] = CTL_NET; 467 mib[1] = PF_ROUTE; 468 mib[2] = 0; 469 mib[3] = AF_INET; 470 mib[4] = NET_RT_FLAGS; 471 mib[5] = RTF_LLINFO; 472 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) 473 err(1, "route-sysctl-estimate"); 474 if (needed == 0) /* empty table */ 475 return 0; 476 if ((buf = malloc(needed)) == NULL) 477 err(1, "malloc"); 478 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) 479 err(1, "actual retrieval of routing table"); 480 lim = buf + needed; 481 for (next = buf; next < lim; next += rtm->rtm_msglen) { 482 rtm = (struct rt_msghdr *)next; 483 sin2 = (struct sockaddr_inarp *)(rtm + 1); 484 sdl = (struct sockaddr_dl *)((char *)sin2 + SA_SIZE(sin2)); 485 if (rifname && if_indextoname(sdl->sdl_index, ifname) && 486 strcmp(ifname, rifname)) 487 continue; 488 if (addr) { 489 if (addr != sin2->sin_addr.s_addr) 490 continue; 491 found_entry = 1; 492 } 493 (*action)(sdl, sin2, rtm); 494 } 495 free(buf); 496 return found_entry; 497 } 498 499 /* 500 * Display an arp entry 501 */ 502 static void 503 print_entry(struct sockaddr_dl *sdl, 504 struct sockaddr_inarp *addr, struct rt_msghdr *rtm) 505 { 506 const char *host; 507 struct hostent *hp; 508 struct iso88025_sockaddr_dl_data *trld; 509 char ifname[IF_NAMESIZE]; 510 int seg; 511 512 if (nflag == 0) 513 hp = gethostbyaddr((caddr_t)&(addr->sin_addr), 514 sizeof addr->sin_addr, AF_INET); 515 else 516 hp = 0; 517 if (hp) 518 host = hp->h_name; 519 else { 520 host = "?"; 521 if (h_errno == TRY_AGAIN) 522 nflag = 1; 523 } 524 printf("%s (%s) at ", host, inet_ntoa(addr->sin_addr)); 525 if (sdl->sdl_alen) { 526 if (sdl->sdl_type == IFT_ETHER && 527 sdl->sdl_alen == ETHER_ADDR_LEN) 528 printf("%s", ether_ntoa((struct ether_addr *)LLADDR(sdl))); 529 else { 530 int n = sdl->sdl_nlen > 0 ? sdl->sdl_nlen + 1 : 0; 531 532 printf("%s", link_ntoa(sdl) + n); 533 } 534 } else 535 printf("(incomplete)"); 536 if (if_indextoname(sdl->sdl_index, ifname) != NULL) 537 printf(" on %s", ifname); 538 if (rtm->rtm_rmx.rmx_expire == 0) 539 printf(" permanent"); 540 if (addr->sin_other & SIN_PROXY) 541 printf(" published (proxy only)"); 542 if (rtm->rtm_addrs & RTA_NETMASK) { 543 addr = (struct sockaddr_inarp *) 544 (SA_SIZE(sdl) + (char *)sdl); 545 if (addr->sin_addr.s_addr == 0xffffffff) 546 printf(" published"); 547 if (addr->sin_len != 8) 548 printf("(weird)"); 549 } 550 switch(sdl->sdl_type) { 551 case IFT_ETHER: 552 printf(" [ethernet]"); 553 break; 554 case IFT_ISO88025: 555 printf(" [token-ring]"); 556 trld = SDL_ISO88025(sdl); 557 if (trld->trld_rcf != 0) { 558 printf(" rt=%x", ntohs(trld->trld_rcf)); 559 for (seg = 0; 560 seg < ((TR_RCF_RIFLEN(trld->trld_rcf) - 2 ) / 2); 561 seg++) 562 printf(":%x", ntohs(*(trld->trld_route[seg]))); 563 } 564 break; 565 case IFT_FDDI: 566 printf(" [fddi]"); 567 break; 568 case IFT_ATM: 569 printf(" [atm]"); 570 break; 571 case IFT_L2VLAN: 572 printf(" [vlan]"); 573 break; 574 case IFT_IEEE1394: 575 printf(" [firewire]"); 576 break; 577 default: 578 break; 579 } 580 581 printf("\n"); 582 583 } 584 585 /* 586 * Nuke an arp entry 587 */ 588 static void 589 nuke_entry(struct sockaddr_dl *sdl __unused, 590 struct sockaddr_inarp *addr, struct rt_msghdr *rtm __unused) 591 { 592 char ip[20]; 593 594 snprintf(ip, sizeof(ip), "%s", inet_ntoa(addr->sin_addr)); 595 delete(ip, 0); 596 } 597 598 static void 599 usage(void) 600 { 601 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n", 602 "usage: arp [-n] [-i interface] hostname", 603 " arp [-n] [-i interface] -a", 604 " arp -d hostname [pub]", 605 " arp -d -a", 606 " arp -s hostname ether_addr [temp] [pub]", 607 " arp -S hostname ether_addr [temp] [pub]", 608 " arp -f filename"); 609 exit(1); 610 } 611 612 static struct rt_msghdr * 613 rtmsg(int cmd, struct sockaddr_inarp *dst, struct sockaddr_dl *sdl) 614 { 615 static int seq; 616 int rlen; 617 int l; 618 struct sockaddr_in so_mask; 619 static int s = -1; 620 static pid_t pid; 621 622 static struct { 623 struct rt_msghdr m_rtm; 624 char m_space[512]; 625 } m_rtmsg; 626 627 struct rt_msghdr *rtm = &m_rtmsg.m_rtm; 628 char *cp = m_rtmsg.m_space; 629 630 if (s < 0) { /* first time: open socket, get pid */ 631 s = socket(PF_ROUTE, SOCK_RAW, 0); 632 if (s < 0) 633 err(1, "socket"); 634 pid = getpid(); 635 } 636 bzero(&so_mask, sizeof(so_mask)); 637 so_mask.sin_len = 8; 638 so_mask.sin_addr.s_addr = 0xffffffff; 639 640 errno = 0; 641 /* 642 * XXX RTM_DELETE relies on a previous RTM_GET to fill the buffer 643 * appropriately. 644 */ 645 if (cmd == RTM_DELETE) 646 goto doit; 647 bzero((char *)&m_rtmsg, sizeof(m_rtmsg)); 648 rtm->rtm_flags = flags; 649 rtm->rtm_version = RTM_VERSION; 650 651 switch (cmd) { 652 default: 653 errx(1, "internal wrong cmd"); 654 case RTM_ADD: 655 rtm->rtm_addrs |= RTA_GATEWAY; 656 rtm->rtm_rmx.rmx_expire = expire_time; 657 rtm->rtm_inits = RTV_EXPIRE; 658 rtm->rtm_flags |= (RTF_HOST | RTF_STATIC); 659 dst->sin_other = 0; 660 if (doing_proxy) { 661 if (proxy_only) 662 dst->sin_other = SIN_PROXY; 663 else { 664 rtm->rtm_addrs |= RTA_NETMASK; 665 rtm->rtm_flags &= ~RTF_HOST; 666 } 667 } 668 /* FALLTHROUGH */ 669 case RTM_GET: 670 rtm->rtm_addrs |= RTA_DST; 671 } 672 #define NEXTADDR(w, s) \ 673 if (s && rtm->rtm_addrs & (w)) { \ 674 bcopy(s, cp, sizeof(*s)); cp += SA_SIZE(s);} 675 676 NEXTADDR(RTA_DST, dst); 677 NEXTADDR(RTA_GATEWAY, sdl); 678 NEXTADDR(RTA_NETMASK, &so_mask); 679 680 rtm->rtm_msglen = cp - (char *)&m_rtmsg; 681 doit: 682 l = rtm->rtm_msglen; 683 rtm->rtm_seq = ++seq; 684 rtm->rtm_type = cmd; 685 if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) { 686 if (errno != ESRCH || cmd != RTM_DELETE) { 687 warn("writing to routing socket"); 688 return NULL; 689 } 690 } 691 do { 692 l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg)); 693 } while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid)); 694 if (l < 0) 695 warn("read from routing socket"); 696 return rtm; 697 } 698 699 /* 700 * get_ether_addr - get the hardware address of an interface on the 701 * the same subnet as ipaddr. 702 */ 703 #define MAX_IFS 32 704 705 static int 706 get_ether_addr(u_int32_t ipaddr, struct ether_addr *hwaddr) 707 { 708 struct ifreq *ifr, *ifend, *ifp; 709 uint32_t ina, mask; 710 struct sockaddr_dl *dla; 711 struct ifreq ifreq; 712 struct ifconf ifc; 713 struct ifreq ifs[MAX_IFS]; 714 int sock; 715 int retval = 0; 716 717 sock = socket(AF_INET, SOCK_DGRAM, 0); 718 if (sock < 0) 719 err(1, "socket"); 720 721 ifc.ifc_len = sizeof(ifs); 722 ifc.ifc_req = ifs; 723 if (ioctl(sock, SIOCGIFCONF, &ifc) < 0) { 724 warnx("ioctl(SIOCGIFCONF)"); 725 goto done; 726 } 727 728 #define NEXTIFR(i) \ 729 ((struct ifreq *)((char *)&(i)->ifr_addr \ 730 + MAX((i)->ifr_addr.sa_len, sizeof((i)->ifr_addr))) ) 731 732 /* 733 * Scan through looking for an interface with an Internet 734 * address on the same subnet as `ipaddr'. 735 */ 736 ifend = (struct ifreq *)(ifc.ifc_buf + ifc.ifc_len); 737 for (ifr = ifc.ifc_req; ifr < ifend; ifr = NEXTIFR(ifr) ) { 738 if (ifr->ifr_addr.sa_family != AF_INET) 739 continue; 740 /* XXX can't we use *ifr instead of ifreq ? */ 741 strncpy(ifreq.ifr_name, ifr->ifr_name, 742 sizeof(ifreq.ifr_name)); 743 /* 744 * Check that the interface is up, 745 * and not point-to-point or loopback. 746 */ 747 if (ioctl(sock, SIOCGIFFLAGS, &ifreq) < 0) 748 continue; 749 if ((ifreq.ifr_flags & 750 (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT| 751 IFF_LOOPBACK|IFF_NOARP)) 752 != (IFF_UP|IFF_BROADCAST)) 753 continue; 754 /* 755 * Get its netmask and check that it's on 756 * the right subnet. 757 */ 758 if (ioctl(sock, SIOCGIFNETMASK, &ifreq) < 0) 759 continue; 760 mask = ((struct sockaddr_in *) 761 &ifreq.ifr_addr)->sin_addr.s_addr; 762 ina = ((struct sockaddr_in *) 763 &ifr->ifr_addr)->sin_addr.s_addr; 764 if ((ipaddr & mask) == (ina & mask)) 765 break; /* ok, we got it! */ 766 } 767 768 if (ifr >= ifend) 769 goto done; 770 771 /* 772 * Now scan through again looking for a link-level address 773 * for this interface. 774 */ 775 ifp = ifr; 776 for (ifr = ifc.ifc_req; ifr < ifend; ifr = NEXTIFR(ifr)) 777 if (strcmp(ifp->ifr_name, ifr->ifr_name) == 0 && 778 ifr->ifr_addr.sa_family == AF_LINK) 779 break; 780 if (ifr >= ifend) 781 goto done; 782 /* 783 * Found the link-level address - copy it out 784 */ 785 dla = (struct sockaddr_dl *) &ifr->ifr_addr; 786 memcpy(hwaddr, LLADDR(dla), dla->sdl_alen); 787 printf("using interface %s for proxy with address ", 788 ifp->ifr_name); 789 printf("%s\n", ether_ntoa(hwaddr)); 790 retval = dla->sdl_alen; 791 done: 792 close(sock); 793 return retval; 794 } 795