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>
83 #include <net/netfilter/nf_conntrack_bpf.h>
84 #include <net/netkit.h>
86 #include <net/xdp_sock_drv.h>
90 static const struct bpf_func_proto *
91 bpf_sk_base_func_proto(enum bpf_func_id func_id);
93 int copy_bpf_fprog_from_user(struct sock_fprog *dst, sockptr_t src, int len)
95 if (in_compat_syscall()) {
96 struct compat_sock_fprog f32;
98 if (len != sizeof(f32))
100 if (copy_from_sockptr(&f32, src, sizeof(f32)))
102 memset(dst, 0, sizeof(*dst));
104 dst->filter = compat_ptr(f32.filter);
106 if (len != sizeof(*dst))
108 if (copy_from_sockptr(dst, src, sizeof(*dst)))
114 EXPORT_SYMBOL_GPL(copy_bpf_fprog_from_user);
117 * sk_filter_trim_cap - run a packet through a socket filter
118 * @sk: sock associated with &sk_buff
119 * @skb: buffer to filter
120 * @cap: limit on how short the eBPF program may trim the packet
122 * Run the eBPF program and then cut skb->data to correct size returned by
123 * the program. If pkt_len is 0 we toss packet. If skb->len is smaller
124 * than pkt_len we keep whole skb->data. This is the socket level
125 * wrapper to bpf_prog_run. It returns 0 if the packet should
126 * be accepted or -EPERM if the packet should be tossed.
129 int sk_filter_trim_cap(struct sock *sk, struct sk_buff *skb, unsigned int cap)
132 struct sk_filter *filter;
135 * If the skb was allocated from pfmemalloc reserves, only
136 * allow SOCK_MEMALLOC sockets to use it as this socket is
137 * helping free memory
139 if (skb_pfmemalloc(skb) && !sock_flag(sk, SOCK_MEMALLOC)) {
140 NET_INC_STATS(sock_net(sk), LINUX_MIB_PFMEMALLOCDROP);
143 err = BPF_CGROUP_RUN_PROG_INET_INGRESS(sk, skb);
147 err = security_sock_rcv_skb(sk, skb);
152 filter = rcu_dereference(sk->sk_filter);
154 struct sock *save_sk = skb->sk;
155 unsigned int pkt_len;
158 pkt_len = bpf_prog_run_save_cb(filter->prog, skb);
160 err = pkt_len ? pskb_trim(skb, max(cap, pkt_len)) : -EPERM;
166 EXPORT_SYMBOL(sk_filter_trim_cap);
168 BPF_CALL_1(bpf_skb_get_pay_offset, struct sk_buff *, skb)
170 return skb_get_poff(skb);
173 BPF_CALL_3(bpf_skb_get_nlattr, struct sk_buff *, skb, u32, a, u32, x)
177 if (skb_is_nonlinear(skb))
180 if (skb->len < sizeof(struct nlattr))
183 if (a > skb->len - sizeof(struct nlattr))
186 nla = nla_find((struct nlattr *) &skb->data[a], skb->len - a, x);
188 return (void *) nla - (void *) skb->data;
193 BPF_CALL_3(bpf_skb_get_nlattr_nest, struct sk_buff *, skb, u32, a, u32, x)
197 if (skb_is_nonlinear(skb))
200 if (skb->len < sizeof(struct nlattr))
203 if (a > skb->len - sizeof(struct nlattr))
206 nla = (struct nlattr *) &skb->data[a];
207 if (!nla_ok(nla, skb->len - a))
210 nla = nla_find_nested(nla, x);
212 return (void *) nla - (void *) skb->data;
217 BPF_CALL_4(bpf_skb_load_helper_8, const struct sk_buff *, skb, const void *,
218 data, int, headlen, int, offset)
221 const int len = sizeof(tmp);
224 if (headlen - offset >= len)
225 return *(u8 *)(data + offset);
226 if (!skb_copy_bits(skb, offset, &tmp, sizeof(tmp)))
229 ptr = bpf_internal_load_pointer_neg_helper(skb, offset, len);
237 BPF_CALL_2(bpf_skb_load_helper_8_no_cache, const struct sk_buff *, skb,
240 return ____bpf_skb_load_helper_8(skb, skb->data, skb->len - skb->data_len,
244 BPF_CALL_4(bpf_skb_load_helper_16, const struct sk_buff *, skb, const void *,
245 data, int, headlen, int, offset)
248 const int len = sizeof(tmp);
251 if (headlen - offset >= len)
252 return get_unaligned_be16(data + offset);
253 if (!skb_copy_bits(skb, offset, &tmp, sizeof(tmp)))
254 return be16_to_cpu(tmp);
256 ptr = bpf_internal_load_pointer_neg_helper(skb, offset, len);
258 return get_unaligned_be16(ptr);
264 BPF_CALL_2(bpf_skb_load_helper_16_no_cache, const struct sk_buff *, skb,
267 return ____bpf_skb_load_helper_16(skb, skb->data, skb->len - skb->data_len,
271 BPF_CALL_4(bpf_skb_load_helper_32, const struct sk_buff *, skb, const void *,
272 data, int, headlen, int, offset)
275 const int len = sizeof(tmp);
277 if (likely(offset >= 0)) {
278 if (headlen - offset >= len)
279 return get_unaligned_be32(data + offset);
280 if (!skb_copy_bits(skb, offset, &tmp, sizeof(tmp)))
281 return be32_to_cpu(tmp);
283 ptr = bpf_internal_load_pointer_neg_helper(skb, offset, len);
285 return get_unaligned_be32(ptr);
291 BPF_CALL_2(bpf_skb_load_helper_32_no_cache, const struct sk_buff *, skb,
294 return ____bpf_skb_load_helper_32(skb, skb->data, skb->len - skb->data_len,
298 static u32 convert_skb_access(int skb_field, int dst_reg, int src_reg,
299 struct bpf_insn *insn_buf)
301 struct bpf_insn *insn = insn_buf;
305 BUILD_BUG_ON(sizeof_field(struct sk_buff, mark) != 4);
307 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
308 offsetof(struct sk_buff, mark));
312 *insn++ = BPF_LDX_MEM(BPF_B, dst_reg, src_reg, PKT_TYPE_OFFSET);
313 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, PKT_TYPE_MAX);
314 #ifdef __BIG_ENDIAN_BITFIELD
315 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 5);
320 BUILD_BUG_ON(sizeof_field(struct sk_buff, queue_mapping) != 2);
322 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
323 offsetof(struct sk_buff, queue_mapping));
326 case SKF_AD_VLAN_TAG:
327 BUILD_BUG_ON(sizeof_field(struct sk_buff, vlan_tci) != 2);
329 /* dst_reg = *(u16 *) (src_reg + offsetof(vlan_tci)) */
330 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
331 offsetof(struct sk_buff, vlan_tci));
333 case SKF_AD_VLAN_TAG_PRESENT:
334 BUILD_BUG_ON(sizeof_field(struct sk_buff, vlan_all) != 4);
335 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
336 offsetof(struct sk_buff, vlan_all));
337 *insn++ = BPF_JMP_IMM(BPF_JEQ, dst_reg, 0, 1);
338 *insn++ = BPF_ALU32_IMM(BPF_MOV, dst_reg, 1);
342 return insn - insn_buf;
345 static bool convert_bpf_extensions(struct sock_filter *fp,
346 struct bpf_insn **insnp)
348 struct bpf_insn *insn = *insnp;
352 case SKF_AD_OFF + SKF_AD_PROTOCOL:
353 BUILD_BUG_ON(sizeof_field(struct sk_buff, protocol) != 2);
355 /* A = *(u16 *) (CTX + offsetof(protocol)) */
356 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
357 offsetof(struct sk_buff, protocol));
358 /* A = ntohs(A) [emitting a nop or swap16] */
359 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
362 case SKF_AD_OFF + SKF_AD_PKTTYPE:
363 cnt = convert_skb_access(SKF_AD_PKTTYPE, BPF_REG_A, BPF_REG_CTX, insn);
367 case SKF_AD_OFF + SKF_AD_IFINDEX:
368 case SKF_AD_OFF + SKF_AD_HATYPE:
369 BUILD_BUG_ON(sizeof_field(struct net_device, ifindex) != 4);
370 BUILD_BUG_ON(sizeof_field(struct net_device, type) != 2);
372 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
373 BPF_REG_TMP, BPF_REG_CTX,
374 offsetof(struct sk_buff, dev));
375 /* if (tmp != 0) goto pc + 1 */
376 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_TMP, 0, 1);
377 *insn++ = BPF_EXIT_INSN();
378 if (fp->k == SKF_AD_OFF + SKF_AD_IFINDEX)
379 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_TMP,
380 offsetof(struct net_device, ifindex));
382 *insn = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_TMP,
383 offsetof(struct net_device, type));
386 case SKF_AD_OFF + SKF_AD_MARK:
387 cnt = convert_skb_access(SKF_AD_MARK, BPF_REG_A, BPF_REG_CTX, insn);
391 case SKF_AD_OFF + SKF_AD_RXHASH:
392 BUILD_BUG_ON(sizeof_field(struct sk_buff, hash) != 4);
394 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX,
395 offsetof(struct sk_buff, hash));
398 case SKF_AD_OFF + SKF_AD_QUEUE:
399 cnt = convert_skb_access(SKF_AD_QUEUE, BPF_REG_A, BPF_REG_CTX, insn);
403 case SKF_AD_OFF + SKF_AD_VLAN_TAG:
404 cnt = convert_skb_access(SKF_AD_VLAN_TAG,
405 BPF_REG_A, BPF_REG_CTX, insn);
409 case SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT:
410 cnt = convert_skb_access(SKF_AD_VLAN_TAG_PRESENT,
411 BPF_REG_A, BPF_REG_CTX, insn);
415 case SKF_AD_OFF + SKF_AD_VLAN_TPID:
416 BUILD_BUG_ON(sizeof_field(struct sk_buff, vlan_proto) != 2);
418 /* A = *(u16 *) (CTX + offsetof(vlan_proto)) */
419 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
420 offsetof(struct sk_buff, vlan_proto));
421 /* A = ntohs(A) [emitting a nop or swap16] */
422 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
425 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
426 case SKF_AD_OFF + SKF_AD_NLATTR:
427 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
428 case SKF_AD_OFF + SKF_AD_CPU:
429 case SKF_AD_OFF + SKF_AD_RANDOM:
431 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX);
433 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_A);
435 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_X);
436 /* Emit call(arg1=CTX, arg2=A, arg3=X) */
438 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
439 *insn = BPF_EMIT_CALL(bpf_skb_get_pay_offset);
441 case SKF_AD_OFF + SKF_AD_NLATTR:
442 *insn = BPF_EMIT_CALL(bpf_skb_get_nlattr);
444 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
445 *insn = BPF_EMIT_CALL(bpf_skb_get_nlattr_nest);
447 case SKF_AD_OFF + SKF_AD_CPU:
448 *insn = BPF_EMIT_CALL(bpf_get_raw_cpu_id);
450 case SKF_AD_OFF + SKF_AD_RANDOM:
451 *insn = BPF_EMIT_CALL(bpf_user_rnd_u32);
452 bpf_user_rnd_init_once();
457 case SKF_AD_OFF + SKF_AD_ALU_XOR_X:
459 *insn = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_X);
463 /* This is just a dummy call to avoid letting the compiler
464 * evict __bpf_call_base() as an optimization. Placed here
465 * where no-one bothers.
467 BUG_ON(__bpf_call_base(0, 0, 0, 0, 0) != 0);
475 static bool convert_bpf_ld_abs(struct sock_filter *fp, struct bpf_insn **insnp)
477 const bool unaligned_ok = IS_BUILTIN(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS);
478 int size = bpf_size_to_bytes(BPF_SIZE(fp->code));
479 bool endian = BPF_SIZE(fp->code) == BPF_H ||
480 BPF_SIZE(fp->code) == BPF_W;
481 bool indirect = BPF_MODE(fp->code) == BPF_IND;
482 const int ip_align = NET_IP_ALIGN;
483 struct bpf_insn *insn = *insnp;
487 ((unaligned_ok && offset >= 0) ||
488 (!unaligned_ok && offset >= 0 &&
489 offset + ip_align >= 0 &&
490 offset + ip_align % size == 0))) {
491 bool ldx_off_ok = offset <= S16_MAX;
493 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_H);
495 *insn++ = BPF_ALU64_IMM(BPF_SUB, BPF_REG_TMP, offset);
496 *insn++ = BPF_JMP_IMM(BPF_JSLT, BPF_REG_TMP,
497 size, 2 + endian + (!ldx_off_ok * 2));
499 *insn++ = BPF_LDX_MEM(BPF_SIZE(fp->code), BPF_REG_A,
502 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_D);
503 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_TMP, offset);
504 *insn++ = BPF_LDX_MEM(BPF_SIZE(fp->code), BPF_REG_A,
508 *insn++ = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, size * 8);
509 *insn++ = BPF_JMP_A(8);
512 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX);
513 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_D);
514 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_H);
516 *insn++ = BPF_MOV64_IMM(BPF_REG_ARG4, offset);
518 *insn++ = BPF_MOV64_REG(BPF_REG_ARG4, BPF_REG_X);
520 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_ARG4, offset);
523 switch (BPF_SIZE(fp->code)) {
525 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_8);
528 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_16);
531 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_32);
537 *insn++ = BPF_JMP_IMM(BPF_JSGE, BPF_REG_A, 0, 2);
538 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
539 *insn = BPF_EXIT_INSN();
546 * bpf_convert_filter - convert filter program
547 * @prog: the user passed filter program
548 * @len: the length of the user passed filter program
549 * @new_prog: allocated 'struct bpf_prog' or NULL
550 * @new_len: pointer to store length of converted program
551 * @seen_ld_abs: bool whether we've seen ld_abs/ind
553 * Remap 'sock_filter' style classic BPF (cBPF) instruction set to 'bpf_insn'
554 * style extended BPF (eBPF).
555 * Conversion workflow:
557 * 1) First pass for calculating the new program length:
558 * bpf_convert_filter(old_prog, old_len, NULL, &new_len, &seen_ld_abs)
560 * 2) 2nd pass to remap in two passes: 1st pass finds new
561 * jump offsets, 2nd pass remapping:
562 * bpf_convert_filter(old_prog, old_len, new_prog, &new_len, &seen_ld_abs)
564 static int bpf_convert_filter(struct sock_filter *prog, int len,
565 struct bpf_prog *new_prog, int *new_len,
568 int new_flen = 0, pass = 0, target, i, stack_off;
569 struct bpf_insn *new_insn, *first_insn = NULL;
570 struct sock_filter *fp;
574 BUILD_BUG_ON(BPF_MEMWORDS * sizeof(u32) > MAX_BPF_STACK);
575 BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG);
577 if (len <= 0 || len > BPF_MAXINSNS)
581 first_insn = new_prog->insnsi;
582 addrs = kcalloc(len, sizeof(*addrs),
583 GFP_KERNEL | __GFP_NOWARN);
589 new_insn = first_insn;
592 /* Classic BPF related prologue emission. */
594 /* Classic BPF expects A and X to be reset first. These need
595 * to be guaranteed to be the first two instructions.
597 *new_insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
598 *new_insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_X, BPF_REG_X);
600 /* All programs must keep CTX in callee saved BPF_REG_CTX.
601 * In eBPF case it's done by the compiler, here we need to
602 * do this ourself. Initial CTX is present in BPF_REG_ARG1.
604 *new_insn++ = BPF_MOV64_REG(BPF_REG_CTX, BPF_REG_ARG1);
606 /* For packet access in classic BPF, cache skb->data
607 * in callee-saved BPF R8 and skb->len - skb->data_len
608 * (headlen) in BPF R9. Since classic BPF is read-only
609 * on CTX, we only need to cache it once.
611 *new_insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
612 BPF_REG_D, BPF_REG_CTX,
613 offsetof(struct sk_buff, data));
614 *new_insn++ = BPF_LDX_MEM(BPF_W, BPF_REG_H, BPF_REG_CTX,
615 offsetof(struct sk_buff, len));
616 *new_insn++ = BPF_LDX_MEM(BPF_W, BPF_REG_TMP, BPF_REG_CTX,
617 offsetof(struct sk_buff, data_len));
618 *new_insn++ = BPF_ALU32_REG(BPF_SUB, BPF_REG_H, BPF_REG_TMP);
624 for (i = 0; i < len; fp++, i++) {
625 struct bpf_insn tmp_insns[32] = { };
626 struct bpf_insn *insn = tmp_insns;
629 addrs[i] = new_insn - first_insn;
632 /* All arithmetic insns and skb loads map as-is. */
633 case BPF_ALU | BPF_ADD | BPF_X:
634 case BPF_ALU | BPF_ADD | BPF_K:
635 case BPF_ALU | BPF_SUB | BPF_X:
636 case BPF_ALU | BPF_SUB | BPF_K:
637 case BPF_ALU | BPF_AND | BPF_X:
638 case BPF_ALU | BPF_AND | BPF_K:
639 case BPF_ALU | BPF_OR | BPF_X:
640 case BPF_ALU | BPF_OR | BPF_K:
641 case BPF_ALU | BPF_LSH | BPF_X:
642 case BPF_ALU | BPF_LSH | BPF_K:
643 case BPF_ALU | BPF_RSH | BPF_X:
644 case BPF_ALU | BPF_RSH | BPF_K:
645 case BPF_ALU | BPF_XOR | BPF_X:
646 case BPF_ALU | BPF_XOR | BPF_K:
647 case BPF_ALU | BPF_MUL | BPF_X:
648 case BPF_ALU | BPF_MUL | BPF_K:
649 case BPF_ALU | BPF_DIV | BPF_X:
650 case BPF_ALU | BPF_DIV | BPF_K:
651 case BPF_ALU | BPF_MOD | BPF_X:
652 case BPF_ALU | BPF_MOD | BPF_K:
653 case BPF_ALU | BPF_NEG:
654 case BPF_LD | BPF_ABS | BPF_W:
655 case BPF_LD | BPF_ABS | BPF_H:
656 case BPF_LD | BPF_ABS | BPF_B:
657 case BPF_LD | BPF_IND | BPF_W:
658 case BPF_LD | BPF_IND | BPF_H:
659 case BPF_LD | BPF_IND | BPF_B:
660 /* Check for overloaded BPF extension and
661 * directly convert it if found, otherwise
662 * just move on with mapping.
664 if (BPF_CLASS(fp->code) == BPF_LD &&
665 BPF_MODE(fp->code) == BPF_ABS &&
666 convert_bpf_extensions(fp, &insn))
668 if (BPF_CLASS(fp->code) == BPF_LD &&
669 convert_bpf_ld_abs(fp, &insn)) {
674 if (fp->code == (BPF_ALU | BPF_DIV | BPF_X) ||
675 fp->code == (BPF_ALU | BPF_MOD | BPF_X)) {
676 *insn++ = BPF_MOV32_REG(BPF_REG_X, BPF_REG_X);
677 /* Error with exception code on div/mod by 0.
678 * For cBPF programs, this was always return 0.
680 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_X, 0, 2);
681 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
682 *insn++ = BPF_EXIT_INSN();
685 *insn = BPF_RAW_INSN(fp->code, BPF_REG_A, BPF_REG_X, 0, fp->k);
688 /* Jump transformation cannot use BPF block macros
689 * everywhere as offset calculation and target updates
690 * require a bit more work than the rest, i.e. jump
691 * opcodes map as-is, but offsets need adjustment.
694 #define BPF_EMIT_JMP \
696 const s32 off_min = S16_MIN, off_max = S16_MAX; \
699 if (target >= len || target < 0) \
701 off = addrs ? addrs[target] - addrs[i] - 1 : 0; \
702 /* Adjust pc relative offset for 2nd or 3rd insn. */ \
703 off -= insn - tmp_insns; \
704 /* Reject anything not fitting into insn->off. */ \
705 if (off < off_min || off > off_max) \
710 case BPF_JMP | BPF_JA:
711 target = i + fp->k + 1;
712 insn->code = fp->code;
716 case BPF_JMP | BPF_JEQ | BPF_K:
717 case BPF_JMP | BPF_JEQ | BPF_X:
718 case BPF_JMP | BPF_JSET | BPF_K:
719 case BPF_JMP | BPF_JSET | BPF_X:
720 case BPF_JMP | BPF_JGT | BPF_K:
721 case BPF_JMP | BPF_JGT | BPF_X:
722 case BPF_JMP | BPF_JGE | BPF_K:
723 case BPF_JMP | BPF_JGE | BPF_X:
724 if (BPF_SRC(fp->code) == BPF_K && (int) fp->k < 0) {
725 /* BPF immediates are signed, zero extend
726 * immediate into tmp register and use it
729 *insn++ = BPF_MOV32_IMM(BPF_REG_TMP, fp->k);
731 insn->dst_reg = BPF_REG_A;
732 insn->src_reg = BPF_REG_TMP;
735 insn->dst_reg = BPF_REG_A;
737 bpf_src = BPF_SRC(fp->code);
738 insn->src_reg = bpf_src == BPF_X ? BPF_REG_X : 0;
741 /* Common case where 'jump_false' is next insn. */
743 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
744 target = i + fp->jt + 1;
749 /* Convert some jumps when 'jump_true' is next insn. */
751 switch (BPF_OP(fp->code)) {
753 insn->code = BPF_JMP | BPF_JNE | bpf_src;
756 insn->code = BPF_JMP | BPF_JLE | bpf_src;
759 insn->code = BPF_JMP | BPF_JLT | bpf_src;
765 target = i + fp->jf + 1;
770 /* Other jumps are mapped into two insns: Jxx and JA. */
771 target = i + fp->jt + 1;
772 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
776 insn->code = BPF_JMP | BPF_JA;
777 target = i + fp->jf + 1;
781 /* ldxb 4 * ([14] & 0xf) is remaped into 6 insns. */
782 case BPF_LDX | BPF_MSH | BPF_B: {
783 struct sock_filter tmp = {
784 .code = BPF_LD | BPF_ABS | BPF_B,
791 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
792 /* A = BPF_R0 = *(u8 *) (skb->data + K) */
793 convert_bpf_ld_abs(&tmp, &insn);
796 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, 0xf);
798 *insn++ = BPF_ALU32_IMM(BPF_LSH, BPF_REG_A, 2);
800 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_X);
802 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
804 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_TMP);
807 /* RET_K is remaped into 2 insns. RET_A case doesn't need an
808 * extra mov as BPF_REG_0 is already mapped into BPF_REG_A.
810 case BPF_RET | BPF_A:
811 case BPF_RET | BPF_K:
812 if (BPF_RVAL(fp->code) == BPF_K)
813 *insn++ = BPF_MOV32_RAW(BPF_K, BPF_REG_0,
815 *insn = BPF_EXIT_INSN();
818 /* Store to stack. */
821 stack_off = fp->k * 4 + 4;
822 *insn = BPF_STX_MEM(BPF_W, BPF_REG_FP, BPF_CLASS(fp->code) ==
823 BPF_ST ? BPF_REG_A : BPF_REG_X,
825 /* check_load_and_stores() verifies that classic BPF can
826 * load from stack only after write, so tracking
827 * stack_depth for ST|STX insns is enough
829 if (new_prog && new_prog->aux->stack_depth < stack_off)
830 new_prog->aux->stack_depth = stack_off;
833 /* Load from stack. */
834 case BPF_LD | BPF_MEM:
835 case BPF_LDX | BPF_MEM:
836 stack_off = fp->k * 4 + 4;
837 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
838 BPF_REG_A : BPF_REG_X, BPF_REG_FP,
843 case BPF_LD | BPF_IMM:
844 case BPF_LDX | BPF_IMM:
845 *insn = BPF_MOV32_IMM(BPF_CLASS(fp->code) == BPF_LD ?
846 BPF_REG_A : BPF_REG_X, fp->k);
850 case BPF_MISC | BPF_TAX:
851 *insn = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
855 case BPF_MISC | BPF_TXA:
856 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_X);
859 /* A = skb->len or X = skb->len */
860 case BPF_LD | BPF_W | BPF_LEN:
861 case BPF_LDX | BPF_W | BPF_LEN:
862 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
863 BPF_REG_A : BPF_REG_X, BPF_REG_CTX,
864 offsetof(struct sk_buff, len));
867 /* Access seccomp_data fields. */
868 case BPF_LDX | BPF_ABS | BPF_W:
869 /* A = *(u32 *) (ctx + K) */
870 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX, fp->k);
873 /* Unknown instruction. */
880 memcpy(new_insn, tmp_insns,
881 sizeof(*insn) * (insn - tmp_insns));
882 new_insn += insn - tmp_insns;
886 /* Only calculating new length. */
887 *new_len = new_insn - first_insn;
889 *new_len += 4; /* Prologue bits. */
894 if (new_flen != new_insn - first_insn) {
895 new_flen = new_insn - first_insn;
902 BUG_ON(*new_len != new_flen);
911 * As we dont want to clear mem[] array for each packet going through
912 * __bpf_prog_run(), we check that filter loaded by user never try to read
913 * a cell if not previously written, and we check all branches to be sure
914 * a malicious user doesn't try to abuse us.
916 static int check_load_and_stores(const struct sock_filter *filter, int flen)
918 u16 *masks, memvalid = 0; /* One bit per cell, 16 cells */
921 BUILD_BUG_ON(BPF_MEMWORDS > 16);
923 masks = kmalloc_array(flen, sizeof(*masks), GFP_KERNEL);
927 memset(masks, 0xff, flen * sizeof(*masks));
929 for (pc = 0; pc < flen; pc++) {
930 memvalid &= masks[pc];
932 switch (filter[pc].code) {
935 memvalid |= (1 << filter[pc].k);
937 case BPF_LD | BPF_MEM:
938 case BPF_LDX | BPF_MEM:
939 if (!(memvalid & (1 << filter[pc].k))) {
944 case BPF_JMP | BPF_JA:
945 /* A jump must set masks on target */
946 masks[pc + 1 + filter[pc].k] &= memvalid;
949 case BPF_JMP | BPF_JEQ | BPF_K:
950 case BPF_JMP | BPF_JEQ | BPF_X:
951 case BPF_JMP | BPF_JGE | BPF_K:
952 case BPF_JMP | BPF_JGE | BPF_X:
953 case BPF_JMP | BPF_JGT | BPF_K:
954 case BPF_JMP | BPF_JGT | BPF_X:
955 case BPF_JMP | BPF_JSET | BPF_K:
956 case BPF_JMP | BPF_JSET | BPF_X:
957 /* A jump must set masks on targets */
958 masks[pc + 1 + filter[pc].jt] &= memvalid;
959 masks[pc + 1 + filter[pc].jf] &= memvalid;
969 static bool chk_code_allowed(u16 code_to_probe)
971 static const bool codes[] = {
972 /* 32 bit ALU operations */
973 [BPF_ALU | BPF_ADD | BPF_K] = true,
974 [BPF_ALU | BPF_ADD | BPF_X] = true,
975 [BPF_ALU | BPF_SUB | BPF_K] = true,
976 [BPF_ALU | BPF_SUB | BPF_X] = true,
977 [BPF_ALU | BPF_MUL | BPF_K] = true,
978 [BPF_ALU | BPF_MUL | BPF_X] = true,
979 [BPF_ALU | BPF_DIV | BPF_K] = true,
980 [BPF_ALU | BPF_DIV | BPF_X] = true,
981 [BPF_ALU | BPF_MOD | BPF_K] = true,
982 [BPF_ALU | BPF_MOD | BPF_X] = true,
983 [BPF_ALU | BPF_AND | BPF_K] = true,
984 [BPF_ALU | BPF_AND | BPF_X] = true,
985 [BPF_ALU | BPF_OR | BPF_K] = true,
986 [BPF_ALU | BPF_OR | BPF_X] = true,
987 [BPF_ALU | BPF_XOR | BPF_K] = true,
988 [BPF_ALU | BPF_XOR | BPF_X] = true,
989 [BPF_ALU | BPF_LSH | BPF_K] = true,
990 [BPF_ALU | BPF_LSH | BPF_X] = true,
991 [BPF_ALU | BPF_RSH | BPF_K] = true,
992 [BPF_ALU | BPF_RSH | BPF_X] = true,
993 [BPF_ALU | BPF_NEG] = true,
994 /* Load instructions */
995 [BPF_LD | BPF_W | BPF_ABS] = true,
996 [BPF_LD | BPF_H | BPF_ABS] = true,
997 [BPF_LD | BPF_B | BPF_ABS] = true,
998 [BPF_LD | BPF_W | BPF_LEN] = true,
999 [BPF_LD | BPF_W | BPF_IND] = true,
1000 [BPF_LD | BPF_H | BPF_IND] = true,
1001 [BPF_LD | BPF_B | BPF_IND] = true,
1002 [BPF_LD | BPF_IMM] = true,
1003 [BPF_LD | BPF_MEM] = true,
1004 [BPF_LDX | BPF_W | BPF_LEN] = true,
1005 [BPF_LDX | BPF_B | BPF_MSH] = true,
1006 [BPF_LDX | BPF_IMM] = true,
1007 [BPF_LDX | BPF_MEM] = true,
1008 /* Store instructions */
1011 /* Misc instructions */
1012 [BPF_MISC | BPF_TAX] = true,
1013 [BPF_MISC | BPF_TXA] = true,
1014 /* Return instructions */
1015 [BPF_RET | BPF_K] = true,
1016 [BPF_RET | BPF_A] = true,
1017 /* Jump instructions */
1018 [BPF_JMP | BPF_JA] = true,
1019 [BPF_JMP | BPF_JEQ | BPF_K] = true,
1020 [BPF_JMP | BPF_JEQ | BPF_X] = true,
1021 [BPF_JMP | BPF_JGE | BPF_K] = true,
1022 [BPF_JMP | BPF_JGE | BPF_X] = true,
1023 [BPF_JMP | BPF_JGT | BPF_K] = true,
1024 [BPF_JMP | BPF_JGT | BPF_X] = true,
1025 [BPF_JMP | BPF_JSET | BPF_K] = true,
1026 [BPF_JMP | BPF_JSET | BPF_X] = true,
1029 if (code_to_probe >= ARRAY_SIZE(codes))
1032 return codes[code_to_probe];
1035 static bool bpf_check_basics_ok(const struct sock_filter *filter,
1040 if (flen == 0 || flen > BPF_MAXINSNS)
1047 * bpf_check_classic - verify socket filter code
1048 * @filter: filter to verify
1049 * @flen: length of filter
1051 * Check the user's filter code. If we let some ugly
1052 * filter code slip through kaboom! The filter must contain
1053 * no references or jumps that are out of range, no illegal
1054 * instructions, and must end with a RET instruction.
1056 * All jumps are forward as they are not signed.
1058 * Returns 0 if the rule set is legal or -EINVAL if not.
1060 static int bpf_check_classic(const struct sock_filter *filter,
1066 /* Check the filter code now */
1067 for (pc = 0; pc < flen; pc++) {
1068 const struct sock_filter *ftest = &filter[pc];
1070 /* May we actually operate on this code? */
1071 if (!chk_code_allowed(ftest->code))
1074 /* Some instructions need special checks */
1075 switch (ftest->code) {
1076 case BPF_ALU | BPF_DIV | BPF_K:
1077 case BPF_ALU | BPF_MOD | BPF_K:
1078 /* Check for division by zero */
1082 case BPF_ALU | BPF_LSH | BPF_K:
1083 case BPF_ALU | BPF_RSH | BPF_K:
1087 case BPF_LD | BPF_MEM:
1088 case BPF_LDX | BPF_MEM:
1091 /* Check for invalid memory addresses */
1092 if (ftest->k >= BPF_MEMWORDS)
1095 case BPF_JMP | BPF_JA:
1096 /* Note, the large ftest->k might cause loops.
1097 * Compare this with conditional jumps below,
1098 * where offsets are limited. --ANK (981016)
1100 if (ftest->k >= (unsigned int)(flen - pc - 1))
1103 case BPF_JMP | BPF_JEQ | BPF_K:
1104 case BPF_JMP | BPF_JEQ | BPF_X:
1105 case BPF_JMP | BPF_JGE | BPF_K:
1106 case BPF_JMP | BPF_JGE | BPF_X:
1107 case BPF_JMP | BPF_JGT | BPF_K:
1108 case BPF_JMP | BPF_JGT | BPF_X:
1109 case BPF_JMP | BPF_JSET | BPF_K:
1110 case BPF_JMP | BPF_JSET | BPF_X:
1111 /* Both conditionals must be safe */
1112 if (pc + ftest->jt + 1 >= flen ||
1113 pc + ftest->jf + 1 >= flen)
1116 case BPF_LD | BPF_W | BPF_ABS:
1117 case BPF_LD | BPF_H | BPF_ABS:
1118 case BPF_LD | BPF_B | BPF_ABS:
1120 if (bpf_anc_helper(ftest) & BPF_ANC)
1122 /* Ancillary operation unknown or unsupported */
1123 if (anc_found == false && ftest->k >= SKF_AD_OFF)
1128 /* Last instruction must be a RET code */
1129 switch (filter[flen - 1].code) {
1130 case BPF_RET | BPF_K:
1131 case BPF_RET | BPF_A:
1132 return check_load_and_stores(filter, flen);
1138 static int bpf_prog_store_orig_filter(struct bpf_prog *fp,
1139 const struct sock_fprog *fprog)
1141 unsigned int fsize = bpf_classic_proglen(fprog);
1142 struct sock_fprog_kern *fkprog;
1144 fp->orig_prog = kmalloc(sizeof(*fkprog), GFP_KERNEL);
1148 fkprog = fp->orig_prog;
1149 fkprog->len = fprog->len;
1151 fkprog->filter = kmemdup(fp->insns, fsize,
1152 GFP_KERNEL | __GFP_NOWARN);
1153 if (!fkprog->filter) {
1154 kfree(fp->orig_prog);
1161 static void bpf_release_orig_filter(struct bpf_prog *fp)
1163 struct sock_fprog_kern *fprog = fp->orig_prog;
1166 kfree(fprog->filter);
1171 static void __bpf_prog_release(struct bpf_prog *prog)
1173 if (prog->type == BPF_PROG_TYPE_SOCKET_FILTER) {
1176 bpf_release_orig_filter(prog);
1177 bpf_prog_free(prog);
1181 static void __sk_filter_release(struct sk_filter *fp)
1183 __bpf_prog_release(fp->prog);
1188 * sk_filter_release_rcu - Release a socket filter by rcu_head
1189 * @rcu: rcu_head that contains the sk_filter to free
1191 static void sk_filter_release_rcu(struct rcu_head *rcu)
1193 struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu);
1195 __sk_filter_release(fp);
1199 * sk_filter_release - release a socket filter
1200 * @fp: filter to remove
1202 * Remove a filter from a socket and release its resources.
1204 static void sk_filter_release(struct sk_filter *fp)
1206 if (refcount_dec_and_test(&fp->refcnt))
1207 call_rcu(&fp->rcu, sk_filter_release_rcu);
1210 void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp)
1212 u32 filter_size = bpf_prog_size(fp->prog->len);
1214 atomic_sub(filter_size, &sk->sk_omem_alloc);
1215 sk_filter_release(fp);
1218 /* try to charge the socket memory if there is space available
1219 * return true on success
1221 static bool __sk_filter_charge(struct sock *sk, struct sk_filter *fp)
1223 int optmem_max = READ_ONCE(sock_net(sk)->core.sysctl_optmem_max);
1224 u32 filter_size = bpf_prog_size(fp->prog->len);
1226 /* same check as in sock_kmalloc() */
1227 if (filter_size <= optmem_max &&
1228 atomic_read(&sk->sk_omem_alloc) + filter_size < optmem_max) {
1229 atomic_add(filter_size, &sk->sk_omem_alloc);
1235 bool sk_filter_charge(struct sock *sk, struct sk_filter *fp)
1237 if (!refcount_inc_not_zero(&fp->refcnt))
1240 if (!__sk_filter_charge(sk, fp)) {
1241 sk_filter_release(fp);
1247 static struct bpf_prog *bpf_migrate_filter(struct bpf_prog *fp)
1249 struct sock_filter *old_prog;
1250 struct bpf_prog *old_fp;
1251 int err, new_len, old_len = fp->len;
1252 bool seen_ld_abs = false;
1254 /* We are free to overwrite insns et al right here as it won't be used at
1255 * this point in time anymore internally after the migration to the eBPF
1256 * instruction representation.
1258 BUILD_BUG_ON(sizeof(struct sock_filter) !=
1259 sizeof(struct bpf_insn));
1261 /* Conversion cannot happen on overlapping memory areas,
1262 * so we need to keep the user BPF around until the 2nd
1263 * pass. At this time, the user BPF is stored in fp->insns.
1265 old_prog = kmemdup(fp->insns, old_len * sizeof(struct sock_filter),
1266 GFP_KERNEL | __GFP_NOWARN);
1272 /* 1st pass: calculate the new program length. */
1273 err = bpf_convert_filter(old_prog, old_len, NULL, &new_len,
1278 /* Expand fp for appending the new filter representation. */
1280 fp = bpf_prog_realloc(old_fp, bpf_prog_size(new_len), 0);
1282 /* The old_fp is still around in case we couldn't
1283 * allocate new memory, so uncharge on that one.
1292 /* 2nd pass: remap sock_filter insns into bpf_insn insns. */
1293 err = bpf_convert_filter(old_prog, old_len, fp, &new_len,
1296 /* 2nd bpf_convert_filter() can fail only if it fails
1297 * to allocate memory, remapping must succeed. Note,
1298 * that at this time old_fp has already been released
1303 fp = bpf_prog_select_runtime(fp, &err);
1313 __bpf_prog_release(fp);
1314 return ERR_PTR(err);
1317 static struct bpf_prog *bpf_prepare_filter(struct bpf_prog *fp,
1318 bpf_aux_classic_check_t trans)
1322 fp->bpf_func = NULL;
1325 err = bpf_check_classic(fp->insns, fp->len);
1327 __bpf_prog_release(fp);
1328 return ERR_PTR(err);
1331 /* There might be additional checks and transformations
1332 * needed on classic filters, f.e. in case of seccomp.
1335 err = trans(fp->insns, fp->len);
1337 __bpf_prog_release(fp);
1338 return ERR_PTR(err);
1342 /* Probe if we can JIT compile the filter and if so, do
1343 * the compilation of the filter.
1345 bpf_jit_compile(fp);
1347 /* JIT compiler couldn't process this filter, so do the eBPF translation
1348 * for the optimized interpreter.
1351 fp = bpf_migrate_filter(fp);
1357 * bpf_prog_create - create an unattached filter
1358 * @pfp: the unattached filter that is created
1359 * @fprog: the filter program
1361 * Create a filter independent of any socket. We first run some
1362 * sanity checks on it to make sure it does not explode on us later.
1363 * If an error occurs or there is insufficient memory for the filter
1364 * a negative errno code is returned. On success the return is zero.
1366 int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog)
1368 unsigned int fsize = bpf_classic_proglen(fprog);
1369 struct bpf_prog *fp;
1371 /* Make sure new filter is there and in the right amounts. */
1372 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1375 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1379 memcpy(fp->insns, fprog->filter, fsize);
1381 fp->len = fprog->len;
1382 /* Since unattached filters are not copied back to user
1383 * space through sk_get_filter(), we do not need to hold
1384 * a copy here, and can spare us the work.
1386 fp->orig_prog = NULL;
1388 /* bpf_prepare_filter() already takes care of freeing
1389 * memory in case something goes wrong.
1391 fp = bpf_prepare_filter(fp, NULL);
1398 EXPORT_SYMBOL_GPL(bpf_prog_create);
1401 * bpf_prog_create_from_user - create an unattached filter from user buffer
1402 * @pfp: the unattached filter that is created
1403 * @fprog: the filter program
1404 * @trans: post-classic verifier transformation handler
1405 * @save_orig: save classic BPF program
1407 * This function effectively does the same as bpf_prog_create(), only
1408 * that it builds up its insns buffer from user space provided buffer.
1409 * It also allows for passing a bpf_aux_classic_check_t handler.
1411 int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog,
1412 bpf_aux_classic_check_t trans, bool save_orig)
1414 unsigned int fsize = bpf_classic_proglen(fprog);
1415 struct bpf_prog *fp;
1418 /* Make sure new filter is there and in the right amounts. */
1419 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1422 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1426 if (copy_from_user(fp->insns, fprog->filter, fsize)) {
1427 __bpf_prog_free(fp);
1431 fp->len = fprog->len;
1432 fp->orig_prog = NULL;
1435 err = bpf_prog_store_orig_filter(fp, fprog);
1437 __bpf_prog_free(fp);
1442 /* bpf_prepare_filter() already takes care of freeing
1443 * memory in case something goes wrong.
1445 fp = bpf_prepare_filter(fp, trans);
1452 EXPORT_SYMBOL_GPL(bpf_prog_create_from_user);
1454 void bpf_prog_destroy(struct bpf_prog *fp)
1456 __bpf_prog_release(fp);
1458 EXPORT_SYMBOL_GPL(bpf_prog_destroy);
1460 static int __sk_attach_prog(struct bpf_prog *prog, struct sock *sk)
1462 struct sk_filter *fp, *old_fp;
1464 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
1470 if (!__sk_filter_charge(sk, fp)) {
1474 refcount_set(&fp->refcnt, 1);
1476 old_fp = rcu_dereference_protected(sk->sk_filter,
1477 lockdep_sock_is_held(sk));
1478 rcu_assign_pointer(sk->sk_filter, fp);
1481 sk_filter_uncharge(sk, old_fp);
1487 struct bpf_prog *__get_filter(struct sock_fprog *fprog, struct sock *sk)
1489 unsigned int fsize = bpf_classic_proglen(fprog);
1490 struct bpf_prog *prog;
1493 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1494 return ERR_PTR(-EPERM);
1496 /* Make sure new filter is there and in the right amounts. */
1497 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1498 return ERR_PTR(-EINVAL);
1500 prog = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1502 return ERR_PTR(-ENOMEM);
1504 if (copy_from_user(prog->insns, fprog->filter, fsize)) {
1505 __bpf_prog_free(prog);
1506 return ERR_PTR(-EFAULT);
1509 prog->len = fprog->len;
1511 err = bpf_prog_store_orig_filter(prog, fprog);
1513 __bpf_prog_free(prog);
1514 return ERR_PTR(-ENOMEM);
1517 /* bpf_prepare_filter() already takes care of freeing
1518 * memory in case something goes wrong.
1520 return bpf_prepare_filter(prog, NULL);
1524 * sk_attach_filter - attach a socket filter
1525 * @fprog: the filter program
1526 * @sk: the socket to use
1528 * Attach the user's filter code. We first run some sanity checks on
1529 * it to make sure it does not explode on us later. If an error
1530 * occurs or there is insufficient memory for the filter a negative
1531 * errno code is returned. On success the return is zero.
1533 int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1535 struct bpf_prog *prog = __get_filter(fprog, sk);
1539 return PTR_ERR(prog);
1541 err = __sk_attach_prog(prog, sk);
1543 __bpf_prog_release(prog);
1549 EXPORT_SYMBOL_GPL(sk_attach_filter);
1551 int sk_reuseport_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1553 struct bpf_prog *prog = __get_filter(fprog, sk);
1554 int err, optmem_max;
1557 return PTR_ERR(prog);
1559 optmem_max = READ_ONCE(sock_net(sk)->core.sysctl_optmem_max);
1560 if (bpf_prog_size(prog->len) > optmem_max)
1563 err = reuseport_attach_prog(sk, prog);
1566 __bpf_prog_release(prog);
1571 static struct bpf_prog *__get_bpf(u32 ufd, struct sock *sk)
1573 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1574 return ERR_PTR(-EPERM);
1576 return bpf_prog_get_type(ufd, BPF_PROG_TYPE_SOCKET_FILTER);
1579 int sk_attach_bpf(u32 ufd, struct sock *sk)
1581 struct bpf_prog *prog = __get_bpf(ufd, sk);
1585 return PTR_ERR(prog);
1587 err = __sk_attach_prog(prog, sk);
1596 int sk_reuseport_attach_bpf(u32 ufd, struct sock *sk)
1598 struct bpf_prog *prog;
1599 int err, optmem_max;
1601 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1604 prog = bpf_prog_get_type(ufd, BPF_PROG_TYPE_SOCKET_FILTER);
1605 if (PTR_ERR(prog) == -EINVAL)
1606 prog = bpf_prog_get_type(ufd, BPF_PROG_TYPE_SK_REUSEPORT);
1608 return PTR_ERR(prog);
1610 if (prog->type == BPF_PROG_TYPE_SK_REUSEPORT) {
1611 /* Like other non BPF_PROG_TYPE_SOCKET_FILTER
1612 * bpf prog (e.g. sockmap). It depends on the
1613 * limitation imposed by bpf_prog_load().
1614 * Hence, sysctl_optmem_max is not checked.
1616 if ((sk->sk_type != SOCK_STREAM &&
1617 sk->sk_type != SOCK_DGRAM) ||
1618 (sk->sk_protocol != IPPROTO_UDP &&
1619 sk->sk_protocol != IPPROTO_TCP) ||
1620 (sk->sk_family != AF_INET &&
1621 sk->sk_family != AF_INET6)) {
1626 /* BPF_PROG_TYPE_SOCKET_FILTER */
1627 optmem_max = READ_ONCE(sock_net(sk)->core.sysctl_optmem_max);
1628 if (bpf_prog_size(prog->len) > optmem_max) {
1634 err = reuseport_attach_prog(sk, prog);
1642 void sk_reuseport_prog_free(struct bpf_prog *prog)
1647 if (prog->type == BPF_PROG_TYPE_SK_REUSEPORT)
1650 bpf_prog_destroy(prog);
1653 struct bpf_scratchpad {
1655 __be32 diff[MAX_BPF_STACK / sizeof(__be32)];
1656 u8 buff[MAX_BPF_STACK];
1660 static DEFINE_PER_CPU(struct bpf_scratchpad, bpf_sp);
1662 static inline int __bpf_try_make_writable(struct sk_buff *skb,
1663 unsigned int write_len)
1665 return skb_ensure_writable(skb, write_len);
1668 static inline int bpf_try_make_writable(struct sk_buff *skb,
1669 unsigned int write_len)
1671 int err = __bpf_try_make_writable(skb, write_len);
1673 bpf_compute_data_pointers(skb);
1677 static int bpf_try_make_head_writable(struct sk_buff *skb)
1679 return bpf_try_make_writable(skb, skb_headlen(skb));
1682 static inline void bpf_push_mac_rcsum(struct sk_buff *skb)
1684 if (skb_at_tc_ingress(skb))
1685 skb_postpush_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1688 static inline void bpf_pull_mac_rcsum(struct sk_buff *skb)
1690 if (skb_at_tc_ingress(skb))
1691 skb_postpull_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1694 BPF_CALL_5(bpf_skb_store_bytes, struct sk_buff *, skb, u32, offset,
1695 const void *, from, u32, len, u64, flags)
1699 if (unlikely(flags & ~(BPF_F_RECOMPUTE_CSUM | BPF_F_INVALIDATE_HASH)))
1701 if (unlikely(offset > INT_MAX))
1703 if (unlikely(bpf_try_make_writable(skb, offset + len)))
1706 ptr = skb->data + offset;
1707 if (flags & BPF_F_RECOMPUTE_CSUM)
1708 __skb_postpull_rcsum(skb, ptr, len, offset);
1710 memcpy(ptr, from, len);
1712 if (flags & BPF_F_RECOMPUTE_CSUM)
1713 __skb_postpush_rcsum(skb, ptr, len, offset);
1714 if (flags & BPF_F_INVALIDATE_HASH)
1715 skb_clear_hash(skb);
1720 static const struct bpf_func_proto bpf_skb_store_bytes_proto = {
1721 .func = bpf_skb_store_bytes,
1723 .ret_type = RET_INTEGER,
1724 .arg1_type = ARG_PTR_TO_CTX,
1725 .arg2_type = ARG_ANYTHING,
1726 .arg3_type = ARG_PTR_TO_MEM | MEM_RDONLY,
1727 .arg4_type = ARG_CONST_SIZE,
1728 .arg5_type = ARG_ANYTHING,
1731 int __bpf_skb_store_bytes(struct sk_buff *skb, u32 offset, const void *from,
1734 return ____bpf_skb_store_bytes(skb, offset, from, len, flags);
1737 BPF_CALL_4(bpf_skb_load_bytes, const struct sk_buff *, skb, u32, offset,
1738 void *, to, u32, len)
1742 if (unlikely(offset > INT_MAX))
1745 ptr = skb_header_pointer(skb, offset, len, to);
1749 memcpy(to, ptr, len);
1757 static const struct bpf_func_proto bpf_skb_load_bytes_proto = {
1758 .func = bpf_skb_load_bytes,
1760 .ret_type = RET_INTEGER,
1761 .arg1_type = ARG_PTR_TO_CTX,
1762 .arg2_type = ARG_ANYTHING,
1763 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
1764 .arg4_type = ARG_CONST_SIZE,
1767 int __bpf_skb_load_bytes(const struct sk_buff *skb, u32 offset, void *to, u32 len)
1769 return ____bpf_skb_load_bytes(skb, offset, to, len);
1772 BPF_CALL_4(bpf_flow_dissector_load_bytes,
1773 const struct bpf_flow_dissector *, ctx, u32, offset,
1774 void *, to, u32, len)
1778 if (unlikely(offset > 0xffff))
1781 if (unlikely(!ctx->skb))
1784 ptr = skb_header_pointer(ctx->skb, offset, len, to);
1788 memcpy(to, ptr, len);
1796 static const struct bpf_func_proto bpf_flow_dissector_load_bytes_proto = {
1797 .func = bpf_flow_dissector_load_bytes,
1799 .ret_type = RET_INTEGER,
1800 .arg1_type = ARG_PTR_TO_CTX,
1801 .arg2_type = ARG_ANYTHING,
1802 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
1803 .arg4_type = ARG_CONST_SIZE,
1806 BPF_CALL_5(bpf_skb_load_bytes_relative, const struct sk_buff *, skb,
1807 u32, offset, void *, to, u32, len, u32, start_header)
1809 u8 *end = skb_tail_pointer(skb);
1812 if (unlikely(offset > 0xffff))
1815 switch (start_header) {
1816 case BPF_HDR_START_MAC:
1817 if (unlikely(!skb_mac_header_was_set(skb)))
1819 start = skb_mac_header(skb);
1821 case BPF_HDR_START_NET:
1822 start = skb_network_header(skb);
1828 ptr = start + offset;
1830 if (likely(ptr + len <= end)) {
1831 memcpy(to, ptr, len);
1840 static const struct bpf_func_proto bpf_skb_load_bytes_relative_proto = {
1841 .func = bpf_skb_load_bytes_relative,
1843 .ret_type = RET_INTEGER,
1844 .arg1_type = ARG_PTR_TO_CTX,
1845 .arg2_type = ARG_ANYTHING,
1846 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
1847 .arg4_type = ARG_CONST_SIZE,
1848 .arg5_type = ARG_ANYTHING,
1851 BPF_CALL_2(bpf_skb_pull_data, struct sk_buff *, skb, u32, len)
1853 /* Idea is the following: should the needed direct read/write
1854 * test fail during runtime, we can pull in more data and redo
1855 * again, since implicitly, we invalidate previous checks here.
1857 * Or, since we know how much we need to make read/writeable,
1858 * this can be done once at the program beginning for direct
1859 * access case. By this we overcome limitations of only current
1860 * headroom being accessible.
1862 return bpf_try_make_writable(skb, len ? : skb_headlen(skb));
1865 static const struct bpf_func_proto bpf_skb_pull_data_proto = {
1866 .func = bpf_skb_pull_data,
1868 .ret_type = RET_INTEGER,
1869 .arg1_type = ARG_PTR_TO_CTX,
1870 .arg2_type = ARG_ANYTHING,
1873 BPF_CALL_1(bpf_sk_fullsock, struct sock *, sk)
1875 return sk_fullsock(sk) ? (unsigned long)sk : (unsigned long)NULL;
1878 static const struct bpf_func_proto bpf_sk_fullsock_proto = {
1879 .func = bpf_sk_fullsock,
1881 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
1882 .arg1_type = ARG_PTR_TO_SOCK_COMMON,
1885 static inline int sk_skb_try_make_writable(struct sk_buff *skb,
1886 unsigned int write_len)
1888 return __bpf_try_make_writable(skb, write_len);
1891 BPF_CALL_2(sk_skb_pull_data, struct sk_buff *, skb, u32, len)
1893 /* Idea is the following: should the needed direct read/write
1894 * test fail during runtime, we can pull in more data and redo
1895 * again, since implicitly, we invalidate previous checks here.
1897 * Or, since we know how much we need to make read/writeable,
1898 * this can be done once at the program beginning for direct
1899 * access case. By this we overcome limitations of only current
1900 * headroom being accessible.
1902 return sk_skb_try_make_writable(skb, len ? : skb_headlen(skb));
1905 static const struct bpf_func_proto sk_skb_pull_data_proto = {
1906 .func = sk_skb_pull_data,
1908 .ret_type = RET_INTEGER,
1909 .arg1_type = ARG_PTR_TO_CTX,
1910 .arg2_type = ARG_ANYTHING,
1913 BPF_CALL_5(bpf_l3_csum_replace, struct sk_buff *, skb, u32, offset,
1914 u64, from, u64, to, u64, flags)
1918 if (unlikely(flags & ~(BPF_F_HDR_FIELD_MASK)))
1920 if (unlikely(offset > 0xffff || offset & 1))
1922 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
1925 ptr = (__sum16 *)(skb->data + offset);
1926 switch (flags & BPF_F_HDR_FIELD_MASK) {
1928 if (unlikely(from != 0))
1931 csum_replace_by_diff(ptr, to);
1934 csum_replace2(ptr, from, to);
1937 csum_replace4(ptr, from, to);
1946 static const struct bpf_func_proto bpf_l3_csum_replace_proto = {
1947 .func = bpf_l3_csum_replace,
1949 .ret_type = RET_INTEGER,
1950 .arg1_type = ARG_PTR_TO_CTX,
1951 .arg2_type = ARG_ANYTHING,
1952 .arg3_type = ARG_ANYTHING,
1953 .arg4_type = ARG_ANYTHING,
1954 .arg5_type = ARG_ANYTHING,
1957 BPF_CALL_5(bpf_l4_csum_replace, struct sk_buff *, skb, u32, offset,
1958 u64, from, u64, to, u64, flags)
1960 bool is_pseudo = flags & BPF_F_PSEUDO_HDR;
1961 bool is_mmzero = flags & BPF_F_MARK_MANGLED_0;
1962 bool do_mforce = flags & BPF_F_MARK_ENFORCE;
1965 if (unlikely(flags & ~(BPF_F_MARK_MANGLED_0 | BPF_F_MARK_ENFORCE |
1966 BPF_F_PSEUDO_HDR | BPF_F_HDR_FIELD_MASK)))
1968 if (unlikely(offset > 0xffff || offset & 1))
1970 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
1973 ptr = (__sum16 *)(skb->data + offset);
1974 if (is_mmzero && !do_mforce && !*ptr)
1977 switch (flags & BPF_F_HDR_FIELD_MASK) {
1979 if (unlikely(from != 0))
1982 inet_proto_csum_replace_by_diff(ptr, skb, to, is_pseudo);
1985 inet_proto_csum_replace2(ptr, skb, from, to, is_pseudo);
1988 inet_proto_csum_replace4(ptr, skb, from, to, is_pseudo);
1994 if (is_mmzero && !*ptr)
1995 *ptr = CSUM_MANGLED_0;
1999 static const struct bpf_func_proto bpf_l4_csum_replace_proto = {
2000 .func = bpf_l4_csum_replace,
2002 .ret_type = RET_INTEGER,
2003 .arg1_type = ARG_PTR_TO_CTX,
2004 .arg2_type = ARG_ANYTHING,
2005 .arg3_type = ARG_ANYTHING,
2006 .arg4_type = ARG_ANYTHING,
2007 .arg5_type = ARG_ANYTHING,
2010 BPF_CALL_5(bpf_csum_diff, __be32 *, from, u32, from_size,
2011 __be32 *, to, u32, to_size, __wsum, seed)
2013 struct bpf_scratchpad *sp = this_cpu_ptr(&bpf_sp);
2014 u32 diff_size = from_size + to_size;
2017 /* This is quite flexible, some examples:
2019 * from_size == 0, to_size > 0, seed := csum --> pushing data
2020 * from_size > 0, to_size == 0, seed := csum --> pulling data
2021 * from_size > 0, to_size > 0, seed := 0 --> diffing data
2023 * Even for diffing, from_size and to_size don't need to be equal.
2025 if (unlikely(((from_size | to_size) & (sizeof(__be32) - 1)) ||
2026 diff_size > sizeof(sp->diff)))
2029 for (i = 0; i < from_size / sizeof(__be32); i++, j++)
2030 sp->diff[j] = ~from[i];
2031 for (i = 0; i < to_size / sizeof(__be32); i++, j++)
2032 sp->diff[j] = to[i];
2034 return csum_partial(sp->diff, diff_size, seed);
2037 static const struct bpf_func_proto bpf_csum_diff_proto = {
2038 .func = bpf_csum_diff,
2041 .ret_type = RET_INTEGER,
2042 .arg1_type = ARG_PTR_TO_MEM | PTR_MAYBE_NULL | MEM_RDONLY,
2043 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
2044 .arg3_type = ARG_PTR_TO_MEM | PTR_MAYBE_NULL | MEM_RDONLY,
2045 .arg4_type = ARG_CONST_SIZE_OR_ZERO,
2046 .arg5_type = ARG_ANYTHING,
2049 BPF_CALL_2(bpf_csum_update, struct sk_buff *, skb, __wsum, csum)
2051 /* The interface is to be used in combination with bpf_csum_diff()
2052 * for direct packet writes. csum rotation for alignment as well
2053 * as emulating csum_sub() can be done from the eBPF program.
2055 if (skb->ip_summed == CHECKSUM_COMPLETE)
2056 return (skb->csum = csum_add(skb->csum, csum));
2061 static const struct bpf_func_proto bpf_csum_update_proto = {
2062 .func = bpf_csum_update,
2064 .ret_type = RET_INTEGER,
2065 .arg1_type = ARG_PTR_TO_CTX,
2066 .arg2_type = ARG_ANYTHING,
2069 BPF_CALL_2(bpf_csum_level, struct sk_buff *, skb, u64, level)
2071 /* The interface is to be used in combination with bpf_skb_adjust_room()
2072 * for encap/decap of packet headers when BPF_F_ADJ_ROOM_NO_CSUM_RESET
2073 * is passed as flags, for example.
2076 case BPF_CSUM_LEVEL_INC:
2077 __skb_incr_checksum_unnecessary(skb);
2079 case BPF_CSUM_LEVEL_DEC:
2080 __skb_decr_checksum_unnecessary(skb);
2082 case BPF_CSUM_LEVEL_RESET:
2083 __skb_reset_checksum_unnecessary(skb);
2085 case BPF_CSUM_LEVEL_QUERY:
2086 return skb->ip_summed == CHECKSUM_UNNECESSARY ?
2087 skb->csum_level : -EACCES;
2095 static const struct bpf_func_proto bpf_csum_level_proto = {
2096 .func = bpf_csum_level,
2098 .ret_type = RET_INTEGER,
2099 .arg1_type = ARG_PTR_TO_CTX,
2100 .arg2_type = ARG_ANYTHING,
2103 static inline int __bpf_rx_skb(struct net_device *dev, struct sk_buff *skb)
2105 return dev_forward_skb_nomtu(dev, skb);
2108 static inline int __bpf_rx_skb_no_mac(struct net_device *dev,
2109 struct sk_buff *skb)
2111 int ret = ____dev_forward_skb(dev, skb, false);
2115 ret = netif_rx(skb);
2121 static inline int __bpf_tx_skb(struct net_device *dev, struct sk_buff *skb)
2125 if (dev_xmit_recursion()) {
2126 net_crit_ratelimited("bpf: recursion limit reached on datapath, buggy bpf program?\n");
2132 skb_set_redirected_noclear(skb, skb_at_tc_ingress(skb));
2133 skb_clear_tstamp(skb);
2135 dev_xmit_recursion_inc();
2136 ret = dev_queue_xmit(skb);
2137 dev_xmit_recursion_dec();
2142 static int __bpf_redirect_no_mac(struct sk_buff *skb, struct net_device *dev,
2145 unsigned int mlen = skb_network_offset(skb);
2147 if (unlikely(skb->len <= mlen)) {
2153 __skb_pull(skb, mlen);
2155 /* At ingress, the mac header has already been pulled once.
2156 * At egress, skb_pospull_rcsum has to be done in case that
2157 * the skb is originated from ingress (i.e. a forwarded skb)
2158 * to ensure that rcsum starts at net header.
2160 if (!skb_at_tc_ingress(skb))
2161 skb_postpull_rcsum(skb, skb_mac_header(skb), mlen);
2163 skb_pop_mac_header(skb);
2164 skb_reset_mac_len(skb);
2165 return flags & BPF_F_INGRESS ?
2166 __bpf_rx_skb_no_mac(dev, skb) : __bpf_tx_skb(dev, skb);
2169 static int __bpf_redirect_common(struct sk_buff *skb, struct net_device *dev,
2172 /* Verify that a link layer header is carried */
2173 if (unlikely(skb->mac_header >= skb->network_header || skb->len == 0)) {
2178 bpf_push_mac_rcsum(skb);
2179 return flags & BPF_F_INGRESS ?
2180 __bpf_rx_skb(dev, skb) : __bpf_tx_skb(dev, skb);
2183 static int __bpf_redirect(struct sk_buff *skb, struct net_device *dev,
2186 if (dev_is_mac_header_xmit(dev))
2187 return __bpf_redirect_common(skb, dev, flags);
2189 return __bpf_redirect_no_mac(skb, dev, flags);
2192 #if IS_ENABLED(CONFIG_IPV6)
2193 static int bpf_out_neigh_v6(struct net *net, struct sk_buff *skb,
2194 struct net_device *dev, struct bpf_nh_params *nh)
2196 u32 hh_len = LL_RESERVED_SPACE(dev);
2197 const struct in6_addr *nexthop;
2198 struct dst_entry *dst = NULL;
2199 struct neighbour *neigh;
2201 if (dev_xmit_recursion()) {
2202 net_crit_ratelimited("bpf: recursion limit reached on datapath, buggy bpf program?\n");
2207 skb_clear_tstamp(skb);
2209 if (unlikely(skb_headroom(skb) < hh_len && dev->header_ops)) {
2210 skb = skb_expand_head(skb, hh_len);
2218 nexthop = rt6_nexthop(container_of(dst, struct rt6_info, dst),
2219 &ipv6_hdr(skb)->daddr);
2221 nexthop = &nh->ipv6_nh;
2223 neigh = ip_neigh_gw6(dev, nexthop);
2224 if (likely(!IS_ERR(neigh))) {
2227 sock_confirm_neigh(skb, neigh);
2229 dev_xmit_recursion_inc();
2230 ret = neigh_output(neigh, skb, false);
2231 dev_xmit_recursion_dec();
2236 rcu_read_unlock_bh();
2238 IP6_INC_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_OUTNOROUTES);
2244 static int __bpf_redirect_neigh_v6(struct sk_buff *skb, struct net_device *dev,
2245 struct bpf_nh_params *nh)
2247 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
2248 struct net *net = dev_net(dev);
2249 int err, ret = NET_XMIT_DROP;
2252 struct dst_entry *dst;
2253 struct flowi6 fl6 = {
2254 .flowi6_flags = FLOWI_FLAG_ANYSRC,
2255 .flowi6_mark = skb->mark,
2256 .flowlabel = ip6_flowinfo(ip6h),
2257 .flowi6_oif = dev->ifindex,
2258 .flowi6_proto = ip6h->nexthdr,
2259 .daddr = ip6h->daddr,
2260 .saddr = ip6h->saddr,
2263 dst = ipv6_stub->ipv6_dst_lookup_flow(net, NULL, &fl6, NULL);
2267 skb_dst_set(skb, dst);
2268 } else if (nh->nh_family != AF_INET6) {
2272 err = bpf_out_neigh_v6(net, skb, dev, nh);
2273 if (unlikely(net_xmit_eval(err)))
2274 dev->stats.tx_errors++;
2276 ret = NET_XMIT_SUCCESS;
2279 dev->stats.tx_errors++;
2285 static int __bpf_redirect_neigh_v6(struct sk_buff *skb, struct net_device *dev,
2286 struct bpf_nh_params *nh)
2289 return NET_XMIT_DROP;
2291 #endif /* CONFIG_IPV6 */
2293 #if IS_ENABLED(CONFIG_INET)
2294 static int bpf_out_neigh_v4(struct net *net, struct sk_buff *skb,
2295 struct net_device *dev, struct bpf_nh_params *nh)
2297 u32 hh_len = LL_RESERVED_SPACE(dev);
2298 struct neighbour *neigh;
2299 bool is_v6gw = false;
2301 if (dev_xmit_recursion()) {
2302 net_crit_ratelimited("bpf: recursion limit reached on datapath, buggy bpf program?\n");
2307 skb_clear_tstamp(skb);
2309 if (unlikely(skb_headroom(skb) < hh_len && dev->header_ops)) {
2310 skb = skb_expand_head(skb, hh_len);
2317 struct dst_entry *dst = skb_dst(skb);
2318 struct rtable *rt = container_of(dst, struct rtable, dst);
2320 neigh = ip_neigh_for_gw(rt, skb, &is_v6gw);
2321 } else if (nh->nh_family == AF_INET6) {
2322 neigh = ip_neigh_gw6(dev, &nh->ipv6_nh);
2324 } else if (nh->nh_family == AF_INET) {
2325 neigh = ip_neigh_gw4(dev, nh->ipv4_nh);
2331 if (likely(!IS_ERR(neigh))) {
2334 sock_confirm_neigh(skb, neigh);
2336 dev_xmit_recursion_inc();
2337 ret = neigh_output(neigh, skb, is_v6gw);
2338 dev_xmit_recursion_dec();
2349 static int __bpf_redirect_neigh_v4(struct sk_buff *skb, struct net_device *dev,
2350 struct bpf_nh_params *nh)
2352 const struct iphdr *ip4h = ip_hdr(skb);
2353 struct net *net = dev_net(dev);
2354 int err, ret = NET_XMIT_DROP;
2357 struct flowi4 fl4 = {
2358 .flowi4_flags = FLOWI_FLAG_ANYSRC,
2359 .flowi4_mark = skb->mark,
2360 .flowi4_tos = RT_TOS(ip4h->tos),
2361 .flowi4_oif = dev->ifindex,
2362 .flowi4_proto = ip4h->protocol,
2363 .daddr = ip4h->daddr,
2364 .saddr = ip4h->saddr,
2368 rt = ip_route_output_flow(net, &fl4, NULL);
2371 if (rt->rt_type != RTN_UNICAST && rt->rt_type != RTN_LOCAL) {
2376 skb_dst_set(skb, &rt->dst);
2379 err = bpf_out_neigh_v4(net, skb, dev, nh);
2380 if (unlikely(net_xmit_eval(err)))
2381 dev->stats.tx_errors++;
2383 ret = NET_XMIT_SUCCESS;
2386 dev->stats.tx_errors++;
2392 static int __bpf_redirect_neigh_v4(struct sk_buff *skb, struct net_device *dev,
2393 struct bpf_nh_params *nh)
2396 return NET_XMIT_DROP;
2398 #endif /* CONFIG_INET */
2400 static int __bpf_redirect_neigh(struct sk_buff *skb, struct net_device *dev,
2401 struct bpf_nh_params *nh)
2403 struct ethhdr *ethh = eth_hdr(skb);
2405 if (unlikely(skb->mac_header >= skb->network_header))
2407 bpf_push_mac_rcsum(skb);
2408 if (is_multicast_ether_addr(ethh->h_dest))
2411 skb_pull(skb, sizeof(*ethh));
2412 skb_unset_mac_header(skb);
2413 skb_reset_network_header(skb);
2415 if (skb->protocol == htons(ETH_P_IP))
2416 return __bpf_redirect_neigh_v4(skb, dev, nh);
2417 else if (skb->protocol == htons(ETH_P_IPV6))
2418 return __bpf_redirect_neigh_v6(skb, dev, nh);
2424 /* Internal, non-exposed redirect flags. */
2426 BPF_F_NEIGH = (1ULL << 1),
2427 BPF_F_PEER = (1ULL << 2),
2428 BPF_F_NEXTHOP = (1ULL << 3),
2429 #define BPF_F_REDIRECT_INTERNAL (BPF_F_NEIGH | BPF_F_PEER | BPF_F_NEXTHOP)
2432 BPF_CALL_3(bpf_clone_redirect, struct sk_buff *, skb, u32, ifindex, u64, flags)
2434 struct net_device *dev;
2435 struct sk_buff *clone;
2438 if (unlikely(flags & (~(BPF_F_INGRESS) | BPF_F_REDIRECT_INTERNAL)))
2441 dev = dev_get_by_index_rcu(dev_net(skb->dev), ifindex);
2445 clone = skb_clone(skb, GFP_ATOMIC);
2446 if (unlikely(!clone))
2449 /* For direct write, we need to keep the invariant that the skbs
2450 * we're dealing with need to be uncloned. Should uncloning fail
2451 * here, we need to free the just generated clone to unclone once
2454 ret = bpf_try_make_head_writable(skb);
2455 if (unlikely(ret)) {
2460 return __bpf_redirect(clone, dev, flags);
2463 static const struct bpf_func_proto bpf_clone_redirect_proto = {
2464 .func = bpf_clone_redirect,
2466 .ret_type = RET_INTEGER,
2467 .arg1_type = ARG_PTR_TO_CTX,
2468 .arg2_type = ARG_ANYTHING,
2469 .arg3_type = ARG_ANYTHING,
2472 DEFINE_PER_CPU(struct bpf_redirect_info, bpf_redirect_info);
2473 EXPORT_PER_CPU_SYMBOL_GPL(bpf_redirect_info);
2475 static struct net_device *skb_get_peer_dev(struct net_device *dev)
2477 const struct net_device_ops *ops = dev->netdev_ops;
2479 if (likely(ops->ndo_get_peer_dev))
2480 return INDIRECT_CALL_1(ops->ndo_get_peer_dev,
2481 netkit_peer_dev, dev);
2485 int skb_do_redirect(struct sk_buff *skb)
2487 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
2488 struct net *net = dev_net(skb->dev);
2489 struct net_device *dev;
2490 u32 flags = ri->flags;
2492 dev = dev_get_by_index_rcu(net, ri->tgt_index);
2497 if (flags & BPF_F_PEER) {
2498 if (unlikely(!skb_at_tc_ingress(skb)))
2500 dev = skb_get_peer_dev(dev);
2501 if (unlikely(!dev ||
2502 !(dev->flags & IFF_UP) ||
2503 net_eq(net, dev_net(dev))))
2506 dev_sw_netstats_rx_add(dev, skb->len);
2509 return flags & BPF_F_NEIGH ?
2510 __bpf_redirect_neigh(skb, dev, flags & BPF_F_NEXTHOP ?
2512 __bpf_redirect(skb, dev, flags);
2518 BPF_CALL_2(bpf_redirect, u32, ifindex, u64, flags)
2520 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
2522 if (unlikely(flags & (~(BPF_F_INGRESS) | BPF_F_REDIRECT_INTERNAL)))
2526 ri->tgt_index = ifindex;
2528 return TC_ACT_REDIRECT;
2531 static const struct bpf_func_proto bpf_redirect_proto = {
2532 .func = bpf_redirect,
2534 .ret_type = RET_INTEGER,
2535 .arg1_type = ARG_ANYTHING,
2536 .arg2_type = ARG_ANYTHING,
2539 BPF_CALL_2(bpf_redirect_peer, u32, ifindex, u64, flags)
2541 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
2543 if (unlikely(flags))
2546 ri->flags = BPF_F_PEER;
2547 ri->tgt_index = ifindex;
2549 return TC_ACT_REDIRECT;
2552 static const struct bpf_func_proto bpf_redirect_peer_proto = {
2553 .func = bpf_redirect_peer,
2555 .ret_type = RET_INTEGER,
2556 .arg1_type = ARG_ANYTHING,
2557 .arg2_type = ARG_ANYTHING,
2560 BPF_CALL_4(bpf_redirect_neigh, u32, ifindex, struct bpf_redir_neigh *, params,
2561 int, plen, u64, flags)
2563 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
2565 if (unlikely((plen && plen < sizeof(*params)) || flags))
2568 ri->flags = BPF_F_NEIGH | (plen ? BPF_F_NEXTHOP : 0);
2569 ri->tgt_index = ifindex;
2571 BUILD_BUG_ON(sizeof(struct bpf_redir_neigh) != sizeof(struct bpf_nh_params));
2573 memcpy(&ri->nh, params, sizeof(ri->nh));
2575 return TC_ACT_REDIRECT;
2578 static const struct bpf_func_proto bpf_redirect_neigh_proto = {
2579 .func = bpf_redirect_neigh,
2581 .ret_type = RET_INTEGER,
2582 .arg1_type = ARG_ANYTHING,
2583 .arg2_type = ARG_PTR_TO_MEM | PTR_MAYBE_NULL | MEM_RDONLY,
2584 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
2585 .arg4_type = ARG_ANYTHING,
2588 BPF_CALL_2(bpf_msg_apply_bytes, struct sk_msg *, msg, u32, bytes)
2590 msg->apply_bytes = bytes;
2594 static const struct bpf_func_proto bpf_msg_apply_bytes_proto = {
2595 .func = bpf_msg_apply_bytes,
2597 .ret_type = RET_INTEGER,
2598 .arg1_type = ARG_PTR_TO_CTX,
2599 .arg2_type = ARG_ANYTHING,
2602 BPF_CALL_2(bpf_msg_cork_bytes, struct sk_msg *, msg, u32, bytes)
2604 msg->cork_bytes = bytes;
2608 static void sk_msg_reset_curr(struct sk_msg *msg)
2610 u32 i = msg->sg.start;
2614 len += sk_msg_elem(msg, i)->length;
2615 sk_msg_iter_var_next(i);
2616 if (len >= msg->sg.size)
2618 } while (i != msg->sg.end);
2621 msg->sg.copybreak = 0;
2624 static const struct bpf_func_proto bpf_msg_cork_bytes_proto = {
2625 .func = bpf_msg_cork_bytes,
2627 .ret_type = RET_INTEGER,
2628 .arg1_type = ARG_PTR_TO_CTX,
2629 .arg2_type = ARG_ANYTHING,
2632 BPF_CALL_4(bpf_msg_pull_data, struct sk_msg *, msg, u32, start,
2633 u32, end, u64, flags)
2635 u32 len = 0, offset = 0, copy = 0, poffset = 0, bytes = end - start;
2636 u32 first_sge, last_sge, i, shift, bytes_sg_total;
2637 struct scatterlist *sge;
2638 u8 *raw, *to, *from;
2641 if (unlikely(flags || end <= start))
2644 /* First find the starting scatterlist element */
2648 len = sk_msg_elem(msg, i)->length;
2649 if (start < offset + len)
2651 sk_msg_iter_var_next(i);
2652 } while (i != msg->sg.end);
2654 if (unlikely(start >= offset + len))
2658 /* The start may point into the sg element so we need to also
2659 * account for the headroom.
2661 bytes_sg_total = start - offset + bytes;
2662 if (!test_bit(i, msg->sg.copy) && bytes_sg_total <= len)
2665 /* At this point we need to linearize multiple scatterlist
2666 * elements or a single shared page. Either way we need to
2667 * copy into a linear buffer exclusively owned by BPF. Then
2668 * place the buffer in the scatterlist and fixup the original
2669 * entries by removing the entries now in the linear buffer
2670 * and shifting the remaining entries. For now we do not try
2671 * to copy partial entries to avoid complexity of running out
2672 * of sg_entry slots. The downside is reading a single byte
2673 * will copy the entire sg entry.
2676 copy += sk_msg_elem(msg, i)->length;
2677 sk_msg_iter_var_next(i);
2678 if (bytes_sg_total <= copy)
2680 } while (i != msg->sg.end);
2683 if (unlikely(bytes_sg_total > copy))
2686 page = alloc_pages(__GFP_NOWARN | GFP_ATOMIC | __GFP_COMP,
2688 if (unlikely(!page))
2691 raw = page_address(page);
2694 sge = sk_msg_elem(msg, i);
2695 from = sg_virt(sge);
2699 memcpy(to, from, len);
2702 put_page(sg_page(sge));
2704 sk_msg_iter_var_next(i);
2705 } while (i != last_sge);
2707 sg_set_page(&msg->sg.data[first_sge], page, copy, 0);
2709 /* To repair sg ring we need to shift entries. If we only
2710 * had a single entry though we can just replace it and
2711 * be done. Otherwise walk the ring and shift the entries.
2713 WARN_ON_ONCE(last_sge == first_sge);
2714 shift = last_sge > first_sge ?
2715 last_sge - first_sge - 1 :
2716 NR_MSG_FRAG_IDS - first_sge + last_sge - 1;
2721 sk_msg_iter_var_next(i);
2725 if (i + shift >= NR_MSG_FRAG_IDS)
2726 move_from = i + shift - NR_MSG_FRAG_IDS;
2728 move_from = i + shift;
2729 if (move_from == msg->sg.end)
2732 msg->sg.data[i] = msg->sg.data[move_from];
2733 msg->sg.data[move_from].length = 0;
2734 msg->sg.data[move_from].page_link = 0;
2735 msg->sg.data[move_from].offset = 0;
2736 sk_msg_iter_var_next(i);
2739 msg->sg.end = msg->sg.end - shift > msg->sg.end ?
2740 msg->sg.end - shift + NR_MSG_FRAG_IDS :
2741 msg->sg.end - shift;
2743 sk_msg_reset_curr(msg);
2744 msg->data = sg_virt(&msg->sg.data[first_sge]) + start - offset;
2745 msg->data_end = msg->data + bytes;
2749 static const struct bpf_func_proto bpf_msg_pull_data_proto = {
2750 .func = bpf_msg_pull_data,
2752 .ret_type = RET_INTEGER,
2753 .arg1_type = ARG_PTR_TO_CTX,
2754 .arg2_type = ARG_ANYTHING,
2755 .arg3_type = ARG_ANYTHING,
2756 .arg4_type = ARG_ANYTHING,
2759 BPF_CALL_4(bpf_msg_push_data, struct sk_msg *, msg, u32, start,
2760 u32, len, u64, flags)
2762 struct scatterlist sge, nsge, nnsge, rsge = {0}, *psge;
2763 u32 new, i = 0, l = 0, space, copy = 0, offset = 0;
2764 u8 *raw, *to, *from;
2767 if (unlikely(flags))
2770 if (unlikely(len == 0))
2773 /* First find the starting scatterlist element */
2777 l = sk_msg_elem(msg, i)->length;
2779 if (start < offset + l)
2781 sk_msg_iter_var_next(i);
2782 } while (i != msg->sg.end);
2784 if (start >= offset + l)
2787 space = MAX_MSG_FRAGS - sk_msg_elem_used(msg);
2789 /* If no space available will fallback to copy, we need at
2790 * least one scatterlist elem available to push data into
2791 * when start aligns to the beginning of an element or two
2792 * when it falls inside an element. We handle the start equals
2793 * offset case because its the common case for inserting a
2796 if (!space || (space == 1 && start != offset))
2797 copy = msg->sg.data[i].length;
2799 page = alloc_pages(__GFP_NOWARN | GFP_ATOMIC | __GFP_COMP,
2800 get_order(copy + len));
2801 if (unlikely(!page))
2807 raw = page_address(page);
2809 psge = sk_msg_elem(msg, i);
2810 front = start - offset;
2811 back = psge->length - front;
2812 from = sg_virt(psge);
2815 memcpy(raw, from, front);
2819 to = raw + front + len;
2821 memcpy(to, from, back);
2824 put_page(sg_page(psge));
2825 } else if (start - offset) {
2826 psge = sk_msg_elem(msg, i);
2827 rsge = sk_msg_elem_cpy(msg, i);
2829 psge->length = start - offset;
2830 rsge.length -= psge->length;
2831 rsge.offset += start;
2833 sk_msg_iter_var_next(i);
2834 sg_unmark_end(psge);
2835 sg_unmark_end(&rsge);
2836 sk_msg_iter_next(msg, end);
2839 /* Slot(s) to place newly allocated data */
2842 /* Shift one or two slots as needed */
2844 sge = sk_msg_elem_cpy(msg, i);
2846 sk_msg_iter_var_next(i);
2847 sg_unmark_end(&sge);
2848 sk_msg_iter_next(msg, end);
2850 nsge = sk_msg_elem_cpy(msg, i);
2852 sk_msg_iter_var_next(i);
2853 nnsge = sk_msg_elem_cpy(msg, i);
2856 while (i != msg->sg.end) {
2857 msg->sg.data[i] = sge;
2859 sk_msg_iter_var_next(i);
2862 nnsge = sk_msg_elem_cpy(msg, i);
2864 nsge = sk_msg_elem_cpy(msg, i);
2869 /* Place newly allocated data buffer */
2870 sk_mem_charge(msg->sk, len);
2871 msg->sg.size += len;
2872 __clear_bit(new, msg->sg.copy);
2873 sg_set_page(&msg->sg.data[new], page, len + copy, 0);
2875 get_page(sg_page(&rsge));
2876 sk_msg_iter_var_next(new);
2877 msg->sg.data[new] = rsge;
2880 sk_msg_reset_curr(msg);
2881 sk_msg_compute_data_pointers(msg);
2885 static const struct bpf_func_proto bpf_msg_push_data_proto = {
2886 .func = bpf_msg_push_data,
2888 .ret_type = RET_INTEGER,
2889 .arg1_type = ARG_PTR_TO_CTX,
2890 .arg2_type = ARG_ANYTHING,
2891 .arg3_type = ARG_ANYTHING,
2892 .arg4_type = ARG_ANYTHING,
2895 static void sk_msg_shift_left(struct sk_msg *msg, int i)
2901 sk_msg_iter_var_next(i);
2902 msg->sg.data[prev] = msg->sg.data[i];
2903 } while (i != msg->sg.end);
2905 sk_msg_iter_prev(msg, end);
2908 static void sk_msg_shift_right(struct sk_msg *msg, int i)
2910 struct scatterlist tmp, sge;
2912 sk_msg_iter_next(msg, end);
2913 sge = sk_msg_elem_cpy(msg, i);
2914 sk_msg_iter_var_next(i);
2915 tmp = sk_msg_elem_cpy(msg, i);
2917 while (i != msg->sg.end) {
2918 msg->sg.data[i] = sge;
2919 sk_msg_iter_var_next(i);
2921 tmp = sk_msg_elem_cpy(msg, i);
2925 BPF_CALL_4(bpf_msg_pop_data, struct sk_msg *, msg, u32, start,
2926 u32, len, u64, flags)
2928 u32 i = 0, l = 0, space, offset = 0;
2929 u64 last = start + len;
2932 if (unlikely(flags))
2935 /* First find the starting scatterlist element */
2939 l = sk_msg_elem(msg, i)->length;
2941 if (start < offset + l)
2943 sk_msg_iter_var_next(i);
2944 } while (i != msg->sg.end);
2946 /* Bounds checks: start and pop must be inside message */
2947 if (start >= offset + l || last >= msg->sg.size)
2950 space = MAX_MSG_FRAGS - sk_msg_elem_used(msg);
2953 /* --------------| offset
2954 * -| start |-------- len -------|
2956 * |----- a ----|-------- pop -------|----- b ----|
2957 * |______________________________________________| length
2960 * a: region at front of scatter element to save
2961 * b: region at back of scatter element to save when length > A + pop
2962 * pop: region to pop from element, same as input 'pop' here will be
2963 * decremented below per iteration.
2965 * Two top-level cases to handle when start != offset, first B is non
2966 * zero and second B is zero corresponding to when a pop includes more
2969 * Then if B is non-zero AND there is no space allocate space and
2970 * compact A, B regions into page. If there is space shift ring to
2971 * the rigth free'ing the next element in ring to place B, leaving
2972 * A untouched except to reduce length.
2974 if (start != offset) {
2975 struct scatterlist *nsge, *sge = sk_msg_elem(msg, i);
2977 int b = sge->length - pop - a;
2979 sk_msg_iter_var_next(i);
2981 if (pop < sge->length - a) {
2984 sk_msg_shift_right(msg, i);
2985 nsge = sk_msg_elem(msg, i);
2986 get_page(sg_page(sge));
2989 b, sge->offset + pop + a);
2991 struct page *page, *orig;
2994 page = alloc_pages(__GFP_NOWARN |
2995 __GFP_COMP | GFP_ATOMIC,
2997 if (unlikely(!page))
3001 orig = sg_page(sge);
3002 from = sg_virt(sge);
3003 to = page_address(page);
3004 memcpy(to, from, a);
3005 memcpy(to + a, from + a + pop, b);
3006 sg_set_page(sge, page, a + b, 0);
3010 } else if (pop >= sge->length - a) {
3011 pop -= (sge->length - a);
3016 /* From above the current layout _must_ be as follows,
3021 * |---- pop ---|---------------- b ------------|
3022 * |____________________________________________| length
3024 * Offset and start of the current msg elem are equal because in the
3025 * previous case we handled offset != start and either consumed the
3026 * entire element and advanced to the next element OR pop == 0.
3028 * Two cases to handle here are first pop is less than the length
3029 * leaving some remainder b above. Simply adjust the element's layout
3030 * in this case. Or pop >= length of the element so that b = 0. In this
3031 * case advance to next element decrementing pop.
3034 struct scatterlist *sge = sk_msg_elem(msg, i);
3036 if (pop < sge->length) {
3042 sk_msg_shift_left(msg, i);
3044 sk_msg_iter_var_next(i);
3047 sk_mem_uncharge(msg->sk, len - pop);
3048 msg->sg.size -= (len - pop);
3049 sk_msg_reset_curr(msg);
3050 sk_msg_compute_data_pointers(msg);
3054 static const struct bpf_func_proto bpf_msg_pop_data_proto = {
3055 .func = bpf_msg_pop_data,
3057 .ret_type = RET_INTEGER,
3058 .arg1_type = ARG_PTR_TO_CTX,
3059 .arg2_type = ARG_ANYTHING,
3060 .arg3_type = ARG_ANYTHING,
3061 .arg4_type = ARG_ANYTHING,
3064 #ifdef CONFIG_CGROUP_NET_CLASSID
3065 BPF_CALL_0(bpf_get_cgroup_classid_curr)
3067 return __task_get_classid(current);
3070 const struct bpf_func_proto bpf_get_cgroup_classid_curr_proto = {
3071 .func = bpf_get_cgroup_classid_curr,
3073 .ret_type = RET_INTEGER,
3076 BPF_CALL_1(bpf_skb_cgroup_classid, const struct sk_buff *, skb)
3078 struct sock *sk = skb_to_full_sk(skb);
3080 if (!sk || !sk_fullsock(sk))
3083 return sock_cgroup_classid(&sk->sk_cgrp_data);
3086 static const struct bpf_func_proto bpf_skb_cgroup_classid_proto = {
3087 .func = bpf_skb_cgroup_classid,
3089 .ret_type = RET_INTEGER,
3090 .arg1_type = ARG_PTR_TO_CTX,
3094 BPF_CALL_1(bpf_get_cgroup_classid, const struct sk_buff *, skb)
3096 return task_get_classid(skb);
3099 static const struct bpf_func_proto bpf_get_cgroup_classid_proto = {
3100 .func = bpf_get_cgroup_classid,
3102 .ret_type = RET_INTEGER,
3103 .arg1_type = ARG_PTR_TO_CTX,
3106 BPF_CALL_1(bpf_get_route_realm, const struct sk_buff *, skb)
3108 return dst_tclassid(skb);
3111 static const struct bpf_func_proto bpf_get_route_realm_proto = {
3112 .func = bpf_get_route_realm,
3114 .ret_type = RET_INTEGER,
3115 .arg1_type = ARG_PTR_TO_CTX,
3118 BPF_CALL_1(bpf_get_hash_recalc, struct sk_buff *, skb)
3120 /* If skb_clear_hash() was called due to mangling, we can
3121 * trigger SW recalculation here. Later access to hash
3122 * can then use the inline skb->hash via context directly
3123 * instead of calling this helper again.
3125 return skb_get_hash(skb);
3128 static const struct bpf_func_proto bpf_get_hash_recalc_proto = {
3129 .func = bpf_get_hash_recalc,
3131 .ret_type = RET_INTEGER,
3132 .arg1_type = ARG_PTR_TO_CTX,
3135 BPF_CALL_1(bpf_set_hash_invalid, struct sk_buff *, skb)
3137 /* After all direct packet write, this can be used once for
3138 * triggering a lazy recalc on next skb_get_hash() invocation.
3140 skb_clear_hash(skb);
3144 static const struct bpf_func_proto bpf_set_hash_invalid_proto = {
3145 .func = bpf_set_hash_invalid,
3147 .ret_type = RET_INTEGER,
3148 .arg1_type = ARG_PTR_TO_CTX,
3151 BPF_CALL_2(bpf_set_hash, struct sk_buff *, skb, u32, hash)
3153 /* Set user specified hash as L4(+), so that it gets returned
3154 * on skb_get_hash() call unless BPF prog later on triggers a
3157 __skb_set_sw_hash(skb, hash, true);
3161 static const struct bpf_func_proto bpf_set_hash_proto = {
3162 .func = bpf_set_hash,
3164 .ret_type = RET_INTEGER,
3165 .arg1_type = ARG_PTR_TO_CTX,
3166 .arg2_type = ARG_ANYTHING,
3169 BPF_CALL_3(bpf_skb_vlan_push, struct sk_buff *, skb, __be16, vlan_proto,
3174 if (unlikely(vlan_proto != htons(ETH_P_8021Q) &&
3175 vlan_proto != htons(ETH_P_8021AD)))
3176 vlan_proto = htons(ETH_P_8021Q);
3178 bpf_push_mac_rcsum(skb);
3179 ret = skb_vlan_push(skb, vlan_proto, vlan_tci);
3180 bpf_pull_mac_rcsum(skb);
3182 bpf_compute_data_pointers(skb);
3186 static const struct bpf_func_proto bpf_skb_vlan_push_proto = {
3187 .func = bpf_skb_vlan_push,
3189 .ret_type = RET_INTEGER,
3190 .arg1_type = ARG_PTR_TO_CTX,
3191 .arg2_type = ARG_ANYTHING,
3192 .arg3_type = ARG_ANYTHING,
3195 BPF_CALL_1(bpf_skb_vlan_pop, struct sk_buff *, skb)
3199 bpf_push_mac_rcsum(skb);
3200 ret = skb_vlan_pop(skb);
3201 bpf_pull_mac_rcsum(skb);
3203 bpf_compute_data_pointers(skb);
3207 static const struct bpf_func_proto bpf_skb_vlan_pop_proto = {
3208 .func = bpf_skb_vlan_pop,
3210 .ret_type = RET_INTEGER,
3211 .arg1_type = ARG_PTR_TO_CTX,
3214 static int bpf_skb_generic_push(struct sk_buff *skb, u32 off, u32 len)
3216 /* Caller already did skb_cow() with len as headroom,
3217 * so no need to do it here.
3220 memmove(skb->data, skb->data + len, off);
3221 memset(skb->data + off, 0, len);
3223 /* No skb_postpush_rcsum(skb, skb->data + off, len)
3224 * needed here as it does not change the skb->csum
3225 * result for checksum complete when summing over
3231 static int bpf_skb_generic_pop(struct sk_buff *skb, u32 off, u32 len)
3235 /* skb_ensure_writable() is not needed here, as we're
3236 * already working on an uncloned skb.
3238 if (unlikely(!pskb_may_pull(skb, off + len)))
3241 old_data = skb->data;
3242 __skb_pull(skb, len);
3243 skb_postpull_rcsum(skb, old_data + off, len);
3244 memmove(skb->data, old_data, off);
3249 static int bpf_skb_net_hdr_push(struct sk_buff *skb, u32 off, u32 len)
3251 bool trans_same = skb->transport_header == skb->network_header;
3254 /* There's no need for __skb_push()/__skb_pull() pair to
3255 * get to the start of the mac header as we're guaranteed
3256 * to always start from here under eBPF.
3258 ret = bpf_skb_generic_push(skb, off, len);
3260 skb->mac_header -= len;
3261 skb->network_header -= len;
3263 skb->transport_header = skb->network_header;
3269 static int bpf_skb_net_hdr_pop(struct sk_buff *skb, u32 off, u32 len)
3271 bool trans_same = skb->transport_header == skb->network_header;
3274 /* Same here, __skb_push()/__skb_pull() pair not needed. */
3275 ret = bpf_skb_generic_pop(skb, off, len);
3277 skb->mac_header += len;
3278 skb->network_header += len;
3280 skb->transport_header = skb->network_header;
3286 static int bpf_skb_proto_4_to_6(struct sk_buff *skb)
3288 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
3289 u32 off = skb_mac_header_len(skb);
3292 ret = skb_cow(skb, len_diff);
3293 if (unlikely(ret < 0))
3296 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
3297 if (unlikely(ret < 0))
3300 if (skb_is_gso(skb)) {
3301 struct skb_shared_info *shinfo = skb_shinfo(skb);
3303 /* SKB_GSO_TCPV4 needs to be changed into SKB_GSO_TCPV6. */
3304 if (shinfo->gso_type & SKB_GSO_TCPV4) {
3305 shinfo->gso_type &= ~SKB_GSO_TCPV4;
3306 shinfo->gso_type |= SKB_GSO_TCPV6;
3310 skb->protocol = htons(ETH_P_IPV6);
3311 skb_clear_hash(skb);
3316 static int bpf_skb_proto_6_to_4(struct sk_buff *skb)
3318 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
3319 u32 off = skb_mac_header_len(skb);
3322 ret = skb_unclone(skb, GFP_ATOMIC);
3323 if (unlikely(ret < 0))
3326 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
3327 if (unlikely(ret < 0))
3330 if (skb_is_gso(skb)) {
3331 struct skb_shared_info *shinfo = skb_shinfo(skb);
3333 /* SKB_GSO_TCPV6 needs to be changed into SKB_GSO_TCPV4. */
3334 if (shinfo->gso_type & SKB_GSO_TCPV6) {
3335 shinfo->gso_type &= ~SKB_GSO_TCPV6;
3336 shinfo->gso_type |= SKB_GSO_TCPV4;
3340 skb->protocol = htons(ETH_P_IP);
3341 skb_clear_hash(skb);
3346 static int bpf_skb_proto_xlat(struct sk_buff *skb, __be16 to_proto)
3348 __be16 from_proto = skb->protocol;
3350 if (from_proto == htons(ETH_P_IP) &&
3351 to_proto == htons(ETH_P_IPV6))
3352 return bpf_skb_proto_4_to_6(skb);
3354 if (from_proto == htons(ETH_P_IPV6) &&
3355 to_proto == htons(ETH_P_IP))
3356 return bpf_skb_proto_6_to_4(skb);
3361 BPF_CALL_3(bpf_skb_change_proto, struct sk_buff *, skb, __be16, proto,
3366 if (unlikely(flags))
3369 /* General idea is that this helper does the basic groundwork
3370 * needed for changing the protocol, and eBPF program fills the
3371 * rest through bpf_skb_store_bytes(), bpf_lX_csum_replace()
3372 * and other helpers, rather than passing a raw buffer here.
3374 * The rationale is to keep this minimal and without a need to
3375 * deal with raw packet data. F.e. even if we would pass buffers
3376 * here, the program still needs to call the bpf_lX_csum_replace()
3377 * helpers anyway. Plus, this way we keep also separation of
3378 * concerns, since f.e. bpf_skb_store_bytes() should only take
3381 * Currently, additional options and extension header space are
3382 * not supported, but flags register is reserved so we can adapt
3383 * that. For offloads, we mark packet as dodgy, so that headers
3384 * need to be verified first.
3386 ret = bpf_skb_proto_xlat(skb, proto);
3387 bpf_compute_data_pointers(skb);
3391 static const struct bpf_func_proto bpf_skb_change_proto_proto = {
3392 .func = bpf_skb_change_proto,
3394 .ret_type = RET_INTEGER,
3395 .arg1_type = ARG_PTR_TO_CTX,
3396 .arg2_type = ARG_ANYTHING,
3397 .arg3_type = ARG_ANYTHING,
3400 BPF_CALL_2(bpf_skb_change_type, struct sk_buff *, skb, u32, pkt_type)
3402 /* We only allow a restricted subset to be changed for now. */
3403 if (unlikely(!skb_pkt_type_ok(skb->pkt_type) ||
3404 !skb_pkt_type_ok(pkt_type)))
3407 skb->pkt_type = pkt_type;
3411 static const struct bpf_func_proto bpf_skb_change_type_proto = {
3412 .func = bpf_skb_change_type,
3414 .ret_type = RET_INTEGER,
3415 .arg1_type = ARG_PTR_TO_CTX,
3416 .arg2_type = ARG_ANYTHING,
3419 static u32 bpf_skb_net_base_len(const struct sk_buff *skb)
3421 switch (skb->protocol) {
3422 case htons(ETH_P_IP):
3423 return sizeof(struct iphdr);
3424 case htons(ETH_P_IPV6):
3425 return sizeof(struct ipv6hdr);
3431 #define BPF_F_ADJ_ROOM_ENCAP_L3_MASK (BPF_F_ADJ_ROOM_ENCAP_L3_IPV4 | \
3432 BPF_F_ADJ_ROOM_ENCAP_L3_IPV6)
3434 #define BPF_F_ADJ_ROOM_DECAP_L3_MASK (BPF_F_ADJ_ROOM_DECAP_L3_IPV4 | \
3435 BPF_F_ADJ_ROOM_DECAP_L3_IPV6)
3437 #define BPF_F_ADJ_ROOM_MASK (BPF_F_ADJ_ROOM_FIXED_GSO | \
3438 BPF_F_ADJ_ROOM_ENCAP_L3_MASK | \
3439 BPF_F_ADJ_ROOM_ENCAP_L4_GRE | \
3440 BPF_F_ADJ_ROOM_ENCAP_L4_UDP | \
3441 BPF_F_ADJ_ROOM_ENCAP_L2_ETH | \
3442 BPF_F_ADJ_ROOM_ENCAP_L2( \
3443 BPF_ADJ_ROOM_ENCAP_L2_MASK) | \
3444 BPF_F_ADJ_ROOM_DECAP_L3_MASK)
3446 static int bpf_skb_net_grow(struct sk_buff *skb, u32 off, u32 len_diff,
3449 u8 inner_mac_len = flags >> BPF_ADJ_ROOM_ENCAP_L2_SHIFT;
3450 bool encap = flags & BPF_F_ADJ_ROOM_ENCAP_L3_MASK;
3451 u16 mac_len = 0, inner_net = 0, inner_trans = 0;
3452 unsigned int gso_type = SKB_GSO_DODGY;
3455 if (skb_is_gso(skb) && !skb_is_gso_tcp(skb)) {
3456 /* udp gso_size delineates datagrams, only allow if fixed */
3457 if (!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) ||
3458 !(flags & BPF_F_ADJ_ROOM_FIXED_GSO))
3462 ret = skb_cow_head(skb, len_diff);
3463 if (unlikely(ret < 0))
3467 if (skb->protocol != htons(ETH_P_IP) &&
3468 skb->protocol != htons(ETH_P_IPV6))
3471 if (flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV4 &&
3472 flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV6)
3475 if (flags & BPF_F_ADJ_ROOM_ENCAP_L4_GRE &&
3476 flags & BPF_F_ADJ_ROOM_ENCAP_L4_UDP)
3479 if (flags & BPF_F_ADJ_ROOM_ENCAP_L2_ETH &&
3480 inner_mac_len < ETH_HLEN)
3483 if (skb->encapsulation)
3486 mac_len = skb->network_header - skb->mac_header;
3487 inner_net = skb->network_header;
3488 if (inner_mac_len > len_diff)
3490 inner_trans = skb->transport_header;
3493 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
3494 if (unlikely(ret < 0))
3498 skb->inner_mac_header = inner_net - inner_mac_len;
3499 skb->inner_network_header = inner_net;
3500 skb->inner_transport_header = inner_trans;
3502 if (flags & BPF_F_ADJ_ROOM_ENCAP_L2_ETH)
3503 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
3505 skb_set_inner_protocol(skb, skb->protocol);
3507 skb->encapsulation = 1;
3508 skb_set_network_header(skb, mac_len);
3510 if (flags & BPF_F_ADJ_ROOM_ENCAP_L4_UDP)
3511 gso_type |= SKB_GSO_UDP_TUNNEL;
3512 else if (flags & BPF_F_ADJ_ROOM_ENCAP_L4_GRE)
3513 gso_type |= SKB_GSO_GRE;
3514 else if (flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV6)
3515 gso_type |= SKB_GSO_IPXIP6;
3516 else if (flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV4)
3517 gso_type |= SKB_GSO_IPXIP4;
3519 if (flags & BPF_F_ADJ_ROOM_ENCAP_L4_GRE ||
3520 flags & BPF_F_ADJ_ROOM_ENCAP_L4_UDP) {
3521 int nh_len = flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV6 ?
3522 sizeof(struct ipv6hdr) :
3523 sizeof(struct iphdr);
3525 skb_set_transport_header(skb, mac_len + nh_len);
3528 /* Match skb->protocol to new outer l3 protocol */
3529 if (skb->protocol == htons(ETH_P_IP) &&
3530 flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV6)
3531 skb->protocol = htons(ETH_P_IPV6);
3532 else if (skb->protocol == htons(ETH_P_IPV6) &&
3533 flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV4)
3534 skb->protocol = htons(ETH_P_IP);
3537 if (skb_is_gso(skb)) {
3538 struct skb_shared_info *shinfo = skb_shinfo(skb);
3540 /* Due to header grow, MSS needs to be downgraded. */
3541 if (!(flags & BPF_F_ADJ_ROOM_FIXED_GSO))
3542 skb_decrease_gso_size(shinfo, len_diff);
3544 /* Header must be checked, and gso_segs recomputed. */
3545 shinfo->gso_type |= gso_type;
3546 shinfo->gso_segs = 0;
3552 static int bpf_skb_net_shrink(struct sk_buff *skb, u32 off, u32 len_diff,
3557 if (unlikely(flags & ~(BPF_F_ADJ_ROOM_FIXED_GSO |
3558 BPF_F_ADJ_ROOM_DECAP_L3_MASK |
3559 BPF_F_ADJ_ROOM_NO_CSUM_RESET)))
3562 if (skb_is_gso(skb) && !skb_is_gso_tcp(skb)) {
3563 /* udp gso_size delineates datagrams, only allow if fixed */
3564 if (!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) ||
3565 !(flags & BPF_F_ADJ_ROOM_FIXED_GSO))
3569 ret = skb_unclone(skb, GFP_ATOMIC);
3570 if (unlikely(ret < 0))
3573 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
3574 if (unlikely(ret < 0))
3577 /* Match skb->protocol to new outer l3 protocol */
3578 if (skb->protocol == htons(ETH_P_IP) &&
3579 flags & BPF_F_ADJ_ROOM_DECAP_L3_IPV6)
3580 skb->protocol = htons(ETH_P_IPV6);
3581 else if (skb->protocol == htons(ETH_P_IPV6) &&
3582 flags & BPF_F_ADJ_ROOM_DECAP_L3_IPV4)
3583 skb->protocol = htons(ETH_P_IP);
3585 if (skb_is_gso(skb)) {
3586 struct skb_shared_info *shinfo = skb_shinfo(skb);
3588 /* Due to header shrink, MSS can be upgraded. */
3589 if (!(flags & BPF_F_ADJ_ROOM_FIXED_GSO))
3590 skb_increase_gso_size(shinfo, len_diff);
3592 /* Header must be checked, and gso_segs recomputed. */
3593 shinfo->gso_type |= SKB_GSO_DODGY;
3594 shinfo->gso_segs = 0;
3600 #define BPF_SKB_MAX_LEN SKB_MAX_ALLOC
3602 BPF_CALL_4(sk_skb_adjust_room, struct sk_buff *, skb, s32, len_diff,
3603 u32, mode, u64, flags)
3605 u32 len_diff_abs = abs(len_diff);
3606 bool shrink = len_diff < 0;
3609 if (unlikely(flags || mode))
3611 if (unlikely(len_diff_abs > 0xfffU))
3615 ret = skb_cow(skb, len_diff);
3616 if (unlikely(ret < 0))
3618 __skb_push(skb, len_diff_abs);
3619 memset(skb->data, 0, len_diff_abs);
3621 if (unlikely(!pskb_may_pull(skb, len_diff_abs)))
3623 __skb_pull(skb, len_diff_abs);
3625 if (tls_sw_has_ctx_rx(skb->sk)) {
3626 struct strp_msg *rxm = strp_msg(skb);
3628 rxm->full_len += len_diff;
3633 static const struct bpf_func_proto sk_skb_adjust_room_proto = {
3634 .func = sk_skb_adjust_room,
3636 .ret_type = RET_INTEGER,
3637 .arg1_type = ARG_PTR_TO_CTX,
3638 .arg2_type = ARG_ANYTHING,
3639 .arg3_type = ARG_ANYTHING,
3640 .arg4_type = ARG_ANYTHING,
3643 BPF_CALL_4(bpf_skb_adjust_room, struct sk_buff *, skb, s32, len_diff,
3644 u32, mode, u64, flags)
3646 u32 len_cur, len_diff_abs = abs(len_diff);
3647 u32 len_min = bpf_skb_net_base_len(skb);
3648 u32 len_max = BPF_SKB_MAX_LEN;
3649 __be16 proto = skb->protocol;
3650 bool shrink = len_diff < 0;
3654 if (unlikely(flags & ~(BPF_F_ADJ_ROOM_MASK |
3655 BPF_F_ADJ_ROOM_NO_CSUM_RESET)))
3657 if (unlikely(len_diff_abs > 0xfffU))
3659 if (unlikely(proto != htons(ETH_P_IP) &&
3660 proto != htons(ETH_P_IPV6)))
3663 off = skb_mac_header_len(skb);
3665 case BPF_ADJ_ROOM_NET:
3666 off += bpf_skb_net_base_len(skb);
3668 case BPF_ADJ_ROOM_MAC:
3674 if (flags & BPF_F_ADJ_ROOM_DECAP_L3_MASK) {
3678 switch (flags & BPF_F_ADJ_ROOM_DECAP_L3_MASK) {
3679 case BPF_F_ADJ_ROOM_DECAP_L3_IPV4:
3680 len_min = sizeof(struct iphdr);
3682 case BPF_F_ADJ_ROOM_DECAP_L3_IPV6:
3683 len_min = sizeof(struct ipv6hdr);
3690 len_cur = skb->len - skb_network_offset(skb);
3691 if ((shrink && (len_diff_abs >= len_cur ||
3692 len_cur - len_diff_abs < len_min)) ||
3693 (!shrink && (skb->len + len_diff_abs > len_max &&
3697 ret = shrink ? bpf_skb_net_shrink(skb, off, len_diff_abs, flags) :
3698 bpf_skb_net_grow(skb, off, len_diff_abs, flags);
3699 if (!ret && !(flags & BPF_F_ADJ_ROOM_NO_CSUM_RESET))
3700 __skb_reset_checksum_unnecessary(skb);
3702 bpf_compute_data_pointers(skb);
3706 static const struct bpf_func_proto bpf_skb_adjust_room_proto = {
3707 .func = bpf_skb_adjust_room,
3709 .ret_type = RET_INTEGER,
3710 .arg1_type = ARG_PTR_TO_CTX,
3711 .arg2_type = ARG_ANYTHING,
3712 .arg3_type = ARG_ANYTHING,
3713 .arg4_type = ARG_ANYTHING,
3716 static u32 __bpf_skb_min_len(const struct sk_buff *skb)
3718 u32 min_len = skb_network_offset(skb);
3720 if (skb_transport_header_was_set(skb))
3721 min_len = skb_transport_offset(skb);
3722 if (skb->ip_summed == CHECKSUM_PARTIAL)
3723 min_len = skb_checksum_start_offset(skb) +
3724 skb->csum_offset + sizeof(__sum16);
3728 static int bpf_skb_grow_rcsum(struct sk_buff *skb, unsigned int new_len)
3730 unsigned int old_len = skb->len;
3733 ret = __skb_grow_rcsum(skb, new_len);
3735 memset(skb->data + old_len, 0, new_len - old_len);
3739 static int bpf_skb_trim_rcsum(struct sk_buff *skb, unsigned int new_len)
3741 return __skb_trim_rcsum(skb, new_len);
3744 static inline int __bpf_skb_change_tail(struct sk_buff *skb, u32 new_len,
3747 u32 max_len = BPF_SKB_MAX_LEN;
3748 u32 min_len = __bpf_skb_min_len(skb);
3751 if (unlikely(flags || new_len > max_len || new_len < min_len))
3753 if (skb->encapsulation)
3756 /* The basic idea of this helper is that it's performing the
3757 * needed work to either grow or trim an skb, and eBPF program
3758 * rewrites the rest via helpers like bpf_skb_store_bytes(),
3759 * bpf_lX_csum_replace() and others rather than passing a raw
3760 * buffer here. This one is a slow path helper and intended
3761 * for replies with control messages.
3763 * Like in bpf_skb_change_proto(), we want to keep this rather
3764 * minimal and without protocol specifics so that we are able
3765 * to separate concerns as in bpf_skb_store_bytes() should only
3766 * be the one responsible for writing buffers.
3768 * It's really expected to be a slow path operation here for
3769 * control message replies, so we're implicitly linearizing,
3770 * uncloning and drop offloads from the skb by this.
3772 ret = __bpf_try_make_writable(skb, skb->len);
3774 if (new_len > skb->len)
3775 ret = bpf_skb_grow_rcsum(skb, new_len);
3776 else if (new_len < skb->len)
3777 ret = bpf_skb_trim_rcsum(skb, new_len);
3778 if (!ret && skb_is_gso(skb))
3784 BPF_CALL_3(bpf_skb_change_tail, struct sk_buff *, skb, u32, new_len,
3787 int ret = __bpf_skb_change_tail(skb, new_len, flags);
3789 bpf_compute_data_pointers(skb);
3793 static const struct bpf_func_proto bpf_skb_change_tail_proto = {
3794 .func = bpf_skb_change_tail,
3796 .ret_type = RET_INTEGER,
3797 .arg1_type = ARG_PTR_TO_CTX,
3798 .arg2_type = ARG_ANYTHING,
3799 .arg3_type = ARG_ANYTHING,
3802 BPF_CALL_3(sk_skb_change_tail, struct sk_buff *, skb, u32, new_len,
3805 return __bpf_skb_change_tail(skb, new_len, flags);
3808 static const struct bpf_func_proto sk_skb_change_tail_proto = {
3809 .func = sk_skb_change_tail,
3811 .ret_type = RET_INTEGER,
3812 .arg1_type = ARG_PTR_TO_CTX,
3813 .arg2_type = ARG_ANYTHING,
3814 .arg3_type = ARG_ANYTHING,
3817 static inline int __bpf_skb_change_head(struct sk_buff *skb, u32 head_room,
3820 u32 max_len = BPF_SKB_MAX_LEN;
3821 u32 new_len = skb->len + head_room;
3824 if (unlikely(flags || (!skb_is_gso(skb) && new_len > max_len) ||
3825 new_len < skb->len))
3828 ret = skb_cow(skb, head_room);
3830 /* Idea for this helper is that we currently only
3831 * allow to expand on mac header. This means that
3832 * skb->protocol network header, etc, stay as is.
3833 * Compared to bpf_skb_change_tail(), we're more
3834 * flexible due to not needing to linearize or
3835 * reset GSO. Intention for this helper is to be
3836 * used by an L3 skb that needs to push mac header
3837 * for redirection into L2 device.
3839 __skb_push(skb, head_room);
3840 memset(skb->data, 0, head_room);
3841 skb_reset_mac_header(skb);
3842 skb_reset_mac_len(skb);
3848 BPF_CALL_3(bpf_skb_change_head, struct sk_buff *, skb, u32, head_room,
3851 int ret = __bpf_skb_change_head(skb, head_room, flags);
3853 bpf_compute_data_pointers(skb);
3857 static const struct bpf_func_proto bpf_skb_change_head_proto = {
3858 .func = bpf_skb_change_head,
3860 .ret_type = RET_INTEGER,
3861 .arg1_type = ARG_PTR_TO_CTX,
3862 .arg2_type = ARG_ANYTHING,
3863 .arg3_type = ARG_ANYTHING,
3866 BPF_CALL_3(sk_skb_change_head, struct sk_buff *, skb, u32, head_room,
3869 return __bpf_skb_change_head(skb, head_room, flags);
3872 static const struct bpf_func_proto sk_skb_change_head_proto = {
3873 .func = sk_skb_change_head,
3875 .ret_type = RET_INTEGER,
3876 .arg1_type = ARG_PTR_TO_CTX,
3877 .arg2_type = ARG_ANYTHING,
3878 .arg3_type = ARG_ANYTHING,
3881 BPF_CALL_1(bpf_xdp_get_buff_len, struct xdp_buff*, xdp)
3883 return xdp_get_buff_len(xdp);
3886 static const struct bpf_func_proto bpf_xdp_get_buff_len_proto = {
3887 .func = bpf_xdp_get_buff_len,
3889 .ret_type = RET_INTEGER,
3890 .arg1_type = ARG_PTR_TO_CTX,
3893 BTF_ID_LIST_SINGLE(bpf_xdp_get_buff_len_bpf_ids, struct, xdp_buff)
3895 const struct bpf_func_proto bpf_xdp_get_buff_len_trace_proto = {
3896 .func = bpf_xdp_get_buff_len,
3898 .arg1_type = ARG_PTR_TO_BTF_ID,
3899 .arg1_btf_id = &bpf_xdp_get_buff_len_bpf_ids[0],
3902 static unsigned long xdp_get_metalen(const struct xdp_buff *xdp)
3904 return xdp_data_meta_unsupported(xdp) ? 0 :
3905 xdp->data - xdp->data_meta;
3908 BPF_CALL_2(bpf_xdp_adjust_head, struct xdp_buff *, xdp, int, offset)
3910 void *xdp_frame_end = xdp->data_hard_start + sizeof(struct xdp_frame);
3911 unsigned long metalen = xdp_get_metalen(xdp);
3912 void *data_start = xdp_frame_end + metalen;
3913 void *data = xdp->data + offset;
3915 if (unlikely(data < data_start ||
3916 data > xdp->data_end - ETH_HLEN))
3920 memmove(xdp->data_meta + offset,
3921 xdp->data_meta, metalen);
3922 xdp->data_meta += offset;
3928 static const struct bpf_func_proto bpf_xdp_adjust_head_proto = {
3929 .func = bpf_xdp_adjust_head,
3931 .ret_type = RET_INTEGER,
3932 .arg1_type = ARG_PTR_TO_CTX,
3933 .arg2_type = ARG_ANYTHING,
3936 void bpf_xdp_copy_buf(struct xdp_buff *xdp, unsigned long off,
3937 void *buf, unsigned long len, bool flush)
3939 unsigned long ptr_len, ptr_off = 0;
3940 skb_frag_t *next_frag, *end_frag;
3941 struct skb_shared_info *sinfo;
3945 if (likely(xdp->data_end - xdp->data >= off + len)) {
3946 src = flush ? buf : xdp->data + off;
3947 dst = flush ? xdp->data + off : buf;
3948 memcpy(dst, src, len);
3952 sinfo = xdp_get_shared_info_from_buff(xdp);
3953 end_frag = &sinfo->frags[sinfo->nr_frags];
3954 next_frag = &sinfo->frags[0];
3956 ptr_len = xdp->data_end - xdp->data;
3957 ptr_buf = xdp->data;
3960 if (off < ptr_off + ptr_len) {
3961 unsigned long copy_off = off - ptr_off;
3962 unsigned long copy_len = min(len, ptr_len - copy_off);
3964 src = flush ? buf : ptr_buf + copy_off;
3965 dst = flush ? ptr_buf + copy_off : buf;
3966 memcpy(dst, src, copy_len);
3973 if (!len || next_frag == end_frag)
3977 ptr_buf = skb_frag_address(next_frag);
3978 ptr_len = skb_frag_size(next_frag);
3983 void *bpf_xdp_pointer(struct xdp_buff *xdp, u32 offset, u32 len)
3985 u32 size = xdp->data_end - xdp->data;
3986 struct skb_shared_info *sinfo;
3987 void *addr = xdp->data;
3990 if (unlikely(offset > 0xffff || len > 0xffff))
3991 return ERR_PTR(-EFAULT);
3993 if (unlikely(offset + len > xdp_get_buff_len(xdp)))
3994 return ERR_PTR(-EINVAL);
3996 if (likely(offset < size)) /* linear area */
3999 sinfo = xdp_get_shared_info_from_buff(xdp);
4001 for (i = 0; i < sinfo->nr_frags; i++) { /* paged area */
4002 u32 frag_size = skb_frag_size(&sinfo->frags[i]);
4004 if (offset < frag_size) {
4005 addr = skb_frag_address(&sinfo->frags[i]);
4009 offset -= frag_size;
4012 return offset + len <= size ? addr + offset : NULL;
4015 BPF_CALL_4(bpf_xdp_load_bytes, struct xdp_buff *, xdp, u32, offset,
4016 void *, buf, u32, len)
4020 ptr = bpf_xdp_pointer(xdp, offset, len);
4022 return PTR_ERR(ptr);
4025 bpf_xdp_copy_buf(xdp, offset, buf, len, false);
4027 memcpy(buf, ptr, len);
4032 static const struct bpf_func_proto bpf_xdp_load_bytes_proto = {
4033 .func = bpf_xdp_load_bytes,
4035 .ret_type = RET_INTEGER,
4036 .arg1_type = ARG_PTR_TO_CTX,
4037 .arg2_type = ARG_ANYTHING,
4038 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
4039 .arg4_type = ARG_CONST_SIZE,
4042 int __bpf_xdp_load_bytes(struct xdp_buff *xdp, u32 offset, void *buf, u32 len)
4044 return ____bpf_xdp_load_bytes(xdp, offset, buf, len);
4047 BPF_CALL_4(bpf_xdp_store_bytes, struct xdp_buff *, xdp, u32, offset,
4048 void *, buf, u32, len)
4052 ptr = bpf_xdp_pointer(xdp, offset, len);
4054 return PTR_ERR(ptr);
4057 bpf_xdp_copy_buf(xdp, offset, buf, len, true);
4059 memcpy(ptr, buf, len);
4064 static const struct bpf_func_proto bpf_xdp_store_bytes_proto = {
4065 .func = bpf_xdp_store_bytes,
4067 .ret_type = RET_INTEGER,
4068 .arg1_type = ARG_PTR_TO_CTX,
4069 .arg2_type = ARG_ANYTHING,
4070 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
4071 .arg4_type = ARG_CONST_SIZE,
4074 int __bpf_xdp_store_bytes(struct xdp_buff *xdp, u32 offset, void *buf, u32 len)
4076 return ____bpf_xdp_store_bytes(xdp, offset, buf, len);
4079 static int bpf_xdp_frags_increase_tail(struct xdp_buff *xdp, int offset)
4081 struct skb_shared_info *sinfo = xdp_get_shared_info_from_buff(xdp);
4082 skb_frag_t *frag = &sinfo->frags[sinfo->nr_frags - 1];
4083 struct xdp_rxq_info *rxq = xdp->rxq;
4084 unsigned int tailroom;
4086 if (!rxq->frag_size || rxq->frag_size > xdp->frame_sz)
4089 tailroom = rxq->frag_size - skb_frag_size(frag) - skb_frag_off(frag);
4090 if (unlikely(offset > tailroom))
4093 memset(skb_frag_address(frag) + skb_frag_size(frag), 0, offset);
4094 skb_frag_size_add(frag, offset);
4095 sinfo->xdp_frags_size += offset;
4096 if (rxq->mem.type == MEM_TYPE_XSK_BUFF_POOL)
4097 xsk_buff_get_tail(xdp)->data_end += offset;
4102 static void bpf_xdp_shrink_data_zc(struct xdp_buff *xdp, int shrink,
4103 struct xdp_mem_info *mem_info, bool release)
4105 struct xdp_buff *zc_frag = xsk_buff_get_tail(xdp);
4108 xsk_buff_del_tail(zc_frag);
4109 __xdp_return(NULL, mem_info, false, zc_frag);
4111 zc_frag->data_end -= shrink;
4115 static bool bpf_xdp_shrink_data(struct xdp_buff *xdp, skb_frag_t *frag,
4118 struct xdp_mem_info *mem_info = &xdp->rxq->mem;
4119 bool release = skb_frag_size(frag) == shrink;
4121 if (mem_info->type == MEM_TYPE_XSK_BUFF_POOL) {
4122 bpf_xdp_shrink_data_zc(xdp, shrink, mem_info, release);
4127 struct page *page = skb_frag_page(frag);
4129 __xdp_return(page_address(page), mem_info, false, NULL);
4136 static int bpf_xdp_frags_shrink_tail(struct xdp_buff *xdp, int offset)
4138 struct skb_shared_info *sinfo = xdp_get_shared_info_from_buff(xdp);
4139 int i, n_frags_free = 0, len_free = 0;
4141 if (unlikely(offset > (int)xdp_get_buff_len(xdp) - ETH_HLEN))
4144 for (i = sinfo->nr_frags - 1; i >= 0 && offset > 0; i--) {
4145 skb_frag_t *frag = &sinfo->frags[i];
4146 int shrink = min_t(int, offset, skb_frag_size(frag));
4150 if (bpf_xdp_shrink_data(xdp, frag, shrink)) {
4153 skb_frag_size_sub(frag, shrink);
4157 sinfo->nr_frags -= n_frags_free;
4158 sinfo->xdp_frags_size -= len_free;
4160 if (unlikely(!sinfo->nr_frags)) {
4161 xdp_buff_clear_frags_flag(xdp);
4162 xdp->data_end -= offset;
4168 BPF_CALL_2(bpf_xdp_adjust_tail, struct xdp_buff *, xdp, int, offset)
4170 void *data_hard_end = xdp_data_hard_end(xdp); /* use xdp->frame_sz */
4171 void *data_end = xdp->data_end + offset;
4173 if (unlikely(xdp_buff_has_frags(xdp))) { /* non-linear xdp buff */
4175 return bpf_xdp_frags_shrink_tail(xdp, -offset);
4177 return bpf_xdp_frags_increase_tail(xdp, offset);
4180 /* Notice that xdp_data_hard_end have reserved some tailroom */
4181 if (unlikely(data_end > data_hard_end))
4184 if (unlikely(data_end < xdp->data + ETH_HLEN))
4187 /* Clear memory area on grow, can contain uninit kernel memory */
4189 memset(xdp->data_end, 0, offset);
4191 xdp->data_end = data_end;
4196 static const struct bpf_func_proto bpf_xdp_adjust_tail_proto = {
4197 .func = bpf_xdp_adjust_tail,
4199 .ret_type = RET_INTEGER,
4200 .arg1_type = ARG_PTR_TO_CTX,
4201 .arg2_type = ARG_ANYTHING,
4204 BPF_CALL_2(bpf_xdp_adjust_meta, struct xdp_buff *, xdp, int, offset)
4206 void *xdp_frame_end = xdp->data_hard_start + sizeof(struct xdp_frame);
4207 void *meta = xdp->data_meta + offset;
4208 unsigned long metalen = xdp->data - meta;
4210 if (xdp_data_meta_unsupported(xdp))
4212 if (unlikely(meta < xdp_frame_end ||
4215 if (unlikely(xdp_metalen_invalid(metalen)))
4218 xdp->data_meta = meta;
4223 static const struct bpf_func_proto bpf_xdp_adjust_meta_proto = {
4224 .func = bpf_xdp_adjust_meta,
4226 .ret_type = RET_INTEGER,
4227 .arg1_type = ARG_PTR_TO_CTX,
4228 .arg2_type = ARG_ANYTHING,
4234 * XDP_REDIRECT works by a three-step process, implemented in the functions
4237 * 1. The bpf_redirect() and bpf_redirect_map() helpers will lookup the target
4238 * of the redirect and store it (along with some other metadata) in a per-CPU
4239 * struct bpf_redirect_info.
4241 * 2. When the program returns the XDP_REDIRECT return code, the driver will
4242 * call xdp_do_redirect() which will use the information in struct
4243 * bpf_redirect_info to actually enqueue the frame into a map type-specific
4244 * bulk queue structure.
4246 * 3. Before exiting its NAPI poll loop, the driver will call
4247 * xdp_do_flush(), which will flush all the different bulk queues,
4248 * thus completing the redirect. Note that xdp_do_flush() must be
4249 * called before napi_complete_done() in the driver, as the
4250 * XDP_REDIRECT logic relies on being inside a single NAPI instance
4251 * through to the xdp_do_flush() call for RCU protection of all
4252 * in-kernel data structures.
4255 * Pointers to the map entries will be kept around for this whole sequence of
4256 * steps, protected by RCU. However, there is no top-level rcu_read_lock() in
4257 * the core code; instead, the RCU protection relies on everything happening
4258 * inside a single NAPI poll sequence, which means it's between a pair of calls
4259 * to local_bh_disable()/local_bh_enable().
4261 * The map entries are marked as __rcu and the map code makes sure to
4262 * dereference those pointers with rcu_dereference_check() in a way that works
4263 * for both sections that to hold an rcu_read_lock() and sections that are
4264 * called from NAPI without a separate rcu_read_lock(). The code below does not
4265 * use RCU annotations, but relies on those in the map code.
4267 void xdp_do_flush(void)
4273 EXPORT_SYMBOL_GPL(xdp_do_flush);
4275 #if defined(CONFIG_DEBUG_NET) && defined(CONFIG_BPF_SYSCALL)
4276 void xdp_do_check_flushed(struct napi_struct *napi)
4280 ret = dev_check_flush();
4281 ret |= cpu_map_check_flush();
4282 ret |= xsk_map_check_flush();
4284 WARN_ONCE(ret, "Missing xdp_do_flush() invocation after NAPI by %ps\n",
4289 void bpf_clear_redirect_map(struct bpf_map *map)
4291 struct bpf_redirect_info *ri;
4294 for_each_possible_cpu(cpu) {
4295 ri = per_cpu_ptr(&bpf_redirect_info, cpu);
4296 /* Avoid polluting remote cacheline due to writes if
4297 * not needed. Once we pass this test, we need the
4298 * cmpxchg() to make sure it hasn't been changed in
4299 * the meantime by remote CPU.
4301 if (unlikely(READ_ONCE(ri->map) == map))
4302 cmpxchg(&ri->map, map, NULL);
4306 DEFINE_STATIC_KEY_FALSE(bpf_master_redirect_enabled_key);
4307 EXPORT_SYMBOL_GPL(bpf_master_redirect_enabled_key);
4309 u32 xdp_master_redirect(struct xdp_buff *xdp)
4311 struct net_device *master, *slave;
4312 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
4314 master = netdev_master_upper_dev_get_rcu(xdp->rxq->dev);
4315 slave = master->netdev_ops->ndo_xdp_get_xmit_slave(master, xdp);
4316 if (slave && slave != xdp->rxq->dev) {
4317 /* The target device is different from the receiving device, so
4318 * redirect it to the new device.
4319 * Using XDP_REDIRECT gets the correct behaviour from XDP enabled
4320 * drivers to unmap the packet from their rx ring.
4322 ri->tgt_index = slave->ifindex;
4323 ri->map_id = INT_MAX;
4324 ri->map_type = BPF_MAP_TYPE_UNSPEC;
4325 return XDP_REDIRECT;
4329 EXPORT_SYMBOL_GPL(xdp_master_redirect);
4331 static inline int __xdp_do_redirect_xsk(struct bpf_redirect_info *ri,
4332 struct net_device *dev,
4333 struct xdp_buff *xdp,
4334 struct bpf_prog *xdp_prog)
4336 enum bpf_map_type map_type = ri->map_type;
4337 void *fwd = ri->tgt_value;
4338 u32 map_id = ri->map_id;
4341 ri->map_id = 0; /* Valid map id idr range: [1,INT_MAX[ */
4342 ri->map_type = BPF_MAP_TYPE_UNSPEC;
4344 err = __xsk_map_redirect(fwd, xdp);
4348 _trace_xdp_redirect_map(dev, xdp_prog, fwd, map_type, map_id, ri->tgt_index);
4351 _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map_type, map_id, ri->tgt_index, err);
4355 static __always_inline int __xdp_do_redirect_frame(struct bpf_redirect_info *ri,
4356 struct net_device *dev,
4357 struct xdp_frame *xdpf,
4358 struct bpf_prog *xdp_prog)
4360 enum bpf_map_type map_type = ri->map_type;
4361 void *fwd = ri->tgt_value;
4362 u32 map_id = ri->map_id;
4363 struct bpf_map *map;
4366 ri->map_id = 0; /* Valid map id idr range: [1,INT_MAX[ */
4367 ri->map_type = BPF_MAP_TYPE_UNSPEC;
4369 if (unlikely(!xdpf)) {
4375 case BPF_MAP_TYPE_DEVMAP:
4377 case BPF_MAP_TYPE_DEVMAP_HASH:
4378 map = READ_ONCE(ri->map);
4379 if (unlikely(map)) {
4380 WRITE_ONCE(ri->map, NULL);
4381 err = dev_map_enqueue_multi(xdpf, dev, map,
4382 ri->flags & BPF_F_EXCLUDE_INGRESS);
4384 err = dev_map_enqueue(fwd, xdpf, dev);
4387 case BPF_MAP_TYPE_CPUMAP:
4388 err = cpu_map_enqueue(fwd, xdpf, dev);
4390 case BPF_MAP_TYPE_UNSPEC:
4391 if (map_id == INT_MAX) {
4392 fwd = dev_get_by_index_rcu(dev_net(dev), ri->tgt_index);
4393 if (unlikely(!fwd)) {
4397 err = dev_xdp_enqueue(fwd, xdpf, dev);
4408 _trace_xdp_redirect_map(dev, xdp_prog, fwd, map_type, map_id, ri->tgt_index);
4411 _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map_type, map_id, ri->tgt_index, err);
4415 int xdp_do_redirect(struct net_device *dev, struct xdp_buff *xdp,
4416 struct bpf_prog *xdp_prog)
4418 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
4419 enum bpf_map_type map_type = ri->map_type;
4421 if (map_type == BPF_MAP_TYPE_XSKMAP)
4422 return __xdp_do_redirect_xsk(ri, dev, xdp, xdp_prog);
4424 return __xdp_do_redirect_frame(ri, dev, xdp_convert_buff_to_frame(xdp),
4427 EXPORT_SYMBOL_GPL(xdp_do_redirect);
4429 int xdp_do_redirect_frame(struct net_device *dev, struct xdp_buff *xdp,
4430 struct xdp_frame *xdpf, struct bpf_prog *xdp_prog)
4432 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
4433 enum bpf_map_type map_type = ri->map_type;
4435 if (map_type == BPF_MAP_TYPE_XSKMAP)
4436 return __xdp_do_redirect_xsk(ri, dev, xdp, xdp_prog);
4438 return __xdp_do_redirect_frame(ri, dev, xdpf, xdp_prog);
4440 EXPORT_SYMBOL_GPL(xdp_do_redirect_frame);
4442 static int xdp_do_generic_redirect_map(struct net_device *dev,
4443 struct sk_buff *skb,
4444 struct xdp_buff *xdp,
4445 struct bpf_prog *xdp_prog,
4447 enum bpf_map_type map_type, u32 map_id)
4449 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
4450 struct bpf_map *map;
4454 case BPF_MAP_TYPE_DEVMAP:
4456 case BPF_MAP_TYPE_DEVMAP_HASH:
4457 map = READ_ONCE(ri->map);
4458 if (unlikely(map)) {
4459 WRITE_ONCE(ri->map, NULL);
4460 err = dev_map_redirect_multi(dev, skb, xdp_prog, map,
4461 ri->flags & BPF_F_EXCLUDE_INGRESS);
4463 err = dev_map_generic_redirect(fwd, skb, xdp_prog);
4468 case BPF_MAP_TYPE_XSKMAP:
4469 err = xsk_generic_rcv(fwd, xdp);
4474 case BPF_MAP_TYPE_CPUMAP:
4475 err = cpu_map_generic_redirect(fwd, skb);
4484 _trace_xdp_redirect_map(dev, xdp_prog, fwd, map_type, map_id, ri->tgt_index);
4487 _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map_type, map_id, ri->tgt_index, err);
4491 int xdp_do_generic_redirect(struct net_device *dev, struct sk_buff *skb,
4492 struct xdp_buff *xdp, struct bpf_prog *xdp_prog)
4494 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
4495 enum bpf_map_type map_type = ri->map_type;
4496 void *fwd = ri->tgt_value;
4497 u32 map_id = ri->map_id;
4500 ri->map_id = 0; /* Valid map id idr range: [1,INT_MAX[ */
4501 ri->map_type = BPF_MAP_TYPE_UNSPEC;
4503 if (map_type == BPF_MAP_TYPE_UNSPEC && map_id == INT_MAX) {
4504 fwd = dev_get_by_index_rcu(dev_net(dev), ri->tgt_index);
4505 if (unlikely(!fwd)) {
4510 err = xdp_ok_fwd_dev(fwd, skb->len);
4515 _trace_xdp_redirect(dev, xdp_prog, ri->tgt_index);
4516 generic_xdp_tx(skb, xdp_prog);
4520 return xdp_do_generic_redirect_map(dev, skb, xdp, xdp_prog, fwd, map_type, map_id);
4522 _trace_xdp_redirect_err(dev, xdp_prog, ri->tgt_index, err);
4526 BPF_CALL_2(bpf_xdp_redirect, u32, ifindex, u64, flags)
4528 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
4530 if (unlikely(flags))
4533 /* NB! Map type UNSPEC and map_id == INT_MAX (never generated
4534 * by map_idr) is used for ifindex based XDP redirect.
4536 ri->tgt_index = ifindex;
4537 ri->map_id = INT_MAX;
4538 ri->map_type = BPF_MAP_TYPE_UNSPEC;
4540 return XDP_REDIRECT;
4543 static const struct bpf_func_proto bpf_xdp_redirect_proto = {
4544 .func = bpf_xdp_redirect,
4546 .ret_type = RET_INTEGER,
4547 .arg1_type = ARG_ANYTHING,
4548 .arg2_type = ARG_ANYTHING,
4551 BPF_CALL_3(bpf_xdp_redirect_map, struct bpf_map *, map, u64, key,
4554 return map->ops->map_redirect(map, key, flags);
4557 static const struct bpf_func_proto bpf_xdp_redirect_map_proto = {
4558 .func = bpf_xdp_redirect_map,
4560 .ret_type = RET_INTEGER,
4561 .arg1_type = ARG_CONST_MAP_PTR,
4562 .arg2_type = ARG_ANYTHING,
4563 .arg3_type = ARG_ANYTHING,
4566 static unsigned long bpf_skb_copy(void *dst_buff, const void *skb,
4567 unsigned long off, unsigned long len)
4569 void *ptr = skb_header_pointer(skb, off, len, dst_buff);
4573 if (ptr != dst_buff)
4574 memcpy(dst_buff, ptr, len);
4579 BPF_CALL_5(bpf_skb_event_output, struct sk_buff *, skb, struct bpf_map *, map,
4580 u64, flags, void *, meta, u64, meta_size)
4582 u64 skb_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
4584 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
4586 if (unlikely(!skb || skb_size > skb->len))
4589 return bpf_event_output(map, flags, meta, meta_size, skb, skb_size,
4593 static const struct bpf_func_proto bpf_skb_event_output_proto = {
4594 .func = bpf_skb_event_output,
4596 .ret_type = RET_INTEGER,
4597 .arg1_type = ARG_PTR_TO_CTX,
4598 .arg2_type = ARG_CONST_MAP_PTR,
4599 .arg3_type = ARG_ANYTHING,
4600 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
4601 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
4604 BTF_ID_LIST_SINGLE(bpf_skb_output_btf_ids, struct, sk_buff)
4606 const struct bpf_func_proto bpf_skb_output_proto = {
4607 .func = bpf_skb_event_output,
4609 .ret_type = RET_INTEGER,
4610 .arg1_type = ARG_PTR_TO_BTF_ID,
4611 .arg1_btf_id = &bpf_skb_output_btf_ids[0],
4612 .arg2_type = ARG_CONST_MAP_PTR,
4613 .arg3_type = ARG_ANYTHING,
4614 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
4615 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
4618 static unsigned short bpf_tunnel_key_af(u64 flags)
4620 return flags & BPF_F_TUNINFO_IPV6 ? AF_INET6 : AF_INET;
4623 BPF_CALL_4(bpf_skb_get_tunnel_key, struct sk_buff *, skb, struct bpf_tunnel_key *, to,
4624 u32, size, u64, flags)
4626 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
4627 u8 compat[sizeof(struct bpf_tunnel_key)];
4631 if (unlikely(!info || (flags & ~(BPF_F_TUNINFO_IPV6 |
4632 BPF_F_TUNINFO_FLAGS)))) {
4636 if (ip_tunnel_info_af(info) != bpf_tunnel_key_af(flags)) {
4640 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
4643 case offsetof(struct bpf_tunnel_key, local_ipv6[0]):
4644 case offsetof(struct bpf_tunnel_key, tunnel_label):
4645 case offsetof(struct bpf_tunnel_key, tunnel_ext):
4647 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
4648 /* Fixup deprecated structure layouts here, so we have
4649 * a common path later on.
4651 if (ip_tunnel_info_af(info) != AF_INET)
4654 to = (struct bpf_tunnel_key *)compat;
4661 to->tunnel_id = be64_to_cpu(info->key.tun_id);
4662 to->tunnel_tos = info->key.tos;
4663 to->tunnel_ttl = info->key.ttl;
4664 if (flags & BPF_F_TUNINFO_FLAGS)
4665 to->tunnel_flags = info->key.tun_flags;
4669 if (flags & BPF_F_TUNINFO_IPV6) {
4670 memcpy(to->remote_ipv6, &info->key.u.ipv6.src,
4671 sizeof(to->remote_ipv6));
4672 memcpy(to->local_ipv6, &info->key.u.ipv6.dst,
4673 sizeof(to->local_ipv6));
4674 to->tunnel_label = be32_to_cpu(info->key.label);
4676 to->remote_ipv4 = be32_to_cpu(info->key.u.ipv4.src);
4677 memset(&to->remote_ipv6[1], 0, sizeof(__u32) * 3);
4678 to->local_ipv4 = be32_to_cpu(info->key.u.ipv4.dst);
4679 memset(&to->local_ipv6[1], 0, sizeof(__u32) * 3);
4680 to->tunnel_label = 0;
4683 if (unlikely(size != sizeof(struct bpf_tunnel_key)))
4684 memcpy(to_orig, to, size);
4688 memset(to_orig, 0, size);
4692 static const struct bpf_func_proto bpf_skb_get_tunnel_key_proto = {
4693 .func = bpf_skb_get_tunnel_key,
4695 .ret_type = RET_INTEGER,
4696 .arg1_type = ARG_PTR_TO_CTX,
4697 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
4698 .arg3_type = ARG_CONST_SIZE,
4699 .arg4_type = ARG_ANYTHING,
4702 BPF_CALL_3(bpf_skb_get_tunnel_opt, struct sk_buff *, skb, u8 *, to, u32, size)
4704 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
4707 if (unlikely(!info ||
4708 !(info->key.tun_flags & TUNNEL_OPTIONS_PRESENT))) {
4712 if (unlikely(size < info->options_len)) {
4717 ip_tunnel_info_opts_get(to, info);
4718 if (size > info->options_len)
4719 memset(to + info->options_len, 0, size - info->options_len);
4721 return info->options_len;
4723 memset(to, 0, size);
4727 static const struct bpf_func_proto bpf_skb_get_tunnel_opt_proto = {
4728 .func = bpf_skb_get_tunnel_opt,
4730 .ret_type = RET_INTEGER,
4731 .arg1_type = ARG_PTR_TO_CTX,
4732 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
4733 .arg3_type = ARG_CONST_SIZE,
4736 static struct metadata_dst __percpu *md_dst;
4738 BPF_CALL_4(bpf_skb_set_tunnel_key, struct sk_buff *, skb,
4739 const struct bpf_tunnel_key *, from, u32, size, u64, flags)
4741 struct metadata_dst *md = this_cpu_ptr(md_dst);
4742 u8 compat[sizeof(struct bpf_tunnel_key)];
4743 struct ip_tunnel_info *info;
4745 if (unlikely(flags & ~(BPF_F_TUNINFO_IPV6 | BPF_F_ZERO_CSUM_TX |
4746 BPF_F_DONT_FRAGMENT | BPF_F_SEQ_NUMBER |
4747 BPF_F_NO_TUNNEL_KEY)))
4749 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
4751 case offsetof(struct bpf_tunnel_key, local_ipv6[0]):
4752 case offsetof(struct bpf_tunnel_key, tunnel_label):
4753 case offsetof(struct bpf_tunnel_key, tunnel_ext):
4754 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
4755 /* Fixup deprecated structure layouts here, so we have
4756 * a common path later on.
4758 memcpy(compat, from, size);
4759 memset(compat + size, 0, sizeof(compat) - size);
4760 from = (const struct bpf_tunnel_key *) compat;
4766 if (unlikely((!(flags & BPF_F_TUNINFO_IPV6) && from->tunnel_label) ||
4771 dst_hold((struct dst_entry *) md);
4772 skb_dst_set(skb, (struct dst_entry *) md);
4774 info = &md->u.tun_info;
4775 memset(info, 0, sizeof(*info));
4776 info->mode = IP_TUNNEL_INFO_TX;
4778 info->key.tun_flags = TUNNEL_KEY | TUNNEL_CSUM | TUNNEL_NOCACHE;
4779 if (flags & BPF_F_DONT_FRAGMENT)
4780 info->key.tun_flags |= TUNNEL_DONT_FRAGMENT;
4781 if (flags & BPF_F_ZERO_CSUM_TX)
4782 info->key.tun_flags &= ~TUNNEL_CSUM;
4783 if (flags & BPF_F_SEQ_NUMBER)
4784 info->key.tun_flags |= TUNNEL_SEQ;
4785 if (flags & BPF_F_NO_TUNNEL_KEY)
4786 info->key.tun_flags &= ~TUNNEL_KEY;
4788 info->key.tun_id = cpu_to_be64(from->tunnel_id);
4789 info->key.tos = from->tunnel_tos;
4790 info->key.ttl = from->tunnel_ttl;
4792 if (flags & BPF_F_TUNINFO_IPV6) {
4793 info->mode |= IP_TUNNEL_INFO_IPV6;
4794 memcpy(&info->key.u.ipv6.dst, from->remote_ipv6,
4795 sizeof(from->remote_ipv6));
4796 memcpy(&info->key.u.ipv6.src, from->local_ipv6,
4797 sizeof(from->local_ipv6));
4798 info->key.label = cpu_to_be32(from->tunnel_label) &
4799 IPV6_FLOWLABEL_MASK;
4801 info->key.u.ipv4.dst = cpu_to_be32(from->remote_ipv4);
4802 info->key.u.ipv4.src = cpu_to_be32(from->local_ipv4);
4803 info->key.flow_flags = FLOWI_FLAG_ANYSRC;
4809 static const struct bpf_func_proto bpf_skb_set_tunnel_key_proto = {
4810 .func = bpf_skb_set_tunnel_key,
4812 .ret_type = RET_INTEGER,
4813 .arg1_type = ARG_PTR_TO_CTX,
4814 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
4815 .arg3_type = ARG_CONST_SIZE,
4816 .arg4_type = ARG_ANYTHING,
4819 BPF_CALL_3(bpf_skb_set_tunnel_opt, struct sk_buff *, skb,
4820 const u8 *, from, u32, size)
4822 struct ip_tunnel_info *info = skb_tunnel_info(skb);
4823 const struct metadata_dst *md = this_cpu_ptr(md_dst);
4825 if (unlikely(info != &md->u.tun_info || (size & (sizeof(u32) - 1))))
4827 if (unlikely(size > IP_TUNNEL_OPTS_MAX))
4830 ip_tunnel_info_opts_set(info, from, size, TUNNEL_OPTIONS_PRESENT);
4835 static const struct bpf_func_proto bpf_skb_set_tunnel_opt_proto = {
4836 .func = bpf_skb_set_tunnel_opt,
4838 .ret_type = RET_INTEGER,
4839 .arg1_type = ARG_PTR_TO_CTX,
4840 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
4841 .arg3_type = ARG_CONST_SIZE,
4844 static const struct bpf_func_proto *
4845 bpf_get_skb_set_tunnel_proto(enum bpf_func_id which)
4848 struct metadata_dst __percpu *tmp;
4850 tmp = metadata_dst_alloc_percpu(IP_TUNNEL_OPTS_MAX,
4855 if (cmpxchg(&md_dst, NULL, tmp))
4856 metadata_dst_free_percpu(tmp);
4860 case BPF_FUNC_skb_set_tunnel_key:
4861 return &bpf_skb_set_tunnel_key_proto;
4862 case BPF_FUNC_skb_set_tunnel_opt:
4863 return &bpf_skb_set_tunnel_opt_proto;
4869 BPF_CALL_3(bpf_skb_under_cgroup, struct sk_buff *, skb, struct bpf_map *, map,
4872 struct bpf_array *array = container_of(map, struct bpf_array, map);
4873 struct cgroup *cgrp;
4876 sk = skb_to_full_sk(skb);
4877 if (!sk || !sk_fullsock(sk))
4879 if (unlikely(idx >= array->map.max_entries))
4882 cgrp = READ_ONCE(array->ptrs[idx]);
4883 if (unlikely(!cgrp))
4886 return sk_under_cgroup_hierarchy(sk, cgrp);
4889 static const struct bpf_func_proto bpf_skb_under_cgroup_proto = {
4890 .func = bpf_skb_under_cgroup,
4892 .ret_type = RET_INTEGER,
4893 .arg1_type = ARG_PTR_TO_CTX,
4894 .arg2_type = ARG_CONST_MAP_PTR,
4895 .arg3_type = ARG_ANYTHING,
4898 #ifdef CONFIG_SOCK_CGROUP_DATA
4899 static inline u64 __bpf_sk_cgroup_id(struct sock *sk)
4901 struct cgroup *cgrp;
4903 sk = sk_to_full_sk(sk);
4904 if (!sk || !sk_fullsock(sk))
4907 cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
4908 return cgroup_id(cgrp);
4911 BPF_CALL_1(bpf_skb_cgroup_id, const struct sk_buff *, skb)
4913 return __bpf_sk_cgroup_id(skb->sk);
4916 static const struct bpf_func_proto bpf_skb_cgroup_id_proto = {
4917 .func = bpf_skb_cgroup_id,
4919 .ret_type = RET_INTEGER,
4920 .arg1_type = ARG_PTR_TO_CTX,
4923 static inline u64 __bpf_sk_ancestor_cgroup_id(struct sock *sk,
4926 struct cgroup *ancestor;
4927 struct cgroup *cgrp;
4929 sk = sk_to_full_sk(sk);
4930 if (!sk || !sk_fullsock(sk))
4933 cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
4934 ancestor = cgroup_ancestor(cgrp, ancestor_level);
4938 return cgroup_id(ancestor);
4941 BPF_CALL_2(bpf_skb_ancestor_cgroup_id, const struct sk_buff *, skb, int,
4944 return __bpf_sk_ancestor_cgroup_id(skb->sk, ancestor_level);
4947 static const struct bpf_func_proto bpf_skb_ancestor_cgroup_id_proto = {
4948 .func = bpf_skb_ancestor_cgroup_id,
4950 .ret_type = RET_INTEGER,
4951 .arg1_type = ARG_PTR_TO_CTX,
4952 .arg2_type = ARG_ANYTHING,
4955 BPF_CALL_1(bpf_sk_cgroup_id, struct sock *, sk)
4957 return __bpf_sk_cgroup_id(sk);
4960 static const struct bpf_func_proto bpf_sk_cgroup_id_proto = {
4961 .func = bpf_sk_cgroup_id,
4963 .ret_type = RET_INTEGER,
4964 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
4967 BPF_CALL_2(bpf_sk_ancestor_cgroup_id, struct sock *, sk, int, ancestor_level)
4969 return __bpf_sk_ancestor_cgroup_id(sk, ancestor_level);
4972 static const struct bpf_func_proto bpf_sk_ancestor_cgroup_id_proto = {
4973 .func = bpf_sk_ancestor_cgroup_id,
4975 .ret_type = RET_INTEGER,
4976 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
4977 .arg2_type = ARG_ANYTHING,
4981 static unsigned long bpf_xdp_copy(void *dst, const void *ctx,
4982 unsigned long off, unsigned long len)
4984 struct xdp_buff *xdp = (struct xdp_buff *)ctx;
4986 bpf_xdp_copy_buf(xdp, off, dst, len, false);
4990 BPF_CALL_5(bpf_xdp_event_output, struct xdp_buff *, xdp, struct bpf_map *, map,
4991 u64, flags, void *, meta, u64, meta_size)
4993 u64 xdp_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
4995 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
4998 if (unlikely(!xdp || xdp_size > xdp_get_buff_len(xdp)))
5001 return bpf_event_output(map, flags, meta, meta_size, xdp,
5002 xdp_size, bpf_xdp_copy);
5005 static const struct bpf_func_proto bpf_xdp_event_output_proto = {
5006 .func = bpf_xdp_event_output,
5008 .ret_type = RET_INTEGER,
5009 .arg1_type = ARG_PTR_TO_CTX,
5010 .arg2_type = ARG_CONST_MAP_PTR,
5011 .arg3_type = ARG_ANYTHING,
5012 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
5013 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
5016 BTF_ID_LIST_SINGLE(bpf_xdp_output_btf_ids, struct, xdp_buff)
5018 const struct bpf_func_proto bpf_xdp_output_proto = {
5019 .func = bpf_xdp_event_output,
5021 .ret_type = RET_INTEGER,
5022 .arg1_type = ARG_PTR_TO_BTF_ID,
5023 .arg1_btf_id = &bpf_xdp_output_btf_ids[0],
5024 .arg2_type = ARG_CONST_MAP_PTR,
5025 .arg3_type = ARG_ANYTHING,
5026 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
5027 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
5030 BPF_CALL_1(bpf_get_socket_cookie, struct sk_buff *, skb)
5032 return skb->sk ? __sock_gen_cookie(skb->sk) : 0;
5035 static const struct bpf_func_proto bpf_get_socket_cookie_proto = {
5036 .func = bpf_get_socket_cookie,
5038 .ret_type = RET_INTEGER,
5039 .arg1_type = ARG_PTR_TO_CTX,
5042 BPF_CALL_1(bpf_get_socket_cookie_sock_addr, struct bpf_sock_addr_kern *, ctx)
5044 return __sock_gen_cookie(ctx->sk);
5047 static const struct bpf_func_proto bpf_get_socket_cookie_sock_addr_proto = {
5048 .func = bpf_get_socket_cookie_sock_addr,
5050 .ret_type = RET_INTEGER,
5051 .arg1_type = ARG_PTR_TO_CTX,
5054 BPF_CALL_1(bpf_get_socket_cookie_sock, struct sock *, ctx)
5056 return __sock_gen_cookie(ctx);
5059 static const struct bpf_func_proto bpf_get_socket_cookie_sock_proto = {
5060 .func = bpf_get_socket_cookie_sock,
5062 .ret_type = RET_INTEGER,
5063 .arg1_type = ARG_PTR_TO_CTX,
5066 BPF_CALL_1(bpf_get_socket_ptr_cookie, struct sock *, sk)
5068 return sk ? sock_gen_cookie(sk) : 0;
5071 const struct bpf_func_proto bpf_get_socket_ptr_cookie_proto = {
5072 .func = bpf_get_socket_ptr_cookie,
5074 .ret_type = RET_INTEGER,
5075 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON | PTR_MAYBE_NULL,
5078 BPF_CALL_1(bpf_get_socket_cookie_sock_ops, struct bpf_sock_ops_kern *, ctx)
5080 return __sock_gen_cookie(ctx->sk);
5083 static const struct bpf_func_proto bpf_get_socket_cookie_sock_ops_proto = {
5084 .func = bpf_get_socket_cookie_sock_ops,
5086 .ret_type = RET_INTEGER,
5087 .arg1_type = ARG_PTR_TO_CTX,
5090 static u64 __bpf_get_netns_cookie(struct sock *sk)
5092 const struct net *net = sk ? sock_net(sk) : &init_net;
5094 return net->net_cookie;
5097 BPF_CALL_1(bpf_get_netns_cookie_sock, struct sock *, ctx)
5099 return __bpf_get_netns_cookie(ctx);
5102 static const struct bpf_func_proto bpf_get_netns_cookie_sock_proto = {
5103 .func = bpf_get_netns_cookie_sock,
5105 .ret_type = RET_INTEGER,
5106 .arg1_type = ARG_PTR_TO_CTX_OR_NULL,
5109 BPF_CALL_1(bpf_get_netns_cookie_sock_addr, struct bpf_sock_addr_kern *, ctx)
5111 return __bpf_get_netns_cookie(ctx ? ctx->sk : NULL);
5114 static const struct bpf_func_proto bpf_get_netns_cookie_sock_addr_proto = {
5115 .func = bpf_get_netns_cookie_sock_addr,
5117 .ret_type = RET_INTEGER,
5118 .arg1_type = ARG_PTR_TO_CTX_OR_NULL,
5121 BPF_CALL_1(bpf_get_netns_cookie_sock_ops, struct bpf_sock_ops_kern *, ctx)
5123 return __bpf_get_netns_cookie(ctx ? ctx->sk : NULL);
5126 static const struct bpf_func_proto bpf_get_netns_cookie_sock_ops_proto = {
5127 .func = bpf_get_netns_cookie_sock_ops,
5129 .ret_type = RET_INTEGER,
5130 .arg1_type = ARG_PTR_TO_CTX_OR_NULL,
5133 BPF_CALL_1(bpf_get_netns_cookie_sk_msg, struct sk_msg *, ctx)
5135 return __bpf_get_netns_cookie(ctx ? ctx->sk : NULL);
5138 static const struct bpf_func_proto bpf_get_netns_cookie_sk_msg_proto = {
5139 .func = bpf_get_netns_cookie_sk_msg,
5141 .ret_type = RET_INTEGER,
5142 .arg1_type = ARG_PTR_TO_CTX_OR_NULL,
5145 BPF_CALL_1(bpf_get_socket_uid, struct sk_buff *, skb)
5147 struct sock *sk = sk_to_full_sk(skb->sk);
5150 if (!sk || !sk_fullsock(sk))
5152 kuid = sock_net_uid(sock_net(sk), sk);
5153 return from_kuid_munged(sock_net(sk)->user_ns, kuid);
5156 static const struct bpf_func_proto bpf_get_socket_uid_proto = {
5157 .func = bpf_get_socket_uid,
5159 .ret_type = RET_INTEGER,
5160 .arg1_type = ARG_PTR_TO_CTX,
5163 static int sol_socket_sockopt(struct sock *sk, int optname,
5164 char *optval, int *optlen,
5176 case SO_MAX_PACING_RATE:
5177 case SO_BINDTOIFINDEX:
5179 if (*optlen != sizeof(int))
5182 case SO_BINDTODEVICE:
5189 if (optname == SO_BINDTODEVICE)
5191 return sk_getsockopt(sk, SOL_SOCKET, optname,
5192 KERNEL_SOCKPTR(optval),
5193 KERNEL_SOCKPTR(optlen));
5196 return sk_setsockopt(sk, SOL_SOCKET, optname,
5197 KERNEL_SOCKPTR(optval), *optlen);
5200 static int bpf_sol_tcp_setsockopt(struct sock *sk, int optname,
5201 char *optval, int optlen)
5203 struct tcp_sock *tp = tcp_sk(sk);
5204 unsigned long timeout;
5207 if (optlen != sizeof(int))
5210 val = *(int *)optval;
5212 /* Only some options are supported */
5215 if (val <= 0 || tp->data_segs_out > tp->syn_data)
5217 tcp_snd_cwnd_set(tp, val);
5219 case TCP_BPF_SNDCWND_CLAMP:
5222 tp->snd_cwnd_clamp = val;
5223 tp->snd_ssthresh = val;
5225 case TCP_BPF_DELACK_MAX:
5226 timeout = usecs_to_jiffies(val);
5227 if (timeout > TCP_DELACK_MAX ||
5228 timeout < TCP_TIMEOUT_MIN)
5230 inet_csk(sk)->icsk_delack_max = timeout;
5232 case TCP_BPF_RTO_MIN:
5233 timeout = usecs_to_jiffies(val);
5234 if (timeout > TCP_RTO_MIN ||
5235 timeout < TCP_TIMEOUT_MIN)
5237 inet_csk(sk)->icsk_rto_min = timeout;
5246 static int sol_tcp_sockopt_congestion(struct sock *sk, char *optval,
5247 int *optlen, bool getopt)
5249 struct tcp_sock *tp;
5256 if (!inet_csk(sk)->icsk_ca_ops)
5258 /* BPF expects NULL-terminated tcp-cc string */
5259 optval[--(*optlen)] = '\0';
5260 return do_tcp_getsockopt(sk, SOL_TCP, TCP_CONGESTION,
5261 KERNEL_SOCKPTR(optval),
5262 KERNEL_SOCKPTR(optlen));
5265 /* "cdg" is the only cc that alloc a ptr
5266 * in inet_csk_ca area. The bpf-tcp-cc may
5267 * overwrite this ptr after switching to cdg.
5269 if (*optlen >= sizeof("cdg") - 1 && !strncmp("cdg", optval, *optlen))
5272 /* It stops this looping
5274 * .init => bpf_setsockopt(tcp_cc) => .init =>
5275 * bpf_setsockopt(tcp_cc)" => .init => ....
5277 * The second bpf_setsockopt(tcp_cc) is not allowed
5278 * in order to break the loop when both .init
5279 * are the same bpf prog.
5281 * This applies even the second bpf_setsockopt(tcp_cc)
5282 * does not cause a loop. This limits only the first
5283 * '.init' can call bpf_setsockopt(TCP_CONGESTION) to
5284 * pick a fallback cc (eg. peer does not support ECN)
5285 * and the second '.init' cannot fallback to
5289 if (tp->bpf_chg_cc_inprogress)
5292 tp->bpf_chg_cc_inprogress = 1;
5293 ret = do_tcp_setsockopt(sk, SOL_TCP, TCP_CONGESTION,
5294 KERNEL_SOCKPTR(optval), *optlen);
5295 tp->bpf_chg_cc_inprogress = 0;
5299 static int sol_tcp_sockopt(struct sock *sk, int optname,
5300 char *optval, int *optlen,
5303 if (sk->sk_protocol != IPPROTO_TCP)
5313 case TCP_WINDOW_CLAMP:
5314 case TCP_THIN_LINEAR_TIMEOUTS:
5315 case TCP_USER_TIMEOUT:
5316 case TCP_NOTSENT_LOWAT:
5318 if (*optlen != sizeof(int))
5321 case TCP_CONGESTION:
5322 return sol_tcp_sockopt_congestion(sk, optval, optlen, getopt);
5330 return bpf_sol_tcp_setsockopt(sk, optname, optval, *optlen);
5334 if (optname == TCP_SAVED_SYN) {
5335 struct tcp_sock *tp = tcp_sk(sk);
5337 if (!tp->saved_syn ||
5338 *optlen > tcp_saved_syn_len(tp->saved_syn))
5340 memcpy(optval, tp->saved_syn->data, *optlen);
5341 /* It cannot free tp->saved_syn here because it
5342 * does not know if the user space still needs it.
5347 return do_tcp_getsockopt(sk, SOL_TCP, optname,
5348 KERNEL_SOCKPTR(optval),
5349 KERNEL_SOCKPTR(optlen));
5352 return do_tcp_setsockopt(sk, SOL_TCP, optname,
5353 KERNEL_SOCKPTR(optval), *optlen);
5356 static int sol_ip_sockopt(struct sock *sk, int optname,
5357 char *optval, int *optlen,
5360 if (sk->sk_family != AF_INET)
5365 if (*optlen != sizeof(int))
5373 return do_ip_getsockopt(sk, SOL_IP, optname,
5374 KERNEL_SOCKPTR(optval),
5375 KERNEL_SOCKPTR(optlen));
5377 return do_ip_setsockopt(sk, SOL_IP, optname,
5378 KERNEL_SOCKPTR(optval), *optlen);
5381 static int sol_ipv6_sockopt(struct sock *sk, int optname,
5382 char *optval, int *optlen,
5385 if (sk->sk_family != AF_INET6)
5390 case IPV6_AUTOFLOWLABEL:
5391 if (*optlen != sizeof(int))
5399 return ipv6_bpf_stub->ipv6_getsockopt(sk, SOL_IPV6, optname,
5400 KERNEL_SOCKPTR(optval),
5401 KERNEL_SOCKPTR(optlen));
5403 return ipv6_bpf_stub->ipv6_setsockopt(sk, SOL_IPV6, optname,
5404 KERNEL_SOCKPTR(optval), *optlen);
5407 static int __bpf_setsockopt(struct sock *sk, int level, int optname,
5408 char *optval, int optlen)
5410 if (!sk_fullsock(sk))
5413 if (level == SOL_SOCKET)
5414 return sol_socket_sockopt(sk, optname, optval, &optlen, false);
5415 else if (IS_ENABLED(CONFIG_INET) && level == SOL_IP)
5416 return sol_ip_sockopt(sk, optname, optval, &optlen, false);
5417 else if (IS_ENABLED(CONFIG_IPV6) && level == SOL_IPV6)
5418 return sol_ipv6_sockopt(sk, optname, optval, &optlen, false);
5419 else if (IS_ENABLED(CONFIG_INET) && level == SOL_TCP)
5420 return sol_tcp_sockopt(sk, optname, optval, &optlen, false);
5425 static int _bpf_setsockopt(struct sock *sk, int level, int optname,
5426 char *optval, int optlen)
5428 if (sk_fullsock(sk))
5429 sock_owned_by_me(sk);
5430 return __bpf_setsockopt(sk, level, optname, optval, optlen);
5433 static int __bpf_getsockopt(struct sock *sk, int level, int optname,
5434 char *optval, int optlen)
5436 int err, saved_optlen = optlen;
5438 if (!sk_fullsock(sk)) {
5443 if (level == SOL_SOCKET)
5444 err = sol_socket_sockopt(sk, optname, optval, &optlen, true);
5445 else if (IS_ENABLED(CONFIG_INET) && level == SOL_TCP)
5446 err = sol_tcp_sockopt(sk, optname, optval, &optlen, true);
5447 else if (IS_ENABLED(CONFIG_INET) && level == SOL_IP)
5448 err = sol_ip_sockopt(sk, optname, optval, &optlen, true);
5449 else if (IS_ENABLED(CONFIG_IPV6) && level == SOL_IPV6)
5450 err = sol_ipv6_sockopt(sk, optname, optval, &optlen, true);
5457 if (optlen < saved_optlen)
5458 memset(optval + optlen, 0, saved_optlen - optlen);
5462 static int _bpf_getsockopt(struct sock *sk, int level, int optname,
5463 char *optval, int optlen)
5465 if (sk_fullsock(sk))
5466 sock_owned_by_me(sk);
5467 return __bpf_getsockopt(sk, level, optname, optval, optlen);
5470 BPF_CALL_5(bpf_sk_setsockopt, struct sock *, sk, int, level,
5471 int, optname, char *, optval, int, optlen)
5473 return _bpf_setsockopt(sk, level, optname, optval, optlen);
5476 const struct bpf_func_proto bpf_sk_setsockopt_proto = {
5477 .func = bpf_sk_setsockopt,
5479 .ret_type = RET_INTEGER,
5480 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
5481 .arg2_type = ARG_ANYTHING,
5482 .arg3_type = ARG_ANYTHING,
5483 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
5484 .arg5_type = ARG_CONST_SIZE,
5487 BPF_CALL_5(bpf_sk_getsockopt, struct sock *, sk, int, level,
5488 int, optname, char *, optval, int, optlen)
5490 return _bpf_getsockopt(sk, level, optname, optval, optlen);
5493 const struct bpf_func_proto bpf_sk_getsockopt_proto = {
5494 .func = bpf_sk_getsockopt,
5496 .ret_type = RET_INTEGER,
5497 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
5498 .arg2_type = ARG_ANYTHING,
5499 .arg3_type = ARG_ANYTHING,
5500 .arg4_type = ARG_PTR_TO_UNINIT_MEM,
5501 .arg5_type = ARG_CONST_SIZE,
5504 BPF_CALL_5(bpf_unlocked_sk_setsockopt, struct sock *, sk, int, level,
5505 int, optname, char *, optval, int, optlen)
5507 return __bpf_setsockopt(sk, level, optname, optval, optlen);
5510 const struct bpf_func_proto bpf_unlocked_sk_setsockopt_proto = {
5511 .func = bpf_unlocked_sk_setsockopt,
5513 .ret_type = RET_INTEGER,
5514 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
5515 .arg2_type = ARG_ANYTHING,
5516 .arg3_type = ARG_ANYTHING,
5517 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
5518 .arg5_type = ARG_CONST_SIZE,
5521 BPF_CALL_5(bpf_unlocked_sk_getsockopt, struct sock *, sk, int, level,
5522 int, optname, char *, optval, int, optlen)
5524 return __bpf_getsockopt(sk, level, optname, optval, optlen);
5527 const struct bpf_func_proto bpf_unlocked_sk_getsockopt_proto = {
5528 .func = bpf_unlocked_sk_getsockopt,
5530 .ret_type = RET_INTEGER,
5531 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
5532 .arg2_type = ARG_ANYTHING,
5533 .arg3_type = ARG_ANYTHING,
5534 .arg4_type = ARG_PTR_TO_UNINIT_MEM,
5535 .arg5_type = ARG_CONST_SIZE,
5538 BPF_CALL_5(bpf_sock_addr_setsockopt, struct bpf_sock_addr_kern *, ctx,
5539 int, level, int, optname, char *, optval, int, optlen)
5541 return _bpf_setsockopt(ctx->sk, level, optname, optval, optlen);
5544 static const struct bpf_func_proto bpf_sock_addr_setsockopt_proto = {
5545 .func = bpf_sock_addr_setsockopt,
5547 .ret_type = RET_INTEGER,
5548 .arg1_type = ARG_PTR_TO_CTX,
5549 .arg2_type = ARG_ANYTHING,
5550 .arg3_type = ARG_ANYTHING,
5551 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
5552 .arg5_type = ARG_CONST_SIZE,
5555 BPF_CALL_5(bpf_sock_addr_getsockopt, struct bpf_sock_addr_kern *, ctx,
5556 int, level, int, optname, char *, optval, int, optlen)
5558 return _bpf_getsockopt(ctx->sk, level, optname, optval, optlen);
5561 static const struct bpf_func_proto bpf_sock_addr_getsockopt_proto = {
5562 .func = bpf_sock_addr_getsockopt,
5564 .ret_type = RET_INTEGER,
5565 .arg1_type = ARG_PTR_TO_CTX,
5566 .arg2_type = ARG_ANYTHING,
5567 .arg3_type = ARG_ANYTHING,
5568 .arg4_type = ARG_PTR_TO_UNINIT_MEM,
5569 .arg5_type = ARG_CONST_SIZE,
5572 BPF_CALL_5(bpf_sock_ops_setsockopt, struct bpf_sock_ops_kern *, bpf_sock,
5573 int, level, int, optname, char *, optval, int, optlen)
5575 return _bpf_setsockopt(bpf_sock->sk, level, optname, optval, optlen);
5578 static const struct bpf_func_proto bpf_sock_ops_setsockopt_proto = {
5579 .func = bpf_sock_ops_setsockopt,
5581 .ret_type = RET_INTEGER,
5582 .arg1_type = ARG_PTR_TO_CTX,
5583 .arg2_type = ARG_ANYTHING,
5584 .arg3_type = ARG_ANYTHING,
5585 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
5586 .arg5_type = ARG_CONST_SIZE,
5589 static int bpf_sock_ops_get_syn(struct bpf_sock_ops_kern *bpf_sock,
5590 int optname, const u8 **start)
5592 struct sk_buff *syn_skb = bpf_sock->syn_skb;
5593 const u8 *hdr_start;
5597 /* sk is a request_sock here */
5599 if (optname == TCP_BPF_SYN) {
5600 hdr_start = syn_skb->data;
5601 ret = tcp_hdrlen(syn_skb);
5602 } else if (optname == TCP_BPF_SYN_IP) {
5603 hdr_start = skb_network_header(syn_skb);
5604 ret = skb_network_header_len(syn_skb) +
5605 tcp_hdrlen(syn_skb);
5607 /* optname == TCP_BPF_SYN_MAC */
5608 hdr_start = skb_mac_header(syn_skb);
5609 ret = skb_mac_header_len(syn_skb) +
5610 skb_network_header_len(syn_skb) +
5611 tcp_hdrlen(syn_skb);
5614 struct sock *sk = bpf_sock->sk;
5615 struct saved_syn *saved_syn;
5617 if (sk->sk_state == TCP_NEW_SYN_RECV)
5618 /* synack retransmit. bpf_sock->syn_skb will
5619 * not be available. It has to resort to
5620 * saved_syn (if it is saved).
5622 saved_syn = inet_reqsk(sk)->saved_syn;
5624 saved_syn = tcp_sk(sk)->saved_syn;
5629 if (optname == TCP_BPF_SYN) {
5630 hdr_start = saved_syn->data +
5631 saved_syn->mac_hdrlen +
5632 saved_syn->network_hdrlen;
5633 ret = saved_syn->tcp_hdrlen;
5634 } else if (optname == TCP_BPF_SYN_IP) {
5635 hdr_start = saved_syn->data +
5636 saved_syn->mac_hdrlen;
5637 ret = saved_syn->network_hdrlen +
5638 saved_syn->tcp_hdrlen;
5640 /* optname == TCP_BPF_SYN_MAC */
5642 /* TCP_SAVE_SYN may not have saved the mac hdr */
5643 if (!saved_syn->mac_hdrlen)
5646 hdr_start = saved_syn->data;
5647 ret = saved_syn->mac_hdrlen +
5648 saved_syn->network_hdrlen +
5649 saved_syn->tcp_hdrlen;
5657 BPF_CALL_5(bpf_sock_ops_getsockopt, struct bpf_sock_ops_kern *, bpf_sock,
5658 int, level, int, optname, char *, optval, int, optlen)
5660 if (IS_ENABLED(CONFIG_INET) && level == SOL_TCP &&
5661 optname >= TCP_BPF_SYN && optname <= TCP_BPF_SYN_MAC) {
5662 int ret, copy_len = 0;
5665 ret = bpf_sock_ops_get_syn(bpf_sock, optname, &start);
5668 if (optlen < copy_len) {
5673 memcpy(optval, start, copy_len);
5676 /* Zero out unused buffer at the end */
5677 memset(optval + copy_len, 0, optlen - copy_len);
5682 return _bpf_getsockopt(bpf_sock->sk, level, optname, optval, optlen);
5685 static const struct bpf_func_proto bpf_sock_ops_getsockopt_proto = {
5686 .func = bpf_sock_ops_getsockopt,
5688 .ret_type = RET_INTEGER,
5689 .arg1_type = ARG_PTR_TO_CTX,
5690 .arg2_type = ARG_ANYTHING,
5691 .arg3_type = ARG_ANYTHING,
5692 .arg4_type = ARG_PTR_TO_UNINIT_MEM,
5693 .arg5_type = ARG_CONST_SIZE,
5696 BPF_CALL_2(bpf_sock_ops_cb_flags_set, struct bpf_sock_ops_kern *, bpf_sock,
5699 struct sock *sk = bpf_sock->sk;
5700 int val = argval & BPF_SOCK_OPS_ALL_CB_FLAGS;
5702 if (!IS_ENABLED(CONFIG_INET) || !sk_fullsock(sk))
5705 tcp_sk(sk)->bpf_sock_ops_cb_flags = val;
5707 return argval & (~BPF_SOCK_OPS_ALL_CB_FLAGS);
5710 static const struct bpf_func_proto bpf_sock_ops_cb_flags_set_proto = {
5711 .func = bpf_sock_ops_cb_flags_set,
5713 .ret_type = RET_INTEGER,
5714 .arg1_type = ARG_PTR_TO_CTX,
5715 .arg2_type = ARG_ANYTHING,
5718 const struct ipv6_bpf_stub *ipv6_bpf_stub __read_mostly;
5719 EXPORT_SYMBOL_GPL(ipv6_bpf_stub);
5721 BPF_CALL_3(bpf_bind, struct bpf_sock_addr_kern *, ctx, struct sockaddr *, addr,
5725 struct sock *sk = ctx->sk;
5726 u32 flags = BIND_FROM_BPF;
5730 if (addr_len < offsetofend(struct sockaddr, sa_family))
5732 if (addr->sa_family == AF_INET) {
5733 if (addr_len < sizeof(struct sockaddr_in))
5735 if (((struct sockaddr_in *)addr)->sin_port == htons(0))
5736 flags |= BIND_FORCE_ADDRESS_NO_PORT;
5737 return __inet_bind(sk, addr, addr_len, flags);
5738 #if IS_ENABLED(CONFIG_IPV6)
5739 } else if (addr->sa_family == AF_INET6) {
5740 if (addr_len < SIN6_LEN_RFC2133)
5742 if (((struct sockaddr_in6 *)addr)->sin6_port == htons(0))
5743 flags |= BIND_FORCE_ADDRESS_NO_PORT;
5744 /* ipv6_bpf_stub cannot be NULL, since it's called from
5745 * bpf_cgroup_inet6_connect hook and ipv6 is already loaded
5747 return ipv6_bpf_stub->inet6_bind(sk, addr, addr_len, flags);
5748 #endif /* CONFIG_IPV6 */
5750 #endif /* CONFIG_INET */
5752 return -EAFNOSUPPORT;
5755 static const struct bpf_func_proto bpf_bind_proto = {
5758 .ret_type = RET_INTEGER,
5759 .arg1_type = ARG_PTR_TO_CTX,
5760 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
5761 .arg3_type = ARG_CONST_SIZE,
5766 #if (IS_BUILTIN(CONFIG_XFRM_INTERFACE) && IS_ENABLED(CONFIG_DEBUG_INFO_BTF)) || \
5767 (IS_MODULE(CONFIG_XFRM_INTERFACE) && IS_ENABLED(CONFIG_DEBUG_INFO_BTF_MODULES))
5769 struct metadata_dst __percpu *xfrm_bpf_md_dst;
5770 EXPORT_SYMBOL_GPL(xfrm_bpf_md_dst);
5774 BPF_CALL_5(bpf_skb_get_xfrm_state, struct sk_buff *, skb, u32, index,
5775 struct bpf_xfrm_state *, to, u32, size, u64, flags)
5777 const struct sec_path *sp = skb_sec_path(skb);
5778 const struct xfrm_state *x;
5780 if (!sp || unlikely(index >= sp->len || flags))
5783 x = sp->xvec[index];
5785 if (unlikely(size != sizeof(struct bpf_xfrm_state)))
5788 to->reqid = x->props.reqid;
5789 to->spi = x->id.spi;
5790 to->family = x->props.family;
5793 if (to->family == AF_INET6) {
5794 memcpy(to->remote_ipv6, x->props.saddr.a6,
5795 sizeof(to->remote_ipv6));
5797 to->remote_ipv4 = x->props.saddr.a4;
5798 memset(&to->remote_ipv6[1], 0, sizeof(__u32) * 3);
5803 memset(to, 0, size);
5807 static const struct bpf_func_proto bpf_skb_get_xfrm_state_proto = {
5808 .func = bpf_skb_get_xfrm_state,
5810 .ret_type = RET_INTEGER,
5811 .arg1_type = ARG_PTR_TO_CTX,
5812 .arg2_type = ARG_ANYTHING,
5813 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
5814 .arg4_type = ARG_CONST_SIZE,
5815 .arg5_type = ARG_ANYTHING,
5819 #if IS_ENABLED(CONFIG_INET) || IS_ENABLED(CONFIG_IPV6)
5820 static int bpf_fib_set_fwd_params(struct bpf_fib_lookup *params, u32 mtu)
5822 params->h_vlan_TCI = 0;
5823 params->h_vlan_proto = 0;
5825 params->mtu_result = mtu; /* union with tot_len */
5831 #if IS_ENABLED(CONFIG_INET)
5832 static int bpf_ipv4_fib_lookup(struct net *net, struct bpf_fib_lookup *params,
5833 u32 flags, bool check_mtu)
5835 struct fib_nh_common *nhc;
5836 struct in_device *in_dev;
5837 struct neighbour *neigh;
5838 struct net_device *dev;
5839 struct fib_result res;
5844 dev = dev_get_by_index_rcu(net, params->ifindex);
5848 /* verify forwarding is enabled on this interface */
5849 in_dev = __in_dev_get_rcu(dev);
5850 if (unlikely(!in_dev || !IN_DEV_FORWARD(in_dev)))
5851 return BPF_FIB_LKUP_RET_FWD_DISABLED;
5853 if (flags & BPF_FIB_LOOKUP_OUTPUT) {
5855 fl4.flowi4_oif = params->ifindex;
5857 fl4.flowi4_iif = params->ifindex;
5860 fl4.flowi4_tos = params->tos & IPTOS_RT_MASK;
5861 fl4.flowi4_scope = RT_SCOPE_UNIVERSE;
5862 fl4.flowi4_flags = 0;
5864 fl4.flowi4_proto = params->l4_protocol;
5865 fl4.daddr = params->ipv4_dst;
5866 fl4.saddr = params->ipv4_src;
5867 fl4.fl4_sport = params->sport;
5868 fl4.fl4_dport = params->dport;
5869 fl4.flowi4_multipath_hash = 0;
5871 if (flags & BPF_FIB_LOOKUP_DIRECT) {
5872 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
5873 struct fib_table *tb;
5875 if (flags & BPF_FIB_LOOKUP_TBID) {
5876 tbid = params->tbid;
5877 /* zero out for vlan output */
5881 tb = fib_get_table(net, tbid);
5883 return BPF_FIB_LKUP_RET_NOT_FWDED;
5885 err = fib_table_lookup(tb, &fl4, &res, FIB_LOOKUP_NOREF);
5887 fl4.flowi4_mark = 0;
5888 fl4.flowi4_secid = 0;
5889 fl4.flowi4_tun_key.tun_id = 0;
5890 fl4.flowi4_uid = sock_net_uid(net, NULL);
5892 err = fib_lookup(net, &fl4, &res, FIB_LOOKUP_NOREF);
5896 /* map fib lookup errors to RTN_ type */
5898 return BPF_FIB_LKUP_RET_BLACKHOLE;
5899 if (err == -EHOSTUNREACH)
5900 return BPF_FIB_LKUP_RET_UNREACHABLE;
5902 return BPF_FIB_LKUP_RET_PROHIBIT;
5904 return BPF_FIB_LKUP_RET_NOT_FWDED;
5907 if (res.type != RTN_UNICAST)
5908 return BPF_FIB_LKUP_RET_NOT_FWDED;
5910 if (fib_info_num_path(res.fi) > 1)
5911 fib_select_path(net, &res, &fl4, NULL);
5914 mtu = ip_mtu_from_fib_result(&res, params->ipv4_dst);
5915 if (params->tot_len > mtu) {
5916 params->mtu_result = mtu; /* union with tot_len */
5917 return BPF_FIB_LKUP_RET_FRAG_NEEDED;
5923 /* do not handle lwt encaps right now */
5924 if (nhc->nhc_lwtstate)
5925 return BPF_FIB_LKUP_RET_UNSUPP_LWT;
5929 params->rt_metric = res.fi->fib_priority;
5930 params->ifindex = dev->ifindex;
5932 if (flags & BPF_FIB_LOOKUP_SRC)
5933 params->ipv4_src = fib_result_prefsrc(net, &res);
5935 /* xdp and cls_bpf programs are run in RCU-bh so
5936 * rcu_read_lock_bh is not needed here
5938 if (likely(nhc->nhc_gw_family != AF_INET6)) {
5939 if (nhc->nhc_gw_family)
5940 params->ipv4_dst = nhc->nhc_gw.ipv4;
5942 struct in6_addr *dst = (struct in6_addr *)params->ipv6_dst;
5944 params->family = AF_INET6;
5945 *dst = nhc->nhc_gw.ipv6;
5948 if (flags & BPF_FIB_LOOKUP_SKIP_NEIGH)
5949 goto set_fwd_params;
5951 if (likely(nhc->nhc_gw_family != AF_INET6))
5952 neigh = __ipv4_neigh_lookup_noref(dev,
5953 (__force u32)params->ipv4_dst);
5955 neigh = __ipv6_neigh_lookup_noref_stub(dev, params->ipv6_dst);
5957 if (!neigh || !(READ_ONCE(neigh->nud_state) & NUD_VALID))
5958 return BPF_FIB_LKUP_RET_NO_NEIGH;
5959 memcpy(params->dmac, neigh->ha, ETH_ALEN);
5960 memcpy(params->smac, dev->dev_addr, ETH_ALEN);
5963 return bpf_fib_set_fwd_params(params, mtu);
5967 #if IS_ENABLED(CONFIG_IPV6)
5968 static int bpf_ipv6_fib_lookup(struct net *net, struct bpf_fib_lookup *params,
5969 u32 flags, bool check_mtu)
5971 struct in6_addr *src = (struct in6_addr *) params->ipv6_src;
5972 struct in6_addr *dst = (struct in6_addr *) params->ipv6_dst;
5973 struct fib6_result res = {};
5974 struct neighbour *neigh;
5975 struct net_device *dev;
5976 struct inet6_dev *idev;
5982 /* link local addresses are never forwarded */
5983 if (rt6_need_strict(dst) || rt6_need_strict(src))
5984 return BPF_FIB_LKUP_RET_NOT_FWDED;
5986 dev = dev_get_by_index_rcu(net, params->ifindex);
5990 idev = __in6_dev_get_safely(dev);
5991 if (unlikely(!idev || !idev->cnf.forwarding))
5992 return BPF_FIB_LKUP_RET_FWD_DISABLED;
5994 if (flags & BPF_FIB_LOOKUP_OUTPUT) {
5996 oif = fl6.flowi6_oif = params->ifindex;
5998 oif = fl6.flowi6_iif = params->ifindex;
6000 strict = RT6_LOOKUP_F_HAS_SADDR;
6002 fl6.flowlabel = params->flowinfo;
6003 fl6.flowi6_scope = 0;
6004 fl6.flowi6_flags = 0;
6007 fl6.flowi6_proto = params->l4_protocol;
6010 fl6.fl6_sport = params->sport;
6011 fl6.fl6_dport = params->dport;
6013 if (flags & BPF_FIB_LOOKUP_DIRECT) {
6014 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
6015 struct fib6_table *tb;
6017 if (flags & BPF_FIB_LOOKUP_TBID) {
6018 tbid = params->tbid;
6019 /* zero out for vlan output */
6023 tb = ipv6_stub->fib6_get_table(net, tbid);
6025 return BPF_FIB_LKUP_RET_NOT_FWDED;
6027 err = ipv6_stub->fib6_table_lookup(net, tb, oif, &fl6, &res,
6030 fl6.flowi6_mark = 0;
6031 fl6.flowi6_secid = 0;
6032 fl6.flowi6_tun_key.tun_id = 0;
6033 fl6.flowi6_uid = sock_net_uid(net, NULL);
6035 err = ipv6_stub->fib6_lookup(net, oif, &fl6, &res, strict);
6038 if (unlikely(err || IS_ERR_OR_NULL(res.f6i) ||
6039 res.f6i == net->ipv6.fib6_null_entry))
6040 return BPF_FIB_LKUP_RET_NOT_FWDED;
6042 switch (res.fib6_type) {
6043 /* only unicast is forwarded */
6047 return BPF_FIB_LKUP_RET_BLACKHOLE;
6048 case RTN_UNREACHABLE:
6049 return BPF_FIB_LKUP_RET_UNREACHABLE;
6051 return BPF_FIB_LKUP_RET_PROHIBIT;
6053 return BPF_FIB_LKUP_RET_NOT_FWDED;
6056 ipv6_stub->fib6_select_path(net, &res, &fl6, fl6.flowi6_oif,
6057 fl6.flowi6_oif != 0, NULL, strict);
6060 mtu = ipv6_stub->ip6_mtu_from_fib6(&res, dst, src);
6061 if (params->tot_len > mtu) {
6062 params->mtu_result = mtu; /* union with tot_len */
6063 return BPF_FIB_LKUP_RET_FRAG_NEEDED;
6067 if (res.nh->fib_nh_lws)
6068 return BPF_FIB_LKUP_RET_UNSUPP_LWT;
6070 if (res.nh->fib_nh_gw_family)
6071 *dst = res.nh->fib_nh_gw6;
6073 dev = res.nh->fib_nh_dev;
6074 params->rt_metric = res.f6i->fib6_metric;
6075 params->ifindex = dev->ifindex;
6077 if (flags & BPF_FIB_LOOKUP_SRC) {
6078 if (res.f6i->fib6_prefsrc.plen) {
6079 *src = res.f6i->fib6_prefsrc.addr;
6081 err = ipv6_bpf_stub->ipv6_dev_get_saddr(net, dev,
6085 return BPF_FIB_LKUP_RET_NO_SRC_ADDR;
6089 if (flags & BPF_FIB_LOOKUP_SKIP_NEIGH)
6090 goto set_fwd_params;
6092 /* xdp and cls_bpf programs are run in RCU-bh so rcu_read_lock_bh is
6095 neigh = __ipv6_neigh_lookup_noref_stub(dev, dst);
6096 if (!neigh || !(READ_ONCE(neigh->nud_state) & NUD_VALID))
6097 return BPF_FIB_LKUP_RET_NO_NEIGH;
6098 memcpy(params->dmac, neigh->ha, ETH_ALEN);
6099 memcpy(params->smac, dev->dev_addr, ETH_ALEN);
6102 return bpf_fib_set_fwd_params(params, mtu);
6106 #define BPF_FIB_LOOKUP_MASK (BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_OUTPUT | \
6107 BPF_FIB_LOOKUP_SKIP_NEIGH | BPF_FIB_LOOKUP_TBID | \
6110 BPF_CALL_4(bpf_xdp_fib_lookup, struct xdp_buff *, ctx,
6111 struct bpf_fib_lookup *, params, int, plen, u32, flags)
6113 if (plen < sizeof(*params))
6116 if (flags & ~BPF_FIB_LOOKUP_MASK)
6119 switch (params->family) {
6120 #if IS_ENABLED(CONFIG_INET)
6122 return bpf_ipv4_fib_lookup(dev_net(ctx->rxq->dev), params,
6125 #if IS_ENABLED(CONFIG_IPV6)
6127 return bpf_ipv6_fib_lookup(dev_net(ctx->rxq->dev), params,
6131 return -EAFNOSUPPORT;
6134 static const struct bpf_func_proto bpf_xdp_fib_lookup_proto = {
6135 .func = bpf_xdp_fib_lookup,
6137 .ret_type = RET_INTEGER,
6138 .arg1_type = ARG_PTR_TO_CTX,
6139 .arg2_type = ARG_PTR_TO_MEM,
6140 .arg3_type = ARG_CONST_SIZE,
6141 .arg4_type = ARG_ANYTHING,
6144 BPF_CALL_4(bpf_skb_fib_lookup, struct sk_buff *, skb,
6145 struct bpf_fib_lookup *, params, int, plen, u32, flags)
6147 struct net *net = dev_net(skb->dev);
6148 int rc = -EAFNOSUPPORT;
6149 bool check_mtu = false;
6151 if (plen < sizeof(*params))
6154 if (flags & ~BPF_FIB_LOOKUP_MASK)
6157 if (params->tot_len)
6160 switch (params->family) {
6161 #if IS_ENABLED(CONFIG_INET)
6163 rc = bpf_ipv4_fib_lookup(net, params, flags, check_mtu);
6166 #if IS_ENABLED(CONFIG_IPV6)
6168 rc = bpf_ipv6_fib_lookup(net, params, flags, check_mtu);
6173 if (rc == BPF_FIB_LKUP_RET_SUCCESS && !check_mtu) {
6174 struct net_device *dev;
6176 /* When tot_len isn't provided by user, check skb
6177 * against MTU of FIB lookup resulting net_device
6179 dev = dev_get_by_index_rcu(net, params->ifindex);
6180 if (!is_skb_forwardable(dev, skb))
6181 rc = BPF_FIB_LKUP_RET_FRAG_NEEDED;
6183 params->mtu_result = dev->mtu; /* union with tot_len */
6189 static const struct bpf_func_proto bpf_skb_fib_lookup_proto = {
6190 .func = bpf_skb_fib_lookup,
6192 .ret_type = RET_INTEGER,
6193 .arg1_type = ARG_PTR_TO_CTX,
6194 .arg2_type = ARG_PTR_TO_MEM,
6195 .arg3_type = ARG_CONST_SIZE,
6196 .arg4_type = ARG_ANYTHING,
6199 static struct net_device *__dev_via_ifindex(struct net_device *dev_curr,
6202 struct net *netns = dev_net(dev_curr);
6204 /* Non-redirect use-cases can use ifindex=0 and save ifindex lookup */
6208 return dev_get_by_index_rcu(netns, ifindex);
6211 BPF_CALL_5(bpf_skb_check_mtu, struct sk_buff *, skb,
6212 u32, ifindex, u32 *, mtu_len, s32, len_diff, u64, flags)
6214 int ret = BPF_MTU_CHK_RET_FRAG_NEEDED;
6215 struct net_device *dev = skb->dev;
6216 int skb_len, dev_len;
6219 if (unlikely(flags & ~(BPF_MTU_CHK_SEGS)))
6222 if (unlikely(flags & BPF_MTU_CHK_SEGS && (len_diff || *mtu_len)))
6225 dev = __dev_via_ifindex(dev, ifindex);
6229 mtu = READ_ONCE(dev->mtu);
6231 dev_len = mtu + dev->hard_header_len;
6233 /* If set use *mtu_len as input, L3 as iph->tot_len (like fib_lookup) */
6234 skb_len = *mtu_len ? *mtu_len + dev->hard_header_len : skb->len;
6236 skb_len += len_diff; /* minus result pass check */
6237 if (skb_len <= dev_len) {
6238 ret = BPF_MTU_CHK_RET_SUCCESS;
6241 /* At this point, skb->len exceed MTU, but as it include length of all
6242 * segments, it can still be below MTU. The SKB can possibly get
6243 * re-segmented in transmit path (see validate_xmit_skb). Thus, user
6244 * must choose if segs are to be MTU checked.
6246 if (skb_is_gso(skb)) {
6247 ret = BPF_MTU_CHK_RET_SUCCESS;
6249 if (flags & BPF_MTU_CHK_SEGS &&
6250 !skb_gso_validate_network_len(skb, mtu))
6251 ret = BPF_MTU_CHK_RET_SEGS_TOOBIG;
6254 /* BPF verifier guarantees valid pointer */
6260 BPF_CALL_5(bpf_xdp_check_mtu, struct xdp_buff *, xdp,
6261 u32, ifindex, u32 *, mtu_len, s32, len_diff, u64, flags)
6263 struct net_device *dev = xdp->rxq->dev;
6264 int xdp_len = xdp->data_end - xdp->data;
6265 int ret = BPF_MTU_CHK_RET_SUCCESS;
6268 /* XDP variant doesn't support multi-buffer segment check (yet) */
6269 if (unlikely(flags))
6272 dev = __dev_via_ifindex(dev, ifindex);
6276 mtu = READ_ONCE(dev->mtu);
6278 /* Add L2-header as dev MTU is L3 size */
6279 dev_len = mtu + dev->hard_header_len;
6281 /* Use *mtu_len as input, L3 as iph->tot_len (like fib_lookup) */
6283 xdp_len = *mtu_len + dev->hard_header_len;
6285 xdp_len += len_diff; /* minus result pass check */
6286 if (xdp_len > dev_len)
6287 ret = BPF_MTU_CHK_RET_FRAG_NEEDED;
6289 /* BPF verifier guarantees valid pointer */
6295 static const struct bpf_func_proto bpf_skb_check_mtu_proto = {
6296 .func = bpf_skb_check_mtu,
6298 .ret_type = RET_INTEGER,
6299 .arg1_type = ARG_PTR_TO_CTX,
6300 .arg2_type = ARG_ANYTHING,
6301 .arg3_type = ARG_PTR_TO_INT,
6302 .arg4_type = ARG_ANYTHING,
6303 .arg5_type = ARG_ANYTHING,
6306 static const struct bpf_func_proto bpf_xdp_check_mtu_proto = {
6307 .func = bpf_xdp_check_mtu,
6309 .ret_type = RET_INTEGER,
6310 .arg1_type = ARG_PTR_TO_CTX,
6311 .arg2_type = ARG_ANYTHING,
6312 .arg3_type = ARG_PTR_TO_INT,
6313 .arg4_type = ARG_ANYTHING,
6314 .arg5_type = ARG_ANYTHING,
6317 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
6318 static int bpf_push_seg6_encap(struct sk_buff *skb, u32 type, void *hdr, u32 len)
6321 struct ipv6_sr_hdr *srh = (struct ipv6_sr_hdr *)hdr;
6323 if (!seg6_validate_srh(srh, len, false))
6327 case BPF_LWT_ENCAP_SEG6_INLINE:
6328 if (skb->protocol != htons(ETH_P_IPV6))
6331 err = seg6_do_srh_inline(skb, srh);
6333 case BPF_LWT_ENCAP_SEG6:
6334 skb_reset_inner_headers(skb);
6335 skb->encapsulation = 1;
6336 err = seg6_do_srh_encap(skb, srh, IPPROTO_IPV6);
6342 bpf_compute_data_pointers(skb);
6346 skb_set_transport_header(skb, sizeof(struct ipv6hdr));
6348 return seg6_lookup_nexthop(skb, NULL, 0);
6350 #endif /* CONFIG_IPV6_SEG6_BPF */
6352 #if IS_ENABLED(CONFIG_LWTUNNEL_BPF)
6353 static int bpf_push_ip_encap(struct sk_buff *skb, void *hdr, u32 len,
6356 return bpf_lwt_push_ip_encap(skb, hdr, len, ingress);
6360 BPF_CALL_4(bpf_lwt_in_push_encap, struct sk_buff *, skb, u32, type, void *, hdr,
6364 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
6365 case BPF_LWT_ENCAP_SEG6:
6366 case BPF_LWT_ENCAP_SEG6_INLINE:
6367 return bpf_push_seg6_encap(skb, type, hdr, len);
6369 #if IS_ENABLED(CONFIG_LWTUNNEL_BPF)
6370 case BPF_LWT_ENCAP_IP:
6371 return bpf_push_ip_encap(skb, hdr, len, true /* ingress */);
6378 BPF_CALL_4(bpf_lwt_xmit_push_encap, struct sk_buff *, skb, u32, type,
6379 void *, hdr, u32, len)
6382 #if IS_ENABLED(CONFIG_LWTUNNEL_BPF)
6383 case BPF_LWT_ENCAP_IP:
6384 return bpf_push_ip_encap(skb, hdr, len, false /* egress */);
6391 static const struct bpf_func_proto bpf_lwt_in_push_encap_proto = {
6392 .func = bpf_lwt_in_push_encap,
6394 .ret_type = RET_INTEGER,
6395 .arg1_type = ARG_PTR_TO_CTX,
6396 .arg2_type = ARG_ANYTHING,
6397 .arg3_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6398 .arg4_type = ARG_CONST_SIZE
6401 static const struct bpf_func_proto bpf_lwt_xmit_push_encap_proto = {
6402 .func = bpf_lwt_xmit_push_encap,
6404 .ret_type = RET_INTEGER,
6405 .arg1_type = ARG_PTR_TO_CTX,
6406 .arg2_type = ARG_ANYTHING,
6407 .arg3_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6408 .arg4_type = ARG_CONST_SIZE
6411 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
6412 BPF_CALL_4(bpf_lwt_seg6_store_bytes, struct sk_buff *, skb, u32, offset,
6413 const void *, from, u32, len)
6415 struct seg6_bpf_srh_state *srh_state =
6416 this_cpu_ptr(&seg6_bpf_srh_states);
6417 struct ipv6_sr_hdr *srh = srh_state->srh;
6418 void *srh_tlvs, *srh_end, *ptr;
6424 srh_tlvs = (void *)((char *)srh + ((srh->first_segment + 1) << 4));
6425 srh_end = (void *)((char *)srh + sizeof(*srh) + srh_state->hdrlen);
6427 ptr = skb->data + offset;
6428 if (ptr >= srh_tlvs && ptr + len <= srh_end)
6429 srh_state->valid = false;
6430 else if (ptr < (void *)&srh->flags ||
6431 ptr + len > (void *)&srh->segments)
6434 if (unlikely(bpf_try_make_writable(skb, offset + len)))
6436 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0)
6438 srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
6440 memcpy(skb->data + offset, from, len);
6444 static const struct bpf_func_proto bpf_lwt_seg6_store_bytes_proto = {
6445 .func = bpf_lwt_seg6_store_bytes,
6447 .ret_type = RET_INTEGER,
6448 .arg1_type = ARG_PTR_TO_CTX,
6449 .arg2_type = ARG_ANYTHING,
6450 .arg3_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6451 .arg4_type = ARG_CONST_SIZE
6454 static void bpf_update_srh_state(struct sk_buff *skb)
6456 struct seg6_bpf_srh_state *srh_state =
6457 this_cpu_ptr(&seg6_bpf_srh_states);
6460 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0) {
6461 srh_state->srh = NULL;
6463 srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
6464 srh_state->hdrlen = srh_state->srh->hdrlen << 3;
6465 srh_state->valid = true;
6469 BPF_CALL_4(bpf_lwt_seg6_action, struct sk_buff *, skb,
6470 u32, action, void *, param, u32, param_len)
6472 struct seg6_bpf_srh_state *srh_state =
6473 this_cpu_ptr(&seg6_bpf_srh_states);
6478 case SEG6_LOCAL_ACTION_END_X:
6479 if (!seg6_bpf_has_valid_srh(skb))
6481 if (param_len != sizeof(struct in6_addr))
6483 return seg6_lookup_nexthop(skb, (struct in6_addr *)param, 0);
6484 case SEG6_LOCAL_ACTION_END_T:
6485 if (!seg6_bpf_has_valid_srh(skb))
6487 if (param_len != sizeof(int))
6489 return seg6_lookup_nexthop(skb, NULL, *(int *)param);
6490 case SEG6_LOCAL_ACTION_END_DT6:
6491 if (!seg6_bpf_has_valid_srh(skb))
6493 if (param_len != sizeof(int))
6496 if (ipv6_find_hdr(skb, &hdroff, IPPROTO_IPV6, NULL, NULL) < 0)
6498 if (!pskb_pull(skb, hdroff))
6501 skb_postpull_rcsum(skb, skb_network_header(skb), hdroff);
6502 skb_reset_network_header(skb);
6503 skb_reset_transport_header(skb);
6504 skb->encapsulation = 0;
6506 bpf_compute_data_pointers(skb);
6507 bpf_update_srh_state(skb);
6508 return seg6_lookup_nexthop(skb, NULL, *(int *)param);
6509 case SEG6_LOCAL_ACTION_END_B6:
6510 if (srh_state->srh && !seg6_bpf_has_valid_srh(skb))
6512 err = bpf_push_seg6_encap(skb, BPF_LWT_ENCAP_SEG6_INLINE,
6515 bpf_update_srh_state(skb);
6518 case SEG6_LOCAL_ACTION_END_B6_ENCAP:
6519 if (srh_state->srh && !seg6_bpf_has_valid_srh(skb))
6521 err = bpf_push_seg6_encap(skb, BPF_LWT_ENCAP_SEG6,
6524 bpf_update_srh_state(skb);
6532 static const struct bpf_func_proto bpf_lwt_seg6_action_proto = {
6533 .func = bpf_lwt_seg6_action,
6535 .ret_type = RET_INTEGER,
6536 .arg1_type = ARG_PTR_TO_CTX,
6537 .arg2_type = ARG_ANYTHING,
6538 .arg3_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6539 .arg4_type = ARG_CONST_SIZE
6542 BPF_CALL_3(bpf_lwt_seg6_adjust_srh, struct sk_buff *, skb, u32, offset,
6545 struct seg6_bpf_srh_state *srh_state =
6546 this_cpu_ptr(&seg6_bpf_srh_states);
6547 struct ipv6_sr_hdr *srh = srh_state->srh;
6548 void *srh_end, *srh_tlvs, *ptr;
6549 struct ipv6hdr *hdr;
6553 if (unlikely(srh == NULL))
6556 srh_tlvs = (void *)((unsigned char *)srh + sizeof(*srh) +
6557 ((srh->first_segment + 1) << 4));
6558 srh_end = (void *)((unsigned char *)srh + sizeof(*srh) +
6560 ptr = skb->data + offset;
6562 if (unlikely(ptr < srh_tlvs || ptr > srh_end))
6564 if (unlikely(len < 0 && (void *)((char *)ptr - len) > srh_end))
6568 ret = skb_cow_head(skb, len);
6569 if (unlikely(ret < 0))
6572 ret = bpf_skb_net_hdr_push(skb, offset, len);
6574 ret = bpf_skb_net_hdr_pop(skb, offset, -1 * len);
6577 bpf_compute_data_pointers(skb);
6578 if (unlikely(ret < 0))
6581 hdr = (struct ipv6hdr *)skb->data;
6582 hdr->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
6584 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0)
6586 srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
6587 srh_state->hdrlen += len;
6588 srh_state->valid = false;
6592 static const struct bpf_func_proto bpf_lwt_seg6_adjust_srh_proto = {
6593 .func = bpf_lwt_seg6_adjust_srh,
6595 .ret_type = RET_INTEGER,
6596 .arg1_type = ARG_PTR_TO_CTX,
6597 .arg2_type = ARG_ANYTHING,
6598 .arg3_type = ARG_ANYTHING,
6600 #endif /* CONFIG_IPV6_SEG6_BPF */
6603 static struct sock *sk_lookup(struct net *net, struct bpf_sock_tuple *tuple,
6604 int dif, int sdif, u8 family, u8 proto)
6606 struct inet_hashinfo *hinfo = net->ipv4.tcp_death_row.hashinfo;
6607 bool refcounted = false;
6608 struct sock *sk = NULL;
6610 if (family == AF_INET) {
6611 __be32 src4 = tuple->ipv4.saddr;
6612 __be32 dst4 = tuple->ipv4.daddr;
6614 if (proto == IPPROTO_TCP)
6615 sk = __inet_lookup(net, hinfo, NULL, 0,
6616 src4, tuple->ipv4.sport,
6617 dst4, tuple->ipv4.dport,
6618 dif, sdif, &refcounted);
6620 sk = __udp4_lib_lookup(net, src4, tuple->ipv4.sport,
6621 dst4, tuple->ipv4.dport,
6622 dif, sdif, net->ipv4.udp_table, NULL);
6623 #if IS_ENABLED(CONFIG_IPV6)
6625 struct in6_addr *src6 = (struct in6_addr *)&tuple->ipv6.saddr;
6626 struct in6_addr *dst6 = (struct in6_addr *)&tuple->ipv6.daddr;
6628 if (proto == IPPROTO_TCP)
6629 sk = __inet6_lookup(net, hinfo, NULL, 0,
6630 src6, tuple->ipv6.sport,
6631 dst6, ntohs(tuple->ipv6.dport),
6632 dif, sdif, &refcounted);
6633 else if (likely(ipv6_bpf_stub))
6634 sk = ipv6_bpf_stub->udp6_lib_lookup(net,
6635 src6, tuple->ipv6.sport,
6636 dst6, tuple->ipv6.dport,
6638 net->ipv4.udp_table, NULL);
6642 if (unlikely(sk && !refcounted && !sock_flag(sk, SOCK_RCU_FREE))) {
6643 WARN_ONCE(1, "Found non-RCU, unreferenced socket!");
6649 /* bpf_skc_lookup performs the core lookup for different types of sockets,
6650 * taking a reference on the socket if it doesn't have the flag SOCK_RCU_FREE.
6652 static struct sock *
6653 __bpf_skc_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
6654 struct net *caller_net, u32 ifindex, u8 proto, u64 netns_id,
6655 u64 flags, int sdif)
6657 struct sock *sk = NULL;
6661 if (len == sizeof(tuple->ipv4))
6663 else if (len == sizeof(tuple->ipv6))
6668 if (unlikely(flags || !((s32)netns_id < 0 || netns_id <= S32_MAX)))
6672 if (family == AF_INET)
6673 sdif = inet_sdif(skb);
6675 sdif = inet6_sdif(skb);
6678 if ((s32)netns_id < 0) {
6680 sk = sk_lookup(net, tuple, ifindex, sdif, family, proto);
6682 net = get_net_ns_by_id(caller_net, netns_id);
6685 sk = sk_lookup(net, tuple, ifindex, sdif, family, proto);
6693 static struct sock *
6694 __bpf_sk_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
6695 struct net *caller_net, u32 ifindex, u8 proto, u64 netns_id,
6696 u64 flags, int sdif)
6698 struct sock *sk = __bpf_skc_lookup(skb, tuple, len, caller_net,
6699 ifindex, proto, netns_id, flags,
6703 struct sock *sk2 = sk_to_full_sk(sk);
6705 /* sk_to_full_sk() may return (sk)->rsk_listener, so make sure the original sk
6706 * sock refcnt is decremented to prevent a request_sock leak.
6708 if (!sk_fullsock(sk2))
6712 /* Ensure there is no need to bump sk2 refcnt */
6713 if (unlikely(sk2 && !sock_flag(sk2, SOCK_RCU_FREE))) {
6714 WARN_ONCE(1, "Found non-RCU, unreferenced socket!");
6724 static struct sock *
6725 bpf_skc_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
6726 u8 proto, u64 netns_id, u64 flags)
6728 struct net *caller_net;
6732 caller_net = dev_net(skb->dev);
6733 ifindex = skb->dev->ifindex;
6735 caller_net = sock_net(skb->sk);
6739 return __bpf_skc_lookup(skb, tuple, len, caller_net, ifindex, proto,
6740 netns_id, flags, -1);
6743 static struct sock *
6744 bpf_sk_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
6745 u8 proto, u64 netns_id, u64 flags)
6747 struct sock *sk = bpf_skc_lookup(skb, tuple, len, proto, netns_id,
6751 struct sock *sk2 = sk_to_full_sk(sk);
6753 /* sk_to_full_sk() may return (sk)->rsk_listener, so make sure the original sk
6754 * sock refcnt is decremented to prevent a request_sock leak.
6756 if (!sk_fullsock(sk2))
6760 /* Ensure there is no need to bump sk2 refcnt */
6761 if (unlikely(sk2 && !sock_flag(sk2, SOCK_RCU_FREE))) {
6762 WARN_ONCE(1, "Found non-RCU, unreferenced socket!");
6772 BPF_CALL_5(bpf_skc_lookup_tcp, struct sk_buff *, skb,
6773 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
6775 return (unsigned long)bpf_skc_lookup(skb, tuple, len, IPPROTO_TCP,
6779 static const struct bpf_func_proto bpf_skc_lookup_tcp_proto = {
6780 .func = bpf_skc_lookup_tcp,
6783 .ret_type = RET_PTR_TO_SOCK_COMMON_OR_NULL,
6784 .arg1_type = ARG_PTR_TO_CTX,
6785 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6786 .arg3_type = ARG_CONST_SIZE,
6787 .arg4_type = ARG_ANYTHING,
6788 .arg5_type = ARG_ANYTHING,
6791 BPF_CALL_5(bpf_sk_lookup_tcp, struct sk_buff *, skb,
6792 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
6794 return (unsigned long)bpf_sk_lookup(skb, tuple, len, IPPROTO_TCP,
6798 static const struct bpf_func_proto bpf_sk_lookup_tcp_proto = {
6799 .func = bpf_sk_lookup_tcp,
6802 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
6803 .arg1_type = ARG_PTR_TO_CTX,
6804 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6805 .arg3_type = ARG_CONST_SIZE,
6806 .arg4_type = ARG_ANYTHING,
6807 .arg5_type = ARG_ANYTHING,
6810 BPF_CALL_5(bpf_sk_lookup_udp, struct sk_buff *, skb,
6811 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
6813 return (unsigned long)bpf_sk_lookup(skb, tuple, len, IPPROTO_UDP,
6817 static const struct bpf_func_proto bpf_sk_lookup_udp_proto = {
6818 .func = bpf_sk_lookup_udp,
6821 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
6822 .arg1_type = ARG_PTR_TO_CTX,
6823 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6824 .arg3_type = ARG_CONST_SIZE,
6825 .arg4_type = ARG_ANYTHING,
6826 .arg5_type = ARG_ANYTHING,
6829 BPF_CALL_5(bpf_tc_skc_lookup_tcp, struct sk_buff *, skb,
6830 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
6832 struct net_device *dev = skb->dev;
6833 int ifindex = dev->ifindex, sdif = dev_sdif(dev);
6834 struct net *caller_net = dev_net(dev);
6836 return (unsigned long)__bpf_skc_lookup(skb, tuple, len, caller_net,
6837 ifindex, IPPROTO_TCP, netns_id,
6841 static const struct bpf_func_proto bpf_tc_skc_lookup_tcp_proto = {
6842 .func = bpf_tc_skc_lookup_tcp,
6845 .ret_type = RET_PTR_TO_SOCK_COMMON_OR_NULL,
6846 .arg1_type = ARG_PTR_TO_CTX,
6847 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6848 .arg3_type = ARG_CONST_SIZE,
6849 .arg4_type = ARG_ANYTHING,
6850 .arg5_type = ARG_ANYTHING,
6853 BPF_CALL_5(bpf_tc_sk_lookup_tcp, struct sk_buff *, skb,
6854 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
6856 struct net_device *dev = skb->dev;
6857 int ifindex = dev->ifindex, sdif = dev_sdif(dev);
6858 struct net *caller_net = dev_net(dev);
6860 return (unsigned long)__bpf_sk_lookup(skb, tuple, len, caller_net,
6861 ifindex, IPPROTO_TCP, netns_id,
6865 static const struct bpf_func_proto bpf_tc_sk_lookup_tcp_proto = {
6866 .func = bpf_tc_sk_lookup_tcp,
6869 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
6870 .arg1_type = ARG_PTR_TO_CTX,
6871 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6872 .arg3_type = ARG_CONST_SIZE,
6873 .arg4_type = ARG_ANYTHING,
6874 .arg5_type = ARG_ANYTHING,
6877 BPF_CALL_5(bpf_tc_sk_lookup_udp, struct sk_buff *, skb,
6878 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
6880 struct net_device *dev = skb->dev;
6881 int ifindex = dev->ifindex, sdif = dev_sdif(dev);
6882 struct net *caller_net = dev_net(dev);
6884 return (unsigned long)__bpf_sk_lookup(skb, tuple, len, caller_net,
6885 ifindex, IPPROTO_UDP, netns_id,
6889 static const struct bpf_func_proto bpf_tc_sk_lookup_udp_proto = {
6890 .func = bpf_tc_sk_lookup_udp,
6893 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
6894 .arg1_type = ARG_PTR_TO_CTX,
6895 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6896 .arg3_type = ARG_CONST_SIZE,
6897 .arg4_type = ARG_ANYTHING,
6898 .arg5_type = ARG_ANYTHING,
6901 BPF_CALL_1(bpf_sk_release, struct sock *, sk)
6903 if (sk && sk_is_refcounted(sk))
6908 static const struct bpf_func_proto bpf_sk_release_proto = {
6909 .func = bpf_sk_release,
6911 .ret_type = RET_INTEGER,
6912 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON | OBJ_RELEASE,
6915 BPF_CALL_5(bpf_xdp_sk_lookup_udp, struct xdp_buff *, ctx,
6916 struct bpf_sock_tuple *, tuple, u32, len, u32, netns_id, u64, flags)
6918 struct net_device *dev = ctx->rxq->dev;
6919 int ifindex = dev->ifindex, sdif = dev_sdif(dev);
6920 struct net *caller_net = dev_net(dev);
6922 return (unsigned long)__bpf_sk_lookup(NULL, tuple, len, caller_net,
6923 ifindex, IPPROTO_UDP, netns_id,
6927 static const struct bpf_func_proto bpf_xdp_sk_lookup_udp_proto = {
6928 .func = bpf_xdp_sk_lookup_udp,
6931 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
6932 .arg1_type = ARG_PTR_TO_CTX,
6933 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6934 .arg3_type = ARG_CONST_SIZE,
6935 .arg4_type = ARG_ANYTHING,
6936 .arg5_type = ARG_ANYTHING,
6939 BPF_CALL_5(bpf_xdp_skc_lookup_tcp, struct xdp_buff *, ctx,
6940 struct bpf_sock_tuple *, tuple, u32, len, u32, netns_id, u64, flags)
6942 struct net_device *dev = ctx->rxq->dev;
6943 int ifindex = dev->ifindex, sdif = dev_sdif(dev);
6944 struct net *caller_net = dev_net(dev);
6946 return (unsigned long)__bpf_skc_lookup(NULL, tuple, len, caller_net,
6947 ifindex, IPPROTO_TCP, netns_id,
6951 static const struct bpf_func_proto bpf_xdp_skc_lookup_tcp_proto = {
6952 .func = bpf_xdp_skc_lookup_tcp,
6955 .ret_type = RET_PTR_TO_SOCK_COMMON_OR_NULL,
6956 .arg1_type = ARG_PTR_TO_CTX,
6957 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6958 .arg3_type = ARG_CONST_SIZE,
6959 .arg4_type = ARG_ANYTHING,
6960 .arg5_type = ARG_ANYTHING,
6963 BPF_CALL_5(bpf_xdp_sk_lookup_tcp, struct xdp_buff *, ctx,
6964 struct bpf_sock_tuple *, tuple, u32, len, u32, netns_id, u64, flags)
6966 struct net_device *dev = ctx->rxq->dev;
6967 int ifindex = dev->ifindex, sdif = dev_sdif(dev);
6968 struct net *caller_net = dev_net(dev);
6970 return (unsigned long)__bpf_sk_lookup(NULL, tuple, len, caller_net,
6971 ifindex, IPPROTO_TCP, netns_id,
6975 static const struct bpf_func_proto bpf_xdp_sk_lookup_tcp_proto = {
6976 .func = bpf_xdp_sk_lookup_tcp,
6979 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
6980 .arg1_type = ARG_PTR_TO_CTX,
6981 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6982 .arg3_type = ARG_CONST_SIZE,
6983 .arg4_type = ARG_ANYTHING,
6984 .arg5_type = ARG_ANYTHING,
6987 BPF_CALL_5(bpf_sock_addr_skc_lookup_tcp, struct bpf_sock_addr_kern *, ctx,
6988 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
6990 return (unsigned long)__bpf_skc_lookup(NULL, tuple, len,
6991 sock_net(ctx->sk), 0,
6992 IPPROTO_TCP, netns_id, flags,
6996 static const struct bpf_func_proto bpf_sock_addr_skc_lookup_tcp_proto = {
6997 .func = bpf_sock_addr_skc_lookup_tcp,
6999 .ret_type = RET_PTR_TO_SOCK_COMMON_OR_NULL,
7000 .arg1_type = ARG_PTR_TO_CTX,
7001 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
7002 .arg3_type = ARG_CONST_SIZE,
7003 .arg4_type = ARG_ANYTHING,
7004 .arg5_type = ARG_ANYTHING,
7007 BPF_CALL_5(bpf_sock_addr_sk_lookup_tcp, struct bpf_sock_addr_kern *, ctx,
7008 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
7010 return (unsigned long)__bpf_sk_lookup(NULL, tuple, len,
7011 sock_net(ctx->sk), 0, IPPROTO_TCP,
7012 netns_id, flags, -1);
7015 static const struct bpf_func_proto bpf_sock_addr_sk_lookup_tcp_proto = {
7016 .func = bpf_sock_addr_sk_lookup_tcp,
7018 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
7019 .arg1_type = ARG_PTR_TO_CTX,
7020 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
7021 .arg3_type = ARG_CONST_SIZE,
7022 .arg4_type = ARG_ANYTHING,
7023 .arg5_type = ARG_ANYTHING,
7026 BPF_CALL_5(bpf_sock_addr_sk_lookup_udp, struct bpf_sock_addr_kern *, ctx,
7027 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
7029 return (unsigned long)__bpf_sk_lookup(NULL, tuple, len,
7030 sock_net(ctx->sk), 0, IPPROTO_UDP,
7031 netns_id, flags, -1);
7034 static const struct bpf_func_proto bpf_sock_addr_sk_lookup_udp_proto = {
7035 .func = bpf_sock_addr_sk_lookup_udp,
7037 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
7038 .arg1_type = ARG_PTR_TO_CTX,
7039 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
7040 .arg3_type = ARG_CONST_SIZE,
7041 .arg4_type = ARG_ANYTHING,
7042 .arg5_type = ARG_ANYTHING,
7045 bool bpf_tcp_sock_is_valid_access(int off, int size, enum bpf_access_type type,
7046 struct bpf_insn_access_aux *info)
7048 if (off < 0 || off >= offsetofend(struct bpf_tcp_sock,
7052 if (off % size != 0)
7056 case offsetof(struct bpf_tcp_sock, bytes_received):
7057 case offsetof(struct bpf_tcp_sock, bytes_acked):
7058 return size == sizeof(__u64);
7060 return size == sizeof(__u32);
7064 u32 bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type,
7065 const struct bpf_insn *si,
7066 struct bpf_insn *insn_buf,
7067 struct bpf_prog *prog, u32 *target_size)
7069 struct bpf_insn *insn = insn_buf;
7071 #define BPF_TCP_SOCK_GET_COMMON(FIELD) \
7073 BUILD_BUG_ON(sizeof_field(struct tcp_sock, FIELD) > \
7074 sizeof_field(struct bpf_tcp_sock, FIELD)); \
7075 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct tcp_sock, FIELD),\
7076 si->dst_reg, si->src_reg, \
7077 offsetof(struct tcp_sock, FIELD)); \
7080 #define BPF_INET_SOCK_GET_COMMON(FIELD) \
7082 BUILD_BUG_ON(sizeof_field(struct inet_connection_sock, \
7084 sizeof_field(struct bpf_tcp_sock, FIELD)); \
7085 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
7086 struct inet_connection_sock, \
7088 si->dst_reg, si->src_reg, \
7090 struct inet_connection_sock, \
7094 BTF_TYPE_EMIT(struct bpf_tcp_sock);
7097 case offsetof(struct bpf_tcp_sock, rtt_min):
7098 BUILD_BUG_ON(sizeof_field(struct tcp_sock, rtt_min) !=
7099 sizeof(struct minmax));
7100 BUILD_BUG_ON(sizeof(struct minmax) <
7101 sizeof(struct minmax_sample));
7103 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
7104 offsetof(struct tcp_sock, rtt_min) +
7105 offsetof(struct minmax_sample, v));
7107 case offsetof(struct bpf_tcp_sock, snd_cwnd):
7108 BPF_TCP_SOCK_GET_COMMON(snd_cwnd);
7110 case offsetof(struct bpf_tcp_sock, srtt_us):
7111 BPF_TCP_SOCK_GET_COMMON(srtt_us);
7113 case offsetof(struct bpf_tcp_sock, snd_ssthresh):
7114 BPF_TCP_SOCK_GET_COMMON(snd_ssthresh);
7116 case offsetof(struct bpf_tcp_sock, rcv_nxt):
7117 BPF_TCP_SOCK_GET_COMMON(rcv_nxt);
7119 case offsetof(struct bpf_tcp_sock, snd_nxt):
7120 BPF_TCP_SOCK_GET_COMMON(snd_nxt);
7122 case offsetof(struct bpf_tcp_sock, snd_una):
7123 BPF_TCP_SOCK_GET_COMMON(snd_una);
7125 case offsetof(struct bpf_tcp_sock, mss_cache):
7126 BPF_TCP_SOCK_GET_COMMON(mss_cache);
7128 case offsetof(struct bpf_tcp_sock, ecn_flags):
7129 BPF_TCP_SOCK_GET_COMMON(ecn_flags);
7131 case offsetof(struct bpf_tcp_sock, rate_delivered):
7132 BPF_TCP_SOCK_GET_COMMON(rate_delivered);
7134 case offsetof(struct bpf_tcp_sock, rate_interval_us):
7135 BPF_TCP_SOCK_GET_COMMON(rate_interval_us);
7137 case offsetof(struct bpf_tcp_sock, packets_out):
7138 BPF_TCP_SOCK_GET_COMMON(packets_out);
7140 case offsetof(struct bpf_tcp_sock, retrans_out):
7141 BPF_TCP_SOCK_GET_COMMON(retrans_out);
7143 case offsetof(struct bpf_tcp_sock, total_retrans):
7144 BPF_TCP_SOCK_GET_COMMON(total_retrans);
7146 case offsetof(struct bpf_tcp_sock, segs_in):
7147 BPF_TCP_SOCK_GET_COMMON(segs_in);
7149 case offsetof(struct bpf_tcp_sock, data_segs_in):
7150 BPF_TCP_SOCK_GET_COMMON(data_segs_in);
7152 case offsetof(struct bpf_tcp_sock, segs_out):
7153 BPF_TCP_SOCK_GET_COMMON(segs_out);
7155 case offsetof(struct bpf_tcp_sock, data_segs_out):
7156 BPF_TCP_SOCK_GET_COMMON(data_segs_out);
7158 case offsetof(struct bpf_tcp_sock, lost_out):
7159 BPF_TCP_SOCK_GET_COMMON(lost_out);
7161 case offsetof(struct bpf_tcp_sock, sacked_out):
7162 BPF_TCP_SOCK_GET_COMMON(sacked_out);
7164 case offsetof(struct bpf_tcp_sock, bytes_received):
7165 BPF_TCP_SOCK_GET_COMMON(bytes_received);
7167 case offsetof(struct bpf_tcp_sock, bytes_acked):
7168 BPF_TCP_SOCK_GET_COMMON(bytes_acked);
7170 case offsetof(struct bpf_tcp_sock, dsack_dups):
7171 BPF_TCP_SOCK_GET_COMMON(dsack_dups);
7173 case offsetof(struct bpf_tcp_sock, delivered):
7174 BPF_TCP_SOCK_GET_COMMON(delivered);
7176 case offsetof(struct bpf_tcp_sock, delivered_ce):
7177 BPF_TCP_SOCK_GET_COMMON(delivered_ce);
7179 case offsetof(struct bpf_tcp_sock, icsk_retransmits):
7180 BPF_INET_SOCK_GET_COMMON(icsk_retransmits);
7184 return insn - insn_buf;
7187 BPF_CALL_1(bpf_tcp_sock, struct sock *, sk)
7189 if (sk_fullsock(sk) && sk->sk_protocol == IPPROTO_TCP)
7190 return (unsigned long)sk;
7192 return (unsigned long)NULL;
7195 const struct bpf_func_proto bpf_tcp_sock_proto = {
7196 .func = bpf_tcp_sock,
7198 .ret_type = RET_PTR_TO_TCP_SOCK_OR_NULL,
7199 .arg1_type = ARG_PTR_TO_SOCK_COMMON,
7202 BPF_CALL_1(bpf_get_listener_sock, struct sock *, sk)
7204 sk = sk_to_full_sk(sk);
7206 if (sk->sk_state == TCP_LISTEN && sock_flag(sk, SOCK_RCU_FREE))
7207 return (unsigned long)sk;
7209 return (unsigned long)NULL;
7212 static const struct bpf_func_proto bpf_get_listener_sock_proto = {
7213 .func = bpf_get_listener_sock,
7215 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
7216 .arg1_type = ARG_PTR_TO_SOCK_COMMON,
7219 BPF_CALL_1(bpf_skb_ecn_set_ce, struct sk_buff *, skb)
7221 unsigned int iphdr_len;
7223 switch (skb_protocol(skb, true)) {
7224 case cpu_to_be16(ETH_P_IP):
7225 iphdr_len = sizeof(struct iphdr);
7227 case cpu_to_be16(ETH_P_IPV6):
7228 iphdr_len = sizeof(struct ipv6hdr);
7234 if (skb_headlen(skb) < iphdr_len)
7237 if (skb_cloned(skb) && !skb_clone_writable(skb, iphdr_len))
7240 return INET_ECN_set_ce(skb);
7243 bool bpf_xdp_sock_is_valid_access(int off, int size, enum bpf_access_type type,
7244 struct bpf_insn_access_aux *info)
7246 if (off < 0 || off >= offsetofend(struct bpf_xdp_sock, queue_id))
7249 if (off % size != 0)
7254 return size == sizeof(__u32);
7258 u32 bpf_xdp_sock_convert_ctx_access(enum bpf_access_type type,
7259 const struct bpf_insn *si,
7260 struct bpf_insn *insn_buf,
7261 struct bpf_prog *prog, u32 *target_size)
7263 struct bpf_insn *insn = insn_buf;
7265 #define BPF_XDP_SOCK_GET(FIELD) \
7267 BUILD_BUG_ON(sizeof_field(struct xdp_sock, FIELD) > \
7268 sizeof_field(struct bpf_xdp_sock, FIELD)); \
7269 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_sock, FIELD),\
7270 si->dst_reg, si->src_reg, \
7271 offsetof(struct xdp_sock, FIELD)); \
7275 case offsetof(struct bpf_xdp_sock, queue_id):
7276 BPF_XDP_SOCK_GET(queue_id);
7280 return insn - insn_buf;
7283 static const struct bpf_func_proto bpf_skb_ecn_set_ce_proto = {
7284 .func = bpf_skb_ecn_set_ce,
7286 .ret_type = RET_INTEGER,
7287 .arg1_type = ARG_PTR_TO_CTX,
7290 BPF_CALL_5(bpf_tcp_check_syncookie, struct sock *, sk, void *, iph, u32, iph_len,
7291 struct tcphdr *, th, u32, th_len)
7293 #ifdef CONFIG_SYN_COOKIES
7296 if (unlikely(!sk || th_len < sizeof(*th)))
7299 /* sk_listener() allows TCP_NEW_SYN_RECV, which makes no sense here. */
7300 if (sk->sk_protocol != IPPROTO_TCP || sk->sk_state != TCP_LISTEN)
7303 if (!READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_syncookies))
7306 if (!th->ack || th->rst || th->syn)
7309 if (unlikely(iph_len < sizeof(struct iphdr)))
7312 if (tcp_synq_no_recent_overflow(sk))
7315 /* Both struct iphdr and struct ipv6hdr have the version field at the
7316 * same offset so we can cast to the shorter header (struct iphdr).
7318 switch (((struct iphdr *)iph)->version) {
7320 if (sk->sk_family == AF_INET6 && ipv6_only_sock(sk))
7323 ret = __cookie_v4_check((struct iphdr *)iph, th);
7326 #if IS_BUILTIN(CONFIG_IPV6)
7328 if (unlikely(iph_len < sizeof(struct ipv6hdr)))
7331 if (sk->sk_family != AF_INET6)
7334 ret = __cookie_v6_check((struct ipv6hdr *)iph, th);
7336 #endif /* CONFIG_IPV6 */
7339 return -EPROTONOSUPPORT;
7351 static const struct bpf_func_proto bpf_tcp_check_syncookie_proto = {
7352 .func = bpf_tcp_check_syncookie,
7355 .ret_type = RET_INTEGER,
7356 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
7357 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
7358 .arg3_type = ARG_CONST_SIZE,
7359 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
7360 .arg5_type = ARG_CONST_SIZE,
7363 BPF_CALL_5(bpf_tcp_gen_syncookie, struct sock *, sk, void *, iph, u32, iph_len,
7364 struct tcphdr *, th, u32, th_len)
7366 #ifdef CONFIG_SYN_COOKIES
7370 if (unlikely(!sk || th_len < sizeof(*th) || th_len != th->doff * 4))
7373 if (sk->sk_protocol != IPPROTO_TCP || sk->sk_state != TCP_LISTEN)
7376 if (!READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_syncookies))
7379 if (!th->syn || th->ack || th->fin || th->rst)
7382 if (unlikely(iph_len < sizeof(struct iphdr)))
7385 /* Both struct iphdr and struct ipv6hdr have the version field at the
7386 * same offset so we can cast to the shorter header (struct iphdr).
7388 switch (((struct iphdr *)iph)->version) {
7390 if (sk->sk_family == AF_INET6 && ipv6_only_sock(sk))
7393 mss = tcp_v4_get_syncookie(sk, iph, th, &cookie);
7396 #if IS_BUILTIN(CONFIG_IPV6)
7398 if (unlikely(iph_len < sizeof(struct ipv6hdr)))
7401 if (sk->sk_family != AF_INET6)
7404 mss = tcp_v6_get_syncookie(sk, iph, th, &cookie);
7406 #endif /* CONFIG_IPV6 */
7409 return -EPROTONOSUPPORT;
7414 return cookie | ((u64)mss << 32);
7417 #endif /* CONFIG_SYN_COOKIES */
7420 static const struct bpf_func_proto bpf_tcp_gen_syncookie_proto = {
7421 .func = bpf_tcp_gen_syncookie,
7422 .gpl_only = true, /* __cookie_v*_init_sequence() is GPL */
7424 .ret_type = RET_INTEGER,
7425 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
7426 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
7427 .arg3_type = ARG_CONST_SIZE,
7428 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
7429 .arg5_type = ARG_CONST_SIZE,
7432 BPF_CALL_3(bpf_sk_assign, struct sk_buff *, skb, struct sock *, sk, u64, flags)
7434 if (!sk || flags != 0)
7436 if (!skb_at_tc_ingress(skb))
7438 if (unlikely(dev_net(skb->dev) != sock_net(sk)))
7439 return -ENETUNREACH;
7440 if (sk_unhashed(sk))
7442 if (sk_is_refcounted(sk) &&
7443 unlikely(!refcount_inc_not_zero(&sk->sk_refcnt)))
7448 skb->destructor = sock_pfree;
7453 static const struct bpf_func_proto bpf_sk_assign_proto = {
7454 .func = bpf_sk_assign,
7456 .ret_type = RET_INTEGER,
7457 .arg1_type = ARG_PTR_TO_CTX,
7458 .arg2_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
7459 .arg3_type = ARG_ANYTHING,
7462 static const u8 *bpf_search_tcp_opt(const u8 *op, const u8 *opend,
7463 u8 search_kind, const u8 *magic,
7464 u8 magic_len, bool *eol)
7470 while (op < opend) {
7473 if (kind == TCPOPT_EOL) {
7475 return ERR_PTR(-ENOMSG);
7476 } else if (kind == TCPOPT_NOP) {
7481 if (opend - op < 2 || opend - op < op[1] || op[1] < 2)
7482 /* Something is wrong in the received header.
7483 * Follow the TCP stack's tcp_parse_options()
7484 * and just bail here.
7486 return ERR_PTR(-EFAULT);
7489 if (search_kind == kind) {
7493 if (magic_len > kind_len - 2)
7494 return ERR_PTR(-ENOMSG);
7496 if (!memcmp(&op[2], magic, magic_len))
7503 return ERR_PTR(-ENOMSG);
7506 BPF_CALL_4(bpf_sock_ops_load_hdr_opt, struct bpf_sock_ops_kern *, bpf_sock,
7507 void *, search_res, u32, len, u64, flags)
7509 bool eol, load_syn = flags & BPF_LOAD_HDR_OPT_TCP_SYN;
7510 const u8 *op, *opend, *magic, *search = search_res;
7511 u8 search_kind, search_len, copy_len, magic_len;
7514 /* 2 byte is the minimal option len except TCPOPT_NOP and
7515 * TCPOPT_EOL which are useless for the bpf prog to learn
7516 * and this helper disallow loading them also.
7518 if (len < 2 || flags & ~BPF_LOAD_HDR_OPT_TCP_SYN)
7521 search_kind = search[0];
7522 search_len = search[1];
7524 if (search_len > len || search_kind == TCPOPT_NOP ||
7525 search_kind == TCPOPT_EOL)
7528 if (search_kind == TCPOPT_EXP || search_kind == 253) {
7529 /* 16 or 32 bit magic. +2 for kind and kind length */
7530 if (search_len != 4 && search_len != 6)
7533 magic_len = search_len - 2;
7542 ret = bpf_sock_ops_get_syn(bpf_sock, TCP_BPF_SYN, &op);
7547 op += sizeof(struct tcphdr);
7549 if (!bpf_sock->skb ||
7550 bpf_sock->op == BPF_SOCK_OPS_HDR_OPT_LEN_CB)
7551 /* This bpf_sock->op cannot call this helper */
7554 opend = bpf_sock->skb_data_end;
7555 op = bpf_sock->skb->data + sizeof(struct tcphdr);
7558 op = bpf_search_tcp_opt(op, opend, search_kind, magic, magic_len,
7565 if (copy_len > len) {
7570 memcpy(search_res, op, copy_len);
7574 static const struct bpf_func_proto bpf_sock_ops_load_hdr_opt_proto = {
7575 .func = bpf_sock_ops_load_hdr_opt,
7577 .ret_type = RET_INTEGER,
7578 .arg1_type = ARG_PTR_TO_CTX,
7579 .arg2_type = ARG_PTR_TO_MEM,
7580 .arg3_type = ARG_CONST_SIZE,
7581 .arg4_type = ARG_ANYTHING,
7584 BPF_CALL_4(bpf_sock_ops_store_hdr_opt, struct bpf_sock_ops_kern *, bpf_sock,
7585 const void *, from, u32, len, u64, flags)
7587 u8 new_kind, new_kind_len, magic_len = 0, *opend;
7588 const u8 *op, *new_op, *magic = NULL;
7589 struct sk_buff *skb;
7592 if (bpf_sock->op != BPF_SOCK_OPS_WRITE_HDR_OPT_CB)
7595 if (len < 2 || flags)
7599 new_kind = new_op[0];
7600 new_kind_len = new_op[1];
7602 if (new_kind_len > len || new_kind == TCPOPT_NOP ||
7603 new_kind == TCPOPT_EOL)
7606 if (new_kind_len > bpf_sock->remaining_opt_len)
7609 /* 253 is another experimental kind */
7610 if (new_kind == TCPOPT_EXP || new_kind == 253) {
7611 if (new_kind_len < 4)
7613 /* Match for the 2 byte magic also.
7614 * RFC 6994: the magic could be 2 or 4 bytes.
7615 * Hence, matching by 2 byte only is on the
7616 * conservative side but it is the right
7617 * thing to do for the 'search-for-duplication'
7624 /* Check for duplication */
7625 skb = bpf_sock->skb;
7626 op = skb->data + sizeof(struct tcphdr);
7627 opend = bpf_sock->skb_data_end;
7629 op = bpf_search_tcp_opt(op, opend, new_kind, magic, magic_len,
7634 if (PTR_ERR(op) != -ENOMSG)
7638 /* The option has been ended. Treat it as no more
7639 * header option can be written.
7643 /* No duplication found. Store the header option. */
7644 memcpy(opend, from, new_kind_len);
7646 bpf_sock->remaining_opt_len -= new_kind_len;
7647 bpf_sock->skb_data_end += new_kind_len;
7652 static const struct bpf_func_proto bpf_sock_ops_store_hdr_opt_proto = {
7653 .func = bpf_sock_ops_store_hdr_opt,
7655 .ret_type = RET_INTEGER,
7656 .arg1_type = ARG_PTR_TO_CTX,
7657 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
7658 .arg3_type = ARG_CONST_SIZE,
7659 .arg4_type = ARG_ANYTHING,
7662 BPF_CALL_3(bpf_sock_ops_reserve_hdr_opt, struct bpf_sock_ops_kern *, bpf_sock,
7663 u32, len, u64, flags)
7665 if (bpf_sock->op != BPF_SOCK_OPS_HDR_OPT_LEN_CB)
7668 if (flags || len < 2)
7671 if (len > bpf_sock->remaining_opt_len)
7674 bpf_sock->remaining_opt_len -= len;
7679 static const struct bpf_func_proto bpf_sock_ops_reserve_hdr_opt_proto = {
7680 .func = bpf_sock_ops_reserve_hdr_opt,
7682 .ret_type = RET_INTEGER,
7683 .arg1_type = ARG_PTR_TO_CTX,
7684 .arg2_type = ARG_ANYTHING,
7685 .arg3_type = ARG_ANYTHING,
7688 BPF_CALL_3(bpf_skb_set_tstamp, struct sk_buff *, skb,
7689 u64, tstamp, u32, tstamp_type)
7691 /* skb_clear_delivery_time() is done for inet protocol */
7692 if (skb->protocol != htons(ETH_P_IP) &&
7693 skb->protocol != htons(ETH_P_IPV6))
7696 switch (tstamp_type) {
7697 case BPF_SKB_TSTAMP_DELIVERY_MONO:
7700 skb->tstamp = tstamp;
7701 skb->mono_delivery_time = 1;
7703 case BPF_SKB_TSTAMP_UNSPEC:
7707 skb->mono_delivery_time = 0;
7716 static const struct bpf_func_proto bpf_skb_set_tstamp_proto = {
7717 .func = bpf_skb_set_tstamp,
7719 .ret_type = RET_INTEGER,
7720 .arg1_type = ARG_PTR_TO_CTX,
7721 .arg2_type = ARG_ANYTHING,
7722 .arg3_type = ARG_ANYTHING,
7725 #ifdef CONFIG_SYN_COOKIES
7726 BPF_CALL_3(bpf_tcp_raw_gen_syncookie_ipv4, struct iphdr *, iph,
7727 struct tcphdr *, th, u32, th_len)
7732 if (unlikely(th_len < sizeof(*th) || th_len != th->doff * 4))
7735 mss = tcp_parse_mss_option(th, 0) ?: TCP_MSS_DEFAULT;
7736 cookie = __cookie_v4_init_sequence(iph, th, &mss);
7738 return cookie | ((u64)mss << 32);
7741 static const struct bpf_func_proto bpf_tcp_raw_gen_syncookie_ipv4_proto = {
7742 .func = bpf_tcp_raw_gen_syncookie_ipv4,
7743 .gpl_only = true, /* __cookie_v4_init_sequence() is GPL */
7745 .ret_type = RET_INTEGER,
7746 .arg1_type = ARG_PTR_TO_FIXED_SIZE_MEM,
7747 .arg1_size = sizeof(struct iphdr),
7748 .arg2_type = ARG_PTR_TO_MEM,
7749 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
7752 BPF_CALL_3(bpf_tcp_raw_gen_syncookie_ipv6, struct ipv6hdr *, iph,
7753 struct tcphdr *, th, u32, th_len)
7755 #if IS_BUILTIN(CONFIG_IPV6)
7756 const u16 mss_clamp = IPV6_MIN_MTU - sizeof(struct tcphdr) -
7757 sizeof(struct ipv6hdr);
7761 if (unlikely(th_len < sizeof(*th) || th_len != th->doff * 4))
7764 mss = tcp_parse_mss_option(th, 0) ?: mss_clamp;
7765 cookie = __cookie_v6_init_sequence(iph, th, &mss);
7767 return cookie | ((u64)mss << 32);
7769 return -EPROTONOSUPPORT;
7773 static const struct bpf_func_proto bpf_tcp_raw_gen_syncookie_ipv6_proto = {
7774 .func = bpf_tcp_raw_gen_syncookie_ipv6,
7775 .gpl_only = true, /* __cookie_v6_init_sequence() is GPL */
7777 .ret_type = RET_INTEGER,
7778 .arg1_type = ARG_PTR_TO_FIXED_SIZE_MEM,
7779 .arg1_size = sizeof(struct ipv6hdr),
7780 .arg2_type = ARG_PTR_TO_MEM,
7781 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
7784 BPF_CALL_2(bpf_tcp_raw_check_syncookie_ipv4, struct iphdr *, iph,
7785 struct tcphdr *, th)
7787 if (__cookie_v4_check(iph, th) > 0)
7793 static const struct bpf_func_proto bpf_tcp_raw_check_syncookie_ipv4_proto = {
7794 .func = bpf_tcp_raw_check_syncookie_ipv4,
7795 .gpl_only = true, /* __cookie_v4_check is GPL */
7797 .ret_type = RET_INTEGER,
7798 .arg1_type = ARG_PTR_TO_FIXED_SIZE_MEM,
7799 .arg1_size = sizeof(struct iphdr),
7800 .arg2_type = ARG_PTR_TO_FIXED_SIZE_MEM,
7801 .arg2_size = sizeof(struct tcphdr),
7804 BPF_CALL_2(bpf_tcp_raw_check_syncookie_ipv6, struct ipv6hdr *, iph,
7805 struct tcphdr *, th)
7807 #if IS_BUILTIN(CONFIG_IPV6)
7808 if (__cookie_v6_check(iph, th) > 0)
7813 return -EPROTONOSUPPORT;
7817 static const struct bpf_func_proto bpf_tcp_raw_check_syncookie_ipv6_proto = {
7818 .func = bpf_tcp_raw_check_syncookie_ipv6,
7819 .gpl_only = true, /* __cookie_v6_check is GPL */
7821 .ret_type = RET_INTEGER,
7822 .arg1_type = ARG_PTR_TO_FIXED_SIZE_MEM,
7823 .arg1_size = sizeof(struct ipv6hdr),
7824 .arg2_type = ARG_PTR_TO_FIXED_SIZE_MEM,
7825 .arg2_size = sizeof(struct tcphdr),
7827 #endif /* CONFIG_SYN_COOKIES */
7829 #endif /* CONFIG_INET */
7831 bool bpf_helper_changes_pkt_data(void *func)
7833 if (func == bpf_skb_vlan_push ||
7834 func == bpf_skb_vlan_pop ||
7835 func == bpf_skb_store_bytes ||
7836 func == bpf_skb_change_proto ||
7837 func == bpf_skb_change_head ||
7838 func == sk_skb_change_head ||
7839 func == bpf_skb_change_tail ||
7840 func == sk_skb_change_tail ||
7841 func == bpf_skb_adjust_room ||
7842 func == sk_skb_adjust_room ||
7843 func == bpf_skb_pull_data ||
7844 func == sk_skb_pull_data ||
7845 func == bpf_clone_redirect ||
7846 func == bpf_l3_csum_replace ||
7847 func == bpf_l4_csum_replace ||
7848 func == bpf_xdp_adjust_head ||
7849 func == bpf_xdp_adjust_meta ||
7850 func == bpf_msg_pull_data ||
7851 func == bpf_msg_push_data ||
7852 func == bpf_msg_pop_data ||
7853 func == bpf_xdp_adjust_tail ||
7854 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
7855 func == bpf_lwt_seg6_store_bytes ||
7856 func == bpf_lwt_seg6_adjust_srh ||
7857 func == bpf_lwt_seg6_action ||
7860 func == bpf_sock_ops_store_hdr_opt ||
7862 func == bpf_lwt_in_push_encap ||
7863 func == bpf_lwt_xmit_push_encap)
7869 const struct bpf_func_proto bpf_event_output_data_proto __weak;
7870 const struct bpf_func_proto bpf_sk_storage_get_cg_sock_proto __weak;
7872 static const struct bpf_func_proto *
7873 sock_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
7875 const struct bpf_func_proto *func_proto;
7877 func_proto = cgroup_common_func_proto(func_id, prog);
7881 func_proto = cgroup_current_func_proto(func_id, prog);
7886 case BPF_FUNC_get_socket_cookie:
7887 return &bpf_get_socket_cookie_sock_proto;
7888 case BPF_FUNC_get_netns_cookie:
7889 return &bpf_get_netns_cookie_sock_proto;
7890 case BPF_FUNC_perf_event_output:
7891 return &bpf_event_output_data_proto;
7892 case BPF_FUNC_sk_storage_get:
7893 return &bpf_sk_storage_get_cg_sock_proto;
7894 case BPF_FUNC_ktime_get_coarse_ns:
7895 return &bpf_ktime_get_coarse_ns_proto;
7897 return bpf_base_func_proto(func_id);
7901 static const struct bpf_func_proto *
7902 sock_addr_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
7904 const struct bpf_func_proto *func_proto;
7906 func_proto = cgroup_common_func_proto(func_id, prog);
7910 func_proto = cgroup_current_func_proto(func_id, prog);
7916 switch (prog->expected_attach_type) {
7917 case BPF_CGROUP_INET4_CONNECT:
7918 case BPF_CGROUP_INET6_CONNECT:
7919 return &bpf_bind_proto;
7923 case BPF_FUNC_get_socket_cookie:
7924 return &bpf_get_socket_cookie_sock_addr_proto;
7925 case BPF_FUNC_get_netns_cookie:
7926 return &bpf_get_netns_cookie_sock_addr_proto;
7927 case BPF_FUNC_perf_event_output:
7928 return &bpf_event_output_data_proto;
7930 case BPF_FUNC_sk_lookup_tcp:
7931 return &bpf_sock_addr_sk_lookup_tcp_proto;
7932 case BPF_FUNC_sk_lookup_udp:
7933 return &bpf_sock_addr_sk_lookup_udp_proto;
7934 case BPF_FUNC_sk_release:
7935 return &bpf_sk_release_proto;
7936 case BPF_FUNC_skc_lookup_tcp:
7937 return &bpf_sock_addr_skc_lookup_tcp_proto;
7938 #endif /* CONFIG_INET */
7939 case BPF_FUNC_sk_storage_get:
7940 return &bpf_sk_storage_get_proto;
7941 case BPF_FUNC_sk_storage_delete:
7942 return &bpf_sk_storage_delete_proto;
7943 case BPF_FUNC_setsockopt:
7944 switch (prog->expected_attach_type) {
7945 case BPF_CGROUP_INET4_BIND:
7946 case BPF_CGROUP_INET6_BIND:
7947 case BPF_CGROUP_INET4_CONNECT:
7948 case BPF_CGROUP_INET6_CONNECT:
7949 case BPF_CGROUP_UNIX_CONNECT:
7950 case BPF_CGROUP_UDP4_RECVMSG:
7951 case BPF_CGROUP_UDP6_RECVMSG:
7952 case BPF_CGROUP_UNIX_RECVMSG:
7953 case BPF_CGROUP_UDP4_SENDMSG:
7954 case BPF_CGROUP_UDP6_SENDMSG:
7955 case BPF_CGROUP_UNIX_SENDMSG:
7956 case BPF_CGROUP_INET4_GETPEERNAME:
7957 case BPF_CGROUP_INET6_GETPEERNAME:
7958 case BPF_CGROUP_UNIX_GETPEERNAME:
7959 case BPF_CGROUP_INET4_GETSOCKNAME:
7960 case BPF_CGROUP_INET6_GETSOCKNAME:
7961 case BPF_CGROUP_UNIX_GETSOCKNAME:
7962 return &bpf_sock_addr_setsockopt_proto;
7966 case BPF_FUNC_getsockopt:
7967 switch (prog->expected_attach_type) {
7968 case BPF_CGROUP_INET4_BIND:
7969 case BPF_CGROUP_INET6_BIND:
7970 case BPF_CGROUP_INET4_CONNECT:
7971 case BPF_CGROUP_INET6_CONNECT:
7972 case BPF_CGROUP_UNIX_CONNECT:
7973 case BPF_CGROUP_UDP4_RECVMSG:
7974 case BPF_CGROUP_UDP6_RECVMSG:
7975 case BPF_CGROUP_UNIX_RECVMSG:
7976 case BPF_CGROUP_UDP4_SENDMSG:
7977 case BPF_CGROUP_UDP6_SENDMSG:
7978 case BPF_CGROUP_UNIX_SENDMSG:
7979 case BPF_CGROUP_INET4_GETPEERNAME:
7980 case BPF_CGROUP_INET6_GETPEERNAME:
7981 case BPF_CGROUP_UNIX_GETPEERNAME:
7982 case BPF_CGROUP_INET4_GETSOCKNAME:
7983 case BPF_CGROUP_INET6_GETSOCKNAME:
7984 case BPF_CGROUP_UNIX_GETSOCKNAME:
7985 return &bpf_sock_addr_getsockopt_proto;
7990 return bpf_sk_base_func_proto(func_id);
7994 static const struct bpf_func_proto *
7995 sk_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
7998 case BPF_FUNC_skb_load_bytes:
7999 return &bpf_skb_load_bytes_proto;
8000 case BPF_FUNC_skb_load_bytes_relative:
8001 return &bpf_skb_load_bytes_relative_proto;
8002 case BPF_FUNC_get_socket_cookie:
8003 return &bpf_get_socket_cookie_proto;
8004 case BPF_FUNC_get_socket_uid:
8005 return &bpf_get_socket_uid_proto;
8006 case BPF_FUNC_perf_event_output:
8007 return &bpf_skb_event_output_proto;
8009 return bpf_sk_base_func_proto(func_id);
8013 const struct bpf_func_proto bpf_sk_storage_get_proto __weak;
8014 const struct bpf_func_proto bpf_sk_storage_delete_proto __weak;
8016 static const struct bpf_func_proto *
8017 cg_skb_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
8019 const struct bpf_func_proto *func_proto;
8021 func_proto = cgroup_common_func_proto(func_id, prog);
8026 case BPF_FUNC_sk_fullsock:
8027 return &bpf_sk_fullsock_proto;
8028 case BPF_FUNC_sk_storage_get:
8029 return &bpf_sk_storage_get_proto;
8030 case BPF_FUNC_sk_storage_delete:
8031 return &bpf_sk_storage_delete_proto;
8032 case BPF_FUNC_perf_event_output:
8033 return &bpf_skb_event_output_proto;
8034 #ifdef CONFIG_SOCK_CGROUP_DATA
8035 case BPF_FUNC_skb_cgroup_id:
8036 return &bpf_skb_cgroup_id_proto;
8037 case BPF_FUNC_skb_ancestor_cgroup_id:
8038 return &bpf_skb_ancestor_cgroup_id_proto;
8039 case BPF_FUNC_sk_cgroup_id:
8040 return &bpf_sk_cgroup_id_proto;
8041 case BPF_FUNC_sk_ancestor_cgroup_id:
8042 return &bpf_sk_ancestor_cgroup_id_proto;
8045 case BPF_FUNC_sk_lookup_tcp:
8046 return &bpf_sk_lookup_tcp_proto;
8047 case BPF_FUNC_sk_lookup_udp:
8048 return &bpf_sk_lookup_udp_proto;
8049 case BPF_FUNC_sk_release:
8050 return &bpf_sk_release_proto;
8051 case BPF_FUNC_skc_lookup_tcp:
8052 return &bpf_skc_lookup_tcp_proto;
8053 case BPF_FUNC_tcp_sock:
8054 return &bpf_tcp_sock_proto;
8055 case BPF_FUNC_get_listener_sock:
8056 return &bpf_get_listener_sock_proto;
8057 case BPF_FUNC_skb_ecn_set_ce:
8058 return &bpf_skb_ecn_set_ce_proto;
8061 return sk_filter_func_proto(func_id, prog);
8065 static const struct bpf_func_proto *
8066 tc_cls_act_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
8069 case BPF_FUNC_skb_store_bytes:
8070 return &bpf_skb_store_bytes_proto;
8071 case BPF_FUNC_skb_load_bytes:
8072 return &bpf_skb_load_bytes_proto;
8073 case BPF_FUNC_skb_load_bytes_relative:
8074 return &bpf_skb_load_bytes_relative_proto;
8075 case BPF_FUNC_skb_pull_data:
8076 return &bpf_skb_pull_data_proto;
8077 case BPF_FUNC_csum_diff:
8078 return &bpf_csum_diff_proto;
8079 case BPF_FUNC_csum_update:
8080 return &bpf_csum_update_proto;
8081 case BPF_FUNC_csum_level:
8082 return &bpf_csum_level_proto;
8083 case BPF_FUNC_l3_csum_replace:
8084 return &bpf_l3_csum_replace_proto;
8085 case BPF_FUNC_l4_csum_replace:
8086 return &bpf_l4_csum_replace_proto;
8087 case BPF_FUNC_clone_redirect:
8088 return &bpf_clone_redirect_proto;
8089 case BPF_FUNC_get_cgroup_classid:
8090 return &bpf_get_cgroup_classid_proto;
8091 case BPF_FUNC_skb_vlan_push:
8092 return &bpf_skb_vlan_push_proto;
8093 case BPF_FUNC_skb_vlan_pop:
8094 return &bpf_skb_vlan_pop_proto;
8095 case BPF_FUNC_skb_change_proto:
8096 return &bpf_skb_change_proto_proto;
8097 case BPF_FUNC_skb_change_type:
8098 return &bpf_skb_change_type_proto;
8099 case BPF_FUNC_skb_adjust_room:
8100 return &bpf_skb_adjust_room_proto;
8101 case BPF_FUNC_skb_change_tail:
8102 return &bpf_skb_change_tail_proto;
8103 case BPF_FUNC_skb_change_head:
8104 return &bpf_skb_change_head_proto;
8105 case BPF_FUNC_skb_get_tunnel_key:
8106 return &bpf_skb_get_tunnel_key_proto;
8107 case BPF_FUNC_skb_set_tunnel_key:
8108 return bpf_get_skb_set_tunnel_proto(func_id);
8109 case BPF_FUNC_skb_get_tunnel_opt:
8110 return &bpf_skb_get_tunnel_opt_proto;
8111 case BPF_FUNC_skb_set_tunnel_opt:
8112 return bpf_get_skb_set_tunnel_proto(func_id);
8113 case BPF_FUNC_redirect:
8114 return &bpf_redirect_proto;
8115 case BPF_FUNC_redirect_neigh:
8116 return &bpf_redirect_neigh_proto;
8117 case BPF_FUNC_redirect_peer:
8118 return &bpf_redirect_peer_proto;
8119 case BPF_FUNC_get_route_realm:
8120 return &bpf_get_route_realm_proto;
8121 case BPF_FUNC_get_hash_recalc:
8122 return &bpf_get_hash_recalc_proto;
8123 case BPF_FUNC_set_hash_invalid:
8124 return &bpf_set_hash_invalid_proto;
8125 case BPF_FUNC_set_hash:
8126 return &bpf_set_hash_proto;
8127 case BPF_FUNC_perf_event_output:
8128 return &bpf_skb_event_output_proto;
8129 case BPF_FUNC_get_smp_processor_id:
8130 return &bpf_get_smp_processor_id_proto;
8131 case BPF_FUNC_skb_under_cgroup:
8132 return &bpf_skb_under_cgroup_proto;
8133 case BPF_FUNC_get_socket_cookie:
8134 return &bpf_get_socket_cookie_proto;
8135 case BPF_FUNC_get_socket_uid:
8136 return &bpf_get_socket_uid_proto;
8137 case BPF_FUNC_fib_lookup:
8138 return &bpf_skb_fib_lookup_proto;
8139 case BPF_FUNC_check_mtu:
8140 return &bpf_skb_check_mtu_proto;
8141 case BPF_FUNC_sk_fullsock:
8142 return &bpf_sk_fullsock_proto;
8143 case BPF_FUNC_sk_storage_get:
8144 return &bpf_sk_storage_get_proto;
8145 case BPF_FUNC_sk_storage_delete:
8146 return &bpf_sk_storage_delete_proto;
8148 case BPF_FUNC_skb_get_xfrm_state:
8149 return &bpf_skb_get_xfrm_state_proto;
8151 #ifdef CONFIG_CGROUP_NET_CLASSID
8152 case BPF_FUNC_skb_cgroup_classid:
8153 return &bpf_skb_cgroup_classid_proto;
8155 #ifdef CONFIG_SOCK_CGROUP_DATA
8156 case BPF_FUNC_skb_cgroup_id:
8157 return &bpf_skb_cgroup_id_proto;
8158 case BPF_FUNC_skb_ancestor_cgroup_id:
8159 return &bpf_skb_ancestor_cgroup_id_proto;
8162 case BPF_FUNC_sk_lookup_tcp:
8163 return &bpf_tc_sk_lookup_tcp_proto;
8164 case BPF_FUNC_sk_lookup_udp:
8165 return &bpf_tc_sk_lookup_udp_proto;
8166 case BPF_FUNC_sk_release:
8167 return &bpf_sk_release_proto;
8168 case BPF_FUNC_tcp_sock:
8169 return &bpf_tcp_sock_proto;
8170 case BPF_FUNC_get_listener_sock:
8171 return &bpf_get_listener_sock_proto;
8172 case BPF_FUNC_skc_lookup_tcp:
8173 return &bpf_tc_skc_lookup_tcp_proto;
8174 case BPF_FUNC_tcp_check_syncookie:
8175 return &bpf_tcp_check_syncookie_proto;
8176 case BPF_FUNC_skb_ecn_set_ce:
8177 return &bpf_skb_ecn_set_ce_proto;
8178 case BPF_FUNC_tcp_gen_syncookie:
8179 return &bpf_tcp_gen_syncookie_proto;
8180 case BPF_FUNC_sk_assign:
8181 return &bpf_sk_assign_proto;
8182 case BPF_FUNC_skb_set_tstamp:
8183 return &bpf_skb_set_tstamp_proto;
8184 #ifdef CONFIG_SYN_COOKIES
8185 case BPF_FUNC_tcp_raw_gen_syncookie_ipv4:
8186 return &bpf_tcp_raw_gen_syncookie_ipv4_proto;
8187 case BPF_FUNC_tcp_raw_gen_syncookie_ipv6:
8188 return &bpf_tcp_raw_gen_syncookie_ipv6_proto;
8189 case BPF_FUNC_tcp_raw_check_syncookie_ipv4:
8190 return &bpf_tcp_raw_check_syncookie_ipv4_proto;
8191 case BPF_FUNC_tcp_raw_check_syncookie_ipv6:
8192 return &bpf_tcp_raw_check_syncookie_ipv6_proto;
8196 return bpf_sk_base_func_proto(func_id);
8200 static const struct bpf_func_proto *
8201 xdp_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
8204 case BPF_FUNC_perf_event_output:
8205 return &bpf_xdp_event_output_proto;
8206 case BPF_FUNC_get_smp_processor_id:
8207 return &bpf_get_smp_processor_id_proto;
8208 case BPF_FUNC_csum_diff:
8209 return &bpf_csum_diff_proto;
8210 case BPF_FUNC_xdp_adjust_head:
8211 return &bpf_xdp_adjust_head_proto;
8212 case BPF_FUNC_xdp_adjust_meta:
8213 return &bpf_xdp_adjust_meta_proto;
8214 case BPF_FUNC_redirect:
8215 return &bpf_xdp_redirect_proto;
8216 case BPF_FUNC_redirect_map:
8217 return &bpf_xdp_redirect_map_proto;
8218 case BPF_FUNC_xdp_adjust_tail:
8219 return &bpf_xdp_adjust_tail_proto;
8220 case BPF_FUNC_xdp_get_buff_len:
8221 return &bpf_xdp_get_buff_len_proto;
8222 case BPF_FUNC_xdp_load_bytes:
8223 return &bpf_xdp_load_bytes_proto;
8224 case BPF_FUNC_xdp_store_bytes:
8225 return &bpf_xdp_store_bytes_proto;
8226 case BPF_FUNC_fib_lookup:
8227 return &bpf_xdp_fib_lookup_proto;
8228 case BPF_FUNC_check_mtu:
8229 return &bpf_xdp_check_mtu_proto;
8231 case BPF_FUNC_sk_lookup_udp:
8232 return &bpf_xdp_sk_lookup_udp_proto;
8233 case BPF_FUNC_sk_lookup_tcp:
8234 return &bpf_xdp_sk_lookup_tcp_proto;
8235 case BPF_FUNC_sk_release:
8236 return &bpf_sk_release_proto;
8237 case BPF_FUNC_skc_lookup_tcp:
8238 return &bpf_xdp_skc_lookup_tcp_proto;
8239 case BPF_FUNC_tcp_check_syncookie:
8240 return &bpf_tcp_check_syncookie_proto;
8241 case BPF_FUNC_tcp_gen_syncookie:
8242 return &bpf_tcp_gen_syncookie_proto;
8243 #ifdef CONFIG_SYN_COOKIES
8244 case BPF_FUNC_tcp_raw_gen_syncookie_ipv4:
8245 return &bpf_tcp_raw_gen_syncookie_ipv4_proto;
8246 case BPF_FUNC_tcp_raw_gen_syncookie_ipv6:
8247 return &bpf_tcp_raw_gen_syncookie_ipv6_proto;
8248 case BPF_FUNC_tcp_raw_check_syncookie_ipv4:
8249 return &bpf_tcp_raw_check_syncookie_ipv4_proto;
8250 case BPF_FUNC_tcp_raw_check_syncookie_ipv6:
8251 return &bpf_tcp_raw_check_syncookie_ipv6_proto;
8255 return bpf_sk_base_func_proto(func_id);
8258 #if IS_MODULE(CONFIG_NF_CONNTRACK) && IS_ENABLED(CONFIG_DEBUG_INFO_BTF_MODULES)
8259 /* The nf_conn___init type is used in the NF_CONNTRACK kfuncs. The
8260 * kfuncs are defined in two different modules, and we want to be able
8261 * to use them interchangably with the same BTF type ID. Because modules
8262 * can't de-duplicate BTF IDs between each other, we need the type to be
8263 * referenced in the vmlinux BTF or the verifier will get confused about
8264 * the different types. So we add this dummy type reference which will
8265 * be included in vmlinux BTF, allowing both modules to refer to the
8268 BTF_TYPE_EMIT(struct nf_conn___init);
8272 const struct bpf_func_proto bpf_sock_map_update_proto __weak;
8273 const struct bpf_func_proto bpf_sock_hash_update_proto __weak;
8275 static const struct bpf_func_proto *
8276 sock_ops_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
8278 const struct bpf_func_proto *func_proto;
8280 func_proto = cgroup_common_func_proto(func_id, prog);
8285 case BPF_FUNC_setsockopt:
8286 return &bpf_sock_ops_setsockopt_proto;
8287 case BPF_FUNC_getsockopt:
8288 return &bpf_sock_ops_getsockopt_proto;
8289 case BPF_FUNC_sock_ops_cb_flags_set:
8290 return &bpf_sock_ops_cb_flags_set_proto;
8291 case BPF_FUNC_sock_map_update:
8292 return &bpf_sock_map_update_proto;
8293 case BPF_FUNC_sock_hash_update:
8294 return &bpf_sock_hash_update_proto;
8295 case BPF_FUNC_get_socket_cookie:
8296 return &bpf_get_socket_cookie_sock_ops_proto;
8297 case BPF_FUNC_perf_event_output:
8298 return &bpf_event_output_data_proto;
8299 case BPF_FUNC_sk_storage_get:
8300 return &bpf_sk_storage_get_proto;
8301 case BPF_FUNC_sk_storage_delete:
8302 return &bpf_sk_storage_delete_proto;
8303 case BPF_FUNC_get_netns_cookie:
8304 return &bpf_get_netns_cookie_sock_ops_proto;
8306 case BPF_FUNC_load_hdr_opt:
8307 return &bpf_sock_ops_load_hdr_opt_proto;
8308 case BPF_FUNC_store_hdr_opt:
8309 return &bpf_sock_ops_store_hdr_opt_proto;
8310 case BPF_FUNC_reserve_hdr_opt:
8311 return &bpf_sock_ops_reserve_hdr_opt_proto;
8312 case BPF_FUNC_tcp_sock:
8313 return &bpf_tcp_sock_proto;
8314 #endif /* CONFIG_INET */
8316 return bpf_sk_base_func_proto(func_id);
8320 const struct bpf_func_proto bpf_msg_redirect_map_proto __weak;
8321 const struct bpf_func_proto bpf_msg_redirect_hash_proto __weak;
8323 static const struct bpf_func_proto *
8324 sk_msg_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
8327 case BPF_FUNC_msg_redirect_map:
8328 return &bpf_msg_redirect_map_proto;
8329 case BPF_FUNC_msg_redirect_hash:
8330 return &bpf_msg_redirect_hash_proto;
8331 case BPF_FUNC_msg_apply_bytes:
8332 return &bpf_msg_apply_bytes_proto;
8333 case BPF_FUNC_msg_cork_bytes:
8334 return &bpf_msg_cork_bytes_proto;
8335 case BPF_FUNC_msg_pull_data:
8336 return &bpf_msg_pull_data_proto;
8337 case BPF_FUNC_msg_push_data:
8338 return &bpf_msg_push_data_proto;
8339 case BPF_FUNC_msg_pop_data:
8340 return &bpf_msg_pop_data_proto;
8341 case BPF_FUNC_perf_event_output:
8342 return &bpf_event_output_data_proto;
8343 case BPF_FUNC_get_current_uid_gid:
8344 return &bpf_get_current_uid_gid_proto;
8345 case BPF_FUNC_get_current_pid_tgid:
8346 return &bpf_get_current_pid_tgid_proto;
8347 case BPF_FUNC_sk_storage_get:
8348 return &bpf_sk_storage_get_proto;
8349 case BPF_FUNC_sk_storage_delete:
8350 return &bpf_sk_storage_delete_proto;
8351 case BPF_FUNC_get_netns_cookie:
8352 return &bpf_get_netns_cookie_sk_msg_proto;
8353 #ifdef CONFIG_CGROUP_NET_CLASSID
8354 case BPF_FUNC_get_cgroup_classid:
8355 return &bpf_get_cgroup_classid_curr_proto;
8358 return bpf_sk_base_func_proto(func_id);
8362 const struct bpf_func_proto bpf_sk_redirect_map_proto __weak;
8363 const struct bpf_func_proto bpf_sk_redirect_hash_proto __weak;
8365 static const struct bpf_func_proto *
8366 sk_skb_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
8369 case BPF_FUNC_skb_store_bytes:
8370 return &bpf_skb_store_bytes_proto;
8371 case BPF_FUNC_skb_load_bytes:
8372 return &bpf_skb_load_bytes_proto;
8373 case BPF_FUNC_skb_pull_data:
8374 return &sk_skb_pull_data_proto;
8375 case BPF_FUNC_skb_change_tail:
8376 return &sk_skb_change_tail_proto;
8377 case BPF_FUNC_skb_change_head:
8378 return &sk_skb_change_head_proto;
8379 case BPF_FUNC_skb_adjust_room:
8380 return &sk_skb_adjust_room_proto;
8381 case BPF_FUNC_get_socket_cookie:
8382 return &bpf_get_socket_cookie_proto;
8383 case BPF_FUNC_get_socket_uid:
8384 return &bpf_get_socket_uid_proto;
8385 case BPF_FUNC_sk_redirect_map:
8386 return &bpf_sk_redirect_map_proto;
8387 case BPF_FUNC_sk_redirect_hash:
8388 return &bpf_sk_redirect_hash_proto;
8389 case BPF_FUNC_perf_event_output:
8390 return &bpf_skb_event_output_proto;
8392 case BPF_FUNC_sk_lookup_tcp:
8393 return &bpf_sk_lookup_tcp_proto;
8394 case BPF_FUNC_sk_lookup_udp:
8395 return &bpf_sk_lookup_udp_proto;
8396 case BPF_FUNC_sk_release:
8397 return &bpf_sk_release_proto;
8398 case BPF_FUNC_skc_lookup_tcp:
8399 return &bpf_skc_lookup_tcp_proto;
8402 return bpf_sk_base_func_proto(func_id);
8406 static const struct bpf_func_proto *
8407 flow_dissector_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
8410 case BPF_FUNC_skb_load_bytes:
8411 return &bpf_flow_dissector_load_bytes_proto;
8413 return bpf_sk_base_func_proto(func_id);
8417 static const struct bpf_func_proto *
8418 lwt_out_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
8421 case BPF_FUNC_skb_load_bytes:
8422 return &bpf_skb_load_bytes_proto;
8423 case BPF_FUNC_skb_pull_data:
8424 return &bpf_skb_pull_data_proto;
8425 case BPF_FUNC_csum_diff:
8426 return &bpf_csum_diff_proto;
8427 case BPF_FUNC_get_cgroup_classid:
8428 return &bpf_get_cgroup_classid_proto;
8429 case BPF_FUNC_get_route_realm:
8430 return &bpf_get_route_realm_proto;
8431 case BPF_FUNC_get_hash_recalc:
8432 return &bpf_get_hash_recalc_proto;
8433 case BPF_FUNC_perf_event_output:
8434 return &bpf_skb_event_output_proto;
8435 case BPF_FUNC_get_smp_processor_id:
8436 return &bpf_get_smp_processor_id_proto;
8437 case BPF_FUNC_skb_under_cgroup:
8438 return &bpf_skb_under_cgroup_proto;
8440 return bpf_sk_base_func_proto(func_id);
8444 static const struct bpf_func_proto *
8445 lwt_in_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
8448 case BPF_FUNC_lwt_push_encap:
8449 return &bpf_lwt_in_push_encap_proto;
8451 return lwt_out_func_proto(func_id, prog);
8455 static const struct bpf_func_proto *
8456 lwt_xmit_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
8459 case BPF_FUNC_skb_get_tunnel_key:
8460 return &bpf_skb_get_tunnel_key_proto;
8461 case BPF_FUNC_skb_set_tunnel_key:
8462 return bpf_get_skb_set_tunnel_proto(func_id);
8463 case BPF_FUNC_skb_get_tunnel_opt:
8464 return &bpf_skb_get_tunnel_opt_proto;
8465 case BPF_FUNC_skb_set_tunnel_opt:
8466 return bpf_get_skb_set_tunnel_proto(func_id);
8467 case BPF_FUNC_redirect:
8468 return &bpf_redirect_proto;
8469 case BPF_FUNC_clone_redirect:
8470 return &bpf_clone_redirect_proto;
8471 case BPF_FUNC_skb_change_tail:
8472 return &bpf_skb_change_tail_proto;
8473 case BPF_FUNC_skb_change_head:
8474 return &bpf_skb_change_head_proto;
8475 case BPF_FUNC_skb_store_bytes:
8476 return &bpf_skb_store_bytes_proto;
8477 case BPF_FUNC_csum_update:
8478 return &bpf_csum_update_proto;
8479 case BPF_FUNC_csum_level:
8480 return &bpf_csum_level_proto;
8481 case BPF_FUNC_l3_csum_replace:
8482 return &bpf_l3_csum_replace_proto;
8483 case BPF_FUNC_l4_csum_replace:
8484 return &bpf_l4_csum_replace_proto;
8485 case BPF_FUNC_set_hash_invalid:
8486 return &bpf_set_hash_invalid_proto;
8487 case BPF_FUNC_lwt_push_encap:
8488 return &bpf_lwt_xmit_push_encap_proto;
8490 return lwt_out_func_proto(func_id, prog);
8494 static const struct bpf_func_proto *
8495 lwt_seg6local_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
8498 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
8499 case BPF_FUNC_lwt_seg6_store_bytes:
8500 return &bpf_lwt_seg6_store_bytes_proto;
8501 case BPF_FUNC_lwt_seg6_action:
8502 return &bpf_lwt_seg6_action_proto;
8503 case BPF_FUNC_lwt_seg6_adjust_srh:
8504 return &bpf_lwt_seg6_adjust_srh_proto;
8507 return lwt_out_func_proto(func_id, prog);
8511 static bool bpf_skb_is_valid_access(int off, int size, enum bpf_access_type type,
8512 const struct bpf_prog *prog,
8513 struct bpf_insn_access_aux *info)
8515 const int size_default = sizeof(__u32);
8517 if (off < 0 || off >= sizeof(struct __sk_buff))
8520 /* The verifier guarantees that size > 0. */
8521 if (off % size != 0)
8525 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
8526 if (off + size > offsetofend(struct __sk_buff, cb[4]))
8529 case bpf_ctx_range_till(struct __sk_buff, remote_ip6[0], remote_ip6[3]):
8530 case bpf_ctx_range_till(struct __sk_buff, local_ip6[0], local_ip6[3]):
8531 case bpf_ctx_range_till(struct __sk_buff, remote_ip4, remote_ip4):
8532 case bpf_ctx_range_till(struct __sk_buff, local_ip4, local_ip4):
8533 case bpf_ctx_range(struct __sk_buff, data):
8534 case bpf_ctx_range(struct __sk_buff, data_meta):
8535 case bpf_ctx_range(struct __sk_buff, data_end):
8536 if (size != size_default)
8539 case bpf_ctx_range_ptr(struct __sk_buff, flow_keys):
8541 case bpf_ctx_range(struct __sk_buff, hwtstamp):
8542 if (type == BPF_WRITE || size != sizeof(__u64))
8545 case bpf_ctx_range(struct __sk_buff, tstamp):
8546 if (size != sizeof(__u64))
8549 case offsetof(struct __sk_buff, sk):
8550 if (type == BPF_WRITE || size != sizeof(__u64))
8552 info->reg_type = PTR_TO_SOCK_COMMON_OR_NULL;
8554 case offsetof(struct __sk_buff, tstamp_type):
8556 case offsetofend(struct __sk_buff, tstamp_type) ... offsetof(struct __sk_buff, hwtstamp) - 1:
8557 /* Explicitly prohibit access to padding in __sk_buff. */
8560 /* Only narrow read access allowed for now. */
8561 if (type == BPF_WRITE) {
8562 if (size != size_default)
8565 bpf_ctx_record_field_size(info, size_default);
8566 if (!bpf_ctx_narrow_access_ok(off, size, size_default))
8574 static bool sk_filter_is_valid_access(int off, int size,
8575 enum bpf_access_type type,
8576 const struct bpf_prog *prog,
8577 struct bpf_insn_access_aux *info)
8580 case bpf_ctx_range(struct __sk_buff, tc_classid):
8581 case bpf_ctx_range(struct __sk_buff, data):
8582 case bpf_ctx_range(struct __sk_buff, data_meta):
8583 case bpf_ctx_range(struct __sk_buff, data_end):
8584 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
8585 case bpf_ctx_range(struct __sk_buff, tstamp):
8586 case bpf_ctx_range(struct __sk_buff, wire_len):
8587 case bpf_ctx_range(struct __sk_buff, hwtstamp):
8591 if (type == BPF_WRITE) {
8593 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
8600 return bpf_skb_is_valid_access(off, size, type, prog, info);
8603 static bool cg_skb_is_valid_access(int off, int size,
8604 enum bpf_access_type type,
8605 const struct bpf_prog *prog,
8606 struct bpf_insn_access_aux *info)
8609 case bpf_ctx_range(struct __sk_buff, tc_classid):
8610 case bpf_ctx_range(struct __sk_buff, data_meta):
8611 case bpf_ctx_range(struct __sk_buff, wire_len):
8613 case bpf_ctx_range(struct __sk_buff, data):
8614 case bpf_ctx_range(struct __sk_buff, data_end):
8620 if (type == BPF_WRITE) {
8622 case bpf_ctx_range(struct __sk_buff, mark):
8623 case bpf_ctx_range(struct __sk_buff, priority):
8624 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
8626 case bpf_ctx_range(struct __sk_buff, tstamp):
8636 case bpf_ctx_range(struct __sk_buff, data):
8637 info->reg_type = PTR_TO_PACKET;
8639 case bpf_ctx_range(struct __sk_buff, data_end):
8640 info->reg_type = PTR_TO_PACKET_END;
8644 return bpf_skb_is_valid_access(off, size, type, prog, info);
8647 static bool lwt_is_valid_access(int off, int size,
8648 enum bpf_access_type type,
8649 const struct bpf_prog *prog,
8650 struct bpf_insn_access_aux *info)
8653 case bpf_ctx_range(struct __sk_buff, tc_classid):
8654 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
8655 case bpf_ctx_range(struct __sk_buff, data_meta):
8656 case bpf_ctx_range(struct __sk_buff, tstamp):
8657 case bpf_ctx_range(struct __sk_buff, wire_len):
8658 case bpf_ctx_range(struct __sk_buff, hwtstamp):
8662 if (type == BPF_WRITE) {
8664 case bpf_ctx_range(struct __sk_buff, mark):
8665 case bpf_ctx_range(struct __sk_buff, priority):
8666 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
8674 case bpf_ctx_range(struct __sk_buff, data):
8675 info->reg_type = PTR_TO_PACKET;
8677 case bpf_ctx_range(struct __sk_buff, data_end):
8678 info->reg_type = PTR_TO_PACKET_END;
8682 return bpf_skb_is_valid_access(off, size, type, prog, info);
8685 /* Attach type specific accesses */
8686 static bool __sock_filter_check_attach_type(int off,
8687 enum bpf_access_type access_type,
8688 enum bpf_attach_type attach_type)
8691 case offsetof(struct bpf_sock, bound_dev_if):
8692 case offsetof(struct bpf_sock, mark):
8693 case offsetof(struct bpf_sock, priority):
8694 switch (attach_type) {
8695 case BPF_CGROUP_INET_SOCK_CREATE:
8696 case BPF_CGROUP_INET_SOCK_RELEASE:
8701 case bpf_ctx_range(struct bpf_sock, src_ip4):
8702 switch (attach_type) {
8703 case BPF_CGROUP_INET4_POST_BIND:
8708 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
8709 switch (attach_type) {
8710 case BPF_CGROUP_INET6_POST_BIND:
8715 case bpf_ctx_range(struct bpf_sock, src_port):
8716 switch (attach_type) {
8717 case BPF_CGROUP_INET4_POST_BIND:
8718 case BPF_CGROUP_INET6_POST_BIND:
8725 return access_type == BPF_READ;
8730 bool bpf_sock_common_is_valid_access(int off, int size,
8731 enum bpf_access_type type,
8732 struct bpf_insn_access_aux *info)
8735 case bpf_ctx_range_till(struct bpf_sock, type, priority):
8738 return bpf_sock_is_valid_access(off, size, type, info);
8742 bool bpf_sock_is_valid_access(int off, int size, enum bpf_access_type type,
8743 struct bpf_insn_access_aux *info)
8745 const int size_default = sizeof(__u32);
8748 if (off < 0 || off >= sizeof(struct bpf_sock))
8750 if (off % size != 0)
8754 case offsetof(struct bpf_sock, state):
8755 case offsetof(struct bpf_sock, family):
8756 case offsetof(struct bpf_sock, type):
8757 case offsetof(struct bpf_sock, protocol):
8758 case offsetof(struct bpf_sock, src_port):
8759 case offsetof(struct bpf_sock, rx_queue_mapping):
8760 case bpf_ctx_range(struct bpf_sock, src_ip4):
8761 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
8762 case bpf_ctx_range(struct bpf_sock, dst_ip4):
8763 case bpf_ctx_range_till(struct bpf_sock, dst_ip6[0], dst_ip6[3]):
8764 bpf_ctx_record_field_size(info, size_default);
8765 return bpf_ctx_narrow_access_ok(off, size, size_default);
8766 case bpf_ctx_range(struct bpf_sock, dst_port):
8767 field_size = size == size_default ?
8768 size_default : sizeof_field(struct bpf_sock, dst_port);
8769 bpf_ctx_record_field_size(info, field_size);
8770 return bpf_ctx_narrow_access_ok(off, size, field_size);
8771 case offsetofend(struct bpf_sock, dst_port) ...
8772 offsetof(struct bpf_sock, dst_ip4) - 1:
8776 return size == size_default;
8779 static bool sock_filter_is_valid_access(int off, int size,
8780 enum bpf_access_type type,
8781 const struct bpf_prog *prog,
8782 struct bpf_insn_access_aux *info)
8784 if (!bpf_sock_is_valid_access(off, size, type, info))
8786 return __sock_filter_check_attach_type(off, type,
8787 prog->expected_attach_type);
8790 static int bpf_noop_prologue(struct bpf_insn *insn_buf, bool direct_write,
8791 const struct bpf_prog *prog)
8793 /* Neither direct read nor direct write requires any preliminary
8799 static int bpf_unclone_prologue(struct bpf_insn *insn_buf, bool direct_write,
8800 const struct bpf_prog *prog, int drop_verdict)
8802 struct bpf_insn *insn = insn_buf;
8807 /* if (!skb->cloned)
8810 * (Fast-path, otherwise approximation that we might be
8811 * a clone, do the rest in helper.)
8813 *insn++ = BPF_LDX_MEM(BPF_B, BPF_REG_6, BPF_REG_1, CLONED_OFFSET);
8814 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_6, CLONED_MASK);
8815 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_6, 0, 7);
8817 /* ret = bpf_skb_pull_data(skb, 0); */
8818 *insn++ = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1);
8819 *insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_2, BPF_REG_2);
8820 *insn++ = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
8821 BPF_FUNC_skb_pull_data);
8824 * return TC_ACT_SHOT;
8826 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2);
8827 *insn++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_0, drop_verdict);
8828 *insn++ = BPF_EXIT_INSN();
8831 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_6);
8833 *insn++ = prog->insnsi[0];
8835 return insn - insn_buf;
8838 static int bpf_gen_ld_abs(const struct bpf_insn *orig,
8839 struct bpf_insn *insn_buf)
8841 bool indirect = BPF_MODE(orig->code) == BPF_IND;
8842 struct bpf_insn *insn = insn_buf;
8845 *insn++ = BPF_MOV64_IMM(BPF_REG_2, orig->imm);
8847 *insn++ = BPF_MOV64_REG(BPF_REG_2, orig->src_reg);
8849 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, orig->imm);
8851 /* We're guaranteed here that CTX is in R6. */
8852 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_CTX);
8854 switch (BPF_SIZE(orig->code)) {
8856 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_8_no_cache);
8859 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_16_no_cache);
8862 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_32_no_cache);
8866 *insn++ = BPF_JMP_IMM(BPF_JSGE, BPF_REG_0, 0, 2);
8867 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_0, BPF_REG_0);
8868 *insn++ = BPF_EXIT_INSN();
8870 return insn - insn_buf;
8873 static int tc_cls_act_prologue(struct bpf_insn *insn_buf, bool direct_write,
8874 const struct bpf_prog *prog)
8876 return bpf_unclone_prologue(insn_buf, direct_write, prog, TC_ACT_SHOT);
8879 static bool tc_cls_act_is_valid_access(int off, int size,
8880 enum bpf_access_type type,
8881 const struct bpf_prog *prog,
8882 struct bpf_insn_access_aux *info)
8884 if (type == BPF_WRITE) {
8886 case bpf_ctx_range(struct __sk_buff, mark):
8887 case bpf_ctx_range(struct __sk_buff, tc_index):
8888 case bpf_ctx_range(struct __sk_buff, priority):
8889 case bpf_ctx_range(struct __sk_buff, tc_classid):
8890 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
8891 case bpf_ctx_range(struct __sk_buff, tstamp):
8892 case bpf_ctx_range(struct __sk_buff, queue_mapping):
8900 case bpf_ctx_range(struct __sk_buff, data):
8901 info->reg_type = PTR_TO_PACKET;
8903 case bpf_ctx_range(struct __sk_buff, data_meta):
8904 info->reg_type = PTR_TO_PACKET_META;
8906 case bpf_ctx_range(struct __sk_buff, data_end):
8907 info->reg_type = PTR_TO_PACKET_END;
8909 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
8911 case offsetof(struct __sk_buff, tstamp_type):
8912 /* The convert_ctx_access() on reading and writing
8913 * __sk_buff->tstamp depends on whether the bpf prog
8914 * has used __sk_buff->tstamp_type or not.
8915 * Thus, we need to set prog->tstamp_type_access
8916 * earlier during is_valid_access() here.
8918 ((struct bpf_prog *)prog)->tstamp_type_access = 1;
8919 return size == sizeof(__u8);
8922 return bpf_skb_is_valid_access(off, size, type, prog, info);
8925 DEFINE_MUTEX(nf_conn_btf_access_lock);
8926 EXPORT_SYMBOL_GPL(nf_conn_btf_access_lock);
8928 int (*nfct_btf_struct_access)(struct bpf_verifier_log *log,
8929 const struct bpf_reg_state *reg,
8931 EXPORT_SYMBOL_GPL(nfct_btf_struct_access);
8933 static int tc_cls_act_btf_struct_access(struct bpf_verifier_log *log,
8934 const struct bpf_reg_state *reg,
8939 mutex_lock(&nf_conn_btf_access_lock);
8940 if (nfct_btf_struct_access)
8941 ret = nfct_btf_struct_access(log, reg, off, size);
8942 mutex_unlock(&nf_conn_btf_access_lock);
8947 static bool __is_valid_xdp_access(int off, int size)
8949 if (off < 0 || off >= sizeof(struct xdp_md))
8951 if (off % size != 0)
8953 if (size != sizeof(__u32))
8959 static bool xdp_is_valid_access(int off, int size,
8960 enum bpf_access_type type,
8961 const struct bpf_prog *prog,
8962 struct bpf_insn_access_aux *info)
8964 if (prog->expected_attach_type != BPF_XDP_DEVMAP) {
8966 case offsetof(struct xdp_md, egress_ifindex):
8971 if (type == BPF_WRITE) {
8972 if (bpf_prog_is_offloaded(prog->aux)) {
8974 case offsetof(struct xdp_md, rx_queue_index):
8975 return __is_valid_xdp_access(off, size);
8982 case offsetof(struct xdp_md, data):
8983 info->reg_type = PTR_TO_PACKET;
8985 case offsetof(struct xdp_md, data_meta):
8986 info->reg_type = PTR_TO_PACKET_META;
8988 case offsetof(struct xdp_md, data_end):
8989 info->reg_type = PTR_TO_PACKET_END;
8993 return __is_valid_xdp_access(off, size);
8996 void bpf_warn_invalid_xdp_action(struct net_device *dev, struct bpf_prog *prog, u32 act)
8998 const u32 act_max = XDP_REDIRECT;
9000 pr_warn_once("%s XDP return value %u on prog %s (id %d) dev %s, expect packet loss!\n",
9001 act > act_max ? "Illegal" : "Driver unsupported",
9002 act, prog->aux->name, prog->aux->id, dev ? dev->name : "N/A");
9004 EXPORT_SYMBOL_GPL(bpf_warn_invalid_xdp_action);
9006 static int xdp_btf_struct_access(struct bpf_verifier_log *log,
9007 const struct bpf_reg_state *reg,
9012 mutex_lock(&nf_conn_btf_access_lock);
9013 if (nfct_btf_struct_access)
9014 ret = nfct_btf_struct_access(log, reg, off, size);
9015 mutex_unlock(&nf_conn_btf_access_lock);
9020 static bool sock_addr_is_valid_access(int off, int size,
9021 enum bpf_access_type type,
9022 const struct bpf_prog *prog,
9023 struct bpf_insn_access_aux *info)
9025 const int size_default = sizeof(__u32);
9027 if (off < 0 || off >= sizeof(struct bpf_sock_addr))
9029 if (off % size != 0)
9032 /* Disallow access to fields not belonging to the attach type's address
9036 case bpf_ctx_range(struct bpf_sock_addr, user_ip4):
9037 switch (prog->expected_attach_type) {
9038 case BPF_CGROUP_INET4_BIND:
9039 case BPF_CGROUP_INET4_CONNECT:
9040 case BPF_CGROUP_INET4_GETPEERNAME:
9041 case BPF_CGROUP_INET4_GETSOCKNAME:
9042 case BPF_CGROUP_UDP4_SENDMSG:
9043 case BPF_CGROUP_UDP4_RECVMSG:
9049 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
9050 switch (prog->expected_attach_type) {
9051 case BPF_CGROUP_INET6_BIND:
9052 case BPF_CGROUP_INET6_CONNECT:
9053 case BPF_CGROUP_INET6_GETPEERNAME:
9054 case BPF_CGROUP_INET6_GETSOCKNAME:
9055 case BPF_CGROUP_UDP6_SENDMSG:
9056 case BPF_CGROUP_UDP6_RECVMSG:
9062 case bpf_ctx_range(struct bpf_sock_addr, msg_src_ip4):
9063 switch (prog->expected_attach_type) {
9064 case BPF_CGROUP_UDP4_SENDMSG:
9070 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
9072 switch (prog->expected_attach_type) {
9073 case BPF_CGROUP_UDP6_SENDMSG:
9082 case bpf_ctx_range(struct bpf_sock_addr, user_ip4):
9083 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
9084 case bpf_ctx_range(struct bpf_sock_addr, msg_src_ip4):
9085 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
9087 case bpf_ctx_range(struct bpf_sock_addr, user_port):
9088 if (type == BPF_READ) {
9089 bpf_ctx_record_field_size(info, size_default);
9091 if (bpf_ctx_wide_access_ok(off, size,
9092 struct bpf_sock_addr,
9096 if (bpf_ctx_wide_access_ok(off, size,
9097 struct bpf_sock_addr,
9101 if (!bpf_ctx_narrow_access_ok(off, size, size_default))
9104 if (bpf_ctx_wide_access_ok(off, size,
9105 struct bpf_sock_addr,
9109 if (bpf_ctx_wide_access_ok(off, size,
9110 struct bpf_sock_addr,
9114 if (size != size_default)
9118 case offsetof(struct bpf_sock_addr, sk):
9119 if (type != BPF_READ)
9121 if (size != sizeof(__u64))
9123 info->reg_type = PTR_TO_SOCKET;
9126 if (type == BPF_READ) {
9127 if (size != size_default)
9137 static bool sock_ops_is_valid_access(int off, int size,
9138 enum bpf_access_type type,
9139 const struct bpf_prog *prog,
9140 struct bpf_insn_access_aux *info)
9142 const int size_default = sizeof(__u32);
9144 if (off < 0 || off >= sizeof(struct bpf_sock_ops))
9147 /* The verifier guarantees that size > 0. */
9148 if (off % size != 0)
9151 if (type == BPF_WRITE) {
9153 case offsetof(struct bpf_sock_ops, reply):
9154 case offsetof(struct bpf_sock_ops, sk_txhash):
9155 if (size != size_default)
9163 case bpf_ctx_range_till(struct bpf_sock_ops, bytes_received,
9165 if (size != sizeof(__u64))
9168 case offsetof(struct bpf_sock_ops, sk):
9169 if (size != sizeof(__u64))
9171 info->reg_type = PTR_TO_SOCKET_OR_NULL;
9173 case offsetof(struct bpf_sock_ops, skb_data):
9174 if (size != sizeof(__u64))
9176 info->reg_type = PTR_TO_PACKET;
9178 case offsetof(struct bpf_sock_ops, skb_data_end):
9179 if (size != sizeof(__u64))
9181 info->reg_type = PTR_TO_PACKET_END;
9183 case offsetof(struct bpf_sock_ops, skb_tcp_flags):
9184 bpf_ctx_record_field_size(info, size_default);
9185 return bpf_ctx_narrow_access_ok(off, size,
9187 case offsetof(struct bpf_sock_ops, skb_hwtstamp):
9188 if (size != sizeof(__u64))
9192 if (size != size_default)
9201 static int sk_skb_prologue(struct bpf_insn *insn_buf, bool direct_write,
9202 const struct bpf_prog *prog)
9204 return bpf_unclone_prologue(insn_buf, direct_write, prog, SK_DROP);
9207 static bool sk_skb_is_valid_access(int off, int size,
9208 enum bpf_access_type type,
9209 const struct bpf_prog *prog,
9210 struct bpf_insn_access_aux *info)
9213 case bpf_ctx_range(struct __sk_buff, tc_classid):
9214 case bpf_ctx_range(struct __sk_buff, data_meta):
9215 case bpf_ctx_range(struct __sk_buff, tstamp):
9216 case bpf_ctx_range(struct __sk_buff, wire_len):
9217 case bpf_ctx_range(struct __sk_buff, hwtstamp):
9221 if (type == BPF_WRITE) {
9223 case bpf_ctx_range(struct __sk_buff, tc_index):
9224 case bpf_ctx_range(struct __sk_buff, priority):
9232 case bpf_ctx_range(struct __sk_buff, mark):
9234 case bpf_ctx_range(struct __sk_buff, data):
9235 info->reg_type = PTR_TO_PACKET;
9237 case bpf_ctx_range(struct __sk_buff, data_end):
9238 info->reg_type = PTR_TO_PACKET_END;
9242 return bpf_skb_is_valid_access(off, size, type, prog, info);
9245 static bool sk_msg_is_valid_access(int off, int size,
9246 enum bpf_access_type type,
9247 const struct bpf_prog *prog,
9248 struct bpf_insn_access_aux *info)
9250 if (type == BPF_WRITE)
9253 if (off % size != 0)
9257 case offsetof(struct sk_msg_md, data):
9258 info->reg_type = PTR_TO_PACKET;
9259 if (size != sizeof(__u64))
9262 case offsetof(struct sk_msg_md, data_end):
9263 info->reg_type = PTR_TO_PACKET_END;
9264 if (size != sizeof(__u64))
9267 case offsetof(struct sk_msg_md, sk):
9268 if (size != sizeof(__u64))
9270 info->reg_type = PTR_TO_SOCKET;
9272 case bpf_ctx_range(struct sk_msg_md, family):
9273 case bpf_ctx_range(struct sk_msg_md, remote_ip4):
9274 case bpf_ctx_range(struct sk_msg_md, local_ip4):
9275 case bpf_ctx_range_till(struct sk_msg_md, remote_ip6[0], remote_ip6[3]):
9276 case bpf_ctx_range_till(struct sk_msg_md, local_ip6[0], local_ip6[3]):
9277 case bpf_ctx_range(struct sk_msg_md, remote_port):
9278 case bpf_ctx_range(struct sk_msg_md, local_port):
9279 case bpf_ctx_range(struct sk_msg_md, size):
9280 if (size != sizeof(__u32))
9289 static bool flow_dissector_is_valid_access(int off, int size,
9290 enum bpf_access_type type,
9291 const struct bpf_prog *prog,
9292 struct bpf_insn_access_aux *info)
9294 const int size_default = sizeof(__u32);
9296 if (off < 0 || off >= sizeof(struct __sk_buff))
9299 if (type == BPF_WRITE)
9303 case bpf_ctx_range(struct __sk_buff, data):
9304 if (size != size_default)
9306 info->reg_type = PTR_TO_PACKET;
9308 case bpf_ctx_range(struct __sk_buff, data_end):
9309 if (size != size_default)
9311 info->reg_type = PTR_TO_PACKET_END;
9313 case bpf_ctx_range_ptr(struct __sk_buff, flow_keys):
9314 if (size != sizeof(__u64))
9316 info->reg_type = PTR_TO_FLOW_KEYS;
9323 static u32 flow_dissector_convert_ctx_access(enum bpf_access_type type,
9324 const struct bpf_insn *si,
9325 struct bpf_insn *insn_buf,
9326 struct bpf_prog *prog,
9330 struct bpf_insn *insn = insn_buf;
9333 case offsetof(struct __sk_buff, data):
9334 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_flow_dissector, data),
9335 si->dst_reg, si->src_reg,
9336 offsetof(struct bpf_flow_dissector, data));
9339 case offsetof(struct __sk_buff, data_end):
9340 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_flow_dissector, data_end),
9341 si->dst_reg, si->src_reg,
9342 offsetof(struct bpf_flow_dissector, data_end));
9345 case offsetof(struct __sk_buff, flow_keys):
9346 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_flow_dissector, flow_keys),
9347 si->dst_reg, si->src_reg,
9348 offsetof(struct bpf_flow_dissector, flow_keys));
9352 return insn - insn_buf;
9355 static struct bpf_insn *bpf_convert_tstamp_type_read(const struct bpf_insn *si,
9356 struct bpf_insn *insn)
9358 __u8 value_reg = si->dst_reg;
9359 __u8 skb_reg = si->src_reg;
9360 /* AX is needed because src_reg and dst_reg could be the same */
9361 __u8 tmp_reg = BPF_REG_AX;
9363 *insn++ = BPF_LDX_MEM(BPF_B, tmp_reg, skb_reg,
9364 SKB_BF_MONO_TC_OFFSET);
9365 *insn++ = BPF_JMP32_IMM(BPF_JSET, tmp_reg,
9366 SKB_MONO_DELIVERY_TIME_MASK, 2);
9367 *insn++ = BPF_MOV32_IMM(value_reg, BPF_SKB_TSTAMP_UNSPEC);
9368 *insn++ = BPF_JMP_A(1);
9369 *insn++ = BPF_MOV32_IMM(value_reg, BPF_SKB_TSTAMP_DELIVERY_MONO);
9374 static struct bpf_insn *bpf_convert_shinfo_access(__u8 dst_reg, __u8 skb_reg,
9375 struct bpf_insn *insn)
9377 /* si->dst_reg = skb_shinfo(SKB); */
9378 #ifdef NET_SKBUFF_DATA_USES_OFFSET
9379 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, end),
9380 BPF_REG_AX, skb_reg,
9381 offsetof(struct sk_buff, end));
9382 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, head),
9384 offsetof(struct sk_buff, head));
9385 *insn++ = BPF_ALU64_REG(BPF_ADD, dst_reg, BPF_REG_AX);
9387 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, end),
9389 offsetof(struct sk_buff, end));
9395 static struct bpf_insn *bpf_convert_tstamp_read(const struct bpf_prog *prog,
9396 const struct bpf_insn *si,
9397 struct bpf_insn *insn)
9399 __u8 value_reg = si->dst_reg;
9400 __u8 skb_reg = si->src_reg;
9402 #ifdef CONFIG_NET_XGRESS
9403 /* If the tstamp_type is read,
9404 * the bpf prog is aware the tstamp could have delivery time.
9405 * Thus, read skb->tstamp as is if tstamp_type_access is true.
9407 if (!prog->tstamp_type_access) {
9408 /* AX is needed because src_reg and dst_reg could be the same */
9409 __u8 tmp_reg = BPF_REG_AX;
9411 *insn++ = BPF_LDX_MEM(BPF_B, tmp_reg, skb_reg, SKB_BF_MONO_TC_OFFSET);
9412 *insn++ = BPF_ALU32_IMM(BPF_AND, tmp_reg,
9413 TC_AT_INGRESS_MASK | SKB_MONO_DELIVERY_TIME_MASK);
9414 *insn++ = BPF_JMP32_IMM(BPF_JNE, tmp_reg,
9415 TC_AT_INGRESS_MASK | SKB_MONO_DELIVERY_TIME_MASK, 2);
9416 /* skb->tc_at_ingress && skb->mono_delivery_time,
9417 * read 0 as the (rcv) timestamp.
9419 *insn++ = BPF_MOV64_IMM(value_reg, 0);
9420 *insn++ = BPF_JMP_A(1);
9424 *insn++ = BPF_LDX_MEM(BPF_DW, value_reg, skb_reg,
9425 offsetof(struct sk_buff, tstamp));
9429 static struct bpf_insn *bpf_convert_tstamp_write(const struct bpf_prog *prog,
9430 const struct bpf_insn *si,
9431 struct bpf_insn *insn)
9433 __u8 value_reg = si->src_reg;
9434 __u8 skb_reg = si->dst_reg;
9436 #ifdef CONFIG_NET_XGRESS
9437 /* If the tstamp_type is read,
9438 * the bpf prog is aware the tstamp could have delivery time.
9439 * Thus, write skb->tstamp as is if tstamp_type_access is true.
9440 * Otherwise, writing at ingress will have to clear the
9441 * mono_delivery_time bit also.
9443 if (!prog->tstamp_type_access) {
9444 __u8 tmp_reg = BPF_REG_AX;
9446 *insn++ = BPF_LDX_MEM(BPF_B, tmp_reg, skb_reg, SKB_BF_MONO_TC_OFFSET);
9447 /* Writing __sk_buff->tstamp as ingress, goto <clear> */
9448 *insn++ = BPF_JMP32_IMM(BPF_JSET, tmp_reg, TC_AT_INGRESS_MASK, 1);
9450 *insn++ = BPF_JMP_A(2);
9451 /* <clear>: mono_delivery_time */
9452 *insn++ = BPF_ALU32_IMM(BPF_AND, tmp_reg, ~SKB_MONO_DELIVERY_TIME_MASK);
9453 *insn++ = BPF_STX_MEM(BPF_B, skb_reg, tmp_reg, SKB_BF_MONO_TC_OFFSET);
9457 /* <store>: skb->tstamp = tstamp */
9458 *insn++ = BPF_RAW_INSN(BPF_CLASS(si->code) | BPF_DW | BPF_MEM,
9459 skb_reg, value_reg, offsetof(struct sk_buff, tstamp), si->imm);
9463 #define BPF_EMIT_STORE(size, si, off) \
9464 BPF_RAW_INSN(BPF_CLASS((si)->code) | (size) | BPF_MEM, \
9465 (si)->dst_reg, (si)->src_reg, (off), (si)->imm)
9467 static u32 bpf_convert_ctx_access(enum bpf_access_type type,
9468 const struct bpf_insn *si,
9469 struct bpf_insn *insn_buf,
9470 struct bpf_prog *prog, u32 *target_size)
9472 struct bpf_insn *insn = insn_buf;
9476 case offsetof(struct __sk_buff, len):
9477 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
9478 bpf_target_off(struct sk_buff, len, 4,
9482 case offsetof(struct __sk_buff, protocol):
9483 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
9484 bpf_target_off(struct sk_buff, protocol, 2,
9488 case offsetof(struct __sk_buff, vlan_proto):
9489 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
9490 bpf_target_off(struct sk_buff, vlan_proto, 2,
9494 case offsetof(struct __sk_buff, priority):
9495 if (type == BPF_WRITE)
9496 *insn++ = BPF_EMIT_STORE(BPF_W, si,
9497 bpf_target_off(struct sk_buff, priority, 4,
9500 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
9501 bpf_target_off(struct sk_buff, priority, 4,
9505 case offsetof(struct __sk_buff, ingress_ifindex):
9506 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
9507 bpf_target_off(struct sk_buff, skb_iif, 4,
9511 case offsetof(struct __sk_buff, ifindex):
9512 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
9513 si->dst_reg, si->src_reg,
9514 offsetof(struct sk_buff, dev));
9515 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
9516 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
9517 bpf_target_off(struct net_device, ifindex, 4,
9521 case offsetof(struct __sk_buff, hash):
9522 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
9523 bpf_target_off(struct sk_buff, hash, 4,
9527 case offsetof(struct __sk_buff, mark):
9528 if (type == BPF_WRITE)
9529 *insn++ = BPF_EMIT_STORE(BPF_W, si,
9530 bpf_target_off(struct sk_buff, mark, 4,
9533 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
9534 bpf_target_off(struct sk_buff, mark, 4,
9538 case offsetof(struct __sk_buff, pkt_type):
9540 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->src_reg,
9542 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, PKT_TYPE_MAX);
9543 #ifdef __BIG_ENDIAN_BITFIELD
9544 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, 5);
9548 case offsetof(struct __sk_buff, queue_mapping):
9549 if (type == BPF_WRITE) {
9550 u32 off = bpf_target_off(struct sk_buff, queue_mapping, 2, target_size);
9552 if (BPF_CLASS(si->code) == BPF_ST && si->imm >= NO_QUEUE_MAPPING) {
9553 *insn++ = BPF_JMP_A(0); /* noop */
9557 if (BPF_CLASS(si->code) == BPF_STX)
9558 *insn++ = BPF_JMP_IMM(BPF_JGE, si->src_reg, NO_QUEUE_MAPPING, 1);
9559 *insn++ = BPF_EMIT_STORE(BPF_H, si, off);
9561 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
9562 bpf_target_off(struct sk_buff,
9568 case offsetof(struct __sk_buff, vlan_present):
9569 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
9570 bpf_target_off(struct sk_buff,
9571 vlan_all, 4, target_size));
9572 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
9573 *insn++ = BPF_ALU32_IMM(BPF_MOV, si->dst_reg, 1);
9576 case offsetof(struct __sk_buff, vlan_tci):
9577 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
9578 bpf_target_off(struct sk_buff, vlan_tci, 2,
9582 case offsetof(struct __sk_buff, cb[0]) ...
9583 offsetofend(struct __sk_buff, cb[4]) - 1:
9584 BUILD_BUG_ON(sizeof_field(struct qdisc_skb_cb, data) < 20);
9585 BUILD_BUG_ON((offsetof(struct sk_buff, cb) +
9586 offsetof(struct qdisc_skb_cb, data)) %
9589 prog->cb_access = 1;
9591 off -= offsetof(struct __sk_buff, cb[0]);
9592 off += offsetof(struct sk_buff, cb);
9593 off += offsetof(struct qdisc_skb_cb, data);
9594 if (type == BPF_WRITE)
9595 *insn++ = BPF_EMIT_STORE(BPF_SIZE(si->code), si, off);
9597 *insn++ = BPF_LDX_MEM(BPF_SIZE(si->code), si->dst_reg,
9601 case offsetof(struct __sk_buff, tc_classid):
9602 BUILD_BUG_ON(sizeof_field(struct qdisc_skb_cb, tc_classid) != 2);
9605 off -= offsetof(struct __sk_buff, tc_classid);
9606 off += offsetof(struct sk_buff, cb);
9607 off += offsetof(struct qdisc_skb_cb, tc_classid);
9609 if (type == BPF_WRITE)
9610 *insn++ = BPF_EMIT_STORE(BPF_H, si, off);
9612 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg,
9616 case offsetof(struct __sk_buff, data):
9617 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
9618 si->dst_reg, si->src_reg,
9619 offsetof(struct sk_buff, data));
9622 case offsetof(struct __sk_buff, data_meta):
9624 off -= offsetof(struct __sk_buff, data_meta);
9625 off += offsetof(struct sk_buff, cb);
9626 off += offsetof(struct bpf_skb_data_end, data_meta);
9627 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
9631 case offsetof(struct __sk_buff, data_end):
9633 off -= offsetof(struct __sk_buff, data_end);
9634 off += offsetof(struct sk_buff, cb);
9635 off += offsetof(struct bpf_skb_data_end, data_end);
9636 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
9640 case offsetof(struct __sk_buff, tc_index):
9641 #ifdef CONFIG_NET_SCHED
9642 if (type == BPF_WRITE)
9643 *insn++ = BPF_EMIT_STORE(BPF_H, si,
9644 bpf_target_off(struct sk_buff, tc_index, 2,
9647 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
9648 bpf_target_off(struct sk_buff, tc_index, 2,
9652 if (type == BPF_WRITE)
9653 *insn++ = BPF_MOV64_REG(si->dst_reg, si->dst_reg);
9655 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
9659 case offsetof(struct __sk_buff, napi_id):
9660 #if defined(CONFIG_NET_RX_BUSY_POLL)
9661 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
9662 bpf_target_off(struct sk_buff, napi_id, 4,
9664 *insn++ = BPF_JMP_IMM(BPF_JGE, si->dst_reg, MIN_NAPI_ID, 1);
9665 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
9668 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
9671 case offsetof(struct __sk_buff, family):
9672 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_family) != 2);
9674 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
9675 si->dst_reg, si->src_reg,
9676 offsetof(struct sk_buff, sk));
9677 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
9678 bpf_target_off(struct sock_common,
9682 case offsetof(struct __sk_buff, remote_ip4):
9683 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_daddr) != 4);
9685 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
9686 si->dst_reg, si->src_reg,
9687 offsetof(struct sk_buff, sk));
9688 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
9689 bpf_target_off(struct sock_common,
9693 case offsetof(struct __sk_buff, local_ip4):
9694 BUILD_BUG_ON(sizeof_field(struct sock_common,
9695 skc_rcv_saddr) != 4);
9697 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
9698 si->dst_reg, si->src_reg,
9699 offsetof(struct sk_buff, sk));
9700 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
9701 bpf_target_off(struct sock_common,
9705 case offsetof(struct __sk_buff, remote_ip6[0]) ...
9706 offsetof(struct __sk_buff, remote_ip6[3]):
9707 #if IS_ENABLED(CONFIG_IPV6)
9708 BUILD_BUG_ON(sizeof_field(struct sock_common,
9709 skc_v6_daddr.s6_addr32[0]) != 4);
9712 off -= offsetof(struct __sk_buff, remote_ip6[0]);
9714 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
9715 si->dst_reg, si->src_reg,
9716 offsetof(struct sk_buff, sk));
9717 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
9718 offsetof(struct sock_common,
9719 skc_v6_daddr.s6_addr32[0]) +
9722 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
9725 case offsetof(struct __sk_buff, local_ip6[0]) ...
9726 offsetof(struct __sk_buff, local_ip6[3]):
9727 #if IS_ENABLED(CONFIG_IPV6)
9728 BUILD_BUG_ON(sizeof_field(struct sock_common,
9729 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
9732 off -= offsetof(struct __sk_buff, local_ip6[0]);
9734 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
9735 si->dst_reg, si->src_reg,
9736 offsetof(struct sk_buff, sk));
9737 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
9738 offsetof(struct sock_common,
9739 skc_v6_rcv_saddr.s6_addr32[0]) +
9742 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
9746 case offsetof(struct __sk_buff, remote_port):
9747 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_dport) != 2);
9749 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
9750 si->dst_reg, si->src_reg,
9751 offsetof(struct sk_buff, sk));
9752 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
9753 bpf_target_off(struct sock_common,
9756 #ifndef __BIG_ENDIAN_BITFIELD
9757 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
9761 case offsetof(struct __sk_buff, local_port):
9762 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_num) != 2);
9764 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
9765 si->dst_reg, si->src_reg,
9766 offsetof(struct sk_buff, sk));
9767 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
9768 bpf_target_off(struct sock_common,
9769 skc_num, 2, target_size));
9772 case offsetof(struct __sk_buff, tstamp):
9773 BUILD_BUG_ON(sizeof_field(struct sk_buff, tstamp) != 8);
9775 if (type == BPF_WRITE)
9776 insn = bpf_convert_tstamp_write(prog, si, insn);
9778 insn = bpf_convert_tstamp_read(prog, si, insn);
9781 case offsetof(struct __sk_buff, tstamp_type):
9782 insn = bpf_convert_tstamp_type_read(si, insn);
9785 case offsetof(struct __sk_buff, gso_segs):
9786 insn = bpf_convert_shinfo_access(si->dst_reg, si->src_reg, insn);
9787 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct skb_shared_info, gso_segs),
9788 si->dst_reg, si->dst_reg,
9789 bpf_target_off(struct skb_shared_info,
9793 case offsetof(struct __sk_buff, gso_size):
9794 insn = bpf_convert_shinfo_access(si->dst_reg, si->src_reg, insn);
9795 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct skb_shared_info, gso_size),
9796 si->dst_reg, si->dst_reg,
9797 bpf_target_off(struct skb_shared_info,
9801 case offsetof(struct __sk_buff, wire_len):
9802 BUILD_BUG_ON(sizeof_field(struct qdisc_skb_cb, pkt_len) != 4);
9805 off -= offsetof(struct __sk_buff, wire_len);
9806 off += offsetof(struct sk_buff, cb);
9807 off += offsetof(struct qdisc_skb_cb, pkt_len);
9809 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, off);
9812 case offsetof(struct __sk_buff, sk):
9813 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
9814 si->dst_reg, si->src_reg,
9815 offsetof(struct sk_buff, sk));
9817 case offsetof(struct __sk_buff, hwtstamp):
9818 BUILD_BUG_ON(sizeof_field(struct skb_shared_hwtstamps, hwtstamp) != 8);
9819 BUILD_BUG_ON(offsetof(struct skb_shared_hwtstamps, hwtstamp) != 0);
9821 insn = bpf_convert_shinfo_access(si->dst_reg, si->src_reg, insn);
9822 *insn++ = BPF_LDX_MEM(BPF_DW,
9823 si->dst_reg, si->dst_reg,
9824 bpf_target_off(struct skb_shared_info,
9830 return insn - insn_buf;
9833 u32 bpf_sock_convert_ctx_access(enum bpf_access_type type,
9834 const struct bpf_insn *si,
9835 struct bpf_insn *insn_buf,
9836 struct bpf_prog *prog, u32 *target_size)
9838 struct bpf_insn *insn = insn_buf;
9842 case offsetof(struct bpf_sock, bound_dev_if):
9843 BUILD_BUG_ON(sizeof_field(struct sock, sk_bound_dev_if) != 4);
9845 if (type == BPF_WRITE)
9846 *insn++ = BPF_EMIT_STORE(BPF_W, si,
9847 offsetof(struct sock, sk_bound_dev_if));
9849 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
9850 offsetof(struct sock, sk_bound_dev_if));
9853 case offsetof(struct bpf_sock, mark):
9854 BUILD_BUG_ON(sizeof_field(struct sock, sk_mark) != 4);
9856 if (type == BPF_WRITE)
9857 *insn++ = BPF_EMIT_STORE(BPF_W, si,
9858 offsetof(struct sock, sk_mark));
9860 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
9861 offsetof(struct sock, sk_mark));
9864 case offsetof(struct bpf_sock, priority):
9865 BUILD_BUG_ON(sizeof_field(struct sock, sk_priority) != 4);
9867 if (type == BPF_WRITE)
9868 *insn++ = BPF_EMIT_STORE(BPF_W, si,
9869 offsetof(struct sock, sk_priority));
9871 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
9872 offsetof(struct sock, sk_priority));
9875 case offsetof(struct bpf_sock, family):
9876 *insn++ = BPF_LDX_MEM(
9877 BPF_FIELD_SIZEOF(struct sock_common, skc_family),
9878 si->dst_reg, si->src_reg,
9879 bpf_target_off(struct sock_common,
9881 sizeof_field(struct sock_common,
9886 case offsetof(struct bpf_sock, type):
9887 *insn++ = BPF_LDX_MEM(
9888 BPF_FIELD_SIZEOF(struct sock, sk_type),
9889 si->dst_reg, si->src_reg,
9890 bpf_target_off(struct sock, sk_type,
9891 sizeof_field(struct sock, sk_type),
9895 case offsetof(struct bpf_sock, protocol):
9896 *insn++ = BPF_LDX_MEM(
9897 BPF_FIELD_SIZEOF(struct sock, sk_protocol),
9898 si->dst_reg, si->src_reg,
9899 bpf_target_off(struct sock, sk_protocol,
9900 sizeof_field(struct sock, sk_protocol),
9904 case offsetof(struct bpf_sock, src_ip4):
9905 *insn++ = BPF_LDX_MEM(
9906 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
9907 bpf_target_off(struct sock_common, skc_rcv_saddr,
9908 sizeof_field(struct sock_common,
9913 case offsetof(struct bpf_sock, dst_ip4):
9914 *insn++ = BPF_LDX_MEM(
9915 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
9916 bpf_target_off(struct sock_common, skc_daddr,
9917 sizeof_field(struct sock_common,
9922 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
9923 #if IS_ENABLED(CONFIG_IPV6)
9925 off -= offsetof(struct bpf_sock, src_ip6[0]);
9926 *insn++ = BPF_LDX_MEM(
9927 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
9930 skc_v6_rcv_saddr.s6_addr32[0],
9931 sizeof_field(struct sock_common,
9932 skc_v6_rcv_saddr.s6_addr32[0]),
9933 target_size) + off);
9936 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
9940 case bpf_ctx_range_till(struct bpf_sock, dst_ip6[0], dst_ip6[3]):
9941 #if IS_ENABLED(CONFIG_IPV6)
9943 off -= offsetof(struct bpf_sock, dst_ip6[0]);
9944 *insn++ = BPF_LDX_MEM(
9945 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
9946 bpf_target_off(struct sock_common,
9947 skc_v6_daddr.s6_addr32[0],
9948 sizeof_field(struct sock_common,
9949 skc_v6_daddr.s6_addr32[0]),
9950 target_size) + off);
9952 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
9957 case offsetof(struct bpf_sock, src_port):
9958 *insn++ = BPF_LDX_MEM(
9959 BPF_FIELD_SIZEOF(struct sock_common, skc_num),
9960 si->dst_reg, si->src_reg,
9961 bpf_target_off(struct sock_common, skc_num,
9962 sizeof_field(struct sock_common,
9967 case offsetof(struct bpf_sock, dst_port):
9968 *insn++ = BPF_LDX_MEM(
9969 BPF_FIELD_SIZEOF(struct sock_common, skc_dport),
9970 si->dst_reg, si->src_reg,
9971 bpf_target_off(struct sock_common, skc_dport,
9972 sizeof_field(struct sock_common,
9977 case offsetof(struct bpf_sock, state):
9978 *insn++ = BPF_LDX_MEM(
9979 BPF_FIELD_SIZEOF(struct sock_common, skc_state),
9980 si->dst_reg, si->src_reg,
9981 bpf_target_off(struct sock_common, skc_state,
9982 sizeof_field(struct sock_common,
9986 case offsetof(struct bpf_sock, rx_queue_mapping):
9987 #ifdef CONFIG_SOCK_RX_QUEUE_MAPPING
9988 *insn++ = BPF_LDX_MEM(
9989 BPF_FIELD_SIZEOF(struct sock, sk_rx_queue_mapping),
9990 si->dst_reg, si->src_reg,
9991 bpf_target_off(struct sock, sk_rx_queue_mapping,
9992 sizeof_field(struct sock,
9993 sk_rx_queue_mapping),
9995 *insn++ = BPF_JMP_IMM(BPF_JNE, si->dst_reg, NO_QUEUE_MAPPING,
9997 *insn++ = BPF_MOV64_IMM(si->dst_reg, -1);
9999 *insn++ = BPF_MOV64_IMM(si->dst_reg, -1);
10005 return insn - insn_buf;
10008 static u32 tc_cls_act_convert_ctx_access(enum bpf_access_type type,
10009 const struct bpf_insn *si,
10010 struct bpf_insn *insn_buf,
10011 struct bpf_prog *prog, u32 *target_size)
10013 struct bpf_insn *insn = insn_buf;
10016 case offsetof(struct __sk_buff, ifindex):
10017 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
10018 si->dst_reg, si->src_reg,
10019 offsetof(struct sk_buff, dev));
10020 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
10021 bpf_target_off(struct net_device, ifindex, 4,
10025 return bpf_convert_ctx_access(type, si, insn_buf, prog,
10029 return insn - insn_buf;
10032 static u32 xdp_convert_ctx_access(enum bpf_access_type type,
10033 const struct bpf_insn *si,
10034 struct bpf_insn *insn_buf,
10035 struct bpf_prog *prog, u32 *target_size)
10037 struct bpf_insn *insn = insn_buf;
10040 case offsetof(struct xdp_md, data):
10041 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data),
10042 si->dst_reg, si->src_reg,
10043 offsetof(struct xdp_buff, data));
10045 case offsetof(struct xdp_md, data_meta):
10046 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_meta),
10047 si->dst_reg, si->src_reg,
10048 offsetof(struct xdp_buff, data_meta));
10050 case offsetof(struct xdp_md, data_end):
10051 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_end),
10052 si->dst_reg, si->src_reg,
10053 offsetof(struct xdp_buff, data_end));
10055 case offsetof(struct xdp_md, ingress_ifindex):
10056 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, rxq),
10057 si->dst_reg, si->src_reg,
10058 offsetof(struct xdp_buff, rxq));
10059 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_rxq_info, dev),
10060 si->dst_reg, si->dst_reg,
10061 offsetof(struct xdp_rxq_info, dev));
10062 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
10063 offsetof(struct net_device, ifindex));
10065 case offsetof(struct xdp_md, rx_queue_index):
10066 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, rxq),
10067 si->dst_reg, si->src_reg,
10068 offsetof(struct xdp_buff, rxq));
10069 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
10070 offsetof(struct xdp_rxq_info,
10073 case offsetof(struct xdp_md, egress_ifindex):
10074 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, txq),
10075 si->dst_reg, si->src_reg,
10076 offsetof(struct xdp_buff, txq));
10077 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_txq_info, dev),
10078 si->dst_reg, si->dst_reg,
10079 offsetof(struct xdp_txq_info, dev));
10080 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
10081 offsetof(struct net_device, ifindex));
10085 return insn - insn_buf;
10088 /* SOCK_ADDR_LOAD_NESTED_FIELD() loads Nested Field S.F.NF where S is type of
10089 * context Structure, F is Field in context structure that contains a pointer
10090 * to Nested Structure of type NS that has the field NF.
10092 * SIZE encodes the load size (BPF_B, BPF_H, etc). It's up to caller to make
10093 * sure that SIZE is not greater than actual size of S.F.NF.
10095 * If offset OFF is provided, the load happens from that offset relative to
10098 #define SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, SIZE, OFF) \
10100 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), si->dst_reg, \
10101 si->src_reg, offsetof(S, F)); \
10102 *insn++ = BPF_LDX_MEM( \
10103 SIZE, si->dst_reg, si->dst_reg, \
10104 bpf_target_off(NS, NF, sizeof_field(NS, NF), \
10109 #define SOCK_ADDR_LOAD_NESTED_FIELD(S, NS, F, NF) \
10110 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, \
10111 BPF_FIELD_SIZEOF(NS, NF), 0)
10113 /* SOCK_ADDR_STORE_NESTED_FIELD_OFF() has semantic similar to
10114 * SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF() but for store operation.
10116 * In addition it uses Temporary Field TF (member of struct S) as the 3rd
10117 * "register" since two registers available in convert_ctx_access are not
10118 * enough: we can't override neither SRC, since it contains value to store, nor
10119 * DST since it contains pointer to context that may be used by later
10120 * instructions. But we need a temporary place to save pointer to nested
10121 * structure whose field we want to store to.
10123 #define SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, SIZE, OFF, TF) \
10125 int tmp_reg = BPF_REG_9; \
10126 if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg) \
10128 if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg) \
10130 *insn++ = BPF_STX_MEM(BPF_DW, si->dst_reg, tmp_reg, \
10131 offsetof(S, TF)); \
10132 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), tmp_reg, \
10133 si->dst_reg, offsetof(S, F)); \
10134 *insn++ = BPF_RAW_INSN(SIZE | BPF_MEM | BPF_CLASS(si->code), \
10135 tmp_reg, si->src_reg, \
10136 bpf_target_off(NS, NF, sizeof_field(NS, NF), \
10140 *insn++ = BPF_LDX_MEM(BPF_DW, tmp_reg, si->dst_reg, \
10141 offsetof(S, TF)); \
10144 #define SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, SIZE, OFF, \
10147 if (type == BPF_WRITE) { \
10148 SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, SIZE, \
10151 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF( \
10152 S, NS, F, NF, SIZE, OFF); \
10156 #define SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD(S, NS, F, NF, TF) \
10157 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF( \
10158 S, NS, F, NF, BPF_FIELD_SIZEOF(NS, NF), 0, TF)
10160 static u32 sock_addr_convert_ctx_access(enum bpf_access_type type,
10161 const struct bpf_insn *si,
10162 struct bpf_insn *insn_buf,
10163 struct bpf_prog *prog, u32 *target_size)
10165 int off, port_size = sizeof_field(struct sockaddr_in6, sin6_port);
10166 struct bpf_insn *insn = insn_buf;
10169 case offsetof(struct bpf_sock_addr, user_family):
10170 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
10171 struct sockaddr, uaddr, sa_family);
10174 case offsetof(struct bpf_sock_addr, user_ip4):
10175 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
10176 struct bpf_sock_addr_kern, struct sockaddr_in, uaddr,
10177 sin_addr, BPF_SIZE(si->code), 0, tmp_reg);
10180 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
10182 off -= offsetof(struct bpf_sock_addr, user_ip6[0]);
10183 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
10184 struct bpf_sock_addr_kern, struct sockaddr_in6, uaddr,
10185 sin6_addr.s6_addr32[0], BPF_SIZE(si->code), off,
10189 case offsetof(struct bpf_sock_addr, user_port):
10190 /* To get port we need to know sa_family first and then treat
10191 * sockaddr as either sockaddr_in or sockaddr_in6.
10192 * Though we can simplify since port field has same offset and
10193 * size in both structures.
10194 * Here we check this invariant and use just one of the
10195 * structures if it's true.
10197 BUILD_BUG_ON(offsetof(struct sockaddr_in, sin_port) !=
10198 offsetof(struct sockaddr_in6, sin6_port));
10199 BUILD_BUG_ON(sizeof_field(struct sockaddr_in, sin_port) !=
10200 sizeof_field(struct sockaddr_in6, sin6_port));
10201 /* Account for sin6_port being smaller than user_port. */
10202 port_size = min(port_size, BPF_LDST_BYTES(si));
10203 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
10204 struct bpf_sock_addr_kern, struct sockaddr_in6, uaddr,
10205 sin6_port, bytes_to_bpf_size(port_size), 0, tmp_reg);
10208 case offsetof(struct bpf_sock_addr, family):
10209 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
10210 struct sock, sk, sk_family);
10213 case offsetof(struct bpf_sock_addr, type):
10214 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
10215 struct sock, sk, sk_type);
10218 case offsetof(struct bpf_sock_addr, protocol):
10219 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
10220 struct sock, sk, sk_protocol);
10223 case offsetof(struct bpf_sock_addr, msg_src_ip4):
10224 /* Treat t_ctx as struct in_addr for msg_src_ip4. */
10225 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
10226 struct bpf_sock_addr_kern, struct in_addr, t_ctx,
10227 s_addr, BPF_SIZE(si->code), 0, tmp_reg);
10230 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
10233 off -= offsetof(struct bpf_sock_addr, msg_src_ip6[0]);
10234 /* Treat t_ctx as struct in6_addr for msg_src_ip6. */
10235 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
10236 struct bpf_sock_addr_kern, struct in6_addr, t_ctx,
10237 s6_addr32[0], BPF_SIZE(si->code), off, tmp_reg);
10239 case offsetof(struct bpf_sock_addr, sk):
10240 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_addr_kern, sk),
10241 si->dst_reg, si->src_reg,
10242 offsetof(struct bpf_sock_addr_kern, sk));
10246 return insn - insn_buf;
10249 static u32 sock_ops_convert_ctx_access(enum bpf_access_type type,
10250 const struct bpf_insn *si,
10251 struct bpf_insn *insn_buf,
10252 struct bpf_prog *prog,
10255 struct bpf_insn *insn = insn_buf;
10258 /* Helper macro for adding read access to tcp_sock or sock fields. */
10259 #define SOCK_OPS_GET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ) \
10261 int fullsock_reg = si->dst_reg, reg = BPF_REG_9, jmp = 2; \
10262 BUILD_BUG_ON(sizeof_field(OBJ, OBJ_FIELD) > \
10263 sizeof_field(struct bpf_sock_ops, BPF_FIELD)); \
10264 if (si->dst_reg == reg || si->src_reg == reg) \
10266 if (si->dst_reg == reg || si->src_reg == reg) \
10268 if (si->dst_reg == si->src_reg) { \
10269 *insn++ = BPF_STX_MEM(BPF_DW, si->src_reg, reg, \
10270 offsetof(struct bpf_sock_ops_kern, \
10272 fullsock_reg = reg; \
10275 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
10276 struct bpf_sock_ops_kern, \
10278 fullsock_reg, si->src_reg, \
10279 offsetof(struct bpf_sock_ops_kern, \
10281 *insn++ = BPF_JMP_IMM(BPF_JEQ, fullsock_reg, 0, jmp); \
10282 if (si->dst_reg == si->src_reg) \
10283 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->src_reg, \
10284 offsetof(struct bpf_sock_ops_kern, \
10286 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
10287 struct bpf_sock_ops_kern, sk),\
10288 si->dst_reg, si->src_reg, \
10289 offsetof(struct bpf_sock_ops_kern, sk));\
10290 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(OBJ, \
10292 si->dst_reg, si->dst_reg, \
10293 offsetof(OBJ, OBJ_FIELD)); \
10294 if (si->dst_reg == si->src_reg) { \
10295 *insn++ = BPF_JMP_A(1); \
10296 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->src_reg, \
10297 offsetof(struct bpf_sock_ops_kern, \
10302 #define SOCK_OPS_GET_SK() \
10304 int fullsock_reg = si->dst_reg, reg = BPF_REG_9, jmp = 1; \
10305 if (si->dst_reg == reg || si->src_reg == reg) \
10307 if (si->dst_reg == reg || si->src_reg == reg) \
10309 if (si->dst_reg == si->src_reg) { \
10310 *insn++ = BPF_STX_MEM(BPF_DW, si->src_reg, reg, \
10311 offsetof(struct bpf_sock_ops_kern, \
10313 fullsock_reg = reg; \
10316 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
10317 struct bpf_sock_ops_kern, \
10319 fullsock_reg, si->src_reg, \
10320 offsetof(struct bpf_sock_ops_kern, \
10322 *insn++ = BPF_JMP_IMM(BPF_JEQ, fullsock_reg, 0, jmp); \
10323 if (si->dst_reg == si->src_reg) \
10324 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->src_reg, \
10325 offsetof(struct bpf_sock_ops_kern, \
10327 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
10328 struct bpf_sock_ops_kern, sk),\
10329 si->dst_reg, si->src_reg, \
10330 offsetof(struct bpf_sock_ops_kern, sk));\
10331 if (si->dst_reg == si->src_reg) { \
10332 *insn++ = BPF_JMP_A(1); \
10333 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->src_reg, \
10334 offsetof(struct bpf_sock_ops_kern, \
10339 #define SOCK_OPS_GET_TCP_SOCK_FIELD(FIELD) \
10340 SOCK_OPS_GET_FIELD(FIELD, FIELD, struct tcp_sock)
10342 /* Helper macro for adding write access to tcp_sock or sock fields.
10343 * The macro is called with two registers, dst_reg which contains a pointer
10344 * to ctx (context) and src_reg which contains the value that should be
10345 * stored. However, we need an additional register since we cannot overwrite
10346 * dst_reg because it may be used later in the program.
10347 * Instead we "borrow" one of the other register. We first save its value
10348 * into a new (temp) field in bpf_sock_ops_kern, use it, and then restore
10349 * it at the end of the macro.
10351 #define SOCK_OPS_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ) \
10353 int reg = BPF_REG_9; \
10354 BUILD_BUG_ON(sizeof_field(OBJ, OBJ_FIELD) > \
10355 sizeof_field(struct bpf_sock_ops, BPF_FIELD)); \
10356 if (si->dst_reg == reg || si->src_reg == reg) \
10358 if (si->dst_reg == reg || si->src_reg == reg) \
10360 *insn++ = BPF_STX_MEM(BPF_DW, si->dst_reg, reg, \
10361 offsetof(struct bpf_sock_ops_kern, \
10363 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
10364 struct bpf_sock_ops_kern, \
10366 reg, si->dst_reg, \
10367 offsetof(struct bpf_sock_ops_kern, \
10369 *insn++ = BPF_JMP_IMM(BPF_JEQ, reg, 0, 2); \
10370 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
10371 struct bpf_sock_ops_kern, sk),\
10372 reg, si->dst_reg, \
10373 offsetof(struct bpf_sock_ops_kern, sk));\
10374 *insn++ = BPF_RAW_INSN(BPF_FIELD_SIZEOF(OBJ, OBJ_FIELD) | \
10375 BPF_MEM | BPF_CLASS(si->code), \
10376 reg, si->src_reg, \
10377 offsetof(OBJ, OBJ_FIELD), \
10379 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->dst_reg, \
10380 offsetof(struct bpf_sock_ops_kern, \
10384 #define SOCK_OPS_GET_OR_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ, TYPE) \
10386 if (TYPE == BPF_WRITE) \
10387 SOCK_OPS_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ); \
10389 SOCK_OPS_GET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ); \
10393 case offsetof(struct bpf_sock_ops, op):
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, op));
10400 case offsetof(struct bpf_sock_ops, replylong[0]) ...
10401 offsetof(struct bpf_sock_ops, replylong[3]):
10402 BUILD_BUG_ON(sizeof_field(struct bpf_sock_ops, reply) !=
10403 sizeof_field(struct bpf_sock_ops_kern, reply));
10404 BUILD_BUG_ON(sizeof_field(struct bpf_sock_ops, replylong) !=
10405 sizeof_field(struct bpf_sock_ops_kern, replylong));
10407 off -= offsetof(struct bpf_sock_ops, replylong[0]);
10408 off += offsetof(struct bpf_sock_ops_kern, replylong[0]);
10409 if (type == BPF_WRITE)
10410 *insn++ = BPF_EMIT_STORE(BPF_W, si, off);
10412 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
10416 case offsetof(struct bpf_sock_ops, family):
10417 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_family) != 2);
10419 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10420 struct bpf_sock_ops_kern, sk),
10421 si->dst_reg, si->src_reg,
10422 offsetof(struct bpf_sock_ops_kern, sk));
10423 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
10424 offsetof(struct sock_common, skc_family));
10427 case offsetof(struct bpf_sock_ops, remote_ip4):
10428 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_daddr) != 4);
10430 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10431 struct bpf_sock_ops_kern, sk),
10432 si->dst_reg, si->src_reg,
10433 offsetof(struct bpf_sock_ops_kern, sk));
10434 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
10435 offsetof(struct sock_common, skc_daddr));
10438 case offsetof(struct bpf_sock_ops, local_ip4):
10439 BUILD_BUG_ON(sizeof_field(struct sock_common,
10440 skc_rcv_saddr) != 4);
10442 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10443 struct bpf_sock_ops_kern, sk),
10444 si->dst_reg, si->src_reg,
10445 offsetof(struct bpf_sock_ops_kern, sk));
10446 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
10447 offsetof(struct sock_common,
10451 case offsetof(struct bpf_sock_ops, remote_ip6[0]) ...
10452 offsetof(struct bpf_sock_ops, remote_ip6[3]):
10453 #if IS_ENABLED(CONFIG_IPV6)
10454 BUILD_BUG_ON(sizeof_field(struct sock_common,
10455 skc_v6_daddr.s6_addr32[0]) != 4);
10458 off -= offsetof(struct bpf_sock_ops, remote_ip6[0]);
10459 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10460 struct bpf_sock_ops_kern, sk),
10461 si->dst_reg, si->src_reg,
10462 offsetof(struct bpf_sock_ops_kern, sk));
10463 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
10464 offsetof(struct sock_common,
10465 skc_v6_daddr.s6_addr32[0]) +
10468 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
10472 case offsetof(struct bpf_sock_ops, local_ip6[0]) ...
10473 offsetof(struct bpf_sock_ops, local_ip6[3]):
10474 #if IS_ENABLED(CONFIG_IPV6)
10475 BUILD_BUG_ON(sizeof_field(struct sock_common,
10476 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
10479 off -= offsetof(struct bpf_sock_ops, local_ip6[0]);
10480 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10481 struct bpf_sock_ops_kern, sk),
10482 si->dst_reg, si->src_reg,
10483 offsetof(struct bpf_sock_ops_kern, sk));
10484 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
10485 offsetof(struct sock_common,
10486 skc_v6_rcv_saddr.s6_addr32[0]) +
10489 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
10493 case offsetof(struct bpf_sock_ops, remote_port):
10494 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_dport) != 2);
10496 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10497 struct bpf_sock_ops_kern, sk),
10498 si->dst_reg, si->src_reg,
10499 offsetof(struct bpf_sock_ops_kern, sk));
10500 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
10501 offsetof(struct sock_common, skc_dport));
10502 #ifndef __BIG_ENDIAN_BITFIELD
10503 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
10507 case offsetof(struct bpf_sock_ops, local_port):
10508 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_num) != 2);
10510 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10511 struct bpf_sock_ops_kern, sk),
10512 si->dst_reg, si->src_reg,
10513 offsetof(struct bpf_sock_ops_kern, sk));
10514 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
10515 offsetof(struct sock_common, skc_num));
10518 case offsetof(struct bpf_sock_ops, is_fullsock):
10519 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10520 struct bpf_sock_ops_kern,
10522 si->dst_reg, si->src_reg,
10523 offsetof(struct bpf_sock_ops_kern,
10527 case offsetof(struct bpf_sock_ops, state):
10528 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_state) != 1);
10530 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10531 struct bpf_sock_ops_kern, sk),
10532 si->dst_reg, si->src_reg,
10533 offsetof(struct bpf_sock_ops_kern, sk));
10534 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->dst_reg,
10535 offsetof(struct sock_common, skc_state));
10538 case offsetof(struct bpf_sock_ops, rtt_min):
10539 BUILD_BUG_ON(sizeof_field(struct tcp_sock, rtt_min) !=
10540 sizeof(struct minmax));
10541 BUILD_BUG_ON(sizeof(struct minmax) <
10542 sizeof(struct minmax_sample));
10544 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10545 struct bpf_sock_ops_kern, sk),
10546 si->dst_reg, si->src_reg,
10547 offsetof(struct bpf_sock_ops_kern, sk));
10548 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
10549 offsetof(struct tcp_sock, rtt_min) +
10550 sizeof_field(struct minmax_sample, t));
10553 case offsetof(struct bpf_sock_ops, bpf_sock_ops_cb_flags):
10554 SOCK_OPS_GET_FIELD(bpf_sock_ops_cb_flags, bpf_sock_ops_cb_flags,
10558 case offsetof(struct bpf_sock_ops, sk_txhash):
10559 SOCK_OPS_GET_OR_SET_FIELD(sk_txhash, sk_txhash,
10560 struct sock, type);
10562 case offsetof(struct bpf_sock_ops, snd_cwnd):
10563 SOCK_OPS_GET_TCP_SOCK_FIELD(snd_cwnd);
10565 case offsetof(struct bpf_sock_ops, srtt_us):
10566 SOCK_OPS_GET_TCP_SOCK_FIELD(srtt_us);
10568 case offsetof(struct bpf_sock_ops, snd_ssthresh):
10569 SOCK_OPS_GET_TCP_SOCK_FIELD(snd_ssthresh);
10571 case offsetof(struct bpf_sock_ops, rcv_nxt):
10572 SOCK_OPS_GET_TCP_SOCK_FIELD(rcv_nxt);
10574 case offsetof(struct bpf_sock_ops, snd_nxt):
10575 SOCK_OPS_GET_TCP_SOCK_FIELD(snd_nxt);
10577 case offsetof(struct bpf_sock_ops, snd_una):
10578 SOCK_OPS_GET_TCP_SOCK_FIELD(snd_una);
10580 case offsetof(struct bpf_sock_ops, mss_cache):
10581 SOCK_OPS_GET_TCP_SOCK_FIELD(mss_cache);
10583 case offsetof(struct bpf_sock_ops, ecn_flags):
10584 SOCK_OPS_GET_TCP_SOCK_FIELD(ecn_flags);
10586 case offsetof(struct bpf_sock_ops, rate_delivered):
10587 SOCK_OPS_GET_TCP_SOCK_FIELD(rate_delivered);
10589 case offsetof(struct bpf_sock_ops, rate_interval_us):
10590 SOCK_OPS_GET_TCP_SOCK_FIELD(rate_interval_us);
10592 case offsetof(struct bpf_sock_ops, packets_out):
10593 SOCK_OPS_GET_TCP_SOCK_FIELD(packets_out);
10595 case offsetof(struct bpf_sock_ops, retrans_out):
10596 SOCK_OPS_GET_TCP_SOCK_FIELD(retrans_out);
10598 case offsetof(struct bpf_sock_ops, total_retrans):
10599 SOCK_OPS_GET_TCP_SOCK_FIELD(total_retrans);
10601 case offsetof(struct bpf_sock_ops, segs_in):
10602 SOCK_OPS_GET_TCP_SOCK_FIELD(segs_in);
10604 case offsetof(struct bpf_sock_ops, data_segs_in):
10605 SOCK_OPS_GET_TCP_SOCK_FIELD(data_segs_in);
10607 case offsetof(struct bpf_sock_ops, segs_out):
10608 SOCK_OPS_GET_TCP_SOCK_FIELD(segs_out);
10610 case offsetof(struct bpf_sock_ops, data_segs_out):
10611 SOCK_OPS_GET_TCP_SOCK_FIELD(data_segs_out);
10613 case offsetof(struct bpf_sock_ops, lost_out):
10614 SOCK_OPS_GET_TCP_SOCK_FIELD(lost_out);
10616 case offsetof(struct bpf_sock_ops, sacked_out):
10617 SOCK_OPS_GET_TCP_SOCK_FIELD(sacked_out);
10619 case offsetof(struct bpf_sock_ops, bytes_received):
10620 SOCK_OPS_GET_TCP_SOCK_FIELD(bytes_received);
10622 case offsetof(struct bpf_sock_ops, bytes_acked):
10623 SOCK_OPS_GET_TCP_SOCK_FIELD(bytes_acked);
10625 case offsetof(struct bpf_sock_ops, sk):
10628 case offsetof(struct bpf_sock_ops, skb_data_end):
10629 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_ops_kern,
10631 si->dst_reg, si->src_reg,
10632 offsetof(struct bpf_sock_ops_kern,
10635 case offsetof(struct bpf_sock_ops, skb_data):
10636 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_ops_kern,
10638 si->dst_reg, si->src_reg,
10639 offsetof(struct bpf_sock_ops_kern,
10641 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
10642 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
10643 si->dst_reg, si->dst_reg,
10644 offsetof(struct sk_buff, data));
10646 case offsetof(struct bpf_sock_ops, skb_len):
10647 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_ops_kern,
10649 si->dst_reg, si->src_reg,
10650 offsetof(struct bpf_sock_ops_kern,
10652 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
10653 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, len),
10654 si->dst_reg, si->dst_reg,
10655 offsetof(struct sk_buff, len));
10657 case offsetof(struct bpf_sock_ops, skb_tcp_flags):
10658 off = offsetof(struct sk_buff, cb);
10659 off += offsetof(struct tcp_skb_cb, tcp_flags);
10660 *target_size = sizeof_field(struct tcp_skb_cb, tcp_flags);
10661 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_ops_kern,
10663 si->dst_reg, si->src_reg,
10664 offsetof(struct bpf_sock_ops_kern,
10666 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
10667 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct tcp_skb_cb,
10669 si->dst_reg, si->dst_reg, off);
10671 case offsetof(struct bpf_sock_ops, skb_hwtstamp): {
10672 struct bpf_insn *jmp_on_null_skb;
10674 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_ops_kern,
10676 si->dst_reg, si->src_reg,
10677 offsetof(struct bpf_sock_ops_kern,
10679 /* Reserve one insn to test skb == NULL */
10680 jmp_on_null_skb = insn++;
10681 insn = bpf_convert_shinfo_access(si->dst_reg, si->dst_reg, insn);
10682 *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
10683 bpf_target_off(struct skb_shared_info,
10686 *jmp_on_null_skb = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0,
10687 insn - jmp_on_null_skb - 1);
10691 return insn - insn_buf;
10694 /* data_end = skb->data + skb_headlen() */
10695 static struct bpf_insn *bpf_convert_data_end_access(const struct bpf_insn *si,
10696 struct bpf_insn *insn)
10699 int temp_reg_off = offsetof(struct sk_buff, cb) +
10700 offsetof(struct sk_skb_cb, temp_reg);
10702 if (si->src_reg == si->dst_reg) {
10703 /* We need an extra register, choose and save a register. */
10705 if (si->src_reg == reg || si->dst_reg == reg)
10707 if (si->src_reg == reg || si->dst_reg == reg)
10709 *insn++ = BPF_STX_MEM(BPF_DW, si->src_reg, reg, temp_reg_off);
10714 /* reg = skb->data */
10715 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
10717 offsetof(struct sk_buff, data));
10718 /* AX = skb->len */
10719 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, len),
10720 BPF_REG_AX, si->src_reg,
10721 offsetof(struct sk_buff, len));
10722 /* reg = skb->data + skb->len */
10723 *insn++ = BPF_ALU64_REG(BPF_ADD, reg, BPF_REG_AX);
10724 /* AX = skb->data_len */
10725 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data_len),
10726 BPF_REG_AX, si->src_reg,
10727 offsetof(struct sk_buff, data_len));
10729 /* reg = skb->data + skb->len - skb->data_len */
10730 *insn++ = BPF_ALU64_REG(BPF_SUB, reg, BPF_REG_AX);
10732 if (si->src_reg == si->dst_reg) {
10733 /* Restore the saved register */
10734 *insn++ = BPF_MOV64_REG(BPF_REG_AX, si->src_reg);
10735 *insn++ = BPF_MOV64_REG(si->dst_reg, reg);
10736 *insn++ = BPF_LDX_MEM(BPF_DW, reg, BPF_REG_AX, temp_reg_off);
10742 static u32 sk_skb_convert_ctx_access(enum bpf_access_type type,
10743 const struct bpf_insn *si,
10744 struct bpf_insn *insn_buf,
10745 struct bpf_prog *prog, u32 *target_size)
10747 struct bpf_insn *insn = insn_buf;
10751 case offsetof(struct __sk_buff, data_end):
10752 insn = bpf_convert_data_end_access(si, insn);
10754 case offsetof(struct __sk_buff, cb[0]) ...
10755 offsetofend(struct __sk_buff, cb[4]) - 1:
10756 BUILD_BUG_ON(sizeof_field(struct sk_skb_cb, data) < 20);
10757 BUILD_BUG_ON((offsetof(struct sk_buff, cb) +
10758 offsetof(struct sk_skb_cb, data)) %
10761 prog->cb_access = 1;
10763 off -= offsetof(struct __sk_buff, cb[0]);
10764 off += offsetof(struct sk_buff, cb);
10765 off += offsetof(struct sk_skb_cb, data);
10766 if (type == BPF_WRITE)
10767 *insn++ = BPF_EMIT_STORE(BPF_SIZE(si->code), si, off);
10769 *insn++ = BPF_LDX_MEM(BPF_SIZE(si->code), si->dst_reg,
10775 return bpf_convert_ctx_access(type, si, insn_buf, prog,
10779 return insn - insn_buf;
10782 static u32 sk_msg_convert_ctx_access(enum bpf_access_type type,
10783 const struct bpf_insn *si,
10784 struct bpf_insn *insn_buf,
10785 struct bpf_prog *prog, u32 *target_size)
10787 struct bpf_insn *insn = insn_buf;
10788 #if IS_ENABLED(CONFIG_IPV6)
10792 /* convert ctx uses the fact sg element is first in struct */
10793 BUILD_BUG_ON(offsetof(struct sk_msg, sg) != 0);
10796 case offsetof(struct sk_msg_md, data):
10797 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg, data),
10798 si->dst_reg, si->src_reg,
10799 offsetof(struct sk_msg, data));
10801 case offsetof(struct sk_msg_md, data_end):
10802 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg, data_end),
10803 si->dst_reg, si->src_reg,
10804 offsetof(struct sk_msg, data_end));
10806 case offsetof(struct sk_msg_md, family):
10807 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_family) != 2);
10809 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10810 struct sk_msg, sk),
10811 si->dst_reg, si->src_reg,
10812 offsetof(struct sk_msg, sk));
10813 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
10814 offsetof(struct sock_common, skc_family));
10817 case offsetof(struct sk_msg_md, remote_ip4):
10818 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_daddr) != 4);
10820 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10821 struct sk_msg, sk),
10822 si->dst_reg, si->src_reg,
10823 offsetof(struct sk_msg, sk));
10824 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
10825 offsetof(struct sock_common, skc_daddr));
10828 case offsetof(struct sk_msg_md, local_ip4):
10829 BUILD_BUG_ON(sizeof_field(struct sock_common,
10830 skc_rcv_saddr) != 4);
10832 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10833 struct sk_msg, sk),
10834 si->dst_reg, si->src_reg,
10835 offsetof(struct sk_msg, sk));
10836 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
10837 offsetof(struct sock_common,
10841 case offsetof(struct sk_msg_md, remote_ip6[0]) ...
10842 offsetof(struct sk_msg_md, remote_ip6[3]):
10843 #if IS_ENABLED(CONFIG_IPV6)
10844 BUILD_BUG_ON(sizeof_field(struct sock_common,
10845 skc_v6_daddr.s6_addr32[0]) != 4);
10848 off -= offsetof(struct sk_msg_md, remote_ip6[0]);
10849 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10850 struct sk_msg, sk),
10851 si->dst_reg, si->src_reg,
10852 offsetof(struct sk_msg, sk));
10853 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
10854 offsetof(struct sock_common,
10855 skc_v6_daddr.s6_addr32[0]) +
10858 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
10862 case offsetof(struct sk_msg_md, local_ip6[0]) ...
10863 offsetof(struct sk_msg_md, local_ip6[3]):
10864 #if IS_ENABLED(CONFIG_IPV6)
10865 BUILD_BUG_ON(sizeof_field(struct sock_common,
10866 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
10869 off -= offsetof(struct sk_msg_md, local_ip6[0]);
10870 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10871 struct sk_msg, sk),
10872 si->dst_reg, si->src_reg,
10873 offsetof(struct sk_msg, sk));
10874 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
10875 offsetof(struct sock_common,
10876 skc_v6_rcv_saddr.s6_addr32[0]) +
10879 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
10883 case offsetof(struct sk_msg_md, remote_port):
10884 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_dport) != 2);
10886 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10887 struct sk_msg, sk),
10888 si->dst_reg, si->src_reg,
10889 offsetof(struct sk_msg, sk));
10890 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
10891 offsetof(struct sock_common, skc_dport));
10892 #ifndef __BIG_ENDIAN_BITFIELD
10893 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
10897 case offsetof(struct sk_msg_md, local_port):
10898 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_num) != 2);
10900 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10901 struct sk_msg, sk),
10902 si->dst_reg, si->src_reg,
10903 offsetof(struct sk_msg, sk));
10904 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
10905 offsetof(struct sock_common, skc_num));
10908 case offsetof(struct sk_msg_md, size):
10909 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg_sg, size),
10910 si->dst_reg, si->src_reg,
10911 offsetof(struct sk_msg_sg, size));
10914 case offsetof(struct sk_msg_md, sk):
10915 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg, sk),
10916 si->dst_reg, si->src_reg,
10917 offsetof(struct sk_msg, sk));
10921 return insn - insn_buf;
10924 const struct bpf_verifier_ops sk_filter_verifier_ops = {
10925 .get_func_proto = sk_filter_func_proto,
10926 .is_valid_access = sk_filter_is_valid_access,
10927 .convert_ctx_access = bpf_convert_ctx_access,
10928 .gen_ld_abs = bpf_gen_ld_abs,
10931 const struct bpf_prog_ops sk_filter_prog_ops = {
10932 .test_run = bpf_prog_test_run_skb,
10935 const struct bpf_verifier_ops tc_cls_act_verifier_ops = {
10936 .get_func_proto = tc_cls_act_func_proto,
10937 .is_valid_access = tc_cls_act_is_valid_access,
10938 .convert_ctx_access = tc_cls_act_convert_ctx_access,
10939 .gen_prologue = tc_cls_act_prologue,
10940 .gen_ld_abs = bpf_gen_ld_abs,
10941 .btf_struct_access = tc_cls_act_btf_struct_access,
10944 const struct bpf_prog_ops tc_cls_act_prog_ops = {
10945 .test_run = bpf_prog_test_run_skb,
10948 const struct bpf_verifier_ops xdp_verifier_ops = {
10949 .get_func_proto = xdp_func_proto,
10950 .is_valid_access = xdp_is_valid_access,
10951 .convert_ctx_access = xdp_convert_ctx_access,
10952 .gen_prologue = bpf_noop_prologue,
10953 .btf_struct_access = xdp_btf_struct_access,
10956 const struct bpf_prog_ops xdp_prog_ops = {
10957 .test_run = bpf_prog_test_run_xdp,
10960 const struct bpf_verifier_ops cg_skb_verifier_ops = {
10961 .get_func_proto = cg_skb_func_proto,
10962 .is_valid_access = cg_skb_is_valid_access,
10963 .convert_ctx_access = bpf_convert_ctx_access,
10966 const struct bpf_prog_ops cg_skb_prog_ops = {
10967 .test_run = bpf_prog_test_run_skb,
10970 const struct bpf_verifier_ops lwt_in_verifier_ops = {
10971 .get_func_proto = lwt_in_func_proto,
10972 .is_valid_access = lwt_is_valid_access,
10973 .convert_ctx_access = bpf_convert_ctx_access,
10976 const struct bpf_prog_ops lwt_in_prog_ops = {
10977 .test_run = bpf_prog_test_run_skb,
10980 const struct bpf_verifier_ops lwt_out_verifier_ops = {
10981 .get_func_proto = lwt_out_func_proto,
10982 .is_valid_access = lwt_is_valid_access,
10983 .convert_ctx_access = bpf_convert_ctx_access,
10986 const struct bpf_prog_ops lwt_out_prog_ops = {
10987 .test_run = bpf_prog_test_run_skb,
10990 const struct bpf_verifier_ops lwt_xmit_verifier_ops = {
10991 .get_func_proto = lwt_xmit_func_proto,
10992 .is_valid_access = lwt_is_valid_access,
10993 .convert_ctx_access = bpf_convert_ctx_access,
10994 .gen_prologue = tc_cls_act_prologue,
10997 const struct bpf_prog_ops lwt_xmit_prog_ops = {
10998 .test_run = bpf_prog_test_run_skb,
11001 const struct bpf_verifier_ops lwt_seg6local_verifier_ops = {
11002 .get_func_proto = lwt_seg6local_func_proto,
11003 .is_valid_access = lwt_is_valid_access,
11004 .convert_ctx_access = bpf_convert_ctx_access,
11007 const struct bpf_prog_ops lwt_seg6local_prog_ops = {
11008 .test_run = bpf_prog_test_run_skb,
11011 const struct bpf_verifier_ops cg_sock_verifier_ops = {
11012 .get_func_proto = sock_filter_func_proto,
11013 .is_valid_access = sock_filter_is_valid_access,
11014 .convert_ctx_access = bpf_sock_convert_ctx_access,
11017 const struct bpf_prog_ops cg_sock_prog_ops = {
11020 const struct bpf_verifier_ops cg_sock_addr_verifier_ops = {
11021 .get_func_proto = sock_addr_func_proto,
11022 .is_valid_access = sock_addr_is_valid_access,
11023 .convert_ctx_access = sock_addr_convert_ctx_access,
11026 const struct bpf_prog_ops cg_sock_addr_prog_ops = {
11029 const struct bpf_verifier_ops sock_ops_verifier_ops = {
11030 .get_func_proto = sock_ops_func_proto,
11031 .is_valid_access = sock_ops_is_valid_access,
11032 .convert_ctx_access = sock_ops_convert_ctx_access,
11035 const struct bpf_prog_ops sock_ops_prog_ops = {
11038 const struct bpf_verifier_ops sk_skb_verifier_ops = {
11039 .get_func_proto = sk_skb_func_proto,
11040 .is_valid_access = sk_skb_is_valid_access,
11041 .convert_ctx_access = sk_skb_convert_ctx_access,
11042 .gen_prologue = sk_skb_prologue,
11045 const struct bpf_prog_ops sk_skb_prog_ops = {
11048 const struct bpf_verifier_ops sk_msg_verifier_ops = {
11049 .get_func_proto = sk_msg_func_proto,
11050 .is_valid_access = sk_msg_is_valid_access,
11051 .convert_ctx_access = sk_msg_convert_ctx_access,
11052 .gen_prologue = bpf_noop_prologue,
11055 const struct bpf_prog_ops sk_msg_prog_ops = {
11058 const struct bpf_verifier_ops flow_dissector_verifier_ops = {
11059 .get_func_proto = flow_dissector_func_proto,
11060 .is_valid_access = flow_dissector_is_valid_access,
11061 .convert_ctx_access = flow_dissector_convert_ctx_access,
11064 const struct bpf_prog_ops flow_dissector_prog_ops = {
11065 .test_run = bpf_prog_test_run_flow_dissector,
11068 int sk_detach_filter(struct sock *sk)
11071 struct sk_filter *filter;
11073 if (sock_flag(sk, SOCK_FILTER_LOCKED))
11076 filter = rcu_dereference_protected(sk->sk_filter,
11077 lockdep_sock_is_held(sk));
11079 RCU_INIT_POINTER(sk->sk_filter, NULL);
11080 sk_filter_uncharge(sk, filter);
11086 EXPORT_SYMBOL_GPL(sk_detach_filter);
11088 int sk_get_filter(struct sock *sk, sockptr_t optval, unsigned int len)
11090 struct sock_fprog_kern *fprog;
11091 struct sk_filter *filter;
11094 sockopt_lock_sock(sk);
11095 filter = rcu_dereference_protected(sk->sk_filter,
11096 lockdep_sock_is_held(sk));
11100 /* We're copying the filter that has been originally attached,
11101 * so no conversion/decode needed anymore. eBPF programs that
11102 * have no original program cannot be dumped through this.
11105 fprog = filter->prog->orig_prog;
11111 /* User space only enquires number of filter blocks. */
11115 if (len < fprog->len)
11119 if (copy_to_sockptr(optval, fprog->filter, bpf_classic_proglen(fprog)))
11122 /* Instead of bytes, the API requests to return the number
11123 * of filter blocks.
11127 sockopt_release_sock(sk);
11132 static void bpf_init_reuseport_kern(struct sk_reuseport_kern *reuse_kern,
11133 struct sock_reuseport *reuse,
11134 struct sock *sk, struct sk_buff *skb,
11135 struct sock *migrating_sk,
11138 reuse_kern->skb = skb;
11139 reuse_kern->sk = sk;
11140 reuse_kern->selected_sk = NULL;
11141 reuse_kern->migrating_sk = migrating_sk;
11142 reuse_kern->data_end = skb->data + skb_headlen(skb);
11143 reuse_kern->hash = hash;
11144 reuse_kern->reuseport_id = reuse->reuseport_id;
11145 reuse_kern->bind_inany = reuse->bind_inany;
11148 struct sock *bpf_run_sk_reuseport(struct sock_reuseport *reuse, struct sock *sk,
11149 struct bpf_prog *prog, struct sk_buff *skb,
11150 struct sock *migrating_sk,
11153 struct sk_reuseport_kern reuse_kern;
11154 enum sk_action action;
11156 bpf_init_reuseport_kern(&reuse_kern, reuse, sk, skb, migrating_sk, hash);
11157 action = bpf_prog_run(prog, &reuse_kern);
11159 if (action == SK_PASS)
11160 return reuse_kern.selected_sk;
11162 return ERR_PTR(-ECONNREFUSED);
11165 BPF_CALL_4(sk_select_reuseport, struct sk_reuseport_kern *, reuse_kern,
11166 struct bpf_map *, map, void *, key, u32, flags)
11168 bool is_sockarray = map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY;
11169 struct sock_reuseport *reuse;
11170 struct sock *selected_sk;
11172 selected_sk = map->ops->map_lookup_elem(map, key);
11176 reuse = rcu_dereference(selected_sk->sk_reuseport_cb);
11178 /* Lookup in sock_map can return TCP ESTABLISHED sockets. */
11179 if (sk_is_refcounted(selected_sk))
11180 sock_put(selected_sk);
11182 /* reuseport_array has only sk with non NULL sk_reuseport_cb.
11183 * The only (!reuse) case here is - the sk has already been
11184 * unhashed (e.g. by close()), so treat it as -ENOENT.
11186 * Other maps (e.g. sock_map) do not provide this guarantee and
11187 * the sk may never be in the reuseport group to begin with.
11189 return is_sockarray ? -ENOENT : -EINVAL;
11192 if (unlikely(reuse->reuseport_id != reuse_kern->reuseport_id)) {
11193 struct sock *sk = reuse_kern->sk;
11195 if (sk->sk_protocol != selected_sk->sk_protocol)
11196 return -EPROTOTYPE;
11197 else if (sk->sk_family != selected_sk->sk_family)
11198 return -EAFNOSUPPORT;
11200 /* Catch all. Likely bound to a different sockaddr. */
11204 reuse_kern->selected_sk = selected_sk;
11209 static const struct bpf_func_proto sk_select_reuseport_proto = {
11210 .func = sk_select_reuseport,
11212 .ret_type = RET_INTEGER,
11213 .arg1_type = ARG_PTR_TO_CTX,
11214 .arg2_type = ARG_CONST_MAP_PTR,
11215 .arg3_type = ARG_PTR_TO_MAP_KEY,
11216 .arg4_type = ARG_ANYTHING,
11219 BPF_CALL_4(sk_reuseport_load_bytes,
11220 const struct sk_reuseport_kern *, reuse_kern, u32, offset,
11221 void *, to, u32, len)
11223 return ____bpf_skb_load_bytes(reuse_kern->skb, offset, to, len);
11226 static const struct bpf_func_proto sk_reuseport_load_bytes_proto = {
11227 .func = sk_reuseport_load_bytes,
11229 .ret_type = RET_INTEGER,
11230 .arg1_type = ARG_PTR_TO_CTX,
11231 .arg2_type = ARG_ANYTHING,
11232 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
11233 .arg4_type = ARG_CONST_SIZE,
11236 BPF_CALL_5(sk_reuseport_load_bytes_relative,
11237 const struct sk_reuseport_kern *, reuse_kern, u32, offset,
11238 void *, to, u32, len, u32, start_header)
11240 return ____bpf_skb_load_bytes_relative(reuse_kern->skb, offset, to,
11241 len, start_header);
11244 static const struct bpf_func_proto sk_reuseport_load_bytes_relative_proto = {
11245 .func = sk_reuseport_load_bytes_relative,
11247 .ret_type = RET_INTEGER,
11248 .arg1_type = ARG_PTR_TO_CTX,
11249 .arg2_type = ARG_ANYTHING,
11250 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
11251 .arg4_type = ARG_CONST_SIZE,
11252 .arg5_type = ARG_ANYTHING,
11255 static const struct bpf_func_proto *
11256 sk_reuseport_func_proto(enum bpf_func_id func_id,
11257 const struct bpf_prog *prog)
11260 case BPF_FUNC_sk_select_reuseport:
11261 return &sk_select_reuseport_proto;
11262 case BPF_FUNC_skb_load_bytes:
11263 return &sk_reuseport_load_bytes_proto;
11264 case BPF_FUNC_skb_load_bytes_relative:
11265 return &sk_reuseport_load_bytes_relative_proto;
11266 case BPF_FUNC_get_socket_cookie:
11267 return &bpf_get_socket_ptr_cookie_proto;
11268 case BPF_FUNC_ktime_get_coarse_ns:
11269 return &bpf_ktime_get_coarse_ns_proto;
11271 return bpf_base_func_proto(func_id);
11276 sk_reuseport_is_valid_access(int off, int size,
11277 enum bpf_access_type type,
11278 const struct bpf_prog *prog,
11279 struct bpf_insn_access_aux *info)
11281 const u32 size_default = sizeof(__u32);
11283 if (off < 0 || off >= sizeof(struct sk_reuseport_md) ||
11284 off % size || type != BPF_READ)
11288 case offsetof(struct sk_reuseport_md, data):
11289 info->reg_type = PTR_TO_PACKET;
11290 return size == sizeof(__u64);
11292 case offsetof(struct sk_reuseport_md, data_end):
11293 info->reg_type = PTR_TO_PACKET_END;
11294 return size == sizeof(__u64);
11296 case offsetof(struct sk_reuseport_md, hash):
11297 return size == size_default;
11299 case offsetof(struct sk_reuseport_md, sk):
11300 info->reg_type = PTR_TO_SOCKET;
11301 return size == sizeof(__u64);
11303 case offsetof(struct sk_reuseport_md, migrating_sk):
11304 info->reg_type = PTR_TO_SOCK_COMMON_OR_NULL;
11305 return size == sizeof(__u64);
11307 /* Fields that allow narrowing */
11308 case bpf_ctx_range(struct sk_reuseport_md, eth_protocol):
11309 if (size < sizeof_field(struct sk_buff, protocol))
11312 case bpf_ctx_range(struct sk_reuseport_md, ip_protocol):
11313 case bpf_ctx_range(struct sk_reuseport_md, bind_inany):
11314 case bpf_ctx_range(struct sk_reuseport_md, len):
11315 bpf_ctx_record_field_size(info, size_default);
11316 return bpf_ctx_narrow_access_ok(off, size, size_default);
11323 #define SK_REUSEPORT_LOAD_FIELD(F) ({ \
11324 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_reuseport_kern, F), \
11325 si->dst_reg, si->src_reg, \
11326 bpf_target_off(struct sk_reuseport_kern, F, \
11327 sizeof_field(struct sk_reuseport_kern, F), \
11331 #define SK_REUSEPORT_LOAD_SKB_FIELD(SKB_FIELD) \
11332 SOCK_ADDR_LOAD_NESTED_FIELD(struct sk_reuseport_kern, \
11337 #define SK_REUSEPORT_LOAD_SK_FIELD(SK_FIELD) \
11338 SOCK_ADDR_LOAD_NESTED_FIELD(struct sk_reuseport_kern, \
11343 static u32 sk_reuseport_convert_ctx_access(enum bpf_access_type type,
11344 const struct bpf_insn *si,
11345 struct bpf_insn *insn_buf,
11346 struct bpf_prog *prog,
11349 struct bpf_insn *insn = insn_buf;
11352 case offsetof(struct sk_reuseport_md, data):
11353 SK_REUSEPORT_LOAD_SKB_FIELD(data);
11356 case offsetof(struct sk_reuseport_md, len):
11357 SK_REUSEPORT_LOAD_SKB_FIELD(len);
11360 case offsetof(struct sk_reuseport_md, eth_protocol):
11361 SK_REUSEPORT_LOAD_SKB_FIELD(protocol);
11364 case offsetof(struct sk_reuseport_md, ip_protocol):
11365 SK_REUSEPORT_LOAD_SK_FIELD(sk_protocol);
11368 case offsetof(struct sk_reuseport_md, data_end):
11369 SK_REUSEPORT_LOAD_FIELD(data_end);
11372 case offsetof(struct sk_reuseport_md, hash):
11373 SK_REUSEPORT_LOAD_FIELD(hash);
11376 case offsetof(struct sk_reuseport_md, bind_inany):
11377 SK_REUSEPORT_LOAD_FIELD(bind_inany);
11380 case offsetof(struct sk_reuseport_md, sk):
11381 SK_REUSEPORT_LOAD_FIELD(sk);
11384 case offsetof(struct sk_reuseport_md, migrating_sk):
11385 SK_REUSEPORT_LOAD_FIELD(migrating_sk);
11389 return insn - insn_buf;
11392 const struct bpf_verifier_ops sk_reuseport_verifier_ops = {
11393 .get_func_proto = sk_reuseport_func_proto,
11394 .is_valid_access = sk_reuseport_is_valid_access,
11395 .convert_ctx_access = sk_reuseport_convert_ctx_access,
11398 const struct bpf_prog_ops sk_reuseport_prog_ops = {
11401 DEFINE_STATIC_KEY_FALSE(bpf_sk_lookup_enabled);
11402 EXPORT_SYMBOL(bpf_sk_lookup_enabled);
11404 BPF_CALL_3(bpf_sk_lookup_assign, struct bpf_sk_lookup_kern *, ctx,
11405 struct sock *, sk, u64, flags)
11407 if (unlikely(flags & ~(BPF_SK_LOOKUP_F_REPLACE |
11408 BPF_SK_LOOKUP_F_NO_REUSEPORT)))
11410 if (unlikely(sk && sk_is_refcounted(sk)))
11411 return -ESOCKTNOSUPPORT; /* reject non-RCU freed sockets */
11412 if (unlikely(sk && sk_is_tcp(sk) && sk->sk_state != TCP_LISTEN))
11413 return -ESOCKTNOSUPPORT; /* only accept TCP socket in LISTEN */
11414 if (unlikely(sk && sk_is_udp(sk) && sk->sk_state != TCP_CLOSE))
11415 return -ESOCKTNOSUPPORT; /* only accept UDP socket in CLOSE */
11417 /* Check if socket is suitable for packet L3/L4 protocol */
11418 if (sk && sk->sk_protocol != ctx->protocol)
11419 return -EPROTOTYPE;
11420 if (sk && sk->sk_family != ctx->family &&
11421 (sk->sk_family == AF_INET || ipv6_only_sock(sk)))
11422 return -EAFNOSUPPORT;
11424 if (ctx->selected_sk && !(flags & BPF_SK_LOOKUP_F_REPLACE))
11427 /* Select socket as lookup result */
11428 ctx->selected_sk = sk;
11429 ctx->no_reuseport = flags & BPF_SK_LOOKUP_F_NO_REUSEPORT;
11433 static const struct bpf_func_proto bpf_sk_lookup_assign_proto = {
11434 .func = bpf_sk_lookup_assign,
11436 .ret_type = RET_INTEGER,
11437 .arg1_type = ARG_PTR_TO_CTX,
11438 .arg2_type = ARG_PTR_TO_SOCKET_OR_NULL,
11439 .arg3_type = ARG_ANYTHING,
11442 static const struct bpf_func_proto *
11443 sk_lookup_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
11446 case BPF_FUNC_perf_event_output:
11447 return &bpf_event_output_data_proto;
11448 case BPF_FUNC_sk_assign:
11449 return &bpf_sk_lookup_assign_proto;
11450 case BPF_FUNC_sk_release:
11451 return &bpf_sk_release_proto;
11453 return bpf_sk_base_func_proto(func_id);
11457 static bool sk_lookup_is_valid_access(int off, int size,
11458 enum bpf_access_type type,
11459 const struct bpf_prog *prog,
11460 struct bpf_insn_access_aux *info)
11462 if (off < 0 || off >= sizeof(struct bpf_sk_lookup))
11464 if (off % size != 0)
11466 if (type != BPF_READ)
11470 case offsetof(struct bpf_sk_lookup, sk):
11471 info->reg_type = PTR_TO_SOCKET_OR_NULL;
11472 return size == sizeof(__u64);
11474 case bpf_ctx_range(struct bpf_sk_lookup, family):
11475 case bpf_ctx_range(struct bpf_sk_lookup, protocol):
11476 case bpf_ctx_range(struct bpf_sk_lookup, remote_ip4):
11477 case bpf_ctx_range(struct bpf_sk_lookup, local_ip4):
11478 case bpf_ctx_range_till(struct bpf_sk_lookup, remote_ip6[0], remote_ip6[3]):
11479 case bpf_ctx_range_till(struct bpf_sk_lookup, local_ip6[0], local_ip6[3]):
11480 case bpf_ctx_range(struct bpf_sk_lookup, local_port):
11481 case bpf_ctx_range(struct bpf_sk_lookup, ingress_ifindex):
11482 bpf_ctx_record_field_size(info, sizeof(__u32));
11483 return bpf_ctx_narrow_access_ok(off, size, sizeof(__u32));
11485 case bpf_ctx_range(struct bpf_sk_lookup, remote_port):
11486 /* Allow 4-byte access to 2-byte field for backward compatibility */
11487 if (size == sizeof(__u32))
11489 bpf_ctx_record_field_size(info, sizeof(__be16));
11490 return bpf_ctx_narrow_access_ok(off, size, sizeof(__be16));
11492 case offsetofend(struct bpf_sk_lookup, remote_port) ...
11493 offsetof(struct bpf_sk_lookup, local_ip4) - 1:
11494 /* Allow access to zero padding for backward compatibility */
11495 bpf_ctx_record_field_size(info, sizeof(__u16));
11496 return bpf_ctx_narrow_access_ok(off, size, sizeof(__u16));
11503 static u32 sk_lookup_convert_ctx_access(enum bpf_access_type type,
11504 const struct bpf_insn *si,
11505 struct bpf_insn *insn_buf,
11506 struct bpf_prog *prog,
11509 struct bpf_insn *insn = insn_buf;
11512 case offsetof(struct bpf_sk_lookup, sk):
11513 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg, si->src_reg,
11514 offsetof(struct bpf_sk_lookup_kern, selected_sk));
11517 case offsetof(struct bpf_sk_lookup, family):
11518 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
11519 bpf_target_off(struct bpf_sk_lookup_kern,
11520 family, 2, target_size));
11523 case offsetof(struct bpf_sk_lookup, protocol):
11524 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
11525 bpf_target_off(struct bpf_sk_lookup_kern,
11526 protocol, 2, target_size));
11529 case offsetof(struct bpf_sk_lookup, remote_ip4):
11530 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
11531 bpf_target_off(struct bpf_sk_lookup_kern,
11532 v4.saddr, 4, target_size));
11535 case offsetof(struct bpf_sk_lookup, local_ip4):
11536 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
11537 bpf_target_off(struct bpf_sk_lookup_kern,
11538 v4.daddr, 4, target_size));
11541 case bpf_ctx_range_till(struct bpf_sk_lookup,
11542 remote_ip6[0], remote_ip6[3]): {
11543 #if IS_ENABLED(CONFIG_IPV6)
11546 off -= offsetof(struct bpf_sk_lookup, remote_ip6[0]);
11547 off += bpf_target_off(struct in6_addr, s6_addr32[0], 4, target_size);
11548 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg, si->src_reg,
11549 offsetof(struct bpf_sk_lookup_kern, v6.saddr));
11550 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
11551 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, off);
11553 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
11557 case bpf_ctx_range_till(struct bpf_sk_lookup,
11558 local_ip6[0], local_ip6[3]): {
11559 #if IS_ENABLED(CONFIG_IPV6)
11562 off -= offsetof(struct bpf_sk_lookup, local_ip6[0]);
11563 off += bpf_target_off(struct in6_addr, s6_addr32[0], 4, target_size);
11564 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg, si->src_reg,
11565 offsetof(struct bpf_sk_lookup_kern, v6.daddr));
11566 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
11567 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, off);
11569 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
11573 case offsetof(struct bpf_sk_lookup, remote_port):
11574 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
11575 bpf_target_off(struct bpf_sk_lookup_kern,
11576 sport, 2, target_size));
11579 case offsetofend(struct bpf_sk_lookup, remote_port):
11581 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
11584 case offsetof(struct bpf_sk_lookup, local_port):
11585 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
11586 bpf_target_off(struct bpf_sk_lookup_kern,
11587 dport, 2, target_size));
11590 case offsetof(struct bpf_sk_lookup, ingress_ifindex):
11591 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
11592 bpf_target_off(struct bpf_sk_lookup_kern,
11593 ingress_ifindex, 4, target_size));
11597 return insn - insn_buf;
11600 const struct bpf_prog_ops sk_lookup_prog_ops = {
11601 .test_run = bpf_prog_test_run_sk_lookup,
11604 const struct bpf_verifier_ops sk_lookup_verifier_ops = {
11605 .get_func_proto = sk_lookup_func_proto,
11606 .is_valid_access = sk_lookup_is_valid_access,
11607 .convert_ctx_access = sk_lookup_convert_ctx_access,
11610 #endif /* CONFIG_INET */
11612 DEFINE_BPF_DISPATCHER(xdp)
11614 void bpf_prog_change_xdp(struct bpf_prog *prev_prog, struct bpf_prog *prog)
11616 bpf_dispatcher_change_prog(BPF_DISPATCHER_PTR(xdp), prev_prog, prog);
11619 BTF_ID_LIST_GLOBAL(btf_sock_ids, MAX_BTF_SOCK_TYPE)
11620 #define BTF_SOCK_TYPE(name, type) BTF_ID(struct, type)
11622 #undef BTF_SOCK_TYPE
11624 BPF_CALL_1(bpf_skc_to_tcp6_sock, struct sock *, sk)
11626 /* tcp6_sock type is not generated in dwarf and hence btf,
11627 * trigger an explicit type generation here.
11629 BTF_TYPE_EMIT(struct tcp6_sock);
11630 if (sk && sk_fullsock(sk) && sk->sk_protocol == IPPROTO_TCP &&
11631 sk->sk_family == AF_INET6)
11632 return (unsigned long)sk;
11634 return (unsigned long)NULL;
11637 const struct bpf_func_proto bpf_skc_to_tcp6_sock_proto = {
11638 .func = bpf_skc_to_tcp6_sock,
11640 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
11641 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
11642 .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_TCP6],
11645 BPF_CALL_1(bpf_skc_to_tcp_sock, struct sock *, sk)
11647 if (sk && sk_fullsock(sk) && sk->sk_protocol == IPPROTO_TCP)
11648 return (unsigned long)sk;
11650 return (unsigned long)NULL;
11653 const struct bpf_func_proto bpf_skc_to_tcp_sock_proto = {
11654 .func = bpf_skc_to_tcp_sock,
11656 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
11657 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
11658 .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_TCP],
11661 BPF_CALL_1(bpf_skc_to_tcp_timewait_sock, struct sock *, sk)
11663 /* BTF types for tcp_timewait_sock and inet_timewait_sock are not
11664 * generated if CONFIG_INET=n. Trigger an explicit generation here.
11666 BTF_TYPE_EMIT(struct inet_timewait_sock);
11667 BTF_TYPE_EMIT(struct tcp_timewait_sock);
11670 if (sk && sk->sk_prot == &tcp_prot && sk->sk_state == TCP_TIME_WAIT)
11671 return (unsigned long)sk;
11674 #if IS_BUILTIN(CONFIG_IPV6)
11675 if (sk && sk->sk_prot == &tcpv6_prot && sk->sk_state == TCP_TIME_WAIT)
11676 return (unsigned long)sk;
11679 return (unsigned long)NULL;
11682 const struct bpf_func_proto bpf_skc_to_tcp_timewait_sock_proto = {
11683 .func = bpf_skc_to_tcp_timewait_sock,
11685 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
11686 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
11687 .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_TCP_TW],
11690 BPF_CALL_1(bpf_skc_to_tcp_request_sock, struct sock *, sk)
11693 if (sk && sk->sk_prot == &tcp_prot && sk->sk_state == TCP_NEW_SYN_RECV)
11694 return (unsigned long)sk;
11697 #if IS_BUILTIN(CONFIG_IPV6)
11698 if (sk && sk->sk_prot == &tcpv6_prot && sk->sk_state == TCP_NEW_SYN_RECV)
11699 return (unsigned long)sk;
11702 return (unsigned long)NULL;
11705 const struct bpf_func_proto bpf_skc_to_tcp_request_sock_proto = {
11706 .func = bpf_skc_to_tcp_request_sock,
11708 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
11709 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
11710 .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_TCP_REQ],
11713 BPF_CALL_1(bpf_skc_to_udp6_sock, struct sock *, sk)
11715 /* udp6_sock type is not generated in dwarf and hence btf,
11716 * trigger an explicit type generation here.
11718 BTF_TYPE_EMIT(struct udp6_sock);
11719 if (sk && sk_fullsock(sk) && sk->sk_protocol == IPPROTO_UDP &&
11720 sk->sk_type == SOCK_DGRAM && sk->sk_family == AF_INET6)
11721 return (unsigned long)sk;
11723 return (unsigned long)NULL;
11726 const struct bpf_func_proto bpf_skc_to_udp6_sock_proto = {
11727 .func = bpf_skc_to_udp6_sock,
11729 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
11730 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
11731 .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_UDP6],
11734 BPF_CALL_1(bpf_skc_to_unix_sock, struct sock *, sk)
11736 /* unix_sock type is not generated in dwarf and hence btf,
11737 * trigger an explicit type generation here.
11739 BTF_TYPE_EMIT(struct unix_sock);
11740 if (sk && sk_fullsock(sk) && sk->sk_family == AF_UNIX)
11741 return (unsigned long)sk;
11743 return (unsigned long)NULL;
11746 const struct bpf_func_proto bpf_skc_to_unix_sock_proto = {
11747 .func = bpf_skc_to_unix_sock,
11749 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
11750 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
11751 .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_UNIX],
11754 BPF_CALL_1(bpf_skc_to_mptcp_sock, struct sock *, sk)
11756 BTF_TYPE_EMIT(struct mptcp_sock);
11757 return (unsigned long)bpf_mptcp_sock_from_subflow(sk);
11760 const struct bpf_func_proto bpf_skc_to_mptcp_sock_proto = {
11761 .func = bpf_skc_to_mptcp_sock,
11763 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
11764 .arg1_type = ARG_PTR_TO_SOCK_COMMON,
11765 .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_MPTCP],
11768 BPF_CALL_1(bpf_sock_from_file, struct file *, file)
11770 return (unsigned long)sock_from_file(file);
11773 BTF_ID_LIST(bpf_sock_from_file_btf_ids)
11774 BTF_ID(struct, socket)
11775 BTF_ID(struct, file)
11777 const struct bpf_func_proto bpf_sock_from_file_proto = {
11778 .func = bpf_sock_from_file,
11780 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
11781 .ret_btf_id = &bpf_sock_from_file_btf_ids[0],
11782 .arg1_type = ARG_PTR_TO_BTF_ID,
11783 .arg1_btf_id = &bpf_sock_from_file_btf_ids[1],
11786 static const struct bpf_func_proto *
11787 bpf_sk_base_func_proto(enum bpf_func_id func_id)
11789 const struct bpf_func_proto *func;
11792 case BPF_FUNC_skc_to_tcp6_sock:
11793 func = &bpf_skc_to_tcp6_sock_proto;
11795 case BPF_FUNC_skc_to_tcp_sock:
11796 func = &bpf_skc_to_tcp_sock_proto;
11798 case BPF_FUNC_skc_to_tcp_timewait_sock:
11799 func = &bpf_skc_to_tcp_timewait_sock_proto;
11801 case BPF_FUNC_skc_to_tcp_request_sock:
11802 func = &bpf_skc_to_tcp_request_sock_proto;
11804 case BPF_FUNC_skc_to_udp6_sock:
11805 func = &bpf_skc_to_udp6_sock_proto;
11807 case BPF_FUNC_skc_to_unix_sock:
11808 func = &bpf_skc_to_unix_sock_proto;
11810 case BPF_FUNC_skc_to_mptcp_sock:
11811 func = &bpf_skc_to_mptcp_sock_proto;
11813 case BPF_FUNC_ktime_get_coarse_ns:
11814 return &bpf_ktime_get_coarse_ns_proto;
11816 return bpf_base_func_proto(func_id);
11819 if (!perfmon_capable())
11825 __bpf_kfunc_start_defs();
11826 __bpf_kfunc int bpf_dynptr_from_skb(struct sk_buff *skb, u64 flags,
11827 struct bpf_dynptr_kern *ptr__uninit)
11830 bpf_dynptr_set_null(ptr__uninit);
11834 bpf_dynptr_init(ptr__uninit, skb, BPF_DYNPTR_TYPE_SKB, 0, skb->len);
11839 __bpf_kfunc int bpf_dynptr_from_xdp(struct xdp_buff *xdp, u64 flags,
11840 struct bpf_dynptr_kern *ptr__uninit)
11843 bpf_dynptr_set_null(ptr__uninit);
11847 bpf_dynptr_init(ptr__uninit, xdp, BPF_DYNPTR_TYPE_XDP, 0, xdp_get_buff_len(xdp));
11852 __bpf_kfunc int bpf_sock_addr_set_sun_path(struct bpf_sock_addr_kern *sa_kern,
11853 const u8 *sun_path, u32 sun_path__sz)
11855 struct sockaddr_un *un;
11857 if (sa_kern->sk->sk_family != AF_UNIX)
11860 /* We do not allow changing the address to unnamed or larger than the
11861 * maximum allowed address size for a unix sockaddr.
11863 if (sun_path__sz == 0 || sun_path__sz > UNIX_PATH_MAX)
11866 un = (struct sockaddr_un *)sa_kern->uaddr;
11867 memcpy(un->sun_path, sun_path, sun_path__sz);
11868 sa_kern->uaddrlen = offsetof(struct sockaddr_un, sun_path) + sun_path__sz;
11872 __bpf_kfunc_end_defs();
11874 int bpf_dynptr_from_skb_rdonly(struct sk_buff *skb, u64 flags,
11875 struct bpf_dynptr_kern *ptr__uninit)
11879 err = bpf_dynptr_from_skb(skb, flags, ptr__uninit);
11883 bpf_dynptr_set_rdonly(ptr__uninit);
11888 BTF_SET8_START(bpf_kfunc_check_set_skb)
11889 BTF_ID_FLAGS(func, bpf_dynptr_from_skb)
11890 BTF_SET8_END(bpf_kfunc_check_set_skb)
11892 BTF_SET8_START(bpf_kfunc_check_set_xdp)
11893 BTF_ID_FLAGS(func, bpf_dynptr_from_xdp)
11894 BTF_SET8_END(bpf_kfunc_check_set_xdp)
11896 BTF_SET8_START(bpf_kfunc_check_set_sock_addr)
11897 BTF_ID_FLAGS(func, bpf_sock_addr_set_sun_path)
11898 BTF_SET8_END(bpf_kfunc_check_set_sock_addr)
11900 static const struct btf_kfunc_id_set bpf_kfunc_set_skb = {
11901 .owner = THIS_MODULE,
11902 .set = &bpf_kfunc_check_set_skb,
11905 static const struct btf_kfunc_id_set bpf_kfunc_set_xdp = {
11906 .owner = THIS_MODULE,
11907 .set = &bpf_kfunc_check_set_xdp,
11910 static const struct btf_kfunc_id_set bpf_kfunc_set_sock_addr = {
11911 .owner = THIS_MODULE,
11912 .set = &bpf_kfunc_check_set_sock_addr,
11915 static int __init bpf_kfunc_init(void)
11919 ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_CLS, &bpf_kfunc_set_skb);
11920 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_ACT, &bpf_kfunc_set_skb);
11921 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SK_SKB, &bpf_kfunc_set_skb);
11922 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_SOCKET_FILTER, &bpf_kfunc_set_skb);
11923 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_CGROUP_SKB, &bpf_kfunc_set_skb);
11924 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_LWT_OUT, &bpf_kfunc_set_skb);
11925 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_LWT_IN, &bpf_kfunc_set_skb);
11926 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_LWT_XMIT, &bpf_kfunc_set_skb);
11927 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_LWT_SEG6LOCAL, &bpf_kfunc_set_skb);
11928 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_NETFILTER, &bpf_kfunc_set_skb);
11929 ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_XDP, &bpf_kfunc_set_xdp);
11930 return ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
11931 &bpf_kfunc_set_sock_addr);
11933 late_initcall(bpf_kfunc_init);
11935 __bpf_kfunc_start_defs();
11937 /* bpf_sock_destroy: Destroy the given socket with ECONNABORTED error code.
11939 * The function expects a non-NULL pointer to a socket, and invokes the
11940 * protocol specific socket destroy handlers.
11942 * The helper can only be called from BPF contexts that have acquired the socket
11946 * @sock: Pointer to socket to be destroyed
11949 * On error, may return EPROTONOSUPPORT, EINVAL.
11950 * EPROTONOSUPPORT if protocol specific destroy handler is not supported.
11953 __bpf_kfunc int bpf_sock_destroy(struct sock_common *sock)
11955 struct sock *sk = (struct sock *)sock;
11957 /* The locking semantics that allow for synchronous execution of the
11958 * destroy handlers are only supported for TCP and UDP.
11959 * Supporting protocols will need to acquire sock lock in the BPF context
11960 * prior to invoking this kfunc.
11962 if (!sk->sk_prot->diag_destroy || (sk->sk_protocol != IPPROTO_TCP &&
11963 sk->sk_protocol != IPPROTO_UDP))
11964 return -EOPNOTSUPP;
11966 return sk->sk_prot->diag_destroy(sk, ECONNABORTED);
11969 __bpf_kfunc_end_defs();
11971 BTF_SET8_START(bpf_sk_iter_kfunc_ids)
11972 BTF_ID_FLAGS(func, bpf_sock_destroy, KF_TRUSTED_ARGS)
11973 BTF_SET8_END(bpf_sk_iter_kfunc_ids)
11975 static int tracing_iter_filter(const struct bpf_prog *prog, u32 kfunc_id)
11977 if (btf_id_set8_contains(&bpf_sk_iter_kfunc_ids, kfunc_id) &&
11978 prog->expected_attach_type != BPF_TRACE_ITER)
11983 static const struct btf_kfunc_id_set bpf_sk_iter_kfunc_set = {
11984 .owner = THIS_MODULE,
11985 .set = &bpf_sk_iter_kfunc_ids,
11986 .filter = tracing_iter_filter,
11989 static int init_subsystem(void)
11991 return register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, &bpf_sk_iter_kfunc_set);
11993 late_initcall(init_subsystem);