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 ioctl_or_perror_and_die(sockfd, SIOCGIFHWADDR, &ifr,
192 "cant get HW-Address for '%s'", ifname);
193 if (hwt && (ifr.ifr_hwaddr.sa_family != hw->type)) {
194 bb_error_msg_and_die("protocol type mismatch");
196 memcpy(sa, &(ifr.ifr_hwaddr), sizeof(struct sockaddr));
198 if (option_mask32 & ARP_OPT_v) {
199 xhw = get_hwntype(ifr.ifr_hwaddr.sa_family);
200 if (!xhw || !xhw->print) {
201 xhw = get_hwntype(-1);
203 bb_error_msg("device '%s' has HW address %s '%s'",
205 xhw->print((char *) &ifr.ifr_hwaddr.sa_data));
209 /* Set an entry in the ARP cache. */
210 /* Called only from main, once */
211 static int arp_set(char **args)
218 memset(&req, 0, sizeof(req));
221 if (ap->input(host, &sa) < 0) {
222 bb_herror_msg_and_die("%s", host);
224 /* If a host has more than one address, use the correct one! */
225 memcpy(&req.arp_pa, &sa, sizeof(struct sockaddr));
227 /* Fetch the hardware address. */
229 bb_error_msg_and_die("need hardware address");
231 if (option_mask32 & ARP_OPT_D) {
232 arp_getdevhw(*args++, &req.arp_ha, hw_set ? hw : NULL);
234 if (hw->input(*args++, &req.arp_ha) < 0) {
235 bb_error_msg_and_die("invalid hardware address");
239 /* Check out any modifiers. */
240 flags = ATF_PERM | ATF_COM;
241 while (*args != NULL) {
242 switch (index_in_str_array(options, *args)) {
255 case 3: /* "trail" */
256 flags |= ATF_USETRAILERS;
259 case 4: /* "dontpub" */
260 #ifdef HAVE_ATF_DONTPUB
261 flags |= ATF_DONTPUB;
263 bb_error_msg("feature ATF_DONTPUB is not supported");
268 #ifdef HAVE_ATF_MAGIC
271 bb_error_msg("feature ATF_MAGIC is not supported");
281 case 7: /* "netmask" */
284 if (strcmp(*args, "255.255.255.255") != 0) {
286 if (ap->input(host, &sa) < 0) {
287 bb_herror_msg_and_die("%s", host);
289 memcpy(&req.arp_netmask, &sa, sizeof(struct sockaddr));
290 flags |= ATF_NETMASK;
300 /* Fill in the remainder of the request. */
301 req.arp_flags = flags;
303 strncpy(req.arp_dev, device, sizeof(req.arp_dev));
305 /* Call the kernel. */
306 if (option_mask32 & ARP_OPT_v)
307 bb_error_msg("SIOCSARP()");
308 xioctl(sockfd, SIOCSARP, &req);
313 /* Print the contents of an ARP request block. */
315 arp_disp(const char *name, char *ip, int type, int arp_flags,
316 char *hwa, char *mask, char *dev)
318 const struct hwtype *xhw;
320 xhw = get_hwntype(type);
322 xhw = get_hwtype(DFLT_HW);
324 printf("%s (%s) at ", name, ip);
326 if (!(arp_flags & ATF_COM)) {
327 if (arp_flags & ATF_PUBL)
330 printf("<incomplete> ");
332 printf("%s [%s] ", hwa, xhw->name);
335 if (arp_flags & ATF_NETMASK)
336 printf("netmask %s ", mask);
338 if (arp_flags & ATF_PERM)
340 if (arp_flags & ATF_PUBL)
342 #ifdef HAVE_ATF_MAGIC
343 if (arp_flags & ATF_MAGIC)
346 #ifdef HAVE_ATF_DONTPUB
347 if (arp_flags & ATF_DONTPUB)
350 if (arp_flags & ATF_USETRAILERS)
353 printf("on %s\n", dev);
356 /* Display the contents of the ARP cache in the kernel. */
357 /* Called only from main, once */
358 static int arp_show(char *name)
361 const char *hostname;
366 unsigned entries = 0, shown = 0;
375 /* Resolve the host name. */
376 if (ap->input(name, &sa) < 0) {
377 bb_herror_msg_and_die("%s", name);
379 host = xstrdup(ap->sprint(&sa, 1));
381 fp = xfopen("/proc/net/arp", "r");
382 /* Bypass header -- read one line */
383 fgets(line, sizeof(line), fp);
385 /* Read the ARP cache entries. */
386 while (fgets(line, sizeof(line), fp)) {
388 mask[0] = '-'; mask[1] = '\0';
389 dev[0] = '-'; dev[1] = '\0';
390 /* All these strings can't overflow
391 * because fgets above reads limited amount of data */
392 num = sscanf(line, "%s 0x%x 0x%x %s %s %s\n",
393 ip, &type, &flags, hwa, mask, dev);
398 /* if the user specified hw-type differs, skip it */
399 if (hw_set && (type != hw->type))
402 /* if the user specified address differs, skip it */
403 if (host && strcmp(ip, host) != 0)
406 /* if the user specified device differs, skip it */
407 if (device[0] && strcmp(dev, device) != 0)
411 /* This IS ugly but it works -be */
413 if (!(option_mask32 & ARP_OPT_n)) {
414 if (ap->input(ip, &sa) < 0)
417 hostname = ap->sprint(&sa, (option_mask32 & ARP_OPT_n) | 0x8000);
418 if (strcmp(hostname, ip) == 0)
422 arp_disp(hostname, ip, type, flags, hwa, mask, dev);
424 if (option_mask32 & ARP_OPT_v)
425 printf("Entries: %d\tSkipped: %d\tFound: %d\n",
426 entries, entries - shown, shown);
429 if (hw_set || host || device[0])
430 printf("No match found in %d entries\n", entries);
432 if (ENABLE_FEATURE_CLEAN_UP) {
439 int arp_main(int argc, char **argv);
440 int arp_main(int argc, char **argv)
445 /* Initialize variables... */
446 ap = get_aftype(DFLT_AF);
448 bb_error_msg_and_die("%s: %s not supported", DFLT_AF, "address family");
450 getopt32(argc, argv, "A:p:H:t:i:adnDsv", &protocol, &protocol,
451 &hw_type, &hw_type, &device);
453 if (option_mask32 & ARP_OPT_A || option_mask32 & ARP_OPT_p) {
454 ap = get_aftype(protocol);
456 bb_error_msg_and_die("%s: unknown %s", protocol, "address family");
458 if (option_mask32 & ARP_OPT_A || option_mask32 & ARP_OPT_p) {
459 hw = get_hwtype(hw_type);
461 bb_error_msg_and_die("%s: unknown %s", hw_type, "hardware type");
464 //if (option_mask32 & ARP_OPT_i)... -i
466 if (ap->af != AF_INET) {
467 bb_error_msg_and_die("%s: kernel only supports 'inet'", ap->name);
470 /* If no hw type specified get default */
472 hw = get_hwtype(DFLT_HW);
474 bb_error_msg_and_die("%s: %s not supported", DFLT_HW, "hardware type");
478 bb_error_msg_and_die("%s: %s without ARP support",
479 hw->name, "hardware type");
481 sockfd = xsocket(AF_INET, SOCK_DGRAM, 0);
483 /* Now see what we have to do here... */
484 if (option_mask32 & (ARP_OPT_d|ARP_OPT_s)) {
486 bb_error_msg_and_die("need host name");
487 if (option_mask32 & ARP_OPT_s)
488 return arp_set(argv);
489 return arp_del(argv);
491 //if (option_mask32 & ARP_OPT_a) - default
492 return arp_show(argv[0]);