2006-07-03 01:17:05 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
2006-05-08 08:50:50 +05:30
|
|
|
/*
|
|
|
|
* arpping.c
|
|
|
|
*
|
|
|
|
* Mostly stolen from: dhcpcd - DHCP client daemon
|
|
|
|
* by Yoichi Hariguchi <yoichi@fore.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <netinet/if_ether.h>
|
|
|
|
#include <net/if_arp.h>
|
|
|
|
|
|
|
|
#include "common.h"
|
2006-11-19 01:21:32 +05:30
|
|
|
#include "dhcpd.h"
|
|
|
|
|
|
|
|
|
|
|
|
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) */
|
2006-11-19 01:21:32 +05:30
|
|
|
} ATTRIBUTE_PACKED;
|
2006-05-08 08:50:50 +05:30
|
|
|
|
|
|
|
|
2007-07-03 21:17:50 +05:30
|
|
|
/* Returns 1 if no reply received */
|
2006-05-08 08:50:50 +05:30
|
|
|
|
2007-07-03 21:17:50 +05:30
|
|
|
int arpping(uint32_t test_ip, uint32_t from_ip, uint8_t *from_mac, const char *interface)
|
|
|
|
{
|
2007-09-07 19:13:28 +05:30
|
|
|
int timeout_ms = 2000;
|
|
|
|
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;
|
2006-05-08 08:50:50 +05:30
|
|
|
|
2006-11-23 04:52:06 +05:30
|
|
|
s = socket(PF_PACKET, SOCK_PACKET, htons(ETH_P_ARP));
|
|
|
|
if (s == -1) {
|
2006-09-07 00:06:50 +05:30
|
|
|
bb_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) {
|
2006-10-14 07:53:43 +05:30
|
|
|
bb_perror_msg("cannot setsocketopt on raw 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 */
|
|
|
|
/* tHaddr */ /* target hardware address */
|
|
|
|
memcpy(arp.tInaddr, &test_ip, sizeof(test_ip)); /* 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));
|
2006-05-08 08:50:50 +05:30
|
|
|
if (sendto(s, &arp, sizeof(arp), 0, &addr, sizeof(addr)) < 0)
|
2007-07-03 21:17:50 +05:30
|
|
|
goto ret;
|
2006-05-08 08:50:50 +05:30
|
|
|
|
2007-07-03 21:17:50 +05:30
|
|
|
/* wait for arp reply, and check it */
|
|
|
|
do {
|
|
|
|
int r;
|
2007-09-07 19:13:28 +05:30
|
|
|
unsigned prevTime = monotonic_us();
|
|
|
|
|
|
|
|
pfd[0].events = POLLIN;
|
|
|
|
r = poll(pfd, 1, timeout_ms);
|
2007-07-03 21:17:50 +05:30
|
|
|
if (r < 0) {
|
2007-09-07 19:13:28 +05:30
|
|
|
if (errno != EINTR) {
|
|
|
|
bb_perror_msg("poll");
|
2007-07-03 21:17:50 +05:30
|
|
|
break;
|
2007-09-07 19:13:28 +05:30
|
|
|
}
|
2007-07-03 21:17:50 +05:30
|
|
|
} else if (r) {
|
2007-09-07 19:13:28 +05:30
|
|
|
if (read(s, &arp, sizeof(arp)) < 0)
|
2007-07-03 21:17:50 +05:30
|
|
|
break;
|
|
|
|
if (arp.operation == htons(ARPOP_REPLY)
|
|
|
|
&& memcmp(arp.tHaddr, from_mac, 6) == 0
|
|
|
|
&& *((uint32_t *) arp.sInaddr) == test_ip
|
|
|
|
) {
|
2006-05-08 08:50:50 +05:30
|
|
|
rv = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-09-07 19:13:28 +05:30
|
|
|
timeout_ms -= (monotonic_us() - prevTime) / 1000;
|
|
|
|
} while (timeout_ms > 0);
|
2007-07-03 21:17:50 +05:30
|
|
|
|
|
|
|
ret:
|
2006-05-08 08:50:50 +05:30
|
|
|
close(s);
|
2007-07-03 21:17:50 +05:30
|
|
|
DEBUG("%srp reply received for this address", rv ? "No a" : "A");
|
2006-05-08 08:50:50 +05:30
|
|
|
return rv;
|
|
|
|
}
|