1/*
2 * arpping.c
3 *
4 * Mostly stolen from: dhcpcd - DHCP client daemon
5 * by Yoichi Hariguchi <yoichi@fore.com>
6 */
7
8#include <sys/types.h>
9#include <sys/time.h>
10#include <time.h>
11#include <sys/types.h>
12#include <sys/socket.h>
13#include <netinet/if_ether.h>
14#include <net/if_arp.h>
15#include <netinet/in.h>
16#include <stdio.h>
17#include <string.h>
18#include <unistd.h>
19#include <errno.h>
20
21#include "dhcpd.h"
22#include "debug.h"
23#include "arpping.h"
24
25/* args:	yiaddr - what IP to ping
26 *		ip - our ip
27 *		mac - our arp address
28 *		interface - interface to use
29 * retn: 	1 addr free
30 *		0 addr used
31 *		-1 error
32 */
33
34int arpping(u_int32_t yiaddr, u_int32_t ip, unsigned char *mac, char *interface)
35{
36
37	int	timeout = 2;
38	int 	optval = 1;
39	int	s;			/* socket */
40	int	rv = 1;			/* return value */
41	struct sockaddr addr;		/* for interface name */
42	struct arpMsg	arp;
43	fd_set		fdset;
44	struct timeval	tm;
45	time_t		prevTime;
46
47
48	if ((s = socket (PF_PACKET, SOCK_PACKET, htons(ETH_P_ARP))) == -1) {
49		LOG(LOG_ERR, "Could not open raw socket");
50		return -1;
51	}
52
53	if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &optval, sizeof(optval)) == -1) {
54		LOG(LOG_ERR, "Could not setsocketopt on raw socket");
55		close(s);
56		return -1;
57	}
58
59	/* send arp request */
60	memset(&arp, 0, sizeof(arp));
61	memcpy(arp.ethhdr.h_dest, MAC_BCAST_ADDR, 6);	/* MAC DA */
62	memcpy(arp.ethhdr.h_source, mac, 6);		/* MAC SA */
63	arp.ethhdr.h_proto = htons(ETH_P_ARP);		/* protocol type (Ethernet) */
64	arp.htype = htons(ARPHRD_ETHER);		/* hardware type */
65	arp.ptype = htons(ETH_P_IP);			/* protocol type (ARP message) */
66	arp.hlen = 6;					/* hardware address length */
67	arp.plen = 4;					/* protocol address length */
68	arp.operation = htons(ARPOP_REQUEST);		/* ARP op code */
69	*((u_int *) arp.sInaddr) = ip;			/* source IP address */
70	memcpy(arp.sHaddr, mac, 6);			/* source hardware address */
71	*((u_int *) arp.tInaddr) = yiaddr;		/* target IP address */
72
73	memset(&addr, 0, sizeof(addr));
74	strcpy(addr.sa_data, interface);
75	if (sendto(s, &arp, sizeof(arp), 0, &addr, sizeof(addr)) < 0)
76		rv = 0;
77
78	/* wait arp reply, and check it */
79	tm.tv_usec = 0;
80	time(&prevTime);
81	while (timeout > 0) {
82		FD_ZERO(&fdset);
83		FD_SET(s, &fdset);
84		tm.tv_sec = timeout;
85		if (select(s + 1, &fdset, (fd_set *) NULL, (fd_set *) NULL, &tm) < 0) {
86			DEBUG(LOG_ERR, "Error on ARPING request: %s", strerror(errno));
87			if (errno != EINTR) rv = 0;
88		} else if (FD_ISSET(s, &fdset)) {
89			if (recv(s, &arp, sizeof(arp), 0) < 0 ) rv = 0;
90			if (arp.operation == htons(ARPOP_REPLY) &&
91			    bcmp(arp.tHaddr, mac, 6) == 0 &&
92			    *((u_int *) arp.sInaddr) == yiaddr) {
93				DEBUG(LOG_INFO, "Valid arp reply receved for this address");
94				rv = 0;
95				break;
96			}
97		}
98		timeout -= time(NULL) - prevTime;
99		time(&prevTime);
100	}
101	close(s);
102	DEBUG(LOG_INFO, "%salid arp replies for this address", rv ? "No v" : "V");
103	return rv;
104}
105