2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1984, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Sun Microsystems, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 static char const copyright[] =
38 "@(#) Copyright (c) 1984, 1993\n\
39 The Regents of the University of California. All rights reserved.\n";
43 static char const sccsid[] = "@(#)from: arp.c 8.2 (Berkeley) 1/2/94";
46 #include <sys/cdefs.h>
48 * arp - display, set, and delete arp table entries
51 #include <sys/param.h>
53 #include <sys/socket.h>
54 #include <sys/sockio.h>
55 #include <sys/sysctl.h>
56 #include <sys/ioctl.h>
60 #include <net/if_dl.h>
61 #include <net/if_types.h>
62 #include <net/route.h>
64 #include <netinet/in.h>
65 #include <netinet/if_ether.h>
67 #include <arpa/inet.h>
85 typedef void (action_fn)(struct sockaddr_dl *sdl, struct sockaddr_in *s_in,
86 struct rt_msghdr *rtm);
87 static void nuke_entries(uint32_t ifindex, struct in_addr addr);
88 static int print_entries(uint32_t ifindex, struct in_addr addr);
90 static int delete(char *host);
91 static void usage(void) __dead2;
92 static int set(int argc, char **argv);
93 static int get(char *host);
94 static int file(char *name);
95 static struct rt_msghdr *rtmsg(int cmd,
96 struct sockaddr_in *dst, struct sockaddr_dl *sdl);
97 static int get_ether_addr(in_addr_t ipaddr, struct ether_addr *hwaddr);
98 static int set_rtsock(struct sockaddr_in *dst, struct sockaddr_dl *sdl_m,
101 struct if_nameindex *ifnameindex;
103 struct arp_opts opts = {};
105 /* which function we're supposed to do */
112 #define SETFUNC(f) { if (func) usage(); func = (f); }
114 #define ARP_XO_VERSION "1"
117 main(int argc, char *argv[])
122 argc = xo_parse_args(argc, argv);
126 while ((ch = getopt(argc, argv, "andfsSi:")) != -1)
147 opts.rifname = optarg;
159 if (func != F_GET && !(func == F_DELETE && opts.aflag))
160 xo_errx(1, "-i not applicable to this operation");
161 if ((opts.rifindex = if_nametoindex(opts.rifname)) == 0) {
163 xo_errx(1, "interface %s does not exist",
166 xo_err(1, "if_nametoindex(%s)", opts.rifname);
175 xo_set_version(ARP_XO_VERSION);
176 xo_open_container("arp");
177 xo_open_list("arp-cache");
179 struct in_addr all_addrs = {};
180 print_entries(opts.rifindex, all_addrs);
182 xo_close_list("arp-cache");
183 xo_close_container("arp");
193 if (argc < 2 || argc > 6)
195 if (func == F_REPLACE)
196 (void)delete(argv[0]);
197 rtn = set(argc, argv) ? 1 : 0;
203 struct in_addr all_addrs = {};
204 nuke_entries(0, all_addrs);
208 rtn = delete(argv[0]);
218 if (ifnameindex != NULL)
219 if_freenameindex(ifnameindex);
225 * Process a file to set standard arp entries
232 char line[100], arg[5][50], *args[5], *p;
234 if ((fp = fopen(name, "r")) == NULL)
235 xo_err(1, "cannot open %s", name);
236 args[0] = &arg[0][0];
237 args[1] = &arg[1][0];
238 args[2] = &arg[2][0];
239 args[3] = &arg[3][0];
240 args[4] = &arg[4][0];
242 while(fgets(line, sizeof(line), fp) != NULL) {
243 if ((p = strchr(line, '#')) != NULL)
245 for (p = line; isblank(*p); p++);
246 if (*p == '\n' || *p == '\0')
248 i = sscanf(p, "%49s %49s %49s %49s %49s", arg[0], arg[1],
249 arg[2], arg[3], arg[4]);
251 xo_warnx("bad line: %s", line);
263 * Given a hostname, fills up a (static) struct sockaddr_in with
264 * the address of the host and returns a pointer to the
271 static struct sockaddr_in reply;
273 bzero(&reply, sizeof(reply));
274 reply.sin_len = sizeof(reply);
275 reply.sin_family = AF_INET;
276 reply.sin_addr.s_addr = inet_addr(host);
277 if (reply.sin_addr.s_addr == INADDR_NONE) {
278 if (!(hp = gethostbyname(host))) {
279 xo_warnx("%s: %s", host, hstrerror(h_errno));
282 bcopy((char *)hp->h_addr, (char *)&reply.sin_addr,
283 sizeof reply.sin_addr);
288 int valid_type(int type);
290 * Returns true if the type is a valid one for ARP.
312 * Set an individual arp entry
315 set(int argc, char **argv)
317 struct sockaddr_in *dst; /* what are we looking for */
318 struct ether_addr *ea;
319 char *host = argv[0], *eaddr = argv[1];
320 struct sockaddr_dl sdl_m;
325 bzero(&sdl_m, sizeof(sdl_m));
326 sdl_m.sdl_len = sizeof(sdl_m);
327 sdl_m.sdl_family = AF_LINK;
333 if (strcmp(argv[0], "temp") == 0) {
335 size_t len = sizeof(max_age);
337 if (sysctlbyname("net.link.ether.inet.max_age",
338 &max_age, &len, NULL, 0) != 0)
339 xo_err(1, "sysctlbyname");
340 opts.expire_time = max_age;
341 } else if (strcmp(argv[0], "pub") == 0) {
342 opts.flags |= RTF_ANNOUNCE;
343 if (argc && strcmp(argv[1], "only") == 0) {
345 * Compatibility: in pre FreeBSD 8 times
346 * the "only" keyword used to mean that
347 * an ARP entry should be announced, but
348 * not installed into routing table.
352 } else if (strcmp(argv[0], "blackhole") == 0) {
353 if (opts.flags & RTF_REJECT) {
354 xo_errx(1, "Choose one of blackhole or reject, "
357 opts.flags |= RTF_BLACKHOLE;
358 } else if (strcmp(argv[0], "reject") == 0) {
359 if (opts.flags & RTF_BLACKHOLE) {
360 xo_errx(1, "Choose one of blackhole or reject, "
363 opts.flags |= RTF_REJECT;
365 xo_warnx("Invalid parameter '%s'", argv[0]);
370 ea = (struct ether_addr *)LLADDR(&sdl_m);
371 if ((opts.flags & RTF_ANNOUNCE) && !strcmp(eaddr, "auto")) {
372 if (!get_ether_addr(dst->sin_addr.s_addr, ea)) {
373 xo_warnx("no interface found for %s",
374 inet_ntoa(dst->sin_addr));
377 sdl_m.sdl_alen = ETHER_ADDR_LEN;
379 struct ether_addr *ea1 = ether_aton(eaddr);
382 xo_warnx("invalid Ethernet address '%s'", eaddr);
386 sdl_m.sdl_alen = ETHER_ADDR_LEN;
389 #ifndef WITHOUT_NETLINK
390 return (set_nl(0, dst, &sdl_m, host));
392 return (set_rtsock(dst, &sdl_m, host));
396 #ifdef WITHOUT_NETLINK
398 set_rtsock(struct sockaddr_in *dst, struct sockaddr_dl *sdl_m, char *host)
400 struct sockaddr_in *addr;
401 struct sockaddr_dl *sdl;
402 struct rt_msghdr *rtm;
405 * In the case a proxy-arp entry is being added for
406 * a remote end point, the RTF_ANNOUNCE flag in the
407 * RTM_GET command is an indication to the kernel
408 * routing code that the interface associated with
409 * the prefix route covering the local end of the
410 * PPP link should be returned, on which ARP applies.
412 rtm = rtmsg(RTM_GET, dst, NULL);
417 addr = (struct sockaddr_in *)(rtm + 1);
418 sdl = (struct sockaddr_dl *)(SA_SIZE(addr) + (char *)addr);
420 if ((sdl->sdl_family != AF_LINK) ||
421 (rtm->rtm_flags & RTF_GATEWAY) ||
422 !valid_type(sdl->sdl_type)) {
423 xo_warnx("cannot intuit interface index and type for %s", host);
426 sdl_m->sdl_type = sdl->sdl_type;
427 sdl_m->sdl_index = sdl->sdl_index;
428 return (rtmsg(RTM_ADD, dst, sdl_m) == NULL);
433 * Display an individual arp entry
438 struct sockaddr_in *addr;
441 addr = getaddr(host);
445 xo_set_version(ARP_XO_VERSION);
446 xo_open_container("arp");
447 xo_open_list("arp-cache");
449 found = print_entries(opts.rifindex, addr->sin_addr);
452 xo_emit("{d:hostname/%s} ({d:ip-address/%s}) -- no entry",
453 host, inet_ntoa(addr->sin_addr));
455 xo_emit(" on {d:interface/%s}", opts.rifname);
459 xo_close_list("arp-cache");
460 xo_close_container("arp");
467 * Delete an arp entry
469 #ifdef WITHOUT_NETLINK
471 delete_rtsock(char *host)
473 struct sockaddr_in *addr, *dst;
474 struct rt_msghdr *rtm;
475 struct sockaddr_dl *sdl;
482 * Perform a regular entry delete first.
484 opts.flags &= ~RTF_ANNOUNCE;
486 for (;;) { /* try twice */
487 rtm = rtmsg(RTM_GET, dst, NULL);
492 addr = (struct sockaddr_in *)(rtm + 1);
493 sdl = (struct sockaddr_dl *)(SA_SIZE(addr) + (char *)addr);
496 * With the new L2/L3 restructure, the route
497 * returned is a prefix route. The important
498 * piece of information from the previous
499 * RTM_GET is the interface index. In the
500 * case of ECMP, the kernel will traverse
501 * the route group for the given entry.
503 if (sdl->sdl_family == AF_LINK &&
504 !(rtm->rtm_flags & RTF_GATEWAY) &&
505 valid_type(sdl->sdl_type) ) {
506 addr->sin_addr.s_addr = dst->sin_addr.s_addr;
511 * Regular entry delete failed, now check if there
512 * is a proxy-arp entry to remove.
514 if (opts.flags & RTF_ANNOUNCE) {
515 xo_warnx("delete: cannot locate %s", host);
519 opts.flags |= RTF_ANNOUNCE;
521 rtm->rtm_flags |= RTF_LLDATA;
522 if (rtmsg(RTM_DELETE, dst, NULL) != NULL) {
523 printf("%s (%s) deleted\n", host, inet_ntoa(addr->sin_addr));
533 #ifdef WITHOUT_NETLINK
534 return (delete_rtsock(host));
536 return (delete_nl(0, host));
542 * Search the arp table and do some action on matching entries
545 search(u_long addr, action_fn *action)
549 char *lim, *buf, *next;
550 struct rt_msghdr *rtm;
551 struct sockaddr_in *sin2;
552 struct sockaddr_dl *sdl;
553 int st, found_entry = 0;
559 mib[4] = NET_RT_FLAGS;
565 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
566 xo_err(1, "route-sysctl-estimate");
567 if (needed == 0) /* empty table */
571 buf = reallocf(buf, needed);
573 xo_errx(1, "could not reallocate memory");
574 st = sysctl(mib, 6, buf, &needed, NULL, 0);
575 if (st == 0 || errno != ENOMEM)
577 needed += needed / 8;
580 xo_err(1, "actual retrieval of routing table");
582 for (next = buf; next < lim; next += rtm->rtm_msglen) {
583 rtm = (struct rt_msghdr *)next;
584 sin2 = (struct sockaddr_in *)(rtm + 1);
585 sdl = (struct sockaddr_dl *)((char *)sin2 + SA_SIZE(sin2));
587 (opts.rifindex != sdl->sdl_index))
590 (addr != sin2->sin_addr.s_addr))
593 (*action)(sdl, sin2, rtm);
596 return (found_entry);
600 * Display an arp entry
604 print_entry(struct sockaddr_dl *sdl,
605 struct sockaddr_in *addr, struct rt_msghdr *rtm)
609 struct if_nameindex *p;
611 if (ifnameindex == NULL)
612 if ((ifnameindex = if_nameindex()) == NULL)
613 xo_err(1, "cannot retrieve interface names");
615 xo_open_instance("arp-cache");
618 hp = gethostbyaddr((caddr_t)&(addr->sin_addr),
619 sizeof addr->sin_addr, AF_INET);
626 if (h_errno == TRY_AGAIN)
629 xo_emit("{:hostname/%s} ({:ip-address/%s}) at ", host,
630 inet_ntoa(addr->sin_addr));
632 if ((sdl->sdl_type == IFT_ETHER ||
633 sdl->sdl_type == IFT_L2VLAN ||
634 sdl->sdl_type == IFT_BRIDGE) &&
635 sdl->sdl_alen == ETHER_ADDR_LEN)
636 xo_emit("{:mac-address/%s}",
637 ether_ntoa((struct ether_addr *)LLADDR(sdl)));
639 int n = sdl->sdl_nlen > 0 ? sdl->sdl_nlen + 1 : 0;
641 xo_emit("{:mac-address/%s}", link_ntoa(sdl) + n);
644 xo_emit("{d:/(incomplete)}{en:incomplete/true}");
646 for (p = ifnameindex; p && p->if_index && p->if_name; p++) {
647 if (p->if_index == sdl->sdl_index) {
648 xo_emit(" on {:interface/%s}", p->if_name);
653 if (rtm->rtm_rmx.rmx_expire == 0)
654 xo_emit("{d:/ permanent}{en:permanent/true}");
656 static struct timespec tp;
657 time_t expire_time = 0;
660 clock_gettime(CLOCK_MONOTONIC, &tp);
661 if ((expire_time = rtm->rtm_rmx.rmx_expire - tp.tv_sec) > 0)
662 xo_emit(" expires in {:expires/%d} seconds",
665 xo_emit("{d:/ expired}{en:expired/true}");
668 if (rtm->rtm_flags & RTF_ANNOUNCE)
669 xo_emit("{d:/ published}{en:published/true}");
671 switch(sdl->sdl_type) {
673 xo_emit(" [{:type/ethernet}]");
676 xo_emit(" [{:type/fddi}]");
679 xo_emit(" [{:type/atm}]");
682 xo_emit(" [{:type/vlan}]");
685 xo_emit(" [{:type/firewire}]");
688 xo_emit(" [{:type/bridge}]");
691 xo_emit(" [{:type/infiniband}]");
699 xo_close_instance("arp-cache");
703 print_entries(uint32_t ifindex, struct in_addr addr)
705 #ifndef WITHOUT_NETLINK
706 return (print_entries_nl(ifindex, addr));
708 return (search(addr.s_addr, print_entry));
717 nuke_entry(struct sockaddr_dl *sdl __unused,
718 struct sockaddr_in *addr, struct rt_msghdr *rtm)
722 if (rtm->rtm_flags & RTF_PINNED)
725 snprintf(ip, sizeof(ip), "%s", inet_ntoa(addr->sin_addr));
730 nuke_entries(uint32_t ifindex, struct in_addr addr)
732 search(addr.s_addr, nuke_entry);
738 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
739 "usage: arp [-n] [-i interface] hostname",
740 " arp [-n] [-i interface] -a",
741 " arp -d hostname [pub]",
742 " arp -d [-i interface] -a",
743 " arp -s hostname ether_addr [temp] [reject | blackhole] [pub [only]]",
744 " arp -S hostname ether_addr [temp] [reject | blackhole] [pub [only]]",
749 static struct rt_msghdr *
750 rtmsg(int cmd, struct sockaddr_in *dst, struct sockaddr_dl *sdl)
759 struct rt_msghdr m_rtm;
763 struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
764 char *cp = m_rtmsg.m_space;
766 if (s < 0) { /* first time: open socket, get pid */
767 s = socket(PF_ROUTE, SOCK_RAW, 0);
775 * XXX RTM_DELETE relies on a previous RTM_GET to fill the buffer
778 if (cmd == RTM_DELETE)
780 bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
781 rtm->rtm_flags = opts.flags;
782 rtm->rtm_version = RTM_VERSION;
786 xo_errx(1, "internal wrong cmd");
788 rtm->rtm_addrs |= RTA_GATEWAY;
789 if (opts.expire_time != 0) {
792 clock_gettime(CLOCK_MONOTONIC, &tp);
793 rtm->rtm_rmx.rmx_expire = opts.expire_time + tp.tv_sec;
795 rtm->rtm_inits = RTV_EXPIRE;
796 rtm->rtm_flags |= (RTF_HOST | RTF_STATIC | RTF_LLDATA);
799 rtm->rtm_addrs |= RTA_DST;
801 #define NEXTADDR(w, s) \
803 if ((s) != NULL && rtm->rtm_addrs & (w)) { \
804 bcopy((s), cp, sizeof(*(s))); \
809 NEXTADDR(RTA_DST, dst);
810 NEXTADDR(RTA_GATEWAY, sdl);
812 rtm->rtm_msglen = cp - (char *)&m_rtmsg;
815 rtm->rtm_seq = ++seq;
817 if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
818 if (errno != ESRCH || cmd != RTM_DELETE) {
819 xo_warn("writing to routing socket");
824 l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
825 } while (l > 0 && (rtm->rtm_type != cmd || rtm->rtm_seq != seq ||
826 rtm->rtm_pid != pid));
828 xo_warn("read from routing socket");
833 * get_ether_addr - get the hardware address of an interface on the
834 * the same subnet as ipaddr.
837 get_ether_addr(in_addr_t ipaddr, struct ether_addr *hwaddr)
839 struct ifaddrs *ifa, *ifd, *ifas = NULL;
841 struct sockaddr_dl *dla;
845 * Scan through looking for an interface with an Internet
846 * address on the same subnet as `ipaddr'.
848 if (getifaddrs(&ifas) < 0) {
849 xo_warnx("getifaddrs");
853 for (ifa = ifas; ifa != NULL; ifa = ifa->ifa_next) {
854 if (ifa->ifa_addr == NULL || ifa->ifa_netmask == NULL)
856 if (ifa->ifa_addr->sa_family != AF_INET)
859 * Check that the interface is up,
860 * and not point-to-point or loopback.
862 if ((ifa->ifa_flags &
863 (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|
864 IFF_LOOPBACK|IFF_NOARP)) != (IFF_UP|IFF_BROADCAST))
866 /* Get its netmask and check that it's on the right subnet. */
867 mask = ((struct sockaddr_in *)
868 ifa->ifa_netmask)->sin_addr.s_addr;
869 ina = ((struct sockaddr_in *)
870 ifa->ifa_addr)->sin_addr.s_addr;
871 if ((ipaddr & mask) == (ina & mask))
872 break; /* ok, we got it! */
878 * Now scan through again looking for a link-level address
879 * for this interface.
881 for (ifd = ifas; ifd != NULL; ifd = ifd->ifa_next) {
882 if (ifd->ifa_addr == NULL)
884 if (strcmp(ifa->ifa_name, ifd->ifa_name) == 0 &&
885 ifd->ifa_addr->sa_family == AF_LINK)
891 * Found the link-level address - copy it out
893 dla = (struct sockaddr_dl *)ifd->ifa_addr;
894 memcpy(hwaddr, LLADDR(dla), dla->sdl_alen);
895 printf("using interface %s for proxy with address %s\n", ifa->ifa_name,
897 retval = dla->sdl_alen;