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