blob: 4831d4371147498c41e282263e3ea7d745de3f07 [file] [log] [blame]
Russ Dill61fb4892002-10-14 21:41:28 +00001/*
2 * arpping.c
3 *
4 * Mostly stolen from: dhcpcd - DHCP client daemon
5 * by Yoichi Hariguchi <yoichi@fore.com>
6 */
7
Russ Dill61fb4892002-10-14 21:41:28 +00008#include <sys/time.h>
9#include <time.h>
Russ Dill61fb4892002-10-14 21:41:28 +000010#include <sys/socket.h>
11#include <netinet/if_ether.h>
12#include <net/if_arp.h>
13#include <netinet/in.h>
Russ Dill61fb4892002-10-14 21:41:28 +000014#include <string.h>
15#include <unistd.h>
16#include <errno.h>
17
18#include "dhcpd.h"
Russ Dill61fb4892002-10-14 21:41:28 +000019#include "arpping.h"
Glenn L McGrath24833432003-06-10 17:22:49 +000020#include "common.h"
Russ Dill61fb4892002-10-14 21:41:28 +000021
22/* args: yiaddr - what IP to ping
23 * ip - our ip
24 * mac - our arp address
25 * interface - interface to use
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000026 * retn: 1 addr free
Russ Dill61fb4892002-10-14 21:41:28 +000027 * 0 addr used
Eric Andersenc7bda1c2004-03-15 08:29:22 +000028 * -1 error
29 */
Russ Dill61fb4892002-10-14 21:41:28 +000030
31/* FIXME: match response against chaddr */
Eric Andersenad953732004-01-30 23:45:53 +000032int arpping(uint32_t yiaddr, uint32_t ip, uint8_t *mac, char *interface)
Russ Dill61fb4892002-10-14 21:41:28 +000033{
34
35 int timeout = 2;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000036 int optval = 1;
Russ Dill61fb4892002-10-14 21:41:28 +000037 int s; /* socket */
38 int rv = 1; /* return value */
39 struct sockaddr addr; /* for interface name */
40 struct arpMsg arp;
41 fd_set fdset;
42 struct timeval tm;
43 time_t prevTime;
44
45
46 if ((s = socket (PF_PACKET, SOCK_PACKET, htons(ETH_P_ARP))) == -1) {
Russ Dill4e864a32003-12-18 22:25:38 +000047#ifdef IN_BUSYBOX
Glenn L McGrath24833432003-06-10 17:22:49 +000048 LOG(LOG_ERR, bb_msg_can_not_create_raw_socket);
Russ Dill4e864a32003-12-18 22:25:38 +000049#else
50 LOG(LOG_ERR, "Could not open raw socket");
51#endif
Russ Dill61fb4892002-10-14 21:41:28 +000052 return -1;
53 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +000054
Russ Dill61fb4892002-10-14 21:41:28 +000055 if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &optval, sizeof(optval)) == -1) {
56 LOG(LOG_ERR, "Could not setsocketopt on raw socket");
57 close(s);
58 return -1;
59 }
60
61 /* send arp request */
62 memset(&arp, 0, sizeof(arp));
Glenn L McGrath3f793002004-03-05 14:25:49 +000063 memcpy(arp.h_dest, MAC_BCAST_ADDR, 6); /* MAC DA */
64 memcpy(arp.h_source, mac, 6); /* MAC SA */
65 arp.h_proto = htons(ETH_P_ARP); /* protocol type (Ethernet) */
Russ Dill61fb4892002-10-14 21:41:28 +000066 arp.htype = htons(ARPHRD_ETHER); /* hardware type */
67 arp.ptype = htons(ETH_P_IP); /* protocol type (ARP message) */
68 arp.hlen = 6; /* hardware address length */
69 arp.plen = 4; /* protocol address length */
70 arp.operation = htons(ARPOP_REQUEST); /* ARP op code */
Russ Dill858fad72003-02-12 22:20:19 +000071 memcpy(arp.sInaddr, &ip, sizeof(ip)); /* source IP address */
Russ Dill61fb4892002-10-14 21:41:28 +000072 memcpy(arp.sHaddr, mac, 6); /* source hardware address */
Russ Dill858fad72003-02-12 22:20:19 +000073 memcpy(arp.tInaddr, &yiaddr, sizeof(yiaddr)); /* target IP address */
Eric Andersenc7bda1c2004-03-15 08:29:22 +000074
Russ Dill61fb4892002-10-14 21:41:28 +000075 memset(&addr, 0, sizeof(addr));
76 strcpy(addr.sa_data, interface);
77 if (sendto(s, &arp, sizeof(arp), 0, &addr, sizeof(addr)) < 0)
78 rv = 0;
Eric Andersenc7bda1c2004-03-15 08:29:22 +000079
Russ Dill61fb4892002-10-14 21:41:28 +000080 /* wait arp reply, and check it */
81 tm.tv_usec = 0;
Rob Landley918f2ab2005-05-04 02:15:23 +000082 prevTime = uptime();
Russ Dill61fb4892002-10-14 21:41:28 +000083 while (timeout > 0) {
84 FD_ZERO(&fdset);
85 FD_SET(s, &fdset);
86 tm.tv_sec = timeout;
87 if (select(s + 1, &fdset, (fd_set *) NULL, (fd_set *) NULL, &tm) < 0) {
Glenn L McGrath24833432003-06-10 17:22:49 +000088 DEBUG(LOG_ERR, "Error on ARPING request: %m");
Russ Dill61fb4892002-10-14 21:41:28 +000089 if (errno != EINTR) rv = 0;
90 } else if (FD_ISSET(s, &fdset)) {
91 if (recv(s, &arp, sizeof(arp), 0) < 0 ) rv = 0;
Eric Andersenc7bda1c2004-03-15 08:29:22 +000092 if (arp.operation == htons(ARPOP_REPLY) &&
93 bcmp(arp.tHaddr, mac, 6) == 0 &&
Eric Andersenad953732004-01-30 23:45:53 +000094 *((uint32_t *) arp.sInaddr) == yiaddr) {
Russ Dill61fb4892002-10-14 21:41:28 +000095 DEBUG(LOG_INFO, "Valid arp reply receved for this address");
96 rv = 0;
97 break;
98 }
99 }
Rob Landley918f2ab2005-05-04 02:15:23 +0000100 timeout -= uptime() - prevTime;
101 prevTime = uptime();
Russ Dill61fb4892002-10-14 21:41:28 +0000102 }
103 close(s);
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000104 DEBUG(LOG_INFO, "%salid arp replies for this address", rv ? "No v" : "V");
Russ Dill61fb4892002-10-14 21:41:28 +0000105 return rv;
106}