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 if (option_mask32 & ARP_OPT_v)
91 bb_error_msg("args=%s", *args);
93 switch (index_in_str_array(options, *args)) {
103 req.arp_flags &= ~ATF_PERM;
107 req.arp_flags |= ATF_USETRAILERS;
111 #ifdef HAVE_ATF_DONTPUB
112 req.arp_flags |= ATF_DONTPUB;
114 bb_error_msg("feature ATF_DONTPUB is not supported");
119 #ifdef HAVE_ATF_MAGIC
120 req.arp_flags |= ATF_MAGIC;
122 bb_error_msg("feature ATF_MAGIC is not supported");
135 if (strcmp(*args, "255.255.255.255") != 0) {
136 safe_strncpy(host, *args, 128);
137 if (ap->input(0, host, &sa) < 0) {
138 bb_herror_msg("%s", host);
141 memcpy(&req.arp_netmask, &sa, sizeof(struct sockaddr));
142 req.arp_flags |= ATF_NETMASK;
154 strncpy(req.arp_dev, device, sizeof(req.arp_dev));
158 /* Call the kernel. */
160 if (option_mask32 & ARP_OPT_v)
161 bb_error_msg("SIOCDARP(nopub)");
162 err = ioctl(sockfd, SIOCDARP, &req);
164 if (errno == ENXIO) {
167 printf("No ARP entry for %s\n", host);
170 bb_perror_msg_and_die("SIOCDARP(priv)");
173 if ((flags & 1) && err) {
175 req.arp_flags |= ATF_PUBL;
176 if (option_mask32 & ARP_OPT_v)
177 bb_error_msg("SIOCDARP(pub)");
178 if (ioctl(sockfd, SIOCDARP, &req) < 0) {
179 if (errno == ENXIO) {
180 printf("No ARP entry for %s\n", host);
183 bb_perror_msg_and_die("SIOCDARP(pub)");
189 /* Get the hardware address to a specified interface name */
191 arp_getdevhw(char *ifname, struct sockaddr *sa, const struct hwtype *hwt)
194 const struct hwtype *xhw;
196 strcpy(ifr.ifr_name, ifname);
197 if (ioctl(sockfd, SIOCGIFHWADDR, &ifr) < 0) {
198 bb_perror_msg_and_die("cant get HW-Address for '%s'", ifname);
200 if (hwt && (ifr.ifr_hwaddr.sa_family != hw->type)) {
201 bb_error_msg_and_die("protocol type mismatch");
203 memcpy(sa, &(ifr.ifr_hwaddr), sizeof(struct sockaddr));
205 if (option_mask32 & ARP_OPT_v) {
206 xhw = get_hwntype(ifr.ifr_hwaddr.sa_family);
207 if (!xhw || !xhw->print) {
208 xhw = get_hwntype(-1);
210 bb_error_msg("device '%s' has HW address %s '%s'",
212 xhw->print((char *) &ifr.ifr_hwaddr.sa_data));
216 /* Set an entry in the ARP cache. */
217 /* Called only from main, once */
218 static int arp_set(char **args)
225 memset(&req, 0, sizeof(req));
227 safe_strncpy(host, *args++, 128);
228 if (ap->input(0, host, &sa) < 0) {
229 bb_herror_msg_and_die("%s", host);
231 /* If a host has more than one address, use the correct one! */
232 memcpy(&req.arp_pa, &sa, sizeof(struct sockaddr));
234 /* Fetch the hardware address. */
236 bb_error_msg_and_die("need hardware address");
238 if (option_mask32 & ARP_OPT_D) {
239 arp_getdevhw(*args++, &req.arp_ha, hw_set ? hw : NULL);
241 if (hw->input(*args++, &req.arp_ha) < 0) {
242 bb_error_msg_and_die("invalid hardware address");
246 /* Check out any modifiers. */
247 flags = ATF_PERM | ATF_COM;
248 while (*args != NULL) {
249 switch (index_in_str_array(options, *args)) {
263 flags |= ATF_USETRAILERS;
267 #ifdef HAVE_ATF_DONTPUB
268 flags |= ATF_DONTPUB;
270 bb_error_msg("feature ATF_DONTPUB is not supported");
275 #ifdef HAVE_ATF_MAGIC
278 bb_error_msg("feature ATF_MAGIC is not supported");
291 if (strcmp(*args, "255.255.255.255") != 0) {
292 safe_strncpy(host, *args++, 128);
293 if (ap->input(0, host, &sa) < 0) {
294 bb_herror_msg_and_die("%s", host);
296 memcpy(&req.arp_netmask, &sa, sizeof(struct sockaddr));
297 flags |= ATF_NETMASK;
307 /* Fill in the remainder of the request. */
308 req.arp_flags = flags;
310 strncpy(req.arp_dev, device, sizeof(req.arp_dev));
312 /* Call the kernel. */
313 if (option_mask32 & ARP_OPT_v)
314 bb_error_msg("SIOCSARP()");
315 if (ioctl(sockfd, SIOCSARP, &req) < 0) {
316 bb_perror_msg_and_die("SIOCSARP");
322 /* Print the contents of an ARP request block. */
324 arp_disp(char *name, char *ip, int type, int arp_flags,
325 char *hwa, char *mask, char *dev)
327 const struct hwtype *xhw;
329 xhw = get_hwntype(type);
331 xhw = get_hwtype(DFLT_HW);
333 printf("%s (%s) at ", name, ip);
335 if (!(arp_flags & ATF_COM)) {
336 if (arp_flags & ATF_PUBL)
339 printf("<incomplete> ");
341 printf("%s [%s] ", hwa, xhw->name);
344 if (arp_flags & ATF_NETMASK)
345 printf("netmask %s ", mask);
347 if (arp_flags & ATF_PERM)
349 if (arp_flags & ATF_PUBL)
351 #ifdef HAVE_ATF_MAGIC
352 if (arp_flags & ATF_MAGIC)
355 #ifdef HAVE_ATF_DONTPUB
356 if (arp_flags & ATF_DONTPUB)
359 if (arp_flags & ATF_USETRAILERS)
362 printf("on %s\n", dev);
365 /* Display the contents of the ARP cache in the kernel. */
366 /* Called only from main, once */
367 static int arp_show(char *name)
379 int num, entries = 0, shown = 0;
384 /* Resolve the host name. */
385 safe_strncpy(host, name, (sizeof host));
386 if (ap->input(0, host, &sa) < 0) {
387 bb_herror_msg_and_die("%s", host);
389 safe_strncpy(host, ap->sprint(&sa, 1), sizeof(host));
391 /* Open the PROCps kernel table. */
392 fp = xfopen(_PATH_PROCNET_ARP, "r");
393 /* Bypass header -- read until newline */
394 if (fgets(line, sizeof(line), fp) != (char *) NULL) {
395 mask[0] = '-'; mask[1] = '\0';
396 dev[0] = '-'; dev[1] = '\0';
397 /* Read the ARP cache entries. */
398 for (; fgets(line, sizeof(line), fp);) {
399 num = sscanf(line, "%s 0x%x 0x%x %100s %100s %100s\n",
400 ip, &type, &flags, hwa, mask, dev);
405 /* if the user specified hw-type differs, skip it */
406 if (hw_set && (type != hw->type))
409 /* if the user specified address differs, skip it */
410 if (host[0] && strcmp(ip, host) != 0)
413 /* if the user specified device differs, skip it */
414 if (device && strcmp(dev, device) != 0)
418 /* This IS ugly but it works -be */
419 if (option_mask32 & ARP_OPT_n)
422 if (ap->input(0, ip, &sa) < 0)
425 hostname = ap->sprint(&sa, (option_mask32 & ARP_OPT_n) | 0x8000);
426 if (strcmp(hostname, ip) == 0)
430 arp_disp(hostname, ip, type, flags, hwa, mask, dev);
433 if (option_mask32 & ARP_OPT_v)
434 printf("Entries: %d\tSkipped: %d\tFound: %d\n",
435 entries, entries - shown, shown);
438 if (hw_set || host[0] || device)
439 printf("No match found in %d entries\n", entries);
447 int arp_main(int argc, char **argv)
452 /* Initialize variables... */
453 ap = get_aftype(DFLT_AF);
455 bb_error_msg_and_die("%s: %s not supported", DFLT_AF, "address family");
457 getopt32(argc, argv, "A:p:H:t:i:adnDsv", &protocol, &protocol,
458 &hw_type, &hw_type, &device);
460 if (option_mask32 & ARP_OPT_A || option_mask32 & ARP_OPT_p) {
461 ap = get_aftype(protocol);
463 bb_error_msg_and_die("%s: unknown %s", protocol, "address family");
465 if (option_mask32 & ARP_OPT_A || option_mask32 & ARP_OPT_p) {
466 hw = get_hwtype(hw_type);
468 bb_error_msg_and_die("%s: unknown %s", hw_type, "hardware type");
471 //if (option_mask32 & ARP_OPT_i)... -i
473 if (ap->af != AF_INET) {
474 bb_error_msg_and_die("%s: kernel only supports 'inet'", ap->name);
477 /* If no hw type specified get default */
479 hw = get_hwtype(DFLT_HW);
481 bb_error_msg_and_die("%s: %s not supported", DFLT_HW, "hardware type");
485 bb_error_msg_and_die("%s: %s without ARP support",
486 hw->name, "hardware type");
488 sockfd = xsocket(AF_INET, SOCK_DGRAM, 0);
490 /* Now see what we have to do here... */
491 if (option_mask32 & (ARP_OPT_d|ARP_OPT_s)) {
493 bb_error_msg_and_die("need host name");
494 if (option_mask32 & ARP_OPT_s)
495 return arp_set(argv);
496 return arp_del(argv);
498 //if (option_mask32 & ARP_OPT_a) - default
499 return arp_show(argv[0]);