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