2 * Address Conflict Detection implementation
3 * Copyright (c) 2008 by Michal Nazarewicz <mina86@mina86.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 * This is part of Tiny Applications Collection
19 * -> http://tinyapps.sourceforge.net/
23 * Address Conflict Detection is described in
24 * draft-cheshire-ipv4-acd-06.txt Internet-Draft which can be found at
25 * ftp://ftp.rfc-editor.org/in-notes/internet-drafts/draft-cheshire-ipv4-acd-06.txt
27 * It is not fully compatibl with the draft since it ignores any other
28 * probes of given address that someone may send but that's because
29 * this tool is ment for something slightly different (it's a good
30 * starting point for full ACD implementation though).
34 #define _DEFAULT_SOURCE
36 #include <arpa/inet.h>
38 #include <net/ethernet.h>
40 #include <net/if_arp.h>
41 #include <netinet/if_ether.h>
42 #include <netinet/in.h>
43 #include <netinet/ip.h>
44 #include <netpacket/packet.h>
49 #include <sys/ioctl.h>
50 #include <sys/select.h>
51 #include <sys/socket.h>
53 #include <sys/types.h>
58 /* http://tools.ietf.org/html/rfc826 */
60 /* Ethernet transmission layer */
61 uint8_t dest
[6]; /* Ethernet address of destination */
62 uint8_t source
[6]; /* Ethernet address of sender */
63 uint16_t proto
; /* Protocol type = htons(ETH_P_ARP) */
65 /* Ethernet packet data */
66 uint16_t htype
; /* Hardware address space = htons(ARPHRD_ETHER); */
67 uint16_t ptype
; /* Protocol address space = htons(ETH_P_IP); */
68 uint8_t hlen
; /* byte length of each hardware address = 6 */
69 uint8_t plen
; /* byte length of each protocol address = 8 */
70 uint16_t op
; /* opcode (ares_op$REQUEST | ares_op$REPLY) */
71 uint8_t sha
[6]; /* Hardware address of sender of this packet */
72 uint8_t spa
[4]; /* Protocol address of sender of this packet */
73 uint8_t tha
[6]; /* Hardware address of target of this packet */
74 uint8_t tpa
[4]; /* Protocol address of target */
76 /* Don't change spa and tpa to uint32_t as then compiler will try
77 to align those elements and __attribute__((packed)) will be
78 required and we might be compiled using compiler which does not
79 support it. Moreover, after packing structure spa and tpa
80 would not be aligned thus on some platforms writing to or
81 reading from those varaibles will cause an exception. */
84 uint8_t pad
[18]; /* Pad for min. ethernet payload (60 bytes) */
85 } /* __attribute__((packed)) */;
87 #define ARP_MSG_SIZE 0x2a
89 struct static_asserts
{
90 /* If compiler shows error here it means arp structure has invalid
91 size. Try uncomenting __attribute__((packed)) few lines
93 char struct_arp_has_wrong_size
[sizeof(struct arp
) - sizeof ((struct arp
*)0)->pad
== ARP_MSG_SIZE
? 1 : -1];
98 #define ERR(msg1, msg2) fprintf(stderr, "%s: %s: %s\n", *argv, (msg1), (msg2))
99 #define PERR(msg) ERR(msg, strerror(errno))
100 #define DIE(n, msg1, msg2) (ERR(msg1, msg2), exit(n))
101 #define PDIE(n, msg) DIE(n, msg, strerror(errno))
105 int main(int argc
, char **argv
) {
106 static const int one
= 1;
108 struct timeval tv
= { 0, 0 };
109 struct sockaddr addr
;
114 /* Parse arguments */
116 fprintf(stderr
, "usage: %s <interface> <ip>\n", *argv
);
121 uint32_t ip
= inet_addr(argv
[2]);
122 if (ip
== INADDR_NONE
) {
123 DIE(3, argv
[2], "invalid IP address");
125 memcpy(arp
.tpa
, &ip
, sizeof arp
.tpa
);
130 s
= socket(PF_PACKET
, SOCK_PACKET
, htons(ETH_P_ARP
));
135 if (setsockopt(s
, SOL_SOCKET
, SO_BINDTODEVICE
,
136 argv
[1], strlen(argv
[1]))<0) {
140 if (setsockopt(s
, SOL_SOCKET
, SO_BROADCAST
, &one
, sizeof one
) < 0) {
141 PDIE(2, "setting broadcast flag");
145 /* Get our hardware address */
146 memset(&addr
, 0, sizeof addr
);
147 strncpy(addr
.sa_data
, argv
[1], sizeof addr
.sa_data
);
150 strncpy(ifreq
.ifr_name
, addr
.sa_data
, sizeof ifreq
.ifr_name
);
151 if (ioctl(s
, SIOCGIFHWADDR
, &ifreq
) < 0) {
152 PDIE(2, "ioctl: get hardware address");
154 memcpy(arp
.source
, ifreq
.ifr_hwaddr
.sa_data
, sizeof arp
.source
);
158 /* Prepare ARP Probe */
159 memset(arp
.dest
, 0xff, sizeof arp
.dest
);
160 /* memcpy(arp.source, ..., 6); -- already set*/
161 arp
.proto
= htons(ETH_P_ARP
);
162 arp
.htype
= htons(ARPHRD_ETHER
);
163 arp
.ptype
= htons(ETH_P_IP
);
164 arp
.hlen
= sizeof arp
.sha
;
165 arp
.plen
= sizeof arp
.spa
;
166 arp
.op
= htons(ARPOP_REQUEST
);
167 memcpy(arp
.sha
, arp
.source
, sizeof arp
.sha
);
168 memset(arp
.spa
, 0, sizeof arp
.spa
);
169 memset(arp
.tha
, 0, sizeof arp
.tha
);
170 /* addr.tpa = ...; -- already set */
173 /* Initial random delay */
176 tv
.tv_usec
= rand() % 1000;
177 select(0, 0, 0, 0, &tv
);
183 if (sendto(s
, &arp
, sizeof arp
, 0, &addr
, sizeof addr
) < 0) {
187 /* Random delay until repeated probes */
188 if (++count
== 3) { /* that's the last one */
191 } else { /* there are more to go */
193 tv
.tv_usec
= rand() % 1000;
196 /* Wait for ARP reply */
204 ret
= select(s
+ 1, &fds
, 0, 0, &tv
);
208 } else if (ret
< 0) {
210 } else if ((ret
= read(s
, &arp_rec
, sizeof arp_rec
)) < 0) {
212 } else if (ret
>= ARP_MSG_SIZE
&&
213 !memcmp(arp_rec
.spa
, arp
.tpa
, sizeof arp
.tpa
)) {
214 printf("%02x:%02x:%02x:%02x:%02x:%02x\n",
215 arp_rec
.sha
[0], arp_rec
.sha
[1], arp_rec
.sha
[2],
216 arp_rec
.sha
[3], arp_rec
.sha
[4], arp_rec
.sha
[5]);