forked from cilium/cilium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbpf_host.c
1459 lines (1281 loc) · 39 KB
/
bpf_host.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
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 */
#include <bpf/ctx/skb.h>
#include <bpf/api.h>
#include <node_config.h>
#include <ep_config.h>
#define IS_BPF_HOST 1
#define EVENT_SOURCE HOST_EP_ID
/* Host endpoint ID for the template bpf_host object file. Will be replaced
* at compile-time with the proper host endpoint ID.
*/
#define TEMPLATE_HOST_EP_ID 0xffff
/* These are configuration options which have a default value in their
* respective header files and must thus be defined beforehand:
*/
/* Pass unknown ICMPv6 NS to stack */
#define ACTION_UNKNOWN_ICMP6_NS CTX_ACT_OK
/* CB_PROXY_MAGIC overlaps with CB_ENCRYPT_MAGIC */
#define ENCRYPT_OR_PROXY_MAGIC 0
/* Controls the inclusion of the CILIUM_CALL_SEND_ICMP6_ECHO_REPLY section in
* the bpf_lxc object file.
*/
#define SKIP_ICMPV6_ECHO_HANDLING
#ifndef VLAN_FILTER
# define VLAN_FILTER(ifindex, vlan_id) return false;
#endif
#include "lib/common.h"
#include "lib/edt.h"
#include "lib/arp.h"
#include "lib/maps.h"
#include "lib/ipv6.h"
#include "lib/ipv4.h"
#include "lib/icmp6.h"
#include "lib/eth.h"
#include "lib/dbg.h"
#include "lib/proxy.h"
#include "lib/trace.h"
#include "lib/identity.h"
#include "lib/l3.h"
#include "lib/l4.h"
#include "lib/drop.h"
#include "lib/encap.h"
#include "lib/nat.h"
#include "lib/lb.h"
#include "lib/nodeport.h"
#include "lib/eps.h"
#include "lib/host_firewall.h"
#include "lib/overloadable.h"
#include "lib/encrypt.h"
static __always_inline bool allow_vlan(__u32 __maybe_unused ifindex, __u32 __maybe_unused vlan_id) {
VLAN_FILTER(ifindex, vlan_id);
}
#if defined(ENABLE_IPV4) || defined(ENABLE_IPV6)
static __always_inline int rewrite_dmac_to_host(struct __ctx_buff *ctx,
__u32 src_identity)
{
/* When attached to cilium_host, we rewrite the DMAC to the mac of
* cilium_host (peer) to ensure the packet is being considered to be
* addressed to the host (PACKET_HOST).
*/
union macaddr cilium_net_mac = CILIUM_NET_MAC;
/* Rewrite to destination MAC of cilium_net (remote peer) */
if (eth_store_daddr(ctx, (__u8 *) &cilium_net_mac.addr, 0) < 0)
return send_drop_notify_error(ctx, src_identity, DROP_WRITE_ERROR,
CTX_ACT_OK, METRIC_INGRESS);
return CTX_ACT_OK;
}
#define SECCTX_FROM_IPCACHE_OK 2
#ifndef SECCTX_FROM_IPCACHE
# define SECCTX_FROM_IPCACHE 0
#endif
static __always_inline bool identity_from_ipcache_ok(void)
{
return SECCTX_FROM_IPCACHE == SECCTX_FROM_IPCACHE_OK;
}
#endif
#ifdef ENABLE_IPV6
static __always_inline __u32
derive_src_id(const union v6addr *node_ip, struct ipv6hdr *ip6, __u32 *identity)
{
if (ipv6_match_prefix_64((union v6addr *) &ip6->saddr, node_ip)) {
/* Read initial 4 bytes of header and then extract flowlabel */
__u32 *tmp = (__u32 *) ip6;
*identity = bpf_ntohl(*tmp & IPV6_FLOWLABEL_MASK);
/* A remote node will map any HOST_ID source to be presented as
* REMOTE_NODE_ID, therefore any attempt to signal HOST_ID as
* source from a remote node can be dropped.
*/
if (*identity == HOST_ID)
return DROP_INVALID_IDENTITY;
}
return 0;
}
# ifdef ENABLE_HOST_FIREWALL
static __always_inline __u32
ipcache_lookup_srcid6(struct __ctx_buff *ctx)
{
struct remote_endpoint_info *info = NULL;
void *data, *data_end;
struct ipv6hdr *ip6;
__u32 srcid = 0;
if (!revalidate_data(ctx, &data, &data_end, &ip6))
return DROP_INVALID;
info = lookup_ip6_remote_endpoint((union v6addr *) &ip6->saddr);
if (info != NULL)
srcid = info->sec_label;
cilium_dbg(ctx, info ? DBG_IP_ID_MAP_SUCCEED6 : DBG_IP_ID_MAP_FAILED6,
ip6->saddr.s6_addr32[3], srcid);
return srcid;
}
# endif /* ENABLE_HOST_FIREWALL */
static __always_inline __u32
resolve_srcid_ipv6(struct __ctx_buff *ctx, __u32 srcid_from_proxy,
const bool from_host)
{
__u32 src_id = WORLD_ID, srcid_from_ipcache = srcid_from_proxy;
struct remote_endpoint_info *info = NULL;
void *data, *data_end;
struct ipv6hdr *ip6;
union v6addr *src;
int ret;
if (!revalidate_data_maybe_pull(ctx, &data, &data_end, &ip6, !from_host))
return DROP_INVALID;
if (!from_host) {
union v6addr node_ip = {};
BPF_V6(node_ip, ROUTER_IP);
ret = derive_src_id(&node_ip, ip6, &src_id);
if (IS_ERR(ret))
return ret;
}
/* Packets from the proxy will already have a real identity. */
if (identity_is_reserved(srcid_from_ipcache)) {
src = (union v6addr *) &ip6->saddr;
info = lookup_ip6_remote_endpoint(src);
if (info != NULL && info->sec_label)
srcid_from_ipcache = info->sec_label;
cilium_dbg(ctx, info ? DBG_IP_ID_MAP_SUCCEED6 : DBG_IP_ID_MAP_FAILED6,
((__u32 *) src)[3], srcid_from_ipcache);
}
if (from_host)
src_id = srcid_from_ipcache;
else if (src_id == WORLD_ID &&
identity_from_ipcache_ok() &&
!identity_is_reserved(srcid_from_ipcache))
src_id = srcid_from_ipcache;
return src_id;
}
static __always_inline int
handle_ipv6(struct __ctx_buff *ctx, __u32 secctx, const bool from_host)
{
struct trace_ctx __maybe_unused trace = {
.reason = TRACE_REASON_UNKNOWN,
.monitor = TRACE_PAYLOAD_LEN,
};
struct remote_endpoint_info *info = NULL;
void *data, *data_end;
struct ipv6hdr *ip6;
union v6addr *dst;
__u32 __maybe_unused remote_id = WORLD_ID;
int ret, l3_off = ETH_HLEN, hdrlen;
bool skip_redirect = false;
struct endpoint_info *ep;
__u8 nexthdr;
if (!revalidate_data(ctx, &data, &data_end, &ip6))
return DROP_INVALID;
nexthdr = ip6->nexthdr;
hdrlen = ipv6_hdrlen(ctx, &nexthdr);
if (hdrlen < 0)
return hdrlen;
if (likely(nexthdr == IPPROTO_ICMPV6)) {
ret = icmp6_host_handle(ctx);
if (ret == SKIP_HOST_FIREWALL)
goto skip_host_firewall;
if (IS_ERR(ret))
return ret;
}
#ifdef ENABLE_NODEPORT
if (!from_host) {
if (ctx_get_xfer(ctx) != XFER_PKT_NO_SVC &&
!bpf_skip_nodeport(ctx)) {
ret = nodeport_lb6(ctx, secctx);
/* nodeport_lb6() returns with TC_ACT_REDIRECT for
* traffic to L7 LB. Policy enforcement needs to take
* place after L7 LB has processed the packet, so we
* return to stack immediately here with
* TC_ACT_REDIRECT.
*/
if (ret < 0 || ret == TC_ACT_REDIRECT)
return ret;
}
/* Verifier workaround: modified ctx access. */
if (!revalidate_data(ctx, &data, &data_end, &ip6))
return DROP_INVALID;
}
#endif /* ENABLE_NODEPORT */
#if defined(NO_REDIRECT) && !defined(ENABLE_HOST_ROUTING)
/* See IPv4 case for NO_REDIRECT/ENABLE_HOST_ROUTING comments */
if (!from_host)
skip_redirect = true;
#endif /* NO_REDIRECT && !ENABLE_HOST_ROUTING */
#ifdef ENABLE_HOST_FIREWALL
if (from_host) {
ret = ipv6_host_policy_egress(ctx, secctx, &trace);
if (IS_ERR(ret))
return ret;
} else if (!ctx_skip_host_fw(ctx)) {
ret = ipv6_host_policy_ingress(ctx, &remote_id, &trace);
if (IS_ERR(ret))
return ret;
}
#endif /* ENABLE_HOST_FIREWALL */
if (skip_redirect)
return CTX_ACT_OK;
skip_host_firewall:
if (from_host) {
/* If we are attached to cilium_host at egress, this will
* rewrite the destination MAC address to the MAC of cilium_net.
*/
ret = rewrite_dmac_to_host(ctx, secctx);
/* DIRECT PACKET READ INVALID */
if (IS_ERR(ret))
return ret;
if (!revalidate_data(ctx, &data, &data_end, &ip6))
return DROP_INVALID;
}
/* Lookup IPv6 address in list of local endpoints */
ep = lookup_ip6_endpoint(ip6);
if (ep) {
/* Let through packets to the node-ip so they are
* processed by the local ip stack.
*/
if (ep->flags & ENDPOINT_F_HOST)
return CTX_ACT_OK;
return ipv6_local_delivery(ctx, l3_off, secctx, ep,
METRIC_INGRESS, from_host);
}
/* Below remainder is only relevant when traffic is pushed via cilium_host.
* For traffic coming from external, we're done here.
*/
if (!from_host)
return CTX_ACT_OK;
#ifdef TUNNEL_MODE
dst = (union v6addr *) &ip6->daddr;
info = ipcache_lookup6(&IPCACHE_MAP, dst, V6_CACHE_KEY_LEN);
if (info != NULL && info->tunnel_endpoint != 0) {
ret = encap_and_redirect_with_nodeid(ctx, info->tunnel_endpoint,
info->key, secctx, &trace);
/* If IPSEC is needed recirc through ingress to use xfrm stack
* and then result will routed back through bpf_netdev on egress
* but with encrypt marks.
*/
if (ret == IPSEC_ENDPOINT)
return CTX_ACT_OK;
else
return ret;
} else {
struct endpoint_key key = {};
/* IPv6 lookup key: daddr/96 */
dst = (union v6addr *) &ip6->daddr;
key.ip6.p1 = dst->p1;
key.ip6.p2 = dst->p2;
key.ip6.p3 = dst->p3;
key.ip6.p4 = 0;
key.family = ENDPOINT_KEY_IPV6;
ret = encap_and_redirect_netdev(ctx, &key, secctx, &trace);
if (ret == IPSEC_ENDPOINT)
return CTX_ACT_OK;
else if (ret != DROP_NO_TUNNEL_ENDPOINT)
return ret;
}
#endif
dst = (union v6addr *) &ip6->daddr;
info = ipcache_lookup6(&IPCACHE_MAP, dst, V6_CACHE_KEY_LEN);
if (info == NULL || info->sec_label == WORLD_ID) {
/* See IPv4 comment. */
return DROP_UNROUTABLE;
}
#ifdef ENABLE_IPSEC
if (info && info->key && info->tunnel_endpoint) {
__u8 key = get_min_encrypt_key(info->key);
set_encrypt_key_meta(ctx, key);
#ifdef IP_POOLS
set_encrypt_dip(ctx, info->tunnel_endpoint);
#else
set_identity_meta(ctx, secctx);
#endif
}
#endif
return CTX_ACT_OK;
}
static __always_inline int
tail_handle_ipv6(struct __ctx_buff *ctx, const bool from_host)
{
__u32 proxy_identity = ctx_load_meta(ctx, CB_SRC_IDENTITY);
int ret;
ctx_store_meta(ctx, CB_SRC_IDENTITY, 0);
ret = handle_ipv6(ctx, proxy_identity, from_host);
if (IS_ERR(ret))
return send_drop_notify_error(ctx, proxy_identity, ret,
CTX_ACT_DROP, METRIC_INGRESS);
return ret;
}
__section_tail(CILIUM_MAP_CALLS, CILIUM_CALL_IPV6_FROM_HOST)
int tail_handle_ipv6_from_host(struct __ctx_buff *ctx __maybe_unused)
{
return tail_handle_ipv6(ctx, true);
}
__section_tail(CILIUM_MAP_CALLS, CILIUM_CALL_IPV6_FROM_NETDEV)
int tail_handle_ipv6_from_netdev(struct __ctx_buff *ctx)
{
return tail_handle_ipv6(ctx, false);
}
# ifdef ENABLE_HOST_FIREWALL
static __always_inline int
handle_to_netdev_ipv6(struct __ctx_buff *ctx, struct trace_ctx *trace)
{
void *data, *data_end;
struct ipv6hdr *ip6;
int hdrlen, ret;
__u32 src_id = 0;
__u8 nexthdr;
if (!revalidate_data_pull(ctx, &data, &data_end, &ip6))
return DROP_INVALID;
nexthdr = ip6->nexthdr;
hdrlen = ipv6_hdrlen(ctx, &nexthdr);
if (hdrlen < 0)
return hdrlen;
if (likely(nexthdr == IPPROTO_ICMPV6)) {
ret = icmp6_host_handle(ctx);
if (ret == SKIP_HOST_FIREWALL)
return CTX_ACT_OK;
if (IS_ERR(ret))
return ret;
}
/* to-netdev is attached to the egress path of the native device. */
src_id = ipcache_lookup_srcid6(ctx);
return ipv6_host_policy_egress(ctx, src_id, trace);
}
#endif /* ENABLE_HOST_FIREWALL */
#endif /* ENABLE_IPV6 */
#ifdef ENABLE_IPV4
static __always_inline __u32
resolve_srcid_ipv4(struct __ctx_buff *ctx, __u32 srcid_from_proxy,
__u32 *sec_label, const bool from_host)
{
__u32 src_id = WORLD_ID, srcid_from_ipcache = srcid_from_proxy;
struct remote_endpoint_info *info = NULL;
void *data, *data_end;
struct iphdr *ip4;
/* This is the first time revalidate_data() is going to be called in
* the "to-netdev" path. Make sure that we don't legitimately drop
* the packet if the skb arrived with the header not being not in the
* linear data.
*/
if (!revalidate_data_maybe_pull(ctx, &data, &data_end, &ip4, !from_host))
return DROP_INVALID;
/* Packets from the proxy will already have a real identity. */
if (identity_is_reserved(srcid_from_ipcache)) {
info = lookup_ip4_remote_endpoint(ip4->saddr);
if (info != NULL) {
*sec_label = info->sec_label;
if (*sec_label) {
/* When SNAT is enabled on traffic ingressing
* into Cilium, all traffic from the world will
* have a source IP of the host. It will only
* actually be from the host if "srcid_from_proxy"
* (passed into this function) reports the src as
* the host. So we can ignore the ipcache if it
* reports the source as HOST_ID.
*/
#ifndef ENABLE_EXTRA_HOST_DEV
if (*sec_label != HOST_ID)
srcid_from_ipcache = *sec_label;
#else
if ((*sec_label != HOST_ID &&
!from_host) || from_host)
srcid_from_ipcache = *sec_label;
#endif /* ENABLE_EXTRA_HOST_DEV */
}
}
cilium_dbg(ctx, info ? DBG_IP_ID_MAP_SUCCEED4 : DBG_IP_ID_MAP_FAILED4,
ip4->saddr, srcid_from_ipcache);
}
if (from_host)
src_id = srcid_from_ipcache;
/* If we could not derive the secctx from the packet itself but
* from the ipcache instead, then use the ipcache identity.
*/
else if (identity_from_ipcache_ok() &&
!identity_is_reserved(srcid_from_ipcache))
src_id = srcid_from_ipcache;
return src_id;
}
static __always_inline int
handle_ipv4(struct __ctx_buff *ctx, __u32 secctx,
__u32 ipcache_srcid __maybe_unused, const bool from_host)
{
struct trace_ctx __maybe_unused trace = {
.reason = TRACE_REASON_UNKNOWN,
.monitor = TRACE_PAYLOAD_LEN,
};
struct remote_endpoint_info *info = NULL;
__u32 __maybe_unused remote_id = 0;
struct ipv4_ct_tuple tuple = {};
bool skip_redirect = false;
struct endpoint_info *ep;
void *data, *data_end;
struct iphdr *ip4;
int ret;
if (!revalidate_data(ctx, &data, &data_end, &ip4))
return DROP_INVALID;
/* If IPv4 fragmentation is disabled
* AND a IPv4 fragmented packet is received,
* then drop the packet.
*/
#ifndef ENABLE_IPV4_FRAGMENTS
if (ipv4_is_fragment(ip4))
return DROP_FRAG_NOSUPPORT;
#endif
#ifdef ENABLE_NODEPORT
if (!from_host) {
if (ctx_get_xfer(ctx) != XFER_PKT_NO_SVC &&
!bpf_skip_nodeport(ctx)) {
ret = nodeport_lb4(ctx, secctx);
if (ret == NAT_46X64_RECIRC) {
ctx_store_meta(ctx, CB_SRC_IDENTITY, secctx);
ep_tail_call(ctx, CILIUM_CALL_IPV6_FROM_NETDEV);
return send_drop_notify_error(ctx, secctx,
DROP_MISSED_TAIL_CALL,
CTX_ACT_DROP,
METRIC_INGRESS);
}
/* nodeport_lb4() returns with TC_ACT_REDIRECT for
* traffic to L7 LB. Policy enforcement needs to take
* place after L7 LB has processed the packet, so we
* return to stack immediately here with
* TC_ACT_REDIRECT.
*/
if (ret < 0 || ret == TC_ACT_REDIRECT)
return ret;
}
/* Verifier workaround: modified ctx access. */
if (!revalidate_data(ctx, &data, &data_end, &ip4))
return DROP_INVALID;
}
#endif /* ENABLE_NODEPORT */
#if defined(NO_REDIRECT) && !defined(ENABLE_HOST_ROUTING)
/* Without bpf_redirect_neigh() helper, we cannot redirect a
* packet to a local endpoint in the direct routing mode, as
* the redirect bypasses nf_conntrack table. This makes a
* second reply from the endpoint to be MASQUERADEd or to be
* DROP-ed by k8s's "--ctstate INVALID -j DROP" depending via
* which interface it was inputed. With bpf_redirect_neigh()
* we bypass request and reply path in the host namespace and
* do not run into this issue.
*/
if (!from_host)
skip_redirect = true;
#endif /* NO_REDIRECT && !ENABLE_HOST_ROUTING */
#ifdef ENABLE_HOST_FIREWALL
if (from_host) {
/* We're on the egress path of cilium_host. */
ret = ipv4_host_policy_egress(ctx, secctx, ipcache_srcid,
&trace);
if (IS_ERR(ret))
return ret;
} else if (!ctx_skip_host_fw(ctx)) {
/* We're on the ingress path of the native device. */
ret = ipv4_host_policy_ingress(ctx, &remote_id, &trace);
if (IS_ERR(ret))
return ret;
}
#endif /* ENABLE_HOST_FIREWALL */
if (skip_redirect)
return CTX_ACT_OK;
tuple.nexthdr = ip4->protocol;
if (from_host) {
/* If we are attached to cilium_host at egress, this will
* rewrite the destination MAC address to the MAC of cilium_net.
*/
ret = rewrite_dmac_to_host(ctx, secctx);
/* DIRECT PACKET READ INVALID */
if (IS_ERR(ret))
return ret;
if (!revalidate_data(ctx, &data, &data_end, &ip4))
return DROP_INVALID;
}
/* Lookup IPv4 address in list of local endpoints and host IPs */
ep = lookup_ip4_endpoint(ip4);
if (ep) {
/* Let through packets to the node-ip so they are processed by
* the local ip stack.
*/
if (ep->flags & ENDPOINT_F_HOST)
return CTX_ACT_OK;
return ipv4_local_delivery(ctx, ETH_HLEN, secctx, ip4, ep,
METRIC_INGRESS, from_host);
}
/* Below remainder is only relevant when traffic is pushed via cilium_host.
* For traffic coming from external, we're done here.
*/
if (!from_host)
return CTX_ACT_OK;
/* Handle VTEP integration in bpf_host to support pod L7 PROXY.
* It requires route setup to VTEP CIDR via dev cilium_host scope link.
*/
#ifdef ENABLE_VTEP
{
struct vtep_key vkey = {};
struct vtep_value *vtep;
vkey.vtep_ip = ip4->daddr & VTEP_MASK;
vtep = map_lookup_elem(&VTEP_MAP, &vkey);
if (!vtep)
goto skip_vtep;
if (vtep->vtep_mac && vtep->tunnel_endpoint) {
if (eth_store_daddr(ctx, (__u8 *)&vtep->vtep_mac, 0) < 0)
return DROP_WRITE_ERROR;
return __encap_and_redirect_with_nodeid(ctx, vtep->tunnel_endpoint,
WORLD_ID, &trace);
}
}
skip_vtep:
#endif
#ifdef TUNNEL_MODE
info = ipcache_lookup4(&IPCACHE_MAP, ip4->daddr, V4_CACHE_KEY_LEN);
if (info != NULL && info->tunnel_endpoint != 0) {
ret = encap_and_redirect_with_nodeid(ctx, info->tunnel_endpoint,
info->key, secctx, &trace);
if (ret == IPSEC_ENDPOINT)
return CTX_ACT_OK;
else
return ret;
} else {
/* IPv4 lookup key: daddr & IPV4_MASK */
struct endpoint_key key = {};
key.ip4 = ip4->daddr & IPV4_MASK;
key.family = ENDPOINT_KEY_IPV4;
cilium_dbg(ctx, DBG_NETDEV_ENCAP4, key.ip4, secctx);
ret = encap_and_redirect_netdev(ctx, &key, secctx, &trace);
if (ret == IPSEC_ENDPOINT)
return CTX_ACT_OK;
else if (ret != DROP_NO_TUNNEL_ENDPOINT)
return ret;
}
#endif
info = ipcache_lookup4(&IPCACHE_MAP, ip4->daddr, V4_CACHE_KEY_LEN);
if (info == NULL || info->sec_label == WORLD_ID) {
/* We have received a packet for which no ipcache entry exists,
* we do not know what to do with this packet, drop it.
*
* The info == NULL test is soley to satisfy verifier requirements
* as in Cilium case we'll always hit the 0.0.0.0/32 catch-all
* entry. Therefore we need to test for WORLD_ID. It is clearly
* wrong to route a ctx to cilium_host for which we don't know
* anything about it as otherwise we'll run into a routing loop.
*/
return DROP_UNROUTABLE;
}
#ifdef ENABLE_IPSEC
if (info && info->key && info->tunnel_endpoint) {
__u8 key = get_min_encrypt_key(info->key);
set_encrypt_key_meta(ctx, key);
#ifdef IP_POOLS
set_encrypt_dip(ctx, info->tunnel_endpoint);
#else
set_identity_meta(ctx, secctx);
#endif
}
#endif
return CTX_ACT_OK;
}
static __always_inline int
tail_handle_ipv4(struct __ctx_buff *ctx, __u32 ipcache_srcid, const bool from_host)
{
__u32 proxy_identity = ctx_load_meta(ctx, CB_SRC_IDENTITY);
int ret;
ctx_store_meta(ctx, CB_SRC_IDENTITY, 0);
ret = handle_ipv4(ctx, proxy_identity, ipcache_srcid, from_host);
if (IS_ERR(ret))
return send_drop_notify_error(ctx, proxy_identity,
ret, CTX_ACT_DROP, METRIC_INGRESS);
return ret;
}
__section_tail(CILIUM_MAP_CALLS, CILIUM_CALL_IPV4_FROM_HOST)
int tail_handle_ipv4_from_host(struct __ctx_buff *ctx)
{
__u32 ipcache_srcid = 0;
#if defined(ENABLE_HOST_FIREWALL) && !defined(ENABLE_MASQUERADE)
ipcache_srcid = ctx_load_meta(ctx, CB_IPCACHE_SRC_LABEL);
ctx_store_meta(ctx, CB_IPCACHE_SRC_LABEL, 0);
#endif
return tail_handle_ipv4(ctx, ipcache_srcid, true);
}
__section_tail(CILIUM_MAP_CALLS, CILIUM_CALL_IPV4_FROM_NETDEV)
int tail_handle_ipv4_from_netdev(struct __ctx_buff *ctx)
{
return tail_handle_ipv4(ctx, 0, false);
}
#ifdef ENABLE_HOST_FIREWALL
static __always_inline int
handle_to_netdev_ipv4(struct __ctx_buff *ctx, struct trace_ctx *trace)
{
void *data, *data_end;
struct iphdr *ip4;
__u32 src_id = 0, ipcache_srcid = 0;
if ((ctx->mark & MARK_MAGIC_HOST_MASK) == MARK_MAGIC_HOST)
src_id = HOST_ID;
src_id = resolve_srcid_ipv4(ctx, src_id, &ipcache_srcid, true);
if (!revalidate_data(ctx, &data, &data_end, &ip4))
return DROP_INVALID;
/* We need to pass the srcid from ipcache to host firewall. See
* comment in ipv4_host_policy_egress() for details.
*/
return ipv4_host_policy_egress(ctx, src_id, ipcache_srcid, trace);
}
#endif /* ENABLE_HOST_FIREWALL */
#endif /* ENABLE_IPV4 */
#ifdef ENABLE_IPSEC
#ifndef TUNNEL_MODE
static __always_inline int
do_netdev_encrypt_pools(struct __ctx_buff *ctx __maybe_unused)
{
int ret = 0;
#ifdef IP_POOLS
__u32 tunnel_endpoint = 0;
void *data, *data_end;
__u32 tunnel_source = IPV4_ENCRYPT_IFACE;
struct iphdr *iphdr;
__be32 sum;
tunnel_endpoint = ctx_load_meta(ctx, CB_ENCRYPT_DST);
ctx->mark = 0;
if (!revalidate_data(ctx, &data, &data_end, &iphdr)) {
ret = DROP_INVALID;
goto drop_err;
}
/* When IP_POOLS is enabled ip addresses are not
* assigned on a per node basis so lacking node
* affinity we can not use IP address to assign the
* destination IP. Instead rewrite it here from cb[].
*/
sum = csum_diff(&iphdr->daddr, 4, &tunnel_endpoint, 4, 0);
if (ctx_store_bytes(ctx, ETH_HLEN + offsetof(struct iphdr, daddr),
&tunnel_endpoint, 4, 0) < 0) {
ret = DROP_WRITE_ERROR;
goto drop_err;
}
if (l3_csum_replace(ctx, ETH_HLEN + offsetof(struct iphdr, check),
0, sum, 0) < 0) {
ret = DROP_CSUM_L3;
goto drop_err;
}
if (!revalidate_data(ctx, &data, &data_end, &iphdr)) {
ret = DROP_INVALID;
goto drop_err;
}
sum = csum_diff(&iphdr->saddr, 4, &tunnel_source, 4, 0);
if (ctx_store_bytes(ctx, ETH_HLEN + offsetof(struct iphdr, saddr),
&tunnel_source, 4, 0) < 0) {
ret = DROP_WRITE_ERROR;
goto drop_err;
}
if (l3_csum_replace(ctx, ETH_HLEN + offsetof(struct iphdr, check),
0, sum, 0) < 0) {
ret = DROP_CSUM_L3;
goto drop_err;
}
drop_err:
#endif /* IP_POOLS */
return ret;
}
static __always_inline int
do_netdev_encrypt_fib(struct __ctx_buff *ctx __maybe_unused,
__u16 proto __maybe_unused,
int *encrypt_iface __maybe_unused)
{
int ret = 0;
/* Only do FIB lookup if both the BPF helper is supported and we know
* the egress ineterface. If we don't have an egress interface,
* typically in an environment with many egress devs than we have
* to let the stack decide how to egress the packet. EKS is the
* example of an environment with multiple egress interfaces.
*/
#if defined(BPF_HAVE_FIB_LOOKUP) && defined(ENCRYPT_IFACE)
struct bpf_fib_lookup fib_params = {};
void *data, *data_end;
int err;
if (proto == bpf_htons(ETH_P_IP)) {
struct iphdr *ip4;
if (!revalidate_data(ctx, &data, &data_end, &ip4)) {
ret = DROP_INVALID;
goto drop_err_fib;
}
fib_params.family = AF_INET;
fib_params.ipv4_src = ip4->saddr;
fib_params.ipv4_dst = ip4->daddr;
} else {
struct ipv6hdr *ip6;
if (!revalidate_data(ctx, &data, &data_end, &ip6)) {
ret = DROP_INVALID;
goto drop_err_fib;
}
fib_params.family = AF_INET6;
ipv6_addr_copy((union v6addr *) &fib_params.ipv6_src, (union v6addr *) &ip6->saddr);
ipv6_addr_copy((union v6addr *) &fib_params.ipv6_dst, (union v6addr *) &ip6->daddr);
}
fib_params.ifindex = *encrypt_iface;
err = fib_lookup(ctx, &fib_params, sizeof(fib_params),
BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_OUTPUT);
if (err != 0) {
ret = DROP_NO_FIB;
goto drop_err_fib;
}
if (eth_store_daddr(ctx, fib_params.dmac, 0) < 0) {
ret = DROP_WRITE_ERROR;
goto drop_err_fib;
}
if (eth_store_saddr(ctx, fib_params.smac, 0) < 0) {
ret = DROP_WRITE_ERROR;
goto drop_err_fib;
}
*encrypt_iface = fib_params.ifindex;
drop_err_fib:
#endif /* BPF_HAVE_FIB_LOOKUP */
return ret;
}
static __always_inline int do_netdev_encrypt(struct __ctx_buff *ctx, __u16 proto,
__u32 src_id)
{
int encrypt_iface = 0;
int ret = 0;
#if defined(ENCRYPT_IFACE) && defined(BPF_HAVE_FIB_LOOKUP)
encrypt_iface = ENCRYPT_IFACE;
#endif
ret = do_netdev_encrypt_pools(ctx);
if (ret)
return send_drop_notify_error(ctx, src_id, ret, CTX_ACT_DROP, METRIC_INGRESS);
ret = do_netdev_encrypt_fib(ctx, proto, &encrypt_iface);
if (ret)
return send_drop_notify_error(ctx, src_id, ret, CTX_ACT_DROP, METRIC_INGRESS);
bpf_clear_meta(ctx);
#ifdef BPF_HAVE_FIB_LOOKUP
/* Redirect only works if we have a fib lookup to set the MAC
* addresses. Otherwise let the stack do the routing and fib
* Note, without FIB lookup implemented the packet may have
* incorrect dmac leaving bpf_host so will need to mark as
* PACKET_HOST or otherwise fixup MAC addresses.
*/
if (encrypt_iface)
return ctx_redirect(ctx, encrypt_iface, 0);
#endif
return CTX_ACT_OK;
}
#else /* TUNNEL_MODE */
static __always_inline int do_netdev_encrypt_encap(struct __ctx_buff *ctx, __u32 src_id)
{
struct trace_ctx trace = {
.reason = TRACE_REASON_ENCRYPTED,
.monitor = TRACE_PAYLOAD_LEN,
};
__u32 tunnel_endpoint = 0;
tunnel_endpoint = ctx_load_meta(ctx, CB_ENCRYPT_DST);
ctx->mark = 0;
bpf_clear_meta(ctx);
return __encap_and_redirect_with_nodeid(ctx, tunnel_endpoint, src_id,
&trace);
}
static __always_inline int do_netdev_encrypt(struct __ctx_buff *ctx, __u16 proto __maybe_unused,
__u32 src_id)
{
return do_netdev_encrypt_encap(ctx, src_id);
}
#endif /* TUNNEL_MODE */
#endif /* ENABLE_IPSEC */
static __always_inline int
do_netdev(struct __ctx_buff *ctx, __u16 proto, const bool from_host)
{
__u32 __maybe_unused identity = 0;
__u32 __maybe_unused ipcache_srcid = 0;
int ret;
#if defined(ENABLE_L7_LB)
if (from_host) {
__u32 magic = ctx->mark & MARK_MAGIC_HOST_MASK;
if (magic == MARK_MAGIC_PROXY_EGRESS_EPID) {
__u32 lxc_id = get_epid(ctx);
ctx->mark = 0;
tail_call_dynamic(ctx, &POLICY_EGRESSCALL_MAP, lxc_id);
return DROP_MISSED_TAIL_CALL;
}
}
#endif
#ifdef ENABLE_IPSEC
if (!from_host && !do_decrypt(ctx, proto))
return CTX_ACT_OK;
#endif
if (from_host) {
__u32 magic;
enum trace_point trace = TRACE_FROM_HOST;
magic = inherit_identity_from_host(ctx, &identity);
if (magic == MARK_MAGIC_PROXY_INGRESS || magic == MARK_MAGIC_PROXY_EGRESS)
trace = TRACE_FROM_PROXY;
#ifdef ENABLE_IPSEC
if (magic == MARK_MAGIC_ENCRYPT) {
send_trace_notify(ctx, TRACE_FROM_STACK, identity, 0, 0,
ctx->ingress_ifindex, TRACE_REASON_ENCRYPTED,
TRACE_PAYLOAD_LEN);
return do_netdev_encrypt(ctx, proto, identity);
}
#endif
send_trace_notify(ctx, trace, identity, 0, 0,
ctx->ingress_ifindex,
TRACE_REASON_UNKNOWN, TRACE_PAYLOAD_LEN);
} else {
bpf_skip_nodeport_clear(ctx);
send_trace_notify(ctx, TRACE_FROM_NETWORK, 0, 0, 0,
ctx->ingress_ifindex,
TRACE_REASON_UNKNOWN, TRACE_PAYLOAD_LEN);
}
bpf_clear_meta(ctx);
switch (proto) {
# if defined ENABLE_ARP_PASSTHROUGH || defined ENABLE_ARP_RESPONDER
case bpf_htons(ETH_P_ARP):
ret = CTX_ACT_OK;
break;
# endif
#ifdef ENABLE_IPV6
case bpf_htons(ETH_P_IPV6):
identity = resolve_srcid_ipv6(ctx, identity, from_host);
ctx_store_meta(ctx, CB_SRC_IDENTITY, identity);
if (from_host)
ep_tail_call(ctx, CILIUM_CALL_IPV6_FROM_HOST);
else
ep_tail_call(ctx, CILIUM_CALL_IPV6_FROM_NETDEV);
/* See comment below for IPv4. */
return send_drop_notify_error(ctx, identity, DROP_MISSED_TAIL_CALL,
CTX_ACT_OK, METRIC_INGRESS);
#endif
#ifdef ENABLE_IPV4
case bpf_htons(ETH_P_IP):
identity = resolve_srcid_ipv4(ctx, identity, &ipcache_srcid,
from_host);
ctx_store_meta(ctx, CB_SRC_IDENTITY, identity);
if (from_host) {
# if defined(ENABLE_HOST_FIREWALL) && !defined(ENABLE_MASQUERADE)
/* If we don't rely on BPF-based masquerading, we need
* to pass the srcid from ipcache to host firewall. See
* comment in ipv4_host_policy_egress() for details.
*/
ctx_store_meta(ctx, CB_IPCACHE_SRC_LABEL, ipcache_srcid);
# endif
ep_tail_call(ctx, CILIUM_CALL_IPV4_FROM_HOST);
} else {
ep_tail_call(ctx, CILIUM_CALL_IPV4_FROM_NETDEV);
}
/* We are not returning an error here to always allow traffic to
* the stack in case maps have become unavailable.
*
* Note: Since drop notification requires a tail call as well,
* this notification is unlikely to succeed.
*/
return send_drop_notify_error(ctx, identity, DROP_MISSED_TAIL_CALL,
CTX_ACT_OK, METRIC_INGRESS);
#endif
default:
#ifdef ENABLE_HOST_FIREWALL
ret = send_drop_notify_error(ctx, identity, DROP_UNKNOWN_L3,
CTX_ACT_DROP, METRIC_INGRESS);
#else
/* Pass unknown traffic to the stack */
ret = CTX_ACT_OK;