1 /* $NetBSD: arp.c,v 1.43 2006/09/23 21:11:14 dyoung Exp $ */ 2 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 #include <sys/cdefs.h> 36 #ifndef lint 37 __COPYRIGHT("@(#) Copyright (c) 1984, 1993\n\ 38 The Regents of the University of California. All rights reserved.\n"); 39 #endif /* not lint */ 40 41 #ifndef lint 42 #if 0 43 static char sccsid[] = "@(#)arp.c 8.3 (Berkeley) 4/28/95"; 44 #else 45 __RCSID("$NetBSD: arp.c,v 1.43 2006/09/23 21:11:14 dyoung Exp $"); 46 #endif 47 #endif /* not lint */ 48 49 /* 50 * arp - display, set, and delete arp table entries 51 */ 52 53 /* Roundup the same way rt_xaddrs does */ 54 #define ROUNDUP(a) \ 55 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long)) 56 57 #include <sys/param.h> 58 #include <sys/file.h> 59 #include <sys/socket.h> 60 #include <sys/sysctl.h> 61 #include <sys/ioctl.h> 62 63 #include <net/if.h> 64 #include <net/if_dl.h> 65 #include <net/if_ether.h> 66 #include <net/if_types.h> 67 #include <net/route.h> 68 #include <netinet/in.h> 69 #include <netinet/if_inarp.h> 70 #include <arpa/inet.h> 71 72 #include <err.h> 73 #include <errno.h> 74 #include <netdb.h> 75 #include <nlist.h> 76 #include <paths.h> 77 #include <stdio.h> 78 #include <stdlib.h> 79 #include <string.h> 80 #include <unistd.h> 81 #include <ifaddrs.h> 82 83 static int is_llinfo(const struct sockaddr_dl *, int); 84 int delete(const char *, const char *); 85 void dump(u_long); 86 void delete_all(void); 87 void sdl_print(const struct sockaddr_dl *); 88 int getifname(u_int16_t, char *, size_t); 89 int atosdl(const char *s, struct sockaddr_dl *sdl); 90 int file(char *); 91 void get(const char *); 92 int getinetaddr(const char *, struct in_addr *); 93 void getsocket(void); 94 int rtmsg(int); 95 int set(int, char **); 96 void usage(void); 97 98 static int pid; 99 static int aflag, nflag, vflag; 100 static int s = -1; 101 static struct ifaddrs* ifaddrs = NULL; 102 103 int 104 main(int argc, char **argv) 105 { 106 int ch; 107 int op = 0; 108 109 setprogname(argv[0]); 110 111 pid = getpid(); 112 113 while ((ch = getopt(argc, argv, "andsfv")) != -1) 114 switch((char)ch) { 115 case 'a': 116 aflag = 1; 117 break; 118 case 'd': 119 case 's': 120 case 'f': 121 if (op) 122 usage(); 123 op = ch; 124 break; 125 case 'n': 126 nflag = 1; 127 break; 128 case 'v': 129 vflag = 1; 130 break; 131 default: 132 usage(); 133 } 134 argc -= optind; 135 argv += optind; 136 137 if (!op && aflag) 138 op = 'a'; 139 140 switch((char)op) { 141 case 'a': 142 dump(0); 143 break; 144 case 'd': 145 if (aflag && argc == 0) 146 delete_all(); 147 else { 148 if (aflag || argc < 1 || argc > 2) 149 usage(); 150 (void)delete(argv[0], argv[1]); 151 } 152 break; 153 case 's': 154 if (argc < 2 || argc > 5) 155 usage(); 156 return (set(argc, argv) ? 1 : 0); 157 case 'f': 158 if (argc != 1) 159 usage(); 160 return (file(argv[0])); 161 default: 162 if (argc != 1) 163 usage(); 164 get(argv[0]); 165 break; 166 } 167 return (0); 168 } 169 170 /* 171 * Process a file to set standard arp entries 172 */ 173 int 174 file(char *name) 175 { 176 char line[100], arg[5][50], *args[5]; 177 int i, retval; 178 FILE *fp; 179 180 if ((fp = fopen(name, "r")) == NULL) 181 err(1, "cannot open %s", name); 182 args[0] = &arg[0][0]; 183 args[1] = &arg[1][0]; 184 args[2] = &arg[2][0]; 185 args[3] = &arg[3][0]; 186 args[4] = &arg[4][0]; 187 retval = 0; 188 while (fgets(line, 100, fp) != NULL) { 189 i = sscanf(line, "%49s %49s %49s %49s %49s", 190 arg[0], arg[1], arg[2], arg[3], arg[4]); 191 if (i < 2) { 192 warnx("bad line: %s", line); 193 retval = 1; 194 continue; 195 } 196 if (set(i, args)) 197 retval = 1; 198 } 199 fclose(fp); 200 return (retval); 201 } 202 203 void 204 getsocket(void) 205 { 206 if (s >= 0) 207 return; 208 s = socket(PF_ROUTE, SOCK_RAW, 0); 209 if (s < 0) 210 err(1, "socket"); 211 } 212 213 struct sockaddr_in so_mask = {8, 0, 0, { 0xffffffff}}; 214 struct sockaddr_inarp blank_sin = {sizeof(blank_sin), AF_INET }, sin_m; 215 struct sockaddr_dl blank_sdl = {sizeof(blank_sdl), AF_LINK }, sdl_m; 216 int expire_time, flags, export_only, doing_proxy, found_entry; 217 struct { 218 struct rt_msghdr m_rtm; 219 char m_space[512]; 220 } m_rtmsg; 221 222 /* 223 * Set an individual arp entry 224 */ 225 int 226 set(int argc, char **argv) 227 { 228 struct sockaddr_inarp *sina; 229 struct sockaddr_dl *sdl; 230 struct rt_msghdr *rtm; 231 char *host = argv[0], *eaddr; 232 int rval; 233 234 sina = &sin_m; 235 rtm = &(m_rtmsg.m_rtm); 236 eaddr = argv[1]; 237 238 getsocket(); 239 argc -= 2; 240 argv += 2; 241 sdl_m = blank_sdl; /* struct copy */ 242 sin_m = blank_sin; /* struct copy */ 243 if (getinetaddr(host, &sina->sin_addr) == -1) 244 return (1); 245 if (atosdl(eaddr, &sdl_m)) 246 warnx("invalid link-level address '%s'", eaddr); 247 doing_proxy = flags = export_only = expire_time = 0; 248 while (argc-- > 0) { 249 if (strncmp(argv[0], "temp", 4) == 0) { 250 struct timeval timev; 251 (void)gettimeofday(&timev, 0); 252 expire_time = timev.tv_sec + 20 * 60; 253 } 254 else if (strncmp(argv[0], "pub", 3) == 0) { 255 flags |= RTF_ANNOUNCE; 256 doing_proxy = SIN_PROXY; 257 } else if (strncmp(argv[0], "trail", 5) == 0) { 258 (void)printf( 259 "%s: Sending trailers is no longer supported\n", 260 host); 261 } 262 argv++; 263 } 264 tryagain: 265 if (rtmsg(RTM_GET) < 0) { 266 warn("%s", host); 267 return (1); 268 } 269 sina = (struct sockaddr_inarp *)(rtm + 1); 270 sdl = (struct sockaddr_dl *)(ROUNDUP(sina->sin_len) + (char *)sina); 271 if (sina->sin_addr.s_addr == sin_m.sin_addr.s_addr) { 272 if (is_llinfo(sdl, rtm->rtm_flags)) 273 goto overwrite; 274 if (doing_proxy == 0) { 275 (void)printf("set: can only proxy for %s\n", host); 276 return (1); 277 } 278 if (sin_m.sin_other & SIN_PROXY) { 279 (void)printf( 280 "set: proxy entry exists for non 802 device\n"); 281 return (1); 282 } 283 sin_m.sin_other = SIN_PROXY; 284 export_only = 1; 285 goto tryagain; 286 } 287 overwrite: 288 if (sdl->sdl_family != AF_LINK) { 289 (void)printf("cannot intuit interface index and type for %s\n", 290 host); 291 return (1); 292 } 293 sdl_m.sdl_type = sdl->sdl_type; 294 sdl_m.sdl_index = sdl->sdl_index; 295 rval = rtmsg(RTM_ADD); 296 if (vflag) 297 (void)printf("%s (%s) added\n", host, eaddr); 298 return (rval); 299 } 300 301 /* 302 * Display an individual arp entry 303 */ 304 void 305 get(const char *host) 306 { 307 struct sockaddr_inarp *sina; 308 309 sina = &sin_m; 310 sin_m = blank_sin; /* struct copy */ 311 if (getinetaddr(host, &sina->sin_addr) == -1) 312 exit(1); 313 dump(sina->sin_addr.s_addr); 314 if (found_entry == 0) { 315 (void)printf("%s (%s) -- no entry\n", host, 316 inet_ntoa(sina->sin_addr)); 317 exit(1); 318 } 319 } 320 321 322 static int 323 is_llinfo(const struct sockaddr_dl *sdl, int rtflags) 324 { 325 if (sdl->sdl_family != AF_LINK || 326 (rtflags & (RTF_LLINFO|RTF_GATEWAY)) != RTF_LLINFO) 327 return 0; 328 329 switch (sdl->sdl_type) { 330 case IFT_ETHER: 331 case IFT_FDDI: 332 case IFT_ISO88023: 333 case IFT_ISO88024: 334 case IFT_ISO88025: 335 case IFT_ARCNET: 336 return 1; 337 default: 338 return 0; 339 } 340 } 341 342 /* 343 * Delete an arp entry 344 */ 345 int 346 delete(const char *host, const char *info) 347 { 348 struct sockaddr_inarp *sina; 349 struct rt_msghdr *rtm; 350 struct sockaddr_dl *sdl; 351 352 sina = &sin_m; 353 rtm = &m_rtmsg.m_rtm; 354 355 if (info && strncmp(info, "pub", 3) == 0) 356 export_only = 1; 357 getsocket(); 358 sin_m = blank_sin; /* struct copy */ 359 if (getinetaddr(host, &sina->sin_addr) == -1) 360 return (1); 361 tryagain: 362 if (rtmsg(RTM_GET) < 0) { 363 warn("%s", host); 364 return (1); 365 } 366 sina = (struct sockaddr_inarp *)(rtm + 1); 367 sdl = (struct sockaddr_dl *)(ROUNDUP(sina->sin_len) + (char *)sina); 368 if (sina->sin_addr.s_addr == sin_m.sin_addr.s_addr && 369 is_llinfo(sdl, rtm->rtm_flags)) 370 goto delete; 371 if (sin_m.sin_other & SIN_PROXY) { 372 warnx("delete: can't locate %s", host); 373 return (1); 374 } else { 375 sin_m.sin_other = SIN_PROXY; 376 goto tryagain; 377 } 378 delete: 379 if (sdl->sdl_family != AF_LINK) { 380 (void)printf("cannot locate %s\n", host); 381 return (1); 382 } 383 if (rtmsg(RTM_DELETE)) 384 return (1); 385 if (vflag) 386 (void)printf("%s (%s) deleted\n", host, 387 inet_ntoa(sina->sin_addr)); 388 return (0); 389 } 390 391 /* 392 * Dump the entire arp table 393 */ 394 void 395 dump(u_long addr) 396 { 397 int mib[6]; 398 size_t needed; 399 char ifname[IFNAMSIZ]; 400 char *lim, *buf, *next; 401 const char *host; 402 struct rt_msghdr *rtm; 403 struct sockaddr_inarp *sina; 404 struct sockaddr_dl *sdl; 405 struct hostent *hp; 406 407 mib[0] = CTL_NET; 408 mib[1] = PF_ROUTE; 409 mib[2] = 0; 410 mib[3] = AF_INET; 411 mib[4] = NET_RT_FLAGS; 412 mib[5] = RTF_LLINFO; 413 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) 414 err(1, "route-sysctl-estimate"); 415 if (needed == 0) 416 return; 417 if ((buf = malloc(needed)) == NULL) 418 err(1, "malloc"); 419 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) 420 err(1, "actual retrieval of routing table"); 421 lim = buf + needed; 422 for (next = buf; next < lim; next += rtm->rtm_msglen) { 423 rtm = (struct rt_msghdr *)next; 424 sina = (struct sockaddr_inarp *)(rtm + 1); 425 sdl = (struct sockaddr_dl *) 426 (ROUNDUP(sina->sin_len) + (char *)sina); 427 if (addr) { 428 if (addr != sina->sin_addr.s_addr) 429 continue; 430 found_entry = 1; 431 } 432 if (nflag == 0) 433 hp = gethostbyaddr((caddr_t)&(sina->sin_addr), 434 sizeof sina->sin_addr, AF_INET); 435 else 436 hp = NULL; 437 438 host = hp ? hp->h_name : "?"; 439 440 (void)printf("%s (%s) at ", host, inet_ntoa(sina->sin_addr)); 441 if (sdl->sdl_alen) 442 sdl_print(sdl); 443 else 444 (void)printf("(incomplete)"); 445 446 if (sdl->sdl_index) { 447 if (getifname(sdl->sdl_index, ifname, sizeof(ifname)) == 0) 448 printf(" on %s", ifname); 449 } 450 451 if (rtm->rtm_rmx.rmx_expire == 0) 452 (void)printf(" permanent"); 453 if (sina->sin_other & SIN_PROXY) 454 (void)printf(" published (proxy only)"); 455 if (rtm->rtm_addrs & RTA_NETMASK) { 456 sina = (struct sockaddr_inarp *) 457 (ROUNDUP(sdl->sdl_len) + (char *)sdl); 458 if (sina->sin_addr.s_addr == 0xffffffff) 459 (void)printf(" published"); 460 if (sina->sin_len != 8) 461 (void)printf("(weird)"); 462 } 463 (void)printf("\n"); 464 } 465 free(buf); 466 } 467 468 /* 469 * Delete the entire arp table 470 */ 471 void 472 delete_all(void) 473 { 474 int mib[6]; 475 size_t needed; 476 char addr[sizeof("000.000.000.000\0")]; 477 char *lim, *buf, *next; 478 struct rt_msghdr *rtm; 479 struct sockaddr_inarp *sina; 480 struct sockaddr_dl *sdl; 481 482 mib[0] = CTL_NET; 483 mib[1] = PF_ROUTE; 484 mib[2] = 0; 485 mib[3] = AF_INET; 486 mib[4] = NET_RT_FLAGS; 487 mib[5] = RTF_LLINFO; 488 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) 489 err(1, "route-sysctl-estimate"); 490 if (needed == 0) 491 return; 492 if ((buf = malloc(needed)) == NULL) 493 err(1, "malloc"); 494 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) 495 err(1, "actual retrieval of routing table"); 496 lim = buf + needed; 497 for (next = buf; next < lim; next += rtm->rtm_msglen) { 498 rtm = (struct rt_msghdr *)next; 499 sina = (struct sockaddr_inarp *)(rtm + 1); 500 sdl = (struct sockaddr_dl *) 501 (ROUNDUP(sina->sin_len) + (char *)sina); 502 snprintf(addr, sizeof(addr), "%s", inet_ntoa(sina->sin_addr)); 503 delete(addr, NULL); 504 } 505 free(buf); 506 } 507 508 void 509 sdl_print(const struct sockaddr_dl *sdl) 510 { 511 char hbuf[NI_MAXHOST]; 512 513 if (getnameinfo((const struct sockaddr *)sdl, sdl->sdl_len, 514 hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0) 515 printf("<invalid>"); 516 else 517 printf("%s", hbuf); 518 } 519 520 int 521 atosdl(const char *ss, struct sockaddr_dl *sdl) 522 { 523 int i; 524 long b; 525 caddr_t endp; 526 caddr_t p; 527 char *t, *r; 528 529 p = LLADDR(sdl); 530 endp = ((caddr_t)sdl) + sdl->sdl_len; 531 i = 0; 532 533 b = strtol(ss, &t, 16); 534 if (t == ss) 535 return 1; 536 537 *p++ = b; 538 ++i; 539 while ((p < endp) && (*t++ == ':')) { 540 b = strtol(t, &r, 16); 541 if (r == t) 542 break; 543 *p++ = b; 544 ++i; 545 t = r; 546 } 547 sdl->sdl_alen = i; 548 549 return 0; 550 } 551 552 void 553 usage(void) 554 { 555 const char *progname; 556 557 progname = getprogname(); 558 (void)fprintf(stderr, "usage: %s [-n] hostname\n", progname); 559 (void)fprintf(stderr, "usage: %s [-nv] -a\n", progname); 560 (void)fprintf(stderr, "usage: %s [-v] -d [-a|hostname [pub]]\n", progname); 561 (void)fprintf(stderr, 562 "usage: %s -s hostname ether_addr [temp] [pub]\n", progname); 563 (void)fprintf(stderr, "usage: %s -f filename\n", progname); 564 exit(1); 565 } 566 567 int 568 rtmsg(int cmd) 569 { 570 static int seq; 571 int rlen; 572 struct rt_msghdr *rtm; 573 char *cp; 574 int l; 575 576 rtm = &m_rtmsg.m_rtm; 577 cp = m_rtmsg.m_space; 578 errno = 0; 579 580 if (cmd == RTM_DELETE) 581 goto doit; 582 (void)memset(&m_rtmsg, 0, sizeof(m_rtmsg)); 583 rtm->rtm_flags = flags; 584 rtm->rtm_version = RTM_VERSION; 585 586 switch (cmd) { 587 default: 588 errx(1, "internal wrong cmd"); 589 /*NOTREACHED*/ 590 case RTM_ADD: 591 rtm->rtm_addrs |= RTA_GATEWAY; 592 rtm->rtm_rmx.rmx_expire = expire_time; 593 rtm->rtm_inits = RTV_EXPIRE; 594 rtm->rtm_flags |= (RTF_HOST | RTF_STATIC); 595 sin_m.sin_other = 0; 596 if (doing_proxy) { 597 if (export_only) 598 sin_m.sin_other = SIN_PROXY; 599 else { 600 rtm->rtm_addrs |= RTA_NETMASK; 601 rtm->rtm_flags &= ~RTF_HOST; 602 } 603 } 604 /* FALLTHROUGH */ 605 case RTM_GET: 606 rtm->rtm_addrs |= RTA_DST; 607 } 608 609 #define NEXTADDR(w, s) \ 610 if (rtm->rtm_addrs & (w)) { \ 611 (void)memcpy(cp, &s, ((struct sockaddr *)&s)->sa_len); \ 612 cp += ROUNDUP(((struct sockaddr *)&s)->sa_len); \ 613 } 614 615 NEXTADDR(RTA_DST, sin_m); 616 NEXTADDR(RTA_GATEWAY, sdl_m); 617 NEXTADDR(RTA_NETMASK, so_mask); 618 619 rtm->rtm_msglen = cp - (char *)&m_rtmsg; 620 doit: 621 l = rtm->rtm_msglen; 622 rtm->rtm_seq = ++seq; 623 rtm->rtm_type = cmd; 624 if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) { 625 if (errno != ESRCH || cmd != RTM_DELETE) { 626 warn("writing to routing socket"); 627 return (-1); 628 } 629 } 630 do { 631 l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg)); 632 } while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid)); 633 if (l < 0) 634 warn("read from routing socket"); 635 return (0); 636 } 637 638 int 639 getinetaddr(const char *host, struct in_addr *inap) 640 { 641 struct hostent *hp; 642 643 if (inet_aton(host, inap) == 1) 644 return (0); 645 if ((hp = gethostbyname(host)) == NULL) { 646 warnx("%s: %s", host, hstrerror(h_errno)); 647 return (-1); 648 } 649 (void)memcpy(inap, hp->h_addr, sizeof(*inap)); 650 return (0); 651 } 652 653 int 654 getifname(u_int16_t ifindex, char *ifname, size_t l) 655 { 656 int i; 657 struct ifaddrs *addr; 658 const struct sockaddr_dl *sdl = NULL; 659 660 if (ifaddrs == NULL) { 661 i = getifaddrs(&ifaddrs); 662 if (i != 0) 663 err(1, "getifaddrs"); 664 } 665 666 for (addr = ifaddrs; addr; addr = addr->ifa_next) { 667 if (addr->ifa_addr == NULL || 668 addr->ifa_addr->sa_family != AF_LINK) 669 continue; 670 671 sdl = (const struct sockaddr_dl *) addr->ifa_addr; 672 if (sdl && sdl->sdl_index == ifindex) { 673 (void) strlcpy(ifname, addr->ifa_name, l); 674 return 0; 675 } 676 } 677 678 return -1; 679 } 680