2006-07-02 19:47:05 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
2006-05-08 03:20:50 +00:00
|
|
|
/*
|
|
|
|
* Mostly stolen from: dhcpcd - DHCP client daemon
|
|
|
|
* by Yoichi Hariguchi <yoichi@fore.com>
|
2008-12-07 00:52:58 +00:00
|
|
|
*
|
2010-08-16 18:14:46 +00:00
|
|
|
* Licensed under GPLv2, see file LICENSE in this source tree.
|
2006-05-08 03:20:50 +00:00
|
|
|
*/
|
|
|
|
#include <netinet/if_ether.h>
|
|
|
|
#include <net/if_arp.h>
|
|
|
|
|
|
|
|
#include "common.h"
|
2006-11-18 19:51:32 +00:00
|
|
|
#include "dhcpd.h"
|
|
|
|
|
|
|
|
struct arpMsg {
|
|
|
|
/* Ethernet header */
|
2007-07-03 15:47:50 +00:00
|
|
|
uint8_t h_dest[6]; /* 00 destination ether addr */
|
|
|
|
uint8_t h_source[6]; /* 06 source ether addr */
|
|
|
|
uint16_t h_proto; /* 0c packet type ID field */
|
2006-11-18 19:51:32 +00:00
|
|
|
|
|
|
|
/* ARP packet */
|
2007-07-03 15:47:50 +00:00
|
|
|
uint16_t htype; /* 0e hardware type (must be ARPHRD_ETHER) */
|
|
|
|
uint16_t ptype; /* 10 protocol type (must be ETH_P_IP) */
|
|
|
|
uint8_t hlen; /* 12 hardware address length (must be 6) */
|
|
|
|
uint8_t plen; /* 13 protocol address length (must be 4) */
|
|
|
|
uint16_t operation; /* 14 ARP opcode */
|
|
|
|
uint8_t sHaddr[6]; /* 16 sender's hardware address */
|
|
|
|
uint8_t sInaddr[4]; /* 1c sender's IP address */
|
|
|
|
uint8_t tHaddr[6]; /* 20 target's hardware address */
|
|
|
|
uint8_t tInaddr[4]; /* 26 target's IP address */
|
|
|
|
uint8_t pad[18]; /* 2a pad for min. ethernet payload (60 bytes) */
|
2008-07-05 09:18:54 +00:00
|
|
|
} PACKED;
|
2006-05-08 03:20:50 +00:00
|
|
|
|
2007-11-22 00:58:49 +00:00
|
|
|
enum {
|
|
|
|
ARP_MSG_SIZE = 0x2a
|
|
|
|
};
|
|
|
|
|
2007-07-03 15:47:50 +00:00
|
|
|
/* Returns 1 if no reply received */
|
2009-06-17 11:24:03 +00:00
|
|
|
int FAST_FUNC arpping(uint32_t test_nip,
|
2009-06-16 08:20:27 +00:00
|
|
|
const uint8_t *safe_mac,
|
|
|
|
uint32_t from_ip,
|
|
|
|
uint8_t *from_mac,
|
|
|
|
const char *interface)
|
2007-07-03 15:47:50 +00:00
|
|
|
{
|
2007-11-22 00:58:49 +00:00
|
|
|
int timeout_ms;
|
2007-09-07 13:43:28 +00:00
|
|
|
struct pollfd pfd[1];
|
|
|
|
#define s (pfd[0].fd) /* socket */
|
2007-07-03 15:47:50 +00:00
|
|
|
int rv = 1; /* "no reply received" yet */
|
|
|
|
struct sockaddr addr; /* for interface name */
|
|
|
|
struct arpMsg arp;
|
2006-05-08 03:20:50 +00:00
|
|
|
|
2006-11-22 23:22:06 +00:00
|
|
|
s = socket(PF_PACKET, SOCK_PACKET, htons(ETH_P_ARP));
|
|
|
|
if (s == -1) {
|
2006-09-06 18:36:50 +00:00
|
|
|
bb_perror_msg(bb_msg_can_not_create_raw_socket);
|
2006-05-08 03:20:50 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-11-22 23:22:06 +00:00
|
|
|
if (setsockopt_broadcast(s) == -1) {
|
2009-11-13 08:08:27 +00:00
|
|
|
bb_perror_msg("can't enable bcast on raw socket");
|
2007-07-03 15:47:50 +00:00
|
|
|
goto ret;
|
2006-05-08 03:20:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* send arp request */
|
|
|
|
memset(&arp, 0, sizeof(arp));
|
2007-07-03 15:47:50 +00:00
|
|
|
memset(arp.h_dest, 0xff, 6); /* MAC DA */
|
|
|
|
memcpy(arp.h_source, from_mac, 6); /* MAC SA */
|
|
|
|
arp.h_proto = htons(ETH_P_ARP); /* protocol type (Ethernet) */
|
|
|
|
arp.htype = htons(ARPHRD_ETHER); /* hardware type */
|
|
|
|
arp.ptype = htons(ETH_P_IP); /* protocol type (ARP message) */
|
|
|
|
arp.hlen = 6; /* hardware address length */
|
|
|
|
arp.plen = 4; /* protocol address length */
|
|
|
|
arp.operation = htons(ARPOP_REQUEST); /* ARP op code */
|
|
|
|
memcpy(arp.sHaddr, from_mac, 6); /* source hardware address */
|
|
|
|
memcpy(arp.sInaddr, &from_ip, sizeof(from_ip)); /* source IP address */
|
2009-06-16 08:20:27 +00:00
|
|
|
/* tHaddr is zero-filled */ /* target hardware address */
|
2009-06-17 11:24:03 +00:00
|
|
|
memcpy(arp.tInaddr, &test_nip, sizeof(test_nip));/* target IP address */
|
2006-05-08 03:20:50 +00:00
|
|
|
|
|
|
|
memset(&addr, 0, sizeof(addr));
|
2007-07-03 15:47:50 +00:00
|
|
|
safe_strncpy(addr.sa_data, interface, sizeof(addr.sa_data));
|
2007-11-22 00:58:49 +00:00
|
|
|
if (sendto(s, &arp, sizeof(arp), 0, &addr, sizeof(addr)) < 0) {
|
|
|
|
// TODO: error message? caller didn't expect us to fail,
|
|
|
|
// just returning 1 "no reply received" misleads it.
|
2007-07-03 15:47:50 +00:00
|
|
|
goto ret;
|
2007-11-22 00:58:49 +00:00
|
|
|
}
|
2006-05-08 03:20:50 +00:00
|
|
|
|
2007-07-03 15:47:50 +00:00
|
|
|
/* wait for arp reply, and check it */
|
2007-11-22 00:58:49 +00:00
|
|
|
timeout_ms = 2000;
|
2007-07-03 15:47:50 +00:00
|
|
|
do {
|
2010-02-04 17:41:18 +00:00
|
|
|
typedef uint32_t aliased_uint32_t FIX_ALIASING;
|
2007-07-03 15:47:50 +00:00
|
|
|
int r;
|
2010-01-12 11:52:30 +00:00
|
|
|
unsigned prevTime = monotonic_ms();
|
2007-09-07 13:43:28 +00:00
|
|
|
|
|
|
|
pfd[0].events = POLLIN;
|
2007-09-27 10:09:59 +00:00
|
|
|
r = safe_poll(pfd, 1, timeout_ms);
|
2007-11-22 00:58:49 +00:00
|
|
|
if (r < 0)
|
2007-09-27 10:09:59 +00:00
|
|
|
break;
|
2007-11-22 00:58:49 +00:00
|
|
|
if (r) {
|
2010-01-12 11:29:55 +00:00
|
|
|
r = safe_read(s, &arp, sizeof(arp));
|
2007-11-22 00:58:49 +00:00
|
|
|
if (r < 0)
|
2007-07-03 15:47:50 +00:00
|
|
|
break;
|
2009-06-16 08:20:27 +00:00
|
|
|
|
2009-06-17 11:56:50 +00:00
|
|
|
//log3("sHaddr %02x:%02x:%02x:%02x:%02x:%02x",
|
2009-06-16 08:20:27 +00:00
|
|
|
// arp.sHaddr[0], arp.sHaddr[1], arp.sHaddr[2],
|
|
|
|
// arp.sHaddr[3], arp.sHaddr[4], arp.sHaddr[5]);
|
|
|
|
|
2007-11-22 00:58:49 +00:00
|
|
|
if (r >= ARP_MSG_SIZE
|
|
|
|
&& arp.operation == htons(ARPOP_REPLY)
|
|
|
|
/* don't check it: Linux doesn't return proper tHaddr (fixed in 2.6.24?) */
|
|
|
|
/* && memcmp(arp.tHaddr, from_mac, 6) == 0 */
|
2010-02-04 17:41:18 +00:00
|
|
|
&& *(aliased_uint32_t*)arp.sInaddr == test_nip
|
2007-07-03 15:47:50 +00:00
|
|
|
) {
|
2009-06-17 11:56:50 +00:00
|
|
|
/* if ARP source MAC matches safe_mac
|
|
|
|
* (which is client's MAC), then it's not a conflict
|
|
|
|
* (client simply already has this IP and replies to ARPs!)
|
|
|
|
*/
|
2009-06-16 08:20:27 +00:00
|
|
|
if (!safe_mac || memcmp(safe_mac, arp.sHaddr, 6) != 0)
|
|
|
|
rv = 0;
|
2009-06-17 11:56:50 +00:00
|
|
|
//else log2("sHaddr == safe_mac");
|
2006-05-08 03:20:50 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-04-17 15:10:31 +00:00
|
|
|
timeout_ms -= (unsigned)monotonic_ms() - prevTime + 1;
|
|
|
|
|
|
|
|
/* We used to check "timeout_ms > 0", but
|
|
|
|
* this is more under/overflow-resistant
|
|
|
|
* (people did see overflows here when system time jumps):
|
|
|
|
*/
|
|
|
|
} while ((unsigned)timeout_ms <= 2000);
|
2007-07-03 15:47:50 +00:00
|
|
|
|
|
|
|
ret:
|
2006-05-08 03:20:50 +00:00
|
|
|
close(s);
|
2009-06-17 09:54:52 +00:00
|
|
|
log1("%srp reply received for this address", rv ? "No a" : "A");
|
2006-05-08 03:20:50 +00:00
|
|
|
return rv;
|
|
|
|
}
|