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>
17 #include "inet_common.h"
19 #include <arpa/inet.h>
21 #include <net/if_arp.h>
22 #include <netinet/ether.h>
23 #include <netpacket/packet.h>
27 #define DFLT_AF "inet"
28 #define DFLT_HW "ether"
30 #define ARP_OPT_A (0x1)
31 #define ARP_OPT_p (0x2)
32 #define ARP_OPT_H (0x4)
33 #define ARP_OPT_t (0x8)
34 #define ARP_OPT_i (0x10)
35 #define ARP_OPT_a (0x20)
36 #define ARP_OPT_d (0x40)
37 #define ARP_OPT_n (0x80) /* do not resolve addresses */
38 #define ARP_OPT_D (0x100) /* HW-address is devicename */
39 #define ARP_OPT_s (0x200)
40 #define ARP_OPT_v (0x400 * DEBUG) /* debugging output flag */
43 static const struct aftype *ap; /* current address family */
44 static const struct hwtype *hw; /* current hardware type */
45 static int sockfd; /* active socket descriptor */
46 static smallint hw_set; /* flag if hw-type was set (-H) */
47 static const char *device = ""; /* current device */
49 static const char *const options[] = {
61 /* Delete an entry from the ARP cache. */
62 /* Called only from main, once */
63 static int arp_del(char **args)
71 memset(&req, 0, sizeof(req));
73 /* Resolve the host name. */
75 if (ap->input(host, &sa) < 0) {
76 bb_herror_msg_and_die("%s", host);
79 /* If a host has more than one address, use the correct one! */
80 memcpy(&req.arp_pa, &sa, sizeof(struct sockaddr));
83 req.arp_ha.sa_family = hw->type;
85 req.arp_flags = ATF_PERM;
87 while (*args != NULL) {
88 switch (index_in_str_array(options, *args)) {
98 req.arp_flags &= ~ATF_PERM;
101 case 3: /* "trail" */
102 req.arp_flags |= ATF_USETRAILERS;
105 case 4: /* "dontpub" */
106 #ifdef HAVE_ATF_DONTPUB
107 req.arp_flags |= ATF_DONTPUB;
109 bb_error_msg("feature ATF_DONTPUB is not supported");
114 #ifdef HAVE_ATF_MAGIC
115 req.arp_flags |= ATF_MAGIC;
117 bb_error_msg("feature ATF_MAGIC is not supported");
127 case 7: /* "netmask" */
130 if (strcmp(*args, "255.255.255.255") != 0) {
132 if (ap->input(host, &sa) < 0) {
133 bb_herror_msg_and_die("%s", host);
135 memcpy(&req.arp_netmask, &sa, sizeof(struct sockaddr));
136 req.arp_flags |= ATF_NETMASK;
148 strncpy(req.arp_dev, device, sizeof(req.arp_dev));
152 /* Call the kernel. */
154 if (option_mask32 & ARP_OPT_v)
155 bb_error_msg("SIOCDARP(nopub)");
156 err = ioctl(sockfd, SIOCDARP, &req);
158 if (errno == ENXIO) {
161 printf("No ARP entry for %s\n", host);
164 bb_perror_msg_and_die("SIOCDARP(priv)");
167 if ((flags & 1) && err) {
169 req.arp_flags |= ATF_PUBL;
170 if (option_mask32 & ARP_OPT_v)
171 bb_error_msg("SIOCDARP(pub)");
172 if (ioctl(sockfd, SIOCDARP, &req) < 0) {
173 if (errno == ENXIO) {
174 printf("No ARP entry for %s\n", host);
177 bb_perror_msg_and_die("SIOCDARP(pub)");
183 /* Get the hardware address to a specified interface name */
184 static void arp_getdevhw(char *ifname, struct sockaddr *sa,
185 const struct hwtype *hwt)
188 const struct hwtype *xhw;
190 strcpy(ifr.ifr_name, ifname);
191 if (ioctl(sockfd, SIOCGIFHWADDR, &ifr) < 0) {
192 bb_perror_msg_and_die("cant get HW-Address for '%s'", ifname);
194 if (hwt && (ifr.ifr_hwaddr.sa_family != hw->type)) {
195 bb_error_msg_and_die("protocol type mismatch");
197 memcpy(sa, &(ifr.ifr_hwaddr), sizeof(struct sockaddr));
199 if (option_mask32 & ARP_OPT_v) {
200 xhw = get_hwntype(ifr.ifr_hwaddr.sa_family);
201 if (!xhw || !xhw->print) {
202 xhw = get_hwntype(-1);
204 bb_error_msg("device '%s' has HW address %s '%s'",
206 xhw->print((char *) &ifr.ifr_hwaddr.sa_data));
210 /* Set an entry in the ARP cache. */
211 /* Called only from main, once */
212 static int arp_set(char **args)
219 memset(&req, 0, sizeof(req));
222 if (ap->input(host, &sa) < 0) {
223 bb_herror_msg_and_die("%s", host);
225 /* If a host has more than one address, use the correct one! */
226 memcpy(&req.arp_pa, &sa, sizeof(struct sockaddr));
228 /* Fetch the hardware address. */
230 bb_error_msg_and_die("need hardware address");
232 if (option_mask32 & ARP_OPT_D) {
233 arp_getdevhw(*args++, &req.arp_ha, hw_set ? hw : NULL);
235 if (hw->input(*args++, &req.arp_ha) < 0) {
236 bb_error_msg_and_die("invalid hardware address");
240 /* Check out any modifiers. */
241 flags = ATF_PERM | ATF_COM;
242 while (*args != NULL) {
243 switch (index_in_str_array(options, *args)) {
256 case 3: /* "trail" */
257 flags |= ATF_USETRAILERS;
260 case 4: /* "dontpub" */
261 #ifdef HAVE_ATF_DONTPUB
262 flags |= ATF_DONTPUB;
264 bb_error_msg("feature ATF_DONTPUB is not supported");
269 #ifdef HAVE_ATF_MAGIC
272 bb_error_msg("feature ATF_MAGIC is not supported");
282 case 7: /* "netmask" */
285 if (strcmp(*args, "255.255.255.255") != 0) {
287 if (ap->input(host, &sa) < 0) {
288 bb_herror_msg_and_die("%s", host);
290 memcpy(&req.arp_netmask, &sa, sizeof(struct sockaddr));
291 flags |= ATF_NETMASK;
301 /* Fill in the remainder of the request. */
302 req.arp_flags = flags;
304 strncpy(req.arp_dev, device, sizeof(req.arp_dev));
306 /* Call the kernel. */
307 if (option_mask32 & ARP_OPT_v)
308 bb_error_msg("SIOCSARP()");
309 if (ioctl(sockfd, SIOCSARP, &req) < 0) {
310 bb_perror_msg_and_die("SIOCSARP");
316 /* Print the contents of an ARP request block. */
318 arp_disp(const char *name, char *ip, int type, int arp_flags,
319 char *hwa, char *mask, char *dev)
321 const struct hwtype *xhw;
323 xhw = get_hwntype(type);
325 xhw = get_hwtype(DFLT_HW);
327 printf("%s (%s) at ", name, ip);
329 if (!(arp_flags & ATF_COM)) {
330 if (arp_flags & ATF_PUBL)
333 printf("<incomplete> ");
335 printf("%s [%s] ", hwa, xhw->name);
338 if (arp_flags & ATF_NETMASK)
339 printf("netmask %s ", mask);
341 if (arp_flags & ATF_PERM)
343 if (arp_flags & ATF_PUBL)
345 #ifdef HAVE_ATF_MAGIC
346 if (arp_flags & ATF_MAGIC)
349 #ifdef HAVE_ATF_DONTPUB
350 if (arp_flags & ATF_DONTPUB)
353 if (arp_flags & ATF_USETRAILERS)
356 printf("on %s\n", dev);
359 /* Display the contents of the ARP cache in the kernel. */
360 /* Called only from main, once */
361 static int arp_show(char *name)
364 const char *hostname;
369 unsigned entries = 0, shown = 0;
378 /* Resolve the host name. */
379 if (ap->input(name, &sa) < 0) {
380 bb_herror_msg_and_die("%s", name);
382 host = xstrdup(ap->sprint(&sa, 1));
384 fp = xfopen("/proc/net/arp", "r");
385 /* Bypass header -- read one line */
386 fgets(line, sizeof(line), fp);
388 /* Read the ARP cache entries. */
389 while (fgets(line, sizeof(line), fp)) {
391 mask[0] = '-'; mask[1] = '\0';
392 dev[0] = '-'; dev[1] = '\0';
393 /* All these strings can't overflow
394 * because fgets above reads limited amount of data */
395 num = sscanf(line, "%s 0x%x 0x%x %s %s %s\n",
396 ip, &type, &flags, hwa, mask, dev);
401 /* if the user specified hw-type differs, skip it */
402 if (hw_set && (type != hw->type))
405 /* if the user specified address differs, skip it */
406 if (host && strcmp(ip, host) != 0)
409 /* if the user specified device differs, skip it */
410 if (device[0] && strcmp(dev, device) != 0)
414 /* This IS ugly but it works -be */
416 if (!(option_mask32 & ARP_OPT_n)) {
417 if (ap->input(ip, &sa) < 0)
420 hostname = ap->sprint(&sa, (option_mask32 & ARP_OPT_n) | 0x8000);
421 if (strcmp(hostname, ip) == 0)
425 arp_disp(hostname, ip, type, flags, hwa, mask, dev);
427 if (option_mask32 & ARP_OPT_v)
428 printf("Entries: %d\tSkipped: %d\tFound: %d\n",
429 entries, entries - shown, shown);
432 if (hw_set || host || device[0])
433 printf("No match found in %d entries\n", entries);
435 if (ENABLE_FEATURE_CLEAN_UP) {
442 int arp_main(int argc, char **argv);
443 int arp_main(int argc, char **argv)
448 /* Initialize variables... */
449 ap = get_aftype(DFLT_AF);
451 bb_error_msg_and_die("%s: %s not supported", DFLT_AF, "address family");
453 getopt32(argc, argv, "A:p:H:t:i:adnDsv", &protocol, &protocol,
454 &hw_type, &hw_type, &device);
456 if (option_mask32 & ARP_OPT_A || option_mask32 & ARP_OPT_p) {
457 ap = get_aftype(protocol);
459 bb_error_msg_and_die("%s: unknown %s", protocol, "address family");
461 if (option_mask32 & ARP_OPT_A || option_mask32 & ARP_OPT_p) {
462 hw = get_hwtype(hw_type);
464 bb_error_msg_and_die("%s: unknown %s", hw_type, "hardware type");
467 //if (option_mask32 & ARP_OPT_i)... -i
469 if (ap->af != AF_INET) {
470 bb_error_msg_and_die("%s: kernel only supports 'inet'", ap->name);
473 /* If no hw type specified get default */
475 hw = get_hwtype(DFLT_HW);
477 bb_error_msg_and_die("%s: %s not supported", DFLT_HW, "hardware type");
481 bb_error_msg_and_die("%s: %s without ARP support",
482 hw->name, "hardware type");
484 sockfd = xsocket(AF_INET, SOCK_DGRAM, 0);
486 /* Now see what we have to do here... */
487 if (option_mask32 & (ARP_OPT_d|ARP_OPT_s)) {
489 bb_error_msg_and_die("need host name");
490 if (option_mask32 & ARP_OPT_s)
491 return arp_set(argv);
492 return arp_del(argv);
494 //if (option_mask32 & ARP_OPT_a) - default
495 return arp_show(argv[0]);