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.7 2005/05/07 23:32:57 corecode 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 s = -1; 97 98 struct sockaddr_in so_mask; 99 struct sockaddr_inarp blank_sin, sin_m; 100 struct sockaddr_dl blank_sdl, sdl_m; 101 int expire_time, flags, doing_proxy, proxy_only, found_entry; 102 struct { 103 struct rt_msghdr m_rtm; 104 char m_space[512]; 105 } m_rtmsg; 106 107 /* which function we're supposed to do */ 108 #define F_GET 1 109 #define F_SET 2 110 #define F_FILESET 3 111 #define F_REPLACE 4 112 #define F_DELETE 5 113 114 #define ROUNDUP(a) \ 115 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long)) 116 #define SETFUNC(f) { if (func) usage(); func = (f); } 117 118 int 119 main(int argc, char **argv) 120 { 121 int ch, func = 0; 122 int rtn = 0; 123 124 pid = getpid(); 125 while ((ch = getopt(argc, argv, "andfsS")) != -1) 126 switch((char)ch) { 127 case 'a': 128 aflag = 1; 129 break; 130 case 'd': 131 SETFUNC(F_DELETE); 132 break; 133 case 'n': 134 nflag = 1; 135 break; 136 case 'S': 137 SETFUNC(F_REPLACE); 138 break; 139 case 's': 140 SETFUNC(F_SET); 141 break; 142 case 'f' : 143 SETFUNC(F_FILESET); 144 break; 145 case '?': 146 default: 147 usage(); 148 } 149 argc -= optind; 150 argv += optind; 151 152 bzero(&so_mask, sizeof(so_mask)); 153 so_mask.sin_len = 8; 154 so_mask.sin_addr.s_addr = 0xffffffff; 155 bzero(&blank_sin, sizeof(blank_sin)); 156 blank_sin.sin_len = sizeof(blank_sin); 157 blank_sin.sin_family = AF_INET; 158 bzero(&blank_sdl, sizeof(blank_sdl)); 159 blank_sdl.sdl_len = sizeof(blank_sdl); 160 blank_sdl.sdl_family = AF_LINK; 161 162 if (!func) 163 func = F_GET; 164 switch (func) { 165 case F_GET: 166 if (aflag) { 167 if (argc != 0) 168 usage(); 169 search(0, print_entry); 170 } else { 171 if (argc != 1) 172 usage(); 173 get(argv[0]); 174 } 175 break; 176 case F_SET: 177 case F_REPLACE: 178 if (argc < 2 || argc > 6) 179 usage(); 180 if (func == F_REPLACE) 181 delete(argv[0], NULL); 182 rtn = set(argc, argv) ? 1 : 0; 183 break; 184 case F_DELETE: 185 if (aflag) { 186 if (argc != 0) 187 usage(); 188 search(0, nuke_entry); 189 } else { 190 if (argc < 1 || argc > 2) 191 usage(); 192 rtn = delete(argv[0], argv[1]); 193 } 194 break; 195 case F_FILESET: 196 if (argc != 1) 197 usage(); 198 rtn = file(argv[0]); 199 break; 200 } 201 202 return(rtn); 203 } 204 205 /* 206 * Process a file to set standard arp entries 207 */ 208 int 209 file(char *name) 210 { 211 FILE *fp; 212 int i, retval; 213 char line[100], arg[5][50], *args[5], *p; 214 215 if ((fp = fopen(name, "r")) == NULL) 216 errx(1, "cannot open %s", name); 217 args[0] = &arg[0][0]; 218 args[1] = &arg[1][0]; 219 args[2] = &arg[2][0]; 220 args[3] = &arg[3][0]; 221 args[4] = &arg[4][0]; 222 retval = 0; 223 while(fgets(line, 100, fp) != NULL) { 224 if ((p = strchr(line, '#')) != NULL) 225 *p = '\0'; 226 for (p = line; isblank(*p); p++); 227 if (*p == '\n' || *p == '\0') 228 continue; 229 i = sscanf(p, "%49s %49s %49s %49s %49s", arg[0], arg[1], 230 arg[2], arg[3], arg[4]); 231 if (i < 2) { 232 warnx("bad line: %s", line); 233 retval = 1; 234 continue; 235 } 236 if (set(i, args)) 237 retval = 1; 238 } 239 fclose(fp); 240 return(retval); 241 } 242 243 void 244 getsocket(void) 245 { 246 if (s < 0) { 247 s = socket(PF_ROUTE, SOCK_RAW, 0); 248 if (s < 0) 249 err(1, "socket"); 250 } 251 } 252 253 /* 254 * Set an individual arp entry 255 */ 256 int 257 set(int argc, char **argv) 258 { 259 struct hostent *hp; 260 struct sockaddr_inarp *addr = &sin_m; 261 struct sockaddr_dl *sdl; 262 struct rt_msghdr *rtm = &(m_rtmsg.m_rtm); 263 struct ether_addr *ea; 264 char *host = argv[0], *eaddr = argv[1]; 265 266 getsocket(); 267 argc -= 2; 268 argv += 2; 269 sdl_m = blank_sdl; 270 sin_m = blank_sin; 271 addr->sin_addr.s_addr = inet_addr(host); 272 if (addr->sin_addr.s_addr == INADDR_NONE) { 273 if (!(hp = gethostbyname(host))) { 274 warnx("%s: %s", host, hstrerror(h_errno)); 275 return(1); 276 } 277 bcopy((char *)hp->h_addr, (char *)&addr->sin_addr, 278 sizeof(addr->sin_addr)); 279 } 280 doing_proxy = flags = proxy_only = expire_time = 0; 281 while (argc-- > 0) { 282 if (strncmp(argv[0], "temp", 4) == 0) { 283 struct timeval tv; 284 gettimeofday(&tv, 0); 285 expire_time = tv.tv_sec + 20 * 60; 286 } 287 else if (strncmp(argv[0], "pub", 3) == 0) { 288 flags |= RTF_ANNOUNCE; 289 doing_proxy = 1; 290 if (argc && strncmp(argv[1], "only", 3) == 0) { 291 proxy_only = 1; 292 sin_m.sin_other = SIN_PROXY; 293 argc--; argv++; 294 } 295 } else if (strncmp(argv[0], "trail", 5) == 0) { 296 printf("%s: Sending trailers is no longer supported\n", 297 host); 298 } 299 argv++; 300 } 301 ea = (struct ether_addr *)LLADDR(&sdl_m); 302 if (doing_proxy && !strcmp(eaddr, "auto")) { 303 if (!get_ether_addr(addr->sin_addr.s_addr, ea)) { 304 printf("no interface found for %s\n", 305 inet_ntoa(addr->sin_addr)); 306 return(1); 307 } 308 sdl_m.sdl_alen = ETHER_ADDR_LEN; 309 } else { 310 if (my_ether_aton(eaddr, ea) == 0) 311 sdl_m.sdl_alen = ETHER_ADDR_LEN; 312 } 313 tryagain: 314 if (rtmsg(RTM_GET) < 0) { 315 warn("%s", host); 316 return(1); 317 } 318 addr = (struct sockaddr_inarp *)(rtm + 1); 319 sdl = (struct sockaddr_dl *)(ROUNDUP(addr->sin_len) + (char *)addr); 320 if (addr->sin_addr.s_addr == sin_m.sin_addr.s_addr) { 321 if (sdl->sdl_family == AF_LINK && 322 (rtm->rtm_flags & RTF_LLINFO) && 323 !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) { 324 case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023: 325 case IFT_ISO88024: case IFT_ISO88025: case IFT_L2VLAN: 326 goto overwrite; 327 } 328 if (doing_proxy == 0) { 329 printf("set: can only proxy for %s\n", host); 330 return(1); 331 } 332 if (sin_m.sin_other & SIN_PROXY) { 333 printf("set: proxy entry exists for non 802 device\n"); 334 return(1); 335 } 336 sin_m.sin_other = SIN_PROXY; 337 proxy_only = 1; 338 goto tryagain; 339 } 340 overwrite: 341 if (sdl->sdl_family != AF_LINK) { 342 printf("cannot intuit interface index and type for %s\n", host); 343 return(1); 344 } 345 sdl_m.sdl_type = sdl->sdl_type; 346 sdl_m.sdl_index = sdl->sdl_index; 347 return(rtmsg(RTM_ADD)); 348 } 349 350 /* 351 * Display an individual arp entry 352 */ 353 int 354 get(char *host) 355 { 356 struct hostent *hp; 357 struct sockaddr_inarp *addr = &sin_m; 358 359 sin_m = blank_sin; 360 addr->sin_addr.s_addr = inet_addr(host); 361 if (addr->sin_addr.s_addr == INADDR_NONE) { 362 if (!(hp = gethostbyname(host))) 363 errx(1, "%s: %s", host, hstrerror(h_errno)); 364 bcopy((char *)hp->h_addr, (char *)&addr->sin_addr, 365 sizeof(addr->sin_addr)); 366 } 367 search(addr->sin_addr.s_addr, print_entry); 368 if (found_entry == 0) { 369 printf("%s (%s) -- no entry\n", 370 host, inet_ntoa(addr->sin_addr)); 371 return(1); 372 } 373 return(0); 374 } 375 376 /* 377 * Delete an arp entry 378 */ 379 int 380 delete(char *host, char *info) 381 { 382 struct hostent *hp; 383 struct sockaddr_inarp *addr = &sin_m; 384 struct rt_msghdr *rtm = &m_rtmsg.m_rtm; 385 struct sockaddr_dl *sdl; 386 387 getsocket(); 388 sin_m = blank_sin; 389 if (info) { 390 if (strncmp(info, "pub", 3) == 0) 391 sin_m.sin_other = SIN_PROXY; 392 else 393 usage(); 394 } 395 addr->sin_addr.s_addr = inet_addr(host); 396 if (addr->sin_addr.s_addr == INADDR_NONE) { 397 if (!(hp = gethostbyname(host))) { 398 warnx("%s: %s", host, hstrerror(h_errno)); 399 return(1); 400 } 401 bcopy((char *)hp->h_addr, (char *)&addr->sin_addr, 402 sizeof(addr->sin_addr)); 403 } 404 tryagain: 405 if (rtmsg(RTM_GET) < 0) { 406 warn("%s", host); 407 return(1); 408 } 409 addr = (struct sockaddr_inarp *)(rtm + 1); 410 sdl = (struct sockaddr_dl *)(ROUNDUP(addr->sin_len) + (char *)addr); 411 if (addr->sin_addr.s_addr == sin_m.sin_addr.s_addr) { 412 if (sdl->sdl_family == AF_LINK && 413 (rtm->rtm_flags & RTF_LLINFO) && 414 !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) { 415 case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023: 416 case IFT_ISO88024: case IFT_ISO88025: case IFT_L2VLAN: 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 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[6]; 458 size_t needed; 459 char *lim, *buf, *next; 460 struct rt_msghdr *rtm; 461 struct sockaddr_inarp *sin2; 462 struct sockaddr_dl *sdl; 463 464 mib[0] = CTL_NET; 465 mib[1] = PF_ROUTE; 466 mib[2] = 0; 467 mib[3] = AF_INET; 468 mib[4] = NET_RT_FLAGS; 469 mib[5] = RTF_LLINFO; 470 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) 471 errx(1, "route-sysctl-estimate"); 472 if ((buf = malloc(needed)) == NULL) 473 errx(1, "malloc"); 474 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) 475 errx(1, "actual retrieval of routing table"); 476 lim = buf + needed; 477 for (next = buf; next < lim; next += rtm->rtm_msglen) { 478 rtm = (struct rt_msghdr *)next; 479 sin2 = (struct sockaddr_inarp *)(rtm + 1); 480 sdl = (struct sockaddr_dl *)((char *)sin2 + ROUNDUP(sin2->sin_len)); 481 if (addr) { 482 if (addr != sin2->sin_addr.s_addr) 483 continue; 484 found_entry = 1; 485 } 486 (*action)(sdl, sin2, rtm); 487 } 488 free(buf); 489 } 490 491 /* 492 * Display an arp entry 493 */ 494 void 495 print_entry(struct sockaddr_dl *sdl, 496 struct sockaddr_inarp *addr, struct rt_msghdr *rtm) 497 { 498 const char *host; 499 struct hostent *hp; 500 struct iso88025_sockaddr_dl_data *trld; 501 char ifname[IF_NAMESIZE]; 502 int seg; 503 504 if (nflag == 0) 505 hp = gethostbyaddr((caddr_t)&(addr->sin_addr), 506 sizeof(addr->sin_addr), AF_INET); 507 else 508 hp = 0; 509 if (hp) 510 host = hp->h_name; 511 else { 512 host = "?"; 513 if (h_errno == TRY_AGAIN) 514 nflag = 1; 515 } 516 printf("%s (%s) at ", host, inet_ntoa(addr->sin_addr)); 517 if (sdl->sdl_alen) 518 printf("%s", ether_ntoa((struct ether_addr *)LLADDR(sdl))); 519 else 520 printf("(incomplete)"); 521 if (if_indextoname(sdl->sdl_index, ifname) != NULL) 522 printf(" on %s", ifname); 523 if (rtm->rtm_rmx.rmx_expire == 0) 524 printf(" permanent"); 525 if (addr->sin_other & SIN_PROXY) 526 printf(" published (proxy only)"); 527 if (rtm->rtm_addrs & RTA_NETMASK) { 528 addr = (struct sockaddr_inarp *) 529 (ROUNDUP(sdl->sdl_len) + (char *)sdl); 530 if (addr->sin_addr.s_addr == 0xffffffff) 531 printf(" published"); 532 if (addr->sin_len != 8) 533 printf("(weird)"); 534 } 535 switch(sdl->sdl_type) { 536 case IFT_ETHER: 537 printf(" [ethernet]"); 538 break; 539 case IFT_ISO88025: 540 printf(" [token-ring]"); 541 trld = SDL_ISO88025(sdl); 542 if (trld->trld_rcf != 0) { 543 printf(" rt=%x", ntohs(trld->trld_rcf)); 544 for (seg = 0; 545 seg < ((TR_RCF_RIFLEN(trld->trld_rcf) - 2 ) / 2); 546 seg++) 547 printf(":%x", ntohs(*(trld->trld_route[seg]))); 548 } 549 break; 550 case IFT_L2VLAN: 551 printf(" [vlan]"); 552 break; 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] hostname", 592 " arp [-n] -a", 593 " arp -d hostname [pub]", 594 " arp -d -a", 595 " arp -s hostname ether_addr [temp] [pub]", 596 " arp -S hostname ether_addr [temp] [pub]", 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