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>
81 static const struct bpf_func_proto *
82 bpf_sk_base_func_proto(enum bpf_func_id func_id);
84 int copy_bpf_fprog_from_user(struct sock_fprog *dst, sockptr_t src, int len)
86 if (in_compat_syscall()) {
87 struct compat_sock_fprog f32;
89 if (len != sizeof(f32))
91 if (copy_from_sockptr(&f32, src, sizeof(f32)))
93 memset(dst, 0, sizeof(*dst));
95 dst->filter = compat_ptr(f32.filter);
97 if (len != sizeof(*dst))
99 if (copy_from_sockptr(dst, src, sizeof(*dst)))
105 EXPORT_SYMBOL_GPL(copy_bpf_fprog_from_user);
108 * sk_filter_trim_cap - run a packet through a socket filter
109 * @sk: sock associated with &sk_buff
110 * @skb: buffer to filter
111 * @cap: limit on how short the eBPF program may trim the packet
113 * Run the eBPF program and then cut skb->data to correct size returned by
114 * the program. If pkt_len is 0 we toss packet. If skb->len is smaller
115 * than pkt_len we keep whole skb->data. This is the socket level
116 * wrapper to BPF_PROG_RUN. It returns 0 if the packet should
117 * be accepted or -EPERM if the packet should be tossed.
120 int sk_filter_trim_cap(struct sock *sk, struct sk_buff *skb, unsigned int cap)
123 struct sk_filter *filter;
126 * If the skb was allocated from pfmemalloc reserves, only
127 * allow SOCK_MEMALLOC sockets to use it as this socket is
128 * helping free memory
130 if (skb_pfmemalloc(skb) && !sock_flag(sk, SOCK_MEMALLOC)) {
131 NET_INC_STATS(sock_net(sk), LINUX_MIB_PFMEMALLOCDROP);
134 err = BPF_CGROUP_RUN_PROG_INET_INGRESS(sk, skb);
138 err = security_sock_rcv_skb(sk, skb);
143 filter = rcu_dereference(sk->sk_filter);
145 struct sock *save_sk = skb->sk;
146 unsigned int pkt_len;
149 pkt_len = bpf_prog_run_save_cb(filter->prog, skb);
151 err = pkt_len ? pskb_trim(skb, max(cap, pkt_len)) : -EPERM;
157 EXPORT_SYMBOL(sk_filter_trim_cap);
159 BPF_CALL_1(bpf_skb_get_pay_offset, struct sk_buff *, skb)
161 return skb_get_poff(skb);
164 BPF_CALL_3(bpf_skb_get_nlattr, struct sk_buff *, skb, u32, a, u32, x)
168 if (skb_is_nonlinear(skb))
171 if (skb->len < sizeof(struct nlattr))
174 if (a > skb->len - sizeof(struct nlattr))
177 nla = nla_find((struct nlattr *) &skb->data[a], skb->len - a, x);
179 return (void *) nla - (void *) skb->data;
184 BPF_CALL_3(bpf_skb_get_nlattr_nest, struct sk_buff *, skb, u32, a, u32, x)
188 if (skb_is_nonlinear(skb))
191 if (skb->len < sizeof(struct nlattr))
194 if (a > skb->len - sizeof(struct nlattr))
197 nla = (struct nlattr *) &skb->data[a];
198 if (nla->nla_len > skb->len - a)
201 nla = nla_find_nested(nla, x);
203 return (void *) nla - (void *) skb->data;
208 BPF_CALL_4(bpf_skb_load_helper_8, const struct sk_buff *, skb, const void *,
209 data, int, headlen, int, offset)
212 const int len = sizeof(tmp);
215 if (headlen - offset >= len)
216 return *(u8 *)(data + offset);
217 if (!skb_copy_bits(skb, offset, &tmp, sizeof(tmp)))
220 ptr = bpf_internal_load_pointer_neg_helper(skb, offset, len);
228 BPF_CALL_2(bpf_skb_load_helper_8_no_cache, const struct sk_buff *, skb,
231 return ____bpf_skb_load_helper_8(skb, skb->data, skb->len - skb->data_len,
235 BPF_CALL_4(bpf_skb_load_helper_16, const struct sk_buff *, skb, const void *,
236 data, int, headlen, int, offset)
239 const int len = sizeof(tmp);
242 if (headlen - offset >= len)
243 return get_unaligned_be16(data + offset);
244 if (!skb_copy_bits(skb, offset, &tmp, sizeof(tmp)))
245 return be16_to_cpu(tmp);
247 ptr = bpf_internal_load_pointer_neg_helper(skb, offset, len);
249 return get_unaligned_be16(ptr);
255 BPF_CALL_2(bpf_skb_load_helper_16_no_cache, const struct sk_buff *, skb,
258 return ____bpf_skb_load_helper_16(skb, skb->data, skb->len - skb->data_len,
262 BPF_CALL_4(bpf_skb_load_helper_32, const struct sk_buff *, skb, const void *,
263 data, int, headlen, int, offset)
266 const int len = sizeof(tmp);
268 if (likely(offset >= 0)) {
269 if (headlen - offset >= len)
270 return get_unaligned_be32(data + offset);
271 if (!skb_copy_bits(skb, offset, &tmp, sizeof(tmp)))
272 return be32_to_cpu(tmp);
274 ptr = bpf_internal_load_pointer_neg_helper(skb, offset, len);
276 return get_unaligned_be32(ptr);
282 BPF_CALL_2(bpf_skb_load_helper_32_no_cache, const struct sk_buff *, skb,
285 return ____bpf_skb_load_helper_32(skb, skb->data, skb->len - skb->data_len,
289 static u32 convert_skb_access(int skb_field, int dst_reg, int src_reg,
290 struct bpf_insn *insn_buf)
292 struct bpf_insn *insn = insn_buf;
296 BUILD_BUG_ON(sizeof_field(struct sk_buff, mark) != 4);
298 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
299 offsetof(struct sk_buff, mark));
303 *insn++ = BPF_LDX_MEM(BPF_B, dst_reg, src_reg, PKT_TYPE_OFFSET());
304 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, PKT_TYPE_MAX);
305 #ifdef __BIG_ENDIAN_BITFIELD
306 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 5);
311 BUILD_BUG_ON(sizeof_field(struct sk_buff, queue_mapping) != 2);
313 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
314 offsetof(struct sk_buff, queue_mapping));
317 case SKF_AD_VLAN_TAG:
318 BUILD_BUG_ON(sizeof_field(struct sk_buff, vlan_tci) != 2);
320 /* dst_reg = *(u16 *) (src_reg + offsetof(vlan_tci)) */
321 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
322 offsetof(struct sk_buff, vlan_tci));
324 case SKF_AD_VLAN_TAG_PRESENT:
325 *insn++ = BPF_LDX_MEM(BPF_B, dst_reg, src_reg, PKT_VLAN_PRESENT_OFFSET());
326 if (PKT_VLAN_PRESENT_BIT)
327 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, PKT_VLAN_PRESENT_BIT);
328 if (PKT_VLAN_PRESENT_BIT < 7)
329 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, 1);
333 return insn - insn_buf;
336 static bool convert_bpf_extensions(struct sock_filter *fp,
337 struct bpf_insn **insnp)
339 struct bpf_insn *insn = *insnp;
343 case SKF_AD_OFF + SKF_AD_PROTOCOL:
344 BUILD_BUG_ON(sizeof_field(struct sk_buff, protocol) != 2);
346 /* A = *(u16 *) (CTX + offsetof(protocol)) */
347 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
348 offsetof(struct sk_buff, protocol));
349 /* A = ntohs(A) [emitting a nop or swap16] */
350 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
353 case SKF_AD_OFF + SKF_AD_PKTTYPE:
354 cnt = convert_skb_access(SKF_AD_PKTTYPE, BPF_REG_A, BPF_REG_CTX, insn);
358 case SKF_AD_OFF + SKF_AD_IFINDEX:
359 case SKF_AD_OFF + SKF_AD_HATYPE:
360 BUILD_BUG_ON(sizeof_field(struct net_device, ifindex) != 4);
361 BUILD_BUG_ON(sizeof_field(struct net_device, type) != 2);
363 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
364 BPF_REG_TMP, BPF_REG_CTX,
365 offsetof(struct sk_buff, dev));
366 /* if (tmp != 0) goto pc + 1 */
367 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_TMP, 0, 1);
368 *insn++ = BPF_EXIT_INSN();
369 if (fp->k == SKF_AD_OFF + SKF_AD_IFINDEX)
370 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_TMP,
371 offsetof(struct net_device, ifindex));
373 *insn = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_TMP,
374 offsetof(struct net_device, type));
377 case SKF_AD_OFF + SKF_AD_MARK:
378 cnt = convert_skb_access(SKF_AD_MARK, BPF_REG_A, BPF_REG_CTX, insn);
382 case SKF_AD_OFF + SKF_AD_RXHASH:
383 BUILD_BUG_ON(sizeof_field(struct sk_buff, hash) != 4);
385 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX,
386 offsetof(struct sk_buff, hash));
389 case SKF_AD_OFF + SKF_AD_QUEUE:
390 cnt = convert_skb_access(SKF_AD_QUEUE, BPF_REG_A, BPF_REG_CTX, insn);
394 case SKF_AD_OFF + SKF_AD_VLAN_TAG:
395 cnt = convert_skb_access(SKF_AD_VLAN_TAG,
396 BPF_REG_A, BPF_REG_CTX, insn);
400 case SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT:
401 cnt = convert_skb_access(SKF_AD_VLAN_TAG_PRESENT,
402 BPF_REG_A, BPF_REG_CTX, insn);
406 case SKF_AD_OFF + SKF_AD_VLAN_TPID:
407 BUILD_BUG_ON(sizeof_field(struct sk_buff, vlan_proto) != 2);
409 /* A = *(u16 *) (CTX + offsetof(vlan_proto)) */
410 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
411 offsetof(struct sk_buff, vlan_proto));
412 /* A = ntohs(A) [emitting a nop or swap16] */
413 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
416 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
417 case SKF_AD_OFF + SKF_AD_NLATTR:
418 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
419 case SKF_AD_OFF + SKF_AD_CPU:
420 case SKF_AD_OFF + SKF_AD_RANDOM:
422 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX);
424 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_A);
426 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_X);
427 /* Emit call(arg1=CTX, arg2=A, arg3=X) */
429 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
430 *insn = BPF_EMIT_CALL(bpf_skb_get_pay_offset);
432 case SKF_AD_OFF + SKF_AD_NLATTR:
433 *insn = BPF_EMIT_CALL(bpf_skb_get_nlattr);
435 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
436 *insn = BPF_EMIT_CALL(bpf_skb_get_nlattr_nest);
438 case SKF_AD_OFF + SKF_AD_CPU:
439 *insn = BPF_EMIT_CALL(bpf_get_raw_cpu_id);
441 case SKF_AD_OFF + SKF_AD_RANDOM:
442 *insn = BPF_EMIT_CALL(bpf_user_rnd_u32);
443 bpf_user_rnd_init_once();
448 case SKF_AD_OFF + SKF_AD_ALU_XOR_X:
450 *insn = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_X);
454 /* This is just a dummy call to avoid letting the compiler
455 * evict __bpf_call_base() as an optimization. Placed here
456 * where no-one bothers.
458 BUG_ON(__bpf_call_base(0, 0, 0, 0, 0) != 0);
466 static bool convert_bpf_ld_abs(struct sock_filter *fp, struct bpf_insn **insnp)
468 const bool unaligned_ok = IS_BUILTIN(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS);
469 int size = bpf_size_to_bytes(BPF_SIZE(fp->code));
470 bool endian = BPF_SIZE(fp->code) == BPF_H ||
471 BPF_SIZE(fp->code) == BPF_W;
472 bool indirect = BPF_MODE(fp->code) == BPF_IND;
473 const int ip_align = NET_IP_ALIGN;
474 struct bpf_insn *insn = *insnp;
478 ((unaligned_ok && offset >= 0) ||
479 (!unaligned_ok && offset >= 0 &&
480 offset + ip_align >= 0 &&
481 offset + ip_align % size == 0))) {
482 bool ldx_off_ok = offset <= S16_MAX;
484 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_H);
486 *insn++ = BPF_ALU64_IMM(BPF_SUB, BPF_REG_TMP, offset);
487 *insn++ = BPF_JMP_IMM(BPF_JSLT, BPF_REG_TMP,
488 size, 2 + endian + (!ldx_off_ok * 2));
490 *insn++ = BPF_LDX_MEM(BPF_SIZE(fp->code), BPF_REG_A,
493 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_D);
494 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_TMP, offset);
495 *insn++ = BPF_LDX_MEM(BPF_SIZE(fp->code), BPF_REG_A,
499 *insn++ = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, size * 8);
500 *insn++ = BPF_JMP_A(8);
503 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX);
504 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_D);
505 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_H);
507 *insn++ = BPF_MOV64_IMM(BPF_REG_ARG4, offset);
509 *insn++ = BPF_MOV64_REG(BPF_REG_ARG4, BPF_REG_X);
511 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_ARG4, offset);
514 switch (BPF_SIZE(fp->code)) {
516 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_8);
519 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_16);
522 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_32);
528 *insn++ = BPF_JMP_IMM(BPF_JSGE, BPF_REG_A, 0, 2);
529 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
530 *insn = BPF_EXIT_INSN();
537 * bpf_convert_filter - convert filter program
538 * @prog: the user passed filter program
539 * @len: the length of the user passed filter program
540 * @new_prog: allocated 'struct bpf_prog' or NULL
541 * @new_len: pointer to store length of converted program
542 * @seen_ld_abs: bool whether we've seen ld_abs/ind
544 * Remap 'sock_filter' style classic BPF (cBPF) instruction set to 'bpf_insn'
545 * style extended BPF (eBPF).
546 * Conversion workflow:
548 * 1) First pass for calculating the new program length:
549 * bpf_convert_filter(old_prog, old_len, NULL, &new_len, &seen_ld_abs)
551 * 2) 2nd pass to remap in two passes: 1st pass finds new
552 * jump offsets, 2nd pass remapping:
553 * bpf_convert_filter(old_prog, old_len, new_prog, &new_len, &seen_ld_abs)
555 static int bpf_convert_filter(struct sock_filter *prog, int len,
556 struct bpf_prog *new_prog, int *new_len,
559 int new_flen = 0, pass = 0, target, i, stack_off;
560 struct bpf_insn *new_insn, *first_insn = NULL;
561 struct sock_filter *fp;
565 BUILD_BUG_ON(BPF_MEMWORDS * sizeof(u32) > MAX_BPF_STACK);
566 BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG);
568 if (len <= 0 || len > BPF_MAXINSNS)
572 first_insn = new_prog->insnsi;
573 addrs = kcalloc(len, sizeof(*addrs),
574 GFP_KERNEL | __GFP_NOWARN);
580 new_insn = first_insn;
583 /* Classic BPF related prologue emission. */
585 /* Classic BPF expects A and X to be reset first. These need
586 * to be guaranteed to be the first two instructions.
588 *new_insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
589 *new_insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_X, BPF_REG_X);
591 /* All programs must keep CTX in callee saved BPF_REG_CTX.
592 * In eBPF case it's done by the compiler, here we need to
593 * do this ourself. Initial CTX is present in BPF_REG_ARG1.
595 *new_insn++ = BPF_MOV64_REG(BPF_REG_CTX, BPF_REG_ARG1);
597 /* For packet access in classic BPF, cache skb->data
598 * in callee-saved BPF R8 and skb->len - skb->data_len
599 * (headlen) in BPF R9. Since classic BPF is read-only
600 * on CTX, we only need to cache it once.
602 *new_insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
603 BPF_REG_D, BPF_REG_CTX,
604 offsetof(struct sk_buff, data));
605 *new_insn++ = BPF_LDX_MEM(BPF_W, BPF_REG_H, BPF_REG_CTX,
606 offsetof(struct sk_buff, len));
607 *new_insn++ = BPF_LDX_MEM(BPF_W, BPF_REG_TMP, BPF_REG_CTX,
608 offsetof(struct sk_buff, data_len));
609 *new_insn++ = BPF_ALU32_REG(BPF_SUB, BPF_REG_H, BPF_REG_TMP);
615 for (i = 0; i < len; fp++, i++) {
616 struct bpf_insn tmp_insns[32] = { };
617 struct bpf_insn *insn = tmp_insns;
620 addrs[i] = new_insn - first_insn;
623 /* All arithmetic insns and skb loads map as-is. */
624 case BPF_ALU | BPF_ADD | BPF_X:
625 case BPF_ALU | BPF_ADD | BPF_K:
626 case BPF_ALU | BPF_SUB | BPF_X:
627 case BPF_ALU | BPF_SUB | BPF_K:
628 case BPF_ALU | BPF_AND | BPF_X:
629 case BPF_ALU | BPF_AND | BPF_K:
630 case BPF_ALU | BPF_OR | BPF_X:
631 case BPF_ALU | BPF_OR | BPF_K:
632 case BPF_ALU | BPF_LSH | BPF_X:
633 case BPF_ALU | BPF_LSH | BPF_K:
634 case BPF_ALU | BPF_RSH | BPF_X:
635 case BPF_ALU | BPF_RSH | BPF_K:
636 case BPF_ALU | BPF_XOR | BPF_X:
637 case BPF_ALU | BPF_XOR | BPF_K:
638 case BPF_ALU | BPF_MUL | BPF_X:
639 case BPF_ALU | BPF_MUL | BPF_K:
640 case BPF_ALU | BPF_DIV | BPF_X:
641 case BPF_ALU | BPF_DIV | BPF_K:
642 case BPF_ALU | BPF_MOD | BPF_X:
643 case BPF_ALU | BPF_MOD | BPF_K:
644 case BPF_ALU | BPF_NEG:
645 case BPF_LD | BPF_ABS | BPF_W:
646 case BPF_LD | BPF_ABS | BPF_H:
647 case BPF_LD | BPF_ABS | BPF_B:
648 case BPF_LD | BPF_IND | BPF_W:
649 case BPF_LD | BPF_IND | BPF_H:
650 case BPF_LD | BPF_IND | BPF_B:
651 /* Check for overloaded BPF extension and
652 * directly convert it if found, otherwise
653 * just move on with mapping.
655 if (BPF_CLASS(fp->code) == BPF_LD &&
656 BPF_MODE(fp->code) == BPF_ABS &&
657 convert_bpf_extensions(fp, &insn))
659 if (BPF_CLASS(fp->code) == BPF_LD &&
660 convert_bpf_ld_abs(fp, &insn)) {
665 if (fp->code == (BPF_ALU | BPF_DIV | BPF_X) ||
666 fp->code == (BPF_ALU | BPF_MOD | BPF_X)) {
667 *insn++ = BPF_MOV32_REG(BPF_REG_X, BPF_REG_X);
668 /* Error with exception code on div/mod by 0.
669 * For cBPF programs, this was always return 0.
671 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_X, 0, 2);
672 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
673 *insn++ = BPF_EXIT_INSN();
676 *insn = BPF_RAW_INSN(fp->code, BPF_REG_A, BPF_REG_X, 0, fp->k);
679 /* Jump transformation cannot use BPF block macros
680 * everywhere as offset calculation and target updates
681 * require a bit more work than the rest, i.e. jump
682 * opcodes map as-is, but offsets need adjustment.
685 #define BPF_EMIT_JMP \
687 const s32 off_min = S16_MIN, off_max = S16_MAX; \
690 if (target >= len || target < 0) \
692 off = addrs ? addrs[target] - addrs[i] - 1 : 0; \
693 /* Adjust pc relative offset for 2nd or 3rd insn. */ \
694 off -= insn - tmp_insns; \
695 /* Reject anything not fitting into insn->off. */ \
696 if (off < off_min || off > off_max) \
701 case BPF_JMP | BPF_JA:
702 target = i + fp->k + 1;
703 insn->code = fp->code;
707 case BPF_JMP | BPF_JEQ | BPF_K:
708 case BPF_JMP | BPF_JEQ | BPF_X:
709 case BPF_JMP | BPF_JSET | BPF_K:
710 case BPF_JMP | BPF_JSET | BPF_X:
711 case BPF_JMP | BPF_JGT | BPF_K:
712 case BPF_JMP | BPF_JGT | BPF_X:
713 case BPF_JMP | BPF_JGE | BPF_K:
714 case BPF_JMP | BPF_JGE | BPF_X:
715 if (BPF_SRC(fp->code) == BPF_K && (int) fp->k < 0) {
716 /* BPF immediates are signed, zero extend
717 * immediate into tmp register and use it
720 *insn++ = BPF_MOV32_IMM(BPF_REG_TMP, fp->k);
722 insn->dst_reg = BPF_REG_A;
723 insn->src_reg = BPF_REG_TMP;
726 insn->dst_reg = BPF_REG_A;
728 bpf_src = BPF_SRC(fp->code);
729 insn->src_reg = bpf_src == BPF_X ? BPF_REG_X : 0;
732 /* Common case where 'jump_false' is next insn. */
734 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
735 target = i + fp->jt + 1;
740 /* Convert some jumps when 'jump_true' is next insn. */
742 switch (BPF_OP(fp->code)) {
744 insn->code = BPF_JMP | BPF_JNE | bpf_src;
747 insn->code = BPF_JMP | BPF_JLE | bpf_src;
750 insn->code = BPF_JMP | BPF_JLT | bpf_src;
756 target = i + fp->jf + 1;
761 /* Other jumps are mapped into two insns: Jxx and JA. */
762 target = i + fp->jt + 1;
763 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
767 insn->code = BPF_JMP | BPF_JA;
768 target = i + fp->jf + 1;
772 /* ldxb 4 * ([14] & 0xf) is remaped into 6 insns. */
773 case BPF_LDX | BPF_MSH | BPF_B: {
774 struct sock_filter tmp = {
775 .code = BPF_LD | BPF_ABS | BPF_B,
782 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
783 /* A = BPF_R0 = *(u8 *) (skb->data + K) */
784 convert_bpf_ld_abs(&tmp, &insn);
787 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, 0xf);
789 *insn++ = BPF_ALU32_IMM(BPF_LSH, BPF_REG_A, 2);
791 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_X);
793 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
795 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_TMP);
798 /* RET_K is remaped into 2 insns. RET_A case doesn't need an
799 * extra mov as BPF_REG_0 is already mapped into BPF_REG_A.
801 case BPF_RET | BPF_A:
802 case BPF_RET | BPF_K:
803 if (BPF_RVAL(fp->code) == BPF_K)
804 *insn++ = BPF_MOV32_RAW(BPF_K, BPF_REG_0,
806 *insn = BPF_EXIT_INSN();
809 /* Store to stack. */
812 stack_off = fp->k * 4 + 4;
813 *insn = BPF_STX_MEM(BPF_W, BPF_REG_FP, BPF_CLASS(fp->code) ==
814 BPF_ST ? BPF_REG_A : BPF_REG_X,
816 /* check_load_and_stores() verifies that classic BPF can
817 * load from stack only after write, so tracking
818 * stack_depth for ST|STX insns is enough
820 if (new_prog && new_prog->aux->stack_depth < stack_off)
821 new_prog->aux->stack_depth = stack_off;
824 /* Load from stack. */
825 case BPF_LD | BPF_MEM:
826 case BPF_LDX | BPF_MEM:
827 stack_off = fp->k * 4 + 4;
828 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
829 BPF_REG_A : BPF_REG_X, BPF_REG_FP,
834 case BPF_LD | BPF_IMM:
835 case BPF_LDX | BPF_IMM:
836 *insn = BPF_MOV32_IMM(BPF_CLASS(fp->code) == BPF_LD ?
837 BPF_REG_A : BPF_REG_X, fp->k);
841 case BPF_MISC | BPF_TAX:
842 *insn = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
846 case BPF_MISC | BPF_TXA:
847 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_X);
850 /* A = skb->len or X = skb->len */
851 case BPF_LD | BPF_W | BPF_LEN:
852 case BPF_LDX | BPF_W | BPF_LEN:
853 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
854 BPF_REG_A : BPF_REG_X, BPF_REG_CTX,
855 offsetof(struct sk_buff, len));
858 /* Access seccomp_data fields. */
859 case BPF_LDX | BPF_ABS | BPF_W:
860 /* A = *(u32 *) (ctx + K) */
861 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX, fp->k);
864 /* Unknown instruction. */
871 memcpy(new_insn, tmp_insns,
872 sizeof(*insn) * (insn - tmp_insns));
873 new_insn += insn - tmp_insns;
877 /* Only calculating new length. */
878 *new_len = new_insn - first_insn;
880 *new_len += 4; /* Prologue bits. */
885 if (new_flen != new_insn - first_insn) {
886 new_flen = new_insn - first_insn;
893 BUG_ON(*new_len != new_flen);
902 * As we dont want to clear mem[] array for each packet going through
903 * __bpf_prog_run(), we check that filter loaded by user never try to read
904 * a cell if not previously written, and we check all branches to be sure
905 * a malicious user doesn't try to abuse us.
907 static int check_load_and_stores(const struct sock_filter *filter, int flen)
909 u16 *masks, memvalid = 0; /* One bit per cell, 16 cells */
912 BUILD_BUG_ON(BPF_MEMWORDS > 16);
914 masks = kmalloc_array(flen, sizeof(*masks), GFP_KERNEL);
918 memset(masks, 0xff, flen * sizeof(*masks));
920 for (pc = 0; pc < flen; pc++) {
921 memvalid &= masks[pc];
923 switch (filter[pc].code) {
926 memvalid |= (1 << filter[pc].k);
928 case BPF_LD | BPF_MEM:
929 case BPF_LDX | BPF_MEM:
930 if (!(memvalid & (1 << filter[pc].k))) {
935 case BPF_JMP | BPF_JA:
936 /* A jump must set masks on target */
937 masks[pc + 1 + filter[pc].k] &= memvalid;
940 case BPF_JMP | BPF_JEQ | BPF_K:
941 case BPF_JMP | BPF_JEQ | BPF_X:
942 case BPF_JMP | BPF_JGE | BPF_K:
943 case BPF_JMP | BPF_JGE | BPF_X:
944 case BPF_JMP | BPF_JGT | BPF_K:
945 case BPF_JMP | BPF_JGT | BPF_X:
946 case BPF_JMP | BPF_JSET | BPF_K:
947 case BPF_JMP | BPF_JSET | BPF_X:
948 /* A jump must set masks on targets */
949 masks[pc + 1 + filter[pc].jt] &= memvalid;
950 masks[pc + 1 + filter[pc].jf] &= memvalid;
960 static bool chk_code_allowed(u16 code_to_probe)
962 static const bool codes[] = {
963 /* 32 bit ALU operations */
964 [BPF_ALU | BPF_ADD | BPF_K] = true,
965 [BPF_ALU | BPF_ADD | BPF_X] = true,
966 [BPF_ALU | BPF_SUB | BPF_K] = true,
967 [BPF_ALU | BPF_SUB | BPF_X] = true,
968 [BPF_ALU | BPF_MUL | BPF_K] = true,
969 [BPF_ALU | BPF_MUL | BPF_X] = true,
970 [BPF_ALU | BPF_DIV | BPF_K] = true,
971 [BPF_ALU | BPF_DIV | BPF_X] = true,
972 [BPF_ALU | BPF_MOD | BPF_K] = true,
973 [BPF_ALU | BPF_MOD | BPF_X] = true,
974 [BPF_ALU | BPF_AND | BPF_K] = true,
975 [BPF_ALU | BPF_AND | BPF_X] = true,
976 [BPF_ALU | BPF_OR | BPF_K] = true,
977 [BPF_ALU | BPF_OR | BPF_X] = true,
978 [BPF_ALU | BPF_XOR | BPF_K] = true,
979 [BPF_ALU | BPF_XOR | BPF_X] = true,
980 [BPF_ALU | BPF_LSH | BPF_K] = true,
981 [BPF_ALU | BPF_LSH | BPF_X] = true,
982 [BPF_ALU | BPF_RSH | BPF_K] = true,
983 [BPF_ALU | BPF_RSH | BPF_X] = true,
984 [BPF_ALU | BPF_NEG] = true,
985 /* Load instructions */
986 [BPF_LD | BPF_W | BPF_ABS] = true,
987 [BPF_LD | BPF_H | BPF_ABS] = true,
988 [BPF_LD | BPF_B | BPF_ABS] = true,
989 [BPF_LD | BPF_W | BPF_LEN] = true,
990 [BPF_LD | BPF_W | BPF_IND] = true,
991 [BPF_LD | BPF_H | BPF_IND] = true,
992 [BPF_LD | BPF_B | BPF_IND] = true,
993 [BPF_LD | BPF_IMM] = true,
994 [BPF_LD | BPF_MEM] = true,
995 [BPF_LDX | BPF_W | BPF_LEN] = true,
996 [BPF_LDX | BPF_B | BPF_MSH] = true,
997 [BPF_LDX | BPF_IMM] = true,
998 [BPF_LDX | BPF_MEM] = true,
999 /* Store instructions */
1002 /* Misc instructions */
1003 [BPF_MISC | BPF_TAX] = true,
1004 [BPF_MISC | BPF_TXA] = true,
1005 /* Return instructions */
1006 [BPF_RET | BPF_K] = true,
1007 [BPF_RET | BPF_A] = true,
1008 /* Jump instructions */
1009 [BPF_JMP | BPF_JA] = true,
1010 [BPF_JMP | BPF_JEQ | BPF_K] = true,
1011 [BPF_JMP | BPF_JEQ | BPF_X] = true,
1012 [BPF_JMP | BPF_JGE | BPF_K] = true,
1013 [BPF_JMP | BPF_JGE | BPF_X] = true,
1014 [BPF_JMP | BPF_JGT | BPF_K] = true,
1015 [BPF_JMP | BPF_JGT | BPF_X] = true,
1016 [BPF_JMP | BPF_JSET | BPF_K] = true,
1017 [BPF_JMP | BPF_JSET | BPF_X] = true,
1020 if (code_to_probe >= ARRAY_SIZE(codes))
1023 return codes[code_to_probe];
1026 static bool bpf_check_basics_ok(const struct sock_filter *filter,
1031 if (flen == 0 || flen > BPF_MAXINSNS)
1038 * bpf_check_classic - verify socket filter code
1039 * @filter: filter to verify
1040 * @flen: length of filter
1042 * Check the user's filter code. If we let some ugly
1043 * filter code slip through kaboom! The filter must contain
1044 * no references or jumps that are out of range, no illegal
1045 * instructions, and must end with a RET instruction.
1047 * All jumps are forward as they are not signed.
1049 * Returns 0 if the rule set is legal or -EINVAL if not.
1051 static int bpf_check_classic(const struct sock_filter *filter,
1057 /* Check the filter code now */
1058 for (pc = 0; pc < flen; pc++) {
1059 const struct sock_filter *ftest = &filter[pc];
1061 /* May we actually operate on this code? */
1062 if (!chk_code_allowed(ftest->code))
1065 /* Some instructions need special checks */
1066 switch (ftest->code) {
1067 case BPF_ALU | BPF_DIV | BPF_K:
1068 case BPF_ALU | BPF_MOD | BPF_K:
1069 /* Check for division by zero */
1073 case BPF_ALU | BPF_LSH | BPF_K:
1074 case BPF_ALU | BPF_RSH | BPF_K:
1078 case BPF_LD | BPF_MEM:
1079 case BPF_LDX | BPF_MEM:
1082 /* Check for invalid memory addresses */
1083 if (ftest->k >= BPF_MEMWORDS)
1086 case BPF_JMP | BPF_JA:
1087 /* Note, the large ftest->k might cause loops.
1088 * Compare this with conditional jumps below,
1089 * where offsets are limited. --ANK (981016)
1091 if (ftest->k >= (unsigned int)(flen - pc - 1))
1094 case BPF_JMP | BPF_JEQ | BPF_K:
1095 case BPF_JMP | BPF_JEQ | BPF_X:
1096 case BPF_JMP | BPF_JGE | BPF_K:
1097 case BPF_JMP | BPF_JGE | BPF_X:
1098 case BPF_JMP | BPF_JGT | BPF_K:
1099 case BPF_JMP | BPF_JGT | BPF_X:
1100 case BPF_JMP | BPF_JSET | BPF_K:
1101 case BPF_JMP | BPF_JSET | BPF_X:
1102 /* Both conditionals must be safe */
1103 if (pc + ftest->jt + 1 >= flen ||
1104 pc + ftest->jf + 1 >= flen)
1107 case BPF_LD | BPF_W | BPF_ABS:
1108 case BPF_LD | BPF_H | BPF_ABS:
1109 case BPF_LD | BPF_B | BPF_ABS:
1111 if (bpf_anc_helper(ftest) & BPF_ANC)
1113 /* Ancillary operation unknown or unsupported */
1114 if (anc_found == false && ftest->k >= SKF_AD_OFF)
1119 /* Last instruction must be a RET code */
1120 switch (filter[flen - 1].code) {
1121 case BPF_RET | BPF_K:
1122 case BPF_RET | BPF_A:
1123 return check_load_and_stores(filter, flen);
1129 static int bpf_prog_store_orig_filter(struct bpf_prog *fp,
1130 const struct sock_fprog *fprog)
1132 unsigned int fsize = bpf_classic_proglen(fprog);
1133 struct sock_fprog_kern *fkprog;
1135 fp->orig_prog = kmalloc(sizeof(*fkprog), GFP_KERNEL);
1139 fkprog = fp->orig_prog;
1140 fkprog->len = fprog->len;
1142 fkprog->filter = kmemdup(fp->insns, fsize,
1143 GFP_KERNEL | __GFP_NOWARN);
1144 if (!fkprog->filter) {
1145 kfree(fp->orig_prog);
1152 static void bpf_release_orig_filter(struct bpf_prog *fp)
1154 struct sock_fprog_kern *fprog = fp->orig_prog;
1157 kfree(fprog->filter);
1162 static void __bpf_prog_release(struct bpf_prog *prog)
1164 if (prog->type == BPF_PROG_TYPE_SOCKET_FILTER) {
1167 bpf_release_orig_filter(prog);
1168 bpf_prog_free(prog);
1172 static void __sk_filter_release(struct sk_filter *fp)
1174 __bpf_prog_release(fp->prog);
1179 * sk_filter_release_rcu - Release a socket filter by rcu_head
1180 * @rcu: rcu_head that contains the sk_filter to free
1182 static void sk_filter_release_rcu(struct rcu_head *rcu)
1184 struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu);
1186 __sk_filter_release(fp);
1190 * sk_filter_release - release a socket filter
1191 * @fp: filter to remove
1193 * Remove a filter from a socket and release its resources.
1195 static void sk_filter_release(struct sk_filter *fp)
1197 if (refcount_dec_and_test(&fp->refcnt))
1198 call_rcu(&fp->rcu, sk_filter_release_rcu);
1201 void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp)
1203 u32 filter_size = bpf_prog_size(fp->prog->len);
1205 atomic_sub(filter_size, &sk->sk_omem_alloc);
1206 sk_filter_release(fp);
1209 /* try to charge the socket memory if there is space available
1210 * return true on success
1212 static bool __sk_filter_charge(struct sock *sk, struct sk_filter *fp)
1214 u32 filter_size = bpf_prog_size(fp->prog->len);
1215 int optmem_max = READ_ONCE(sysctl_optmem_max);
1217 /* same check as in sock_kmalloc() */
1218 if (filter_size <= optmem_max &&
1219 atomic_read(&sk->sk_omem_alloc) + filter_size < optmem_max) {
1220 atomic_add(filter_size, &sk->sk_omem_alloc);
1226 bool sk_filter_charge(struct sock *sk, struct sk_filter *fp)
1228 if (!refcount_inc_not_zero(&fp->refcnt))
1231 if (!__sk_filter_charge(sk, fp)) {
1232 sk_filter_release(fp);
1238 static struct bpf_prog *bpf_migrate_filter(struct bpf_prog *fp)
1240 struct sock_filter *old_prog;
1241 struct bpf_prog *old_fp;
1242 int err, new_len, old_len = fp->len;
1243 bool seen_ld_abs = false;
1245 /* We are free to overwrite insns et al right here as it
1246 * won't be used at this point in time anymore internally
1247 * after the migration to the internal BPF instruction
1250 BUILD_BUG_ON(sizeof(struct sock_filter) !=
1251 sizeof(struct bpf_insn));
1253 /* Conversion cannot happen on overlapping memory areas,
1254 * so we need to keep the user BPF around until the 2nd
1255 * pass. At this time, the user BPF is stored in fp->insns.
1257 old_prog = kmemdup(fp->insns, old_len * sizeof(struct sock_filter),
1258 GFP_KERNEL | __GFP_NOWARN);
1264 /* 1st pass: calculate the new program length. */
1265 err = bpf_convert_filter(old_prog, old_len, NULL, &new_len,
1270 /* Expand fp for appending the new filter representation. */
1272 fp = bpf_prog_realloc(old_fp, bpf_prog_size(new_len), 0);
1274 /* The old_fp is still around in case we couldn't
1275 * allocate new memory, so uncharge on that one.
1284 /* 2nd pass: remap sock_filter insns into bpf_insn insns. */
1285 err = bpf_convert_filter(old_prog, old_len, fp, &new_len,
1288 /* 2nd bpf_convert_filter() can fail only if it fails
1289 * to allocate memory, remapping must succeed. Note,
1290 * that at this time old_fp has already been released
1295 fp = bpf_prog_select_runtime(fp, &err);
1305 __bpf_prog_release(fp);
1306 return ERR_PTR(err);
1309 static struct bpf_prog *bpf_prepare_filter(struct bpf_prog *fp,
1310 bpf_aux_classic_check_t trans)
1314 fp->bpf_func = NULL;
1317 err = bpf_check_classic(fp->insns, fp->len);
1319 __bpf_prog_release(fp);
1320 return ERR_PTR(err);
1323 /* There might be additional checks and transformations
1324 * needed on classic filters, f.e. in case of seccomp.
1327 err = trans(fp->insns, fp->len);
1329 __bpf_prog_release(fp);
1330 return ERR_PTR(err);
1334 /* Probe if we can JIT compile the filter and if so, do
1335 * the compilation of the filter.
1337 bpf_jit_compile(fp);
1339 /* JIT compiler couldn't process this filter, so do the
1340 * internal BPF translation for the optimized interpreter.
1343 fp = bpf_migrate_filter(fp);
1349 * bpf_prog_create - create an unattached filter
1350 * @pfp: the unattached filter that is created
1351 * @fprog: the filter program
1353 * Create a filter independent of any socket. We first run some
1354 * sanity checks on it to make sure it does not explode on us later.
1355 * If an error occurs or there is insufficient memory for the filter
1356 * a negative errno code is returned. On success the return is zero.
1358 int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog)
1360 unsigned int fsize = bpf_classic_proglen(fprog);
1361 struct bpf_prog *fp;
1363 /* Make sure new filter is there and in the right amounts. */
1364 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1367 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1371 memcpy(fp->insns, fprog->filter, fsize);
1373 fp->len = fprog->len;
1374 /* Since unattached filters are not copied back to user
1375 * space through sk_get_filter(), we do not need to hold
1376 * a copy here, and can spare us the work.
1378 fp->orig_prog = NULL;
1380 /* bpf_prepare_filter() already takes care of freeing
1381 * memory in case something goes wrong.
1383 fp = bpf_prepare_filter(fp, NULL);
1390 EXPORT_SYMBOL_GPL(bpf_prog_create);
1393 * bpf_prog_create_from_user - create an unattached filter from user buffer
1394 * @pfp: the unattached filter that is created
1395 * @fprog: the filter program
1396 * @trans: post-classic verifier transformation handler
1397 * @save_orig: save classic BPF program
1399 * This function effectively does the same as bpf_prog_create(), only
1400 * that it builds up its insns buffer from user space provided buffer.
1401 * It also allows for passing a bpf_aux_classic_check_t handler.
1403 int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog,
1404 bpf_aux_classic_check_t trans, bool save_orig)
1406 unsigned int fsize = bpf_classic_proglen(fprog);
1407 struct bpf_prog *fp;
1410 /* Make sure new filter is there and in the right amounts. */
1411 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1414 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1418 if (copy_from_user(fp->insns, fprog->filter, fsize)) {
1419 __bpf_prog_free(fp);
1423 fp->len = fprog->len;
1424 fp->orig_prog = NULL;
1427 err = bpf_prog_store_orig_filter(fp, fprog);
1429 __bpf_prog_free(fp);
1434 /* bpf_prepare_filter() already takes care of freeing
1435 * memory in case something goes wrong.
1437 fp = bpf_prepare_filter(fp, trans);
1444 EXPORT_SYMBOL_GPL(bpf_prog_create_from_user);
1446 void bpf_prog_destroy(struct bpf_prog *fp)
1448 __bpf_prog_release(fp);
1450 EXPORT_SYMBOL_GPL(bpf_prog_destroy);
1452 static int __sk_attach_prog(struct bpf_prog *prog, struct sock *sk)
1454 struct sk_filter *fp, *old_fp;
1456 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
1462 if (!__sk_filter_charge(sk, fp)) {
1466 refcount_set(&fp->refcnt, 1);
1468 old_fp = rcu_dereference_protected(sk->sk_filter,
1469 lockdep_sock_is_held(sk));
1470 rcu_assign_pointer(sk->sk_filter, fp);
1473 sk_filter_uncharge(sk, old_fp);
1479 struct bpf_prog *__get_filter(struct sock_fprog *fprog, struct sock *sk)
1481 unsigned int fsize = bpf_classic_proglen(fprog);
1482 struct bpf_prog *prog;
1485 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1486 return ERR_PTR(-EPERM);
1488 /* Make sure new filter is there and in the right amounts. */
1489 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1490 return ERR_PTR(-EINVAL);
1492 prog = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1494 return ERR_PTR(-ENOMEM);
1496 if (copy_from_user(prog->insns, fprog->filter, fsize)) {
1497 __bpf_prog_free(prog);
1498 return ERR_PTR(-EFAULT);
1501 prog->len = fprog->len;
1503 err = bpf_prog_store_orig_filter(prog, fprog);
1505 __bpf_prog_free(prog);
1506 return ERR_PTR(-ENOMEM);
1509 /* bpf_prepare_filter() already takes care of freeing
1510 * memory in case something goes wrong.
1512 return bpf_prepare_filter(prog, NULL);
1516 * sk_attach_filter - attach a socket filter
1517 * @fprog: the filter program
1518 * @sk: the socket to use
1520 * Attach the user's filter code. We first run some sanity checks on
1521 * it to make sure it does not explode on us later. If an error
1522 * occurs or there is insufficient memory for the filter a negative
1523 * errno code is returned. On success the return is zero.
1525 int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1527 struct bpf_prog *prog = __get_filter(fprog, sk);
1531 return PTR_ERR(prog);
1533 err = __sk_attach_prog(prog, sk);
1535 __bpf_prog_release(prog);
1541 EXPORT_SYMBOL_GPL(sk_attach_filter);
1543 int sk_reuseport_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1545 struct bpf_prog *prog = __get_filter(fprog, sk);
1549 return PTR_ERR(prog);
1551 if (bpf_prog_size(prog->len) > READ_ONCE(sysctl_optmem_max))
1554 err = reuseport_attach_prog(sk, prog);
1557 __bpf_prog_release(prog);
1562 static struct bpf_prog *__get_bpf(u32 ufd, struct sock *sk)
1564 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1565 return ERR_PTR(-EPERM);
1567 return bpf_prog_get_type(ufd, BPF_PROG_TYPE_SOCKET_FILTER);
1570 int sk_attach_bpf(u32 ufd, struct sock *sk)
1572 struct bpf_prog *prog = __get_bpf(ufd, sk);
1576 return PTR_ERR(prog);
1578 err = __sk_attach_prog(prog, sk);
1587 int sk_reuseport_attach_bpf(u32 ufd, struct sock *sk)
1589 struct bpf_prog *prog;
1592 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1595 prog = bpf_prog_get_type(ufd, BPF_PROG_TYPE_SOCKET_FILTER);
1596 if (PTR_ERR(prog) == -EINVAL)
1597 prog = bpf_prog_get_type(ufd, BPF_PROG_TYPE_SK_REUSEPORT);
1599 return PTR_ERR(prog);
1601 if (prog->type == BPF_PROG_TYPE_SK_REUSEPORT) {
1602 /* Like other non BPF_PROG_TYPE_SOCKET_FILTER
1603 * bpf prog (e.g. sockmap). It depends on the
1604 * limitation imposed by bpf_prog_load().
1605 * Hence, sysctl_optmem_max is not checked.
1607 if ((sk->sk_type != SOCK_STREAM &&
1608 sk->sk_type != SOCK_DGRAM) ||
1609 (sk->sk_protocol != IPPROTO_UDP &&
1610 sk->sk_protocol != IPPROTO_TCP) ||
1611 (sk->sk_family != AF_INET &&
1612 sk->sk_family != AF_INET6)) {
1617 /* BPF_PROG_TYPE_SOCKET_FILTER */
1618 if (bpf_prog_size(prog->len) > READ_ONCE(sysctl_optmem_max)) {
1624 err = reuseport_attach_prog(sk, prog);
1632 void sk_reuseport_prog_free(struct bpf_prog *prog)
1637 if (prog->type == BPF_PROG_TYPE_SK_REUSEPORT)
1640 bpf_prog_destroy(prog);
1643 struct bpf_scratchpad {
1645 __be32 diff[MAX_BPF_STACK / sizeof(__be32)];
1646 u8 buff[MAX_BPF_STACK];
1650 static DEFINE_PER_CPU(struct bpf_scratchpad, bpf_sp);
1652 static inline int __bpf_try_make_writable(struct sk_buff *skb,
1653 unsigned int write_len)
1655 return skb_ensure_writable(skb, write_len);
1658 static inline int bpf_try_make_writable(struct sk_buff *skb,
1659 unsigned int write_len)
1661 int err = __bpf_try_make_writable(skb, write_len);
1663 bpf_compute_data_pointers(skb);
1667 static int bpf_try_make_head_writable(struct sk_buff *skb)
1669 return bpf_try_make_writable(skb, skb_headlen(skb));
1672 static inline void bpf_push_mac_rcsum(struct sk_buff *skb)
1674 if (skb_at_tc_ingress(skb))
1675 skb_postpush_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1678 static inline void bpf_pull_mac_rcsum(struct sk_buff *skb)
1680 if (skb_at_tc_ingress(skb))
1681 skb_postpull_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1684 BPF_CALL_5(bpf_skb_store_bytes, struct sk_buff *, skb, u32, offset,
1685 const void *, from, u32, len, u64, flags)
1689 if (unlikely(flags & ~(BPF_F_RECOMPUTE_CSUM | BPF_F_INVALIDATE_HASH)))
1691 if (unlikely(offset > INT_MAX))
1693 if (unlikely(bpf_try_make_writable(skb, offset + len)))
1696 ptr = skb->data + offset;
1697 if (flags & BPF_F_RECOMPUTE_CSUM)
1698 __skb_postpull_rcsum(skb, ptr, len, offset);
1700 memcpy(ptr, from, len);
1702 if (flags & BPF_F_RECOMPUTE_CSUM)
1703 __skb_postpush_rcsum(skb, ptr, len, offset);
1704 if (flags & BPF_F_INVALIDATE_HASH)
1705 skb_clear_hash(skb);
1710 static const struct bpf_func_proto bpf_skb_store_bytes_proto = {
1711 .func = bpf_skb_store_bytes,
1713 .ret_type = RET_INTEGER,
1714 .arg1_type = ARG_PTR_TO_CTX,
1715 .arg2_type = ARG_ANYTHING,
1716 .arg3_type = ARG_PTR_TO_MEM,
1717 .arg4_type = ARG_CONST_SIZE,
1718 .arg5_type = ARG_ANYTHING,
1721 BPF_CALL_4(bpf_skb_load_bytes, const struct sk_buff *, skb, u32, offset,
1722 void *, to, u32, len)
1726 if (unlikely(offset > INT_MAX))
1729 ptr = skb_header_pointer(skb, offset, len, to);
1733 memcpy(to, ptr, len);
1741 static const struct bpf_func_proto bpf_skb_load_bytes_proto = {
1742 .func = bpf_skb_load_bytes,
1744 .ret_type = RET_INTEGER,
1745 .arg1_type = ARG_PTR_TO_CTX,
1746 .arg2_type = ARG_ANYTHING,
1747 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
1748 .arg4_type = ARG_CONST_SIZE,
1751 BPF_CALL_4(bpf_flow_dissector_load_bytes,
1752 const struct bpf_flow_dissector *, ctx, u32, offset,
1753 void *, to, u32, len)
1757 if (unlikely(offset > 0xffff))
1760 if (unlikely(!ctx->skb))
1763 ptr = skb_header_pointer(ctx->skb, offset, len, to);
1767 memcpy(to, ptr, len);
1775 static const struct bpf_func_proto bpf_flow_dissector_load_bytes_proto = {
1776 .func = bpf_flow_dissector_load_bytes,
1778 .ret_type = RET_INTEGER,
1779 .arg1_type = ARG_PTR_TO_CTX,
1780 .arg2_type = ARG_ANYTHING,
1781 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
1782 .arg4_type = ARG_CONST_SIZE,
1785 BPF_CALL_5(bpf_skb_load_bytes_relative, const struct sk_buff *, skb,
1786 u32, offset, void *, to, u32, len, u32, start_header)
1788 u8 *end = skb_tail_pointer(skb);
1791 if (unlikely(offset > 0xffff))
1794 switch (start_header) {
1795 case BPF_HDR_START_MAC:
1796 if (unlikely(!skb_mac_header_was_set(skb)))
1798 start = skb_mac_header(skb);
1800 case BPF_HDR_START_NET:
1801 start = skb_network_header(skb);
1807 ptr = start + offset;
1809 if (likely(ptr + len <= end)) {
1810 memcpy(to, ptr, len);
1819 static const struct bpf_func_proto bpf_skb_load_bytes_relative_proto = {
1820 .func = bpf_skb_load_bytes_relative,
1822 .ret_type = RET_INTEGER,
1823 .arg1_type = ARG_PTR_TO_CTX,
1824 .arg2_type = ARG_ANYTHING,
1825 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
1826 .arg4_type = ARG_CONST_SIZE,
1827 .arg5_type = ARG_ANYTHING,
1830 BPF_CALL_2(bpf_skb_pull_data, struct sk_buff *, skb, u32, len)
1832 /* Idea is the following: should the needed direct read/write
1833 * test fail during runtime, we can pull in more data and redo
1834 * again, since implicitly, we invalidate previous checks here.
1836 * Or, since we know how much we need to make read/writeable,
1837 * this can be done once at the program beginning for direct
1838 * access case. By this we overcome limitations of only current
1839 * headroom being accessible.
1841 return bpf_try_make_writable(skb, len ? : skb_headlen(skb));
1844 static const struct bpf_func_proto bpf_skb_pull_data_proto = {
1845 .func = bpf_skb_pull_data,
1847 .ret_type = RET_INTEGER,
1848 .arg1_type = ARG_PTR_TO_CTX,
1849 .arg2_type = ARG_ANYTHING,
1852 BPF_CALL_1(bpf_sk_fullsock, struct sock *, sk)
1854 return sk_fullsock(sk) ? (unsigned long)sk : (unsigned long)NULL;
1857 static const struct bpf_func_proto bpf_sk_fullsock_proto = {
1858 .func = bpf_sk_fullsock,
1860 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
1861 .arg1_type = ARG_PTR_TO_SOCK_COMMON,
1864 static inline int sk_skb_try_make_writable(struct sk_buff *skb,
1865 unsigned int write_len)
1867 int err = __bpf_try_make_writable(skb, write_len);
1869 bpf_compute_data_end_sk_skb(skb);
1873 BPF_CALL_2(sk_skb_pull_data, struct sk_buff *, skb, u32, len)
1875 /* Idea is the following: should the needed direct read/write
1876 * test fail during runtime, we can pull in more data and redo
1877 * again, since implicitly, we invalidate previous checks here.
1879 * Or, since we know how much we need to make read/writeable,
1880 * this can be done once at the program beginning for direct
1881 * access case. By this we overcome limitations of only current
1882 * headroom being accessible.
1884 return sk_skb_try_make_writable(skb, len ? : skb_headlen(skb));
1887 static const struct bpf_func_proto sk_skb_pull_data_proto = {
1888 .func = sk_skb_pull_data,
1890 .ret_type = RET_INTEGER,
1891 .arg1_type = ARG_PTR_TO_CTX,
1892 .arg2_type = ARG_ANYTHING,
1895 BPF_CALL_5(bpf_l3_csum_replace, struct sk_buff *, skb, u32, offset,
1896 u64, from, u64, to, u64, flags)
1900 if (unlikely(flags & ~(BPF_F_HDR_FIELD_MASK)))
1902 if (unlikely(offset > 0xffff || offset & 1))
1904 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
1907 ptr = (__sum16 *)(skb->data + offset);
1908 switch (flags & BPF_F_HDR_FIELD_MASK) {
1910 if (unlikely(from != 0))
1913 csum_replace_by_diff(ptr, to);
1916 csum_replace2(ptr, from, to);
1919 csum_replace4(ptr, from, to);
1928 static const struct bpf_func_proto bpf_l3_csum_replace_proto = {
1929 .func = bpf_l3_csum_replace,
1931 .ret_type = RET_INTEGER,
1932 .arg1_type = ARG_PTR_TO_CTX,
1933 .arg2_type = ARG_ANYTHING,
1934 .arg3_type = ARG_ANYTHING,
1935 .arg4_type = ARG_ANYTHING,
1936 .arg5_type = ARG_ANYTHING,
1939 BPF_CALL_5(bpf_l4_csum_replace, struct sk_buff *, skb, u32, offset,
1940 u64, from, u64, to, u64, flags)
1942 bool is_pseudo = flags & BPF_F_PSEUDO_HDR;
1943 bool is_mmzero = flags & BPF_F_MARK_MANGLED_0;
1944 bool do_mforce = flags & BPF_F_MARK_ENFORCE;
1947 if (unlikely(flags & ~(BPF_F_MARK_MANGLED_0 | BPF_F_MARK_ENFORCE |
1948 BPF_F_PSEUDO_HDR | BPF_F_HDR_FIELD_MASK)))
1950 if (unlikely(offset > 0xffff || offset & 1))
1952 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
1955 ptr = (__sum16 *)(skb->data + offset);
1956 if (is_mmzero && !do_mforce && !*ptr)
1959 switch (flags & BPF_F_HDR_FIELD_MASK) {
1961 if (unlikely(from != 0))
1964 inet_proto_csum_replace_by_diff(ptr, skb, to, is_pseudo);
1967 inet_proto_csum_replace2(ptr, skb, from, to, is_pseudo);
1970 inet_proto_csum_replace4(ptr, skb, from, to, is_pseudo);
1976 if (is_mmzero && !*ptr)
1977 *ptr = CSUM_MANGLED_0;
1981 static const struct bpf_func_proto bpf_l4_csum_replace_proto = {
1982 .func = bpf_l4_csum_replace,
1984 .ret_type = RET_INTEGER,
1985 .arg1_type = ARG_PTR_TO_CTX,
1986 .arg2_type = ARG_ANYTHING,
1987 .arg3_type = ARG_ANYTHING,
1988 .arg4_type = ARG_ANYTHING,
1989 .arg5_type = ARG_ANYTHING,
1992 BPF_CALL_5(bpf_csum_diff, __be32 *, from, u32, from_size,
1993 __be32 *, to, u32, to_size, __wsum, seed)
1995 struct bpf_scratchpad *sp = this_cpu_ptr(&bpf_sp);
1996 u32 diff_size = from_size + to_size;
1999 /* This is quite flexible, some examples:
2001 * from_size == 0, to_size > 0, seed := csum --> pushing data
2002 * from_size > 0, to_size == 0, seed := csum --> pulling data
2003 * from_size > 0, to_size > 0, seed := 0 --> diffing data
2005 * Even for diffing, from_size and to_size don't need to be equal.
2007 if (unlikely(((from_size | to_size) & (sizeof(__be32) - 1)) ||
2008 diff_size > sizeof(sp->diff)))
2011 for (i = 0; i < from_size / sizeof(__be32); i++, j++)
2012 sp->diff[j] = ~from[i];
2013 for (i = 0; i < to_size / sizeof(__be32); i++, j++)
2014 sp->diff[j] = to[i];
2016 return csum_partial(sp->diff, diff_size, seed);
2019 static const struct bpf_func_proto bpf_csum_diff_proto = {
2020 .func = bpf_csum_diff,
2023 .ret_type = RET_INTEGER,
2024 .arg1_type = ARG_PTR_TO_MEM_OR_NULL,
2025 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
2026 .arg3_type = ARG_PTR_TO_MEM_OR_NULL,
2027 .arg4_type = ARG_CONST_SIZE_OR_ZERO,
2028 .arg5_type = ARG_ANYTHING,
2031 BPF_CALL_2(bpf_csum_update, struct sk_buff *, skb, __wsum, csum)
2033 /* The interface is to be used in combination with bpf_csum_diff()
2034 * for direct packet writes. csum rotation for alignment as well
2035 * as emulating csum_sub() can be done from the eBPF program.
2037 if (skb->ip_summed == CHECKSUM_COMPLETE)
2038 return (skb->csum = csum_add(skb->csum, csum));
2043 static const struct bpf_func_proto bpf_csum_update_proto = {
2044 .func = bpf_csum_update,
2046 .ret_type = RET_INTEGER,
2047 .arg1_type = ARG_PTR_TO_CTX,
2048 .arg2_type = ARG_ANYTHING,
2051 BPF_CALL_2(bpf_csum_level, struct sk_buff *, skb, u64, level)
2053 /* The interface is to be used in combination with bpf_skb_adjust_room()
2054 * for encap/decap of packet headers when BPF_F_ADJ_ROOM_NO_CSUM_RESET
2055 * is passed as flags, for example.
2058 case BPF_CSUM_LEVEL_INC:
2059 __skb_incr_checksum_unnecessary(skb);
2061 case BPF_CSUM_LEVEL_DEC:
2062 __skb_decr_checksum_unnecessary(skb);
2064 case BPF_CSUM_LEVEL_RESET:
2065 __skb_reset_checksum_unnecessary(skb);
2067 case BPF_CSUM_LEVEL_QUERY:
2068 return skb->ip_summed == CHECKSUM_UNNECESSARY ?
2069 skb->csum_level : -EACCES;
2077 static const struct bpf_func_proto bpf_csum_level_proto = {
2078 .func = bpf_csum_level,
2080 .ret_type = RET_INTEGER,
2081 .arg1_type = ARG_PTR_TO_CTX,
2082 .arg2_type = ARG_ANYTHING,
2085 static inline int __bpf_rx_skb(struct net_device *dev, struct sk_buff *skb)
2087 return dev_forward_skb(dev, skb);
2090 static inline int __bpf_rx_skb_no_mac(struct net_device *dev,
2091 struct sk_buff *skb)
2093 int ret = ____dev_forward_skb(dev, skb);
2097 ret = netif_rx(skb);
2103 static inline int __bpf_tx_skb(struct net_device *dev, struct sk_buff *skb)
2107 if (dev_xmit_recursion()) {
2108 net_crit_ratelimited("bpf: recursion limit reached on datapath, buggy bpf program?\n");
2116 dev_xmit_recursion_inc();
2117 ret = dev_queue_xmit(skb);
2118 dev_xmit_recursion_dec();
2123 static int __bpf_redirect_no_mac(struct sk_buff *skb, struct net_device *dev,
2126 unsigned int mlen = skb_network_offset(skb);
2128 if (unlikely(skb->len <= mlen)) {
2134 __skb_pull(skb, mlen);
2135 if (unlikely(!skb->len)) {
2140 /* At ingress, the mac header has already been pulled once.
2141 * At egress, skb_pospull_rcsum has to be done in case that
2142 * the skb is originated from ingress (i.e. a forwarded skb)
2143 * to ensure that rcsum starts at net header.
2145 if (!skb_at_tc_ingress(skb))
2146 skb_postpull_rcsum(skb, skb_mac_header(skb), mlen);
2148 skb_pop_mac_header(skb);
2149 skb_reset_mac_len(skb);
2150 return flags & BPF_F_INGRESS ?
2151 __bpf_rx_skb_no_mac(dev, skb) : __bpf_tx_skb(dev, skb);
2154 static int __bpf_redirect_common(struct sk_buff *skb, struct net_device *dev,
2157 /* Verify that a link layer header is carried */
2158 if (unlikely(skb->mac_header >= skb->network_header || skb->len == 0)) {
2163 bpf_push_mac_rcsum(skb);
2164 return flags & BPF_F_INGRESS ?
2165 __bpf_rx_skb(dev, skb) : __bpf_tx_skb(dev, skb);
2168 static int __bpf_redirect(struct sk_buff *skb, struct net_device *dev,
2171 if (dev_is_mac_header_xmit(dev))
2172 return __bpf_redirect_common(skb, dev, flags);
2174 return __bpf_redirect_no_mac(skb, dev, flags);
2177 #if IS_ENABLED(CONFIG_IPV6)
2178 static int bpf_out_neigh_v6(struct net *net, struct sk_buff *skb,
2179 struct net_device *dev, struct bpf_nh_params *nh)
2181 u32 hh_len = LL_RESERVED_SPACE(dev);
2182 const struct in6_addr *nexthop;
2183 struct dst_entry *dst = NULL;
2184 struct neighbour *neigh;
2186 if (dev_xmit_recursion()) {
2187 net_crit_ratelimited("bpf: recursion limit reached on datapath, buggy bpf program?\n");
2194 if (unlikely(skb_headroom(skb) < hh_len && dev->header_ops)) {
2195 struct sk_buff *skb2;
2197 skb2 = skb_realloc_headroom(skb, hh_len);
2198 if (unlikely(!skb2)) {
2203 skb_set_owner_w(skb2, skb->sk);
2211 nexthop = rt6_nexthop(container_of(dst, struct rt6_info, dst),
2212 &ipv6_hdr(skb)->daddr);
2214 nexthop = &nh->ipv6_nh;
2216 neigh = ip_neigh_gw6(dev, nexthop);
2217 if (likely(!IS_ERR(neigh))) {
2220 sock_confirm_neigh(skb, neigh);
2221 dev_xmit_recursion_inc();
2222 ret = neigh_output(neigh, skb, false);
2223 dev_xmit_recursion_dec();
2224 rcu_read_unlock_bh();
2227 rcu_read_unlock_bh();
2229 IP6_INC_STATS(dev_net(dst->dev),
2230 ip6_dst_idev(dst), IPSTATS_MIB_OUTNOROUTES);
2236 static int __bpf_redirect_neigh_v6(struct sk_buff *skb, struct net_device *dev,
2237 struct bpf_nh_params *nh)
2239 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
2240 struct net *net = dev_net(dev);
2241 int err, ret = NET_XMIT_DROP;
2244 struct dst_entry *dst;
2245 struct flowi6 fl6 = {
2246 .flowi6_flags = FLOWI_FLAG_ANYSRC,
2247 .flowi6_mark = skb->mark,
2248 .flowlabel = ip6_flowinfo(ip6h),
2249 .flowi6_oif = dev->ifindex,
2250 .flowi6_proto = ip6h->nexthdr,
2251 .daddr = ip6h->daddr,
2252 .saddr = ip6h->saddr,
2255 dst = ipv6_stub->ipv6_dst_lookup_flow(net, NULL, &fl6, NULL);
2259 skb_dst_set(skb, dst);
2260 } else if (nh->nh_family != AF_INET6) {
2264 err = bpf_out_neigh_v6(net, skb, dev, nh);
2265 if (unlikely(net_xmit_eval(err)))
2266 dev->stats.tx_errors++;
2268 ret = NET_XMIT_SUCCESS;
2271 dev->stats.tx_errors++;
2277 static int __bpf_redirect_neigh_v6(struct sk_buff *skb, struct net_device *dev,
2278 struct bpf_nh_params *nh)
2281 return NET_XMIT_DROP;
2283 #endif /* CONFIG_IPV6 */
2285 #if IS_ENABLED(CONFIG_INET)
2286 static int bpf_out_neigh_v4(struct net *net, struct sk_buff *skb,
2287 struct net_device *dev, struct bpf_nh_params *nh)
2289 u32 hh_len = LL_RESERVED_SPACE(dev);
2290 struct neighbour *neigh;
2291 bool is_v6gw = false;
2293 if (dev_xmit_recursion()) {
2294 net_crit_ratelimited("bpf: recursion limit reached on datapath, buggy bpf program?\n");
2301 if (unlikely(skb_headroom(skb) < hh_len && dev->header_ops)) {
2302 struct sk_buff *skb2;
2304 skb2 = skb_realloc_headroom(skb, hh_len);
2305 if (unlikely(!skb2)) {
2310 skb_set_owner_w(skb2, skb->sk);
2317 struct dst_entry *dst = skb_dst(skb);
2318 struct rtable *rt = container_of(dst, struct rtable, dst);
2320 neigh = ip_neigh_for_gw(rt, skb, &is_v6gw);
2321 } else if (nh->nh_family == AF_INET6) {
2322 neigh = ip_neigh_gw6(dev, &nh->ipv6_nh);
2324 } else if (nh->nh_family == AF_INET) {
2325 neigh = ip_neigh_gw4(dev, nh->ipv4_nh);
2327 rcu_read_unlock_bh();
2331 if (likely(!IS_ERR(neigh))) {
2334 sock_confirm_neigh(skb, neigh);
2335 dev_xmit_recursion_inc();
2336 ret = neigh_output(neigh, skb, is_v6gw);
2337 dev_xmit_recursion_dec();
2338 rcu_read_unlock_bh();
2341 rcu_read_unlock_bh();
2347 static int __bpf_redirect_neigh_v4(struct sk_buff *skb, struct net_device *dev,
2348 struct bpf_nh_params *nh)
2350 const struct iphdr *ip4h = ip_hdr(skb);
2351 struct net *net = dev_net(dev);
2352 int err, ret = NET_XMIT_DROP;
2355 struct flowi4 fl4 = {
2356 .flowi4_flags = FLOWI_FLAG_ANYSRC,
2357 .flowi4_mark = skb->mark,
2358 .flowi4_tos = RT_TOS(ip4h->tos),
2359 .flowi4_oif = dev->ifindex,
2360 .flowi4_proto = ip4h->protocol,
2361 .daddr = ip4h->daddr,
2362 .saddr = ip4h->saddr,
2366 rt = ip_route_output_flow(net, &fl4, NULL);
2369 if (rt->rt_type != RTN_UNICAST && rt->rt_type != RTN_LOCAL) {
2374 skb_dst_set(skb, &rt->dst);
2377 err = bpf_out_neigh_v4(net, skb, dev, nh);
2378 if (unlikely(net_xmit_eval(err)))
2379 dev->stats.tx_errors++;
2381 ret = NET_XMIT_SUCCESS;
2384 dev->stats.tx_errors++;
2390 static int __bpf_redirect_neigh_v4(struct sk_buff *skb, struct net_device *dev,
2391 struct bpf_nh_params *nh)
2394 return NET_XMIT_DROP;
2396 #endif /* CONFIG_INET */
2398 static int __bpf_redirect_neigh(struct sk_buff *skb, struct net_device *dev,
2399 struct bpf_nh_params *nh)
2401 struct ethhdr *ethh = eth_hdr(skb);
2403 if (unlikely(skb->mac_header >= skb->network_header))
2405 bpf_push_mac_rcsum(skb);
2406 if (is_multicast_ether_addr(ethh->h_dest))
2409 skb_pull(skb, sizeof(*ethh));
2410 skb_unset_mac_header(skb);
2411 skb_reset_network_header(skb);
2413 if (skb->protocol == htons(ETH_P_IP))
2414 return __bpf_redirect_neigh_v4(skb, dev, nh);
2415 else if (skb->protocol == htons(ETH_P_IPV6))
2416 return __bpf_redirect_neigh_v6(skb, dev, nh);
2422 /* Internal, non-exposed redirect flags. */
2424 BPF_F_NEIGH = (1ULL << 1),
2425 BPF_F_PEER = (1ULL << 2),
2426 BPF_F_NEXTHOP = (1ULL << 3),
2427 #define BPF_F_REDIRECT_INTERNAL (BPF_F_NEIGH | BPF_F_PEER | BPF_F_NEXTHOP)
2430 BPF_CALL_3(bpf_clone_redirect, struct sk_buff *, skb, u32, ifindex, u64, flags)
2432 struct net_device *dev;
2433 struct sk_buff *clone;
2436 if (unlikely(flags & (~(BPF_F_INGRESS) | BPF_F_REDIRECT_INTERNAL)))
2439 dev = dev_get_by_index_rcu(dev_net(skb->dev), ifindex);
2443 clone = skb_clone(skb, GFP_ATOMIC);
2444 if (unlikely(!clone))
2447 /* For direct write, we need to keep the invariant that the skbs
2448 * we're dealing with need to be uncloned. Should uncloning fail
2449 * here, we need to free the just generated clone to unclone once
2452 ret = bpf_try_make_head_writable(skb);
2453 if (unlikely(ret)) {
2458 return __bpf_redirect(clone, dev, flags);
2461 static const struct bpf_func_proto bpf_clone_redirect_proto = {
2462 .func = bpf_clone_redirect,
2464 .ret_type = RET_INTEGER,
2465 .arg1_type = ARG_PTR_TO_CTX,
2466 .arg2_type = ARG_ANYTHING,
2467 .arg3_type = ARG_ANYTHING,
2470 DEFINE_PER_CPU(struct bpf_redirect_info, bpf_redirect_info);
2471 EXPORT_PER_CPU_SYMBOL_GPL(bpf_redirect_info);
2473 int skb_do_redirect(struct sk_buff *skb)
2475 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
2476 struct net *net = dev_net(skb->dev);
2477 struct net_device *dev;
2478 u32 flags = ri->flags;
2480 dev = dev_get_by_index_rcu(net, ri->tgt_index);
2485 if (flags & BPF_F_PEER) {
2486 const struct net_device_ops *ops = dev->netdev_ops;
2488 if (unlikely(!ops->ndo_get_peer_dev ||
2489 !skb_at_tc_ingress(skb)))
2491 dev = ops->ndo_get_peer_dev(dev);
2492 if (unlikely(!dev ||
2493 !is_skb_forwardable(dev, skb) ||
2494 net_eq(net, dev_net(dev))))
2499 return flags & BPF_F_NEIGH ?
2500 __bpf_redirect_neigh(skb, dev, flags & BPF_F_NEXTHOP ?
2502 __bpf_redirect(skb, dev, flags);
2508 BPF_CALL_2(bpf_redirect, u32, ifindex, u64, flags)
2510 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
2512 if (unlikely(flags & (~(BPF_F_INGRESS) | BPF_F_REDIRECT_INTERNAL)))
2516 ri->tgt_index = ifindex;
2518 return TC_ACT_REDIRECT;
2521 static const struct bpf_func_proto bpf_redirect_proto = {
2522 .func = bpf_redirect,
2524 .ret_type = RET_INTEGER,
2525 .arg1_type = ARG_ANYTHING,
2526 .arg2_type = ARG_ANYTHING,
2529 BPF_CALL_2(bpf_redirect_peer, u32, ifindex, u64, flags)
2531 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
2533 if (unlikely(flags))
2536 ri->flags = BPF_F_PEER;
2537 ri->tgt_index = ifindex;
2539 return TC_ACT_REDIRECT;
2542 static const struct bpf_func_proto bpf_redirect_peer_proto = {
2543 .func = bpf_redirect_peer,
2545 .ret_type = RET_INTEGER,
2546 .arg1_type = ARG_ANYTHING,
2547 .arg2_type = ARG_ANYTHING,
2550 BPF_CALL_4(bpf_redirect_neigh, u32, ifindex, struct bpf_redir_neigh *, params,
2551 int, plen, u64, flags)
2553 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
2555 if (unlikely((plen && plen < sizeof(*params)) || flags))
2558 ri->flags = BPF_F_NEIGH | (plen ? BPF_F_NEXTHOP : 0);
2559 ri->tgt_index = ifindex;
2561 BUILD_BUG_ON(sizeof(struct bpf_redir_neigh) != sizeof(struct bpf_nh_params));
2563 memcpy(&ri->nh, params, sizeof(ri->nh));
2565 return TC_ACT_REDIRECT;
2568 static const struct bpf_func_proto bpf_redirect_neigh_proto = {
2569 .func = bpf_redirect_neigh,
2571 .ret_type = RET_INTEGER,
2572 .arg1_type = ARG_ANYTHING,
2573 .arg2_type = ARG_PTR_TO_MEM_OR_NULL,
2574 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
2575 .arg4_type = ARG_ANYTHING,
2578 BPF_CALL_2(bpf_msg_apply_bytes, struct sk_msg *, msg, u32, bytes)
2580 msg->apply_bytes = bytes;
2584 static const struct bpf_func_proto bpf_msg_apply_bytes_proto = {
2585 .func = bpf_msg_apply_bytes,
2587 .ret_type = RET_INTEGER,
2588 .arg1_type = ARG_PTR_TO_CTX,
2589 .arg2_type = ARG_ANYTHING,
2592 BPF_CALL_2(bpf_msg_cork_bytes, struct sk_msg *, msg, u32, bytes)
2594 msg->cork_bytes = bytes;
2598 static void sk_msg_reset_curr(struct sk_msg *msg)
2600 u32 i = msg->sg.start;
2604 len += sk_msg_elem(msg, i)->length;
2605 sk_msg_iter_var_next(i);
2606 if (len >= msg->sg.size)
2608 } while (i != msg->sg.end);
2611 msg->sg.copybreak = 0;
2614 static const struct bpf_func_proto bpf_msg_cork_bytes_proto = {
2615 .func = bpf_msg_cork_bytes,
2617 .ret_type = RET_INTEGER,
2618 .arg1_type = ARG_PTR_TO_CTX,
2619 .arg2_type = ARG_ANYTHING,
2622 BPF_CALL_4(bpf_msg_pull_data, struct sk_msg *, msg, u32, start,
2623 u32, end, u64, flags)
2625 u32 len = 0, offset = 0, copy = 0, poffset = 0, bytes = end - start;
2626 u32 first_sge, last_sge, i, shift, bytes_sg_total;
2627 struct scatterlist *sge;
2628 u8 *raw, *to, *from;
2631 if (unlikely(flags || end <= start))
2634 /* First find the starting scatterlist element */
2638 len = sk_msg_elem(msg, i)->length;
2639 if (start < offset + len)
2641 sk_msg_iter_var_next(i);
2642 } while (i != msg->sg.end);
2644 if (unlikely(start >= offset + len))
2648 /* The start may point into the sg element so we need to also
2649 * account for the headroom.
2651 bytes_sg_total = start - offset + bytes;
2652 if (!test_bit(i, &msg->sg.copy) && bytes_sg_total <= len)
2655 /* At this point we need to linearize multiple scatterlist
2656 * elements or a single shared page. Either way we need to
2657 * copy into a linear buffer exclusively owned by BPF. Then
2658 * place the buffer in the scatterlist and fixup the original
2659 * entries by removing the entries now in the linear buffer
2660 * and shifting the remaining entries. For now we do not try
2661 * to copy partial entries to avoid complexity of running out
2662 * of sg_entry slots. The downside is reading a single byte
2663 * will copy the entire sg entry.
2666 copy += sk_msg_elem(msg, i)->length;
2667 sk_msg_iter_var_next(i);
2668 if (bytes_sg_total <= copy)
2670 } while (i != msg->sg.end);
2673 if (unlikely(bytes_sg_total > copy))
2676 page = alloc_pages(__GFP_NOWARN | GFP_ATOMIC | __GFP_COMP,
2678 if (unlikely(!page))
2681 raw = page_address(page);
2684 sge = sk_msg_elem(msg, i);
2685 from = sg_virt(sge);
2689 memcpy(to, from, len);
2692 put_page(sg_page(sge));
2694 sk_msg_iter_var_next(i);
2695 } while (i != last_sge);
2697 sg_set_page(&msg->sg.data[first_sge], page, copy, 0);
2699 /* To repair sg ring we need to shift entries. If we only
2700 * had a single entry though we can just replace it and
2701 * be done. Otherwise walk the ring and shift the entries.
2703 WARN_ON_ONCE(last_sge == first_sge);
2704 shift = last_sge > first_sge ?
2705 last_sge - first_sge - 1 :
2706 NR_MSG_FRAG_IDS - first_sge + last_sge - 1;
2711 sk_msg_iter_var_next(i);
2715 if (i + shift >= NR_MSG_FRAG_IDS)
2716 move_from = i + shift - NR_MSG_FRAG_IDS;
2718 move_from = i + shift;
2719 if (move_from == msg->sg.end)
2722 msg->sg.data[i] = msg->sg.data[move_from];
2723 msg->sg.data[move_from].length = 0;
2724 msg->sg.data[move_from].page_link = 0;
2725 msg->sg.data[move_from].offset = 0;
2726 sk_msg_iter_var_next(i);
2729 msg->sg.end = msg->sg.end - shift > msg->sg.end ?
2730 msg->sg.end - shift + NR_MSG_FRAG_IDS :
2731 msg->sg.end - shift;
2733 sk_msg_reset_curr(msg);
2734 msg->data = sg_virt(&msg->sg.data[first_sge]) + start - offset;
2735 msg->data_end = msg->data + bytes;
2739 static const struct bpf_func_proto bpf_msg_pull_data_proto = {
2740 .func = bpf_msg_pull_data,
2742 .ret_type = RET_INTEGER,
2743 .arg1_type = ARG_PTR_TO_CTX,
2744 .arg2_type = ARG_ANYTHING,
2745 .arg3_type = ARG_ANYTHING,
2746 .arg4_type = ARG_ANYTHING,
2749 BPF_CALL_4(bpf_msg_push_data, struct sk_msg *, msg, u32, start,
2750 u32, len, u64, flags)
2752 struct scatterlist sge, nsge, nnsge, rsge = {0}, *psge;
2753 u32 new, i = 0, l = 0, space, copy = 0, offset = 0;
2754 u8 *raw, *to, *from;
2757 if (unlikely(flags))
2760 if (unlikely(len == 0))
2763 /* First find the starting scatterlist element */
2767 l = sk_msg_elem(msg, i)->length;
2769 if (start < offset + l)
2771 sk_msg_iter_var_next(i);
2772 } while (i != msg->sg.end);
2774 if (start >= offset + l)
2777 space = MAX_MSG_FRAGS - sk_msg_elem_used(msg);
2779 /* If no space available will fallback to copy, we need at
2780 * least one scatterlist elem available to push data into
2781 * when start aligns to the beginning of an element or two
2782 * when it falls inside an element. We handle the start equals
2783 * offset case because its the common case for inserting a
2786 if (!space || (space == 1 && start != offset))
2787 copy = msg->sg.data[i].length;
2789 page = alloc_pages(__GFP_NOWARN | GFP_ATOMIC | __GFP_COMP,
2790 get_order(copy + len));
2791 if (unlikely(!page))
2797 raw = page_address(page);
2799 psge = sk_msg_elem(msg, i);
2800 front = start - offset;
2801 back = psge->length - front;
2802 from = sg_virt(psge);
2805 memcpy(raw, from, front);
2809 to = raw + front + len;
2811 memcpy(to, from, back);
2814 put_page(sg_page(psge));
2815 } else if (start - offset) {
2816 psge = sk_msg_elem(msg, i);
2817 rsge = sk_msg_elem_cpy(msg, i);
2819 psge->length = start - offset;
2820 rsge.length -= psge->length;
2821 rsge.offset += start;
2823 sk_msg_iter_var_next(i);
2824 sg_unmark_end(psge);
2825 sg_unmark_end(&rsge);
2826 sk_msg_iter_next(msg, end);
2829 /* Slot(s) to place newly allocated data */
2832 /* Shift one or two slots as needed */
2834 sge = sk_msg_elem_cpy(msg, i);
2836 sk_msg_iter_var_next(i);
2837 sg_unmark_end(&sge);
2838 sk_msg_iter_next(msg, end);
2840 nsge = sk_msg_elem_cpy(msg, i);
2842 sk_msg_iter_var_next(i);
2843 nnsge = sk_msg_elem_cpy(msg, i);
2846 while (i != msg->sg.end) {
2847 msg->sg.data[i] = sge;
2849 sk_msg_iter_var_next(i);
2852 nnsge = sk_msg_elem_cpy(msg, i);
2854 nsge = sk_msg_elem_cpy(msg, i);
2859 /* Place newly allocated data buffer */
2860 sk_mem_charge(msg->sk, len);
2861 msg->sg.size += len;
2862 __clear_bit(new, &msg->sg.copy);
2863 sg_set_page(&msg->sg.data[new], page, len + copy, 0);
2865 get_page(sg_page(&rsge));
2866 sk_msg_iter_var_next(new);
2867 msg->sg.data[new] = rsge;
2870 sk_msg_reset_curr(msg);
2871 sk_msg_compute_data_pointers(msg);
2875 static const struct bpf_func_proto bpf_msg_push_data_proto = {
2876 .func = bpf_msg_push_data,
2878 .ret_type = RET_INTEGER,
2879 .arg1_type = ARG_PTR_TO_CTX,
2880 .arg2_type = ARG_ANYTHING,
2881 .arg3_type = ARG_ANYTHING,
2882 .arg4_type = ARG_ANYTHING,
2885 static void sk_msg_shift_left(struct sk_msg *msg, int i)
2891 sk_msg_iter_var_next(i);
2892 msg->sg.data[prev] = msg->sg.data[i];
2893 } while (i != msg->sg.end);
2895 sk_msg_iter_prev(msg, end);
2898 static void sk_msg_shift_right(struct sk_msg *msg, int i)
2900 struct scatterlist tmp, sge;
2902 sk_msg_iter_next(msg, end);
2903 sge = sk_msg_elem_cpy(msg, i);
2904 sk_msg_iter_var_next(i);
2905 tmp = sk_msg_elem_cpy(msg, i);
2907 while (i != msg->sg.end) {
2908 msg->sg.data[i] = sge;
2909 sk_msg_iter_var_next(i);
2911 tmp = sk_msg_elem_cpy(msg, i);
2915 BPF_CALL_4(bpf_msg_pop_data, struct sk_msg *, msg, u32, start,
2916 u32, len, u64, flags)
2918 u32 i = 0, l = 0, space, offset = 0;
2919 u64 last = start + len;
2922 if (unlikely(flags))
2925 /* First find the starting scatterlist element */
2929 l = sk_msg_elem(msg, i)->length;
2931 if (start < offset + l)
2933 sk_msg_iter_var_next(i);
2934 } while (i != msg->sg.end);
2936 /* Bounds checks: start and pop must be inside message */
2937 if (start >= offset + l || last >= msg->sg.size)
2940 space = MAX_MSG_FRAGS - sk_msg_elem_used(msg);
2943 /* --------------| offset
2944 * -| start |-------- len -------|
2946 * |----- a ----|-------- pop -------|----- b ----|
2947 * |______________________________________________| length
2950 * a: region at front of scatter element to save
2951 * b: region at back of scatter element to save when length > A + pop
2952 * pop: region to pop from element, same as input 'pop' here will be
2953 * decremented below per iteration.
2955 * Two top-level cases to handle when start != offset, first B is non
2956 * zero and second B is zero corresponding to when a pop includes more
2959 * Then if B is non-zero AND there is no space allocate space and
2960 * compact A, B regions into page. If there is space shift ring to
2961 * the rigth free'ing the next element in ring to place B, leaving
2962 * A untouched except to reduce length.
2964 if (start != offset) {
2965 struct scatterlist *nsge, *sge = sk_msg_elem(msg, i);
2967 int b = sge->length - pop - a;
2969 sk_msg_iter_var_next(i);
2971 if (pop < sge->length - a) {
2974 sk_msg_shift_right(msg, i);
2975 nsge = sk_msg_elem(msg, i);
2976 get_page(sg_page(sge));
2979 b, sge->offset + pop + a);
2981 struct page *page, *orig;
2984 page = alloc_pages(__GFP_NOWARN |
2985 __GFP_COMP | GFP_ATOMIC,
2987 if (unlikely(!page))
2991 orig = sg_page(sge);
2992 from = sg_virt(sge);
2993 to = page_address(page);
2994 memcpy(to, from, a);
2995 memcpy(to + a, from + a + pop, b);
2996 sg_set_page(sge, page, a + b, 0);
3000 } else if (pop >= sge->length - a) {
3001 pop -= (sge->length - a);
3006 /* From above the current layout _must_ be as follows,
3011 * |---- pop ---|---------------- b ------------|
3012 * |____________________________________________| length
3014 * Offset and start of the current msg elem are equal because in the
3015 * previous case we handled offset != start and either consumed the
3016 * entire element and advanced to the next element OR pop == 0.
3018 * Two cases to handle here are first pop is less than the length
3019 * leaving some remainder b above. Simply adjust the element's layout
3020 * in this case. Or pop >= length of the element so that b = 0. In this
3021 * case advance to next element decrementing pop.
3024 struct scatterlist *sge = sk_msg_elem(msg, i);
3026 if (pop < sge->length) {
3032 sk_msg_shift_left(msg, i);
3034 sk_msg_iter_var_next(i);
3037 sk_mem_uncharge(msg->sk, len - pop);
3038 msg->sg.size -= (len - pop);
3039 sk_msg_reset_curr(msg);
3040 sk_msg_compute_data_pointers(msg);
3044 static const struct bpf_func_proto bpf_msg_pop_data_proto = {
3045 .func = bpf_msg_pop_data,
3047 .ret_type = RET_INTEGER,
3048 .arg1_type = ARG_PTR_TO_CTX,
3049 .arg2_type = ARG_ANYTHING,
3050 .arg3_type = ARG_ANYTHING,
3051 .arg4_type = ARG_ANYTHING,
3054 #ifdef CONFIG_CGROUP_NET_CLASSID
3055 BPF_CALL_0(bpf_get_cgroup_classid_curr)
3057 return __task_get_classid(current);
3060 static const struct bpf_func_proto bpf_get_cgroup_classid_curr_proto = {
3061 .func = bpf_get_cgroup_classid_curr,
3063 .ret_type = RET_INTEGER,
3066 BPF_CALL_1(bpf_skb_cgroup_classid, const struct sk_buff *, skb)
3068 struct sock *sk = skb_to_full_sk(skb);
3070 if (!sk || !sk_fullsock(sk))
3073 return sock_cgroup_classid(&sk->sk_cgrp_data);
3076 static const struct bpf_func_proto bpf_skb_cgroup_classid_proto = {
3077 .func = bpf_skb_cgroup_classid,
3079 .ret_type = RET_INTEGER,
3080 .arg1_type = ARG_PTR_TO_CTX,
3084 BPF_CALL_1(bpf_get_cgroup_classid, const struct sk_buff *, skb)
3086 return task_get_classid(skb);
3089 static const struct bpf_func_proto bpf_get_cgroup_classid_proto = {
3090 .func = bpf_get_cgroup_classid,
3092 .ret_type = RET_INTEGER,
3093 .arg1_type = ARG_PTR_TO_CTX,
3096 BPF_CALL_1(bpf_get_route_realm, const struct sk_buff *, skb)
3098 return dst_tclassid(skb);
3101 static const struct bpf_func_proto bpf_get_route_realm_proto = {
3102 .func = bpf_get_route_realm,
3104 .ret_type = RET_INTEGER,
3105 .arg1_type = ARG_PTR_TO_CTX,
3108 BPF_CALL_1(bpf_get_hash_recalc, struct sk_buff *, skb)
3110 /* If skb_clear_hash() was called due to mangling, we can
3111 * trigger SW recalculation here. Later access to hash
3112 * can then use the inline skb->hash via context directly
3113 * instead of calling this helper again.
3115 return skb_get_hash(skb);
3118 static const struct bpf_func_proto bpf_get_hash_recalc_proto = {
3119 .func = bpf_get_hash_recalc,
3121 .ret_type = RET_INTEGER,
3122 .arg1_type = ARG_PTR_TO_CTX,
3125 BPF_CALL_1(bpf_set_hash_invalid, struct sk_buff *, skb)
3127 /* After all direct packet write, this can be used once for
3128 * triggering a lazy recalc on next skb_get_hash() invocation.
3130 skb_clear_hash(skb);
3134 static const struct bpf_func_proto bpf_set_hash_invalid_proto = {
3135 .func = bpf_set_hash_invalid,
3137 .ret_type = RET_INTEGER,
3138 .arg1_type = ARG_PTR_TO_CTX,
3141 BPF_CALL_2(bpf_set_hash, struct sk_buff *, skb, u32, hash)
3143 /* Set user specified hash as L4(+), so that it gets returned
3144 * on skb_get_hash() call unless BPF prog later on triggers a
3147 __skb_set_sw_hash(skb, hash, true);
3151 static const struct bpf_func_proto bpf_set_hash_proto = {
3152 .func = bpf_set_hash,
3154 .ret_type = RET_INTEGER,
3155 .arg1_type = ARG_PTR_TO_CTX,
3156 .arg2_type = ARG_ANYTHING,
3159 BPF_CALL_3(bpf_skb_vlan_push, struct sk_buff *, skb, __be16, vlan_proto,
3164 if (unlikely(vlan_proto != htons(ETH_P_8021Q) &&
3165 vlan_proto != htons(ETH_P_8021AD)))
3166 vlan_proto = htons(ETH_P_8021Q);
3168 bpf_push_mac_rcsum(skb);
3169 ret = skb_vlan_push(skb, vlan_proto, vlan_tci);
3170 bpf_pull_mac_rcsum(skb);
3172 bpf_compute_data_pointers(skb);
3176 static const struct bpf_func_proto bpf_skb_vlan_push_proto = {
3177 .func = bpf_skb_vlan_push,
3179 .ret_type = RET_INTEGER,
3180 .arg1_type = ARG_PTR_TO_CTX,
3181 .arg2_type = ARG_ANYTHING,
3182 .arg3_type = ARG_ANYTHING,
3185 BPF_CALL_1(bpf_skb_vlan_pop, struct sk_buff *, skb)
3189 bpf_push_mac_rcsum(skb);
3190 ret = skb_vlan_pop(skb);
3191 bpf_pull_mac_rcsum(skb);
3193 bpf_compute_data_pointers(skb);
3197 static const struct bpf_func_proto bpf_skb_vlan_pop_proto = {
3198 .func = bpf_skb_vlan_pop,
3200 .ret_type = RET_INTEGER,
3201 .arg1_type = ARG_PTR_TO_CTX,
3204 static int bpf_skb_generic_push(struct sk_buff *skb, u32 off, u32 len)
3206 /* Caller already did skb_cow() with len as headroom,
3207 * so no need to do it here.
3210 memmove(skb->data, skb->data + len, off);
3211 memset(skb->data + off, 0, len);
3213 /* No skb_postpush_rcsum(skb, skb->data + off, len)
3214 * needed here as it does not change the skb->csum
3215 * result for checksum complete when summing over
3221 static int bpf_skb_generic_pop(struct sk_buff *skb, u32 off, u32 len)
3225 /* skb_ensure_writable() is not needed here, as we're
3226 * already working on an uncloned skb.
3228 if (unlikely(!pskb_may_pull(skb, off + len)))
3231 old_data = skb->data;
3232 __skb_pull(skb, len);
3233 skb_postpull_rcsum(skb, old_data + off, len);
3234 memmove(skb->data, old_data, off);
3239 static int bpf_skb_net_hdr_push(struct sk_buff *skb, u32 off, u32 len)
3241 bool trans_same = skb->transport_header == skb->network_header;
3244 /* There's no need for __skb_push()/__skb_pull() pair to
3245 * get to the start of the mac header as we're guaranteed
3246 * to always start from here under eBPF.
3248 ret = bpf_skb_generic_push(skb, off, len);
3250 skb->mac_header -= len;
3251 skb->network_header -= len;
3253 skb->transport_header = skb->network_header;
3259 static int bpf_skb_net_hdr_pop(struct sk_buff *skb, u32 off, u32 len)
3261 bool trans_same = skb->transport_header == skb->network_header;
3264 /* Same here, __skb_push()/__skb_pull() pair not needed. */
3265 ret = bpf_skb_generic_pop(skb, off, len);
3267 skb->mac_header += len;
3268 skb->network_header += len;
3270 skb->transport_header = skb->network_header;
3276 static int bpf_skb_proto_4_to_6(struct sk_buff *skb)
3278 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
3279 u32 off = skb_mac_header_len(skb);
3282 if (skb_is_gso(skb) && !skb_is_gso_tcp(skb))
3285 ret = skb_cow(skb, len_diff);
3286 if (unlikely(ret < 0))
3289 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
3290 if (unlikely(ret < 0))
3293 if (skb_is_gso(skb)) {
3294 struct skb_shared_info *shinfo = skb_shinfo(skb);
3296 /* SKB_GSO_TCPV4 needs to be changed into
3299 if (shinfo->gso_type & SKB_GSO_TCPV4) {
3300 shinfo->gso_type &= ~SKB_GSO_TCPV4;
3301 shinfo->gso_type |= SKB_GSO_TCPV6;
3304 /* Header must be checked, and gso_segs recomputed. */
3305 shinfo->gso_type |= SKB_GSO_DODGY;
3306 shinfo->gso_segs = 0;
3309 skb->protocol = htons(ETH_P_IPV6);
3310 skb_clear_hash(skb);
3315 static int bpf_skb_proto_6_to_4(struct sk_buff *skb)
3317 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
3318 u32 off = skb_mac_header_len(skb);
3321 if (skb_is_gso(skb) && !skb_is_gso_tcp(skb))
3324 ret = skb_unclone(skb, GFP_ATOMIC);
3325 if (unlikely(ret < 0))
3328 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
3329 if (unlikely(ret < 0))
3332 if (skb_is_gso(skb)) {
3333 struct skb_shared_info *shinfo = skb_shinfo(skb);
3335 /* SKB_GSO_TCPV6 needs to be changed into
3338 if (shinfo->gso_type & SKB_GSO_TCPV6) {
3339 shinfo->gso_type &= ~SKB_GSO_TCPV6;
3340 shinfo->gso_type |= SKB_GSO_TCPV4;
3343 /* Header must be checked, and gso_segs recomputed. */
3344 shinfo->gso_type |= SKB_GSO_DODGY;
3345 shinfo->gso_segs = 0;
3348 skb->protocol = htons(ETH_P_IP);
3349 skb_clear_hash(skb);
3354 static int bpf_skb_proto_xlat(struct sk_buff *skb, __be16 to_proto)
3356 __be16 from_proto = skb->protocol;
3358 if (from_proto == htons(ETH_P_IP) &&
3359 to_proto == htons(ETH_P_IPV6))
3360 return bpf_skb_proto_4_to_6(skb);
3362 if (from_proto == htons(ETH_P_IPV6) &&
3363 to_proto == htons(ETH_P_IP))
3364 return bpf_skb_proto_6_to_4(skb);
3369 BPF_CALL_3(bpf_skb_change_proto, struct sk_buff *, skb, __be16, proto,
3374 if (unlikely(flags))
3377 /* General idea is that this helper does the basic groundwork
3378 * needed for changing the protocol, and eBPF program fills the
3379 * rest through bpf_skb_store_bytes(), bpf_lX_csum_replace()
3380 * and other helpers, rather than passing a raw buffer here.
3382 * The rationale is to keep this minimal and without a need to
3383 * deal with raw packet data. F.e. even if we would pass buffers
3384 * here, the program still needs to call the bpf_lX_csum_replace()
3385 * helpers anyway. Plus, this way we keep also separation of
3386 * concerns, since f.e. bpf_skb_store_bytes() should only take
3389 * Currently, additional options and extension header space are
3390 * not supported, but flags register is reserved so we can adapt
3391 * that. For offloads, we mark packet as dodgy, so that headers
3392 * need to be verified first.
3394 ret = bpf_skb_proto_xlat(skb, proto);
3395 bpf_compute_data_pointers(skb);
3399 static const struct bpf_func_proto bpf_skb_change_proto_proto = {
3400 .func = bpf_skb_change_proto,
3402 .ret_type = RET_INTEGER,
3403 .arg1_type = ARG_PTR_TO_CTX,
3404 .arg2_type = ARG_ANYTHING,
3405 .arg3_type = ARG_ANYTHING,
3408 BPF_CALL_2(bpf_skb_change_type, struct sk_buff *, skb, u32, pkt_type)
3410 /* We only allow a restricted subset to be changed for now. */
3411 if (unlikely(!skb_pkt_type_ok(skb->pkt_type) ||
3412 !skb_pkt_type_ok(pkt_type)))
3415 skb->pkt_type = pkt_type;
3419 static const struct bpf_func_proto bpf_skb_change_type_proto = {
3420 .func = bpf_skb_change_type,
3422 .ret_type = RET_INTEGER,
3423 .arg1_type = ARG_PTR_TO_CTX,
3424 .arg2_type = ARG_ANYTHING,
3427 static u32 bpf_skb_net_base_len(const struct sk_buff *skb)
3429 switch (skb->protocol) {
3430 case htons(ETH_P_IP):
3431 return sizeof(struct iphdr);
3432 case htons(ETH_P_IPV6):
3433 return sizeof(struct ipv6hdr);
3439 #define BPF_F_ADJ_ROOM_ENCAP_L3_MASK (BPF_F_ADJ_ROOM_ENCAP_L3_IPV4 | \
3440 BPF_F_ADJ_ROOM_ENCAP_L3_IPV6)
3442 #define BPF_F_ADJ_ROOM_MASK (BPF_F_ADJ_ROOM_FIXED_GSO | \
3443 BPF_F_ADJ_ROOM_ENCAP_L3_MASK | \
3444 BPF_F_ADJ_ROOM_ENCAP_L4_GRE | \
3445 BPF_F_ADJ_ROOM_ENCAP_L4_UDP | \
3446 BPF_F_ADJ_ROOM_ENCAP_L2( \
3447 BPF_ADJ_ROOM_ENCAP_L2_MASK))
3449 static int bpf_skb_net_grow(struct sk_buff *skb, u32 off, u32 len_diff,
3452 u8 inner_mac_len = flags >> BPF_ADJ_ROOM_ENCAP_L2_SHIFT;
3453 bool encap = flags & BPF_F_ADJ_ROOM_ENCAP_L3_MASK;
3454 u16 mac_len = 0, inner_net = 0, inner_trans = 0;
3455 unsigned int gso_type = SKB_GSO_DODGY;
3458 if (skb_is_gso(skb) && !skb_is_gso_tcp(skb)) {
3459 /* udp gso_size delineates datagrams, only allow if fixed */
3460 if (!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) ||
3461 !(flags & BPF_F_ADJ_ROOM_FIXED_GSO))
3465 ret = skb_cow_head(skb, len_diff);
3466 if (unlikely(ret < 0))
3470 if (skb->protocol != htons(ETH_P_IP) &&
3471 skb->protocol != htons(ETH_P_IPV6))
3474 if (flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV4 &&
3475 flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV6)
3478 if (flags & BPF_F_ADJ_ROOM_ENCAP_L4_GRE &&
3479 flags & BPF_F_ADJ_ROOM_ENCAP_L4_UDP)
3482 if (skb->encapsulation)
3485 mac_len = skb->network_header - skb->mac_header;
3486 inner_net = skb->network_header;
3487 if (inner_mac_len > len_diff)
3489 inner_trans = skb->transport_header;
3492 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
3493 if (unlikely(ret < 0))
3497 skb->inner_mac_header = inner_net - inner_mac_len;
3498 skb->inner_network_header = inner_net;
3499 skb->inner_transport_header = inner_trans;
3500 skb_set_inner_protocol(skb, skb->protocol);
3502 skb->encapsulation = 1;
3503 skb_set_network_header(skb, mac_len);
3505 if (flags & BPF_F_ADJ_ROOM_ENCAP_L4_UDP)
3506 gso_type |= SKB_GSO_UDP_TUNNEL;
3507 else if (flags & BPF_F_ADJ_ROOM_ENCAP_L4_GRE)
3508 gso_type |= SKB_GSO_GRE;
3509 else if (flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV6)
3510 gso_type |= SKB_GSO_IPXIP6;
3511 else if (flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV4)
3512 gso_type |= SKB_GSO_IPXIP4;
3514 if (flags & BPF_F_ADJ_ROOM_ENCAP_L4_GRE ||
3515 flags & BPF_F_ADJ_ROOM_ENCAP_L4_UDP) {
3516 int nh_len = flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV6 ?
3517 sizeof(struct ipv6hdr) :
3518 sizeof(struct iphdr);
3520 skb_set_transport_header(skb, mac_len + nh_len);
3523 /* Match skb->protocol to new outer l3 protocol */
3524 if (skb->protocol == htons(ETH_P_IP) &&
3525 flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV6)
3526 skb->protocol = htons(ETH_P_IPV6);
3527 else if (skb->protocol == htons(ETH_P_IPV6) &&
3528 flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV4)
3529 skb->protocol = htons(ETH_P_IP);
3532 if (skb_is_gso(skb)) {
3533 struct skb_shared_info *shinfo = skb_shinfo(skb);
3535 /* Due to header grow, MSS needs to be downgraded. */
3536 if (!(flags & BPF_F_ADJ_ROOM_FIXED_GSO))
3537 skb_decrease_gso_size(shinfo, len_diff);
3539 /* Header must be checked, and gso_segs recomputed. */
3540 shinfo->gso_type |= gso_type;
3541 shinfo->gso_segs = 0;
3547 static int bpf_skb_net_shrink(struct sk_buff *skb, u32 off, u32 len_diff,
3552 if (unlikely(flags & ~(BPF_F_ADJ_ROOM_FIXED_GSO |
3553 BPF_F_ADJ_ROOM_NO_CSUM_RESET)))
3556 if (skb_is_gso(skb) && !skb_is_gso_tcp(skb)) {
3557 /* udp gso_size delineates datagrams, only allow if fixed */
3558 if (!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) ||
3559 !(flags & BPF_F_ADJ_ROOM_FIXED_GSO))
3563 ret = skb_unclone(skb, GFP_ATOMIC);
3564 if (unlikely(ret < 0))
3567 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
3568 if (unlikely(ret < 0))
3571 if (skb_is_gso(skb)) {
3572 struct skb_shared_info *shinfo = skb_shinfo(skb);
3574 /* Due to header shrink, MSS can be upgraded. */
3575 if (!(flags & BPF_F_ADJ_ROOM_FIXED_GSO))
3576 skb_increase_gso_size(shinfo, len_diff);
3578 /* Header must be checked, and gso_segs recomputed. */
3579 shinfo->gso_type |= SKB_GSO_DODGY;
3580 shinfo->gso_segs = 0;
3586 #define BPF_SKB_MAX_LEN SKB_MAX_ALLOC
3588 BPF_CALL_4(sk_skb_adjust_room, struct sk_buff *, skb, s32, len_diff,
3589 u32, mode, u64, flags)
3591 u32 len_diff_abs = abs(len_diff);
3592 bool shrink = len_diff < 0;
3595 if (unlikely(flags || mode))
3597 if (unlikely(len_diff_abs > 0xfffU))
3601 ret = skb_cow(skb, len_diff);
3602 if (unlikely(ret < 0))
3604 __skb_push(skb, len_diff_abs);
3605 memset(skb->data, 0, len_diff_abs);
3607 if (unlikely(!pskb_may_pull(skb, len_diff_abs)))
3609 __skb_pull(skb, len_diff_abs);
3611 bpf_compute_data_end_sk_skb(skb);
3612 if (tls_sw_has_ctx_rx(skb->sk)) {
3613 struct strp_msg *rxm = strp_msg(skb);
3615 rxm->full_len += len_diff;
3620 static const struct bpf_func_proto sk_skb_adjust_room_proto = {
3621 .func = sk_skb_adjust_room,
3623 .ret_type = RET_INTEGER,
3624 .arg1_type = ARG_PTR_TO_CTX,
3625 .arg2_type = ARG_ANYTHING,
3626 .arg3_type = ARG_ANYTHING,
3627 .arg4_type = ARG_ANYTHING,
3630 BPF_CALL_4(bpf_skb_adjust_room, struct sk_buff *, skb, s32, len_diff,
3631 u32, mode, u64, flags)
3633 u32 len_cur, len_diff_abs = abs(len_diff);
3634 u32 len_min = bpf_skb_net_base_len(skb);
3635 u32 len_max = BPF_SKB_MAX_LEN;
3636 __be16 proto = skb->protocol;
3637 bool shrink = len_diff < 0;
3641 if (unlikely(flags & ~(BPF_F_ADJ_ROOM_MASK |
3642 BPF_F_ADJ_ROOM_NO_CSUM_RESET)))
3644 if (unlikely(len_diff_abs > 0xfffU))
3646 if (unlikely(proto != htons(ETH_P_IP) &&
3647 proto != htons(ETH_P_IPV6)))
3650 off = skb_mac_header_len(skb);
3652 case BPF_ADJ_ROOM_NET:
3653 off += bpf_skb_net_base_len(skb);
3655 case BPF_ADJ_ROOM_MAC:
3661 len_cur = skb->len - skb_network_offset(skb);
3662 if ((shrink && (len_diff_abs >= len_cur ||
3663 len_cur - len_diff_abs < len_min)) ||
3664 (!shrink && (skb->len + len_diff_abs > len_max &&
3668 ret = shrink ? bpf_skb_net_shrink(skb, off, len_diff_abs, flags) :
3669 bpf_skb_net_grow(skb, off, len_diff_abs, flags);
3670 if (!ret && !(flags & BPF_F_ADJ_ROOM_NO_CSUM_RESET))
3671 __skb_reset_checksum_unnecessary(skb);
3673 bpf_compute_data_pointers(skb);
3677 static const struct bpf_func_proto bpf_skb_adjust_room_proto = {
3678 .func = bpf_skb_adjust_room,
3680 .ret_type = RET_INTEGER,
3681 .arg1_type = ARG_PTR_TO_CTX,
3682 .arg2_type = ARG_ANYTHING,
3683 .arg3_type = ARG_ANYTHING,
3684 .arg4_type = ARG_ANYTHING,
3687 static u32 __bpf_skb_min_len(const struct sk_buff *skb)
3689 u32 min_len = skb_network_offset(skb);
3691 if (skb_transport_header_was_set(skb))
3692 min_len = skb_transport_offset(skb);
3693 if (skb->ip_summed == CHECKSUM_PARTIAL)
3694 min_len = skb_checksum_start_offset(skb) +
3695 skb->csum_offset + sizeof(__sum16);
3699 static int bpf_skb_grow_rcsum(struct sk_buff *skb, unsigned int new_len)
3701 unsigned int old_len = skb->len;
3704 ret = __skb_grow_rcsum(skb, new_len);
3706 memset(skb->data + old_len, 0, new_len - old_len);
3710 static int bpf_skb_trim_rcsum(struct sk_buff *skb, unsigned int new_len)
3712 return __skb_trim_rcsum(skb, new_len);
3715 static inline int __bpf_skb_change_tail(struct sk_buff *skb, u32 new_len,
3718 u32 max_len = BPF_SKB_MAX_LEN;
3719 u32 min_len = __bpf_skb_min_len(skb);
3722 if (unlikely(flags || new_len > max_len || new_len < min_len))
3724 if (skb->encapsulation)
3727 /* The basic idea of this helper is that it's performing the
3728 * needed work to either grow or trim an skb, and eBPF program
3729 * rewrites the rest via helpers like bpf_skb_store_bytes(),
3730 * bpf_lX_csum_replace() and others rather than passing a raw
3731 * buffer here. This one is a slow path helper and intended
3732 * for replies with control messages.
3734 * Like in bpf_skb_change_proto(), we want to keep this rather
3735 * minimal and without protocol specifics so that we are able
3736 * to separate concerns as in bpf_skb_store_bytes() should only
3737 * be the one responsible for writing buffers.
3739 * It's really expected to be a slow path operation here for
3740 * control message replies, so we're implicitly linearizing,
3741 * uncloning and drop offloads from the skb by this.
3743 ret = __bpf_try_make_writable(skb, skb->len);
3745 if (new_len > skb->len)
3746 ret = bpf_skb_grow_rcsum(skb, new_len);
3747 else if (new_len < skb->len)
3748 ret = bpf_skb_trim_rcsum(skb, new_len);
3749 if (!ret && skb_is_gso(skb))
3755 BPF_CALL_3(bpf_skb_change_tail, struct sk_buff *, skb, u32, new_len,
3758 int ret = __bpf_skb_change_tail(skb, new_len, flags);
3760 bpf_compute_data_pointers(skb);
3764 static const struct bpf_func_proto bpf_skb_change_tail_proto = {
3765 .func = bpf_skb_change_tail,
3767 .ret_type = RET_INTEGER,
3768 .arg1_type = ARG_PTR_TO_CTX,
3769 .arg2_type = ARG_ANYTHING,
3770 .arg3_type = ARG_ANYTHING,
3773 BPF_CALL_3(sk_skb_change_tail, struct sk_buff *, skb, u32, new_len,
3776 int ret = __bpf_skb_change_tail(skb, new_len, flags);
3778 bpf_compute_data_end_sk_skb(skb);
3782 static const struct bpf_func_proto sk_skb_change_tail_proto = {
3783 .func = sk_skb_change_tail,
3785 .ret_type = RET_INTEGER,
3786 .arg1_type = ARG_PTR_TO_CTX,
3787 .arg2_type = ARG_ANYTHING,
3788 .arg3_type = ARG_ANYTHING,
3791 static inline int __bpf_skb_change_head(struct sk_buff *skb, u32 head_room,
3794 u32 max_len = BPF_SKB_MAX_LEN;
3795 u32 new_len = skb->len + head_room;
3798 if (unlikely(flags || (!skb_is_gso(skb) && new_len > max_len) ||
3799 new_len < skb->len))
3802 ret = skb_cow(skb, head_room);
3804 /* Idea for this helper is that we currently only
3805 * allow to expand on mac header. This means that
3806 * skb->protocol network header, etc, stay as is.
3807 * Compared to bpf_skb_change_tail(), we're more
3808 * flexible due to not needing to linearize or
3809 * reset GSO. Intention for this helper is to be
3810 * used by an L3 skb that needs to push mac header
3811 * for redirection into L2 device.
3813 __skb_push(skb, head_room);
3814 memset(skb->data, 0, head_room);
3815 skb_reset_mac_header(skb);
3816 skb_reset_mac_len(skb);
3822 BPF_CALL_3(bpf_skb_change_head, struct sk_buff *, skb, u32, head_room,
3825 int ret = __bpf_skb_change_head(skb, head_room, flags);
3827 bpf_compute_data_pointers(skb);
3831 static const struct bpf_func_proto bpf_skb_change_head_proto = {
3832 .func = bpf_skb_change_head,
3834 .ret_type = RET_INTEGER,
3835 .arg1_type = ARG_PTR_TO_CTX,
3836 .arg2_type = ARG_ANYTHING,
3837 .arg3_type = ARG_ANYTHING,
3840 BPF_CALL_3(sk_skb_change_head, struct sk_buff *, skb, u32, head_room,
3843 int ret = __bpf_skb_change_head(skb, head_room, flags);
3845 bpf_compute_data_end_sk_skb(skb);
3849 static const struct bpf_func_proto sk_skb_change_head_proto = {
3850 .func = sk_skb_change_head,
3852 .ret_type = RET_INTEGER,
3853 .arg1_type = ARG_PTR_TO_CTX,
3854 .arg2_type = ARG_ANYTHING,
3855 .arg3_type = ARG_ANYTHING,
3857 static unsigned long xdp_get_metalen(const struct xdp_buff *xdp)
3859 return xdp_data_meta_unsupported(xdp) ? 0 :
3860 xdp->data - xdp->data_meta;
3863 BPF_CALL_2(bpf_xdp_adjust_head, struct xdp_buff *, xdp, int, offset)
3865 void *xdp_frame_end = xdp->data_hard_start + sizeof(struct xdp_frame);
3866 unsigned long metalen = xdp_get_metalen(xdp);
3867 void *data_start = xdp_frame_end + metalen;
3868 void *data = xdp->data + offset;
3870 if (unlikely(data < data_start ||
3871 data > xdp->data_end - ETH_HLEN))
3875 memmove(xdp->data_meta + offset,
3876 xdp->data_meta, metalen);
3877 xdp->data_meta += offset;
3883 static const struct bpf_func_proto bpf_xdp_adjust_head_proto = {
3884 .func = bpf_xdp_adjust_head,
3886 .ret_type = RET_INTEGER,
3887 .arg1_type = ARG_PTR_TO_CTX,
3888 .arg2_type = ARG_ANYTHING,
3891 BPF_CALL_2(bpf_xdp_adjust_tail, struct xdp_buff *, xdp, int, offset)
3893 void *data_hard_end = xdp_data_hard_end(xdp); /* use xdp->frame_sz */
3894 void *data_end = xdp->data_end + offset;
3896 /* Notice that xdp_data_hard_end have reserved some tailroom */
3897 if (unlikely(data_end > data_hard_end))
3900 /* ALL drivers MUST init xdp->frame_sz, chicken check below */
3901 if (unlikely(xdp->frame_sz > PAGE_SIZE)) {
3902 WARN_ONCE(1, "Too BIG xdp->frame_sz = %d\n", xdp->frame_sz);
3906 if (unlikely(data_end < xdp->data + ETH_HLEN))
3909 /* Clear memory area on grow, can contain uninit kernel memory */
3911 memset(xdp->data_end, 0, offset);
3913 xdp->data_end = data_end;
3918 static const struct bpf_func_proto bpf_xdp_adjust_tail_proto = {
3919 .func = bpf_xdp_adjust_tail,
3921 .ret_type = RET_INTEGER,
3922 .arg1_type = ARG_PTR_TO_CTX,
3923 .arg2_type = ARG_ANYTHING,
3926 BPF_CALL_2(bpf_xdp_adjust_meta, struct xdp_buff *, xdp, int, offset)
3928 void *xdp_frame_end = xdp->data_hard_start + sizeof(struct xdp_frame);
3929 void *meta = xdp->data_meta + offset;
3930 unsigned long metalen = xdp->data - meta;
3932 if (xdp_data_meta_unsupported(xdp))
3934 if (unlikely(meta < xdp_frame_end ||
3937 if (unlikely((metalen & (sizeof(__u32) - 1)) ||
3941 xdp->data_meta = meta;
3946 static const struct bpf_func_proto bpf_xdp_adjust_meta_proto = {
3947 .func = bpf_xdp_adjust_meta,
3949 .ret_type = RET_INTEGER,
3950 .arg1_type = ARG_PTR_TO_CTX,
3951 .arg2_type = ARG_ANYTHING,
3954 static int __bpf_tx_xdp_map(struct net_device *dev_rx, void *fwd,
3955 struct bpf_map *map, struct xdp_buff *xdp)
3957 switch (map->map_type) {
3958 case BPF_MAP_TYPE_DEVMAP:
3959 case BPF_MAP_TYPE_DEVMAP_HASH:
3960 return dev_map_enqueue(fwd, xdp, dev_rx);
3961 case BPF_MAP_TYPE_CPUMAP:
3962 return cpu_map_enqueue(fwd, xdp, dev_rx);
3963 case BPF_MAP_TYPE_XSKMAP:
3964 return __xsk_map_redirect(fwd, xdp);
3971 void xdp_do_flush(void)
3977 EXPORT_SYMBOL_GPL(xdp_do_flush);
3979 static inline void *__xdp_map_lookup_elem(struct bpf_map *map, u32 index)
3981 switch (map->map_type) {
3982 case BPF_MAP_TYPE_DEVMAP:
3983 return __dev_map_lookup_elem(map, index);
3984 case BPF_MAP_TYPE_DEVMAP_HASH:
3985 return __dev_map_hash_lookup_elem(map, index);
3986 case BPF_MAP_TYPE_CPUMAP:
3987 return __cpu_map_lookup_elem(map, index);
3988 case BPF_MAP_TYPE_XSKMAP:
3989 return __xsk_map_lookup_elem(map, index);
3995 void bpf_clear_redirect_map(struct bpf_map *map)
3997 struct bpf_redirect_info *ri;
4000 for_each_possible_cpu(cpu) {
4001 ri = per_cpu_ptr(&bpf_redirect_info, cpu);
4002 /* Avoid polluting remote cacheline due to writes if
4003 * not needed. Once we pass this test, we need the
4004 * cmpxchg() to make sure it hasn't been changed in
4005 * the meantime by remote CPU.
4007 if (unlikely(READ_ONCE(ri->map) == map))
4008 cmpxchg(&ri->map, map, NULL);
4012 int xdp_do_redirect(struct net_device *dev, struct xdp_buff *xdp,
4013 struct bpf_prog *xdp_prog)
4015 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
4016 struct bpf_map *map = READ_ONCE(ri->map);
4017 u32 index = ri->tgt_index;
4018 void *fwd = ri->tgt_value;
4022 ri->tgt_value = NULL;
4023 WRITE_ONCE(ri->map, NULL);
4025 if (unlikely(!map)) {
4026 fwd = dev_get_by_index_rcu(dev_net(dev), index);
4027 if (unlikely(!fwd)) {
4032 err = dev_xdp_enqueue(fwd, xdp, dev);
4034 err = __bpf_tx_xdp_map(dev, fwd, map, xdp);
4040 _trace_xdp_redirect_map(dev, xdp_prog, fwd, map, index);
4043 _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map, index, err);
4046 EXPORT_SYMBOL_GPL(xdp_do_redirect);
4048 static int xdp_do_generic_redirect_map(struct net_device *dev,
4049 struct sk_buff *skb,
4050 struct xdp_buff *xdp,
4051 struct bpf_prog *xdp_prog,
4052 struct bpf_map *map)
4054 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
4055 u32 index = ri->tgt_index;
4056 void *fwd = ri->tgt_value;
4060 ri->tgt_value = NULL;
4061 WRITE_ONCE(ri->map, NULL);
4063 if (map->map_type == BPF_MAP_TYPE_DEVMAP ||
4064 map->map_type == BPF_MAP_TYPE_DEVMAP_HASH) {
4065 struct bpf_dtab_netdev *dst = fwd;
4067 err = dev_map_generic_redirect(dst, skb, xdp_prog);
4070 } else if (map->map_type == BPF_MAP_TYPE_XSKMAP) {
4071 struct xdp_sock *xs = fwd;
4073 err = xsk_generic_rcv(xs, xdp);
4078 /* TODO: Handle BPF_MAP_TYPE_CPUMAP */
4083 _trace_xdp_redirect_map(dev, xdp_prog, fwd, map, index);
4086 _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map, index, err);
4090 int xdp_do_generic_redirect(struct net_device *dev, struct sk_buff *skb,
4091 struct xdp_buff *xdp, struct bpf_prog *xdp_prog)
4093 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
4094 struct bpf_map *map = READ_ONCE(ri->map);
4095 u32 index = ri->tgt_index;
4096 struct net_device *fwd;
4100 return xdp_do_generic_redirect_map(dev, skb, xdp, xdp_prog,
4103 fwd = dev_get_by_index_rcu(dev_net(dev), index);
4104 if (unlikely(!fwd)) {
4109 err = xdp_ok_fwd_dev(fwd, skb->len);
4114 _trace_xdp_redirect(dev, xdp_prog, index);
4115 generic_xdp_tx(skb, xdp_prog);
4118 _trace_xdp_redirect_err(dev, xdp_prog, index, err);
4122 BPF_CALL_2(bpf_xdp_redirect, u32, ifindex, u64, flags)
4124 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
4126 if (unlikely(flags))
4130 ri->tgt_index = ifindex;
4131 ri->tgt_value = NULL;
4132 WRITE_ONCE(ri->map, NULL);
4134 return XDP_REDIRECT;
4137 static const struct bpf_func_proto bpf_xdp_redirect_proto = {
4138 .func = bpf_xdp_redirect,
4140 .ret_type = RET_INTEGER,
4141 .arg1_type = ARG_ANYTHING,
4142 .arg2_type = ARG_ANYTHING,
4145 BPF_CALL_3(bpf_xdp_redirect_map, struct bpf_map *, map, u32, ifindex,
4148 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
4150 /* Lower bits of the flags are used as return code on lookup failure */
4151 if (unlikely(flags > XDP_TX))
4154 ri->tgt_value = __xdp_map_lookup_elem(map, ifindex);
4155 if (unlikely(!ri->tgt_value)) {
4156 /* If the lookup fails we want to clear out the state in the
4157 * redirect_info struct completely, so that if an eBPF program
4158 * performs multiple lookups, the last one always takes
4161 WRITE_ONCE(ri->map, NULL);
4166 ri->tgt_index = ifindex;
4167 WRITE_ONCE(ri->map, map);
4169 return XDP_REDIRECT;
4172 static const struct bpf_func_proto bpf_xdp_redirect_map_proto = {
4173 .func = bpf_xdp_redirect_map,
4175 .ret_type = RET_INTEGER,
4176 .arg1_type = ARG_CONST_MAP_PTR,
4177 .arg2_type = ARG_ANYTHING,
4178 .arg3_type = ARG_ANYTHING,
4181 static unsigned long bpf_skb_copy(void *dst_buff, const void *skb,
4182 unsigned long off, unsigned long len)
4184 void *ptr = skb_header_pointer(skb, off, len, dst_buff);
4188 if (ptr != dst_buff)
4189 memcpy(dst_buff, ptr, len);
4194 BPF_CALL_5(bpf_skb_event_output, struct sk_buff *, skb, struct bpf_map *, map,
4195 u64, flags, void *, meta, u64, meta_size)
4197 u64 skb_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
4199 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
4201 if (unlikely(!skb || skb_size > skb->len))
4204 return bpf_event_output(map, flags, meta, meta_size, skb, skb_size,
4208 static const struct bpf_func_proto bpf_skb_event_output_proto = {
4209 .func = bpf_skb_event_output,
4211 .ret_type = RET_INTEGER,
4212 .arg1_type = ARG_PTR_TO_CTX,
4213 .arg2_type = ARG_CONST_MAP_PTR,
4214 .arg3_type = ARG_ANYTHING,
4215 .arg4_type = ARG_PTR_TO_MEM,
4216 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
4219 BTF_ID_LIST_SINGLE(bpf_skb_output_btf_ids, struct, sk_buff)
4221 const struct bpf_func_proto bpf_skb_output_proto = {
4222 .func = bpf_skb_event_output,
4224 .ret_type = RET_INTEGER,
4225 .arg1_type = ARG_PTR_TO_BTF_ID,
4226 .arg1_btf_id = &bpf_skb_output_btf_ids[0],
4227 .arg2_type = ARG_CONST_MAP_PTR,
4228 .arg3_type = ARG_ANYTHING,
4229 .arg4_type = ARG_PTR_TO_MEM,
4230 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
4233 static unsigned short bpf_tunnel_key_af(u64 flags)
4235 return flags & BPF_F_TUNINFO_IPV6 ? AF_INET6 : AF_INET;
4238 BPF_CALL_4(bpf_skb_get_tunnel_key, struct sk_buff *, skb, struct bpf_tunnel_key *, to,
4239 u32, size, u64, flags)
4241 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
4242 u8 compat[sizeof(struct bpf_tunnel_key)];
4246 if (unlikely(!info || (flags & ~(BPF_F_TUNINFO_IPV6)))) {
4250 if (ip_tunnel_info_af(info) != bpf_tunnel_key_af(flags)) {
4254 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
4257 case offsetof(struct bpf_tunnel_key, tunnel_label):
4258 case offsetof(struct bpf_tunnel_key, tunnel_ext):
4260 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
4261 /* Fixup deprecated structure layouts here, so we have
4262 * a common path later on.
4264 if (ip_tunnel_info_af(info) != AF_INET)
4267 to = (struct bpf_tunnel_key *)compat;
4274 to->tunnel_id = be64_to_cpu(info->key.tun_id);
4275 to->tunnel_tos = info->key.tos;
4276 to->tunnel_ttl = info->key.ttl;
4279 if (flags & BPF_F_TUNINFO_IPV6) {
4280 memcpy(to->remote_ipv6, &info->key.u.ipv6.src,
4281 sizeof(to->remote_ipv6));
4282 to->tunnel_label = be32_to_cpu(info->key.label);
4284 to->remote_ipv4 = be32_to_cpu(info->key.u.ipv4.src);
4285 memset(&to->remote_ipv6[1], 0, sizeof(__u32) * 3);
4286 to->tunnel_label = 0;
4289 if (unlikely(size != sizeof(struct bpf_tunnel_key)))
4290 memcpy(to_orig, to, size);
4294 memset(to_orig, 0, size);
4298 static const struct bpf_func_proto bpf_skb_get_tunnel_key_proto = {
4299 .func = bpf_skb_get_tunnel_key,
4301 .ret_type = RET_INTEGER,
4302 .arg1_type = ARG_PTR_TO_CTX,
4303 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
4304 .arg3_type = ARG_CONST_SIZE,
4305 .arg4_type = ARG_ANYTHING,
4308 BPF_CALL_3(bpf_skb_get_tunnel_opt, struct sk_buff *, skb, u8 *, to, u32, size)
4310 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
4313 if (unlikely(!info ||
4314 !(info->key.tun_flags & TUNNEL_OPTIONS_PRESENT))) {
4318 if (unlikely(size < info->options_len)) {
4323 ip_tunnel_info_opts_get(to, info);
4324 if (size > info->options_len)
4325 memset(to + info->options_len, 0, size - info->options_len);
4327 return info->options_len;
4329 memset(to, 0, size);
4333 static const struct bpf_func_proto bpf_skb_get_tunnel_opt_proto = {
4334 .func = bpf_skb_get_tunnel_opt,
4336 .ret_type = RET_INTEGER,
4337 .arg1_type = ARG_PTR_TO_CTX,
4338 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
4339 .arg3_type = ARG_CONST_SIZE,
4342 static struct metadata_dst __percpu *md_dst;
4344 BPF_CALL_4(bpf_skb_set_tunnel_key, struct sk_buff *, skb,
4345 const struct bpf_tunnel_key *, from, u32, size, u64, flags)
4347 struct metadata_dst *md = this_cpu_ptr(md_dst);
4348 u8 compat[sizeof(struct bpf_tunnel_key)];
4349 struct ip_tunnel_info *info;
4351 if (unlikely(flags & ~(BPF_F_TUNINFO_IPV6 | BPF_F_ZERO_CSUM_TX |
4352 BPF_F_DONT_FRAGMENT | BPF_F_SEQ_NUMBER)))
4354 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
4356 case offsetof(struct bpf_tunnel_key, tunnel_label):
4357 case offsetof(struct bpf_tunnel_key, tunnel_ext):
4358 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
4359 /* Fixup deprecated structure layouts here, so we have
4360 * a common path later on.
4362 memcpy(compat, from, size);
4363 memset(compat + size, 0, sizeof(compat) - size);
4364 from = (const struct bpf_tunnel_key *) compat;
4370 if (unlikely((!(flags & BPF_F_TUNINFO_IPV6) && from->tunnel_label) ||
4375 dst_hold((struct dst_entry *) md);
4376 skb_dst_set(skb, (struct dst_entry *) md);
4378 info = &md->u.tun_info;
4379 memset(info, 0, sizeof(*info));
4380 info->mode = IP_TUNNEL_INFO_TX;
4382 info->key.tun_flags = TUNNEL_KEY | TUNNEL_CSUM | TUNNEL_NOCACHE;
4383 if (flags & BPF_F_DONT_FRAGMENT)
4384 info->key.tun_flags |= TUNNEL_DONT_FRAGMENT;
4385 if (flags & BPF_F_ZERO_CSUM_TX)
4386 info->key.tun_flags &= ~TUNNEL_CSUM;
4387 if (flags & BPF_F_SEQ_NUMBER)
4388 info->key.tun_flags |= TUNNEL_SEQ;
4390 info->key.tun_id = cpu_to_be64(from->tunnel_id);
4391 info->key.tos = from->tunnel_tos;
4392 info->key.ttl = from->tunnel_ttl;
4394 if (flags & BPF_F_TUNINFO_IPV6) {
4395 info->mode |= IP_TUNNEL_INFO_IPV6;
4396 memcpy(&info->key.u.ipv6.dst, from->remote_ipv6,
4397 sizeof(from->remote_ipv6));
4398 info->key.label = cpu_to_be32(from->tunnel_label) &
4399 IPV6_FLOWLABEL_MASK;
4401 info->key.u.ipv4.dst = cpu_to_be32(from->remote_ipv4);
4407 static const struct bpf_func_proto bpf_skb_set_tunnel_key_proto = {
4408 .func = bpf_skb_set_tunnel_key,
4410 .ret_type = RET_INTEGER,
4411 .arg1_type = ARG_PTR_TO_CTX,
4412 .arg2_type = ARG_PTR_TO_MEM,
4413 .arg3_type = ARG_CONST_SIZE,
4414 .arg4_type = ARG_ANYTHING,
4417 BPF_CALL_3(bpf_skb_set_tunnel_opt, struct sk_buff *, skb,
4418 const u8 *, from, u32, size)
4420 struct ip_tunnel_info *info = skb_tunnel_info(skb);
4421 const struct metadata_dst *md = this_cpu_ptr(md_dst);
4423 if (unlikely(info != &md->u.tun_info || (size & (sizeof(u32) - 1))))
4425 if (unlikely(size > IP_TUNNEL_OPTS_MAX))
4428 ip_tunnel_info_opts_set(info, from, size, TUNNEL_OPTIONS_PRESENT);
4433 static const struct bpf_func_proto bpf_skb_set_tunnel_opt_proto = {
4434 .func = bpf_skb_set_tunnel_opt,
4436 .ret_type = RET_INTEGER,
4437 .arg1_type = ARG_PTR_TO_CTX,
4438 .arg2_type = ARG_PTR_TO_MEM,
4439 .arg3_type = ARG_CONST_SIZE,
4442 static const struct bpf_func_proto *
4443 bpf_get_skb_set_tunnel_proto(enum bpf_func_id which)
4446 struct metadata_dst __percpu *tmp;
4448 tmp = metadata_dst_alloc_percpu(IP_TUNNEL_OPTS_MAX,
4453 if (cmpxchg(&md_dst, NULL, tmp))
4454 metadata_dst_free_percpu(tmp);
4458 case BPF_FUNC_skb_set_tunnel_key:
4459 return &bpf_skb_set_tunnel_key_proto;
4460 case BPF_FUNC_skb_set_tunnel_opt:
4461 return &bpf_skb_set_tunnel_opt_proto;
4467 BPF_CALL_3(bpf_skb_under_cgroup, struct sk_buff *, skb, struct bpf_map *, map,
4470 struct bpf_array *array = container_of(map, struct bpf_array, map);
4471 struct cgroup *cgrp;
4474 sk = skb_to_full_sk(skb);
4475 if (!sk || !sk_fullsock(sk))
4477 if (unlikely(idx >= array->map.max_entries))
4480 cgrp = READ_ONCE(array->ptrs[idx]);
4481 if (unlikely(!cgrp))
4484 return sk_under_cgroup_hierarchy(sk, cgrp);
4487 static const struct bpf_func_proto bpf_skb_under_cgroup_proto = {
4488 .func = bpf_skb_under_cgroup,
4490 .ret_type = RET_INTEGER,
4491 .arg1_type = ARG_PTR_TO_CTX,
4492 .arg2_type = ARG_CONST_MAP_PTR,
4493 .arg3_type = ARG_ANYTHING,
4496 #ifdef CONFIG_SOCK_CGROUP_DATA
4497 static inline u64 __bpf_sk_cgroup_id(struct sock *sk)
4499 struct cgroup *cgrp;
4501 sk = sk_to_full_sk(sk);
4502 if (!sk || !sk_fullsock(sk))
4505 cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
4506 return cgroup_id(cgrp);
4509 BPF_CALL_1(bpf_skb_cgroup_id, const struct sk_buff *, skb)
4511 return __bpf_sk_cgroup_id(skb->sk);
4514 static const struct bpf_func_proto bpf_skb_cgroup_id_proto = {
4515 .func = bpf_skb_cgroup_id,
4517 .ret_type = RET_INTEGER,
4518 .arg1_type = ARG_PTR_TO_CTX,
4521 static inline u64 __bpf_sk_ancestor_cgroup_id(struct sock *sk,
4524 struct cgroup *ancestor;
4525 struct cgroup *cgrp;
4527 sk = sk_to_full_sk(sk);
4528 if (!sk || !sk_fullsock(sk))
4531 cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
4532 ancestor = cgroup_ancestor(cgrp, ancestor_level);
4536 return cgroup_id(ancestor);
4539 BPF_CALL_2(bpf_skb_ancestor_cgroup_id, const struct sk_buff *, skb, int,
4542 return __bpf_sk_ancestor_cgroup_id(skb->sk, ancestor_level);
4545 static const struct bpf_func_proto bpf_skb_ancestor_cgroup_id_proto = {
4546 .func = bpf_skb_ancestor_cgroup_id,
4548 .ret_type = RET_INTEGER,
4549 .arg1_type = ARG_PTR_TO_CTX,
4550 .arg2_type = ARG_ANYTHING,
4553 BPF_CALL_1(bpf_sk_cgroup_id, struct sock *, sk)
4555 return __bpf_sk_cgroup_id(sk);
4558 static const struct bpf_func_proto bpf_sk_cgroup_id_proto = {
4559 .func = bpf_sk_cgroup_id,
4561 .ret_type = RET_INTEGER,
4562 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
4565 BPF_CALL_2(bpf_sk_ancestor_cgroup_id, struct sock *, sk, int, ancestor_level)
4567 return __bpf_sk_ancestor_cgroup_id(sk, ancestor_level);
4570 static const struct bpf_func_proto bpf_sk_ancestor_cgroup_id_proto = {
4571 .func = bpf_sk_ancestor_cgroup_id,
4573 .ret_type = RET_INTEGER,
4574 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
4575 .arg2_type = ARG_ANYTHING,
4579 static unsigned long bpf_xdp_copy(void *dst_buff, const void *src_buff,
4580 unsigned long off, unsigned long len)
4582 memcpy(dst_buff, src_buff + off, len);
4586 BPF_CALL_5(bpf_xdp_event_output, struct xdp_buff *, xdp, struct bpf_map *, map,
4587 u64, flags, void *, meta, u64, meta_size)
4589 u64 xdp_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
4591 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
4593 if (unlikely(!xdp ||
4594 xdp_size > (unsigned long)(xdp->data_end - xdp->data)))
4597 return bpf_event_output(map, flags, meta, meta_size, xdp->data,
4598 xdp_size, bpf_xdp_copy);
4601 static const struct bpf_func_proto bpf_xdp_event_output_proto = {
4602 .func = bpf_xdp_event_output,
4604 .ret_type = RET_INTEGER,
4605 .arg1_type = ARG_PTR_TO_CTX,
4606 .arg2_type = ARG_CONST_MAP_PTR,
4607 .arg3_type = ARG_ANYTHING,
4608 .arg4_type = ARG_PTR_TO_MEM,
4609 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
4612 BTF_ID_LIST_SINGLE(bpf_xdp_output_btf_ids, struct, xdp_buff)
4614 const struct bpf_func_proto bpf_xdp_output_proto = {
4615 .func = bpf_xdp_event_output,
4617 .ret_type = RET_INTEGER,
4618 .arg1_type = ARG_PTR_TO_BTF_ID,
4619 .arg1_btf_id = &bpf_xdp_output_btf_ids[0],
4620 .arg2_type = ARG_CONST_MAP_PTR,
4621 .arg3_type = ARG_ANYTHING,
4622 .arg4_type = ARG_PTR_TO_MEM,
4623 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
4626 BPF_CALL_1(bpf_get_socket_cookie, struct sk_buff *, skb)
4628 return skb->sk ? __sock_gen_cookie(skb->sk) : 0;
4631 static const struct bpf_func_proto bpf_get_socket_cookie_proto = {
4632 .func = bpf_get_socket_cookie,
4634 .ret_type = RET_INTEGER,
4635 .arg1_type = ARG_PTR_TO_CTX,
4638 BPF_CALL_1(bpf_get_socket_cookie_sock_addr, struct bpf_sock_addr_kern *, ctx)
4640 return __sock_gen_cookie(ctx->sk);
4643 static const struct bpf_func_proto bpf_get_socket_cookie_sock_addr_proto = {
4644 .func = bpf_get_socket_cookie_sock_addr,
4646 .ret_type = RET_INTEGER,
4647 .arg1_type = ARG_PTR_TO_CTX,
4650 BPF_CALL_1(bpf_get_socket_cookie_sock, struct sock *, ctx)
4652 return __sock_gen_cookie(ctx);
4655 static const struct bpf_func_proto bpf_get_socket_cookie_sock_proto = {
4656 .func = bpf_get_socket_cookie_sock,
4658 .ret_type = RET_INTEGER,
4659 .arg1_type = ARG_PTR_TO_CTX,
4662 BPF_CALL_1(bpf_get_socket_cookie_sock_ops, struct bpf_sock_ops_kern *, ctx)
4664 return __sock_gen_cookie(ctx->sk);
4667 static const struct bpf_func_proto bpf_get_socket_cookie_sock_ops_proto = {
4668 .func = bpf_get_socket_cookie_sock_ops,
4670 .ret_type = RET_INTEGER,
4671 .arg1_type = ARG_PTR_TO_CTX,
4674 static u64 __bpf_get_netns_cookie(struct sock *sk)
4676 #ifdef CONFIG_NET_NS
4677 return __net_gen_cookie(sk ? sk->sk_net.net : &init_net);
4683 BPF_CALL_1(bpf_get_netns_cookie_sock, struct sock *, ctx)
4685 return __bpf_get_netns_cookie(ctx);
4688 static const struct bpf_func_proto bpf_get_netns_cookie_sock_proto = {
4689 .func = bpf_get_netns_cookie_sock,
4691 .ret_type = RET_INTEGER,
4692 .arg1_type = ARG_PTR_TO_CTX_OR_NULL,
4695 BPF_CALL_1(bpf_get_netns_cookie_sock_addr, struct bpf_sock_addr_kern *, ctx)
4697 return __bpf_get_netns_cookie(ctx ? ctx->sk : NULL);
4700 static const struct bpf_func_proto bpf_get_netns_cookie_sock_addr_proto = {
4701 .func = bpf_get_netns_cookie_sock_addr,
4703 .ret_type = RET_INTEGER,
4704 .arg1_type = ARG_PTR_TO_CTX_OR_NULL,
4707 BPF_CALL_1(bpf_get_socket_uid, struct sk_buff *, skb)
4709 struct sock *sk = sk_to_full_sk(skb->sk);
4712 if (!sk || !sk_fullsock(sk))
4714 kuid = sock_net_uid(sock_net(sk), sk);
4715 return from_kuid_munged(sock_net(sk)->user_ns, kuid);
4718 static const struct bpf_func_proto bpf_get_socket_uid_proto = {
4719 .func = bpf_get_socket_uid,
4721 .ret_type = RET_INTEGER,
4722 .arg1_type = ARG_PTR_TO_CTX,
4725 static int _bpf_setsockopt(struct sock *sk, int level, int optname,
4726 char *optval, int optlen)
4728 char devname[IFNAMSIZ];
4734 if (!sk_fullsock(sk))
4737 sock_owned_by_me(sk);
4739 if (level == SOL_SOCKET) {
4740 if (optlen != sizeof(int) && optname != SO_BINDTODEVICE)
4742 val = *((int *)optval);
4743 valbool = val ? 1 : 0;
4745 /* Only some socketops are supported */
4748 val = min_t(u32, val, READ_ONCE(sysctl_rmem_max));
4749 val = min_t(int, val, INT_MAX / 2);
4750 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
4751 WRITE_ONCE(sk->sk_rcvbuf,
4752 max_t(int, val * 2, SOCK_MIN_RCVBUF));
4755 val = min_t(u32, val, READ_ONCE(sysctl_wmem_max));
4756 val = min_t(int, val, INT_MAX / 2);
4757 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
4758 WRITE_ONCE(sk->sk_sndbuf,
4759 max_t(int, val * 2, SOCK_MIN_SNDBUF));
4761 case SO_MAX_PACING_RATE: /* 32bit version */
4763 cmpxchg(&sk->sk_pacing_status,
4766 sk->sk_max_pacing_rate = (val == ~0U) ?
4767 ~0UL : (unsigned int)val;
4768 sk->sk_pacing_rate = min(sk->sk_pacing_rate,
4769 sk->sk_max_pacing_rate);
4772 sk->sk_priority = val;
4777 WRITE_ONCE(sk->sk_rcvlowat, val ? : 1);
4780 if (sk->sk_mark != val) {
4785 case SO_BINDTODEVICE:
4786 optlen = min_t(long, optlen, IFNAMSIZ - 1);
4787 strncpy(devname, optval, optlen);
4788 devname[optlen] = 0;
4791 if (devname[0] != '\0') {
4792 struct net_device *dev;
4797 dev = dev_get_by_name(net, devname);
4800 ifindex = dev->ifindex;
4803 ret = sock_bindtoindex(sk, ifindex, false);
4806 if (sk->sk_prot->keepalive)
4807 sk->sk_prot->keepalive(sk, valbool);
4808 sock_valbool_flag(sk, SOCK_KEEPOPEN, valbool);
4814 } else if (level == SOL_IP) {
4815 if (optlen != sizeof(int) || sk->sk_family != AF_INET)
4818 val = *((int *)optval);
4819 /* Only some options are supported */
4822 if (val < -1 || val > 0xff) {
4825 struct inet_sock *inet = inet_sk(sk);
4835 #if IS_ENABLED(CONFIG_IPV6)
4836 } else if (level == SOL_IPV6) {
4837 if (optlen != sizeof(int) || sk->sk_family != AF_INET6)
4840 val = *((int *)optval);
4841 /* Only some options are supported */
4844 if (val < -1 || val > 0xff) {
4847 struct ipv6_pinfo *np = inet6_sk(sk);
4858 } else if (level == SOL_TCP &&
4859 sk->sk_prot->setsockopt == tcp_setsockopt) {
4860 if (optname == TCP_CONGESTION) {
4861 char name[TCP_CA_NAME_MAX];
4863 strncpy(name, optval, min_t(long, optlen,
4864 TCP_CA_NAME_MAX-1));
4865 name[TCP_CA_NAME_MAX-1] = 0;
4866 ret = tcp_set_congestion_control(sk, name, false, true);
4868 struct inet_connection_sock *icsk = inet_csk(sk);
4869 struct tcp_sock *tp = tcp_sk(sk);
4870 unsigned long timeout;
4872 if (optlen != sizeof(int))
4875 val = *((int *)optval);
4876 /* Only some options are supported */
4879 if (val <= 0 || tp->data_segs_out > tp->syn_data)
4884 case TCP_BPF_SNDCWND_CLAMP:
4888 tp->snd_cwnd_clamp = val;
4889 tp->snd_ssthresh = val;
4892 case TCP_BPF_DELACK_MAX:
4893 timeout = usecs_to_jiffies(val);
4894 if (timeout > TCP_DELACK_MAX ||
4895 timeout < TCP_TIMEOUT_MIN)
4897 inet_csk(sk)->icsk_delack_max = timeout;
4899 case TCP_BPF_RTO_MIN:
4900 timeout = usecs_to_jiffies(val);
4901 if (timeout > TCP_RTO_MIN ||
4902 timeout < TCP_TIMEOUT_MIN)
4904 inet_csk(sk)->icsk_rto_min = timeout;
4907 if (val < 0 || val > 1)
4913 ret = tcp_sock_set_keepidle_locked(sk, val);
4916 if (val < 1 || val > MAX_TCP_KEEPINTVL)
4919 tp->keepalive_intvl = val * HZ;
4922 if (val < 1 || val > MAX_TCP_KEEPCNT)
4925 tp->keepalive_probes = val;
4928 if (val < 1 || val > MAX_TCP_SYNCNT)
4931 icsk->icsk_syn_retries = val;
4933 case TCP_USER_TIMEOUT:
4937 icsk->icsk_user_timeout = val;
4939 case TCP_NOTSENT_LOWAT:
4940 tp->notsent_lowat = val;
4941 sk->sk_write_space(sk);
4954 static int _bpf_getsockopt(struct sock *sk, int level, int optname,
4955 char *optval, int optlen)
4957 if (!sk_fullsock(sk))
4960 sock_owned_by_me(sk);
4963 if (level == SOL_TCP && sk->sk_prot->getsockopt == tcp_getsockopt) {
4964 struct inet_connection_sock *icsk;
4965 struct tcp_sock *tp;
4968 case TCP_CONGESTION:
4969 icsk = inet_csk(sk);
4971 if (!icsk->icsk_ca_ops || optlen <= 1)
4973 strncpy(optval, icsk->icsk_ca_ops->name, optlen);
4974 optval[optlen - 1] = 0;
4979 if (optlen <= 0 || !tp->saved_syn ||
4980 optlen > tcp_saved_syn_len(tp->saved_syn))
4982 memcpy(optval, tp->saved_syn->data, optlen);
4987 } else if (level == SOL_IP) {
4988 struct inet_sock *inet = inet_sk(sk);
4990 if (optlen != sizeof(int) || sk->sk_family != AF_INET)
4993 /* Only some options are supported */
4996 *((int *)optval) = (int)inet->tos;
5001 #if IS_ENABLED(CONFIG_IPV6)
5002 } else if (level == SOL_IPV6) {
5003 struct ipv6_pinfo *np = inet6_sk(sk);
5005 if (optlen != sizeof(int) || sk->sk_family != AF_INET6)
5008 /* Only some options are supported */
5011 *((int *)optval) = (int)np->tclass;
5023 memset(optval, 0, optlen);
5027 BPF_CALL_5(bpf_sock_addr_setsockopt, struct bpf_sock_addr_kern *, ctx,
5028 int, level, int, optname, char *, optval, int, optlen)
5030 return _bpf_setsockopt(ctx->sk, level, optname, optval, optlen);
5033 static const struct bpf_func_proto bpf_sock_addr_setsockopt_proto = {
5034 .func = bpf_sock_addr_setsockopt,
5036 .ret_type = RET_INTEGER,
5037 .arg1_type = ARG_PTR_TO_CTX,
5038 .arg2_type = ARG_ANYTHING,
5039 .arg3_type = ARG_ANYTHING,
5040 .arg4_type = ARG_PTR_TO_MEM,
5041 .arg5_type = ARG_CONST_SIZE,
5044 BPF_CALL_5(bpf_sock_addr_getsockopt, struct bpf_sock_addr_kern *, ctx,
5045 int, level, int, optname, char *, optval, int, optlen)
5047 return _bpf_getsockopt(ctx->sk, level, optname, optval, optlen);
5050 static const struct bpf_func_proto bpf_sock_addr_getsockopt_proto = {
5051 .func = bpf_sock_addr_getsockopt,
5053 .ret_type = RET_INTEGER,
5054 .arg1_type = ARG_PTR_TO_CTX,
5055 .arg2_type = ARG_ANYTHING,
5056 .arg3_type = ARG_ANYTHING,
5057 .arg4_type = ARG_PTR_TO_UNINIT_MEM,
5058 .arg5_type = ARG_CONST_SIZE,
5061 BPF_CALL_5(bpf_sock_ops_setsockopt, struct bpf_sock_ops_kern *, bpf_sock,
5062 int, level, int, optname, char *, optval, int, optlen)
5064 return _bpf_setsockopt(bpf_sock->sk, level, optname, optval, optlen);
5067 static const struct bpf_func_proto bpf_sock_ops_setsockopt_proto = {
5068 .func = bpf_sock_ops_setsockopt,
5070 .ret_type = RET_INTEGER,
5071 .arg1_type = ARG_PTR_TO_CTX,
5072 .arg2_type = ARG_ANYTHING,
5073 .arg3_type = ARG_ANYTHING,
5074 .arg4_type = ARG_PTR_TO_MEM,
5075 .arg5_type = ARG_CONST_SIZE,
5078 static int bpf_sock_ops_get_syn(struct bpf_sock_ops_kern *bpf_sock,
5079 int optname, const u8 **start)
5081 struct sk_buff *syn_skb = bpf_sock->syn_skb;
5082 const u8 *hdr_start;
5086 /* sk is a request_sock here */
5088 if (optname == TCP_BPF_SYN) {
5089 hdr_start = syn_skb->data;
5090 ret = tcp_hdrlen(syn_skb);
5091 } else if (optname == TCP_BPF_SYN_IP) {
5092 hdr_start = skb_network_header(syn_skb);
5093 ret = skb_network_header_len(syn_skb) +
5094 tcp_hdrlen(syn_skb);
5096 /* optname == TCP_BPF_SYN_MAC */
5097 hdr_start = skb_mac_header(syn_skb);
5098 ret = skb_mac_header_len(syn_skb) +
5099 skb_network_header_len(syn_skb) +
5100 tcp_hdrlen(syn_skb);
5103 struct sock *sk = bpf_sock->sk;
5104 struct saved_syn *saved_syn;
5106 if (sk->sk_state == TCP_NEW_SYN_RECV)
5107 /* synack retransmit. bpf_sock->syn_skb will
5108 * not be available. It has to resort to
5109 * saved_syn (if it is saved).
5111 saved_syn = inet_reqsk(sk)->saved_syn;
5113 saved_syn = tcp_sk(sk)->saved_syn;
5118 if (optname == TCP_BPF_SYN) {
5119 hdr_start = saved_syn->data +
5120 saved_syn->mac_hdrlen +
5121 saved_syn->network_hdrlen;
5122 ret = saved_syn->tcp_hdrlen;
5123 } else if (optname == TCP_BPF_SYN_IP) {
5124 hdr_start = saved_syn->data +
5125 saved_syn->mac_hdrlen;
5126 ret = saved_syn->network_hdrlen +
5127 saved_syn->tcp_hdrlen;
5129 /* optname == TCP_BPF_SYN_MAC */
5131 /* TCP_SAVE_SYN may not have saved the mac hdr */
5132 if (!saved_syn->mac_hdrlen)
5135 hdr_start = saved_syn->data;
5136 ret = saved_syn->mac_hdrlen +
5137 saved_syn->network_hdrlen +
5138 saved_syn->tcp_hdrlen;
5146 BPF_CALL_5(bpf_sock_ops_getsockopt, struct bpf_sock_ops_kern *, bpf_sock,
5147 int, level, int, optname, char *, optval, int, optlen)
5149 if (IS_ENABLED(CONFIG_INET) && level == SOL_TCP &&
5150 optname >= TCP_BPF_SYN && optname <= TCP_BPF_SYN_MAC) {
5151 int ret, copy_len = 0;
5154 ret = bpf_sock_ops_get_syn(bpf_sock, optname, &start);
5157 if (optlen < copy_len) {
5162 memcpy(optval, start, copy_len);
5165 /* Zero out unused buffer at the end */
5166 memset(optval + copy_len, 0, optlen - copy_len);
5171 return _bpf_getsockopt(bpf_sock->sk, level, optname, optval, optlen);
5174 static const struct bpf_func_proto bpf_sock_ops_getsockopt_proto = {
5175 .func = bpf_sock_ops_getsockopt,
5177 .ret_type = RET_INTEGER,
5178 .arg1_type = ARG_PTR_TO_CTX,
5179 .arg2_type = ARG_ANYTHING,
5180 .arg3_type = ARG_ANYTHING,
5181 .arg4_type = ARG_PTR_TO_UNINIT_MEM,
5182 .arg5_type = ARG_CONST_SIZE,
5185 BPF_CALL_2(bpf_sock_ops_cb_flags_set, struct bpf_sock_ops_kern *, bpf_sock,
5188 struct sock *sk = bpf_sock->sk;
5189 int val = argval & BPF_SOCK_OPS_ALL_CB_FLAGS;
5191 if (!IS_ENABLED(CONFIG_INET) || !sk_fullsock(sk))
5194 tcp_sk(sk)->bpf_sock_ops_cb_flags = val;
5196 return argval & (~BPF_SOCK_OPS_ALL_CB_FLAGS);
5199 static const struct bpf_func_proto bpf_sock_ops_cb_flags_set_proto = {
5200 .func = bpf_sock_ops_cb_flags_set,
5202 .ret_type = RET_INTEGER,
5203 .arg1_type = ARG_PTR_TO_CTX,
5204 .arg2_type = ARG_ANYTHING,
5207 const struct ipv6_bpf_stub *ipv6_bpf_stub __read_mostly;
5208 EXPORT_SYMBOL_GPL(ipv6_bpf_stub);
5210 BPF_CALL_3(bpf_bind, struct bpf_sock_addr_kern *, ctx, struct sockaddr *, addr,
5214 struct sock *sk = ctx->sk;
5215 u32 flags = BIND_FROM_BPF;
5219 if (addr_len < offsetofend(struct sockaddr, sa_family))
5221 if (addr->sa_family == AF_INET) {
5222 if (addr_len < sizeof(struct sockaddr_in))
5224 if (((struct sockaddr_in *)addr)->sin_port == htons(0))
5225 flags |= BIND_FORCE_ADDRESS_NO_PORT;
5226 return __inet_bind(sk, addr, addr_len, flags);
5227 #if IS_ENABLED(CONFIG_IPV6)
5228 } else if (addr->sa_family == AF_INET6) {
5229 if (addr_len < SIN6_LEN_RFC2133)
5231 if (((struct sockaddr_in6 *)addr)->sin6_port == htons(0))
5232 flags |= BIND_FORCE_ADDRESS_NO_PORT;
5233 /* ipv6_bpf_stub cannot be NULL, since it's called from
5234 * bpf_cgroup_inet6_connect hook and ipv6 is already loaded
5236 return ipv6_bpf_stub->inet6_bind(sk, addr, addr_len, flags);
5237 #endif /* CONFIG_IPV6 */
5239 #endif /* CONFIG_INET */
5241 return -EAFNOSUPPORT;
5244 static const struct bpf_func_proto bpf_bind_proto = {
5247 .ret_type = RET_INTEGER,
5248 .arg1_type = ARG_PTR_TO_CTX,
5249 .arg2_type = ARG_PTR_TO_MEM,
5250 .arg3_type = ARG_CONST_SIZE,
5254 BPF_CALL_5(bpf_skb_get_xfrm_state, struct sk_buff *, skb, u32, index,
5255 struct bpf_xfrm_state *, to, u32, size, u64, flags)
5257 const struct sec_path *sp = skb_sec_path(skb);
5258 const struct xfrm_state *x;
5260 if (!sp || unlikely(index >= sp->len || flags))
5263 x = sp->xvec[index];
5265 if (unlikely(size != sizeof(struct bpf_xfrm_state)))
5268 to->reqid = x->props.reqid;
5269 to->spi = x->id.spi;
5270 to->family = x->props.family;
5273 if (to->family == AF_INET6) {
5274 memcpy(to->remote_ipv6, x->props.saddr.a6,
5275 sizeof(to->remote_ipv6));
5277 to->remote_ipv4 = x->props.saddr.a4;
5278 memset(&to->remote_ipv6[1], 0, sizeof(__u32) * 3);
5283 memset(to, 0, size);
5287 static const struct bpf_func_proto bpf_skb_get_xfrm_state_proto = {
5288 .func = bpf_skb_get_xfrm_state,
5290 .ret_type = RET_INTEGER,
5291 .arg1_type = ARG_PTR_TO_CTX,
5292 .arg2_type = ARG_ANYTHING,
5293 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
5294 .arg4_type = ARG_CONST_SIZE,
5295 .arg5_type = ARG_ANYTHING,
5299 #if IS_ENABLED(CONFIG_INET) || IS_ENABLED(CONFIG_IPV6)
5300 static int bpf_fib_set_fwd_params(struct bpf_fib_lookup *params,
5301 const struct neighbour *neigh,
5302 const struct net_device *dev)
5304 memcpy(params->dmac, neigh->ha, ETH_ALEN);
5305 memcpy(params->smac, dev->dev_addr, ETH_ALEN);
5306 params->h_vlan_TCI = 0;
5307 params->h_vlan_proto = 0;
5313 #if IS_ENABLED(CONFIG_INET)
5314 static int bpf_ipv4_fib_lookup(struct net *net, struct bpf_fib_lookup *params,
5315 u32 flags, bool check_mtu)
5317 struct fib_nh_common *nhc;
5318 struct in_device *in_dev;
5319 struct neighbour *neigh;
5320 struct net_device *dev;
5321 struct fib_result res;
5326 dev = dev_get_by_index_rcu(net, params->ifindex);
5330 /* verify forwarding is enabled on this interface */
5331 in_dev = __in_dev_get_rcu(dev);
5332 if (unlikely(!in_dev || !IN_DEV_FORWARD(in_dev)))
5333 return BPF_FIB_LKUP_RET_FWD_DISABLED;
5335 if (flags & BPF_FIB_LOOKUP_OUTPUT) {
5337 fl4.flowi4_oif = params->ifindex;
5339 fl4.flowi4_iif = params->ifindex;
5342 fl4.flowi4_tos = params->tos & IPTOS_RT_MASK;
5343 fl4.flowi4_scope = RT_SCOPE_UNIVERSE;
5344 fl4.flowi4_flags = 0;
5346 fl4.flowi4_proto = params->l4_protocol;
5347 fl4.daddr = params->ipv4_dst;
5348 fl4.saddr = params->ipv4_src;
5349 fl4.fl4_sport = params->sport;
5350 fl4.fl4_dport = params->dport;
5351 fl4.flowi4_multipath_hash = 0;
5353 if (flags & BPF_FIB_LOOKUP_DIRECT) {
5354 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
5355 struct fib_table *tb;
5357 tb = fib_get_table(net, tbid);
5359 return BPF_FIB_LKUP_RET_NOT_FWDED;
5361 err = fib_table_lookup(tb, &fl4, &res, FIB_LOOKUP_NOREF);
5363 fl4.flowi4_mark = 0;
5364 fl4.flowi4_secid = 0;
5365 fl4.flowi4_tun_key.tun_id = 0;
5366 fl4.flowi4_uid = sock_net_uid(net, NULL);
5368 err = fib_lookup(net, &fl4, &res, FIB_LOOKUP_NOREF);
5372 /* map fib lookup errors to RTN_ type */
5374 return BPF_FIB_LKUP_RET_BLACKHOLE;
5375 if (err == -EHOSTUNREACH)
5376 return BPF_FIB_LKUP_RET_UNREACHABLE;
5378 return BPF_FIB_LKUP_RET_PROHIBIT;
5380 return BPF_FIB_LKUP_RET_NOT_FWDED;
5383 if (res.type != RTN_UNICAST)
5384 return BPF_FIB_LKUP_RET_NOT_FWDED;
5386 if (fib_info_num_path(res.fi) > 1)
5387 fib_select_path(net, &res, &fl4, NULL);
5390 mtu = ip_mtu_from_fib_result(&res, params->ipv4_dst);
5391 if (params->tot_len > mtu)
5392 return BPF_FIB_LKUP_RET_FRAG_NEEDED;
5397 /* do not handle lwt encaps right now */
5398 if (nhc->nhc_lwtstate)
5399 return BPF_FIB_LKUP_RET_UNSUPP_LWT;
5403 params->rt_metric = res.fi->fib_priority;
5404 params->ifindex = dev->ifindex;
5406 /* xdp and cls_bpf programs are run in RCU-bh so
5407 * rcu_read_lock_bh is not needed here
5409 if (likely(nhc->nhc_gw_family != AF_INET6)) {
5410 if (nhc->nhc_gw_family)
5411 params->ipv4_dst = nhc->nhc_gw.ipv4;
5413 neigh = __ipv4_neigh_lookup_noref(dev,
5414 (__force u32)params->ipv4_dst);
5416 struct in6_addr *dst = (struct in6_addr *)params->ipv6_dst;
5418 params->family = AF_INET6;
5419 *dst = nhc->nhc_gw.ipv6;
5420 neigh = __ipv6_neigh_lookup_noref_stub(dev, dst);
5423 if (!neigh || !(neigh->nud_state & NUD_VALID))
5424 return BPF_FIB_LKUP_RET_NO_NEIGH;
5426 return bpf_fib_set_fwd_params(params, neigh, dev);
5430 #if IS_ENABLED(CONFIG_IPV6)
5431 static int bpf_ipv6_fib_lookup(struct net *net, struct bpf_fib_lookup *params,
5432 u32 flags, bool check_mtu)
5434 struct in6_addr *src = (struct in6_addr *) params->ipv6_src;
5435 struct in6_addr *dst = (struct in6_addr *) params->ipv6_dst;
5436 struct fib6_result res = {};
5437 struct neighbour *neigh;
5438 struct net_device *dev;
5439 struct inet6_dev *idev;
5445 /* link local addresses are never forwarded */
5446 if (rt6_need_strict(dst) || rt6_need_strict(src))
5447 return BPF_FIB_LKUP_RET_NOT_FWDED;
5449 dev = dev_get_by_index_rcu(net, params->ifindex);
5453 idev = __in6_dev_get_safely(dev);
5454 if (unlikely(!idev || !idev->cnf.forwarding))
5455 return BPF_FIB_LKUP_RET_FWD_DISABLED;
5457 if (flags & BPF_FIB_LOOKUP_OUTPUT) {
5459 oif = fl6.flowi6_oif = params->ifindex;
5461 oif = fl6.flowi6_iif = params->ifindex;
5463 strict = RT6_LOOKUP_F_HAS_SADDR;
5465 fl6.flowlabel = params->flowinfo;
5466 fl6.flowi6_scope = 0;
5467 fl6.flowi6_flags = 0;
5470 fl6.flowi6_proto = params->l4_protocol;
5473 fl6.fl6_sport = params->sport;
5474 fl6.fl6_dport = params->dport;
5476 if (flags & BPF_FIB_LOOKUP_DIRECT) {
5477 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
5478 struct fib6_table *tb;
5480 tb = ipv6_stub->fib6_get_table(net, tbid);
5482 return BPF_FIB_LKUP_RET_NOT_FWDED;
5484 err = ipv6_stub->fib6_table_lookup(net, tb, oif, &fl6, &res,
5487 fl6.flowi6_mark = 0;
5488 fl6.flowi6_secid = 0;
5489 fl6.flowi6_tun_key.tun_id = 0;
5490 fl6.flowi6_uid = sock_net_uid(net, NULL);
5492 err = ipv6_stub->fib6_lookup(net, oif, &fl6, &res, strict);
5495 if (unlikely(err || IS_ERR_OR_NULL(res.f6i) ||
5496 res.f6i == net->ipv6.fib6_null_entry))
5497 return BPF_FIB_LKUP_RET_NOT_FWDED;
5499 switch (res.fib6_type) {
5500 /* only unicast is forwarded */
5504 return BPF_FIB_LKUP_RET_BLACKHOLE;
5505 case RTN_UNREACHABLE:
5506 return BPF_FIB_LKUP_RET_UNREACHABLE;
5508 return BPF_FIB_LKUP_RET_PROHIBIT;
5510 return BPF_FIB_LKUP_RET_NOT_FWDED;
5513 ipv6_stub->fib6_select_path(net, &res, &fl6, fl6.flowi6_oif,
5514 fl6.flowi6_oif != 0, NULL, strict);
5517 mtu = ipv6_stub->ip6_mtu_from_fib6(&res, dst, src);
5518 if (params->tot_len > mtu)
5519 return BPF_FIB_LKUP_RET_FRAG_NEEDED;
5522 if (res.nh->fib_nh_lws)
5523 return BPF_FIB_LKUP_RET_UNSUPP_LWT;
5525 if (res.nh->fib_nh_gw_family)
5526 *dst = res.nh->fib_nh_gw6;
5528 dev = res.nh->fib_nh_dev;
5529 params->rt_metric = res.f6i->fib6_metric;
5530 params->ifindex = dev->ifindex;
5532 /* xdp and cls_bpf programs are run in RCU-bh so rcu_read_lock_bh is
5535 neigh = __ipv6_neigh_lookup_noref_stub(dev, dst);
5536 if (!neigh || !(neigh->nud_state & NUD_VALID))
5537 return BPF_FIB_LKUP_RET_NO_NEIGH;
5539 return bpf_fib_set_fwd_params(params, neigh, dev);
5543 BPF_CALL_4(bpf_xdp_fib_lookup, struct xdp_buff *, ctx,
5544 struct bpf_fib_lookup *, params, int, plen, u32, flags)
5546 if (plen < sizeof(*params))
5549 if (flags & ~(BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_OUTPUT))
5552 switch (params->family) {
5553 #if IS_ENABLED(CONFIG_INET)
5555 return bpf_ipv4_fib_lookup(dev_net(ctx->rxq->dev), params,
5558 #if IS_ENABLED(CONFIG_IPV6)
5560 return bpf_ipv6_fib_lookup(dev_net(ctx->rxq->dev), params,
5564 return -EAFNOSUPPORT;
5567 static const struct bpf_func_proto bpf_xdp_fib_lookup_proto = {
5568 .func = bpf_xdp_fib_lookup,
5570 .ret_type = RET_INTEGER,
5571 .arg1_type = ARG_PTR_TO_CTX,
5572 .arg2_type = ARG_PTR_TO_MEM,
5573 .arg3_type = ARG_CONST_SIZE,
5574 .arg4_type = ARG_ANYTHING,
5577 BPF_CALL_4(bpf_skb_fib_lookup, struct sk_buff *, skb,
5578 struct bpf_fib_lookup *, params, int, plen, u32, flags)
5580 struct net *net = dev_net(skb->dev);
5581 int rc = -EAFNOSUPPORT;
5582 bool check_mtu = false;
5584 if (plen < sizeof(*params))
5587 if (flags & ~(BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_OUTPUT))
5590 if (params->tot_len)
5593 switch (params->family) {
5594 #if IS_ENABLED(CONFIG_INET)
5596 rc = bpf_ipv4_fib_lookup(net, params, flags, check_mtu);
5599 #if IS_ENABLED(CONFIG_IPV6)
5601 rc = bpf_ipv6_fib_lookup(net, params, flags, check_mtu);
5606 if (rc == BPF_FIB_LKUP_RET_SUCCESS && !check_mtu) {
5607 struct net_device *dev;
5609 /* When tot_len isn't provided by user, check skb
5610 * against MTU of FIB lookup resulting net_device
5612 dev = dev_get_by_index_rcu(net, params->ifindex);
5613 if (!is_skb_forwardable(dev, skb))
5614 rc = BPF_FIB_LKUP_RET_FRAG_NEEDED;
5620 static const struct bpf_func_proto bpf_skb_fib_lookup_proto = {
5621 .func = bpf_skb_fib_lookup,
5623 .ret_type = RET_INTEGER,
5624 .arg1_type = ARG_PTR_TO_CTX,
5625 .arg2_type = ARG_PTR_TO_MEM,
5626 .arg3_type = ARG_CONST_SIZE,
5627 .arg4_type = ARG_ANYTHING,
5630 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
5631 static int bpf_push_seg6_encap(struct sk_buff *skb, u32 type, void *hdr, u32 len)
5634 struct ipv6_sr_hdr *srh = (struct ipv6_sr_hdr *)hdr;
5636 if (!seg6_validate_srh(srh, len, false))
5640 case BPF_LWT_ENCAP_SEG6_INLINE:
5641 if (skb->protocol != htons(ETH_P_IPV6))
5644 err = seg6_do_srh_inline(skb, srh);
5646 case BPF_LWT_ENCAP_SEG6:
5647 skb_reset_inner_headers(skb);
5648 skb->encapsulation = 1;
5649 err = seg6_do_srh_encap(skb, srh, IPPROTO_IPV6);
5655 bpf_compute_data_pointers(skb);
5659 skb_set_transport_header(skb, sizeof(struct ipv6hdr));
5661 return seg6_lookup_nexthop(skb, NULL, 0);
5663 #endif /* CONFIG_IPV6_SEG6_BPF */
5665 #if IS_ENABLED(CONFIG_LWTUNNEL_BPF)
5666 static int bpf_push_ip_encap(struct sk_buff *skb, void *hdr, u32 len,
5669 return bpf_lwt_push_ip_encap(skb, hdr, len, ingress);
5673 BPF_CALL_4(bpf_lwt_in_push_encap, struct sk_buff *, skb, u32, type, void *, hdr,
5677 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
5678 case BPF_LWT_ENCAP_SEG6:
5679 case BPF_LWT_ENCAP_SEG6_INLINE:
5680 return bpf_push_seg6_encap(skb, type, hdr, len);
5682 #if IS_ENABLED(CONFIG_LWTUNNEL_BPF)
5683 case BPF_LWT_ENCAP_IP:
5684 return bpf_push_ip_encap(skb, hdr, len, true /* ingress */);
5691 BPF_CALL_4(bpf_lwt_xmit_push_encap, struct sk_buff *, skb, u32, type,
5692 void *, hdr, u32, len)
5695 #if IS_ENABLED(CONFIG_LWTUNNEL_BPF)
5696 case BPF_LWT_ENCAP_IP:
5697 return bpf_push_ip_encap(skb, hdr, len, false /* egress */);
5704 static const struct bpf_func_proto bpf_lwt_in_push_encap_proto = {
5705 .func = bpf_lwt_in_push_encap,
5707 .ret_type = RET_INTEGER,
5708 .arg1_type = ARG_PTR_TO_CTX,
5709 .arg2_type = ARG_ANYTHING,
5710 .arg3_type = ARG_PTR_TO_MEM,
5711 .arg4_type = ARG_CONST_SIZE
5714 static const struct bpf_func_proto bpf_lwt_xmit_push_encap_proto = {
5715 .func = bpf_lwt_xmit_push_encap,
5717 .ret_type = RET_INTEGER,
5718 .arg1_type = ARG_PTR_TO_CTX,
5719 .arg2_type = ARG_ANYTHING,
5720 .arg3_type = ARG_PTR_TO_MEM,
5721 .arg4_type = ARG_CONST_SIZE
5724 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
5725 BPF_CALL_4(bpf_lwt_seg6_store_bytes, struct sk_buff *, skb, u32, offset,
5726 const void *, from, u32, len)
5728 struct seg6_bpf_srh_state *srh_state =
5729 this_cpu_ptr(&seg6_bpf_srh_states);
5730 struct ipv6_sr_hdr *srh = srh_state->srh;
5731 void *srh_tlvs, *srh_end, *ptr;
5737 srh_tlvs = (void *)((char *)srh + ((srh->first_segment + 1) << 4));
5738 srh_end = (void *)((char *)srh + sizeof(*srh) + srh_state->hdrlen);
5740 ptr = skb->data + offset;
5741 if (ptr >= srh_tlvs && ptr + len <= srh_end)
5742 srh_state->valid = false;
5743 else if (ptr < (void *)&srh->flags ||
5744 ptr + len > (void *)&srh->segments)
5747 if (unlikely(bpf_try_make_writable(skb, offset + len)))
5749 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0)
5751 srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
5753 memcpy(skb->data + offset, from, len);
5757 static const struct bpf_func_proto bpf_lwt_seg6_store_bytes_proto = {
5758 .func = bpf_lwt_seg6_store_bytes,
5760 .ret_type = RET_INTEGER,
5761 .arg1_type = ARG_PTR_TO_CTX,
5762 .arg2_type = ARG_ANYTHING,
5763 .arg3_type = ARG_PTR_TO_MEM,
5764 .arg4_type = ARG_CONST_SIZE
5767 static void bpf_update_srh_state(struct sk_buff *skb)
5769 struct seg6_bpf_srh_state *srh_state =
5770 this_cpu_ptr(&seg6_bpf_srh_states);
5773 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0) {
5774 srh_state->srh = NULL;
5776 srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
5777 srh_state->hdrlen = srh_state->srh->hdrlen << 3;
5778 srh_state->valid = true;
5782 BPF_CALL_4(bpf_lwt_seg6_action, struct sk_buff *, skb,
5783 u32, action, void *, param, u32, param_len)
5785 struct seg6_bpf_srh_state *srh_state =
5786 this_cpu_ptr(&seg6_bpf_srh_states);
5791 case SEG6_LOCAL_ACTION_END_X:
5792 if (!seg6_bpf_has_valid_srh(skb))
5794 if (param_len != sizeof(struct in6_addr))
5796 return seg6_lookup_nexthop(skb, (struct in6_addr *)param, 0);
5797 case SEG6_LOCAL_ACTION_END_T:
5798 if (!seg6_bpf_has_valid_srh(skb))
5800 if (param_len != sizeof(int))
5802 return seg6_lookup_nexthop(skb, NULL, *(int *)param);
5803 case SEG6_LOCAL_ACTION_END_DT6:
5804 if (!seg6_bpf_has_valid_srh(skb))
5806 if (param_len != sizeof(int))
5809 if (ipv6_find_hdr(skb, &hdroff, IPPROTO_IPV6, NULL, NULL) < 0)
5811 if (!pskb_pull(skb, hdroff))
5814 skb_postpull_rcsum(skb, skb_network_header(skb), hdroff);
5815 skb_reset_network_header(skb);
5816 skb_reset_transport_header(skb);
5817 skb->encapsulation = 0;
5819 bpf_compute_data_pointers(skb);
5820 bpf_update_srh_state(skb);
5821 return seg6_lookup_nexthop(skb, NULL, *(int *)param);
5822 case SEG6_LOCAL_ACTION_END_B6:
5823 if (srh_state->srh && !seg6_bpf_has_valid_srh(skb))
5825 err = bpf_push_seg6_encap(skb, BPF_LWT_ENCAP_SEG6_INLINE,
5828 bpf_update_srh_state(skb);
5831 case SEG6_LOCAL_ACTION_END_B6_ENCAP:
5832 if (srh_state->srh && !seg6_bpf_has_valid_srh(skb))
5834 err = bpf_push_seg6_encap(skb, BPF_LWT_ENCAP_SEG6,
5837 bpf_update_srh_state(skb);
5845 static const struct bpf_func_proto bpf_lwt_seg6_action_proto = {
5846 .func = bpf_lwt_seg6_action,
5848 .ret_type = RET_INTEGER,
5849 .arg1_type = ARG_PTR_TO_CTX,
5850 .arg2_type = ARG_ANYTHING,
5851 .arg3_type = ARG_PTR_TO_MEM,
5852 .arg4_type = ARG_CONST_SIZE
5855 BPF_CALL_3(bpf_lwt_seg6_adjust_srh, struct sk_buff *, skb, u32, offset,
5858 struct seg6_bpf_srh_state *srh_state =
5859 this_cpu_ptr(&seg6_bpf_srh_states);
5860 struct ipv6_sr_hdr *srh = srh_state->srh;
5861 void *srh_end, *srh_tlvs, *ptr;
5862 struct ipv6hdr *hdr;
5866 if (unlikely(srh == NULL))
5869 srh_tlvs = (void *)((unsigned char *)srh + sizeof(*srh) +
5870 ((srh->first_segment + 1) << 4));
5871 srh_end = (void *)((unsigned char *)srh + sizeof(*srh) +
5873 ptr = skb->data + offset;
5875 if (unlikely(ptr < srh_tlvs || ptr > srh_end))
5877 if (unlikely(len < 0 && (void *)((char *)ptr - len) > srh_end))
5881 ret = skb_cow_head(skb, len);
5882 if (unlikely(ret < 0))
5885 ret = bpf_skb_net_hdr_push(skb, offset, len);
5887 ret = bpf_skb_net_hdr_pop(skb, offset, -1 * len);
5890 bpf_compute_data_pointers(skb);
5891 if (unlikely(ret < 0))
5894 hdr = (struct ipv6hdr *)skb->data;
5895 hdr->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
5897 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0)
5899 srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
5900 srh_state->hdrlen += len;
5901 srh_state->valid = false;
5905 static const struct bpf_func_proto bpf_lwt_seg6_adjust_srh_proto = {
5906 .func = bpf_lwt_seg6_adjust_srh,
5908 .ret_type = RET_INTEGER,
5909 .arg1_type = ARG_PTR_TO_CTX,
5910 .arg2_type = ARG_ANYTHING,
5911 .arg3_type = ARG_ANYTHING,
5913 #endif /* CONFIG_IPV6_SEG6_BPF */
5916 static struct sock *sk_lookup(struct net *net, struct bpf_sock_tuple *tuple,
5917 int dif, int sdif, u8 family, u8 proto)
5919 bool refcounted = false;
5920 struct sock *sk = NULL;
5922 if (family == AF_INET) {
5923 __be32 src4 = tuple->ipv4.saddr;
5924 __be32 dst4 = tuple->ipv4.daddr;
5926 if (proto == IPPROTO_TCP)
5927 sk = __inet_lookup(net, &tcp_hashinfo, NULL, 0,
5928 src4, tuple->ipv4.sport,
5929 dst4, tuple->ipv4.dport,
5930 dif, sdif, &refcounted);
5932 sk = __udp4_lib_lookup(net, src4, tuple->ipv4.sport,
5933 dst4, tuple->ipv4.dport,
5934 dif, sdif, &udp_table, NULL);
5935 #if IS_ENABLED(CONFIG_IPV6)
5937 struct in6_addr *src6 = (struct in6_addr *)&tuple->ipv6.saddr;
5938 struct in6_addr *dst6 = (struct in6_addr *)&tuple->ipv6.daddr;
5940 if (proto == IPPROTO_TCP)
5941 sk = __inet6_lookup(net, &tcp_hashinfo, NULL, 0,
5942 src6, tuple->ipv6.sport,
5943 dst6, ntohs(tuple->ipv6.dport),
5944 dif, sdif, &refcounted);
5945 else if (likely(ipv6_bpf_stub))
5946 sk = ipv6_bpf_stub->udp6_lib_lookup(net,
5947 src6, tuple->ipv6.sport,
5948 dst6, tuple->ipv6.dport,
5954 if (unlikely(sk && !refcounted && !sock_flag(sk, SOCK_RCU_FREE))) {
5955 WARN_ONCE(1, "Found non-RCU, unreferenced socket!");
5961 /* bpf_skc_lookup performs the core lookup for different types of sockets,
5962 * taking a reference on the socket if it doesn't have the flag SOCK_RCU_FREE.
5963 * Returns the socket as an 'unsigned long' to simplify the casting in the
5964 * callers to satisfy BPF_CALL declarations.
5966 static struct sock *
5967 __bpf_skc_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
5968 struct net *caller_net, u32 ifindex, u8 proto, u64 netns_id,
5971 struct sock *sk = NULL;
5972 u8 family = AF_UNSPEC;
5976 if (len == sizeof(tuple->ipv4))
5978 else if (len == sizeof(tuple->ipv6))
5983 if (unlikely(family == AF_UNSPEC || flags ||
5984 !((s32)netns_id < 0 || netns_id <= S32_MAX)))
5987 if (family == AF_INET)
5988 sdif = inet_sdif(skb);
5990 sdif = inet6_sdif(skb);
5992 if ((s32)netns_id < 0) {
5994 sk = sk_lookup(net, tuple, ifindex, sdif, family, proto);
5996 net = get_net_ns_by_id(caller_net, netns_id);
5999 sk = sk_lookup(net, tuple, ifindex, sdif, family, proto);
6007 static struct sock *
6008 __bpf_sk_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
6009 struct net *caller_net, u32 ifindex, u8 proto, u64 netns_id,
6012 struct sock *sk = __bpf_skc_lookup(skb, tuple, len, caller_net,
6013 ifindex, proto, netns_id, flags);
6016 struct sock *sk2 = sk_to_full_sk(sk);
6018 /* sk_to_full_sk() may return (sk)->rsk_listener, so make sure the original sk
6019 * sock refcnt is decremented to prevent a request_sock leak.
6021 if (!sk_fullsock(sk2))
6025 /* Ensure there is no need to bump sk2 refcnt */
6026 if (unlikely(sk2 && !sock_flag(sk2, SOCK_RCU_FREE))) {
6027 WARN_ONCE(1, "Found non-RCU, unreferenced socket!");
6037 static struct sock *
6038 bpf_skc_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
6039 u8 proto, u64 netns_id, u64 flags)
6041 struct net *caller_net;
6045 caller_net = dev_net(skb->dev);
6046 ifindex = skb->dev->ifindex;
6048 caller_net = sock_net(skb->sk);
6052 return __bpf_skc_lookup(skb, tuple, len, caller_net, ifindex, proto,
6056 static struct sock *
6057 bpf_sk_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
6058 u8 proto, u64 netns_id, u64 flags)
6060 struct sock *sk = bpf_skc_lookup(skb, tuple, len, proto, netns_id,
6064 struct sock *sk2 = sk_to_full_sk(sk);
6066 /* sk_to_full_sk() may return (sk)->rsk_listener, so make sure the original sk
6067 * sock refcnt is decremented to prevent a request_sock leak.
6069 if (!sk_fullsock(sk2))
6073 /* Ensure there is no need to bump sk2 refcnt */
6074 if (unlikely(sk2 && !sock_flag(sk2, SOCK_RCU_FREE))) {
6075 WARN_ONCE(1, "Found non-RCU, unreferenced socket!");
6085 BPF_CALL_5(bpf_skc_lookup_tcp, struct sk_buff *, skb,
6086 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
6088 return (unsigned long)bpf_skc_lookup(skb, tuple, len, IPPROTO_TCP,
6092 static const struct bpf_func_proto bpf_skc_lookup_tcp_proto = {
6093 .func = bpf_skc_lookup_tcp,
6096 .ret_type = RET_PTR_TO_SOCK_COMMON_OR_NULL,
6097 .arg1_type = ARG_PTR_TO_CTX,
6098 .arg2_type = ARG_PTR_TO_MEM,
6099 .arg3_type = ARG_CONST_SIZE,
6100 .arg4_type = ARG_ANYTHING,
6101 .arg5_type = ARG_ANYTHING,
6104 BPF_CALL_5(bpf_sk_lookup_tcp, struct sk_buff *, skb,
6105 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
6107 return (unsigned long)bpf_sk_lookup(skb, tuple, len, IPPROTO_TCP,
6111 static const struct bpf_func_proto bpf_sk_lookup_tcp_proto = {
6112 .func = bpf_sk_lookup_tcp,
6115 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
6116 .arg1_type = ARG_PTR_TO_CTX,
6117 .arg2_type = ARG_PTR_TO_MEM,
6118 .arg3_type = ARG_CONST_SIZE,
6119 .arg4_type = ARG_ANYTHING,
6120 .arg5_type = ARG_ANYTHING,
6123 BPF_CALL_5(bpf_sk_lookup_udp, struct sk_buff *, skb,
6124 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
6126 return (unsigned long)bpf_sk_lookup(skb, tuple, len, IPPROTO_UDP,
6130 static const struct bpf_func_proto bpf_sk_lookup_udp_proto = {
6131 .func = bpf_sk_lookup_udp,
6134 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
6135 .arg1_type = ARG_PTR_TO_CTX,
6136 .arg2_type = ARG_PTR_TO_MEM,
6137 .arg3_type = ARG_CONST_SIZE,
6138 .arg4_type = ARG_ANYTHING,
6139 .arg5_type = ARG_ANYTHING,
6142 BPF_CALL_1(bpf_sk_release, struct sock *, sk)
6144 if (sk && sk_is_refcounted(sk))
6149 static const struct bpf_func_proto bpf_sk_release_proto = {
6150 .func = bpf_sk_release,
6152 .ret_type = RET_INTEGER,
6153 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
6156 BPF_CALL_5(bpf_xdp_sk_lookup_udp, struct xdp_buff *, ctx,
6157 struct bpf_sock_tuple *, tuple, u32, len, u32, netns_id, u64, flags)
6159 struct net *caller_net = dev_net(ctx->rxq->dev);
6160 int ifindex = ctx->rxq->dev->ifindex;
6162 return (unsigned long)__bpf_sk_lookup(NULL, tuple, len, caller_net,
6163 ifindex, IPPROTO_UDP, netns_id,
6167 static const struct bpf_func_proto bpf_xdp_sk_lookup_udp_proto = {
6168 .func = bpf_xdp_sk_lookup_udp,
6171 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
6172 .arg1_type = ARG_PTR_TO_CTX,
6173 .arg2_type = ARG_PTR_TO_MEM,
6174 .arg3_type = ARG_CONST_SIZE,
6175 .arg4_type = ARG_ANYTHING,
6176 .arg5_type = ARG_ANYTHING,
6179 BPF_CALL_5(bpf_xdp_skc_lookup_tcp, struct xdp_buff *, ctx,
6180 struct bpf_sock_tuple *, tuple, u32, len, u32, netns_id, u64, flags)
6182 struct net *caller_net = dev_net(ctx->rxq->dev);
6183 int ifindex = ctx->rxq->dev->ifindex;
6185 return (unsigned long)__bpf_skc_lookup(NULL, tuple, len, caller_net,
6186 ifindex, IPPROTO_TCP, netns_id,
6190 static const struct bpf_func_proto bpf_xdp_skc_lookup_tcp_proto = {
6191 .func = bpf_xdp_skc_lookup_tcp,
6194 .ret_type = RET_PTR_TO_SOCK_COMMON_OR_NULL,
6195 .arg1_type = ARG_PTR_TO_CTX,
6196 .arg2_type = ARG_PTR_TO_MEM,
6197 .arg3_type = ARG_CONST_SIZE,
6198 .arg4_type = ARG_ANYTHING,
6199 .arg5_type = ARG_ANYTHING,
6202 BPF_CALL_5(bpf_xdp_sk_lookup_tcp, struct xdp_buff *, ctx,
6203 struct bpf_sock_tuple *, tuple, u32, len, u32, netns_id, u64, flags)
6205 struct net *caller_net = dev_net(ctx->rxq->dev);
6206 int ifindex = ctx->rxq->dev->ifindex;
6208 return (unsigned long)__bpf_sk_lookup(NULL, tuple, len, caller_net,
6209 ifindex, IPPROTO_TCP, netns_id,
6213 static const struct bpf_func_proto bpf_xdp_sk_lookup_tcp_proto = {
6214 .func = bpf_xdp_sk_lookup_tcp,
6217 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
6218 .arg1_type = ARG_PTR_TO_CTX,
6219 .arg2_type = ARG_PTR_TO_MEM,
6220 .arg3_type = ARG_CONST_SIZE,
6221 .arg4_type = ARG_ANYTHING,
6222 .arg5_type = ARG_ANYTHING,
6225 BPF_CALL_5(bpf_sock_addr_skc_lookup_tcp, struct bpf_sock_addr_kern *, ctx,
6226 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
6228 return (unsigned long)__bpf_skc_lookup(NULL, tuple, len,
6229 sock_net(ctx->sk), 0,
6230 IPPROTO_TCP, netns_id, flags);
6233 static const struct bpf_func_proto bpf_sock_addr_skc_lookup_tcp_proto = {
6234 .func = bpf_sock_addr_skc_lookup_tcp,
6236 .ret_type = RET_PTR_TO_SOCK_COMMON_OR_NULL,
6237 .arg1_type = ARG_PTR_TO_CTX,
6238 .arg2_type = ARG_PTR_TO_MEM,
6239 .arg3_type = ARG_CONST_SIZE,
6240 .arg4_type = ARG_ANYTHING,
6241 .arg5_type = ARG_ANYTHING,
6244 BPF_CALL_5(bpf_sock_addr_sk_lookup_tcp, struct bpf_sock_addr_kern *, ctx,
6245 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
6247 return (unsigned long)__bpf_sk_lookup(NULL, tuple, len,
6248 sock_net(ctx->sk), 0, IPPROTO_TCP,
6252 static const struct bpf_func_proto bpf_sock_addr_sk_lookup_tcp_proto = {
6253 .func = bpf_sock_addr_sk_lookup_tcp,
6255 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
6256 .arg1_type = ARG_PTR_TO_CTX,
6257 .arg2_type = ARG_PTR_TO_MEM,
6258 .arg3_type = ARG_CONST_SIZE,
6259 .arg4_type = ARG_ANYTHING,
6260 .arg5_type = ARG_ANYTHING,
6263 BPF_CALL_5(bpf_sock_addr_sk_lookup_udp, struct bpf_sock_addr_kern *, ctx,
6264 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
6266 return (unsigned long)__bpf_sk_lookup(NULL, tuple, len,
6267 sock_net(ctx->sk), 0, IPPROTO_UDP,
6271 static const struct bpf_func_proto bpf_sock_addr_sk_lookup_udp_proto = {
6272 .func = bpf_sock_addr_sk_lookup_udp,
6274 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
6275 .arg1_type = ARG_PTR_TO_CTX,
6276 .arg2_type = ARG_PTR_TO_MEM,
6277 .arg3_type = ARG_CONST_SIZE,
6278 .arg4_type = ARG_ANYTHING,
6279 .arg5_type = ARG_ANYTHING,
6282 bool bpf_tcp_sock_is_valid_access(int off, int size, enum bpf_access_type type,
6283 struct bpf_insn_access_aux *info)
6285 if (off < 0 || off >= offsetofend(struct bpf_tcp_sock,
6289 if (off % size != 0)
6293 case offsetof(struct bpf_tcp_sock, bytes_received):
6294 case offsetof(struct bpf_tcp_sock, bytes_acked):
6295 return size == sizeof(__u64);
6297 return size == sizeof(__u32);
6301 u32 bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type,
6302 const struct bpf_insn *si,
6303 struct bpf_insn *insn_buf,
6304 struct bpf_prog *prog, u32 *target_size)
6306 struct bpf_insn *insn = insn_buf;
6308 #define BPF_TCP_SOCK_GET_COMMON(FIELD) \
6310 BUILD_BUG_ON(sizeof_field(struct tcp_sock, FIELD) > \
6311 sizeof_field(struct bpf_tcp_sock, FIELD)); \
6312 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct tcp_sock, FIELD),\
6313 si->dst_reg, si->src_reg, \
6314 offsetof(struct tcp_sock, FIELD)); \
6317 #define BPF_INET_SOCK_GET_COMMON(FIELD) \
6319 BUILD_BUG_ON(sizeof_field(struct inet_connection_sock, \
6321 sizeof_field(struct bpf_tcp_sock, FIELD)); \
6322 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
6323 struct inet_connection_sock, \
6325 si->dst_reg, si->src_reg, \
6327 struct inet_connection_sock, \
6331 if (insn > insn_buf)
6332 return insn - insn_buf;
6335 case offsetof(struct bpf_tcp_sock, rtt_min):
6336 BUILD_BUG_ON(sizeof_field(struct tcp_sock, rtt_min) !=
6337 sizeof(struct minmax));
6338 BUILD_BUG_ON(sizeof(struct minmax) <
6339 sizeof(struct minmax_sample));
6341 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6342 offsetof(struct tcp_sock, rtt_min) +
6343 offsetof(struct minmax_sample, v));
6345 case offsetof(struct bpf_tcp_sock, snd_cwnd):
6346 BPF_TCP_SOCK_GET_COMMON(snd_cwnd);
6348 case offsetof(struct bpf_tcp_sock, srtt_us):
6349 BPF_TCP_SOCK_GET_COMMON(srtt_us);
6351 case offsetof(struct bpf_tcp_sock, snd_ssthresh):
6352 BPF_TCP_SOCK_GET_COMMON(snd_ssthresh);
6354 case offsetof(struct bpf_tcp_sock, rcv_nxt):
6355 BPF_TCP_SOCK_GET_COMMON(rcv_nxt);
6357 case offsetof(struct bpf_tcp_sock, snd_nxt):
6358 BPF_TCP_SOCK_GET_COMMON(snd_nxt);
6360 case offsetof(struct bpf_tcp_sock, snd_una):
6361 BPF_TCP_SOCK_GET_COMMON(snd_una);
6363 case offsetof(struct bpf_tcp_sock, mss_cache):
6364 BPF_TCP_SOCK_GET_COMMON(mss_cache);
6366 case offsetof(struct bpf_tcp_sock, ecn_flags):
6367 BPF_TCP_SOCK_GET_COMMON(ecn_flags);
6369 case offsetof(struct bpf_tcp_sock, rate_delivered):
6370 BPF_TCP_SOCK_GET_COMMON(rate_delivered);
6372 case offsetof(struct bpf_tcp_sock, rate_interval_us):
6373 BPF_TCP_SOCK_GET_COMMON(rate_interval_us);
6375 case offsetof(struct bpf_tcp_sock, packets_out):
6376 BPF_TCP_SOCK_GET_COMMON(packets_out);
6378 case offsetof(struct bpf_tcp_sock, retrans_out):
6379 BPF_TCP_SOCK_GET_COMMON(retrans_out);
6381 case offsetof(struct bpf_tcp_sock, total_retrans):
6382 BPF_TCP_SOCK_GET_COMMON(total_retrans);
6384 case offsetof(struct bpf_tcp_sock, segs_in):
6385 BPF_TCP_SOCK_GET_COMMON(segs_in);
6387 case offsetof(struct bpf_tcp_sock, data_segs_in):
6388 BPF_TCP_SOCK_GET_COMMON(data_segs_in);
6390 case offsetof(struct bpf_tcp_sock, segs_out):
6391 BPF_TCP_SOCK_GET_COMMON(segs_out);
6393 case offsetof(struct bpf_tcp_sock, data_segs_out):
6394 BPF_TCP_SOCK_GET_COMMON(data_segs_out);
6396 case offsetof(struct bpf_tcp_sock, lost_out):
6397 BPF_TCP_SOCK_GET_COMMON(lost_out);
6399 case offsetof(struct bpf_tcp_sock, sacked_out):
6400 BPF_TCP_SOCK_GET_COMMON(sacked_out);
6402 case offsetof(struct bpf_tcp_sock, bytes_received):
6403 BPF_TCP_SOCK_GET_COMMON(bytes_received);
6405 case offsetof(struct bpf_tcp_sock, bytes_acked):
6406 BPF_TCP_SOCK_GET_COMMON(bytes_acked);
6408 case offsetof(struct bpf_tcp_sock, dsack_dups):
6409 BPF_TCP_SOCK_GET_COMMON(dsack_dups);
6411 case offsetof(struct bpf_tcp_sock, delivered):
6412 BPF_TCP_SOCK_GET_COMMON(delivered);
6414 case offsetof(struct bpf_tcp_sock, delivered_ce):
6415 BPF_TCP_SOCK_GET_COMMON(delivered_ce);
6417 case offsetof(struct bpf_tcp_sock, icsk_retransmits):
6418 BPF_INET_SOCK_GET_COMMON(icsk_retransmits);
6422 return insn - insn_buf;
6425 BPF_CALL_1(bpf_tcp_sock, struct sock *, sk)
6427 if (sk_fullsock(sk) && sk->sk_protocol == IPPROTO_TCP)
6428 return (unsigned long)sk;
6430 return (unsigned long)NULL;
6433 const struct bpf_func_proto bpf_tcp_sock_proto = {
6434 .func = bpf_tcp_sock,
6436 .ret_type = RET_PTR_TO_TCP_SOCK_OR_NULL,
6437 .arg1_type = ARG_PTR_TO_SOCK_COMMON,
6440 BPF_CALL_1(bpf_get_listener_sock, struct sock *, sk)
6442 sk = sk_to_full_sk(sk);
6444 if (sk->sk_state == TCP_LISTEN && sock_flag(sk, SOCK_RCU_FREE))
6445 return (unsigned long)sk;
6447 return (unsigned long)NULL;
6450 static const struct bpf_func_proto bpf_get_listener_sock_proto = {
6451 .func = bpf_get_listener_sock,
6453 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
6454 .arg1_type = ARG_PTR_TO_SOCK_COMMON,
6457 BPF_CALL_1(bpf_skb_ecn_set_ce, struct sk_buff *, skb)
6459 unsigned int iphdr_len;
6461 switch (skb_protocol(skb, true)) {
6462 case cpu_to_be16(ETH_P_IP):
6463 iphdr_len = sizeof(struct iphdr);
6465 case cpu_to_be16(ETH_P_IPV6):
6466 iphdr_len = sizeof(struct ipv6hdr);
6472 if (skb_headlen(skb) < iphdr_len)
6475 if (skb_cloned(skb) && !skb_clone_writable(skb, iphdr_len))
6478 return INET_ECN_set_ce(skb);
6481 bool bpf_xdp_sock_is_valid_access(int off, int size, enum bpf_access_type type,
6482 struct bpf_insn_access_aux *info)
6484 if (off < 0 || off >= offsetofend(struct bpf_xdp_sock, queue_id))
6487 if (off % size != 0)
6492 return size == sizeof(__u32);
6496 u32 bpf_xdp_sock_convert_ctx_access(enum bpf_access_type type,
6497 const struct bpf_insn *si,
6498 struct bpf_insn *insn_buf,
6499 struct bpf_prog *prog, u32 *target_size)
6501 struct bpf_insn *insn = insn_buf;
6503 #define BPF_XDP_SOCK_GET(FIELD) \
6505 BUILD_BUG_ON(sizeof_field(struct xdp_sock, FIELD) > \
6506 sizeof_field(struct bpf_xdp_sock, FIELD)); \
6507 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_sock, FIELD),\
6508 si->dst_reg, si->src_reg, \
6509 offsetof(struct xdp_sock, FIELD)); \
6513 case offsetof(struct bpf_xdp_sock, queue_id):
6514 BPF_XDP_SOCK_GET(queue_id);
6518 return insn - insn_buf;
6521 static const struct bpf_func_proto bpf_skb_ecn_set_ce_proto = {
6522 .func = bpf_skb_ecn_set_ce,
6524 .ret_type = RET_INTEGER,
6525 .arg1_type = ARG_PTR_TO_CTX,
6528 BPF_CALL_5(bpf_tcp_check_syncookie, struct sock *, sk, void *, iph, u32, iph_len,
6529 struct tcphdr *, th, u32, th_len)
6531 #ifdef CONFIG_SYN_COOKIES
6535 if (unlikely(!sk || th_len < sizeof(*th)))
6538 /* sk_listener() allows TCP_NEW_SYN_RECV, which makes no sense here. */
6539 if (sk->sk_protocol != IPPROTO_TCP || sk->sk_state != TCP_LISTEN)
6542 if (!READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_syncookies))
6545 if (!th->ack || th->rst || th->syn)
6548 if (unlikely(iph_len < sizeof(struct iphdr)))
6551 if (tcp_synq_no_recent_overflow(sk))
6554 cookie = ntohl(th->ack_seq) - 1;
6556 /* Both struct iphdr and struct ipv6hdr have the version field at the
6557 * same offset so we can cast to the shorter header (struct iphdr).
6559 switch (((struct iphdr *)iph)->version) {
6561 if (sk->sk_family == AF_INET6 && ipv6_only_sock(sk))
6564 ret = __cookie_v4_check((struct iphdr *)iph, th, cookie);
6567 #if IS_BUILTIN(CONFIG_IPV6)
6569 if (unlikely(iph_len < sizeof(struct ipv6hdr)))
6572 if (sk->sk_family != AF_INET6)
6575 ret = __cookie_v6_check((struct ipv6hdr *)iph, th, cookie);
6577 #endif /* CONFIG_IPV6 */
6580 return -EPROTONOSUPPORT;
6592 static const struct bpf_func_proto bpf_tcp_check_syncookie_proto = {
6593 .func = bpf_tcp_check_syncookie,
6596 .ret_type = RET_INTEGER,
6597 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
6598 .arg2_type = ARG_PTR_TO_MEM,
6599 .arg3_type = ARG_CONST_SIZE,
6600 .arg4_type = ARG_PTR_TO_MEM,
6601 .arg5_type = ARG_CONST_SIZE,
6604 BPF_CALL_5(bpf_tcp_gen_syncookie, struct sock *, sk, void *, iph, u32, iph_len,
6605 struct tcphdr *, th, u32, th_len)
6607 #ifdef CONFIG_SYN_COOKIES
6611 if (unlikely(!sk || th_len < sizeof(*th) || th_len != th->doff * 4))
6614 if (sk->sk_protocol != IPPROTO_TCP || sk->sk_state != TCP_LISTEN)
6617 if (!READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_syncookies))
6620 if (!th->syn || th->ack || th->fin || th->rst)
6623 if (unlikely(iph_len < sizeof(struct iphdr)))
6626 /* Both struct iphdr and struct ipv6hdr have the version field at the
6627 * same offset so we can cast to the shorter header (struct iphdr).
6629 switch (((struct iphdr *)iph)->version) {
6631 if (sk->sk_family == AF_INET6 && sk->sk_ipv6only)
6634 mss = tcp_v4_get_syncookie(sk, iph, th, &cookie);
6637 #if IS_BUILTIN(CONFIG_IPV6)
6639 if (unlikely(iph_len < sizeof(struct ipv6hdr)))
6642 if (sk->sk_family != AF_INET6)
6645 mss = tcp_v6_get_syncookie(sk, iph, th, &cookie);
6647 #endif /* CONFIG_IPV6 */
6650 return -EPROTONOSUPPORT;
6655 return cookie | ((u64)mss << 32);
6658 #endif /* CONFIG_SYN_COOKIES */
6661 static const struct bpf_func_proto bpf_tcp_gen_syncookie_proto = {
6662 .func = bpf_tcp_gen_syncookie,
6663 .gpl_only = true, /* __cookie_v*_init_sequence() is GPL */
6665 .ret_type = RET_INTEGER,
6666 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
6667 .arg2_type = ARG_PTR_TO_MEM,
6668 .arg3_type = ARG_CONST_SIZE,
6669 .arg4_type = ARG_PTR_TO_MEM,
6670 .arg5_type = ARG_CONST_SIZE,
6673 BPF_CALL_3(bpf_sk_assign, struct sk_buff *, skb, struct sock *, sk, u64, flags)
6675 if (!sk || flags != 0)
6677 if (!skb_at_tc_ingress(skb))
6679 if (unlikely(dev_net(skb->dev) != sock_net(sk)))
6680 return -ENETUNREACH;
6681 if (unlikely(sk_fullsock(sk) && sk->sk_reuseport))
6682 return -ESOCKTNOSUPPORT;
6683 if (sk_unhashed(sk))
6685 if (sk_is_refcounted(sk) &&
6686 unlikely(!refcount_inc_not_zero(&sk->sk_refcnt)))
6691 skb->destructor = sock_pfree;
6696 static const struct bpf_func_proto bpf_sk_assign_proto = {
6697 .func = bpf_sk_assign,
6699 .ret_type = RET_INTEGER,
6700 .arg1_type = ARG_PTR_TO_CTX,
6701 .arg2_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
6702 .arg3_type = ARG_ANYTHING,
6705 static const u8 *bpf_search_tcp_opt(const u8 *op, const u8 *opend,
6706 u8 search_kind, const u8 *magic,
6707 u8 magic_len, bool *eol)
6713 while (op < opend) {
6716 if (kind == TCPOPT_EOL) {
6718 return ERR_PTR(-ENOMSG);
6719 } else if (kind == TCPOPT_NOP) {
6724 if (opend - op < 2 || opend - op < op[1] || op[1] < 2)
6725 /* Something is wrong in the received header.
6726 * Follow the TCP stack's tcp_parse_options()
6727 * and just bail here.
6729 return ERR_PTR(-EFAULT);
6732 if (search_kind == kind) {
6736 if (magic_len > kind_len - 2)
6737 return ERR_PTR(-ENOMSG);
6739 if (!memcmp(&op[2], magic, magic_len))
6746 return ERR_PTR(-ENOMSG);
6749 BPF_CALL_4(bpf_sock_ops_load_hdr_opt, struct bpf_sock_ops_kern *, bpf_sock,
6750 void *, search_res, u32, len, u64, flags)
6752 bool eol, load_syn = flags & BPF_LOAD_HDR_OPT_TCP_SYN;
6753 const u8 *op, *opend, *magic, *search = search_res;
6754 u8 search_kind, search_len, copy_len, magic_len;
6757 /* 2 byte is the minimal option len except TCPOPT_NOP and
6758 * TCPOPT_EOL which are useless for the bpf prog to learn
6759 * and this helper disallow loading them also.
6761 if (len < 2 || flags & ~BPF_LOAD_HDR_OPT_TCP_SYN)
6764 search_kind = search[0];
6765 search_len = search[1];
6767 if (search_len > len || search_kind == TCPOPT_NOP ||
6768 search_kind == TCPOPT_EOL)
6771 if (search_kind == TCPOPT_EXP || search_kind == 253) {
6772 /* 16 or 32 bit magic. +2 for kind and kind length */
6773 if (search_len != 4 && search_len != 6)
6776 magic_len = search_len - 2;
6785 ret = bpf_sock_ops_get_syn(bpf_sock, TCP_BPF_SYN, &op);
6790 op += sizeof(struct tcphdr);
6792 if (!bpf_sock->skb ||
6793 bpf_sock->op == BPF_SOCK_OPS_HDR_OPT_LEN_CB)
6794 /* This bpf_sock->op cannot call this helper */
6797 opend = bpf_sock->skb_data_end;
6798 op = bpf_sock->skb->data + sizeof(struct tcphdr);
6801 op = bpf_search_tcp_opt(op, opend, search_kind, magic, magic_len,
6808 if (copy_len > len) {
6813 memcpy(search_res, op, copy_len);
6817 static const struct bpf_func_proto bpf_sock_ops_load_hdr_opt_proto = {
6818 .func = bpf_sock_ops_load_hdr_opt,
6820 .ret_type = RET_INTEGER,
6821 .arg1_type = ARG_PTR_TO_CTX,
6822 .arg2_type = ARG_PTR_TO_MEM,
6823 .arg3_type = ARG_CONST_SIZE,
6824 .arg4_type = ARG_ANYTHING,
6827 BPF_CALL_4(bpf_sock_ops_store_hdr_opt, struct bpf_sock_ops_kern *, bpf_sock,
6828 const void *, from, u32, len, u64, flags)
6830 u8 new_kind, new_kind_len, magic_len = 0, *opend;
6831 const u8 *op, *new_op, *magic = NULL;
6832 struct sk_buff *skb;
6835 if (bpf_sock->op != BPF_SOCK_OPS_WRITE_HDR_OPT_CB)
6838 if (len < 2 || flags)
6842 new_kind = new_op[0];
6843 new_kind_len = new_op[1];
6845 if (new_kind_len > len || new_kind == TCPOPT_NOP ||
6846 new_kind == TCPOPT_EOL)
6849 if (new_kind_len > bpf_sock->remaining_opt_len)
6852 /* 253 is another experimental kind */
6853 if (new_kind == TCPOPT_EXP || new_kind == 253) {
6854 if (new_kind_len < 4)
6856 /* Match for the 2 byte magic also.
6857 * RFC 6994: the magic could be 2 or 4 bytes.
6858 * Hence, matching by 2 byte only is on the
6859 * conservative side but it is the right
6860 * thing to do for the 'search-for-duplication'
6867 /* Check for duplication */
6868 skb = bpf_sock->skb;
6869 op = skb->data + sizeof(struct tcphdr);
6870 opend = bpf_sock->skb_data_end;
6872 op = bpf_search_tcp_opt(op, opend, new_kind, magic, magic_len,
6877 if (PTR_ERR(op) != -ENOMSG)
6881 /* The option has been ended. Treat it as no more
6882 * header option can be written.
6886 /* No duplication found. Store the header option. */
6887 memcpy(opend, from, new_kind_len);
6889 bpf_sock->remaining_opt_len -= new_kind_len;
6890 bpf_sock->skb_data_end += new_kind_len;
6895 static const struct bpf_func_proto bpf_sock_ops_store_hdr_opt_proto = {
6896 .func = bpf_sock_ops_store_hdr_opt,
6898 .ret_type = RET_INTEGER,
6899 .arg1_type = ARG_PTR_TO_CTX,
6900 .arg2_type = ARG_PTR_TO_MEM,
6901 .arg3_type = ARG_CONST_SIZE,
6902 .arg4_type = ARG_ANYTHING,
6905 BPF_CALL_3(bpf_sock_ops_reserve_hdr_opt, struct bpf_sock_ops_kern *, bpf_sock,
6906 u32, len, u64, flags)
6908 if (bpf_sock->op != BPF_SOCK_OPS_HDR_OPT_LEN_CB)
6911 if (flags || len < 2)
6914 if (len > bpf_sock->remaining_opt_len)
6917 bpf_sock->remaining_opt_len -= len;
6922 static const struct bpf_func_proto bpf_sock_ops_reserve_hdr_opt_proto = {
6923 .func = bpf_sock_ops_reserve_hdr_opt,
6925 .ret_type = RET_INTEGER,
6926 .arg1_type = ARG_PTR_TO_CTX,
6927 .arg2_type = ARG_ANYTHING,
6928 .arg3_type = ARG_ANYTHING,
6931 #endif /* CONFIG_INET */
6933 bool bpf_helper_changes_pkt_data(void *func)
6935 if (func == bpf_skb_vlan_push ||
6936 func == bpf_skb_vlan_pop ||
6937 func == bpf_skb_store_bytes ||
6938 func == bpf_skb_change_proto ||
6939 func == bpf_skb_change_head ||
6940 func == sk_skb_change_head ||
6941 func == bpf_skb_change_tail ||
6942 func == sk_skb_change_tail ||
6943 func == bpf_skb_adjust_room ||
6944 func == sk_skb_adjust_room ||
6945 func == bpf_skb_pull_data ||
6946 func == sk_skb_pull_data ||
6947 func == bpf_clone_redirect ||
6948 func == bpf_l3_csum_replace ||
6949 func == bpf_l4_csum_replace ||
6950 func == bpf_xdp_adjust_head ||
6951 func == bpf_xdp_adjust_meta ||
6952 func == bpf_msg_pull_data ||
6953 func == bpf_msg_push_data ||
6954 func == bpf_msg_pop_data ||
6955 func == bpf_xdp_adjust_tail ||
6956 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
6957 func == bpf_lwt_seg6_store_bytes ||
6958 func == bpf_lwt_seg6_adjust_srh ||
6959 func == bpf_lwt_seg6_action ||
6962 func == bpf_sock_ops_store_hdr_opt ||
6964 func == bpf_lwt_in_push_encap ||
6965 func == bpf_lwt_xmit_push_encap)
6971 const struct bpf_func_proto bpf_event_output_data_proto __weak;
6972 const struct bpf_func_proto bpf_sk_storage_get_cg_sock_proto __weak;
6974 static const struct bpf_func_proto *
6975 sock_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6978 /* inet and inet6 sockets are created in a process
6979 * context so there is always a valid uid/gid
6981 case BPF_FUNC_get_current_uid_gid:
6982 return &bpf_get_current_uid_gid_proto;
6983 case BPF_FUNC_get_local_storage:
6984 return &bpf_get_local_storage_proto;
6985 case BPF_FUNC_get_socket_cookie:
6986 return &bpf_get_socket_cookie_sock_proto;
6987 case BPF_FUNC_get_netns_cookie:
6988 return &bpf_get_netns_cookie_sock_proto;
6989 case BPF_FUNC_perf_event_output:
6990 return &bpf_event_output_data_proto;
6991 case BPF_FUNC_get_current_pid_tgid:
6992 return &bpf_get_current_pid_tgid_proto;
6993 case BPF_FUNC_get_current_comm:
6994 return &bpf_get_current_comm_proto;
6995 #ifdef CONFIG_CGROUPS
6996 case BPF_FUNC_get_current_cgroup_id:
6997 return &bpf_get_current_cgroup_id_proto;
6998 case BPF_FUNC_get_current_ancestor_cgroup_id:
6999 return &bpf_get_current_ancestor_cgroup_id_proto;
7001 #ifdef CONFIG_CGROUP_NET_CLASSID
7002 case BPF_FUNC_get_cgroup_classid:
7003 return &bpf_get_cgroup_classid_curr_proto;
7005 case BPF_FUNC_sk_storage_get:
7006 return &bpf_sk_storage_get_cg_sock_proto;
7008 return bpf_base_func_proto(func_id);
7012 static const struct bpf_func_proto *
7013 sock_addr_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
7016 /* inet and inet6 sockets are created in a process
7017 * context so there is always a valid uid/gid
7019 case BPF_FUNC_get_current_uid_gid:
7020 return &bpf_get_current_uid_gid_proto;
7022 switch (prog->expected_attach_type) {
7023 case BPF_CGROUP_INET4_CONNECT:
7024 case BPF_CGROUP_INET6_CONNECT:
7025 return &bpf_bind_proto;
7029 case BPF_FUNC_get_socket_cookie:
7030 return &bpf_get_socket_cookie_sock_addr_proto;
7031 case BPF_FUNC_get_netns_cookie:
7032 return &bpf_get_netns_cookie_sock_addr_proto;
7033 case BPF_FUNC_get_local_storage:
7034 return &bpf_get_local_storage_proto;
7035 case BPF_FUNC_perf_event_output:
7036 return &bpf_event_output_data_proto;
7037 case BPF_FUNC_get_current_pid_tgid:
7038 return &bpf_get_current_pid_tgid_proto;
7039 case BPF_FUNC_get_current_comm:
7040 return &bpf_get_current_comm_proto;
7041 #ifdef CONFIG_CGROUPS
7042 case BPF_FUNC_get_current_cgroup_id:
7043 return &bpf_get_current_cgroup_id_proto;
7044 case BPF_FUNC_get_current_ancestor_cgroup_id:
7045 return &bpf_get_current_ancestor_cgroup_id_proto;
7047 #ifdef CONFIG_CGROUP_NET_CLASSID
7048 case BPF_FUNC_get_cgroup_classid:
7049 return &bpf_get_cgroup_classid_curr_proto;
7052 case BPF_FUNC_sk_lookup_tcp:
7053 return &bpf_sock_addr_sk_lookup_tcp_proto;
7054 case BPF_FUNC_sk_lookup_udp:
7055 return &bpf_sock_addr_sk_lookup_udp_proto;
7056 case BPF_FUNC_sk_release:
7057 return &bpf_sk_release_proto;
7058 case BPF_FUNC_skc_lookup_tcp:
7059 return &bpf_sock_addr_skc_lookup_tcp_proto;
7060 #endif /* CONFIG_INET */
7061 case BPF_FUNC_sk_storage_get:
7062 return &bpf_sk_storage_get_proto;
7063 case BPF_FUNC_sk_storage_delete:
7064 return &bpf_sk_storage_delete_proto;
7065 case BPF_FUNC_setsockopt:
7066 switch (prog->expected_attach_type) {
7067 case BPF_CGROUP_INET4_CONNECT:
7068 case BPF_CGROUP_INET6_CONNECT:
7069 return &bpf_sock_addr_setsockopt_proto;
7073 case BPF_FUNC_getsockopt:
7074 switch (prog->expected_attach_type) {
7075 case BPF_CGROUP_INET4_CONNECT:
7076 case BPF_CGROUP_INET6_CONNECT:
7077 return &bpf_sock_addr_getsockopt_proto;
7082 return bpf_sk_base_func_proto(func_id);
7086 static const struct bpf_func_proto *
7087 sk_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
7090 case BPF_FUNC_skb_load_bytes:
7091 return &bpf_skb_load_bytes_proto;
7092 case BPF_FUNC_skb_load_bytes_relative:
7093 return &bpf_skb_load_bytes_relative_proto;
7094 case BPF_FUNC_get_socket_cookie:
7095 return &bpf_get_socket_cookie_proto;
7096 case BPF_FUNC_get_socket_uid:
7097 return &bpf_get_socket_uid_proto;
7098 case BPF_FUNC_perf_event_output:
7099 return &bpf_skb_event_output_proto;
7101 return bpf_sk_base_func_proto(func_id);
7105 const struct bpf_func_proto bpf_sk_storage_get_proto __weak;
7106 const struct bpf_func_proto bpf_sk_storage_delete_proto __weak;
7108 static const struct bpf_func_proto *
7109 cg_skb_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
7112 case BPF_FUNC_get_local_storage:
7113 return &bpf_get_local_storage_proto;
7114 case BPF_FUNC_sk_fullsock:
7115 return &bpf_sk_fullsock_proto;
7116 case BPF_FUNC_sk_storage_get:
7117 return &bpf_sk_storage_get_proto;
7118 case BPF_FUNC_sk_storage_delete:
7119 return &bpf_sk_storage_delete_proto;
7120 case BPF_FUNC_perf_event_output:
7121 return &bpf_skb_event_output_proto;
7122 #ifdef CONFIG_SOCK_CGROUP_DATA
7123 case BPF_FUNC_skb_cgroup_id:
7124 return &bpf_skb_cgroup_id_proto;
7125 case BPF_FUNC_skb_ancestor_cgroup_id:
7126 return &bpf_skb_ancestor_cgroup_id_proto;
7127 case BPF_FUNC_sk_cgroup_id:
7128 return &bpf_sk_cgroup_id_proto;
7129 case BPF_FUNC_sk_ancestor_cgroup_id:
7130 return &bpf_sk_ancestor_cgroup_id_proto;
7133 case BPF_FUNC_sk_lookup_tcp:
7134 return &bpf_sk_lookup_tcp_proto;
7135 case BPF_FUNC_sk_lookup_udp:
7136 return &bpf_sk_lookup_udp_proto;
7137 case BPF_FUNC_sk_release:
7138 return &bpf_sk_release_proto;
7139 case BPF_FUNC_skc_lookup_tcp:
7140 return &bpf_skc_lookup_tcp_proto;
7141 case BPF_FUNC_tcp_sock:
7142 return &bpf_tcp_sock_proto;
7143 case BPF_FUNC_get_listener_sock:
7144 return &bpf_get_listener_sock_proto;
7145 case BPF_FUNC_skb_ecn_set_ce:
7146 return &bpf_skb_ecn_set_ce_proto;
7149 return sk_filter_func_proto(func_id, prog);
7153 static const struct bpf_func_proto *
7154 tc_cls_act_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
7157 case BPF_FUNC_skb_store_bytes:
7158 return &bpf_skb_store_bytes_proto;
7159 case BPF_FUNC_skb_load_bytes:
7160 return &bpf_skb_load_bytes_proto;
7161 case BPF_FUNC_skb_load_bytes_relative:
7162 return &bpf_skb_load_bytes_relative_proto;
7163 case BPF_FUNC_skb_pull_data:
7164 return &bpf_skb_pull_data_proto;
7165 case BPF_FUNC_csum_diff:
7166 return &bpf_csum_diff_proto;
7167 case BPF_FUNC_csum_update:
7168 return &bpf_csum_update_proto;
7169 case BPF_FUNC_csum_level:
7170 return &bpf_csum_level_proto;
7171 case BPF_FUNC_l3_csum_replace:
7172 return &bpf_l3_csum_replace_proto;
7173 case BPF_FUNC_l4_csum_replace:
7174 return &bpf_l4_csum_replace_proto;
7175 case BPF_FUNC_clone_redirect:
7176 return &bpf_clone_redirect_proto;
7177 case BPF_FUNC_get_cgroup_classid:
7178 return &bpf_get_cgroup_classid_proto;
7179 case BPF_FUNC_skb_vlan_push:
7180 return &bpf_skb_vlan_push_proto;
7181 case BPF_FUNC_skb_vlan_pop:
7182 return &bpf_skb_vlan_pop_proto;
7183 case BPF_FUNC_skb_change_proto:
7184 return &bpf_skb_change_proto_proto;
7185 case BPF_FUNC_skb_change_type:
7186 return &bpf_skb_change_type_proto;
7187 case BPF_FUNC_skb_adjust_room:
7188 return &bpf_skb_adjust_room_proto;
7189 case BPF_FUNC_skb_change_tail:
7190 return &bpf_skb_change_tail_proto;
7191 case BPF_FUNC_skb_change_head:
7192 return &bpf_skb_change_head_proto;
7193 case BPF_FUNC_skb_get_tunnel_key:
7194 return &bpf_skb_get_tunnel_key_proto;
7195 case BPF_FUNC_skb_set_tunnel_key:
7196 return bpf_get_skb_set_tunnel_proto(func_id);
7197 case BPF_FUNC_skb_get_tunnel_opt:
7198 return &bpf_skb_get_tunnel_opt_proto;
7199 case BPF_FUNC_skb_set_tunnel_opt:
7200 return bpf_get_skb_set_tunnel_proto(func_id);
7201 case BPF_FUNC_redirect:
7202 return &bpf_redirect_proto;
7203 case BPF_FUNC_redirect_neigh:
7204 return &bpf_redirect_neigh_proto;
7205 case BPF_FUNC_redirect_peer:
7206 return &bpf_redirect_peer_proto;
7207 case BPF_FUNC_get_route_realm:
7208 return &bpf_get_route_realm_proto;
7209 case BPF_FUNC_get_hash_recalc:
7210 return &bpf_get_hash_recalc_proto;
7211 case BPF_FUNC_set_hash_invalid:
7212 return &bpf_set_hash_invalid_proto;
7213 case BPF_FUNC_set_hash:
7214 return &bpf_set_hash_proto;
7215 case BPF_FUNC_perf_event_output:
7216 return &bpf_skb_event_output_proto;
7217 case BPF_FUNC_get_smp_processor_id:
7218 return &bpf_get_smp_processor_id_proto;
7219 case BPF_FUNC_skb_under_cgroup:
7220 return &bpf_skb_under_cgroup_proto;
7221 case BPF_FUNC_get_socket_cookie:
7222 return &bpf_get_socket_cookie_proto;
7223 case BPF_FUNC_get_socket_uid:
7224 return &bpf_get_socket_uid_proto;
7225 case BPF_FUNC_fib_lookup:
7226 return &bpf_skb_fib_lookup_proto;
7227 case BPF_FUNC_sk_fullsock:
7228 return &bpf_sk_fullsock_proto;
7229 case BPF_FUNC_sk_storage_get:
7230 return &bpf_sk_storage_get_proto;
7231 case BPF_FUNC_sk_storage_delete:
7232 return &bpf_sk_storage_delete_proto;
7234 case BPF_FUNC_skb_get_xfrm_state:
7235 return &bpf_skb_get_xfrm_state_proto;
7237 #ifdef CONFIG_CGROUP_NET_CLASSID
7238 case BPF_FUNC_skb_cgroup_classid:
7239 return &bpf_skb_cgroup_classid_proto;
7241 #ifdef CONFIG_SOCK_CGROUP_DATA
7242 case BPF_FUNC_skb_cgroup_id:
7243 return &bpf_skb_cgroup_id_proto;
7244 case BPF_FUNC_skb_ancestor_cgroup_id:
7245 return &bpf_skb_ancestor_cgroup_id_proto;
7248 case BPF_FUNC_sk_lookup_tcp:
7249 return &bpf_sk_lookup_tcp_proto;
7250 case BPF_FUNC_sk_lookup_udp:
7251 return &bpf_sk_lookup_udp_proto;
7252 case BPF_FUNC_sk_release:
7253 return &bpf_sk_release_proto;
7254 case BPF_FUNC_tcp_sock:
7255 return &bpf_tcp_sock_proto;
7256 case BPF_FUNC_get_listener_sock:
7257 return &bpf_get_listener_sock_proto;
7258 case BPF_FUNC_skc_lookup_tcp:
7259 return &bpf_skc_lookup_tcp_proto;
7260 case BPF_FUNC_tcp_check_syncookie:
7261 return &bpf_tcp_check_syncookie_proto;
7262 case BPF_FUNC_skb_ecn_set_ce:
7263 return &bpf_skb_ecn_set_ce_proto;
7264 case BPF_FUNC_tcp_gen_syncookie:
7265 return &bpf_tcp_gen_syncookie_proto;
7266 case BPF_FUNC_sk_assign:
7267 return &bpf_sk_assign_proto;
7270 return bpf_sk_base_func_proto(func_id);
7274 static const struct bpf_func_proto *
7275 xdp_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
7278 case BPF_FUNC_perf_event_output:
7279 return &bpf_xdp_event_output_proto;
7280 case BPF_FUNC_get_smp_processor_id:
7281 return &bpf_get_smp_processor_id_proto;
7282 case BPF_FUNC_csum_diff:
7283 return &bpf_csum_diff_proto;
7284 case BPF_FUNC_xdp_adjust_head:
7285 return &bpf_xdp_adjust_head_proto;
7286 case BPF_FUNC_xdp_adjust_meta:
7287 return &bpf_xdp_adjust_meta_proto;
7288 case BPF_FUNC_redirect:
7289 return &bpf_xdp_redirect_proto;
7290 case BPF_FUNC_redirect_map:
7291 return &bpf_xdp_redirect_map_proto;
7292 case BPF_FUNC_xdp_adjust_tail:
7293 return &bpf_xdp_adjust_tail_proto;
7294 case BPF_FUNC_fib_lookup:
7295 return &bpf_xdp_fib_lookup_proto;
7297 case BPF_FUNC_sk_lookup_udp:
7298 return &bpf_xdp_sk_lookup_udp_proto;
7299 case BPF_FUNC_sk_lookup_tcp:
7300 return &bpf_xdp_sk_lookup_tcp_proto;
7301 case BPF_FUNC_sk_release:
7302 return &bpf_sk_release_proto;
7303 case BPF_FUNC_skc_lookup_tcp:
7304 return &bpf_xdp_skc_lookup_tcp_proto;
7305 case BPF_FUNC_tcp_check_syncookie:
7306 return &bpf_tcp_check_syncookie_proto;
7307 case BPF_FUNC_tcp_gen_syncookie:
7308 return &bpf_tcp_gen_syncookie_proto;
7311 return bpf_sk_base_func_proto(func_id);
7315 const struct bpf_func_proto bpf_sock_map_update_proto __weak;
7316 const struct bpf_func_proto bpf_sock_hash_update_proto __weak;
7318 static const struct bpf_func_proto *
7319 sock_ops_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
7322 case BPF_FUNC_setsockopt:
7323 return &bpf_sock_ops_setsockopt_proto;
7324 case BPF_FUNC_getsockopt:
7325 return &bpf_sock_ops_getsockopt_proto;
7326 case BPF_FUNC_sock_ops_cb_flags_set:
7327 return &bpf_sock_ops_cb_flags_set_proto;
7328 case BPF_FUNC_sock_map_update:
7329 return &bpf_sock_map_update_proto;
7330 case BPF_FUNC_sock_hash_update:
7331 return &bpf_sock_hash_update_proto;
7332 case BPF_FUNC_get_socket_cookie:
7333 return &bpf_get_socket_cookie_sock_ops_proto;
7334 case BPF_FUNC_get_local_storage:
7335 return &bpf_get_local_storage_proto;
7336 case BPF_FUNC_perf_event_output:
7337 return &bpf_event_output_data_proto;
7338 case BPF_FUNC_sk_storage_get:
7339 return &bpf_sk_storage_get_proto;
7340 case BPF_FUNC_sk_storage_delete:
7341 return &bpf_sk_storage_delete_proto;
7343 case BPF_FUNC_load_hdr_opt:
7344 return &bpf_sock_ops_load_hdr_opt_proto;
7345 case BPF_FUNC_store_hdr_opt:
7346 return &bpf_sock_ops_store_hdr_opt_proto;
7347 case BPF_FUNC_reserve_hdr_opt:
7348 return &bpf_sock_ops_reserve_hdr_opt_proto;
7349 case BPF_FUNC_tcp_sock:
7350 return &bpf_tcp_sock_proto;
7351 #endif /* CONFIG_INET */
7353 return bpf_sk_base_func_proto(func_id);
7357 const struct bpf_func_proto bpf_msg_redirect_map_proto __weak;
7358 const struct bpf_func_proto bpf_msg_redirect_hash_proto __weak;
7360 static const struct bpf_func_proto *
7361 sk_msg_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
7364 case BPF_FUNC_msg_redirect_map:
7365 return &bpf_msg_redirect_map_proto;
7366 case BPF_FUNC_msg_redirect_hash:
7367 return &bpf_msg_redirect_hash_proto;
7368 case BPF_FUNC_msg_apply_bytes:
7369 return &bpf_msg_apply_bytes_proto;
7370 case BPF_FUNC_msg_cork_bytes:
7371 return &bpf_msg_cork_bytes_proto;
7372 case BPF_FUNC_msg_pull_data:
7373 return &bpf_msg_pull_data_proto;
7374 case BPF_FUNC_msg_push_data:
7375 return &bpf_msg_push_data_proto;
7376 case BPF_FUNC_msg_pop_data:
7377 return &bpf_msg_pop_data_proto;
7378 case BPF_FUNC_perf_event_output:
7379 return &bpf_event_output_data_proto;
7380 case BPF_FUNC_get_current_uid_gid:
7381 return &bpf_get_current_uid_gid_proto;
7382 case BPF_FUNC_get_current_pid_tgid:
7383 return &bpf_get_current_pid_tgid_proto;
7384 case BPF_FUNC_sk_storage_get:
7385 return &bpf_sk_storage_get_proto;
7386 case BPF_FUNC_sk_storage_delete:
7387 return &bpf_sk_storage_delete_proto;
7388 #ifdef CONFIG_CGROUPS
7389 case BPF_FUNC_get_current_cgroup_id:
7390 return &bpf_get_current_cgroup_id_proto;
7391 case BPF_FUNC_get_current_ancestor_cgroup_id:
7392 return &bpf_get_current_ancestor_cgroup_id_proto;
7394 #ifdef CONFIG_CGROUP_NET_CLASSID
7395 case BPF_FUNC_get_cgroup_classid:
7396 return &bpf_get_cgroup_classid_curr_proto;
7399 return bpf_sk_base_func_proto(func_id);
7403 const struct bpf_func_proto bpf_sk_redirect_map_proto __weak;
7404 const struct bpf_func_proto bpf_sk_redirect_hash_proto __weak;
7406 static const struct bpf_func_proto *
7407 sk_skb_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
7410 case BPF_FUNC_skb_store_bytes:
7411 return &bpf_skb_store_bytes_proto;
7412 case BPF_FUNC_skb_load_bytes:
7413 return &bpf_skb_load_bytes_proto;
7414 case BPF_FUNC_skb_pull_data:
7415 return &sk_skb_pull_data_proto;
7416 case BPF_FUNC_skb_change_tail:
7417 return &sk_skb_change_tail_proto;
7418 case BPF_FUNC_skb_change_head:
7419 return &sk_skb_change_head_proto;
7420 case BPF_FUNC_skb_adjust_room:
7421 return &sk_skb_adjust_room_proto;
7422 case BPF_FUNC_get_socket_cookie:
7423 return &bpf_get_socket_cookie_proto;
7424 case BPF_FUNC_get_socket_uid:
7425 return &bpf_get_socket_uid_proto;
7426 case BPF_FUNC_sk_redirect_map:
7427 return &bpf_sk_redirect_map_proto;
7428 case BPF_FUNC_sk_redirect_hash:
7429 return &bpf_sk_redirect_hash_proto;
7430 case BPF_FUNC_perf_event_output:
7431 return &bpf_skb_event_output_proto;
7433 case BPF_FUNC_sk_lookup_tcp:
7434 return &bpf_sk_lookup_tcp_proto;
7435 case BPF_FUNC_sk_lookup_udp:
7436 return &bpf_sk_lookup_udp_proto;
7437 case BPF_FUNC_sk_release:
7438 return &bpf_sk_release_proto;
7439 case BPF_FUNC_skc_lookup_tcp:
7440 return &bpf_skc_lookup_tcp_proto;
7443 return bpf_sk_base_func_proto(func_id);
7447 static const struct bpf_func_proto *
7448 flow_dissector_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
7451 case BPF_FUNC_skb_load_bytes:
7452 return &bpf_flow_dissector_load_bytes_proto;
7454 return bpf_sk_base_func_proto(func_id);
7458 static const struct bpf_func_proto *
7459 lwt_out_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
7462 case BPF_FUNC_skb_load_bytes:
7463 return &bpf_skb_load_bytes_proto;
7464 case BPF_FUNC_skb_pull_data:
7465 return &bpf_skb_pull_data_proto;
7466 case BPF_FUNC_csum_diff:
7467 return &bpf_csum_diff_proto;
7468 case BPF_FUNC_get_cgroup_classid:
7469 return &bpf_get_cgroup_classid_proto;
7470 case BPF_FUNC_get_route_realm:
7471 return &bpf_get_route_realm_proto;
7472 case BPF_FUNC_get_hash_recalc:
7473 return &bpf_get_hash_recalc_proto;
7474 case BPF_FUNC_perf_event_output:
7475 return &bpf_skb_event_output_proto;
7476 case BPF_FUNC_get_smp_processor_id:
7477 return &bpf_get_smp_processor_id_proto;
7478 case BPF_FUNC_skb_under_cgroup:
7479 return &bpf_skb_under_cgroup_proto;
7481 return bpf_sk_base_func_proto(func_id);
7485 static const struct bpf_func_proto *
7486 lwt_in_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
7489 case BPF_FUNC_lwt_push_encap:
7490 return &bpf_lwt_in_push_encap_proto;
7492 return lwt_out_func_proto(func_id, prog);
7496 static const struct bpf_func_proto *
7497 lwt_xmit_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
7500 case BPF_FUNC_skb_get_tunnel_key:
7501 return &bpf_skb_get_tunnel_key_proto;
7502 case BPF_FUNC_skb_set_tunnel_key:
7503 return bpf_get_skb_set_tunnel_proto(func_id);
7504 case BPF_FUNC_skb_get_tunnel_opt:
7505 return &bpf_skb_get_tunnel_opt_proto;
7506 case BPF_FUNC_skb_set_tunnel_opt:
7507 return bpf_get_skb_set_tunnel_proto(func_id);
7508 case BPF_FUNC_redirect:
7509 return &bpf_redirect_proto;
7510 case BPF_FUNC_clone_redirect:
7511 return &bpf_clone_redirect_proto;
7512 case BPF_FUNC_skb_change_tail:
7513 return &bpf_skb_change_tail_proto;
7514 case BPF_FUNC_skb_change_head:
7515 return &bpf_skb_change_head_proto;
7516 case BPF_FUNC_skb_store_bytes:
7517 return &bpf_skb_store_bytes_proto;
7518 case BPF_FUNC_csum_update:
7519 return &bpf_csum_update_proto;
7520 case BPF_FUNC_csum_level:
7521 return &bpf_csum_level_proto;
7522 case BPF_FUNC_l3_csum_replace:
7523 return &bpf_l3_csum_replace_proto;
7524 case BPF_FUNC_l4_csum_replace:
7525 return &bpf_l4_csum_replace_proto;
7526 case BPF_FUNC_set_hash_invalid:
7527 return &bpf_set_hash_invalid_proto;
7528 case BPF_FUNC_lwt_push_encap:
7529 return &bpf_lwt_xmit_push_encap_proto;
7531 return lwt_out_func_proto(func_id, prog);
7535 static const struct bpf_func_proto *
7536 lwt_seg6local_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
7539 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
7540 case BPF_FUNC_lwt_seg6_store_bytes:
7541 return &bpf_lwt_seg6_store_bytes_proto;
7542 case BPF_FUNC_lwt_seg6_action:
7543 return &bpf_lwt_seg6_action_proto;
7544 case BPF_FUNC_lwt_seg6_adjust_srh:
7545 return &bpf_lwt_seg6_adjust_srh_proto;
7548 return lwt_out_func_proto(func_id, prog);
7552 static bool bpf_skb_is_valid_access(int off, int size, enum bpf_access_type type,
7553 const struct bpf_prog *prog,
7554 struct bpf_insn_access_aux *info)
7556 const int size_default = sizeof(__u32);
7558 if (off < 0 || off >= sizeof(struct __sk_buff))
7561 /* The verifier guarantees that size > 0. */
7562 if (off % size != 0)
7566 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
7567 if (off + size > offsetofend(struct __sk_buff, cb[4]))
7570 case bpf_ctx_range_till(struct __sk_buff, remote_ip6[0], remote_ip6[3]):
7571 case bpf_ctx_range_till(struct __sk_buff, local_ip6[0], local_ip6[3]):
7572 case bpf_ctx_range_till(struct __sk_buff, remote_ip4, remote_ip4):
7573 case bpf_ctx_range_till(struct __sk_buff, local_ip4, local_ip4):
7574 case bpf_ctx_range(struct __sk_buff, data):
7575 case bpf_ctx_range(struct __sk_buff, data_meta):
7576 case bpf_ctx_range(struct __sk_buff, data_end):
7577 if (size != size_default)
7580 case bpf_ctx_range_ptr(struct __sk_buff, flow_keys):
7582 case bpf_ctx_range(struct __sk_buff, tstamp):
7583 if (size != sizeof(__u64))
7586 case offsetof(struct __sk_buff, sk):
7587 if (type == BPF_WRITE || size != sizeof(__u64))
7589 info->reg_type = PTR_TO_SOCK_COMMON_OR_NULL;
7592 /* Only narrow read access allowed for now. */
7593 if (type == BPF_WRITE) {
7594 if (size != size_default)
7597 bpf_ctx_record_field_size(info, size_default);
7598 if (!bpf_ctx_narrow_access_ok(off, size, size_default))
7606 static bool sk_filter_is_valid_access(int off, int size,
7607 enum bpf_access_type type,
7608 const struct bpf_prog *prog,
7609 struct bpf_insn_access_aux *info)
7612 case bpf_ctx_range(struct __sk_buff, tc_classid):
7613 case bpf_ctx_range(struct __sk_buff, data):
7614 case bpf_ctx_range(struct __sk_buff, data_meta):
7615 case bpf_ctx_range(struct __sk_buff, data_end):
7616 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
7617 case bpf_ctx_range(struct __sk_buff, tstamp):
7618 case bpf_ctx_range(struct __sk_buff, wire_len):
7622 if (type == BPF_WRITE) {
7624 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
7631 return bpf_skb_is_valid_access(off, size, type, prog, info);
7634 static bool cg_skb_is_valid_access(int off, int size,
7635 enum bpf_access_type type,
7636 const struct bpf_prog *prog,
7637 struct bpf_insn_access_aux *info)
7640 case bpf_ctx_range(struct __sk_buff, tc_classid):
7641 case bpf_ctx_range(struct __sk_buff, data_meta):
7642 case bpf_ctx_range(struct __sk_buff, wire_len):
7644 case bpf_ctx_range(struct __sk_buff, data):
7645 case bpf_ctx_range(struct __sk_buff, data_end):
7651 if (type == BPF_WRITE) {
7653 case bpf_ctx_range(struct __sk_buff, mark):
7654 case bpf_ctx_range(struct __sk_buff, priority):
7655 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
7657 case bpf_ctx_range(struct __sk_buff, tstamp):
7667 case bpf_ctx_range(struct __sk_buff, data):
7668 info->reg_type = PTR_TO_PACKET;
7670 case bpf_ctx_range(struct __sk_buff, data_end):
7671 info->reg_type = PTR_TO_PACKET_END;
7675 return bpf_skb_is_valid_access(off, size, type, prog, info);
7678 static bool lwt_is_valid_access(int off, int size,
7679 enum bpf_access_type type,
7680 const struct bpf_prog *prog,
7681 struct bpf_insn_access_aux *info)
7684 case bpf_ctx_range(struct __sk_buff, tc_classid):
7685 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
7686 case bpf_ctx_range(struct __sk_buff, data_meta):
7687 case bpf_ctx_range(struct __sk_buff, tstamp):
7688 case bpf_ctx_range(struct __sk_buff, wire_len):
7692 if (type == BPF_WRITE) {
7694 case bpf_ctx_range(struct __sk_buff, mark):
7695 case bpf_ctx_range(struct __sk_buff, priority):
7696 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
7704 case bpf_ctx_range(struct __sk_buff, data):
7705 info->reg_type = PTR_TO_PACKET;
7707 case bpf_ctx_range(struct __sk_buff, data_end):
7708 info->reg_type = PTR_TO_PACKET_END;
7712 return bpf_skb_is_valid_access(off, size, type, prog, info);
7715 /* Attach type specific accesses */
7716 static bool __sock_filter_check_attach_type(int off,
7717 enum bpf_access_type access_type,
7718 enum bpf_attach_type attach_type)
7721 case offsetof(struct bpf_sock, bound_dev_if):
7722 case offsetof(struct bpf_sock, mark):
7723 case offsetof(struct bpf_sock, priority):
7724 switch (attach_type) {
7725 case BPF_CGROUP_INET_SOCK_CREATE:
7726 case BPF_CGROUP_INET_SOCK_RELEASE:
7731 case bpf_ctx_range(struct bpf_sock, src_ip4):
7732 switch (attach_type) {
7733 case BPF_CGROUP_INET4_POST_BIND:
7738 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
7739 switch (attach_type) {
7740 case BPF_CGROUP_INET6_POST_BIND:
7745 case bpf_ctx_range(struct bpf_sock, src_port):
7746 switch (attach_type) {
7747 case BPF_CGROUP_INET4_POST_BIND:
7748 case BPF_CGROUP_INET6_POST_BIND:
7755 return access_type == BPF_READ;
7760 bool bpf_sock_common_is_valid_access(int off, int size,
7761 enum bpf_access_type type,
7762 struct bpf_insn_access_aux *info)
7765 case bpf_ctx_range_till(struct bpf_sock, type, priority):
7768 return bpf_sock_is_valid_access(off, size, type, info);
7772 bool bpf_sock_is_valid_access(int off, int size, enum bpf_access_type type,
7773 struct bpf_insn_access_aux *info)
7775 const int size_default = sizeof(__u32);
7778 if (off < 0 || off >= sizeof(struct bpf_sock))
7780 if (off % size != 0)
7784 case offsetof(struct bpf_sock, state):
7785 case offsetof(struct bpf_sock, family):
7786 case offsetof(struct bpf_sock, type):
7787 case offsetof(struct bpf_sock, protocol):
7788 case offsetof(struct bpf_sock, src_port):
7789 case offsetof(struct bpf_sock, rx_queue_mapping):
7790 case bpf_ctx_range(struct bpf_sock, src_ip4):
7791 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
7792 case bpf_ctx_range(struct bpf_sock, dst_ip4):
7793 case bpf_ctx_range_till(struct bpf_sock, dst_ip6[0], dst_ip6[3]):
7794 bpf_ctx_record_field_size(info, size_default);
7795 return bpf_ctx_narrow_access_ok(off, size, size_default);
7796 case bpf_ctx_range(struct bpf_sock, dst_port):
7797 field_size = size == size_default ?
7798 size_default : sizeof_field(struct bpf_sock, dst_port);
7799 bpf_ctx_record_field_size(info, field_size);
7800 return bpf_ctx_narrow_access_ok(off, size, field_size);
7801 case offsetofend(struct bpf_sock, dst_port) ...
7802 offsetof(struct bpf_sock, dst_ip4) - 1:
7806 return size == size_default;
7809 static bool sock_filter_is_valid_access(int off, int size,
7810 enum bpf_access_type type,
7811 const struct bpf_prog *prog,
7812 struct bpf_insn_access_aux *info)
7814 if (!bpf_sock_is_valid_access(off, size, type, info))
7816 return __sock_filter_check_attach_type(off, type,
7817 prog->expected_attach_type);
7820 static int bpf_noop_prologue(struct bpf_insn *insn_buf, bool direct_write,
7821 const struct bpf_prog *prog)
7823 /* Neither direct read nor direct write requires any preliminary
7829 static int bpf_unclone_prologue(struct bpf_insn *insn_buf, bool direct_write,
7830 const struct bpf_prog *prog, int drop_verdict)
7832 struct bpf_insn *insn = insn_buf;
7837 /* if (!skb->cloned)
7840 * (Fast-path, otherwise approximation that we might be
7841 * a clone, do the rest in helper.)
7843 *insn++ = BPF_LDX_MEM(BPF_B, BPF_REG_6, BPF_REG_1, CLONED_OFFSET());
7844 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_6, CLONED_MASK);
7845 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_6, 0, 7);
7847 /* ret = bpf_skb_pull_data(skb, 0); */
7848 *insn++ = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1);
7849 *insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_2, BPF_REG_2);
7850 *insn++ = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
7851 BPF_FUNC_skb_pull_data);
7854 * return TC_ACT_SHOT;
7856 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2);
7857 *insn++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_0, drop_verdict);
7858 *insn++ = BPF_EXIT_INSN();
7861 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_6);
7863 *insn++ = prog->insnsi[0];
7865 return insn - insn_buf;
7868 static int bpf_gen_ld_abs(const struct bpf_insn *orig,
7869 struct bpf_insn *insn_buf)
7871 bool indirect = BPF_MODE(orig->code) == BPF_IND;
7872 struct bpf_insn *insn = insn_buf;
7875 *insn++ = BPF_MOV64_IMM(BPF_REG_2, orig->imm);
7877 *insn++ = BPF_MOV64_REG(BPF_REG_2, orig->src_reg);
7879 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, orig->imm);
7881 /* We're guaranteed here that CTX is in R6. */
7882 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_CTX);
7884 switch (BPF_SIZE(orig->code)) {
7886 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_8_no_cache);
7889 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_16_no_cache);
7892 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_32_no_cache);
7896 *insn++ = BPF_JMP_IMM(BPF_JSGE, BPF_REG_0, 0, 2);
7897 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_0, BPF_REG_0);
7898 *insn++ = BPF_EXIT_INSN();
7900 return insn - insn_buf;
7903 static int tc_cls_act_prologue(struct bpf_insn *insn_buf, bool direct_write,
7904 const struct bpf_prog *prog)
7906 return bpf_unclone_prologue(insn_buf, direct_write, prog, TC_ACT_SHOT);
7909 static bool tc_cls_act_is_valid_access(int off, int size,
7910 enum bpf_access_type type,
7911 const struct bpf_prog *prog,
7912 struct bpf_insn_access_aux *info)
7914 if (type == BPF_WRITE) {
7916 case bpf_ctx_range(struct __sk_buff, mark):
7917 case bpf_ctx_range(struct __sk_buff, tc_index):
7918 case bpf_ctx_range(struct __sk_buff, priority):
7919 case bpf_ctx_range(struct __sk_buff, tc_classid):
7920 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
7921 case bpf_ctx_range(struct __sk_buff, tstamp):
7922 case bpf_ctx_range(struct __sk_buff, queue_mapping):
7930 case bpf_ctx_range(struct __sk_buff, data):
7931 info->reg_type = PTR_TO_PACKET;
7933 case bpf_ctx_range(struct __sk_buff, data_meta):
7934 info->reg_type = PTR_TO_PACKET_META;
7936 case bpf_ctx_range(struct __sk_buff, data_end):
7937 info->reg_type = PTR_TO_PACKET_END;
7939 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
7943 return bpf_skb_is_valid_access(off, size, type, prog, info);
7946 static bool __is_valid_xdp_access(int off, int size)
7948 if (off < 0 || off >= sizeof(struct xdp_md))
7950 if (off % size != 0)
7952 if (size != sizeof(__u32))
7958 static bool xdp_is_valid_access(int off, int size,
7959 enum bpf_access_type type,
7960 const struct bpf_prog *prog,
7961 struct bpf_insn_access_aux *info)
7963 if (prog->expected_attach_type != BPF_XDP_DEVMAP) {
7965 case offsetof(struct xdp_md, egress_ifindex):
7970 if (type == BPF_WRITE) {
7971 if (bpf_prog_is_dev_bound(prog->aux)) {
7973 case offsetof(struct xdp_md, rx_queue_index):
7974 return __is_valid_xdp_access(off, size);
7981 case offsetof(struct xdp_md, data):
7982 info->reg_type = PTR_TO_PACKET;
7984 case offsetof(struct xdp_md, data_meta):
7985 info->reg_type = PTR_TO_PACKET_META;
7987 case offsetof(struct xdp_md, data_end):
7988 info->reg_type = PTR_TO_PACKET_END;
7992 return __is_valid_xdp_access(off, size);
7995 void bpf_warn_invalid_xdp_action(u32 act)
7997 const u32 act_max = XDP_REDIRECT;
7999 pr_warn_once("%s XDP return value %u, expect packet loss!\n",
8000 act > act_max ? "Illegal" : "Driver unsupported",
8003 EXPORT_SYMBOL_GPL(bpf_warn_invalid_xdp_action);
8005 static bool sock_addr_is_valid_access(int off, int size,
8006 enum bpf_access_type type,
8007 const struct bpf_prog *prog,
8008 struct bpf_insn_access_aux *info)
8010 const int size_default = sizeof(__u32);
8012 if (off < 0 || off >= sizeof(struct bpf_sock_addr))
8014 if (off % size != 0)
8017 /* Disallow access to IPv6 fields from IPv4 contex and vise
8021 case bpf_ctx_range(struct bpf_sock_addr, user_ip4):
8022 switch (prog->expected_attach_type) {
8023 case BPF_CGROUP_INET4_BIND:
8024 case BPF_CGROUP_INET4_CONNECT:
8025 case BPF_CGROUP_INET4_GETPEERNAME:
8026 case BPF_CGROUP_INET4_GETSOCKNAME:
8027 case BPF_CGROUP_UDP4_SENDMSG:
8028 case BPF_CGROUP_UDP4_RECVMSG:
8034 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
8035 switch (prog->expected_attach_type) {
8036 case BPF_CGROUP_INET6_BIND:
8037 case BPF_CGROUP_INET6_CONNECT:
8038 case BPF_CGROUP_INET6_GETPEERNAME:
8039 case BPF_CGROUP_INET6_GETSOCKNAME:
8040 case BPF_CGROUP_UDP6_SENDMSG:
8041 case BPF_CGROUP_UDP6_RECVMSG:
8047 case bpf_ctx_range(struct bpf_sock_addr, msg_src_ip4):
8048 switch (prog->expected_attach_type) {
8049 case BPF_CGROUP_UDP4_SENDMSG:
8055 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
8057 switch (prog->expected_attach_type) {
8058 case BPF_CGROUP_UDP6_SENDMSG:
8067 case bpf_ctx_range(struct bpf_sock_addr, user_ip4):
8068 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
8069 case bpf_ctx_range(struct bpf_sock_addr, msg_src_ip4):
8070 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
8072 case bpf_ctx_range(struct bpf_sock_addr, user_port):
8073 if (type == BPF_READ) {
8074 bpf_ctx_record_field_size(info, size_default);
8076 if (bpf_ctx_wide_access_ok(off, size,
8077 struct bpf_sock_addr,
8081 if (bpf_ctx_wide_access_ok(off, size,
8082 struct bpf_sock_addr,
8086 if (!bpf_ctx_narrow_access_ok(off, size, size_default))
8089 if (bpf_ctx_wide_access_ok(off, size,
8090 struct bpf_sock_addr,
8094 if (bpf_ctx_wide_access_ok(off, size,
8095 struct bpf_sock_addr,
8099 if (size != size_default)
8103 case offsetof(struct bpf_sock_addr, sk):
8104 if (type != BPF_READ)
8106 if (size != sizeof(__u64))
8108 info->reg_type = PTR_TO_SOCKET;
8111 if (type == BPF_READ) {
8112 if (size != size_default)
8122 static bool sock_ops_is_valid_access(int off, int size,
8123 enum bpf_access_type type,
8124 const struct bpf_prog *prog,
8125 struct bpf_insn_access_aux *info)
8127 const int size_default = sizeof(__u32);
8129 if (off < 0 || off >= sizeof(struct bpf_sock_ops))
8132 /* The verifier guarantees that size > 0. */
8133 if (off % size != 0)
8136 if (type == BPF_WRITE) {
8138 case offsetof(struct bpf_sock_ops, reply):
8139 case offsetof(struct bpf_sock_ops, sk_txhash):
8140 if (size != size_default)
8148 case bpf_ctx_range_till(struct bpf_sock_ops, bytes_received,
8150 if (size != sizeof(__u64))
8153 case offsetof(struct bpf_sock_ops, sk):
8154 if (size != sizeof(__u64))
8156 info->reg_type = PTR_TO_SOCKET_OR_NULL;
8158 case offsetof(struct bpf_sock_ops, skb_data):
8159 if (size != sizeof(__u64))
8161 info->reg_type = PTR_TO_PACKET;
8163 case offsetof(struct bpf_sock_ops, skb_data_end):
8164 if (size != sizeof(__u64))
8166 info->reg_type = PTR_TO_PACKET_END;
8168 case offsetof(struct bpf_sock_ops, skb_tcp_flags):
8169 bpf_ctx_record_field_size(info, size_default);
8170 return bpf_ctx_narrow_access_ok(off, size,
8173 if (size != size_default)
8182 static int sk_skb_prologue(struct bpf_insn *insn_buf, bool direct_write,
8183 const struct bpf_prog *prog)
8185 return bpf_unclone_prologue(insn_buf, direct_write, prog, SK_DROP);
8188 static bool sk_skb_is_valid_access(int off, int size,
8189 enum bpf_access_type type,
8190 const struct bpf_prog *prog,
8191 struct bpf_insn_access_aux *info)
8194 case bpf_ctx_range(struct __sk_buff, tc_classid):
8195 case bpf_ctx_range(struct __sk_buff, data_meta):
8196 case bpf_ctx_range(struct __sk_buff, tstamp):
8197 case bpf_ctx_range(struct __sk_buff, wire_len):
8201 if (type == BPF_WRITE) {
8203 case bpf_ctx_range(struct __sk_buff, tc_index):
8204 case bpf_ctx_range(struct __sk_buff, priority):
8212 case bpf_ctx_range(struct __sk_buff, mark):
8214 case bpf_ctx_range(struct __sk_buff, data):
8215 info->reg_type = PTR_TO_PACKET;
8217 case bpf_ctx_range(struct __sk_buff, data_end):
8218 info->reg_type = PTR_TO_PACKET_END;
8222 return bpf_skb_is_valid_access(off, size, type, prog, info);
8225 static bool sk_msg_is_valid_access(int off, int size,
8226 enum bpf_access_type type,
8227 const struct bpf_prog *prog,
8228 struct bpf_insn_access_aux *info)
8230 if (type == BPF_WRITE)
8233 if (off % size != 0)
8237 case offsetof(struct sk_msg_md, data):
8238 info->reg_type = PTR_TO_PACKET;
8239 if (size != sizeof(__u64))
8242 case offsetof(struct sk_msg_md, data_end):
8243 info->reg_type = PTR_TO_PACKET_END;
8244 if (size != sizeof(__u64))
8247 case offsetof(struct sk_msg_md, sk):
8248 if (size != sizeof(__u64))
8250 info->reg_type = PTR_TO_SOCKET;
8252 case bpf_ctx_range(struct sk_msg_md, family):
8253 case bpf_ctx_range(struct sk_msg_md, remote_ip4):
8254 case bpf_ctx_range(struct sk_msg_md, local_ip4):
8255 case bpf_ctx_range_till(struct sk_msg_md, remote_ip6[0], remote_ip6[3]):
8256 case bpf_ctx_range_till(struct sk_msg_md, local_ip6[0], local_ip6[3]):
8257 case bpf_ctx_range(struct sk_msg_md, remote_port):
8258 case bpf_ctx_range(struct sk_msg_md, local_port):
8259 case bpf_ctx_range(struct sk_msg_md, size):
8260 if (size != sizeof(__u32))
8269 static bool flow_dissector_is_valid_access(int off, int size,
8270 enum bpf_access_type type,
8271 const struct bpf_prog *prog,
8272 struct bpf_insn_access_aux *info)
8274 const int size_default = sizeof(__u32);
8276 if (off < 0 || off >= sizeof(struct __sk_buff))
8279 if (type == BPF_WRITE)
8283 case bpf_ctx_range(struct __sk_buff, data):
8284 if (size != size_default)
8286 info->reg_type = PTR_TO_PACKET;
8288 case bpf_ctx_range(struct __sk_buff, data_end):
8289 if (size != size_default)
8291 info->reg_type = PTR_TO_PACKET_END;
8293 case bpf_ctx_range_ptr(struct __sk_buff, flow_keys):
8294 if (size != sizeof(__u64))
8296 info->reg_type = PTR_TO_FLOW_KEYS;
8303 static u32 flow_dissector_convert_ctx_access(enum bpf_access_type type,
8304 const struct bpf_insn *si,
8305 struct bpf_insn *insn_buf,
8306 struct bpf_prog *prog,
8310 struct bpf_insn *insn = insn_buf;
8313 case offsetof(struct __sk_buff, data):
8314 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_flow_dissector, data),
8315 si->dst_reg, si->src_reg,
8316 offsetof(struct bpf_flow_dissector, data));
8319 case offsetof(struct __sk_buff, data_end):
8320 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_flow_dissector, data_end),
8321 si->dst_reg, si->src_reg,
8322 offsetof(struct bpf_flow_dissector, data_end));
8325 case offsetof(struct __sk_buff, flow_keys):
8326 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_flow_dissector, flow_keys),
8327 si->dst_reg, si->src_reg,
8328 offsetof(struct bpf_flow_dissector, flow_keys));
8332 return insn - insn_buf;
8335 static struct bpf_insn *bpf_convert_shinfo_access(const struct bpf_insn *si,
8336 struct bpf_insn *insn)
8338 /* si->dst_reg = skb_shinfo(SKB); */
8339 #ifdef NET_SKBUFF_DATA_USES_OFFSET
8340 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, end),
8341 BPF_REG_AX, si->src_reg,
8342 offsetof(struct sk_buff, end));
8343 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, head),
8344 si->dst_reg, si->src_reg,
8345 offsetof(struct sk_buff, head));
8346 *insn++ = BPF_ALU64_REG(BPF_ADD, si->dst_reg, BPF_REG_AX);
8348 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, end),
8349 si->dst_reg, si->src_reg,
8350 offsetof(struct sk_buff, end));
8356 static u32 bpf_convert_ctx_access(enum bpf_access_type type,
8357 const struct bpf_insn *si,
8358 struct bpf_insn *insn_buf,
8359 struct bpf_prog *prog, u32 *target_size)
8361 struct bpf_insn *insn = insn_buf;
8365 case offsetof(struct __sk_buff, len):
8366 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
8367 bpf_target_off(struct sk_buff, len, 4,
8371 case offsetof(struct __sk_buff, protocol):
8372 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
8373 bpf_target_off(struct sk_buff, protocol, 2,
8377 case offsetof(struct __sk_buff, vlan_proto):
8378 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
8379 bpf_target_off(struct sk_buff, vlan_proto, 2,
8383 case offsetof(struct __sk_buff, priority):
8384 if (type == BPF_WRITE)
8385 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
8386 bpf_target_off(struct sk_buff, priority, 4,
8389 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
8390 bpf_target_off(struct sk_buff, priority, 4,
8394 case offsetof(struct __sk_buff, ingress_ifindex):
8395 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
8396 bpf_target_off(struct sk_buff, skb_iif, 4,
8400 case offsetof(struct __sk_buff, ifindex):
8401 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
8402 si->dst_reg, si->src_reg,
8403 offsetof(struct sk_buff, dev));
8404 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
8405 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
8406 bpf_target_off(struct net_device, ifindex, 4,
8410 case offsetof(struct __sk_buff, hash):
8411 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
8412 bpf_target_off(struct sk_buff, hash, 4,
8416 case offsetof(struct __sk_buff, mark):
8417 if (type == BPF_WRITE)
8418 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
8419 bpf_target_off(struct sk_buff, mark, 4,
8422 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
8423 bpf_target_off(struct sk_buff, mark, 4,
8427 case offsetof(struct __sk_buff, pkt_type):
8429 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->src_reg,
8431 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, PKT_TYPE_MAX);
8432 #ifdef __BIG_ENDIAN_BITFIELD
8433 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, 5);
8437 case offsetof(struct __sk_buff, queue_mapping):
8438 if (type == BPF_WRITE) {
8439 *insn++ = BPF_JMP_IMM(BPF_JGE, si->src_reg, NO_QUEUE_MAPPING, 1);
8440 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg, si->src_reg,
8441 bpf_target_off(struct sk_buff,
8445 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
8446 bpf_target_off(struct sk_buff,
8452 case offsetof(struct __sk_buff, vlan_present):
8454 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->src_reg,
8455 PKT_VLAN_PRESENT_OFFSET());
8456 if (PKT_VLAN_PRESENT_BIT)
8457 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, PKT_VLAN_PRESENT_BIT);
8458 if (PKT_VLAN_PRESENT_BIT < 7)
8459 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, 1);
8462 case offsetof(struct __sk_buff, vlan_tci):
8463 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
8464 bpf_target_off(struct sk_buff, vlan_tci, 2,
8468 case offsetof(struct __sk_buff, cb[0]) ...
8469 offsetofend(struct __sk_buff, cb[4]) - 1:
8470 BUILD_BUG_ON(sizeof_field(struct qdisc_skb_cb, data) < 20);
8471 BUILD_BUG_ON((offsetof(struct sk_buff, cb) +
8472 offsetof(struct qdisc_skb_cb, data)) %
8475 prog->cb_access = 1;
8477 off -= offsetof(struct __sk_buff, cb[0]);
8478 off += offsetof(struct sk_buff, cb);
8479 off += offsetof(struct qdisc_skb_cb, data);
8480 if (type == BPF_WRITE)
8481 *insn++ = BPF_STX_MEM(BPF_SIZE(si->code), si->dst_reg,
8484 *insn++ = BPF_LDX_MEM(BPF_SIZE(si->code), si->dst_reg,
8488 case offsetof(struct __sk_buff, tc_classid):
8489 BUILD_BUG_ON(sizeof_field(struct qdisc_skb_cb, tc_classid) != 2);
8492 off -= offsetof(struct __sk_buff, tc_classid);
8493 off += offsetof(struct sk_buff, cb);
8494 off += offsetof(struct qdisc_skb_cb, tc_classid);
8496 if (type == BPF_WRITE)
8497 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg,
8500 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg,
8504 case offsetof(struct __sk_buff, data):
8505 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
8506 si->dst_reg, si->src_reg,
8507 offsetof(struct sk_buff, data));
8510 case offsetof(struct __sk_buff, data_meta):
8512 off -= offsetof(struct __sk_buff, data_meta);
8513 off += offsetof(struct sk_buff, cb);
8514 off += offsetof(struct bpf_skb_data_end, data_meta);
8515 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
8519 case offsetof(struct __sk_buff, data_end):
8521 off -= offsetof(struct __sk_buff, data_end);
8522 off += offsetof(struct sk_buff, cb);
8523 off += offsetof(struct bpf_skb_data_end, data_end);
8524 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
8528 case offsetof(struct __sk_buff, tc_index):
8529 #ifdef CONFIG_NET_SCHED
8530 if (type == BPF_WRITE)
8531 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg, si->src_reg,
8532 bpf_target_off(struct sk_buff, tc_index, 2,
8535 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
8536 bpf_target_off(struct sk_buff, tc_index, 2,
8540 if (type == BPF_WRITE)
8541 *insn++ = BPF_MOV64_REG(si->dst_reg, si->dst_reg);
8543 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
8547 case offsetof(struct __sk_buff, napi_id):
8548 #if defined(CONFIG_NET_RX_BUSY_POLL)
8549 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
8550 bpf_target_off(struct sk_buff, napi_id, 4,
8552 *insn++ = BPF_JMP_IMM(BPF_JGE, si->dst_reg, MIN_NAPI_ID, 1);
8553 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
8556 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
8559 case offsetof(struct __sk_buff, family):
8560 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_family) != 2);
8562 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
8563 si->dst_reg, si->src_reg,
8564 offsetof(struct sk_buff, sk));
8565 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
8566 bpf_target_off(struct sock_common,
8570 case offsetof(struct __sk_buff, remote_ip4):
8571 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_daddr) != 4);
8573 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
8574 si->dst_reg, si->src_reg,
8575 offsetof(struct sk_buff, sk));
8576 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
8577 bpf_target_off(struct sock_common,
8581 case offsetof(struct __sk_buff, local_ip4):
8582 BUILD_BUG_ON(sizeof_field(struct sock_common,
8583 skc_rcv_saddr) != 4);
8585 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
8586 si->dst_reg, si->src_reg,
8587 offsetof(struct sk_buff, sk));
8588 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
8589 bpf_target_off(struct sock_common,
8593 case offsetof(struct __sk_buff, remote_ip6[0]) ...
8594 offsetof(struct __sk_buff, remote_ip6[3]):
8595 #if IS_ENABLED(CONFIG_IPV6)
8596 BUILD_BUG_ON(sizeof_field(struct sock_common,
8597 skc_v6_daddr.s6_addr32[0]) != 4);
8600 off -= offsetof(struct __sk_buff, remote_ip6[0]);
8602 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
8603 si->dst_reg, si->src_reg,
8604 offsetof(struct sk_buff, sk));
8605 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
8606 offsetof(struct sock_common,
8607 skc_v6_daddr.s6_addr32[0]) +
8610 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
8613 case offsetof(struct __sk_buff, local_ip6[0]) ...
8614 offsetof(struct __sk_buff, local_ip6[3]):
8615 #if IS_ENABLED(CONFIG_IPV6)
8616 BUILD_BUG_ON(sizeof_field(struct sock_common,
8617 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
8620 off -= offsetof(struct __sk_buff, local_ip6[0]);
8622 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
8623 si->dst_reg, si->src_reg,
8624 offsetof(struct sk_buff, sk));
8625 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
8626 offsetof(struct sock_common,
8627 skc_v6_rcv_saddr.s6_addr32[0]) +
8630 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
8634 case offsetof(struct __sk_buff, remote_port):
8635 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_dport) != 2);
8637 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
8638 si->dst_reg, si->src_reg,
8639 offsetof(struct sk_buff, sk));
8640 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
8641 bpf_target_off(struct sock_common,
8644 #ifndef __BIG_ENDIAN_BITFIELD
8645 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
8649 case offsetof(struct __sk_buff, local_port):
8650 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_num) != 2);
8652 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
8653 si->dst_reg, si->src_reg,
8654 offsetof(struct sk_buff, sk));
8655 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
8656 bpf_target_off(struct sock_common,
8657 skc_num, 2, target_size));
8660 case offsetof(struct __sk_buff, tstamp):
8661 BUILD_BUG_ON(sizeof_field(struct sk_buff, tstamp) != 8);
8663 if (type == BPF_WRITE)
8664 *insn++ = BPF_STX_MEM(BPF_DW,
8665 si->dst_reg, si->src_reg,
8666 bpf_target_off(struct sk_buff,
8670 *insn++ = BPF_LDX_MEM(BPF_DW,
8671 si->dst_reg, si->src_reg,
8672 bpf_target_off(struct sk_buff,
8677 case offsetof(struct __sk_buff, gso_segs):
8678 insn = bpf_convert_shinfo_access(si, insn);
8679 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct skb_shared_info, gso_segs),
8680 si->dst_reg, si->dst_reg,
8681 bpf_target_off(struct skb_shared_info,
8685 case offsetof(struct __sk_buff, gso_size):
8686 insn = bpf_convert_shinfo_access(si, insn);
8687 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct skb_shared_info, gso_size),
8688 si->dst_reg, si->dst_reg,
8689 bpf_target_off(struct skb_shared_info,
8693 case offsetof(struct __sk_buff, wire_len):
8694 BUILD_BUG_ON(sizeof_field(struct qdisc_skb_cb, pkt_len) != 4);
8697 off -= offsetof(struct __sk_buff, wire_len);
8698 off += offsetof(struct sk_buff, cb);
8699 off += offsetof(struct qdisc_skb_cb, pkt_len);
8701 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, off);
8704 case offsetof(struct __sk_buff, sk):
8705 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
8706 si->dst_reg, si->src_reg,
8707 offsetof(struct sk_buff, sk));
8711 return insn - insn_buf;
8714 u32 bpf_sock_convert_ctx_access(enum bpf_access_type type,
8715 const struct bpf_insn *si,
8716 struct bpf_insn *insn_buf,
8717 struct bpf_prog *prog, u32 *target_size)
8719 struct bpf_insn *insn = insn_buf;
8723 case offsetof(struct bpf_sock, bound_dev_if):
8724 BUILD_BUG_ON(sizeof_field(struct sock, sk_bound_dev_if) != 4);
8726 if (type == BPF_WRITE)
8727 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
8728 offsetof(struct sock, sk_bound_dev_if));
8730 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
8731 offsetof(struct sock, sk_bound_dev_if));
8734 case offsetof(struct bpf_sock, mark):
8735 BUILD_BUG_ON(sizeof_field(struct sock, sk_mark) != 4);
8737 if (type == BPF_WRITE)
8738 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
8739 offsetof(struct sock, sk_mark));
8741 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
8742 offsetof(struct sock, sk_mark));
8745 case offsetof(struct bpf_sock, priority):
8746 BUILD_BUG_ON(sizeof_field(struct sock, sk_priority) != 4);
8748 if (type == BPF_WRITE)
8749 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
8750 offsetof(struct sock, sk_priority));
8752 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
8753 offsetof(struct sock, sk_priority));
8756 case offsetof(struct bpf_sock, family):
8757 *insn++ = BPF_LDX_MEM(
8758 BPF_FIELD_SIZEOF(struct sock_common, skc_family),
8759 si->dst_reg, si->src_reg,
8760 bpf_target_off(struct sock_common,
8762 sizeof_field(struct sock_common,
8767 case offsetof(struct bpf_sock, type):
8768 *insn++ = BPF_LDX_MEM(
8769 BPF_FIELD_SIZEOF(struct sock, sk_type),
8770 si->dst_reg, si->src_reg,
8771 bpf_target_off(struct sock, sk_type,
8772 sizeof_field(struct sock, sk_type),
8776 case offsetof(struct bpf_sock, protocol):
8777 *insn++ = BPF_LDX_MEM(
8778 BPF_FIELD_SIZEOF(struct sock, sk_protocol),
8779 si->dst_reg, si->src_reg,
8780 bpf_target_off(struct sock, sk_protocol,
8781 sizeof_field(struct sock, sk_protocol),
8785 case offsetof(struct bpf_sock, src_ip4):
8786 *insn++ = BPF_LDX_MEM(
8787 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
8788 bpf_target_off(struct sock_common, skc_rcv_saddr,
8789 sizeof_field(struct sock_common,
8794 case offsetof(struct bpf_sock, dst_ip4):
8795 *insn++ = BPF_LDX_MEM(
8796 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
8797 bpf_target_off(struct sock_common, skc_daddr,
8798 sizeof_field(struct sock_common,
8803 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
8804 #if IS_ENABLED(CONFIG_IPV6)
8806 off -= offsetof(struct bpf_sock, src_ip6[0]);
8807 *insn++ = BPF_LDX_MEM(
8808 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
8811 skc_v6_rcv_saddr.s6_addr32[0],
8812 sizeof_field(struct sock_common,
8813 skc_v6_rcv_saddr.s6_addr32[0]),
8814 target_size) + off);
8817 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
8821 case bpf_ctx_range_till(struct bpf_sock, dst_ip6[0], dst_ip6[3]):
8822 #if IS_ENABLED(CONFIG_IPV6)
8824 off -= offsetof(struct bpf_sock, dst_ip6[0]);
8825 *insn++ = BPF_LDX_MEM(
8826 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
8827 bpf_target_off(struct sock_common,
8828 skc_v6_daddr.s6_addr32[0],
8829 sizeof_field(struct sock_common,
8830 skc_v6_daddr.s6_addr32[0]),
8831 target_size) + off);
8833 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
8838 case offsetof(struct bpf_sock, src_port):
8839 *insn++ = BPF_LDX_MEM(
8840 BPF_FIELD_SIZEOF(struct sock_common, skc_num),
8841 si->dst_reg, si->src_reg,
8842 bpf_target_off(struct sock_common, skc_num,
8843 sizeof_field(struct sock_common,
8848 case offsetof(struct bpf_sock, dst_port):
8849 *insn++ = BPF_LDX_MEM(
8850 BPF_FIELD_SIZEOF(struct sock_common, skc_dport),
8851 si->dst_reg, si->src_reg,
8852 bpf_target_off(struct sock_common, skc_dport,
8853 sizeof_field(struct sock_common,
8858 case offsetof(struct bpf_sock, state):
8859 *insn++ = BPF_LDX_MEM(
8860 BPF_FIELD_SIZEOF(struct sock_common, skc_state),
8861 si->dst_reg, si->src_reg,
8862 bpf_target_off(struct sock_common, skc_state,
8863 sizeof_field(struct sock_common,
8867 case offsetof(struct bpf_sock, rx_queue_mapping):
8869 *insn++ = BPF_LDX_MEM(
8870 BPF_FIELD_SIZEOF(struct sock, sk_rx_queue_mapping),
8871 si->dst_reg, si->src_reg,
8872 bpf_target_off(struct sock, sk_rx_queue_mapping,
8873 sizeof_field(struct sock,
8874 sk_rx_queue_mapping),
8876 *insn++ = BPF_JMP_IMM(BPF_JNE, si->dst_reg, NO_QUEUE_MAPPING,
8878 *insn++ = BPF_MOV64_IMM(si->dst_reg, -1);
8880 *insn++ = BPF_MOV64_IMM(si->dst_reg, -1);
8886 return insn - insn_buf;
8889 static u32 tc_cls_act_convert_ctx_access(enum bpf_access_type type,
8890 const struct bpf_insn *si,
8891 struct bpf_insn *insn_buf,
8892 struct bpf_prog *prog, u32 *target_size)
8894 struct bpf_insn *insn = insn_buf;
8897 case offsetof(struct __sk_buff, ifindex):
8898 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
8899 si->dst_reg, si->src_reg,
8900 offsetof(struct sk_buff, dev));
8901 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
8902 bpf_target_off(struct net_device, ifindex, 4,
8906 return bpf_convert_ctx_access(type, si, insn_buf, prog,
8910 return insn - insn_buf;
8913 static u32 xdp_convert_ctx_access(enum bpf_access_type type,
8914 const struct bpf_insn *si,
8915 struct bpf_insn *insn_buf,
8916 struct bpf_prog *prog, u32 *target_size)
8918 struct bpf_insn *insn = insn_buf;
8921 case offsetof(struct xdp_md, data):
8922 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data),
8923 si->dst_reg, si->src_reg,
8924 offsetof(struct xdp_buff, data));
8926 case offsetof(struct xdp_md, data_meta):
8927 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_meta),
8928 si->dst_reg, si->src_reg,
8929 offsetof(struct xdp_buff, data_meta));
8931 case offsetof(struct xdp_md, data_end):
8932 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_end),
8933 si->dst_reg, si->src_reg,
8934 offsetof(struct xdp_buff, data_end));
8936 case offsetof(struct xdp_md, ingress_ifindex):
8937 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, rxq),
8938 si->dst_reg, si->src_reg,
8939 offsetof(struct xdp_buff, rxq));
8940 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_rxq_info, dev),
8941 si->dst_reg, si->dst_reg,
8942 offsetof(struct xdp_rxq_info, dev));
8943 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
8944 offsetof(struct net_device, ifindex));
8946 case offsetof(struct xdp_md, rx_queue_index):
8947 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, rxq),
8948 si->dst_reg, si->src_reg,
8949 offsetof(struct xdp_buff, rxq));
8950 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
8951 offsetof(struct xdp_rxq_info,
8954 case offsetof(struct xdp_md, egress_ifindex):
8955 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, txq),
8956 si->dst_reg, si->src_reg,
8957 offsetof(struct xdp_buff, txq));
8958 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_txq_info, dev),
8959 si->dst_reg, si->dst_reg,
8960 offsetof(struct xdp_txq_info, dev));
8961 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
8962 offsetof(struct net_device, ifindex));
8966 return insn - insn_buf;
8969 /* SOCK_ADDR_LOAD_NESTED_FIELD() loads Nested Field S.F.NF where S is type of
8970 * context Structure, F is Field in context structure that contains a pointer
8971 * to Nested Structure of type NS that has the field NF.
8973 * SIZE encodes the load size (BPF_B, BPF_H, etc). It's up to caller to make
8974 * sure that SIZE is not greater than actual size of S.F.NF.
8976 * If offset OFF is provided, the load happens from that offset relative to
8979 #define SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, SIZE, OFF) \
8981 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), si->dst_reg, \
8982 si->src_reg, offsetof(S, F)); \
8983 *insn++ = BPF_LDX_MEM( \
8984 SIZE, si->dst_reg, si->dst_reg, \
8985 bpf_target_off(NS, NF, sizeof_field(NS, NF), \
8990 #define SOCK_ADDR_LOAD_NESTED_FIELD(S, NS, F, NF) \
8991 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, \
8992 BPF_FIELD_SIZEOF(NS, NF), 0)
8994 /* SOCK_ADDR_STORE_NESTED_FIELD_OFF() has semantic similar to
8995 * SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF() but for store operation.
8997 * In addition it uses Temporary Field TF (member of struct S) as the 3rd
8998 * "register" since two registers available in convert_ctx_access are not
8999 * enough: we can't override neither SRC, since it contains value to store, nor
9000 * DST since it contains pointer to context that may be used by later
9001 * instructions. But we need a temporary place to save pointer to nested
9002 * structure whose field we want to store to.
9004 #define SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, SIZE, OFF, TF) \
9006 int tmp_reg = BPF_REG_9; \
9007 if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg) \
9009 if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg) \
9011 *insn++ = BPF_STX_MEM(BPF_DW, si->dst_reg, tmp_reg, \
9013 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), tmp_reg, \
9014 si->dst_reg, offsetof(S, F)); \
9015 *insn++ = BPF_STX_MEM(SIZE, tmp_reg, si->src_reg, \
9016 bpf_target_off(NS, NF, sizeof_field(NS, NF), \
9019 *insn++ = BPF_LDX_MEM(BPF_DW, tmp_reg, si->dst_reg, \
9023 #define SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, SIZE, OFF, \
9026 if (type == BPF_WRITE) { \
9027 SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, SIZE, \
9030 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF( \
9031 S, NS, F, NF, SIZE, OFF); \
9035 #define SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD(S, NS, F, NF, TF) \
9036 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF( \
9037 S, NS, F, NF, BPF_FIELD_SIZEOF(NS, NF), 0, TF)
9039 static u32 sock_addr_convert_ctx_access(enum bpf_access_type type,
9040 const struct bpf_insn *si,
9041 struct bpf_insn *insn_buf,
9042 struct bpf_prog *prog, u32 *target_size)
9044 int off, port_size = sizeof_field(struct sockaddr_in6, sin6_port);
9045 struct bpf_insn *insn = insn_buf;
9048 case offsetof(struct bpf_sock_addr, user_family):
9049 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
9050 struct sockaddr, uaddr, sa_family);
9053 case offsetof(struct bpf_sock_addr, user_ip4):
9054 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
9055 struct bpf_sock_addr_kern, struct sockaddr_in, uaddr,
9056 sin_addr, BPF_SIZE(si->code), 0, tmp_reg);
9059 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
9061 off -= offsetof(struct bpf_sock_addr, user_ip6[0]);
9062 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
9063 struct bpf_sock_addr_kern, struct sockaddr_in6, uaddr,
9064 sin6_addr.s6_addr32[0], BPF_SIZE(si->code), off,
9068 case offsetof(struct bpf_sock_addr, user_port):
9069 /* To get port we need to know sa_family first and then treat
9070 * sockaddr as either sockaddr_in or sockaddr_in6.
9071 * Though we can simplify since port field has same offset and
9072 * size in both structures.
9073 * Here we check this invariant and use just one of the
9074 * structures if it's true.
9076 BUILD_BUG_ON(offsetof(struct sockaddr_in, sin_port) !=
9077 offsetof(struct sockaddr_in6, sin6_port));
9078 BUILD_BUG_ON(sizeof_field(struct sockaddr_in, sin_port) !=
9079 sizeof_field(struct sockaddr_in6, sin6_port));
9080 /* Account for sin6_port being smaller than user_port. */
9081 port_size = min(port_size, BPF_LDST_BYTES(si));
9082 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
9083 struct bpf_sock_addr_kern, struct sockaddr_in6, uaddr,
9084 sin6_port, bytes_to_bpf_size(port_size), 0, tmp_reg);
9087 case offsetof(struct bpf_sock_addr, family):
9088 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
9089 struct sock, sk, sk_family);
9092 case offsetof(struct bpf_sock_addr, type):
9093 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
9094 struct sock, sk, sk_type);
9097 case offsetof(struct bpf_sock_addr, protocol):
9098 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
9099 struct sock, sk, sk_protocol);
9102 case offsetof(struct bpf_sock_addr, msg_src_ip4):
9103 /* Treat t_ctx as struct in_addr for msg_src_ip4. */
9104 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
9105 struct bpf_sock_addr_kern, struct in_addr, t_ctx,
9106 s_addr, BPF_SIZE(si->code), 0, tmp_reg);
9109 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
9112 off -= offsetof(struct bpf_sock_addr, msg_src_ip6[0]);
9113 /* Treat t_ctx as struct in6_addr for msg_src_ip6. */
9114 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
9115 struct bpf_sock_addr_kern, struct in6_addr, t_ctx,
9116 s6_addr32[0], BPF_SIZE(si->code), off, tmp_reg);
9118 case offsetof(struct bpf_sock_addr, sk):
9119 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_addr_kern, sk),
9120 si->dst_reg, si->src_reg,
9121 offsetof(struct bpf_sock_addr_kern, sk));
9125 return insn - insn_buf;
9128 static u32 sock_ops_convert_ctx_access(enum bpf_access_type type,
9129 const struct bpf_insn *si,
9130 struct bpf_insn *insn_buf,
9131 struct bpf_prog *prog,
9134 struct bpf_insn *insn = insn_buf;
9137 /* Helper macro for adding read access to tcp_sock or sock fields. */
9138 #define SOCK_OPS_GET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ) \
9140 int fullsock_reg = si->dst_reg, reg = BPF_REG_9, jmp = 2; \
9141 BUILD_BUG_ON(sizeof_field(OBJ, OBJ_FIELD) > \
9142 sizeof_field(struct bpf_sock_ops, BPF_FIELD)); \
9143 if (si->dst_reg == reg || si->src_reg == reg) \
9145 if (si->dst_reg == reg || si->src_reg == reg) \
9147 if (si->dst_reg == si->src_reg) { \
9148 *insn++ = BPF_STX_MEM(BPF_DW, si->src_reg, reg, \
9149 offsetof(struct bpf_sock_ops_kern, \
9151 fullsock_reg = reg; \
9154 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
9155 struct bpf_sock_ops_kern, \
9157 fullsock_reg, si->src_reg, \
9158 offsetof(struct bpf_sock_ops_kern, \
9160 *insn++ = BPF_JMP_IMM(BPF_JEQ, fullsock_reg, 0, jmp); \
9161 if (si->dst_reg == si->src_reg) \
9162 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->src_reg, \
9163 offsetof(struct bpf_sock_ops_kern, \
9165 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
9166 struct bpf_sock_ops_kern, sk),\
9167 si->dst_reg, si->src_reg, \
9168 offsetof(struct bpf_sock_ops_kern, sk));\
9169 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(OBJ, \
9171 si->dst_reg, si->dst_reg, \
9172 offsetof(OBJ, OBJ_FIELD)); \
9173 if (si->dst_reg == si->src_reg) { \
9174 *insn++ = BPF_JMP_A(1); \
9175 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->src_reg, \
9176 offsetof(struct bpf_sock_ops_kern, \
9181 #define SOCK_OPS_GET_SK() \
9183 int fullsock_reg = si->dst_reg, reg = BPF_REG_9, jmp = 1; \
9184 if (si->dst_reg == reg || si->src_reg == reg) \
9186 if (si->dst_reg == reg || si->src_reg == reg) \
9188 if (si->dst_reg == si->src_reg) { \
9189 *insn++ = BPF_STX_MEM(BPF_DW, si->src_reg, reg, \
9190 offsetof(struct bpf_sock_ops_kern, \
9192 fullsock_reg = reg; \
9195 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
9196 struct bpf_sock_ops_kern, \
9198 fullsock_reg, si->src_reg, \
9199 offsetof(struct bpf_sock_ops_kern, \
9201 *insn++ = BPF_JMP_IMM(BPF_JEQ, fullsock_reg, 0, jmp); \
9202 if (si->dst_reg == si->src_reg) \
9203 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->src_reg, \
9204 offsetof(struct bpf_sock_ops_kern, \
9206 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
9207 struct bpf_sock_ops_kern, sk),\
9208 si->dst_reg, si->src_reg, \
9209 offsetof(struct bpf_sock_ops_kern, sk));\
9210 if (si->dst_reg == si->src_reg) { \
9211 *insn++ = BPF_JMP_A(1); \
9212 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->src_reg, \
9213 offsetof(struct bpf_sock_ops_kern, \
9218 #define SOCK_OPS_GET_TCP_SOCK_FIELD(FIELD) \
9219 SOCK_OPS_GET_FIELD(FIELD, FIELD, struct tcp_sock)
9221 /* Helper macro for adding write access to tcp_sock or sock fields.
9222 * The macro is called with two registers, dst_reg which contains a pointer
9223 * to ctx (context) and src_reg which contains the value that should be
9224 * stored. However, we need an additional register since we cannot overwrite
9225 * dst_reg because it may be used later in the program.
9226 * Instead we "borrow" one of the other register. We first save its value
9227 * into a new (temp) field in bpf_sock_ops_kern, use it, and then restore
9228 * it at the end of the macro.
9230 #define SOCK_OPS_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ) \
9232 int reg = BPF_REG_9; \
9233 BUILD_BUG_ON(sizeof_field(OBJ, OBJ_FIELD) > \
9234 sizeof_field(struct bpf_sock_ops, BPF_FIELD)); \
9235 if (si->dst_reg == reg || si->src_reg == reg) \
9237 if (si->dst_reg == reg || si->src_reg == reg) \
9239 *insn++ = BPF_STX_MEM(BPF_DW, si->dst_reg, reg, \
9240 offsetof(struct bpf_sock_ops_kern, \
9242 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
9243 struct bpf_sock_ops_kern, \
9246 offsetof(struct bpf_sock_ops_kern, \
9248 *insn++ = BPF_JMP_IMM(BPF_JEQ, reg, 0, 2); \
9249 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
9250 struct bpf_sock_ops_kern, sk),\
9252 offsetof(struct bpf_sock_ops_kern, sk));\
9253 *insn++ = BPF_STX_MEM(BPF_FIELD_SIZEOF(OBJ, OBJ_FIELD), \
9255 offsetof(OBJ, OBJ_FIELD)); \
9256 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->dst_reg, \
9257 offsetof(struct bpf_sock_ops_kern, \
9261 #define SOCK_OPS_GET_OR_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ, TYPE) \
9263 if (TYPE == BPF_WRITE) \
9264 SOCK_OPS_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ); \
9266 SOCK_OPS_GET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ); \
9269 if (insn > insn_buf)
9270 return insn - insn_buf;
9273 case offsetof(struct bpf_sock_ops, op):
9274 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_ops_kern,
9276 si->dst_reg, si->src_reg,
9277 offsetof(struct bpf_sock_ops_kern, op));
9280 case offsetof(struct bpf_sock_ops, replylong[0]) ...
9281 offsetof(struct bpf_sock_ops, replylong[3]):
9282 BUILD_BUG_ON(sizeof_field(struct bpf_sock_ops, reply) !=
9283 sizeof_field(struct bpf_sock_ops_kern, reply));
9284 BUILD_BUG_ON(sizeof_field(struct bpf_sock_ops, replylong) !=
9285 sizeof_field(struct bpf_sock_ops_kern, replylong));
9287 off -= offsetof(struct bpf_sock_ops, replylong[0]);
9288 off += offsetof(struct bpf_sock_ops_kern, replylong[0]);
9289 if (type == BPF_WRITE)
9290 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
9293 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
9297 case offsetof(struct bpf_sock_ops, family):
9298 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_family) != 2);
9300 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
9301 struct bpf_sock_ops_kern, sk),
9302 si->dst_reg, si->src_reg,
9303 offsetof(struct bpf_sock_ops_kern, sk));
9304 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
9305 offsetof(struct sock_common, skc_family));
9308 case offsetof(struct bpf_sock_ops, remote_ip4):
9309 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_daddr) != 4);
9311 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
9312 struct bpf_sock_ops_kern, sk),
9313 si->dst_reg, si->src_reg,
9314 offsetof(struct bpf_sock_ops_kern, sk));
9315 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
9316 offsetof(struct sock_common, skc_daddr));
9319 case offsetof(struct bpf_sock_ops, local_ip4):
9320 BUILD_BUG_ON(sizeof_field(struct sock_common,
9321 skc_rcv_saddr) != 4);
9323 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
9324 struct bpf_sock_ops_kern, sk),
9325 si->dst_reg, si->src_reg,
9326 offsetof(struct bpf_sock_ops_kern, sk));
9327 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
9328 offsetof(struct sock_common,
9332 case offsetof(struct bpf_sock_ops, remote_ip6[0]) ...
9333 offsetof(struct bpf_sock_ops, remote_ip6[3]):
9334 #if IS_ENABLED(CONFIG_IPV6)
9335 BUILD_BUG_ON(sizeof_field(struct sock_common,
9336 skc_v6_daddr.s6_addr32[0]) != 4);
9339 off -= offsetof(struct bpf_sock_ops, remote_ip6[0]);
9340 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
9341 struct bpf_sock_ops_kern, sk),
9342 si->dst_reg, si->src_reg,
9343 offsetof(struct bpf_sock_ops_kern, sk));
9344 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
9345 offsetof(struct sock_common,
9346 skc_v6_daddr.s6_addr32[0]) +
9349 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
9353 case offsetof(struct bpf_sock_ops, local_ip6[0]) ...
9354 offsetof(struct bpf_sock_ops, local_ip6[3]):
9355 #if IS_ENABLED(CONFIG_IPV6)
9356 BUILD_BUG_ON(sizeof_field(struct sock_common,
9357 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
9360 off -= offsetof(struct bpf_sock_ops, local_ip6[0]);
9361 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
9362 struct bpf_sock_ops_kern, sk),
9363 si->dst_reg, si->src_reg,
9364 offsetof(struct bpf_sock_ops_kern, sk));
9365 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
9366 offsetof(struct sock_common,
9367 skc_v6_rcv_saddr.s6_addr32[0]) +
9370 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
9374 case offsetof(struct bpf_sock_ops, remote_port):
9375 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_dport) != 2);
9377 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
9378 struct bpf_sock_ops_kern, sk),
9379 si->dst_reg, si->src_reg,
9380 offsetof(struct bpf_sock_ops_kern, sk));
9381 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
9382 offsetof(struct sock_common, skc_dport));
9383 #ifndef __BIG_ENDIAN_BITFIELD
9384 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
9388 case offsetof(struct bpf_sock_ops, local_port):
9389 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_num) != 2);
9391 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
9392 struct bpf_sock_ops_kern, sk),
9393 si->dst_reg, si->src_reg,
9394 offsetof(struct bpf_sock_ops_kern, sk));
9395 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
9396 offsetof(struct sock_common, skc_num));
9399 case offsetof(struct bpf_sock_ops, is_fullsock):
9400 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
9401 struct bpf_sock_ops_kern,
9403 si->dst_reg, si->src_reg,
9404 offsetof(struct bpf_sock_ops_kern,
9408 case offsetof(struct bpf_sock_ops, state):
9409 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_state) != 1);
9411 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
9412 struct bpf_sock_ops_kern, sk),
9413 si->dst_reg, si->src_reg,
9414 offsetof(struct bpf_sock_ops_kern, sk));
9415 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->dst_reg,
9416 offsetof(struct sock_common, skc_state));
9419 case offsetof(struct bpf_sock_ops, rtt_min):
9420 BUILD_BUG_ON(sizeof_field(struct tcp_sock, rtt_min) !=
9421 sizeof(struct minmax));
9422 BUILD_BUG_ON(sizeof(struct minmax) <
9423 sizeof(struct minmax_sample));
9425 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
9426 struct bpf_sock_ops_kern, sk),
9427 si->dst_reg, si->src_reg,
9428 offsetof(struct bpf_sock_ops_kern, sk));
9429 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
9430 offsetof(struct tcp_sock, rtt_min) +
9431 sizeof_field(struct minmax_sample, t));
9434 case offsetof(struct bpf_sock_ops, bpf_sock_ops_cb_flags):
9435 SOCK_OPS_GET_FIELD(bpf_sock_ops_cb_flags, bpf_sock_ops_cb_flags,
9439 case offsetof(struct bpf_sock_ops, sk_txhash):
9440 SOCK_OPS_GET_OR_SET_FIELD(sk_txhash, sk_txhash,
9443 case offsetof(struct bpf_sock_ops, snd_cwnd):
9444 SOCK_OPS_GET_TCP_SOCK_FIELD(snd_cwnd);
9446 case offsetof(struct bpf_sock_ops, srtt_us):
9447 SOCK_OPS_GET_TCP_SOCK_FIELD(srtt_us);
9449 case offsetof(struct bpf_sock_ops, snd_ssthresh):
9450 SOCK_OPS_GET_TCP_SOCK_FIELD(snd_ssthresh);
9452 case offsetof(struct bpf_sock_ops, rcv_nxt):
9453 SOCK_OPS_GET_TCP_SOCK_FIELD(rcv_nxt);
9455 case offsetof(struct bpf_sock_ops, snd_nxt):
9456 SOCK_OPS_GET_TCP_SOCK_FIELD(snd_nxt);
9458 case offsetof(struct bpf_sock_ops, snd_una):
9459 SOCK_OPS_GET_TCP_SOCK_FIELD(snd_una);
9461 case offsetof(struct bpf_sock_ops, mss_cache):
9462 SOCK_OPS_GET_TCP_SOCK_FIELD(mss_cache);
9464 case offsetof(struct bpf_sock_ops, ecn_flags):
9465 SOCK_OPS_GET_TCP_SOCK_FIELD(ecn_flags);
9467 case offsetof(struct bpf_sock_ops, rate_delivered):
9468 SOCK_OPS_GET_TCP_SOCK_FIELD(rate_delivered);
9470 case offsetof(struct bpf_sock_ops, rate_interval_us):
9471 SOCK_OPS_GET_TCP_SOCK_FIELD(rate_interval_us);
9473 case offsetof(struct bpf_sock_ops, packets_out):
9474 SOCK_OPS_GET_TCP_SOCK_FIELD(packets_out);
9476 case offsetof(struct bpf_sock_ops, retrans_out):
9477 SOCK_OPS_GET_TCP_SOCK_FIELD(retrans_out);
9479 case offsetof(struct bpf_sock_ops, total_retrans):
9480 SOCK_OPS_GET_TCP_SOCK_FIELD(total_retrans);
9482 case offsetof(struct bpf_sock_ops, segs_in):
9483 SOCK_OPS_GET_TCP_SOCK_FIELD(segs_in);
9485 case offsetof(struct bpf_sock_ops, data_segs_in):
9486 SOCK_OPS_GET_TCP_SOCK_FIELD(data_segs_in);
9488 case offsetof(struct bpf_sock_ops, segs_out):
9489 SOCK_OPS_GET_TCP_SOCK_FIELD(segs_out);
9491 case offsetof(struct bpf_sock_ops, data_segs_out):
9492 SOCK_OPS_GET_TCP_SOCK_FIELD(data_segs_out);
9494 case offsetof(struct bpf_sock_ops, lost_out):
9495 SOCK_OPS_GET_TCP_SOCK_FIELD(lost_out);
9497 case offsetof(struct bpf_sock_ops, sacked_out):
9498 SOCK_OPS_GET_TCP_SOCK_FIELD(sacked_out);
9500 case offsetof(struct bpf_sock_ops, bytes_received):
9501 SOCK_OPS_GET_TCP_SOCK_FIELD(bytes_received);
9503 case offsetof(struct bpf_sock_ops, bytes_acked):
9504 SOCK_OPS_GET_TCP_SOCK_FIELD(bytes_acked);
9506 case offsetof(struct bpf_sock_ops, sk):
9509 case offsetof(struct bpf_sock_ops, skb_data_end):
9510 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_ops_kern,
9512 si->dst_reg, si->src_reg,
9513 offsetof(struct bpf_sock_ops_kern,
9516 case offsetof(struct bpf_sock_ops, skb_data):
9517 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_ops_kern,
9519 si->dst_reg, si->src_reg,
9520 offsetof(struct bpf_sock_ops_kern,
9522 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
9523 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
9524 si->dst_reg, si->dst_reg,
9525 offsetof(struct sk_buff, data));
9527 case offsetof(struct bpf_sock_ops, skb_len):
9528 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_ops_kern,
9530 si->dst_reg, si->src_reg,
9531 offsetof(struct bpf_sock_ops_kern,
9533 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
9534 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, len),
9535 si->dst_reg, si->dst_reg,
9536 offsetof(struct sk_buff, len));
9538 case offsetof(struct bpf_sock_ops, skb_tcp_flags):
9539 off = offsetof(struct sk_buff, cb);
9540 off += offsetof(struct tcp_skb_cb, tcp_flags);
9541 *target_size = sizeof_field(struct tcp_skb_cb, tcp_flags);
9542 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_ops_kern,
9544 si->dst_reg, si->src_reg,
9545 offsetof(struct bpf_sock_ops_kern,
9547 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
9548 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct tcp_skb_cb,
9550 si->dst_reg, si->dst_reg, off);
9553 return insn - insn_buf;
9556 static u32 sk_skb_convert_ctx_access(enum bpf_access_type type,
9557 const struct bpf_insn *si,
9558 struct bpf_insn *insn_buf,
9559 struct bpf_prog *prog, u32 *target_size)
9561 struct bpf_insn *insn = insn_buf;
9565 case offsetof(struct __sk_buff, data_end):
9567 off -= offsetof(struct __sk_buff, data_end);
9568 off += offsetof(struct sk_buff, cb);
9569 off += offsetof(struct tcp_skb_cb, bpf.data_end);
9570 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
9573 case offsetof(struct __sk_buff, cb[0]) ...
9574 offsetofend(struct __sk_buff, cb[4]) - 1:
9575 BUILD_BUG_ON(sizeof_field(struct sk_skb_cb, data) < 20);
9576 BUILD_BUG_ON((offsetof(struct sk_buff, cb) +
9577 offsetof(struct sk_skb_cb, data)) %
9580 prog->cb_access = 1;
9582 off -= offsetof(struct __sk_buff, cb[0]);
9583 off += offsetof(struct sk_buff, cb);
9584 off += offsetof(struct sk_skb_cb, data);
9585 if (type == BPF_WRITE)
9586 *insn++ = BPF_STX_MEM(BPF_SIZE(si->code), si->dst_reg,
9589 *insn++ = BPF_LDX_MEM(BPF_SIZE(si->code), si->dst_reg,
9595 return bpf_convert_ctx_access(type, si, insn_buf, prog,
9599 return insn - insn_buf;
9602 static u32 sk_msg_convert_ctx_access(enum bpf_access_type type,
9603 const struct bpf_insn *si,
9604 struct bpf_insn *insn_buf,
9605 struct bpf_prog *prog, u32 *target_size)
9607 struct bpf_insn *insn = insn_buf;
9608 #if IS_ENABLED(CONFIG_IPV6)
9612 /* convert ctx uses the fact sg element is first in struct */
9613 BUILD_BUG_ON(offsetof(struct sk_msg, sg) != 0);
9616 case offsetof(struct sk_msg_md, data):
9617 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg, data),
9618 si->dst_reg, si->src_reg,
9619 offsetof(struct sk_msg, data));
9621 case offsetof(struct sk_msg_md, data_end):
9622 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg, data_end),
9623 si->dst_reg, si->src_reg,
9624 offsetof(struct sk_msg, data_end));
9626 case offsetof(struct sk_msg_md, family):
9627 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_family) != 2);
9629 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
9631 si->dst_reg, si->src_reg,
9632 offsetof(struct sk_msg, sk));
9633 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
9634 offsetof(struct sock_common, skc_family));
9637 case offsetof(struct sk_msg_md, remote_ip4):
9638 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_daddr) != 4);
9640 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
9642 si->dst_reg, si->src_reg,
9643 offsetof(struct sk_msg, sk));
9644 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
9645 offsetof(struct sock_common, skc_daddr));
9648 case offsetof(struct sk_msg_md, local_ip4):
9649 BUILD_BUG_ON(sizeof_field(struct sock_common,
9650 skc_rcv_saddr) != 4);
9652 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
9654 si->dst_reg, si->src_reg,
9655 offsetof(struct sk_msg, sk));
9656 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
9657 offsetof(struct sock_common,
9661 case offsetof(struct sk_msg_md, remote_ip6[0]) ...
9662 offsetof(struct sk_msg_md, remote_ip6[3]):
9663 #if IS_ENABLED(CONFIG_IPV6)
9664 BUILD_BUG_ON(sizeof_field(struct sock_common,
9665 skc_v6_daddr.s6_addr32[0]) != 4);
9668 off -= offsetof(struct sk_msg_md, remote_ip6[0]);
9669 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
9671 si->dst_reg, si->src_reg,
9672 offsetof(struct sk_msg, sk));
9673 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
9674 offsetof(struct sock_common,
9675 skc_v6_daddr.s6_addr32[0]) +
9678 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
9682 case offsetof(struct sk_msg_md, local_ip6[0]) ...
9683 offsetof(struct sk_msg_md, local_ip6[3]):
9684 #if IS_ENABLED(CONFIG_IPV6)
9685 BUILD_BUG_ON(sizeof_field(struct sock_common,
9686 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
9689 off -= offsetof(struct sk_msg_md, local_ip6[0]);
9690 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
9692 si->dst_reg, si->src_reg,
9693 offsetof(struct sk_msg, sk));
9694 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
9695 offsetof(struct sock_common,
9696 skc_v6_rcv_saddr.s6_addr32[0]) +
9699 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
9703 case offsetof(struct sk_msg_md, remote_port):
9704 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_dport) != 2);
9706 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
9708 si->dst_reg, si->src_reg,
9709 offsetof(struct sk_msg, sk));
9710 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
9711 offsetof(struct sock_common, skc_dport));
9712 #ifndef __BIG_ENDIAN_BITFIELD
9713 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
9717 case offsetof(struct sk_msg_md, local_port):
9718 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_num) != 2);
9720 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
9722 si->dst_reg, si->src_reg,
9723 offsetof(struct sk_msg, sk));
9724 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
9725 offsetof(struct sock_common, skc_num));
9728 case offsetof(struct sk_msg_md, size):
9729 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg_sg, size),
9730 si->dst_reg, si->src_reg,
9731 offsetof(struct sk_msg_sg, size));
9734 case offsetof(struct sk_msg_md, sk):
9735 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg, sk),
9736 si->dst_reg, si->src_reg,
9737 offsetof(struct sk_msg, sk));
9741 return insn - insn_buf;
9744 const struct bpf_verifier_ops sk_filter_verifier_ops = {
9745 .get_func_proto = sk_filter_func_proto,
9746 .is_valid_access = sk_filter_is_valid_access,
9747 .convert_ctx_access = bpf_convert_ctx_access,
9748 .gen_ld_abs = bpf_gen_ld_abs,
9751 const struct bpf_prog_ops sk_filter_prog_ops = {
9752 .test_run = bpf_prog_test_run_skb,
9755 const struct bpf_verifier_ops tc_cls_act_verifier_ops = {
9756 .get_func_proto = tc_cls_act_func_proto,
9757 .is_valid_access = tc_cls_act_is_valid_access,
9758 .convert_ctx_access = tc_cls_act_convert_ctx_access,
9759 .gen_prologue = tc_cls_act_prologue,
9760 .gen_ld_abs = bpf_gen_ld_abs,
9763 const struct bpf_prog_ops tc_cls_act_prog_ops = {
9764 .test_run = bpf_prog_test_run_skb,
9767 const struct bpf_verifier_ops xdp_verifier_ops = {
9768 .get_func_proto = xdp_func_proto,
9769 .is_valid_access = xdp_is_valid_access,
9770 .convert_ctx_access = xdp_convert_ctx_access,
9771 .gen_prologue = bpf_noop_prologue,
9774 const struct bpf_prog_ops xdp_prog_ops = {
9775 .test_run = bpf_prog_test_run_xdp,
9778 const struct bpf_verifier_ops cg_skb_verifier_ops = {
9779 .get_func_proto = cg_skb_func_proto,
9780 .is_valid_access = cg_skb_is_valid_access,
9781 .convert_ctx_access = bpf_convert_ctx_access,
9784 const struct bpf_prog_ops cg_skb_prog_ops = {
9785 .test_run = bpf_prog_test_run_skb,
9788 const struct bpf_verifier_ops lwt_in_verifier_ops = {
9789 .get_func_proto = lwt_in_func_proto,
9790 .is_valid_access = lwt_is_valid_access,
9791 .convert_ctx_access = bpf_convert_ctx_access,
9794 const struct bpf_prog_ops lwt_in_prog_ops = {
9795 .test_run = bpf_prog_test_run_skb,
9798 const struct bpf_verifier_ops lwt_out_verifier_ops = {
9799 .get_func_proto = lwt_out_func_proto,
9800 .is_valid_access = lwt_is_valid_access,
9801 .convert_ctx_access = bpf_convert_ctx_access,
9804 const struct bpf_prog_ops lwt_out_prog_ops = {
9805 .test_run = bpf_prog_test_run_skb,
9808 const struct bpf_verifier_ops lwt_xmit_verifier_ops = {
9809 .get_func_proto = lwt_xmit_func_proto,
9810 .is_valid_access = lwt_is_valid_access,
9811 .convert_ctx_access = bpf_convert_ctx_access,
9812 .gen_prologue = tc_cls_act_prologue,
9815 const struct bpf_prog_ops lwt_xmit_prog_ops = {
9816 .test_run = bpf_prog_test_run_skb,
9819 const struct bpf_verifier_ops lwt_seg6local_verifier_ops = {
9820 .get_func_proto = lwt_seg6local_func_proto,
9821 .is_valid_access = lwt_is_valid_access,
9822 .convert_ctx_access = bpf_convert_ctx_access,
9825 const struct bpf_prog_ops lwt_seg6local_prog_ops = {
9826 .test_run = bpf_prog_test_run_skb,
9829 const struct bpf_verifier_ops cg_sock_verifier_ops = {
9830 .get_func_proto = sock_filter_func_proto,
9831 .is_valid_access = sock_filter_is_valid_access,
9832 .convert_ctx_access = bpf_sock_convert_ctx_access,
9835 const struct bpf_prog_ops cg_sock_prog_ops = {
9838 const struct bpf_verifier_ops cg_sock_addr_verifier_ops = {
9839 .get_func_proto = sock_addr_func_proto,
9840 .is_valid_access = sock_addr_is_valid_access,
9841 .convert_ctx_access = sock_addr_convert_ctx_access,
9844 const struct bpf_prog_ops cg_sock_addr_prog_ops = {
9847 const struct bpf_verifier_ops sock_ops_verifier_ops = {
9848 .get_func_proto = sock_ops_func_proto,
9849 .is_valid_access = sock_ops_is_valid_access,
9850 .convert_ctx_access = sock_ops_convert_ctx_access,
9853 const struct bpf_prog_ops sock_ops_prog_ops = {
9856 const struct bpf_verifier_ops sk_skb_verifier_ops = {
9857 .get_func_proto = sk_skb_func_proto,
9858 .is_valid_access = sk_skb_is_valid_access,
9859 .convert_ctx_access = sk_skb_convert_ctx_access,
9860 .gen_prologue = sk_skb_prologue,
9863 const struct bpf_prog_ops sk_skb_prog_ops = {
9866 const struct bpf_verifier_ops sk_msg_verifier_ops = {
9867 .get_func_proto = sk_msg_func_proto,
9868 .is_valid_access = sk_msg_is_valid_access,
9869 .convert_ctx_access = sk_msg_convert_ctx_access,
9870 .gen_prologue = bpf_noop_prologue,
9873 const struct bpf_prog_ops sk_msg_prog_ops = {
9876 const struct bpf_verifier_ops flow_dissector_verifier_ops = {
9877 .get_func_proto = flow_dissector_func_proto,
9878 .is_valid_access = flow_dissector_is_valid_access,
9879 .convert_ctx_access = flow_dissector_convert_ctx_access,
9882 const struct bpf_prog_ops flow_dissector_prog_ops = {
9883 .test_run = bpf_prog_test_run_flow_dissector,
9886 int sk_detach_filter(struct sock *sk)
9889 struct sk_filter *filter;
9891 if (sock_flag(sk, SOCK_FILTER_LOCKED))
9894 filter = rcu_dereference_protected(sk->sk_filter,
9895 lockdep_sock_is_held(sk));
9897 RCU_INIT_POINTER(sk->sk_filter, NULL);
9898 sk_filter_uncharge(sk, filter);
9904 EXPORT_SYMBOL_GPL(sk_detach_filter);
9906 int sk_get_filter(struct sock *sk, sockptr_t optval, unsigned int len)
9908 struct sock_fprog_kern *fprog;
9909 struct sk_filter *filter;
9913 filter = rcu_dereference_protected(sk->sk_filter,
9914 lockdep_sock_is_held(sk));
9918 /* We're copying the filter that has been originally attached,
9919 * so no conversion/decode needed anymore. eBPF programs that
9920 * have no original program cannot be dumped through this.
9923 fprog = filter->prog->orig_prog;
9929 /* User space only enquires number of filter blocks. */
9933 if (len < fprog->len)
9937 if (copy_to_sockptr(optval, fprog->filter, bpf_classic_proglen(fprog)))
9940 /* Instead of bytes, the API requests to return the number
9950 static void bpf_init_reuseport_kern(struct sk_reuseport_kern *reuse_kern,
9951 struct sock_reuseport *reuse,
9952 struct sock *sk, struct sk_buff *skb,
9955 reuse_kern->skb = skb;
9956 reuse_kern->sk = sk;
9957 reuse_kern->selected_sk = NULL;
9958 reuse_kern->data_end = skb->data + skb_headlen(skb);
9959 reuse_kern->hash = hash;
9960 reuse_kern->reuseport_id = reuse->reuseport_id;
9961 reuse_kern->bind_inany = reuse->bind_inany;
9964 struct sock *bpf_run_sk_reuseport(struct sock_reuseport *reuse, struct sock *sk,
9965 struct bpf_prog *prog, struct sk_buff *skb,
9968 struct sk_reuseport_kern reuse_kern;
9969 enum sk_action action;
9971 bpf_init_reuseport_kern(&reuse_kern, reuse, sk, skb, hash);
9972 action = BPF_PROG_RUN(prog, &reuse_kern);
9974 if (action == SK_PASS)
9975 return reuse_kern.selected_sk;
9977 return ERR_PTR(-ECONNREFUSED);
9980 BPF_CALL_4(sk_select_reuseport, struct sk_reuseport_kern *, reuse_kern,
9981 struct bpf_map *, map, void *, key, u32, flags)
9983 bool is_sockarray = map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY;
9984 struct sock_reuseport *reuse;
9985 struct sock *selected_sk;
9987 selected_sk = map->ops->map_lookup_elem(map, key);
9991 reuse = rcu_dereference(selected_sk->sk_reuseport_cb);
9993 /* Lookup in sock_map can return TCP ESTABLISHED sockets. */
9994 if (sk_is_refcounted(selected_sk))
9995 sock_put(selected_sk);
9997 /* reuseport_array has only sk with non NULL sk_reuseport_cb.
9998 * The only (!reuse) case here is - the sk has already been
9999 * unhashed (e.g. by close()), so treat it as -ENOENT.
10001 * Other maps (e.g. sock_map) do not provide this guarantee and
10002 * the sk may never be in the reuseport group to begin with.
10004 return is_sockarray ? -ENOENT : -EINVAL;
10007 if (unlikely(reuse->reuseport_id != reuse_kern->reuseport_id)) {
10008 struct sock *sk = reuse_kern->sk;
10010 if (sk->sk_protocol != selected_sk->sk_protocol)
10011 return -EPROTOTYPE;
10012 else if (sk->sk_family != selected_sk->sk_family)
10013 return -EAFNOSUPPORT;
10015 /* Catch all. Likely bound to a different sockaddr. */
10019 reuse_kern->selected_sk = selected_sk;
10024 static const struct bpf_func_proto sk_select_reuseport_proto = {
10025 .func = sk_select_reuseport,
10027 .ret_type = RET_INTEGER,
10028 .arg1_type = ARG_PTR_TO_CTX,
10029 .arg2_type = ARG_CONST_MAP_PTR,
10030 .arg3_type = ARG_PTR_TO_MAP_KEY,
10031 .arg4_type = ARG_ANYTHING,
10034 BPF_CALL_4(sk_reuseport_load_bytes,
10035 const struct sk_reuseport_kern *, reuse_kern, u32, offset,
10036 void *, to, u32, len)
10038 return ____bpf_skb_load_bytes(reuse_kern->skb, offset, to, len);
10041 static const struct bpf_func_proto sk_reuseport_load_bytes_proto = {
10042 .func = sk_reuseport_load_bytes,
10044 .ret_type = RET_INTEGER,
10045 .arg1_type = ARG_PTR_TO_CTX,
10046 .arg2_type = ARG_ANYTHING,
10047 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
10048 .arg4_type = ARG_CONST_SIZE,
10051 BPF_CALL_5(sk_reuseport_load_bytes_relative,
10052 const struct sk_reuseport_kern *, reuse_kern, u32, offset,
10053 void *, to, u32, len, u32, start_header)
10055 return ____bpf_skb_load_bytes_relative(reuse_kern->skb, offset, to,
10056 len, start_header);
10059 static const struct bpf_func_proto sk_reuseport_load_bytes_relative_proto = {
10060 .func = sk_reuseport_load_bytes_relative,
10062 .ret_type = RET_INTEGER,
10063 .arg1_type = ARG_PTR_TO_CTX,
10064 .arg2_type = ARG_ANYTHING,
10065 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
10066 .arg4_type = ARG_CONST_SIZE,
10067 .arg5_type = ARG_ANYTHING,
10070 static const struct bpf_func_proto *
10071 sk_reuseport_func_proto(enum bpf_func_id func_id,
10072 const struct bpf_prog *prog)
10075 case BPF_FUNC_sk_select_reuseport:
10076 return &sk_select_reuseport_proto;
10077 case BPF_FUNC_skb_load_bytes:
10078 return &sk_reuseport_load_bytes_proto;
10079 case BPF_FUNC_skb_load_bytes_relative:
10080 return &sk_reuseport_load_bytes_relative_proto;
10082 return bpf_base_func_proto(func_id);
10087 sk_reuseport_is_valid_access(int off, int size,
10088 enum bpf_access_type type,
10089 const struct bpf_prog *prog,
10090 struct bpf_insn_access_aux *info)
10092 const u32 size_default = sizeof(__u32);
10094 if (off < 0 || off >= sizeof(struct sk_reuseport_md) ||
10095 off % size || type != BPF_READ)
10099 case offsetof(struct sk_reuseport_md, data):
10100 info->reg_type = PTR_TO_PACKET;
10101 return size == sizeof(__u64);
10103 case offsetof(struct sk_reuseport_md, data_end):
10104 info->reg_type = PTR_TO_PACKET_END;
10105 return size == sizeof(__u64);
10107 case offsetof(struct sk_reuseport_md, hash):
10108 return size == size_default;
10110 /* Fields that allow narrowing */
10111 case bpf_ctx_range(struct sk_reuseport_md, eth_protocol):
10112 if (size < sizeof_field(struct sk_buff, protocol))
10115 case bpf_ctx_range(struct sk_reuseport_md, ip_protocol):
10116 case bpf_ctx_range(struct sk_reuseport_md, bind_inany):
10117 case bpf_ctx_range(struct sk_reuseport_md, len):
10118 bpf_ctx_record_field_size(info, size_default);
10119 return bpf_ctx_narrow_access_ok(off, size, size_default);
10126 #define SK_REUSEPORT_LOAD_FIELD(F) ({ \
10127 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_reuseport_kern, F), \
10128 si->dst_reg, si->src_reg, \
10129 bpf_target_off(struct sk_reuseport_kern, F, \
10130 sizeof_field(struct sk_reuseport_kern, F), \
10134 #define SK_REUSEPORT_LOAD_SKB_FIELD(SKB_FIELD) \
10135 SOCK_ADDR_LOAD_NESTED_FIELD(struct sk_reuseport_kern, \
10140 #define SK_REUSEPORT_LOAD_SK_FIELD(SK_FIELD) \
10141 SOCK_ADDR_LOAD_NESTED_FIELD(struct sk_reuseport_kern, \
10146 static u32 sk_reuseport_convert_ctx_access(enum bpf_access_type type,
10147 const struct bpf_insn *si,
10148 struct bpf_insn *insn_buf,
10149 struct bpf_prog *prog,
10152 struct bpf_insn *insn = insn_buf;
10155 case offsetof(struct sk_reuseport_md, data):
10156 SK_REUSEPORT_LOAD_SKB_FIELD(data);
10159 case offsetof(struct sk_reuseport_md, len):
10160 SK_REUSEPORT_LOAD_SKB_FIELD(len);
10163 case offsetof(struct sk_reuseport_md, eth_protocol):
10164 SK_REUSEPORT_LOAD_SKB_FIELD(protocol);
10167 case offsetof(struct sk_reuseport_md, ip_protocol):
10168 SK_REUSEPORT_LOAD_SK_FIELD(sk_protocol);
10171 case offsetof(struct sk_reuseport_md, data_end):
10172 SK_REUSEPORT_LOAD_FIELD(data_end);
10175 case offsetof(struct sk_reuseport_md, hash):
10176 SK_REUSEPORT_LOAD_FIELD(hash);
10179 case offsetof(struct sk_reuseport_md, bind_inany):
10180 SK_REUSEPORT_LOAD_FIELD(bind_inany);
10184 return insn - insn_buf;
10187 const struct bpf_verifier_ops sk_reuseport_verifier_ops = {
10188 .get_func_proto = sk_reuseport_func_proto,
10189 .is_valid_access = sk_reuseport_is_valid_access,
10190 .convert_ctx_access = sk_reuseport_convert_ctx_access,
10193 const struct bpf_prog_ops sk_reuseport_prog_ops = {
10196 DEFINE_STATIC_KEY_FALSE(bpf_sk_lookup_enabled);
10197 EXPORT_SYMBOL(bpf_sk_lookup_enabled);
10199 BPF_CALL_3(bpf_sk_lookup_assign, struct bpf_sk_lookup_kern *, ctx,
10200 struct sock *, sk, u64, flags)
10202 if (unlikely(flags & ~(BPF_SK_LOOKUP_F_REPLACE |
10203 BPF_SK_LOOKUP_F_NO_REUSEPORT)))
10205 if (unlikely(sk && sk_is_refcounted(sk)))
10206 return -ESOCKTNOSUPPORT; /* reject non-RCU freed sockets */
10207 if (unlikely(sk && sk->sk_state == TCP_ESTABLISHED))
10208 return -ESOCKTNOSUPPORT; /* reject connected sockets */
10210 /* Check if socket is suitable for packet L3/L4 protocol */
10211 if (sk && sk->sk_protocol != ctx->protocol)
10212 return -EPROTOTYPE;
10213 if (sk && sk->sk_family != ctx->family &&
10214 (sk->sk_family == AF_INET || ipv6_only_sock(sk)))
10215 return -EAFNOSUPPORT;
10217 if (ctx->selected_sk && !(flags & BPF_SK_LOOKUP_F_REPLACE))
10220 /* Select socket as lookup result */
10221 ctx->selected_sk = sk;
10222 ctx->no_reuseport = flags & BPF_SK_LOOKUP_F_NO_REUSEPORT;
10226 static const struct bpf_func_proto bpf_sk_lookup_assign_proto = {
10227 .func = bpf_sk_lookup_assign,
10229 .ret_type = RET_INTEGER,
10230 .arg1_type = ARG_PTR_TO_CTX,
10231 .arg2_type = ARG_PTR_TO_SOCKET_OR_NULL,
10232 .arg3_type = ARG_ANYTHING,
10235 static const struct bpf_func_proto *
10236 sk_lookup_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
10239 case BPF_FUNC_perf_event_output:
10240 return &bpf_event_output_data_proto;
10241 case BPF_FUNC_sk_assign:
10242 return &bpf_sk_lookup_assign_proto;
10243 case BPF_FUNC_sk_release:
10244 return &bpf_sk_release_proto;
10246 return bpf_sk_base_func_proto(func_id);
10250 static bool sk_lookup_is_valid_access(int off, int size,
10251 enum bpf_access_type type,
10252 const struct bpf_prog *prog,
10253 struct bpf_insn_access_aux *info)
10255 if (off < 0 || off >= sizeof(struct bpf_sk_lookup))
10257 if (off % size != 0)
10259 if (type != BPF_READ)
10263 case offsetof(struct bpf_sk_lookup, sk):
10264 info->reg_type = PTR_TO_SOCKET_OR_NULL;
10265 return size == sizeof(__u64);
10267 case bpf_ctx_range(struct bpf_sk_lookup, family):
10268 case bpf_ctx_range(struct bpf_sk_lookup, protocol):
10269 case bpf_ctx_range(struct bpf_sk_lookup, remote_ip4):
10270 case bpf_ctx_range(struct bpf_sk_lookup, local_ip4):
10271 case bpf_ctx_range_till(struct bpf_sk_lookup, remote_ip6[0], remote_ip6[3]):
10272 case bpf_ctx_range_till(struct bpf_sk_lookup, local_ip6[0], local_ip6[3]):
10273 case bpf_ctx_range(struct bpf_sk_lookup, remote_port):
10274 case bpf_ctx_range(struct bpf_sk_lookup, local_port):
10275 bpf_ctx_record_field_size(info, sizeof(__u32));
10276 return bpf_ctx_narrow_access_ok(off, size, sizeof(__u32));
10283 static u32 sk_lookup_convert_ctx_access(enum bpf_access_type type,
10284 const struct bpf_insn *si,
10285 struct bpf_insn *insn_buf,
10286 struct bpf_prog *prog,
10289 struct bpf_insn *insn = insn_buf;
10292 case offsetof(struct bpf_sk_lookup, sk):
10293 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg, si->src_reg,
10294 offsetof(struct bpf_sk_lookup_kern, selected_sk));
10297 case offsetof(struct bpf_sk_lookup, family):
10298 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
10299 bpf_target_off(struct bpf_sk_lookup_kern,
10300 family, 2, target_size));
10303 case offsetof(struct bpf_sk_lookup, protocol):
10304 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
10305 bpf_target_off(struct bpf_sk_lookup_kern,
10306 protocol, 2, target_size));
10309 case offsetof(struct bpf_sk_lookup, remote_ip4):
10310 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
10311 bpf_target_off(struct bpf_sk_lookup_kern,
10312 v4.saddr, 4, target_size));
10315 case offsetof(struct bpf_sk_lookup, local_ip4):
10316 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
10317 bpf_target_off(struct bpf_sk_lookup_kern,
10318 v4.daddr, 4, target_size));
10321 case bpf_ctx_range_till(struct bpf_sk_lookup,
10322 remote_ip6[0], remote_ip6[3]): {
10323 #if IS_ENABLED(CONFIG_IPV6)
10326 off -= offsetof(struct bpf_sk_lookup, remote_ip6[0]);
10327 off += bpf_target_off(struct in6_addr, s6_addr32[0], 4, target_size);
10328 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg, si->src_reg,
10329 offsetof(struct bpf_sk_lookup_kern, v6.saddr));
10330 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
10331 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, off);
10333 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
10337 case bpf_ctx_range_till(struct bpf_sk_lookup,
10338 local_ip6[0], local_ip6[3]): {
10339 #if IS_ENABLED(CONFIG_IPV6)
10342 off -= offsetof(struct bpf_sk_lookup, local_ip6[0]);
10343 off += bpf_target_off(struct in6_addr, s6_addr32[0], 4, target_size);
10344 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg, si->src_reg,
10345 offsetof(struct bpf_sk_lookup_kern, v6.daddr));
10346 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
10347 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, off);
10349 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
10353 case offsetof(struct bpf_sk_lookup, remote_port):
10354 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
10355 bpf_target_off(struct bpf_sk_lookup_kern,
10356 sport, 2, target_size));
10359 case offsetof(struct bpf_sk_lookup, local_port):
10360 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
10361 bpf_target_off(struct bpf_sk_lookup_kern,
10362 dport, 2, target_size));
10366 return insn - insn_buf;
10369 const struct bpf_prog_ops sk_lookup_prog_ops = {
10370 .test_run = bpf_prog_test_run_sk_lookup,
10373 const struct bpf_verifier_ops sk_lookup_verifier_ops = {
10374 .get_func_proto = sk_lookup_func_proto,
10375 .is_valid_access = sk_lookup_is_valid_access,
10376 .convert_ctx_access = sk_lookup_convert_ctx_access,
10379 #endif /* CONFIG_INET */
10381 DEFINE_BPF_DISPATCHER(xdp)
10383 void bpf_prog_change_xdp(struct bpf_prog *prev_prog, struct bpf_prog *prog)
10385 bpf_dispatcher_change_prog(BPF_DISPATCHER_PTR(xdp), prev_prog, prog);
10388 #ifdef CONFIG_DEBUG_INFO_BTF
10389 BTF_ID_LIST_GLOBAL(btf_sock_ids)
10390 #define BTF_SOCK_TYPE(name, type) BTF_ID(struct, type)
10392 #undef BTF_SOCK_TYPE
10394 u32 btf_sock_ids[MAX_BTF_SOCK_TYPE];
10397 BPF_CALL_1(bpf_skc_to_tcp6_sock, struct sock *, sk)
10399 /* tcp6_sock type is not generated in dwarf and hence btf,
10400 * trigger an explicit type generation here.
10402 BTF_TYPE_EMIT(struct tcp6_sock);
10403 if (sk && sk_fullsock(sk) && sk->sk_protocol == IPPROTO_TCP &&
10404 sk->sk_family == AF_INET6)
10405 return (unsigned long)sk;
10407 return (unsigned long)NULL;
10410 const struct bpf_func_proto bpf_skc_to_tcp6_sock_proto = {
10411 .func = bpf_skc_to_tcp6_sock,
10413 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
10414 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
10415 .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_TCP6],
10418 BPF_CALL_1(bpf_skc_to_tcp_sock, struct sock *, sk)
10420 if (sk && sk_fullsock(sk) && sk->sk_protocol == IPPROTO_TCP)
10421 return (unsigned long)sk;
10423 return (unsigned long)NULL;
10426 const struct bpf_func_proto bpf_skc_to_tcp_sock_proto = {
10427 .func = bpf_skc_to_tcp_sock,
10429 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
10430 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
10431 .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_TCP],
10434 BPF_CALL_1(bpf_skc_to_tcp_timewait_sock, struct sock *, sk)
10436 /* BTF types for tcp_timewait_sock and inet_timewait_sock are not
10437 * generated if CONFIG_INET=n. Trigger an explicit generation here.
10439 BTF_TYPE_EMIT(struct inet_timewait_sock);
10440 BTF_TYPE_EMIT(struct tcp_timewait_sock);
10443 if (sk && sk->sk_prot == &tcp_prot && sk->sk_state == TCP_TIME_WAIT)
10444 return (unsigned long)sk;
10447 #if IS_BUILTIN(CONFIG_IPV6)
10448 if (sk && sk->sk_prot == &tcpv6_prot && sk->sk_state == TCP_TIME_WAIT)
10449 return (unsigned long)sk;
10452 return (unsigned long)NULL;
10455 const struct bpf_func_proto bpf_skc_to_tcp_timewait_sock_proto = {
10456 .func = bpf_skc_to_tcp_timewait_sock,
10458 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
10459 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
10460 .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_TCP_TW],
10463 BPF_CALL_1(bpf_skc_to_tcp_request_sock, struct sock *, sk)
10466 if (sk && sk->sk_prot == &tcp_prot && sk->sk_state == TCP_NEW_SYN_RECV)
10467 return (unsigned long)sk;
10470 #if IS_BUILTIN(CONFIG_IPV6)
10471 if (sk && sk->sk_prot == &tcpv6_prot && sk->sk_state == TCP_NEW_SYN_RECV)
10472 return (unsigned long)sk;
10475 return (unsigned long)NULL;
10478 const struct bpf_func_proto bpf_skc_to_tcp_request_sock_proto = {
10479 .func = bpf_skc_to_tcp_request_sock,
10481 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
10482 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
10483 .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_TCP_REQ],
10486 BPF_CALL_1(bpf_skc_to_udp6_sock, struct sock *, sk)
10488 /* udp6_sock type is not generated in dwarf and hence btf,
10489 * trigger an explicit type generation here.
10491 BTF_TYPE_EMIT(struct udp6_sock);
10492 if (sk && sk_fullsock(sk) && sk->sk_protocol == IPPROTO_UDP &&
10493 sk->sk_type == SOCK_DGRAM && sk->sk_family == AF_INET6)
10494 return (unsigned long)sk;
10496 return (unsigned long)NULL;
10499 const struct bpf_func_proto bpf_skc_to_udp6_sock_proto = {
10500 .func = bpf_skc_to_udp6_sock,
10502 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
10503 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
10504 .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_UDP6],
10507 static const struct bpf_func_proto *
10508 bpf_sk_base_func_proto(enum bpf_func_id func_id)
10510 const struct bpf_func_proto *func;
10513 case BPF_FUNC_skc_to_tcp6_sock:
10514 func = &bpf_skc_to_tcp6_sock_proto;
10516 case BPF_FUNC_skc_to_tcp_sock:
10517 func = &bpf_skc_to_tcp_sock_proto;
10519 case BPF_FUNC_skc_to_tcp_timewait_sock:
10520 func = &bpf_skc_to_tcp_timewait_sock_proto;
10522 case BPF_FUNC_skc_to_tcp_request_sock:
10523 func = &bpf_skc_to_tcp_request_sock_proto;
10525 case BPF_FUNC_skc_to_udp6_sock:
10526 func = &bpf_skc_to_udp6_sock_proto;
10529 return bpf_base_func_proto(func_id);
10532 if (!perfmon_capable())