Remove code for protocols we don't properly support. (Most of this could
[oweals/busybox.git] / networking / arping.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * arping.c - Ping hosts by ARP requests/replies
4  *
5  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
6  *
7  * Author:      Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
8  * Busybox port: Nick Fedchik <nick@fedchik.org.ua>
9  */
10
11 #include <sys/ioctl.h>
12 #include <signal.h>
13 #include <sys/time.h>
14
15 #include <errno.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <unistd.h>
19
20 #include <arpa/inet.h>
21 #include <net/if.h>
22 #include <netinet/ether.h>
23 #include <netpacket/packet.h>
24
25 #include "busybox.h"
26
27 static struct in_addr src;
28 static struct in_addr dst;
29 static struct sockaddr_ll me;
30 static struct sockaddr_ll he;
31 static struct timeval last;
32
33 enum cfg_e {
34         dad = 1,
35         unsolicited = 2,
36         advert = 4,
37         quiet = 8,
38         quit_on_reply = 16,
39         broadcast_only = 32,
40         unicasting = 64
41 };
42 static int cfg;
43
44 static int s;
45 static int count = -1;
46 static int timeout;
47 static int sent;
48 static int brd_sent;
49 static int received;
50 static int brd_recv;
51 static int req_recv;
52
53
54 #define MS_TDIFF(tv1,tv2) ( ((tv1).tv_sec-(tv2).tv_sec)*1000 + \
55                            ((tv1).tv_usec-(tv2).tv_usec)/1000 )
56 #if 0
57 static void set_signal(int signo, void (*handler) (void))
58 {
59         struct sigaction sa;
60
61         memset(&sa, 0, sizeof(sa));
62         sa.sa_handler = (void (*)(int)) handler;
63         sa.sa_flags = SA_RESTART;
64         sigaction(signo, &sa, NULL);
65 }
66 #endif
67
68 static int send_pack(int sock, struct in_addr *src_addr,
69                                          struct in_addr *dst_addr, struct sockaddr_ll *ME,
70                                          struct sockaddr_ll *HE)
71 {
72         int err;
73         struct timeval now;
74         RESERVE_CONFIG_UBUFFER(buf, 256);
75         struct arphdr *ah = (struct arphdr *) buf;
76         unsigned char *p = (unsigned char *) (ah + 1);
77
78         ah->ar_hrd = htons(ME->sll_hatype);
79         ah->ar_hrd = htons(ARPHRD_ETHER);
80         ah->ar_pro = htons(ETH_P_IP);
81         ah->ar_hln = ME->sll_halen;
82         ah->ar_pln = 4;
83         ah->ar_op = cfg&advert ? htons(ARPOP_REPLY) : htons(ARPOP_REQUEST);
84
85         memcpy(p, &ME->sll_addr, ah->ar_hln);
86         p += ME->sll_halen;
87
88         memcpy(p, src_addr, 4);
89         p += 4;
90
91         if (cfg&advert)
92                 memcpy(p, &ME->sll_addr, ah->ar_hln);
93         else
94                 memcpy(p, &HE->sll_addr, ah->ar_hln);
95         p += ah->ar_hln;
96
97         memcpy(p, dst_addr, 4);
98         p += 4;
99
100         gettimeofday(&now, NULL);
101         err = sendto(sock, buf, p - buf, 0, (struct sockaddr *) HE, sizeof(*HE));
102         if (err == p - buf) {
103                 last = now;
104                 sent++;
105                 if (!(cfg&unicasting))
106                         brd_sent++;
107         }
108         RELEASE_CONFIG_BUFFER(buf);
109         return err;
110 }
111
112 static void finish(void)
113 {
114         if (!(cfg&quiet)) {
115                 printf("Sent %d probes (%d broadcast(s))\n"
116                         "Received %d repl%s",
117                         sent, brd_sent,
118                         received, (received > 1) ? "ies" : "y");
119                 if (brd_recv || req_recv) {
120                         printf(" (");
121                         if (req_recv)
122                                 printf("%d request(s)", req_recv);
123                         if (brd_recv)
124                                 printf("%s%d broadcast(s)", req_recv ? ", " : "", brd_recv);
125                         putchar(')');
126                 }
127                 putchar('\n');
128                 fflush(stdout);
129         }
130         if (cfg&dad)
131                 exit(!!received);
132         if (cfg&unsolicited)
133                 exit(0);
134         exit(!received);
135 }
136
137 static void catcher(void)
138 {
139         struct timeval tv;
140         static struct timeval start;
141
142         gettimeofday(&tv, NULL);
143
144         if (start.tv_sec == 0)
145                 start = tv;
146
147         if (count-- == 0
148                 || (timeout && MS_TDIFF(tv, start) > timeout * 1000 + 500))
149                 finish();
150
151         if (last.tv_sec == 0 || MS_TDIFF(tv, last) > 500) {
152                 send_pack(s, &src, &dst, &me, &he);
153                 if (count == 0 && cfg&unsolicited)
154                         finish();
155         }
156         alarm(1);
157 }
158
159 static int recv_pack(unsigned char *buf, int len, struct sockaddr_ll *FROM)
160 {
161         struct arphdr *ah = (struct arphdr *) buf;
162         unsigned char *p = (unsigned char *) (ah + 1);
163         struct in_addr src_ip, dst_ip;
164
165         /* Filter out wild packets */
166         if (FROM->sll_pkttype != PACKET_HOST &&
167                 FROM->sll_pkttype != PACKET_BROADCAST &&
168                 FROM->sll_pkttype != PACKET_MULTICAST)
169                 return 0;
170
171         /* Only these types are recognised */
172         if (ah->ar_op != htons(ARPOP_REQUEST) && ah->ar_op != htons(ARPOP_REPLY))
173                 return 0;
174
175         /* ARPHRD check and this darned FDDI hack here :-( */
176         if (ah->ar_hrd != htons(FROM->sll_hatype) &&
177                 (FROM->sll_hatype != ARPHRD_FDDI
178                  || ah->ar_hrd != htons(ARPHRD_ETHER)))
179                 return 0;
180
181         /* Protocol must be IP. */
182         if (ah->ar_pro != htons(ETH_P_IP))
183                 return 0;
184         if (ah->ar_pln != 4)
185                 return 0;
186         if (ah->ar_hln != me.sll_halen)
187                 return 0;
188         if (len < sizeof(*ah) + 2 * (4 + ah->ar_hln))
189                 return 0;
190         memcpy(&src_ip, p + ah->ar_hln, 4);
191         memcpy(&dst_ip, p + ah->ar_hln + 4 + ah->ar_hln, 4);
192         if (!(cfg&dad)) {
193                 if (src_ip.s_addr != dst.s_addr)
194                         return 0;
195                 if (src.s_addr != dst_ip.s_addr)
196                         return 0;
197                 if (memcmp(p + ah->ar_hln + 4, &me.sll_addr, ah->ar_hln))
198                         return 0;
199         } else {
200                 /* DAD packet was:
201                    src_ip = 0 (or some src)
202                    src_hw = ME
203                    dst_ip = tested address
204                    dst_hw = <unspec>
205
206                    We fail, if receive request/reply with:
207                    src_ip = tested_address
208                    src_hw != ME
209                    if src_ip in request was not zero, check
210                    also that it matches to dst_ip, otherwise
211                    dst_ip/dst_hw do not matter.
212                  */
213                 if (src_ip.s_addr != dst.s_addr)
214                         return 0;
215                 if (memcmp(p, &me.sll_addr, me.sll_halen) == 0)
216                         return 0;
217                 if (src.s_addr && src.s_addr != dst_ip.s_addr)
218                         return 0;
219         }
220         if (!(cfg&quiet)) {
221                 int s_printed = 0;
222                 struct timeval tv;
223
224                 gettimeofday(&tv, NULL);
225
226                 printf("%s %s from %s [%s]",
227                         FROM->sll_pkttype == PACKET_HOST ? "Unicast" : "Broadcast",
228                         ah->ar_op == htons(ARPOP_REPLY) ? "reply" : "request",
229                         inet_ntoa(src_ip),
230                         ether_ntoa((struct ether_addr *) p));
231                 if (dst_ip.s_addr != src.s_addr) {
232                         printf("for %s ", inet_ntoa(dst_ip));
233                         s_printed = 1;
234                 }
235                 if (memcmp(p + ah->ar_hln + 4, me.sll_addr, ah->ar_hln)) {
236                         if (!s_printed)
237                                 printf("for ");
238                         printf("[%s]",
239                                    ether_ntoa((struct ether_addr *) p + ah->ar_hln + 4));
240                 }
241
242                 if (last.tv_sec) {
243                         long usecs = (tv.tv_sec - last.tv_sec) * 1000000 +
244                                 tv.tv_usec - last.tv_usec;
245                         long msecs = (usecs + 500) / 1000;
246
247                         usecs -= msecs * 1000 - 500;
248                         printf(" %ld.%03ldms\n", msecs, usecs);
249                 } else {
250                         printf(" UNSOLICITED?\n");
251                 }
252                 fflush(stdout);
253         }
254         received++;
255         if (FROM->sll_pkttype != PACKET_HOST)
256                 brd_recv++;
257         if (ah->ar_op == htons(ARPOP_REQUEST))
258                 req_recv++;
259         if (cfg&quit_on_reply)
260                 finish();
261         if (!(cfg&broadcast_only)) {
262                 memcpy(he.sll_addr, p, me.sll_halen);
263                 cfg |= unicasting;
264         }
265         return 1;
266 }
267
268 int arping_main(int argc, char **argv)
269 {
270         char *device = "eth0";
271         int ifindex;
272         char *source = NULL;
273         char *target;
274
275         s = socket(PF_PACKET, SOCK_DGRAM, 0);
276         ifindex = errno;
277
278         setuid(getuid());
279
280         {
281                 unsigned long opt;
282                 char *_count, *_timeout, *_device;
283
284                 /* Dad also sets quit_on_reply.
285                  * Advert also sets unsolicited.
286                  */
287                 bb_opt_complementally = "Df:AU";
288                 opt = bb_getopt_ulflags(argc, argv, "DUAqfbc:w:i:s:",
289                                                 &_count, &_timeout, &_device);
290                 cfg |= opt & 63; /* set respective flags */
291                 if (opt & 64) /* count */
292                         count = atoi(_count);
293                 if (opt & 128) /* timeout */
294                         timeout = atoi(_timeout);
295                 if (opt & 256) { /* interface */
296                         if (bb_strlen(_device) > IF_NAMESIZE) {
297                                 bb_error_msg_and_die("Interface name `%s' must be less than %d",
298                                                                 _device, IF_NAMESIZE);
299                         }
300                         device = _device;
301                 }
302                 if (opt & 512) /* source */
303                         source = optarg;
304         }
305         argc -= optind;
306         argv += optind;
307
308         if (argc != 1)
309                 bb_show_usage();
310
311         target = *argv;
312
313
314         if (s < 0) {
315                 bb_default_error_retval = ifindex;
316                 bb_perror_msg_and_die("socket");
317         }
318         bb_default_error_retval = 2;
319
320         {
321                 struct ifreq ifr;
322
323                 memset(&ifr, 0, sizeof(ifr));
324                 strncpy(ifr.ifr_name, device, IFNAMSIZ - 1);
325                 if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
326                         bb_error_msg_and_die("Interface %s not found", device);
327                 }
328                 ifindex = ifr.ifr_ifindex;
329
330                 if (ioctl(s, SIOCGIFFLAGS, (char *) &ifr)) {
331                         bb_error_msg_and_die("SIOCGIFFLAGS");
332                 }
333                 if (!(ifr.ifr_flags & IFF_UP)) {
334                         bb_error_msg_and_die("Interface %s is down", device);
335                 }
336                 if (ifr.ifr_flags & (IFF_NOARP | IFF_LOOPBACK)) {
337                         bb_error_msg("Interface %s is not ARPable", device);
338                         exit(cfg&dad ? 0 : 2);
339                 }
340         }
341
342         if (!inet_aton(target, &dst)) {
343                 struct hostent *hp;
344
345                 hp = gethostbyname2(target, AF_INET);
346                 if (!hp) {
347                         bb_error_msg_and_die("invalid or unknown target %s", target);
348                 }
349                 memcpy(&dst, hp->h_addr, 4);
350         }
351
352         if (source && !inet_aton(source, &src)) {
353                 bb_error_msg_and_die("invalid source address %s", source);
354         }
355
356         if (!(cfg&dad) && cfg&unsolicited && src.s_addr == 0)
357                 src = dst;
358
359         if (!(cfg&dad) || src.s_addr) {
360                 struct sockaddr_in saddr;
361                 int probe_fd = socket(AF_INET, SOCK_DGRAM, 0); /* maybe use bb_xsocket? */
362
363                 if (probe_fd < 0) {
364                         bb_error_msg_and_die("socket");
365                 }
366                 if (device) {
367                         if (setsockopt
368                                 (probe_fd, SOL_SOCKET, SO_BINDTODEVICE, device,
369                                  strlen(device) + 1) == -1)
370                                 bb_error_msg("WARNING: interface %s is ignored", device);
371                 }
372                 memset(&saddr, 0, sizeof(saddr));
373                 saddr.sin_family = AF_INET;
374                 if (src.s_addr) {
375                         saddr.sin_addr = src;
376                         if (bind(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr)) == -1) {
377                                 bb_error_msg_and_die("bind");
378                         }
379                 } else if (!(cfg&dad)) {
380                         int on = 1;
381                         socklen_t alen = sizeof(saddr);
382
383                         saddr.sin_port = htons(1025);
384                         saddr.sin_addr = dst;
385
386                         if (setsockopt
387                                 (probe_fd, SOL_SOCKET, SO_DONTROUTE, (char *) &on,
388                                  sizeof(on)) == -1)
389                                 bb_perror_msg("WARNING: setsockopt(SO_DONTROUTE)");
390                         if (connect(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr))
391                                 == -1) {
392                                 bb_error_msg_and_die("connect");
393                         }
394                         if (getsockname(probe_fd, (struct sockaddr *) &saddr, &alen) ==
395                                 -1) {
396                                 bb_error_msg_and_die("getsockname");
397                         }
398                         src = saddr.sin_addr;
399                 }
400                 close(probe_fd);
401         };
402
403         me.sll_family = AF_PACKET;
404         me.sll_ifindex = ifindex;
405         me.sll_protocol = htons(ETH_P_ARP);
406         if (bind(s, (struct sockaddr *) &me, sizeof(me)) == -1) {
407                 bb_error_msg_and_die("bind");
408         }
409
410         {
411                 socklen_t alen = sizeof(me);
412
413                 if (getsockname(s, (struct sockaddr *) &me, &alen) == -1) {
414                         bb_error_msg_and_die("getsockname");
415                 }
416         }
417         if (me.sll_halen == 0) {
418                 bb_error_msg("Interface \"%s\" is not ARPable (no ll address)", device);
419                 exit(cfg&dad ? 0 : 2);
420         }
421         he = me;
422         memset(he.sll_addr, -1, he.sll_halen);
423
424         if (!(cfg&quiet)) {
425                 printf("ARPING to %s from %s via %s\n",
426                         inet_ntoa(dst), inet_ntoa(src),
427                         device ? device : "unknown");
428         }
429
430         if (!src.s_addr && !(cfg&dad)) {
431                 bb_error_msg_and_die("no src address in the non-DAD mode");
432         }
433
434         {
435                 struct sigaction sa;
436
437                 memset(&sa, 0, sizeof(sa));
438                 sa.sa_flags = SA_RESTART;
439
440                 sa.sa_handler = (void (*)(int)) finish;
441                 sigaction(SIGINT, &sa, NULL);
442
443                 sa.sa_handler = (void (*)(int)) catcher;
444                 sigaction(SIGALRM, &sa, NULL);
445         }
446
447         catcher();
448
449         while (1) {
450                 sigset_t sset, osset;
451                 RESERVE_CONFIG_UBUFFER(packet, 4096);
452                 struct sockaddr_ll from;
453                 socklen_t alen = sizeof(from);
454                 int cc;
455
456                 if ((cc = recvfrom(s, packet, 4096, 0,
457                                                    (struct sockaddr *) &from, &alen)) < 0) {
458                         perror("recvfrom");
459                         continue;
460                 }
461                 sigemptyset(&sset);
462                 sigaddset(&sset, SIGALRM);
463                 sigaddset(&sset, SIGINT);
464                 sigprocmask(SIG_BLOCK, &sset, &osset);
465                 recv_pack(packet, cc, &from);
466                 sigprocmask(SIG_SETMASK, &osset, NULL);
467                 RELEASE_CONFIG_BUFFER(packet);
468         }
469 }