*: remove "Options:" string from help texts
[oweals/busybox.git] / networking / arp.c
1 /* 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
16 //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:       "\n      -a              Display (all) hosts"
25 //usage:       "\n      -s              Set new ARP entry"
26 //usage:       "\n      -d              Delete a specified entry"
27 //usage:       "\n      -v              Verbose"
28 //usage:       "\n      -n              Don't resolve names"
29 //usage:       "\n      -i IF           Network interface"
30 //usage:       "\n      -D              Read <hwaddr> from given device"
31 //usage:       "\n      -A,-p AF        Protocol family"
32 //usage:       "\n      -H HWTYPE       Hardware address type"
33
34 #include "libbb.h"
35 #include "inet_common.h"
36
37 #include <arpa/inet.h>
38 #include <net/if.h>
39 #include <net/if_arp.h>
40 #include <netinet/ether.h>
41 #include <netpacket/packet.h>
42
43 #define DEBUG 0
44
45 #define DFLT_AF "inet"
46 #define DFLT_HW "ether"
47
48 enum {
49         ARP_OPT_A = (1 << 0),
50         ARP_OPT_p = (1 << 1),
51         ARP_OPT_H = (1 << 2),
52         ARP_OPT_t = (1 << 3),
53         ARP_OPT_i = (1 << 4),
54         ARP_OPT_a = (1 << 5),
55         ARP_OPT_d = (1 << 6),
56         ARP_OPT_n = (1 << 7), /* do not resolve addresses */
57         ARP_OPT_D = (1 << 8), /* HW-address is devicename */
58         ARP_OPT_s = (1 << 9),
59         ARP_OPT_v = (1 << 10) * DEBUG, /* debugging output flag */
60 };
61
62 enum {
63         sockfd = 3, /* active socket descriptor */
64 };
65
66 struct globals {
67         const struct aftype *ap; /* current address family */
68         const struct hwtype *hw; /* current hardware type */
69         const char *device;      /* current device */
70         smallint hw_set;         /* flag if hw-type was set (-H) */
71
72 } FIX_ALIASING;
73 #define G (*(struct globals*)&bb_common_bufsiz1)
74 #define ap         (G.ap        )
75 #define hw         (G.hw        )
76 #define device     (G.device    )
77 #define hw_set     (G.hw_set    )
78 #define INIT_G() do { \
79         device = ""; \
80 } while (0)
81
82
83 static const char options[] ALIGN1 =
84         "pub\0"
85         "priv\0"
86         "temp\0"
87         "trail\0"
88         "dontpub\0"
89         "auto\0"
90         "dev\0"
91         "netmask\0";
92
93 /* Delete an entry from the ARP cache. */
94 /* Called only from main, once */
95 static int arp_del(char **args)
96 {
97         char *host;
98         struct arpreq req;
99         struct sockaddr sa;
100         int flags = 0;
101         int err;
102
103         memset(&req, 0, sizeof(req));
104
105         /* Resolve the host name. */
106         host = *args;
107         if (ap->input(host, &sa) < 0) {
108                 bb_herror_msg_and_die("%s", host);
109         }
110
111         /* If a host has more than one address, use the correct one! */
112         memcpy(&req.arp_pa, &sa, sizeof(struct sockaddr));
113
114         if (hw_set)
115                 req.arp_ha.sa_family = hw->type;
116
117         req.arp_flags = ATF_PERM;
118         args++;
119         while (*args != NULL) {
120                 switch (index_in_strings(options, *args)) {
121                 case 0: /* "pub" */
122                         flags |= 1;
123                         args++;
124                         break;
125                 case 1: /* "priv" */
126                         flags |= 2;
127                         args++;
128                         break;
129                 case 2: /* "temp" */
130                         req.arp_flags &= ~ATF_PERM;
131                         args++;
132                         break;
133                 case 3: /* "trail" */
134                         req.arp_flags |= ATF_USETRAILERS;
135                         args++;
136                         break;
137                 case 4: /* "dontpub" */
138 #ifdef HAVE_ATF_DONTPUB
139                         req.arp_flags |= ATF_DONTPUB;
140 #else
141                         bb_error_msg("feature ATF_DONTPUB is not supported");
142 #endif
143                         args++;
144                         break;
145                 case 5: /* "auto" */
146 #ifdef HAVE_ATF_MAGIC
147                         req.arp_flags |= ATF_MAGIC;
148 #else
149                         bb_error_msg("feature ATF_MAGIC is not supported");
150 #endif
151                         args++;
152                         break;
153                 case 6: /* "dev" */
154                         if (*++args == NULL)
155                                 bb_show_usage();
156                         device = *args;
157                         args++;
158                         break;
159                 case 7: /* "netmask" */
160                         if (*++args == NULL)
161                                 bb_show_usage();
162                         if (strcmp(*args, "255.255.255.255") != 0) {
163                                 host = *args;
164                                 if (ap->input(host, &sa) < 0) {
165                                         bb_herror_msg_and_die("%s", host);
166                                 }
167                                 memcpy(&req.arp_netmask, &sa, sizeof(struct sockaddr));
168                                 req.arp_flags |= ATF_NETMASK;
169                         }
170                         args++;
171                         break;
172                 default:
173                         bb_show_usage();
174                         break;
175                 }
176         }
177         if (flags == 0)
178                 flags = 3;
179
180         strncpy(req.arp_dev, device, sizeof(req.arp_dev));
181
182         err = -1;
183
184         /* Call the kernel. */
185         if (flags & 2) {
186                 if (option_mask32 & ARP_OPT_v)
187                         bb_error_msg("SIOCDARP(nopub)");
188                 err = ioctl(sockfd, SIOCDARP, &req);
189                 if (err < 0) {
190                         if (errno == ENXIO) {
191                                 if (flags & 1)
192                                         goto nopub;
193                                 printf("No ARP entry for %s\n", host);
194                                 return -1;
195                         }
196                         bb_perror_msg_and_die("SIOCDARP(priv)");
197                 }
198         }
199         if ((flags & 1) && err) {
200  nopub:
201                 req.arp_flags |= ATF_PUBL;
202                 if (option_mask32 & ARP_OPT_v)
203                         bb_error_msg("SIOCDARP(pub)");
204                 if (ioctl(sockfd, SIOCDARP, &req) < 0) {
205                         if (errno == ENXIO) {
206                                 printf("No ARP entry for %s\n", host);
207                                 return -1;
208                         }
209                         bb_perror_msg_and_die("SIOCDARP(pub)");
210                 }
211         }
212         return 0;
213 }
214
215 /* Get the hardware address to a specified interface name */
216 static void arp_getdevhw(char *ifname, struct sockaddr *sa,
217                                                  const struct hwtype *hwt)
218 {
219         struct ifreq ifr;
220         const struct hwtype *xhw;
221
222         strcpy(ifr.ifr_name, ifname);
223         ioctl_or_perror_and_die(sockfd, SIOCGIFHWADDR, &ifr,
224                                         "cant get HW-Address for '%s'", ifname);
225         if (hwt && (ifr.ifr_hwaddr.sa_family != hw->type)) {
226                 bb_error_msg_and_die("protocol type mismatch");
227         }
228         memcpy(sa, &(ifr.ifr_hwaddr), sizeof(struct sockaddr));
229
230         if (option_mask32 & ARP_OPT_v) {
231                 xhw = get_hwntype(ifr.ifr_hwaddr.sa_family);
232                 if (!xhw || !xhw->print) {
233                         xhw = get_hwntype(-1);
234                 }
235                 bb_error_msg("device '%s' has HW address %s '%s'",
236                                          ifname, xhw->name,
237                                          xhw->print((unsigned char *) &ifr.ifr_hwaddr.sa_data));
238         }
239 }
240
241 /* Set an entry in the ARP cache. */
242 /* Called only from main, once */
243 static int arp_set(char **args)
244 {
245         char *host;
246         struct arpreq req;
247         struct sockaddr sa;
248         int flags;
249
250         memset(&req, 0, sizeof(req));
251
252         host = *args++;
253         if (ap->input(host, &sa) < 0) {
254                 bb_herror_msg_and_die("%s", host);
255         }
256         /* If a host has more than one address, use the correct one! */
257         memcpy(&req.arp_pa, &sa, sizeof(struct sockaddr));
258
259         /* Fetch the hardware address. */
260         if (*args == NULL) {
261                 bb_error_msg_and_die("need hardware address");
262         }
263         if (option_mask32 & ARP_OPT_D) {
264                 arp_getdevhw(*args++, &req.arp_ha, hw_set ? hw : NULL);
265         } else {
266                 if (hw->input(*args++, &req.arp_ha) < 0) {
267                         bb_error_msg_and_die("invalid hardware address");
268                 }
269         }
270
271         /* Check out any modifiers. */
272         flags = ATF_PERM | ATF_COM;
273         while (*args != NULL) {
274                 switch (index_in_strings(options, *args)) {
275                 case 0: /* "pub" */
276                         flags |= ATF_PUBL;
277                         args++;
278                         break;
279                 case 1: /* "priv" */
280                         flags &= ~ATF_PUBL;
281                         args++;
282                         break;
283                 case 2: /* "temp" */
284                         flags &= ~ATF_PERM;
285                         args++;
286                         break;
287                 case 3: /* "trail" */
288                         flags |= ATF_USETRAILERS;
289                         args++;
290                         break;
291                 case 4: /* "dontpub" */
292 #ifdef HAVE_ATF_DONTPUB
293                         flags |= ATF_DONTPUB;
294 #else
295                         bb_error_msg("feature ATF_DONTPUB is not supported");
296 #endif
297                         args++;
298                         break;
299                 case 5: /* "auto" */
300 #ifdef HAVE_ATF_MAGIC
301                         flags |= ATF_MAGIC;
302 #else
303                         bb_error_msg("feature ATF_MAGIC is not supported");
304 #endif
305                         args++;
306                         break;
307                 case 6: /* "dev" */
308                         if (*++args == NULL)
309                                 bb_show_usage();
310                         device = *args;
311                         args++;
312                         break;
313                 case 7: /* "netmask" */
314                         if (*++args == NULL)
315                                 bb_show_usage();
316                         if (strcmp(*args, "255.255.255.255") != 0) {
317                                 host = *args;
318                                 if (ap->input(host, &sa) < 0) {
319                                         bb_herror_msg_and_die("%s", host);
320                                 }
321                                 memcpy(&req.arp_netmask, &sa, sizeof(struct sockaddr));
322                                 flags |= ATF_NETMASK;
323                         }
324                         args++;
325                         break;
326                 default:
327                         bb_show_usage();
328                         break;
329                 }
330         }
331
332         /* Fill in the remainder of the request. */
333         req.arp_flags = flags;
334
335         strncpy(req.arp_dev, device, sizeof(req.arp_dev));
336
337         /* Call the kernel. */
338         if (option_mask32 & ARP_OPT_v)
339                 bb_error_msg("SIOCSARP()");
340         xioctl(sockfd, SIOCSARP, &req);
341         return 0;
342 }
343
344
345 /* Print the contents of an ARP request block. */
346 static void
347 arp_disp(const char *name, char *ip, int type, int arp_flags,
348                  char *hwa, char *mask, char *dev)
349 {
350         static const int arp_masks[] = {
351                 ATF_PERM, ATF_PUBL,
352 #ifdef HAVE_ATF_MAGIC
353                 ATF_MAGIC,
354 #endif
355 #ifdef HAVE_ATF_DONTPUB
356                 ATF_DONTPUB,
357 #endif
358                 ATF_USETRAILERS,
359         };
360         static const char arp_labels[] ALIGN1 = "PERM\0""PUP\0"
361 #ifdef HAVE_ATF_MAGIC
362                 "AUTO\0"
363 #endif
364 #ifdef HAVE_ATF_DONTPUB
365                 "DONTPUB\0"
366 #endif
367                 "TRAIL\0"
368         ;
369
370         const struct hwtype *xhw;
371
372         xhw = get_hwntype(type);
373         if (xhw == NULL)
374                 xhw = get_hwtype(DFLT_HW);
375
376         printf("%s (%s) at ", name, ip);
377
378         if (!(arp_flags & ATF_COM)) {
379                 if (arp_flags & ATF_PUBL)
380                         printf("* ");
381                 else
382                         printf("<incomplete> ");
383         } else {
384                 printf("%s [%s] ", hwa, xhw->name);
385         }
386
387         if (arp_flags & ATF_NETMASK)
388                 printf("netmask %s ", mask);
389
390         print_flags_separated(arp_masks, arp_labels, arp_flags, " ");
391         printf(" on %s\n", dev);
392 }
393
394 /* Display the contents of the ARP cache in the kernel. */
395 /* Called only from main, once */
396 static int arp_show(char *name)
397 {
398         const char *host;
399         const char *hostname;
400         FILE *fp;
401         struct sockaddr sa;
402         int type, flags;
403         int num;
404         unsigned entries = 0, shown = 0;
405         char ip[128];
406         char hwa[128];
407         char mask[128];
408         char line[128];
409         char dev[128];
410
411         host = NULL;
412         if (name != NULL) {
413                 /* Resolve the host name. */
414                 if (ap->input(name, &sa) < 0) {
415                         bb_herror_msg_and_die("%s", name);
416                 }
417                 host = xstrdup(ap->sprint(&sa, 1));
418         }
419         fp = xfopen_for_read("/proc/net/arp");
420         /* Bypass header -- read one line */
421         fgets(line, sizeof(line), fp);
422
423         /* Read the ARP cache entries. */
424         while (fgets(line, sizeof(line), fp)) {
425
426                 mask[0] = '-'; mask[1] = '\0';
427                 dev[0] = '-'; dev[1] = '\0';
428                 /* All these strings can't overflow
429                  * because fgets above reads limited amount of data */
430                 num = sscanf(line, "%s 0x%x 0x%x %s %s %s\n",
431                                          ip, &type, &flags, hwa, mask, dev);
432                 if (num < 4)
433                         break;
434
435                 entries++;
436                 /* if the user specified hw-type differs, skip it */
437                 if (hw_set && (type != hw->type))
438                         continue;
439
440                 /* if the user specified address differs, skip it */
441                 if (host && strcmp(ip, host) != 0)
442                         continue;
443
444                 /* if the user specified device differs, skip it */
445                 if (device[0] && strcmp(dev, device) != 0)
446                         continue;
447
448                 shown++;
449                 /* This IS ugly but it works -be */
450                 hostname = "?";
451                 if (!(option_mask32 & ARP_OPT_n)) {
452                         if (ap->input(ip, &sa) < 0)
453                                 hostname = ip;
454                         else
455                                 hostname = ap->sprint(&sa, (option_mask32 & ARP_OPT_n) | 0x8000);
456                         if (strcmp(hostname, ip) == 0)
457                                 hostname = "?";
458                 }
459
460                 arp_disp(hostname, ip, type, flags, hwa, mask, dev);
461         }
462         if (option_mask32 & ARP_OPT_v)
463                 printf("Entries: %d\tSkipped: %d\tFound: %d\n",
464                            entries, entries - shown, shown);
465
466         if (!shown) {
467                 if (hw_set || host || device[0])
468                         printf("No match found in %d entries\n", entries);
469         }
470         if (ENABLE_FEATURE_CLEAN_UP) {
471                 free((char*)host);
472                 fclose(fp);
473         }
474         return 0;
475 }
476
477 int arp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
478 int arp_main(int argc UNUSED_PARAM, char **argv)
479 {
480         const char *hw_type = "ether";
481         const char *protocol;
482         unsigned opts;
483
484         INIT_G();
485
486         xmove_fd(xsocket(AF_INET, SOCK_DGRAM, 0), sockfd);
487         ap = get_aftype(DFLT_AF);
488         if (!ap)
489                 bb_error_msg_and_die("%s: %s not supported", DFLT_AF, "address family");
490
491         opts = getopt32(argv, "A:p:H:t:i:adnDsv", &protocol, &protocol,
492                                  &hw_type, &hw_type, &device);
493         argv += optind;
494         if (opts & (ARP_OPT_A | ARP_OPT_p)) {
495                 ap = get_aftype(protocol);
496                 if (ap == NULL)
497                         bb_error_msg_and_die("%s: unknown %s", protocol, "address family");
498         }
499         if (opts & (ARP_OPT_A | ARP_OPT_p)) {
500                 hw = get_hwtype(hw_type);
501                 if (hw == NULL)
502                         bb_error_msg_and_die("%s: unknown %s", hw_type, "hardware type");
503                 hw_set = 1;
504         }
505         //if (opts & ARP_OPT_i)... -i
506
507         if (ap->af != AF_INET) {
508                 bb_error_msg_and_die("%s: kernel only supports 'inet'", ap->name);
509         }
510
511         /* If no hw type specified get default */
512         if (!hw) {
513                 hw = get_hwtype(DFLT_HW);
514                 if (!hw)
515                         bb_error_msg_and_die("%s: %s not supported", DFLT_HW, "hardware type");
516         }
517
518         if (hw->alen <= 0) {
519                 bb_error_msg_and_die("%s: %s without ARP support",
520                                                          hw->name, "hardware type");
521         }
522
523         /* Now see what we have to do here... */
524         if (opts & (ARP_OPT_d | ARP_OPT_s)) {
525                 if (argv[0] == NULL)
526                         bb_error_msg_and_die("need host name");
527                 if (opts & ARP_OPT_s)
528                         return arp_set(argv);
529                 return arp_del(argv);
530         }
531         //if (opts & ARP_OPT_a) - default
532         return arp_show(argv[0]);
533 }