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.
9
* Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
13
#include <sys/param.h>
14
#include <sys/socket.h>
15
#include <linux/sockios.h>
18
#include <sys/signal.h>
19
#include <sys/ioctl.h>
21
#include <linux/if_arp.h>
30
#include <netinet/in.h>
31
#include <arpa/inet.h>
35
static void usage(void) __attribute__((noreturn));
41
struct in_addr src, dst;
43
int dad, unsolicited, advert;
51
struct sockaddr_ll me;
52
struct sockaddr_ll he;
54
struct timeval start, last;
57
int received, brd_recv, req_recv;
59
#define MS_TDIFF(tv1,tv2) ( ((tv1).tv_sec-(tv2).tv_sec)*1000 + \
60
((tv1).tv_usec-(tv2).tv_usec)/1000 )
65
"Usage: arping [-fqbDUAV] [-c count] [-w timeout] [-I device] [-s source] destination\n"
66
" -f : quit on first reply\n"
68
" -b : keep broadcasting, don't go unicast\n"
69
" -D : duplicate address detection mode\n"
70
" -U : Unsolicited ARP mode, update your neighbours\n"
71
" -A : ARP answer mode, update your neighbours\n"
72
" -V : print version and exit\n"
73
" -c count : how many packets to send\n"
74
" -w timeout : how long to wait for a reply\n"
75
" -I device : which ethernet device to use (eth0)\n"
76
" -s source : source ip address\n"
77
" destination : ask for what ip address\n"
82
void set_signal(int signo, void (*handler)(void))
86
memset(&sa, 0, sizeof(sa));
87
sa.sa_handler = (void (*)(int))handler;
88
sa.sa_flags = SA_RESTART;
89
sigaction(signo, &sa, NULL);
92
int send_pack(int s, struct in_addr src, struct in_addr dst,
93
struct sockaddr_ll *ME, struct sockaddr_ll *HE)
97
unsigned char buf[256];
98
struct arphdr *ah = (struct arphdr*)buf;
99
unsigned char *p = (unsigned char *)(ah+1);
101
ah->ar_hrd = htons(ME->sll_hatype);
102
if (ah->ar_hrd == htons(ARPHRD_FDDI))
103
ah->ar_hrd = htons(ARPHRD_ETHER);
104
ah->ar_pro = htons(ETH_P_IP);
105
ah->ar_hln = ME->sll_halen;
107
ah->ar_op = advert ? htons(ARPOP_REPLY) : htons(ARPOP_REQUEST);
109
memcpy(p, &ME->sll_addr, ah->ar_hln);
116
memcpy(p, &ME->sll_addr, ah->ar_hln);
118
memcpy(p, &HE->sll_addr, ah->ar_hln);
124
gettimeofday(&now, NULL);
125
err = sendto(s, buf, p-buf, 0, (struct sockaddr*)HE, sizeof(*HE));
138
printf("Sent %d probes (%d broadcast(s))\n", sent, brd_sent);
139
printf("Received %d response(s)", received);
140
if (brd_recv || req_recv) {
143
printf("%d request(s)", req_recv);
145
printf("%s%d broadcast(s)",
146
req_recv ? ", " : "",
164
gettimeofday(&tv, NULL);
169
if (count-- == 0 || (timeout && MS_TDIFF(tv,start) > timeout*1000 + 500))
172
if (last.tv_sec==0 || MS_TDIFF(tv,last) > 500) {
173
send_pack(s, src, dst, &me, &he);
174
if (count == 0 && unsolicited)
180
void print_hex(unsigned char *p, int len)
183
for (i=0; i<len; i++) {
184
printf("%02X", p[i]);
190
int recv_pack(unsigned char *buf, int len, struct sockaddr_ll *FROM)
193
struct arphdr *ah = (struct arphdr*)buf;
194
unsigned char *p = (unsigned char *)(ah+1);
195
struct in_addr src_ip, dst_ip;
197
gettimeofday(&tv, NULL);
199
/* Filter out wild packets */
200
if (FROM->sll_pkttype != PACKET_HOST &&
201
FROM->sll_pkttype != PACKET_BROADCAST &&
202
FROM->sll_pkttype != PACKET_MULTICAST)
205
/* Only these types are recognised */
206
if (ah->ar_op != htons(ARPOP_REQUEST) &&
207
ah->ar_op != htons(ARPOP_REPLY))
210
/* ARPHRD check and this darned FDDI hack here :-( */
211
if (ah->ar_hrd != htons(FROM->sll_hatype) &&
212
(FROM->sll_hatype != ARPHRD_FDDI || ah->ar_hrd != htons(ARPHRD_ETHER)))
215
/* Protocol must be IP. */
216
if (ah->ar_pro != htons(ETH_P_IP))
220
if (ah->ar_hln != me.sll_halen)
222
if (len < sizeof(*ah) + 2*(4 + ah->ar_hln))
224
memcpy(&src_ip, p+ah->ar_hln, 4);
225
memcpy(&dst_ip, p+ah->ar_hln+4+ah->ar_hln, 4);
227
if (src_ip.s_addr != dst.s_addr)
229
if (src.s_addr != dst_ip.s_addr)
231
if (memcmp(p+ah->ar_hln+4, &me.sll_addr, ah->ar_hln))
235
src_ip = 0 (or some src)
237
dst_ip = tested address
240
We fail, if receive request/reply with:
241
src_ip = tested_address
243
if src_ip in request was not zero, check
244
also that it matches to dst_ip, otherwise
245
dst_ip/dst_hw do not matter.
247
if (src_ip.s_addr != dst.s_addr)
249
if (memcmp(p, &me.sll_addr, me.sll_halen) == 0)
251
if (src.s_addr && src.s_addr != dst_ip.s_addr)
256
printf("%s ", FROM->sll_pkttype==PACKET_HOST ? "Unicast" : "Broadcast");
257
printf("%s from ", ah->ar_op == htons(ARPOP_REPLY) ? "reply" : "request");
258
printf("%s [", inet_ntoa(src_ip));
259
print_hex(p, ah->ar_hln);
261
if (dst_ip.s_addr != src.s_addr) {
262
printf("for %s ", inet_ntoa(dst_ip));
265
if (memcmp(p+ah->ar_hln+4, me.sll_addr, ah->ar_hln)) {
269
print_hex(p+ah->ar_hln+4, ah->ar_hln);
273
long usecs = (tv.tv_sec-last.tv_sec) * 1000000 +
274
tv.tv_usec-last.tv_usec;
275
long msecs = (usecs+500)/1000;
276
usecs -= msecs*1000 - 500;
277
printf(" %ld.%03ldms\n", msecs, usecs);
279
printf(" UNSOLICITED?\n");
284
if (FROM->sll_pkttype != PACKET_HOST)
286
if (ah->ar_op == htons(ARPOP_REQUEST))
290
if(!broadcast_only) {
291
memcpy(he.sll_addr, p, me.sll_halen);
298
main(int argc, char **argv)
302
uid_t uid = getuid();
304
s = socket(PF_PACKET, SOCK_DGRAM, 0);
305
socket_errno = errno;
309
while ((ch = getopt(argc, argv, "h?bfDUAqc:w:s:I:V")) != EOF) {
329
count = atoi(optarg);
332
timeout = atoi(optarg);
344
printf("arping utility, iputils-ss%s\n", SNAPSHOT);
360
if (device == NULL) {
361
fprintf(stderr, "arping: device (option -I) is required\n");
366
errno = socket_errno;
367
perror("arping: socket");
373
memset(&ifr, 0, sizeof(ifr));
374
strncpy(ifr.ifr_name, device, IFNAMSIZ-1);
375
if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
376
fprintf(stderr, "arping: unknown iface %s\n", device);
379
ifindex = ifr.ifr_ifindex;
381
if (ioctl(s, SIOCGIFFLAGS, (char*)&ifr)) {
382
perror("ioctl(SIOCGIFFLAGS)");
385
if (!(ifr.ifr_flags&IFF_UP)) {
387
printf("Interface \"%s\" is down\n", device);
390
if (ifr.ifr_flags&(IFF_NOARP|IFF_LOOPBACK)) {
392
printf("Interface \"%s\" is not ARPable\n", device);
397
if (inet_aton(target, &dst) != 1) {
399
hp = gethostbyname2(target, AF_INET);
401
fprintf(stderr, "arping: unknown host %s\n", target);
404
memcpy(&dst, hp->h_addr, 4);
407
if (source && inet_aton(source, &src) != 1) {
408
fprintf(stderr, "arping: invalid source %s\n", source);
412
if (!dad && unsolicited && src.s_addr == 0)
415
if (!dad || src.s_addr) {
416
struct sockaddr_in saddr;
417
int probe_fd = socket(AF_INET, SOCK_DGRAM, 0);
424
if (setsockopt(probe_fd, SOL_SOCKET, SO_BINDTODEVICE, device, strlen(device)+1) == -1)
425
perror("WARNING: interface is ignored");
427
memset(&saddr, 0, sizeof(saddr));
428
saddr.sin_family = AF_INET;
430
saddr.sin_addr = src;
431
if (bind(probe_fd, (struct sockaddr*)&saddr, sizeof(saddr)) == -1) {
437
int alen = sizeof(saddr);
439
saddr.sin_port = htons(1025);
440
saddr.sin_addr = dst;
442
if (setsockopt(probe_fd, SOL_SOCKET, SO_DONTROUTE, (char*)&on, sizeof(on)) == -1)
443
perror("WARNING: setsockopt(SO_DONTROUTE)");
444
if (connect(probe_fd, (struct sockaddr*)&saddr, sizeof(saddr)) == -1) {
448
if (getsockname(probe_fd, (struct sockaddr*)&saddr, &alen) == -1) {
449
perror("getsockname");
452
src = saddr.sin_addr;
457
me.sll_family = AF_PACKET;
458
me.sll_ifindex = ifindex;
459
me.sll_protocol = htons(ETH_P_ARP);
460
if (bind(s, (struct sockaddr*)&me, sizeof(me)) == -1) {
466
int alen = sizeof(me);
467
if (getsockname(s, (struct sockaddr*)&me, &alen) == -1) {
468
perror("getsockname");
472
if (me.sll_halen == 0) {
474
printf("Interface \"%s\" is not ARPable (no ll address)\n", device);
479
memset(he.sll_addr, -1, he.sll_halen);
482
printf("ARPING %s ", inet_ntoa(dst));
483
printf("from %s %s\n", inet_ntoa(src), device ? : "");
486
if (!src.s_addr && !dad) {
487
fprintf(stderr, "arping: no source address in not-DAD mode\n");
491
set_signal(SIGINT, finish);
492
set_signal(SIGALRM, catcher);
497
sigset_t sset, osset;
499
struct sockaddr_ll from;
500
int alen = sizeof(from);
503
if ((cc = recvfrom(s, packet, sizeof(packet), 0,
504
(struct sockaddr *)&from, &alen)) < 0) {
505
perror("arping: recvfrom");
509
sigaddset(&sset, SIGALRM);
510
sigaddset(&sset, SIGINT);
511
sigprocmask(SIG_BLOCK, &sset, &osset);
512
recv_pack(packet, cc, &from);
513
sigprocmask(SIG_SETMASK, &osset, NULL);