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