blob: b10bff6514e24599403f4e8fb671b41ee8d37dcc [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/*
3 * arpping.c
4 *
5 * Mostly stolen from: dhcpcd - DHCP client daemon
6 * by Yoichi Hariguchi <yoichi@fore.com>
Denis Vlasenkodb12d1d2008-12-07 00:52:58 +00007 *
8 * Licensed under GPLv2, see file LICENSE in this tarball for details.
Mike Frysinger7031f622006-05-08 03:20:50 +00009 */
10
Mike Frysinger7031f622006-05-08 03:20:50 +000011#include <netinet/if_ether.h>
12#include <net/if_arp.h>
Mike Frysinger7031f622006-05-08 03:20:50 +000013
Mike Frysinger7031f622006-05-08 03:20:50 +000014#include "common.h"
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000015#include "dhcpd.h"
16
17
18struct arpMsg {
19 /* Ethernet header */
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000020 uint8_t h_dest[6]; /* 00 destination ether addr */
21 uint8_t h_source[6]; /* 06 source ether addr */
22 uint16_t h_proto; /* 0c packet type ID field */
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000023
24 /* ARP packet */
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000025 uint16_t htype; /* 0e hardware type (must be ARPHRD_ETHER) */
26 uint16_t ptype; /* 10 protocol type (must be ETH_P_IP) */
27 uint8_t hlen; /* 12 hardware address length (must be 6) */
28 uint8_t plen; /* 13 protocol address length (must be 4) */
29 uint16_t operation; /* 14 ARP opcode */
30 uint8_t sHaddr[6]; /* 16 sender's hardware address */
31 uint8_t sInaddr[4]; /* 1c sender's IP address */
32 uint8_t tHaddr[6]; /* 20 target's hardware address */
33 uint8_t tInaddr[4]; /* 26 target's IP address */
34 uint8_t pad[18]; /* 2a pad for min. ethernet payload (60 bytes) */
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000035} PACKED;
Mike Frysinger7031f622006-05-08 03:20:50 +000036
Denis Vlasenko223bc972007-11-22 00:58:49 +000037enum {
38 ARP_MSG_SIZE = 0x2a
39};
40
Mike Frysinger7031f622006-05-08 03:20:50 +000041
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000042/* Returns 1 if no reply received */
43
Denis Vlasenkof1980f62008-09-26 09:34:59 +000044int FAST_FUNC arpping(uint32_t test_ip, uint32_t from_ip, uint8_t *from_mac, const char *interface)
Mike Frysinger7031f622006-05-08 03:20:50 +000045{
Denis Vlasenko223bc972007-11-22 00:58:49 +000046 int timeout_ms;
Denis Vlasenko87f3b262007-09-07 13:43:28 +000047 struct pollfd pfd[1];
48#define s (pfd[0].fd) /* socket */
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000049 int rv = 1; /* "no reply received" yet */
50 struct sockaddr addr; /* for interface name */
51 struct arpMsg arp;
Mike Frysinger7031f622006-05-08 03:20:50 +000052
Denis Vlasenko48237b02006-11-22 23:22:06 +000053 s = socket(PF_PACKET, SOCK_PACKET, htons(ETH_P_ARP));
54 if (s == -1) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000055 bb_perror_msg(bb_msg_can_not_create_raw_socket);
Mike Frysinger7031f622006-05-08 03:20:50 +000056 return -1;
57 }
58
Denis Vlasenko48237b02006-11-22 23:22:06 +000059 if (setsockopt_broadcast(s) == -1) {
Denis Vlasenko223bc972007-11-22 00:58:49 +000060 bb_perror_msg("cannot enable bcast on raw socket");
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000061 goto ret;
Mike Frysinger7031f622006-05-08 03:20:50 +000062 }
63
64 /* send arp request */
65 memset(&arp, 0, sizeof(arp));
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000066 memset(arp.h_dest, 0xff, 6); /* MAC DA */
67 memcpy(arp.h_source, from_mac, 6); /* MAC SA */
68 arp.h_proto = htons(ETH_P_ARP); /* protocol type (Ethernet) */
69 arp.htype = htons(ARPHRD_ETHER); /* hardware type */
70 arp.ptype = htons(ETH_P_IP); /* protocol type (ARP message) */
71 arp.hlen = 6; /* hardware address length */
72 arp.plen = 4; /* protocol address length */
73 arp.operation = htons(ARPOP_REQUEST); /* ARP op code */
74 memcpy(arp.sHaddr, from_mac, 6); /* source hardware address */
75 memcpy(arp.sInaddr, &from_ip, sizeof(from_ip)); /* source IP address */
Denis Vlasenko223bc972007-11-22 00:58:49 +000076 /* tHaddr is zero-fiiled */ /* target hardware address */
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000077 memcpy(arp.tInaddr, &test_ip, sizeof(test_ip)); /* target IP address */
Mike Frysinger7031f622006-05-08 03:20:50 +000078
79 memset(&addr, 0, sizeof(addr));
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000080 safe_strncpy(addr.sa_data, interface, sizeof(addr.sa_data));
Denis Vlasenko223bc972007-11-22 00:58:49 +000081 if (sendto(s, &arp, sizeof(arp), 0, &addr, sizeof(addr)) < 0) {
82 // TODO: error message? caller didn't expect us to fail,
83 // just returning 1 "no reply received" misleads it.
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000084 goto ret;
Denis Vlasenko223bc972007-11-22 00:58:49 +000085 }
Mike Frysinger7031f622006-05-08 03:20:50 +000086
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000087 /* wait for arp reply, and check it */
Denis Vlasenko223bc972007-11-22 00:58:49 +000088 timeout_ms = 2000;
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000089 do {
90 int r;
Denis Vlasenko87f3b262007-09-07 13:43:28 +000091 unsigned prevTime = monotonic_us();
92
93 pfd[0].events = POLLIN;
Denis Vlasenko5d61e712007-09-27 10:09:59 +000094 r = safe_poll(pfd, 1, timeout_ms);
Denis Vlasenko223bc972007-11-22 00:58:49 +000095 if (r < 0)
Denis Vlasenko5d61e712007-09-27 10:09:59 +000096 break;
Denis Vlasenko223bc972007-11-22 00:58:49 +000097 if (r) {
98 r = read(s, &arp, sizeof(arp));
99 if (r < 0)
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000100 break;
Denis Vlasenko223bc972007-11-22 00:58:49 +0000101 if (r >= ARP_MSG_SIZE
102 && arp.operation == htons(ARPOP_REPLY)
103 /* don't check it: Linux doesn't return proper tHaddr (fixed in 2.6.24?) */
104 /* && memcmp(arp.tHaddr, from_mac, 6) == 0 */
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000105 && *((uint32_t *) arp.sInaddr) == test_ip
106 ) {
Mike Frysinger7031f622006-05-08 03:20:50 +0000107 rv = 0;
108 break;
109 }
110 }
Denis Vlasenkob89e2022008-05-09 11:55:58 +0000111 timeout_ms -= ((unsigned)monotonic_us() - prevTime) / 1000;
Denis Vlasenko87f3b262007-09-07 13:43:28 +0000112 } while (timeout_ms > 0);
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000113
114 ret:
Mike Frysinger7031f622006-05-08 03:20:50 +0000115 close(s);
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000116 DEBUG("%srp reply received for this address", rv ? "No a" : "A");
Mike Frysinger7031f622006-05-08 03:20:50 +0000117 return rv;
118}