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