-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
nodeport.h
1498 lines (1292 loc) · 39 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 "nat.h"
#include "lb.h"
#include "common.h"
#include "overloadable.h"
#include "conntrack.h"
#include "csum.h"
#include "encap.h"
#include "trace.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
#if defined(ENABLE_IPV4) && !defined(IPV4_NODEPORT)
#define IPV4_NODEPORT 0
#endif
#if defined(ENABLE_IPV6) && !defined(IPV6_NODEPORT_V)
DEFINE_IPV6(IPV6_NODEPORT, 0xbe, 0xef, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x1, 0x0, 0x1, 0x0, 0x0);
# define IPV6_NODEPORT_V
#endif
#endif /* ENABLE_NODEPORT */
#ifdef IPV6_NODEPORT_VAL
# define BPF_V6_NODEPORT(dst) \
({ \
union v6addr tmp = IPV6_NODEPORT_VAL; \
dst = tmp; \
})
#else
# define BPF_V6_NODEPORT(dst) BPF_V6(dst, IPV6_NODEPORT)
#endif
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_must_drop_remote(void)
{
#if defined(ENABLE_NODEPORT_ACCELERATION) && !defined(FROM_ENCAP_DEV) && \
__ctx_is == __ctx_skb
/* We already handled remote backends on XDP layer. Anything
* landing here that is remote is a bug.
*/
return true;
#else
return false;
#endif
}
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_ACCELERATION);
}
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_room_dsr_flags(void)
{
#ifdef BPF_HAVE_CSUM_LEVEL
return BPF_F_ADJ_ROOM_NO_CSUM_RESET;
#else
return 0;
#endif
}
#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_nat_ipv6_needed(struct __ctx_buff *ctx,
union v6addr *addr, int dir)
{
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 nodeport_nat_ipv4_needed(). */
if (dir == NAT_DIR_EGRESS)
return !ipv6_addrcmp((union v6addr *)&ip6->saddr, addr);
else
return !ipv6_addrcmp((union v6addr *)&ip6->daddr, 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 = 65535,
};
int ret;
ipv6_addr_copy(&target.addr, addr);
ret = nodeport_nat_ipv6_needed(ctx, addr, NAT_DIR_EGRESS) ?
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
static __always_inline int set_dsr_ext6(struct __ctx_buff *ctx,
struct ipv6hdr *ip6,
union v6addr *svc_addr, __be32 svc_port)
{
struct dsr_opt_v6 opt __align_stack_8 = {};
opt.nexthdr = ip6->nexthdr;
ip6->nexthdr = NEXTHDR_DEST;
ip6->payload_len = bpf_htons(bpf_ntohs(ip6->payload_len) + 24);
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_room(ctx, sizeof(opt), BPF_ADJ_ROOM_NET,
ctx_adjust_room_dsr_flags()))
return DROP_INVALID;
if (ctx_store_bytes(ctx, ETH_HLEN + sizeof(*ip6), &opt, sizeof(opt), 0) < 0)
return DROP_INVALID;
return 0;
}
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;
}
__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 = {};
__be32 dport;
int ret;
if (!revalidate_data(ctx, &data, &data_end, &ip6))
return DROP_INVALID;
dport = ctx_load_meta(ctx, CB_SVC_PORT);
addr.p1 = ctx_load_meta(ctx, CB_SVC_ADDR_V6_1);
addr.p2 = ctx_load_meta(ctx, CB_SVC_ADDR_V6_2);
addr.p3 = ctx_load_meta(ctx, CB_SVC_ADDR_V6_3);
addr.p4 = ctx_load_meta(ctx, CB_SVC_ADDR_V6_4);
ret = set_dsr_ext6(ctx, ip6, &addr, dport);
if (ret)
return ret;
if (!revalidate_data(ctx, &data, &data_end, &ip6))
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)
return DROP_WRITE_ERROR;
if (eth_store_saddr_aligned(ctx, mac.addr, 0) < 0)
return DROP_WRITE_ERROR;
} 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)
return DROP_NO_FIB;
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)
return DROP_WRITE_ERROR;
if (eth_store_saddr(ctx, fib_params.l.smac, 0) < 0)
return DROP_WRITE_ERROR;
}
return ctx_redirect(ctx, fib_params.l.ifindex, 0);
}
# 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;
target.addr = tmp;
#ifdef ENCAP_IFINDEX
if (dir == NAT_DIR_EGRESS) {
struct remote_endpoint_info *info;
union v6addr *dst;
if (!revalidate_data(ctx, &data, &data_end, &ip6))
return DROP_INVALID;
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)
return ret;
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)
return DROP_WRITE_ERROR;
if (eth_store_saddr(ctx, fib_params.l.smac, 0) < 0)
return DROP_WRITE_ERROR;
}
}
#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;
}
if (ret == NAT_PUNT_TO_STACK)
ret = CTX_ACT_OK;
else
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 ENCAP_IFINDEX
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;
}
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: __maybe_unused
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 = {};
struct ct_state ct_state = {};
union macaddr smac, *mac;
bool backend_local;
__u32 monitor = 0;
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_UNKNOWN_L4)
return CTX_ACT_OK;
else
return ret;
}
if ((svc = lb6_lookup_service(&key)) != NULL) {
ret = lb6_local(get_ct_map6(&tuple), ctx, l3_off, l4_off,
&csum_off, &key, &tuple, svc, &ct_state_new);
if (IS_ERR(ret))
return ret;
}
if (!svc || (!lb6_svc_is_nodeport(svc) &&
!lb6_svc_is_external_ip(svc) &&
!lb6_svc_is_hostport(svc))) {
if (svc)
return DROP_IS_CLUSTER_IP;
ctx_set_xfer(ctx, XFER_PKT_NO_SVC);
if (nodeport_uses_dsr6(&tuple)) {
return CTX_ACT_OK;
} else {
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;
}
}
ret = ct_lookup6(get_ct_map6(&tuple), &tuple, ctx, l4_off, CT_EGRESS,
&ct_state, &monitor);
if (ret < 0)
return ret;
if (!revalidate_data(ctx, &data, &data_end, &ip6))
return DROP_INVALID;
backend_local = lookup_ip6_endpoint(ip6);
if (!backend_local && (lb6_svc_is_hostport(svc) ||
nodeport_must_drop_remote()))
return DROP_INVALID;
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_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 (backend_local || (!backend_local && !nodeport_uses_dsr6(&tuple))) {
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) {
if (nodeport_uses_dsr6(&tuple)) {
ctx_store_meta(ctx, CB_SVC_PORT, key.dport);
ctx_store_meta(ctx, CB_SVC_ADDR_V6_1, key.address.p1);
ctx_store_meta(ctx, CB_SVC_ADDR_V6_2, key.address.p2);
ctx_store_meta(ctx, CB_SVC_ADDR_V6_3, key.address.p3);
ctx_store_meta(ctx, CB_SVC_ADDR_V6_4, key.address.p4);
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;
} else {
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;
__u32 monitor = 0;
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);
#ifdef ENCAP_IFINDEX
{
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
dmac = map_lookup_elem(&NODEPORT_NEIGH6, &tuple.daddr);
if (dmac) {
union macaddr mac = NATIVE_DEV_MAC_BY_IFINDEX(*ifindex);
if (eth_store_daddr_aligned(ctx, dmac->addr, 0) < 0)
return DROP_WRITE_ERROR;
if (eth_store_saddr_aligned(ctx, mac.addr, 0) < 0)
return DROP_WRITE_ERROR;
} else {
fib_params.family = AF_INET6;
fib_params.ifindex = *ifindex;
ipv6_addr_copy((union v6addr *) &fib_params.ipv6_src, &tuple.saddr);
ipv6_addr_copy((union v6addr *) &fib_params.ipv6_dst, &tuple.daddr);
ret = fib_lookup(ctx, &fib_params, sizeof(fib_params),
BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_OUTPUT);
if (ret != 0)
return DROP_NO_FIB;
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;
}
} else {
if (!bpf_skip_recirculation(ctx)) {
bpf_skip_nodeport_set(ctx);
ep_tail_call(ctx, CILIUM_CALL_IPV6_FROM_LXC);
return DROP_MISSED_TAIL_CALL;
}
}
return CTX_ACT_OK;
}
__section_tail(CILIUM_MAP_CALLS, CILIUM_CALL_IPV6_NODEPORT_REVNAT)
int tail_rev_nodeport_lb6(struct __ctx_buff *ctx)
{
int ifindex = 0;
int ret = 0;
ret = rev_nodeport_lb6(ctx, &ifindex);
if (IS_ERR(ret))
return send_drop_notify_error(ctx, 0, ret, CTX_ACT_DROP, METRIC_EGRESS);
return ctx_redirect(ctx, ifindex, 0);
}
#endif /* ENABLE_IPV6 */
#ifdef ENABLE_IPV4
static __always_inline bool nodeport_uses_dsr4(const struct ipv4_ct_tuple *tuple)
{
return nodeport_uses_dsr(tuple->nexthdr);
}
static __always_inline bool nodeport_nat_ipv4_needed(struct __ctx_buff *ctx,
__be32 addr, int dir,
bool *from_endpoint __maybe_unused,
const bool encap __maybe_unused)
{
struct endpoint_info *ep __maybe_unused;
void *data, *data_end;
struct iphdr *ip4;
if (!revalidate_data(ctx, &data, &data_end, &ip4))
return false;
/* Basic minimum is to only NAT when there is a potential of
* overlapping tuples, e.g. applications in hostns reusing
* source IPs we SNAT in node-port.
*/
if (dir == NAT_DIR_EGRESS) {
if (ip4->saddr == addr) {
return true;
}
#ifdef ENABLE_MASQUERADE /* SNAT local pod to world packets */
/* Do not MASQ when this function is executed from bpf_overlay
* (encap=true denotes this fact). Otherwise, a packet will be
* SNAT'd to cilium_host IP addr. */
if (encap)
return false;
#ifdef IPV4_SNAT_EXCLUSION_DST_CIDR
/* Do not MASQ if a dst IP belongs to a pods CIDR
* (native-routing-cidr if specified, otherwise local pod CIDR).
* The check is performed before we determine that a packet is
* sent from a local pod, as this check is cheaper than
* the map lookup done in the latter check. */
if (ipv4_is_in_subnet(ip4->daddr, IPV4_SNAT_EXCLUSION_DST_CIDR,
IPV4_SNAT_EXCLUSION_DST_CIDR_LEN))
return false;
#endif
/* Check whether packet is from a local endpoint */
if ((ep = __lookup_ip4_endpoint(ip4->saddr)) != NULL &&
!(ep->flags & ENDPOINT_F_HOST)) {
struct remote_endpoint_info *info;
*from_endpoint = true;
info = ipcache_lookup4(&IPCACHE_MAP, ip4->daddr,
V4_CACHE_KEY_LEN);
if (info != NULL) {
#ifdef ENABLE_IP_MASQ_AGENT
/* Do not SNAT if dst belongs to any
* ip-masq-agent subnet */
struct lpm_v4_key pfx;
pfx.lpm.prefixlen = 32;
memcpy(pfx.lpm.data, &ip4->daddr, sizeof(pfx.addr));
if (map_lookup_elem(&IP_MASQ_AGENT_IPV4, &pfx))
return false;
#endif
#ifndef ENCAP_IFINDEX
/* In the tunnel mode, a packet from a local ep
* to a remote node is not encap'd, and is sent
* via a native dev. Therefore, such packet has
* to be MASQ'd. Otherwise, it might be dropped
* either by underlying network (e.g. AWS drops
* packets by default from unknown subnets) or
* by the remote node if its native dev's
* rp_filter=1. */
if (info->sec_label == REMOTE_NODE_ID)
return false;
#endif
return true;
}
}
#endif /*ENABLE_MASQUERADE */
return false;
} else {
return ip4->daddr == addr;
}
}
static __always_inline int nodeport_nat_ipv4_fwd(struct __ctx_buff *ctx,
const __be32 addr,
const bool encap)
{
bool from_endpoint = false;
struct ipv4_nat_target target = {
.min_port = NODEPORT_PORT_MIN_NAT,
.max_port = 65535,
.addr = addr,
};
int ret = CTX_ACT_OK;
if (nodeport_nat_ipv4_needed(ctx, addr, NAT_DIR_EGRESS,
&from_endpoint, encap))
ret = snat_v4_process(ctx, NAT_DIR_EGRESS, &target,
from_endpoint);
if (ret == NAT_PUNT_TO_STACK)
ret = CTX_ACT_OK;
return ret;
}
# ifdef ENABLE_DSR
/* Helper function to set the IPv4 option for DSR when a backend is remote.
* NOTE: Revalidate data after calling the function.
*/
static __always_inline int set_dsr_opt4(struct __ctx_buff *ctx,
struct iphdr *ip4,
__be32 svc_addr, __be32 svc_port)
{
__u32 iph_old, iph_new, opt[2];
__be32 sum;
if (ip4->protocol == IPPROTO_TCP) {
union tcp_flags tcp_flags = { .value = 0 };
if (ctx_load_bytes(ctx, ETH_HLEN + sizeof(*ip4) + 12,
&tcp_flags, 2) < 0)
return DROP_CT_INVALID_HDR;
/* Setting the option is required only for the first packet
* (SYN), in the case of TCP, as for further packets of the
* same connection a remote node will use a NAT entry to
* reverse xlate a reply.
*/
if (!(tcp_flags.value & (TCP_FLAG_SYN)))
return 0;
}
iph_old = *(__u32 *)ip4;
ip4->ihl += 0x2; /* To accommodate u64 option. */
ip4->tot_len = bpf_htons(bpf_ntohs(ip4->tot_len) + 0x8);
iph_new = *(__u32 *)ip4;
opt[0] = bpf_htonl(DSR_IPV4_OPT_32 | svc_port);
opt[1] = bpf_htonl(svc_addr);
sum = csum_diff(&iph_old, 4, &iph_new, 4, 0);
sum = csum_diff(NULL, 0, &opt, sizeof(opt), sum);
if (ctx_adjust_room(ctx, 0x8, BPF_ADJ_ROOM_NET,
ctx_adjust_room_dsr_flags()))
return DROP_INVALID;
if (ctx_store_bytes(ctx, ETH_HLEN + sizeof(*ip4),
&opt, sizeof(opt), 0) < 0)
return DROP_INVALID;
if (l3_csum_replace(ctx, ETH_HLEN + offsetof(struct iphdr, check),
0, sum, 0) < 0)
return DROP_CSUM_L3;
return 0;
}
static __always_inline int handle_dsr_v4(struct __ctx_buff *ctx, bool *dsr)
{
void *data, *data_end;
struct iphdr *ip4;
if (!revalidate_data(ctx, &data, &data_end, &ip4))
return DROP_INVALID;
/* Check whether IPv4 header contains a 64-bit option (IPv4 header
* w/o option (5 x 32-bit words) + the DSR option (2 x 32-bit words)).
*/
if (ip4->ihl == 0x7) {
__u32 opt1 = 0, opt2 = 0;
__be32 address, dport;
if (ctx_load_bytes(ctx, ETH_HLEN + sizeof(struct iphdr),
&opt1, sizeof(opt1)) < 0)
return DROP_INVALID;
opt1 = bpf_ntohl(opt1);
if ((opt1 & DSR_IPV4_OPT_MASK) == DSR_IPV4_OPT_32) {
if (ctx_load_bytes(ctx, ETH_HLEN +
sizeof(struct iphdr) +
sizeof(opt1),
&opt2, sizeof(opt2)) < 0)
return DROP_INVALID;
opt2 = bpf_ntohl(opt2);
dport = opt1 & DSR_IPV4_DPORT_MASK;
address = opt2;
*dsr = true;
if (snat_v4_create_dsr(ctx, address, dport) < 0)
return DROP_INVALID;
}
}
return 0;
}
static __always_inline int xlate_dsr_v4(struct __ctx_buff *ctx,
const struct ipv4_ct_tuple *tuple,
int l4_off)
{
struct ipv4_ct_tuple nat_tup = *tuple;
struct ipv4_nat_entry *entry;
int ret = 0;
nat_tup.flags = NAT_DIR_EGRESS;
nat_tup.sport = tuple->dport;
nat_tup.dport = tuple->sport;
entry = snat_v4_lookup(&nat_tup);
if (entry)
ret = snat_v4_rewrite_egress(ctx, &nat_tup, entry, l4_off);
return ret;
}