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