-
Notifications
You must be signed in to change notification settings - Fork 1
/
module.c
638 lines (448 loc) · 13.4 KB
/
module.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
#include <stdbool.h>
#include <linux/if_ether.h>
#include <linux/ip.h>
#include <linux/ipv6.h>
#include <linux/udp.h>
#include <linux/tcp.h>
#include <linux/icmp.h>
#include <linux/icmpv6.h>
#include <linux/in.h>
#include "../headers/bpf_helpers.h"
#include "../headers/bpf_endian.h"
#include "../headers/common.h"
#include "module.h"
struct {
__uint(type, BPF_MAP_TYPE_HASH);
__uint(max_entries, 500000);
__uint(key, 254); // FQDN, host byte order (little endian)
__type(value, __u8); // 1 = banned, 0 = unbanned
} domain_bans SEC(".maps");
struct {
__uint(type, BPF_MAP_TYPE_HASH);
__uint(max_entries, 500000);
__type(key, __u32); // ipv4 address, network byte order (big endian)
__type(value, __u8); // 1 = banned, 0 = unbanned
} ipv4_bans SEC(".maps");
struct {
__uint(type, BPF_MAP_TYPE_LRU_HASH);
__uint(max_entries, 128);
__type(key, __u32); // ipv4 address, network byte order (big endian)
__type(value, __u8); // amount of warnings
} ipv4_warnings SEC(".maps");
struct {
__uint(type, BPF_MAP_TYPE_HASH);
__uint(max_entries, 500000);
__type(key, __u128); // ipv6 address, network byte order (big endian)
__type(value, __u8); // 1 = banned, 0 = unbanned
} ipv6_bans SEC(".maps");
struct {
__uint(type, BPF_MAP_TYPE_LRU_HASH);
__uint(max_entries, 128);
__type(key, __u128); // ipv6 address, network byte order (big endian)
__type(value, __u8); // amount of warnings
} ipv6_warnings SEC(".maps");
struct {
__uint(type, BPF_MAP_TYPE_HASH);
__uint(max_entries, 65535);
__type(key, __u16); // port number, network byte order (big endian)
__type(value, __u8); // 1 = banned, 0 = unbanned
} port_bans SEC(".maps");
static __always_inline bool warn_ipv4(__u32 *address) {
__u8 *warnings_ipv4 = NULL;
warnings_ipv4 = bpf_map_lookup_elem(&ipv4_warnings, address);
if (warnings_ipv4 != NULL) {
__u8 tmp = 0;
tmp = *warnings_ipv4 + 1;
bpf_map_update_elem(&ipv4_warnings, address, &tmp, BPF_ANY);
if (tmp > 16) {
__u8 banned = 1;
bpf_map_update_elem(&ipv4_bans, address, &banned, BPF_ANY);
tmp = 0;
bpf_map_update_elem(&ipv4_warnings, address, &tmp, BPF_ANY);
}
} else {
__u8 tmp = 1;
bpf_map_update_elem(&ipv4_warnings, address, &tmp, BPF_ANY);
}
return true;
}
static __always_inline bool warn_ipv6(__u128 *address) {
__u8 *warnings_ipv6 = NULL;
warnings_ipv6 = bpf_map_lookup_elem(&ipv6_warnings, address);
if (warnings_ipv6 != NULL) {
__u8 tmp = 0;
tmp = *warnings_ipv6 + 1;
bpf_map_update_elem(&ipv6_warnings, address, &tmp, BPF_ANY);
if (tmp > 16) {
__u8 banned = 1;
bpf_map_update_elem(&ipv6_bans, address, &banned, BPF_ANY);
tmp = 0;
bpf_map_update_elem(&ipv6_warnings, address, &tmp, BPF_ANY);
}
} else {
__u8 tmp = 1;
bpf_map_update_elem(&ipv6_warnings, address, &tmp, BPF_ANY);
}
return true;
}
static __always_inline bool is_filtered_ipv4(__u32 *address) {
__u8 *banned = NULL;
banned = bpf_map_lookup_elem(&ipv4_bans, address);
if (banned != NULL && banned != 0) {
return true;
}
return false;
}
static __always_inline bool is_filtered_ipv6(__u128 *address) {
__u8 *banned = NULL;
banned = bpf_map_lookup_elem(&ipv6_bans, address);
if (banned != NULL && banned != 0) {
return true;
}
return false;
}
static __always_inline bool is_filtered_port(__u16 port) {
__u8 *blocked_port = NULL;
blocked_port = bpf_map_lookup_elem(&port_bans, &port);
if (blocked_port != NULL && blocked_port != 0) {
return true;
}
return false;
}
static __always_inline bool is_filtered_icmp_packet(struct icmphdr *icmp_header) {
int type = icmp_header->type;
if (
type == 0 // echo reply
|| type == 1 // reserved
|| type == 2 // reserved
|| type == 4 // source quench
|| type == 6 // deprecated (alternate host address)
|| type == 7 // reserved
|| type == 8 // echo request
|| type == 9 // router advertisement
|| type == 10 // router solicitation
|| type == 13 // timestamp request
|| type == 14 // timestamp reply
|| type == 15 // deprecated (information request)
|| type == 16 // deprecated (information reply)
|| type == 17 // deprecated (address mask request)
|| type == 18 // deprecated (address mask reply)
|| type == 19 // reserved
|| (type >= 20 && type <= 41) // reserved
|| type == 42 // extended echo request
|| type == 43 // extended echo request
|| (type >= 44 && type <= 255) // reserved
) {
return true;
}
return false;
}
static __always_inline bool is_filtered_icmp6_packet(struct icmp6hdr *icmp6_header) {
int type = icmp6_header->icmp6_type;
if (
type == 0 // echo reply
|| type == 1 // reserved
|| type == 2 // reserved
|| type == 4 // source quench
|| type == 6 // deprecated (alternate host address)
|| type == 7 // reserved
|| type == 8 // echo request
|| type == 9 // router advertisement
|| type == 10 // router solicitation
|| type == 13 // timestamp request
|| type == 14 // timestamp reply
|| type == 15 // deprecated (information request)
|| type == 16 // deprecated (information reply)
|| type == 17 // deprecated (address mask request)
|| type == 18 // deprecated (address mask reply)
|| type == 19 // reserved
|| (type >= 20 && type <= 41) // reserved
|| type == 42 // extended echo request
|| type == 43 // extended echo request
|| (type >= 44 && type <= 127) // reserved
|| type == 128 // echo request
|| type == 129 // echo reply
|| type == 133 // router solicitation
|| type == 134 // router advertisement
|| type == 135 // neighbor solicitation
|| type == 136 // neighbor advertisement
|| type == 150 // reserved
|| type == 151 // multicast router advertisement
|| type == 152 // multicast router solicitation
|| type == 153 // multicast router termination
|| type == 154 // reserved
|| (type >= 156 && type <= 255) // reserved
) {
return true;
}
return false;
}
static __always_inline bool is_filtered_nmap_tcp_scan(struct tcphdr *tcp_header) {
if (
tcp_header->fin == 1
&& tcp_header->syn == 0
&& tcp_header->rst == 0
&& tcp_header->psh == 0
&& tcp_header->ack == 0
&& tcp_header->urg == 0
&& tcp_header->ece == 0
&& tcp_header->cwr == 0
) {
return true;
}
// nmap -sT
if (
tcp_header->fin == 0
&& tcp_header->syn == 0
&& tcp_header->rst == 1
&& tcp_header->psh == 0
&& tcp_header->ack == 1
&& tcp_header->urg == 0
&& tcp_header->ece == 0
&& tcp_header->cwr == 0
&& tcp_header->window == 0
) {
return true;
}
return false;
}
static __always_inline bool is_likely_nmap_tcp_scan(struct tcphdr *tcp_header) {
// nmap -sP
// nmap -P0
if (
tcp_header->fin == 0
&& tcp_header->cwr == 0
&& tcp_header->ece == 0
&& tcp_header->ack == 0
&& tcp_header->psh == 0
&& tcp_header->rst == 0
&& tcp_header->syn == 1
&& tcp_header->window == 61690
) {
return true;
}
return false;
}
SEC("xdp")
int xdp_prog_main(struct xdp_md *ctx) {
// Initialize data.
void *data_end = (void *)(long)ctx->data_end;
void *data = (void *)(long)ctx->data;
if (data + sizeof(struct ethhdr) > data_end) {
return XDP_DROP;
}
int ethernet_protocol;
struct hdr_cursor cursor;
struct ethhdr *ethernet_header = NULL;
struct iphdr *ipv4_header = NULL;
struct ipv6hdr *ipv6_header = NULL;
cursor.pos = data;
ethernet_protocol = parse_ethhdr(&cursor, data_end, ðernet_header);
// struct udphdr *udp_header = NULL;
// __u8 *blocked_ip = NULL;
// __u32 ipv4_address = 0;
// __u128 ipv6_address = 0;
// __u16 ipv4_port = 0;
// __u16 ipv6_port = 0;
if (ethernet_protocol == bpf_htons(ETH_P_IP)) {
int protocol = parse_iphdr(&cursor, data_end, &ipv4_header);
__u32 ipv4_source = 0;
__u32 ipv4_dest = 0;
if (ipv4_header) {
memcpy(&ipv4_source, &ipv4_header->saddr, sizeof(ipv4_source));
memcpy(&ipv4_dest, &ipv4_header->daddr, sizeof(ipv4_dest));
}
if (ipv4_source != 0) {
if (is_filtered_ipv4(&ipv4_source) == true) {
#ifdef ENABLE_DEBUG
// bpf_printk("Banned %pI4", &ipv4_source);
#endif
return XDP_DROP;
}
}
if (protocol == IPPROTO_UDP) {
struct udphdr *udp_header = NULL;
int length = parse_udphdr(&cursor, data_end, &udp_header);
if (length < 0) {
return XDP_DROP;
}
__u16 port_source = udp_header->source;
// remote port is filtered
if (is_filtered_port(port_source) == true) {
return XDP_DROP;
}
__u16 port_dest = udp_header->dest;
// local port is filtered
if (is_filtered_port(port_dest) == true) {
return XDP_DROP;
}
#ifdef ENABLE_DNSFILTER
if (
port_source == bpf_htons(53)
|| port_source == bpf_htons(853)
|| port_source == bpf_htons(5353)
|| port_dest == bpf_htons(53)
|| port_dest == bpf_htons(853)
|| port_dest == bpf_htons(5353)
) {
struct dnshdr *dns_header = NULL;
int length = parse_dnshdr(&cursor, data_end, &dns_header);
if (length < 0) {
return XDP_DROP;
}
bpf_printk("ETH_P_IP/UDP port %d -> %d", bpf_ntohs(port_source), bpf_ntohs(port_dest));
bpf_printk("%pI4", &ipv4_source);
bpf_printk("%pI4", &ipv4_dest);
}
#endif
} else if (protocol == IPPROTO_TCP) {
struct tcphdr *tcp_header = NULL;
int length = parse_tcphdr(&cursor, data_end, &tcp_header);
if (length < 0) {
return XDP_DROP;
}
if (is_filtered_nmap_tcp_scan(tcp_header) == true) {
return XDP_DROP;
} else if (is_likely_nmap_tcp_scan(tcp_header) == true) {
warn_ipv4(&ipv4_source);
}
__u16 port_source = tcp_header->source;
// remote port is filtered
if (is_filtered_port(port_source) == true) {
return XDP_DROP;
}
__u16 port_dest = tcp_header->dest;
// local port is filtered
if (is_filtered_port(port_dest) == true) {
return XDP_DROP;
}
#ifdef ENABLE_DNSFILTER
if (
port_source == bpf_htons(53)
|| port_source == bpf_htons(853)
|| port_source == bpf_htons(5353)
|| port_dest == bpf_htons(53)
|| port_dest == bpf_htons(853)
|| port_dest == bpf_htons(5353)
) {
// TODO: struct dnshdr *dns_header = NULL;
// TODO: dnshdr is offsetted by two bytes which represent the length
}
#endif
} else if (protocol == IPPROTO_ICMP) {
struct icmphdr *icmp_header = NULL;
parse_icmphdr(&cursor, data_end, &icmp_header);
if (icmp_header && is_filtered_icmp_packet(icmp_header) == true) {
return XDP_DROP;
}
#ifdef ENABLE_DEBUG
// bpf_printk("ETH_P_IP/ICMP");
// bpf_printk("%pI4", &ipv4_source);
// bpf_printk("%pI4", &ipv4_dest);
#endif
}
return XDP_PASS;
} else if (ethernet_protocol == bpf_htons(ETH_P_IPV6)) {
int protocol = parse_ipv6hdr(&cursor, data_end, &ipv6_header);
__u128 ipv6_source = 0;
__u128 ipv6_dest = 0;
if (ipv6_header) {
memcpy(&ipv6_source, &ipv6_header->saddr.in6_u.u6_addr32, sizeof(ipv6_source));
memcpy(&ipv6_dest, &ipv6_header->daddr.in6_u.u6_addr32, sizeof(ipv6_dest));
}
if (ipv6_source != 0) {
if (is_filtered_ipv6(&ipv6_source) == true) {
#ifdef ENABLE_DEBUG
// bpf_printk("Banned %pI6", &ipv6_source);
#endif
return XDP_DROP;
}
}
if (protocol == IPPROTO_UDP) {
struct udphdr *udp_header = NULL;
int length = parse_udphdr(&cursor, data_end, &udp_header);
if (length < 0) {
return XDP_DROP;
}
__u16 port_source = udp_header->source;
// remote port is filtered
if (is_filtered_port(port_source) == true) {
return XDP_DROP;
}
__u16 port_dest = udp_header->dest;
// local port is filtered
if (is_filtered_port(port_dest) == true) {
return XDP_DROP;
}
#ifdef ENABLE_DNSFILTER
if (
port_source == bpf_htons(53)
|| port_source == bpf_htons(853)
|| port_source == bpf_htons(5353)
|| port_dest == bpf_htons(53)
|| port_dest == bpf_htons(853)
|| port_dest == bpf_htons(5353)
) {
// TODO: struct dnshdr *dns_header = NULL;
}
#endif
} else if (protocol == IPPROTO_TCP) {
struct tcphdr *tcp_header = NULL;
int length = parse_tcphdr(&cursor, data_end, &tcp_header);
if (length < 0) {
return XDP_DROP;
}
if (is_filtered_nmap_tcp_scan(tcp_header) == true) {
return XDP_DROP;
} else if (is_likely_nmap_tcp_scan(tcp_header) == true) {
warn_ipv6(&ipv6_source);
}
__u16 port_source = tcp_header->source;
// remote port is filtered
if (is_filtered_port(port_source) == true) {
return XDP_DROP;
}
__u16 port_dest = tcp_header->dest;
// local port is filtered
if (is_filtered_port(port_dest) == true) {
return XDP_DROP;
}
#ifdef ENABLE_DNSFILTER
if (
port_source == bpf_htons(53)
|| port_source == bpf_htons(853)
|| port_source == bpf_htons(5353)
|| port_dest == bpf_htons(53)
|| port_dest == bpf_htons(853)
|| port_dest == bpf_htons(5353)
) {
// TODO: struct dnshdr *dns_header = NULL;
}
#endif
} else if (protocol == IPPROTO_ICMP) {
struct icmphdr *icmp_header = NULL;
parse_icmphdr(&cursor, data_end, &icmp_header);
if (icmp_header && is_filtered_icmp_packet(icmp_header) == true) {
return XDP_DROP;
}
#ifdef ENABLE_DEBUG
// bpf_printk("ETH_P_IP/ICMP");
// bpf_printk("%pI6", &ipv6_source);
// bpf_printk("%pI6", &ipv6_dest);
#endif
} else if (protocol == IPPROTO_ICMPV6) {
struct icmp6hdr *icmp6_header = NULL;
parse_icmp6hdr(&cursor, data_end, &icmp6_header);
if (icmp6_header && is_filtered_icmp6_packet(icmp6_header) == true) {
return XDP_DROP;
}
#ifdef ENABLE_DEBUG
// bpf_printk("ETH_P_IP/ICMP6");
// bpf_printk("%pI6", &ipv6_source);
// bpf_printk("%pI6", &ipv6_dest);
#endif
}
}
return XDP_PASS;
}
char _license[] SEC("license") = "GPL";