forked from cilium/cilium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbpf_xdp.c
365 lines (303 loc) · 8.77 KB
/
bpf_xdp.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
// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
/* Copyright Authors of Cilium */
#include <bpf/ctx/xdp.h>
#include <bpf/api.h>
#include <node_config.h>
#include <netdev_config.h>
#include <filter_config.h>
#define SKIP_POLICY_MAP 1
/* Controls the inclusion of the CILIUM_CALL_HANDLE_ICMP6_NS section in the
* bpf_lxc object file.
*/
#define SKIP_ICMPV6_NS_HANDLING
/* Controls the inclusion of the CILIUM_CALL_SEND_ICMP6_TIME_EXCEEDED section
* in the bpf_lxc object file. This is needed for all callers of
* ipv6_local_delivery, which calls into the IPv6 L3 handling.
*/
#define SKIP_ICMPV6_HOPLIMIT_HANDLING
/* Controls the inclusion of the CILIUM_CALL_SRV6 section in the object file.
*/
#define SKIP_SRV6_HANDLING
/* The XDP datapath does not take care of health probes from the local node,
* thus do not compile it in.
*/
#undef ENABLE_HEALTH_CHECK
#include "lib/common.h"
#include "lib/maps.h"
#include "lib/eps.h"
#include "lib/events.h"
#include "lib/nodeport.h"
#ifdef ENABLE_PREFILTER
#ifdef CIDR4_FILTER
struct {
__uint(type, BPF_MAP_TYPE_HASH);
__type(key, struct lpm_v4_key);
__type(value, struct lpm_val);
__uint(pinning, LIBBPF_PIN_BY_NAME);
__uint(max_entries, CIDR4_HMAP_ELEMS);
__uint(map_flags, BPF_F_NO_PREALLOC);
} CIDR4_HMAP_NAME __section_maps_btf;
#ifdef CIDR4_LPM_PREFILTER
struct {
__uint(type, BPF_MAP_TYPE_LPM_TRIE);
__type(key, struct lpm_v4_key);
__type(value, struct lpm_val);
__uint(pinning, LIBBPF_PIN_BY_NAME);
__uint(max_entries, CIDR4_LMAP_ELEMS);
__uint(map_flags, BPF_F_NO_PREALLOC);
} CIDR4_LMAP_NAME __section_maps_btf;
#endif /* CIDR4_LPM_PREFILTER */
#endif /* CIDR4_FILTER */
#ifdef CIDR6_FILTER
struct {
__uint(type, BPF_MAP_TYPE_HASH);
__type(key, struct lpm_v6_key);
__type(value, struct lpm_val);
__uint(pinning, LIBBPF_PIN_BY_NAME);
__uint(max_entries, CIDR4_HMAP_ELEMS);
__uint(map_flags, BPF_F_NO_PREALLOC);
} CIDR6_HMAP_NAME __section_maps_btf;
#ifdef CIDR6_LPM_PREFILTER
struct {
__uint(type, BPF_MAP_TYPE_LPM_TRIE);
__type(key, struct lpm_v6_key);
__type(value, struct lpm_val);
__uint(pinning, LIBBPF_PIN_BY_NAME);
__uint(max_entries, CIDR4_LMAP_ELEMS);
__uint(map_flags, BPF_F_NO_PREALLOC);
} CIDR6_LMAP_NAME __section_maps_btf;
#endif /* CIDR6_LPM_PREFILTER */
#endif /* CIDR6_FILTER */
#endif /* ENABLE_PREFILTER */
static __always_inline __maybe_unused int
bpf_xdp_exit(struct __ctx_buff *ctx, const int verdict)
{
if (verdict == CTX_ACT_OK)
ctx_move_xfer(ctx);
return verdict;
}
#ifdef ENABLE_IPV4
#ifdef ENABLE_NODEPORT_ACCELERATION
__section_tail(CILIUM_MAP_CALLS, CILIUM_CALL_IPV4_FROM_NETDEV)
int tail_lb_ipv4(struct __ctx_buff *ctx)
{
int ret = CTX_ACT_OK;
__s8 ext_err = 0;
if (!ctx_skip_nodeport(ctx)) {
int l3_off = ETH_HLEN;
void *data, *data_end;
struct iphdr *ip4;
bool __maybe_unused is_dsr = false;
if (!revalidate_data(ctx, &data, &data_end, &ip4)) {
ret = DROP_INVALID;
goto out;
}
#if defined(ENABLE_DSR) && !defined(ENABLE_DSR_HYBRID) && DSR_ENCAP_MODE == DSR_ENCAP_GENEVE
{
int l4_off, inner_l2_off;
struct genevehdr geneve;
__sum16 udp_csum;
__be16 dport;
__u16 proto;
if (ip4->protocol != IPPROTO_UDP)
goto no_encap;
/* Punt packets with IP options to TC */
if (ipv4_hdrlen(ip4) != sizeof(*ip4))
goto no_encap;
l4_off = l3_off + sizeof(*ip4);
if (l4_load_port(ctx, l4_off + UDP_DPORT_OFF, &dport) < 0) {
ret = DROP_INVALID;
goto out;
}
if (dport != bpf_htons(TUNNEL_PORT))
goto no_encap;
/* Cilium uses BPF_F_ZERO_CSUM_TX for its tunnel traffic.
*
* Adding LB support for checksummed packets would require
* that we adjust udp->check
* 1. after DNAT of the inner packet,
* 2. after re-writing the outer headers and inserting
* the DSR option
*/
if (ctx_load_bytes(ctx, l4_off + offsetof(struct udphdr, check),
&udp_csum, sizeof(udp_csum)) < 0) {
ret = DROP_INVALID;
goto out;
}
if (udp_csum != 0)
goto no_encap;
if (ctx_load_bytes(ctx, l4_off + sizeof(struct udphdr), &geneve,
sizeof(geneve)) < 0) {
ret = DROP_INVALID;
goto out;
}
if (geneve.protocol_type != bpf_htons(ETH_P_TEB))
goto no_encap;
/* Punt packets with GENEVE options to TC */
if (geneve.opt_len)
goto no_encap;
inner_l2_off = l4_off + sizeof(struct udphdr) + sizeof(struct genevehdr);
/* point at the inner L3 header: */
if (!validate_ethertype_l2_off(ctx, inner_l2_off, &proto))
goto no_encap;
if (proto != bpf_htons(ETH_P_IP))
goto no_encap;
l3_off = inner_l2_off + ETH_HLEN;
if (!revalidate_data_l3_off(ctx, &data, &data_end, &ip4, l3_off)) {
ret = DROP_INVALID;
goto out;
}
}
no_encap:
#endif /* ENABLE_DSR && !ENABLE_DSR_HYBRID && DSR_ENCAP_MODE == DSR_ENCAP_GENEVE */
ret = nodeport_lb4(ctx, ip4, l3_off, 0, &ext_err, &is_dsr);
if (ret == NAT_46X64_RECIRC)
ret = tail_call_internal(ctx, CILIUM_CALL_IPV6_FROM_NETDEV,
&ext_err);
}
out:
if (IS_ERR(ret))
return send_drop_notify_error_ext(ctx, 0, ret, ext_err,
CTX_ACT_DROP, METRIC_INGRESS);
return bpf_xdp_exit(ctx, ret);
}
static __always_inline int check_v4_lb(struct __ctx_buff *ctx)
{
__s8 ext_err = 0;
int ret;
ret = tail_call_internal(ctx, CILIUM_CALL_IPV4_FROM_NETDEV, &ext_err);
return send_drop_notify_error_ext(ctx, 0, ret, ext_err, CTX_ACT_DROP,
METRIC_INGRESS);
}
#else
static __always_inline int check_v4_lb(struct __ctx_buff *ctx __maybe_unused)
{
return CTX_ACT_OK;
}
#endif /* ENABLE_NODEPORT_ACCELERATION */
#ifdef ENABLE_PREFILTER
static __always_inline int check_v4(struct __ctx_buff *ctx)
{
void *data_end = ctx_data_end(ctx);
void *data = ctx_data(ctx);
struct iphdr *ipv4_hdr = data + sizeof(struct ethhdr);
struct lpm_v4_key pfx __maybe_unused;
if (ctx_no_room(ipv4_hdr + 1, data_end))
return CTX_ACT_DROP;
#ifdef CIDR4_FILTER
memcpy(pfx.lpm.data, &ipv4_hdr->saddr, sizeof(pfx.addr));
pfx.lpm.prefixlen = 32;
#ifdef CIDR4_LPM_PREFILTER
if (map_lookup_elem(&CIDR4_LMAP_NAME, &pfx))
return CTX_ACT_DROP;
#endif /* CIDR4_LPM_PREFILTER */
return map_lookup_elem(&CIDR4_HMAP_NAME, &pfx) ?
CTX_ACT_DROP : check_v4_lb(ctx);
#else
return check_v4_lb(ctx);
#endif /* CIDR4_FILTER */
}
#else
static __always_inline int check_v4(struct __ctx_buff *ctx)
{
return check_v4_lb(ctx);
}
#endif /* ENABLE_PREFILTER */
#endif /* ENABLE_IPV4 */
#ifdef ENABLE_IPV6
#ifdef ENABLE_NODEPORT_ACCELERATION
__section_tail(CILIUM_MAP_CALLS, CILIUM_CALL_IPV6_FROM_NETDEV)
int tail_lb_ipv6(struct __ctx_buff *ctx)
{
int ret = CTX_ACT_OK;
__s8 ext_err = 0;
if (!ctx_skip_nodeport(ctx)) {
void *data, *data_end;
struct ipv6hdr *ip6;
bool is_dsr = false;
if (!revalidate_data(ctx, &data, &data_end, &ip6)) {
ret = DROP_INVALID;
goto drop_err;
}
ret = nodeport_lb6(ctx, ip6, 0, &ext_err, &is_dsr);
if (IS_ERR(ret))
goto drop_err;
}
return bpf_xdp_exit(ctx, ret);
drop_err:
return send_drop_notify_error_ext(ctx, 0, ret, ext_err,
CTX_ACT_DROP, METRIC_INGRESS);
}
static __always_inline int check_v6_lb(struct __ctx_buff *ctx)
{
__s8 ext_err = 0;
int ret;
ret = tail_call_internal(ctx, CILIUM_CALL_IPV6_FROM_NETDEV, &ext_err);
return send_drop_notify_error_ext(ctx, 0, ret, ext_err, CTX_ACT_DROP,
METRIC_INGRESS);
}
#else
static __always_inline int check_v6_lb(struct __ctx_buff *ctx __maybe_unused)
{
return CTX_ACT_OK;
}
#endif /* ENABLE_NODEPORT_ACCELERATION */
#ifdef ENABLE_PREFILTER
static __always_inline int check_v6(struct __ctx_buff *ctx)
{
void *data_end = ctx_data_end(ctx);
void *data = ctx_data(ctx);
struct ipv6hdr *ipv6_hdr = data + sizeof(struct ethhdr);
struct lpm_v6_key pfx __maybe_unused;
if (ctx_no_room(ipv6_hdr + 1, data_end))
return CTX_ACT_DROP;
#ifdef CIDR6_FILTER
__bpf_memcpy_builtin(pfx.lpm.data, &ipv6_hdr->saddr, sizeof(pfx.addr));
pfx.lpm.prefixlen = 128;
#ifdef CIDR6_LPM_PREFILTER
if (map_lookup_elem(&CIDR6_LMAP_NAME, &pfx))
return CTX_ACT_DROP;
#endif /* CIDR6_LPM_PREFILTER */
return map_lookup_elem(&CIDR6_HMAP_NAME, &pfx) ?
CTX_ACT_DROP : check_v6_lb(ctx);
#else
return check_v6_lb(ctx);
#endif /* CIDR6_FILTER */
}
#else
static __always_inline int check_v6(struct __ctx_buff *ctx)
{
return check_v6_lb(ctx);
}
#endif /* ENABLE_PREFILTER */
#endif /* ENABLE_IPV6 */
static __always_inline int check_filters(struct __ctx_buff *ctx)
{
int ret = CTX_ACT_OK;
__u16 proto;
if (!validate_ethertype(ctx, &proto))
return CTX_ACT_OK;
ctx_store_meta(ctx, XFER_MARKER, 0);
ctx_skip_nodeport_clear(ctx);
switch (proto) {
#ifdef ENABLE_IPV4
case bpf_htons(ETH_P_IP):
ret = check_v4(ctx);
break;
#endif /* ENABLE_IPV4 */
#ifdef ENABLE_IPV6
case bpf_htons(ETH_P_IPV6):
ret = check_v6(ctx);
break;
#endif /* ENABLE_IPV6 */
default:
break;
}
return bpf_xdp_exit(ctx, ret);
}
__section_entry
int cil_xdp_entry(struct __ctx_buff *ctx)
{
return check_filters(ctx);
}
BPF_LICENSE("Dual BSD/GPL");