blob: 3f68f5cf7b1abbb93dcd5aecca879c70c3f62b99 [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 */
15
Pere Orga5bc8c002011-04-11 03:29:49 +020016//usage:#define arp_trivial_usage
17//usage: "\n[-vn] [-H HWTYPE] [-i IF] -a [HOSTNAME]"
18//usage: "\n[-v] [-i IF] -d HOSTNAME [pub]"
19//usage: "\n[-v] [-H HWTYPE] [-i IF] -s HOSTNAME HWADDR [temp]"
20//usage: "\n[-v] [-H HWTYPE] [-i IF] -s HOSTNAME HWADDR [netmask MASK] pub"
21//usage: "\n[-v] [-H HWTYPE] [-i IF] -Ds HOSTNAME IFACE [netmask MASK] pub"
22//usage:#define arp_full_usage "\n\n"
23//usage: "Manipulate ARP cache\n"
24//usage: "\nOptions:"
25//usage: "\n -a Display (all) hosts"
26//usage: "\n -s Set new ARP entry"
27//usage: "\n -d Delete a specified entry"
28//usage: "\n -v Verbose"
29//usage: "\n -n Don't resolve names"
30//usage: "\n -i IF Network interface"
31//usage: "\n -D Read <hwaddr> from given device"
32//usage: "\n -A,-p AF Protocol family"
33//usage: "\n -H HWTYPE Hardware address type"
34
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000035#include "libbb.h"
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +000036#include "inet_common.h"
37
38#include <arpa/inet.h>
39#include <net/if.h>
40#include <net/if_arp.h>
41#include <netinet/ether.h>
42#include <netpacket/packet.h>
43
44#define DEBUG 0
45
46#define DFLT_AF "inet"
47#define DFLT_HW "ether"
48
Denis Vlasenko4d476922008-11-13 00:05:17 +000049enum {
50 ARP_OPT_A = (1 << 0),
51 ARP_OPT_p = (1 << 1),
52 ARP_OPT_H = (1 << 2),
53 ARP_OPT_t = (1 << 3),
54 ARP_OPT_i = (1 << 4),
55 ARP_OPT_a = (1 << 5),
56 ARP_OPT_d = (1 << 6),
57 ARP_OPT_n = (1 << 7), /* do not resolve addresses */
58 ARP_OPT_D = (1 << 8), /* HW-address is devicename */
59 ARP_OPT_s = (1 << 9),
60 ARP_OPT_v = (1 << 10) * DEBUG, /* debugging output flag */
61};
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +000062
Denis Vlasenko4d476922008-11-13 00:05:17 +000063enum {
64 sockfd = 3, /* active socket descriptor */
65};
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +000066
Denis Vlasenko4d476922008-11-13 00:05:17 +000067struct globals {
68 const struct aftype *ap; /* current address family */
69 const struct hwtype *hw; /* current hardware type */
70 const char *device; /* current device */
71 smallint hw_set; /* flag if hw-type was set (-H) */
72
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +010073} FIX_ALIASING;
Denis Vlasenko4d476922008-11-13 00:05:17 +000074#define G (*(struct globals*)&bb_common_bufsiz1)
75#define ap (G.ap )
76#define hw (G.hw )
77#define device (G.device )
78#define hw_set (G.hw_set )
79#define INIT_G() do { \
80 device = ""; \
81} while (0)
82
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +000083
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000084static const char options[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +000085 "pub\0"
86 "priv\0"
87 "temp\0"
88 "trail\0"
89 "dontpub\0"
90 "auto\0"
91 "dev\0"
92 "netmask\0";
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +000093
94/* Delete an entry from the ARP cache. */
95/* Called only from main, once */
96static int arp_del(char **args)
97{
Denis Vlasenko7f2527e2007-03-14 22:11:20 +000098 char *host;
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +000099 struct arpreq req;
100 struct sockaddr sa;
101 int flags = 0;
102 int err;
103
104 memset(&req, 0, sizeof(req));
105
106 /* Resolve the host name. */
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000107 host = *args;
108 if (ap->input(host, &sa) < 0) {
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000109 bb_herror_msg_and_die("%s", host);
110 }
111
112 /* If a host has more than one address, use the correct one! */
113 memcpy(&req.arp_pa, &sa, sizeof(struct sockaddr));
114
115 if (hw_set)
116 req.arp_ha.sa_family = hw->type;
117
118 req.arp_flags = ATF_PERM;
119 args++;
120 while (*args != NULL) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000121 switch (index_in_strings(options, *args)) {
Bernhard Reutner-Fischer42646c52007-01-07 22:12:35 +0000122 case 0: /* "pub" */
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000123 flags |= 1;
124 args++;
125 break;
Bernhard Reutner-Fischer42646c52007-01-07 22:12:35 +0000126 case 1: /* "priv" */
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000127 flags |= 2;
128 args++;
129 break;
Bernhard Reutner-Fischer42646c52007-01-07 22:12:35 +0000130 case 2: /* "temp" */
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000131 req.arp_flags &= ~ATF_PERM;
132 args++;
133 break;
Bernhard Reutner-Fischer42646c52007-01-07 22:12:35 +0000134 case 3: /* "trail" */
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000135 req.arp_flags |= ATF_USETRAILERS;
136 args++;
137 break;
Bernhard Reutner-Fischer42646c52007-01-07 22:12:35 +0000138 case 4: /* "dontpub" */
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000139#ifdef HAVE_ATF_DONTPUB
140 req.arp_flags |= ATF_DONTPUB;
141#else
142 bb_error_msg("feature ATF_DONTPUB is not supported");
143#endif
144 args++;
145 break;
Bernhard Reutner-Fischer42646c52007-01-07 22:12:35 +0000146 case 5: /* "auto" */
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000147#ifdef HAVE_ATF_MAGIC
148 req.arp_flags |= ATF_MAGIC;
149#else
150 bb_error_msg("feature ATF_MAGIC is not supported");
151#endif
152 args++;
153 break;
Bernhard Reutner-Fischer42646c52007-01-07 22:12:35 +0000154 case 6: /* "dev" */
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000155 if (*++args == NULL)
156 bb_show_usage();
157 device = *args;
158 args++;
159 break;
Bernhard Reutner-Fischer42646c52007-01-07 22:12:35 +0000160 case 7: /* "netmask" */
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000161 if (*++args == NULL)
162 bb_show_usage();
163 if (strcmp(*args, "255.255.255.255") != 0) {
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000164 host = *args;
165 if (ap->input(host, &sa) < 0) {
Bernhard Reutner-Fischer42646c52007-01-07 22:12:35 +0000166 bb_herror_msg_and_die("%s", host);
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000167 }
168 memcpy(&req.arp_netmask, &sa, sizeof(struct sockaddr));
169 req.arp_flags |= ATF_NETMASK;
170 }
171 args++;
172 break;
173 default:
174 bb_show_usage();
175 break;
176 }
177 }
178 if (flags == 0)
179 flags = 3;
180
181 strncpy(req.arp_dev, device, sizeof(req.arp_dev));
182
183 err = -1;
184
185 /* Call the kernel. */
186 if (flags & 2) {
187 if (option_mask32 & ARP_OPT_v)
188 bb_error_msg("SIOCDARP(nopub)");
189 err = ioctl(sockfd, SIOCDARP, &req);
190 if (err < 0) {
191 if (errno == ENXIO) {
192 if (flags & 1)
193 goto nopub;
194 printf("No ARP entry for %s\n", host);
195 return -1;
196 }
197 bb_perror_msg_and_die("SIOCDARP(priv)");
198 }
199 }
200 if ((flags & 1) && err) {
201 nopub:
202 req.arp_flags |= ATF_PUBL;
203 if (option_mask32 & ARP_OPT_v)
204 bb_error_msg("SIOCDARP(pub)");
205 if (ioctl(sockfd, SIOCDARP, &req) < 0) {
206 if (errno == ENXIO) {
207 printf("No ARP entry for %s\n", host);
208 return -1;
209 }
210 bb_perror_msg_and_die("SIOCDARP(pub)");
211 }
212 }
213 return 0;
214}
215
216/* Get the hardware address to a specified interface name */
Bernhard Reutner-Fischer42646c52007-01-07 22:12:35 +0000217static void arp_getdevhw(char *ifname, struct sockaddr *sa,
218 const struct hwtype *hwt)
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000219{
220 struct ifreq ifr;
221 const struct hwtype *xhw;
222
223 strcpy(ifr.ifr_name, ifname);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000224 ioctl_or_perror_and_die(sockfd, SIOCGIFHWADDR, &ifr,
225 "cant get HW-Address for '%s'", ifname);
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000226 if (hwt && (ifr.ifr_hwaddr.sa_family != hw->type)) {
227 bb_error_msg_and_die("protocol type mismatch");
228 }
229 memcpy(sa, &(ifr.ifr_hwaddr), sizeof(struct sockaddr));
230
231 if (option_mask32 & ARP_OPT_v) {
232 xhw = get_hwntype(ifr.ifr_hwaddr.sa_family);
233 if (!xhw || !xhw->print) {
234 xhw = get_hwntype(-1);
235 }
236 bb_error_msg("device '%s' has HW address %s '%s'",
237 ifname, xhw->name,
Denis Vlasenko023dc672008-05-09 18:07:15 +0000238 xhw->print((unsigned char *) &ifr.ifr_hwaddr.sa_data));
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000239 }
240}
241
242/* Set an entry in the ARP cache. */
243/* Called only from main, once */
244static int arp_set(char **args)
245{
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000246 char *host;
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000247 struct arpreq req;
248 struct sockaddr sa;
249 int flags;
Bernhard Reutner-Fischer42646c52007-01-07 22:12:35 +0000250
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000251 memset(&req, 0, sizeof(req));
252
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000253 host = *args++;
254 if (ap->input(host, &sa) < 0) {
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000255 bb_herror_msg_and_die("%s", host);
256 }
257 /* If a host has more than one address, use the correct one! */
258 memcpy(&req.arp_pa, &sa, sizeof(struct sockaddr));
259
260 /* Fetch the hardware address. */
261 if (*args == NULL) {
262 bb_error_msg_and_die("need hardware address");
263 }
264 if (option_mask32 & ARP_OPT_D) {
265 arp_getdevhw(*args++, &req.arp_ha, hw_set ? hw : NULL);
266 } else {
267 if (hw->input(*args++, &req.arp_ha) < 0) {
268 bb_error_msg_and_die("invalid hardware address");
269 }
270 }
271
272 /* Check out any modifiers. */
273 flags = ATF_PERM | ATF_COM;
274 while (*args != NULL) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000275 switch (index_in_strings(options, *args)) {
Bernhard Reutner-Fischer42646c52007-01-07 22:12:35 +0000276 case 0: /* "pub" */
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000277 flags |= ATF_PUBL;
278 args++;
279 break;
Bernhard Reutner-Fischer42646c52007-01-07 22:12:35 +0000280 case 1: /* "priv" */
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000281 flags &= ~ATF_PUBL;
282 args++;
283 break;
Bernhard Reutner-Fischer42646c52007-01-07 22:12:35 +0000284 case 2: /* "temp" */
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000285 flags &= ~ATF_PERM;
286 args++;
287 break;
Bernhard Reutner-Fischer42646c52007-01-07 22:12:35 +0000288 case 3: /* "trail" */
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000289 flags |= ATF_USETRAILERS;
290 args++;
291 break;
Bernhard Reutner-Fischer42646c52007-01-07 22:12:35 +0000292 case 4: /* "dontpub" */
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000293#ifdef HAVE_ATF_DONTPUB
294 flags |= ATF_DONTPUB;
295#else
296 bb_error_msg("feature ATF_DONTPUB is not supported");
297#endif
298 args++;
299 break;
Bernhard Reutner-Fischer42646c52007-01-07 22:12:35 +0000300 case 5: /* "auto" */
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000301#ifdef HAVE_ATF_MAGIC
302 flags |= ATF_MAGIC;
303#else
304 bb_error_msg("feature ATF_MAGIC is not supported");
305#endif
306 args++;
307 break;
Bernhard Reutner-Fischer42646c52007-01-07 22:12:35 +0000308 case 6: /* "dev" */
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000309 if (*++args == NULL)
310 bb_show_usage();
311 device = *args;
312 args++;
313 break;
Bernhard Reutner-Fischer42646c52007-01-07 22:12:35 +0000314 case 7: /* "netmask" */
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000315 if (*++args == NULL)
316 bb_show_usage();
317 if (strcmp(*args, "255.255.255.255") != 0) {
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000318 host = *args;
319 if (ap->input(host, &sa) < 0) {
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000320 bb_herror_msg_and_die("%s", host);
321 }
322 memcpy(&req.arp_netmask, &sa, sizeof(struct sockaddr));
323 flags |= ATF_NETMASK;
324 }
325 args++;
326 break;
327 default:
328 bb_show_usage();
329 break;
330 }
331 }
332
333 /* Fill in the remainder of the request. */
334 req.arp_flags = flags;
335
336 strncpy(req.arp_dev, device, sizeof(req.arp_dev));
337
338 /* Call the kernel. */
339 if (option_mask32 & ARP_OPT_v)
340 bb_error_msg("SIOCSARP()");
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000341 xioctl(sockfd, SIOCSARP, &req);
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000342 return 0;
343}
344
345
346/* Print the contents of an ARP request block. */
347static void
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000348arp_disp(const char *name, char *ip, int type, int arp_flags,
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000349 char *hwa, char *mask, char *dev)
350{
Denis Vlasenko53354ac2008-06-07 15:10:29 +0000351 static const int arp_masks[] = {
Denis Vlasenkof45c4f42008-06-16 04:09:25 +0000352 ATF_PERM, ATF_PUBL,
Denis Vlasenko53354ac2008-06-07 15:10:29 +0000353#ifdef HAVE_ATF_MAGIC
354 ATF_MAGIC,
355#endif
356#ifdef HAVE_ATF_DONTPUB
357 ATF_DONTPUB,
358#endif
359 ATF_USETRAILERS,
360 };
361 static const char arp_labels[] ALIGN1 = "PERM\0""PUP\0"
362#ifdef HAVE_ATF_MAGIC
363 "AUTO\0"
364#endif
365#ifdef HAVE_ATF_DONTPUB
366 "DONTPUB\0"
367#endif
368 "TRAIL\0"
369 ;
370
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000371 const struct hwtype *xhw;
372
373 xhw = get_hwntype(type);
374 if (xhw == NULL)
375 xhw = get_hwtype(DFLT_HW);
376
377 printf("%s (%s) at ", name, ip);
378
379 if (!(arp_flags & ATF_COM)) {
380 if (arp_flags & ATF_PUBL)
381 printf("* ");
382 else
383 printf("<incomplete> ");
384 } else {
385 printf("%s [%s] ", hwa, xhw->name);
386 }
387
388 if (arp_flags & ATF_NETMASK)
389 printf("netmask %s ", mask);
390
Denis Vlasenko53354ac2008-06-07 15:10:29 +0000391 print_flags_separated(arp_masks, arp_labels, arp_flags, " ");
392 printf(" on %s\n", dev);
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000393}
394
395/* Display the contents of the ARP cache in the kernel. */
396/* Called only from main, once */
397static int arp_show(char *name)
398{
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000399 const char *host;
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000400 const char *hostname;
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000401 FILE *fp;
402 struct sockaddr sa;
403 int type, flags;
Bernhard Reutner-Fischer42646c52007-01-07 22:12:35 +0000404 int num;
405 unsigned entries = 0, shown = 0;
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000406 char ip[128];
407 char hwa[128];
408 char mask[128];
409 char line[128];
410 char dev[128];
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000411
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000412 host = NULL;
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000413 if (name != NULL) {
414 /* Resolve the host name. */
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000415 if (ap->input(name, &sa) < 0) {
416 bb_herror_msg_and_die("%s", name);
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000417 }
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000418 host = xstrdup(ap->sprint(&sa, 1));
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000419 }
Denis Vlasenko5415c852008-07-21 23:05:26 +0000420 fp = xfopen_for_read("/proc/net/arp");
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000421 /* Bypass header -- read one line */
422 fgets(line, sizeof(line), fp);
423
424 /* Read the ARP cache entries. */
425 while (fgets(line, sizeof(line), fp)) {
426
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000427 mask[0] = '-'; mask[1] = '\0';
428 dev[0] = '-'; dev[1] = '\0';
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000429 /* All these strings can't overflow
430 * because fgets above reads limited amount of data */
431 num = sscanf(line, "%s 0x%x 0x%x %s %s %s\n",
432 ip, &type, &flags, hwa, mask, dev);
433 if (num < 4)
434 break;
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000435
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000436 entries++;
437 /* if the user specified hw-type differs, skip it */
438 if (hw_set && (type != hw->type))
439 continue;
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000440
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000441 /* if the user specified address differs, skip it */
442 if (host && strcmp(ip, host) != 0)
443 continue;
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000444
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000445 /* if the user specified device differs, skip it */
446 if (device[0] && strcmp(dev, device) != 0)
447 continue;
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000448
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000449 shown++;
450 /* This IS ugly but it works -be */
451 hostname = "?";
452 if (!(option_mask32 & ARP_OPT_n)) {
453 if (ap->input(ip, &sa) < 0)
454 hostname = ip;
455 else
456 hostname = ap->sprint(&sa, (option_mask32 & ARP_OPT_n) | 0x8000);
457 if (strcmp(hostname, ip) == 0)
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000458 hostname = "?";
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000459 }
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000460
461 arp_disp(hostname, ip, type, flags, hwa, mask, dev);
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000462 }
463 if (option_mask32 & ARP_OPT_v)
464 printf("Entries: %d\tSkipped: %d\tFound: %d\n",
465 entries, entries - shown, shown);
466
467 if (!shown) {
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000468 if (hw_set || host || device[0])
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000469 printf("No match found in %d entries\n", entries);
470 }
Denis Vlasenko7f2527e2007-03-14 22:11:20 +0000471 if (ENABLE_FEATURE_CLEAN_UP) {
472 free((char*)host);
473 fclose(fp);
474 }
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000475 return 0;
476}
477
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000478int arp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000479int arp_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000480{
Denis Vlasenkoec7e7ae2008-08-15 20:14:23 +0000481 const char *hw_type = "ether";
482 const char *protocol;
Denis Vlasenko4d476922008-11-13 00:05:17 +0000483 unsigned opts;
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000484
Denis Vlasenko4d476922008-11-13 00:05:17 +0000485 INIT_G();
486
487 xmove_fd(xsocket(AF_INET, SOCK_DGRAM, 0), sockfd);
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000488 ap = get_aftype(DFLT_AF);
489 if (!ap)
490 bb_error_msg_and_die("%s: %s not supported", DFLT_AF, "address family");
491
Denis Vlasenko4d476922008-11-13 00:05:17 +0000492 opts = getopt32(argv, "A:p:H:t:i:adnDsv", &protocol, &protocol,
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000493 &hw_type, &hw_type, &device);
494 argv += optind;
Denis Vlasenko4d476922008-11-13 00:05:17 +0000495 if (opts & (ARP_OPT_A | ARP_OPT_p)) {
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000496 ap = get_aftype(protocol);
497 if (ap == NULL)
498 bb_error_msg_and_die("%s: unknown %s", protocol, "address family");
499 }
Denis Vlasenko4d476922008-11-13 00:05:17 +0000500 if (opts & (ARP_OPT_A | ARP_OPT_p)) {
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000501 hw = get_hwtype(hw_type);
502 if (hw == NULL)
503 bb_error_msg_and_die("%s: unknown %s", hw_type, "hardware type");
504 hw_set = 1;
505 }
Denis Vlasenko4d476922008-11-13 00:05:17 +0000506 //if (opts & ARP_OPT_i)... -i
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000507
508 if (ap->af != AF_INET) {
509 bb_error_msg_and_die("%s: kernel only supports 'inet'", ap->name);
510 }
511
512 /* If no hw type specified get default */
513 if (!hw) {
514 hw = get_hwtype(DFLT_HW);
515 if (!hw)
516 bb_error_msg_and_die("%s: %s not supported", DFLT_HW, "hardware type");
517 }
518
519 if (hw->alen <= 0) {
520 bb_error_msg_and_die("%s: %s without ARP support",
521 hw->name, "hardware type");
522 }
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000523
524 /* Now see what we have to do here... */
Denis Vlasenko4d476922008-11-13 00:05:17 +0000525 if (opts & (ARP_OPT_d | ARP_OPT_s)) {
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000526 if (argv[0] == NULL)
527 bb_error_msg_and_die("need host name");
Denis Vlasenko4d476922008-11-13 00:05:17 +0000528 if (opts & ARP_OPT_s)
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000529 return arp_set(argv);
530 return arp_del(argv);
531 }
Denis Vlasenko4d476922008-11-13 00:05:17 +0000532 //if (opts & ARP_OPT_a) - default
Denis Vlasenko88e2b1c2007-01-07 19:35:11 +0000533 return arp_show(argv[0]);
534}