-
Notifications
You must be signed in to change notification settings - Fork 3k
/
nodeport.h
2204 lines (1938 loc) · 58.3 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 */
/* Copyright (C) 2019-2020 Authors of Cilium */
#ifndef __NODEPORT_H_
#define __NODEPORT_H_
#include <bpf/ctx/ctx.h>
#include <bpf/api.h>
#include "tailcall.h"
#include "nat.h"
#include "edt.h"
#include "lb.h"
#include "common.h"
#include "overloadable.h"
#include "eps.h"
#include "conntrack.h"
#include "csum.h"
#include "encap.h"
#include "trace.h"
#include "ghash.h"
#include "pcap.h"
#include "host_firewall.h"
#define CB_SRC_IDENTITY 0
#ifdef ENABLE_NODEPORT
/* Define dummy values to make bpf_{lxc,overlay}.c to compile */
#ifndef NATIVE_DEV_IFINDEX
#define NATIVE_DEV_IFINDEX 0
#endif
#ifndef DSR_ENCAP_MODE
#define DSR_ENCAP_MODE 0
#define DSR_ENCAP_IPIP 2
#endif
#if defined(ENABLE_IPV4) && defined(ENABLE_MASQUERADE) && !defined(IPV4_MASQUERADE)
#define IPV4_MASQUERADE 0
#endif
#endif /* ENABLE_NODEPORT */
static __always_inline __maybe_unused void
bpf_skip_nodeport_clear(struct __ctx_buff *ctx)
{
ctx_skip_nodeport_clear(ctx);
}
static __always_inline __maybe_unused void
bpf_skip_nodeport_set(struct __ctx_buff *ctx)
{
ctx_skip_nodeport_set(ctx);
}
static __always_inline __maybe_unused bool
bpf_skip_nodeport(struct __ctx_buff *ctx)
{
return ctx_skip_nodeport(ctx);
}
#ifdef ENABLE_NODEPORT
#ifdef ENABLE_IPV4
struct bpf_elf_map __section_maps NODEPORT_NEIGH4 = {
.type = BPF_MAP_TYPE_LRU_HASH,
.size_key = sizeof(__be32), /* ipv4 addr */
.size_value = sizeof(union macaddr), /* hw addr */
.pinning = PIN_GLOBAL_NS,
.max_elem = NODEPORT_NEIGH4_SIZE,
};
#endif /* ENABLE_IPV4 */
#ifdef ENABLE_IPV6
struct bpf_elf_map __section_maps NODEPORT_NEIGH6 = {
.type = BPF_MAP_TYPE_LRU_HASH,
.size_key = sizeof(union v6addr), /* ipv6 addr */
.size_value = sizeof(union macaddr), /* hw addr */
.pinning = PIN_GLOBAL_NS,
.max_elem = NODEPORT_NEIGH6_SIZE,
};
/* The IPv6 extension should be 8-bytes aligned */
struct dsr_opt_v6 {
__u8 nexthdr;
__u8 len;
__u8 opt_type;
__u8 opt_len;
union v6addr addr;
__be32 port;
};
#endif /* ENABLE_IPV6 */
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 nodeport_lb_hairpin(void)
{
return is_defined(ENABLE_NODEPORT_HAIRPIN);
}
static __always_inline bool fib_lookup_bypass(void)
{
return is_defined(ENABLE_FIB_LOOKUP_BYPASS);
}
static __always_inline void
bpf_mark_snat_done(struct __ctx_buff *ctx __maybe_unused)
{
/* From XDP layer, we do not go through an egress hook from
* here, hence nothing to be done.
*/
#if __ctx_is == __ctx_skb
ctx->mark |= MARK_MAGIC_SNAT_DONE;
#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
}
static __always_inline __u64 ctx_adjust_hroom_dsr_flags(void)
{
#ifdef BPF_HAVE_CSUM_LEVEL
return BPF_F_ADJ_ROOM_NO_CSUM_RESET;
#else
return 0;
#endif
}
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
maybe_add_l2_hdr(struct __ctx_buff *ctx __maybe_unused,
__u32 ifindex __maybe_unused,
bool *l2_hdr_required __maybe_unused)
{
if (IS_L3_DEV(ifindex))
/* NodePort request is going to be redirected to L3 dev, so skip
* L2 addr settings.
*/
*l2_hdr_required = false;
else if (ETH_HLEN == 0) {
/* NodePort request is going to be redirected from L3 to L2 dev,
* so we need to create L2 hdr first.
*/
__u16 proto = ctx_get_protocol(ctx);
if (ctx_change_head(ctx, __ETH_HLEN, 0))
return DROP_INVALID;
if (eth_store_proto(ctx, proto, 0) < 0)
return DROP_WRITE_ERROR;
}
return 0;
}
#ifdef ENABLE_IPV6
static __always_inline bool nodeport_uses_dsr6(const struct ipv6_ct_tuple *tuple)
{
return nodeport_uses_dsr(tuple->nexthdr);
}
/* TODO(brb): after GH#6320, we can move snat_v{4,6}_needed() to lib/nat.h, as
* then the helper function won't depend the dsr checks.
*/
static __always_inline bool snat_v6_needed(struct __ctx_buff *ctx,
union v6addr *addr)
{
void *data, *data_end;
struct ipv6hdr *ip6;
if (!revalidate_data(ctx, &data, &data_end, &ip6))
return false;
#ifdef ENABLE_DSR_HYBRID
{
__u8 nexthdr = ip6->nexthdr;
int ret;
ret = ipv6_hdrlen(ctx, ETH_HLEN, &nexthdr);
if (ret > 0) {
if (nodeport_uses_dsr(nexthdr))
return false;
}
}
#endif /* ENABLE_DSR_HYBRID */
/* See snat_v4_needed(). */
return !ipv6_addrcmp((union v6addr *)&ip6->saddr, addr);
}
static __always_inline int nodeport_nat_ipv6_fwd(struct __ctx_buff *ctx,
union v6addr *addr)
{
struct ipv6_nat_target target = {
.min_port = NODEPORT_PORT_MIN_NAT,
.max_port = NODEPORT_PORT_MAX_NAT,
};
int ret;
ipv6_addr_copy(&target.addr, addr);
ret = snat_v6_needed(ctx, addr) ?
snat_v6_process(ctx, NAT_DIR_EGRESS, &target) : CTX_ACT_OK;
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,
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_dsr_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,
union v6addr *svc_addr,
__be32 svc_port, int *ohead)
{
struct dsr_opt_v6 opt __align_stack_8 = {};
__u16 payload_len = bpf_ntohs(ip6->payload_len) + sizeof(opt);
if (dsr_is_too_big(ctx, payload_len)) {
*ohead = sizeof(opt);
return DROP_FRAG_NEEDED;
}
opt.nexthdr = ip6->nexthdr;
ip6->nexthdr = NEXTHDR_DEST;
ip6->payload_len = bpf_htons(payload_len);
opt.len = 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_dsr_flags()))
return DROP_INVALID;
if (ctx_store_bytes(ctx, ETH_HLEN + sizeof(*ip6), &opt,
sizeof(opt), 0) < 0)
return DROP_INVALID;
return 0;
}
#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;
}
}
nh = opthdr.nexthdr;
if (nh == NEXTHDR_AUTH)
len += ipv6_authlen(&opthdr);
else
len += ipv6_optlen(&opthdr);
break;
default:
return 0;
}
}
/* Reached limit of supported extension headers */
return DROP_INVALID_EXTHDR;
}
static __always_inline int handle_dsr_v6(struct __ctx_buff *ctx, bool *dsr)
{
struct dsr_opt_v6 opt __align_stack_8 = {};
void *data, *data_end;
struct ipv6hdr *ip6;
int ret;
if (!revalidate_data(ctx, &data, &data_end, &ip6))
return DROP_INVALID;
ret = find_dsr_v6(ctx, ip6->nexthdr, &opt, dsr);
if (ret != 0)
return ret;
if (*dsr) {
if (snat_v6_create_dsr(ctx, &opt.addr, opt.port) < 0)
return DROP_INVALID;
}
return 0;
}
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;
int ret = 0;
nat_tup.flags = NAT_DIR_EGRESS;
nat_tup.sport = tuple->dport;
nat_tup.dport = tuple->sport;
entry = snat_v6_lookup(&nat_tup);
if (entry)
ret = snat_v6_rewrite_egress(ctx, &nat_tup, entry, l4_off);
return ret;
}
static __always_inline int dsr_reply_icmp6(struct __ctx_buff *ctx,
struct ipv6hdr *ip6 __maybe_unused,
int code, int ohead __maybe_unused)
{
#ifdef ENABLE_DSR_ICMP_ERRORS
const __s32 orig_dgram = 64, off = ETH_HLEN;
const __u32 l3_max = sizeof(*ip6) + 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);
__wsum wsum;
union macaddr smac, dmac;
struct icmp6hdr icmp __align_stack_8 = {
.icmp6_type = ICMPV6_PKT_TOOBIG,
.icmp6_mtu = bpf_htonl(THIS_MTU - ohead),
};
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(sizeof(icmp) + len_new - off),
};
update_metrics(ctx_full_len(ctx), METRIC_EGRESS, -code);
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, data + off, l3_max,
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_dsr_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;
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 = DIRECT_ROUTING_DEV_IFINDEX,
},
};
union macaddr *dmac = NULL;
void *data, *data_end;
struct ipv6hdr *ip6;
union v6addr addr;
int ret, ohead = 0;
bool l2_hdr_required = true;
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);
#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,
ctx_load_meta(ctx, CB_PORT), &ohead);
#else
# error "Invalid load balancer DSR encapsulation mode!"
#endif
if (unlikely(ret)) {
if (dsr_fail_needs_reply(ret))
return dsr_reply_icmp6(ctx, ip6, ret, ohead);
goto drop_err;
}
if (!revalidate_data(ctx, &data, &data_end, &ip6)) {
ret = DROP_INVALID;
goto drop_err;
}
ret = maybe_add_l2_hdr(ctx, DIRECT_ROUTING_DEV_IFINDEX,
&l2_hdr_required);
if (ret != 0)
goto drop_err;
if (!l2_hdr_required)
goto out_send;
else if (!revalidate_data_with_eth_hlen(ctx, &data, &data_end, &ip6,
__ETH_HLEN))
return DROP_INVALID;
if (nodeport_lb_hairpin())
dmac = map_lookup_elem(&NODEPORT_NEIGH6, &ip6->daddr);
if (dmac) {
union macaddr mac = NATIVE_DEV_MAC_BY_IFINDEX(fib_params.l.ifindex);
if (eth_store_daddr_aligned(ctx, dmac->addr, 0) < 0) {
ret = DROP_WRITE_ERROR;
goto drop_err;
}
if (eth_store_saddr_aligned(ctx, mac.addr, 0) < 0) {
ret = DROP_WRITE_ERROR;
goto drop_err;
}
} else {
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_lookup(ctx, &fib_params.l, sizeof(fib_params),
BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_OUTPUT);
if (ret != 0) {
ret = DROP_NO_FIB;
goto drop_err;
}
if (nodeport_lb_hairpin())
map_update_elem(&NODEPORT_NEIGH6, &ip6->daddr,
fib_params.l.dmac, 0);
if (eth_store_daddr(ctx, fib_params.l.dmac, 0) < 0) {
ret = DROP_WRITE_ERROR;
goto drop_err;
}
if (eth_store_saddr(ctx, fib_params.l.smac, 0) < 0) {
ret = DROP_WRITE_ERROR;
goto drop_err;
}
}
out_send:
cilium_capture_out(ctx);
return ctx_redirect(ctx, fib_params.l.ifindex, 0);
drop_err:
return send_drop_notify_error(ctx, 0, ret, CTX_ACT_DROP, METRIC_EGRESS);
}
#endif /* ENABLE_DSR */
__section_tail(CILIUM_MAP_CALLS, CILIUM_CALL_IPV6_NODEPORT_NAT)
int tail_nodeport_nat_ipv6(struct __ctx_buff *ctx)
{
int ret, dir = ctx_load_meta(ctx, CB_NAT);
union v6addr tmp = IPV6_DIRECT_ROUTING;
struct bpf_fib_lookup_padded fib_params = {
.l = {
.family = AF_INET6,
.ifindex = DIRECT_ROUTING_DEV_IFINDEX,
},
};
struct ipv6_nat_target target = {
.min_port = NODEPORT_PORT_MIN_NAT,
.max_port = NODEPORT_PORT_MAX_NAT,
.src_from_world = true,
};
union macaddr *dmac = NULL;
void *data, *data_end;
struct ipv6hdr *ip6;
bool l2_hdr_required = true;
target.addr = tmp;
#ifdef TUNNEL_MODE
if (dir == NAT_DIR_EGRESS) {
struct remote_endpoint_info *info;
union v6addr *dst;
if (!revalidate_data(ctx, &data, &data_end, &ip6)) {
ret = DROP_INVALID;
goto drop_err;
}
dst = (union v6addr *)&ip6->daddr;
info = ipcache_lookup6(&IPCACHE_MAP, dst, V6_CACHE_KEY_LEN);
if (info != NULL && info->tunnel_endpoint != 0) {
ret = __encap_with_nodeid(ctx, info->tunnel_endpoint,
SECLABEL, TRACE_PAYLOAD_LEN);
if (ret)
goto drop_err;
BPF_V6(target.addr, ROUTER_IP);
fib_params.l.ifindex = ENCAP_IFINDEX;
/* fib lookup not necessary when going over tunnel. */
if (eth_store_daddr(ctx, fib_params.l.dmac, 0) < 0) {
ret = DROP_WRITE_ERROR;
goto drop_err;
}
if (eth_store_saddr(ctx, fib_params.l.smac, 0) < 0) {
ret = DROP_WRITE_ERROR;
goto drop_err;
}
}
}
#endif
ret = snat_v6_process(ctx, dir, &target);
if (IS_ERR(ret)) {
/* In case of no mapping, recircle back to main path. SNAT is very
* expensive in terms of instructions (since we don't have BPF to
* BPF calls as we use tail calls) and complexity, hence this is
* done inside a tail call here.
*/
if (dir == NAT_DIR_INGRESS) {
bpf_skip_nodeport_set(ctx);
ep_tail_call(ctx, CILIUM_CALL_IPV6_FROM_LXC);
ret = DROP_MISSED_TAIL_CALL;
goto drop_err;
}
if (ret != NAT_PUNT_TO_STACK)
goto drop_err;
}
bpf_mark_snat_done(ctx);
if (dir == NAT_DIR_INGRESS) {
ep_tail_call(ctx, CILIUM_CALL_IPV6_NODEPORT_REVNAT);
ret = DROP_MISSED_TAIL_CALL;
goto drop_err;
}
#ifdef TUNNEL_MODE
if (fib_params.l.ifindex == ENCAP_IFINDEX)
goto out_send;
#endif
if (!revalidate_data(ctx, &data, &data_end, &ip6)) {
ret = DROP_INVALID;
goto drop_err;
}
ret = maybe_add_l2_hdr(ctx, DIRECT_ROUTING_DEV_IFINDEX,
&l2_hdr_required);
if (ret != 0)
goto drop_err;
if (!l2_hdr_required)
goto out_send;
else if (!revalidate_data_with_eth_hlen(ctx, &data, &data_end, &ip6,
__ETH_HLEN))
return DROP_INVALID;
if (nodeport_lb_hairpin())
dmac = map_lookup_elem(&NODEPORT_NEIGH6, &ip6->daddr);
if (dmac) {
union macaddr mac = NATIVE_DEV_MAC_BY_IFINDEX(fib_params.l.ifindex);
if (eth_store_daddr_aligned(ctx, dmac->addr, 0) < 0) {
ret = DROP_WRITE_ERROR;
goto drop_err;
}
if (eth_store_saddr_aligned(ctx, mac.addr, 0) < 0) {
ret = DROP_WRITE_ERROR;
goto drop_err;
}
} else {
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_lookup(ctx, &fib_params.l, sizeof(fib_params),
BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_OUTPUT);
if (ret != 0) {
ret = DROP_NO_FIB;
goto drop_err;
}
if (nodeport_lb_hairpin())
map_update_elem(&NODEPORT_NEIGH6, &ip6->daddr,
fib_params.l.dmac, 0);
if (eth_store_daddr(ctx, fib_params.l.dmac, 0) < 0) {
ret = DROP_WRITE_ERROR;
goto drop_err;
}
if (eth_store_saddr(ctx, fib_params.l.smac, 0) < 0) {
ret = DROP_WRITE_ERROR;
goto drop_err;
}
}
out_send:
cilium_capture_out(ctx);
return ctx_redirect(ctx, fib_params.l.ifindex, 0);
drop_err:
return send_drop_notify_error(ctx, 0, ret, CTX_ACT_DROP,
dir == NAT_DIR_INGRESS ?
METRIC_INGRESS : METRIC_EGRESS);
}
/* See nodeport_lb4(). */
static __always_inline int nodeport_lb6(struct __ctx_buff *ctx,
__u32 src_identity)
{
int ret, l3_off = ETH_HLEN, l4_off, hdrlen;
struct ipv6_ct_tuple tuple = {};
void *data, *data_end;
struct ipv6hdr *ip6;
struct csum_offset csum_off = {};
struct lb6_service *svc;
struct lb6_key key = {};
struct ct_state ct_state_new = {};
union macaddr smac, *mac;
bool backend_local;
__u32 monitor = 0;
cilium_capture_in(ctx);
if (!revalidate_data(ctx, &data, &data_end, &ip6))
return DROP_INVALID;
tuple.nexthdr = ip6->nexthdr;
ipv6_addr_copy(&tuple.daddr, (union v6addr *) &ip6->daddr);
ipv6_addr_copy(&tuple.saddr, (union v6addr *) &ip6->saddr);
hdrlen = ipv6_hdrlen(ctx, l3_off, &tuple.nexthdr);
if (hdrlen < 0)
return hdrlen;
l4_off = l3_off + hdrlen;
ret = lb6_extract_key(ctx, &tuple, l4_off, &key, &csum_off, CT_EGRESS);
if (IS_ERR(ret)) {
if (ret == DROP_NO_SERVICE)
goto skip_service_lookup;
else if (ret == DROP_UNKNOWN_L4)
return CTX_ACT_OK;
else
return ret;
}
svc = lb6_lookup_service(&key, false);
if (svc) {
const bool skip_l3_xlate = DSR_ENCAP_MODE == DSR_ENCAP_IPIP;
if (!lb6_src_range_ok(svc, (union v6addr *)&ip6->saddr))
return DROP_NOT_IN_SRC_RANGE;
ret = lb6_local(get_ct_map6(&tuple), ctx, l3_off, l4_off,
&csum_off, &key, &tuple, svc, &ct_state_new,
skip_l3_xlate);
if (IS_ERR(ret))
return ret;
}
if (!svc || !lb6_svc_is_routable(svc)) {
if (svc)
return DROP_IS_CLUSTER_IP;
skip_service_lookup:
ctx_set_xfer(ctx, XFER_PKT_NO_SVC);
if (nodeport_uses_dsr6(&tuple))
return CTX_ACT_OK;
ctx_store_meta(ctx, CB_NAT, NAT_DIR_INGRESS);
ctx_store_meta(ctx, CB_SRC_IDENTITY, src_identity);
ep_tail_call(ctx, CILIUM_CALL_IPV6_NODEPORT_NAT);
return DROP_MISSED_TAIL_CALL;
}
backend_local = __lookup_ip6_endpoint(&tuple.daddr);
if (!backend_local && lb6_svc_is_hostport(svc))
return DROP_INVALID;
if (backend_local || !nodeport_uses_dsr6(&tuple)) {
struct ct_state ct_state = {};
ret = ct_lookup6(get_ct_map6(&tuple), &tuple, ctx, l4_off,
CT_EGRESS, &ct_state, &monitor);
switch (ret) {
case CT_NEW:
redo_all:
ct_state_new.src_sec_id = SECLABEL;
ct_state_new.node_port = 1;
ct_state_new.ifindex = NATIVE_DEV_IFINDEX;
ret = ct_create6(get_ct_map6(&tuple), NULL, &tuple, ctx,
CT_EGRESS, &ct_state_new, false);
if (IS_ERR(ret))
return ret;
if (backend_local) {
ct_flip_tuple_dir6(&tuple);
redo_local:
ct_state_new.rev_nat_index = 0;
ret = ct_create6(get_ct_map6(&tuple), NULL,
&tuple, ctx, CT_INGRESS,
&ct_state_new, false);
if (IS_ERR(ret))
return ret;
}
break;
case CT_REOPENED:
case CT_ESTABLISHED:
case CT_REPLY:
if (unlikely(ct_state.rev_nat_index !=
svc->rev_nat_index))
goto redo_all;
if (backend_local) {
ct_flip_tuple_dir6(&tuple);
if (!__ct_entry_keep_alive(get_ct_map6(&tuple),
&tuple)) {
ct_state_new.src_sec_id = SECLABEL;
ct_state_new.node_port = 1;
ct_state_new.ifindex = NATIVE_DEV_IFINDEX;
goto redo_local;
}
}
break;
default:
return DROP_UNKNOWN_CT;
}
if (!revalidate_data(ctx, &data, &data_end, &ip6))
return DROP_INVALID;
if (eth_load_saddr(ctx, smac.addr, 0) < 0)
return DROP_INVALID;
mac = map_lookup_elem(&NODEPORT_NEIGH6, &ip6->saddr);
if (!mac || eth_addrcmp(mac, &smac)) {
ret = map_update_elem(&NODEPORT_NEIGH6, &ip6->saddr,
&smac, 0);
if (ret < 0)
return ret;
}
}
if (!backend_local) {
edt_set_aggregate(ctx, 0);
if (nodeport_uses_dsr6(&tuple)) {
#if DSR_ENCAP_MODE == DSR_ENCAP_IPIP
ctx_store_meta(ctx, CB_HINT,
((__u32)tuple.sport << 16) | tuple.dport);
ctx_store_meta(ctx, CB_ADDR_V6_1, tuple.daddr.p1);
ctx_store_meta(ctx, CB_ADDR_V6_2, tuple.daddr.p2);
ctx_store_meta(ctx, CB_ADDR_V6_3, tuple.daddr.p3);
ctx_store_meta(ctx, CB_ADDR_V6_4, tuple.daddr.p4);
#elif DSR_ENCAP_MODE == DSR_ENCAP_NONE
ctx_store_meta(ctx, CB_PORT, key.dport);
ctx_store_meta(ctx, CB_ADDR_V6_1, key.address.p1);
ctx_store_meta(ctx, CB_ADDR_V6_2, key.address.p2);
ctx_store_meta(ctx, CB_ADDR_V6_3, key.address.p3);
ctx_store_meta(ctx, CB_ADDR_V6_4, key.address.p4);
#endif /* DSR_ENCAP_MODE */
ep_tail_call(ctx, CILIUM_CALL_IPV6_NODEPORT_DSR);
} else {
ctx_store_meta(ctx, CB_NAT, NAT_DIR_EGRESS);
ep_tail_call(ctx, CILIUM_CALL_IPV6_NODEPORT_NAT);
}
return DROP_MISSED_TAIL_CALL;
}
ctx_set_xfer(ctx, XFER_PKT_NO_SVC);
return CTX_ACT_OK;
}
/* See comment in tail_rev_nodeport_lb4(). */
static __always_inline int rev_nodeport_lb6(struct __ctx_buff *ctx, int *ifindex)
{
int ret, ret2, l3_off = ETH_HLEN, l4_off, hdrlen;
struct ipv6_ct_tuple tuple = {};
void *data, *data_end;
struct ipv6hdr *ip6;
struct csum_offset csum_off = {};
struct ct_state ct_state = {};
struct bpf_fib_lookup fib_params = {};
union macaddr *dmac = NULL;
__u32 monitor = 0;
bool l2_hdr_required = true;
if (!revalidate_data(ctx, &data, &data_end, &ip6))
return DROP_INVALID;
tuple.nexthdr = ip6->nexthdr;
ipv6_addr_copy(&tuple.daddr, (union v6addr *) &ip6->daddr);
ipv6_addr_copy(&tuple.saddr, (union v6addr *) &ip6->saddr);
hdrlen = ipv6_hdrlen(ctx, l3_off, &tuple.nexthdr);
if (hdrlen < 0)
return hdrlen;
l4_off = l3_off + hdrlen;
csum_l4_offset_and_flags(tuple.nexthdr, &csum_off);
ret = ct_lookup6(get_ct_map6(&tuple), &tuple, ctx, l4_off, CT_INGRESS, &ct_state,
&monitor);
if (ret == CT_REPLY && ct_state.node_port == 1 && ct_state.rev_nat_index != 0) {
ret2 = lb6_rev_nat(ctx, l4_off, &csum_off, ct_state.rev_nat_index,
&tuple, REV_NAT_F_TUPLE_SADDR);
if (IS_ERR(ret2))
return ret2;
if (!revalidate_data(ctx, &data, &data_end, &ip6))
return DROP_INVALID;
bpf_mark_snat_done(ctx);
*ifindex = ct_state.ifindex;
#ifdef TUNNEL_MODE
{
union v6addr *dst = (union v6addr *)&ip6->daddr;
struct remote_endpoint_info *info;
info = ipcache_lookup6(&IPCACHE_MAP, dst, V6_CACHE_KEY_LEN);
if (info != NULL && info->tunnel_endpoint != 0) {
ret = __encap_with_nodeid(ctx, info->tunnel_endpoint,
SECLABEL, TRACE_PAYLOAD_LEN);
if (ret)
return ret;
*ifindex = ENCAP_IFINDEX;
/* fib lookup not necessary when going over tunnel. */
if (eth_store_daddr(ctx, fib_params.dmac, 0) < 0)
return DROP_WRITE_ERROR;
if (eth_store_saddr(ctx, fib_params.smac, 0) < 0)
return DROP_WRITE_ERROR;
return CTX_ACT_OK;
}
}
#endif
ret = maybe_add_l2_hdr(ctx, *ifindex, &l2_hdr_required);
if (ret != 0)
return ret;
if (!l2_hdr_required)
return CTX_ACT_OK;
else if (!revalidate_data_with_eth_hlen(ctx, &data, &data_end,
&ip6, __ETH_HLEN))
return DROP_INVALID;
if (fib_lookup_bypass())
dmac = map_lookup_elem(&NODEPORT_NEIGH6, &tuple.daddr);
if (dmac) {