-
Notifications
You must be signed in to change notification settings - Fork 0
/
skel.c
368 lines (319 loc) · 8.83 KB
/
skel.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
#include "skel.h"
int interfaces[ROUTER_NUM_INTERFACES];
int get_sock(const char *if_name)
{
int res;
int s = socket(AF_PACKET, SOCK_RAW, 768);
DIE(s == -1, "socket");
struct ifreq intf;
strcpy(intf.ifr_name, if_name);
res = ioctl(s, SIOCGIFINDEX, &intf);
DIE(res, "ioctl SIOCGIFINDEX");
struct sockaddr_ll addr;
memset(&addr, 0x00, sizeof(addr));
addr.sll_family = AF_PACKET;
addr.sll_ifindex = intf.ifr_ifindex;
res = bind(s , (struct sockaddr *)&addr , sizeof(addr));
DIE(res == -1, "bind");
return s;
}
packet* socket_receive_message(int sockfd, packet *m)
{
/*
* Note that "buffer" should be at least the MTU size of the
* interface, eg 1500 bytes
* */
m->len = read(sockfd, m->payload, MAX_LEN);
DIE(m->len == -1, "read");
return m;
}
int send_packet(int sockfd, packet *m)
{
/*
* Note that "buffer" should be at least the MTU size of the
* interface, eg 1500 bytes
* */
int ret;
ret = write(interfaces[sockfd], m->payload, m->len);
DIE(ret == -1, "write");
return ret;
}
int get_packet(packet *m) {
int res;
fd_set set;
FD_ZERO(&set);
while (1) {
for (int i = 0; i < ROUTER_NUM_INTERFACES; i++) {
FD_SET(interfaces[i], &set);
}
res = select(interfaces[ROUTER_NUM_INTERFACES - 1] + 1, &set, NULL, NULL, NULL);
DIE(res == -1, "select");
for (int i = 0; i < ROUTER_NUM_INTERFACES; i++) {
if (FD_ISSET(interfaces[i], &set)) {
socket_receive_message(interfaces[i], m);
m->interface = i;
return 0;
}
}
}
return -1;
}
char *get_interface_ip(int interface)
{
struct ifreq ifr;
if (interface == 0)
sprintf(ifr.ifr_name, "rr-0-1");
else {
sprintf(ifr.ifr_name, "r-%u", interface - 1);
}
ioctl(interfaces[interface], SIOCGIFADDR, &ifr);
return inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr);
}
void get_interface_mac(int interface, uint8_t *mac)
{
struct ifreq ifr;
if (interface == 0)
sprintf(ifr.ifr_name, "rr-0-1");
else {
sprintf(ifr.ifr_name, "r-%u", interface - 1);
}
ioctl(interfaces[interface], SIOCGIFHWADDR, &ifr);
memcpy(mac, ifr.ifr_addr.sa_data, 6);
}
static int hex2num(char c)
{
if (c >= '0' && c <= '9')
return c - '0';
if (c >= 'a' && c <= 'f')
return c - 'a' + 10;
if (c >= 'A' && c <= 'F')
return c - 'A' + 10;
return -1;
}
int hex2byte(const char *hex)
{
int a, b;
a = hex2num(*hex++);
if (a < 0)
return -1;
b = hex2num(*hex++);
if (b < 0)
return -1;
return (a << 4) | b;
}
/**
* hwaddr_aton - Convert ASCII string to MAC address (colon-delimited format)
* @txt: MAC address as a string (e.g., "00:11:22:33:44:55")
* @addr: Buffer for the MAC address (ETH_ALEN = 6 bytes)
* Returns: 0 on success, -1 on failure (e.g., string not a MAC address)
*/
int hwaddr_aton(const char *txt, uint8_t *addr)
{
int i;
for (i = 0; i < 6; i++) {
int a, b;
a = hex2num(*txt++);
if (a < 0)
return -1;
b = hex2num(*txt++);
if (b < 0)
return -1;
*addr++ = (a << 4) | b;
if (i < 5 && *txt++ != ':')
return -1;
}
return 0;
}
void init(int argc, char *argv[])
{
for (int i = 0; i < argc; ++i) {
printf("Setting up interface: %s\n", argv[i]);
interfaces[i] = get_sock(argv[i]);
}
}
uint16_t icmp_checksum(uint16_t *buffer, uint32_t size)
{
unsigned long cksum=0;
while(size >1)
{
cksum+=*buffer++;
size -=sizeof(unsigned short);
}
if(size )
{
cksum += *(unsigned short*)buffer;
}
cksum = (cksum >> 16) + (cksum & 0xffff);
cksum += (cksum >>16);
return (uint16_t)(~cksum);
}
uint16_t ip_checksum(void* vdata,size_t length) {
// Cast the data pointer to one that can be indexed.
char* data=(char*)vdata;
// Initialise the accumulator.
uint64_t acc=0xffff;
// Handle any partial block at the start of the data.
unsigned int offset=((uintptr_t)data)&3;
if (offset) {
size_t count=4-offset;
if (count>length) count=length;
uint32_t word=0;
memcpy(offset+(char*)&word,data,count);
acc+=ntohl(word);
data+=count;
length-=count;
}
// Handle any complete 32-bit blocks.
char* data_end=data+(length&~3);
while (data!=data_end) {
uint32_t word;
memcpy(&word,data,4);
acc+=ntohl(word);
data+=4;
}
length&=3;
// Handle any partial block at the end of the data.
if (length) {
uint32_t word=0;
memcpy(&word,data,length);
acc+=ntohl(word);
}
// Handle deferred carries.
acc=(acc&0xffffffff)+(acc>>32);
while (acc>>16) {
acc=(acc&0xffff)+(acc>>16);
}
// If the data began at an odd byte address
// then reverse the byte order to compensate.
if (offset&1) {
acc=((acc&0xff00)>>8)|((acc&0x00ff)<<8);
}
// Return the checksum in network byte order.
return htons(~acc);
}
void build_ethhdr(struct ether_header *eth_hdr, uint8_t *sha, uint8_t *dha, unsigned short type)
{
memcpy(eth_hdr->ether_dhost, dha, ETH_ALEN);
memcpy(eth_hdr->ether_shost, sha, ETH_ALEN);
eth_hdr->ether_type = type;
}
void send_icmp(uint32_t daddr, uint32_t saddr, uint8_t *sha, uint8_t *dha, u_int8_t type, u_int8_t code, int interface, int id, int seq)
{
struct ether_header eth_hdr;
struct iphdr ip_hdr;
struct icmphdr icmp_hdr = {
.type = type,
.code = code,
.checksum = 0,
.un.echo = {
.id = id,
.sequence = seq,
}
};
packet packet;
void *payload;
build_ethhdr(ð_hdr, sha, dha, htons(ETHERTYPE_IP));
/* No options */
ip_hdr.version = 4;
ip_hdr.ihl = 5;
ip_hdr.tos = 0;
ip_hdr.protocol = IPPROTO_ICMP;
ip_hdr.tot_len = htons(sizeof(struct iphdr) + sizeof(struct icmphdr));
ip_hdr.id = htons(1);
ip_hdr.frag_off = 0;
ip_hdr.ttl = 64;
ip_hdr.check = 0;
ip_hdr.daddr = daddr;
ip_hdr.saddr = saddr;
ip_hdr.check = ip_checksum(&ip_hdr, sizeof(struct iphdr));
icmp_hdr.checksum = icmp_checksum((uint16_t *)&icmp_hdr, sizeof(struct icmphdr));
payload = packet.payload;
memcpy(payload, ð_hdr, sizeof(struct ether_header));
payload += sizeof(struct ether_header);
memcpy(payload, &ip_hdr, sizeof(struct iphdr));
payload += sizeof(struct iphdr);
memcpy(payload, &icmp_hdr, sizeof(struct icmphdr));
packet.len = sizeof(struct ether_header) + sizeof(struct iphdr) + sizeof(struct icmphdr);
send_packet(interface, &packet);
}
void send_icmp_error(uint32_t daddr, uint32_t saddr, uint8_t *sha, uint8_t *dha, u_int8_t type, u_int8_t code, int interface)
{
struct ether_header eth_hdr;
struct iphdr ip_hdr;
struct icmphdr icmp_hdr = {
.type = type,
.code = code,
.checksum = 0,
};
packet packet;
void *payload;
build_ethhdr(ð_hdr, sha, dha, htons(ETHERTYPE_IP));
/* No options */
ip_hdr.version = 4;
ip_hdr.ihl = 5;
ip_hdr.tos = 0;
ip_hdr.protocol = IPPROTO_ICMP;
ip_hdr.tot_len = htons(sizeof(struct iphdr) + sizeof(struct icmphdr));
ip_hdr.id = htons(1);
ip_hdr.frag_off = 0;
ip_hdr.ttl = 64;
ip_hdr.check = 0;
ip_hdr.daddr = daddr;
ip_hdr.saddr = saddr;
ip_hdr.check = ip_checksum(&ip_hdr, sizeof(struct iphdr));
icmp_hdr.checksum = icmp_checksum((uint16_t *)&icmp_hdr, sizeof(struct icmphdr));
payload = packet.payload;
memcpy(payload, ð_hdr, sizeof(struct ether_header));
payload += sizeof(struct ether_header);
memcpy(payload, &ip_hdr, sizeof(struct iphdr));
payload += sizeof(struct iphdr);
memcpy(payload, &icmp_hdr, sizeof(struct icmphdr));
packet.len = sizeof(struct ether_header) + sizeof(struct iphdr) + sizeof(struct icmphdr);
send_packet(interface, &packet);
}
void send_arp(uint32_t daddr, uint32_t saddr, struct ether_header *eth_hdr, int interface, uint16_t arp_op)
{
struct arp_header arp_hdr;
packet packet;
arp_hdr.htype = htons(ARPHRD_ETHER);
arp_hdr.ptype = htons(2048);
arp_hdr.op = arp_op;
arp_hdr.hlen = 6;
arp_hdr.plen = 4;
memcpy(arp_hdr.sha, eth_hdr->ether_shost, 6);
memcpy(arp_hdr.tha, eth_hdr->ether_dhost, 6);
arp_hdr.spa = saddr;
arp_hdr.tpa = daddr;
memset(packet.payload, 0, 1600);
memcpy(packet.payload, eth_hdr, sizeof(struct ethhdr));
memcpy(packet.payload + sizeof(struct ethhdr), &arp_hdr, sizeof(struct arp_header));
packet.len = sizeof(struct arp_header) + sizeof(struct ethhdr);
send_packet(interface, &packet);
}
struct arp_header* parse_arp(void *buffer)
{
struct arp_header *arp_hdr;
struct ether_header *eth_hdr;
eth_hdr = (struct ether_header *)buffer;
if (ntohs(eth_hdr->ether_type) == ETHERTYPE_ARP) {
arp_hdr = (struct arp_header *)(buffer + sizeof(struct ether_header));
return arp_hdr;
} else
return NULL;
}
struct icmphdr * parse_icmp(void *buffer)
{
struct ether_header *eth_hdr;
struct iphdr *ip_hdr;
eth_hdr = (struct ether_header *)buffer;
if (ntohs(eth_hdr->ether_type) == ETHERTYPE_IP) {
ip_hdr = (struct iphdr *)(buffer + sizeof(struct ether_header));
if (ip_hdr->protocol == 1) {
struct icmphdr *icmp_hdr;
icmp_hdr = (struct icmphdr *)(buffer + sizeof(struct iphdr) + sizeof(struct ether_header));
return icmp_hdr;
} else
return NULL;
} else
return NULL;
}