1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Linux Socket Filter - Kernel level socket filtering
4 *
5 * Based on the design of the Berkeley Packet Filter. The new
6 * internal format has been designed by PLUMgrid:
7 *
8 * Copyright (c) 2011 - 2014 PLUMgrid, http://plumgrid.com
9 *
10 * Authors:
11 *
12 * Jay Schulist <jschlst@samba.org>
13 * Alexei Starovoitov <ast@plumgrid.com>
14 * Daniel Borkmann <dborkman@redhat.com>
15 *
16 * Andi Kleen - Fix a few bad bugs and races.
17 * Kris Katterjohn - Added many additional checks in bpf_check_classic()
18 */
19
20 #include <linux/atomic.h>
21 #include <linux/bpf_verifier.h>
22 #include <linux/module.h>
23 #include <linux/types.h>
24 #include <linux/mm.h>
25 #include <linux/fcntl.h>
26 #include <linux/socket.h>
27 #include <linux/sock_diag.h>
28 #include <linux/in.h>
29 #include <linux/inet.h>
30 #include <linux/netdevice.h>
31 #include <linux/if_packet.h>
32 #include <linux/if_arp.h>
33 #include <linux/gfp.h>
34 #include <net/inet_common.h>
35 #include <net/ip.h>
36 #include <net/protocol.h>
37 #include <net/netlink.h>
38 #include <linux/skbuff.h>
39 #include <linux/skmsg.h>
40 #include <net/sock.h>
41 #include <net/flow_dissector.h>
42 #include <linux/errno.h>
43 #include <linux/timer.h>
44 #include <linux/uaccess.h>
45 #include <linux/unaligned.h>
46 #include <linux/filter.h>
47 #include <linux/ratelimit.h>
48 #include <linux/seccomp.h>
49 #include <linux/if_vlan.h>
50 #include <linux/bpf.h>
51 #include <linux/btf.h>
52 #include <net/sch_generic.h>
53 #include <net/cls_cgroup.h>
54 #include <net/dst_metadata.h>
55 #include <net/dst.h>
56 #include <net/sock_reuseport.h>
57 #include <net/busy_poll.h>
58 #include <net/tcp.h>
59 #include <net/xfrm.h>
60 #include <net/udp.h>
61 #include <linux/bpf_trace.h>
62 #include <net/xdp_sock.h>
63 #include <linux/inetdevice.h>
64 #include <net/inet_hashtables.h>
65 #include <net/inet6_hashtables.h>
66 #include <net/ip_fib.h>
67 #include <net/nexthop.h>
68 #include <net/flow.h>
69 #include <net/arp.h>
70 #include <net/ipv6.h>
71 #include <net/net_namespace.h>
72 #include <linux/seg6_local.h>
73 #include <net/seg6.h>
74 #include <net/seg6_local.h>
75 #include <net/lwtunnel.h>
76 #include <net/ipv6_stubs.h>
77 #include <net/bpf_sk_storage.h>
78 #include <net/transp_v6.h>
79 #include <linux/btf_ids.h>
80 #include <net/tls.h>
81 #include <net/xdp.h>
82 #include <net/mptcp.h>
83 #include <net/netfilter/nf_conntrack_bpf.h>
84 #include <net/netkit.h>
85 #include <linux/un.h>
86 #include <net/xdp_sock_drv.h>
87 #include <net/inet_dscp.h>
88
89 #include "dev.h"
90
91 /* Keep the struct bpf_fib_lookup small so that it fits into a cacheline */
92 static_assert(sizeof(struct bpf_fib_lookup) == 64, "struct bpf_fib_lookup size check");
93
94 static const struct bpf_func_proto *
95 bpf_sk_base_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog);
96
copy_bpf_fprog_from_user(struct sock_fprog * dst,sockptr_t src,int len)97 int copy_bpf_fprog_from_user(struct sock_fprog *dst, sockptr_t src, int len)
98 {
99 if (in_compat_syscall()) {
100 struct compat_sock_fprog f32;
101
102 if (len != sizeof(f32))
103 return -EINVAL;
104 if (copy_from_sockptr(&f32, src, sizeof(f32)))
105 return -EFAULT;
106 memset(dst, 0, sizeof(*dst));
107 dst->len = f32.len;
108 dst->filter = compat_ptr(f32.filter);
109 } else {
110 if (len != sizeof(*dst))
111 return -EINVAL;
112 if (copy_from_sockptr(dst, src, sizeof(*dst)))
113 return -EFAULT;
114 }
115
116 return 0;
117 }
118 EXPORT_SYMBOL_GPL(copy_bpf_fprog_from_user);
119
120 /**
121 * sk_filter_trim_cap - run a packet through a socket filter
122 * @sk: sock associated with &sk_buff
123 * @skb: buffer to filter
124 * @cap: limit on how short the eBPF program may trim the packet
125 * @reason: record drop reason on errors (negative return value)
126 *
127 * Run the eBPF program and then cut skb->data to correct size returned by
128 * the program. If pkt_len is 0 we toss packet. If skb->len is smaller
129 * than pkt_len we keep whole skb->data. This is the socket level
130 * wrapper to bpf_prog_run. It returns 0 if the packet should
131 * be accepted or -EPERM if the packet should be tossed.
132 *
133 */
sk_filter_trim_cap(struct sock * sk,struct sk_buff * skb,unsigned int cap,enum skb_drop_reason * reason)134 int sk_filter_trim_cap(struct sock *sk, struct sk_buff *skb,
135 unsigned int cap, enum skb_drop_reason *reason)
136 {
137 int err;
138 struct sk_filter *filter;
139
140 /*
141 * If the skb was allocated from pfmemalloc reserves, only
142 * allow SOCK_MEMALLOC sockets to use it as this socket is
143 * helping free memory
144 */
145 if (skb_pfmemalloc(skb) && !sock_flag(sk, SOCK_MEMALLOC)) {
146 NET_INC_STATS(sock_net(sk), LINUX_MIB_PFMEMALLOCDROP);
147 *reason = SKB_DROP_REASON_PFMEMALLOC;
148 return -ENOMEM;
149 }
150 err = BPF_CGROUP_RUN_PROG_INET_INGRESS(sk, skb);
151 if (err) {
152 *reason = SKB_DROP_REASON_SOCKET_FILTER;
153 return err;
154 }
155
156 err = security_sock_rcv_skb(sk, skb);
157 if (err) {
158 *reason = SKB_DROP_REASON_SECURITY_HOOK;
159 return err;
160 }
161
162 rcu_read_lock();
163 filter = rcu_dereference(sk->sk_filter);
164 if (filter) {
165 struct sock *save_sk = skb->sk;
166 unsigned int pkt_len;
167
168 skb->sk = sk;
169 pkt_len = bpf_prog_run_save_cb(filter->prog, skb);
170 skb->sk = save_sk;
171 err = pkt_len ? pskb_trim(skb, max(cap, pkt_len)) : -EPERM;
172 if (err)
173 *reason = SKB_DROP_REASON_SOCKET_FILTER;
174 }
175 rcu_read_unlock();
176
177 return err;
178 }
179 EXPORT_SYMBOL(sk_filter_trim_cap);
180
BPF_CALL_1(bpf_skb_get_pay_offset,struct sk_buff *,skb)181 BPF_CALL_1(bpf_skb_get_pay_offset, struct sk_buff *, skb)
182 {
183 return skb_get_poff(skb);
184 }
185
BPF_CALL_3(bpf_skb_get_nlattr,struct sk_buff *,skb,u32,a,u32,x)186 BPF_CALL_3(bpf_skb_get_nlattr, struct sk_buff *, skb, u32, a, u32, x)
187 {
188 struct nlattr *nla;
189
190 if (skb_is_nonlinear(skb))
191 return 0;
192
193 if (skb->len < sizeof(struct nlattr))
194 return 0;
195
196 if (a > skb->len - sizeof(struct nlattr))
197 return 0;
198
199 nla = nla_find((struct nlattr *) &skb->data[a], skb->len - a, x);
200 if (nla)
201 return (void *) nla - (void *) skb->data;
202
203 return 0;
204 }
205
BPF_CALL_3(bpf_skb_get_nlattr_nest,struct sk_buff *,skb,u32,a,u32,x)206 BPF_CALL_3(bpf_skb_get_nlattr_nest, struct sk_buff *, skb, u32, a, u32, x)
207 {
208 struct nlattr *nla;
209
210 if (skb_is_nonlinear(skb))
211 return 0;
212
213 if (skb->len < sizeof(struct nlattr))
214 return 0;
215
216 if (a > skb->len - sizeof(struct nlattr))
217 return 0;
218
219 nla = (struct nlattr *) &skb->data[a];
220 if (!nla_ok(nla, skb->len - a))
221 return 0;
222
223 nla = nla_find_nested(nla, x);
224 if (nla)
225 return (void *) nla - (void *) skb->data;
226
227 return 0;
228 }
229
bpf_skb_load_helper_convert_offset(const struct sk_buff * skb,int offset)230 static int bpf_skb_load_helper_convert_offset(const struct sk_buff *skb, int offset)
231 {
232 if (likely(offset >= 0))
233 return offset;
234
235 if (offset >= SKF_NET_OFF)
236 return offset - SKF_NET_OFF + skb_network_offset(skb);
237
238 if (offset >= SKF_LL_OFF && skb_mac_header_was_set(skb))
239 return offset - SKF_LL_OFF + skb_mac_offset(skb);
240
241 return INT_MIN;
242 }
243
BPF_CALL_4(bpf_skb_load_helper_8,const struct sk_buff *,skb,const void *,data,int,headlen,int,offset)244 BPF_CALL_4(bpf_skb_load_helper_8, const struct sk_buff *, skb, const void *,
245 data, int, headlen, int, offset)
246 {
247 u8 tmp;
248 const int len = sizeof(tmp);
249
250 offset = bpf_skb_load_helper_convert_offset(skb, offset);
251 if (offset == INT_MIN)
252 return -EFAULT;
253
254 if (headlen - offset >= len)
255 return *(u8 *)(data + offset);
256 if (!skb_copy_bits(skb, offset, &tmp, sizeof(tmp)))
257 return tmp;
258 else
259 return -EFAULT;
260 }
261
BPF_CALL_2(bpf_skb_load_helper_8_no_cache,const struct sk_buff *,skb,int,offset)262 BPF_CALL_2(bpf_skb_load_helper_8_no_cache, const struct sk_buff *, skb,
263 int, offset)
264 {
265 return ____bpf_skb_load_helper_8(skb, skb->data, skb->len - skb->data_len,
266 offset);
267 }
268
BPF_CALL_4(bpf_skb_load_helper_16,const struct sk_buff *,skb,const void *,data,int,headlen,int,offset)269 BPF_CALL_4(bpf_skb_load_helper_16, const struct sk_buff *, skb, const void *,
270 data, int, headlen, int, offset)
271 {
272 __be16 tmp;
273 const int len = sizeof(tmp);
274
275 offset = bpf_skb_load_helper_convert_offset(skb, offset);
276 if (offset == INT_MIN)
277 return -EFAULT;
278
279 if (headlen - offset >= len)
280 return get_unaligned_be16(data + offset);
281 if (!skb_copy_bits(skb, offset, &tmp, sizeof(tmp)))
282 return be16_to_cpu(tmp);
283 else
284 return -EFAULT;
285 }
286
BPF_CALL_2(bpf_skb_load_helper_16_no_cache,const struct sk_buff *,skb,int,offset)287 BPF_CALL_2(bpf_skb_load_helper_16_no_cache, const struct sk_buff *, skb,
288 int, offset)
289 {
290 return ____bpf_skb_load_helper_16(skb, skb->data, skb->len - skb->data_len,
291 offset);
292 }
293
BPF_CALL_4(bpf_skb_load_helper_32,const struct sk_buff *,skb,const void *,data,int,headlen,int,offset)294 BPF_CALL_4(bpf_skb_load_helper_32, const struct sk_buff *, skb, const void *,
295 data, int, headlen, int, offset)
296 {
297 __be32 tmp;
298 const int len = sizeof(tmp);
299
300 offset = bpf_skb_load_helper_convert_offset(skb, offset);
301 if (offset == INT_MIN)
302 return -EFAULT;
303
304 if (headlen - offset >= len)
305 return get_unaligned_be32(data + offset);
306 if (!skb_copy_bits(skb, offset, &tmp, sizeof(tmp)))
307 return be32_to_cpu(tmp);
308 else
309 return -EFAULT;
310 }
311
BPF_CALL_2(bpf_skb_load_helper_32_no_cache,const struct sk_buff *,skb,int,offset)312 BPF_CALL_2(bpf_skb_load_helper_32_no_cache, const struct sk_buff *, skb,
313 int, offset)
314 {
315 return ____bpf_skb_load_helper_32(skb, skb->data, skb->len - skb->data_len,
316 offset);
317 }
318
convert_skb_access(int skb_field,int dst_reg,int src_reg,struct bpf_insn * insn_buf)319 static u32 convert_skb_access(int skb_field, int dst_reg, int src_reg,
320 struct bpf_insn *insn_buf)
321 {
322 struct bpf_insn *insn = insn_buf;
323
324 switch (skb_field) {
325 case SKF_AD_MARK:
326 BUILD_BUG_ON(sizeof_field(struct sk_buff, mark) != 4);
327
328 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
329 offsetof(struct sk_buff, mark));
330 break;
331
332 case SKF_AD_PKTTYPE:
333 *insn++ = BPF_LDX_MEM(BPF_B, dst_reg, src_reg, PKT_TYPE_OFFSET);
334 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, PKT_TYPE_MAX);
335 #ifdef __BIG_ENDIAN_BITFIELD
336 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 5);
337 #endif
338 break;
339
340 case SKF_AD_QUEUE:
341 BUILD_BUG_ON(sizeof_field(struct sk_buff, queue_mapping) != 2);
342
343 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
344 offsetof(struct sk_buff, queue_mapping));
345 break;
346
347 case SKF_AD_VLAN_TAG:
348 BUILD_BUG_ON(sizeof_field(struct sk_buff, vlan_tci) != 2);
349
350 /* dst_reg = *(u16 *) (src_reg + offsetof(vlan_tci)) */
351 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
352 offsetof(struct sk_buff, vlan_tci));
353 break;
354 case SKF_AD_VLAN_TAG_PRESENT:
355 BUILD_BUG_ON(sizeof_field(struct sk_buff, vlan_all) != 4);
356 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
357 offsetof(struct sk_buff, vlan_all));
358 *insn++ = BPF_JMP_IMM(BPF_JEQ, dst_reg, 0, 1);
359 *insn++ = BPF_ALU32_IMM(BPF_MOV, dst_reg, 1);
360 break;
361 }
362
363 return insn - insn_buf;
364 }
365
convert_bpf_extensions(struct sock_filter * fp,struct bpf_insn ** insnp)366 static bool convert_bpf_extensions(struct sock_filter *fp,
367 struct bpf_insn **insnp)
368 {
369 struct bpf_insn *insn = *insnp;
370 u32 cnt;
371
372 switch (fp->k) {
373 case SKF_AD_OFF + SKF_AD_PROTOCOL:
374 BUILD_BUG_ON(sizeof_field(struct sk_buff, protocol) != 2);
375
376 /* A = *(u16 *) (CTX + offsetof(protocol)) */
377 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
378 offsetof(struct sk_buff, protocol));
379 /* A = ntohs(A) [emitting a nop or swap16] */
380 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
381 break;
382
383 case SKF_AD_OFF + SKF_AD_PKTTYPE:
384 cnt = convert_skb_access(SKF_AD_PKTTYPE, BPF_REG_A, BPF_REG_CTX, insn);
385 insn += cnt - 1;
386 break;
387
388 case SKF_AD_OFF + SKF_AD_IFINDEX:
389 case SKF_AD_OFF + SKF_AD_HATYPE:
390 BUILD_BUG_ON(sizeof_field(struct net_device, ifindex) != 4);
391 BUILD_BUG_ON(sizeof_field(struct net_device, type) != 2);
392
393 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
394 BPF_REG_TMP, BPF_REG_CTX,
395 offsetof(struct sk_buff, dev));
396 /* if (tmp != 0) goto pc + 1 */
397 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_TMP, 0, 1);
398 *insn++ = BPF_EXIT_INSN();
399 if (fp->k == SKF_AD_OFF + SKF_AD_IFINDEX)
400 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_TMP,
401 offsetof(struct net_device, ifindex));
402 else
403 *insn = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_TMP,
404 offsetof(struct net_device, type));
405 break;
406
407 case SKF_AD_OFF + SKF_AD_MARK:
408 cnt = convert_skb_access(SKF_AD_MARK, BPF_REG_A, BPF_REG_CTX, insn);
409 insn += cnt - 1;
410 break;
411
412 case SKF_AD_OFF + SKF_AD_RXHASH:
413 BUILD_BUG_ON(sizeof_field(struct sk_buff, hash) != 4);
414
415 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX,
416 offsetof(struct sk_buff, hash));
417 break;
418
419 case SKF_AD_OFF + SKF_AD_QUEUE:
420 cnt = convert_skb_access(SKF_AD_QUEUE, BPF_REG_A, BPF_REG_CTX, insn);
421 insn += cnt - 1;
422 break;
423
424 case SKF_AD_OFF + SKF_AD_VLAN_TAG:
425 cnt = convert_skb_access(SKF_AD_VLAN_TAG,
426 BPF_REG_A, BPF_REG_CTX, insn);
427 insn += cnt - 1;
428 break;
429
430 case SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT:
431 cnt = convert_skb_access(SKF_AD_VLAN_TAG_PRESENT,
432 BPF_REG_A, BPF_REG_CTX, insn);
433 insn += cnt - 1;
434 break;
435
436 case SKF_AD_OFF + SKF_AD_VLAN_TPID:
437 BUILD_BUG_ON(sizeof_field(struct sk_buff, vlan_proto) != 2);
438
439 /* A = *(u16 *) (CTX + offsetof(vlan_proto)) */
440 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
441 offsetof(struct sk_buff, vlan_proto));
442 /* A = ntohs(A) [emitting a nop or swap16] */
443 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
444 break;
445
446 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
447 case SKF_AD_OFF + SKF_AD_NLATTR:
448 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
449 case SKF_AD_OFF + SKF_AD_CPU:
450 case SKF_AD_OFF + SKF_AD_RANDOM:
451 /* arg1 = CTX */
452 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX);
453 /* arg2 = A */
454 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_A);
455 /* arg3 = X */
456 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_X);
457 /* Emit call(arg1=CTX, arg2=A, arg3=X) */
458 switch (fp->k) {
459 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
460 *insn = BPF_EMIT_CALL(bpf_skb_get_pay_offset);
461 break;
462 case SKF_AD_OFF + SKF_AD_NLATTR:
463 *insn = BPF_EMIT_CALL(bpf_skb_get_nlattr);
464 break;
465 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
466 *insn = BPF_EMIT_CALL(bpf_skb_get_nlattr_nest);
467 break;
468 case SKF_AD_OFF + SKF_AD_CPU:
469 *insn = BPF_EMIT_CALL(bpf_get_raw_cpu_id);
470 break;
471 case SKF_AD_OFF + SKF_AD_RANDOM:
472 *insn = BPF_EMIT_CALL(bpf_user_rnd_u32);
473 bpf_user_rnd_init_once();
474 break;
475 }
476 break;
477
478 case SKF_AD_OFF + SKF_AD_ALU_XOR_X:
479 /* A ^= X */
480 *insn = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_X);
481 break;
482
483 default:
484 /* This is just a dummy call to avoid letting the compiler
485 * evict __bpf_call_base() as an optimization. Placed here
486 * where no-one bothers.
487 */
488 BUG_ON(__bpf_call_base(0, 0, 0, 0, 0) != 0);
489 return false;
490 }
491
492 *insnp = insn;
493 return true;
494 }
495
convert_bpf_ld_abs(struct sock_filter * fp,struct bpf_insn ** insnp)496 static bool convert_bpf_ld_abs(struct sock_filter *fp, struct bpf_insn **insnp)
497 {
498 const bool unaligned_ok = IS_BUILTIN(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS);
499 int size = bpf_size_to_bytes(BPF_SIZE(fp->code));
500 bool endian = BPF_SIZE(fp->code) == BPF_H ||
501 BPF_SIZE(fp->code) == BPF_W;
502 bool indirect = BPF_MODE(fp->code) == BPF_IND;
503 const int ip_align = NET_IP_ALIGN;
504 struct bpf_insn *insn = *insnp;
505 int offset = fp->k;
506
507 if (!indirect &&
508 ((unaligned_ok && offset >= 0) ||
509 (!unaligned_ok && offset >= 0 &&
510 offset + ip_align >= 0 &&
511 offset + ip_align % size == 0))) {
512 bool ldx_off_ok = offset <= S16_MAX;
513
514 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_H);
515 if (offset)
516 *insn++ = BPF_ALU64_IMM(BPF_SUB, BPF_REG_TMP, offset);
517 *insn++ = BPF_JMP_IMM(BPF_JSLT, BPF_REG_TMP,
518 size, 2 + endian + (!ldx_off_ok * 2));
519 if (ldx_off_ok) {
520 *insn++ = BPF_LDX_MEM(BPF_SIZE(fp->code), BPF_REG_A,
521 BPF_REG_D, offset);
522 } else {
523 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_D);
524 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_TMP, offset);
525 *insn++ = BPF_LDX_MEM(BPF_SIZE(fp->code), BPF_REG_A,
526 BPF_REG_TMP, 0);
527 }
528 if (endian)
529 *insn++ = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, size * 8);
530 *insn++ = BPF_JMP_A(8);
531 }
532
533 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX);
534 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_D);
535 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_H);
536 if (!indirect) {
537 *insn++ = BPF_MOV64_IMM(BPF_REG_ARG4, offset);
538 } else {
539 *insn++ = BPF_MOV64_REG(BPF_REG_ARG4, BPF_REG_X);
540 if (fp->k)
541 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_ARG4, offset);
542 }
543
544 switch (BPF_SIZE(fp->code)) {
545 case BPF_B:
546 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_8);
547 break;
548 case BPF_H:
549 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_16);
550 break;
551 case BPF_W:
552 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_32);
553 break;
554 default:
555 return false;
556 }
557
558 *insn++ = BPF_JMP_IMM(BPF_JSGE, BPF_REG_A, 0, 2);
559 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
560 *insn = BPF_EXIT_INSN();
561
562 *insnp = insn;
563 return true;
564 }
565
566 /**
567 * bpf_convert_filter - convert filter program
568 * @prog: the user passed filter program
569 * @len: the length of the user passed filter program
570 * @new_prog: allocated 'struct bpf_prog' or NULL
571 * @new_len: pointer to store length of converted program
572 * @seen_ld_abs: bool whether we've seen ld_abs/ind
573 *
574 * Remap 'sock_filter' style classic BPF (cBPF) instruction set to 'bpf_insn'
575 * style extended BPF (eBPF).
576 * Conversion workflow:
577 *
578 * 1) First pass for calculating the new program length:
579 * bpf_convert_filter(old_prog, old_len, NULL, &new_len, &seen_ld_abs)
580 *
581 * 2) 2nd pass to remap in two passes: 1st pass finds new
582 * jump offsets, 2nd pass remapping:
583 * bpf_convert_filter(old_prog, old_len, new_prog, &new_len, &seen_ld_abs)
584 */
bpf_convert_filter(struct sock_filter * prog,int len,struct bpf_prog * new_prog,int * new_len,bool * seen_ld_abs)585 static int bpf_convert_filter(struct sock_filter *prog, int len,
586 struct bpf_prog *new_prog, int *new_len,
587 bool *seen_ld_abs)
588 {
589 int new_flen = 0, pass = 0, target, i, stack_off;
590 struct bpf_insn *new_insn, *first_insn = NULL;
591 struct sock_filter *fp;
592 int *addrs = NULL;
593 u8 bpf_src;
594
595 BUILD_BUG_ON(BPF_MEMWORDS * sizeof(u32) > MAX_BPF_STACK);
596 BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG);
597
598 if (len <= 0 || len > BPF_MAXINSNS)
599 return -EINVAL;
600
601 if (new_prog) {
602 first_insn = new_prog->insnsi;
603 addrs = kzalloc_objs(*addrs, len, GFP_KERNEL | __GFP_NOWARN);
604 if (!addrs)
605 return -ENOMEM;
606 }
607
608 do_pass:
609 new_insn = first_insn;
610 fp = prog;
611
612 /* Classic BPF related prologue emission. */
613 if (new_prog) {
614 /* Classic BPF expects A and X to be reset first. These need
615 * to be guaranteed to be the first two instructions.
616 */
617 *new_insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
618 *new_insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_X, BPF_REG_X);
619
620 /* All programs must keep CTX in callee saved BPF_REG_CTX.
621 * In eBPF case it's done by the compiler, here we need to
622 * do this ourself. Initial CTX is present in BPF_REG_ARG1.
623 */
624 *new_insn++ = BPF_MOV64_REG(BPF_REG_CTX, BPF_REG_ARG1);
625 if (*seen_ld_abs) {
626 /* For packet access in classic BPF, cache skb->data
627 * in callee-saved BPF R8 and skb->len - skb->data_len
628 * (headlen) in BPF R9. Since classic BPF is read-only
629 * on CTX, we only need to cache it once.
630 */
631 *new_insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
632 BPF_REG_D, BPF_REG_CTX,
633 offsetof(struct sk_buff, data));
634 *new_insn++ = BPF_LDX_MEM(BPF_W, BPF_REG_H, BPF_REG_CTX,
635 offsetof(struct sk_buff, len));
636 *new_insn++ = BPF_LDX_MEM(BPF_W, BPF_REG_TMP, BPF_REG_CTX,
637 offsetof(struct sk_buff, data_len));
638 *new_insn++ = BPF_ALU32_REG(BPF_SUB, BPF_REG_H, BPF_REG_TMP);
639 }
640 } else {
641 new_insn += 3;
642 }
643
644 for (i = 0; i < len; fp++, i++) {
645 struct bpf_insn tmp_insns[32] = { };
646 struct bpf_insn *insn = tmp_insns;
647
648 if (addrs)
649 addrs[i] = new_insn - first_insn;
650
651 switch (fp->code) {
652 /* All arithmetic insns and skb loads map as-is. */
653 case BPF_ALU | BPF_ADD | BPF_X:
654 case BPF_ALU | BPF_ADD | BPF_K:
655 case BPF_ALU | BPF_SUB | BPF_X:
656 case BPF_ALU | BPF_SUB | BPF_K:
657 case BPF_ALU | BPF_AND | BPF_X:
658 case BPF_ALU | BPF_AND | BPF_K:
659 case BPF_ALU | BPF_OR | BPF_X:
660 case BPF_ALU | BPF_OR | BPF_K:
661 case BPF_ALU | BPF_LSH | BPF_X:
662 case BPF_ALU | BPF_LSH | BPF_K:
663 case BPF_ALU | BPF_RSH | BPF_X:
664 case BPF_ALU | BPF_RSH | BPF_K:
665 case BPF_ALU | BPF_XOR | BPF_X:
666 case BPF_ALU | BPF_XOR | BPF_K:
667 case BPF_ALU | BPF_MUL | BPF_X:
668 case BPF_ALU | BPF_MUL | BPF_K:
669 case BPF_ALU | BPF_DIV | BPF_X:
670 case BPF_ALU | BPF_DIV | BPF_K:
671 case BPF_ALU | BPF_MOD | BPF_X:
672 case BPF_ALU | BPF_MOD | BPF_K:
673 case BPF_ALU | BPF_NEG:
674 case BPF_LD | BPF_ABS | BPF_W:
675 case BPF_LD | BPF_ABS | BPF_H:
676 case BPF_LD | BPF_ABS | BPF_B:
677 case BPF_LD | BPF_IND | BPF_W:
678 case BPF_LD | BPF_IND | BPF_H:
679 case BPF_LD | BPF_IND | BPF_B:
680 /* Check for overloaded BPF extension and
681 * directly convert it if found, otherwise
682 * just move on with mapping.
683 */
684 if (BPF_CLASS(fp->code) == BPF_LD &&
685 BPF_MODE(fp->code) == BPF_ABS &&
686 convert_bpf_extensions(fp, &insn))
687 break;
688 if (BPF_CLASS(fp->code) == BPF_LD &&
689 convert_bpf_ld_abs(fp, &insn)) {
690 *seen_ld_abs = true;
691 break;
692 }
693
694 if (fp->code == (BPF_ALU | BPF_DIV | BPF_X) ||
695 fp->code == (BPF_ALU | BPF_MOD | BPF_X)) {
696 *insn++ = BPF_MOV32_REG(BPF_REG_X, BPF_REG_X);
697 /* Error with exception code on div/mod by 0.
698 * For cBPF programs, this was always return 0.
699 */
700 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_X, 0, 2);
701 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
702 *insn++ = BPF_EXIT_INSN();
703 }
704
705 *insn = BPF_RAW_INSN(fp->code, BPF_REG_A, BPF_REG_X, 0, fp->k);
706 break;
707
708 /* Jump transformation cannot use BPF block macros
709 * everywhere as offset calculation and target updates
710 * require a bit more work than the rest, i.e. jump
711 * opcodes map as-is, but offsets need adjustment.
712 */
713
714 #define BPF_EMIT_JMP \
715 do { \
716 const s32 off_min = S16_MIN, off_max = S16_MAX; \
717 s32 off; \
718 \
719 if (target >= len || target < 0) \
720 goto err; \
721 off = addrs ? addrs[target] - addrs[i] - 1 : 0; \
722 /* Adjust pc relative offset for 2nd or 3rd insn. */ \
723 off -= insn - tmp_insns; \
724 /* Reject anything not fitting into insn->off. */ \
725 if (off < off_min || off > off_max) \
726 goto err; \
727 insn->off = off; \
728 } while (0)
729
730 case BPF_JMP | BPF_JA:
731 target = i + fp->k + 1;
732 insn->code = fp->code;
733 BPF_EMIT_JMP;
734 break;
735
736 case BPF_JMP | BPF_JEQ | BPF_K:
737 case BPF_JMP | BPF_JEQ | BPF_X:
738 case BPF_JMP | BPF_JSET | BPF_K:
739 case BPF_JMP | BPF_JSET | BPF_X:
740 case BPF_JMP | BPF_JGT | BPF_K:
741 case BPF_JMP | BPF_JGT | BPF_X:
742 case BPF_JMP | BPF_JGE | BPF_K:
743 case BPF_JMP | BPF_JGE | BPF_X:
744 if (BPF_SRC(fp->code) == BPF_K && (int) fp->k < 0) {
745 /* BPF immediates are signed, zero extend
746 * immediate into tmp register and use it
747 * in compare insn.
748 */
749 *insn++ = BPF_MOV32_IMM(BPF_REG_TMP, fp->k);
750
751 insn->dst_reg = BPF_REG_A;
752 insn->src_reg = BPF_REG_TMP;
753 bpf_src = BPF_X;
754 } else {
755 insn->dst_reg = BPF_REG_A;
756 insn->imm = fp->k;
757 bpf_src = BPF_SRC(fp->code);
758 insn->src_reg = bpf_src == BPF_X ? BPF_REG_X : 0;
759 }
760
761 /* Common case where 'jump_false' is next insn. */
762 if (fp->jf == 0) {
763 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
764 target = i + fp->jt + 1;
765 BPF_EMIT_JMP;
766 break;
767 }
768
769 /* Convert some jumps when 'jump_true' is next insn. */
770 if (fp->jt == 0) {
771 switch (BPF_OP(fp->code)) {
772 case BPF_JEQ:
773 insn->code = BPF_JMP | BPF_JNE | bpf_src;
774 break;
775 case BPF_JGT:
776 insn->code = BPF_JMP | BPF_JLE | bpf_src;
777 break;
778 case BPF_JGE:
779 insn->code = BPF_JMP | BPF_JLT | bpf_src;
780 break;
781 default:
782 goto jmp_rest;
783 }
784
785 target = i + fp->jf + 1;
786 BPF_EMIT_JMP;
787 break;
788 }
789 jmp_rest:
790 /* Other jumps are mapped into two insns: Jxx and JA. */
791 target = i + fp->jt + 1;
792 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
793 BPF_EMIT_JMP;
794 insn++;
795
796 insn->code = BPF_JMP | BPF_JA;
797 target = i + fp->jf + 1;
798 BPF_EMIT_JMP;
799 break;
800
801 /* ldxb 4 * ([14] & 0xf) is remapped into 6 insns. */
802 case BPF_LDX | BPF_MSH | BPF_B: {
803 struct sock_filter tmp = {
804 .code = BPF_LD | BPF_ABS | BPF_B,
805 .k = fp->k,
806 };
807
808 *seen_ld_abs = true;
809
810 /* X = A */
811 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
812 /* A = BPF_R0 = *(u8 *) (skb->data + K) */
813 convert_bpf_ld_abs(&tmp, &insn);
814 insn++;
815 /* A &= 0xf */
816 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, 0xf);
817 /* A <<= 2 */
818 *insn++ = BPF_ALU32_IMM(BPF_LSH, BPF_REG_A, 2);
819 /* tmp = X */
820 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_X);
821 /* X = A */
822 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
823 /* A = tmp */
824 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_TMP);
825 break;
826 }
827 /* RET_K is remapped into 2 insns. RET_A case doesn't need an
828 * extra mov as BPF_REG_0 is already mapped into BPF_REG_A.
829 */
830 case BPF_RET | BPF_A:
831 case BPF_RET | BPF_K:
832 if (BPF_RVAL(fp->code) == BPF_K)
833 *insn++ = BPF_MOV32_RAW(BPF_K, BPF_REG_0,
834 0, fp->k);
835 *insn = BPF_EXIT_INSN();
836 break;
837
838 /* Store to stack. */
839 case BPF_ST:
840 case BPF_STX:
841 stack_off = fp->k * 4 + 4;
842 *insn = BPF_STX_MEM(BPF_W, BPF_REG_FP, BPF_CLASS(fp->code) ==
843 BPF_ST ? BPF_REG_A : BPF_REG_X,
844 -stack_off);
845 /* check_load_and_stores() verifies that classic BPF can
846 * load from stack only after write, so tracking
847 * stack_depth for ST|STX insns is enough
848 */
849 if (new_prog && new_prog->aux->stack_depth < stack_off)
850 new_prog->aux->stack_depth = stack_off;
851 break;
852
853 /* Load from stack. */
854 case BPF_LD | BPF_MEM:
855 case BPF_LDX | BPF_MEM:
856 stack_off = fp->k * 4 + 4;
857 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
858 BPF_REG_A : BPF_REG_X, BPF_REG_FP,
859 -stack_off);
860 break;
861
862 /* A = K or X = K */
863 case BPF_LD | BPF_IMM:
864 case BPF_LDX | BPF_IMM:
865 *insn = BPF_MOV32_IMM(BPF_CLASS(fp->code) == BPF_LD ?
866 BPF_REG_A : BPF_REG_X, fp->k);
867 break;
868
869 /* X = A */
870 case BPF_MISC | BPF_TAX:
871 *insn = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
872 break;
873
874 /* A = X */
875 case BPF_MISC | BPF_TXA:
876 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_X);
877 break;
878
879 /* A = skb->len or X = skb->len */
880 case BPF_LD | BPF_W | BPF_LEN:
881 case BPF_LDX | BPF_W | BPF_LEN:
882 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
883 BPF_REG_A : BPF_REG_X, BPF_REG_CTX,
884 offsetof(struct sk_buff, len));
885 break;
886
887 /* Access seccomp_data fields. */
888 case BPF_LDX | BPF_ABS | BPF_W:
889 /* A = *(u32 *) (ctx + K) */
890 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX, fp->k);
891 break;
892
893 /* Unknown instruction. */
894 default:
895 goto err;
896 }
897
898 insn++;
899 if (new_prog)
900 memcpy(new_insn, tmp_insns,
901 sizeof(*insn) * (insn - tmp_insns));
902 new_insn += insn - tmp_insns;
903 }
904
905 if (!new_prog) {
906 /* Only calculating new length. */
907 *new_len = new_insn - first_insn;
908 if (*seen_ld_abs)
909 *new_len += 4; /* Prologue bits. */
910 return 0;
911 }
912
913 pass++;
914 if (new_flen != new_insn - first_insn) {
915 new_flen = new_insn - first_insn;
916 if (pass > 2)
917 goto err;
918 goto do_pass;
919 }
920
921 kfree(addrs);
922 BUG_ON(*new_len != new_flen);
923 return 0;
924 err:
925 kfree(addrs);
926 return -EINVAL;
927 }
928
929 /* Security:
930 *
931 * As we dont want to clear mem[] array for each packet going through
932 * __bpf_prog_run(), we check that filter loaded by user never try to read
933 * a cell if not previously written, and we check all branches to be sure
934 * a malicious user doesn't try to abuse us.
935 */
check_load_and_stores(const struct sock_filter * filter,int flen)936 static int check_load_and_stores(const struct sock_filter *filter, int flen)
937 {
938 u16 *masks, memvalid = 0; /* One bit per cell, 16 cells */
939 int pc, ret = 0;
940
941 BUILD_BUG_ON(BPF_MEMWORDS > 16);
942
943 masks = kmalloc_array(flen, sizeof(*masks), GFP_KERNEL);
944 if (!masks)
945 return -ENOMEM;
946
947 memset(masks, 0xff, flen * sizeof(*masks));
948
949 for (pc = 0; pc < flen; pc++) {
950 memvalid &= masks[pc];
951
952 switch (filter[pc].code) {
953 case BPF_ST:
954 case BPF_STX:
955 memvalid |= (1 << filter[pc].k);
956 break;
957 case BPF_LD | BPF_MEM:
958 case BPF_LDX | BPF_MEM:
959 if (!(memvalid & (1 << filter[pc].k))) {
960 ret = -EINVAL;
961 goto error;
962 }
963 break;
964 case BPF_JMP | BPF_JA:
965 /* A jump must set masks on target */
966 masks[pc + 1 + filter[pc].k] &= memvalid;
967 memvalid = ~0;
968 break;
969 case BPF_JMP | BPF_JEQ | BPF_K:
970 case BPF_JMP | BPF_JEQ | BPF_X:
971 case BPF_JMP | BPF_JGE | BPF_K:
972 case BPF_JMP | BPF_JGE | BPF_X:
973 case BPF_JMP | BPF_JGT | BPF_K:
974 case BPF_JMP | BPF_JGT | BPF_X:
975 case BPF_JMP | BPF_JSET | BPF_K:
976 case BPF_JMP | BPF_JSET | BPF_X:
977 /* A jump must set masks on targets */
978 masks[pc + 1 + filter[pc].jt] &= memvalid;
979 masks[pc + 1 + filter[pc].jf] &= memvalid;
980 memvalid = ~0;
981 break;
982 }
983 }
984 error:
985 kfree(masks);
986 return ret;
987 }
988
chk_code_allowed(u16 code_to_probe)989 static bool chk_code_allowed(u16 code_to_probe)
990 {
991 static const bool codes[] = {
992 /* 32 bit ALU operations */
993 [BPF_ALU | BPF_ADD | BPF_K] = true,
994 [BPF_ALU | BPF_ADD | BPF_X] = true,
995 [BPF_ALU | BPF_SUB | BPF_K] = true,
996 [BPF_ALU | BPF_SUB | BPF_X] = true,
997 [BPF_ALU | BPF_MUL | BPF_K] = true,
998 [BPF_ALU | BPF_MUL | BPF_X] = true,
999 [BPF_ALU | BPF_DIV | BPF_K] = true,
1000 [BPF_ALU | BPF_DIV | BPF_X] = true,
1001 [BPF_ALU | BPF_MOD | BPF_K] = true,
1002 [BPF_ALU | BPF_MOD | BPF_X] = true,
1003 [BPF_ALU | BPF_AND | BPF_K] = true,
1004 [BPF_ALU | BPF_AND | BPF_X] = true,
1005 [BPF_ALU | BPF_OR | BPF_K] = true,
1006 [BPF_ALU | BPF_OR | BPF_X] = true,
1007 [BPF_ALU | BPF_XOR | BPF_K] = true,
1008 [BPF_ALU | BPF_XOR | BPF_X] = true,
1009 [BPF_ALU | BPF_LSH | BPF_K] = true,
1010 [BPF_ALU | BPF_LSH | BPF_X] = true,
1011 [BPF_ALU | BPF_RSH | BPF_K] = true,
1012 [BPF_ALU | BPF_RSH | BPF_X] = true,
1013 [BPF_ALU | BPF_NEG] = true,
1014 /* Load instructions */
1015 [BPF_LD | BPF_W | BPF_ABS] = true,
1016 [BPF_LD | BPF_H | BPF_ABS] = true,
1017 [BPF_LD | BPF_B | BPF_ABS] = true,
1018 [BPF_LD | BPF_W | BPF_LEN] = true,
1019 [BPF_LD | BPF_W | BPF_IND] = true,
1020 [BPF_LD | BPF_H | BPF_IND] = true,
1021 [BPF_LD | BPF_B | BPF_IND] = true,
1022 [BPF_LD | BPF_IMM] = true,
1023 [BPF_LD | BPF_MEM] = true,
1024 [BPF_LDX | BPF_W | BPF_LEN] = true,
1025 [BPF_LDX | BPF_B | BPF_MSH] = true,
1026 [BPF_LDX | BPF_IMM] = true,
1027 [BPF_LDX | BPF_MEM] = true,
1028 /* Store instructions */
1029 [BPF_ST] = true,
1030 [BPF_STX] = true,
1031 /* Misc instructions */
1032 [BPF_MISC | BPF_TAX] = true,
1033 [BPF_MISC | BPF_TXA] = true,
1034 /* Return instructions */
1035 [BPF_RET | BPF_K] = true,
1036 [BPF_RET | BPF_A] = true,
1037 /* Jump instructions */
1038 [BPF_JMP | BPF_JA] = true,
1039 [BPF_JMP | BPF_JEQ | BPF_K] = true,
1040 [BPF_JMP | BPF_JEQ | BPF_X] = true,
1041 [BPF_JMP | BPF_JGE | BPF_K] = true,
1042 [BPF_JMP | BPF_JGE | BPF_X] = true,
1043 [BPF_JMP | BPF_JGT | BPF_K] = true,
1044 [BPF_JMP | BPF_JGT | BPF_X] = true,
1045 [BPF_JMP | BPF_JSET | BPF_K] = true,
1046 [BPF_JMP | BPF_JSET | BPF_X] = true,
1047 };
1048
1049 if (code_to_probe >= ARRAY_SIZE(codes))
1050 return false;
1051
1052 return codes[code_to_probe];
1053 }
1054
bpf_check_basics_ok(const struct sock_filter * filter,unsigned int flen)1055 static bool bpf_check_basics_ok(const struct sock_filter *filter,
1056 unsigned int flen)
1057 {
1058 if (filter == NULL)
1059 return false;
1060 if (flen == 0 || flen > BPF_MAXINSNS)
1061 return false;
1062
1063 return true;
1064 }
1065
1066 /**
1067 * bpf_check_classic - verify socket filter code
1068 * @filter: filter to verify
1069 * @flen: length of filter
1070 *
1071 * Check the user's filter code. If we let some ugly
1072 * filter code slip through kaboom! The filter must contain
1073 * no references or jumps that are out of range, no illegal
1074 * instructions, and must end with a RET instruction.
1075 *
1076 * All jumps are forward as they are not signed.
1077 *
1078 * Returns 0 if the rule set is legal or -EINVAL if not.
1079 */
bpf_check_classic(const struct sock_filter * filter,unsigned int flen)1080 static int bpf_check_classic(const struct sock_filter *filter,
1081 unsigned int flen)
1082 {
1083 bool anc_found;
1084 int pc;
1085
1086 /* Check the filter code now */
1087 for (pc = 0; pc < flen; pc++) {
1088 const struct sock_filter *ftest = &filter[pc];
1089
1090 /* May we actually operate on this code? */
1091 if (!chk_code_allowed(ftest->code))
1092 return -EINVAL;
1093
1094 /* Some instructions need special checks */
1095 switch (ftest->code) {
1096 case BPF_ALU | BPF_DIV | BPF_K:
1097 case BPF_ALU | BPF_MOD | BPF_K:
1098 /* Check for division by zero */
1099 if (ftest->k == 0)
1100 return -EINVAL;
1101 break;
1102 case BPF_ALU | BPF_LSH | BPF_K:
1103 case BPF_ALU | BPF_RSH | BPF_K:
1104 if (ftest->k >= 32)
1105 return -EINVAL;
1106 break;
1107 case BPF_LD | BPF_MEM:
1108 case BPF_LDX | BPF_MEM:
1109 case BPF_ST:
1110 case BPF_STX:
1111 /* Check for invalid memory addresses */
1112 if (ftest->k >= BPF_MEMWORDS)
1113 return -EINVAL;
1114 break;
1115 case BPF_JMP | BPF_JA:
1116 /* Note, the large ftest->k might cause loops.
1117 * Compare this with conditional jumps below,
1118 * where offsets are limited. --ANK (981016)
1119 */
1120 if (ftest->k >= (unsigned int)(flen - pc - 1))
1121 return -EINVAL;
1122 break;
1123 case BPF_JMP | BPF_JEQ | BPF_K:
1124 case BPF_JMP | BPF_JEQ | BPF_X:
1125 case BPF_JMP | BPF_JGE | BPF_K:
1126 case BPF_JMP | BPF_JGE | BPF_X:
1127 case BPF_JMP | BPF_JGT | BPF_K:
1128 case BPF_JMP | BPF_JGT | BPF_X:
1129 case BPF_JMP | BPF_JSET | BPF_K:
1130 case BPF_JMP | BPF_JSET | BPF_X:
1131 /* Both conditionals must be safe */
1132 if (pc + ftest->jt + 1 >= flen ||
1133 pc + ftest->jf + 1 >= flen)
1134 return -EINVAL;
1135 break;
1136 case BPF_LD | BPF_W | BPF_ABS:
1137 case BPF_LD | BPF_H | BPF_ABS:
1138 case BPF_LD | BPF_B | BPF_ABS:
1139 anc_found = false;
1140 if (bpf_anc_helper(ftest) & BPF_ANC)
1141 anc_found = true;
1142 /* Ancillary operation unknown or unsupported */
1143 if (anc_found == false && ftest->k >= SKF_AD_OFF)
1144 return -EINVAL;
1145 }
1146 }
1147
1148 /* Last instruction must be a RET code */
1149 switch (filter[flen - 1].code) {
1150 case BPF_RET | BPF_K:
1151 case BPF_RET | BPF_A:
1152 return check_load_and_stores(filter, flen);
1153 }
1154
1155 return -EINVAL;
1156 }
1157
bpf_prog_store_orig_filter(struct bpf_prog * fp,const struct sock_fprog * fprog)1158 static int bpf_prog_store_orig_filter(struct bpf_prog *fp,
1159 const struct sock_fprog *fprog)
1160 {
1161 unsigned int fsize = bpf_classic_proglen(fprog);
1162 struct sock_fprog_kern *fkprog;
1163
1164 fp->orig_prog = kmalloc_obj(*fkprog);
1165 if (!fp->orig_prog)
1166 return -ENOMEM;
1167
1168 fkprog = fp->orig_prog;
1169 fkprog->len = fprog->len;
1170
1171 fkprog->filter = kmemdup(fp->insns, fsize,
1172 GFP_KERNEL | __GFP_NOWARN);
1173 if (!fkprog->filter) {
1174 kfree(fp->orig_prog);
1175 return -ENOMEM;
1176 }
1177
1178 return 0;
1179 }
1180
bpf_release_orig_filter(struct bpf_prog * fp)1181 static void bpf_release_orig_filter(struct bpf_prog *fp)
1182 {
1183 struct sock_fprog_kern *fprog = fp->orig_prog;
1184
1185 if (fprog) {
1186 kfree(fprog->filter);
1187 kfree(fprog);
1188 }
1189 }
1190
__bpf_prog_release(struct bpf_prog * prog)1191 static void __bpf_prog_release(struct bpf_prog *prog)
1192 {
1193 if (prog->type == BPF_PROG_TYPE_SOCKET_FILTER) {
1194 bpf_prog_put(prog);
1195 } else {
1196 bpf_release_orig_filter(prog);
1197 bpf_prog_free(prog);
1198 }
1199 }
1200
__sk_filter_release(struct sk_filter * fp)1201 static void __sk_filter_release(struct sk_filter *fp)
1202 {
1203 __bpf_prog_release(fp->prog);
1204 kfree(fp);
1205 }
1206
1207 /**
1208 * sk_filter_release_rcu - Release a socket filter by rcu_head
1209 * @rcu: rcu_head that contains the sk_filter to free
1210 */
sk_filter_release_rcu(struct rcu_head * rcu)1211 static void sk_filter_release_rcu(struct rcu_head *rcu)
1212 {
1213 struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu);
1214
1215 __sk_filter_release(fp);
1216 }
1217
1218 /**
1219 * sk_filter_release - release a socket filter
1220 * @fp: filter to remove
1221 *
1222 * Remove a filter from a socket and release its resources.
1223 */
sk_filter_release(struct sk_filter * fp)1224 static void sk_filter_release(struct sk_filter *fp)
1225 {
1226 if (refcount_dec_and_test(&fp->refcnt))
1227 call_rcu(&fp->rcu, sk_filter_release_rcu);
1228 }
1229
sk_filter_uncharge(struct sock * sk,struct sk_filter * fp)1230 void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp)
1231 {
1232 u32 filter_size = bpf_prog_size(fp->prog->len);
1233
1234 atomic_sub(filter_size, &sk->sk_omem_alloc);
1235 sk_filter_release(fp);
1236 }
1237
1238 /* try to charge the socket memory if there is space available
1239 * return true on success
1240 */
__sk_filter_charge(struct sock * sk,struct sk_filter * fp)1241 static bool __sk_filter_charge(struct sock *sk, struct sk_filter *fp)
1242 {
1243 int optmem_max = READ_ONCE(sock_net(sk)->core.sysctl_optmem_max);
1244 u32 filter_size = bpf_prog_size(fp->prog->len);
1245
1246 /* same check as in sock_kmalloc() */
1247 if (filter_size <= optmem_max &&
1248 atomic_read(&sk->sk_omem_alloc) + filter_size < optmem_max) {
1249 atomic_add(filter_size, &sk->sk_omem_alloc);
1250 return true;
1251 }
1252 return false;
1253 }
1254
sk_filter_charge(struct sock * sk,struct sk_filter * fp)1255 bool sk_filter_charge(struct sock *sk, struct sk_filter *fp)
1256 {
1257 if (!refcount_inc_not_zero(&fp->refcnt))
1258 return false;
1259
1260 if (!__sk_filter_charge(sk, fp)) {
1261 sk_filter_release(fp);
1262 return false;
1263 }
1264 return true;
1265 }
1266
bpf_migrate_filter(struct bpf_prog * fp)1267 static struct bpf_prog *bpf_migrate_filter(struct bpf_prog *fp)
1268 {
1269 struct sock_filter *old_prog;
1270 struct bpf_prog *old_fp;
1271 int err, new_len, old_len = fp->len;
1272 bool seen_ld_abs = false;
1273
1274 /* We are free to overwrite insns et al right here as it won't be used at
1275 * this point in time anymore internally after the migration to the eBPF
1276 * instruction representation.
1277 */
1278 BUILD_BUG_ON(sizeof(struct sock_filter) !=
1279 sizeof(struct bpf_insn));
1280
1281 /* Conversion cannot happen on overlapping memory areas,
1282 * so we need to keep the user BPF around until the 2nd
1283 * pass. At this time, the user BPF is stored in fp->insns.
1284 */
1285 old_prog = kmemdup_array(fp->insns, old_len, sizeof(struct sock_filter),
1286 GFP_KERNEL | __GFP_NOWARN);
1287 if (!old_prog) {
1288 err = -ENOMEM;
1289 goto out_err;
1290 }
1291
1292 /* 1st pass: calculate the new program length. */
1293 err = bpf_convert_filter(old_prog, old_len, NULL, &new_len,
1294 &seen_ld_abs);
1295 if (err)
1296 goto out_err_free;
1297
1298 /* Expand fp for appending the new filter representation. */
1299 old_fp = fp;
1300 fp = bpf_prog_realloc(old_fp, bpf_prog_size(new_len), 0);
1301 if (!fp) {
1302 /* The old_fp is still around in case we couldn't
1303 * allocate new memory, so uncharge on that one.
1304 */
1305 fp = old_fp;
1306 err = -ENOMEM;
1307 goto out_err_free;
1308 }
1309
1310 fp->len = new_len;
1311
1312 /* 2nd pass: remap sock_filter insns into bpf_insn insns. */
1313 err = bpf_convert_filter(old_prog, old_len, fp, &new_len,
1314 &seen_ld_abs);
1315 if (err)
1316 /* 2nd bpf_convert_filter() can fail only if it fails
1317 * to allocate memory, remapping must succeed. Note,
1318 * that at this time old_fp has already been released
1319 * by krealloc().
1320 */
1321 goto out_err_free;
1322
1323 fp = bpf_prog_select_runtime(fp, &err);
1324 if (err)
1325 goto out_err_free;
1326
1327 kfree(old_prog);
1328 return fp;
1329
1330 out_err_free:
1331 kfree(old_prog);
1332 out_err:
1333 __bpf_prog_release(fp);
1334 return ERR_PTR(err);
1335 }
1336
bpf_prepare_filter(struct bpf_prog * fp,bpf_aux_classic_check_t trans)1337 static struct bpf_prog *bpf_prepare_filter(struct bpf_prog *fp,
1338 bpf_aux_classic_check_t trans)
1339 {
1340 int err;
1341
1342 fp->bpf_func = NULL;
1343 fp->jited = 0;
1344
1345 err = bpf_check_classic(fp->insns, fp->len);
1346 if (err) {
1347 __bpf_prog_release(fp);
1348 return ERR_PTR(err);
1349 }
1350
1351 /* There might be additional checks and transformations
1352 * needed on classic filters, f.e. in case of seccomp.
1353 */
1354 if (trans) {
1355 err = trans(fp->insns, fp->len);
1356 if (err) {
1357 __bpf_prog_release(fp);
1358 return ERR_PTR(err);
1359 }
1360 }
1361
1362 /* Probe if we can JIT compile the filter and if so, do
1363 * the compilation of the filter.
1364 */
1365 bpf_jit_compile(fp);
1366
1367 /* JIT compiler couldn't process this filter, so do the eBPF translation
1368 * for the optimized interpreter.
1369 */
1370 if (!fp->jited)
1371 fp = bpf_migrate_filter(fp);
1372
1373 return fp;
1374 }
1375
1376 /**
1377 * bpf_prog_create - create an unattached filter
1378 * @pfp: the unattached filter that is created
1379 * @fprog: the filter program
1380 *
1381 * Create a filter independent of any socket. We first run some
1382 * sanity checks on it to make sure it does not explode on us later.
1383 * If an error occurs or there is insufficient memory for the filter
1384 * a negative errno code is returned. On success the return is zero.
1385 */
bpf_prog_create(struct bpf_prog ** pfp,struct sock_fprog_kern * fprog)1386 int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog)
1387 {
1388 unsigned int fsize = bpf_classic_proglen(fprog);
1389 struct bpf_prog *fp;
1390
1391 /* Make sure new filter is there and in the right amounts. */
1392 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1393 return -EINVAL;
1394
1395 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1396 if (!fp)
1397 return -ENOMEM;
1398
1399 memcpy(fp->insns, fprog->filter, fsize);
1400
1401 fp->len = fprog->len;
1402 /* Since unattached filters are not copied back to user
1403 * space through sk_get_filter(), we do not need to hold
1404 * a copy here, and can spare us the work.
1405 */
1406 fp->orig_prog = NULL;
1407
1408 /* bpf_prepare_filter() already takes care of freeing
1409 * memory in case something goes wrong.
1410 */
1411 fp = bpf_prepare_filter(fp, NULL);
1412 if (IS_ERR(fp))
1413 return PTR_ERR(fp);
1414
1415 *pfp = fp;
1416 return 0;
1417 }
1418 EXPORT_SYMBOL_GPL(bpf_prog_create);
1419
1420 /**
1421 * bpf_prog_create_from_user - create an unattached filter from user buffer
1422 * @pfp: the unattached filter that is created
1423 * @fprog: the filter program
1424 * @trans: post-classic verifier transformation handler
1425 * @save_orig: save classic BPF program
1426 *
1427 * This function effectively does the same as bpf_prog_create(), only
1428 * that it builds up its insns buffer from user space provided buffer.
1429 * It also allows for passing a bpf_aux_classic_check_t handler.
1430 */
bpf_prog_create_from_user(struct bpf_prog ** pfp,struct sock_fprog * fprog,bpf_aux_classic_check_t trans,bool save_orig)1431 int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog,
1432 bpf_aux_classic_check_t trans, bool save_orig)
1433 {
1434 unsigned int fsize = bpf_classic_proglen(fprog);
1435 struct bpf_prog *fp;
1436 int err;
1437
1438 /* Make sure new filter is there and in the right amounts. */
1439 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1440 return -EINVAL;
1441
1442 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1443 if (!fp)
1444 return -ENOMEM;
1445
1446 if (copy_from_user(fp->insns, fprog->filter, fsize)) {
1447 __bpf_prog_free(fp);
1448 return -EFAULT;
1449 }
1450
1451 fp->len = fprog->len;
1452 fp->orig_prog = NULL;
1453
1454 if (save_orig) {
1455 err = bpf_prog_store_orig_filter(fp, fprog);
1456 if (err) {
1457 __bpf_prog_free(fp);
1458 return -ENOMEM;
1459 }
1460 }
1461
1462 /* bpf_prepare_filter() already takes care of freeing
1463 * memory in case something goes wrong.
1464 */
1465 fp = bpf_prepare_filter(fp, trans);
1466 if (IS_ERR(fp))
1467 return PTR_ERR(fp);
1468
1469 *pfp = fp;
1470 return 0;
1471 }
1472 EXPORT_SYMBOL_GPL(bpf_prog_create_from_user);
1473
bpf_prog_destroy(struct bpf_prog * fp)1474 void bpf_prog_destroy(struct bpf_prog *fp)
1475 {
1476 __bpf_prog_release(fp);
1477 }
1478 EXPORT_SYMBOL_GPL(bpf_prog_destroy);
1479
__sk_attach_prog(struct bpf_prog * prog,struct sock * sk)1480 static int __sk_attach_prog(struct bpf_prog *prog, struct sock *sk)
1481 {
1482 struct sk_filter *fp, *old_fp;
1483
1484 fp = kmalloc_obj(*fp);
1485 if (!fp)
1486 return -ENOMEM;
1487
1488 fp->prog = prog;
1489
1490 if (!__sk_filter_charge(sk, fp)) {
1491 kfree(fp);
1492 return -ENOMEM;
1493 }
1494 refcount_set(&fp->refcnt, 1);
1495
1496 old_fp = rcu_dereference_protected(sk->sk_filter,
1497 lockdep_sock_is_held(sk));
1498 rcu_assign_pointer(sk->sk_filter, fp);
1499
1500 if (old_fp)
1501 sk_filter_uncharge(sk, old_fp);
1502
1503 return 0;
1504 }
1505
1506 static
__get_filter(struct sock_fprog * fprog,struct sock * sk)1507 struct bpf_prog *__get_filter(struct sock_fprog *fprog, struct sock *sk)
1508 {
1509 unsigned int fsize = bpf_classic_proglen(fprog);
1510 struct bpf_prog *prog;
1511 int err;
1512
1513 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1514 return ERR_PTR(-EPERM);
1515
1516 /* Make sure new filter is there and in the right amounts. */
1517 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1518 return ERR_PTR(-EINVAL);
1519
1520 prog = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1521 if (!prog)
1522 return ERR_PTR(-ENOMEM);
1523
1524 if (copy_from_user(prog->insns, fprog->filter, fsize)) {
1525 __bpf_prog_free(prog);
1526 return ERR_PTR(-EFAULT);
1527 }
1528
1529 prog->len = fprog->len;
1530
1531 err = bpf_prog_store_orig_filter(prog, fprog);
1532 if (err) {
1533 __bpf_prog_free(prog);
1534 return ERR_PTR(-ENOMEM);
1535 }
1536
1537 /* bpf_prepare_filter() already takes care of freeing
1538 * memory in case something goes wrong.
1539 */
1540 return bpf_prepare_filter(prog, NULL);
1541 }
1542
1543 /**
1544 * sk_attach_filter - attach a socket filter
1545 * @fprog: the filter program
1546 * @sk: the socket to use
1547 *
1548 * Attach the user's filter code. We first run some sanity checks on
1549 * it to make sure it does not explode on us later. If an error
1550 * occurs or there is insufficient memory for the filter a negative
1551 * errno code is returned. On success the return is zero.
1552 */
sk_attach_filter(struct sock_fprog * fprog,struct sock * sk)1553 int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1554 {
1555 struct bpf_prog *prog = __get_filter(fprog, sk);
1556 int err;
1557
1558 if (IS_ERR(prog))
1559 return PTR_ERR(prog);
1560
1561 err = __sk_attach_prog(prog, sk);
1562 if (err < 0) {
1563 __bpf_prog_release(prog);
1564 return err;
1565 }
1566
1567 return 0;
1568 }
1569 EXPORT_SYMBOL_GPL(sk_attach_filter);
1570
sk_reuseport_attach_filter(struct sock_fprog * fprog,struct sock * sk)1571 int sk_reuseport_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1572 {
1573 struct bpf_prog *prog = __get_filter(fprog, sk);
1574 int err, optmem_max;
1575
1576 if (IS_ERR(prog))
1577 return PTR_ERR(prog);
1578
1579 optmem_max = READ_ONCE(sock_net(sk)->core.sysctl_optmem_max);
1580 if (bpf_prog_size(prog->len) > optmem_max)
1581 err = -ENOMEM;
1582 else
1583 err = reuseport_attach_prog(sk, prog);
1584
1585 if (err)
1586 __bpf_prog_release(prog);
1587
1588 return err;
1589 }
1590
__get_bpf(u32 ufd,struct sock * sk)1591 static struct bpf_prog *__get_bpf(u32 ufd, struct sock *sk)
1592 {
1593 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1594 return ERR_PTR(-EPERM);
1595
1596 return bpf_prog_get_type(ufd, BPF_PROG_TYPE_SOCKET_FILTER);
1597 }
1598
sk_attach_bpf(u32 ufd,struct sock * sk)1599 int sk_attach_bpf(u32 ufd, struct sock *sk)
1600 {
1601 struct bpf_prog *prog = __get_bpf(ufd, sk);
1602 int err;
1603
1604 if (IS_ERR(prog))
1605 return PTR_ERR(prog);
1606
1607 err = __sk_attach_prog(prog, sk);
1608 if (err < 0) {
1609 bpf_prog_put(prog);
1610 return err;
1611 }
1612
1613 return 0;
1614 }
1615
sk_reuseport_attach_bpf(u32 ufd,struct sock * sk)1616 int sk_reuseport_attach_bpf(u32 ufd, struct sock *sk)
1617 {
1618 struct bpf_prog *prog;
1619 int err, optmem_max;
1620
1621 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1622 return -EPERM;
1623
1624 prog = bpf_prog_get_type(ufd, BPF_PROG_TYPE_SOCKET_FILTER);
1625 if (PTR_ERR(prog) == -EINVAL)
1626 prog = bpf_prog_get_type(ufd, BPF_PROG_TYPE_SK_REUSEPORT);
1627 if (IS_ERR(prog))
1628 return PTR_ERR(prog);
1629
1630 if (prog->type == BPF_PROG_TYPE_SK_REUSEPORT) {
1631 /* Like other non BPF_PROG_TYPE_SOCKET_FILTER
1632 * bpf prog (e.g. sockmap). It depends on the
1633 * limitation imposed by bpf_prog_load().
1634 * Hence, sysctl_optmem_max is not checked.
1635 */
1636 if ((sk->sk_type != SOCK_STREAM &&
1637 sk->sk_type != SOCK_DGRAM) ||
1638 (sk->sk_protocol != IPPROTO_UDP &&
1639 sk->sk_protocol != IPPROTO_TCP) ||
1640 (sk->sk_family != AF_INET &&
1641 sk->sk_family != AF_INET6)) {
1642 err = -ENOTSUPP;
1643 goto err_prog_put;
1644 }
1645 } else {
1646 /* BPF_PROG_TYPE_SOCKET_FILTER */
1647 optmem_max = READ_ONCE(sock_net(sk)->core.sysctl_optmem_max);
1648 if (bpf_prog_size(prog->len) > optmem_max) {
1649 err = -ENOMEM;
1650 goto err_prog_put;
1651 }
1652 }
1653
1654 err = reuseport_attach_prog(sk, prog);
1655 err_prog_put:
1656 if (err)
1657 bpf_prog_put(prog);
1658
1659 return err;
1660 }
1661
sk_reuseport_prog_free(struct bpf_prog * prog)1662 void sk_reuseport_prog_free(struct bpf_prog *prog)
1663 {
1664 if (!prog)
1665 return;
1666
1667 if (prog->type == BPF_PROG_TYPE_SK_REUSEPORT)
1668 bpf_prog_put(prog);
1669 else
1670 bpf_prog_destroy(prog);
1671 }
1672
__bpf_try_make_writable(struct sk_buff * skb,unsigned int write_len)1673 static inline int __bpf_try_make_writable(struct sk_buff *skb,
1674 unsigned int write_len)
1675 {
1676 #ifdef CONFIG_DEBUG_NET
1677 /* Avoid a splat in pskb_may_pull_reason() */
1678 if (write_len > INT_MAX)
1679 return -EINVAL;
1680 #endif
1681 return skb_ensure_writable(skb, write_len);
1682 }
1683
bpf_try_make_writable(struct sk_buff * skb,unsigned int write_len)1684 static inline int bpf_try_make_writable(struct sk_buff *skb,
1685 unsigned int write_len)
1686 {
1687 int err = __bpf_try_make_writable(skb, write_len);
1688
1689 bpf_compute_data_pointers(skb);
1690 return err;
1691 }
1692
bpf_try_make_head_writable(struct sk_buff * skb)1693 static int bpf_try_make_head_writable(struct sk_buff *skb)
1694 {
1695 return bpf_try_make_writable(skb, skb_headlen(skb));
1696 }
1697
bpf_push_mac_rcsum(struct sk_buff * skb)1698 static inline void bpf_push_mac_rcsum(struct sk_buff *skb)
1699 {
1700 if (skb_at_tc_ingress(skb))
1701 skb_postpush_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1702 }
1703
bpf_pull_mac_rcsum(struct sk_buff * skb)1704 static inline void bpf_pull_mac_rcsum(struct sk_buff *skb)
1705 {
1706 if (skb_at_tc_ingress(skb))
1707 skb_postpull_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1708 }
1709
BPF_CALL_5(bpf_skb_store_bytes,struct sk_buff *,skb,u32,offset,const void *,from,u32,len,u64,flags)1710 BPF_CALL_5(bpf_skb_store_bytes, struct sk_buff *, skb, u32, offset,
1711 const void *, from, u32, len, u64, flags)
1712 {
1713 void *ptr;
1714
1715 if (unlikely(flags & ~(BPF_F_RECOMPUTE_CSUM | BPF_F_INVALIDATE_HASH)))
1716 return -EINVAL;
1717 if (unlikely(offset > INT_MAX))
1718 return -EFAULT;
1719 if (unlikely(bpf_try_make_writable(skb, offset + len)))
1720 return -EFAULT;
1721
1722 ptr = skb->data + offset;
1723 if (flags & BPF_F_RECOMPUTE_CSUM)
1724 __skb_postpull_rcsum(skb, ptr, len, offset);
1725
1726 memcpy(ptr, from, len);
1727
1728 if (flags & BPF_F_RECOMPUTE_CSUM)
1729 __skb_postpush_rcsum(skb, ptr, len, offset);
1730 if (flags & BPF_F_INVALIDATE_HASH)
1731 skb_clear_hash(skb);
1732
1733 return 0;
1734 }
1735
1736 static const struct bpf_func_proto bpf_skb_store_bytes_proto = {
1737 .func = bpf_skb_store_bytes,
1738 .gpl_only = false,
1739 .ret_type = RET_INTEGER,
1740 .arg1_type = ARG_PTR_TO_CTX,
1741 .arg2_type = ARG_ANYTHING,
1742 .arg3_type = ARG_PTR_TO_MEM | MEM_RDONLY,
1743 .arg4_type = ARG_CONST_SIZE,
1744 .arg5_type = ARG_ANYTHING,
1745 };
1746
__bpf_skb_store_bytes(struct sk_buff * skb,u32 offset,const void * from,u32 len,u64 flags)1747 int __bpf_skb_store_bytes(struct sk_buff *skb, u32 offset, const void *from,
1748 u32 len, u64 flags)
1749 {
1750 return ____bpf_skb_store_bytes(skb, offset, from, len, flags);
1751 }
1752
BPF_CALL_4(bpf_skb_load_bytes,const struct sk_buff *,skb,u32,offset,void *,to,u32,len)1753 BPF_CALL_4(bpf_skb_load_bytes, const struct sk_buff *, skb, u32, offset,
1754 void *, to, u32, len)
1755 {
1756 void *ptr;
1757
1758 if (unlikely(offset > INT_MAX))
1759 goto err_clear;
1760
1761 ptr = skb_header_pointer(skb, offset, len, to);
1762 if (unlikely(!ptr))
1763 goto err_clear;
1764 if (ptr != to)
1765 memcpy(to, ptr, len);
1766
1767 return 0;
1768 err_clear:
1769 memset(to, 0, len);
1770 return -EFAULT;
1771 }
1772
1773 static const struct bpf_func_proto bpf_skb_load_bytes_proto = {
1774 .func = bpf_skb_load_bytes,
1775 .gpl_only = false,
1776 .ret_type = RET_INTEGER,
1777 .arg1_type = ARG_PTR_TO_CTX,
1778 .arg2_type = ARG_ANYTHING,
1779 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
1780 .arg4_type = ARG_CONST_SIZE,
1781 };
1782
__bpf_skb_load_bytes(const struct sk_buff * skb,u32 offset,void * to,u32 len)1783 int __bpf_skb_load_bytes(const struct sk_buff *skb, u32 offset, void *to, u32 len)
1784 {
1785 return ____bpf_skb_load_bytes(skb, offset, to, len);
1786 }
1787
BPF_CALL_4(bpf_flow_dissector_load_bytes,const struct bpf_flow_dissector *,ctx,u32,offset,void *,to,u32,len)1788 BPF_CALL_4(bpf_flow_dissector_load_bytes,
1789 const struct bpf_flow_dissector *, ctx, u32, offset,
1790 void *, to, u32, len)
1791 {
1792 void *ptr;
1793
1794 if (unlikely(offset > 0xffff))
1795 goto err_clear;
1796
1797 if (unlikely(!ctx->skb))
1798 goto err_clear;
1799
1800 ptr = skb_header_pointer(ctx->skb, offset, len, to);
1801 if (unlikely(!ptr))
1802 goto err_clear;
1803 if (ptr != to)
1804 memcpy(to, ptr, len);
1805
1806 return 0;
1807 err_clear:
1808 memset(to, 0, len);
1809 return -EFAULT;
1810 }
1811
1812 static const struct bpf_func_proto bpf_flow_dissector_load_bytes_proto = {
1813 .func = bpf_flow_dissector_load_bytes,
1814 .gpl_only = false,
1815 .ret_type = RET_INTEGER,
1816 .arg1_type = ARG_PTR_TO_CTX,
1817 .arg2_type = ARG_ANYTHING,
1818 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
1819 .arg4_type = ARG_CONST_SIZE,
1820 };
1821
BPF_CALL_5(bpf_skb_load_bytes_relative,const struct sk_buff *,skb,u32,offset,void *,to,u32,len,u32,start_header)1822 BPF_CALL_5(bpf_skb_load_bytes_relative, const struct sk_buff *, skb,
1823 u32, offset, void *, to, u32, len, u32, start_header)
1824 {
1825 u8 *end = skb_tail_pointer(skb);
1826 u8 *start, *ptr;
1827
1828 if (unlikely(offset > 0xffff))
1829 goto err_clear;
1830
1831 switch (start_header) {
1832 case BPF_HDR_START_MAC:
1833 if (unlikely(!skb_mac_header_was_set(skb)))
1834 goto err_clear;
1835 start = skb_mac_header(skb);
1836 break;
1837 case BPF_HDR_START_NET:
1838 start = skb_network_header(skb);
1839 break;
1840 default:
1841 goto err_clear;
1842 }
1843
1844 ptr = start + offset;
1845
1846 if (likely(ptr + len <= end)) {
1847 memcpy(to, ptr, len);
1848 return 0;
1849 }
1850
1851 err_clear:
1852 memset(to, 0, len);
1853 return -EFAULT;
1854 }
1855
1856 static const struct bpf_func_proto bpf_skb_load_bytes_relative_proto = {
1857 .func = bpf_skb_load_bytes_relative,
1858 .gpl_only = false,
1859 .ret_type = RET_INTEGER,
1860 .arg1_type = ARG_PTR_TO_CTX,
1861 .arg2_type = ARG_ANYTHING,
1862 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
1863 .arg4_type = ARG_CONST_SIZE,
1864 .arg5_type = ARG_ANYTHING,
1865 };
1866
BPF_CALL_2(bpf_skb_pull_data,struct sk_buff *,skb,u32,len)1867 BPF_CALL_2(bpf_skb_pull_data, struct sk_buff *, skb, u32, len)
1868 {
1869 /* Idea is the following: should the needed direct read/write
1870 * test fail during runtime, we can pull in more data and redo
1871 * again, since implicitly, we invalidate previous checks here.
1872 *
1873 * Or, since we know how much we need to make read/writeable,
1874 * this can be done once at the program beginning for direct
1875 * access case. By this we overcome limitations of only current
1876 * headroom being accessible.
1877 */
1878 return bpf_try_make_writable(skb, len ? : skb_headlen(skb));
1879 }
1880
1881 static const struct bpf_func_proto bpf_skb_pull_data_proto = {
1882 .func = bpf_skb_pull_data,
1883 .gpl_only = false,
1884 .ret_type = RET_INTEGER,
1885 .arg1_type = ARG_PTR_TO_CTX,
1886 .arg2_type = ARG_ANYTHING,
1887 };
1888
BPF_CALL_1(bpf_sk_fullsock,struct sock *,sk)1889 BPF_CALL_1(bpf_sk_fullsock, struct sock *, sk)
1890 {
1891 return sk_fullsock(sk) ? (unsigned long)sk : (unsigned long)NULL;
1892 }
1893
1894 static const struct bpf_func_proto bpf_sk_fullsock_proto = {
1895 .func = bpf_sk_fullsock,
1896 .gpl_only = false,
1897 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
1898 .arg1_type = ARG_PTR_TO_SOCK_COMMON,
1899 };
1900
sk_skb_try_make_writable(struct sk_buff * skb,unsigned int write_len)1901 static inline int sk_skb_try_make_writable(struct sk_buff *skb,
1902 unsigned int write_len)
1903 {
1904 return __bpf_try_make_writable(skb, write_len);
1905 }
1906
BPF_CALL_2(sk_skb_pull_data,struct sk_buff *,skb,u32,len)1907 BPF_CALL_2(sk_skb_pull_data, struct sk_buff *, skb, u32, len)
1908 {
1909 /* Idea is the following: should the needed direct read/write
1910 * test fail during runtime, we can pull in more data and redo
1911 * again, since implicitly, we invalidate previous checks here.
1912 *
1913 * Or, since we know how much we need to make read/writeable,
1914 * this can be done once at the program beginning for direct
1915 * access case. By this we overcome limitations of only current
1916 * headroom being accessible.
1917 */
1918 return sk_skb_try_make_writable(skb, len ? : skb_headlen(skb));
1919 }
1920
1921 static const struct bpf_func_proto sk_skb_pull_data_proto = {
1922 .func = sk_skb_pull_data,
1923 .gpl_only = false,
1924 .ret_type = RET_INTEGER,
1925 .arg1_type = ARG_PTR_TO_CTX,
1926 .arg2_type = ARG_ANYTHING,
1927 };
1928
BPF_CALL_5(bpf_l3_csum_replace,struct sk_buff *,skb,u32,offset,u64,from,u64,to,u64,flags)1929 BPF_CALL_5(bpf_l3_csum_replace, struct sk_buff *, skb, u32, offset,
1930 u64, from, u64, to, u64, flags)
1931 {
1932 __sum16 *ptr;
1933
1934 if (unlikely(flags & ~(BPF_F_HDR_FIELD_MASK)))
1935 return -EINVAL;
1936 if (unlikely(offset > 0xffff || offset & 1))
1937 return -EFAULT;
1938 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
1939 return -EFAULT;
1940
1941 ptr = (__sum16 *)(skb->data + offset);
1942 switch (flags & BPF_F_HDR_FIELD_MASK) {
1943 case 0:
1944 if (unlikely(from != 0))
1945 return -EINVAL;
1946
1947 csum_replace_by_diff(ptr, to);
1948 break;
1949 case 2:
1950 csum_replace2(ptr, from, to);
1951 break;
1952 case 4:
1953 csum_replace4(ptr, from, to);
1954 break;
1955 default:
1956 return -EINVAL;
1957 }
1958
1959 return 0;
1960 }
1961
1962 static const struct bpf_func_proto bpf_l3_csum_replace_proto = {
1963 .func = bpf_l3_csum_replace,
1964 .gpl_only = false,
1965 .ret_type = RET_INTEGER,
1966 .arg1_type = ARG_PTR_TO_CTX,
1967 .arg2_type = ARG_ANYTHING,
1968 .arg3_type = ARG_ANYTHING,
1969 .arg4_type = ARG_ANYTHING,
1970 .arg5_type = ARG_ANYTHING,
1971 };
1972
BPF_CALL_5(bpf_l4_csum_replace,struct sk_buff *,skb,u32,offset,u64,from,u64,to,u64,flags)1973 BPF_CALL_5(bpf_l4_csum_replace, struct sk_buff *, skb, u32, offset,
1974 u64, from, u64, to, u64, flags)
1975 {
1976 bool is_pseudo = flags & BPF_F_PSEUDO_HDR;
1977 bool is_mmzero = flags & BPF_F_MARK_MANGLED_0;
1978 bool do_mforce = flags & BPF_F_MARK_ENFORCE;
1979 bool is_ipv6 = flags & BPF_F_IPV6;
1980 __sum16 *ptr;
1981
1982 if (unlikely(flags & ~(BPF_F_MARK_MANGLED_0 | BPF_F_MARK_ENFORCE |
1983 BPF_F_PSEUDO_HDR | BPF_F_HDR_FIELD_MASK | BPF_F_IPV6)))
1984 return -EINVAL;
1985 if (unlikely(offset > 0xffff || offset & 1))
1986 return -EFAULT;
1987 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
1988 return -EFAULT;
1989
1990 ptr = (__sum16 *)(skb->data + offset);
1991 if (is_mmzero && !do_mforce && !*ptr)
1992 return 0;
1993
1994 switch (flags & BPF_F_HDR_FIELD_MASK) {
1995 case 0:
1996 if (unlikely(from != 0))
1997 return -EINVAL;
1998
1999 inet_proto_csum_replace_by_diff(ptr, skb, to, is_pseudo, is_ipv6);
2000 break;
2001 case 2:
2002 inet_proto_csum_replace2(ptr, skb, from, to, is_pseudo);
2003 break;
2004 case 4:
2005 inet_proto_csum_replace4(ptr, skb, from, to, is_pseudo);
2006 break;
2007 default:
2008 return -EINVAL;
2009 }
2010
2011 if (is_mmzero && !*ptr)
2012 *ptr = CSUM_MANGLED_0;
2013 return 0;
2014 }
2015
2016 static const struct bpf_func_proto bpf_l4_csum_replace_proto = {
2017 .func = bpf_l4_csum_replace,
2018 .gpl_only = false,
2019 .ret_type = RET_INTEGER,
2020 .arg1_type = ARG_PTR_TO_CTX,
2021 .arg2_type = ARG_ANYTHING,
2022 .arg3_type = ARG_ANYTHING,
2023 .arg4_type = ARG_ANYTHING,
2024 .arg5_type = ARG_ANYTHING,
2025 };
2026
BPF_CALL_5(bpf_csum_diff,__be32 *,from,u32,from_size,__be32 *,to,u32,to_size,__wsum,seed)2027 BPF_CALL_5(bpf_csum_diff, __be32 *, from, u32, from_size,
2028 __be32 *, to, u32, to_size, __wsum, seed)
2029 {
2030 /* This is quite flexible, some examples:
2031 *
2032 * from_size == 0, to_size > 0, seed := csum --> pushing data
2033 * from_size > 0, to_size == 0, seed := csum --> pulling data
2034 * from_size > 0, to_size > 0, seed := 0 --> diffing data
2035 *
2036 * Even for diffing, from_size and to_size don't need to be equal.
2037 */
2038
2039 __wsum ret = seed;
2040
2041 if (from_size && to_size)
2042 ret = csum_sub(csum_partial(to, to_size, ret),
2043 csum_partial(from, from_size, 0));
2044 else if (to_size)
2045 ret = csum_partial(to, to_size, ret);
2046
2047 else if (from_size)
2048 ret = ~csum_partial(from, from_size, ~ret);
2049
2050 return csum_from32to16((__force unsigned int)ret);
2051 }
2052
2053 static const struct bpf_func_proto bpf_csum_diff_proto = {
2054 .func = bpf_csum_diff,
2055 .gpl_only = false,
2056 .pkt_access = true,
2057 .ret_type = RET_INTEGER,
2058 .arg1_type = ARG_PTR_TO_MEM | PTR_MAYBE_NULL | MEM_RDONLY,
2059 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
2060 .arg3_type = ARG_PTR_TO_MEM | PTR_MAYBE_NULL | MEM_RDONLY,
2061 .arg4_type = ARG_CONST_SIZE_OR_ZERO,
2062 .arg5_type = ARG_ANYTHING,
2063 };
2064
BPF_CALL_2(bpf_csum_update,struct sk_buff *,skb,__wsum,csum)2065 BPF_CALL_2(bpf_csum_update, struct sk_buff *, skb, __wsum, csum)
2066 {
2067 /* The interface is to be used in combination with bpf_csum_diff()
2068 * for direct packet writes. csum rotation for alignment as well
2069 * as emulating csum_sub() can be done from the eBPF program.
2070 */
2071 if (skb->ip_summed == CHECKSUM_COMPLETE)
2072 return (skb->csum = csum_add(skb->csum, csum));
2073
2074 return -ENOTSUPP;
2075 }
2076
2077 static const struct bpf_func_proto bpf_csum_update_proto = {
2078 .func = bpf_csum_update,
2079 .gpl_only = false,
2080 .ret_type = RET_INTEGER,
2081 .arg1_type = ARG_PTR_TO_CTX,
2082 .arg2_type = ARG_ANYTHING,
2083 };
2084
BPF_CALL_2(bpf_csum_level,struct sk_buff *,skb,u64,level)2085 BPF_CALL_2(bpf_csum_level, struct sk_buff *, skb, u64, level)
2086 {
2087 /* The interface is to be used in combination with bpf_skb_adjust_room()
2088 * for encap/decap of packet headers when BPF_F_ADJ_ROOM_NO_CSUM_RESET
2089 * is passed as flags, for example.
2090 */
2091 switch (level) {
2092 case BPF_CSUM_LEVEL_INC:
2093 __skb_incr_checksum_unnecessary(skb);
2094 break;
2095 case BPF_CSUM_LEVEL_DEC:
2096 __skb_decr_checksum_unnecessary(skb);
2097 break;
2098 case BPF_CSUM_LEVEL_RESET:
2099 __skb_reset_checksum_unnecessary(skb);
2100 break;
2101 case BPF_CSUM_LEVEL_QUERY:
2102 return skb->ip_summed == CHECKSUM_UNNECESSARY ?
2103 skb->csum_level : -EACCES;
2104 default:
2105 return -EINVAL;
2106 }
2107
2108 return 0;
2109 }
2110
2111 static const struct bpf_func_proto bpf_csum_level_proto = {
2112 .func = bpf_csum_level,
2113 .gpl_only = false,
2114 .ret_type = RET_INTEGER,
2115 .arg1_type = ARG_PTR_TO_CTX,
2116 .arg2_type = ARG_ANYTHING,
2117 };
2118
__bpf_rx_skb(struct net_device * dev,struct sk_buff * skb)2119 static inline int __bpf_rx_skb(struct net_device *dev, struct sk_buff *skb)
2120 {
2121 return dev_forward_skb_nomtu(dev, skb);
2122 }
2123
__bpf_rx_skb_no_mac(struct net_device * dev,struct sk_buff * skb)2124 static inline int __bpf_rx_skb_no_mac(struct net_device *dev,
2125 struct sk_buff *skb)
2126 {
2127 int ret = ____dev_forward_skb(dev, skb, false);
2128
2129 if (likely(!ret)) {
2130 skb->dev = dev;
2131 ret = netif_rx(skb);
2132 }
2133
2134 return ret;
2135 }
2136
__bpf_tx_skb(struct net_device * dev,struct sk_buff * skb)2137 static inline int __bpf_tx_skb(struct net_device *dev, struct sk_buff *skb)
2138 {
2139 int ret;
2140
2141 if (dev_xmit_recursion()) {
2142 net_crit_ratelimited("bpf: recursion limit reached on datapath, buggy bpf program?\n");
2143 kfree_skb(skb);
2144 return -ENETDOWN;
2145 }
2146
2147 skb->dev = dev;
2148 skb_set_redirected_noclear(skb, skb_at_tc_ingress(skb));
2149 skb_clear_tstamp(skb);
2150
2151 dev_xmit_recursion_inc();
2152 ret = dev_queue_xmit(skb);
2153 dev_xmit_recursion_dec();
2154
2155 return ret;
2156 }
2157
__bpf_redirect_no_mac(struct sk_buff * skb,struct net_device * dev,u32 flags)2158 static int __bpf_redirect_no_mac(struct sk_buff *skb, struct net_device *dev,
2159 u32 flags)
2160 {
2161 unsigned int mlen = skb_network_offset(skb);
2162
2163 if (unlikely(skb->len <= mlen)) {
2164 kfree_skb(skb);
2165 return -ERANGE;
2166 }
2167
2168 if (mlen) {
2169 __skb_pull(skb, mlen);
2170
2171 /* At ingress, the mac header has already been pulled once.
2172 * At egress, skb_pospull_rcsum has to be done in case that
2173 * the skb is originated from ingress (i.e. a forwarded skb)
2174 * to ensure that rcsum starts at net header.
2175 */
2176 if (!skb_at_tc_ingress(skb))
2177 skb_postpull_rcsum(skb, skb_mac_header(skb), mlen);
2178 }
2179 skb_pop_mac_header(skb);
2180 skb_reset_mac_len(skb);
2181 return flags & BPF_F_INGRESS ?
2182 __bpf_rx_skb_no_mac(dev, skb) : __bpf_tx_skb(dev, skb);
2183 }
2184
__bpf_redirect_common(struct sk_buff * skb,struct net_device * dev,u32 flags)2185 static int __bpf_redirect_common(struct sk_buff *skb, struct net_device *dev,
2186 u32 flags)
2187 {
2188 /* Verify that a link layer header is carried */
2189 if (unlikely(skb->mac_header >= skb->network_header || skb->len == 0)) {
2190 kfree_skb(skb);
2191 return -ERANGE;
2192 }
2193
2194 bpf_push_mac_rcsum(skb);
2195 return flags & BPF_F_INGRESS ?
2196 __bpf_rx_skb(dev, skb) : __bpf_tx_skb(dev, skb);
2197 }
2198
__bpf_redirect(struct sk_buff * skb,struct net_device * dev,u32 flags)2199 static int __bpf_redirect(struct sk_buff *skb, struct net_device *dev,
2200 u32 flags)
2201 {
2202 if (dev_is_mac_header_xmit(dev))
2203 return __bpf_redirect_common(skb, dev, flags);
2204 else
2205 return __bpf_redirect_no_mac(skb, dev, flags);
2206 }
2207
2208 #if IS_ENABLED(CONFIG_IPV6)
bpf_out_neigh_v6(struct net * net,struct sk_buff * skb,struct net_device * dev,struct bpf_nh_params * nh)2209 static int bpf_out_neigh_v6(struct net *net, struct sk_buff *skb,
2210 struct net_device *dev, struct bpf_nh_params *nh)
2211 {
2212 u32 hh_len = LL_RESERVED_SPACE(dev);
2213 const struct in6_addr *nexthop;
2214 struct dst_entry *dst = NULL;
2215 struct neighbour *neigh;
2216
2217 if (dev_xmit_recursion()) {
2218 net_crit_ratelimited("bpf: recursion limit reached on datapath, buggy bpf program?\n");
2219 goto out_drop;
2220 }
2221
2222 skb->dev = dev;
2223 skb_clear_tstamp(skb);
2224
2225 if (unlikely(skb_headroom(skb) < hh_len && dev->header_ops)) {
2226 skb = skb_expand_head(skb, hh_len);
2227 if (!skb)
2228 return -ENOMEM;
2229 }
2230
2231 rcu_read_lock();
2232 if (!nh) {
2233 dst = skb_dst(skb);
2234 nexthop = rt6_nexthop(dst_rt6_info(dst),
2235 &ipv6_hdr(skb)->daddr);
2236 } else {
2237 nexthop = &nh->ipv6_nh;
2238 }
2239 neigh = ip_neigh_gw6(dev, nexthop);
2240 if (likely(!IS_ERR(neigh))) {
2241 int ret;
2242
2243 sock_confirm_neigh(skb, neigh);
2244 local_bh_disable();
2245 dev_xmit_recursion_inc();
2246 ret = neigh_output(neigh, skb, false);
2247 dev_xmit_recursion_dec();
2248 local_bh_enable();
2249 rcu_read_unlock();
2250 return ret;
2251 }
2252 rcu_read_unlock();
2253 if (dst)
2254 IP6_INC_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_OUTNOROUTES);
2255 out_drop:
2256 kfree_skb(skb);
2257 return -ENETDOWN;
2258 }
2259
__bpf_redirect_neigh_v6(struct sk_buff * skb,struct net_device * dev,struct bpf_nh_params * nh)2260 static int __bpf_redirect_neigh_v6(struct sk_buff *skb, struct net_device *dev,
2261 struct bpf_nh_params *nh)
2262 {
2263 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
2264 struct net *net = dev_net(dev);
2265 int err, ret = NET_XMIT_DROP;
2266
2267 if (!nh) {
2268 struct dst_entry *dst;
2269 struct flowi6 fl6 = {
2270 .flowi6_flags = FLOWI_FLAG_ANYSRC,
2271 .flowi6_mark = skb->mark,
2272 .flowlabel = ip6_flowinfo(ip6h),
2273 .flowi6_oif = dev->ifindex,
2274 .flowi6_proto = ip6h->nexthdr,
2275 .daddr = ip6h->daddr,
2276 .saddr = ip6h->saddr,
2277 };
2278
2279 dst = ipv6_stub->ipv6_dst_lookup_flow(net, NULL, &fl6, NULL);
2280 if (IS_ERR(dst))
2281 goto out_drop;
2282
2283 skb_dst_drop(skb);
2284 skb_dst_set(skb, dst);
2285 } else if (nh->nh_family != AF_INET6) {
2286 goto out_drop;
2287 }
2288
2289 err = bpf_out_neigh_v6(net, skb, dev, nh);
2290 if (unlikely(net_xmit_eval(err)))
2291 dev_core_stats_tx_dropped_inc(dev);
2292 else
2293 ret = NET_XMIT_SUCCESS;
2294 goto out_xmit;
2295 out_drop:
2296 dev_core_stats_tx_dropped_inc(dev);
2297 kfree_skb(skb);
2298 out_xmit:
2299 return ret;
2300 }
2301 #else
__bpf_redirect_neigh_v6(struct sk_buff * skb,struct net_device * dev,struct bpf_nh_params * nh)2302 static int __bpf_redirect_neigh_v6(struct sk_buff *skb, struct net_device *dev,
2303 struct bpf_nh_params *nh)
2304 {
2305 kfree_skb(skb);
2306 return NET_XMIT_DROP;
2307 }
2308 #endif /* CONFIG_IPV6 */
2309
2310 #if IS_ENABLED(CONFIG_INET)
bpf_out_neigh_v4(struct net * net,struct sk_buff * skb,struct net_device * dev,struct bpf_nh_params * nh)2311 static int bpf_out_neigh_v4(struct net *net, struct sk_buff *skb,
2312 struct net_device *dev, struct bpf_nh_params *nh)
2313 {
2314 u32 hh_len = LL_RESERVED_SPACE(dev);
2315 struct neighbour *neigh;
2316 bool is_v6gw = false;
2317
2318 if (dev_xmit_recursion()) {
2319 net_crit_ratelimited("bpf: recursion limit reached on datapath, buggy bpf program?\n");
2320 goto out_drop;
2321 }
2322
2323 skb->dev = dev;
2324 skb_clear_tstamp(skb);
2325
2326 if (unlikely(skb_headroom(skb) < hh_len && dev->header_ops)) {
2327 skb = skb_expand_head(skb, hh_len);
2328 if (!skb)
2329 return -ENOMEM;
2330 }
2331
2332 rcu_read_lock();
2333 if (!nh) {
2334 struct rtable *rt = skb_rtable(skb);
2335
2336 neigh = ip_neigh_for_gw(rt, skb, &is_v6gw);
2337 } else if (nh->nh_family == AF_INET6) {
2338 neigh = ip_neigh_gw6(dev, &nh->ipv6_nh);
2339 is_v6gw = true;
2340 } else if (nh->nh_family == AF_INET) {
2341 neigh = ip_neigh_gw4(dev, nh->ipv4_nh);
2342 } else {
2343 rcu_read_unlock();
2344 goto out_drop;
2345 }
2346
2347 if (likely(!IS_ERR(neigh))) {
2348 int ret;
2349
2350 sock_confirm_neigh(skb, neigh);
2351 local_bh_disable();
2352 dev_xmit_recursion_inc();
2353 ret = neigh_output(neigh, skb, is_v6gw);
2354 dev_xmit_recursion_dec();
2355 local_bh_enable();
2356 rcu_read_unlock();
2357 return ret;
2358 }
2359 rcu_read_unlock();
2360 out_drop:
2361 kfree_skb(skb);
2362 return -ENETDOWN;
2363 }
2364
__bpf_redirect_neigh_v4(struct sk_buff * skb,struct net_device * dev,struct bpf_nh_params * nh)2365 static int __bpf_redirect_neigh_v4(struct sk_buff *skb, struct net_device *dev,
2366 struct bpf_nh_params *nh)
2367 {
2368 const struct iphdr *ip4h = ip_hdr(skb);
2369 struct net *net = dev_net(dev);
2370 int err, ret = NET_XMIT_DROP;
2371
2372 if (!nh) {
2373 struct flowi4 fl4 = {
2374 .flowi4_flags = FLOWI_FLAG_ANYSRC,
2375 .flowi4_mark = skb->mark,
2376 .flowi4_dscp = ip4h_dscp(ip4h),
2377 .flowi4_oif = dev->ifindex,
2378 .flowi4_proto = ip4h->protocol,
2379 .daddr = ip4h->daddr,
2380 .saddr = ip4h->saddr,
2381 };
2382 struct rtable *rt;
2383
2384 rt = ip_route_output_flow(net, &fl4, NULL);
2385 if (IS_ERR(rt))
2386 goto out_drop;
2387 if (rt->rt_type != RTN_UNICAST && rt->rt_type != RTN_LOCAL) {
2388 ip_rt_put(rt);
2389 goto out_drop;
2390 }
2391
2392 skb_dst_drop(skb);
2393 skb_dst_set(skb, &rt->dst);
2394 }
2395
2396 err = bpf_out_neigh_v4(net, skb, dev, nh);
2397 if (unlikely(net_xmit_eval(err)))
2398 dev_core_stats_tx_dropped_inc(dev);
2399 else
2400 ret = NET_XMIT_SUCCESS;
2401 goto out_xmit;
2402 out_drop:
2403 dev_core_stats_tx_dropped_inc(dev);
2404 kfree_skb(skb);
2405 out_xmit:
2406 return ret;
2407 }
2408 #else
__bpf_redirect_neigh_v4(struct sk_buff * skb,struct net_device * dev,struct bpf_nh_params * nh)2409 static int __bpf_redirect_neigh_v4(struct sk_buff *skb, struct net_device *dev,
2410 struct bpf_nh_params *nh)
2411 {
2412 kfree_skb(skb);
2413 return NET_XMIT_DROP;
2414 }
2415 #endif /* CONFIG_INET */
2416
__bpf_redirect_neigh(struct sk_buff * skb,struct net_device * dev,struct bpf_nh_params * nh)2417 static int __bpf_redirect_neigh(struct sk_buff *skb, struct net_device *dev,
2418 struct bpf_nh_params *nh)
2419 {
2420 struct ethhdr *ethh = eth_hdr(skb);
2421
2422 if (unlikely(skb->mac_header >= skb->network_header))
2423 goto out;
2424 bpf_push_mac_rcsum(skb);
2425 if (is_multicast_ether_addr(ethh->h_dest))
2426 goto out;
2427
2428 skb_pull(skb, sizeof(*ethh));
2429 skb_unset_mac_header(skb);
2430 skb_reset_network_header(skb);
2431
2432 if (skb->protocol == htons(ETH_P_IP))
2433 return __bpf_redirect_neigh_v4(skb, dev, nh);
2434 else if (skb->protocol == htons(ETH_P_IPV6))
2435 return __bpf_redirect_neigh_v6(skb, dev, nh);
2436 out:
2437 kfree_skb(skb);
2438 return -ENOTSUPP;
2439 }
2440
2441 /* Internal, non-exposed redirect flags. */
2442 enum {
2443 BPF_F_NEIGH = (1ULL << 16),
2444 BPF_F_PEER = (1ULL << 17),
2445 BPF_F_NEXTHOP = (1ULL << 18),
2446 #define BPF_F_REDIRECT_INTERNAL (BPF_F_NEIGH | BPF_F_PEER | BPF_F_NEXTHOP)
2447 };
2448
BPF_CALL_3(bpf_clone_redirect,struct sk_buff *,skb,u32,ifindex,u64,flags)2449 BPF_CALL_3(bpf_clone_redirect, struct sk_buff *, skb, u32, ifindex, u64, flags)
2450 {
2451 struct net_device *dev;
2452 struct sk_buff *clone;
2453 int ret;
2454
2455 BUILD_BUG_ON(BPF_F_REDIRECT_INTERNAL & BPF_F_REDIRECT_FLAGS);
2456
2457 if (unlikely(flags & (~(BPF_F_INGRESS) | BPF_F_REDIRECT_INTERNAL)))
2458 return -EINVAL;
2459
2460 /* BPF test infra's convert___skb_to_skb() can create type-less
2461 * GSO packets. gso_features_check() will detect this as a bad
2462 * offload. However, lets not leak them out in the first place.
2463 */
2464 if (unlikely(skb_is_gso(skb) && !skb_shinfo(skb)->gso_type))
2465 return -EBADMSG;
2466
2467 dev = dev_get_by_index_rcu(dev_net(skb->dev), ifindex);
2468 if (unlikely(!dev))
2469 return -EINVAL;
2470
2471 clone = skb_clone(skb, GFP_ATOMIC);
2472 if (unlikely(!clone))
2473 return -ENOMEM;
2474
2475 /* For direct write, we need to keep the invariant that the skbs
2476 * we're dealing with need to be uncloned. Should uncloning fail
2477 * here, we need to free the just generated clone to unclone once
2478 * again.
2479 */
2480 ret = bpf_try_make_head_writable(skb);
2481 if (unlikely(ret)) {
2482 kfree_skb(clone);
2483 return -ENOMEM;
2484 }
2485
2486 return __bpf_redirect(clone, dev, flags);
2487 }
2488
2489 static const struct bpf_func_proto bpf_clone_redirect_proto = {
2490 .func = bpf_clone_redirect,
2491 .gpl_only = false,
2492 .ret_type = RET_INTEGER,
2493 .arg1_type = ARG_PTR_TO_CTX,
2494 .arg2_type = ARG_ANYTHING,
2495 .arg3_type = ARG_ANYTHING,
2496 };
2497
skb_get_peer_dev(struct net_device * dev)2498 static struct net_device *skb_get_peer_dev(struct net_device *dev)
2499 {
2500 const struct net_device_ops *ops = dev->netdev_ops;
2501
2502 if (likely(ops->ndo_get_peer_dev))
2503 return INDIRECT_CALL_1(ops->ndo_get_peer_dev,
2504 netkit_peer_dev, dev);
2505 return NULL;
2506 }
2507
skb_do_redirect(struct sk_buff * skb)2508 int skb_do_redirect(struct sk_buff *skb)
2509 {
2510 struct bpf_redirect_info *ri = bpf_net_ctx_get_ri();
2511 struct net *net = dev_net(skb->dev);
2512 struct net_device *dev;
2513 u32 flags = ri->flags;
2514
2515 dev = dev_get_by_index_rcu(net, ri->tgt_index);
2516 ri->tgt_index = 0;
2517 ri->flags = 0;
2518 if (unlikely(!dev))
2519 goto out_drop;
2520 if (flags & BPF_F_PEER) {
2521 if (unlikely(!skb_at_tc_ingress(skb)))
2522 goto out_drop;
2523 dev = skb_get_peer_dev(dev);
2524 if (unlikely(!dev ||
2525 !(dev->flags & IFF_UP) ||
2526 net_eq(net, dev_net(dev))))
2527 goto out_drop;
2528 skb->dev = dev;
2529 dev_sw_netstats_rx_add(dev, skb->len);
2530 skb_scrub_packet(skb, false);
2531 return -EAGAIN;
2532 }
2533 return flags & BPF_F_NEIGH ?
2534 __bpf_redirect_neigh(skb, dev, flags & BPF_F_NEXTHOP ?
2535 &ri->nh : NULL) :
2536 __bpf_redirect(skb, dev, flags);
2537 out_drop:
2538 kfree_skb(skb);
2539 return -EINVAL;
2540 }
2541
BPF_CALL_2(bpf_redirect,u32,ifindex,u64,flags)2542 BPF_CALL_2(bpf_redirect, u32, ifindex, u64, flags)
2543 {
2544 struct bpf_redirect_info *ri = bpf_net_ctx_get_ri();
2545
2546 if (unlikely(flags & (~(BPF_F_INGRESS) | BPF_F_REDIRECT_INTERNAL)))
2547 return TC_ACT_SHOT;
2548
2549 ri->flags = flags;
2550 ri->tgt_index = ifindex;
2551
2552 return TC_ACT_REDIRECT;
2553 }
2554
2555 static const struct bpf_func_proto bpf_redirect_proto = {
2556 .func = bpf_redirect,
2557 .gpl_only = false,
2558 .ret_type = RET_INTEGER,
2559 .arg1_type = ARG_ANYTHING,
2560 .arg2_type = ARG_ANYTHING,
2561 };
2562
BPF_CALL_2(bpf_redirect_peer,u32,ifindex,u64,flags)2563 BPF_CALL_2(bpf_redirect_peer, u32, ifindex, u64, flags)
2564 {
2565 struct bpf_redirect_info *ri = bpf_net_ctx_get_ri();
2566
2567 if (unlikely(flags))
2568 return TC_ACT_SHOT;
2569
2570 ri->flags = BPF_F_PEER;
2571 ri->tgt_index = ifindex;
2572
2573 return TC_ACT_REDIRECT;
2574 }
2575
2576 static const struct bpf_func_proto bpf_redirect_peer_proto = {
2577 .func = bpf_redirect_peer,
2578 .gpl_only = false,
2579 .ret_type = RET_INTEGER,
2580 .arg1_type = ARG_ANYTHING,
2581 .arg2_type = ARG_ANYTHING,
2582 };
2583
BPF_CALL_4(bpf_redirect_neigh,u32,ifindex,struct bpf_redir_neigh *,params,int,plen,u64,flags)2584 BPF_CALL_4(bpf_redirect_neigh, u32, ifindex, struct bpf_redir_neigh *, params,
2585 int, plen, u64, flags)
2586 {
2587 struct bpf_redirect_info *ri = bpf_net_ctx_get_ri();
2588
2589 if (unlikely((plen && plen < sizeof(*params)) || flags))
2590 return TC_ACT_SHOT;
2591
2592 ri->flags = BPF_F_NEIGH | (plen ? BPF_F_NEXTHOP : 0);
2593 ri->tgt_index = ifindex;
2594
2595 BUILD_BUG_ON(sizeof(struct bpf_redir_neigh) != sizeof(struct bpf_nh_params));
2596 if (plen)
2597 memcpy(&ri->nh, params, sizeof(ri->nh));
2598
2599 return TC_ACT_REDIRECT;
2600 }
2601
2602 static const struct bpf_func_proto bpf_redirect_neigh_proto = {
2603 .func = bpf_redirect_neigh,
2604 .gpl_only = false,
2605 .ret_type = RET_INTEGER,
2606 .arg1_type = ARG_ANYTHING,
2607 .arg2_type = ARG_PTR_TO_MEM | PTR_MAYBE_NULL | MEM_RDONLY,
2608 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
2609 .arg4_type = ARG_ANYTHING,
2610 };
2611
BPF_CALL_2(bpf_msg_apply_bytes,struct sk_msg *,msg,u32,bytes)2612 BPF_CALL_2(bpf_msg_apply_bytes, struct sk_msg *, msg, u32, bytes)
2613 {
2614 msg->apply_bytes = bytes;
2615 return 0;
2616 }
2617
2618 static const struct bpf_func_proto bpf_msg_apply_bytes_proto = {
2619 .func = bpf_msg_apply_bytes,
2620 .gpl_only = false,
2621 .ret_type = RET_INTEGER,
2622 .arg1_type = ARG_PTR_TO_CTX,
2623 .arg2_type = ARG_ANYTHING,
2624 };
2625
BPF_CALL_2(bpf_msg_cork_bytes,struct sk_msg *,msg,u32,bytes)2626 BPF_CALL_2(bpf_msg_cork_bytes, struct sk_msg *, msg, u32, bytes)
2627 {
2628 msg->cork_bytes = bytes;
2629 return 0;
2630 }
2631
sk_msg_reset_curr(struct sk_msg * msg)2632 static void sk_msg_reset_curr(struct sk_msg *msg)
2633 {
2634 if (!msg->sg.size) {
2635 msg->sg.curr = msg->sg.start;
2636 msg->sg.copybreak = 0;
2637 } else {
2638 u32 i = msg->sg.end;
2639
2640 sk_msg_iter_var_prev(i);
2641 msg->sg.curr = i;
2642 msg->sg.copybreak = msg->sg.data[i].length;
2643 }
2644 }
2645
2646 static const struct bpf_func_proto bpf_msg_cork_bytes_proto = {
2647 .func = bpf_msg_cork_bytes,
2648 .gpl_only = false,
2649 .ret_type = RET_INTEGER,
2650 .arg1_type = ARG_PTR_TO_CTX,
2651 .arg2_type = ARG_ANYTHING,
2652 };
2653
BPF_CALL_4(bpf_msg_pull_data,struct sk_msg *,msg,u32,start,u32,end,u64,flags)2654 BPF_CALL_4(bpf_msg_pull_data, struct sk_msg *, msg, u32, start,
2655 u32, end, u64, flags)
2656 {
2657 u32 len = 0, offset = 0, copy = 0, poffset = 0, bytes = end - start;
2658 u32 first_sge, last_sge, i, shift, bytes_sg_total;
2659 struct scatterlist *sge;
2660 u8 *raw, *to, *from;
2661 struct page *page;
2662
2663 if (unlikely(flags || end <= start))
2664 return -EINVAL;
2665
2666 /* First find the starting scatterlist element */
2667 i = msg->sg.start;
2668 do {
2669 offset += len;
2670 len = sk_msg_elem(msg, i)->length;
2671 if (start < offset + len)
2672 break;
2673 sk_msg_iter_var_next(i);
2674 } while (i != msg->sg.end);
2675
2676 if (unlikely(start >= offset + len))
2677 return -EINVAL;
2678
2679 first_sge = i;
2680 /* The start may point into the sg element so we need to also
2681 * account for the headroom.
2682 */
2683 bytes_sg_total = start - offset + bytes;
2684 if (!test_bit(i, msg->sg.copy) && bytes_sg_total <= len)
2685 goto out;
2686
2687 /* At this point we need to linearize multiple scatterlist
2688 * elements or a single shared page. Either way we need to
2689 * copy into a linear buffer exclusively owned by BPF. Then
2690 * place the buffer in the scatterlist and fixup the original
2691 * entries by removing the entries now in the linear buffer
2692 * and shifting the remaining entries. For now we do not try
2693 * to copy partial entries to avoid complexity of running out
2694 * of sg_entry slots. The downside is reading a single byte
2695 * will copy the entire sg entry.
2696 */
2697 do {
2698 copy += sk_msg_elem(msg, i)->length;
2699 sk_msg_iter_var_next(i);
2700 if (bytes_sg_total <= copy)
2701 break;
2702 } while (i != msg->sg.end);
2703 last_sge = i;
2704
2705 if (unlikely(bytes_sg_total > copy))
2706 return -EINVAL;
2707
2708 page = alloc_pages(__GFP_NOWARN | GFP_ATOMIC | __GFP_COMP,
2709 get_order(copy));
2710 if (unlikely(!page))
2711 return -ENOMEM;
2712
2713 raw = page_address(page);
2714 i = first_sge;
2715 do {
2716 sge = sk_msg_elem(msg, i);
2717 from = sg_virt(sge);
2718 len = sge->length;
2719 to = raw + poffset;
2720
2721 memcpy(to, from, len);
2722 poffset += len;
2723 sge->length = 0;
2724 put_page(sg_page(sge));
2725
2726 sk_msg_iter_var_next(i);
2727 } while (i != last_sge);
2728
2729 sg_set_page(&msg->sg.data[first_sge], page, copy, 0);
2730
2731 /* To repair sg ring we need to shift entries. If we only
2732 * had a single entry though we can just replace it and
2733 * be done. Otherwise walk the ring and shift the entries.
2734 */
2735 WARN_ON_ONCE(last_sge == first_sge);
2736 shift = last_sge > first_sge ?
2737 last_sge - first_sge - 1 :
2738 NR_MSG_FRAG_IDS - first_sge + last_sge - 1;
2739 if (!shift)
2740 goto out;
2741
2742 i = first_sge;
2743 sk_msg_iter_var_next(i);
2744 do {
2745 u32 move_from;
2746
2747 if (i + shift >= NR_MSG_FRAG_IDS)
2748 move_from = i + shift - NR_MSG_FRAG_IDS;
2749 else
2750 move_from = i + shift;
2751 if (move_from == msg->sg.end)
2752 break;
2753
2754 msg->sg.data[i] = msg->sg.data[move_from];
2755 msg->sg.data[move_from].length = 0;
2756 msg->sg.data[move_from].page_link = 0;
2757 msg->sg.data[move_from].offset = 0;
2758 sk_msg_iter_var_next(i);
2759 } while (1);
2760
2761 msg->sg.end = msg->sg.end - shift > msg->sg.end ?
2762 msg->sg.end - shift + NR_MSG_FRAG_IDS :
2763 msg->sg.end - shift;
2764 out:
2765 sk_msg_reset_curr(msg);
2766 msg->data = sg_virt(&msg->sg.data[first_sge]) + start - offset;
2767 msg->data_end = msg->data + bytes;
2768 return 0;
2769 }
2770
2771 static const struct bpf_func_proto bpf_msg_pull_data_proto = {
2772 .func = bpf_msg_pull_data,
2773 .gpl_only = false,
2774 .ret_type = RET_INTEGER,
2775 .arg1_type = ARG_PTR_TO_CTX,
2776 .arg2_type = ARG_ANYTHING,
2777 .arg3_type = ARG_ANYTHING,
2778 .arg4_type = ARG_ANYTHING,
2779 };
2780
BPF_CALL_4(bpf_msg_push_data,struct sk_msg *,msg,u32,start,u32,len,u64,flags)2781 BPF_CALL_4(bpf_msg_push_data, struct sk_msg *, msg, u32, start,
2782 u32, len, u64, flags)
2783 {
2784 struct scatterlist sge, nsge, nnsge, rsge = {0}, *psge;
2785 u32 new, i = 0, l = 0, space, copy = 0, offset = 0;
2786 u8 *raw, *to, *from;
2787 struct page *page;
2788
2789 if (unlikely(flags))
2790 return -EINVAL;
2791
2792 if (unlikely(len == 0))
2793 return 0;
2794
2795 /* First find the starting scatterlist element */
2796 i = msg->sg.start;
2797 do {
2798 offset += l;
2799 l = sk_msg_elem(msg, i)->length;
2800
2801 if (start < offset + l)
2802 break;
2803 sk_msg_iter_var_next(i);
2804 } while (i != msg->sg.end);
2805
2806 if (start > offset + l)
2807 return -EINVAL;
2808
2809 space = MAX_MSG_FRAGS - sk_msg_elem_used(msg);
2810
2811 /* If no space available will fallback to copy, we need at
2812 * least one scatterlist elem available to push data into
2813 * when start aligns to the beginning of an element or two
2814 * when it falls inside an element. We handle the start equals
2815 * offset case because its the common case for inserting a
2816 * header.
2817 */
2818 if (!space || (space == 1 && start != offset))
2819 copy = msg->sg.data[i].length;
2820
2821 page = alloc_pages(__GFP_NOWARN | GFP_ATOMIC | __GFP_COMP,
2822 get_order(copy + len));
2823 if (unlikely(!page))
2824 return -ENOMEM;
2825
2826 if (copy) {
2827 int front, back;
2828
2829 raw = page_address(page);
2830
2831 if (i == msg->sg.end)
2832 sk_msg_iter_var_prev(i);
2833 psge = sk_msg_elem(msg, i);
2834 front = start - offset;
2835 back = psge->length - front;
2836 from = sg_virt(psge);
2837
2838 if (front)
2839 memcpy(raw, from, front);
2840
2841 if (back) {
2842 from += front;
2843 to = raw + front + len;
2844
2845 memcpy(to, from, back);
2846 }
2847
2848 put_page(sg_page(psge));
2849 new = i;
2850 goto place_new;
2851 }
2852
2853 if (start - offset) {
2854 if (i == msg->sg.end)
2855 sk_msg_iter_var_prev(i);
2856 psge = sk_msg_elem(msg, i);
2857 rsge = sk_msg_elem_cpy(msg, i);
2858
2859 psge->length = start - offset;
2860 rsge.length -= psge->length;
2861 rsge.offset += start;
2862
2863 sk_msg_iter_var_next(i);
2864 sg_unmark_end(psge);
2865 sg_unmark_end(&rsge);
2866 }
2867
2868 /* Slot(s) to place newly allocated data */
2869 sk_msg_iter_next(msg, end);
2870 new = i;
2871 sk_msg_iter_var_next(i);
2872
2873 if (i == msg->sg.end) {
2874 if (!rsge.length)
2875 goto place_new;
2876 sk_msg_iter_next(msg, end);
2877 goto place_new;
2878 }
2879
2880 /* Shift one or two slots as needed */
2881 sge = sk_msg_elem_cpy(msg, new);
2882 sg_unmark_end(&sge);
2883
2884 nsge = sk_msg_elem_cpy(msg, i);
2885 if (rsge.length) {
2886 sk_msg_iter_var_next(i);
2887 nnsge = sk_msg_elem_cpy(msg, i);
2888 sk_msg_iter_next(msg, end);
2889 }
2890
2891 while (i != msg->sg.end) {
2892 msg->sg.data[i] = sge;
2893 sge = nsge;
2894 sk_msg_iter_var_next(i);
2895 if (rsge.length) {
2896 nsge = nnsge;
2897 nnsge = sk_msg_elem_cpy(msg, i);
2898 } else {
2899 nsge = sk_msg_elem_cpy(msg, i);
2900 }
2901 }
2902
2903 place_new:
2904 /* Place newly allocated data buffer */
2905 sk_mem_charge(msg->sk, len);
2906 msg->sg.size += len;
2907 __clear_bit(new, msg->sg.copy);
2908 sg_set_page(&msg->sg.data[new], page, len + copy, 0);
2909 if (rsge.length) {
2910 get_page(sg_page(&rsge));
2911 sk_msg_iter_var_next(new);
2912 msg->sg.data[new] = rsge;
2913 }
2914
2915 sk_msg_reset_curr(msg);
2916 sk_msg_compute_data_pointers(msg);
2917 return 0;
2918 }
2919
2920 static const struct bpf_func_proto bpf_msg_push_data_proto = {
2921 .func = bpf_msg_push_data,
2922 .gpl_only = false,
2923 .ret_type = RET_INTEGER,
2924 .arg1_type = ARG_PTR_TO_CTX,
2925 .arg2_type = ARG_ANYTHING,
2926 .arg3_type = ARG_ANYTHING,
2927 .arg4_type = ARG_ANYTHING,
2928 };
2929
sk_msg_shift_left(struct sk_msg * msg,int i)2930 static void sk_msg_shift_left(struct sk_msg *msg, int i)
2931 {
2932 struct scatterlist *sge = sk_msg_elem(msg, i);
2933 int prev;
2934
2935 put_page(sg_page(sge));
2936 do {
2937 prev = i;
2938 sk_msg_iter_var_next(i);
2939 msg->sg.data[prev] = msg->sg.data[i];
2940 } while (i != msg->sg.end);
2941
2942 sk_msg_iter_prev(msg, end);
2943 }
2944
sk_msg_shift_right(struct sk_msg * msg,int i)2945 static void sk_msg_shift_right(struct sk_msg *msg, int i)
2946 {
2947 struct scatterlist tmp, sge;
2948
2949 sk_msg_iter_next(msg, end);
2950 sge = sk_msg_elem_cpy(msg, i);
2951 sk_msg_iter_var_next(i);
2952 tmp = sk_msg_elem_cpy(msg, i);
2953
2954 while (i != msg->sg.end) {
2955 msg->sg.data[i] = sge;
2956 sk_msg_iter_var_next(i);
2957 sge = tmp;
2958 tmp = sk_msg_elem_cpy(msg, i);
2959 }
2960 }
2961
BPF_CALL_4(bpf_msg_pop_data,struct sk_msg *,msg,u32,start,u32,len,u64,flags)2962 BPF_CALL_4(bpf_msg_pop_data, struct sk_msg *, msg, u32, start,
2963 u32, len, u64, flags)
2964 {
2965 u32 i = 0, l = 0, space, offset = 0;
2966 u64 last = start + len;
2967 int pop;
2968
2969 if (unlikely(flags))
2970 return -EINVAL;
2971
2972 if (unlikely(len == 0))
2973 return 0;
2974
2975 /* First find the starting scatterlist element */
2976 i = msg->sg.start;
2977 do {
2978 offset += l;
2979 l = sk_msg_elem(msg, i)->length;
2980
2981 if (start < offset + l)
2982 break;
2983 sk_msg_iter_var_next(i);
2984 } while (i != msg->sg.end);
2985
2986 /* Bounds checks: start and pop must be inside message */
2987 if (start >= offset + l || last > msg->sg.size)
2988 return -EINVAL;
2989
2990 space = MAX_MSG_FRAGS - sk_msg_elem_used(msg);
2991
2992 pop = len;
2993 /* --------------| offset
2994 * -| start |-------- len -------|
2995 *
2996 * |----- a ----|-------- pop -------|----- b ----|
2997 * |______________________________________________| length
2998 *
2999 *
3000 * a: region at front of scatter element to save
3001 * b: region at back of scatter element to save when length > A + pop
3002 * pop: region to pop from element, same as input 'pop' here will be
3003 * decremented below per iteration.
3004 *
3005 * Two top-level cases to handle when start != offset, first B is non
3006 * zero and second B is zero corresponding to when a pop includes more
3007 * than one element.
3008 *
3009 * Then if B is non-zero AND there is no space allocate space and
3010 * compact A, B regions into page. If there is space shift ring to
3011 * the right free'ing the next element in ring to place B, leaving
3012 * A untouched except to reduce length.
3013 */
3014 if (start != offset) {
3015 struct scatterlist *nsge, *sge = sk_msg_elem(msg, i);
3016 int a = start - offset;
3017 int b = sge->length - pop - a;
3018
3019 sk_msg_iter_var_next(i);
3020
3021 if (b > 0) {
3022 if (space) {
3023 sge->length = a;
3024 sk_msg_shift_right(msg, i);
3025 nsge = sk_msg_elem(msg, i);
3026 get_page(sg_page(sge));
3027 sg_set_page(nsge,
3028 sg_page(sge),
3029 b, sge->offset + pop + a);
3030 } else {
3031 struct page *page, *orig;
3032 u8 *to, *from;
3033
3034 page = alloc_pages(__GFP_NOWARN |
3035 __GFP_COMP | GFP_ATOMIC,
3036 get_order(a + b));
3037 if (unlikely(!page))
3038 return -ENOMEM;
3039
3040 orig = sg_page(sge);
3041 from = sg_virt(sge);
3042 to = page_address(page);
3043 memcpy(to, from, a);
3044 memcpy(to + a, from + a + pop, b);
3045 sg_set_page(sge, page, a + b, 0);
3046 put_page(orig);
3047 }
3048 pop = 0;
3049 } else {
3050 pop -= (sge->length - a);
3051 sge->length = a;
3052 }
3053 }
3054
3055 /* From above the current layout _must_ be as follows,
3056 *
3057 * -| offset
3058 * -| start
3059 *
3060 * |---- pop ---|---------------- b ------------|
3061 * |____________________________________________| length
3062 *
3063 * Offset and start of the current msg elem are equal because in the
3064 * previous case we handled offset != start and either consumed the
3065 * entire element and advanced to the next element OR pop == 0.
3066 *
3067 * Two cases to handle here are first pop is less than the length
3068 * leaving some remainder b above. Simply adjust the element's layout
3069 * in this case. Or pop >= length of the element so that b = 0. In this
3070 * case advance to next element decrementing pop.
3071 */
3072 while (pop) {
3073 struct scatterlist *sge = sk_msg_elem(msg, i);
3074
3075 if (pop < sge->length) {
3076 sge->length -= pop;
3077 sge->offset += pop;
3078 pop = 0;
3079 } else {
3080 pop -= sge->length;
3081 sk_msg_shift_left(msg, i);
3082 }
3083 }
3084
3085 sk_mem_uncharge(msg->sk, len - pop);
3086 msg->sg.size -= (len - pop);
3087 sk_msg_reset_curr(msg);
3088 sk_msg_compute_data_pointers(msg);
3089 return 0;
3090 }
3091
3092 static const struct bpf_func_proto bpf_msg_pop_data_proto = {
3093 .func = bpf_msg_pop_data,
3094 .gpl_only = false,
3095 .ret_type = RET_INTEGER,
3096 .arg1_type = ARG_PTR_TO_CTX,
3097 .arg2_type = ARG_ANYTHING,
3098 .arg3_type = ARG_ANYTHING,
3099 .arg4_type = ARG_ANYTHING,
3100 };
3101
3102 #ifdef CONFIG_CGROUP_NET_CLASSID
BPF_CALL_0(bpf_get_cgroup_classid_curr)3103 BPF_CALL_0(bpf_get_cgroup_classid_curr)
3104 {
3105 return __task_get_classid(current);
3106 }
3107
3108 const struct bpf_func_proto bpf_get_cgroup_classid_curr_proto = {
3109 .func = bpf_get_cgroup_classid_curr,
3110 .gpl_only = false,
3111 .ret_type = RET_INTEGER,
3112 };
3113
BPF_CALL_1(bpf_skb_cgroup_classid,const struct sk_buff *,skb)3114 BPF_CALL_1(bpf_skb_cgroup_classid, const struct sk_buff *, skb)
3115 {
3116 struct sock *sk = skb_to_full_sk(skb);
3117
3118 if (!sk || !sk_fullsock(sk))
3119 return 0;
3120
3121 return sock_cgroup_classid(&sk->sk_cgrp_data);
3122 }
3123
3124 static const struct bpf_func_proto bpf_skb_cgroup_classid_proto = {
3125 .func = bpf_skb_cgroup_classid,
3126 .gpl_only = false,
3127 .ret_type = RET_INTEGER,
3128 .arg1_type = ARG_PTR_TO_CTX,
3129 };
3130 #endif
3131
BPF_CALL_1(bpf_get_cgroup_classid,const struct sk_buff *,skb)3132 BPF_CALL_1(bpf_get_cgroup_classid, const struct sk_buff *, skb)
3133 {
3134 return task_get_classid(skb);
3135 }
3136
3137 static const struct bpf_func_proto bpf_get_cgroup_classid_proto = {
3138 .func = bpf_get_cgroup_classid,
3139 .gpl_only = false,
3140 .ret_type = RET_INTEGER,
3141 .arg1_type = ARG_PTR_TO_CTX,
3142 };
3143
BPF_CALL_1(bpf_get_route_realm,const struct sk_buff *,skb)3144 BPF_CALL_1(bpf_get_route_realm, const struct sk_buff *, skb)
3145 {
3146 return dst_tclassid(skb);
3147 }
3148
3149 static const struct bpf_func_proto bpf_get_route_realm_proto = {
3150 .func = bpf_get_route_realm,
3151 .gpl_only = false,
3152 .ret_type = RET_INTEGER,
3153 .arg1_type = ARG_PTR_TO_CTX,
3154 };
3155
BPF_CALL_1(bpf_get_hash_recalc,struct sk_buff *,skb)3156 BPF_CALL_1(bpf_get_hash_recalc, struct sk_buff *, skb)
3157 {
3158 /* If skb_clear_hash() was called due to mangling, we can
3159 * trigger SW recalculation here. Later access to hash
3160 * can then use the inline skb->hash via context directly
3161 * instead of calling this helper again.
3162 */
3163 return skb_get_hash(skb);
3164 }
3165
3166 static const struct bpf_func_proto bpf_get_hash_recalc_proto = {
3167 .func = bpf_get_hash_recalc,
3168 .gpl_only = false,
3169 .ret_type = RET_INTEGER,
3170 .arg1_type = ARG_PTR_TO_CTX,
3171 };
3172
BPF_CALL_1(bpf_set_hash_invalid,struct sk_buff *,skb)3173 BPF_CALL_1(bpf_set_hash_invalid, struct sk_buff *, skb)
3174 {
3175 /* After all direct packet write, this can be used once for
3176 * triggering a lazy recalc on next skb_get_hash() invocation.
3177 */
3178 skb_clear_hash(skb);
3179 return 0;
3180 }
3181
3182 static const struct bpf_func_proto bpf_set_hash_invalid_proto = {
3183 .func = bpf_set_hash_invalid,
3184 .gpl_only = false,
3185 .ret_type = RET_INTEGER,
3186 .arg1_type = ARG_PTR_TO_CTX,
3187 };
3188
BPF_CALL_2(bpf_set_hash,struct sk_buff *,skb,u32,hash)3189 BPF_CALL_2(bpf_set_hash, struct sk_buff *, skb, u32, hash)
3190 {
3191 /* Set user specified hash as L4(+), so that it gets returned
3192 * on skb_get_hash() call unless BPF prog later on triggers a
3193 * skb_clear_hash().
3194 */
3195 __skb_set_sw_hash(skb, hash, true);
3196 return 0;
3197 }
3198
3199 static const struct bpf_func_proto bpf_set_hash_proto = {
3200 .func = bpf_set_hash,
3201 .gpl_only = false,
3202 .ret_type = RET_INTEGER,
3203 .arg1_type = ARG_PTR_TO_CTX,
3204 .arg2_type = ARG_ANYTHING,
3205 };
3206
BPF_CALL_3(bpf_skb_vlan_push,struct sk_buff *,skb,__be16,vlan_proto,u16,vlan_tci)3207 BPF_CALL_3(bpf_skb_vlan_push, struct sk_buff *, skb, __be16, vlan_proto,
3208 u16, vlan_tci)
3209 {
3210 int ret;
3211
3212 if (unlikely(vlan_proto != htons(ETH_P_8021Q) &&
3213 vlan_proto != htons(ETH_P_8021AD)))
3214 vlan_proto = htons(ETH_P_8021Q);
3215
3216 bpf_push_mac_rcsum(skb);
3217 ret = skb_vlan_push(skb, vlan_proto, vlan_tci);
3218 bpf_pull_mac_rcsum(skb);
3219 skb_reset_mac_len(skb);
3220
3221 bpf_compute_data_pointers(skb);
3222 return ret;
3223 }
3224
3225 static const struct bpf_func_proto bpf_skb_vlan_push_proto = {
3226 .func = bpf_skb_vlan_push,
3227 .gpl_only = false,
3228 .ret_type = RET_INTEGER,
3229 .arg1_type = ARG_PTR_TO_CTX,
3230 .arg2_type = ARG_ANYTHING,
3231 .arg3_type = ARG_ANYTHING,
3232 };
3233
BPF_CALL_1(bpf_skb_vlan_pop,struct sk_buff *,skb)3234 BPF_CALL_1(bpf_skb_vlan_pop, struct sk_buff *, skb)
3235 {
3236 int ret;
3237
3238 bpf_push_mac_rcsum(skb);
3239 ret = skb_vlan_pop(skb);
3240 bpf_pull_mac_rcsum(skb);
3241
3242 bpf_compute_data_pointers(skb);
3243 return ret;
3244 }
3245
3246 static const struct bpf_func_proto bpf_skb_vlan_pop_proto = {
3247 .func = bpf_skb_vlan_pop,
3248 .gpl_only = false,
3249 .ret_type = RET_INTEGER,
3250 .arg1_type = ARG_PTR_TO_CTX,
3251 };
3252
bpf_skb_change_protocol(struct sk_buff * skb,u16 proto)3253 static void bpf_skb_change_protocol(struct sk_buff *skb, u16 proto)
3254 {
3255 skb->protocol = htons(proto);
3256 if (skb_valid_dst(skb))
3257 skb_dst_drop(skb);
3258 }
3259
bpf_skb_generic_push(struct sk_buff * skb,u32 off,u32 len)3260 static int bpf_skb_generic_push(struct sk_buff *skb, u32 off, u32 len)
3261 {
3262 /* Caller already did skb_cow() with meta_len+len as headroom,
3263 * so no need to do it here.
3264 */
3265 skb_push(skb, len);
3266 skb_postpush_data_move(skb, len, off);
3267 memset(skb->data + off, 0, len);
3268
3269 /* No skb_postpush_rcsum(skb, skb->data + off, len)
3270 * needed here as it does not change the skb->csum
3271 * result for checksum complete when summing over
3272 * zeroed blocks.
3273 */
3274 return 0;
3275 }
3276
bpf_skb_generic_pop(struct sk_buff * skb,u32 off,u32 len)3277 static int bpf_skb_generic_pop(struct sk_buff *skb, u32 off, u32 len)
3278 {
3279 void *old_data;
3280
3281 /* skb_ensure_writable() is not needed here, as we're
3282 * already working on an uncloned skb.
3283 */
3284 if (unlikely(!pskb_may_pull(skb, off + len)))
3285 return -ENOMEM;
3286
3287 old_data = skb->data;
3288 __skb_pull(skb, len);
3289 skb_postpull_rcsum(skb, old_data + off, len);
3290 skb_postpull_data_move(skb, len, off);
3291
3292 return 0;
3293 }
3294
bpf_skb_net_hdr_push(struct sk_buff * skb,u32 off,u32 len)3295 static int bpf_skb_net_hdr_push(struct sk_buff *skb, u32 off, u32 len)
3296 {
3297 bool trans_same = skb->transport_header == skb->network_header;
3298 int ret;
3299
3300 /* There's no need for __skb_push()/__skb_pull() pair to
3301 * get to the start of the mac header as we're guaranteed
3302 * to always start from here under eBPF.
3303 */
3304 ret = bpf_skb_generic_push(skb, off, len);
3305 if (likely(!ret)) {
3306 skb->mac_header -= len;
3307 skb->network_header -= len;
3308 if (trans_same)
3309 skb->transport_header = skb->network_header;
3310 }
3311
3312 return ret;
3313 }
3314
bpf_skb_net_hdr_pop(struct sk_buff * skb,u32 off,u32 len)3315 static int bpf_skb_net_hdr_pop(struct sk_buff *skb, u32 off, u32 len)
3316 {
3317 bool trans_same = skb->transport_header == skb->network_header;
3318 int ret;
3319
3320 /* Same here, __skb_push()/__skb_pull() pair not needed. */
3321 ret = bpf_skb_generic_pop(skb, off, len);
3322 if (likely(!ret)) {
3323 skb->mac_header += len;
3324 skb->network_header += len;
3325 if (trans_same)
3326 skb->transport_header = skb->network_header;
3327 }
3328
3329 return ret;
3330 }
3331
bpf_skb_proto_4_to_6(struct sk_buff * skb)3332 static int bpf_skb_proto_4_to_6(struct sk_buff *skb)
3333 {
3334 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
3335 const u8 meta_len = skb_metadata_len(skb);
3336 u32 off = skb_mac_header_len(skb);
3337 int ret;
3338
3339 ret = skb_cow(skb, meta_len + len_diff);
3340 if (unlikely(ret < 0))
3341 return ret;
3342
3343 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
3344 if (unlikely(ret < 0))
3345 return ret;
3346
3347 if (skb_is_gso(skb)) {
3348 struct skb_shared_info *shinfo = skb_shinfo(skb);
3349
3350 /* SKB_GSO_TCPV4 needs to be changed into SKB_GSO_TCPV6. */
3351 if (shinfo->gso_type & SKB_GSO_TCPV4) {
3352 shinfo->gso_type &= ~SKB_GSO_TCPV4;
3353 shinfo->gso_type |= SKB_GSO_TCPV6;
3354 }
3355 shinfo->gso_type |= SKB_GSO_DODGY;
3356 }
3357
3358 bpf_skb_change_protocol(skb, ETH_P_IPV6);
3359 skb_clear_hash(skb);
3360
3361 return 0;
3362 }
3363
bpf_skb_proto_6_to_4(struct sk_buff * skb)3364 static int bpf_skb_proto_6_to_4(struct sk_buff *skb)
3365 {
3366 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
3367 u32 off = skb_mac_header_len(skb);
3368 int ret;
3369
3370 ret = skb_unclone(skb, GFP_ATOMIC);
3371 if (unlikely(ret < 0))
3372 return ret;
3373
3374 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
3375 if (unlikely(ret < 0))
3376 return ret;
3377
3378 if (skb_is_gso(skb)) {
3379 struct skb_shared_info *shinfo = skb_shinfo(skb);
3380
3381 /* SKB_GSO_TCPV6 needs to be changed into SKB_GSO_TCPV4. */
3382 if (shinfo->gso_type & SKB_GSO_TCPV6) {
3383 shinfo->gso_type &= ~SKB_GSO_TCPV6;
3384 shinfo->gso_type |= SKB_GSO_TCPV4;
3385 }
3386 shinfo->gso_type |= SKB_GSO_DODGY;
3387 }
3388
3389 bpf_skb_change_protocol(skb, ETH_P_IP);
3390 skb_clear_hash(skb);
3391
3392 return 0;
3393 }
3394
bpf_skb_proto_xlat(struct sk_buff * skb,__be16 to_proto)3395 static int bpf_skb_proto_xlat(struct sk_buff *skb, __be16 to_proto)
3396 {
3397 __be16 from_proto = skb->protocol;
3398
3399 if (from_proto == htons(ETH_P_IP) &&
3400 to_proto == htons(ETH_P_IPV6))
3401 return bpf_skb_proto_4_to_6(skb);
3402
3403 if (from_proto == htons(ETH_P_IPV6) &&
3404 to_proto == htons(ETH_P_IP))
3405 return bpf_skb_proto_6_to_4(skb);
3406
3407 return -ENOTSUPP;
3408 }
3409
BPF_CALL_3(bpf_skb_change_proto,struct sk_buff *,skb,__be16,proto,u64,flags)3410 BPF_CALL_3(bpf_skb_change_proto, struct sk_buff *, skb, __be16, proto,
3411 u64, flags)
3412 {
3413 int ret;
3414
3415 if (unlikely(flags))
3416 return -EINVAL;
3417
3418 /* General idea is that this helper does the basic groundwork
3419 * needed for changing the protocol, and eBPF program fills the
3420 * rest through bpf_skb_store_bytes(), bpf_lX_csum_replace()
3421 * and other helpers, rather than passing a raw buffer here.
3422 *
3423 * The rationale is to keep this minimal and without a need to
3424 * deal with raw packet data. F.e. even if we would pass buffers
3425 * here, the program still needs to call the bpf_lX_csum_replace()
3426 * helpers anyway. Plus, this way we keep also separation of
3427 * concerns, since f.e. bpf_skb_store_bytes() should only take
3428 * care of stores.
3429 *
3430 * Currently, additional options and extension header space are
3431 * not supported, but flags register is reserved so we can adapt
3432 * that. For offloads, we mark packet as dodgy, so that headers
3433 * need to be verified first.
3434 */
3435 ret = bpf_skb_proto_xlat(skb, proto);
3436 bpf_compute_data_pointers(skb);
3437 return ret;
3438 }
3439
3440 static const struct bpf_func_proto bpf_skb_change_proto_proto = {
3441 .func = bpf_skb_change_proto,
3442 .gpl_only = false,
3443 .ret_type = RET_INTEGER,
3444 .arg1_type = ARG_PTR_TO_CTX,
3445 .arg2_type = ARG_ANYTHING,
3446 .arg3_type = ARG_ANYTHING,
3447 };
3448
BPF_CALL_2(bpf_skb_change_type,struct sk_buff *,skb,u32,pkt_type)3449 BPF_CALL_2(bpf_skb_change_type, struct sk_buff *, skb, u32, pkt_type)
3450 {
3451 /* We only allow a restricted subset to be changed for now. */
3452 if (unlikely(!skb_pkt_type_ok(skb->pkt_type) ||
3453 !skb_pkt_type_ok(pkt_type)))
3454 return -EINVAL;
3455
3456 skb->pkt_type = pkt_type;
3457 return 0;
3458 }
3459
3460 static const struct bpf_func_proto bpf_skb_change_type_proto = {
3461 .func = bpf_skb_change_type,
3462 .gpl_only = false,
3463 .ret_type = RET_INTEGER,
3464 .arg1_type = ARG_PTR_TO_CTX,
3465 .arg2_type = ARG_ANYTHING,
3466 };
3467
bpf_skb_net_base_len(const struct sk_buff * skb)3468 static u32 bpf_skb_net_base_len(const struct sk_buff *skb)
3469 {
3470 switch (skb->protocol) {
3471 case htons(ETH_P_IP):
3472 return sizeof(struct iphdr);
3473 case htons(ETH_P_IPV6):
3474 return sizeof(struct ipv6hdr);
3475 default:
3476 return ~0U;
3477 }
3478 }
3479
3480 #define BPF_F_ADJ_ROOM_ENCAP_L3_MASK (BPF_F_ADJ_ROOM_ENCAP_L3_IPV4 | \
3481 BPF_F_ADJ_ROOM_ENCAP_L3_IPV6)
3482
3483 #define BPF_F_ADJ_ROOM_DECAP_L3_MASK (BPF_F_ADJ_ROOM_DECAP_L3_IPV4 | \
3484 BPF_F_ADJ_ROOM_DECAP_L3_IPV6)
3485
3486 #define BPF_F_ADJ_ROOM_MASK (BPF_F_ADJ_ROOM_FIXED_GSO | \
3487 BPF_F_ADJ_ROOM_ENCAP_L3_MASK | \
3488 BPF_F_ADJ_ROOM_ENCAP_L4_GRE | \
3489 BPF_F_ADJ_ROOM_ENCAP_L4_UDP | \
3490 BPF_F_ADJ_ROOM_ENCAP_L2_ETH | \
3491 BPF_F_ADJ_ROOM_ENCAP_L2( \
3492 BPF_ADJ_ROOM_ENCAP_L2_MASK) | \
3493 BPF_F_ADJ_ROOM_DECAP_L3_MASK)
3494
bpf_skb_net_grow(struct sk_buff * skb,u32 off,u32 len_diff,u64 flags)3495 static int bpf_skb_net_grow(struct sk_buff *skb, u32 off, u32 len_diff,
3496 u64 flags)
3497 {
3498 u8 inner_mac_len = flags >> BPF_ADJ_ROOM_ENCAP_L2_SHIFT;
3499 bool encap = flags & BPF_F_ADJ_ROOM_ENCAP_L3_MASK;
3500 u16 mac_len = 0, inner_net = 0, inner_trans = 0;
3501 const u8 meta_len = skb_metadata_len(skb);
3502 unsigned int gso_type = SKB_GSO_DODGY;
3503 int ret;
3504
3505 if (skb_is_gso(skb) && !skb_is_gso_tcp(skb)) {
3506 /* udp gso_size delineates datagrams, only allow if fixed */
3507 if (!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) ||
3508 !(flags & BPF_F_ADJ_ROOM_FIXED_GSO))
3509 return -ENOTSUPP;
3510 }
3511
3512 ret = skb_cow_head(skb, meta_len + len_diff);
3513 if (unlikely(ret < 0))
3514 return ret;
3515
3516 if (encap) {
3517 if (skb->protocol != htons(ETH_P_IP) &&
3518 skb->protocol != htons(ETH_P_IPV6))
3519 return -ENOTSUPP;
3520
3521 if (flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV4 &&
3522 flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV6)
3523 return -EINVAL;
3524
3525 if (flags & BPF_F_ADJ_ROOM_ENCAP_L4_GRE &&
3526 flags & BPF_F_ADJ_ROOM_ENCAP_L4_UDP)
3527 return -EINVAL;
3528
3529 if (flags & BPF_F_ADJ_ROOM_ENCAP_L2_ETH &&
3530 inner_mac_len < ETH_HLEN)
3531 return -EINVAL;
3532
3533 if (skb->encapsulation)
3534 return -EALREADY;
3535
3536 mac_len = skb->network_header - skb->mac_header;
3537 inner_net = skb->network_header;
3538 if (inner_mac_len > len_diff)
3539 return -EINVAL;
3540 inner_trans = skb->transport_header;
3541 }
3542
3543 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
3544 if (unlikely(ret < 0))
3545 return ret;
3546
3547 if (encap) {
3548 skb->inner_mac_header = inner_net - inner_mac_len;
3549 skb->inner_network_header = inner_net;
3550 skb->inner_transport_header = inner_trans;
3551
3552 if (flags & BPF_F_ADJ_ROOM_ENCAP_L2_ETH)
3553 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
3554 else
3555 skb_set_inner_protocol(skb, skb->protocol);
3556
3557 skb->encapsulation = 1;
3558 skb_set_network_header(skb, mac_len);
3559
3560 if (flags & BPF_F_ADJ_ROOM_ENCAP_L4_UDP)
3561 gso_type |= SKB_GSO_UDP_TUNNEL;
3562 else if (flags & BPF_F_ADJ_ROOM_ENCAP_L4_GRE)
3563 gso_type |= SKB_GSO_GRE;
3564 else if (flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV6)
3565 gso_type |= SKB_GSO_IPXIP6;
3566 else if (flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV4)
3567 gso_type |= SKB_GSO_IPXIP4;
3568
3569 if (flags & BPF_F_ADJ_ROOM_ENCAP_L4_GRE ||
3570 flags & BPF_F_ADJ_ROOM_ENCAP_L4_UDP) {
3571 int nh_len = flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV6 ?
3572 sizeof(struct ipv6hdr) :
3573 sizeof(struct iphdr);
3574
3575 skb_set_transport_header(skb, mac_len + nh_len);
3576 }
3577
3578 /* Match skb->protocol to new outer l3 protocol */
3579 if (skb->protocol == htons(ETH_P_IP) &&
3580 flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV6)
3581 bpf_skb_change_protocol(skb, ETH_P_IPV6);
3582 else if (skb->protocol == htons(ETH_P_IPV6) &&
3583 flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV4)
3584 bpf_skb_change_protocol(skb, ETH_P_IP);
3585 }
3586
3587 if (skb_is_gso(skb)) {
3588 struct skb_shared_info *shinfo = skb_shinfo(skb);
3589
3590 /* Header must be checked, and gso_segs recomputed. */
3591 shinfo->gso_type |= gso_type;
3592 shinfo->gso_segs = 0;
3593
3594 /* Due to header growth, MSS needs to be downgraded.
3595 * There is a BUG_ON() when segmenting the frag_list with
3596 * head_frag true, so linearize the skb after downgrading
3597 * the MSS.
3598 */
3599 if (!(flags & BPF_F_ADJ_ROOM_FIXED_GSO)) {
3600 skb_decrease_gso_size(shinfo, len_diff);
3601 if (shinfo->frag_list)
3602 return skb_linearize(skb);
3603 }
3604 }
3605
3606 return 0;
3607 }
3608
bpf_skb_net_shrink(struct sk_buff * skb,u32 off,u32 len_diff,u64 flags)3609 static int bpf_skb_net_shrink(struct sk_buff *skb, u32 off, u32 len_diff,
3610 u64 flags)
3611 {
3612 int ret;
3613
3614 if (unlikely(flags & ~(BPF_F_ADJ_ROOM_FIXED_GSO |
3615 BPF_F_ADJ_ROOM_DECAP_L3_MASK |
3616 BPF_F_ADJ_ROOM_NO_CSUM_RESET)))
3617 return -EINVAL;
3618
3619 if (skb_is_gso(skb) && !skb_is_gso_tcp(skb)) {
3620 /* udp gso_size delineates datagrams, only allow if fixed */
3621 if (!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) ||
3622 !(flags & BPF_F_ADJ_ROOM_FIXED_GSO))
3623 return -ENOTSUPP;
3624 }
3625
3626 ret = skb_unclone(skb, GFP_ATOMIC);
3627 if (unlikely(ret < 0))
3628 return ret;
3629
3630 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
3631 if (unlikely(ret < 0))
3632 return ret;
3633
3634 /* Match skb->protocol to new outer l3 protocol */
3635 if (skb->protocol == htons(ETH_P_IP) &&
3636 flags & BPF_F_ADJ_ROOM_DECAP_L3_IPV6)
3637 bpf_skb_change_protocol(skb, ETH_P_IPV6);
3638 else if (skb->protocol == htons(ETH_P_IPV6) &&
3639 flags & BPF_F_ADJ_ROOM_DECAP_L3_IPV4)
3640 bpf_skb_change_protocol(skb, ETH_P_IP);
3641
3642 if (skb_is_gso(skb)) {
3643 struct skb_shared_info *shinfo = skb_shinfo(skb);
3644
3645 /* Due to header shrink, MSS can be upgraded. */
3646 if (!(flags & BPF_F_ADJ_ROOM_FIXED_GSO))
3647 skb_increase_gso_size(shinfo, len_diff);
3648
3649 /* Header must be checked, and gso_segs recomputed. */
3650 shinfo->gso_type |= SKB_GSO_DODGY;
3651 shinfo->gso_segs = 0;
3652 }
3653
3654 return 0;
3655 }
3656
3657 #define BPF_SKB_MAX_LEN SKB_MAX_ALLOC
3658
BPF_CALL_4(sk_skb_adjust_room,struct sk_buff *,skb,s32,len_diff,u32,mode,u64,flags)3659 BPF_CALL_4(sk_skb_adjust_room, struct sk_buff *, skb, s32, len_diff,
3660 u32, mode, u64, flags)
3661 {
3662 u32 len_diff_abs = abs(len_diff);
3663 bool shrink = len_diff < 0;
3664 int ret = 0;
3665
3666 if (unlikely(flags || mode))
3667 return -EINVAL;
3668 if (unlikely(len_diff_abs > 0xfffU))
3669 return -EFAULT;
3670
3671 if (!shrink) {
3672 ret = skb_cow(skb, len_diff);
3673 if (unlikely(ret < 0))
3674 return ret;
3675 __skb_push(skb, len_diff_abs);
3676 memset(skb->data, 0, len_diff_abs);
3677 } else {
3678 if (unlikely(!pskb_may_pull(skb, len_diff_abs)))
3679 return -ENOMEM;
3680 __skb_pull(skb, len_diff_abs);
3681 }
3682 if (tls_sw_has_ctx_rx(skb->sk)) {
3683 struct strp_msg *rxm = strp_msg(skb);
3684
3685 rxm->full_len += len_diff;
3686 }
3687 return ret;
3688 }
3689
3690 static const struct bpf_func_proto sk_skb_adjust_room_proto = {
3691 .func = sk_skb_adjust_room,
3692 .gpl_only = false,
3693 .ret_type = RET_INTEGER,
3694 .arg1_type = ARG_PTR_TO_CTX,
3695 .arg2_type = ARG_ANYTHING,
3696 .arg3_type = ARG_ANYTHING,
3697 .arg4_type = ARG_ANYTHING,
3698 };
3699
BPF_CALL_4(bpf_skb_adjust_room,struct sk_buff *,skb,s32,len_diff,u32,mode,u64,flags)3700 BPF_CALL_4(bpf_skb_adjust_room, struct sk_buff *, skb, s32, len_diff,
3701 u32, mode, u64, flags)
3702 {
3703 u32 len_cur, len_diff_abs = abs(len_diff);
3704 u32 len_min = bpf_skb_net_base_len(skb);
3705 u32 len_max = BPF_SKB_MAX_LEN;
3706 __be16 proto = skb->protocol;
3707 bool shrink = len_diff < 0;
3708 u32 off;
3709 int ret;
3710
3711 if (unlikely(flags & ~(BPF_F_ADJ_ROOM_MASK |
3712 BPF_F_ADJ_ROOM_NO_CSUM_RESET)))
3713 return -EINVAL;
3714 if (unlikely(len_diff_abs > 0xfffU))
3715 return -EFAULT;
3716 if (unlikely(proto != htons(ETH_P_IP) &&
3717 proto != htons(ETH_P_IPV6)))
3718 return -ENOTSUPP;
3719
3720 off = skb_mac_header_len(skb);
3721 switch (mode) {
3722 case BPF_ADJ_ROOM_NET:
3723 off += bpf_skb_net_base_len(skb);
3724 break;
3725 case BPF_ADJ_ROOM_MAC:
3726 break;
3727 default:
3728 return -ENOTSUPP;
3729 }
3730
3731 if (flags & BPF_F_ADJ_ROOM_DECAP_L3_MASK) {
3732 if (!shrink)
3733 return -EINVAL;
3734
3735 switch (flags & BPF_F_ADJ_ROOM_DECAP_L3_MASK) {
3736 case BPF_F_ADJ_ROOM_DECAP_L3_IPV4:
3737 len_min = sizeof(struct iphdr);
3738 break;
3739 case BPF_F_ADJ_ROOM_DECAP_L3_IPV6:
3740 len_min = sizeof(struct ipv6hdr);
3741 break;
3742 default:
3743 return -EINVAL;
3744 }
3745 }
3746
3747 len_cur = skb->len - skb_network_offset(skb);
3748 if ((shrink && (len_diff_abs >= len_cur ||
3749 len_cur - len_diff_abs < len_min)) ||
3750 (!shrink && (skb->len + len_diff_abs > len_max &&
3751 !skb_is_gso(skb))))
3752 return -ENOTSUPP;
3753
3754 ret = shrink ? bpf_skb_net_shrink(skb, off, len_diff_abs, flags) :
3755 bpf_skb_net_grow(skb, off, len_diff_abs, flags);
3756 if (!ret && !(flags & BPF_F_ADJ_ROOM_NO_CSUM_RESET))
3757 __skb_reset_checksum_unnecessary(skb);
3758
3759 bpf_compute_data_pointers(skb);
3760 return ret;
3761 }
3762
3763 static const struct bpf_func_proto bpf_skb_adjust_room_proto = {
3764 .func = bpf_skb_adjust_room,
3765 .gpl_only = false,
3766 .ret_type = RET_INTEGER,
3767 .arg1_type = ARG_PTR_TO_CTX,
3768 .arg2_type = ARG_ANYTHING,
3769 .arg3_type = ARG_ANYTHING,
3770 .arg4_type = ARG_ANYTHING,
3771 };
3772
__bpf_skb_min_len(const struct sk_buff * skb)3773 static u32 __bpf_skb_min_len(const struct sk_buff *skb)
3774 {
3775 int offset = skb_network_offset(skb);
3776 u32 min_len = 0;
3777
3778 if (offset > 0)
3779 min_len = offset;
3780 if (skb_transport_header_was_set(skb)) {
3781 offset = skb_transport_offset(skb);
3782 if (offset > 0)
3783 min_len = offset;
3784 }
3785 if (skb->ip_summed == CHECKSUM_PARTIAL) {
3786 offset = skb_checksum_start_offset(skb) +
3787 skb->csum_offset + sizeof(__sum16);
3788 if (offset > 0)
3789 min_len = offset;
3790 }
3791 return min_len;
3792 }
3793
bpf_skb_grow_rcsum(struct sk_buff * skb,unsigned int new_len)3794 static int bpf_skb_grow_rcsum(struct sk_buff *skb, unsigned int new_len)
3795 {
3796 unsigned int old_len = skb->len;
3797 int ret;
3798
3799 ret = __skb_grow_rcsum(skb, new_len);
3800 if (!ret)
3801 memset(skb->data + old_len, 0, new_len - old_len);
3802 return ret;
3803 }
3804
bpf_skb_trim_rcsum(struct sk_buff * skb,unsigned int new_len)3805 static int bpf_skb_trim_rcsum(struct sk_buff *skb, unsigned int new_len)
3806 {
3807 return __skb_trim_rcsum(skb, new_len);
3808 }
3809
__bpf_skb_change_tail(struct sk_buff * skb,u32 new_len,u64 flags)3810 static inline int __bpf_skb_change_tail(struct sk_buff *skb, u32 new_len,
3811 u64 flags)
3812 {
3813 u32 max_len = BPF_SKB_MAX_LEN;
3814 u32 min_len = __bpf_skb_min_len(skb);
3815 int ret;
3816
3817 if (unlikely(flags || new_len > max_len || new_len < min_len))
3818 return -EINVAL;
3819 if (skb->encapsulation)
3820 return -ENOTSUPP;
3821
3822 /* The basic idea of this helper is that it's performing the
3823 * needed work to either grow or trim an skb, and eBPF program
3824 * rewrites the rest via helpers like bpf_skb_store_bytes(),
3825 * bpf_lX_csum_replace() and others rather than passing a raw
3826 * buffer here. This one is a slow path helper and intended
3827 * for replies with control messages.
3828 *
3829 * Like in bpf_skb_change_proto(), we want to keep this rather
3830 * minimal and without protocol specifics so that we are able
3831 * to separate concerns as in bpf_skb_store_bytes() should only
3832 * be the one responsible for writing buffers.
3833 *
3834 * It's really expected to be a slow path operation here for
3835 * control message replies, so we're implicitly linearizing,
3836 * uncloning and drop offloads from the skb by this.
3837 */
3838 ret = __bpf_try_make_writable(skb, skb->len);
3839 if (!ret) {
3840 if (new_len > skb->len)
3841 ret = bpf_skb_grow_rcsum(skb, new_len);
3842 else if (new_len < skb->len)
3843 ret = bpf_skb_trim_rcsum(skb, new_len);
3844 if (!ret && skb_is_gso(skb))
3845 skb_gso_reset(skb);
3846 }
3847 return ret;
3848 }
3849
BPF_CALL_3(bpf_skb_change_tail,struct sk_buff *,skb,u32,new_len,u64,flags)3850 BPF_CALL_3(bpf_skb_change_tail, struct sk_buff *, skb, u32, new_len,
3851 u64, flags)
3852 {
3853 int ret = __bpf_skb_change_tail(skb, new_len, flags);
3854
3855 bpf_compute_data_pointers(skb);
3856 return ret;
3857 }
3858
3859 static const struct bpf_func_proto bpf_skb_change_tail_proto = {
3860 .func = bpf_skb_change_tail,
3861 .gpl_only = false,
3862 .ret_type = RET_INTEGER,
3863 .arg1_type = ARG_PTR_TO_CTX,
3864 .arg2_type = ARG_ANYTHING,
3865 .arg3_type = ARG_ANYTHING,
3866 };
3867
BPF_CALL_3(sk_skb_change_tail,struct sk_buff *,skb,u32,new_len,u64,flags)3868 BPF_CALL_3(sk_skb_change_tail, struct sk_buff *, skb, u32, new_len,
3869 u64, flags)
3870 {
3871 return __bpf_skb_change_tail(skb, new_len, flags);
3872 }
3873
3874 static const struct bpf_func_proto sk_skb_change_tail_proto = {
3875 .func = sk_skb_change_tail,
3876 .gpl_only = false,
3877 .ret_type = RET_INTEGER,
3878 .arg1_type = ARG_PTR_TO_CTX,
3879 .arg2_type = ARG_ANYTHING,
3880 .arg3_type = ARG_ANYTHING,
3881 };
3882
__bpf_skb_change_head(struct sk_buff * skb,u32 head_room,u64 flags)3883 static inline int __bpf_skb_change_head(struct sk_buff *skb, u32 head_room,
3884 u64 flags)
3885 {
3886 const u8 meta_len = skb_metadata_len(skb);
3887 u32 max_len = BPF_SKB_MAX_LEN;
3888 u32 new_len = skb->len + head_room;
3889 int ret;
3890
3891 if (unlikely(flags || (int)head_room < 0 ||
3892 (!skb_is_gso(skb) && new_len > max_len) ||
3893 new_len < skb->len))
3894 return -EINVAL;
3895
3896 ret = skb_cow(skb, meta_len + head_room);
3897 if (likely(!ret)) {
3898 /* Idea for this helper is that we currently only
3899 * allow to expand on mac header. This means that
3900 * skb->protocol network header, etc, stay as is.
3901 * Compared to bpf_skb_change_tail(), we're more
3902 * flexible due to not needing to linearize or
3903 * reset GSO. Intention for this helper is to be
3904 * used by an L3 skb that needs to push mac header
3905 * for redirection into L2 device.
3906 */
3907 __skb_push(skb, head_room);
3908 skb_postpush_data_move(skb, head_room, 0);
3909 memset(skb->data, 0, head_room);
3910 skb_reset_mac_header(skb);
3911 skb_reset_mac_len(skb);
3912 }
3913
3914 return ret;
3915 }
3916
BPF_CALL_3(bpf_skb_change_head,struct sk_buff *,skb,u32,head_room,u64,flags)3917 BPF_CALL_3(bpf_skb_change_head, struct sk_buff *, skb, u32, head_room,
3918 u64, flags)
3919 {
3920 int ret = __bpf_skb_change_head(skb, head_room, flags);
3921
3922 bpf_compute_data_pointers(skb);
3923 return ret;
3924 }
3925
3926 static const struct bpf_func_proto bpf_skb_change_head_proto = {
3927 .func = bpf_skb_change_head,
3928 .gpl_only = false,
3929 .ret_type = RET_INTEGER,
3930 .arg1_type = ARG_PTR_TO_CTX,
3931 .arg2_type = ARG_ANYTHING,
3932 .arg3_type = ARG_ANYTHING,
3933 };
3934
BPF_CALL_3(sk_skb_change_head,struct sk_buff *,skb,u32,head_room,u64,flags)3935 BPF_CALL_3(sk_skb_change_head, struct sk_buff *, skb, u32, head_room,
3936 u64, flags)
3937 {
3938 return __bpf_skb_change_head(skb, head_room, flags);
3939 }
3940
3941 static const struct bpf_func_proto sk_skb_change_head_proto = {
3942 .func = sk_skb_change_head,
3943 .gpl_only = false,
3944 .ret_type = RET_INTEGER,
3945 .arg1_type = ARG_PTR_TO_CTX,
3946 .arg2_type = ARG_ANYTHING,
3947 .arg3_type = ARG_ANYTHING,
3948 };
3949
BPF_CALL_1(bpf_xdp_get_buff_len,struct xdp_buff *,xdp)3950 BPF_CALL_1(bpf_xdp_get_buff_len, struct xdp_buff*, xdp)
3951 {
3952 return xdp_get_buff_len(xdp);
3953 }
3954
3955 static const struct bpf_func_proto bpf_xdp_get_buff_len_proto = {
3956 .func = bpf_xdp_get_buff_len,
3957 .gpl_only = false,
3958 .ret_type = RET_INTEGER,
3959 .arg1_type = ARG_PTR_TO_CTX,
3960 };
3961
3962 BTF_ID_LIST_SINGLE(bpf_xdp_get_buff_len_bpf_ids, struct, xdp_buff)
3963
3964 const struct bpf_func_proto bpf_xdp_get_buff_len_trace_proto = {
3965 .func = bpf_xdp_get_buff_len,
3966 .gpl_only = false,
3967 .arg1_type = ARG_PTR_TO_BTF_ID,
3968 .arg1_btf_id = &bpf_xdp_get_buff_len_bpf_ids[0],
3969 };
3970
xdp_get_metalen(const struct xdp_buff * xdp)3971 static unsigned long xdp_get_metalen(const struct xdp_buff *xdp)
3972 {
3973 return xdp_data_meta_unsupported(xdp) ? 0 :
3974 xdp->data - xdp->data_meta;
3975 }
3976
BPF_CALL_2(bpf_xdp_adjust_head,struct xdp_buff *,xdp,int,offset)3977 BPF_CALL_2(bpf_xdp_adjust_head, struct xdp_buff *, xdp, int, offset)
3978 {
3979 void *xdp_frame_end = xdp->data_hard_start + sizeof(struct xdp_frame);
3980 unsigned long metalen = xdp_get_metalen(xdp);
3981 void *data_start = xdp_frame_end + metalen;
3982 void *data = xdp->data + offset;
3983
3984 if (unlikely(data < data_start ||
3985 data > xdp->data_end - ETH_HLEN))
3986 return -EINVAL;
3987
3988 if (metalen)
3989 memmove(xdp->data_meta + offset,
3990 xdp->data_meta, metalen);
3991 xdp->data_meta += offset;
3992 xdp->data = data;
3993
3994 return 0;
3995 }
3996
3997 static const struct bpf_func_proto bpf_xdp_adjust_head_proto = {
3998 .func = bpf_xdp_adjust_head,
3999 .gpl_only = false,
4000 .ret_type = RET_INTEGER,
4001 .arg1_type = ARG_PTR_TO_CTX,
4002 .arg2_type = ARG_ANYTHING,
4003 };
4004
bpf_xdp_copy_buf(struct xdp_buff * xdp,unsigned long off,void * buf,unsigned long len,bool flush)4005 void bpf_xdp_copy_buf(struct xdp_buff *xdp, unsigned long off,
4006 void *buf, unsigned long len, bool flush)
4007 {
4008 unsigned long ptr_len, ptr_off = 0;
4009 skb_frag_t *next_frag, *end_frag;
4010 struct skb_shared_info *sinfo;
4011 void *src, *dst;
4012 u8 *ptr_buf;
4013
4014 if (likely(xdp->data_end - xdp->data >= off + len)) {
4015 src = flush ? buf : xdp->data + off;
4016 dst = flush ? xdp->data + off : buf;
4017 memcpy(dst, src, len);
4018 return;
4019 }
4020
4021 sinfo = xdp_get_shared_info_from_buff(xdp);
4022 end_frag = &sinfo->frags[sinfo->nr_frags];
4023 next_frag = &sinfo->frags[0];
4024
4025 ptr_len = xdp->data_end - xdp->data;
4026 ptr_buf = xdp->data;
4027
4028 while (true) {
4029 if (off < ptr_off + ptr_len) {
4030 unsigned long copy_off = off - ptr_off;
4031 unsigned long copy_len = min(len, ptr_len - copy_off);
4032
4033 src = flush ? buf : ptr_buf + copy_off;
4034 dst = flush ? ptr_buf + copy_off : buf;
4035 memcpy(dst, src, copy_len);
4036
4037 off += copy_len;
4038 len -= copy_len;
4039 buf += copy_len;
4040 }
4041
4042 if (!len || next_frag == end_frag)
4043 break;
4044
4045 ptr_off += ptr_len;
4046 ptr_buf = skb_frag_address(next_frag);
4047 ptr_len = skb_frag_size(next_frag);
4048 next_frag++;
4049 }
4050 }
4051
bpf_xdp_pointer(struct xdp_buff * xdp,u32 offset,u32 len)4052 void *bpf_xdp_pointer(struct xdp_buff *xdp, u32 offset, u32 len)
4053 {
4054 u32 size = xdp->data_end - xdp->data;
4055 struct skb_shared_info *sinfo;
4056 void *addr = xdp->data;
4057 int i;
4058
4059 if (unlikely(offset > 0xffff || len > 0xffff))
4060 return ERR_PTR(-EFAULT);
4061
4062 if (unlikely(offset + len > xdp_get_buff_len(xdp)))
4063 return ERR_PTR(-EINVAL);
4064
4065 if (likely(offset < size)) /* linear area */
4066 goto out;
4067
4068 sinfo = xdp_get_shared_info_from_buff(xdp);
4069 offset -= size;
4070 for (i = 0; i < sinfo->nr_frags; i++) { /* paged area */
4071 u32 frag_size = skb_frag_size(&sinfo->frags[i]);
4072
4073 if (offset < frag_size) {
4074 addr = skb_frag_address(&sinfo->frags[i]);
4075 size = frag_size;
4076 break;
4077 }
4078 offset -= frag_size;
4079 }
4080 out:
4081 return offset + len <= size ? addr + offset : NULL;
4082 }
4083
BPF_CALL_4(bpf_xdp_load_bytes,struct xdp_buff *,xdp,u32,offset,void *,buf,u32,len)4084 BPF_CALL_4(bpf_xdp_load_bytes, struct xdp_buff *, xdp, u32, offset,
4085 void *, buf, u32, len)
4086 {
4087 void *ptr;
4088
4089 ptr = bpf_xdp_pointer(xdp, offset, len);
4090 if (IS_ERR(ptr))
4091 return PTR_ERR(ptr);
4092
4093 if (!ptr)
4094 bpf_xdp_copy_buf(xdp, offset, buf, len, false);
4095 else
4096 memcpy(buf, ptr, len);
4097
4098 return 0;
4099 }
4100
4101 static const struct bpf_func_proto bpf_xdp_load_bytes_proto = {
4102 .func = bpf_xdp_load_bytes,
4103 .gpl_only = false,
4104 .ret_type = RET_INTEGER,
4105 .arg1_type = ARG_PTR_TO_CTX,
4106 .arg2_type = ARG_ANYTHING,
4107 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
4108 .arg4_type = ARG_CONST_SIZE,
4109 };
4110
__bpf_xdp_load_bytes(struct xdp_buff * xdp,u32 offset,void * buf,u32 len)4111 int __bpf_xdp_load_bytes(struct xdp_buff *xdp, u32 offset, void *buf, u32 len)
4112 {
4113 return ____bpf_xdp_load_bytes(xdp, offset, buf, len);
4114 }
4115
BPF_CALL_4(bpf_xdp_store_bytes,struct xdp_buff *,xdp,u32,offset,void *,buf,u32,len)4116 BPF_CALL_4(bpf_xdp_store_bytes, struct xdp_buff *, xdp, u32, offset,
4117 void *, buf, u32, len)
4118 {
4119 void *ptr;
4120
4121 ptr = bpf_xdp_pointer(xdp, offset, len);
4122 if (IS_ERR(ptr))
4123 return PTR_ERR(ptr);
4124
4125 if (!ptr)
4126 bpf_xdp_copy_buf(xdp, offset, buf, len, true);
4127 else
4128 memcpy(ptr, buf, len);
4129
4130 return 0;
4131 }
4132
4133 static const struct bpf_func_proto bpf_xdp_store_bytes_proto = {
4134 .func = bpf_xdp_store_bytes,
4135 .gpl_only = false,
4136 .ret_type = RET_INTEGER,
4137 .arg1_type = ARG_PTR_TO_CTX,
4138 .arg2_type = ARG_ANYTHING,
4139 .arg3_type = ARG_PTR_TO_MEM | MEM_RDONLY,
4140 .arg4_type = ARG_CONST_SIZE,
4141 };
4142
__bpf_xdp_store_bytes(struct xdp_buff * xdp,u32 offset,void * buf,u32 len)4143 int __bpf_xdp_store_bytes(struct xdp_buff *xdp, u32 offset, void *buf, u32 len)
4144 {
4145 return ____bpf_xdp_store_bytes(xdp, offset, buf, len);
4146 }
4147
bpf_xdp_frags_increase_tail(struct xdp_buff * xdp,int offset)4148 static int bpf_xdp_frags_increase_tail(struct xdp_buff *xdp, int offset)
4149 {
4150 struct skb_shared_info *sinfo = xdp_get_shared_info_from_buff(xdp);
4151 skb_frag_t *frag = &sinfo->frags[sinfo->nr_frags - 1];
4152 struct xdp_rxq_info *rxq = xdp->rxq;
4153 unsigned int tailroom;
4154
4155 if (!rxq->frag_size || rxq->frag_size > xdp->frame_sz)
4156 return -EOPNOTSUPP;
4157
4158 tailroom = rxq->frag_size - skb_frag_size(frag) - skb_frag_off(frag);
4159 if (unlikely(offset > tailroom))
4160 return -EINVAL;
4161
4162 memset(skb_frag_address(frag) + skb_frag_size(frag), 0, offset);
4163 skb_frag_size_add(frag, offset);
4164 sinfo->xdp_frags_size += offset;
4165 if (rxq->mem.type == MEM_TYPE_XSK_BUFF_POOL)
4166 xsk_buff_get_tail(xdp)->data_end += offset;
4167
4168 return 0;
4169 }
4170
bpf_xdp_shrink_data_zc(struct xdp_buff * xdp,int shrink,bool tail,bool release)4171 static struct xdp_buff *bpf_xdp_shrink_data_zc(struct xdp_buff *xdp, int shrink,
4172 bool tail, bool release)
4173 {
4174 struct xdp_buff *zc_frag = tail ? xsk_buff_get_tail(xdp) :
4175 xsk_buff_get_head(xdp);
4176
4177 if (release) {
4178 xsk_buff_del_frag(zc_frag);
4179 } else {
4180 if (tail)
4181 zc_frag->data_end -= shrink;
4182 else
4183 zc_frag->data += shrink;
4184 }
4185
4186 return zc_frag;
4187 }
4188
bpf_xdp_shrink_data(struct xdp_buff * xdp,skb_frag_t * frag,int shrink,bool tail)4189 static bool bpf_xdp_shrink_data(struct xdp_buff *xdp, skb_frag_t *frag,
4190 int shrink, bool tail)
4191 {
4192 enum xdp_mem_type mem_type = xdp->rxq->mem.type;
4193 bool release = skb_frag_size(frag) == shrink;
4194 netmem_ref netmem = skb_frag_netmem(frag);
4195 struct xdp_buff *zc_frag = NULL;
4196
4197 if (mem_type == MEM_TYPE_XSK_BUFF_POOL) {
4198 netmem = 0;
4199 zc_frag = bpf_xdp_shrink_data_zc(xdp, shrink, tail, release);
4200 }
4201
4202 if (release) {
4203 __xdp_return(netmem, mem_type, false, zc_frag);
4204 } else {
4205 if (!tail)
4206 skb_frag_off_add(frag, shrink);
4207 skb_frag_size_sub(frag, shrink);
4208 }
4209
4210 return release;
4211 }
4212
bpf_xdp_frags_shrink_tail(struct xdp_buff * xdp,int offset)4213 static int bpf_xdp_frags_shrink_tail(struct xdp_buff *xdp, int offset)
4214 {
4215 struct skb_shared_info *sinfo = xdp_get_shared_info_from_buff(xdp);
4216 int i, n_frags_free = 0, len_free = 0;
4217
4218 if (unlikely(offset > (int)xdp_get_buff_len(xdp) - ETH_HLEN))
4219 return -EINVAL;
4220
4221 for (i = sinfo->nr_frags - 1; i >= 0 && offset > 0; i--) {
4222 skb_frag_t *frag = &sinfo->frags[i];
4223 int shrink = min_t(int, offset, skb_frag_size(frag));
4224
4225 len_free += shrink;
4226 offset -= shrink;
4227 if (bpf_xdp_shrink_data(xdp, frag, shrink, true))
4228 n_frags_free++;
4229 }
4230 sinfo->nr_frags -= n_frags_free;
4231 sinfo->xdp_frags_size -= len_free;
4232
4233 if (unlikely(!sinfo->nr_frags)) {
4234 xdp_buff_clear_frags_flag(xdp);
4235 xdp_buff_clear_frag_pfmemalloc(xdp);
4236 xdp->data_end -= offset;
4237 }
4238
4239 return 0;
4240 }
4241
BPF_CALL_2(bpf_xdp_adjust_tail,struct xdp_buff *,xdp,int,offset)4242 BPF_CALL_2(bpf_xdp_adjust_tail, struct xdp_buff *, xdp, int, offset)
4243 {
4244 void *data_hard_end = xdp_data_hard_end(xdp); /* use xdp->frame_sz */
4245 void *data_end = xdp->data_end + offset;
4246
4247 if (unlikely(xdp_buff_has_frags(xdp))) { /* non-linear xdp buff */
4248 if (offset < 0)
4249 return bpf_xdp_frags_shrink_tail(xdp, -offset);
4250
4251 return bpf_xdp_frags_increase_tail(xdp, offset);
4252 }
4253
4254 /* Notice that xdp_data_hard_end have reserved some tailroom */
4255 if (unlikely(data_end > data_hard_end))
4256 return -EINVAL;
4257
4258 if (unlikely(data_end < xdp->data + ETH_HLEN))
4259 return -EINVAL;
4260
4261 /* Clear memory area on grow, can contain uninit kernel memory */
4262 if (offset > 0)
4263 memset(xdp->data_end, 0, offset);
4264
4265 xdp->data_end = data_end;
4266
4267 return 0;
4268 }
4269
4270 static const struct bpf_func_proto bpf_xdp_adjust_tail_proto = {
4271 .func = bpf_xdp_adjust_tail,
4272 .gpl_only = false,
4273 .ret_type = RET_INTEGER,
4274 .arg1_type = ARG_PTR_TO_CTX,
4275 .arg2_type = ARG_ANYTHING,
4276 };
4277
BPF_CALL_2(bpf_xdp_adjust_meta,struct xdp_buff *,xdp,int,offset)4278 BPF_CALL_2(bpf_xdp_adjust_meta, struct xdp_buff *, xdp, int, offset)
4279 {
4280 void *xdp_frame_end = xdp->data_hard_start + sizeof(struct xdp_frame);
4281 void *meta = xdp->data_meta + offset;
4282 unsigned long metalen = xdp->data - meta;
4283
4284 if (xdp_data_meta_unsupported(xdp))
4285 return -ENOTSUPP;
4286 if (unlikely(meta < xdp_frame_end ||
4287 meta > xdp->data))
4288 return -EINVAL;
4289 if (unlikely(xdp_metalen_invalid(metalen)))
4290 return -EACCES;
4291
4292 xdp->data_meta = meta;
4293
4294 return 0;
4295 }
4296
4297 static const struct bpf_func_proto bpf_xdp_adjust_meta_proto = {
4298 .func = bpf_xdp_adjust_meta,
4299 .gpl_only = false,
4300 .ret_type = RET_INTEGER,
4301 .arg1_type = ARG_PTR_TO_CTX,
4302 .arg2_type = ARG_ANYTHING,
4303 };
4304
4305 /**
4306 * DOC: xdp redirect
4307 *
4308 * XDP_REDIRECT works by a three-step process, implemented in the functions
4309 * below:
4310 *
4311 * 1. The bpf_redirect() and bpf_redirect_map() helpers will lookup the target
4312 * of the redirect and store it (along with some other metadata) in a per-CPU
4313 * struct bpf_redirect_info.
4314 *
4315 * 2. When the program returns the XDP_REDIRECT return code, the driver will
4316 * call xdp_do_redirect() which will use the information in struct
4317 * bpf_redirect_info to actually enqueue the frame into a map type-specific
4318 * bulk queue structure.
4319 *
4320 * 3. Before exiting its NAPI poll loop, the driver will call
4321 * xdp_do_flush(), which will flush all the different bulk queues,
4322 * thus completing the redirect. Note that xdp_do_flush() must be
4323 * called before napi_complete_done() in the driver, as the
4324 * XDP_REDIRECT logic relies on being inside a single NAPI instance
4325 * through to the xdp_do_flush() call for RCU protection of all
4326 * in-kernel data structures.
4327 */
4328 /*
4329 * Pointers to the map entries will be kept around for this whole sequence of
4330 * steps, protected by RCU. However, there is no top-level rcu_read_lock() in
4331 * the core code; instead, the RCU protection relies on everything happening
4332 * inside a single NAPI poll sequence, which means it's between a pair of calls
4333 * to local_bh_disable()/local_bh_enable().
4334 *
4335 * The map entries are marked as __rcu and the map code makes sure to
4336 * dereference those pointers with rcu_dereference_check() in a way that works
4337 * for both sections that to hold an rcu_read_lock() and sections that are
4338 * called from NAPI without a separate rcu_read_lock(). The code below does not
4339 * use RCU annotations, but relies on those in the map code.
4340 */
xdp_do_flush(void)4341 void xdp_do_flush(void)
4342 {
4343 struct list_head *lh_map, *lh_dev, *lh_xsk;
4344
4345 bpf_net_ctx_get_all_used_flush_lists(&lh_map, &lh_dev, &lh_xsk);
4346 if (lh_dev)
4347 __dev_flush(lh_dev);
4348 if (lh_map)
4349 __cpu_map_flush(lh_map);
4350 if (lh_xsk)
4351 __xsk_map_flush(lh_xsk);
4352 }
4353 EXPORT_SYMBOL_GPL(xdp_do_flush);
4354
4355 #if defined(CONFIG_DEBUG_NET) && defined(CONFIG_BPF_SYSCALL)
xdp_do_check_flushed(struct napi_struct * napi)4356 void xdp_do_check_flushed(struct napi_struct *napi)
4357 {
4358 struct list_head *lh_map, *lh_dev, *lh_xsk;
4359 bool missed = false;
4360
4361 bpf_net_ctx_get_all_used_flush_lists(&lh_map, &lh_dev, &lh_xsk);
4362 if (lh_dev) {
4363 __dev_flush(lh_dev);
4364 missed = true;
4365 }
4366 if (lh_map) {
4367 __cpu_map_flush(lh_map);
4368 missed = true;
4369 }
4370 if (lh_xsk) {
4371 __xsk_map_flush(lh_xsk);
4372 missed = true;
4373 }
4374
4375 WARN_ONCE(missed, "Missing xdp_do_flush() invocation after NAPI by %ps\n",
4376 napi->poll);
4377 }
4378 #endif
4379
4380 DEFINE_STATIC_KEY_FALSE(bpf_master_redirect_enabled_key);
4381 EXPORT_SYMBOL_GPL(bpf_master_redirect_enabled_key);
4382
xdp_master_redirect(struct xdp_buff * xdp)4383 u32 xdp_master_redirect(struct xdp_buff *xdp)
4384 {
4385 struct bpf_redirect_info *ri = bpf_net_ctx_get_ri();
4386 struct net_device *master, *slave;
4387
4388 master = netdev_master_upper_dev_get_rcu(xdp->rxq->dev);
4389 slave = master->netdev_ops->ndo_xdp_get_xmit_slave(master, xdp);
4390 if (slave && slave != xdp->rxq->dev) {
4391 /* The target device is different from the receiving device, so
4392 * redirect it to the new device.
4393 * Using XDP_REDIRECT gets the correct behaviour from XDP enabled
4394 * drivers to unmap the packet from their rx ring.
4395 */
4396 ri->tgt_index = slave->ifindex;
4397 ri->map_id = INT_MAX;
4398 ri->map_type = BPF_MAP_TYPE_UNSPEC;
4399 return XDP_REDIRECT;
4400 }
4401 return XDP_TX;
4402 }
4403 EXPORT_SYMBOL_GPL(xdp_master_redirect);
4404
__xdp_do_redirect_xsk(struct bpf_redirect_info * ri,const struct net_device * dev,struct xdp_buff * xdp,const struct bpf_prog * xdp_prog)4405 static inline int __xdp_do_redirect_xsk(struct bpf_redirect_info *ri,
4406 const struct net_device *dev,
4407 struct xdp_buff *xdp,
4408 const struct bpf_prog *xdp_prog)
4409 {
4410 enum bpf_map_type map_type = ri->map_type;
4411 void *fwd = ri->tgt_value;
4412 u32 map_id = ri->map_id;
4413 int err;
4414
4415 ri->map_id = 0; /* Valid map id idr range: [1,INT_MAX[ */
4416 ri->map_type = BPF_MAP_TYPE_UNSPEC;
4417
4418 err = __xsk_map_redirect(fwd, xdp);
4419 if (unlikely(err))
4420 goto err;
4421
4422 _trace_xdp_redirect_map(dev, xdp_prog, fwd, map_type, map_id, ri->tgt_index);
4423 return 0;
4424 err:
4425 _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map_type, map_id, ri->tgt_index, err);
4426 return err;
4427 }
4428
4429 static __always_inline int
__xdp_do_redirect_frame(struct bpf_redirect_info * ri,struct net_device * dev,struct xdp_frame * xdpf,const struct bpf_prog * xdp_prog)4430 __xdp_do_redirect_frame(struct bpf_redirect_info *ri, struct net_device *dev,
4431 struct xdp_frame *xdpf,
4432 const struct bpf_prog *xdp_prog)
4433 {
4434 enum bpf_map_type map_type = ri->map_type;
4435 void *fwd = ri->tgt_value;
4436 u32 map_id = ri->map_id;
4437 u32 flags = ri->flags;
4438 struct bpf_map *map;
4439 int err;
4440
4441 ri->map_id = 0; /* Valid map id idr range: [1,INT_MAX[ */
4442 ri->flags = 0;
4443 ri->map_type = BPF_MAP_TYPE_UNSPEC;
4444
4445 if (unlikely(!xdpf)) {
4446 err = -EOVERFLOW;
4447 goto err;
4448 }
4449
4450 switch (map_type) {
4451 case BPF_MAP_TYPE_DEVMAP:
4452 fallthrough;
4453 case BPF_MAP_TYPE_DEVMAP_HASH:
4454 if (unlikely(flags & BPF_F_BROADCAST)) {
4455 map = READ_ONCE(ri->map);
4456
4457 /* The map pointer is cleared when the map is being torn
4458 * down by dev_map_free()
4459 */
4460 if (unlikely(!map)) {
4461 err = -ENOENT;
4462 break;
4463 }
4464
4465 WRITE_ONCE(ri->map, NULL);
4466 err = dev_map_enqueue_multi(xdpf, dev, map,
4467 flags & BPF_F_EXCLUDE_INGRESS);
4468 } else {
4469 err = dev_map_enqueue(fwd, xdpf, dev);
4470 }
4471 break;
4472 case BPF_MAP_TYPE_CPUMAP:
4473 err = cpu_map_enqueue(fwd, xdpf, dev);
4474 break;
4475 case BPF_MAP_TYPE_UNSPEC:
4476 if (map_id == INT_MAX) {
4477 fwd = dev_get_by_index_rcu(dev_net(dev), ri->tgt_index);
4478 if (unlikely(!fwd)) {
4479 err = -EINVAL;
4480 break;
4481 }
4482 err = dev_xdp_enqueue(fwd, xdpf, dev);
4483 break;
4484 }
4485 fallthrough;
4486 default:
4487 err = -EBADRQC;
4488 }
4489
4490 if (unlikely(err))
4491 goto err;
4492
4493 _trace_xdp_redirect_map(dev, xdp_prog, fwd, map_type, map_id, ri->tgt_index);
4494 return 0;
4495 err:
4496 _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map_type, map_id, ri->tgt_index, err);
4497 return err;
4498 }
4499
xdp_do_redirect(struct net_device * dev,struct xdp_buff * xdp,const struct bpf_prog * xdp_prog)4500 int xdp_do_redirect(struct net_device *dev, struct xdp_buff *xdp,
4501 const struct bpf_prog *xdp_prog)
4502 {
4503 struct bpf_redirect_info *ri = bpf_net_ctx_get_ri();
4504 enum bpf_map_type map_type = ri->map_type;
4505
4506 if (map_type == BPF_MAP_TYPE_XSKMAP)
4507 return __xdp_do_redirect_xsk(ri, dev, xdp, xdp_prog);
4508
4509 return __xdp_do_redirect_frame(ri, dev, xdp_convert_buff_to_frame(xdp),
4510 xdp_prog);
4511 }
4512 EXPORT_SYMBOL_GPL(xdp_do_redirect);
4513
xdp_do_redirect_frame(struct net_device * dev,struct xdp_buff * xdp,struct xdp_frame * xdpf,const struct bpf_prog * xdp_prog)4514 int xdp_do_redirect_frame(struct net_device *dev, struct xdp_buff *xdp,
4515 struct xdp_frame *xdpf,
4516 const struct bpf_prog *xdp_prog)
4517 {
4518 struct bpf_redirect_info *ri = bpf_net_ctx_get_ri();
4519 enum bpf_map_type map_type = ri->map_type;
4520
4521 if (map_type == BPF_MAP_TYPE_XSKMAP)
4522 return __xdp_do_redirect_xsk(ri, dev, xdp, xdp_prog);
4523
4524 return __xdp_do_redirect_frame(ri, dev, xdpf, xdp_prog);
4525 }
4526 EXPORT_SYMBOL_GPL(xdp_do_redirect_frame);
4527
xdp_do_generic_redirect_map(struct net_device * dev,struct sk_buff * skb,struct xdp_buff * xdp,const struct bpf_prog * xdp_prog,void * fwd,enum bpf_map_type map_type,u32 map_id,u32 flags)4528 static int xdp_do_generic_redirect_map(struct net_device *dev,
4529 struct sk_buff *skb,
4530 struct xdp_buff *xdp,
4531 const struct bpf_prog *xdp_prog,
4532 void *fwd, enum bpf_map_type map_type,
4533 u32 map_id, u32 flags)
4534 {
4535 struct bpf_redirect_info *ri = bpf_net_ctx_get_ri();
4536 struct bpf_map *map;
4537 int err;
4538
4539 switch (map_type) {
4540 case BPF_MAP_TYPE_DEVMAP:
4541 fallthrough;
4542 case BPF_MAP_TYPE_DEVMAP_HASH:
4543 if (unlikely(flags & BPF_F_BROADCAST)) {
4544 map = READ_ONCE(ri->map);
4545
4546 /* The map pointer is cleared when the map is being torn
4547 * down by dev_map_free()
4548 */
4549 if (unlikely(!map)) {
4550 err = -ENOENT;
4551 break;
4552 }
4553
4554 WRITE_ONCE(ri->map, NULL);
4555 err = dev_map_redirect_multi(dev, skb, xdp_prog, map,
4556 flags & BPF_F_EXCLUDE_INGRESS);
4557 } else {
4558 err = dev_map_generic_redirect(fwd, skb, xdp_prog);
4559 }
4560 if (unlikely(err))
4561 goto err;
4562 break;
4563 case BPF_MAP_TYPE_XSKMAP:
4564 err = xsk_generic_rcv(fwd, xdp);
4565 if (err)
4566 goto err;
4567 consume_skb(skb);
4568 break;
4569 case BPF_MAP_TYPE_CPUMAP:
4570 err = cpu_map_generic_redirect(fwd, skb);
4571 if (unlikely(err))
4572 goto err;
4573 break;
4574 default:
4575 err = -EBADRQC;
4576 goto err;
4577 }
4578
4579 _trace_xdp_redirect_map(dev, xdp_prog, fwd, map_type, map_id, ri->tgt_index);
4580 return 0;
4581 err:
4582 _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map_type, map_id, ri->tgt_index, err);
4583 return err;
4584 }
4585
xdp_do_generic_redirect(struct net_device * dev,struct sk_buff * skb,struct xdp_buff * xdp,const struct bpf_prog * xdp_prog)4586 int xdp_do_generic_redirect(struct net_device *dev, struct sk_buff *skb,
4587 struct xdp_buff *xdp,
4588 const struct bpf_prog *xdp_prog)
4589 {
4590 struct bpf_redirect_info *ri = bpf_net_ctx_get_ri();
4591 enum bpf_map_type map_type = ri->map_type;
4592 void *fwd = ri->tgt_value;
4593 u32 map_id = ri->map_id;
4594 u32 flags = ri->flags;
4595 int err;
4596
4597 ri->map_id = 0; /* Valid map id idr range: [1,INT_MAX[ */
4598 ri->flags = 0;
4599 ri->map_type = BPF_MAP_TYPE_UNSPEC;
4600
4601 if (map_type == BPF_MAP_TYPE_UNSPEC && map_id == INT_MAX) {
4602 fwd = dev_get_by_index_rcu(dev_net(dev), ri->tgt_index);
4603 if (unlikely(!fwd)) {
4604 err = -EINVAL;
4605 goto err;
4606 }
4607
4608 err = xdp_ok_fwd_dev(fwd, skb->len);
4609 if (unlikely(err))
4610 goto err;
4611
4612 skb->dev = fwd;
4613 _trace_xdp_redirect(dev, xdp_prog, ri->tgt_index);
4614 generic_xdp_tx(skb, xdp_prog);
4615 return 0;
4616 }
4617
4618 return xdp_do_generic_redirect_map(dev, skb, xdp, xdp_prog, fwd, map_type, map_id, flags);
4619 err:
4620 _trace_xdp_redirect_err(dev, xdp_prog, ri->tgt_index, err);
4621 return err;
4622 }
4623
BPF_CALL_2(bpf_xdp_redirect,u32,ifindex,u64,flags)4624 BPF_CALL_2(bpf_xdp_redirect, u32, ifindex, u64, flags)
4625 {
4626 struct bpf_redirect_info *ri = bpf_net_ctx_get_ri();
4627
4628 if (unlikely(flags))
4629 return XDP_ABORTED;
4630
4631 /* NB! Map type UNSPEC and map_id == INT_MAX (never generated
4632 * by map_idr) is used for ifindex based XDP redirect.
4633 */
4634 ri->tgt_index = ifindex;
4635 ri->map_id = INT_MAX;
4636 ri->map_type = BPF_MAP_TYPE_UNSPEC;
4637
4638 return XDP_REDIRECT;
4639 }
4640
4641 static const struct bpf_func_proto bpf_xdp_redirect_proto = {
4642 .func = bpf_xdp_redirect,
4643 .gpl_only = false,
4644 .ret_type = RET_INTEGER,
4645 .arg1_type = ARG_ANYTHING,
4646 .arg2_type = ARG_ANYTHING,
4647 };
4648
BPF_CALL_3(bpf_xdp_redirect_map,struct bpf_map *,map,u64,key,u64,flags)4649 BPF_CALL_3(bpf_xdp_redirect_map, struct bpf_map *, map, u64, key,
4650 u64, flags)
4651 {
4652 return map->ops->map_redirect(map, key, flags);
4653 }
4654
4655 static const struct bpf_func_proto bpf_xdp_redirect_map_proto = {
4656 .func = bpf_xdp_redirect_map,
4657 .gpl_only = false,
4658 .ret_type = RET_INTEGER,
4659 .arg1_type = ARG_CONST_MAP_PTR,
4660 .arg2_type = ARG_ANYTHING,
4661 .arg3_type = ARG_ANYTHING,
4662 };
4663
bpf_skb_copy(void * dst_buff,const void * skb,unsigned long off,unsigned long len)4664 static unsigned long bpf_skb_copy(void *dst_buff, const void *skb,
4665 unsigned long off, unsigned long len)
4666 {
4667 void *ptr = skb_header_pointer(skb, off, len, dst_buff);
4668
4669 if (unlikely(!ptr))
4670 return len;
4671 if (ptr != dst_buff)
4672 memcpy(dst_buff, ptr, len);
4673
4674 return 0;
4675 }
4676
BPF_CALL_5(bpf_skb_event_output,struct sk_buff *,skb,struct bpf_map *,map,u64,flags,void *,meta,u64,meta_size)4677 BPF_CALL_5(bpf_skb_event_output, struct sk_buff *, skb, struct bpf_map *, map,
4678 u64, flags, void *, meta, u64, meta_size)
4679 {
4680 u64 skb_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
4681
4682 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
4683 return -EINVAL;
4684 if (unlikely(!skb || skb_size > skb->len))
4685 return -EFAULT;
4686
4687 return bpf_event_output(map, flags, meta, meta_size, skb, skb_size,
4688 bpf_skb_copy);
4689 }
4690
4691 static const struct bpf_func_proto bpf_skb_event_output_proto = {
4692 .func = bpf_skb_event_output,
4693 .gpl_only = true,
4694 .ret_type = RET_INTEGER,
4695 .arg1_type = ARG_PTR_TO_CTX,
4696 .arg2_type = ARG_CONST_MAP_PTR,
4697 .arg3_type = ARG_ANYTHING,
4698 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
4699 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
4700 };
4701
4702 BTF_ID_LIST_SINGLE(bpf_skb_output_btf_ids, struct, sk_buff)
4703
4704 const struct bpf_func_proto bpf_skb_output_proto = {
4705 .func = bpf_skb_event_output,
4706 .gpl_only = true,
4707 .ret_type = RET_INTEGER,
4708 .arg1_type = ARG_PTR_TO_BTF_ID,
4709 .arg1_btf_id = &bpf_skb_output_btf_ids[0],
4710 .arg2_type = ARG_CONST_MAP_PTR,
4711 .arg3_type = ARG_ANYTHING,
4712 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
4713 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
4714 };
4715
bpf_tunnel_key_af(u64 flags)4716 static unsigned short bpf_tunnel_key_af(u64 flags)
4717 {
4718 return flags & BPF_F_TUNINFO_IPV6 ? AF_INET6 : AF_INET;
4719 }
4720
BPF_CALL_4(bpf_skb_get_tunnel_key,struct sk_buff *,skb,struct bpf_tunnel_key *,to,u32,size,u64,flags)4721 BPF_CALL_4(bpf_skb_get_tunnel_key, struct sk_buff *, skb, struct bpf_tunnel_key *, to,
4722 u32, size, u64, flags)
4723 {
4724 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
4725 u8 compat[sizeof(struct bpf_tunnel_key)];
4726 void *to_orig = to;
4727 int err;
4728
4729 if (unlikely(!info || (flags & ~(BPF_F_TUNINFO_IPV6 |
4730 BPF_F_TUNINFO_FLAGS)))) {
4731 err = -EINVAL;
4732 goto err_clear;
4733 }
4734 if (ip_tunnel_info_af(info) != bpf_tunnel_key_af(flags)) {
4735 err = -EPROTO;
4736 goto err_clear;
4737 }
4738 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
4739 err = -EINVAL;
4740 switch (size) {
4741 case offsetof(struct bpf_tunnel_key, local_ipv6[0]):
4742 case offsetof(struct bpf_tunnel_key, tunnel_label):
4743 case offsetof(struct bpf_tunnel_key, tunnel_ext):
4744 goto set_compat;
4745 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
4746 /* Fixup deprecated structure layouts here, so we have
4747 * a common path later on.
4748 */
4749 if (ip_tunnel_info_af(info) != AF_INET)
4750 goto err_clear;
4751 set_compat:
4752 to = (struct bpf_tunnel_key *)compat;
4753 break;
4754 default:
4755 goto err_clear;
4756 }
4757 }
4758
4759 to->tunnel_id = be64_to_cpu(info->key.tun_id);
4760 to->tunnel_tos = info->key.tos;
4761 to->tunnel_ttl = info->key.ttl;
4762 if (flags & BPF_F_TUNINFO_FLAGS)
4763 to->tunnel_flags = ip_tunnel_flags_to_be16(info->key.tun_flags);
4764 else
4765 to->tunnel_ext = 0;
4766
4767 if (flags & BPF_F_TUNINFO_IPV6) {
4768 memcpy(to->remote_ipv6, &info->key.u.ipv6.src,
4769 sizeof(to->remote_ipv6));
4770 memcpy(to->local_ipv6, &info->key.u.ipv6.dst,
4771 sizeof(to->local_ipv6));
4772 to->tunnel_label = be32_to_cpu(info->key.label);
4773 } else {
4774 to->remote_ipv4 = be32_to_cpu(info->key.u.ipv4.src);
4775 memset(&to->remote_ipv6[1], 0, sizeof(__u32) * 3);
4776 to->local_ipv4 = be32_to_cpu(info->key.u.ipv4.dst);
4777 memset(&to->local_ipv6[1], 0, sizeof(__u32) * 3);
4778 to->tunnel_label = 0;
4779 }
4780
4781 if (unlikely(size != sizeof(struct bpf_tunnel_key)))
4782 memcpy(to_orig, to, size);
4783
4784 return 0;
4785 err_clear:
4786 memset(to_orig, 0, size);
4787 return err;
4788 }
4789
4790 static const struct bpf_func_proto bpf_skb_get_tunnel_key_proto = {
4791 .func = bpf_skb_get_tunnel_key,
4792 .gpl_only = false,
4793 .ret_type = RET_INTEGER,
4794 .arg1_type = ARG_PTR_TO_CTX,
4795 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
4796 .arg3_type = ARG_CONST_SIZE,
4797 .arg4_type = ARG_ANYTHING,
4798 };
4799
BPF_CALL_3(bpf_skb_get_tunnel_opt,struct sk_buff *,skb,u8 *,to,u32,size)4800 BPF_CALL_3(bpf_skb_get_tunnel_opt, struct sk_buff *, skb, u8 *, to, u32, size)
4801 {
4802 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
4803 int err;
4804
4805 if (unlikely(!info ||
4806 !ip_tunnel_is_options_present(info->key.tun_flags))) {
4807 err = -ENOENT;
4808 goto err_clear;
4809 }
4810 if (unlikely(size < info->options_len)) {
4811 err = -ENOMEM;
4812 goto err_clear;
4813 }
4814
4815 ip_tunnel_info_opts_get(to, info);
4816 if (size > info->options_len)
4817 memset(to + info->options_len, 0, size - info->options_len);
4818
4819 return info->options_len;
4820 err_clear:
4821 memset(to, 0, size);
4822 return err;
4823 }
4824
4825 static const struct bpf_func_proto bpf_skb_get_tunnel_opt_proto = {
4826 .func = bpf_skb_get_tunnel_opt,
4827 .gpl_only = false,
4828 .ret_type = RET_INTEGER,
4829 .arg1_type = ARG_PTR_TO_CTX,
4830 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
4831 .arg3_type = ARG_CONST_SIZE,
4832 };
4833
4834 static struct metadata_dst __percpu *md_dst;
4835
BPF_CALL_4(bpf_skb_set_tunnel_key,struct sk_buff *,skb,const struct bpf_tunnel_key *,from,u32,size,u64,flags)4836 BPF_CALL_4(bpf_skb_set_tunnel_key, struct sk_buff *, skb,
4837 const struct bpf_tunnel_key *, from, u32, size, u64, flags)
4838 {
4839 struct metadata_dst *md = this_cpu_ptr(md_dst);
4840 u8 compat[sizeof(struct bpf_tunnel_key)];
4841 struct ip_tunnel_info *info;
4842
4843 if (unlikely(flags & ~(BPF_F_TUNINFO_IPV6 | BPF_F_ZERO_CSUM_TX |
4844 BPF_F_DONT_FRAGMENT | BPF_F_SEQ_NUMBER |
4845 BPF_F_NO_TUNNEL_KEY)))
4846 return -EINVAL;
4847 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
4848 switch (size) {
4849 case offsetof(struct bpf_tunnel_key, local_ipv6[0]):
4850 case offsetof(struct bpf_tunnel_key, tunnel_label):
4851 case offsetof(struct bpf_tunnel_key, tunnel_ext):
4852 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
4853 /* Fixup deprecated structure layouts here, so we have
4854 * a common path later on.
4855 */
4856 memcpy(compat, from, size);
4857 memset(compat + size, 0, sizeof(compat) - size);
4858 from = (const struct bpf_tunnel_key *) compat;
4859 break;
4860 default:
4861 return -EINVAL;
4862 }
4863 }
4864 if (unlikely((!(flags & BPF_F_TUNINFO_IPV6) && from->tunnel_label) ||
4865 from->tunnel_ext))
4866 return -EINVAL;
4867
4868 skb_dst_drop(skb);
4869 dst_hold((struct dst_entry *) md);
4870 skb_dst_set(skb, (struct dst_entry *) md);
4871
4872 info = &md->u.tun_info;
4873 memset(info, 0, sizeof(*info));
4874 info->mode = IP_TUNNEL_INFO_TX;
4875
4876 __set_bit(IP_TUNNEL_NOCACHE_BIT, info->key.tun_flags);
4877 __assign_bit(IP_TUNNEL_DONT_FRAGMENT_BIT, info->key.tun_flags,
4878 flags & BPF_F_DONT_FRAGMENT);
4879 __assign_bit(IP_TUNNEL_CSUM_BIT, info->key.tun_flags,
4880 !(flags & BPF_F_ZERO_CSUM_TX));
4881 __assign_bit(IP_TUNNEL_SEQ_BIT, info->key.tun_flags,
4882 flags & BPF_F_SEQ_NUMBER);
4883 __assign_bit(IP_TUNNEL_KEY_BIT, info->key.tun_flags,
4884 !(flags & BPF_F_NO_TUNNEL_KEY));
4885
4886 info->key.tun_id = cpu_to_be64(from->tunnel_id);
4887 info->key.tos = from->tunnel_tos;
4888 info->key.ttl = from->tunnel_ttl;
4889
4890 if (flags & BPF_F_TUNINFO_IPV6) {
4891 info->mode |= IP_TUNNEL_INFO_IPV6;
4892 memcpy(&info->key.u.ipv6.dst, from->remote_ipv6,
4893 sizeof(from->remote_ipv6));
4894 memcpy(&info->key.u.ipv6.src, from->local_ipv6,
4895 sizeof(from->local_ipv6));
4896 info->key.label = cpu_to_be32(from->tunnel_label) &
4897 IPV6_FLOWLABEL_MASK;
4898 } else {
4899 info->key.u.ipv4.dst = cpu_to_be32(from->remote_ipv4);
4900 info->key.u.ipv4.src = cpu_to_be32(from->local_ipv4);
4901 info->key.flow_flags = FLOWI_FLAG_ANYSRC;
4902 }
4903
4904 return 0;
4905 }
4906
4907 static const struct bpf_func_proto bpf_skb_set_tunnel_key_proto = {
4908 .func = bpf_skb_set_tunnel_key,
4909 .gpl_only = false,
4910 .ret_type = RET_INTEGER,
4911 .arg1_type = ARG_PTR_TO_CTX,
4912 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
4913 .arg3_type = ARG_CONST_SIZE,
4914 .arg4_type = ARG_ANYTHING,
4915 };
4916
BPF_CALL_3(bpf_skb_set_tunnel_opt,struct sk_buff *,skb,const u8 *,from,u32,size)4917 BPF_CALL_3(bpf_skb_set_tunnel_opt, struct sk_buff *, skb,
4918 const u8 *, from, u32, size)
4919 {
4920 struct ip_tunnel_info *info = skb_tunnel_info(skb);
4921 const struct metadata_dst *md = this_cpu_ptr(md_dst);
4922 IP_TUNNEL_DECLARE_FLAGS(present) = { };
4923
4924 if (unlikely(info != &md->u.tun_info || (size & (sizeof(u32) - 1))))
4925 return -EINVAL;
4926 if (unlikely(size > IP_TUNNEL_OPTS_MAX))
4927 return -ENOMEM;
4928
4929 ip_tunnel_set_options_present(present);
4930 ip_tunnel_info_opts_set(info, from, size, present);
4931
4932 return 0;
4933 }
4934
4935 static const struct bpf_func_proto bpf_skb_set_tunnel_opt_proto = {
4936 .func = bpf_skb_set_tunnel_opt,
4937 .gpl_only = false,
4938 .ret_type = RET_INTEGER,
4939 .arg1_type = ARG_PTR_TO_CTX,
4940 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
4941 .arg3_type = ARG_CONST_SIZE,
4942 };
4943
4944 static const struct bpf_func_proto *
bpf_get_skb_set_tunnel_proto(enum bpf_func_id which)4945 bpf_get_skb_set_tunnel_proto(enum bpf_func_id which)
4946 {
4947 if (!md_dst) {
4948 struct metadata_dst __percpu *tmp;
4949
4950 tmp = metadata_dst_alloc_percpu(IP_TUNNEL_OPTS_MAX,
4951 METADATA_IP_TUNNEL,
4952 GFP_KERNEL);
4953 if (!tmp)
4954 return NULL;
4955 if (cmpxchg(&md_dst, NULL, tmp))
4956 metadata_dst_free_percpu(tmp);
4957 }
4958
4959 switch (which) {
4960 case BPF_FUNC_skb_set_tunnel_key:
4961 return &bpf_skb_set_tunnel_key_proto;
4962 case BPF_FUNC_skb_set_tunnel_opt:
4963 return &bpf_skb_set_tunnel_opt_proto;
4964 default:
4965 return NULL;
4966 }
4967 }
4968
BPF_CALL_3(bpf_skb_under_cgroup,struct sk_buff *,skb,struct bpf_map *,map,u32,idx)4969 BPF_CALL_3(bpf_skb_under_cgroup, struct sk_buff *, skb, struct bpf_map *, map,
4970 u32, idx)
4971 {
4972 struct bpf_array *array = container_of(map, struct bpf_array, map);
4973 struct cgroup *cgrp;
4974 struct sock *sk;
4975
4976 sk = skb_to_full_sk(skb);
4977 if (!sk || !sk_fullsock(sk))
4978 return -ENOENT;
4979 if (unlikely(idx >= array->map.max_entries))
4980 return -E2BIG;
4981
4982 cgrp = READ_ONCE(array->ptrs[idx]);
4983 if (unlikely(!cgrp))
4984 return -EAGAIN;
4985
4986 return sk_under_cgroup_hierarchy(sk, cgrp);
4987 }
4988
4989 static const struct bpf_func_proto bpf_skb_under_cgroup_proto = {
4990 .func = bpf_skb_under_cgroup,
4991 .gpl_only = false,
4992 .ret_type = RET_INTEGER,
4993 .arg1_type = ARG_PTR_TO_CTX,
4994 .arg2_type = ARG_CONST_MAP_PTR,
4995 .arg3_type = ARG_ANYTHING,
4996 };
4997
4998 #ifdef CONFIG_SOCK_CGROUP_DATA
__bpf_sk_cgroup_id(struct sock * sk)4999 static inline u64 __bpf_sk_cgroup_id(struct sock *sk)
5000 {
5001 struct cgroup *cgrp;
5002
5003 sk = sk_to_full_sk(sk);
5004 if (!sk || !sk_fullsock(sk))
5005 return 0;
5006
5007 cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
5008 return cgroup_id(cgrp);
5009 }
5010
BPF_CALL_1(bpf_skb_cgroup_id,const struct sk_buff *,skb)5011 BPF_CALL_1(bpf_skb_cgroup_id, const struct sk_buff *, skb)
5012 {
5013 return __bpf_sk_cgroup_id(skb->sk);
5014 }
5015
5016 static const struct bpf_func_proto bpf_skb_cgroup_id_proto = {
5017 .func = bpf_skb_cgroup_id,
5018 .gpl_only = false,
5019 .ret_type = RET_INTEGER,
5020 .arg1_type = ARG_PTR_TO_CTX,
5021 };
5022
__bpf_sk_ancestor_cgroup_id(struct sock * sk,int ancestor_level)5023 static inline u64 __bpf_sk_ancestor_cgroup_id(struct sock *sk,
5024 int ancestor_level)
5025 {
5026 struct cgroup *ancestor;
5027 struct cgroup *cgrp;
5028
5029 sk = sk_to_full_sk(sk);
5030 if (!sk || !sk_fullsock(sk))
5031 return 0;
5032
5033 cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
5034 ancestor = cgroup_ancestor(cgrp, ancestor_level);
5035 if (!ancestor)
5036 return 0;
5037
5038 return cgroup_id(ancestor);
5039 }
5040
BPF_CALL_2(bpf_skb_ancestor_cgroup_id,const struct sk_buff *,skb,int,ancestor_level)5041 BPF_CALL_2(bpf_skb_ancestor_cgroup_id, const struct sk_buff *, skb, int,
5042 ancestor_level)
5043 {
5044 return __bpf_sk_ancestor_cgroup_id(skb->sk, ancestor_level);
5045 }
5046
5047 static const struct bpf_func_proto bpf_skb_ancestor_cgroup_id_proto = {
5048 .func = bpf_skb_ancestor_cgroup_id,
5049 .gpl_only = false,
5050 .ret_type = RET_INTEGER,
5051 .arg1_type = ARG_PTR_TO_CTX,
5052 .arg2_type = ARG_ANYTHING,
5053 };
5054
BPF_CALL_1(bpf_sk_cgroup_id,struct sock *,sk)5055 BPF_CALL_1(bpf_sk_cgroup_id, struct sock *, sk)
5056 {
5057 return __bpf_sk_cgroup_id(sk);
5058 }
5059
5060 static const struct bpf_func_proto bpf_sk_cgroup_id_proto = {
5061 .func = bpf_sk_cgroup_id,
5062 .gpl_only = false,
5063 .ret_type = RET_INTEGER,
5064 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
5065 };
5066
BPF_CALL_2(bpf_sk_ancestor_cgroup_id,struct sock *,sk,int,ancestor_level)5067 BPF_CALL_2(bpf_sk_ancestor_cgroup_id, struct sock *, sk, int, ancestor_level)
5068 {
5069 return __bpf_sk_ancestor_cgroup_id(sk, ancestor_level);
5070 }
5071
5072 static const struct bpf_func_proto bpf_sk_ancestor_cgroup_id_proto = {
5073 .func = bpf_sk_ancestor_cgroup_id,
5074 .gpl_only = false,
5075 .ret_type = RET_INTEGER,
5076 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
5077 .arg2_type = ARG_ANYTHING,
5078 };
5079 #endif
5080
bpf_xdp_copy(void * dst,const void * ctx,unsigned long off,unsigned long len)5081 static unsigned long bpf_xdp_copy(void *dst, const void *ctx,
5082 unsigned long off, unsigned long len)
5083 {
5084 struct xdp_buff *xdp = (struct xdp_buff *)ctx;
5085
5086 bpf_xdp_copy_buf(xdp, off, dst, len, false);
5087 return 0;
5088 }
5089
BPF_CALL_5(bpf_xdp_event_output,struct xdp_buff *,xdp,struct bpf_map *,map,u64,flags,void *,meta,u64,meta_size)5090 BPF_CALL_5(bpf_xdp_event_output, struct xdp_buff *, xdp, struct bpf_map *, map,
5091 u64, flags, void *, meta, u64, meta_size)
5092 {
5093 u64 xdp_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
5094
5095 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
5096 return -EINVAL;
5097
5098 if (unlikely(!xdp || xdp_size > xdp_get_buff_len(xdp)))
5099 return -EFAULT;
5100
5101 return bpf_event_output(map, flags, meta, meta_size, xdp,
5102 xdp_size, bpf_xdp_copy);
5103 }
5104
5105 static const struct bpf_func_proto bpf_xdp_event_output_proto = {
5106 .func = bpf_xdp_event_output,
5107 .gpl_only = true,
5108 .ret_type = RET_INTEGER,
5109 .arg1_type = ARG_PTR_TO_CTX,
5110 .arg2_type = ARG_CONST_MAP_PTR,
5111 .arg3_type = ARG_ANYTHING,
5112 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
5113 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
5114 };
5115
5116 BTF_ID_LIST_SINGLE(bpf_xdp_output_btf_ids, struct, xdp_buff)
5117
5118 const struct bpf_func_proto bpf_xdp_output_proto = {
5119 .func = bpf_xdp_event_output,
5120 .gpl_only = true,
5121 .ret_type = RET_INTEGER,
5122 .arg1_type = ARG_PTR_TO_BTF_ID,
5123 .arg1_btf_id = &bpf_xdp_output_btf_ids[0],
5124 .arg2_type = ARG_CONST_MAP_PTR,
5125 .arg3_type = ARG_ANYTHING,
5126 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
5127 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
5128 };
5129
BPF_CALL_1(bpf_get_socket_cookie,struct sk_buff *,skb)5130 BPF_CALL_1(bpf_get_socket_cookie, struct sk_buff *, skb)
5131 {
5132 return skb->sk ? __sock_gen_cookie(skb->sk) : 0;
5133 }
5134
5135 static const struct bpf_func_proto bpf_get_socket_cookie_proto = {
5136 .func = bpf_get_socket_cookie,
5137 .gpl_only = false,
5138 .ret_type = RET_INTEGER,
5139 .arg1_type = ARG_PTR_TO_CTX,
5140 };
5141
BPF_CALL_1(bpf_get_socket_cookie_sock_addr,struct bpf_sock_addr_kern *,ctx)5142 BPF_CALL_1(bpf_get_socket_cookie_sock_addr, struct bpf_sock_addr_kern *, ctx)
5143 {
5144 return __sock_gen_cookie(ctx->sk);
5145 }
5146
5147 static const struct bpf_func_proto bpf_get_socket_cookie_sock_addr_proto = {
5148 .func = bpf_get_socket_cookie_sock_addr,
5149 .gpl_only = false,
5150 .ret_type = RET_INTEGER,
5151 .arg1_type = ARG_PTR_TO_CTX,
5152 };
5153
BPF_CALL_1(bpf_get_socket_cookie_sock,struct sock *,ctx)5154 BPF_CALL_1(bpf_get_socket_cookie_sock, struct sock *, ctx)
5155 {
5156 return __sock_gen_cookie(ctx);
5157 }
5158
5159 static const struct bpf_func_proto bpf_get_socket_cookie_sock_proto = {
5160 .func = bpf_get_socket_cookie_sock,
5161 .gpl_only = false,
5162 .ret_type = RET_INTEGER,
5163 .arg1_type = ARG_PTR_TO_CTX,
5164 };
5165
BPF_CALL_1(bpf_get_socket_ptr_cookie,struct sock *,sk)5166 BPF_CALL_1(bpf_get_socket_ptr_cookie, struct sock *, sk)
5167 {
5168 return sk ? sock_gen_cookie(sk) : 0;
5169 }
5170
5171 const struct bpf_func_proto bpf_get_socket_ptr_cookie_proto = {
5172 .func = bpf_get_socket_ptr_cookie,
5173 .gpl_only = false,
5174 .ret_type = RET_INTEGER,
5175 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON | PTR_MAYBE_NULL,
5176 };
5177
BPF_CALL_1(bpf_get_socket_cookie_sock_ops,struct bpf_sock_ops_kern *,ctx)5178 BPF_CALL_1(bpf_get_socket_cookie_sock_ops, struct bpf_sock_ops_kern *, ctx)
5179 {
5180 return __sock_gen_cookie(ctx->sk);
5181 }
5182
5183 static const struct bpf_func_proto bpf_get_socket_cookie_sock_ops_proto = {
5184 .func = bpf_get_socket_cookie_sock_ops,
5185 .gpl_only = false,
5186 .ret_type = RET_INTEGER,
5187 .arg1_type = ARG_PTR_TO_CTX,
5188 };
5189
__bpf_get_netns_cookie(struct sock * sk)5190 static u64 __bpf_get_netns_cookie(struct sock *sk)
5191 {
5192 const struct net *net = sk ? sock_net(sk) : &init_net;
5193
5194 return net->net_cookie;
5195 }
5196
BPF_CALL_1(bpf_get_netns_cookie,struct sk_buff *,skb)5197 BPF_CALL_1(bpf_get_netns_cookie, struct sk_buff *, skb)
5198 {
5199 return __bpf_get_netns_cookie(skb && skb->sk ? skb->sk : NULL);
5200 }
5201
5202 static const struct bpf_func_proto bpf_get_netns_cookie_proto = {
5203 .func = bpf_get_netns_cookie,
5204 .ret_type = RET_INTEGER,
5205 .arg1_type = ARG_PTR_TO_CTX_OR_NULL,
5206 };
5207
BPF_CALL_1(bpf_get_netns_cookie_sock,struct sock *,ctx)5208 BPF_CALL_1(bpf_get_netns_cookie_sock, struct sock *, ctx)
5209 {
5210 return __bpf_get_netns_cookie(ctx);
5211 }
5212
5213 static const struct bpf_func_proto bpf_get_netns_cookie_sock_proto = {
5214 .func = bpf_get_netns_cookie_sock,
5215 .gpl_only = false,
5216 .ret_type = RET_INTEGER,
5217 .arg1_type = ARG_PTR_TO_CTX_OR_NULL,
5218 };
5219
BPF_CALL_1(bpf_get_netns_cookie_sock_addr,struct bpf_sock_addr_kern *,ctx)5220 BPF_CALL_1(bpf_get_netns_cookie_sock_addr, struct bpf_sock_addr_kern *, ctx)
5221 {
5222 return __bpf_get_netns_cookie(ctx ? ctx->sk : NULL);
5223 }
5224
5225 static const struct bpf_func_proto bpf_get_netns_cookie_sock_addr_proto = {
5226 .func = bpf_get_netns_cookie_sock_addr,
5227 .gpl_only = false,
5228 .ret_type = RET_INTEGER,
5229 .arg1_type = ARG_PTR_TO_CTX_OR_NULL,
5230 };
5231
BPF_CALL_1(bpf_get_netns_cookie_sock_ops,struct bpf_sock_ops_kern *,ctx)5232 BPF_CALL_1(bpf_get_netns_cookie_sock_ops, struct bpf_sock_ops_kern *, ctx)
5233 {
5234 return __bpf_get_netns_cookie(ctx ? ctx->sk : NULL);
5235 }
5236
5237 static const struct bpf_func_proto bpf_get_netns_cookie_sock_ops_proto = {
5238 .func = bpf_get_netns_cookie_sock_ops,
5239 .gpl_only = false,
5240 .ret_type = RET_INTEGER,
5241 .arg1_type = ARG_PTR_TO_CTX_OR_NULL,
5242 };
5243
BPF_CALL_1(bpf_get_netns_cookie_sk_msg,struct sk_msg *,ctx)5244 BPF_CALL_1(bpf_get_netns_cookie_sk_msg, struct sk_msg *, ctx)
5245 {
5246 return __bpf_get_netns_cookie(ctx ? ctx->sk : NULL);
5247 }
5248
5249 static const struct bpf_func_proto bpf_get_netns_cookie_sk_msg_proto = {
5250 .func = bpf_get_netns_cookie_sk_msg,
5251 .gpl_only = false,
5252 .ret_type = RET_INTEGER,
5253 .arg1_type = ARG_PTR_TO_CTX_OR_NULL,
5254 };
5255
BPF_CALL_1(bpf_get_socket_uid,struct sk_buff *,skb)5256 BPF_CALL_1(bpf_get_socket_uid, struct sk_buff *, skb)
5257 {
5258 struct sock *sk = sk_to_full_sk(skb->sk);
5259 kuid_t kuid;
5260
5261 if (!sk || !sk_fullsock(sk))
5262 return overflowuid;
5263 kuid = sock_net_uid(sock_net(sk), sk);
5264 return from_kuid_munged(sock_net(sk)->user_ns, kuid);
5265 }
5266
5267 static const struct bpf_func_proto bpf_get_socket_uid_proto = {
5268 .func = bpf_get_socket_uid,
5269 .gpl_only = false,
5270 .ret_type = RET_INTEGER,
5271 .arg1_type = ARG_PTR_TO_CTX,
5272 };
5273
sk_bpf_set_get_cb_flags(struct sock * sk,char * optval,bool getopt)5274 static int sk_bpf_set_get_cb_flags(struct sock *sk, char *optval, bool getopt)
5275 {
5276 u32 sk_bpf_cb_flags;
5277
5278 if (getopt) {
5279 *(u32 *)optval = sk->sk_bpf_cb_flags;
5280 return 0;
5281 }
5282
5283 sk_bpf_cb_flags = *(u32 *)optval;
5284
5285 if (sk_bpf_cb_flags & ~SK_BPF_CB_MASK)
5286 return -EINVAL;
5287
5288 sk->sk_bpf_cb_flags = sk_bpf_cb_flags;
5289
5290 return 0;
5291 }
5292
sol_socket_sockopt(struct sock * sk,int optname,char * optval,int * optlen,bool getopt)5293 static int sol_socket_sockopt(struct sock *sk, int optname,
5294 char *optval, int *optlen,
5295 bool getopt)
5296 {
5297 switch (optname) {
5298 case SO_REUSEADDR:
5299 case SO_SNDBUF:
5300 case SO_RCVBUF:
5301 case SO_KEEPALIVE:
5302 case SO_PRIORITY:
5303 case SO_REUSEPORT:
5304 case SO_RCVLOWAT:
5305 case SO_MARK:
5306 case SO_MAX_PACING_RATE:
5307 case SO_BINDTOIFINDEX:
5308 case SO_TXREHASH:
5309 case SK_BPF_CB_FLAGS:
5310 if (*optlen != sizeof(int))
5311 return -EINVAL;
5312 break;
5313 case SO_BINDTODEVICE:
5314 break;
5315 default:
5316 return -EINVAL;
5317 }
5318
5319 if (optname == SK_BPF_CB_FLAGS)
5320 return sk_bpf_set_get_cb_flags(sk, optval, getopt);
5321
5322 if (getopt) {
5323 if (optname == SO_BINDTODEVICE)
5324 return -EINVAL;
5325 return sk_getsockopt(sk, SOL_SOCKET, optname,
5326 KERNEL_SOCKPTR(optval),
5327 KERNEL_SOCKPTR(optlen));
5328 }
5329
5330 return sk_setsockopt(sk, SOL_SOCKET, optname,
5331 KERNEL_SOCKPTR(optval), *optlen);
5332 }
5333
bpf_sol_tcp_getsockopt(struct sock * sk,int optname,char * optval,int optlen)5334 static int bpf_sol_tcp_getsockopt(struct sock *sk, int optname,
5335 char *optval, int optlen)
5336 {
5337 if (optlen != sizeof(int))
5338 return -EINVAL;
5339
5340 switch (optname) {
5341 case TCP_BPF_SOCK_OPS_CB_FLAGS: {
5342 int cb_flags = tcp_sk(sk)->bpf_sock_ops_cb_flags;
5343
5344 memcpy(optval, &cb_flags, optlen);
5345 break;
5346 }
5347 case TCP_BPF_RTO_MIN: {
5348 int rto_min_us = jiffies_to_usecs(inet_csk(sk)->icsk_rto_min);
5349
5350 memcpy(optval, &rto_min_us, optlen);
5351 break;
5352 }
5353 case TCP_BPF_DELACK_MAX: {
5354 int delack_max_us = jiffies_to_usecs(inet_csk(sk)->icsk_delack_max);
5355
5356 memcpy(optval, &delack_max_us, optlen);
5357 break;
5358 }
5359 default:
5360 return -EINVAL;
5361 }
5362
5363 return 0;
5364 }
5365
bpf_sol_tcp_setsockopt(struct sock * sk,int optname,char * optval,int optlen)5366 static int bpf_sol_tcp_setsockopt(struct sock *sk, int optname,
5367 char *optval, int optlen)
5368 {
5369 struct tcp_sock *tp = tcp_sk(sk);
5370 unsigned long timeout;
5371 int val;
5372
5373 if (optlen != sizeof(int))
5374 return -EINVAL;
5375
5376 val = *(int *)optval;
5377
5378 /* Only some options are supported */
5379 switch (optname) {
5380 case TCP_BPF_IW:
5381 if (val <= 0 || tp->data_segs_out > tp->syn_data)
5382 return -EINVAL;
5383 tcp_snd_cwnd_set(tp, val);
5384 break;
5385 case TCP_BPF_SNDCWND_CLAMP:
5386 if (val <= 0)
5387 return -EINVAL;
5388 tp->snd_cwnd_clamp = val;
5389 tp->snd_ssthresh = val;
5390 break;
5391 case TCP_BPF_DELACK_MAX:
5392 timeout = usecs_to_jiffies(val);
5393 if (timeout > TCP_DELACK_MAX ||
5394 timeout < TCP_TIMEOUT_MIN)
5395 return -EINVAL;
5396 inet_csk(sk)->icsk_delack_max = timeout;
5397 break;
5398 case TCP_BPF_RTO_MIN:
5399 timeout = usecs_to_jiffies(val);
5400 if (timeout > TCP_RTO_MIN ||
5401 timeout < TCP_TIMEOUT_MIN)
5402 return -EINVAL;
5403 inet_csk(sk)->icsk_rto_min = timeout;
5404 break;
5405 case TCP_BPF_SOCK_OPS_CB_FLAGS:
5406 if (val & ~(BPF_SOCK_OPS_ALL_CB_FLAGS))
5407 return -EINVAL;
5408 tp->bpf_sock_ops_cb_flags = val;
5409 break;
5410 default:
5411 return -EINVAL;
5412 }
5413
5414 return 0;
5415 }
5416
sol_tcp_sockopt_congestion(struct sock * sk,char * optval,int * optlen,bool getopt)5417 static int sol_tcp_sockopt_congestion(struct sock *sk, char *optval,
5418 int *optlen, bool getopt)
5419 {
5420 struct tcp_sock *tp;
5421 int ret;
5422
5423 if (*optlen < 2)
5424 return -EINVAL;
5425
5426 if (getopt) {
5427 if (!inet_csk(sk)->icsk_ca_ops)
5428 return -EINVAL;
5429 /* BPF expects NULL-terminated tcp-cc string */
5430 optval[--(*optlen)] = '\0';
5431 return do_tcp_getsockopt(sk, SOL_TCP, TCP_CONGESTION,
5432 KERNEL_SOCKPTR(optval),
5433 KERNEL_SOCKPTR(optlen));
5434 }
5435
5436 /* "cdg" is the only cc that alloc a ptr
5437 * in inet_csk_ca area. The bpf-tcp-cc may
5438 * overwrite this ptr after switching to cdg.
5439 */
5440 if (*optlen >= sizeof("cdg") - 1 && !strncmp("cdg", optval, *optlen))
5441 return -ENOTSUPP;
5442
5443 /* It stops this looping
5444 *
5445 * .init => bpf_setsockopt(tcp_cc) => .init =>
5446 * bpf_setsockopt(tcp_cc)" => .init => ....
5447 *
5448 * The second bpf_setsockopt(tcp_cc) is not allowed
5449 * in order to break the loop when both .init
5450 * are the same bpf prog.
5451 *
5452 * This applies even the second bpf_setsockopt(tcp_cc)
5453 * does not cause a loop. This limits only the first
5454 * '.init' can call bpf_setsockopt(TCP_CONGESTION) to
5455 * pick a fallback cc (eg. peer does not support ECN)
5456 * and the second '.init' cannot fallback to
5457 * another.
5458 */
5459 tp = tcp_sk(sk);
5460 if (tp->bpf_chg_cc_inprogress)
5461 return -EBUSY;
5462
5463 tp->bpf_chg_cc_inprogress = 1;
5464 ret = do_tcp_setsockopt(sk, SOL_TCP, TCP_CONGESTION,
5465 KERNEL_SOCKPTR(optval), *optlen);
5466 tp->bpf_chg_cc_inprogress = 0;
5467 return ret;
5468 }
5469
sol_tcp_sockopt(struct sock * sk,int optname,char * optval,int * optlen,bool getopt)5470 static int sol_tcp_sockopt(struct sock *sk, int optname,
5471 char *optval, int *optlen,
5472 bool getopt)
5473 {
5474 if (sk->sk_protocol != IPPROTO_TCP)
5475 return -EINVAL;
5476
5477 switch (optname) {
5478 case TCP_NODELAY:
5479 case TCP_MAXSEG:
5480 case TCP_KEEPIDLE:
5481 case TCP_KEEPINTVL:
5482 case TCP_KEEPCNT:
5483 case TCP_SYNCNT:
5484 case TCP_WINDOW_CLAMP:
5485 case TCP_THIN_LINEAR_TIMEOUTS:
5486 case TCP_USER_TIMEOUT:
5487 case TCP_NOTSENT_LOWAT:
5488 case TCP_SAVE_SYN:
5489 case TCP_RTO_MAX_MS:
5490 if (*optlen != sizeof(int))
5491 return -EINVAL;
5492 break;
5493 case TCP_CONGESTION:
5494 return sol_tcp_sockopt_congestion(sk, optval, optlen, getopt);
5495 case TCP_SAVED_SYN:
5496 if (*optlen < 1)
5497 return -EINVAL;
5498 break;
5499 default:
5500 if (getopt)
5501 return bpf_sol_tcp_getsockopt(sk, optname, optval, *optlen);
5502 return bpf_sol_tcp_setsockopt(sk, optname, optval, *optlen);
5503 }
5504
5505 if (getopt) {
5506 if (optname == TCP_SAVED_SYN) {
5507 struct tcp_sock *tp = tcp_sk(sk);
5508
5509 if (!tp->saved_syn ||
5510 *optlen > tcp_saved_syn_len(tp->saved_syn))
5511 return -EINVAL;
5512 memcpy(optval, tp->saved_syn->data, *optlen);
5513 /* It cannot free tp->saved_syn here because it
5514 * does not know if the user space still needs it.
5515 */
5516 return 0;
5517 }
5518
5519 return do_tcp_getsockopt(sk, SOL_TCP, optname,
5520 KERNEL_SOCKPTR(optval),
5521 KERNEL_SOCKPTR(optlen));
5522 }
5523
5524 return do_tcp_setsockopt(sk, SOL_TCP, optname,
5525 KERNEL_SOCKPTR(optval), *optlen);
5526 }
5527
sol_ip_sockopt(struct sock * sk,int optname,char * optval,int * optlen,bool getopt)5528 static int sol_ip_sockopt(struct sock *sk, int optname,
5529 char *optval, int *optlen,
5530 bool getopt)
5531 {
5532 if (sk->sk_family != AF_INET)
5533 return -EINVAL;
5534
5535 switch (optname) {
5536 case IP_TOS:
5537 if (*optlen != sizeof(int))
5538 return -EINVAL;
5539 break;
5540 default:
5541 return -EINVAL;
5542 }
5543
5544 if (getopt)
5545 return do_ip_getsockopt(sk, SOL_IP, optname,
5546 KERNEL_SOCKPTR(optval),
5547 KERNEL_SOCKPTR(optlen));
5548
5549 return do_ip_setsockopt(sk, SOL_IP, optname,
5550 KERNEL_SOCKPTR(optval), *optlen);
5551 }
5552
sol_ipv6_sockopt(struct sock * sk,int optname,char * optval,int * optlen,bool getopt)5553 static int sol_ipv6_sockopt(struct sock *sk, int optname,
5554 char *optval, int *optlen,
5555 bool getopt)
5556 {
5557 if (sk->sk_family != AF_INET6)
5558 return -EINVAL;
5559
5560 switch (optname) {
5561 case IPV6_TCLASS:
5562 case IPV6_AUTOFLOWLABEL:
5563 if (*optlen != sizeof(int))
5564 return -EINVAL;
5565 break;
5566 default:
5567 return -EINVAL;
5568 }
5569
5570 if (getopt)
5571 return ipv6_bpf_stub->ipv6_getsockopt(sk, SOL_IPV6, optname,
5572 KERNEL_SOCKPTR(optval),
5573 KERNEL_SOCKPTR(optlen));
5574
5575 return ipv6_bpf_stub->ipv6_setsockopt(sk, SOL_IPV6, optname,
5576 KERNEL_SOCKPTR(optval), *optlen);
5577 }
5578
__bpf_setsockopt(struct sock * sk,int level,int optname,char * optval,int optlen)5579 static int __bpf_setsockopt(struct sock *sk, int level, int optname,
5580 char *optval, int optlen)
5581 {
5582 if (!sk_fullsock(sk))
5583 return -EINVAL;
5584
5585 if (level == SOL_SOCKET)
5586 return sol_socket_sockopt(sk, optname, optval, &optlen, false);
5587 else if (IS_ENABLED(CONFIG_INET) && level == SOL_IP)
5588 return sol_ip_sockopt(sk, optname, optval, &optlen, false);
5589 else if (IS_ENABLED(CONFIG_IPV6) && level == SOL_IPV6)
5590 return sol_ipv6_sockopt(sk, optname, optval, &optlen, false);
5591 else if (IS_ENABLED(CONFIG_INET) && level == SOL_TCP)
5592 return sol_tcp_sockopt(sk, optname, optval, &optlen, false);
5593
5594 return -EINVAL;
5595 }
5596
is_locked_tcp_sock_ops(struct bpf_sock_ops_kern * bpf_sock)5597 static bool is_locked_tcp_sock_ops(struct bpf_sock_ops_kern *bpf_sock)
5598 {
5599 return bpf_sock->op <= BPF_SOCK_OPS_WRITE_HDR_OPT_CB;
5600 }
5601
_bpf_setsockopt(struct sock * sk,int level,int optname,char * optval,int optlen)5602 static int _bpf_setsockopt(struct sock *sk, int level, int optname,
5603 char *optval, int optlen)
5604 {
5605 if (sk_fullsock(sk))
5606 sock_owned_by_me(sk);
5607 return __bpf_setsockopt(sk, level, optname, optval, optlen);
5608 }
5609
__bpf_getsockopt(struct sock * sk,int level,int optname,char * optval,int optlen)5610 static int __bpf_getsockopt(struct sock *sk, int level, int optname,
5611 char *optval, int optlen)
5612 {
5613 int err, saved_optlen = optlen;
5614
5615 if (!sk_fullsock(sk)) {
5616 err = -EINVAL;
5617 goto done;
5618 }
5619
5620 if (level == SOL_SOCKET)
5621 err = sol_socket_sockopt(sk, optname, optval, &optlen, true);
5622 else if (IS_ENABLED(CONFIG_INET) && level == SOL_TCP)
5623 err = sol_tcp_sockopt(sk, optname, optval, &optlen, true);
5624 else if (IS_ENABLED(CONFIG_INET) && level == SOL_IP)
5625 err = sol_ip_sockopt(sk, optname, optval, &optlen, true);
5626 else if (IS_ENABLED(CONFIG_IPV6) && level == SOL_IPV6)
5627 err = sol_ipv6_sockopt(sk, optname, optval, &optlen, true);
5628 else
5629 err = -EINVAL;
5630
5631 done:
5632 if (err)
5633 optlen = 0;
5634 if (optlen < saved_optlen)
5635 memset(optval + optlen, 0, saved_optlen - optlen);
5636 return err;
5637 }
5638
_bpf_getsockopt(struct sock * sk,int level,int optname,char * optval,int optlen)5639 static int _bpf_getsockopt(struct sock *sk, int level, int optname,
5640 char *optval, int optlen)
5641 {
5642 if (sk_fullsock(sk))
5643 sock_owned_by_me(sk);
5644 return __bpf_getsockopt(sk, level, optname, optval, optlen);
5645 }
5646
BPF_CALL_5(bpf_sk_setsockopt,struct sock *,sk,int,level,int,optname,char *,optval,int,optlen)5647 BPF_CALL_5(bpf_sk_setsockopt, struct sock *, sk, int, level,
5648 int, optname, char *, optval, int, optlen)
5649 {
5650 return _bpf_setsockopt(sk, level, optname, optval, optlen);
5651 }
5652
5653 const struct bpf_func_proto bpf_sk_setsockopt_proto = {
5654 .func = bpf_sk_setsockopt,
5655 .gpl_only = false,
5656 .ret_type = RET_INTEGER,
5657 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
5658 .arg2_type = ARG_ANYTHING,
5659 .arg3_type = ARG_ANYTHING,
5660 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
5661 .arg5_type = ARG_CONST_SIZE,
5662 };
5663
BPF_CALL_5(bpf_sk_getsockopt,struct sock *,sk,int,level,int,optname,char *,optval,int,optlen)5664 BPF_CALL_5(bpf_sk_getsockopt, struct sock *, sk, int, level,
5665 int, optname, char *, optval, int, optlen)
5666 {
5667 return _bpf_getsockopt(sk, level, optname, optval, optlen);
5668 }
5669
5670 const struct bpf_func_proto bpf_sk_getsockopt_proto = {
5671 .func = bpf_sk_getsockopt,
5672 .gpl_only = false,
5673 .ret_type = RET_INTEGER,
5674 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
5675 .arg2_type = ARG_ANYTHING,
5676 .arg3_type = ARG_ANYTHING,
5677 .arg4_type = ARG_PTR_TO_UNINIT_MEM,
5678 .arg5_type = ARG_CONST_SIZE,
5679 };
5680
BPF_CALL_5(bpf_unlocked_sk_setsockopt,struct sock *,sk,int,level,int,optname,char *,optval,int,optlen)5681 BPF_CALL_5(bpf_unlocked_sk_setsockopt, struct sock *, sk, int, level,
5682 int, optname, char *, optval, int, optlen)
5683 {
5684 return __bpf_setsockopt(sk, level, optname, optval, optlen);
5685 }
5686
5687 const struct bpf_func_proto bpf_unlocked_sk_setsockopt_proto = {
5688 .func = bpf_unlocked_sk_setsockopt,
5689 .gpl_only = false,
5690 .ret_type = RET_INTEGER,
5691 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
5692 .arg2_type = ARG_ANYTHING,
5693 .arg3_type = ARG_ANYTHING,
5694 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
5695 .arg5_type = ARG_CONST_SIZE,
5696 };
5697
BPF_CALL_5(bpf_unlocked_sk_getsockopt,struct sock *,sk,int,level,int,optname,char *,optval,int,optlen)5698 BPF_CALL_5(bpf_unlocked_sk_getsockopt, struct sock *, sk, int, level,
5699 int, optname, char *, optval, int, optlen)
5700 {
5701 return __bpf_getsockopt(sk, level, optname, optval, optlen);
5702 }
5703
5704 const struct bpf_func_proto bpf_unlocked_sk_getsockopt_proto = {
5705 .func = bpf_unlocked_sk_getsockopt,
5706 .gpl_only = false,
5707 .ret_type = RET_INTEGER,
5708 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
5709 .arg2_type = ARG_ANYTHING,
5710 .arg3_type = ARG_ANYTHING,
5711 .arg4_type = ARG_PTR_TO_UNINIT_MEM,
5712 .arg5_type = ARG_CONST_SIZE,
5713 };
5714
BPF_CALL_5(bpf_sock_addr_setsockopt,struct bpf_sock_addr_kern *,ctx,int,level,int,optname,char *,optval,int,optlen)5715 BPF_CALL_5(bpf_sock_addr_setsockopt, struct bpf_sock_addr_kern *, ctx,
5716 int, level, int, optname, char *, optval, int, optlen)
5717 {
5718 return _bpf_setsockopt(ctx->sk, level, optname, optval, optlen);
5719 }
5720
5721 static const struct bpf_func_proto bpf_sock_addr_setsockopt_proto = {
5722 .func = bpf_sock_addr_setsockopt,
5723 .gpl_only = false,
5724 .ret_type = RET_INTEGER,
5725 .arg1_type = ARG_PTR_TO_CTX,
5726 .arg2_type = ARG_ANYTHING,
5727 .arg3_type = ARG_ANYTHING,
5728 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
5729 .arg5_type = ARG_CONST_SIZE,
5730 };
5731
BPF_CALL_5(bpf_sock_addr_getsockopt,struct bpf_sock_addr_kern *,ctx,int,level,int,optname,char *,optval,int,optlen)5732 BPF_CALL_5(bpf_sock_addr_getsockopt, struct bpf_sock_addr_kern *, ctx,
5733 int, level, int, optname, char *, optval, int, optlen)
5734 {
5735 return _bpf_getsockopt(ctx->sk, level, optname, optval, optlen);
5736 }
5737
5738 static const struct bpf_func_proto bpf_sock_addr_getsockopt_proto = {
5739 .func = bpf_sock_addr_getsockopt,
5740 .gpl_only = false,
5741 .ret_type = RET_INTEGER,
5742 .arg1_type = ARG_PTR_TO_CTX,
5743 .arg2_type = ARG_ANYTHING,
5744 .arg3_type = ARG_ANYTHING,
5745 .arg4_type = ARG_PTR_TO_UNINIT_MEM,
5746 .arg5_type = ARG_CONST_SIZE,
5747 };
5748
sk_bpf_set_get_bypass_prot_mem(struct sock * sk,char * optval,int optlen,bool getopt)5749 static int sk_bpf_set_get_bypass_prot_mem(struct sock *sk,
5750 char *optval, int optlen,
5751 bool getopt)
5752 {
5753 int val;
5754
5755 if (optlen != sizeof(int))
5756 return -EINVAL;
5757
5758 if (!sk_has_account(sk))
5759 return -EOPNOTSUPP;
5760
5761 if (getopt) {
5762 *(int *)optval = sk->sk_bypass_prot_mem;
5763 return 0;
5764 }
5765
5766 val = *(int *)optval;
5767 if (val < 0 || val > 1)
5768 return -EINVAL;
5769
5770 sk->sk_bypass_prot_mem = val;
5771 return 0;
5772 }
5773
BPF_CALL_5(bpf_sock_create_setsockopt,struct sock *,sk,int,level,int,optname,char *,optval,int,optlen)5774 BPF_CALL_5(bpf_sock_create_setsockopt, struct sock *, sk, int, level,
5775 int, optname, char *, optval, int, optlen)
5776 {
5777 if (level == SOL_SOCKET && optname == SK_BPF_BYPASS_PROT_MEM)
5778 return sk_bpf_set_get_bypass_prot_mem(sk, optval, optlen, false);
5779
5780 return __bpf_setsockopt(sk, level, optname, optval, optlen);
5781 }
5782
5783 static const struct bpf_func_proto bpf_sock_create_setsockopt_proto = {
5784 .func = bpf_sock_create_setsockopt,
5785 .gpl_only = false,
5786 .ret_type = RET_INTEGER,
5787 .arg1_type = ARG_PTR_TO_CTX,
5788 .arg2_type = ARG_ANYTHING,
5789 .arg3_type = ARG_ANYTHING,
5790 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
5791 .arg5_type = ARG_CONST_SIZE,
5792 };
5793
BPF_CALL_5(bpf_sock_create_getsockopt,struct sock *,sk,int,level,int,optname,char *,optval,int,optlen)5794 BPF_CALL_5(bpf_sock_create_getsockopt, struct sock *, sk, int, level,
5795 int, optname, char *, optval, int, optlen)
5796 {
5797 if (level == SOL_SOCKET && optname == SK_BPF_BYPASS_PROT_MEM) {
5798 int err = sk_bpf_set_get_bypass_prot_mem(sk, optval, optlen, true);
5799
5800 if (err)
5801 memset(optval, 0, optlen);
5802
5803 return err;
5804 }
5805
5806 return __bpf_getsockopt(sk, level, optname, optval, optlen);
5807 }
5808
5809 static const struct bpf_func_proto bpf_sock_create_getsockopt_proto = {
5810 .func = bpf_sock_create_getsockopt,
5811 .gpl_only = false,
5812 .ret_type = RET_INTEGER,
5813 .arg1_type = ARG_PTR_TO_CTX,
5814 .arg2_type = ARG_ANYTHING,
5815 .arg3_type = ARG_ANYTHING,
5816 .arg4_type = ARG_PTR_TO_UNINIT_MEM,
5817 .arg5_type = ARG_CONST_SIZE,
5818 };
5819
BPF_CALL_5(bpf_sock_ops_setsockopt,struct bpf_sock_ops_kern *,bpf_sock,int,level,int,optname,char *,optval,int,optlen)5820 BPF_CALL_5(bpf_sock_ops_setsockopt, struct bpf_sock_ops_kern *, bpf_sock,
5821 int, level, int, optname, char *, optval, int, optlen)
5822 {
5823 if (!is_locked_tcp_sock_ops(bpf_sock))
5824 return -EOPNOTSUPP;
5825
5826 return _bpf_setsockopt(bpf_sock->sk, level, optname, optval, optlen);
5827 }
5828
5829 static const struct bpf_func_proto bpf_sock_ops_setsockopt_proto = {
5830 .func = bpf_sock_ops_setsockopt,
5831 .gpl_only = false,
5832 .ret_type = RET_INTEGER,
5833 .arg1_type = ARG_PTR_TO_CTX,
5834 .arg2_type = ARG_ANYTHING,
5835 .arg3_type = ARG_ANYTHING,
5836 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
5837 .arg5_type = ARG_CONST_SIZE,
5838 };
5839
bpf_sock_ops_get_syn(struct bpf_sock_ops_kern * bpf_sock,int optname,const u8 ** start)5840 static int bpf_sock_ops_get_syn(struct bpf_sock_ops_kern *bpf_sock,
5841 int optname, const u8 **start)
5842 {
5843 struct sk_buff *syn_skb = bpf_sock->syn_skb;
5844 const u8 *hdr_start;
5845 int ret;
5846
5847 if (syn_skb) {
5848 /* sk is a request_sock here */
5849
5850 if (optname == TCP_BPF_SYN) {
5851 hdr_start = syn_skb->data;
5852 ret = tcp_hdrlen(syn_skb);
5853 } else if (optname == TCP_BPF_SYN_IP) {
5854 hdr_start = skb_network_header(syn_skb);
5855 ret = skb_network_header_len(syn_skb) +
5856 tcp_hdrlen(syn_skb);
5857 } else {
5858 /* optname == TCP_BPF_SYN_MAC */
5859 hdr_start = skb_mac_header(syn_skb);
5860 ret = skb_mac_header_len(syn_skb) +
5861 skb_network_header_len(syn_skb) +
5862 tcp_hdrlen(syn_skb);
5863 }
5864 } else {
5865 struct sock *sk = bpf_sock->sk;
5866 struct saved_syn *saved_syn;
5867
5868 if (sk->sk_state == TCP_NEW_SYN_RECV)
5869 /* synack retransmit. bpf_sock->syn_skb will
5870 * not be available. It has to resort to
5871 * saved_syn (if it is saved).
5872 */
5873 saved_syn = inet_reqsk(sk)->saved_syn;
5874 else
5875 saved_syn = tcp_sk(sk)->saved_syn;
5876
5877 if (!saved_syn)
5878 return -ENOENT;
5879
5880 if (optname == TCP_BPF_SYN) {
5881 hdr_start = saved_syn->data +
5882 saved_syn->mac_hdrlen +
5883 saved_syn->network_hdrlen;
5884 ret = saved_syn->tcp_hdrlen;
5885 } else if (optname == TCP_BPF_SYN_IP) {
5886 hdr_start = saved_syn->data +
5887 saved_syn->mac_hdrlen;
5888 ret = saved_syn->network_hdrlen +
5889 saved_syn->tcp_hdrlen;
5890 } else {
5891 /* optname == TCP_BPF_SYN_MAC */
5892
5893 /* TCP_SAVE_SYN may not have saved the mac hdr */
5894 if (!saved_syn->mac_hdrlen)
5895 return -ENOENT;
5896
5897 hdr_start = saved_syn->data;
5898 ret = saved_syn->mac_hdrlen +
5899 saved_syn->network_hdrlen +
5900 saved_syn->tcp_hdrlen;
5901 }
5902 }
5903
5904 *start = hdr_start;
5905 return ret;
5906 }
5907
BPF_CALL_5(bpf_sock_ops_getsockopt,struct bpf_sock_ops_kern *,bpf_sock,int,level,int,optname,char *,optval,int,optlen)5908 BPF_CALL_5(bpf_sock_ops_getsockopt, struct bpf_sock_ops_kern *, bpf_sock,
5909 int, level, int, optname, char *, optval, int, optlen)
5910 {
5911 if (!is_locked_tcp_sock_ops(bpf_sock))
5912 return -EOPNOTSUPP;
5913
5914 if (IS_ENABLED(CONFIG_INET) && level == SOL_TCP &&
5915 optname >= TCP_BPF_SYN && optname <= TCP_BPF_SYN_MAC) {
5916 int ret, copy_len = 0;
5917 const u8 *start;
5918
5919 ret = bpf_sock_ops_get_syn(bpf_sock, optname, &start);
5920 if (ret > 0) {
5921 copy_len = ret;
5922 if (optlen < copy_len) {
5923 copy_len = optlen;
5924 ret = -ENOSPC;
5925 }
5926
5927 memcpy(optval, start, copy_len);
5928 }
5929
5930 /* Zero out unused buffer at the end */
5931 memset(optval + copy_len, 0, optlen - copy_len);
5932
5933 return ret;
5934 }
5935
5936 return _bpf_getsockopt(bpf_sock->sk, level, optname, optval, optlen);
5937 }
5938
5939 static const struct bpf_func_proto bpf_sock_ops_getsockopt_proto = {
5940 .func = bpf_sock_ops_getsockopt,
5941 .gpl_only = false,
5942 .ret_type = RET_INTEGER,
5943 .arg1_type = ARG_PTR_TO_CTX,
5944 .arg2_type = ARG_ANYTHING,
5945 .arg3_type = ARG_ANYTHING,
5946 .arg4_type = ARG_PTR_TO_UNINIT_MEM,
5947 .arg5_type = ARG_CONST_SIZE,
5948 };
5949
BPF_CALL_2(bpf_sock_ops_cb_flags_set,struct bpf_sock_ops_kern *,bpf_sock,int,argval)5950 BPF_CALL_2(bpf_sock_ops_cb_flags_set, struct bpf_sock_ops_kern *, bpf_sock,
5951 int, argval)
5952 {
5953 struct sock *sk = bpf_sock->sk;
5954 int val = argval & BPF_SOCK_OPS_ALL_CB_FLAGS;
5955
5956 if (!is_locked_tcp_sock_ops(bpf_sock))
5957 return -EOPNOTSUPP;
5958
5959 if (!IS_ENABLED(CONFIG_INET) || !sk_fullsock(sk))
5960 return -EINVAL;
5961
5962 tcp_sk(sk)->bpf_sock_ops_cb_flags = val;
5963
5964 return argval & (~BPF_SOCK_OPS_ALL_CB_FLAGS);
5965 }
5966
5967 static const struct bpf_func_proto bpf_sock_ops_cb_flags_set_proto = {
5968 .func = bpf_sock_ops_cb_flags_set,
5969 .gpl_only = false,
5970 .ret_type = RET_INTEGER,
5971 .arg1_type = ARG_PTR_TO_CTX,
5972 .arg2_type = ARG_ANYTHING,
5973 };
5974
5975 const struct ipv6_bpf_stub *ipv6_bpf_stub __read_mostly;
5976 EXPORT_SYMBOL_GPL(ipv6_bpf_stub);
5977
BPF_CALL_3(bpf_bind,struct bpf_sock_addr_kern *,ctx,struct sockaddr *,addr,int,addr_len)5978 BPF_CALL_3(bpf_bind, struct bpf_sock_addr_kern *, ctx, struct sockaddr *, addr,
5979 int, addr_len)
5980 {
5981 #ifdef CONFIG_INET
5982 struct sock *sk = ctx->sk;
5983 u32 flags = BIND_FROM_BPF;
5984 int err;
5985
5986 err = -EINVAL;
5987 if (addr_len < offsetofend(struct sockaddr, sa_family))
5988 return err;
5989 if (addr->sa_family == AF_INET) {
5990 if (addr_len < sizeof(struct sockaddr_in))
5991 return err;
5992 if (((struct sockaddr_in *)addr)->sin_port == htons(0))
5993 flags |= BIND_FORCE_ADDRESS_NO_PORT;
5994 return __inet_bind(sk, (struct sockaddr_unsized *)addr, addr_len, flags);
5995 #if IS_ENABLED(CONFIG_IPV6)
5996 } else if (addr->sa_family == AF_INET6) {
5997 if (addr_len < SIN6_LEN_RFC2133)
5998 return err;
5999 if (((struct sockaddr_in6 *)addr)->sin6_port == htons(0))
6000 flags |= BIND_FORCE_ADDRESS_NO_PORT;
6001 /* ipv6_bpf_stub cannot be NULL, since it's called from
6002 * bpf_cgroup_inet6_connect hook and ipv6 is already loaded
6003 */
6004 return ipv6_bpf_stub->inet6_bind(sk, (struct sockaddr_unsized *)addr,
6005 addr_len, flags);
6006 #endif /* CONFIG_IPV6 */
6007 }
6008 #endif /* CONFIG_INET */
6009
6010 return -EAFNOSUPPORT;
6011 }
6012
6013 static const struct bpf_func_proto bpf_bind_proto = {
6014 .func = bpf_bind,
6015 .gpl_only = false,
6016 .ret_type = RET_INTEGER,
6017 .arg1_type = ARG_PTR_TO_CTX,
6018 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6019 .arg3_type = ARG_CONST_SIZE,
6020 };
6021
6022 #ifdef CONFIG_XFRM
6023
6024 #if (IS_BUILTIN(CONFIG_XFRM_INTERFACE) && IS_ENABLED(CONFIG_DEBUG_INFO_BTF)) || \
6025 (IS_MODULE(CONFIG_XFRM_INTERFACE) && IS_ENABLED(CONFIG_DEBUG_INFO_BTF_MODULES))
6026
6027 struct metadata_dst __percpu *xfrm_bpf_md_dst;
6028 EXPORT_SYMBOL_GPL(xfrm_bpf_md_dst);
6029
6030 #endif
6031
BPF_CALL_5(bpf_skb_get_xfrm_state,struct sk_buff *,skb,u32,index,struct bpf_xfrm_state *,to,u32,size,u64,flags)6032 BPF_CALL_5(bpf_skb_get_xfrm_state, struct sk_buff *, skb, u32, index,
6033 struct bpf_xfrm_state *, to, u32, size, u64, flags)
6034 {
6035 const struct sec_path *sp = skb_sec_path(skb);
6036 const struct xfrm_state *x;
6037
6038 if (!sp || unlikely(index >= sp->len || flags))
6039 goto err_clear;
6040
6041 x = sp->xvec[index];
6042
6043 if (unlikely(size != sizeof(struct bpf_xfrm_state)))
6044 goto err_clear;
6045
6046 to->reqid = x->props.reqid;
6047 to->spi = x->id.spi;
6048 to->family = x->props.family;
6049 to->ext = 0;
6050
6051 if (to->family == AF_INET6) {
6052 memcpy(to->remote_ipv6, x->props.saddr.a6,
6053 sizeof(to->remote_ipv6));
6054 } else {
6055 to->remote_ipv4 = x->props.saddr.a4;
6056 memset(&to->remote_ipv6[1], 0, sizeof(__u32) * 3);
6057 }
6058
6059 return 0;
6060 err_clear:
6061 memset(to, 0, size);
6062 return -EINVAL;
6063 }
6064
6065 static const struct bpf_func_proto bpf_skb_get_xfrm_state_proto = {
6066 .func = bpf_skb_get_xfrm_state,
6067 .gpl_only = false,
6068 .ret_type = RET_INTEGER,
6069 .arg1_type = ARG_PTR_TO_CTX,
6070 .arg2_type = ARG_ANYTHING,
6071 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
6072 .arg4_type = ARG_CONST_SIZE,
6073 .arg5_type = ARG_ANYTHING,
6074 };
6075 #endif
6076
6077 #if IS_ENABLED(CONFIG_INET) || IS_ENABLED(CONFIG_IPV6)
bpf_fib_set_fwd_params(struct bpf_fib_lookup * params,u32 mtu)6078 static int bpf_fib_set_fwd_params(struct bpf_fib_lookup *params, u32 mtu)
6079 {
6080 params->h_vlan_TCI = 0;
6081 params->h_vlan_proto = 0;
6082 if (mtu)
6083 params->mtu_result = mtu; /* union with tot_len */
6084
6085 return 0;
6086 }
6087 #endif
6088
6089 #if IS_ENABLED(CONFIG_INET)
bpf_ipv4_fib_lookup(struct net * net,struct bpf_fib_lookup * params,u32 flags,bool check_mtu)6090 static int bpf_ipv4_fib_lookup(struct net *net, struct bpf_fib_lookup *params,
6091 u32 flags, bool check_mtu)
6092 {
6093 struct fib_nh_common *nhc;
6094 struct in_device *in_dev;
6095 struct neighbour *neigh;
6096 struct net_device *dev;
6097 struct fib_result res;
6098 struct flowi4 fl4;
6099 u32 mtu = 0;
6100 int err;
6101
6102 dev = dev_get_by_index_rcu(net, params->ifindex);
6103 if (unlikely(!dev))
6104 return -ENODEV;
6105
6106 /* verify forwarding is enabled on this interface */
6107 in_dev = __in_dev_get_rcu(dev);
6108 if (unlikely(!in_dev || !IN_DEV_FORWARD(in_dev)))
6109 return BPF_FIB_LKUP_RET_FWD_DISABLED;
6110
6111 if (flags & BPF_FIB_LOOKUP_OUTPUT) {
6112 fl4.flowi4_iif = 1;
6113 fl4.flowi4_oif = params->ifindex;
6114 } else {
6115 fl4.flowi4_iif = params->ifindex;
6116 fl4.flowi4_oif = 0;
6117 }
6118 fl4.flowi4_dscp = inet_dsfield_to_dscp(params->tos);
6119 fl4.flowi4_scope = RT_SCOPE_UNIVERSE;
6120 fl4.flowi4_flags = 0;
6121
6122 fl4.flowi4_proto = params->l4_protocol;
6123 fl4.daddr = params->ipv4_dst;
6124 fl4.saddr = params->ipv4_src;
6125 fl4.fl4_sport = params->sport;
6126 fl4.fl4_dport = params->dport;
6127 fl4.flowi4_multipath_hash = 0;
6128
6129 if (flags & BPF_FIB_LOOKUP_DIRECT) {
6130 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
6131 struct fib_table *tb;
6132
6133 if (flags & BPF_FIB_LOOKUP_TBID) {
6134 tbid = params->tbid;
6135 /* zero out for vlan output */
6136 params->tbid = 0;
6137 }
6138
6139 tb = fib_get_table(net, tbid);
6140 if (unlikely(!tb))
6141 return BPF_FIB_LKUP_RET_NOT_FWDED;
6142
6143 err = fib_table_lookup(tb, &fl4, &res, FIB_LOOKUP_NOREF);
6144 } else {
6145 if (flags & BPF_FIB_LOOKUP_MARK)
6146 fl4.flowi4_mark = params->mark;
6147 else
6148 fl4.flowi4_mark = 0;
6149 fl4.flowi4_secid = 0;
6150 fl4.flowi4_tun_key.tun_id = 0;
6151 fl4.flowi4_uid = sock_net_uid(net, NULL);
6152
6153 err = fib_lookup(net, &fl4, &res, FIB_LOOKUP_NOREF);
6154 }
6155
6156 if (err) {
6157 /* map fib lookup errors to RTN_ type */
6158 if (err == -EINVAL)
6159 return BPF_FIB_LKUP_RET_BLACKHOLE;
6160 if (err == -EHOSTUNREACH)
6161 return BPF_FIB_LKUP_RET_UNREACHABLE;
6162 if (err == -EACCES)
6163 return BPF_FIB_LKUP_RET_PROHIBIT;
6164
6165 return BPF_FIB_LKUP_RET_NOT_FWDED;
6166 }
6167
6168 if (res.type != RTN_UNICAST)
6169 return BPF_FIB_LKUP_RET_NOT_FWDED;
6170
6171 if (fib_info_num_path(res.fi) > 1)
6172 fib_select_path(net, &res, &fl4, NULL);
6173
6174 if (check_mtu) {
6175 mtu = ip_mtu_from_fib_result(&res, params->ipv4_dst);
6176 if (params->tot_len > mtu) {
6177 params->mtu_result = mtu; /* union with tot_len */
6178 return BPF_FIB_LKUP_RET_FRAG_NEEDED;
6179 }
6180 }
6181
6182 nhc = res.nhc;
6183
6184 /* do not handle lwt encaps right now */
6185 if (nhc->nhc_lwtstate)
6186 return BPF_FIB_LKUP_RET_UNSUPP_LWT;
6187
6188 dev = nhc->nhc_dev;
6189
6190 params->rt_metric = res.fi->fib_priority;
6191 params->ifindex = dev->ifindex;
6192
6193 if (flags & BPF_FIB_LOOKUP_SRC)
6194 params->ipv4_src = fib_result_prefsrc(net, &res);
6195
6196 /* xdp and cls_bpf programs are run in RCU-bh so
6197 * rcu_read_lock_bh is not needed here
6198 */
6199 if (likely(nhc->nhc_gw_family != AF_INET6)) {
6200 if (nhc->nhc_gw_family)
6201 params->ipv4_dst = nhc->nhc_gw.ipv4;
6202 } else {
6203 struct in6_addr *dst = (struct in6_addr *)params->ipv6_dst;
6204
6205 params->family = AF_INET6;
6206 *dst = nhc->nhc_gw.ipv6;
6207 }
6208
6209 if (flags & BPF_FIB_LOOKUP_SKIP_NEIGH)
6210 goto set_fwd_params;
6211
6212 if (likely(nhc->nhc_gw_family != AF_INET6))
6213 neigh = __ipv4_neigh_lookup_noref(dev,
6214 (__force u32)params->ipv4_dst);
6215 else
6216 neigh = __ipv6_neigh_lookup_noref_stub(dev, params->ipv6_dst);
6217
6218 if (!neigh || !(READ_ONCE(neigh->nud_state) & NUD_VALID))
6219 return BPF_FIB_LKUP_RET_NO_NEIGH;
6220 memcpy(params->dmac, neigh->ha, ETH_ALEN);
6221 memcpy(params->smac, dev->dev_addr, ETH_ALEN);
6222
6223 set_fwd_params:
6224 return bpf_fib_set_fwd_params(params, mtu);
6225 }
6226 #endif
6227
6228 #if IS_ENABLED(CONFIG_IPV6)
bpf_ipv6_fib_lookup(struct net * net,struct bpf_fib_lookup * params,u32 flags,bool check_mtu)6229 static int bpf_ipv6_fib_lookup(struct net *net, struct bpf_fib_lookup *params,
6230 u32 flags, bool check_mtu)
6231 {
6232 struct in6_addr *src = (struct in6_addr *) params->ipv6_src;
6233 struct in6_addr *dst = (struct in6_addr *) params->ipv6_dst;
6234 struct fib6_result res = {};
6235 struct neighbour *neigh;
6236 struct net_device *dev;
6237 struct inet6_dev *idev;
6238 struct flowi6 fl6;
6239 int strict = 0;
6240 int oif, err;
6241 u32 mtu = 0;
6242
6243 /* link local addresses are never forwarded */
6244 if (rt6_need_strict(dst) || rt6_need_strict(src))
6245 return BPF_FIB_LKUP_RET_NOT_FWDED;
6246
6247 dev = dev_get_by_index_rcu(net, params->ifindex);
6248 if (unlikely(!dev))
6249 return -ENODEV;
6250
6251 idev = __in6_dev_get_safely(dev);
6252 if (unlikely(!idev || !READ_ONCE(idev->cnf.forwarding)))
6253 return BPF_FIB_LKUP_RET_FWD_DISABLED;
6254
6255 if (flags & BPF_FIB_LOOKUP_OUTPUT) {
6256 fl6.flowi6_iif = 1;
6257 oif = fl6.flowi6_oif = params->ifindex;
6258 } else {
6259 oif = fl6.flowi6_iif = params->ifindex;
6260 fl6.flowi6_oif = 0;
6261 strict = RT6_LOOKUP_F_HAS_SADDR;
6262 }
6263 fl6.flowlabel = params->flowinfo;
6264 fl6.flowi6_scope = 0;
6265 fl6.flowi6_flags = 0;
6266 fl6.mp_hash = 0;
6267
6268 fl6.flowi6_proto = params->l4_protocol;
6269 fl6.daddr = *dst;
6270 fl6.saddr = *src;
6271 fl6.fl6_sport = params->sport;
6272 fl6.fl6_dport = params->dport;
6273
6274 if (flags & BPF_FIB_LOOKUP_DIRECT) {
6275 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
6276 struct fib6_table *tb;
6277
6278 if (flags & BPF_FIB_LOOKUP_TBID) {
6279 tbid = params->tbid;
6280 /* zero out for vlan output */
6281 params->tbid = 0;
6282 }
6283
6284 tb = ipv6_stub->fib6_get_table(net, tbid);
6285 if (unlikely(!tb))
6286 return BPF_FIB_LKUP_RET_NOT_FWDED;
6287
6288 err = ipv6_stub->fib6_table_lookup(net, tb, oif, &fl6, &res,
6289 strict);
6290 } else {
6291 if (flags & BPF_FIB_LOOKUP_MARK)
6292 fl6.flowi6_mark = params->mark;
6293 else
6294 fl6.flowi6_mark = 0;
6295 fl6.flowi6_secid = 0;
6296 fl6.flowi6_tun_key.tun_id = 0;
6297 fl6.flowi6_uid = sock_net_uid(net, NULL);
6298
6299 err = ipv6_stub->fib6_lookup(net, oif, &fl6, &res, strict);
6300 }
6301
6302 if (unlikely(err || IS_ERR_OR_NULL(res.f6i) ||
6303 res.f6i == net->ipv6.fib6_null_entry))
6304 return BPF_FIB_LKUP_RET_NOT_FWDED;
6305
6306 switch (res.fib6_type) {
6307 /* only unicast is forwarded */
6308 case RTN_UNICAST:
6309 break;
6310 case RTN_BLACKHOLE:
6311 return BPF_FIB_LKUP_RET_BLACKHOLE;
6312 case RTN_UNREACHABLE:
6313 return BPF_FIB_LKUP_RET_UNREACHABLE;
6314 case RTN_PROHIBIT:
6315 return BPF_FIB_LKUP_RET_PROHIBIT;
6316 default:
6317 return BPF_FIB_LKUP_RET_NOT_FWDED;
6318 }
6319
6320 ipv6_stub->fib6_select_path(net, &res, &fl6, fl6.flowi6_oif,
6321 fl6.flowi6_oif != 0, NULL, strict);
6322
6323 if (check_mtu) {
6324 mtu = ipv6_stub->ip6_mtu_from_fib6(&res, dst, src);
6325 if (params->tot_len > mtu) {
6326 params->mtu_result = mtu; /* union with tot_len */
6327 return BPF_FIB_LKUP_RET_FRAG_NEEDED;
6328 }
6329 }
6330
6331 if (res.nh->fib_nh_lws)
6332 return BPF_FIB_LKUP_RET_UNSUPP_LWT;
6333
6334 if (res.nh->fib_nh_gw_family)
6335 *dst = res.nh->fib_nh_gw6;
6336
6337 dev = res.nh->fib_nh_dev;
6338 params->rt_metric = res.f6i->fib6_metric;
6339 params->ifindex = dev->ifindex;
6340
6341 if (flags & BPF_FIB_LOOKUP_SRC) {
6342 if (res.f6i->fib6_prefsrc.plen) {
6343 *src = res.f6i->fib6_prefsrc.addr;
6344 } else {
6345 err = ipv6_bpf_stub->ipv6_dev_get_saddr(net, dev,
6346 &fl6.daddr, 0,
6347 src);
6348 if (err)
6349 return BPF_FIB_LKUP_RET_NO_SRC_ADDR;
6350 }
6351 }
6352
6353 if (flags & BPF_FIB_LOOKUP_SKIP_NEIGH)
6354 goto set_fwd_params;
6355
6356 /* xdp and cls_bpf programs are run in RCU-bh so rcu_read_lock_bh is
6357 * not needed here.
6358 */
6359 neigh = __ipv6_neigh_lookup_noref_stub(dev, dst);
6360 if (!neigh || !(READ_ONCE(neigh->nud_state) & NUD_VALID))
6361 return BPF_FIB_LKUP_RET_NO_NEIGH;
6362 memcpy(params->dmac, neigh->ha, ETH_ALEN);
6363 memcpy(params->smac, dev->dev_addr, ETH_ALEN);
6364
6365 set_fwd_params:
6366 return bpf_fib_set_fwd_params(params, mtu);
6367 }
6368 #endif
6369
6370 #define BPF_FIB_LOOKUP_MASK (BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_OUTPUT | \
6371 BPF_FIB_LOOKUP_SKIP_NEIGH | BPF_FIB_LOOKUP_TBID | \
6372 BPF_FIB_LOOKUP_SRC | BPF_FIB_LOOKUP_MARK)
6373
BPF_CALL_4(bpf_xdp_fib_lookup,struct xdp_buff *,ctx,struct bpf_fib_lookup *,params,int,plen,u32,flags)6374 BPF_CALL_4(bpf_xdp_fib_lookup, struct xdp_buff *, ctx,
6375 struct bpf_fib_lookup *, params, int, plen, u32, flags)
6376 {
6377 if (plen < sizeof(*params))
6378 return -EINVAL;
6379
6380 if (flags & ~BPF_FIB_LOOKUP_MASK)
6381 return -EINVAL;
6382
6383 switch (params->family) {
6384 #if IS_ENABLED(CONFIG_INET)
6385 case AF_INET:
6386 return bpf_ipv4_fib_lookup(dev_net(ctx->rxq->dev), params,
6387 flags, true);
6388 #endif
6389 #if IS_ENABLED(CONFIG_IPV6)
6390 case AF_INET6:
6391 return bpf_ipv6_fib_lookup(dev_net(ctx->rxq->dev), params,
6392 flags, true);
6393 #endif
6394 }
6395 return -EAFNOSUPPORT;
6396 }
6397
6398 static const struct bpf_func_proto bpf_xdp_fib_lookup_proto = {
6399 .func = bpf_xdp_fib_lookup,
6400 .gpl_only = true,
6401 .ret_type = RET_INTEGER,
6402 .arg1_type = ARG_PTR_TO_CTX,
6403 .arg2_type = ARG_PTR_TO_MEM | MEM_WRITE,
6404 .arg3_type = ARG_CONST_SIZE,
6405 .arg4_type = ARG_ANYTHING,
6406 };
6407
BPF_CALL_4(bpf_skb_fib_lookup,struct sk_buff *,skb,struct bpf_fib_lookup *,params,int,plen,u32,flags)6408 BPF_CALL_4(bpf_skb_fib_lookup, struct sk_buff *, skb,
6409 struct bpf_fib_lookup *, params, int, plen, u32, flags)
6410 {
6411 struct net *net = dev_net(skb->dev);
6412 int rc = -EAFNOSUPPORT;
6413 bool check_mtu = false;
6414
6415 if (plen < sizeof(*params))
6416 return -EINVAL;
6417
6418 if (flags & ~BPF_FIB_LOOKUP_MASK)
6419 return -EINVAL;
6420
6421 if (params->tot_len)
6422 check_mtu = true;
6423
6424 switch (params->family) {
6425 #if IS_ENABLED(CONFIG_INET)
6426 case AF_INET:
6427 rc = bpf_ipv4_fib_lookup(net, params, flags, check_mtu);
6428 break;
6429 #endif
6430 #if IS_ENABLED(CONFIG_IPV6)
6431 case AF_INET6:
6432 rc = bpf_ipv6_fib_lookup(net, params, flags, check_mtu);
6433 break;
6434 #endif
6435 }
6436
6437 if (rc == BPF_FIB_LKUP_RET_SUCCESS && !check_mtu) {
6438 struct net_device *dev;
6439
6440 /* When tot_len isn't provided by user, check skb
6441 * against MTU of FIB lookup resulting net_device
6442 */
6443 dev = dev_get_by_index_rcu(net, params->ifindex);
6444 if (!is_skb_forwardable(dev, skb))
6445 rc = BPF_FIB_LKUP_RET_FRAG_NEEDED;
6446
6447 params->mtu_result = dev->mtu; /* union with tot_len */
6448 }
6449
6450 return rc;
6451 }
6452
6453 static const struct bpf_func_proto bpf_skb_fib_lookup_proto = {
6454 .func = bpf_skb_fib_lookup,
6455 .gpl_only = true,
6456 .ret_type = RET_INTEGER,
6457 .arg1_type = ARG_PTR_TO_CTX,
6458 .arg2_type = ARG_PTR_TO_MEM | MEM_WRITE,
6459 .arg3_type = ARG_CONST_SIZE,
6460 .arg4_type = ARG_ANYTHING,
6461 };
6462
__dev_via_ifindex(struct net_device * dev_curr,u32 ifindex)6463 static struct net_device *__dev_via_ifindex(struct net_device *dev_curr,
6464 u32 ifindex)
6465 {
6466 struct net *netns = dev_net(dev_curr);
6467
6468 /* Non-redirect use-cases can use ifindex=0 and save ifindex lookup */
6469 if (ifindex == 0)
6470 return dev_curr;
6471
6472 return dev_get_by_index_rcu(netns, ifindex);
6473 }
6474
BPF_CALL_5(bpf_skb_check_mtu,struct sk_buff *,skb,u32,ifindex,u32 *,mtu_len,s32,len_diff,u64,flags)6475 BPF_CALL_5(bpf_skb_check_mtu, struct sk_buff *, skb,
6476 u32, ifindex, u32 *, mtu_len, s32, len_diff, u64, flags)
6477 {
6478 int ret = BPF_MTU_CHK_RET_FRAG_NEEDED;
6479 struct net_device *dev = skb->dev;
6480 int mtu, dev_len, skb_len;
6481
6482 if (unlikely(flags & ~(BPF_MTU_CHK_SEGS)))
6483 return -EINVAL;
6484 if (unlikely(flags & BPF_MTU_CHK_SEGS && (len_diff || *mtu_len)))
6485 return -EINVAL;
6486
6487 dev = __dev_via_ifindex(dev, ifindex);
6488 if (unlikely(!dev))
6489 return -ENODEV;
6490
6491 mtu = READ_ONCE(dev->mtu);
6492 dev_len = mtu + dev->hard_header_len;
6493
6494 /* If set use *mtu_len as input, L3 as iph->tot_len (like fib_lookup) */
6495 skb_len = *mtu_len ? *mtu_len + dev->hard_header_len : skb->len;
6496
6497 skb_len += len_diff; /* minus result pass check */
6498 if (skb_len <= dev_len) {
6499 ret = BPF_MTU_CHK_RET_SUCCESS;
6500 goto out;
6501 }
6502 /* At this point, skb->len exceed MTU, but as it include length of all
6503 * segments, it can still be below MTU. The SKB can possibly get
6504 * re-segmented in transmit path (see validate_xmit_skb). Thus, user
6505 * must choose if segs are to be MTU checked.
6506 */
6507 if (skb_is_gso(skb)) {
6508 ret = BPF_MTU_CHK_RET_SUCCESS;
6509 if (flags & BPF_MTU_CHK_SEGS) {
6510 if (!skb_transport_header_was_set(skb))
6511 return -EINVAL;
6512 if (!skb_gso_validate_network_len(skb, mtu))
6513 ret = BPF_MTU_CHK_RET_SEGS_TOOBIG;
6514 }
6515 }
6516 out:
6517 *mtu_len = mtu;
6518 return ret;
6519 }
6520
BPF_CALL_5(bpf_xdp_check_mtu,struct xdp_buff *,xdp,u32,ifindex,u32 *,mtu_len,s32,len_diff,u64,flags)6521 BPF_CALL_5(bpf_xdp_check_mtu, struct xdp_buff *, xdp,
6522 u32, ifindex, u32 *, mtu_len, s32, len_diff, u64, flags)
6523 {
6524 struct net_device *dev = xdp->rxq->dev;
6525 int xdp_len = xdp->data_end - xdp->data;
6526 int ret = BPF_MTU_CHK_RET_SUCCESS;
6527 int mtu, dev_len;
6528
6529 /* XDP variant doesn't support multi-buffer segment check (yet) */
6530 if (unlikely(flags))
6531 return -EINVAL;
6532
6533 dev = __dev_via_ifindex(dev, ifindex);
6534 if (unlikely(!dev))
6535 return -ENODEV;
6536
6537 mtu = READ_ONCE(dev->mtu);
6538 dev_len = mtu + dev->hard_header_len;
6539
6540 /* Use *mtu_len as input, L3 as iph->tot_len (like fib_lookup) */
6541 if (*mtu_len)
6542 xdp_len = *mtu_len + dev->hard_header_len;
6543
6544 xdp_len += len_diff; /* minus result pass check */
6545 if (xdp_len > dev_len)
6546 ret = BPF_MTU_CHK_RET_FRAG_NEEDED;
6547
6548 *mtu_len = mtu;
6549 return ret;
6550 }
6551
6552 static const struct bpf_func_proto bpf_skb_check_mtu_proto = {
6553 .func = bpf_skb_check_mtu,
6554 .gpl_only = true,
6555 .ret_type = RET_INTEGER,
6556 .arg1_type = ARG_PTR_TO_CTX,
6557 .arg2_type = ARG_ANYTHING,
6558 .arg3_type = ARG_PTR_TO_FIXED_SIZE_MEM | MEM_WRITE | MEM_ALIGNED,
6559 .arg3_size = sizeof(u32),
6560 .arg4_type = ARG_ANYTHING,
6561 .arg5_type = ARG_ANYTHING,
6562 };
6563
6564 static const struct bpf_func_proto bpf_xdp_check_mtu_proto = {
6565 .func = bpf_xdp_check_mtu,
6566 .gpl_only = true,
6567 .ret_type = RET_INTEGER,
6568 .arg1_type = ARG_PTR_TO_CTX,
6569 .arg2_type = ARG_ANYTHING,
6570 .arg3_type = ARG_PTR_TO_FIXED_SIZE_MEM | MEM_WRITE | MEM_ALIGNED,
6571 .arg3_size = sizeof(u32),
6572 .arg4_type = ARG_ANYTHING,
6573 .arg5_type = ARG_ANYTHING,
6574 };
6575
6576 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
bpf_push_seg6_encap(struct sk_buff * skb,u32 type,void * hdr,u32 len)6577 static int bpf_push_seg6_encap(struct sk_buff *skb, u32 type, void *hdr, u32 len)
6578 {
6579 int err;
6580 struct ipv6_sr_hdr *srh = (struct ipv6_sr_hdr *)hdr;
6581
6582 if (!seg6_validate_srh(srh, len, false))
6583 return -EINVAL;
6584
6585 switch (type) {
6586 case BPF_LWT_ENCAP_SEG6_INLINE:
6587 if (skb->protocol != htons(ETH_P_IPV6))
6588 return -EBADMSG;
6589
6590 err = seg6_do_srh_inline(skb, srh);
6591 break;
6592 case BPF_LWT_ENCAP_SEG6:
6593 skb_reset_inner_headers(skb);
6594 skb->encapsulation = 1;
6595 err = seg6_do_srh_encap(skb, srh, IPPROTO_IPV6);
6596 break;
6597 default:
6598 return -EINVAL;
6599 }
6600
6601 bpf_compute_data_pointers(skb);
6602 if (err)
6603 return err;
6604
6605 skb_set_transport_header(skb, sizeof(struct ipv6hdr));
6606
6607 return seg6_lookup_nexthop(skb, NULL, 0);
6608 }
6609 #endif /* CONFIG_IPV6_SEG6_BPF */
6610
6611 #if IS_ENABLED(CONFIG_LWTUNNEL_BPF)
bpf_push_ip_encap(struct sk_buff * skb,void * hdr,u32 len,bool ingress)6612 static int bpf_push_ip_encap(struct sk_buff *skb, void *hdr, u32 len,
6613 bool ingress)
6614 {
6615 return bpf_lwt_push_ip_encap(skb, hdr, len, ingress);
6616 }
6617 #endif
6618
BPF_CALL_4(bpf_lwt_in_push_encap,struct sk_buff *,skb,u32,type,void *,hdr,u32,len)6619 BPF_CALL_4(bpf_lwt_in_push_encap, struct sk_buff *, skb, u32, type, void *, hdr,
6620 u32, len)
6621 {
6622 switch (type) {
6623 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
6624 case BPF_LWT_ENCAP_SEG6:
6625 case BPF_LWT_ENCAP_SEG6_INLINE:
6626 return bpf_push_seg6_encap(skb, type, hdr, len);
6627 #endif
6628 #if IS_ENABLED(CONFIG_LWTUNNEL_BPF)
6629 case BPF_LWT_ENCAP_IP:
6630 return bpf_push_ip_encap(skb, hdr, len, true /* ingress */);
6631 #endif
6632 default:
6633 return -EINVAL;
6634 }
6635 }
6636
BPF_CALL_4(bpf_lwt_xmit_push_encap,struct sk_buff *,skb,u32,type,void *,hdr,u32,len)6637 BPF_CALL_4(bpf_lwt_xmit_push_encap, struct sk_buff *, skb, u32, type,
6638 void *, hdr, u32, len)
6639 {
6640 switch (type) {
6641 #if IS_ENABLED(CONFIG_LWTUNNEL_BPF)
6642 case BPF_LWT_ENCAP_IP:
6643 return bpf_push_ip_encap(skb, hdr, len, false /* egress */);
6644 #endif
6645 default:
6646 return -EINVAL;
6647 }
6648 }
6649
6650 static const struct bpf_func_proto bpf_lwt_in_push_encap_proto = {
6651 .func = bpf_lwt_in_push_encap,
6652 .gpl_only = false,
6653 .ret_type = RET_INTEGER,
6654 .arg1_type = ARG_PTR_TO_CTX,
6655 .arg2_type = ARG_ANYTHING,
6656 .arg3_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6657 .arg4_type = ARG_CONST_SIZE
6658 };
6659
6660 static const struct bpf_func_proto bpf_lwt_xmit_push_encap_proto = {
6661 .func = bpf_lwt_xmit_push_encap,
6662 .gpl_only = false,
6663 .ret_type = RET_INTEGER,
6664 .arg1_type = ARG_PTR_TO_CTX,
6665 .arg2_type = ARG_ANYTHING,
6666 .arg3_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6667 .arg4_type = ARG_CONST_SIZE
6668 };
6669
6670 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
BPF_CALL_4(bpf_lwt_seg6_store_bytes,struct sk_buff *,skb,u32,offset,const void *,from,u32,len)6671 BPF_CALL_4(bpf_lwt_seg6_store_bytes, struct sk_buff *, skb, u32, offset,
6672 const void *, from, u32, len)
6673 {
6674 struct seg6_bpf_srh_state *srh_state =
6675 this_cpu_ptr(&seg6_bpf_srh_states);
6676 struct ipv6_sr_hdr *srh = srh_state->srh;
6677 void *srh_tlvs, *srh_end, *ptr;
6678 int srhoff = 0;
6679
6680 lockdep_assert_held(&srh_state->bh_lock);
6681 if (srh == NULL)
6682 return -EINVAL;
6683
6684 srh_tlvs = (void *)((char *)srh + ((srh->first_segment + 1) << 4));
6685 srh_end = (void *)((char *)srh + sizeof(*srh) + srh_state->hdrlen);
6686
6687 ptr = skb->data + offset;
6688 if (ptr >= srh_tlvs && ptr + len <= srh_end)
6689 srh_state->valid = false;
6690 else if (ptr < (void *)&srh->flags ||
6691 ptr + len > (void *)&srh->segments)
6692 return -EFAULT;
6693
6694 if (unlikely(bpf_try_make_writable(skb, offset + len)))
6695 return -EFAULT;
6696 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0)
6697 return -EINVAL;
6698 srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
6699
6700 memcpy(skb->data + offset, from, len);
6701 return 0;
6702 }
6703
6704 static const struct bpf_func_proto bpf_lwt_seg6_store_bytes_proto = {
6705 .func = bpf_lwt_seg6_store_bytes,
6706 .gpl_only = false,
6707 .ret_type = RET_INTEGER,
6708 .arg1_type = ARG_PTR_TO_CTX,
6709 .arg2_type = ARG_ANYTHING,
6710 .arg3_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6711 .arg4_type = ARG_CONST_SIZE
6712 };
6713
bpf_update_srh_state(struct sk_buff * skb)6714 static void bpf_update_srh_state(struct sk_buff *skb)
6715 {
6716 struct seg6_bpf_srh_state *srh_state =
6717 this_cpu_ptr(&seg6_bpf_srh_states);
6718 int srhoff = 0;
6719
6720 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0) {
6721 srh_state->srh = NULL;
6722 } else {
6723 srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
6724 srh_state->hdrlen = srh_state->srh->hdrlen << 3;
6725 srh_state->valid = true;
6726 }
6727 }
6728
BPF_CALL_4(bpf_lwt_seg6_action,struct sk_buff *,skb,u32,action,void *,param,u32,param_len)6729 BPF_CALL_4(bpf_lwt_seg6_action, struct sk_buff *, skb,
6730 u32, action, void *, param, u32, param_len)
6731 {
6732 struct seg6_bpf_srh_state *srh_state =
6733 this_cpu_ptr(&seg6_bpf_srh_states);
6734 int hdroff = 0;
6735 int err;
6736
6737 lockdep_assert_held(&srh_state->bh_lock);
6738 switch (action) {
6739 case SEG6_LOCAL_ACTION_END_X:
6740 if (!seg6_bpf_has_valid_srh(skb))
6741 return -EBADMSG;
6742 if (param_len != sizeof(struct in6_addr))
6743 return -EINVAL;
6744 return seg6_lookup_nexthop(skb, (struct in6_addr *)param, 0);
6745 case SEG6_LOCAL_ACTION_END_T:
6746 if (!seg6_bpf_has_valid_srh(skb))
6747 return -EBADMSG;
6748 if (param_len != sizeof(int))
6749 return -EINVAL;
6750 return seg6_lookup_nexthop(skb, NULL, *(int *)param);
6751 case SEG6_LOCAL_ACTION_END_DT6:
6752 if (!seg6_bpf_has_valid_srh(skb))
6753 return -EBADMSG;
6754 if (param_len != sizeof(int))
6755 return -EINVAL;
6756
6757 if (ipv6_find_hdr(skb, &hdroff, IPPROTO_IPV6, NULL, NULL) < 0)
6758 return -EBADMSG;
6759 if (!pskb_pull(skb, hdroff))
6760 return -EBADMSG;
6761
6762 skb_postpull_rcsum(skb, skb_network_header(skb), hdroff);
6763 skb_reset_network_header(skb);
6764 skb_reset_transport_header(skb);
6765 skb->encapsulation = 0;
6766
6767 bpf_compute_data_pointers(skb);
6768 bpf_update_srh_state(skb);
6769 return seg6_lookup_nexthop(skb, NULL, *(int *)param);
6770 case SEG6_LOCAL_ACTION_END_B6:
6771 if (srh_state->srh && !seg6_bpf_has_valid_srh(skb))
6772 return -EBADMSG;
6773 err = bpf_push_seg6_encap(skb, BPF_LWT_ENCAP_SEG6_INLINE,
6774 param, param_len);
6775 if (!err)
6776 bpf_update_srh_state(skb);
6777
6778 return err;
6779 case SEG6_LOCAL_ACTION_END_B6_ENCAP:
6780 if (srh_state->srh && !seg6_bpf_has_valid_srh(skb))
6781 return -EBADMSG;
6782 err = bpf_push_seg6_encap(skb, BPF_LWT_ENCAP_SEG6,
6783 param, param_len);
6784 if (!err)
6785 bpf_update_srh_state(skb);
6786
6787 return err;
6788 default:
6789 return -EINVAL;
6790 }
6791 }
6792
6793 static const struct bpf_func_proto bpf_lwt_seg6_action_proto = {
6794 .func = bpf_lwt_seg6_action,
6795 .gpl_only = false,
6796 .ret_type = RET_INTEGER,
6797 .arg1_type = ARG_PTR_TO_CTX,
6798 .arg2_type = ARG_ANYTHING,
6799 .arg3_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6800 .arg4_type = ARG_CONST_SIZE
6801 };
6802
BPF_CALL_3(bpf_lwt_seg6_adjust_srh,struct sk_buff *,skb,u32,offset,s32,len)6803 BPF_CALL_3(bpf_lwt_seg6_adjust_srh, struct sk_buff *, skb, u32, offset,
6804 s32, len)
6805 {
6806 struct seg6_bpf_srh_state *srh_state =
6807 this_cpu_ptr(&seg6_bpf_srh_states);
6808 struct ipv6_sr_hdr *srh = srh_state->srh;
6809 void *srh_end, *srh_tlvs, *ptr;
6810 struct ipv6hdr *hdr;
6811 int srhoff = 0;
6812 int ret;
6813
6814 lockdep_assert_held(&srh_state->bh_lock);
6815 if (unlikely(srh == NULL))
6816 return -EINVAL;
6817
6818 srh_tlvs = (void *)((unsigned char *)srh + sizeof(*srh) +
6819 ((srh->first_segment + 1) << 4));
6820 srh_end = (void *)((unsigned char *)srh + sizeof(*srh) +
6821 srh_state->hdrlen);
6822 ptr = skb->data + offset;
6823
6824 if (unlikely(ptr < srh_tlvs || ptr > srh_end))
6825 return -EFAULT;
6826 if (unlikely(len < 0 && (void *)((char *)ptr - len) > srh_end))
6827 return -EFAULT;
6828
6829 if (len > 0) {
6830 ret = skb_cow_head(skb, len);
6831 if (unlikely(ret < 0))
6832 return ret;
6833
6834 ret = bpf_skb_net_hdr_push(skb, offset, len);
6835 } else {
6836 ret = bpf_skb_net_hdr_pop(skb, offset, -1 * len);
6837 }
6838
6839 bpf_compute_data_pointers(skb);
6840 if (unlikely(ret < 0))
6841 return ret;
6842
6843 hdr = (struct ipv6hdr *)skb->data;
6844 hdr->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
6845
6846 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0)
6847 return -EINVAL;
6848 srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
6849 srh_state->hdrlen += len;
6850 srh_state->valid = false;
6851 return 0;
6852 }
6853
6854 static const struct bpf_func_proto bpf_lwt_seg6_adjust_srh_proto = {
6855 .func = bpf_lwt_seg6_adjust_srh,
6856 .gpl_only = false,
6857 .ret_type = RET_INTEGER,
6858 .arg1_type = ARG_PTR_TO_CTX,
6859 .arg2_type = ARG_ANYTHING,
6860 .arg3_type = ARG_ANYTHING,
6861 };
6862 #endif /* CONFIG_IPV6_SEG6_BPF */
6863
6864 #ifdef CONFIG_INET
sk_lookup(struct net * net,struct bpf_sock_tuple * tuple,int dif,int sdif,u8 family,u8 proto)6865 static struct sock *sk_lookup(struct net *net, struct bpf_sock_tuple *tuple,
6866 int dif, int sdif, u8 family, u8 proto)
6867 {
6868 bool refcounted = false;
6869 struct sock *sk = NULL;
6870
6871 if (family == AF_INET) {
6872 __be32 src4 = tuple->ipv4.saddr;
6873 __be32 dst4 = tuple->ipv4.daddr;
6874
6875 if (proto == IPPROTO_TCP)
6876 sk = __inet_lookup(net, NULL, 0,
6877 src4, tuple->ipv4.sport,
6878 dst4, tuple->ipv4.dport,
6879 dif, sdif, &refcounted);
6880 else
6881 sk = __udp4_lib_lookup(net, src4, tuple->ipv4.sport,
6882 dst4, tuple->ipv4.dport,
6883 dif, sdif, net->ipv4.udp_table, NULL);
6884 #if IS_ENABLED(CONFIG_IPV6)
6885 } else {
6886 struct in6_addr *src6 = (struct in6_addr *)&tuple->ipv6.saddr;
6887 struct in6_addr *dst6 = (struct in6_addr *)&tuple->ipv6.daddr;
6888
6889 if (proto == IPPROTO_TCP)
6890 sk = __inet6_lookup(net, NULL, 0,
6891 src6, tuple->ipv6.sport,
6892 dst6, ntohs(tuple->ipv6.dport),
6893 dif, sdif, &refcounted);
6894 else if (likely(ipv6_bpf_stub))
6895 sk = ipv6_bpf_stub->udp6_lib_lookup(net,
6896 src6, tuple->ipv6.sport,
6897 dst6, tuple->ipv6.dport,
6898 dif, sdif,
6899 net->ipv4.udp_table, NULL);
6900 #endif
6901 }
6902
6903 if (unlikely(sk && !refcounted && !sock_flag(sk, SOCK_RCU_FREE))) {
6904 WARN_ONCE(1, "Found non-RCU, unreferenced socket!");
6905 sk = NULL;
6906 }
6907 return sk;
6908 }
6909
6910 /* bpf_skc_lookup performs the core lookup for different types of sockets,
6911 * taking a reference on the socket if it doesn't have the flag SOCK_RCU_FREE.
6912 */
6913 static struct sock *
__bpf_skc_lookup(struct sk_buff * skb,struct bpf_sock_tuple * tuple,u32 len,struct net * caller_net,u32 ifindex,u8 proto,u64 netns_id,u64 flags,int sdif)6914 __bpf_skc_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
6915 struct net *caller_net, u32 ifindex, u8 proto, u64 netns_id,
6916 u64 flags, int sdif)
6917 {
6918 struct sock *sk = NULL;
6919 struct net *net;
6920 u8 family;
6921
6922 if (len == sizeof(tuple->ipv4))
6923 family = AF_INET;
6924 else if (len == sizeof(tuple->ipv6))
6925 family = AF_INET6;
6926 else
6927 return NULL;
6928
6929 if (unlikely(flags || !((s32)netns_id < 0 || netns_id <= S32_MAX)))
6930 goto out;
6931
6932 if (sdif < 0) {
6933 if (family == AF_INET)
6934 sdif = inet_sdif(skb);
6935 else
6936 sdif = inet6_sdif(skb);
6937 }
6938
6939 if ((s32)netns_id < 0) {
6940 net = caller_net;
6941 sk = sk_lookup(net, tuple, ifindex, sdif, family, proto);
6942 } else {
6943 net = get_net_ns_by_id(caller_net, netns_id);
6944 if (unlikely(!net))
6945 goto out;
6946 sk = sk_lookup(net, tuple, ifindex, sdif, family, proto);
6947 put_net(net);
6948 }
6949
6950 out:
6951 return sk;
6952 }
6953
6954 static struct sock *
__bpf_sk_lookup(struct sk_buff * skb,struct bpf_sock_tuple * tuple,u32 len,struct net * caller_net,u32 ifindex,u8 proto,u64 netns_id,u64 flags,int sdif)6955 __bpf_sk_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
6956 struct net *caller_net, u32 ifindex, u8 proto, u64 netns_id,
6957 u64 flags, int sdif)
6958 {
6959 struct sock *sk = __bpf_skc_lookup(skb, tuple, len, caller_net,
6960 ifindex, proto, netns_id, flags,
6961 sdif);
6962
6963 if (sk) {
6964 struct sock *sk2 = sk_to_full_sk(sk);
6965
6966 /* sk_to_full_sk() may return (sk)->rsk_listener, so make sure the original sk
6967 * sock refcnt is decremented to prevent a request_sock leak.
6968 */
6969 if (sk2 != sk) {
6970 sock_gen_put(sk);
6971 /* Ensure there is no need to bump sk2 refcnt */
6972 if (unlikely(sk2 && !sock_flag(sk2, SOCK_RCU_FREE))) {
6973 WARN_ONCE(1, "Found non-RCU, unreferenced socket!");
6974 return NULL;
6975 }
6976 sk = sk2;
6977 }
6978 }
6979
6980 return sk;
6981 }
6982
6983 static struct sock *
bpf_skc_lookup(struct sk_buff * skb,struct bpf_sock_tuple * tuple,u32 len,u8 proto,u64 netns_id,u64 flags)6984 bpf_skc_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
6985 u8 proto, u64 netns_id, u64 flags)
6986 {
6987 struct net *caller_net;
6988 int ifindex;
6989
6990 if (skb->dev) {
6991 caller_net = dev_net(skb->dev);
6992 ifindex = skb->dev->ifindex;
6993 } else {
6994 caller_net = sock_net(skb->sk);
6995 ifindex = 0;
6996 }
6997
6998 return __bpf_skc_lookup(skb, tuple, len, caller_net, ifindex, proto,
6999 netns_id, flags, -1);
7000 }
7001
7002 static struct sock *
bpf_sk_lookup(struct sk_buff * skb,struct bpf_sock_tuple * tuple,u32 len,u8 proto,u64 netns_id,u64 flags)7003 bpf_sk_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
7004 u8 proto, u64 netns_id, u64 flags)
7005 {
7006 struct sock *sk = bpf_skc_lookup(skb, tuple, len, proto, netns_id,
7007 flags);
7008
7009 if (sk) {
7010 struct sock *sk2 = sk_to_full_sk(sk);
7011
7012 /* sk_to_full_sk() may return (sk)->rsk_listener, so make sure the original sk
7013 * sock refcnt is decremented to prevent a request_sock leak.
7014 */
7015 if (sk2 != sk) {
7016 sock_gen_put(sk);
7017 /* Ensure there is no need to bump sk2 refcnt */
7018 if (unlikely(sk2 && !sock_flag(sk2, SOCK_RCU_FREE))) {
7019 WARN_ONCE(1, "Found non-RCU, unreferenced socket!");
7020 return NULL;
7021 }
7022 sk = sk2;
7023 }
7024 }
7025
7026 return sk;
7027 }
7028
BPF_CALL_5(bpf_skc_lookup_tcp,struct sk_buff *,skb,struct bpf_sock_tuple *,tuple,u32,len,u64,netns_id,u64,flags)7029 BPF_CALL_5(bpf_skc_lookup_tcp, struct sk_buff *, skb,
7030 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
7031 {
7032 return (unsigned long)bpf_skc_lookup(skb, tuple, len, IPPROTO_TCP,
7033 netns_id, flags);
7034 }
7035
7036 static const struct bpf_func_proto bpf_skc_lookup_tcp_proto = {
7037 .func = bpf_skc_lookup_tcp,
7038 .gpl_only = false,
7039 .pkt_access = true,
7040 .ret_type = RET_PTR_TO_SOCK_COMMON_OR_NULL,
7041 .arg1_type = ARG_PTR_TO_CTX,
7042 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
7043 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
7044 .arg4_type = ARG_ANYTHING,
7045 .arg5_type = ARG_ANYTHING,
7046 };
7047
BPF_CALL_5(bpf_sk_lookup_tcp,struct sk_buff *,skb,struct bpf_sock_tuple *,tuple,u32,len,u64,netns_id,u64,flags)7048 BPF_CALL_5(bpf_sk_lookup_tcp, struct sk_buff *, skb,
7049 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
7050 {
7051 return (unsigned long)bpf_sk_lookup(skb, tuple, len, IPPROTO_TCP,
7052 netns_id, flags);
7053 }
7054
7055 static const struct bpf_func_proto bpf_sk_lookup_tcp_proto = {
7056 .func = bpf_sk_lookup_tcp,
7057 .gpl_only = false,
7058 .pkt_access = true,
7059 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
7060 .arg1_type = ARG_PTR_TO_CTX,
7061 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
7062 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
7063 .arg4_type = ARG_ANYTHING,
7064 .arg5_type = ARG_ANYTHING,
7065 };
7066
BPF_CALL_5(bpf_sk_lookup_udp,struct sk_buff *,skb,struct bpf_sock_tuple *,tuple,u32,len,u64,netns_id,u64,flags)7067 BPF_CALL_5(bpf_sk_lookup_udp, struct sk_buff *, skb,
7068 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
7069 {
7070 return (unsigned long)bpf_sk_lookup(skb, tuple, len, IPPROTO_UDP,
7071 netns_id, flags);
7072 }
7073
7074 static const struct bpf_func_proto bpf_sk_lookup_udp_proto = {
7075 .func = bpf_sk_lookup_udp,
7076 .gpl_only = false,
7077 .pkt_access = true,
7078 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
7079 .arg1_type = ARG_PTR_TO_CTX,
7080 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
7081 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
7082 .arg4_type = ARG_ANYTHING,
7083 .arg5_type = ARG_ANYTHING,
7084 };
7085
BPF_CALL_5(bpf_tc_skc_lookup_tcp,struct sk_buff *,skb,struct bpf_sock_tuple *,tuple,u32,len,u64,netns_id,u64,flags)7086 BPF_CALL_5(bpf_tc_skc_lookup_tcp, struct sk_buff *, skb,
7087 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
7088 {
7089 struct net_device *dev = skb->dev;
7090 int ifindex = dev->ifindex, sdif = dev_sdif(dev);
7091 struct net *caller_net = dev_net(dev);
7092
7093 return (unsigned long)__bpf_skc_lookup(skb, tuple, len, caller_net,
7094 ifindex, IPPROTO_TCP, netns_id,
7095 flags, sdif);
7096 }
7097
7098 static const struct bpf_func_proto bpf_tc_skc_lookup_tcp_proto = {
7099 .func = bpf_tc_skc_lookup_tcp,
7100 .gpl_only = false,
7101 .pkt_access = true,
7102 .ret_type = RET_PTR_TO_SOCK_COMMON_OR_NULL,
7103 .arg1_type = ARG_PTR_TO_CTX,
7104 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
7105 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
7106 .arg4_type = ARG_ANYTHING,
7107 .arg5_type = ARG_ANYTHING,
7108 };
7109
BPF_CALL_5(bpf_tc_sk_lookup_tcp,struct sk_buff *,skb,struct bpf_sock_tuple *,tuple,u32,len,u64,netns_id,u64,flags)7110 BPF_CALL_5(bpf_tc_sk_lookup_tcp, struct sk_buff *, skb,
7111 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
7112 {
7113 struct net_device *dev = skb->dev;
7114 int ifindex = dev->ifindex, sdif = dev_sdif(dev);
7115 struct net *caller_net = dev_net(dev);
7116
7117 return (unsigned long)__bpf_sk_lookup(skb, tuple, len, caller_net,
7118 ifindex, IPPROTO_TCP, netns_id,
7119 flags, sdif);
7120 }
7121
7122 static const struct bpf_func_proto bpf_tc_sk_lookup_tcp_proto = {
7123 .func = bpf_tc_sk_lookup_tcp,
7124 .gpl_only = false,
7125 .pkt_access = true,
7126 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
7127 .arg1_type = ARG_PTR_TO_CTX,
7128 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
7129 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
7130 .arg4_type = ARG_ANYTHING,
7131 .arg5_type = ARG_ANYTHING,
7132 };
7133
BPF_CALL_5(bpf_tc_sk_lookup_udp,struct sk_buff *,skb,struct bpf_sock_tuple *,tuple,u32,len,u64,netns_id,u64,flags)7134 BPF_CALL_5(bpf_tc_sk_lookup_udp, struct sk_buff *, skb,
7135 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
7136 {
7137 struct net_device *dev = skb->dev;
7138 int ifindex = dev->ifindex, sdif = dev_sdif(dev);
7139 struct net *caller_net = dev_net(dev);
7140
7141 return (unsigned long)__bpf_sk_lookup(skb, tuple, len, caller_net,
7142 ifindex, IPPROTO_UDP, netns_id,
7143 flags, sdif);
7144 }
7145
7146 static const struct bpf_func_proto bpf_tc_sk_lookup_udp_proto = {
7147 .func = bpf_tc_sk_lookup_udp,
7148 .gpl_only = false,
7149 .pkt_access = true,
7150 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
7151 .arg1_type = ARG_PTR_TO_CTX,
7152 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
7153 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
7154 .arg4_type = ARG_ANYTHING,
7155 .arg5_type = ARG_ANYTHING,
7156 };
7157
BPF_CALL_1(bpf_sk_release,struct sock *,sk)7158 BPF_CALL_1(bpf_sk_release, struct sock *, sk)
7159 {
7160 if (sk && sk_is_refcounted(sk))
7161 sock_gen_put(sk);
7162 return 0;
7163 }
7164
7165 static const struct bpf_func_proto bpf_sk_release_proto = {
7166 .func = bpf_sk_release,
7167 .gpl_only = false,
7168 .ret_type = RET_INTEGER,
7169 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON | OBJ_RELEASE,
7170 };
7171
BPF_CALL_5(bpf_xdp_sk_lookup_udp,struct xdp_buff *,ctx,struct bpf_sock_tuple *,tuple,u32,len,u32,netns_id,u64,flags)7172 BPF_CALL_5(bpf_xdp_sk_lookup_udp, struct xdp_buff *, ctx,
7173 struct bpf_sock_tuple *, tuple, u32, len, u32, netns_id, u64, flags)
7174 {
7175 struct net_device *dev = ctx->rxq->dev;
7176 int ifindex = dev->ifindex, sdif = dev_sdif(dev);
7177 struct net *caller_net = dev_net(dev);
7178
7179 return (unsigned long)__bpf_sk_lookup(NULL, tuple, len, caller_net,
7180 ifindex, IPPROTO_UDP, netns_id,
7181 flags, sdif);
7182 }
7183
7184 static const struct bpf_func_proto bpf_xdp_sk_lookup_udp_proto = {
7185 .func = bpf_xdp_sk_lookup_udp,
7186 .gpl_only = false,
7187 .pkt_access = true,
7188 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
7189 .arg1_type = ARG_PTR_TO_CTX,
7190 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
7191 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
7192 .arg4_type = ARG_ANYTHING,
7193 .arg5_type = ARG_ANYTHING,
7194 };
7195
BPF_CALL_5(bpf_xdp_skc_lookup_tcp,struct xdp_buff *,ctx,struct bpf_sock_tuple *,tuple,u32,len,u32,netns_id,u64,flags)7196 BPF_CALL_5(bpf_xdp_skc_lookup_tcp, struct xdp_buff *, ctx,
7197 struct bpf_sock_tuple *, tuple, u32, len, u32, netns_id, u64, flags)
7198 {
7199 struct net_device *dev = ctx->rxq->dev;
7200 int ifindex = dev->ifindex, sdif = dev_sdif(dev);
7201 struct net *caller_net = dev_net(dev);
7202
7203 return (unsigned long)__bpf_skc_lookup(NULL, tuple, len, caller_net,
7204 ifindex, IPPROTO_TCP, netns_id,
7205 flags, sdif);
7206 }
7207
7208 static const struct bpf_func_proto bpf_xdp_skc_lookup_tcp_proto = {
7209 .func = bpf_xdp_skc_lookup_tcp,
7210 .gpl_only = false,
7211 .pkt_access = true,
7212 .ret_type = RET_PTR_TO_SOCK_COMMON_OR_NULL,
7213 .arg1_type = ARG_PTR_TO_CTX,
7214 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
7215 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
7216 .arg4_type = ARG_ANYTHING,
7217 .arg5_type = ARG_ANYTHING,
7218 };
7219
BPF_CALL_5(bpf_xdp_sk_lookup_tcp,struct xdp_buff *,ctx,struct bpf_sock_tuple *,tuple,u32,len,u32,netns_id,u64,flags)7220 BPF_CALL_5(bpf_xdp_sk_lookup_tcp, struct xdp_buff *, ctx,
7221 struct bpf_sock_tuple *, tuple, u32, len, u32, netns_id, u64, flags)
7222 {
7223 struct net_device *dev = ctx->rxq->dev;
7224 int ifindex = dev->ifindex, sdif = dev_sdif(dev);
7225 struct net *caller_net = dev_net(dev);
7226
7227 return (unsigned long)__bpf_sk_lookup(NULL, tuple, len, caller_net,
7228 ifindex, IPPROTO_TCP, netns_id,
7229 flags, sdif);
7230 }
7231
7232 static const struct bpf_func_proto bpf_xdp_sk_lookup_tcp_proto = {
7233 .func = bpf_xdp_sk_lookup_tcp,
7234 .gpl_only = false,
7235 .pkt_access = true,
7236 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
7237 .arg1_type = ARG_PTR_TO_CTX,
7238 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
7239 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
7240 .arg4_type = ARG_ANYTHING,
7241 .arg5_type = ARG_ANYTHING,
7242 };
7243
BPF_CALL_5(bpf_sock_addr_skc_lookup_tcp,struct bpf_sock_addr_kern *,ctx,struct bpf_sock_tuple *,tuple,u32,len,u64,netns_id,u64,flags)7244 BPF_CALL_5(bpf_sock_addr_skc_lookup_tcp, struct bpf_sock_addr_kern *, ctx,
7245 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
7246 {
7247 return (unsigned long)__bpf_skc_lookup(NULL, tuple, len,
7248 sock_net(ctx->sk), 0,
7249 IPPROTO_TCP, netns_id, flags,
7250 -1);
7251 }
7252
7253 static const struct bpf_func_proto bpf_sock_addr_skc_lookup_tcp_proto = {
7254 .func = bpf_sock_addr_skc_lookup_tcp,
7255 .gpl_only = false,
7256 .ret_type = RET_PTR_TO_SOCK_COMMON_OR_NULL,
7257 .arg1_type = ARG_PTR_TO_CTX,
7258 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
7259 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
7260 .arg4_type = ARG_ANYTHING,
7261 .arg5_type = ARG_ANYTHING,
7262 };
7263
BPF_CALL_5(bpf_sock_addr_sk_lookup_tcp,struct bpf_sock_addr_kern *,ctx,struct bpf_sock_tuple *,tuple,u32,len,u64,netns_id,u64,flags)7264 BPF_CALL_5(bpf_sock_addr_sk_lookup_tcp, struct bpf_sock_addr_kern *, ctx,
7265 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
7266 {
7267 return (unsigned long)__bpf_sk_lookup(NULL, tuple, len,
7268 sock_net(ctx->sk), 0, IPPROTO_TCP,
7269 netns_id, flags, -1);
7270 }
7271
7272 static const struct bpf_func_proto bpf_sock_addr_sk_lookup_tcp_proto = {
7273 .func = bpf_sock_addr_sk_lookup_tcp,
7274 .gpl_only = false,
7275 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
7276 .arg1_type = ARG_PTR_TO_CTX,
7277 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
7278 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
7279 .arg4_type = ARG_ANYTHING,
7280 .arg5_type = ARG_ANYTHING,
7281 };
7282
BPF_CALL_5(bpf_sock_addr_sk_lookup_udp,struct bpf_sock_addr_kern *,ctx,struct bpf_sock_tuple *,tuple,u32,len,u64,netns_id,u64,flags)7283 BPF_CALL_5(bpf_sock_addr_sk_lookup_udp, struct bpf_sock_addr_kern *, ctx,
7284 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
7285 {
7286 return (unsigned long)__bpf_sk_lookup(NULL, tuple, len,
7287 sock_net(ctx->sk), 0, IPPROTO_UDP,
7288 netns_id, flags, -1);
7289 }
7290
7291 static const struct bpf_func_proto bpf_sock_addr_sk_lookup_udp_proto = {
7292 .func = bpf_sock_addr_sk_lookup_udp,
7293 .gpl_only = false,
7294 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
7295 .arg1_type = ARG_PTR_TO_CTX,
7296 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
7297 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
7298 .arg4_type = ARG_ANYTHING,
7299 .arg5_type = ARG_ANYTHING,
7300 };
7301
bpf_tcp_sock_is_valid_access(int off,int size,enum bpf_access_type type,struct bpf_insn_access_aux * info)7302 bool bpf_tcp_sock_is_valid_access(int off, int size, enum bpf_access_type type,
7303 struct bpf_insn_access_aux *info)
7304 {
7305 if (off < 0 || off >= offsetofend(struct bpf_tcp_sock,
7306 icsk_retransmits))
7307 return false;
7308
7309 if (off % size != 0)
7310 return false;
7311
7312 switch (off) {
7313 case offsetof(struct bpf_tcp_sock, bytes_received):
7314 case offsetof(struct bpf_tcp_sock, bytes_acked):
7315 return size == sizeof(__u64);
7316 default:
7317 return size == sizeof(__u32);
7318 }
7319 }
7320
bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type,const struct bpf_insn * si,struct bpf_insn * insn_buf,struct bpf_prog * prog,u32 * target_size)7321 u32 bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type,
7322 const struct bpf_insn *si,
7323 struct bpf_insn *insn_buf,
7324 struct bpf_prog *prog, u32 *target_size)
7325 {
7326 struct bpf_insn *insn = insn_buf;
7327
7328 #define BPF_TCP_SOCK_GET_COMMON(FIELD) \
7329 do { \
7330 BUILD_BUG_ON(sizeof_field(struct tcp_sock, FIELD) > \
7331 sizeof_field(struct bpf_tcp_sock, FIELD)); \
7332 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct tcp_sock, FIELD),\
7333 si->dst_reg, si->src_reg, \
7334 offsetof(struct tcp_sock, FIELD)); \
7335 } while (0)
7336
7337 #define BPF_INET_SOCK_GET_COMMON(FIELD) \
7338 do { \
7339 BUILD_BUG_ON(sizeof_field(struct inet_connection_sock, \
7340 FIELD) > \
7341 sizeof_field(struct bpf_tcp_sock, FIELD)); \
7342 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
7343 struct inet_connection_sock, \
7344 FIELD), \
7345 si->dst_reg, si->src_reg, \
7346 offsetof( \
7347 struct inet_connection_sock, \
7348 FIELD)); \
7349 } while (0)
7350
7351 BTF_TYPE_EMIT(struct bpf_tcp_sock);
7352
7353 switch (si->off) {
7354 case offsetof(struct bpf_tcp_sock, rtt_min):
7355 BUILD_BUG_ON(sizeof_field(struct tcp_sock, rtt_min) !=
7356 sizeof(struct minmax));
7357 BUILD_BUG_ON(sizeof(struct minmax) <
7358 sizeof(struct minmax_sample));
7359
7360 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
7361 offsetof(struct tcp_sock, rtt_min) +
7362 offsetof(struct minmax_sample, v));
7363 break;
7364 case offsetof(struct bpf_tcp_sock, snd_cwnd):
7365 BPF_TCP_SOCK_GET_COMMON(snd_cwnd);
7366 break;
7367 case offsetof(struct bpf_tcp_sock, srtt_us):
7368 BPF_TCP_SOCK_GET_COMMON(srtt_us);
7369 break;
7370 case offsetof(struct bpf_tcp_sock, snd_ssthresh):
7371 BPF_TCP_SOCK_GET_COMMON(snd_ssthresh);
7372 break;
7373 case offsetof(struct bpf_tcp_sock, rcv_nxt):
7374 BPF_TCP_SOCK_GET_COMMON(rcv_nxt);
7375 break;
7376 case offsetof(struct bpf_tcp_sock, snd_nxt):
7377 BPF_TCP_SOCK_GET_COMMON(snd_nxt);
7378 break;
7379 case offsetof(struct bpf_tcp_sock, snd_una):
7380 BPF_TCP_SOCK_GET_COMMON(snd_una);
7381 break;
7382 case offsetof(struct bpf_tcp_sock, mss_cache):
7383 BPF_TCP_SOCK_GET_COMMON(mss_cache);
7384 break;
7385 case offsetof(struct bpf_tcp_sock, ecn_flags):
7386 BPF_TCP_SOCK_GET_COMMON(ecn_flags);
7387 break;
7388 case offsetof(struct bpf_tcp_sock, rate_delivered):
7389 BPF_TCP_SOCK_GET_COMMON(rate_delivered);
7390 break;
7391 case offsetof(struct bpf_tcp_sock, rate_interval_us):
7392 BPF_TCP_SOCK_GET_COMMON(rate_interval_us);
7393 break;
7394 case offsetof(struct bpf_tcp_sock, packets_out):
7395 BPF_TCP_SOCK_GET_COMMON(packets_out);
7396 break;
7397 case offsetof(struct bpf_tcp_sock, retrans_out):
7398 BPF_TCP_SOCK_GET_COMMON(retrans_out);
7399 break;
7400 case offsetof(struct bpf_tcp_sock, total_retrans):
7401 BPF_TCP_SOCK_GET_COMMON(total_retrans);
7402 break;
7403 case offsetof(struct bpf_tcp_sock, segs_in):
7404 BPF_TCP_SOCK_GET_COMMON(segs_in);
7405 break;
7406 case offsetof(struct bpf_tcp_sock, data_segs_in):
7407 BPF_TCP_SOCK_GET_COMMON(data_segs_in);
7408 break;
7409 case offsetof(struct bpf_tcp_sock, segs_out):
7410 BPF_TCP_SOCK_GET_COMMON(segs_out);
7411 break;
7412 case offsetof(struct bpf_tcp_sock, data_segs_out):
7413 BPF_TCP_SOCK_GET_COMMON(data_segs_out);
7414 break;
7415 case offsetof(struct bpf_tcp_sock, lost_out):
7416 BPF_TCP_SOCK_GET_COMMON(lost_out);
7417 break;
7418 case offsetof(struct bpf_tcp_sock, sacked_out):
7419 BPF_TCP_SOCK_GET_COMMON(sacked_out);
7420 break;
7421 case offsetof(struct bpf_tcp_sock, bytes_received):
7422 BPF_TCP_SOCK_GET_COMMON(bytes_received);
7423 break;
7424 case offsetof(struct bpf_tcp_sock, bytes_acked):
7425 BPF_TCP_SOCK_GET_COMMON(bytes_acked);
7426 break;
7427 case offsetof(struct bpf_tcp_sock, dsack_dups):
7428 BPF_TCP_SOCK_GET_COMMON(dsack_dups);
7429 break;
7430 case offsetof(struct bpf_tcp_sock, delivered):
7431 BPF_TCP_SOCK_GET_COMMON(delivered);
7432 break;
7433 case offsetof(struct bpf_tcp_sock, delivered_ce):
7434 BPF_TCP_SOCK_GET_COMMON(delivered_ce);
7435 break;
7436 case offsetof(struct bpf_tcp_sock, icsk_retransmits):
7437 BPF_INET_SOCK_GET_COMMON(icsk_retransmits);
7438 break;
7439 }
7440
7441 return insn - insn_buf;
7442 }
7443
BPF_CALL_1(bpf_tcp_sock,struct sock *,sk)7444 BPF_CALL_1(bpf_tcp_sock, struct sock *, sk)
7445 {
7446 if (sk_fullsock(sk) && sk->sk_protocol == IPPROTO_TCP)
7447 return (unsigned long)sk;
7448
7449 return (unsigned long)NULL;
7450 }
7451
7452 const struct bpf_func_proto bpf_tcp_sock_proto = {
7453 .func = bpf_tcp_sock,
7454 .gpl_only = false,
7455 .ret_type = RET_PTR_TO_TCP_SOCK_OR_NULL,
7456 .arg1_type = ARG_PTR_TO_SOCK_COMMON,
7457 };
7458
BPF_CALL_1(bpf_get_listener_sock,struct sock *,sk)7459 BPF_CALL_1(bpf_get_listener_sock, struct sock *, sk)
7460 {
7461 sk = sk_to_full_sk(sk);
7462
7463 if (sk && sk->sk_state == TCP_LISTEN && sock_flag(sk, SOCK_RCU_FREE))
7464 return (unsigned long)sk;
7465
7466 return (unsigned long)NULL;
7467 }
7468
7469 static const struct bpf_func_proto bpf_get_listener_sock_proto = {
7470 .func = bpf_get_listener_sock,
7471 .gpl_only = false,
7472 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
7473 .arg1_type = ARG_PTR_TO_SOCK_COMMON,
7474 };
7475
BPF_CALL_1(bpf_skb_ecn_set_ce,struct sk_buff *,skb)7476 BPF_CALL_1(bpf_skb_ecn_set_ce, struct sk_buff *, skb)
7477 {
7478 unsigned int iphdr_len;
7479
7480 switch (skb_protocol(skb, true)) {
7481 case cpu_to_be16(ETH_P_IP):
7482 iphdr_len = sizeof(struct iphdr);
7483 break;
7484 case cpu_to_be16(ETH_P_IPV6):
7485 iphdr_len = sizeof(struct ipv6hdr);
7486 break;
7487 default:
7488 return 0;
7489 }
7490
7491 if (skb_headlen(skb) < iphdr_len)
7492 return 0;
7493
7494 if (skb_cloned(skb) && !skb_clone_writable(skb, iphdr_len))
7495 return 0;
7496
7497 return INET_ECN_set_ce(skb);
7498 }
7499
bpf_xdp_sock_is_valid_access(int off,int size,enum bpf_access_type type,struct bpf_insn_access_aux * info)7500 bool bpf_xdp_sock_is_valid_access(int off, int size, enum bpf_access_type type,
7501 struct bpf_insn_access_aux *info)
7502 {
7503 if (off < 0 || off >= offsetofend(struct bpf_xdp_sock, queue_id))
7504 return false;
7505
7506 if (off % size != 0)
7507 return false;
7508
7509 switch (off) {
7510 default:
7511 return size == sizeof(__u32);
7512 }
7513 }
7514
bpf_xdp_sock_convert_ctx_access(enum bpf_access_type type,const struct bpf_insn * si,struct bpf_insn * insn_buf,struct bpf_prog * prog,u32 * target_size)7515 u32 bpf_xdp_sock_convert_ctx_access(enum bpf_access_type type,
7516 const struct bpf_insn *si,
7517 struct bpf_insn *insn_buf,
7518 struct bpf_prog *prog, u32 *target_size)
7519 {
7520 struct bpf_insn *insn = insn_buf;
7521
7522 #define BPF_XDP_SOCK_GET(FIELD) \
7523 do { \
7524 BUILD_BUG_ON(sizeof_field(struct xdp_sock, FIELD) > \
7525 sizeof_field(struct bpf_xdp_sock, FIELD)); \
7526 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_sock, FIELD),\
7527 si->dst_reg, si->src_reg, \
7528 offsetof(struct xdp_sock, FIELD)); \
7529 } while (0)
7530
7531 BTF_TYPE_EMIT(struct bpf_xdp_sock);
7532
7533 switch (si->off) {
7534 case offsetof(struct bpf_xdp_sock, queue_id):
7535 BPF_XDP_SOCK_GET(queue_id);
7536 break;
7537 }
7538
7539 return insn - insn_buf;
7540 }
7541
7542 static const struct bpf_func_proto bpf_skb_ecn_set_ce_proto = {
7543 .func = bpf_skb_ecn_set_ce,
7544 .gpl_only = false,
7545 .ret_type = RET_INTEGER,
7546 .arg1_type = ARG_PTR_TO_CTX,
7547 };
7548
BPF_CALL_5(bpf_tcp_check_syncookie,struct sock *,sk,void *,iph,u32,iph_len,struct tcphdr *,th,u32,th_len)7549 BPF_CALL_5(bpf_tcp_check_syncookie, struct sock *, sk, void *, iph, u32, iph_len,
7550 struct tcphdr *, th, u32, th_len)
7551 {
7552 #ifdef CONFIG_SYN_COOKIES
7553 int ret;
7554
7555 if (unlikely(!sk || th_len < sizeof(*th)))
7556 return -EINVAL;
7557
7558 /* sk_listener() allows TCP_NEW_SYN_RECV, which makes no sense here. */
7559 if (sk->sk_protocol != IPPROTO_TCP || sk->sk_state != TCP_LISTEN)
7560 return -EINVAL;
7561
7562 if (!READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_syncookies))
7563 return -EINVAL;
7564
7565 if (!th->ack || th->rst || th->syn)
7566 return -ENOENT;
7567
7568 if (unlikely(iph_len < sizeof(struct iphdr)))
7569 return -EINVAL;
7570
7571 if (tcp_synq_no_recent_overflow(sk))
7572 return -ENOENT;
7573
7574 /* Both struct iphdr and struct ipv6hdr have the version field at the
7575 * same offset so we can cast to the shorter header (struct iphdr).
7576 */
7577 switch (((struct iphdr *)iph)->version) {
7578 case 4:
7579 if (sk->sk_family == AF_INET6 && ipv6_only_sock(sk))
7580 return -EINVAL;
7581
7582 ret = __cookie_v4_check((struct iphdr *)iph, th);
7583 break;
7584
7585 #if IS_BUILTIN(CONFIG_IPV6)
7586 case 6:
7587 if (unlikely(iph_len < sizeof(struct ipv6hdr)))
7588 return -EINVAL;
7589
7590 if (sk->sk_family != AF_INET6)
7591 return -EINVAL;
7592
7593 ret = __cookie_v6_check((struct ipv6hdr *)iph, th);
7594 break;
7595 #endif /* CONFIG_IPV6 */
7596
7597 default:
7598 return -EPROTONOSUPPORT;
7599 }
7600
7601 if (ret > 0)
7602 return 0;
7603
7604 return -ENOENT;
7605 #else
7606 return -ENOTSUPP;
7607 #endif
7608 }
7609
7610 static const struct bpf_func_proto bpf_tcp_check_syncookie_proto = {
7611 .func = bpf_tcp_check_syncookie,
7612 .gpl_only = true,
7613 .pkt_access = true,
7614 .ret_type = RET_INTEGER,
7615 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
7616 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
7617 .arg3_type = ARG_CONST_SIZE,
7618 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
7619 .arg5_type = ARG_CONST_SIZE,
7620 };
7621
BPF_CALL_5(bpf_tcp_gen_syncookie,struct sock *,sk,void *,iph,u32,iph_len,struct tcphdr *,th,u32,th_len)7622 BPF_CALL_5(bpf_tcp_gen_syncookie, struct sock *, sk, void *, iph, u32, iph_len,
7623 struct tcphdr *, th, u32, th_len)
7624 {
7625 #ifdef CONFIG_SYN_COOKIES
7626 u32 cookie;
7627 u16 mss;
7628
7629 if (unlikely(!sk || th_len < sizeof(*th) || th_len != th->doff * 4))
7630 return -EINVAL;
7631
7632 if (sk->sk_protocol != IPPROTO_TCP || sk->sk_state != TCP_LISTEN)
7633 return -EINVAL;
7634
7635 if (!READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_syncookies))
7636 return -ENOENT;
7637
7638 if (!th->syn || th->ack || th->fin || th->rst)
7639 return -EINVAL;
7640
7641 if (unlikely(iph_len < sizeof(struct iphdr)))
7642 return -EINVAL;
7643
7644 /* Both struct iphdr and struct ipv6hdr have the version field at the
7645 * same offset so we can cast to the shorter header (struct iphdr).
7646 */
7647 switch (((struct iphdr *)iph)->version) {
7648 case 4:
7649 if (sk->sk_family == AF_INET6 && ipv6_only_sock(sk))
7650 return -EINVAL;
7651
7652 mss = tcp_v4_get_syncookie(sk, iph, th, &cookie);
7653 break;
7654
7655 #if IS_BUILTIN(CONFIG_IPV6)
7656 case 6:
7657 if (unlikely(iph_len < sizeof(struct ipv6hdr)))
7658 return -EINVAL;
7659
7660 if (sk->sk_family != AF_INET6)
7661 return -EINVAL;
7662
7663 mss = tcp_v6_get_syncookie(sk, iph, th, &cookie);
7664 break;
7665 #endif /* CONFIG_IPV6 */
7666
7667 default:
7668 return -EPROTONOSUPPORT;
7669 }
7670 if (mss == 0)
7671 return -ENOENT;
7672
7673 return cookie | ((u64)mss << 32);
7674 #else
7675 return -EOPNOTSUPP;
7676 #endif /* CONFIG_SYN_COOKIES */
7677 }
7678
7679 static const struct bpf_func_proto bpf_tcp_gen_syncookie_proto = {
7680 .func = bpf_tcp_gen_syncookie,
7681 .gpl_only = true, /* __cookie_v*_init_sequence() is GPL */
7682 .pkt_access = true,
7683 .ret_type = RET_INTEGER,
7684 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
7685 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
7686 .arg3_type = ARG_CONST_SIZE,
7687 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
7688 .arg5_type = ARG_CONST_SIZE,
7689 };
7690
BPF_CALL_3(bpf_sk_assign,struct sk_buff *,skb,struct sock *,sk,u64,flags)7691 BPF_CALL_3(bpf_sk_assign, struct sk_buff *, skb, struct sock *, sk, u64, flags)
7692 {
7693 if (!sk || flags != 0)
7694 return -EINVAL;
7695 if (!skb_at_tc_ingress(skb))
7696 return -EOPNOTSUPP;
7697 if (unlikely(dev_net(skb->dev) != sock_net(sk)))
7698 return -ENETUNREACH;
7699 if (sk_unhashed(sk))
7700 return -EOPNOTSUPP;
7701 if (sk_is_refcounted(sk) &&
7702 unlikely(!refcount_inc_not_zero(&sk->sk_refcnt)))
7703 return -ENOENT;
7704
7705 skb_orphan(skb);
7706 skb->sk = sk;
7707 skb->destructor = sock_pfree;
7708
7709 return 0;
7710 }
7711
7712 static const struct bpf_func_proto bpf_sk_assign_proto = {
7713 .func = bpf_sk_assign,
7714 .gpl_only = false,
7715 .ret_type = RET_INTEGER,
7716 .arg1_type = ARG_PTR_TO_CTX,
7717 .arg2_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
7718 .arg3_type = ARG_ANYTHING,
7719 };
7720
bpf_search_tcp_opt(const u8 * op,const u8 * opend,u8 search_kind,const u8 * magic,u8 magic_len,bool * eol)7721 static const u8 *bpf_search_tcp_opt(const u8 *op, const u8 *opend,
7722 u8 search_kind, const u8 *magic,
7723 u8 magic_len, bool *eol)
7724 {
7725 u8 kind, kind_len;
7726
7727 *eol = false;
7728
7729 while (op < opend) {
7730 kind = op[0];
7731
7732 if (kind == TCPOPT_EOL) {
7733 *eol = true;
7734 return ERR_PTR(-ENOMSG);
7735 } else if (kind == TCPOPT_NOP) {
7736 op++;
7737 continue;
7738 }
7739
7740 if (opend - op < 2 || opend - op < op[1] || op[1] < 2)
7741 /* Something is wrong in the received header.
7742 * Follow the TCP stack's tcp_parse_options()
7743 * and just bail here.
7744 */
7745 return ERR_PTR(-EFAULT);
7746
7747 kind_len = op[1];
7748 if (search_kind == kind) {
7749 if (!magic_len)
7750 return op;
7751
7752 if (magic_len > kind_len - 2)
7753 return ERR_PTR(-ENOMSG);
7754
7755 if (!memcmp(&op[2], magic, magic_len))
7756 return op;
7757 }
7758
7759 op += kind_len;
7760 }
7761
7762 return ERR_PTR(-ENOMSG);
7763 }
7764
BPF_CALL_4(bpf_sock_ops_load_hdr_opt,struct bpf_sock_ops_kern *,bpf_sock,void *,search_res,u32,len,u64,flags)7765 BPF_CALL_4(bpf_sock_ops_load_hdr_opt, struct bpf_sock_ops_kern *, bpf_sock,
7766 void *, search_res, u32, len, u64, flags)
7767 {
7768 bool eol, load_syn = flags & BPF_LOAD_HDR_OPT_TCP_SYN;
7769 const u8 *op, *opend, *magic, *search = search_res;
7770 u8 search_kind, search_len, copy_len, magic_len;
7771 int ret;
7772
7773 if (!is_locked_tcp_sock_ops(bpf_sock))
7774 return -EOPNOTSUPP;
7775
7776 /* 2 byte is the minimal option len except TCPOPT_NOP and
7777 * TCPOPT_EOL which are useless for the bpf prog to learn
7778 * and this helper disallow loading them also.
7779 */
7780 if (len < 2 || flags & ~BPF_LOAD_HDR_OPT_TCP_SYN)
7781 return -EINVAL;
7782
7783 search_kind = search[0];
7784 search_len = search[1];
7785
7786 if (search_len > len || search_kind == TCPOPT_NOP ||
7787 search_kind == TCPOPT_EOL)
7788 return -EINVAL;
7789
7790 if (search_kind == TCPOPT_EXP || search_kind == 253) {
7791 /* 16 or 32 bit magic. +2 for kind and kind length */
7792 if (search_len != 4 && search_len != 6)
7793 return -EINVAL;
7794 magic = &search[2];
7795 magic_len = search_len - 2;
7796 } else {
7797 if (search_len)
7798 return -EINVAL;
7799 magic = NULL;
7800 magic_len = 0;
7801 }
7802
7803 if (load_syn) {
7804 ret = bpf_sock_ops_get_syn(bpf_sock, TCP_BPF_SYN, &op);
7805 if (ret < 0)
7806 return ret;
7807
7808 opend = op + ret;
7809 op += sizeof(struct tcphdr);
7810 } else {
7811 if (!bpf_sock->skb ||
7812 bpf_sock->op == BPF_SOCK_OPS_HDR_OPT_LEN_CB)
7813 /* This bpf_sock->op cannot call this helper */
7814 return -EPERM;
7815
7816 opend = bpf_sock->skb_data_end;
7817 op = bpf_sock->skb->data + sizeof(struct tcphdr);
7818 }
7819
7820 op = bpf_search_tcp_opt(op, opend, search_kind, magic, magic_len,
7821 &eol);
7822 if (IS_ERR(op))
7823 return PTR_ERR(op);
7824
7825 copy_len = op[1];
7826 ret = copy_len;
7827 if (copy_len > len) {
7828 ret = -ENOSPC;
7829 copy_len = len;
7830 }
7831
7832 memcpy(search_res, op, copy_len);
7833 return ret;
7834 }
7835
7836 static const struct bpf_func_proto bpf_sock_ops_load_hdr_opt_proto = {
7837 .func = bpf_sock_ops_load_hdr_opt,
7838 .gpl_only = false,
7839 .ret_type = RET_INTEGER,
7840 .arg1_type = ARG_PTR_TO_CTX,
7841 .arg2_type = ARG_PTR_TO_MEM | MEM_WRITE,
7842 .arg3_type = ARG_CONST_SIZE,
7843 .arg4_type = ARG_ANYTHING,
7844 };
7845
BPF_CALL_4(bpf_sock_ops_store_hdr_opt,struct bpf_sock_ops_kern *,bpf_sock,const void *,from,u32,len,u64,flags)7846 BPF_CALL_4(bpf_sock_ops_store_hdr_opt, struct bpf_sock_ops_kern *, bpf_sock,
7847 const void *, from, u32, len, u64, flags)
7848 {
7849 u8 new_kind, new_kind_len, magic_len = 0, *opend;
7850 const u8 *op, *new_op, *magic = NULL;
7851 struct sk_buff *skb;
7852 bool eol;
7853
7854 if (bpf_sock->op != BPF_SOCK_OPS_WRITE_HDR_OPT_CB)
7855 return -EPERM;
7856
7857 if (len < 2 || flags)
7858 return -EINVAL;
7859
7860 new_op = from;
7861 new_kind = new_op[0];
7862 new_kind_len = new_op[1];
7863
7864 if (new_kind_len > len || new_kind == TCPOPT_NOP ||
7865 new_kind == TCPOPT_EOL)
7866 return -EINVAL;
7867
7868 if (new_kind_len > bpf_sock->remaining_opt_len)
7869 return -ENOSPC;
7870
7871 /* 253 is another experimental kind */
7872 if (new_kind == TCPOPT_EXP || new_kind == 253) {
7873 if (new_kind_len < 4)
7874 return -EINVAL;
7875 /* Match for the 2 byte magic also.
7876 * RFC 6994: the magic could be 2 or 4 bytes.
7877 * Hence, matching by 2 byte only is on the
7878 * conservative side but it is the right
7879 * thing to do for the 'search-for-duplication'
7880 * purpose.
7881 */
7882 magic = &new_op[2];
7883 magic_len = 2;
7884 }
7885
7886 /* Check for duplication */
7887 skb = bpf_sock->skb;
7888 op = skb->data + sizeof(struct tcphdr);
7889 opend = bpf_sock->skb_data_end;
7890
7891 op = bpf_search_tcp_opt(op, opend, new_kind, magic, magic_len,
7892 &eol);
7893 if (!IS_ERR(op))
7894 return -EEXIST;
7895
7896 if (PTR_ERR(op) != -ENOMSG)
7897 return PTR_ERR(op);
7898
7899 if (eol)
7900 /* The option has been ended. Treat it as no more
7901 * header option can be written.
7902 */
7903 return -ENOSPC;
7904
7905 /* No duplication found. Store the header option. */
7906 memcpy(opend, from, new_kind_len);
7907
7908 bpf_sock->remaining_opt_len -= new_kind_len;
7909 bpf_sock->skb_data_end += new_kind_len;
7910
7911 return 0;
7912 }
7913
7914 static const struct bpf_func_proto bpf_sock_ops_store_hdr_opt_proto = {
7915 .func = bpf_sock_ops_store_hdr_opt,
7916 .gpl_only = false,
7917 .ret_type = RET_INTEGER,
7918 .arg1_type = ARG_PTR_TO_CTX,
7919 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
7920 .arg3_type = ARG_CONST_SIZE,
7921 .arg4_type = ARG_ANYTHING,
7922 };
7923
BPF_CALL_3(bpf_sock_ops_reserve_hdr_opt,struct bpf_sock_ops_kern *,bpf_sock,u32,len,u64,flags)7924 BPF_CALL_3(bpf_sock_ops_reserve_hdr_opt, struct bpf_sock_ops_kern *, bpf_sock,
7925 u32, len, u64, flags)
7926 {
7927 if (bpf_sock->op != BPF_SOCK_OPS_HDR_OPT_LEN_CB)
7928 return -EPERM;
7929
7930 if (flags || len < 2)
7931 return -EINVAL;
7932
7933 if (len > bpf_sock->remaining_opt_len)
7934 return -ENOSPC;
7935
7936 bpf_sock->remaining_opt_len -= len;
7937
7938 return 0;
7939 }
7940
7941 static const struct bpf_func_proto bpf_sock_ops_reserve_hdr_opt_proto = {
7942 .func = bpf_sock_ops_reserve_hdr_opt,
7943 .gpl_only = false,
7944 .ret_type = RET_INTEGER,
7945 .arg1_type = ARG_PTR_TO_CTX,
7946 .arg2_type = ARG_ANYTHING,
7947 .arg3_type = ARG_ANYTHING,
7948 };
7949
BPF_CALL_3(bpf_skb_set_tstamp,struct sk_buff *,skb,u64,tstamp,u32,tstamp_type)7950 BPF_CALL_3(bpf_skb_set_tstamp, struct sk_buff *, skb,
7951 u64, tstamp, u32, tstamp_type)
7952 {
7953 /* skb_clear_delivery_time() is done for inet protocol */
7954 if (skb->protocol != htons(ETH_P_IP) &&
7955 skb->protocol != htons(ETH_P_IPV6))
7956 return -EOPNOTSUPP;
7957
7958 switch (tstamp_type) {
7959 case BPF_SKB_CLOCK_REALTIME:
7960 skb->tstamp = tstamp;
7961 skb->tstamp_type = SKB_CLOCK_REALTIME;
7962 break;
7963 case BPF_SKB_CLOCK_MONOTONIC:
7964 if (!tstamp)
7965 return -EINVAL;
7966 skb->tstamp = tstamp;
7967 skb->tstamp_type = SKB_CLOCK_MONOTONIC;
7968 break;
7969 case BPF_SKB_CLOCK_TAI:
7970 if (!tstamp)
7971 return -EINVAL;
7972 skb->tstamp = tstamp;
7973 skb->tstamp_type = SKB_CLOCK_TAI;
7974 break;
7975 default:
7976 return -EINVAL;
7977 }
7978
7979 return 0;
7980 }
7981
7982 static const struct bpf_func_proto bpf_skb_set_tstamp_proto = {
7983 .func = bpf_skb_set_tstamp,
7984 .gpl_only = false,
7985 .ret_type = RET_INTEGER,
7986 .arg1_type = ARG_PTR_TO_CTX,
7987 .arg2_type = ARG_ANYTHING,
7988 .arg3_type = ARG_ANYTHING,
7989 };
7990
7991 #ifdef CONFIG_SYN_COOKIES
BPF_CALL_3(bpf_tcp_raw_gen_syncookie_ipv4,struct iphdr *,iph,struct tcphdr *,th,u32,th_len)7992 BPF_CALL_3(bpf_tcp_raw_gen_syncookie_ipv4, struct iphdr *, iph,
7993 struct tcphdr *, th, u32, th_len)
7994 {
7995 u32 cookie;
7996 u16 mss;
7997
7998 if (unlikely(th_len < sizeof(*th) || th_len != th->doff * 4))
7999 return -EINVAL;
8000
8001 mss = tcp_parse_mss_option(th, 0) ?: TCP_MSS_DEFAULT;
8002 cookie = __cookie_v4_init_sequence(iph, th, &mss);
8003
8004 return cookie | ((u64)mss << 32);
8005 }
8006
8007 static const struct bpf_func_proto bpf_tcp_raw_gen_syncookie_ipv4_proto = {
8008 .func = bpf_tcp_raw_gen_syncookie_ipv4,
8009 .gpl_only = true, /* __cookie_v4_init_sequence() is GPL */
8010 .pkt_access = true,
8011 .ret_type = RET_INTEGER,
8012 .arg1_type = ARG_PTR_TO_FIXED_SIZE_MEM | MEM_RDONLY,
8013 .arg1_size = sizeof(struct iphdr),
8014 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
8015 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
8016 };
8017
BPF_CALL_3(bpf_tcp_raw_gen_syncookie_ipv6,struct ipv6hdr *,iph,struct tcphdr *,th,u32,th_len)8018 BPF_CALL_3(bpf_tcp_raw_gen_syncookie_ipv6, struct ipv6hdr *, iph,
8019 struct tcphdr *, th, u32, th_len)
8020 {
8021 #if IS_BUILTIN(CONFIG_IPV6)
8022 const u16 mss_clamp = IPV6_MIN_MTU - sizeof(struct tcphdr) -
8023 sizeof(struct ipv6hdr);
8024 u32 cookie;
8025 u16 mss;
8026
8027 if (unlikely(th_len < sizeof(*th) || th_len != th->doff * 4))
8028 return -EINVAL;
8029
8030 mss = tcp_parse_mss_option(th, 0) ?: mss_clamp;
8031 cookie = __cookie_v6_init_sequence(iph, th, &mss);
8032
8033 return cookie | ((u64)mss << 32);
8034 #else
8035 return -EPROTONOSUPPORT;
8036 #endif
8037 }
8038
8039 static const struct bpf_func_proto bpf_tcp_raw_gen_syncookie_ipv6_proto = {
8040 .func = bpf_tcp_raw_gen_syncookie_ipv6,
8041 .gpl_only = true, /* __cookie_v6_init_sequence() is GPL */
8042 .pkt_access = true,
8043 .ret_type = RET_INTEGER,
8044 .arg1_type = ARG_PTR_TO_FIXED_SIZE_MEM | MEM_RDONLY,
8045 .arg1_size = sizeof(struct ipv6hdr),
8046 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
8047 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
8048 };
8049
BPF_CALL_2(bpf_tcp_raw_check_syncookie_ipv4,struct iphdr *,iph,struct tcphdr *,th)8050 BPF_CALL_2(bpf_tcp_raw_check_syncookie_ipv4, struct iphdr *, iph,
8051 struct tcphdr *, th)
8052 {
8053 if (__cookie_v4_check(iph, th) > 0)
8054 return 0;
8055
8056 return -EACCES;
8057 }
8058
8059 static const struct bpf_func_proto bpf_tcp_raw_check_syncookie_ipv4_proto = {
8060 .func = bpf_tcp_raw_check_syncookie_ipv4,
8061 .gpl_only = true, /* __cookie_v4_check is GPL */
8062 .pkt_access = true,
8063 .ret_type = RET_INTEGER,
8064 .arg1_type = ARG_PTR_TO_FIXED_SIZE_MEM | MEM_RDONLY,
8065 .arg1_size = sizeof(struct iphdr),
8066 .arg2_type = ARG_PTR_TO_FIXED_SIZE_MEM | MEM_RDONLY,
8067 .arg2_size = sizeof(struct tcphdr),
8068 };
8069
BPF_CALL_2(bpf_tcp_raw_check_syncookie_ipv6,struct ipv6hdr *,iph,struct tcphdr *,th)8070 BPF_CALL_2(bpf_tcp_raw_check_syncookie_ipv6, struct ipv6hdr *, iph,
8071 struct tcphdr *, th)
8072 {
8073 #if IS_BUILTIN(CONFIG_IPV6)
8074 if (__cookie_v6_check(iph, th) > 0)
8075 return 0;
8076
8077 return -EACCES;
8078 #else
8079 return -EPROTONOSUPPORT;
8080 #endif
8081 }
8082
8083 static const struct bpf_func_proto bpf_tcp_raw_check_syncookie_ipv6_proto = {
8084 .func = bpf_tcp_raw_check_syncookie_ipv6,
8085 .gpl_only = true, /* __cookie_v6_check is GPL */
8086 .pkt_access = true,
8087 .ret_type = RET_INTEGER,
8088 .arg1_type = ARG_PTR_TO_FIXED_SIZE_MEM | MEM_RDONLY,
8089 .arg1_size = sizeof(struct ipv6hdr),
8090 .arg2_type = ARG_PTR_TO_FIXED_SIZE_MEM | MEM_RDONLY,
8091 .arg2_size = sizeof(struct tcphdr),
8092 };
8093 #endif /* CONFIG_SYN_COOKIES */
8094
8095 #endif /* CONFIG_INET */
8096
bpf_helper_changes_pkt_data(enum bpf_func_id func_id)8097 bool bpf_helper_changes_pkt_data(enum bpf_func_id func_id)
8098 {
8099 switch (func_id) {
8100 case BPF_FUNC_clone_redirect:
8101 case BPF_FUNC_l3_csum_replace:
8102 case BPF_FUNC_l4_csum_replace:
8103 case BPF_FUNC_lwt_push_encap:
8104 case BPF_FUNC_lwt_seg6_action:
8105 case BPF_FUNC_lwt_seg6_adjust_srh:
8106 case BPF_FUNC_lwt_seg6_store_bytes:
8107 case BPF_FUNC_msg_pop_data:
8108 case BPF_FUNC_msg_pull_data:
8109 case BPF_FUNC_msg_push_data:
8110 case BPF_FUNC_skb_adjust_room:
8111 case BPF_FUNC_skb_change_head:
8112 case BPF_FUNC_skb_change_proto:
8113 case BPF_FUNC_skb_change_tail:
8114 case BPF_FUNC_skb_pull_data:
8115 case BPF_FUNC_skb_store_bytes:
8116 case BPF_FUNC_skb_vlan_pop:
8117 case BPF_FUNC_skb_vlan_push:
8118 case BPF_FUNC_store_hdr_opt:
8119 case BPF_FUNC_xdp_adjust_head:
8120 case BPF_FUNC_xdp_adjust_meta:
8121 case BPF_FUNC_xdp_adjust_tail:
8122 /* tail-called program could call any of the above */
8123 case BPF_FUNC_tail_call:
8124 return true;
8125 default:
8126 return false;
8127 }
8128 }
8129
8130 const struct bpf_func_proto bpf_event_output_data_proto __weak;
8131 const struct bpf_func_proto bpf_sk_storage_get_cg_sock_proto __weak;
8132
8133 static const struct bpf_func_proto *
sock_filter_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)8134 sock_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
8135 {
8136 const struct bpf_func_proto *func_proto;
8137
8138 func_proto = cgroup_common_func_proto(func_id, prog);
8139 if (func_proto)
8140 return func_proto;
8141
8142 switch (func_id) {
8143 case BPF_FUNC_get_socket_cookie:
8144 return &bpf_get_socket_cookie_sock_proto;
8145 case BPF_FUNC_get_netns_cookie:
8146 return &bpf_get_netns_cookie_sock_proto;
8147 case BPF_FUNC_perf_event_output:
8148 return &bpf_event_output_data_proto;
8149 case BPF_FUNC_sk_storage_get:
8150 return &bpf_sk_storage_get_cg_sock_proto;
8151 case BPF_FUNC_ktime_get_coarse_ns:
8152 return &bpf_ktime_get_coarse_ns_proto;
8153 case BPF_FUNC_setsockopt:
8154 switch (prog->expected_attach_type) {
8155 case BPF_CGROUP_INET_SOCK_CREATE:
8156 return &bpf_sock_create_setsockopt_proto;
8157 default:
8158 return NULL;
8159 }
8160 case BPF_FUNC_getsockopt:
8161 switch (prog->expected_attach_type) {
8162 case BPF_CGROUP_INET_SOCK_CREATE:
8163 return &bpf_sock_create_getsockopt_proto;
8164 default:
8165 return NULL;
8166 }
8167 default:
8168 return bpf_base_func_proto(func_id, prog);
8169 }
8170 }
8171
8172 static const struct bpf_func_proto *
sock_addr_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)8173 sock_addr_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
8174 {
8175 const struct bpf_func_proto *func_proto;
8176
8177 func_proto = cgroup_common_func_proto(func_id, prog);
8178 if (func_proto)
8179 return func_proto;
8180
8181 switch (func_id) {
8182 case BPF_FUNC_bind:
8183 switch (prog->expected_attach_type) {
8184 case BPF_CGROUP_INET4_CONNECT:
8185 case BPF_CGROUP_INET6_CONNECT:
8186 return &bpf_bind_proto;
8187 default:
8188 return NULL;
8189 }
8190 case BPF_FUNC_get_socket_cookie:
8191 return &bpf_get_socket_cookie_sock_addr_proto;
8192 case BPF_FUNC_get_netns_cookie:
8193 return &bpf_get_netns_cookie_sock_addr_proto;
8194 case BPF_FUNC_perf_event_output:
8195 return &bpf_event_output_data_proto;
8196 #ifdef CONFIG_INET
8197 case BPF_FUNC_sk_lookup_tcp:
8198 return &bpf_sock_addr_sk_lookup_tcp_proto;
8199 case BPF_FUNC_sk_lookup_udp:
8200 return &bpf_sock_addr_sk_lookup_udp_proto;
8201 case BPF_FUNC_sk_release:
8202 return &bpf_sk_release_proto;
8203 case BPF_FUNC_skc_lookup_tcp:
8204 return &bpf_sock_addr_skc_lookup_tcp_proto;
8205 #endif /* CONFIG_INET */
8206 case BPF_FUNC_sk_storage_get:
8207 return &bpf_sk_storage_get_proto;
8208 case BPF_FUNC_sk_storage_delete:
8209 return &bpf_sk_storage_delete_proto;
8210 case BPF_FUNC_setsockopt:
8211 switch (prog->expected_attach_type) {
8212 case BPF_CGROUP_INET4_BIND:
8213 case BPF_CGROUP_INET6_BIND:
8214 case BPF_CGROUP_INET4_CONNECT:
8215 case BPF_CGROUP_INET6_CONNECT:
8216 case BPF_CGROUP_UNIX_CONNECT:
8217 case BPF_CGROUP_UDP4_RECVMSG:
8218 case BPF_CGROUP_UDP6_RECVMSG:
8219 case BPF_CGROUP_UNIX_RECVMSG:
8220 case BPF_CGROUP_UDP4_SENDMSG:
8221 case BPF_CGROUP_UDP6_SENDMSG:
8222 case BPF_CGROUP_UNIX_SENDMSG:
8223 case BPF_CGROUP_INET4_GETPEERNAME:
8224 case BPF_CGROUP_INET6_GETPEERNAME:
8225 case BPF_CGROUP_UNIX_GETPEERNAME:
8226 case BPF_CGROUP_INET4_GETSOCKNAME:
8227 case BPF_CGROUP_INET6_GETSOCKNAME:
8228 case BPF_CGROUP_UNIX_GETSOCKNAME:
8229 return &bpf_sock_addr_setsockopt_proto;
8230 default:
8231 return NULL;
8232 }
8233 case BPF_FUNC_getsockopt:
8234 switch (prog->expected_attach_type) {
8235 case BPF_CGROUP_INET4_BIND:
8236 case BPF_CGROUP_INET6_BIND:
8237 case BPF_CGROUP_INET4_CONNECT:
8238 case BPF_CGROUP_INET6_CONNECT:
8239 case BPF_CGROUP_UNIX_CONNECT:
8240 case BPF_CGROUP_UDP4_RECVMSG:
8241 case BPF_CGROUP_UDP6_RECVMSG:
8242 case BPF_CGROUP_UNIX_RECVMSG:
8243 case BPF_CGROUP_UDP4_SENDMSG:
8244 case BPF_CGROUP_UDP6_SENDMSG:
8245 case BPF_CGROUP_UNIX_SENDMSG:
8246 case BPF_CGROUP_INET4_GETPEERNAME:
8247 case BPF_CGROUP_INET6_GETPEERNAME:
8248 case BPF_CGROUP_UNIX_GETPEERNAME:
8249 case BPF_CGROUP_INET4_GETSOCKNAME:
8250 case BPF_CGROUP_INET6_GETSOCKNAME:
8251 case BPF_CGROUP_UNIX_GETSOCKNAME:
8252 return &bpf_sock_addr_getsockopt_proto;
8253 default:
8254 return NULL;
8255 }
8256 default:
8257 return bpf_sk_base_func_proto(func_id, prog);
8258 }
8259 }
8260
8261 static const struct bpf_func_proto *
sk_filter_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)8262 sk_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
8263 {
8264 switch (func_id) {
8265 case BPF_FUNC_skb_load_bytes:
8266 return &bpf_skb_load_bytes_proto;
8267 case BPF_FUNC_skb_load_bytes_relative:
8268 return &bpf_skb_load_bytes_relative_proto;
8269 case BPF_FUNC_get_socket_cookie:
8270 return &bpf_get_socket_cookie_proto;
8271 case BPF_FUNC_get_netns_cookie:
8272 return &bpf_get_netns_cookie_proto;
8273 case BPF_FUNC_get_socket_uid:
8274 return &bpf_get_socket_uid_proto;
8275 case BPF_FUNC_perf_event_output:
8276 return &bpf_skb_event_output_proto;
8277 default:
8278 return bpf_sk_base_func_proto(func_id, prog);
8279 }
8280 }
8281
8282 const struct bpf_func_proto bpf_sk_storage_get_proto __weak;
8283 const struct bpf_func_proto bpf_sk_storage_delete_proto __weak;
8284
8285 static const struct bpf_func_proto *
cg_skb_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)8286 cg_skb_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
8287 {
8288 const struct bpf_func_proto *func_proto;
8289
8290 func_proto = cgroup_common_func_proto(func_id, prog);
8291 if (func_proto)
8292 return func_proto;
8293
8294 switch (func_id) {
8295 case BPF_FUNC_sk_fullsock:
8296 return &bpf_sk_fullsock_proto;
8297 case BPF_FUNC_sk_storage_get:
8298 return &bpf_sk_storage_get_proto;
8299 case BPF_FUNC_sk_storage_delete:
8300 return &bpf_sk_storage_delete_proto;
8301 case BPF_FUNC_perf_event_output:
8302 return &bpf_skb_event_output_proto;
8303 #ifdef CONFIG_SOCK_CGROUP_DATA
8304 case BPF_FUNC_skb_cgroup_id:
8305 return &bpf_skb_cgroup_id_proto;
8306 case BPF_FUNC_skb_ancestor_cgroup_id:
8307 return &bpf_skb_ancestor_cgroup_id_proto;
8308 case BPF_FUNC_sk_cgroup_id:
8309 return &bpf_sk_cgroup_id_proto;
8310 case BPF_FUNC_sk_ancestor_cgroup_id:
8311 return &bpf_sk_ancestor_cgroup_id_proto;
8312 #endif
8313 #ifdef CONFIG_INET
8314 case BPF_FUNC_sk_lookup_tcp:
8315 return &bpf_sk_lookup_tcp_proto;
8316 case BPF_FUNC_sk_lookup_udp:
8317 return &bpf_sk_lookup_udp_proto;
8318 case BPF_FUNC_sk_release:
8319 return &bpf_sk_release_proto;
8320 case BPF_FUNC_skc_lookup_tcp:
8321 return &bpf_skc_lookup_tcp_proto;
8322 case BPF_FUNC_tcp_sock:
8323 return &bpf_tcp_sock_proto;
8324 case BPF_FUNC_get_listener_sock:
8325 return &bpf_get_listener_sock_proto;
8326 case BPF_FUNC_skb_ecn_set_ce:
8327 return &bpf_skb_ecn_set_ce_proto;
8328 #endif
8329 default:
8330 return sk_filter_func_proto(func_id, prog);
8331 }
8332 }
8333
8334 static const struct bpf_func_proto *
tc_cls_act_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)8335 tc_cls_act_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
8336 {
8337 switch (func_id) {
8338 case BPF_FUNC_skb_store_bytes:
8339 return &bpf_skb_store_bytes_proto;
8340 case BPF_FUNC_skb_load_bytes:
8341 return &bpf_skb_load_bytes_proto;
8342 case BPF_FUNC_skb_load_bytes_relative:
8343 return &bpf_skb_load_bytes_relative_proto;
8344 case BPF_FUNC_skb_pull_data:
8345 return &bpf_skb_pull_data_proto;
8346 case BPF_FUNC_csum_diff:
8347 return &bpf_csum_diff_proto;
8348 case BPF_FUNC_csum_update:
8349 return &bpf_csum_update_proto;
8350 case BPF_FUNC_csum_level:
8351 return &bpf_csum_level_proto;
8352 case BPF_FUNC_l3_csum_replace:
8353 return &bpf_l3_csum_replace_proto;
8354 case BPF_FUNC_l4_csum_replace:
8355 return &bpf_l4_csum_replace_proto;
8356 case BPF_FUNC_clone_redirect:
8357 return &bpf_clone_redirect_proto;
8358 case BPF_FUNC_get_cgroup_classid:
8359 return &bpf_get_cgroup_classid_proto;
8360 case BPF_FUNC_skb_vlan_push:
8361 return &bpf_skb_vlan_push_proto;
8362 case BPF_FUNC_skb_vlan_pop:
8363 return &bpf_skb_vlan_pop_proto;
8364 case BPF_FUNC_skb_change_proto:
8365 return &bpf_skb_change_proto_proto;
8366 case BPF_FUNC_skb_change_type:
8367 return &bpf_skb_change_type_proto;
8368 case BPF_FUNC_skb_adjust_room:
8369 return &bpf_skb_adjust_room_proto;
8370 case BPF_FUNC_skb_change_tail:
8371 return &bpf_skb_change_tail_proto;
8372 case BPF_FUNC_skb_change_head:
8373 return &bpf_skb_change_head_proto;
8374 case BPF_FUNC_skb_get_tunnel_key:
8375 return &bpf_skb_get_tunnel_key_proto;
8376 case BPF_FUNC_skb_set_tunnel_key:
8377 return bpf_get_skb_set_tunnel_proto(func_id);
8378 case BPF_FUNC_skb_get_tunnel_opt:
8379 return &bpf_skb_get_tunnel_opt_proto;
8380 case BPF_FUNC_skb_set_tunnel_opt:
8381 return bpf_get_skb_set_tunnel_proto(func_id);
8382 case BPF_FUNC_redirect:
8383 return &bpf_redirect_proto;
8384 case BPF_FUNC_redirect_neigh:
8385 return &bpf_redirect_neigh_proto;
8386 case BPF_FUNC_redirect_peer:
8387 return &bpf_redirect_peer_proto;
8388 case BPF_FUNC_get_route_realm:
8389 return &bpf_get_route_realm_proto;
8390 case BPF_FUNC_get_hash_recalc:
8391 return &bpf_get_hash_recalc_proto;
8392 case BPF_FUNC_set_hash_invalid:
8393 return &bpf_set_hash_invalid_proto;
8394 case BPF_FUNC_set_hash:
8395 return &bpf_set_hash_proto;
8396 case BPF_FUNC_perf_event_output:
8397 return &bpf_skb_event_output_proto;
8398 case BPF_FUNC_get_smp_processor_id:
8399 return &bpf_get_smp_processor_id_proto;
8400 case BPF_FUNC_skb_under_cgroup:
8401 return &bpf_skb_under_cgroup_proto;
8402 case BPF_FUNC_get_socket_cookie:
8403 return &bpf_get_socket_cookie_proto;
8404 case BPF_FUNC_get_netns_cookie:
8405 return &bpf_get_netns_cookie_proto;
8406 case BPF_FUNC_get_socket_uid:
8407 return &bpf_get_socket_uid_proto;
8408 case BPF_FUNC_fib_lookup:
8409 return &bpf_skb_fib_lookup_proto;
8410 case BPF_FUNC_check_mtu:
8411 return &bpf_skb_check_mtu_proto;
8412 case BPF_FUNC_sk_fullsock:
8413 return &bpf_sk_fullsock_proto;
8414 case BPF_FUNC_sk_storage_get:
8415 return &bpf_sk_storage_get_proto;
8416 case BPF_FUNC_sk_storage_delete:
8417 return &bpf_sk_storage_delete_proto;
8418 #ifdef CONFIG_XFRM
8419 case BPF_FUNC_skb_get_xfrm_state:
8420 return &bpf_skb_get_xfrm_state_proto;
8421 #endif
8422 #ifdef CONFIG_CGROUP_NET_CLASSID
8423 case BPF_FUNC_skb_cgroup_classid:
8424 return &bpf_skb_cgroup_classid_proto;
8425 #endif
8426 #ifdef CONFIG_SOCK_CGROUP_DATA
8427 case BPF_FUNC_skb_cgroup_id:
8428 return &bpf_skb_cgroup_id_proto;
8429 case BPF_FUNC_skb_ancestor_cgroup_id:
8430 return &bpf_skb_ancestor_cgroup_id_proto;
8431 #endif
8432 #ifdef CONFIG_INET
8433 case BPF_FUNC_sk_lookup_tcp:
8434 return &bpf_tc_sk_lookup_tcp_proto;
8435 case BPF_FUNC_sk_lookup_udp:
8436 return &bpf_tc_sk_lookup_udp_proto;
8437 case BPF_FUNC_sk_release:
8438 return &bpf_sk_release_proto;
8439 case BPF_FUNC_tcp_sock:
8440 return &bpf_tcp_sock_proto;
8441 case BPF_FUNC_get_listener_sock:
8442 return &bpf_get_listener_sock_proto;
8443 case BPF_FUNC_skc_lookup_tcp:
8444 return &bpf_tc_skc_lookup_tcp_proto;
8445 case BPF_FUNC_tcp_check_syncookie:
8446 return &bpf_tcp_check_syncookie_proto;
8447 case BPF_FUNC_skb_ecn_set_ce:
8448 return &bpf_skb_ecn_set_ce_proto;
8449 case BPF_FUNC_tcp_gen_syncookie:
8450 return &bpf_tcp_gen_syncookie_proto;
8451 case BPF_FUNC_sk_assign:
8452 return &bpf_sk_assign_proto;
8453 case BPF_FUNC_skb_set_tstamp:
8454 return &bpf_skb_set_tstamp_proto;
8455 #ifdef CONFIG_SYN_COOKIES
8456 case BPF_FUNC_tcp_raw_gen_syncookie_ipv4:
8457 return &bpf_tcp_raw_gen_syncookie_ipv4_proto;
8458 case BPF_FUNC_tcp_raw_gen_syncookie_ipv6:
8459 return &bpf_tcp_raw_gen_syncookie_ipv6_proto;
8460 case BPF_FUNC_tcp_raw_check_syncookie_ipv4:
8461 return &bpf_tcp_raw_check_syncookie_ipv4_proto;
8462 case BPF_FUNC_tcp_raw_check_syncookie_ipv6:
8463 return &bpf_tcp_raw_check_syncookie_ipv6_proto;
8464 #endif
8465 #endif
8466 default:
8467 return bpf_sk_base_func_proto(func_id, prog);
8468 }
8469 }
8470
8471 static const struct bpf_func_proto *
xdp_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)8472 xdp_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
8473 {
8474 switch (func_id) {
8475 case BPF_FUNC_perf_event_output:
8476 return &bpf_xdp_event_output_proto;
8477 case BPF_FUNC_get_smp_processor_id:
8478 return &bpf_get_smp_processor_id_proto;
8479 case BPF_FUNC_csum_diff:
8480 return &bpf_csum_diff_proto;
8481 case BPF_FUNC_xdp_adjust_head:
8482 return &bpf_xdp_adjust_head_proto;
8483 case BPF_FUNC_xdp_adjust_meta:
8484 return &bpf_xdp_adjust_meta_proto;
8485 case BPF_FUNC_redirect:
8486 return &bpf_xdp_redirect_proto;
8487 case BPF_FUNC_redirect_map:
8488 return &bpf_xdp_redirect_map_proto;
8489 case BPF_FUNC_xdp_adjust_tail:
8490 return &bpf_xdp_adjust_tail_proto;
8491 case BPF_FUNC_xdp_get_buff_len:
8492 return &bpf_xdp_get_buff_len_proto;
8493 case BPF_FUNC_xdp_load_bytes:
8494 return &bpf_xdp_load_bytes_proto;
8495 case BPF_FUNC_xdp_store_bytes:
8496 return &bpf_xdp_store_bytes_proto;
8497 case BPF_FUNC_fib_lookup:
8498 return &bpf_xdp_fib_lookup_proto;
8499 case BPF_FUNC_check_mtu:
8500 return &bpf_xdp_check_mtu_proto;
8501 #ifdef CONFIG_INET
8502 case BPF_FUNC_sk_lookup_udp:
8503 return &bpf_xdp_sk_lookup_udp_proto;
8504 case BPF_FUNC_sk_lookup_tcp:
8505 return &bpf_xdp_sk_lookup_tcp_proto;
8506 case BPF_FUNC_sk_release:
8507 return &bpf_sk_release_proto;
8508 case BPF_FUNC_skc_lookup_tcp:
8509 return &bpf_xdp_skc_lookup_tcp_proto;
8510 case BPF_FUNC_tcp_check_syncookie:
8511 return &bpf_tcp_check_syncookie_proto;
8512 case BPF_FUNC_tcp_gen_syncookie:
8513 return &bpf_tcp_gen_syncookie_proto;
8514 #ifdef CONFIG_SYN_COOKIES
8515 case BPF_FUNC_tcp_raw_gen_syncookie_ipv4:
8516 return &bpf_tcp_raw_gen_syncookie_ipv4_proto;
8517 case BPF_FUNC_tcp_raw_gen_syncookie_ipv6:
8518 return &bpf_tcp_raw_gen_syncookie_ipv6_proto;
8519 case BPF_FUNC_tcp_raw_check_syncookie_ipv4:
8520 return &bpf_tcp_raw_check_syncookie_ipv4_proto;
8521 case BPF_FUNC_tcp_raw_check_syncookie_ipv6:
8522 return &bpf_tcp_raw_check_syncookie_ipv6_proto;
8523 #endif
8524 #endif
8525 default:
8526 return bpf_sk_base_func_proto(func_id, prog);
8527 }
8528
8529 #if IS_MODULE(CONFIG_NF_CONNTRACK) && IS_ENABLED(CONFIG_DEBUG_INFO_BTF_MODULES)
8530 /* The nf_conn___init type is used in the NF_CONNTRACK kfuncs. The
8531 * kfuncs are defined in two different modules, and we want to be able
8532 * to use them interchangeably with the same BTF type ID. Because modules
8533 * can't de-duplicate BTF IDs between each other, we need the type to be
8534 * referenced in the vmlinux BTF or the verifier will get confused about
8535 * the different types. So we add this dummy type reference which will
8536 * be included in vmlinux BTF, allowing both modules to refer to the
8537 * same type ID.
8538 */
8539 BTF_TYPE_EMIT(struct nf_conn___init);
8540 #endif
8541 }
8542
8543 const struct bpf_func_proto bpf_sock_map_update_proto __weak;
8544 const struct bpf_func_proto bpf_sock_hash_update_proto __weak;
8545
8546 static const struct bpf_func_proto *
sock_ops_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)8547 sock_ops_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
8548 {
8549 const struct bpf_func_proto *func_proto;
8550
8551 func_proto = cgroup_common_func_proto(func_id, prog);
8552 if (func_proto)
8553 return func_proto;
8554
8555 switch (func_id) {
8556 case BPF_FUNC_setsockopt:
8557 return &bpf_sock_ops_setsockopt_proto;
8558 case BPF_FUNC_getsockopt:
8559 return &bpf_sock_ops_getsockopt_proto;
8560 case BPF_FUNC_sock_ops_cb_flags_set:
8561 return &bpf_sock_ops_cb_flags_set_proto;
8562 case BPF_FUNC_sock_map_update:
8563 return &bpf_sock_map_update_proto;
8564 case BPF_FUNC_sock_hash_update:
8565 return &bpf_sock_hash_update_proto;
8566 case BPF_FUNC_get_socket_cookie:
8567 return &bpf_get_socket_cookie_sock_ops_proto;
8568 case BPF_FUNC_perf_event_output:
8569 return &bpf_event_output_data_proto;
8570 case BPF_FUNC_sk_storage_get:
8571 return &bpf_sk_storage_get_proto;
8572 case BPF_FUNC_sk_storage_delete:
8573 return &bpf_sk_storage_delete_proto;
8574 case BPF_FUNC_get_netns_cookie:
8575 return &bpf_get_netns_cookie_sock_ops_proto;
8576 #ifdef CONFIG_INET
8577 case BPF_FUNC_load_hdr_opt:
8578 return &bpf_sock_ops_load_hdr_opt_proto;
8579 case BPF_FUNC_store_hdr_opt:
8580 return &bpf_sock_ops_store_hdr_opt_proto;
8581 case BPF_FUNC_reserve_hdr_opt:
8582 return &bpf_sock_ops_reserve_hdr_opt_proto;
8583 case BPF_FUNC_tcp_sock:
8584 return &bpf_tcp_sock_proto;
8585 #endif /* CONFIG_INET */
8586 default:
8587 return bpf_sk_base_func_proto(func_id, prog);
8588 }
8589 }
8590
8591 const struct bpf_func_proto bpf_msg_redirect_map_proto __weak;
8592 const struct bpf_func_proto bpf_msg_redirect_hash_proto __weak;
8593
8594 static const struct bpf_func_proto *
sk_msg_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)8595 sk_msg_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
8596 {
8597 switch (func_id) {
8598 case BPF_FUNC_msg_redirect_map:
8599 return &bpf_msg_redirect_map_proto;
8600 case BPF_FUNC_msg_redirect_hash:
8601 return &bpf_msg_redirect_hash_proto;
8602 case BPF_FUNC_msg_apply_bytes:
8603 return &bpf_msg_apply_bytes_proto;
8604 case BPF_FUNC_msg_cork_bytes:
8605 return &bpf_msg_cork_bytes_proto;
8606 case BPF_FUNC_msg_pull_data:
8607 return &bpf_msg_pull_data_proto;
8608 case BPF_FUNC_msg_push_data:
8609 return &bpf_msg_push_data_proto;
8610 case BPF_FUNC_msg_pop_data:
8611 return &bpf_msg_pop_data_proto;
8612 case BPF_FUNC_perf_event_output:
8613 return &bpf_event_output_data_proto;
8614 case BPF_FUNC_sk_storage_get:
8615 return &bpf_sk_storage_get_proto;
8616 case BPF_FUNC_sk_storage_delete:
8617 return &bpf_sk_storage_delete_proto;
8618 case BPF_FUNC_get_netns_cookie:
8619 return &bpf_get_netns_cookie_sk_msg_proto;
8620 default:
8621 return bpf_sk_base_func_proto(func_id, prog);
8622 }
8623 }
8624
8625 const struct bpf_func_proto bpf_sk_redirect_map_proto __weak;
8626 const struct bpf_func_proto bpf_sk_redirect_hash_proto __weak;
8627
8628 static const struct bpf_func_proto *
sk_skb_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)8629 sk_skb_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
8630 {
8631 switch (func_id) {
8632 case BPF_FUNC_skb_store_bytes:
8633 return &bpf_skb_store_bytes_proto;
8634 case BPF_FUNC_skb_load_bytes:
8635 return &bpf_skb_load_bytes_proto;
8636 case BPF_FUNC_skb_pull_data:
8637 return &sk_skb_pull_data_proto;
8638 case BPF_FUNC_skb_change_tail:
8639 return &sk_skb_change_tail_proto;
8640 case BPF_FUNC_skb_change_head:
8641 return &sk_skb_change_head_proto;
8642 case BPF_FUNC_skb_adjust_room:
8643 return &sk_skb_adjust_room_proto;
8644 case BPF_FUNC_get_socket_cookie:
8645 return &bpf_get_socket_cookie_proto;
8646 case BPF_FUNC_get_socket_uid:
8647 return &bpf_get_socket_uid_proto;
8648 case BPF_FUNC_sk_redirect_map:
8649 return &bpf_sk_redirect_map_proto;
8650 case BPF_FUNC_sk_redirect_hash:
8651 return &bpf_sk_redirect_hash_proto;
8652 case BPF_FUNC_perf_event_output:
8653 return &bpf_skb_event_output_proto;
8654 #ifdef CONFIG_INET
8655 case BPF_FUNC_sk_lookup_tcp:
8656 return &bpf_sk_lookup_tcp_proto;
8657 case BPF_FUNC_sk_lookup_udp:
8658 return &bpf_sk_lookup_udp_proto;
8659 case BPF_FUNC_sk_release:
8660 return &bpf_sk_release_proto;
8661 case BPF_FUNC_skc_lookup_tcp:
8662 return &bpf_skc_lookup_tcp_proto;
8663 #endif
8664 default:
8665 return bpf_sk_base_func_proto(func_id, prog);
8666 }
8667 }
8668
8669 static const struct bpf_func_proto *
flow_dissector_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)8670 flow_dissector_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
8671 {
8672 switch (func_id) {
8673 case BPF_FUNC_skb_load_bytes:
8674 return &bpf_flow_dissector_load_bytes_proto;
8675 default:
8676 return bpf_sk_base_func_proto(func_id, prog);
8677 }
8678 }
8679
8680 static const struct bpf_func_proto *
lwt_out_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)8681 lwt_out_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
8682 {
8683 switch (func_id) {
8684 case BPF_FUNC_skb_load_bytes:
8685 return &bpf_skb_load_bytes_proto;
8686 case BPF_FUNC_skb_pull_data:
8687 return &bpf_skb_pull_data_proto;
8688 case BPF_FUNC_csum_diff:
8689 return &bpf_csum_diff_proto;
8690 case BPF_FUNC_get_cgroup_classid:
8691 return &bpf_get_cgroup_classid_proto;
8692 case BPF_FUNC_get_route_realm:
8693 return &bpf_get_route_realm_proto;
8694 case BPF_FUNC_get_hash_recalc:
8695 return &bpf_get_hash_recalc_proto;
8696 case BPF_FUNC_perf_event_output:
8697 return &bpf_skb_event_output_proto;
8698 case BPF_FUNC_get_smp_processor_id:
8699 return &bpf_get_smp_processor_id_proto;
8700 case BPF_FUNC_skb_under_cgroup:
8701 return &bpf_skb_under_cgroup_proto;
8702 default:
8703 return bpf_sk_base_func_proto(func_id, prog);
8704 }
8705 }
8706
8707 static const struct bpf_func_proto *
lwt_in_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)8708 lwt_in_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
8709 {
8710 switch (func_id) {
8711 case BPF_FUNC_lwt_push_encap:
8712 return &bpf_lwt_in_push_encap_proto;
8713 default:
8714 return lwt_out_func_proto(func_id, prog);
8715 }
8716 }
8717
8718 static const struct bpf_func_proto *
lwt_xmit_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)8719 lwt_xmit_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
8720 {
8721 switch (func_id) {
8722 case BPF_FUNC_skb_get_tunnel_key:
8723 return &bpf_skb_get_tunnel_key_proto;
8724 case BPF_FUNC_skb_set_tunnel_key:
8725 return bpf_get_skb_set_tunnel_proto(func_id);
8726 case BPF_FUNC_skb_get_tunnel_opt:
8727 return &bpf_skb_get_tunnel_opt_proto;
8728 case BPF_FUNC_skb_set_tunnel_opt:
8729 return bpf_get_skb_set_tunnel_proto(func_id);
8730 case BPF_FUNC_redirect:
8731 return &bpf_redirect_proto;
8732 case BPF_FUNC_clone_redirect:
8733 return &bpf_clone_redirect_proto;
8734 case BPF_FUNC_skb_change_tail:
8735 return &bpf_skb_change_tail_proto;
8736 case BPF_FUNC_skb_change_head:
8737 return &bpf_skb_change_head_proto;
8738 case BPF_FUNC_skb_store_bytes:
8739 return &bpf_skb_store_bytes_proto;
8740 case BPF_FUNC_csum_update:
8741 return &bpf_csum_update_proto;
8742 case BPF_FUNC_csum_level:
8743 return &bpf_csum_level_proto;
8744 case BPF_FUNC_l3_csum_replace:
8745 return &bpf_l3_csum_replace_proto;
8746 case BPF_FUNC_l4_csum_replace:
8747 return &bpf_l4_csum_replace_proto;
8748 case BPF_FUNC_set_hash_invalid:
8749 return &bpf_set_hash_invalid_proto;
8750 case BPF_FUNC_lwt_push_encap:
8751 return &bpf_lwt_xmit_push_encap_proto;
8752 default:
8753 return lwt_out_func_proto(func_id, prog);
8754 }
8755 }
8756
8757 static const struct bpf_func_proto *
lwt_seg6local_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)8758 lwt_seg6local_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
8759 {
8760 switch (func_id) {
8761 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
8762 case BPF_FUNC_lwt_seg6_store_bytes:
8763 return &bpf_lwt_seg6_store_bytes_proto;
8764 case BPF_FUNC_lwt_seg6_action:
8765 return &bpf_lwt_seg6_action_proto;
8766 case BPF_FUNC_lwt_seg6_adjust_srh:
8767 return &bpf_lwt_seg6_adjust_srh_proto;
8768 #endif
8769 default:
8770 return lwt_out_func_proto(func_id, prog);
8771 }
8772 }
8773
bpf_skb_is_valid_access(int off,int size,enum bpf_access_type type,const struct bpf_prog * prog,struct bpf_insn_access_aux * info)8774 static bool bpf_skb_is_valid_access(int off, int size, enum bpf_access_type type,
8775 const struct bpf_prog *prog,
8776 struct bpf_insn_access_aux *info)
8777 {
8778 const int size_default = sizeof(__u32);
8779
8780 if (off < 0 || off >= sizeof(struct __sk_buff))
8781 return false;
8782
8783 /* The verifier guarantees that size > 0. */
8784 if (off % size != 0)
8785 return false;
8786
8787 switch (off) {
8788 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
8789 if (off + size > offsetofend(struct __sk_buff, cb[4]))
8790 return false;
8791 break;
8792 case bpf_ctx_range(struct __sk_buff, data):
8793 case bpf_ctx_range(struct __sk_buff, data_meta):
8794 case bpf_ctx_range(struct __sk_buff, data_end):
8795 if (info->is_ldsx || size != size_default)
8796 return false;
8797 break;
8798 case bpf_ctx_range_till(struct __sk_buff, remote_ip6[0], remote_ip6[3]):
8799 case bpf_ctx_range_till(struct __sk_buff, local_ip6[0], local_ip6[3]):
8800 case bpf_ctx_range_till(struct __sk_buff, remote_ip4, remote_ip4):
8801 case bpf_ctx_range_till(struct __sk_buff, local_ip4, local_ip4):
8802 if (size != size_default)
8803 return false;
8804 break;
8805 case bpf_ctx_range_ptr(struct __sk_buff, flow_keys):
8806 return false;
8807 case bpf_ctx_range(struct __sk_buff, hwtstamp):
8808 if (type == BPF_WRITE || size != sizeof(__u64))
8809 return false;
8810 break;
8811 case bpf_ctx_range(struct __sk_buff, tstamp):
8812 if (size != sizeof(__u64))
8813 return false;
8814 break;
8815 case bpf_ctx_range_ptr(struct __sk_buff, sk):
8816 if (type == BPF_WRITE || size != sizeof(__u64))
8817 return false;
8818 info->reg_type = PTR_TO_SOCK_COMMON_OR_NULL;
8819 break;
8820 case offsetof(struct __sk_buff, tstamp_type):
8821 return false;
8822 case offsetofend(struct __sk_buff, tstamp_type) ... offsetof(struct __sk_buff, hwtstamp) - 1:
8823 /* Explicitly prohibit access to padding in __sk_buff. */
8824 return false;
8825 default:
8826 /* Only narrow read access allowed for now. */
8827 if (type == BPF_WRITE) {
8828 if (size != size_default)
8829 return false;
8830 } else {
8831 bpf_ctx_record_field_size(info, size_default);
8832 if (!bpf_ctx_narrow_access_ok(off, size, size_default))
8833 return false;
8834 }
8835 }
8836
8837 return true;
8838 }
8839
sk_filter_is_valid_access(int off,int size,enum bpf_access_type type,const struct bpf_prog * prog,struct bpf_insn_access_aux * info)8840 static bool sk_filter_is_valid_access(int off, int size,
8841 enum bpf_access_type type,
8842 const struct bpf_prog *prog,
8843 struct bpf_insn_access_aux *info)
8844 {
8845 switch (off) {
8846 case bpf_ctx_range(struct __sk_buff, tc_classid):
8847 case bpf_ctx_range(struct __sk_buff, data):
8848 case bpf_ctx_range(struct __sk_buff, data_meta):
8849 case bpf_ctx_range(struct __sk_buff, data_end):
8850 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
8851 case bpf_ctx_range(struct __sk_buff, tstamp):
8852 case bpf_ctx_range(struct __sk_buff, wire_len):
8853 case bpf_ctx_range(struct __sk_buff, hwtstamp):
8854 return false;
8855 }
8856
8857 if (type == BPF_WRITE) {
8858 switch (off) {
8859 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
8860 break;
8861 default:
8862 return false;
8863 }
8864 }
8865
8866 return bpf_skb_is_valid_access(off, size, type, prog, info);
8867 }
8868
cg_skb_is_valid_access(int off,int size,enum bpf_access_type type,const struct bpf_prog * prog,struct bpf_insn_access_aux * info)8869 static bool cg_skb_is_valid_access(int off, int size,
8870 enum bpf_access_type type,
8871 const struct bpf_prog *prog,
8872 struct bpf_insn_access_aux *info)
8873 {
8874 switch (off) {
8875 case bpf_ctx_range(struct __sk_buff, tc_classid):
8876 case bpf_ctx_range(struct __sk_buff, data_meta):
8877 case bpf_ctx_range(struct __sk_buff, wire_len):
8878 return false;
8879 case bpf_ctx_range(struct __sk_buff, data):
8880 case bpf_ctx_range(struct __sk_buff, data_end):
8881 if (!bpf_token_capable(prog->aux->token, CAP_BPF))
8882 return false;
8883 break;
8884 }
8885
8886 if (type == BPF_WRITE) {
8887 switch (off) {
8888 case bpf_ctx_range(struct __sk_buff, mark):
8889 case bpf_ctx_range(struct __sk_buff, priority):
8890 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
8891 break;
8892 case bpf_ctx_range(struct __sk_buff, tstamp):
8893 if (!bpf_token_capable(prog->aux->token, CAP_BPF))
8894 return false;
8895 break;
8896 default:
8897 return false;
8898 }
8899 }
8900
8901 switch (off) {
8902 case bpf_ctx_range(struct __sk_buff, data):
8903 info->reg_type = PTR_TO_PACKET;
8904 break;
8905 case bpf_ctx_range(struct __sk_buff, data_end):
8906 info->reg_type = PTR_TO_PACKET_END;
8907 break;
8908 }
8909
8910 return bpf_skb_is_valid_access(off, size, type, prog, info);
8911 }
8912
lwt_is_valid_access(int off,int size,enum bpf_access_type type,const struct bpf_prog * prog,struct bpf_insn_access_aux * info)8913 static bool lwt_is_valid_access(int off, int size,
8914 enum bpf_access_type type,
8915 const struct bpf_prog *prog,
8916 struct bpf_insn_access_aux *info)
8917 {
8918 switch (off) {
8919 case bpf_ctx_range(struct __sk_buff, tc_classid):
8920 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
8921 case bpf_ctx_range(struct __sk_buff, data_meta):
8922 case bpf_ctx_range(struct __sk_buff, tstamp):
8923 case bpf_ctx_range(struct __sk_buff, wire_len):
8924 case bpf_ctx_range(struct __sk_buff, hwtstamp):
8925 return false;
8926 }
8927
8928 if (type == BPF_WRITE) {
8929 switch (off) {
8930 case bpf_ctx_range(struct __sk_buff, mark):
8931 case bpf_ctx_range(struct __sk_buff, priority):
8932 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
8933 break;
8934 default:
8935 return false;
8936 }
8937 }
8938
8939 switch (off) {
8940 case bpf_ctx_range(struct __sk_buff, data):
8941 info->reg_type = PTR_TO_PACKET;
8942 break;
8943 case bpf_ctx_range(struct __sk_buff, data_end):
8944 info->reg_type = PTR_TO_PACKET_END;
8945 break;
8946 }
8947
8948 return bpf_skb_is_valid_access(off, size, type, prog, info);
8949 }
8950
8951 /* Attach type specific accesses */
__sock_filter_check_attach_type(int off,enum bpf_access_type access_type,enum bpf_attach_type attach_type)8952 static bool __sock_filter_check_attach_type(int off,
8953 enum bpf_access_type access_type,
8954 enum bpf_attach_type attach_type)
8955 {
8956 switch (off) {
8957 case offsetof(struct bpf_sock, bound_dev_if):
8958 case offsetof(struct bpf_sock, mark):
8959 case offsetof(struct bpf_sock, priority):
8960 switch (attach_type) {
8961 case BPF_CGROUP_INET_SOCK_CREATE:
8962 case BPF_CGROUP_INET_SOCK_RELEASE:
8963 goto full_access;
8964 default:
8965 return false;
8966 }
8967 case bpf_ctx_range(struct bpf_sock, src_ip4):
8968 switch (attach_type) {
8969 case BPF_CGROUP_INET4_POST_BIND:
8970 goto read_only;
8971 default:
8972 return false;
8973 }
8974 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
8975 switch (attach_type) {
8976 case BPF_CGROUP_INET6_POST_BIND:
8977 goto read_only;
8978 default:
8979 return false;
8980 }
8981 case bpf_ctx_range(struct bpf_sock, src_port):
8982 switch (attach_type) {
8983 case BPF_CGROUP_INET4_POST_BIND:
8984 case BPF_CGROUP_INET6_POST_BIND:
8985 goto read_only;
8986 default:
8987 return false;
8988 }
8989 }
8990 read_only:
8991 return access_type == BPF_READ;
8992 full_access:
8993 return true;
8994 }
8995
bpf_sock_common_is_valid_access(int off,int size,enum bpf_access_type type,struct bpf_insn_access_aux * info)8996 bool bpf_sock_common_is_valid_access(int off, int size,
8997 enum bpf_access_type type,
8998 struct bpf_insn_access_aux *info)
8999 {
9000 switch (off) {
9001 case bpf_ctx_range_till(struct bpf_sock, type, priority):
9002 return false;
9003 default:
9004 return bpf_sock_is_valid_access(off, size, type, info);
9005 }
9006 }
9007
bpf_sock_is_valid_access(int off,int size,enum bpf_access_type type,struct bpf_insn_access_aux * info)9008 bool bpf_sock_is_valid_access(int off, int size, enum bpf_access_type type,
9009 struct bpf_insn_access_aux *info)
9010 {
9011 const int size_default = sizeof(__u32);
9012 int field_size;
9013
9014 if (off < 0 || off >= sizeof(struct bpf_sock))
9015 return false;
9016 if (off % size != 0)
9017 return false;
9018
9019 switch (off) {
9020 case offsetof(struct bpf_sock, state):
9021 case offsetof(struct bpf_sock, family):
9022 case offsetof(struct bpf_sock, type):
9023 case offsetof(struct bpf_sock, protocol):
9024 case offsetof(struct bpf_sock, src_port):
9025 case offsetof(struct bpf_sock, rx_queue_mapping):
9026 case bpf_ctx_range(struct bpf_sock, src_ip4):
9027 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
9028 case bpf_ctx_range(struct bpf_sock, dst_ip4):
9029 case bpf_ctx_range_till(struct bpf_sock, dst_ip6[0], dst_ip6[3]):
9030 bpf_ctx_record_field_size(info, size_default);
9031 return bpf_ctx_narrow_access_ok(off, size, size_default);
9032 case bpf_ctx_range(struct bpf_sock, dst_port):
9033 field_size = size == size_default ?
9034 size_default : sizeof_field(struct bpf_sock, dst_port);
9035 bpf_ctx_record_field_size(info, field_size);
9036 return bpf_ctx_narrow_access_ok(off, size, field_size);
9037 case offsetofend(struct bpf_sock, dst_port) ...
9038 offsetof(struct bpf_sock, dst_ip4) - 1:
9039 return false;
9040 }
9041
9042 return size == size_default;
9043 }
9044
sock_filter_is_valid_access(int off,int size,enum bpf_access_type type,const struct bpf_prog * prog,struct bpf_insn_access_aux * info)9045 static bool sock_filter_is_valid_access(int off, int size,
9046 enum bpf_access_type type,
9047 const struct bpf_prog *prog,
9048 struct bpf_insn_access_aux *info)
9049 {
9050 if (!bpf_sock_is_valid_access(off, size, type, info))
9051 return false;
9052 return __sock_filter_check_attach_type(off, type,
9053 prog->expected_attach_type);
9054 }
9055
bpf_noop_prologue(struct bpf_insn * insn_buf,bool direct_write,const struct bpf_prog * prog)9056 static int bpf_noop_prologue(struct bpf_insn *insn_buf, bool direct_write,
9057 const struct bpf_prog *prog)
9058 {
9059 /* Neither direct read nor direct write requires any preliminary
9060 * action.
9061 */
9062 return 0;
9063 }
9064
bpf_unclone_prologue(struct bpf_insn * insn_buf,bool direct_write,const struct bpf_prog * prog,int drop_verdict)9065 static int bpf_unclone_prologue(struct bpf_insn *insn_buf, bool direct_write,
9066 const struct bpf_prog *prog, int drop_verdict)
9067 {
9068 struct bpf_insn *insn = insn_buf;
9069
9070 if (!direct_write)
9071 return 0;
9072
9073 /* if (!skb->cloned)
9074 * goto start;
9075 *
9076 * (Fast-path, otherwise approximation that we might be
9077 * a clone, do the rest in helper.)
9078 */
9079 *insn++ = BPF_LDX_MEM(BPF_B, BPF_REG_6, BPF_REG_1, CLONED_OFFSET);
9080 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_6, CLONED_MASK);
9081 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_6, 0, 7);
9082
9083 /* ret = bpf_skb_pull_data(skb, 0); */
9084 *insn++ = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1);
9085 *insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_2, BPF_REG_2);
9086 *insn++ = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
9087 BPF_FUNC_skb_pull_data);
9088 /* if (!ret)
9089 * goto restore;
9090 * return TC_ACT_SHOT;
9091 */
9092 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2);
9093 *insn++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_0, drop_verdict);
9094 *insn++ = BPF_EXIT_INSN();
9095
9096 /* restore: */
9097 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_6);
9098 /* start: */
9099 *insn++ = prog->insnsi[0];
9100
9101 return insn - insn_buf;
9102 }
9103
bpf_gen_ld_abs(const struct bpf_insn * orig,struct bpf_insn * insn_buf)9104 static int bpf_gen_ld_abs(const struct bpf_insn *orig,
9105 struct bpf_insn *insn_buf)
9106 {
9107 bool indirect = BPF_MODE(orig->code) == BPF_IND;
9108 struct bpf_insn *insn = insn_buf;
9109
9110 if (!indirect) {
9111 *insn++ = BPF_MOV64_IMM(BPF_REG_2, orig->imm);
9112 } else {
9113 *insn++ = BPF_MOV64_REG(BPF_REG_2, orig->src_reg);
9114 if (orig->imm)
9115 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, orig->imm);
9116 }
9117 /* We're guaranteed here that CTX is in R6. */
9118 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_CTX);
9119
9120 switch (BPF_SIZE(orig->code)) {
9121 case BPF_B:
9122 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_8_no_cache);
9123 break;
9124 case BPF_H:
9125 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_16_no_cache);
9126 break;
9127 case BPF_W:
9128 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_32_no_cache);
9129 break;
9130 }
9131
9132 *insn++ = BPF_JMP_IMM(BPF_JSGE, BPF_REG_0, 0, 2);
9133 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_0, BPF_REG_0);
9134 *insn++ = BPF_EXIT_INSN();
9135
9136 return insn - insn_buf;
9137 }
9138
tc_cls_act_prologue(struct bpf_insn * insn_buf,bool direct_write,const struct bpf_prog * prog)9139 static int tc_cls_act_prologue(struct bpf_insn *insn_buf, bool direct_write,
9140 const struct bpf_prog *prog)
9141 {
9142 return bpf_unclone_prologue(insn_buf, direct_write, prog, TC_ACT_SHOT);
9143 }
9144
tc_cls_act_is_valid_access(int off,int size,enum bpf_access_type type,const struct bpf_prog * prog,struct bpf_insn_access_aux * info)9145 static bool tc_cls_act_is_valid_access(int off, int size,
9146 enum bpf_access_type type,
9147 const struct bpf_prog *prog,
9148 struct bpf_insn_access_aux *info)
9149 {
9150 if (type == BPF_WRITE) {
9151 switch (off) {
9152 case bpf_ctx_range(struct __sk_buff, mark):
9153 case bpf_ctx_range(struct __sk_buff, tc_index):
9154 case bpf_ctx_range(struct __sk_buff, priority):
9155 case bpf_ctx_range(struct __sk_buff, tc_classid):
9156 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
9157 case bpf_ctx_range(struct __sk_buff, tstamp):
9158 case bpf_ctx_range(struct __sk_buff, queue_mapping):
9159 break;
9160 default:
9161 return false;
9162 }
9163 }
9164
9165 switch (off) {
9166 case bpf_ctx_range(struct __sk_buff, data):
9167 info->reg_type = PTR_TO_PACKET;
9168 break;
9169 case bpf_ctx_range(struct __sk_buff, data_meta):
9170 info->reg_type = PTR_TO_PACKET_META;
9171 break;
9172 case bpf_ctx_range(struct __sk_buff, data_end):
9173 info->reg_type = PTR_TO_PACKET_END;
9174 break;
9175 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
9176 return false;
9177 case offsetof(struct __sk_buff, tstamp_type):
9178 /* The convert_ctx_access() on reading and writing
9179 * __sk_buff->tstamp depends on whether the bpf prog
9180 * has used __sk_buff->tstamp_type or not.
9181 * Thus, we need to set prog->tstamp_type_access
9182 * earlier during is_valid_access() here.
9183 */
9184 ((struct bpf_prog *)prog)->tstamp_type_access = 1;
9185 return size == sizeof(__u8);
9186 }
9187
9188 return bpf_skb_is_valid_access(off, size, type, prog, info);
9189 }
9190
9191 DEFINE_MUTEX(nf_conn_btf_access_lock);
9192 EXPORT_SYMBOL_GPL(nf_conn_btf_access_lock);
9193
9194 int (*nfct_btf_struct_access)(struct bpf_verifier_log *log,
9195 const struct bpf_reg_state *reg,
9196 int off, int size);
9197 EXPORT_SYMBOL_GPL(nfct_btf_struct_access);
9198
tc_cls_act_btf_struct_access(struct bpf_verifier_log * log,const struct bpf_reg_state * reg,int off,int size)9199 static int tc_cls_act_btf_struct_access(struct bpf_verifier_log *log,
9200 const struct bpf_reg_state *reg,
9201 int off, int size)
9202 {
9203 int ret = -EACCES;
9204
9205 mutex_lock(&nf_conn_btf_access_lock);
9206 if (nfct_btf_struct_access)
9207 ret = nfct_btf_struct_access(log, reg, off, size);
9208 mutex_unlock(&nf_conn_btf_access_lock);
9209
9210 return ret;
9211 }
9212
__is_valid_xdp_access(int off,int size)9213 static bool __is_valid_xdp_access(int off, int size)
9214 {
9215 if (off < 0 || off >= sizeof(struct xdp_md))
9216 return false;
9217 if (off % size != 0)
9218 return false;
9219 if (size != sizeof(__u32))
9220 return false;
9221
9222 return true;
9223 }
9224
xdp_is_valid_access(int off,int size,enum bpf_access_type type,const struct bpf_prog * prog,struct bpf_insn_access_aux * info)9225 static bool xdp_is_valid_access(int off, int size,
9226 enum bpf_access_type type,
9227 const struct bpf_prog *prog,
9228 struct bpf_insn_access_aux *info)
9229 {
9230 if (prog->expected_attach_type != BPF_XDP_DEVMAP) {
9231 switch (off) {
9232 case offsetof(struct xdp_md, egress_ifindex):
9233 return false;
9234 }
9235 }
9236
9237 if (type == BPF_WRITE) {
9238 if (bpf_prog_is_offloaded(prog->aux)) {
9239 switch (off) {
9240 case offsetof(struct xdp_md, rx_queue_index):
9241 return __is_valid_xdp_access(off, size);
9242 }
9243 }
9244 return false;
9245 } else {
9246 switch (off) {
9247 case offsetof(struct xdp_md, data_meta):
9248 case offsetof(struct xdp_md, data):
9249 case offsetof(struct xdp_md, data_end):
9250 if (info->is_ldsx)
9251 return false;
9252 }
9253 }
9254
9255 switch (off) {
9256 case offsetof(struct xdp_md, data):
9257 info->reg_type = PTR_TO_PACKET;
9258 break;
9259 case offsetof(struct xdp_md, data_meta):
9260 info->reg_type = PTR_TO_PACKET_META;
9261 break;
9262 case offsetof(struct xdp_md, data_end):
9263 info->reg_type = PTR_TO_PACKET_END;
9264 break;
9265 }
9266
9267 return __is_valid_xdp_access(off, size);
9268 }
9269
bpf_warn_invalid_xdp_action(const struct net_device * dev,const struct bpf_prog * prog,u32 act)9270 void bpf_warn_invalid_xdp_action(const struct net_device *dev,
9271 const struct bpf_prog *prog, u32 act)
9272 {
9273 const u32 act_max = XDP_REDIRECT;
9274
9275 pr_warn_once("%s XDP return value %u on prog %s (id %d) dev %s, expect packet loss!\n",
9276 act > act_max ? "Illegal" : "Driver unsupported",
9277 act, prog->aux->name, prog->aux->id, dev ? dev->name : "N/A");
9278 }
9279 EXPORT_SYMBOL_GPL(bpf_warn_invalid_xdp_action);
9280
xdp_btf_struct_access(struct bpf_verifier_log * log,const struct bpf_reg_state * reg,int off,int size)9281 static int xdp_btf_struct_access(struct bpf_verifier_log *log,
9282 const struct bpf_reg_state *reg,
9283 int off, int size)
9284 {
9285 int ret = -EACCES;
9286
9287 mutex_lock(&nf_conn_btf_access_lock);
9288 if (nfct_btf_struct_access)
9289 ret = nfct_btf_struct_access(log, reg, off, size);
9290 mutex_unlock(&nf_conn_btf_access_lock);
9291
9292 return ret;
9293 }
9294
sock_addr_is_valid_access(int off,int size,enum bpf_access_type type,const struct bpf_prog * prog,struct bpf_insn_access_aux * info)9295 static bool sock_addr_is_valid_access(int off, int size,
9296 enum bpf_access_type type,
9297 const struct bpf_prog *prog,
9298 struct bpf_insn_access_aux *info)
9299 {
9300 const int size_default = sizeof(__u32);
9301
9302 if (off < 0 || off >= sizeof(struct bpf_sock_addr))
9303 return false;
9304 if (off % size != 0)
9305 return false;
9306
9307 /* Disallow access to fields not belonging to the attach type's address
9308 * family.
9309 */
9310 switch (off) {
9311 case bpf_ctx_range(struct bpf_sock_addr, user_ip4):
9312 switch (prog->expected_attach_type) {
9313 case BPF_CGROUP_INET4_BIND:
9314 case BPF_CGROUP_INET4_CONNECT:
9315 case BPF_CGROUP_INET4_GETPEERNAME:
9316 case BPF_CGROUP_INET4_GETSOCKNAME:
9317 case BPF_CGROUP_UDP4_SENDMSG:
9318 case BPF_CGROUP_UDP4_RECVMSG:
9319 break;
9320 default:
9321 return false;
9322 }
9323 break;
9324 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
9325 switch (prog->expected_attach_type) {
9326 case BPF_CGROUP_INET6_BIND:
9327 case BPF_CGROUP_INET6_CONNECT:
9328 case BPF_CGROUP_INET6_GETPEERNAME:
9329 case BPF_CGROUP_INET6_GETSOCKNAME:
9330 case BPF_CGROUP_UDP6_SENDMSG:
9331 case BPF_CGROUP_UDP6_RECVMSG:
9332 break;
9333 default:
9334 return false;
9335 }
9336 break;
9337 case bpf_ctx_range(struct bpf_sock_addr, msg_src_ip4):
9338 switch (prog->expected_attach_type) {
9339 case BPF_CGROUP_UDP4_SENDMSG:
9340 break;
9341 default:
9342 return false;
9343 }
9344 break;
9345 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
9346 msg_src_ip6[3]):
9347 switch (prog->expected_attach_type) {
9348 case BPF_CGROUP_UDP6_SENDMSG:
9349 break;
9350 default:
9351 return false;
9352 }
9353 break;
9354 }
9355
9356 switch (off) {
9357 case bpf_ctx_range(struct bpf_sock_addr, user_ip4):
9358 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
9359 case bpf_ctx_range(struct bpf_sock_addr, msg_src_ip4):
9360 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
9361 msg_src_ip6[3]):
9362 case bpf_ctx_range(struct bpf_sock_addr, user_port):
9363 if (type == BPF_READ) {
9364 bpf_ctx_record_field_size(info, size_default);
9365
9366 if (bpf_ctx_wide_access_ok(off, size,
9367 struct bpf_sock_addr,
9368 user_ip6))
9369 return true;
9370
9371 if (bpf_ctx_wide_access_ok(off, size,
9372 struct bpf_sock_addr,
9373 msg_src_ip6))
9374 return true;
9375
9376 if (!bpf_ctx_narrow_access_ok(off, size, size_default))
9377 return false;
9378 } else {
9379 if (bpf_ctx_wide_access_ok(off, size,
9380 struct bpf_sock_addr,
9381 user_ip6))
9382 return true;
9383
9384 if (bpf_ctx_wide_access_ok(off, size,
9385 struct bpf_sock_addr,
9386 msg_src_ip6))
9387 return true;
9388
9389 if (size != size_default)
9390 return false;
9391 }
9392 break;
9393 case bpf_ctx_range_ptr(struct bpf_sock_addr, sk):
9394 if (type != BPF_READ)
9395 return false;
9396 if (size != sizeof(__u64))
9397 return false;
9398 info->reg_type = PTR_TO_SOCKET;
9399 break;
9400 case bpf_ctx_range(struct bpf_sock_addr, user_family):
9401 case bpf_ctx_range(struct bpf_sock_addr, family):
9402 case bpf_ctx_range(struct bpf_sock_addr, type):
9403 case bpf_ctx_range(struct bpf_sock_addr, protocol):
9404 if (type != BPF_READ)
9405 return false;
9406 if (size != size_default)
9407 return false;
9408 break;
9409 default:
9410 return false;
9411 }
9412
9413 return true;
9414 }
9415
sock_ops_is_valid_access(int off,int size,enum bpf_access_type type,const struct bpf_prog * prog,struct bpf_insn_access_aux * info)9416 static bool sock_ops_is_valid_access(int off, int size,
9417 enum bpf_access_type type,
9418 const struct bpf_prog *prog,
9419 struct bpf_insn_access_aux *info)
9420 {
9421 const int size_default = sizeof(__u32);
9422
9423 if (off < 0 || off >= sizeof(struct bpf_sock_ops))
9424 return false;
9425
9426 /* The verifier guarantees that size > 0. */
9427 if (off % size != 0)
9428 return false;
9429
9430 if (type == BPF_WRITE) {
9431 switch (off) {
9432 case offsetof(struct bpf_sock_ops, reply):
9433 case offsetof(struct bpf_sock_ops, sk_txhash):
9434 if (size != size_default)
9435 return false;
9436 break;
9437 default:
9438 return false;
9439 }
9440 } else {
9441 switch (off) {
9442 case bpf_ctx_range_till(struct bpf_sock_ops, bytes_received,
9443 bytes_acked):
9444 if (size != sizeof(__u64))
9445 return false;
9446 break;
9447 case bpf_ctx_range_ptr(struct bpf_sock_ops, sk):
9448 if (size != sizeof(__u64))
9449 return false;
9450 info->reg_type = PTR_TO_SOCKET_OR_NULL;
9451 break;
9452 case bpf_ctx_range_ptr(struct bpf_sock_ops, skb_data):
9453 if (size != sizeof(__u64))
9454 return false;
9455 info->reg_type = PTR_TO_PACKET;
9456 break;
9457 case bpf_ctx_range_ptr(struct bpf_sock_ops, skb_data_end):
9458 if (size != sizeof(__u64))
9459 return false;
9460 info->reg_type = PTR_TO_PACKET_END;
9461 break;
9462 case offsetof(struct bpf_sock_ops, skb_tcp_flags):
9463 bpf_ctx_record_field_size(info, size_default);
9464 return bpf_ctx_narrow_access_ok(off, size,
9465 size_default);
9466 case bpf_ctx_range(struct bpf_sock_ops, skb_hwtstamp):
9467 if (size != sizeof(__u64))
9468 return false;
9469 break;
9470 default:
9471 if (size != size_default)
9472 return false;
9473 break;
9474 }
9475 }
9476
9477 return true;
9478 }
9479
sk_skb_prologue(struct bpf_insn * insn_buf,bool direct_write,const struct bpf_prog * prog)9480 static int sk_skb_prologue(struct bpf_insn *insn_buf, bool direct_write,
9481 const struct bpf_prog *prog)
9482 {
9483 return bpf_unclone_prologue(insn_buf, direct_write, prog, SK_DROP);
9484 }
9485
sk_skb_is_valid_access(int off,int size,enum bpf_access_type type,const struct bpf_prog * prog,struct bpf_insn_access_aux * info)9486 static bool sk_skb_is_valid_access(int off, int size,
9487 enum bpf_access_type type,
9488 const struct bpf_prog *prog,
9489 struct bpf_insn_access_aux *info)
9490 {
9491 switch (off) {
9492 case bpf_ctx_range(struct __sk_buff, tc_classid):
9493 case bpf_ctx_range(struct __sk_buff, data_meta):
9494 case bpf_ctx_range(struct __sk_buff, tstamp):
9495 case bpf_ctx_range(struct __sk_buff, wire_len):
9496 case bpf_ctx_range(struct __sk_buff, hwtstamp):
9497 return false;
9498 }
9499
9500 if (type == BPF_WRITE) {
9501 switch (off) {
9502 case bpf_ctx_range(struct __sk_buff, tc_index):
9503 case bpf_ctx_range(struct __sk_buff, priority):
9504 break;
9505 default:
9506 return false;
9507 }
9508 }
9509
9510 switch (off) {
9511 case bpf_ctx_range(struct __sk_buff, mark):
9512 return false;
9513 case bpf_ctx_range(struct __sk_buff, data):
9514 info->reg_type = PTR_TO_PACKET;
9515 break;
9516 case bpf_ctx_range(struct __sk_buff, data_end):
9517 info->reg_type = PTR_TO_PACKET_END;
9518 break;
9519 }
9520
9521 return bpf_skb_is_valid_access(off, size, type, prog, info);
9522 }
9523
sk_msg_is_valid_access(int off,int size,enum bpf_access_type type,const struct bpf_prog * prog,struct bpf_insn_access_aux * info)9524 static bool sk_msg_is_valid_access(int off, int size,
9525 enum bpf_access_type type,
9526 const struct bpf_prog *prog,
9527 struct bpf_insn_access_aux *info)
9528 {
9529 if (type == BPF_WRITE)
9530 return false;
9531
9532 if (off % size != 0)
9533 return false;
9534
9535 switch (off) {
9536 case bpf_ctx_range_ptr(struct sk_msg_md, data):
9537 info->reg_type = PTR_TO_PACKET;
9538 if (size != sizeof(__u64))
9539 return false;
9540 break;
9541 case bpf_ctx_range_ptr(struct sk_msg_md, data_end):
9542 info->reg_type = PTR_TO_PACKET_END;
9543 if (size != sizeof(__u64))
9544 return false;
9545 break;
9546 case bpf_ctx_range_ptr(struct sk_msg_md, sk):
9547 if (size != sizeof(__u64))
9548 return false;
9549 info->reg_type = PTR_TO_SOCKET;
9550 break;
9551 case bpf_ctx_range(struct sk_msg_md, family):
9552 case bpf_ctx_range(struct sk_msg_md, remote_ip4):
9553 case bpf_ctx_range(struct sk_msg_md, local_ip4):
9554 case bpf_ctx_range_till(struct sk_msg_md, remote_ip6[0], remote_ip6[3]):
9555 case bpf_ctx_range_till(struct sk_msg_md, local_ip6[0], local_ip6[3]):
9556 case bpf_ctx_range(struct sk_msg_md, remote_port):
9557 case bpf_ctx_range(struct sk_msg_md, local_port):
9558 case bpf_ctx_range(struct sk_msg_md, size):
9559 if (size != sizeof(__u32))
9560 return false;
9561 break;
9562 default:
9563 return false;
9564 }
9565 return true;
9566 }
9567
flow_dissector_is_valid_access(int off,int size,enum bpf_access_type type,const struct bpf_prog * prog,struct bpf_insn_access_aux * info)9568 static bool flow_dissector_is_valid_access(int off, int size,
9569 enum bpf_access_type type,
9570 const struct bpf_prog *prog,
9571 struct bpf_insn_access_aux *info)
9572 {
9573 const int size_default = sizeof(__u32);
9574
9575 if (off < 0 || off >= sizeof(struct __sk_buff))
9576 return false;
9577
9578 if (off % size != 0)
9579 return false;
9580
9581 if (type == BPF_WRITE)
9582 return false;
9583
9584 switch (off) {
9585 case bpf_ctx_range(struct __sk_buff, data):
9586 if (info->is_ldsx || size != size_default)
9587 return false;
9588 info->reg_type = PTR_TO_PACKET;
9589 return true;
9590 case bpf_ctx_range(struct __sk_buff, data_end):
9591 if (info->is_ldsx || size != size_default)
9592 return false;
9593 info->reg_type = PTR_TO_PACKET_END;
9594 return true;
9595 case bpf_ctx_range_ptr(struct __sk_buff, flow_keys):
9596 if (size != sizeof(__u64))
9597 return false;
9598 info->reg_type = PTR_TO_FLOW_KEYS;
9599 return true;
9600 default:
9601 return false;
9602 }
9603 }
9604
flow_dissector_convert_ctx_access(enum bpf_access_type type,const struct bpf_insn * si,struct bpf_insn * insn_buf,struct bpf_prog * prog,u32 * target_size)9605 static u32 flow_dissector_convert_ctx_access(enum bpf_access_type type,
9606 const struct bpf_insn *si,
9607 struct bpf_insn *insn_buf,
9608 struct bpf_prog *prog,
9609 u32 *target_size)
9610
9611 {
9612 struct bpf_insn *insn = insn_buf;
9613
9614 switch (si->off) {
9615 case offsetof(struct __sk_buff, data):
9616 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_flow_dissector, data),
9617 si->dst_reg, si->src_reg,
9618 offsetof(struct bpf_flow_dissector, data));
9619 break;
9620
9621 case offsetof(struct __sk_buff, data_end):
9622 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_flow_dissector, data_end),
9623 si->dst_reg, si->src_reg,
9624 offsetof(struct bpf_flow_dissector, data_end));
9625 break;
9626
9627 case offsetof(struct __sk_buff, flow_keys):
9628 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_flow_dissector, flow_keys),
9629 si->dst_reg, si->src_reg,
9630 offsetof(struct bpf_flow_dissector, flow_keys));
9631 break;
9632 }
9633
9634 return insn - insn_buf;
9635 }
9636
bpf_convert_tstamp_type_read(const struct bpf_insn * si,struct bpf_insn * insn)9637 static struct bpf_insn *bpf_convert_tstamp_type_read(const struct bpf_insn *si,
9638 struct bpf_insn *insn)
9639 {
9640 __u8 value_reg = si->dst_reg;
9641 __u8 skb_reg = si->src_reg;
9642 BUILD_BUG_ON(__SKB_CLOCK_MAX != (int)BPF_SKB_CLOCK_TAI);
9643 BUILD_BUG_ON(SKB_CLOCK_REALTIME != (int)BPF_SKB_CLOCK_REALTIME);
9644 BUILD_BUG_ON(SKB_CLOCK_MONOTONIC != (int)BPF_SKB_CLOCK_MONOTONIC);
9645 BUILD_BUG_ON(SKB_CLOCK_TAI != (int)BPF_SKB_CLOCK_TAI);
9646 *insn++ = BPF_LDX_MEM(BPF_B, value_reg, skb_reg, SKB_BF_MONO_TC_OFFSET);
9647 *insn++ = BPF_ALU32_IMM(BPF_AND, value_reg, SKB_TSTAMP_TYPE_MASK);
9648 #ifdef __BIG_ENDIAN_BITFIELD
9649 *insn++ = BPF_ALU32_IMM(BPF_RSH, value_reg, SKB_TSTAMP_TYPE_RSHIFT);
9650 #else
9651 BUILD_BUG_ON(!(SKB_TSTAMP_TYPE_MASK & 0x1));
9652 #endif
9653
9654 return insn;
9655 }
9656
bpf_convert_shinfo_access(__u8 dst_reg,__u8 skb_reg,struct bpf_insn * insn)9657 static struct bpf_insn *bpf_convert_shinfo_access(__u8 dst_reg, __u8 skb_reg,
9658 struct bpf_insn *insn)
9659 {
9660 /* si->dst_reg = skb_shinfo(SKB); */
9661 #ifdef NET_SKBUFF_DATA_USES_OFFSET
9662 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, end),
9663 BPF_REG_AX, skb_reg,
9664 offsetof(struct sk_buff, end));
9665 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, head),
9666 dst_reg, skb_reg,
9667 offsetof(struct sk_buff, head));
9668 *insn++ = BPF_ALU64_REG(BPF_ADD, dst_reg, BPF_REG_AX);
9669 #else
9670 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, end),
9671 dst_reg, skb_reg,
9672 offsetof(struct sk_buff, end));
9673 #endif
9674
9675 return insn;
9676 }
9677
bpf_convert_tstamp_read(const struct bpf_prog * prog,const struct bpf_insn * si,struct bpf_insn * insn)9678 static struct bpf_insn *bpf_convert_tstamp_read(const struct bpf_prog *prog,
9679 const struct bpf_insn *si,
9680 struct bpf_insn *insn)
9681 {
9682 __u8 value_reg = si->dst_reg;
9683 __u8 skb_reg = si->src_reg;
9684
9685 #ifdef CONFIG_NET_XGRESS
9686 /* If the tstamp_type is read,
9687 * the bpf prog is aware the tstamp could have delivery time.
9688 * Thus, read skb->tstamp as is if tstamp_type_access is true.
9689 */
9690 if (!prog->tstamp_type_access) {
9691 /* AX is needed because src_reg and dst_reg could be the same */
9692 __u8 tmp_reg = BPF_REG_AX;
9693
9694 *insn++ = BPF_LDX_MEM(BPF_B, tmp_reg, skb_reg, SKB_BF_MONO_TC_OFFSET);
9695 /* check if ingress mask bits is set */
9696 *insn++ = BPF_JMP32_IMM(BPF_JSET, tmp_reg, TC_AT_INGRESS_MASK, 1);
9697 *insn++ = BPF_JMP_A(4);
9698 *insn++ = BPF_JMP32_IMM(BPF_JSET, tmp_reg, SKB_TSTAMP_TYPE_MASK, 1);
9699 *insn++ = BPF_JMP_A(2);
9700 /* skb->tc_at_ingress && skb->tstamp_type,
9701 * read 0 as the (rcv) timestamp.
9702 */
9703 *insn++ = BPF_MOV64_IMM(value_reg, 0);
9704 *insn++ = BPF_JMP_A(1);
9705 }
9706 #endif
9707
9708 *insn++ = BPF_LDX_MEM(BPF_DW, value_reg, skb_reg,
9709 offsetof(struct sk_buff, tstamp));
9710 return insn;
9711 }
9712
bpf_convert_tstamp_write(const struct bpf_prog * prog,const struct bpf_insn * si,struct bpf_insn * insn)9713 static struct bpf_insn *bpf_convert_tstamp_write(const struct bpf_prog *prog,
9714 const struct bpf_insn *si,
9715 struct bpf_insn *insn)
9716 {
9717 __u8 value_reg = si->src_reg;
9718 __u8 skb_reg = si->dst_reg;
9719
9720 #ifdef CONFIG_NET_XGRESS
9721 /* If the tstamp_type is read,
9722 * the bpf prog is aware the tstamp could have delivery time.
9723 * Thus, write skb->tstamp as is if tstamp_type_access is true.
9724 * Otherwise, writing at ingress will have to clear the
9725 * skb->tstamp_type bit also.
9726 */
9727 if (!prog->tstamp_type_access) {
9728 __u8 tmp_reg = BPF_REG_AX;
9729
9730 *insn++ = BPF_LDX_MEM(BPF_B, tmp_reg, skb_reg, SKB_BF_MONO_TC_OFFSET);
9731 /* Writing __sk_buff->tstamp as ingress, goto <clear> */
9732 *insn++ = BPF_JMP32_IMM(BPF_JSET, tmp_reg, TC_AT_INGRESS_MASK, 1);
9733 /* goto <store> */
9734 *insn++ = BPF_JMP_A(2);
9735 /* <clear>: skb->tstamp_type */
9736 *insn++ = BPF_ALU32_IMM(BPF_AND, tmp_reg, ~SKB_TSTAMP_TYPE_MASK);
9737 *insn++ = BPF_STX_MEM(BPF_B, skb_reg, tmp_reg, SKB_BF_MONO_TC_OFFSET);
9738 }
9739 #endif
9740
9741 /* <store>: skb->tstamp = tstamp */
9742 *insn++ = BPF_RAW_INSN(BPF_CLASS(si->code) | BPF_DW | BPF_MEM,
9743 skb_reg, value_reg, offsetof(struct sk_buff, tstamp), si->imm);
9744 return insn;
9745 }
9746
9747 #define BPF_EMIT_STORE(size, si, off) \
9748 BPF_RAW_INSN(BPF_CLASS((si)->code) | (size) | BPF_MEM, \
9749 (si)->dst_reg, (si)->src_reg, (off), (si)->imm)
9750
bpf_convert_ctx_access(enum bpf_access_type type,const struct bpf_insn * si,struct bpf_insn * insn_buf,struct bpf_prog * prog,u32 * target_size)9751 static u32 bpf_convert_ctx_access(enum bpf_access_type type,
9752 const struct bpf_insn *si,
9753 struct bpf_insn *insn_buf,
9754 struct bpf_prog *prog, u32 *target_size)
9755 {
9756 struct bpf_insn *insn = insn_buf;
9757 int off;
9758
9759 switch (si->off) {
9760 case offsetof(struct __sk_buff, len):
9761 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
9762 bpf_target_off(struct sk_buff, len, 4,
9763 target_size));
9764 break;
9765
9766 case offsetof(struct __sk_buff, protocol):
9767 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
9768 bpf_target_off(struct sk_buff, protocol, 2,
9769 target_size));
9770 break;
9771
9772 case offsetof(struct __sk_buff, vlan_proto):
9773 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
9774 bpf_target_off(struct sk_buff, vlan_proto, 2,
9775 target_size));
9776 break;
9777
9778 case offsetof(struct __sk_buff, priority):
9779 if (type == BPF_WRITE)
9780 *insn++ = BPF_EMIT_STORE(BPF_W, si,
9781 bpf_target_off(struct sk_buff, priority, 4,
9782 target_size));
9783 else
9784 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
9785 bpf_target_off(struct sk_buff, priority, 4,
9786 target_size));
9787 break;
9788
9789 case offsetof(struct __sk_buff, ingress_ifindex):
9790 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
9791 bpf_target_off(struct sk_buff, skb_iif, 4,
9792 target_size));
9793 break;
9794
9795 case offsetof(struct __sk_buff, ifindex):
9796 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
9797 si->dst_reg, si->src_reg,
9798 offsetof(struct sk_buff, dev));
9799 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
9800 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
9801 bpf_target_off(struct net_device, ifindex, 4,
9802 target_size));
9803 break;
9804
9805 case offsetof(struct __sk_buff, hash):
9806 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
9807 bpf_target_off(struct sk_buff, hash, 4,
9808 target_size));
9809 break;
9810
9811 case offsetof(struct __sk_buff, mark):
9812 if (type == BPF_WRITE)
9813 *insn++ = BPF_EMIT_STORE(BPF_W, si,
9814 bpf_target_off(struct sk_buff, mark, 4,
9815 target_size));
9816 else
9817 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
9818 bpf_target_off(struct sk_buff, mark, 4,
9819 target_size));
9820 break;
9821
9822 case offsetof(struct __sk_buff, pkt_type):
9823 *target_size = 1;
9824 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->src_reg,
9825 PKT_TYPE_OFFSET);
9826 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, PKT_TYPE_MAX);
9827 #ifdef __BIG_ENDIAN_BITFIELD
9828 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, 5);
9829 #endif
9830 break;
9831
9832 case offsetof(struct __sk_buff, queue_mapping):
9833 if (type == BPF_WRITE) {
9834 u32 offset = bpf_target_off(struct sk_buff, queue_mapping, 2, target_size);
9835
9836 if (BPF_CLASS(si->code) == BPF_ST && si->imm >= NO_QUEUE_MAPPING) {
9837 *insn++ = BPF_JMP_A(0); /* noop */
9838 break;
9839 }
9840
9841 if (BPF_CLASS(si->code) == BPF_STX)
9842 *insn++ = BPF_JMP_IMM(BPF_JGE, si->src_reg, NO_QUEUE_MAPPING, 1);
9843 *insn++ = BPF_EMIT_STORE(BPF_H, si, offset);
9844 } else {
9845 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
9846 bpf_target_off(struct sk_buff,
9847 queue_mapping,
9848 2, target_size));
9849 }
9850 break;
9851
9852 case offsetof(struct __sk_buff, vlan_present):
9853 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
9854 bpf_target_off(struct sk_buff,
9855 vlan_all, 4, target_size));
9856 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
9857 *insn++ = BPF_ALU32_IMM(BPF_MOV, si->dst_reg, 1);
9858 break;
9859
9860 case offsetof(struct __sk_buff, vlan_tci):
9861 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
9862 bpf_target_off(struct sk_buff, vlan_tci, 2,
9863 target_size));
9864 break;
9865
9866 case offsetof(struct __sk_buff, cb[0]) ...
9867 offsetofend(struct __sk_buff, cb[4]) - 1:
9868 BUILD_BUG_ON(sizeof_field(struct qdisc_skb_cb, data) < 20);
9869 BUILD_BUG_ON((offsetof(struct sk_buff, cb) +
9870 offsetof(struct qdisc_skb_cb, data)) %
9871 sizeof(__u64));
9872
9873 prog->cb_access = 1;
9874 off = si->off;
9875 off -= offsetof(struct __sk_buff, cb[0]);
9876 off += offsetof(struct sk_buff, cb);
9877 off += offsetof(struct qdisc_skb_cb, data);
9878 if (type == BPF_WRITE)
9879 *insn++ = BPF_EMIT_STORE(BPF_SIZE(si->code), si, off);
9880 else
9881 *insn++ = BPF_LDX_MEM(BPF_SIZE(si->code), si->dst_reg,
9882 si->src_reg, off);
9883 break;
9884
9885 case offsetof(struct __sk_buff, tc_classid):
9886 BUILD_BUG_ON(sizeof_field(struct qdisc_skb_cb, tc_classid) != 2);
9887
9888 off = si->off;
9889 off -= offsetof(struct __sk_buff, tc_classid);
9890 off += offsetof(struct sk_buff, cb);
9891 off += offsetof(struct qdisc_skb_cb, tc_classid);
9892 *target_size = 2;
9893 if (type == BPF_WRITE)
9894 *insn++ = BPF_EMIT_STORE(BPF_H, si, off);
9895 else
9896 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg,
9897 si->src_reg, off);
9898 break;
9899
9900 case offsetof(struct __sk_buff, data):
9901 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
9902 si->dst_reg, si->src_reg,
9903 offsetof(struct sk_buff, data));
9904 break;
9905
9906 case offsetof(struct __sk_buff, data_meta):
9907 off = si->off;
9908 off -= offsetof(struct __sk_buff, data_meta);
9909 off += offsetof(struct sk_buff, cb);
9910 off += offsetof(struct bpf_skb_data_end, data_meta);
9911 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
9912 si->src_reg, off);
9913 break;
9914
9915 case offsetof(struct __sk_buff, data_end):
9916 off = si->off;
9917 off -= offsetof(struct __sk_buff, data_end);
9918 off += offsetof(struct sk_buff, cb);
9919 off += offsetof(struct bpf_skb_data_end, data_end);
9920 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
9921 si->src_reg, off);
9922 break;
9923
9924 case offsetof(struct __sk_buff, tc_index):
9925 #ifdef CONFIG_NET_SCHED
9926 if (type == BPF_WRITE)
9927 *insn++ = BPF_EMIT_STORE(BPF_H, si,
9928 bpf_target_off(struct sk_buff, tc_index, 2,
9929 target_size));
9930 else
9931 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
9932 bpf_target_off(struct sk_buff, tc_index, 2,
9933 target_size));
9934 #else
9935 *target_size = 2;
9936 if (type == BPF_WRITE)
9937 *insn++ = BPF_MOV64_REG(si->dst_reg, si->dst_reg);
9938 else
9939 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
9940 #endif
9941 break;
9942
9943 case offsetof(struct __sk_buff, napi_id):
9944 #if defined(CONFIG_NET_RX_BUSY_POLL)
9945 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
9946 bpf_target_off(struct sk_buff, napi_id, 4,
9947 target_size));
9948 *insn++ = BPF_JMP_IMM(BPF_JGE, si->dst_reg, MIN_NAPI_ID, 1);
9949 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
9950 #else
9951 *target_size = 4;
9952 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
9953 #endif
9954 break;
9955 case offsetof(struct __sk_buff, family):
9956 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_family) != 2);
9957
9958 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
9959 si->dst_reg, si->src_reg,
9960 offsetof(struct sk_buff, sk));
9961 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
9962 bpf_target_off(struct sock_common,
9963 skc_family,
9964 2, target_size));
9965 break;
9966 case offsetof(struct __sk_buff, remote_ip4):
9967 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_daddr) != 4);
9968
9969 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
9970 si->dst_reg, si->src_reg,
9971 offsetof(struct sk_buff, sk));
9972 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
9973 bpf_target_off(struct sock_common,
9974 skc_daddr,
9975 4, target_size));
9976 break;
9977 case offsetof(struct __sk_buff, local_ip4):
9978 BUILD_BUG_ON(sizeof_field(struct sock_common,
9979 skc_rcv_saddr) != 4);
9980
9981 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
9982 si->dst_reg, si->src_reg,
9983 offsetof(struct sk_buff, sk));
9984 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
9985 bpf_target_off(struct sock_common,
9986 skc_rcv_saddr,
9987 4, target_size));
9988 break;
9989 case offsetof(struct __sk_buff, remote_ip6[0]) ...
9990 offsetof(struct __sk_buff, remote_ip6[3]):
9991 #if IS_ENABLED(CONFIG_IPV6)
9992 BUILD_BUG_ON(sizeof_field(struct sock_common,
9993 skc_v6_daddr.s6_addr32[0]) != 4);
9994
9995 off = si->off;
9996 off -= offsetof(struct __sk_buff, remote_ip6[0]);
9997
9998 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
9999 si->dst_reg, si->src_reg,
10000 offsetof(struct sk_buff, sk));
10001 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
10002 offsetof(struct sock_common,
10003 skc_v6_daddr.s6_addr32[0]) +
10004 off);
10005 #else
10006 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
10007 #endif
10008 break;
10009 case offsetof(struct __sk_buff, local_ip6[0]) ...
10010 offsetof(struct __sk_buff, local_ip6[3]):
10011 #if IS_ENABLED(CONFIG_IPV6)
10012 BUILD_BUG_ON(sizeof_field(struct sock_common,
10013 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
10014
10015 off = si->off;
10016 off -= offsetof(struct __sk_buff, local_ip6[0]);
10017
10018 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
10019 si->dst_reg, si->src_reg,
10020 offsetof(struct sk_buff, sk));
10021 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
10022 offsetof(struct sock_common,
10023 skc_v6_rcv_saddr.s6_addr32[0]) +
10024 off);
10025 #else
10026 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
10027 #endif
10028 break;
10029
10030 case offsetof(struct __sk_buff, remote_port):
10031 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_dport) != 2);
10032
10033 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
10034 si->dst_reg, si->src_reg,
10035 offsetof(struct sk_buff, sk));
10036 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
10037 bpf_target_off(struct sock_common,
10038 skc_dport,
10039 2, target_size));
10040 #ifndef __BIG_ENDIAN_BITFIELD
10041 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
10042 #endif
10043 break;
10044
10045 case offsetof(struct __sk_buff, local_port):
10046 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_num) != 2);
10047
10048 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
10049 si->dst_reg, si->src_reg,
10050 offsetof(struct sk_buff, sk));
10051 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
10052 bpf_target_off(struct sock_common,
10053 skc_num, 2, target_size));
10054 break;
10055
10056 case offsetof(struct __sk_buff, tstamp):
10057 BUILD_BUG_ON(sizeof_field(struct sk_buff, tstamp) != 8);
10058
10059 if (type == BPF_WRITE)
10060 insn = bpf_convert_tstamp_write(prog, si, insn);
10061 else
10062 insn = bpf_convert_tstamp_read(prog, si, insn);
10063 break;
10064
10065 case offsetof(struct __sk_buff, tstamp_type):
10066 insn = bpf_convert_tstamp_type_read(si, insn);
10067 break;
10068
10069 case offsetof(struct __sk_buff, gso_segs):
10070 insn = bpf_convert_shinfo_access(si->dst_reg, si->src_reg, insn);
10071 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct skb_shared_info, gso_segs),
10072 si->dst_reg, si->dst_reg,
10073 bpf_target_off(struct skb_shared_info,
10074 gso_segs, 2,
10075 target_size));
10076 break;
10077 case offsetof(struct __sk_buff, gso_size):
10078 insn = bpf_convert_shinfo_access(si->dst_reg, si->src_reg, insn);
10079 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct skb_shared_info, gso_size),
10080 si->dst_reg, si->dst_reg,
10081 bpf_target_off(struct skb_shared_info,
10082 gso_size, 2,
10083 target_size));
10084 break;
10085 case offsetof(struct __sk_buff, wire_len):
10086 BUILD_BUG_ON(sizeof_field(struct qdisc_skb_cb, pkt_len) != 4);
10087
10088 off = si->off;
10089 off -= offsetof(struct __sk_buff, wire_len);
10090 off += offsetof(struct sk_buff, cb);
10091 off += offsetof(struct qdisc_skb_cb, pkt_len);
10092 *target_size = 4;
10093 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, off);
10094 break;
10095
10096 case offsetof(struct __sk_buff, sk):
10097 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
10098 si->dst_reg, si->src_reg,
10099 offsetof(struct sk_buff, sk));
10100 break;
10101 case offsetof(struct __sk_buff, hwtstamp):
10102 BUILD_BUG_ON(sizeof_field(struct skb_shared_hwtstamps, hwtstamp) != 8);
10103 BUILD_BUG_ON(offsetof(struct skb_shared_hwtstamps, hwtstamp) != 0);
10104
10105 insn = bpf_convert_shinfo_access(si->dst_reg, si->src_reg, insn);
10106 *insn++ = BPF_LDX_MEM(BPF_DW,
10107 si->dst_reg, si->dst_reg,
10108 bpf_target_off(struct skb_shared_info,
10109 hwtstamps, 8,
10110 target_size));
10111 break;
10112 }
10113
10114 return insn - insn_buf;
10115 }
10116
bpf_sock_convert_ctx_access(enum bpf_access_type type,const struct bpf_insn * si,struct bpf_insn * insn_buf,struct bpf_prog * prog,u32 * target_size)10117 u32 bpf_sock_convert_ctx_access(enum bpf_access_type type,
10118 const struct bpf_insn *si,
10119 struct bpf_insn *insn_buf,
10120 struct bpf_prog *prog, u32 *target_size)
10121 {
10122 struct bpf_insn *insn = insn_buf;
10123 int off;
10124
10125 switch (si->off) {
10126 case offsetof(struct bpf_sock, bound_dev_if):
10127 BUILD_BUG_ON(sizeof_field(struct sock, sk_bound_dev_if) != 4);
10128
10129 if (type == BPF_WRITE)
10130 *insn++ = BPF_EMIT_STORE(BPF_W, si,
10131 offsetof(struct sock, sk_bound_dev_if));
10132 else
10133 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
10134 offsetof(struct sock, sk_bound_dev_if));
10135 break;
10136
10137 case offsetof(struct bpf_sock, mark):
10138 BUILD_BUG_ON(sizeof_field(struct sock, sk_mark) != 4);
10139
10140 if (type == BPF_WRITE)
10141 *insn++ = BPF_EMIT_STORE(BPF_W, si,
10142 offsetof(struct sock, sk_mark));
10143 else
10144 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
10145 offsetof(struct sock, sk_mark));
10146 break;
10147
10148 case offsetof(struct bpf_sock, priority):
10149 BUILD_BUG_ON(sizeof_field(struct sock, sk_priority) != 4);
10150
10151 if (type == BPF_WRITE)
10152 *insn++ = BPF_EMIT_STORE(BPF_W, si,
10153 offsetof(struct sock, sk_priority));
10154 else
10155 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
10156 offsetof(struct sock, sk_priority));
10157 break;
10158
10159 case offsetof(struct bpf_sock, family):
10160 *insn++ = BPF_LDX_MEM(
10161 BPF_FIELD_SIZEOF(struct sock_common, skc_family),
10162 si->dst_reg, si->src_reg,
10163 bpf_target_off(struct sock_common,
10164 skc_family,
10165 sizeof_field(struct sock_common,
10166 skc_family),
10167 target_size));
10168 break;
10169
10170 case offsetof(struct bpf_sock, type):
10171 *insn++ = BPF_LDX_MEM(
10172 BPF_FIELD_SIZEOF(struct sock, sk_type),
10173 si->dst_reg, si->src_reg,
10174 bpf_target_off(struct sock, sk_type,
10175 sizeof_field(struct sock, sk_type),
10176 target_size));
10177 break;
10178
10179 case offsetof(struct bpf_sock, protocol):
10180 *insn++ = BPF_LDX_MEM(
10181 BPF_FIELD_SIZEOF(struct sock, sk_protocol),
10182 si->dst_reg, si->src_reg,
10183 bpf_target_off(struct sock, sk_protocol,
10184 sizeof_field(struct sock, sk_protocol),
10185 target_size));
10186 break;
10187
10188 case offsetof(struct bpf_sock, src_ip4):
10189 *insn++ = BPF_LDX_MEM(
10190 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
10191 bpf_target_off(struct sock_common, skc_rcv_saddr,
10192 sizeof_field(struct sock_common,
10193 skc_rcv_saddr),
10194 target_size));
10195 break;
10196
10197 case offsetof(struct bpf_sock, dst_ip4):
10198 *insn++ = BPF_LDX_MEM(
10199 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
10200 bpf_target_off(struct sock_common, skc_daddr,
10201 sizeof_field(struct sock_common,
10202 skc_daddr),
10203 target_size));
10204 break;
10205
10206 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
10207 #if IS_ENABLED(CONFIG_IPV6)
10208 off = si->off;
10209 off -= offsetof(struct bpf_sock, src_ip6[0]);
10210 *insn++ = BPF_LDX_MEM(
10211 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
10212 bpf_target_off(
10213 struct sock_common,
10214 skc_v6_rcv_saddr.s6_addr32[0],
10215 sizeof_field(struct sock_common,
10216 skc_v6_rcv_saddr.s6_addr32[0]),
10217 target_size) + off);
10218 #else
10219 (void)off;
10220 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
10221 #endif
10222 break;
10223
10224 case bpf_ctx_range_till(struct bpf_sock, dst_ip6[0], dst_ip6[3]):
10225 #if IS_ENABLED(CONFIG_IPV6)
10226 off = si->off;
10227 off -= offsetof(struct bpf_sock, dst_ip6[0]);
10228 *insn++ = BPF_LDX_MEM(
10229 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
10230 bpf_target_off(struct sock_common,
10231 skc_v6_daddr.s6_addr32[0],
10232 sizeof_field(struct sock_common,
10233 skc_v6_daddr.s6_addr32[0]),
10234 target_size) + off);
10235 #else
10236 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
10237 *target_size = 4;
10238 #endif
10239 break;
10240
10241 case offsetof(struct bpf_sock, src_port):
10242 *insn++ = BPF_LDX_MEM(
10243 BPF_FIELD_SIZEOF(struct sock_common, skc_num),
10244 si->dst_reg, si->src_reg,
10245 bpf_target_off(struct sock_common, skc_num,
10246 sizeof_field(struct sock_common,
10247 skc_num),
10248 target_size));
10249 break;
10250
10251 case offsetof(struct bpf_sock, dst_port):
10252 *insn++ = BPF_LDX_MEM(
10253 BPF_FIELD_SIZEOF(struct sock_common, skc_dport),
10254 si->dst_reg, si->src_reg,
10255 bpf_target_off(struct sock_common, skc_dport,
10256 sizeof_field(struct sock_common,
10257 skc_dport),
10258 target_size));
10259 break;
10260
10261 case offsetof(struct bpf_sock, state):
10262 *insn++ = BPF_LDX_MEM(
10263 BPF_FIELD_SIZEOF(struct sock_common, skc_state),
10264 si->dst_reg, si->src_reg,
10265 bpf_target_off(struct sock_common, skc_state,
10266 sizeof_field(struct sock_common,
10267 skc_state),
10268 target_size));
10269 break;
10270 case offsetof(struct bpf_sock, rx_queue_mapping):
10271 #ifdef CONFIG_SOCK_RX_QUEUE_MAPPING
10272 *insn++ = BPF_LDX_MEM(
10273 BPF_FIELD_SIZEOF(struct sock, sk_rx_queue_mapping),
10274 si->dst_reg, si->src_reg,
10275 bpf_target_off(struct sock, sk_rx_queue_mapping,
10276 sizeof_field(struct sock,
10277 sk_rx_queue_mapping),
10278 target_size));
10279 *insn++ = BPF_JMP_IMM(BPF_JNE, si->dst_reg, NO_QUEUE_MAPPING,
10280 1);
10281 *insn++ = BPF_MOV64_IMM(si->dst_reg, -1);
10282 #else
10283 *insn++ = BPF_MOV64_IMM(si->dst_reg, -1);
10284 *target_size = 2;
10285 #endif
10286 break;
10287 }
10288
10289 return insn - insn_buf;
10290 }
10291
tc_cls_act_convert_ctx_access(enum bpf_access_type type,const struct bpf_insn * si,struct bpf_insn * insn_buf,struct bpf_prog * prog,u32 * target_size)10292 static u32 tc_cls_act_convert_ctx_access(enum bpf_access_type type,
10293 const struct bpf_insn *si,
10294 struct bpf_insn *insn_buf,
10295 struct bpf_prog *prog, u32 *target_size)
10296 {
10297 struct bpf_insn *insn = insn_buf;
10298
10299 switch (si->off) {
10300 case offsetof(struct __sk_buff, ifindex):
10301 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
10302 si->dst_reg, si->src_reg,
10303 offsetof(struct sk_buff, dev));
10304 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
10305 bpf_target_off(struct net_device, ifindex, 4,
10306 target_size));
10307 break;
10308 default:
10309 return bpf_convert_ctx_access(type, si, insn_buf, prog,
10310 target_size);
10311 }
10312
10313 return insn - insn_buf;
10314 }
10315
xdp_convert_ctx_access(enum bpf_access_type type,const struct bpf_insn * si,struct bpf_insn * insn_buf,struct bpf_prog * prog,u32 * target_size)10316 static u32 xdp_convert_ctx_access(enum bpf_access_type type,
10317 const struct bpf_insn *si,
10318 struct bpf_insn *insn_buf,
10319 struct bpf_prog *prog, u32 *target_size)
10320 {
10321 struct bpf_insn *insn = insn_buf;
10322
10323 switch (si->off) {
10324 case offsetof(struct xdp_md, data):
10325 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data),
10326 si->dst_reg, si->src_reg,
10327 offsetof(struct xdp_buff, data));
10328 break;
10329 case offsetof(struct xdp_md, data_meta):
10330 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_meta),
10331 si->dst_reg, si->src_reg,
10332 offsetof(struct xdp_buff, data_meta));
10333 break;
10334 case offsetof(struct xdp_md, data_end):
10335 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_end),
10336 si->dst_reg, si->src_reg,
10337 offsetof(struct xdp_buff, data_end));
10338 break;
10339 case offsetof(struct xdp_md, ingress_ifindex):
10340 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, rxq),
10341 si->dst_reg, si->src_reg,
10342 offsetof(struct xdp_buff, rxq));
10343 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_rxq_info, dev),
10344 si->dst_reg, si->dst_reg,
10345 offsetof(struct xdp_rxq_info, dev));
10346 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
10347 offsetof(struct net_device, ifindex));
10348 break;
10349 case offsetof(struct xdp_md, rx_queue_index):
10350 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, rxq),
10351 si->dst_reg, si->src_reg,
10352 offsetof(struct xdp_buff, rxq));
10353 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
10354 offsetof(struct xdp_rxq_info,
10355 queue_index));
10356 break;
10357 case offsetof(struct xdp_md, egress_ifindex):
10358 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, txq),
10359 si->dst_reg, si->src_reg,
10360 offsetof(struct xdp_buff, txq));
10361 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_txq_info, dev),
10362 si->dst_reg, si->dst_reg,
10363 offsetof(struct xdp_txq_info, dev));
10364 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
10365 offsetof(struct net_device, ifindex));
10366 break;
10367 }
10368
10369 return insn - insn_buf;
10370 }
10371
10372 /* SOCK_ADDR_LOAD_NESTED_FIELD() loads Nested Field S.F.NF where S is type of
10373 * context Structure, F is Field in context structure that contains a pointer
10374 * to Nested Structure of type NS that has the field NF.
10375 *
10376 * SIZE encodes the load size (BPF_B, BPF_H, etc). It's up to caller to make
10377 * sure that SIZE is not greater than actual size of S.F.NF.
10378 *
10379 * If offset OFF is provided, the load happens from that offset relative to
10380 * offset of NF.
10381 */
10382 #define SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, SIZE, OFF) \
10383 do { \
10384 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), si->dst_reg, \
10385 si->src_reg, offsetof(S, F)); \
10386 *insn++ = BPF_LDX_MEM( \
10387 SIZE, si->dst_reg, si->dst_reg, \
10388 bpf_target_off(NS, NF, sizeof_field(NS, NF), \
10389 target_size) \
10390 + OFF); \
10391 } while (0)
10392
10393 #define SOCK_ADDR_LOAD_NESTED_FIELD(S, NS, F, NF) \
10394 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, \
10395 BPF_FIELD_SIZEOF(NS, NF), 0)
10396
10397 /* SOCK_ADDR_STORE_NESTED_FIELD_OFF() has semantic similar to
10398 * SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF() but for store operation.
10399 *
10400 * In addition it uses Temporary Field TF (member of struct S) as the 3rd
10401 * "register" since two registers available in convert_ctx_access are not
10402 * enough: we can't override neither SRC, since it contains value to store, nor
10403 * DST since it contains pointer to context that may be used by later
10404 * instructions. But we need a temporary place to save pointer to nested
10405 * structure whose field we want to store to.
10406 */
10407 #define SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, SIZE, OFF, TF) \
10408 do { \
10409 int tmp_reg = BPF_REG_9; \
10410 if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg) \
10411 --tmp_reg; \
10412 if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg) \
10413 --tmp_reg; \
10414 *insn++ = BPF_STX_MEM(BPF_DW, si->dst_reg, tmp_reg, \
10415 offsetof(S, TF)); \
10416 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), tmp_reg, \
10417 si->dst_reg, offsetof(S, F)); \
10418 *insn++ = BPF_RAW_INSN(SIZE | BPF_MEM | BPF_CLASS(si->code), \
10419 tmp_reg, si->src_reg, \
10420 bpf_target_off(NS, NF, sizeof_field(NS, NF), \
10421 target_size) \
10422 + OFF, \
10423 si->imm); \
10424 *insn++ = BPF_LDX_MEM(BPF_DW, tmp_reg, si->dst_reg, \
10425 offsetof(S, TF)); \
10426 } while (0)
10427
10428 #define SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, SIZE, OFF, \
10429 TF) \
10430 do { \
10431 if (type == BPF_WRITE) { \
10432 SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, SIZE, \
10433 OFF, TF); \
10434 } else { \
10435 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF( \
10436 S, NS, F, NF, SIZE, OFF); \
10437 } \
10438 } while (0)
10439
sock_addr_convert_ctx_access(enum bpf_access_type type,const struct bpf_insn * si,struct bpf_insn * insn_buf,struct bpf_prog * prog,u32 * target_size)10440 static u32 sock_addr_convert_ctx_access(enum bpf_access_type type,
10441 const struct bpf_insn *si,
10442 struct bpf_insn *insn_buf,
10443 struct bpf_prog *prog, u32 *target_size)
10444 {
10445 int off, port_size = sizeof_field(struct sockaddr_in6, sin6_port);
10446 struct bpf_insn *insn = insn_buf;
10447
10448 switch (si->off) {
10449 case offsetof(struct bpf_sock_addr, user_family):
10450 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
10451 struct sockaddr, uaddr, sa_family);
10452 break;
10453
10454 case offsetof(struct bpf_sock_addr, user_ip4):
10455 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
10456 struct bpf_sock_addr_kern, struct sockaddr_in, uaddr,
10457 sin_addr, BPF_SIZE(si->code), 0, tmp_reg);
10458 break;
10459
10460 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
10461 off = si->off;
10462 off -= offsetof(struct bpf_sock_addr, user_ip6[0]);
10463 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
10464 struct bpf_sock_addr_kern, struct sockaddr_in6, uaddr,
10465 sin6_addr.s6_addr32[0], BPF_SIZE(si->code), off,
10466 tmp_reg);
10467 break;
10468
10469 case offsetof(struct bpf_sock_addr, user_port):
10470 /* To get port we need to know sa_family first and then treat
10471 * sockaddr as either sockaddr_in or sockaddr_in6.
10472 * Though we can simplify since port field has same offset and
10473 * size in both structures.
10474 * Here we check this invariant and use just one of the
10475 * structures if it's true.
10476 */
10477 BUILD_BUG_ON(offsetof(struct sockaddr_in, sin_port) !=
10478 offsetof(struct sockaddr_in6, sin6_port));
10479 BUILD_BUG_ON(sizeof_field(struct sockaddr_in, sin_port) !=
10480 sizeof_field(struct sockaddr_in6, sin6_port));
10481 /* Account for sin6_port being smaller than user_port. */
10482 port_size = min(port_size, BPF_LDST_BYTES(si));
10483 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
10484 struct bpf_sock_addr_kern, struct sockaddr_in6, uaddr,
10485 sin6_port, bytes_to_bpf_size(port_size), 0, tmp_reg);
10486 break;
10487
10488 case offsetof(struct bpf_sock_addr, family):
10489 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
10490 struct sock, sk, sk_family);
10491 break;
10492
10493 case offsetof(struct bpf_sock_addr, type):
10494 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
10495 struct sock, sk, sk_type);
10496 break;
10497
10498 case offsetof(struct bpf_sock_addr, protocol):
10499 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
10500 struct sock, sk, sk_protocol);
10501 break;
10502
10503 case offsetof(struct bpf_sock_addr, msg_src_ip4):
10504 /* Treat t_ctx as struct in_addr for msg_src_ip4. */
10505 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
10506 struct bpf_sock_addr_kern, struct in_addr, t_ctx,
10507 s_addr, BPF_SIZE(si->code), 0, tmp_reg);
10508 break;
10509
10510 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
10511 msg_src_ip6[3]):
10512 off = si->off;
10513 off -= offsetof(struct bpf_sock_addr, msg_src_ip6[0]);
10514 /* Treat t_ctx as struct in6_addr for msg_src_ip6. */
10515 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
10516 struct bpf_sock_addr_kern, struct in6_addr, t_ctx,
10517 s6_addr32[0], BPF_SIZE(si->code), off, tmp_reg);
10518 break;
10519 case offsetof(struct bpf_sock_addr, sk):
10520 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_addr_kern, sk),
10521 si->dst_reg, si->src_reg,
10522 offsetof(struct bpf_sock_addr_kern, sk));
10523 break;
10524 }
10525
10526 return insn - insn_buf;
10527 }
10528
sock_ops_convert_ctx_access(enum bpf_access_type type,const struct bpf_insn * si,struct bpf_insn * insn_buf,struct bpf_prog * prog,u32 * target_size)10529 static u32 sock_ops_convert_ctx_access(enum bpf_access_type type,
10530 const struct bpf_insn *si,
10531 struct bpf_insn *insn_buf,
10532 struct bpf_prog *prog,
10533 u32 *target_size)
10534 {
10535 struct bpf_insn *insn = insn_buf;
10536 int off;
10537
10538 /* Helper macro for adding read access to tcp_sock or sock fields. */
10539 #define SOCK_OPS_GET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ) \
10540 do { \
10541 int fullsock_reg = si->dst_reg, reg = BPF_REG_9, jmp = 2; \
10542 BUILD_BUG_ON(sizeof_field(OBJ, OBJ_FIELD) > \
10543 sizeof_field(struct bpf_sock_ops, BPF_FIELD)); \
10544 if (si->dst_reg == reg || si->src_reg == reg) \
10545 reg--; \
10546 if (si->dst_reg == reg || si->src_reg == reg) \
10547 reg--; \
10548 if (si->dst_reg == si->src_reg) { \
10549 *insn++ = BPF_STX_MEM(BPF_DW, si->src_reg, reg, \
10550 offsetof(struct bpf_sock_ops_kern, \
10551 temp)); \
10552 fullsock_reg = reg; \
10553 jmp += 2; \
10554 } \
10555 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
10556 struct bpf_sock_ops_kern, \
10557 is_locked_tcp_sock), \
10558 fullsock_reg, si->src_reg, \
10559 offsetof(struct bpf_sock_ops_kern, \
10560 is_locked_tcp_sock)); \
10561 *insn++ = BPF_JMP_IMM(BPF_JEQ, fullsock_reg, 0, jmp); \
10562 if (si->dst_reg == si->src_reg) \
10563 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->src_reg, \
10564 offsetof(struct bpf_sock_ops_kern, \
10565 temp)); \
10566 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
10567 struct bpf_sock_ops_kern, sk),\
10568 si->dst_reg, si->src_reg, \
10569 offsetof(struct bpf_sock_ops_kern, sk));\
10570 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(OBJ, \
10571 OBJ_FIELD), \
10572 si->dst_reg, si->dst_reg, \
10573 offsetof(OBJ, OBJ_FIELD)); \
10574 if (si->dst_reg == si->src_reg) { \
10575 *insn++ = BPF_JMP_A(1); \
10576 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->src_reg, \
10577 offsetof(struct bpf_sock_ops_kern, \
10578 temp)); \
10579 } \
10580 } while (0)
10581
10582 #define SOCK_OPS_GET_SK() \
10583 do { \
10584 int fullsock_reg = si->dst_reg, reg = BPF_REG_9, jmp = 1; \
10585 if (si->dst_reg == reg || si->src_reg == reg) \
10586 reg--; \
10587 if (si->dst_reg == reg || si->src_reg == reg) \
10588 reg--; \
10589 if (si->dst_reg == si->src_reg) { \
10590 *insn++ = BPF_STX_MEM(BPF_DW, si->src_reg, reg, \
10591 offsetof(struct bpf_sock_ops_kern, \
10592 temp)); \
10593 fullsock_reg = reg; \
10594 jmp += 2; \
10595 } \
10596 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
10597 struct bpf_sock_ops_kern, \
10598 is_fullsock), \
10599 fullsock_reg, si->src_reg, \
10600 offsetof(struct bpf_sock_ops_kern, \
10601 is_fullsock)); \
10602 *insn++ = BPF_JMP_IMM(BPF_JEQ, fullsock_reg, 0, jmp); \
10603 if (si->dst_reg == si->src_reg) \
10604 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->src_reg, \
10605 offsetof(struct bpf_sock_ops_kern, \
10606 temp)); \
10607 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
10608 struct bpf_sock_ops_kern, sk),\
10609 si->dst_reg, si->src_reg, \
10610 offsetof(struct bpf_sock_ops_kern, sk));\
10611 if (si->dst_reg == si->src_reg) { \
10612 *insn++ = BPF_JMP_A(1); \
10613 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->src_reg, \
10614 offsetof(struct bpf_sock_ops_kern, \
10615 temp)); \
10616 } \
10617 } while (0)
10618
10619 #define SOCK_OPS_GET_TCP_SOCK_FIELD(FIELD) \
10620 SOCK_OPS_GET_FIELD(FIELD, FIELD, struct tcp_sock)
10621
10622 /* Helper macro for adding write access to tcp_sock or sock fields.
10623 * The macro is called with two registers, dst_reg which contains a pointer
10624 * to ctx (context) and src_reg which contains the value that should be
10625 * stored. However, we need an additional register since we cannot overwrite
10626 * dst_reg because it may be used later in the program.
10627 * Instead we "borrow" one of the other register. We first save its value
10628 * into a new (temp) field in bpf_sock_ops_kern, use it, and then restore
10629 * it at the end of the macro.
10630 */
10631 #define SOCK_OPS_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ) \
10632 do { \
10633 int reg = BPF_REG_9; \
10634 BUILD_BUG_ON(sizeof_field(OBJ, OBJ_FIELD) > \
10635 sizeof_field(struct bpf_sock_ops, BPF_FIELD)); \
10636 if (si->dst_reg == reg || si->src_reg == reg) \
10637 reg--; \
10638 if (si->dst_reg == reg || si->src_reg == reg) \
10639 reg--; \
10640 *insn++ = BPF_STX_MEM(BPF_DW, si->dst_reg, reg, \
10641 offsetof(struct bpf_sock_ops_kern, \
10642 temp)); \
10643 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
10644 struct bpf_sock_ops_kern, \
10645 is_locked_tcp_sock), \
10646 reg, si->dst_reg, \
10647 offsetof(struct bpf_sock_ops_kern, \
10648 is_locked_tcp_sock)); \
10649 *insn++ = BPF_JMP_IMM(BPF_JEQ, reg, 0, 2); \
10650 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
10651 struct bpf_sock_ops_kern, sk),\
10652 reg, si->dst_reg, \
10653 offsetof(struct bpf_sock_ops_kern, sk));\
10654 *insn++ = BPF_RAW_INSN(BPF_FIELD_SIZEOF(OBJ, OBJ_FIELD) | \
10655 BPF_MEM | BPF_CLASS(si->code), \
10656 reg, si->src_reg, \
10657 offsetof(OBJ, OBJ_FIELD), \
10658 si->imm); \
10659 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->dst_reg, \
10660 offsetof(struct bpf_sock_ops_kern, \
10661 temp)); \
10662 } while (0)
10663
10664 #define SOCK_OPS_GET_OR_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ, TYPE) \
10665 do { \
10666 if (TYPE == BPF_WRITE) \
10667 SOCK_OPS_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ); \
10668 else \
10669 SOCK_OPS_GET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ); \
10670 } while (0)
10671
10672 switch (si->off) {
10673 case offsetof(struct bpf_sock_ops, op):
10674 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_ops_kern,
10675 op),
10676 si->dst_reg, si->src_reg,
10677 offsetof(struct bpf_sock_ops_kern, op));
10678 break;
10679
10680 case offsetof(struct bpf_sock_ops, replylong[0]) ...
10681 offsetof(struct bpf_sock_ops, replylong[3]):
10682 BUILD_BUG_ON(sizeof_field(struct bpf_sock_ops, reply) !=
10683 sizeof_field(struct bpf_sock_ops_kern, reply));
10684 BUILD_BUG_ON(sizeof_field(struct bpf_sock_ops, replylong) !=
10685 sizeof_field(struct bpf_sock_ops_kern, replylong));
10686 off = si->off;
10687 off -= offsetof(struct bpf_sock_ops, replylong[0]);
10688 off += offsetof(struct bpf_sock_ops_kern, replylong[0]);
10689 if (type == BPF_WRITE)
10690 *insn++ = BPF_EMIT_STORE(BPF_W, si, off);
10691 else
10692 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
10693 off);
10694 break;
10695
10696 case offsetof(struct bpf_sock_ops, family):
10697 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_family) != 2);
10698
10699 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10700 struct bpf_sock_ops_kern, sk),
10701 si->dst_reg, si->src_reg,
10702 offsetof(struct bpf_sock_ops_kern, sk));
10703 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
10704 offsetof(struct sock_common, skc_family));
10705 break;
10706
10707 case offsetof(struct bpf_sock_ops, remote_ip4):
10708 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_daddr) != 4);
10709
10710 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10711 struct bpf_sock_ops_kern, sk),
10712 si->dst_reg, si->src_reg,
10713 offsetof(struct bpf_sock_ops_kern, sk));
10714 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
10715 offsetof(struct sock_common, skc_daddr));
10716 break;
10717
10718 case offsetof(struct bpf_sock_ops, local_ip4):
10719 BUILD_BUG_ON(sizeof_field(struct sock_common,
10720 skc_rcv_saddr) != 4);
10721
10722 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10723 struct bpf_sock_ops_kern, sk),
10724 si->dst_reg, si->src_reg,
10725 offsetof(struct bpf_sock_ops_kern, sk));
10726 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
10727 offsetof(struct sock_common,
10728 skc_rcv_saddr));
10729 break;
10730
10731 case offsetof(struct bpf_sock_ops, remote_ip6[0]) ...
10732 offsetof(struct bpf_sock_ops, remote_ip6[3]):
10733 #if IS_ENABLED(CONFIG_IPV6)
10734 BUILD_BUG_ON(sizeof_field(struct sock_common,
10735 skc_v6_daddr.s6_addr32[0]) != 4);
10736
10737 off = si->off;
10738 off -= offsetof(struct bpf_sock_ops, remote_ip6[0]);
10739 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10740 struct bpf_sock_ops_kern, sk),
10741 si->dst_reg, si->src_reg,
10742 offsetof(struct bpf_sock_ops_kern, sk));
10743 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
10744 offsetof(struct sock_common,
10745 skc_v6_daddr.s6_addr32[0]) +
10746 off);
10747 #else
10748 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
10749 #endif
10750 break;
10751
10752 case offsetof(struct bpf_sock_ops, local_ip6[0]) ...
10753 offsetof(struct bpf_sock_ops, local_ip6[3]):
10754 #if IS_ENABLED(CONFIG_IPV6)
10755 BUILD_BUG_ON(sizeof_field(struct sock_common,
10756 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
10757
10758 off = si->off;
10759 off -= offsetof(struct bpf_sock_ops, local_ip6[0]);
10760 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10761 struct bpf_sock_ops_kern, sk),
10762 si->dst_reg, si->src_reg,
10763 offsetof(struct bpf_sock_ops_kern, sk));
10764 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
10765 offsetof(struct sock_common,
10766 skc_v6_rcv_saddr.s6_addr32[0]) +
10767 off);
10768 #else
10769 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
10770 #endif
10771 break;
10772
10773 case offsetof(struct bpf_sock_ops, remote_port):
10774 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_dport) != 2);
10775
10776 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10777 struct bpf_sock_ops_kern, sk),
10778 si->dst_reg, si->src_reg,
10779 offsetof(struct bpf_sock_ops_kern, sk));
10780 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
10781 offsetof(struct sock_common, skc_dport));
10782 #ifndef __BIG_ENDIAN_BITFIELD
10783 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
10784 #endif
10785 break;
10786
10787 case offsetof(struct bpf_sock_ops, local_port):
10788 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_num) != 2);
10789
10790 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10791 struct bpf_sock_ops_kern, sk),
10792 si->dst_reg, si->src_reg,
10793 offsetof(struct bpf_sock_ops_kern, sk));
10794 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
10795 offsetof(struct sock_common, skc_num));
10796 break;
10797
10798 case offsetof(struct bpf_sock_ops, is_fullsock):
10799 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10800 struct bpf_sock_ops_kern,
10801 is_fullsock),
10802 si->dst_reg, si->src_reg,
10803 offsetof(struct bpf_sock_ops_kern,
10804 is_fullsock));
10805 break;
10806
10807 case offsetof(struct bpf_sock_ops, state):
10808 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_state) != 1);
10809
10810 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10811 struct bpf_sock_ops_kern, sk),
10812 si->dst_reg, si->src_reg,
10813 offsetof(struct bpf_sock_ops_kern, sk));
10814 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->dst_reg,
10815 offsetof(struct sock_common, skc_state));
10816 break;
10817
10818 case offsetof(struct bpf_sock_ops, rtt_min):
10819 BUILD_BUG_ON(sizeof_field(struct tcp_sock, rtt_min) !=
10820 sizeof(struct minmax));
10821 BUILD_BUG_ON(sizeof(struct minmax) <
10822 sizeof(struct minmax_sample));
10823
10824 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10825 struct bpf_sock_ops_kern, sk),
10826 si->dst_reg, si->src_reg,
10827 offsetof(struct bpf_sock_ops_kern, sk));
10828 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
10829 offsetof(struct tcp_sock, rtt_min) +
10830 sizeof_field(struct minmax_sample, t));
10831 break;
10832
10833 case offsetof(struct bpf_sock_ops, bpf_sock_ops_cb_flags):
10834 SOCK_OPS_GET_FIELD(bpf_sock_ops_cb_flags, bpf_sock_ops_cb_flags,
10835 struct tcp_sock);
10836 break;
10837
10838 case offsetof(struct bpf_sock_ops, sk_txhash):
10839 SOCK_OPS_GET_OR_SET_FIELD(sk_txhash, sk_txhash,
10840 struct sock, type);
10841 break;
10842 case offsetof(struct bpf_sock_ops, snd_cwnd):
10843 SOCK_OPS_GET_TCP_SOCK_FIELD(snd_cwnd);
10844 break;
10845 case offsetof(struct bpf_sock_ops, srtt_us):
10846 SOCK_OPS_GET_TCP_SOCK_FIELD(srtt_us);
10847 break;
10848 case offsetof(struct bpf_sock_ops, snd_ssthresh):
10849 SOCK_OPS_GET_TCP_SOCK_FIELD(snd_ssthresh);
10850 break;
10851 case offsetof(struct bpf_sock_ops, rcv_nxt):
10852 SOCK_OPS_GET_TCP_SOCK_FIELD(rcv_nxt);
10853 break;
10854 case offsetof(struct bpf_sock_ops, snd_nxt):
10855 SOCK_OPS_GET_TCP_SOCK_FIELD(snd_nxt);
10856 break;
10857 case offsetof(struct bpf_sock_ops, snd_una):
10858 SOCK_OPS_GET_TCP_SOCK_FIELD(snd_una);
10859 break;
10860 case offsetof(struct bpf_sock_ops, mss_cache):
10861 SOCK_OPS_GET_TCP_SOCK_FIELD(mss_cache);
10862 break;
10863 case offsetof(struct bpf_sock_ops, ecn_flags):
10864 SOCK_OPS_GET_TCP_SOCK_FIELD(ecn_flags);
10865 break;
10866 case offsetof(struct bpf_sock_ops, rate_delivered):
10867 SOCK_OPS_GET_TCP_SOCK_FIELD(rate_delivered);
10868 break;
10869 case offsetof(struct bpf_sock_ops, rate_interval_us):
10870 SOCK_OPS_GET_TCP_SOCK_FIELD(rate_interval_us);
10871 break;
10872 case offsetof(struct bpf_sock_ops, packets_out):
10873 SOCK_OPS_GET_TCP_SOCK_FIELD(packets_out);
10874 break;
10875 case offsetof(struct bpf_sock_ops, retrans_out):
10876 SOCK_OPS_GET_TCP_SOCK_FIELD(retrans_out);
10877 break;
10878 case offsetof(struct bpf_sock_ops, total_retrans):
10879 SOCK_OPS_GET_TCP_SOCK_FIELD(total_retrans);
10880 break;
10881 case offsetof(struct bpf_sock_ops, segs_in):
10882 SOCK_OPS_GET_TCP_SOCK_FIELD(segs_in);
10883 break;
10884 case offsetof(struct bpf_sock_ops, data_segs_in):
10885 SOCK_OPS_GET_TCP_SOCK_FIELD(data_segs_in);
10886 break;
10887 case offsetof(struct bpf_sock_ops, segs_out):
10888 SOCK_OPS_GET_TCP_SOCK_FIELD(segs_out);
10889 break;
10890 case offsetof(struct bpf_sock_ops, data_segs_out):
10891 SOCK_OPS_GET_TCP_SOCK_FIELD(data_segs_out);
10892 break;
10893 case offsetof(struct bpf_sock_ops, lost_out):
10894 SOCK_OPS_GET_TCP_SOCK_FIELD(lost_out);
10895 break;
10896 case offsetof(struct bpf_sock_ops, sacked_out):
10897 SOCK_OPS_GET_TCP_SOCK_FIELD(sacked_out);
10898 break;
10899 case offsetof(struct bpf_sock_ops, bytes_received):
10900 SOCK_OPS_GET_TCP_SOCK_FIELD(bytes_received);
10901 break;
10902 case offsetof(struct bpf_sock_ops, bytes_acked):
10903 SOCK_OPS_GET_TCP_SOCK_FIELD(bytes_acked);
10904 break;
10905 case offsetof(struct bpf_sock_ops, sk):
10906 SOCK_OPS_GET_SK();
10907 break;
10908 case offsetof(struct bpf_sock_ops, skb_data_end):
10909 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_ops_kern,
10910 skb_data_end),
10911 si->dst_reg, si->src_reg,
10912 offsetof(struct bpf_sock_ops_kern,
10913 skb_data_end));
10914 break;
10915 case offsetof(struct bpf_sock_ops, skb_data):
10916 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_ops_kern,
10917 skb),
10918 si->dst_reg, si->src_reg,
10919 offsetof(struct bpf_sock_ops_kern,
10920 skb));
10921 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
10922 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
10923 si->dst_reg, si->dst_reg,
10924 offsetof(struct sk_buff, data));
10925 break;
10926 case offsetof(struct bpf_sock_ops, skb_len):
10927 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_ops_kern,
10928 skb),
10929 si->dst_reg, si->src_reg,
10930 offsetof(struct bpf_sock_ops_kern,
10931 skb));
10932 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
10933 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, len),
10934 si->dst_reg, si->dst_reg,
10935 offsetof(struct sk_buff, len));
10936 break;
10937 case offsetof(struct bpf_sock_ops, skb_tcp_flags):
10938 off = offsetof(struct sk_buff, cb);
10939 off += offsetof(struct tcp_skb_cb, tcp_flags);
10940 *target_size = sizeof_field(struct tcp_skb_cb, tcp_flags);
10941 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_ops_kern,
10942 skb),
10943 si->dst_reg, si->src_reg,
10944 offsetof(struct bpf_sock_ops_kern,
10945 skb));
10946 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
10947 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct tcp_skb_cb,
10948 tcp_flags),
10949 si->dst_reg, si->dst_reg, off);
10950 break;
10951 case offsetof(struct bpf_sock_ops, skb_hwtstamp): {
10952 struct bpf_insn *jmp_on_null_skb;
10953
10954 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_ops_kern,
10955 skb),
10956 si->dst_reg, si->src_reg,
10957 offsetof(struct bpf_sock_ops_kern,
10958 skb));
10959 /* Reserve one insn to test skb == NULL */
10960 jmp_on_null_skb = insn++;
10961 insn = bpf_convert_shinfo_access(si->dst_reg, si->dst_reg, insn);
10962 *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
10963 bpf_target_off(struct skb_shared_info,
10964 hwtstamps, 8,
10965 target_size));
10966 *jmp_on_null_skb = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0,
10967 insn - jmp_on_null_skb - 1);
10968 break;
10969 }
10970 }
10971 return insn - insn_buf;
10972 }
10973
10974 /* data_end = skb->data + skb_headlen() */
bpf_convert_data_end_access(const struct bpf_insn * si,struct bpf_insn * insn)10975 static struct bpf_insn *bpf_convert_data_end_access(const struct bpf_insn *si,
10976 struct bpf_insn *insn)
10977 {
10978 int reg;
10979 int temp_reg_off = offsetof(struct sk_buff, cb) +
10980 offsetof(struct sk_skb_cb, temp_reg);
10981
10982 if (si->src_reg == si->dst_reg) {
10983 /* We need an extra register, choose and save a register. */
10984 reg = BPF_REG_9;
10985 if (si->src_reg == reg || si->dst_reg == reg)
10986 reg--;
10987 if (si->src_reg == reg || si->dst_reg == reg)
10988 reg--;
10989 *insn++ = BPF_STX_MEM(BPF_DW, si->src_reg, reg, temp_reg_off);
10990 } else {
10991 reg = si->dst_reg;
10992 }
10993
10994 /* reg = skb->data */
10995 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
10996 reg, si->src_reg,
10997 offsetof(struct sk_buff, data));
10998 /* AX = skb->len */
10999 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, len),
11000 BPF_REG_AX, si->src_reg,
11001 offsetof(struct sk_buff, len));
11002 /* reg = skb->data + skb->len */
11003 *insn++ = BPF_ALU64_REG(BPF_ADD, reg, BPF_REG_AX);
11004 /* AX = skb->data_len */
11005 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data_len),
11006 BPF_REG_AX, si->src_reg,
11007 offsetof(struct sk_buff, data_len));
11008
11009 /* reg = skb->data + skb->len - skb->data_len */
11010 *insn++ = BPF_ALU64_REG(BPF_SUB, reg, BPF_REG_AX);
11011
11012 if (si->src_reg == si->dst_reg) {
11013 /* Restore the saved register */
11014 *insn++ = BPF_MOV64_REG(BPF_REG_AX, si->src_reg);
11015 *insn++ = BPF_MOV64_REG(si->dst_reg, reg);
11016 *insn++ = BPF_LDX_MEM(BPF_DW, reg, BPF_REG_AX, temp_reg_off);
11017 }
11018
11019 return insn;
11020 }
11021
sk_skb_convert_ctx_access(enum bpf_access_type type,const struct bpf_insn * si,struct bpf_insn * insn_buf,struct bpf_prog * prog,u32 * target_size)11022 static u32 sk_skb_convert_ctx_access(enum bpf_access_type type,
11023 const struct bpf_insn *si,
11024 struct bpf_insn *insn_buf,
11025 struct bpf_prog *prog, u32 *target_size)
11026 {
11027 struct bpf_insn *insn = insn_buf;
11028 int off;
11029
11030 switch (si->off) {
11031 case offsetof(struct __sk_buff, data_end):
11032 insn = bpf_convert_data_end_access(si, insn);
11033 break;
11034 case offsetof(struct __sk_buff, cb[0]) ...
11035 offsetofend(struct __sk_buff, cb[4]) - 1:
11036 BUILD_BUG_ON(sizeof_field(struct sk_skb_cb, data) < 20);
11037 BUILD_BUG_ON((offsetof(struct sk_buff, cb) +
11038 offsetof(struct sk_skb_cb, data)) %
11039 sizeof(__u64));
11040
11041 prog->cb_access = 1;
11042 off = si->off;
11043 off -= offsetof(struct __sk_buff, cb[0]);
11044 off += offsetof(struct sk_buff, cb);
11045 off += offsetof(struct sk_skb_cb, data);
11046 if (type == BPF_WRITE)
11047 *insn++ = BPF_EMIT_STORE(BPF_SIZE(si->code), si, off);
11048 else
11049 *insn++ = BPF_LDX_MEM(BPF_SIZE(si->code), si->dst_reg,
11050 si->src_reg, off);
11051 break;
11052
11053
11054 default:
11055 return bpf_convert_ctx_access(type, si, insn_buf, prog,
11056 target_size);
11057 }
11058
11059 return insn - insn_buf;
11060 }
11061
sk_msg_convert_ctx_access(enum bpf_access_type type,const struct bpf_insn * si,struct bpf_insn * insn_buf,struct bpf_prog * prog,u32 * target_size)11062 static u32 sk_msg_convert_ctx_access(enum bpf_access_type type,
11063 const struct bpf_insn *si,
11064 struct bpf_insn *insn_buf,
11065 struct bpf_prog *prog, u32 *target_size)
11066 {
11067 struct bpf_insn *insn = insn_buf;
11068 #if IS_ENABLED(CONFIG_IPV6)
11069 int off;
11070 #endif
11071
11072 /* convert ctx uses the fact sg element is first in struct */
11073 BUILD_BUG_ON(offsetof(struct sk_msg, sg) != 0);
11074
11075 switch (si->off) {
11076 case offsetof(struct sk_msg_md, data):
11077 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg, data),
11078 si->dst_reg, si->src_reg,
11079 offsetof(struct sk_msg, data));
11080 break;
11081 case offsetof(struct sk_msg_md, data_end):
11082 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg, data_end),
11083 si->dst_reg, si->src_reg,
11084 offsetof(struct sk_msg, data_end));
11085 break;
11086 case offsetof(struct sk_msg_md, family):
11087 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_family) != 2);
11088
11089 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
11090 struct sk_msg, sk),
11091 si->dst_reg, si->src_reg,
11092 offsetof(struct sk_msg, sk));
11093 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
11094 offsetof(struct sock_common, skc_family));
11095 break;
11096
11097 case offsetof(struct sk_msg_md, remote_ip4):
11098 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_daddr) != 4);
11099
11100 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
11101 struct sk_msg, sk),
11102 si->dst_reg, si->src_reg,
11103 offsetof(struct sk_msg, sk));
11104 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
11105 offsetof(struct sock_common, skc_daddr));
11106 break;
11107
11108 case offsetof(struct sk_msg_md, local_ip4):
11109 BUILD_BUG_ON(sizeof_field(struct sock_common,
11110 skc_rcv_saddr) != 4);
11111
11112 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
11113 struct sk_msg, sk),
11114 si->dst_reg, si->src_reg,
11115 offsetof(struct sk_msg, sk));
11116 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
11117 offsetof(struct sock_common,
11118 skc_rcv_saddr));
11119 break;
11120
11121 case offsetof(struct sk_msg_md, remote_ip6[0]) ...
11122 offsetof(struct sk_msg_md, remote_ip6[3]):
11123 #if IS_ENABLED(CONFIG_IPV6)
11124 BUILD_BUG_ON(sizeof_field(struct sock_common,
11125 skc_v6_daddr.s6_addr32[0]) != 4);
11126
11127 off = si->off;
11128 off -= offsetof(struct sk_msg_md, remote_ip6[0]);
11129 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
11130 struct sk_msg, sk),
11131 si->dst_reg, si->src_reg,
11132 offsetof(struct sk_msg, sk));
11133 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
11134 offsetof(struct sock_common,
11135 skc_v6_daddr.s6_addr32[0]) +
11136 off);
11137 #else
11138 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
11139 #endif
11140 break;
11141
11142 case offsetof(struct sk_msg_md, local_ip6[0]) ...
11143 offsetof(struct sk_msg_md, local_ip6[3]):
11144 #if IS_ENABLED(CONFIG_IPV6)
11145 BUILD_BUG_ON(sizeof_field(struct sock_common,
11146 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
11147
11148 off = si->off;
11149 off -= offsetof(struct sk_msg_md, local_ip6[0]);
11150 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
11151 struct sk_msg, sk),
11152 si->dst_reg, si->src_reg,
11153 offsetof(struct sk_msg, sk));
11154 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
11155 offsetof(struct sock_common,
11156 skc_v6_rcv_saddr.s6_addr32[0]) +
11157 off);
11158 #else
11159 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
11160 #endif
11161 break;
11162
11163 case offsetof(struct sk_msg_md, remote_port):
11164 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_dport) != 2);
11165
11166 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
11167 struct sk_msg, sk),
11168 si->dst_reg, si->src_reg,
11169 offsetof(struct sk_msg, sk));
11170 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
11171 offsetof(struct sock_common, skc_dport));
11172 #ifndef __BIG_ENDIAN_BITFIELD
11173 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
11174 #endif
11175 break;
11176
11177 case offsetof(struct sk_msg_md, local_port):
11178 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_num) != 2);
11179
11180 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
11181 struct sk_msg, sk),
11182 si->dst_reg, si->src_reg,
11183 offsetof(struct sk_msg, sk));
11184 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
11185 offsetof(struct sock_common, skc_num));
11186 break;
11187
11188 case offsetof(struct sk_msg_md, size):
11189 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg_sg, size),
11190 si->dst_reg, si->src_reg,
11191 offsetof(struct sk_msg_sg, size));
11192 break;
11193
11194 case offsetof(struct sk_msg_md, sk):
11195 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg, sk),
11196 si->dst_reg, si->src_reg,
11197 offsetof(struct sk_msg, sk));
11198 break;
11199 }
11200
11201 return insn - insn_buf;
11202 }
11203
11204 const struct bpf_verifier_ops sk_filter_verifier_ops = {
11205 .get_func_proto = sk_filter_func_proto,
11206 .is_valid_access = sk_filter_is_valid_access,
11207 .convert_ctx_access = bpf_convert_ctx_access,
11208 .gen_ld_abs = bpf_gen_ld_abs,
11209 };
11210
11211 const struct bpf_prog_ops sk_filter_prog_ops = {
11212 .test_run = bpf_prog_test_run_skb,
11213 };
11214
11215 const struct bpf_verifier_ops tc_cls_act_verifier_ops = {
11216 .get_func_proto = tc_cls_act_func_proto,
11217 .is_valid_access = tc_cls_act_is_valid_access,
11218 .convert_ctx_access = tc_cls_act_convert_ctx_access,
11219 .gen_prologue = tc_cls_act_prologue,
11220 .gen_ld_abs = bpf_gen_ld_abs,
11221 .btf_struct_access = tc_cls_act_btf_struct_access,
11222 };
11223
11224 const struct bpf_prog_ops tc_cls_act_prog_ops = {
11225 .test_run = bpf_prog_test_run_skb,
11226 };
11227
11228 const struct bpf_verifier_ops xdp_verifier_ops = {
11229 .get_func_proto = xdp_func_proto,
11230 .is_valid_access = xdp_is_valid_access,
11231 .convert_ctx_access = xdp_convert_ctx_access,
11232 .gen_prologue = bpf_noop_prologue,
11233 .btf_struct_access = xdp_btf_struct_access,
11234 };
11235
11236 const struct bpf_prog_ops xdp_prog_ops = {
11237 .test_run = bpf_prog_test_run_xdp,
11238 };
11239
11240 const struct bpf_verifier_ops cg_skb_verifier_ops = {
11241 .get_func_proto = cg_skb_func_proto,
11242 .is_valid_access = cg_skb_is_valid_access,
11243 .convert_ctx_access = bpf_convert_ctx_access,
11244 };
11245
11246 const struct bpf_prog_ops cg_skb_prog_ops = {
11247 .test_run = bpf_prog_test_run_skb,
11248 };
11249
11250 const struct bpf_verifier_ops lwt_in_verifier_ops = {
11251 .get_func_proto = lwt_in_func_proto,
11252 .is_valid_access = lwt_is_valid_access,
11253 .convert_ctx_access = bpf_convert_ctx_access,
11254 };
11255
11256 const struct bpf_prog_ops lwt_in_prog_ops = {
11257 .test_run = bpf_prog_test_run_skb,
11258 };
11259
11260 const struct bpf_verifier_ops lwt_out_verifier_ops = {
11261 .get_func_proto = lwt_out_func_proto,
11262 .is_valid_access = lwt_is_valid_access,
11263 .convert_ctx_access = bpf_convert_ctx_access,
11264 };
11265
11266 const struct bpf_prog_ops lwt_out_prog_ops = {
11267 .test_run = bpf_prog_test_run_skb,
11268 };
11269
11270 const struct bpf_verifier_ops lwt_xmit_verifier_ops = {
11271 .get_func_proto = lwt_xmit_func_proto,
11272 .is_valid_access = lwt_is_valid_access,
11273 .convert_ctx_access = bpf_convert_ctx_access,
11274 .gen_prologue = tc_cls_act_prologue,
11275 };
11276
11277 const struct bpf_prog_ops lwt_xmit_prog_ops = {
11278 .test_run = bpf_prog_test_run_skb,
11279 };
11280
11281 const struct bpf_verifier_ops lwt_seg6local_verifier_ops = {
11282 .get_func_proto = lwt_seg6local_func_proto,
11283 .is_valid_access = lwt_is_valid_access,
11284 .convert_ctx_access = bpf_convert_ctx_access,
11285 };
11286
11287 const struct bpf_prog_ops lwt_seg6local_prog_ops = {
11288 };
11289
11290 const struct bpf_verifier_ops cg_sock_verifier_ops = {
11291 .get_func_proto = sock_filter_func_proto,
11292 .is_valid_access = sock_filter_is_valid_access,
11293 .convert_ctx_access = bpf_sock_convert_ctx_access,
11294 };
11295
11296 const struct bpf_prog_ops cg_sock_prog_ops = {
11297 };
11298
11299 const struct bpf_verifier_ops cg_sock_addr_verifier_ops = {
11300 .get_func_proto = sock_addr_func_proto,
11301 .is_valid_access = sock_addr_is_valid_access,
11302 .convert_ctx_access = sock_addr_convert_ctx_access,
11303 };
11304
11305 const struct bpf_prog_ops cg_sock_addr_prog_ops = {
11306 };
11307
11308 const struct bpf_verifier_ops sock_ops_verifier_ops = {
11309 .get_func_proto = sock_ops_func_proto,
11310 .is_valid_access = sock_ops_is_valid_access,
11311 .convert_ctx_access = sock_ops_convert_ctx_access,
11312 };
11313
11314 const struct bpf_prog_ops sock_ops_prog_ops = {
11315 };
11316
11317 const struct bpf_verifier_ops sk_skb_verifier_ops = {
11318 .get_func_proto = sk_skb_func_proto,
11319 .is_valid_access = sk_skb_is_valid_access,
11320 .convert_ctx_access = sk_skb_convert_ctx_access,
11321 .gen_prologue = sk_skb_prologue,
11322 };
11323
11324 const struct bpf_prog_ops sk_skb_prog_ops = {
11325 };
11326
11327 const struct bpf_verifier_ops sk_msg_verifier_ops = {
11328 .get_func_proto = sk_msg_func_proto,
11329 .is_valid_access = sk_msg_is_valid_access,
11330 .convert_ctx_access = sk_msg_convert_ctx_access,
11331 .gen_prologue = bpf_noop_prologue,
11332 };
11333
11334 const struct bpf_prog_ops sk_msg_prog_ops = {
11335 };
11336
11337 const struct bpf_verifier_ops flow_dissector_verifier_ops = {
11338 .get_func_proto = flow_dissector_func_proto,
11339 .is_valid_access = flow_dissector_is_valid_access,
11340 .convert_ctx_access = flow_dissector_convert_ctx_access,
11341 };
11342
11343 const struct bpf_prog_ops flow_dissector_prog_ops = {
11344 .test_run = bpf_prog_test_run_flow_dissector,
11345 };
11346
sk_detach_filter(struct sock * sk)11347 int sk_detach_filter(struct sock *sk)
11348 {
11349 int ret = -ENOENT;
11350 struct sk_filter *filter;
11351
11352 if (sock_flag(sk, SOCK_FILTER_LOCKED))
11353 return -EPERM;
11354
11355 filter = rcu_dereference_protected(sk->sk_filter,
11356 lockdep_sock_is_held(sk));
11357 if (filter) {
11358 RCU_INIT_POINTER(sk->sk_filter, NULL);
11359 sk_filter_uncharge(sk, filter);
11360 ret = 0;
11361 }
11362
11363 return ret;
11364 }
11365 EXPORT_SYMBOL_GPL(sk_detach_filter);
11366
sk_get_filter(struct sock * sk,sockptr_t optval,unsigned int len)11367 int sk_get_filter(struct sock *sk, sockptr_t optval, unsigned int len)
11368 {
11369 struct sock_fprog_kern *fprog;
11370 struct sk_filter *filter;
11371 int ret = 0;
11372
11373 sockopt_lock_sock(sk);
11374 filter = rcu_dereference_protected(sk->sk_filter,
11375 lockdep_sock_is_held(sk));
11376 if (!filter)
11377 goto out;
11378
11379 /* We're copying the filter that has been originally attached,
11380 * so no conversion/decode needed anymore. eBPF programs that
11381 * have no original program cannot be dumped through this.
11382 */
11383 ret = -EACCES;
11384 fprog = filter->prog->orig_prog;
11385 if (!fprog)
11386 goto out;
11387
11388 ret = fprog->len;
11389 if (!len)
11390 /* User space only enquires number of filter blocks. */
11391 goto out;
11392
11393 ret = -EINVAL;
11394 if (len < fprog->len)
11395 goto out;
11396
11397 ret = -EFAULT;
11398 if (copy_to_sockptr(optval, fprog->filter, bpf_classic_proglen(fprog)))
11399 goto out;
11400
11401 /* Instead of bytes, the API requests to return the number
11402 * of filter blocks.
11403 */
11404 ret = fprog->len;
11405 out:
11406 sockopt_release_sock(sk);
11407 return ret;
11408 }
11409
11410 #ifdef CONFIG_INET
bpf_init_reuseport_kern(struct sk_reuseport_kern * reuse_kern,struct sock_reuseport * reuse,struct sock * sk,struct sk_buff * skb,struct sock * migrating_sk,u32 hash)11411 static void bpf_init_reuseport_kern(struct sk_reuseport_kern *reuse_kern,
11412 struct sock_reuseport *reuse,
11413 struct sock *sk, struct sk_buff *skb,
11414 struct sock *migrating_sk,
11415 u32 hash)
11416 {
11417 reuse_kern->skb = skb;
11418 reuse_kern->sk = sk;
11419 reuse_kern->selected_sk = NULL;
11420 reuse_kern->migrating_sk = migrating_sk;
11421 reuse_kern->data_end = skb->data + skb_headlen(skb);
11422 reuse_kern->hash = hash;
11423 reuse_kern->reuseport_id = reuse->reuseport_id;
11424 reuse_kern->bind_inany = reuse->bind_inany;
11425 }
11426
bpf_run_sk_reuseport(struct sock_reuseport * reuse,struct sock * sk,struct bpf_prog * prog,struct sk_buff * skb,struct sock * migrating_sk,u32 hash)11427 struct sock *bpf_run_sk_reuseport(struct sock_reuseport *reuse, struct sock *sk,
11428 struct bpf_prog *prog, struct sk_buff *skb,
11429 struct sock *migrating_sk,
11430 u32 hash)
11431 {
11432 struct sk_reuseport_kern reuse_kern;
11433 enum sk_action action;
11434
11435 bpf_init_reuseport_kern(&reuse_kern, reuse, sk, skb, migrating_sk, hash);
11436 action = bpf_prog_run(prog, &reuse_kern);
11437
11438 if (action == SK_PASS)
11439 return reuse_kern.selected_sk;
11440 else
11441 return ERR_PTR(-ECONNREFUSED);
11442 }
11443
BPF_CALL_4(sk_select_reuseport,struct sk_reuseport_kern *,reuse_kern,struct bpf_map *,map,void *,key,u32,flags)11444 BPF_CALL_4(sk_select_reuseport, struct sk_reuseport_kern *, reuse_kern,
11445 struct bpf_map *, map, void *, key, u32, flags)
11446 {
11447 bool is_sockarray = map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY;
11448 struct sock_reuseport *reuse;
11449 struct sock *selected_sk;
11450 int err;
11451
11452 selected_sk = map->ops->map_lookup_elem(map, key);
11453 if (!selected_sk)
11454 return -ENOENT;
11455
11456 reuse = rcu_dereference(selected_sk->sk_reuseport_cb);
11457 if (!reuse) {
11458 /* reuseport_array has only sk with non NULL sk_reuseport_cb.
11459 * The only (!reuse) case here is - the sk has already been
11460 * unhashed (e.g. by close()), so treat it as -ENOENT.
11461 *
11462 * Other maps (e.g. sock_map) do not provide this guarantee and
11463 * the sk may never be in the reuseport group to begin with.
11464 */
11465 err = is_sockarray ? -ENOENT : -EINVAL;
11466 goto error;
11467 }
11468
11469 if (unlikely(reuse->reuseport_id != reuse_kern->reuseport_id)) {
11470 struct sock *sk = reuse_kern->sk;
11471
11472 if (sk->sk_protocol != selected_sk->sk_protocol) {
11473 err = -EPROTOTYPE;
11474 } else if (sk->sk_family != selected_sk->sk_family) {
11475 err = -EAFNOSUPPORT;
11476 } else {
11477 /* Catch all. Likely bound to a different sockaddr. */
11478 err = -EBADFD;
11479 }
11480 goto error;
11481 }
11482
11483 reuse_kern->selected_sk = selected_sk;
11484
11485 return 0;
11486 error:
11487 /* Lookup in sock_map can return TCP ESTABLISHED sockets. */
11488 if (sk_is_refcounted(selected_sk))
11489 sock_put(selected_sk);
11490
11491 return err;
11492 }
11493
11494 static const struct bpf_func_proto sk_select_reuseport_proto = {
11495 .func = sk_select_reuseport,
11496 .gpl_only = false,
11497 .ret_type = RET_INTEGER,
11498 .arg1_type = ARG_PTR_TO_CTX,
11499 .arg2_type = ARG_CONST_MAP_PTR,
11500 .arg3_type = ARG_PTR_TO_MAP_KEY,
11501 .arg4_type = ARG_ANYTHING,
11502 };
11503
BPF_CALL_4(sk_reuseport_load_bytes,const struct sk_reuseport_kern *,reuse_kern,u32,offset,void *,to,u32,len)11504 BPF_CALL_4(sk_reuseport_load_bytes,
11505 const struct sk_reuseport_kern *, reuse_kern, u32, offset,
11506 void *, to, u32, len)
11507 {
11508 return ____bpf_skb_load_bytes(reuse_kern->skb, offset, to, len);
11509 }
11510
11511 static const struct bpf_func_proto sk_reuseport_load_bytes_proto = {
11512 .func = sk_reuseport_load_bytes,
11513 .gpl_only = false,
11514 .ret_type = RET_INTEGER,
11515 .arg1_type = ARG_PTR_TO_CTX,
11516 .arg2_type = ARG_ANYTHING,
11517 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
11518 .arg4_type = ARG_CONST_SIZE,
11519 };
11520
BPF_CALL_5(sk_reuseport_load_bytes_relative,const struct sk_reuseport_kern *,reuse_kern,u32,offset,void *,to,u32,len,u32,start_header)11521 BPF_CALL_5(sk_reuseport_load_bytes_relative,
11522 const struct sk_reuseport_kern *, reuse_kern, u32, offset,
11523 void *, to, u32, len, u32, start_header)
11524 {
11525 return ____bpf_skb_load_bytes_relative(reuse_kern->skb, offset, to,
11526 len, start_header);
11527 }
11528
11529 static const struct bpf_func_proto sk_reuseport_load_bytes_relative_proto = {
11530 .func = sk_reuseport_load_bytes_relative,
11531 .gpl_only = false,
11532 .ret_type = RET_INTEGER,
11533 .arg1_type = ARG_PTR_TO_CTX,
11534 .arg2_type = ARG_ANYTHING,
11535 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
11536 .arg4_type = ARG_CONST_SIZE,
11537 .arg5_type = ARG_ANYTHING,
11538 };
11539
11540 static const struct bpf_func_proto *
sk_reuseport_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)11541 sk_reuseport_func_proto(enum bpf_func_id func_id,
11542 const struct bpf_prog *prog)
11543 {
11544 switch (func_id) {
11545 case BPF_FUNC_sk_select_reuseport:
11546 return &sk_select_reuseport_proto;
11547 case BPF_FUNC_skb_load_bytes:
11548 return &sk_reuseport_load_bytes_proto;
11549 case BPF_FUNC_skb_load_bytes_relative:
11550 return &sk_reuseport_load_bytes_relative_proto;
11551 case BPF_FUNC_get_socket_cookie:
11552 return &bpf_get_socket_ptr_cookie_proto;
11553 case BPF_FUNC_ktime_get_coarse_ns:
11554 return &bpf_ktime_get_coarse_ns_proto;
11555 default:
11556 return bpf_base_func_proto(func_id, prog);
11557 }
11558 }
11559
11560 static bool
sk_reuseport_is_valid_access(int off,int size,enum bpf_access_type type,const struct bpf_prog * prog,struct bpf_insn_access_aux * info)11561 sk_reuseport_is_valid_access(int off, int size,
11562 enum bpf_access_type type,
11563 const struct bpf_prog *prog,
11564 struct bpf_insn_access_aux *info)
11565 {
11566 const u32 size_default = sizeof(__u32);
11567
11568 if (off < 0 || off >= sizeof(struct sk_reuseport_md) ||
11569 off % size || type != BPF_READ)
11570 return false;
11571
11572 switch (off) {
11573 case offsetof(struct sk_reuseport_md, data):
11574 info->reg_type = PTR_TO_PACKET;
11575 return size == sizeof(__u64);
11576
11577 case offsetof(struct sk_reuseport_md, data_end):
11578 info->reg_type = PTR_TO_PACKET_END;
11579 return size == sizeof(__u64);
11580
11581 case offsetof(struct sk_reuseport_md, hash):
11582 return size == size_default;
11583
11584 case offsetof(struct sk_reuseport_md, sk):
11585 info->reg_type = PTR_TO_SOCKET;
11586 return size == sizeof(__u64);
11587
11588 case offsetof(struct sk_reuseport_md, migrating_sk):
11589 info->reg_type = PTR_TO_SOCK_COMMON_OR_NULL;
11590 return size == sizeof(__u64);
11591
11592 /* Fields that allow narrowing */
11593 case bpf_ctx_range(struct sk_reuseport_md, eth_protocol):
11594 if (size < sizeof_field(struct sk_buff, protocol))
11595 return false;
11596 fallthrough;
11597 case bpf_ctx_range(struct sk_reuseport_md, ip_protocol):
11598 case bpf_ctx_range(struct sk_reuseport_md, bind_inany):
11599 case bpf_ctx_range(struct sk_reuseport_md, len):
11600 bpf_ctx_record_field_size(info, size_default);
11601 return bpf_ctx_narrow_access_ok(off, size, size_default);
11602
11603 default:
11604 return false;
11605 }
11606 }
11607
11608 #define SK_REUSEPORT_LOAD_FIELD(F) ({ \
11609 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_reuseport_kern, F), \
11610 si->dst_reg, si->src_reg, \
11611 bpf_target_off(struct sk_reuseport_kern, F, \
11612 sizeof_field(struct sk_reuseport_kern, F), \
11613 target_size)); \
11614 })
11615
11616 #define SK_REUSEPORT_LOAD_SKB_FIELD(SKB_FIELD) \
11617 SOCK_ADDR_LOAD_NESTED_FIELD(struct sk_reuseport_kern, \
11618 struct sk_buff, \
11619 skb, \
11620 SKB_FIELD)
11621
11622 #define SK_REUSEPORT_LOAD_SK_FIELD(SK_FIELD) \
11623 SOCK_ADDR_LOAD_NESTED_FIELD(struct sk_reuseport_kern, \
11624 struct sock, \
11625 sk, \
11626 SK_FIELD)
11627
sk_reuseport_convert_ctx_access(enum bpf_access_type type,const struct bpf_insn * si,struct bpf_insn * insn_buf,struct bpf_prog * prog,u32 * target_size)11628 static u32 sk_reuseport_convert_ctx_access(enum bpf_access_type type,
11629 const struct bpf_insn *si,
11630 struct bpf_insn *insn_buf,
11631 struct bpf_prog *prog,
11632 u32 *target_size)
11633 {
11634 struct bpf_insn *insn = insn_buf;
11635
11636 switch (si->off) {
11637 case offsetof(struct sk_reuseport_md, data):
11638 SK_REUSEPORT_LOAD_SKB_FIELD(data);
11639 break;
11640
11641 case offsetof(struct sk_reuseport_md, len):
11642 SK_REUSEPORT_LOAD_SKB_FIELD(len);
11643 break;
11644
11645 case offsetof(struct sk_reuseport_md, eth_protocol):
11646 SK_REUSEPORT_LOAD_SKB_FIELD(protocol);
11647 break;
11648
11649 case offsetof(struct sk_reuseport_md, ip_protocol):
11650 SK_REUSEPORT_LOAD_SK_FIELD(sk_protocol);
11651 break;
11652
11653 case offsetof(struct sk_reuseport_md, data_end):
11654 SK_REUSEPORT_LOAD_FIELD(data_end);
11655 break;
11656
11657 case offsetof(struct sk_reuseport_md, hash):
11658 SK_REUSEPORT_LOAD_FIELD(hash);
11659 break;
11660
11661 case offsetof(struct sk_reuseport_md, bind_inany):
11662 SK_REUSEPORT_LOAD_FIELD(bind_inany);
11663 break;
11664
11665 case offsetof(struct sk_reuseport_md, sk):
11666 SK_REUSEPORT_LOAD_FIELD(sk);
11667 break;
11668
11669 case offsetof(struct sk_reuseport_md, migrating_sk):
11670 SK_REUSEPORT_LOAD_FIELD(migrating_sk);
11671 break;
11672 }
11673
11674 return insn - insn_buf;
11675 }
11676
11677 const struct bpf_verifier_ops sk_reuseport_verifier_ops = {
11678 .get_func_proto = sk_reuseport_func_proto,
11679 .is_valid_access = sk_reuseport_is_valid_access,
11680 .convert_ctx_access = sk_reuseport_convert_ctx_access,
11681 };
11682
11683 const struct bpf_prog_ops sk_reuseport_prog_ops = {
11684 };
11685
11686 DEFINE_STATIC_KEY_FALSE(bpf_sk_lookup_enabled);
11687 EXPORT_SYMBOL(bpf_sk_lookup_enabled);
11688
BPF_CALL_3(bpf_sk_lookup_assign,struct bpf_sk_lookup_kern *,ctx,struct sock *,sk,u64,flags)11689 BPF_CALL_3(bpf_sk_lookup_assign, struct bpf_sk_lookup_kern *, ctx,
11690 struct sock *, sk, u64, flags)
11691 {
11692 if (unlikely(flags & ~(BPF_SK_LOOKUP_F_REPLACE |
11693 BPF_SK_LOOKUP_F_NO_REUSEPORT)))
11694 return -EINVAL;
11695 if (unlikely(sk && sk_is_refcounted(sk)))
11696 return -ESOCKTNOSUPPORT; /* reject non-RCU freed sockets */
11697 if (unlikely(sk && sk_is_tcp(sk) && sk->sk_state != TCP_LISTEN))
11698 return -ESOCKTNOSUPPORT; /* only accept TCP socket in LISTEN */
11699 if (unlikely(sk && sk_is_udp(sk) && sk->sk_state != TCP_CLOSE))
11700 return -ESOCKTNOSUPPORT; /* only accept UDP socket in CLOSE */
11701
11702 /* Check if socket is suitable for packet L3/L4 protocol */
11703 if (sk && sk->sk_protocol != ctx->protocol)
11704 return -EPROTOTYPE;
11705 if (sk && sk->sk_family != ctx->family &&
11706 (sk->sk_family == AF_INET || ipv6_only_sock(sk)))
11707 return -EAFNOSUPPORT;
11708
11709 if (ctx->selected_sk && !(flags & BPF_SK_LOOKUP_F_REPLACE))
11710 return -EEXIST;
11711
11712 /* Select socket as lookup result */
11713 ctx->selected_sk = sk;
11714 ctx->no_reuseport = flags & BPF_SK_LOOKUP_F_NO_REUSEPORT;
11715 return 0;
11716 }
11717
11718 static const struct bpf_func_proto bpf_sk_lookup_assign_proto = {
11719 .func = bpf_sk_lookup_assign,
11720 .gpl_only = false,
11721 .ret_type = RET_INTEGER,
11722 .arg1_type = ARG_PTR_TO_CTX,
11723 .arg2_type = ARG_PTR_TO_SOCKET_OR_NULL,
11724 .arg3_type = ARG_ANYTHING,
11725 };
11726
11727 static const struct bpf_func_proto *
sk_lookup_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)11728 sk_lookup_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
11729 {
11730 switch (func_id) {
11731 case BPF_FUNC_perf_event_output:
11732 return &bpf_event_output_data_proto;
11733 case BPF_FUNC_sk_assign:
11734 return &bpf_sk_lookup_assign_proto;
11735 case BPF_FUNC_sk_release:
11736 return &bpf_sk_release_proto;
11737 default:
11738 return bpf_sk_base_func_proto(func_id, prog);
11739 }
11740 }
11741
sk_lookup_is_valid_access(int off,int size,enum bpf_access_type type,const struct bpf_prog * prog,struct bpf_insn_access_aux * info)11742 static bool sk_lookup_is_valid_access(int off, int size,
11743 enum bpf_access_type type,
11744 const struct bpf_prog *prog,
11745 struct bpf_insn_access_aux *info)
11746 {
11747 if (off < 0 || off >= sizeof(struct bpf_sk_lookup))
11748 return false;
11749 if (off % size != 0)
11750 return false;
11751 if (type != BPF_READ)
11752 return false;
11753
11754 switch (off) {
11755 case bpf_ctx_range_ptr(struct bpf_sk_lookup, sk):
11756 info->reg_type = PTR_TO_SOCKET_OR_NULL;
11757 return size == sizeof(__u64);
11758
11759 case bpf_ctx_range(struct bpf_sk_lookup, family):
11760 case bpf_ctx_range(struct bpf_sk_lookup, protocol):
11761 case bpf_ctx_range(struct bpf_sk_lookup, remote_ip4):
11762 case bpf_ctx_range(struct bpf_sk_lookup, local_ip4):
11763 case bpf_ctx_range_till(struct bpf_sk_lookup, remote_ip6[0], remote_ip6[3]):
11764 case bpf_ctx_range_till(struct bpf_sk_lookup, local_ip6[0], local_ip6[3]):
11765 case bpf_ctx_range(struct bpf_sk_lookup, local_port):
11766 case bpf_ctx_range(struct bpf_sk_lookup, ingress_ifindex):
11767 bpf_ctx_record_field_size(info, sizeof(__u32));
11768 return bpf_ctx_narrow_access_ok(off, size, sizeof(__u32));
11769
11770 case bpf_ctx_range(struct bpf_sk_lookup, remote_port):
11771 /* Allow 4-byte access to 2-byte field for backward compatibility */
11772 if (size == sizeof(__u32))
11773 return true;
11774 bpf_ctx_record_field_size(info, sizeof(__be16));
11775 return bpf_ctx_narrow_access_ok(off, size, sizeof(__be16));
11776
11777 case offsetofend(struct bpf_sk_lookup, remote_port) ...
11778 offsetof(struct bpf_sk_lookup, local_ip4) - 1:
11779 /* Allow access to zero padding for backward compatibility */
11780 bpf_ctx_record_field_size(info, sizeof(__u16));
11781 return bpf_ctx_narrow_access_ok(off, size, sizeof(__u16));
11782
11783 default:
11784 return false;
11785 }
11786 }
11787
sk_lookup_convert_ctx_access(enum bpf_access_type type,const struct bpf_insn * si,struct bpf_insn * insn_buf,struct bpf_prog * prog,u32 * target_size)11788 static u32 sk_lookup_convert_ctx_access(enum bpf_access_type type,
11789 const struct bpf_insn *si,
11790 struct bpf_insn *insn_buf,
11791 struct bpf_prog *prog,
11792 u32 *target_size)
11793 {
11794 struct bpf_insn *insn = insn_buf;
11795
11796 switch (si->off) {
11797 case offsetof(struct bpf_sk_lookup, sk):
11798 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg, si->src_reg,
11799 offsetof(struct bpf_sk_lookup_kern, selected_sk));
11800 break;
11801
11802 case offsetof(struct bpf_sk_lookup, family):
11803 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
11804 bpf_target_off(struct bpf_sk_lookup_kern,
11805 family, 2, target_size));
11806 break;
11807
11808 case offsetof(struct bpf_sk_lookup, protocol):
11809 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
11810 bpf_target_off(struct bpf_sk_lookup_kern,
11811 protocol, 2, target_size));
11812 break;
11813
11814 case offsetof(struct bpf_sk_lookup, remote_ip4):
11815 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
11816 bpf_target_off(struct bpf_sk_lookup_kern,
11817 v4.saddr, 4, target_size));
11818 break;
11819
11820 case offsetof(struct bpf_sk_lookup, local_ip4):
11821 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
11822 bpf_target_off(struct bpf_sk_lookup_kern,
11823 v4.daddr, 4, target_size));
11824 break;
11825
11826 case bpf_ctx_range_till(struct bpf_sk_lookup,
11827 remote_ip6[0], remote_ip6[3]): {
11828 #if IS_ENABLED(CONFIG_IPV6)
11829 int off = si->off;
11830
11831 off -= offsetof(struct bpf_sk_lookup, remote_ip6[0]);
11832 off += bpf_target_off(struct in6_addr, s6_addr32[0], 4, target_size);
11833 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg, si->src_reg,
11834 offsetof(struct bpf_sk_lookup_kern, v6.saddr));
11835 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
11836 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, off);
11837 #else
11838 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
11839 #endif
11840 break;
11841 }
11842 case bpf_ctx_range_till(struct bpf_sk_lookup,
11843 local_ip6[0], local_ip6[3]): {
11844 #if IS_ENABLED(CONFIG_IPV6)
11845 int off = si->off;
11846
11847 off -= offsetof(struct bpf_sk_lookup, local_ip6[0]);
11848 off += bpf_target_off(struct in6_addr, s6_addr32[0], 4, target_size);
11849 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg, si->src_reg,
11850 offsetof(struct bpf_sk_lookup_kern, v6.daddr));
11851 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
11852 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, off);
11853 #else
11854 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
11855 #endif
11856 break;
11857 }
11858 case offsetof(struct bpf_sk_lookup, remote_port):
11859 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
11860 bpf_target_off(struct bpf_sk_lookup_kern,
11861 sport, 2, target_size));
11862 break;
11863
11864 case offsetofend(struct bpf_sk_lookup, remote_port):
11865 *target_size = 2;
11866 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
11867 break;
11868
11869 case offsetof(struct bpf_sk_lookup, local_port):
11870 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
11871 bpf_target_off(struct bpf_sk_lookup_kern,
11872 dport, 2, target_size));
11873 break;
11874
11875 case offsetof(struct bpf_sk_lookup, ingress_ifindex):
11876 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
11877 bpf_target_off(struct bpf_sk_lookup_kern,
11878 ingress_ifindex, 4, target_size));
11879 break;
11880 }
11881
11882 return insn - insn_buf;
11883 }
11884
11885 const struct bpf_prog_ops sk_lookup_prog_ops = {
11886 .test_run = bpf_prog_test_run_sk_lookup,
11887 };
11888
11889 const struct bpf_verifier_ops sk_lookup_verifier_ops = {
11890 .get_func_proto = sk_lookup_func_proto,
11891 .is_valid_access = sk_lookup_is_valid_access,
11892 .convert_ctx_access = sk_lookup_convert_ctx_access,
11893 };
11894
11895 #endif /* CONFIG_INET */
11896
DEFINE_BPF_DISPATCHER(xdp)11897 DEFINE_BPF_DISPATCHER(xdp)
11898
11899 void bpf_prog_change_xdp(struct bpf_prog *prev_prog, struct bpf_prog *prog)
11900 {
11901 bpf_dispatcher_change_prog(BPF_DISPATCHER_PTR(xdp), prev_prog, prog);
11902 }
11903
BTF_ID_LIST_GLOBAL(btf_sock_ids,MAX_BTF_SOCK_TYPE)11904 BTF_ID_LIST_GLOBAL(btf_sock_ids, MAX_BTF_SOCK_TYPE)
11905 #define BTF_SOCK_TYPE(name, type) BTF_ID(struct, type)
11906 BTF_SOCK_TYPE_xxx
11907 #undef BTF_SOCK_TYPE
11908
11909 BPF_CALL_1(bpf_skc_to_tcp6_sock, struct sock *, sk)
11910 {
11911 /* tcp6_sock type is not generated in dwarf and hence btf,
11912 * trigger an explicit type generation here.
11913 */
11914 BTF_TYPE_EMIT(struct tcp6_sock);
11915 if (sk && sk_fullsock(sk) && sk->sk_protocol == IPPROTO_TCP &&
11916 sk->sk_family == AF_INET6)
11917 return (unsigned long)sk;
11918
11919 return (unsigned long)NULL;
11920 }
11921
11922 const struct bpf_func_proto bpf_skc_to_tcp6_sock_proto = {
11923 .func = bpf_skc_to_tcp6_sock,
11924 .gpl_only = false,
11925 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
11926 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
11927 .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_TCP6],
11928 };
11929
BPF_CALL_1(bpf_skc_to_tcp_sock,struct sock *,sk)11930 BPF_CALL_1(bpf_skc_to_tcp_sock, struct sock *, sk)
11931 {
11932 if (sk && sk_fullsock(sk) && sk->sk_protocol == IPPROTO_TCP)
11933 return (unsigned long)sk;
11934
11935 return (unsigned long)NULL;
11936 }
11937
11938 const struct bpf_func_proto bpf_skc_to_tcp_sock_proto = {
11939 .func = bpf_skc_to_tcp_sock,
11940 .gpl_only = false,
11941 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
11942 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
11943 .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_TCP],
11944 };
11945
BPF_CALL_1(bpf_skc_to_tcp_timewait_sock,struct sock *,sk)11946 BPF_CALL_1(bpf_skc_to_tcp_timewait_sock, struct sock *, sk)
11947 {
11948 /* BTF types for tcp_timewait_sock and inet_timewait_sock are not
11949 * generated if CONFIG_INET=n. Trigger an explicit generation here.
11950 */
11951 BTF_TYPE_EMIT(struct inet_timewait_sock);
11952 BTF_TYPE_EMIT(struct tcp_timewait_sock);
11953
11954 #ifdef CONFIG_INET
11955 if (sk && sk->sk_prot == &tcp_prot && sk->sk_state == TCP_TIME_WAIT)
11956 return (unsigned long)sk;
11957 #endif
11958
11959 #if IS_BUILTIN(CONFIG_IPV6)
11960 if (sk && sk->sk_prot == &tcpv6_prot && sk->sk_state == TCP_TIME_WAIT)
11961 return (unsigned long)sk;
11962 #endif
11963
11964 return (unsigned long)NULL;
11965 }
11966
11967 const struct bpf_func_proto bpf_skc_to_tcp_timewait_sock_proto = {
11968 .func = bpf_skc_to_tcp_timewait_sock,
11969 .gpl_only = false,
11970 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
11971 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
11972 .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_TCP_TW],
11973 };
11974
BPF_CALL_1(bpf_skc_to_tcp_request_sock,struct sock *,sk)11975 BPF_CALL_1(bpf_skc_to_tcp_request_sock, struct sock *, sk)
11976 {
11977 #ifdef CONFIG_INET
11978 if (sk && sk->sk_prot == &tcp_prot && sk->sk_state == TCP_NEW_SYN_RECV)
11979 return (unsigned long)sk;
11980 #endif
11981
11982 #if IS_BUILTIN(CONFIG_IPV6)
11983 if (sk && sk->sk_prot == &tcpv6_prot && sk->sk_state == TCP_NEW_SYN_RECV)
11984 return (unsigned long)sk;
11985 #endif
11986
11987 return (unsigned long)NULL;
11988 }
11989
11990 const struct bpf_func_proto bpf_skc_to_tcp_request_sock_proto = {
11991 .func = bpf_skc_to_tcp_request_sock,
11992 .gpl_only = false,
11993 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
11994 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
11995 .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_TCP_REQ],
11996 };
11997
BPF_CALL_1(bpf_skc_to_udp6_sock,struct sock *,sk)11998 BPF_CALL_1(bpf_skc_to_udp6_sock, struct sock *, sk)
11999 {
12000 /* udp6_sock type is not generated in dwarf and hence btf,
12001 * trigger an explicit type generation here.
12002 */
12003 BTF_TYPE_EMIT(struct udp6_sock);
12004 if (sk && sk_fullsock(sk) && sk->sk_protocol == IPPROTO_UDP &&
12005 sk->sk_type == SOCK_DGRAM && sk->sk_family == AF_INET6)
12006 return (unsigned long)sk;
12007
12008 return (unsigned long)NULL;
12009 }
12010
12011 const struct bpf_func_proto bpf_skc_to_udp6_sock_proto = {
12012 .func = bpf_skc_to_udp6_sock,
12013 .gpl_only = false,
12014 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
12015 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
12016 .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_UDP6],
12017 };
12018
BPF_CALL_1(bpf_skc_to_unix_sock,struct sock *,sk)12019 BPF_CALL_1(bpf_skc_to_unix_sock, struct sock *, sk)
12020 {
12021 /* unix_sock type is not generated in dwarf and hence btf,
12022 * trigger an explicit type generation here.
12023 */
12024 BTF_TYPE_EMIT(struct unix_sock);
12025 if (sk && sk_is_unix(sk))
12026 return (unsigned long)sk;
12027
12028 return (unsigned long)NULL;
12029 }
12030
12031 const struct bpf_func_proto bpf_skc_to_unix_sock_proto = {
12032 .func = bpf_skc_to_unix_sock,
12033 .gpl_only = false,
12034 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
12035 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
12036 .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_UNIX],
12037 };
12038
BPF_CALL_1(bpf_skc_to_mptcp_sock,struct sock *,sk)12039 BPF_CALL_1(bpf_skc_to_mptcp_sock, struct sock *, sk)
12040 {
12041 BTF_TYPE_EMIT(struct mptcp_sock);
12042 return (unsigned long)bpf_mptcp_sock_from_subflow(sk);
12043 }
12044
12045 const struct bpf_func_proto bpf_skc_to_mptcp_sock_proto = {
12046 .func = bpf_skc_to_mptcp_sock,
12047 .gpl_only = false,
12048 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
12049 .arg1_type = ARG_PTR_TO_SOCK_COMMON,
12050 .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_MPTCP],
12051 };
12052
BPF_CALL_1(bpf_sock_from_file,struct file *,file)12053 BPF_CALL_1(bpf_sock_from_file, struct file *, file)
12054 {
12055 return (unsigned long)sock_from_file(file);
12056 }
12057
12058 BTF_ID_LIST(bpf_sock_from_file_btf_ids)
12059 BTF_ID(struct, socket)
12060 BTF_ID(struct, file)
12061
12062 const struct bpf_func_proto bpf_sock_from_file_proto = {
12063 .func = bpf_sock_from_file,
12064 .gpl_only = false,
12065 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
12066 .ret_btf_id = &bpf_sock_from_file_btf_ids[0],
12067 .arg1_type = ARG_PTR_TO_BTF_ID,
12068 .arg1_btf_id = &bpf_sock_from_file_btf_ids[1],
12069 };
12070
12071 static const struct bpf_func_proto *
bpf_sk_base_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)12072 bpf_sk_base_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
12073 {
12074 const struct bpf_func_proto *func;
12075
12076 switch (func_id) {
12077 case BPF_FUNC_skc_to_tcp6_sock:
12078 func = &bpf_skc_to_tcp6_sock_proto;
12079 break;
12080 case BPF_FUNC_skc_to_tcp_sock:
12081 func = &bpf_skc_to_tcp_sock_proto;
12082 break;
12083 case BPF_FUNC_skc_to_tcp_timewait_sock:
12084 func = &bpf_skc_to_tcp_timewait_sock_proto;
12085 break;
12086 case BPF_FUNC_skc_to_tcp_request_sock:
12087 func = &bpf_skc_to_tcp_request_sock_proto;
12088 break;
12089 case BPF_FUNC_skc_to_udp6_sock:
12090 func = &bpf_skc_to_udp6_sock_proto;
12091 break;
12092 case BPF_FUNC_skc_to_unix_sock:
12093 func = &bpf_skc_to_unix_sock_proto;
12094 break;
12095 case BPF_FUNC_skc_to_mptcp_sock:
12096 func = &bpf_skc_to_mptcp_sock_proto;
12097 break;
12098 case BPF_FUNC_ktime_get_coarse_ns:
12099 return &bpf_ktime_get_coarse_ns_proto;
12100 default:
12101 return bpf_base_func_proto(func_id, prog);
12102 }
12103
12104 if (!bpf_token_capable(prog->aux->token, CAP_PERFMON))
12105 return NULL;
12106
12107 return func;
12108 }
12109
12110 /**
12111 * bpf_skb_meta_pointer() - Gets a mutable pointer within the skb metadata area.
12112 * @skb: socket buffer carrying the metadata
12113 * @offset: offset into the metadata area, must be <= skb_metadata_len()
12114 */
bpf_skb_meta_pointer(struct sk_buff * skb,u32 offset)12115 void *bpf_skb_meta_pointer(struct sk_buff *skb, u32 offset)
12116 {
12117 return skb_metadata_end(skb) - skb_metadata_len(skb) + offset;
12118 }
12119
__bpf_skb_meta_store_bytes(struct sk_buff * skb,u32 offset,const void * from,u32 len,u64 flags)12120 int __bpf_skb_meta_store_bytes(struct sk_buff *skb, u32 offset,
12121 const void *from, u32 len, u64 flags)
12122 {
12123 if (unlikely(flags))
12124 return -EINVAL;
12125 if (unlikely(bpf_try_make_writable(skb, 0)))
12126 return -EFAULT;
12127
12128 memmove(bpf_skb_meta_pointer(skb, offset), from, len);
12129 return 0;
12130 }
12131
12132 __bpf_kfunc_start_defs();
bpf_dynptr_from_skb(struct __sk_buff * s,u64 flags,struct bpf_dynptr * ptr__uninit)12133 __bpf_kfunc int bpf_dynptr_from_skb(struct __sk_buff *s, u64 flags,
12134 struct bpf_dynptr *ptr__uninit)
12135 {
12136 struct bpf_dynptr_kern *ptr = (struct bpf_dynptr_kern *)ptr__uninit;
12137 struct sk_buff *skb = (struct sk_buff *)s;
12138
12139 if (flags) {
12140 bpf_dynptr_set_null(ptr);
12141 return -EINVAL;
12142 }
12143
12144 bpf_dynptr_init(ptr, skb, BPF_DYNPTR_TYPE_SKB, 0, skb->len);
12145
12146 return 0;
12147 }
12148
12149 /**
12150 * bpf_dynptr_from_skb_meta() - Initialize a dynptr to the skb metadata area.
12151 * @skb_: socket buffer carrying the metadata
12152 * @flags: future use, must be zero
12153 * @ptr__uninit: dynptr to initialize
12154 *
12155 * Set up a dynptr for access to the metadata area earlier allocated from the
12156 * XDP context with bpf_xdp_adjust_meta(). Serves as an alternative to
12157 * &__sk_buff->data_meta.
12158 *
12159 * Return:
12160 * * %0 - dynptr ready to use
12161 * * %-EINVAL - invalid flags, dynptr set to null
12162 */
bpf_dynptr_from_skb_meta(struct __sk_buff * skb_,u64 flags,struct bpf_dynptr * ptr__uninit)12163 __bpf_kfunc int bpf_dynptr_from_skb_meta(struct __sk_buff *skb_, u64 flags,
12164 struct bpf_dynptr *ptr__uninit)
12165 {
12166 struct bpf_dynptr_kern *ptr = (struct bpf_dynptr_kern *)ptr__uninit;
12167 struct sk_buff *skb = (struct sk_buff *)skb_;
12168
12169 if (flags) {
12170 bpf_dynptr_set_null(ptr);
12171 return -EINVAL;
12172 }
12173
12174 bpf_dynptr_init(ptr, skb, BPF_DYNPTR_TYPE_SKB_META, 0, skb_metadata_len(skb));
12175
12176 return 0;
12177 }
12178
bpf_dynptr_from_xdp(struct xdp_md * x,u64 flags,struct bpf_dynptr * ptr__uninit)12179 __bpf_kfunc int bpf_dynptr_from_xdp(struct xdp_md *x, u64 flags,
12180 struct bpf_dynptr *ptr__uninit)
12181 {
12182 struct bpf_dynptr_kern *ptr = (struct bpf_dynptr_kern *)ptr__uninit;
12183 struct xdp_buff *xdp = (struct xdp_buff *)x;
12184
12185 if (flags) {
12186 bpf_dynptr_set_null(ptr);
12187 return -EINVAL;
12188 }
12189
12190 bpf_dynptr_init(ptr, xdp, BPF_DYNPTR_TYPE_XDP, 0, xdp_get_buff_len(xdp));
12191
12192 return 0;
12193 }
12194
bpf_sock_addr_set_sun_path(struct bpf_sock_addr_kern * sa_kern,const u8 * sun_path,u32 sun_path__sz)12195 __bpf_kfunc int bpf_sock_addr_set_sun_path(struct bpf_sock_addr_kern *sa_kern,
12196 const u8 *sun_path, u32 sun_path__sz)
12197 {
12198 struct sockaddr_un *un;
12199
12200 if (sa_kern->sk->sk_family != AF_UNIX)
12201 return -EINVAL;
12202
12203 /* We do not allow changing the address to unnamed or larger than the
12204 * maximum allowed address size for a unix sockaddr.
12205 */
12206 if (sun_path__sz == 0 || sun_path__sz > UNIX_PATH_MAX)
12207 return -EINVAL;
12208
12209 un = (struct sockaddr_un *)sa_kern->uaddr;
12210 memcpy(un->sun_path, sun_path, sun_path__sz);
12211 sa_kern->uaddrlen = offsetof(struct sockaddr_un, sun_path) + sun_path__sz;
12212
12213 return 0;
12214 }
12215
bpf_sk_assign_tcp_reqsk(struct __sk_buff * s,struct sock * sk,struct bpf_tcp_req_attrs * attrs,int attrs__sz)12216 __bpf_kfunc int bpf_sk_assign_tcp_reqsk(struct __sk_buff *s, struct sock *sk,
12217 struct bpf_tcp_req_attrs *attrs, int attrs__sz)
12218 {
12219 #if IS_ENABLED(CONFIG_SYN_COOKIES)
12220 struct sk_buff *skb = (struct sk_buff *)s;
12221 const struct request_sock_ops *ops;
12222 struct inet_request_sock *ireq;
12223 struct tcp_request_sock *treq;
12224 struct request_sock *req;
12225 struct net *net;
12226 __u16 min_mss;
12227 u32 tsoff = 0;
12228
12229 if (attrs__sz != sizeof(*attrs) ||
12230 attrs->reserved[0] || attrs->reserved[1] || attrs->reserved[2])
12231 return -EINVAL;
12232
12233 if (!skb_at_tc_ingress(skb))
12234 return -EINVAL;
12235
12236 net = dev_net(skb->dev);
12237 if (net != sock_net(sk))
12238 return -ENETUNREACH;
12239
12240 switch (skb->protocol) {
12241 case htons(ETH_P_IP):
12242 ops = &tcp_request_sock_ops;
12243 min_mss = 536;
12244 break;
12245 #if IS_BUILTIN(CONFIG_IPV6)
12246 case htons(ETH_P_IPV6):
12247 ops = &tcp6_request_sock_ops;
12248 min_mss = IPV6_MIN_MTU - 60;
12249 break;
12250 #endif
12251 default:
12252 return -EINVAL;
12253 }
12254
12255 if (sk->sk_type != SOCK_STREAM || sk->sk_state != TCP_LISTEN ||
12256 sk_is_mptcp(sk))
12257 return -EINVAL;
12258
12259 if (attrs->mss < min_mss)
12260 return -EINVAL;
12261
12262 if (attrs->wscale_ok) {
12263 if (!READ_ONCE(net->ipv4.sysctl_tcp_window_scaling))
12264 return -EINVAL;
12265
12266 if (attrs->snd_wscale > TCP_MAX_WSCALE ||
12267 attrs->rcv_wscale > TCP_MAX_WSCALE)
12268 return -EINVAL;
12269 }
12270
12271 if (attrs->sack_ok && !READ_ONCE(net->ipv4.sysctl_tcp_sack))
12272 return -EINVAL;
12273
12274 if (attrs->tstamp_ok) {
12275 if (!READ_ONCE(net->ipv4.sysctl_tcp_timestamps))
12276 return -EINVAL;
12277
12278 tsoff = attrs->rcv_tsecr - tcp_ns_to_ts(attrs->usec_ts_ok, tcp_clock_ns());
12279 }
12280
12281 req = inet_reqsk_alloc(ops, sk, false);
12282 if (!req)
12283 return -ENOMEM;
12284
12285 ireq = inet_rsk(req);
12286 treq = tcp_rsk(req);
12287
12288 req->rsk_listener = sk;
12289 req->syncookie = 1;
12290 req->mss = attrs->mss;
12291 req->ts_recent = attrs->rcv_tsval;
12292
12293 ireq->snd_wscale = attrs->snd_wscale;
12294 ireq->rcv_wscale = attrs->rcv_wscale;
12295 ireq->tstamp_ok = !!attrs->tstamp_ok;
12296 ireq->sack_ok = !!attrs->sack_ok;
12297 ireq->wscale_ok = !!attrs->wscale_ok;
12298 ireq->ecn_ok = !!attrs->ecn_ok;
12299
12300 treq->req_usec_ts = !!attrs->usec_ts_ok;
12301 treq->ts_off = tsoff;
12302
12303 skb_orphan(skb);
12304 skb->sk = req_to_sk(req);
12305 skb->destructor = sock_pfree;
12306
12307 return 0;
12308 #else
12309 return -EOPNOTSUPP;
12310 #endif
12311 }
12312
bpf_sock_ops_enable_tx_tstamp(struct bpf_sock_ops_kern * skops,u64 flags)12313 __bpf_kfunc int bpf_sock_ops_enable_tx_tstamp(struct bpf_sock_ops_kern *skops,
12314 u64 flags)
12315 {
12316 struct sk_buff *skb;
12317
12318 if (skops->op != BPF_SOCK_OPS_TSTAMP_SENDMSG_CB)
12319 return -EOPNOTSUPP;
12320
12321 if (flags)
12322 return -EINVAL;
12323
12324 skb = skops->skb;
12325 skb_shinfo(skb)->tx_flags |= SKBTX_BPF;
12326 TCP_SKB_CB(skb)->txstamp_ack |= TSTAMP_ACK_BPF;
12327 skb_shinfo(skb)->tskey = TCP_SKB_CB(skb)->seq + skb->len - 1;
12328
12329 return 0;
12330 }
12331
12332 /**
12333 * bpf_xdp_pull_data() - Pull in non-linear xdp data.
12334 * @x: &xdp_md associated with the XDP buffer
12335 * @len: length of data to be made directly accessible in the linear part
12336 *
12337 * Pull in data in case the XDP buffer associated with @x is non-linear and
12338 * not all @len are in the linear data area.
12339 *
12340 * Direct packet access allows reading and writing linear XDP data through
12341 * packet pointers (i.e., &xdp_md->data + offsets). The amount of data which
12342 * ends up in the linear part of the xdp_buff depends on the NIC and its
12343 * configuration. When a frag-capable XDP program wants to directly access
12344 * headers that may be in the non-linear area, call this kfunc to make sure
12345 * the data is available in the linear area. Alternatively, use dynptr or
12346 * bpf_xdp_{load,store}_bytes() to access data without pulling.
12347 *
12348 * This kfunc can also be used with bpf_xdp_adjust_head() to decapsulate
12349 * headers in the non-linear data area.
12350 *
12351 * A call to this kfunc may reduce headroom. If there is not enough tailroom
12352 * in the linear data area, metadata and data will be shifted down.
12353 *
12354 * A call to this kfunc is susceptible to change the buffer geometry.
12355 * Therefore, at load time, all checks on pointers previously done by the
12356 * verifier are invalidated and must be performed again, if the kfunc is used
12357 * in combination with direct packet access.
12358 *
12359 * Return:
12360 * * %0 - success
12361 * * %-EINVAL - invalid len
12362 */
bpf_xdp_pull_data(struct xdp_md * x,u32 len)12363 __bpf_kfunc int bpf_xdp_pull_data(struct xdp_md *x, u32 len)
12364 {
12365 struct xdp_buff *xdp = (struct xdp_buff *)x;
12366 struct skb_shared_info *sinfo = xdp_get_shared_info_from_buff(xdp);
12367 int i, delta, shift, headroom, tailroom, n_frags_free = 0;
12368 void *data_hard_end = xdp_data_hard_end(xdp);
12369 int data_len = xdp->data_end - xdp->data;
12370 void *start;
12371
12372 if (len <= data_len)
12373 return 0;
12374
12375 if (unlikely(len > xdp_get_buff_len(xdp)))
12376 return -EINVAL;
12377
12378 start = xdp_data_meta_unsupported(xdp) ? xdp->data : xdp->data_meta;
12379
12380 headroom = start - xdp->data_hard_start - sizeof(struct xdp_frame);
12381 tailroom = data_hard_end - xdp->data_end;
12382
12383 delta = len - data_len;
12384 if (unlikely(delta > tailroom + headroom))
12385 return -EINVAL;
12386
12387 shift = delta - tailroom;
12388 if (shift > 0) {
12389 memmove(start - shift, start, xdp->data_end - start);
12390
12391 xdp->data_meta -= shift;
12392 xdp->data -= shift;
12393 xdp->data_end -= shift;
12394 }
12395
12396 for (i = 0; i < sinfo->nr_frags && delta; i++) {
12397 skb_frag_t *frag = &sinfo->frags[i];
12398 u32 shrink = min_t(u32, delta, skb_frag_size(frag));
12399
12400 memcpy(xdp->data_end, skb_frag_address(frag), shrink);
12401
12402 xdp->data_end += shrink;
12403 sinfo->xdp_frags_size -= shrink;
12404 delta -= shrink;
12405 if (bpf_xdp_shrink_data(xdp, frag, shrink, false))
12406 n_frags_free++;
12407 }
12408
12409 if (unlikely(n_frags_free)) {
12410 memmove(sinfo->frags, sinfo->frags + n_frags_free,
12411 (sinfo->nr_frags - n_frags_free) * sizeof(skb_frag_t));
12412
12413 sinfo->nr_frags -= n_frags_free;
12414
12415 if (!sinfo->nr_frags) {
12416 xdp_buff_clear_frags_flag(xdp);
12417 xdp_buff_clear_frag_pfmemalloc(xdp);
12418 }
12419 }
12420
12421 return 0;
12422 }
12423
12424 __bpf_kfunc_end_defs();
12425
bpf_dynptr_from_skb_rdonly(struct __sk_buff * skb,u64 flags,struct bpf_dynptr * ptr__uninit)12426 int bpf_dynptr_from_skb_rdonly(struct __sk_buff *skb, u64 flags,
12427 struct bpf_dynptr *ptr__uninit)
12428 {
12429 struct bpf_dynptr_kern *ptr = (struct bpf_dynptr_kern *)ptr__uninit;
12430 int err;
12431
12432 err = bpf_dynptr_from_skb(skb, flags, ptr__uninit);
12433 if (err)
12434 return err;
12435
12436 bpf_dynptr_set_rdonly(ptr);
12437
12438 return 0;
12439 }
12440
12441 BTF_KFUNCS_START(bpf_kfunc_check_set_skb)
12442 BTF_ID_FLAGS(func, bpf_dynptr_from_skb)
12443 BTF_KFUNCS_END(bpf_kfunc_check_set_skb)
12444
12445 BTF_KFUNCS_START(bpf_kfunc_check_set_skb_meta)
12446 BTF_ID_FLAGS(func, bpf_dynptr_from_skb_meta)
12447 BTF_KFUNCS_END(bpf_kfunc_check_set_skb_meta)
12448
12449 BTF_KFUNCS_START(bpf_kfunc_check_set_xdp)
12450 BTF_ID_FLAGS(func, bpf_dynptr_from_xdp)
12451 BTF_ID_FLAGS(func, bpf_xdp_pull_data)
12452 BTF_KFUNCS_END(bpf_kfunc_check_set_xdp)
12453
12454 BTF_KFUNCS_START(bpf_kfunc_check_set_sock_addr)
12455 BTF_ID_FLAGS(func, bpf_sock_addr_set_sun_path)
12456 BTF_KFUNCS_END(bpf_kfunc_check_set_sock_addr)
12457
12458 BTF_KFUNCS_START(bpf_kfunc_check_set_tcp_reqsk)
12459 BTF_ID_FLAGS(func, bpf_sk_assign_tcp_reqsk)
12460 BTF_KFUNCS_END(bpf_kfunc_check_set_tcp_reqsk)
12461
12462 BTF_KFUNCS_START(bpf_kfunc_check_set_sock_ops)
12463 BTF_ID_FLAGS(func, bpf_sock_ops_enable_tx_tstamp)
12464 BTF_KFUNCS_END(bpf_kfunc_check_set_sock_ops)
12465
12466 static const struct btf_kfunc_id_set bpf_kfunc_set_skb = {
12467 .owner = THIS_MODULE,
12468 .set = &bpf_kfunc_check_set_skb,
12469 };
12470
12471 static const struct btf_kfunc_id_set bpf_kfunc_set_skb_meta = {
12472 .owner = THIS_MODULE,
12473 .set = &bpf_kfunc_check_set_skb_meta,
12474 };
12475
12476 static const struct btf_kfunc_id_set bpf_kfunc_set_xdp = {
12477 .owner = THIS_MODULE,
12478 .set = &bpf_kfunc_check_set_xdp,
12479 };
12480
12481 static const struct btf_kfunc_id_set bpf_kfunc_set_sock_addr = {
12482 .owner = THIS_MODULE,
12483 .set = &bpf_kfunc_check_set_sock_addr,
12484 };
12485
12486 static const struct btf_kfunc_id_set bpf_kfunc_set_tcp_reqsk = {
12487 .owner = THIS_MODULE,
12488 .set = &bpf_kfunc_check_set_tcp_reqsk,
12489 };
12490
12491 static const struct btf_kfunc_id_set bpf_kfunc_set_sock_ops = {
12492 .owner = THIS_MODULE,
12493 .set = &bpf_kfunc_check_set_sock_ops,
12494 };
12495
bpf_kfunc_init(void)12496 static int __init bpf_kfunc_init(void)
12497 {
12498 int ret;
12499
12500 ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_CLS, &bpf_kfunc_set_skb);
12501 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_ACT, &bpf_kfunc_set_skb);
12502 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SK_SKB, &bpf_kfunc_set_skb);
12503 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SOCKET_FILTER, &bpf_kfunc_set_skb);
12504 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_CGROUP_SKB, &bpf_kfunc_set_skb);
12505 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_LWT_OUT, &bpf_kfunc_set_skb);
12506 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_LWT_IN, &bpf_kfunc_set_skb);
12507 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_LWT_XMIT, &bpf_kfunc_set_skb);
12508 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_LWT_SEG6LOCAL, &bpf_kfunc_set_skb);
12509 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_NETFILTER, &bpf_kfunc_set_skb);
12510 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, &bpf_kfunc_set_skb);
12511 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_CLS, &bpf_kfunc_set_skb_meta);
12512 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_ACT, &bpf_kfunc_set_skb_meta);
12513 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_XDP, &bpf_kfunc_set_xdp);
12514 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
12515 &bpf_kfunc_set_sock_addr);
12516 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_CLS, &bpf_kfunc_set_tcp_reqsk);
12517 return ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SOCK_OPS, &bpf_kfunc_set_sock_ops);
12518 }
12519 late_initcall(bpf_kfunc_init);
12520
12521 __bpf_kfunc_start_defs();
12522
12523 /* bpf_sock_destroy: Destroy the given socket with ECONNABORTED error code.
12524 *
12525 * The function expects a non-NULL pointer to a socket, and invokes the
12526 * protocol specific socket destroy handlers.
12527 *
12528 * The helper can only be called from BPF contexts that have acquired the socket
12529 * locks.
12530 *
12531 * Parameters:
12532 * @sock: Pointer to socket to be destroyed
12533 *
12534 * Return:
12535 * On error, may return EPROTONOSUPPORT, EINVAL.
12536 * EPROTONOSUPPORT if protocol specific destroy handler is not supported.
12537 * 0 otherwise
12538 */
bpf_sock_destroy(struct sock_common * sock)12539 __bpf_kfunc int bpf_sock_destroy(struct sock_common *sock)
12540 {
12541 struct sock *sk = (struct sock *)sock;
12542
12543 /* The locking semantics that allow for synchronous execution of the
12544 * destroy handlers are only supported for TCP and UDP.
12545 * Supporting protocols will need to acquire sock lock in the BPF context
12546 * prior to invoking this kfunc.
12547 */
12548 if (!sk->sk_prot->diag_destroy || (sk->sk_protocol != IPPROTO_TCP &&
12549 sk->sk_protocol != IPPROTO_UDP))
12550 return -EOPNOTSUPP;
12551
12552 return sk->sk_prot->diag_destroy(sk, ECONNABORTED);
12553 }
12554
12555 __bpf_kfunc_end_defs();
12556
12557 BTF_KFUNCS_START(bpf_sk_iter_kfunc_ids)
BTF_ID_FLAGS(func,bpf_sock_destroy)12558 BTF_ID_FLAGS(func, bpf_sock_destroy)
12559 BTF_KFUNCS_END(bpf_sk_iter_kfunc_ids)
12560
12561 static int tracing_iter_filter(const struct bpf_prog *prog, u32 kfunc_id)
12562 {
12563 if (btf_id_set8_contains(&bpf_sk_iter_kfunc_ids, kfunc_id) &&
12564 prog->expected_attach_type != BPF_TRACE_ITER)
12565 return -EACCES;
12566 return 0;
12567 }
12568
12569 static const struct btf_kfunc_id_set bpf_sk_iter_kfunc_set = {
12570 .owner = THIS_MODULE,
12571 .set = &bpf_sk_iter_kfunc_ids,
12572 .filter = tracing_iter_filter,
12573 };
12574
init_subsystem(void)12575 static int init_subsystem(void)
12576 {
12577 return register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, &bpf_sk_iter_kfunc_set);
12578 }
12579 late_initcall(init_subsystem);
12580