-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathxdp_flowtable.bpf.c
621 lines (525 loc) · 15.4 KB
/
xdp_flowtable.bpf.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
// SPDX-License-Identifier: GPL-2.0
/* Original xdp_fwd sample Copyright (c) 2017-18 David Ahern <[email protected]>
*/
#include <bpf/vmlinux.h>
#include <linux/bpf.h>
#include <linux/netfilter.h>
#include <bpf/bpf_core_read.h>
#define AF_INET 2
#define AF_INET6 10
#define IPV6_FLOWINFO_MASK bpf_htons(0x0FFFFFFF)
#define IP_MF 0x2000 /* "More Fragments" */
#define IP_OFFSET 0x1fff /* "Fragment Offset" */
#define CSUM_MANGLED_0 ((__sum16)0xffff)
#define BIT(x) (1 << (x))
struct {
__uint(type, BPF_MAP_TYPE_DEVMAP);
__uint(key_size, sizeof(int));
__uint(value_size, sizeof(int));
__uint(max_entries, 64);
} xdp_tx_ports SEC(".maps");
struct bpf_flowtable_opts {
__s32 error;
};
struct flow_offload_tuple_rhash *
bpf_xdp_flow_lookup(struct xdp_md *, struct bpf_fib_lookup *,
struct bpf_flowtable_opts *, __u32) __ksym;
/* from include/net/ip.h */
static __always_inline int ip_decrease_ttl(struct iphdr *iph)
{
__u32 check = (__u32)iph->check;
check += (__u32)bpf_htons(0x0100);
iph->check = (__sum16)(check + (check >= 0xFFFF));
return --iph->ttl;
}
static __always_inline __u32 csum_add(__u32 csum, __u32 addend)
{
__u32 res = csum + addend;
return res + (res < addend);
}
static __always_inline __u16 csum_fold(__u32 csum)
{
csum = (csum & 0xffff) + (csum >> 16);
csum = (csum & 0xffff) + (csum >> 16);
return ~csum;
}
static __always_inline __u16 csum_replace4(__u32 csum, __u32 from, __u32 to)
{
__u32 tmp = csum_add(~csum, ~from);
return csum_fold(csum_add(tmp, to));
}
static __always_inline __u16 csum_replace16(__u32 csum, __u32 *from, __u32 *to)
{
__u32 diff[] = {
~from[0], ~from[1], ~from[2], ~from[3],
to[0], to[1], to[2], to[3],
};
csum = bpf_csum_diff(0, 0, diff, sizeof(diff), ~csum);
return csum_fold(csum);
}
static __always_inline int
xdp_flowtable_check_tcp_state(void *ports, void *data_end, __u8 proto)
{
if (proto == IPPROTO_TCP) {
struct tcphdr *tcph = ports;
if (tcph + 1 > data_end)
return -1;
if (tcph->fin || tcph->rst)
return -1;
}
return 0;
}
static __always_inline void
xdp_flowtable_update_port_csum(struct flow_ports *ports, void *data_end,
__u8 proto, __be16 port, __be16 nat_port)
{
switch (proto) {
case IPPROTO_TCP: {
struct tcphdr *tcph = (struct tcphdr *)ports;
if (tcph + 1 > data_end)
break;
tcph->check = csum_replace4((__u32)tcph->check, (__u32)port,
(__u32)nat_port);
break;
}
case IPPROTO_UDP: {
struct udphdr *udph = (struct udphdr *)ports;
if (udph + 1 > data_end)
break;
if (!udph->check)
break;
udph->check = csum_replace4((__u32)udph->check, (__u32)port,
(__u32)nat_port);
if (!udph->check)
udph->check = CSUM_MANGLED_0;
break;
}
default:
break;
}
}
static __always_inline void
xdp_flowtable_snat_port(const struct flow_offload *flow,
struct flow_ports *ports, void *data_end,
__u8 proto, enum flow_offload_tuple_dir dir)
{
__be16 port, nat_port;
if (ports + 1 > data_end)
return;
switch (dir) {
case FLOW_OFFLOAD_DIR_ORIGINAL:
port = ports->source;
/* For original direction (FLOW_OFFLOAD_DIR_ORIGINAL):
* - tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.dst_port contains
* the source port used for the traffic transmitted by the
* host.
* - tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.src_port contains
* the destination port used for the traffic transmitted by
* the host.
*/
bpf_core_read(&nat_port, bpf_core_type_size(nat_port),
&flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.dst_port);
ports->source = nat_port;
break;
case FLOW_OFFLOAD_DIR_REPLY:
/* For reply direction (FLOW_OFFLOAD_DIR_REPLY):
* - tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.dst_port
* contains source port used for the traffic received by the
* host.
* - tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.src_port
* contains the destination port used for the traffic
* received by the host.
*/
port = ports->dest;
bpf_core_read(&nat_port, bpf_core_type_size(nat_port),
&flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.src_port);
ports->dest = nat_port;
break;
default:
return;
}
xdp_flowtable_update_port_csum(ports, data_end, proto, port, nat_port);
}
static __always_inline void
xdp_flowtable_dnat_port(const struct flow_offload *flow,
struct flow_ports *ports, void *data_end, __u8 proto,
enum flow_offload_tuple_dir dir)
{
__be16 port, nat_port;
if (ports + 1 > data_end)
return;
switch (dir) {
case FLOW_OFFLOAD_DIR_ORIGINAL:
/* For original direction (FLOW_OFFLOAD_DIR_ORIGINAL):
* - tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.dst_port contains
* the source port used for the traffic transmitted by the
* host.
* - tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.src_port contains
* the destination port used for the traffic transmitted by
* the host.
*/
port = ports->dest;
bpf_core_read(&nat_port, bpf_core_type_size(nat_port),
&flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.src_port);
ports->dest = nat_port;
break;
case FLOW_OFFLOAD_DIR_REPLY:
/* For reply direction (FLOW_OFFLOAD_DIR_REPLY):
* - tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.dst_port
* contains the source port used for the traffic received by
* the host.
* - tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.src_port
* contains destination port used for the traffic received by
* the host.
*/
port = ports->source;
bpf_core_read(&nat_port, bpf_core_type_size(nat_port),
&flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.dst_port);
ports->source = nat_port;
break;
default:
return;
}
xdp_flowtable_update_port_csum(ports, data_end, proto, port, nat_port);
}
static __always_inline void
xdp_flowtable_update_ipv4_csum(struct iphdr *iph, void *data_end,
__be32 addr, __be32 nat_addr)
{
switch (iph->protocol) {
case IPPROTO_TCP: {
struct tcphdr *tcph = (struct tcphdr *)(iph + 1);
if (tcph + 1 > data_end)
break;
tcph->check = csum_replace4((__u32)tcph->check, addr,
nat_addr);
break;
}
case IPPROTO_UDP: {
struct udphdr *udph = (struct udphdr *)(iph + 1);
if (udph + 1 > data_end)
break;
if (!udph->check)
break;
udph->check = csum_replace4((__u32)udph->check, addr,
nat_addr);
if (!udph->check)
udph->check = CSUM_MANGLED_0;
break;
}
default:
break;
}
}
static __always_inline void
xdp_flowtable_snat_ip(const struct flow_offload *flow, struct iphdr *iph,
void *data_end, enum flow_offload_tuple_dir dir)
{
__be32 addr, nat_addr;
switch (dir) {
case FLOW_OFFLOAD_DIR_ORIGINAL:
addr = iph->saddr;
bpf_core_read(&nat_addr, bpf_core_type_size(nat_addr),
&flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.dst_v4.s_addr);
iph->saddr = nat_addr;
break;
case FLOW_OFFLOAD_DIR_REPLY:
addr = iph->daddr;
bpf_core_read(&nat_addr, bpf_core_type_size(nat_addr),
&flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.src_v4.s_addr);
iph->daddr = nat_addr;
break;
default:
return;
}
iph->check = csum_replace4((__u32)iph->check, addr, nat_addr);
xdp_flowtable_update_ipv4_csum(iph, data_end, addr, nat_addr);
}
static __always_inline void
xdp_flowtable_get_dnat_ip(__be32 *addr, const struct flow_offload *flow,
enum flow_offload_tuple_dir dir)
{
switch (dir) {
case FLOW_OFFLOAD_DIR_ORIGINAL:
bpf_core_read(addr, sizeof(*addr),
&flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.src_v4.s_addr);
break;
case FLOW_OFFLOAD_DIR_REPLY:
bpf_core_read(addr, sizeof(*addr),
&flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.dst_v4.s_addr);
break;
default:
break;
}
}
static __always_inline void
xdp_flowtable_dnat_ip(const struct flow_offload *flow, struct iphdr *iph,
void *data_end, enum flow_offload_tuple_dir dir)
{
__be32 addr, nat_addr;
xdp_flowtable_get_dnat_ip(&nat_addr, flow, dir);
switch (dir) {
case FLOW_OFFLOAD_DIR_ORIGINAL:
addr = iph->daddr;
iph->daddr = nat_addr;
break;
case FLOW_OFFLOAD_DIR_REPLY:
addr = iph->saddr;
iph->saddr = nat_addr;
break;
default:
return;
}
iph->check = csum_replace4((__u32)iph->check, addr, nat_addr);
xdp_flowtable_update_ipv4_csum(iph, data_end, addr, nat_addr);
}
static __always_inline void
xdp_flowtable_update_ipv6_csum(struct ipv6hdr *ip6h, void *data_end,
struct in6_addr *addr,
struct in6_addr *nat_addr)
{
switch (ip6h->nexthdr) {
case IPPROTO_TCP: {
struct tcphdr *tcph = (struct tcphdr *)(ip6h + 1);
if (tcph + 1 > data_end)
break;
tcph->check = csum_replace16((__u32)tcph->check,
addr->in6_u.u6_addr32,
nat_addr->in6_u.u6_addr32);
break;
}
case IPPROTO_UDP: {
struct udphdr *udph = (struct udphdr *)(ip6h + 1);
if (udph + 1 > data_end)
break;
if (!udph->check)
break;
udph->check = csum_replace16((__u32)udph->check,
addr->in6_u.u6_addr32,
nat_addr->in6_u.u6_addr32);
if (!udph->check)
udph->check = CSUM_MANGLED_0;
break;
}
default:
break;
}
}
static __always_inline void
xdp_flowtable_snat_ipv6(const struct flow_offload *flow, struct ipv6hdr *ip6h,
void *data_end, enum flow_offload_tuple_dir dir)
{
struct in6_addr addr, nat_addr;
switch (dir) {
case FLOW_OFFLOAD_DIR_ORIGINAL:
addr = ip6h->saddr;
bpf_core_read(&nat_addr, bpf_core_type_size(nat_addr),
&flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.dst_v6);
ip6h->saddr = nat_addr;
break;
case FLOW_OFFLOAD_DIR_REPLY:
addr = ip6h->daddr;
bpf_core_read(&nat_addr, bpf_core_type_size(nat_addr),
&flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.src_v6);
ip6h->daddr = nat_addr;
break;
default:
return;
}
xdp_flowtable_update_ipv6_csum(ip6h, data_end, &addr, &nat_addr);
}
static __always_inline void
xdp_flowtable_get_dnat_ipv6(struct in6_addr *addr,
const struct flow_offload *flow,
enum flow_offload_tuple_dir dir)
{
switch (dir) {
case FLOW_OFFLOAD_DIR_ORIGINAL:
bpf_core_read(addr, sizeof(*addr),
&flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.src_v6);
break;
case FLOW_OFFLOAD_DIR_REPLY:
bpf_core_read(addr, sizeof(*addr),
&flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.dst_v6);
break;
default:
break;
}
}
static __always_inline void
xdp_flowtable_dnat_ipv6(const struct flow_offload *flow, struct ipv6hdr *ip6h,
void *data_end, enum flow_offload_tuple_dir dir)
{
struct in6_addr addr, nat_addr;
xdp_flowtable_get_dnat_ipv6(&nat_addr, flow, dir);
switch (dir) {
case FLOW_OFFLOAD_DIR_ORIGINAL:
addr = ip6h->daddr;
ip6h->daddr = nat_addr;
break;
case FLOW_OFFLOAD_DIR_REPLY:
addr = ip6h->saddr;
ip6h->saddr = nat_addr;
break;
default:
return;
}
xdp_flowtable_update_ipv6_csum(ip6h, data_end, &addr, &nat_addr);
}
static __always_inline void
xdp_flowtable_forward_ip(const struct flow_offload *flow, void *data,
void *data_end, struct flow_ports *ports,
enum flow_offload_tuple_dir dir,
unsigned long flags)
{
struct iphdr *iph = data + sizeof(struct ethhdr);
if (iph + 1 > data_end)
return;
if (flags & BIT(NF_FLOW_SNAT)) {
xdp_flowtable_snat_port(flow, ports, data_end, iph->protocol,
dir);
xdp_flowtable_snat_ip(flow, iph, data_end, dir);
}
if (flags & BIT(NF_FLOW_DNAT)) {
xdp_flowtable_dnat_port(flow, ports, data_end, iph->protocol,
dir);
xdp_flowtable_dnat_ip(flow, iph, data_end, dir);
}
ip_decrease_ttl(iph);
}
static __always_inline void
xdp_flowtable_forward_ipv6(const struct flow_offload *flow, void *data,
void *data_end, struct flow_ports *ports,
enum flow_offload_tuple_dir dir,
unsigned long flags)
{
struct ipv6hdr *ip6h = data + sizeof(struct ethhdr);
if (ip6h + 1 > data_end)
return;
if (flags & BIT(NF_FLOW_SNAT)) {
xdp_flowtable_snat_port(flow, ports, data_end, ip6h->nexthdr,
dir);
xdp_flowtable_snat_ipv6(flow, ip6h, data_end, dir);
}
if (flags & BIT(NF_FLOW_DNAT)) {
xdp_flowtable_dnat_port(flow, ports, data_end, ip6h->nexthdr,
dir);
xdp_flowtable_dnat_ipv6(flow, ip6h, data_end, dir);
}
ip6h->hop_limit--;
}
static __always_inline int xdp_flowtable_flags(struct xdp_md *ctx,
__u32 fib_flags)
{
void *data_end = (void *)(long)ctx->data_end;
struct flow_offload_tuple_rhash *tuplehash;
struct bpf_fib_lookup tuple = {
.ifindex = ctx->ingress_ifindex,
};
void *data = (void *)(long)ctx->data;
struct bpf_flowtable_opts opts = {};
enum flow_offload_tuple_dir dir;
struct ethhdr *eth = data;
struct flow_offload *flow;
struct flow_ports *ports;
unsigned long flags;
if (eth + 1 > data_end)
return XDP_PASS;
switch (eth->h_proto) {
case bpf_htons(ETH_P_IP): {
struct iphdr *iph = data + sizeof(*eth);
ports = (struct flow_ports *)(iph + 1);
if (ports + 1 > data_end)
return XDP_PASS;
/* ip fragmented traffic */
if (iph->frag_off & bpf_htons(IP_MF | IP_OFFSET))
return XDP_PASS;
/* ip options */
if (iph->ihl * 4 != sizeof(*iph))
return XDP_PASS;
if (iph->ttl <= 1)
return XDP_PASS;
if (xdp_flowtable_check_tcp_state(ports, data_end,
iph->protocol) < 0)
return XDP_PASS;
tuple.family = AF_INET;
tuple.tos = iph->tos;
tuple.l4_protocol = iph->protocol;
tuple.tot_len = bpf_ntohs(iph->tot_len);
tuple.ipv4_src = iph->saddr;
tuple.ipv4_dst = iph->daddr;
tuple.sport = ports->source;
tuple.dport = ports->dest;
break;
}
case bpf_htons(ETH_P_IPV6): {
struct in6_addr *src = (struct in6_addr *)tuple.ipv6_src;
struct in6_addr *dst = (struct in6_addr *)tuple.ipv6_dst;
struct ipv6hdr *ip6h = data + sizeof(*eth);
ports = (struct flow_ports *)(ip6h + 1);
if (ports + 1 > data_end)
return XDP_PASS;
if (ip6h->hop_limit <= 1)
return XDP_PASS;
if (xdp_flowtable_check_tcp_state(ports, data_end,
ip6h->nexthdr) < 0)
return XDP_PASS;
tuple.family = AF_INET6;
tuple.l4_protocol = ip6h->nexthdr;
tuple.tot_len = bpf_ntohs(ip6h->payload_len);
*src = ip6h->saddr;
*dst = ip6h->daddr;
tuple.sport = ports->source;
tuple.dport = ports->dest;
break;
}
default:
return XDP_PASS;
}
tuplehash = bpf_xdp_flow_lookup(ctx, &tuple, &opts, sizeof(opts));
if (!tuplehash)
return XDP_PASS;
flow = container_of(tuplehash, struct flow_offload, tuplehash);
if (bpf_core_read(&flags, sizeof(flags), &flow->flags))
return XDP_PASS;
if (tuplehash->tuple.xmit_type != FLOW_OFFLOAD_XMIT_NEIGH)
return XDP_PASS;
dir = tuplehash->tuple.dir;
if (dir >= FLOW_OFFLOAD_DIR_MAX)
return XDP_PASS;
/* update the destination address in case of dnatting before
* performing the route lookup
*/
if (tuple.family == AF_INET6) {
struct in6_addr *dst_addr = (struct in6_addr *)&tuple.ipv6_dst;
xdp_flowtable_get_dnat_ipv6(dst_addr, flow, dir);
} else {
xdp_flowtable_get_dnat_ip(&tuple.ipv4_dst, flow, dir);
}
if (bpf_fib_lookup(ctx, &tuple, sizeof(tuple), fib_flags) !=
BPF_FIB_LKUP_RET_SUCCESS)
return XDP_PASS;
/* Verify egress index has been configured as TX-port */
if (!bpf_map_lookup_elem(&xdp_tx_ports, &tuple.ifindex))
return XDP_PASS;
if (tuple.family == AF_INET6)
xdp_flowtable_forward_ipv6(flow, data, data_end, ports, dir,
flags);
else
xdp_flowtable_forward_ip(flow, data, data_end, ports, dir,
flags);
__builtin_memcpy(eth->h_dest, tuple.dmac, ETH_ALEN);
__builtin_memcpy(eth->h_source, tuple.smac, ETH_ALEN);
return bpf_redirect_map(&xdp_tx_ports, tuple.ifindex, 0);
}
SEC("xdp")
int xdp_fwd_flowtable_full(struct xdp_md *ctx)
{
return xdp_flowtable_flags(ctx, 0);
}
SEC("xdp")
int xdp_fwd_flowtable_direct(struct xdp_md *ctx)
{
return xdp_flowtable_flags(ctx, BPF_FIB_LOOKUP_DIRECT);
}
char _license[] SEC("license") = "GPL";