-
Notifications
You must be signed in to change notification settings - Fork 22
/
ingress_node_firewall_kernel.c
556 lines (512 loc) · 18.6 KB
/
ingress_node_firewall_kernel.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
// +build ignore
// clang-format off
#include <vmlinux.h>
// clang-format on
#include "bpf_tracing.h"
#include "ingress_node_firewall.h"
#define MAX_CPUS 256
#if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && \
__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
#define bpf_ntohs(x) __builtin_bswap16(x)
#define bpf_htons(x) __builtin_bswap16(x)
#elif defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
#define bpf_ntohs(x) (x)
#define bpf_htons(x) (x)
#else
#error "Endianness detection needs to be set up for your compiler?!"
#endif
/*
* ingress_node_firewall_events_map: is perf event array map type
* key is the rule id, packet header is captured and used to generate events.
*/
struct {
__uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
__type(key, __u32);
__type(value, __u32);
__uint(max_entries, MAX_CPUS);
} ingress_node_firewall_events_map SEC(".maps");
/*
* ingress_node_firewall_statistics_map: is per cpu array map type
* key is the rule id
* user space collects statistics per CPU and aggregate them.
*/
struct {
__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
__type(key, __u32); // ruleId
__type(value, struct ruleStatistics_st);
__uint(max_entries, MAX_TARGETS);
} ingress_node_firewall_statistics_map SEC(".maps");
/*
* ingress_node_firewall_table_map: is LPM trie map type
* key is the ingress interface index and the sourceCIDR.
* lookup returns an array of rules with actions for the XDP program
* to process.
* Note: this map is pinned to specific path in bpffs.
*/
struct {
__uint(type, BPF_MAP_TYPE_LPM_TRIE);
__type(key, struct lpm_ip_key_st);
__type(value, struct rulesVal_st);
__uint(max_entries, MAX_TARGETS);
__uint(map_flags, BPF_F_NO_PREALLOC);
__uint(pinning, LIBBPF_PIN_BY_NAME);
} ingress_node_firewall_table_map SEC(".maps");
struct {
__uint(type, BPF_MAP_TYPE_HASH);
__type(key, struct lpm_ip_key_st);
__type(value, struct lpm_ip_key_st);
__uint(max_entries, 16384);
} ingress_node_firewall_dbg_map SEC(".maps");
/*
* ingress_node_firewall_printk: macro used to generate prog traces for
* debugging only to enable uncomment the following line
*/
// #define ENABLE_BPF_PRINTK
#ifdef ENABLE_BPF_PRINTK
#define ingress_node_firewall_printk(fmt, args...) bpf_printk(fmt, ##args)
#else
#define ingress_node_firewall_printk(fmt, args...)
#endif
// Global used to enable lookup debug hashmap
static volatile const __u32 debug_lookup = 0;
/*
* ip_extract_l4info(): extracts L4 info for the supported protocols from
* the incoming packet's headers.
* Input:
* void *data: pointer to the packet data.
* void *data_end: pointer to the end of the packet data.
* bool is_v4: true for ipv4 and false for ipv6.
* Output:
* __u16 *dstPort: pointer to L4 destination port for TCP/UDP/SCTP protocols.
* __u8 *icmpType: pointer to ICMP or ICMPv6's type value.
* __u8 *icmpCode: pointer to ICMP or ICMPv6's code value.
* Return:
* 0 for Success.
* -1 for Failure.
*/
__attribute__((__always_inline__)) static inline int
ip_extract_l4info(void *data, void *dataEnd, __u8 *proto, __u16 *dstPort,
__u8 *icmpType, __u8 *icmpCode, __u8 is_v4) {
void *dataStart = data + sizeof(struct ethhdr);
if (likely(is_v4)) {
struct iphdr *iph = dataStart;
dataStart += sizeof(struct iphdr);
if (unlikely(dataStart > dataEnd)) {
return -1;
}
*proto = iph->protocol;
} else {
struct ipv6hdr *iph = dataStart;
dataStart += sizeof(struct ipv6hdr);
if (unlikely(dataStart > dataEnd)) {
return -1;
}
*proto = iph->nexthdr;
}
switch (*proto) {
case IPPROTO_TCP: {
struct tcphdr *tcph = (struct tcphdr *)dataStart;
dataStart += sizeof(struct tcphdr);
if (unlikely(dataStart > dataEnd)) {
return -1;
}
*dstPort = tcph->dest;
break;
}
case IPPROTO_UDP: {
struct udphdr *udph = (struct udphdr *)dataStart;
dataStart += sizeof(struct udphdr);
if (unlikely(dataStart > dataEnd)) {
return -1;
}
*dstPort = udph->dest;
break;
}
case IPPROTO_SCTP: {
struct sctphdr *sctph = (struct sctphdr *)dataStart;
dataStart += sizeof(struct sctphdr);
if (unlikely(dataStart > dataEnd)) {
return -1;
}
*dstPort = sctph->dest;
break;
}
case IPPROTO_ICMP: {
struct icmphdr *icmph = (struct icmphdr *)dataStart;
dataStart += sizeof(struct icmphdr);
if (unlikely(dataStart > dataEnd)) {
return -1;
}
*icmpType = icmph->type;
*icmpCode = icmph->code;
break;
}
case IPPROTO_ICMPV6: {
struct icmp6hdr *icmp6h = (struct icmp6hdr *)dataStart;
dataStart += sizeof(struct icmp6hdr);
if (unlikely(dataStart > dataEnd)) {
return -1;
}
*icmpType = icmp6h->icmp6_type;
*icmpCode = icmp6h->icmp6_code;
break;
}
default:
return -1;
}
return 0;
}
/*
* ipv4_firewall_lookup(): matches ipv4 packet with LPM map's key,
* match L4 headers with the result rules in order and return the action.
* if there is no match it will return UNDEF action.
* Input:
* void *data: pointer to the packet data.
* void *data_end: pointer to the end of the packet data.
* __u32 ifID: ingress interface index where the packet is received from.
* Output:
* none.
* Return:
* __u32 action: returned action is the logical or of the rule id and action
* field from the matching rule, in case of no match it returns UNDEF.
*/
__attribute__((__always_inline__)) static inline __u32
ipv4_firewall_lookup(void *data, void *data_end, __u32 ifId) {
struct iphdr *iph = data + sizeof(struct ethhdr);
struct lpm_ip_key_st key;
__u32 srcAddr = 0;
__u16 dstPort = 0;
__u8 icmpCode = 0, icmpType = 0, proto = 0;
int i;
if (unlikely(ip_extract_l4info(data, data_end, &proto, &dstPort, &icmpType,
&icmpCode, 1) < 0)) {
ingress_node_firewall_printk("failed to extract l4 info");
return SET_ACTION(UNDEF);
}
srcAddr = iph->saddr;
memset(&key, 0, sizeof(key));
key.prefixLen = 64; // ipv4 address + ifId
memcpy(key.ip_data, &srcAddr, sizeof(srcAddr));
key.ingress_ifindex = ifId;
if (unlikely(debug_lookup != 0)) {
(void)bpf_map_update_elem(&ingress_node_firewall_dbg_map, &key, &key,
BPF_NOEXIST);
}
struct rulesVal_st *rulesVal = (struct rulesVal_st *)bpf_map_lookup_elem(
&ingress_node_firewall_table_map, &key);
if (likely(NULL != rulesVal)) {
#pragma clang loop unroll(full)
for (i = 0; i < MAX_RULES_PER_TARGET; ++i) {
struct ruleType_st *rule = &rulesVal->rules[i];
if (rule->ruleId == INVALID_RULE_ID) {
continue;
}
if (likely((rule->protocol != 0) && (rule->protocol == proto))) {
ingress_node_firewall_printk("ruleInfo (protocol %d, Id %d, action %d)",
rule->protocol, rule->ruleId,
rule->action);
if ((rule->protocol == IPPROTO_TCP) ||
(rule->protocol == IPPROTO_UDP) ||
(rule->protocol == IPPROTO_SCTP)) {
ingress_node_firewall_printk("TCP/UDP/SCTP packet rule_dstPortStart "
"%d rule_dstPortEnd %d pkt_dstPort %d",
rule->dstPortStart, rule->dstPortEnd,
bpf_ntohs(dstPort));
if (rule->dstPortEnd == 0) {
if (rule->dstPortStart == bpf_ntohs(dstPort)) {
return SET_ACTIONRULE_RESPONSE(rule->action, rule->ruleId);
}
} else {
if ((bpf_ntohs(dstPort) >= rule->dstPortStart) &&
(bpf_ntohs(dstPort) < rule->dstPortEnd)) {
return SET_ACTIONRULE_RESPONSE(rule->action, rule->ruleId);
}
}
}
if (rule->protocol == IPPROTO_ICMP) {
ingress_node_firewall_printk(
"ICMP packet rule(type:%d, code:%d) pkt(type:%d, code %d)",
rule->icmpType, rule->icmpCode, icmpType, icmpCode);
if ((rule->icmpType == icmpType) && (rule->icmpCode == icmpCode)) {
return SET_ACTIONRULE_RESPONSE(rule->action, rule->ruleId);
}
}
}
// Protocol is not set so just apply the action
if (rule->protocol == 0) {
return SET_ACTIONRULE_RESPONSE(rule->action, rule->ruleId);
}
}
ingress_node_firewall_printk(
"Packet didn't match any rule proto %d port %d", proto,
bpf_ntohs(dstPort));
}
return SET_ACTION(UNDEF);
}
/*
* ipv6_firewall_lookup(): matches ipv6 packet with LPM map's key,
* match L4 headers with the result rules in order and return the action.
* if there is no rule match it will return UNDEF action.
* Input:
* void *data: pointer to the packet data.
* void *data_end: pointer to the end of the packet data.
* __u32 ifID: ingress interface index where the packet is received from.
* Output:
* none.
* Return:
__u32 action: returned action is the logical or of the rule id and action field
* from the matching rule, in case of no match it returns UNDEF.
*/
__attribute__((__always_inline__)) static inline __u32
ipv6_firewall_lookup(void *data, void *data_end, __u32 ifId) {
struct ipv6hdr *iph = data + sizeof(struct ethhdr);
struct lpm_ip_key_st key;
__u8 *srcAddr = NULL;
__u16 dstPort = 0;
__u8 icmpCode = 0, icmpType = 0, proto = 0;
int i;
if (unlikely(ip_extract_l4info(data, data_end, &proto, &dstPort, &icmpType,
&icmpCode, 0) < 0)) {
ingress_node_firewall_printk("failed to extract l4 info");
return SET_ACTION(UNDEF);
}
srcAddr = iph->saddr.in6_u.u6_addr8;
memset(&key, 0, sizeof(key));
key.prefixLen = 160; // ipv6 address + ifId
memcpy(key.ip_data, srcAddr, 16);
key.ingress_ifindex = ifId;
if (unlikely(debug_lookup != 0)) {
(void)bpf_map_update_elem(&ingress_node_firewall_dbg_map, &key, &key,
BPF_NOEXIST);
}
struct rulesVal_st *rulesVal = (struct rulesVal_st *)bpf_map_lookup_elem(
&ingress_node_firewall_table_map, &key);
if (NULL != rulesVal) {
#pragma clang loop unroll(full)
for (i = 0; i < MAX_RULES_PER_TARGET; ++i) {
struct ruleType_st *rule = &rulesVal->rules[i];
if (rule->ruleId == INVALID_RULE_ID) {
continue;
}
if (likely((rule->protocol != 0) && (rule->protocol == proto))) {
ingress_node_firewall_printk("ruleInfo (protocol %d, Id %d, action %d)",
rule->protocol, rule->ruleId,
rule->action);
if ((rule->protocol == IPPROTO_TCP) ||
(rule->protocol == IPPROTO_UDP) ||
(rule->protocol == IPPROTO_SCTP)) {
ingress_node_firewall_printk("TCP/UDP/SCTP packet rule_dstPortStart "
"%d rule_dstPortEnd %d pkt_dstPort %d",
rule->dstPortStart, rule->dstPortEnd,
bpf_ntohs(dstPort));
if (rule->dstPortEnd == 0) {
if (rule->dstPortStart == bpf_ntohs(dstPort)) {
return SET_ACTIONRULE_RESPONSE(rule->action, rule->ruleId);
}
} else {
if ((bpf_ntohs(dstPort) >= rule->dstPortStart) &&
(bpf_ntohs(dstPort) < rule->dstPortEnd)) {
return SET_ACTIONRULE_RESPONSE(rule->action, rule->ruleId);
}
}
}
if (rule->protocol == IPPROTO_ICMPV6) {
ingress_node_firewall_printk(
"ICMPV6 packet rule(type:%d, code:%d) pkt(type:%d, code %d)",
rule->icmpType, rule->icmpCode, icmpType, icmpCode);
if ((rule->icmpType == icmpType) && (rule->icmpCode == icmpCode)) {
return SET_ACTIONRULE_RESPONSE(rule->action, rule->ruleId);
}
}
}
// Protocol is not set so just apply the action
if (rule->protocol == 0) {
return SET_ACTIONRULE_RESPONSE(rule->action, rule->ruleId);
}
}
ingress_node_firewall_printk(
"Packet didn't match any rule proto %d port %d", proto,
bpf_ntohs(dstPort));
}
return SET_ACTION(UNDEF);
}
/*
* generate_event_and_update_statistics() : it will generate eBPF event
* including the packet header and update statistics for the specificed rule id.
* Input:
* struct xdp_md *ctx: pointer to XDP context including input interface and
* packet pointer.
* __u64 packet_len: packet length in bytes including layer2 header.
* __u8 action: valid actions ALLOW/DENY/UNDEF.
* __u16 ruleId: ruled id where the packet matches against (in case of match of
* course).
* __u8 generateEvent: need to generate event for this packet or not.
* __u32 ifID: input interface index where the packet is arrived from.
* Output:
* none.
* Return:
* none.
*/
__attribute__((__always_inline__)) static inline void
generate_event_and_update_statistics(void *ctx, __u64 packet_len, __u8 action,
__u16 ruleId, __u8 generateEvent,
__u32 ifId) {
struct ruleStatistics_st *statistics, initialStats;
struct event_hdr_st hdr;
__u64 flags = BPF_F_CURRENT_CPU;
__u16 headerSize;
__u32 key = ruleId;
memset(&hdr, 0, sizeof(hdr));
hdr.ruleId = ruleId;
hdr.action = action;
hdr.pktLength = (__u16)packet_len;
hdr.ifId = (__u16)ifId;
memset(&initialStats, 0, sizeof(initialStats));
statistics = bpf_map_lookup_elem(&ingress_node_firewall_statistics_map, &key);
if (likely(statistics)) {
switch (action) {
case ALLOW:
__sync_fetch_and_add(&statistics->allow_stats.packets, 1);
__sync_fetch_and_add(&statistics->allow_stats.bytes, packet_len);
break;
case DENY:
__sync_fetch_and_add(&statistics->deny_stats.packets, 1);
__sync_fetch_and_add(&statistics->deny_stats.bytes, packet_len);
break;
}
} else {
bpf_map_update_elem(&ingress_node_firewall_statistics_map, &key,
&initialStats, BPF_ANY);
}
if (generateEvent) {
headerSize = packet_len < MAX_EVENT_DATA ? packet_len : MAX_EVENT_DATA;
// enable the following flag to dump packet header
flags |= (__u64)headerSize << 32;
(void)bpf_perf_event_output(ctx, &ingress_node_firewall_events_map, flags,
&hdr, sizeof(hdr));
}
}
/*
* xdp_ingress_node_firewall_main(): is the entry point for the XDP program to
* do ingress node firewall. Input: struct xdp_md *ctx: pointer to XDP context
* which contains packet pointer and input interface index. Output: none.
* Return:
* int XDP action: valid values XDP_DROP and XDP_PASS.
*/
__attribute__((__always_inline__)) static inline int
xdp_ingress_node_firewall_main(struct xdp_md *ctx) {
void *data = (void *)(long)ctx->data;
void *dataEnd = (void *)(long)ctx->data_end;
struct ethhdr *eth = data;
void *dataStart = data + sizeof(struct ethhdr);
__u32 result = UNDEF;
__u32 ifId = ctx->ingress_ifindex;
ingress_node_firewall_printk(
"XDP: Ingress node firewall start processing a packet on %d", ifId);
if (unlikely(dataStart > dataEnd)) {
ingress_node_firewall_printk(
"XDP: Ingress node firewall bad packet XDP_DROP");
return XDP_DROP;
}
switch (eth->h_proto) {
case bpf_htons(ETH_P_IP):
ingress_node_firewall_printk(
"XDP: Ingress node firewall process IPv4 packet");
result = ipv4_firewall_lookup(data, dataEnd, ifId);
break;
case bpf_htons(ETH_P_IPV6):
ingress_node_firewall_printk(
"XDP: Ingress node firewall process IPv6 packet");
result = ipv6_firewall_lookup(data, dataEnd, ifId);
break;
default:
ingress_node_firewall_printk(
"XDP: Ingress node firewall unknown L3 protocol XDP_PASS");
return XDP_PASS;
}
__u16 ruleId = GET_RULE_ID(result);
__u8 action = GET_ACTION(result);
switch (action) {
case DENY:
generate_event_and_update_statistics(ctx, bpf_xdp_get_buff_len(ctx), DENY,
ruleId, 1, ifId);
ingress_node_firewall_printk(
"XDP: Ingress node firewall action DENY -> XDP_DROP");
return XDP_DROP;
case ALLOW:
generate_event_and_update_statistics(ctx, bpf_xdp_get_buff_len(ctx), ALLOW,
ruleId, 0, ifId);
ingress_node_firewall_printk(
"XDP: Ingress node firewall action ALLOW -> XDP_PASS");
return XDP_PASS;
default:
ingress_node_firewall_printk("XDP: Ingress node firewall action UNDEF");
return XDP_PASS;
}
}
/*
* tcx_ingress_node_firewall_main(): is the entry point for the TCX program to
* do ingress node firewall. Input: struct __sk_buff *ctx: pointer to sk_buff
* which contains packet pointer and input interface index. Output: none.
* Return:
* int TCX action: valid values TCX_DROP and TCX_NEXT.
*/
__attribute__((__always_inline__)) static inline int
tcx_ingress_node_firewall_main(struct __sk_buff *ctx) {
void *data = (void *)(long)ctx->data;
void *dataEnd = (void *)(long)ctx->data_end;
struct ethhdr *eth = data;
void *dataStart = data + sizeof(struct ethhdr);
__u32 result = UNDEF;
__u32 ifId = ctx->ifindex;
ingress_node_firewall_printk(
"TCX: Ingress node firewall start processing a packet on %d", ifId);
if (unlikely(dataStart > dataEnd)) {
ingress_node_firewall_printk(
"TCX: Ingress node firewall bad packet TCX_DROP");
return TCX_DROP;
}
switch (eth->h_proto) {
case bpf_htons(ETH_P_IP):
ingress_node_firewall_printk(
"TCX: Ingress node firewall process IPv4 packet");
result = ipv4_firewall_lookup(data, dataEnd, ifId);
break;
case bpf_htons(ETH_P_IPV6):
ingress_node_firewall_printk(
"TCX: Ingress node firewall process IPv6 packet");
result = ipv6_firewall_lookup(data, dataEnd, ifId);
break;
default:
ingress_node_firewall_printk(
"TCX: Ingress node firewall unknown L3 protocol TCX_NEXT");
return TCX_NEXT;
}
__u16 ruleId = GET_RULE_ID(result);
__u8 action = GET_ACTION(result);
switch (action) {
case DENY:
generate_event_and_update_statistics(ctx, ctx->len, DENY, ruleId, 1, ifId);
ingress_node_firewall_printk(
"TCX: Ingress node firewall action DENY -> TCX_DROP");
return TCX_DROP;
case ALLOW:
generate_event_and_update_statistics(ctx, ctx->len, ALLOW, ruleId, 0, ifId);
ingress_node_firewall_printk(
"TCX: Ingress node firewall action ALLOW -> TCX_NEXT");
return TCX_NEXT;
default:
ingress_node_firewall_printk("TCX: Ingress node firewall action UNDEF");
return TCX_NEXT;
}
}
SEC("xdp.frags")
int xdp_ingress_node_firewall_process(struct xdp_md *ctx) {
return xdp_ingress_node_firewall_main(ctx);
}
SEC("tcx_ingress_node_fw")
int tcx_ingress_node_firewall_process(struct __sk_buff *skb) {
return tcx_ingress_node_firewall_main(skb);
}
char __license[] SEC("license") = "Dual BSD/GPL";