forked from pandax381/microps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
arp.c
340 lines (304 loc) · 9.87 KB
/
arp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <sys/time.h>
#include "platform.h"
#include "util.h"
#include "net.h"
#include "ether.h"
#include "arp.h"
#include "ip.h"
/* see https://www.iana.org/assignments/arp-parameters/arp-parameters.txt */
#define ARP_HRD_ETHER 0x0001
/* NOTE: use same value as the Ethernet types */
#define ARP_PRO_IP ETHER_TYPE_IP
#define ARP_OP_REQUEST 0x0001
#define ARP_OP_REPLY 0x0002
#define ARP_CACHE_SIZE 32
#define ARP_CACHE_TIMEOUT 30 /* seconds */
#define ARP_CACHE_STATE_FREE 0
#define ARP_CACHE_STATE_INCOMPLETE 1
#define ARP_CACHE_STATE_RESOLVED 2
#define ARP_CACHE_STATE_STATIC 3
struct arp_hdr {
uint16_t hrd;
uint16_t pro;
uint8_t hln;
uint8_t pln;
uint16_t op;
};
struct arp_ether {
struct arp_hdr hdr;
uint8_t sha[ETHER_ADDR_LEN];
uint8_t spa[IP_ADDR_LEN];
uint8_t tha[ETHER_ADDR_LEN];
uint8_t tpa[IP_ADDR_LEN];
};
struct arp_cache {
unsigned char state;
ip_addr_t pa;
uint8_t ha[ETHER_ADDR_LEN];
struct timeval timestamp;
};
static mutex_t mutex = MUTEX_INITIALIZER;
static struct arp_cache caches[ARP_CACHE_SIZE];
static char *
arp_opcode_ntoa(uint16_t opcode)
{
switch (ntoh16(opcode)) {
case ARP_OP_REQUEST:
return "Request";
case ARP_OP_REPLY:
return "Reply";
}
return "Unknown";
}
static void
arp_dump(const uint8_t *data, size_t len)
{
struct arp_ether *message;
ip_addr_t spa, tpa;
char addr[128];
message = (struct arp_ether *)data;
flockfile(stderr);
fprintf(stderr, " hrd: 0x%04x\n", ntoh16(message->hdr.hrd));
fprintf(stderr, " pro: 0x%04x\n", ntoh16(message->hdr.pro));
fprintf(stderr, " hln: %u\n", message->hdr.hln);
fprintf(stderr, " pln: %u\n", message->hdr.pln);
fprintf(stderr, " op: 0x%04x (%s)\n", ntoh16(message->hdr.op), arp_opcode_ntoa(message->hdr.op));
fprintf(stderr, " sha: %s\n", ether_addr_ntop(message->sha, addr, sizeof(addr)));
memcpy(&spa, message->spa, sizeof(spa));
fprintf(stderr, " spa: %s\n", ip_addr_ntop(spa, addr, sizeof(addr)));
fprintf(stderr, " tha: %s\n", ether_addr_ntop(message->tha, addr, sizeof(addr)));
memcpy(&tpa, message->tpa, sizeof(tpa));
fprintf(stderr, " tpa: %s\n", ip_addr_ntop(tpa, addr, sizeof(addr)));
#ifdef HEXDUMP
hexdump(stderr, data, len);
#endif
funlockfile(stderr);
}
/*
* ARP Cache
*
* NOTE: ARP Cache functions must be called after mutex locked
*/
static struct arp_cache *
arp_cache_alloc(void)
{
struct arp_cache *entry, *oldest = NULL;
for (entry = caches; entry < tailof(caches); entry++) {
if (entry->state == ARP_CACHE_STATE_FREE) {
return entry;
}
if (!oldest || timercmp(&oldest->timestamp, &entry->timestamp, >)) {
oldest = entry;
}
}
return oldest;
}
static struct arp_cache *
arp_cache_select(ip_addr_t pa)
{
struct arp_cache *entry;
for (entry = caches; entry < tailof(caches); entry++) {
if (entry->state != ARP_CACHE_STATE_FREE && entry->pa == pa) {
return entry;
}
}
return NULL;
}
static struct arp_cache *
arp_cache_update(ip_addr_t pa, const uint8_t *ha)
{
struct arp_cache *cache;
char addr1[IP_ADDR_STR_LEN];
char addr2[ETHER_ADDR_STR_LEN];
cache = arp_cache_select(pa);
if (!cache) {
/* not found */
return NULL;
}
cache->state = ARP_CACHE_STATE_RESOLVED;
memcpy(cache->ha, ha, ETHER_ADDR_LEN);
gettimeofday(&cache->timestamp, NULL);
debugf("UPDATE: pa=%s, ha=%s", ip_addr_ntop(pa, addr1, sizeof(addr1)), ether_addr_ntop(ha, addr2, sizeof(addr2)));
return cache;
}
static struct arp_cache *
arp_cache_insert(ip_addr_t pa, const uint8_t *ha)
{
struct arp_cache *cache;
char addr1[IP_ADDR_STR_LEN];
char addr2[ETHER_ADDR_STR_LEN];
cache = arp_cache_alloc();
if (!cache) {
errorf("arp_cache_alloc() failure");
return NULL;
}
cache->state = ARP_CACHE_STATE_RESOLVED;
cache->pa = pa;
memcpy(cache->ha, ha, ETHER_ADDR_LEN);
gettimeofday(&cache->timestamp, NULL);
debugf("INSERT: pa=%s, ha=%s", ip_addr_ntop(pa, addr1, sizeof(addr1)), ether_addr_ntop(ha, addr2, sizeof(addr2)));
return cache;
}
static void
arp_cache_delete(struct arp_cache *cache)
{
char addr1[IP_ADDR_STR_LEN];
char addr2[ETHER_ADDR_STR_LEN];
debugf("DELETE: pa=%s, ha=%s", ip_addr_ntop(cache->pa, addr1, sizeof(addr1)), ether_addr_ntop(cache->ha, addr2, sizeof(addr2)));
cache->state = ARP_CACHE_STATE_FREE;
cache->pa = 0;
memset(cache->ha, 0, ETHER_ADDR_LEN);
timerclear(&cache->timestamp);
}
static int
arp_request(struct net_iface *iface, ip_addr_t tpa)
{
struct arp_ether request;
request.hdr.hrd = hton16(ARP_HRD_ETHER);
request.hdr.pro = hton16(ARP_PRO_IP);
request.hdr.hln = ETHER_ADDR_LEN;
request.hdr.pln = IP_ADDR_LEN;
request.hdr.op = hton16(ARP_OP_REQUEST);
memcpy(request.sha, iface->dev->addr, ETHER_ADDR_LEN);
memcpy(request.spa, &((struct ip_iface *)iface)->unicast, IP_ADDR_LEN);
memset(request.tha, 0, ETHER_ADDR_LEN);
memcpy(request.tpa, &tpa, IP_ADDR_LEN);
debugf("dev=%s, opcode=%s(0x%04x), len=%zu", iface->dev->name, arp_opcode_ntoa(request.hdr.op), ntoh16(request.hdr.op), sizeof(request));
arp_dump((uint8_t *)&request, sizeof(request));
return net_device_output(iface->dev, ETHER_TYPE_ARP, (uint8_t *)&request, sizeof(request), iface->dev->broadcast);
}
static int
arp_reply(struct net_iface *iface, const uint8_t *tha, ip_addr_t tpa, const uint8_t *dst)
{
struct arp_ether reply;
reply.hdr.hrd = hton16(ARP_HRD_ETHER);
reply.hdr.pro = hton16(ARP_PRO_IP);
reply.hdr.hln = ETHER_ADDR_LEN;
reply.hdr.pln = IP_ADDR_LEN;
reply.hdr.op = hton16(ARP_OP_REPLY);
memcpy(reply.sha, iface->dev->addr, ETHER_ADDR_LEN);
memcpy(reply.spa, &((struct ip_iface *)iface)->unicast, IP_ADDR_LEN);
memcpy(reply.tha, tha, ETHER_ADDR_LEN);
memcpy(reply.tpa, &tpa, IP_ADDR_LEN);
debugf("dev=%s, opcode=%s(0x%04x), len=%zu", iface->dev->name, arp_opcode_ntoa(reply.hdr.op), ntoh16(reply.hdr.op), sizeof(reply));
arp_dump((uint8_t *)&reply, sizeof(reply));
return net_device_output(iface->dev, ETHER_TYPE_ARP, (uint8_t *)&reply, sizeof(reply), dst);
}
static void
arp_input(const uint8_t *data, size_t len, struct net_device *dev)
{
struct arp_ether *msg;
ip_addr_t spa, tpa;
int merge = 0;
struct net_iface *iface;
if (len < sizeof(*msg)) {
errorf("too short");
return;
}
msg = (struct arp_ether *)data;
if (ntoh16(msg->hdr.hrd) != ARP_HRD_ETHER || msg->hdr.hln != ETHER_ADDR_LEN) {
errorf("unsupported hardware address");
return;
}
if (ntoh16(msg->hdr.pro) != ARP_PRO_IP || msg->hdr.pln != IP_ADDR_LEN) {
errorf("unsupported protocol address");
return;
}
debugf("dev=%s, opcode=%s(0x%04x), len=%zu", dev->name, arp_opcode_ntoa(msg->hdr.op), ntoh16(msg->hdr.op), len);
arp_dump(data, len);
memcpy(&spa, msg->spa, sizeof(spa));
memcpy(&tpa, msg->tpa, sizeof(tpa));
mutex_lock(&mutex);
if (arp_cache_update(spa, msg->sha)) {
/* updated */
merge = 1;
}
mutex_unlock(&mutex);
iface = net_device_get_iface(dev, NET_IFACE_FAMILY_IP);
if (iface && ((struct ip_iface *)iface)->unicast == tpa) {
if (!merge) {
mutex_lock(&mutex);
arp_cache_insert(spa, msg->sha);
mutex_unlock(&mutex);
}
if (ntoh16(msg->hdr.op) == ARP_OP_REQUEST) {
arp_reply(iface, msg->sha, spa, msg->sha);
}
}
}
int
arp_resolve(struct net_iface *iface, ip_addr_t pa, uint8_t *ha)
{
struct arp_cache *cache;
char addr1[IP_ADDR_STR_LEN];
char addr2[ETHER_ADDR_STR_LEN];
if (iface->dev->type != NET_DEVICE_TYPE_ETHERNET) {
debugf("unsupported hardware address type");
return ARP_RESOLVE_ERROR;
}
if (iface->family != NET_IFACE_FAMILY_IP) {
debugf("unsupported protocol address type");
return ARP_RESOLVE_ERROR;
}
mutex_lock(&mutex);
cache = arp_cache_select(pa);
if (!cache) {
cache = arp_cache_alloc();
if (!cache) {
mutex_unlock(&mutex);
errorf("arp_cache_alloc() failure");
return ARP_RESOLVE_ERROR;
}
cache->state = ARP_CACHE_STATE_INCOMPLETE;
cache->pa = pa;
gettimeofday(&cache->timestamp, NULL);
arp_request(iface, pa);
mutex_unlock(&mutex);
debugf("cache not found, pa=%s", ip_addr_ntop(pa, addr1, sizeof(addr1)));
return ARP_RESOLVE_INCOMPLETE;
}
if (cache->state == ARP_CACHE_STATE_INCOMPLETE) {
arp_request(iface, pa); /* just in case packet loss */
mutex_unlock(&mutex);
return ARP_RESOLVE_INCOMPLETE;
}
memcpy(ha, cache->ha, ETHER_ADDR_LEN);
mutex_unlock(&mutex);
debugf("resolved, pa=%s, ha=%s",
ip_addr_ntop(pa, addr1, sizeof(addr1)), ether_addr_ntop(ha, addr2, sizeof(addr2)));
return ARP_RESOLVE_FOUND;
}
static void
arp_timer(void)
{
struct arp_cache *entry;
struct timeval now, diff;
mutex_lock(&mutex);
gettimeofday(&now, NULL);
for (entry = caches; entry < tailof(caches); entry++) {
if (entry->state != ARP_CACHE_STATE_FREE && entry->state != ARP_CACHE_STATE_STATIC) {
timersub(&now, &entry->timestamp, &diff);
if (diff.tv_sec > ARP_CACHE_TIMEOUT) {
arp_cache_delete(entry);
}
}
}
mutex_unlock(&mutex);
}
int
arp_init(void)
{
struct timeval interval = {1, 0};
if (net_protocol_register("ARP", NET_PROTOCOL_TYPE_ARP, arp_input) == -1) {
errorf("net_protocol_register() failure");
return -1;
}
if (net_timer_register("ARP Timer", interval, arp_timer) == -1) {
errorf("net_timer_register() failure");
return -1;
}
return 0;
}