Magellan Linux

Annotation of /trunk/mkinitrd-magellan/busybox/networking/arping.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 532 - (hide annotations) (download)
Sat Sep 1 22:45:15 2007 UTC (16 years, 8 months ago) by niro
File MIME type: text/plain
File size: 10025 byte(s)
-import if magellan mkinitrd; it is a fork of redhats mkinitrd-5.0.8 with all magellan patches and features; deprecates magellan-src/mkinitrd

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