2 * Copyright (c) 1984, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Sun Microsystems, Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
38 static char const copyright[] =
39 "@(#) Copyright (c) 1984, 1993\n\
40 The Regents of the University of California. All rights reserved.\n";
45 static char const sccsid[] = "@(#)from: arp.c 8.2 (Berkeley) 1/2/94";
47 static const char rcsid[] =
52 * arp - display, set, and delete arp table entries
56 #include <sys/param.h>
58 #include <sys/socket.h>
59 #include <sys/sockio.h>
60 #include <sys/sysctl.h>
61 #include <sys/ioctl.h>
65 #include <net/if_dl.h>
66 #include <net/if_types.h>
67 #include <net/route.h>
68 #include <net/iso88025.h>
70 #include <netinet/in.h>
71 #include <netinet/if_ether.h>
73 #include <arpa/inet.h>
86 void search(u_long addr, void (*action)(struct sockaddr_dl *sdl,
87 struct sockaddr_inarp *sin, struct rt_msghdr *rtm));
88 void print_entry(struct sockaddr_dl *sdl,
89 struct sockaddr_inarp *addr, struct rt_msghdr *rtm);
90 void nuke_entry(struct sockaddr_dl *sdl,
91 struct sockaddr_inarp *addr, struct rt_msghdr *rtm);
92 int delete(char *host, char *info);
94 int set(int argc, char **argv);
98 int my_ether_aton(char *a, struct ether_addr *n);
100 int get_ether_addr(u_int32_t ipaddr, struct ether_addr *hwaddr);
103 static int nflag; /* no reverse dns lookups */
104 static int aflag; /* do it for all entries */
107 struct sockaddr_in so_mask;
108 struct sockaddr_inarp blank_sin, sin_m;
109 struct sockaddr_dl blank_sdl, sdl_m;
110 int expire_time, flags, doing_proxy, proxy_only, found_entry;
112 struct rt_msghdr m_rtm;
116 /* which function we're supposed to do */
124 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
125 #define SETFUNC(f) { if (func) usage(); func = (f); }
128 main(int argc, char *argv[])
134 while ((ch = getopt(argc, argv, "andfsS")) != -1)
161 bzero(&so_mask, sizeof(so_mask));
163 so_mask.sin_addr.s_addr = 0xffffffff;
164 bzero(&blank_sin, sizeof(blank_sin));
165 blank_sin.sin_len = sizeof(blank_sin);
166 blank_sin.sin_family = AF_INET;
167 bzero(&blank_sdl, sizeof(blank_sdl));
168 blank_sdl.sdl_len = sizeof(blank_sdl);
169 blank_sdl.sdl_family = AF_LINK;
178 search(0, print_entry);
187 if (argc < 2 || argc > 6)
189 if (func == F_REPLACE)
190 (void) delete(argv[0], NULL);
191 rtn = set(argc, argv) ? 1 : 0;
197 search(0, nuke_entry);
199 if (argc < 1 || argc > 2)
201 rtn = delete(argv[0], argv[1]);
215 * Process a file to set standard arp entries
222 char line[100], arg[5][50], *args[5];
224 if ((fp = fopen(name, "r")) == NULL)
225 errx(1, "cannot open %s", name);
226 args[0] = &arg[0][0];
227 args[1] = &arg[1][0];
228 args[2] = &arg[2][0];
229 args[3] = &arg[3][0];
230 args[4] = &arg[4][0];
232 while(fgets(line, 100, fp) != NULL) {
233 i = sscanf(line, "%49s %49s %49s %49s %49s", arg[0], arg[1],
234 arg[2], arg[3], arg[4]);
236 warnx("bad line: %s", line);
251 s = socket(PF_ROUTE, SOCK_RAW, 0);
258 * Set an individual arp entry
261 set(int argc, char **argv)
264 register struct sockaddr_inarp *addr = &sin_m;
265 register struct sockaddr_dl *sdl;
266 register struct rt_msghdr *rtm = &(m_rtmsg.m_rtm);
267 struct ether_addr *ea;
268 char *host = argv[0], *eaddr = argv[1];
275 addr->sin_addr.s_addr = inet_addr(host);
276 if (addr->sin_addr.s_addr == INADDR_NONE) {
277 if (!(hp = gethostbyname(host))) {
278 warnx("%s: %s", host, hstrerror(h_errno));
281 bcopy((char *)hp->h_addr, (char *)&addr->sin_addr,
282 sizeof addr->sin_addr);
284 doing_proxy = flags = proxy_only = expire_time = 0;
286 if (strncmp(argv[0], "temp", 4) == 0) {
288 gettimeofday(&tv, 0);
289 expire_time = tv.tv_sec + 20 * 60;
291 else if (strncmp(argv[0], "pub", 3) == 0) {
292 flags |= RTF_ANNOUNCE;
294 if (argc && strncmp(argv[1], "only", 3) == 0) {
296 sin_m.sin_other = SIN_PROXY;
299 } else if (strncmp(argv[0], "trail", 5) == 0) {
300 printf("%s: Sending trailers is no longer supported\n",
305 ea = (struct ether_addr *)LLADDR(&sdl_m);
306 if (doing_proxy && !strcmp(eaddr, "auto")) {
307 if (!get_ether_addr(addr->sin_addr.s_addr, ea)) {
308 printf("no interface found for %s\n",
309 inet_ntoa(addr->sin_addr));
312 sdl_m.sdl_alen = ETHER_ADDR_LEN;
314 if (my_ether_aton(eaddr, ea) == 0)
315 sdl_m.sdl_alen = ETHER_ADDR_LEN;
318 if (rtmsg(RTM_GET) < 0) {
322 addr = (struct sockaddr_inarp *)(rtm + 1);
323 sdl = (struct sockaddr_dl *)(ROUNDUP(addr->sin_len) + (char *)addr);
324 if (addr->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
325 if (sdl->sdl_family == AF_LINK &&
326 (rtm->rtm_flags & RTF_LLINFO) &&
327 !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) {
328 case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
329 case IFT_ISO88024: case IFT_ISO88025: case IFT_L2VLAN:
332 if (doing_proxy == 0) {
333 printf("set: can only proxy for %s\n", host);
336 if (sin_m.sin_other & SIN_PROXY) {
337 printf("set: proxy entry exists for non 802 device\n");
340 sin_m.sin_other = SIN_PROXY;
345 if (sdl->sdl_family != AF_LINK) {
346 printf("cannot intuit interface index and type for %s\n", host);
349 sdl_m.sdl_type = sdl->sdl_type;
350 sdl_m.sdl_index = sdl->sdl_index;
351 return (rtmsg(RTM_ADD));
355 * Display an individual arp entry
361 struct sockaddr_inarp *addr = &sin_m;
364 addr->sin_addr.s_addr = inet_addr(host);
365 if (addr->sin_addr.s_addr == INADDR_NONE) {
366 if (!(hp = gethostbyname(host)))
367 errx(1, "%s: %s", host, hstrerror(h_errno));
368 bcopy((char *)hp->h_addr, (char *)&addr->sin_addr,
369 sizeof addr->sin_addr);
371 search(addr->sin_addr.s_addr, print_entry);
372 if (found_entry == 0) {
373 printf("%s (%s) -- no entry\n",
374 host, inet_ntoa(addr->sin_addr));
381 * Delete an arp entry
384 delete(char *host, char *info)
387 register struct sockaddr_inarp *addr = &sin_m;
388 register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
389 struct sockaddr_dl *sdl;
394 if (strncmp(info, "pub", 3) == 0)
395 sin_m.sin_other = SIN_PROXY;
399 addr->sin_addr.s_addr = inet_addr(host);
400 if (addr->sin_addr.s_addr == INADDR_NONE) {
401 if (!(hp = gethostbyname(host))) {
402 warnx("%s: %s", host, hstrerror(h_errno));
405 bcopy((char *)hp->h_addr, (char *)&addr->sin_addr,
406 sizeof addr->sin_addr);
409 if (rtmsg(RTM_GET) < 0) {
413 addr = (struct sockaddr_inarp *)(rtm + 1);
414 sdl = (struct sockaddr_dl *)(ROUNDUP(addr->sin_len) + (char *)addr);
415 if (addr->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
416 if (sdl->sdl_family == AF_LINK &&
417 (rtm->rtm_flags & RTF_LLINFO) &&
418 !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) {
419 case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
420 case IFT_ISO88024: case IFT_ISO88025: case IFT_L2VLAN:
424 if (sin_m.sin_other & SIN_PROXY) {
425 fprintf(stderr, "delete: can't locate %s\n",host);
428 sin_m.sin_other = SIN_PROXY;
432 if (sdl->sdl_family != AF_LINK) {
433 printf("cannot locate %s\n", host);
436 if (rtmsg(RTM_DELETE) == 0) {
437 printf("%s (%s) deleted\n", host, inet_ntoa(addr->sin_addr));
444 * Search the arp table and do some action on matching entries
447 search(u_long addr, void (*action)(struct sockaddr_dl *sdl,
448 struct sockaddr_inarp *sin, struct rt_msghdr *rtm))
452 char *lim, *buf, *next;
453 struct rt_msghdr *rtm;
454 struct sockaddr_inarp *sin2;
455 struct sockaddr_dl *sdl;
461 mib[4] = NET_RT_FLAGS;
463 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
464 errx(1, "route-sysctl-estimate");
465 if ((buf = malloc(needed)) == NULL)
467 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
468 errx(1, "actual retrieval of routing table");
470 for (next = buf; next < lim; next += rtm->rtm_msglen) {
471 rtm = (struct rt_msghdr *)next;
472 sin2 = (struct sockaddr_inarp *)(rtm + 1);
473 (char *)sdl = (char *)sin2 + ROUNDUP(sin2->sin_len);
475 if (addr != sin2->sin_addr.s_addr)
479 (*action)(sdl, sin2, rtm);
485 * Display an arp entry
488 print_entry(struct sockaddr_dl *sdl,
489 struct sockaddr_inarp *addr, struct rt_msghdr *rtm)
493 struct iso88025_sockaddr_dl_data *trld;
494 char ifname[IF_NAMESIZE];
498 hp = gethostbyaddr((caddr_t)&(addr->sin_addr),
499 sizeof addr->sin_addr, AF_INET);
506 if (h_errno == TRY_AGAIN)
509 printf("%s (%s) at ", host, inet_ntoa(addr->sin_addr));
511 printf("%s", ether_ntoa((struct ether_addr *)LLADDR(sdl)));
513 printf("(incomplete)");
514 if (if_indextoname(sdl->sdl_index, ifname) != NULL)
515 printf(" on %s", ifname);
516 if (rtm->rtm_rmx.rmx_expire == 0)
517 printf(" permanent");
518 if (addr->sin_other & SIN_PROXY)
519 printf(" published (proxy only)");
520 if (rtm->rtm_addrs & RTA_NETMASK) {
521 addr = (struct sockaddr_inarp *)
522 (ROUNDUP(sdl->sdl_len) + (char *)sdl);
523 if (addr->sin_addr.s_addr == 0xffffffff)
524 printf(" published");
525 if (addr->sin_len != 8)
528 switch(sdl->sdl_type) {
530 printf(" [ethernet]");
533 printf(" [token-ring]");
534 trld = SDL_ISO88025(sdl);
535 if (trld->trld_rcf != 0) {
536 printf(" rt=%x", ntohs(trld->trld_rcf));
538 seg < ((TR_RCF_RIFLEN(trld->trld_rcf) - 2 ) / 2);
540 printf(":%x", ntohs(*(trld->trld_route[seg])));
564 nuke_entry(struct sockaddr_dl *sdl __unused,
565 struct sockaddr_inarp *addr, struct rt_msghdr *rtm __unused)
569 snprintf(ip, sizeof(ip), "%s", inet_ntoa(addr->sin_addr));
574 my_ether_aton(char *a, struct ether_addr *n)
576 struct ether_addr *ea;
578 if ((ea = ether_aton(a)) == NULL) {
579 warnx("invalid Ethernet address '%s'", a);
589 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
590 "usage: arp [-n] hostname",
592 " arp -d hostname [pub]",
594 " arp -s hostname ether_addr [temp] [pub]",
595 " arp -S hostname ether_addr [temp] [pub]",
605 register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
606 register char *cp = m_rtmsg.m_space;
610 if (cmd == RTM_DELETE)
612 bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
613 rtm->rtm_flags = flags;
614 rtm->rtm_version = RTM_VERSION;
618 errx(1, "internal wrong cmd");
620 rtm->rtm_addrs |= RTA_GATEWAY;
621 rtm->rtm_rmx.rmx_expire = expire_time;
622 rtm->rtm_inits = RTV_EXPIRE;
623 rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
627 sin_m.sin_other = SIN_PROXY;
629 rtm->rtm_addrs |= RTA_NETMASK;
630 rtm->rtm_flags &= ~RTF_HOST;
635 rtm->rtm_addrs |= RTA_DST;
637 #define NEXTADDR(w, s) \
638 if (rtm->rtm_addrs & (w)) { \
639 bcopy((char *)&s, cp, sizeof(s)); cp += ROUNDUP(sizeof(s));}
641 NEXTADDR(RTA_DST, sin_m);
642 NEXTADDR(RTA_GATEWAY, sdl_m);
643 NEXTADDR(RTA_NETMASK, so_mask);
645 rtm->rtm_msglen = cp - (char *)&m_rtmsg;
648 rtm->rtm_seq = ++seq;
650 if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
651 if (errno != ESRCH || cmd != RTM_DELETE) {
652 warn("writing to routing socket");
657 l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
658 } while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
660 warn("read from routing socket");
665 * get_ether_addr - get the hardware address of an interface on the
666 * the same subnet as ipaddr.
671 get_ether_addr(u_int32_t ipaddr, struct ether_addr *hwaddr)
673 struct ifreq *ifr, *ifend, *ifp;
675 struct sockaddr_dl *dla;
678 struct ifreq ifs[MAX_IFS];
681 sock = socket(AF_INET, SOCK_DGRAM, 0);
685 ifc.ifc_len = sizeof(ifs);
687 if (ioctl(sock, SIOCGIFCONF, &ifc) < 0) {
688 warnx("ioctl(SIOCGIFCONF)");
694 * Scan through looking for an interface with an Internet
695 * address on the same subnet as `ipaddr'.
697 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
698 for (ifr = ifc.ifc_req; ifr < ifend; ) {
699 if (ifr->ifr_addr.sa_family == AF_INET) {
700 ina = ((struct sockaddr_in *)
701 &ifr->ifr_addr)->sin_addr.s_addr;
702 strncpy(ifreq.ifr_name, ifr->ifr_name,
703 sizeof(ifreq.ifr_name));
705 * Check that the interface is up,
706 * and not point-to-point or loopback.
708 if (ioctl(sock, SIOCGIFFLAGS, &ifreq) < 0)
710 if ((ifreq.ifr_flags &
711 (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|
712 IFF_LOOPBACK|IFF_NOARP))
713 != (IFF_UP|IFF_BROADCAST))
716 * Get its netmask and check that it's on
719 if (ioctl(sock, SIOCGIFNETMASK, &ifreq) < 0)
721 mask = ((struct sockaddr_in *)
722 &ifreq.ifr_addr)->sin_addr.s_addr;
723 if ((ipaddr & mask) != (ina & mask))
728 ifr = (struct ifreq *) ((char *)&ifr->ifr_addr
729 + MAX(ifr->ifr_addr.sa_len, sizeof(ifr->ifr_addr)));
738 * Now scan through again looking for a link-level address
739 * for this interface.
742 for (ifr = ifc.ifc_req; ifr < ifend; ) {
743 if (strcmp(ifp->ifr_name, ifr->ifr_name) == 0
744 && ifr->ifr_addr.sa_family == AF_LINK) {
746 * Found the link-level address - copy it out
748 dla = (struct sockaddr_dl *) &ifr->ifr_addr;
749 memcpy(hwaddr, LLADDR(dla), dla->sdl_alen);
751 printf("using interface %s for proxy with address ",
753 printf("%s\n", ether_ntoa(hwaddr));
754 return dla->sdl_alen;
756 ifr = (struct ifreq *) ((char *)&ifr->ifr_addr
757 + MAX(ifr->ifr_addr.sa_len, sizeof(ifr->ifr_addr)));