1 /* vi: set sw=4 ts=4: */
3 * arp.c - Manipulate the system ARP cache
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version
8 * 2 of the License, or (at your option) any later version.
10 * Author: Fred N. van Kempen, <waltje at uwalt.nl.mugnet.org>
11 * Busybox port: Paul van Gool <pvangool at mimotech.com>
13 * modified for getopt32 by Arne Bernin <arne [at] alamut.de>
16 //usage:#define arp_trivial_usage
17 //usage: "\n[-vn] [-H HWTYPE] [-i IF] -a [HOSTNAME]"
18 //usage: "\n[-v] [-i IF] -d HOSTNAME [pub]"
19 //usage: "\n[-v] [-H HWTYPE] [-i IF] -s HOSTNAME HWADDR [temp]"
20 //usage: "\n[-v] [-H HWTYPE] [-i IF] -s HOSTNAME HWADDR [netmask MASK] pub"
21 //usage: "\n[-v] [-H HWTYPE] [-i IF] -Ds HOSTNAME IFACE [netmask MASK] pub"
22 //usage:#define arp_full_usage "\n\n"
23 //usage: "Manipulate ARP cache\n"
24 //usage: "\n -a Display (all) hosts"
25 //usage: "\n -d Delete ARP entry"
26 //usage: "\n -s Set new entry"
27 //usage: "\n -v Verbose"
28 //usage: "\n -n Don't resolve names"
29 //usage: "\n -i IF Network interface"
30 //usage: "\n -D Read HWADDR from IFACE"
31 //usage: "\n -A,-p AF Protocol family"
32 //usage: "\n -H HWTYPE Hardware address type"
35 #include "inet_common.h"
37 #include <arpa/inet.h>
39 #include <net/if_arp.h>
40 #include <netinet/ether.h>
41 #include <netpacket/packet.h>
45 #define DFLT_AF "inet"
46 #define DFLT_HW "ether"
56 ARP_OPT_n = (1 << 7), /* do not resolve addresses */
57 ARP_OPT_D = (1 << 8), /* HW-address is devicename */
59 ARP_OPT_v = (1 << 10) * DEBUG, /* debugging output flag */
63 sockfd = 3, /* active socket descriptor */
67 const struct aftype *ap; /* current address family */
68 const struct hwtype *hw; /* current hardware type */
69 const char *device; /* current device */
70 smallint hw_set; /* flag if hw-type was set (-H) */
73 #define G (*(struct globals*)&bb_common_bufsiz1)
76 #define device (G.device )
77 #define hw_set (G.hw_set )
78 #define INIT_G() do { \
83 static const char options[] ALIGN1 =
93 /* Delete an entry from the ARP cache. */
94 /* Called only from main, once */
95 static int arp_del(char **args)
103 memset(&req, 0, sizeof(req));
105 /* Resolve the host name. */
107 if (ap->input(host, &sa) < 0) {
108 bb_herror_msg_and_die("%s", host);
111 /* If a host has more than one address, use the correct one! */
112 memcpy(&req.arp_pa, &sa, sizeof(struct sockaddr));
115 req.arp_ha.sa_family = hw->type;
117 req.arp_flags = ATF_PERM;
119 while (*args != NULL) {
120 switch (index_in_strings(options, *args)) {
130 req.arp_flags &= ~ATF_PERM;
133 case 3: /* "trail" */
134 req.arp_flags |= ATF_USETRAILERS;
137 case 4: /* "dontpub" */
138 #ifdef HAVE_ATF_DONTPUB
139 req.arp_flags |= ATF_DONTPUB;
141 bb_error_msg("feature ATF_DONTPUB is not supported");
146 #ifdef HAVE_ATF_MAGIC
147 req.arp_flags |= ATF_MAGIC;
149 bb_error_msg("feature ATF_MAGIC is not supported");
159 case 7: /* "netmask" */
162 if (strcmp(*args, "255.255.255.255") != 0) {
164 if (ap->input(host, &sa) < 0) {
165 bb_herror_msg_and_die("%s", host);
167 memcpy(&req.arp_netmask, &sa, sizeof(struct sockaddr));
168 req.arp_flags |= ATF_NETMASK;
180 strncpy(req.arp_dev, device, sizeof(req.arp_dev));
184 /* Call the kernel. */
186 if (option_mask32 & ARP_OPT_v)
187 bb_error_msg("SIOCDARP(nopub)");
188 err = ioctl(sockfd, SIOCDARP, &req);
190 if (errno == ENXIO) {
193 printf("No ARP entry for %s\n", host);
196 bb_perror_msg_and_die("SIOCDARP(priv)");
199 if ((flags & 1) && err) {
201 req.arp_flags |= ATF_PUBL;
202 if (option_mask32 & ARP_OPT_v)
203 bb_error_msg("SIOCDARP(pub)");
204 if (ioctl(sockfd, SIOCDARP, &req) < 0) {
205 if (errno == ENXIO) {
206 printf("No ARP entry for %s\n", host);
209 bb_perror_msg_and_die("SIOCDARP(pub)");
215 /* Get the hardware address to a specified interface name */
216 static void arp_getdevhw(char *ifname, struct sockaddr *sa)
219 const struct hwtype *xhw;
221 strcpy(ifr.ifr_name, ifname);
222 ioctl_or_perror_and_die(sockfd, SIOCGIFHWADDR, &ifr,
223 "can't get HW-Address for '%s'", ifname);
224 if (hw_set && (ifr.ifr_hwaddr.sa_family != hw->type)) {
225 bb_error_msg_and_die("protocol type mismatch");
227 memcpy(sa, &(ifr.ifr_hwaddr), sizeof(struct sockaddr));
229 if (option_mask32 & ARP_OPT_v) {
230 xhw = get_hwntype(ifr.ifr_hwaddr.sa_family);
231 if (!xhw || !xhw->print) {
232 xhw = get_hwntype(-1);
234 bb_error_msg("device '%s' has HW address %s '%s'",
236 xhw->print((unsigned char *) &ifr.ifr_hwaddr.sa_data));
240 /* Set an entry in the ARP cache. */
241 /* Called only from main, once */
242 static int arp_set(char **args)
249 memset(&req, 0, sizeof(req));
252 if (ap->input(host, &sa) < 0) {
253 bb_herror_msg_and_die("%s", host);
255 /* If a host has more than one address, use the correct one! */
256 memcpy(&req.arp_pa, &sa, sizeof(struct sockaddr));
258 /* Fetch the hardware address. */
260 bb_error_msg_and_die("need hardware address");
262 if (option_mask32 & ARP_OPT_D) {
263 arp_getdevhw(*args++, &req.arp_ha);
265 if (hw->input(*args++, &req.arp_ha) < 0) {
266 bb_error_msg_and_die("invalid hardware address");
270 /* Check out any modifiers. */
271 flags = ATF_PERM | ATF_COM;
272 while (*args != NULL) {
273 switch (index_in_strings(options, *args)) {
286 case 3: /* "trail" */
287 flags |= ATF_USETRAILERS;
290 case 4: /* "dontpub" */
291 #ifdef HAVE_ATF_DONTPUB
292 flags |= ATF_DONTPUB;
294 bb_error_msg("feature ATF_DONTPUB is not supported");
299 #ifdef HAVE_ATF_MAGIC
302 bb_error_msg("feature ATF_MAGIC is not supported");
312 case 7: /* "netmask" */
315 if (strcmp(*args, "255.255.255.255") != 0) {
317 if (ap->input(host, &sa) < 0) {
318 bb_herror_msg_and_die("%s", host);
320 memcpy(&req.arp_netmask, &sa, sizeof(struct sockaddr));
321 flags |= ATF_NETMASK;
331 /* Fill in the remainder of the request. */
332 req.arp_flags = flags;
334 strncpy(req.arp_dev, device, sizeof(req.arp_dev));
336 /* Call the kernel. */
337 if (option_mask32 & ARP_OPT_v)
338 bb_error_msg("SIOCSARP()");
339 xioctl(sockfd, SIOCSARP, &req);
344 /* Print the contents of an ARP request block. */
346 arp_disp(const char *name, char *ip, int type, int arp_flags,
347 char *hwa, char *mask, char *dev)
349 static const int arp_masks[] = {
351 #ifdef HAVE_ATF_MAGIC
354 #ifdef HAVE_ATF_DONTPUB
359 static const char arp_labels[] ALIGN1 = "PERM\0""PUP\0"
360 #ifdef HAVE_ATF_MAGIC
363 #ifdef HAVE_ATF_DONTPUB
369 const struct hwtype *xhw;
371 xhw = get_hwntype(type);
373 xhw = get_hwtype(DFLT_HW);
375 printf("%s (%s) at ", name, ip);
377 if (!(arp_flags & ATF_COM)) {
378 if (arp_flags & ATF_PUBL)
381 printf("<incomplete> ");
383 printf("%s [%s] ", hwa, xhw->name);
386 if (arp_flags & ATF_NETMASK)
387 printf("netmask %s ", mask);
389 print_flags_separated(arp_masks, arp_labels, arp_flags, " ");
390 printf(" on %s\n", dev);
393 /* Display the contents of the ARP cache in the kernel. */
394 /* Called only from main, once */
395 static int arp_show(char *name)
398 const char *hostname;
403 unsigned entries = 0, shown = 0;
412 /* Resolve the host name. */
413 if (ap->input(name, &sa) < 0) {
414 bb_herror_msg_and_die("%s", name);
416 host = xstrdup(ap->sprint(&sa, 1));
418 fp = xfopen_for_read("/proc/net/arp");
419 /* Bypass header -- read one line */
420 fgets(line, sizeof(line), fp);
422 /* Read the ARP cache entries. */
423 while (fgets(line, sizeof(line), fp)) {
425 mask[0] = '-'; mask[1] = '\0';
426 dev[0] = '-'; dev[1] = '\0';
427 /* All these strings can't overflow
428 * because fgets above reads limited amount of data */
429 num = sscanf(line, "%s 0x%x 0x%x %s %s %s\n",
430 ip, &type, &flags, hwa, mask, dev);
435 /* if the user specified hw-type differs, skip it */
436 if (hw_set && (type != hw->type))
439 /* if the user specified address differs, skip it */
440 if (host && strcmp(ip, host) != 0)
443 /* if the user specified device differs, skip it */
444 if (device[0] && strcmp(dev, device) != 0)
448 /* This IS ugly but it works -be */
450 if (!(option_mask32 & ARP_OPT_n)) {
451 if (ap->input(ip, &sa) < 0)
454 hostname = ap->sprint(&sa, (option_mask32 & ARP_OPT_n) | 0x8000);
455 if (strcmp(hostname, ip) == 0)
459 arp_disp(hostname, ip, type, flags, hwa, mask, dev);
461 if (option_mask32 & ARP_OPT_v)
462 printf("Entries: %d\tSkipped: %d\tFound: %d\n",
463 entries, entries - shown, shown);
466 if (hw_set || host || device[0])
467 printf("No match found in %d entries\n", entries);
469 if (ENABLE_FEATURE_CLEAN_UP) {
476 int arp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
477 int arp_main(int argc UNUSED_PARAM, char **argv)
480 const char *protocol;
485 xmove_fd(xsocket(AF_INET, SOCK_DGRAM, 0), sockfd);
487 ap = get_aftype(DFLT_AF);
488 /* Defaults are always supported */
490 // bb_error_msg_and_die("%s: %s not supported", DFLT_AF, "address family");
491 hw = get_hwtype(DFLT_HW);
493 // bb_error_msg_and_die("%s: %s not supported", DFLT_HW, "hardware type");
495 opts = getopt32(argv, "A:p:H:t:i:adnDsv", &protocol, &protocol,
496 &hw_type, &hw_type, &device);
498 if (opts & (ARP_OPT_A | ARP_OPT_p)) {
499 ap = get_aftype(protocol);
501 bb_error_msg_and_die("%s: unknown %s", protocol, "address family");
503 if (opts & (ARP_OPT_H | ARP_OPT_t)) {
504 hw = get_hwtype(hw_type);
506 bb_error_msg_and_die("%s: unknown %s", hw_type, "hardware type");
509 //if (opts & ARP_OPT_i)... -i
511 if (ap->af != AF_INET) {
512 bb_error_msg_and_die("%s: kernel only supports 'inet'", ap->name);
515 bb_error_msg_and_die("%s: %s without ARP support",
516 hw->name, "hardware type");
519 /* Now see what we have to do here... */
520 if (opts & (ARP_OPT_d | ARP_OPT_s)) {
522 bb_error_msg_and_die("need host name");
523 if (opts & ARP_OPT_s)
524 return arp_set(argv);
525 return arp_del(argv);
528 //if (opts & ARP_OPT_a) - default
529 return arp_show(argv[0]);