-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patharp.c
340 lines (268 loc) · 10.7 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 <stdlib.h>
#include <stdbool.h>
#include <memory.h>
#include <netinet/in.h>
#define NETSTACK_LOG_UNIT "ARP"
#include <netstack/eth/arp.h>
#include <netstack/inet/neigh.h>
bool arp_log(struct pkt_log *log, struct frame *frame) {
struct arp_hdr *msg = (struct arp_hdr *) frame->head;
frame->data += ARP_HDR_LEN;
struct log_trans *trans = &log->t;
LOGT(trans, "hw 0x%X ", ntohs(msg->hwtype));
struct arp_ipv4 *req;
switch (ntohs(msg->proto)) {
case ETH_P_IP:
req = (struct arp_ipv4 *) frame->data;
switch (ntohs(msg->op)) {
case ARP_OP_REQUEST:
LOGT(trans, "Who has %s? ", fmtip4(ntohl(req->dipv4)));
LOGT(trans, "Tell %s ", fmtip4(ntohl(req->sipv4)));
break;
case ARP_OP_REPLY:
LOGT(trans, "Reply %s ", fmtip4(ntohl(req->sipv4)));
LOGT(trans, "is at %s ", fmtmac(req->saddr));
break;
default:
LOGT(trans, "invalid op %d", ntohs(msg->op));
}
break;
default:
LOGT(trans, "unrecognised proto %d", ntohs(msg->proto));
break;
};
return true;
}
void arp_recv(struct frame *frame) {
struct arp_hdr *msg = (struct arp_hdr *) frame->head;
frame->data += ARP_HDR_LEN;
switch (ntohs(msg->hwtype)) {
case ARP_HW_ETHER:
// this is good
break;
default:
LOG(LINFO, "ARP hardware %d not supported", ntohs(msg->hwtype));
}
// https://tools.ietf.org/html/rfc826
struct arp_ipv4 *req;
uint16_t proto = ntohs(msg->proto);
switch (proto) {
case ETH_P_IP:
// also good
req = (struct arp_ipv4 *) frame->data;
addr_t ether = {.proto = PROTO_ETHER, .ether = eth_arr(req->saddr)};
addr_t ipv4 = {.proto = PROTO_IPV4, .ipv4 = ntohl(req->sipv4)};
// Try to update an existing ARP entry
bool updated = arp_update_entry(frame->intf, ðer, &ipv4);
// Only cache ARP entry if it was sent to us
if (intf_has_addr(frame->intf, &ipv4)) {
// If the entry wasn't an update, it must be new
if (!updated)
arp_cache_entry(frame->intf, ðer, &ipv4);
}
// Print the newly-updated/inserted ARP table
if (updated)
arp_log_tbl(frame->intf, LINFO);
switch (ntohs(msg->op)) {
case ARP_OP_REQUEST: {
// If asking for us, send a reply with our LL address
addr_t ip = {.proto = PROTO_IPV4, .ipv4 = ntohl(req->dipv4)};
if (intf_has_addr(frame->intf, &ip))
arp_send_reply(frame->intf, ARP_HW_ETHER,
ntohl(req->dipv4), ntohl(req->sipv4),
req->saddr);
break;
}
case ARP_OP_REPLY:
default:
break;
}
break;
default:
LOG(LINFO, "ARP protocol %s (0x%04x) not supported",
fmt_ethertype(proto), proto);
};
}
void arp_log_tbl(struct intf *intf, loglvl_t level) {
struct log_trans trans = LOG_TRANS(level);
LOGT(&trans, "Intf\tProtocol\tHW Address\t\tState");
for_each_llist(&intf->arptbl) {
struct arp_entry *entry = llist_elem_data();
LOGT(&trans, "\n\t%s\t", intf->name);
LOGT(&trans, "%s\t", straddr(&entry->protoaddr));
LOGT(&trans, "%s\t", (entry->state & ARP_PENDING) ?
"(pending)\t" : straddr(&entry->hwaddr));
LOGT(&trans, "%s", fmt_arp_state(entry->state));
}
LOGT_COMMIT(&trans);
}
/* Retrieves IPv4 address from table, otherwise NULL */
struct arp_entry *arp_get_entry(llist_t *arptbl, proto_t hwtype,
addr_t *protoaddr) {
// Lock the table
pthread_mutex_lock(&arptbl->lock);
for_each_llist(arptbl) {
struct arp_entry *entry = llist_elem_data();
if (entry == NULL) {
LOG(LERR, "arp_entry_ipv4 is null?\t");
continue;
}
pthread_mutex_lock(&entry->lock);
// Check matching protocols
if (addreq(&entry->protoaddr, protoaddr)
&& entry->hwaddr.proto == hwtype) {
// Release the locks and return found entry
pthread_mutex_unlock(&arptbl->lock);
return entry;
}
pthread_mutex_unlock(&entry->lock);
}
pthread_mutex_unlock(&arptbl->lock);
return NULL;
}
uint16_t arp_proto_hw(proto_t proto) {
switch (proto) {
case PROTO_ETHER:
return ARP_HW_ETHER;
default:
return 0;
}
}
bool arp_update_entry(struct intf *intf, addr_t *hwaddr, addr_t *protoaddr) {
// TODO: Use hashtable for ARP lookups on IPv4
// Lock the table
pthread_mutex_lock(&intf->arptbl.lock);
for_each_llist(&intf->arptbl) {
struct arp_entry *entry = llist_elem_data();
pthread_mutex_lock(&entry->lock);
// If existing IP match, update it
// TODO: ARP doesn't account for protocol addresses that change hw
if (addreq(&entry->protoaddr, protoaddr)) {
bool updated = false;
// Only update hwaddr if it has actually changed
if (!addreq(&entry->hwaddr, hwaddr)) {
LOG(LINFO, "ARP cache entry %s changed", straddr(protoaddr));
// Update hwaddr for IP
memcpy(&entry->hwaddr, hwaddr, sizeof(addr_t));
updated = true;
}
// Remove PENDING and add RESOLVED
entry->state &= ~ARP_PENDING;
entry->state |= ARP_RESOLVED;
// Release all locks
pthread_mutex_unlock(&entry->lock);
pthread_mutex_unlock(&intf->arptbl.lock);
// Send any queued packets waiting for a hwaddr
neigh_update_hwaddr(intf, protoaddr, hwaddr);
// An entry was updated
return updated;
}
// Unlock entry lock
pthread_mutex_unlock(&entry->lock);
}
// Unlock the ARP table
pthread_mutex_unlock(&intf->arptbl.lock);
// Nothing was updated
return false;
}
bool arp_cache_entry(struct intf *intf, addr_t *hwaddr, addr_t *protoaddr) {
LOG(LINFO, "Storing new ARP entry for %s", straddr(protoaddr));
struct arp_entry *entry = malloc(sizeof(struct arp_entry));
entry->state = ARP_RESOLVED;
entry->lock = (pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER;
memcpy(&entry->hwaddr, hwaddr, sizeof(addr_t));
memcpy(&entry->protoaddr, protoaddr, sizeof(addr_t));
llist_append(&intf->arptbl, entry);
return true;
}
int arp_send_req(struct intf *intf, uint16_t hwtype,
addr_t *saddr, addr_t *daddr) {
struct log_trans trans = LOG_TRANS(LVERB);
LOGT(&trans, "arp_request(%s, %s", intf->name, straddr(saddr));
LOGT(&trans, ", %s);", straddr(daddr));
LOGT_COMMIT(&trans);
struct frame *frame = intf_frame_new(intf, intf_max_frame_size(intf));
struct arp_ipv4 *req = frame_data_alloc(frame, sizeof(struct arp_ipv4));
struct arp_hdr *hdr = frame_data_alloc(frame, sizeof(struct arp_hdr));
// TODO: Use hwtype to determine length and type of address
// TODO: Change arp_send_req to handle other address types
memcpy(&req->saddr, intf->ll_addr, ETH_ADDR_LEN);
memcpy(&req->daddr, ETH_BRD_ADDR, ETH_ADDR_LEN);
req->sipv4 = htonl(saddr->ipv4);
req->dipv4 = htonl(daddr->ipv4);
hdr->hwtype = htons(hwtype);
hdr->proto = htons(ETH_P_IP);
hdr->hlen = ETH_ADDR_LEN;
hdr->plen = (uint8_t) addrlen(PROTO_IPV4);
hdr->op = htons(ARP_OP_REQUEST);
frame_unlock(frame);
// Send the request frame
int ret = ether_send(frame, ETH_P_ARP, ETH_BRD_ADDR);
// Ensure frame is free'd if it was never actually sent
frame_decref(frame);
// Sending ARP request was successful, add incomplete cache entry
struct arp_entry *entry = NULL;
if (ret) {
// There was an error, return error-code immediately
return ret;
} else {
// Lock arptbl before sending to prevent race condition where reply
// arrives before we wait on it, deadlocking waiting on the reply that
// has already arrived.
pthread_mutex_lock(&intf->arptbl.lock);
// Check if partial entry already exists, so to not add multiple
for_each_llist(&intf->arptbl) {
entry = llist_elem_data();
if (entry == NULL)
continue;
pthread_mutex_lock(&entry->lock);
if (addreq(&entry->protoaddr, daddr))
break;
// Not the entry we want. Unlock it and put it back
pthread_mutex_unlock(&entry->lock);
// Clear the entry to ensure the if below runs if we do not find
// and lock an entry within this loop.
entry = NULL;
}
// Don't add another partial entry if one is there already
if (entry == NULL) {
entry = malloc(sizeof(struct arp_entry));
*entry = (struct arp_entry) {
.state = ARP_PENDING,
.hwaddr = {.proto = PROTO_ETHER, .ether = eth_arr(ETH_NUL_ADDR)},
.protoaddr = *daddr,
.lock = PTHREAD_MUTEX_INITIALIZER
};
pthread_mutex_lock(&entry->lock);
llist_append_nolock(&intf->arptbl, entry);
arp_log_tbl(intf, LINFO);
}
}
// At this point we have an arp_entry that is locked with entry->lock
pthread_mutex_unlock(&intf->arptbl.lock);
// Unlock the entry
pthread_mutex_unlock(&entry->lock);
return ret;
}
int arp_send_reply(struct intf *intf, uint16_t hwtype, ip4_addr_t sip,
ip4_addr_t dip, eth_addr_t daddr) {
// TODO: Change arp_send_reply to handle other address types
struct frame *frame = intf_frame_new(intf, intf_max_frame_size(intf));
struct arp_ipv4 *req = frame_data_alloc(frame, sizeof(struct arp_ipv4));
struct arp_hdr *hdr = frame_data_alloc(frame, sizeof(struct arp_hdr));
// TODO: Use hwtype to determine length and type of address
memcpy(&req->saddr, intf->ll_addr, ETH_ADDR_LEN);
memcpy(&req->daddr, daddr, ETH_ADDR_LEN);
req->sipv4 = htonl(sip);
req->dipv4 = htonl(dip);
hdr->hwtype = htons(hwtype);
hdr->proto = htons(ETH_P_IP);
hdr->hlen = ETH_ADDR_LEN;
hdr->plen = (uint8_t) addrlen(PROTO_IPV4);
hdr->op = htons(ARP_OP_REPLY);
frame_unlock(frame);
int ret = ether_send(frame, ETH_P_ARP, daddr);
frame_decref(frame);
return ret;
}