blob: 615a91172165b0f5e6a9baaad38d43244c2e90aa [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 Vlasenko28703012006-12-19 20:32:02 +000018 uint8_t h_dest[6]; /* destination ether addr */
19 uint8_t h_source[6]; /* source ether addr */
20 uint16_t h_proto; /* packet type ID field */
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000021
22 /* ARP packet */
23 uint16_t htype; /* hardware type (must be ARPHRD_ETHER) */
24 uint16_t ptype; /* protocol type (must be ETH_P_IP) */
25 uint8_t hlen; /* hardware address length (must be 6) */
26 uint8_t plen; /* protocol address length (must be 4) */
27 uint16_t operation; /* ARP opcode */
28 uint8_t sHaddr[6]; /* sender's hardware address */
29 uint8_t sInaddr[4]; /* sender's IP address */
30 uint8_t tHaddr[6]; /* target's hardware address */
31 uint8_t tInaddr[4]; /* target's IP address */
32 uint8_t pad[18]; /* pad for min. Ethernet payload (60 bytes) */
33} ATTRIBUTE_PACKED;
Mike Frysinger7031f622006-05-08 03:20:50 +000034
35/* args: yiaddr - what IP to ping
36 * ip - our ip
37 * mac - our arp address
38 * interface - interface to use
39 * retn: 1 addr free
40 * 0 addr used
41 * -1 error
42 */
43
44/* FIXME: match response against chaddr */
45int arpping(uint32_t yiaddr, uint32_t ip, uint8_t *mac, char *interface)
46{
Mike Frysinger7031f622006-05-08 03:20:50 +000047 int timeout = 2;
Mike Frysinger7031f622006-05-08 03:20:50 +000048 int s; /* socket */
49 int rv = 1; /* return value */
50 struct sockaddr addr; /* for interface name */
51 struct arpMsg arp;
52 fd_set fdset;
53 struct timeval tm;
54 time_t prevTime;
55
56
Denis Vlasenko48237b02006-11-22 23:22:06 +000057 s = socket(PF_PACKET, SOCK_PACKET, htons(ETH_P_ARP));
58 if (s == -1) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000059 bb_perror_msg(bb_msg_can_not_create_raw_socket);
Mike Frysinger7031f622006-05-08 03:20:50 +000060 return -1;
61 }
62
Denis Vlasenko48237b02006-11-22 23:22:06 +000063 if (setsockopt_broadcast(s) == -1) {
Denis Vlasenkoea620772006-10-14 02:23:43 +000064 bb_perror_msg("cannot setsocketopt on raw socket");
Mike Frysinger7031f622006-05-08 03:20:50 +000065 close(s);
66 return -1;
67 }
68
69 /* send arp request */
70 memset(&arp, 0, sizeof(arp));
71 memcpy(arp.h_dest, MAC_BCAST_ADDR, 6); /* MAC DA */
72 memcpy(arp.h_source, mac, 6); /* MAC SA */
73 arp.h_proto = htons(ETH_P_ARP); /* protocol type (Ethernet) */
74 arp.htype = htons(ARPHRD_ETHER); /* hardware type */
75 arp.ptype = htons(ETH_P_IP); /* protocol type (ARP message) */
76 arp.hlen = 6; /* hardware address length */
77 arp.plen = 4; /* protocol address length */
78 arp.operation = htons(ARPOP_REQUEST); /* ARP op code */
79 memcpy(arp.sInaddr, &ip, sizeof(ip)); /* source IP address */
80 memcpy(arp.sHaddr, mac, 6); /* source hardware address */
81 memcpy(arp.tInaddr, &yiaddr, sizeof(yiaddr)); /* target IP address */
82
83 memset(&addr, 0, sizeof(addr));
84 strcpy(addr.sa_data, interface);
85 if (sendto(s, &arp, sizeof(arp), 0, &addr, sizeof(addr)) < 0)
86 rv = 0;
87
88 /* wait arp reply, and check it */
89 tm.tv_usec = 0;
90 prevTime = uptime();
91 while (timeout > 0) {
92 FD_ZERO(&fdset);
93 FD_SET(s, &fdset);
94 tm.tv_sec = timeout;
95 if (select(s + 1, &fdset, (fd_set *) NULL, (fd_set *) NULL, &tm) < 0) {
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +000096 bb_perror_msg("error on ARPING request");
Denis Vlasenko219d14d2007-03-24 15:40:16 +000097 if (errno != EINTR)
98 rv = 0;
Mike Frysinger7031f622006-05-08 03:20:50 +000099 } else if (FD_ISSET(s, &fdset)) {
Denis Vlasenko219d14d2007-03-24 15:40:16 +0000100 if (recv(s, &arp, sizeof(arp), 0) < 0)
101 rv = 0;
Mike Frysinger7031f622006-05-08 03:20:50 +0000102 if (arp.operation == htons(ARPOP_REPLY) &&
103 memcmp(arp.tHaddr, mac, 6) == 0 &&
104 *((uint32_t *) arp.sInaddr) == yiaddr) {
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000105 DEBUG("Valid arp reply received for this address");
Mike Frysinger7031f622006-05-08 03:20:50 +0000106 rv = 0;
107 break;
108 }
109 }
110 timeout -= uptime() - prevTime;
111 prevTime = uptime();
112 }
113 close(s);
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000114 DEBUG("%salid arp replies for this address", rv ? "No v" : "V");
Mike Frysinger7031f622006-05-08 03:20:50 +0000115 return rv;
116}