1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1984, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Sun Microsystems, Inc. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 #if 0 36 #ifndef lint 37 static char const copyright[] = 38 "@(#) Copyright (c) 1984, 1993\n\ 39 The Regents of the University of California. All rights reserved.\n"; 40 #endif /* not lint */ 41 42 #endif 43 #include <sys/cdefs.h> 44 /* 45 * arp - display, set, and delete arp table entries 46 */ 47 48 #include <sys/param.h> 49 #include <sys/file.h> 50 #include <sys/socket.h> 51 #include <sys/sockio.h> 52 #include <sys/sysctl.h> 53 #include <sys/ioctl.h> 54 #include <sys/time.h> 55 56 #include <net/if.h> 57 #include <net/if_dl.h> 58 #include <net/if_types.h> 59 #include <net/route.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 <stdbool.h> 73 #include <stdio.h> 74 #include <stdlib.h> 75 #include <string.h> 76 #include <strings.h> 77 #include <unistd.h> 78 #include <ifaddrs.h> 79 #include <libxo/xo.h> 80 #include "arp.h" 81 82 typedef void (action_fn)(struct sockaddr_dl *sdl, struct sockaddr_in *s_in, 83 struct rt_msghdr *rtm); 84 static void nuke_entries(uint32_t ifindex, struct in_addr addr); 85 static int print_entries(uint32_t ifindex, struct in_addr addr); 86 87 static int delete(char *host); 88 static void usage(void) __dead2; 89 static int set(int argc, char **argv); 90 static int get(char *host); 91 static int file(char *name); 92 static struct rt_msghdr *rtmsg(int cmd, 93 struct sockaddr_in *dst, struct sockaddr_dl *sdl); 94 static int get_ether_addr(in_addr_t ipaddr, struct ether_addr *hwaddr); 95 static int set_rtsock(struct sockaddr_in *dst, struct sockaddr_dl *sdl_m, 96 char *host); 97 98 struct if_nameindex *ifnameindex; 99 100 struct arp_opts opts = {}; 101 102 /* which function we're supposed to do */ 103 #define F_GET 1 104 #define F_SET 2 105 #define F_FILESET 3 106 #define F_REPLACE 4 107 #define F_DELETE 5 108 109 #define SETFUNC(f) { if (func) usage(); func = (f); } 110 111 #define ARP_XO_VERSION "1" 112 113 int 114 main(int argc, char *argv[]) 115 { 116 int ch, func = 0; 117 int rtn = 0; 118 119 argc = xo_parse_args(argc, argv); 120 if (argc < 0) 121 exit(1); 122 123 while ((ch = getopt(argc, argv, "andfsSi:")) != -1) 124 switch(ch) { 125 case 'a': 126 opts.aflag = true; 127 break; 128 case 'd': 129 SETFUNC(F_DELETE); 130 break; 131 case 'n': 132 opts.nflag = true; 133 break; 134 case 'S': 135 SETFUNC(F_REPLACE); 136 break; 137 case 's': 138 SETFUNC(F_SET); 139 break; 140 case 'f' : 141 SETFUNC(F_FILESET); 142 break; 143 case 'i': 144 opts.rifname = optarg; 145 break; 146 case '?': 147 default: 148 usage(); 149 } 150 argc -= optind; 151 argv += optind; 152 153 if (!func) 154 func = F_GET; 155 if (opts.rifname) { 156 if (func != F_GET && !(func == F_DELETE && opts.aflag)) 157 xo_errx(1, "-i not applicable to this operation"); 158 if ((opts.rifindex = if_nametoindex(opts.rifname)) == 0) { 159 if (errno == ENXIO) 160 xo_errx(1, "interface %s does not exist", 161 opts.rifname); 162 else 163 xo_err(1, "if_nametoindex(%s)", opts.rifname); 164 } 165 } 166 switch (func) { 167 case F_GET: 168 if (opts.aflag) { 169 if (argc != 0) 170 usage(); 171 172 xo_set_version(ARP_XO_VERSION); 173 xo_open_container("arp"); 174 xo_open_list("arp-cache"); 175 176 struct in_addr all_addrs = {}; 177 print_entries(opts.rifindex, all_addrs); 178 179 xo_close_list("arp-cache"); 180 xo_close_container("arp"); 181 xo_finish(); 182 } else { 183 if (argc != 1) 184 usage(); 185 rtn = get(argv[0]); 186 } 187 break; 188 case F_SET: 189 case F_REPLACE: 190 if (argc < 2 || argc > 6) 191 usage(); 192 if (func == F_REPLACE) 193 (void)delete(argv[0]); 194 rtn = set(argc, argv) ? 1 : 0; 195 break; 196 case F_DELETE: 197 if (opts.aflag) { 198 if (argc != 0) 199 usage(); 200 struct in_addr all_addrs = {}; 201 nuke_entries(0, all_addrs); 202 } else { 203 if (argc != 1) 204 usage(); 205 rtn = delete(argv[0]); 206 } 207 break; 208 case F_FILESET: 209 if (argc != 1) 210 usage(); 211 rtn = file(argv[0]); 212 break; 213 } 214 215 if (ifnameindex != NULL) 216 if_freenameindex(ifnameindex); 217 218 return (rtn); 219 } 220 221 /* 222 * Process a file to set standard arp entries 223 */ 224 static int 225 file(char *name) 226 { 227 FILE *fp; 228 int i, retval; 229 char line[100], arg[5][50], *args[5], *p; 230 231 if ((fp = fopen(name, "r")) == NULL) 232 xo_err(1, "cannot open %s", name); 233 args[0] = &arg[0][0]; 234 args[1] = &arg[1][0]; 235 args[2] = &arg[2][0]; 236 args[3] = &arg[3][0]; 237 args[4] = &arg[4][0]; 238 retval = 0; 239 while(fgets(line, sizeof(line), fp) != NULL) { 240 if ((p = strchr(line, '#')) != NULL) 241 *p = '\0'; 242 for (p = line; isblank(*p); p++); 243 if (*p == '\n' || *p == '\0') 244 continue; 245 i = sscanf(p, "%49s %49s %49s %49s %49s", arg[0], arg[1], 246 arg[2], arg[3], arg[4]); 247 if (i < 2) { 248 xo_warnx("bad line: %s", line); 249 retval = 1; 250 continue; 251 } 252 if (set(i, args)) 253 retval = 1; 254 } 255 fclose(fp); 256 return (retval); 257 } 258 259 /* 260 * Given a hostname, fills up a (static) struct sockaddr_in with 261 * the address of the host and returns a pointer to the 262 * structure. 263 */ 264 struct sockaddr_in * 265 getaddr(char *host) 266 { 267 struct hostent *hp; 268 static struct sockaddr_in reply; 269 270 bzero(&reply, sizeof(reply)); 271 reply.sin_len = sizeof(reply); 272 reply.sin_family = AF_INET; 273 reply.sin_addr.s_addr = inet_addr(host); 274 if (reply.sin_addr.s_addr == INADDR_NONE) { 275 if (!(hp = gethostbyname(host))) { 276 xo_warnx("%s: %s", host, hstrerror(h_errno)); 277 return (NULL); 278 } 279 bcopy((char *)hp->h_addr, (char *)&reply.sin_addr, 280 sizeof reply.sin_addr); 281 } 282 return (&reply); 283 } 284 285 int valid_type(int type); 286 /* 287 * Returns true if the type is a valid one for ARP. 288 */ 289 int 290 valid_type(int type) 291 { 292 293 switch (type) { 294 case IFT_ETHER: 295 case IFT_FDDI: 296 case IFT_IEEE1394: 297 case IFT_INFINIBAND: 298 case IFT_ISO88023: 299 case IFT_ISO88024: 300 case IFT_L2VLAN: 301 case IFT_BRIDGE: 302 return (1); 303 default: 304 return (0); 305 } 306 } 307 308 /* 309 * Set an individual arp entry 310 */ 311 static int 312 set(int argc, char **argv) 313 { 314 struct sockaddr_in *dst; /* what are we looking for */ 315 struct ether_addr *ea; 316 char *host = argv[0], *eaddr = argv[1]; 317 struct sockaddr_dl sdl_m; 318 319 argc -= 2; 320 argv += 2; 321 322 bzero(&sdl_m, sizeof(sdl_m)); 323 sdl_m.sdl_len = sizeof(sdl_m); 324 sdl_m.sdl_family = AF_LINK; 325 326 dst = getaddr(host); 327 if (dst == NULL) 328 return (1); 329 while (argc-- > 0) { 330 if (strcmp(argv[0], "temp") == 0) { 331 int max_age; 332 size_t len = sizeof(max_age); 333 334 if (sysctlbyname("net.link.ether.inet.max_age", 335 &max_age, &len, NULL, 0) != 0) 336 xo_err(1, "sysctlbyname"); 337 opts.expire_time = max_age; 338 } else if (strcmp(argv[0], "pub") == 0) { 339 opts.flags |= RTF_ANNOUNCE; 340 if (argc && strcmp(argv[1], "only") == 0) { 341 /* 342 * Compatibility: in pre FreeBSD 8 times 343 * the "only" keyword used to mean that 344 * an ARP entry should be announced, but 345 * not installed into routing table. 346 */ 347 argc--; argv++; 348 } 349 } else if (strcmp(argv[0], "blackhole") == 0) { 350 if (opts.flags & RTF_REJECT) { 351 xo_errx(1, "Choose one of blackhole or reject, " 352 "not both."); 353 } 354 opts.flags |= RTF_BLACKHOLE; 355 } else if (strcmp(argv[0], "reject") == 0) { 356 if (opts.flags & RTF_BLACKHOLE) { 357 xo_errx(1, "Choose one of blackhole or reject, " 358 "not both."); 359 } 360 opts.flags |= RTF_REJECT; 361 } else { 362 xo_warnx("Invalid parameter '%s'", argv[0]); 363 usage(); 364 } 365 argv++; 366 } 367 ea = (struct ether_addr *)LLADDR(&sdl_m); 368 if ((opts.flags & RTF_ANNOUNCE) && !strcmp(eaddr, "auto")) { 369 if (!get_ether_addr(dst->sin_addr.s_addr, ea)) { 370 xo_warnx("no interface found for %s", 371 inet_ntoa(dst->sin_addr)); 372 return (1); 373 } 374 sdl_m.sdl_alen = ETHER_ADDR_LEN; 375 } else { 376 struct ether_addr *ea1 = ether_aton(eaddr); 377 378 if (ea1 == NULL) { 379 xo_warnx("invalid Ethernet address '%s'", eaddr); 380 return (1); 381 } else { 382 *ea = *ea1; 383 sdl_m.sdl_alen = ETHER_ADDR_LEN; 384 } 385 } 386 #ifndef WITHOUT_NETLINK 387 return (set_nl(0, dst, &sdl_m, host)); 388 #else 389 return (set_rtsock(dst, &sdl_m, host)); 390 #endif 391 } 392 393 #ifdef WITHOUT_NETLINK 394 static int 395 set_rtsock(struct sockaddr_in *dst, struct sockaddr_dl *sdl_m, char *host) 396 { 397 struct sockaddr_in *addr; 398 struct sockaddr_dl *sdl; 399 struct rt_msghdr *rtm; 400 401 /* 402 * In the case a proxy-arp entry is being added for 403 * a remote end point, the RTF_ANNOUNCE flag in the 404 * RTM_GET command is an indication to the kernel 405 * routing code that the interface associated with 406 * the prefix route covering the local end of the 407 * PPP link should be returned, on which ARP applies. 408 */ 409 rtm = rtmsg(RTM_GET, dst, NULL); 410 if (rtm == NULL) { 411 xo_warn("%s", host); 412 return (1); 413 } 414 addr = (struct sockaddr_in *)(rtm + 1); 415 sdl = (struct sockaddr_dl *)(SA_SIZE(addr) + (char *)addr); 416 417 if ((sdl->sdl_family != AF_LINK) || 418 (rtm->rtm_flags & RTF_GATEWAY) || 419 !valid_type(sdl->sdl_type)) { 420 xo_warnx("cannot intuit interface index and type for %s", host); 421 return (1); 422 } 423 sdl_m->sdl_type = sdl->sdl_type; 424 sdl_m->sdl_index = sdl->sdl_index; 425 return (rtmsg(RTM_ADD, dst, sdl_m) == NULL); 426 } 427 #endif 428 429 /* 430 * Display an individual arp entry 431 */ 432 static int 433 get(char *host) 434 { 435 struct sockaddr_in *addr; 436 int found; 437 438 addr = getaddr(host); 439 if (addr == NULL) 440 return (1); 441 442 xo_set_version(ARP_XO_VERSION); 443 xo_open_container("arp"); 444 xo_open_list("arp-cache"); 445 446 found = print_entries(opts.rifindex, addr->sin_addr); 447 448 if (found == 0) { 449 xo_emit("{d:hostname/%s} ({d:ip-address/%s}) -- no entry", 450 host, inet_ntoa(addr->sin_addr)); 451 if (opts.rifname) 452 xo_emit(" on {d:interface/%s}", opts.rifname); 453 xo_emit("\n"); 454 } 455 456 xo_close_list("arp-cache"); 457 xo_close_container("arp"); 458 xo_finish(); 459 460 return (found == 0); 461 } 462 463 /* 464 * Delete an arp entry 465 */ 466 #ifdef WITHOUT_NETLINK 467 static int 468 delete_rtsock(char *host) 469 { 470 struct sockaddr_in *addr, *dst; 471 struct rt_msghdr *rtm; 472 struct sockaddr_dl *sdl; 473 474 dst = getaddr(host); 475 if (dst == NULL) 476 return (1); 477 478 /* 479 * Perform a regular entry delete first. 480 */ 481 opts.flags &= ~RTF_ANNOUNCE; 482 483 for (;;) { /* try twice */ 484 rtm = rtmsg(RTM_GET, dst, NULL); 485 if (rtm == NULL) { 486 xo_warn("%s", host); 487 return (1); 488 } 489 addr = (struct sockaddr_in *)(rtm + 1); 490 sdl = (struct sockaddr_dl *)(SA_SIZE(addr) + (char *)addr); 491 492 /* 493 * With the new L2/L3 restructure, the route 494 * returned is a prefix route. The important 495 * piece of information from the previous 496 * RTM_GET is the interface index. In the 497 * case of ECMP, the kernel will traverse 498 * the route group for the given entry. 499 */ 500 if (sdl->sdl_family == AF_LINK && 501 !(rtm->rtm_flags & RTF_GATEWAY) && 502 valid_type(sdl->sdl_type) ) { 503 addr->sin_addr.s_addr = dst->sin_addr.s_addr; 504 break; 505 } 506 507 /* 508 * Regular entry delete failed, now check if there 509 * is a proxy-arp entry to remove. 510 */ 511 if (opts.flags & RTF_ANNOUNCE) { 512 xo_warnx("delete: cannot locate %s", host); 513 return (1); 514 } 515 516 opts.flags |= RTF_ANNOUNCE; 517 } 518 rtm->rtm_flags |= RTF_LLDATA; 519 if (rtmsg(RTM_DELETE, dst, NULL) != NULL) { 520 printf("%s (%s) deleted\n", host, inet_ntoa(addr->sin_addr)); 521 return (0); 522 } 523 return (1); 524 } 525 #endif 526 527 static int 528 delete(char *host) 529 { 530 #ifdef WITHOUT_NETLINK 531 return (delete_rtsock(host)); 532 #else 533 return (delete_nl(0, host)); 534 #endif 535 } 536 537 538 /* 539 * Search the arp table and do some action on matching entries 540 */ 541 static int 542 search(u_long addr, action_fn *action) 543 { 544 int mib[6]; 545 size_t needed; 546 char *lim, *buf, *next; 547 struct rt_msghdr *rtm; 548 struct sockaddr_in *sin2; 549 struct sockaddr_dl *sdl; 550 int st, found_entry = 0; 551 552 mib[0] = CTL_NET; 553 mib[1] = PF_ROUTE; 554 mib[2] = 0; 555 mib[3] = AF_INET; 556 mib[4] = NET_RT_FLAGS; 557 #ifdef RTF_LLINFO 558 mib[5] = RTF_LLINFO; 559 #else 560 mib[5] = 0; 561 #endif 562 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) 563 xo_err(1, "route-sysctl-estimate"); 564 if (needed == 0) /* empty table */ 565 return 0; 566 buf = NULL; 567 for (;;) { 568 buf = reallocf(buf, needed); 569 if (buf == NULL) 570 xo_errx(1, "could not reallocate memory"); 571 st = sysctl(mib, 6, buf, &needed, NULL, 0); 572 if (st == 0 || errno != ENOMEM) 573 break; 574 needed += needed / 8; 575 } 576 if (st == -1) 577 xo_err(1, "actual retrieval of routing table"); 578 lim = buf + needed; 579 for (next = buf; next < lim; next += rtm->rtm_msglen) { 580 rtm = (struct rt_msghdr *)next; 581 sin2 = (struct sockaddr_in *)(rtm + 1); 582 sdl = (struct sockaddr_dl *)((char *)sin2 + SA_SIZE(sin2)); 583 if (opts.rifindex && 584 (opts.rifindex != sdl->sdl_index)) 585 continue; 586 if (addr && 587 (addr != sin2->sin_addr.s_addr)) 588 continue; 589 found_entry = 1; 590 (*action)(sdl, sin2, rtm); 591 } 592 free(buf); 593 return (found_entry); 594 } 595 596 /* 597 * Display an arp entry 598 */ 599 600 static void 601 print_entry(struct sockaddr_dl *sdl, 602 struct sockaddr_in *addr, struct rt_msghdr *rtm) 603 { 604 const char *host; 605 struct hostent *hp; 606 struct if_nameindex *p; 607 608 if (ifnameindex == NULL) 609 if ((ifnameindex = if_nameindex()) == NULL) 610 xo_err(1, "cannot retrieve interface names"); 611 612 xo_open_instance("arp-cache"); 613 614 if (!opts.nflag) 615 hp = gethostbyaddr((caddr_t)&(addr->sin_addr), 616 sizeof addr->sin_addr, AF_INET); 617 else 618 hp = 0; 619 if (hp) 620 host = hp->h_name; 621 else { 622 host = "?"; 623 if (h_errno == TRY_AGAIN) 624 opts.nflag = true; 625 } 626 xo_emit("{:hostname/%s} ({:ip-address/%s}) at ", host, 627 inet_ntoa(addr->sin_addr)); 628 if (sdl->sdl_alen) { 629 if ((sdl->sdl_type == IFT_ETHER || 630 sdl->sdl_type == IFT_L2VLAN || 631 sdl->sdl_type == IFT_BRIDGE) && 632 sdl->sdl_alen == ETHER_ADDR_LEN) 633 xo_emit("{:mac-address/%s}", 634 ether_ntoa((struct ether_addr *)LLADDR(sdl))); 635 else { 636 int n = sdl->sdl_nlen > 0 ? sdl->sdl_nlen + 1 : 0; 637 638 xo_emit("{:mac-address/%s}", link_ntoa(sdl) + n); 639 } 640 } else 641 xo_emit("{d:/(incomplete)}{en:incomplete/true}"); 642 643 for (p = ifnameindex; p && p->if_index && p->if_name; p++) { 644 if (p->if_index == sdl->sdl_index) { 645 xo_emit(" on {:interface/%s}", p->if_name); 646 break; 647 } 648 } 649 650 if (rtm->rtm_rmx.rmx_expire == 0) 651 xo_emit("{d:/ permanent}{en:permanent/true}"); 652 else { 653 static struct timespec tp; 654 time_t expire_time = 0; 655 656 if (tp.tv_sec == 0) 657 clock_gettime(CLOCK_MONOTONIC, &tp); 658 if ((expire_time = rtm->rtm_rmx.rmx_expire - tp.tv_sec) > 0) 659 xo_emit(" expires in {:expires/%d} seconds", 660 (int)expire_time); 661 else 662 xo_emit("{d:/ expired}{en:expired/true}"); 663 } 664 665 if (rtm->rtm_flags & RTF_ANNOUNCE) 666 xo_emit("{d:/ published}{en:published/true}"); 667 668 switch(sdl->sdl_type) { 669 case IFT_ETHER: 670 xo_emit(" [{:type/ethernet}]"); 671 break; 672 case IFT_FDDI: 673 xo_emit(" [{:type/fddi}]"); 674 break; 675 case IFT_ATM: 676 xo_emit(" [{:type/atm}]"); 677 break; 678 case IFT_L2VLAN: 679 xo_emit(" [{:type/vlan}]"); 680 break; 681 case IFT_IEEE1394: 682 xo_emit(" [{:type/firewire}]"); 683 break; 684 case IFT_BRIDGE: 685 xo_emit(" [{:type/bridge}]"); 686 break; 687 case IFT_INFINIBAND: 688 xo_emit(" [{:type/infiniband}]"); 689 break; 690 default: 691 break; 692 } 693 694 xo_emit("\n"); 695 696 xo_close_instance("arp-cache"); 697 } 698 699 static int 700 print_entries(uint32_t ifindex, struct in_addr addr) 701 { 702 #ifndef WITHOUT_NETLINK 703 return (print_entries_nl(ifindex, addr)); 704 #else 705 return (search(addr.s_addr, print_entry)); 706 #endif 707 } 708 709 710 /* 711 * Nuke an arp entry 712 */ 713 static void 714 nuke_entry(struct sockaddr_dl *sdl __unused, 715 struct sockaddr_in *addr, struct rt_msghdr *rtm) 716 { 717 char ip[20]; 718 719 if (rtm->rtm_flags & RTF_PINNED) 720 return; 721 722 snprintf(ip, sizeof(ip), "%s", inet_ntoa(addr->sin_addr)); 723 delete(ip); 724 } 725 726 static void 727 nuke_entries(uint32_t ifindex, struct in_addr addr) 728 { 729 search(addr.s_addr, nuke_entry); 730 } 731 732 static void 733 usage(void) 734 { 735 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n", 736 "usage: arp [-n] [-i interface] hostname", 737 " arp [-n] [-i interface] -a", 738 " arp -d hostname [pub]", 739 " arp -d [-i interface] -a", 740 " arp -s hostname ether_addr [temp] [reject | blackhole] [pub [only]]", 741 " arp -S hostname ether_addr [temp] [reject | blackhole] [pub [only]]", 742 " arp -f filename"); 743 exit(1); 744 } 745 746 static struct rt_msghdr * 747 rtmsg(int cmd, struct sockaddr_in *dst, struct sockaddr_dl *sdl) 748 { 749 static int seq; 750 int rlen; 751 int l; 752 static int s = -1; 753 static pid_t pid; 754 755 static struct { 756 struct rt_msghdr m_rtm; 757 char m_space[512]; 758 } m_rtmsg; 759 760 struct rt_msghdr *rtm = &m_rtmsg.m_rtm; 761 char *cp = m_rtmsg.m_space; 762 763 if (s < 0) { /* first time: open socket, get pid */ 764 s = socket(PF_ROUTE, SOCK_RAW, 0); 765 if (s < 0) 766 xo_err(1, "socket"); 767 pid = getpid(); 768 } 769 770 errno = 0; 771 /* 772 * XXX RTM_DELETE relies on a previous RTM_GET to fill the buffer 773 * appropriately. 774 */ 775 if (cmd == RTM_DELETE) 776 goto doit; 777 bzero((char *)&m_rtmsg, sizeof(m_rtmsg)); 778 rtm->rtm_flags = opts.flags; 779 rtm->rtm_version = RTM_VERSION; 780 781 switch (cmd) { 782 default: 783 xo_errx(1, "internal wrong cmd"); 784 case RTM_ADD: 785 rtm->rtm_addrs |= RTA_GATEWAY; 786 if (opts.expire_time != 0) { 787 struct timespec tp; 788 789 clock_gettime(CLOCK_MONOTONIC, &tp); 790 rtm->rtm_rmx.rmx_expire = opts.expire_time + tp.tv_sec; 791 } 792 rtm->rtm_inits = RTV_EXPIRE; 793 rtm->rtm_flags |= (RTF_HOST | RTF_STATIC | RTF_LLDATA); 794 /* FALLTHROUGH */ 795 case RTM_GET: 796 rtm->rtm_addrs |= RTA_DST; 797 } 798 #define NEXTADDR(w, s) \ 799 do { \ 800 if ((s) != NULL && rtm->rtm_addrs & (w)) { \ 801 bcopy((s), cp, sizeof(*(s))); \ 802 cp += SA_SIZE(s); \ 803 } \ 804 } while (0) 805 806 NEXTADDR(RTA_DST, dst); 807 NEXTADDR(RTA_GATEWAY, sdl); 808 809 rtm->rtm_msglen = cp - (char *)&m_rtmsg; 810 doit: 811 l = rtm->rtm_msglen; 812 rtm->rtm_seq = ++seq; 813 rtm->rtm_type = cmd; 814 if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) { 815 if (errno != ESRCH || cmd != RTM_DELETE) { 816 xo_warn("writing to routing socket"); 817 return (NULL); 818 } 819 } 820 do { 821 l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg)); 822 } while (l > 0 && (rtm->rtm_type != cmd || rtm->rtm_seq != seq || 823 rtm->rtm_pid != pid)); 824 if (l < 0) 825 xo_warn("read from routing socket"); 826 return (rtm); 827 } 828 829 /* 830 * get_ether_addr - get the hardware address of an interface on the 831 * the same subnet as ipaddr. 832 */ 833 static int 834 get_ether_addr(in_addr_t ipaddr, struct ether_addr *hwaddr) 835 { 836 struct ifaddrs *ifa, *ifd, *ifas = NULL; 837 in_addr_t ina, mask; 838 struct sockaddr_dl *dla; 839 int retval = 0; 840 841 /* 842 * Scan through looking for an interface with an Internet 843 * address on the same subnet as `ipaddr'. 844 */ 845 if (getifaddrs(&ifas) < 0) { 846 xo_warnx("getifaddrs"); 847 goto done; 848 } 849 850 for (ifa = ifas; ifa != NULL; ifa = ifa->ifa_next) { 851 if (ifa->ifa_addr == NULL || ifa->ifa_netmask == NULL) 852 continue; 853 if (ifa->ifa_addr->sa_family != AF_INET) 854 continue; 855 /* 856 * Check that the interface is up, 857 * and not point-to-point or loopback. 858 */ 859 if ((ifa->ifa_flags & 860 (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT| 861 IFF_LOOPBACK|IFF_NOARP)) != (IFF_UP|IFF_BROADCAST)) 862 continue; 863 /* Get its netmask and check that it's on the right subnet. */ 864 mask = ((struct sockaddr_in *) 865 ifa->ifa_netmask)->sin_addr.s_addr; 866 ina = ((struct sockaddr_in *) 867 ifa->ifa_addr)->sin_addr.s_addr; 868 if ((ipaddr & mask) == (ina & mask)) 869 break; /* ok, we got it! */ 870 } 871 if (ifa == NULL) 872 goto done; 873 874 /* 875 * Now scan through again looking for a link-level address 876 * for this interface. 877 */ 878 for (ifd = ifas; ifd != NULL; ifd = ifd->ifa_next) { 879 if (ifd->ifa_addr == NULL) 880 continue; 881 if (strcmp(ifa->ifa_name, ifd->ifa_name) == 0 && 882 ifd->ifa_addr->sa_family == AF_LINK) 883 break; 884 } 885 if (ifd == NULL) 886 goto done; 887 /* 888 * Found the link-level address - copy it out 889 */ 890 dla = (struct sockaddr_dl *)ifd->ifa_addr; 891 memcpy(hwaddr, LLADDR(dla), dla->sdl_alen); 892 printf("using interface %s for proxy with address %s\n", ifa->ifa_name, 893 ether_ntoa(hwaddr)); 894 retval = dla->sdl_alen; 895 done: 896 if (ifas != NULL) 897 freeifaddrs(ifas); 898 return (retval); 899 } 900