move remaining help text from include/usage.src.h
[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:     "\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
35 #include "libbb.h"
36 #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
49 enum {
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 };
62
63 enum {
64         sockfd = 3, /* active socket descriptor */
65 };
66
67 struct 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
73 } FIX_ALIASING;
74 #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
83
84 static const char options[] ALIGN1 =
85         "pub\0"
86         "priv\0"
87         "temp\0"
88         "trail\0"
89         "dontpub\0"
90         "auto\0"
91         "dev\0"
92         "netmask\0";
93
94 /* Delete an entry from the ARP cache. */
95 /* Called only from main, once */
96 static int arp_del(char **args)
97 {
98         char *host;
99         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. */
107         host = *args;
108         if (ap->input(host, &sa) < 0) {
109                 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) {
121                 switch (index_in_strings(options, *args)) {
122                 case 0: /* "pub" */
123                         flags |= 1;
124                         args++;
125                         break;
126                 case 1: /* "priv" */
127                         flags |= 2;
128                         args++;
129                         break;
130                 case 2: /* "temp" */
131                         req.arp_flags &= ~ATF_PERM;
132                         args++;
133                         break;
134                 case 3: /* "trail" */
135                         req.arp_flags |= ATF_USETRAILERS;
136                         args++;
137                         break;
138                 case 4: /* "dontpub" */
139 #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;
146                 case 5: /* "auto" */
147 #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;
154                 case 6: /* "dev" */
155                         if (*++args == NULL)
156                                 bb_show_usage();
157                         device = *args;
158                         args++;
159                         break;
160                 case 7: /* "netmask" */
161                         if (*++args == NULL)
162                                 bb_show_usage();
163                         if (strcmp(*args, "255.255.255.255") != 0) {
164                                 host = *args;
165                                 if (ap->input(host, &sa) < 0) {
166                                         bb_herror_msg_and_die("%s", host);
167                                 }
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 */
217 static void arp_getdevhw(char *ifname, struct sockaddr *sa,
218                                                  const struct hwtype *hwt)
219 {
220         struct ifreq ifr;
221         const struct hwtype *xhw;
222
223         strcpy(ifr.ifr_name, ifname);
224         ioctl_or_perror_and_die(sockfd, SIOCGIFHWADDR, &ifr,
225                                         "cant get HW-Address for '%s'", ifname);
226         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,
238                                          xhw->print((unsigned char *) &ifr.ifr_hwaddr.sa_data));
239         }
240 }
241
242 /* Set an entry in the ARP cache. */
243 /* Called only from main, once */
244 static int arp_set(char **args)
245 {
246         char *host;
247         struct arpreq req;
248         struct sockaddr sa;
249         int flags;
250
251         memset(&req, 0, sizeof(req));
252
253         host = *args++;
254         if (ap->input(host, &sa) < 0) {
255                 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) {
275                 switch (index_in_strings(options, *args)) {
276                 case 0: /* "pub" */
277                         flags |= ATF_PUBL;
278                         args++;
279                         break;
280                 case 1: /* "priv" */
281                         flags &= ~ATF_PUBL;
282                         args++;
283                         break;
284                 case 2: /* "temp" */
285                         flags &= ~ATF_PERM;
286                         args++;
287                         break;
288                 case 3: /* "trail" */
289                         flags |= ATF_USETRAILERS;
290                         args++;
291                         break;
292                 case 4: /* "dontpub" */
293 #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;
300                 case 5: /* "auto" */
301 #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;
308                 case 6: /* "dev" */
309                         if (*++args == NULL)
310                                 bb_show_usage();
311                         device = *args;
312                         args++;
313                         break;
314                 case 7: /* "netmask" */
315                         if (*++args == NULL)
316                                 bb_show_usage();
317                         if (strcmp(*args, "255.255.255.255") != 0) {
318                                 host = *args;
319                                 if (ap->input(host, &sa) < 0) {
320                                         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()");
341         xioctl(sockfd, SIOCSARP, &req);
342         return 0;
343 }
344
345
346 /* Print the contents of an ARP request block. */
347 static void
348 arp_disp(const char *name, char *ip, int type, int arp_flags,
349                  char *hwa, char *mask, char *dev)
350 {
351         static const int arp_masks[] = {
352                 ATF_PERM, ATF_PUBL,
353 #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
371         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
391         print_flags_separated(arp_masks, arp_labels, arp_flags, " ");
392         printf(" on %s\n", dev);
393 }
394
395 /* Display the contents of the ARP cache in the kernel. */
396 /* Called only from main, once */
397 static int arp_show(char *name)
398 {
399         const char *host;
400         const char *hostname;
401         FILE *fp;
402         struct sockaddr sa;
403         int type, flags;
404         int num;
405         unsigned entries = 0, shown = 0;
406         char ip[128];
407         char hwa[128];
408         char mask[128];
409         char line[128];
410         char dev[128];
411
412         host = NULL;
413         if (name != NULL) {
414                 /* Resolve the host name. */
415                 if (ap->input(name, &sa) < 0) {
416                         bb_herror_msg_and_die("%s", name);
417                 }
418                 host = xstrdup(ap->sprint(&sa, 1));
419         }
420         fp = xfopen_for_read("/proc/net/arp");
421         /* 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
427                 mask[0] = '-'; mask[1] = '\0';
428                 dev[0] = '-'; dev[1] = '\0';
429                 /* 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;
435
436                 entries++;
437                 /* if the user specified hw-type differs, skip it */
438                 if (hw_set && (type != hw->type))
439                         continue;
440
441                 /* if the user specified address differs, skip it */
442                 if (host && strcmp(ip, host) != 0)
443                         continue;
444
445                 /* if the user specified device differs, skip it */
446                 if (device[0] && strcmp(dev, device) != 0)
447                         continue;
448
449                 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)
458                                 hostname = "?";
459                 }
460
461                 arp_disp(hostname, ip, type, flags, hwa, mask, dev);
462         }
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) {
468                 if (hw_set || host || device[0])
469                         printf("No match found in %d entries\n", entries);
470         }
471         if (ENABLE_FEATURE_CLEAN_UP) {
472                 free((char*)host);
473                 fclose(fp);
474         }
475         return 0;
476 }
477
478 int arp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
479 int arp_main(int argc UNUSED_PARAM, char **argv)
480 {
481         const char *hw_type = "ether";
482         const char *protocol;
483         unsigned opts;
484
485         INIT_G();
486
487         xmove_fd(xsocket(AF_INET, SOCK_DGRAM, 0), sockfd);
488         ap = get_aftype(DFLT_AF);
489         if (!ap)
490                 bb_error_msg_and_die("%s: %s not supported", DFLT_AF, "address family");
491
492         opts = getopt32(argv, "A:p:H:t:i:adnDsv", &protocol, &protocol,
493                                  &hw_type, &hw_type, &device);
494         argv += optind;
495         if (opts & (ARP_OPT_A | ARP_OPT_p)) {
496                 ap = get_aftype(protocol);
497                 if (ap == NULL)
498                         bb_error_msg_and_die("%s: unknown %s", protocol, "address family");
499         }
500         if (opts & (ARP_OPT_A | ARP_OPT_p)) {
501                 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         }
506         //if (opts & ARP_OPT_i)... -i
507
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         }
523
524         /* Now see what we have to do here... */
525         if (opts & (ARP_OPT_d | ARP_OPT_s)) {
526                 if (argv[0] == NULL)
527                         bb_error_msg_and_die("need host name");
528                 if (opts & ARP_OPT_s)
529                         return arp_set(argv);
530                 return arp_del(argv);
531         }
532         //if (opts & ARP_OPT_a) - default
533         return arp_show(argv[0]);
534 }