blob: 16783ab95327cbb43fed62221a58da4940a2fd24 [file] [log] [blame]
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +00001/* vi: set sw=4 ts=4: */
2/*
3 * arp.c - Manipulate the system ARP cache
4 *
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.
9 *
10 * Author: Fred N. van Kempen, <waltje at uwalt.nl.mugnet.org>
11 * Busybox port: Paul van Gool <pvangool at mimotech.com>
12 *
13 * modified for getopt32 by Arne Bernin <arne [at] alamut.de>
14 */
Denys Vlasenko47367e12016-11-23 09:05:14 +010015//config:config ARP
Denys Vlasenkob097a842018-12-28 03:20:17 +010016//config: bool "arp (10 kb)"
Denys Vlasenko47367e12016-11-23 09:05:14 +010017//config: default y
Denys Vlasenko47367e12016-11-23 09:05:14 +010018//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020019//config: Manipulate the system ARP cache.
Denys Vlasenko47367e12016-11-23 09:05:14 +010020
21//applet:IF_ARP(APPLET(arp, BB_DIR_SBIN, BB_SUID_DROP))
22
23//kbuild:lib-$(CONFIG_ARP) += arp.o interface.o
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +000024
Pere Orga5bc8c002011-04-11 03:29:49 +020025//usage:#define arp_trivial_usage
26//usage: "\n[-vn] [-H HWTYPE] [-i IF] -a [HOSTNAME]"
27//usage: "\n[-v] [-i IF] -d HOSTNAME [pub]"
28//usage: "\n[-v] [-H HWTYPE] [-i IF] -s HOSTNAME HWADDR [temp]"
29//usage: "\n[-v] [-H HWTYPE] [-i IF] -s HOSTNAME HWADDR [netmask MASK] pub"
30//usage: "\n[-v] [-H HWTYPE] [-i IF] -Ds HOSTNAME IFACE [netmask MASK] pub"
31//usage:#define arp_full_usage "\n\n"
32//usage: "Manipulate ARP cache\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020033//usage: "\n -a Display (all) hosts"
Denys Vlasenko06af5692013-02-04 16:18:58 +010034//usage: "\n -d Delete ARP entry"
35//usage: "\n -s Set new entry"
Pere Orga5bc8c002011-04-11 03:29:49 +020036//usage: "\n -v Verbose"
37//usage: "\n -n Don't resolve names"
38//usage: "\n -i IF Network interface"
Denys Vlasenko06af5692013-02-04 16:18:58 +010039//usage: "\n -D Read HWADDR from IFACE"
Pere Orga5bc8c002011-04-11 03:29:49 +020040//usage: "\n -A,-p AF Protocol family"
41//usage: "\n -H HWTYPE Hardware address type"
42
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000043#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020044#include "common_bufsiz.h"
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +000045#include "inet_common.h"
46
47#include <arpa/inet.h>
48#include <net/if.h>
49#include <net/if_arp.h>
50#include <netinet/ether.h>
51#include <netpacket/packet.h>
52
53#define DEBUG 0
54
55#define DFLT_AF "inet"
56#define DFLT_HW "ether"
57
Denis Vlasenko4d476922008-11-13 00:05:17 +000058enum {
59 ARP_OPT_A = (1 << 0),
60 ARP_OPT_p = (1 << 1),
61 ARP_OPT_H = (1 << 2),
62 ARP_OPT_t = (1 << 3),
63 ARP_OPT_i = (1 << 4),
64 ARP_OPT_a = (1 << 5),
65 ARP_OPT_d = (1 << 6),
66 ARP_OPT_n = (1 << 7), /* do not resolve addresses */
67 ARP_OPT_D = (1 << 8), /* HW-address is devicename */
68 ARP_OPT_s = (1 << 9),
69 ARP_OPT_v = (1 << 10) * DEBUG, /* debugging output flag */
70};
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +000071
Denis Vlasenko4d476922008-11-13 00:05:17 +000072enum {
73 sockfd = 3, /* active socket descriptor */
74};
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +000075
Denis Vlasenko4d476922008-11-13 00:05:17 +000076struct globals {
77 const struct aftype *ap; /* current address family */
78 const struct hwtype *hw; /* current hardware type */
79 const char *device; /* current device */
80 smallint hw_set; /* flag if hw-type was set (-H) */
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +010081} FIX_ALIASING;
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020082#define G (*(struct globals*)bb_common_bufsiz1)
Denis Vlasenko4d476922008-11-13 00:05:17 +000083#define ap (G.ap )
84#define hw (G.hw )
85#define device (G.device )
86#define hw_set (G.hw_set )
87#define INIT_G() do { \
Denys Vlasenko47cfbf32016-04-21 18:18:48 +020088 setup_common_bufsiz(); \
Denis Vlasenko4d476922008-11-13 00:05:17 +000089 device = ""; \
90} while (0)
91
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +000092
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000093static const char options[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +000094 "pub\0"
95 "priv\0"
96 "temp\0"
97 "trail\0"
98 "dontpub\0"
99 "auto\0"
100 "dev\0"
101 "netmask\0";
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000102
103/* Delete an entry from the ARP cache. */
104/* Called only from main, once */
105static int arp_del(char **args)
106{
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000107 char *host;
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000108 struct arpreq req;
109 struct sockaddr sa;
110 int flags = 0;
111 int err;
112
113 memset(&req, 0, sizeof(req));
114
115 /* Resolve the host name. */
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000116 host = *args;
117 if (ap->input(host, &sa) < 0) {
James Byrne69374872019-07-02 11:35:03 +0200118 bb_simple_herror_msg_and_die(host);
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000119 }
120
121 /* If a host has more than one address, use the correct one! */
122 memcpy(&req.arp_pa, &sa, sizeof(struct sockaddr));
123
124 if (hw_set)
125 req.arp_ha.sa_family = hw->type;
126
127 req.arp_flags = ATF_PERM;
128 args++;
129 while (*args != NULL) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000130 switch (index_in_strings(options, *args)) {
Bernhard Reutner-Fischer42646c52007-01-07 22:12:35 +0000131 case 0: /* "pub" */
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000132 flags |= 1;
133 args++;
134 break;
Bernhard Reutner-Fischer42646c52007-01-07 22:12:35 +0000135 case 1: /* "priv" */
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000136 flags |= 2;
137 args++;
138 break;
Bernhard Reutner-Fischer42646c52007-01-07 22:12:35 +0000139 case 2: /* "temp" */
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000140 req.arp_flags &= ~ATF_PERM;
141 args++;
142 break;
Bernhard Reutner-Fischer42646c52007-01-07 22:12:35 +0000143 case 3: /* "trail" */
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000144 req.arp_flags |= ATF_USETRAILERS;
145 args++;
146 break;
Bernhard Reutner-Fischer42646c52007-01-07 22:12:35 +0000147 case 4: /* "dontpub" */
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000148#ifdef HAVE_ATF_DONTPUB
149 req.arp_flags |= ATF_DONTPUB;
150#else
James Byrne69374872019-07-02 11:35:03 +0200151 bb_simple_error_msg("feature ATF_DONTPUB is not supported");
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000152#endif
153 args++;
154 break;
Bernhard Reutner-Fischer42646c52007-01-07 22:12:35 +0000155 case 5: /* "auto" */
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000156#ifdef HAVE_ATF_MAGIC
157 req.arp_flags |= ATF_MAGIC;
158#else
James Byrne69374872019-07-02 11:35:03 +0200159 bb_simple_error_msg("feature ATF_MAGIC is not supported");
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000160#endif
161 args++;
162 break;
Bernhard Reutner-Fischer42646c52007-01-07 22:12:35 +0000163 case 6: /* "dev" */
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000164 if (*++args == NULL)
165 bb_show_usage();
166 device = *args;
167 args++;
168 break;
Bernhard Reutner-Fischer42646c52007-01-07 22:12:35 +0000169 case 7: /* "netmask" */
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000170 if (*++args == NULL)
171 bb_show_usage();
172 if (strcmp(*args, "255.255.255.255") != 0) {
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000173 host = *args;
174 if (ap->input(host, &sa) < 0) {
James Byrne69374872019-07-02 11:35:03 +0200175 bb_simple_herror_msg_and_die(host);
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000176 }
177 memcpy(&req.arp_netmask, &sa, sizeof(struct sockaddr));
178 req.arp_flags |= ATF_NETMASK;
179 }
180 args++;
181 break;
182 default:
183 bb_show_usage();
184 break;
185 }
186 }
187 if (flags == 0)
188 flags = 3;
189
Denys Vlasenkoee772a02016-07-04 17:38:01 +0200190 strncpy_IFNAMSIZ(req.arp_dev, device);
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000191
192 err = -1;
193
194 /* Call the kernel. */
195 if (flags & 2) {
196 if (option_mask32 & ARP_OPT_v)
James Byrne69374872019-07-02 11:35:03 +0200197 bb_simple_error_msg("SIOCDARP(nopub)");
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000198 err = ioctl(sockfd, SIOCDARP, &req);
199 if (err < 0) {
200 if (errno == ENXIO) {
201 if (flags & 1)
202 goto nopub;
203 printf("No ARP entry for %s\n", host);
204 return -1;
205 }
James Byrne69374872019-07-02 11:35:03 +0200206 bb_simple_perror_msg_and_die("SIOCDARP(priv)");
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000207 }
208 }
209 if ((flags & 1) && err) {
210 nopub:
211 req.arp_flags |= ATF_PUBL;
212 if (option_mask32 & ARP_OPT_v)
James Byrne69374872019-07-02 11:35:03 +0200213 bb_simple_error_msg("SIOCDARP(pub)");
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000214 if (ioctl(sockfd, SIOCDARP, &req) < 0) {
215 if (errno == ENXIO) {
216 printf("No ARP entry for %s\n", host);
217 return -1;
218 }
James Byrne69374872019-07-02 11:35:03 +0200219 bb_simple_perror_msg_and_die("SIOCDARP(pub)");
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000220 }
221 }
222 return 0;
223}
224
225/* Get the hardware address to a specified interface name */
Denys Vlasenko06af5692013-02-04 16:18:58 +0100226static void arp_getdevhw(char *ifname, struct sockaddr *sa)
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000227{
228 struct ifreq ifr;
229 const struct hwtype *xhw;
230
Denys Vlasenkoee772a02016-07-04 17:38:01 +0200231 strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000232 ioctl_or_perror_and_die(sockfd, SIOCGIFHWADDR, &ifr,
Denys Vlasenko06af5692013-02-04 16:18:58 +0100233 "can't get HW-Address for '%s'", ifname);
234 if (hw_set && (ifr.ifr_hwaddr.sa_family != hw->type)) {
James Byrne69374872019-07-02 11:35:03 +0200235 bb_simple_error_msg_and_die("protocol type mismatch");
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000236 }
237 memcpy(sa, &(ifr.ifr_hwaddr), sizeof(struct sockaddr));
238
239 if (option_mask32 & ARP_OPT_v) {
240 xhw = get_hwntype(ifr.ifr_hwaddr.sa_family);
241 if (!xhw || !xhw->print) {
242 xhw = get_hwntype(-1);
243 }
244 bb_error_msg("device '%s' has HW address %s '%s'",
Denys Vlasenko06af5692013-02-04 16:18:58 +0100245 ifname, xhw->name,
246 xhw->print((unsigned char *) &ifr.ifr_hwaddr.sa_data));
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000247 }
248}
249
250/* Set an entry in the ARP cache. */
251/* Called only from main, once */
252static int arp_set(char **args)
253{
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000254 char *host;
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000255 struct arpreq req;
256 struct sockaddr sa;
257 int flags;
Bernhard Reutner-Fischer42646c52007-01-07 22:12:35 +0000258
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000259 memset(&req, 0, sizeof(req));
260
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000261 host = *args++;
262 if (ap->input(host, &sa) < 0) {
James Byrne69374872019-07-02 11:35:03 +0200263 bb_simple_herror_msg_and_die(host);
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000264 }
265 /* If a host has more than one address, use the correct one! */
266 memcpy(&req.arp_pa, &sa, sizeof(struct sockaddr));
267
268 /* Fetch the hardware address. */
269 if (*args == NULL) {
James Byrne69374872019-07-02 11:35:03 +0200270 bb_simple_error_msg_and_die("need hardware address");
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000271 }
272 if (option_mask32 & ARP_OPT_D) {
Denys Vlasenko06af5692013-02-04 16:18:58 +0100273 arp_getdevhw(*args++, &req.arp_ha);
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000274 } else {
275 if (hw->input(*args++, &req.arp_ha) < 0) {
James Byrne69374872019-07-02 11:35:03 +0200276 bb_simple_error_msg_and_die("invalid hardware address");
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000277 }
278 }
279
280 /* Check out any modifiers. */
281 flags = ATF_PERM | ATF_COM;
282 while (*args != NULL) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000283 switch (index_in_strings(options, *args)) {
Bernhard Reutner-Fischer42646c52007-01-07 22:12:35 +0000284 case 0: /* "pub" */
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000285 flags |= ATF_PUBL;
286 args++;
287 break;
Bernhard Reutner-Fischer42646c52007-01-07 22:12:35 +0000288 case 1: /* "priv" */
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000289 flags &= ~ATF_PUBL;
290 args++;
291 break;
Bernhard Reutner-Fischer42646c52007-01-07 22:12:35 +0000292 case 2: /* "temp" */
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000293 flags &= ~ATF_PERM;
294 args++;
295 break;
Bernhard Reutner-Fischer42646c52007-01-07 22:12:35 +0000296 case 3: /* "trail" */
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000297 flags |= ATF_USETRAILERS;
298 args++;
299 break;
Bernhard Reutner-Fischer42646c52007-01-07 22:12:35 +0000300 case 4: /* "dontpub" */
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000301#ifdef HAVE_ATF_DONTPUB
302 flags |= ATF_DONTPUB;
303#else
James Byrne69374872019-07-02 11:35:03 +0200304 bb_simple_error_msg("feature ATF_DONTPUB is not supported");
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000305#endif
306 args++;
307 break;
Bernhard Reutner-Fischer42646c52007-01-07 22:12:35 +0000308 case 5: /* "auto" */
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000309#ifdef HAVE_ATF_MAGIC
310 flags |= ATF_MAGIC;
311#else
James Byrne69374872019-07-02 11:35:03 +0200312 bb_simple_error_msg("feature ATF_MAGIC is not supported");
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000313#endif
314 args++;
315 break;
Bernhard Reutner-Fischer42646c52007-01-07 22:12:35 +0000316 case 6: /* "dev" */
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000317 if (*++args == NULL)
318 bb_show_usage();
319 device = *args;
320 args++;
321 break;
Bernhard Reutner-Fischer42646c52007-01-07 22:12:35 +0000322 case 7: /* "netmask" */
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000323 if (*++args == NULL)
324 bb_show_usage();
325 if (strcmp(*args, "255.255.255.255") != 0) {
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000326 host = *args;
327 if (ap->input(host, &sa) < 0) {
James Byrne69374872019-07-02 11:35:03 +0200328 bb_simple_herror_msg_and_die(host);
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000329 }
330 memcpy(&req.arp_netmask, &sa, sizeof(struct sockaddr));
331 flags |= ATF_NETMASK;
332 }
333 args++;
334 break;
335 default:
336 bb_show_usage();
337 break;
338 }
339 }
340
341 /* Fill in the remainder of the request. */
342 req.arp_flags = flags;
343
Denys Vlasenkoee772a02016-07-04 17:38:01 +0200344 strncpy_IFNAMSIZ(req.arp_dev, device);
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000345
346 /* Call the kernel. */
347 if (option_mask32 & ARP_OPT_v)
James Byrne69374872019-07-02 11:35:03 +0200348 bb_simple_error_msg("SIOCSARP()");
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000349 xioctl(sockfd, SIOCSARP, &req);
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000350 return 0;
351}
352
353
354/* Print the contents of an ARP request block. */
355static void
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000356arp_disp(const char *name, char *ip, int type, int arp_flags,
Denys Vlasenko69675782013-01-14 01:34:48 +0100357 char *hwa, char *mask, char *dev)
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000358{
Denis Vlasenko53354ac2008-06-07 15:10:29 +0000359 static const int arp_masks[] = {
Denis Vlasenkof45c4f42008-06-16 04:09:25 +0000360 ATF_PERM, ATF_PUBL,
Denis Vlasenko53354ac2008-06-07 15:10:29 +0000361#ifdef HAVE_ATF_MAGIC
362 ATF_MAGIC,
363#endif
364#ifdef HAVE_ATF_DONTPUB
365 ATF_DONTPUB,
366#endif
367 ATF_USETRAILERS,
368 };
369 static const char arp_labels[] ALIGN1 = "PERM\0""PUP\0"
370#ifdef HAVE_ATF_MAGIC
371 "AUTO\0"
372#endif
373#ifdef HAVE_ATF_DONTPUB
374 "DONTPUB\0"
375#endif
376 "TRAIL\0"
377 ;
378
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000379 const struct hwtype *xhw;
380
381 xhw = get_hwntype(type);
382 if (xhw == NULL)
383 xhw = get_hwtype(DFLT_HW);
384
385 printf("%s (%s) at ", name, ip);
386
387 if (!(arp_flags & ATF_COM)) {
388 if (arp_flags & ATF_PUBL)
389 printf("* ");
390 else
391 printf("<incomplete> ");
392 } else {
393 printf("%s [%s] ", hwa, xhw->name);
394 }
395
396 if (arp_flags & ATF_NETMASK)
397 printf("netmask %s ", mask);
398
Denis Vlasenko53354ac2008-06-07 15:10:29 +0000399 print_flags_separated(arp_masks, arp_labels, arp_flags, " ");
400 printf(" on %s\n", dev);
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000401}
402
403/* Display the contents of the ARP cache in the kernel. */
404/* Called only from main, once */
405static int arp_show(char *name)
406{
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000407 const char *host;
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000408 const char *hostname;
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000409 FILE *fp;
410 struct sockaddr sa;
411 int type, flags;
Bernhard Reutner-Fischer42646c52007-01-07 22:12:35 +0000412 int num;
413 unsigned entries = 0, shown = 0;
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000414 char ip[128];
415 char hwa[128];
416 char mask[128];
417 char line[128];
418 char dev[128];
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000419
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000420 host = NULL;
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000421 if (name != NULL) {
422 /* Resolve the host name. */
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000423 if (ap->input(name, &sa) < 0) {
James Byrne69374872019-07-02 11:35:03 +0200424 bb_simple_herror_msg_and_die(name);
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000425 }
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000426 host = xstrdup(ap->sprint(&sa, 1));
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000427 }
Denis Vlasenko5415c852008-07-21 23:05:26 +0000428 fp = xfopen_for_read("/proc/net/arp");
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000429 /* Bypass header -- read one line */
430 fgets(line, sizeof(line), fp);
431
432 /* Read the ARP cache entries. */
433 while (fgets(line, sizeof(line), fp)) {
434
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000435 mask[0] = '-'; mask[1] = '\0';
436 dev[0] = '-'; dev[1] = '\0';
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000437 /* All these strings can't overflow
438 * because fgets above reads limited amount of data */
439 num = sscanf(line, "%s 0x%x 0x%x %s %s %s\n",
Denys Vlasenko69675782013-01-14 01:34:48 +0100440 ip, &type, &flags, hwa, mask, dev);
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000441 if (num < 4)
442 break;
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000443
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000444 entries++;
445 /* if the user specified hw-type differs, skip it */
446 if (hw_set && (type != hw->type))
447 continue;
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000448
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000449 /* if the user specified address differs, skip it */
450 if (host && strcmp(ip, host) != 0)
451 continue;
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000452
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000453 /* if the user specified device differs, skip it */
454 if (device[0] && strcmp(dev, device) != 0)
455 continue;
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000456
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000457 shown++;
458 /* This IS ugly but it works -be */
459 hostname = "?";
460 if (!(option_mask32 & ARP_OPT_n)) {
461 if (ap->input(ip, &sa) < 0)
462 hostname = ip;
463 else
464 hostname = ap->sprint(&sa, (option_mask32 & ARP_OPT_n) | 0x8000);
465 if (strcmp(hostname, ip) == 0)
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000466 hostname = "?";
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000467 }
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000468
469 arp_disp(hostname, ip, type, flags, hwa, mask, dev);
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000470 }
471 if (option_mask32 & ARP_OPT_v)
Denys Vlasenko327f5502013-11-29 16:45:45 +0100472 printf("Entries: %u\tSkipped: %u\tFound: %u\n",
Denys Vlasenko69675782013-01-14 01:34:48 +0100473 entries, entries - shown, shown);
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000474
475 if (!shown) {
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000476 if (hw_set || host || device[0])
Denys Vlasenko327f5502013-11-29 16:45:45 +0100477 printf("No match found in %u entries\n", entries);
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000478 }
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000479 if (ENABLE_FEATURE_CLEAN_UP) {
480 free((char*)host);
481 fclose(fp);
482 }
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000483 return 0;
484}
485
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000486int arp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000487int arp_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000488{
Kuleshov Alekseya8816da2013-02-04 15:14:20 +0100489 const char *hw_type;
Denis Vlasenkoec7e7ae2008-08-15 20:14:23 +0000490 const char *protocol;
Denis Vlasenko4d476922008-11-13 00:05:17 +0000491 unsigned opts;
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000492
Denis Vlasenko4d476922008-11-13 00:05:17 +0000493 INIT_G();
494
495 xmove_fd(xsocket(AF_INET, SOCK_DGRAM, 0), sockfd);
Kuleshov Alekseya8816da2013-02-04 15:14:20 +0100496
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000497 ap = get_aftype(DFLT_AF);
Kuleshov Alekseya8816da2013-02-04 15:14:20 +0100498 /* Defaults are always supported */
499 //if (!ap)
500 // bb_error_msg_and_die("%s: %s not supported", DFLT_AF, "address family");
501 hw = get_hwtype(DFLT_HW);
502 //if (!hw)
503 // bb_error_msg_and_die("%s: %s not supported", DFLT_HW, "hardware type");
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000504
Denis Vlasenko4d476922008-11-13 00:05:17 +0000505 opts = getopt32(argv, "A:p:H:t:i:adnDsv", &protocol, &protocol,
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000506 &hw_type, &hw_type, &device);
507 argv += optind;
Denis Vlasenko4d476922008-11-13 00:05:17 +0000508 if (opts & (ARP_OPT_A | ARP_OPT_p)) {
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000509 ap = get_aftype(protocol);
Kuleshov Alekseya8816da2013-02-04 15:14:20 +0100510 if (!ap)
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000511 bb_error_msg_and_die("%s: unknown %s", protocol, "address family");
512 }
Kuleshov Alekseya8816da2013-02-04 15:14:20 +0100513 if (opts & (ARP_OPT_H | ARP_OPT_t)) {
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000514 hw = get_hwtype(hw_type);
Kuleshov Alekseya8816da2013-02-04 15:14:20 +0100515 if (!hw)
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000516 bb_error_msg_and_die("%s: unknown %s", hw_type, "hardware type");
517 hw_set = 1;
518 }
Denis Vlasenko4d476922008-11-13 00:05:17 +0000519 //if (opts & ARP_OPT_i)... -i
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000520
521 if (ap->af != AF_INET) {
522 bb_error_msg_and_die("%s: kernel only supports 'inet'", ap->name);
523 }
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000524 if (hw->alen <= 0) {
525 bb_error_msg_and_die("%s: %s without ARP support",
Denys Vlasenko69675782013-01-14 01:34:48 +0100526 hw->name, "hardware type");
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000527 }
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000528
529 /* Now see what we have to do here... */
Denis Vlasenko4d476922008-11-13 00:05:17 +0000530 if (opts & (ARP_OPT_d | ARP_OPT_s)) {
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000531 if (argv[0] == NULL)
James Byrne69374872019-07-02 11:35:03 +0200532 bb_simple_error_msg_and_die("need host name");
Denis Vlasenko4d476922008-11-13 00:05:17 +0000533 if (opts & ARP_OPT_s)
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000534 return arp_set(argv);
535 return arp_del(argv);
536 }
Kuleshov Alekseya8816da2013-02-04 15:14:20 +0100537
Denis Vlasenko4d476922008-11-13 00:05:17 +0000538 //if (opts & ARP_OPT_a) - default
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000539 return arp_show(argv[0]);
540}