-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
nodeport.h
3312 lines (2870 loc) · 88.4 KB
/
nodeport.h
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
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */
/* Copyright Authors of Cilium */
#ifndef __NODEPORT_H_
#define __NODEPORT_H_
#include <bpf/ctx/ctx.h>
#include <bpf/api.h>
#include "bpf/compiler.h"
#include "tailcall.h"
#include "nat.h"
#include "edt.h"
#include "lb.h"
#include "common.h"
#include "overloadable.h"
#include "egress_gateway.h"
#include "eps.h"
#include "conntrack.h"
#include "csum.h"
#include "encap.h"
#include "identity.h"
#include "trace.h"
#include "ghash.h"
#include "pcap.h"
#include "host_firewall.h"
#include "stubs.h"
#include "proxy_hairpin.h"
#include "fib.h"
#ifdef ENABLE_NODEPORT
/* The IPv6 extension should be 8-bytes aligned */
struct dsr_opt_v6 {
struct ipv6_opt_hdr hdr;
__u8 opt_type;
__u8 opt_len;
union v6addr addr;
__be16 port;
__u16 pad;
};
struct dsr_opt_v4 {
__u8 type;
__u8 len;
__u16 port;
__u32 addr;
};
static __always_inline bool nodeport_uses_dsr(__u8 nexthdr __maybe_unused)
{
# if defined(ENABLE_DSR) && !defined(ENABLE_DSR_HYBRID)
return true;
# elif defined(ENABLE_DSR) && defined(ENABLE_DSR_HYBRID)
if (nexthdr == IPPROTO_TCP)
return true;
return false;
# else
return false;
# endif
}
static __always_inline bool
bpf_skip_recirculation(const struct __ctx_buff *ctx __maybe_unused)
{
/* From XDP layer, we do not go through an egress hook from
* here, hence nothing to be skipped.
*/
#if __ctx_is == __ctx_skb
return ctx->tc_index & TC_INDEX_F_SKIP_RECIRCULATION;
#else
return false;
#endif
}
#ifdef HAVE_ENCAP
static __always_inline int
nodeport_add_tunnel_encap(struct __ctx_buff *ctx, __u32 src_ip, __be16 src_port,
__be32 dst_ip, __u32 src_sec_identity, __u32 dst_sec_identity,
enum trace_reason ct_reason, __u32 monitor, int *ifindex)
{
/* Let kernel choose the outer source ip */
if (ctx_is_skb())
src_ip = 0;
return __encap_with_nodeid(ctx, src_ip, src_port, dst_ip,
src_sec_identity, dst_sec_identity, NOT_VTEP_DST,
ct_reason, monitor, ifindex);
}
# if defined(ENABLE_DSR) && DSR_ENCAP_MODE == DSR_ENCAP_GENEVE
static __always_inline int
nodeport_add_tunnel_encap_opt(struct __ctx_buff *ctx, __u32 src_ip, __be16 src_port,
__be32 dst_ip, __u32 src_sec_identity, __u32 dst_sec_identity,
void *opt, __u32 opt_len, enum trace_reason ct_reason,
__u32 monitor, int *ifindex)
{
/* Let kernel choose the outer source ip */
if (ctx_is_skb())
src_ip = 0;
return __encap_with_nodeid_opt(ctx, src_ip, src_port, dst_ip,
src_sec_identity, dst_sec_identity, NOT_VTEP_DST,
opt, opt_len, ct_reason, monitor, ifindex);
}
# endif
#endif /* HAVE_ENCAP */
static __always_inline bool dsr_fail_needs_reply(int code __maybe_unused)
{
#ifdef ENABLE_DSR_ICMP_ERRORS
if (code == DROP_FRAG_NEEDED)
return true;
#endif
return false;
}
static __always_inline bool dsr_is_too_big(struct __ctx_buff *ctx __maybe_unused,
__u16 expanded_len __maybe_unused)
{
#ifdef ENABLE_DSR_ICMP_ERRORS
if (expanded_len > THIS_MTU)
return true;
#endif
return false;
}
static __always_inline int
nodeport_fib_lookup_and_redirect(struct __ctx_buff *ctx,
struct bpf_fib_lookup_padded *fib_params,
__s8 *ext_err)
{
int oif = NATIVE_DEV_IFINDEX;
int ret;
ret = fib_lookup(ctx, &fib_params->l, sizeof(fib_params->l), 0);
*ext_err = (__s8)ret;
switch (ret) {
case BPF_FIB_LKUP_RET_SUCCESS:
case BPF_FIB_LKUP_RET_NO_NEIGH:
if ((__u32)oif == fib_params->l.ifindex)
return CTX_ACT_OK;
return fib_do_redirect(ctx, true, fib_params, ext_err, &oif);
default:
return DROP_NO_FIB;
}
}
#ifdef ENABLE_IPV6
static __always_inline bool nodeport_uses_dsr6(const struct ipv6_ct_tuple *tuple)
{
return nodeport_uses_dsr(tuple->nexthdr);
}
static __always_inline bool
nodeport_has_nat_conflict_ipv6(const struct ipv6hdr *ip6 __maybe_unused,
struct ipv6_nat_target *target __maybe_unused)
{
#if defined(TUNNEL_MODE) && defined(IS_BPF_OVERLAY)
union v6addr router_ip;
BPF_V6(router_ip, ROUTER_IP);
if (ipv6_addr_equals((union v6addr *)&ip6->saddr, &router_ip)) {
ipv6_addr_copy(&target->addr, &router_ip);
target->needs_ct = true;
return true;
}
#endif /* TUNNEL_MODE && IS_BPF_OVERLAY */
#if defined(IS_BPF_HOST)
const union v6addr dr_addr = IPV6_DIRECT_ROUTING;
__u32 dr_ifindex = DIRECT_ROUTING_DEV_IFINDEX;
/* See comment in nodeport_has_nat_conflict_ipv4(). */
if (dr_ifindex == NATIVE_DEV_IFINDEX &&
ipv6_addr_equals((union v6addr *)&ip6->saddr, &dr_addr)) {
ipv6_addr_copy(&target->addr, &dr_addr);
target->needs_ct = true;
return true;
}
#endif /* IS_BPF_HOST */
return false;
}
static __always_inline int nodeport_snat_fwd_ipv6(struct __ctx_buff *ctx,
struct trace_ctx *trace,
__s8 *ext_err)
{
struct ipv6_nat_target target = {
.min_port = NODEPORT_PORT_MIN_NAT,
.max_port = NODEPORT_PORT_MAX_NAT,
};
struct ipv6_ct_tuple tuple = {};
int hdrlen, l4_off, ret;
void *data, *data_end;
struct ipv6hdr *ip6;
if (!revalidate_data(ctx, &data, &data_end, &ip6))
return DROP_INVALID;
tuple.nexthdr = ip6->nexthdr;
hdrlen = ipv6_hdrlen(ctx, &tuple.nexthdr);
if (hdrlen < 0)
return hdrlen;
snat_v6_init_tuple(ip6, NAT_DIR_EGRESS, &tuple);
l4_off = ETH_HLEN + hdrlen;
if (lb_is_svc_proto(tuple.nexthdr) &&
!nodeport_uses_dsr6(&tuple) &&
nodeport_has_nat_conflict_ipv6(ip6, &target))
goto apply_snat;
ret = snat_v6_needs_masquerade(ctx, &tuple, l4_off, &target);
if (IS_ERR(ret))
goto out;
apply_snat:
ret = snat_v6_nat(ctx, &tuple, l4_off, &target, trace, ext_err);
if (IS_ERR(ret))
goto out;
/* See the equivalent v4 path for comment */
ctx_snat_done_set(ctx);
out:
if (ret == NAT_PUNT_TO_STACK)
ret = CTX_ACT_OK;
return ret;
}
#ifdef ENABLE_DSR
#if DSR_ENCAP_MODE == DSR_ENCAP_IPIP
static __always_inline void rss_gen_src6(union v6addr *src,
const union v6addr *client,
__be32 l4_hint)
{
__u32 bits = 128 - IPV6_RSS_PREFIX_BITS;
*src = (union v6addr)IPV6_RSS_PREFIX;
if (bits) {
__u32 todo;
if (bits > 96) {
todo = bits - 96;
src->p1 |= bpf_htonl(hash_32(client->p1 ^ l4_hint, todo));
bits -= todo;
}
if (bits > 64) {
todo = bits - 64;
src->p2 |= bpf_htonl(hash_32(client->p2 ^ l4_hint, todo));
bits -= todo;
}
if (bits > 32) {
todo = bits - 32;
src->p3 |= bpf_htonl(hash_32(client->p3 ^ l4_hint, todo));
bits -= todo;
}
src->p4 |= bpf_htonl(hash_32(client->p4 ^ l4_hint, bits));
}
}
static __always_inline int dsr_set_ipip6(struct __ctx_buff *ctx,
const struct ipv6hdr *ip6,
const union v6addr *backend_addr,
__be32 l4_hint, int *ohead)
{
__u16 payload_len = bpf_ntohs(ip6->payload_len) + sizeof(*ip6);
const int l3_off = ETH_HLEN;
union v6addr saddr;
struct {
__be16 payload_len;
__u8 nexthdr;
__u8 hop_limit;
} tp_new = {
.payload_len = bpf_htons(payload_len),
.nexthdr = IPPROTO_IPV6,
.hop_limit = IPDEFTTL,
};
if (dsr_is_too_big(ctx, payload_len + sizeof(*ip6))) {
*ohead = sizeof(*ip6);
return DROP_FRAG_NEEDED;
}
rss_gen_src6(&saddr, (union v6addr *)&ip6->saddr, l4_hint);
if (ctx_adjust_hroom(ctx, sizeof(*ip6), BPF_ADJ_ROOM_NET,
ctx_adjust_hroom_flags()))
return DROP_INVALID;
if (ctx_store_bytes(ctx, l3_off + offsetof(struct ipv6hdr, payload_len),
&tp_new.payload_len, 4, 0) < 0)
return DROP_WRITE_ERROR;
if (ctx_store_bytes(ctx, l3_off + offsetof(struct ipv6hdr, daddr),
backend_addr, sizeof(ip6->daddr), 0) < 0)
return DROP_WRITE_ERROR;
if (ctx_store_bytes(ctx, l3_off + offsetof(struct ipv6hdr, saddr),
&saddr, sizeof(ip6->saddr), 0) < 0)
return DROP_WRITE_ERROR;
return 0;
}
#elif DSR_ENCAP_MODE == DSR_ENCAP_NONE
static __always_inline int dsr_set_ext6(struct __ctx_buff *ctx,
struct ipv6hdr *ip6,
const union v6addr *svc_addr,
__be16 svc_port, int *ohead)
{
struct dsr_opt_v6 opt __align_stack_8 = {};
__u16 payload_len = bpf_ntohs(ip6->payload_len) + sizeof(opt);
__u16 total_len = bpf_ntohs(ip6->payload_len) + sizeof(struct ipv6hdr) + sizeof(opt);
__u8 nexthdr = ip6->nexthdr;
int hdrlen;
/* The IPv6 extension should be 8-bytes aligned */
build_bug_on((sizeof(struct dsr_opt_v6) % 8) != 0);
hdrlen = ipv6_hdrlen(ctx, &nexthdr);
if (hdrlen < 0)
return hdrlen;
/* See dsr_set_opt4(): */
if (nexthdr == IPPROTO_TCP) {
union tcp_flags tcp_flags = { .value = 0 };
if (l4_load_tcp_flags(ctx, ETH_HLEN + hdrlen, &tcp_flags) < 0)
return DROP_CT_INVALID_HDR;
if (!(tcp_flags.value & (TCP_FLAG_SYN)))
return 0;
}
if (dsr_is_too_big(ctx, total_len)) {
*ohead = sizeof(opt);
return DROP_FRAG_NEEDED;
}
opt.hdr.nexthdr = ip6->nexthdr;
ip6->nexthdr = NEXTHDR_DEST;
ip6->payload_len = bpf_htons(payload_len);
opt.hdr.hdrlen = DSR_IPV6_EXT_LEN;
opt.opt_type = DSR_IPV6_OPT_TYPE;
opt.opt_len = DSR_IPV6_OPT_LEN;
ipv6_addr_copy(&opt.addr, svc_addr);
opt.port = svc_port;
if (ctx_adjust_hroom(ctx, sizeof(opt), BPF_ADJ_ROOM_NET,
ctx_adjust_hroom_flags()))
return DROP_INVALID;
if (ctx_store_bytes(ctx, ETH_HLEN + sizeof(*ip6), &opt,
sizeof(opt), 0) < 0)
return DROP_INVALID;
return 0;
}
#elif DSR_ENCAP_MODE == DSR_ENCAP_GENEVE
static __always_inline int encap_geneve_dsr_opt6(struct __ctx_buff *ctx,
struct ipv6hdr *ip6,
const union v6addr *svc_addr,
__be16 svc_port,
int *ifindex, int *ohead)
{
struct remote_endpoint_info *info;
struct ipv6_ct_tuple tuple = {};
struct geneve_dsr_opt6 gopt;
union v6addr *dst;
bool need_opt = true;
__u16 encap_len = sizeof(struct ipv6hdr) + sizeof(struct udphdr) +
sizeof(struct genevehdr) + ETH_HLEN;
__u16 payload_len = bpf_ntohs(ip6->payload_len) + sizeof(*ip6);
__u32 dst_sec_identity;
__be32 tunnel_endpoint;
__u16 total_len = 0;
__be16 src_port;
int l4_off, ret;
build_bug_on((sizeof(gopt) % 4) != 0);
dst = (union v6addr *)&ip6->daddr;
info = lookup_ip6_remote_endpoint(dst, 0);
if (!info || info->tunnel_endpoint == 0)
return DROP_NO_TUNNEL_ENDPOINT;
tunnel_endpoint = info->tunnel_endpoint;
dst_sec_identity = info->sec_identity;
ret = lb6_extract_tuple(ctx, ip6, ETH_HLEN, &l4_off, &tuple);
if (IS_ERR(ret))
return ret;
src_port = tunnel_gen_src_port_v6(&tuple);
/* See encap_geneve_dsr_opt4(): */
if (tuple.nexthdr == IPPROTO_TCP) {
union tcp_flags tcp_flags = { .value = 0 };
if (l4_load_tcp_flags(ctx, l4_off, &tcp_flags) < 0)
return DROP_CT_INVALID_HDR;
if (!(tcp_flags.value & (TCP_FLAG_SYN)))
need_opt = false;
}
if (need_opt) {
encap_len += sizeof(struct geneve_dsr_opt6);
set_geneve_dsr_opt6(svc_port, svc_addr, &gopt);
}
total_len = encap_len + payload_len;
if (dsr_is_too_big(ctx, total_len)) {
*ohead = encap_len;
return DROP_FRAG_NEEDED;
}
if (need_opt)
return nodeport_add_tunnel_encap_opt(ctx,
IPV4_DIRECT_ROUTING,
src_port,
tunnel_endpoint,
WORLD_IPV6_ID,
dst_sec_identity,
&gopt,
sizeof(gopt),
(enum trace_reason)CT_NEW,
TRACE_PAYLOAD_LEN,
ifindex);
return nodeport_add_tunnel_encap(ctx,
IPV4_DIRECT_ROUTING,
src_port,
tunnel_endpoint,
WORLD_IPV6_ID,
dst_sec_identity,
(enum trace_reason)CT_NEW,
TRACE_PAYLOAD_LEN,
ifindex);
}
#endif /* DSR_ENCAP_MODE */
static __always_inline int find_dsr_v6(struct __ctx_buff *ctx, __u8 nexthdr,
struct dsr_opt_v6 *dsr_opt, bool *found)
{
struct ipv6_opt_hdr opthdr __align_stack_8;
int i, len = sizeof(struct ipv6hdr);
__u8 nh = nexthdr;
#pragma unroll
for (i = 0; i < IPV6_MAX_HEADERS; i++) {
switch (nh) {
case NEXTHDR_NONE:
return DROP_INVALID_EXTHDR;
case NEXTHDR_FRAGMENT:
return DROP_FRAG_NOSUPPORT;
case NEXTHDR_HOP:
case NEXTHDR_ROUTING:
case NEXTHDR_AUTH:
case NEXTHDR_DEST:
if (ctx_load_bytes(ctx, ETH_HLEN + len, &opthdr, sizeof(opthdr)) < 0)
return DROP_INVALID;
if (nh == NEXTHDR_DEST && opthdr.hdrlen == DSR_IPV6_EXT_LEN) {
if (ctx_load_bytes(ctx, ETH_HLEN + len, dsr_opt,
sizeof(*dsr_opt)) < 0)
return DROP_INVALID;
if (dsr_opt->opt_type == DSR_IPV6_OPT_TYPE &&
dsr_opt->opt_len == DSR_IPV6_OPT_LEN) {
*found = true;
return 0;
}
}
if (nh == NEXTHDR_AUTH)
len += ipv6_authlen(&opthdr);
else
len += ipv6_optlen(&opthdr);
nh = opthdr.nexthdr;
break;
default:
return 0;
}
}
/* Reached limit of supported extension headers */
return DROP_INVALID_EXTHDR;
}
static __always_inline int
nodeport_extract_dsr_v6(struct __ctx_buff *ctx,
struct ipv6hdr *ip6 __maybe_unused,
const struct ipv6_ct_tuple *tuple, int l4_off,
union v6addr *addr, __be16 *port, bool *dsr)
{
struct ipv6_ct_tuple tmp = *tuple;
if (tuple->nexthdr == IPPROTO_TCP) {
union tcp_flags tcp_flags = {};
if (l4_load_tcp_flags(ctx, l4_off, &tcp_flags) < 0)
return DROP_CT_INVALID_HDR;
ipv6_ct_tuple_reverse(&tmp);
if (!(tcp_flags.value & TCP_FLAG_SYN)) {
*dsr = ct_has_dsr_egress_entry6(get_ct_map6(&tmp), &tmp);
*port = 0;
return 0;
}
}
#if defined(IS_BPF_OVERLAY)
{
struct geneve_dsr_opt6 gopt;
int ret = ctx_get_tunnel_opt(ctx, &gopt, sizeof(gopt));
if (ret > 0) {
if (gopt.hdr.opt_class == bpf_htons(DSR_GENEVE_OPT_CLASS) &&
gopt.hdr.type == DSR_GENEVE_OPT_TYPE) {
*dsr = true;
*port = gopt.port;
ipv6_addr_copy(addr, (union v6addr *)&gopt.addr);
return 0;
}
}
}
#else
{
struct dsr_opt_v6 opt __align_stack_8 = {};
int ret;
ret = find_dsr_v6(ctx, ip6->nexthdr, &opt, dsr);
if (ret != 0)
return ret;
if (*dsr) {
*addr = opt.addr;
*port = opt.port;
return 0;
}
}
#endif
if (tuple->nexthdr == IPPROTO_TCP)
ct_update_dsr(get_ct_map6(&tmp), &tmp, false);
return 0;
}
static __always_inline struct ipv6_nat_entry *
nodeport_dsr_lookup_v6_nat_entry(const struct ipv6_ct_tuple *nat_tuple)
{
return snat_v6_lookup(nat_tuple);
}
static __always_inline int xlate_dsr_v6(struct __ctx_buff *ctx,
const struct ipv6_ct_tuple *tuple,
int l4_off)
{
struct ipv6_ct_tuple nat_tup = *tuple;
struct ipv6_nat_entry *entry;
nat_tup.flags = NAT_DIR_EGRESS;
nat_tup.sport = tuple->dport;
nat_tup.dport = tuple->sport;
entry = nodeport_dsr_lookup_v6_nat_entry(&nat_tup);
if (!entry)
return 0;
ctx_snat_done_set(ctx);
return snat_v6_rewrite_headers(ctx, nat_tup.nexthdr, ETH_HLEN, l4_off,
&nat_tup.saddr, &entry->to_saddr, IPV6_SADDR_OFF,
nat_tup.sport, entry->to_sport, TCP_SPORT_OFF);
}
static __always_inline int dsr_reply_icmp6(struct __ctx_buff *ctx,
const struct ipv6hdr *ip6 __maybe_unused,
const union v6addr *svc_addr __maybe_unused,
__be16 dport __maybe_unused,
int code, int ohead __maybe_unused)
{
#ifdef ENABLE_DSR_ICMP_ERRORS
const __s32 orig_dgram = 64, off = ETH_HLEN;
__u8 orig_ipv6_hdr[orig_dgram];
__be16 type = bpf_htons(ETH_P_IPV6);
__u64 len_new = off + sizeof(*ip6) + orig_dgram;
__u64 len_old = ctx_full_len(ctx);
void *data_end = ctx_data_end(ctx);
void *data = ctx_data(ctx);
__u8 reason = (__u8)-code;
__wsum wsum;
union macaddr smac, dmac;
struct icmp6hdr icmp __align_stack_8 = {
.icmp6_type = ICMPV6_PKT_TOOBIG,
.icmp6_mtu = bpf_htonl(THIS_MTU - ohead),
};
__u64 payload_len = sizeof(*ip6) + sizeof(icmp) + orig_dgram;
struct ipv6hdr ip __align_stack_8 = {
.version = 6,
.priority = ip6->priority,
.flow_lbl[0] = ip6->flow_lbl[0],
.flow_lbl[1] = ip6->flow_lbl[1],
.flow_lbl[2] = ip6->flow_lbl[2],
.nexthdr = IPPROTO_ICMPV6,
.hop_limit = IPDEFTTL,
.saddr = ip6->daddr,
.daddr = ip6->saddr,
.payload_len = bpf_htons((__u16)payload_len),
};
struct ipv6hdr inner_ipv6_hdr __align_stack_8 = *ip6;
__s32 l4_dport_offset;
/* DSR changes the destination address from service ip to pod ip and
* destination port from service port to pod port. While resppnding
* back with ICMP error, it is necessary to set it to original ip and
* port.
*/
ipv6_addr_copy((union v6addr *)&inner_ipv6_hdr.daddr, svc_addr);
if (inner_ipv6_hdr.nexthdr == IPPROTO_UDP)
l4_dport_offset = UDP_DPORT_OFF;
else if (inner_ipv6_hdr.nexthdr == IPPROTO_TCP)
l4_dport_offset = TCP_DPORT_OFF;
else
goto drop_err;
if (ctx_load_bytes(ctx, off + sizeof(inner_ipv6_hdr), orig_ipv6_hdr,
sizeof(orig_ipv6_hdr)) < 0)
goto drop_err;
memcpy(orig_ipv6_hdr + l4_dport_offset, &dport, sizeof(dport));
update_metrics(ctx_full_len(ctx), METRIC_EGRESS, reason);
if (eth_load_saddr(ctx, smac.addr, 0) < 0)
goto drop_err;
if (eth_load_daddr(ctx, dmac.addr, 0) < 0)
goto drop_err;
if (unlikely(data + len_new > data_end))
goto drop_err;
wsum = ipv6_pseudohdr_checksum(&ip, IPPROTO_ICMPV6,
bpf_ntohs(ip.payload_len), 0);
icmp.icmp6_cksum = csum_fold(csum_diff(NULL, 0, orig_ipv6_hdr, sizeof(orig_ipv6_hdr),
csum_diff(NULL, 0, &inner_ipv6_hdr,
sizeof(inner_ipv6_hdr),
csum_diff(NULL, 0, &icmp,
sizeof(icmp), wsum))));
if (ctx_adjust_troom(ctx, -(len_old - len_new)) < 0)
goto drop_err;
if (ctx_adjust_hroom(ctx, sizeof(ip) + sizeof(icmp),
BPF_ADJ_ROOM_NET,
ctx_adjust_hroom_flags()) < 0)
goto drop_err;
if (eth_store_daddr(ctx, smac.addr, 0) < 0)
goto drop_err;
if (eth_store_saddr(ctx, dmac.addr, 0) < 0)
goto drop_err;
if (ctx_store_bytes(ctx, ETH_ALEN * 2, &type, sizeof(type), 0) < 0)
goto drop_err;
if (ctx_store_bytes(ctx, off, &ip, sizeof(ip), 0) < 0)
goto drop_err;
if (ctx_store_bytes(ctx, off + sizeof(ip), &icmp,
sizeof(icmp), 0) < 0)
goto drop_err;
if (ctx_store_bytes(ctx, off + sizeof(ip) + sizeof(icmp), &inner_ipv6_hdr,
sizeof(inner_ipv6_hdr), 0) < 0)
goto drop_err;
if (ctx_store_bytes(ctx, off + sizeof(ip) + sizeof(icmp) +
sizeof(inner_ipv6_hdr) + l4_dport_offset,
&dport, sizeof(dport), 0) < 0)
goto drop_err;
return ctx_redirect(ctx, ctx_get_ifindex(ctx), 0);
drop_err:
#endif
return send_drop_notify_error(ctx, 0, code, CTX_ACT_DROP,
METRIC_EGRESS);
}
__section_tail(CILIUM_MAP_CALLS, CILIUM_CALL_IPV6_NODEPORT_DSR)
int tail_nodeport_ipv6_dsr(struct __ctx_buff *ctx)
{
struct bpf_fib_lookup_padded fib_params = {
.l = {
.family = AF_INET6,
.ifindex = ctx_get_ifindex(ctx),
},
};
int ret, oif = 0, ohead = 0;
void *data, *data_end;
struct ipv6hdr *ip6;
union v6addr addr;
__s8 ext_err = 0;
__be16 port;
if (!revalidate_data(ctx, &data, &data_end, &ip6)) {
ret = DROP_INVALID;
goto drop_err;
}
addr.p1 = ctx_load_meta(ctx, CB_ADDR_V6_1);
addr.p2 = ctx_load_meta(ctx, CB_ADDR_V6_2);
addr.p3 = ctx_load_meta(ctx, CB_ADDR_V6_3);
addr.p4 = ctx_load_meta(ctx, CB_ADDR_V6_4);
port = (__be16)ctx_load_meta(ctx, CB_PORT);
#if DSR_ENCAP_MODE == DSR_ENCAP_IPIP
ret = dsr_set_ipip6(ctx, ip6, &addr,
ctx_load_meta(ctx, CB_HINT), &ohead);
#elif DSR_ENCAP_MODE == DSR_ENCAP_NONE
ret = dsr_set_ext6(ctx, ip6, &addr, port, &ohead);
#elif DSR_ENCAP_MODE == DSR_ENCAP_GENEVE
ret = encap_geneve_dsr_opt6(ctx, ip6, &addr, port, &oif, &ohead);
if (!IS_ERR(ret)) {
if (ret == CTX_ACT_REDIRECT && oif) {
cilium_capture_out(ctx);
return ctx_redirect(ctx, oif, 0);
}
fib_params.l.family = AF_INET;
}
#else
# error "Invalid load balancer DSR encapsulation mode!"
#endif
if (IS_ERR(ret)) {
if (dsr_fail_needs_reply(ret))
return dsr_reply_icmp6(ctx, ip6, &addr, port, ret, ohead);
goto drop_err;
}
if (fib_params.l.family == AF_INET) {
struct iphdr *ip4;
if (!revalidate_data(ctx, &data, &data_end, &ip4)) {
ret = DROP_INVALID;
goto drop_err;
}
fib_params.l.ipv4_src = ip4->saddr;
fib_params.l.ipv4_dst = ip4->daddr;
} else {
if (!revalidate_data(ctx, &data, &data_end, &ip6)) {
ret = DROP_INVALID;
goto drop_err;
}
ipv6_addr_copy((union v6addr *)&fib_params.l.ipv6_src,
(union v6addr *)&ip6->saddr);
ipv6_addr_copy((union v6addr *)&fib_params.l.ipv6_dst,
(union v6addr *)&ip6->daddr);
}
ret = fib_redirect(ctx, true, &fib_params, &ext_err, &oif);
if (fib_ok(ret)) {
cilium_capture_out(ctx);
return ret;
}
drop_err:
return send_drop_notify_error_ext(ctx, 0, ret, ext_err,
CTX_ACT_DROP, METRIC_EGRESS);
}
static __always_inline int
nodeport_dsr_ingress_ipv6(struct __ctx_buff *ctx, struct ipv6_ct_tuple *tuple,
int l4_off, union v6addr *addr, __be16 port,
__s8 *ext_err)
{
struct ct_state ct_state_new = {};
struct ct_state ct_state = {};
__u32 monitor = 0;
int ret;
/* look up with SCOPE_FORWARD: */
__ipv6_ct_tuple_reverse(tuple);
ret = ct_lazy_lookup6(get_ct_map6(tuple), tuple, ctx, l4_off,
CT_EGRESS, SCOPE_FORWARD, CT_ENTRY_DSR,
&ct_state, &monitor);
switch (ret) {
case CT_NEW:
case CT_REOPENED:
create_ct:
if (port == 0)
return DROP_INVALID;
ct_state_new.src_sec_id = WORLD_IPV6_ID;
ct_state_new.dsr = 1;
ret = ct_create6(get_ct_map6(tuple), NULL, tuple, ctx,
CT_EGRESS, &ct_state_new, false, false, ext_err);
if (!IS_ERR(ret))
ret = snat_v6_create_dsr(tuple, addr, port, ext_err);
if (IS_ERR(ret))
return ret;
break;
case CT_ESTABLISHED:
if (tuple->nexthdr == IPPROTO_TCP && port)
goto create_ct;
break;
default:
return DROP_UNKNOWN_CT;
}
return CTX_ACT_OK;
}
#endif /* ENABLE_DSR */
static __always_inline struct lb6_reverse_nat *
nodeport_rev_dnat_get_info_ipv6(struct __ctx_buff *ctx,
struct ipv6_ct_tuple *tuple)
{
struct ipv6_nat_entry *dsr_entry __maybe_unused;
struct ipv6_ct_tuple dsr_tuple __maybe_unused;
__u16 rev_nat_index = 0;
if (!ct_has_nodeport_egress_entry6(get_ct_map6(tuple), tuple,
&rev_nat_index, is_defined(ENABLE_DSR)))
return NULL;
if (rev_nat_index)
return lb6_lookup_rev_nat_entry(ctx, rev_nat_index);
#ifdef ENABLE_DSR
dsr_tuple = *tuple;
dsr_tuple.flags = NAT_DIR_EGRESS;
dsr_tuple.sport = tuple->dport;
dsr_tuple.dport = tuple->sport;
dsr_entry = nodeport_dsr_lookup_v6_nat_entry(&dsr_tuple);
if (dsr_entry)
return &dsr_entry->nat_info;
#endif
return NULL;
}
#ifdef ENABLE_NAT_46X64_GATEWAY
__section_tail(CILIUM_MAP_CALLS, CILIUM_CALL_IPV46_RFC8215)
int tail_nat_ipv46(struct __ctx_buff *ctx)
{
int ret, oif = 0, l3_off = ETH_HLEN;
void *data, *data_end;
struct ipv6hdr *ip6;
struct iphdr *ip4;
__s8 ext_err = 0;
if (!revalidate_data(ctx, &data, &data_end, &ip4)) {
ret = DROP_INVALID;
goto drop_err;
}
if (nat46_rfc8215(ctx, ip4, l3_off)) {
ret = DROP_NAT46;
goto drop_err;
}
if (!revalidate_data(ctx, &data, &data_end, &ip6)) {
ret = DROP_INVALID;
goto drop_err;
}
ret = fib_redirect_v6(ctx, l3_off, ip6, false, &ext_err, &oif);
if (fib_ok(ret)) {
cilium_capture_out(ctx);
return ret;
}
drop_err:
return send_drop_notify_error_ext(ctx, 0, ret, ext_err,
CTX_ACT_DROP, METRIC_EGRESS);
}
__section_tail(CILIUM_MAP_CALLS, CILIUM_CALL_IPV64_RFC8215)
int tail_nat_ipv64(struct __ctx_buff *ctx)
{
int ret, oif = 0, l3_off = ETH_HLEN;
void *data, *data_end;
struct ipv6hdr *ip6;
struct iphdr *ip4;
__s8 ext_err = 0;
if (!revalidate_data(ctx, &data, &data_end, &ip6)) {
ret = DROP_INVALID;
goto drop_err;
}
if (nat64_rfc8215(ctx, ip6)) {
ret = DROP_NAT64;
goto drop_err;
}
if (!revalidate_data(ctx, &data, &data_end, &ip4)) {
ret = DROP_INVALID;
goto drop_err;
}
ret = fib_redirect_v4(ctx, l3_off, ip4, false, &ext_err, &oif);
if (fib_ok(ret)) {
cilium_capture_out(ctx);
return ret;
}
drop_err:
return send_drop_notify_error_ext(ctx, 0, ret, ext_err,
CTX_ACT_DROP, METRIC_EGRESS);
}
#endif /* ENABLE_NAT_46X64_GATEWAY */
static __always_inline int
nodeport_rev_dnat_ingress_ipv6(struct __ctx_buff *ctx, struct trace_ctx *trace,
__s8 *ext_err)
{
#ifdef ENABLE_NAT_46X64_GATEWAY
const bool nat_46x64_fib = nat46x64_cb_route(ctx);
#endif
struct bpf_fib_lookup_padded fib_params = {
.l = {
.family = AF_INET6,
.ifindex = ctx_get_ifindex(ctx),
},
};
int ret, l4_off;
struct ipv6_ct_tuple tuple = {};
struct ct_state ct_state = {};
void *data, *data_end;
struct ipv6hdr *ip6;
__u32 tunnel_endpoint __maybe_unused = 0;
__u32 dst_sec_identity __maybe_unused = 0;
__be16 src_port __maybe_unused = 0;
int ifindex = 0;
if (!revalidate_data(ctx, &data, &data_end, &ip6))
return DROP_INVALID;
#ifdef ENABLE_NAT_46X64_GATEWAY
if (nat_46x64_fib)
goto fib_lookup;
#endif
ret = lb6_extract_tuple(ctx, ip6, ETH_HLEN, &l4_off, &tuple);
if (ret < 0) {
if (ret == DROP_UNSUPP_SERVICE_PROTO || ret == DROP_UNKNOWN_L4)
goto out;
return ret;
}
ret = ct_lazy_lookup6(get_ct_map6(&tuple), &tuple, ctx, l4_off,
CT_INGRESS, SCOPE_REVERSE, CT_ENTRY_NODEPORT,
&ct_state, &trace->monitor);
if (ret == CT_REPLY) {
trace->reason = TRACE_REASON_CT_REPLY;
ret = ipv6_l3(ctx, ETH_HLEN, NULL, NULL, METRIC_EGRESS);
if (unlikely(ret != CTX_ACT_OK))
return ret;
ret = lb6_rev_nat(ctx, l4_off, ct_state.rev_nat_index,
&tuple, REV_NAT_F_TUPLE_SADDR);
if (IS_ERR(ret))
return ret;
if (!revalidate_data(ctx, &data, &data_end, &ip6))
return DROP_INVALID;
ctx_snat_done_set(ctx);
#ifndef HAVE_FIB_IFINDEX
ifindex = ct_state.ifindex;
#endif
#ifdef TUNNEL_MODE
{
union v6addr *dst = (union v6addr *)&ip6->daddr;
struct remote_endpoint_info *info;
info = lookup_ip6_remote_endpoint(dst, 0);
if (info && info->tunnel_endpoint) {
tunnel_endpoint = info->tunnel_endpoint;
dst_sec_identity = info->sec_identity;
goto encap_redirect;
}
}
#endif
goto fib_lookup;
}
out:
return CTX_ACT_OK;
#ifdef TUNNEL_MODE
encap_redirect:
src_port = tunnel_gen_src_port_v6(&tuple);
ret = nodeport_add_tunnel_encap(ctx, IPV4_DIRECT_ROUTING, src_port,
tunnel_endpoint, SECLABEL, dst_sec_identity,
trace->reason, trace->monitor, &ifindex);
if (IS_ERR(ret))
return ret;
if (ret == CTX_ACT_REDIRECT && ifindex)
return ctx_redirect(ctx, ifindex, 0);
goto fib_ipv4;