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