blob: b43e52e964e0b48c3031b41e9c820b9da2d21969 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Mike Frysinger7031f622006-05-08 03:20:50 +00002/*
Mike Frysinger7031f622006-05-08 03:20:50 +00003 * Mostly stolen from: dhcpcd - DHCP client daemon
4 * by Yoichi Hariguchi <yoichi@fore.com>
Denis Vlasenkodb12d1d2008-12-07 00:52:58 +00005 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02006 * Licensed under GPLv2, see file LICENSE in this source tree.
Mike Frysinger7031f622006-05-08 03:20:50 +00007 */
Mike Frysinger7031f622006-05-08 03:20:50 +00008#include <netinet/if_ether.h>
9#include <net/if_arp.h>
Mike Frysinger7031f622006-05-08 03:20:50 +000010
Mike Frysinger7031f622006-05-08 03:20:50 +000011#include "common.h"
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000012#include "dhcpd.h"
13
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000014struct arpMsg {
15 /* Ethernet header */
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000016 uint8_t h_dest[6]; /* 00 destination ether addr */
17 uint8_t h_source[6]; /* 06 source ether addr */
18 uint16_t h_proto; /* 0c packet type ID field */
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000019
20 /* ARP packet */
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000021 uint16_t htype; /* 0e hardware type (must be ARPHRD_ETHER) */
22 uint16_t ptype; /* 10 protocol type (must be ETH_P_IP) */
23 uint8_t hlen; /* 12 hardware address length (must be 6) */
24 uint8_t plen; /* 13 protocol address length (must be 4) */
25 uint16_t operation; /* 14 ARP opcode */
26 uint8_t sHaddr[6]; /* 16 sender's hardware address */
27 uint8_t sInaddr[4]; /* 1c sender's IP address */
28 uint8_t tHaddr[6]; /* 20 target's hardware address */
29 uint8_t tInaddr[4]; /* 26 target's IP address */
30 uint8_t pad[18]; /* 2a pad for min. ethernet payload (60 bytes) */
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000031} PACKED;
Mike Frysinger7031f622006-05-08 03:20:50 +000032
Denis Vlasenko223bc972007-11-22 00:58:49 +000033enum {
34 ARP_MSG_SIZE = 0x2a
35};
36
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000037/* Returns 1 if no reply received */
Denys Vlasenko6947d2c2009-06-17 13:24:03 +020038int FAST_FUNC arpping(uint32_t test_nip,
Denys Vlasenko47f2d7e2009-06-16 10:20:27 +020039 const uint8_t *safe_mac,
40 uint32_t from_ip,
41 uint8_t *from_mac,
42 const char *interface)
Mike Frysinger7031f622006-05-08 03:20:50 +000043{
Denis Vlasenko223bc972007-11-22 00:58:49 +000044 int timeout_ms;
Denis Vlasenko87f3b262007-09-07 13:43:28 +000045 struct pollfd pfd[1];
46#define s (pfd[0].fd) /* socket */
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000047 int rv = 1; /* "no reply received" yet */
48 struct sockaddr addr; /* for interface name */
49 struct arpMsg arp;
Mike Frysinger7031f622006-05-08 03:20:50 +000050
Denis Vlasenko48237b02006-11-22 23:22:06 +000051 s = socket(PF_PACKET, SOCK_PACKET, htons(ETH_P_ARP));
52 if (s == -1) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000053 bb_perror_msg(bb_msg_can_not_create_raw_socket);
Mike Frysinger7031f622006-05-08 03:20:50 +000054 return -1;
55 }
56
Denis Vlasenko48237b02006-11-22 23:22:06 +000057 if (setsockopt_broadcast(s) == -1) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +010058 bb_perror_msg("can't enable bcast on raw socket");
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000059 goto ret;
Mike Frysinger7031f622006-05-08 03:20:50 +000060 }
61
62 /* send arp request */
63 memset(&arp, 0, sizeof(arp));
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000064 memset(arp.h_dest, 0xff, 6); /* MAC DA */
65 memcpy(arp.h_source, from_mac, 6); /* MAC SA */
66 arp.h_proto = htons(ETH_P_ARP); /* protocol type (Ethernet) */
67 arp.htype = htons(ARPHRD_ETHER); /* hardware type */
68 arp.ptype = htons(ETH_P_IP); /* protocol type (ARP message) */
69 arp.hlen = 6; /* hardware address length */
70 arp.plen = 4; /* protocol address length */
71 arp.operation = htons(ARPOP_REQUEST); /* ARP op code */
72 memcpy(arp.sHaddr, from_mac, 6); /* source hardware address */
73 memcpy(arp.sInaddr, &from_ip, sizeof(from_ip)); /* source IP address */
Denys Vlasenko47f2d7e2009-06-16 10:20:27 +020074 /* tHaddr is zero-filled */ /* target hardware address */
Denys Vlasenko6947d2c2009-06-17 13:24:03 +020075 memcpy(arp.tInaddr, &test_nip, sizeof(test_nip));/* target IP address */
Mike Frysinger7031f622006-05-08 03:20:50 +000076
77 memset(&addr, 0, sizeof(addr));
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000078 safe_strncpy(addr.sa_data, interface, sizeof(addr.sa_data));
Denis Vlasenko223bc972007-11-22 00:58:49 +000079 if (sendto(s, &arp, sizeof(arp), 0, &addr, sizeof(addr)) < 0) {
80 // TODO: error message? caller didn't expect us to fail,
81 // just returning 1 "no reply received" misleads it.
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000082 goto ret;
Denis Vlasenko223bc972007-11-22 00:58:49 +000083 }
Mike Frysinger7031f622006-05-08 03:20:50 +000084
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000085 /* wait for arp reply, and check it */
Denis Vlasenko223bc972007-11-22 00:58:49 +000086 timeout_ms = 2000;
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000087 do {
Denys Vlasenko12ca0802010-02-04 18:41:18 +010088 typedef uint32_t aliased_uint32_t FIX_ALIASING;
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000089 int r;
Denys Vlasenkof2c8aa62010-01-12 12:52:30 +010090 unsigned prevTime = monotonic_ms();
Denis Vlasenko87f3b262007-09-07 13:43:28 +000091
92 pfd[0].events = POLLIN;
Denis Vlasenko5d61e712007-09-27 10:09:59 +000093 r = safe_poll(pfd, 1, timeout_ms);
Denis Vlasenko223bc972007-11-22 00:58:49 +000094 if (r < 0)
Denis Vlasenko5d61e712007-09-27 10:09:59 +000095 break;
Denis Vlasenko223bc972007-11-22 00:58:49 +000096 if (r) {
Denys Vlasenkocb23d932010-01-12 12:29:55 +010097 r = safe_read(s, &arp, sizeof(arp));
Denis Vlasenko223bc972007-11-22 00:58:49 +000098 if (r < 0)
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000099 break;
Denys Vlasenko47f2d7e2009-06-16 10:20:27 +0200100
Denys Vlasenkodc36a722009-06-17 13:56:50 +0200101 //log3("sHaddr %02x:%02x:%02x:%02x:%02x:%02x",
Denys Vlasenko47f2d7e2009-06-16 10:20:27 +0200102 // arp.sHaddr[0], arp.sHaddr[1], arp.sHaddr[2],
103 // arp.sHaddr[3], arp.sHaddr[4], arp.sHaddr[5]);
104
Denis Vlasenko223bc972007-11-22 00:58:49 +0000105 if (r >= ARP_MSG_SIZE
106 && arp.operation == htons(ARPOP_REPLY)
107 /* don't check it: Linux doesn't return proper tHaddr (fixed in 2.6.24?) */
108 /* && memcmp(arp.tHaddr, from_mac, 6) == 0 */
Denys Vlasenko12ca0802010-02-04 18:41:18 +0100109 && *(aliased_uint32_t*)arp.sInaddr == test_nip
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000110 ) {
Denys Vlasenkodc36a722009-06-17 13:56:50 +0200111 /* if ARP source MAC matches safe_mac
112 * (which is client's MAC), then it's not a conflict
113 * (client simply already has this IP and replies to ARPs!)
114 */
Denys Vlasenko47f2d7e2009-06-16 10:20:27 +0200115 if (!safe_mac || memcmp(safe_mac, arp.sHaddr, 6) != 0)
116 rv = 0;
Denys Vlasenkodc36a722009-06-17 13:56:50 +0200117 //else log2("sHaddr == safe_mac");
Mike Frysinger7031f622006-05-08 03:20:50 +0000118 break;
119 }
120 }
Denys Vlasenko6500d122012-04-17 17:10:31 +0200121 timeout_ms -= (unsigned)monotonic_ms() - prevTime + 1;
122
123 /* We used to check "timeout_ms > 0", but
124 * this is more under/overflow-resistant
125 * (people did see overflows here when system time jumps):
126 */
127 } while ((unsigned)timeout_ms <= 2000);
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000128
129 ret:
Mike Frysinger7031f622006-05-08 03:20:50 +0000130 close(s);
Denys Vlasenkoac906fa2009-06-17 11:54:52 +0200131 log1("%srp reply received for this address", rv ? "No a" : "A");
Mike Frysinger7031f622006-05-08 03:20:50 +0000132 return rv;
133}