-
Notifications
You must be signed in to change notification settings - Fork 20
/
xdp_prog.c
672 lines (529 loc) · 19.7 KB
/
xdp_prog.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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
#include <linux/bpf.h>
#include <bpf_helpers.h>
#include <linux/if_ether.h>
#include <linux/ip.h>
#include <linux/tcp.h>
#include <linux/udp.h>
#include <linux/icmp.h>
#include <linux/in.h>
#include <inttypes.h>
#include "xdp_prog.h"
#include "xdpfwd.h"
#include "csum.h"
//#define DEBUG
#ifdef DEBUG
#define bpf_printk(fmt, ...) \
({ \
char ____fmt[] = fmt; \
bpf_trace_printk(____fmt, sizeof(____fmt), \
##__VA_ARGS__); \
})
#endif
struct bpf_map_def SEC("maps") forward_map =
{
.type = BPF_MAP_TYPE_HASH,
.key_size = sizeof(struct forward_key),
.value_size = sizeof(struct forward_info),
.max_entries = MAXRULES
};
struct bpf_map_def SEC("maps") tcp_map =
{
.type = BPF_MAP_TYPE_LRU_HASH,
.key_size = sizeof(struct port_key),
.value_size = sizeof(struct connection),
.max_entries = (MAXRULES * (MAXPORT - (MINPORT - 1)))
};
struct bpf_map_def SEC("maps") udp_map =
{
.type = BPF_MAP_TYPE_LRU_HASH,
.key_size = sizeof(struct port_key),
.value_size = sizeof(struct connection),
.max_entries = (MAXRULES * (MAXPORT - (MINPORT - 1)))
};
struct bpf_map_def SEC("maps") connection_map =
{
.type = BPF_MAP_TYPE_LRU_HASH,
.key_size = sizeof(struct conn_key),
.value_size = sizeof(uint16_t),
.max_entries = MAXCONNECTIONS
};
/**
* Swaps the Ethernet source and destination MAC addresses.
*
* @param eth A pointer to the Ethernet header (ethhdr) struct that points to the Ethernet header within the packet.
*
* @return void
*/
static __always_inline void swapeth(struct ethhdr *eth)
{
uint8_t tmp[ETH_ALEN];
memcpy(&tmp, eth->h_source, ETH_ALEN);
memcpy(eth->h_source, eth->h_dest, ETH_ALEN);
memcpy(eth->h_dest, &tmp, ETH_ALEN);
}
/**
* Forwards an IPv4 packet from or back to the client.
*
* @param info A pointer to a forward_info struct that represents what forwarding rule we're sending to. If NULL, will indicate we're sending back to the client.
* @param conn A pointer to a connection struct that represents the connection we're forwarding to or back to.
* @param ctx A pointer to the xdp_md struct containing all packet information.
*
* @return XDP_TX (sends packet back out TX path).
*/
static __always_inline int forwardpacket4(struct forward_info *info, struct connection *conn, struct xdp_md *ctx)
{
// Redefine packet and check headers.
void *data = (void *)(long)ctx->data;
void *data_end = (void *)(long)ctx->data_end;
struct ethhdr *eth = data;
if (eth + 1 > (struct ethhdr *)data_end)
{
return XDP_DROP;
}
struct iphdr *iph = data + sizeof(struct ethhdr);
if (iph + 1 > (struct iphdr *)data_end)
{
return XDP_DROP;
}
// Swap ethernet source and destination MAC addresses.
swapeth(eth);
// Define ICMP header, but set it to NULL.
struct icmphdr *icmph = NULL;
if (iph->protocol == IPPROTO_ICMP)
{
icmph = data + sizeof(struct ethhdr) + (iph->ihl * 4);
if (icmph + 1 > (struct icmphdr *)data_end)
{
return XDP_DROP;
}
}
// Swap IP addresses.
uint32_t oldsrcaddr = iph->saddr;
uint32_t olddestaddr = iph->daddr;
iph->saddr = iph->daddr;
if (info)
{
iph->daddr = info->destaddr;
}
else
{
if (!icmph)
{
iph->daddr = conn->clientaddr;
}
}
// Handle ICMP protocol.
if (icmph)
{
if (info)
{
// We'll want to add the client's unsigned 32-bit (4 bytes) IP address to the ICMP data so we know where to send it when it replies back.
// First, let's add four bytes to the packet.
if (bpf_xdp_adjust_tail(ctx, (int)sizeof(uint32_t)))
{
return XDP_DROP;
}
// We need to redefine packet and check headers again.
data = (void *)(long)ctx->data;
data_end = (void *)(long)ctx->data_end;
eth = data;
if (eth + 1 > (struct ethhdr *)data_end)
{
return XDP_DROP;
}
iph = data + sizeof(struct ethhdr);
if (iph + 1 > (struct iphdr *)data_end)
{
return XDP_DROP;
}
icmph = data + sizeof(struct ethhdr) + (iph->ihl * 4);
if (icmph + 1 > (struct icmphdr *)data_end)
{
return XDP_DROP;
}
// Now let's add the new data.
// Unfortunately, we can't start from the packet end (data_end) pointer. Therefore, we must calculate the length of the packet and use the data pointer. Thanks for the help, Srivats! (https://lore.kernel.org/bpf/CANzUK5-g9wLiwUF88em4uVzMja_aR4xj9yzMS_ZObNKjvX6C6g@mail.gmail.com/)
unsigned int len = (ctx->data_end - ctx->data);
if (data + len > data_end)
{
return XDP_DROP;
}
unsigned int off = (len - sizeof(uint32_t)) & 0x3fff;
uint32_t *icmpdata = data + off;
if (icmpdata + 1 > (uint32_t *)data_end)
{
return XDP_DROP;
}
memcpy(icmpdata, &conn->clientaddr, sizeof(uint32_t));
// We'll want to add four bytes to the IP header.
iph->tot_len = htons(ntohs(iph->tot_len) + sizeof(uint32_t));
// Recalculate ICMP checksum.
icmph->checksum = csum_diff4(0, conn->clientaddr, icmph->checksum);
}
else
{
// When sending packets back, we'll want to get the client IP address from the ICMP data (last four bytes).
// First ensure the ICMP data is enough.
if (icmph + sizeof(uint32_t) > (struct icmphdr *)data_end)
{
return XDP_PASS;
}
// Now access the data.
unsigned int len = (ctx->data_end - ctx->data);
if (data + len > data_end)
{
return XDP_DROP;
}
unsigned int off = (len - sizeof(uint32_t)) & 0x3fff;
uint32_t *clientaddr = data + off;
if (clientaddr + 1 > (uint32_t *)data_end)
{
return XDP_DROP;
}
iph->daddr = *clientaddr;
// Now we'll want to remove the additional four bytes we added when forwarding.
if (bpf_xdp_adjust_tail(ctx, 0 - (int)sizeof(uint32_t)))
{
return XDP_DROP;
}
// We need to redefine packet and check headers again.
data = (void *)(long)ctx->data;
data_end = (void *)(long)ctx->data_end;
eth = data;
if (eth + 1 > (struct ethhdr *)data_end)
{
return XDP_DROP;
}
iph = data + sizeof(struct ethhdr);
if (iph + 1 > (struct iphdr *)data_end)
{
return XDP_DROP;
}
icmph = data + sizeof(struct ethhdr) + (iph->ihl * 4);
if (icmph + 1 > (struct icmphdr *)data_end)
{
return XDP_DROP;
}
// Remove four bytes from the IP header's total length.
iph->tot_len = htons(ntohs(iph->tot_len) - sizeof(uint32_t));
// Recalculate ICMP checksum.
icmph->checksum = csum_diff4(iph->daddr, 0, icmph->checksum);
}
}
// Handle protocol.
if (iph->protocol == IPPROTO_TCP)
{
struct tcphdr *tcph = data + sizeof(struct ethhdr) + (iph->ihl * 4);
// Check header.
if (tcph + 1 > (struct tcphdr *)data_end)
{
return XDP_DROP;
}
// Handle ports.
uint16_t oldsrcport = tcph->source;
uint16_t olddestport = tcph->dest;
if (info)
{
tcph->source = conn->port;
tcph->dest = info->destport;
}
else
{
tcph->source = conn->bindport;
tcph->dest = conn->clientport;
}
// Recalculate checksum.
tcph->check = csum_diff4(olddestaddr, iph->daddr, tcph->check);
tcph->check = csum_diff4(oldsrcaddr, iph->saddr, tcph->check);
tcph->check = csum_diff4(oldsrcport, tcph->source, tcph->check);
tcph->check = csum_diff4(olddestport, tcph->dest, tcph->check);
#ifdef DEBUG
bpf_printk("Forward Port => %" PRIu16 ":%" PRIu16 ".\n", ntohs(tcph->source), ntohs(tcph->dest));
#endif
}
else if (iph->protocol == IPPROTO_UDP)
{
struct udphdr *udph = data + sizeof(struct ethhdr) + (iph->ihl * 4);
// Check header.
if (udph + 1 > (struct udphdr *)data_end)
{
return XDP_DROP;
}
// Handle ports.
uint16_t oldsrcport = udph->source;
uint16_t olddestport = udph->dest;
if (info)
{
udph->source = conn->port;
udph->dest = info->destport;
}
else
{
udph->source = conn->bindport;
udph->dest = conn->clientport;
}
// Recalculate checksum.
udph->check = csum_diff4(olddestaddr, iph->daddr, udph->check);
udph->check = csum_diff4(oldsrcaddr, iph->saddr, udph->check);
udph->check = csum_diff4(oldsrcport, udph->source, udph->check);
udph->check = csum_diff4(olddestport, udph->dest, udph->check);
#ifdef DEBUG
bpf_printk("Forward Port => %" PRIu16 ":%" PRIu16 ".\n", ntohs(udph->source), ntohs(udph->dest));
#endif
}
// Recalculate IP checksum and send packet back out TX path.
update_iph_checksum(iph);
#ifdef DEBUG
bpf_printk("Forward IP => %" PRIu32 ":%" PRIu32 " (%" PRIu8")\n", iph->saddr, iph->daddr, iph->protocol);
#endif
return XDP_TX;
}
SEC("xdp_prog")
int xdp_prog_main(struct xdp_md *ctx)
{
// Initialize packet information.
void *data = (void *)(long)ctx->data;
void *data_end = (void *)(long)ctx->data_end;
// Initialize Ethernet header.
struct ethhdr *eth = data;
// Check Ethernet header.
if (unlikely(eth + 1 > (struct ethhdr *)data_end))
{
return XDP_DROP;
}
// If not IPv4, pass down network stack. Will be adding IPv6 support later on.
if (eth->h_proto != htons(ETH_P_IP))
{
return XDP_PASS;
}
// Initialize IP header.
struct iphdr *iph = data + sizeof(struct ethhdr);
// Check IP header.
if (unlikely(iph + 1 > (struct iphdr *)data_end))
{
return XDP_DROP;
}
// We only support TCP, UDP, and ICMP for forwarding at this moment.
if (iph->protocol != IPPROTO_TCP && iph->protocol != IPPROTO_UDP && iph->protocol != IPPROTO_ICMP)
{
return XDP_PASS;
}
// Get layer-4 protocol information.
struct udphdr *udph = NULL;
struct tcphdr *tcph = NULL;
struct icmphdr *icmph = NULL;
uint16_t portkey = 0;
switch (iph->protocol)
{
case IPPROTO_TCP:
tcph = data + sizeof(struct ethhdr) + (iph->ihl * 4);
if (tcph + 1 > (struct tcphdr *)data_end)
{
return XDP_DROP;
}
break;
case IPPROTO_UDP:
udph = data + sizeof(struct ethhdr) + (iph->ihl * 4);
if (udph + 1 > (struct udphdr *)data_end)
{
return XDP_DROP;
}
break;
case IPPROTO_ICMP:
icmph = data + sizeof(struct ethhdr) + (iph->ihl * 4);
if (icmph + 1 > (struct icmphdr *)data_end)
{
return XDP_DROP;
}
break;
}
portkey = (tcph) ? tcph->dest : (udph) ? udph->dest : 0;
// Choose which map we're using.
struct bpf_map_def *map = (tcph) ? &tcp_map : (udph) ? &udp_map : NULL;
// Construct forward key.
struct forward_key fwdkey = {0};
fwdkey.bindaddr = iph->daddr;
fwdkey.protocol = iph->protocol;
fwdkey.bindport = portkey;
struct forward_info *fwdinfo = bpf_map_lookup_elem(&forward_map, &fwdkey);
if (fwdinfo)
{
#ifdef DEBUG
bpf_printk("Matched forward rule %" PRIu32 ":%" PRIu16 " (%" PRIu8 ").\n", fwdkey.bindaddr, fwdkey.bindport, fwdkey.protocol);
#endif
if (!map && !icmph)
{
return XDP_PASS;
}
uint64_t now = bpf_ktime_get_ns();
// Ensure we aren't actually receiving replies back from the destination address on the same bind and source port. Or ICMP replies.
if (iph->saddr == fwdinfo->destaddr)
{
goto reply;
}
// Check if we have an existing connection.
struct conn_key connkey = {0};
connkey.clientaddr = iph->saddr;
connkey.clientport = (tcph) ? tcph->source : (udph) ? udph->source : 0;
connkey.bindaddr = iph->daddr;
connkey.bindport = portkey;
connkey.protocol = iph->protocol;
// Check for existing connection with UDP/TCP.
if (map)
{
uint16_t *connport = bpf_map_lookup_elem(&connection_map, &connkey);
if (connport)
{
// Now attempt to retrieve connection from port map.
struct port_key pkey = {0};
pkey.bindaddr = iph->daddr;
pkey.destaddr = fwdinfo->destaddr;
pkey.port = *connport;
struct connection *conn = bpf_map_lookup_elem(map, &pkey);
if (conn)
{
// Update connection stats before forwarding packet.
conn->lastseen = now;
conn->count++;
#ifdef DEBUG
bpf_printk("Forwarding packet from existing connection. %" PRIu32 " with count %" PRIu64 "\n", iph->saddr, conn->count);
bpf_printk("VV1 = %" PRIu32 " : %" PRIu16 ".\n", connkey.clientaddr, ntohs(connkey.clientport));
bpf_printk("VV2 = %" PRIu32 " : %" PRIu16 " : %" PRIu8 ".\n", connkey.bindaddr, ntohs(connkey.bindport), connkey.protocol);
#endif
// Forward the packet!
if (conn->clientport == connkey.clientport)
{
return forwardpacket4(fwdinfo, conn, ctx);
}
else
{
bpf_map_delete_elem(map, &pkey);
#ifdef DEBUG
bpf_printk("Somehow found different client port on connection and port maps. Conn map => %" PRIu16 ". Port map => %" PRIu16 ".\n", ntohs(conn->clientport), ntohs(connkey.clientport));
#endif
}
}
}
}
#ifdef DEBUG
bpf_printk("Inserting new connection for %" PRIu32 "\n", iph->saddr);
#endif
uint16_t porttouse = 0;
if (map)
{
uint64_t last = UINT64_MAX;
// Creating the port_key struct outside of the loop and assigning bind address should save some CPU cycles.
struct port_key pkey = {0};
pkey.bindaddr = iph->daddr;
pkey.destaddr = fwdinfo->destaddr;
for (uint16_t i = MINPORT; i <= MAXPORT; i++)
{
pkey.port = htons(i);
struct connection *newconn = bpf_map_lookup_elem(map, &pkey);
if (!newconn)
{
porttouse = i;
break;
}
else
{
// For some reason when trying to divide by any number (such as 1000000000 to get the actual PPS), the BPF verifier doesn't like that.
// Doesn't matter though and perhaps better we don't divide since that's one less calculation to worry about.
uint64_t pps = (newconn->lastseen - newconn->firstseen) / newconn->count;
// We'll want to replace the most inactive connection.
if (last > pps)
{
porttouse = i;
last = pps;
}
}
}
}
#ifdef DEBUG
bpf_printk("Decided to use port %" PRIu16 "\n", porttouse);
#endif
if (porttouse > 0 || icmph)
{
uint16_t port = 0;
if (map)
{
#ifdef DEBUG
struct port_key pkey = {0};
pkey.bindaddr = iph->daddr;
pkey.destaddr = fwdinfo->destaddr;
pkey.port = htons(porttouse);
struct connection *conntodel = bpf_map_lookup_elem(map, &pkey);
if (conntodel)
{
bpf_printk("Deleting connection due to port exhaust (%" PRIu32 ":%" PRIu16 ").\n", conntodel->clientaddr, ntohs(conntodel->clientport));
}
#endif
// Insert information about connection.
struct conn_key nconnkey = {0};
nconnkey.bindaddr = iph->daddr;
nconnkey.bindport = portkey;
nconnkey.clientaddr = iph->saddr;
nconnkey.clientport = connkey.clientport;
nconnkey.protocol = iph->protocol;
port = htons(porttouse);
bpf_map_update_elem(&connection_map, &nconnkey, &port, BPF_ANY);
}
// Insert new connection into port map.
struct port_key npkey = {0};
npkey.bindaddr = iph->daddr;
npkey.destaddr = fwdinfo->destaddr;
npkey.port = port;
struct connection newconn = {0};
newconn.clientaddr = iph->saddr;
newconn.clientport = connkey.clientport;
newconn.firstseen = now;
newconn.lastseen = now;
newconn.count = 1;
newconn.bindport = portkey;
newconn.port = port;
if (map)
{
bpf_map_update_elem(map, &npkey, &newconn, BPF_ANY);
}
#ifdef DEBUG
bpf_printk("New connection: BPort => %" PRIu16 ". Port => %" PRIu16 ". BAddr => %" PRIu32 ".\n", ntohs(newconn.bindport), ntohs(npkey.port), npkey.bindaddr);
#endif
#ifdef DEBUG
bpf_printk("Forwarding packet from new connection for %" PRIu32 "\n", iph->saddr);
#endif
// Finally, forward packet.
return forwardpacket4(fwdinfo, &newconn, ctx);
}
}
else
{
reply:;
// Look for packets coming back from bind addresses.
portkey = (tcph) ? tcph->dest : (udph) ? udph->dest : 0;
if (map)
{
struct port_key pkey = {0};
pkey.bindaddr = iph->daddr;
pkey.destaddr = iph->saddr;
pkey.port = portkey;
// Find out what the client IP is.
struct connection *conn = bpf_map_lookup_elem(map, &pkey);
if (conn)
{
#ifdef DEBUG
bpf_printk("Found connection on %" PRIu16 ". Forwarding back to %" PRIu32 ":%" PRIu16 "\n", ntohs(pkey.port), conn->clientaddr, ntohs(conn->clientport));
#endif
// Now forward packet back to actual client.
return forwardpacket4(NULL, conn, ctx);
}
}
else if (icmph && icmph->type == ICMP_ECHOREPLY)
{
// Handle ICMP replies.
struct connection newconn = {0};
return forwardpacket4(NULL, &newconn, ctx);
}
}
return XDP_PASS;
}
char _license[] SEC("license") = "GPL";