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