blob: 7b702d8f38427324d0665b20961d744b08aaa902 [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>
7 */
8
Mike Frysinger7031f622006-05-08 03:20:50 +00009#include <netinet/if_ether.h>
10#include <net/if_arp.h>
Mike Frysinger7031f622006-05-08 03:20:50 +000011
Mike Frysinger7031f622006-05-08 03:20:50 +000012#include "common.h"
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000013#include "dhcpd.h"
14
15
16struct arpMsg {
17 /* Ethernet header */
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000018 uint8_t h_dest[6]; /* 00 destination ether addr */
19 uint8_t h_source[6]; /* 06 source ether addr */
20 uint16_t h_proto; /* 0c packet type ID field */
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000021
22 /* ARP packet */
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000023 uint16_t htype; /* 0e hardware type (must be ARPHRD_ETHER) */
24 uint16_t ptype; /* 10 protocol type (must be ETH_P_IP) */
25 uint8_t hlen; /* 12 hardware address length (must be 6) */
26 uint8_t plen; /* 13 protocol address length (must be 4) */
27 uint16_t operation; /* 14 ARP opcode */
28 uint8_t sHaddr[6]; /* 16 sender's hardware address */
29 uint8_t sInaddr[4]; /* 1c sender's IP address */
30 uint8_t tHaddr[6]; /* 20 target's hardware address */
31 uint8_t tInaddr[4]; /* 26 target's IP address */
32 uint8_t pad[18]; /* 2a pad for min. ethernet payload (60 bytes) */
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000033} ATTRIBUTE_PACKED;
Mike Frysinger7031f622006-05-08 03:20:50 +000034
Mike Frysinger7031f622006-05-08 03:20:50 +000035
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000036/* Returns 1 if no reply received */
37
38int arpping(uint32_t test_ip, uint32_t from_ip, uint8_t *from_mac, const char *interface)
Mike Frysinger7031f622006-05-08 03:20:50 +000039{
Denis Vlasenko87f3b262007-09-07 13:43:28 +000040 int timeout_ms = 2000;
41 struct pollfd pfd[1];
42#define s (pfd[0].fd) /* socket */
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000043 int rv = 1; /* "no reply received" yet */
44 struct sockaddr addr; /* for interface name */
45 struct arpMsg arp;
Mike Frysinger7031f622006-05-08 03:20:50 +000046
Denis Vlasenko48237b02006-11-22 23:22:06 +000047 s = socket(PF_PACKET, SOCK_PACKET, htons(ETH_P_ARP));
48 if (s == -1) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000049 bb_perror_msg(bb_msg_can_not_create_raw_socket);
Mike Frysinger7031f622006-05-08 03:20:50 +000050 return -1;
51 }
52
Denis Vlasenko48237b02006-11-22 23:22:06 +000053 if (setsockopt_broadcast(s) == -1) {
Denis Vlasenkoea620772006-10-14 02:23:43 +000054 bb_perror_msg("cannot setsocketopt on raw socket");
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000055 goto ret;
Mike Frysinger7031f622006-05-08 03:20:50 +000056 }
57
58 /* send arp request */
59 memset(&arp, 0, sizeof(arp));
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000060 memset(arp.h_dest, 0xff, 6); /* MAC DA */
61 memcpy(arp.h_source, from_mac, 6); /* MAC SA */
62 arp.h_proto = htons(ETH_P_ARP); /* protocol type (Ethernet) */
63 arp.htype = htons(ARPHRD_ETHER); /* hardware type */
64 arp.ptype = htons(ETH_P_IP); /* protocol type (ARP message) */
65 arp.hlen = 6; /* hardware address length */
66 arp.plen = 4; /* protocol address length */
67 arp.operation = htons(ARPOP_REQUEST); /* ARP op code */
68 memcpy(arp.sHaddr, from_mac, 6); /* source hardware address */
69 memcpy(arp.sInaddr, &from_ip, sizeof(from_ip)); /* source IP address */
70 /* tHaddr */ /* target hardware address */
71 memcpy(arp.tInaddr, &test_ip, sizeof(test_ip)); /* target IP address */
Mike Frysinger7031f622006-05-08 03:20:50 +000072
73 memset(&addr, 0, sizeof(addr));
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000074 safe_strncpy(addr.sa_data, interface, sizeof(addr.sa_data));
Mike Frysinger7031f622006-05-08 03:20:50 +000075 if (sendto(s, &arp, sizeof(arp), 0, &addr, sizeof(addr)) < 0)
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000076 goto ret;
Mike Frysinger7031f622006-05-08 03:20:50 +000077
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000078 /* wait for arp reply, and check it */
79 do {
80 int r;
Denis Vlasenko87f3b262007-09-07 13:43:28 +000081 unsigned prevTime = monotonic_us();
82
83 pfd[0].events = POLLIN;
Denis Vlasenko5d61e712007-09-27 10:09:59 +000084 r = safe_poll(pfd, 1, timeout_ms);
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000085 if (r < 0) {
Denis Vlasenko5d61e712007-09-27 10:09:59 +000086 break;
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000087 } else if (r) {
Denis Vlasenko87f3b262007-09-07 13:43:28 +000088 if (read(s, &arp, sizeof(arp)) < 0)
Denis Vlasenko42b3dea2007-07-03 15:47:50 +000089 break;
90 if (arp.operation == htons(ARPOP_REPLY)
91 && memcmp(arp.tHaddr, from_mac, 6) == 0
92 && *((uint32_t *) arp.sInaddr) == test_ip
93 ) {
Mike Frysinger7031f622006-05-08 03:20:50 +000094 rv = 0;
95 break;
96 }
97 }
Denis Vlasenko87f3b262007-09-07 13:43:28 +000098 timeout_ms -= (monotonic_us() - prevTime) / 1000;
99 } while (timeout_ms > 0);
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000100
101 ret:
Mike Frysinger7031f622006-05-08 03:20:50 +0000102 close(s);
Denis Vlasenko42b3dea2007-07-03 15:47:50 +0000103 DEBUG("%srp reply received for this address", rv ? "No a" : "A");
Mike Frysinger7031f622006-05-08 03:20:50 +0000104 return rv;
105}