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
36 * @(#) Copyright (c) 1984, 1993 The Regents of the University of California. All rights reserved.
37 * @(#)from: arp.c 8.2 (Berkeley) 1/2/94
38 * $FreeBSD: src/usr.sbin/arp/arp.c,v 1.22.2.12 2003/04/16 10:02:37 ru Exp $
39 * $DragonFly: src/usr.sbin/arp/arp.c,v 1.11 2008/02/03 00:41:44 swildner Exp $
43 * arp - display, set, and delete arp table entries
47 #include <sys/param.h>
49 #include <sys/socket.h>
50 #include <sys/sockio.h>
51 #include <sys/sysctl.h>
52 #include <sys/ioctl.h>
56 #include <net/if_dl.h>
57 #include <net/if_types.h>
58 #include <net/route.h>
60 #include <netinet/in.h>
61 #include <netinet/if_ether.h>
63 #include <arpa/inet.h>
76 void search(u_long addr, void (*action)(struct sockaddr_dl *sdl,
77 struct sockaddr_inarp *sin, struct rt_msghdr *rtm));
78 void print_entry(struct sockaddr_dl *sdl,
79 struct sockaddr_inarp *addr, struct rt_msghdr *rtm);
80 void nuke_entry(struct sockaddr_dl *sdl,
81 struct sockaddr_inarp *addr, struct rt_msghdr *rtm);
82 int delete(char *host, char *info);
84 int set(int argc, char **argv);
88 int my_ether_aton(char *a, struct ether_addr *n);
90 int get_ether_addr(u_int32_t ipaddr, struct ether_addr *hwaddr);
93 static int nflag; /* no reverse dns lookups */
94 static int aflag; /* do it for all entries */
95 static int cpuflag = -1;
98 struct sockaddr_in so_mask;
99 struct sockaddr_inarp blank_sin, sin_m;
100 struct sockaddr_dl blank_sdl, sdl_m;
101 int expire_time, flags, doing_proxy, proxy_only, found_entry;
103 struct rt_msghdr m_rtm;
107 /* which function we're supposed to do */
115 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
116 #define SETFUNC(f) { if (func) usage(); func = (f); }
119 main(int argc, char **argv)
125 while ((ch = getopt(argc, argv, "ac:ndfsS")) != -1)
131 cpuflag = strtol(optarg, NULL, 0);
155 bzero(&so_mask, sizeof(so_mask));
157 so_mask.sin_addr.s_addr = 0xffffffff;
158 bzero(&blank_sin, sizeof(blank_sin));
159 blank_sin.sin_len = sizeof(blank_sin);
160 blank_sin.sin_family = AF_INET;
161 bzero(&blank_sdl, sizeof(blank_sdl));
162 blank_sdl.sdl_len = sizeof(blank_sdl);
163 blank_sdl.sdl_family = AF_LINK;
172 search(0, print_entry);
181 if (argc < 2 || argc > 6)
183 if (func == F_REPLACE)
184 delete(argv[0], NULL);
185 rtn = set(argc, argv) ? 1 : 0;
191 search(0, nuke_entry);
193 if (argc < 1 || argc > 2)
195 rtn = delete(argv[0], argv[1]);
209 * Process a file to set standard arp entries
216 char line[100], arg[5][50], *args[5], *p;
218 if ((fp = fopen(name, "r")) == NULL)
219 errx(1, "cannot open %s", name);
220 args[0] = &arg[0][0];
221 args[1] = &arg[1][0];
222 args[2] = &arg[2][0];
223 args[3] = &arg[3][0];
224 args[4] = &arg[4][0];
226 while(fgets(line, 100, fp) != NULL) {
227 if ((p = strchr(line, '#')) != NULL)
229 for (p = line; isblank(*p); p++);
230 if (*p == '\n' || *p == '\0')
232 i = sscanf(p, "%49s %49s %49s %49s %49s", arg[0], arg[1],
233 arg[2], arg[3], arg[4]);
235 warnx("bad line: %s", line);
250 s = socket(PF_ROUTE, SOCK_RAW, 0);
257 * Set an individual arp entry
260 set(int argc, char **argv)
263 struct sockaddr_inarp *addr = &sin_m;
264 struct sockaddr_dl *sdl;
265 struct rt_msghdr *rtm = &(m_rtmsg.m_rtm);
266 struct ether_addr *ea;
267 char *host = argv[0], *eaddr = argv[1];
274 addr->sin_addr.s_addr = inet_addr(host);
275 if (addr->sin_addr.s_addr == INADDR_NONE) {
276 if (!(hp = gethostbyname(host))) {
277 warnx("%s: %s", host, hstrerror(h_errno));
280 bcopy((char *)hp->h_addr, (char *)&addr->sin_addr,
281 sizeof(addr->sin_addr));
283 doing_proxy = flags = proxy_only = expire_time = 0;
285 if (strncmp(argv[0], "temp", 4) == 0) {
287 gettimeofday(&tv, 0);
288 expire_time = tv.tv_sec + 20 * 60;
290 else if (strncmp(argv[0], "pub", 3) == 0) {
291 flags |= RTF_ANNOUNCE;
293 if (argc && strncmp(argv[1], "only", 3) == 0) {
295 sin_m.sin_other = SIN_PROXY;
298 } else if (strncmp(argv[0], "trail", 5) == 0) {
299 printf("%s: Sending trailers is no longer supported\n",
304 ea = (struct ether_addr *)LLADDR(&sdl_m);
305 if (doing_proxy && !strcmp(eaddr, "auto")) {
306 if (!get_ether_addr(addr->sin_addr.s_addr, ea)) {
307 printf("no interface found for %s\n",
308 inet_ntoa(addr->sin_addr));
311 sdl_m.sdl_alen = ETHER_ADDR_LEN;
313 if (my_ether_aton(eaddr, ea) == 0)
314 sdl_m.sdl_alen = ETHER_ADDR_LEN;
317 if (rtmsg(RTM_GET) < 0) {
321 addr = (struct sockaddr_inarp *)(rtm + 1);
322 sdl = (struct sockaddr_dl *)(ROUNDUP(addr->sin_len) + (char *)addr);
323 if (addr->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
324 if (sdl->sdl_family == AF_LINK &&
325 (rtm->rtm_flags & RTF_LLINFO) &&
326 !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) {
327 case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
328 case IFT_ISO88024: case IFT_ISO88025: case IFT_L2VLAN:
331 if (doing_proxy == 0) {
332 printf("set: can only proxy for %s\n", host);
335 if (sin_m.sin_other & SIN_PROXY) {
336 printf("set: proxy entry exists for non 802 device\n");
339 sin_m.sin_other = SIN_PROXY;
344 if (sdl->sdl_family != AF_LINK) {
345 printf("cannot intuit interface index and type for %s\n", host);
348 sdl_m.sdl_type = sdl->sdl_type;
349 sdl_m.sdl_index = sdl->sdl_index;
350 return(rtmsg(RTM_ADD));
354 * Display an individual arp entry
360 struct sockaddr_inarp *addr = &sin_m;
363 addr->sin_addr.s_addr = inet_addr(host);
364 if (addr->sin_addr.s_addr == INADDR_NONE) {
365 if (!(hp = gethostbyname(host)))
366 errx(1, "%s: %s", host, hstrerror(h_errno));
367 bcopy((char *)hp->h_addr, (char *)&addr->sin_addr,
368 sizeof(addr->sin_addr));
370 search(addr->sin_addr.s_addr, print_entry);
371 if (found_entry == 0) {
372 printf("%s (%s) -- no entry\n",
373 host, inet_ntoa(addr->sin_addr));
380 * Delete an arp entry
383 delete(char *host, char *info)
386 struct sockaddr_inarp *addr = &sin_m;
387 struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
388 struct sockaddr_dl *sdl;
393 if (strncmp(info, "pub", 3) == 0)
394 sin_m.sin_other = SIN_PROXY;
398 addr->sin_addr.s_addr = inet_addr(host);
399 if (addr->sin_addr.s_addr == INADDR_NONE) {
400 if (!(hp = gethostbyname(host))) {
401 warnx("%s: %s", host, hstrerror(h_errno));
404 bcopy((char *)hp->h_addr, (char *)&addr->sin_addr,
405 sizeof(addr->sin_addr));
408 if (rtmsg(RTM_GET) < 0) {
412 addr = (struct sockaddr_inarp *)(rtm + 1);
413 sdl = (struct sockaddr_dl *)(ROUNDUP(addr->sin_len) + (char *)addr);
414 if (addr->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
415 if (sdl->sdl_family == AF_LINK &&
416 (rtm->rtm_flags & RTF_LLINFO) &&
417 !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) {
418 case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
419 case IFT_ISO88024: case IFT_ISO88025: case IFT_L2VLAN:
423 if (sin_m.sin_other & SIN_PROXY) {
424 fprintf(stderr, "delete: can't locate %s\n",host);
427 sin_m.sin_other = SIN_PROXY;
431 if (sdl->sdl_family != AF_LINK) {
432 printf("cannot locate %s\n", host);
435 if (aflag && (rtm->rtm_flags & RTF_STATIC)) {
438 if (rtmsg(RTM_CHANGE) == 0) {
439 printf("%s (%s) cleared\n",
440 host, inet_ntoa(addr->sin_addr));
444 if (rtmsg(RTM_DELETE) == 0) {
445 printf("%s (%s) deleted\n",
446 host, inet_ntoa(addr->sin_addr));
454 * Search the arp table and do some action on matching entries
457 search(u_long addr, void (*action)(struct sockaddr_dl *sdl,
458 struct sockaddr_inarp *sin, struct rt_msghdr *rtm))
463 char *lim, *buf, *next;
464 struct rt_msghdr *rtm;
465 struct sockaddr_inarp *sin2;
466 struct sockaddr_dl *sdl;
472 mib[4] = NET_RT_FLAGS;
480 if (sysctl(mib, miblen, NULL, &needed, NULL, 0) < 0)
481 errx(1, "route-sysctl-estimate");
482 if ((buf = malloc(needed)) == NULL)
484 if (sysctl(mib, miblen, buf, &needed, NULL, 0) < 0)
485 errx(1, "actual retrieval of routing table");
487 for (next = buf; next < lim; next += rtm->rtm_msglen) {
488 rtm = (struct rt_msghdr *)next;
489 sin2 = (struct sockaddr_inarp *)(rtm + 1);
490 sdl = (struct sockaddr_dl *)((char *)sin2 + ROUNDUP(sin2->sin_len));
492 if (addr != sin2->sin_addr.s_addr)
496 (*action)(sdl, sin2, rtm);
502 * Display an arp entry
505 print_entry(struct sockaddr_dl *sdl,
506 struct sockaddr_inarp *addr, struct rt_msghdr *rtm)
510 char ifname[IF_NAMESIZE];
513 hp = gethostbyaddr(&(addr->sin_addr), sizeof(addr->sin_addr),
521 if (h_errno == TRY_AGAIN)
524 printf("%s (%s) at ", host, inet_ntoa(addr->sin_addr));
526 printf("%s", ether_ntoa((struct ether_addr *)LLADDR(sdl)));
528 printf("(incomplete)");
529 if (if_indextoname(sdl->sdl_index, ifname) != NULL)
530 printf(" on %s", ifname);
531 if (rtm->rtm_rmx.rmx_expire == 0)
532 printf(" permanent");
533 if (addr->sin_other & SIN_PROXY)
534 printf(" published (proxy only)");
535 if (rtm->rtm_addrs & RTA_NETMASK) {
536 addr = (struct sockaddr_inarp *)
537 (ROUNDUP(sdl->sdl_len) + (char *)sdl);
538 if (addr->sin_addr.s_addr == 0xffffffff)
539 printf(" published");
540 if (addr->sin_len != 8)
543 switch(sdl->sdl_type) {
545 printf(" [ethernet]");
562 nuke_entry(struct sockaddr_dl *sdl __unused,
563 struct sockaddr_inarp *addr, struct rt_msghdr *rtm __unused)
567 snprintf(ip, sizeof(ip), "%s", inet_ntoa(addr->sin_addr));
572 my_ether_aton(char *a, struct ether_addr *n)
574 struct ether_addr *ea;
576 if ((ea = ether_aton(a)) == NULL) {
577 warnx("invalid Ethernet address '%s'", a);
587 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
588 "usage: arp [-n] [-c cpu] hostname",
589 " arp [-n] [-c cpu] -a",
590 " arp -d hostname [pub]",
592 " arp -s hostname ether_addr [temp] [pub [only]]",
593 " arp -S hostname ether_addr [temp] [pub [only]]",
603 struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
604 char *cp = m_rtmsg.m_space;
608 if (cmd == RTM_DELETE || cmd == RTM_CHANGE)
610 bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
611 rtm->rtm_flags = flags;
612 rtm->rtm_version = RTM_VERSION;
616 errx(1, "internal wrong cmd");
618 rtm->rtm_addrs |= RTA_GATEWAY;
619 rtm->rtm_rmx.rmx_expire = expire_time;
620 rtm->rtm_inits = RTV_EXPIRE;
621 rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
625 sin_m.sin_other = SIN_PROXY;
627 rtm->rtm_addrs |= RTA_NETMASK;
628 rtm->rtm_flags &= ~RTF_HOST;
633 rtm->rtm_addrs |= RTA_DST;
635 #define NEXTADDR(w, s) \
636 if (rtm->rtm_addrs & (w)) { \
637 bcopy((char *)&s, cp, sizeof(s)); cp += ROUNDUP(sizeof(s));}
639 NEXTADDR(RTA_DST, sin_m);
640 NEXTADDR(RTA_GATEWAY, sdl_m);
641 NEXTADDR(RTA_NETMASK, so_mask);
643 rtm->rtm_msglen = cp - (char *)&m_rtmsg;
646 rtm->rtm_seq = ++seq;
648 if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
649 if (errno != ESRCH || (cmd != RTM_DELETE && cmd != RTM_CHANGE)) {
650 warn("writing to routing socket");
655 l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
656 } while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
658 warn("read from routing socket");
663 * get_ether_addr - get the hardware address of an interface on the
664 * the same subnet as ipaddr.
669 get_ether_addr(u_int32_t ipaddr, struct ether_addr *hwaddr)
671 struct ifreq *ifr, *ifend, *ifp;
673 struct sockaddr_dl *dla;
676 struct ifreq ifs[MAX_IFS];
679 sock = socket(AF_INET, SOCK_DGRAM, 0);
683 ifc.ifc_len = sizeof(ifs);
685 if (ioctl(sock, SIOCGIFCONF, &ifc) < 0) {
686 warnx("ioctl(SIOCGIFCONF)");
692 * Scan through looking for an interface with an Internet
693 * address on the same subnet as `ipaddr'.
695 ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
696 for (ifr = ifc.ifc_req; ifr < ifend; ) {
697 if (ifr->ifr_addr.sa_family == AF_INET) {
698 ina = ((struct sockaddr_in *)
699 &ifr->ifr_addr)->sin_addr.s_addr;
700 strncpy(ifreq.ifr_name, ifr->ifr_name,
701 sizeof(ifreq.ifr_name));
703 * Check that the interface is up,
704 * and not point-to-point or loopback.
706 if (ioctl(sock, SIOCGIFFLAGS, &ifreq) < 0)
708 if ((ifreq.ifr_flags &
709 (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|
710 IFF_LOOPBACK|IFF_NOARP))
711 != (IFF_UP|IFF_BROADCAST))
714 * Get its netmask and check that it's on
717 if (ioctl(sock, SIOCGIFNETMASK, &ifreq) < 0)
719 mask = ((struct sockaddr_in *)
720 &ifreq.ifr_addr)->sin_addr.s_addr;
721 if ((ipaddr & mask) != (ina & mask))
726 ifr = (struct ifreq *) ((char *)&ifr->ifr_addr
727 + MAX(ifr->ifr_addr.sa_len, sizeof(ifr->ifr_addr)));
736 * Now scan through again looking for a link-level address
737 * for this interface.
740 for (ifr = ifc.ifc_req; ifr < ifend; ) {
741 if (strcmp(ifp->ifr_name, ifr->ifr_name) == 0
742 && ifr->ifr_addr.sa_family == AF_LINK) {
744 * Found the link-level address - copy it out
746 dla = (struct sockaddr_dl *) &ifr->ifr_addr;
747 memcpy(hwaddr, LLADDR(dla), dla->sdl_alen);
749 printf("using interface %s for proxy with address ",
751 printf("%s\n", ether_ntoa(hwaddr));
752 return(dla->sdl_alen);
754 ifr = (struct ifreq *) ((char *)&ifr->ifr_addr
755 + MAX(ifr->ifr_addr.sa_len, sizeof(ifr->ifr_addr)));