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"
38 ARP_OPT_n = (1 << 7), /* do not resolve addresses */
39 ARP_OPT_D = (1 << 8), /* HW-address is devicename */
41 ARP_OPT_v = (1 << 10) * DEBUG, /* debugging output flag */
45 sockfd = 3, /* active socket descriptor */
49 const struct aftype *ap; /* current address family */
50 const struct hwtype *hw; /* current hardware type */
51 const char *device; /* current device */
52 smallint hw_set; /* flag if hw-type was set (-H) */
55 #define G (*(struct globals*)&bb_common_bufsiz1)
58 #define device (G.device )
59 #define hw_set (G.hw_set )
60 #define INIT_G() do { \
65 static const char options[] ALIGN1 =
75 /* Delete an entry from the ARP cache. */
76 /* Called only from main, once */
77 static int arp_del(char **args)
85 memset(&req, 0, sizeof(req));
87 /* Resolve the host name. */
89 if (ap->input(host, &sa) < 0) {
90 bb_herror_msg_and_die("%s", host);
93 /* If a host has more than one address, use the correct one! */
94 memcpy(&req.arp_pa, &sa, sizeof(struct sockaddr));
97 req.arp_ha.sa_family = hw->type;
99 req.arp_flags = ATF_PERM;
101 while (*args != NULL) {
102 switch (index_in_strings(options, *args)) {
112 req.arp_flags &= ~ATF_PERM;
115 case 3: /* "trail" */
116 req.arp_flags |= ATF_USETRAILERS;
119 case 4: /* "dontpub" */
120 #ifdef HAVE_ATF_DONTPUB
121 req.arp_flags |= ATF_DONTPUB;
123 bb_error_msg("feature ATF_DONTPUB is not supported");
128 #ifdef HAVE_ATF_MAGIC
129 req.arp_flags |= ATF_MAGIC;
131 bb_error_msg("feature ATF_MAGIC is not supported");
141 case 7: /* "netmask" */
144 if (strcmp(*args, "255.255.255.255") != 0) {
146 if (ap->input(host, &sa) < 0) {
147 bb_herror_msg_and_die("%s", host);
149 memcpy(&req.arp_netmask, &sa, sizeof(struct sockaddr));
150 req.arp_flags |= ATF_NETMASK;
162 strncpy(req.arp_dev, device, sizeof(req.arp_dev));
166 /* Call the kernel. */
168 if (option_mask32 & ARP_OPT_v)
169 bb_error_msg("SIOCDARP(nopub)");
170 err = ioctl(sockfd, SIOCDARP, &req);
172 if (errno == ENXIO) {
175 printf("No ARP entry for %s\n", host);
178 bb_perror_msg_and_die("SIOCDARP(priv)");
181 if ((flags & 1) && err) {
183 req.arp_flags |= ATF_PUBL;
184 if (option_mask32 & ARP_OPT_v)
185 bb_error_msg("SIOCDARP(pub)");
186 if (ioctl(sockfd, SIOCDARP, &req) < 0) {
187 if (errno == ENXIO) {
188 printf("No ARP entry for %s\n", host);
191 bb_perror_msg_and_die("SIOCDARP(pub)");
197 /* Get the hardware address to a specified interface name */
198 static void arp_getdevhw(char *ifname, struct sockaddr *sa,
199 const struct hwtype *hwt)
202 const struct hwtype *xhw;
204 strcpy(ifr.ifr_name, ifname);
205 ioctl_or_perror_and_die(sockfd, SIOCGIFHWADDR, &ifr,
206 "cant get HW-Address for '%s'", ifname);
207 if (hwt && (ifr.ifr_hwaddr.sa_family != hw->type)) {
208 bb_error_msg_and_die("protocol type mismatch");
210 memcpy(sa, &(ifr.ifr_hwaddr), sizeof(struct sockaddr));
212 if (option_mask32 & ARP_OPT_v) {
213 xhw = get_hwntype(ifr.ifr_hwaddr.sa_family);
214 if (!xhw || !xhw->print) {
215 xhw = get_hwntype(-1);
217 bb_error_msg("device '%s' has HW address %s '%s'",
219 xhw->print((unsigned char *) &ifr.ifr_hwaddr.sa_data));
223 /* Set an entry in the ARP cache. */
224 /* Called only from main, once */
225 static int arp_set(char **args)
232 memset(&req, 0, sizeof(req));
235 if (ap->input(host, &sa) < 0) {
236 bb_herror_msg_and_die("%s", host);
238 /* If a host has more than one address, use the correct one! */
239 memcpy(&req.arp_pa, &sa, sizeof(struct sockaddr));
241 /* Fetch the hardware address. */
243 bb_error_msg_and_die("need hardware address");
245 if (option_mask32 & ARP_OPT_D) {
246 arp_getdevhw(*args++, &req.arp_ha, hw_set ? hw : NULL);
248 if (hw->input(*args++, &req.arp_ha) < 0) {
249 bb_error_msg_and_die("invalid hardware address");
253 /* Check out any modifiers. */
254 flags = ATF_PERM | ATF_COM;
255 while (*args != NULL) {
256 switch (index_in_strings(options, *args)) {
269 case 3: /* "trail" */
270 flags |= ATF_USETRAILERS;
273 case 4: /* "dontpub" */
274 #ifdef HAVE_ATF_DONTPUB
275 flags |= ATF_DONTPUB;
277 bb_error_msg("feature ATF_DONTPUB is not supported");
282 #ifdef HAVE_ATF_MAGIC
285 bb_error_msg("feature ATF_MAGIC is not supported");
295 case 7: /* "netmask" */
298 if (strcmp(*args, "255.255.255.255") != 0) {
300 if (ap->input(host, &sa) < 0) {
301 bb_herror_msg_and_die("%s", host);
303 memcpy(&req.arp_netmask, &sa, sizeof(struct sockaddr));
304 flags |= ATF_NETMASK;
314 /* Fill in the remainder of the request. */
315 req.arp_flags = flags;
317 strncpy(req.arp_dev, device, sizeof(req.arp_dev));
319 /* Call the kernel. */
320 if (option_mask32 & ARP_OPT_v)
321 bb_error_msg("SIOCSARP()");
322 xioctl(sockfd, SIOCSARP, &req);
327 /* Print the contents of an ARP request block. */
329 arp_disp(const char *name, char *ip, int type, int arp_flags,
330 char *hwa, char *mask, char *dev)
332 static const int arp_masks[] = {
334 #ifdef HAVE_ATF_MAGIC
337 #ifdef HAVE_ATF_DONTPUB
342 static const char arp_labels[] ALIGN1 = "PERM\0""PUP\0"
343 #ifdef HAVE_ATF_MAGIC
346 #ifdef HAVE_ATF_DONTPUB
352 const struct hwtype *xhw;
354 xhw = get_hwntype(type);
356 xhw = get_hwtype(DFLT_HW);
358 printf("%s (%s) at ", name, ip);
360 if (!(arp_flags & ATF_COM)) {
361 if (arp_flags & ATF_PUBL)
364 printf("<incomplete> ");
366 printf("%s [%s] ", hwa, xhw->name);
369 if (arp_flags & ATF_NETMASK)
370 printf("netmask %s ", mask);
372 print_flags_separated(arp_masks, arp_labels, arp_flags, " ");
373 printf(" on %s\n", dev);
376 /* Display the contents of the ARP cache in the kernel. */
377 /* Called only from main, once */
378 static int arp_show(char *name)
381 const char *hostname;
386 unsigned entries = 0, shown = 0;
395 /* Resolve the host name. */
396 if (ap->input(name, &sa) < 0) {
397 bb_herror_msg_and_die("%s", name);
399 host = xstrdup(ap->sprint(&sa, 1));
401 fp = xfopen_for_read("/proc/net/arp");
402 /* Bypass header -- read one line */
403 fgets(line, sizeof(line), fp);
405 /* Read the ARP cache entries. */
406 while (fgets(line, sizeof(line), fp)) {
408 mask[0] = '-'; mask[1] = '\0';
409 dev[0] = '-'; dev[1] = '\0';
410 /* All these strings can't overflow
411 * because fgets above reads limited amount of data */
412 num = sscanf(line, "%s 0x%x 0x%x %s %s %s\n",
413 ip, &type, &flags, hwa, mask, dev);
418 /* if the user specified hw-type differs, skip it */
419 if (hw_set && (type != hw->type))
422 /* if the user specified address differs, skip it */
423 if (host && strcmp(ip, host) != 0)
426 /* if the user specified device differs, skip it */
427 if (device[0] && strcmp(dev, device) != 0)
431 /* This IS ugly but it works -be */
433 if (!(option_mask32 & ARP_OPT_n)) {
434 if (ap->input(ip, &sa) < 0)
437 hostname = ap->sprint(&sa, (option_mask32 & ARP_OPT_n) | 0x8000);
438 if (strcmp(hostname, ip) == 0)
442 arp_disp(hostname, ip, type, flags, hwa, mask, dev);
444 if (option_mask32 & ARP_OPT_v)
445 printf("Entries: %d\tSkipped: %d\tFound: %d\n",
446 entries, entries - shown, shown);
449 if (hw_set || host || device[0])
450 printf("No match found in %d entries\n", entries);
452 if (ENABLE_FEATURE_CLEAN_UP) {
459 int arp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
460 int arp_main(int argc UNUSED_PARAM, char **argv)
462 const char *hw_type = "ether";
463 const char *protocol;
468 xmove_fd(xsocket(AF_INET, SOCK_DGRAM, 0), sockfd);
469 ap = get_aftype(DFLT_AF);
471 bb_error_msg_and_die("%s: %s not supported", DFLT_AF, "address family");
473 opts = getopt32(argv, "A:p:H:t:i:adnDsv", &protocol, &protocol,
474 &hw_type, &hw_type, &device);
476 if (opts & (ARP_OPT_A | ARP_OPT_p)) {
477 ap = get_aftype(protocol);
479 bb_error_msg_and_die("%s: unknown %s", protocol, "address family");
481 if (opts & (ARP_OPT_A | ARP_OPT_p)) {
482 hw = get_hwtype(hw_type);
484 bb_error_msg_and_die("%s: unknown %s", hw_type, "hardware type");
487 //if (opts & ARP_OPT_i)... -i
489 if (ap->af != AF_INET) {
490 bb_error_msg_and_die("%s: kernel only supports 'inet'", ap->name);
493 /* If no hw type specified get default */
495 hw = get_hwtype(DFLT_HW);
497 bb_error_msg_and_die("%s: %s not supported", DFLT_HW, "hardware type");
501 bb_error_msg_and_die("%s: %s without ARP support",
502 hw->name, "hardware type");
505 /* Now see what we have to do here... */
506 if (opts & (ARP_OPT_d | ARP_OPT_s)) {
508 bb_error_msg_and_die("need host name");
509 if (opts & ARP_OPT_s)
510 return arp_set(argv);
511 return arp_del(argv);
513 //if (opts & ARP_OPT_a) - default
514 return arp_show(argv[0]);