-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsr_arp_cache.c
498 lines (470 loc) · 20.4 KB
/
sr_arp_cache.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/* Filename: sr_arp_cache.c
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <sys/time.h>
#include <time.h>
#include "sr_common.h"
#include "sr_ethernet.h"
#include "sr_router.h"
#include "sr_arp_cache.h"
#include "cli/helper.h"
#include "lwtcp/lwip/sys.h"
#include "packet_dispatcher.h"
#include "linked_list.h"
#include "sr_arp_cache_entry.h"
#include "sr_integration.h"
#include "sr_arp.h"
#include "sr_icmp.h"
#include "sr_ip.h"
#include "sr_icmp_types_send.h"
#include "cli/cli.h"
#include "reg_defines.h"
// Interval for the ARP cache garbage collector thread in seconds
#define ARP_GC_INTERVAL_S 5
// The time after which entries are removed from the ARP cache
#define ARP_CACHE_TIMEOUT 30
// The time after which packets are removed from the queue
#define PACKET_TIMEOUT 2
/**
* Initialize the ARP response cache and starts a new detached thread which will
* scrub old entries from the cached as defined by ARP_GC_INTERVAL_S.
* If initialisation failed, router->arp_cache is set to NULL.
*
* @param router The router whose ARP cache (router->arp_cache) will be inited
*/
void arp_cache_init( sr_router* router ) {
printf(" ** arp_cache_init(..) called \n");
// Initialize cache
router->arp_cache = (arp_cache_t*) malloc_or_die(sizeof(arp_cache_t));
// Initialize cache lists
router->arp_cache->arp_cache_list = llist_new();
router->arp_cache->arp_static_cache_list = llist_new();
router->arp_cache->arp_request_queue = llist_new();
// Initialize mutexes
pthread_mutex_init(&router->arp_cache->lock_static, NULL);
pthread_mutex_init(&router->arp_cache->lock_dynamic, NULL);
pthread_mutex_init(&router->arp_cache->lock_queue, NULL);
// Initialize packet dispatcher
router->arp_cache->packet_dispatcher = packet_dispatcher_new();
packet_dispatcher_run(router->arp_cache->packet_dispatcher);
// Run cache timeout thread
time_out_run(router);
}
/**
* Destroys an existing ARP cache (frees the memory associated with it and its
* members).
*
* @param cache The ARP cache to destroy
*/
void cache_destroy( arp_cache_t* cache ) {
printf(" ** arp cache_destroy(..) called\n");
pthread_mutex_lock(&cache->lock_dynamic);
cache->arp_cache_list = llist_delete_no_count(cache->arp_cache_list);
pthread_mutex_unlock(&cache->lock_dynamic);
pthread_mutex_lock(&cache->lock_static);
cache->arp_static_cache_list = llist_delete_no_count(cache->arp_static_cache_list);
pthread_mutex_unlock(&cache->lock_static);
pthread_mutex_unlock(&cache->lock_queue);
cache->arp_request_queue = llist_delete_no_count(cache->arp_request_queue);
pthread_mutex_unlock(&cache->lock_queue);
pthread_mutex_destroy(&cache->lock_static);
pthread_mutex_destroy(&cache->lock_dynamic);
pthread_mutex_destroy(&cache->lock_queue);
pthread_cancel(cache->time_out_thread);
}
/**
* Add a static entry to the static ARP cache. Used by the telnet command
* line interface.
* @return true if succeeded (fails if the max # of static entries are already
* in the cache).
*/
bool cache_static_entry_add( sr_router* router,
addr_ip_t ip,
addr_mac_t* mac ) {
printf(" ** arp cache_static_entry_add(..) called\n");
arp_cache_entry_t *entry = (arp_cache_entry_t*) malloc_or_die(sizeof(arp_cache_entry_t));
entry->ip = ip;
entry->mac = *mac;
entry->timeout = (struct timeval*) malloc_or_die(sizeof(struct timeval));
gettimeofday(entry->timeout, NULL);
entry->timeout->tv_sec = entry->timeout->tv_sec + ARP_CACHE_TIMEOUT;
pthread_mutex_lock(&router->arp_cache->lock_static);
router->arp_cache->arp_static_cache_list = llist_update_beginning(router->arp_cache->arp_static_cache_list, predicate_ip,(void*) entry);
pthread_mutex_unlock(&router->arp_cache->lock_static);
// update hw
arp_write_hw();
return TRUE;
}
/**
* Remove a static entry to the static ARP cache.
* @return true if succeeded (false if ip wasn't in the cache as a static entry)
*/
bool cache_static_entry_remove( sr_router* router, addr_ip_t ip ) {
printf(" ** arp cache_static_entry_remove(..) called\n");
pthread_mutex_lock(&router->arp_cache->lock_static);
node *ret = (node*) llist_remove(router->arp_cache->arp_static_cache_list, predicate_ip, (void*)&ip);
pthread_mutex_unlock(&router->arp_cache->lock_static);
if(ret != NULL) {
router->arp_cache->arp_static_cache_list = ret;
// update hw
arp_write_hw();
return TRUE;
}
return FALSE;
}
/**
* Remove all static entries from the ARP cache.
* @return number of static entries removed
*/
unsigned cache_static_purge( struct sr_router* router ) {
printf(" ** arp cache_static_purge(..) called\n");
int ret = 0;
pthread_mutex_lock(&router->arp_cache->lock_static);
router->arp_cache->arp_static_cache_list = llist_delete(router->arp_cache->arp_static_cache_list, &ret);
pthread_mutex_unlock(&router->arp_cache->lock_static);
// update hw
arp_write_hw();
return ret;
}
/**
* Add a dynamic entry to the dynamic ARP cache. Used by the telnet command
* line interface.
* @return true if succeeded (fails if the max # of dynamic entries are already
* in the cache).
*/
bool cache_dynamic_entry_add( struct sr_router* router,
addr_ip_t ip,
addr_mac_t* mac ) {
printf(" ** arp cache_dynamic_entry_add(..) called\n");
arp_cache_entry_t *entry = (arp_cache_entry_t*) malloc_or_die(sizeof(arp_cache_entry_t));
entry->ip = ip;
entry->mac = *mac;
entry->timeout = (struct timeval*) malloc_or_die(sizeof(struct timeval));
gettimeofday(entry->timeout, NULL);
entry->timeout->tv_sec = entry->timeout->tv_sec + ARP_CACHE_TIMEOUT;
pthread_mutex_lock(&router->arp_cache->lock_dynamic);
router->arp_cache->arp_cache_list = llist_update_beginning(router->arp_cache->arp_cache_list, predicate_ip, (void*) entry);
pthread_mutex_unlock(&router->arp_cache->lock_dynamic);
// update hw
arp_write_hw();
return TRUE;
}
/**
* Remove a dynamic entry from the dynamic ARP cache.
* @return true if succeeded (false if ip wasn't in the cache as a dynamic entry)
*/
bool cache_dynamic_entry_remove( sr_router* router, addr_ip_t ip ) {
printf(" ** arp dynamic_entry_remove(..) called\n");
pthread_mutex_lock(&router->arp_cache->lock_dynamic);
node *ret = (node*) llist_remove(router->arp_cache->arp_cache_list, predicate_ip, (void*)&ip);
pthread_mutex_unlock(&router->arp_cache->lock_dynamic);
if(ret != NULL) {
router->arp_cache->arp_cache_list = ret;
// update hw
arp_write_hw();
return TRUE;
}
return FALSE;
}
/**
* Remove all dynamic entries from the ARP cache.
* @return number of dynamic entries removed
*/
unsigned cache_dynamic_purge( struct sr_router* router ) {
printf(" ** arp cache_dynamic_purge(..) called\n");
int ret = 0;
pthread_mutex_lock(&router->arp_cache->lock_dynamic);
router->arp_cache->arp_cache_list = llist_delete(router->arp_cache->arp_cache_list, &ret);
pthread_mutex_unlock(&router->arp_cache->lock_dynamic);
// update hw
arp_write_hw();
return ret;
}
/*Function to remove timed-out entries from both caches*/
void remove_timed_out(void* router_input) {
printf(" ** arp time out thread called\n");
sr_router* router = (sr_router*) router_input;
struct timespec *timeout =(struct timespec*) malloc_or_die(sizeof(struct timespec));
struct timespec *timeout_rem =(struct timespec*) malloc_or_die(sizeof(struct timespec));
timeout->tv_sec = (time_t) ARP_GC_INTERVAL_S;
timeout->tv_nsec = 0;
while(1) {
// sleep for interval
nanosleep(timeout, timeout_rem);
printf(" ** arp time out thread awoken\n");
// get current time
struct timeval *current_time = (struct timeval*)malloc_or_die(sizeof(struct timeval));
gettimeofday(current_time, NULL);
// remove timed out entries from dynamic list
// lock list
pthread_mutex_lock(&router->arp_cache->lock_dynamic);
// display current list
llist_display_all(router->arp_cache->arp_cache_list, display_cache_entry);
// remove all timed out
if(current_time == NULL)
printf("Current time is null\n");
router->arp_cache->arp_cache_list = llist_remove_all_no_count(router->arp_cache->arp_cache_list, predicate_timeval2, (void*)current_time);
// unlock list
pthread_mutex_unlock(&router->arp_cache->lock_dynamic);
// update hw
arp_write_hw();
}
}
void time_out_run(struct sr_router* router) {
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);
sys_thread_new(remove_timed_out, (void*) router);
}
/**
* Fetches the HW address associated with the specified IP address and sets it
* as the destination MAC address, then sends the packet. If the IP cannot be
* resolved to a MAC, sends an ICMP host unreachable back to the sender (except
* where a host unreachable would be inappropriate).
*
* @param dst_ip the IP whose corresponding MAC needs to be placed in the frame
* @param ethernet_frame the ethernet frame which needs its dst MAC (will be
* freed by this method).
* @param len number of bytes in the ethernet_frame
*
* @return 0 if the send call failed (had ARP address), or
* 1 if it is delayed (needs ARP, could fail), or
* 2 if the packet was sent right away
*/
int arp_cache_handle_partial_frame( struct sr_router* router,
interface_t* intf,
addr_ip_t dst_ip,
byte* payload /* given */,
unsigned len,
uint16_t type) {
/*
* This functions handles all outgoing packets. There are three conditions
* that could occur for an outgoing packet:
* a) The IP address is already in the ARP cache. In this case the
* corresponding MAC address just needs to be copied into the destination
* MAC address in the ethernet header of the packet and the packet sent.
* Note that the MAC address can just be copied from a addr_mac_t with
* memcpy, and the packet can be sent with sr_integ_low_level_output() from
* sr_integration.h.
* b) The IP address is not in the ARP cache but we have recently sent an ARP
* request for this IP. In this case, the packet should be queued for when
* the ARP reply arrives.
* c) The IP address is not in the ARP cache and we haven't recently sent an
* ARP request for that IP. In this case, we need to send an ARP request
* and queue the packet.
* For queued packets, you could (should?) make use of the functions and
* structures in packet_dispatcher.h. You can queue any packets with the
* packet dispatcher using packet_dispatcher_put_packet() and tell it about
* any updates to the cache using packet_dispatcher_put_update(). It will
* send out any packets, ICMP host unreachables, etc. that are appropriate
*/
printf(" ** arp_cache_handle_partial_frame(..) called \n");
printf(" ** arp_cache_handle_partial_frame(..): packet of len: %u \n", len);
node* ret, *q_ret;
pthread_mutex_lock(&router->arp_cache->lock_dynamic);
ret = llist_find(router->arp_cache->arp_cache_list, predicate_ip, (void*) &dst_ip);
pthread_mutex_unlock(&router->arp_cache->lock_dynamic);
if(ret != NULL) {
printf(" ** arp_cache_handle_partial_frame(..): mac found in cache\n");
arp_cache_entry_t* entry_dynamic = (arp_cache_entry_t*) ret->data;
ethernet_send_frame(&entry_dynamic->mac, intf, type, payload, len);
return 2;
} else {
pthread_mutex_lock(&router->arp_cache->lock_static);
ret = llist_find(router->arp_cache->arp_static_cache_list, predicate_ip, (void*) &dst_ip);
pthread_mutex_unlock(&router->arp_cache->lock_static);
if(ret != NULL) {
printf(" ** arp_cache_handle_partial_frame(..): mac found in static cache\n");
arp_cache_entry_t* entry_static = (arp_cache_entry_t*) ret->data;
ethernet_send_frame(&entry_static->mac, intf, type, payload, len);
return 2;
}
else {
pthread_mutex_lock(&router->arp_cache->lock_queue);
q_ret = llist_find(router->arp_cache->arp_request_queue, predicate_ip_arp_queue_entry_t, (void*) &dst_ip);
pthread_mutex_unlock(&router->arp_cache->lock_queue);
if(q_ret == NULL) {
printf(" ** arp_cache_handle_partial_frame(..): Sending ARP request\n");
arp_queue_entry_t *item = (arp_queue_entry_t*) malloc_or_die(sizeof(arp_queue_entry_t));
item->ip = dst_ip;
item->tries = 1;
pthread_mutex_lock(&router->arp_cache->lock_queue);
router->arp_cache->arp_request_queue = llist_insert_beginning(router->arp_cache->arp_request_queue, (void*) item);
pthread_mutex_unlock(&router->arp_cache->lock_queue);
// Send ARP Request
arp_send_request( dst_ip, intf );
packet_dispatcher_put_packet(router->arp_cache->packet_dispatcher, dst_ip, payload, len, intf, Q_PACKET_TIMEOUT );
return 1;
} else {
printf(" ** arp_cache_handle_partial_frame(..): ARP request recently sent\n");
//check if request sent 5 times
arp_queue_entry_t *item = (arp_queue_entry_t*) q_ret->data;
if(item->tries >=5) {
printf(" ** arp_cache_handle_partial_frame(..): Host hasn't replied to 5 requests, sending ICMP host unreachable\n");
//check if sender not self
if(intf->ip != dst_ip) {
// send icmp destination host unreachable
uint8_t code = ICMP_TYPE_CODE_DST_UNREACH_HOST;
// get original ip header
struct ip* ip_header = make_ip_header(payload);
uint16_t packet_len = ip_header->ip_len;
icmp_type_dst_unreach_send(&code, payload, &packet_len, ip_header);
} else {
printf(" ** arp_cache_handle_partial_frame(..): Host hasn't replied to 5 requests, but I'm the host, so no ICMP error message\n");
}
} else {
// increment tries
item->tries = item->tries + 1;
printf(" ** arp_cache_handle_partial_frame(..): Sending ARP request tries: %d\n", item->tries);
// put item back on queue
pthread_mutex_lock(&router->arp_cache->lock_queue);
router->arp_cache->arp_request_queue = llist_update_beginning_delete(router->arp_cache->arp_request_queue, predicate_ip_arp_queue_entry_t, (void*) item);
pthread_mutex_unlock(&router->arp_cache->lock_queue);
// Send ARP Request
arp_send_request( dst_ip, intf );
packet_dispatcher_put_packet(router->arp_cache->packet_dispatcher, dst_ip, payload, len, intf, Q_PACKET_TIMEOUT );
}
packet_dispatcher_put_packet(router->arp_cache->packet_dispatcher, dst_ip, payload, len, intf, Q_PACKET_TIMEOUT );
return 0;
}
}
}
}
/**
* Fills buf with a string representation of the arp cache.
*
*/
void arp_cache_to_string() {
char *str;
cli_send_str("Static entries: \n");
asprintf(&str, "IP, MAC, Timestamp\n");
cli_send_str(str);
free(str);
arp_cache_entry_t *item;
node* head;
// get instance of router
struct sr_instance* sr_inst = get_sr();
struct sr_router* router = (struct sr_router*)sr_get_subsystem(sr_inst);
// put static cache
pthread_mutex_lock(&router->arp_cache->lock_static);
head = router->arp_cache->arp_static_cache_list;
while(head != NULL) {
item = (arp_cache_entry_t*) head->data;
asprintf(&str, "%s, %s, %ld.%06ld\n", quick_ip_to_string(item->ip), quick_mac_to_string(item->mac.octet), item->timeout->tv_sec, item->timeout->tv_usec);
cli_send_str(str);
free(str);
head = head->next;
}
pthread_mutex_unlock(&router->arp_cache->lock_static);
// put dynamic cache
cli_send_str("Dynamic entries: \n");
asprintf(&str, "IP, MAC, Timestamp\n");
cli_send_str(str);
free(str);
pthread_mutex_lock(&router->arp_cache->lock_dynamic);
head = router->arp_cache->arp_cache_list;
while(head != NULL) {
item = (arp_cache_entry_t*) head->data;
asprintf(&str, "%s, %s, %ld.%06ld\n", quick_ip_to_string(item->ip), quick_mac_to_string(item->mac.octet), item->timeout->tv_sec, item->timeout->tv_usec);
cli_send_str(str);
free(str);
head = head->next;
}
pthread_mutex_unlock(&router->arp_cache->lock_dynamic);
}
void arp_write_hw() {
#ifdef _CPUMODE_
printf(" ** arp_write_hw(..) called \n");
struct sr_instance* sr_inst = get_sr();
struct sr_router* router = (struct sr_router*)sr_get_subsystem(sr_inst);
node* static_head;
node* dynamic_head;
unsigned int mac_hi = 0;
unsigned int mac_lo = 0;
int i = 0;
arp_cache_entry_t* cache_entry;
pthread_mutex_lock(&router->arp_cache->lock_static);
static_head = router->arp_cache->arp_static_cache_list;
while(static_head != NULL && i < ROUTER_OP_LUT_ARP_TABLE_DEPTH) {
mac_hi = 0;
mac_lo = 0;
cache_entry = (arp_cache_entry_t*) static_head->data;
mac_hi |= ((unsigned int)cache_entry->mac.octet[0]) << 8;
mac_hi |= ((unsigned int)cache_entry->mac.octet[1]);
mac_lo |= ((unsigned int)cache_entry->mac.octet[2]) << 24;
mac_lo |= ((unsigned int)cache_entry->mac.octet[3]) << 16;
mac_lo |= ((unsigned int)cache_entry->mac.octet[4]) << 8;
mac_lo |= ((unsigned int)cache_entry->mac.octet[5]);
// write mac
writeReg(&router->hw_device, ROUTER_OP_LUT_ARP_TABLE_ENTRY_MAC_HI_REG, mac_hi);
writeReg(&router->hw_device, ROUTER_OP_LUT_ARP_TABLE_ENTRY_MAC_LO_REG, mac_lo);
// write ip
writeReg(&router->hw_device, ROUTER_OP_LUT_ARP_TABLE_ENTRY_NEXT_HOP_IP_REG, ntohl(cache_entry->ip));
// write table index
writeReg(&router->hw_device, ROUTER_OP_LUT_ARP_TABLE_WR_ADDR_REG, i);
static_head = static_head->next;
i++;
}
pthread_mutex_unlock(&router->arp_cache->lock_static);
// now dynamic cache
pthread_mutex_lock(&router->arp_cache->lock_dynamic);
dynamic_head = router->arp_cache->arp_cache_list;
while(dynamic_head != NULL && i < ROUTER_OP_LUT_ARP_TABLE_DEPTH) {
mac_hi = 0;
mac_lo = 0;
cache_entry = (arp_cache_entry_t*) dynamic_head->data;
mac_hi |= ((unsigned int)cache_entry->mac.octet[0]) << 8;
mac_hi |= ((unsigned int)cache_entry->mac.octet[1]);
mac_lo |= ((unsigned int)cache_entry->mac.octet[2]) << 24;
mac_lo |= ((unsigned int)cache_entry->mac.octet[3]) << 16;
mac_lo |= ((unsigned int)cache_entry->mac.octet[4]) << 8;
mac_lo |= ((unsigned int)cache_entry->mac.octet[5]);
// write mac
writeReg(&router->hw_device, ROUTER_OP_LUT_ARP_TABLE_ENTRY_MAC_HI_REG, mac_hi);
writeReg(&router->hw_device, ROUTER_OP_LUT_ARP_TABLE_ENTRY_MAC_LO_REG, mac_lo);
// write ip
writeReg(&router->hw_device, ROUTER_OP_LUT_ARP_TABLE_ENTRY_NEXT_HOP_IP_REG, ntohl(cache_entry->ip));
// write table index
writeReg(&router->hw_device, ROUTER_OP_LUT_ARP_TABLE_WR_ADDR_REG, i);
dynamic_head = dynamic_head->next;
i++;
}
pthread_mutex_unlock(&router->arp_cache->lock_dynamic);
#endif
}
void arp_read_hw() {
#ifdef _CPUMODE_
printf(" ** arp_read__hw(..) called \n");
struct sr_instance* sr_inst = get_sr();
struct sr_router* router = (struct sr_router*)sr_get_subsystem(sr_inst);
char *str;
int i;
unsigned int mac[2];
uint32_t ip;
for (i = 0; i < ROUTER_OP_LUT_ARP_TABLE_DEPTH; i++) {
mac[0] = 0;
mac[1] = 0;
// write table index to read register
writeReg(&router->hw_device, ROUTER_OP_LUT_ARP_TABLE_RD_ADDR_REG, i);
// read mac hi, lo
readReg(&router->hw_device, ROUTER_OP_LUT_ARP_TABLE_ENTRY_MAC_LO_REG, &mac[1]);
readReg(&router->hw_device, ROUTER_OP_LUT_ARP_TABLE_ENTRY_MAC_HI_REG, &mac[2]);
mac[0] = htonl(mac[0]);
mac[1] = htonl(mac[1]);
// read ip
readReg(&router->hw_device, ROUTER_OP_LUT_ARP_TABLE_ENTRY_NEXT_HOP_IP_REG, &ip);
ip = htonl(ip);
// check for blank entries
if (ip == 0)
break;
asprintf(&str, "MAC, IP\n");
cli_send_str(str);
free(str);
asprintf(&str, "%s, %s\n", quick_mac_to_string(((uint8_t*)mac) + 2), quick_ip_to_string(ip));
cli_send_str(str);
free(str);
}
#endif
}