forked from mit-pdos/xv6-public
-
Notifications
You must be signed in to change notification settings - Fork 14
/
arp.c
320 lines (288 loc) · 8.26 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
// Copyright (c) 2012-2020 YAMAMOTO Masaya
// SPDX-License-Identifier: MIT
#include "types.h"
#include "defs.h"
#include "spinlock.h"
#include "net.h"
#include "ethernet.h"
#include "arp.h"
#include "ip.h"
#define ARP_HRD_ETHERNET 0x0001
#define ARP_OP_REQUEST 1
#define ARP_OP_REPLY 2
#define ARP_TABLE_SIZE 4096
#define ARP_TABLE_TIMEOUT_SEC 300
struct arp_hdr {
uint16_t hrd;
uint16_t pro;
uint8_t hln;
uint8_t pln;
uint16_t op;
};
struct arp_ethernet {
struct arp_hdr hdr;
uint8_t sha[ETHERNET_ADDR_LEN];
ip_addr_t spa;
uint8_t tha[ETHERNET_ADDR_LEN];
ip_addr_t tpa;
} __attribute__ ((packed));
struct arp_entry {
unsigned char used;
ip_addr_t pa;
uint8_t ha[ETHERNET_ADDR_LEN];
time_t timestamp;
void *data;
size_t len;
struct netif *netif;
};
static struct spinlock arplock;
static struct arp_entry arp_table[ARP_TABLE_SIZE];
static time_t timestamp;
static char *
arp_opcode_ntop (uint16_t opcode) {
switch (ntoh16(opcode)) {
case ARP_OP_REQUEST:
return "REQUEST";
case ARP_OP_REPLY:
return "REPLY";
}
return "UNKNOWN";
}
void
arp_dump (uint8_t *packet, size_t plen) {
struct arp_ethernet *message;
char addr[128];
message = (struct arp_ethernet *)packet;
cprintf(" hrd: 0x%04x\n", ntoh16(message->hdr.hrd));
cprintf(" pro: 0x%04x\n", ntoh16(message->hdr.pro));
cprintf(" hln: %u\n", message->hdr.hln);
cprintf(" pln: %u\n", message->hdr.pln);
cprintf(" op: %u (%s)\n", ntoh16(message->hdr.op), arp_opcode_ntop(message->hdr.op));
cprintf(" sha: %s\n", ethernet_addr_ntop(message->sha, addr, sizeof(addr)));
cprintf(" spa: %s\n", ip_addr_ntop(&message->spa, addr, sizeof(addr)));
cprintf(" tha: %s\n", ethernet_addr_ntop(message->tha, addr, sizeof(addr)));
cprintf(" tpa: %s\n", ip_addr_ntop(&message->tpa, addr, sizeof(addr)));
}
static struct arp_entry *
arp_table_select (const ip_addr_t *pa) {
struct arp_entry *entry;
for (entry = arp_table; entry < array_tailof(arp_table); entry++) {
if (entry->used && entry->pa == *pa) {
return entry;
}
}
return NULL;
}
static int
arp_table_update (struct netdev *dev, const ip_addr_t *pa, const uint8_t *ha) {
struct arp_entry *entry;
entry = arp_table_select(pa);
if (!entry) {
return -1;
}
memcpy(entry->ha, ha, ETHERNET_ADDR_LEN);
time(&entry->timestamp);
if (entry->data) {
if (entry->netif->dev != dev) {
/* warning: receive response from unintended device */
dev = entry->netif->dev;
}
dev->ops->xmit(dev, ETHERNET_TYPE_IP, (uint8_t *)entry->data, entry->len, entry->ha);
kfree(entry->data);
entry->data = NULL;
entry->len = 0;
}
//pthread_cond_broadcast(&entry->cond);
return 0;
}
static struct arp_entry *
arp_table_freespace (void) {
struct arp_entry *entry;
for (entry = arp_table; entry < array_tailof(arp_table); entry++) {
if (!entry->used) {
return entry;
}
}
return NULL;
}
static int
arp_table_insert (const ip_addr_t *pa, const uint8_t *ha) {
struct arp_entry *entry;
entry = arp_table_freespace();
if (!entry) {
return -1;
}
entry->used = 1;
entry->pa = *pa;
memcpy(entry->ha, ha, ETHERNET_ADDR_LEN);
time(&entry->timestamp);
//pthread_cond_broadcast(&entry->cond);
return 0;
}
static void
arp_entry_clear (struct arp_entry *entry) {
entry->used = 0;
entry->pa = 0;
memset(entry->ha, 0, ETHERNET_ADDR_LEN);
//entry->timestamp = 0;
if (entry->data) {
kfree(entry->data);
entry->data = NULL;
entry->len = 0;
}
entry->netif = NULL;
/* !!! Don't touch entry->cond !!! */
}
static void
arp_table_patrol (void) {
struct arp_entry *entry;
for (entry = arp_table; entry < array_tailof(arp_table); entry++) {
if (entry->used && timestamp - entry->timestamp > ARP_TABLE_TIMEOUT_SEC) {
arp_entry_clear(entry);
//pthread_cond_broadcast(&entry->cond);
}
}
}
static int
arp_send_request (struct netif *netif, const ip_addr_t *tpa) {
struct arp_ethernet request;
if (!tpa) {
return -1;
}
request.hdr.hrd = hton16(ARP_HRD_ETHERNET);
request.hdr.pro = hton16(ETHERNET_TYPE_IP);
request.hdr.hln = ETHERNET_ADDR_LEN;
request.hdr.pln = IP_ADDR_LEN;
request.hdr.op = hton16(ARP_OP_REQUEST);
memcpy(request.sha, netif->dev->addr, ETHERNET_ADDR_LEN);
request.spa = ((struct netif_ip *)netif)->unicast;
memset(request.tha, 0, ETHERNET_ADDR_LEN);
request.tpa = *tpa;
#ifdef DEBUG
fprintf(stderr, ">>> arp_send_request <<<\n");
arp_dump((uint8_t *)&request, sizeof(request));
#endif
if (netif->dev->ops->xmit(netif->dev, ETHERNET_TYPE_ARP, (uint8_t *)&request, sizeof(request), ETHERNET_ADDR_BROADCAST) == -1) {
return -1;
}
return 0;
}
static int
arp_send_reply (struct netif *netif, const uint8_t *tha, const ip_addr_t *tpa, const uint8_t *dst) {
struct arp_ethernet reply;
if (!tha || !tpa) {
return -1;
}
reply.hdr.hrd = hton16(ARP_HRD_ETHERNET);
reply.hdr.pro = hton16(ETHERNET_TYPE_IP);
reply.hdr.hln = ETHERNET_ADDR_LEN;
reply.hdr.pln = IP_ADDR_LEN;
reply.hdr.op = hton16(ARP_OP_REPLY);
memcpy(reply.sha, netif->dev->addr, ETHERNET_ADDR_LEN);
reply.spa = ((struct netif_ip *)netif)->unicast;
memcpy(reply.tha, tha, ETHERNET_ADDR_LEN);
reply.tpa = *tpa;
#ifdef DEBUG
cprintf(">>> arp_send_reply <<<\n");
arp_dump((uint8_t *)&reply, sizeof(reply));
#endif
if (netif->dev->ops->xmit(netif->dev, ETHERNET_TYPE_ARP, (uint8_t *)&reply, sizeof(reply), dst) < 0) {
return -1;
}
return 0;
}
static void
arp_rx (uint8_t *packet, size_t plen, struct netdev *dev) {
struct arp_ethernet *message;
time_t now;
int marge = 0;
struct netif *netif;
if (plen < sizeof(struct arp_ethernet)) {
return;
}
message = (struct arp_ethernet *)packet;
if (ntoh16(message->hdr.hrd) != ARP_HRD_ETHERNET) {
return;
}
if (ntoh16(message->hdr.pro) != ETHERNET_TYPE_IP) {
return;
}
if (message->hdr.hln != ETHERNET_ADDR_LEN) {
return;
}
if (message->hdr.pln != IP_ADDR_LEN) {
return;
}
#ifdef DEBUG
cprintf(">>> arp_rx <<<\n");
arp_dump(packet, plen);
#endif
acquire(&arplock);
time(&now);
if (now - timestamp > 10) {
timestamp = now;
arp_table_patrol();
}
marge = (arp_table_update(dev, &message->spa, message->sha) == 0) ? 1 : 0;
release(&arplock);
netif = netdev_get_netif(dev, NETIF_FAMILY_IPV4);
if (netif && ((struct netif_ip *)netif)->unicast == message->tpa) {
if (!marge) {
acquire(&arplock);
arp_table_insert(&message->spa, message->sha);
release(&arplock);
}
if (ntoh16(message->hdr.op) == ARP_OP_REQUEST) {
arp_send_reply(netif, message->sha, &message->spa, message->sha);
}
}
return;
}
int
arp_resolve (struct netif *netif, const ip_addr_t *pa, uint8_t *ha, const void *data, size_t len) {
struct arp_entry *entry;
int ret;
acquire(&arplock);
entry = arp_table_select(pa);
if (entry) {
if (memcmp(entry->ha, ETHERNET_ADDR_ANY, ETHERNET_ADDR_LEN) == 0) {
arp_send_request(netif, pa); /* just in case packet loss */
release(&arplock);
return ARP_RESOLVE_QUERY;
}
memcpy(ha, entry->ha, ETHERNET_ADDR_LEN);
release(&arplock);
return ARP_RESOLVE_FOUND;
}
entry = arp_table_freespace();
if (!entry) {
release(&arplock);
return ARP_RESOLVE_ERROR;
}
/*
if (data) {
entry->data = (uint8_t*)kalloc();
if (!entry->data) {
release(&arplock);
return ARP_RESOLVE_ERROR;
}
memcpy(entry->data, data, len);
entry->len = len;
}
*/
entry->used = 1;
entry->pa = *pa;
time(&entry->timestamp);
entry->netif = netif;
arp_send_request(netif, pa);
release(&arplock);
return ARP_RESOLVE_QUERY;
}
int
arp_init (void) {
struct arp_entry *entry;
time(×tamp);
initlock(&arplock, "arp");
netproto_register(NETPROTO_TYPE_ARP, arp_rx);
return 0;
}