1 /* 2 * Copyright (c) 1984, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Sun Microsystems, Inc. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37 #ifndef lint 38 static char copyright[] = 39 "@(#) Copyright (c) 1984, 1993\n\ 40 The Regents of the University of California. All rights reserved.\n"; 41 #endif /* not lint */ 42 43 #ifndef lint 44 static char sccsid[] = "@(#)arp.c 8.2 (Berkeley) 1/2/94"; 45 #endif /* not lint */ 46 47 /* 48 * arp - display, set, and delete arp table entries 49 */ 50 51 52 #include <sys/param.h> 53 #include <sys/file.h> 54 #include <sys/socket.h> 55 #include <sys/sysctl.h> 56 57 #include <net/if.h> 58 #include <net/if_dl.h> 59 #include <net/if_types.h> 60 #include <net/route.h> 61 62 #include <netinet/in.h> 63 #include <netinet/if_ether.h> 64 65 #include <arpa/inet.h> 66 67 #include <netdb.h> 68 #include <errno.h> 69 #include <nlist.h> 70 #include <stdio.h> 71 #include <paths.h> 72 73 extern int errno; 74 static int pid; 75 static int kflag; 76 static int nflag; 77 static int s = -1; 78 79 main(argc, argv) 80 int argc; 81 char **argv; 82 { 83 int ch; 84 85 pid = getpid(); 86 while ((ch = getopt(argc, argv, "ands")) != EOF) 87 switch((char)ch) { 88 case 'a': 89 dump(0); 90 exit(0); 91 case 'd': 92 if (argc < 3 || argc > 4) 93 usage(); 94 delete(argv[2], argv[3]); 95 exit(0); 96 case 'n': 97 nflag = 1; 98 continue; 99 case 's': 100 if (argc < 4 || argc > 7) 101 usage(); 102 exit(set(argc-2, &argv[2]) ? 1 : 0); 103 case '?': 104 default: 105 usage(); 106 } 107 if (argc != 2) 108 usage(); 109 get(argv[1]); 110 exit(0); 111 } 112 113 /* 114 * Process a file to set standard arp entries 115 */ 116 file(name) 117 char *name; 118 { 119 FILE *fp; 120 int i, retval; 121 char line[100], arg[5][50], *args[5]; 122 123 if ((fp = fopen(name, "r")) == NULL) { 124 fprintf(stderr, "arp: cannot open %s\n", name); 125 exit(1); 126 } 127 args[0] = &arg[0][0]; 128 args[1] = &arg[1][0]; 129 args[2] = &arg[2][0]; 130 args[3] = &arg[3][0]; 131 args[4] = &arg[4][0]; 132 retval = 0; 133 while(fgets(line, 100, fp) != NULL) { 134 i = sscanf(line, "%s %s %s %s %s", arg[0], arg[1], arg[2], 135 arg[3], arg[4]); 136 if (i < 2) { 137 fprintf(stderr, "arp: bad line: %s\n", line); 138 retval = 1; 139 continue; 140 } 141 if (set(i, args)) 142 retval = 1; 143 } 144 fclose(fp); 145 return (retval); 146 } 147 148 getsocket() { 149 if (s < 0) { 150 s = socket(PF_ROUTE, SOCK_RAW, 0); 151 if (s < 0) { 152 perror("arp: socket"); 153 exit(1); 154 } 155 } 156 } 157 158 struct sockaddr_in so_mask = {8, 0, 0, { 0xffffffff}}; 159 struct sockaddr_inarp blank_sin = {sizeof(blank_sin), AF_INET }, sin_m; 160 struct sockaddr_dl blank_sdl = {sizeof(blank_sdl), AF_LINK }, sdl_m; 161 int expire_time, flags, export_only, doing_proxy, found_entry; 162 struct { 163 struct rt_msghdr m_rtm; 164 char m_space[512]; 165 } m_rtmsg; 166 167 /* 168 * Set an individual arp entry 169 */ 170 set(argc, argv) 171 int argc; 172 char **argv; 173 { 174 struct hostent *hp; 175 register struct sockaddr_inarp *sin = &sin_m; 176 register struct sockaddr_dl *sdl; 177 register struct rt_msghdr *rtm = &(m_rtmsg.m_rtm); 178 u_char *ea; 179 char *host = argv[0], *eaddr = argv[1]; 180 181 getsocket(); 182 argc -= 2; 183 argv += 2; 184 sdl_m = blank_sdl; 185 sin_m = blank_sin; 186 sin->sin_addr.s_addr = inet_addr(host); 187 if (sin->sin_addr.s_addr == -1) { 188 if (!(hp = gethostbyname(host))) { 189 fprintf(stderr, "arp: %s: ", host); 190 herror((char *)NULL); 191 return (1); 192 } 193 bcopy((char *)hp->h_addr, (char *)&sin->sin_addr, 194 sizeof sin->sin_addr); 195 } 196 ea = (u_char *)LLADDR(&sdl_m); 197 if (ether_aton(eaddr, ea) == 0) 198 sdl_m.sdl_alen = 6; 199 doing_proxy = flags = export_only = expire_time = 0; 200 while (argc-- > 0) { 201 if (strncmp(argv[0], "temp", 4) == 0) { 202 struct timeval time; 203 gettimeofday(&time, 0); 204 expire_time = time.tv_sec + 20 * 60; 205 } 206 else if (strncmp(argv[0], "pub", 3) == 0) { 207 flags |= RTF_ANNOUNCE; 208 doing_proxy = SIN_PROXY; 209 } else if (strncmp(argv[0], "trail", 5) == 0) { 210 printf("%s: Sending trailers is no longer supported\n", 211 host); 212 } 213 argv++; 214 } 215 tryagain: 216 if (rtmsg(RTM_GET) < 0) { 217 perror(host); 218 return (1); 219 } 220 sin = (struct sockaddr_inarp *)(rtm + 1); 221 sdl = (struct sockaddr_dl *)(sin->sin_len + (char *)sin); 222 if (sin->sin_addr.s_addr == sin_m.sin_addr.s_addr) { 223 if (sdl->sdl_family == AF_LINK && 224 (rtm->rtm_flags & RTF_LLINFO) && 225 !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) { 226 case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023: 227 case IFT_ISO88024: case IFT_ISO88025: 228 goto overwrite; 229 } 230 if (doing_proxy == 0) { 231 printf("set: can only proxy for %s\n", host); 232 return (1); 233 } 234 if (sin_m.sin_other & SIN_PROXY) { 235 printf("set: proxy entry exists for non 802 device\n"); 236 return(1); 237 } 238 sin_m.sin_other = SIN_PROXY; 239 export_only = 1; 240 goto tryagain; 241 } 242 overwrite: 243 if (sdl->sdl_family != AF_LINK) { 244 printf("cannot intuit interface index and type for %s\n", host); 245 return (1); 246 } 247 sdl_m.sdl_type = sdl->sdl_type; 248 sdl_m.sdl_index = sdl->sdl_index; 249 return (rtmsg(RTM_ADD)); 250 } 251 252 /* 253 * Display an individual arp entry 254 */ 255 get(host) 256 char *host; 257 { 258 struct hostent *hp; 259 struct sockaddr_inarp *sin = &sin_m; 260 u_char *ea; 261 262 sin_m = blank_sin; 263 sin->sin_addr.s_addr = inet_addr(host); 264 if (sin->sin_addr.s_addr == -1) { 265 if (!(hp = gethostbyname(host))) { 266 fprintf(stderr, "arp: %s: ", host); 267 herror((char *)NULL); 268 exit(1); 269 } 270 bcopy((char *)hp->h_addr, (char *)&sin->sin_addr, 271 sizeof sin->sin_addr); 272 } 273 dump(sin->sin_addr.s_addr); 274 if (found_entry == 0) { 275 printf("%s (%s) -- no entry\n", 276 host, inet_ntoa(sin->sin_addr)); 277 exit(1); 278 } 279 } 280 281 /* 282 * Delete an arp entry 283 */ 284 delete(host, info) 285 char *host; 286 char *info; 287 { 288 struct hostent *hp; 289 register struct sockaddr_inarp *sin = &sin_m; 290 register struct rt_msghdr *rtm = &m_rtmsg.m_rtm; 291 struct sockaddr_dl *sdl; 292 u_char *ea; 293 char *eaddr; 294 295 if (info && strncmp(info, "pro", 3) ) 296 export_only = 1; 297 getsocket(); 298 sin_m = blank_sin; 299 sin->sin_addr.s_addr = inet_addr(host); 300 if (sin->sin_addr.s_addr == -1) { 301 if (!(hp = gethostbyname(host))) { 302 fprintf(stderr, "arp: %s: ", host); 303 herror((char *)NULL); 304 return (1); 305 } 306 bcopy((char *)hp->h_addr, (char *)&sin->sin_addr, 307 sizeof sin->sin_addr); 308 } 309 tryagain: 310 if (rtmsg(RTM_GET) < 0) { 311 perror(host); 312 return (1); 313 } 314 sin = (struct sockaddr_inarp *)(rtm + 1); 315 sdl = (struct sockaddr_dl *)(sin->sin_len + (char *)sin); 316 if (sin->sin_addr.s_addr == sin_m.sin_addr.s_addr) { 317 if (sdl->sdl_family == AF_LINK && 318 (rtm->rtm_flags & RTF_LLINFO) && 319 !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) { 320 case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023: 321 case IFT_ISO88024: case IFT_ISO88025: 322 goto delete; 323 } 324 } 325 if (sin_m.sin_other & SIN_PROXY) { 326 fprintf(stderr, "delete: can't locate %s\n",host); 327 return (1); 328 } else { 329 sin_m.sin_other = SIN_PROXY; 330 goto tryagain; 331 } 332 delete: 333 if (sdl->sdl_family != AF_LINK) { 334 printf("cannot locate %s\n", host); 335 return (1); 336 } 337 if (rtmsg(RTM_DELETE) == 0) 338 printf("%s (%s) deleted\n", host, inet_ntoa(sin->sin_addr)); 339 } 340 341 /* 342 * Dump the entire arp table 343 */ 344 dump(addr) 345 u_long addr; 346 { 347 int mib[6]; 348 size_t needed; 349 char *host, *malloc(), *lim, *buf, *next; 350 struct rt_msghdr *rtm; 351 struct sockaddr_inarp *sin; 352 struct sockaddr_dl *sdl; 353 extern int h_errno; 354 struct hostent *hp; 355 356 mib[0] = CTL_NET; 357 mib[1] = PF_ROUTE; 358 mib[2] = 0; 359 mib[3] = AF_INET; 360 mib[4] = NET_RT_FLAGS; 361 mib[5] = RTF_LLINFO; 362 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) 363 quit("route-sysctl-estimate"); 364 if ((buf = malloc(needed)) == NULL) 365 quit("malloc"); 366 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) 367 quit("actual retrieval of routing table"); 368 lim = buf + needed; 369 for (next = buf; next < lim; next += rtm->rtm_msglen) { 370 rtm = (struct rt_msghdr *)next; 371 sin = (struct sockaddr_inarp *)(rtm + 1); 372 sdl = (struct sockaddr_dl *)(sin + 1); 373 if (addr) { 374 if (addr != sin->sin_addr.s_addr) 375 continue; 376 found_entry = 1; 377 } 378 if (nflag == 0) 379 hp = gethostbyaddr((caddr_t)&(sin->sin_addr), 380 sizeof sin->sin_addr, AF_INET); 381 else 382 hp = 0; 383 if (hp) 384 host = hp->h_name; 385 else { 386 host = "?"; 387 if (h_errno == TRY_AGAIN) 388 nflag = 1; 389 } 390 printf("%s (%s) at ", host, inet_ntoa(sin->sin_addr)); 391 if (sdl->sdl_alen) 392 ether_print(LLADDR(sdl)); 393 else 394 printf("(incomplete)"); 395 if (rtm->rtm_rmx.rmx_expire == 0) 396 printf(" permanent"); 397 if (sin->sin_other & SIN_PROXY) 398 printf(" published (proxy only)"); 399 if (rtm->rtm_addrs & RTA_NETMASK) { 400 sin = (struct sockaddr_inarp *) 401 (sdl->sdl_len + (char *)sdl); 402 if (sin->sin_addr.s_addr == 0xffffffff) 403 printf(" published"); 404 if (sin->sin_len != 8) 405 printf("(wierd)"); 406 } 407 printf("\n"); 408 } 409 } 410 411 ether_print(cp) 412 u_char *cp; 413 { 414 printf("%x:%x:%x:%x:%x:%x", cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]); 415 } 416 417 ether_aton(a, n) 418 char *a; 419 u_char *n; 420 { 421 int i, o[6]; 422 423 i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2], 424 &o[3], &o[4], &o[5]); 425 if (i != 6) { 426 fprintf(stderr, "arp: invalid Ethernet address '%s'\n", a); 427 return (1); 428 } 429 for (i=0; i<6; i++) 430 n[i] = o[i]; 431 return (0); 432 } 433 434 usage() 435 { 436 printf("usage: arp hostname\n"); 437 printf(" arp -a [kernel] [kernel_memory]\n"); 438 printf(" arp -d hostname\n"); 439 printf(" arp -s hostname ether_addr [temp] [pub]\n"); 440 printf(" arp -f filename\n"); 441 exit(1); 442 } 443 444 rtmsg(cmd) 445 { 446 static int seq; 447 int rlen; 448 register struct rt_msghdr *rtm = &m_rtmsg.m_rtm; 449 register char *cp = m_rtmsg.m_space; 450 register int l; 451 452 errno = 0; 453 if (cmd == RTM_DELETE) 454 goto doit; 455 bzero((char *)&m_rtmsg, sizeof(m_rtmsg)); 456 rtm->rtm_flags = flags; 457 rtm->rtm_version = RTM_VERSION; 458 459 switch (cmd) { 460 default: 461 fprintf(stderr, "arp: internal wrong cmd\n"); 462 exit(1); 463 case RTM_ADD: 464 rtm->rtm_addrs |= RTA_GATEWAY; 465 rtm->rtm_rmx.rmx_expire = expire_time; 466 rtm->rtm_inits = RTV_EXPIRE; 467 rtm->rtm_flags |= (RTF_HOST | RTF_STATIC); 468 sin_m.sin_other = 0; 469 if (doing_proxy) { 470 if (export_only) 471 sin_m.sin_other = SIN_PROXY; 472 else { 473 rtm->rtm_addrs |= RTA_NETMASK; 474 rtm->rtm_flags &= ~RTF_HOST; 475 } 476 } 477 /* FALLTHROUGH */ 478 case RTM_GET: 479 rtm->rtm_addrs |= RTA_DST; 480 } 481 #define NEXTADDR(w, s) \ 482 if (rtm->rtm_addrs & (w)) { \ 483 bcopy((char *)&s, cp, sizeof(s)); cp += sizeof(s);} 484 485 NEXTADDR(RTA_DST, sin_m); 486 NEXTADDR(RTA_GATEWAY, sdl_m); 487 NEXTADDR(RTA_NETMASK, so_mask); 488 489 rtm->rtm_msglen = cp - (char *)&m_rtmsg; 490 doit: 491 l = rtm->rtm_msglen; 492 rtm->rtm_seq = ++seq; 493 rtm->rtm_type = cmd; 494 if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) { 495 if (errno != ESRCH || cmd != RTM_DELETE) { 496 perror("writing to routing socket"); 497 return (-1); 498 } 499 } 500 do { 501 l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg)); 502 } while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid)); 503 if (l < 0) 504 (void) fprintf(stderr, "arp: read from routing socket: %s\n", 505 strerror(errno)); 506 return (0); 507 } 508 509 quit(msg) 510 char *msg; 511 { 512 fprintf(stderr, "%s\n", msg); 513 exit(1); 514 } 515