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