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