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