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 * @(#) Copyright (c) 1984, 1993 The Regents of the University of California. All rights reserved. 37 * @(#)from: arp.c 8.2 (Berkeley) 1/2/94 38 * $FreeBSD: src/usr.sbin/arp/arp.c,v 1.22.2.12 2003/04/16 10:02:37 ru Exp $ 39 */ 40 41 /* 42 * arp - display, set, and delete arp table entries 43 */ 44 45 46 #include <sys/param.h> 47 #include <sys/file.h> 48 #include <sys/socket.h> 49 #include <sys/sockio.h> 50 #include <sys/sysctl.h> 51 #include <sys/ioctl.h> 52 #include <sys/time.h> 53 54 #include <net/if.h> 55 #include <net/if_dl.h> 56 #include <net/if_types.h> 57 #include <net/route.h> 58 59 #include <netinet/in.h> 60 #include <netinet/if_ether.h> 61 62 #include <arpa/inet.h> 63 64 #include <ctype.h> 65 #include <err.h> 66 #include <errno.h> 67 #include <netdb.h> 68 #include <nlist.h> 69 #include <paths.h> 70 #include <stdio.h> 71 #include <stdlib.h> 72 #include <string.h> 73 #include <unistd.h> 74 75 void search(u_long addr, void (*action)(struct sockaddr_dl *sdl, 76 struct sockaddr_inarp *sin, struct rt_msghdr *rtm)); 77 void print_entry(struct sockaddr_dl *sdl, 78 struct sockaddr_inarp *addr, struct rt_msghdr *rtm); 79 void nuke_entry(struct sockaddr_dl *sdl, 80 struct sockaddr_inarp *addr, struct rt_msghdr *rtm); 81 int delete(char *host, char *info); 82 void usage(void); 83 int set(int argc, char **argv); 84 int get(char *host); 85 int file(char *name); 86 void getsocket(void); 87 int my_ether_aton(char *a, struct ether_addr *n); 88 int rtmsg(int cmd); 89 int get_ether_addr(u_int32_t ipaddr, struct ether_addr *hwaddr); 90 91 static int pid; 92 static int nflag; /* no reverse dns lookups */ 93 static int aflag; /* do it for all entries */ 94 static int cpuflag = -1; 95 static int s = -1; 96 97 struct sockaddr_in so_mask; 98 struct sockaddr_inarp blank_sin, sin_m; 99 struct sockaddr_dl blank_sdl, sdl_m; 100 int expire_time, flags, doing_proxy, proxy_only, found_entry; 101 struct { 102 struct rt_msghdr m_rtm; 103 char m_space[512]; 104 } m_rtmsg; 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(int argc, char **argv) 119 { 120 int ch, func = 0; 121 int rtn = 0; 122 123 pid = getpid(); 124 while ((ch = getopt(argc, argv, "ac:ndfsS")) != -1) 125 switch((char)ch) { 126 case 'a': 127 aflag = 1; 128 break; 129 case 'c': 130 cpuflag = strtol(optarg, NULL, 0); 131 break; 132 case 'd': 133 SETFUNC(F_DELETE); 134 break; 135 case 'n': 136 nflag = 1; 137 break; 138 case 'S': 139 SETFUNC(F_REPLACE); 140 break; 141 case 's': 142 SETFUNC(F_SET); 143 break; 144 case 'f' : 145 SETFUNC(F_FILESET); 146 break; 147 case '?': 148 default: 149 usage(); 150 } 151 argc -= optind; 152 argv += optind; 153 154 bzero(&so_mask, sizeof(so_mask)); 155 so_mask.sin_len = 8; 156 so_mask.sin_addr.s_addr = 0xffffffff; 157 bzero(&blank_sin, sizeof(blank_sin)); 158 blank_sin.sin_len = sizeof(blank_sin); 159 blank_sin.sin_family = AF_INET; 160 bzero(&blank_sdl, sizeof(blank_sdl)); 161 blank_sdl.sdl_len = sizeof(blank_sdl); 162 blank_sdl.sdl_family = AF_LINK; 163 164 if (!func) 165 func = F_GET; 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], NULL); 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 < 1 || argc > 2) 193 usage(); 194 rtn = delete(argv[0], argv[1]); 195 } 196 break; 197 case F_FILESET: 198 if (argc != 1) 199 usage(); 200 rtn = file(argv[0]); 201 break; 202 } 203 204 return(rtn); 205 } 206 207 /* 208 * Process a file to set standard arp entries 209 */ 210 int 211 file(char *name) 212 { 213 FILE *fp; 214 int i, retval; 215 char line[100], arg[5][50], *args[5], *p; 216 217 if ((fp = fopen(name, "r")) == NULL) 218 errx(1, "cannot open %s", name); 219 args[0] = &arg[0][0]; 220 args[1] = &arg[1][0]; 221 args[2] = &arg[2][0]; 222 args[3] = &arg[3][0]; 223 args[4] = &arg[4][0]; 224 retval = 0; 225 while(fgets(line, 100, fp) != NULL) { 226 if ((p = strchr(line, '#')) != NULL) 227 *p = '\0'; 228 for (p = line; isblank(*p); p++); 229 if (*p == '\n' || *p == '\0') 230 continue; 231 i = sscanf(p, "%49s %49s %49s %49s %49s", arg[0], arg[1], 232 arg[2], arg[3], arg[4]); 233 if (i < 2) { 234 warnx("bad line: %s", line); 235 retval = 1; 236 continue; 237 } 238 if (set(i, args)) 239 retval = 1; 240 } 241 fclose(fp); 242 return(retval); 243 } 244 245 void 246 getsocket(void) 247 { 248 if (s < 0) { 249 s = socket(PF_ROUTE, SOCK_RAW, 0); 250 if (s < 0) 251 err(1, "socket"); 252 } 253 } 254 255 /* 256 * Set an individual arp entry 257 */ 258 int 259 set(int argc, char **argv) 260 { 261 struct hostent *hp; 262 struct sockaddr_inarp *addr = &sin_m; 263 struct sockaddr_dl *sdl; 264 struct rt_msghdr *rtm = &(m_rtmsg.m_rtm); 265 struct ether_addr *ea; 266 char *host = argv[0], *eaddr = argv[1]; 267 268 getsocket(); 269 argc -= 2; 270 argv += 2; 271 sdl_m = blank_sdl; 272 sin_m = blank_sin; 273 addr->sin_addr.s_addr = inet_addr(host); 274 if (addr->sin_addr.s_addr == INADDR_NONE) { 275 if (!(hp = gethostbyname(host))) { 276 warnx("%s: %s", host, hstrerror(h_errno)); 277 return(1); 278 } 279 bcopy((char *)hp->h_addr, (char *)&addr->sin_addr, 280 sizeof(addr->sin_addr)); 281 } 282 doing_proxy = flags = proxy_only = expire_time = 0; 283 while (argc-- > 0) { 284 if (strncmp(argv[0], "temp", 4) == 0) { 285 struct timeval tv; 286 gettimeofday(&tv, 0); 287 expire_time = tv.tv_sec + 20 * 60; 288 } 289 else if (strncmp(argv[0], "pub", 3) == 0) { 290 flags |= RTF_ANNOUNCE; 291 doing_proxy = 1; 292 if (argc && strncmp(argv[1], "only", 3) == 0) { 293 proxy_only = 1; 294 sin_m.sin_other = SIN_PROXY; 295 argc--; argv++; 296 } 297 } else if (strncmp(argv[0], "trail", 5) == 0) { 298 printf("%s: Sending trailers is no longer supported\n", 299 host); 300 } 301 argv++; 302 } 303 ea = (struct ether_addr *)LLADDR(&sdl_m); 304 if (doing_proxy && !strcmp(eaddr, "auto")) { 305 if (!get_ether_addr(addr->sin_addr.s_addr, ea)) { 306 printf("no interface found for %s\n", 307 inet_ntoa(addr->sin_addr)); 308 return(1); 309 } 310 sdl_m.sdl_alen = ETHER_ADDR_LEN; 311 } else { 312 if (my_ether_aton(eaddr, ea) == 0) 313 sdl_m.sdl_alen = ETHER_ADDR_LEN; 314 } 315 tryagain: 316 if (rtmsg(RTM_GET) < 0) { 317 warn("%s", host); 318 return(1); 319 } 320 addr = (struct sockaddr_inarp *)(rtm + 1); 321 sdl = (struct sockaddr_dl *)(ROUNDUP(addr->sin_len) + (char *)addr); 322 if (addr->sin_addr.s_addr == sin_m.sin_addr.s_addr) { 323 if (sdl->sdl_family == AF_LINK && 324 (rtm->rtm_flags & RTF_LLINFO) && 325 !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) { 326 case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023: 327 case IFT_ISO88024: case IFT_ISO88025: case IFT_L2VLAN: 328 case IFT_CARP: 329 goto overwrite; 330 } 331 if (doing_proxy == 0) { 332 printf("set: can only proxy for %s\n", host); 333 return(1); 334 } 335 if (sin_m.sin_other & SIN_PROXY) { 336 printf("set: proxy entry exists for non 802 device\n"); 337 return(1); 338 } 339 sin_m.sin_other = SIN_PROXY; 340 proxy_only = 1; 341 goto tryagain; 342 } 343 overwrite: 344 if (sdl->sdl_family != AF_LINK) { 345 printf("cannot intuit interface index and type for %s\n", host); 346 return(1); 347 } 348 sdl_m.sdl_type = sdl->sdl_type; 349 sdl_m.sdl_index = sdl->sdl_index; 350 return(rtmsg(RTM_ADD)); 351 } 352 353 /* 354 * Display an individual arp entry 355 */ 356 int 357 get(char *host) 358 { 359 struct hostent *hp; 360 struct sockaddr_inarp *addr = &sin_m; 361 362 sin_m = blank_sin; 363 addr->sin_addr.s_addr = inet_addr(host); 364 if (addr->sin_addr.s_addr == INADDR_NONE) { 365 if (!(hp = gethostbyname(host))) 366 errx(1, "%s: %s", host, hstrerror(h_errno)); 367 bcopy((char *)hp->h_addr, (char *)&addr->sin_addr, 368 sizeof(addr->sin_addr)); 369 } 370 search(addr->sin_addr.s_addr, print_entry); 371 if (found_entry == 0) { 372 printf("%s (%s) -- no entry\n", 373 host, inet_ntoa(addr->sin_addr)); 374 return(1); 375 } 376 return(0); 377 } 378 379 /* 380 * Delete an arp entry 381 */ 382 int 383 delete(char *host, char *info) 384 { 385 struct hostent *hp; 386 struct sockaddr_inarp *addr = &sin_m; 387 struct rt_msghdr *rtm = &m_rtmsg.m_rtm; 388 struct sockaddr_dl *sdl; 389 390 getsocket(); 391 sin_m = blank_sin; 392 if (info) { 393 if (strncmp(info, "pub", 3) == 0) 394 sin_m.sin_other = SIN_PROXY; 395 else 396 usage(); 397 } 398 addr->sin_addr.s_addr = inet_addr(host); 399 if (addr->sin_addr.s_addr == INADDR_NONE) { 400 if (!(hp = gethostbyname(host))) { 401 warnx("%s: %s", host, hstrerror(h_errno)); 402 return(1); 403 } 404 bcopy((char *)hp->h_addr, (char *)&addr->sin_addr, 405 sizeof(addr->sin_addr)); 406 } 407 tryagain: 408 if (rtmsg(RTM_GET) < 0) { 409 warn("%s", host); 410 return(1); 411 } 412 addr = (struct sockaddr_inarp *)(rtm + 1); 413 sdl = (struct sockaddr_dl *)(ROUNDUP(addr->sin_len) + (char *)addr); 414 if (addr->sin_addr.s_addr == sin_m.sin_addr.s_addr) { 415 if (sdl->sdl_family == AF_LINK && 416 (rtm->rtm_flags & RTF_LLINFO) && 417 !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) { 418 case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023: 419 case IFT_ISO88024: case IFT_ISO88025: case IFT_L2VLAN: 420 case IFT_CARP: 421 goto delete; 422 } 423 } 424 if (sin_m.sin_other & SIN_PROXY) { 425 fprintf(stderr, "delete: can't locate %s\n",host); 426 return(1); 427 } else { 428 sin_m.sin_other = SIN_PROXY; 429 goto tryagain; 430 } 431 delete: 432 if (sdl->sdl_family != AF_LINK) { 433 printf("cannot locate %s\n", host); 434 return(1); 435 } 436 if (aflag && (rtm->rtm_flags & RTF_STATIC)) { 437 sdl->sdl_alen = 0; 438 sdl->sdl_slen = 0; 439 if (rtmsg(RTM_CHANGE) == 0) { 440 printf("%s (%s) cleared\n", 441 host, inet_ntoa(addr->sin_addr)); 442 return(0); 443 } 444 } else { 445 if (rtmsg(RTM_DELETE) == 0) { 446 printf("%s (%s) deleted\n", 447 host, inet_ntoa(addr->sin_addr)); 448 return(0); 449 } 450 } 451 return(1); 452 } 453 454 /* 455 * Search the arp table and do some action on matching entries 456 */ 457 void 458 search(u_long addr, void (*action)(struct sockaddr_dl *sdl, 459 struct sockaddr_inarp *sin, struct rt_msghdr *rtm)) 460 { 461 int mib[7]; 462 int miblen; 463 size_t needed; 464 char *lim, *buf, *next; 465 struct rt_msghdr *rtm; 466 struct sockaddr_inarp *sin2; 467 struct sockaddr_dl *sdl; 468 469 mib[0] = CTL_NET; 470 mib[1] = PF_ROUTE; 471 mib[2] = 0; 472 mib[3] = AF_INET; 473 mib[4] = NET_RT_FLAGS; 474 mib[5] = RTF_LLINFO; 475 if (cpuflag >= 0) { 476 mib[6] = cpuflag; 477 miblen = 7; 478 } else { 479 miblen = 6; 480 } 481 if (sysctl(mib, miblen, NULL, &needed, NULL, 0) < 0) 482 errx(1, "route-sysctl-estimate"); 483 if ((buf = malloc(needed)) == NULL) 484 errx(1, "malloc"); 485 if (sysctl(mib, miblen, buf, &needed, NULL, 0) < 0) 486 errx(1, "actual retrieval of routing table"); 487 lim = buf + needed; 488 for (next = buf; next < lim; next += rtm->rtm_msglen) { 489 rtm = (struct rt_msghdr *)next; 490 sin2 = (struct sockaddr_inarp *)(rtm + 1); 491 sdl = (struct sockaddr_dl *)((char *)sin2 + ROUNDUP(sin2->sin_len)); 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 } 501 502 /* 503 * Display an arp entry 504 */ 505 void 506 print_entry(struct sockaddr_dl *sdl, 507 struct sockaddr_inarp *addr, struct rt_msghdr *rtm) 508 { 509 const char *host; 510 struct hostent *hp; 511 char ifname[IF_NAMESIZE]; 512 513 if (nflag == 0) 514 hp = gethostbyaddr(&(addr->sin_addr), sizeof(addr->sin_addr), 515 AF_INET); 516 else 517 hp = NULL; 518 if (hp) 519 host = hp->h_name; 520 else { 521 host = "?"; 522 if (h_errno == TRY_AGAIN) 523 nflag = 1; 524 } 525 printf("%s (%s) at ", host, inet_ntoa(addr->sin_addr)); 526 if (sdl->sdl_alen) 527 printf("%s", ether_ntoa((struct ether_addr *)LLADDR(sdl))); 528 else 529 printf("(incomplete)"); 530 if (if_indextoname(sdl->sdl_index, ifname) != NULL) 531 printf(" on %s", ifname); 532 if (rtm->rtm_rmx.rmx_expire == 0) 533 printf(" permanent"); 534 if (addr->sin_other & SIN_PROXY) 535 printf(" published (proxy only)"); 536 if (rtm->rtm_addrs & RTA_NETMASK) { 537 addr = (struct sockaddr_inarp *) 538 (ROUNDUP(sdl->sdl_len) + (char *)sdl); 539 if (addr->sin_addr.s_addr == 0xffffffff) 540 printf(" published"); 541 if (addr->sin_len != 8) 542 printf("(weird)"); 543 } 544 switch(sdl->sdl_type) { 545 case IFT_ETHER: 546 printf(" [ethernet]"); 547 break; 548 case IFT_L2VLAN: 549 printf(" [vlan]"); 550 break; 551 case IFT_CARP: 552 printf(" [carp]"); 553 default: 554 break; 555 } 556 557 printf("\n"); 558 559 } 560 561 /* 562 * Nuke an arp entry 563 */ 564 void 565 nuke_entry(struct sockaddr_dl *sdl __unused, 566 struct sockaddr_inarp *addr, struct rt_msghdr *rtm __unused) 567 { 568 char ip[20]; 569 570 snprintf(ip, sizeof(ip), "%s", inet_ntoa(addr->sin_addr)); 571 delete(ip, NULL); 572 } 573 574 int 575 my_ether_aton(char *a, struct ether_addr *n) 576 { 577 struct ether_addr *ea; 578 579 if ((ea = ether_aton(a)) == NULL) { 580 warnx("invalid Ethernet address '%s'", a); 581 return(1); 582 } 583 *n = *ea; 584 return(0); 585 } 586 587 void 588 usage(void) 589 { 590 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n", 591 "usage: arp [-n] [-c cpu] hostname", 592 " arp [-n] [-c cpu] -a", 593 " arp -d hostname [pub]", 594 " arp -d -a", 595 " arp -s hostname ether_addr [temp] [pub [only]]", 596 " arp -S hostname ether_addr [temp] [pub [only]]", 597 " arp -f filename"); 598 exit(1); 599 } 600 601 int 602 rtmsg(int cmd) 603 { 604 static int seq; 605 int rlen; 606 struct rt_msghdr *rtm = &m_rtmsg.m_rtm; 607 char *cp = m_rtmsg.m_space; 608 int l; 609 610 errno = 0; 611 if (cmd == RTM_DELETE || cmd == RTM_CHANGE) 612 goto doit; 613 bzero((char *)&m_rtmsg, sizeof(m_rtmsg)); 614 rtm->rtm_flags = flags; 615 rtm->rtm_version = RTM_VERSION; 616 617 switch (cmd) { 618 default: 619 errx(1, "internal wrong cmd"); 620 case RTM_ADD: 621 rtm->rtm_addrs |= RTA_GATEWAY; 622 rtm->rtm_rmx.rmx_expire = expire_time; 623 rtm->rtm_inits = RTV_EXPIRE; 624 rtm->rtm_flags |= (RTF_HOST | RTF_STATIC); 625 sin_m.sin_other = 0; 626 if (doing_proxy) { 627 if (proxy_only) 628 sin_m.sin_other = SIN_PROXY; 629 else { 630 rtm->rtm_addrs |= RTA_NETMASK; 631 rtm->rtm_flags &= ~RTF_HOST; 632 } 633 } 634 /* FALLTHROUGH */ 635 case RTM_GET: 636 rtm->rtm_addrs |= RTA_DST; 637 } 638 #define NEXTADDR(w, s) \ 639 if (rtm->rtm_addrs & (w)) { \ 640 bcopy((char *)&s, cp, sizeof(s)); cp += ROUNDUP(sizeof(s));} 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 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