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_type == IFT_L2VLAN) && 528 sdl->sdl_alen == ETHER_ADDR_LEN) 529 printf("%s", ether_ntoa((struct ether_addr *)LLADDR(sdl))); 530 else { 531 int n = sdl->sdl_nlen > 0 ? sdl->sdl_nlen + 1 : 0; 532 533 printf("%s", link_ntoa(sdl) + n); 534 } 535 } else 536 printf("(incomplete)"); 537 if (if_indextoname(sdl->sdl_index, ifname) != NULL) 538 printf(" on %s", ifname); 539 if (rtm->rtm_rmx.rmx_expire == 0) 540 printf(" permanent"); 541 if (addr->sin_other & SIN_PROXY) 542 printf(" published (proxy only)"); 543 if (rtm->rtm_addrs & RTA_NETMASK) { 544 addr = (struct sockaddr_inarp *) 545 (SA_SIZE(sdl) + (char *)sdl); 546 if (addr->sin_addr.s_addr == 0xffffffff) 547 printf(" published"); 548 if (addr->sin_len != 8) 549 printf("(weird)"); 550 } 551 switch(sdl->sdl_type) { 552 case IFT_ETHER: 553 printf(" [ethernet]"); 554 break; 555 case IFT_ISO88025: 556 printf(" [token-ring]"); 557 trld = SDL_ISO88025(sdl); 558 if (trld->trld_rcf != 0) { 559 printf(" rt=%x", ntohs(trld->trld_rcf)); 560 for (seg = 0; 561 seg < ((TR_RCF_RIFLEN(trld->trld_rcf) - 2 ) / 2); 562 seg++) 563 printf(":%x", ntohs(*(trld->trld_route[seg]))); 564 } 565 break; 566 case IFT_FDDI: 567 printf(" [fddi]"); 568 break; 569 case IFT_ATM: 570 printf(" [atm]"); 571 break; 572 case IFT_L2VLAN: 573 printf(" [vlan]"); 574 break; 575 case IFT_IEEE1394: 576 printf(" [firewire]"); 577 break; 578 default: 579 break; 580 } 581 582 printf("\n"); 583 584 } 585 586 /* 587 * Nuke an arp entry 588 */ 589 static void 590 nuke_entry(struct sockaddr_dl *sdl __unused, 591 struct sockaddr_inarp *addr, struct rt_msghdr *rtm __unused) 592 { 593 char ip[20]; 594 595 snprintf(ip, sizeof(ip), "%s", inet_ntoa(addr->sin_addr)); 596 delete(ip, 0); 597 } 598 599 static void 600 usage(void) 601 { 602 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n", 603 "usage: arp [-n] [-i interface] hostname", 604 " arp [-n] [-i interface] -a", 605 " arp -d hostname [pub]", 606 " arp -d -a", 607 " arp -s hostname ether_addr [temp] [pub]", 608 " arp -S hostname ether_addr [temp] [pub]", 609 " arp -f filename"); 610 exit(1); 611 } 612 613 static struct rt_msghdr * 614 rtmsg(int cmd, struct sockaddr_inarp *dst, struct sockaddr_dl *sdl) 615 { 616 static int seq; 617 int rlen; 618 int l; 619 struct sockaddr_in so_mask; 620 static int s = -1; 621 static pid_t pid; 622 623 static struct { 624 struct rt_msghdr m_rtm; 625 char m_space[512]; 626 } m_rtmsg; 627 628 struct rt_msghdr *rtm = &m_rtmsg.m_rtm; 629 char *cp = m_rtmsg.m_space; 630 631 if (s < 0) { /* first time: open socket, get pid */ 632 s = socket(PF_ROUTE, SOCK_RAW, 0); 633 if (s < 0) 634 err(1, "socket"); 635 pid = getpid(); 636 } 637 bzero(&so_mask, sizeof(so_mask)); 638 so_mask.sin_len = 8; 639 so_mask.sin_addr.s_addr = 0xffffffff; 640 641 errno = 0; 642 /* 643 * XXX RTM_DELETE relies on a previous RTM_GET to fill the buffer 644 * appropriately. 645 */ 646 if (cmd == RTM_DELETE) 647 goto doit; 648 bzero((char *)&m_rtmsg, sizeof(m_rtmsg)); 649 rtm->rtm_flags = flags; 650 rtm->rtm_version = RTM_VERSION; 651 652 switch (cmd) { 653 default: 654 errx(1, "internal wrong cmd"); 655 case RTM_ADD: 656 rtm->rtm_addrs |= RTA_GATEWAY; 657 rtm->rtm_rmx.rmx_expire = expire_time; 658 rtm->rtm_inits = RTV_EXPIRE; 659 rtm->rtm_flags |= (RTF_HOST | RTF_STATIC); 660 dst->sin_other = 0; 661 if (doing_proxy) { 662 if (proxy_only) 663 dst->sin_other = SIN_PROXY; 664 else { 665 rtm->rtm_addrs |= RTA_NETMASK; 666 rtm->rtm_flags &= ~RTF_HOST; 667 } 668 } 669 /* FALLTHROUGH */ 670 case RTM_GET: 671 rtm->rtm_addrs |= RTA_DST; 672 } 673 #define NEXTADDR(w, s) \ 674 if (s && rtm->rtm_addrs & (w)) { \ 675 bcopy(s, cp, sizeof(*s)); cp += SA_SIZE(s);} 676 677 NEXTADDR(RTA_DST, dst); 678 NEXTADDR(RTA_GATEWAY, sdl); 679 NEXTADDR(RTA_NETMASK, &so_mask); 680 681 rtm->rtm_msglen = cp - (char *)&m_rtmsg; 682 doit: 683 l = rtm->rtm_msglen; 684 rtm->rtm_seq = ++seq; 685 rtm->rtm_type = cmd; 686 if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) { 687 if (errno != ESRCH || cmd != RTM_DELETE) { 688 warn("writing to routing socket"); 689 return NULL; 690 } 691 } 692 do { 693 l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg)); 694 } while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid)); 695 if (l < 0) 696 warn("read from routing socket"); 697 return rtm; 698 } 699 700 /* 701 * get_ether_addr - get the hardware address of an interface on the 702 * the same subnet as ipaddr. 703 */ 704 #define MAX_IFS 32 705 706 static int 707 get_ether_addr(u_int32_t ipaddr, struct ether_addr *hwaddr) 708 { 709 struct ifreq *ifr, *ifend, *ifp; 710 uint32_t ina, mask; 711 struct sockaddr_dl *dla; 712 struct ifreq ifreq; 713 struct ifconf ifc; 714 struct ifreq ifs[MAX_IFS]; 715 int sock; 716 int retval = 0; 717 718 sock = socket(AF_INET, SOCK_DGRAM, 0); 719 if (sock < 0) 720 err(1, "socket"); 721 722 ifc.ifc_len = sizeof(ifs); 723 ifc.ifc_req = ifs; 724 if (ioctl(sock, SIOCGIFCONF, &ifc) < 0) { 725 warnx("ioctl(SIOCGIFCONF)"); 726 goto done; 727 } 728 729 #define NEXTIFR(i) \ 730 ((struct ifreq *)((char *)&(i)->ifr_addr \ 731 + MAX((i)->ifr_addr.sa_len, sizeof((i)->ifr_addr))) ) 732 733 /* 734 * Scan through looking for an interface with an Internet 735 * address on the same subnet as `ipaddr'. 736 */ 737 ifend = (struct ifreq *)(ifc.ifc_buf + ifc.ifc_len); 738 for (ifr = ifc.ifc_req; ifr < ifend; ifr = NEXTIFR(ifr) ) { 739 if (ifr->ifr_addr.sa_family != AF_INET) 740 continue; 741 /* XXX can't we use *ifr instead of ifreq ? */ 742 strncpy(ifreq.ifr_name, ifr->ifr_name, 743 sizeof(ifreq.ifr_name)); 744 /* 745 * Check that the interface is up, 746 * and not point-to-point or loopback. 747 */ 748 if (ioctl(sock, SIOCGIFFLAGS, &ifreq) < 0) 749 continue; 750 if ((ifreq.ifr_flags & 751 (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT| 752 IFF_LOOPBACK|IFF_NOARP)) 753 != (IFF_UP|IFF_BROADCAST)) 754 continue; 755 /* 756 * Get its netmask and check that it's on 757 * the right subnet. 758 */ 759 if (ioctl(sock, SIOCGIFNETMASK, &ifreq) < 0) 760 continue; 761 mask = ((struct sockaddr_in *) 762 &ifreq.ifr_addr)->sin_addr.s_addr; 763 ina = ((struct sockaddr_in *) 764 &ifr->ifr_addr)->sin_addr.s_addr; 765 if ((ipaddr & mask) == (ina & mask)) 766 break; /* ok, we got it! */ 767 } 768 769 if (ifr >= ifend) 770 goto done; 771 772 /* 773 * Now scan through again looking for a link-level address 774 * for this interface. 775 */ 776 ifp = ifr; 777 for (ifr = ifc.ifc_req; ifr < ifend; ifr = NEXTIFR(ifr)) 778 if (strcmp(ifp->ifr_name, ifr->ifr_name) == 0 && 779 ifr->ifr_addr.sa_family == AF_LINK) 780 break; 781 if (ifr >= ifend) 782 goto done; 783 /* 784 * Found the link-level address - copy it out 785 */ 786 dla = (struct sockaddr_dl *) &ifr->ifr_addr; 787 memcpy(hwaddr, LLADDR(dla), dla->sdl_alen); 788 printf("using interface %s for proxy with address ", 789 ifp->ifr_name); 790 printf("%s\n", ether_ntoa(hwaddr)); 791 retval = dla->sdl_alen; 792 done: 793 close(sock); 794 return retval; 795 } 796