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