1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Linux Socket Filter - Kernel level socket filtering
5 * Based on the design of the Berkeley Packet Filter. The new
6 * internal format has been designed by PLUMgrid:
8 * Copyright (c) 2011 - 2014 PLUMgrid, http://plumgrid.com
12 * Jay Schulist <jschlst@samba.org>
13 * Alexei Starovoitov <ast@plumgrid.com>
14 * Daniel Borkmann <dborkman@redhat.com>
16 * Andi Kleen - Fix a few bad bugs and races.
17 * Kris Katterjohn - Added many additional checks in bpf_check_classic()
20 #include <linux/atomic.h>
21 #include <linux/bpf_verifier.h>
22 #include <linux/module.h>
23 #include <linux/types.h>
25 #include <linux/fcntl.h>
26 #include <linux/socket.h>
27 #include <linux/sock_diag.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>
36 #include <net/protocol.h>
37 #include <net/netlink.h>
38 #include <linux/skbuff.h>
39 #include <linux/skmsg.h>
41 #include <net/flow_dissector.h>
42 #include <linux/errno.h>
43 #include <linux/timer.h>
44 #include <linux/uaccess.h>
45 #include <asm/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>
56 #include <net/sock_reuseport.h>
57 #include <net/busy_poll.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>
71 #include <net/net_namespace.h>
72 #include <linux/seg6_local.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>
82 #include <net/mptcp.h>
84 static const struct bpf_func_proto *
85 bpf_sk_base_func_proto(enum bpf_func_id func_id);
87 int copy_bpf_fprog_from_user(struct sock_fprog *dst, sockptr_t src, int len)
89 if (in_compat_syscall()) {
90 struct compat_sock_fprog f32;
92 if (len != sizeof(f32))
94 if (copy_from_sockptr(&f32, src, sizeof(f32)))
96 memset(dst, 0, sizeof(*dst));
98 dst->filter = compat_ptr(f32.filter);
100 if (len != sizeof(*dst))
102 if (copy_from_sockptr(dst, src, sizeof(*dst)))
108 EXPORT_SYMBOL_GPL(copy_bpf_fprog_from_user);
111 * sk_filter_trim_cap - run a packet through a socket filter
112 * @sk: sock associated with &sk_buff
113 * @skb: buffer to filter
114 * @cap: limit on how short the eBPF program may trim the packet
116 * Run the eBPF program and then cut skb->data to correct size returned by
117 * the program. If pkt_len is 0 we toss packet. If skb->len is smaller
118 * than pkt_len we keep whole skb->data. This is the socket level
119 * wrapper to bpf_prog_run. It returns 0 if the packet should
120 * be accepted or -EPERM if the packet should be tossed.
123 int sk_filter_trim_cap(struct sock *sk, struct sk_buff *skb, unsigned int cap)
126 struct sk_filter *filter;
129 * If the skb was allocated from pfmemalloc reserves, only
130 * allow SOCK_MEMALLOC sockets to use it as this socket is
131 * helping free memory
133 if (skb_pfmemalloc(skb) && !sock_flag(sk, SOCK_MEMALLOC)) {
134 NET_INC_STATS(sock_net(sk), LINUX_MIB_PFMEMALLOCDROP);
137 err = BPF_CGROUP_RUN_PROG_INET_INGRESS(sk, skb);
141 err = security_sock_rcv_skb(sk, skb);
146 filter = rcu_dereference(sk->sk_filter);
148 struct sock *save_sk = skb->sk;
149 unsigned int pkt_len;
152 pkt_len = bpf_prog_run_save_cb(filter->prog, skb);
154 err = pkt_len ? pskb_trim(skb, max(cap, pkt_len)) : -EPERM;
160 EXPORT_SYMBOL(sk_filter_trim_cap);
162 BPF_CALL_1(bpf_skb_get_pay_offset, struct sk_buff *, skb)
164 return skb_get_poff(skb);
167 BPF_CALL_3(bpf_skb_get_nlattr, struct sk_buff *, skb, u32, a, u32, x)
171 if (skb_is_nonlinear(skb))
174 if (skb->len < sizeof(struct nlattr))
177 if (a > skb->len - sizeof(struct nlattr))
180 nla = nla_find((struct nlattr *) &skb->data[a], skb->len - a, x);
182 return (void *) nla - (void *) skb->data;
187 BPF_CALL_3(bpf_skb_get_nlattr_nest, struct sk_buff *, skb, u32, a, u32, x)
191 if (skb_is_nonlinear(skb))
194 if (skb->len < sizeof(struct nlattr))
197 if (a > skb->len - sizeof(struct nlattr))
200 nla = (struct nlattr *) &skb->data[a];
201 if (nla->nla_len > skb->len - a)
204 nla = nla_find_nested(nla, x);
206 return (void *) nla - (void *) skb->data;
211 BPF_CALL_4(bpf_skb_load_helper_8, const struct sk_buff *, skb, const void *,
212 data, int, headlen, int, offset)
215 const int len = sizeof(tmp);
218 if (headlen - offset >= len)
219 return *(u8 *)(data + offset);
220 if (!skb_copy_bits(skb, offset, &tmp, sizeof(tmp)))
223 ptr = bpf_internal_load_pointer_neg_helper(skb, offset, len);
231 BPF_CALL_2(bpf_skb_load_helper_8_no_cache, const struct sk_buff *, skb,
234 return ____bpf_skb_load_helper_8(skb, skb->data, skb->len - skb->data_len,
238 BPF_CALL_4(bpf_skb_load_helper_16, const struct sk_buff *, skb, const void *,
239 data, int, headlen, int, offset)
242 const int len = sizeof(tmp);
245 if (headlen - offset >= len)
246 return get_unaligned_be16(data + offset);
247 if (!skb_copy_bits(skb, offset, &tmp, sizeof(tmp)))
248 return be16_to_cpu(tmp);
250 ptr = bpf_internal_load_pointer_neg_helper(skb, offset, len);
252 return get_unaligned_be16(ptr);
258 BPF_CALL_2(bpf_skb_load_helper_16_no_cache, const struct sk_buff *, skb,
261 return ____bpf_skb_load_helper_16(skb, skb->data, skb->len - skb->data_len,
265 BPF_CALL_4(bpf_skb_load_helper_32, const struct sk_buff *, skb, const void *,
266 data, int, headlen, int, offset)
269 const int len = sizeof(tmp);
271 if (likely(offset >= 0)) {
272 if (headlen - offset >= len)
273 return get_unaligned_be32(data + offset);
274 if (!skb_copy_bits(skb, offset, &tmp, sizeof(tmp)))
275 return be32_to_cpu(tmp);
277 ptr = bpf_internal_load_pointer_neg_helper(skb, offset, len);
279 return get_unaligned_be32(ptr);
285 BPF_CALL_2(bpf_skb_load_helper_32_no_cache, const struct sk_buff *, skb,
288 return ____bpf_skb_load_helper_32(skb, skb->data, skb->len - skb->data_len,
292 static u32 convert_skb_access(int skb_field, int dst_reg, int src_reg,
293 struct bpf_insn *insn_buf)
295 struct bpf_insn *insn = insn_buf;
299 BUILD_BUG_ON(sizeof_field(struct sk_buff, mark) != 4);
301 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
302 offsetof(struct sk_buff, mark));
306 *insn++ = BPF_LDX_MEM(BPF_B, dst_reg, src_reg, PKT_TYPE_OFFSET);
307 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, PKT_TYPE_MAX);
308 #ifdef __BIG_ENDIAN_BITFIELD
309 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 5);
314 BUILD_BUG_ON(sizeof_field(struct sk_buff, queue_mapping) != 2);
316 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
317 offsetof(struct sk_buff, queue_mapping));
320 case SKF_AD_VLAN_TAG:
321 BUILD_BUG_ON(sizeof_field(struct sk_buff, vlan_tci) != 2);
323 /* dst_reg = *(u16 *) (src_reg + offsetof(vlan_tci)) */
324 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
325 offsetof(struct sk_buff, vlan_tci));
327 case SKF_AD_VLAN_TAG_PRESENT:
328 BUILD_BUG_ON(sizeof_field(struct sk_buff, vlan_all) != 4);
329 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
330 offsetof(struct sk_buff, vlan_all));
331 *insn++ = BPF_JMP_IMM(BPF_JEQ, dst_reg, 0, 1);
332 *insn++ = BPF_ALU32_IMM(BPF_MOV, dst_reg, 1);
336 return insn - insn_buf;
339 static bool convert_bpf_extensions(struct sock_filter *fp,
340 struct bpf_insn **insnp)
342 struct bpf_insn *insn = *insnp;
346 case SKF_AD_OFF + SKF_AD_PROTOCOL:
347 BUILD_BUG_ON(sizeof_field(struct sk_buff, protocol) != 2);
349 /* A = *(u16 *) (CTX + offsetof(protocol)) */
350 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
351 offsetof(struct sk_buff, protocol));
352 /* A = ntohs(A) [emitting a nop or swap16] */
353 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
356 case SKF_AD_OFF + SKF_AD_PKTTYPE:
357 cnt = convert_skb_access(SKF_AD_PKTTYPE, BPF_REG_A, BPF_REG_CTX, insn);
361 case SKF_AD_OFF + SKF_AD_IFINDEX:
362 case SKF_AD_OFF + SKF_AD_HATYPE:
363 BUILD_BUG_ON(sizeof_field(struct net_device, ifindex) != 4);
364 BUILD_BUG_ON(sizeof_field(struct net_device, type) != 2);
366 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
367 BPF_REG_TMP, BPF_REG_CTX,
368 offsetof(struct sk_buff, dev));
369 /* if (tmp != 0) goto pc + 1 */
370 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_TMP, 0, 1);
371 *insn++ = BPF_EXIT_INSN();
372 if (fp->k == SKF_AD_OFF + SKF_AD_IFINDEX)
373 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_TMP,
374 offsetof(struct net_device, ifindex));
376 *insn = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_TMP,
377 offsetof(struct net_device, type));
380 case SKF_AD_OFF + SKF_AD_MARK:
381 cnt = convert_skb_access(SKF_AD_MARK, BPF_REG_A, BPF_REG_CTX, insn);
385 case SKF_AD_OFF + SKF_AD_RXHASH:
386 BUILD_BUG_ON(sizeof_field(struct sk_buff, hash) != 4);
388 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX,
389 offsetof(struct sk_buff, hash));
392 case SKF_AD_OFF + SKF_AD_QUEUE:
393 cnt = convert_skb_access(SKF_AD_QUEUE, BPF_REG_A, BPF_REG_CTX, insn);
397 case SKF_AD_OFF + SKF_AD_VLAN_TAG:
398 cnt = convert_skb_access(SKF_AD_VLAN_TAG,
399 BPF_REG_A, BPF_REG_CTX, insn);
403 case SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT:
404 cnt = convert_skb_access(SKF_AD_VLAN_TAG_PRESENT,
405 BPF_REG_A, BPF_REG_CTX, insn);
409 case SKF_AD_OFF + SKF_AD_VLAN_TPID:
410 BUILD_BUG_ON(sizeof_field(struct sk_buff, vlan_proto) != 2);
412 /* A = *(u16 *) (CTX + offsetof(vlan_proto)) */
413 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
414 offsetof(struct sk_buff, vlan_proto));
415 /* A = ntohs(A) [emitting a nop or swap16] */
416 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
419 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
420 case SKF_AD_OFF + SKF_AD_NLATTR:
421 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
422 case SKF_AD_OFF + SKF_AD_CPU:
423 case SKF_AD_OFF + SKF_AD_RANDOM:
425 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX);
427 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_A);
429 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_X);
430 /* Emit call(arg1=CTX, arg2=A, arg3=X) */
432 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
433 *insn = BPF_EMIT_CALL(bpf_skb_get_pay_offset);
435 case SKF_AD_OFF + SKF_AD_NLATTR:
436 *insn = BPF_EMIT_CALL(bpf_skb_get_nlattr);
438 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
439 *insn = BPF_EMIT_CALL(bpf_skb_get_nlattr_nest);
441 case SKF_AD_OFF + SKF_AD_CPU:
442 *insn = BPF_EMIT_CALL(bpf_get_raw_cpu_id);
444 case SKF_AD_OFF + SKF_AD_RANDOM:
445 *insn = BPF_EMIT_CALL(bpf_user_rnd_u32);
446 bpf_user_rnd_init_once();
451 case SKF_AD_OFF + SKF_AD_ALU_XOR_X:
453 *insn = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_X);
457 /* This is just a dummy call to avoid letting the compiler
458 * evict __bpf_call_base() as an optimization. Placed here
459 * where no-one bothers.
461 BUG_ON(__bpf_call_base(0, 0, 0, 0, 0) != 0);
469 static bool convert_bpf_ld_abs(struct sock_filter *fp, struct bpf_insn **insnp)
471 const bool unaligned_ok = IS_BUILTIN(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS);
472 int size = bpf_size_to_bytes(BPF_SIZE(fp->code));
473 bool endian = BPF_SIZE(fp->code) == BPF_H ||
474 BPF_SIZE(fp->code) == BPF_W;
475 bool indirect = BPF_MODE(fp->code) == BPF_IND;
476 const int ip_align = NET_IP_ALIGN;
477 struct bpf_insn *insn = *insnp;
481 ((unaligned_ok && offset >= 0) ||
482 (!unaligned_ok && offset >= 0 &&
483 offset + ip_align >= 0 &&
484 offset + ip_align % size == 0))) {
485 bool ldx_off_ok = offset <= S16_MAX;
487 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_H);
489 *insn++ = BPF_ALU64_IMM(BPF_SUB, BPF_REG_TMP, offset);
490 *insn++ = BPF_JMP_IMM(BPF_JSLT, BPF_REG_TMP,
491 size, 2 + endian + (!ldx_off_ok * 2));
493 *insn++ = BPF_LDX_MEM(BPF_SIZE(fp->code), BPF_REG_A,
496 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_D);
497 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_TMP, offset);
498 *insn++ = BPF_LDX_MEM(BPF_SIZE(fp->code), BPF_REG_A,
502 *insn++ = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, size * 8);
503 *insn++ = BPF_JMP_A(8);
506 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX);
507 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_D);
508 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_H);
510 *insn++ = BPF_MOV64_IMM(BPF_REG_ARG4, offset);
512 *insn++ = BPF_MOV64_REG(BPF_REG_ARG4, BPF_REG_X);
514 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_ARG4, offset);
517 switch (BPF_SIZE(fp->code)) {
519 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_8);
522 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_16);
525 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_32);
531 *insn++ = BPF_JMP_IMM(BPF_JSGE, BPF_REG_A, 0, 2);
532 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
533 *insn = BPF_EXIT_INSN();
540 * bpf_convert_filter - convert filter program
541 * @prog: the user passed filter program
542 * @len: the length of the user passed filter program
543 * @new_prog: allocated 'struct bpf_prog' or NULL
544 * @new_len: pointer to store length of converted program
545 * @seen_ld_abs: bool whether we've seen ld_abs/ind
547 * Remap 'sock_filter' style classic BPF (cBPF) instruction set to 'bpf_insn'
548 * style extended BPF (eBPF).
549 * Conversion workflow:
551 * 1) First pass for calculating the new program length:
552 * bpf_convert_filter(old_prog, old_len, NULL, &new_len, &seen_ld_abs)
554 * 2) 2nd pass to remap in two passes: 1st pass finds new
555 * jump offsets, 2nd pass remapping:
556 * bpf_convert_filter(old_prog, old_len, new_prog, &new_len, &seen_ld_abs)
558 static int bpf_convert_filter(struct sock_filter *prog, int len,
559 struct bpf_prog *new_prog, int *new_len,
562 int new_flen = 0, pass = 0, target, i, stack_off;
563 struct bpf_insn *new_insn, *first_insn = NULL;
564 struct sock_filter *fp;
568 BUILD_BUG_ON(BPF_MEMWORDS * sizeof(u32) > MAX_BPF_STACK);
569 BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG);
571 if (len <= 0 || len > BPF_MAXINSNS)
575 first_insn = new_prog->insnsi;
576 addrs = kcalloc(len, sizeof(*addrs),
577 GFP_KERNEL | __GFP_NOWARN);
583 new_insn = first_insn;
586 /* Classic BPF related prologue emission. */
588 /* Classic BPF expects A and X to be reset first. These need
589 * to be guaranteed to be the first two instructions.
591 *new_insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
592 *new_insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_X, BPF_REG_X);
594 /* All programs must keep CTX in callee saved BPF_REG_CTX.
595 * In eBPF case it's done by the compiler, here we need to
596 * do this ourself. Initial CTX is present in BPF_REG_ARG1.
598 *new_insn++ = BPF_MOV64_REG(BPF_REG_CTX, BPF_REG_ARG1);
600 /* For packet access in classic BPF, cache skb->data
601 * in callee-saved BPF R8 and skb->len - skb->data_len
602 * (headlen) in BPF R9. Since classic BPF is read-only
603 * on CTX, we only need to cache it once.
605 *new_insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
606 BPF_REG_D, BPF_REG_CTX,
607 offsetof(struct sk_buff, data));
608 *new_insn++ = BPF_LDX_MEM(BPF_W, BPF_REG_H, BPF_REG_CTX,
609 offsetof(struct sk_buff, len));
610 *new_insn++ = BPF_LDX_MEM(BPF_W, BPF_REG_TMP, BPF_REG_CTX,
611 offsetof(struct sk_buff, data_len));
612 *new_insn++ = BPF_ALU32_REG(BPF_SUB, BPF_REG_H, BPF_REG_TMP);
618 for (i = 0; i < len; fp++, i++) {
619 struct bpf_insn tmp_insns[32] = { };
620 struct bpf_insn *insn = tmp_insns;
623 addrs[i] = new_insn - first_insn;
626 /* All arithmetic insns and skb loads map as-is. */
627 case BPF_ALU | BPF_ADD | BPF_X:
628 case BPF_ALU | BPF_ADD | BPF_K:
629 case BPF_ALU | BPF_SUB | BPF_X:
630 case BPF_ALU | BPF_SUB | BPF_K:
631 case BPF_ALU | BPF_AND | BPF_X:
632 case BPF_ALU | BPF_AND | BPF_K:
633 case BPF_ALU | BPF_OR | BPF_X:
634 case BPF_ALU | BPF_OR | BPF_K:
635 case BPF_ALU | BPF_LSH | BPF_X:
636 case BPF_ALU | BPF_LSH | BPF_K:
637 case BPF_ALU | BPF_RSH | BPF_X:
638 case BPF_ALU | BPF_RSH | BPF_K:
639 case BPF_ALU | BPF_XOR | BPF_X:
640 case BPF_ALU | BPF_XOR | BPF_K:
641 case BPF_ALU | BPF_MUL | BPF_X:
642 case BPF_ALU | BPF_MUL | BPF_K:
643 case BPF_ALU | BPF_DIV | BPF_X:
644 case BPF_ALU | BPF_DIV | BPF_K:
645 case BPF_ALU | BPF_MOD | BPF_X:
646 case BPF_ALU | BPF_MOD | BPF_K:
647 case BPF_ALU | BPF_NEG:
648 case BPF_LD | BPF_ABS | BPF_W:
649 case BPF_LD | BPF_ABS | BPF_H:
650 case BPF_LD | BPF_ABS | BPF_B:
651 case BPF_LD | BPF_IND | BPF_W:
652 case BPF_LD | BPF_IND | BPF_H:
653 case BPF_LD | BPF_IND | BPF_B:
654 /* Check for overloaded BPF extension and
655 * directly convert it if found, otherwise
656 * just move on with mapping.
658 if (BPF_CLASS(fp->code) == BPF_LD &&
659 BPF_MODE(fp->code) == BPF_ABS &&
660 convert_bpf_extensions(fp, &insn))
662 if (BPF_CLASS(fp->code) == BPF_LD &&
663 convert_bpf_ld_abs(fp, &insn)) {
668 if (fp->code == (BPF_ALU | BPF_DIV | BPF_X) ||
669 fp->code == (BPF_ALU | BPF_MOD | BPF_X)) {
670 *insn++ = BPF_MOV32_REG(BPF_REG_X, BPF_REG_X);
671 /* Error with exception code on div/mod by 0.
672 * For cBPF programs, this was always return 0.
674 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_X, 0, 2);
675 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
676 *insn++ = BPF_EXIT_INSN();
679 *insn = BPF_RAW_INSN(fp->code, BPF_REG_A, BPF_REG_X, 0, fp->k);
682 /* Jump transformation cannot use BPF block macros
683 * everywhere as offset calculation and target updates
684 * require a bit more work than the rest, i.e. jump
685 * opcodes map as-is, but offsets need adjustment.
688 #define BPF_EMIT_JMP \
690 const s32 off_min = S16_MIN, off_max = S16_MAX; \
693 if (target >= len || target < 0) \
695 off = addrs ? addrs[target] - addrs[i] - 1 : 0; \
696 /* Adjust pc relative offset for 2nd or 3rd insn. */ \
697 off -= insn - tmp_insns; \
698 /* Reject anything not fitting into insn->off. */ \
699 if (off < off_min || off > off_max) \
704 case BPF_JMP | BPF_JA:
705 target = i + fp->k + 1;
706 insn->code = fp->code;
710 case BPF_JMP | BPF_JEQ | BPF_K:
711 case BPF_JMP | BPF_JEQ | BPF_X:
712 case BPF_JMP | BPF_JSET | BPF_K:
713 case BPF_JMP | BPF_JSET | BPF_X:
714 case BPF_JMP | BPF_JGT | BPF_K:
715 case BPF_JMP | BPF_JGT | BPF_X:
716 case BPF_JMP | BPF_JGE | BPF_K:
717 case BPF_JMP | BPF_JGE | BPF_X:
718 if (BPF_SRC(fp->code) == BPF_K && (int) fp->k < 0) {
719 /* BPF immediates are signed, zero extend
720 * immediate into tmp register and use it
723 *insn++ = BPF_MOV32_IMM(BPF_REG_TMP, fp->k);
725 insn->dst_reg = BPF_REG_A;
726 insn->src_reg = BPF_REG_TMP;
729 insn->dst_reg = BPF_REG_A;
731 bpf_src = BPF_SRC(fp->code);
732 insn->src_reg = bpf_src == BPF_X ? BPF_REG_X : 0;
735 /* Common case where 'jump_false' is next insn. */
737 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
738 target = i + fp->jt + 1;
743 /* Convert some jumps when 'jump_true' is next insn. */
745 switch (BPF_OP(fp->code)) {
747 insn->code = BPF_JMP | BPF_JNE | bpf_src;
750 insn->code = BPF_JMP | BPF_JLE | bpf_src;
753 insn->code = BPF_JMP | BPF_JLT | bpf_src;
759 target = i + fp->jf + 1;
764 /* Other jumps are mapped into two insns: Jxx and JA. */
765 target = i + fp->jt + 1;
766 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
770 insn->code = BPF_JMP | BPF_JA;
771 target = i + fp->jf + 1;
775 /* ldxb 4 * ([14] & 0xf) is remaped into 6 insns. */
776 case BPF_LDX | BPF_MSH | BPF_B: {
777 struct sock_filter tmp = {
778 .code = BPF_LD | BPF_ABS | BPF_B,
785 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
786 /* A = BPF_R0 = *(u8 *) (skb->data + K) */
787 convert_bpf_ld_abs(&tmp, &insn);
790 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, 0xf);
792 *insn++ = BPF_ALU32_IMM(BPF_LSH, BPF_REG_A, 2);
794 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_X);
796 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
798 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_TMP);
801 /* RET_K is remaped into 2 insns. RET_A case doesn't need an
802 * extra mov as BPF_REG_0 is already mapped into BPF_REG_A.
804 case BPF_RET | BPF_A:
805 case BPF_RET | BPF_K:
806 if (BPF_RVAL(fp->code) == BPF_K)
807 *insn++ = BPF_MOV32_RAW(BPF_K, BPF_REG_0,
809 *insn = BPF_EXIT_INSN();
812 /* Store to stack. */
815 stack_off = fp->k * 4 + 4;
816 *insn = BPF_STX_MEM(BPF_W, BPF_REG_FP, BPF_CLASS(fp->code) ==
817 BPF_ST ? BPF_REG_A : BPF_REG_X,
819 /* check_load_and_stores() verifies that classic BPF can
820 * load from stack only after write, so tracking
821 * stack_depth for ST|STX insns is enough
823 if (new_prog && new_prog->aux->stack_depth < stack_off)
824 new_prog->aux->stack_depth = stack_off;
827 /* Load from stack. */
828 case BPF_LD | BPF_MEM:
829 case BPF_LDX | BPF_MEM:
830 stack_off = fp->k * 4 + 4;
831 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
832 BPF_REG_A : BPF_REG_X, BPF_REG_FP,
837 case BPF_LD | BPF_IMM:
838 case BPF_LDX | BPF_IMM:
839 *insn = BPF_MOV32_IMM(BPF_CLASS(fp->code) == BPF_LD ?
840 BPF_REG_A : BPF_REG_X, fp->k);
844 case BPF_MISC | BPF_TAX:
845 *insn = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
849 case BPF_MISC | BPF_TXA:
850 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_X);
853 /* A = skb->len or X = skb->len */
854 case BPF_LD | BPF_W | BPF_LEN:
855 case BPF_LDX | BPF_W | BPF_LEN:
856 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
857 BPF_REG_A : BPF_REG_X, BPF_REG_CTX,
858 offsetof(struct sk_buff, len));
861 /* Access seccomp_data fields. */
862 case BPF_LDX | BPF_ABS | BPF_W:
863 /* A = *(u32 *) (ctx + K) */
864 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX, fp->k);
867 /* Unknown instruction. */
874 memcpy(new_insn, tmp_insns,
875 sizeof(*insn) * (insn - tmp_insns));
876 new_insn += insn - tmp_insns;
880 /* Only calculating new length. */
881 *new_len = new_insn - first_insn;
883 *new_len += 4; /* Prologue bits. */
888 if (new_flen != new_insn - first_insn) {
889 new_flen = new_insn - first_insn;
896 BUG_ON(*new_len != new_flen);
905 * As we dont want to clear mem[] array for each packet going through
906 * __bpf_prog_run(), we check that filter loaded by user never try to read
907 * a cell if not previously written, and we check all branches to be sure
908 * a malicious user doesn't try to abuse us.
910 static int check_load_and_stores(const struct sock_filter *filter, int flen)
912 u16 *masks, memvalid = 0; /* One bit per cell, 16 cells */
915 BUILD_BUG_ON(BPF_MEMWORDS > 16);
917 masks = kmalloc_array(flen, sizeof(*masks), GFP_KERNEL);
921 memset(masks, 0xff, flen * sizeof(*masks));
923 for (pc = 0; pc < flen; pc++) {
924 memvalid &= masks[pc];
926 switch (filter[pc].code) {
929 memvalid |= (1 << filter[pc].k);
931 case BPF_LD | BPF_MEM:
932 case BPF_LDX | BPF_MEM:
933 if (!(memvalid & (1 << filter[pc].k))) {
938 case BPF_JMP | BPF_JA:
939 /* A jump must set masks on target */
940 masks[pc + 1 + filter[pc].k] &= memvalid;
943 case BPF_JMP | BPF_JEQ | BPF_K:
944 case BPF_JMP | BPF_JEQ | BPF_X:
945 case BPF_JMP | BPF_JGE | BPF_K:
946 case BPF_JMP | BPF_JGE | BPF_X:
947 case BPF_JMP | BPF_JGT | BPF_K:
948 case BPF_JMP | BPF_JGT | BPF_X:
949 case BPF_JMP | BPF_JSET | BPF_K:
950 case BPF_JMP | BPF_JSET | BPF_X:
951 /* A jump must set masks on targets */
952 masks[pc + 1 + filter[pc].jt] &= memvalid;
953 masks[pc + 1 + filter[pc].jf] &= memvalid;
963 static bool chk_code_allowed(u16 code_to_probe)
965 static const bool codes[] = {
966 /* 32 bit ALU operations */
967 [BPF_ALU | BPF_ADD | BPF_K] = true,
968 [BPF_ALU | BPF_ADD | BPF_X] = true,
969 [BPF_ALU | BPF_SUB | BPF_K] = true,
970 [BPF_ALU | BPF_SUB | BPF_X] = true,
971 [BPF_ALU | BPF_MUL | BPF_K] = true,
972 [BPF_ALU | BPF_MUL | BPF_X] = true,
973 [BPF_ALU | BPF_DIV | BPF_K] = true,
974 [BPF_ALU | BPF_DIV | BPF_X] = true,
975 [BPF_ALU | BPF_MOD | BPF_K] = true,
976 [BPF_ALU | BPF_MOD | BPF_X] = true,
977 [BPF_ALU | BPF_AND | BPF_K] = true,
978 [BPF_ALU | BPF_AND | BPF_X] = true,
979 [BPF_ALU | BPF_OR | BPF_K] = true,
980 [BPF_ALU | BPF_OR | BPF_X] = true,
981 [BPF_ALU | BPF_XOR | BPF_K] = true,
982 [BPF_ALU | BPF_XOR | BPF_X] = true,
983 [BPF_ALU | BPF_LSH | BPF_K] = true,
984 [BPF_ALU | BPF_LSH | BPF_X] = true,
985 [BPF_ALU | BPF_RSH | BPF_K] = true,
986 [BPF_ALU | BPF_RSH | BPF_X] = true,
987 [BPF_ALU | BPF_NEG] = true,
988 /* Load instructions */
989 [BPF_LD | BPF_W | BPF_ABS] = true,
990 [BPF_LD | BPF_H | BPF_ABS] = true,
991 [BPF_LD | BPF_B | BPF_ABS] = true,
992 [BPF_LD | BPF_W | BPF_LEN] = true,
993 [BPF_LD | BPF_W | BPF_IND] = true,
994 [BPF_LD | BPF_H | BPF_IND] = true,
995 [BPF_LD | BPF_B | BPF_IND] = true,
996 [BPF_LD | BPF_IMM] = true,
997 [BPF_LD | BPF_MEM] = true,
998 [BPF_LDX | BPF_W | BPF_LEN] = true,
999 [BPF_LDX | BPF_B | BPF_MSH] = true,
1000 [BPF_LDX | BPF_IMM] = true,
1001 [BPF_LDX | BPF_MEM] = true,
1002 /* Store instructions */
1005 /* Misc instructions */
1006 [BPF_MISC | BPF_TAX] = true,
1007 [BPF_MISC | BPF_TXA] = true,
1008 /* Return instructions */
1009 [BPF_RET | BPF_K] = true,
1010 [BPF_RET | BPF_A] = true,
1011 /* Jump instructions */
1012 [BPF_JMP | BPF_JA] = true,
1013 [BPF_JMP | BPF_JEQ | BPF_K] = true,
1014 [BPF_JMP | BPF_JEQ | BPF_X] = true,
1015 [BPF_JMP | BPF_JGE | BPF_K] = true,
1016 [BPF_JMP | BPF_JGE | BPF_X] = true,
1017 [BPF_JMP | BPF_JGT | BPF_K] = true,
1018 [BPF_JMP | BPF_JGT | BPF_X] = true,
1019 [BPF_JMP | BPF_JSET | BPF_K] = true,
1020 [BPF_JMP | BPF_JSET | BPF_X] = true,
1023 if (code_to_probe >= ARRAY_SIZE(codes))
1026 return codes[code_to_probe];
1029 static bool bpf_check_basics_ok(const struct sock_filter *filter,
1034 if (flen == 0 || flen > BPF_MAXINSNS)
1041 * bpf_check_classic - verify socket filter code
1042 * @filter: filter to verify
1043 * @flen: length of filter
1045 * Check the user's filter code. If we let some ugly
1046 * filter code slip through kaboom! The filter must contain
1047 * no references or jumps that are out of range, no illegal
1048 * instructions, and must end with a RET instruction.
1050 * All jumps are forward as they are not signed.
1052 * Returns 0 if the rule set is legal or -EINVAL if not.
1054 static int bpf_check_classic(const struct sock_filter *filter,
1060 /* Check the filter code now */
1061 for (pc = 0; pc < flen; pc++) {
1062 const struct sock_filter *ftest = &filter[pc];
1064 /* May we actually operate on this code? */
1065 if (!chk_code_allowed(ftest->code))
1068 /* Some instructions need special checks */
1069 switch (ftest->code) {
1070 case BPF_ALU | BPF_DIV | BPF_K:
1071 case BPF_ALU | BPF_MOD | BPF_K:
1072 /* Check for division by zero */
1076 case BPF_ALU | BPF_LSH | BPF_K:
1077 case BPF_ALU | BPF_RSH | BPF_K:
1081 case BPF_LD | BPF_MEM:
1082 case BPF_LDX | BPF_MEM:
1085 /* Check for invalid memory addresses */
1086 if (ftest->k >= BPF_MEMWORDS)
1089 case BPF_JMP | BPF_JA:
1090 /* Note, the large ftest->k might cause loops.
1091 * Compare this with conditional jumps below,
1092 * where offsets are limited. --ANK (981016)
1094 if (ftest->k >= (unsigned int)(flen - pc - 1))
1097 case BPF_JMP | BPF_JEQ | BPF_K:
1098 case BPF_JMP | BPF_JEQ | BPF_X:
1099 case BPF_JMP | BPF_JGE | BPF_K:
1100 case BPF_JMP | BPF_JGE | BPF_X:
1101 case BPF_JMP | BPF_JGT | BPF_K:
1102 case BPF_JMP | BPF_JGT | BPF_X:
1103 case BPF_JMP | BPF_JSET | BPF_K:
1104 case BPF_JMP | BPF_JSET | BPF_X:
1105 /* Both conditionals must be safe */
1106 if (pc + ftest->jt + 1 >= flen ||
1107 pc + ftest->jf + 1 >= flen)
1110 case BPF_LD | BPF_W | BPF_ABS:
1111 case BPF_LD | BPF_H | BPF_ABS:
1112 case BPF_LD | BPF_B | BPF_ABS:
1114 if (bpf_anc_helper(ftest) & BPF_ANC)
1116 /* Ancillary operation unknown or unsupported */
1117 if (anc_found == false && ftest->k >= SKF_AD_OFF)
1122 /* Last instruction must be a RET code */
1123 switch (filter[flen - 1].code) {
1124 case BPF_RET | BPF_K:
1125 case BPF_RET | BPF_A:
1126 return check_load_and_stores(filter, flen);
1132 static int bpf_prog_store_orig_filter(struct bpf_prog *fp,
1133 const struct sock_fprog *fprog)
1135 unsigned int fsize = bpf_classic_proglen(fprog);
1136 struct sock_fprog_kern *fkprog;
1138 fp->orig_prog = kmalloc(sizeof(*fkprog), GFP_KERNEL);
1142 fkprog = fp->orig_prog;
1143 fkprog->len = fprog->len;
1145 fkprog->filter = kmemdup(fp->insns, fsize,
1146 GFP_KERNEL | __GFP_NOWARN);
1147 if (!fkprog->filter) {
1148 kfree(fp->orig_prog);
1155 static void bpf_release_orig_filter(struct bpf_prog *fp)
1157 struct sock_fprog_kern *fprog = fp->orig_prog;
1160 kfree(fprog->filter);
1165 static void __bpf_prog_release(struct bpf_prog *prog)
1167 if (prog->type == BPF_PROG_TYPE_SOCKET_FILTER) {
1170 bpf_release_orig_filter(prog);
1171 bpf_prog_free(prog);
1175 static void __sk_filter_release(struct sk_filter *fp)
1177 __bpf_prog_release(fp->prog);
1182 * sk_filter_release_rcu - Release a socket filter by rcu_head
1183 * @rcu: rcu_head that contains the sk_filter to free
1185 static void sk_filter_release_rcu(struct rcu_head *rcu)
1187 struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu);
1189 __sk_filter_release(fp);
1193 * sk_filter_release - release a socket filter
1194 * @fp: filter to remove
1196 * Remove a filter from a socket and release its resources.
1198 static void sk_filter_release(struct sk_filter *fp)
1200 if (refcount_dec_and_test(&fp->refcnt))
1201 call_rcu(&fp->rcu, sk_filter_release_rcu);
1204 void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp)
1206 u32 filter_size = bpf_prog_size(fp->prog->len);
1208 atomic_sub(filter_size, &sk->sk_omem_alloc);
1209 sk_filter_release(fp);
1212 /* try to charge the socket memory if there is space available
1213 * return true on success
1215 static bool __sk_filter_charge(struct sock *sk, struct sk_filter *fp)
1217 u32 filter_size = bpf_prog_size(fp->prog->len);
1218 int optmem_max = READ_ONCE(sysctl_optmem_max);
1220 /* same check as in sock_kmalloc() */
1221 if (filter_size <= optmem_max &&
1222 atomic_read(&sk->sk_omem_alloc) + filter_size < optmem_max) {
1223 atomic_add(filter_size, &sk->sk_omem_alloc);
1229 bool sk_filter_charge(struct sock *sk, struct sk_filter *fp)
1231 if (!refcount_inc_not_zero(&fp->refcnt))
1234 if (!__sk_filter_charge(sk, fp)) {
1235 sk_filter_release(fp);
1241 static struct bpf_prog *bpf_migrate_filter(struct bpf_prog *fp)
1243 struct sock_filter *old_prog;
1244 struct bpf_prog *old_fp;
1245 int err, new_len, old_len = fp->len;
1246 bool seen_ld_abs = false;
1248 /* We are free to overwrite insns et al right here as it won't be used at
1249 * this point in time anymore internally after the migration to the eBPF
1250 * instruction representation.
1252 BUILD_BUG_ON(sizeof(struct sock_filter) !=
1253 sizeof(struct bpf_insn));
1255 /* Conversion cannot happen on overlapping memory areas,
1256 * so we need to keep the user BPF around until the 2nd
1257 * pass. At this time, the user BPF is stored in fp->insns.
1259 old_prog = kmemdup(fp->insns, old_len * sizeof(struct sock_filter),
1260 GFP_KERNEL | __GFP_NOWARN);
1266 /* 1st pass: calculate the new program length. */
1267 err = bpf_convert_filter(old_prog, old_len, NULL, &new_len,
1272 /* Expand fp for appending the new filter representation. */
1274 fp = bpf_prog_realloc(old_fp, bpf_prog_size(new_len), 0);
1276 /* The old_fp is still around in case we couldn't
1277 * allocate new memory, so uncharge on that one.
1286 /* 2nd pass: remap sock_filter insns into bpf_insn insns. */
1287 err = bpf_convert_filter(old_prog, old_len, fp, &new_len,
1290 /* 2nd bpf_convert_filter() can fail only if it fails
1291 * to allocate memory, remapping must succeed. Note,
1292 * that at this time old_fp has already been released
1297 fp = bpf_prog_select_runtime(fp, &err);
1307 __bpf_prog_release(fp);
1308 return ERR_PTR(err);
1311 static struct bpf_prog *bpf_prepare_filter(struct bpf_prog *fp,
1312 bpf_aux_classic_check_t trans)
1316 fp->bpf_func = NULL;
1319 err = bpf_check_classic(fp->insns, fp->len);
1321 __bpf_prog_release(fp);
1322 return ERR_PTR(err);
1325 /* There might be additional checks and transformations
1326 * needed on classic filters, f.e. in case of seccomp.
1329 err = trans(fp->insns, fp->len);
1331 __bpf_prog_release(fp);
1332 return ERR_PTR(err);
1336 /* Probe if we can JIT compile the filter and if so, do
1337 * the compilation of the filter.
1339 bpf_jit_compile(fp);
1341 /* JIT compiler couldn't process this filter, so do the eBPF translation
1342 * for the optimized interpreter.
1345 fp = bpf_migrate_filter(fp);
1351 * bpf_prog_create - create an unattached filter
1352 * @pfp: the unattached filter that is created
1353 * @fprog: the filter program
1355 * Create a filter independent of any socket. We first run some
1356 * sanity checks on it to make sure it does not explode on us later.
1357 * If an error occurs or there is insufficient memory for the filter
1358 * a negative errno code is returned. On success the return is zero.
1360 int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog)
1362 unsigned int fsize = bpf_classic_proglen(fprog);
1363 struct bpf_prog *fp;
1365 /* Make sure new filter is there and in the right amounts. */
1366 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1369 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1373 memcpy(fp->insns, fprog->filter, fsize);
1375 fp->len = fprog->len;
1376 /* Since unattached filters are not copied back to user
1377 * space through sk_get_filter(), we do not need to hold
1378 * a copy here, and can spare us the work.
1380 fp->orig_prog = NULL;
1382 /* bpf_prepare_filter() already takes care of freeing
1383 * memory in case something goes wrong.
1385 fp = bpf_prepare_filter(fp, NULL);
1392 EXPORT_SYMBOL_GPL(bpf_prog_create);
1395 * bpf_prog_create_from_user - create an unattached filter from user buffer
1396 * @pfp: the unattached filter that is created
1397 * @fprog: the filter program
1398 * @trans: post-classic verifier transformation handler
1399 * @save_orig: save classic BPF program
1401 * This function effectively does the same as bpf_prog_create(), only
1402 * that it builds up its insns buffer from user space provided buffer.
1403 * It also allows for passing a bpf_aux_classic_check_t handler.
1405 int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog,
1406 bpf_aux_classic_check_t trans, bool save_orig)
1408 unsigned int fsize = bpf_classic_proglen(fprog);
1409 struct bpf_prog *fp;
1412 /* Make sure new filter is there and in the right amounts. */
1413 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1416 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1420 if (copy_from_user(fp->insns, fprog->filter, fsize)) {
1421 __bpf_prog_free(fp);
1425 fp->len = fprog->len;
1426 fp->orig_prog = NULL;
1429 err = bpf_prog_store_orig_filter(fp, fprog);
1431 __bpf_prog_free(fp);
1436 /* bpf_prepare_filter() already takes care of freeing
1437 * memory in case something goes wrong.
1439 fp = bpf_prepare_filter(fp, trans);
1446 EXPORT_SYMBOL_GPL(bpf_prog_create_from_user);
1448 void bpf_prog_destroy(struct bpf_prog *fp)
1450 __bpf_prog_release(fp);
1452 EXPORT_SYMBOL_GPL(bpf_prog_destroy);
1454 static int __sk_attach_prog(struct bpf_prog *prog, struct sock *sk)
1456 struct sk_filter *fp, *old_fp;
1458 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
1464 if (!__sk_filter_charge(sk, fp)) {
1468 refcount_set(&fp->refcnt, 1);
1470 old_fp = rcu_dereference_protected(sk->sk_filter,
1471 lockdep_sock_is_held(sk));
1472 rcu_assign_pointer(sk->sk_filter, fp);
1475 sk_filter_uncharge(sk, old_fp);
1481 struct bpf_prog *__get_filter(struct sock_fprog *fprog, struct sock *sk)
1483 unsigned int fsize = bpf_classic_proglen(fprog);
1484 struct bpf_prog *prog;
1487 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1488 return ERR_PTR(-EPERM);
1490 /* Make sure new filter is there and in the right amounts. */
1491 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1492 return ERR_PTR(-EINVAL);
1494 prog = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1496 return ERR_PTR(-ENOMEM);
1498 if (copy_from_user(prog->insns, fprog->filter, fsize)) {
1499 __bpf_prog_free(prog);
1500 return ERR_PTR(-EFAULT);
1503 prog->len = fprog->len;
1505 err = bpf_prog_store_orig_filter(prog, fprog);
1507 __bpf_prog_free(prog);
1508 return ERR_PTR(-ENOMEM);
1511 /* bpf_prepare_filter() already takes care of freeing
1512 * memory in case something goes wrong.
1514 return bpf_prepare_filter(prog, NULL);
1518 * sk_attach_filter - attach a socket filter
1519 * @fprog: the filter program
1520 * @sk: the socket to use
1522 * Attach the user's filter code. We first run some sanity checks on
1523 * it to make sure it does not explode on us later. If an error
1524 * occurs or there is insufficient memory for the filter a negative
1525 * errno code is returned. On success the return is zero.
1527 int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1529 struct bpf_prog *prog = __get_filter(fprog, sk);
1533 return PTR_ERR(prog);
1535 err = __sk_attach_prog(prog, sk);
1537 __bpf_prog_release(prog);
1543 EXPORT_SYMBOL_GPL(sk_attach_filter);
1545 int sk_reuseport_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1547 struct bpf_prog *prog = __get_filter(fprog, sk);
1551 return PTR_ERR(prog);
1553 if (bpf_prog_size(prog->len) > READ_ONCE(sysctl_optmem_max))
1556 err = reuseport_attach_prog(sk, prog);
1559 __bpf_prog_release(prog);
1564 static struct bpf_prog *__get_bpf(u32 ufd, struct sock *sk)
1566 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1567 return ERR_PTR(-EPERM);
1569 return bpf_prog_get_type(ufd, BPF_PROG_TYPE_SOCKET_FILTER);
1572 int sk_attach_bpf(u32 ufd, struct sock *sk)
1574 struct bpf_prog *prog = __get_bpf(ufd, sk);
1578 return PTR_ERR(prog);
1580 err = __sk_attach_prog(prog, sk);
1589 int sk_reuseport_attach_bpf(u32 ufd, struct sock *sk)
1591 struct bpf_prog *prog;
1594 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1597 prog = bpf_prog_get_type(ufd, BPF_PROG_TYPE_SOCKET_FILTER);
1598 if (PTR_ERR(prog) == -EINVAL)
1599 prog = bpf_prog_get_type(ufd, BPF_PROG_TYPE_SK_REUSEPORT);
1601 return PTR_ERR(prog);
1603 if (prog->type == BPF_PROG_TYPE_SK_REUSEPORT) {
1604 /* Like other non BPF_PROG_TYPE_SOCKET_FILTER
1605 * bpf prog (e.g. sockmap). It depends on the
1606 * limitation imposed by bpf_prog_load().
1607 * Hence, sysctl_optmem_max is not checked.
1609 if ((sk->sk_type != SOCK_STREAM &&
1610 sk->sk_type != SOCK_DGRAM) ||
1611 (sk->sk_protocol != IPPROTO_UDP &&
1612 sk->sk_protocol != IPPROTO_TCP) ||
1613 (sk->sk_family != AF_INET &&
1614 sk->sk_family != AF_INET6)) {
1619 /* BPF_PROG_TYPE_SOCKET_FILTER */
1620 if (bpf_prog_size(prog->len) > READ_ONCE(sysctl_optmem_max)) {
1626 err = reuseport_attach_prog(sk, prog);
1634 void sk_reuseport_prog_free(struct bpf_prog *prog)
1639 if (prog->type == BPF_PROG_TYPE_SK_REUSEPORT)
1642 bpf_prog_destroy(prog);
1645 struct bpf_scratchpad {
1647 __be32 diff[MAX_BPF_STACK / sizeof(__be32)];
1648 u8 buff[MAX_BPF_STACK];
1652 static DEFINE_PER_CPU(struct bpf_scratchpad, bpf_sp);
1654 static inline int __bpf_try_make_writable(struct sk_buff *skb,
1655 unsigned int write_len)
1657 return skb_ensure_writable(skb, write_len);
1660 static inline int bpf_try_make_writable(struct sk_buff *skb,
1661 unsigned int write_len)
1663 int err = __bpf_try_make_writable(skb, write_len);
1665 bpf_compute_data_pointers(skb);
1669 static int bpf_try_make_head_writable(struct sk_buff *skb)
1671 return bpf_try_make_writable(skb, skb_headlen(skb));
1674 static inline void bpf_push_mac_rcsum(struct sk_buff *skb)
1676 if (skb_at_tc_ingress(skb))
1677 skb_postpush_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1680 static inline void bpf_pull_mac_rcsum(struct sk_buff *skb)
1682 if (skb_at_tc_ingress(skb))
1683 skb_postpull_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1686 BPF_CALL_5(bpf_skb_store_bytes, struct sk_buff *, skb, u32, offset,
1687 const void *, from, u32, len, u64, flags)
1691 if (unlikely(flags & ~(BPF_F_RECOMPUTE_CSUM | BPF_F_INVALIDATE_HASH)))
1693 if (unlikely(offset > INT_MAX))
1695 if (unlikely(bpf_try_make_writable(skb, offset + len)))
1698 ptr = skb->data + offset;
1699 if (flags & BPF_F_RECOMPUTE_CSUM)
1700 __skb_postpull_rcsum(skb, ptr, len, offset);
1702 memcpy(ptr, from, len);
1704 if (flags & BPF_F_RECOMPUTE_CSUM)
1705 __skb_postpush_rcsum(skb, ptr, len, offset);
1706 if (flags & BPF_F_INVALIDATE_HASH)
1707 skb_clear_hash(skb);
1712 static const struct bpf_func_proto bpf_skb_store_bytes_proto = {
1713 .func = bpf_skb_store_bytes,
1715 .ret_type = RET_INTEGER,
1716 .arg1_type = ARG_PTR_TO_CTX,
1717 .arg2_type = ARG_ANYTHING,
1718 .arg3_type = ARG_PTR_TO_MEM | MEM_RDONLY,
1719 .arg4_type = ARG_CONST_SIZE,
1720 .arg5_type = ARG_ANYTHING,
1723 BPF_CALL_4(bpf_skb_load_bytes, const struct sk_buff *, skb, u32, offset,
1724 void *, to, u32, len)
1728 if (unlikely(offset > INT_MAX))
1731 ptr = skb_header_pointer(skb, offset, len, to);
1735 memcpy(to, ptr, len);
1743 static const struct bpf_func_proto bpf_skb_load_bytes_proto = {
1744 .func = bpf_skb_load_bytes,
1746 .ret_type = RET_INTEGER,
1747 .arg1_type = ARG_PTR_TO_CTX,
1748 .arg2_type = ARG_ANYTHING,
1749 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
1750 .arg4_type = ARG_CONST_SIZE,
1753 BPF_CALL_4(bpf_flow_dissector_load_bytes,
1754 const struct bpf_flow_dissector *, ctx, u32, offset,
1755 void *, to, u32, len)
1759 if (unlikely(offset > 0xffff))
1762 if (unlikely(!ctx->skb))
1765 ptr = skb_header_pointer(ctx->skb, offset, len, to);
1769 memcpy(to, ptr, len);
1777 static const struct bpf_func_proto bpf_flow_dissector_load_bytes_proto = {
1778 .func = bpf_flow_dissector_load_bytes,
1780 .ret_type = RET_INTEGER,
1781 .arg1_type = ARG_PTR_TO_CTX,
1782 .arg2_type = ARG_ANYTHING,
1783 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
1784 .arg4_type = ARG_CONST_SIZE,
1787 BPF_CALL_5(bpf_skb_load_bytes_relative, const struct sk_buff *, skb,
1788 u32, offset, void *, to, u32, len, u32, start_header)
1790 u8 *end = skb_tail_pointer(skb);
1793 if (unlikely(offset > 0xffff))
1796 switch (start_header) {
1797 case BPF_HDR_START_MAC:
1798 if (unlikely(!skb_mac_header_was_set(skb)))
1800 start = skb_mac_header(skb);
1802 case BPF_HDR_START_NET:
1803 start = skb_network_header(skb);
1809 ptr = start + offset;
1811 if (likely(ptr + len <= end)) {
1812 memcpy(to, ptr, len);
1821 static const struct bpf_func_proto bpf_skb_load_bytes_relative_proto = {
1822 .func = bpf_skb_load_bytes_relative,
1824 .ret_type = RET_INTEGER,
1825 .arg1_type = ARG_PTR_TO_CTX,
1826 .arg2_type = ARG_ANYTHING,
1827 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
1828 .arg4_type = ARG_CONST_SIZE,
1829 .arg5_type = ARG_ANYTHING,
1832 BPF_CALL_2(bpf_skb_pull_data, struct sk_buff *, skb, u32, len)
1834 /* Idea is the following: should the needed direct read/write
1835 * test fail during runtime, we can pull in more data and redo
1836 * again, since implicitly, we invalidate previous checks here.
1838 * Or, since we know how much we need to make read/writeable,
1839 * this can be done once at the program beginning for direct
1840 * access case. By this we overcome limitations of only current
1841 * headroom being accessible.
1843 return bpf_try_make_writable(skb, len ? : skb_headlen(skb));
1846 static const struct bpf_func_proto bpf_skb_pull_data_proto = {
1847 .func = bpf_skb_pull_data,
1849 .ret_type = RET_INTEGER,
1850 .arg1_type = ARG_PTR_TO_CTX,
1851 .arg2_type = ARG_ANYTHING,
1854 BPF_CALL_1(bpf_sk_fullsock, struct sock *, sk)
1856 return sk_fullsock(sk) ? (unsigned long)sk : (unsigned long)NULL;
1859 static const struct bpf_func_proto bpf_sk_fullsock_proto = {
1860 .func = bpf_sk_fullsock,
1862 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
1863 .arg1_type = ARG_PTR_TO_SOCK_COMMON,
1866 static inline int sk_skb_try_make_writable(struct sk_buff *skb,
1867 unsigned int write_len)
1869 return __bpf_try_make_writable(skb, write_len);
1872 BPF_CALL_2(sk_skb_pull_data, struct sk_buff *, skb, u32, len)
1874 /* Idea is the following: should the needed direct read/write
1875 * test fail during runtime, we can pull in more data and redo
1876 * again, since implicitly, we invalidate previous checks here.
1878 * Or, since we know how much we need to make read/writeable,
1879 * this can be done once at the program beginning for direct
1880 * access case. By this we overcome limitations of only current
1881 * headroom being accessible.
1883 return sk_skb_try_make_writable(skb, len ? : skb_headlen(skb));
1886 static const struct bpf_func_proto sk_skb_pull_data_proto = {
1887 .func = sk_skb_pull_data,
1889 .ret_type = RET_INTEGER,
1890 .arg1_type = ARG_PTR_TO_CTX,
1891 .arg2_type = ARG_ANYTHING,
1894 BPF_CALL_5(bpf_l3_csum_replace, struct sk_buff *, skb, u32, offset,
1895 u64, from, u64, to, u64, flags)
1899 if (unlikely(flags & ~(BPF_F_HDR_FIELD_MASK)))
1901 if (unlikely(offset > 0xffff || offset & 1))
1903 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
1906 ptr = (__sum16 *)(skb->data + offset);
1907 switch (flags & BPF_F_HDR_FIELD_MASK) {
1909 if (unlikely(from != 0))
1912 csum_replace_by_diff(ptr, to);
1915 csum_replace2(ptr, from, to);
1918 csum_replace4(ptr, from, to);
1927 static const struct bpf_func_proto bpf_l3_csum_replace_proto = {
1928 .func = bpf_l3_csum_replace,
1930 .ret_type = RET_INTEGER,
1931 .arg1_type = ARG_PTR_TO_CTX,
1932 .arg2_type = ARG_ANYTHING,
1933 .arg3_type = ARG_ANYTHING,
1934 .arg4_type = ARG_ANYTHING,
1935 .arg5_type = ARG_ANYTHING,
1938 BPF_CALL_5(bpf_l4_csum_replace, struct sk_buff *, skb, u32, offset,
1939 u64, from, u64, to, u64, flags)
1941 bool is_pseudo = flags & BPF_F_PSEUDO_HDR;
1942 bool is_mmzero = flags & BPF_F_MARK_MANGLED_0;
1943 bool do_mforce = flags & BPF_F_MARK_ENFORCE;
1946 if (unlikely(flags & ~(BPF_F_MARK_MANGLED_0 | BPF_F_MARK_ENFORCE |
1947 BPF_F_PSEUDO_HDR | BPF_F_HDR_FIELD_MASK)))
1949 if (unlikely(offset > 0xffff || offset & 1))
1951 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
1954 ptr = (__sum16 *)(skb->data + offset);
1955 if (is_mmzero && !do_mforce && !*ptr)
1958 switch (flags & BPF_F_HDR_FIELD_MASK) {
1960 if (unlikely(from != 0))
1963 inet_proto_csum_replace_by_diff(ptr, skb, to, is_pseudo);
1966 inet_proto_csum_replace2(ptr, skb, from, to, is_pseudo);
1969 inet_proto_csum_replace4(ptr, skb, from, to, is_pseudo);
1975 if (is_mmzero && !*ptr)
1976 *ptr = CSUM_MANGLED_0;
1980 static const struct bpf_func_proto bpf_l4_csum_replace_proto = {
1981 .func = bpf_l4_csum_replace,
1983 .ret_type = RET_INTEGER,
1984 .arg1_type = ARG_PTR_TO_CTX,
1985 .arg2_type = ARG_ANYTHING,
1986 .arg3_type = ARG_ANYTHING,
1987 .arg4_type = ARG_ANYTHING,
1988 .arg5_type = ARG_ANYTHING,
1991 BPF_CALL_5(bpf_csum_diff, __be32 *, from, u32, from_size,
1992 __be32 *, to, u32, to_size, __wsum, seed)
1994 struct bpf_scratchpad *sp = this_cpu_ptr(&bpf_sp);
1995 u32 diff_size = from_size + to_size;
1998 /* This is quite flexible, some examples:
2000 * from_size == 0, to_size > 0, seed := csum --> pushing data
2001 * from_size > 0, to_size == 0, seed := csum --> pulling data
2002 * from_size > 0, to_size > 0, seed := 0 --> diffing data
2004 * Even for diffing, from_size and to_size don't need to be equal.
2006 if (unlikely(((from_size | to_size) & (sizeof(__be32) - 1)) ||
2007 diff_size > sizeof(sp->diff)))
2010 for (i = 0; i < from_size / sizeof(__be32); i++, j++)
2011 sp->diff[j] = ~from[i];
2012 for (i = 0; i < to_size / sizeof(__be32); i++, j++)
2013 sp->diff[j] = to[i];
2015 return csum_partial(sp->diff, diff_size, seed);
2018 static const struct bpf_func_proto bpf_csum_diff_proto = {
2019 .func = bpf_csum_diff,
2022 .ret_type = RET_INTEGER,
2023 .arg1_type = ARG_PTR_TO_MEM | PTR_MAYBE_NULL | MEM_RDONLY,
2024 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
2025 .arg3_type = ARG_PTR_TO_MEM | PTR_MAYBE_NULL | MEM_RDONLY,
2026 .arg4_type = ARG_CONST_SIZE_OR_ZERO,
2027 .arg5_type = ARG_ANYTHING,
2030 BPF_CALL_2(bpf_csum_update, struct sk_buff *, skb, __wsum, csum)
2032 /* The interface is to be used in combination with bpf_csum_diff()
2033 * for direct packet writes. csum rotation for alignment as well
2034 * as emulating csum_sub() can be done from the eBPF program.
2036 if (skb->ip_summed == CHECKSUM_COMPLETE)
2037 return (skb->csum = csum_add(skb->csum, csum));
2042 static const struct bpf_func_proto bpf_csum_update_proto = {
2043 .func = bpf_csum_update,
2045 .ret_type = RET_INTEGER,
2046 .arg1_type = ARG_PTR_TO_CTX,
2047 .arg2_type = ARG_ANYTHING,
2050 BPF_CALL_2(bpf_csum_level, struct sk_buff *, skb, u64, level)
2052 /* The interface is to be used in combination with bpf_skb_adjust_room()
2053 * for encap/decap of packet headers when BPF_F_ADJ_ROOM_NO_CSUM_RESET
2054 * is passed as flags, for example.
2057 case BPF_CSUM_LEVEL_INC:
2058 __skb_incr_checksum_unnecessary(skb);
2060 case BPF_CSUM_LEVEL_DEC:
2061 __skb_decr_checksum_unnecessary(skb);
2063 case BPF_CSUM_LEVEL_RESET:
2064 __skb_reset_checksum_unnecessary(skb);
2066 case BPF_CSUM_LEVEL_QUERY:
2067 return skb->ip_summed == CHECKSUM_UNNECESSARY ?
2068 skb->csum_level : -EACCES;
2076 static const struct bpf_func_proto bpf_csum_level_proto = {
2077 .func = bpf_csum_level,
2079 .ret_type = RET_INTEGER,
2080 .arg1_type = ARG_PTR_TO_CTX,
2081 .arg2_type = ARG_ANYTHING,
2084 static inline int __bpf_rx_skb(struct net_device *dev, struct sk_buff *skb)
2086 return dev_forward_skb_nomtu(dev, skb);
2089 static inline int __bpf_rx_skb_no_mac(struct net_device *dev,
2090 struct sk_buff *skb)
2092 int ret = ____dev_forward_skb(dev, skb, false);
2096 ret = netif_rx(skb);
2102 static inline int __bpf_tx_skb(struct net_device *dev, struct sk_buff *skb)
2106 if (dev_xmit_recursion()) {
2107 net_crit_ratelimited("bpf: recursion limit reached on datapath, buggy bpf program?\n");
2113 skb_clear_tstamp(skb);
2115 dev_xmit_recursion_inc();
2116 ret = dev_queue_xmit(skb);
2117 dev_xmit_recursion_dec();
2122 static int __bpf_redirect_no_mac(struct sk_buff *skb, struct net_device *dev,
2125 unsigned int mlen = skb_network_offset(skb);
2127 if (unlikely(skb->len <= mlen)) {
2133 __skb_pull(skb, mlen);
2135 /* At ingress, the mac header has already been pulled once.
2136 * At egress, skb_pospull_rcsum has to be done in case that
2137 * the skb is originated from ingress (i.e. a forwarded skb)
2138 * to ensure that rcsum starts at net header.
2140 if (!skb_at_tc_ingress(skb))
2141 skb_postpull_rcsum(skb, skb_mac_header(skb), mlen);
2143 skb_pop_mac_header(skb);
2144 skb_reset_mac_len(skb);
2145 return flags & BPF_F_INGRESS ?
2146 __bpf_rx_skb_no_mac(dev, skb) : __bpf_tx_skb(dev, skb);
2149 static int __bpf_redirect_common(struct sk_buff *skb, struct net_device *dev,
2152 /* Verify that a link layer header is carried */
2153 if (unlikely(skb->mac_header >= skb->network_header || skb->len == 0)) {
2158 bpf_push_mac_rcsum(skb);
2159 return flags & BPF_F_INGRESS ?
2160 __bpf_rx_skb(dev, skb) : __bpf_tx_skb(dev, skb);
2163 static int __bpf_redirect(struct sk_buff *skb, struct net_device *dev,
2166 if (dev_is_mac_header_xmit(dev))
2167 return __bpf_redirect_common(skb, dev, flags);
2169 return __bpf_redirect_no_mac(skb, dev, flags);
2172 #if IS_ENABLED(CONFIG_IPV6)
2173 static int bpf_out_neigh_v6(struct net *net, struct sk_buff *skb,
2174 struct net_device *dev, struct bpf_nh_params *nh)
2176 u32 hh_len = LL_RESERVED_SPACE(dev);
2177 const struct in6_addr *nexthop;
2178 struct dst_entry *dst = NULL;
2179 struct neighbour *neigh;
2181 if (dev_xmit_recursion()) {
2182 net_crit_ratelimited("bpf: recursion limit reached on datapath, buggy bpf program?\n");
2187 skb_clear_tstamp(skb);
2189 if (unlikely(skb_headroom(skb) < hh_len && dev->header_ops)) {
2190 skb = skb_expand_head(skb, hh_len);
2198 nexthop = rt6_nexthop(container_of(dst, struct rt6_info, dst),
2199 &ipv6_hdr(skb)->daddr);
2201 nexthop = &nh->ipv6_nh;
2203 neigh = ip_neigh_gw6(dev, nexthop);
2204 if (likely(!IS_ERR(neigh))) {
2207 sock_confirm_neigh(skb, neigh);
2208 dev_xmit_recursion_inc();
2209 ret = neigh_output(neigh, skb, false);
2210 dev_xmit_recursion_dec();
2211 rcu_read_unlock_bh();
2214 rcu_read_unlock_bh();
2216 IP6_INC_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_OUTNOROUTES);
2222 static int __bpf_redirect_neigh_v6(struct sk_buff *skb, struct net_device *dev,
2223 struct bpf_nh_params *nh)
2225 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
2226 struct net *net = dev_net(dev);
2227 int err, ret = NET_XMIT_DROP;
2230 struct dst_entry *dst;
2231 struct flowi6 fl6 = {
2232 .flowi6_flags = FLOWI_FLAG_ANYSRC,
2233 .flowi6_mark = skb->mark,
2234 .flowlabel = ip6_flowinfo(ip6h),
2235 .flowi6_oif = dev->ifindex,
2236 .flowi6_proto = ip6h->nexthdr,
2237 .daddr = ip6h->daddr,
2238 .saddr = ip6h->saddr,
2241 dst = ipv6_stub->ipv6_dst_lookup_flow(net, NULL, &fl6, NULL);
2245 skb_dst_set(skb, dst);
2246 } else if (nh->nh_family != AF_INET6) {
2250 err = bpf_out_neigh_v6(net, skb, dev, nh);
2251 if (unlikely(net_xmit_eval(err)))
2252 dev->stats.tx_errors++;
2254 ret = NET_XMIT_SUCCESS;
2257 dev->stats.tx_errors++;
2263 static int __bpf_redirect_neigh_v6(struct sk_buff *skb, struct net_device *dev,
2264 struct bpf_nh_params *nh)
2267 return NET_XMIT_DROP;
2269 #endif /* CONFIG_IPV6 */
2271 #if IS_ENABLED(CONFIG_INET)
2272 static int bpf_out_neigh_v4(struct net *net, struct sk_buff *skb,
2273 struct net_device *dev, struct bpf_nh_params *nh)
2275 u32 hh_len = LL_RESERVED_SPACE(dev);
2276 struct neighbour *neigh;
2277 bool is_v6gw = false;
2279 if (dev_xmit_recursion()) {
2280 net_crit_ratelimited("bpf: recursion limit reached on datapath, buggy bpf program?\n");
2285 skb_clear_tstamp(skb);
2287 if (unlikely(skb_headroom(skb) < hh_len && dev->header_ops)) {
2288 skb = skb_expand_head(skb, hh_len);
2295 struct dst_entry *dst = skb_dst(skb);
2296 struct rtable *rt = container_of(dst, struct rtable, dst);
2298 neigh = ip_neigh_for_gw(rt, skb, &is_v6gw);
2299 } else if (nh->nh_family == AF_INET6) {
2300 neigh = ip_neigh_gw6(dev, &nh->ipv6_nh);
2302 } else if (nh->nh_family == AF_INET) {
2303 neigh = ip_neigh_gw4(dev, nh->ipv4_nh);
2305 rcu_read_unlock_bh();
2309 if (likely(!IS_ERR(neigh))) {
2312 sock_confirm_neigh(skb, neigh);
2313 dev_xmit_recursion_inc();
2314 ret = neigh_output(neigh, skb, is_v6gw);
2315 dev_xmit_recursion_dec();
2316 rcu_read_unlock_bh();
2319 rcu_read_unlock_bh();
2325 static int __bpf_redirect_neigh_v4(struct sk_buff *skb, struct net_device *dev,
2326 struct bpf_nh_params *nh)
2328 const struct iphdr *ip4h = ip_hdr(skb);
2329 struct net *net = dev_net(dev);
2330 int err, ret = NET_XMIT_DROP;
2333 struct flowi4 fl4 = {
2334 .flowi4_flags = FLOWI_FLAG_ANYSRC,
2335 .flowi4_mark = skb->mark,
2336 .flowi4_tos = RT_TOS(ip4h->tos),
2337 .flowi4_oif = dev->ifindex,
2338 .flowi4_proto = ip4h->protocol,
2339 .daddr = ip4h->daddr,
2340 .saddr = ip4h->saddr,
2344 rt = ip_route_output_flow(net, &fl4, NULL);
2347 if (rt->rt_type != RTN_UNICAST && rt->rt_type != RTN_LOCAL) {
2352 skb_dst_set(skb, &rt->dst);
2355 err = bpf_out_neigh_v4(net, skb, dev, nh);
2356 if (unlikely(net_xmit_eval(err)))
2357 dev->stats.tx_errors++;
2359 ret = NET_XMIT_SUCCESS;
2362 dev->stats.tx_errors++;
2368 static int __bpf_redirect_neigh_v4(struct sk_buff *skb, struct net_device *dev,
2369 struct bpf_nh_params *nh)
2372 return NET_XMIT_DROP;
2374 #endif /* CONFIG_INET */
2376 static int __bpf_redirect_neigh(struct sk_buff *skb, struct net_device *dev,
2377 struct bpf_nh_params *nh)
2379 struct ethhdr *ethh = eth_hdr(skb);
2381 if (unlikely(skb->mac_header >= skb->network_header))
2383 bpf_push_mac_rcsum(skb);
2384 if (is_multicast_ether_addr(ethh->h_dest))
2387 skb_pull(skb, sizeof(*ethh));
2388 skb_unset_mac_header(skb);
2389 skb_reset_network_header(skb);
2391 if (skb->protocol == htons(ETH_P_IP))
2392 return __bpf_redirect_neigh_v4(skb, dev, nh);
2393 else if (skb->protocol == htons(ETH_P_IPV6))
2394 return __bpf_redirect_neigh_v6(skb, dev, nh);
2400 /* Internal, non-exposed redirect flags. */
2402 BPF_F_NEIGH = (1ULL << 1),
2403 BPF_F_PEER = (1ULL << 2),
2404 BPF_F_NEXTHOP = (1ULL << 3),
2405 #define BPF_F_REDIRECT_INTERNAL (BPF_F_NEIGH | BPF_F_PEER | BPF_F_NEXTHOP)
2408 BPF_CALL_3(bpf_clone_redirect, struct sk_buff *, skb, u32, ifindex, u64, flags)
2410 struct net_device *dev;
2411 struct sk_buff *clone;
2414 if (unlikely(flags & (~(BPF_F_INGRESS) | BPF_F_REDIRECT_INTERNAL)))
2417 dev = dev_get_by_index_rcu(dev_net(skb->dev), ifindex);
2421 clone = skb_clone(skb, GFP_ATOMIC);
2422 if (unlikely(!clone))
2425 /* For direct write, we need to keep the invariant that the skbs
2426 * we're dealing with need to be uncloned. Should uncloning fail
2427 * here, we need to free the just generated clone to unclone once
2430 ret = bpf_try_make_head_writable(skb);
2431 if (unlikely(ret)) {
2436 return __bpf_redirect(clone, dev, flags);
2439 static const struct bpf_func_proto bpf_clone_redirect_proto = {
2440 .func = bpf_clone_redirect,
2442 .ret_type = RET_INTEGER,
2443 .arg1_type = ARG_PTR_TO_CTX,
2444 .arg2_type = ARG_ANYTHING,
2445 .arg3_type = ARG_ANYTHING,
2448 DEFINE_PER_CPU(struct bpf_redirect_info, bpf_redirect_info);
2449 EXPORT_PER_CPU_SYMBOL_GPL(bpf_redirect_info);
2451 int skb_do_redirect(struct sk_buff *skb)
2453 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
2454 struct net *net = dev_net(skb->dev);
2455 struct net_device *dev;
2456 u32 flags = ri->flags;
2458 dev = dev_get_by_index_rcu(net, ri->tgt_index);
2463 if (flags & BPF_F_PEER) {
2464 const struct net_device_ops *ops = dev->netdev_ops;
2466 if (unlikely(!ops->ndo_get_peer_dev ||
2467 !skb_at_tc_ingress(skb)))
2469 dev = ops->ndo_get_peer_dev(dev);
2470 if (unlikely(!dev ||
2471 !(dev->flags & IFF_UP) ||
2472 net_eq(net, dev_net(dev))))
2477 return flags & BPF_F_NEIGH ?
2478 __bpf_redirect_neigh(skb, dev, flags & BPF_F_NEXTHOP ?
2480 __bpf_redirect(skb, dev, flags);
2486 BPF_CALL_2(bpf_redirect, u32, ifindex, u64, flags)
2488 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
2490 if (unlikely(flags & (~(BPF_F_INGRESS) | BPF_F_REDIRECT_INTERNAL)))
2494 ri->tgt_index = ifindex;
2496 return TC_ACT_REDIRECT;
2499 static const struct bpf_func_proto bpf_redirect_proto = {
2500 .func = bpf_redirect,
2502 .ret_type = RET_INTEGER,
2503 .arg1_type = ARG_ANYTHING,
2504 .arg2_type = ARG_ANYTHING,
2507 BPF_CALL_2(bpf_redirect_peer, u32, ifindex, u64, flags)
2509 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
2511 if (unlikely(flags))
2514 ri->flags = BPF_F_PEER;
2515 ri->tgt_index = ifindex;
2517 return TC_ACT_REDIRECT;
2520 static const struct bpf_func_proto bpf_redirect_peer_proto = {
2521 .func = bpf_redirect_peer,
2523 .ret_type = RET_INTEGER,
2524 .arg1_type = ARG_ANYTHING,
2525 .arg2_type = ARG_ANYTHING,
2528 BPF_CALL_4(bpf_redirect_neigh, u32, ifindex, struct bpf_redir_neigh *, params,
2529 int, plen, u64, flags)
2531 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
2533 if (unlikely((plen && plen < sizeof(*params)) || flags))
2536 ri->flags = BPF_F_NEIGH | (plen ? BPF_F_NEXTHOP : 0);
2537 ri->tgt_index = ifindex;
2539 BUILD_BUG_ON(sizeof(struct bpf_redir_neigh) != sizeof(struct bpf_nh_params));
2541 memcpy(&ri->nh, params, sizeof(ri->nh));
2543 return TC_ACT_REDIRECT;
2546 static const struct bpf_func_proto bpf_redirect_neigh_proto = {
2547 .func = bpf_redirect_neigh,
2549 .ret_type = RET_INTEGER,
2550 .arg1_type = ARG_ANYTHING,
2551 .arg2_type = ARG_PTR_TO_MEM | PTR_MAYBE_NULL | MEM_RDONLY,
2552 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
2553 .arg4_type = ARG_ANYTHING,
2556 BPF_CALL_2(bpf_msg_apply_bytes, struct sk_msg *, msg, u32, bytes)
2558 msg->apply_bytes = bytes;
2562 static const struct bpf_func_proto bpf_msg_apply_bytes_proto = {
2563 .func = bpf_msg_apply_bytes,
2565 .ret_type = RET_INTEGER,
2566 .arg1_type = ARG_PTR_TO_CTX,
2567 .arg2_type = ARG_ANYTHING,
2570 BPF_CALL_2(bpf_msg_cork_bytes, struct sk_msg *, msg, u32, bytes)
2572 msg->cork_bytes = bytes;
2576 static const struct bpf_func_proto bpf_msg_cork_bytes_proto = {
2577 .func = bpf_msg_cork_bytes,
2579 .ret_type = RET_INTEGER,
2580 .arg1_type = ARG_PTR_TO_CTX,
2581 .arg2_type = ARG_ANYTHING,
2584 BPF_CALL_4(bpf_msg_pull_data, struct sk_msg *, msg, u32, start,
2585 u32, end, u64, flags)
2587 u32 len = 0, offset = 0, copy = 0, poffset = 0, bytes = end - start;
2588 u32 first_sge, last_sge, i, shift, bytes_sg_total;
2589 struct scatterlist *sge;
2590 u8 *raw, *to, *from;
2593 if (unlikely(flags || end <= start))
2596 /* First find the starting scatterlist element */
2600 len = sk_msg_elem(msg, i)->length;
2601 if (start < offset + len)
2603 sk_msg_iter_var_next(i);
2604 } while (i != msg->sg.end);
2606 if (unlikely(start >= offset + len))
2610 /* The start may point into the sg element so we need to also
2611 * account for the headroom.
2613 bytes_sg_total = start - offset + bytes;
2614 if (!test_bit(i, msg->sg.copy) && bytes_sg_total <= len)
2617 /* At this point we need to linearize multiple scatterlist
2618 * elements or a single shared page. Either way we need to
2619 * copy into a linear buffer exclusively owned by BPF. Then
2620 * place the buffer in the scatterlist and fixup the original
2621 * entries by removing the entries now in the linear buffer
2622 * and shifting the remaining entries. For now we do not try
2623 * to copy partial entries to avoid complexity of running out
2624 * of sg_entry slots. The downside is reading a single byte
2625 * will copy the entire sg entry.
2628 copy += sk_msg_elem(msg, i)->length;
2629 sk_msg_iter_var_next(i);
2630 if (bytes_sg_total <= copy)
2632 } while (i != msg->sg.end);
2635 if (unlikely(bytes_sg_total > copy))
2638 page = alloc_pages(__GFP_NOWARN | GFP_ATOMIC | __GFP_COMP,
2640 if (unlikely(!page))
2643 raw = page_address(page);
2646 sge = sk_msg_elem(msg, i);
2647 from = sg_virt(sge);
2651 memcpy(to, from, len);
2654 put_page(sg_page(sge));
2656 sk_msg_iter_var_next(i);
2657 } while (i != last_sge);
2659 sg_set_page(&msg->sg.data[first_sge], page, copy, 0);
2661 /* To repair sg ring we need to shift entries. If we only
2662 * had a single entry though we can just replace it and
2663 * be done. Otherwise walk the ring and shift the entries.
2665 WARN_ON_ONCE(last_sge == first_sge);
2666 shift = last_sge > first_sge ?
2667 last_sge - first_sge - 1 :
2668 NR_MSG_FRAG_IDS - first_sge + last_sge - 1;
2673 sk_msg_iter_var_next(i);
2677 if (i + shift >= NR_MSG_FRAG_IDS)
2678 move_from = i + shift - NR_MSG_FRAG_IDS;
2680 move_from = i + shift;
2681 if (move_from == msg->sg.end)
2684 msg->sg.data[i] = msg->sg.data[move_from];
2685 msg->sg.data[move_from].length = 0;
2686 msg->sg.data[move_from].page_link = 0;
2687 msg->sg.data[move_from].offset = 0;
2688 sk_msg_iter_var_next(i);
2691 msg->sg.end = msg->sg.end - shift > msg->sg.end ?
2692 msg->sg.end - shift + NR_MSG_FRAG_IDS :
2693 msg->sg.end - shift;
2695 msg->data = sg_virt(&msg->sg.data[first_sge]) + start - offset;
2696 msg->data_end = msg->data + bytes;
2700 static const struct bpf_func_proto bpf_msg_pull_data_proto = {
2701 .func = bpf_msg_pull_data,
2703 .ret_type = RET_INTEGER,
2704 .arg1_type = ARG_PTR_TO_CTX,
2705 .arg2_type = ARG_ANYTHING,
2706 .arg3_type = ARG_ANYTHING,
2707 .arg4_type = ARG_ANYTHING,
2710 BPF_CALL_4(bpf_msg_push_data, struct sk_msg *, msg, u32, start,
2711 u32, len, u64, flags)
2713 struct scatterlist sge, nsge, nnsge, rsge = {0}, *psge;
2714 u32 new, i = 0, l = 0, space, copy = 0, offset = 0;
2715 u8 *raw, *to, *from;
2718 if (unlikely(flags))
2721 if (unlikely(len == 0))
2724 /* First find the starting scatterlist element */
2728 l = sk_msg_elem(msg, i)->length;
2730 if (start < offset + l)
2732 sk_msg_iter_var_next(i);
2733 } while (i != msg->sg.end);
2735 if (start >= offset + l)
2738 space = MAX_MSG_FRAGS - sk_msg_elem_used(msg);
2740 /* If no space available will fallback to copy, we need at
2741 * least one scatterlist elem available to push data into
2742 * when start aligns to the beginning of an element or two
2743 * when it falls inside an element. We handle the start equals
2744 * offset case because its the common case for inserting a
2747 if (!space || (space == 1 && start != offset))
2748 copy = msg->sg.data[i].length;
2750 page = alloc_pages(__GFP_NOWARN | GFP_ATOMIC | __GFP_COMP,
2751 get_order(copy + len));
2752 if (unlikely(!page))
2758 raw = page_address(page);
2760 psge = sk_msg_elem(msg, i);
2761 front = start - offset;
2762 back = psge->length - front;
2763 from = sg_virt(psge);
2766 memcpy(raw, from, front);
2770 to = raw + front + len;
2772 memcpy(to, from, back);
2775 put_page(sg_page(psge));
2776 } else if (start - offset) {
2777 psge = sk_msg_elem(msg, i);
2778 rsge = sk_msg_elem_cpy(msg, i);
2780 psge->length = start - offset;
2781 rsge.length -= psge->length;
2782 rsge.offset += start;
2784 sk_msg_iter_var_next(i);
2785 sg_unmark_end(psge);
2786 sg_unmark_end(&rsge);
2787 sk_msg_iter_next(msg, end);
2790 /* Slot(s) to place newly allocated data */
2793 /* Shift one or two slots as needed */
2795 sge = sk_msg_elem_cpy(msg, i);
2797 sk_msg_iter_var_next(i);
2798 sg_unmark_end(&sge);
2799 sk_msg_iter_next(msg, end);
2801 nsge = sk_msg_elem_cpy(msg, i);
2803 sk_msg_iter_var_next(i);
2804 nnsge = sk_msg_elem_cpy(msg, i);
2807 while (i != msg->sg.end) {
2808 msg->sg.data[i] = sge;
2810 sk_msg_iter_var_next(i);
2813 nnsge = sk_msg_elem_cpy(msg, i);
2815 nsge = sk_msg_elem_cpy(msg, i);
2820 /* Place newly allocated data buffer */
2821 sk_mem_charge(msg->sk, len);
2822 msg->sg.size += len;
2823 __clear_bit(new, msg->sg.copy);
2824 sg_set_page(&msg->sg.data[new], page, len + copy, 0);
2826 get_page(sg_page(&rsge));
2827 sk_msg_iter_var_next(new);
2828 msg->sg.data[new] = rsge;
2831 sk_msg_compute_data_pointers(msg);
2835 static const struct bpf_func_proto bpf_msg_push_data_proto = {
2836 .func = bpf_msg_push_data,
2838 .ret_type = RET_INTEGER,
2839 .arg1_type = ARG_PTR_TO_CTX,
2840 .arg2_type = ARG_ANYTHING,
2841 .arg3_type = ARG_ANYTHING,
2842 .arg4_type = ARG_ANYTHING,
2845 static void sk_msg_shift_left(struct sk_msg *msg, int i)
2851 sk_msg_iter_var_next(i);
2852 msg->sg.data[prev] = msg->sg.data[i];
2853 } while (i != msg->sg.end);
2855 sk_msg_iter_prev(msg, end);
2858 static void sk_msg_shift_right(struct sk_msg *msg, int i)
2860 struct scatterlist tmp, sge;
2862 sk_msg_iter_next(msg, end);
2863 sge = sk_msg_elem_cpy(msg, i);
2864 sk_msg_iter_var_next(i);
2865 tmp = sk_msg_elem_cpy(msg, i);
2867 while (i != msg->sg.end) {
2868 msg->sg.data[i] = sge;
2869 sk_msg_iter_var_next(i);
2871 tmp = sk_msg_elem_cpy(msg, i);
2875 BPF_CALL_4(bpf_msg_pop_data, struct sk_msg *, msg, u32, start,
2876 u32, len, u64, flags)
2878 u32 i = 0, l = 0, space, offset = 0;
2879 u64 last = start + len;
2882 if (unlikely(flags))
2885 /* First find the starting scatterlist element */
2889 l = sk_msg_elem(msg, i)->length;
2891 if (start < offset + l)
2893 sk_msg_iter_var_next(i);
2894 } while (i != msg->sg.end);
2896 /* Bounds checks: start and pop must be inside message */
2897 if (start >= offset + l || last >= msg->sg.size)
2900 space = MAX_MSG_FRAGS - sk_msg_elem_used(msg);
2903 /* --------------| offset
2904 * -| start |-------- len -------|
2906 * |----- a ----|-------- pop -------|----- b ----|
2907 * |______________________________________________| length
2910 * a: region at front of scatter element to save
2911 * b: region at back of scatter element to save when length > A + pop
2912 * pop: region to pop from element, same as input 'pop' here will be
2913 * decremented below per iteration.
2915 * Two top-level cases to handle when start != offset, first B is non
2916 * zero and second B is zero corresponding to when a pop includes more
2919 * Then if B is non-zero AND there is no space allocate space and
2920 * compact A, B regions into page. If there is space shift ring to
2921 * the rigth free'ing the next element in ring to place B, leaving
2922 * A untouched except to reduce length.
2924 if (start != offset) {
2925 struct scatterlist *nsge, *sge = sk_msg_elem(msg, i);
2927 int b = sge->length - pop - a;
2929 sk_msg_iter_var_next(i);
2931 if (pop < sge->length - a) {
2934 sk_msg_shift_right(msg, i);
2935 nsge = sk_msg_elem(msg, i);
2936 get_page(sg_page(sge));
2939 b, sge->offset + pop + a);
2941 struct page *page, *orig;
2944 page = alloc_pages(__GFP_NOWARN |
2945 __GFP_COMP | GFP_ATOMIC,
2947 if (unlikely(!page))
2951 orig = sg_page(sge);
2952 from = sg_virt(sge);
2953 to = page_address(page);
2954 memcpy(to, from, a);
2955 memcpy(to + a, from + a + pop, b);
2956 sg_set_page(sge, page, a + b, 0);
2960 } else if (pop >= sge->length - a) {
2961 pop -= (sge->length - a);
2966 /* From above the current layout _must_ be as follows,
2971 * |---- pop ---|---------------- b ------------|
2972 * |____________________________________________| length
2974 * Offset and start of the current msg elem are equal because in the
2975 * previous case we handled offset != start and either consumed the
2976 * entire element and advanced to the next element OR pop == 0.
2978 * Two cases to handle here are first pop is less than the length
2979 * leaving some remainder b above. Simply adjust the element's layout
2980 * in this case. Or pop >= length of the element so that b = 0. In this
2981 * case advance to next element decrementing pop.
2984 struct scatterlist *sge = sk_msg_elem(msg, i);
2986 if (pop < sge->length) {
2992 sk_msg_shift_left(msg, i);
2994 sk_msg_iter_var_next(i);
2997 sk_mem_uncharge(msg->sk, len - pop);
2998 msg->sg.size -= (len - pop);
2999 sk_msg_compute_data_pointers(msg);
3003 static const struct bpf_func_proto bpf_msg_pop_data_proto = {
3004 .func = bpf_msg_pop_data,
3006 .ret_type = RET_INTEGER,
3007 .arg1_type = ARG_PTR_TO_CTX,
3008 .arg2_type = ARG_ANYTHING,
3009 .arg3_type = ARG_ANYTHING,
3010 .arg4_type = ARG_ANYTHING,
3013 #ifdef CONFIG_CGROUP_NET_CLASSID
3014 BPF_CALL_0(bpf_get_cgroup_classid_curr)
3016 return __task_get_classid(current);
3019 const struct bpf_func_proto bpf_get_cgroup_classid_curr_proto = {
3020 .func = bpf_get_cgroup_classid_curr,
3022 .ret_type = RET_INTEGER,
3025 BPF_CALL_1(bpf_skb_cgroup_classid, const struct sk_buff *, skb)
3027 struct sock *sk = skb_to_full_sk(skb);
3029 if (!sk || !sk_fullsock(sk))
3032 return sock_cgroup_classid(&sk->sk_cgrp_data);
3035 static const struct bpf_func_proto bpf_skb_cgroup_classid_proto = {
3036 .func = bpf_skb_cgroup_classid,
3038 .ret_type = RET_INTEGER,
3039 .arg1_type = ARG_PTR_TO_CTX,
3043 BPF_CALL_1(bpf_get_cgroup_classid, const struct sk_buff *, skb)
3045 return task_get_classid(skb);
3048 static const struct bpf_func_proto bpf_get_cgroup_classid_proto = {
3049 .func = bpf_get_cgroup_classid,
3051 .ret_type = RET_INTEGER,
3052 .arg1_type = ARG_PTR_TO_CTX,
3055 BPF_CALL_1(bpf_get_route_realm, const struct sk_buff *, skb)
3057 return dst_tclassid(skb);
3060 static const struct bpf_func_proto bpf_get_route_realm_proto = {
3061 .func = bpf_get_route_realm,
3063 .ret_type = RET_INTEGER,
3064 .arg1_type = ARG_PTR_TO_CTX,
3067 BPF_CALL_1(bpf_get_hash_recalc, struct sk_buff *, skb)
3069 /* If skb_clear_hash() was called due to mangling, we can
3070 * trigger SW recalculation here. Later access to hash
3071 * can then use the inline skb->hash via context directly
3072 * instead of calling this helper again.
3074 return skb_get_hash(skb);
3077 static const struct bpf_func_proto bpf_get_hash_recalc_proto = {
3078 .func = bpf_get_hash_recalc,
3080 .ret_type = RET_INTEGER,
3081 .arg1_type = ARG_PTR_TO_CTX,
3084 BPF_CALL_1(bpf_set_hash_invalid, struct sk_buff *, skb)
3086 /* After all direct packet write, this can be used once for
3087 * triggering a lazy recalc on next skb_get_hash() invocation.
3089 skb_clear_hash(skb);
3093 static const struct bpf_func_proto bpf_set_hash_invalid_proto = {
3094 .func = bpf_set_hash_invalid,
3096 .ret_type = RET_INTEGER,
3097 .arg1_type = ARG_PTR_TO_CTX,
3100 BPF_CALL_2(bpf_set_hash, struct sk_buff *, skb, u32, hash)
3102 /* Set user specified hash as L4(+), so that it gets returned
3103 * on skb_get_hash() call unless BPF prog later on triggers a
3106 __skb_set_sw_hash(skb, hash, true);
3110 static const struct bpf_func_proto bpf_set_hash_proto = {
3111 .func = bpf_set_hash,
3113 .ret_type = RET_INTEGER,
3114 .arg1_type = ARG_PTR_TO_CTX,
3115 .arg2_type = ARG_ANYTHING,
3118 BPF_CALL_3(bpf_skb_vlan_push, struct sk_buff *, skb, __be16, vlan_proto,
3123 if (unlikely(vlan_proto != htons(ETH_P_8021Q) &&
3124 vlan_proto != htons(ETH_P_8021AD)))
3125 vlan_proto = htons(ETH_P_8021Q);
3127 bpf_push_mac_rcsum(skb);
3128 ret = skb_vlan_push(skb, vlan_proto, vlan_tci);
3129 bpf_pull_mac_rcsum(skb);
3131 bpf_compute_data_pointers(skb);
3135 static const struct bpf_func_proto bpf_skb_vlan_push_proto = {
3136 .func = bpf_skb_vlan_push,
3138 .ret_type = RET_INTEGER,
3139 .arg1_type = ARG_PTR_TO_CTX,
3140 .arg2_type = ARG_ANYTHING,
3141 .arg3_type = ARG_ANYTHING,
3144 BPF_CALL_1(bpf_skb_vlan_pop, struct sk_buff *, skb)
3148 bpf_push_mac_rcsum(skb);
3149 ret = skb_vlan_pop(skb);
3150 bpf_pull_mac_rcsum(skb);
3152 bpf_compute_data_pointers(skb);
3156 static const struct bpf_func_proto bpf_skb_vlan_pop_proto = {
3157 .func = bpf_skb_vlan_pop,
3159 .ret_type = RET_INTEGER,
3160 .arg1_type = ARG_PTR_TO_CTX,
3163 static int bpf_skb_generic_push(struct sk_buff *skb, u32 off, u32 len)
3165 /* Caller already did skb_cow() with len as headroom,
3166 * so no need to do it here.
3169 memmove(skb->data, skb->data + len, off);
3170 memset(skb->data + off, 0, len);
3172 /* No skb_postpush_rcsum(skb, skb->data + off, len)
3173 * needed here as it does not change the skb->csum
3174 * result for checksum complete when summing over
3180 static int bpf_skb_generic_pop(struct sk_buff *skb, u32 off, u32 len)
3182 /* skb_ensure_writable() is not needed here, as we're
3183 * already working on an uncloned skb.
3185 if (unlikely(!pskb_may_pull(skb, off + len)))
3188 skb_postpull_rcsum(skb, skb->data + off, len);
3189 memmove(skb->data + len, skb->data, off);
3190 __skb_pull(skb, len);
3195 static int bpf_skb_net_hdr_push(struct sk_buff *skb, u32 off, u32 len)
3197 bool trans_same = skb->transport_header == skb->network_header;
3200 /* There's no need for __skb_push()/__skb_pull() pair to
3201 * get to the start of the mac header as we're guaranteed
3202 * to always start from here under eBPF.
3204 ret = bpf_skb_generic_push(skb, off, len);
3206 skb->mac_header -= len;
3207 skb->network_header -= len;
3209 skb->transport_header = skb->network_header;
3215 static int bpf_skb_net_hdr_pop(struct sk_buff *skb, u32 off, u32 len)
3217 bool trans_same = skb->transport_header == skb->network_header;
3220 /* Same here, __skb_push()/__skb_pull() pair not needed. */
3221 ret = bpf_skb_generic_pop(skb, off, len);
3223 skb->mac_header += len;
3224 skb->network_header += len;
3226 skb->transport_header = skb->network_header;
3232 static int bpf_skb_proto_4_to_6(struct sk_buff *skb)
3234 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
3235 u32 off = skb_mac_header_len(skb);
3238 ret = skb_cow(skb, len_diff);
3239 if (unlikely(ret < 0))
3242 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
3243 if (unlikely(ret < 0))
3246 if (skb_is_gso(skb)) {
3247 struct skb_shared_info *shinfo = skb_shinfo(skb);
3249 /* SKB_GSO_TCPV4 needs to be changed into SKB_GSO_TCPV6. */
3250 if (shinfo->gso_type & SKB_GSO_TCPV4) {
3251 shinfo->gso_type &= ~SKB_GSO_TCPV4;
3252 shinfo->gso_type |= SKB_GSO_TCPV6;
3256 skb->protocol = htons(ETH_P_IPV6);
3257 skb_clear_hash(skb);
3262 static int bpf_skb_proto_6_to_4(struct sk_buff *skb)
3264 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
3265 u32 off = skb_mac_header_len(skb);
3268 ret = skb_unclone(skb, GFP_ATOMIC);
3269 if (unlikely(ret < 0))
3272 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
3273 if (unlikely(ret < 0))
3276 if (skb_is_gso(skb)) {
3277 struct skb_shared_info *shinfo = skb_shinfo(skb);
3279 /* SKB_GSO_TCPV6 needs to be changed into SKB_GSO_TCPV4. */
3280 if (shinfo->gso_type & SKB_GSO_TCPV6) {
3281 shinfo->gso_type &= ~SKB_GSO_TCPV6;
3282 shinfo->gso_type |= SKB_GSO_TCPV4;
3286 skb->protocol = htons(ETH_P_IP);
3287 skb_clear_hash(skb);
3292 static int bpf_skb_proto_xlat(struct sk_buff *skb, __be16 to_proto)
3294 __be16 from_proto = skb->protocol;
3296 if (from_proto == htons(ETH_P_IP) &&
3297 to_proto == htons(ETH_P_IPV6))
3298 return bpf_skb_proto_4_to_6(skb);
3300 if (from_proto == htons(ETH_P_IPV6) &&
3301 to_proto == htons(ETH_P_IP))
3302 return bpf_skb_proto_6_to_4(skb);
3307 BPF_CALL_3(bpf_skb_change_proto, struct sk_buff *, skb, __be16, proto,
3312 if (unlikely(flags))
3315 /* General idea is that this helper does the basic groundwork
3316 * needed for changing the protocol, and eBPF program fills the
3317 * rest through bpf_skb_store_bytes(), bpf_lX_csum_replace()
3318 * and other helpers, rather than passing a raw buffer here.
3320 * The rationale is to keep this minimal and without a need to
3321 * deal with raw packet data. F.e. even if we would pass buffers
3322 * here, the program still needs to call the bpf_lX_csum_replace()
3323 * helpers anyway. Plus, this way we keep also separation of
3324 * concerns, since f.e. bpf_skb_store_bytes() should only take
3327 * Currently, additional options and extension header space are
3328 * not supported, but flags register is reserved so we can adapt
3329 * that. For offloads, we mark packet as dodgy, so that headers
3330 * need to be verified first.
3332 ret = bpf_skb_proto_xlat(skb, proto);
3333 bpf_compute_data_pointers(skb);
3337 static const struct bpf_func_proto bpf_skb_change_proto_proto = {
3338 .func = bpf_skb_change_proto,
3340 .ret_type = RET_INTEGER,
3341 .arg1_type = ARG_PTR_TO_CTX,
3342 .arg2_type = ARG_ANYTHING,
3343 .arg3_type = ARG_ANYTHING,
3346 BPF_CALL_2(bpf_skb_change_type, struct sk_buff *, skb, u32, pkt_type)
3348 /* We only allow a restricted subset to be changed for now. */
3349 if (unlikely(!skb_pkt_type_ok(skb->pkt_type) ||
3350 !skb_pkt_type_ok(pkt_type)))
3353 skb->pkt_type = pkt_type;
3357 static const struct bpf_func_proto bpf_skb_change_type_proto = {
3358 .func = bpf_skb_change_type,
3360 .ret_type = RET_INTEGER,
3361 .arg1_type = ARG_PTR_TO_CTX,
3362 .arg2_type = ARG_ANYTHING,
3365 static u32 bpf_skb_net_base_len(const struct sk_buff *skb)
3367 switch (skb->protocol) {
3368 case htons(ETH_P_IP):
3369 return sizeof(struct iphdr);
3370 case htons(ETH_P_IPV6):
3371 return sizeof(struct ipv6hdr);
3377 #define BPF_F_ADJ_ROOM_ENCAP_L3_MASK (BPF_F_ADJ_ROOM_ENCAP_L3_IPV4 | \
3378 BPF_F_ADJ_ROOM_ENCAP_L3_IPV6)
3380 #define BPF_F_ADJ_ROOM_MASK (BPF_F_ADJ_ROOM_FIXED_GSO | \
3381 BPF_F_ADJ_ROOM_ENCAP_L3_MASK | \
3382 BPF_F_ADJ_ROOM_ENCAP_L4_GRE | \
3383 BPF_F_ADJ_ROOM_ENCAP_L4_UDP | \
3384 BPF_F_ADJ_ROOM_ENCAP_L2_ETH | \
3385 BPF_F_ADJ_ROOM_ENCAP_L2( \
3386 BPF_ADJ_ROOM_ENCAP_L2_MASK))
3388 static int bpf_skb_net_grow(struct sk_buff *skb, u32 off, u32 len_diff,
3391 u8 inner_mac_len = flags >> BPF_ADJ_ROOM_ENCAP_L2_SHIFT;
3392 bool encap = flags & BPF_F_ADJ_ROOM_ENCAP_L3_MASK;
3393 u16 mac_len = 0, inner_net = 0, inner_trans = 0;
3394 unsigned int gso_type = SKB_GSO_DODGY;
3397 if (skb_is_gso(skb) && !skb_is_gso_tcp(skb)) {
3398 /* udp gso_size delineates datagrams, only allow if fixed */
3399 if (!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) ||
3400 !(flags & BPF_F_ADJ_ROOM_FIXED_GSO))
3404 ret = skb_cow_head(skb, len_diff);
3405 if (unlikely(ret < 0))
3409 if (skb->protocol != htons(ETH_P_IP) &&
3410 skb->protocol != htons(ETH_P_IPV6))
3413 if (flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV4 &&
3414 flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV6)
3417 if (flags & BPF_F_ADJ_ROOM_ENCAP_L4_GRE &&
3418 flags & BPF_F_ADJ_ROOM_ENCAP_L4_UDP)
3421 if (flags & BPF_F_ADJ_ROOM_ENCAP_L2_ETH &&
3422 inner_mac_len < ETH_HLEN)
3425 if (skb->encapsulation)
3428 mac_len = skb->network_header - skb->mac_header;
3429 inner_net = skb->network_header;
3430 if (inner_mac_len > len_diff)
3432 inner_trans = skb->transport_header;
3435 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
3436 if (unlikely(ret < 0))
3440 skb->inner_mac_header = inner_net - inner_mac_len;
3441 skb->inner_network_header = inner_net;
3442 skb->inner_transport_header = inner_trans;
3444 if (flags & BPF_F_ADJ_ROOM_ENCAP_L2_ETH)
3445 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
3447 skb_set_inner_protocol(skb, skb->protocol);
3449 skb->encapsulation = 1;
3450 skb_set_network_header(skb, mac_len);
3452 if (flags & BPF_F_ADJ_ROOM_ENCAP_L4_UDP)
3453 gso_type |= SKB_GSO_UDP_TUNNEL;
3454 else if (flags & BPF_F_ADJ_ROOM_ENCAP_L4_GRE)
3455 gso_type |= SKB_GSO_GRE;
3456 else if (flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV6)
3457 gso_type |= SKB_GSO_IPXIP6;
3458 else if (flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV4)
3459 gso_type |= SKB_GSO_IPXIP4;
3461 if (flags & BPF_F_ADJ_ROOM_ENCAP_L4_GRE ||
3462 flags & BPF_F_ADJ_ROOM_ENCAP_L4_UDP) {
3463 int nh_len = flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV6 ?
3464 sizeof(struct ipv6hdr) :
3465 sizeof(struct iphdr);
3467 skb_set_transport_header(skb, mac_len + nh_len);
3470 /* Match skb->protocol to new outer l3 protocol */
3471 if (skb->protocol == htons(ETH_P_IP) &&
3472 flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV6)
3473 skb->protocol = htons(ETH_P_IPV6);
3474 else if (skb->protocol == htons(ETH_P_IPV6) &&
3475 flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV4)
3476 skb->protocol = htons(ETH_P_IP);
3479 if (skb_is_gso(skb)) {
3480 struct skb_shared_info *shinfo = skb_shinfo(skb);
3482 /* Due to header grow, MSS needs to be downgraded. */
3483 if (!(flags & BPF_F_ADJ_ROOM_FIXED_GSO))
3484 skb_decrease_gso_size(shinfo, len_diff);
3486 /* Header must be checked, and gso_segs recomputed. */
3487 shinfo->gso_type |= gso_type;
3488 shinfo->gso_segs = 0;
3494 static int bpf_skb_net_shrink(struct sk_buff *skb, u32 off, u32 len_diff,
3499 if (unlikely(flags & ~(BPF_F_ADJ_ROOM_FIXED_GSO |
3500 BPF_F_ADJ_ROOM_NO_CSUM_RESET)))
3503 if (skb_is_gso(skb) && !skb_is_gso_tcp(skb)) {
3504 /* udp gso_size delineates datagrams, only allow if fixed */
3505 if (!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) ||
3506 !(flags & BPF_F_ADJ_ROOM_FIXED_GSO))
3510 ret = skb_unclone(skb, GFP_ATOMIC);
3511 if (unlikely(ret < 0))
3514 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
3515 if (unlikely(ret < 0))
3518 if (skb_is_gso(skb)) {
3519 struct skb_shared_info *shinfo = skb_shinfo(skb);
3521 /* Due to header shrink, MSS can be upgraded. */
3522 if (!(flags & BPF_F_ADJ_ROOM_FIXED_GSO))
3523 skb_increase_gso_size(shinfo, len_diff);
3525 /* Header must be checked, and gso_segs recomputed. */
3526 shinfo->gso_type |= SKB_GSO_DODGY;
3527 shinfo->gso_segs = 0;
3533 #define BPF_SKB_MAX_LEN SKB_MAX_ALLOC
3535 BPF_CALL_4(sk_skb_adjust_room, struct sk_buff *, skb, s32, len_diff,
3536 u32, mode, u64, flags)
3538 u32 len_diff_abs = abs(len_diff);
3539 bool shrink = len_diff < 0;
3542 if (unlikely(flags || mode))
3544 if (unlikely(len_diff_abs > 0xfffU))
3548 ret = skb_cow(skb, len_diff);
3549 if (unlikely(ret < 0))
3551 __skb_push(skb, len_diff_abs);
3552 memset(skb->data, 0, len_diff_abs);
3554 if (unlikely(!pskb_may_pull(skb, len_diff_abs)))
3556 __skb_pull(skb, len_diff_abs);
3558 if (tls_sw_has_ctx_rx(skb->sk)) {
3559 struct strp_msg *rxm = strp_msg(skb);
3561 rxm->full_len += len_diff;
3566 static const struct bpf_func_proto sk_skb_adjust_room_proto = {
3567 .func = sk_skb_adjust_room,
3569 .ret_type = RET_INTEGER,
3570 .arg1_type = ARG_PTR_TO_CTX,
3571 .arg2_type = ARG_ANYTHING,
3572 .arg3_type = ARG_ANYTHING,
3573 .arg4_type = ARG_ANYTHING,
3576 BPF_CALL_4(bpf_skb_adjust_room, struct sk_buff *, skb, s32, len_diff,
3577 u32, mode, u64, flags)
3579 u32 len_cur, len_diff_abs = abs(len_diff);
3580 u32 len_min = bpf_skb_net_base_len(skb);
3581 u32 len_max = BPF_SKB_MAX_LEN;
3582 __be16 proto = skb->protocol;
3583 bool shrink = len_diff < 0;
3587 if (unlikely(flags & ~(BPF_F_ADJ_ROOM_MASK |
3588 BPF_F_ADJ_ROOM_NO_CSUM_RESET)))
3590 if (unlikely(len_diff_abs > 0xfffU))
3592 if (unlikely(proto != htons(ETH_P_IP) &&
3593 proto != htons(ETH_P_IPV6)))
3596 off = skb_mac_header_len(skb);
3598 case BPF_ADJ_ROOM_NET:
3599 off += bpf_skb_net_base_len(skb);
3601 case BPF_ADJ_ROOM_MAC:
3607 len_cur = skb->len - skb_network_offset(skb);
3608 if ((shrink && (len_diff_abs >= len_cur ||
3609 len_cur - len_diff_abs < len_min)) ||
3610 (!shrink && (skb->len + len_diff_abs > len_max &&
3614 ret = shrink ? bpf_skb_net_shrink(skb, off, len_diff_abs, flags) :
3615 bpf_skb_net_grow(skb, off, len_diff_abs, flags);
3616 if (!ret && !(flags & BPF_F_ADJ_ROOM_NO_CSUM_RESET))
3617 __skb_reset_checksum_unnecessary(skb);
3619 bpf_compute_data_pointers(skb);
3623 static const struct bpf_func_proto bpf_skb_adjust_room_proto = {
3624 .func = bpf_skb_adjust_room,
3626 .ret_type = RET_INTEGER,
3627 .arg1_type = ARG_PTR_TO_CTX,
3628 .arg2_type = ARG_ANYTHING,
3629 .arg3_type = ARG_ANYTHING,
3630 .arg4_type = ARG_ANYTHING,
3633 static u32 __bpf_skb_min_len(const struct sk_buff *skb)
3635 u32 min_len = skb_network_offset(skb);
3637 if (skb_transport_header_was_set(skb))
3638 min_len = skb_transport_offset(skb);
3639 if (skb->ip_summed == CHECKSUM_PARTIAL)
3640 min_len = skb_checksum_start_offset(skb) +
3641 skb->csum_offset + sizeof(__sum16);
3645 static int bpf_skb_grow_rcsum(struct sk_buff *skb, unsigned int new_len)
3647 unsigned int old_len = skb->len;
3650 ret = __skb_grow_rcsum(skb, new_len);
3652 memset(skb->data + old_len, 0, new_len - old_len);
3656 static int bpf_skb_trim_rcsum(struct sk_buff *skb, unsigned int new_len)
3658 return __skb_trim_rcsum(skb, new_len);
3661 static inline int __bpf_skb_change_tail(struct sk_buff *skb, u32 new_len,
3664 u32 max_len = BPF_SKB_MAX_LEN;
3665 u32 min_len = __bpf_skb_min_len(skb);
3668 if (unlikely(flags || new_len > max_len || new_len < min_len))
3670 if (skb->encapsulation)
3673 /* The basic idea of this helper is that it's performing the
3674 * needed work to either grow or trim an skb, and eBPF program
3675 * rewrites the rest via helpers like bpf_skb_store_bytes(),
3676 * bpf_lX_csum_replace() and others rather than passing a raw
3677 * buffer here. This one is a slow path helper and intended
3678 * for replies with control messages.
3680 * Like in bpf_skb_change_proto(), we want to keep this rather
3681 * minimal and without protocol specifics so that we are able
3682 * to separate concerns as in bpf_skb_store_bytes() should only
3683 * be the one responsible for writing buffers.
3685 * It's really expected to be a slow path operation here for
3686 * control message replies, so we're implicitly linearizing,
3687 * uncloning and drop offloads from the skb by this.
3689 ret = __bpf_try_make_writable(skb, skb->len);
3691 if (new_len > skb->len)
3692 ret = bpf_skb_grow_rcsum(skb, new_len);
3693 else if (new_len < skb->len)
3694 ret = bpf_skb_trim_rcsum(skb, new_len);
3695 if (!ret && skb_is_gso(skb))
3701 BPF_CALL_3(bpf_skb_change_tail, struct sk_buff *, skb, u32, new_len,
3704 int ret = __bpf_skb_change_tail(skb, new_len, flags);
3706 bpf_compute_data_pointers(skb);
3710 static const struct bpf_func_proto bpf_skb_change_tail_proto = {
3711 .func = bpf_skb_change_tail,
3713 .ret_type = RET_INTEGER,
3714 .arg1_type = ARG_PTR_TO_CTX,
3715 .arg2_type = ARG_ANYTHING,
3716 .arg3_type = ARG_ANYTHING,
3719 BPF_CALL_3(sk_skb_change_tail, struct sk_buff *, skb, u32, new_len,
3722 return __bpf_skb_change_tail(skb, new_len, flags);
3725 static const struct bpf_func_proto sk_skb_change_tail_proto = {
3726 .func = sk_skb_change_tail,
3728 .ret_type = RET_INTEGER,
3729 .arg1_type = ARG_PTR_TO_CTX,
3730 .arg2_type = ARG_ANYTHING,
3731 .arg3_type = ARG_ANYTHING,
3734 static inline int __bpf_skb_change_head(struct sk_buff *skb, u32 head_room,
3737 u32 max_len = BPF_SKB_MAX_LEN;
3738 u32 new_len = skb->len + head_room;
3741 if (unlikely(flags || (!skb_is_gso(skb) && new_len > max_len) ||
3742 new_len < skb->len))
3745 ret = skb_cow(skb, head_room);
3747 /* Idea for this helper is that we currently only
3748 * allow to expand on mac header. This means that
3749 * skb->protocol network header, etc, stay as is.
3750 * Compared to bpf_skb_change_tail(), we're more
3751 * flexible due to not needing to linearize or
3752 * reset GSO. Intention for this helper is to be
3753 * used by an L3 skb that needs to push mac header
3754 * for redirection into L2 device.
3756 __skb_push(skb, head_room);
3757 memset(skb->data, 0, head_room);
3758 skb_reset_mac_header(skb);
3759 skb_reset_mac_len(skb);
3765 BPF_CALL_3(bpf_skb_change_head, struct sk_buff *, skb, u32, head_room,
3768 int ret = __bpf_skb_change_head(skb, head_room, flags);
3770 bpf_compute_data_pointers(skb);
3774 static const struct bpf_func_proto bpf_skb_change_head_proto = {
3775 .func = bpf_skb_change_head,
3777 .ret_type = RET_INTEGER,
3778 .arg1_type = ARG_PTR_TO_CTX,
3779 .arg2_type = ARG_ANYTHING,
3780 .arg3_type = ARG_ANYTHING,
3783 BPF_CALL_3(sk_skb_change_head, struct sk_buff *, skb, u32, head_room,
3786 return __bpf_skb_change_head(skb, head_room, flags);
3789 static const struct bpf_func_proto sk_skb_change_head_proto = {
3790 .func = sk_skb_change_head,
3792 .ret_type = RET_INTEGER,
3793 .arg1_type = ARG_PTR_TO_CTX,
3794 .arg2_type = ARG_ANYTHING,
3795 .arg3_type = ARG_ANYTHING,
3798 BPF_CALL_1(bpf_xdp_get_buff_len, struct xdp_buff*, xdp)
3800 return xdp_get_buff_len(xdp);
3803 static const struct bpf_func_proto bpf_xdp_get_buff_len_proto = {
3804 .func = bpf_xdp_get_buff_len,
3806 .ret_type = RET_INTEGER,
3807 .arg1_type = ARG_PTR_TO_CTX,
3810 BTF_ID_LIST_SINGLE(bpf_xdp_get_buff_len_bpf_ids, struct, xdp_buff)
3812 const struct bpf_func_proto bpf_xdp_get_buff_len_trace_proto = {
3813 .func = bpf_xdp_get_buff_len,
3815 .arg1_type = ARG_PTR_TO_BTF_ID,
3816 .arg1_btf_id = &bpf_xdp_get_buff_len_bpf_ids[0],
3819 static unsigned long xdp_get_metalen(const struct xdp_buff *xdp)
3821 return xdp_data_meta_unsupported(xdp) ? 0 :
3822 xdp->data - xdp->data_meta;
3825 BPF_CALL_2(bpf_xdp_adjust_head, struct xdp_buff *, xdp, int, offset)
3827 void *xdp_frame_end = xdp->data_hard_start + sizeof(struct xdp_frame);
3828 unsigned long metalen = xdp_get_metalen(xdp);
3829 void *data_start = xdp_frame_end + metalen;
3830 void *data = xdp->data + offset;
3832 if (unlikely(data < data_start ||
3833 data > xdp->data_end - ETH_HLEN))
3837 memmove(xdp->data_meta + offset,
3838 xdp->data_meta, metalen);
3839 xdp->data_meta += offset;
3845 static const struct bpf_func_proto bpf_xdp_adjust_head_proto = {
3846 .func = bpf_xdp_adjust_head,
3848 .ret_type = RET_INTEGER,
3849 .arg1_type = ARG_PTR_TO_CTX,
3850 .arg2_type = ARG_ANYTHING,
3853 static void bpf_xdp_copy_buf(struct xdp_buff *xdp, unsigned long off,
3854 void *buf, unsigned long len, bool flush)
3856 unsigned long ptr_len, ptr_off = 0;
3857 skb_frag_t *next_frag, *end_frag;
3858 struct skb_shared_info *sinfo;
3862 if (likely(xdp->data_end - xdp->data >= off + len)) {
3863 src = flush ? buf : xdp->data + off;
3864 dst = flush ? xdp->data + off : buf;
3865 memcpy(dst, src, len);
3869 sinfo = xdp_get_shared_info_from_buff(xdp);
3870 end_frag = &sinfo->frags[sinfo->nr_frags];
3871 next_frag = &sinfo->frags[0];
3873 ptr_len = xdp->data_end - xdp->data;
3874 ptr_buf = xdp->data;
3877 if (off < ptr_off + ptr_len) {
3878 unsigned long copy_off = off - ptr_off;
3879 unsigned long copy_len = min(len, ptr_len - copy_off);
3881 src = flush ? buf : ptr_buf + copy_off;
3882 dst = flush ? ptr_buf + copy_off : buf;
3883 memcpy(dst, src, copy_len);
3890 if (!len || next_frag == end_frag)
3894 ptr_buf = skb_frag_address(next_frag);
3895 ptr_len = skb_frag_size(next_frag);
3900 static void *bpf_xdp_pointer(struct xdp_buff *xdp, u32 offset, u32 len)
3902 struct skb_shared_info *sinfo = xdp_get_shared_info_from_buff(xdp);
3903 u32 size = xdp->data_end - xdp->data;
3904 void *addr = xdp->data;
3907 if (unlikely(offset > 0xffff || len > 0xffff))
3908 return ERR_PTR(-EFAULT);
3910 if (offset + len > xdp_get_buff_len(xdp))
3911 return ERR_PTR(-EINVAL);
3913 if (offset < size) /* linear area */
3917 for (i = 0; i < sinfo->nr_frags; i++) { /* paged area */
3918 u32 frag_size = skb_frag_size(&sinfo->frags[i]);
3920 if (offset < frag_size) {
3921 addr = skb_frag_address(&sinfo->frags[i]);
3925 offset -= frag_size;
3928 return offset + len <= size ? addr + offset : NULL;
3931 BPF_CALL_4(bpf_xdp_load_bytes, struct xdp_buff *, xdp, u32, offset,
3932 void *, buf, u32, len)
3936 ptr = bpf_xdp_pointer(xdp, offset, len);
3938 return PTR_ERR(ptr);
3941 bpf_xdp_copy_buf(xdp, offset, buf, len, false);
3943 memcpy(buf, ptr, len);
3948 static const struct bpf_func_proto bpf_xdp_load_bytes_proto = {
3949 .func = bpf_xdp_load_bytes,
3951 .ret_type = RET_INTEGER,
3952 .arg1_type = ARG_PTR_TO_CTX,
3953 .arg2_type = ARG_ANYTHING,
3954 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
3955 .arg4_type = ARG_CONST_SIZE,
3958 BPF_CALL_4(bpf_xdp_store_bytes, struct xdp_buff *, xdp, u32, offset,
3959 void *, buf, u32, len)
3963 ptr = bpf_xdp_pointer(xdp, offset, len);
3965 return PTR_ERR(ptr);
3968 bpf_xdp_copy_buf(xdp, offset, buf, len, true);
3970 memcpy(ptr, buf, len);
3975 static const struct bpf_func_proto bpf_xdp_store_bytes_proto = {
3976 .func = bpf_xdp_store_bytes,
3978 .ret_type = RET_INTEGER,
3979 .arg1_type = ARG_PTR_TO_CTX,
3980 .arg2_type = ARG_ANYTHING,
3981 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
3982 .arg4_type = ARG_CONST_SIZE,
3985 static int bpf_xdp_frags_increase_tail(struct xdp_buff *xdp, int offset)
3987 struct skb_shared_info *sinfo = xdp_get_shared_info_from_buff(xdp);
3988 skb_frag_t *frag = &sinfo->frags[sinfo->nr_frags - 1];
3989 struct xdp_rxq_info *rxq = xdp->rxq;
3990 unsigned int tailroom;
3992 if (!rxq->frag_size || rxq->frag_size > xdp->frame_sz)
3995 tailroom = rxq->frag_size - skb_frag_size(frag) - skb_frag_off(frag);
3996 if (unlikely(offset > tailroom))
3999 memset(skb_frag_address(frag) + skb_frag_size(frag), 0, offset);
4000 skb_frag_size_add(frag, offset);
4001 sinfo->xdp_frags_size += offset;
4006 static int bpf_xdp_frags_shrink_tail(struct xdp_buff *xdp, int offset)
4008 struct skb_shared_info *sinfo = xdp_get_shared_info_from_buff(xdp);
4009 int i, n_frags_free = 0, len_free = 0;
4011 if (unlikely(offset > (int)xdp_get_buff_len(xdp) - ETH_HLEN))
4014 for (i = sinfo->nr_frags - 1; i >= 0 && offset > 0; i--) {
4015 skb_frag_t *frag = &sinfo->frags[i];
4016 int shrink = min_t(int, offset, skb_frag_size(frag));
4021 if (skb_frag_size(frag) == shrink) {
4022 struct page *page = skb_frag_page(frag);
4024 __xdp_return(page_address(page), &xdp->rxq->mem,
4028 skb_frag_size_sub(frag, shrink);
4032 sinfo->nr_frags -= n_frags_free;
4033 sinfo->xdp_frags_size -= len_free;
4035 if (unlikely(!sinfo->nr_frags)) {
4036 xdp_buff_clear_frags_flag(xdp);
4037 xdp->data_end -= offset;
4043 BPF_CALL_2(bpf_xdp_adjust_tail, struct xdp_buff *, xdp, int, offset)
4045 void *data_hard_end = xdp_data_hard_end(xdp); /* use xdp->frame_sz */
4046 void *data_end = xdp->data_end + offset;
4048 if (unlikely(xdp_buff_has_frags(xdp))) { /* non-linear xdp buff */
4050 return bpf_xdp_frags_shrink_tail(xdp, -offset);
4052 return bpf_xdp_frags_increase_tail(xdp, offset);
4055 /* Notice that xdp_data_hard_end have reserved some tailroom */
4056 if (unlikely(data_end > data_hard_end))
4059 /* ALL drivers MUST init xdp->frame_sz, chicken check below */
4060 if (unlikely(xdp->frame_sz > PAGE_SIZE)) {
4061 WARN_ONCE(1, "Too BIG xdp->frame_sz = %d\n", xdp->frame_sz);
4065 if (unlikely(data_end < xdp->data + ETH_HLEN))
4068 /* Clear memory area on grow, can contain uninit kernel memory */
4070 memset(xdp->data_end, 0, offset);
4072 xdp->data_end = data_end;
4077 static const struct bpf_func_proto bpf_xdp_adjust_tail_proto = {
4078 .func = bpf_xdp_adjust_tail,
4080 .ret_type = RET_INTEGER,
4081 .arg1_type = ARG_PTR_TO_CTX,
4082 .arg2_type = ARG_ANYTHING,
4085 BPF_CALL_2(bpf_xdp_adjust_meta, struct xdp_buff *, xdp, int, offset)
4087 void *xdp_frame_end = xdp->data_hard_start + sizeof(struct xdp_frame);
4088 void *meta = xdp->data_meta + offset;
4089 unsigned long metalen = xdp->data - meta;
4091 if (xdp_data_meta_unsupported(xdp))
4093 if (unlikely(meta < xdp_frame_end ||
4096 if (unlikely(xdp_metalen_invalid(metalen)))
4099 xdp->data_meta = meta;
4104 static const struct bpf_func_proto bpf_xdp_adjust_meta_proto = {
4105 .func = bpf_xdp_adjust_meta,
4107 .ret_type = RET_INTEGER,
4108 .arg1_type = ARG_PTR_TO_CTX,
4109 .arg2_type = ARG_ANYTHING,
4115 * XDP_REDIRECT works by a three-step process, implemented in the functions
4118 * 1. The bpf_redirect() and bpf_redirect_map() helpers will lookup the target
4119 * of the redirect and store it (along with some other metadata) in a per-CPU
4120 * struct bpf_redirect_info.
4122 * 2. When the program returns the XDP_REDIRECT return code, the driver will
4123 * call xdp_do_redirect() which will use the information in struct
4124 * bpf_redirect_info to actually enqueue the frame into a map type-specific
4125 * bulk queue structure.
4127 * 3. Before exiting its NAPI poll loop, the driver will call xdp_do_flush(),
4128 * which will flush all the different bulk queues, thus completing the
4132 * Pointers to the map entries will be kept around for this whole sequence of
4133 * steps, protected by RCU. However, there is no top-level rcu_read_lock() in
4134 * the core code; instead, the RCU protection relies on everything happening
4135 * inside a single NAPI poll sequence, which means it's between a pair of calls
4136 * to local_bh_disable()/local_bh_enable().
4138 * The map entries are marked as __rcu and the map code makes sure to
4139 * dereference those pointers with rcu_dereference_check() in a way that works
4140 * for both sections that to hold an rcu_read_lock() and sections that are
4141 * called from NAPI without a separate rcu_read_lock(). The code below does not
4142 * use RCU annotations, but relies on those in the map code.
4144 void xdp_do_flush(void)
4150 EXPORT_SYMBOL_GPL(xdp_do_flush);
4152 void bpf_clear_redirect_map(struct bpf_map *map)
4154 struct bpf_redirect_info *ri;
4157 for_each_possible_cpu(cpu) {
4158 ri = per_cpu_ptr(&bpf_redirect_info, cpu);
4159 /* Avoid polluting remote cacheline due to writes if
4160 * not needed. Once we pass this test, we need the
4161 * cmpxchg() to make sure it hasn't been changed in
4162 * the meantime by remote CPU.
4164 if (unlikely(READ_ONCE(ri->map) == map))
4165 cmpxchg(&ri->map, map, NULL);
4169 DEFINE_STATIC_KEY_FALSE(bpf_master_redirect_enabled_key);
4170 EXPORT_SYMBOL_GPL(bpf_master_redirect_enabled_key);
4172 u32 xdp_master_redirect(struct xdp_buff *xdp)
4174 struct net_device *master, *slave;
4175 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
4177 master = netdev_master_upper_dev_get_rcu(xdp->rxq->dev);
4178 slave = master->netdev_ops->ndo_xdp_get_xmit_slave(master, xdp);
4179 if (slave && slave != xdp->rxq->dev) {
4180 /* The target device is different from the receiving device, so
4181 * redirect it to the new device.
4182 * Using XDP_REDIRECT gets the correct behaviour from XDP enabled
4183 * drivers to unmap the packet from their rx ring.
4185 ri->tgt_index = slave->ifindex;
4186 ri->map_id = INT_MAX;
4187 ri->map_type = BPF_MAP_TYPE_UNSPEC;
4188 return XDP_REDIRECT;
4192 EXPORT_SYMBOL_GPL(xdp_master_redirect);
4194 static inline int __xdp_do_redirect_xsk(struct bpf_redirect_info *ri,
4195 struct net_device *dev,
4196 struct xdp_buff *xdp,
4197 struct bpf_prog *xdp_prog)
4199 enum bpf_map_type map_type = ri->map_type;
4200 void *fwd = ri->tgt_value;
4201 u32 map_id = ri->map_id;
4204 ri->map_id = 0; /* Valid map id idr range: [1,INT_MAX[ */
4205 ri->map_type = BPF_MAP_TYPE_UNSPEC;
4207 err = __xsk_map_redirect(fwd, xdp);
4211 _trace_xdp_redirect_map(dev, xdp_prog, fwd, map_type, map_id, ri->tgt_index);
4214 _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map_type, map_id, ri->tgt_index, err);
4218 static __always_inline int __xdp_do_redirect_frame(struct bpf_redirect_info *ri,
4219 struct net_device *dev,
4220 struct xdp_frame *xdpf,
4221 struct bpf_prog *xdp_prog)
4223 enum bpf_map_type map_type = ri->map_type;
4224 void *fwd = ri->tgt_value;
4225 u32 map_id = ri->map_id;
4226 struct bpf_map *map;
4229 ri->map_id = 0; /* Valid map id idr range: [1,INT_MAX[ */
4230 ri->map_type = BPF_MAP_TYPE_UNSPEC;
4232 if (unlikely(!xdpf)) {
4238 case BPF_MAP_TYPE_DEVMAP:
4240 case BPF_MAP_TYPE_DEVMAP_HASH:
4241 map = READ_ONCE(ri->map);
4242 if (unlikely(map)) {
4243 WRITE_ONCE(ri->map, NULL);
4244 err = dev_map_enqueue_multi(xdpf, dev, map,
4245 ri->flags & BPF_F_EXCLUDE_INGRESS);
4247 err = dev_map_enqueue(fwd, xdpf, dev);
4250 case BPF_MAP_TYPE_CPUMAP:
4251 err = cpu_map_enqueue(fwd, xdpf, dev);
4253 case BPF_MAP_TYPE_UNSPEC:
4254 if (map_id == INT_MAX) {
4255 fwd = dev_get_by_index_rcu(dev_net(dev), ri->tgt_index);
4256 if (unlikely(!fwd)) {
4260 err = dev_xdp_enqueue(fwd, xdpf, dev);
4271 _trace_xdp_redirect_map(dev, xdp_prog, fwd, map_type, map_id, ri->tgt_index);
4274 _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map_type, map_id, ri->tgt_index, err);
4278 int xdp_do_redirect(struct net_device *dev, struct xdp_buff *xdp,
4279 struct bpf_prog *xdp_prog)
4281 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
4282 enum bpf_map_type map_type = ri->map_type;
4284 /* XDP_REDIRECT is not fully supported yet for xdp frags since
4285 * not all XDP capable drivers can map non-linear xdp_frame in
4288 if (unlikely(xdp_buff_has_frags(xdp) &&
4289 map_type != BPF_MAP_TYPE_CPUMAP))
4292 if (map_type == BPF_MAP_TYPE_XSKMAP)
4293 return __xdp_do_redirect_xsk(ri, dev, xdp, xdp_prog);
4295 return __xdp_do_redirect_frame(ri, dev, xdp_convert_buff_to_frame(xdp),
4298 EXPORT_SYMBOL_GPL(xdp_do_redirect);
4300 int xdp_do_redirect_frame(struct net_device *dev, struct xdp_buff *xdp,
4301 struct xdp_frame *xdpf, struct bpf_prog *xdp_prog)
4303 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
4304 enum bpf_map_type map_type = ri->map_type;
4306 if (map_type == BPF_MAP_TYPE_XSKMAP)
4307 return __xdp_do_redirect_xsk(ri, dev, xdp, xdp_prog);
4309 return __xdp_do_redirect_frame(ri, dev, xdpf, xdp_prog);
4311 EXPORT_SYMBOL_GPL(xdp_do_redirect_frame);
4313 static int xdp_do_generic_redirect_map(struct net_device *dev,
4314 struct sk_buff *skb,
4315 struct xdp_buff *xdp,
4316 struct bpf_prog *xdp_prog,
4318 enum bpf_map_type map_type, u32 map_id)
4320 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
4321 struct bpf_map *map;
4325 case BPF_MAP_TYPE_DEVMAP:
4327 case BPF_MAP_TYPE_DEVMAP_HASH:
4328 map = READ_ONCE(ri->map);
4329 if (unlikely(map)) {
4330 WRITE_ONCE(ri->map, NULL);
4331 err = dev_map_redirect_multi(dev, skb, xdp_prog, map,
4332 ri->flags & BPF_F_EXCLUDE_INGRESS);
4334 err = dev_map_generic_redirect(fwd, skb, xdp_prog);
4339 case BPF_MAP_TYPE_XSKMAP:
4340 err = xsk_generic_rcv(fwd, xdp);
4345 case BPF_MAP_TYPE_CPUMAP:
4346 err = cpu_map_generic_redirect(fwd, skb);
4355 _trace_xdp_redirect_map(dev, xdp_prog, fwd, map_type, map_id, ri->tgt_index);
4358 _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map_type, map_id, ri->tgt_index, err);
4362 int xdp_do_generic_redirect(struct net_device *dev, struct sk_buff *skb,
4363 struct xdp_buff *xdp, struct bpf_prog *xdp_prog)
4365 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
4366 enum bpf_map_type map_type = ri->map_type;
4367 void *fwd = ri->tgt_value;
4368 u32 map_id = ri->map_id;
4371 ri->map_id = 0; /* Valid map id idr range: [1,INT_MAX[ */
4372 ri->map_type = BPF_MAP_TYPE_UNSPEC;
4374 if (map_type == BPF_MAP_TYPE_UNSPEC && map_id == INT_MAX) {
4375 fwd = dev_get_by_index_rcu(dev_net(dev), ri->tgt_index);
4376 if (unlikely(!fwd)) {
4381 err = xdp_ok_fwd_dev(fwd, skb->len);
4386 _trace_xdp_redirect(dev, xdp_prog, ri->tgt_index);
4387 generic_xdp_tx(skb, xdp_prog);
4391 return xdp_do_generic_redirect_map(dev, skb, xdp, xdp_prog, fwd, map_type, map_id);
4393 _trace_xdp_redirect_err(dev, xdp_prog, ri->tgt_index, err);
4397 BPF_CALL_2(bpf_xdp_redirect, u32, ifindex, u64, flags)
4399 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
4401 if (unlikely(flags))
4404 /* NB! Map type UNSPEC and map_id == INT_MAX (never generated
4405 * by map_idr) is used for ifindex based XDP redirect.
4407 ri->tgt_index = ifindex;
4408 ri->map_id = INT_MAX;
4409 ri->map_type = BPF_MAP_TYPE_UNSPEC;
4411 return XDP_REDIRECT;
4414 static const struct bpf_func_proto bpf_xdp_redirect_proto = {
4415 .func = bpf_xdp_redirect,
4417 .ret_type = RET_INTEGER,
4418 .arg1_type = ARG_ANYTHING,
4419 .arg2_type = ARG_ANYTHING,
4422 BPF_CALL_3(bpf_xdp_redirect_map, struct bpf_map *, map, u64, key,
4425 return map->ops->map_redirect(map, key, flags);
4428 static const struct bpf_func_proto bpf_xdp_redirect_map_proto = {
4429 .func = bpf_xdp_redirect_map,
4431 .ret_type = RET_INTEGER,
4432 .arg1_type = ARG_CONST_MAP_PTR,
4433 .arg2_type = ARG_ANYTHING,
4434 .arg3_type = ARG_ANYTHING,
4437 static unsigned long bpf_skb_copy(void *dst_buff, const void *skb,
4438 unsigned long off, unsigned long len)
4440 void *ptr = skb_header_pointer(skb, off, len, dst_buff);
4444 if (ptr != dst_buff)
4445 memcpy(dst_buff, ptr, len);
4450 BPF_CALL_5(bpf_skb_event_output, struct sk_buff *, skb, struct bpf_map *, map,
4451 u64, flags, void *, meta, u64, meta_size)
4453 u64 skb_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
4455 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
4457 if (unlikely(!skb || skb_size > skb->len))
4460 return bpf_event_output(map, flags, meta, meta_size, skb, skb_size,
4464 static const struct bpf_func_proto bpf_skb_event_output_proto = {
4465 .func = bpf_skb_event_output,
4467 .ret_type = RET_INTEGER,
4468 .arg1_type = ARG_PTR_TO_CTX,
4469 .arg2_type = ARG_CONST_MAP_PTR,
4470 .arg3_type = ARG_ANYTHING,
4471 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
4472 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
4475 BTF_ID_LIST_SINGLE(bpf_skb_output_btf_ids, struct, sk_buff)
4477 const struct bpf_func_proto bpf_skb_output_proto = {
4478 .func = bpf_skb_event_output,
4480 .ret_type = RET_INTEGER,
4481 .arg1_type = ARG_PTR_TO_BTF_ID,
4482 .arg1_btf_id = &bpf_skb_output_btf_ids[0],
4483 .arg2_type = ARG_CONST_MAP_PTR,
4484 .arg3_type = ARG_ANYTHING,
4485 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
4486 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
4489 static unsigned short bpf_tunnel_key_af(u64 flags)
4491 return flags & BPF_F_TUNINFO_IPV6 ? AF_INET6 : AF_INET;
4494 BPF_CALL_4(bpf_skb_get_tunnel_key, struct sk_buff *, skb, struct bpf_tunnel_key *, to,
4495 u32, size, u64, flags)
4497 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
4498 u8 compat[sizeof(struct bpf_tunnel_key)];
4502 if (unlikely(!info || (flags & ~(BPF_F_TUNINFO_IPV6 |
4503 BPF_F_TUNINFO_FLAGS)))) {
4507 if (ip_tunnel_info_af(info) != bpf_tunnel_key_af(flags)) {
4511 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
4514 case offsetof(struct bpf_tunnel_key, local_ipv6[0]):
4515 case offsetof(struct bpf_tunnel_key, tunnel_label):
4516 case offsetof(struct bpf_tunnel_key, tunnel_ext):
4518 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
4519 /* Fixup deprecated structure layouts here, so we have
4520 * a common path later on.
4522 if (ip_tunnel_info_af(info) != AF_INET)
4525 to = (struct bpf_tunnel_key *)compat;
4532 to->tunnel_id = be64_to_cpu(info->key.tun_id);
4533 to->tunnel_tos = info->key.tos;
4534 to->tunnel_ttl = info->key.ttl;
4535 if (flags & BPF_F_TUNINFO_FLAGS)
4536 to->tunnel_flags = info->key.tun_flags;
4540 if (flags & BPF_F_TUNINFO_IPV6) {
4541 memcpy(to->remote_ipv6, &info->key.u.ipv6.src,
4542 sizeof(to->remote_ipv6));
4543 memcpy(to->local_ipv6, &info->key.u.ipv6.dst,
4544 sizeof(to->local_ipv6));
4545 to->tunnel_label = be32_to_cpu(info->key.label);
4547 to->remote_ipv4 = be32_to_cpu(info->key.u.ipv4.src);
4548 memset(&to->remote_ipv6[1], 0, sizeof(__u32) * 3);
4549 to->local_ipv4 = be32_to_cpu(info->key.u.ipv4.dst);
4550 memset(&to->local_ipv6[1], 0, sizeof(__u32) * 3);
4551 to->tunnel_label = 0;
4554 if (unlikely(size != sizeof(struct bpf_tunnel_key)))
4555 memcpy(to_orig, to, size);
4559 memset(to_orig, 0, size);
4563 static const struct bpf_func_proto bpf_skb_get_tunnel_key_proto = {
4564 .func = bpf_skb_get_tunnel_key,
4566 .ret_type = RET_INTEGER,
4567 .arg1_type = ARG_PTR_TO_CTX,
4568 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
4569 .arg3_type = ARG_CONST_SIZE,
4570 .arg4_type = ARG_ANYTHING,
4573 BPF_CALL_3(bpf_skb_get_tunnel_opt, struct sk_buff *, skb, u8 *, to, u32, size)
4575 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
4578 if (unlikely(!info ||
4579 !(info->key.tun_flags & TUNNEL_OPTIONS_PRESENT))) {
4583 if (unlikely(size < info->options_len)) {
4588 ip_tunnel_info_opts_get(to, info);
4589 if (size > info->options_len)
4590 memset(to + info->options_len, 0, size - info->options_len);
4592 return info->options_len;
4594 memset(to, 0, size);
4598 static const struct bpf_func_proto bpf_skb_get_tunnel_opt_proto = {
4599 .func = bpf_skb_get_tunnel_opt,
4601 .ret_type = RET_INTEGER,
4602 .arg1_type = ARG_PTR_TO_CTX,
4603 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
4604 .arg3_type = ARG_CONST_SIZE,
4607 static struct metadata_dst __percpu *md_dst;
4609 BPF_CALL_4(bpf_skb_set_tunnel_key, struct sk_buff *, skb,
4610 const struct bpf_tunnel_key *, from, u32, size, u64, flags)
4612 struct metadata_dst *md = this_cpu_ptr(md_dst);
4613 u8 compat[sizeof(struct bpf_tunnel_key)];
4614 struct ip_tunnel_info *info;
4616 if (unlikely(flags & ~(BPF_F_TUNINFO_IPV6 | BPF_F_ZERO_CSUM_TX |
4617 BPF_F_DONT_FRAGMENT | BPF_F_SEQ_NUMBER)))
4619 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
4621 case offsetof(struct bpf_tunnel_key, local_ipv6[0]):
4622 case offsetof(struct bpf_tunnel_key, tunnel_label):
4623 case offsetof(struct bpf_tunnel_key, tunnel_ext):
4624 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
4625 /* Fixup deprecated structure layouts here, so we have
4626 * a common path later on.
4628 memcpy(compat, from, size);
4629 memset(compat + size, 0, sizeof(compat) - size);
4630 from = (const struct bpf_tunnel_key *) compat;
4636 if (unlikely((!(flags & BPF_F_TUNINFO_IPV6) && from->tunnel_label) ||
4641 dst_hold((struct dst_entry *) md);
4642 skb_dst_set(skb, (struct dst_entry *) md);
4644 info = &md->u.tun_info;
4645 memset(info, 0, sizeof(*info));
4646 info->mode = IP_TUNNEL_INFO_TX;
4648 info->key.tun_flags = TUNNEL_KEY | TUNNEL_CSUM | TUNNEL_NOCACHE;
4649 if (flags & BPF_F_DONT_FRAGMENT)
4650 info->key.tun_flags |= TUNNEL_DONT_FRAGMENT;
4651 if (flags & BPF_F_ZERO_CSUM_TX)
4652 info->key.tun_flags &= ~TUNNEL_CSUM;
4653 if (flags & BPF_F_SEQ_NUMBER)
4654 info->key.tun_flags |= TUNNEL_SEQ;
4656 info->key.tun_id = cpu_to_be64(from->tunnel_id);
4657 info->key.tos = from->tunnel_tos;
4658 info->key.ttl = from->tunnel_ttl;
4660 if (flags & BPF_F_TUNINFO_IPV6) {
4661 info->mode |= IP_TUNNEL_INFO_IPV6;
4662 memcpy(&info->key.u.ipv6.dst, from->remote_ipv6,
4663 sizeof(from->remote_ipv6));
4664 memcpy(&info->key.u.ipv6.src, from->local_ipv6,
4665 sizeof(from->local_ipv6));
4666 info->key.label = cpu_to_be32(from->tunnel_label) &
4667 IPV6_FLOWLABEL_MASK;
4669 info->key.u.ipv4.dst = cpu_to_be32(from->remote_ipv4);
4670 info->key.u.ipv4.src = cpu_to_be32(from->local_ipv4);
4671 info->key.flow_flags = FLOWI_FLAG_ANYSRC;
4677 static const struct bpf_func_proto bpf_skb_set_tunnel_key_proto = {
4678 .func = bpf_skb_set_tunnel_key,
4680 .ret_type = RET_INTEGER,
4681 .arg1_type = ARG_PTR_TO_CTX,
4682 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
4683 .arg3_type = ARG_CONST_SIZE,
4684 .arg4_type = ARG_ANYTHING,
4687 BPF_CALL_3(bpf_skb_set_tunnel_opt, struct sk_buff *, skb,
4688 const u8 *, from, u32, size)
4690 struct ip_tunnel_info *info = skb_tunnel_info(skb);
4691 const struct metadata_dst *md = this_cpu_ptr(md_dst);
4693 if (unlikely(info != &md->u.tun_info || (size & (sizeof(u32) - 1))))
4695 if (unlikely(size > IP_TUNNEL_OPTS_MAX))
4698 ip_tunnel_info_opts_set(info, from, size, TUNNEL_OPTIONS_PRESENT);
4703 static const struct bpf_func_proto bpf_skb_set_tunnel_opt_proto = {
4704 .func = bpf_skb_set_tunnel_opt,
4706 .ret_type = RET_INTEGER,
4707 .arg1_type = ARG_PTR_TO_CTX,
4708 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
4709 .arg3_type = ARG_CONST_SIZE,
4712 static const struct bpf_func_proto *
4713 bpf_get_skb_set_tunnel_proto(enum bpf_func_id which)
4716 struct metadata_dst __percpu *tmp;
4718 tmp = metadata_dst_alloc_percpu(IP_TUNNEL_OPTS_MAX,
4723 if (cmpxchg(&md_dst, NULL, tmp))
4724 metadata_dst_free_percpu(tmp);
4728 case BPF_FUNC_skb_set_tunnel_key:
4729 return &bpf_skb_set_tunnel_key_proto;
4730 case BPF_FUNC_skb_set_tunnel_opt:
4731 return &bpf_skb_set_tunnel_opt_proto;
4737 BPF_CALL_3(bpf_skb_under_cgroup, struct sk_buff *, skb, struct bpf_map *, map,
4740 struct bpf_array *array = container_of(map, struct bpf_array, map);
4741 struct cgroup *cgrp;
4744 sk = skb_to_full_sk(skb);
4745 if (!sk || !sk_fullsock(sk))
4747 if (unlikely(idx >= array->map.max_entries))
4750 cgrp = READ_ONCE(array->ptrs[idx]);
4751 if (unlikely(!cgrp))
4754 return sk_under_cgroup_hierarchy(sk, cgrp);
4757 static const struct bpf_func_proto bpf_skb_under_cgroup_proto = {
4758 .func = bpf_skb_under_cgroup,
4760 .ret_type = RET_INTEGER,
4761 .arg1_type = ARG_PTR_TO_CTX,
4762 .arg2_type = ARG_CONST_MAP_PTR,
4763 .arg3_type = ARG_ANYTHING,
4766 #ifdef CONFIG_SOCK_CGROUP_DATA
4767 static inline u64 __bpf_sk_cgroup_id(struct sock *sk)
4769 struct cgroup *cgrp;
4771 sk = sk_to_full_sk(sk);
4772 if (!sk || !sk_fullsock(sk))
4775 cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
4776 return cgroup_id(cgrp);
4779 BPF_CALL_1(bpf_skb_cgroup_id, const struct sk_buff *, skb)
4781 return __bpf_sk_cgroup_id(skb->sk);
4784 static const struct bpf_func_proto bpf_skb_cgroup_id_proto = {
4785 .func = bpf_skb_cgroup_id,
4787 .ret_type = RET_INTEGER,
4788 .arg1_type = ARG_PTR_TO_CTX,
4791 static inline u64 __bpf_sk_ancestor_cgroup_id(struct sock *sk,
4794 struct cgroup *ancestor;
4795 struct cgroup *cgrp;
4797 sk = sk_to_full_sk(sk);
4798 if (!sk || !sk_fullsock(sk))
4801 cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
4802 ancestor = cgroup_ancestor(cgrp, ancestor_level);
4806 return cgroup_id(ancestor);
4809 BPF_CALL_2(bpf_skb_ancestor_cgroup_id, const struct sk_buff *, skb, int,
4812 return __bpf_sk_ancestor_cgroup_id(skb->sk, ancestor_level);
4815 static const struct bpf_func_proto bpf_skb_ancestor_cgroup_id_proto = {
4816 .func = bpf_skb_ancestor_cgroup_id,
4818 .ret_type = RET_INTEGER,
4819 .arg1_type = ARG_PTR_TO_CTX,
4820 .arg2_type = ARG_ANYTHING,
4823 BPF_CALL_1(bpf_sk_cgroup_id, struct sock *, sk)
4825 return __bpf_sk_cgroup_id(sk);
4828 static const struct bpf_func_proto bpf_sk_cgroup_id_proto = {
4829 .func = bpf_sk_cgroup_id,
4831 .ret_type = RET_INTEGER,
4832 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
4835 BPF_CALL_2(bpf_sk_ancestor_cgroup_id, struct sock *, sk, int, ancestor_level)
4837 return __bpf_sk_ancestor_cgroup_id(sk, ancestor_level);
4840 static const struct bpf_func_proto bpf_sk_ancestor_cgroup_id_proto = {
4841 .func = bpf_sk_ancestor_cgroup_id,
4843 .ret_type = RET_INTEGER,
4844 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
4845 .arg2_type = ARG_ANYTHING,
4849 static unsigned long bpf_xdp_copy(void *dst, const void *ctx,
4850 unsigned long off, unsigned long len)
4852 struct xdp_buff *xdp = (struct xdp_buff *)ctx;
4854 bpf_xdp_copy_buf(xdp, off, dst, len, false);
4858 BPF_CALL_5(bpf_xdp_event_output, struct xdp_buff *, xdp, struct bpf_map *, map,
4859 u64, flags, void *, meta, u64, meta_size)
4861 u64 xdp_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
4863 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
4866 if (unlikely(!xdp || xdp_size > xdp_get_buff_len(xdp)))
4869 return bpf_event_output(map, flags, meta, meta_size, xdp,
4870 xdp_size, bpf_xdp_copy);
4873 static const struct bpf_func_proto bpf_xdp_event_output_proto = {
4874 .func = bpf_xdp_event_output,
4876 .ret_type = RET_INTEGER,
4877 .arg1_type = ARG_PTR_TO_CTX,
4878 .arg2_type = ARG_CONST_MAP_PTR,
4879 .arg3_type = ARG_ANYTHING,
4880 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
4881 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
4884 BTF_ID_LIST_SINGLE(bpf_xdp_output_btf_ids, struct, xdp_buff)
4886 const struct bpf_func_proto bpf_xdp_output_proto = {
4887 .func = bpf_xdp_event_output,
4889 .ret_type = RET_INTEGER,
4890 .arg1_type = ARG_PTR_TO_BTF_ID,
4891 .arg1_btf_id = &bpf_xdp_output_btf_ids[0],
4892 .arg2_type = ARG_CONST_MAP_PTR,
4893 .arg3_type = ARG_ANYTHING,
4894 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
4895 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
4898 BPF_CALL_1(bpf_get_socket_cookie, struct sk_buff *, skb)
4900 return skb->sk ? __sock_gen_cookie(skb->sk) : 0;
4903 static const struct bpf_func_proto bpf_get_socket_cookie_proto = {
4904 .func = bpf_get_socket_cookie,
4906 .ret_type = RET_INTEGER,
4907 .arg1_type = ARG_PTR_TO_CTX,
4910 BPF_CALL_1(bpf_get_socket_cookie_sock_addr, struct bpf_sock_addr_kern *, ctx)
4912 return __sock_gen_cookie(ctx->sk);
4915 static const struct bpf_func_proto bpf_get_socket_cookie_sock_addr_proto = {
4916 .func = bpf_get_socket_cookie_sock_addr,
4918 .ret_type = RET_INTEGER,
4919 .arg1_type = ARG_PTR_TO_CTX,
4922 BPF_CALL_1(bpf_get_socket_cookie_sock, struct sock *, ctx)
4924 return __sock_gen_cookie(ctx);
4927 static const struct bpf_func_proto bpf_get_socket_cookie_sock_proto = {
4928 .func = bpf_get_socket_cookie_sock,
4930 .ret_type = RET_INTEGER,
4931 .arg1_type = ARG_PTR_TO_CTX,
4934 BPF_CALL_1(bpf_get_socket_ptr_cookie, struct sock *, sk)
4936 return sk ? sock_gen_cookie(sk) : 0;
4939 const struct bpf_func_proto bpf_get_socket_ptr_cookie_proto = {
4940 .func = bpf_get_socket_ptr_cookie,
4942 .ret_type = RET_INTEGER,
4943 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
4946 BPF_CALL_1(bpf_get_socket_cookie_sock_ops, struct bpf_sock_ops_kern *, ctx)
4948 return __sock_gen_cookie(ctx->sk);
4951 static const struct bpf_func_proto bpf_get_socket_cookie_sock_ops_proto = {
4952 .func = bpf_get_socket_cookie_sock_ops,
4954 .ret_type = RET_INTEGER,
4955 .arg1_type = ARG_PTR_TO_CTX,
4958 static u64 __bpf_get_netns_cookie(struct sock *sk)
4960 const struct net *net = sk ? sock_net(sk) : &init_net;
4962 return net->net_cookie;
4965 BPF_CALL_1(bpf_get_netns_cookie_sock, struct sock *, ctx)
4967 return __bpf_get_netns_cookie(ctx);
4970 static const struct bpf_func_proto bpf_get_netns_cookie_sock_proto = {
4971 .func = bpf_get_netns_cookie_sock,
4973 .ret_type = RET_INTEGER,
4974 .arg1_type = ARG_PTR_TO_CTX_OR_NULL,
4977 BPF_CALL_1(bpf_get_netns_cookie_sock_addr, struct bpf_sock_addr_kern *, ctx)
4979 return __bpf_get_netns_cookie(ctx ? ctx->sk : NULL);
4982 static const struct bpf_func_proto bpf_get_netns_cookie_sock_addr_proto = {
4983 .func = bpf_get_netns_cookie_sock_addr,
4985 .ret_type = RET_INTEGER,
4986 .arg1_type = ARG_PTR_TO_CTX_OR_NULL,
4989 BPF_CALL_1(bpf_get_netns_cookie_sock_ops, struct bpf_sock_ops_kern *, ctx)
4991 return __bpf_get_netns_cookie(ctx ? ctx->sk : NULL);
4994 static const struct bpf_func_proto bpf_get_netns_cookie_sock_ops_proto = {
4995 .func = bpf_get_netns_cookie_sock_ops,
4997 .ret_type = RET_INTEGER,
4998 .arg1_type = ARG_PTR_TO_CTX_OR_NULL,
5001 BPF_CALL_1(bpf_get_netns_cookie_sk_msg, struct sk_msg *, ctx)
5003 return __bpf_get_netns_cookie(ctx ? ctx->sk : NULL);
5006 static const struct bpf_func_proto bpf_get_netns_cookie_sk_msg_proto = {
5007 .func = bpf_get_netns_cookie_sk_msg,
5009 .ret_type = RET_INTEGER,
5010 .arg1_type = ARG_PTR_TO_CTX_OR_NULL,
5013 BPF_CALL_1(bpf_get_socket_uid, struct sk_buff *, skb)
5015 struct sock *sk = sk_to_full_sk(skb->sk);
5018 if (!sk || !sk_fullsock(sk))
5020 kuid = sock_net_uid(sock_net(sk), sk);
5021 return from_kuid_munged(sock_net(sk)->user_ns, kuid);
5024 static const struct bpf_func_proto bpf_get_socket_uid_proto = {
5025 .func = bpf_get_socket_uid,
5027 .ret_type = RET_INTEGER,
5028 .arg1_type = ARG_PTR_TO_CTX,
5031 static int sol_socket_sockopt(struct sock *sk, int optname,
5032 char *optval, int *optlen,
5044 case SO_MAX_PACING_RATE:
5045 case SO_BINDTOIFINDEX:
5047 if (*optlen != sizeof(int))
5050 case SO_BINDTODEVICE:
5057 if (optname == SO_BINDTODEVICE)
5059 return sk_getsockopt(sk, SOL_SOCKET, optname,
5060 KERNEL_SOCKPTR(optval),
5061 KERNEL_SOCKPTR(optlen));
5064 return sk_setsockopt(sk, SOL_SOCKET, optname,
5065 KERNEL_SOCKPTR(optval), *optlen);
5068 static int bpf_sol_tcp_setsockopt(struct sock *sk, int optname,
5069 char *optval, int optlen)
5071 struct tcp_sock *tp = tcp_sk(sk);
5072 unsigned long timeout;
5075 if (optlen != sizeof(int))
5078 val = *(int *)optval;
5080 /* Only some options are supported */
5083 if (val <= 0 || tp->data_segs_out > tp->syn_data)
5085 tcp_snd_cwnd_set(tp, val);
5087 case TCP_BPF_SNDCWND_CLAMP:
5090 tp->snd_cwnd_clamp = val;
5091 tp->snd_ssthresh = val;
5093 case TCP_BPF_DELACK_MAX:
5094 timeout = usecs_to_jiffies(val);
5095 if (timeout > TCP_DELACK_MAX ||
5096 timeout < TCP_TIMEOUT_MIN)
5098 inet_csk(sk)->icsk_delack_max = timeout;
5100 case TCP_BPF_RTO_MIN:
5101 timeout = usecs_to_jiffies(val);
5102 if (timeout > TCP_RTO_MIN ||
5103 timeout < TCP_TIMEOUT_MIN)
5105 inet_csk(sk)->icsk_rto_min = timeout;
5114 static int sol_tcp_sockopt_congestion(struct sock *sk, char *optval,
5115 int *optlen, bool getopt)
5117 struct tcp_sock *tp;
5124 if (!inet_csk(sk)->icsk_ca_ops)
5126 /* BPF expects NULL-terminated tcp-cc string */
5127 optval[--(*optlen)] = '\0';
5128 return do_tcp_getsockopt(sk, SOL_TCP, TCP_CONGESTION,
5129 KERNEL_SOCKPTR(optval),
5130 KERNEL_SOCKPTR(optlen));
5133 /* "cdg" is the only cc that alloc a ptr
5134 * in inet_csk_ca area. The bpf-tcp-cc may
5135 * overwrite this ptr after switching to cdg.
5137 if (*optlen >= sizeof("cdg") - 1 && !strncmp("cdg", optval, *optlen))
5140 /* It stops this looping
5142 * .init => bpf_setsockopt(tcp_cc) => .init =>
5143 * bpf_setsockopt(tcp_cc)" => .init => ....
5145 * The second bpf_setsockopt(tcp_cc) is not allowed
5146 * in order to break the loop when both .init
5147 * are the same bpf prog.
5149 * This applies even the second bpf_setsockopt(tcp_cc)
5150 * does not cause a loop. This limits only the first
5151 * '.init' can call bpf_setsockopt(TCP_CONGESTION) to
5152 * pick a fallback cc (eg. peer does not support ECN)
5153 * and the second '.init' cannot fallback to
5157 if (tp->bpf_chg_cc_inprogress)
5160 tp->bpf_chg_cc_inprogress = 1;
5161 ret = do_tcp_setsockopt(sk, SOL_TCP, TCP_CONGESTION,
5162 KERNEL_SOCKPTR(optval), *optlen);
5163 tp->bpf_chg_cc_inprogress = 0;
5167 static int sol_tcp_sockopt(struct sock *sk, int optname,
5168 char *optval, int *optlen,
5171 if (sk->sk_prot->setsockopt != tcp_setsockopt)
5181 case TCP_WINDOW_CLAMP:
5182 case TCP_THIN_LINEAR_TIMEOUTS:
5183 case TCP_USER_TIMEOUT:
5184 case TCP_NOTSENT_LOWAT:
5186 if (*optlen != sizeof(int))
5189 case TCP_CONGESTION:
5190 return sol_tcp_sockopt_congestion(sk, optval, optlen, getopt);
5198 return bpf_sol_tcp_setsockopt(sk, optname, optval, *optlen);
5202 if (optname == TCP_SAVED_SYN) {
5203 struct tcp_sock *tp = tcp_sk(sk);
5205 if (!tp->saved_syn ||
5206 *optlen > tcp_saved_syn_len(tp->saved_syn))
5208 memcpy(optval, tp->saved_syn->data, *optlen);
5209 /* It cannot free tp->saved_syn here because it
5210 * does not know if the user space still needs it.
5215 return do_tcp_getsockopt(sk, SOL_TCP, optname,
5216 KERNEL_SOCKPTR(optval),
5217 KERNEL_SOCKPTR(optlen));
5220 return do_tcp_setsockopt(sk, SOL_TCP, optname,
5221 KERNEL_SOCKPTR(optval), *optlen);
5224 static int sol_ip_sockopt(struct sock *sk, int optname,
5225 char *optval, int *optlen,
5228 if (sk->sk_family != AF_INET)
5233 if (*optlen != sizeof(int))
5241 return do_ip_getsockopt(sk, SOL_IP, optname,
5242 KERNEL_SOCKPTR(optval),
5243 KERNEL_SOCKPTR(optlen));
5245 return do_ip_setsockopt(sk, SOL_IP, optname,
5246 KERNEL_SOCKPTR(optval), *optlen);
5249 static int sol_ipv6_sockopt(struct sock *sk, int optname,
5250 char *optval, int *optlen,
5253 if (sk->sk_family != AF_INET6)
5258 case IPV6_AUTOFLOWLABEL:
5259 if (*optlen != sizeof(int))
5267 return ipv6_bpf_stub->ipv6_getsockopt(sk, SOL_IPV6, optname,
5268 KERNEL_SOCKPTR(optval),
5269 KERNEL_SOCKPTR(optlen));
5271 return ipv6_bpf_stub->ipv6_setsockopt(sk, SOL_IPV6, optname,
5272 KERNEL_SOCKPTR(optval), *optlen);
5275 static int __bpf_setsockopt(struct sock *sk, int level, int optname,
5276 char *optval, int optlen)
5278 if (!sk_fullsock(sk))
5281 if (level == SOL_SOCKET)
5282 return sol_socket_sockopt(sk, optname, optval, &optlen, false);
5283 else if (IS_ENABLED(CONFIG_INET) && level == SOL_IP)
5284 return sol_ip_sockopt(sk, optname, optval, &optlen, false);
5285 else if (IS_ENABLED(CONFIG_IPV6) && level == SOL_IPV6)
5286 return sol_ipv6_sockopt(sk, optname, optval, &optlen, false);
5287 else if (IS_ENABLED(CONFIG_INET) && level == SOL_TCP)
5288 return sol_tcp_sockopt(sk, optname, optval, &optlen, false);
5293 static int _bpf_setsockopt(struct sock *sk, int level, int optname,
5294 char *optval, int optlen)
5296 if (sk_fullsock(sk))
5297 sock_owned_by_me(sk);
5298 return __bpf_setsockopt(sk, level, optname, optval, optlen);
5301 static int __bpf_getsockopt(struct sock *sk, int level, int optname,
5302 char *optval, int optlen)
5304 int err, saved_optlen = optlen;
5306 if (!sk_fullsock(sk)) {
5311 if (level == SOL_SOCKET)
5312 err = sol_socket_sockopt(sk, optname, optval, &optlen, true);
5313 else if (IS_ENABLED(CONFIG_INET) && level == SOL_TCP)
5314 err = sol_tcp_sockopt(sk, optname, optval, &optlen, true);
5315 else if (IS_ENABLED(CONFIG_INET) && level == SOL_IP)
5316 err = sol_ip_sockopt(sk, optname, optval, &optlen, true);
5317 else if (IS_ENABLED(CONFIG_IPV6) && level == SOL_IPV6)
5318 err = sol_ipv6_sockopt(sk, optname, optval, &optlen, true);
5325 if (optlen < saved_optlen)
5326 memset(optval + optlen, 0, saved_optlen - optlen);
5330 static int _bpf_getsockopt(struct sock *sk, int level, int optname,
5331 char *optval, int optlen)
5333 if (sk_fullsock(sk))
5334 sock_owned_by_me(sk);
5335 return __bpf_getsockopt(sk, level, optname, optval, optlen);
5338 BPF_CALL_5(bpf_sk_setsockopt, struct sock *, sk, int, level,
5339 int, optname, char *, optval, int, optlen)
5341 return _bpf_setsockopt(sk, level, optname, optval, optlen);
5344 const struct bpf_func_proto bpf_sk_setsockopt_proto = {
5345 .func = bpf_sk_setsockopt,
5347 .ret_type = RET_INTEGER,
5348 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
5349 .arg2_type = ARG_ANYTHING,
5350 .arg3_type = ARG_ANYTHING,
5351 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
5352 .arg5_type = ARG_CONST_SIZE,
5355 BPF_CALL_5(bpf_sk_getsockopt, struct sock *, sk, int, level,
5356 int, optname, char *, optval, int, optlen)
5358 return _bpf_getsockopt(sk, level, optname, optval, optlen);
5361 const struct bpf_func_proto bpf_sk_getsockopt_proto = {
5362 .func = bpf_sk_getsockopt,
5364 .ret_type = RET_INTEGER,
5365 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
5366 .arg2_type = ARG_ANYTHING,
5367 .arg3_type = ARG_ANYTHING,
5368 .arg4_type = ARG_PTR_TO_UNINIT_MEM,
5369 .arg5_type = ARG_CONST_SIZE,
5372 BPF_CALL_5(bpf_unlocked_sk_setsockopt, struct sock *, sk, int, level,
5373 int, optname, char *, optval, int, optlen)
5375 return __bpf_setsockopt(sk, level, optname, optval, optlen);
5378 const struct bpf_func_proto bpf_unlocked_sk_setsockopt_proto = {
5379 .func = bpf_unlocked_sk_setsockopt,
5381 .ret_type = RET_INTEGER,
5382 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
5383 .arg2_type = ARG_ANYTHING,
5384 .arg3_type = ARG_ANYTHING,
5385 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
5386 .arg5_type = ARG_CONST_SIZE,
5389 BPF_CALL_5(bpf_unlocked_sk_getsockopt, struct sock *, sk, int, level,
5390 int, optname, char *, optval, int, optlen)
5392 return __bpf_getsockopt(sk, level, optname, optval, optlen);
5395 const struct bpf_func_proto bpf_unlocked_sk_getsockopt_proto = {
5396 .func = bpf_unlocked_sk_getsockopt,
5398 .ret_type = RET_INTEGER,
5399 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
5400 .arg2_type = ARG_ANYTHING,
5401 .arg3_type = ARG_ANYTHING,
5402 .arg4_type = ARG_PTR_TO_UNINIT_MEM,
5403 .arg5_type = ARG_CONST_SIZE,
5406 BPF_CALL_5(bpf_sock_addr_setsockopt, struct bpf_sock_addr_kern *, ctx,
5407 int, level, int, optname, char *, optval, int, optlen)
5409 return _bpf_setsockopt(ctx->sk, level, optname, optval, optlen);
5412 static const struct bpf_func_proto bpf_sock_addr_setsockopt_proto = {
5413 .func = bpf_sock_addr_setsockopt,
5415 .ret_type = RET_INTEGER,
5416 .arg1_type = ARG_PTR_TO_CTX,
5417 .arg2_type = ARG_ANYTHING,
5418 .arg3_type = ARG_ANYTHING,
5419 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
5420 .arg5_type = ARG_CONST_SIZE,
5423 BPF_CALL_5(bpf_sock_addr_getsockopt, struct bpf_sock_addr_kern *, ctx,
5424 int, level, int, optname, char *, optval, int, optlen)
5426 return _bpf_getsockopt(ctx->sk, level, optname, optval, optlen);
5429 static const struct bpf_func_proto bpf_sock_addr_getsockopt_proto = {
5430 .func = bpf_sock_addr_getsockopt,
5432 .ret_type = RET_INTEGER,
5433 .arg1_type = ARG_PTR_TO_CTX,
5434 .arg2_type = ARG_ANYTHING,
5435 .arg3_type = ARG_ANYTHING,
5436 .arg4_type = ARG_PTR_TO_UNINIT_MEM,
5437 .arg5_type = ARG_CONST_SIZE,
5440 BPF_CALL_5(bpf_sock_ops_setsockopt, struct bpf_sock_ops_kern *, bpf_sock,
5441 int, level, int, optname, char *, optval, int, optlen)
5443 return _bpf_setsockopt(bpf_sock->sk, level, optname, optval, optlen);
5446 static const struct bpf_func_proto bpf_sock_ops_setsockopt_proto = {
5447 .func = bpf_sock_ops_setsockopt,
5449 .ret_type = RET_INTEGER,
5450 .arg1_type = ARG_PTR_TO_CTX,
5451 .arg2_type = ARG_ANYTHING,
5452 .arg3_type = ARG_ANYTHING,
5453 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
5454 .arg5_type = ARG_CONST_SIZE,
5457 static int bpf_sock_ops_get_syn(struct bpf_sock_ops_kern *bpf_sock,
5458 int optname, const u8 **start)
5460 struct sk_buff *syn_skb = bpf_sock->syn_skb;
5461 const u8 *hdr_start;
5465 /* sk is a request_sock here */
5467 if (optname == TCP_BPF_SYN) {
5468 hdr_start = syn_skb->data;
5469 ret = tcp_hdrlen(syn_skb);
5470 } else if (optname == TCP_BPF_SYN_IP) {
5471 hdr_start = skb_network_header(syn_skb);
5472 ret = skb_network_header_len(syn_skb) +
5473 tcp_hdrlen(syn_skb);
5475 /* optname == TCP_BPF_SYN_MAC */
5476 hdr_start = skb_mac_header(syn_skb);
5477 ret = skb_mac_header_len(syn_skb) +
5478 skb_network_header_len(syn_skb) +
5479 tcp_hdrlen(syn_skb);
5482 struct sock *sk = bpf_sock->sk;
5483 struct saved_syn *saved_syn;
5485 if (sk->sk_state == TCP_NEW_SYN_RECV)
5486 /* synack retransmit. bpf_sock->syn_skb will
5487 * not be available. It has to resort to
5488 * saved_syn (if it is saved).
5490 saved_syn = inet_reqsk(sk)->saved_syn;
5492 saved_syn = tcp_sk(sk)->saved_syn;
5497 if (optname == TCP_BPF_SYN) {
5498 hdr_start = saved_syn->data +
5499 saved_syn->mac_hdrlen +
5500 saved_syn->network_hdrlen;
5501 ret = saved_syn->tcp_hdrlen;
5502 } else if (optname == TCP_BPF_SYN_IP) {
5503 hdr_start = saved_syn->data +
5504 saved_syn->mac_hdrlen;
5505 ret = saved_syn->network_hdrlen +
5506 saved_syn->tcp_hdrlen;
5508 /* optname == TCP_BPF_SYN_MAC */
5510 /* TCP_SAVE_SYN may not have saved the mac hdr */
5511 if (!saved_syn->mac_hdrlen)
5514 hdr_start = saved_syn->data;
5515 ret = saved_syn->mac_hdrlen +
5516 saved_syn->network_hdrlen +
5517 saved_syn->tcp_hdrlen;
5525 BPF_CALL_5(bpf_sock_ops_getsockopt, struct bpf_sock_ops_kern *, bpf_sock,
5526 int, level, int, optname, char *, optval, int, optlen)
5528 if (IS_ENABLED(CONFIG_INET) && level == SOL_TCP &&
5529 optname >= TCP_BPF_SYN && optname <= TCP_BPF_SYN_MAC) {
5530 int ret, copy_len = 0;
5533 ret = bpf_sock_ops_get_syn(bpf_sock, optname, &start);
5536 if (optlen < copy_len) {
5541 memcpy(optval, start, copy_len);
5544 /* Zero out unused buffer at the end */
5545 memset(optval + copy_len, 0, optlen - copy_len);
5550 return _bpf_getsockopt(bpf_sock->sk, level, optname, optval, optlen);
5553 static const struct bpf_func_proto bpf_sock_ops_getsockopt_proto = {
5554 .func = bpf_sock_ops_getsockopt,
5556 .ret_type = RET_INTEGER,
5557 .arg1_type = ARG_PTR_TO_CTX,
5558 .arg2_type = ARG_ANYTHING,
5559 .arg3_type = ARG_ANYTHING,
5560 .arg4_type = ARG_PTR_TO_UNINIT_MEM,
5561 .arg5_type = ARG_CONST_SIZE,
5564 BPF_CALL_2(bpf_sock_ops_cb_flags_set, struct bpf_sock_ops_kern *, bpf_sock,
5567 struct sock *sk = bpf_sock->sk;
5568 int val = argval & BPF_SOCK_OPS_ALL_CB_FLAGS;
5570 if (!IS_ENABLED(CONFIG_INET) || !sk_fullsock(sk))
5573 tcp_sk(sk)->bpf_sock_ops_cb_flags = val;
5575 return argval & (~BPF_SOCK_OPS_ALL_CB_FLAGS);
5578 static const struct bpf_func_proto bpf_sock_ops_cb_flags_set_proto = {
5579 .func = bpf_sock_ops_cb_flags_set,
5581 .ret_type = RET_INTEGER,
5582 .arg1_type = ARG_PTR_TO_CTX,
5583 .arg2_type = ARG_ANYTHING,
5586 const struct ipv6_bpf_stub *ipv6_bpf_stub __read_mostly;
5587 EXPORT_SYMBOL_GPL(ipv6_bpf_stub);
5589 BPF_CALL_3(bpf_bind, struct bpf_sock_addr_kern *, ctx, struct sockaddr *, addr,
5593 struct sock *sk = ctx->sk;
5594 u32 flags = BIND_FROM_BPF;
5598 if (addr_len < offsetofend(struct sockaddr, sa_family))
5600 if (addr->sa_family == AF_INET) {
5601 if (addr_len < sizeof(struct sockaddr_in))
5603 if (((struct sockaddr_in *)addr)->sin_port == htons(0))
5604 flags |= BIND_FORCE_ADDRESS_NO_PORT;
5605 return __inet_bind(sk, addr, addr_len, flags);
5606 #if IS_ENABLED(CONFIG_IPV6)
5607 } else if (addr->sa_family == AF_INET6) {
5608 if (addr_len < SIN6_LEN_RFC2133)
5610 if (((struct sockaddr_in6 *)addr)->sin6_port == htons(0))
5611 flags |= BIND_FORCE_ADDRESS_NO_PORT;
5612 /* ipv6_bpf_stub cannot be NULL, since it's called from
5613 * bpf_cgroup_inet6_connect hook and ipv6 is already loaded
5615 return ipv6_bpf_stub->inet6_bind(sk, addr, addr_len, flags);
5616 #endif /* CONFIG_IPV6 */
5618 #endif /* CONFIG_INET */
5620 return -EAFNOSUPPORT;
5623 static const struct bpf_func_proto bpf_bind_proto = {
5626 .ret_type = RET_INTEGER,
5627 .arg1_type = ARG_PTR_TO_CTX,
5628 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
5629 .arg3_type = ARG_CONST_SIZE,
5633 BPF_CALL_5(bpf_skb_get_xfrm_state, struct sk_buff *, skb, u32, index,
5634 struct bpf_xfrm_state *, to, u32, size, u64, flags)
5636 const struct sec_path *sp = skb_sec_path(skb);
5637 const struct xfrm_state *x;
5639 if (!sp || unlikely(index >= sp->len || flags))
5642 x = sp->xvec[index];
5644 if (unlikely(size != sizeof(struct bpf_xfrm_state)))
5647 to->reqid = x->props.reqid;
5648 to->spi = x->id.spi;
5649 to->family = x->props.family;
5652 if (to->family == AF_INET6) {
5653 memcpy(to->remote_ipv6, x->props.saddr.a6,
5654 sizeof(to->remote_ipv6));
5656 to->remote_ipv4 = x->props.saddr.a4;
5657 memset(&to->remote_ipv6[1], 0, sizeof(__u32) * 3);
5662 memset(to, 0, size);
5666 static const struct bpf_func_proto bpf_skb_get_xfrm_state_proto = {
5667 .func = bpf_skb_get_xfrm_state,
5669 .ret_type = RET_INTEGER,
5670 .arg1_type = ARG_PTR_TO_CTX,
5671 .arg2_type = ARG_ANYTHING,
5672 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
5673 .arg4_type = ARG_CONST_SIZE,
5674 .arg5_type = ARG_ANYTHING,
5678 #if IS_ENABLED(CONFIG_INET) || IS_ENABLED(CONFIG_IPV6)
5679 static int bpf_fib_set_fwd_params(struct bpf_fib_lookup *params,
5680 const struct neighbour *neigh,
5681 const struct net_device *dev, u32 mtu)
5683 memcpy(params->dmac, neigh->ha, ETH_ALEN);
5684 memcpy(params->smac, dev->dev_addr, ETH_ALEN);
5685 params->h_vlan_TCI = 0;
5686 params->h_vlan_proto = 0;
5688 params->mtu_result = mtu; /* union with tot_len */
5694 #if IS_ENABLED(CONFIG_INET)
5695 static int bpf_ipv4_fib_lookup(struct net *net, struct bpf_fib_lookup *params,
5696 u32 flags, bool check_mtu)
5698 struct fib_nh_common *nhc;
5699 struct in_device *in_dev;
5700 struct neighbour *neigh;
5701 struct net_device *dev;
5702 struct fib_result res;
5707 dev = dev_get_by_index_rcu(net, params->ifindex);
5711 /* verify forwarding is enabled on this interface */
5712 in_dev = __in_dev_get_rcu(dev);
5713 if (unlikely(!in_dev || !IN_DEV_FORWARD(in_dev)))
5714 return BPF_FIB_LKUP_RET_FWD_DISABLED;
5716 if (flags & BPF_FIB_LOOKUP_OUTPUT) {
5718 fl4.flowi4_oif = params->ifindex;
5720 fl4.flowi4_iif = params->ifindex;
5723 fl4.flowi4_tos = params->tos & IPTOS_RT_MASK;
5724 fl4.flowi4_scope = RT_SCOPE_UNIVERSE;
5725 fl4.flowi4_flags = 0;
5727 fl4.flowi4_proto = params->l4_protocol;
5728 fl4.daddr = params->ipv4_dst;
5729 fl4.saddr = params->ipv4_src;
5730 fl4.fl4_sport = params->sport;
5731 fl4.fl4_dport = params->dport;
5732 fl4.flowi4_multipath_hash = 0;
5734 if (flags & BPF_FIB_LOOKUP_DIRECT) {
5735 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
5736 struct fib_table *tb;
5738 tb = fib_get_table(net, tbid);
5740 return BPF_FIB_LKUP_RET_NOT_FWDED;
5742 err = fib_table_lookup(tb, &fl4, &res, FIB_LOOKUP_NOREF);
5744 fl4.flowi4_mark = 0;
5745 fl4.flowi4_secid = 0;
5746 fl4.flowi4_tun_key.tun_id = 0;
5747 fl4.flowi4_uid = sock_net_uid(net, NULL);
5749 err = fib_lookup(net, &fl4, &res, FIB_LOOKUP_NOREF);
5753 /* map fib lookup errors to RTN_ type */
5755 return BPF_FIB_LKUP_RET_BLACKHOLE;
5756 if (err == -EHOSTUNREACH)
5757 return BPF_FIB_LKUP_RET_UNREACHABLE;
5759 return BPF_FIB_LKUP_RET_PROHIBIT;
5761 return BPF_FIB_LKUP_RET_NOT_FWDED;
5764 if (res.type != RTN_UNICAST)
5765 return BPF_FIB_LKUP_RET_NOT_FWDED;
5767 if (fib_info_num_path(res.fi) > 1)
5768 fib_select_path(net, &res, &fl4, NULL);
5771 mtu = ip_mtu_from_fib_result(&res, params->ipv4_dst);
5772 if (params->tot_len > mtu) {
5773 params->mtu_result = mtu; /* union with tot_len */
5774 return BPF_FIB_LKUP_RET_FRAG_NEEDED;
5780 /* do not handle lwt encaps right now */
5781 if (nhc->nhc_lwtstate)
5782 return BPF_FIB_LKUP_RET_UNSUPP_LWT;
5786 params->rt_metric = res.fi->fib_priority;
5787 params->ifindex = dev->ifindex;
5789 /* xdp and cls_bpf programs are run in RCU-bh so
5790 * rcu_read_lock_bh is not needed here
5792 if (likely(nhc->nhc_gw_family != AF_INET6)) {
5793 if (nhc->nhc_gw_family)
5794 params->ipv4_dst = nhc->nhc_gw.ipv4;
5796 neigh = __ipv4_neigh_lookup_noref(dev,
5797 (__force u32)params->ipv4_dst);
5799 struct in6_addr *dst = (struct in6_addr *)params->ipv6_dst;
5801 params->family = AF_INET6;
5802 *dst = nhc->nhc_gw.ipv6;
5803 neigh = __ipv6_neigh_lookup_noref_stub(dev, dst);
5807 return BPF_FIB_LKUP_RET_NO_NEIGH;
5809 return bpf_fib_set_fwd_params(params, neigh, dev, mtu);
5813 #if IS_ENABLED(CONFIG_IPV6)
5814 static int bpf_ipv6_fib_lookup(struct net *net, struct bpf_fib_lookup *params,
5815 u32 flags, bool check_mtu)
5817 struct in6_addr *src = (struct in6_addr *) params->ipv6_src;
5818 struct in6_addr *dst = (struct in6_addr *) params->ipv6_dst;
5819 struct fib6_result res = {};
5820 struct neighbour *neigh;
5821 struct net_device *dev;
5822 struct inet6_dev *idev;
5828 /* link local addresses are never forwarded */
5829 if (rt6_need_strict(dst) || rt6_need_strict(src))
5830 return BPF_FIB_LKUP_RET_NOT_FWDED;
5832 dev = dev_get_by_index_rcu(net, params->ifindex);
5836 idev = __in6_dev_get_safely(dev);
5837 if (unlikely(!idev || !idev->cnf.forwarding))
5838 return BPF_FIB_LKUP_RET_FWD_DISABLED;
5840 if (flags & BPF_FIB_LOOKUP_OUTPUT) {
5842 oif = fl6.flowi6_oif = params->ifindex;
5844 oif = fl6.flowi6_iif = params->ifindex;
5846 strict = RT6_LOOKUP_F_HAS_SADDR;
5848 fl6.flowlabel = params->flowinfo;
5849 fl6.flowi6_scope = 0;
5850 fl6.flowi6_flags = 0;
5853 fl6.flowi6_proto = params->l4_protocol;
5856 fl6.fl6_sport = params->sport;
5857 fl6.fl6_dport = params->dport;
5859 if (flags & BPF_FIB_LOOKUP_DIRECT) {
5860 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
5861 struct fib6_table *tb;
5863 tb = ipv6_stub->fib6_get_table(net, tbid);
5865 return BPF_FIB_LKUP_RET_NOT_FWDED;
5867 err = ipv6_stub->fib6_table_lookup(net, tb, oif, &fl6, &res,
5870 fl6.flowi6_mark = 0;
5871 fl6.flowi6_secid = 0;
5872 fl6.flowi6_tun_key.tun_id = 0;
5873 fl6.flowi6_uid = sock_net_uid(net, NULL);
5875 err = ipv6_stub->fib6_lookup(net, oif, &fl6, &res, strict);
5878 if (unlikely(err || IS_ERR_OR_NULL(res.f6i) ||
5879 res.f6i == net->ipv6.fib6_null_entry))
5880 return BPF_FIB_LKUP_RET_NOT_FWDED;
5882 switch (res.fib6_type) {
5883 /* only unicast is forwarded */
5887 return BPF_FIB_LKUP_RET_BLACKHOLE;
5888 case RTN_UNREACHABLE:
5889 return BPF_FIB_LKUP_RET_UNREACHABLE;
5891 return BPF_FIB_LKUP_RET_PROHIBIT;
5893 return BPF_FIB_LKUP_RET_NOT_FWDED;
5896 ipv6_stub->fib6_select_path(net, &res, &fl6, fl6.flowi6_oif,
5897 fl6.flowi6_oif != 0, NULL, strict);
5900 mtu = ipv6_stub->ip6_mtu_from_fib6(&res, dst, src);
5901 if (params->tot_len > mtu) {
5902 params->mtu_result = mtu; /* union with tot_len */
5903 return BPF_FIB_LKUP_RET_FRAG_NEEDED;
5907 if (res.nh->fib_nh_lws)
5908 return BPF_FIB_LKUP_RET_UNSUPP_LWT;
5910 if (res.nh->fib_nh_gw_family)
5911 *dst = res.nh->fib_nh_gw6;
5913 dev = res.nh->fib_nh_dev;
5914 params->rt_metric = res.f6i->fib6_metric;
5915 params->ifindex = dev->ifindex;
5917 /* xdp and cls_bpf programs are run in RCU-bh so rcu_read_lock_bh is
5920 neigh = __ipv6_neigh_lookup_noref_stub(dev, dst);
5922 return BPF_FIB_LKUP_RET_NO_NEIGH;
5924 return bpf_fib_set_fwd_params(params, neigh, dev, mtu);
5928 BPF_CALL_4(bpf_xdp_fib_lookup, struct xdp_buff *, ctx,
5929 struct bpf_fib_lookup *, params, int, plen, u32, flags)
5931 if (plen < sizeof(*params))
5934 if (flags & ~(BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_OUTPUT))
5937 switch (params->family) {
5938 #if IS_ENABLED(CONFIG_INET)
5940 return bpf_ipv4_fib_lookup(dev_net(ctx->rxq->dev), params,
5943 #if IS_ENABLED(CONFIG_IPV6)
5945 return bpf_ipv6_fib_lookup(dev_net(ctx->rxq->dev), params,
5949 return -EAFNOSUPPORT;
5952 static const struct bpf_func_proto bpf_xdp_fib_lookup_proto = {
5953 .func = bpf_xdp_fib_lookup,
5955 .ret_type = RET_INTEGER,
5956 .arg1_type = ARG_PTR_TO_CTX,
5957 .arg2_type = ARG_PTR_TO_MEM,
5958 .arg3_type = ARG_CONST_SIZE,
5959 .arg4_type = ARG_ANYTHING,
5962 BPF_CALL_4(bpf_skb_fib_lookup, struct sk_buff *, skb,
5963 struct bpf_fib_lookup *, params, int, plen, u32, flags)
5965 struct net *net = dev_net(skb->dev);
5966 int rc = -EAFNOSUPPORT;
5967 bool check_mtu = false;
5969 if (plen < sizeof(*params))
5972 if (flags & ~(BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_OUTPUT))
5975 if (params->tot_len)
5978 switch (params->family) {
5979 #if IS_ENABLED(CONFIG_INET)
5981 rc = bpf_ipv4_fib_lookup(net, params, flags, check_mtu);
5984 #if IS_ENABLED(CONFIG_IPV6)
5986 rc = bpf_ipv6_fib_lookup(net, params, flags, check_mtu);
5991 if (rc == BPF_FIB_LKUP_RET_SUCCESS && !check_mtu) {
5992 struct net_device *dev;
5994 /* When tot_len isn't provided by user, check skb
5995 * against MTU of FIB lookup resulting net_device
5997 dev = dev_get_by_index_rcu(net, params->ifindex);
5998 if (!is_skb_forwardable(dev, skb))
5999 rc = BPF_FIB_LKUP_RET_FRAG_NEEDED;
6001 params->mtu_result = dev->mtu; /* union with tot_len */
6007 static const struct bpf_func_proto bpf_skb_fib_lookup_proto = {
6008 .func = bpf_skb_fib_lookup,
6010 .ret_type = RET_INTEGER,
6011 .arg1_type = ARG_PTR_TO_CTX,
6012 .arg2_type = ARG_PTR_TO_MEM,
6013 .arg3_type = ARG_CONST_SIZE,
6014 .arg4_type = ARG_ANYTHING,
6017 static struct net_device *__dev_via_ifindex(struct net_device *dev_curr,
6020 struct net *netns = dev_net(dev_curr);
6022 /* Non-redirect use-cases can use ifindex=0 and save ifindex lookup */
6026 return dev_get_by_index_rcu(netns, ifindex);
6029 BPF_CALL_5(bpf_skb_check_mtu, struct sk_buff *, skb,
6030 u32, ifindex, u32 *, mtu_len, s32, len_diff, u64, flags)
6032 int ret = BPF_MTU_CHK_RET_FRAG_NEEDED;
6033 struct net_device *dev = skb->dev;
6034 int skb_len, dev_len;
6037 if (unlikely(flags & ~(BPF_MTU_CHK_SEGS)))
6040 if (unlikely(flags & BPF_MTU_CHK_SEGS && (len_diff || *mtu_len)))
6043 dev = __dev_via_ifindex(dev, ifindex);
6047 mtu = READ_ONCE(dev->mtu);
6049 dev_len = mtu + dev->hard_header_len;
6051 /* If set use *mtu_len as input, L3 as iph->tot_len (like fib_lookup) */
6052 skb_len = *mtu_len ? *mtu_len + dev->hard_header_len : skb->len;
6054 skb_len += len_diff; /* minus result pass check */
6055 if (skb_len <= dev_len) {
6056 ret = BPF_MTU_CHK_RET_SUCCESS;
6059 /* At this point, skb->len exceed MTU, but as it include length of all
6060 * segments, it can still be below MTU. The SKB can possibly get
6061 * re-segmented in transmit path (see validate_xmit_skb). Thus, user
6062 * must choose if segs are to be MTU checked.
6064 if (skb_is_gso(skb)) {
6065 ret = BPF_MTU_CHK_RET_SUCCESS;
6067 if (flags & BPF_MTU_CHK_SEGS &&
6068 !skb_gso_validate_network_len(skb, mtu))
6069 ret = BPF_MTU_CHK_RET_SEGS_TOOBIG;
6072 /* BPF verifier guarantees valid pointer */
6078 BPF_CALL_5(bpf_xdp_check_mtu, struct xdp_buff *, xdp,
6079 u32, ifindex, u32 *, mtu_len, s32, len_diff, u64, flags)
6081 struct net_device *dev = xdp->rxq->dev;
6082 int xdp_len = xdp->data_end - xdp->data;
6083 int ret = BPF_MTU_CHK_RET_SUCCESS;
6086 /* XDP variant doesn't support multi-buffer segment check (yet) */
6087 if (unlikely(flags))
6090 dev = __dev_via_ifindex(dev, ifindex);
6094 mtu = READ_ONCE(dev->mtu);
6096 /* Add L2-header as dev MTU is L3 size */
6097 dev_len = mtu + dev->hard_header_len;
6099 /* Use *mtu_len as input, L3 as iph->tot_len (like fib_lookup) */
6101 xdp_len = *mtu_len + dev->hard_header_len;
6103 xdp_len += len_diff; /* minus result pass check */
6104 if (xdp_len > dev_len)
6105 ret = BPF_MTU_CHK_RET_FRAG_NEEDED;
6107 /* BPF verifier guarantees valid pointer */
6113 static const struct bpf_func_proto bpf_skb_check_mtu_proto = {
6114 .func = bpf_skb_check_mtu,
6116 .ret_type = RET_INTEGER,
6117 .arg1_type = ARG_PTR_TO_CTX,
6118 .arg2_type = ARG_ANYTHING,
6119 .arg3_type = ARG_PTR_TO_INT,
6120 .arg4_type = ARG_ANYTHING,
6121 .arg5_type = ARG_ANYTHING,
6124 static const struct bpf_func_proto bpf_xdp_check_mtu_proto = {
6125 .func = bpf_xdp_check_mtu,
6127 .ret_type = RET_INTEGER,
6128 .arg1_type = ARG_PTR_TO_CTX,
6129 .arg2_type = ARG_ANYTHING,
6130 .arg3_type = ARG_PTR_TO_INT,
6131 .arg4_type = ARG_ANYTHING,
6132 .arg5_type = ARG_ANYTHING,
6135 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
6136 static int bpf_push_seg6_encap(struct sk_buff *skb, u32 type, void *hdr, u32 len)
6139 struct ipv6_sr_hdr *srh = (struct ipv6_sr_hdr *)hdr;
6141 if (!seg6_validate_srh(srh, len, false))
6145 case BPF_LWT_ENCAP_SEG6_INLINE:
6146 if (skb->protocol != htons(ETH_P_IPV6))
6149 err = seg6_do_srh_inline(skb, srh);
6151 case BPF_LWT_ENCAP_SEG6:
6152 skb_reset_inner_headers(skb);
6153 skb->encapsulation = 1;
6154 err = seg6_do_srh_encap(skb, srh, IPPROTO_IPV6);
6160 bpf_compute_data_pointers(skb);
6164 skb_set_transport_header(skb, sizeof(struct ipv6hdr));
6166 return seg6_lookup_nexthop(skb, NULL, 0);
6168 #endif /* CONFIG_IPV6_SEG6_BPF */
6170 #if IS_ENABLED(CONFIG_LWTUNNEL_BPF)
6171 static int bpf_push_ip_encap(struct sk_buff *skb, void *hdr, u32 len,
6174 return bpf_lwt_push_ip_encap(skb, hdr, len, ingress);
6178 BPF_CALL_4(bpf_lwt_in_push_encap, struct sk_buff *, skb, u32, type, void *, hdr,
6182 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
6183 case BPF_LWT_ENCAP_SEG6:
6184 case BPF_LWT_ENCAP_SEG6_INLINE:
6185 return bpf_push_seg6_encap(skb, type, hdr, len);
6187 #if IS_ENABLED(CONFIG_LWTUNNEL_BPF)
6188 case BPF_LWT_ENCAP_IP:
6189 return bpf_push_ip_encap(skb, hdr, len, true /* ingress */);
6196 BPF_CALL_4(bpf_lwt_xmit_push_encap, struct sk_buff *, skb, u32, type,
6197 void *, hdr, u32, len)
6200 #if IS_ENABLED(CONFIG_LWTUNNEL_BPF)
6201 case BPF_LWT_ENCAP_IP:
6202 return bpf_push_ip_encap(skb, hdr, len, false /* egress */);
6209 static const struct bpf_func_proto bpf_lwt_in_push_encap_proto = {
6210 .func = bpf_lwt_in_push_encap,
6212 .ret_type = RET_INTEGER,
6213 .arg1_type = ARG_PTR_TO_CTX,
6214 .arg2_type = ARG_ANYTHING,
6215 .arg3_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6216 .arg4_type = ARG_CONST_SIZE
6219 static const struct bpf_func_proto bpf_lwt_xmit_push_encap_proto = {
6220 .func = bpf_lwt_xmit_push_encap,
6222 .ret_type = RET_INTEGER,
6223 .arg1_type = ARG_PTR_TO_CTX,
6224 .arg2_type = ARG_ANYTHING,
6225 .arg3_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6226 .arg4_type = ARG_CONST_SIZE
6229 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
6230 BPF_CALL_4(bpf_lwt_seg6_store_bytes, struct sk_buff *, skb, u32, offset,
6231 const void *, from, u32, len)
6233 struct seg6_bpf_srh_state *srh_state =
6234 this_cpu_ptr(&seg6_bpf_srh_states);
6235 struct ipv6_sr_hdr *srh = srh_state->srh;
6236 void *srh_tlvs, *srh_end, *ptr;
6242 srh_tlvs = (void *)((char *)srh + ((srh->first_segment + 1) << 4));
6243 srh_end = (void *)((char *)srh + sizeof(*srh) + srh_state->hdrlen);
6245 ptr = skb->data + offset;
6246 if (ptr >= srh_tlvs && ptr + len <= srh_end)
6247 srh_state->valid = false;
6248 else if (ptr < (void *)&srh->flags ||
6249 ptr + len > (void *)&srh->segments)
6252 if (unlikely(bpf_try_make_writable(skb, offset + len)))
6254 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0)
6256 srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
6258 memcpy(skb->data + offset, from, len);
6262 static const struct bpf_func_proto bpf_lwt_seg6_store_bytes_proto = {
6263 .func = bpf_lwt_seg6_store_bytes,
6265 .ret_type = RET_INTEGER,
6266 .arg1_type = ARG_PTR_TO_CTX,
6267 .arg2_type = ARG_ANYTHING,
6268 .arg3_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6269 .arg4_type = ARG_CONST_SIZE
6272 static void bpf_update_srh_state(struct sk_buff *skb)
6274 struct seg6_bpf_srh_state *srh_state =
6275 this_cpu_ptr(&seg6_bpf_srh_states);
6278 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0) {
6279 srh_state->srh = NULL;
6281 srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
6282 srh_state->hdrlen = srh_state->srh->hdrlen << 3;
6283 srh_state->valid = true;
6287 BPF_CALL_4(bpf_lwt_seg6_action, struct sk_buff *, skb,
6288 u32, action, void *, param, u32, param_len)
6290 struct seg6_bpf_srh_state *srh_state =
6291 this_cpu_ptr(&seg6_bpf_srh_states);
6296 case SEG6_LOCAL_ACTION_END_X:
6297 if (!seg6_bpf_has_valid_srh(skb))
6299 if (param_len != sizeof(struct in6_addr))
6301 return seg6_lookup_nexthop(skb, (struct in6_addr *)param, 0);
6302 case SEG6_LOCAL_ACTION_END_T:
6303 if (!seg6_bpf_has_valid_srh(skb))
6305 if (param_len != sizeof(int))
6307 return seg6_lookup_nexthop(skb, NULL, *(int *)param);
6308 case SEG6_LOCAL_ACTION_END_DT6:
6309 if (!seg6_bpf_has_valid_srh(skb))
6311 if (param_len != sizeof(int))
6314 if (ipv6_find_hdr(skb, &hdroff, IPPROTO_IPV6, NULL, NULL) < 0)
6316 if (!pskb_pull(skb, hdroff))
6319 skb_postpull_rcsum(skb, skb_network_header(skb), hdroff);
6320 skb_reset_network_header(skb);
6321 skb_reset_transport_header(skb);
6322 skb->encapsulation = 0;
6324 bpf_compute_data_pointers(skb);
6325 bpf_update_srh_state(skb);
6326 return seg6_lookup_nexthop(skb, NULL, *(int *)param);
6327 case SEG6_LOCAL_ACTION_END_B6:
6328 if (srh_state->srh && !seg6_bpf_has_valid_srh(skb))
6330 err = bpf_push_seg6_encap(skb, BPF_LWT_ENCAP_SEG6_INLINE,
6333 bpf_update_srh_state(skb);
6336 case SEG6_LOCAL_ACTION_END_B6_ENCAP:
6337 if (srh_state->srh && !seg6_bpf_has_valid_srh(skb))
6339 err = bpf_push_seg6_encap(skb, BPF_LWT_ENCAP_SEG6,
6342 bpf_update_srh_state(skb);
6350 static const struct bpf_func_proto bpf_lwt_seg6_action_proto = {
6351 .func = bpf_lwt_seg6_action,
6353 .ret_type = RET_INTEGER,
6354 .arg1_type = ARG_PTR_TO_CTX,
6355 .arg2_type = ARG_ANYTHING,
6356 .arg3_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6357 .arg4_type = ARG_CONST_SIZE
6360 BPF_CALL_3(bpf_lwt_seg6_adjust_srh, struct sk_buff *, skb, u32, offset,
6363 struct seg6_bpf_srh_state *srh_state =
6364 this_cpu_ptr(&seg6_bpf_srh_states);
6365 struct ipv6_sr_hdr *srh = srh_state->srh;
6366 void *srh_end, *srh_tlvs, *ptr;
6367 struct ipv6hdr *hdr;
6371 if (unlikely(srh == NULL))
6374 srh_tlvs = (void *)((unsigned char *)srh + sizeof(*srh) +
6375 ((srh->first_segment + 1) << 4));
6376 srh_end = (void *)((unsigned char *)srh + sizeof(*srh) +
6378 ptr = skb->data + offset;
6380 if (unlikely(ptr < srh_tlvs || ptr > srh_end))
6382 if (unlikely(len < 0 && (void *)((char *)ptr - len) > srh_end))
6386 ret = skb_cow_head(skb, len);
6387 if (unlikely(ret < 0))
6390 ret = bpf_skb_net_hdr_push(skb, offset, len);
6392 ret = bpf_skb_net_hdr_pop(skb, offset, -1 * len);
6395 bpf_compute_data_pointers(skb);
6396 if (unlikely(ret < 0))
6399 hdr = (struct ipv6hdr *)skb->data;
6400 hdr->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
6402 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0)
6404 srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
6405 srh_state->hdrlen += len;
6406 srh_state->valid = false;
6410 static const struct bpf_func_proto bpf_lwt_seg6_adjust_srh_proto = {
6411 .func = bpf_lwt_seg6_adjust_srh,
6413 .ret_type = RET_INTEGER,
6414 .arg1_type = ARG_PTR_TO_CTX,
6415 .arg2_type = ARG_ANYTHING,
6416 .arg3_type = ARG_ANYTHING,
6418 #endif /* CONFIG_IPV6_SEG6_BPF */
6421 static struct sock *sk_lookup(struct net *net, struct bpf_sock_tuple *tuple,
6422 int dif, int sdif, u8 family, u8 proto)
6424 struct inet_hashinfo *hinfo = net->ipv4.tcp_death_row.hashinfo;
6425 bool refcounted = false;
6426 struct sock *sk = NULL;
6428 if (family == AF_INET) {
6429 __be32 src4 = tuple->ipv4.saddr;
6430 __be32 dst4 = tuple->ipv4.daddr;
6432 if (proto == IPPROTO_TCP)
6433 sk = __inet_lookup(net, hinfo, NULL, 0,
6434 src4, tuple->ipv4.sport,
6435 dst4, tuple->ipv4.dport,
6436 dif, sdif, &refcounted);
6438 sk = __udp4_lib_lookup(net, src4, tuple->ipv4.sport,
6439 dst4, tuple->ipv4.dport,
6440 dif, sdif, &udp_table, NULL);
6441 #if IS_ENABLED(CONFIG_IPV6)
6443 struct in6_addr *src6 = (struct in6_addr *)&tuple->ipv6.saddr;
6444 struct in6_addr *dst6 = (struct in6_addr *)&tuple->ipv6.daddr;
6446 if (proto == IPPROTO_TCP)
6447 sk = __inet6_lookup(net, hinfo, NULL, 0,
6448 src6, tuple->ipv6.sport,
6449 dst6, ntohs(tuple->ipv6.dport),
6450 dif, sdif, &refcounted);
6451 else if (likely(ipv6_bpf_stub))
6452 sk = ipv6_bpf_stub->udp6_lib_lookup(net,
6453 src6, tuple->ipv6.sport,
6454 dst6, tuple->ipv6.dport,
6460 if (unlikely(sk && !refcounted && !sock_flag(sk, SOCK_RCU_FREE))) {
6461 WARN_ONCE(1, "Found non-RCU, unreferenced socket!");
6467 /* bpf_skc_lookup performs the core lookup for different types of sockets,
6468 * taking a reference on the socket if it doesn't have the flag SOCK_RCU_FREE.
6470 static struct sock *
6471 __bpf_skc_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
6472 struct net *caller_net, u32 ifindex, u8 proto, u64 netns_id,
6475 struct sock *sk = NULL;
6480 if (len == sizeof(tuple->ipv4))
6482 else if (len == sizeof(tuple->ipv6))
6487 if (unlikely(flags || !((s32)netns_id < 0 || netns_id <= S32_MAX)))
6490 if (family == AF_INET)
6491 sdif = inet_sdif(skb);
6493 sdif = inet6_sdif(skb);
6495 if ((s32)netns_id < 0) {
6497 sk = sk_lookup(net, tuple, ifindex, sdif, family, proto);
6499 net = get_net_ns_by_id(caller_net, netns_id);
6502 sk = sk_lookup(net, tuple, ifindex, sdif, family, proto);
6510 static struct sock *
6511 __bpf_sk_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
6512 struct net *caller_net, u32 ifindex, u8 proto, u64 netns_id,
6515 struct sock *sk = __bpf_skc_lookup(skb, tuple, len, caller_net,
6516 ifindex, proto, netns_id, flags);
6519 struct sock *sk2 = sk_to_full_sk(sk);
6521 /* sk_to_full_sk() may return (sk)->rsk_listener, so make sure the original sk
6522 * sock refcnt is decremented to prevent a request_sock leak.
6524 if (!sk_fullsock(sk2))
6528 /* Ensure there is no need to bump sk2 refcnt */
6529 if (unlikely(sk2 && !sock_flag(sk2, SOCK_RCU_FREE))) {
6530 WARN_ONCE(1, "Found non-RCU, unreferenced socket!");
6540 static struct sock *
6541 bpf_skc_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
6542 u8 proto, u64 netns_id, u64 flags)
6544 struct net *caller_net;
6548 caller_net = dev_net(skb->dev);
6549 ifindex = skb->dev->ifindex;
6551 caller_net = sock_net(skb->sk);
6555 return __bpf_skc_lookup(skb, tuple, len, caller_net, ifindex, proto,
6559 static struct sock *
6560 bpf_sk_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
6561 u8 proto, u64 netns_id, u64 flags)
6563 struct sock *sk = bpf_skc_lookup(skb, tuple, len, proto, netns_id,
6567 struct sock *sk2 = sk_to_full_sk(sk);
6569 /* sk_to_full_sk() may return (sk)->rsk_listener, so make sure the original sk
6570 * sock refcnt is decremented to prevent a request_sock leak.
6572 if (!sk_fullsock(sk2))
6576 /* Ensure there is no need to bump sk2 refcnt */
6577 if (unlikely(sk2 && !sock_flag(sk2, SOCK_RCU_FREE))) {
6578 WARN_ONCE(1, "Found non-RCU, unreferenced socket!");
6588 BPF_CALL_5(bpf_skc_lookup_tcp, struct sk_buff *, skb,
6589 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
6591 return (unsigned long)bpf_skc_lookup(skb, tuple, len, IPPROTO_TCP,
6595 static const struct bpf_func_proto bpf_skc_lookup_tcp_proto = {
6596 .func = bpf_skc_lookup_tcp,
6599 .ret_type = RET_PTR_TO_SOCK_COMMON_OR_NULL,
6600 .arg1_type = ARG_PTR_TO_CTX,
6601 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6602 .arg3_type = ARG_CONST_SIZE,
6603 .arg4_type = ARG_ANYTHING,
6604 .arg5_type = ARG_ANYTHING,
6607 BPF_CALL_5(bpf_sk_lookup_tcp, struct sk_buff *, skb,
6608 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
6610 return (unsigned long)bpf_sk_lookup(skb, tuple, len, IPPROTO_TCP,
6614 static const struct bpf_func_proto bpf_sk_lookup_tcp_proto = {
6615 .func = bpf_sk_lookup_tcp,
6618 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
6619 .arg1_type = ARG_PTR_TO_CTX,
6620 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6621 .arg3_type = ARG_CONST_SIZE,
6622 .arg4_type = ARG_ANYTHING,
6623 .arg5_type = ARG_ANYTHING,
6626 BPF_CALL_5(bpf_sk_lookup_udp, struct sk_buff *, skb,
6627 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
6629 return (unsigned long)bpf_sk_lookup(skb, tuple, len, IPPROTO_UDP,
6633 static const struct bpf_func_proto bpf_sk_lookup_udp_proto = {
6634 .func = bpf_sk_lookup_udp,
6637 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
6638 .arg1_type = ARG_PTR_TO_CTX,
6639 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6640 .arg3_type = ARG_CONST_SIZE,
6641 .arg4_type = ARG_ANYTHING,
6642 .arg5_type = ARG_ANYTHING,
6645 BPF_CALL_1(bpf_sk_release, struct sock *, sk)
6647 if (sk && sk_is_refcounted(sk))
6652 static const struct bpf_func_proto bpf_sk_release_proto = {
6653 .func = bpf_sk_release,
6655 .ret_type = RET_INTEGER,
6656 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON | OBJ_RELEASE,
6659 BPF_CALL_5(bpf_xdp_sk_lookup_udp, struct xdp_buff *, ctx,
6660 struct bpf_sock_tuple *, tuple, u32, len, u32, netns_id, u64, flags)
6662 struct net *caller_net = dev_net(ctx->rxq->dev);
6663 int ifindex = ctx->rxq->dev->ifindex;
6665 return (unsigned long)__bpf_sk_lookup(NULL, tuple, len, caller_net,
6666 ifindex, IPPROTO_UDP, netns_id,
6670 static const struct bpf_func_proto bpf_xdp_sk_lookup_udp_proto = {
6671 .func = bpf_xdp_sk_lookup_udp,
6674 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
6675 .arg1_type = ARG_PTR_TO_CTX,
6676 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6677 .arg3_type = ARG_CONST_SIZE,
6678 .arg4_type = ARG_ANYTHING,
6679 .arg5_type = ARG_ANYTHING,
6682 BPF_CALL_5(bpf_xdp_skc_lookup_tcp, struct xdp_buff *, ctx,
6683 struct bpf_sock_tuple *, tuple, u32, len, u32, netns_id, u64, flags)
6685 struct net *caller_net = dev_net(ctx->rxq->dev);
6686 int ifindex = ctx->rxq->dev->ifindex;
6688 return (unsigned long)__bpf_skc_lookup(NULL, tuple, len, caller_net,
6689 ifindex, IPPROTO_TCP, netns_id,
6693 static const struct bpf_func_proto bpf_xdp_skc_lookup_tcp_proto = {
6694 .func = bpf_xdp_skc_lookup_tcp,
6697 .ret_type = RET_PTR_TO_SOCK_COMMON_OR_NULL,
6698 .arg1_type = ARG_PTR_TO_CTX,
6699 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6700 .arg3_type = ARG_CONST_SIZE,
6701 .arg4_type = ARG_ANYTHING,
6702 .arg5_type = ARG_ANYTHING,
6705 BPF_CALL_5(bpf_xdp_sk_lookup_tcp, struct xdp_buff *, ctx,
6706 struct bpf_sock_tuple *, tuple, u32, len, u32, netns_id, u64, flags)
6708 struct net *caller_net = dev_net(ctx->rxq->dev);
6709 int ifindex = ctx->rxq->dev->ifindex;
6711 return (unsigned long)__bpf_sk_lookup(NULL, tuple, len, caller_net,
6712 ifindex, IPPROTO_TCP, netns_id,
6716 static const struct bpf_func_proto bpf_xdp_sk_lookup_tcp_proto = {
6717 .func = bpf_xdp_sk_lookup_tcp,
6720 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
6721 .arg1_type = ARG_PTR_TO_CTX,
6722 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6723 .arg3_type = ARG_CONST_SIZE,
6724 .arg4_type = ARG_ANYTHING,
6725 .arg5_type = ARG_ANYTHING,
6728 BPF_CALL_5(bpf_sock_addr_skc_lookup_tcp, struct bpf_sock_addr_kern *, ctx,
6729 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
6731 return (unsigned long)__bpf_skc_lookup(NULL, tuple, len,
6732 sock_net(ctx->sk), 0,
6733 IPPROTO_TCP, netns_id, flags);
6736 static const struct bpf_func_proto bpf_sock_addr_skc_lookup_tcp_proto = {
6737 .func = bpf_sock_addr_skc_lookup_tcp,
6739 .ret_type = RET_PTR_TO_SOCK_COMMON_OR_NULL,
6740 .arg1_type = ARG_PTR_TO_CTX,
6741 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6742 .arg3_type = ARG_CONST_SIZE,
6743 .arg4_type = ARG_ANYTHING,
6744 .arg5_type = ARG_ANYTHING,
6747 BPF_CALL_5(bpf_sock_addr_sk_lookup_tcp, struct bpf_sock_addr_kern *, ctx,
6748 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
6750 return (unsigned long)__bpf_sk_lookup(NULL, tuple, len,
6751 sock_net(ctx->sk), 0, IPPROTO_TCP,
6755 static const struct bpf_func_proto bpf_sock_addr_sk_lookup_tcp_proto = {
6756 .func = bpf_sock_addr_sk_lookup_tcp,
6758 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
6759 .arg1_type = ARG_PTR_TO_CTX,
6760 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6761 .arg3_type = ARG_CONST_SIZE,
6762 .arg4_type = ARG_ANYTHING,
6763 .arg5_type = ARG_ANYTHING,
6766 BPF_CALL_5(bpf_sock_addr_sk_lookup_udp, struct bpf_sock_addr_kern *, ctx,
6767 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
6769 return (unsigned long)__bpf_sk_lookup(NULL, tuple, len,
6770 sock_net(ctx->sk), 0, IPPROTO_UDP,
6774 static const struct bpf_func_proto bpf_sock_addr_sk_lookup_udp_proto = {
6775 .func = bpf_sock_addr_sk_lookup_udp,
6777 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
6778 .arg1_type = ARG_PTR_TO_CTX,
6779 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6780 .arg3_type = ARG_CONST_SIZE,
6781 .arg4_type = ARG_ANYTHING,
6782 .arg5_type = ARG_ANYTHING,
6785 bool bpf_tcp_sock_is_valid_access(int off, int size, enum bpf_access_type type,
6786 struct bpf_insn_access_aux *info)
6788 if (off < 0 || off >= offsetofend(struct bpf_tcp_sock,
6792 if (off % size != 0)
6796 case offsetof(struct bpf_tcp_sock, bytes_received):
6797 case offsetof(struct bpf_tcp_sock, bytes_acked):
6798 return size == sizeof(__u64);
6800 return size == sizeof(__u32);
6804 u32 bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type,
6805 const struct bpf_insn *si,
6806 struct bpf_insn *insn_buf,
6807 struct bpf_prog *prog, u32 *target_size)
6809 struct bpf_insn *insn = insn_buf;
6811 #define BPF_TCP_SOCK_GET_COMMON(FIELD) \
6813 BUILD_BUG_ON(sizeof_field(struct tcp_sock, FIELD) > \
6814 sizeof_field(struct bpf_tcp_sock, FIELD)); \
6815 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct tcp_sock, FIELD),\
6816 si->dst_reg, si->src_reg, \
6817 offsetof(struct tcp_sock, FIELD)); \
6820 #define BPF_INET_SOCK_GET_COMMON(FIELD) \
6822 BUILD_BUG_ON(sizeof_field(struct inet_connection_sock, \
6824 sizeof_field(struct bpf_tcp_sock, FIELD)); \
6825 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
6826 struct inet_connection_sock, \
6828 si->dst_reg, si->src_reg, \
6830 struct inet_connection_sock, \
6834 if (insn > insn_buf)
6835 return insn - insn_buf;
6838 case offsetof(struct bpf_tcp_sock, rtt_min):
6839 BUILD_BUG_ON(sizeof_field(struct tcp_sock, rtt_min) !=
6840 sizeof(struct minmax));
6841 BUILD_BUG_ON(sizeof(struct minmax) <
6842 sizeof(struct minmax_sample));
6844 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6845 offsetof(struct tcp_sock, rtt_min) +
6846 offsetof(struct minmax_sample, v));
6848 case offsetof(struct bpf_tcp_sock, snd_cwnd):
6849 BPF_TCP_SOCK_GET_COMMON(snd_cwnd);
6851 case offsetof(struct bpf_tcp_sock, srtt_us):
6852 BPF_TCP_SOCK_GET_COMMON(srtt_us);
6854 case offsetof(struct bpf_tcp_sock, snd_ssthresh):
6855 BPF_TCP_SOCK_GET_COMMON(snd_ssthresh);
6857 case offsetof(struct bpf_tcp_sock, rcv_nxt):
6858 BPF_TCP_SOCK_GET_COMMON(rcv_nxt);
6860 case offsetof(struct bpf_tcp_sock, snd_nxt):
6861 BPF_TCP_SOCK_GET_COMMON(snd_nxt);
6863 case offsetof(struct bpf_tcp_sock, snd_una):
6864 BPF_TCP_SOCK_GET_COMMON(snd_una);
6866 case offsetof(struct bpf_tcp_sock, mss_cache):
6867 BPF_TCP_SOCK_GET_COMMON(mss_cache);
6869 case offsetof(struct bpf_tcp_sock, ecn_flags):
6870 BPF_TCP_SOCK_GET_COMMON(ecn_flags);
6872 case offsetof(struct bpf_tcp_sock, rate_delivered):
6873 BPF_TCP_SOCK_GET_COMMON(rate_delivered);
6875 case offsetof(struct bpf_tcp_sock, rate_interval_us):
6876 BPF_TCP_SOCK_GET_COMMON(rate_interval_us);
6878 case offsetof(struct bpf_tcp_sock, packets_out):
6879 BPF_TCP_SOCK_GET_COMMON(packets_out);
6881 case offsetof(struct bpf_tcp_sock, retrans_out):
6882 BPF_TCP_SOCK_GET_COMMON(retrans_out);
6884 case offsetof(struct bpf_tcp_sock, total_retrans):
6885 BPF_TCP_SOCK_GET_COMMON(total_retrans);
6887 case offsetof(struct bpf_tcp_sock, segs_in):
6888 BPF_TCP_SOCK_GET_COMMON(segs_in);
6890 case offsetof(struct bpf_tcp_sock, data_segs_in):
6891 BPF_TCP_SOCK_GET_COMMON(data_segs_in);
6893 case offsetof(struct bpf_tcp_sock, segs_out):
6894 BPF_TCP_SOCK_GET_COMMON(segs_out);
6896 case offsetof(struct bpf_tcp_sock, data_segs_out):
6897 BPF_TCP_SOCK_GET_COMMON(data_segs_out);
6899 case offsetof(struct bpf_tcp_sock, lost_out):
6900 BPF_TCP_SOCK_GET_COMMON(lost_out);
6902 case offsetof(struct bpf_tcp_sock, sacked_out):
6903 BPF_TCP_SOCK_GET_COMMON(sacked_out);
6905 case offsetof(struct bpf_tcp_sock, bytes_received):
6906 BPF_TCP_SOCK_GET_COMMON(bytes_received);
6908 case offsetof(struct bpf_tcp_sock, bytes_acked):
6909 BPF_TCP_SOCK_GET_COMMON(bytes_acked);
6911 case offsetof(struct bpf_tcp_sock, dsack_dups):
6912 BPF_TCP_SOCK_GET_COMMON(dsack_dups);
6914 case offsetof(struct bpf_tcp_sock, delivered):
6915 BPF_TCP_SOCK_GET_COMMON(delivered);
6917 case offsetof(struct bpf_tcp_sock, delivered_ce):
6918 BPF_TCP_SOCK_GET_COMMON(delivered_ce);
6920 case offsetof(struct bpf_tcp_sock, icsk_retransmits):
6921 BPF_INET_SOCK_GET_COMMON(icsk_retransmits);
6925 return insn - insn_buf;
6928 BPF_CALL_1(bpf_tcp_sock, struct sock *, sk)
6930 if (sk_fullsock(sk) && sk->sk_protocol == IPPROTO_TCP)
6931 return (unsigned long)sk;
6933 return (unsigned long)NULL;
6936 const struct bpf_func_proto bpf_tcp_sock_proto = {
6937 .func = bpf_tcp_sock,
6939 .ret_type = RET_PTR_TO_TCP_SOCK_OR_NULL,
6940 .arg1_type = ARG_PTR_TO_SOCK_COMMON,
6943 BPF_CALL_1(bpf_get_listener_sock, struct sock *, sk)
6945 sk = sk_to_full_sk(sk);
6947 if (sk->sk_state == TCP_LISTEN && sock_flag(sk, SOCK_RCU_FREE))
6948 return (unsigned long)sk;
6950 return (unsigned long)NULL;
6953 static const struct bpf_func_proto bpf_get_listener_sock_proto = {
6954 .func = bpf_get_listener_sock,
6956 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
6957 .arg1_type = ARG_PTR_TO_SOCK_COMMON,
6960 BPF_CALL_1(bpf_skb_ecn_set_ce, struct sk_buff *, skb)
6962 unsigned int iphdr_len;
6964 switch (skb_protocol(skb, true)) {
6965 case cpu_to_be16(ETH_P_IP):
6966 iphdr_len = sizeof(struct iphdr);
6968 case cpu_to_be16(ETH_P_IPV6):
6969 iphdr_len = sizeof(struct ipv6hdr);
6975 if (skb_headlen(skb) < iphdr_len)
6978 if (skb_cloned(skb) && !skb_clone_writable(skb, iphdr_len))
6981 return INET_ECN_set_ce(skb);
6984 bool bpf_xdp_sock_is_valid_access(int off, int size, enum bpf_access_type type,
6985 struct bpf_insn_access_aux *info)
6987 if (off < 0 || off >= offsetofend(struct bpf_xdp_sock, queue_id))
6990 if (off % size != 0)
6995 return size == sizeof(__u32);
6999 u32 bpf_xdp_sock_convert_ctx_access(enum bpf_access_type type,
7000 const struct bpf_insn *si,
7001 struct bpf_insn *insn_buf,
7002 struct bpf_prog *prog, u32 *target_size)
7004 struct bpf_insn *insn = insn_buf;
7006 #define BPF_XDP_SOCK_GET(FIELD) \
7008 BUILD_BUG_ON(sizeof_field(struct xdp_sock, FIELD) > \
7009 sizeof_field(struct bpf_xdp_sock, FIELD)); \
7010 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_sock, FIELD),\
7011 si->dst_reg, si->src_reg, \
7012 offsetof(struct xdp_sock, FIELD)); \
7016 case offsetof(struct bpf_xdp_sock, queue_id):
7017 BPF_XDP_SOCK_GET(queue_id);
7021 return insn - insn_buf;
7024 static const struct bpf_func_proto bpf_skb_ecn_set_ce_proto = {
7025 .func = bpf_skb_ecn_set_ce,
7027 .ret_type = RET_INTEGER,
7028 .arg1_type = ARG_PTR_TO_CTX,
7031 BPF_CALL_5(bpf_tcp_check_syncookie, struct sock *, sk, void *, iph, u32, iph_len,
7032 struct tcphdr *, th, u32, th_len)
7034 #ifdef CONFIG_SYN_COOKIES
7038 if (unlikely(!sk || th_len < sizeof(*th)))
7041 /* sk_listener() allows TCP_NEW_SYN_RECV, which makes no sense here. */
7042 if (sk->sk_protocol != IPPROTO_TCP || sk->sk_state != TCP_LISTEN)
7045 if (!READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_syncookies))
7048 if (!th->ack || th->rst || th->syn)
7051 if (unlikely(iph_len < sizeof(struct iphdr)))
7054 if (tcp_synq_no_recent_overflow(sk))
7057 cookie = ntohl(th->ack_seq) - 1;
7059 /* Both struct iphdr and struct ipv6hdr have the version field at the
7060 * same offset so we can cast to the shorter header (struct iphdr).
7062 switch (((struct iphdr *)iph)->version) {
7064 if (sk->sk_family == AF_INET6 && ipv6_only_sock(sk))
7067 ret = __cookie_v4_check((struct iphdr *)iph, th, cookie);
7070 #if IS_BUILTIN(CONFIG_IPV6)
7072 if (unlikely(iph_len < sizeof(struct ipv6hdr)))
7075 if (sk->sk_family != AF_INET6)
7078 ret = __cookie_v6_check((struct ipv6hdr *)iph, th, cookie);
7080 #endif /* CONFIG_IPV6 */
7083 return -EPROTONOSUPPORT;
7095 static const struct bpf_func_proto bpf_tcp_check_syncookie_proto = {
7096 .func = bpf_tcp_check_syncookie,
7099 .ret_type = RET_INTEGER,
7100 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
7101 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
7102 .arg3_type = ARG_CONST_SIZE,
7103 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
7104 .arg5_type = ARG_CONST_SIZE,
7107 BPF_CALL_5(bpf_tcp_gen_syncookie, struct sock *, sk, void *, iph, u32, iph_len,
7108 struct tcphdr *, th, u32, th_len)
7110 #ifdef CONFIG_SYN_COOKIES
7114 if (unlikely(!sk || th_len < sizeof(*th) || th_len != th->doff * 4))
7117 if (sk->sk_protocol != IPPROTO_TCP || sk->sk_state != TCP_LISTEN)
7120 if (!READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_syncookies))
7123 if (!th->syn || th->ack || th->fin || th->rst)
7126 if (unlikely(iph_len < sizeof(struct iphdr)))
7129 /* Both struct iphdr and struct ipv6hdr have the version field at the
7130 * same offset so we can cast to the shorter header (struct iphdr).
7132 switch (((struct iphdr *)iph)->version) {
7134 if (sk->sk_family == AF_INET6 && ipv6_only_sock(sk))
7137 mss = tcp_v4_get_syncookie(sk, iph, th, &cookie);
7140 #if IS_BUILTIN(CONFIG_IPV6)
7142 if (unlikely(iph_len < sizeof(struct ipv6hdr)))
7145 if (sk->sk_family != AF_INET6)
7148 mss = tcp_v6_get_syncookie(sk, iph, th, &cookie);
7150 #endif /* CONFIG_IPV6 */
7153 return -EPROTONOSUPPORT;
7158 return cookie | ((u64)mss << 32);
7161 #endif /* CONFIG_SYN_COOKIES */
7164 static const struct bpf_func_proto bpf_tcp_gen_syncookie_proto = {
7165 .func = bpf_tcp_gen_syncookie,
7166 .gpl_only = true, /* __cookie_v*_init_sequence() is GPL */
7168 .ret_type = RET_INTEGER,
7169 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
7170 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
7171 .arg3_type = ARG_CONST_SIZE,
7172 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
7173 .arg5_type = ARG_CONST_SIZE,
7176 BPF_CALL_3(bpf_sk_assign, struct sk_buff *, skb, struct sock *, sk, u64, flags)
7178 if (!sk || flags != 0)
7180 if (!skb_at_tc_ingress(skb))
7182 if (unlikely(dev_net(skb->dev) != sock_net(sk)))
7183 return -ENETUNREACH;
7184 if (unlikely(sk_fullsock(sk) && sk->sk_reuseport))
7185 return -ESOCKTNOSUPPORT;
7186 if (sk_is_refcounted(sk) &&
7187 unlikely(!refcount_inc_not_zero(&sk->sk_refcnt)))
7192 skb->destructor = sock_pfree;
7197 static const struct bpf_func_proto bpf_sk_assign_proto = {
7198 .func = bpf_sk_assign,
7200 .ret_type = RET_INTEGER,
7201 .arg1_type = ARG_PTR_TO_CTX,
7202 .arg2_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
7203 .arg3_type = ARG_ANYTHING,
7206 static const u8 *bpf_search_tcp_opt(const u8 *op, const u8 *opend,
7207 u8 search_kind, const u8 *magic,
7208 u8 magic_len, bool *eol)
7214 while (op < opend) {
7217 if (kind == TCPOPT_EOL) {
7219 return ERR_PTR(-ENOMSG);
7220 } else if (kind == TCPOPT_NOP) {
7225 if (opend - op < 2 || opend - op < op[1] || op[1] < 2)
7226 /* Something is wrong in the received header.
7227 * Follow the TCP stack's tcp_parse_options()
7228 * and just bail here.
7230 return ERR_PTR(-EFAULT);
7233 if (search_kind == kind) {
7237 if (magic_len > kind_len - 2)
7238 return ERR_PTR(-ENOMSG);
7240 if (!memcmp(&op[2], magic, magic_len))
7247 return ERR_PTR(-ENOMSG);
7250 BPF_CALL_4(bpf_sock_ops_load_hdr_opt, struct bpf_sock_ops_kern *, bpf_sock,
7251 void *, search_res, u32, len, u64, flags)
7253 bool eol, load_syn = flags & BPF_LOAD_HDR_OPT_TCP_SYN;
7254 const u8 *op, *opend, *magic, *search = search_res;
7255 u8 search_kind, search_len, copy_len, magic_len;
7258 /* 2 byte is the minimal option len except TCPOPT_NOP and
7259 * TCPOPT_EOL which are useless for the bpf prog to learn
7260 * and this helper disallow loading them also.
7262 if (len < 2 || flags & ~BPF_LOAD_HDR_OPT_TCP_SYN)
7265 search_kind = search[0];
7266 search_len = search[1];
7268 if (search_len > len || search_kind == TCPOPT_NOP ||
7269 search_kind == TCPOPT_EOL)
7272 if (search_kind == TCPOPT_EXP || search_kind == 253) {
7273 /* 16 or 32 bit magic. +2 for kind and kind length */
7274 if (search_len != 4 && search_len != 6)
7277 magic_len = search_len - 2;
7286 ret = bpf_sock_ops_get_syn(bpf_sock, TCP_BPF_SYN, &op);
7291 op += sizeof(struct tcphdr);
7293 if (!bpf_sock->skb ||
7294 bpf_sock->op == BPF_SOCK_OPS_HDR_OPT_LEN_CB)
7295 /* This bpf_sock->op cannot call this helper */
7298 opend = bpf_sock->skb_data_end;
7299 op = bpf_sock->skb->data + sizeof(struct tcphdr);
7302 op = bpf_search_tcp_opt(op, opend, search_kind, magic, magic_len,
7309 if (copy_len > len) {
7314 memcpy(search_res, op, copy_len);
7318 static const struct bpf_func_proto bpf_sock_ops_load_hdr_opt_proto = {
7319 .func = bpf_sock_ops_load_hdr_opt,
7321 .ret_type = RET_INTEGER,
7322 .arg1_type = ARG_PTR_TO_CTX,
7323 .arg2_type = ARG_PTR_TO_MEM,
7324 .arg3_type = ARG_CONST_SIZE,
7325 .arg4_type = ARG_ANYTHING,
7328 BPF_CALL_4(bpf_sock_ops_store_hdr_opt, struct bpf_sock_ops_kern *, bpf_sock,
7329 const void *, from, u32, len, u64, flags)
7331 u8 new_kind, new_kind_len, magic_len = 0, *opend;
7332 const u8 *op, *new_op, *magic = NULL;
7333 struct sk_buff *skb;
7336 if (bpf_sock->op != BPF_SOCK_OPS_WRITE_HDR_OPT_CB)
7339 if (len < 2 || flags)
7343 new_kind = new_op[0];
7344 new_kind_len = new_op[1];
7346 if (new_kind_len > len || new_kind == TCPOPT_NOP ||
7347 new_kind == TCPOPT_EOL)
7350 if (new_kind_len > bpf_sock->remaining_opt_len)
7353 /* 253 is another experimental kind */
7354 if (new_kind == TCPOPT_EXP || new_kind == 253) {
7355 if (new_kind_len < 4)
7357 /* Match for the 2 byte magic also.
7358 * RFC 6994: the magic could be 2 or 4 bytes.
7359 * Hence, matching by 2 byte only is on the
7360 * conservative side but it is the right
7361 * thing to do for the 'search-for-duplication'
7368 /* Check for duplication */
7369 skb = bpf_sock->skb;
7370 op = skb->data + sizeof(struct tcphdr);
7371 opend = bpf_sock->skb_data_end;
7373 op = bpf_search_tcp_opt(op, opend, new_kind, magic, magic_len,
7378 if (PTR_ERR(op) != -ENOMSG)
7382 /* The option has been ended. Treat it as no more
7383 * header option can be written.
7387 /* No duplication found. Store the header option. */
7388 memcpy(opend, from, new_kind_len);
7390 bpf_sock->remaining_opt_len -= new_kind_len;
7391 bpf_sock->skb_data_end += new_kind_len;
7396 static const struct bpf_func_proto bpf_sock_ops_store_hdr_opt_proto = {
7397 .func = bpf_sock_ops_store_hdr_opt,
7399 .ret_type = RET_INTEGER,
7400 .arg1_type = ARG_PTR_TO_CTX,
7401 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
7402 .arg3_type = ARG_CONST_SIZE,
7403 .arg4_type = ARG_ANYTHING,
7406 BPF_CALL_3(bpf_sock_ops_reserve_hdr_opt, struct bpf_sock_ops_kern *, bpf_sock,
7407 u32, len, u64, flags)
7409 if (bpf_sock->op != BPF_SOCK_OPS_HDR_OPT_LEN_CB)
7412 if (flags || len < 2)
7415 if (len > bpf_sock->remaining_opt_len)
7418 bpf_sock->remaining_opt_len -= len;
7423 static const struct bpf_func_proto bpf_sock_ops_reserve_hdr_opt_proto = {
7424 .func = bpf_sock_ops_reserve_hdr_opt,
7426 .ret_type = RET_INTEGER,
7427 .arg1_type = ARG_PTR_TO_CTX,
7428 .arg2_type = ARG_ANYTHING,
7429 .arg3_type = ARG_ANYTHING,
7432 BPF_CALL_3(bpf_skb_set_tstamp, struct sk_buff *, skb,
7433 u64, tstamp, u32, tstamp_type)
7435 /* skb_clear_delivery_time() is done for inet protocol */
7436 if (skb->protocol != htons(ETH_P_IP) &&
7437 skb->protocol != htons(ETH_P_IPV6))
7440 switch (tstamp_type) {
7441 case BPF_SKB_TSTAMP_DELIVERY_MONO:
7444 skb->tstamp = tstamp;
7445 skb->mono_delivery_time = 1;
7447 case BPF_SKB_TSTAMP_UNSPEC:
7451 skb->mono_delivery_time = 0;
7460 static const struct bpf_func_proto bpf_skb_set_tstamp_proto = {
7461 .func = bpf_skb_set_tstamp,
7463 .ret_type = RET_INTEGER,
7464 .arg1_type = ARG_PTR_TO_CTX,
7465 .arg2_type = ARG_ANYTHING,
7466 .arg3_type = ARG_ANYTHING,
7469 #ifdef CONFIG_SYN_COOKIES
7470 BPF_CALL_3(bpf_tcp_raw_gen_syncookie_ipv4, struct iphdr *, iph,
7471 struct tcphdr *, th, u32, th_len)
7476 if (unlikely(th_len < sizeof(*th) || th_len != th->doff * 4))
7479 mss = tcp_parse_mss_option(th, 0) ?: TCP_MSS_DEFAULT;
7480 cookie = __cookie_v4_init_sequence(iph, th, &mss);
7482 return cookie | ((u64)mss << 32);
7485 static const struct bpf_func_proto bpf_tcp_raw_gen_syncookie_ipv4_proto = {
7486 .func = bpf_tcp_raw_gen_syncookie_ipv4,
7487 .gpl_only = true, /* __cookie_v4_init_sequence() is GPL */
7489 .ret_type = RET_INTEGER,
7490 .arg1_type = ARG_PTR_TO_FIXED_SIZE_MEM,
7491 .arg1_size = sizeof(struct iphdr),
7492 .arg2_type = ARG_PTR_TO_MEM,
7493 .arg3_type = ARG_CONST_SIZE,
7496 BPF_CALL_3(bpf_tcp_raw_gen_syncookie_ipv6, struct ipv6hdr *, iph,
7497 struct tcphdr *, th, u32, th_len)
7499 #if IS_BUILTIN(CONFIG_IPV6)
7500 const u16 mss_clamp = IPV6_MIN_MTU - sizeof(struct tcphdr) -
7501 sizeof(struct ipv6hdr);
7505 if (unlikely(th_len < sizeof(*th) || th_len != th->doff * 4))
7508 mss = tcp_parse_mss_option(th, 0) ?: mss_clamp;
7509 cookie = __cookie_v6_init_sequence(iph, th, &mss);
7511 return cookie | ((u64)mss << 32);
7513 return -EPROTONOSUPPORT;
7517 static const struct bpf_func_proto bpf_tcp_raw_gen_syncookie_ipv6_proto = {
7518 .func = bpf_tcp_raw_gen_syncookie_ipv6,
7519 .gpl_only = true, /* __cookie_v6_init_sequence() is GPL */
7521 .ret_type = RET_INTEGER,
7522 .arg1_type = ARG_PTR_TO_FIXED_SIZE_MEM,
7523 .arg1_size = sizeof(struct ipv6hdr),
7524 .arg2_type = ARG_PTR_TO_MEM,
7525 .arg3_type = ARG_CONST_SIZE,
7528 BPF_CALL_2(bpf_tcp_raw_check_syncookie_ipv4, struct iphdr *, iph,
7529 struct tcphdr *, th)
7531 u32 cookie = ntohl(th->ack_seq) - 1;
7533 if (__cookie_v4_check(iph, th, cookie) > 0)
7539 static const struct bpf_func_proto bpf_tcp_raw_check_syncookie_ipv4_proto = {
7540 .func = bpf_tcp_raw_check_syncookie_ipv4,
7541 .gpl_only = true, /* __cookie_v4_check is GPL */
7543 .ret_type = RET_INTEGER,
7544 .arg1_type = ARG_PTR_TO_FIXED_SIZE_MEM,
7545 .arg1_size = sizeof(struct iphdr),
7546 .arg2_type = ARG_PTR_TO_FIXED_SIZE_MEM,
7547 .arg2_size = sizeof(struct tcphdr),
7550 BPF_CALL_2(bpf_tcp_raw_check_syncookie_ipv6, struct ipv6hdr *, iph,
7551 struct tcphdr *, th)
7553 #if IS_BUILTIN(CONFIG_IPV6)
7554 u32 cookie = ntohl(th->ack_seq) - 1;
7556 if (__cookie_v6_check(iph, th, cookie) > 0)
7561 return -EPROTONOSUPPORT;
7565 static const struct bpf_func_proto bpf_tcp_raw_check_syncookie_ipv6_proto = {
7566 .func = bpf_tcp_raw_check_syncookie_ipv6,
7567 .gpl_only = true, /* __cookie_v6_check is GPL */
7569 .ret_type = RET_INTEGER,
7570 .arg1_type = ARG_PTR_TO_FIXED_SIZE_MEM,
7571 .arg1_size = sizeof(struct ipv6hdr),
7572 .arg2_type = ARG_PTR_TO_FIXED_SIZE_MEM,
7573 .arg2_size = sizeof(struct tcphdr),
7575 #endif /* CONFIG_SYN_COOKIES */
7577 #endif /* CONFIG_INET */
7579 bool bpf_helper_changes_pkt_data(void *func)
7581 if (func == bpf_skb_vlan_push ||
7582 func == bpf_skb_vlan_pop ||
7583 func == bpf_skb_store_bytes ||
7584 func == bpf_skb_change_proto ||
7585 func == bpf_skb_change_head ||
7586 func == sk_skb_change_head ||
7587 func == bpf_skb_change_tail ||
7588 func == sk_skb_change_tail ||
7589 func == bpf_skb_adjust_room ||
7590 func == sk_skb_adjust_room ||
7591 func == bpf_skb_pull_data ||
7592 func == sk_skb_pull_data ||
7593 func == bpf_clone_redirect ||
7594 func == bpf_l3_csum_replace ||
7595 func == bpf_l4_csum_replace ||
7596 func == bpf_xdp_adjust_head ||
7597 func == bpf_xdp_adjust_meta ||
7598 func == bpf_msg_pull_data ||
7599 func == bpf_msg_push_data ||
7600 func == bpf_msg_pop_data ||
7601 func == bpf_xdp_adjust_tail ||
7602 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
7603 func == bpf_lwt_seg6_store_bytes ||
7604 func == bpf_lwt_seg6_adjust_srh ||
7605 func == bpf_lwt_seg6_action ||
7608 func == bpf_sock_ops_store_hdr_opt ||
7610 func == bpf_lwt_in_push_encap ||
7611 func == bpf_lwt_xmit_push_encap)
7617 const struct bpf_func_proto bpf_event_output_data_proto __weak;
7618 const struct bpf_func_proto bpf_sk_storage_get_cg_sock_proto __weak;
7620 static const struct bpf_func_proto *
7621 sock_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
7623 const struct bpf_func_proto *func_proto;
7625 func_proto = cgroup_common_func_proto(func_id, prog);
7629 func_proto = cgroup_current_func_proto(func_id, prog);
7634 case BPF_FUNC_get_socket_cookie:
7635 return &bpf_get_socket_cookie_sock_proto;
7636 case BPF_FUNC_get_netns_cookie:
7637 return &bpf_get_netns_cookie_sock_proto;
7638 case BPF_FUNC_perf_event_output:
7639 return &bpf_event_output_data_proto;
7640 case BPF_FUNC_sk_storage_get:
7641 return &bpf_sk_storage_get_cg_sock_proto;
7642 case BPF_FUNC_ktime_get_coarse_ns:
7643 return &bpf_ktime_get_coarse_ns_proto;
7645 return bpf_base_func_proto(func_id);
7649 static const struct bpf_func_proto *
7650 sock_addr_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
7652 const struct bpf_func_proto *func_proto;
7654 func_proto = cgroup_common_func_proto(func_id, prog);
7658 func_proto = cgroup_current_func_proto(func_id, prog);
7664 switch (prog->expected_attach_type) {
7665 case BPF_CGROUP_INET4_CONNECT:
7666 case BPF_CGROUP_INET6_CONNECT:
7667 return &bpf_bind_proto;
7671 case BPF_FUNC_get_socket_cookie:
7672 return &bpf_get_socket_cookie_sock_addr_proto;
7673 case BPF_FUNC_get_netns_cookie:
7674 return &bpf_get_netns_cookie_sock_addr_proto;
7675 case BPF_FUNC_perf_event_output:
7676 return &bpf_event_output_data_proto;
7678 case BPF_FUNC_sk_lookup_tcp:
7679 return &bpf_sock_addr_sk_lookup_tcp_proto;
7680 case BPF_FUNC_sk_lookup_udp:
7681 return &bpf_sock_addr_sk_lookup_udp_proto;
7682 case BPF_FUNC_sk_release:
7683 return &bpf_sk_release_proto;
7684 case BPF_FUNC_skc_lookup_tcp:
7685 return &bpf_sock_addr_skc_lookup_tcp_proto;
7686 #endif /* CONFIG_INET */
7687 case BPF_FUNC_sk_storage_get:
7688 return &bpf_sk_storage_get_proto;
7689 case BPF_FUNC_sk_storage_delete:
7690 return &bpf_sk_storage_delete_proto;
7691 case BPF_FUNC_setsockopt:
7692 switch (prog->expected_attach_type) {
7693 case BPF_CGROUP_INET4_BIND:
7694 case BPF_CGROUP_INET6_BIND:
7695 case BPF_CGROUP_INET4_CONNECT:
7696 case BPF_CGROUP_INET6_CONNECT:
7697 case BPF_CGROUP_UDP4_RECVMSG:
7698 case BPF_CGROUP_UDP6_RECVMSG:
7699 case BPF_CGROUP_UDP4_SENDMSG:
7700 case BPF_CGROUP_UDP6_SENDMSG:
7701 case BPF_CGROUP_INET4_GETPEERNAME:
7702 case BPF_CGROUP_INET6_GETPEERNAME:
7703 case BPF_CGROUP_INET4_GETSOCKNAME:
7704 case BPF_CGROUP_INET6_GETSOCKNAME:
7705 return &bpf_sock_addr_setsockopt_proto;
7709 case BPF_FUNC_getsockopt:
7710 switch (prog->expected_attach_type) {
7711 case BPF_CGROUP_INET4_BIND:
7712 case BPF_CGROUP_INET6_BIND:
7713 case BPF_CGROUP_INET4_CONNECT:
7714 case BPF_CGROUP_INET6_CONNECT:
7715 case BPF_CGROUP_UDP4_RECVMSG:
7716 case BPF_CGROUP_UDP6_RECVMSG:
7717 case BPF_CGROUP_UDP4_SENDMSG:
7718 case BPF_CGROUP_UDP6_SENDMSG:
7719 case BPF_CGROUP_INET4_GETPEERNAME:
7720 case BPF_CGROUP_INET6_GETPEERNAME:
7721 case BPF_CGROUP_INET4_GETSOCKNAME:
7722 case BPF_CGROUP_INET6_GETSOCKNAME:
7723 return &bpf_sock_addr_getsockopt_proto;
7728 return bpf_sk_base_func_proto(func_id);
7732 static const struct bpf_func_proto *
7733 sk_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
7736 case BPF_FUNC_skb_load_bytes:
7737 return &bpf_skb_load_bytes_proto;
7738 case BPF_FUNC_skb_load_bytes_relative:
7739 return &bpf_skb_load_bytes_relative_proto;
7740 case BPF_FUNC_get_socket_cookie:
7741 return &bpf_get_socket_cookie_proto;
7742 case BPF_FUNC_get_socket_uid:
7743 return &bpf_get_socket_uid_proto;
7744 case BPF_FUNC_perf_event_output:
7745 return &bpf_skb_event_output_proto;
7747 return bpf_sk_base_func_proto(func_id);
7751 const struct bpf_func_proto bpf_sk_storage_get_proto __weak;
7752 const struct bpf_func_proto bpf_sk_storage_delete_proto __weak;
7754 static const struct bpf_func_proto *
7755 cg_skb_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
7757 const struct bpf_func_proto *func_proto;
7759 func_proto = cgroup_common_func_proto(func_id, prog);
7764 case BPF_FUNC_sk_fullsock:
7765 return &bpf_sk_fullsock_proto;
7766 case BPF_FUNC_sk_storage_get:
7767 return &bpf_sk_storage_get_proto;
7768 case BPF_FUNC_sk_storage_delete:
7769 return &bpf_sk_storage_delete_proto;
7770 case BPF_FUNC_perf_event_output:
7771 return &bpf_skb_event_output_proto;
7772 #ifdef CONFIG_SOCK_CGROUP_DATA
7773 case BPF_FUNC_skb_cgroup_id:
7774 return &bpf_skb_cgroup_id_proto;
7775 case BPF_FUNC_skb_ancestor_cgroup_id:
7776 return &bpf_skb_ancestor_cgroup_id_proto;
7777 case BPF_FUNC_sk_cgroup_id:
7778 return &bpf_sk_cgroup_id_proto;
7779 case BPF_FUNC_sk_ancestor_cgroup_id:
7780 return &bpf_sk_ancestor_cgroup_id_proto;
7783 case BPF_FUNC_sk_lookup_tcp:
7784 return &bpf_sk_lookup_tcp_proto;
7785 case BPF_FUNC_sk_lookup_udp:
7786 return &bpf_sk_lookup_udp_proto;
7787 case BPF_FUNC_sk_release:
7788 return &bpf_sk_release_proto;
7789 case BPF_FUNC_skc_lookup_tcp:
7790 return &bpf_skc_lookup_tcp_proto;
7791 case BPF_FUNC_tcp_sock:
7792 return &bpf_tcp_sock_proto;
7793 case BPF_FUNC_get_listener_sock:
7794 return &bpf_get_listener_sock_proto;
7795 case BPF_FUNC_skb_ecn_set_ce:
7796 return &bpf_skb_ecn_set_ce_proto;
7799 return sk_filter_func_proto(func_id, prog);
7803 static const struct bpf_func_proto *
7804 tc_cls_act_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
7807 case BPF_FUNC_skb_store_bytes:
7808 return &bpf_skb_store_bytes_proto;
7809 case BPF_FUNC_skb_load_bytes:
7810 return &bpf_skb_load_bytes_proto;
7811 case BPF_FUNC_skb_load_bytes_relative:
7812 return &bpf_skb_load_bytes_relative_proto;
7813 case BPF_FUNC_skb_pull_data:
7814 return &bpf_skb_pull_data_proto;
7815 case BPF_FUNC_csum_diff:
7816 return &bpf_csum_diff_proto;
7817 case BPF_FUNC_csum_update:
7818 return &bpf_csum_update_proto;
7819 case BPF_FUNC_csum_level:
7820 return &bpf_csum_level_proto;
7821 case BPF_FUNC_l3_csum_replace:
7822 return &bpf_l3_csum_replace_proto;
7823 case BPF_FUNC_l4_csum_replace:
7824 return &bpf_l4_csum_replace_proto;
7825 case BPF_FUNC_clone_redirect:
7826 return &bpf_clone_redirect_proto;
7827 case BPF_FUNC_get_cgroup_classid:
7828 return &bpf_get_cgroup_classid_proto;
7829 case BPF_FUNC_skb_vlan_push:
7830 return &bpf_skb_vlan_push_proto;
7831 case BPF_FUNC_skb_vlan_pop:
7832 return &bpf_skb_vlan_pop_proto;
7833 case BPF_FUNC_skb_change_proto:
7834 return &bpf_skb_change_proto_proto;
7835 case BPF_FUNC_skb_change_type:
7836 return &bpf_skb_change_type_proto;
7837 case BPF_FUNC_skb_adjust_room:
7838 return &bpf_skb_adjust_room_proto;
7839 case BPF_FUNC_skb_change_tail:
7840 return &bpf_skb_change_tail_proto;
7841 case BPF_FUNC_skb_change_head:
7842 return &bpf_skb_change_head_proto;
7843 case BPF_FUNC_skb_get_tunnel_key:
7844 return &bpf_skb_get_tunnel_key_proto;
7845 case BPF_FUNC_skb_set_tunnel_key:
7846 return bpf_get_skb_set_tunnel_proto(func_id);
7847 case BPF_FUNC_skb_get_tunnel_opt:
7848 return &bpf_skb_get_tunnel_opt_proto;
7849 case BPF_FUNC_skb_set_tunnel_opt:
7850 return bpf_get_skb_set_tunnel_proto(func_id);
7851 case BPF_FUNC_redirect:
7852 return &bpf_redirect_proto;
7853 case BPF_FUNC_redirect_neigh:
7854 return &bpf_redirect_neigh_proto;
7855 case BPF_FUNC_redirect_peer:
7856 return &bpf_redirect_peer_proto;
7857 case BPF_FUNC_get_route_realm:
7858 return &bpf_get_route_realm_proto;
7859 case BPF_FUNC_get_hash_recalc:
7860 return &bpf_get_hash_recalc_proto;
7861 case BPF_FUNC_set_hash_invalid:
7862 return &bpf_set_hash_invalid_proto;
7863 case BPF_FUNC_set_hash:
7864 return &bpf_set_hash_proto;
7865 case BPF_FUNC_perf_event_output:
7866 return &bpf_skb_event_output_proto;
7867 case BPF_FUNC_get_smp_processor_id:
7868 return &bpf_get_smp_processor_id_proto;
7869 case BPF_FUNC_skb_under_cgroup:
7870 return &bpf_skb_under_cgroup_proto;
7871 case BPF_FUNC_get_socket_cookie:
7872 return &bpf_get_socket_cookie_proto;
7873 case BPF_FUNC_get_socket_uid:
7874 return &bpf_get_socket_uid_proto;
7875 case BPF_FUNC_fib_lookup:
7876 return &bpf_skb_fib_lookup_proto;
7877 case BPF_FUNC_check_mtu:
7878 return &bpf_skb_check_mtu_proto;
7879 case BPF_FUNC_sk_fullsock:
7880 return &bpf_sk_fullsock_proto;
7881 case BPF_FUNC_sk_storage_get:
7882 return &bpf_sk_storage_get_proto;
7883 case BPF_FUNC_sk_storage_delete:
7884 return &bpf_sk_storage_delete_proto;
7886 case BPF_FUNC_skb_get_xfrm_state:
7887 return &bpf_skb_get_xfrm_state_proto;
7889 #ifdef CONFIG_CGROUP_NET_CLASSID
7890 case BPF_FUNC_skb_cgroup_classid:
7891 return &bpf_skb_cgroup_classid_proto;
7893 #ifdef CONFIG_SOCK_CGROUP_DATA
7894 case BPF_FUNC_skb_cgroup_id:
7895 return &bpf_skb_cgroup_id_proto;
7896 case BPF_FUNC_skb_ancestor_cgroup_id:
7897 return &bpf_skb_ancestor_cgroup_id_proto;
7900 case BPF_FUNC_sk_lookup_tcp:
7901 return &bpf_sk_lookup_tcp_proto;
7902 case BPF_FUNC_sk_lookup_udp:
7903 return &bpf_sk_lookup_udp_proto;
7904 case BPF_FUNC_sk_release:
7905 return &bpf_sk_release_proto;
7906 case BPF_FUNC_tcp_sock:
7907 return &bpf_tcp_sock_proto;
7908 case BPF_FUNC_get_listener_sock:
7909 return &bpf_get_listener_sock_proto;
7910 case BPF_FUNC_skc_lookup_tcp:
7911 return &bpf_skc_lookup_tcp_proto;
7912 case BPF_FUNC_tcp_check_syncookie:
7913 return &bpf_tcp_check_syncookie_proto;
7914 case BPF_FUNC_skb_ecn_set_ce:
7915 return &bpf_skb_ecn_set_ce_proto;
7916 case BPF_FUNC_tcp_gen_syncookie:
7917 return &bpf_tcp_gen_syncookie_proto;
7918 case BPF_FUNC_sk_assign:
7919 return &bpf_sk_assign_proto;
7920 case BPF_FUNC_skb_set_tstamp:
7921 return &bpf_skb_set_tstamp_proto;
7922 #ifdef CONFIG_SYN_COOKIES
7923 case BPF_FUNC_tcp_raw_gen_syncookie_ipv4:
7924 return &bpf_tcp_raw_gen_syncookie_ipv4_proto;
7925 case BPF_FUNC_tcp_raw_gen_syncookie_ipv6:
7926 return &bpf_tcp_raw_gen_syncookie_ipv6_proto;
7927 case BPF_FUNC_tcp_raw_check_syncookie_ipv4:
7928 return &bpf_tcp_raw_check_syncookie_ipv4_proto;
7929 case BPF_FUNC_tcp_raw_check_syncookie_ipv6:
7930 return &bpf_tcp_raw_check_syncookie_ipv6_proto;
7934 return bpf_sk_base_func_proto(func_id);
7938 static const struct bpf_func_proto *
7939 xdp_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
7942 case BPF_FUNC_perf_event_output:
7943 return &bpf_xdp_event_output_proto;
7944 case BPF_FUNC_get_smp_processor_id:
7945 return &bpf_get_smp_processor_id_proto;
7946 case BPF_FUNC_csum_diff:
7947 return &bpf_csum_diff_proto;
7948 case BPF_FUNC_xdp_adjust_head:
7949 return &bpf_xdp_adjust_head_proto;
7950 case BPF_FUNC_xdp_adjust_meta:
7951 return &bpf_xdp_adjust_meta_proto;
7952 case BPF_FUNC_redirect:
7953 return &bpf_xdp_redirect_proto;
7954 case BPF_FUNC_redirect_map:
7955 return &bpf_xdp_redirect_map_proto;
7956 case BPF_FUNC_xdp_adjust_tail:
7957 return &bpf_xdp_adjust_tail_proto;
7958 case BPF_FUNC_xdp_get_buff_len:
7959 return &bpf_xdp_get_buff_len_proto;
7960 case BPF_FUNC_xdp_load_bytes:
7961 return &bpf_xdp_load_bytes_proto;
7962 case BPF_FUNC_xdp_store_bytes:
7963 return &bpf_xdp_store_bytes_proto;
7964 case BPF_FUNC_fib_lookup:
7965 return &bpf_xdp_fib_lookup_proto;
7966 case BPF_FUNC_check_mtu:
7967 return &bpf_xdp_check_mtu_proto;
7969 case BPF_FUNC_sk_lookup_udp:
7970 return &bpf_xdp_sk_lookup_udp_proto;
7971 case BPF_FUNC_sk_lookup_tcp:
7972 return &bpf_xdp_sk_lookup_tcp_proto;
7973 case BPF_FUNC_sk_release:
7974 return &bpf_sk_release_proto;
7975 case BPF_FUNC_skc_lookup_tcp:
7976 return &bpf_xdp_skc_lookup_tcp_proto;
7977 case BPF_FUNC_tcp_check_syncookie:
7978 return &bpf_tcp_check_syncookie_proto;
7979 case BPF_FUNC_tcp_gen_syncookie:
7980 return &bpf_tcp_gen_syncookie_proto;
7981 #ifdef CONFIG_SYN_COOKIES
7982 case BPF_FUNC_tcp_raw_gen_syncookie_ipv4:
7983 return &bpf_tcp_raw_gen_syncookie_ipv4_proto;
7984 case BPF_FUNC_tcp_raw_gen_syncookie_ipv6:
7985 return &bpf_tcp_raw_gen_syncookie_ipv6_proto;
7986 case BPF_FUNC_tcp_raw_check_syncookie_ipv4:
7987 return &bpf_tcp_raw_check_syncookie_ipv4_proto;
7988 case BPF_FUNC_tcp_raw_check_syncookie_ipv6:
7989 return &bpf_tcp_raw_check_syncookie_ipv6_proto;
7993 return bpf_sk_base_func_proto(func_id);
7997 const struct bpf_func_proto bpf_sock_map_update_proto __weak;
7998 const struct bpf_func_proto bpf_sock_hash_update_proto __weak;
8000 static const struct bpf_func_proto *
8001 sock_ops_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
8003 const struct bpf_func_proto *func_proto;
8005 func_proto = cgroup_common_func_proto(func_id, prog);
8010 case BPF_FUNC_setsockopt:
8011 return &bpf_sock_ops_setsockopt_proto;
8012 case BPF_FUNC_getsockopt:
8013 return &bpf_sock_ops_getsockopt_proto;
8014 case BPF_FUNC_sock_ops_cb_flags_set:
8015 return &bpf_sock_ops_cb_flags_set_proto;
8016 case BPF_FUNC_sock_map_update:
8017 return &bpf_sock_map_update_proto;
8018 case BPF_FUNC_sock_hash_update:
8019 return &bpf_sock_hash_update_proto;
8020 case BPF_FUNC_get_socket_cookie:
8021 return &bpf_get_socket_cookie_sock_ops_proto;
8022 case BPF_FUNC_perf_event_output:
8023 return &bpf_event_output_data_proto;
8024 case BPF_FUNC_sk_storage_get:
8025 return &bpf_sk_storage_get_proto;
8026 case BPF_FUNC_sk_storage_delete:
8027 return &bpf_sk_storage_delete_proto;
8028 case BPF_FUNC_get_netns_cookie:
8029 return &bpf_get_netns_cookie_sock_ops_proto;
8031 case BPF_FUNC_load_hdr_opt:
8032 return &bpf_sock_ops_load_hdr_opt_proto;
8033 case BPF_FUNC_store_hdr_opt:
8034 return &bpf_sock_ops_store_hdr_opt_proto;
8035 case BPF_FUNC_reserve_hdr_opt:
8036 return &bpf_sock_ops_reserve_hdr_opt_proto;
8037 case BPF_FUNC_tcp_sock:
8038 return &bpf_tcp_sock_proto;
8039 #endif /* CONFIG_INET */
8041 return bpf_sk_base_func_proto(func_id);
8045 const struct bpf_func_proto bpf_msg_redirect_map_proto __weak;
8046 const struct bpf_func_proto bpf_msg_redirect_hash_proto __weak;
8048 static const struct bpf_func_proto *
8049 sk_msg_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
8052 case BPF_FUNC_msg_redirect_map:
8053 return &bpf_msg_redirect_map_proto;
8054 case BPF_FUNC_msg_redirect_hash:
8055 return &bpf_msg_redirect_hash_proto;
8056 case BPF_FUNC_msg_apply_bytes:
8057 return &bpf_msg_apply_bytes_proto;
8058 case BPF_FUNC_msg_cork_bytes:
8059 return &bpf_msg_cork_bytes_proto;
8060 case BPF_FUNC_msg_pull_data:
8061 return &bpf_msg_pull_data_proto;
8062 case BPF_FUNC_msg_push_data:
8063 return &bpf_msg_push_data_proto;
8064 case BPF_FUNC_msg_pop_data:
8065 return &bpf_msg_pop_data_proto;
8066 case BPF_FUNC_perf_event_output:
8067 return &bpf_event_output_data_proto;
8068 case BPF_FUNC_get_current_uid_gid:
8069 return &bpf_get_current_uid_gid_proto;
8070 case BPF_FUNC_get_current_pid_tgid:
8071 return &bpf_get_current_pid_tgid_proto;
8072 case BPF_FUNC_sk_storage_get:
8073 return &bpf_sk_storage_get_proto;
8074 case BPF_FUNC_sk_storage_delete:
8075 return &bpf_sk_storage_delete_proto;
8076 case BPF_FUNC_get_netns_cookie:
8077 return &bpf_get_netns_cookie_sk_msg_proto;
8078 #ifdef CONFIG_CGROUPS
8079 case BPF_FUNC_get_current_cgroup_id:
8080 return &bpf_get_current_cgroup_id_proto;
8081 case BPF_FUNC_get_current_ancestor_cgroup_id:
8082 return &bpf_get_current_ancestor_cgroup_id_proto;
8084 #ifdef CONFIG_CGROUP_NET_CLASSID
8085 case BPF_FUNC_get_cgroup_classid:
8086 return &bpf_get_cgroup_classid_curr_proto;
8089 return bpf_sk_base_func_proto(func_id);
8093 const struct bpf_func_proto bpf_sk_redirect_map_proto __weak;
8094 const struct bpf_func_proto bpf_sk_redirect_hash_proto __weak;
8096 static const struct bpf_func_proto *
8097 sk_skb_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
8100 case BPF_FUNC_skb_store_bytes:
8101 return &bpf_skb_store_bytes_proto;
8102 case BPF_FUNC_skb_load_bytes:
8103 return &bpf_skb_load_bytes_proto;
8104 case BPF_FUNC_skb_pull_data:
8105 return &sk_skb_pull_data_proto;
8106 case BPF_FUNC_skb_change_tail:
8107 return &sk_skb_change_tail_proto;
8108 case BPF_FUNC_skb_change_head:
8109 return &sk_skb_change_head_proto;
8110 case BPF_FUNC_skb_adjust_room:
8111 return &sk_skb_adjust_room_proto;
8112 case BPF_FUNC_get_socket_cookie:
8113 return &bpf_get_socket_cookie_proto;
8114 case BPF_FUNC_get_socket_uid:
8115 return &bpf_get_socket_uid_proto;
8116 case BPF_FUNC_sk_redirect_map:
8117 return &bpf_sk_redirect_map_proto;
8118 case BPF_FUNC_sk_redirect_hash:
8119 return &bpf_sk_redirect_hash_proto;
8120 case BPF_FUNC_perf_event_output:
8121 return &bpf_skb_event_output_proto;
8123 case BPF_FUNC_sk_lookup_tcp:
8124 return &bpf_sk_lookup_tcp_proto;
8125 case BPF_FUNC_sk_lookup_udp:
8126 return &bpf_sk_lookup_udp_proto;
8127 case BPF_FUNC_sk_release:
8128 return &bpf_sk_release_proto;
8129 case BPF_FUNC_skc_lookup_tcp:
8130 return &bpf_skc_lookup_tcp_proto;
8133 return bpf_sk_base_func_proto(func_id);
8137 static const struct bpf_func_proto *
8138 flow_dissector_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
8141 case BPF_FUNC_skb_load_bytes:
8142 return &bpf_flow_dissector_load_bytes_proto;
8144 return bpf_sk_base_func_proto(func_id);
8148 static const struct bpf_func_proto *
8149 lwt_out_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
8152 case BPF_FUNC_skb_load_bytes:
8153 return &bpf_skb_load_bytes_proto;
8154 case BPF_FUNC_skb_pull_data:
8155 return &bpf_skb_pull_data_proto;
8156 case BPF_FUNC_csum_diff:
8157 return &bpf_csum_diff_proto;
8158 case BPF_FUNC_get_cgroup_classid:
8159 return &bpf_get_cgroup_classid_proto;
8160 case BPF_FUNC_get_route_realm:
8161 return &bpf_get_route_realm_proto;
8162 case BPF_FUNC_get_hash_recalc:
8163 return &bpf_get_hash_recalc_proto;
8164 case BPF_FUNC_perf_event_output:
8165 return &bpf_skb_event_output_proto;
8166 case BPF_FUNC_get_smp_processor_id:
8167 return &bpf_get_smp_processor_id_proto;
8168 case BPF_FUNC_skb_under_cgroup:
8169 return &bpf_skb_under_cgroup_proto;
8171 return bpf_sk_base_func_proto(func_id);
8175 static const struct bpf_func_proto *
8176 lwt_in_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
8179 case BPF_FUNC_lwt_push_encap:
8180 return &bpf_lwt_in_push_encap_proto;
8182 return lwt_out_func_proto(func_id, prog);
8186 static const struct bpf_func_proto *
8187 lwt_xmit_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
8190 case BPF_FUNC_skb_get_tunnel_key:
8191 return &bpf_skb_get_tunnel_key_proto;
8192 case BPF_FUNC_skb_set_tunnel_key:
8193 return bpf_get_skb_set_tunnel_proto(func_id);
8194 case BPF_FUNC_skb_get_tunnel_opt:
8195 return &bpf_skb_get_tunnel_opt_proto;
8196 case BPF_FUNC_skb_set_tunnel_opt:
8197 return bpf_get_skb_set_tunnel_proto(func_id);
8198 case BPF_FUNC_redirect:
8199 return &bpf_redirect_proto;
8200 case BPF_FUNC_clone_redirect:
8201 return &bpf_clone_redirect_proto;
8202 case BPF_FUNC_skb_change_tail:
8203 return &bpf_skb_change_tail_proto;
8204 case BPF_FUNC_skb_change_head:
8205 return &bpf_skb_change_head_proto;
8206 case BPF_FUNC_skb_store_bytes:
8207 return &bpf_skb_store_bytes_proto;
8208 case BPF_FUNC_csum_update:
8209 return &bpf_csum_update_proto;
8210 case BPF_FUNC_csum_level:
8211 return &bpf_csum_level_proto;
8212 case BPF_FUNC_l3_csum_replace:
8213 return &bpf_l3_csum_replace_proto;
8214 case BPF_FUNC_l4_csum_replace:
8215 return &bpf_l4_csum_replace_proto;
8216 case BPF_FUNC_set_hash_invalid:
8217 return &bpf_set_hash_invalid_proto;
8218 case BPF_FUNC_lwt_push_encap:
8219 return &bpf_lwt_xmit_push_encap_proto;
8221 return lwt_out_func_proto(func_id, prog);
8225 static const struct bpf_func_proto *
8226 lwt_seg6local_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
8229 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
8230 case BPF_FUNC_lwt_seg6_store_bytes:
8231 return &bpf_lwt_seg6_store_bytes_proto;
8232 case BPF_FUNC_lwt_seg6_action:
8233 return &bpf_lwt_seg6_action_proto;
8234 case BPF_FUNC_lwt_seg6_adjust_srh:
8235 return &bpf_lwt_seg6_adjust_srh_proto;
8238 return lwt_out_func_proto(func_id, prog);
8242 static bool bpf_skb_is_valid_access(int off, int size, enum bpf_access_type type,
8243 const struct bpf_prog *prog,
8244 struct bpf_insn_access_aux *info)
8246 const int size_default = sizeof(__u32);
8248 if (off < 0 || off >= sizeof(struct __sk_buff))
8251 /* The verifier guarantees that size > 0. */
8252 if (off % size != 0)
8256 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
8257 if (off + size > offsetofend(struct __sk_buff, cb[4]))
8260 case bpf_ctx_range_till(struct __sk_buff, remote_ip6[0], remote_ip6[3]):
8261 case bpf_ctx_range_till(struct __sk_buff, local_ip6[0], local_ip6[3]):
8262 case bpf_ctx_range_till(struct __sk_buff, remote_ip4, remote_ip4):
8263 case bpf_ctx_range_till(struct __sk_buff, local_ip4, local_ip4):
8264 case bpf_ctx_range(struct __sk_buff, data):
8265 case bpf_ctx_range(struct __sk_buff, data_meta):
8266 case bpf_ctx_range(struct __sk_buff, data_end):
8267 if (size != size_default)
8270 case bpf_ctx_range_ptr(struct __sk_buff, flow_keys):
8272 case bpf_ctx_range(struct __sk_buff, hwtstamp):
8273 if (type == BPF_WRITE || size != sizeof(__u64))
8276 case bpf_ctx_range(struct __sk_buff, tstamp):
8277 if (size != sizeof(__u64))
8280 case offsetof(struct __sk_buff, sk):
8281 if (type == BPF_WRITE || size != sizeof(__u64))
8283 info->reg_type = PTR_TO_SOCK_COMMON_OR_NULL;
8285 case offsetof(struct __sk_buff, tstamp_type):
8287 case offsetofend(struct __sk_buff, tstamp_type) ... offsetof(struct __sk_buff, hwtstamp) - 1:
8288 /* Explicitly prohibit access to padding in __sk_buff. */
8291 /* Only narrow read access allowed for now. */
8292 if (type == BPF_WRITE) {
8293 if (size != size_default)
8296 bpf_ctx_record_field_size(info, size_default);
8297 if (!bpf_ctx_narrow_access_ok(off, size, size_default))
8305 static bool sk_filter_is_valid_access(int off, int size,
8306 enum bpf_access_type type,
8307 const struct bpf_prog *prog,
8308 struct bpf_insn_access_aux *info)
8311 case bpf_ctx_range(struct __sk_buff, tc_classid):
8312 case bpf_ctx_range(struct __sk_buff, data):
8313 case bpf_ctx_range(struct __sk_buff, data_meta):
8314 case bpf_ctx_range(struct __sk_buff, data_end):
8315 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
8316 case bpf_ctx_range(struct __sk_buff, tstamp):
8317 case bpf_ctx_range(struct __sk_buff, wire_len):
8318 case bpf_ctx_range(struct __sk_buff, hwtstamp):
8322 if (type == BPF_WRITE) {
8324 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
8331 return bpf_skb_is_valid_access(off, size, type, prog, info);
8334 static bool cg_skb_is_valid_access(int off, int size,
8335 enum bpf_access_type type,
8336 const struct bpf_prog *prog,
8337 struct bpf_insn_access_aux *info)
8340 case bpf_ctx_range(struct __sk_buff, tc_classid):
8341 case bpf_ctx_range(struct __sk_buff, data_meta):
8342 case bpf_ctx_range(struct __sk_buff, wire_len):
8344 case bpf_ctx_range(struct __sk_buff, data):
8345 case bpf_ctx_range(struct __sk_buff, data_end):
8351 if (type == BPF_WRITE) {
8353 case bpf_ctx_range(struct __sk_buff, mark):
8354 case bpf_ctx_range(struct __sk_buff, priority):
8355 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
8357 case bpf_ctx_range(struct __sk_buff, tstamp):
8367 case bpf_ctx_range(struct __sk_buff, data):
8368 info->reg_type = PTR_TO_PACKET;
8370 case bpf_ctx_range(struct __sk_buff, data_end):
8371 info->reg_type = PTR_TO_PACKET_END;
8375 return bpf_skb_is_valid_access(off, size, type, prog, info);
8378 static bool lwt_is_valid_access(int off, int size,
8379 enum bpf_access_type type,
8380 const struct bpf_prog *prog,
8381 struct bpf_insn_access_aux *info)
8384 case bpf_ctx_range(struct __sk_buff, tc_classid):
8385 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
8386 case bpf_ctx_range(struct __sk_buff, data_meta):
8387 case bpf_ctx_range(struct __sk_buff, tstamp):
8388 case bpf_ctx_range(struct __sk_buff, wire_len):
8389 case bpf_ctx_range(struct __sk_buff, hwtstamp):
8393 if (type == BPF_WRITE) {
8395 case bpf_ctx_range(struct __sk_buff, mark):
8396 case bpf_ctx_range(struct __sk_buff, priority):
8397 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
8405 case bpf_ctx_range(struct __sk_buff, data):
8406 info->reg_type = PTR_TO_PACKET;
8408 case bpf_ctx_range(struct __sk_buff, data_end):
8409 info->reg_type = PTR_TO_PACKET_END;
8413 return bpf_skb_is_valid_access(off, size, type, prog, info);
8416 /* Attach type specific accesses */
8417 static bool __sock_filter_check_attach_type(int off,
8418 enum bpf_access_type access_type,
8419 enum bpf_attach_type attach_type)
8422 case offsetof(struct bpf_sock, bound_dev_if):
8423 case offsetof(struct bpf_sock, mark):
8424 case offsetof(struct bpf_sock, priority):
8425 switch (attach_type) {
8426 case BPF_CGROUP_INET_SOCK_CREATE:
8427 case BPF_CGROUP_INET_SOCK_RELEASE:
8432 case bpf_ctx_range(struct bpf_sock, src_ip4):
8433 switch (attach_type) {
8434 case BPF_CGROUP_INET4_POST_BIND:
8439 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
8440 switch (attach_type) {
8441 case BPF_CGROUP_INET6_POST_BIND:
8446 case bpf_ctx_range(struct bpf_sock, src_port):
8447 switch (attach_type) {
8448 case BPF_CGROUP_INET4_POST_BIND:
8449 case BPF_CGROUP_INET6_POST_BIND:
8456 return access_type == BPF_READ;
8461 bool bpf_sock_common_is_valid_access(int off, int size,
8462 enum bpf_access_type type,
8463 struct bpf_insn_access_aux *info)
8466 case bpf_ctx_range_till(struct bpf_sock, type, priority):
8469 return bpf_sock_is_valid_access(off, size, type, info);
8473 bool bpf_sock_is_valid_access(int off, int size, enum bpf_access_type type,
8474 struct bpf_insn_access_aux *info)
8476 const int size_default = sizeof(__u32);
8479 if (off < 0 || off >= sizeof(struct bpf_sock))
8481 if (off % size != 0)
8485 case offsetof(struct bpf_sock, state):
8486 case offsetof(struct bpf_sock, family):
8487 case offsetof(struct bpf_sock, type):
8488 case offsetof(struct bpf_sock, protocol):
8489 case offsetof(struct bpf_sock, src_port):
8490 case offsetof(struct bpf_sock, rx_queue_mapping):
8491 case bpf_ctx_range(struct bpf_sock, src_ip4):
8492 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
8493 case bpf_ctx_range(struct bpf_sock, dst_ip4):
8494 case bpf_ctx_range_till(struct bpf_sock, dst_ip6[0], dst_ip6[3]):
8495 bpf_ctx_record_field_size(info, size_default);
8496 return bpf_ctx_narrow_access_ok(off, size, size_default);
8497 case bpf_ctx_range(struct bpf_sock, dst_port):
8498 field_size = size == size_default ?
8499 size_default : sizeof_field(struct bpf_sock, dst_port);
8500 bpf_ctx_record_field_size(info, field_size);
8501 return bpf_ctx_narrow_access_ok(off, size, field_size);
8502 case offsetofend(struct bpf_sock, dst_port) ...
8503 offsetof(struct bpf_sock, dst_ip4) - 1:
8507 return size == size_default;
8510 static bool sock_filter_is_valid_access(int off, int size,
8511 enum bpf_access_type type,
8512 const struct bpf_prog *prog,
8513 struct bpf_insn_access_aux *info)
8515 if (!bpf_sock_is_valid_access(off, size, type, info))
8517 return __sock_filter_check_attach_type(off, type,
8518 prog->expected_attach_type);
8521 static int bpf_noop_prologue(struct bpf_insn *insn_buf, bool direct_write,
8522 const struct bpf_prog *prog)
8524 /* Neither direct read nor direct write requires any preliminary
8530 static int bpf_unclone_prologue(struct bpf_insn *insn_buf, bool direct_write,
8531 const struct bpf_prog *prog, int drop_verdict)
8533 struct bpf_insn *insn = insn_buf;
8538 /* if (!skb->cloned)
8541 * (Fast-path, otherwise approximation that we might be
8542 * a clone, do the rest in helper.)
8544 *insn++ = BPF_LDX_MEM(BPF_B, BPF_REG_6, BPF_REG_1, CLONED_OFFSET);
8545 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_6, CLONED_MASK);
8546 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_6, 0, 7);
8548 /* ret = bpf_skb_pull_data(skb, 0); */
8549 *insn++ = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1);
8550 *insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_2, BPF_REG_2);
8551 *insn++ = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
8552 BPF_FUNC_skb_pull_data);
8555 * return TC_ACT_SHOT;
8557 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2);
8558 *insn++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_0, drop_verdict);
8559 *insn++ = BPF_EXIT_INSN();
8562 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_6);
8564 *insn++ = prog->insnsi[0];
8566 return insn - insn_buf;
8569 static int bpf_gen_ld_abs(const struct bpf_insn *orig,
8570 struct bpf_insn *insn_buf)
8572 bool indirect = BPF_MODE(orig->code) == BPF_IND;
8573 struct bpf_insn *insn = insn_buf;
8576 *insn++ = BPF_MOV64_IMM(BPF_REG_2, orig->imm);
8578 *insn++ = BPF_MOV64_REG(BPF_REG_2, orig->src_reg);
8580 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, orig->imm);
8582 /* We're guaranteed here that CTX is in R6. */
8583 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_CTX);
8585 switch (BPF_SIZE(orig->code)) {
8587 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_8_no_cache);
8590 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_16_no_cache);
8593 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_32_no_cache);
8597 *insn++ = BPF_JMP_IMM(BPF_JSGE, BPF_REG_0, 0, 2);
8598 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_0, BPF_REG_0);
8599 *insn++ = BPF_EXIT_INSN();
8601 return insn - insn_buf;
8604 static int tc_cls_act_prologue(struct bpf_insn *insn_buf, bool direct_write,
8605 const struct bpf_prog *prog)
8607 return bpf_unclone_prologue(insn_buf, direct_write, prog, TC_ACT_SHOT);
8610 static bool tc_cls_act_is_valid_access(int off, int size,
8611 enum bpf_access_type type,
8612 const struct bpf_prog *prog,
8613 struct bpf_insn_access_aux *info)
8615 if (type == BPF_WRITE) {
8617 case bpf_ctx_range(struct __sk_buff, mark):
8618 case bpf_ctx_range(struct __sk_buff, tc_index):
8619 case bpf_ctx_range(struct __sk_buff, priority):
8620 case bpf_ctx_range(struct __sk_buff, tc_classid):
8621 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
8622 case bpf_ctx_range(struct __sk_buff, tstamp):
8623 case bpf_ctx_range(struct __sk_buff, queue_mapping):
8631 case bpf_ctx_range(struct __sk_buff, data):
8632 info->reg_type = PTR_TO_PACKET;
8634 case bpf_ctx_range(struct __sk_buff, data_meta):
8635 info->reg_type = PTR_TO_PACKET_META;
8637 case bpf_ctx_range(struct __sk_buff, data_end):
8638 info->reg_type = PTR_TO_PACKET_END;
8640 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
8642 case offsetof(struct __sk_buff, tstamp_type):
8643 /* The convert_ctx_access() on reading and writing
8644 * __sk_buff->tstamp depends on whether the bpf prog
8645 * has used __sk_buff->tstamp_type or not.
8646 * Thus, we need to set prog->tstamp_type_access
8647 * earlier during is_valid_access() here.
8649 ((struct bpf_prog *)prog)->tstamp_type_access = 1;
8650 return size == sizeof(__u8);
8653 return bpf_skb_is_valid_access(off, size, type, prog, info);
8656 DEFINE_MUTEX(nf_conn_btf_access_lock);
8657 EXPORT_SYMBOL_GPL(nf_conn_btf_access_lock);
8659 int (*nfct_btf_struct_access)(struct bpf_verifier_log *log,
8660 const struct bpf_reg_state *reg,
8661 int off, int size, enum bpf_access_type atype,
8662 u32 *next_btf_id, enum bpf_type_flag *flag);
8663 EXPORT_SYMBOL_GPL(nfct_btf_struct_access);
8665 static int tc_cls_act_btf_struct_access(struct bpf_verifier_log *log,
8666 const struct bpf_reg_state *reg,
8667 int off, int size, enum bpf_access_type atype,
8668 u32 *next_btf_id, enum bpf_type_flag *flag)
8672 if (atype == BPF_READ)
8673 return btf_struct_access(log, reg, off, size, atype, next_btf_id, flag);
8675 mutex_lock(&nf_conn_btf_access_lock);
8676 if (nfct_btf_struct_access)
8677 ret = nfct_btf_struct_access(log, reg, off, size, atype, next_btf_id, flag);
8678 mutex_unlock(&nf_conn_btf_access_lock);
8683 static bool __is_valid_xdp_access(int off, int size)
8685 if (off < 0 || off >= sizeof(struct xdp_md))
8687 if (off % size != 0)
8689 if (size != sizeof(__u32))
8695 static bool xdp_is_valid_access(int off, int size,
8696 enum bpf_access_type type,
8697 const struct bpf_prog *prog,
8698 struct bpf_insn_access_aux *info)
8700 if (prog->expected_attach_type != BPF_XDP_DEVMAP) {
8702 case offsetof(struct xdp_md, egress_ifindex):
8707 if (type == BPF_WRITE) {
8708 if (bpf_prog_is_dev_bound(prog->aux)) {
8710 case offsetof(struct xdp_md, rx_queue_index):
8711 return __is_valid_xdp_access(off, size);
8718 case offsetof(struct xdp_md, data):
8719 info->reg_type = PTR_TO_PACKET;
8721 case offsetof(struct xdp_md, data_meta):
8722 info->reg_type = PTR_TO_PACKET_META;
8724 case offsetof(struct xdp_md, data_end):
8725 info->reg_type = PTR_TO_PACKET_END;
8729 return __is_valid_xdp_access(off, size);
8732 void bpf_warn_invalid_xdp_action(struct net_device *dev, struct bpf_prog *prog, u32 act)
8734 const u32 act_max = XDP_REDIRECT;
8736 pr_warn_once("%s XDP return value %u on prog %s (id %d) dev %s, expect packet loss!\n",
8737 act > act_max ? "Illegal" : "Driver unsupported",
8738 act, prog->aux->name, prog->aux->id, dev ? dev->name : "N/A");
8740 EXPORT_SYMBOL_GPL(bpf_warn_invalid_xdp_action);
8742 static int xdp_btf_struct_access(struct bpf_verifier_log *log,
8743 const struct bpf_reg_state *reg,
8744 int off, int size, enum bpf_access_type atype,
8745 u32 *next_btf_id, enum bpf_type_flag *flag)
8749 if (atype == BPF_READ)
8750 return btf_struct_access(log, reg, off, size, atype, next_btf_id, flag);
8752 mutex_lock(&nf_conn_btf_access_lock);
8753 if (nfct_btf_struct_access)
8754 ret = nfct_btf_struct_access(log, reg, off, size, atype, next_btf_id, flag);
8755 mutex_unlock(&nf_conn_btf_access_lock);
8760 static bool sock_addr_is_valid_access(int off, int size,
8761 enum bpf_access_type type,
8762 const struct bpf_prog *prog,
8763 struct bpf_insn_access_aux *info)
8765 const int size_default = sizeof(__u32);
8767 if (off < 0 || off >= sizeof(struct bpf_sock_addr))
8769 if (off % size != 0)
8772 /* Disallow access to IPv6 fields from IPv4 contex and vise
8776 case bpf_ctx_range(struct bpf_sock_addr, user_ip4):
8777 switch (prog->expected_attach_type) {
8778 case BPF_CGROUP_INET4_BIND:
8779 case BPF_CGROUP_INET4_CONNECT:
8780 case BPF_CGROUP_INET4_GETPEERNAME:
8781 case BPF_CGROUP_INET4_GETSOCKNAME:
8782 case BPF_CGROUP_UDP4_SENDMSG:
8783 case BPF_CGROUP_UDP4_RECVMSG:
8789 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
8790 switch (prog->expected_attach_type) {
8791 case BPF_CGROUP_INET6_BIND:
8792 case BPF_CGROUP_INET6_CONNECT:
8793 case BPF_CGROUP_INET6_GETPEERNAME:
8794 case BPF_CGROUP_INET6_GETSOCKNAME:
8795 case BPF_CGROUP_UDP6_SENDMSG:
8796 case BPF_CGROUP_UDP6_RECVMSG:
8802 case bpf_ctx_range(struct bpf_sock_addr, msg_src_ip4):
8803 switch (prog->expected_attach_type) {
8804 case BPF_CGROUP_UDP4_SENDMSG:
8810 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
8812 switch (prog->expected_attach_type) {
8813 case BPF_CGROUP_UDP6_SENDMSG:
8822 case bpf_ctx_range(struct bpf_sock_addr, user_ip4):
8823 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
8824 case bpf_ctx_range(struct bpf_sock_addr, msg_src_ip4):
8825 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
8827 case bpf_ctx_range(struct bpf_sock_addr, user_port):
8828 if (type == BPF_READ) {
8829 bpf_ctx_record_field_size(info, size_default);
8831 if (bpf_ctx_wide_access_ok(off, size,
8832 struct bpf_sock_addr,
8836 if (bpf_ctx_wide_access_ok(off, size,
8837 struct bpf_sock_addr,
8841 if (!bpf_ctx_narrow_access_ok(off, size, size_default))
8844 if (bpf_ctx_wide_access_ok(off, size,
8845 struct bpf_sock_addr,
8849 if (bpf_ctx_wide_access_ok(off, size,
8850 struct bpf_sock_addr,
8854 if (size != size_default)
8858 case offsetof(struct bpf_sock_addr, sk):
8859 if (type != BPF_READ)
8861 if (size != sizeof(__u64))
8863 info->reg_type = PTR_TO_SOCKET;
8866 if (type == BPF_READ) {
8867 if (size != size_default)
8877 static bool sock_ops_is_valid_access(int off, int size,
8878 enum bpf_access_type type,
8879 const struct bpf_prog *prog,
8880 struct bpf_insn_access_aux *info)
8882 const int size_default = sizeof(__u32);
8884 if (off < 0 || off >= sizeof(struct bpf_sock_ops))
8887 /* The verifier guarantees that size > 0. */
8888 if (off % size != 0)
8891 if (type == BPF_WRITE) {
8893 case offsetof(struct bpf_sock_ops, reply):
8894 case offsetof(struct bpf_sock_ops, sk_txhash):
8895 if (size != size_default)
8903 case bpf_ctx_range_till(struct bpf_sock_ops, bytes_received,
8905 if (size != sizeof(__u64))
8908 case offsetof(struct bpf_sock_ops, sk):
8909 if (size != sizeof(__u64))
8911 info->reg_type = PTR_TO_SOCKET_OR_NULL;
8913 case offsetof(struct bpf_sock_ops, skb_data):
8914 if (size != sizeof(__u64))
8916 info->reg_type = PTR_TO_PACKET;
8918 case offsetof(struct bpf_sock_ops, skb_data_end):
8919 if (size != sizeof(__u64))
8921 info->reg_type = PTR_TO_PACKET_END;
8923 case offsetof(struct bpf_sock_ops, skb_tcp_flags):
8924 bpf_ctx_record_field_size(info, size_default);
8925 return bpf_ctx_narrow_access_ok(off, size,
8927 case offsetof(struct bpf_sock_ops, skb_hwtstamp):
8928 if (size != sizeof(__u64))
8932 if (size != size_default)
8941 static int sk_skb_prologue(struct bpf_insn *insn_buf, bool direct_write,
8942 const struct bpf_prog *prog)
8944 return bpf_unclone_prologue(insn_buf, direct_write, prog, SK_DROP);
8947 static bool sk_skb_is_valid_access(int off, int size,
8948 enum bpf_access_type type,
8949 const struct bpf_prog *prog,
8950 struct bpf_insn_access_aux *info)
8953 case bpf_ctx_range(struct __sk_buff, tc_classid):
8954 case bpf_ctx_range(struct __sk_buff, data_meta):
8955 case bpf_ctx_range(struct __sk_buff, tstamp):
8956 case bpf_ctx_range(struct __sk_buff, wire_len):
8957 case bpf_ctx_range(struct __sk_buff, hwtstamp):
8961 if (type == BPF_WRITE) {
8963 case bpf_ctx_range(struct __sk_buff, tc_index):
8964 case bpf_ctx_range(struct __sk_buff, priority):
8972 case bpf_ctx_range(struct __sk_buff, mark):
8974 case bpf_ctx_range(struct __sk_buff, data):
8975 info->reg_type = PTR_TO_PACKET;
8977 case bpf_ctx_range(struct __sk_buff, data_end):
8978 info->reg_type = PTR_TO_PACKET_END;
8982 return bpf_skb_is_valid_access(off, size, type, prog, info);
8985 static bool sk_msg_is_valid_access(int off, int size,
8986 enum bpf_access_type type,
8987 const struct bpf_prog *prog,
8988 struct bpf_insn_access_aux *info)
8990 if (type == BPF_WRITE)
8993 if (off % size != 0)
8997 case offsetof(struct sk_msg_md, data):
8998 info->reg_type = PTR_TO_PACKET;
8999 if (size != sizeof(__u64))
9002 case offsetof(struct sk_msg_md, data_end):
9003 info->reg_type = PTR_TO_PACKET_END;
9004 if (size != sizeof(__u64))
9007 case offsetof(struct sk_msg_md, sk):
9008 if (size != sizeof(__u64))
9010 info->reg_type = PTR_TO_SOCKET;
9012 case bpf_ctx_range(struct sk_msg_md, family):
9013 case bpf_ctx_range(struct sk_msg_md, remote_ip4):
9014 case bpf_ctx_range(struct sk_msg_md, local_ip4):
9015 case bpf_ctx_range_till(struct sk_msg_md, remote_ip6[0], remote_ip6[3]):
9016 case bpf_ctx_range_till(struct sk_msg_md, local_ip6[0], local_ip6[3]):
9017 case bpf_ctx_range(struct sk_msg_md, remote_port):
9018 case bpf_ctx_range(struct sk_msg_md, local_port):
9019 case bpf_ctx_range(struct sk_msg_md, size):
9020 if (size != sizeof(__u32))
9029 static bool flow_dissector_is_valid_access(int off, int size,
9030 enum bpf_access_type type,
9031 const struct bpf_prog *prog,
9032 struct bpf_insn_access_aux *info)
9034 const int size_default = sizeof(__u32);
9036 if (off < 0 || off >= sizeof(struct __sk_buff))
9039 if (type == BPF_WRITE)
9043 case bpf_ctx_range(struct __sk_buff, data):
9044 if (size != size_default)
9046 info->reg_type = PTR_TO_PACKET;
9048 case bpf_ctx_range(struct __sk_buff, data_end):
9049 if (size != size_default)
9051 info->reg_type = PTR_TO_PACKET_END;
9053 case bpf_ctx_range_ptr(struct __sk_buff, flow_keys):
9054 if (size != sizeof(__u64))
9056 info->reg_type = PTR_TO_FLOW_KEYS;
9063 static u32 flow_dissector_convert_ctx_access(enum bpf_access_type type,
9064 const struct bpf_insn *si,
9065 struct bpf_insn *insn_buf,
9066 struct bpf_prog *prog,
9070 struct bpf_insn *insn = insn_buf;
9073 case offsetof(struct __sk_buff, data):
9074 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_flow_dissector, data),
9075 si->dst_reg, si->src_reg,
9076 offsetof(struct bpf_flow_dissector, data));
9079 case offsetof(struct __sk_buff, data_end):
9080 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_flow_dissector, data_end),
9081 si->dst_reg, si->src_reg,
9082 offsetof(struct bpf_flow_dissector, data_end));
9085 case offsetof(struct __sk_buff, flow_keys):
9086 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_flow_dissector, flow_keys),
9087 si->dst_reg, si->src_reg,
9088 offsetof(struct bpf_flow_dissector, flow_keys));
9092 return insn - insn_buf;
9095 static struct bpf_insn *bpf_convert_tstamp_type_read(const struct bpf_insn *si,
9096 struct bpf_insn *insn)
9098 __u8 value_reg = si->dst_reg;
9099 __u8 skb_reg = si->src_reg;
9100 /* AX is needed because src_reg and dst_reg could be the same */
9101 __u8 tmp_reg = BPF_REG_AX;
9103 *insn++ = BPF_LDX_MEM(BPF_B, tmp_reg, skb_reg,
9104 PKT_VLAN_PRESENT_OFFSET);
9105 *insn++ = BPF_JMP32_IMM(BPF_JSET, tmp_reg,
9106 SKB_MONO_DELIVERY_TIME_MASK, 2);
9107 *insn++ = BPF_MOV32_IMM(value_reg, BPF_SKB_TSTAMP_UNSPEC);
9108 *insn++ = BPF_JMP_A(1);
9109 *insn++ = BPF_MOV32_IMM(value_reg, BPF_SKB_TSTAMP_DELIVERY_MONO);
9114 static struct bpf_insn *bpf_convert_shinfo_access(__u8 dst_reg, __u8 skb_reg,
9115 struct bpf_insn *insn)
9117 /* si->dst_reg = skb_shinfo(SKB); */
9118 #ifdef NET_SKBUFF_DATA_USES_OFFSET
9119 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, end),
9120 BPF_REG_AX, skb_reg,
9121 offsetof(struct sk_buff, end));
9122 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, head),
9124 offsetof(struct sk_buff, head));
9125 *insn++ = BPF_ALU64_REG(BPF_ADD, dst_reg, BPF_REG_AX);
9127 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, end),
9129 offsetof(struct sk_buff, end));
9135 static struct bpf_insn *bpf_convert_tstamp_read(const struct bpf_prog *prog,
9136 const struct bpf_insn *si,
9137 struct bpf_insn *insn)
9139 __u8 value_reg = si->dst_reg;
9140 __u8 skb_reg = si->src_reg;
9142 #ifdef CONFIG_NET_CLS_ACT
9143 /* If the tstamp_type is read,
9144 * the bpf prog is aware the tstamp could have delivery time.
9145 * Thus, read skb->tstamp as is if tstamp_type_access is true.
9147 if (!prog->tstamp_type_access) {
9148 /* AX is needed because src_reg and dst_reg could be the same */
9149 __u8 tmp_reg = BPF_REG_AX;
9151 *insn++ = BPF_LDX_MEM(BPF_B, tmp_reg, skb_reg, PKT_VLAN_PRESENT_OFFSET);
9152 *insn++ = BPF_ALU32_IMM(BPF_AND, tmp_reg,
9153 TC_AT_INGRESS_MASK | SKB_MONO_DELIVERY_TIME_MASK);
9154 *insn++ = BPF_JMP32_IMM(BPF_JNE, tmp_reg,
9155 TC_AT_INGRESS_MASK | SKB_MONO_DELIVERY_TIME_MASK, 2);
9156 /* skb->tc_at_ingress && skb->mono_delivery_time,
9157 * read 0 as the (rcv) timestamp.
9159 *insn++ = BPF_MOV64_IMM(value_reg, 0);
9160 *insn++ = BPF_JMP_A(1);
9164 *insn++ = BPF_LDX_MEM(BPF_DW, value_reg, skb_reg,
9165 offsetof(struct sk_buff, tstamp));
9169 static struct bpf_insn *bpf_convert_tstamp_write(const struct bpf_prog *prog,
9170 const struct bpf_insn *si,
9171 struct bpf_insn *insn)
9173 __u8 value_reg = si->src_reg;
9174 __u8 skb_reg = si->dst_reg;
9176 #ifdef CONFIG_NET_CLS_ACT
9177 /* If the tstamp_type is read,
9178 * the bpf prog is aware the tstamp could have delivery time.
9179 * Thus, write skb->tstamp as is if tstamp_type_access is true.
9180 * Otherwise, writing at ingress will have to clear the
9181 * mono_delivery_time bit also.
9183 if (!prog->tstamp_type_access) {
9184 __u8 tmp_reg = BPF_REG_AX;
9186 *insn++ = BPF_LDX_MEM(BPF_B, tmp_reg, skb_reg, PKT_VLAN_PRESENT_OFFSET);
9187 /* Writing __sk_buff->tstamp as ingress, goto <clear> */
9188 *insn++ = BPF_JMP32_IMM(BPF_JSET, tmp_reg, TC_AT_INGRESS_MASK, 1);
9190 *insn++ = BPF_JMP_A(2);
9191 /* <clear>: mono_delivery_time */
9192 *insn++ = BPF_ALU32_IMM(BPF_AND, tmp_reg, ~SKB_MONO_DELIVERY_TIME_MASK);
9193 *insn++ = BPF_STX_MEM(BPF_B, skb_reg, tmp_reg, PKT_VLAN_PRESENT_OFFSET);
9197 /* <store>: skb->tstamp = tstamp */
9198 *insn++ = BPF_STX_MEM(BPF_DW, skb_reg, value_reg,
9199 offsetof(struct sk_buff, tstamp));
9203 static u32 bpf_convert_ctx_access(enum bpf_access_type type,
9204 const struct bpf_insn *si,
9205 struct bpf_insn *insn_buf,
9206 struct bpf_prog *prog, u32 *target_size)
9208 struct bpf_insn *insn = insn_buf;
9212 case offsetof(struct __sk_buff, len):
9213 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
9214 bpf_target_off(struct sk_buff, len, 4,
9218 case offsetof(struct __sk_buff, protocol):
9219 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
9220 bpf_target_off(struct sk_buff, protocol, 2,
9224 case offsetof(struct __sk_buff, vlan_proto):
9225 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
9226 bpf_target_off(struct sk_buff, vlan_proto, 2,
9230 case offsetof(struct __sk_buff, priority):
9231 if (type == BPF_WRITE)
9232 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
9233 bpf_target_off(struct sk_buff, priority, 4,
9236 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
9237 bpf_target_off(struct sk_buff, priority, 4,
9241 case offsetof(struct __sk_buff, ingress_ifindex):
9242 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
9243 bpf_target_off(struct sk_buff, skb_iif, 4,
9247 case offsetof(struct __sk_buff, ifindex):
9248 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
9249 si->dst_reg, si->src_reg,
9250 offsetof(struct sk_buff, dev));
9251 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
9252 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
9253 bpf_target_off(struct net_device, ifindex, 4,
9257 case offsetof(struct __sk_buff, hash):
9258 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
9259 bpf_target_off(struct sk_buff, hash, 4,
9263 case offsetof(struct __sk_buff, mark):
9264 if (type == BPF_WRITE)
9265 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
9266 bpf_target_off(struct sk_buff, mark, 4,
9269 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
9270 bpf_target_off(struct sk_buff, mark, 4,
9274 case offsetof(struct __sk_buff, pkt_type):
9276 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->src_reg,
9278 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, PKT_TYPE_MAX);
9279 #ifdef __BIG_ENDIAN_BITFIELD
9280 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, 5);
9284 case offsetof(struct __sk_buff, queue_mapping):
9285 if (type == BPF_WRITE) {
9286 *insn++ = BPF_JMP_IMM(BPF_JGE, si->src_reg, NO_QUEUE_MAPPING, 1);
9287 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg, si->src_reg,
9288 bpf_target_off(struct sk_buff,
9292 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
9293 bpf_target_off(struct sk_buff,
9299 case offsetof(struct __sk_buff, vlan_present):
9300 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
9301 bpf_target_off(struct sk_buff,
9302 vlan_all, 4, target_size));
9303 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
9304 *insn++ = BPF_ALU32_IMM(BPF_MOV, si->dst_reg, 1);
9307 case offsetof(struct __sk_buff, vlan_tci):
9308 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
9309 bpf_target_off(struct sk_buff, vlan_tci, 2,
9313 case offsetof(struct __sk_buff, cb[0]) ...
9314 offsetofend(struct __sk_buff, cb[4]) - 1:
9315 BUILD_BUG_ON(sizeof_field(struct qdisc_skb_cb, data) < 20);
9316 BUILD_BUG_ON((offsetof(struct sk_buff, cb) +
9317 offsetof(struct qdisc_skb_cb, data)) %
9320 prog->cb_access = 1;
9322 off -= offsetof(struct __sk_buff, cb[0]);
9323 off += offsetof(struct sk_buff, cb);
9324 off += offsetof(struct qdisc_skb_cb, data);
9325 if (type == BPF_WRITE)
9326 *insn++ = BPF_STX_MEM(BPF_SIZE(si->code), si->dst_reg,
9329 *insn++ = BPF_LDX_MEM(BPF_SIZE(si->code), si->dst_reg,
9333 case offsetof(struct __sk_buff, tc_classid):
9334 BUILD_BUG_ON(sizeof_field(struct qdisc_skb_cb, tc_classid) != 2);
9337 off -= offsetof(struct __sk_buff, tc_classid);
9338 off += offsetof(struct sk_buff, cb);
9339 off += offsetof(struct qdisc_skb_cb, tc_classid);
9341 if (type == BPF_WRITE)
9342 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg,
9345 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg,
9349 case offsetof(struct __sk_buff, data):
9350 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
9351 si->dst_reg, si->src_reg,
9352 offsetof(struct sk_buff, data));
9355 case offsetof(struct __sk_buff, data_meta):
9357 off -= offsetof(struct __sk_buff, data_meta);
9358 off += offsetof(struct sk_buff, cb);
9359 off += offsetof(struct bpf_skb_data_end, data_meta);
9360 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
9364 case offsetof(struct __sk_buff, data_end):
9366 off -= offsetof(struct __sk_buff, data_end);
9367 off += offsetof(struct sk_buff, cb);
9368 off += offsetof(struct bpf_skb_data_end, data_end);
9369 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
9373 case offsetof(struct __sk_buff, tc_index):
9374 #ifdef CONFIG_NET_SCHED
9375 if (type == BPF_WRITE)
9376 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg, si->src_reg,
9377 bpf_target_off(struct sk_buff, tc_index, 2,
9380 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
9381 bpf_target_off(struct sk_buff, tc_index, 2,
9385 if (type == BPF_WRITE)
9386 *insn++ = BPF_MOV64_REG(si->dst_reg, si->dst_reg);
9388 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
9392 case offsetof(struct __sk_buff, napi_id):
9393 #if defined(CONFIG_NET_RX_BUSY_POLL)
9394 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
9395 bpf_target_off(struct sk_buff, napi_id, 4,
9397 *insn++ = BPF_JMP_IMM(BPF_JGE, si->dst_reg, MIN_NAPI_ID, 1);
9398 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
9401 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
9404 case offsetof(struct __sk_buff, family):
9405 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_family) != 2);
9407 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
9408 si->dst_reg, si->src_reg,
9409 offsetof(struct sk_buff, sk));
9410 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
9411 bpf_target_off(struct sock_common,
9415 case offsetof(struct __sk_buff, remote_ip4):
9416 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_daddr) != 4);
9418 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
9419 si->dst_reg, si->src_reg,
9420 offsetof(struct sk_buff, sk));
9421 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
9422 bpf_target_off(struct sock_common,
9426 case offsetof(struct __sk_buff, local_ip4):
9427 BUILD_BUG_ON(sizeof_field(struct sock_common,
9428 skc_rcv_saddr) != 4);
9430 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
9431 si->dst_reg, si->src_reg,
9432 offsetof(struct sk_buff, sk));
9433 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
9434 bpf_target_off(struct sock_common,
9438 case offsetof(struct __sk_buff, remote_ip6[0]) ...
9439 offsetof(struct __sk_buff, remote_ip6[3]):
9440 #if IS_ENABLED(CONFIG_IPV6)
9441 BUILD_BUG_ON(sizeof_field(struct sock_common,
9442 skc_v6_daddr.s6_addr32[0]) != 4);
9445 off -= offsetof(struct __sk_buff, remote_ip6[0]);
9447 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
9448 si->dst_reg, si->src_reg,
9449 offsetof(struct sk_buff, sk));
9450 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
9451 offsetof(struct sock_common,
9452 skc_v6_daddr.s6_addr32[0]) +
9455 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
9458 case offsetof(struct __sk_buff, local_ip6[0]) ...
9459 offsetof(struct __sk_buff, local_ip6[3]):
9460 #if IS_ENABLED(CONFIG_IPV6)
9461 BUILD_BUG_ON(sizeof_field(struct sock_common,
9462 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
9465 off -= offsetof(struct __sk_buff, local_ip6[0]);
9467 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
9468 si->dst_reg, si->src_reg,
9469 offsetof(struct sk_buff, sk));
9470 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
9471 offsetof(struct sock_common,
9472 skc_v6_rcv_saddr.s6_addr32[0]) +
9475 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
9479 case offsetof(struct __sk_buff, remote_port):
9480 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_dport) != 2);
9482 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
9483 si->dst_reg, si->src_reg,
9484 offsetof(struct sk_buff, sk));
9485 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
9486 bpf_target_off(struct sock_common,
9489 #ifndef __BIG_ENDIAN_BITFIELD
9490 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
9494 case offsetof(struct __sk_buff, local_port):
9495 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_num) != 2);
9497 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
9498 si->dst_reg, si->src_reg,
9499 offsetof(struct sk_buff, sk));
9500 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
9501 bpf_target_off(struct sock_common,
9502 skc_num, 2, target_size));
9505 case offsetof(struct __sk_buff, tstamp):
9506 BUILD_BUG_ON(sizeof_field(struct sk_buff, tstamp) != 8);
9508 if (type == BPF_WRITE)
9509 insn = bpf_convert_tstamp_write(prog, si, insn);
9511 insn = bpf_convert_tstamp_read(prog, si, insn);
9514 case offsetof(struct __sk_buff, tstamp_type):
9515 insn = bpf_convert_tstamp_type_read(si, insn);
9518 case offsetof(struct __sk_buff, gso_segs):
9519 insn = bpf_convert_shinfo_access(si->dst_reg, si->src_reg, insn);
9520 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct skb_shared_info, gso_segs),
9521 si->dst_reg, si->dst_reg,
9522 bpf_target_off(struct skb_shared_info,
9526 case offsetof(struct __sk_buff, gso_size):
9527 insn = bpf_convert_shinfo_access(si->dst_reg, si->src_reg, insn);
9528 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct skb_shared_info, gso_size),
9529 si->dst_reg, si->dst_reg,
9530 bpf_target_off(struct skb_shared_info,
9534 case offsetof(struct __sk_buff, wire_len):
9535 BUILD_BUG_ON(sizeof_field(struct qdisc_skb_cb, pkt_len) != 4);
9538 off -= offsetof(struct __sk_buff, wire_len);
9539 off += offsetof(struct sk_buff, cb);
9540 off += offsetof(struct qdisc_skb_cb, pkt_len);
9542 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, off);
9545 case offsetof(struct __sk_buff, sk):
9546 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
9547 si->dst_reg, si->src_reg,
9548 offsetof(struct sk_buff, sk));
9550 case offsetof(struct __sk_buff, hwtstamp):
9551 BUILD_BUG_ON(sizeof_field(struct skb_shared_hwtstamps, hwtstamp) != 8);
9552 BUILD_BUG_ON(offsetof(struct skb_shared_hwtstamps, hwtstamp) != 0);
9554 insn = bpf_convert_shinfo_access(si->dst_reg, si->src_reg, insn);
9555 *insn++ = BPF_LDX_MEM(BPF_DW,
9556 si->dst_reg, si->dst_reg,
9557 bpf_target_off(struct skb_shared_info,
9563 return insn - insn_buf;
9566 u32 bpf_sock_convert_ctx_access(enum bpf_access_type type,
9567 const struct bpf_insn *si,
9568 struct bpf_insn *insn_buf,
9569 struct bpf_prog *prog, u32 *target_size)
9571 struct bpf_insn *insn = insn_buf;
9575 case offsetof(struct bpf_sock, bound_dev_if):
9576 BUILD_BUG_ON(sizeof_field(struct sock, sk_bound_dev_if) != 4);
9578 if (type == BPF_WRITE)
9579 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
9580 offsetof(struct sock, sk_bound_dev_if));
9582 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
9583 offsetof(struct sock, sk_bound_dev_if));
9586 case offsetof(struct bpf_sock, mark):
9587 BUILD_BUG_ON(sizeof_field(struct sock, sk_mark) != 4);
9589 if (type == BPF_WRITE)
9590 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
9591 offsetof(struct sock, sk_mark));
9593 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
9594 offsetof(struct sock, sk_mark));
9597 case offsetof(struct bpf_sock, priority):
9598 BUILD_BUG_ON(sizeof_field(struct sock, sk_priority) != 4);
9600 if (type == BPF_WRITE)
9601 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
9602 offsetof(struct sock, sk_priority));
9604 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
9605 offsetof(struct sock, sk_priority));
9608 case offsetof(struct bpf_sock, family):
9609 *insn++ = BPF_LDX_MEM(
9610 BPF_FIELD_SIZEOF(struct sock_common, skc_family),
9611 si->dst_reg, si->src_reg,
9612 bpf_target_off(struct sock_common,
9614 sizeof_field(struct sock_common,
9619 case offsetof(struct bpf_sock, type):
9620 *insn++ = BPF_LDX_MEM(
9621 BPF_FIELD_SIZEOF(struct sock, sk_type),
9622 si->dst_reg, si->src_reg,
9623 bpf_target_off(struct sock, sk_type,
9624 sizeof_field(struct sock, sk_type),
9628 case offsetof(struct bpf_sock, protocol):
9629 *insn++ = BPF_LDX_MEM(
9630 BPF_FIELD_SIZEOF(struct sock, sk_protocol),
9631 si->dst_reg, si->src_reg,
9632 bpf_target_off(struct sock, sk_protocol,
9633 sizeof_field(struct sock, sk_protocol),
9637 case offsetof(struct bpf_sock, src_ip4):
9638 *insn++ = BPF_LDX_MEM(
9639 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
9640 bpf_target_off(struct sock_common, skc_rcv_saddr,
9641 sizeof_field(struct sock_common,
9646 case offsetof(struct bpf_sock, dst_ip4):
9647 *insn++ = BPF_LDX_MEM(
9648 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
9649 bpf_target_off(struct sock_common, skc_daddr,
9650 sizeof_field(struct sock_common,
9655 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
9656 #if IS_ENABLED(CONFIG_IPV6)
9658 off -= offsetof(struct bpf_sock, src_ip6[0]);
9659 *insn++ = BPF_LDX_MEM(
9660 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
9663 skc_v6_rcv_saddr.s6_addr32[0],
9664 sizeof_field(struct sock_common,
9665 skc_v6_rcv_saddr.s6_addr32[0]),
9666 target_size) + off);
9669 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
9673 case bpf_ctx_range_till(struct bpf_sock, dst_ip6[0], dst_ip6[3]):
9674 #if IS_ENABLED(CONFIG_IPV6)
9676 off -= offsetof(struct bpf_sock, dst_ip6[0]);
9677 *insn++ = BPF_LDX_MEM(
9678 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
9679 bpf_target_off(struct sock_common,
9680 skc_v6_daddr.s6_addr32[0],
9681 sizeof_field(struct sock_common,
9682 skc_v6_daddr.s6_addr32[0]),
9683 target_size) + off);
9685 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
9690 case offsetof(struct bpf_sock, src_port):
9691 *insn++ = BPF_LDX_MEM(
9692 BPF_FIELD_SIZEOF(struct sock_common, skc_num),
9693 si->dst_reg, si->src_reg,
9694 bpf_target_off(struct sock_common, skc_num,
9695 sizeof_field(struct sock_common,
9700 case offsetof(struct bpf_sock, dst_port):
9701 *insn++ = BPF_LDX_MEM(
9702 BPF_FIELD_SIZEOF(struct sock_common, skc_dport),
9703 si->dst_reg, si->src_reg,
9704 bpf_target_off(struct sock_common, skc_dport,
9705 sizeof_field(struct sock_common,
9710 case offsetof(struct bpf_sock, state):
9711 *insn++ = BPF_LDX_MEM(
9712 BPF_FIELD_SIZEOF(struct sock_common, skc_state),
9713 si->dst_reg, si->src_reg,
9714 bpf_target_off(struct sock_common, skc_state,
9715 sizeof_field(struct sock_common,
9719 case offsetof(struct bpf_sock, rx_queue_mapping):
9720 #ifdef CONFIG_SOCK_RX_QUEUE_MAPPING
9721 *insn++ = BPF_LDX_MEM(
9722 BPF_FIELD_SIZEOF(struct sock, sk_rx_queue_mapping),
9723 si->dst_reg, si->src_reg,
9724 bpf_target_off(struct sock, sk_rx_queue_mapping,
9725 sizeof_field(struct sock,
9726 sk_rx_queue_mapping),
9728 *insn++ = BPF_JMP_IMM(BPF_JNE, si->dst_reg, NO_QUEUE_MAPPING,
9730 *insn++ = BPF_MOV64_IMM(si->dst_reg, -1);
9732 *insn++ = BPF_MOV64_IMM(si->dst_reg, -1);
9738 return insn - insn_buf;
9741 static u32 tc_cls_act_convert_ctx_access(enum bpf_access_type type,
9742 const struct bpf_insn *si,
9743 struct bpf_insn *insn_buf,
9744 struct bpf_prog *prog, u32 *target_size)
9746 struct bpf_insn *insn = insn_buf;
9749 case offsetof(struct __sk_buff, ifindex):
9750 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
9751 si->dst_reg, si->src_reg,
9752 offsetof(struct sk_buff, dev));
9753 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
9754 bpf_target_off(struct net_device, ifindex, 4,
9758 return bpf_convert_ctx_access(type, si, insn_buf, prog,
9762 return insn - insn_buf;
9765 static u32 xdp_convert_ctx_access(enum bpf_access_type type,
9766 const struct bpf_insn *si,
9767 struct bpf_insn *insn_buf,
9768 struct bpf_prog *prog, u32 *target_size)
9770 struct bpf_insn *insn = insn_buf;
9773 case offsetof(struct xdp_md, data):
9774 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data),
9775 si->dst_reg, si->src_reg,
9776 offsetof(struct xdp_buff, data));
9778 case offsetof(struct xdp_md, data_meta):
9779 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_meta),
9780 si->dst_reg, si->src_reg,
9781 offsetof(struct xdp_buff, data_meta));
9783 case offsetof(struct xdp_md, data_end):
9784 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_end),
9785 si->dst_reg, si->src_reg,
9786 offsetof(struct xdp_buff, data_end));
9788 case offsetof(struct xdp_md, ingress_ifindex):
9789 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, rxq),
9790 si->dst_reg, si->src_reg,
9791 offsetof(struct xdp_buff, rxq));
9792 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_rxq_info, dev),
9793 si->dst_reg, si->dst_reg,
9794 offsetof(struct xdp_rxq_info, dev));
9795 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
9796 offsetof(struct net_device, ifindex));
9798 case offsetof(struct xdp_md, rx_queue_index):
9799 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, rxq),
9800 si->dst_reg, si->src_reg,
9801 offsetof(struct xdp_buff, rxq));
9802 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
9803 offsetof(struct xdp_rxq_info,
9806 case offsetof(struct xdp_md, egress_ifindex):
9807 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, txq),
9808 si->dst_reg, si->src_reg,
9809 offsetof(struct xdp_buff, txq));
9810 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_txq_info, dev),
9811 si->dst_reg, si->dst_reg,
9812 offsetof(struct xdp_txq_info, dev));
9813 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
9814 offsetof(struct net_device, ifindex));
9818 return insn - insn_buf;
9821 /* SOCK_ADDR_LOAD_NESTED_FIELD() loads Nested Field S.F.NF where S is type of
9822 * context Structure, F is Field in context structure that contains a pointer
9823 * to Nested Structure of type NS that has the field NF.
9825 * SIZE encodes the load size (BPF_B, BPF_H, etc). It's up to caller to make
9826 * sure that SIZE is not greater than actual size of S.F.NF.
9828 * If offset OFF is provided, the load happens from that offset relative to
9831 #define SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, SIZE, OFF) \
9833 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), si->dst_reg, \
9834 si->src_reg, offsetof(S, F)); \
9835 *insn++ = BPF_LDX_MEM( \
9836 SIZE, si->dst_reg, si->dst_reg, \
9837 bpf_target_off(NS, NF, sizeof_field(NS, NF), \
9842 #define SOCK_ADDR_LOAD_NESTED_FIELD(S, NS, F, NF) \
9843 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, \
9844 BPF_FIELD_SIZEOF(NS, NF), 0)
9846 /* SOCK_ADDR_STORE_NESTED_FIELD_OFF() has semantic similar to
9847 * SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF() but for store operation.
9849 * In addition it uses Temporary Field TF (member of struct S) as the 3rd
9850 * "register" since two registers available in convert_ctx_access are not
9851 * enough: we can't override neither SRC, since it contains value to store, nor
9852 * DST since it contains pointer to context that may be used by later
9853 * instructions. But we need a temporary place to save pointer to nested
9854 * structure whose field we want to store to.
9856 #define SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, SIZE, OFF, TF) \
9858 int tmp_reg = BPF_REG_9; \
9859 if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg) \
9861 if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg) \
9863 *insn++ = BPF_STX_MEM(BPF_DW, si->dst_reg, tmp_reg, \
9865 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), tmp_reg, \
9866 si->dst_reg, offsetof(S, F)); \
9867 *insn++ = BPF_STX_MEM(SIZE, tmp_reg, si->src_reg, \
9868 bpf_target_off(NS, NF, sizeof_field(NS, NF), \
9871 *insn++ = BPF_LDX_MEM(BPF_DW, tmp_reg, si->dst_reg, \
9875 #define SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, SIZE, OFF, \
9878 if (type == BPF_WRITE) { \
9879 SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, SIZE, \
9882 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF( \
9883 S, NS, F, NF, SIZE, OFF); \
9887 #define SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD(S, NS, F, NF, TF) \
9888 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF( \
9889 S, NS, F, NF, BPF_FIELD_SIZEOF(NS, NF), 0, TF)
9891 static u32 sock_addr_convert_ctx_access(enum bpf_access_type type,
9892 const struct bpf_insn *si,
9893 struct bpf_insn *insn_buf,
9894 struct bpf_prog *prog, u32 *target_size)
9896 int off, port_size = sizeof_field(struct sockaddr_in6, sin6_port);
9897 struct bpf_insn *insn = insn_buf;
9900 case offsetof(struct bpf_sock_addr, user_family):
9901 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
9902 struct sockaddr, uaddr, sa_family);
9905 case offsetof(struct bpf_sock_addr, user_ip4):
9906 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
9907 struct bpf_sock_addr_kern, struct sockaddr_in, uaddr,
9908 sin_addr, BPF_SIZE(si->code), 0, tmp_reg);
9911 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
9913 off -= offsetof(struct bpf_sock_addr, user_ip6[0]);
9914 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
9915 struct bpf_sock_addr_kern, struct sockaddr_in6, uaddr,
9916 sin6_addr.s6_addr32[0], BPF_SIZE(si->code), off,
9920 case offsetof(struct bpf_sock_addr, user_port):
9921 /* To get port we need to know sa_family first and then treat
9922 * sockaddr as either sockaddr_in or sockaddr_in6.
9923 * Though we can simplify since port field has same offset and
9924 * size in both structures.
9925 * Here we check this invariant and use just one of the
9926 * structures if it's true.
9928 BUILD_BUG_ON(offsetof(struct sockaddr_in, sin_port) !=
9929 offsetof(struct sockaddr_in6, sin6_port));
9930 BUILD_BUG_ON(sizeof_field(struct sockaddr_in, sin_port) !=
9931 sizeof_field(struct sockaddr_in6, sin6_port));
9932 /* Account for sin6_port being smaller than user_port. */
9933 port_size = min(port_size, BPF_LDST_BYTES(si));
9934 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
9935 struct bpf_sock_addr_kern, struct sockaddr_in6, uaddr,
9936 sin6_port, bytes_to_bpf_size(port_size), 0, tmp_reg);
9939 case offsetof(struct bpf_sock_addr, family):
9940 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
9941 struct sock, sk, sk_family);
9944 case offsetof(struct bpf_sock_addr, type):
9945 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
9946 struct sock, sk, sk_type);
9949 case offsetof(struct bpf_sock_addr, protocol):
9950 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
9951 struct sock, sk, sk_protocol);
9954 case offsetof(struct bpf_sock_addr, msg_src_ip4):
9955 /* Treat t_ctx as struct in_addr for msg_src_ip4. */
9956 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
9957 struct bpf_sock_addr_kern, struct in_addr, t_ctx,
9958 s_addr, BPF_SIZE(si->code), 0, tmp_reg);
9961 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
9964 off -= offsetof(struct bpf_sock_addr, msg_src_ip6[0]);
9965 /* Treat t_ctx as struct in6_addr for msg_src_ip6. */
9966 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
9967 struct bpf_sock_addr_kern, struct in6_addr, t_ctx,
9968 s6_addr32[0], BPF_SIZE(si->code), off, tmp_reg);
9970 case offsetof(struct bpf_sock_addr, sk):
9971 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_addr_kern, sk),
9972 si->dst_reg, si->src_reg,
9973 offsetof(struct bpf_sock_addr_kern, sk));
9977 return insn - insn_buf;
9980 static u32 sock_ops_convert_ctx_access(enum bpf_access_type type,
9981 const struct bpf_insn *si,
9982 struct bpf_insn *insn_buf,
9983 struct bpf_prog *prog,
9986 struct bpf_insn *insn = insn_buf;
9989 /* Helper macro for adding read access to tcp_sock or sock fields. */
9990 #define SOCK_OPS_GET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ) \
9992 int fullsock_reg = si->dst_reg, reg = BPF_REG_9, jmp = 2; \
9993 BUILD_BUG_ON(sizeof_field(OBJ, OBJ_FIELD) > \
9994 sizeof_field(struct bpf_sock_ops, BPF_FIELD)); \
9995 if (si->dst_reg == reg || si->src_reg == reg) \
9997 if (si->dst_reg == reg || si->src_reg == reg) \
9999 if (si->dst_reg == si->src_reg) { \
10000 *insn++ = BPF_STX_MEM(BPF_DW, si->src_reg, reg, \
10001 offsetof(struct bpf_sock_ops_kern, \
10003 fullsock_reg = reg; \
10006 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
10007 struct bpf_sock_ops_kern, \
10009 fullsock_reg, si->src_reg, \
10010 offsetof(struct bpf_sock_ops_kern, \
10012 *insn++ = BPF_JMP_IMM(BPF_JEQ, fullsock_reg, 0, jmp); \
10013 if (si->dst_reg == si->src_reg) \
10014 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->src_reg, \
10015 offsetof(struct bpf_sock_ops_kern, \
10017 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
10018 struct bpf_sock_ops_kern, sk),\
10019 si->dst_reg, si->src_reg, \
10020 offsetof(struct bpf_sock_ops_kern, sk));\
10021 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(OBJ, \
10023 si->dst_reg, si->dst_reg, \
10024 offsetof(OBJ, OBJ_FIELD)); \
10025 if (si->dst_reg == si->src_reg) { \
10026 *insn++ = BPF_JMP_A(1); \
10027 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->src_reg, \
10028 offsetof(struct bpf_sock_ops_kern, \
10033 #define SOCK_OPS_GET_SK() \
10035 int fullsock_reg = si->dst_reg, reg = BPF_REG_9, jmp = 1; \
10036 if (si->dst_reg == reg || si->src_reg == reg) \
10038 if (si->dst_reg == reg || si->src_reg == reg) \
10040 if (si->dst_reg == si->src_reg) { \
10041 *insn++ = BPF_STX_MEM(BPF_DW, si->src_reg, reg, \
10042 offsetof(struct bpf_sock_ops_kern, \
10044 fullsock_reg = reg; \
10047 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
10048 struct bpf_sock_ops_kern, \
10050 fullsock_reg, si->src_reg, \
10051 offsetof(struct bpf_sock_ops_kern, \
10053 *insn++ = BPF_JMP_IMM(BPF_JEQ, fullsock_reg, 0, jmp); \
10054 if (si->dst_reg == si->src_reg) \
10055 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->src_reg, \
10056 offsetof(struct bpf_sock_ops_kern, \
10058 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
10059 struct bpf_sock_ops_kern, sk),\
10060 si->dst_reg, si->src_reg, \
10061 offsetof(struct bpf_sock_ops_kern, sk));\
10062 if (si->dst_reg == si->src_reg) { \
10063 *insn++ = BPF_JMP_A(1); \
10064 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->src_reg, \
10065 offsetof(struct bpf_sock_ops_kern, \
10070 #define SOCK_OPS_GET_TCP_SOCK_FIELD(FIELD) \
10071 SOCK_OPS_GET_FIELD(FIELD, FIELD, struct tcp_sock)
10073 /* Helper macro for adding write access to tcp_sock or sock fields.
10074 * The macro is called with two registers, dst_reg which contains a pointer
10075 * to ctx (context) and src_reg which contains the value that should be
10076 * stored. However, we need an additional register since we cannot overwrite
10077 * dst_reg because it may be used later in the program.
10078 * Instead we "borrow" one of the other register. We first save its value
10079 * into a new (temp) field in bpf_sock_ops_kern, use it, and then restore
10080 * it at the end of the macro.
10082 #define SOCK_OPS_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ) \
10084 int reg = BPF_REG_9; \
10085 BUILD_BUG_ON(sizeof_field(OBJ, OBJ_FIELD) > \
10086 sizeof_field(struct bpf_sock_ops, BPF_FIELD)); \
10087 if (si->dst_reg == reg || si->src_reg == reg) \
10089 if (si->dst_reg == reg || si->src_reg == reg) \
10091 *insn++ = BPF_STX_MEM(BPF_DW, si->dst_reg, reg, \
10092 offsetof(struct bpf_sock_ops_kern, \
10094 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
10095 struct bpf_sock_ops_kern, \
10097 reg, si->dst_reg, \
10098 offsetof(struct bpf_sock_ops_kern, \
10100 *insn++ = BPF_JMP_IMM(BPF_JEQ, reg, 0, 2); \
10101 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
10102 struct bpf_sock_ops_kern, sk),\
10103 reg, si->dst_reg, \
10104 offsetof(struct bpf_sock_ops_kern, sk));\
10105 *insn++ = BPF_STX_MEM(BPF_FIELD_SIZEOF(OBJ, OBJ_FIELD), \
10106 reg, si->src_reg, \
10107 offsetof(OBJ, OBJ_FIELD)); \
10108 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->dst_reg, \
10109 offsetof(struct bpf_sock_ops_kern, \
10113 #define SOCK_OPS_GET_OR_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ, TYPE) \
10115 if (TYPE == BPF_WRITE) \
10116 SOCK_OPS_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ); \
10118 SOCK_OPS_GET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ); \
10121 if (insn > insn_buf)
10122 return insn - insn_buf;
10125 case offsetof(struct bpf_sock_ops, op):
10126 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_ops_kern,
10128 si->dst_reg, si->src_reg,
10129 offsetof(struct bpf_sock_ops_kern, op));
10132 case offsetof(struct bpf_sock_ops, replylong[0]) ...
10133 offsetof(struct bpf_sock_ops, replylong[3]):
10134 BUILD_BUG_ON(sizeof_field(struct bpf_sock_ops, reply) !=
10135 sizeof_field(struct bpf_sock_ops_kern, reply));
10136 BUILD_BUG_ON(sizeof_field(struct bpf_sock_ops, replylong) !=
10137 sizeof_field(struct bpf_sock_ops_kern, replylong));
10139 off -= offsetof(struct bpf_sock_ops, replylong[0]);
10140 off += offsetof(struct bpf_sock_ops_kern, replylong[0]);
10141 if (type == BPF_WRITE)
10142 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
10145 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
10149 case offsetof(struct bpf_sock_ops, family):
10150 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_family) != 2);
10152 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10153 struct bpf_sock_ops_kern, sk),
10154 si->dst_reg, si->src_reg,
10155 offsetof(struct bpf_sock_ops_kern, sk));
10156 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
10157 offsetof(struct sock_common, skc_family));
10160 case offsetof(struct bpf_sock_ops, remote_ip4):
10161 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_daddr) != 4);
10163 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10164 struct bpf_sock_ops_kern, sk),
10165 si->dst_reg, si->src_reg,
10166 offsetof(struct bpf_sock_ops_kern, sk));
10167 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
10168 offsetof(struct sock_common, skc_daddr));
10171 case offsetof(struct bpf_sock_ops, local_ip4):
10172 BUILD_BUG_ON(sizeof_field(struct sock_common,
10173 skc_rcv_saddr) != 4);
10175 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10176 struct bpf_sock_ops_kern, sk),
10177 si->dst_reg, si->src_reg,
10178 offsetof(struct bpf_sock_ops_kern, sk));
10179 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
10180 offsetof(struct sock_common,
10184 case offsetof(struct bpf_sock_ops, remote_ip6[0]) ...
10185 offsetof(struct bpf_sock_ops, remote_ip6[3]):
10186 #if IS_ENABLED(CONFIG_IPV6)
10187 BUILD_BUG_ON(sizeof_field(struct sock_common,
10188 skc_v6_daddr.s6_addr32[0]) != 4);
10191 off -= offsetof(struct bpf_sock_ops, remote_ip6[0]);
10192 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10193 struct bpf_sock_ops_kern, sk),
10194 si->dst_reg, si->src_reg,
10195 offsetof(struct bpf_sock_ops_kern, sk));
10196 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
10197 offsetof(struct sock_common,
10198 skc_v6_daddr.s6_addr32[0]) +
10201 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
10205 case offsetof(struct bpf_sock_ops, local_ip6[0]) ...
10206 offsetof(struct bpf_sock_ops, local_ip6[3]):
10207 #if IS_ENABLED(CONFIG_IPV6)
10208 BUILD_BUG_ON(sizeof_field(struct sock_common,
10209 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
10212 off -= offsetof(struct bpf_sock_ops, local_ip6[0]);
10213 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10214 struct bpf_sock_ops_kern, sk),
10215 si->dst_reg, si->src_reg,
10216 offsetof(struct bpf_sock_ops_kern, sk));
10217 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
10218 offsetof(struct sock_common,
10219 skc_v6_rcv_saddr.s6_addr32[0]) +
10222 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
10226 case offsetof(struct bpf_sock_ops, remote_port):
10227 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_dport) != 2);
10229 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10230 struct bpf_sock_ops_kern, sk),
10231 si->dst_reg, si->src_reg,
10232 offsetof(struct bpf_sock_ops_kern, sk));
10233 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
10234 offsetof(struct sock_common, skc_dport));
10235 #ifndef __BIG_ENDIAN_BITFIELD
10236 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
10240 case offsetof(struct bpf_sock_ops, local_port):
10241 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_num) != 2);
10243 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10244 struct bpf_sock_ops_kern, sk),
10245 si->dst_reg, si->src_reg,
10246 offsetof(struct bpf_sock_ops_kern, sk));
10247 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
10248 offsetof(struct sock_common, skc_num));
10251 case offsetof(struct bpf_sock_ops, is_fullsock):
10252 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10253 struct bpf_sock_ops_kern,
10255 si->dst_reg, si->src_reg,
10256 offsetof(struct bpf_sock_ops_kern,
10260 case offsetof(struct bpf_sock_ops, state):
10261 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_state) != 1);
10263 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10264 struct bpf_sock_ops_kern, sk),
10265 si->dst_reg, si->src_reg,
10266 offsetof(struct bpf_sock_ops_kern, sk));
10267 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->dst_reg,
10268 offsetof(struct sock_common, skc_state));
10271 case offsetof(struct bpf_sock_ops, rtt_min):
10272 BUILD_BUG_ON(sizeof_field(struct tcp_sock, rtt_min) !=
10273 sizeof(struct minmax));
10274 BUILD_BUG_ON(sizeof(struct minmax) <
10275 sizeof(struct minmax_sample));
10277 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10278 struct bpf_sock_ops_kern, sk),
10279 si->dst_reg, si->src_reg,
10280 offsetof(struct bpf_sock_ops_kern, sk));
10281 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
10282 offsetof(struct tcp_sock, rtt_min) +
10283 sizeof_field(struct minmax_sample, t));
10286 case offsetof(struct bpf_sock_ops, bpf_sock_ops_cb_flags):
10287 SOCK_OPS_GET_FIELD(bpf_sock_ops_cb_flags, bpf_sock_ops_cb_flags,
10291 case offsetof(struct bpf_sock_ops, sk_txhash):
10292 SOCK_OPS_GET_OR_SET_FIELD(sk_txhash, sk_txhash,
10293 struct sock, type);
10295 case offsetof(struct bpf_sock_ops, snd_cwnd):
10296 SOCK_OPS_GET_TCP_SOCK_FIELD(snd_cwnd);
10298 case offsetof(struct bpf_sock_ops, srtt_us):
10299 SOCK_OPS_GET_TCP_SOCK_FIELD(srtt_us);
10301 case offsetof(struct bpf_sock_ops, snd_ssthresh):
10302 SOCK_OPS_GET_TCP_SOCK_FIELD(snd_ssthresh);
10304 case offsetof(struct bpf_sock_ops, rcv_nxt):
10305 SOCK_OPS_GET_TCP_SOCK_FIELD(rcv_nxt);
10307 case offsetof(struct bpf_sock_ops, snd_nxt):
10308 SOCK_OPS_GET_TCP_SOCK_FIELD(snd_nxt);
10310 case offsetof(struct bpf_sock_ops, snd_una):
10311 SOCK_OPS_GET_TCP_SOCK_FIELD(snd_una);
10313 case offsetof(struct bpf_sock_ops, mss_cache):
10314 SOCK_OPS_GET_TCP_SOCK_FIELD(mss_cache);
10316 case offsetof(struct bpf_sock_ops, ecn_flags):
10317 SOCK_OPS_GET_TCP_SOCK_FIELD(ecn_flags);
10319 case offsetof(struct bpf_sock_ops, rate_delivered):
10320 SOCK_OPS_GET_TCP_SOCK_FIELD(rate_delivered);
10322 case offsetof(struct bpf_sock_ops, rate_interval_us):
10323 SOCK_OPS_GET_TCP_SOCK_FIELD(rate_interval_us);
10325 case offsetof(struct bpf_sock_ops, packets_out):
10326 SOCK_OPS_GET_TCP_SOCK_FIELD(packets_out);
10328 case offsetof(struct bpf_sock_ops, retrans_out):
10329 SOCK_OPS_GET_TCP_SOCK_FIELD(retrans_out);
10331 case offsetof(struct bpf_sock_ops, total_retrans):
10332 SOCK_OPS_GET_TCP_SOCK_FIELD(total_retrans);
10334 case offsetof(struct bpf_sock_ops, segs_in):
10335 SOCK_OPS_GET_TCP_SOCK_FIELD(segs_in);
10337 case offsetof(struct bpf_sock_ops, data_segs_in):
10338 SOCK_OPS_GET_TCP_SOCK_FIELD(data_segs_in);
10340 case offsetof(struct bpf_sock_ops, segs_out):
10341 SOCK_OPS_GET_TCP_SOCK_FIELD(segs_out);
10343 case offsetof(struct bpf_sock_ops, data_segs_out):
10344 SOCK_OPS_GET_TCP_SOCK_FIELD(data_segs_out);
10346 case offsetof(struct bpf_sock_ops, lost_out):
10347 SOCK_OPS_GET_TCP_SOCK_FIELD(lost_out);
10349 case offsetof(struct bpf_sock_ops, sacked_out):
10350 SOCK_OPS_GET_TCP_SOCK_FIELD(sacked_out);
10352 case offsetof(struct bpf_sock_ops, bytes_received):
10353 SOCK_OPS_GET_TCP_SOCK_FIELD(bytes_received);
10355 case offsetof(struct bpf_sock_ops, bytes_acked):
10356 SOCK_OPS_GET_TCP_SOCK_FIELD(bytes_acked);
10358 case offsetof(struct bpf_sock_ops, sk):
10361 case offsetof(struct bpf_sock_ops, skb_data_end):
10362 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_ops_kern,
10364 si->dst_reg, si->src_reg,
10365 offsetof(struct bpf_sock_ops_kern,
10368 case offsetof(struct bpf_sock_ops, skb_data):
10369 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_ops_kern,
10371 si->dst_reg, si->src_reg,
10372 offsetof(struct bpf_sock_ops_kern,
10374 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
10375 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
10376 si->dst_reg, si->dst_reg,
10377 offsetof(struct sk_buff, data));
10379 case offsetof(struct bpf_sock_ops, skb_len):
10380 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_ops_kern,
10382 si->dst_reg, si->src_reg,
10383 offsetof(struct bpf_sock_ops_kern,
10385 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
10386 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, len),
10387 si->dst_reg, si->dst_reg,
10388 offsetof(struct sk_buff, len));
10390 case offsetof(struct bpf_sock_ops, skb_tcp_flags):
10391 off = offsetof(struct sk_buff, cb);
10392 off += offsetof(struct tcp_skb_cb, tcp_flags);
10393 *target_size = sizeof_field(struct tcp_skb_cb, tcp_flags);
10394 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_ops_kern,
10396 si->dst_reg, si->src_reg,
10397 offsetof(struct bpf_sock_ops_kern,
10399 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
10400 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct tcp_skb_cb,
10402 si->dst_reg, si->dst_reg, off);
10404 case offsetof(struct bpf_sock_ops, skb_hwtstamp): {
10405 struct bpf_insn *jmp_on_null_skb;
10407 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_ops_kern,
10409 si->dst_reg, si->src_reg,
10410 offsetof(struct bpf_sock_ops_kern,
10412 /* Reserve one insn to test skb == NULL */
10413 jmp_on_null_skb = insn++;
10414 insn = bpf_convert_shinfo_access(si->dst_reg, si->dst_reg, insn);
10415 *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
10416 bpf_target_off(struct skb_shared_info,
10419 *jmp_on_null_skb = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0,
10420 insn - jmp_on_null_skb - 1);
10424 return insn - insn_buf;
10427 /* data_end = skb->data + skb_headlen() */
10428 static struct bpf_insn *bpf_convert_data_end_access(const struct bpf_insn *si,
10429 struct bpf_insn *insn)
10432 int temp_reg_off = offsetof(struct sk_buff, cb) +
10433 offsetof(struct sk_skb_cb, temp_reg);
10435 if (si->src_reg == si->dst_reg) {
10436 /* We need an extra register, choose and save a register. */
10438 if (si->src_reg == reg || si->dst_reg == reg)
10440 if (si->src_reg == reg || si->dst_reg == reg)
10442 *insn++ = BPF_STX_MEM(BPF_DW, si->src_reg, reg, temp_reg_off);
10447 /* reg = skb->data */
10448 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
10450 offsetof(struct sk_buff, data));
10451 /* AX = skb->len */
10452 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, len),
10453 BPF_REG_AX, si->src_reg,
10454 offsetof(struct sk_buff, len));
10455 /* reg = skb->data + skb->len */
10456 *insn++ = BPF_ALU64_REG(BPF_ADD, reg, BPF_REG_AX);
10457 /* AX = skb->data_len */
10458 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data_len),
10459 BPF_REG_AX, si->src_reg,
10460 offsetof(struct sk_buff, data_len));
10462 /* reg = skb->data + skb->len - skb->data_len */
10463 *insn++ = BPF_ALU64_REG(BPF_SUB, reg, BPF_REG_AX);
10465 if (si->src_reg == si->dst_reg) {
10466 /* Restore the saved register */
10467 *insn++ = BPF_MOV64_REG(BPF_REG_AX, si->src_reg);
10468 *insn++ = BPF_MOV64_REG(si->dst_reg, reg);
10469 *insn++ = BPF_LDX_MEM(BPF_DW, reg, BPF_REG_AX, temp_reg_off);
10475 static u32 sk_skb_convert_ctx_access(enum bpf_access_type type,
10476 const struct bpf_insn *si,
10477 struct bpf_insn *insn_buf,
10478 struct bpf_prog *prog, u32 *target_size)
10480 struct bpf_insn *insn = insn_buf;
10484 case offsetof(struct __sk_buff, data_end):
10485 insn = bpf_convert_data_end_access(si, insn);
10487 case offsetof(struct __sk_buff, cb[0]) ...
10488 offsetofend(struct __sk_buff, cb[4]) - 1:
10489 BUILD_BUG_ON(sizeof_field(struct sk_skb_cb, data) < 20);
10490 BUILD_BUG_ON((offsetof(struct sk_buff, cb) +
10491 offsetof(struct sk_skb_cb, data)) %
10494 prog->cb_access = 1;
10496 off -= offsetof(struct __sk_buff, cb[0]);
10497 off += offsetof(struct sk_buff, cb);
10498 off += offsetof(struct sk_skb_cb, data);
10499 if (type == BPF_WRITE)
10500 *insn++ = BPF_STX_MEM(BPF_SIZE(si->code), si->dst_reg,
10503 *insn++ = BPF_LDX_MEM(BPF_SIZE(si->code), si->dst_reg,
10509 return bpf_convert_ctx_access(type, si, insn_buf, prog,
10513 return insn - insn_buf;
10516 static u32 sk_msg_convert_ctx_access(enum bpf_access_type type,
10517 const struct bpf_insn *si,
10518 struct bpf_insn *insn_buf,
10519 struct bpf_prog *prog, u32 *target_size)
10521 struct bpf_insn *insn = insn_buf;
10522 #if IS_ENABLED(CONFIG_IPV6)
10526 /* convert ctx uses the fact sg element is first in struct */
10527 BUILD_BUG_ON(offsetof(struct sk_msg, sg) != 0);
10530 case offsetof(struct sk_msg_md, data):
10531 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg, data),
10532 si->dst_reg, si->src_reg,
10533 offsetof(struct sk_msg, data));
10535 case offsetof(struct sk_msg_md, data_end):
10536 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg, data_end),
10537 si->dst_reg, si->src_reg,
10538 offsetof(struct sk_msg, data_end));
10540 case offsetof(struct sk_msg_md, family):
10541 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_family) != 2);
10543 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10544 struct sk_msg, sk),
10545 si->dst_reg, si->src_reg,
10546 offsetof(struct sk_msg, sk));
10547 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
10548 offsetof(struct sock_common, skc_family));
10551 case offsetof(struct sk_msg_md, remote_ip4):
10552 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_daddr) != 4);
10554 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10555 struct sk_msg, sk),
10556 si->dst_reg, si->src_reg,
10557 offsetof(struct sk_msg, sk));
10558 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
10559 offsetof(struct sock_common, skc_daddr));
10562 case offsetof(struct sk_msg_md, local_ip4):
10563 BUILD_BUG_ON(sizeof_field(struct sock_common,
10564 skc_rcv_saddr) != 4);
10566 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10567 struct sk_msg, sk),
10568 si->dst_reg, si->src_reg,
10569 offsetof(struct sk_msg, sk));
10570 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
10571 offsetof(struct sock_common,
10575 case offsetof(struct sk_msg_md, remote_ip6[0]) ...
10576 offsetof(struct sk_msg_md, remote_ip6[3]):
10577 #if IS_ENABLED(CONFIG_IPV6)
10578 BUILD_BUG_ON(sizeof_field(struct sock_common,
10579 skc_v6_daddr.s6_addr32[0]) != 4);
10582 off -= offsetof(struct sk_msg_md, remote_ip6[0]);
10583 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10584 struct sk_msg, sk),
10585 si->dst_reg, si->src_reg,
10586 offsetof(struct sk_msg, sk));
10587 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
10588 offsetof(struct sock_common,
10589 skc_v6_daddr.s6_addr32[0]) +
10592 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
10596 case offsetof(struct sk_msg_md, local_ip6[0]) ...
10597 offsetof(struct sk_msg_md, local_ip6[3]):
10598 #if IS_ENABLED(CONFIG_IPV6)
10599 BUILD_BUG_ON(sizeof_field(struct sock_common,
10600 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
10603 off -= offsetof(struct sk_msg_md, local_ip6[0]);
10604 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10605 struct sk_msg, sk),
10606 si->dst_reg, si->src_reg,
10607 offsetof(struct sk_msg, sk));
10608 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
10609 offsetof(struct sock_common,
10610 skc_v6_rcv_saddr.s6_addr32[0]) +
10613 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
10617 case offsetof(struct sk_msg_md, remote_port):
10618 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_dport) != 2);
10620 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10621 struct sk_msg, sk),
10622 si->dst_reg, si->src_reg,
10623 offsetof(struct sk_msg, sk));
10624 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
10625 offsetof(struct sock_common, skc_dport));
10626 #ifndef __BIG_ENDIAN_BITFIELD
10627 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
10631 case offsetof(struct sk_msg_md, local_port):
10632 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_num) != 2);
10634 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10635 struct sk_msg, sk),
10636 si->dst_reg, si->src_reg,
10637 offsetof(struct sk_msg, sk));
10638 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
10639 offsetof(struct sock_common, skc_num));
10642 case offsetof(struct sk_msg_md, size):
10643 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg_sg, size),
10644 si->dst_reg, si->src_reg,
10645 offsetof(struct sk_msg_sg, size));
10648 case offsetof(struct sk_msg_md, sk):
10649 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg, sk),
10650 si->dst_reg, si->src_reg,
10651 offsetof(struct sk_msg, sk));
10655 return insn - insn_buf;
10658 const struct bpf_verifier_ops sk_filter_verifier_ops = {
10659 .get_func_proto = sk_filter_func_proto,
10660 .is_valid_access = sk_filter_is_valid_access,
10661 .convert_ctx_access = bpf_convert_ctx_access,
10662 .gen_ld_abs = bpf_gen_ld_abs,
10665 const struct bpf_prog_ops sk_filter_prog_ops = {
10666 .test_run = bpf_prog_test_run_skb,
10669 const struct bpf_verifier_ops tc_cls_act_verifier_ops = {
10670 .get_func_proto = tc_cls_act_func_proto,
10671 .is_valid_access = tc_cls_act_is_valid_access,
10672 .convert_ctx_access = tc_cls_act_convert_ctx_access,
10673 .gen_prologue = tc_cls_act_prologue,
10674 .gen_ld_abs = bpf_gen_ld_abs,
10675 .btf_struct_access = tc_cls_act_btf_struct_access,
10678 const struct bpf_prog_ops tc_cls_act_prog_ops = {
10679 .test_run = bpf_prog_test_run_skb,
10682 const struct bpf_verifier_ops xdp_verifier_ops = {
10683 .get_func_proto = xdp_func_proto,
10684 .is_valid_access = xdp_is_valid_access,
10685 .convert_ctx_access = xdp_convert_ctx_access,
10686 .gen_prologue = bpf_noop_prologue,
10687 .btf_struct_access = xdp_btf_struct_access,
10690 const struct bpf_prog_ops xdp_prog_ops = {
10691 .test_run = bpf_prog_test_run_xdp,
10694 const struct bpf_verifier_ops cg_skb_verifier_ops = {
10695 .get_func_proto = cg_skb_func_proto,
10696 .is_valid_access = cg_skb_is_valid_access,
10697 .convert_ctx_access = bpf_convert_ctx_access,
10700 const struct bpf_prog_ops cg_skb_prog_ops = {
10701 .test_run = bpf_prog_test_run_skb,
10704 const struct bpf_verifier_ops lwt_in_verifier_ops = {
10705 .get_func_proto = lwt_in_func_proto,
10706 .is_valid_access = lwt_is_valid_access,
10707 .convert_ctx_access = bpf_convert_ctx_access,
10710 const struct bpf_prog_ops lwt_in_prog_ops = {
10711 .test_run = bpf_prog_test_run_skb,
10714 const struct bpf_verifier_ops lwt_out_verifier_ops = {
10715 .get_func_proto = lwt_out_func_proto,
10716 .is_valid_access = lwt_is_valid_access,
10717 .convert_ctx_access = bpf_convert_ctx_access,
10720 const struct bpf_prog_ops lwt_out_prog_ops = {
10721 .test_run = bpf_prog_test_run_skb,
10724 const struct bpf_verifier_ops lwt_xmit_verifier_ops = {
10725 .get_func_proto = lwt_xmit_func_proto,
10726 .is_valid_access = lwt_is_valid_access,
10727 .convert_ctx_access = bpf_convert_ctx_access,
10728 .gen_prologue = tc_cls_act_prologue,
10731 const struct bpf_prog_ops lwt_xmit_prog_ops = {
10732 .test_run = bpf_prog_test_run_skb,
10735 const struct bpf_verifier_ops lwt_seg6local_verifier_ops = {
10736 .get_func_proto = lwt_seg6local_func_proto,
10737 .is_valid_access = lwt_is_valid_access,
10738 .convert_ctx_access = bpf_convert_ctx_access,
10741 const struct bpf_prog_ops lwt_seg6local_prog_ops = {
10742 .test_run = bpf_prog_test_run_skb,
10745 const struct bpf_verifier_ops cg_sock_verifier_ops = {
10746 .get_func_proto = sock_filter_func_proto,
10747 .is_valid_access = sock_filter_is_valid_access,
10748 .convert_ctx_access = bpf_sock_convert_ctx_access,
10751 const struct bpf_prog_ops cg_sock_prog_ops = {
10754 const struct bpf_verifier_ops cg_sock_addr_verifier_ops = {
10755 .get_func_proto = sock_addr_func_proto,
10756 .is_valid_access = sock_addr_is_valid_access,
10757 .convert_ctx_access = sock_addr_convert_ctx_access,
10760 const struct bpf_prog_ops cg_sock_addr_prog_ops = {
10763 const struct bpf_verifier_ops sock_ops_verifier_ops = {
10764 .get_func_proto = sock_ops_func_proto,
10765 .is_valid_access = sock_ops_is_valid_access,
10766 .convert_ctx_access = sock_ops_convert_ctx_access,
10769 const struct bpf_prog_ops sock_ops_prog_ops = {
10772 const struct bpf_verifier_ops sk_skb_verifier_ops = {
10773 .get_func_proto = sk_skb_func_proto,
10774 .is_valid_access = sk_skb_is_valid_access,
10775 .convert_ctx_access = sk_skb_convert_ctx_access,
10776 .gen_prologue = sk_skb_prologue,
10779 const struct bpf_prog_ops sk_skb_prog_ops = {
10782 const struct bpf_verifier_ops sk_msg_verifier_ops = {
10783 .get_func_proto = sk_msg_func_proto,
10784 .is_valid_access = sk_msg_is_valid_access,
10785 .convert_ctx_access = sk_msg_convert_ctx_access,
10786 .gen_prologue = bpf_noop_prologue,
10789 const struct bpf_prog_ops sk_msg_prog_ops = {
10792 const struct bpf_verifier_ops flow_dissector_verifier_ops = {
10793 .get_func_proto = flow_dissector_func_proto,
10794 .is_valid_access = flow_dissector_is_valid_access,
10795 .convert_ctx_access = flow_dissector_convert_ctx_access,
10798 const struct bpf_prog_ops flow_dissector_prog_ops = {
10799 .test_run = bpf_prog_test_run_flow_dissector,
10802 int sk_detach_filter(struct sock *sk)
10805 struct sk_filter *filter;
10807 if (sock_flag(sk, SOCK_FILTER_LOCKED))
10810 filter = rcu_dereference_protected(sk->sk_filter,
10811 lockdep_sock_is_held(sk));
10813 RCU_INIT_POINTER(sk->sk_filter, NULL);
10814 sk_filter_uncharge(sk, filter);
10820 EXPORT_SYMBOL_GPL(sk_detach_filter);
10822 int sk_get_filter(struct sock *sk, sockptr_t optval, unsigned int len)
10824 struct sock_fprog_kern *fprog;
10825 struct sk_filter *filter;
10828 sockopt_lock_sock(sk);
10829 filter = rcu_dereference_protected(sk->sk_filter,
10830 lockdep_sock_is_held(sk));
10834 /* We're copying the filter that has been originally attached,
10835 * so no conversion/decode needed anymore. eBPF programs that
10836 * have no original program cannot be dumped through this.
10839 fprog = filter->prog->orig_prog;
10845 /* User space only enquires number of filter blocks. */
10849 if (len < fprog->len)
10853 if (copy_to_sockptr(optval, fprog->filter, bpf_classic_proglen(fprog)))
10856 /* Instead of bytes, the API requests to return the number
10857 * of filter blocks.
10861 sockopt_release_sock(sk);
10866 static void bpf_init_reuseport_kern(struct sk_reuseport_kern *reuse_kern,
10867 struct sock_reuseport *reuse,
10868 struct sock *sk, struct sk_buff *skb,
10869 struct sock *migrating_sk,
10872 reuse_kern->skb = skb;
10873 reuse_kern->sk = sk;
10874 reuse_kern->selected_sk = NULL;
10875 reuse_kern->migrating_sk = migrating_sk;
10876 reuse_kern->data_end = skb->data + skb_headlen(skb);
10877 reuse_kern->hash = hash;
10878 reuse_kern->reuseport_id = reuse->reuseport_id;
10879 reuse_kern->bind_inany = reuse->bind_inany;
10882 struct sock *bpf_run_sk_reuseport(struct sock_reuseport *reuse, struct sock *sk,
10883 struct bpf_prog *prog, struct sk_buff *skb,
10884 struct sock *migrating_sk,
10887 struct sk_reuseport_kern reuse_kern;
10888 enum sk_action action;
10890 bpf_init_reuseport_kern(&reuse_kern, reuse, sk, skb, migrating_sk, hash);
10891 action = bpf_prog_run(prog, &reuse_kern);
10893 if (action == SK_PASS)
10894 return reuse_kern.selected_sk;
10896 return ERR_PTR(-ECONNREFUSED);
10899 BPF_CALL_4(sk_select_reuseport, struct sk_reuseport_kern *, reuse_kern,
10900 struct bpf_map *, map, void *, key, u32, flags)
10902 bool is_sockarray = map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY;
10903 struct sock_reuseport *reuse;
10904 struct sock *selected_sk;
10906 selected_sk = map->ops->map_lookup_elem(map, key);
10910 reuse = rcu_dereference(selected_sk->sk_reuseport_cb);
10912 /* Lookup in sock_map can return TCP ESTABLISHED sockets. */
10913 if (sk_is_refcounted(selected_sk))
10914 sock_put(selected_sk);
10916 /* reuseport_array has only sk with non NULL sk_reuseport_cb.
10917 * The only (!reuse) case here is - the sk has already been
10918 * unhashed (e.g. by close()), so treat it as -ENOENT.
10920 * Other maps (e.g. sock_map) do not provide this guarantee and
10921 * the sk may never be in the reuseport group to begin with.
10923 return is_sockarray ? -ENOENT : -EINVAL;
10926 if (unlikely(reuse->reuseport_id != reuse_kern->reuseport_id)) {
10927 struct sock *sk = reuse_kern->sk;
10929 if (sk->sk_protocol != selected_sk->sk_protocol)
10930 return -EPROTOTYPE;
10931 else if (sk->sk_family != selected_sk->sk_family)
10932 return -EAFNOSUPPORT;
10934 /* Catch all. Likely bound to a different sockaddr. */
10938 reuse_kern->selected_sk = selected_sk;
10943 static const struct bpf_func_proto sk_select_reuseport_proto = {
10944 .func = sk_select_reuseport,
10946 .ret_type = RET_INTEGER,
10947 .arg1_type = ARG_PTR_TO_CTX,
10948 .arg2_type = ARG_CONST_MAP_PTR,
10949 .arg3_type = ARG_PTR_TO_MAP_KEY,
10950 .arg4_type = ARG_ANYTHING,
10953 BPF_CALL_4(sk_reuseport_load_bytes,
10954 const struct sk_reuseport_kern *, reuse_kern, u32, offset,
10955 void *, to, u32, len)
10957 return ____bpf_skb_load_bytes(reuse_kern->skb, offset, to, len);
10960 static const struct bpf_func_proto sk_reuseport_load_bytes_proto = {
10961 .func = sk_reuseport_load_bytes,
10963 .ret_type = RET_INTEGER,
10964 .arg1_type = ARG_PTR_TO_CTX,
10965 .arg2_type = ARG_ANYTHING,
10966 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
10967 .arg4_type = ARG_CONST_SIZE,
10970 BPF_CALL_5(sk_reuseport_load_bytes_relative,
10971 const struct sk_reuseport_kern *, reuse_kern, u32, offset,
10972 void *, to, u32, len, u32, start_header)
10974 return ____bpf_skb_load_bytes_relative(reuse_kern->skb, offset, to,
10975 len, start_header);
10978 static const struct bpf_func_proto sk_reuseport_load_bytes_relative_proto = {
10979 .func = sk_reuseport_load_bytes_relative,
10981 .ret_type = RET_INTEGER,
10982 .arg1_type = ARG_PTR_TO_CTX,
10983 .arg2_type = ARG_ANYTHING,
10984 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
10985 .arg4_type = ARG_CONST_SIZE,
10986 .arg5_type = ARG_ANYTHING,
10989 static const struct bpf_func_proto *
10990 sk_reuseport_func_proto(enum bpf_func_id func_id,
10991 const struct bpf_prog *prog)
10994 case BPF_FUNC_sk_select_reuseport:
10995 return &sk_select_reuseport_proto;
10996 case BPF_FUNC_skb_load_bytes:
10997 return &sk_reuseport_load_bytes_proto;
10998 case BPF_FUNC_skb_load_bytes_relative:
10999 return &sk_reuseport_load_bytes_relative_proto;
11000 case BPF_FUNC_get_socket_cookie:
11001 return &bpf_get_socket_ptr_cookie_proto;
11002 case BPF_FUNC_ktime_get_coarse_ns:
11003 return &bpf_ktime_get_coarse_ns_proto;
11005 return bpf_base_func_proto(func_id);
11010 sk_reuseport_is_valid_access(int off, int size,
11011 enum bpf_access_type type,
11012 const struct bpf_prog *prog,
11013 struct bpf_insn_access_aux *info)
11015 const u32 size_default = sizeof(__u32);
11017 if (off < 0 || off >= sizeof(struct sk_reuseport_md) ||
11018 off % size || type != BPF_READ)
11022 case offsetof(struct sk_reuseport_md, data):
11023 info->reg_type = PTR_TO_PACKET;
11024 return size == sizeof(__u64);
11026 case offsetof(struct sk_reuseport_md, data_end):
11027 info->reg_type = PTR_TO_PACKET_END;
11028 return size == sizeof(__u64);
11030 case offsetof(struct sk_reuseport_md, hash):
11031 return size == size_default;
11033 case offsetof(struct sk_reuseport_md, sk):
11034 info->reg_type = PTR_TO_SOCKET;
11035 return size == sizeof(__u64);
11037 case offsetof(struct sk_reuseport_md, migrating_sk):
11038 info->reg_type = PTR_TO_SOCK_COMMON_OR_NULL;
11039 return size == sizeof(__u64);
11041 /* Fields that allow narrowing */
11042 case bpf_ctx_range(struct sk_reuseport_md, eth_protocol):
11043 if (size < sizeof_field(struct sk_buff, protocol))
11046 case bpf_ctx_range(struct sk_reuseport_md, ip_protocol):
11047 case bpf_ctx_range(struct sk_reuseport_md, bind_inany):
11048 case bpf_ctx_range(struct sk_reuseport_md, len):
11049 bpf_ctx_record_field_size(info, size_default);
11050 return bpf_ctx_narrow_access_ok(off, size, size_default);
11057 #define SK_REUSEPORT_LOAD_FIELD(F) ({ \
11058 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_reuseport_kern, F), \
11059 si->dst_reg, si->src_reg, \
11060 bpf_target_off(struct sk_reuseport_kern, F, \
11061 sizeof_field(struct sk_reuseport_kern, F), \
11065 #define SK_REUSEPORT_LOAD_SKB_FIELD(SKB_FIELD) \
11066 SOCK_ADDR_LOAD_NESTED_FIELD(struct sk_reuseport_kern, \
11071 #define SK_REUSEPORT_LOAD_SK_FIELD(SK_FIELD) \
11072 SOCK_ADDR_LOAD_NESTED_FIELD(struct sk_reuseport_kern, \
11077 static u32 sk_reuseport_convert_ctx_access(enum bpf_access_type type,
11078 const struct bpf_insn *si,
11079 struct bpf_insn *insn_buf,
11080 struct bpf_prog *prog,
11083 struct bpf_insn *insn = insn_buf;
11086 case offsetof(struct sk_reuseport_md, data):
11087 SK_REUSEPORT_LOAD_SKB_FIELD(data);
11090 case offsetof(struct sk_reuseport_md, len):
11091 SK_REUSEPORT_LOAD_SKB_FIELD(len);
11094 case offsetof(struct sk_reuseport_md, eth_protocol):
11095 SK_REUSEPORT_LOAD_SKB_FIELD(protocol);
11098 case offsetof(struct sk_reuseport_md, ip_protocol):
11099 SK_REUSEPORT_LOAD_SK_FIELD(sk_protocol);
11102 case offsetof(struct sk_reuseport_md, data_end):
11103 SK_REUSEPORT_LOAD_FIELD(data_end);
11106 case offsetof(struct sk_reuseport_md, hash):
11107 SK_REUSEPORT_LOAD_FIELD(hash);
11110 case offsetof(struct sk_reuseport_md, bind_inany):
11111 SK_REUSEPORT_LOAD_FIELD(bind_inany);
11114 case offsetof(struct sk_reuseport_md, sk):
11115 SK_REUSEPORT_LOAD_FIELD(sk);
11118 case offsetof(struct sk_reuseport_md, migrating_sk):
11119 SK_REUSEPORT_LOAD_FIELD(migrating_sk);
11123 return insn - insn_buf;
11126 const struct bpf_verifier_ops sk_reuseport_verifier_ops = {
11127 .get_func_proto = sk_reuseport_func_proto,
11128 .is_valid_access = sk_reuseport_is_valid_access,
11129 .convert_ctx_access = sk_reuseport_convert_ctx_access,
11132 const struct bpf_prog_ops sk_reuseport_prog_ops = {
11135 DEFINE_STATIC_KEY_FALSE(bpf_sk_lookup_enabled);
11136 EXPORT_SYMBOL(bpf_sk_lookup_enabled);
11138 BPF_CALL_3(bpf_sk_lookup_assign, struct bpf_sk_lookup_kern *, ctx,
11139 struct sock *, sk, u64, flags)
11141 if (unlikely(flags & ~(BPF_SK_LOOKUP_F_REPLACE |
11142 BPF_SK_LOOKUP_F_NO_REUSEPORT)))
11144 if (unlikely(sk && sk_is_refcounted(sk)))
11145 return -ESOCKTNOSUPPORT; /* reject non-RCU freed sockets */
11146 if (unlikely(sk && sk_is_tcp(sk) && sk->sk_state != TCP_LISTEN))
11147 return -ESOCKTNOSUPPORT; /* only accept TCP socket in LISTEN */
11148 if (unlikely(sk && sk_is_udp(sk) && sk->sk_state != TCP_CLOSE))
11149 return -ESOCKTNOSUPPORT; /* only accept UDP socket in CLOSE */
11151 /* Check if socket is suitable for packet L3/L4 protocol */
11152 if (sk && sk->sk_protocol != ctx->protocol)
11153 return -EPROTOTYPE;
11154 if (sk && sk->sk_family != ctx->family &&
11155 (sk->sk_family == AF_INET || ipv6_only_sock(sk)))
11156 return -EAFNOSUPPORT;
11158 if (ctx->selected_sk && !(flags & BPF_SK_LOOKUP_F_REPLACE))
11161 /* Select socket as lookup result */
11162 ctx->selected_sk = sk;
11163 ctx->no_reuseport = flags & BPF_SK_LOOKUP_F_NO_REUSEPORT;
11167 static const struct bpf_func_proto bpf_sk_lookup_assign_proto = {
11168 .func = bpf_sk_lookup_assign,
11170 .ret_type = RET_INTEGER,
11171 .arg1_type = ARG_PTR_TO_CTX,
11172 .arg2_type = ARG_PTR_TO_SOCKET_OR_NULL,
11173 .arg3_type = ARG_ANYTHING,
11176 static const struct bpf_func_proto *
11177 sk_lookup_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
11180 case BPF_FUNC_perf_event_output:
11181 return &bpf_event_output_data_proto;
11182 case BPF_FUNC_sk_assign:
11183 return &bpf_sk_lookup_assign_proto;
11184 case BPF_FUNC_sk_release:
11185 return &bpf_sk_release_proto;
11187 return bpf_sk_base_func_proto(func_id);
11191 static bool sk_lookup_is_valid_access(int off, int size,
11192 enum bpf_access_type type,
11193 const struct bpf_prog *prog,
11194 struct bpf_insn_access_aux *info)
11196 if (off < 0 || off >= sizeof(struct bpf_sk_lookup))
11198 if (off % size != 0)
11200 if (type != BPF_READ)
11204 case offsetof(struct bpf_sk_lookup, sk):
11205 info->reg_type = PTR_TO_SOCKET_OR_NULL;
11206 return size == sizeof(__u64);
11208 case bpf_ctx_range(struct bpf_sk_lookup, family):
11209 case bpf_ctx_range(struct bpf_sk_lookup, protocol):
11210 case bpf_ctx_range(struct bpf_sk_lookup, remote_ip4):
11211 case bpf_ctx_range(struct bpf_sk_lookup, local_ip4):
11212 case bpf_ctx_range_till(struct bpf_sk_lookup, remote_ip6[0], remote_ip6[3]):
11213 case bpf_ctx_range_till(struct bpf_sk_lookup, local_ip6[0], local_ip6[3]):
11214 case bpf_ctx_range(struct bpf_sk_lookup, local_port):
11215 case bpf_ctx_range(struct bpf_sk_lookup, ingress_ifindex):
11216 bpf_ctx_record_field_size(info, sizeof(__u32));
11217 return bpf_ctx_narrow_access_ok(off, size, sizeof(__u32));
11219 case bpf_ctx_range(struct bpf_sk_lookup, remote_port):
11220 /* Allow 4-byte access to 2-byte field for backward compatibility */
11221 if (size == sizeof(__u32))
11223 bpf_ctx_record_field_size(info, sizeof(__be16));
11224 return bpf_ctx_narrow_access_ok(off, size, sizeof(__be16));
11226 case offsetofend(struct bpf_sk_lookup, remote_port) ...
11227 offsetof(struct bpf_sk_lookup, local_ip4) - 1:
11228 /* Allow access to zero padding for backward compatibility */
11229 bpf_ctx_record_field_size(info, sizeof(__u16));
11230 return bpf_ctx_narrow_access_ok(off, size, sizeof(__u16));
11237 static u32 sk_lookup_convert_ctx_access(enum bpf_access_type type,
11238 const struct bpf_insn *si,
11239 struct bpf_insn *insn_buf,
11240 struct bpf_prog *prog,
11243 struct bpf_insn *insn = insn_buf;
11246 case offsetof(struct bpf_sk_lookup, sk):
11247 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg, si->src_reg,
11248 offsetof(struct bpf_sk_lookup_kern, selected_sk));
11251 case offsetof(struct bpf_sk_lookup, family):
11252 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
11253 bpf_target_off(struct bpf_sk_lookup_kern,
11254 family, 2, target_size));
11257 case offsetof(struct bpf_sk_lookup, protocol):
11258 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
11259 bpf_target_off(struct bpf_sk_lookup_kern,
11260 protocol, 2, target_size));
11263 case offsetof(struct bpf_sk_lookup, remote_ip4):
11264 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
11265 bpf_target_off(struct bpf_sk_lookup_kern,
11266 v4.saddr, 4, target_size));
11269 case offsetof(struct bpf_sk_lookup, local_ip4):
11270 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
11271 bpf_target_off(struct bpf_sk_lookup_kern,
11272 v4.daddr, 4, target_size));
11275 case bpf_ctx_range_till(struct bpf_sk_lookup,
11276 remote_ip6[0], remote_ip6[3]): {
11277 #if IS_ENABLED(CONFIG_IPV6)
11280 off -= offsetof(struct bpf_sk_lookup, remote_ip6[0]);
11281 off += bpf_target_off(struct in6_addr, s6_addr32[0], 4, target_size);
11282 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg, si->src_reg,
11283 offsetof(struct bpf_sk_lookup_kern, v6.saddr));
11284 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
11285 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, off);
11287 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
11291 case bpf_ctx_range_till(struct bpf_sk_lookup,
11292 local_ip6[0], local_ip6[3]): {
11293 #if IS_ENABLED(CONFIG_IPV6)
11296 off -= offsetof(struct bpf_sk_lookup, local_ip6[0]);
11297 off += bpf_target_off(struct in6_addr, s6_addr32[0], 4, target_size);
11298 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg, si->src_reg,
11299 offsetof(struct bpf_sk_lookup_kern, v6.daddr));
11300 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
11301 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, off);
11303 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
11307 case offsetof(struct bpf_sk_lookup, remote_port):
11308 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
11309 bpf_target_off(struct bpf_sk_lookup_kern,
11310 sport, 2, target_size));
11313 case offsetofend(struct bpf_sk_lookup, remote_port):
11315 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
11318 case offsetof(struct bpf_sk_lookup, local_port):
11319 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
11320 bpf_target_off(struct bpf_sk_lookup_kern,
11321 dport, 2, target_size));
11324 case offsetof(struct bpf_sk_lookup, ingress_ifindex):
11325 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
11326 bpf_target_off(struct bpf_sk_lookup_kern,
11327 ingress_ifindex, 4, target_size));
11331 return insn - insn_buf;
11334 const struct bpf_prog_ops sk_lookup_prog_ops = {
11335 .test_run = bpf_prog_test_run_sk_lookup,
11338 const struct bpf_verifier_ops sk_lookup_verifier_ops = {
11339 .get_func_proto = sk_lookup_func_proto,
11340 .is_valid_access = sk_lookup_is_valid_access,
11341 .convert_ctx_access = sk_lookup_convert_ctx_access,
11344 #endif /* CONFIG_INET */
11346 DEFINE_BPF_DISPATCHER(xdp)
11348 void bpf_prog_change_xdp(struct bpf_prog *prev_prog, struct bpf_prog *prog)
11350 bpf_dispatcher_change_prog(BPF_DISPATCHER_PTR(xdp), prev_prog, prog);
11353 BTF_ID_LIST_GLOBAL(btf_sock_ids, MAX_BTF_SOCK_TYPE)
11354 #define BTF_SOCK_TYPE(name, type) BTF_ID(struct, type)
11356 #undef BTF_SOCK_TYPE
11358 BPF_CALL_1(bpf_skc_to_tcp6_sock, struct sock *, sk)
11360 /* tcp6_sock type is not generated in dwarf and hence btf,
11361 * trigger an explicit type generation here.
11363 BTF_TYPE_EMIT(struct tcp6_sock);
11364 if (sk && sk_fullsock(sk) && sk->sk_protocol == IPPROTO_TCP &&
11365 sk->sk_family == AF_INET6)
11366 return (unsigned long)sk;
11368 return (unsigned long)NULL;
11371 const struct bpf_func_proto bpf_skc_to_tcp6_sock_proto = {
11372 .func = bpf_skc_to_tcp6_sock,
11374 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
11375 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
11376 .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_TCP6],
11379 BPF_CALL_1(bpf_skc_to_tcp_sock, struct sock *, sk)
11381 if (sk && sk_fullsock(sk) && sk->sk_protocol == IPPROTO_TCP)
11382 return (unsigned long)sk;
11384 return (unsigned long)NULL;
11387 const struct bpf_func_proto bpf_skc_to_tcp_sock_proto = {
11388 .func = bpf_skc_to_tcp_sock,
11390 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
11391 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
11392 .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_TCP],
11395 BPF_CALL_1(bpf_skc_to_tcp_timewait_sock, struct sock *, sk)
11397 /* BTF types for tcp_timewait_sock and inet_timewait_sock are not
11398 * generated if CONFIG_INET=n. Trigger an explicit generation here.
11400 BTF_TYPE_EMIT(struct inet_timewait_sock);
11401 BTF_TYPE_EMIT(struct tcp_timewait_sock);
11404 if (sk && sk->sk_prot == &tcp_prot && sk->sk_state == TCP_TIME_WAIT)
11405 return (unsigned long)sk;
11408 #if IS_BUILTIN(CONFIG_IPV6)
11409 if (sk && sk->sk_prot == &tcpv6_prot && sk->sk_state == TCP_TIME_WAIT)
11410 return (unsigned long)sk;
11413 return (unsigned long)NULL;
11416 const struct bpf_func_proto bpf_skc_to_tcp_timewait_sock_proto = {
11417 .func = bpf_skc_to_tcp_timewait_sock,
11419 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
11420 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
11421 .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_TCP_TW],
11424 BPF_CALL_1(bpf_skc_to_tcp_request_sock, struct sock *, sk)
11427 if (sk && sk->sk_prot == &tcp_prot && sk->sk_state == TCP_NEW_SYN_RECV)
11428 return (unsigned long)sk;
11431 #if IS_BUILTIN(CONFIG_IPV6)
11432 if (sk && sk->sk_prot == &tcpv6_prot && sk->sk_state == TCP_NEW_SYN_RECV)
11433 return (unsigned long)sk;
11436 return (unsigned long)NULL;
11439 const struct bpf_func_proto bpf_skc_to_tcp_request_sock_proto = {
11440 .func = bpf_skc_to_tcp_request_sock,
11442 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
11443 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
11444 .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_TCP_REQ],
11447 BPF_CALL_1(bpf_skc_to_udp6_sock, struct sock *, sk)
11449 /* udp6_sock type is not generated in dwarf and hence btf,
11450 * trigger an explicit type generation here.
11452 BTF_TYPE_EMIT(struct udp6_sock);
11453 if (sk && sk_fullsock(sk) && sk->sk_protocol == IPPROTO_UDP &&
11454 sk->sk_type == SOCK_DGRAM && sk->sk_family == AF_INET6)
11455 return (unsigned long)sk;
11457 return (unsigned long)NULL;
11460 const struct bpf_func_proto bpf_skc_to_udp6_sock_proto = {
11461 .func = bpf_skc_to_udp6_sock,
11463 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
11464 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
11465 .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_UDP6],
11468 BPF_CALL_1(bpf_skc_to_unix_sock, struct sock *, sk)
11470 /* unix_sock type is not generated in dwarf and hence btf,
11471 * trigger an explicit type generation here.
11473 BTF_TYPE_EMIT(struct unix_sock);
11474 if (sk && sk_fullsock(sk) && sk->sk_family == AF_UNIX)
11475 return (unsigned long)sk;
11477 return (unsigned long)NULL;
11480 const struct bpf_func_proto bpf_skc_to_unix_sock_proto = {
11481 .func = bpf_skc_to_unix_sock,
11483 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
11484 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
11485 .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_UNIX],
11488 BPF_CALL_1(bpf_skc_to_mptcp_sock, struct sock *, sk)
11490 BTF_TYPE_EMIT(struct mptcp_sock);
11491 return (unsigned long)bpf_mptcp_sock_from_subflow(sk);
11494 const struct bpf_func_proto bpf_skc_to_mptcp_sock_proto = {
11495 .func = bpf_skc_to_mptcp_sock,
11497 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
11498 .arg1_type = ARG_PTR_TO_SOCK_COMMON,
11499 .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_MPTCP],
11502 BPF_CALL_1(bpf_sock_from_file, struct file *, file)
11504 return (unsigned long)sock_from_file(file);
11507 BTF_ID_LIST(bpf_sock_from_file_btf_ids)
11508 BTF_ID(struct, socket)
11509 BTF_ID(struct, file)
11511 const struct bpf_func_proto bpf_sock_from_file_proto = {
11512 .func = bpf_sock_from_file,
11514 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
11515 .ret_btf_id = &bpf_sock_from_file_btf_ids[0],
11516 .arg1_type = ARG_PTR_TO_BTF_ID,
11517 .arg1_btf_id = &bpf_sock_from_file_btf_ids[1],
11520 static const struct bpf_func_proto *
11521 bpf_sk_base_func_proto(enum bpf_func_id func_id)
11523 const struct bpf_func_proto *func;
11526 case BPF_FUNC_skc_to_tcp6_sock:
11527 func = &bpf_skc_to_tcp6_sock_proto;
11529 case BPF_FUNC_skc_to_tcp_sock:
11530 func = &bpf_skc_to_tcp_sock_proto;
11532 case BPF_FUNC_skc_to_tcp_timewait_sock:
11533 func = &bpf_skc_to_tcp_timewait_sock_proto;
11535 case BPF_FUNC_skc_to_tcp_request_sock:
11536 func = &bpf_skc_to_tcp_request_sock_proto;
11538 case BPF_FUNC_skc_to_udp6_sock:
11539 func = &bpf_skc_to_udp6_sock_proto;
11541 case BPF_FUNC_skc_to_unix_sock:
11542 func = &bpf_skc_to_unix_sock_proto;
11544 case BPF_FUNC_skc_to_mptcp_sock:
11545 func = &bpf_skc_to_mptcp_sock_proto;
11547 case BPF_FUNC_ktime_get_coarse_ns:
11548 return &bpf_ktime_get_coarse_ns_proto;
11550 return bpf_base_func_proto(func_id);
11553 if (!perfmon_capable())