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