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