-
Notifications
You must be signed in to change notification settings - Fork 0
/
sr_arp.c
575 lines (492 loc) · 16.8 KB
/
sr_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
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
#include "sr_arp.h"
#include "sr_protocol.h"
#include <string.h>
#include "sr_if.h"
#include "sr_router.h"
#include "sr_handler.h"
/*
adds a new entry at start of list (just after head)
sets correct creation time
then returns the pointer to the new entry
*/
void add_cache_entry(struct sr_instance* sr, uint32_t IP, uint8_t MAC[6], time_t created){
assert(sr);
struct sr_router* subs = (struct sr_router*)sr_get_subsystem(sr);
assert(subs);
struct arp_cache_list* new_entry = (struct arp_cache_list*) malloc_or_die(sizeof(struct arp_cache_list));
new_entry->IP = IP;
mac_copy(MAC, new_entry->MAC);
new_entry->created = created;
if(subs->arp_cache == NULL){ /* there was no pre-extant cache*/
new_entry->next = NULL;
subs->arp_cache = new_entry;
return;
}
//headRef = &subs->arp_cache;
new_entry->next = subs->arp_cache;
subs->arp_cache = new_entry;
}
/*
adds a new entry at start of list (just after head)
sets correct creation time
then returns the pointer to the new entry
*/
int add_static_entry(struct sr_instance* sr, uint32_t IP, uint8_t MAC[6]){
assert(sr);
struct sr_router* subs = (struct sr_router*)sr_get_subsystem(sr);
assert(subs);
struct arp_cache_list* new_entry = (struct arp_cache_list*) malloc_or_die(sizeof(struct arp_cache_list));
new_entry->IP = IP;
mac_copy(MAC, new_entry->MAC);
if(subs->arp_cache_static == NULL){ /* there was no pre-extant cache*/
new_entry->next = NULL;
subs->arp_cache_static = new_entry;
return 2;
}
//headRef = &subs->arp_cache;
new_entry->next = subs->arp_cache_static;
subs->arp_cache_static = new_entry;
return 1;
}
/*
Looks through the cache list
updates the first match
returns 1 if it found a match
or 0 if nothing has been found (or list doesn't exist yet)
*/
int find_and_update(struct sr_instance* sr, uint32_t IP, uint8_t MAC[6], time_t now){
assert(sr);
struct sr_router* subs = (struct sr_router*)sr_get_subsystem(sr);
assert(subs);
struct arp_cache_list* head = (struct arp_cache_list*)malloc_or_die(sizeof(struct arp_cache_list));
head = subs->arp_cache;
// CHANGING TO sr_instance from arp_cache_list
while (head != NULL){
if (head->IP == IP){
break;
}
head = head->next;
}
if(head != NULL){
mac_copy(MAC, head->MAC);
head->created = now;
printf("*********** %s updated an entry in the cache\n", __func__);
return 1;
}
else{
printf("*********** I didn't already have it in cache, so I will add it\n");
add_cache_entry(sr, IP, MAC, now);
return 0;
}
}
uint8_t* find_mac_in_cache(struct sr_instance* sr, uint32_t IP, unsigned int type){
assert(sr);
struct sr_router* subs = (struct sr_router*)sr_get_subsystem(sr);
assert(subs);
struct arp_cache_list* head = (struct arp_cache_list*)malloc_or_die(sizeof(struct arp_cache_list));
if(type = 0){
head = subs->arp_cache_static;
}
else{
head = subs->arp_cache;
}
unsigned char* MAC = (unsigned char*) malloc_or_die(6* sizeof(unsigned char));
while (head != NULL){
printf(" ~~~~~~~~~~~~~~~~~~ IP = 0x%x, looking for 0x%x\n", head->IP, IP);
if(head->IP == IP){
break;
}
head = head->next;
}
if(head != NULL){
printf("****************** %s Found a MAC address\n", __func__);
mac_copy(head->MAC, MAC);
/* Hopefully this isn't going to go wrong if the entry is subsequently deleted? */
return MAC;
}
return NULL;
}
/*
calculate the length of the arp cache (ignoring head)
rturns the count
*/
int get_cache_length(struct sr_instance* sr, unsigned int type){
assert(sr);
struct sr_router* sub = (struct sr_router*)sr_get_subsystem(sr);
assert(sub);
int length = 0;
if(type == 1){ /* dynamic cache */
struct arp_cache_list* current = sub->arp_cache;
while(current != NULL){
printf("length %d ", length);
length++;
current = current->next;
}
}
else { /* assume it must be static*/
struct arp_cache_list* current = sub->arp_cache_static;
while(current != NULL){
length++;
current = current->next;
}
}
return length;
}
/* remove all entries with this IP address
returns the number of entries removed
Type 0 = static, Type 1 dynamic
*/
int remove_cache_entry(struct sr_instance* sr, uint32_t IP, unsigned int type){
assert(sr);
struct sr_router* sub = (struct sr_router*)sr_get_subsystem(sr);
assert(sub);
int count = 0;
struct arp_cache_list* prev;
struct arp_cache_list* current;
if(type == 0){
prev = sub->arp_cache_static;
if(prev->IP == IP){
free(prev);
sub->arp_cache_static = NULL;
return 1;
}
current = prev->next;
while (current != NULL){
if(current->IP == IP){
prev->next = current->next;
count++;
}
current = current->next;
}
return count;
}
else{
prev = sub->arp_cache;
if(prev->IP == IP){
free(prev);
sub->arp_cache = NULL;
return 1;
}
current = prev->next;
while (current != NULL){
if(current->IP == IP){
prev->next = current->next;
count++;
}
current = current->next;
}
return count;
}
return 0;
}
/***********************************************
* 2 identical functions to delete all entries
* in eitehr static or dynamic cache. Both
* return the number of entries deleted. These
* will be hooked into the two arp_cache_*_purge
* commands in cli.
************************************************/
int delete_arp_cache(struct sr_instance* sr){
assert(sr);
struct sr_router* sub = (struct sr_router*)sr_get_subsystem(sr);
assert(sub);
int count = 0;
struct arp_cache_list* step;
struct arp_cache_list* prev;
step = sub->arp_cache;
if(step != NULL){
prev=step;
step=step->next;
count++;
while(step != NULL){
free(prev);
prev = step;
step = step->next;
count++;
}
sub->arp_cache = NULL;
return count;
}
return 0;
}
int delete_static_cache(struct sr_instance* sr){
assert(sr);
struct sr_router* sub = (struct sr_router*)sr_get_subsystem(sr);
assert(sub);
int count = 0;
struct arp_cache_list* step;
struct arp_cache_list* prev;
step = sub->arp_cache_static;
if(step != NULL){
prev=step;
step=step->next;
count++;
while(step != NULL){
free(prev);
prev = step;
step = step->next;
count++;
}
sub->arp_cache_static = NULL;
return count;
}
return 0;
return 0;
}
// CODE BELOW HERE MAY BE WRONG
//generic arp handling function
// ARP packets can only be doing 2 things:
// requesting a response (e.g. a request) - these can only be sent for correct interface
// (but they can also be used to update the cache entry for that sender)
// replying to a request - this means a request had been sent. But for safety we only
// do anything if this is the correct interface. In which case we have to find
// any packets we had queued waiting for this response
int process_arp(struct sr_instance* sr, char* intf, struct sr_arphdr* arp){
//1) is this our packet? Or perhaps we should check that before we pass it?
//2) Then we use it to update our ARP cache
//3) Then check if it is a request and reply to it
assert(sr);
assert(intf);
assert(arp);
struct sr_vns_if* interface = sr_get_interface(sr, intf);
//check this is a well-formed ARP packet
if(arp->ar_hrd != ARPHDR_ETHER){
printf("@@@@@@@@@@@@@@@@@@@@@@ %s not an ethernet packet\n", __func__);
return -1;
}
if(arp->ar_pro != ETHERTYPE_IP){
printf("@@@@@@@@@@@@@@@@@@@@@@ %s I expected IP in this ARP packet\n", __func__);
return -1;
}
if(arp->ar_hln != 6){
printf("@@@@@@@@@@@@@@@@@@@@@@ %s wrong ethernet address length\n", __func__);
return -1;
}
if(arp->ar_pln != 4){
printf("@@@@@@@@@@@@@@@@@@@@@@ %s wrong IP address length\n", __func__);
return -1;
}
if(check_my_interface(sr, arp->ar_tip)){
//this is our packet
//look for an existing entry (and update if needed)
time_t now = time(NULL);
if(find_and_update(sr, arp->ar_sip, arp->ar_sha, now)){
printf("***************** %s Found an entry and updated it \n", __func__);
}
if(arp->ar_op == ARP_REQUEST){
//create a reply - how?
if(send_arp_reply(sr, intf, arp)){
return 2;
}
else{
return -2;
}
}
else if(arp->ar_op == ARP_REPLY){
//do what?
}
else{
printf("@@@@@@@@@@@@@@@@@@@@@@@@@ %s this was a malformed ARP packet (or I have gone wrong)\n", __func__);
return -1;
}
}
//what do we do if this wasn't our packet?
else{
return -1;
}
return -1;
}
/*
create an arp reply, then send it out
return 1 if it succeeds
return 0 if it fails
*/
int send_arp_reply(struct sr_instance* sr, char* intf, struct sr_arphdr* arp){
assert(arp);
assert(sr);
int arp_pkt_len;
arp_pkt_len = ETH_HDR_LEN + sizeof(struct sr_arphdr);
struct sr_vns_if * interface;
interface = sr_get_interface(sr, intf);
struct sr_arphdr* tmparp = (struct sr_arphdr*) malloc_or_die(28*sizeof(uint8_t));
//use 'arp' to create the reply
tmparp->ar_hrd = htons(arp->ar_hrd);
tmparp->ar_pro = htons(arp->ar_pro);
tmparp->ar_op = htons(ARP_REPLY);
tmparp->ar_hln = arp->ar_hln;
tmparp->ar_pln = arp->ar_pln;
mac_copy(arp->ar_sha, tmparp->ar_tha);
tmparp->ar_tip = arp->ar_sip;
mac_copy(interface->addr, tmparp->ar_sha);
tmparp->ar_sip = interface->ip;
struct sr_ethernet_hdr* tmpHdr = (struct sr_ethernet_hdr*) malloc_or_die(14* sizeof(uint8_t));
mac_copy(tmparp->ar_tha, tmpHdr->ether_dhost);
mac_copy(tmparp->ar_sha, tmpHdr->ether_shost);
tmpHdr->ether_type = htons(ETHERTYPE_ARP);
uint8_t* newPkt = (uint8_t*) malloc_or_die(arp_pkt_len);
// How do we join the pieces?
newPkt = create_ethernet_frame(tmpHdr, tmparp, 28);
//pad the frame somehow?
//now we send it using sr_send_packet
if((sr_integ_low_level_output(sr, newPkt, (unsigned int)arp_pkt_len, intf)) == 0){
printf("********** %s successfully sent an ARP reply\n", __func__);
return 1;
}
else {
printf("\n @@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@ %s sending ARP reply failed?\n", __func__);
return 0;
}
return 1;
}
//need arp_lookup(struct sr_instance sr, char* interface, uint8_t* payload, uint32_t dstIP, unsigned int len)
//This will take the dstIP, look it up in the ARP cache. If it's there, get the MAC
//If it isn't there it will have to send an arp request. Use send_arp_request as below
//
//If you have the MAC, you can create the ethernet frame, then send
//retun will be 1 for success, 2 for ARP, 0 for fail
int arp_lookup(struct sr_instance * sr, char* interface, uint8_t* payload, uint32_t dstIP, unsigned int len){
struct sr_ethernet_hdr* ethHdr = (struct sr_ethernet_hdr*) malloc_or_die(sizeof(struct sr_ethernet_hdr));
struct sr_vns_if* intf = sr_get_interface(sr, interface);
//struct sr_router* sub = (struct sr_router*)sr_get_subsystem(sr);
int rtn;
//uint8_t src[6];
mac_copy(intf->addr, ethHdr->ether_shost);
#if 0
printf("********* at %s Payload is 0x ", __func__);
for (i = 0; i < len; i++){
printf("%x", payload[i]);
}
printf("\n");
#endif
uint8_t* dst;
dst = find_mac_in_cache(sr, dstIP, 0);
if(dst == NULL){
dst = find_mac_in_cache(sr, dstIP, 1);
}
if(dst != NULL){
mac_copy(dst, ethHdr->ether_dhost);
mac_copy(intf->addr, ethHdr->ether_shost);
ethHdr->ether_type = htons(ETHERTYPE_IP);
printf("************* %s We got ARP ******************\n", __func__);
uint8_t* newPkt = (uint8_t*) malloc_or_die(len+14);
newPkt = create_ethernet_frame(ethHdr, payload, len);
//pad the frame somehow?
//now we send it using sr_integ_low_level_output
if((sr_integ_low_level_output(sr, newPkt, len+14, interface)) == 0){
printf("********** %s successfully sent a packet\n", __func__);
rtn = 1;
}
else {
printf("\n @@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@ %s sending packet failed?\n", __func__);
rtn = 0;
}
return 1;
}
//else we need to send off an arp request
printf("************* %s send ARP ******************\n", __func__);
rtn = send_arp_request(sr, interface, payload, dstIP, len);
if(rtn == 1){
return 2;
}
else{
return 0;
}
return 0;
}
//need a send_arp_request fuction that also queues the raw ether packet ready to send
int send_arp_request(struct sr_instance * sr, char* iface, uint8_t* payload, uint32_t dstIP, unsigned int len){
// create an ARP request bound for 0xffffff/dstIP, source as in interface
// add a send_list entry for payload, dstIP, now
printf("************ %s so sending ARP request\n", __func__);
time_t now = time(NULL);
int rtn = 0;
struct send_list* new = (struct send_list*)malloc_or_die(sizeof(struct send_list));
struct sr_router* sub = (struct sr_router*)sr_get_subsystem(sr);
struct sr_arphdr* tmparp = (struct sr_arphdr*) malloc_or_die(28*sizeof(uint8_t));
struct sr_vns_if* intf = sr_get_interface(sr, iface);
int arp_pkt_len;
arp_pkt_len = ETH_HDR_LEN + sizeof(struct sr_arphdr);
//make the ARP header for an ARP request
tmparp->ar_hrd = htons(ARPHDR_ETHER);
tmparp->ar_pro = htons(ETHERTYPE_IP);
tmparp->ar_op = htons(ARP_REQUEST);
tmparp->ar_hln = 6;
tmparp->ar_pln = 4;
mac_broadcast(tmparp->ar_tha);
tmparp->ar_tip = dstIP;
mac_copy(intf->addr, tmparp->ar_sha);
tmparp->ar_sip = intf->ip;
struct sr_ethernet_hdr* tmpHdr = (struct sr_ethernet_hdr*) malloc_or_die(14* sizeof(uint8_t));
mac_copy(tmparp->ar_tha, tmpHdr->ether_dhost);
mac_copy(tmparp->ar_sha, tmpHdr->ether_shost);
tmpHdr->ether_type = htons(ETHERTYPE_ARP);
uint8_t* newPkt = (uint8_t*) malloc_or_die(arp_pkt_len);
newPkt = create_ethernet_frame(tmpHdr, tmparp, 28);
//pad the frame somehow?
//now we send it using sr_integ_low_level_output
printf("£££££££££££££££££££££££££££££££££\n");
if((sr_integ_low_level_output(sr, newPkt, (unsigned int)arp_pkt_len, iface)) == 0){
printf("********** %s successfully sent an ARP request\n", __func__);
rtn = 1;
}
else {
printf("\n @@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@ %s sending ARP request failed?\n", __func__);
rtn = 0;
}
//before leaving make an entry in the send list
send_list_add(sub->waitSend, dstIP, payload, len, iface, ETHERTYPE_ARP, now);
return rtn;
}
//which in turn means we need a structure to store the pending requests
#if 1
void arp_cache_cleanup(struct sr_instance *sr){
//struct sr_router* sub = (struct sr_router*)sr_get_subsystem(sr); /*garbage collect */
while (1) {
assert(sr);
struct sr_router* sub = (struct sr_router*)sr_get_subsystem(sr);
assert(sub);
struct timespec delay, remains;
delay.tv_sec = ARP_GC_INTERVAL_S;
delay.tv_nsec = 0;
int done = 1;
int count;
/* so what do we need to do to garbage collect?*/
done = nanosleep(&delay, &remains);
if(done == 0) {
count = get_cache_length(sr, 1);
pthread_mutex_lock(sub->lock);
printf("@ * GARBAGE COLLECTION * @\n");
time_t now = time(NULL);
//time_t then;
struct arp_cache_list* step;
struct arp_cache_list* prev;
//struct arp_cache_list* tmp;
step = sub->arp_cache;
prev = sub->arp_cache;
while(step != NULL){
if (difftime(now, sub->arp_cache->created) > ARP_TIMEOUT_INTERVAL){
printf("***** $$$$ ***** %s: We had %d ARP entries and current one timed out\n",__func__, count);
if(count >1){
prev->next = step->next; //remove the link to current
count--;
}
else{
printf("@@@@ ******** KILL THE CACHE\n");
count = 0;
sub->arp_cache = NULL;
}
}
else {
printf("Nothing to delete in ARP cache\n");
prev = step;
}
step = step->next;
}
pthread_mutex_unlock(sub->lock);
done = 1;
}
else
printf("******************\n");
}
}
#endif