OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / user / iputils / arping.c
1 /*
2  * arping.c
3  *
4  *              This program is free software; you can redistribute it and/or
5  *              modify it under the terms of the GNU General Public License
6  *              as published by the Free Software Foundation; either version
7  *              2 of the License, or (at your option) any later version.
8  *
9  * Authors:     Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10  */
11
12 #include <stdlib.h>
13 #include <sys/param.h>
14 #include <sys/socket.h>
15 #include <linux/sockios.h>
16 #include <sys/file.h>
17 #include <sys/time.h>
18 #include <sys/signal.h>
19 #include <sys/ioctl.h>
20 #include <linux/if.h>
21 #include <linux/if_packet.h>
22 #include <linux/if_ether.h>
23 #include <net/if_arp.h>
24 #include <sys/uio.h>
25
26 #include <netdb.h>
27 #include <unistd.h>
28 #include <stdio.h>
29 #include <ctype.h>
30 #include <errno.h>
31 #include <string.h>
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
34
35 #include "SNAPSHOT.h"
36
37 static void usage(void) __attribute__((noreturn));
38
39 int quit_on_reply=0;
40 char *device="eth0";
41 int ifindex;
42 char *source;
43 struct in_addr src, dst;
44 char *target;
45 int dad, unsolicited, advert;
46 int quiet;
47 int count=-1;
48 int timeout;
49 int unicasting;
50 int s;
51 int broadcast_only;
52
53 struct sockaddr_ll me;
54 struct sockaddr_ll he;
55
56 struct timeval start, last;
57
58 int sent, brd_sent;
59 int received, brd_recv, req_recv;
60
61 #define MS_TDIFF(tv1,tv2) ( ((tv1).tv_sec-(tv2).tv_sec)*1000 + \
62                            ((tv1).tv_usec-(tv2).tv_usec)/1000 )
63
64 void usage(void)
65 {
66         fprintf(stderr,
67                 "Usage: arping [-fqbDUAV] [-c count] [-w timeout] [-I device] [-s source] destination\n"
68                 "  -f : quit on first reply\n"
69                 "  -q : be quiet\n"
70                 "  -b : keep broadcasting, don't go unicast\n"
71                 "  -D : duplicate address detection mode\n"
72                 "  -U : Unsolicited ARP mode, update your neighbours\n"
73                 "  -A : ARP answer mode, update your neighbours\n"
74                 "  -V : print version and exit\n"
75                 "  -c count : how many packets to send\n"
76                 "  -w timeout : how long to wait for a reply\n"
77                 "  -I device : which ethernet device to use (eth0)\n"
78                 "  -s source : source ip address\n"
79                 "  destination : ask for what ip address\n"
80                 );
81         exit(2);
82 }
83
84 void set_signal(int signo, void (*handler)(void))
85 {
86         struct sigaction sa;
87
88         memset(&sa, 0, sizeof(sa));
89         sa.sa_handler = (void (*)(int))handler;
90         sa.sa_flags = SA_RESTART;
91         sigaction(signo, &sa, NULL);
92 }
93
94 int send_pack(int s, struct in_addr src, struct in_addr dst,
95               struct sockaddr_ll *ME, struct sockaddr_ll *HE)
96 {
97         int err;
98         struct timeval now;
99         unsigned char buf[256];
100         struct arphdr *ah = (struct arphdr*)buf;
101         unsigned char *p = (unsigned char *)(ah+1);
102
103         ah->ar_hrd = htons(ME->sll_hatype);
104         if (ah->ar_hrd == htons(ARPHRD_FDDI))
105                 ah->ar_hrd = htons(ARPHRD_ETHER);
106         ah->ar_pro = htons(ETH_P_IP);
107         ah->ar_hln = ME->sll_halen;
108         ah->ar_pln = 4;
109         ah->ar_op  = advert ? htons(ARPOP_REPLY) : htons(ARPOP_REQUEST);
110
111         memcpy(p, &ME->sll_addr, ah->ar_hln);
112         p+=ME->sll_halen;
113
114         memcpy(p, &src, 4);
115         p+=4;
116
117         if (advert)
118                 memcpy(p, &ME->sll_addr, ah->ar_hln);
119         else
120                 memcpy(p, &HE->sll_addr, ah->ar_hln);
121         p+=ah->ar_hln;
122
123         memcpy(p, &dst, 4);
124         p+=4;
125
126         gettimeofday(&now, NULL);
127         err = sendto(s, buf, p-buf, 0, (struct sockaddr*)HE, sizeof(*HE));
128         if (err == p-buf) {
129                 last = now;
130                 sent++;
131                 if (!unicasting)
132                         brd_sent++;
133         }
134         return err;
135 }
136
137 void finish(void)
138 {
139         if (!quiet) {
140                 printf("Sent %d probes (%d broadcast(s))\n", sent, brd_sent);
141                 printf("Received %d response(s)", received);
142                 if (brd_recv || req_recv) {
143                         printf(" (");
144                         if (req_recv)
145                                 printf("%d request(s)", req_recv);
146                         if (brd_recv)
147                                 printf("%s%d broadcast(s)",
148                                        req_recv ? ", " : "",
149                                        brd_recv);
150                         printf(")");
151                 }
152                 printf("\n");
153                 fflush(stdout);
154         }
155         if (dad)
156                 exit(!!received);
157         if (unsolicited)
158                 exit(0);
159         exit(!received);
160 }
161
162 void catcher(void)
163 {
164         struct timeval tv;
165
166         gettimeofday(&tv, NULL);
167
168         if (start.tv_sec==0)
169                 start = tv;
170
171         if (count-- == 0 || (timeout && MS_TDIFF(tv,start) > timeout*1000 + 500))
172                 finish();
173
174         if (last.tv_sec==0 || MS_TDIFF(tv,last) > 500) {
175                 send_pack(s, src, dst, &me, &he);
176                 if (count == 0 && unsolicited)
177                         finish();
178         }
179         alarm(1);
180 }
181
182 void print_hex(unsigned char *p, int len)
183 {
184         int i;
185         for (i=0; i<len; i++) {
186                 printf("%02X", p[i]);
187                 if (i != len-1)
188                         printf(":");
189         }
190 }
191
192 int recv_pack(unsigned char *buf, int len, struct sockaddr_ll *FROM)
193 {
194         struct timeval tv;
195         struct arphdr *ah = (struct arphdr*)buf;
196         unsigned char *p = (unsigned char *)(ah+1);
197         struct in_addr src_ip, dst_ip;
198
199         gettimeofday(&tv, NULL);
200
201         /* Filter out wild packets */
202         if (FROM->sll_pkttype != PACKET_HOST &&
203             FROM->sll_pkttype != PACKET_BROADCAST &&
204             FROM->sll_pkttype != PACKET_MULTICAST)
205                 return 0;
206
207         /* Only these types are recognised */
208         if (ah->ar_op != htons(ARPOP_REQUEST) &&
209             ah->ar_op != htons(ARPOP_REPLY))
210                 return 0;
211
212         /* ARPHRD check and this darned FDDI hack here :-( */
213         if (ah->ar_hrd != htons(FROM->sll_hatype) &&
214             (FROM->sll_hatype != ARPHRD_FDDI || ah->ar_hrd != htons(ARPHRD_ETHER)))
215                 return 0;
216
217         /* Protocol must be IP. */
218         if (ah->ar_pro != htons(ETH_P_IP))
219                 return 0;
220         if (ah->ar_pln != 4)
221                 return 0;
222         if (ah->ar_hln != me.sll_halen)
223                 return 0;
224         if (len < sizeof(*ah) + 2*(4 + ah->ar_hln))
225                 return 0;
226         memcpy(&src_ip, p+ah->ar_hln, 4);
227         memcpy(&dst_ip, p+ah->ar_hln+4+ah->ar_hln, 4);
228         if (!dad) {
229                 if (src_ip.s_addr != dst.s_addr)
230                         return 0;
231                 if (src.s_addr != dst_ip.s_addr)
232                         return 0;
233                 if (memcmp(p+ah->ar_hln+4, &me.sll_addr, ah->ar_hln))
234                         return 0;
235         } else {
236                 /* DAD packet was:
237                    src_ip = 0 (or some src)
238                    src_hw = ME
239                    dst_ip = tested address
240                    dst_hw = <unspec>
241
242                    We fail, if receive request/reply with:
243                    src_ip = tested_address
244                    src_hw != ME
245                    if src_ip in request was not zero, check
246                    also that it matches to dst_ip, otherwise
247                    dst_ip/dst_hw do not matter.
248                  */
249                 if (src_ip.s_addr != dst.s_addr)
250                         return 0;
251                 if (memcmp(p, &me.sll_addr, me.sll_halen) == 0)
252                         return 0;
253                 if (src.s_addr && src.s_addr != dst_ip.s_addr)
254                         return 0;
255         }
256         if (!quiet) {
257                 int s_printed = 0;
258                 printf("%s ", FROM->sll_pkttype==PACKET_HOST ? "Unicast" : "Broadcast");
259                 printf("%s from ", ah->ar_op == htons(ARPOP_REPLY) ? "reply" : "request");
260                 printf("%s [", inet_ntoa(src_ip));
261                 print_hex(p, ah->ar_hln);
262                 printf("] ");
263                 if (dst_ip.s_addr != src.s_addr) {
264                         printf("for %s ", inet_ntoa(dst_ip));
265                         s_printed = 1;
266                 }
267                 if (memcmp(p+ah->ar_hln+4, me.sll_addr, ah->ar_hln)) {
268                         if (!s_printed)
269                                 printf("for ");
270                         printf("[");
271                         print_hex(p+ah->ar_hln+4, ah->ar_hln);
272                         printf("]");
273                 }
274                 if (last.tv_sec) {
275                         long usecs = (tv.tv_sec-last.tv_sec) * 1000000 +
276                                 tv.tv_usec-last.tv_usec;
277                         long msecs = (usecs+500)/1000;
278                         usecs -= msecs*1000 - 500;
279                         printf(" %ld.%03ldms\n", msecs, usecs);
280                 } else {
281                         printf(" UNSOLICITED?\n");
282                 }
283                 fflush(stdout);
284         }
285         received++;
286         if (FROM->sll_pkttype != PACKET_HOST)
287                 brd_recv++;
288         if (ah->ar_op == htons(ARPOP_REQUEST))
289                 req_recv++;
290         if (quit_on_reply)
291                 finish();
292         if(!broadcast_only) {
293                 memcpy(he.sll_addr, p, me.sll_halen);
294                 unicasting=1;
295         }
296         return 1;
297 }
298
299 int
300 main(int argc, char **argv)
301 {
302         int socket_errno;
303         int ch;
304         uid_t uid = getuid();
305
306         s = socket(PF_PACKET, SOCK_DGRAM, 0);
307         socket_errno = errno;
308
309         if (setuid(uid)) {
310                 perror("arping: setuid");
311                 exit(-1);
312         }
313
314         while ((ch = getopt(argc, argv, "h?bfDUAqc:w:s:I:V")) != EOF) {
315                 switch(ch) {
316                 case 'b':
317                         broadcast_only=1;
318                         break;
319                 case 'D':
320                         dad++;
321                         quit_on_reply=1;
322                         break;
323                 case 'U':
324                         unsolicited++;
325                         break;
326                 case 'A':
327                         advert++;
328                         unsolicited++;
329                         break;
330                 case 'q':
331                         quiet++;
332                         break;
333                 case 'c':
334                         count = atoi(optarg);
335                         break;
336                 case 'w':
337                         timeout = atoi(optarg);
338                         break;
339                 case 'I':
340                         device = optarg;
341                         break;
342                 case 'f':
343                         quit_on_reply=1;
344                         break;
345                 case 's':
346                         source = optarg;
347                         break;
348                 case 'V':
349                         printf("arping utility, iputils-ss%s\n", SNAPSHOT);
350                         exit(0);
351                 case 'h':
352                 case '?':
353                 default:
354                         usage();
355                 }
356         }
357         argc -= optind;
358         argv += optind;
359
360         if (argc != 1)
361                 usage();
362
363         target = *argv;
364
365         if (device == NULL) {
366                 fprintf(stderr, "arping: device (option -I) is required\n");
367                 usage();
368         }
369
370         if (s < 0) {
371                 errno = socket_errno;
372                 perror("arping: socket");
373                 exit(2);
374         }
375
376         if (1) {
377                 struct ifreq ifr;
378                 memset(&ifr, 0, sizeof(ifr));
379                 strncpy(ifr.ifr_name, device, IFNAMSIZ-1);
380                 if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
381                         fprintf(stderr, "arping: unknown iface %s\n", device);
382                         exit(2);
383                 }
384                 ifindex = ifr.ifr_ifindex;
385
386                 if (ioctl(s, SIOCGIFFLAGS, (char*)&ifr)) {
387                         perror("ioctl(SIOCGIFFLAGS)");
388                         exit(2);
389                 }
390                 if (!(ifr.ifr_flags&IFF_UP)) {
391                         if (!quiet)
392                                 printf("Interface \"%s\" is down\n", device);
393                         exit(2);
394                 }
395                 if (ifr.ifr_flags&(IFF_NOARP|IFF_LOOPBACK)) {
396                         if (!quiet)
397                                 printf("Interface \"%s\" is not ARPable\n", device);
398                         exit(dad?0:2);
399                 }
400         }
401
402         if (inet_aton(target, &dst) != 1) {
403                 struct hostent *hp;
404                 hp = gethostbyname2(target, AF_INET);
405                 if (!hp) {
406                         fprintf(stderr, "arping: unknown host %s\n", target);
407                         exit(2);
408                 }
409                 memcpy(&dst, hp->h_addr, 4);
410         }
411
412         if (source && inet_aton(source, &src) != 1) {
413                 fprintf(stderr, "arping: invalid source %s\n", source);
414                 exit(2);
415         }
416
417         if (!dad && unsolicited && src.s_addr == 0)
418                 src = dst;
419         
420         if (!dad || src.s_addr) {
421                 struct sockaddr_in saddr;
422                 int probe_fd = socket(AF_INET, SOCK_DGRAM, 0);
423
424                 if (probe_fd < 0) {
425                         perror("socket");
426                         exit(2);
427                 }
428                 if (device) {
429                         if (setsockopt(probe_fd, SOL_SOCKET, SO_BINDTODEVICE, device, strlen(device)+1) == -1)
430                                 perror("WARNING: interface is ignored");
431                 }
432                 memset(&saddr, 0, sizeof(saddr));
433                 saddr.sin_family = AF_INET;
434                 if (src.s_addr) {
435                         saddr.sin_addr = src;
436                         if (bind(probe_fd, (struct sockaddr*)&saddr, sizeof(saddr)) == -1) {
437                                 perror("bind");
438                                 exit(2);
439                         }
440                 } else if (!dad) {
441                         int on = 1;
442                         socklen_t alen = sizeof(saddr);
443
444                         saddr.sin_port = htons(1025);
445                         saddr.sin_addr = dst;
446
447                         if (setsockopt(probe_fd, SOL_SOCKET, SO_DONTROUTE, (char*)&on, sizeof(on)) == -1)
448                                 perror("WARNING: setsockopt(SO_DONTROUTE)");
449                         if (connect(probe_fd, (struct sockaddr*)&saddr, sizeof(saddr)) == -1) {
450                                 perror("connect");
451                                 exit(2);
452                         }
453                         if (getsockname(probe_fd, (struct sockaddr*)&saddr, &alen) == -1) {
454                                 perror("getsockname");
455                                 exit(2);
456                         }
457                         src = saddr.sin_addr;
458                 }
459                 close(probe_fd);
460         };
461
462         me.sll_family = AF_PACKET;
463         me.sll_ifindex = ifindex;
464         me.sll_protocol = htons(ETH_P_ARP);
465         if (bind(s, (struct sockaddr*)&me, sizeof(me)) == -1) {
466                 perror("bind");
467                 exit(2);
468         }
469
470         if (1) {
471                 socklen_t alen = sizeof(me);
472                 if (getsockname(s, (struct sockaddr*)&me, &alen) == -1) {
473                         perror("getsockname");
474                         exit(2);
475                 }
476         }
477         if (me.sll_halen == 0) {
478                 if (!quiet)
479                         printf("Interface \"%s\" is not ARPable (no ll address)\n", device);
480                 exit(dad?0:2);
481         }
482
483         he = me;
484         memset(he.sll_addr, -1, he.sll_halen);
485
486         if (!quiet) {
487                 printf("ARPING %s ", inet_ntoa(dst));
488                 printf("from %s %s\n",  inet_ntoa(src), device ? : "");
489         }
490
491         if (!src.s_addr && !dad) {
492                 fprintf(stderr, "arping: no source address in not-DAD mode\n");
493                 exit(2);
494         }
495
496         set_signal(SIGINT, finish);
497         set_signal(SIGALRM, catcher);
498
499         catcher();
500
501         while(1) {
502                 sigset_t sset, osset;
503                 unsigned char packet[4096];
504                 struct sockaddr_ll from;
505                 socklen_t alen = sizeof(from);
506                 int cc;
507
508                 if ((cc = recvfrom(s, packet, sizeof(packet), 0,
509                                    (struct sockaddr *)&from, &alen)) < 0) {
510                         perror("arping: recvfrom");
511                         continue;
512                 }
513                 sigemptyset(&sset);
514                 sigaddset(&sset, SIGALRM);
515                 sigaddset(&sset, SIGINT);
516                 sigprocmask(SIG_BLOCK, &sset, &osset);
517                 recv_pack(packet, cc, &from);
518                 sigprocmask(SIG_SETMASK, &osset, NULL);
519         }
520 }
521
522