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/module.h>
21 #include <linux/types.h>
23 #include <linux/fcntl.h>
24 #include <linux/socket.h>
25 #include <linux/sock_diag.h>
27 #include <linux/inet.h>
28 #include <linux/netdevice.h>
29 #include <linux/if_packet.h>
30 #include <linux/if_arp.h>
31 #include <linux/gfp.h>
32 #include <net/inet_common.h>
34 #include <net/protocol.h>
35 #include <net/netlink.h>
36 #include <linux/skbuff.h>
37 #include <linux/skmsg.h>
39 #include <net/flow_dissector.h>
40 #include <linux/errno.h>
41 #include <linux/timer.h>
42 #include <linux/uaccess.h>
43 #include <asm/unaligned.h>
44 #include <asm/cmpxchg.h>
45 #include <linux/filter.h>
46 #include <linux/ratelimit.h>
47 #include <linux/seccomp.h>
48 #include <linux/if_vlan.h>
49 #include <linux/bpf.h>
50 #include <linux/btf.h>
51 #include <net/sch_generic.h>
52 #include <net/cls_cgroup.h>
53 #include <net/dst_metadata.h>
55 #include <net/sock_reuseport.h>
56 #include <net/busy_poll.h>
60 #include <linux/bpf_trace.h>
61 #include <net/xdp_sock.h>
62 #include <linux/inetdevice.h>
63 #include <net/inet_hashtables.h>
64 #include <net/inet6_hashtables.h>
65 #include <net/ip_fib.h>
66 #include <net/nexthop.h>
70 #include <net/net_namespace.h>
71 #include <linux/seg6_local.h>
73 #include <net/seg6_local.h>
74 #include <net/lwtunnel.h>
75 #include <net/ipv6_stubs.h>
76 #include <net/bpf_sk_storage.h>
77 #include <net/transp_v6.h>
78 #include <linux/btf_ids.h>
80 int copy_bpf_fprog_from_user(struct sock_fprog *dst, void __user *src, int len)
82 if (in_compat_syscall()) {
83 struct compat_sock_fprog f32;
85 if (len != sizeof(f32))
87 if (copy_from_user(&f32, src, sizeof(f32)))
89 memset(dst, 0, sizeof(*dst));
91 dst->filter = compat_ptr(f32.filter);
93 if (len != sizeof(*dst))
95 if (copy_from_user(dst, src, sizeof(*dst)))
101 EXPORT_SYMBOL_GPL(copy_bpf_fprog_from_user);
104 * sk_filter_trim_cap - run a packet through a socket filter
105 * @sk: sock associated with &sk_buff
106 * @skb: buffer to filter
107 * @cap: limit on how short the eBPF program may trim the packet
109 * Run the eBPF program and then cut skb->data to correct size returned by
110 * the program. If pkt_len is 0 we toss packet. If skb->len is smaller
111 * than pkt_len we keep whole skb->data. This is the socket level
112 * wrapper to BPF_PROG_RUN. It returns 0 if the packet should
113 * be accepted or -EPERM if the packet should be tossed.
116 int sk_filter_trim_cap(struct sock *sk, struct sk_buff *skb, unsigned int cap)
119 struct sk_filter *filter;
122 * If the skb was allocated from pfmemalloc reserves, only
123 * allow SOCK_MEMALLOC sockets to use it as this socket is
124 * helping free memory
126 if (skb_pfmemalloc(skb) && !sock_flag(sk, SOCK_MEMALLOC)) {
127 NET_INC_STATS(sock_net(sk), LINUX_MIB_PFMEMALLOCDROP);
130 err = BPF_CGROUP_RUN_PROG_INET_INGRESS(sk, skb);
134 err = security_sock_rcv_skb(sk, skb);
139 filter = rcu_dereference(sk->sk_filter);
141 struct sock *save_sk = skb->sk;
142 unsigned int pkt_len;
145 pkt_len = bpf_prog_run_save_cb(filter->prog, skb);
147 err = pkt_len ? pskb_trim(skb, max(cap, pkt_len)) : -EPERM;
153 EXPORT_SYMBOL(sk_filter_trim_cap);
155 BPF_CALL_1(bpf_skb_get_pay_offset, struct sk_buff *, skb)
157 return skb_get_poff(skb);
160 BPF_CALL_3(bpf_skb_get_nlattr, struct sk_buff *, skb, u32, a, u32, x)
164 if (skb_is_nonlinear(skb))
167 if (skb->len < sizeof(struct nlattr))
170 if (a > skb->len - sizeof(struct nlattr))
173 nla = nla_find((struct nlattr *) &skb->data[a], skb->len - a, x);
175 return (void *) nla - (void *) skb->data;
180 BPF_CALL_3(bpf_skb_get_nlattr_nest, struct sk_buff *, skb, u32, a, u32, x)
184 if (skb_is_nonlinear(skb))
187 if (skb->len < sizeof(struct nlattr))
190 if (a > skb->len - sizeof(struct nlattr))
193 nla = (struct nlattr *) &skb->data[a];
194 if (nla->nla_len > skb->len - a)
197 nla = nla_find_nested(nla, x);
199 return (void *) nla - (void *) skb->data;
204 BPF_CALL_4(bpf_skb_load_helper_8, const struct sk_buff *, skb, const void *,
205 data, int, headlen, int, offset)
208 const int len = sizeof(tmp);
211 if (headlen - offset >= len)
212 return *(u8 *)(data + offset);
213 if (!skb_copy_bits(skb, offset, &tmp, sizeof(tmp)))
216 ptr = bpf_internal_load_pointer_neg_helper(skb, offset, len);
224 BPF_CALL_2(bpf_skb_load_helper_8_no_cache, const struct sk_buff *, skb,
227 return ____bpf_skb_load_helper_8(skb, skb->data, skb->len - skb->data_len,
231 BPF_CALL_4(bpf_skb_load_helper_16, const struct sk_buff *, skb, const void *,
232 data, int, headlen, int, offset)
235 const int len = sizeof(tmp);
238 if (headlen - offset >= len)
239 return get_unaligned_be16(data + offset);
240 if (!skb_copy_bits(skb, offset, &tmp, sizeof(tmp)))
241 return be16_to_cpu(tmp);
243 ptr = bpf_internal_load_pointer_neg_helper(skb, offset, len);
245 return get_unaligned_be16(ptr);
251 BPF_CALL_2(bpf_skb_load_helper_16_no_cache, const struct sk_buff *, skb,
254 return ____bpf_skb_load_helper_16(skb, skb->data, skb->len - skb->data_len,
258 BPF_CALL_4(bpf_skb_load_helper_32, const struct sk_buff *, skb, const void *,
259 data, int, headlen, int, offset)
262 const int len = sizeof(tmp);
264 if (likely(offset >= 0)) {
265 if (headlen - offset >= len)
266 return get_unaligned_be32(data + offset);
267 if (!skb_copy_bits(skb, offset, &tmp, sizeof(tmp)))
268 return be32_to_cpu(tmp);
270 ptr = bpf_internal_load_pointer_neg_helper(skb, offset, len);
272 return get_unaligned_be32(ptr);
278 BPF_CALL_2(bpf_skb_load_helper_32_no_cache, const struct sk_buff *, skb,
281 return ____bpf_skb_load_helper_32(skb, skb->data, skb->len - skb->data_len,
285 static u32 convert_skb_access(int skb_field, int dst_reg, int src_reg,
286 struct bpf_insn *insn_buf)
288 struct bpf_insn *insn = insn_buf;
292 BUILD_BUG_ON(sizeof_field(struct sk_buff, mark) != 4);
294 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
295 offsetof(struct sk_buff, mark));
299 *insn++ = BPF_LDX_MEM(BPF_B, dst_reg, src_reg, PKT_TYPE_OFFSET());
300 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, PKT_TYPE_MAX);
301 #ifdef __BIG_ENDIAN_BITFIELD
302 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 5);
307 BUILD_BUG_ON(sizeof_field(struct sk_buff, queue_mapping) != 2);
309 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
310 offsetof(struct sk_buff, queue_mapping));
313 case SKF_AD_VLAN_TAG:
314 BUILD_BUG_ON(sizeof_field(struct sk_buff, vlan_tci) != 2);
316 /* dst_reg = *(u16 *) (src_reg + offsetof(vlan_tci)) */
317 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
318 offsetof(struct sk_buff, vlan_tci));
320 case SKF_AD_VLAN_TAG_PRESENT:
321 *insn++ = BPF_LDX_MEM(BPF_B, dst_reg, src_reg, PKT_VLAN_PRESENT_OFFSET());
322 if (PKT_VLAN_PRESENT_BIT)
323 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, PKT_VLAN_PRESENT_BIT);
324 if (PKT_VLAN_PRESENT_BIT < 7)
325 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, 1);
329 return insn - insn_buf;
332 static bool convert_bpf_extensions(struct sock_filter *fp,
333 struct bpf_insn **insnp)
335 struct bpf_insn *insn = *insnp;
339 case SKF_AD_OFF + SKF_AD_PROTOCOL:
340 BUILD_BUG_ON(sizeof_field(struct sk_buff, protocol) != 2);
342 /* A = *(u16 *) (CTX + offsetof(protocol)) */
343 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
344 offsetof(struct sk_buff, protocol));
345 /* A = ntohs(A) [emitting a nop or swap16] */
346 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
349 case SKF_AD_OFF + SKF_AD_PKTTYPE:
350 cnt = convert_skb_access(SKF_AD_PKTTYPE, BPF_REG_A, BPF_REG_CTX, insn);
354 case SKF_AD_OFF + SKF_AD_IFINDEX:
355 case SKF_AD_OFF + SKF_AD_HATYPE:
356 BUILD_BUG_ON(sizeof_field(struct net_device, ifindex) != 4);
357 BUILD_BUG_ON(sizeof_field(struct net_device, type) != 2);
359 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
360 BPF_REG_TMP, BPF_REG_CTX,
361 offsetof(struct sk_buff, dev));
362 /* if (tmp != 0) goto pc + 1 */
363 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_TMP, 0, 1);
364 *insn++ = BPF_EXIT_INSN();
365 if (fp->k == SKF_AD_OFF + SKF_AD_IFINDEX)
366 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_TMP,
367 offsetof(struct net_device, ifindex));
369 *insn = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_TMP,
370 offsetof(struct net_device, type));
373 case SKF_AD_OFF + SKF_AD_MARK:
374 cnt = convert_skb_access(SKF_AD_MARK, BPF_REG_A, BPF_REG_CTX, insn);
378 case SKF_AD_OFF + SKF_AD_RXHASH:
379 BUILD_BUG_ON(sizeof_field(struct sk_buff, hash) != 4);
381 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX,
382 offsetof(struct sk_buff, hash));
385 case SKF_AD_OFF + SKF_AD_QUEUE:
386 cnt = convert_skb_access(SKF_AD_QUEUE, BPF_REG_A, BPF_REG_CTX, insn);
390 case SKF_AD_OFF + SKF_AD_VLAN_TAG:
391 cnt = convert_skb_access(SKF_AD_VLAN_TAG,
392 BPF_REG_A, BPF_REG_CTX, insn);
396 case SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT:
397 cnt = convert_skb_access(SKF_AD_VLAN_TAG_PRESENT,
398 BPF_REG_A, BPF_REG_CTX, insn);
402 case SKF_AD_OFF + SKF_AD_VLAN_TPID:
403 BUILD_BUG_ON(sizeof_field(struct sk_buff, vlan_proto) != 2);
405 /* A = *(u16 *) (CTX + offsetof(vlan_proto)) */
406 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
407 offsetof(struct sk_buff, vlan_proto));
408 /* A = ntohs(A) [emitting a nop or swap16] */
409 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
412 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
413 case SKF_AD_OFF + SKF_AD_NLATTR:
414 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
415 case SKF_AD_OFF + SKF_AD_CPU:
416 case SKF_AD_OFF + SKF_AD_RANDOM:
418 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX);
420 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_A);
422 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_X);
423 /* Emit call(arg1=CTX, arg2=A, arg3=X) */
425 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
426 *insn = BPF_EMIT_CALL(bpf_skb_get_pay_offset);
428 case SKF_AD_OFF + SKF_AD_NLATTR:
429 *insn = BPF_EMIT_CALL(bpf_skb_get_nlattr);
431 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
432 *insn = BPF_EMIT_CALL(bpf_skb_get_nlattr_nest);
434 case SKF_AD_OFF + SKF_AD_CPU:
435 *insn = BPF_EMIT_CALL(bpf_get_raw_cpu_id);
437 case SKF_AD_OFF + SKF_AD_RANDOM:
438 *insn = BPF_EMIT_CALL(bpf_user_rnd_u32);
439 bpf_user_rnd_init_once();
444 case SKF_AD_OFF + SKF_AD_ALU_XOR_X:
446 *insn = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_X);
450 /* This is just a dummy call to avoid letting the compiler
451 * evict __bpf_call_base() as an optimization. Placed here
452 * where no-one bothers.
454 BUG_ON(__bpf_call_base(0, 0, 0, 0, 0) != 0);
462 static bool convert_bpf_ld_abs(struct sock_filter *fp, struct bpf_insn **insnp)
464 const bool unaligned_ok = IS_BUILTIN(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS);
465 int size = bpf_size_to_bytes(BPF_SIZE(fp->code));
466 bool endian = BPF_SIZE(fp->code) == BPF_H ||
467 BPF_SIZE(fp->code) == BPF_W;
468 bool indirect = BPF_MODE(fp->code) == BPF_IND;
469 const int ip_align = NET_IP_ALIGN;
470 struct bpf_insn *insn = *insnp;
474 ((unaligned_ok && offset >= 0) ||
475 (!unaligned_ok && offset >= 0 &&
476 offset + ip_align >= 0 &&
477 offset + ip_align % size == 0))) {
478 bool ldx_off_ok = offset <= S16_MAX;
480 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_H);
482 *insn++ = BPF_ALU64_IMM(BPF_SUB, BPF_REG_TMP, offset);
483 *insn++ = BPF_JMP_IMM(BPF_JSLT, BPF_REG_TMP,
484 size, 2 + endian + (!ldx_off_ok * 2));
486 *insn++ = BPF_LDX_MEM(BPF_SIZE(fp->code), BPF_REG_A,
489 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_D);
490 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_TMP, offset);
491 *insn++ = BPF_LDX_MEM(BPF_SIZE(fp->code), BPF_REG_A,
495 *insn++ = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, size * 8);
496 *insn++ = BPF_JMP_A(8);
499 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX);
500 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_D);
501 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_H);
503 *insn++ = BPF_MOV64_IMM(BPF_REG_ARG4, offset);
505 *insn++ = BPF_MOV64_REG(BPF_REG_ARG4, BPF_REG_X);
507 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_ARG4, offset);
510 switch (BPF_SIZE(fp->code)) {
512 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_8);
515 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_16);
518 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_32);
524 *insn++ = BPF_JMP_IMM(BPF_JSGE, BPF_REG_A, 0, 2);
525 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
526 *insn = BPF_EXIT_INSN();
533 * bpf_convert_filter - convert filter program
534 * @prog: the user passed filter program
535 * @len: the length of the user passed filter program
536 * @new_prog: allocated 'struct bpf_prog' or NULL
537 * @new_len: pointer to store length of converted program
538 * @seen_ld_abs: bool whether we've seen ld_abs/ind
540 * Remap 'sock_filter' style classic BPF (cBPF) instruction set to 'bpf_insn'
541 * style extended BPF (eBPF).
542 * Conversion workflow:
544 * 1) First pass for calculating the new program length:
545 * bpf_convert_filter(old_prog, old_len, NULL, &new_len, &seen_ld_abs)
547 * 2) 2nd pass to remap in two passes: 1st pass finds new
548 * jump offsets, 2nd pass remapping:
549 * bpf_convert_filter(old_prog, old_len, new_prog, &new_len, &seen_ld_abs)
551 static int bpf_convert_filter(struct sock_filter *prog, int len,
552 struct bpf_prog *new_prog, int *new_len,
555 int new_flen = 0, pass = 0, target, i, stack_off;
556 struct bpf_insn *new_insn, *first_insn = NULL;
557 struct sock_filter *fp;
561 BUILD_BUG_ON(BPF_MEMWORDS * sizeof(u32) > MAX_BPF_STACK);
562 BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG);
564 if (len <= 0 || len > BPF_MAXINSNS)
568 first_insn = new_prog->insnsi;
569 addrs = kcalloc(len, sizeof(*addrs),
570 GFP_KERNEL | __GFP_NOWARN);
576 new_insn = first_insn;
579 /* Classic BPF related prologue emission. */
581 /* Classic BPF expects A and X to be reset first. These need
582 * to be guaranteed to be the first two instructions.
584 *new_insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
585 *new_insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_X, BPF_REG_X);
587 /* All programs must keep CTX in callee saved BPF_REG_CTX.
588 * In eBPF case it's done by the compiler, here we need to
589 * do this ourself. Initial CTX is present in BPF_REG_ARG1.
591 *new_insn++ = BPF_MOV64_REG(BPF_REG_CTX, BPF_REG_ARG1);
593 /* For packet access in classic BPF, cache skb->data
594 * in callee-saved BPF R8 and skb->len - skb->data_len
595 * (headlen) in BPF R9. Since classic BPF is read-only
596 * on CTX, we only need to cache it once.
598 *new_insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
599 BPF_REG_D, BPF_REG_CTX,
600 offsetof(struct sk_buff, data));
601 *new_insn++ = BPF_LDX_MEM(BPF_W, BPF_REG_H, BPF_REG_CTX,
602 offsetof(struct sk_buff, len));
603 *new_insn++ = BPF_LDX_MEM(BPF_W, BPF_REG_TMP, BPF_REG_CTX,
604 offsetof(struct sk_buff, data_len));
605 *new_insn++ = BPF_ALU32_REG(BPF_SUB, BPF_REG_H, BPF_REG_TMP);
611 for (i = 0; i < len; fp++, i++) {
612 struct bpf_insn tmp_insns[32] = { };
613 struct bpf_insn *insn = tmp_insns;
616 addrs[i] = new_insn - first_insn;
619 /* All arithmetic insns and skb loads map as-is. */
620 case BPF_ALU | BPF_ADD | BPF_X:
621 case BPF_ALU | BPF_ADD | BPF_K:
622 case BPF_ALU | BPF_SUB | BPF_X:
623 case BPF_ALU | BPF_SUB | BPF_K:
624 case BPF_ALU | BPF_AND | BPF_X:
625 case BPF_ALU | BPF_AND | BPF_K:
626 case BPF_ALU | BPF_OR | BPF_X:
627 case BPF_ALU | BPF_OR | BPF_K:
628 case BPF_ALU | BPF_LSH | BPF_X:
629 case BPF_ALU | BPF_LSH | BPF_K:
630 case BPF_ALU | BPF_RSH | BPF_X:
631 case BPF_ALU | BPF_RSH | BPF_K:
632 case BPF_ALU | BPF_XOR | BPF_X:
633 case BPF_ALU | BPF_XOR | BPF_K:
634 case BPF_ALU | BPF_MUL | BPF_X:
635 case BPF_ALU | BPF_MUL | BPF_K:
636 case BPF_ALU | BPF_DIV | BPF_X:
637 case BPF_ALU | BPF_DIV | BPF_K:
638 case BPF_ALU | BPF_MOD | BPF_X:
639 case BPF_ALU | BPF_MOD | BPF_K:
640 case BPF_ALU | BPF_NEG:
641 case BPF_LD | BPF_ABS | BPF_W:
642 case BPF_LD | BPF_ABS | BPF_H:
643 case BPF_LD | BPF_ABS | BPF_B:
644 case BPF_LD | BPF_IND | BPF_W:
645 case BPF_LD | BPF_IND | BPF_H:
646 case BPF_LD | BPF_IND | BPF_B:
647 /* Check for overloaded BPF extension and
648 * directly convert it if found, otherwise
649 * just move on with mapping.
651 if (BPF_CLASS(fp->code) == BPF_LD &&
652 BPF_MODE(fp->code) == BPF_ABS &&
653 convert_bpf_extensions(fp, &insn))
655 if (BPF_CLASS(fp->code) == BPF_LD &&
656 convert_bpf_ld_abs(fp, &insn)) {
661 if (fp->code == (BPF_ALU | BPF_DIV | BPF_X) ||
662 fp->code == (BPF_ALU | BPF_MOD | BPF_X)) {
663 *insn++ = BPF_MOV32_REG(BPF_REG_X, BPF_REG_X);
664 /* Error with exception code on div/mod by 0.
665 * For cBPF programs, this was always return 0.
667 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_X, 0, 2);
668 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
669 *insn++ = BPF_EXIT_INSN();
672 *insn = BPF_RAW_INSN(fp->code, BPF_REG_A, BPF_REG_X, 0, fp->k);
675 /* Jump transformation cannot use BPF block macros
676 * everywhere as offset calculation and target updates
677 * require a bit more work than the rest, i.e. jump
678 * opcodes map as-is, but offsets need adjustment.
681 #define BPF_EMIT_JMP \
683 const s32 off_min = S16_MIN, off_max = S16_MAX; \
686 if (target >= len || target < 0) \
688 off = addrs ? addrs[target] - addrs[i] - 1 : 0; \
689 /* Adjust pc relative offset for 2nd or 3rd insn. */ \
690 off -= insn - tmp_insns; \
691 /* Reject anything not fitting into insn->off. */ \
692 if (off < off_min || off > off_max) \
697 case BPF_JMP | BPF_JA:
698 target = i + fp->k + 1;
699 insn->code = fp->code;
703 case BPF_JMP | BPF_JEQ | BPF_K:
704 case BPF_JMP | BPF_JEQ | BPF_X:
705 case BPF_JMP | BPF_JSET | BPF_K:
706 case BPF_JMP | BPF_JSET | BPF_X:
707 case BPF_JMP | BPF_JGT | BPF_K:
708 case BPF_JMP | BPF_JGT | BPF_X:
709 case BPF_JMP | BPF_JGE | BPF_K:
710 case BPF_JMP | BPF_JGE | BPF_X:
711 if (BPF_SRC(fp->code) == BPF_K && (int) fp->k < 0) {
712 /* BPF immediates are signed, zero extend
713 * immediate into tmp register and use it
716 *insn++ = BPF_MOV32_IMM(BPF_REG_TMP, fp->k);
718 insn->dst_reg = BPF_REG_A;
719 insn->src_reg = BPF_REG_TMP;
722 insn->dst_reg = BPF_REG_A;
724 bpf_src = BPF_SRC(fp->code);
725 insn->src_reg = bpf_src == BPF_X ? BPF_REG_X : 0;
728 /* Common case where 'jump_false' is next insn. */
730 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
731 target = i + fp->jt + 1;
736 /* Convert some jumps when 'jump_true' is next insn. */
738 switch (BPF_OP(fp->code)) {
740 insn->code = BPF_JMP | BPF_JNE | bpf_src;
743 insn->code = BPF_JMP | BPF_JLE | bpf_src;
746 insn->code = BPF_JMP | BPF_JLT | bpf_src;
752 target = i + fp->jf + 1;
757 /* Other jumps are mapped into two insns: Jxx and JA. */
758 target = i + fp->jt + 1;
759 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
763 insn->code = BPF_JMP | BPF_JA;
764 target = i + fp->jf + 1;
768 /* ldxb 4 * ([14] & 0xf) is remaped into 6 insns. */
769 case BPF_LDX | BPF_MSH | BPF_B: {
770 struct sock_filter tmp = {
771 .code = BPF_LD | BPF_ABS | BPF_B,
778 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
779 /* A = BPF_R0 = *(u8 *) (skb->data + K) */
780 convert_bpf_ld_abs(&tmp, &insn);
783 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, 0xf);
785 *insn++ = BPF_ALU32_IMM(BPF_LSH, BPF_REG_A, 2);
787 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_X);
789 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
791 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_TMP);
794 /* RET_K is remaped into 2 insns. RET_A case doesn't need an
795 * extra mov as BPF_REG_0 is already mapped into BPF_REG_A.
797 case BPF_RET | BPF_A:
798 case BPF_RET | BPF_K:
799 if (BPF_RVAL(fp->code) == BPF_K)
800 *insn++ = BPF_MOV32_RAW(BPF_K, BPF_REG_0,
802 *insn = BPF_EXIT_INSN();
805 /* Store to stack. */
808 stack_off = fp->k * 4 + 4;
809 *insn = BPF_STX_MEM(BPF_W, BPF_REG_FP, BPF_CLASS(fp->code) ==
810 BPF_ST ? BPF_REG_A : BPF_REG_X,
812 /* check_load_and_stores() verifies that classic BPF can
813 * load from stack only after write, so tracking
814 * stack_depth for ST|STX insns is enough
816 if (new_prog && new_prog->aux->stack_depth < stack_off)
817 new_prog->aux->stack_depth = stack_off;
820 /* Load from stack. */
821 case BPF_LD | BPF_MEM:
822 case BPF_LDX | BPF_MEM:
823 stack_off = fp->k * 4 + 4;
824 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
825 BPF_REG_A : BPF_REG_X, BPF_REG_FP,
830 case BPF_LD | BPF_IMM:
831 case BPF_LDX | BPF_IMM:
832 *insn = BPF_MOV32_IMM(BPF_CLASS(fp->code) == BPF_LD ?
833 BPF_REG_A : BPF_REG_X, fp->k);
837 case BPF_MISC | BPF_TAX:
838 *insn = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
842 case BPF_MISC | BPF_TXA:
843 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_X);
846 /* A = skb->len or X = skb->len */
847 case BPF_LD | BPF_W | BPF_LEN:
848 case BPF_LDX | BPF_W | BPF_LEN:
849 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
850 BPF_REG_A : BPF_REG_X, BPF_REG_CTX,
851 offsetof(struct sk_buff, len));
854 /* Access seccomp_data fields. */
855 case BPF_LDX | BPF_ABS | BPF_W:
856 /* A = *(u32 *) (ctx + K) */
857 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX, fp->k);
860 /* Unknown instruction. */
867 memcpy(new_insn, tmp_insns,
868 sizeof(*insn) * (insn - tmp_insns));
869 new_insn += insn - tmp_insns;
873 /* Only calculating new length. */
874 *new_len = new_insn - first_insn;
876 *new_len += 4; /* Prologue bits. */
881 if (new_flen != new_insn - first_insn) {
882 new_flen = new_insn - first_insn;
889 BUG_ON(*new_len != new_flen);
898 * As we dont want to clear mem[] array for each packet going through
899 * __bpf_prog_run(), we check that filter loaded by user never try to read
900 * a cell if not previously written, and we check all branches to be sure
901 * a malicious user doesn't try to abuse us.
903 static int check_load_and_stores(const struct sock_filter *filter, int flen)
905 u16 *masks, memvalid = 0; /* One bit per cell, 16 cells */
908 BUILD_BUG_ON(BPF_MEMWORDS > 16);
910 masks = kmalloc_array(flen, sizeof(*masks), GFP_KERNEL);
914 memset(masks, 0xff, flen * sizeof(*masks));
916 for (pc = 0; pc < flen; pc++) {
917 memvalid &= masks[pc];
919 switch (filter[pc].code) {
922 memvalid |= (1 << filter[pc].k);
924 case BPF_LD | BPF_MEM:
925 case BPF_LDX | BPF_MEM:
926 if (!(memvalid & (1 << filter[pc].k))) {
931 case BPF_JMP | BPF_JA:
932 /* A jump must set masks on target */
933 masks[pc + 1 + filter[pc].k] &= memvalid;
936 case BPF_JMP | BPF_JEQ | BPF_K:
937 case BPF_JMP | BPF_JEQ | BPF_X:
938 case BPF_JMP | BPF_JGE | BPF_K:
939 case BPF_JMP | BPF_JGE | BPF_X:
940 case BPF_JMP | BPF_JGT | BPF_K:
941 case BPF_JMP | BPF_JGT | BPF_X:
942 case BPF_JMP | BPF_JSET | BPF_K:
943 case BPF_JMP | BPF_JSET | BPF_X:
944 /* A jump must set masks on targets */
945 masks[pc + 1 + filter[pc].jt] &= memvalid;
946 masks[pc + 1 + filter[pc].jf] &= memvalid;
956 static bool chk_code_allowed(u16 code_to_probe)
958 static const bool codes[] = {
959 /* 32 bit ALU operations */
960 [BPF_ALU | BPF_ADD | BPF_K] = true,
961 [BPF_ALU | BPF_ADD | BPF_X] = true,
962 [BPF_ALU | BPF_SUB | BPF_K] = true,
963 [BPF_ALU | BPF_SUB | BPF_X] = true,
964 [BPF_ALU | BPF_MUL | BPF_K] = true,
965 [BPF_ALU | BPF_MUL | BPF_X] = true,
966 [BPF_ALU | BPF_DIV | BPF_K] = true,
967 [BPF_ALU | BPF_DIV | BPF_X] = true,
968 [BPF_ALU | BPF_MOD | BPF_K] = true,
969 [BPF_ALU | BPF_MOD | BPF_X] = true,
970 [BPF_ALU | BPF_AND | BPF_K] = true,
971 [BPF_ALU | BPF_AND | BPF_X] = true,
972 [BPF_ALU | BPF_OR | BPF_K] = true,
973 [BPF_ALU | BPF_OR | BPF_X] = true,
974 [BPF_ALU | BPF_XOR | BPF_K] = true,
975 [BPF_ALU | BPF_XOR | BPF_X] = true,
976 [BPF_ALU | BPF_LSH | BPF_K] = true,
977 [BPF_ALU | BPF_LSH | BPF_X] = true,
978 [BPF_ALU | BPF_RSH | BPF_K] = true,
979 [BPF_ALU | BPF_RSH | BPF_X] = true,
980 [BPF_ALU | BPF_NEG] = true,
981 /* Load instructions */
982 [BPF_LD | BPF_W | BPF_ABS] = true,
983 [BPF_LD | BPF_H | BPF_ABS] = true,
984 [BPF_LD | BPF_B | BPF_ABS] = true,
985 [BPF_LD | BPF_W | BPF_LEN] = true,
986 [BPF_LD | BPF_W | BPF_IND] = true,
987 [BPF_LD | BPF_H | BPF_IND] = true,
988 [BPF_LD | BPF_B | BPF_IND] = true,
989 [BPF_LD | BPF_IMM] = true,
990 [BPF_LD | BPF_MEM] = true,
991 [BPF_LDX | BPF_W | BPF_LEN] = true,
992 [BPF_LDX | BPF_B | BPF_MSH] = true,
993 [BPF_LDX | BPF_IMM] = true,
994 [BPF_LDX | BPF_MEM] = true,
995 /* Store instructions */
998 /* Misc instructions */
999 [BPF_MISC | BPF_TAX] = true,
1000 [BPF_MISC | BPF_TXA] = true,
1001 /* Return instructions */
1002 [BPF_RET | BPF_K] = true,
1003 [BPF_RET | BPF_A] = true,
1004 /* Jump instructions */
1005 [BPF_JMP | BPF_JA] = true,
1006 [BPF_JMP | BPF_JEQ | BPF_K] = true,
1007 [BPF_JMP | BPF_JEQ | BPF_X] = true,
1008 [BPF_JMP | BPF_JGE | BPF_K] = true,
1009 [BPF_JMP | BPF_JGE | BPF_X] = true,
1010 [BPF_JMP | BPF_JGT | BPF_K] = true,
1011 [BPF_JMP | BPF_JGT | BPF_X] = true,
1012 [BPF_JMP | BPF_JSET | BPF_K] = true,
1013 [BPF_JMP | BPF_JSET | BPF_X] = true,
1016 if (code_to_probe >= ARRAY_SIZE(codes))
1019 return codes[code_to_probe];
1022 static bool bpf_check_basics_ok(const struct sock_filter *filter,
1027 if (flen == 0 || flen > BPF_MAXINSNS)
1034 * bpf_check_classic - verify socket filter code
1035 * @filter: filter to verify
1036 * @flen: length of filter
1038 * Check the user's filter code. If we let some ugly
1039 * filter code slip through kaboom! The filter must contain
1040 * no references or jumps that are out of range, no illegal
1041 * instructions, and must end with a RET instruction.
1043 * All jumps are forward as they are not signed.
1045 * Returns 0 if the rule set is legal or -EINVAL if not.
1047 static int bpf_check_classic(const struct sock_filter *filter,
1053 /* Check the filter code now */
1054 for (pc = 0; pc < flen; pc++) {
1055 const struct sock_filter *ftest = &filter[pc];
1057 /* May we actually operate on this code? */
1058 if (!chk_code_allowed(ftest->code))
1061 /* Some instructions need special checks */
1062 switch (ftest->code) {
1063 case BPF_ALU | BPF_DIV | BPF_K:
1064 case BPF_ALU | BPF_MOD | BPF_K:
1065 /* Check for division by zero */
1069 case BPF_ALU | BPF_LSH | BPF_K:
1070 case BPF_ALU | BPF_RSH | BPF_K:
1074 case BPF_LD | BPF_MEM:
1075 case BPF_LDX | BPF_MEM:
1078 /* Check for invalid memory addresses */
1079 if (ftest->k >= BPF_MEMWORDS)
1082 case BPF_JMP | BPF_JA:
1083 /* Note, the large ftest->k might cause loops.
1084 * Compare this with conditional jumps below,
1085 * where offsets are limited. --ANK (981016)
1087 if (ftest->k >= (unsigned int)(flen - pc - 1))
1090 case BPF_JMP | BPF_JEQ | BPF_K:
1091 case BPF_JMP | BPF_JEQ | BPF_X:
1092 case BPF_JMP | BPF_JGE | BPF_K:
1093 case BPF_JMP | BPF_JGE | BPF_X:
1094 case BPF_JMP | BPF_JGT | BPF_K:
1095 case BPF_JMP | BPF_JGT | BPF_X:
1096 case BPF_JMP | BPF_JSET | BPF_K:
1097 case BPF_JMP | BPF_JSET | BPF_X:
1098 /* Both conditionals must be safe */
1099 if (pc + ftest->jt + 1 >= flen ||
1100 pc + ftest->jf + 1 >= flen)
1103 case BPF_LD | BPF_W | BPF_ABS:
1104 case BPF_LD | BPF_H | BPF_ABS:
1105 case BPF_LD | BPF_B | BPF_ABS:
1107 if (bpf_anc_helper(ftest) & BPF_ANC)
1109 /* Ancillary operation unknown or unsupported */
1110 if (anc_found == false && ftest->k >= SKF_AD_OFF)
1115 /* Last instruction must be a RET code */
1116 switch (filter[flen - 1].code) {
1117 case BPF_RET | BPF_K:
1118 case BPF_RET | BPF_A:
1119 return check_load_and_stores(filter, flen);
1125 static int bpf_prog_store_orig_filter(struct bpf_prog *fp,
1126 const struct sock_fprog *fprog)
1128 unsigned int fsize = bpf_classic_proglen(fprog);
1129 struct sock_fprog_kern *fkprog;
1131 fp->orig_prog = kmalloc(sizeof(*fkprog), GFP_KERNEL);
1135 fkprog = fp->orig_prog;
1136 fkprog->len = fprog->len;
1138 fkprog->filter = kmemdup(fp->insns, fsize,
1139 GFP_KERNEL | __GFP_NOWARN);
1140 if (!fkprog->filter) {
1141 kfree(fp->orig_prog);
1148 static void bpf_release_orig_filter(struct bpf_prog *fp)
1150 struct sock_fprog_kern *fprog = fp->orig_prog;
1153 kfree(fprog->filter);
1158 static void __bpf_prog_release(struct bpf_prog *prog)
1160 if (prog->type == BPF_PROG_TYPE_SOCKET_FILTER) {
1163 bpf_release_orig_filter(prog);
1164 bpf_prog_free(prog);
1168 static void __sk_filter_release(struct sk_filter *fp)
1170 __bpf_prog_release(fp->prog);
1175 * sk_filter_release_rcu - Release a socket filter by rcu_head
1176 * @rcu: rcu_head that contains the sk_filter to free
1178 static void sk_filter_release_rcu(struct rcu_head *rcu)
1180 struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu);
1182 __sk_filter_release(fp);
1186 * sk_filter_release - release a socket filter
1187 * @fp: filter to remove
1189 * Remove a filter from a socket and release its resources.
1191 static void sk_filter_release(struct sk_filter *fp)
1193 if (refcount_dec_and_test(&fp->refcnt))
1194 call_rcu(&fp->rcu, sk_filter_release_rcu);
1197 void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp)
1199 u32 filter_size = bpf_prog_size(fp->prog->len);
1201 atomic_sub(filter_size, &sk->sk_omem_alloc);
1202 sk_filter_release(fp);
1205 /* try to charge the socket memory if there is space available
1206 * return true on success
1208 static bool __sk_filter_charge(struct sock *sk, struct sk_filter *fp)
1210 u32 filter_size = bpf_prog_size(fp->prog->len);
1212 /* same check as in sock_kmalloc() */
1213 if (filter_size <= sysctl_optmem_max &&
1214 atomic_read(&sk->sk_omem_alloc) + filter_size < sysctl_optmem_max) {
1215 atomic_add(filter_size, &sk->sk_omem_alloc);
1221 bool sk_filter_charge(struct sock *sk, struct sk_filter *fp)
1223 if (!refcount_inc_not_zero(&fp->refcnt))
1226 if (!__sk_filter_charge(sk, fp)) {
1227 sk_filter_release(fp);
1233 static struct bpf_prog *bpf_migrate_filter(struct bpf_prog *fp)
1235 struct sock_filter *old_prog;
1236 struct bpf_prog *old_fp;
1237 int err, new_len, old_len = fp->len;
1238 bool seen_ld_abs = false;
1240 /* We are free to overwrite insns et al right here as it
1241 * won't be used at this point in time anymore internally
1242 * after the migration to the internal BPF instruction
1245 BUILD_BUG_ON(sizeof(struct sock_filter) !=
1246 sizeof(struct bpf_insn));
1248 /* Conversion cannot happen on overlapping memory areas,
1249 * so we need to keep the user BPF around until the 2nd
1250 * pass. At this time, the user BPF is stored in fp->insns.
1252 old_prog = kmemdup(fp->insns, old_len * sizeof(struct sock_filter),
1253 GFP_KERNEL | __GFP_NOWARN);
1259 /* 1st pass: calculate the new program length. */
1260 err = bpf_convert_filter(old_prog, old_len, NULL, &new_len,
1265 /* Expand fp for appending the new filter representation. */
1267 fp = bpf_prog_realloc(old_fp, bpf_prog_size(new_len), 0);
1269 /* The old_fp is still around in case we couldn't
1270 * allocate new memory, so uncharge on that one.
1279 /* 2nd pass: remap sock_filter insns into bpf_insn insns. */
1280 err = bpf_convert_filter(old_prog, old_len, fp, &new_len,
1283 /* 2nd bpf_convert_filter() can fail only if it fails
1284 * to allocate memory, remapping must succeed. Note,
1285 * that at this time old_fp has already been released
1290 fp = bpf_prog_select_runtime(fp, &err);
1300 __bpf_prog_release(fp);
1301 return ERR_PTR(err);
1304 static struct bpf_prog *bpf_prepare_filter(struct bpf_prog *fp,
1305 bpf_aux_classic_check_t trans)
1309 fp->bpf_func = NULL;
1312 err = bpf_check_classic(fp->insns, fp->len);
1314 __bpf_prog_release(fp);
1315 return ERR_PTR(err);
1318 /* There might be additional checks and transformations
1319 * needed on classic filters, f.e. in case of seccomp.
1322 err = trans(fp->insns, fp->len);
1324 __bpf_prog_release(fp);
1325 return ERR_PTR(err);
1329 /* Probe if we can JIT compile the filter and if so, do
1330 * the compilation of the filter.
1332 bpf_jit_compile(fp);
1334 /* JIT compiler couldn't process this filter, so do the
1335 * internal BPF translation for the optimized interpreter.
1338 fp = bpf_migrate_filter(fp);
1344 * bpf_prog_create - create an unattached filter
1345 * @pfp: the unattached filter that is created
1346 * @fprog: the filter program
1348 * Create a filter independent of any socket. We first run some
1349 * sanity checks on it to make sure it does not explode on us later.
1350 * If an error occurs or there is insufficient memory for the filter
1351 * a negative errno code is returned. On success the return is zero.
1353 int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog)
1355 unsigned int fsize = bpf_classic_proglen(fprog);
1356 struct bpf_prog *fp;
1358 /* Make sure new filter is there and in the right amounts. */
1359 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1362 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1366 memcpy(fp->insns, fprog->filter, fsize);
1368 fp->len = fprog->len;
1369 /* Since unattached filters are not copied back to user
1370 * space through sk_get_filter(), we do not need to hold
1371 * a copy here, and can spare us the work.
1373 fp->orig_prog = NULL;
1375 /* bpf_prepare_filter() already takes care of freeing
1376 * memory in case something goes wrong.
1378 fp = bpf_prepare_filter(fp, NULL);
1385 EXPORT_SYMBOL_GPL(bpf_prog_create);
1388 * bpf_prog_create_from_user - create an unattached filter from user buffer
1389 * @pfp: the unattached filter that is created
1390 * @fprog: the filter program
1391 * @trans: post-classic verifier transformation handler
1392 * @save_orig: save classic BPF program
1394 * This function effectively does the same as bpf_prog_create(), only
1395 * that it builds up its insns buffer from user space provided buffer.
1396 * It also allows for passing a bpf_aux_classic_check_t handler.
1398 int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog,
1399 bpf_aux_classic_check_t trans, bool save_orig)
1401 unsigned int fsize = bpf_classic_proglen(fprog);
1402 struct bpf_prog *fp;
1405 /* Make sure new filter is there and in the right amounts. */
1406 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1409 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1413 if (copy_from_user(fp->insns, fprog->filter, fsize)) {
1414 __bpf_prog_free(fp);
1418 fp->len = fprog->len;
1419 fp->orig_prog = NULL;
1422 err = bpf_prog_store_orig_filter(fp, fprog);
1424 __bpf_prog_free(fp);
1429 /* bpf_prepare_filter() already takes care of freeing
1430 * memory in case something goes wrong.
1432 fp = bpf_prepare_filter(fp, trans);
1439 EXPORT_SYMBOL_GPL(bpf_prog_create_from_user);
1441 void bpf_prog_destroy(struct bpf_prog *fp)
1443 __bpf_prog_release(fp);
1445 EXPORT_SYMBOL_GPL(bpf_prog_destroy);
1447 static int __sk_attach_prog(struct bpf_prog *prog, struct sock *sk)
1449 struct sk_filter *fp, *old_fp;
1451 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
1457 if (!__sk_filter_charge(sk, fp)) {
1461 refcount_set(&fp->refcnt, 1);
1463 old_fp = rcu_dereference_protected(sk->sk_filter,
1464 lockdep_sock_is_held(sk));
1465 rcu_assign_pointer(sk->sk_filter, fp);
1468 sk_filter_uncharge(sk, old_fp);
1474 struct bpf_prog *__get_filter(struct sock_fprog *fprog, struct sock *sk)
1476 unsigned int fsize = bpf_classic_proglen(fprog);
1477 struct bpf_prog *prog;
1480 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1481 return ERR_PTR(-EPERM);
1483 /* Make sure new filter is there and in the right amounts. */
1484 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1485 return ERR_PTR(-EINVAL);
1487 prog = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1489 return ERR_PTR(-ENOMEM);
1491 if (copy_from_user(prog->insns, fprog->filter, fsize)) {
1492 __bpf_prog_free(prog);
1493 return ERR_PTR(-EFAULT);
1496 prog->len = fprog->len;
1498 err = bpf_prog_store_orig_filter(prog, fprog);
1500 __bpf_prog_free(prog);
1501 return ERR_PTR(-ENOMEM);
1504 /* bpf_prepare_filter() already takes care of freeing
1505 * memory in case something goes wrong.
1507 return bpf_prepare_filter(prog, NULL);
1511 * sk_attach_filter - attach a socket filter
1512 * @fprog: the filter program
1513 * @sk: the socket to use
1515 * Attach the user's filter code. We first run some sanity checks on
1516 * it to make sure it does not explode on us later. If an error
1517 * occurs or there is insufficient memory for the filter a negative
1518 * errno code is returned. On success the return is zero.
1520 int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1522 struct bpf_prog *prog = __get_filter(fprog, sk);
1526 return PTR_ERR(prog);
1528 err = __sk_attach_prog(prog, sk);
1530 __bpf_prog_release(prog);
1536 EXPORT_SYMBOL_GPL(sk_attach_filter);
1538 int sk_reuseport_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1540 struct bpf_prog *prog = __get_filter(fprog, sk);
1544 return PTR_ERR(prog);
1546 if (bpf_prog_size(prog->len) > sysctl_optmem_max)
1549 err = reuseport_attach_prog(sk, prog);
1552 __bpf_prog_release(prog);
1557 static struct bpf_prog *__get_bpf(u32 ufd, struct sock *sk)
1559 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1560 return ERR_PTR(-EPERM);
1562 return bpf_prog_get_type(ufd, BPF_PROG_TYPE_SOCKET_FILTER);
1565 int sk_attach_bpf(u32 ufd, struct sock *sk)
1567 struct bpf_prog *prog = __get_bpf(ufd, sk);
1571 return PTR_ERR(prog);
1573 err = __sk_attach_prog(prog, sk);
1582 int sk_reuseport_attach_bpf(u32 ufd, struct sock *sk)
1584 struct bpf_prog *prog;
1587 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1590 prog = bpf_prog_get_type(ufd, BPF_PROG_TYPE_SOCKET_FILTER);
1591 if (PTR_ERR(prog) == -EINVAL)
1592 prog = bpf_prog_get_type(ufd, BPF_PROG_TYPE_SK_REUSEPORT);
1594 return PTR_ERR(prog);
1596 if (prog->type == BPF_PROG_TYPE_SK_REUSEPORT) {
1597 /* Like other non BPF_PROG_TYPE_SOCKET_FILTER
1598 * bpf prog (e.g. sockmap). It depends on the
1599 * limitation imposed by bpf_prog_load().
1600 * Hence, sysctl_optmem_max is not checked.
1602 if ((sk->sk_type != SOCK_STREAM &&
1603 sk->sk_type != SOCK_DGRAM) ||
1604 (sk->sk_protocol != IPPROTO_UDP &&
1605 sk->sk_protocol != IPPROTO_TCP) ||
1606 (sk->sk_family != AF_INET &&
1607 sk->sk_family != AF_INET6)) {
1612 /* BPF_PROG_TYPE_SOCKET_FILTER */
1613 if (bpf_prog_size(prog->len) > sysctl_optmem_max) {
1619 err = reuseport_attach_prog(sk, prog);
1627 void sk_reuseport_prog_free(struct bpf_prog *prog)
1632 if (prog->type == BPF_PROG_TYPE_SK_REUSEPORT)
1635 bpf_prog_destroy(prog);
1638 struct bpf_scratchpad {
1640 __be32 diff[MAX_BPF_STACK / sizeof(__be32)];
1641 u8 buff[MAX_BPF_STACK];
1645 static DEFINE_PER_CPU(struct bpf_scratchpad, bpf_sp);
1647 static inline int __bpf_try_make_writable(struct sk_buff *skb,
1648 unsigned int write_len)
1650 return skb_ensure_writable(skb, write_len);
1653 static inline int bpf_try_make_writable(struct sk_buff *skb,
1654 unsigned int write_len)
1656 int err = __bpf_try_make_writable(skb, write_len);
1658 bpf_compute_data_pointers(skb);
1662 static int bpf_try_make_head_writable(struct sk_buff *skb)
1664 return bpf_try_make_writable(skb, skb_headlen(skb));
1667 static inline void bpf_push_mac_rcsum(struct sk_buff *skb)
1669 if (skb_at_tc_ingress(skb))
1670 skb_postpush_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1673 static inline void bpf_pull_mac_rcsum(struct sk_buff *skb)
1675 if (skb_at_tc_ingress(skb))
1676 skb_postpull_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1679 BPF_CALL_5(bpf_skb_store_bytes, struct sk_buff *, skb, u32, offset,
1680 const void *, from, u32, len, u64, flags)
1684 if (unlikely(flags & ~(BPF_F_RECOMPUTE_CSUM | BPF_F_INVALIDATE_HASH)))
1686 if (unlikely(offset > 0xffff))
1688 if (unlikely(bpf_try_make_writable(skb, offset + len)))
1691 ptr = skb->data + offset;
1692 if (flags & BPF_F_RECOMPUTE_CSUM)
1693 __skb_postpull_rcsum(skb, ptr, len, offset);
1695 memcpy(ptr, from, len);
1697 if (flags & BPF_F_RECOMPUTE_CSUM)
1698 __skb_postpush_rcsum(skb, ptr, len, offset);
1699 if (flags & BPF_F_INVALIDATE_HASH)
1700 skb_clear_hash(skb);
1705 static const struct bpf_func_proto bpf_skb_store_bytes_proto = {
1706 .func = bpf_skb_store_bytes,
1708 .ret_type = RET_INTEGER,
1709 .arg1_type = ARG_PTR_TO_CTX,
1710 .arg2_type = ARG_ANYTHING,
1711 .arg3_type = ARG_PTR_TO_MEM,
1712 .arg4_type = ARG_CONST_SIZE,
1713 .arg5_type = ARG_ANYTHING,
1716 BPF_CALL_4(bpf_skb_load_bytes, const struct sk_buff *, skb, u32, offset,
1717 void *, to, u32, len)
1721 if (unlikely(offset > 0xffff))
1724 ptr = skb_header_pointer(skb, offset, len, to);
1728 memcpy(to, ptr, len);
1736 static const struct bpf_func_proto bpf_skb_load_bytes_proto = {
1737 .func = bpf_skb_load_bytes,
1739 .ret_type = RET_INTEGER,
1740 .arg1_type = ARG_PTR_TO_CTX,
1741 .arg2_type = ARG_ANYTHING,
1742 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
1743 .arg4_type = ARG_CONST_SIZE,
1746 BPF_CALL_4(bpf_flow_dissector_load_bytes,
1747 const struct bpf_flow_dissector *, ctx, u32, offset,
1748 void *, to, u32, len)
1752 if (unlikely(offset > 0xffff))
1755 if (unlikely(!ctx->skb))
1758 ptr = skb_header_pointer(ctx->skb, offset, len, to);
1762 memcpy(to, ptr, len);
1770 static const struct bpf_func_proto bpf_flow_dissector_load_bytes_proto = {
1771 .func = bpf_flow_dissector_load_bytes,
1773 .ret_type = RET_INTEGER,
1774 .arg1_type = ARG_PTR_TO_CTX,
1775 .arg2_type = ARG_ANYTHING,
1776 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
1777 .arg4_type = ARG_CONST_SIZE,
1780 BPF_CALL_5(bpf_skb_load_bytes_relative, const struct sk_buff *, skb,
1781 u32, offset, void *, to, u32, len, u32, start_header)
1783 u8 *end = skb_tail_pointer(skb);
1786 if (unlikely(offset > 0xffff))
1789 switch (start_header) {
1790 case BPF_HDR_START_MAC:
1791 if (unlikely(!skb_mac_header_was_set(skb)))
1793 start = skb_mac_header(skb);
1795 case BPF_HDR_START_NET:
1796 start = skb_network_header(skb);
1802 ptr = start + offset;
1804 if (likely(ptr + len <= end)) {
1805 memcpy(to, ptr, len);
1814 static const struct bpf_func_proto bpf_skb_load_bytes_relative_proto = {
1815 .func = bpf_skb_load_bytes_relative,
1817 .ret_type = RET_INTEGER,
1818 .arg1_type = ARG_PTR_TO_CTX,
1819 .arg2_type = ARG_ANYTHING,
1820 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
1821 .arg4_type = ARG_CONST_SIZE,
1822 .arg5_type = ARG_ANYTHING,
1825 BPF_CALL_2(bpf_skb_pull_data, struct sk_buff *, skb, u32, len)
1827 /* Idea is the following: should the needed direct read/write
1828 * test fail during runtime, we can pull in more data and redo
1829 * again, since implicitly, we invalidate previous checks here.
1831 * Or, since we know how much we need to make read/writeable,
1832 * this can be done once at the program beginning for direct
1833 * access case. By this we overcome limitations of only current
1834 * headroom being accessible.
1836 return bpf_try_make_writable(skb, len ? : skb_headlen(skb));
1839 static const struct bpf_func_proto bpf_skb_pull_data_proto = {
1840 .func = bpf_skb_pull_data,
1842 .ret_type = RET_INTEGER,
1843 .arg1_type = ARG_PTR_TO_CTX,
1844 .arg2_type = ARG_ANYTHING,
1847 BPF_CALL_1(bpf_sk_fullsock, struct sock *, sk)
1849 return sk_fullsock(sk) ? (unsigned long)sk : (unsigned long)NULL;
1852 static const struct bpf_func_proto bpf_sk_fullsock_proto = {
1853 .func = bpf_sk_fullsock,
1855 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
1856 .arg1_type = ARG_PTR_TO_SOCK_COMMON,
1859 static inline int sk_skb_try_make_writable(struct sk_buff *skb,
1860 unsigned int write_len)
1862 int err = __bpf_try_make_writable(skb, write_len);
1864 bpf_compute_data_end_sk_skb(skb);
1868 BPF_CALL_2(sk_skb_pull_data, struct sk_buff *, skb, u32, len)
1870 /* Idea is the following: should the needed direct read/write
1871 * test fail during runtime, we can pull in more data and redo
1872 * again, since implicitly, we invalidate previous checks here.
1874 * Or, since we know how much we need to make read/writeable,
1875 * this can be done once at the program beginning for direct
1876 * access case. By this we overcome limitations of only current
1877 * headroom being accessible.
1879 return sk_skb_try_make_writable(skb, len ? : skb_headlen(skb));
1882 static const struct bpf_func_proto sk_skb_pull_data_proto = {
1883 .func = sk_skb_pull_data,
1885 .ret_type = RET_INTEGER,
1886 .arg1_type = ARG_PTR_TO_CTX,
1887 .arg2_type = ARG_ANYTHING,
1890 BPF_CALL_5(bpf_l3_csum_replace, struct sk_buff *, skb, u32, offset,
1891 u64, from, u64, to, u64, flags)
1895 if (unlikely(flags & ~(BPF_F_HDR_FIELD_MASK)))
1897 if (unlikely(offset > 0xffff || offset & 1))
1899 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
1902 ptr = (__sum16 *)(skb->data + offset);
1903 switch (flags & BPF_F_HDR_FIELD_MASK) {
1905 if (unlikely(from != 0))
1908 csum_replace_by_diff(ptr, to);
1911 csum_replace2(ptr, from, to);
1914 csum_replace4(ptr, from, to);
1923 static const struct bpf_func_proto bpf_l3_csum_replace_proto = {
1924 .func = bpf_l3_csum_replace,
1926 .ret_type = RET_INTEGER,
1927 .arg1_type = ARG_PTR_TO_CTX,
1928 .arg2_type = ARG_ANYTHING,
1929 .arg3_type = ARG_ANYTHING,
1930 .arg4_type = ARG_ANYTHING,
1931 .arg5_type = ARG_ANYTHING,
1934 BPF_CALL_5(bpf_l4_csum_replace, struct sk_buff *, skb, u32, offset,
1935 u64, from, u64, to, u64, flags)
1937 bool is_pseudo = flags & BPF_F_PSEUDO_HDR;
1938 bool is_mmzero = flags & BPF_F_MARK_MANGLED_0;
1939 bool do_mforce = flags & BPF_F_MARK_ENFORCE;
1942 if (unlikely(flags & ~(BPF_F_MARK_MANGLED_0 | BPF_F_MARK_ENFORCE |
1943 BPF_F_PSEUDO_HDR | BPF_F_HDR_FIELD_MASK)))
1945 if (unlikely(offset > 0xffff || offset & 1))
1947 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
1950 ptr = (__sum16 *)(skb->data + offset);
1951 if (is_mmzero && !do_mforce && !*ptr)
1954 switch (flags & BPF_F_HDR_FIELD_MASK) {
1956 if (unlikely(from != 0))
1959 inet_proto_csum_replace_by_diff(ptr, skb, to, is_pseudo);
1962 inet_proto_csum_replace2(ptr, skb, from, to, is_pseudo);
1965 inet_proto_csum_replace4(ptr, skb, from, to, is_pseudo);
1971 if (is_mmzero && !*ptr)
1972 *ptr = CSUM_MANGLED_0;
1976 static const struct bpf_func_proto bpf_l4_csum_replace_proto = {
1977 .func = bpf_l4_csum_replace,
1979 .ret_type = RET_INTEGER,
1980 .arg1_type = ARG_PTR_TO_CTX,
1981 .arg2_type = ARG_ANYTHING,
1982 .arg3_type = ARG_ANYTHING,
1983 .arg4_type = ARG_ANYTHING,
1984 .arg5_type = ARG_ANYTHING,
1987 BPF_CALL_5(bpf_csum_diff, __be32 *, from, u32, from_size,
1988 __be32 *, to, u32, to_size, __wsum, seed)
1990 struct bpf_scratchpad *sp = this_cpu_ptr(&bpf_sp);
1991 u32 diff_size = from_size + to_size;
1994 /* This is quite flexible, some examples:
1996 * from_size == 0, to_size > 0, seed := csum --> pushing data
1997 * from_size > 0, to_size == 0, seed := csum --> pulling data
1998 * from_size > 0, to_size > 0, seed := 0 --> diffing data
2000 * Even for diffing, from_size and to_size don't need to be equal.
2002 if (unlikely(((from_size | to_size) & (sizeof(__be32) - 1)) ||
2003 diff_size > sizeof(sp->diff)))
2006 for (i = 0; i < from_size / sizeof(__be32); i++, j++)
2007 sp->diff[j] = ~from[i];
2008 for (i = 0; i < to_size / sizeof(__be32); i++, j++)
2009 sp->diff[j] = to[i];
2011 return csum_partial(sp->diff, diff_size, seed);
2014 static const struct bpf_func_proto bpf_csum_diff_proto = {
2015 .func = bpf_csum_diff,
2018 .ret_type = RET_INTEGER,
2019 .arg1_type = ARG_PTR_TO_MEM_OR_NULL,
2020 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
2021 .arg3_type = ARG_PTR_TO_MEM_OR_NULL,
2022 .arg4_type = ARG_CONST_SIZE_OR_ZERO,
2023 .arg5_type = ARG_ANYTHING,
2026 BPF_CALL_2(bpf_csum_update, struct sk_buff *, skb, __wsum, csum)
2028 /* The interface is to be used in combination with bpf_csum_diff()
2029 * for direct packet writes. csum rotation for alignment as well
2030 * as emulating csum_sub() can be done from the eBPF program.
2032 if (skb->ip_summed == CHECKSUM_COMPLETE)
2033 return (skb->csum = csum_add(skb->csum, csum));
2038 static const struct bpf_func_proto bpf_csum_update_proto = {
2039 .func = bpf_csum_update,
2041 .ret_type = RET_INTEGER,
2042 .arg1_type = ARG_PTR_TO_CTX,
2043 .arg2_type = ARG_ANYTHING,
2046 BPF_CALL_2(bpf_csum_level, struct sk_buff *, skb, u64, level)
2048 /* The interface is to be used in combination with bpf_skb_adjust_room()
2049 * for encap/decap of packet headers when BPF_F_ADJ_ROOM_NO_CSUM_RESET
2050 * is passed as flags, for example.
2053 case BPF_CSUM_LEVEL_INC:
2054 __skb_incr_checksum_unnecessary(skb);
2056 case BPF_CSUM_LEVEL_DEC:
2057 __skb_decr_checksum_unnecessary(skb);
2059 case BPF_CSUM_LEVEL_RESET:
2060 __skb_reset_checksum_unnecessary(skb);
2062 case BPF_CSUM_LEVEL_QUERY:
2063 return skb->ip_summed == CHECKSUM_UNNECESSARY ?
2064 skb->csum_level : -EACCES;
2072 static const struct bpf_func_proto bpf_csum_level_proto = {
2073 .func = bpf_csum_level,
2075 .ret_type = RET_INTEGER,
2076 .arg1_type = ARG_PTR_TO_CTX,
2077 .arg2_type = ARG_ANYTHING,
2080 static inline int __bpf_rx_skb(struct net_device *dev, struct sk_buff *skb)
2082 return dev_forward_skb(dev, skb);
2085 static inline int __bpf_rx_skb_no_mac(struct net_device *dev,
2086 struct sk_buff *skb)
2088 int ret = ____dev_forward_skb(dev, skb);
2092 ret = netif_rx(skb);
2098 static inline int __bpf_tx_skb(struct net_device *dev, struct sk_buff *skb)
2102 if (dev_xmit_recursion()) {
2103 net_crit_ratelimited("bpf: recursion limit reached on datapath, buggy bpf program?\n");
2111 dev_xmit_recursion_inc();
2112 ret = dev_queue_xmit(skb);
2113 dev_xmit_recursion_dec();
2118 static int __bpf_redirect_no_mac(struct sk_buff *skb, struct net_device *dev,
2121 unsigned int mlen = skb_network_offset(skb);
2124 __skb_pull(skb, mlen);
2126 /* At ingress, the mac header has already been pulled once.
2127 * At egress, skb_pospull_rcsum has to be done in case that
2128 * the skb is originated from ingress (i.e. a forwarded skb)
2129 * to ensure that rcsum starts at net header.
2131 if (!skb_at_tc_ingress(skb))
2132 skb_postpull_rcsum(skb, skb_mac_header(skb), mlen);
2134 skb_pop_mac_header(skb);
2135 skb_reset_mac_len(skb);
2136 return flags & BPF_F_INGRESS ?
2137 __bpf_rx_skb_no_mac(dev, skb) : __bpf_tx_skb(dev, skb);
2140 static int __bpf_redirect_common(struct sk_buff *skb, struct net_device *dev,
2143 /* Verify that a link layer header is carried */
2144 if (unlikely(skb->mac_header >= skb->network_header)) {
2149 bpf_push_mac_rcsum(skb);
2150 return flags & BPF_F_INGRESS ?
2151 __bpf_rx_skb(dev, skb) : __bpf_tx_skb(dev, skb);
2154 static int __bpf_redirect(struct sk_buff *skb, struct net_device *dev,
2157 if (dev_is_mac_header_xmit(dev))
2158 return __bpf_redirect_common(skb, dev, flags);
2160 return __bpf_redirect_no_mac(skb, dev, flags);
2163 BPF_CALL_3(bpf_clone_redirect, struct sk_buff *, skb, u32, ifindex, u64, flags)
2165 struct net_device *dev;
2166 struct sk_buff *clone;
2169 if (unlikely(flags & ~(BPF_F_INGRESS)))
2172 dev = dev_get_by_index_rcu(dev_net(skb->dev), ifindex);
2176 clone = skb_clone(skb, GFP_ATOMIC);
2177 if (unlikely(!clone))
2180 /* For direct write, we need to keep the invariant that the skbs
2181 * we're dealing with need to be uncloned. Should uncloning fail
2182 * here, we need to free the just generated clone to unclone once
2185 ret = bpf_try_make_head_writable(skb);
2186 if (unlikely(ret)) {
2191 return __bpf_redirect(clone, dev, flags);
2194 static const struct bpf_func_proto bpf_clone_redirect_proto = {
2195 .func = bpf_clone_redirect,
2197 .ret_type = RET_INTEGER,
2198 .arg1_type = ARG_PTR_TO_CTX,
2199 .arg2_type = ARG_ANYTHING,
2200 .arg3_type = ARG_ANYTHING,
2203 DEFINE_PER_CPU(struct bpf_redirect_info, bpf_redirect_info);
2204 EXPORT_PER_CPU_SYMBOL_GPL(bpf_redirect_info);
2206 BPF_CALL_2(bpf_redirect, u32, ifindex, u64, flags)
2208 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
2210 if (unlikely(flags & ~(BPF_F_INGRESS)))
2214 ri->tgt_index = ifindex;
2216 return TC_ACT_REDIRECT;
2219 int skb_do_redirect(struct sk_buff *skb)
2221 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
2222 struct net_device *dev;
2224 dev = dev_get_by_index_rcu(dev_net(skb->dev), ri->tgt_index);
2226 if (unlikely(!dev)) {
2231 return __bpf_redirect(skb, dev, ri->flags);
2234 static const struct bpf_func_proto bpf_redirect_proto = {
2235 .func = bpf_redirect,
2237 .ret_type = RET_INTEGER,
2238 .arg1_type = ARG_ANYTHING,
2239 .arg2_type = ARG_ANYTHING,
2242 BPF_CALL_2(bpf_msg_apply_bytes, struct sk_msg *, msg, u32, bytes)
2244 msg->apply_bytes = bytes;
2248 static const struct bpf_func_proto bpf_msg_apply_bytes_proto = {
2249 .func = bpf_msg_apply_bytes,
2251 .ret_type = RET_INTEGER,
2252 .arg1_type = ARG_PTR_TO_CTX,
2253 .arg2_type = ARG_ANYTHING,
2256 BPF_CALL_2(bpf_msg_cork_bytes, struct sk_msg *, msg, u32, bytes)
2258 msg->cork_bytes = bytes;
2262 static const struct bpf_func_proto bpf_msg_cork_bytes_proto = {
2263 .func = bpf_msg_cork_bytes,
2265 .ret_type = RET_INTEGER,
2266 .arg1_type = ARG_PTR_TO_CTX,
2267 .arg2_type = ARG_ANYTHING,
2270 BPF_CALL_4(bpf_msg_pull_data, struct sk_msg *, msg, u32, start,
2271 u32, end, u64, flags)
2273 u32 len = 0, offset = 0, copy = 0, poffset = 0, bytes = end - start;
2274 u32 first_sge, last_sge, i, shift, bytes_sg_total;
2275 struct scatterlist *sge;
2276 u8 *raw, *to, *from;
2279 if (unlikely(flags || end <= start))
2282 /* First find the starting scatterlist element */
2286 len = sk_msg_elem(msg, i)->length;
2287 if (start < offset + len)
2289 sk_msg_iter_var_next(i);
2290 } while (i != msg->sg.end);
2292 if (unlikely(start >= offset + len))
2296 /* The start may point into the sg element so we need to also
2297 * account for the headroom.
2299 bytes_sg_total = start - offset + bytes;
2300 if (!test_bit(i, &msg->sg.copy) && bytes_sg_total <= len)
2303 /* At this point we need to linearize multiple scatterlist
2304 * elements or a single shared page. Either way we need to
2305 * copy into a linear buffer exclusively owned by BPF. Then
2306 * place the buffer in the scatterlist and fixup the original
2307 * entries by removing the entries now in the linear buffer
2308 * and shifting the remaining entries. For now we do not try
2309 * to copy partial entries to avoid complexity of running out
2310 * of sg_entry slots. The downside is reading a single byte
2311 * will copy the entire sg entry.
2314 copy += sk_msg_elem(msg, i)->length;
2315 sk_msg_iter_var_next(i);
2316 if (bytes_sg_total <= copy)
2318 } while (i != msg->sg.end);
2321 if (unlikely(bytes_sg_total > copy))
2324 page = alloc_pages(__GFP_NOWARN | GFP_ATOMIC | __GFP_COMP,
2326 if (unlikely(!page))
2329 raw = page_address(page);
2332 sge = sk_msg_elem(msg, i);
2333 from = sg_virt(sge);
2337 memcpy(to, from, len);
2340 put_page(sg_page(sge));
2342 sk_msg_iter_var_next(i);
2343 } while (i != last_sge);
2345 sg_set_page(&msg->sg.data[first_sge], page, copy, 0);
2347 /* To repair sg ring we need to shift entries. If we only
2348 * had a single entry though we can just replace it and
2349 * be done. Otherwise walk the ring and shift the entries.
2351 WARN_ON_ONCE(last_sge == first_sge);
2352 shift = last_sge > first_sge ?
2353 last_sge - first_sge - 1 :
2354 NR_MSG_FRAG_IDS - first_sge + last_sge - 1;
2359 sk_msg_iter_var_next(i);
2363 if (i + shift >= NR_MSG_FRAG_IDS)
2364 move_from = i + shift - NR_MSG_FRAG_IDS;
2366 move_from = i + shift;
2367 if (move_from == msg->sg.end)
2370 msg->sg.data[i] = msg->sg.data[move_from];
2371 msg->sg.data[move_from].length = 0;
2372 msg->sg.data[move_from].page_link = 0;
2373 msg->sg.data[move_from].offset = 0;
2374 sk_msg_iter_var_next(i);
2377 msg->sg.end = msg->sg.end - shift > msg->sg.end ?
2378 msg->sg.end - shift + NR_MSG_FRAG_IDS :
2379 msg->sg.end - shift;
2381 msg->data = sg_virt(&msg->sg.data[first_sge]) + start - offset;
2382 msg->data_end = msg->data + bytes;
2386 static const struct bpf_func_proto bpf_msg_pull_data_proto = {
2387 .func = bpf_msg_pull_data,
2389 .ret_type = RET_INTEGER,
2390 .arg1_type = ARG_PTR_TO_CTX,
2391 .arg2_type = ARG_ANYTHING,
2392 .arg3_type = ARG_ANYTHING,
2393 .arg4_type = ARG_ANYTHING,
2396 BPF_CALL_4(bpf_msg_push_data, struct sk_msg *, msg, u32, start,
2397 u32, len, u64, flags)
2399 struct scatterlist sge, nsge, nnsge, rsge = {0}, *psge;
2400 u32 new, i = 0, l = 0, space, copy = 0, offset = 0;
2401 u8 *raw, *to, *from;
2404 if (unlikely(flags))
2407 /* First find the starting scatterlist element */
2411 l = sk_msg_elem(msg, i)->length;
2413 if (start < offset + l)
2415 sk_msg_iter_var_next(i);
2416 } while (i != msg->sg.end);
2418 if (start >= offset + l)
2421 space = MAX_MSG_FRAGS - sk_msg_elem_used(msg);
2423 /* If no space available will fallback to copy, we need at
2424 * least one scatterlist elem available to push data into
2425 * when start aligns to the beginning of an element or two
2426 * when it falls inside an element. We handle the start equals
2427 * offset case because its the common case for inserting a
2430 if (!space || (space == 1 && start != offset))
2431 copy = msg->sg.data[i].length;
2433 page = alloc_pages(__GFP_NOWARN | GFP_ATOMIC | __GFP_COMP,
2434 get_order(copy + len));
2435 if (unlikely(!page))
2441 raw = page_address(page);
2443 psge = sk_msg_elem(msg, i);
2444 front = start - offset;
2445 back = psge->length - front;
2446 from = sg_virt(psge);
2449 memcpy(raw, from, front);
2453 to = raw + front + len;
2455 memcpy(to, from, back);
2458 put_page(sg_page(psge));
2459 } else if (start - offset) {
2460 psge = sk_msg_elem(msg, i);
2461 rsge = sk_msg_elem_cpy(msg, i);
2463 psge->length = start - offset;
2464 rsge.length -= psge->length;
2465 rsge.offset += start;
2467 sk_msg_iter_var_next(i);
2468 sg_unmark_end(psge);
2469 sg_unmark_end(&rsge);
2470 sk_msg_iter_next(msg, end);
2473 /* Slot(s) to place newly allocated data */
2476 /* Shift one or two slots as needed */
2478 sge = sk_msg_elem_cpy(msg, i);
2480 sk_msg_iter_var_next(i);
2481 sg_unmark_end(&sge);
2482 sk_msg_iter_next(msg, end);
2484 nsge = sk_msg_elem_cpy(msg, i);
2486 sk_msg_iter_var_next(i);
2487 nnsge = sk_msg_elem_cpy(msg, i);
2490 while (i != msg->sg.end) {
2491 msg->sg.data[i] = sge;
2493 sk_msg_iter_var_next(i);
2496 nnsge = sk_msg_elem_cpy(msg, i);
2498 nsge = sk_msg_elem_cpy(msg, i);
2503 /* Place newly allocated data buffer */
2504 sk_mem_charge(msg->sk, len);
2505 msg->sg.size += len;
2506 __clear_bit(new, &msg->sg.copy);
2507 sg_set_page(&msg->sg.data[new], page, len + copy, 0);
2509 get_page(sg_page(&rsge));
2510 sk_msg_iter_var_next(new);
2511 msg->sg.data[new] = rsge;
2514 sk_msg_compute_data_pointers(msg);
2518 static const struct bpf_func_proto bpf_msg_push_data_proto = {
2519 .func = bpf_msg_push_data,
2521 .ret_type = RET_INTEGER,
2522 .arg1_type = ARG_PTR_TO_CTX,
2523 .arg2_type = ARG_ANYTHING,
2524 .arg3_type = ARG_ANYTHING,
2525 .arg4_type = ARG_ANYTHING,
2528 static void sk_msg_shift_left(struct sk_msg *msg, int i)
2534 sk_msg_iter_var_next(i);
2535 msg->sg.data[prev] = msg->sg.data[i];
2536 } while (i != msg->sg.end);
2538 sk_msg_iter_prev(msg, end);
2541 static void sk_msg_shift_right(struct sk_msg *msg, int i)
2543 struct scatterlist tmp, sge;
2545 sk_msg_iter_next(msg, end);
2546 sge = sk_msg_elem_cpy(msg, i);
2547 sk_msg_iter_var_next(i);
2548 tmp = sk_msg_elem_cpy(msg, i);
2550 while (i != msg->sg.end) {
2551 msg->sg.data[i] = sge;
2552 sk_msg_iter_var_next(i);
2554 tmp = sk_msg_elem_cpy(msg, i);
2558 BPF_CALL_4(bpf_msg_pop_data, struct sk_msg *, msg, u32, start,
2559 u32, len, u64, flags)
2561 u32 i = 0, l = 0, space, offset = 0;
2562 u64 last = start + len;
2565 if (unlikely(flags))
2568 /* First find the starting scatterlist element */
2572 l = sk_msg_elem(msg, i)->length;
2574 if (start < offset + l)
2576 sk_msg_iter_var_next(i);
2577 } while (i != msg->sg.end);
2579 /* Bounds checks: start and pop must be inside message */
2580 if (start >= offset + l || last >= msg->sg.size)
2583 space = MAX_MSG_FRAGS - sk_msg_elem_used(msg);
2586 /* --------------| offset
2587 * -| start |-------- len -------|
2589 * |----- a ----|-------- pop -------|----- b ----|
2590 * |______________________________________________| length
2593 * a: region at front of scatter element to save
2594 * b: region at back of scatter element to save when length > A + pop
2595 * pop: region to pop from element, same as input 'pop' here will be
2596 * decremented below per iteration.
2598 * Two top-level cases to handle when start != offset, first B is non
2599 * zero and second B is zero corresponding to when a pop includes more
2602 * Then if B is non-zero AND there is no space allocate space and
2603 * compact A, B regions into page. If there is space shift ring to
2604 * the rigth free'ing the next element in ring to place B, leaving
2605 * A untouched except to reduce length.
2607 if (start != offset) {
2608 struct scatterlist *nsge, *sge = sk_msg_elem(msg, i);
2610 int b = sge->length - pop - a;
2612 sk_msg_iter_var_next(i);
2614 if (pop < sge->length - a) {
2617 sk_msg_shift_right(msg, i);
2618 nsge = sk_msg_elem(msg, i);
2619 get_page(sg_page(sge));
2622 b, sge->offset + pop + a);
2624 struct page *page, *orig;
2627 page = alloc_pages(__GFP_NOWARN |
2628 __GFP_COMP | GFP_ATOMIC,
2630 if (unlikely(!page))
2634 orig = sg_page(sge);
2635 from = sg_virt(sge);
2636 to = page_address(page);
2637 memcpy(to, from, a);
2638 memcpy(to + a, from + a + pop, b);
2639 sg_set_page(sge, page, a + b, 0);
2643 } else if (pop >= sge->length - a) {
2644 pop -= (sge->length - a);
2649 /* From above the current layout _must_ be as follows,
2654 * |---- pop ---|---------------- b ------------|
2655 * |____________________________________________| length
2657 * Offset and start of the current msg elem are equal because in the
2658 * previous case we handled offset != start and either consumed the
2659 * entire element and advanced to the next element OR pop == 0.
2661 * Two cases to handle here are first pop is less than the length
2662 * leaving some remainder b above. Simply adjust the element's layout
2663 * in this case. Or pop >= length of the element so that b = 0. In this
2664 * case advance to next element decrementing pop.
2667 struct scatterlist *sge = sk_msg_elem(msg, i);
2669 if (pop < sge->length) {
2675 sk_msg_shift_left(msg, i);
2677 sk_msg_iter_var_next(i);
2680 sk_mem_uncharge(msg->sk, len - pop);
2681 msg->sg.size -= (len - pop);
2682 sk_msg_compute_data_pointers(msg);
2686 static const struct bpf_func_proto bpf_msg_pop_data_proto = {
2687 .func = bpf_msg_pop_data,
2689 .ret_type = RET_INTEGER,
2690 .arg1_type = ARG_PTR_TO_CTX,
2691 .arg2_type = ARG_ANYTHING,
2692 .arg3_type = ARG_ANYTHING,
2693 .arg4_type = ARG_ANYTHING,
2696 #ifdef CONFIG_CGROUP_NET_CLASSID
2697 BPF_CALL_0(bpf_get_cgroup_classid_curr)
2699 return __task_get_classid(current);
2702 static const struct bpf_func_proto bpf_get_cgroup_classid_curr_proto = {
2703 .func = bpf_get_cgroup_classid_curr,
2705 .ret_type = RET_INTEGER,
2709 BPF_CALL_1(bpf_get_cgroup_classid, const struct sk_buff *, skb)
2711 return task_get_classid(skb);
2714 static const struct bpf_func_proto bpf_get_cgroup_classid_proto = {
2715 .func = bpf_get_cgroup_classid,
2717 .ret_type = RET_INTEGER,
2718 .arg1_type = ARG_PTR_TO_CTX,
2721 BPF_CALL_1(bpf_get_route_realm, const struct sk_buff *, skb)
2723 return dst_tclassid(skb);
2726 static const struct bpf_func_proto bpf_get_route_realm_proto = {
2727 .func = bpf_get_route_realm,
2729 .ret_type = RET_INTEGER,
2730 .arg1_type = ARG_PTR_TO_CTX,
2733 BPF_CALL_1(bpf_get_hash_recalc, struct sk_buff *, skb)
2735 /* If skb_clear_hash() was called due to mangling, we can
2736 * trigger SW recalculation here. Later access to hash
2737 * can then use the inline skb->hash via context directly
2738 * instead of calling this helper again.
2740 return skb_get_hash(skb);
2743 static const struct bpf_func_proto bpf_get_hash_recalc_proto = {
2744 .func = bpf_get_hash_recalc,
2746 .ret_type = RET_INTEGER,
2747 .arg1_type = ARG_PTR_TO_CTX,
2750 BPF_CALL_1(bpf_set_hash_invalid, struct sk_buff *, skb)
2752 /* After all direct packet write, this can be used once for
2753 * triggering a lazy recalc on next skb_get_hash() invocation.
2755 skb_clear_hash(skb);
2759 static const struct bpf_func_proto bpf_set_hash_invalid_proto = {
2760 .func = bpf_set_hash_invalid,
2762 .ret_type = RET_INTEGER,
2763 .arg1_type = ARG_PTR_TO_CTX,
2766 BPF_CALL_2(bpf_set_hash, struct sk_buff *, skb, u32, hash)
2768 /* Set user specified hash as L4(+), so that it gets returned
2769 * on skb_get_hash() call unless BPF prog later on triggers a
2772 __skb_set_sw_hash(skb, hash, true);
2776 static const struct bpf_func_proto bpf_set_hash_proto = {
2777 .func = bpf_set_hash,
2779 .ret_type = RET_INTEGER,
2780 .arg1_type = ARG_PTR_TO_CTX,
2781 .arg2_type = ARG_ANYTHING,
2784 BPF_CALL_3(bpf_skb_vlan_push, struct sk_buff *, skb, __be16, vlan_proto,
2789 if (unlikely(vlan_proto != htons(ETH_P_8021Q) &&
2790 vlan_proto != htons(ETH_P_8021AD)))
2791 vlan_proto = htons(ETH_P_8021Q);
2793 bpf_push_mac_rcsum(skb);
2794 ret = skb_vlan_push(skb, vlan_proto, vlan_tci);
2795 bpf_pull_mac_rcsum(skb);
2797 bpf_compute_data_pointers(skb);
2801 static const struct bpf_func_proto bpf_skb_vlan_push_proto = {
2802 .func = bpf_skb_vlan_push,
2804 .ret_type = RET_INTEGER,
2805 .arg1_type = ARG_PTR_TO_CTX,
2806 .arg2_type = ARG_ANYTHING,
2807 .arg3_type = ARG_ANYTHING,
2810 BPF_CALL_1(bpf_skb_vlan_pop, struct sk_buff *, skb)
2814 bpf_push_mac_rcsum(skb);
2815 ret = skb_vlan_pop(skb);
2816 bpf_pull_mac_rcsum(skb);
2818 bpf_compute_data_pointers(skb);
2822 static const struct bpf_func_proto bpf_skb_vlan_pop_proto = {
2823 .func = bpf_skb_vlan_pop,
2825 .ret_type = RET_INTEGER,
2826 .arg1_type = ARG_PTR_TO_CTX,
2829 static int bpf_skb_generic_push(struct sk_buff *skb, u32 off, u32 len)
2831 /* Caller already did skb_cow() with len as headroom,
2832 * so no need to do it here.
2835 memmove(skb->data, skb->data + len, off);
2836 memset(skb->data + off, 0, len);
2838 /* No skb_postpush_rcsum(skb, skb->data + off, len)
2839 * needed here as it does not change the skb->csum
2840 * result for checksum complete when summing over
2846 static int bpf_skb_generic_pop(struct sk_buff *skb, u32 off, u32 len)
2848 /* skb_ensure_writable() is not needed here, as we're
2849 * already working on an uncloned skb.
2851 if (unlikely(!pskb_may_pull(skb, off + len)))
2854 skb_postpull_rcsum(skb, skb->data + off, len);
2855 memmove(skb->data + len, skb->data, off);
2856 __skb_pull(skb, len);
2861 static int bpf_skb_net_hdr_push(struct sk_buff *skb, u32 off, u32 len)
2863 bool trans_same = skb->transport_header == skb->network_header;
2866 /* There's no need for __skb_push()/__skb_pull() pair to
2867 * get to the start of the mac header as we're guaranteed
2868 * to always start from here under eBPF.
2870 ret = bpf_skb_generic_push(skb, off, len);
2872 skb->mac_header -= len;
2873 skb->network_header -= len;
2875 skb->transport_header = skb->network_header;
2881 static int bpf_skb_net_hdr_pop(struct sk_buff *skb, u32 off, u32 len)
2883 bool trans_same = skb->transport_header == skb->network_header;
2886 /* Same here, __skb_push()/__skb_pull() pair not needed. */
2887 ret = bpf_skb_generic_pop(skb, off, len);
2889 skb->mac_header += len;
2890 skb->network_header += len;
2892 skb->transport_header = skb->network_header;
2898 static int bpf_skb_proto_4_to_6(struct sk_buff *skb)
2900 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
2901 u32 off = skb_mac_header_len(skb);
2904 if (skb_is_gso(skb) && !skb_is_gso_tcp(skb))
2907 ret = skb_cow(skb, len_diff);
2908 if (unlikely(ret < 0))
2911 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
2912 if (unlikely(ret < 0))
2915 if (skb_is_gso(skb)) {
2916 struct skb_shared_info *shinfo = skb_shinfo(skb);
2918 /* SKB_GSO_TCPV4 needs to be changed into
2921 if (shinfo->gso_type & SKB_GSO_TCPV4) {
2922 shinfo->gso_type &= ~SKB_GSO_TCPV4;
2923 shinfo->gso_type |= SKB_GSO_TCPV6;
2926 /* Due to IPv6 header, MSS needs to be downgraded. */
2927 skb_decrease_gso_size(shinfo, len_diff);
2928 /* Header must be checked, and gso_segs recomputed. */
2929 shinfo->gso_type |= SKB_GSO_DODGY;
2930 shinfo->gso_segs = 0;
2933 skb->protocol = htons(ETH_P_IPV6);
2934 skb_clear_hash(skb);
2939 static int bpf_skb_proto_6_to_4(struct sk_buff *skb)
2941 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
2942 u32 off = skb_mac_header_len(skb);
2945 if (skb_is_gso(skb) && !skb_is_gso_tcp(skb))
2948 ret = skb_unclone(skb, GFP_ATOMIC);
2949 if (unlikely(ret < 0))
2952 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
2953 if (unlikely(ret < 0))
2956 if (skb_is_gso(skb)) {
2957 struct skb_shared_info *shinfo = skb_shinfo(skb);
2959 /* SKB_GSO_TCPV6 needs to be changed into
2962 if (shinfo->gso_type & SKB_GSO_TCPV6) {
2963 shinfo->gso_type &= ~SKB_GSO_TCPV6;
2964 shinfo->gso_type |= SKB_GSO_TCPV4;
2967 /* Due to IPv4 header, MSS can be upgraded. */
2968 skb_increase_gso_size(shinfo, len_diff);
2969 /* Header must be checked, and gso_segs recomputed. */
2970 shinfo->gso_type |= SKB_GSO_DODGY;
2971 shinfo->gso_segs = 0;
2974 skb->protocol = htons(ETH_P_IP);
2975 skb_clear_hash(skb);
2980 static int bpf_skb_proto_xlat(struct sk_buff *skb, __be16 to_proto)
2982 __be16 from_proto = skb->protocol;
2984 if (from_proto == htons(ETH_P_IP) &&
2985 to_proto == htons(ETH_P_IPV6))
2986 return bpf_skb_proto_4_to_6(skb);
2988 if (from_proto == htons(ETH_P_IPV6) &&
2989 to_proto == htons(ETH_P_IP))
2990 return bpf_skb_proto_6_to_4(skb);
2995 BPF_CALL_3(bpf_skb_change_proto, struct sk_buff *, skb, __be16, proto,
3000 if (unlikely(flags))
3003 /* General idea is that this helper does the basic groundwork
3004 * needed for changing the protocol, and eBPF program fills the
3005 * rest through bpf_skb_store_bytes(), bpf_lX_csum_replace()
3006 * and other helpers, rather than passing a raw buffer here.
3008 * The rationale is to keep this minimal and without a need to
3009 * deal with raw packet data. F.e. even if we would pass buffers
3010 * here, the program still needs to call the bpf_lX_csum_replace()
3011 * helpers anyway. Plus, this way we keep also separation of
3012 * concerns, since f.e. bpf_skb_store_bytes() should only take
3015 * Currently, additional options and extension header space are
3016 * not supported, but flags register is reserved so we can adapt
3017 * that. For offloads, we mark packet as dodgy, so that headers
3018 * need to be verified first.
3020 ret = bpf_skb_proto_xlat(skb, proto);
3021 bpf_compute_data_pointers(skb);
3025 static const struct bpf_func_proto bpf_skb_change_proto_proto = {
3026 .func = bpf_skb_change_proto,
3028 .ret_type = RET_INTEGER,
3029 .arg1_type = ARG_PTR_TO_CTX,
3030 .arg2_type = ARG_ANYTHING,
3031 .arg3_type = ARG_ANYTHING,
3034 BPF_CALL_2(bpf_skb_change_type, struct sk_buff *, skb, u32, pkt_type)
3036 /* We only allow a restricted subset to be changed for now. */
3037 if (unlikely(!skb_pkt_type_ok(skb->pkt_type) ||
3038 !skb_pkt_type_ok(pkt_type)))
3041 skb->pkt_type = pkt_type;
3045 static const struct bpf_func_proto bpf_skb_change_type_proto = {
3046 .func = bpf_skb_change_type,
3048 .ret_type = RET_INTEGER,
3049 .arg1_type = ARG_PTR_TO_CTX,
3050 .arg2_type = ARG_ANYTHING,
3053 static u32 bpf_skb_net_base_len(const struct sk_buff *skb)
3055 switch (skb->protocol) {
3056 case htons(ETH_P_IP):
3057 return sizeof(struct iphdr);
3058 case htons(ETH_P_IPV6):
3059 return sizeof(struct ipv6hdr);
3065 #define BPF_F_ADJ_ROOM_ENCAP_L3_MASK (BPF_F_ADJ_ROOM_ENCAP_L3_IPV4 | \
3066 BPF_F_ADJ_ROOM_ENCAP_L3_IPV6)
3068 #define BPF_F_ADJ_ROOM_MASK (BPF_F_ADJ_ROOM_FIXED_GSO | \
3069 BPF_F_ADJ_ROOM_ENCAP_L3_MASK | \
3070 BPF_F_ADJ_ROOM_ENCAP_L4_GRE | \
3071 BPF_F_ADJ_ROOM_ENCAP_L4_UDP | \
3072 BPF_F_ADJ_ROOM_ENCAP_L2( \
3073 BPF_ADJ_ROOM_ENCAP_L2_MASK))
3075 static int bpf_skb_net_grow(struct sk_buff *skb, u32 off, u32 len_diff,
3078 u8 inner_mac_len = flags >> BPF_ADJ_ROOM_ENCAP_L2_SHIFT;
3079 bool encap = flags & BPF_F_ADJ_ROOM_ENCAP_L3_MASK;
3080 u16 mac_len = 0, inner_net = 0, inner_trans = 0;
3081 unsigned int gso_type = SKB_GSO_DODGY;
3084 if (skb_is_gso(skb) && !skb_is_gso_tcp(skb)) {
3085 /* udp gso_size delineates datagrams, only allow if fixed */
3086 if (!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) ||
3087 !(flags & BPF_F_ADJ_ROOM_FIXED_GSO))
3091 ret = skb_cow_head(skb, len_diff);
3092 if (unlikely(ret < 0))
3096 if (skb->protocol != htons(ETH_P_IP) &&
3097 skb->protocol != htons(ETH_P_IPV6))
3100 if (flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV4 &&
3101 flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV6)
3104 if (flags & BPF_F_ADJ_ROOM_ENCAP_L4_GRE &&
3105 flags & BPF_F_ADJ_ROOM_ENCAP_L4_UDP)
3108 if (skb->encapsulation)
3111 mac_len = skb->network_header - skb->mac_header;
3112 inner_net = skb->network_header;
3113 if (inner_mac_len > len_diff)
3115 inner_trans = skb->transport_header;
3118 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
3119 if (unlikely(ret < 0))
3123 skb->inner_mac_header = inner_net - inner_mac_len;
3124 skb->inner_network_header = inner_net;
3125 skb->inner_transport_header = inner_trans;
3126 skb_set_inner_protocol(skb, skb->protocol);
3128 skb->encapsulation = 1;
3129 skb_set_network_header(skb, mac_len);
3131 if (flags & BPF_F_ADJ_ROOM_ENCAP_L4_UDP)
3132 gso_type |= SKB_GSO_UDP_TUNNEL;
3133 else if (flags & BPF_F_ADJ_ROOM_ENCAP_L4_GRE)
3134 gso_type |= SKB_GSO_GRE;
3135 else if (flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV6)
3136 gso_type |= SKB_GSO_IPXIP6;
3137 else if (flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV4)
3138 gso_type |= SKB_GSO_IPXIP4;
3140 if (flags & BPF_F_ADJ_ROOM_ENCAP_L4_GRE ||
3141 flags & BPF_F_ADJ_ROOM_ENCAP_L4_UDP) {
3142 int nh_len = flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV6 ?
3143 sizeof(struct ipv6hdr) :
3144 sizeof(struct iphdr);
3146 skb_set_transport_header(skb, mac_len + nh_len);
3149 /* Match skb->protocol to new outer l3 protocol */
3150 if (skb->protocol == htons(ETH_P_IP) &&
3151 flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV6)
3152 skb->protocol = htons(ETH_P_IPV6);
3153 else if (skb->protocol == htons(ETH_P_IPV6) &&
3154 flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV4)
3155 skb->protocol = htons(ETH_P_IP);
3158 if (skb_is_gso(skb)) {
3159 struct skb_shared_info *shinfo = skb_shinfo(skb);
3161 /* Due to header grow, MSS needs to be downgraded. */
3162 if (!(flags & BPF_F_ADJ_ROOM_FIXED_GSO))
3163 skb_decrease_gso_size(shinfo, len_diff);
3165 /* Header must be checked, and gso_segs recomputed. */
3166 shinfo->gso_type |= gso_type;
3167 shinfo->gso_segs = 0;
3173 static int bpf_skb_net_shrink(struct sk_buff *skb, u32 off, u32 len_diff,
3178 if (unlikely(flags & ~(BPF_F_ADJ_ROOM_FIXED_GSO |
3179 BPF_F_ADJ_ROOM_NO_CSUM_RESET)))
3182 if (skb_is_gso(skb) && !skb_is_gso_tcp(skb)) {
3183 /* udp gso_size delineates datagrams, only allow if fixed */
3184 if (!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) ||
3185 !(flags & BPF_F_ADJ_ROOM_FIXED_GSO))
3189 ret = skb_unclone(skb, GFP_ATOMIC);
3190 if (unlikely(ret < 0))
3193 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
3194 if (unlikely(ret < 0))
3197 if (skb_is_gso(skb)) {
3198 struct skb_shared_info *shinfo = skb_shinfo(skb);
3200 /* Due to header shrink, MSS can be upgraded. */
3201 if (!(flags & BPF_F_ADJ_ROOM_FIXED_GSO))
3202 skb_increase_gso_size(shinfo, len_diff);
3204 /* Header must be checked, and gso_segs recomputed. */
3205 shinfo->gso_type |= SKB_GSO_DODGY;
3206 shinfo->gso_segs = 0;
3212 static u32 __bpf_skb_max_len(const struct sk_buff *skb)
3214 return skb->dev ? skb->dev->mtu + skb->dev->hard_header_len :
3218 BPF_CALL_4(bpf_skb_adjust_room, struct sk_buff *, skb, s32, len_diff,
3219 u32, mode, u64, flags)
3221 u32 len_cur, len_diff_abs = abs(len_diff);
3222 u32 len_min = bpf_skb_net_base_len(skb);
3223 u32 len_max = __bpf_skb_max_len(skb);
3224 __be16 proto = skb->protocol;
3225 bool shrink = len_diff < 0;
3229 if (unlikely(flags & ~(BPF_F_ADJ_ROOM_MASK |
3230 BPF_F_ADJ_ROOM_NO_CSUM_RESET)))
3232 if (unlikely(len_diff_abs > 0xfffU))
3234 if (unlikely(proto != htons(ETH_P_IP) &&
3235 proto != htons(ETH_P_IPV6)))
3238 off = skb_mac_header_len(skb);
3240 case BPF_ADJ_ROOM_NET:
3241 off += bpf_skb_net_base_len(skb);
3243 case BPF_ADJ_ROOM_MAC:
3249 len_cur = skb->len - skb_network_offset(skb);
3250 if ((shrink && (len_diff_abs >= len_cur ||
3251 len_cur - len_diff_abs < len_min)) ||
3252 (!shrink && (skb->len + len_diff_abs > len_max &&
3256 ret = shrink ? bpf_skb_net_shrink(skb, off, len_diff_abs, flags) :
3257 bpf_skb_net_grow(skb, off, len_diff_abs, flags);
3258 if (!ret && !(flags & BPF_F_ADJ_ROOM_NO_CSUM_RESET))
3259 __skb_reset_checksum_unnecessary(skb);
3261 bpf_compute_data_pointers(skb);
3265 static const struct bpf_func_proto bpf_skb_adjust_room_proto = {
3266 .func = bpf_skb_adjust_room,
3268 .ret_type = RET_INTEGER,
3269 .arg1_type = ARG_PTR_TO_CTX,
3270 .arg2_type = ARG_ANYTHING,
3271 .arg3_type = ARG_ANYTHING,
3272 .arg4_type = ARG_ANYTHING,
3275 static u32 __bpf_skb_min_len(const struct sk_buff *skb)
3277 u32 min_len = skb_network_offset(skb);
3279 if (skb_transport_header_was_set(skb))
3280 min_len = skb_transport_offset(skb);
3281 if (skb->ip_summed == CHECKSUM_PARTIAL)
3282 min_len = skb_checksum_start_offset(skb) +
3283 skb->csum_offset + sizeof(__sum16);
3287 static int bpf_skb_grow_rcsum(struct sk_buff *skb, unsigned int new_len)
3289 unsigned int old_len = skb->len;
3292 ret = __skb_grow_rcsum(skb, new_len);
3294 memset(skb->data + old_len, 0, new_len - old_len);
3298 static int bpf_skb_trim_rcsum(struct sk_buff *skb, unsigned int new_len)
3300 return __skb_trim_rcsum(skb, new_len);
3303 static inline int __bpf_skb_change_tail(struct sk_buff *skb, u32 new_len,
3306 u32 max_len = __bpf_skb_max_len(skb);
3307 u32 min_len = __bpf_skb_min_len(skb);
3310 if (unlikely(flags || new_len > max_len || new_len < min_len))
3312 if (skb->encapsulation)
3315 /* The basic idea of this helper is that it's performing the
3316 * needed work to either grow or trim an skb, and eBPF program
3317 * rewrites the rest via helpers like bpf_skb_store_bytes(),
3318 * bpf_lX_csum_replace() and others rather than passing a raw
3319 * buffer here. This one is a slow path helper and intended
3320 * for replies with control messages.
3322 * Like in bpf_skb_change_proto(), we want to keep this rather
3323 * minimal and without protocol specifics so that we are able
3324 * to separate concerns as in bpf_skb_store_bytes() should only
3325 * be the one responsible for writing buffers.
3327 * It's really expected to be a slow path operation here for
3328 * control message replies, so we're implicitly linearizing,
3329 * uncloning and drop offloads from the skb by this.
3331 ret = __bpf_try_make_writable(skb, skb->len);
3333 if (new_len > skb->len)
3334 ret = bpf_skb_grow_rcsum(skb, new_len);
3335 else if (new_len < skb->len)
3336 ret = bpf_skb_trim_rcsum(skb, new_len);
3337 if (!ret && skb_is_gso(skb))
3343 BPF_CALL_3(bpf_skb_change_tail, struct sk_buff *, skb, u32, new_len,
3346 int ret = __bpf_skb_change_tail(skb, new_len, flags);
3348 bpf_compute_data_pointers(skb);
3352 static const struct bpf_func_proto bpf_skb_change_tail_proto = {
3353 .func = bpf_skb_change_tail,
3355 .ret_type = RET_INTEGER,
3356 .arg1_type = ARG_PTR_TO_CTX,
3357 .arg2_type = ARG_ANYTHING,
3358 .arg3_type = ARG_ANYTHING,
3361 BPF_CALL_3(sk_skb_change_tail, struct sk_buff *, skb, u32, new_len,
3364 int ret = __bpf_skb_change_tail(skb, new_len, flags);
3366 bpf_compute_data_end_sk_skb(skb);
3370 static const struct bpf_func_proto sk_skb_change_tail_proto = {
3371 .func = sk_skb_change_tail,
3373 .ret_type = RET_INTEGER,
3374 .arg1_type = ARG_PTR_TO_CTX,
3375 .arg2_type = ARG_ANYTHING,
3376 .arg3_type = ARG_ANYTHING,
3379 static inline int __bpf_skb_change_head(struct sk_buff *skb, u32 head_room,
3382 u32 max_len = __bpf_skb_max_len(skb);
3383 u32 new_len = skb->len + head_room;
3386 if (unlikely(flags || (!skb_is_gso(skb) && new_len > max_len) ||
3387 new_len < skb->len))
3390 ret = skb_cow(skb, head_room);
3392 /* Idea for this helper is that we currently only
3393 * allow to expand on mac header. This means that
3394 * skb->protocol network header, etc, stay as is.
3395 * Compared to bpf_skb_change_tail(), we're more
3396 * flexible due to not needing to linearize or
3397 * reset GSO. Intention for this helper is to be
3398 * used by an L3 skb that needs to push mac header
3399 * for redirection into L2 device.
3401 __skb_push(skb, head_room);
3402 memset(skb->data, 0, head_room);
3403 skb_reset_mac_header(skb);
3409 BPF_CALL_3(bpf_skb_change_head, struct sk_buff *, skb, u32, head_room,
3412 int ret = __bpf_skb_change_head(skb, head_room, flags);
3414 bpf_compute_data_pointers(skb);
3418 static const struct bpf_func_proto bpf_skb_change_head_proto = {
3419 .func = bpf_skb_change_head,
3421 .ret_type = RET_INTEGER,
3422 .arg1_type = ARG_PTR_TO_CTX,
3423 .arg2_type = ARG_ANYTHING,
3424 .arg3_type = ARG_ANYTHING,
3427 BPF_CALL_3(sk_skb_change_head, struct sk_buff *, skb, u32, head_room,
3430 int ret = __bpf_skb_change_head(skb, head_room, flags);
3432 bpf_compute_data_end_sk_skb(skb);
3436 static const struct bpf_func_proto sk_skb_change_head_proto = {
3437 .func = sk_skb_change_head,
3439 .ret_type = RET_INTEGER,
3440 .arg1_type = ARG_PTR_TO_CTX,
3441 .arg2_type = ARG_ANYTHING,
3442 .arg3_type = ARG_ANYTHING,
3444 static unsigned long xdp_get_metalen(const struct xdp_buff *xdp)
3446 return xdp_data_meta_unsupported(xdp) ? 0 :
3447 xdp->data - xdp->data_meta;
3450 BPF_CALL_2(bpf_xdp_adjust_head, struct xdp_buff *, xdp, int, offset)
3452 void *xdp_frame_end = xdp->data_hard_start + sizeof(struct xdp_frame);
3453 unsigned long metalen = xdp_get_metalen(xdp);
3454 void *data_start = xdp_frame_end + metalen;
3455 void *data = xdp->data + offset;
3457 if (unlikely(data < data_start ||
3458 data > xdp->data_end - ETH_HLEN))
3462 memmove(xdp->data_meta + offset,
3463 xdp->data_meta, metalen);
3464 xdp->data_meta += offset;
3470 static const struct bpf_func_proto bpf_xdp_adjust_head_proto = {
3471 .func = bpf_xdp_adjust_head,
3473 .ret_type = RET_INTEGER,
3474 .arg1_type = ARG_PTR_TO_CTX,
3475 .arg2_type = ARG_ANYTHING,
3478 BPF_CALL_2(bpf_xdp_adjust_tail, struct xdp_buff *, xdp, int, offset)
3480 void *data_hard_end = xdp_data_hard_end(xdp); /* use xdp->frame_sz */
3481 void *data_end = xdp->data_end + offset;
3483 /* Notice that xdp_data_hard_end have reserved some tailroom */
3484 if (unlikely(data_end > data_hard_end))
3487 /* ALL drivers MUST init xdp->frame_sz, chicken check below */
3488 if (unlikely(xdp->frame_sz > PAGE_SIZE)) {
3489 WARN_ONCE(1, "Too BIG xdp->frame_sz = %d\n", xdp->frame_sz);
3493 if (unlikely(data_end < xdp->data + ETH_HLEN))
3496 /* Clear memory area on grow, can contain uninit kernel memory */
3498 memset(xdp->data_end, 0, offset);
3500 xdp->data_end = data_end;
3505 static const struct bpf_func_proto bpf_xdp_adjust_tail_proto = {
3506 .func = bpf_xdp_adjust_tail,
3508 .ret_type = RET_INTEGER,
3509 .arg1_type = ARG_PTR_TO_CTX,
3510 .arg2_type = ARG_ANYTHING,
3513 BPF_CALL_2(bpf_xdp_adjust_meta, struct xdp_buff *, xdp, int, offset)
3515 void *xdp_frame_end = xdp->data_hard_start + sizeof(struct xdp_frame);
3516 void *meta = xdp->data_meta + offset;
3517 unsigned long metalen = xdp->data - meta;
3519 if (xdp_data_meta_unsupported(xdp))
3521 if (unlikely(meta < xdp_frame_end ||
3524 if (unlikely((metalen & (sizeof(__u32) - 1)) ||
3528 xdp->data_meta = meta;
3533 static const struct bpf_func_proto bpf_xdp_adjust_meta_proto = {
3534 .func = bpf_xdp_adjust_meta,
3536 .ret_type = RET_INTEGER,
3537 .arg1_type = ARG_PTR_TO_CTX,
3538 .arg2_type = ARG_ANYTHING,
3541 static int __bpf_tx_xdp_map(struct net_device *dev_rx, void *fwd,
3542 struct bpf_map *map, struct xdp_buff *xdp)
3544 switch (map->map_type) {
3545 case BPF_MAP_TYPE_DEVMAP:
3546 case BPF_MAP_TYPE_DEVMAP_HASH:
3547 return dev_map_enqueue(fwd, xdp, dev_rx);
3548 case BPF_MAP_TYPE_CPUMAP:
3549 return cpu_map_enqueue(fwd, xdp, dev_rx);
3550 case BPF_MAP_TYPE_XSKMAP:
3551 return __xsk_map_redirect(fwd, xdp);
3558 void xdp_do_flush(void)
3564 EXPORT_SYMBOL_GPL(xdp_do_flush);
3566 static inline void *__xdp_map_lookup_elem(struct bpf_map *map, u32 index)
3568 switch (map->map_type) {
3569 case BPF_MAP_TYPE_DEVMAP:
3570 return __dev_map_lookup_elem(map, index);
3571 case BPF_MAP_TYPE_DEVMAP_HASH:
3572 return __dev_map_hash_lookup_elem(map, index);
3573 case BPF_MAP_TYPE_CPUMAP:
3574 return __cpu_map_lookup_elem(map, index);
3575 case BPF_MAP_TYPE_XSKMAP:
3576 return __xsk_map_lookup_elem(map, index);
3582 void bpf_clear_redirect_map(struct bpf_map *map)
3584 struct bpf_redirect_info *ri;
3587 for_each_possible_cpu(cpu) {
3588 ri = per_cpu_ptr(&bpf_redirect_info, cpu);
3589 /* Avoid polluting remote cacheline due to writes if
3590 * not needed. Once we pass this test, we need the
3591 * cmpxchg() to make sure it hasn't been changed in
3592 * the meantime by remote CPU.
3594 if (unlikely(READ_ONCE(ri->map) == map))
3595 cmpxchg(&ri->map, map, NULL);
3599 int xdp_do_redirect(struct net_device *dev, struct xdp_buff *xdp,
3600 struct bpf_prog *xdp_prog)
3602 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
3603 struct bpf_map *map = READ_ONCE(ri->map);
3604 u32 index = ri->tgt_index;
3605 void *fwd = ri->tgt_value;
3609 ri->tgt_value = NULL;
3610 WRITE_ONCE(ri->map, NULL);
3612 if (unlikely(!map)) {
3613 fwd = dev_get_by_index_rcu(dev_net(dev), index);
3614 if (unlikely(!fwd)) {
3619 err = dev_xdp_enqueue(fwd, xdp, dev);
3621 err = __bpf_tx_xdp_map(dev, fwd, map, xdp);
3627 _trace_xdp_redirect_map(dev, xdp_prog, fwd, map, index);
3630 _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map, index, err);
3633 EXPORT_SYMBOL_GPL(xdp_do_redirect);
3635 static int xdp_do_generic_redirect_map(struct net_device *dev,
3636 struct sk_buff *skb,
3637 struct xdp_buff *xdp,
3638 struct bpf_prog *xdp_prog,
3639 struct bpf_map *map)
3641 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
3642 u32 index = ri->tgt_index;
3643 void *fwd = ri->tgt_value;
3647 ri->tgt_value = NULL;
3648 WRITE_ONCE(ri->map, NULL);
3650 if (map->map_type == BPF_MAP_TYPE_DEVMAP ||
3651 map->map_type == BPF_MAP_TYPE_DEVMAP_HASH) {
3652 struct bpf_dtab_netdev *dst = fwd;
3654 err = dev_map_generic_redirect(dst, skb, xdp_prog);
3657 } else if (map->map_type == BPF_MAP_TYPE_XSKMAP) {
3658 struct xdp_sock *xs = fwd;
3660 err = xsk_generic_rcv(xs, xdp);
3665 /* TODO: Handle BPF_MAP_TYPE_CPUMAP */
3670 _trace_xdp_redirect_map(dev, xdp_prog, fwd, map, index);
3673 _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map, index, err);
3677 int xdp_do_generic_redirect(struct net_device *dev, struct sk_buff *skb,
3678 struct xdp_buff *xdp, struct bpf_prog *xdp_prog)
3680 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
3681 struct bpf_map *map = READ_ONCE(ri->map);
3682 u32 index = ri->tgt_index;
3683 struct net_device *fwd;
3687 return xdp_do_generic_redirect_map(dev, skb, xdp, xdp_prog,
3690 fwd = dev_get_by_index_rcu(dev_net(dev), index);
3691 if (unlikely(!fwd)) {
3696 err = xdp_ok_fwd_dev(fwd, skb->len);
3701 _trace_xdp_redirect(dev, xdp_prog, index);
3702 generic_xdp_tx(skb, xdp_prog);
3705 _trace_xdp_redirect_err(dev, xdp_prog, index, err);
3709 BPF_CALL_2(bpf_xdp_redirect, u32, ifindex, u64, flags)
3711 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
3713 if (unlikely(flags))
3717 ri->tgt_index = ifindex;
3718 ri->tgt_value = NULL;
3719 WRITE_ONCE(ri->map, NULL);
3721 return XDP_REDIRECT;
3724 static const struct bpf_func_proto bpf_xdp_redirect_proto = {
3725 .func = bpf_xdp_redirect,
3727 .ret_type = RET_INTEGER,
3728 .arg1_type = ARG_ANYTHING,
3729 .arg2_type = ARG_ANYTHING,
3732 BPF_CALL_3(bpf_xdp_redirect_map, struct bpf_map *, map, u32, ifindex,
3735 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
3737 /* Lower bits of the flags are used as return code on lookup failure */
3738 if (unlikely(flags > XDP_TX))
3741 ri->tgt_value = __xdp_map_lookup_elem(map, ifindex);
3742 if (unlikely(!ri->tgt_value)) {
3743 /* If the lookup fails we want to clear out the state in the
3744 * redirect_info struct completely, so that if an eBPF program
3745 * performs multiple lookups, the last one always takes
3748 WRITE_ONCE(ri->map, NULL);
3753 ri->tgt_index = ifindex;
3754 WRITE_ONCE(ri->map, map);
3756 return XDP_REDIRECT;
3759 static const struct bpf_func_proto bpf_xdp_redirect_map_proto = {
3760 .func = bpf_xdp_redirect_map,
3762 .ret_type = RET_INTEGER,
3763 .arg1_type = ARG_CONST_MAP_PTR,
3764 .arg2_type = ARG_ANYTHING,
3765 .arg3_type = ARG_ANYTHING,
3768 static unsigned long bpf_skb_copy(void *dst_buff, const void *skb,
3769 unsigned long off, unsigned long len)
3771 void *ptr = skb_header_pointer(skb, off, len, dst_buff);
3775 if (ptr != dst_buff)
3776 memcpy(dst_buff, ptr, len);
3781 BPF_CALL_5(bpf_skb_event_output, struct sk_buff *, skb, struct bpf_map *, map,
3782 u64, flags, void *, meta, u64, meta_size)
3784 u64 skb_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
3786 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
3788 if (unlikely(!skb || skb_size > skb->len))
3791 return bpf_event_output(map, flags, meta, meta_size, skb, skb_size,
3795 static const struct bpf_func_proto bpf_skb_event_output_proto = {
3796 .func = bpf_skb_event_output,
3798 .ret_type = RET_INTEGER,
3799 .arg1_type = ARG_PTR_TO_CTX,
3800 .arg2_type = ARG_CONST_MAP_PTR,
3801 .arg3_type = ARG_ANYTHING,
3802 .arg4_type = ARG_PTR_TO_MEM,
3803 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
3806 BTF_ID_LIST(bpf_skb_output_btf_ids)
3807 BTF_ID(struct, sk_buff)
3809 const struct bpf_func_proto bpf_skb_output_proto = {
3810 .func = bpf_skb_event_output,
3812 .ret_type = RET_INTEGER,
3813 .arg1_type = ARG_PTR_TO_BTF_ID,
3814 .arg2_type = ARG_CONST_MAP_PTR,
3815 .arg3_type = ARG_ANYTHING,
3816 .arg4_type = ARG_PTR_TO_MEM,
3817 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
3818 .btf_id = bpf_skb_output_btf_ids,
3821 static unsigned short bpf_tunnel_key_af(u64 flags)
3823 return flags & BPF_F_TUNINFO_IPV6 ? AF_INET6 : AF_INET;
3826 BPF_CALL_4(bpf_skb_get_tunnel_key, struct sk_buff *, skb, struct bpf_tunnel_key *, to,
3827 u32, size, u64, flags)
3829 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
3830 u8 compat[sizeof(struct bpf_tunnel_key)];
3834 if (unlikely(!info || (flags & ~(BPF_F_TUNINFO_IPV6)))) {
3838 if (ip_tunnel_info_af(info) != bpf_tunnel_key_af(flags)) {
3842 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
3845 case offsetof(struct bpf_tunnel_key, tunnel_label):
3846 case offsetof(struct bpf_tunnel_key, tunnel_ext):
3848 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
3849 /* Fixup deprecated structure layouts here, so we have
3850 * a common path later on.
3852 if (ip_tunnel_info_af(info) != AF_INET)
3855 to = (struct bpf_tunnel_key *)compat;
3862 to->tunnel_id = be64_to_cpu(info->key.tun_id);
3863 to->tunnel_tos = info->key.tos;
3864 to->tunnel_ttl = info->key.ttl;
3867 if (flags & BPF_F_TUNINFO_IPV6) {
3868 memcpy(to->remote_ipv6, &info->key.u.ipv6.src,
3869 sizeof(to->remote_ipv6));
3870 to->tunnel_label = be32_to_cpu(info->key.label);
3872 to->remote_ipv4 = be32_to_cpu(info->key.u.ipv4.src);
3873 memset(&to->remote_ipv6[1], 0, sizeof(__u32) * 3);
3874 to->tunnel_label = 0;
3877 if (unlikely(size != sizeof(struct bpf_tunnel_key)))
3878 memcpy(to_orig, to, size);
3882 memset(to_orig, 0, size);
3886 static const struct bpf_func_proto bpf_skb_get_tunnel_key_proto = {
3887 .func = bpf_skb_get_tunnel_key,
3889 .ret_type = RET_INTEGER,
3890 .arg1_type = ARG_PTR_TO_CTX,
3891 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
3892 .arg3_type = ARG_CONST_SIZE,
3893 .arg4_type = ARG_ANYTHING,
3896 BPF_CALL_3(bpf_skb_get_tunnel_opt, struct sk_buff *, skb, u8 *, to, u32, size)
3898 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
3901 if (unlikely(!info ||
3902 !(info->key.tun_flags & TUNNEL_OPTIONS_PRESENT))) {
3906 if (unlikely(size < info->options_len)) {
3911 ip_tunnel_info_opts_get(to, info);
3912 if (size > info->options_len)
3913 memset(to + info->options_len, 0, size - info->options_len);
3915 return info->options_len;
3917 memset(to, 0, size);
3921 static const struct bpf_func_proto bpf_skb_get_tunnel_opt_proto = {
3922 .func = bpf_skb_get_tunnel_opt,
3924 .ret_type = RET_INTEGER,
3925 .arg1_type = ARG_PTR_TO_CTX,
3926 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
3927 .arg3_type = ARG_CONST_SIZE,
3930 static struct metadata_dst __percpu *md_dst;
3932 BPF_CALL_4(bpf_skb_set_tunnel_key, struct sk_buff *, skb,
3933 const struct bpf_tunnel_key *, from, u32, size, u64, flags)
3935 struct metadata_dst *md = this_cpu_ptr(md_dst);
3936 u8 compat[sizeof(struct bpf_tunnel_key)];
3937 struct ip_tunnel_info *info;
3939 if (unlikely(flags & ~(BPF_F_TUNINFO_IPV6 | BPF_F_ZERO_CSUM_TX |
3940 BPF_F_DONT_FRAGMENT | BPF_F_SEQ_NUMBER)))
3942 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
3944 case offsetof(struct bpf_tunnel_key, tunnel_label):
3945 case offsetof(struct bpf_tunnel_key, tunnel_ext):
3946 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
3947 /* Fixup deprecated structure layouts here, so we have
3948 * a common path later on.
3950 memcpy(compat, from, size);
3951 memset(compat + size, 0, sizeof(compat) - size);
3952 from = (const struct bpf_tunnel_key *) compat;
3958 if (unlikely((!(flags & BPF_F_TUNINFO_IPV6) && from->tunnel_label) ||
3963 dst_hold((struct dst_entry *) md);
3964 skb_dst_set(skb, (struct dst_entry *) md);
3966 info = &md->u.tun_info;
3967 memset(info, 0, sizeof(*info));
3968 info->mode = IP_TUNNEL_INFO_TX;
3970 info->key.tun_flags = TUNNEL_KEY | TUNNEL_CSUM | TUNNEL_NOCACHE;
3971 if (flags & BPF_F_DONT_FRAGMENT)
3972 info->key.tun_flags |= TUNNEL_DONT_FRAGMENT;
3973 if (flags & BPF_F_ZERO_CSUM_TX)
3974 info->key.tun_flags &= ~TUNNEL_CSUM;
3975 if (flags & BPF_F_SEQ_NUMBER)
3976 info->key.tun_flags |= TUNNEL_SEQ;
3978 info->key.tun_id = cpu_to_be64(from->tunnel_id);
3979 info->key.tos = from->tunnel_tos;
3980 info->key.ttl = from->tunnel_ttl;
3982 if (flags & BPF_F_TUNINFO_IPV6) {
3983 info->mode |= IP_TUNNEL_INFO_IPV6;
3984 memcpy(&info->key.u.ipv6.dst, from->remote_ipv6,
3985 sizeof(from->remote_ipv6));
3986 info->key.label = cpu_to_be32(from->tunnel_label) &
3987 IPV6_FLOWLABEL_MASK;
3989 info->key.u.ipv4.dst = cpu_to_be32(from->remote_ipv4);
3995 static const struct bpf_func_proto bpf_skb_set_tunnel_key_proto = {
3996 .func = bpf_skb_set_tunnel_key,
3998 .ret_type = RET_INTEGER,
3999 .arg1_type = ARG_PTR_TO_CTX,
4000 .arg2_type = ARG_PTR_TO_MEM,
4001 .arg3_type = ARG_CONST_SIZE,
4002 .arg4_type = ARG_ANYTHING,
4005 BPF_CALL_3(bpf_skb_set_tunnel_opt, struct sk_buff *, skb,
4006 const u8 *, from, u32, size)
4008 struct ip_tunnel_info *info = skb_tunnel_info(skb);
4009 const struct metadata_dst *md = this_cpu_ptr(md_dst);
4011 if (unlikely(info != &md->u.tun_info || (size & (sizeof(u32) - 1))))
4013 if (unlikely(size > IP_TUNNEL_OPTS_MAX))
4016 ip_tunnel_info_opts_set(info, from, size, TUNNEL_OPTIONS_PRESENT);
4021 static const struct bpf_func_proto bpf_skb_set_tunnel_opt_proto = {
4022 .func = bpf_skb_set_tunnel_opt,
4024 .ret_type = RET_INTEGER,
4025 .arg1_type = ARG_PTR_TO_CTX,
4026 .arg2_type = ARG_PTR_TO_MEM,
4027 .arg3_type = ARG_CONST_SIZE,
4030 static const struct bpf_func_proto *
4031 bpf_get_skb_set_tunnel_proto(enum bpf_func_id which)
4034 struct metadata_dst __percpu *tmp;
4036 tmp = metadata_dst_alloc_percpu(IP_TUNNEL_OPTS_MAX,
4041 if (cmpxchg(&md_dst, NULL, tmp))
4042 metadata_dst_free_percpu(tmp);
4046 case BPF_FUNC_skb_set_tunnel_key:
4047 return &bpf_skb_set_tunnel_key_proto;
4048 case BPF_FUNC_skb_set_tunnel_opt:
4049 return &bpf_skb_set_tunnel_opt_proto;
4055 BPF_CALL_3(bpf_skb_under_cgroup, struct sk_buff *, skb, struct bpf_map *, map,
4058 struct bpf_array *array = container_of(map, struct bpf_array, map);
4059 struct cgroup *cgrp;
4062 sk = skb_to_full_sk(skb);
4063 if (!sk || !sk_fullsock(sk))
4065 if (unlikely(idx >= array->map.max_entries))
4068 cgrp = READ_ONCE(array->ptrs[idx]);
4069 if (unlikely(!cgrp))
4072 return sk_under_cgroup_hierarchy(sk, cgrp);
4075 static const struct bpf_func_proto bpf_skb_under_cgroup_proto = {
4076 .func = bpf_skb_under_cgroup,
4078 .ret_type = RET_INTEGER,
4079 .arg1_type = ARG_PTR_TO_CTX,
4080 .arg2_type = ARG_CONST_MAP_PTR,
4081 .arg3_type = ARG_ANYTHING,
4084 #ifdef CONFIG_SOCK_CGROUP_DATA
4085 static inline u64 __bpf_sk_cgroup_id(struct sock *sk)
4087 struct cgroup *cgrp;
4089 cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
4090 return cgroup_id(cgrp);
4093 BPF_CALL_1(bpf_skb_cgroup_id, const struct sk_buff *, skb)
4095 struct sock *sk = skb_to_full_sk(skb);
4097 if (!sk || !sk_fullsock(sk))
4100 return __bpf_sk_cgroup_id(sk);
4103 static const struct bpf_func_proto bpf_skb_cgroup_id_proto = {
4104 .func = bpf_skb_cgroup_id,
4106 .ret_type = RET_INTEGER,
4107 .arg1_type = ARG_PTR_TO_CTX,
4110 static inline u64 __bpf_sk_ancestor_cgroup_id(struct sock *sk,
4113 struct cgroup *ancestor;
4114 struct cgroup *cgrp;
4116 cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
4117 ancestor = cgroup_ancestor(cgrp, ancestor_level);
4121 return cgroup_id(ancestor);
4124 BPF_CALL_2(bpf_skb_ancestor_cgroup_id, const struct sk_buff *, skb, int,
4127 struct sock *sk = skb_to_full_sk(skb);
4129 if (!sk || !sk_fullsock(sk))
4132 return __bpf_sk_ancestor_cgroup_id(sk, ancestor_level);
4135 static const struct bpf_func_proto bpf_skb_ancestor_cgroup_id_proto = {
4136 .func = bpf_skb_ancestor_cgroup_id,
4138 .ret_type = RET_INTEGER,
4139 .arg1_type = ARG_PTR_TO_CTX,
4140 .arg2_type = ARG_ANYTHING,
4143 BPF_CALL_1(bpf_sk_cgroup_id, struct sock *, sk)
4145 return __bpf_sk_cgroup_id(sk);
4148 static const struct bpf_func_proto bpf_sk_cgroup_id_proto = {
4149 .func = bpf_sk_cgroup_id,
4151 .ret_type = RET_INTEGER,
4152 .arg1_type = ARG_PTR_TO_SOCKET,
4155 BPF_CALL_2(bpf_sk_ancestor_cgroup_id, struct sock *, sk, int, ancestor_level)
4157 return __bpf_sk_ancestor_cgroup_id(sk, ancestor_level);
4160 static const struct bpf_func_proto bpf_sk_ancestor_cgroup_id_proto = {
4161 .func = bpf_sk_ancestor_cgroup_id,
4163 .ret_type = RET_INTEGER,
4164 .arg1_type = ARG_PTR_TO_SOCKET,
4165 .arg2_type = ARG_ANYTHING,
4169 static unsigned long bpf_xdp_copy(void *dst_buff, const void *src_buff,
4170 unsigned long off, unsigned long len)
4172 memcpy(dst_buff, src_buff + off, len);
4176 BPF_CALL_5(bpf_xdp_event_output, struct xdp_buff *, xdp, struct bpf_map *, map,
4177 u64, flags, void *, meta, u64, meta_size)
4179 u64 xdp_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
4181 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
4183 if (unlikely(!xdp ||
4184 xdp_size > (unsigned long)(xdp->data_end - xdp->data)))
4187 return bpf_event_output(map, flags, meta, meta_size, xdp->data,
4188 xdp_size, bpf_xdp_copy);
4191 static const struct bpf_func_proto bpf_xdp_event_output_proto = {
4192 .func = bpf_xdp_event_output,
4194 .ret_type = RET_INTEGER,
4195 .arg1_type = ARG_PTR_TO_CTX,
4196 .arg2_type = ARG_CONST_MAP_PTR,
4197 .arg3_type = ARG_ANYTHING,
4198 .arg4_type = ARG_PTR_TO_MEM,
4199 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
4202 BTF_ID_LIST(bpf_xdp_output_btf_ids)
4203 BTF_ID(struct, xdp_buff)
4205 const struct bpf_func_proto bpf_xdp_output_proto = {
4206 .func = bpf_xdp_event_output,
4208 .ret_type = RET_INTEGER,
4209 .arg1_type = ARG_PTR_TO_BTF_ID,
4210 .arg2_type = ARG_CONST_MAP_PTR,
4211 .arg3_type = ARG_ANYTHING,
4212 .arg4_type = ARG_PTR_TO_MEM,
4213 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
4214 .btf_id = bpf_xdp_output_btf_ids,
4217 BPF_CALL_1(bpf_get_socket_cookie, struct sk_buff *, skb)
4219 return skb->sk ? sock_gen_cookie(skb->sk) : 0;
4222 static const struct bpf_func_proto bpf_get_socket_cookie_proto = {
4223 .func = bpf_get_socket_cookie,
4225 .ret_type = RET_INTEGER,
4226 .arg1_type = ARG_PTR_TO_CTX,
4229 BPF_CALL_1(bpf_get_socket_cookie_sock_addr, struct bpf_sock_addr_kern *, ctx)
4231 return sock_gen_cookie(ctx->sk);
4234 static const struct bpf_func_proto bpf_get_socket_cookie_sock_addr_proto = {
4235 .func = bpf_get_socket_cookie_sock_addr,
4237 .ret_type = RET_INTEGER,
4238 .arg1_type = ARG_PTR_TO_CTX,
4241 BPF_CALL_1(bpf_get_socket_cookie_sock, struct sock *, ctx)
4243 return sock_gen_cookie(ctx);
4246 static const struct bpf_func_proto bpf_get_socket_cookie_sock_proto = {
4247 .func = bpf_get_socket_cookie_sock,
4249 .ret_type = RET_INTEGER,
4250 .arg1_type = ARG_PTR_TO_CTX,
4253 BPF_CALL_1(bpf_get_socket_cookie_sock_ops, struct bpf_sock_ops_kern *, ctx)
4255 return sock_gen_cookie(ctx->sk);
4258 static const struct bpf_func_proto bpf_get_socket_cookie_sock_ops_proto = {
4259 .func = bpf_get_socket_cookie_sock_ops,
4261 .ret_type = RET_INTEGER,
4262 .arg1_type = ARG_PTR_TO_CTX,
4265 static u64 __bpf_get_netns_cookie(struct sock *sk)
4267 #ifdef CONFIG_NET_NS
4268 return net_gen_cookie(sk ? sk->sk_net.net : &init_net);
4274 BPF_CALL_1(bpf_get_netns_cookie_sock, struct sock *, ctx)
4276 return __bpf_get_netns_cookie(ctx);
4279 static const struct bpf_func_proto bpf_get_netns_cookie_sock_proto = {
4280 .func = bpf_get_netns_cookie_sock,
4282 .ret_type = RET_INTEGER,
4283 .arg1_type = ARG_PTR_TO_CTX_OR_NULL,
4286 BPF_CALL_1(bpf_get_netns_cookie_sock_addr, struct bpf_sock_addr_kern *, ctx)
4288 return __bpf_get_netns_cookie(ctx ? ctx->sk : NULL);
4291 static const struct bpf_func_proto bpf_get_netns_cookie_sock_addr_proto = {
4292 .func = bpf_get_netns_cookie_sock_addr,
4294 .ret_type = RET_INTEGER,
4295 .arg1_type = ARG_PTR_TO_CTX_OR_NULL,
4298 BPF_CALL_1(bpf_get_socket_uid, struct sk_buff *, skb)
4300 struct sock *sk = sk_to_full_sk(skb->sk);
4303 if (!sk || !sk_fullsock(sk))
4305 kuid = sock_net_uid(sock_net(sk), sk);
4306 return from_kuid_munged(sock_net(sk)->user_ns, kuid);
4309 static const struct bpf_func_proto bpf_get_socket_uid_proto = {
4310 .func = bpf_get_socket_uid,
4312 .ret_type = RET_INTEGER,
4313 .arg1_type = ARG_PTR_TO_CTX,
4316 #define SOCKOPT_CC_REINIT (1 << 0)
4318 static int _bpf_setsockopt(struct sock *sk, int level, int optname,
4319 char *optval, int optlen, u32 flags)
4321 char devname[IFNAMSIZ];
4327 if (!sk_fullsock(sk))
4330 sock_owned_by_me(sk);
4332 if (level == SOL_SOCKET) {
4333 if (optlen != sizeof(int) && optname != SO_BINDTODEVICE)
4335 val = *((int *)optval);
4336 valbool = val ? 1 : 0;
4338 /* Only some socketops are supported */
4341 val = min_t(u32, val, sysctl_rmem_max);
4342 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
4343 WRITE_ONCE(sk->sk_rcvbuf,
4344 max_t(int, val * 2, SOCK_MIN_RCVBUF));
4347 val = min_t(u32, val, sysctl_wmem_max);
4348 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
4349 WRITE_ONCE(sk->sk_sndbuf,
4350 max_t(int, val * 2, SOCK_MIN_SNDBUF));
4352 case SO_MAX_PACING_RATE: /* 32bit version */
4354 cmpxchg(&sk->sk_pacing_status,
4357 sk->sk_max_pacing_rate = (val == ~0U) ? ~0UL : val;
4358 sk->sk_pacing_rate = min(sk->sk_pacing_rate,
4359 sk->sk_max_pacing_rate);
4362 sk->sk_priority = val;
4367 WRITE_ONCE(sk->sk_rcvlowat, val ? : 1);
4370 if (sk->sk_mark != val) {
4375 case SO_BINDTODEVICE:
4376 optlen = min_t(long, optlen, IFNAMSIZ - 1);
4377 strncpy(devname, optval, optlen);
4378 devname[optlen] = 0;
4381 if (devname[0] != '\0') {
4382 struct net_device *dev;
4387 dev = dev_get_by_name(net, devname);
4390 ifindex = dev->ifindex;
4393 ret = sock_bindtoindex(sk, ifindex, false);
4396 if (sk->sk_prot->keepalive)
4397 sk->sk_prot->keepalive(sk, valbool);
4398 sock_valbool_flag(sk, SOCK_KEEPOPEN, valbool);
4404 } else if (level == SOL_IP) {
4405 if (optlen != sizeof(int) || sk->sk_family != AF_INET)
4408 val = *((int *)optval);
4409 /* Only some options are supported */
4412 if (val < -1 || val > 0xff) {
4415 struct inet_sock *inet = inet_sk(sk);
4425 #if IS_ENABLED(CONFIG_IPV6)
4426 } else if (level == SOL_IPV6) {
4427 if (optlen != sizeof(int) || sk->sk_family != AF_INET6)
4430 val = *((int *)optval);
4431 /* Only some options are supported */
4434 if (val < -1 || val > 0xff) {
4437 struct ipv6_pinfo *np = inet6_sk(sk);
4448 } else if (level == SOL_TCP &&
4449 sk->sk_prot->setsockopt == tcp_setsockopt) {
4450 if (optname == TCP_CONGESTION) {
4451 char name[TCP_CA_NAME_MAX];
4452 bool reinit = flags & SOCKOPT_CC_REINIT;
4454 strncpy(name, optval, min_t(long, optlen,
4455 TCP_CA_NAME_MAX-1));
4456 name[TCP_CA_NAME_MAX-1] = 0;
4457 ret = tcp_set_congestion_control(sk, name, false,
4460 struct inet_connection_sock *icsk = inet_csk(sk);
4461 struct tcp_sock *tp = tcp_sk(sk);
4463 if (optlen != sizeof(int))
4466 val = *((int *)optval);
4467 /* Only some options are supported */
4470 if (val <= 0 || tp->data_segs_out > tp->syn_data)
4475 case TCP_BPF_SNDCWND_CLAMP:
4479 tp->snd_cwnd_clamp = val;
4480 tp->snd_ssthresh = val;
4484 if (val < 0 || val > 1)
4490 ret = tcp_sock_set_keepidle_locked(sk, val);
4493 if (val < 1 || val > MAX_TCP_KEEPINTVL)
4496 tp->keepalive_intvl = val * HZ;
4499 if (val < 1 || val > MAX_TCP_KEEPCNT)
4502 tp->keepalive_probes = val;
4505 if (val < 1 || val > MAX_TCP_SYNCNT)
4508 icsk->icsk_syn_retries = val;
4510 case TCP_USER_TIMEOUT:
4514 icsk->icsk_user_timeout = val;
4527 static int _bpf_getsockopt(struct sock *sk, int level, int optname,
4528 char *optval, int optlen)
4530 if (!sk_fullsock(sk))
4533 sock_owned_by_me(sk);
4536 if (level == SOL_TCP && sk->sk_prot->getsockopt == tcp_getsockopt) {
4537 struct inet_connection_sock *icsk;
4538 struct tcp_sock *tp;
4541 case TCP_CONGESTION:
4542 icsk = inet_csk(sk);
4544 if (!icsk->icsk_ca_ops || optlen <= 1)
4546 strncpy(optval, icsk->icsk_ca_ops->name, optlen);
4547 optval[optlen - 1] = 0;
4552 if (optlen <= 0 || !tp->saved_syn ||
4553 optlen > tp->saved_syn[0])
4555 memcpy(optval, tp->saved_syn + 1, optlen);
4560 } else if (level == SOL_IP) {
4561 struct inet_sock *inet = inet_sk(sk);
4563 if (optlen != sizeof(int) || sk->sk_family != AF_INET)
4566 /* Only some options are supported */
4569 *((int *)optval) = (int)inet->tos;
4574 #if IS_ENABLED(CONFIG_IPV6)
4575 } else if (level == SOL_IPV6) {
4576 struct ipv6_pinfo *np = inet6_sk(sk);
4578 if (optlen != sizeof(int) || sk->sk_family != AF_INET6)
4581 /* Only some options are supported */
4584 *((int *)optval) = (int)np->tclass;
4596 memset(optval, 0, optlen);
4600 BPF_CALL_5(bpf_sock_addr_setsockopt, struct bpf_sock_addr_kern *, ctx,
4601 int, level, int, optname, char *, optval, int, optlen)
4604 return _bpf_setsockopt(ctx->sk, level, optname, optval, optlen,
4608 static const struct bpf_func_proto bpf_sock_addr_setsockopt_proto = {
4609 .func = bpf_sock_addr_setsockopt,
4611 .ret_type = RET_INTEGER,
4612 .arg1_type = ARG_PTR_TO_CTX,
4613 .arg2_type = ARG_ANYTHING,
4614 .arg3_type = ARG_ANYTHING,
4615 .arg4_type = ARG_PTR_TO_MEM,
4616 .arg5_type = ARG_CONST_SIZE,
4619 BPF_CALL_5(bpf_sock_addr_getsockopt, struct bpf_sock_addr_kern *, ctx,
4620 int, level, int, optname, char *, optval, int, optlen)
4622 return _bpf_getsockopt(ctx->sk, level, optname, optval, optlen);
4625 static const struct bpf_func_proto bpf_sock_addr_getsockopt_proto = {
4626 .func = bpf_sock_addr_getsockopt,
4628 .ret_type = RET_INTEGER,
4629 .arg1_type = ARG_PTR_TO_CTX,
4630 .arg2_type = ARG_ANYTHING,
4631 .arg3_type = ARG_ANYTHING,
4632 .arg4_type = ARG_PTR_TO_UNINIT_MEM,
4633 .arg5_type = ARG_CONST_SIZE,
4636 BPF_CALL_5(bpf_sock_ops_setsockopt, struct bpf_sock_ops_kern *, bpf_sock,
4637 int, level, int, optname, char *, optval, int, optlen)
4640 if (bpf_sock->op > BPF_SOCK_OPS_NEEDS_ECN)
4641 flags |= SOCKOPT_CC_REINIT;
4642 return _bpf_setsockopt(bpf_sock->sk, level, optname, optval, optlen,
4646 static const struct bpf_func_proto bpf_sock_ops_setsockopt_proto = {
4647 .func = bpf_sock_ops_setsockopt,
4649 .ret_type = RET_INTEGER,
4650 .arg1_type = ARG_PTR_TO_CTX,
4651 .arg2_type = ARG_ANYTHING,
4652 .arg3_type = ARG_ANYTHING,
4653 .arg4_type = ARG_PTR_TO_MEM,
4654 .arg5_type = ARG_CONST_SIZE,
4657 BPF_CALL_5(bpf_sock_ops_getsockopt, struct bpf_sock_ops_kern *, bpf_sock,
4658 int, level, int, optname, char *, optval, int, optlen)
4660 return _bpf_getsockopt(bpf_sock->sk, level, optname, optval, optlen);
4663 static const struct bpf_func_proto bpf_sock_ops_getsockopt_proto = {
4664 .func = bpf_sock_ops_getsockopt,
4666 .ret_type = RET_INTEGER,
4667 .arg1_type = ARG_PTR_TO_CTX,
4668 .arg2_type = ARG_ANYTHING,
4669 .arg3_type = ARG_ANYTHING,
4670 .arg4_type = ARG_PTR_TO_UNINIT_MEM,
4671 .arg5_type = ARG_CONST_SIZE,
4674 BPF_CALL_2(bpf_sock_ops_cb_flags_set, struct bpf_sock_ops_kern *, bpf_sock,
4677 struct sock *sk = bpf_sock->sk;
4678 int val = argval & BPF_SOCK_OPS_ALL_CB_FLAGS;
4680 if (!IS_ENABLED(CONFIG_INET) || !sk_fullsock(sk))
4683 tcp_sk(sk)->bpf_sock_ops_cb_flags = val;
4685 return argval & (~BPF_SOCK_OPS_ALL_CB_FLAGS);
4688 static const struct bpf_func_proto bpf_sock_ops_cb_flags_set_proto = {
4689 .func = bpf_sock_ops_cb_flags_set,
4691 .ret_type = RET_INTEGER,
4692 .arg1_type = ARG_PTR_TO_CTX,
4693 .arg2_type = ARG_ANYTHING,
4696 const struct ipv6_bpf_stub *ipv6_bpf_stub __read_mostly;
4697 EXPORT_SYMBOL_GPL(ipv6_bpf_stub);
4699 BPF_CALL_3(bpf_bind, struct bpf_sock_addr_kern *, ctx, struct sockaddr *, addr,
4703 struct sock *sk = ctx->sk;
4704 u32 flags = BIND_FROM_BPF;
4708 if (addr_len < offsetofend(struct sockaddr, sa_family))
4710 if (addr->sa_family == AF_INET) {
4711 if (addr_len < sizeof(struct sockaddr_in))
4713 if (((struct sockaddr_in *)addr)->sin_port == htons(0))
4714 flags |= BIND_FORCE_ADDRESS_NO_PORT;
4715 return __inet_bind(sk, addr, addr_len, flags);
4716 #if IS_ENABLED(CONFIG_IPV6)
4717 } else if (addr->sa_family == AF_INET6) {
4718 if (addr_len < SIN6_LEN_RFC2133)
4720 if (((struct sockaddr_in6 *)addr)->sin6_port == htons(0))
4721 flags |= BIND_FORCE_ADDRESS_NO_PORT;
4722 /* ipv6_bpf_stub cannot be NULL, since it's called from
4723 * bpf_cgroup_inet6_connect hook and ipv6 is already loaded
4725 return ipv6_bpf_stub->inet6_bind(sk, addr, addr_len, flags);
4726 #endif /* CONFIG_IPV6 */
4728 #endif /* CONFIG_INET */
4730 return -EAFNOSUPPORT;
4733 static const struct bpf_func_proto bpf_bind_proto = {
4736 .ret_type = RET_INTEGER,
4737 .arg1_type = ARG_PTR_TO_CTX,
4738 .arg2_type = ARG_PTR_TO_MEM,
4739 .arg3_type = ARG_CONST_SIZE,
4743 BPF_CALL_5(bpf_skb_get_xfrm_state, struct sk_buff *, skb, u32, index,
4744 struct bpf_xfrm_state *, to, u32, size, u64, flags)
4746 const struct sec_path *sp = skb_sec_path(skb);
4747 const struct xfrm_state *x;
4749 if (!sp || unlikely(index >= sp->len || flags))
4752 x = sp->xvec[index];
4754 if (unlikely(size != sizeof(struct bpf_xfrm_state)))
4757 to->reqid = x->props.reqid;
4758 to->spi = x->id.spi;
4759 to->family = x->props.family;
4762 if (to->family == AF_INET6) {
4763 memcpy(to->remote_ipv6, x->props.saddr.a6,
4764 sizeof(to->remote_ipv6));
4766 to->remote_ipv4 = x->props.saddr.a4;
4767 memset(&to->remote_ipv6[1], 0, sizeof(__u32) * 3);
4772 memset(to, 0, size);
4776 static const struct bpf_func_proto bpf_skb_get_xfrm_state_proto = {
4777 .func = bpf_skb_get_xfrm_state,
4779 .ret_type = RET_INTEGER,
4780 .arg1_type = ARG_PTR_TO_CTX,
4781 .arg2_type = ARG_ANYTHING,
4782 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
4783 .arg4_type = ARG_CONST_SIZE,
4784 .arg5_type = ARG_ANYTHING,
4788 #if IS_ENABLED(CONFIG_INET) || IS_ENABLED(CONFIG_IPV6)
4789 static int bpf_fib_set_fwd_params(struct bpf_fib_lookup *params,
4790 const struct neighbour *neigh,
4791 const struct net_device *dev)
4793 memcpy(params->dmac, neigh->ha, ETH_ALEN);
4794 memcpy(params->smac, dev->dev_addr, ETH_ALEN);
4795 params->h_vlan_TCI = 0;
4796 params->h_vlan_proto = 0;
4797 params->ifindex = dev->ifindex;
4803 #if IS_ENABLED(CONFIG_INET)
4804 static int bpf_ipv4_fib_lookup(struct net *net, struct bpf_fib_lookup *params,
4805 u32 flags, bool check_mtu)
4807 struct fib_nh_common *nhc;
4808 struct in_device *in_dev;
4809 struct neighbour *neigh;
4810 struct net_device *dev;
4811 struct fib_result res;
4816 dev = dev_get_by_index_rcu(net, params->ifindex);
4820 /* verify forwarding is enabled on this interface */
4821 in_dev = __in_dev_get_rcu(dev);
4822 if (unlikely(!in_dev || !IN_DEV_FORWARD(in_dev)))
4823 return BPF_FIB_LKUP_RET_FWD_DISABLED;
4825 if (flags & BPF_FIB_LOOKUP_OUTPUT) {
4827 fl4.flowi4_oif = params->ifindex;
4829 fl4.flowi4_iif = params->ifindex;
4832 fl4.flowi4_tos = params->tos & IPTOS_RT_MASK;
4833 fl4.flowi4_scope = RT_SCOPE_UNIVERSE;
4834 fl4.flowi4_flags = 0;
4836 fl4.flowi4_proto = params->l4_protocol;
4837 fl4.daddr = params->ipv4_dst;
4838 fl4.saddr = params->ipv4_src;
4839 fl4.fl4_sport = params->sport;
4840 fl4.fl4_dport = params->dport;
4842 if (flags & BPF_FIB_LOOKUP_DIRECT) {
4843 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
4844 struct fib_table *tb;
4846 tb = fib_get_table(net, tbid);
4848 return BPF_FIB_LKUP_RET_NOT_FWDED;
4850 err = fib_table_lookup(tb, &fl4, &res, FIB_LOOKUP_NOREF);
4852 fl4.flowi4_mark = 0;
4853 fl4.flowi4_secid = 0;
4854 fl4.flowi4_tun_key.tun_id = 0;
4855 fl4.flowi4_uid = sock_net_uid(net, NULL);
4857 err = fib_lookup(net, &fl4, &res, FIB_LOOKUP_NOREF);
4861 /* map fib lookup errors to RTN_ type */
4863 return BPF_FIB_LKUP_RET_BLACKHOLE;
4864 if (err == -EHOSTUNREACH)
4865 return BPF_FIB_LKUP_RET_UNREACHABLE;
4867 return BPF_FIB_LKUP_RET_PROHIBIT;
4869 return BPF_FIB_LKUP_RET_NOT_FWDED;
4872 if (res.type != RTN_UNICAST)
4873 return BPF_FIB_LKUP_RET_NOT_FWDED;
4875 if (fib_info_num_path(res.fi) > 1)
4876 fib_select_path(net, &res, &fl4, NULL);
4879 mtu = ip_mtu_from_fib_result(&res, params->ipv4_dst);
4880 if (params->tot_len > mtu)
4881 return BPF_FIB_LKUP_RET_FRAG_NEEDED;
4886 /* do not handle lwt encaps right now */
4887 if (nhc->nhc_lwtstate)
4888 return BPF_FIB_LKUP_RET_UNSUPP_LWT;
4892 params->rt_metric = res.fi->fib_priority;
4894 /* xdp and cls_bpf programs are run in RCU-bh so
4895 * rcu_read_lock_bh is not needed here
4897 if (likely(nhc->nhc_gw_family != AF_INET6)) {
4898 if (nhc->nhc_gw_family)
4899 params->ipv4_dst = nhc->nhc_gw.ipv4;
4901 neigh = __ipv4_neigh_lookup_noref(dev,
4902 (__force u32)params->ipv4_dst);
4904 struct in6_addr *dst = (struct in6_addr *)params->ipv6_dst;
4906 params->family = AF_INET6;
4907 *dst = nhc->nhc_gw.ipv6;
4908 neigh = __ipv6_neigh_lookup_noref_stub(dev, dst);
4912 return BPF_FIB_LKUP_RET_NO_NEIGH;
4914 return bpf_fib_set_fwd_params(params, neigh, dev);
4918 #if IS_ENABLED(CONFIG_IPV6)
4919 static int bpf_ipv6_fib_lookup(struct net *net, struct bpf_fib_lookup *params,
4920 u32 flags, bool check_mtu)
4922 struct in6_addr *src = (struct in6_addr *) params->ipv6_src;
4923 struct in6_addr *dst = (struct in6_addr *) params->ipv6_dst;
4924 struct fib6_result res = {};
4925 struct neighbour *neigh;
4926 struct net_device *dev;
4927 struct inet6_dev *idev;
4933 /* link local addresses are never forwarded */
4934 if (rt6_need_strict(dst) || rt6_need_strict(src))
4935 return BPF_FIB_LKUP_RET_NOT_FWDED;
4937 dev = dev_get_by_index_rcu(net, params->ifindex);
4941 idev = __in6_dev_get_safely(dev);
4942 if (unlikely(!idev || !idev->cnf.forwarding))
4943 return BPF_FIB_LKUP_RET_FWD_DISABLED;
4945 if (flags & BPF_FIB_LOOKUP_OUTPUT) {
4947 oif = fl6.flowi6_oif = params->ifindex;
4949 oif = fl6.flowi6_iif = params->ifindex;
4951 strict = RT6_LOOKUP_F_HAS_SADDR;
4953 fl6.flowlabel = params->flowinfo;
4954 fl6.flowi6_scope = 0;
4955 fl6.flowi6_flags = 0;
4958 fl6.flowi6_proto = params->l4_protocol;
4961 fl6.fl6_sport = params->sport;
4962 fl6.fl6_dport = params->dport;
4964 if (flags & BPF_FIB_LOOKUP_DIRECT) {
4965 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
4966 struct fib6_table *tb;
4968 tb = ipv6_stub->fib6_get_table(net, tbid);
4970 return BPF_FIB_LKUP_RET_NOT_FWDED;
4972 err = ipv6_stub->fib6_table_lookup(net, tb, oif, &fl6, &res,
4975 fl6.flowi6_mark = 0;
4976 fl6.flowi6_secid = 0;
4977 fl6.flowi6_tun_key.tun_id = 0;
4978 fl6.flowi6_uid = sock_net_uid(net, NULL);
4980 err = ipv6_stub->fib6_lookup(net, oif, &fl6, &res, strict);
4983 if (unlikely(err || IS_ERR_OR_NULL(res.f6i) ||
4984 res.f6i == net->ipv6.fib6_null_entry))
4985 return BPF_FIB_LKUP_RET_NOT_FWDED;
4987 switch (res.fib6_type) {
4988 /* only unicast is forwarded */
4992 return BPF_FIB_LKUP_RET_BLACKHOLE;
4993 case RTN_UNREACHABLE:
4994 return BPF_FIB_LKUP_RET_UNREACHABLE;
4996 return BPF_FIB_LKUP_RET_PROHIBIT;
4998 return BPF_FIB_LKUP_RET_NOT_FWDED;
5001 ipv6_stub->fib6_select_path(net, &res, &fl6, fl6.flowi6_oif,
5002 fl6.flowi6_oif != 0, NULL, strict);
5005 mtu = ipv6_stub->ip6_mtu_from_fib6(&res, dst, src);
5006 if (params->tot_len > mtu)
5007 return BPF_FIB_LKUP_RET_FRAG_NEEDED;
5010 if (res.nh->fib_nh_lws)
5011 return BPF_FIB_LKUP_RET_UNSUPP_LWT;
5013 if (res.nh->fib_nh_gw_family)
5014 *dst = res.nh->fib_nh_gw6;
5016 dev = res.nh->fib_nh_dev;
5017 params->rt_metric = res.f6i->fib6_metric;
5019 /* xdp and cls_bpf programs are run in RCU-bh so rcu_read_lock_bh is
5022 neigh = __ipv6_neigh_lookup_noref_stub(dev, dst);
5024 return BPF_FIB_LKUP_RET_NO_NEIGH;
5026 return bpf_fib_set_fwd_params(params, neigh, dev);
5030 BPF_CALL_4(bpf_xdp_fib_lookup, struct xdp_buff *, ctx,
5031 struct bpf_fib_lookup *, params, int, plen, u32, flags)
5033 if (plen < sizeof(*params))
5036 if (flags & ~(BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_OUTPUT))
5039 switch (params->family) {
5040 #if IS_ENABLED(CONFIG_INET)
5042 return bpf_ipv4_fib_lookup(dev_net(ctx->rxq->dev), params,
5045 #if IS_ENABLED(CONFIG_IPV6)
5047 return bpf_ipv6_fib_lookup(dev_net(ctx->rxq->dev), params,
5051 return -EAFNOSUPPORT;
5054 static const struct bpf_func_proto bpf_xdp_fib_lookup_proto = {
5055 .func = bpf_xdp_fib_lookup,
5057 .ret_type = RET_INTEGER,
5058 .arg1_type = ARG_PTR_TO_CTX,
5059 .arg2_type = ARG_PTR_TO_MEM,
5060 .arg3_type = ARG_CONST_SIZE,
5061 .arg4_type = ARG_ANYTHING,
5064 BPF_CALL_4(bpf_skb_fib_lookup, struct sk_buff *, skb,
5065 struct bpf_fib_lookup *, params, int, plen, u32, flags)
5067 struct net *net = dev_net(skb->dev);
5068 int rc = -EAFNOSUPPORT;
5070 if (plen < sizeof(*params))
5073 if (flags & ~(BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_OUTPUT))
5076 switch (params->family) {
5077 #if IS_ENABLED(CONFIG_INET)
5079 rc = bpf_ipv4_fib_lookup(net, params, flags, false);
5082 #if IS_ENABLED(CONFIG_IPV6)
5084 rc = bpf_ipv6_fib_lookup(net, params, flags, false);
5090 struct net_device *dev;
5092 dev = dev_get_by_index_rcu(net, params->ifindex);
5093 if (!is_skb_forwardable(dev, skb))
5094 rc = BPF_FIB_LKUP_RET_FRAG_NEEDED;
5100 static const struct bpf_func_proto bpf_skb_fib_lookup_proto = {
5101 .func = bpf_skb_fib_lookup,
5103 .ret_type = RET_INTEGER,
5104 .arg1_type = ARG_PTR_TO_CTX,
5105 .arg2_type = ARG_PTR_TO_MEM,
5106 .arg3_type = ARG_CONST_SIZE,
5107 .arg4_type = ARG_ANYTHING,
5110 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
5111 static int bpf_push_seg6_encap(struct sk_buff *skb, u32 type, void *hdr, u32 len)
5114 struct ipv6_sr_hdr *srh = (struct ipv6_sr_hdr *)hdr;
5116 if (!seg6_validate_srh(srh, len, false))
5120 case BPF_LWT_ENCAP_SEG6_INLINE:
5121 if (skb->protocol != htons(ETH_P_IPV6))
5124 err = seg6_do_srh_inline(skb, srh);
5126 case BPF_LWT_ENCAP_SEG6:
5127 skb_reset_inner_headers(skb);
5128 skb->encapsulation = 1;
5129 err = seg6_do_srh_encap(skb, srh, IPPROTO_IPV6);
5135 bpf_compute_data_pointers(skb);
5139 ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
5140 skb_set_transport_header(skb, sizeof(struct ipv6hdr));
5142 return seg6_lookup_nexthop(skb, NULL, 0);
5144 #endif /* CONFIG_IPV6_SEG6_BPF */
5146 #if IS_ENABLED(CONFIG_LWTUNNEL_BPF)
5147 static int bpf_push_ip_encap(struct sk_buff *skb, void *hdr, u32 len,
5150 return bpf_lwt_push_ip_encap(skb, hdr, len, ingress);
5154 BPF_CALL_4(bpf_lwt_in_push_encap, struct sk_buff *, skb, u32, type, void *, hdr,
5158 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
5159 case BPF_LWT_ENCAP_SEG6:
5160 case BPF_LWT_ENCAP_SEG6_INLINE:
5161 return bpf_push_seg6_encap(skb, type, hdr, len);
5163 #if IS_ENABLED(CONFIG_LWTUNNEL_BPF)
5164 case BPF_LWT_ENCAP_IP:
5165 return bpf_push_ip_encap(skb, hdr, len, true /* ingress */);
5172 BPF_CALL_4(bpf_lwt_xmit_push_encap, struct sk_buff *, skb, u32, type,
5173 void *, hdr, u32, len)
5176 #if IS_ENABLED(CONFIG_LWTUNNEL_BPF)
5177 case BPF_LWT_ENCAP_IP:
5178 return bpf_push_ip_encap(skb, hdr, len, false /* egress */);
5185 static const struct bpf_func_proto bpf_lwt_in_push_encap_proto = {
5186 .func = bpf_lwt_in_push_encap,
5188 .ret_type = RET_INTEGER,
5189 .arg1_type = ARG_PTR_TO_CTX,
5190 .arg2_type = ARG_ANYTHING,
5191 .arg3_type = ARG_PTR_TO_MEM,
5192 .arg4_type = ARG_CONST_SIZE
5195 static const struct bpf_func_proto bpf_lwt_xmit_push_encap_proto = {
5196 .func = bpf_lwt_xmit_push_encap,
5198 .ret_type = RET_INTEGER,
5199 .arg1_type = ARG_PTR_TO_CTX,
5200 .arg2_type = ARG_ANYTHING,
5201 .arg3_type = ARG_PTR_TO_MEM,
5202 .arg4_type = ARG_CONST_SIZE
5205 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
5206 BPF_CALL_4(bpf_lwt_seg6_store_bytes, struct sk_buff *, skb, u32, offset,
5207 const void *, from, u32, len)
5209 struct seg6_bpf_srh_state *srh_state =
5210 this_cpu_ptr(&seg6_bpf_srh_states);
5211 struct ipv6_sr_hdr *srh = srh_state->srh;
5212 void *srh_tlvs, *srh_end, *ptr;
5218 srh_tlvs = (void *)((char *)srh + ((srh->first_segment + 1) << 4));
5219 srh_end = (void *)((char *)srh + sizeof(*srh) + srh_state->hdrlen);
5221 ptr = skb->data + offset;
5222 if (ptr >= srh_tlvs && ptr + len <= srh_end)
5223 srh_state->valid = false;
5224 else if (ptr < (void *)&srh->flags ||
5225 ptr + len > (void *)&srh->segments)
5228 if (unlikely(bpf_try_make_writable(skb, offset + len)))
5230 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0)
5232 srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
5234 memcpy(skb->data + offset, from, len);
5238 static const struct bpf_func_proto bpf_lwt_seg6_store_bytes_proto = {
5239 .func = bpf_lwt_seg6_store_bytes,
5241 .ret_type = RET_INTEGER,
5242 .arg1_type = ARG_PTR_TO_CTX,
5243 .arg2_type = ARG_ANYTHING,
5244 .arg3_type = ARG_PTR_TO_MEM,
5245 .arg4_type = ARG_CONST_SIZE
5248 static void bpf_update_srh_state(struct sk_buff *skb)
5250 struct seg6_bpf_srh_state *srh_state =
5251 this_cpu_ptr(&seg6_bpf_srh_states);
5254 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0) {
5255 srh_state->srh = NULL;
5257 srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
5258 srh_state->hdrlen = srh_state->srh->hdrlen << 3;
5259 srh_state->valid = true;
5263 BPF_CALL_4(bpf_lwt_seg6_action, struct sk_buff *, skb,
5264 u32, action, void *, param, u32, param_len)
5266 struct seg6_bpf_srh_state *srh_state =
5267 this_cpu_ptr(&seg6_bpf_srh_states);
5272 case SEG6_LOCAL_ACTION_END_X:
5273 if (!seg6_bpf_has_valid_srh(skb))
5275 if (param_len != sizeof(struct in6_addr))
5277 return seg6_lookup_nexthop(skb, (struct in6_addr *)param, 0);
5278 case SEG6_LOCAL_ACTION_END_T:
5279 if (!seg6_bpf_has_valid_srh(skb))
5281 if (param_len != sizeof(int))
5283 return seg6_lookup_nexthop(skb, NULL, *(int *)param);
5284 case SEG6_LOCAL_ACTION_END_DT6:
5285 if (!seg6_bpf_has_valid_srh(skb))
5287 if (param_len != sizeof(int))
5290 if (ipv6_find_hdr(skb, &hdroff, IPPROTO_IPV6, NULL, NULL) < 0)
5292 if (!pskb_pull(skb, hdroff))
5295 skb_postpull_rcsum(skb, skb_network_header(skb), hdroff);
5296 skb_reset_network_header(skb);
5297 skb_reset_transport_header(skb);
5298 skb->encapsulation = 0;
5300 bpf_compute_data_pointers(skb);
5301 bpf_update_srh_state(skb);
5302 return seg6_lookup_nexthop(skb, NULL, *(int *)param);
5303 case SEG6_LOCAL_ACTION_END_B6:
5304 if (srh_state->srh && !seg6_bpf_has_valid_srh(skb))
5306 err = bpf_push_seg6_encap(skb, BPF_LWT_ENCAP_SEG6_INLINE,
5309 bpf_update_srh_state(skb);
5312 case SEG6_LOCAL_ACTION_END_B6_ENCAP:
5313 if (srh_state->srh && !seg6_bpf_has_valid_srh(skb))
5315 err = bpf_push_seg6_encap(skb, BPF_LWT_ENCAP_SEG6,
5318 bpf_update_srh_state(skb);
5326 static const struct bpf_func_proto bpf_lwt_seg6_action_proto = {
5327 .func = bpf_lwt_seg6_action,
5329 .ret_type = RET_INTEGER,
5330 .arg1_type = ARG_PTR_TO_CTX,
5331 .arg2_type = ARG_ANYTHING,
5332 .arg3_type = ARG_PTR_TO_MEM,
5333 .arg4_type = ARG_CONST_SIZE
5336 BPF_CALL_3(bpf_lwt_seg6_adjust_srh, struct sk_buff *, skb, u32, offset,
5339 struct seg6_bpf_srh_state *srh_state =
5340 this_cpu_ptr(&seg6_bpf_srh_states);
5341 struct ipv6_sr_hdr *srh = srh_state->srh;
5342 void *srh_end, *srh_tlvs, *ptr;
5343 struct ipv6hdr *hdr;
5347 if (unlikely(srh == NULL))
5350 srh_tlvs = (void *)((unsigned char *)srh + sizeof(*srh) +
5351 ((srh->first_segment + 1) << 4));
5352 srh_end = (void *)((unsigned char *)srh + sizeof(*srh) +
5354 ptr = skb->data + offset;
5356 if (unlikely(ptr < srh_tlvs || ptr > srh_end))
5358 if (unlikely(len < 0 && (void *)((char *)ptr - len) > srh_end))
5362 ret = skb_cow_head(skb, len);
5363 if (unlikely(ret < 0))
5366 ret = bpf_skb_net_hdr_push(skb, offset, len);
5368 ret = bpf_skb_net_hdr_pop(skb, offset, -1 * len);
5371 bpf_compute_data_pointers(skb);
5372 if (unlikely(ret < 0))
5375 hdr = (struct ipv6hdr *)skb->data;
5376 hdr->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
5378 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0)
5380 srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
5381 srh_state->hdrlen += len;
5382 srh_state->valid = false;
5386 static const struct bpf_func_proto bpf_lwt_seg6_adjust_srh_proto = {
5387 .func = bpf_lwt_seg6_adjust_srh,
5389 .ret_type = RET_INTEGER,
5390 .arg1_type = ARG_PTR_TO_CTX,
5391 .arg2_type = ARG_ANYTHING,
5392 .arg3_type = ARG_ANYTHING,
5394 #endif /* CONFIG_IPV6_SEG6_BPF */
5397 static struct sock *sk_lookup(struct net *net, struct bpf_sock_tuple *tuple,
5398 int dif, int sdif, u8 family, u8 proto)
5400 bool refcounted = false;
5401 struct sock *sk = NULL;
5403 if (family == AF_INET) {
5404 __be32 src4 = tuple->ipv4.saddr;
5405 __be32 dst4 = tuple->ipv4.daddr;
5407 if (proto == IPPROTO_TCP)
5408 sk = __inet_lookup(net, &tcp_hashinfo, NULL, 0,
5409 src4, tuple->ipv4.sport,
5410 dst4, tuple->ipv4.dport,
5411 dif, sdif, &refcounted);
5413 sk = __udp4_lib_lookup(net, src4, tuple->ipv4.sport,
5414 dst4, tuple->ipv4.dport,
5415 dif, sdif, &udp_table, NULL);
5416 #if IS_ENABLED(CONFIG_IPV6)
5418 struct in6_addr *src6 = (struct in6_addr *)&tuple->ipv6.saddr;
5419 struct in6_addr *dst6 = (struct in6_addr *)&tuple->ipv6.daddr;
5421 if (proto == IPPROTO_TCP)
5422 sk = __inet6_lookup(net, &tcp_hashinfo, NULL, 0,
5423 src6, tuple->ipv6.sport,
5424 dst6, ntohs(tuple->ipv6.dport),
5425 dif, sdif, &refcounted);
5426 else if (likely(ipv6_bpf_stub))
5427 sk = ipv6_bpf_stub->udp6_lib_lookup(net,
5428 src6, tuple->ipv6.sport,
5429 dst6, tuple->ipv6.dport,
5435 if (unlikely(sk && !refcounted && !sock_flag(sk, SOCK_RCU_FREE))) {
5436 WARN_ONCE(1, "Found non-RCU, unreferenced socket!");
5442 /* bpf_skc_lookup performs the core lookup for different types of sockets,
5443 * taking a reference on the socket if it doesn't have the flag SOCK_RCU_FREE.
5444 * Returns the socket as an 'unsigned long' to simplify the casting in the
5445 * callers to satisfy BPF_CALL declarations.
5447 static struct sock *
5448 __bpf_skc_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
5449 struct net *caller_net, u32 ifindex, u8 proto, u64 netns_id,
5452 struct sock *sk = NULL;
5453 u8 family = AF_UNSPEC;
5457 if (len == sizeof(tuple->ipv4))
5459 else if (len == sizeof(tuple->ipv6))
5464 if (unlikely(family == AF_UNSPEC || flags ||
5465 !((s32)netns_id < 0 || netns_id <= S32_MAX)))
5468 if (family == AF_INET)
5469 sdif = inet_sdif(skb);
5471 sdif = inet6_sdif(skb);
5473 if ((s32)netns_id < 0) {
5475 sk = sk_lookup(net, tuple, ifindex, sdif, family, proto);
5477 net = get_net_ns_by_id(caller_net, netns_id);
5480 sk = sk_lookup(net, tuple, ifindex, sdif, family, proto);
5488 static struct sock *
5489 __bpf_sk_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
5490 struct net *caller_net, u32 ifindex, u8 proto, u64 netns_id,
5493 struct sock *sk = __bpf_skc_lookup(skb, tuple, len, caller_net,
5494 ifindex, proto, netns_id, flags);
5497 sk = sk_to_full_sk(sk);
5498 if (!sk_fullsock(sk)) {
5507 static struct sock *
5508 bpf_skc_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
5509 u8 proto, u64 netns_id, u64 flags)
5511 struct net *caller_net;
5515 caller_net = dev_net(skb->dev);
5516 ifindex = skb->dev->ifindex;
5518 caller_net = sock_net(skb->sk);
5522 return __bpf_skc_lookup(skb, tuple, len, caller_net, ifindex, proto,
5526 static struct sock *
5527 bpf_sk_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
5528 u8 proto, u64 netns_id, u64 flags)
5530 struct sock *sk = bpf_skc_lookup(skb, tuple, len, proto, netns_id,
5534 sk = sk_to_full_sk(sk);
5535 if (!sk_fullsock(sk)) {
5544 BPF_CALL_5(bpf_skc_lookup_tcp, struct sk_buff *, skb,
5545 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
5547 return (unsigned long)bpf_skc_lookup(skb, tuple, len, IPPROTO_TCP,
5551 static const struct bpf_func_proto bpf_skc_lookup_tcp_proto = {
5552 .func = bpf_skc_lookup_tcp,
5555 .ret_type = RET_PTR_TO_SOCK_COMMON_OR_NULL,
5556 .arg1_type = ARG_PTR_TO_CTX,
5557 .arg2_type = ARG_PTR_TO_MEM,
5558 .arg3_type = ARG_CONST_SIZE,
5559 .arg4_type = ARG_ANYTHING,
5560 .arg5_type = ARG_ANYTHING,
5563 BPF_CALL_5(bpf_sk_lookup_tcp, struct sk_buff *, skb,
5564 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
5566 return (unsigned long)bpf_sk_lookup(skb, tuple, len, IPPROTO_TCP,
5570 static const struct bpf_func_proto bpf_sk_lookup_tcp_proto = {
5571 .func = bpf_sk_lookup_tcp,
5574 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
5575 .arg1_type = ARG_PTR_TO_CTX,
5576 .arg2_type = ARG_PTR_TO_MEM,
5577 .arg3_type = ARG_CONST_SIZE,
5578 .arg4_type = ARG_ANYTHING,
5579 .arg5_type = ARG_ANYTHING,
5582 BPF_CALL_5(bpf_sk_lookup_udp, struct sk_buff *, skb,
5583 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
5585 return (unsigned long)bpf_sk_lookup(skb, tuple, len, IPPROTO_UDP,
5589 static const struct bpf_func_proto bpf_sk_lookup_udp_proto = {
5590 .func = bpf_sk_lookup_udp,
5593 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
5594 .arg1_type = ARG_PTR_TO_CTX,
5595 .arg2_type = ARG_PTR_TO_MEM,
5596 .arg3_type = ARG_CONST_SIZE,
5597 .arg4_type = ARG_ANYTHING,
5598 .arg5_type = ARG_ANYTHING,
5601 BPF_CALL_1(bpf_sk_release, struct sock *, sk)
5603 if (sk_is_refcounted(sk))
5608 static const struct bpf_func_proto bpf_sk_release_proto = {
5609 .func = bpf_sk_release,
5611 .ret_type = RET_INTEGER,
5612 .arg1_type = ARG_PTR_TO_SOCK_COMMON,
5615 BPF_CALL_5(bpf_xdp_sk_lookup_udp, struct xdp_buff *, ctx,
5616 struct bpf_sock_tuple *, tuple, u32, len, u32, netns_id, u64, flags)
5618 struct net *caller_net = dev_net(ctx->rxq->dev);
5619 int ifindex = ctx->rxq->dev->ifindex;
5621 return (unsigned long)__bpf_sk_lookup(NULL, tuple, len, caller_net,
5622 ifindex, IPPROTO_UDP, netns_id,
5626 static const struct bpf_func_proto bpf_xdp_sk_lookup_udp_proto = {
5627 .func = bpf_xdp_sk_lookup_udp,
5630 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
5631 .arg1_type = ARG_PTR_TO_CTX,
5632 .arg2_type = ARG_PTR_TO_MEM,
5633 .arg3_type = ARG_CONST_SIZE,
5634 .arg4_type = ARG_ANYTHING,
5635 .arg5_type = ARG_ANYTHING,
5638 BPF_CALL_5(bpf_xdp_skc_lookup_tcp, struct xdp_buff *, ctx,
5639 struct bpf_sock_tuple *, tuple, u32, len, u32, netns_id, u64, flags)
5641 struct net *caller_net = dev_net(ctx->rxq->dev);
5642 int ifindex = ctx->rxq->dev->ifindex;
5644 return (unsigned long)__bpf_skc_lookup(NULL, tuple, len, caller_net,
5645 ifindex, IPPROTO_TCP, netns_id,
5649 static const struct bpf_func_proto bpf_xdp_skc_lookup_tcp_proto = {
5650 .func = bpf_xdp_skc_lookup_tcp,
5653 .ret_type = RET_PTR_TO_SOCK_COMMON_OR_NULL,
5654 .arg1_type = ARG_PTR_TO_CTX,
5655 .arg2_type = ARG_PTR_TO_MEM,
5656 .arg3_type = ARG_CONST_SIZE,
5657 .arg4_type = ARG_ANYTHING,
5658 .arg5_type = ARG_ANYTHING,
5661 BPF_CALL_5(bpf_xdp_sk_lookup_tcp, struct xdp_buff *, ctx,
5662 struct bpf_sock_tuple *, tuple, u32, len, u32, netns_id, u64, flags)
5664 struct net *caller_net = dev_net(ctx->rxq->dev);
5665 int ifindex = ctx->rxq->dev->ifindex;
5667 return (unsigned long)__bpf_sk_lookup(NULL, tuple, len, caller_net,
5668 ifindex, IPPROTO_TCP, netns_id,
5672 static const struct bpf_func_proto bpf_xdp_sk_lookup_tcp_proto = {
5673 .func = bpf_xdp_sk_lookup_tcp,
5676 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
5677 .arg1_type = ARG_PTR_TO_CTX,
5678 .arg2_type = ARG_PTR_TO_MEM,
5679 .arg3_type = ARG_CONST_SIZE,
5680 .arg4_type = ARG_ANYTHING,
5681 .arg5_type = ARG_ANYTHING,
5684 BPF_CALL_5(bpf_sock_addr_skc_lookup_tcp, struct bpf_sock_addr_kern *, ctx,
5685 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
5687 return (unsigned long)__bpf_skc_lookup(NULL, tuple, len,
5688 sock_net(ctx->sk), 0,
5689 IPPROTO_TCP, netns_id, flags);
5692 static const struct bpf_func_proto bpf_sock_addr_skc_lookup_tcp_proto = {
5693 .func = bpf_sock_addr_skc_lookup_tcp,
5695 .ret_type = RET_PTR_TO_SOCK_COMMON_OR_NULL,
5696 .arg1_type = ARG_PTR_TO_CTX,
5697 .arg2_type = ARG_PTR_TO_MEM,
5698 .arg3_type = ARG_CONST_SIZE,
5699 .arg4_type = ARG_ANYTHING,
5700 .arg5_type = ARG_ANYTHING,
5703 BPF_CALL_5(bpf_sock_addr_sk_lookup_tcp, struct bpf_sock_addr_kern *, ctx,
5704 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
5706 return (unsigned long)__bpf_sk_lookup(NULL, tuple, len,
5707 sock_net(ctx->sk), 0, IPPROTO_TCP,
5711 static const struct bpf_func_proto bpf_sock_addr_sk_lookup_tcp_proto = {
5712 .func = bpf_sock_addr_sk_lookup_tcp,
5714 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
5715 .arg1_type = ARG_PTR_TO_CTX,
5716 .arg2_type = ARG_PTR_TO_MEM,
5717 .arg3_type = ARG_CONST_SIZE,
5718 .arg4_type = ARG_ANYTHING,
5719 .arg5_type = ARG_ANYTHING,
5722 BPF_CALL_5(bpf_sock_addr_sk_lookup_udp, struct bpf_sock_addr_kern *, ctx,
5723 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
5725 return (unsigned long)__bpf_sk_lookup(NULL, tuple, len,
5726 sock_net(ctx->sk), 0, IPPROTO_UDP,
5730 static const struct bpf_func_proto bpf_sock_addr_sk_lookup_udp_proto = {
5731 .func = bpf_sock_addr_sk_lookup_udp,
5733 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
5734 .arg1_type = ARG_PTR_TO_CTX,
5735 .arg2_type = ARG_PTR_TO_MEM,
5736 .arg3_type = ARG_CONST_SIZE,
5737 .arg4_type = ARG_ANYTHING,
5738 .arg5_type = ARG_ANYTHING,
5741 bool bpf_tcp_sock_is_valid_access(int off, int size, enum bpf_access_type type,
5742 struct bpf_insn_access_aux *info)
5744 if (off < 0 || off >= offsetofend(struct bpf_tcp_sock,
5748 if (off % size != 0)
5752 case offsetof(struct bpf_tcp_sock, bytes_received):
5753 case offsetof(struct bpf_tcp_sock, bytes_acked):
5754 return size == sizeof(__u64);
5756 return size == sizeof(__u32);
5760 u32 bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type,
5761 const struct bpf_insn *si,
5762 struct bpf_insn *insn_buf,
5763 struct bpf_prog *prog, u32 *target_size)
5765 struct bpf_insn *insn = insn_buf;
5767 #define BPF_TCP_SOCK_GET_COMMON(FIELD) \
5769 BUILD_BUG_ON(sizeof_field(struct tcp_sock, FIELD) > \
5770 sizeof_field(struct bpf_tcp_sock, FIELD)); \
5771 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct tcp_sock, FIELD),\
5772 si->dst_reg, si->src_reg, \
5773 offsetof(struct tcp_sock, FIELD)); \
5776 #define BPF_INET_SOCK_GET_COMMON(FIELD) \
5778 BUILD_BUG_ON(sizeof_field(struct inet_connection_sock, \
5780 sizeof_field(struct bpf_tcp_sock, FIELD)); \
5781 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
5782 struct inet_connection_sock, \
5784 si->dst_reg, si->src_reg, \
5786 struct inet_connection_sock, \
5790 if (insn > insn_buf)
5791 return insn - insn_buf;
5794 case offsetof(struct bpf_tcp_sock, rtt_min):
5795 BUILD_BUG_ON(sizeof_field(struct tcp_sock, rtt_min) !=
5796 sizeof(struct minmax));
5797 BUILD_BUG_ON(sizeof(struct minmax) <
5798 sizeof(struct minmax_sample));
5800 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
5801 offsetof(struct tcp_sock, rtt_min) +
5802 offsetof(struct minmax_sample, v));
5804 case offsetof(struct bpf_tcp_sock, snd_cwnd):
5805 BPF_TCP_SOCK_GET_COMMON(snd_cwnd);
5807 case offsetof(struct bpf_tcp_sock, srtt_us):
5808 BPF_TCP_SOCK_GET_COMMON(srtt_us);
5810 case offsetof(struct bpf_tcp_sock, snd_ssthresh):
5811 BPF_TCP_SOCK_GET_COMMON(snd_ssthresh);
5813 case offsetof(struct bpf_tcp_sock, rcv_nxt):
5814 BPF_TCP_SOCK_GET_COMMON(rcv_nxt);
5816 case offsetof(struct bpf_tcp_sock, snd_nxt):
5817 BPF_TCP_SOCK_GET_COMMON(snd_nxt);
5819 case offsetof(struct bpf_tcp_sock, snd_una):
5820 BPF_TCP_SOCK_GET_COMMON(snd_una);
5822 case offsetof(struct bpf_tcp_sock, mss_cache):
5823 BPF_TCP_SOCK_GET_COMMON(mss_cache);
5825 case offsetof(struct bpf_tcp_sock, ecn_flags):
5826 BPF_TCP_SOCK_GET_COMMON(ecn_flags);
5828 case offsetof(struct bpf_tcp_sock, rate_delivered):
5829 BPF_TCP_SOCK_GET_COMMON(rate_delivered);
5831 case offsetof(struct bpf_tcp_sock, rate_interval_us):
5832 BPF_TCP_SOCK_GET_COMMON(rate_interval_us);
5834 case offsetof(struct bpf_tcp_sock, packets_out):
5835 BPF_TCP_SOCK_GET_COMMON(packets_out);
5837 case offsetof(struct bpf_tcp_sock, retrans_out):
5838 BPF_TCP_SOCK_GET_COMMON(retrans_out);
5840 case offsetof(struct bpf_tcp_sock, total_retrans):
5841 BPF_TCP_SOCK_GET_COMMON(total_retrans);
5843 case offsetof(struct bpf_tcp_sock, segs_in):
5844 BPF_TCP_SOCK_GET_COMMON(segs_in);
5846 case offsetof(struct bpf_tcp_sock, data_segs_in):
5847 BPF_TCP_SOCK_GET_COMMON(data_segs_in);
5849 case offsetof(struct bpf_tcp_sock, segs_out):
5850 BPF_TCP_SOCK_GET_COMMON(segs_out);
5852 case offsetof(struct bpf_tcp_sock, data_segs_out):
5853 BPF_TCP_SOCK_GET_COMMON(data_segs_out);
5855 case offsetof(struct bpf_tcp_sock, lost_out):
5856 BPF_TCP_SOCK_GET_COMMON(lost_out);
5858 case offsetof(struct bpf_tcp_sock, sacked_out):
5859 BPF_TCP_SOCK_GET_COMMON(sacked_out);
5861 case offsetof(struct bpf_tcp_sock, bytes_received):
5862 BPF_TCP_SOCK_GET_COMMON(bytes_received);
5864 case offsetof(struct bpf_tcp_sock, bytes_acked):
5865 BPF_TCP_SOCK_GET_COMMON(bytes_acked);
5867 case offsetof(struct bpf_tcp_sock, dsack_dups):
5868 BPF_TCP_SOCK_GET_COMMON(dsack_dups);
5870 case offsetof(struct bpf_tcp_sock, delivered):
5871 BPF_TCP_SOCK_GET_COMMON(delivered);
5873 case offsetof(struct bpf_tcp_sock, delivered_ce):
5874 BPF_TCP_SOCK_GET_COMMON(delivered_ce);
5876 case offsetof(struct bpf_tcp_sock, icsk_retransmits):
5877 BPF_INET_SOCK_GET_COMMON(icsk_retransmits);
5881 return insn - insn_buf;
5884 BPF_CALL_1(bpf_tcp_sock, struct sock *, sk)
5886 if (sk_fullsock(sk) && sk->sk_protocol == IPPROTO_TCP)
5887 return (unsigned long)sk;
5889 return (unsigned long)NULL;
5892 const struct bpf_func_proto bpf_tcp_sock_proto = {
5893 .func = bpf_tcp_sock,
5895 .ret_type = RET_PTR_TO_TCP_SOCK_OR_NULL,
5896 .arg1_type = ARG_PTR_TO_SOCK_COMMON,
5899 BPF_CALL_1(bpf_get_listener_sock, struct sock *, sk)
5901 sk = sk_to_full_sk(sk);
5903 if (sk->sk_state == TCP_LISTEN && sock_flag(sk, SOCK_RCU_FREE))
5904 return (unsigned long)sk;
5906 return (unsigned long)NULL;
5909 static const struct bpf_func_proto bpf_get_listener_sock_proto = {
5910 .func = bpf_get_listener_sock,
5912 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
5913 .arg1_type = ARG_PTR_TO_SOCK_COMMON,
5916 BPF_CALL_1(bpf_skb_ecn_set_ce, struct sk_buff *, skb)
5918 unsigned int iphdr_len;
5920 switch (skb_protocol(skb, true)) {
5921 case cpu_to_be16(ETH_P_IP):
5922 iphdr_len = sizeof(struct iphdr);
5924 case cpu_to_be16(ETH_P_IPV6):
5925 iphdr_len = sizeof(struct ipv6hdr);
5931 if (skb_headlen(skb) < iphdr_len)
5934 if (skb_cloned(skb) && !skb_clone_writable(skb, iphdr_len))
5937 return INET_ECN_set_ce(skb);
5940 bool bpf_xdp_sock_is_valid_access(int off, int size, enum bpf_access_type type,
5941 struct bpf_insn_access_aux *info)
5943 if (off < 0 || off >= offsetofend(struct bpf_xdp_sock, queue_id))
5946 if (off % size != 0)
5951 return size == sizeof(__u32);
5955 u32 bpf_xdp_sock_convert_ctx_access(enum bpf_access_type type,
5956 const struct bpf_insn *si,
5957 struct bpf_insn *insn_buf,
5958 struct bpf_prog *prog, u32 *target_size)
5960 struct bpf_insn *insn = insn_buf;
5962 #define BPF_XDP_SOCK_GET(FIELD) \
5964 BUILD_BUG_ON(sizeof_field(struct xdp_sock, FIELD) > \
5965 sizeof_field(struct bpf_xdp_sock, FIELD)); \
5966 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_sock, FIELD),\
5967 si->dst_reg, si->src_reg, \
5968 offsetof(struct xdp_sock, FIELD)); \
5972 case offsetof(struct bpf_xdp_sock, queue_id):
5973 BPF_XDP_SOCK_GET(queue_id);
5977 return insn - insn_buf;
5980 static const struct bpf_func_proto bpf_skb_ecn_set_ce_proto = {
5981 .func = bpf_skb_ecn_set_ce,
5983 .ret_type = RET_INTEGER,
5984 .arg1_type = ARG_PTR_TO_CTX,
5987 BPF_CALL_5(bpf_tcp_check_syncookie, struct sock *, sk, void *, iph, u32, iph_len,
5988 struct tcphdr *, th, u32, th_len)
5990 #ifdef CONFIG_SYN_COOKIES
5994 if (unlikely(th_len < sizeof(*th)))
5997 /* sk_listener() allows TCP_NEW_SYN_RECV, which makes no sense here. */
5998 if (sk->sk_protocol != IPPROTO_TCP || sk->sk_state != TCP_LISTEN)
6001 if (!sock_net(sk)->ipv4.sysctl_tcp_syncookies)
6004 if (!th->ack || th->rst || th->syn)
6007 if (tcp_synq_no_recent_overflow(sk))
6010 cookie = ntohl(th->ack_seq) - 1;
6012 switch (sk->sk_family) {
6014 if (unlikely(iph_len < sizeof(struct iphdr)))
6017 ret = __cookie_v4_check((struct iphdr *)iph, th, cookie);
6020 #if IS_BUILTIN(CONFIG_IPV6)
6022 if (unlikely(iph_len < sizeof(struct ipv6hdr)))
6025 ret = __cookie_v6_check((struct ipv6hdr *)iph, th, cookie);
6027 #endif /* CONFIG_IPV6 */
6030 return -EPROTONOSUPPORT;
6042 static const struct bpf_func_proto bpf_tcp_check_syncookie_proto = {
6043 .func = bpf_tcp_check_syncookie,
6046 .ret_type = RET_INTEGER,
6047 .arg1_type = ARG_PTR_TO_SOCK_COMMON,
6048 .arg2_type = ARG_PTR_TO_MEM,
6049 .arg3_type = ARG_CONST_SIZE,
6050 .arg4_type = ARG_PTR_TO_MEM,
6051 .arg5_type = ARG_CONST_SIZE,
6054 BPF_CALL_5(bpf_tcp_gen_syncookie, struct sock *, sk, void *, iph, u32, iph_len,
6055 struct tcphdr *, th, u32, th_len)
6057 #ifdef CONFIG_SYN_COOKIES
6061 if (unlikely(th_len < sizeof(*th) || th_len != th->doff * 4))
6064 if (sk->sk_protocol != IPPROTO_TCP || sk->sk_state != TCP_LISTEN)
6067 if (!sock_net(sk)->ipv4.sysctl_tcp_syncookies)
6070 if (!th->syn || th->ack || th->fin || th->rst)
6073 if (unlikely(iph_len < sizeof(struct iphdr)))
6076 /* Both struct iphdr and struct ipv6hdr have the version field at the
6077 * same offset so we can cast to the shorter header (struct iphdr).
6079 switch (((struct iphdr *)iph)->version) {
6081 if (sk->sk_family == AF_INET6 && sk->sk_ipv6only)
6084 mss = tcp_v4_get_syncookie(sk, iph, th, &cookie);
6087 #if IS_BUILTIN(CONFIG_IPV6)
6089 if (unlikely(iph_len < sizeof(struct ipv6hdr)))
6092 if (sk->sk_family != AF_INET6)
6095 mss = tcp_v6_get_syncookie(sk, iph, th, &cookie);
6097 #endif /* CONFIG_IPV6 */
6100 return -EPROTONOSUPPORT;
6105 return cookie | ((u64)mss << 32);
6108 #endif /* CONFIG_SYN_COOKIES */
6111 static const struct bpf_func_proto bpf_tcp_gen_syncookie_proto = {
6112 .func = bpf_tcp_gen_syncookie,
6113 .gpl_only = true, /* __cookie_v*_init_sequence() is GPL */
6115 .ret_type = RET_INTEGER,
6116 .arg1_type = ARG_PTR_TO_SOCK_COMMON,
6117 .arg2_type = ARG_PTR_TO_MEM,
6118 .arg3_type = ARG_CONST_SIZE,
6119 .arg4_type = ARG_PTR_TO_MEM,
6120 .arg5_type = ARG_CONST_SIZE,
6123 BPF_CALL_3(bpf_sk_assign, struct sk_buff *, skb, struct sock *, sk, u64, flags)
6127 if (!skb_at_tc_ingress(skb))
6129 if (unlikely(dev_net(skb->dev) != sock_net(sk)))
6130 return -ENETUNREACH;
6131 if (unlikely(sk_fullsock(sk) && sk->sk_reuseport))
6132 return -ESOCKTNOSUPPORT;
6133 if (sk_is_refcounted(sk) &&
6134 unlikely(!refcount_inc_not_zero(&sk->sk_refcnt)))
6139 skb->destructor = sock_pfree;
6144 static const struct bpf_func_proto bpf_sk_assign_proto = {
6145 .func = bpf_sk_assign,
6147 .ret_type = RET_INTEGER,
6148 .arg1_type = ARG_PTR_TO_CTX,
6149 .arg2_type = ARG_PTR_TO_SOCK_COMMON,
6150 .arg3_type = ARG_ANYTHING,
6153 #endif /* CONFIG_INET */
6155 bool bpf_helper_changes_pkt_data(void *func)
6157 if (func == bpf_skb_vlan_push ||
6158 func == bpf_skb_vlan_pop ||
6159 func == bpf_skb_store_bytes ||
6160 func == bpf_skb_change_proto ||
6161 func == bpf_skb_change_head ||
6162 func == sk_skb_change_head ||
6163 func == bpf_skb_change_tail ||
6164 func == sk_skb_change_tail ||
6165 func == bpf_skb_adjust_room ||
6166 func == bpf_skb_pull_data ||
6167 func == sk_skb_pull_data ||
6168 func == bpf_clone_redirect ||
6169 func == bpf_l3_csum_replace ||
6170 func == bpf_l4_csum_replace ||
6171 func == bpf_xdp_adjust_head ||
6172 func == bpf_xdp_adjust_meta ||
6173 func == bpf_msg_pull_data ||
6174 func == bpf_msg_push_data ||
6175 func == bpf_msg_pop_data ||
6176 func == bpf_xdp_adjust_tail ||
6177 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
6178 func == bpf_lwt_seg6_store_bytes ||
6179 func == bpf_lwt_seg6_adjust_srh ||
6180 func == bpf_lwt_seg6_action ||
6182 func == bpf_lwt_in_push_encap ||
6183 func == bpf_lwt_xmit_push_encap)
6189 const struct bpf_func_proto bpf_event_output_data_proto __weak;
6191 static const struct bpf_func_proto *
6192 sock_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6195 /* inet and inet6 sockets are created in a process
6196 * context so there is always a valid uid/gid
6198 case BPF_FUNC_get_current_uid_gid:
6199 return &bpf_get_current_uid_gid_proto;
6200 case BPF_FUNC_get_local_storage:
6201 return &bpf_get_local_storage_proto;
6202 case BPF_FUNC_get_socket_cookie:
6203 return &bpf_get_socket_cookie_sock_proto;
6204 case BPF_FUNC_get_netns_cookie:
6205 return &bpf_get_netns_cookie_sock_proto;
6206 case BPF_FUNC_perf_event_output:
6207 return &bpf_event_output_data_proto;
6208 case BPF_FUNC_get_current_pid_tgid:
6209 return &bpf_get_current_pid_tgid_proto;
6210 case BPF_FUNC_get_current_comm:
6211 return &bpf_get_current_comm_proto;
6212 #ifdef CONFIG_CGROUPS
6213 case BPF_FUNC_get_current_cgroup_id:
6214 return &bpf_get_current_cgroup_id_proto;
6215 case BPF_FUNC_get_current_ancestor_cgroup_id:
6216 return &bpf_get_current_ancestor_cgroup_id_proto;
6218 #ifdef CONFIG_CGROUP_NET_CLASSID
6219 case BPF_FUNC_get_cgroup_classid:
6220 return &bpf_get_cgroup_classid_curr_proto;
6223 return bpf_base_func_proto(func_id);
6227 static const struct bpf_func_proto *
6228 sock_addr_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6231 /* inet and inet6 sockets are created in a process
6232 * context so there is always a valid uid/gid
6234 case BPF_FUNC_get_current_uid_gid:
6235 return &bpf_get_current_uid_gid_proto;
6237 switch (prog->expected_attach_type) {
6238 case BPF_CGROUP_INET4_CONNECT:
6239 case BPF_CGROUP_INET6_CONNECT:
6240 return &bpf_bind_proto;
6244 case BPF_FUNC_get_socket_cookie:
6245 return &bpf_get_socket_cookie_sock_addr_proto;
6246 case BPF_FUNC_get_netns_cookie:
6247 return &bpf_get_netns_cookie_sock_addr_proto;
6248 case BPF_FUNC_get_local_storage:
6249 return &bpf_get_local_storage_proto;
6250 case BPF_FUNC_perf_event_output:
6251 return &bpf_event_output_data_proto;
6252 case BPF_FUNC_get_current_pid_tgid:
6253 return &bpf_get_current_pid_tgid_proto;
6254 case BPF_FUNC_get_current_comm:
6255 return &bpf_get_current_comm_proto;
6256 #ifdef CONFIG_CGROUPS
6257 case BPF_FUNC_get_current_cgroup_id:
6258 return &bpf_get_current_cgroup_id_proto;
6259 case BPF_FUNC_get_current_ancestor_cgroup_id:
6260 return &bpf_get_current_ancestor_cgroup_id_proto;
6262 #ifdef CONFIG_CGROUP_NET_CLASSID
6263 case BPF_FUNC_get_cgroup_classid:
6264 return &bpf_get_cgroup_classid_curr_proto;
6267 case BPF_FUNC_sk_lookup_tcp:
6268 return &bpf_sock_addr_sk_lookup_tcp_proto;
6269 case BPF_FUNC_sk_lookup_udp:
6270 return &bpf_sock_addr_sk_lookup_udp_proto;
6271 case BPF_FUNC_sk_release:
6272 return &bpf_sk_release_proto;
6273 case BPF_FUNC_skc_lookup_tcp:
6274 return &bpf_sock_addr_skc_lookup_tcp_proto;
6275 #endif /* CONFIG_INET */
6276 case BPF_FUNC_sk_storage_get:
6277 return &bpf_sk_storage_get_proto;
6278 case BPF_FUNC_sk_storage_delete:
6279 return &bpf_sk_storage_delete_proto;
6280 case BPF_FUNC_setsockopt:
6281 switch (prog->expected_attach_type) {
6282 case BPF_CGROUP_INET4_CONNECT:
6283 case BPF_CGROUP_INET6_CONNECT:
6284 return &bpf_sock_addr_setsockopt_proto;
6288 case BPF_FUNC_getsockopt:
6289 switch (prog->expected_attach_type) {
6290 case BPF_CGROUP_INET4_CONNECT:
6291 case BPF_CGROUP_INET6_CONNECT:
6292 return &bpf_sock_addr_getsockopt_proto;
6297 return bpf_base_func_proto(func_id);
6301 static const struct bpf_func_proto *
6302 sk_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6305 case BPF_FUNC_skb_load_bytes:
6306 return &bpf_skb_load_bytes_proto;
6307 case BPF_FUNC_skb_load_bytes_relative:
6308 return &bpf_skb_load_bytes_relative_proto;
6309 case BPF_FUNC_get_socket_cookie:
6310 return &bpf_get_socket_cookie_proto;
6311 case BPF_FUNC_get_socket_uid:
6312 return &bpf_get_socket_uid_proto;
6313 case BPF_FUNC_perf_event_output:
6314 return &bpf_skb_event_output_proto;
6316 return bpf_base_func_proto(func_id);
6320 const struct bpf_func_proto bpf_sk_storage_get_proto __weak;
6321 const struct bpf_func_proto bpf_sk_storage_delete_proto __weak;
6323 static const struct bpf_func_proto *
6324 cg_skb_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6327 case BPF_FUNC_get_local_storage:
6328 return &bpf_get_local_storage_proto;
6329 case BPF_FUNC_sk_fullsock:
6330 return &bpf_sk_fullsock_proto;
6331 case BPF_FUNC_sk_storage_get:
6332 return &bpf_sk_storage_get_proto;
6333 case BPF_FUNC_sk_storage_delete:
6334 return &bpf_sk_storage_delete_proto;
6335 case BPF_FUNC_perf_event_output:
6336 return &bpf_skb_event_output_proto;
6337 #ifdef CONFIG_SOCK_CGROUP_DATA
6338 case BPF_FUNC_skb_cgroup_id:
6339 return &bpf_skb_cgroup_id_proto;
6340 case BPF_FUNC_skb_ancestor_cgroup_id:
6341 return &bpf_skb_ancestor_cgroup_id_proto;
6342 case BPF_FUNC_sk_cgroup_id:
6343 return &bpf_sk_cgroup_id_proto;
6344 case BPF_FUNC_sk_ancestor_cgroup_id:
6345 return &bpf_sk_ancestor_cgroup_id_proto;
6348 case BPF_FUNC_sk_lookup_tcp:
6349 return &bpf_sk_lookup_tcp_proto;
6350 case BPF_FUNC_sk_lookup_udp:
6351 return &bpf_sk_lookup_udp_proto;
6352 case BPF_FUNC_sk_release:
6353 return &bpf_sk_release_proto;
6354 case BPF_FUNC_skc_lookup_tcp:
6355 return &bpf_skc_lookup_tcp_proto;
6356 case BPF_FUNC_tcp_sock:
6357 return &bpf_tcp_sock_proto;
6358 case BPF_FUNC_get_listener_sock:
6359 return &bpf_get_listener_sock_proto;
6360 case BPF_FUNC_skb_ecn_set_ce:
6361 return &bpf_skb_ecn_set_ce_proto;
6364 return sk_filter_func_proto(func_id, prog);
6368 static const struct bpf_func_proto *
6369 tc_cls_act_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6372 case BPF_FUNC_skb_store_bytes:
6373 return &bpf_skb_store_bytes_proto;
6374 case BPF_FUNC_skb_load_bytes:
6375 return &bpf_skb_load_bytes_proto;
6376 case BPF_FUNC_skb_load_bytes_relative:
6377 return &bpf_skb_load_bytes_relative_proto;
6378 case BPF_FUNC_skb_pull_data:
6379 return &bpf_skb_pull_data_proto;
6380 case BPF_FUNC_csum_diff:
6381 return &bpf_csum_diff_proto;
6382 case BPF_FUNC_csum_update:
6383 return &bpf_csum_update_proto;
6384 case BPF_FUNC_csum_level:
6385 return &bpf_csum_level_proto;
6386 case BPF_FUNC_l3_csum_replace:
6387 return &bpf_l3_csum_replace_proto;
6388 case BPF_FUNC_l4_csum_replace:
6389 return &bpf_l4_csum_replace_proto;
6390 case BPF_FUNC_clone_redirect:
6391 return &bpf_clone_redirect_proto;
6392 case BPF_FUNC_get_cgroup_classid:
6393 return &bpf_get_cgroup_classid_proto;
6394 case BPF_FUNC_skb_vlan_push:
6395 return &bpf_skb_vlan_push_proto;
6396 case BPF_FUNC_skb_vlan_pop:
6397 return &bpf_skb_vlan_pop_proto;
6398 case BPF_FUNC_skb_change_proto:
6399 return &bpf_skb_change_proto_proto;
6400 case BPF_FUNC_skb_change_type:
6401 return &bpf_skb_change_type_proto;
6402 case BPF_FUNC_skb_adjust_room:
6403 return &bpf_skb_adjust_room_proto;
6404 case BPF_FUNC_skb_change_tail:
6405 return &bpf_skb_change_tail_proto;
6406 case BPF_FUNC_skb_change_head:
6407 return &bpf_skb_change_head_proto;
6408 case BPF_FUNC_skb_get_tunnel_key:
6409 return &bpf_skb_get_tunnel_key_proto;
6410 case BPF_FUNC_skb_set_tunnel_key:
6411 return bpf_get_skb_set_tunnel_proto(func_id);
6412 case BPF_FUNC_skb_get_tunnel_opt:
6413 return &bpf_skb_get_tunnel_opt_proto;
6414 case BPF_FUNC_skb_set_tunnel_opt:
6415 return bpf_get_skb_set_tunnel_proto(func_id);
6416 case BPF_FUNC_redirect:
6417 return &bpf_redirect_proto;
6418 case BPF_FUNC_get_route_realm:
6419 return &bpf_get_route_realm_proto;
6420 case BPF_FUNC_get_hash_recalc:
6421 return &bpf_get_hash_recalc_proto;
6422 case BPF_FUNC_set_hash_invalid:
6423 return &bpf_set_hash_invalid_proto;
6424 case BPF_FUNC_set_hash:
6425 return &bpf_set_hash_proto;
6426 case BPF_FUNC_perf_event_output:
6427 return &bpf_skb_event_output_proto;
6428 case BPF_FUNC_get_smp_processor_id:
6429 return &bpf_get_smp_processor_id_proto;
6430 case BPF_FUNC_skb_under_cgroup:
6431 return &bpf_skb_under_cgroup_proto;
6432 case BPF_FUNC_get_socket_cookie:
6433 return &bpf_get_socket_cookie_proto;
6434 case BPF_FUNC_get_socket_uid:
6435 return &bpf_get_socket_uid_proto;
6436 case BPF_FUNC_fib_lookup:
6437 return &bpf_skb_fib_lookup_proto;
6438 case BPF_FUNC_sk_fullsock:
6439 return &bpf_sk_fullsock_proto;
6440 case BPF_FUNC_sk_storage_get:
6441 return &bpf_sk_storage_get_proto;
6442 case BPF_FUNC_sk_storage_delete:
6443 return &bpf_sk_storage_delete_proto;
6445 case BPF_FUNC_skb_get_xfrm_state:
6446 return &bpf_skb_get_xfrm_state_proto;
6448 #ifdef CONFIG_SOCK_CGROUP_DATA
6449 case BPF_FUNC_skb_cgroup_id:
6450 return &bpf_skb_cgroup_id_proto;
6451 case BPF_FUNC_skb_ancestor_cgroup_id:
6452 return &bpf_skb_ancestor_cgroup_id_proto;
6455 case BPF_FUNC_sk_lookup_tcp:
6456 return &bpf_sk_lookup_tcp_proto;
6457 case BPF_FUNC_sk_lookup_udp:
6458 return &bpf_sk_lookup_udp_proto;
6459 case BPF_FUNC_sk_release:
6460 return &bpf_sk_release_proto;
6461 case BPF_FUNC_tcp_sock:
6462 return &bpf_tcp_sock_proto;
6463 case BPF_FUNC_get_listener_sock:
6464 return &bpf_get_listener_sock_proto;
6465 case BPF_FUNC_skc_lookup_tcp:
6466 return &bpf_skc_lookup_tcp_proto;
6467 case BPF_FUNC_tcp_check_syncookie:
6468 return &bpf_tcp_check_syncookie_proto;
6469 case BPF_FUNC_skb_ecn_set_ce:
6470 return &bpf_skb_ecn_set_ce_proto;
6471 case BPF_FUNC_tcp_gen_syncookie:
6472 return &bpf_tcp_gen_syncookie_proto;
6473 case BPF_FUNC_sk_assign:
6474 return &bpf_sk_assign_proto;
6477 return bpf_base_func_proto(func_id);
6481 static const struct bpf_func_proto *
6482 xdp_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6485 case BPF_FUNC_perf_event_output:
6486 return &bpf_xdp_event_output_proto;
6487 case BPF_FUNC_get_smp_processor_id:
6488 return &bpf_get_smp_processor_id_proto;
6489 case BPF_FUNC_csum_diff:
6490 return &bpf_csum_diff_proto;
6491 case BPF_FUNC_xdp_adjust_head:
6492 return &bpf_xdp_adjust_head_proto;
6493 case BPF_FUNC_xdp_adjust_meta:
6494 return &bpf_xdp_adjust_meta_proto;
6495 case BPF_FUNC_redirect:
6496 return &bpf_xdp_redirect_proto;
6497 case BPF_FUNC_redirect_map:
6498 return &bpf_xdp_redirect_map_proto;
6499 case BPF_FUNC_xdp_adjust_tail:
6500 return &bpf_xdp_adjust_tail_proto;
6501 case BPF_FUNC_fib_lookup:
6502 return &bpf_xdp_fib_lookup_proto;
6504 case BPF_FUNC_sk_lookup_udp:
6505 return &bpf_xdp_sk_lookup_udp_proto;
6506 case BPF_FUNC_sk_lookup_tcp:
6507 return &bpf_xdp_sk_lookup_tcp_proto;
6508 case BPF_FUNC_sk_release:
6509 return &bpf_sk_release_proto;
6510 case BPF_FUNC_skc_lookup_tcp:
6511 return &bpf_xdp_skc_lookup_tcp_proto;
6512 case BPF_FUNC_tcp_check_syncookie:
6513 return &bpf_tcp_check_syncookie_proto;
6514 case BPF_FUNC_tcp_gen_syncookie:
6515 return &bpf_tcp_gen_syncookie_proto;
6518 return bpf_base_func_proto(func_id);
6522 const struct bpf_func_proto bpf_sock_map_update_proto __weak;
6523 const struct bpf_func_proto bpf_sock_hash_update_proto __weak;
6525 static const struct bpf_func_proto *
6526 sock_ops_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6529 case BPF_FUNC_setsockopt:
6530 return &bpf_sock_ops_setsockopt_proto;
6531 case BPF_FUNC_getsockopt:
6532 return &bpf_sock_ops_getsockopt_proto;
6533 case BPF_FUNC_sock_ops_cb_flags_set:
6534 return &bpf_sock_ops_cb_flags_set_proto;
6535 case BPF_FUNC_sock_map_update:
6536 return &bpf_sock_map_update_proto;
6537 case BPF_FUNC_sock_hash_update:
6538 return &bpf_sock_hash_update_proto;
6539 case BPF_FUNC_get_socket_cookie:
6540 return &bpf_get_socket_cookie_sock_ops_proto;
6541 case BPF_FUNC_get_local_storage:
6542 return &bpf_get_local_storage_proto;
6543 case BPF_FUNC_perf_event_output:
6544 return &bpf_event_output_data_proto;
6545 case BPF_FUNC_sk_storage_get:
6546 return &bpf_sk_storage_get_proto;
6547 case BPF_FUNC_sk_storage_delete:
6548 return &bpf_sk_storage_delete_proto;
6550 case BPF_FUNC_tcp_sock:
6551 return &bpf_tcp_sock_proto;
6552 #endif /* CONFIG_INET */
6554 return bpf_base_func_proto(func_id);
6558 const struct bpf_func_proto bpf_msg_redirect_map_proto __weak;
6559 const struct bpf_func_proto bpf_msg_redirect_hash_proto __weak;
6561 static const struct bpf_func_proto *
6562 sk_msg_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6565 case BPF_FUNC_msg_redirect_map:
6566 return &bpf_msg_redirect_map_proto;
6567 case BPF_FUNC_msg_redirect_hash:
6568 return &bpf_msg_redirect_hash_proto;
6569 case BPF_FUNC_msg_apply_bytes:
6570 return &bpf_msg_apply_bytes_proto;
6571 case BPF_FUNC_msg_cork_bytes:
6572 return &bpf_msg_cork_bytes_proto;
6573 case BPF_FUNC_msg_pull_data:
6574 return &bpf_msg_pull_data_proto;
6575 case BPF_FUNC_msg_push_data:
6576 return &bpf_msg_push_data_proto;
6577 case BPF_FUNC_msg_pop_data:
6578 return &bpf_msg_pop_data_proto;
6579 case BPF_FUNC_perf_event_output:
6580 return &bpf_event_output_data_proto;
6581 case BPF_FUNC_get_current_uid_gid:
6582 return &bpf_get_current_uid_gid_proto;
6583 case BPF_FUNC_get_current_pid_tgid:
6584 return &bpf_get_current_pid_tgid_proto;
6585 case BPF_FUNC_sk_storage_get:
6586 return &bpf_sk_storage_get_proto;
6587 case BPF_FUNC_sk_storage_delete:
6588 return &bpf_sk_storage_delete_proto;
6589 #ifdef CONFIG_CGROUPS
6590 case BPF_FUNC_get_current_cgroup_id:
6591 return &bpf_get_current_cgroup_id_proto;
6592 case BPF_FUNC_get_current_ancestor_cgroup_id:
6593 return &bpf_get_current_ancestor_cgroup_id_proto;
6595 #ifdef CONFIG_CGROUP_NET_CLASSID
6596 case BPF_FUNC_get_cgroup_classid:
6597 return &bpf_get_cgroup_classid_curr_proto;
6600 return bpf_base_func_proto(func_id);
6604 const struct bpf_func_proto bpf_sk_redirect_map_proto __weak;
6605 const struct bpf_func_proto bpf_sk_redirect_hash_proto __weak;
6607 static const struct bpf_func_proto *
6608 sk_skb_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6611 case BPF_FUNC_skb_store_bytes:
6612 return &bpf_skb_store_bytes_proto;
6613 case BPF_FUNC_skb_load_bytes:
6614 return &bpf_skb_load_bytes_proto;
6615 case BPF_FUNC_skb_pull_data:
6616 return &sk_skb_pull_data_proto;
6617 case BPF_FUNC_skb_change_tail:
6618 return &sk_skb_change_tail_proto;
6619 case BPF_FUNC_skb_change_head:
6620 return &sk_skb_change_head_proto;
6621 case BPF_FUNC_get_socket_cookie:
6622 return &bpf_get_socket_cookie_proto;
6623 case BPF_FUNC_get_socket_uid:
6624 return &bpf_get_socket_uid_proto;
6625 case BPF_FUNC_sk_redirect_map:
6626 return &bpf_sk_redirect_map_proto;
6627 case BPF_FUNC_sk_redirect_hash:
6628 return &bpf_sk_redirect_hash_proto;
6629 case BPF_FUNC_perf_event_output:
6630 return &bpf_skb_event_output_proto;
6632 case BPF_FUNC_sk_lookup_tcp:
6633 return &bpf_sk_lookup_tcp_proto;
6634 case BPF_FUNC_sk_lookup_udp:
6635 return &bpf_sk_lookup_udp_proto;
6636 case BPF_FUNC_sk_release:
6637 return &bpf_sk_release_proto;
6638 case BPF_FUNC_skc_lookup_tcp:
6639 return &bpf_skc_lookup_tcp_proto;
6642 return bpf_base_func_proto(func_id);
6646 static const struct bpf_func_proto *
6647 flow_dissector_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6650 case BPF_FUNC_skb_load_bytes:
6651 return &bpf_flow_dissector_load_bytes_proto;
6653 return bpf_base_func_proto(func_id);
6657 static const struct bpf_func_proto *
6658 lwt_out_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6661 case BPF_FUNC_skb_load_bytes:
6662 return &bpf_skb_load_bytes_proto;
6663 case BPF_FUNC_skb_pull_data:
6664 return &bpf_skb_pull_data_proto;
6665 case BPF_FUNC_csum_diff:
6666 return &bpf_csum_diff_proto;
6667 case BPF_FUNC_get_cgroup_classid:
6668 return &bpf_get_cgroup_classid_proto;
6669 case BPF_FUNC_get_route_realm:
6670 return &bpf_get_route_realm_proto;
6671 case BPF_FUNC_get_hash_recalc:
6672 return &bpf_get_hash_recalc_proto;
6673 case BPF_FUNC_perf_event_output:
6674 return &bpf_skb_event_output_proto;
6675 case BPF_FUNC_get_smp_processor_id:
6676 return &bpf_get_smp_processor_id_proto;
6677 case BPF_FUNC_skb_under_cgroup:
6678 return &bpf_skb_under_cgroup_proto;
6680 return bpf_base_func_proto(func_id);
6684 static const struct bpf_func_proto *
6685 lwt_in_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6688 case BPF_FUNC_lwt_push_encap:
6689 return &bpf_lwt_in_push_encap_proto;
6691 return lwt_out_func_proto(func_id, prog);
6695 static const struct bpf_func_proto *
6696 lwt_xmit_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6699 case BPF_FUNC_skb_get_tunnel_key:
6700 return &bpf_skb_get_tunnel_key_proto;
6701 case BPF_FUNC_skb_set_tunnel_key:
6702 return bpf_get_skb_set_tunnel_proto(func_id);
6703 case BPF_FUNC_skb_get_tunnel_opt:
6704 return &bpf_skb_get_tunnel_opt_proto;
6705 case BPF_FUNC_skb_set_tunnel_opt:
6706 return bpf_get_skb_set_tunnel_proto(func_id);
6707 case BPF_FUNC_redirect:
6708 return &bpf_redirect_proto;
6709 case BPF_FUNC_clone_redirect:
6710 return &bpf_clone_redirect_proto;
6711 case BPF_FUNC_skb_change_tail:
6712 return &bpf_skb_change_tail_proto;
6713 case BPF_FUNC_skb_change_head:
6714 return &bpf_skb_change_head_proto;
6715 case BPF_FUNC_skb_store_bytes:
6716 return &bpf_skb_store_bytes_proto;
6717 case BPF_FUNC_csum_update:
6718 return &bpf_csum_update_proto;
6719 case BPF_FUNC_csum_level:
6720 return &bpf_csum_level_proto;
6721 case BPF_FUNC_l3_csum_replace:
6722 return &bpf_l3_csum_replace_proto;
6723 case BPF_FUNC_l4_csum_replace:
6724 return &bpf_l4_csum_replace_proto;
6725 case BPF_FUNC_set_hash_invalid:
6726 return &bpf_set_hash_invalid_proto;
6727 case BPF_FUNC_lwt_push_encap:
6728 return &bpf_lwt_xmit_push_encap_proto;
6730 return lwt_out_func_proto(func_id, prog);
6734 static const struct bpf_func_proto *
6735 lwt_seg6local_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6738 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
6739 case BPF_FUNC_lwt_seg6_store_bytes:
6740 return &bpf_lwt_seg6_store_bytes_proto;
6741 case BPF_FUNC_lwt_seg6_action:
6742 return &bpf_lwt_seg6_action_proto;
6743 case BPF_FUNC_lwt_seg6_adjust_srh:
6744 return &bpf_lwt_seg6_adjust_srh_proto;
6747 return lwt_out_func_proto(func_id, prog);
6751 static bool bpf_skb_is_valid_access(int off, int size, enum bpf_access_type type,
6752 const struct bpf_prog *prog,
6753 struct bpf_insn_access_aux *info)
6755 const int size_default = sizeof(__u32);
6757 if (off < 0 || off >= sizeof(struct __sk_buff))
6760 /* The verifier guarantees that size > 0. */
6761 if (off % size != 0)
6765 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
6766 if (off + size > offsetofend(struct __sk_buff, cb[4]))
6769 case bpf_ctx_range_till(struct __sk_buff, remote_ip6[0], remote_ip6[3]):
6770 case bpf_ctx_range_till(struct __sk_buff, local_ip6[0], local_ip6[3]):
6771 case bpf_ctx_range_till(struct __sk_buff, remote_ip4, remote_ip4):
6772 case bpf_ctx_range_till(struct __sk_buff, local_ip4, local_ip4):
6773 case bpf_ctx_range(struct __sk_buff, data):
6774 case bpf_ctx_range(struct __sk_buff, data_meta):
6775 case bpf_ctx_range(struct __sk_buff, data_end):
6776 if (size != size_default)
6779 case bpf_ctx_range_ptr(struct __sk_buff, flow_keys):
6781 case bpf_ctx_range(struct __sk_buff, tstamp):
6782 if (size != sizeof(__u64))
6785 case offsetof(struct __sk_buff, sk):
6786 if (type == BPF_WRITE || size != sizeof(__u64))
6788 info->reg_type = PTR_TO_SOCK_COMMON_OR_NULL;
6791 /* Only narrow read access allowed for now. */
6792 if (type == BPF_WRITE) {
6793 if (size != size_default)
6796 bpf_ctx_record_field_size(info, size_default);
6797 if (!bpf_ctx_narrow_access_ok(off, size, size_default))
6805 static bool sk_filter_is_valid_access(int off, int size,
6806 enum bpf_access_type type,
6807 const struct bpf_prog *prog,
6808 struct bpf_insn_access_aux *info)
6811 case bpf_ctx_range(struct __sk_buff, tc_classid):
6812 case bpf_ctx_range(struct __sk_buff, data):
6813 case bpf_ctx_range(struct __sk_buff, data_meta):
6814 case bpf_ctx_range(struct __sk_buff, data_end):
6815 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
6816 case bpf_ctx_range(struct __sk_buff, tstamp):
6817 case bpf_ctx_range(struct __sk_buff, wire_len):
6821 if (type == BPF_WRITE) {
6823 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
6830 return bpf_skb_is_valid_access(off, size, type, prog, info);
6833 static bool cg_skb_is_valid_access(int off, int size,
6834 enum bpf_access_type type,
6835 const struct bpf_prog *prog,
6836 struct bpf_insn_access_aux *info)
6839 case bpf_ctx_range(struct __sk_buff, tc_classid):
6840 case bpf_ctx_range(struct __sk_buff, data_meta):
6841 case bpf_ctx_range(struct __sk_buff, wire_len):
6843 case bpf_ctx_range(struct __sk_buff, data):
6844 case bpf_ctx_range(struct __sk_buff, data_end):
6850 if (type == BPF_WRITE) {
6852 case bpf_ctx_range(struct __sk_buff, mark):
6853 case bpf_ctx_range(struct __sk_buff, priority):
6854 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
6856 case bpf_ctx_range(struct __sk_buff, tstamp):
6866 case bpf_ctx_range(struct __sk_buff, data):
6867 info->reg_type = PTR_TO_PACKET;
6869 case bpf_ctx_range(struct __sk_buff, data_end):
6870 info->reg_type = PTR_TO_PACKET_END;
6874 return bpf_skb_is_valid_access(off, size, type, prog, info);
6877 static bool lwt_is_valid_access(int off, int size,
6878 enum bpf_access_type type,
6879 const struct bpf_prog *prog,
6880 struct bpf_insn_access_aux *info)
6883 case bpf_ctx_range(struct __sk_buff, tc_classid):
6884 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
6885 case bpf_ctx_range(struct __sk_buff, data_meta):
6886 case bpf_ctx_range(struct __sk_buff, tstamp):
6887 case bpf_ctx_range(struct __sk_buff, wire_len):
6891 if (type == BPF_WRITE) {
6893 case bpf_ctx_range(struct __sk_buff, mark):
6894 case bpf_ctx_range(struct __sk_buff, priority):
6895 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
6903 case bpf_ctx_range(struct __sk_buff, data):
6904 info->reg_type = PTR_TO_PACKET;
6906 case bpf_ctx_range(struct __sk_buff, data_end):
6907 info->reg_type = PTR_TO_PACKET_END;
6911 return bpf_skb_is_valid_access(off, size, type, prog, info);
6914 /* Attach type specific accesses */
6915 static bool __sock_filter_check_attach_type(int off,
6916 enum bpf_access_type access_type,
6917 enum bpf_attach_type attach_type)
6920 case offsetof(struct bpf_sock, bound_dev_if):
6921 case offsetof(struct bpf_sock, mark):
6922 case offsetof(struct bpf_sock, priority):
6923 switch (attach_type) {
6924 case BPF_CGROUP_INET_SOCK_CREATE:
6925 case BPF_CGROUP_INET_SOCK_RELEASE:
6930 case bpf_ctx_range(struct bpf_sock, src_ip4):
6931 switch (attach_type) {
6932 case BPF_CGROUP_INET4_POST_BIND:
6937 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
6938 switch (attach_type) {
6939 case BPF_CGROUP_INET6_POST_BIND:
6944 case bpf_ctx_range(struct bpf_sock, src_port):
6945 switch (attach_type) {
6946 case BPF_CGROUP_INET4_POST_BIND:
6947 case BPF_CGROUP_INET6_POST_BIND:
6954 return access_type == BPF_READ;
6959 bool bpf_sock_common_is_valid_access(int off, int size,
6960 enum bpf_access_type type,
6961 struct bpf_insn_access_aux *info)
6964 case bpf_ctx_range_till(struct bpf_sock, type, priority):
6967 return bpf_sock_is_valid_access(off, size, type, info);
6971 bool bpf_sock_is_valid_access(int off, int size, enum bpf_access_type type,
6972 struct bpf_insn_access_aux *info)
6974 const int size_default = sizeof(__u32);
6976 if (off < 0 || off >= sizeof(struct bpf_sock))
6978 if (off % size != 0)
6982 case offsetof(struct bpf_sock, state):
6983 case offsetof(struct bpf_sock, family):
6984 case offsetof(struct bpf_sock, type):
6985 case offsetof(struct bpf_sock, protocol):
6986 case offsetof(struct bpf_sock, dst_port):
6987 case offsetof(struct bpf_sock, src_port):
6988 case offsetof(struct bpf_sock, rx_queue_mapping):
6989 case bpf_ctx_range(struct bpf_sock, src_ip4):
6990 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
6991 case bpf_ctx_range(struct bpf_sock, dst_ip4):
6992 case bpf_ctx_range_till(struct bpf_sock, dst_ip6[0], dst_ip6[3]):
6993 bpf_ctx_record_field_size(info, size_default);
6994 return bpf_ctx_narrow_access_ok(off, size, size_default);
6997 return size == size_default;
7000 static bool sock_filter_is_valid_access(int off, int size,
7001 enum bpf_access_type type,
7002 const struct bpf_prog *prog,
7003 struct bpf_insn_access_aux *info)
7005 if (!bpf_sock_is_valid_access(off, size, type, info))
7007 return __sock_filter_check_attach_type(off, type,
7008 prog->expected_attach_type);
7011 static int bpf_noop_prologue(struct bpf_insn *insn_buf, bool direct_write,
7012 const struct bpf_prog *prog)
7014 /* Neither direct read nor direct write requires any preliminary
7020 static int bpf_unclone_prologue(struct bpf_insn *insn_buf, bool direct_write,
7021 const struct bpf_prog *prog, int drop_verdict)
7023 struct bpf_insn *insn = insn_buf;
7028 /* if (!skb->cloned)
7031 * (Fast-path, otherwise approximation that we might be
7032 * a clone, do the rest in helper.)
7034 *insn++ = BPF_LDX_MEM(BPF_B, BPF_REG_6, BPF_REG_1, CLONED_OFFSET());
7035 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_6, CLONED_MASK);
7036 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_6, 0, 7);
7038 /* ret = bpf_skb_pull_data(skb, 0); */
7039 *insn++ = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1);
7040 *insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_2, BPF_REG_2);
7041 *insn++ = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
7042 BPF_FUNC_skb_pull_data);
7045 * return TC_ACT_SHOT;
7047 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2);
7048 *insn++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_0, drop_verdict);
7049 *insn++ = BPF_EXIT_INSN();
7052 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_6);
7054 *insn++ = prog->insnsi[0];
7056 return insn - insn_buf;
7059 static int bpf_gen_ld_abs(const struct bpf_insn *orig,
7060 struct bpf_insn *insn_buf)
7062 bool indirect = BPF_MODE(orig->code) == BPF_IND;
7063 struct bpf_insn *insn = insn_buf;
7065 /* We're guaranteed here that CTX is in R6. */
7066 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_CTX);
7068 *insn++ = BPF_MOV64_IMM(BPF_REG_2, orig->imm);
7070 *insn++ = BPF_MOV64_REG(BPF_REG_2, orig->src_reg);
7072 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, orig->imm);
7075 switch (BPF_SIZE(orig->code)) {
7077 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_8_no_cache);
7080 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_16_no_cache);
7083 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_32_no_cache);
7087 *insn++ = BPF_JMP_IMM(BPF_JSGE, BPF_REG_0, 0, 2);
7088 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_0, BPF_REG_0);
7089 *insn++ = BPF_EXIT_INSN();
7091 return insn - insn_buf;
7094 static int tc_cls_act_prologue(struct bpf_insn *insn_buf, bool direct_write,
7095 const struct bpf_prog *prog)
7097 return bpf_unclone_prologue(insn_buf, direct_write, prog, TC_ACT_SHOT);
7100 static bool tc_cls_act_is_valid_access(int off, int size,
7101 enum bpf_access_type type,
7102 const struct bpf_prog *prog,
7103 struct bpf_insn_access_aux *info)
7105 if (type == BPF_WRITE) {
7107 case bpf_ctx_range(struct __sk_buff, mark):
7108 case bpf_ctx_range(struct __sk_buff, tc_index):
7109 case bpf_ctx_range(struct __sk_buff, priority):
7110 case bpf_ctx_range(struct __sk_buff, tc_classid):
7111 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
7112 case bpf_ctx_range(struct __sk_buff, tstamp):
7113 case bpf_ctx_range(struct __sk_buff, queue_mapping):
7121 case bpf_ctx_range(struct __sk_buff, data):
7122 info->reg_type = PTR_TO_PACKET;
7124 case bpf_ctx_range(struct __sk_buff, data_meta):
7125 info->reg_type = PTR_TO_PACKET_META;
7127 case bpf_ctx_range(struct __sk_buff, data_end):
7128 info->reg_type = PTR_TO_PACKET_END;
7130 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
7134 return bpf_skb_is_valid_access(off, size, type, prog, info);
7137 static bool __is_valid_xdp_access(int off, int size)
7139 if (off < 0 || off >= sizeof(struct xdp_md))
7141 if (off % size != 0)
7143 if (size != sizeof(__u32))
7149 static bool xdp_is_valid_access(int off, int size,
7150 enum bpf_access_type type,
7151 const struct bpf_prog *prog,
7152 struct bpf_insn_access_aux *info)
7154 if (prog->expected_attach_type != BPF_XDP_DEVMAP) {
7156 case offsetof(struct xdp_md, egress_ifindex):
7161 if (type == BPF_WRITE) {
7162 if (bpf_prog_is_dev_bound(prog->aux)) {
7164 case offsetof(struct xdp_md, rx_queue_index):
7165 return __is_valid_xdp_access(off, size);
7172 case offsetof(struct xdp_md, data):
7173 info->reg_type = PTR_TO_PACKET;
7175 case offsetof(struct xdp_md, data_meta):
7176 info->reg_type = PTR_TO_PACKET_META;
7178 case offsetof(struct xdp_md, data_end):
7179 info->reg_type = PTR_TO_PACKET_END;
7183 return __is_valid_xdp_access(off, size);
7186 void bpf_warn_invalid_xdp_action(u32 act)
7188 const u32 act_max = XDP_REDIRECT;
7190 WARN_ONCE(1, "%s XDP return value %u, expect packet loss!\n",
7191 act > act_max ? "Illegal" : "Driver unsupported",
7194 EXPORT_SYMBOL_GPL(bpf_warn_invalid_xdp_action);
7196 static bool sock_addr_is_valid_access(int off, int size,
7197 enum bpf_access_type type,
7198 const struct bpf_prog *prog,
7199 struct bpf_insn_access_aux *info)
7201 const int size_default = sizeof(__u32);
7203 if (off < 0 || off >= sizeof(struct bpf_sock_addr))
7205 if (off % size != 0)
7208 /* Disallow access to IPv6 fields from IPv4 contex and vise
7212 case bpf_ctx_range(struct bpf_sock_addr, user_ip4):
7213 switch (prog->expected_attach_type) {
7214 case BPF_CGROUP_INET4_BIND:
7215 case BPF_CGROUP_INET4_CONNECT:
7216 case BPF_CGROUP_INET4_GETPEERNAME:
7217 case BPF_CGROUP_INET4_GETSOCKNAME:
7218 case BPF_CGROUP_UDP4_SENDMSG:
7219 case BPF_CGROUP_UDP4_RECVMSG:
7225 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
7226 switch (prog->expected_attach_type) {
7227 case BPF_CGROUP_INET6_BIND:
7228 case BPF_CGROUP_INET6_CONNECT:
7229 case BPF_CGROUP_INET6_GETPEERNAME:
7230 case BPF_CGROUP_INET6_GETSOCKNAME:
7231 case BPF_CGROUP_UDP6_SENDMSG:
7232 case BPF_CGROUP_UDP6_RECVMSG:
7238 case bpf_ctx_range(struct bpf_sock_addr, msg_src_ip4):
7239 switch (prog->expected_attach_type) {
7240 case BPF_CGROUP_UDP4_SENDMSG:
7246 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
7248 switch (prog->expected_attach_type) {
7249 case BPF_CGROUP_UDP6_SENDMSG:
7258 case bpf_ctx_range(struct bpf_sock_addr, user_ip4):
7259 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
7260 case bpf_ctx_range(struct bpf_sock_addr, msg_src_ip4):
7261 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
7263 case bpf_ctx_range(struct bpf_sock_addr, user_port):
7264 if (type == BPF_READ) {
7265 bpf_ctx_record_field_size(info, size_default);
7267 if (bpf_ctx_wide_access_ok(off, size,
7268 struct bpf_sock_addr,
7272 if (bpf_ctx_wide_access_ok(off, size,
7273 struct bpf_sock_addr,
7277 if (!bpf_ctx_narrow_access_ok(off, size, size_default))
7280 if (bpf_ctx_wide_access_ok(off, size,
7281 struct bpf_sock_addr,
7285 if (bpf_ctx_wide_access_ok(off, size,
7286 struct bpf_sock_addr,
7290 if (size != size_default)
7294 case offsetof(struct bpf_sock_addr, sk):
7295 if (type != BPF_READ)
7297 if (size != sizeof(__u64))
7299 info->reg_type = PTR_TO_SOCKET;
7302 if (type == BPF_READ) {
7303 if (size != size_default)
7313 static bool sock_ops_is_valid_access(int off, int size,
7314 enum bpf_access_type type,
7315 const struct bpf_prog *prog,
7316 struct bpf_insn_access_aux *info)
7318 const int size_default = sizeof(__u32);
7320 if (off < 0 || off >= sizeof(struct bpf_sock_ops))
7323 /* The verifier guarantees that size > 0. */
7324 if (off % size != 0)
7327 if (type == BPF_WRITE) {
7329 case offsetof(struct bpf_sock_ops, reply):
7330 case offsetof(struct bpf_sock_ops, sk_txhash):
7331 if (size != size_default)
7339 case bpf_ctx_range_till(struct bpf_sock_ops, bytes_received,
7341 if (size != sizeof(__u64))
7344 case offsetof(struct bpf_sock_ops, sk):
7345 if (size != sizeof(__u64))
7347 info->reg_type = PTR_TO_SOCKET_OR_NULL;
7350 if (size != size_default)
7359 static int sk_skb_prologue(struct bpf_insn *insn_buf, bool direct_write,
7360 const struct bpf_prog *prog)
7362 return bpf_unclone_prologue(insn_buf, direct_write, prog, SK_DROP);
7365 static bool sk_skb_is_valid_access(int off, int size,
7366 enum bpf_access_type type,
7367 const struct bpf_prog *prog,
7368 struct bpf_insn_access_aux *info)
7371 case bpf_ctx_range(struct __sk_buff, tc_classid):
7372 case bpf_ctx_range(struct __sk_buff, data_meta):
7373 case bpf_ctx_range(struct __sk_buff, tstamp):
7374 case bpf_ctx_range(struct __sk_buff, wire_len):
7378 if (type == BPF_WRITE) {
7380 case bpf_ctx_range(struct __sk_buff, tc_index):
7381 case bpf_ctx_range(struct __sk_buff, priority):
7389 case bpf_ctx_range(struct __sk_buff, mark):
7391 case bpf_ctx_range(struct __sk_buff, data):
7392 info->reg_type = PTR_TO_PACKET;
7394 case bpf_ctx_range(struct __sk_buff, data_end):
7395 info->reg_type = PTR_TO_PACKET_END;
7399 return bpf_skb_is_valid_access(off, size, type, prog, info);
7402 static bool sk_msg_is_valid_access(int off, int size,
7403 enum bpf_access_type type,
7404 const struct bpf_prog *prog,
7405 struct bpf_insn_access_aux *info)
7407 if (type == BPF_WRITE)
7410 if (off % size != 0)
7414 case offsetof(struct sk_msg_md, data):
7415 info->reg_type = PTR_TO_PACKET;
7416 if (size != sizeof(__u64))
7419 case offsetof(struct sk_msg_md, data_end):
7420 info->reg_type = PTR_TO_PACKET_END;
7421 if (size != sizeof(__u64))
7424 case offsetof(struct sk_msg_md, sk):
7425 if (size != sizeof(__u64))
7427 info->reg_type = PTR_TO_SOCKET;
7429 case bpf_ctx_range(struct sk_msg_md, family):
7430 case bpf_ctx_range(struct sk_msg_md, remote_ip4):
7431 case bpf_ctx_range(struct sk_msg_md, local_ip4):
7432 case bpf_ctx_range_till(struct sk_msg_md, remote_ip6[0], remote_ip6[3]):
7433 case bpf_ctx_range_till(struct sk_msg_md, local_ip6[0], local_ip6[3]):
7434 case bpf_ctx_range(struct sk_msg_md, remote_port):
7435 case bpf_ctx_range(struct sk_msg_md, local_port):
7436 case bpf_ctx_range(struct sk_msg_md, size):
7437 if (size != sizeof(__u32))
7446 static bool flow_dissector_is_valid_access(int off, int size,
7447 enum bpf_access_type type,
7448 const struct bpf_prog *prog,
7449 struct bpf_insn_access_aux *info)
7451 const int size_default = sizeof(__u32);
7453 if (off < 0 || off >= sizeof(struct __sk_buff))
7456 if (type == BPF_WRITE)
7460 case bpf_ctx_range(struct __sk_buff, data):
7461 if (size != size_default)
7463 info->reg_type = PTR_TO_PACKET;
7465 case bpf_ctx_range(struct __sk_buff, data_end):
7466 if (size != size_default)
7468 info->reg_type = PTR_TO_PACKET_END;
7470 case bpf_ctx_range_ptr(struct __sk_buff, flow_keys):
7471 if (size != sizeof(__u64))
7473 info->reg_type = PTR_TO_FLOW_KEYS;
7480 static u32 flow_dissector_convert_ctx_access(enum bpf_access_type type,
7481 const struct bpf_insn *si,
7482 struct bpf_insn *insn_buf,
7483 struct bpf_prog *prog,
7487 struct bpf_insn *insn = insn_buf;
7490 case offsetof(struct __sk_buff, data):
7491 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_flow_dissector, data),
7492 si->dst_reg, si->src_reg,
7493 offsetof(struct bpf_flow_dissector, data));
7496 case offsetof(struct __sk_buff, data_end):
7497 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_flow_dissector, data_end),
7498 si->dst_reg, si->src_reg,
7499 offsetof(struct bpf_flow_dissector, data_end));
7502 case offsetof(struct __sk_buff, flow_keys):
7503 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_flow_dissector, flow_keys),
7504 si->dst_reg, si->src_reg,
7505 offsetof(struct bpf_flow_dissector, flow_keys));
7509 return insn - insn_buf;
7512 static struct bpf_insn *bpf_convert_shinfo_access(const struct bpf_insn *si,
7513 struct bpf_insn *insn)
7515 /* si->dst_reg = skb_shinfo(SKB); */
7516 #ifdef NET_SKBUFF_DATA_USES_OFFSET
7517 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, end),
7518 BPF_REG_AX, si->src_reg,
7519 offsetof(struct sk_buff, end));
7520 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, head),
7521 si->dst_reg, si->src_reg,
7522 offsetof(struct sk_buff, head));
7523 *insn++ = BPF_ALU64_REG(BPF_ADD, si->dst_reg, BPF_REG_AX);
7525 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, end),
7526 si->dst_reg, si->src_reg,
7527 offsetof(struct sk_buff, end));
7533 static u32 bpf_convert_ctx_access(enum bpf_access_type type,
7534 const struct bpf_insn *si,
7535 struct bpf_insn *insn_buf,
7536 struct bpf_prog *prog, u32 *target_size)
7538 struct bpf_insn *insn = insn_buf;
7542 case offsetof(struct __sk_buff, len):
7543 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
7544 bpf_target_off(struct sk_buff, len, 4,
7548 case offsetof(struct __sk_buff, protocol):
7549 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
7550 bpf_target_off(struct sk_buff, protocol, 2,
7554 case offsetof(struct __sk_buff, vlan_proto):
7555 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
7556 bpf_target_off(struct sk_buff, vlan_proto, 2,
7560 case offsetof(struct __sk_buff, priority):
7561 if (type == BPF_WRITE)
7562 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
7563 bpf_target_off(struct sk_buff, priority, 4,
7566 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
7567 bpf_target_off(struct sk_buff, priority, 4,
7571 case offsetof(struct __sk_buff, ingress_ifindex):
7572 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
7573 bpf_target_off(struct sk_buff, skb_iif, 4,
7577 case offsetof(struct __sk_buff, ifindex):
7578 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
7579 si->dst_reg, si->src_reg,
7580 offsetof(struct sk_buff, dev));
7581 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
7582 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7583 bpf_target_off(struct net_device, ifindex, 4,
7587 case offsetof(struct __sk_buff, hash):
7588 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
7589 bpf_target_off(struct sk_buff, hash, 4,
7593 case offsetof(struct __sk_buff, mark):
7594 if (type == BPF_WRITE)
7595 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
7596 bpf_target_off(struct sk_buff, mark, 4,
7599 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
7600 bpf_target_off(struct sk_buff, mark, 4,
7604 case offsetof(struct __sk_buff, pkt_type):
7606 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->src_reg,
7608 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, PKT_TYPE_MAX);
7609 #ifdef __BIG_ENDIAN_BITFIELD
7610 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, 5);
7614 case offsetof(struct __sk_buff, queue_mapping):
7615 if (type == BPF_WRITE) {
7616 *insn++ = BPF_JMP_IMM(BPF_JGE, si->src_reg, NO_QUEUE_MAPPING, 1);
7617 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg, si->src_reg,
7618 bpf_target_off(struct sk_buff,
7622 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
7623 bpf_target_off(struct sk_buff,
7629 case offsetof(struct __sk_buff, vlan_present):
7631 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->src_reg,
7632 PKT_VLAN_PRESENT_OFFSET());
7633 if (PKT_VLAN_PRESENT_BIT)
7634 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, PKT_VLAN_PRESENT_BIT);
7635 if (PKT_VLAN_PRESENT_BIT < 7)
7636 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, 1);
7639 case offsetof(struct __sk_buff, vlan_tci):
7640 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
7641 bpf_target_off(struct sk_buff, vlan_tci, 2,
7645 case offsetof(struct __sk_buff, cb[0]) ...
7646 offsetofend(struct __sk_buff, cb[4]) - 1:
7647 BUILD_BUG_ON(sizeof_field(struct qdisc_skb_cb, data) < 20);
7648 BUILD_BUG_ON((offsetof(struct sk_buff, cb) +
7649 offsetof(struct qdisc_skb_cb, data)) %
7652 prog->cb_access = 1;
7654 off -= offsetof(struct __sk_buff, cb[0]);
7655 off += offsetof(struct sk_buff, cb);
7656 off += offsetof(struct qdisc_skb_cb, data);
7657 if (type == BPF_WRITE)
7658 *insn++ = BPF_STX_MEM(BPF_SIZE(si->code), si->dst_reg,
7661 *insn++ = BPF_LDX_MEM(BPF_SIZE(si->code), si->dst_reg,
7665 case offsetof(struct __sk_buff, tc_classid):
7666 BUILD_BUG_ON(sizeof_field(struct qdisc_skb_cb, tc_classid) != 2);
7669 off -= offsetof(struct __sk_buff, tc_classid);
7670 off += offsetof(struct sk_buff, cb);
7671 off += offsetof(struct qdisc_skb_cb, tc_classid);
7673 if (type == BPF_WRITE)
7674 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg,
7677 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg,
7681 case offsetof(struct __sk_buff, data):
7682 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
7683 si->dst_reg, si->src_reg,
7684 offsetof(struct sk_buff, data));
7687 case offsetof(struct __sk_buff, data_meta):
7689 off -= offsetof(struct __sk_buff, data_meta);
7690 off += offsetof(struct sk_buff, cb);
7691 off += offsetof(struct bpf_skb_data_end, data_meta);
7692 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
7696 case offsetof(struct __sk_buff, data_end):
7698 off -= offsetof(struct __sk_buff, data_end);
7699 off += offsetof(struct sk_buff, cb);
7700 off += offsetof(struct bpf_skb_data_end, data_end);
7701 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
7705 case offsetof(struct __sk_buff, tc_index):
7706 #ifdef CONFIG_NET_SCHED
7707 if (type == BPF_WRITE)
7708 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg, si->src_reg,
7709 bpf_target_off(struct sk_buff, tc_index, 2,
7712 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
7713 bpf_target_off(struct sk_buff, tc_index, 2,
7717 if (type == BPF_WRITE)
7718 *insn++ = BPF_MOV64_REG(si->dst_reg, si->dst_reg);
7720 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
7724 case offsetof(struct __sk_buff, napi_id):
7725 #if defined(CONFIG_NET_RX_BUSY_POLL)
7726 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
7727 bpf_target_off(struct sk_buff, napi_id, 4,
7729 *insn++ = BPF_JMP_IMM(BPF_JGE, si->dst_reg, MIN_NAPI_ID, 1);
7730 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
7733 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
7736 case offsetof(struct __sk_buff, family):
7737 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_family) != 2);
7739 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
7740 si->dst_reg, si->src_reg,
7741 offsetof(struct sk_buff, sk));
7742 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
7743 bpf_target_off(struct sock_common,
7747 case offsetof(struct __sk_buff, remote_ip4):
7748 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_daddr) != 4);
7750 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
7751 si->dst_reg, si->src_reg,
7752 offsetof(struct sk_buff, sk));
7753 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7754 bpf_target_off(struct sock_common,
7758 case offsetof(struct __sk_buff, local_ip4):
7759 BUILD_BUG_ON(sizeof_field(struct sock_common,
7760 skc_rcv_saddr) != 4);
7762 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
7763 si->dst_reg, si->src_reg,
7764 offsetof(struct sk_buff, sk));
7765 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7766 bpf_target_off(struct sock_common,
7770 case offsetof(struct __sk_buff, remote_ip6[0]) ...
7771 offsetof(struct __sk_buff, remote_ip6[3]):
7772 #if IS_ENABLED(CONFIG_IPV6)
7773 BUILD_BUG_ON(sizeof_field(struct sock_common,
7774 skc_v6_daddr.s6_addr32[0]) != 4);
7777 off -= offsetof(struct __sk_buff, remote_ip6[0]);
7779 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
7780 si->dst_reg, si->src_reg,
7781 offsetof(struct sk_buff, sk));
7782 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7783 offsetof(struct sock_common,
7784 skc_v6_daddr.s6_addr32[0]) +
7787 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
7790 case offsetof(struct __sk_buff, local_ip6[0]) ...
7791 offsetof(struct __sk_buff, local_ip6[3]):
7792 #if IS_ENABLED(CONFIG_IPV6)
7793 BUILD_BUG_ON(sizeof_field(struct sock_common,
7794 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
7797 off -= offsetof(struct __sk_buff, local_ip6[0]);
7799 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
7800 si->dst_reg, si->src_reg,
7801 offsetof(struct sk_buff, sk));
7802 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7803 offsetof(struct sock_common,
7804 skc_v6_rcv_saddr.s6_addr32[0]) +
7807 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
7811 case offsetof(struct __sk_buff, remote_port):
7812 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_dport) != 2);
7814 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
7815 si->dst_reg, si->src_reg,
7816 offsetof(struct sk_buff, sk));
7817 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
7818 bpf_target_off(struct sock_common,
7821 #ifndef __BIG_ENDIAN_BITFIELD
7822 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
7826 case offsetof(struct __sk_buff, local_port):
7827 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_num) != 2);
7829 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
7830 si->dst_reg, si->src_reg,
7831 offsetof(struct sk_buff, sk));
7832 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
7833 bpf_target_off(struct sock_common,
7834 skc_num, 2, target_size));
7837 case offsetof(struct __sk_buff, tstamp):
7838 BUILD_BUG_ON(sizeof_field(struct sk_buff, tstamp) != 8);
7840 if (type == BPF_WRITE)
7841 *insn++ = BPF_STX_MEM(BPF_DW,
7842 si->dst_reg, si->src_reg,
7843 bpf_target_off(struct sk_buff,
7847 *insn++ = BPF_LDX_MEM(BPF_DW,
7848 si->dst_reg, si->src_reg,
7849 bpf_target_off(struct sk_buff,
7854 case offsetof(struct __sk_buff, gso_segs):
7855 insn = bpf_convert_shinfo_access(si, insn);
7856 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct skb_shared_info, gso_segs),
7857 si->dst_reg, si->dst_reg,
7858 bpf_target_off(struct skb_shared_info,
7862 case offsetof(struct __sk_buff, gso_size):
7863 insn = bpf_convert_shinfo_access(si, insn);
7864 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct skb_shared_info, gso_size),
7865 si->dst_reg, si->dst_reg,
7866 bpf_target_off(struct skb_shared_info,
7870 case offsetof(struct __sk_buff, wire_len):
7871 BUILD_BUG_ON(sizeof_field(struct qdisc_skb_cb, pkt_len) != 4);
7874 off -= offsetof(struct __sk_buff, wire_len);
7875 off += offsetof(struct sk_buff, cb);
7876 off += offsetof(struct qdisc_skb_cb, pkt_len);
7878 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, off);
7881 case offsetof(struct __sk_buff, sk):
7882 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
7883 si->dst_reg, si->src_reg,
7884 offsetof(struct sk_buff, sk));
7888 return insn - insn_buf;
7891 u32 bpf_sock_convert_ctx_access(enum bpf_access_type type,
7892 const struct bpf_insn *si,
7893 struct bpf_insn *insn_buf,
7894 struct bpf_prog *prog, u32 *target_size)
7896 struct bpf_insn *insn = insn_buf;
7900 case offsetof(struct bpf_sock, bound_dev_if):
7901 BUILD_BUG_ON(sizeof_field(struct sock, sk_bound_dev_if) != 4);
7903 if (type == BPF_WRITE)
7904 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
7905 offsetof(struct sock, sk_bound_dev_if));
7907 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
7908 offsetof(struct sock, sk_bound_dev_if));
7911 case offsetof(struct bpf_sock, mark):
7912 BUILD_BUG_ON(sizeof_field(struct sock, sk_mark) != 4);
7914 if (type == BPF_WRITE)
7915 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
7916 offsetof(struct sock, sk_mark));
7918 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
7919 offsetof(struct sock, sk_mark));
7922 case offsetof(struct bpf_sock, priority):
7923 BUILD_BUG_ON(sizeof_field(struct sock, sk_priority) != 4);
7925 if (type == BPF_WRITE)
7926 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
7927 offsetof(struct sock, sk_priority));
7929 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
7930 offsetof(struct sock, sk_priority));
7933 case offsetof(struct bpf_sock, family):
7934 *insn++ = BPF_LDX_MEM(
7935 BPF_FIELD_SIZEOF(struct sock_common, skc_family),
7936 si->dst_reg, si->src_reg,
7937 bpf_target_off(struct sock_common,
7939 sizeof_field(struct sock_common,
7944 case offsetof(struct bpf_sock, type):
7945 *insn++ = BPF_LDX_MEM(
7946 BPF_FIELD_SIZEOF(struct sock, sk_type),
7947 si->dst_reg, si->src_reg,
7948 bpf_target_off(struct sock, sk_type,
7949 sizeof_field(struct sock, sk_type),
7953 case offsetof(struct bpf_sock, protocol):
7954 *insn++ = BPF_LDX_MEM(
7955 BPF_FIELD_SIZEOF(struct sock, sk_protocol),
7956 si->dst_reg, si->src_reg,
7957 bpf_target_off(struct sock, sk_protocol,
7958 sizeof_field(struct sock, sk_protocol),
7962 case offsetof(struct bpf_sock, src_ip4):
7963 *insn++ = BPF_LDX_MEM(
7964 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
7965 bpf_target_off(struct sock_common, skc_rcv_saddr,
7966 sizeof_field(struct sock_common,
7971 case offsetof(struct bpf_sock, dst_ip4):
7972 *insn++ = BPF_LDX_MEM(
7973 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
7974 bpf_target_off(struct sock_common, skc_daddr,
7975 sizeof_field(struct sock_common,
7980 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
7981 #if IS_ENABLED(CONFIG_IPV6)
7983 off -= offsetof(struct bpf_sock, src_ip6[0]);
7984 *insn++ = BPF_LDX_MEM(
7985 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
7988 skc_v6_rcv_saddr.s6_addr32[0],
7989 sizeof_field(struct sock_common,
7990 skc_v6_rcv_saddr.s6_addr32[0]),
7991 target_size) + off);
7994 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
7998 case bpf_ctx_range_till(struct bpf_sock, dst_ip6[0], dst_ip6[3]):
7999 #if IS_ENABLED(CONFIG_IPV6)
8001 off -= offsetof(struct bpf_sock, dst_ip6[0]);
8002 *insn++ = BPF_LDX_MEM(
8003 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
8004 bpf_target_off(struct sock_common,
8005 skc_v6_daddr.s6_addr32[0],
8006 sizeof_field(struct sock_common,
8007 skc_v6_daddr.s6_addr32[0]),
8008 target_size) + off);
8010 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
8015 case offsetof(struct bpf_sock, src_port):
8016 *insn++ = BPF_LDX_MEM(
8017 BPF_FIELD_SIZEOF(struct sock_common, skc_num),
8018 si->dst_reg, si->src_reg,
8019 bpf_target_off(struct sock_common, skc_num,
8020 sizeof_field(struct sock_common,
8025 case offsetof(struct bpf_sock, dst_port):
8026 *insn++ = BPF_LDX_MEM(
8027 BPF_FIELD_SIZEOF(struct sock_common, skc_dport),
8028 si->dst_reg, si->src_reg,
8029 bpf_target_off(struct sock_common, skc_dport,
8030 sizeof_field(struct sock_common,
8035 case offsetof(struct bpf_sock, state):
8036 *insn++ = BPF_LDX_MEM(
8037 BPF_FIELD_SIZEOF(struct sock_common, skc_state),
8038 si->dst_reg, si->src_reg,
8039 bpf_target_off(struct sock_common, skc_state,
8040 sizeof_field(struct sock_common,
8044 case offsetof(struct bpf_sock, rx_queue_mapping):
8046 *insn++ = BPF_LDX_MEM(
8047 BPF_FIELD_SIZEOF(struct sock, sk_rx_queue_mapping),
8048 si->dst_reg, si->src_reg,
8049 bpf_target_off(struct sock, sk_rx_queue_mapping,
8050 sizeof_field(struct sock,
8051 sk_rx_queue_mapping),
8053 *insn++ = BPF_JMP_IMM(BPF_JNE, si->dst_reg, NO_QUEUE_MAPPING,
8055 *insn++ = BPF_MOV64_IMM(si->dst_reg, -1);
8057 *insn++ = BPF_MOV64_IMM(si->dst_reg, -1);
8063 return insn - insn_buf;
8066 static u32 tc_cls_act_convert_ctx_access(enum bpf_access_type type,
8067 const struct bpf_insn *si,
8068 struct bpf_insn *insn_buf,
8069 struct bpf_prog *prog, u32 *target_size)
8071 struct bpf_insn *insn = insn_buf;
8074 case offsetof(struct __sk_buff, ifindex):
8075 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
8076 si->dst_reg, si->src_reg,
8077 offsetof(struct sk_buff, dev));
8078 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
8079 bpf_target_off(struct net_device, ifindex, 4,
8083 return bpf_convert_ctx_access(type, si, insn_buf, prog,
8087 return insn - insn_buf;
8090 static u32 xdp_convert_ctx_access(enum bpf_access_type type,
8091 const struct bpf_insn *si,
8092 struct bpf_insn *insn_buf,
8093 struct bpf_prog *prog, u32 *target_size)
8095 struct bpf_insn *insn = insn_buf;
8098 case offsetof(struct xdp_md, data):
8099 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data),
8100 si->dst_reg, si->src_reg,
8101 offsetof(struct xdp_buff, data));
8103 case offsetof(struct xdp_md, data_meta):
8104 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_meta),
8105 si->dst_reg, si->src_reg,
8106 offsetof(struct xdp_buff, data_meta));
8108 case offsetof(struct xdp_md, data_end):
8109 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_end),
8110 si->dst_reg, si->src_reg,
8111 offsetof(struct xdp_buff, data_end));
8113 case offsetof(struct xdp_md, ingress_ifindex):
8114 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, rxq),
8115 si->dst_reg, si->src_reg,
8116 offsetof(struct xdp_buff, rxq));
8117 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_rxq_info, dev),
8118 si->dst_reg, si->dst_reg,
8119 offsetof(struct xdp_rxq_info, dev));
8120 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
8121 offsetof(struct net_device, ifindex));
8123 case offsetof(struct xdp_md, rx_queue_index):
8124 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, rxq),
8125 si->dst_reg, si->src_reg,
8126 offsetof(struct xdp_buff, rxq));
8127 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
8128 offsetof(struct xdp_rxq_info,
8131 case offsetof(struct xdp_md, egress_ifindex):
8132 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, txq),
8133 si->dst_reg, si->src_reg,
8134 offsetof(struct xdp_buff, txq));
8135 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_txq_info, dev),
8136 si->dst_reg, si->dst_reg,
8137 offsetof(struct xdp_txq_info, dev));
8138 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
8139 offsetof(struct net_device, ifindex));
8143 return insn - insn_buf;
8146 /* SOCK_ADDR_LOAD_NESTED_FIELD() loads Nested Field S.F.NF where S is type of
8147 * context Structure, F is Field in context structure that contains a pointer
8148 * to Nested Structure of type NS that has the field NF.
8150 * SIZE encodes the load size (BPF_B, BPF_H, etc). It's up to caller to make
8151 * sure that SIZE is not greater than actual size of S.F.NF.
8153 * If offset OFF is provided, the load happens from that offset relative to
8156 #define SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, SIZE, OFF) \
8158 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), si->dst_reg, \
8159 si->src_reg, offsetof(S, F)); \
8160 *insn++ = BPF_LDX_MEM( \
8161 SIZE, si->dst_reg, si->dst_reg, \
8162 bpf_target_off(NS, NF, sizeof_field(NS, NF), \
8167 #define SOCK_ADDR_LOAD_NESTED_FIELD(S, NS, F, NF) \
8168 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, \
8169 BPF_FIELD_SIZEOF(NS, NF), 0)
8171 /* SOCK_ADDR_STORE_NESTED_FIELD_OFF() has semantic similar to
8172 * SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF() but for store operation.
8174 * In addition it uses Temporary Field TF (member of struct S) as the 3rd
8175 * "register" since two registers available in convert_ctx_access are not
8176 * enough: we can't override neither SRC, since it contains value to store, nor
8177 * DST since it contains pointer to context that may be used by later
8178 * instructions. But we need a temporary place to save pointer to nested
8179 * structure whose field we want to store to.
8181 #define SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, SIZE, OFF, TF) \
8183 int tmp_reg = BPF_REG_9; \
8184 if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg) \
8186 if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg) \
8188 *insn++ = BPF_STX_MEM(BPF_DW, si->dst_reg, tmp_reg, \
8190 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), tmp_reg, \
8191 si->dst_reg, offsetof(S, F)); \
8192 *insn++ = BPF_STX_MEM(SIZE, tmp_reg, si->src_reg, \
8193 bpf_target_off(NS, NF, sizeof_field(NS, NF), \
8196 *insn++ = BPF_LDX_MEM(BPF_DW, tmp_reg, si->dst_reg, \
8200 #define SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, SIZE, OFF, \
8203 if (type == BPF_WRITE) { \
8204 SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, SIZE, \
8207 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF( \
8208 S, NS, F, NF, SIZE, OFF); \
8212 #define SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD(S, NS, F, NF, TF) \
8213 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF( \
8214 S, NS, F, NF, BPF_FIELD_SIZEOF(NS, NF), 0, TF)
8216 static u32 sock_addr_convert_ctx_access(enum bpf_access_type type,
8217 const struct bpf_insn *si,
8218 struct bpf_insn *insn_buf,
8219 struct bpf_prog *prog, u32 *target_size)
8221 int off, port_size = sizeof_field(struct sockaddr_in6, sin6_port);
8222 struct bpf_insn *insn = insn_buf;
8225 case offsetof(struct bpf_sock_addr, user_family):
8226 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
8227 struct sockaddr, uaddr, sa_family);
8230 case offsetof(struct bpf_sock_addr, user_ip4):
8231 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
8232 struct bpf_sock_addr_kern, struct sockaddr_in, uaddr,
8233 sin_addr, BPF_SIZE(si->code), 0, tmp_reg);
8236 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
8238 off -= offsetof(struct bpf_sock_addr, user_ip6[0]);
8239 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
8240 struct bpf_sock_addr_kern, struct sockaddr_in6, uaddr,
8241 sin6_addr.s6_addr32[0], BPF_SIZE(si->code), off,
8245 case offsetof(struct bpf_sock_addr, user_port):
8246 /* To get port we need to know sa_family first and then treat
8247 * sockaddr as either sockaddr_in or sockaddr_in6.
8248 * Though we can simplify since port field has same offset and
8249 * size in both structures.
8250 * Here we check this invariant and use just one of the
8251 * structures if it's true.
8253 BUILD_BUG_ON(offsetof(struct sockaddr_in, sin_port) !=
8254 offsetof(struct sockaddr_in6, sin6_port));
8255 BUILD_BUG_ON(sizeof_field(struct sockaddr_in, sin_port) !=
8256 sizeof_field(struct sockaddr_in6, sin6_port));
8257 /* Account for sin6_port being smaller than user_port. */
8258 port_size = min(port_size, BPF_LDST_BYTES(si));
8259 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
8260 struct bpf_sock_addr_kern, struct sockaddr_in6, uaddr,
8261 sin6_port, bytes_to_bpf_size(port_size), 0, tmp_reg);
8264 case offsetof(struct bpf_sock_addr, family):
8265 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
8266 struct sock, sk, sk_family);
8269 case offsetof(struct bpf_sock_addr, type):
8270 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
8271 struct sock, sk, sk_type);
8274 case offsetof(struct bpf_sock_addr, protocol):
8275 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
8276 struct sock, sk, sk_protocol);
8279 case offsetof(struct bpf_sock_addr, msg_src_ip4):
8280 /* Treat t_ctx as struct in_addr for msg_src_ip4. */
8281 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
8282 struct bpf_sock_addr_kern, struct in_addr, t_ctx,
8283 s_addr, BPF_SIZE(si->code), 0, tmp_reg);
8286 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
8289 off -= offsetof(struct bpf_sock_addr, msg_src_ip6[0]);
8290 /* Treat t_ctx as struct in6_addr for msg_src_ip6. */
8291 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
8292 struct bpf_sock_addr_kern, struct in6_addr, t_ctx,
8293 s6_addr32[0], BPF_SIZE(si->code), off, tmp_reg);
8295 case offsetof(struct bpf_sock_addr, sk):
8296 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_addr_kern, sk),
8297 si->dst_reg, si->src_reg,
8298 offsetof(struct bpf_sock_addr_kern, sk));
8302 return insn - insn_buf;
8305 static u32 sock_ops_convert_ctx_access(enum bpf_access_type type,
8306 const struct bpf_insn *si,
8307 struct bpf_insn *insn_buf,
8308 struct bpf_prog *prog,
8311 struct bpf_insn *insn = insn_buf;
8314 /* Helper macro for adding read access to tcp_sock or sock fields. */
8315 #define SOCK_OPS_GET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ) \
8317 BUILD_BUG_ON(sizeof_field(OBJ, OBJ_FIELD) > \
8318 sizeof_field(struct bpf_sock_ops, BPF_FIELD)); \
8319 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
8320 struct bpf_sock_ops_kern, \
8322 si->dst_reg, si->src_reg, \
8323 offsetof(struct bpf_sock_ops_kern, \
8325 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 2); \
8326 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
8327 struct bpf_sock_ops_kern, sk),\
8328 si->dst_reg, si->src_reg, \
8329 offsetof(struct bpf_sock_ops_kern, sk));\
8330 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(OBJ, \
8332 si->dst_reg, si->dst_reg, \
8333 offsetof(OBJ, OBJ_FIELD)); \
8336 #define SOCK_OPS_GET_TCP_SOCK_FIELD(FIELD) \
8337 SOCK_OPS_GET_FIELD(FIELD, FIELD, struct tcp_sock)
8339 /* Helper macro for adding write access to tcp_sock or sock fields.
8340 * The macro is called with two registers, dst_reg which contains a pointer
8341 * to ctx (context) and src_reg which contains the value that should be
8342 * stored. However, we need an additional register since we cannot overwrite
8343 * dst_reg because it may be used later in the program.
8344 * Instead we "borrow" one of the other register. We first save its value
8345 * into a new (temp) field in bpf_sock_ops_kern, use it, and then restore
8346 * it at the end of the macro.
8348 #define SOCK_OPS_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ) \
8350 int reg = BPF_REG_9; \
8351 BUILD_BUG_ON(sizeof_field(OBJ, OBJ_FIELD) > \
8352 sizeof_field(struct bpf_sock_ops, BPF_FIELD)); \
8353 if (si->dst_reg == reg || si->src_reg == reg) \
8355 if (si->dst_reg == reg || si->src_reg == reg) \
8357 *insn++ = BPF_STX_MEM(BPF_DW, si->dst_reg, reg, \
8358 offsetof(struct bpf_sock_ops_kern, \
8360 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
8361 struct bpf_sock_ops_kern, \
8364 offsetof(struct bpf_sock_ops_kern, \
8366 *insn++ = BPF_JMP_IMM(BPF_JEQ, reg, 0, 2); \
8367 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
8368 struct bpf_sock_ops_kern, sk),\
8370 offsetof(struct bpf_sock_ops_kern, sk));\
8371 *insn++ = BPF_STX_MEM(BPF_FIELD_SIZEOF(OBJ, OBJ_FIELD), \
8373 offsetof(OBJ, OBJ_FIELD)); \
8374 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->dst_reg, \
8375 offsetof(struct bpf_sock_ops_kern, \
8379 #define SOCK_OPS_GET_OR_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ, TYPE) \
8381 if (TYPE == BPF_WRITE) \
8382 SOCK_OPS_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ); \
8384 SOCK_OPS_GET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ); \
8387 if (insn > insn_buf)
8388 return insn - insn_buf;
8391 case offsetof(struct bpf_sock_ops, op) ...
8392 offsetof(struct bpf_sock_ops, replylong[3]):
8393 BUILD_BUG_ON(sizeof_field(struct bpf_sock_ops, op) !=
8394 sizeof_field(struct bpf_sock_ops_kern, op));
8395 BUILD_BUG_ON(sizeof_field(struct bpf_sock_ops, reply) !=
8396 sizeof_field(struct bpf_sock_ops_kern, reply));
8397 BUILD_BUG_ON(sizeof_field(struct bpf_sock_ops, replylong) !=
8398 sizeof_field(struct bpf_sock_ops_kern, replylong));
8400 off -= offsetof(struct bpf_sock_ops, op);
8401 off += offsetof(struct bpf_sock_ops_kern, op);
8402 if (type == BPF_WRITE)
8403 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
8406 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
8410 case offsetof(struct bpf_sock_ops, family):
8411 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_family) != 2);
8413 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8414 struct bpf_sock_ops_kern, sk),
8415 si->dst_reg, si->src_reg,
8416 offsetof(struct bpf_sock_ops_kern, sk));
8417 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
8418 offsetof(struct sock_common, skc_family));
8421 case offsetof(struct bpf_sock_ops, remote_ip4):
8422 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_daddr) != 4);
8424 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8425 struct bpf_sock_ops_kern, sk),
8426 si->dst_reg, si->src_reg,
8427 offsetof(struct bpf_sock_ops_kern, sk));
8428 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
8429 offsetof(struct sock_common, skc_daddr));
8432 case offsetof(struct bpf_sock_ops, local_ip4):
8433 BUILD_BUG_ON(sizeof_field(struct sock_common,
8434 skc_rcv_saddr) != 4);
8436 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8437 struct bpf_sock_ops_kern, sk),
8438 si->dst_reg, si->src_reg,
8439 offsetof(struct bpf_sock_ops_kern, sk));
8440 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
8441 offsetof(struct sock_common,
8445 case offsetof(struct bpf_sock_ops, remote_ip6[0]) ...
8446 offsetof(struct bpf_sock_ops, remote_ip6[3]):
8447 #if IS_ENABLED(CONFIG_IPV6)
8448 BUILD_BUG_ON(sizeof_field(struct sock_common,
8449 skc_v6_daddr.s6_addr32[0]) != 4);
8452 off -= offsetof(struct bpf_sock_ops, remote_ip6[0]);
8453 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8454 struct bpf_sock_ops_kern, sk),
8455 si->dst_reg, si->src_reg,
8456 offsetof(struct bpf_sock_ops_kern, sk));
8457 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
8458 offsetof(struct sock_common,
8459 skc_v6_daddr.s6_addr32[0]) +
8462 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
8466 case offsetof(struct bpf_sock_ops, local_ip6[0]) ...
8467 offsetof(struct bpf_sock_ops, local_ip6[3]):
8468 #if IS_ENABLED(CONFIG_IPV6)
8469 BUILD_BUG_ON(sizeof_field(struct sock_common,
8470 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
8473 off -= offsetof(struct bpf_sock_ops, local_ip6[0]);
8474 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8475 struct bpf_sock_ops_kern, sk),
8476 si->dst_reg, si->src_reg,
8477 offsetof(struct bpf_sock_ops_kern, sk));
8478 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
8479 offsetof(struct sock_common,
8480 skc_v6_rcv_saddr.s6_addr32[0]) +
8483 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
8487 case offsetof(struct bpf_sock_ops, remote_port):
8488 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_dport) != 2);
8490 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8491 struct bpf_sock_ops_kern, sk),
8492 si->dst_reg, si->src_reg,
8493 offsetof(struct bpf_sock_ops_kern, sk));
8494 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
8495 offsetof(struct sock_common, skc_dport));
8496 #ifndef __BIG_ENDIAN_BITFIELD
8497 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
8501 case offsetof(struct bpf_sock_ops, local_port):
8502 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_num) != 2);
8504 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8505 struct bpf_sock_ops_kern, sk),
8506 si->dst_reg, si->src_reg,
8507 offsetof(struct bpf_sock_ops_kern, sk));
8508 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
8509 offsetof(struct sock_common, skc_num));
8512 case offsetof(struct bpf_sock_ops, is_fullsock):
8513 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8514 struct bpf_sock_ops_kern,
8516 si->dst_reg, si->src_reg,
8517 offsetof(struct bpf_sock_ops_kern,
8521 case offsetof(struct bpf_sock_ops, state):
8522 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_state) != 1);
8524 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8525 struct bpf_sock_ops_kern, sk),
8526 si->dst_reg, si->src_reg,
8527 offsetof(struct bpf_sock_ops_kern, sk));
8528 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->dst_reg,
8529 offsetof(struct sock_common, skc_state));
8532 case offsetof(struct bpf_sock_ops, rtt_min):
8533 BUILD_BUG_ON(sizeof_field(struct tcp_sock, rtt_min) !=
8534 sizeof(struct minmax));
8535 BUILD_BUG_ON(sizeof(struct minmax) <
8536 sizeof(struct minmax_sample));
8538 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8539 struct bpf_sock_ops_kern, sk),
8540 si->dst_reg, si->src_reg,
8541 offsetof(struct bpf_sock_ops_kern, sk));
8542 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
8543 offsetof(struct tcp_sock, rtt_min) +
8544 sizeof_field(struct minmax_sample, t));
8547 case offsetof(struct bpf_sock_ops, bpf_sock_ops_cb_flags):
8548 SOCK_OPS_GET_FIELD(bpf_sock_ops_cb_flags, bpf_sock_ops_cb_flags,
8552 case offsetof(struct bpf_sock_ops, sk_txhash):
8553 SOCK_OPS_GET_OR_SET_FIELD(sk_txhash, sk_txhash,
8556 case offsetof(struct bpf_sock_ops, snd_cwnd):
8557 SOCK_OPS_GET_TCP_SOCK_FIELD(snd_cwnd);
8559 case offsetof(struct bpf_sock_ops, srtt_us):
8560 SOCK_OPS_GET_TCP_SOCK_FIELD(srtt_us);
8562 case offsetof(struct bpf_sock_ops, snd_ssthresh):
8563 SOCK_OPS_GET_TCP_SOCK_FIELD(snd_ssthresh);
8565 case offsetof(struct bpf_sock_ops, rcv_nxt):
8566 SOCK_OPS_GET_TCP_SOCK_FIELD(rcv_nxt);
8568 case offsetof(struct bpf_sock_ops, snd_nxt):
8569 SOCK_OPS_GET_TCP_SOCK_FIELD(snd_nxt);
8571 case offsetof(struct bpf_sock_ops, snd_una):
8572 SOCK_OPS_GET_TCP_SOCK_FIELD(snd_una);
8574 case offsetof(struct bpf_sock_ops, mss_cache):
8575 SOCK_OPS_GET_TCP_SOCK_FIELD(mss_cache);
8577 case offsetof(struct bpf_sock_ops, ecn_flags):
8578 SOCK_OPS_GET_TCP_SOCK_FIELD(ecn_flags);
8580 case offsetof(struct bpf_sock_ops, rate_delivered):
8581 SOCK_OPS_GET_TCP_SOCK_FIELD(rate_delivered);
8583 case offsetof(struct bpf_sock_ops, rate_interval_us):
8584 SOCK_OPS_GET_TCP_SOCK_FIELD(rate_interval_us);
8586 case offsetof(struct bpf_sock_ops, packets_out):
8587 SOCK_OPS_GET_TCP_SOCK_FIELD(packets_out);
8589 case offsetof(struct bpf_sock_ops, retrans_out):
8590 SOCK_OPS_GET_TCP_SOCK_FIELD(retrans_out);
8592 case offsetof(struct bpf_sock_ops, total_retrans):
8593 SOCK_OPS_GET_TCP_SOCK_FIELD(total_retrans);
8595 case offsetof(struct bpf_sock_ops, segs_in):
8596 SOCK_OPS_GET_TCP_SOCK_FIELD(segs_in);
8598 case offsetof(struct bpf_sock_ops, data_segs_in):
8599 SOCK_OPS_GET_TCP_SOCK_FIELD(data_segs_in);
8601 case offsetof(struct bpf_sock_ops, segs_out):
8602 SOCK_OPS_GET_TCP_SOCK_FIELD(segs_out);
8604 case offsetof(struct bpf_sock_ops, data_segs_out):
8605 SOCK_OPS_GET_TCP_SOCK_FIELD(data_segs_out);
8607 case offsetof(struct bpf_sock_ops, lost_out):
8608 SOCK_OPS_GET_TCP_SOCK_FIELD(lost_out);
8610 case offsetof(struct bpf_sock_ops, sacked_out):
8611 SOCK_OPS_GET_TCP_SOCK_FIELD(sacked_out);
8613 case offsetof(struct bpf_sock_ops, bytes_received):
8614 SOCK_OPS_GET_TCP_SOCK_FIELD(bytes_received);
8616 case offsetof(struct bpf_sock_ops, bytes_acked):
8617 SOCK_OPS_GET_TCP_SOCK_FIELD(bytes_acked);
8619 case offsetof(struct bpf_sock_ops, sk):
8620 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8621 struct bpf_sock_ops_kern,
8623 si->dst_reg, si->src_reg,
8624 offsetof(struct bpf_sock_ops_kern,
8626 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
8627 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8628 struct bpf_sock_ops_kern, sk),
8629 si->dst_reg, si->src_reg,
8630 offsetof(struct bpf_sock_ops_kern, sk));
8633 return insn - insn_buf;
8636 static u32 sk_skb_convert_ctx_access(enum bpf_access_type type,
8637 const struct bpf_insn *si,
8638 struct bpf_insn *insn_buf,
8639 struct bpf_prog *prog, u32 *target_size)
8641 struct bpf_insn *insn = insn_buf;
8645 case offsetof(struct __sk_buff, data_end):
8647 off -= offsetof(struct __sk_buff, data_end);
8648 off += offsetof(struct sk_buff, cb);
8649 off += offsetof(struct tcp_skb_cb, bpf.data_end);
8650 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
8654 return bpf_convert_ctx_access(type, si, insn_buf, prog,
8658 return insn - insn_buf;
8661 static u32 sk_msg_convert_ctx_access(enum bpf_access_type type,
8662 const struct bpf_insn *si,
8663 struct bpf_insn *insn_buf,
8664 struct bpf_prog *prog, u32 *target_size)
8666 struct bpf_insn *insn = insn_buf;
8667 #if IS_ENABLED(CONFIG_IPV6)
8671 /* convert ctx uses the fact sg element is first in struct */
8672 BUILD_BUG_ON(offsetof(struct sk_msg, sg) != 0);
8675 case offsetof(struct sk_msg_md, data):
8676 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg, data),
8677 si->dst_reg, si->src_reg,
8678 offsetof(struct sk_msg, data));
8680 case offsetof(struct sk_msg_md, data_end):
8681 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg, data_end),
8682 si->dst_reg, si->src_reg,
8683 offsetof(struct sk_msg, data_end));
8685 case offsetof(struct sk_msg_md, family):
8686 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_family) != 2);
8688 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8690 si->dst_reg, si->src_reg,
8691 offsetof(struct sk_msg, sk));
8692 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
8693 offsetof(struct sock_common, skc_family));
8696 case offsetof(struct sk_msg_md, remote_ip4):
8697 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_daddr) != 4);
8699 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8701 si->dst_reg, si->src_reg,
8702 offsetof(struct sk_msg, sk));
8703 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
8704 offsetof(struct sock_common, skc_daddr));
8707 case offsetof(struct sk_msg_md, local_ip4):
8708 BUILD_BUG_ON(sizeof_field(struct sock_common,
8709 skc_rcv_saddr) != 4);
8711 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8713 si->dst_reg, si->src_reg,
8714 offsetof(struct sk_msg, sk));
8715 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
8716 offsetof(struct sock_common,
8720 case offsetof(struct sk_msg_md, remote_ip6[0]) ...
8721 offsetof(struct sk_msg_md, remote_ip6[3]):
8722 #if IS_ENABLED(CONFIG_IPV6)
8723 BUILD_BUG_ON(sizeof_field(struct sock_common,
8724 skc_v6_daddr.s6_addr32[0]) != 4);
8727 off -= offsetof(struct sk_msg_md, remote_ip6[0]);
8728 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8730 si->dst_reg, si->src_reg,
8731 offsetof(struct sk_msg, sk));
8732 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
8733 offsetof(struct sock_common,
8734 skc_v6_daddr.s6_addr32[0]) +
8737 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
8741 case offsetof(struct sk_msg_md, local_ip6[0]) ...
8742 offsetof(struct sk_msg_md, local_ip6[3]):
8743 #if IS_ENABLED(CONFIG_IPV6)
8744 BUILD_BUG_ON(sizeof_field(struct sock_common,
8745 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
8748 off -= offsetof(struct sk_msg_md, local_ip6[0]);
8749 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8751 si->dst_reg, si->src_reg,
8752 offsetof(struct sk_msg, sk));
8753 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
8754 offsetof(struct sock_common,
8755 skc_v6_rcv_saddr.s6_addr32[0]) +
8758 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
8762 case offsetof(struct sk_msg_md, remote_port):
8763 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_dport) != 2);
8765 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8767 si->dst_reg, si->src_reg,
8768 offsetof(struct sk_msg, sk));
8769 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
8770 offsetof(struct sock_common, skc_dport));
8771 #ifndef __BIG_ENDIAN_BITFIELD
8772 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
8776 case offsetof(struct sk_msg_md, local_port):
8777 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_num) != 2);
8779 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8781 si->dst_reg, si->src_reg,
8782 offsetof(struct sk_msg, sk));
8783 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
8784 offsetof(struct sock_common, skc_num));
8787 case offsetof(struct sk_msg_md, size):
8788 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg_sg, size),
8789 si->dst_reg, si->src_reg,
8790 offsetof(struct sk_msg_sg, size));
8793 case offsetof(struct sk_msg_md, sk):
8794 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg, sk),
8795 si->dst_reg, si->src_reg,
8796 offsetof(struct sk_msg, sk));
8800 return insn - insn_buf;
8803 const struct bpf_verifier_ops sk_filter_verifier_ops = {
8804 .get_func_proto = sk_filter_func_proto,
8805 .is_valid_access = sk_filter_is_valid_access,
8806 .convert_ctx_access = bpf_convert_ctx_access,
8807 .gen_ld_abs = bpf_gen_ld_abs,
8810 const struct bpf_prog_ops sk_filter_prog_ops = {
8811 .test_run = bpf_prog_test_run_skb,
8814 const struct bpf_verifier_ops tc_cls_act_verifier_ops = {
8815 .get_func_proto = tc_cls_act_func_proto,
8816 .is_valid_access = tc_cls_act_is_valid_access,
8817 .convert_ctx_access = tc_cls_act_convert_ctx_access,
8818 .gen_prologue = tc_cls_act_prologue,
8819 .gen_ld_abs = bpf_gen_ld_abs,
8822 const struct bpf_prog_ops tc_cls_act_prog_ops = {
8823 .test_run = bpf_prog_test_run_skb,
8826 const struct bpf_verifier_ops xdp_verifier_ops = {
8827 .get_func_proto = xdp_func_proto,
8828 .is_valid_access = xdp_is_valid_access,
8829 .convert_ctx_access = xdp_convert_ctx_access,
8830 .gen_prologue = bpf_noop_prologue,
8833 const struct bpf_prog_ops xdp_prog_ops = {
8834 .test_run = bpf_prog_test_run_xdp,
8837 const struct bpf_verifier_ops cg_skb_verifier_ops = {
8838 .get_func_proto = cg_skb_func_proto,
8839 .is_valid_access = cg_skb_is_valid_access,
8840 .convert_ctx_access = bpf_convert_ctx_access,
8843 const struct bpf_prog_ops cg_skb_prog_ops = {
8844 .test_run = bpf_prog_test_run_skb,
8847 const struct bpf_verifier_ops lwt_in_verifier_ops = {
8848 .get_func_proto = lwt_in_func_proto,
8849 .is_valid_access = lwt_is_valid_access,
8850 .convert_ctx_access = bpf_convert_ctx_access,
8853 const struct bpf_prog_ops lwt_in_prog_ops = {
8854 .test_run = bpf_prog_test_run_skb,
8857 const struct bpf_verifier_ops lwt_out_verifier_ops = {
8858 .get_func_proto = lwt_out_func_proto,
8859 .is_valid_access = lwt_is_valid_access,
8860 .convert_ctx_access = bpf_convert_ctx_access,
8863 const struct bpf_prog_ops lwt_out_prog_ops = {
8864 .test_run = bpf_prog_test_run_skb,
8867 const struct bpf_verifier_ops lwt_xmit_verifier_ops = {
8868 .get_func_proto = lwt_xmit_func_proto,
8869 .is_valid_access = lwt_is_valid_access,
8870 .convert_ctx_access = bpf_convert_ctx_access,
8871 .gen_prologue = tc_cls_act_prologue,
8874 const struct bpf_prog_ops lwt_xmit_prog_ops = {
8875 .test_run = bpf_prog_test_run_skb,
8878 const struct bpf_verifier_ops lwt_seg6local_verifier_ops = {
8879 .get_func_proto = lwt_seg6local_func_proto,
8880 .is_valid_access = lwt_is_valid_access,
8881 .convert_ctx_access = bpf_convert_ctx_access,
8884 const struct bpf_prog_ops lwt_seg6local_prog_ops = {
8885 .test_run = bpf_prog_test_run_skb,
8888 const struct bpf_verifier_ops cg_sock_verifier_ops = {
8889 .get_func_proto = sock_filter_func_proto,
8890 .is_valid_access = sock_filter_is_valid_access,
8891 .convert_ctx_access = bpf_sock_convert_ctx_access,
8894 const struct bpf_prog_ops cg_sock_prog_ops = {
8897 const struct bpf_verifier_ops cg_sock_addr_verifier_ops = {
8898 .get_func_proto = sock_addr_func_proto,
8899 .is_valid_access = sock_addr_is_valid_access,
8900 .convert_ctx_access = sock_addr_convert_ctx_access,
8903 const struct bpf_prog_ops cg_sock_addr_prog_ops = {
8906 const struct bpf_verifier_ops sock_ops_verifier_ops = {
8907 .get_func_proto = sock_ops_func_proto,
8908 .is_valid_access = sock_ops_is_valid_access,
8909 .convert_ctx_access = sock_ops_convert_ctx_access,
8912 const struct bpf_prog_ops sock_ops_prog_ops = {
8915 const struct bpf_verifier_ops sk_skb_verifier_ops = {
8916 .get_func_proto = sk_skb_func_proto,
8917 .is_valid_access = sk_skb_is_valid_access,
8918 .convert_ctx_access = sk_skb_convert_ctx_access,
8919 .gen_prologue = sk_skb_prologue,
8922 const struct bpf_prog_ops sk_skb_prog_ops = {
8925 const struct bpf_verifier_ops sk_msg_verifier_ops = {
8926 .get_func_proto = sk_msg_func_proto,
8927 .is_valid_access = sk_msg_is_valid_access,
8928 .convert_ctx_access = sk_msg_convert_ctx_access,
8929 .gen_prologue = bpf_noop_prologue,
8932 const struct bpf_prog_ops sk_msg_prog_ops = {
8935 const struct bpf_verifier_ops flow_dissector_verifier_ops = {
8936 .get_func_proto = flow_dissector_func_proto,
8937 .is_valid_access = flow_dissector_is_valid_access,
8938 .convert_ctx_access = flow_dissector_convert_ctx_access,
8941 const struct bpf_prog_ops flow_dissector_prog_ops = {
8942 .test_run = bpf_prog_test_run_flow_dissector,
8945 int sk_detach_filter(struct sock *sk)
8948 struct sk_filter *filter;
8950 if (sock_flag(sk, SOCK_FILTER_LOCKED))
8953 filter = rcu_dereference_protected(sk->sk_filter,
8954 lockdep_sock_is_held(sk));
8956 RCU_INIT_POINTER(sk->sk_filter, NULL);
8957 sk_filter_uncharge(sk, filter);
8963 EXPORT_SYMBOL_GPL(sk_detach_filter);
8965 int sk_get_filter(struct sock *sk, struct sock_filter __user *ubuf,
8968 struct sock_fprog_kern *fprog;
8969 struct sk_filter *filter;
8973 filter = rcu_dereference_protected(sk->sk_filter,
8974 lockdep_sock_is_held(sk));
8978 /* We're copying the filter that has been originally attached,
8979 * so no conversion/decode needed anymore. eBPF programs that
8980 * have no original program cannot be dumped through this.
8983 fprog = filter->prog->orig_prog;
8989 /* User space only enquires number of filter blocks. */
8993 if (len < fprog->len)
8997 if (copy_to_user(ubuf, fprog->filter, bpf_classic_proglen(fprog)))
9000 /* Instead of bytes, the API requests to return the number
9010 static void bpf_init_reuseport_kern(struct sk_reuseport_kern *reuse_kern,
9011 struct sock_reuseport *reuse,
9012 struct sock *sk, struct sk_buff *skb,
9015 reuse_kern->skb = skb;
9016 reuse_kern->sk = sk;
9017 reuse_kern->selected_sk = NULL;
9018 reuse_kern->data_end = skb->data + skb_headlen(skb);
9019 reuse_kern->hash = hash;
9020 reuse_kern->reuseport_id = reuse->reuseport_id;
9021 reuse_kern->bind_inany = reuse->bind_inany;
9024 struct sock *bpf_run_sk_reuseport(struct sock_reuseport *reuse, struct sock *sk,
9025 struct bpf_prog *prog, struct sk_buff *skb,
9028 struct sk_reuseport_kern reuse_kern;
9029 enum sk_action action;
9031 bpf_init_reuseport_kern(&reuse_kern, reuse, sk, skb, hash);
9032 action = BPF_PROG_RUN(prog, &reuse_kern);
9034 if (action == SK_PASS)
9035 return reuse_kern.selected_sk;
9037 return ERR_PTR(-ECONNREFUSED);
9040 BPF_CALL_4(sk_select_reuseport, struct sk_reuseport_kern *, reuse_kern,
9041 struct bpf_map *, map, void *, key, u32, flags)
9043 bool is_sockarray = map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY;
9044 struct sock_reuseport *reuse;
9045 struct sock *selected_sk;
9047 selected_sk = map->ops->map_lookup_elem(map, key);
9051 reuse = rcu_dereference(selected_sk->sk_reuseport_cb);
9053 /* Lookup in sock_map can return TCP ESTABLISHED sockets. */
9054 if (sk_is_refcounted(selected_sk))
9055 sock_put(selected_sk);
9057 /* reuseport_array has only sk with non NULL sk_reuseport_cb.
9058 * The only (!reuse) case here is - the sk has already been
9059 * unhashed (e.g. by close()), so treat it as -ENOENT.
9061 * Other maps (e.g. sock_map) do not provide this guarantee and
9062 * the sk may never be in the reuseport group to begin with.
9064 return is_sockarray ? -ENOENT : -EINVAL;
9067 if (unlikely(reuse->reuseport_id != reuse_kern->reuseport_id)) {
9068 struct sock *sk = reuse_kern->sk;
9070 if (sk->sk_protocol != selected_sk->sk_protocol)
9072 else if (sk->sk_family != selected_sk->sk_family)
9073 return -EAFNOSUPPORT;
9075 /* Catch all. Likely bound to a different sockaddr. */
9079 reuse_kern->selected_sk = selected_sk;
9084 static const struct bpf_func_proto sk_select_reuseport_proto = {
9085 .func = sk_select_reuseport,
9087 .ret_type = RET_INTEGER,
9088 .arg1_type = ARG_PTR_TO_CTX,
9089 .arg2_type = ARG_CONST_MAP_PTR,
9090 .arg3_type = ARG_PTR_TO_MAP_KEY,
9091 .arg4_type = ARG_ANYTHING,
9094 BPF_CALL_4(sk_reuseport_load_bytes,
9095 const struct sk_reuseport_kern *, reuse_kern, u32, offset,
9096 void *, to, u32, len)
9098 return ____bpf_skb_load_bytes(reuse_kern->skb, offset, to, len);
9101 static const struct bpf_func_proto sk_reuseport_load_bytes_proto = {
9102 .func = sk_reuseport_load_bytes,
9104 .ret_type = RET_INTEGER,
9105 .arg1_type = ARG_PTR_TO_CTX,
9106 .arg2_type = ARG_ANYTHING,
9107 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
9108 .arg4_type = ARG_CONST_SIZE,
9111 BPF_CALL_5(sk_reuseport_load_bytes_relative,
9112 const struct sk_reuseport_kern *, reuse_kern, u32, offset,
9113 void *, to, u32, len, u32, start_header)
9115 return ____bpf_skb_load_bytes_relative(reuse_kern->skb, offset, to,
9119 static const struct bpf_func_proto sk_reuseport_load_bytes_relative_proto = {
9120 .func = sk_reuseport_load_bytes_relative,
9122 .ret_type = RET_INTEGER,
9123 .arg1_type = ARG_PTR_TO_CTX,
9124 .arg2_type = ARG_ANYTHING,
9125 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
9126 .arg4_type = ARG_CONST_SIZE,
9127 .arg5_type = ARG_ANYTHING,
9130 static const struct bpf_func_proto *
9131 sk_reuseport_func_proto(enum bpf_func_id func_id,
9132 const struct bpf_prog *prog)
9135 case BPF_FUNC_sk_select_reuseport:
9136 return &sk_select_reuseport_proto;
9137 case BPF_FUNC_skb_load_bytes:
9138 return &sk_reuseport_load_bytes_proto;
9139 case BPF_FUNC_skb_load_bytes_relative:
9140 return &sk_reuseport_load_bytes_relative_proto;
9142 return bpf_base_func_proto(func_id);
9147 sk_reuseport_is_valid_access(int off, int size,
9148 enum bpf_access_type type,
9149 const struct bpf_prog *prog,
9150 struct bpf_insn_access_aux *info)
9152 const u32 size_default = sizeof(__u32);
9154 if (off < 0 || off >= sizeof(struct sk_reuseport_md) ||
9155 off % size || type != BPF_READ)
9159 case offsetof(struct sk_reuseport_md, data):
9160 info->reg_type = PTR_TO_PACKET;
9161 return size == sizeof(__u64);
9163 case offsetof(struct sk_reuseport_md, data_end):
9164 info->reg_type = PTR_TO_PACKET_END;
9165 return size == sizeof(__u64);
9167 case offsetof(struct sk_reuseport_md, hash):
9168 return size == size_default;
9170 /* Fields that allow narrowing */
9171 case bpf_ctx_range(struct sk_reuseport_md, eth_protocol):
9172 if (size < sizeof_field(struct sk_buff, protocol))
9175 case bpf_ctx_range(struct sk_reuseport_md, ip_protocol):
9176 case bpf_ctx_range(struct sk_reuseport_md, bind_inany):
9177 case bpf_ctx_range(struct sk_reuseport_md, len):
9178 bpf_ctx_record_field_size(info, size_default);
9179 return bpf_ctx_narrow_access_ok(off, size, size_default);
9186 #define SK_REUSEPORT_LOAD_FIELD(F) ({ \
9187 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_reuseport_kern, F), \
9188 si->dst_reg, si->src_reg, \
9189 bpf_target_off(struct sk_reuseport_kern, F, \
9190 sizeof_field(struct sk_reuseport_kern, F), \
9194 #define SK_REUSEPORT_LOAD_SKB_FIELD(SKB_FIELD) \
9195 SOCK_ADDR_LOAD_NESTED_FIELD(struct sk_reuseport_kern, \
9200 #define SK_REUSEPORT_LOAD_SK_FIELD(SK_FIELD) \
9201 SOCK_ADDR_LOAD_NESTED_FIELD(struct sk_reuseport_kern, \
9206 static u32 sk_reuseport_convert_ctx_access(enum bpf_access_type type,
9207 const struct bpf_insn *si,
9208 struct bpf_insn *insn_buf,
9209 struct bpf_prog *prog,
9212 struct bpf_insn *insn = insn_buf;
9215 case offsetof(struct sk_reuseport_md, data):
9216 SK_REUSEPORT_LOAD_SKB_FIELD(data);
9219 case offsetof(struct sk_reuseport_md, len):
9220 SK_REUSEPORT_LOAD_SKB_FIELD(len);
9223 case offsetof(struct sk_reuseport_md, eth_protocol):
9224 SK_REUSEPORT_LOAD_SKB_FIELD(protocol);
9227 case offsetof(struct sk_reuseport_md, ip_protocol):
9228 SK_REUSEPORT_LOAD_SK_FIELD(sk_protocol);
9231 case offsetof(struct sk_reuseport_md, data_end):
9232 SK_REUSEPORT_LOAD_FIELD(data_end);
9235 case offsetof(struct sk_reuseport_md, hash):
9236 SK_REUSEPORT_LOAD_FIELD(hash);
9239 case offsetof(struct sk_reuseport_md, bind_inany):
9240 SK_REUSEPORT_LOAD_FIELD(bind_inany);
9244 return insn - insn_buf;
9247 const struct bpf_verifier_ops sk_reuseport_verifier_ops = {
9248 .get_func_proto = sk_reuseport_func_proto,
9249 .is_valid_access = sk_reuseport_is_valid_access,
9250 .convert_ctx_access = sk_reuseport_convert_ctx_access,
9253 const struct bpf_prog_ops sk_reuseport_prog_ops = {
9256 DEFINE_STATIC_KEY_FALSE(bpf_sk_lookup_enabled);
9257 EXPORT_SYMBOL(bpf_sk_lookup_enabled);
9259 BPF_CALL_3(bpf_sk_lookup_assign, struct bpf_sk_lookup_kern *, ctx,
9260 struct sock *, sk, u64, flags)
9262 if (unlikely(flags & ~(BPF_SK_LOOKUP_F_REPLACE |
9263 BPF_SK_LOOKUP_F_NO_REUSEPORT)))
9265 if (unlikely(sk && sk_is_refcounted(sk)))
9266 return -ESOCKTNOSUPPORT; /* reject non-RCU freed sockets */
9267 if (unlikely(sk && sk->sk_state == TCP_ESTABLISHED))
9268 return -ESOCKTNOSUPPORT; /* reject connected sockets */
9270 /* Check if socket is suitable for packet L3/L4 protocol */
9271 if (sk && sk->sk_protocol != ctx->protocol)
9273 if (sk && sk->sk_family != ctx->family &&
9274 (sk->sk_family == AF_INET || ipv6_only_sock(sk)))
9275 return -EAFNOSUPPORT;
9277 if (ctx->selected_sk && !(flags & BPF_SK_LOOKUP_F_REPLACE))
9280 /* Select socket as lookup result */
9281 ctx->selected_sk = sk;
9282 ctx->no_reuseport = flags & BPF_SK_LOOKUP_F_NO_REUSEPORT;
9286 static const struct bpf_func_proto bpf_sk_lookup_assign_proto = {
9287 .func = bpf_sk_lookup_assign,
9289 .ret_type = RET_INTEGER,
9290 .arg1_type = ARG_PTR_TO_CTX,
9291 .arg2_type = ARG_PTR_TO_SOCKET_OR_NULL,
9292 .arg3_type = ARG_ANYTHING,
9295 static const struct bpf_func_proto *
9296 sk_lookup_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
9299 case BPF_FUNC_perf_event_output:
9300 return &bpf_event_output_data_proto;
9301 case BPF_FUNC_sk_assign:
9302 return &bpf_sk_lookup_assign_proto;
9303 case BPF_FUNC_sk_release:
9304 return &bpf_sk_release_proto;
9306 return bpf_base_func_proto(func_id);
9310 static bool sk_lookup_is_valid_access(int off, int size,
9311 enum bpf_access_type type,
9312 const struct bpf_prog *prog,
9313 struct bpf_insn_access_aux *info)
9315 if (off < 0 || off >= sizeof(struct bpf_sk_lookup))
9317 if (off % size != 0)
9319 if (type != BPF_READ)
9323 case offsetof(struct bpf_sk_lookup, sk):
9324 info->reg_type = PTR_TO_SOCKET_OR_NULL;
9325 return size == sizeof(__u64);
9327 case bpf_ctx_range(struct bpf_sk_lookup, family):
9328 case bpf_ctx_range(struct bpf_sk_lookup, protocol):
9329 case bpf_ctx_range(struct bpf_sk_lookup, remote_ip4):
9330 case bpf_ctx_range(struct bpf_sk_lookup, local_ip4):
9331 case bpf_ctx_range_till(struct bpf_sk_lookup, remote_ip6[0], remote_ip6[3]):
9332 case bpf_ctx_range_till(struct bpf_sk_lookup, local_ip6[0], local_ip6[3]):
9333 case bpf_ctx_range(struct bpf_sk_lookup, remote_port):
9334 case bpf_ctx_range(struct bpf_sk_lookup, local_port):
9335 bpf_ctx_record_field_size(info, sizeof(__u32));
9336 return bpf_ctx_narrow_access_ok(off, size, sizeof(__u32));
9343 static u32 sk_lookup_convert_ctx_access(enum bpf_access_type type,
9344 const struct bpf_insn *si,
9345 struct bpf_insn *insn_buf,
9346 struct bpf_prog *prog,
9349 struct bpf_insn *insn = insn_buf;
9352 case offsetof(struct bpf_sk_lookup, sk):
9353 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg, si->src_reg,
9354 offsetof(struct bpf_sk_lookup_kern, selected_sk));
9357 case offsetof(struct bpf_sk_lookup, family):
9358 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
9359 bpf_target_off(struct bpf_sk_lookup_kern,
9360 family, 2, target_size));
9363 case offsetof(struct bpf_sk_lookup, protocol):
9364 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
9365 bpf_target_off(struct bpf_sk_lookup_kern,
9366 protocol, 2, target_size));
9369 case offsetof(struct bpf_sk_lookup, remote_ip4):
9370 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
9371 bpf_target_off(struct bpf_sk_lookup_kern,
9372 v4.saddr, 4, target_size));
9375 case offsetof(struct bpf_sk_lookup, local_ip4):
9376 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
9377 bpf_target_off(struct bpf_sk_lookup_kern,
9378 v4.daddr, 4, target_size));
9381 case bpf_ctx_range_till(struct bpf_sk_lookup,
9382 remote_ip6[0], remote_ip6[3]): {
9383 #if IS_ENABLED(CONFIG_IPV6)
9386 off -= offsetof(struct bpf_sk_lookup, remote_ip6[0]);
9387 off += bpf_target_off(struct in6_addr, s6_addr32[0], 4, target_size);
9388 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg, si->src_reg,
9389 offsetof(struct bpf_sk_lookup_kern, v6.saddr));
9390 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
9391 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, off);
9393 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
9397 case bpf_ctx_range_till(struct bpf_sk_lookup,
9398 local_ip6[0], local_ip6[3]): {
9399 #if IS_ENABLED(CONFIG_IPV6)
9402 off -= offsetof(struct bpf_sk_lookup, local_ip6[0]);
9403 off += bpf_target_off(struct in6_addr, s6_addr32[0], 4, target_size);
9404 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg, si->src_reg,
9405 offsetof(struct bpf_sk_lookup_kern, v6.daddr));
9406 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
9407 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, off);
9409 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
9413 case offsetof(struct bpf_sk_lookup, remote_port):
9414 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
9415 bpf_target_off(struct bpf_sk_lookup_kern,
9416 sport, 2, target_size));
9419 case offsetof(struct bpf_sk_lookup, local_port):
9420 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
9421 bpf_target_off(struct bpf_sk_lookup_kern,
9422 dport, 2, target_size));
9426 return insn - insn_buf;
9429 const struct bpf_prog_ops sk_lookup_prog_ops = {
9432 const struct bpf_verifier_ops sk_lookup_verifier_ops = {
9433 .get_func_proto = sk_lookup_func_proto,
9434 .is_valid_access = sk_lookup_is_valid_access,
9435 .convert_ctx_access = sk_lookup_convert_ctx_access,
9438 #endif /* CONFIG_INET */
9440 DEFINE_BPF_DISPATCHER(xdp)
9442 void bpf_prog_change_xdp(struct bpf_prog *prev_prog, struct bpf_prog *prog)
9444 bpf_dispatcher_change_prog(BPF_DISPATCHER_PTR(xdp), prev_prog, prog);
9447 #ifdef CONFIG_DEBUG_INFO_BTF
9448 BTF_ID_LIST_GLOBAL(btf_sock_ids)
9449 #define BTF_SOCK_TYPE(name, type) BTF_ID(struct, type)
9451 #undef BTF_SOCK_TYPE
9453 u32 btf_sock_ids[MAX_BTF_SOCK_TYPE];
9456 static bool check_arg_btf_id(u32 btf_id, u32 arg)
9460 /* only one argument, no need to check arg */
9461 for (i = 0; i < MAX_BTF_SOCK_TYPE; i++)
9462 if (btf_sock_ids[i] == btf_id)
9467 BPF_CALL_1(bpf_skc_to_tcp6_sock, struct sock *, sk)
9469 /* tcp6_sock type is not generated in dwarf and hence btf,
9470 * trigger an explicit type generation here.
9472 BTF_TYPE_EMIT(struct tcp6_sock);
9473 if (sk_fullsock(sk) && sk->sk_protocol == IPPROTO_TCP &&
9474 sk->sk_family == AF_INET6)
9475 return (unsigned long)sk;
9477 return (unsigned long)NULL;
9480 const struct bpf_func_proto bpf_skc_to_tcp6_sock_proto = {
9481 .func = bpf_skc_to_tcp6_sock,
9483 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
9484 .arg1_type = ARG_PTR_TO_BTF_ID,
9485 .check_btf_id = check_arg_btf_id,
9486 .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_TCP6],
9489 BPF_CALL_1(bpf_skc_to_tcp_sock, struct sock *, sk)
9491 if (sk_fullsock(sk) && sk->sk_protocol == IPPROTO_TCP)
9492 return (unsigned long)sk;
9494 return (unsigned long)NULL;
9497 const struct bpf_func_proto bpf_skc_to_tcp_sock_proto = {
9498 .func = bpf_skc_to_tcp_sock,
9500 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
9501 .arg1_type = ARG_PTR_TO_BTF_ID,
9502 .check_btf_id = check_arg_btf_id,
9503 .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_TCP],
9506 BPF_CALL_1(bpf_skc_to_tcp_timewait_sock, struct sock *, sk)
9509 if (sk->sk_prot == &tcp_prot && sk->sk_state == TCP_TIME_WAIT)
9510 return (unsigned long)sk;
9513 #if IS_BUILTIN(CONFIG_IPV6)
9514 if (sk->sk_prot == &tcpv6_prot && sk->sk_state == TCP_TIME_WAIT)
9515 return (unsigned long)sk;
9518 return (unsigned long)NULL;
9521 const struct bpf_func_proto bpf_skc_to_tcp_timewait_sock_proto = {
9522 .func = bpf_skc_to_tcp_timewait_sock,
9524 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
9525 .arg1_type = ARG_PTR_TO_BTF_ID,
9526 .check_btf_id = check_arg_btf_id,
9527 .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_TCP_TW],
9530 BPF_CALL_1(bpf_skc_to_tcp_request_sock, struct sock *, sk)
9533 if (sk->sk_prot == &tcp_prot && sk->sk_state == TCP_NEW_SYN_RECV)
9534 return (unsigned long)sk;
9537 #if IS_BUILTIN(CONFIG_IPV6)
9538 if (sk->sk_prot == &tcpv6_prot && sk->sk_state == TCP_NEW_SYN_RECV)
9539 return (unsigned long)sk;
9542 return (unsigned long)NULL;
9545 const struct bpf_func_proto bpf_skc_to_tcp_request_sock_proto = {
9546 .func = bpf_skc_to_tcp_request_sock,
9548 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
9549 .arg1_type = ARG_PTR_TO_BTF_ID,
9550 .check_btf_id = check_arg_btf_id,
9551 .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_TCP_REQ],
9554 BPF_CALL_1(bpf_skc_to_udp6_sock, struct sock *, sk)
9556 /* udp6_sock type is not generated in dwarf and hence btf,
9557 * trigger an explicit type generation here.
9559 BTF_TYPE_EMIT(struct udp6_sock);
9560 if (sk_fullsock(sk) && sk->sk_protocol == IPPROTO_UDP &&
9561 sk->sk_type == SOCK_DGRAM && sk->sk_family == AF_INET6)
9562 return (unsigned long)sk;
9564 return (unsigned long)NULL;
9567 const struct bpf_func_proto bpf_skc_to_udp6_sock_proto = {
9568 .func = bpf_skc_to_udp6_sock,
9570 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
9571 .arg1_type = ARG_PTR_TO_BTF_ID,
9572 .check_btf_id = check_arg_btf_id,
9573 .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_UDP6],