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 options[] ALIGN1 =
59 /* Delete an entry from the ARP cache. */
60 /* Called only from main, once */
61 static int arp_del(char **args)
69 memset(&req, 0, sizeof(req));
71 /* Resolve the host name. */
73 if (ap->input(host, &sa) < 0) {
74 bb_herror_msg_and_die("%s", host);
77 /* If a host has more than one address, use the correct one! */
78 memcpy(&req.arp_pa, &sa, sizeof(struct sockaddr));
81 req.arp_ha.sa_family = hw->type;
83 req.arp_flags = ATF_PERM;
85 while (*args != NULL) {
86 switch (index_in_strings(options, *args)) {
96 req.arp_flags &= ~ATF_PERM;
100 req.arp_flags |= ATF_USETRAILERS;
103 case 4: /* "dontpub" */
104 #ifdef HAVE_ATF_DONTPUB
105 req.arp_flags |= ATF_DONTPUB;
107 bb_error_msg("feature ATF_DONTPUB is not supported");
112 #ifdef HAVE_ATF_MAGIC
113 req.arp_flags |= ATF_MAGIC;
115 bb_error_msg("feature ATF_MAGIC is not supported");
125 case 7: /* "netmask" */
128 if (strcmp(*args, "255.255.255.255") != 0) {
130 if (ap->input(host, &sa) < 0) {
131 bb_herror_msg_and_die("%s", host);
133 memcpy(&req.arp_netmask, &sa, sizeof(struct sockaddr));
134 req.arp_flags |= ATF_NETMASK;
146 strncpy(req.arp_dev, device, sizeof(req.arp_dev));
150 /* Call the kernel. */
152 if (option_mask32 & ARP_OPT_v)
153 bb_error_msg("SIOCDARP(nopub)");
154 err = ioctl(sockfd, SIOCDARP, &req);
156 if (errno == ENXIO) {
159 printf("No ARP entry for %s\n", host);
162 bb_perror_msg_and_die("SIOCDARP(priv)");
165 if ((flags & 1) && err) {
167 req.arp_flags |= ATF_PUBL;
168 if (option_mask32 & ARP_OPT_v)
169 bb_error_msg("SIOCDARP(pub)");
170 if (ioctl(sockfd, SIOCDARP, &req) < 0) {
171 if (errno == ENXIO) {
172 printf("No ARP entry for %s\n", host);
175 bb_perror_msg_and_die("SIOCDARP(pub)");
181 /* Get the hardware address to a specified interface name */
182 static void arp_getdevhw(char *ifname, struct sockaddr *sa,
183 const struct hwtype *hwt)
186 const struct hwtype *xhw;
188 strcpy(ifr.ifr_name, ifname);
189 ioctl_or_perror_and_die(sockfd, SIOCGIFHWADDR, &ifr,
190 "cant get HW-Address for '%s'", ifname);
191 if (hwt && (ifr.ifr_hwaddr.sa_family != hw->type)) {
192 bb_error_msg_and_die("protocol type mismatch");
194 memcpy(sa, &(ifr.ifr_hwaddr), sizeof(struct sockaddr));
196 if (option_mask32 & ARP_OPT_v) {
197 xhw = get_hwntype(ifr.ifr_hwaddr.sa_family);
198 if (!xhw || !xhw->print) {
199 xhw = get_hwntype(-1);
201 bb_error_msg("device '%s' has HW address %s '%s'",
203 xhw->print((char *) &ifr.ifr_hwaddr.sa_data));
207 /* Set an entry in the ARP cache. */
208 /* Called only from main, once */
209 static int arp_set(char **args)
216 memset(&req, 0, sizeof(req));
219 if (ap->input(host, &sa) < 0) {
220 bb_herror_msg_and_die("%s", host);
222 /* If a host has more than one address, use the correct one! */
223 memcpy(&req.arp_pa, &sa, sizeof(struct sockaddr));
225 /* Fetch the hardware address. */
227 bb_error_msg_and_die("need hardware address");
229 if (option_mask32 & ARP_OPT_D) {
230 arp_getdevhw(*args++, &req.arp_ha, hw_set ? hw : NULL);
232 if (hw->input(*args++, &req.arp_ha) < 0) {
233 bb_error_msg_and_die("invalid hardware address");
237 /* Check out any modifiers. */
238 flags = ATF_PERM | ATF_COM;
239 while (*args != NULL) {
240 switch (index_in_strings(options, *args)) {
253 case 3: /* "trail" */
254 flags |= ATF_USETRAILERS;
257 case 4: /* "dontpub" */
258 #ifdef HAVE_ATF_DONTPUB
259 flags |= ATF_DONTPUB;
261 bb_error_msg("feature ATF_DONTPUB is not supported");
266 #ifdef HAVE_ATF_MAGIC
269 bb_error_msg("feature ATF_MAGIC is not supported");
279 case 7: /* "netmask" */
282 if (strcmp(*args, "255.255.255.255") != 0) {
284 if (ap->input(host, &sa) < 0) {
285 bb_herror_msg_and_die("%s", host);
287 memcpy(&req.arp_netmask, &sa, sizeof(struct sockaddr));
288 flags |= ATF_NETMASK;
298 /* Fill in the remainder of the request. */
299 req.arp_flags = flags;
301 strncpy(req.arp_dev, device, sizeof(req.arp_dev));
303 /* Call the kernel. */
304 if (option_mask32 & ARP_OPT_v)
305 bb_error_msg("SIOCSARP()");
306 xioctl(sockfd, SIOCSARP, &req);
311 /* Print the contents of an ARP request block. */
313 arp_disp(const char *name, char *ip, int type, int arp_flags,
314 char *hwa, char *mask, char *dev)
316 const struct hwtype *xhw;
318 xhw = get_hwntype(type);
320 xhw = get_hwtype(DFLT_HW);
322 printf("%s (%s) at ", name, ip);
324 if (!(arp_flags & ATF_COM)) {
325 if (arp_flags & ATF_PUBL)
328 printf("<incomplete> ");
330 printf("%s [%s] ", hwa, xhw->name);
333 if (arp_flags & ATF_NETMASK)
334 printf("netmask %s ", mask);
336 if (arp_flags & ATF_PERM)
338 if (arp_flags & ATF_PUBL)
340 #ifdef HAVE_ATF_MAGIC
341 if (arp_flags & ATF_MAGIC)
344 #ifdef HAVE_ATF_DONTPUB
345 if (arp_flags & ATF_DONTPUB)
348 if (arp_flags & ATF_USETRAILERS)
351 printf("on %s\n", dev);
354 /* Display the contents of the ARP cache in the kernel. */
355 /* Called only from main, once */
356 static int arp_show(char *name)
359 const char *hostname;
364 unsigned entries = 0, shown = 0;
373 /* Resolve the host name. */
374 if (ap->input(name, &sa) < 0) {
375 bb_herror_msg_and_die("%s", name);
377 host = xstrdup(ap->sprint(&sa, 1));
379 fp = xfopen("/proc/net/arp", "r");
380 /* Bypass header -- read one line */
381 fgets(line, sizeof(line), fp);
383 /* Read the ARP cache entries. */
384 while (fgets(line, sizeof(line), fp)) {
386 mask[0] = '-'; mask[1] = '\0';
387 dev[0] = '-'; dev[1] = '\0';
388 /* All these strings can't overflow
389 * because fgets above reads limited amount of data */
390 num = sscanf(line, "%s 0x%x 0x%x %s %s %s\n",
391 ip, &type, &flags, hwa, mask, dev);
396 /* if the user specified hw-type differs, skip it */
397 if (hw_set && (type != hw->type))
400 /* if the user specified address differs, skip it */
401 if (host && strcmp(ip, host) != 0)
404 /* if the user specified device differs, skip it */
405 if (device[0] && strcmp(dev, device) != 0)
409 /* This IS ugly but it works -be */
411 if (!(option_mask32 & ARP_OPT_n)) {
412 if (ap->input(ip, &sa) < 0)
415 hostname = ap->sprint(&sa, (option_mask32 & ARP_OPT_n) | 0x8000);
416 if (strcmp(hostname, ip) == 0)
420 arp_disp(hostname, ip, type, flags, hwa, mask, dev);
422 if (option_mask32 & ARP_OPT_v)
423 printf("Entries: %d\tSkipped: %d\tFound: %d\n",
424 entries, entries - shown, shown);
427 if (hw_set || host || device[0])
428 printf("No match found in %d entries\n", entries);
430 if (ENABLE_FEATURE_CLEAN_UP) {
437 int arp_main(int argc, char **argv);
438 int arp_main(int argc, char **argv)
443 /* Initialize variables... */
444 ap = get_aftype(DFLT_AF);
446 bb_error_msg_and_die("%s: %s not supported", DFLT_AF, "address family");
448 getopt32(argc, argv, "A:p:H:t:i:adnDsv", &protocol, &protocol,
449 &hw_type, &hw_type, &device);
451 if (option_mask32 & ARP_OPT_A || option_mask32 & ARP_OPT_p) {
452 ap = get_aftype(protocol);
454 bb_error_msg_and_die("%s: unknown %s", protocol, "address family");
456 if (option_mask32 & ARP_OPT_A || option_mask32 & ARP_OPT_p) {
457 hw = get_hwtype(hw_type);
459 bb_error_msg_and_die("%s: unknown %s", hw_type, "hardware type");
462 //if (option_mask32 & ARP_OPT_i)... -i
464 if (ap->af != AF_INET) {
465 bb_error_msg_and_die("%s: kernel only supports 'inet'", ap->name);
468 /* If no hw type specified get default */
470 hw = get_hwtype(DFLT_HW);
472 bb_error_msg_and_die("%s: %s not supported", DFLT_HW, "hardware type");
476 bb_error_msg_and_die("%s: %s without ARP support",
477 hw->name, "hardware type");
479 sockfd = xsocket(AF_INET, SOCK_DGRAM, 0);
481 /* Now see what we have to do here... */
482 if (option_mask32 & (ARP_OPT_d|ARP_OPT_s)) {
484 bb_error_msg_and_die("need host name");
485 if (option_mask32 & ARP_OPT_s)
486 return arp_set(argv);
487 return arp_del(argv);
489 //if (option_mask32 & ARP_OPT_a) - default
490 return arp_show(argv[0]);