2006-07-03 01:17:05 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
2006-05-08 08:50:50 +05:30
|
|
|
/*
|
|
|
|
* Mostly stolen from: dhcpcd - DHCP client daemon
|
|
|
|
* by Yoichi Hariguchi <yoichi@fore.com>
|
2008-12-07 06:22:58 +05:30
|
|
|
*
|
2010-08-16 23:44:46 +05:30
|
|
|
* Licensed under GPLv2, see file LICENSE in this source tree.
|
2006-05-08 08:50:50 +05:30
|
|
|
*/
|
|
|
|
#include <netinet/if_ether.h>
|
|
|
|
#include <net/if_arp.h>
|
|
|
|
|
|
|
|
#include "common.h"
|
2006-11-19 01:21:32 +05:30
|
|
|
|
|
|
|
struct arpMsg {
|
|
|
|
/* Ethernet header */
|
2007-07-03 21:17:50 +05:30
|
|
|
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-19 01:21:32 +05:30
|
|
|
|
|
|
|
/* ARP packet */
|
2007-07-03 21:17:50 +05:30
|
|
|
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 14:48:54 +05:30
|
|
|
} PACKED;
|
2006-05-08 08:50:50 +05:30
|
|
|
|
2007-11-22 06:28:49 +05:30
|
|
|
enum {
|
|
|
|
ARP_MSG_SIZE = 0x2a
|
|
|
|
};
|
|
|
|
|
2007-07-03 21:17:50 +05:30
|
|
|
/* Returns 1 if no reply received */
|
2009-06-17 16:54:03 +05:30
|
|
|
int FAST_FUNC arpping(uint32_t test_nip,
|
2009-06-16 13:50:27 +05:30
|
|
|
const uint8_t *safe_mac,
|
|
|
|
uint32_t from_ip,
|
|
|
|
uint8_t *from_mac,
|
2014-10-30 16:29:04 +05:30
|
|
|
const char *interface,
|
|
|
|
unsigned timeo)
|
2007-07-03 21:17:50 +05:30
|
|
|
{
|
2007-11-22 06:28:49 +05:30
|
|
|
int timeout_ms;
|
2007-09-07 19:13:28 +05:30
|
|
|
struct pollfd pfd[1];
|
|
|
|
#define s (pfd[0].fd) /* socket */
|
2007-07-03 21:17:50 +05:30
|
|
|
int rv = 1; /* "no reply received" yet */
|
|
|
|
struct sockaddr addr; /* for interface name */
|
|
|
|
struct arpMsg arp;
|
2021-06-03 15:02:40 +05:30
|
|
|
const char *msg;
|
2006-05-08 08:50:50 +05:30
|
|
|
|
2014-10-30 16:29:04 +05:30
|
|
|
if (!timeo)
|
|
|
|
return 1;
|
|
|
|
|
2006-11-23 04:52:06 +05:30
|
|
|
s = socket(PF_PACKET, SOCK_PACKET, htons(ETH_P_ARP));
|
|
|
|
if (s == -1) {
|
libbb: reduce the overhead of single parameter bb_error_msg() calls
Back in 2007, commit 0c97c9d43707 ("'simple' error message functions by
Loic Grenie") introduced bb_simple_perror_msg() to allow for a lower
overhead call to bb_perror_msg() when only a string was being printed
with no parameters. This saves space for some CPU architectures because
it avoids the overhead of a call to a variadic function. However there
has never been a simple version of bb_error_msg(), and since 2007 many
new calls to bb_perror_msg() have been added that only take a single
parameter and so could have been using bb_simple_perror_message().
This changeset introduces 'simple' versions of bb_info_msg(),
bb_error_msg(), bb_error_msg_and_die(), bb_herror_msg() and
bb_herror_msg_and_die(), and replaces all calls that only take a
single parameter, or use something like ("%s", arg), with calls to the
corresponding 'simple' version.
Since it is likely that single parameter calls to the variadic functions
may be accidentally reintroduced in the future a new debugging config
option WARN_SIMPLE_MSG has been introduced. This uses some macro magic
which will cause any such calls to generate a warning, but this is
turned off by default to avoid use of the unpleasant macros in normal
circumstances.
This is a large changeset due to the number of calls that have been
replaced. The only files that contain changes other than simple
substitution of function calls are libbb.h, libbb/herror_msg.c,
libbb/verror_msg.c and libbb/xfuncs_printf.c. In miscutils/devfsd.c,
networking/udhcp/common.h and util-linux/mdev.c additonal macros have
been added for logging so that single parameter and multiple parameter
logging variants exist.
The amount of space saved varies considerably by architecture, and was
found to be as follows (for 'defconfig' using GCC 7.4):
Arm: -92 bytes
MIPS: -52 bytes
PPC: -1836 bytes
x86_64: -938 bytes
Note that for the MIPS architecture only an exception had to be made
disabling the 'simple' calls for 'udhcp' (in networking/udhcp/common.h)
because it made these files larger on MIPS.
Signed-off-by: James Byrne <james.byrne@origamienergy.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2019-07-02 15:05:03 +05:30
|
|
|
bb_simple_perror_msg(bb_msg_can_not_create_raw_socket);
|
2006-05-08 08:50:50 +05:30
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-11-23 04:52:06 +05:30
|
|
|
if (setsockopt_broadcast(s) == -1) {
|
2021-06-03 15:02:40 +05:30
|
|
|
bb_simple_perror_msg("can't enable bcast on ARP socket");
|
2007-07-03 21:17:50 +05:30
|
|
|
goto ret;
|
2006-05-08 08:50:50 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* send arp request */
|
|
|
|
memset(&arp, 0, sizeof(arp));
|
2007-07-03 21:17:50 +05:30
|
|
|
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 13:50:27 +05:30
|
|
|
/* tHaddr is zero-filled */ /* target hardware address */
|
2009-06-17 16:54:03 +05:30
|
|
|
memcpy(arp.tInaddr, &test_nip, sizeof(test_nip));/* target IP address */
|
2006-05-08 08:50:50 +05:30
|
|
|
|
|
|
|
memset(&addr, 0, sizeof(addr));
|
2007-07-03 21:17:50 +05:30
|
|
|
safe_strncpy(addr.sa_data, interface, sizeof(addr.sa_data));
|
2007-11-22 06:28:49 +05:30
|
|
|
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 21:17:50 +05:30
|
|
|
goto ret;
|
2007-11-22 06:28:49 +05:30
|
|
|
}
|
2006-05-08 08:50:50 +05:30
|
|
|
|
2007-07-03 21:17:50 +05:30
|
|
|
/* wait for arp reply, and check it */
|
2014-10-30 16:29:04 +05:30
|
|
|
timeout_ms = (int)timeo;
|
2007-07-03 21:17:50 +05:30
|
|
|
do {
|
2010-02-04 23:11:18 +05:30
|
|
|
typedef uint32_t aliased_uint32_t FIX_ALIASING;
|
2007-07-03 21:17:50 +05:30
|
|
|
int r;
|
2010-01-12 17:22:30 +05:30
|
|
|
unsigned prevTime = monotonic_ms();
|
2007-09-07 19:13:28 +05:30
|
|
|
|
|
|
|
pfd[0].events = POLLIN;
|
2007-09-27 15:39:59 +05:30
|
|
|
r = safe_poll(pfd, 1, timeout_ms);
|
2007-11-22 06:28:49 +05:30
|
|
|
if (r < 0)
|
2007-09-27 15:39:59 +05:30
|
|
|
break;
|
2007-11-22 06:28:49 +05:30
|
|
|
if (r) {
|
2010-01-12 16:59:55 +05:30
|
|
|
r = safe_read(s, &arp, sizeof(arp));
|
2007-11-22 06:28:49 +05:30
|
|
|
if (r < 0)
|
2007-07-03 21:17:50 +05:30
|
|
|
break;
|
2009-06-16 13:50:27 +05:30
|
|
|
|
2009-06-17 17:26:50 +05:30
|
|
|
//log3("sHaddr %02x:%02x:%02x:%02x:%02x:%02x",
|
2009-06-16 13:50:27 +05:30
|
|
|
// arp.sHaddr[0], arp.sHaddr[1], arp.sHaddr[2],
|
|
|
|
// arp.sHaddr[3], arp.sHaddr[4], arp.sHaddr[5]);
|
|
|
|
|
2007-11-22 06:28:49 +05:30
|
|
|
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 23:11:18 +05:30
|
|
|
&& *(aliased_uint32_t*)arp.sInaddr == test_nip
|
2007-07-03 21:17:50 +05:30
|
|
|
) {
|
2009-06-17 17:26:50 +05:30
|
|
|
/* 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 13:50:27 +05:30
|
|
|
if (!safe_mac || memcmp(safe_mac, arp.sHaddr, 6) != 0)
|
|
|
|
rv = 0;
|
2009-06-17 17:26:50 +05:30
|
|
|
//else log2("sHaddr == safe_mac");
|
2006-05-08 08:50:50 +05:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-04-17 20:40:31 +05:30
|
|
|
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):
|
|
|
|
*/
|
2014-10-30 16:29:04 +05:30
|
|
|
} while ((unsigned)timeout_ms <= timeo);
|
2007-07-03 21:17:50 +05:30
|
|
|
|
|
|
|
ret:
|
2006-05-08 08:50:50 +05:30
|
|
|
close(s);
|
2021-06-03 15:02:40 +05:30
|
|
|
msg = "no ARP reply received for this address";
|
|
|
|
if (rv == 0)
|
|
|
|
msg += 3;
|
|
|
|
log1s(msg);
|
2006-05-08 08:50:50 +05:30
|
|
|
return rv;
|
|
|
|
}
|