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>
69 #include <netinet/in.h>
70 #include <netinet/if_ether.h>
72 #include <arpa/inet.h>
84 void search(u_long addr, void (*action)(struct sockaddr_dl *sdl,
85 struct sockaddr_inarp *sin, struct rt_msghdr *rtm));
86 void print_entry(struct sockaddr_dl *sdl,
87 struct sockaddr_inarp *sin, struct rt_msghdr *rtm);
88 void nuke_entry(struct sockaddr_dl *sdl,
89 struct sockaddr_inarp *sin, struct rt_msghdr *rtm);
90 int delete(char *host, char *info);
91 void ether_print(u_char *cp);
93 int set(int argc, char **argv);
97 int my_ether_aton(char *a, u_char *n);
99 int get_ether_addr(u_int32_t ipaddr, u_char *hwaddr);
102 static int nflag; /* no reverse dns lookups */
103 static int aflag; /* do it for all entries */
106 /* which function we're supposed to do */
114 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
115 #define SETFUNC(f) { if (func) usage(); func = (f); }
126 while ((ch = getopt(argc, argv, "andfsS")) != -1)
160 search(0, print_entry);
169 if (argc < 2 || argc > 5)
171 if (func == F_REPLACE)
172 (void) delete(argv[0], NULL);
173 rtn = set(argc, argv) ? 1 : 0;
179 search(0, nuke_entry);
181 if (argc < 1 || argc > 2)
183 rtn = delete(argv[0], argv[1]);
197 * Process a file to set standard arp entries
204 char line[100], arg[5][50], *args[5];
206 if ((fp = fopen(name, "r")) == NULL)
207 errx(1, "cannot open %s", name);
208 args[0] = &arg[0][0];
209 args[1] = &arg[1][0];
210 args[2] = &arg[2][0];
211 args[3] = &arg[3][0];
212 args[4] = &arg[4][0];
214 while(fgets(line, 100, fp) != NULL) {
215 i = sscanf(line, "%49s %49s %49s %49s %49s", arg[0], arg[1],
216 arg[2], arg[3], arg[4]);
218 warnx("bad line: %s", line);
233 s = socket(PF_ROUTE, SOCK_RAW, 0);
239 struct sockaddr_in so_mask = {8, 0, 0, { 0xffffffff}};
240 struct sockaddr_inarp blank_sin = {sizeof(blank_sin), AF_INET }, sin_m;
241 struct sockaddr_dl blank_sdl = {sizeof(blank_sdl), AF_LINK }, sdl_m;
242 int expire_time, flags, export_only, doing_proxy, found_entry;
244 struct rt_msghdr m_rtm;
249 * Set an individual arp entry
252 set(int argc, char **argv)
255 register struct sockaddr_inarp *sin = &sin_m;
256 register struct sockaddr_dl *sdl;
257 register struct rt_msghdr *rtm = &(m_rtmsg.m_rtm);
259 char *host = argv[0], *eaddr = argv[1];
266 sin->sin_addr.s_addr = inet_addr(host);
267 if (sin->sin_addr.s_addr == -1) {
268 if (!(hp = gethostbyname(host))) {
269 warnx("%s: %s", host, hstrerror(h_errno));
272 bcopy((char *)hp->h_addr, (char *)&sin->sin_addr,
273 sizeof sin->sin_addr);
275 doing_proxy = flags = export_only = expire_time = 0;
277 if (strncmp(argv[0], "temp", 4) == 0) {
279 gettimeofday(&time, 0);
280 expire_time = time.tv_sec + 20 * 60;
282 else if (strncmp(argv[0], "pub", 3) == 0) {
283 flags |= RTF_ANNOUNCE;
284 doing_proxy = SIN_PROXY;
285 } else if (strncmp(argv[0], "trail", 5) == 0) {
286 printf("%s: Sending trailers is no longer supported\n",
291 ea = (u_char *)LLADDR(&sdl_m);
292 if (doing_proxy && !strcmp(eaddr, "auto")) {
293 if (!get_ether_addr(sin->sin_addr.s_addr, ea)) {
298 if (my_ether_aton(eaddr, ea) == 0)
302 if (rtmsg(RTM_GET) < 0) {
306 sin = (struct sockaddr_inarp *)(rtm + 1);
307 sdl = (struct sockaddr_dl *)(ROUNDUP(sin->sin_len) + (char *)sin);
308 if (sin->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
309 if (sdl->sdl_family == AF_LINK &&
310 (rtm->rtm_flags & RTF_LLINFO) &&
311 !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) {
312 case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
313 case IFT_ISO88024: case IFT_ISO88025:
316 if (doing_proxy == 0) {
317 printf("set: can only proxy for %s\n", host);
320 if (sin_m.sin_other & SIN_PROXY) {
321 printf("set: proxy entry exists for non 802 device\n");
324 sin_m.sin_other = SIN_PROXY;
329 if (sdl->sdl_family != AF_LINK) {
330 printf("cannot intuit interface index and type for %s\n", host);
333 sdl_m.sdl_type = sdl->sdl_type;
334 sdl_m.sdl_index = sdl->sdl_index;
335 return (rtmsg(RTM_ADD));
339 * Display an individual arp entry
345 struct sockaddr_inarp *sin = &sin_m;
348 sin->sin_addr.s_addr = inet_addr(host);
349 if (sin->sin_addr.s_addr == -1) {
350 if (!(hp = gethostbyname(host)))
351 errx(1, "%s: %s", host, hstrerror(h_errno));
352 bcopy((char *)hp->h_addr, (char *)&sin->sin_addr,
353 sizeof sin->sin_addr);
355 search(sin->sin_addr.s_addr, print_entry);
356 if (found_entry == 0) {
357 printf("%s (%s) -- no entry\n",
358 host, inet_ntoa(sin->sin_addr));
365 * Delete an arp entry
368 delete(char *host, char *info)
371 register struct sockaddr_inarp *sin = &sin_m;
372 register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
373 struct sockaddr_dl *sdl;
375 if (info && strncmp(info, "pro", 3) )
379 sin->sin_addr.s_addr = inet_addr(host);
380 if (sin->sin_addr.s_addr == -1) {
381 if (!(hp = gethostbyname(host))) {
382 warnx("%s: %s", host, hstrerror(h_errno));
385 bcopy((char *)hp->h_addr, (char *)&sin->sin_addr,
386 sizeof sin->sin_addr);
389 if (rtmsg(RTM_GET) < 0) {
393 sin = (struct sockaddr_inarp *)(rtm + 1);
394 sdl = (struct sockaddr_dl *)(ROUNDUP(sin->sin_len) + (char *)sin);
395 if (sin->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
396 if (sdl->sdl_family == AF_LINK &&
397 (rtm->rtm_flags & RTF_LLINFO) &&
398 !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) {
399 case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
400 case IFT_ISO88024: case IFT_ISO88025:
404 if (sin_m.sin_other & SIN_PROXY) {
405 fprintf(stderr, "delete: can't locate %s\n",host);
408 sin_m.sin_other = SIN_PROXY;
412 if (sdl->sdl_family != AF_LINK) {
413 printf("cannot locate %s\n", host);
416 if (rtmsg(RTM_DELETE) == 0) {
417 printf("%s (%s) deleted\n", host, inet_ntoa(sin->sin_addr));
424 * Search the arp table and do some action on matching entries
427 search(u_long addr, void (*action)(struct sockaddr_dl *sdl,
428 struct sockaddr_inarp *sin, struct rt_msghdr *rtm))
432 char *lim, *buf, *next;
433 struct rt_msghdr *rtm;
434 struct sockaddr_inarp *sin;
435 struct sockaddr_dl *sdl;
442 mib[4] = NET_RT_FLAGS;
444 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
445 errx(1, "route-sysctl-estimate");
446 if ((buf = malloc(needed)) == NULL)
448 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
449 errx(1, "actual retrieval of routing table");
451 for (next = buf; next < lim; next += rtm->rtm_msglen) {
452 rtm = (struct rt_msghdr *)next;
453 sin = (struct sockaddr_inarp *)(rtm + 1);
454 (char *)sdl = (char *)sin + ROUNDUP(sin->sin_len);
456 if (addr != sin->sin_addr.s_addr)
460 (*action)(sdl, sin, rtm);
465 * Display an arp entry
468 print_entry(struct sockaddr_dl *sdl,
469 struct sockaddr_inarp *sin, struct rt_msghdr *rtm)
477 hp = gethostbyaddr((caddr_t)&(sin->sin_addr),
478 sizeof sin->sin_addr, AF_INET);
485 if (h_errno == TRY_AGAIN)
488 printf("%s (%s) at ", host, inet_ntoa(sin->sin_addr));
490 ether_print(LLADDR(sdl));
492 printf("(incomplete)");
493 if (rtm->rtm_rmx.rmx_expire == 0)
494 printf(" permanent");
495 if (sin->sin_other & SIN_PROXY)
496 printf(" published (proxy only)");
497 if (rtm->rtm_addrs & RTA_NETMASK) {
498 sin = (struct sockaddr_inarp *)
499 (ROUNDUP(sdl->sdl_len) + (char *)sdl);
500 if (sin->sin_addr.s_addr == 0xffffffff)
501 printf(" published");
502 if (sin->sin_len != 8)
505 switch(sdl->sdl_type) {
507 printf(" [ethernet]");
510 printf(" [token-ring]");
514 if (sdl->sdl_rcf != NULL) {
515 printf(" rt=%x", ntohs(sdl->sdl_rcf));
516 for (seg = 0; seg < ((((ntohs(sdl->sdl_rcf) & 0x1f00) >> 8) - 2 ) / 2); seg++)
517 printf(":%x", ntohs(sdl->sdl_route[seg]));
528 nuke_entry(struct sockaddr_dl *sdl,
529 struct sockaddr_inarp *sin, struct rt_msghdr *rtm)
533 snprintf(ip, sizeof(ip), "%s", inet_ntoa(sin->sin_addr));
538 ether_print(u_char *cp)
540 printf("%x:%x:%x:%x:%x:%x", cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]);
544 my_ether_aton(char *a, u_char *n)
548 i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2],
549 &o[3], &o[4], &o[5]);
551 warnx("invalid Ethernet address '%s'", a);
562 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
563 "usage: arp [-n] hostname",
565 " arp -d hostname [proxy]",
567 " arp -s hostname ether_addr [temp] [pub]",
568 " arp -S hostname ether_addr [temp] [pub]",
578 register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
579 register char *cp = m_rtmsg.m_space;
583 if (cmd == RTM_DELETE)
585 bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
586 rtm->rtm_flags = flags;
587 rtm->rtm_version = RTM_VERSION;
591 errx(1, "internal wrong cmd");
593 rtm->rtm_addrs |= RTA_GATEWAY;
594 rtm->rtm_rmx.rmx_expire = expire_time;
595 rtm->rtm_inits = RTV_EXPIRE;
596 rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
600 sin_m.sin_other = SIN_PROXY;
602 rtm->rtm_addrs |= RTA_NETMASK;
603 rtm->rtm_flags &= ~RTF_HOST;
608 rtm->rtm_addrs |= RTA_DST;
610 #define NEXTADDR(w, s) \
611 if (rtm->rtm_addrs & (w)) { \
612 bcopy((char *)&s, cp, sizeof(s)); cp += ROUNDUP(sizeof(s));}
614 NEXTADDR(RTA_DST, sin_m);
615 NEXTADDR(RTA_GATEWAY, sdl_m);
616 NEXTADDR(RTA_NETMASK, so_mask);
618 rtm->rtm_msglen = cp - (char *)&m_rtmsg;
621 rtm->rtm_seq = ++seq;
623 if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
624 if (errno != ESRCH || cmd != RTM_DELETE) {
625 warn("writing to routing socket");
630 l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
631 } while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
633 warn("read from routing socket");
638 * get_ether_addr - get the hardware address of an interface on the
639 * the same subnet as ipaddr.
644 get_ether_addr(u_int32_t ipaddr, u_char *hwaddr)
646 struct ifreq *ifr, *ifend, *ifp;
648 struct sockaddr_dl *dla;
651 struct ifreq ifs[MAX_IFS];
654 s = socket(AF_INET, SOCK_DGRAM, 0);
658 ifc.ifc_len = sizeof(ifs);
660 if (ioctl(s, SIOCGIFCONF, &ifc) < 0) {
661 warnx("ioctl(SIOCGIFCONF)");
667 * Scan through looking for an interface with an Internet
668 * address on the same subnet as `ipaddr'.
670 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
671 for (ifr = ifc.ifc_req; ifr < ifend; ) {
672 if (ifr->ifr_addr.sa_family == AF_INET) {
673 ina = ((struct sockaddr_in *)
674 &ifr->ifr_addr)->sin_addr.s_addr;
675 strncpy(ifreq.ifr_name, ifr->ifr_name,
676 sizeof(ifreq.ifr_name));
678 * Check that the interface is up,
679 * and not point-to-point or loopback.
681 if (ioctl(s, SIOCGIFFLAGS, &ifreq) < 0)
683 if ((ifreq.ifr_flags &
684 (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|
685 IFF_LOOPBACK|IFF_NOARP))
686 != (IFF_UP|IFF_BROADCAST))
689 * Get its netmask and check that it's on
692 if (ioctl(s, SIOCGIFNETMASK, &ifreq) < 0)
694 mask = ((struct sockaddr_in *)
695 &ifreq.ifr_addr)->sin_addr.s_addr;
696 if ((ipaddr & mask) != (ina & mask))
701 ifr = (struct ifreq *) ((char *)&ifr->ifr_addr
702 + MAX(ifr->ifr_addr.sa_len, sizeof(ifr->ifr_addr)));
711 * Now scan through again looking for a link-level address
712 * for this interface.
715 for (ifr = ifc.ifc_req; ifr < ifend; ) {
716 if (strcmp(ifp->ifr_name, ifr->ifr_name) == 0
717 && ifr->ifr_addr.sa_family == AF_LINK) {
719 * Found the link-level address - copy it out
721 dla = (struct sockaddr_dl *) &ifr->ifr_addr;
722 memcpy(hwaddr, LLADDR(dla), dla->sdl_alen);
724 printf("using interface %s for proxy with address ",
728 return dla->sdl_alen;
730 ifr = (struct ifreq *) ((char *)&ifr->ifr_addr
731 + MAX(ifr->ifr_addr.sa_len, sizeof(ifr->ifr_addr)));