-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrouter.c
More file actions
402 lines (307 loc) · 12.7 KB
/
router.c
File metadata and controls
402 lines (307 loc) · 12.7 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include "protocols.h"
#include "queue.h"
#include "lib.h"
#include "define.h"
struct arp_table_entry *arp_entrypint(uint32_t IPv4);
void process_arp_packet(char *pkt_data, size_t pkt_len, size_t idx_interface);
struct route_table_entry *rtable = NULL;
int rtable_len = 0;
struct arp_table_entry *arp_table = NULL;
int arp_table_len = 0;
queue arp_request_queue;
void renew(uint32_t ip, uint8_t *mac) {
for (size_t i = 0; i < arp_table_len; i++) {
if (arp_table[i].ip == ip) {
memcpy(arp_table[i].mac, mac, 6);
return;
}
}
if (arp_table_len < 100) {
arp_table[arp_table_len].ip = ip;
memcpy(arp_table[arp_table_len].mac, mac, 6);
arp_table_len++;
} else {
for (size_t i = 1; i < arp_table_len; i++) {
arp_table[i - 1] = arp_table[i];
}
arp_table[arp_table_len - 1].ip = ip;
memcpy(arp_table[arp_table_len - 1].mac, mac, 6);
}
}
struct route_table_entry *get_best_route(uint32_t dest_ip) {
struct route_table_entry *best = NULL;
uint32_t max_mask = 0;
for (int i = 0; i < rtable_len; i++) {
if ((dest_ip & rtable[i].mask) == rtable[i].prefix) {
if (rtable[i].mask > max_mask) {
best = &rtable[i];
max_mask = rtable[i].mask;
if (max_mask == 0xFFFFFFFF) {
return best;
}
}
}
}
return best;
}
struct arp_table_entry *arp_entrypint(uint32_t IPv4) {
struct arp_table_entry *entry = NULL;
for (int i = 0; i < arp_table_len; i++) {
if (arp_table[i].ip == IPv4) {
entry = &arp_table[i];
break;
}
}
return entry;
}
int what_router(uint32_t ip) {
if (ip == INADDR_NONE) {
return 0;
}
int found_match = 0;
for (int i = 0; i < ROUTER_NUM_INTERFACES; i++) {
char *iface_ip_str = get_interface_ip(i);
if (iface_ip_str == NULL) {
continue;
}
struct in_addr addr;
if (inet_pton(AF_INET, iface_ip_str, &addr) != 1) {
continue;
}
uint32_t iface_ip = addr.s_addr;
if (ip == iface_ip) {
found_match = 1;
break;
}
}
if (found_match) {
return 1;
} else {
return 0;
}
}
void send_arp_request(uint32_t dest_ip, size_t interface) {
size_t frame_size = sizeof(struct ether_hdr) + sizeof(struct arp_hdr);
struct ether_hdr *eth_hdr = (struct ether_hdr *)malloc(frame_size);
struct arp_hdr *arp_hdr = (struct arp_hdr *)((char *)eth_hdr + sizeof(struct ether_hdr));
memset(eth_hdr, 0, frame_size);
eth_hdr->ethr_type = htons(ARPETH);
get_interface_mac(interface, eth_hdr->ethr_shost);
memset(eth_hdr->ethr_dhost, 0xFF, 6);
arp_hdr->hw_type = htons(HRD);
arp_hdr->proto_type = htons(IPv4ETH);
arp_hdr->hw_len = 6;
arp_hdr->proto_len = 4;
arp_hdr->opcode = htons(REQARP);
get_interface_mac(interface, arp_hdr->shwa);
arp_hdr->sprotoa = inet_addr(get_interface_ip(interface));
memset(arp_hdr->thwa, 0, 6);
arp_hdr->tprotoa = dest_ip;
send_to_link(frame_size, (char *)eth_hdr, interface);
free(eth_hdr);
}
void send_icmp_dest_unreachable(uint32_t src_ip, uint32_t dst_ip, size_t interface) {
size_t frame_size = sizeof(struct ether_hdr) + sizeof(struct ip_hdr) + sizeof(struct icmp_hdr);
struct ether_hdr *eth_hdr = (struct ether_hdr *)malloc(frame_size);
struct ip_hdr *ip_hdr = (struct ip_hdr *)(eth_hdr + 1);
struct icmp_hdr *icmp_hdr = (struct icmp_hdr *)(ip_hdr + 1);
get_interface_mac(interface, eth_hdr->ethr_shost);
memset(eth_hdr->ethr_dhost, 0, 6);
eth_hdr->ethr_type = htons(IPv4ETH);
ip_hdr->ihl = 5;
ip_hdr->ver = 4;
ip_hdr->tos = 0;
ip_hdr->tot_len = htons(sizeof(struct ip_hdr) + sizeof(struct icmp_hdr));
ip_hdr->id = 0;
ip_hdr->frag = 0;
ip_hdr->ttl = 64;
ip_hdr->proto = IPPROTO_ICMP;
ip_hdr->source_addr = dst_ip;
ip_hdr->dest_addr = src_ip;
ip_hdr->checksum = 0;
ip_hdr->checksum = htons(checksum((uint16_t *)ip_hdr, sizeof(struct ip_hdr)));
icmp_hdr->mtype = NODEST;
icmp_hdr->mcode = 0;
icmp_hdr->check = 0;
icmp_hdr->check = htons(checksum((uint16_t *)icmp_hdr, sizeof(struct icmp_hdr)));
send_to_link(frame_size, (char *)eth_hdr, interface);
free(eth_hdr);
}
void send_icmp_time_exceeded(uint32_t src_ip, uint32_t dst_ip, size_t interface) {
size_t frame_size = sizeof(struct ether_hdr) + sizeof(struct ip_hdr) + sizeof(struct icmp_hdr);
struct ether_hdr *eth_hdr = (struct ether_hdr *)malloc(frame_size);
struct ip_hdr *ip_hdr = (struct ip_hdr *)(eth_hdr + 1);
struct icmp_hdr *icmp_hdr = (struct icmp_hdr *)(ip_hdr + 1);
get_interface_mac(interface, eth_hdr->ethr_shost);
memset(eth_hdr->ethr_dhost, 0, 6); // Destination MAC will be filled by next hop lookup if needed, but here it's locally generated so likely needs valid L2 destination?
// Wait, send_icmp_dest_unreachable sets dhost to 0.
// And send_to_link sends it.
// But who fills the destination MAC?
// In this simple implementation, maybe standard behavior is expected, but let's follow send_icmp_dest_unreachable pattern.
eth_hdr->ethr_type = htons(IPv4ETH);
ip_hdr->ihl = 5;
ip_hdr->ver = 4;
ip_hdr->tos = 0;
ip_hdr->tot_len = htons(sizeof(struct ip_hdr) + sizeof(struct icmp_hdr));
ip_hdr->id = 0;
ip_hdr->frag = 0;
ip_hdr->ttl = 64;
ip_hdr->proto = IPPROTO_ICMP;
ip_hdr->source_addr = dst_ip; // Router IP (or interface IP)
ip_hdr->dest_addr = src_ip; // Original sender
ip_hdr->checksum = 0;
ip_hdr->checksum = htons(checksum((uint16_t *)ip_hdr, sizeof(struct ip_hdr)));
icmp_hdr->mtype = 11; // Time Exceeded
icmp_hdr->mcode = 0; // TTL exceeded in transit
icmp_hdr->check = 0;
icmp_hdr->check = htons(checksum((uint16_t *)icmp_hdr, sizeof(struct icmp_hdr)));
send_to_link(frame_size, (char *)eth_hdr, interface);
free(eth_hdr);
}
void process_arp_packet(char *buf, size_t len, size_t recv_iface) {
struct ether_hdr *eth_hdr = (struct ether_hdr *)buf;
struct arp_hdr *arp_pkt = (struct arp_hdr *)(buf + sizeof(struct ether_hdr));
uint16_t opcode = ntohs(arp_pkt->opcode);
if (opcode == REPARP) {
renew(arp_pkt->sprotoa, arp_pkt->shwa);
queue temp_queue = create_queue();
while (!queue_empty(arp_request_queue)) {
pkt *pending = (pkt *)queue_deq(arp_request_queue);
struct ether_hdr *pending_eth = (struct ether_hdr *)pending->payload;
struct ip_hdr *pending_ip = (struct ip_hdr *)(pending->payload + sizeof(struct ether_hdr));
struct route_table_entry *br = get_best_route(pending_ip->dest_addr);
if (br && (br->next_hop == arp_pkt->sprotoa)) {
memcpy(pending_eth->ethr_dhost, arp_pkt->shwa, 6);
get_interface_mac(br->interface, pending_eth->ethr_shost);
send_to_link(pending->len, pending->payload, br->interface);
free(pending->payload);
free(pending);
} else {
queue_enq(temp_queue, pending);
}
}
while (!queue_empty(temp_queue)) {
pkt *p = (pkt *)queue_deq(temp_queue);
queue_enq(arp_request_queue, p);
}
}
else if (opcode == REQARP) {
for (int i = 0; i < ROUTER_NUM_INTERFACES; i++) {
uint32_t iface_ip = inet_addr(get_interface_ip(i));
if (iface_ip == arp_pkt->tprotoa) {
char reply[MAX_PACKET_LEN];
memset(reply, 0, MAX_PACKET_LEN);
struct ether_hdr *eth_reply = (struct ether_hdr *)reply;
struct arp_hdr *arp_reply = (struct arp_hdr *)(reply + sizeof(struct ether_hdr));
get_interface_mac(i, eth_reply->ethr_shost);
memcpy(eth_reply->ethr_dhost, eth_hdr->ethr_shost, 6);
eth_reply->ethr_type = htons(ARPETH);
arp_reply->hw_type = htons(HRD);
arp_reply->proto_type = htons(IPv4ETH);
arp_reply->hw_len = 6;
arp_reply->proto_len = 4;
arp_reply->opcode = htons(REPARP);
get_interface_mac(i, arp_reply->shwa);
arp_reply->sprotoa = arp_pkt->tprotoa;
memcpy(arp_reply->thwa, arp_pkt->shwa, 6);
arp_reply->tprotoa = arp_pkt->sprotoa;
send_to_link(sizeof(struct ether_hdr) + sizeof(struct arp_hdr), reply, i);
}
}
}
}
void update_ip_ttl_and_checksum(struct ip_hdr *ip) {
ip->ttl--;
ip->checksum = 0;
ip->checksum = htons(checksum((uint16_t *)ip, sizeof(struct ip_hdr)));
}
void process_icmp_echo(struct ether_hdr *eth_hdr, struct ip_hdr *ip_hdr, struct icmp_hdr *icmp_hdr, char *packet_data, size_t interface_index) {
uint16_t ip_header_len = sizeof(struct ip_hdr);
uint16_t icmp_header_len = sizeof(struct icmp_hdr);
uint16_t payload_size = ntohs(ip_hdr->tot_len) - ip_header_len - icmp_header_len;
icmp_hdr->mtype = REPLY;
icmp_hdr->mcode = 0;
uint32_t original_src = ip_hdr->source_addr;
ip_hdr->source_addr = ip_hdr->dest_addr;
ip_hdr->dest_addr = original_src;
memcpy(eth_hdr->ethr_dhost, eth_hdr->ethr_shost, 6);
get_interface_mac(interface_index, eth_hdr->ethr_shost);
ip_hdr->checksum = 0;
ip_hdr->checksum = htons(checksum((uint16_t *)ip_hdr, ip_header_len));
icmp_hdr->check = 0;
icmp_hdr->check = htons(checksum((uint16_t *)icmp_hdr, icmp_header_len + payload_size));
size_t total_len = sizeof(struct ether_hdr) + ntohs(ip_hdr->tot_len);
send_to_link(total_len, packet_data, interface_index);
}
int main(int argc, char *argv[]) {
char packet[MAX_PACKET_LEN];
init(argv + 2, argc - 2);
rtable_len = 100000;
rtable = malloc(rtable_len * sizeof(struct route_table_entry));
arp_table = malloc(100 * sizeof(struct arp_table_entry));
arp_request_queue = create_queue();
arp_table_len = 0;
rtable_len = read_rtable(argv[1], rtable);
while (1) {
size_t pkt_len, iface = recv_from_any_link(packet, &pkt_len);
struct ether_hdr *eth = (struct ether_hdr *)packet;
uint16_t ether_type = ntohs(eth->ethr_type);
if (ether_type == IPv4ETH) {
struct ip_hdr *ip = (struct ip_hdr *)(packet + sizeof(struct ether_hdr));
struct icmp_hdr *icmp = (struct icmp_hdr *)(ip + 1);
uint16_t orig_cksum = ip->checksum;
ip->checksum = 0;
if (orig_cksum != htons(checksum((uint16_t *)ip, sizeof(struct ip_hdr)))) {
continue;
}
if (what_router(ip->dest_addr)) {
if (ip->proto == IPPROTO_ICMP && icmp->mtype == ECHO) {
process_icmp_echo(eth, ip, icmp, packet, iface);
}
continue;
}
struct route_table_entry *route = get_best_route(ip->dest_addr);
if (!route) {
send_icmp_dest_unreachable(ip->source_addr, ip->dest_addr, iface);
continue;
}
if (ip->ttl <= 1) {
send_icmp_time_exceeded(ip->source_addr, ip->dest_addr, iface);
continue;
}
update_ip_ttl_and_checksum(ip);
struct arp_table_entry *arp_entry = arp_entrypint(route->next_hop);
if (!arp_entry) {
pkt *pending = malloc(sizeof(pkt));
pending->payload = malloc(pkt_len);
memcpy(pending->payload, packet, pkt_len);
pending->len = pkt_len;
pending->interface = iface;
if (queue_empty(arp_request_queue)) {
queue_enq(arp_request_queue, pending);
send_arp_request(route->next_hop, route->interface);
} else {
queue_enq(arp_request_queue, pending);
send_arp_request(route->next_hop, route->interface);
}
continue;
}
if (arp_entry) {
uint8_t *mac_dst = arp_entry->mac;
uint8_t *mac_src = eth->ethr_shost;
memcpy(eth->ethr_dhost, mac_dst, sizeof(arp_entry->mac));
get_interface_mac(route->interface, mac_src);
send_to_link(pkt_len, packet, route->interface);
}
}
else if (ether_type == ARPETH) {
process_arp_packet(packet, pkt_len, iface);
}
}
return 0;
}