Initial public busybox upstream commit
[busybox4maemo] / networking / udhcp / arpping.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * arpping.c
4  *
5  * Mostly stolen from: dhcpcd - DHCP client daemon
6  * by Yoichi Hariguchi <yoichi@fore.com>
7  */
8
9 #include <netinet/if_ether.h>
10 #include <net/if_arp.h>
11
12 #include "common.h"
13 #include "dhcpd.h"
14
15
16 struct arpMsg {
17         /* Ethernet header */
18         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 */
21
22         /* ARP packet */
23         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) */
33 } ATTRIBUTE_PACKED;
34
35 enum {
36         ARP_MSG_SIZE = 0x2a
37 };
38
39
40 /* Returns 1 if no reply received */
41
42 int arpping(uint32_t test_ip, uint32_t from_ip, uint8_t *from_mac, const char *interface)
43 {
44         int timeout_ms;
45         struct pollfd pfd[1];
46 #define s (pfd[0].fd)           /* socket */
47         int rv = 1;             /* "no reply received" yet */
48         struct sockaddr addr;   /* for interface name */
49         struct arpMsg arp;
50
51         s = socket(PF_PACKET, SOCK_PACKET, htons(ETH_P_ARP));
52         if (s == -1) {
53                 bb_perror_msg(bb_msg_can_not_create_raw_socket);
54                 return -1;
55         }
56
57         if (setsockopt_broadcast(s) == -1) {
58                 bb_perror_msg("cannot enable bcast on raw socket");
59                 goto ret;
60         }
61
62         /* send arp request */
63         memset(&arp, 0, sizeof(arp));
64         memset(arp.h_dest, 0xff, 6);                    /* MAC DA */
65         memcpy(arp.h_source, from_mac, 6);              /* MAC SA */
66         arp.h_proto = htons(ETH_P_ARP);                 /* protocol type (Ethernet) */
67         arp.htype = htons(ARPHRD_ETHER);                /* hardware type */
68         arp.ptype = htons(ETH_P_IP);                    /* protocol type (ARP message) */
69         arp.hlen = 6;                                   /* hardware address length */
70         arp.plen = 4;                                   /* protocol address length */
71         arp.operation = htons(ARPOP_REQUEST);           /* ARP op code */
72         memcpy(arp.sHaddr, from_mac, 6);                /* source hardware address */
73         memcpy(arp.sInaddr, &from_ip, sizeof(from_ip)); /* source IP address */
74         /* tHaddr is zero-fiiled */                     /* target hardware address */
75         memcpy(arp.tInaddr, &test_ip, sizeof(test_ip)); /* target IP address */
76
77         memset(&addr, 0, sizeof(addr));
78         safe_strncpy(addr.sa_data, interface, sizeof(addr.sa_data));
79         if (sendto(s, &arp, sizeof(arp), 0, &addr, sizeof(addr)) < 0) {
80                 // TODO: error message? caller didn't expect us to fail,
81                 // just returning 1 "no reply received" misleads it.
82                 goto ret;
83         }
84
85         /* wait for arp reply, and check it */
86         timeout_ms = 2000;
87         do {
88                 int r;
89                 unsigned prevTime = monotonic_us();
90
91                 pfd[0].events = POLLIN;
92                 r = safe_poll(pfd, 1, timeout_ms);
93                 if (r < 0)
94                         break;
95                 if (r) {
96                         r = read(s, &arp, sizeof(arp));
97                         if (r < 0)
98                                 break;
99                         if (r >= ARP_MSG_SIZE
100                          && arp.operation == htons(ARPOP_REPLY)
101                          /* don't check it: Linux doesn't return proper tHaddr (fixed in 2.6.24?) */
102                          /* && memcmp(arp.tHaddr, from_mac, 6) == 0 */
103                          && *((uint32_t *) arp.sInaddr) == test_ip
104                         ) {
105                                 rv = 0;
106                                 break;
107                         }
108                 }
109                 timeout_ms -= (monotonic_us() - prevTime) / 1000;
110         } while (timeout_ms > 0);
111
112  ret:
113         close(s);
114         DEBUG("%srp reply received for this address", rv ? "No a" : "A");
115         return rv;
116 }