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