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 = 500;        /* in ms */ /* rtsai modified, 06/08/2006 */    /* @XBOX */
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    /* Foxconn modified pling start 03/0/9/2006: avoid alignment problem */
72	/* *((u_int *) arp.tInaddr) = yiaddr;*/		/* target IP address */
73    memcpy(arp.tInaddr, (char *)&yiaddr, 4);
74    /* Foxconn modified pling end 03/0/9/2006 */
75
76	memset(&addr, 0, sizeof(addr));
77	strcpy(addr.sa_data, interface);
78	if (sendto(s, &arp, sizeof(arp), 0, &addr, sizeof(addr)) < 0)
79		rv = 0;
80
81	/* wait arp reply, and check it */
82	time(&prevTime);
83	while (timeout > 0) {
84		FD_ZERO(&fdset);
85		FD_SET(s, &fdset);
86		/* foxconn modified, rtsai, 06/12/2006 */   /* @XBOX */
87    	tm.tv_usec = timeout*1000;
88		tm.tv_sec = 0;
89		/* foxconn modified end, rtsai, 06/12/2006 */
90		if (select(s + 1, &fdset, (fd_set *) NULL, (fd_set *) NULL, &tm) < 0) {
91			DEBUG(LOG_ERR, "Error on ARPING request: %s", strerror(errno));
92			if (errno != EINTR) rv = 0;
93		} else if (FD_ISSET(s, &fdset)) {
94			if (recv(s, &arp, sizeof(arp), 0) < 0 ) rv = 0;
95			if (arp.operation == htons(ARPOP_REPLY) &&
96			    bcmp(arp.tHaddr, mac, 6) == 0 &&
97			    *((u_int *) arp.sInaddr) == yiaddr) {
98				DEBUG(LOG_INFO, "Valid arp reply receved for this address");
99				rv = 0;
100				break;
101			}
102		}
103		/* timeout -= (time(NULL) - prevTime) * 1000;*/  /* foxconn modified, rtsai, 06/12/2006 */ /* @XBOX */
104        timeout = 0;
105		time(&prevTime);
106	}
107	close(s);
108	DEBUG(LOG_INFO, "%salid arp replies for this address", rv ? "No v" : "V");
109	return rv;
110}
111