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 _PATH_PROCNET_ARP "/proc/net/arp"
32 #define ARP_OPT_A (0x1)
33 #define ARP_OPT_p (0x2)
34 #define ARP_OPT_H (0x4)
35 #define ARP_OPT_t (0x8)
36 #define ARP_OPT_i (0x10)
37 #define ARP_OPT_a (0x20)
38 #define ARP_OPT_d (0x40)
39 #define ARP_OPT_n (0x80) /* do not resolve addresses */
40 #define ARP_OPT_D (0x100) /* HW-address is devicename */
41 #define ARP_OPT_s (0x200)
42 #define ARP_OPT_v (0x400 * DEBUG) /* debugging output flag */
45 static const struct aftype *ap; /* current address family */
46 static const struct hwtype *hw; /* current hardware type */
47 static int sockfd; /* active socket descriptor */
48 static smallint hw_set; /* flag if hw-type was set (-H) */
49 static char *device = ""; /* current device */
51 static const char *const options[] = {
63 /* Delete an entry from the ARP cache. */
64 /* Called only from main, once */
65 static int arp_del(char **args)
73 memset(&req, 0, sizeof(req));
75 /* Resolve the host name. */
76 safe_strncpy(host, *args, 128);
77 if (ap->input(0, host, &sa) < 0) {
78 bb_herror_msg_and_die("%s", host);
81 /* If a host has more than one address, use the correct one! */
82 memcpy(&req.arp_pa, &sa, sizeof(struct sockaddr));
85 req.arp_ha.sa_family = hw->type;
87 req.arp_flags = ATF_PERM;
89 while (*args != NULL) {
90 switch (index_in_str_array(options, *args)) {
100 req.arp_flags &= ~ATF_PERM;
103 case 3: /* "trail" */
104 req.arp_flags |= ATF_USETRAILERS;
107 case 4: /* "dontpub" */
108 #ifdef HAVE_ATF_DONTPUB
109 req.arp_flags |= ATF_DONTPUB;
111 bb_error_msg("feature ATF_DONTPUB is not supported");
116 #ifdef HAVE_ATF_MAGIC
117 req.arp_flags |= ATF_MAGIC;
119 bb_error_msg("feature ATF_MAGIC is not supported");
129 case 7: /* "netmask" */
132 if (strcmp(*args, "255.255.255.255") != 0) {
133 safe_strncpy(host, *args, 128);
134 if (ap->input(0, host, &sa) < 0) {
135 bb_herror_msg_and_die("%s", host);
137 memcpy(&req.arp_netmask, &sa, sizeof(struct sockaddr));
138 req.arp_flags |= ATF_NETMASK;
150 strncpy(req.arp_dev, device, sizeof(req.arp_dev));
154 /* Call the kernel. */
156 if (option_mask32 & ARP_OPT_v)
157 bb_error_msg("SIOCDARP(nopub)");
158 err = ioctl(sockfd, SIOCDARP, &req);
160 if (errno == ENXIO) {
163 printf("No ARP entry for %s\n", host);
166 bb_perror_msg_and_die("SIOCDARP(priv)");
169 if ((flags & 1) && err) {
171 req.arp_flags |= ATF_PUBL;
172 if (option_mask32 & ARP_OPT_v)
173 bb_error_msg("SIOCDARP(pub)");
174 if (ioctl(sockfd, SIOCDARP, &req) < 0) {
175 if (errno == ENXIO) {
176 printf("No ARP entry for %s\n", host);
179 bb_perror_msg_and_die("SIOCDARP(pub)");
185 /* Get the hardware address to a specified interface name */
186 static void arp_getdevhw(char *ifname, struct sockaddr *sa,
187 const struct hwtype *hwt)
190 const struct hwtype *xhw;
192 strcpy(ifr.ifr_name, ifname);
193 if (ioctl(sockfd, SIOCGIFHWADDR, &ifr) < 0) {
194 bb_perror_msg_and_die("cant get HW-Address for '%s'", ifname);
196 if (hwt && (ifr.ifr_hwaddr.sa_family != hw->type)) {
197 bb_error_msg_and_die("protocol type mismatch");
199 memcpy(sa, &(ifr.ifr_hwaddr), sizeof(struct sockaddr));
201 if (option_mask32 & ARP_OPT_v) {
202 xhw = get_hwntype(ifr.ifr_hwaddr.sa_family);
203 if (!xhw || !xhw->print) {
204 xhw = get_hwntype(-1);
206 bb_error_msg("device '%s' has HW address %s '%s'",
208 xhw->print((char *) &ifr.ifr_hwaddr.sa_data));
212 /* Set an entry in the ARP cache. */
213 /* Called only from main, once */
214 static int arp_set(char **args)
221 memset(&req, 0, sizeof(req));
223 safe_strncpy(host, *args++, 128);
224 if (ap->input(0, host, &sa) < 0) {
225 bb_herror_msg_and_die("%s", host);
227 /* If a host has more than one address, use the correct one! */
228 memcpy(&req.arp_pa, &sa, sizeof(struct sockaddr));
230 /* Fetch the hardware address. */
232 bb_error_msg_and_die("need hardware address");
234 if (option_mask32 & ARP_OPT_D) {
235 arp_getdevhw(*args++, &req.arp_ha, hw_set ? hw : NULL);
237 if (hw->input(*args++, &req.arp_ha) < 0) {
238 bb_error_msg_and_die("invalid hardware address");
242 /* Check out any modifiers. */
243 flags = ATF_PERM | ATF_COM;
244 while (*args != NULL) {
245 switch (index_in_str_array(options, *args)) {
258 case 3: /* "trail" */
259 flags |= ATF_USETRAILERS;
262 case 4: /* "dontpub" */
263 #ifdef HAVE_ATF_DONTPUB
264 flags |= ATF_DONTPUB;
266 bb_error_msg("feature ATF_DONTPUB is not supported");
271 #ifdef HAVE_ATF_MAGIC
274 bb_error_msg("feature ATF_MAGIC is not supported");
284 case 7: /* "netmask" */
287 if (strcmp(*args, "255.255.255.255") != 0) {
288 safe_strncpy(host, *args++, 128);
289 if (ap->input(0, host, &sa) < 0) {
290 bb_herror_msg_and_die("%s", host);
292 memcpy(&req.arp_netmask, &sa, sizeof(struct sockaddr));
293 flags |= ATF_NETMASK;
303 /* Fill in the remainder of the request. */
304 req.arp_flags = flags;
306 strncpy(req.arp_dev, device, sizeof(req.arp_dev));
308 /* Call the kernel. */
309 if (option_mask32 & ARP_OPT_v)
310 bb_error_msg("SIOCSARP()");
311 if (ioctl(sockfd, SIOCSARP, &req) < 0) {
312 bb_perror_msg_and_die("SIOCSARP");
318 /* Print the contents of an ARP request block. */
320 arp_disp(char *name, char *ip, int type, int arp_flags,
321 char *hwa, char *mask, char *dev)
323 const struct hwtype *xhw;
325 xhw = get_hwntype(type);
327 xhw = get_hwtype(DFLT_HW);
329 printf("%s (%s) at ", name, ip);
331 if (!(arp_flags & ATF_COM)) {
332 if (arp_flags & ATF_PUBL)
335 printf("<incomplete> ");
337 printf("%s [%s] ", hwa, xhw->name);
340 if (arp_flags & ATF_NETMASK)
341 printf("netmask %s ", mask);
343 if (arp_flags & ATF_PERM)
345 if (arp_flags & ATF_PUBL)
347 #ifdef HAVE_ATF_MAGIC
348 if (arp_flags & ATF_MAGIC)
351 #ifdef HAVE_ATF_DONTPUB
352 if (arp_flags & ATF_DONTPUB)
355 if (arp_flags & ATF_USETRAILERS)
358 printf("on %s\n", dev);
361 /* Display the contents of the ARP cache in the kernel. */
362 /* Called only from main, once */
363 static int arp_show(char *name)
376 unsigned entries = 0, shown = 0;
381 /* Resolve the host name. */
382 safe_strncpy(host, name, (sizeof host));
383 if (ap->input(0, host, &sa) < 0) {
384 bb_herror_msg_and_die("%s", host);
386 safe_strncpy(host, ap->sprint(&sa, 1), sizeof(host));
388 /* Open the PROCps kernel table. */
389 fp = xfopen(_PATH_PROCNET_ARP, "r");
390 /* Bypass header -- read until newline */
391 if (fgets(line, sizeof(line), fp) != (char *) NULL) {
392 mask[0] = '-'; mask[1] = '\0';
393 dev[0] = '-'; dev[1] = '\0';
394 /* Read the ARP cache entries. */
395 for (; fgets(line, sizeof(line), fp);) {
396 num = sscanf(line, "%s 0x%x 0x%x %100s %100s %100s\n",
397 ip, &type, &flags, hwa, mask, dev);
402 /* if the user specified hw-type differs, skip it */
403 if (hw_set && (type != hw->type))
406 /* if the user specified address differs, skip it */
407 if (host[0] && strcmp(ip, host) != 0)
410 /* if the user specified device differs, skip it */
411 if (device[0] && strcmp(dev, device) != 0)
415 /* This IS ugly but it works -be */
416 if (option_mask32 & ARP_OPT_n)
419 if (ap->input(0, ip, &sa) < 0)
422 hostname = ap->sprint(&sa, (option_mask32 & ARP_OPT_n) | 0x8000);
423 if (strcmp(hostname, ip) == 0)
427 arp_disp(hostname, ip, type, flags, hwa, mask, dev);
430 if (option_mask32 & ARP_OPT_v)
431 printf("Entries: %d\tSkipped: %d\tFound: %d\n",
432 entries, entries - shown, shown);
435 if (hw_set || host[0] || device[0])
436 printf("No match found in %d entries\n", entries);
444 int arp_main(int argc, char **argv)
449 /* Initialize variables... */
450 ap = get_aftype(DFLT_AF);
452 bb_error_msg_and_die("%s: %s not supported", DFLT_AF, "address family");
454 getopt32(argc, argv, "A:p:H:t:i:adnDsv", &protocol, &protocol,
455 &hw_type, &hw_type, &device);
457 if (option_mask32 & ARP_OPT_A || option_mask32 & ARP_OPT_p) {
458 ap = get_aftype(protocol);
460 bb_error_msg_and_die("%s: unknown %s", protocol, "address family");
462 if (option_mask32 & ARP_OPT_A || option_mask32 & ARP_OPT_p) {
463 hw = get_hwtype(hw_type);
465 bb_error_msg_and_die("%s: unknown %s", hw_type, "hardware type");
468 //if (option_mask32 & ARP_OPT_i)... -i
470 if (ap->af != AF_INET) {
471 bb_error_msg_and_die("%s: kernel only supports 'inet'", ap->name);
474 /* If no hw type specified get default */
476 hw = get_hwtype(DFLT_HW);
478 bb_error_msg_and_die("%s: %s not supported", DFLT_HW, "hardware type");
482 bb_error_msg_and_die("%s: %s without ARP support",
483 hw->name, "hardware type");
485 sockfd = xsocket(AF_INET, SOCK_DGRAM, 0);
487 /* Now see what we have to do here... */
488 if (option_mask32 & (ARP_OPT_d|ARP_OPT_s)) {
490 bb_error_msg_and_die("need host name");
491 if (option_mask32 & ARP_OPT_s)
492 return arp_set(argv);
493 return arp_del(argv);
495 //if (option_mask32 & ARP_OPT_a) - default
496 return arp_show(argv[0]);