2 * arp This file contains an implementation of the command
3 * that maintains the kernel's ARP cache. It is derived
4 * from Berkeley UNIX arp(8), but cleaner and with sup-
5 * port for devices other than Ethernet.
7 * NET-TOOLS A collection of programs that form the base set of the
8 * NET-3 Networking Distribution for the LINUX operating
11 * Version: $Id: arp.c,v 1.9 1998/11/15 20:07:31 freitag Exp $
13 * Maintainer: Bernd 'eckes' Eckenfels, <net-tools@lina.inka.de>
15 * Author: Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
18 * (based on work from Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>)
19 * Alan Cox : modified for NET3
20 * Andrew Tridgell : proxy arp netmasks
21 * Bernd Eckenfels : -n option
22 * Bernd Eckenfels : Use only /proc for display
23 * {1.60} Bernd Eckenfels : new arpcode (-i) for 1.3.42 but works
25 * {1.61} Bernd Eckenfels : more verbose messages
26 * {1.62} Bernd Eckenfels : check -t for hw adresses and try to
27 * explain EINVAL (jeff)
28 *970125 {1.63} Bernd Eckenfels : -a print hardwarename instead of tiltle
29 *970201 {1.64} Bernd Eckenfels : net-features.h support
30 *970203 {1.65} Bernd Eckenfels : "#define" in "#if",
31 * -H|-A additional to -t|-p
32 *970214 {1.66} Bernd Eckenfels : Fix optarg required for -H and -A
33 *970412 {1.67} Bernd Eckenfels : device=""; is default
34 *970514 {1.68} Bernd Eckenfels : -N and -D
35 *970517 {1.69} Bernd Eckenfels : usage() fixed
36 *970622 {1.70} Bernd Eckenfels : arp -d priv
37 *970106 {1.80} Bernd Eckenfels : new syntax without -D and with "dev <If>",
38 * ATF_MAGIC, ATF_DONTPUB support.
39 * Typo fix (Debian Bug#5728 Giuliano Procida)
40 *970803 {1.81} Bernd Eckenfels : removed junk comment line 1
41 *970925 {1.82} Bernd Eckenfels : include fix for libc6
42 *980213 (1.83) Phil Blundell: set ATF_COM on new entries
43 *980629 (1.84) Arnaldo Carvalho de Melo: gettext instead of catgets
46 * This program is free software; you can redistribute it
47 * and/or modify it under the terms of the GNU General
48 * Public License as published by the Free Software
49 * Foundation; either version 2 of the License, or (at
50 * your option) any later version.
52 #include <sys/types.h>
53 #include <sys/socket.h>
54 #include <sys/ioctl.h>
56 /* #include <linux/netdevice.h> */
57 /* #include <linux/if_arp.h> */
58 #include <net/if_arp.h>
67 #include "net-support.h"
68 #include "pathnames.h"
73 #define DFLT_AF "inet"
74 #define DFLT_HW "ether"
77 #include "lib/net-features.h"
79 char *Release = RELEASE, *Version = "arp 1.84 (1998-06-29)";
81 int opt_n = 0; /* do not resolve addresses */
82 int opt_N = 0; /* use symbolic names */
83 int opt_v = 0; /* debugging output flag */
84 int opt_D = 0; /* HW-address is devicename */
85 int opt_e = 0; /* 0=BSD output, 1=new linux */
86 int opt_a = 0; /* all entries, substring match */
87 struct aftype *ap; /* current address family */
88 struct hwtype *hw; /* current hardware type */
89 int sockfd = 0; /* active socket descriptor */
90 int hw_set = 0; /* flag if hw-type was set (-H) */
91 char device[16] = ""; /* current device */
92 static void usage(void);
94 /* Delete an entry from the ARP cache. */
95 static int arp_del(char **args)
103 memset((char *) &req, 0, sizeof(req));
105 /* Resolve the host name. */
107 fprintf(stderr, _("arp: need host name\n"));
110 host[(sizeof host) - 1] = 0;
111 strncpy(host, *args, (sizeof host) - 1);
112 if (ap->input(0, host, &sa) < 0) {
116 /* If a host has more than one address, use the correct one! */
117 memcpy((char *) &req.arp_pa, (char *) &sa, sizeof(struct sockaddr));
120 req.arp_ha.sa_family = hw->type;
122 req.arp_flags = ATF_PERM;
124 while (*args != NULL) {
126 fprintf(stderr, "args=%s\n", *args);
127 if (!strcmp(*args, "pub")) {
132 if (!strcmp(*args, "priv")) {
137 if (!strcmp(*args, "temp")) {
138 req.arp_flags &= ~ATF_PERM;
142 if (!strcmp(*args, "trail")) {
143 req.arp_flags |= ATF_USETRAILERS;
147 if (!strcmp(*args, "dontpub")) {
148 #ifdef HAVE_ATF_DONTPUB
149 req.arp_flags |= ATF_DONTPUB;
151 ENOSUPP("arp", "ATF_DONTPUB");
156 if (!strcmp(*args, "auto")) {
157 #ifdef HAVE_ATF_MAGIC
158 req.arp_flags |= ATF_MAGIC;
160 ENOSUPP("arp", "ATF_MAGIC");
165 if (!strcmp(*args, "dev")) {
168 strncpy(device, *args, sizeof(device) - 1);
169 device[sizeof(device) - 1] = '\0';
173 if (!strcmp(*args, "netmask")) {
176 if (strcmp(*args, "255.255.255.255") != 0) {
178 if (ap->input(0, host, &sa) < 0) {
182 memcpy((char *) &req.arp_netmask, (char *) &sa,
183 sizeof(struct sockaddr));
184 req.arp_flags |= ATF_NETMASK;
194 strcpy(req.arp_dev, device);
198 /* Call the kernel. */
201 fprintf(stderr, "arp: SIOCDARP(nopub)\n");
202 if ((err = ioctl(sockfd, SIOCDARP, &req) < 0)) {
203 if (errno == ENXIO) {
206 printf(_("No ARP entry for %s\n"), host);
209 perror("SIOCDARP(priv)");
213 if ((flags & 1) && (err)) {
215 req.arp_flags |= ATF_PUBL;
217 fprintf(stderr, "arp: SIOCDARP(pub)\n");
218 if (ioctl(sockfd, SIOCDARP, &req) < 0) {
219 if (errno == ENXIO) {
220 printf(_("No ARP entry for %s\n"), host);
223 perror("SIOCDARP(pub)");
230 /* Get the hardware address to a specified interface name */
231 static int arp_getdevhw(char *ifname, struct sockaddr *sa, struct hwtype *hw)
236 strcpy(ifr.ifr_name, ifname);
237 if (ioctl(sockfd, SIOCGIFHWADDR, &ifr) < 0) {
238 fprintf(stderr, "arp: cant get HW-Address for `%s': %s.\n", ifname, strerror(errno));
241 if (hw && (ifr.ifr_hwaddr.sa_family != hw->type)) {
242 fprintf(stderr, "arp: protocol type missmatch.\n");
245 memcpy((char *) sa, (char *) &(ifr.ifr_hwaddr), sizeof(struct sockaddr));
248 if (!(xhw = get_hwntype(ifr.ifr_hwaddr.sa_family)) || (xhw->sprint == 0)) {
249 xhw = get_hwntype(-1);
251 fprintf(stderr, "arp: device `%s' has HW address %s `%s'.\n", ifname, xhw->name, xhw->sprint(&ifr.ifr_hwaddr));
256 /* Set an entry in the ARP cache. */
257 static int arp_set(char **args)
264 memset((char *) &req, 0, sizeof(req));
266 /* Resolve the host name. */
268 fprintf(stderr, _("arp: need host name\n"));
271 host[(sizeof host) - 1] = 0;
272 strncpy(host, *args++, (sizeof host) - 1);
273 if (ap->input(0, host, &sa) < 0) {
277 /* If a host has more than one address, use the correct one! */
278 memcpy((char *) &req.arp_pa, (char *) &sa, sizeof(struct sockaddr));
280 /* Fetch the hardware address. */
282 fprintf(stderr, _("arp: need hardware address\n"));
286 if (arp_getdevhw(*args++, &req.arp_ha, hw_set ? hw : NULL) < 0)
289 if (hw->input(*args++, &req.arp_ha) < 0) {
290 fprintf(stderr, _("arp: invalid hardware address\n"));
295 /* Check out any modifiers. */
296 flags = ATF_PERM | ATF_COM;
297 while (*args != NULL) {
298 if (!strcmp(*args, "temp")) {
303 if (!strcmp(*args, "pub")) {
308 if (!strcmp(*args, "priv")) {
313 if (!strcmp(*args, "trail")) {
314 flags |= ATF_USETRAILERS;
318 if (!strcmp(*args, "dontpub")) {
319 #ifdef HAVE_ATF_DONTPUB
320 flags |= ATF_DONTPUB;
322 ENOSUPP("arp", "ATF_DONTPUB");
327 if (!strcmp(*args, "auto")) {
328 #ifdef HAVE_ATF_MAGIC
331 ENOSUPP("arp", "ATF_MAGIC");
336 if (!strcmp(*args, "dev")) {
339 strncpy(device, *args, sizeof(device) - 1);
340 device[sizeof(device) - 1] = '\0';
344 if (!strcmp(*args, "netmask")) {
347 if (strcmp(*args, "255.255.255.255") != 0) {
349 if (ap->input(0, host, &sa) < 0) {
353 memcpy((char *) &req.arp_netmask, (char *) &sa,
354 sizeof(struct sockaddr));
355 flags |= ATF_NETMASK;
363 /* Fill in the remainder of the request. */
364 req.arp_flags = flags;
366 strcpy(req.arp_dev, device);
368 /* Call the kernel. */
370 fprintf(stderr, "arp: SIOCSARP()\n");
371 if (ioctl(sockfd, SIOCSARP, &req) < 0) {
372 if (errno != EINVAL) {
381 /* Process an EtherFile */
382 static int arp_file(char *name)
389 if ((fp = fopen(name, "r")) == NULL) {
390 fprintf(stderr, _("arp: cannot open etherfile %s !\n"), name);
393 /* Read the lines in the file. */
395 while (fgets(buff, sizeof(buff), fp) != (char *) NULL) {
398 fprintf(stderr, ">> %s", buff);
399 if ((sp = strchr(buff, '\n')) != (char *) NULL)
401 if (buff[0] == '#' || buff[0] == '\0')
404 argc = getargs(buff, args);
406 fprintf(stderr, _("arp: format error on line %u of etherfile %s !\n"),
410 if (arp_set(args) != 0)
411 fprintf(stderr, _("arp: cannot set entry on line %u of etherfile %s !\n"),
420 /* Print the contents of an ARP request block. */
421 static void arp_disp_2(char *name, int type, int arp_flags, char *hwa, char *mask, char *dev)
423 static int title = 0;
427 xhw = get_hwntype(type);
429 xhw = get_hwtype(DFLT_HW);
432 printf(_("Address\t\t\tHWtype\tHWaddress\t Flags Mask\t\t Iface\n"));
434 /* Setup the flags. */
436 if (arp_flags & ATF_COM)
438 if (arp_flags & ATF_PERM)
440 if (arp_flags & ATF_PUBL)
442 #ifdef HAVE_ATF_MAGIC
443 if (arp_flags & ATF_MAGIC)
446 #ifdef HAVE_ATF_DONTPUB
447 if (arp_flags & ATF_DONTPUB)
450 if (arp_flags & ATF_USETRAILERS)
453 if (!(arp_flags & ATF_NETMASK))
456 printf("%-23.23s\t", name);
458 if (!(arp_flags & ATF_COM)) {
459 if (arp_flags & ATF_PUBL)
460 printf("%-8.8s%-20.20s", "*", "*");
462 printf("%-8.8s%-20.20s", "", "(incomplete)");
464 printf("%-8.8s%-20.20s", xhw->name, hwa);
467 printf("%-6.6s%-15.15s %s\n", flags, mask, dev);
470 /* Print the contents of an ARP request block. */
471 static void arp_disp(char *name, char *ip, int type, int arp_flags, char *hwa, char *mask, char *dev)
475 xhw = get_hwntype(type);
477 xhw = get_hwtype(DFLT_HW);
479 printf("%s (%s) at ", name, ip);
481 if (!(arp_flags & ATF_COM)) {
482 if (arp_flags & ATF_PUBL)
485 printf("<incomplete> ");
487 printf("%s [%s] ", hwa, xhw->name);
490 if (arp_flags & ATF_NETMASK)
491 printf("netmask %s ", mask);
493 if (arp_flags & ATF_PERM)
495 if (arp_flags & ATF_PUBL)
497 #ifdef HAVE_ATF_MAGIC
498 if (arp_flags & ATF_MAGIC)
501 #ifdef HAVE_ATF_DONTPUB
502 if (arp_flags & ATF_DONTPUB)
505 if (arp_flags & ATF_USETRAILERS)
508 printf("on %s\n", dev);
512 /* Display the contents of the ARP cache in the kernel. */
513 static int arp_show(char *name)
525 int num, entries = 0, showed = 0;
530 /* Resolve the host name. */
531 host[(sizeof host) - 1] = 0;
532 strncpy(host, name, (sizeof host) - 1);
533 if (ap->input(0, host, &sa) < 0) {
537 strcpy(host, ap->sprint(&sa, 1));
539 /* Open the PROCps kernel table. */
540 if ((fp = fopen(_PATH_PROCNET_ARP, "r")) == NULL) {
541 perror(_PATH_PROCNET_ARP);
544 /* Bypass header -- read until newline */
545 if (fgets(line, sizeof(line), fp) != (char *) NULL) {
548 /* Read the ARP cache entries. */
549 for (; fgets(line, sizeof(line), fp);) {
550 num = sscanf(line, "%s 0x%x 0x%x %100s %100s %100s\n",
551 ip, &type, &flags, hwa, mask, dev);
556 /* if the user specified hw-type differs, skip it */
557 if (hw_set && (type != hw->type))
560 /* if the user specified address differs, skip it */
561 if (host[0] && strcmp(ip, host))
564 /* if the user specified device differs, skip it */
565 if (device[0] && strcmp(dev, device))
569 /* This IS ugly but it works -be */
573 if (ap->input(0, ip, &sa) < 0)
576 hostname = ap->sprint(&sa, opt_n | 0x8000);
577 if (strcmp(hostname, ip) == 0)
582 arp_disp_2(hostname[0] == '?' ? ip : hostname, type, flags, hwa, mask, dev);
584 arp_disp(hostname, ip, type, flags, hwa, mask, dev);
588 printf(_("Entries: %d\tSkipped: %d\tFound: %d\n"), entries, entries - showed, showed);
591 if (host[0] && !opt_a)
592 printf("%s (%s) -- no entry\n", name, host);
593 else if (hw_set || host[0] || device[0]) {
594 printf(_("arp: in %d entries no match found.\n"), entries);
601 static void version(void)
603 fprintf(stderr, "%s\n%s\n%s\n", Release, Version, Features);
607 static void usage(void)
609 fprintf(stderr, _("Usage: arp [-vn] [-H type] [-i if] -a [hostname]\n"));
610 fprintf(stderr, _(" arp [-v] [-i if] -d hostname [pub][nopub]\n"));
611 fprintf(stderr, _(" arp [-v] [-H type] [-i if] -s hostname hw_addr [temp][nopub]\n"));
612 fprintf(stderr, _(" arp [-v] [-H type] [-i if] -s hostname hw_addr [netmask nm] pub\n"));
613 fprintf(stderr, _(" arp [-v] [-H type] [-i if] -Ds hostname if [netmask nm] pub\n"));
614 fprintf(stderr, _(" arp [-vnD] [-H type] [-i if] -f filename\n"));
618 int main(int argc, char **argv)
621 struct option longopts[] =
623 {"verbose", 0, 0, 'v'},
624 {"version", 0, 0, 'V'},
626 {"delete", 0, 0, 'd'},
628 {"numeric", 0, 0, 'n'},
630 {"protocol", 1, 0, 'A'},
631 {"hw-type", 1, 0, 'H'},
632 {"device", 0, 0, 'i'},
634 {"use-device", 0, 0, 'D'},
635 {"symbolic", 0, 0, 'N'},
640 bindtextdomain("net-tools", "/usr/share/locale");
641 textdomain("net-tools");
644 /* Initialize variables... */
645 if ((hw = get_hwtype(DFLT_HW)) == NULL) {
646 fprintf(stderr, _("%s: hardware type not supported!\n"), DFLT_HW);
649 if ((ap = get_aftype(DFLT_AF)) == NULL) {
650 fprintf(stderr, _("%s: address family not supported!\n"), DFLT_AF);
655 /* Fetch the command-line arguments. */
657 while ((i = getopt_long(argc, argv, "A:H:adfp:nsei:t:vh?DNV", longopts, &lop)) != EOF)
685 fprintf(stderr, "arp: -N not yet supported.\n");
693 ap = get_aftype(optarg);
695 fprintf(stderr, _("arp: %s: unknown address family.\n"),
702 hw = get_hwtype(optarg);
704 fprintf(stderr, _("arp: %s: unknown hardware type.\n"),
711 strncpy(device, optarg, sizeof(device) - 1);
712 device[sizeof(device) - 1] = '\0';
723 if (ap->af != AF_INET) {
724 fprintf(stderr, _("arp: %s: kernel only supports 'inet'.\n"),
729 fprintf(stderr, _("arp: %s: hardware type without ARP support.\n"),
733 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
737 /* Now see what we have to do here... */
741 what = arp_show(argv[optind]);
744 case 1: /* show an ARP entry in the cache */
745 what = arp_show(argv[optind]);
748 case 2: /* process an EtherFile */
749 what = arp_file(argv[optind]);
752 case 3: /* delete an ARP entry from the cache */
753 what = arp_del(&argv[optind]);
756 case 4: /* set an ARP entry in the cache */
757 what = arp_set(&argv[optind]);