1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Linux Socket Filter - Kernel level socket filtering
5 * Based on the design of the Berkeley Packet Filter. The new
6 * internal format has been designed by PLUMgrid:
8 * Copyright (c) 2011 - 2014 PLUMgrid, http://plumgrid.com
12 * Jay Schulist <jschlst@samba.org>
13 * Alexei Starovoitov <ast@plumgrid.com>
14 * Daniel Borkmann <dborkman@redhat.com>
16 * Andi Kleen - Fix a few bad bugs and races.
17 * Kris Katterjohn - Added many additional checks in bpf_check_classic()
20 #include <linux/atomic.h>
21 #include <linux/bpf_verifier.h>
22 #include <linux/module.h>
23 #include <linux/types.h>
25 #include <linux/fcntl.h>
26 #include <linux/socket.h>
27 #include <linux/sock_diag.h>
29 #include <linux/inet.h>
30 #include <linux/netdevice.h>
31 #include <linux/if_packet.h>
32 #include <linux/if_arp.h>
33 #include <linux/gfp.h>
34 #include <net/inet_common.h>
36 #include <net/protocol.h>
37 #include <net/netlink.h>
38 #include <linux/skbuff.h>
39 #include <linux/skmsg.h>
41 #include <net/flow_dissector.h>
42 #include <linux/errno.h>
43 #include <linux/timer.h>
44 #include <linux/uaccess.h>
45 #include <asm/unaligned.h>
46 #include <linux/filter.h>
47 #include <linux/ratelimit.h>
48 #include <linux/seccomp.h>
49 #include <linux/if_vlan.h>
50 #include <linux/bpf.h>
51 #include <linux/btf.h>
52 #include <net/sch_generic.h>
53 #include <net/cls_cgroup.h>
54 #include <net/dst_metadata.h>
56 #include <net/sock_reuseport.h>
57 #include <net/busy_poll.h>
61 #include <linux/bpf_trace.h>
62 #include <net/xdp_sock.h>
63 #include <linux/inetdevice.h>
64 #include <net/inet_hashtables.h>
65 #include <net/inet6_hashtables.h>
66 #include <net/ip_fib.h>
67 #include <net/nexthop.h>
71 #include <net/net_namespace.h>
72 #include <linux/seg6_local.h>
74 #include <net/seg6_local.h>
75 #include <net/lwtunnel.h>
76 #include <net/ipv6_stubs.h>
77 #include <net/bpf_sk_storage.h>
78 #include <net/transp_v6.h>
79 #include <linux/btf_ids.h>
82 #include <net/mptcp.h>
84 static const struct bpf_func_proto *
85 bpf_sk_base_func_proto(enum bpf_func_id func_id);
87 int copy_bpf_fprog_from_user(struct sock_fprog *dst, sockptr_t src, int len)
89 if (in_compat_syscall()) {
90 struct compat_sock_fprog f32;
92 if (len != sizeof(f32))
94 if (copy_from_sockptr(&f32, src, sizeof(f32)))
96 memset(dst, 0, sizeof(*dst));
98 dst->filter = compat_ptr(f32.filter);
100 if (len != sizeof(*dst))
102 if (copy_from_sockptr(dst, src, sizeof(*dst)))
108 EXPORT_SYMBOL_GPL(copy_bpf_fprog_from_user);
111 * sk_filter_trim_cap - run a packet through a socket filter
112 * @sk: sock associated with &sk_buff
113 * @skb: buffer to filter
114 * @cap: limit on how short the eBPF program may trim the packet
116 * Run the eBPF program and then cut skb->data to correct size returned by
117 * the program. If pkt_len is 0 we toss packet. If skb->len is smaller
118 * than pkt_len we keep whole skb->data. This is the socket level
119 * wrapper to bpf_prog_run. It returns 0 if the packet should
120 * be accepted or -EPERM if the packet should be tossed.
123 int sk_filter_trim_cap(struct sock *sk, struct sk_buff *skb, unsigned int cap)
126 struct sk_filter *filter;
129 * If the skb was allocated from pfmemalloc reserves, only
130 * allow SOCK_MEMALLOC sockets to use it as this socket is
131 * helping free memory
133 if (skb_pfmemalloc(skb) && !sock_flag(sk, SOCK_MEMALLOC)) {
134 NET_INC_STATS(sock_net(sk), LINUX_MIB_PFMEMALLOCDROP);
137 err = BPF_CGROUP_RUN_PROG_INET_INGRESS(sk, skb);
141 err = security_sock_rcv_skb(sk, skb);
146 filter = rcu_dereference(sk->sk_filter);
148 struct sock *save_sk = skb->sk;
149 unsigned int pkt_len;
152 pkt_len = bpf_prog_run_save_cb(filter->prog, skb);
154 err = pkt_len ? pskb_trim(skb, max(cap, pkt_len)) : -EPERM;
160 EXPORT_SYMBOL(sk_filter_trim_cap);
162 BPF_CALL_1(bpf_skb_get_pay_offset, struct sk_buff *, skb)
164 return skb_get_poff(skb);
167 BPF_CALL_3(bpf_skb_get_nlattr, struct sk_buff *, skb, u32, a, u32, x)
171 if (skb_is_nonlinear(skb))
174 if (skb->len < sizeof(struct nlattr))
177 if (a > skb->len - sizeof(struct nlattr))
180 nla = nla_find((struct nlattr *) &skb->data[a], skb->len - a, x);
182 return (void *) nla - (void *) skb->data;
187 BPF_CALL_3(bpf_skb_get_nlattr_nest, struct sk_buff *, skb, u32, a, u32, x)
191 if (skb_is_nonlinear(skb))
194 if (skb->len < sizeof(struct nlattr))
197 if (a > skb->len - sizeof(struct nlattr))
200 nla = (struct nlattr *) &skb->data[a];
201 if (nla->nla_len > skb->len - a)
204 nla = nla_find_nested(nla, x);
206 return (void *) nla - (void *) skb->data;
211 BPF_CALL_4(bpf_skb_load_helper_8, const struct sk_buff *, skb, const void *,
212 data, int, headlen, int, offset)
215 const int len = sizeof(tmp);
218 if (headlen - offset >= len)
219 return *(u8 *)(data + offset);
220 if (!skb_copy_bits(skb, offset, &tmp, sizeof(tmp)))
223 ptr = bpf_internal_load_pointer_neg_helper(skb, offset, len);
231 BPF_CALL_2(bpf_skb_load_helper_8_no_cache, const struct sk_buff *, skb,
234 return ____bpf_skb_load_helper_8(skb, skb->data, skb->len - skb->data_len,
238 BPF_CALL_4(bpf_skb_load_helper_16, const struct sk_buff *, skb, const void *,
239 data, int, headlen, int, offset)
242 const int len = sizeof(tmp);
245 if (headlen - offset >= len)
246 return get_unaligned_be16(data + offset);
247 if (!skb_copy_bits(skb, offset, &tmp, sizeof(tmp)))
248 return be16_to_cpu(tmp);
250 ptr = bpf_internal_load_pointer_neg_helper(skb, offset, len);
252 return get_unaligned_be16(ptr);
258 BPF_CALL_2(bpf_skb_load_helper_16_no_cache, const struct sk_buff *, skb,
261 return ____bpf_skb_load_helper_16(skb, skb->data, skb->len - skb->data_len,
265 BPF_CALL_4(bpf_skb_load_helper_32, const struct sk_buff *, skb, const void *,
266 data, int, headlen, int, offset)
269 const int len = sizeof(tmp);
271 if (likely(offset >= 0)) {
272 if (headlen - offset >= len)
273 return get_unaligned_be32(data + offset);
274 if (!skb_copy_bits(skb, offset, &tmp, sizeof(tmp)))
275 return be32_to_cpu(tmp);
277 ptr = bpf_internal_load_pointer_neg_helper(skb, offset, len);
279 return get_unaligned_be32(ptr);
285 BPF_CALL_2(bpf_skb_load_helper_32_no_cache, const struct sk_buff *, skb,
288 return ____bpf_skb_load_helper_32(skb, skb->data, skb->len - skb->data_len,
292 static u32 convert_skb_access(int skb_field, int dst_reg, int src_reg,
293 struct bpf_insn *insn_buf)
295 struct bpf_insn *insn = insn_buf;
299 BUILD_BUG_ON(sizeof_field(struct sk_buff, mark) != 4);
301 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
302 offsetof(struct sk_buff, mark));
306 *insn++ = BPF_LDX_MEM(BPF_B, dst_reg, src_reg, PKT_TYPE_OFFSET);
307 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, PKT_TYPE_MAX);
308 #ifdef __BIG_ENDIAN_BITFIELD
309 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 5);
314 BUILD_BUG_ON(sizeof_field(struct sk_buff, queue_mapping) != 2);
316 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
317 offsetof(struct sk_buff, queue_mapping));
320 case SKF_AD_VLAN_TAG:
321 BUILD_BUG_ON(sizeof_field(struct sk_buff, vlan_tci) != 2);
323 /* dst_reg = *(u16 *) (src_reg + offsetof(vlan_tci)) */
324 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
325 offsetof(struct sk_buff, vlan_tci));
327 case SKF_AD_VLAN_TAG_PRESENT:
328 BUILD_BUG_ON(sizeof_field(struct sk_buff, vlan_all) != 4);
329 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
330 offsetof(struct sk_buff, vlan_all));
331 *insn++ = BPF_JMP_IMM(BPF_JEQ, dst_reg, 0, 1);
332 *insn++ = BPF_ALU32_IMM(BPF_MOV, dst_reg, 1);
336 return insn - insn_buf;
339 static bool convert_bpf_extensions(struct sock_filter *fp,
340 struct bpf_insn **insnp)
342 struct bpf_insn *insn = *insnp;
346 case SKF_AD_OFF + SKF_AD_PROTOCOL:
347 BUILD_BUG_ON(sizeof_field(struct sk_buff, protocol) != 2);
349 /* A = *(u16 *) (CTX + offsetof(protocol)) */
350 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
351 offsetof(struct sk_buff, protocol));
352 /* A = ntohs(A) [emitting a nop or swap16] */
353 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
356 case SKF_AD_OFF + SKF_AD_PKTTYPE:
357 cnt = convert_skb_access(SKF_AD_PKTTYPE, BPF_REG_A, BPF_REG_CTX, insn);
361 case SKF_AD_OFF + SKF_AD_IFINDEX:
362 case SKF_AD_OFF + SKF_AD_HATYPE:
363 BUILD_BUG_ON(sizeof_field(struct net_device, ifindex) != 4);
364 BUILD_BUG_ON(sizeof_field(struct net_device, type) != 2);
366 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
367 BPF_REG_TMP, BPF_REG_CTX,
368 offsetof(struct sk_buff, dev));
369 /* if (tmp != 0) goto pc + 1 */
370 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_TMP, 0, 1);
371 *insn++ = BPF_EXIT_INSN();
372 if (fp->k == SKF_AD_OFF + SKF_AD_IFINDEX)
373 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_TMP,
374 offsetof(struct net_device, ifindex));
376 *insn = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_TMP,
377 offsetof(struct net_device, type));
380 case SKF_AD_OFF + SKF_AD_MARK:
381 cnt = convert_skb_access(SKF_AD_MARK, BPF_REG_A, BPF_REG_CTX, insn);
385 case SKF_AD_OFF + SKF_AD_RXHASH:
386 BUILD_BUG_ON(sizeof_field(struct sk_buff, hash) != 4);
388 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX,
389 offsetof(struct sk_buff, hash));
392 case SKF_AD_OFF + SKF_AD_QUEUE:
393 cnt = convert_skb_access(SKF_AD_QUEUE, BPF_REG_A, BPF_REG_CTX, insn);
397 case SKF_AD_OFF + SKF_AD_VLAN_TAG:
398 cnt = convert_skb_access(SKF_AD_VLAN_TAG,
399 BPF_REG_A, BPF_REG_CTX, insn);
403 case SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT:
404 cnt = convert_skb_access(SKF_AD_VLAN_TAG_PRESENT,
405 BPF_REG_A, BPF_REG_CTX, insn);
409 case SKF_AD_OFF + SKF_AD_VLAN_TPID:
410 BUILD_BUG_ON(sizeof_field(struct sk_buff, vlan_proto) != 2);
412 /* A = *(u16 *) (CTX + offsetof(vlan_proto)) */
413 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
414 offsetof(struct sk_buff, vlan_proto));
415 /* A = ntohs(A) [emitting a nop or swap16] */
416 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
419 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
420 case SKF_AD_OFF + SKF_AD_NLATTR:
421 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
422 case SKF_AD_OFF + SKF_AD_CPU:
423 case SKF_AD_OFF + SKF_AD_RANDOM:
425 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX);
427 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_A);
429 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_X);
430 /* Emit call(arg1=CTX, arg2=A, arg3=X) */
432 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
433 *insn = BPF_EMIT_CALL(bpf_skb_get_pay_offset);
435 case SKF_AD_OFF + SKF_AD_NLATTR:
436 *insn = BPF_EMIT_CALL(bpf_skb_get_nlattr);
438 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
439 *insn = BPF_EMIT_CALL(bpf_skb_get_nlattr_nest);
441 case SKF_AD_OFF + SKF_AD_CPU:
442 *insn = BPF_EMIT_CALL(bpf_get_raw_cpu_id);
444 case SKF_AD_OFF + SKF_AD_RANDOM:
445 *insn = BPF_EMIT_CALL(bpf_user_rnd_u32);
446 bpf_user_rnd_init_once();
451 case SKF_AD_OFF + SKF_AD_ALU_XOR_X:
453 *insn = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_X);
457 /* This is just a dummy call to avoid letting the compiler
458 * evict __bpf_call_base() as an optimization. Placed here
459 * where no-one bothers.
461 BUG_ON(__bpf_call_base(0, 0, 0, 0, 0) != 0);
469 static bool convert_bpf_ld_abs(struct sock_filter *fp, struct bpf_insn **insnp)
471 const bool unaligned_ok = IS_BUILTIN(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS);
472 int size = bpf_size_to_bytes(BPF_SIZE(fp->code));
473 bool endian = BPF_SIZE(fp->code) == BPF_H ||
474 BPF_SIZE(fp->code) == BPF_W;
475 bool indirect = BPF_MODE(fp->code) == BPF_IND;
476 const int ip_align = NET_IP_ALIGN;
477 struct bpf_insn *insn = *insnp;
481 ((unaligned_ok && offset >= 0) ||
482 (!unaligned_ok && offset >= 0 &&
483 offset + ip_align >= 0 &&
484 offset + ip_align % size == 0))) {
485 bool ldx_off_ok = offset <= S16_MAX;
487 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_H);
489 *insn++ = BPF_ALU64_IMM(BPF_SUB, BPF_REG_TMP, offset);
490 *insn++ = BPF_JMP_IMM(BPF_JSLT, BPF_REG_TMP,
491 size, 2 + endian + (!ldx_off_ok * 2));
493 *insn++ = BPF_LDX_MEM(BPF_SIZE(fp->code), BPF_REG_A,
496 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_D);
497 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_TMP, offset);
498 *insn++ = BPF_LDX_MEM(BPF_SIZE(fp->code), BPF_REG_A,
502 *insn++ = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, size * 8);
503 *insn++ = BPF_JMP_A(8);
506 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX);
507 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_D);
508 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_H);
510 *insn++ = BPF_MOV64_IMM(BPF_REG_ARG4, offset);
512 *insn++ = BPF_MOV64_REG(BPF_REG_ARG4, BPF_REG_X);
514 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_ARG4, offset);
517 switch (BPF_SIZE(fp->code)) {
519 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_8);
522 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_16);
525 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_32);
531 *insn++ = BPF_JMP_IMM(BPF_JSGE, BPF_REG_A, 0, 2);
532 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
533 *insn = BPF_EXIT_INSN();
540 * bpf_convert_filter - convert filter program
541 * @prog: the user passed filter program
542 * @len: the length of the user passed filter program
543 * @new_prog: allocated 'struct bpf_prog' or NULL
544 * @new_len: pointer to store length of converted program
545 * @seen_ld_abs: bool whether we've seen ld_abs/ind
547 * Remap 'sock_filter' style classic BPF (cBPF) instruction set to 'bpf_insn'
548 * style extended BPF (eBPF).
549 * Conversion workflow:
551 * 1) First pass for calculating the new program length:
552 * bpf_convert_filter(old_prog, old_len, NULL, &new_len, &seen_ld_abs)
554 * 2) 2nd pass to remap in two passes: 1st pass finds new
555 * jump offsets, 2nd pass remapping:
556 * bpf_convert_filter(old_prog, old_len, new_prog, &new_len, &seen_ld_abs)
558 static int bpf_convert_filter(struct sock_filter *prog, int len,
559 struct bpf_prog *new_prog, int *new_len,
562 int new_flen = 0, pass = 0, target, i, stack_off;
563 struct bpf_insn *new_insn, *first_insn = NULL;
564 struct sock_filter *fp;
568 BUILD_BUG_ON(BPF_MEMWORDS * sizeof(u32) > MAX_BPF_STACK);
569 BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG);
571 if (len <= 0 || len > BPF_MAXINSNS)
575 first_insn = new_prog->insnsi;
576 addrs = kcalloc(len, sizeof(*addrs),
577 GFP_KERNEL | __GFP_NOWARN);
583 new_insn = first_insn;
586 /* Classic BPF related prologue emission. */
588 /* Classic BPF expects A and X to be reset first. These need
589 * to be guaranteed to be the first two instructions.
591 *new_insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
592 *new_insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_X, BPF_REG_X);
594 /* All programs must keep CTX in callee saved BPF_REG_CTX.
595 * In eBPF case it's done by the compiler, here we need to
596 * do this ourself. Initial CTX is present in BPF_REG_ARG1.
598 *new_insn++ = BPF_MOV64_REG(BPF_REG_CTX, BPF_REG_ARG1);
600 /* For packet access in classic BPF, cache skb->data
601 * in callee-saved BPF R8 and skb->len - skb->data_len
602 * (headlen) in BPF R9. Since classic BPF is read-only
603 * on CTX, we only need to cache it once.
605 *new_insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
606 BPF_REG_D, BPF_REG_CTX,
607 offsetof(struct sk_buff, data));
608 *new_insn++ = BPF_LDX_MEM(BPF_W, BPF_REG_H, BPF_REG_CTX,
609 offsetof(struct sk_buff, len));
610 *new_insn++ = BPF_LDX_MEM(BPF_W, BPF_REG_TMP, BPF_REG_CTX,
611 offsetof(struct sk_buff, data_len));
612 *new_insn++ = BPF_ALU32_REG(BPF_SUB, BPF_REG_H, BPF_REG_TMP);
618 for (i = 0; i < len; fp++, i++) {
619 struct bpf_insn tmp_insns[32] = { };
620 struct bpf_insn *insn = tmp_insns;
623 addrs[i] = new_insn - first_insn;
626 /* All arithmetic insns and skb loads map as-is. */
627 case BPF_ALU | BPF_ADD | BPF_X:
628 case BPF_ALU | BPF_ADD | BPF_K:
629 case BPF_ALU | BPF_SUB | BPF_X:
630 case BPF_ALU | BPF_SUB | BPF_K:
631 case BPF_ALU | BPF_AND | BPF_X:
632 case BPF_ALU | BPF_AND | BPF_K:
633 case BPF_ALU | BPF_OR | BPF_X:
634 case BPF_ALU | BPF_OR | BPF_K:
635 case BPF_ALU | BPF_LSH | BPF_X:
636 case BPF_ALU | BPF_LSH | BPF_K:
637 case BPF_ALU | BPF_RSH | BPF_X:
638 case BPF_ALU | BPF_RSH | BPF_K:
639 case BPF_ALU | BPF_XOR | BPF_X:
640 case BPF_ALU | BPF_XOR | BPF_K:
641 case BPF_ALU | BPF_MUL | BPF_X:
642 case BPF_ALU | BPF_MUL | BPF_K:
643 case BPF_ALU | BPF_DIV | BPF_X:
644 case BPF_ALU | BPF_DIV | BPF_K:
645 case BPF_ALU | BPF_MOD | BPF_X:
646 case BPF_ALU | BPF_MOD | BPF_K:
647 case BPF_ALU | BPF_NEG:
648 case BPF_LD | BPF_ABS | BPF_W:
649 case BPF_LD | BPF_ABS | BPF_H:
650 case BPF_LD | BPF_ABS | BPF_B:
651 case BPF_LD | BPF_IND | BPF_W:
652 case BPF_LD | BPF_IND | BPF_H:
653 case BPF_LD | BPF_IND | BPF_B:
654 /* Check for overloaded BPF extension and
655 * directly convert it if found, otherwise
656 * just move on with mapping.
658 if (BPF_CLASS(fp->code) == BPF_LD &&
659 BPF_MODE(fp->code) == BPF_ABS &&
660 convert_bpf_extensions(fp, &insn))
662 if (BPF_CLASS(fp->code) == BPF_LD &&
663 convert_bpf_ld_abs(fp, &insn)) {
668 if (fp->code == (BPF_ALU | BPF_DIV | BPF_X) ||
669 fp->code == (BPF_ALU | BPF_MOD | BPF_X)) {
670 *insn++ = BPF_MOV32_REG(BPF_REG_X, BPF_REG_X);
671 /* Error with exception code on div/mod by 0.
672 * For cBPF programs, this was always return 0.
674 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_X, 0, 2);
675 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
676 *insn++ = BPF_EXIT_INSN();
679 *insn = BPF_RAW_INSN(fp->code, BPF_REG_A, BPF_REG_X, 0, fp->k);
682 /* Jump transformation cannot use BPF block macros
683 * everywhere as offset calculation and target updates
684 * require a bit more work than the rest, i.e. jump
685 * opcodes map as-is, but offsets need adjustment.
688 #define BPF_EMIT_JMP \
690 const s32 off_min = S16_MIN, off_max = S16_MAX; \
693 if (target >= len || target < 0) \
695 off = addrs ? addrs[target] - addrs[i] - 1 : 0; \
696 /* Adjust pc relative offset for 2nd or 3rd insn. */ \
697 off -= insn - tmp_insns; \
698 /* Reject anything not fitting into insn->off. */ \
699 if (off < off_min || off > off_max) \
704 case BPF_JMP | BPF_JA:
705 target = i + fp->k + 1;
706 insn->code = fp->code;
710 case BPF_JMP | BPF_JEQ | BPF_K:
711 case BPF_JMP | BPF_JEQ | BPF_X:
712 case BPF_JMP | BPF_JSET | BPF_K:
713 case BPF_JMP | BPF_JSET | BPF_X:
714 case BPF_JMP | BPF_JGT | BPF_K:
715 case BPF_JMP | BPF_JGT | BPF_X:
716 case BPF_JMP | BPF_JGE | BPF_K:
717 case BPF_JMP | BPF_JGE | BPF_X:
718 if (BPF_SRC(fp->code) == BPF_K && (int) fp->k < 0) {
719 /* BPF immediates are signed, zero extend
720 * immediate into tmp register and use it
723 *insn++ = BPF_MOV32_IMM(BPF_REG_TMP, fp->k);
725 insn->dst_reg = BPF_REG_A;
726 insn->src_reg = BPF_REG_TMP;
729 insn->dst_reg = BPF_REG_A;
731 bpf_src = BPF_SRC(fp->code);
732 insn->src_reg = bpf_src == BPF_X ? BPF_REG_X : 0;
735 /* Common case where 'jump_false' is next insn. */
737 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
738 target = i + fp->jt + 1;
743 /* Convert some jumps when 'jump_true' is next insn. */
745 switch (BPF_OP(fp->code)) {
747 insn->code = BPF_JMP | BPF_JNE | bpf_src;
750 insn->code = BPF_JMP | BPF_JLE | bpf_src;
753 insn->code = BPF_JMP | BPF_JLT | bpf_src;
759 target = i + fp->jf + 1;
764 /* Other jumps are mapped into two insns: Jxx and JA. */
765 target = i + fp->jt + 1;
766 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
770 insn->code = BPF_JMP | BPF_JA;
771 target = i + fp->jf + 1;
775 /* ldxb 4 * ([14] & 0xf) is remaped into 6 insns. */
776 case BPF_LDX | BPF_MSH | BPF_B: {
777 struct sock_filter tmp = {
778 .code = BPF_LD | BPF_ABS | BPF_B,
785 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
786 /* A = BPF_R0 = *(u8 *) (skb->data + K) */
787 convert_bpf_ld_abs(&tmp, &insn);
790 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, 0xf);
792 *insn++ = BPF_ALU32_IMM(BPF_LSH, BPF_REG_A, 2);
794 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_X);
796 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
798 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_TMP);
801 /* RET_K is remaped into 2 insns. RET_A case doesn't need an
802 * extra mov as BPF_REG_0 is already mapped into BPF_REG_A.
804 case BPF_RET | BPF_A:
805 case BPF_RET | BPF_K:
806 if (BPF_RVAL(fp->code) == BPF_K)
807 *insn++ = BPF_MOV32_RAW(BPF_K, BPF_REG_0,
809 *insn = BPF_EXIT_INSN();
812 /* Store to stack. */
815 stack_off = fp->k * 4 + 4;
816 *insn = BPF_STX_MEM(BPF_W, BPF_REG_FP, BPF_CLASS(fp->code) ==
817 BPF_ST ? BPF_REG_A : BPF_REG_X,
819 /* check_load_and_stores() verifies that classic BPF can
820 * load from stack only after write, so tracking
821 * stack_depth for ST|STX insns is enough
823 if (new_prog && new_prog->aux->stack_depth < stack_off)
824 new_prog->aux->stack_depth = stack_off;
827 /* Load from stack. */
828 case BPF_LD | BPF_MEM:
829 case BPF_LDX | BPF_MEM:
830 stack_off = fp->k * 4 + 4;
831 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
832 BPF_REG_A : BPF_REG_X, BPF_REG_FP,
837 case BPF_LD | BPF_IMM:
838 case BPF_LDX | BPF_IMM:
839 *insn = BPF_MOV32_IMM(BPF_CLASS(fp->code) == BPF_LD ?
840 BPF_REG_A : BPF_REG_X, fp->k);
844 case BPF_MISC | BPF_TAX:
845 *insn = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
849 case BPF_MISC | BPF_TXA:
850 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_X);
853 /* A = skb->len or X = skb->len */
854 case BPF_LD | BPF_W | BPF_LEN:
855 case BPF_LDX | BPF_W | BPF_LEN:
856 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
857 BPF_REG_A : BPF_REG_X, BPF_REG_CTX,
858 offsetof(struct sk_buff, len));
861 /* Access seccomp_data fields. */
862 case BPF_LDX | BPF_ABS | BPF_W:
863 /* A = *(u32 *) (ctx + K) */
864 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX, fp->k);
867 /* Unknown instruction. */
874 memcpy(new_insn, tmp_insns,
875 sizeof(*insn) * (insn - tmp_insns));
876 new_insn += insn - tmp_insns;
880 /* Only calculating new length. */
881 *new_len = new_insn - first_insn;
883 *new_len += 4; /* Prologue bits. */
888 if (new_flen != new_insn - first_insn) {
889 new_flen = new_insn - first_insn;
896 BUG_ON(*new_len != new_flen);
905 * As we dont want to clear mem[] array for each packet going through
906 * __bpf_prog_run(), we check that filter loaded by user never try to read
907 * a cell if not previously written, and we check all branches to be sure
908 * a malicious user doesn't try to abuse us.
910 static int check_load_and_stores(const struct sock_filter *filter, int flen)
912 u16 *masks, memvalid = 0; /* One bit per cell, 16 cells */
915 BUILD_BUG_ON(BPF_MEMWORDS > 16);
917 masks = kmalloc_array(flen, sizeof(*masks), GFP_KERNEL);
921 memset(masks, 0xff, flen * sizeof(*masks));
923 for (pc = 0; pc < flen; pc++) {
924 memvalid &= masks[pc];
926 switch (filter[pc].code) {
929 memvalid |= (1 << filter[pc].k);
931 case BPF_LD | BPF_MEM:
932 case BPF_LDX | BPF_MEM:
933 if (!(memvalid & (1 << filter[pc].k))) {
938 case BPF_JMP | BPF_JA:
939 /* A jump must set masks on target */
940 masks[pc + 1 + filter[pc].k] &= memvalid;
943 case BPF_JMP | BPF_JEQ | BPF_K:
944 case BPF_JMP | BPF_JEQ | BPF_X:
945 case BPF_JMP | BPF_JGE | BPF_K:
946 case BPF_JMP | BPF_JGE | BPF_X:
947 case BPF_JMP | BPF_JGT | BPF_K:
948 case BPF_JMP | BPF_JGT | BPF_X:
949 case BPF_JMP | BPF_JSET | BPF_K:
950 case BPF_JMP | BPF_JSET | BPF_X:
951 /* A jump must set masks on targets */
952 masks[pc + 1 + filter[pc].jt] &= memvalid;
953 masks[pc + 1 + filter[pc].jf] &= memvalid;
963 static bool chk_code_allowed(u16 code_to_probe)
965 static const bool codes[] = {
966 /* 32 bit ALU operations */
967 [BPF_ALU | BPF_ADD | BPF_K] = true,
968 [BPF_ALU | BPF_ADD | BPF_X] = true,
969 [BPF_ALU | BPF_SUB | BPF_K] = true,
970 [BPF_ALU | BPF_SUB | BPF_X] = true,
971 [BPF_ALU | BPF_MUL | BPF_K] = true,
972 [BPF_ALU | BPF_MUL | BPF_X] = true,
973 [BPF_ALU | BPF_DIV | BPF_K] = true,
974 [BPF_ALU | BPF_DIV | BPF_X] = true,
975 [BPF_ALU | BPF_MOD | BPF_K] = true,
976 [BPF_ALU | BPF_MOD | BPF_X] = true,
977 [BPF_ALU | BPF_AND | BPF_K] = true,
978 [BPF_ALU | BPF_AND | BPF_X] = true,
979 [BPF_ALU | BPF_OR | BPF_K] = true,
980 [BPF_ALU | BPF_OR | BPF_X] = true,
981 [BPF_ALU | BPF_XOR | BPF_K] = true,
982 [BPF_ALU | BPF_XOR | BPF_X] = true,
983 [BPF_ALU | BPF_LSH | BPF_K] = true,
984 [BPF_ALU | BPF_LSH | BPF_X] = true,
985 [BPF_ALU | BPF_RSH | BPF_K] = true,
986 [BPF_ALU | BPF_RSH | BPF_X] = true,
987 [BPF_ALU | BPF_NEG] = true,
988 /* Load instructions */
989 [BPF_LD | BPF_W | BPF_ABS] = true,
990 [BPF_LD | BPF_H | BPF_ABS] = true,
991 [BPF_LD | BPF_B | BPF_ABS] = true,
992 [BPF_LD | BPF_W | BPF_LEN] = true,
993 [BPF_LD | BPF_W | BPF_IND] = true,
994 [BPF_LD | BPF_H | BPF_IND] = true,
995 [BPF_LD | BPF_B | BPF_IND] = true,
996 [BPF_LD | BPF_IMM] = true,
997 [BPF_LD | BPF_MEM] = true,
998 [BPF_LDX | BPF_W | BPF_LEN] = true,
999 [BPF_LDX | BPF_B | BPF_MSH] = true,
1000 [BPF_LDX | BPF_IMM] = true,
1001 [BPF_LDX | BPF_MEM] = true,
1002 /* Store instructions */
1005 /* Misc instructions */
1006 [BPF_MISC | BPF_TAX] = true,
1007 [BPF_MISC | BPF_TXA] = true,
1008 /* Return instructions */
1009 [BPF_RET | BPF_K] = true,
1010 [BPF_RET | BPF_A] = true,
1011 /* Jump instructions */
1012 [BPF_JMP | BPF_JA] = true,
1013 [BPF_JMP | BPF_JEQ | BPF_K] = true,
1014 [BPF_JMP | BPF_JEQ | BPF_X] = true,
1015 [BPF_JMP | BPF_JGE | BPF_K] = true,
1016 [BPF_JMP | BPF_JGE | BPF_X] = true,
1017 [BPF_JMP | BPF_JGT | BPF_K] = true,
1018 [BPF_JMP | BPF_JGT | BPF_X] = true,
1019 [BPF_JMP | BPF_JSET | BPF_K] = true,
1020 [BPF_JMP | BPF_JSET | BPF_X] = true,
1023 if (code_to_probe >= ARRAY_SIZE(codes))
1026 return codes[code_to_probe];
1029 static bool bpf_check_basics_ok(const struct sock_filter *filter,
1034 if (flen == 0 || flen > BPF_MAXINSNS)
1041 * bpf_check_classic - verify socket filter code
1042 * @filter: filter to verify
1043 * @flen: length of filter
1045 * Check the user's filter code. If we let some ugly
1046 * filter code slip through kaboom! The filter must contain
1047 * no references or jumps that are out of range, no illegal
1048 * instructions, and must end with a RET instruction.
1050 * All jumps are forward as they are not signed.
1052 * Returns 0 if the rule set is legal or -EINVAL if not.
1054 static int bpf_check_classic(const struct sock_filter *filter,
1060 /* Check the filter code now */
1061 for (pc = 0; pc < flen; pc++) {
1062 const struct sock_filter *ftest = &filter[pc];
1064 /* May we actually operate on this code? */
1065 if (!chk_code_allowed(ftest->code))
1068 /* Some instructions need special checks */
1069 switch (ftest->code) {
1070 case BPF_ALU | BPF_DIV | BPF_K:
1071 case BPF_ALU | BPF_MOD | BPF_K:
1072 /* Check for division by zero */
1076 case BPF_ALU | BPF_LSH | BPF_K:
1077 case BPF_ALU | BPF_RSH | BPF_K:
1081 case BPF_LD | BPF_MEM:
1082 case BPF_LDX | BPF_MEM:
1085 /* Check for invalid memory addresses */
1086 if (ftest->k >= BPF_MEMWORDS)
1089 case BPF_JMP | BPF_JA:
1090 /* Note, the large ftest->k might cause loops.
1091 * Compare this with conditional jumps below,
1092 * where offsets are limited. --ANK (981016)
1094 if (ftest->k >= (unsigned int)(flen - pc - 1))
1097 case BPF_JMP | BPF_JEQ | BPF_K:
1098 case BPF_JMP | BPF_JEQ | BPF_X:
1099 case BPF_JMP | BPF_JGE | BPF_K:
1100 case BPF_JMP | BPF_JGE | BPF_X:
1101 case BPF_JMP | BPF_JGT | BPF_K:
1102 case BPF_JMP | BPF_JGT | BPF_X:
1103 case BPF_JMP | BPF_JSET | BPF_K:
1104 case BPF_JMP | BPF_JSET | BPF_X:
1105 /* Both conditionals must be safe */
1106 if (pc + ftest->jt + 1 >= flen ||
1107 pc + ftest->jf + 1 >= flen)
1110 case BPF_LD | BPF_W | BPF_ABS:
1111 case BPF_LD | BPF_H | BPF_ABS:
1112 case BPF_LD | BPF_B | BPF_ABS:
1114 if (bpf_anc_helper(ftest) & BPF_ANC)
1116 /* Ancillary operation unknown or unsupported */
1117 if (anc_found == false && ftest->k >= SKF_AD_OFF)
1122 /* Last instruction must be a RET code */
1123 switch (filter[flen - 1].code) {
1124 case BPF_RET | BPF_K:
1125 case BPF_RET | BPF_A:
1126 return check_load_and_stores(filter, flen);
1132 static int bpf_prog_store_orig_filter(struct bpf_prog *fp,
1133 const struct sock_fprog *fprog)
1135 unsigned int fsize = bpf_classic_proglen(fprog);
1136 struct sock_fprog_kern *fkprog;
1138 fp->orig_prog = kmalloc(sizeof(*fkprog), GFP_KERNEL);
1142 fkprog = fp->orig_prog;
1143 fkprog->len = fprog->len;
1145 fkprog->filter = kmemdup(fp->insns, fsize,
1146 GFP_KERNEL | __GFP_NOWARN);
1147 if (!fkprog->filter) {
1148 kfree(fp->orig_prog);
1155 static void bpf_release_orig_filter(struct bpf_prog *fp)
1157 struct sock_fprog_kern *fprog = fp->orig_prog;
1160 kfree(fprog->filter);
1165 static void __bpf_prog_release(struct bpf_prog *prog)
1167 if (prog->type == BPF_PROG_TYPE_SOCKET_FILTER) {
1170 bpf_release_orig_filter(prog);
1171 bpf_prog_free(prog);
1175 static void __sk_filter_release(struct sk_filter *fp)
1177 __bpf_prog_release(fp->prog);
1182 * sk_filter_release_rcu - Release a socket filter by rcu_head
1183 * @rcu: rcu_head that contains the sk_filter to free
1185 static void sk_filter_release_rcu(struct rcu_head *rcu)
1187 struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu);
1189 __sk_filter_release(fp);
1193 * sk_filter_release - release a socket filter
1194 * @fp: filter to remove
1196 * Remove a filter from a socket and release its resources.
1198 static void sk_filter_release(struct sk_filter *fp)
1200 if (refcount_dec_and_test(&fp->refcnt))
1201 call_rcu(&fp->rcu, sk_filter_release_rcu);
1204 void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp)
1206 u32 filter_size = bpf_prog_size(fp->prog->len);
1208 atomic_sub(filter_size, &sk->sk_omem_alloc);
1209 sk_filter_release(fp);
1212 /* try to charge the socket memory if there is space available
1213 * return true on success
1215 static bool __sk_filter_charge(struct sock *sk, struct sk_filter *fp)
1217 u32 filter_size = bpf_prog_size(fp->prog->len);
1218 int optmem_max = READ_ONCE(sysctl_optmem_max);
1220 /* same check as in sock_kmalloc() */
1221 if (filter_size <= optmem_max &&
1222 atomic_read(&sk->sk_omem_alloc) + filter_size < optmem_max) {
1223 atomic_add(filter_size, &sk->sk_omem_alloc);
1229 bool sk_filter_charge(struct sock *sk, struct sk_filter *fp)
1231 if (!refcount_inc_not_zero(&fp->refcnt))
1234 if (!__sk_filter_charge(sk, fp)) {
1235 sk_filter_release(fp);
1241 static struct bpf_prog *bpf_migrate_filter(struct bpf_prog *fp)
1243 struct sock_filter *old_prog;
1244 struct bpf_prog *old_fp;
1245 int err, new_len, old_len = fp->len;
1246 bool seen_ld_abs = false;
1248 /* We are free to overwrite insns et al right here as it won't be used at
1249 * this point in time anymore internally after the migration to the eBPF
1250 * instruction representation.
1252 BUILD_BUG_ON(sizeof(struct sock_filter) !=
1253 sizeof(struct bpf_insn));
1255 /* Conversion cannot happen on overlapping memory areas,
1256 * so we need to keep the user BPF around until the 2nd
1257 * pass. At this time, the user BPF is stored in fp->insns.
1259 old_prog = kmemdup(fp->insns, old_len * sizeof(struct sock_filter),
1260 GFP_KERNEL | __GFP_NOWARN);
1266 /* 1st pass: calculate the new program length. */
1267 err = bpf_convert_filter(old_prog, old_len, NULL, &new_len,
1272 /* Expand fp for appending the new filter representation. */
1274 fp = bpf_prog_realloc(old_fp, bpf_prog_size(new_len), 0);
1276 /* The old_fp is still around in case we couldn't
1277 * allocate new memory, so uncharge on that one.
1286 /* 2nd pass: remap sock_filter insns into bpf_insn insns. */
1287 err = bpf_convert_filter(old_prog, old_len, fp, &new_len,
1290 /* 2nd bpf_convert_filter() can fail only if it fails
1291 * to allocate memory, remapping must succeed. Note,
1292 * that at this time old_fp has already been released
1297 fp = bpf_prog_select_runtime(fp, &err);
1307 __bpf_prog_release(fp);
1308 return ERR_PTR(err);
1311 static struct bpf_prog *bpf_prepare_filter(struct bpf_prog *fp,
1312 bpf_aux_classic_check_t trans)
1316 fp->bpf_func = NULL;
1319 err = bpf_check_classic(fp->insns, fp->len);
1321 __bpf_prog_release(fp);
1322 return ERR_PTR(err);
1325 /* There might be additional checks and transformations
1326 * needed on classic filters, f.e. in case of seccomp.
1329 err = trans(fp->insns, fp->len);
1331 __bpf_prog_release(fp);
1332 return ERR_PTR(err);
1336 /* Probe if we can JIT compile the filter and if so, do
1337 * the compilation of the filter.
1339 bpf_jit_compile(fp);
1341 /* JIT compiler couldn't process this filter, so do the eBPF translation
1342 * for the optimized interpreter.
1345 fp = bpf_migrate_filter(fp);
1351 * bpf_prog_create - create an unattached filter
1352 * @pfp: the unattached filter that is created
1353 * @fprog: the filter program
1355 * Create a filter independent of any socket. We first run some
1356 * sanity checks on it to make sure it does not explode on us later.
1357 * If an error occurs or there is insufficient memory for the filter
1358 * a negative errno code is returned. On success the return is zero.
1360 int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog)
1362 unsigned int fsize = bpf_classic_proglen(fprog);
1363 struct bpf_prog *fp;
1365 /* Make sure new filter is there and in the right amounts. */
1366 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1369 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1373 memcpy(fp->insns, fprog->filter, fsize);
1375 fp->len = fprog->len;
1376 /* Since unattached filters are not copied back to user
1377 * space through sk_get_filter(), we do not need to hold
1378 * a copy here, and can spare us the work.
1380 fp->orig_prog = NULL;
1382 /* bpf_prepare_filter() already takes care of freeing
1383 * memory in case something goes wrong.
1385 fp = bpf_prepare_filter(fp, NULL);
1392 EXPORT_SYMBOL_GPL(bpf_prog_create);
1395 * bpf_prog_create_from_user - create an unattached filter from user buffer
1396 * @pfp: the unattached filter that is created
1397 * @fprog: the filter program
1398 * @trans: post-classic verifier transformation handler
1399 * @save_orig: save classic BPF program
1401 * This function effectively does the same as bpf_prog_create(), only
1402 * that it builds up its insns buffer from user space provided buffer.
1403 * It also allows for passing a bpf_aux_classic_check_t handler.
1405 int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog,
1406 bpf_aux_classic_check_t trans, bool save_orig)
1408 unsigned int fsize = bpf_classic_proglen(fprog);
1409 struct bpf_prog *fp;
1412 /* Make sure new filter is there and in the right amounts. */
1413 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1416 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1420 if (copy_from_user(fp->insns, fprog->filter, fsize)) {
1421 __bpf_prog_free(fp);
1425 fp->len = fprog->len;
1426 fp->orig_prog = NULL;
1429 err = bpf_prog_store_orig_filter(fp, fprog);
1431 __bpf_prog_free(fp);
1436 /* bpf_prepare_filter() already takes care of freeing
1437 * memory in case something goes wrong.
1439 fp = bpf_prepare_filter(fp, trans);
1446 EXPORT_SYMBOL_GPL(bpf_prog_create_from_user);
1448 void bpf_prog_destroy(struct bpf_prog *fp)
1450 __bpf_prog_release(fp);
1452 EXPORT_SYMBOL_GPL(bpf_prog_destroy);
1454 static int __sk_attach_prog(struct bpf_prog *prog, struct sock *sk)
1456 struct sk_filter *fp, *old_fp;
1458 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
1464 if (!__sk_filter_charge(sk, fp)) {
1468 refcount_set(&fp->refcnt, 1);
1470 old_fp = rcu_dereference_protected(sk->sk_filter,
1471 lockdep_sock_is_held(sk));
1472 rcu_assign_pointer(sk->sk_filter, fp);
1475 sk_filter_uncharge(sk, old_fp);
1481 struct bpf_prog *__get_filter(struct sock_fprog *fprog, struct sock *sk)
1483 unsigned int fsize = bpf_classic_proglen(fprog);
1484 struct bpf_prog *prog;
1487 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1488 return ERR_PTR(-EPERM);
1490 /* Make sure new filter is there and in the right amounts. */
1491 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1492 return ERR_PTR(-EINVAL);
1494 prog = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1496 return ERR_PTR(-ENOMEM);
1498 if (copy_from_user(prog->insns, fprog->filter, fsize)) {
1499 __bpf_prog_free(prog);
1500 return ERR_PTR(-EFAULT);
1503 prog->len = fprog->len;
1505 err = bpf_prog_store_orig_filter(prog, fprog);
1507 __bpf_prog_free(prog);
1508 return ERR_PTR(-ENOMEM);
1511 /* bpf_prepare_filter() already takes care of freeing
1512 * memory in case something goes wrong.
1514 return bpf_prepare_filter(prog, NULL);
1518 * sk_attach_filter - attach a socket filter
1519 * @fprog: the filter program
1520 * @sk: the socket to use
1522 * Attach the user's filter code. We first run some sanity checks on
1523 * it to make sure it does not explode on us later. If an error
1524 * occurs or there is insufficient memory for the filter a negative
1525 * errno code is returned. On success the return is zero.
1527 int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1529 struct bpf_prog *prog = __get_filter(fprog, sk);
1533 return PTR_ERR(prog);
1535 err = __sk_attach_prog(prog, sk);
1537 __bpf_prog_release(prog);
1543 EXPORT_SYMBOL_GPL(sk_attach_filter);
1545 int sk_reuseport_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1547 struct bpf_prog *prog = __get_filter(fprog, sk);
1551 return PTR_ERR(prog);
1553 if (bpf_prog_size(prog->len) > READ_ONCE(sysctl_optmem_max))
1556 err = reuseport_attach_prog(sk, prog);
1559 __bpf_prog_release(prog);
1564 static struct bpf_prog *__get_bpf(u32 ufd, struct sock *sk)
1566 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1567 return ERR_PTR(-EPERM);
1569 return bpf_prog_get_type(ufd, BPF_PROG_TYPE_SOCKET_FILTER);
1572 int sk_attach_bpf(u32 ufd, struct sock *sk)
1574 struct bpf_prog *prog = __get_bpf(ufd, sk);
1578 return PTR_ERR(prog);
1580 err = __sk_attach_prog(prog, sk);
1589 int sk_reuseport_attach_bpf(u32 ufd, struct sock *sk)
1591 struct bpf_prog *prog;
1594 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1597 prog = bpf_prog_get_type(ufd, BPF_PROG_TYPE_SOCKET_FILTER);
1598 if (PTR_ERR(prog) == -EINVAL)
1599 prog = bpf_prog_get_type(ufd, BPF_PROG_TYPE_SK_REUSEPORT);
1601 return PTR_ERR(prog);
1603 if (prog->type == BPF_PROG_TYPE_SK_REUSEPORT) {
1604 /* Like other non BPF_PROG_TYPE_SOCKET_FILTER
1605 * bpf prog (e.g. sockmap). It depends on the
1606 * limitation imposed by bpf_prog_load().
1607 * Hence, sysctl_optmem_max is not checked.
1609 if ((sk->sk_type != SOCK_STREAM &&
1610 sk->sk_type != SOCK_DGRAM) ||
1611 (sk->sk_protocol != IPPROTO_UDP &&
1612 sk->sk_protocol != IPPROTO_TCP) ||
1613 (sk->sk_family != AF_INET &&
1614 sk->sk_family != AF_INET6)) {
1619 /* BPF_PROG_TYPE_SOCKET_FILTER */
1620 if (bpf_prog_size(prog->len) > READ_ONCE(sysctl_optmem_max)) {
1626 err = reuseport_attach_prog(sk, prog);
1634 void sk_reuseport_prog_free(struct bpf_prog *prog)
1639 if (prog->type == BPF_PROG_TYPE_SK_REUSEPORT)
1642 bpf_prog_destroy(prog);
1645 struct bpf_scratchpad {
1647 __be32 diff[MAX_BPF_STACK / sizeof(__be32)];
1648 u8 buff[MAX_BPF_STACK];
1652 static DEFINE_PER_CPU(struct bpf_scratchpad, bpf_sp);
1654 static inline int __bpf_try_make_writable(struct sk_buff *skb,
1655 unsigned int write_len)
1657 return skb_ensure_writable(skb, write_len);
1660 static inline int bpf_try_make_writable(struct sk_buff *skb,
1661 unsigned int write_len)
1663 int err = __bpf_try_make_writable(skb, write_len);
1665 bpf_compute_data_pointers(skb);
1669 static int bpf_try_make_head_writable(struct sk_buff *skb)
1671 return bpf_try_make_writable(skb, skb_headlen(skb));
1674 static inline void bpf_push_mac_rcsum(struct sk_buff *skb)
1676 if (skb_at_tc_ingress(skb))
1677 skb_postpush_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1680 static inline void bpf_pull_mac_rcsum(struct sk_buff *skb)
1682 if (skb_at_tc_ingress(skb))
1683 skb_postpull_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1686 BPF_CALL_5(bpf_skb_store_bytes, struct sk_buff *, skb, u32, offset,
1687 const void *, from, u32, len, u64, flags)
1691 if (unlikely(flags & ~(BPF_F_RECOMPUTE_CSUM | BPF_F_INVALIDATE_HASH)))
1693 if (unlikely(offset > INT_MAX))
1695 if (unlikely(bpf_try_make_writable(skb, offset + len)))
1698 ptr = skb->data + offset;
1699 if (flags & BPF_F_RECOMPUTE_CSUM)
1700 __skb_postpull_rcsum(skb, ptr, len, offset);
1702 memcpy(ptr, from, len);
1704 if (flags & BPF_F_RECOMPUTE_CSUM)
1705 __skb_postpush_rcsum(skb, ptr, len, offset);
1706 if (flags & BPF_F_INVALIDATE_HASH)
1707 skb_clear_hash(skb);
1712 static const struct bpf_func_proto bpf_skb_store_bytes_proto = {
1713 .func = bpf_skb_store_bytes,
1715 .ret_type = RET_INTEGER,
1716 .arg1_type = ARG_PTR_TO_CTX,
1717 .arg2_type = ARG_ANYTHING,
1718 .arg3_type = ARG_PTR_TO_MEM | MEM_RDONLY,
1719 .arg4_type = ARG_CONST_SIZE,
1720 .arg5_type = ARG_ANYTHING,
1723 BPF_CALL_4(bpf_skb_load_bytes, const struct sk_buff *, skb, u32, offset,
1724 void *, to, u32, len)
1728 if (unlikely(offset > INT_MAX))
1731 ptr = skb_header_pointer(skb, offset, len, to);
1735 memcpy(to, ptr, len);
1743 static const struct bpf_func_proto bpf_skb_load_bytes_proto = {
1744 .func = bpf_skb_load_bytes,
1746 .ret_type = RET_INTEGER,
1747 .arg1_type = ARG_PTR_TO_CTX,
1748 .arg2_type = ARG_ANYTHING,
1749 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
1750 .arg4_type = ARG_CONST_SIZE,
1753 BPF_CALL_4(bpf_flow_dissector_load_bytes,
1754 const struct bpf_flow_dissector *, ctx, u32, offset,
1755 void *, to, u32, len)
1759 if (unlikely(offset > 0xffff))
1762 if (unlikely(!ctx->skb))
1765 ptr = skb_header_pointer(ctx->skb, offset, len, to);
1769 memcpy(to, ptr, len);
1777 static const struct bpf_func_proto bpf_flow_dissector_load_bytes_proto = {
1778 .func = bpf_flow_dissector_load_bytes,
1780 .ret_type = RET_INTEGER,
1781 .arg1_type = ARG_PTR_TO_CTX,
1782 .arg2_type = ARG_ANYTHING,
1783 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
1784 .arg4_type = ARG_CONST_SIZE,
1787 BPF_CALL_5(bpf_skb_load_bytes_relative, const struct sk_buff *, skb,
1788 u32, offset, void *, to, u32, len, u32, start_header)
1790 u8 *end = skb_tail_pointer(skb);
1793 if (unlikely(offset > 0xffff))
1796 switch (start_header) {
1797 case BPF_HDR_START_MAC:
1798 if (unlikely(!skb_mac_header_was_set(skb)))
1800 start = skb_mac_header(skb);
1802 case BPF_HDR_START_NET:
1803 start = skb_network_header(skb);
1809 ptr = start + offset;
1811 if (likely(ptr + len <= end)) {
1812 memcpy(to, ptr, len);
1821 static const struct bpf_func_proto bpf_skb_load_bytes_relative_proto = {
1822 .func = bpf_skb_load_bytes_relative,
1824 .ret_type = RET_INTEGER,
1825 .arg1_type = ARG_PTR_TO_CTX,
1826 .arg2_type = ARG_ANYTHING,
1827 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
1828 .arg4_type = ARG_CONST_SIZE,
1829 .arg5_type = ARG_ANYTHING,
1832 BPF_CALL_2(bpf_skb_pull_data, struct sk_buff *, skb, u32, len)
1834 /* Idea is the following: should the needed direct read/write
1835 * test fail during runtime, we can pull in more data and redo
1836 * again, since implicitly, we invalidate previous checks here.
1838 * Or, since we know how much we need to make read/writeable,
1839 * this can be done once at the program beginning for direct
1840 * access case. By this we overcome limitations of only current
1841 * headroom being accessible.
1843 return bpf_try_make_writable(skb, len ? : skb_headlen(skb));
1846 static const struct bpf_func_proto bpf_skb_pull_data_proto = {
1847 .func = bpf_skb_pull_data,
1849 .ret_type = RET_INTEGER,
1850 .arg1_type = ARG_PTR_TO_CTX,
1851 .arg2_type = ARG_ANYTHING,
1854 BPF_CALL_1(bpf_sk_fullsock, struct sock *, sk)
1856 return sk_fullsock(sk) ? (unsigned long)sk : (unsigned long)NULL;
1859 static const struct bpf_func_proto bpf_sk_fullsock_proto = {
1860 .func = bpf_sk_fullsock,
1862 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
1863 .arg1_type = ARG_PTR_TO_SOCK_COMMON,
1866 static inline int sk_skb_try_make_writable(struct sk_buff *skb,
1867 unsigned int write_len)
1869 return __bpf_try_make_writable(skb, write_len);
1872 BPF_CALL_2(sk_skb_pull_data, struct sk_buff *, skb, u32, len)
1874 /* Idea is the following: should the needed direct read/write
1875 * test fail during runtime, we can pull in more data and redo
1876 * again, since implicitly, we invalidate previous checks here.
1878 * Or, since we know how much we need to make read/writeable,
1879 * this can be done once at the program beginning for direct
1880 * access case. By this we overcome limitations of only current
1881 * headroom being accessible.
1883 return sk_skb_try_make_writable(skb, len ? : skb_headlen(skb));
1886 static const struct bpf_func_proto sk_skb_pull_data_proto = {
1887 .func = sk_skb_pull_data,
1889 .ret_type = RET_INTEGER,
1890 .arg1_type = ARG_PTR_TO_CTX,
1891 .arg2_type = ARG_ANYTHING,
1894 BPF_CALL_5(bpf_l3_csum_replace, struct sk_buff *, skb, u32, offset,
1895 u64, from, u64, to, u64, flags)
1899 if (unlikely(flags & ~(BPF_F_HDR_FIELD_MASK)))
1901 if (unlikely(offset > 0xffff || offset & 1))
1903 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
1906 ptr = (__sum16 *)(skb->data + offset);
1907 switch (flags & BPF_F_HDR_FIELD_MASK) {
1909 if (unlikely(from != 0))
1912 csum_replace_by_diff(ptr, to);
1915 csum_replace2(ptr, from, to);
1918 csum_replace4(ptr, from, to);
1927 static const struct bpf_func_proto bpf_l3_csum_replace_proto = {
1928 .func = bpf_l3_csum_replace,
1930 .ret_type = RET_INTEGER,
1931 .arg1_type = ARG_PTR_TO_CTX,
1932 .arg2_type = ARG_ANYTHING,
1933 .arg3_type = ARG_ANYTHING,
1934 .arg4_type = ARG_ANYTHING,
1935 .arg5_type = ARG_ANYTHING,
1938 BPF_CALL_5(bpf_l4_csum_replace, struct sk_buff *, skb, u32, offset,
1939 u64, from, u64, to, u64, flags)
1941 bool is_pseudo = flags & BPF_F_PSEUDO_HDR;
1942 bool is_mmzero = flags & BPF_F_MARK_MANGLED_0;
1943 bool do_mforce = flags & BPF_F_MARK_ENFORCE;
1946 if (unlikely(flags & ~(BPF_F_MARK_MANGLED_0 | BPF_F_MARK_ENFORCE |
1947 BPF_F_PSEUDO_HDR | BPF_F_HDR_FIELD_MASK)))
1949 if (unlikely(offset > 0xffff || offset & 1))
1951 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
1954 ptr = (__sum16 *)(skb->data + offset);
1955 if (is_mmzero && !do_mforce && !*ptr)
1958 switch (flags & BPF_F_HDR_FIELD_MASK) {
1960 if (unlikely(from != 0))
1963 inet_proto_csum_replace_by_diff(ptr, skb, to, is_pseudo);
1966 inet_proto_csum_replace2(ptr, skb, from, to, is_pseudo);
1969 inet_proto_csum_replace4(ptr, skb, from, to, is_pseudo);
1975 if (is_mmzero && !*ptr)
1976 *ptr = CSUM_MANGLED_0;
1980 static const struct bpf_func_proto bpf_l4_csum_replace_proto = {
1981 .func = bpf_l4_csum_replace,
1983 .ret_type = RET_INTEGER,
1984 .arg1_type = ARG_PTR_TO_CTX,
1985 .arg2_type = ARG_ANYTHING,
1986 .arg3_type = ARG_ANYTHING,
1987 .arg4_type = ARG_ANYTHING,
1988 .arg5_type = ARG_ANYTHING,
1991 BPF_CALL_5(bpf_csum_diff, __be32 *, from, u32, from_size,
1992 __be32 *, to, u32, to_size, __wsum, seed)
1994 struct bpf_scratchpad *sp = this_cpu_ptr(&bpf_sp);
1995 u32 diff_size = from_size + to_size;
1998 /* This is quite flexible, some examples:
2000 * from_size == 0, to_size > 0, seed := csum --> pushing data
2001 * from_size > 0, to_size == 0, seed := csum --> pulling data
2002 * from_size > 0, to_size > 0, seed := 0 --> diffing data
2004 * Even for diffing, from_size and to_size don't need to be equal.
2006 if (unlikely(((from_size | to_size) & (sizeof(__be32) - 1)) ||
2007 diff_size > sizeof(sp->diff)))
2010 for (i = 0; i < from_size / sizeof(__be32); i++, j++)
2011 sp->diff[j] = ~from[i];
2012 for (i = 0; i < to_size / sizeof(__be32); i++, j++)
2013 sp->diff[j] = to[i];
2015 return csum_partial(sp->diff, diff_size, seed);
2018 static const struct bpf_func_proto bpf_csum_diff_proto = {
2019 .func = bpf_csum_diff,
2022 .ret_type = RET_INTEGER,
2023 .arg1_type = ARG_PTR_TO_MEM | PTR_MAYBE_NULL | MEM_RDONLY,
2024 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
2025 .arg3_type = ARG_PTR_TO_MEM | PTR_MAYBE_NULL | MEM_RDONLY,
2026 .arg4_type = ARG_CONST_SIZE_OR_ZERO,
2027 .arg5_type = ARG_ANYTHING,
2030 BPF_CALL_2(bpf_csum_update, struct sk_buff *, skb, __wsum, csum)
2032 /* The interface is to be used in combination with bpf_csum_diff()
2033 * for direct packet writes. csum rotation for alignment as well
2034 * as emulating csum_sub() can be done from the eBPF program.
2036 if (skb->ip_summed == CHECKSUM_COMPLETE)
2037 return (skb->csum = csum_add(skb->csum, csum));
2042 static const struct bpf_func_proto bpf_csum_update_proto = {
2043 .func = bpf_csum_update,
2045 .ret_type = RET_INTEGER,
2046 .arg1_type = ARG_PTR_TO_CTX,
2047 .arg2_type = ARG_ANYTHING,
2050 BPF_CALL_2(bpf_csum_level, struct sk_buff *, skb, u64, level)
2052 /* The interface is to be used in combination with bpf_skb_adjust_room()
2053 * for encap/decap of packet headers when BPF_F_ADJ_ROOM_NO_CSUM_RESET
2054 * is passed as flags, for example.
2057 case BPF_CSUM_LEVEL_INC:
2058 __skb_incr_checksum_unnecessary(skb);
2060 case BPF_CSUM_LEVEL_DEC:
2061 __skb_decr_checksum_unnecessary(skb);
2063 case BPF_CSUM_LEVEL_RESET:
2064 __skb_reset_checksum_unnecessary(skb);
2066 case BPF_CSUM_LEVEL_QUERY:
2067 return skb->ip_summed == CHECKSUM_UNNECESSARY ?
2068 skb->csum_level : -EACCES;
2076 static const struct bpf_func_proto bpf_csum_level_proto = {
2077 .func = bpf_csum_level,
2079 .ret_type = RET_INTEGER,
2080 .arg1_type = ARG_PTR_TO_CTX,
2081 .arg2_type = ARG_ANYTHING,
2084 static inline int __bpf_rx_skb(struct net_device *dev, struct sk_buff *skb)
2086 return dev_forward_skb_nomtu(dev, skb);
2089 static inline int __bpf_rx_skb_no_mac(struct net_device *dev,
2090 struct sk_buff *skb)
2092 int ret = ____dev_forward_skb(dev, skb, false);
2096 ret = netif_rx(skb);
2102 static inline int __bpf_tx_skb(struct net_device *dev, struct sk_buff *skb)
2106 if (dev_xmit_recursion()) {
2107 net_crit_ratelimited("bpf: recursion limit reached on datapath, buggy bpf program?\n");
2113 skb_clear_tstamp(skb);
2115 dev_xmit_recursion_inc();
2116 ret = dev_queue_xmit(skb);
2117 dev_xmit_recursion_dec();
2122 static int __bpf_redirect_no_mac(struct sk_buff *skb, struct net_device *dev,
2125 unsigned int mlen = skb_network_offset(skb);
2128 __skb_pull(skb, mlen);
2129 if (unlikely(!skb->len)) {
2134 /* At ingress, the mac header has already been pulled once.
2135 * At egress, skb_pospull_rcsum has to be done in case that
2136 * the skb is originated from ingress (i.e. a forwarded skb)
2137 * to ensure that rcsum starts at net header.
2139 if (!skb_at_tc_ingress(skb))
2140 skb_postpull_rcsum(skb, skb_mac_header(skb), mlen);
2142 skb_pop_mac_header(skb);
2143 skb_reset_mac_len(skb);
2144 return flags & BPF_F_INGRESS ?
2145 __bpf_rx_skb_no_mac(dev, skb) : __bpf_tx_skb(dev, skb);
2148 static int __bpf_redirect_common(struct sk_buff *skb, struct net_device *dev,
2151 /* Verify that a link layer header is carried */
2152 if (unlikely(skb->mac_header >= skb->network_header)) {
2157 bpf_push_mac_rcsum(skb);
2158 return flags & BPF_F_INGRESS ?
2159 __bpf_rx_skb(dev, skb) : __bpf_tx_skb(dev, skb);
2162 static int __bpf_redirect(struct sk_buff *skb, struct net_device *dev,
2165 if (dev_is_mac_header_xmit(dev))
2166 return __bpf_redirect_common(skb, dev, flags);
2168 return __bpf_redirect_no_mac(skb, dev, flags);
2171 #if IS_ENABLED(CONFIG_IPV6)
2172 static int bpf_out_neigh_v6(struct net *net, struct sk_buff *skb,
2173 struct net_device *dev, struct bpf_nh_params *nh)
2175 u32 hh_len = LL_RESERVED_SPACE(dev);
2176 const struct in6_addr *nexthop;
2177 struct dst_entry *dst = NULL;
2178 struct neighbour *neigh;
2180 if (dev_xmit_recursion()) {
2181 net_crit_ratelimited("bpf: recursion limit reached on datapath, buggy bpf program?\n");
2186 skb_clear_tstamp(skb);
2188 if (unlikely(skb_headroom(skb) < hh_len && dev->header_ops)) {
2189 skb = skb_expand_head(skb, hh_len);
2197 nexthop = rt6_nexthop(container_of(dst, struct rt6_info, dst),
2198 &ipv6_hdr(skb)->daddr);
2200 nexthop = &nh->ipv6_nh;
2202 neigh = ip_neigh_gw6(dev, nexthop);
2203 if (likely(!IS_ERR(neigh))) {
2206 sock_confirm_neigh(skb, neigh);
2207 dev_xmit_recursion_inc();
2208 ret = neigh_output(neigh, skb, false);
2209 dev_xmit_recursion_dec();
2210 rcu_read_unlock_bh();
2213 rcu_read_unlock_bh();
2215 IP6_INC_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_OUTNOROUTES);
2221 static int __bpf_redirect_neigh_v6(struct sk_buff *skb, struct net_device *dev,
2222 struct bpf_nh_params *nh)
2224 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
2225 struct net *net = dev_net(dev);
2226 int err, ret = NET_XMIT_DROP;
2229 struct dst_entry *dst;
2230 struct flowi6 fl6 = {
2231 .flowi6_flags = FLOWI_FLAG_ANYSRC,
2232 .flowi6_mark = skb->mark,
2233 .flowlabel = ip6_flowinfo(ip6h),
2234 .flowi6_oif = dev->ifindex,
2235 .flowi6_proto = ip6h->nexthdr,
2236 .daddr = ip6h->daddr,
2237 .saddr = ip6h->saddr,
2240 dst = ipv6_stub->ipv6_dst_lookup_flow(net, NULL, &fl6, NULL);
2244 skb_dst_set(skb, dst);
2245 } else if (nh->nh_family != AF_INET6) {
2249 err = bpf_out_neigh_v6(net, skb, dev, nh);
2250 if (unlikely(net_xmit_eval(err)))
2251 dev->stats.tx_errors++;
2253 ret = NET_XMIT_SUCCESS;
2256 dev->stats.tx_errors++;
2262 static int __bpf_redirect_neigh_v6(struct sk_buff *skb, struct net_device *dev,
2263 struct bpf_nh_params *nh)
2266 return NET_XMIT_DROP;
2268 #endif /* CONFIG_IPV6 */
2270 #if IS_ENABLED(CONFIG_INET)
2271 static int bpf_out_neigh_v4(struct net *net, struct sk_buff *skb,
2272 struct net_device *dev, struct bpf_nh_params *nh)
2274 u32 hh_len = LL_RESERVED_SPACE(dev);
2275 struct neighbour *neigh;
2276 bool is_v6gw = false;
2278 if (dev_xmit_recursion()) {
2279 net_crit_ratelimited("bpf: recursion limit reached on datapath, buggy bpf program?\n");
2284 skb_clear_tstamp(skb);
2286 if (unlikely(skb_headroom(skb) < hh_len && dev->header_ops)) {
2287 skb = skb_expand_head(skb, hh_len);
2294 struct dst_entry *dst = skb_dst(skb);
2295 struct rtable *rt = container_of(dst, struct rtable, dst);
2297 neigh = ip_neigh_for_gw(rt, skb, &is_v6gw);
2298 } else if (nh->nh_family == AF_INET6) {
2299 neigh = ip_neigh_gw6(dev, &nh->ipv6_nh);
2301 } else if (nh->nh_family == AF_INET) {
2302 neigh = ip_neigh_gw4(dev, nh->ipv4_nh);
2304 rcu_read_unlock_bh();
2308 if (likely(!IS_ERR(neigh))) {
2311 sock_confirm_neigh(skb, neigh);
2312 dev_xmit_recursion_inc();
2313 ret = neigh_output(neigh, skb, is_v6gw);
2314 dev_xmit_recursion_dec();
2315 rcu_read_unlock_bh();
2318 rcu_read_unlock_bh();
2324 static int __bpf_redirect_neigh_v4(struct sk_buff *skb, struct net_device *dev,
2325 struct bpf_nh_params *nh)
2327 const struct iphdr *ip4h = ip_hdr(skb);
2328 struct net *net = dev_net(dev);
2329 int err, ret = NET_XMIT_DROP;
2332 struct flowi4 fl4 = {
2333 .flowi4_flags = FLOWI_FLAG_ANYSRC,
2334 .flowi4_mark = skb->mark,
2335 .flowi4_tos = RT_TOS(ip4h->tos),
2336 .flowi4_oif = dev->ifindex,
2337 .flowi4_proto = ip4h->protocol,
2338 .daddr = ip4h->daddr,
2339 .saddr = ip4h->saddr,
2343 rt = ip_route_output_flow(net, &fl4, NULL);
2346 if (rt->rt_type != RTN_UNICAST && rt->rt_type != RTN_LOCAL) {
2351 skb_dst_set(skb, &rt->dst);
2354 err = bpf_out_neigh_v4(net, skb, dev, nh);
2355 if (unlikely(net_xmit_eval(err)))
2356 dev->stats.tx_errors++;
2358 ret = NET_XMIT_SUCCESS;
2361 dev->stats.tx_errors++;
2367 static int __bpf_redirect_neigh_v4(struct sk_buff *skb, struct net_device *dev,
2368 struct bpf_nh_params *nh)
2371 return NET_XMIT_DROP;
2373 #endif /* CONFIG_INET */
2375 static int __bpf_redirect_neigh(struct sk_buff *skb, struct net_device *dev,
2376 struct bpf_nh_params *nh)
2378 struct ethhdr *ethh = eth_hdr(skb);
2380 if (unlikely(skb->mac_header >= skb->network_header))
2382 bpf_push_mac_rcsum(skb);
2383 if (is_multicast_ether_addr(ethh->h_dest))
2386 skb_pull(skb, sizeof(*ethh));
2387 skb_unset_mac_header(skb);
2388 skb_reset_network_header(skb);
2390 if (skb->protocol == htons(ETH_P_IP))
2391 return __bpf_redirect_neigh_v4(skb, dev, nh);
2392 else if (skb->protocol == htons(ETH_P_IPV6))
2393 return __bpf_redirect_neigh_v6(skb, dev, nh);
2399 /* Internal, non-exposed redirect flags. */
2401 BPF_F_NEIGH = (1ULL << 1),
2402 BPF_F_PEER = (1ULL << 2),
2403 BPF_F_NEXTHOP = (1ULL << 3),
2404 #define BPF_F_REDIRECT_INTERNAL (BPF_F_NEIGH | BPF_F_PEER | BPF_F_NEXTHOP)
2407 BPF_CALL_3(bpf_clone_redirect, struct sk_buff *, skb, u32, ifindex, u64, flags)
2409 struct net_device *dev;
2410 struct sk_buff *clone;
2413 if (unlikely(flags & (~(BPF_F_INGRESS) | BPF_F_REDIRECT_INTERNAL)))
2416 dev = dev_get_by_index_rcu(dev_net(skb->dev), ifindex);
2420 clone = skb_clone(skb, GFP_ATOMIC);
2421 if (unlikely(!clone))
2424 /* For direct write, we need to keep the invariant that the skbs
2425 * we're dealing with need to be uncloned. Should uncloning fail
2426 * here, we need to free the just generated clone to unclone once
2429 ret = bpf_try_make_head_writable(skb);
2430 if (unlikely(ret)) {
2435 return __bpf_redirect(clone, dev, flags);
2438 static const struct bpf_func_proto bpf_clone_redirect_proto = {
2439 .func = bpf_clone_redirect,
2441 .ret_type = RET_INTEGER,
2442 .arg1_type = ARG_PTR_TO_CTX,
2443 .arg2_type = ARG_ANYTHING,
2444 .arg3_type = ARG_ANYTHING,
2447 DEFINE_PER_CPU(struct bpf_redirect_info, bpf_redirect_info);
2448 EXPORT_PER_CPU_SYMBOL_GPL(bpf_redirect_info);
2450 int skb_do_redirect(struct sk_buff *skb)
2452 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
2453 struct net *net = dev_net(skb->dev);
2454 struct net_device *dev;
2455 u32 flags = ri->flags;
2457 dev = dev_get_by_index_rcu(net, ri->tgt_index);
2462 if (flags & BPF_F_PEER) {
2463 const struct net_device_ops *ops = dev->netdev_ops;
2465 if (unlikely(!ops->ndo_get_peer_dev ||
2466 !skb_at_tc_ingress(skb)))
2468 dev = ops->ndo_get_peer_dev(dev);
2469 if (unlikely(!dev ||
2470 !(dev->flags & IFF_UP) ||
2471 net_eq(net, dev_net(dev))))
2476 return flags & BPF_F_NEIGH ?
2477 __bpf_redirect_neigh(skb, dev, flags & BPF_F_NEXTHOP ?
2479 __bpf_redirect(skb, dev, flags);
2485 BPF_CALL_2(bpf_redirect, u32, ifindex, u64, flags)
2487 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
2489 if (unlikely(flags & (~(BPF_F_INGRESS) | BPF_F_REDIRECT_INTERNAL)))
2493 ri->tgt_index = ifindex;
2495 return TC_ACT_REDIRECT;
2498 static const struct bpf_func_proto bpf_redirect_proto = {
2499 .func = bpf_redirect,
2501 .ret_type = RET_INTEGER,
2502 .arg1_type = ARG_ANYTHING,
2503 .arg2_type = ARG_ANYTHING,
2506 BPF_CALL_2(bpf_redirect_peer, u32, ifindex, u64, flags)
2508 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
2510 if (unlikely(flags))
2513 ri->flags = BPF_F_PEER;
2514 ri->tgt_index = ifindex;
2516 return TC_ACT_REDIRECT;
2519 static const struct bpf_func_proto bpf_redirect_peer_proto = {
2520 .func = bpf_redirect_peer,
2522 .ret_type = RET_INTEGER,
2523 .arg1_type = ARG_ANYTHING,
2524 .arg2_type = ARG_ANYTHING,
2527 BPF_CALL_4(bpf_redirect_neigh, u32, ifindex, struct bpf_redir_neigh *, params,
2528 int, plen, u64, flags)
2530 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
2532 if (unlikely((plen && plen < sizeof(*params)) || flags))
2535 ri->flags = BPF_F_NEIGH | (plen ? BPF_F_NEXTHOP : 0);
2536 ri->tgt_index = ifindex;
2538 BUILD_BUG_ON(sizeof(struct bpf_redir_neigh) != sizeof(struct bpf_nh_params));
2540 memcpy(&ri->nh, params, sizeof(ri->nh));
2542 return TC_ACT_REDIRECT;
2545 static const struct bpf_func_proto bpf_redirect_neigh_proto = {
2546 .func = bpf_redirect_neigh,
2548 .ret_type = RET_INTEGER,
2549 .arg1_type = ARG_ANYTHING,
2550 .arg2_type = ARG_PTR_TO_MEM | PTR_MAYBE_NULL | MEM_RDONLY,
2551 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
2552 .arg4_type = ARG_ANYTHING,
2555 BPF_CALL_2(bpf_msg_apply_bytes, struct sk_msg *, msg, u32, bytes)
2557 msg->apply_bytes = bytes;
2561 static const struct bpf_func_proto bpf_msg_apply_bytes_proto = {
2562 .func = bpf_msg_apply_bytes,
2564 .ret_type = RET_INTEGER,
2565 .arg1_type = ARG_PTR_TO_CTX,
2566 .arg2_type = ARG_ANYTHING,
2569 BPF_CALL_2(bpf_msg_cork_bytes, struct sk_msg *, msg, u32, bytes)
2571 msg->cork_bytes = bytes;
2575 static const struct bpf_func_proto bpf_msg_cork_bytes_proto = {
2576 .func = bpf_msg_cork_bytes,
2578 .ret_type = RET_INTEGER,
2579 .arg1_type = ARG_PTR_TO_CTX,
2580 .arg2_type = ARG_ANYTHING,
2583 BPF_CALL_4(bpf_msg_pull_data, struct sk_msg *, msg, u32, start,
2584 u32, end, u64, flags)
2586 u32 len = 0, offset = 0, copy = 0, poffset = 0, bytes = end - start;
2587 u32 first_sge, last_sge, i, shift, bytes_sg_total;
2588 struct scatterlist *sge;
2589 u8 *raw, *to, *from;
2592 if (unlikely(flags || end <= start))
2595 /* First find the starting scatterlist element */
2599 len = sk_msg_elem(msg, i)->length;
2600 if (start < offset + len)
2602 sk_msg_iter_var_next(i);
2603 } while (i != msg->sg.end);
2605 if (unlikely(start >= offset + len))
2609 /* The start may point into the sg element so we need to also
2610 * account for the headroom.
2612 bytes_sg_total = start - offset + bytes;
2613 if (!test_bit(i, msg->sg.copy) && bytes_sg_total <= len)
2616 /* At this point we need to linearize multiple scatterlist
2617 * elements or a single shared page. Either way we need to
2618 * copy into a linear buffer exclusively owned by BPF. Then
2619 * place the buffer in the scatterlist and fixup the original
2620 * entries by removing the entries now in the linear buffer
2621 * and shifting the remaining entries. For now we do not try
2622 * to copy partial entries to avoid complexity of running out
2623 * of sg_entry slots. The downside is reading a single byte
2624 * will copy the entire sg entry.
2627 copy += sk_msg_elem(msg, i)->length;
2628 sk_msg_iter_var_next(i);
2629 if (bytes_sg_total <= copy)
2631 } while (i != msg->sg.end);
2634 if (unlikely(bytes_sg_total > copy))
2637 page = alloc_pages(__GFP_NOWARN | GFP_ATOMIC | __GFP_COMP,
2639 if (unlikely(!page))
2642 raw = page_address(page);
2645 sge = sk_msg_elem(msg, i);
2646 from = sg_virt(sge);
2650 memcpy(to, from, len);
2653 put_page(sg_page(sge));
2655 sk_msg_iter_var_next(i);
2656 } while (i != last_sge);
2658 sg_set_page(&msg->sg.data[first_sge], page, copy, 0);
2660 /* To repair sg ring we need to shift entries. If we only
2661 * had a single entry though we can just replace it and
2662 * be done. Otherwise walk the ring and shift the entries.
2664 WARN_ON_ONCE(last_sge == first_sge);
2665 shift = last_sge > first_sge ?
2666 last_sge - first_sge - 1 :
2667 NR_MSG_FRAG_IDS - first_sge + last_sge - 1;
2672 sk_msg_iter_var_next(i);
2676 if (i + shift >= NR_MSG_FRAG_IDS)
2677 move_from = i + shift - NR_MSG_FRAG_IDS;
2679 move_from = i + shift;
2680 if (move_from == msg->sg.end)
2683 msg->sg.data[i] = msg->sg.data[move_from];
2684 msg->sg.data[move_from].length = 0;
2685 msg->sg.data[move_from].page_link = 0;
2686 msg->sg.data[move_from].offset = 0;
2687 sk_msg_iter_var_next(i);
2690 msg->sg.end = msg->sg.end - shift > msg->sg.end ?
2691 msg->sg.end - shift + NR_MSG_FRAG_IDS :
2692 msg->sg.end - shift;
2694 msg->data = sg_virt(&msg->sg.data[first_sge]) + start - offset;
2695 msg->data_end = msg->data + bytes;
2699 static const struct bpf_func_proto bpf_msg_pull_data_proto = {
2700 .func = bpf_msg_pull_data,
2702 .ret_type = RET_INTEGER,
2703 .arg1_type = ARG_PTR_TO_CTX,
2704 .arg2_type = ARG_ANYTHING,
2705 .arg3_type = ARG_ANYTHING,
2706 .arg4_type = ARG_ANYTHING,
2709 BPF_CALL_4(bpf_msg_push_data, struct sk_msg *, msg, u32, start,
2710 u32, len, u64, flags)
2712 struct scatterlist sge, nsge, nnsge, rsge = {0}, *psge;
2713 u32 new, i = 0, l = 0, space, copy = 0, offset = 0;
2714 u8 *raw, *to, *from;
2717 if (unlikely(flags))
2720 if (unlikely(len == 0))
2723 /* First find the starting scatterlist element */
2727 l = sk_msg_elem(msg, i)->length;
2729 if (start < offset + l)
2731 sk_msg_iter_var_next(i);
2732 } while (i != msg->sg.end);
2734 if (start >= offset + l)
2737 space = MAX_MSG_FRAGS - sk_msg_elem_used(msg);
2739 /* If no space available will fallback to copy, we need at
2740 * least one scatterlist elem available to push data into
2741 * when start aligns to the beginning of an element or two
2742 * when it falls inside an element. We handle the start equals
2743 * offset case because its the common case for inserting a
2746 if (!space || (space == 1 && start != offset))
2747 copy = msg->sg.data[i].length;
2749 page = alloc_pages(__GFP_NOWARN | GFP_ATOMIC | __GFP_COMP,
2750 get_order(copy + len));
2751 if (unlikely(!page))
2757 raw = page_address(page);
2759 psge = sk_msg_elem(msg, i);
2760 front = start - offset;
2761 back = psge->length - front;
2762 from = sg_virt(psge);
2765 memcpy(raw, from, front);
2769 to = raw + front + len;
2771 memcpy(to, from, back);
2774 put_page(sg_page(psge));
2775 } else if (start - offset) {
2776 psge = sk_msg_elem(msg, i);
2777 rsge = sk_msg_elem_cpy(msg, i);
2779 psge->length = start - offset;
2780 rsge.length -= psge->length;
2781 rsge.offset += start;
2783 sk_msg_iter_var_next(i);
2784 sg_unmark_end(psge);
2785 sg_unmark_end(&rsge);
2786 sk_msg_iter_next(msg, end);
2789 /* Slot(s) to place newly allocated data */
2792 /* Shift one or two slots as needed */
2794 sge = sk_msg_elem_cpy(msg, i);
2796 sk_msg_iter_var_next(i);
2797 sg_unmark_end(&sge);
2798 sk_msg_iter_next(msg, end);
2800 nsge = sk_msg_elem_cpy(msg, i);
2802 sk_msg_iter_var_next(i);
2803 nnsge = sk_msg_elem_cpy(msg, i);
2806 while (i != msg->sg.end) {
2807 msg->sg.data[i] = sge;
2809 sk_msg_iter_var_next(i);
2812 nnsge = sk_msg_elem_cpy(msg, i);
2814 nsge = sk_msg_elem_cpy(msg, i);
2819 /* Place newly allocated data buffer */
2820 sk_mem_charge(msg->sk, len);
2821 msg->sg.size += len;
2822 __clear_bit(new, msg->sg.copy);
2823 sg_set_page(&msg->sg.data[new], page, len + copy, 0);
2825 get_page(sg_page(&rsge));
2826 sk_msg_iter_var_next(new);
2827 msg->sg.data[new] = rsge;
2830 sk_msg_compute_data_pointers(msg);
2834 static const struct bpf_func_proto bpf_msg_push_data_proto = {
2835 .func = bpf_msg_push_data,
2837 .ret_type = RET_INTEGER,
2838 .arg1_type = ARG_PTR_TO_CTX,
2839 .arg2_type = ARG_ANYTHING,
2840 .arg3_type = ARG_ANYTHING,
2841 .arg4_type = ARG_ANYTHING,
2844 static void sk_msg_shift_left(struct sk_msg *msg, int i)
2850 sk_msg_iter_var_next(i);
2851 msg->sg.data[prev] = msg->sg.data[i];
2852 } while (i != msg->sg.end);
2854 sk_msg_iter_prev(msg, end);
2857 static void sk_msg_shift_right(struct sk_msg *msg, int i)
2859 struct scatterlist tmp, sge;
2861 sk_msg_iter_next(msg, end);
2862 sge = sk_msg_elem_cpy(msg, i);
2863 sk_msg_iter_var_next(i);
2864 tmp = sk_msg_elem_cpy(msg, i);
2866 while (i != msg->sg.end) {
2867 msg->sg.data[i] = sge;
2868 sk_msg_iter_var_next(i);
2870 tmp = sk_msg_elem_cpy(msg, i);
2874 BPF_CALL_4(bpf_msg_pop_data, struct sk_msg *, msg, u32, start,
2875 u32, len, u64, flags)
2877 u32 i = 0, l = 0, space, offset = 0;
2878 u64 last = start + len;
2881 if (unlikely(flags))
2884 /* First find the starting scatterlist element */
2888 l = sk_msg_elem(msg, i)->length;
2890 if (start < offset + l)
2892 sk_msg_iter_var_next(i);
2893 } while (i != msg->sg.end);
2895 /* Bounds checks: start and pop must be inside message */
2896 if (start >= offset + l || last >= msg->sg.size)
2899 space = MAX_MSG_FRAGS - sk_msg_elem_used(msg);
2902 /* --------------| offset
2903 * -| start |-------- len -------|
2905 * |----- a ----|-------- pop -------|----- b ----|
2906 * |______________________________________________| length
2909 * a: region at front of scatter element to save
2910 * b: region at back of scatter element to save when length > A + pop
2911 * pop: region to pop from element, same as input 'pop' here will be
2912 * decremented below per iteration.
2914 * Two top-level cases to handle when start != offset, first B is non
2915 * zero and second B is zero corresponding to when a pop includes more
2918 * Then if B is non-zero AND there is no space allocate space and
2919 * compact A, B regions into page. If there is space shift ring to
2920 * the rigth free'ing the next element in ring to place B, leaving
2921 * A untouched except to reduce length.
2923 if (start != offset) {
2924 struct scatterlist *nsge, *sge = sk_msg_elem(msg, i);
2926 int b = sge->length - pop - a;
2928 sk_msg_iter_var_next(i);
2930 if (pop < sge->length - a) {
2933 sk_msg_shift_right(msg, i);
2934 nsge = sk_msg_elem(msg, i);
2935 get_page(sg_page(sge));
2938 b, sge->offset + pop + a);
2940 struct page *page, *orig;
2943 page = alloc_pages(__GFP_NOWARN |
2944 __GFP_COMP | GFP_ATOMIC,
2946 if (unlikely(!page))
2950 orig = sg_page(sge);
2951 from = sg_virt(sge);
2952 to = page_address(page);
2953 memcpy(to, from, a);
2954 memcpy(to + a, from + a + pop, b);
2955 sg_set_page(sge, page, a + b, 0);
2959 } else if (pop >= sge->length - a) {
2960 pop -= (sge->length - a);
2965 /* From above the current layout _must_ be as follows,
2970 * |---- pop ---|---------------- b ------------|
2971 * |____________________________________________| length
2973 * Offset and start of the current msg elem are equal because in the
2974 * previous case we handled offset != start and either consumed the
2975 * entire element and advanced to the next element OR pop == 0.
2977 * Two cases to handle here are first pop is less than the length
2978 * leaving some remainder b above. Simply adjust the element's layout
2979 * in this case. Or pop >= length of the element so that b = 0. In this
2980 * case advance to next element decrementing pop.
2983 struct scatterlist *sge = sk_msg_elem(msg, i);
2985 if (pop < sge->length) {
2991 sk_msg_shift_left(msg, i);
2993 sk_msg_iter_var_next(i);
2996 sk_mem_uncharge(msg->sk, len - pop);
2997 msg->sg.size -= (len - pop);
2998 sk_msg_compute_data_pointers(msg);
3002 static const struct bpf_func_proto bpf_msg_pop_data_proto = {
3003 .func = bpf_msg_pop_data,
3005 .ret_type = RET_INTEGER,
3006 .arg1_type = ARG_PTR_TO_CTX,
3007 .arg2_type = ARG_ANYTHING,
3008 .arg3_type = ARG_ANYTHING,
3009 .arg4_type = ARG_ANYTHING,
3012 #ifdef CONFIG_CGROUP_NET_CLASSID
3013 BPF_CALL_0(bpf_get_cgroup_classid_curr)
3015 return __task_get_classid(current);
3018 const struct bpf_func_proto bpf_get_cgroup_classid_curr_proto = {
3019 .func = bpf_get_cgroup_classid_curr,
3021 .ret_type = RET_INTEGER,
3024 BPF_CALL_1(bpf_skb_cgroup_classid, const struct sk_buff *, skb)
3026 struct sock *sk = skb_to_full_sk(skb);
3028 if (!sk || !sk_fullsock(sk))
3031 return sock_cgroup_classid(&sk->sk_cgrp_data);
3034 static const struct bpf_func_proto bpf_skb_cgroup_classid_proto = {
3035 .func = bpf_skb_cgroup_classid,
3037 .ret_type = RET_INTEGER,
3038 .arg1_type = ARG_PTR_TO_CTX,
3042 BPF_CALL_1(bpf_get_cgroup_classid, const struct sk_buff *, skb)
3044 return task_get_classid(skb);
3047 static const struct bpf_func_proto bpf_get_cgroup_classid_proto = {
3048 .func = bpf_get_cgroup_classid,
3050 .ret_type = RET_INTEGER,
3051 .arg1_type = ARG_PTR_TO_CTX,
3054 BPF_CALL_1(bpf_get_route_realm, const struct sk_buff *, skb)
3056 return dst_tclassid(skb);
3059 static const struct bpf_func_proto bpf_get_route_realm_proto = {
3060 .func = bpf_get_route_realm,
3062 .ret_type = RET_INTEGER,
3063 .arg1_type = ARG_PTR_TO_CTX,
3066 BPF_CALL_1(bpf_get_hash_recalc, struct sk_buff *, skb)
3068 /* If skb_clear_hash() was called due to mangling, we can
3069 * trigger SW recalculation here. Later access to hash
3070 * can then use the inline skb->hash via context directly
3071 * instead of calling this helper again.
3073 return skb_get_hash(skb);
3076 static const struct bpf_func_proto bpf_get_hash_recalc_proto = {
3077 .func = bpf_get_hash_recalc,
3079 .ret_type = RET_INTEGER,
3080 .arg1_type = ARG_PTR_TO_CTX,
3083 BPF_CALL_1(bpf_set_hash_invalid, struct sk_buff *, skb)
3085 /* After all direct packet write, this can be used once for
3086 * triggering a lazy recalc on next skb_get_hash() invocation.
3088 skb_clear_hash(skb);
3092 static const struct bpf_func_proto bpf_set_hash_invalid_proto = {
3093 .func = bpf_set_hash_invalid,
3095 .ret_type = RET_INTEGER,
3096 .arg1_type = ARG_PTR_TO_CTX,
3099 BPF_CALL_2(bpf_set_hash, struct sk_buff *, skb, u32, hash)
3101 /* Set user specified hash as L4(+), so that it gets returned
3102 * on skb_get_hash() call unless BPF prog later on triggers a
3105 __skb_set_sw_hash(skb, hash, true);
3109 static const struct bpf_func_proto bpf_set_hash_proto = {
3110 .func = bpf_set_hash,
3112 .ret_type = RET_INTEGER,
3113 .arg1_type = ARG_PTR_TO_CTX,
3114 .arg2_type = ARG_ANYTHING,
3117 BPF_CALL_3(bpf_skb_vlan_push, struct sk_buff *, skb, __be16, vlan_proto,
3122 if (unlikely(vlan_proto != htons(ETH_P_8021Q) &&
3123 vlan_proto != htons(ETH_P_8021AD)))
3124 vlan_proto = htons(ETH_P_8021Q);
3126 bpf_push_mac_rcsum(skb);
3127 ret = skb_vlan_push(skb, vlan_proto, vlan_tci);
3128 bpf_pull_mac_rcsum(skb);
3130 bpf_compute_data_pointers(skb);
3134 static const struct bpf_func_proto bpf_skb_vlan_push_proto = {
3135 .func = bpf_skb_vlan_push,
3137 .ret_type = RET_INTEGER,
3138 .arg1_type = ARG_PTR_TO_CTX,
3139 .arg2_type = ARG_ANYTHING,
3140 .arg3_type = ARG_ANYTHING,
3143 BPF_CALL_1(bpf_skb_vlan_pop, struct sk_buff *, skb)
3147 bpf_push_mac_rcsum(skb);
3148 ret = skb_vlan_pop(skb);
3149 bpf_pull_mac_rcsum(skb);
3151 bpf_compute_data_pointers(skb);
3155 static const struct bpf_func_proto bpf_skb_vlan_pop_proto = {
3156 .func = bpf_skb_vlan_pop,
3158 .ret_type = RET_INTEGER,
3159 .arg1_type = ARG_PTR_TO_CTX,
3162 static int bpf_skb_generic_push(struct sk_buff *skb, u32 off, u32 len)
3164 /* Caller already did skb_cow() with len as headroom,
3165 * so no need to do it here.
3168 memmove(skb->data, skb->data + len, off);
3169 memset(skb->data + off, 0, len);
3171 /* No skb_postpush_rcsum(skb, skb->data + off, len)
3172 * needed here as it does not change the skb->csum
3173 * result for checksum complete when summing over
3179 static int bpf_skb_generic_pop(struct sk_buff *skb, u32 off, u32 len)
3181 /* skb_ensure_writable() is not needed here, as we're
3182 * already working on an uncloned skb.
3184 if (unlikely(!pskb_may_pull(skb, off + len)))
3187 skb_postpull_rcsum(skb, skb->data + off, len);
3188 memmove(skb->data + len, skb->data, off);
3189 __skb_pull(skb, len);
3194 static int bpf_skb_net_hdr_push(struct sk_buff *skb, u32 off, u32 len)
3196 bool trans_same = skb->transport_header == skb->network_header;
3199 /* There's no need for __skb_push()/__skb_pull() pair to
3200 * get to the start of the mac header as we're guaranteed
3201 * to always start from here under eBPF.
3203 ret = bpf_skb_generic_push(skb, off, len);
3205 skb->mac_header -= len;
3206 skb->network_header -= len;
3208 skb->transport_header = skb->network_header;
3214 static int bpf_skb_net_hdr_pop(struct sk_buff *skb, u32 off, u32 len)
3216 bool trans_same = skb->transport_header == skb->network_header;
3219 /* Same here, __skb_push()/__skb_pull() pair not needed. */
3220 ret = bpf_skb_generic_pop(skb, off, len);
3222 skb->mac_header += len;
3223 skb->network_header += len;
3225 skb->transport_header = skb->network_header;
3231 static int bpf_skb_proto_4_to_6(struct sk_buff *skb)
3233 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
3234 u32 off = skb_mac_header_len(skb);
3237 ret = skb_cow(skb, len_diff);
3238 if (unlikely(ret < 0))
3241 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
3242 if (unlikely(ret < 0))
3245 if (skb_is_gso(skb)) {
3246 struct skb_shared_info *shinfo = skb_shinfo(skb);
3248 /* SKB_GSO_TCPV4 needs to be changed into SKB_GSO_TCPV6. */
3249 if (shinfo->gso_type & SKB_GSO_TCPV4) {
3250 shinfo->gso_type &= ~SKB_GSO_TCPV4;
3251 shinfo->gso_type |= SKB_GSO_TCPV6;
3255 skb->protocol = htons(ETH_P_IPV6);
3256 skb_clear_hash(skb);
3261 static int bpf_skb_proto_6_to_4(struct sk_buff *skb)
3263 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
3264 u32 off = skb_mac_header_len(skb);
3267 ret = skb_unclone(skb, GFP_ATOMIC);
3268 if (unlikely(ret < 0))
3271 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
3272 if (unlikely(ret < 0))
3275 if (skb_is_gso(skb)) {
3276 struct skb_shared_info *shinfo = skb_shinfo(skb);
3278 /* SKB_GSO_TCPV6 needs to be changed into SKB_GSO_TCPV4. */
3279 if (shinfo->gso_type & SKB_GSO_TCPV6) {
3280 shinfo->gso_type &= ~SKB_GSO_TCPV6;
3281 shinfo->gso_type |= SKB_GSO_TCPV4;
3285 skb->protocol = htons(ETH_P_IP);
3286 skb_clear_hash(skb);
3291 static int bpf_skb_proto_xlat(struct sk_buff *skb, __be16 to_proto)
3293 __be16 from_proto = skb->protocol;
3295 if (from_proto == htons(ETH_P_IP) &&
3296 to_proto == htons(ETH_P_IPV6))
3297 return bpf_skb_proto_4_to_6(skb);
3299 if (from_proto == htons(ETH_P_IPV6) &&
3300 to_proto == htons(ETH_P_IP))
3301 return bpf_skb_proto_6_to_4(skb);
3306 BPF_CALL_3(bpf_skb_change_proto, struct sk_buff *, skb, __be16, proto,
3311 if (unlikely(flags))
3314 /* General idea is that this helper does the basic groundwork
3315 * needed for changing the protocol, and eBPF program fills the
3316 * rest through bpf_skb_store_bytes(), bpf_lX_csum_replace()
3317 * and other helpers, rather than passing a raw buffer here.
3319 * The rationale is to keep this minimal and without a need to
3320 * deal with raw packet data. F.e. even if we would pass buffers
3321 * here, the program still needs to call the bpf_lX_csum_replace()
3322 * helpers anyway. Plus, this way we keep also separation of
3323 * concerns, since f.e. bpf_skb_store_bytes() should only take
3326 * Currently, additional options and extension header space are
3327 * not supported, but flags register is reserved so we can adapt
3328 * that. For offloads, we mark packet as dodgy, so that headers
3329 * need to be verified first.
3331 ret = bpf_skb_proto_xlat(skb, proto);
3332 bpf_compute_data_pointers(skb);
3336 static const struct bpf_func_proto bpf_skb_change_proto_proto = {
3337 .func = bpf_skb_change_proto,
3339 .ret_type = RET_INTEGER,
3340 .arg1_type = ARG_PTR_TO_CTX,
3341 .arg2_type = ARG_ANYTHING,
3342 .arg3_type = ARG_ANYTHING,
3345 BPF_CALL_2(bpf_skb_change_type, struct sk_buff *, skb, u32, pkt_type)
3347 /* We only allow a restricted subset to be changed for now. */
3348 if (unlikely(!skb_pkt_type_ok(skb->pkt_type) ||
3349 !skb_pkt_type_ok(pkt_type)))
3352 skb->pkt_type = pkt_type;
3356 static const struct bpf_func_proto bpf_skb_change_type_proto = {
3357 .func = bpf_skb_change_type,
3359 .ret_type = RET_INTEGER,
3360 .arg1_type = ARG_PTR_TO_CTX,
3361 .arg2_type = ARG_ANYTHING,
3364 static u32 bpf_skb_net_base_len(const struct sk_buff *skb)
3366 switch (skb->protocol) {
3367 case htons(ETH_P_IP):
3368 return sizeof(struct iphdr);
3369 case htons(ETH_P_IPV6):
3370 return sizeof(struct ipv6hdr);
3376 #define BPF_F_ADJ_ROOM_ENCAP_L3_MASK (BPF_F_ADJ_ROOM_ENCAP_L3_IPV4 | \
3377 BPF_F_ADJ_ROOM_ENCAP_L3_IPV6)
3379 #define BPF_F_ADJ_ROOM_MASK (BPF_F_ADJ_ROOM_FIXED_GSO | \
3380 BPF_F_ADJ_ROOM_ENCAP_L3_MASK | \
3381 BPF_F_ADJ_ROOM_ENCAP_L4_GRE | \
3382 BPF_F_ADJ_ROOM_ENCAP_L4_UDP | \
3383 BPF_F_ADJ_ROOM_ENCAP_L2_ETH | \
3384 BPF_F_ADJ_ROOM_ENCAP_L2( \
3385 BPF_ADJ_ROOM_ENCAP_L2_MASK))
3387 static int bpf_skb_net_grow(struct sk_buff *skb, u32 off, u32 len_diff,
3390 u8 inner_mac_len = flags >> BPF_ADJ_ROOM_ENCAP_L2_SHIFT;
3391 bool encap = flags & BPF_F_ADJ_ROOM_ENCAP_L3_MASK;
3392 u16 mac_len = 0, inner_net = 0, inner_trans = 0;
3393 unsigned int gso_type = SKB_GSO_DODGY;
3396 if (skb_is_gso(skb) && !skb_is_gso_tcp(skb)) {
3397 /* udp gso_size delineates datagrams, only allow if fixed */
3398 if (!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) ||
3399 !(flags & BPF_F_ADJ_ROOM_FIXED_GSO))
3403 ret = skb_cow_head(skb, len_diff);
3404 if (unlikely(ret < 0))
3408 if (skb->protocol != htons(ETH_P_IP) &&
3409 skb->protocol != htons(ETH_P_IPV6))
3412 if (flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV4 &&
3413 flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV6)
3416 if (flags & BPF_F_ADJ_ROOM_ENCAP_L4_GRE &&
3417 flags & BPF_F_ADJ_ROOM_ENCAP_L4_UDP)
3420 if (flags & BPF_F_ADJ_ROOM_ENCAP_L2_ETH &&
3421 inner_mac_len < ETH_HLEN)
3424 if (skb->encapsulation)
3427 mac_len = skb->network_header - skb->mac_header;
3428 inner_net = skb->network_header;
3429 if (inner_mac_len > len_diff)
3431 inner_trans = skb->transport_header;
3434 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
3435 if (unlikely(ret < 0))
3439 skb->inner_mac_header = inner_net - inner_mac_len;
3440 skb->inner_network_header = inner_net;
3441 skb->inner_transport_header = inner_trans;
3443 if (flags & BPF_F_ADJ_ROOM_ENCAP_L2_ETH)
3444 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
3446 skb_set_inner_protocol(skb, skb->protocol);
3448 skb->encapsulation = 1;
3449 skb_set_network_header(skb, mac_len);
3451 if (flags & BPF_F_ADJ_ROOM_ENCAP_L4_UDP)
3452 gso_type |= SKB_GSO_UDP_TUNNEL;
3453 else if (flags & BPF_F_ADJ_ROOM_ENCAP_L4_GRE)
3454 gso_type |= SKB_GSO_GRE;
3455 else if (flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV6)
3456 gso_type |= SKB_GSO_IPXIP6;
3457 else if (flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV4)
3458 gso_type |= SKB_GSO_IPXIP4;
3460 if (flags & BPF_F_ADJ_ROOM_ENCAP_L4_GRE ||
3461 flags & BPF_F_ADJ_ROOM_ENCAP_L4_UDP) {
3462 int nh_len = flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV6 ?
3463 sizeof(struct ipv6hdr) :
3464 sizeof(struct iphdr);
3466 skb_set_transport_header(skb, mac_len + nh_len);
3469 /* Match skb->protocol to new outer l3 protocol */
3470 if (skb->protocol == htons(ETH_P_IP) &&
3471 flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV6)
3472 skb->protocol = htons(ETH_P_IPV6);
3473 else if (skb->protocol == htons(ETH_P_IPV6) &&
3474 flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV4)
3475 skb->protocol = htons(ETH_P_IP);
3478 if (skb_is_gso(skb)) {
3479 struct skb_shared_info *shinfo = skb_shinfo(skb);
3481 /* Due to header grow, MSS needs to be downgraded. */
3482 if (!(flags & BPF_F_ADJ_ROOM_FIXED_GSO))
3483 skb_decrease_gso_size(shinfo, len_diff);
3485 /* Header must be checked, and gso_segs recomputed. */
3486 shinfo->gso_type |= gso_type;
3487 shinfo->gso_segs = 0;
3493 static int bpf_skb_net_shrink(struct sk_buff *skb, u32 off, u32 len_diff,
3498 if (unlikely(flags & ~(BPF_F_ADJ_ROOM_FIXED_GSO |
3499 BPF_F_ADJ_ROOM_NO_CSUM_RESET)))
3502 if (skb_is_gso(skb) && !skb_is_gso_tcp(skb)) {
3503 /* udp gso_size delineates datagrams, only allow if fixed */
3504 if (!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) ||
3505 !(flags & BPF_F_ADJ_ROOM_FIXED_GSO))
3509 ret = skb_unclone(skb, GFP_ATOMIC);
3510 if (unlikely(ret < 0))
3513 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
3514 if (unlikely(ret < 0))
3517 if (skb_is_gso(skb)) {
3518 struct skb_shared_info *shinfo = skb_shinfo(skb);
3520 /* Due to header shrink, MSS can be upgraded. */
3521 if (!(flags & BPF_F_ADJ_ROOM_FIXED_GSO))
3522 skb_increase_gso_size(shinfo, len_diff);
3524 /* Header must be checked, and gso_segs recomputed. */
3525 shinfo->gso_type |= SKB_GSO_DODGY;
3526 shinfo->gso_segs = 0;
3532 #define BPF_SKB_MAX_LEN SKB_MAX_ALLOC
3534 BPF_CALL_4(sk_skb_adjust_room, struct sk_buff *, skb, s32, len_diff,
3535 u32, mode, u64, flags)
3537 u32 len_diff_abs = abs(len_diff);
3538 bool shrink = len_diff < 0;
3541 if (unlikely(flags || mode))
3543 if (unlikely(len_diff_abs > 0xfffU))
3547 ret = skb_cow(skb, len_diff);
3548 if (unlikely(ret < 0))
3550 __skb_push(skb, len_diff_abs);
3551 memset(skb->data, 0, len_diff_abs);
3553 if (unlikely(!pskb_may_pull(skb, len_diff_abs)))
3555 __skb_pull(skb, len_diff_abs);
3557 if (tls_sw_has_ctx_rx(skb->sk)) {
3558 struct strp_msg *rxm = strp_msg(skb);
3560 rxm->full_len += len_diff;
3565 static const struct bpf_func_proto sk_skb_adjust_room_proto = {
3566 .func = sk_skb_adjust_room,
3568 .ret_type = RET_INTEGER,
3569 .arg1_type = ARG_PTR_TO_CTX,
3570 .arg2_type = ARG_ANYTHING,
3571 .arg3_type = ARG_ANYTHING,
3572 .arg4_type = ARG_ANYTHING,
3575 BPF_CALL_4(bpf_skb_adjust_room, struct sk_buff *, skb, s32, len_diff,
3576 u32, mode, u64, flags)
3578 u32 len_cur, len_diff_abs = abs(len_diff);
3579 u32 len_min = bpf_skb_net_base_len(skb);
3580 u32 len_max = BPF_SKB_MAX_LEN;
3581 __be16 proto = skb->protocol;
3582 bool shrink = len_diff < 0;
3586 if (unlikely(flags & ~(BPF_F_ADJ_ROOM_MASK |
3587 BPF_F_ADJ_ROOM_NO_CSUM_RESET)))
3589 if (unlikely(len_diff_abs > 0xfffU))
3591 if (unlikely(proto != htons(ETH_P_IP) &&
3592 proto != htons(ETH_P_IPV6)))
3595 off = skb_mac_header_len(skb);
3597 case BPF_ADJ_ROOM_NET:
3598 off += bpf_skb_net_base_len(skb);
3600 case BPF_ADJ_ROOM_MAC:
3606 len_cur = skb->len - skb_network_offset(skb);
3607 if ((shrink && (len_diff_abs >= len_cur ||
3608 len_cur - len_diff_abs < len_min)) ||
3609 (!shrink && (skb->len + len_diff_abs > len_max &&
3613 ret = shrink ? bpf_skb_net_shrink(skb, off, len_diff_abs, flags) :
3614 bpf_skb_net_grow(skb, off, len_diff_abs, flags);
3615 if (!ret && !(flags & BPF_F_ADJ_ROOM_NO_CSUM_RESET))
3616 __skb_reset_checksum_unnecessary(skb);
3618 bpf_compute_data_pointers(skb);
3622 static const struct bpf_func_proto bpf_skb_adjust_room_proto = {
3623 .func = bpf_skb_adjust_room,
3625 .ret_type = RET_INTEGER,
3626 .arg1_type = ARG_PTR_TO_CTX,
3627 .arg2_type = ARG_ANYTHING,
3628 .arg3_type = ARG_ANYTHING,
3629 .arg4_type = ARG_ANYTHING,
3632 static u32 __bpf_skb_min_len(const struct sk_buff *skb)
3634 u32 min_len = skb_network_offset(skb);
3636 if (skb_transport_header_was_set(skb))
3637 min_len = skb_transport_offset(skb);
3638 if (skb->ip_summed == CHECKSUM_PARTIAL)
3639 min_len = skb_checksum_start_offset(skb) +
3640 skb->csum_offset + sizeof(__sum16);
3644 static int bpf_skb_grow_rcsum(struct sk_buff *skb, unsigned int new_len)
3646 unsigned int old_len = skb->len;
3649 ret = __skb_grow_rcsum(skb, new_len);
3651 memset(skb->data + old_len, 0, new_len - old_len);
3655 static int bpf_skb_trim_rcsum(struct sk_buff *skb, unsigned int new_len)
3657 return __skb_trim_rcsum(skb, new_len);
3660 static inline int __bpf_skb_change_tail(struct sk_buff *skb, u32 new_len,
3663 u32 max_len = BPF_SKB_MAX_LEN;
3664 u32 min_len = __bpf_skb_min_len(skb);
3667 if (unlikely(flags || new_len > max_len || new_len < min_len))
3669 if (skb->encapsulation)
3672 /* The basic idea of this helper is that it's performing the
3673 * needed work to either grow or trim an skb, and eBPF program
3674 * rewrites the rest via helpers like bpf_skb_store_bytes(),
3675 * bpf_lX_csum_replace() and others rather than passing a raw
3676 * buffer here. This one is a slow path helper and intended
3677 * for replies with control messages.
3679 * Like in bpf_skb_change_proto(), we want to keep this rather
3680 * minimal and without protocol specifics so that we are able
3681 * to separate concerns as in bpf_skb_store_bytes() should only
3682 * be the one responsible for writing buffers.
3684 * It's really expected to be a slow path operation here for
3685 * control message replies, so we're implicitly linearizing,
3686 * uncloning and drop offloads from the skb by this.
3688 ret = __bpf_try_make_writable(skb, skb->len);
3690 if (new_len > skb->len)
3691 ret = bpf_skb_grow_rcsum(skb, new_len);
3692 else if (new_len < skb->len)
3693 ret = bpf_skb_trim_rcsum(skb, new_len);
3694 if (!ret && skb_is_gso(skb))
3700 BPF_CALL_3(bpf_skb_change_tail, struct sk_buff *, skb, u32, new_len,
3703 int ret = __bpf_skb_change_tail(skb, new_len, flags);
3705 bpf_compute_data_pointers(skb);
3709 static const struct bpf_func_proto bpf_skb_change_tail_proto = {
3710 .func = bpf_skb_change_tail,
3712 .ret_type = RET_INTEGER,
3713 .arg1_type = ARG_PTR_TO_CTX,
3714 .arg2_type = ARG_ANYTHING,
3715 .arg3_type = ARG_ANYTHING,
3718 BPF_CALL_3(sk_skb_change_tail, struct sk_buff *, skb, u32, new_len,
3721 return __bpf_skb_change_tail(skb, new_len, flags);
3724 static const struct bpf_func_proto sk_skb_change_tail_proto = {
3725 .func = sk_skb_change_tail,
3727 .ret_type = RET_INTEGER,
3728 .arg1_type = ARG_PTR_TO_CTX,
3729 .arg2_type = ARG_ANYTHING,
3730 .arg3_type = ARG_ANYTHING,
3733 static inline int __bpf_skb_change_head(struct sk_buff *skb, u32 head_room,
3736 u32 max_len = BPF_SKB_MAX_LEN;
3737 u32 new_len = skb->len + head_room;
3740 if (unlikely(flags || (!skb_is_gso(skb) && new_len > max_len) ||
3741 new_len < skb->len))
3744 ret = skb_cow(skb, head_room);
3746 /* Idea for this helper is that we currently only
3747 * allow to expand on mac header. This means that
3748 * skb->protocol network header, etc, stay as is.
3749 * Compared to bpf_skb_change_tail(), we're more
3750 * flexible due to not needing to linearize or
3751 * reset GSO. Intention for this helper is to be
3752 * used by an L3 skb that needs to push mac header
3753 * for redirection into L2 device.
3755 __skb_push(skb, head_room);
3756 memset(skb->data, 0, head_room);
3757 skb_reset_mac_header(skb);
3758 skb_reset_mac_len(skb);
3764 BPF_CALL_3(bpf_skb_change_head, struct sk_buff *, skb, u32, head_room,
3767 int ret = __bpf_skb_change_head(skb, head_room, flags);
3769 bpf_compute_data_pointers(skb);
3773 static const struct bpf_func_proto bpf_skb_change_head_proto = {
3774 .func = bpf_skb_change_head,
3776 .ret_type = RET_INTEGER,
3777 .arg1_type = ARG_PTR_TO_CTX,
3778 .arg2_type = ARG_ANYTHING,
3779 .arg3_type = ARG_ANYTHING,
3782 BPF_CALL_3(sk_skb_change_head, struct sk_buff *, skb, u32, head_room,
3785 return __bpf_skb_change_head(skb, head_room, flags);
3788 static const struct bpf_func_proto sk_skb_change_head_proto = {
3789 .func = sk_skb_change_head,
3791 .ret_type = RET_INTEGER,
3792 .arg1_type = ARG_PTR_TO_CTX,
3793 .arg2_type = ARG_ANYTHING,
3794 .arg3_type = ARG_ANYTHING,
3797 BPF_CALL_1(bpf_xdp_get_buff_len, struct xdp_buff*, xdp)
3799 return xdp_get_buff_len(xdp);
3802 static const struct bpf_func_proto bpf_xdp_get_buff_len_proto = {
3803 .func = bpf_xdp_get_buff_len,
3805 .ret_type = RET_INTEGER,
3806 .arg1_type = ARG_PTR_TO_CTX,
3809 BTF_ID_LIST_SINGLE(bpf_xdp_get_buff_len_bpf_ids, struct, xdp_buff)
3811 const struct bpf_func_proto bpf_xdp_get_buff_len_trace_proto = {
3812 .func = bpf_xdp_get_buff_len,
3814 .arg1_type = ARG_PTR_TO_BTF_ID,
3815 .arg1_btf_id = &bpf_xdp_get_buff_len_bpf_ids[0],
3818 static unsigned long xdp_get_metalen(const struct xdp_buff *xdp)
3820 return xdp_data_meta_unsupported(xdp) ? 0 :
3821 xdp->data - xdp->data_meta;
3824 BPF_CALL_2(bpf_xdp_adjust_head, struct xdp_buff *, xdp, int, offset)
3826 void *xdp_frame_end = xdp->data_hard_start + sizeof(struct xdp_frame);
3827 unsigned long metalen = xdp_get_metalen(xdp);
3828 void *data_start = xdp_frame_end + metalen;
3829 void *data = xdp->data + offset;
3831 if (unlikely(data < data_start ||
3832 data > xdp->data_end - ETH_HLEN))
3836 memmove(xdp->data_meta + offset,
3837 xdp->data_meta, metalen);
3838 xdp->data_meta += offset;
3844 static const struct bpf_func_proto bpf_xdp_adjust_head_proto = {
3845 .func = bpf_xdp_adjust_head,
3847 .ret_type = RET_INTEGER,
3848 .arg1_type = ARG_PTR_TO_CTX,
3849 .arg2_type = ARG_ANYTHING,
3852 static void bpf_xdp_copy_buf(struct xdp_buff *xdp, unsigned long off,
3853 void *buf, unsigned long len, bool flush)
3855 unsigned long ptr_len, ptr_off = 0;
3856 skb_frag_t *next_frag, *end_frag;
3857 struct skb_shared_info *sinfo;
3861 if (likely(xdp->data_end - xdp->data >= off + len)) {
3862 src = flush ? buf : xdp->data + off;
3863 dst = flush ? xdp->data + off : buf;
3864 memcpy(dst, src, len);
3868 sinfo = xdp_get_shared_info_from_buff(xdp);
3869 end_frag = &sinfo->frags[sinfo->nr_frags];
3870 next_frag = &sinfo->frags[0];
3872 ptr_len = xdp->data_end - xdp->data;
3873 ptr_buf = xdp->data;
3876 if (off < ptr_off + ptr_len) {
3877 unsigned long copy_off = off - ptr_off;
3878 unsigned long copy_len = min(len, ptr_len - copy_off);
3880 src = flush ? buf : ptr_buf + copy_off;
3881 dst = flush ? ptr_buf + copy_off : buf;
3882 memcpy(dst, src, copy_len);
3889 if (!len || next_frag == end_frag)
3893 ptr_buf = skb_frag_address(next_frag);
3894 ptr_len = skb_frag_size(next_frag);
3899 static void *bpf_xdp_pointer(struct xdp_buff *xdp, u32 offset, u32 len)
3901 struct skb_shared_info *sinfo = xdp_get_shared_info_from_buff(xdp);
3902 u32 size = xdp->data_end - xdp->data;
3903 void *addr = xdp->data;
3906 if (unlikely(offset > 0xffff || len > 0xffff))
3907 return ERR_PTR(-EFAULT);
3909 if (offset + len > xdp_get_buff_len(xdp))
3910 return ERR_PTR(-EINVAL);
3912 if (offset < size) /* linear area */
3916 for (i = 0; i < sinfo->nr_frags; i++) { /* paged area */
3917 u32 frag_size = skb_frag_size(&sinfo->frags[i]);
3919 if (offset < frag_size) {
3920 addr = skb_frag_address(&sinfo->frags[i]);
3924 offset -= frag_size;
3927 return offset + len <= size ? addr + offset : NULL;
3930 BPF_CALL_4(bpf_xdp_load_bytes, struct xdp_buff *, xdp, u32, offset,
3931 void *, buf, u32, len)
3935 ptr = bpf_xdp_pointer(xdp, offset, len);
3937 return PTR_ERR(ptr);
3940 bpf_xdp_copy_buf(xdp, offset, buf, len, false);
3942 memcpy(buf, ptr, len);
3947 static const struct bpf_func_proto bpf_xdp_load_bytes_proto = {
3948 .func = bpf_xdp_load_bytes,
3950 .ret_type = RET_INTEGER,
3951 .arg1_type = ARG_PTR_TO_CTX,
3952 .arg2_type = ARG_ANYTHING,
3953 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
3954 .arg4_type = ARG_CONST_SIZE,
3957 BPF_CALL_4(bpf_xdp_store_bytes, struct xdp_buff *, xdp, u32, offset,
3958 void *, buf, u32, len)
3962 ptr = bpf_xdp_pointer(xdp, offset, len);
3964 return PTR_ERR(ptr);
3967 bpf_xdp_copy_buf(xdp, offset, buf, len, true);
3969 memcpy(ptr, buf, len);
3974 static const struct bpf_func_proto bpf_xdp_store_bytes_proto = {
3975 .func = bpf_xdp_store_bytes,
3977 .ret_type = RET_INTEGER,
3978 .arg1_type = ARG_PTR_TO_CTX,
3979 .arg2_type = ARG_ANYTHING,
3980 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
3981 .arg4_type = ARG_CONST_SIZE,
3984 static int bpf_xdp_frags_increase_tail(struct xdp_buff *xdp, int offset)
3986 struct skb_shared_info *sinfo = xdp_get_shared_info_from_buff(xdp);
3987 skb_frag_t *frag = &sinfo->frags[sinfo->nr_frags - 1];
3988 struct xdp_rxq_info *rxq = xdp->rxq;
3989 unsigned int tailroom;
3991 if (!rxq->frag_size || rxq->frag_size > xdp->frame_sz)
3994 tailroom = rxq->frag_size - skb_frag_size(frag) - skb_frag_off(frag);
3995 if (unlikely(offset > tailroom))
3998 memset(skb_frag_address(frag) + skb_frag_size(frag), 0, offset);
3999 skb_frag_size_add(frag, offset);
4000 sinfo->xdp_frags_size += offset;
4005 static int bpf_xdp_frags_shrink_tail(struct xdp_buff *xdp, int offset)
4007 struct skb_shared_info *sinfo = xdp_get_shared_info_from_buff(xdp);
4008 int i, n_frags_free = 0, len_free = 0;
4010 if (unlikely(offset > (int)xdp_get_buff_len(xdp) - ETH_HLEN))
4013 for (i = sinfo->nr_frags - 1; i >= 0 && offset > 0; i--) {
4014 skb_frag_t *frag = &sinfo->frags[i];
4015 int shrink = min_t(int, offset, skb_frag_size(frag));
4020 if (skb_frag_size(frag) == shrink) {
4021 struct page *page = skb_frag_page(frag);
4023 __xdp_return(page_address(page), &xdp->rxq->mem,
4027 skb_frag_size_sub(frag, shrink);
4031 sinfo->nr_frags -= n_frags_free;
4032 sinfo->xdp_frags_size -= len_free;
4034 if (unlikely(!sinfo->nr_frags)) {
4035 xdp_buff_clear_frags_flag(xdp);
4036 xdp->data_end -= offset;
4042 BPF_CALL_2(bpf_xdp_adjust_tail, struct xdp_buff *, xdp, int, offset)
4044 void *data_hard_end = xdp_data_hard_end(xdp); /* use xdp->frame_sz */
4045 void *data_end = xdp->data_end + offset;
4047 if (unlikely(xdp_buff_has_frags(xdp))) { /* non-linear xdp buff */
4049 return bpf_xdp_frags_shrink_tail(xdp, -offset);
4051 return bpf_xdp_frags_increase_tail(xdp, offset);
4054 /* Notice that xdp_data_hard_end have reserved some tailroom */
4055 if (unlikely(data_end > data_hard_end))
4058 /* ALL drivers MUST init xdp->frame_sz, chicken check below */
4059 if (unlikely(xdp->frame_sz > PAGE_SIZE)) {
4060 WARN_ONCE(1, "Too BIG xdp->frame_sz = %d\n", xdp->frame_sz);
4064 if (unlikely(data_end < xdp->data + ETH_HLEN))
4067 /* Clear memory area on grow, can contain uninit kernel memory */
4069 memset(xdp->data_end, 0, offset);
4071 xdp->data_end = data_end;
4076 static const struct bpf_func_proto bpf_xdp_adjust_tail_proto = {
4077 .func = bpf_xdp_adjust_tail,
4079 .ret_type = RET_INTEGER,
4080 .arg1_type = ARG_PTR_TO_CTX,
4081 .arg2_type = ARG_ANYTHING,
4084 BPF_CALL_2(bpf_xdp_adjust_meta, struct xdp_buff *, xdp, int, offset)
4086 void *xdp_frame_end = xdp->data_hard_start + sizeof(struct xdp_frame);
4087 void *meta = xdp->data_meta + offset;
4088 unsigned long metalen = xdp->data - meta;
4090 if (xdp_data_meta_unsupported(xdp))
4092 if (unlikely(meta < xdp_frame_end ||
4095 if (unlikely(xdp_metalen_invalid(metalen)))
4098 xdp->data_meta = meta;
4103 static const struct bpf_func_proto bpf_xdp_adjust_meta_proto = {
4104 .func = bpf_xdp_adjust_meta,
4106 .ret_type = RET_INTEGER,
4107 .arg1_type = ARG_PTR_TO_CTX,
4108 .arg2_type = ARG_ANYTHING,
4111 /* XDP_REDIRECT works by a three-step process, implemented in the functions
4114 * 1. The bpf_redirect() and bpf_redirect_map() helpers will lookup the target
4115 * of the redirect and store it (along with some other metadata) in a per-CPU
4116 * struct bpf_redirect_info.
4118 * 2. When the program returns the XDP_REDIRECT return code, the driver will
4119 * call xdp_do_redirect() which will use the information in struct
4120 * bpf_redirect_info to actually enqueue the frame into a map type-specific
4121 * bulk queue structure.
4123 * 3. Before exiting its NAPI poll loop, the driver will call xdp_do_flush(),
4124 * which will flush all the different bulk queues, thus completing the
4127 * Pointers to the map entries will be kept around for this whole sequence of
4128 * steps, protected by RCU. However, there is no top-level rcu_read_lock() in
4129 * the core code; instead, the RCU protection relies on everything happening
4130 * inside a single NAPI poll sequence, which means it's between a pair of calls
4131 * to local_bh_disable()/local_bh_enable().
4133 * The map entries are marked as __rcu and the map code makes sure to
4134 * dereference those pointers with rcu_dereference_check() in a way that works
4135 * for both sections that to hold an rcu_read_lock() and sections that are
4136 * called from NAPI without a separate rcu_read_lock(). The code below does not
4137 * use RCU annotations, but relies on those in the map code.
4139 void xdp_do_flush(void)
4145 EXPORT_SYMBOL_GPL(xdp_do_flush);
4147 void bpf_clear_redirect_map(struct bpf_map *map)
4149 struct bpf_redirect_info *ri;
4152 for_each_possible_cpu(cpu) {
4153 ri = per_cpu_ptr(&bpf_redirect_info, cpu);
4154 /* Avoid polluting remote cacheline due to writes if
4155 * not needed. Once we pass this test, we need the
4156 * cmpxchg() to make sure it hasn't been changed in
4157 * the meantime by remote CPU.
4159 if (unlikely(READ_ONCE(ri->map) == map))
4160 cmpxchg(&ri->map, map, NULL);
4164 DEFINE_STATIC_KEY_FALSE(bpf_master_redirect_enabled_key);
4165 EXPORT_SYMBOL_GPL(bpf_master_redirect_enabled_key);
4167 u32 xdp_master_redirect(struct xdp_buff *xdp)
4169 struct net_device *master, *slave;
4170 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
4172 master = netdev_master_upper_dev_get_rcu(xdp->rxq->dev);
4173 slave = master->netdev_ops->ndo_xdp_get_xmit_slave(master, xdp);
4174 if (slave && slave != xdp->rxq->dev) {
4175 /* The target device is different from the receiving device, so
4176 * redirect it to the new device.
4177 * Using XDP_REDIRECT gets the correct behaviour from XDP enabled
4178 * drivers to unmap the packet from their rx ring.
4180 ri->tgt_index = slave->ifindex;
4181 ri->map_id = INT_MAX;
4182 ri->map_type = BPF_MAP_TYPE_UNSPEC;
4183 return XDP_REDIRECT;
4187 EXPORT_SYMBOL_GPL(xdp_master_redirect);
4189 static inline int __xdp_do_redirect_xsk(struct bpf_redirect_info *ri,
4190 struct net_device *dev,
4191 struct xdp_buff *xdp,
4192 struct bpf_prog *xdp_prog)
4194 enum bpf_map_type map_type = ri->map_type;
4195 void *fwd = ri->tgt_value;
4196 u32 map_id = ri->map_id;
4199 ri->map_id = 0; /* Valid map id idr range: [1,INT_MAX[ */
4200 ri->map_type = BPF_MAP_TYPE_UNSPEC;
4202 err = __xsk_map_redirect(fwd, xdp);
4206 _trace_xdp_redirect_map(dev, xdp_prog, fwd, map_type, map_id, ri->tgt_index);
4209 _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map_type, map_id, ri->tgt_index, err);
4213 static __always_inline int __xdp_do_redirect_frame(struct bpf_redirect_info *ri,
4214 struct net_device *dev,
4215 struct xdp_frame *xdpf,
4216 struct bpf_prog *xdp_prog)
4218 enum bpf_map_type map_type = ri->map_type;
4219 void *fwd = ri->tgt_value;
4220 u32 map_id = ri->map_id;
4221 struct bpf_map *map;
4224 ri->map_id = 0; /* Valid map id idr range: [1,INT_MAX[ */
4225 ri->map_type = BPF_MAP_TYPE_UNSPEC;
4227 if (unlikely(!xdpf)) {
4233 case BPF_MAP_TYPE_DEVMAP:
4235 case BPF_MAP_TYPE_DEVMAP_HASH:
4236 map = READ_ONCE(ri->map);
4237 if (unlikely(map)) {
4238 WRITE_ONCE(ri->map, NULL);
4239 err = dev_map_enqueue_multi(xdpf, dev, map,
4240 ri->flags & BPF_F_EXCLUDE_INGRESS);
4242 err = dev_map_enqueue(fwd, xdpf, dev);
4245 case BPF_MAP_TYPE_CPUMAP:
4246 err = cpu_map_enqueue(fwd, xdpf, dev);
4248 case BPF_MAP_TYPE_UNSPEC:
4249 if (map_id == INT_MAX) {
4250 fwd = dev_get_by_index_rcu(dev_net(dev), ri->tgt_index);
4251 if (unlikely(!fwd)) {
4255 err = dev_xdp_enqueue(fwd, xdpf, dev);
4266 _trace_xdp_redirect_map(dev, xdp_prog, fwd, map_type, map_id, ri->tgt_index);
4269 _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map_type, map_id, ri->tgt_index, err);
4273 int xdp_do_redirect(struct net_device *dev, struct xdp_buff *xdp,
4274 struct bpf_prog *xdp_prog)
4276 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
4277 enum bpf_map_type map_type = ri->map_type;
4279 /* XDP_REDIRECT is not fully supported yet for xdp frags since
4280 * not all XDP capable drivers can map non-linear xdp_frame in
4283 if (unlikely(xdp_buff_has_frags(xdp) &&
4284 map_type != BPF_MAP_TYPE_CPUMAP))
4287 if (map_type == BPF_MAP_TYPE_XSKMAP)
4288 return __xdp_do_redirect_xsk(ri, dev, xdp, xdp_prog);
4290 return __xdp_do_redirect_frame(ri, dev, xdp_convert_buff_to_frame(xdp),
4293 EXPORT_SYMBOL_GPL(xdp_do_redirect);
4295 int xdp_do_redirect_frame(struct net_device *dev, struct xdp_buff *xdp,
4296 struct xdp_frame *xdpf, struct bpf_prog *xdp_prog)
4298 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
4299 enum bpf_map_type map_type = ri->map_type;
4301 if (map_type == BPF_MAP_TYPE_XSKMAP)
4302 return __xdp_do_redirect_xsk(ri, dev, xdp, xdp_prog);
4304 return __xdp_do_redirect_frame(ri, dev, xdpf, xdp_prog);
4306 EXPORT_SYMBOL_GPL(xdp_do_redirect_frame);
4308 static int xdp_do_generic_redirect_map(struct net_device *dev,
4309 struct sk_buff *skb,
4310 struct xdp_buff *xdp,
4311 struct bpf_prog *xdp_prog,
4313 enum bpf_map_type map_type, u32 map_id)
4315 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
4316 struct bpf_map *map;
4320 case BPF_MAP_TYPE_DEVMAP:
4322 case BPF_MAP_TYPE_DEVMAP_HASH:
4323 map = READ_ONCE(ri->map);
4324 if (unlikely(map)) {
4325 WRITE_ONCE(ri->map, NULL);
4326 err = dev_map_redirect_multi(dev, skb, xdp_prog, map,
4327 ri->flags & BPF_F_EXCLUDE_INGRESS);
4329 err = dev_map_generic_redirect(fwd, skb, xdp_prog);
4334 case BPF_MAP_TYPE_XSKMAP:
4335 err = xsk_generic_rcv(fwd, xdp);
4340 case BPF_MAP_TYPE_CPUMAP:
4341 err = cpu_map_generic_redirect(fwd, skb);
4350 _trace_xdp_redirect_map(dev, xdp_prog, fwd, map_type, map_id, ri->tgt_index);
4353 _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map_type, map_id, ri->tgt_index, err);
4357 int xdp_do_generic_redirect(struct net_device *dev, struct sk_buff *skb,
4358 struct xdp_buff *xdp, struct bpf_prog *xdp_prog)
4360 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
4361 enum bpf_map_type map_type = ri->map_type;
4362 void *fwd = ri->tgt_value;
4363 u32 map_id = ri->map_id;
4366 ri->map_id = 0; /* Valid map id idr range: [1,INT_MAX[ */
4367 ri->map_type = BPF_MAP_TYPE_UNSPEC;
4369 if (map_type == BPF_MAP_TYPE_UNSPEC && map_id == INT_MAX) {
4370 fwd = dev_get_by_index_rcu(dev_net(dev), ri->tgt_index);
4371 if (unlikely(!fwd)) {
4376 err = xdp_ok_fwd_dev(fwd, skb->len);
4381 _trace_xdp_redirect(dev, xdp_prog, ri->tgt_index);
4382 generic_xdp_tx(skb, xdp_prog);
4386 return xdp_do_generic_redirect_map(dev, skb, xdp, xdp_prog, fwd, map_type, map_id);
4388 _trace_xdp_redirect_err(dev, xdp_prog, ri->tgt_index, err);
4392 BPF_CALL_2(bpf_xdp_redirect, u32, ifindex, u64, flags)
4394 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
4396 if (unlikely(flags))
4399 /* NB! Map type UNSPEC and map_id == INT_MAX (never generated
4400 * by map_idr) is used for ifindex based XDP redirect.
4402 ri->tgt_index = ifindex;
4403 ri->map_id = INT_MAX;
4404 ri->map_type = BPF_MAP_TYPE_UNSPEC;
4406 return XDP_REDIRECT;
4409 static const struct bpf_func_proto bpf_xdp_redirect_proto = {
4410 .func = bpf_xdp_redirect,
4412 .ret_type = RET_INTEGER,
4413 .arg1_type = ARG_ANYTHING,
4414 .arg2_type = ARG_ANYTHING,
4417 BPF_CALL_3(bpf_xdp_redirect_map, struct bpf_map *, map, u32, ifindex,
4420 return map->ops->map_redirect(map, ifindex, flags);
4423 static const struct bpf_func_proto bpf_xdp_redirect_map_proto = {
4424 .func = bpf_xdp_redirect_map,
4426 .ret_type = RET_INTEGER,
4427 .arg1_type = ARG_CONST_MAP_PTR,
4428 .arg2_type = ARG_ANYTHING,
4429 .arg3_type = ARG_ANYTHING,
4432 static unsigned long bpf_skb_copy(void *dst_buff, const void *skb,
4433 unsigned long off, unsigned long len)
4435 void *ptr = skb_header_pointer(skb, off, len, dst_buff);
4439 if (ptr != dst_buff)
4440 memcpy(dst_buff, ptr, len);
4445 BPF_CALL_5(bpf_skb_event_output, struct sk_buff *, skb, struct bpf_map *, map,
4446 u64, flags, void *, meta, u64, meta_size)
4448 u64 skb_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
4450 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
4452 if (unlikely(!skb || skb_size > skb->len))
4455 return bpf_event_output(map, flags, meta, meta_size, skb, skb_size,
4459 static const struct bpf_func_proto bpf_skb_event_output_proto = {
4460 .func = bpf_skb_event_output,
4462 .ret_type = RET_INTEGER,
4463 .arg1_type = ARG_PTR_TO_CTX,
4464 .arg2_type = ARG_CONST_MAP_PTR,
4465 .arg3_type = ARG_ANYTHING,
4466 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
4467 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
4470 BTF_ID_LIST_SINGLE(bpf_skb_output_btf_ids, struct, sk_buff)
4472 const struct bpf_func_proto bpf_skb_output_proto = {
4473 .func = bpf_skb_event_output,
4475 .ret_type = RET_INTEGER,
4476 .arg1_type = ARG_PTR_TO_BTF_ID,
4477 .arg1_btf_id = &bpf_skb_output_btf_ids[0],
4478 .arg2_type = ARG_CONST_MAP_PTR,
4479 .arg3_type = ARG_ANYTHING,
4480 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
4481 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
4484 static unsigned short bpf_tunnel_key_af(u64 flags)
4486 return flags & BPF_F_TUNINFO_IPV6 ? AF_INET6 : AF_INET;
4489 BPF_CALL_4(bpf_skb_get_tunnel_key, struct sk_buff *, skb, struct bpf_tunnel_key *, to,
4490 u32, size, u64, flags)
4492 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
4493 u8 compat[sizeof(struct bpf_tunnel_key)];
4497 if (unlikely(!info || (flags & ~(BPF_F_TUNINFO_IPV6 |
4498 BPF_F_TUNINFO_FLAGS)))) {
4502 if (ip_tunnel_info_af(info) != bpf_tunnel_key_af(flags)) {
4506 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
4509 case offsetof(struct bpf_tunnel_key, local_ipv6[0]):
4510 case offsetof(struct bpf_tunnel_key, tunnel_label):
4511 case offsetof(struct bpf_tunnel_key, tunnel_ext):
4513 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
4514 /* Fixup deprecated structure layouts here, so we have
4515 * a common path later on.
4517 if (ip_tunnel_info_af(info) != AF_INET)
4520 to = (struct bpf_tunnel_key *)compat;
4527 to->tunnel_id = be64_to_cpu(info->key.tun_id);
4528 to->tunnel_tos = info->key.tos;
4529 to->tunnel_ttl = info->key.ttl;
4530 if (flags & BPF_F_TUNINFO_FLAGS)
4531 to->tunnel_flags = info->key.tun_flags;
4535 if (flags & BPF_F_TUNINFO_IPV6) {
4536 memcpy(to->remote_ipv6, &info->key.u.ipv6.src,
4537 sizeof(to->remote_ipv6));
4538 memcpy(to->local_ipv6, &info->key.u.ipv6.dst,
4539 sizeof(to->local_ipv6));
4540 to->tunnel_label = be32_to_cpu(info->key.label);
4542 to->remote_ipv4 = be32_to_cpu(info->key.u.ipv4.src);
4543 memset(&to->remote_ipv6[1], 0, sizeof(__u32) * 3);
4544 to->local_ipv4 = be32_to_cpu(info->key.u.ipv4.dst);
4545 memset(&to->local_ipv6[1], 0, sizeof(__u32) * 3);
4546 to->tunnel_label = 0;
4549 if (unlikely(size != sizeof(struct bpf_tunnel_key)))
4550 memcpy(to_orig, to, size);
4554 memset(to_orig, 0, size);
4558 static const struct bpf_func_proto bpf_skb_get_tunnel_key_proto = {
4559 .func = bpf_skb_get_tunnel_key,
4561 .ret_type = RET_INTEGER,
4562 .arg1_type = ARG_PTR_TO_CTX,
4563 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
4564 .arg3_type = ARG_CONST_SIZE,
4565 .arg4_type = ARG_ANYTHING,
4568 BPF_CALL_3(bpf_skb_get_tunnel_opt, struct sk_buff *, skb, u8 *, to, u32, size)
4570 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
4573 if (unlikely(!info ||
4574 !(info->key.tun_flags & TUNNEL_OPTIONS_PRESENT))) {
4578 if (unlikely(size < info->options_len)) {
4583 ip_tunnel_info_opts_get(to, info);
4584 if (size > info->options_len)
4585 memset(to + info->options_len, 0, size - info->options_len);
4587 return info->options_len;
4589 memset(to, 0, size);
4593 static const struct bpf_func_proto bpf_skb_get_tunnel_opt_proto = {
4594 .func = bpf_skb_get_tunnel_opt,
4596 .ret_type = RET_INTEGER,
4597 .arg1_type = ARG_PTR_TO_CTX,
4598 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
4599 .arg3_type = ARG_CONST_SIZE,
4602 static struct metadata_dst __percpu *md_dst;
4604 BPF_CALL_4(bpf_skb_set_tunnel_key, struct sk_buff *, skb,
4605 const struct bpf_tunnel_key *, from, u32, size, u64, flags)
4607 struct metadata_dst *md = this_cpu_ptr(md_dst);
4608 u8 compat[sizeof(struct bpf_tunnel_key)];
4609 struct ip_tunnel_info *info;
4611 if (unlikely(flags & ~(BPF_F_TUNINFO_IPV6 | BPF_F_ZERO_CSUM_TX |
4612 BPF_F_DONT_FRAGMENT | BPF_F_SEQ_NUMBER)))
4614 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
4616 case offsetof(struct bpf_tunnel_key, local_ipv6[0]):
4617 case offsetof(struct bpf_tunnel_key, tunnel_label):
4618 case offsetof(struct bpf_tunnel_key, tunnel_ext):
4619 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
4620 /* Fixup deprecated structure layouts here, so we have
4621 * a common path later on.
4623 memcpy(compat, from, size);
4624 memset(compat + size, 0, sizeof(compat) - size);
4625 from = (const struct bpf_tunnel_key *) compat;
4631 if (unlikely((!(flags & BPF_F_TUNINFO_IPV6) && from->tunnel_label) ||
4636 dst_hold((struct dst_entry *) md);
4637 skb_dst_set(skb, (struct dst_entry *) md);
4639 info = &md->u.tun_info;
4640 memset(info, 0, sizeof(*info));
4641 info->mode = IP_TUNNEL_INFO_TX;
4643 info->key.tun_flags = TUNNEL_KEY | TUNNEL_CSUM | TUNNEL_NOCACHE;
4644 if (flags & BPF_F_DONT_FRAGMENT)
4645 info->key.tun_flags |= TUNNEL_DONT_FRAGMENT;
4646 if (flags & BPF_F_ZERO_CSUM_TX)
4647 info->key.tun_flags &= ~TUNNEL_CSUM;
4648 if (flags & BPF_F_SEQ_NUMBER)
4649 info->key.tun_flags |= TUNNEL_SEQ;
4651 info->key.tun_id = cpu_to_be64(from->tunnel_id);
4652 info->key.tos = from->tunnel_tos;
4653 info->key.ttl = from->tunnel_ttl;
4655 if (flags & BPF_F_TUNINFO_IPV6) {
4656 info->mode |= IP_TUNNEL_INFO_IPV6;
4657 memcpy(&info->key.u.ipv6.dst, from->remote_ipv6,
4658 sizeof(from->remote_ipv6));
4659 memcpy(&info->key.u.ipv6.src, from->local_ipv6,
4660 sizeof(from->local_ipv6));
4661 info->key.label = cpu_to_be32(from->tunnel_label) &
4662 IPV6_FLOWLABEL_MASK;
4664 info->key.u.ipv4.dst = cpu_to_be32(from->remote_ipv4);
4665 info->key.u.ipv4.src = cpu_to_be32(from->local_ipv4);
4666 info->key.flow_flags = FLOWI_FLAG_ANYSRC;
4672 static const struct bpf_func_proto bpf_skb_set_tunnel_key_proto = {
4673 .func = bpf_skb_set_tunnel_key,
4675 .ret_type = RET_INTEGER,
4676 .arg1_type = ARG_PTR_TO_CTX,
4677 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
4678 .arg3_type = ARG_CONST_SIZE,
4679 .arg4_type = ARG_ANYTHING,
4682 BPF_CALL_3(bpf_skb_set_tunnel_opt, struct sk_buff *, skb,
4683 const u8 *, from, u32, size)
4685 struct ip_tunnel_info *info = skb_tunnel_info(skb);
4686 const struct metadata_dst *md = this_cpu_ptr(md_dst);
4688 if (unlikely(info != &md->u.tun_info || (size & (sizeof(u32) - 1))))
4690 if (unlikely(size > IP_TUNNEL_OPTS_MAX))
4693 ip_tunnel_info_opts_set(info, from, size, TUNNEL_OPTIONS_PRESENT);
4698 static const struct bpf_func_proto bpf_skb_set_tunnel_opt_proto = {
4699 .func = bpf_skb_set_tunnel_opt,
4701 .ret_type = RET_INTEGER,
4702 .arg1_type = ARG_PTR_TO_CTX,
4703 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
4704 .arg3_type = ARG_CONST_SIZE,
4707 static const struct bpf_func_proto *
4708 bpf_get_skb_set_tunnel_proto(enum bpf_func_id which)
4711 struct metadata_dst __percpu *tmp;
4713 tmp = metadata_dst_alloc_percpu(IP_TUNNEL_OPTS_MAX,
4718 if (cmpxchg(&md_dst, NULL, tmp))
4719 metadata_dst_free_percpu(tmp);
4723 case BPF_FUNC_skb_set_tunnel_key:
4724 return &bpf_skb_set_tunnel_key_proto;
4725 case BPF_FUNC_skb_set_tunnel_opt:
4726 return &bpf_skb_set_tunnel_opt_proto;
4732 BPF_CALL_3(bpf_skb_under_cgroup, struct sk_buff *, skb, struct bpf_map *, map,
4735 struct bpf_array *array = container_of(map, struct bpf_array, map);
4736 struct cgroup *cgrp;
4739 sk = skb_to_full_sk(skb);
4740 if (!sk || !sk_fullsock(sk))
4742 if (unlikely(idx >= array->map.max_entries))
4745 cgrp = READ_ONCE(array->ptrs[idx]);
4746 if (unlikely(!cgrp))
4749 return sk_under_cgroup_hierarchy(sk, cgrp);
4752 static const struct bpf_func_proto bpf_skb_under_cgroup_proto = {
4753 .func = bpf_skb_under_cgroup,
4755 .ret_type = RET_INTEGER,
4756 .arg1_type = ARG_PTR_TO_CTX,
4757 .arg2_type = ARG_CONST_MAP_PTR,
4758 .arg3_type = ARG_ANYTHING,
4761 #ifdef CONFIG_SOCK_CGROUP_DATA
4762 static inline u64 __bpf_sk_cgroup_id(struct sock *sk)
4764 struct cgroup *cgrp;
4766 sk = sk_to_full_sk(sk);
4767 if (!sk || !sk_fullsock(sk))
4770 cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
4771 return cgroup_id(cgrp);
4774 BPF_CALL_1(bpf_skb_cgroup_id, const struct sk_buff *, skb)
4776 return __bpf_sk_cgroup_id(skb->sk);
4779 static const struct bpf_func_proto bpf_skb_cgroup_id_proto = {
4780 .func = bpf_skb_cgroup_id,
4782 .ret_type = RET_INTEGER,
4783 .arg1_type = ARG_PTR_TO_CTX,
4786 static inline u64 __bpf_sk_ancestor_cgroup_id(struct sock *sk,
4789 struct cgroup *ancestor;
4790 struct cgroup *cgrp;
4792 sk = sk_to_full_sk(sk);
4793 if (!sk || !sk_fullsock(sk))
4796 cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
4797 ancestor = cgroup_ancestor(cgrp, ancestor_level);
4801 return cgroup_id(ancestor);
4804 BPF_CALL_2(bpf_skb_ancestor_cgroup_id, const struct sk_buff *, skb, int,
4807 return __bpf_sk_ancestor_cgroup_id(skb->sk, ancestor_level);
4810 static const struct bpf_func_proto bpf_skb_ancestor_cgroup_id_proto = {
4811 .func = bpf_skb_ancestor_cgroup_id,
4813 .ret_type = RET_INTEGER,
4814 .arg1_type = ARG_PTR_TO_CTX,
4815 .arg2_type = ARG_ANYTHING,
4818 BPF_CALL_1(bpf_sk_cgroup_id, struct sock *, sk)
4820 return __bpf_sk_cgroup_id(sk);
4823 static const struct bpf_func_proto bpf_sk_cgroup_id_proto = {
4824 .func = bpf_sk_cgroup_id,
4826 .ret_type = RET_INTEGER,
4827 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
4830 BPF_CALL_2(bpf_sk_ancestor_cgroup_id, struct sock *, sk, int, ancestor_level)
4832 return __bpf_sk_ancestor_cgroup_id(sk, ancestor_level);
4835 static const struct bpf_func_proto bpf_sk_ancestor_cgroup_id_proto = {
4836 .func = bpf_sk_ancestor_cgroup_id,
4838 .ret_type = RET_INTEGER,
4839 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
4840 .arg2_type = ARG_ANYTHING,
4844 static unsigned long bpf_xdp_copy(void *dst, const void *ctx,
4845 unsigned long off, unsigned long len)
4847 struct xdp_buff *xdp = (struct xdp_buff *)ctx;
4849 bpf_xdp_copy_buf(xdp, off, dst, len, false);
4853 BPF_CALL_5(bpf_xdp_event_output, struct xdp_buff *, xdp, struct bpf_map *, map,
4854 u64, flags, void *, meta, u64, meta_size)
4856 u64 xdp_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
4858 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
4861 if (unlikely(!xdp || xdp_size > xdp_get_buff_len(xdp)))
4864 return bpf_event_output(map, flags, meta, meta_size, xdp,
4865 xdp_size, bpf_xdp_copy);
4868 static const struct bpf_func_proto bpf_xdp_event_output_proto = {
4869 .func = bpf_xdp_event_output,
4871 .ret_type = RET_INTEGER,
4872 .arg1_type = ARG_PTR_TO_CTX,
4873 .arg2_type = ARG_CONST_MAP_PTR,
4874 .arg3_type = ARG_ANYTHING,
4875 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
4876 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
4879 BTF_ID_LIST_SINGLE(bpf_xdp_output_btf_ids, struct, xdp_buff)
4881 const struct bpf_func_proto bpf_xdp_output_proto = {
4882 .func = bpf_xdp_event_output,
4884 .ret_type = RET_INTEGER,
4885 .arg1_type = ARG_PTR_TO_BTF_ID,
4886 .arg1_btf_id = &bpf_xdp_output_btf_ids[0],
4887 .arg2_type = ARG_CONST_MAP_PTR,
4888 .arg3_type = ARG_ANYTHING,
4889 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
4890 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
4893 BPF_CALL_1(bpf_get_socket_cookie, struct sk_buff *, skb)
4895 return skb->sk ? __sock_gen_cookie(skb->sk) : 0;
4898 static const struct bpf_func_proto bpf_get_socket_cookie_proto = {
4899 .func = bpf_get_socket_cookie,
4901 .ret_type = RET_INTEGER,
4902 .arg1_type = ARG_PTR_TO_CTX,
4905 BPF_CALL_1(bpf_get_socket_cookie_sock_addr, struct bpf_sock_addr_kern *, ctx)
4907 return __sock_gen_cookie(ctx->sk);
4910 static const struct bpf_func_proto bpf_get_socket_cookie_sock_addr_proto = {
4911 .func = bpf_get_socket_cookie_sock_addr,
4913 .ret_type = RET_INTEGER,
4914 .arg1_type = ARG_PTR_TO_CTX,
4917 BPF_CALL_1(bpf_get_socket_cookie_sock, struct sock *, ctx)
4919 return __sock_gen_cookie(ctx);
4922 static const struct bpf_func_proto bpf_get_socket_cookie_sock_proto = {
4923 .func = bpf_get_socket_cookie_sock,
4925 .ret_type = RET_INTEGER,
4926 .arg1_type = ARG_PTR_TO_CTX,
4929 BPF_CALL_1(bpf_get_socket_ptr_cookie, struct sock *, sk)
4931 return sk ? sock_gen_cookie(sk) : 0;
4934 const struct bpf_func_proto bpf_get_socket_ptr_cookie_proto = {
4935 .func = bpf_get_socket_ptr_cookie,
4937 .ret_type = RET_INTEGER,
4938 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
4941 BPF_CALL_1(bpf_get_socket_cookie_sock_ops, struct bpf_sock_ops_kern *, ctx)
4943 return __sock_gen_cookie(ctx->sk);
4946 static const struct bpf_func_proto bpf_get_socket_cookie_sock_ops_proto = {
4947 .func = bpf_get_socket_cookie_sock_ops,
4949 .ret_type = RET_INTEGER,
4950 .arg1_type = ARG_PTR_TO_CTX,
4953 static u64 __bpf_get_netns_cookie(struct sock *sk)
4955 const struct net *net = sk ? sock_net(sk) : &init_net;
4957 return net->net_cookie;
4960 BPF_CALL_1(bpf_get_netns_cookie_sock, struct sock *, ctx)
4962 return __bpf_get_netns_cookie(ctx);
4965 static const struct bpf_func_proto bpf_get_netns_cookie_sock_proto = {
4966 .func = bpf_get_netns_cookie_sock,
4968 .ret_type = RET_INTEGER,
4969 .arg1_type = ARG_PTR_TO_CTX_OR_NULL,
4972 BPF_CALL_1(bpf_get_netns_cookie_sock_addr, struct bpf_sock_addr_kern *, ctx)
4974 return __bpf_get_netns_cookie(ctx ? ctx->sk : NULL);
4977 static const struct bpf_func_proto bpf_get_netns_cookie_sock_addr_proto = {
4978 .func = bpf_get_netns_cookie_sock_addr,
4980 .ret_type = RET_INTEGER,
4981 .arg1_type = ARG_PTR_TO_CTX_OR_NULL,
4984 BPF_CALL_1(bpf_get_netns_cookie_sock_ops, struct bpf_sock_ops_kern *, ctx)
4986 return __bpf_get_netns_cookie(ctx ? ctx->sk : NULL);
4989 static const struct bpf_func_proto bpf_get_netns_cookie_sock_ops_proto = {
4990 .func = bpf_get_netns_cookie_sock_ops,
4992 .ret_type = RET_INTEGER,
4993 .arg1_type = ARG_PTR_TO_CTX_OR_NULL,
4996 BPF_CALL_1(bpf_get_netns_cookie_sk_msg, struct sk_msg *, ctx)
4998 return __bpf_get_netns_cookie(ctx ? ctx->sk : NULL);
5001 static const struct bpf_func_proto bpf_get_netns_cookie_sk_msg_proto = {
5002 .func = bpf_get_netns_cookie_sk_msg,
5004 .ret_type = RET_INTEGER,
5005 .arg1_type = ARG_PTR_TO_CTX_OR_NULL,
5008 BPF_CALL_1(bpf_get_socket_uid, struct sk_buff *, skb)
5010 struct sock *sk = sk_to_full_sk(skb->sk);
5013 if (!sk || !sk_fullsock(sk))
5015 kuid = sock_net_uid(sock_net(sk), sk);
5016 return from_kuid_munged(sock_net(sk)->user_ns, kuid);
5019 static const struct bpf_func_proto bpf_get_socket_uid_proto = {
5020 .func = bpf_get_socket_uid,
5022 .ret_type = RET_INTEGER,
5023 .arg1_type = ARG_PTR_TO_CTX,
5026 static int sol_socket_sockopt(struct sock *sk, int optname,
5027 char *optval, int *optlen,
5039 case SO_MAX_PACING_RATE:
5040 case SO_BINDTOIFINDEX:
5042 if (*optlen != sizeof(int))
5045 case SO_BINDTODEVICE:
5052 if (optname == SO_BINDTODEVICE)
5054 return sk_getsockopt(sk, SOL_SOCKET, optname,
5055 KERNEL_SOCKPTR(optval),
5056 KERNEL_SOCKPTR(optlen));
5059 return sk_setsockopt(sk, SOL_SOCKET, optname,
5060 KERNEL_SOCKPTR(optval), *optlen);
5063 static int bpf_sol_tcp_setsockopt(struct sock *sk, int optname,
5064 char *optval, int optlen)
5066 struct tcp_sock *tp = tcp_sk(sk);
5067 unsigned long timeout;
5070 if (optlen != sizeof(int))
5073 val = *(int *)optval;
5075 /* Only some options are supported */
5078 if (val <= 0 || tp->data_segs_out > tp->syn_data)
5080 tcp_snd_cwnd_set(tp, val);
5082 case TCP_BPF_SNDCWND_CLAMP:
5085 tp->snd_cwnd_clamp = val;
5086 tp->snd_ssthresh = val;
5088 case TCP_BPF_DELACK_MAX:
5089 timeout = usecs_to_jiffies(val);
5090 if (timeout > TCP_DELACK_MAX ||
5091 timeout < TCP_TIMEOUT_MIN)
5093 inet_csk(sk)->icsk_delack_max = timeout;
5095 case TCP_BPF_RTO_MIN:
5096 timeout = usecs_to_jiffies(val);
5097 if (timeout > TCP_RTO_MIN ||
5098 timeout < TCP_TIMEOUT_MIN)
5100 inet_csk(sk)->icsk_rto_min = timeout;
5109 static int sol_tcp_sockopt_congestion(struct sock *sk, char *optval,
5110 int *optlen, bool getopt)
5112 struct tcp_sock *tp;
5119 if (!inet_csk(sk)->icsk_ca_ops)
5121 /* BPF expects NULL-terminated tcp-cc string */
5122 optval[--(*optlen)] = '\0';
5123 return do_tcp_getsockopt(sk, SOL_TCP, TCP_CONGESTION,
5124 KERNEL_SOCKPTR(optval),
5125 KERNEL_SOCKPTR(optlen));
5128 /* "cdg" is the only cc that alloc a ptr
5129 * in inet_csk_ca area. The bpf-tcp-cc may
5130 * overwrite this ptr after switching to cdg.
5132 if (*optlen >= sizeof("cdg") - 1 && !strncmp("cdg", optval, *optlen))
5135 /* It stops this looping
5137 * .init => bpf_setsockopt(tcp_cc) => .init =>
5138 * bpf_setsockopt(tcp_cc)" => .init => ....
5140 * The second bpf_setsockopt(tcp_cc) is not allowed
5141 * in order to break the loop when both .init
5142 * are the same bpf prog.
5144 * This applies even the second bpf_setsockopt(tcp_cc)
5145 * does not cause a loop. This limits only the first
5146 * '.init' can call bpf_setsockopt(TCP_CONGESTION) to
5147 * pick a fallback cc (eg. peer does not support ECN)
5148 * and the second '.init' cannot fallback to
5152 if (tp->bpf_chg_cc_inprogress)
5155 tp->bpf_chg_cc_inprogress = 1;
5156 ret = do_tcp_setsockopt(sk, SOL_TCP, TCP_CONGESTION,
5157 KERNEL_SOCKPTR(optval), *optlen);
5158 tp->bpf_chg_cc_inprogress = 0;
5162 static int sol_tcp_sockopt(struct sock *sk, int optname,
5163 char *optval, int *optlen,
5166 if (sk->sk_prot->setsockopt != tcp_setsockopt)
5176 case TCP_WINDOW_CLAMP:
5177 case TCP_THIN_LINEAR_TIMEOUTS:
5178 case TCP_USER_TIMEOUT:
5179 case TCP_NOTSENT_LOWAT:
5181 if (*optlen != sizeof(int))
5184 case TCP_CONGESTION:
5185 return sol_tcp_sockopt_congestion(sk, optval, optlen, getopt);
5193 return bpf_sol_tcp_setsockopt(sk, optname, optval, *optlen);
5197 if (optname == TCP_SAVED_SYN) {
5198 struct tcp_sock *tp = tcp_sk(sk);
5200 if (!tp->saved_syn ||
5201 *optlen > tcp_saved_syn_len(tp->saved_syn))
5203 memcpy(optval, tp->saved_syn->data, *optlen);
5204 /* It cannot free tp->saved_syn here because it
5205 * does not know if the user space still needs it.
5210 return do_tcp_getsockopt(sk, SOL_TCP, optname,
5211 KERNEL_SOCKPTR(optval),
5212 KERNEL_SOCKPTR(optlen));
5215 return do_tcp_setsockopt(sk, SOL_TCP, optname,
5216 KERNEL_SOCKPTR(optval), *optlen);
5219 static int sol_ip_sockopt(struct sock *sk, int optname,
5220 char *optval, int *optlen,
5223 if (sk->sk_family != AF_INET)
5228 if (*optlen != sizeof(int))
5236 return do_ip_getsockopt(sk, SOL_IP, optname,
5237 KERNEL_SOCKPTR(optval),
5238 KERNEL_SOCKPTR(optlen));
5240 return do_ip_setsockopt(sk, SOL_IP, optname,
5241 KERNEL_SOCKPTR(optval), *optlen);
5244 static int sol_ipv6_sockopt(struct sock *sk, int optname,
5245 char *optval, int *optlen,
5248 if (sk->sk_family != AF_INET6)
5253 case IPV6_AUTOFLOWLABEL:
5254 if (*optlen != sizeof(int))
5262 return ipv6_bpf_stub->ipv6_getsockopt(sk, SOL_IPV6, optname,
5263 KERNEL_SOCKPTR(optval),
5264 KERNEL_SOCKPTR(optlen));
5266 return ipv6_bpf_stub->ipv6_setsockopt(sk, SOL_IPV6, optname,
5267 KERNEL_SOCKPTR(optval), *optlen);
5270 static int __bpf_setsockopt(struct sock *sk, int level, int optname,
5271 char *optval, int optlen)
5273 if (!sk_fullsock(sk))
5276 if (level == SOL_SOCKET)
5277 return sol_socket_sockopt(sk, optname, optval, &optlen, false);
5278 else if (IS_ENABLED(CONFIG_INET) && level == SOL_IP)
5279 return sol_ip_sockopt(sk, optname, optval, &optlen, false);
5280 else if (IS_ENABLED(CONFIG_IPV6) && level == SOL_IPV6)
5281 return sol_ipv6_sockopt(sk, optname, optval, &optlen, false);
5282 else if (IS_ENABLED(CONFIG_INET) && level == SOL_TCP)
5283 return sol_tcp_sockopt(sk, optname, optval, &optlen, false);
5288 static int _bpf_setsockopt(struct sock *sk, int level, int optname,
5289 char *optval, int optlen)
5291 if (sk_fullsock(sk))
5292 sock_owned_by_me(sk);
5293 return __bpf_setsockopt(sk, level, optname, optval, optlen);
5296 static int __bpf_getsockopt(struct sock *sk, int level, int optname,
5297 char *optval, int optlen)
5299 int err, saved_optlen = optlen;
5301 if (!sk_fullsock(sk)) {
5306 if (level == SOL_SOCKET)
5307 err = sol_socket_sockopt(sk, optname, optval, &optlen, true);
5308 else if (IS_ENABLED(CONFIG_INET) && level == SOL_TCP)
5309 err = sol_tcp_sockopt(sk, optname, optval, &optlen, true);
5310 else if (IS_ENABLED(CONFIG_INET) && level == SOL_IP)
5311 err = sol_ip_sockopt(sk, optname, optval, &optlen, true);
5312 else if (IS_ENABLED(CONFIG_IPV6) && level == SOL_IPV6)
5313 err = sol_ipv6_sockopt(sk, optname, optval, &optlen, true);
5320 if (optlen < saved_optlen)
5321 memset(optval + optlen, 0, saved_optlen - optlen);
5325 static int _bpf_getsockopt(struct sock *sk, int level, int optname,
5326 char *optval, int optlen)
5328 if (sk_fullsock(sk))
5329 sock_owned_by_me(sk);
5330 return __bpf_getsockopt(sk, level, optname, optval, optlen);
5333 BPF_CALL_5(bpf_sk_setsockopt, struct sock *, sk, int, level,
5334 int, optname, char *, optval, int, optlen)
5336 return _bpf_setsockopt(sk, level, optname, optval, optlen);
5339 const struct bpf_func_proto bpf_sk_setsockopt_proto = {
5340 .func = bpf_sk_setsockopt,
5342 .ret_type = RET_INTEGER,
5343 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
5344 .arg2_type = ARG_ANYTHING,
5345 .arg3_type = ARG_ANYTHING,
5346 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
5347 .arg5_type = ARG_CONST_SIZE,
5350 BPF_CALL_5(bpf_sk_getsockopt, struct sock *, sk, int, level,
5351 int, optname, char *, optval, int, optlen)
5353 return _bpf_getsockopt(sk, level, optname, optval, optlen);
5356 const struct bpf_func_proto bpf_sk_getsockopt_proto = {
5357 .func = bpf_sk_getsockopt,
5359 .ret_type = RET_INTEGER,
5360 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
5361 .arg2_type = ARG_ANYTHING,
5362 .arg3_type = ARG_ANYTHING,
5363 .arg4_type = ARG_PTR_TO_UNINIT_MEM,
5364 .arg5_type = ARG_CONST_SIZE,
5367 BPF_CALL_5(bpf_unlocked_sk_setsockopt, struct sock *, sk, int, level,
5368 int, optname, char *, optval, int, optlen)
5370 return __bpf_setsockopt(sk, level, optname, optval, optlen);
5373 const struct bpf_func_proto bpf_unlocked_sk_setsockopt_proto = {
5374 .func = bpf_unlocked_sk_setsockopt,
5376 .ret_type = RET_INTEGER,
5377 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
5378 .arg2_type = ARG_ANYTHING,
5379 .arg3_type = ARG_ANYTHING,
5380 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
5381 .arg5_type = ARG_CONST_SIZE,
5384 BPF_CALL_5(bpf_unlocked_sk_getsockopt, struct sock *, sk, int, level,
5385 int, optname, char *, optval, int, optlen)
5387 return __bpf_getsockopt(sk, level, optname, optval, optlen);
5390 const struct bpf_func_proto bpf_unlocked_sk_getsockopt_proto = {
5391 .func = bpf_unlocked_sk_getsockopt,
5393 .ret_type = RET_INTEGER,
5394 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
5395 .arg2_type = ARG_ANYTHING,
5396 .arg3_type = ARG_ANYTHING,
5397 .arg4_type = ARG_PTR_TO_UNINIT_MEM,
5398 .arg5_type = ARG_CONST_SIZE,
5401 BPF_CALL_5(bpf_sock_addr_setsockopt, struct bpf_sock_addr_kern *, ctx,
5402 int, level, int, optname, char *, optval, int, optlen)
5404 return _bpf_setsockopt(ctx->sk, level, optname, optval, optlen);
5407 static const struct bpf_func_proto bpf_sock_addr_setsockopt_proto = {
5408 .func = bpf_sock_addr_setsockopt,
5410 .ret_type = RET_INTEGER,
5411 .arg1_type = ARG_PTR_TO_CTX,
5412 .arg2_type = ARG_ANYTHING,
5413 .arg3_type = ARG_ANYTHING,
5414 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
5415 .arg5_type = ARG_CONST_SIZE,
5418 BPF_CALL_5(bpf_sock_addr_getsockopt, struct bpf_sock_addr_kern *, ctx,
5419 int, level, int, optname, char *, optval, int, optlen)
5421 return _bpf_getsockopt(ctx->sk, level, optname, optval, optlen);
5424 static const struct bpf_func_proto bpf_sock_addr_getsockopt_proto = {
5425 .func = bpf_sock_addr_getsockopt,
5427 .ret_type = RET_INTEGER,
5428 .arg1_type = ARG_PTR_TO_CTX,
5429 .arg2_type = ARG_ANYTHING,
5430 .arg3_type = ARG_ANYTHING,
5431 .arg4_type = ARG_PTR_TO_UNINIT_MEM,
5432 .arg5_type = ARG_CONST_SIZE,
5435 BPF_CALL_5(bpf_sock_ops_setsockopt, struct bpf_sock_ops_kern *, bpf_sock,
5436 int, level, int, optname, char *, optval, int, optlen)
5438 return _bpf_setsockopt(bpf_sock->sk, level, optname, optval, optlen);
5441 static const struct bpf_func_proto bpf_sock_ops_setsockopt_proto = {
5442 .func = bpf_sock_ops_setsockopt,
5444 .ret_type = RET_INTEGER,
5445 .arg1_type = ARG_PTR_TO_CTX,
5446 .arg2_type = ARG_ANYTHING,
5447 .arg3_type = ARG_ANYTHING,
5448 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
5449 .arg5_type = ARG_CONST_SIZE,
5452 static int bpf_sock_ops_get_syn(struct bpf_sock_ops_kern *bpf_sock,
5453 int optname, const u8 **start)
5455 struct sk_buff *syn_skb = bpf_sock->syn_skb;
5456 const u8 *hdr_start;
5460 /* sk is a request_sock here */
5462 if (optname == TCP_BPF_SYN) {
5463 hdr_start = syn_skb->data;
5464 ret = tcp_hdrlen(syn_skb);
5465 } else if (optname == TCP_BPF_SYN_IP) {
5466 hdr_start = skb_network_header(syn_skb);
5467 ret = skb_network_header_len(syn_skb) +
5468 tcp_hdrlen(syn_skb);
5470 /* optname == TCP_BPF_SYN_MAC */
5471 hdr_start = skb_mac_header(syn_skb);
5472 ret = skb_mac_header_len(syn_skb) +
5473 skb_network_header_len(syn_skb) +
5474 tcp_hdrlen(syn_skb);
5477 struct sock *sk = bpf_sock->sk;
5478 struct saved_syn *saved_syn;
5480 if (sk->sk_state == TCP_NEW_SYN_RECV)
5481 /* synack retransmit. bpf_sock->syn_skb will
5482 * not be available. It has to resort to
5483 * saved_syn (if it is saved).
5485 saved_syn = inet_reqsk(sk)->saved_syn;
5487 saved_syn = tcp_sk(sk)->saved_syn;
5492 if (optname == TCP_BPF_SYN) {
5493 hdr_start = saved_syn->data +
5494 saved_syn->mac_hdrlen +
5495 saved_syn->network_hdrlen;
5496 ret = saved_syn->tcp_hdrlen;
5497 } else if (optname == TCP_BPF_SYN_IP) {
5498 hdr_start = saved_syn->data +
5499 saved_syn->mac_hdrlen;
5500 ret = saved_syn->network_hdrlen +
5501 saved_syn->tcp_hdrlen;
5503 /* optname == TCP_BPF_SYN_MAC */
5505 /* TCP_SAVE_SYN may not have saved the mac hdr */
5506 if (!saved_syn->mac_hdrlen)
5509 hdr_start = saved_syn->data;
5510 ret = saved_syn->mac_hdrlen +
5511 saved_syn->network_hdrlen +
5512 saved_syn->tcp_hdrlen;
5520 BPF_CALL_5(bpf_sock_ops_getsockopt, struct bpf_sock_ops_kern *, bpf_sock,
5521 int, level, int, optname, char *, optval, int, optlen)
5523 if (IS_ENABLED(CONFIG_INET) && level == SOL_TCP &&
5524 optname >= TCP_BPF_SYN && optname <= TCP_BPF_SYN_MAC) {
5525 int ret, copy_len = 0;
5528 ret = bpf_sock_ops_get_syn(bpf_sock, optname, &start);
5531 if (optlen < copy_len) {
5536 memcpy(optval, start, copy_len);
5539 /* Zero out unused buffer at the end */
5540 memset(optval + copy_len, 0, optlen - copy_len);
5545 return _bpf_getsockopt(bpf_sock->sk, level, optname, optval, optlen);
5548 static const struct bpf_func_proto bpf_sock_ops_getsockopt_proto = {
5549 .func = bpf_sock_ops_getsockopt,
5551 .ret_type = RET_INTEGER,
5552 .arg1_type = ARG_PTR_TO_CTX,
5553 .arg2_type = ARG_ANYTHING,
5554 .arg3_type = ARG_ANYTHING,
5555 .arg4_type = ARG_PTR_TO_UNINIT_MEM,
5556 .arg5_type = ARG_CONST_SIZE,
5559 BPF_CALL_2(bpf_sock_ops_cb_flags_set, struct bpf_sock_ops_kern *, bpf_sock,
5562 struct sock *sk = bpf_sock->sk;
5563 int val = argval & BPF_SOCK_OPS_ALL_CB_FLAGS;
5565 if (!IS_ENABLED(CONFIG_INET) || !sk_fullsock(sk))
5568 tcp_sk(sk)->bpf_sock_ops_cb_flags = val;
5570 return argval & (~BPF_SOCK_OPS_ALL_CB_FLAGS);
5573 static const struct bpf_func_proto bpf_sock_ops_cb_flags_set_proto = {
5574 .func = bpf_sock_ops_cb_flags_set,
5576 .ret_type = RET_INTEGER,
5577 .arg1_type = ARG_PTR_TO_CTX,
5578 .arg2_type = ARG_ANYTHING,
5581 const struct ipv6_bpf_stub *ipv6_bpf_stub __read_mostly;
5582 EXPORT_SYMBOL_GPL(ipv6_bpf_stub);
5584 BPF_CALL_3(bpf_bind, struct bpf_sock_addr_kern *, ctx, struct sockaddr *, addr,
5588 struct sock *sk = ctx->sk;
5589 u32 flags = BIND_FROM_BPF;
5593 if (addr_len < offsetofend(struct sockaddr, sa_family))
5595 if (addr->sa_family == AF_INET) {
5596 if (addr_len < sizeof(struct sockaddr_in))
5598 if (((struct sockaddr_in *)addr)->sin_port == htons(0))
5599 flags |= BIND_FORCE_ADDRESS_NO_PORT;
5600 return __inet_bind(sk, addr, addr_len, flags);
5601 #if IS_ENABLED(CONFIG_IPV6)
5602 } else if (addr->sa_family == AF_INET6) {
5603 if (addr_len < SIN6_LEN_RFC2133)
5605 if (((struct sockaddr_in6 *)addr)->sin6_port == htons(0))
5606 flags |= BIND_FORCE_ADDRESS_NO_PORT;
5607 /* ipv6_bpf_stub cannot be NULL, since it's called from
5608 * bpf_cgroup_inet6_connect hook and ipv6 is already loaded
5610 return ipv6_bpf_stub->inet6_bind(sk, addr, addr_len, flags);
5611 #endif /* CONFIG_IPV6 */
5613 #endif /* CONFIG_INET */
5615 return -EAFNOSUPPORT;
5618 static const struct bpf_func_proto bpf_bind_proto = {
5621 .ret_type = RET_INTEGER,
5622 .arg1_type = ARG_PTR_TO_CTX,
5623 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
5624 .arg3_type = ARG_CONST_SIZE,
5628 BPF_CALL_5(bpf_skb_get_xfrm_state, struct sk_buff *, skb, u32, index,
5629 struct bpf_xfrm_state *, to, u32, size, u64, flags)
5631 const struct sec_path *sp = skb_sec_path(skb);
5632 const struct xfrm_state *x;
5634 if (!sp || unlikely(index >= sp->len || flags))
5637 x = sp->xvec[index];
5639 if (unlikely(size != sizeof(struct bpf_xfrm_state)))
5642 to->reqid = x->props.reqid;
5643 to->spi = x->id.spi;
5644 to->family = x->props.family;
5647 if (to->family == AF_INET6) {
5648 memcpy(to->remote_ipv6, x->props.saddr.a6,
5649 sizeof(to->remote_ipv6));
5651 to->remote_ipv4 = x->props.saddr.a4;
5652 memset(&to->remote_ipv6[1], 0, sizeof(__u32) * 3);
5657 memset(to, 0, size);
5661 static const struct bpf_func_proto bpf_skb_get_xfrm_state_proto = {
5662 .func = bpf_skb_get_xfrm_state,
5664 .ret_type = RET_INTEGER,
5665 .arg1_type = ARG_PTR_TO_CTX,
5666 .arg2_type = ARG_ANYTHING,
5667 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
5668 .arg4_type = ARG_CONST_SIZE,
5669 .arg5_type = ARG_ANYTHING,
5673 #if IS_ENABLED(CONFIG_INET) || IS_ENABLED(CONFIG_IPV6)
5674 static int bpf_fib_set_fwd_params(struct bpf_fib_lookup *params,
5675 const struct neighbour *neigh,
5676 const struct net_device *dev, u32 mtu)
5678 memcpy(params->dmac, neigh->ha, ETH_ALEN);
5679 memcpy(params->smac, dev->dev_addr, ETH_ALEN);
5680 params->h_vlan_TCI = 0;
5681 params->h_vlan_proto = 0;
5683 params->mtu_result = mtu; /* union with tot_len */
5689 #if IS_ENABLED(CONFIG_INET)
5690 static int bpf_ipv4_fib_lookup(struct net *net, struct bpf_fib_lookup *params,
5691 u32 flags, bool check_mtu)
5693 struct fib_nh_common *nhc;
5694 struct in_device *in_dev;
5695 struct neighbour *neigh;
5696 struct net_device *dev;
5697 struct fib_result res;
5702 dev = dev_get_by_index_rcu(net, params->ifindex);
5706 /* verify forwarding is enabled on this interface */
5707 in_dev = __in_dev_get_rcu(dev);
5708 if (unlikely(!in_dev || !IN_DEV_FORWARD(in_dev)))
5709 return BPF_FIB_LKUP_RET_FWD_DISABLED;
5711 if (flags & BPF_FIB_LOOKUP_OUTPUT) {
5713 fl4.flowi4_oif = params->ifindex;
5715 fl4.flowi4_iif = params->ifindex;
5718 fl4.flowi4_tos = params->tos & IPTOS_RT_MASK;
5719 fl4.flowi4_scope = RT_SCOPE_UNIVERSE;
5720 fl4.flowi4_flags = 0;
5722 fl4.flowi4_proto = params->l4_protocol;
5723 fl4.daddr = params->ipv4_dst;
5724 fl4.saddr = params->ipv4_src;
5725 fl4.fl4_sport = params->sport;
5726 fl4.fl4_dport = params->dport;
5727 fl4.flowi4_multipath_hash = 0;
5729 if (flags & BPF_FIB_LOOKUP_DIRECT) {
5730 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
5731 struct fib_table *tb;
5733 tb = fib_get_table(net, tbid);
5735 return BPF_FIB_LKUP_RET_NOT_FWDED;
5737 err = fib_table_lookup(tb, &fl4, &res, FIB_LOOKUP_NOREF);
5739 fl4.flowi4_mark = 0;
5740 fl4.flowi4_secid = 0;
5741 fl4.flowi4_tun_key.tun_id = 0;
5742 fl4.flowi4_uid = sock_net_uid(net, NULL);
5744 err = fib_lookup(net, &fl4, &res, FIB_LOOKUP_NOREF);
5748 /* map fib lookup errors to RTN_ type */
5750 return BPF_FIB_LKUP_RET_BLACKHOLE;
5751 if (err == -EHOSTUNREACH)
5752 return BPF_FIB_LKUP_RET_UNREACHABLE;
5754 return BPF_FIB_LKUP_RET_PROHIBIT;
5756 return BPF_FIB_LKUP_RET_NOT_FWDED;
5759 if (res.type != RTN_UNICAST)
5760 return BPF_FIB_LKUP_RET_NOT_FWDED;
5762 if (fib_info_num_path(res.fi) > 1)
5763 fib_select_path(net, &res, &fl4, NULL);
5766 mtu = ip_mtu_from_fib_result(&res, params->ipv4_dst);
5767 if (params->tot_len > mtu) {
5768 params->mtu_result = mtu; /* union with tot_len */
5769 return BPF_FIB_LKUP_RET_FRAG_NEEDED;
5775 /* do not handle lwt encaps right now */
5776 if (nhc->nhc_lwtstate)
5777 return BPF_FIB_LKUP_RET_UNSUPP_LWT;
5781 params->rt_metric = res.fi->fib_priority;
5782 params->ifindex = dev->ifindex;
5784 /* xdp and cls_bpf programs are run in RCU-bh so
5785 * rcu_read_lock_bh is not needed here
5787 if (likely(nhc->nhc_gw_family != AF_INET6)) {
5788 if (nhc->nhc_gw_family)
5789 params->ipv4_dst = nhc->nhc_gw.ipv4;
5791 neigh = __ipv4_neigh_lookup_noref(dev,
5792 (__force u32)params->ipv4_dst);
5794 struct in6_addr *dst = (struct in6_addr *)params->ipv6_dst;
5796 params->family = AF_INET6;
5797 *dst = nhc->nhc_gw.ipv6;
5798 neigh = __ipv6_neigh_lookup_noref_stub(dev, dst);
5802 return BPF_FIB_LKUP_RET_NO_NEIGH;
5804 return bpf_fib_set_fwd_params(params, neigh, dev, mtu);
5808 #if IS_ENABLED(CONFIG_IPV6)
5809 static int bpf_ipv6_fib_lookup(struct net *net, struct bpf_fib_lookup *params,
5810 u32 flags, bool check_mtu)
5812 struct in6_addr *src = (struct in6_addr *) params->ipv6_src;
5813 struct in6_addr *dst = (struct in6_addr *) params->ipv6_dst;
5814 struct fib6_result res = {};
5815 struct neighbour *neigh;
5816 struct net_device *dev;
5817 struct inet6_dev *idev;
5823 /* link local addresses are never forwarded */
5824 if (rt6_need_strict(dst) || rt6_need_strict(src))
5825 return BPF_FIB_LKUP_RET_NOT_FWDED;
5827 dev = dev_get_by_index_rcu(net, params->ifindex);
5831 idev = __in6_dev_get_safely(dev);
5832 if (unlikely(!idev || !idev->cnf.forwarding))
5833 return BPF_FIB_LKUP_RET_FWD_DISABLED;
5835 if (flags & BPF_FIB_LOOKUP_OUTPUT) {
5837 oif = fl6.flowi6_oif = params->ifindex;
5839 oif = fl6.flowi6_iif = params->ifindex;
5841 strict = RT6_LOOKUP_F_HAS_SADDR;
5843 fl6.flowlabel = params->flowinfo;
5844 fl6.flowi6_scope = 0;
5845 fl6.flowi6_flags = 0;
5848 fl6.flowi6_proto = params->l4_protocol;
5851 fl6.fl6_sport = params->sport;
5852 fl6.fl6_dport = params->dport;
5854 if (flags & BPF_FIB_LOOKUP_DIRECT) {
5855 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
5856 struct fib6_table *tb;
5858 tb = ipv6_stub->fib6_get_table(net, tbid);
5860 return BPF_FIB_LKUP_RET_NOT_FWDED;
5862 err = ipv6_stub->fib6_table_lookup(net, tb, oif, &fl6, &res,
5865 fl6.flowi6_mark = 0;
5866 fl6.flowi6_secid = 0;
5867 fl6.flowi6_tun_key.tun_id = 0;
5868 fl6.flowi6_uid = sock_net_uid(net, NULL);
5870 err = ipv6_stub->fib6_lookup(net, oif, &fl6, &res, strict);
5873 if (unlikely(err || IS_ERR_OR_NULL(res.f6i) ||
5874 res.f6i == net->ipv6.fib6_null_entry))
5875 return BPF_FIB_LKUP_RET_NOT_FWDED;
5877 switch (res.fib6_type) {
5878 /* only unicast is forwarded */
5882 return BPF_FIB_LKUP_RET_BLACKHOLE;
5883 case RTN_UNREACHABLE:
5884 return BPF_FIB_LKUP_RET_UNREACHABLE;
5886 return BPF_FIB_LKUP_RET_PROHIBIT;
5888 return BPF_FIB_LKUP_RET_NOT_FWDED;
5891 ipv6_stub->fib6_select_path(net, &res, &fl6, fl6.flowi6_oif,
5892 fl6.flowi6_oif != 0, NULL, strict);
5895 mtu = ipv6_stub->ip6_mtu_from_fib6(&res, dst, src);
5896 if (params->tot_len > mtu) {
5897 params->mtu_result = mtu; /* union with tot_len */
5898 return BPF_FIB_LKUP_RET_FRAG_NEEDED;
5902 if (res.nh->fib_nh_lws)
5903 return BPF_FIB_LKUP_RET_UNSUPP_LWT;
5905 if (res.nh->fib_nh_gw_family)
5906 *dst = res.nh->fib_nh_gw6;
5908 dev = res.nh->fib_nh_dev;
5909 params->rt_metric = res.f6i->fib6_metric;
5910 params->ifindex = dev->ifindex;
5912 /* xdp and cls_bpf programs are run in RCU-bh so rcu_read_lock_bh is
5915 neigh = __ipv6_neigh_lookup_noref_stub(dev, dst);
5917 return BPF_FIB_LKUP_RET_NO_NEIGH;
5919 return bpf_fib_set_fwd_params(params, neigh, dev, mtu);
5923 BPF_CALL_4(bpf_xdp_fib_lookup, struct xdp_buff *, ctx,
5924 struct bpf_fib_lookup *, params, int, plen, u32, flags)
5926 if (plen < sizeof(*params))
5929 if (flags & ~(BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_OUTPUT))
5932 switch (params->family) {
5933 #if IS_ENABLED(CONFIG_INET)
5935 return bpf_ipv4_fib_lookup(dev_net(ctx->rxq->dev), params,
5938 #if IS_ENABLED(CONFIG_IPV6)
5940 return bpf_ipv6_fib_lookup(dev_net(ctx->rxq->dev), params,
5944 return -EAFNOSUPPORT;
5947 static const struct bpf_func_proto bpf_xdp_fib_lookup_proto = {
5948 .func = bpf_xdp_fib_lookup,
5950 .ret_type = RET_INTEGER,
5951 .arg1_type = ARG_PTR_TO_CTX,
5952 .arg2_type = ARG_PTR_TO_MEM,
5953 .arg3_type = ARG_CONST_SIZE,
5954 .arg4_type = ARG_ANYTHING,
5957 BPF_CALL_4(bpf_skb_fib_lookup, struct sk_buff *, skb,
5958 struct bpf_fib_lookup *, params, int, plen, u32, flags)
5960 struct net *net = dev_net(skb->dev);
5961 int rc = -EAFNOSUPPORT;
5962 bool check_mtu = false;
5964 if (plen < sizeof(*params))
5967 if (flags & ~(BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_OUTPUT))
5970 if (params->tot_len)
5973 switch (params->family) {
5974 #if IS_ENABLED(CONFIG_INET)
5976 rc = bpf_ipv4_fib_lookup(net, params, flags, check_mtu);
5979 #if IS_ENABLED(CONFIG_IPV6)
5981 rc = bpf_ipv6_fib_lookup(net, params, flags, check_mtu);
5986 if (rc == BPF_FIB_LKUP_RET_SUCCESS && !check_mtu) {
5987 struct net_device *dev;
5989 /* When tot_len isn't provided by user, check skb
5990 * against MTU of FIB lookup resulting net_device
5992 dev = dev_get_by_index_rcu(net, params->ifindex);
5993 if (!is_skb_forwardable(dev, skb))
5994 rc = BPF_FIB_LKUP_RET_FRAG_NEEDED;
5996 params->mtu_result = dev->mtu; /* union with tot_len */
6002 static const struct bpf_func_proto bpf_skb_fib_lookup_proto = {
6003 .func = bpf_skb_fib_lookup,
6005 .ret_type = RET_INTEGER,
6006 .arg1_type = ARG_PTR_TO_CTX,
6007 .arg2_type = ARG_PTR_TO_MEM,
6008 .arg3_type = ARG_CONST_SIZE,
6009 .arg4_type = ARG_ANYTHING,
6012 static struct net_device *__dev_via_ifindex(struct net_device *dev_curr,
6015 struct net *netns = dev_net(dev_curr);
6017 /* Non-redirect use-cases can use ifindex=0 and save ifindex lookup */
6021 return dev_get_by_index_rcu(netns, ifindex);
6024 BPF_CALL_5(bpf_skb_check_mtu, struct sk_buff *, skb,
6025 u32, ifindex, u32 *, mtu_len, s32, len_diff, u64, flags)
6027 int ret = BPF_MTU_CHK_RET_FRAG_NEEDED;
6028 struct net_device *dev = skb->dev;
6029 int skb_len, dev_len;
6032 if (unlikely(flags & ~(BPF_MTU_CHK_SEGS)))
6035 if (unlikely(flags & BPF_MTU_CHK_SEGS && (len_diff || *mtu_len)))
6038 dev = __dev_via_ifindex(dev, ifindex);
6042 mtu = READ_ONCE(dev->mtu);
6044 dev_len = mtu + dev->hard_header_len;
6046 /* If set use *mtu_len as input, L3 as iph->tot_len (like fib_lookup) */
6047 skb_len = *mtu_len ? *mtu_len + dev->hard_header_len : skb->len;
6049 skb_len += len_diff; /* minus result pass check */
6050 if (skb_len <= dev_len) {
6051 ret = BPF_MTU_CHK_RET_SUCCESS;
6054 /* At this point, skb->len exceed MTU, but as it include length of all
6055 * segments, it can still be below MTU. The SKB can possibly get
6056 * re-segmented in transmit path (see validate_xmit_skb). Thus, user
6057 * must choose if segs are to be MTU checked.
6059 if (skb_is_gso(skb)) {
6060 ret = BPF_MTU_CHK_RET_SUCCESS;
6062 if (flags & BPF_MTU_CHK_SEGS &&
6063 !skb_gso_validate_network_len(skb, mtu))
6064 ret = BPF_MTU_CHK_RET_SEGS_TOOBIG;
6067 /* BPF verifier guarantees valid pointer */
6073 BPF_CALL_5(bpf_xdp_check_mtu, struct xdp_buff *, xdp,
6074 u32, ifindex, u32 *, mtu_len, s32, len_diff, u64, flags)
6076 struct net_device *dev = xdp->rxq->dev;
6077 int xdp_len = xdp->data_end - xdp->data;
6078 int ret = BPF_MTU_CHK_RET_SUCCESS;
6081 /* XDP variant doesn't support multi-buffer segment check (yet) */
6082 if (unlikely(flags))
6085 dev = __dev_via_ifindex(dev, ifindex);
6089 mtu = READ_ONCE(dev->mtu);
6091 /* Add L2-header as dev MTU is L3 size */
6092 dev_len = mtu + dev->hard_header_len;
6094 /* Use *mtu_len as input, L3 as iph->tot_len (like fib_lookup) */
6096 xdp_len = *mtu_len + dev->hard_header_len;
6098 xdp_len += len_diff; /* minus result pass check */
6099 if (xdp_len > dev_len)
6100 ret = BPF_MTU_CHK_RET_FRAG_NEEDED;
6102 /* BPF verifier guarantees valid pointer */
6108 static const struct bpf_func_proto bpf_skb_check_mtu_proto = {
6109 .func = bpf_skb_check_mtu,
6111 .ret_type = RET_INTEGER,
6112 .arg1_type = ARG_PTR_TO_CTX,
6113 .arg2_type = ARG_ANYTHING,
6114 .arg3_type = ARG_PTR_TO_INT,
6115 .arg4_type = ARG_ANYTHING,
6116 .arg5_type = ARG_ANYTHING,
6119 static const struct bpf_func_proto bpf_xdp_check_mtu_proto = {
6120 .func = bpf_xdp_check_mtu,
6122 .ret_type = RET_INTEGER,
6123 .arg1_type = ARG_PTR_TO_CTX,
6124 .arg2_type = ARG_ANYTHING,
6125 .arg3_type = ARG_PTR_TO_INT,
6126 .arg4_type = ARG_ANYTHING,
6127 .arg5_type = ARG_ANYTHING,
6130 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
6131 static int bpf_push_seg6_encap(struct sk_buff *skb, u32 type, void *hdr, u32 len)
6134 struct ipv6_sr_hdr *srh = (struct ipv6_sr_hdr *)hdr;
6136 if (!seg6_validate_srh(srh, len, false))
6140 case BPF_LWT_ENCAP_SEG6_INLINE:
6141 if (skb->protocol != htons(ETH_P_IPV6))
6144 err = seg6_do_srh_inline(skb, srh);
6146 case BPF_LWT_ENCAP_SEG6:
6147 skb_reset_inner_headers(skb);
6148 skb->encapsulation = 1;
6149 err = seg6_do_srh_encap(skb, srh, IPPROTO_IPV6);
6155 bpf_compute_data_pointers(skb);
6159 skb_set_transport_header(skb, sizeof(struct ipv6hdr));
6161 return seg6_lookup_nexthop(skb, NULL, 0);
6163 #endif /* CONFIG_IPV6_SEG6_BPF */
6165 #if IS_ENABLED(CONFIG_LWTUNNEL_BPF)
6166 static int bpf_push_ip_encap(struct sk_buff *skb, void *hdr, u32 len,
6169 return bpf_lwt_push_ip_encap(skb, hdr, len, ingress);
6173 BPF_CALL_4(bpf_lwt_in_push_encap, struct sk_buff *, skb, u32, type, void *, hdr,
6177 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
6178 case BPF_LWT_ENCAP_SEG6:
6179 case BPF_LWT_ENCAP_SEG6_INLINE:
6180 return bpf_push_seg6_encap(skb, type, hdr, len);
6182 #if IS_ENABLED(CONFIG_LWTUNNEL_BPF)
6183 case BPF_LWT_ENCAP_IP:
6184 return bpf_push_ip_encap(skb, hdr, len, true /* ingress */);
6191 BPF_CALL_4(bpf_lwt_xmit_push_encap, struct sk_buff *, skb, u32, type,
6192 void *, hdr, u32, len)
6195 #if IS_ENABLED(CONFIG_LWTUNNEL_BPF)
6196 case BPF_LWT_ENCAP_IP:
6197 return bpf_push_ip_encap(skb, hdr, len, false /* egress */);
6204 static const struct bpf_func_proto bpf_lwt_in_push_encap_proto = {
6205 .func = bpf_lwt_in_push_encap,
6207 .ret_type = RET_INTEGER,
6208 .arg1_type = ARG_PTR_TO_CTX,
6209 .arg2_type = ARG_ANYTHING,
6210 .arg3_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6211 .arg4_type = ARG_CONST_SIZE
6214 static const struct bpf_func_proto bpf_lwt_xmit_push_encap_proto = {
6215 .func = bpf_lwt_xmit_push_encap,
6217 .ret_type = RET_INTEGER,
6218 .arg1_type = ARG_PTR_TO_CTX,
6219 .arg2_type = ARG_ANYTHING,
6220 .arg3_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6221 .arg4_type = ARG_CONST_SIZE
6224 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
6225 BPF_CALL_4(bpf_lwt_seg6_store_bytes, struct sk_buff *, skb, u32, offset,
6226 const void *, from, u32, len)
6228 struct seg6_bpf_srh_state *srh_state =
6229 this_cpu_ptr(&seg6_bpf_srh_states);
6230 struct ipv6_sr_hdr *srh = srh_state->srh;
6231 void *srh_tlvs, *srh_end, *ptr;
6237 srh_tlvs = (void *)((char *)srh + ((srh->first_segment + 1) << 4));
6238 srh_end = (void *)((char *)srh + sizeof(*srh) + srh_state->hdrlen);
6240 ptr = skb->data + offset;
6241 if (ptr >= srh_tlvs && ptr + len <= srh_end)
6242 srh_state->valid = false;
6243 else if (ptr < (void *)&srh->flags ||
6244 ptr + len > (void *)&srh->segments)
6247 if (unlikely(bpf_try_make_writable(skb, offset + len)))
6249 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0)
6251 srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
6253 memcpy(skb->data + offset, from, len);
6257 static const struct bpf_func_proto bpf_lwt_seg6_store_bytes_proto = {
6258 .func = bpf_lwt_seg6_store_bytes,
6260 .ret_type = RET_INTEGER,
6261 .arg1_type = ARG_PTR_TO_CTX,
6262 .arg2_type = ARG_ANYTHING,
6263 .arg3_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6264 .arg4_type = ARG_CONST_SIZE
6267 static void bpf_update_srh_state(struct sk_buff *skb)
6269 struct seg6_bpf_srh_state *srh_state =
6270 this_cpu_ptr(&seg6_bpf_srh_states);
6273 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0) {
6274 srh_state->srh = NULL;
6276 srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
6277 srh_state->hdrlen = srh_state->srh->hdrlen << 3;
6278 srh_state->valid = true;
6282 BPF_CALL_4(bpf_lwt_seg6_action, struct sk_buff *, skb,
6283 u32, action, void *, param, u32, param_len)
6285 struct seg6_bpf_srh_state *srh_state =
6286 this_cpu_ptr(&seg6_bpf_srh_states);
6291 case SEG6_LOCAL_ACTION_END_X:
6292 if (!seg6_bpf_has_valid_srh(skb))
6294 if (param_len != sizeof(struct in6_addr))
6296 return seg6_lookup_nexthop(skb, (struct in6_addr *)param, 0);
6297 case SEG6_LOCAL_ACTION_END_T:
6298 if (!seg6_bpf_has_valid_srh(skb))
6300 if (param_len != sizeof(int))
6302 return seg6_lookup_nexthop(skb, NULL, *(int *)param);
6303 case SEG6_LOCAL_ACTION_END_DT6:
6304 if (!seg6_bpf_has_valid_srh(skb))
6306 if (param_len != sizeof(int))
6309 if (ipv6_find_hdr(skb, &hdroff, IPPROTO_IPV6, NULL, NULL) < 0)
6311 if (!pskb_pull(skb, hdroff))
6314 skb_postpull_rcsum(skb, skb_network_header(skb), hdroff);
6315 skb_reset_network_header(skb);
6316 skb_reset_transport_header(skb);
6317 skb->encapsulation = 0;
6319 bpf_compute_data_pointers(skb);
6320 bpf_update_srh_state(skb);
6321 return seg6_lookup_nexthop(skb, NULL, *(int *)param);
6322 case SEG6_LOCAL_ACTION_END_B6:
6323 if (srh_state->srh && !seg6_bpf_has_valid_srh(skb))
6325 err = bpf_push_seg6_encap(skb, BPF_LWT_ENCAP_SEG6_INLINE,
6328 bpf_update_srh_state(skb);
6331 case SEG6_LOCAL_ACTION_END_B6_ENCAP:
6332 if (srh_state->srh && !seg6_bpf_has_valid_srh(skb))
6334 err = bpf_push_seg6_encap(skb, BPF_LWT_ENCAP_SEG6,
6337 bpf_update_srh_state(skb);
6345 static const struct bpf_func_proto bpf_lwt_seg6_action_proto = {
6346 .func = bpf_lwt_seg6_action,
6348 .ret_type = RET_INTEGER,
6349 .arg1_type = ARG_PTR_TO_CTX,
6350 .arg2_type = ARG_ANYTHING,
6351 .arg3_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6352 .arg4_type = ARG_CONST_SIZE
6355 BPF_CALL_3(bpf_lwt_seg6_adjust_srh, struct sk_buff *, skb, u32, offset,
6358 struct seg6_bpf_srh_state *srh_state =
6359 this_cpu_ptr(&seg6_bpf_srh_states);
6360 struct ipv6_sr_hdr *srh = srh_state->srh;
6361 void *srh_end, *srh_tlvs, *ptr;
6362 struct ipv6hdr *hdr;
6366 if (unlikely(srh == NULL))
6369 srh_tlvs = (void *)((unsigned char *)srh + sizeof(*srh) +
6370 ((srh->first_segment + 1) << 4));
6371 srh_end = (void *)((unsigned char *)srh + sizeof(*srh) +
6373 ptr = skb->data + offset;
6375 if (unlikely(ptr < srh_tlvs || ptr > srh_end))
6377 if (unlikely(len < 0 && (void *)((char *)ptr - len) > srh_end))
6381 ret = skb_cow_head(skb, len);
6382 if (unlikely(ret < 0))
6385 ret = bpf_skb_net_hdr_push(skb, offset, len);
6387 ret = bpf_skb_net_hdr_pop(skb, offset, -1 * len);
6390 bpf_compute_data_pointers(skb);
6391 if (unlikely(ret < 0))
6394 hdr = (struct ipv6hdr *)skb->data;
6395 hdr->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
6397 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0)
6399 srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
6400 srh_state->hdrlen += len;
6401 srh_state->valid = false;
6405 static const struct bpf_func_proto bpf_lwt_seg6_adjust_srh_proto = {
6406 .func = bpf_lwt_seg6_adjust_srh,
6408 .ret_type = RET_INTEGER,
6409 .arg1_type = ARG_PTR_TO_CTX,
6410 .arg2_type = ARG_ANYTHING,
6411 .arg3_type = ARG_ANYTHING,
6413 #endif /* CONFIG_IPV6_SEG6_BPF */
6416 static struct sock *sk_lookup(struct net *net, struct bpf_sock_tuple *tuple,
6417 int dif, int sdif, u8 family, u8 proto)
6419 struct inet_hashinfo *hinfo = net->ipv4.tcp_death_row.hashinfo;
6420 bool refcounted = false;
6421 struct sock *sk = NULL;
6423 if (family == AF_INET) {
6424 __be32 src4 = tuple->ipv4.saddr;
6425 __be32 dst4 = tuple->ipv4.daddr;
6427 if (proto == IPPROTO_TCP)
6428 sk = __inet_lookup(net, hinfo, NULL, 0,
6429 src4, tuple->ipv4.sport,
6430 dst4, tuple->ipv4.dport,
6431 dif, sdif, &refcounted);
6433 sk = __udp4_lib_lookup(net, src4, tuple->ipv4.sport,
6434 dst4, tuple->ipv4.dport,
6435 dif, sdif, net->ipv4.udp_table, NULL);
6436 #if IS_ENABLED(CONFIG_IPV6)
6438 struct in6_addr *src6 = (struct in6_addr *)&tuple->ipv6.saddr;
6439 struct in6_addr *dst6 = (struct in6_addr *)&tuple->ipv6.daddr;
6441 if (proto == IPPROTO_TCP)
6442 sk = __inet6_lookup(net, hinfo, NULL, 0,
6443 src6, tuple->ipv6.sport,
6444 dst6, ntohs(tuple->ipv6.dport),
6445 dif, sdif, &refcounted);
6446 else if (likely(ipv6_bpf_stub))
6447 sk = ipv6_bpf_stub->udp6_lib_lookup(net,
6448 src6, tuple->ipv6.sport,
6449 dst6, tuple->ipv6.dport,
6451 net->ipv4.udp_table, NULL);
6455 if (unlikely(sk && !refcounted && !sock_flag(sk, SOCK_RCU_FREE))) {
6456 WARN_ONCE(1, "Found non-RCU, unreferenced socket!");
6462 /* bpf_skc_lookup performs the core lookup for different types of sockets,
6463 * taking a reference on the socket if it doesn't have the flag SOCK_RCU_FREE.
6465 static struct sock *
6466 __bpf_skc_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
6467 struct net *caller_net, u32 ifindex, u8 proto, u64 netns_id,
6470 struct sock *sk = NULL;
6475 if (len == sizeof(tuple->ipv4))
6477 else if (len == sizeof(tuple->ipv6))
6482 if (unlikely(flags || !((s32)netns_id < 0 || netns_id <= S32_MAX)))
6485 if (family == AF_INET)
6486 sdif = inet_sdif(skb);
6488 sdif = inet6_sdif(skb);
6490 if ((s32)netns_id < 0) {
6492 sk = sk_lookup(net, tuple, ifindex, sdif, family, proto);
6494 net = get_net_ns_by_id(caller_net, netns_id);
6497 sk = sk_lookup(net, tuple, ifindex, sdif, family, proto);
6505 static struct sock *
6506 __bpf_sk_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
6507 struct net *caller_net, u32 ifindex, u8 proto, u64 netns_id,
6510 struct sock *sk = __bpf_skc_lookup(skb, tuple, len, caller_net,
6511 ifindex, proto, netns_id, flags);
6514 struct sock *sk2 = sk_to_full_sk(sk);
6516 /* sk_to_full_sk() may return (sk)->rsk_listener, so make sure the original sk
6517 * sock refcnt is decremented to prevent a request_sock leak.
6519 if (!sk_fullsock(sk2))
6523 /* Ensure there is no need to bump sk2 refcnt */
6524 if (unlikely(sk2 && !sock_flag(sk2, SOCK_RCU_FREE))) {
6525 WARN_ONCE(1, "Found non-RCU, unreferenced socket!");
6535 static struct sock *
6536 bpf_skc_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
6537 u8 proto, u64 netns_id, u64 flags)
6539 struct net *caller_net;
6543 caller_net = dev_net(skb->dev);
6544 ifindex = skb->dev->ifindex;
6546 caller_net = sock_net(skb->sk);
6550 return __bpf_skc_lookup(skb, tuple, len, caller_net, ifindex, proto,
6554 static struct sock *
6555 bpf_sk_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
6556 u8 proto, u64 netns_id, u64 flags)
6558 struct sock *sk = bpf_skc_lookup(skb, tuple, len, proto, netns_id,
6562 struct sock *sk2 = sk_to_full_sk(sk);
6564 /* sk_to_full_sk() may return (sk)->rsk_listener, so make sure the original sk
6565 * sock refcnt is decremented to prevent a request_sock leak.
6567 if (!sk_fullsock(sk2))
6571 /* Ensure there is no need to bump sk2 refcnt */
6572 if (unlikely(sk2 && !sock_flag(sk2, SOCK_RCU_FREE))) {
6573 WARN_ONCE(1, "Found non-RCU, unreferenced socket!");
6583 BPF_CALL_5(bpf_skc_lookup_tcp, struct sk_buff *, skb,
6584 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
6586 return (unsigned long)bpf_skc_lookup(skb, tuple, len, IPPROTO_TCP,
6590 static const struct bpf_func_proto bpf_skc_lookup_tcp_proto = {
6591 .func = bpf_skc_lookup_tcp,
6594 .ret_type = RET_PTR_TO_SOCK_COMMON_OR_NULL,
6595 .arg1_type = ARG_PTR_TO_CTX,
6596 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6597 .arg3_type = ARG_CONST_SIZE,
6598 .arg4_type = ARG_ANYTHING,
6599 .arg5_type = ARG_ANYTHING,
6602 BPF_CALL_5(bpf_sk_lookup_tcp, struct sk_buff *, skb,
6603 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
6605 return (unsigned long)bpf_sk_lookup(skb, tuple, len, IPPROTO_TCP,
6609 static const struct bpf_func_proto bpf_sk_lookup_tcp_proto = {
6610 .func = bpf_sk_lookup_tcp,
6613 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
6614 .arg1_type = ARG_PTR_TO_CTX,
6615 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6616 .arg3_type = ARG_CONST_SIZE,
6617 .arg4_type = ARG_ANYTHING,
6618 .arg5_type = ARG_ANYTHING,
6621 BPF_CALL_5(bpf_sk_lookup_udp, struct sk_buff *, skb,
6622 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
6624 return (unsigned long)bpf_sk_lookup(skb, tuple, len, IPPROTO_UDP,
6628 static const struct bpf_func_proto bpf_sk_lookup_udp_proto = {
6629 .func = bpf_sk_lookup_udp,
6632 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
6633 .arg1_type = ARG_PTR_TO_CTX,
6634 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6635 .arg3_type = ARG_CONST_SIZE,
6636 .arg4_type = ARG_ANYTHING,
6637 .arg5_type = ARG_ANYTHING,
6640 BPF_CALL_1(bpf_sk_release, struct sock *, sk)
6642 if (sk && sk_is_refcounted(sk))
6647 static const struct bpf_func_proto bpf_sk_release_proto = {
6648 .func = bpf_sk_release,
6650 .ret_type = RET_INTEGER,
6651 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON | OBJ_RELEASE,
6654 BPF_CALL_5(bpf_xdp_sk_lookup_udp, struct xdp_buff *, ctx,
6655 struct bpf_sock_tuple *, tuple, u32, len, u32, netns_id, u64, flags)
6657 struct net *caller_net = dev_net(ctx->rxq->dev);
6658 int ifindex = ctx->rxq->dev->ifindex;
6660 return (unsigned long)__bpf_sk_lookup(NULL, tuple, len, caller_net,
6661 ifindex, IPPROTO_UDP, netns_id,
6665 static const struct bpf_func_proto bpf_xdp_sk_lookup_udp_proto = {
6666 .func = bpf_xdp_sk_lookup_udp,
6669 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
6670 .arg1_type = ARG_PTR_TO_CTX,
6671 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6672 .arg3_type = ARG_CONST_SIZE,
6673 .arg4_type = ARG_ANYTHING,
6674 .arg5_type = ARG_ANYTHING,
6677 BPF_CALL_5(bpf_xdp_skc_lookup_tcp, struct xdp_buff *, ctx,
6678 struct bpf_sock_tuple *, tuple, u32, len, u32, netns_id, u64, flags)
6680 struct net *caller_net = dev_net(ctx->rxq->dev);
6681 int ifindex = ctx->rxq->dev->ifindex;
6683 return (unsigned long)__bpf_skc_lookup(NULL, tuple, len, caller_net,
6684 ifindex, IPPROTO_TCP, netns_id,
6688 static const struct bpf_func_proto bpf_xdp_skc_lookup_tcp_proto = {
6689 .func = bpf_xdp_skc_lookup_tcp,
6692 .ret_type = RET_PTR_TO_SOCK_COMMON_OR_NULL,
6693 .arg1_type = ARG_PTR_TO_CTX,
6694 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6695 .arg3_type = ARG_CONST_SIZE,
6696 .arg4_type = ARG_ANYTHING,
6697 .arg5_type = ARG_ANYTHING,
6700 BPF_CALL_5(bpf_xdp_sk_lookup_tcp, struct xdp_buff *, ctx,
6701 struct bpf_sock_tuple *, tuple, u32, len, u32, netns_id, u64, flags)
6703 struct net *caller_net = dev_net(ctx->rxq->dev);
6704 int ifindex = ctx->rxq->dev->ifindex;
6706 return (unsigned long)__bpf_sk_lookup(NULL, tuple, len, caller_net,
6707 ifindex, IPPROTO_TCP, netns_id,
6711 static const struct bpf_func_proto bpf_xdp_sk_lookup_tcp_proto = {
6712 .func = bpf_xdp_sk_lookup_tcp,
6715 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
6716 .arg1_type = ARG_PTR_TO_CTX,
6717 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6718 .arg3_type = ARG_CONST_SIZE,
6719 .arg4_type = ARG_ANYTHING,
6720 .arg5_type = ARG_ANYTHING,
6723 BPF_CALL_5(bpf_sock_addr_skc_lookup_tcp, struct bpf_sock_addr_kern *, ctx,
6724 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
6726 return (unsigned long)__bpf_skc_lookup(NULL, tuple, len,
6727 sock_net(ctx->sk), 0,
6728 IPPROTO_TCP, netns_id, flags);
6731 static const struct bpf_func_proto bpf_sock_addr_skc_lookup_tcp_proto = {
6732 .func = bpf_sock_addr_skc_lookup_tcp,
6734 .ret_type = RET_PTR_TO_SOCK_COMMON_OR_NULL,
6735 .arg1_type = ARG_PTR_TO_CTX,
6736 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6737 .arg3_type = ARG_CONST_SIZE,
6738 .arg4_type = ARG_ANYTHING,
6739 .arg5_type = ARG_ANYTHING,
6742 BPF_CALL_5(bpf_sock_addr_sk_lookup_tcp, struct bpf_sock_addr_kern *, ctx,
6743 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
6745 return (unsigned long)__bpf_sk_lookup(NULL, tuple, len,
6746 sock_net(ctx->sk), 0, IPPROTO_TCP,
6750 static const struct bpf_func_proto bpf_sock_addr_sk_lookup_tcp_proto = {
6751 .func = bpf_sock_addr_sk_lookup_tcp,
6753 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
6754 .arg1_type = ARG_PTR_TO_CTX,
6755 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6756 .arg3_type = ARG_CONST_SIZE,
6757 .arg4_type = ARG_ANYTHING,
6758 .arg5_type = ARG_ANYTHING,
6761 BPF_CALL_5(bpf_sock_addr_sk_lookup_udp, struct bpf_sock_addr_kern *, ctx,
6762 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
6764 return (unsigned long)__bpf_sk_lookup(NULL, tuple, len,
6765 sock_net(ctx->sk), 0, IPPROTO_UDP,
6769 static const struct bpf_func_proto bpf_sock_addr_sk_lookup_udp_proto = {
6770 .func = bpf_sock_addr_sk_lookup_udp,
6772 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
6773 .arg1_type = ARG_PTR_TO_CTX,
6774 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
6775 .arg3_type = ARG_CONST_SIZE,
6776 .arg4_type = ARG_ANYTHING,
6777 .arg5_type = ARG_ANYTHING,
6780 bool bpf_tcp_sock_is_valid_access(int off, int size, enum bpf_access_type type,
6781 struct bpf_insn_access_aux *info)
6783 if (off < 0 || off >= offsetofend(struct bpf_tcp_sock,
6787 if (off % size != 0)
6791 case offsetof(struct bpf_tcp_sock, bytes_received):
6792 case offsetof(struct bpf_tcp_sock, bytes_acked):
6793 return size == sizeof(__u64);
6795 return size == sizeof(__u32);
6799 u32 bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type,
6800 const struct bpf_insn *si,
6801 struct bpf_insn *insn_buf,
6802 struct bpf_prog *prog, u32 *target_size)
6804 struct bpf_insn *insn = insn_buf;
6806 #define BPF_TCP_SOCK_GET_COMMON(FIELD) \
6808 BUILD_BUG_ON(sizeof_field(struct tcp_sock, FIELD) > \
6809 sizeof_field(struct bpf_tcp_sock, FIELD)); \
6810 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct tcp_sock, FIELD),\
6811 si->dst_reg, si->src_reg, \
6812 offsetof(struct tcp_sock, FIELD)); \
6815 #define BPF_INET_SOCK_GET_COMMON(FIELD) \
6817 BUILD_BUG_ON(sizeof_field(struct inet_connection_sock, \
6819 sizeof_field(struct bpf_tcp_sock, FIELD)); \
6820 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
6821 struct inet_connection_sock, \
6823 si->dst_reg, si->src_reg, \
6825 struct inet_connection_sock, \
6829 if (insn > insn_buf)
6830 return insn - insn_buf;
6833 case offsetof(struct bpf_tcp_sock, rtt_min):
6834 BUILD_BUG_ON(sizeof_field(struct tcp_sock, rtt_min) !=
6835 sizeof(struct minmax));
6836 BUILD_BUG_ON(sizeof(struct minmax) <
6837 sizeof(struct minmax_sample));
6839 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6840 offsetof(struct tcp_sock, rtt_min) +
6841 offsetof(struct minmax_sample, v));
6843 case offsetof(struct bpf_tcp_sock, snd_cwnd):
6844 BPF_TCP_SOCK_GET_COMMON(snd_cwnd);
6846 case offsetof(struct bpf_tcp_sock, srtt_us):
6847 BPF_TCP_SOCK_GET_COMMON(srtt_us);
6849 case offsetof(struct bpf_tcp_sock, snd_ssthresh):
6850 BPF_TCP_SOCK_GET_COMMON(snd_ssthresh);
6852 case offsetof(struct bpf_tcp_sock, rcv_nxt):
6853 BPF_TCP_SOCK_GET_COMMON(rcv_nxt);
6855 case offsetof(struct bpf_tcp_sock, snd_nxt):
6856 BPF_TCP_SOCK_GET_COMMON(snd_nxt);
6858 case offsetof(struct bpf_tcp_sock, snd_una):
6859 BPF_TCP_SOCK_GET_COMMON(snd_una);
6861 case offsetof(struct bpf_tcp_sock, mss_cache):
6862 BPF_TCP_SOCK_GET_COMMON(mss_cache);
6864 case offsetof(struct bpf_tcp_sock, ecn_flags):
6865 BPF_TCP_SOCK_GET_COMMON(ecn_flags);
6867 case offsetof(struct bpf_tcp_sock, rate_delivered):
6868 BPF_TCP_SOCK_GET_COMMON(rate_delivered);
6870 case offsetof(struct bpf_tcp_sock, rate_interval_us):
6871 BPF_TCP_SOCK_GET_COMMON(rate_interval_us);
6873 case offsetof(struct bpf_tcp_sock, packets_out):
6874 BPF_TCP_SOCK_GET_COMMON(packets_out);
6876 case offsetof(struct bpf_tcp_sock, retrans_out):
6877 BPF_TCP_SOCK_GET_COMMON(retrans_out);
6879 case offsetof(struct bpf_tcp_sock, total_retrans):
6880 BPF_TCP_SOCK_GET_COMMON(total_retrans);
6882 case offsetof(struct bpf_tcp_sock, segs_in):
6883 BPF_TCP_SOCK_GET_COMMON(segs_in);
6885 case offsetof(struct bpf_tcp_sock, data_segs_in):
6886 BPF_TCP_SOCK_GET_COMMON(data_segs_in);
6888 case offsetof(struct bpf_tcp_sock, segs_out):
6889 BPF_TCP_SOCK_GET_COMMON(segs_out);
6891 case offsetof(struct bpf_tcp_sock, data_segs_out):
6892 BPF_TCP_SOCK_GET_COMMON(data_segs_out);
6894 case offsetof(struct bpf_tcp_sock, lost_out):
6895 BPF_TCP_SOCK_GET_COMMON(lost_out);
6897 case offsetof(struct bpf_tcp_sock, sacked_out):
6898 BPF_TCP_SOCK_GET_COMMON(sacked_out);
6900 case offsetof(struct bpf_tcp_sock, bytes_received):
6901 BPF_TCP_SOCK_GET_COMMON(bytes_received);
6903 case offsetof(struct bpf_tcp_sock, bytes_acked):
6904 BPF_TCP_SOCK_GET_COMMON(bytes_acked);
6906 case offsetof(struct bpf_tcp_sock, dsack_dups):
6907 BPF_TCP_SOCK_GET_COMMON(dsack_dups);
6909 case offsetof(struct bpf_tcp_sock, delivered):
6910 BPF_TCP_SOCK_GET_COMMON(delivered);
6912 case offsetof(struct bpf_tcp_sock, delivered_ce):
6913 BPF_TCP_SOCK_GET_COMMON(delivered_ce);
6915 case offsetof(struct bpf_tcp_sock, icsk_retransmits):
6916 BPF_INET_SOCK_GET_COMMON(icsk_retransmits);
6920 return insn - insn_buf;
6923 BPF_CALL_1(bpf_tcp_sock, struct sock *, sk)
6925 if (sk_fullsock(sk) && sk->sk_protocol == IPPROTO_TCP)
6926 return (unsigned long)sk;
6928 return (unsigned long)NULL;
6931 const struct bpf_func_proto bpf_tcp_sock_proto = {
6932 .func = bpf_tcp_sock,
6934 .ret_type = RET_PTR_TO_TCP_SOCK_OR_NULL,
6935 .arg1_type = ARG_PTR_TO_SOCK_COMMON,
6938 BPF_CALL_1(bpf_get_listener_sock, struct sock *, sk)
6940 sk = sk_to_full_sk(sk);
6942 if (sk->sk_state == TCP_LISTEN && sock_flag(sk, SOCK_RCU_FREE))
6943 return (unsigned long)sk;
6945 return (unsigned long)NULL;
6948 static const struct bpf_func_proto bpf_get_listener_sock_proto = {
6949 .func = bpf_get_listener_sock,
6951 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
6952 .arg1_type = ARG_PTR_TO_SOCK_COMMON,
6955 BPF_CALL_1(bpf_skb_ecn_set_ce, struct sk_buff *, skb)
6957 unsigned int iphdr_len;
6959 switch (skb_protocol(skb, true)) {
6960 case cpu_to_be16(ETH_P_IP):
6961 iphdr_len = sizeof(struct iphdr);
6963 case cpu_to_be16(ETH_P_IPV6):
6964 iphdr_len = sizeof(struct ipv6hdr);
6970 if (skb_headlen(skb) < iphdr_len)
6973 if (skb_cloned(skb) && !skb_clone_writable(skb, iphdr_len))
6976 return INET_ECN_set_ce(skb);
6979 bool bpf_xdp_sock_is_valid_access(int off, int size, enum bpf_access_type type,
6980 struct bpf_insn_access_aux *info)
6982 if (off < 0 || off >= offsetofend(struct bpf_xdp_sock, queue_id))
6985 if (off % size != 0)
6990 return size == sizeof(__u32);
6994 u32 bpf_xdp_sock_convert_ctx_access(enum bpf_access_type type,
6995 const struct bpf_insn *si,
6996 struct bpf_insn *insn_buf,
6997 struct bpf_prog *prog, u32 *target_size)
6999 struct bpf_insn *insn = insn_buf;
7001 #define BPF_XDP_SOCK_GET(FIELD) \
7003 BUILD_BUG_ON(sizeof_field(struct xdp_sock, FIELD) > \
7004 sizeof_field(struct bpf_xdp_sock, FIELD)); \
7005 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_sock, FIELD),\
7006 si->dst_reg, si->src_reg, \
7007 offsetof(struct xdp_sock, FIELD)); \
7011 case offsetof(struct bpf_xdp_sock, queue_id):
7012 BPF_XDP_SOCK_GET(queue_id);
7016 return insn - insn_buf;
7019 static const struct bpf_func_proto bpf_skb_ecn_set_ce_proto = {
7020 .func = bpf_skb_ecn_set_ce,
7022 .ret_type = RET_INTEGER,
7023 .arg1_type = ARG_PTR_TO_CTX,
7026 BPF_CALL_5(bpf_tcp_check_syncookie, struct sock *, sk, void *, iph, u32, iph_len,
7027 struct tcphdr *, th, u32, th_len)
7029 #ifdef CONFIG_SYN_COOKIES
7033 if (unlikely(!sk || th_len < sizeof(*th)))
7036 /* sk_listener() allows TCP_NEW_SYN_RECV, which makes no sense here. */
7037 if (sk->sk_protocol != IPPROTO_TCP || sk->sk_state != TCP_LISTEN)
7040 if (!READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_syncookies))
7043 if (!th->ack || th->rst || th->syn)
7046 if (unlikely(iph_len < sizeof(struct iphdr)))
7049 if (tcp_synq_no_recent_overflow(sk))
7052 cookie = ntohl(th->ack_seq) - 1;
7054 /* Both struct iphdr and struct ipv6hdr have the version field at the
7055 * same offset so we can cast to the shorter header (struct iphdr).
7057 switch (((struct iphdr *)iph)->version) {
7059 if (sk->sk_family == AF_INET6 && ipv6_only_sock(sk))
7062 ret = __cookie_v4_check((struct iphdr *)iph, th, cookie);
7065 #if IS_BUILTIN(CONFIG_IPV6)
7067 if (unlikely(iph_len < sizeof(struct ipv6hdr)))
7070 if (sk->sk_family != AF_INET6)
7073 ret = __cookie_v6_check((struct ipv6hdr *)iph, th, cookie);
7075 #endif /* CONFIG_IPV6 */
7078 return -EPROTONOSUPPORT;
7090 static const struct bpf_func_proto bpf_tcp_check_syncookie_proto = {
7091 .func = bpf_tcp_check_syncookie,
7094 .ret_type = RET_INTEGER,
7095 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
7096 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
7097 .arg3_type = ARG_CONST_SIZE,
7098 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
7099 .arg5_type = ARG_CONST_SIZE,
7102 BPF_CALL_5(bpf_tcp_gen_syncookie, struct sock *, sk, void *, iph, u32, iph_len,
7103 struct tcphdr *, th, u32, th_len)
7105 #ifdef CONFIG_SYN_COOKIES
7109 if (unlikely(!sk || th_len < sizeof(*th) || th_len != th->doff * 4))
7112 if (sk->sk_protocol != IPPROTO_TCP || sk->sk_state != TCP_LISTEN)
7115 if (!READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_syncookies))
7118 if (!th->syn || th->ack || th->fin || th->rst)
7121 if (unlikely(iph_len < sizeof(struct iphdr)))
7124 /* Both struct iphdr and struct ipv6hdr have the version field at the
7125 * same offset so we can cast to the shorter header (struct iphdr).
7127 switch (((struct iphdr *)iph)->version) {
7129 if (sk->sk_family == AF_INET6 && ipv6_only_sock(sk))
7132 mss = tcp_v4_get_syncookie(sk, iph, th, &cookie);
7135 #if IS_BUILTIN(CONFIG_IPV6)
7137 if (unlikely(iph_len < sizeof(struct ipv6hdr)))
7140 if (sk->sk_family != AF_INET6)
7143 mss = tcp_v6_get_syncookie(sk, iph, th, &cookie);
7145 #endif /* CONFIG_IPV6 */
7148 return -EPROTONOSUPPORT;
7153 return cookie | ((u64)mss << 32);
7156 #endif /* CONFIG_SYN_COOKIES */
7159 static const struct bpf_func_proto bpf_tcp_gen_syncookie_proto = {
7160 .func = bpf_tcp_gen_syncookie,
7161 .gpl_only = true, /* __cookie_v*_init_sequence() is GPL */
7163 .ret_type = RET_INTEGER,
7164 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
7165 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
7166 .arg3_type = ARG_CONST_SIZE,
7167 .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
7168 .arg5_type = ARG_CONST_SIZE,
7171 BPF_CALL_3(bpf_sk_assign, struct sk_buff *, skb, struct sock *, sk, u64, flags)
7173 if (!sk || flags != 0)
7175 if (!skb_at_tc_ingress(skb))
7177 if (unlikely(dev_net(skb->dev) != sock_net(sk)))
7178 return -ENETUNREACH;
7179 if (unlikely(sk_fullsock(sk) && sk->sk_reuseport))
7180 return -ESOCKTNOSUPPORT;
7181 if (sk_is_refcounted(sk) &&
7182 unlikely(!refcount_inc_not_zero(&sk->sk_refcnt)))
7187 skb->destructor = sock_pfree;
7192 static const struct bpf_func_proto bpf_sk_assign_proto = {
7193 .func = bpf_sk_assign,
7195 .ret_type = RET_INTEGER,
7196 .arg1_type = ARG_PTR_TO_CTX,
7197 .arg2_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
7198 .arg3_type = ARG_ANYTHING,
7201 static const u8 *bpf_search_tcp_opt(const u8 *op, const u8 *opend,
7202 u8 search_kind, const u8 *magic,
7203 u8 magic_len, bool *eol)
7209 while (op < opend) {
7212 if (kind == TCPOPT_EOL) {
7214 return ERR_PTR(-ENOMSG);
7215 } else if (kind == TCPOPT_NOP) {
7220 if (opend - op < 2 || opend - op < op[1] || op[1] < 2)
7221 /* Something is wrong in the received header.
7222 * Follow the TCP stack's tcp_parse_options()
7223 * and just bail here.
7225 return ERR_PTR(-EFAULT);
7228 if (search_kind == kind) {
7232 if (magic_len > kind_len - 2)
7233 return ERR_PTR(-ENOMSG);
7235 if (!memcmp(&op[2], magic, magic_len))
7242 return ERR_PTR(-ENOMSG);
7245 BPF_CALL_4(bpf_sock_ops_load_hdr_opt, struct bpf_sock_ops_kern *, bpf_sock,
7246 void *, search_res, u32, len, u64, flags)
7248 bool eol, load_syn = flags & BPF_LOAD_HDR_OPT_TCP_SYN;
7249 const u8 *op, *opend, *magic, *search = search_res;
7250 u8 search_kind, search_len, copy_len, magic_len;
7253 /* 2 byte is the minimal option len except TCPOPT_NOP and
7254 * TCPOPT_EOL which are useless for the bpf prog to learn
7255 * and this helper disallow loading them also.
7257 if (len < 2 || flags & ~BPF_LOAD_HDR_OPT_TCP_SYN)
7260 search_kind = search[0];
7261 search_len = search[1];
7263 if (search_len > len || search_kind == TCPOPT_NOP ||
7264 search_kind == TCPOPT_EOL)
7267 if (search_kind == TCPOPT_EXP || search_kind == 253) {
7268 /* 16 or 32 bit magic. +2 for kind and kind length */
7269 if (search_len != 4 && search_len != 6)
7272 magic_len = search_len - 2;
7281 ret = bpf_sock_ops_get_syn(bpf_sock, TCP_BPF_SYN, &op);
7286 op += sizeof(struct tcphdr);
7288 if (!bpf_sock->skb ||
7289 bpf_sock->op == BPF_SOCK_OPS_HDR_OPT_LEN_CB)
7290 /* This bpf_sock->op cannot call this helper */
7293 opend = bpf_sock->skb_data_end;
7294 op = bpf_sock->skb->data + sizeof(struct tcphdr);
7297 op = bpf_search_tcp_opt(op, opend, search_kind, magic, magic_len,
7304 if (copy_len > len) {
7309 memcpy(search_res, op, copy_len);
7313 static const struct bpf_func_proto bpf_sock_ops_load_hdr_opt_proto = {
7314 .func = bpf_sock_ops_load_hdr_opt,
7316 .ret_type = RET_INTEGER,
7317 .arg1_type = ARG_PTR_TO_CTX,
7318 .arg2_type = ARG_PTR_TO_MEM,
7319 .arg3_type = ARG_CONST_SIZE,
7320 .arg4_type = ARG_ANYTHING,
7323 BPF_CALL_4(bpf_sock_ops_store_hdr_opt, struct bpf_sock_ops_kern *, bpf_sock,
7324 const void *, from, u32, len, u64, flags)
7326 u8 new_kind, new_kind_len, magic_len = 0, *opend;
7327 const u8 *op, *new_op, *magic = NULL;
7328 struct sk_buff *skb;
7331 if (bpf_sock->op != BPF_SOCK_OPS_WRITE_HDR_OPT_CB)
7334 if (len < 2 || flags)
7338 new_kind = new_op[0];
7339 new_kind_len = new_op[1];
7341 if (new_kind_len > len || new_kind == TCPOPT_NOP ||
7342 new_kind == TCPOPT_EOL)
7345 if (new_kind_len > bpf_sock->remaining_opt_len)
7348 /* 253 is another experimental kind */
7349 if (new_kind == TCPOPT_EXP || new_kind == 253) {
7350 if (new_kind_len < 4)
7352 /* Match for the 2 byte magic also.
7353 * RFC 6994: the magic could be 2 or 4 bytes.
7354 * Hence, matching by 2 byte only is on the
7355 * conservative side but it is the right
7356 * thing to do for the 'search-for-duplication'
7363 /* Check for duplication */
7364 skb = bpf_sock->skb;
7365 op = skb->data + sizeof(struct tcphdr);
7366 opend = bpf_sock->skb_data_end;
7368 op = bpf_search_tcp_opt(op, opend, new_kind, magic, magic_len,
7373 if (PTR_ERR(op) != -ENOMSG)
7377 /* The option has been ended. Treat it as no more
7378 * header option can be written.
7382 /* No duplication found. Store the header option. */
7383 memcpy(opend, from, new_kind_len);
7385 bpf_sock->remaining_opt_len -= new_kind_len;
7386 bpf_sock->skb_data_end += new_kind_len;
7391 static const struct bpf_func_proto bpf_sock_ops_store_hdr_opt_proto = {
7392 .func = bpf_sock_ops_store_hdr_opt,
7394 .ret_type = RET_INTEGER,
7395 .arg1_type = ARG_PTR_TO_CTX,
7396 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
7397 .arg3_type = ARG_CONST_SIZE,
7398 .arg4_type = ARG_ANYTHING,
7401 BPF_CALL_3(bpf_sock_ops_reserve_hdr_opt, struct bpf_sock_ops_kern *, bpf_sock,
7402 u32, len, u64, flags)
7404 if (bpf_sock->op != BPF_SOCK_OPS_HDR_OPT_LEN_CB)
7407 if (flags || len < 2)
7410 if (len > bpf_sock->remaining_opt_len)
7413 bpf_sock->remaining_opt_len -= len;
7418 static const struct bpf_func_proto bpf_sock_ops_reserve_hdr_opt_proto = {
7419 .func = bpf_sock_ops_reserve_hdr_opt,
7421 .ret_type = RET_INTEGER,
7422 .arg1_type = ARG_PTR_TO_CTX,
7423 .arg2_type = ARG_ANYTHING,
7424 .arg3_type = ARG_ANYTHING,
7427 BPF_CALL_3(bpf_skb_set_tstamp, struct sk_buff *, skb,
7428 u64, tstamp, u32, tstamp_type)
7430 /* skb_clear_delivery_time() is done for inet protocol */
7431 if (skb->protocol != htons(ETH_P_IP) &&
7432 skb->protocol != htons(ETH_P_IPV6))
7435 switch (tstamp_type) {
7436 case BPF_SKB_TSTAMP_DELIVERY_MONO:
7439 skb->tstamp = tstamp;
7440 skb->mono_delivery_time = 1;
7442 case BPF_SKB_TSTAMP_UNSPEC:
7446 skb->mono_delivery_time = 0;
7455 static const struct bpf_func_proto bpf_skb_set_tstamp_proto = {
7456 .func = bpf_skb_set_tstamp,
7458 .ret_type = RET_INTEGER,
7459 .arg1_type = ARG_PTR_TO_CTX,
7460 .arg2_type = ARG_ANYTHING,
7461 .arg3_type = ARG_ANYTHING,
7464 #ifdef CONFIG_SYN_COOKIES
7465 BPF_CALL_3(bpf_tcp_raw_gen_syncookie_ipv4, struct iphdr *, iph,
7466 struct tcphdr *, th, u32, th_len)
7471 if (unlikely(th_len < sizeof(*th) || th_len != th->doff * 4))
7474 mss = tcp_parse_mss_option(th, 0) ?: TCP_MSS_DEFAULT;
7475 cookie = __cookie_v4_init_sequence(iph, th, &mss);
7477 return cookie | ((u64)mss << 32);
7480 static const struct bpf_func_proto bpf_tcp_raw_gen_syncookie_ipv4_proto = {
7481 .func = bpf_tcp_raw_gen_syncookie_ipv4,
7482 .gpl_only = true, /* __cookie_v4_init_sequence() is GPL */
7484 .ret_type = RET_INTEGER,
7485 .arg1_type = ARG_PTR_TO_FIXED_SIZE_MEM,
7486 .arg1_size = sizeof(struct iphdr),
7487 .arg2_type = ARG_PTR_TO_MEM,
7488 .arg3_type = ARG_CONST_SIZE,
7491 BPF_CALL_3(bpf_tcp_raw_gen_syncookie_ipv6, struct ipv6hdr *, iph,
7492 struct tcphdr *, th, u32, th_len)
7494 #if IS_BUILTIN(CONFIG_IPV6)
7495 const u16 mss_clamp = IPV6_MIN_MTU - sizeof(struct tcphdr) -
7496 sizeof(struct ipv6hdr);
7500 if (unlikely(th_len < sizeof(*th) || th_len != th->doff * 4))
7503 mss = tcp_parse_mss_option(th, 0) ?: mss_clamp;
7504 cookie = __cookie_v6_init_sequence(iph, th, &mss);
7506 return cookie | ((u64)mss << 32);
7508 return -EPROTONOSUPPORT;
7512 static const struct bpf_func_proto bpf_tcp_raw_gen_syncookie_ipv6_proto = {
7513 .func = bpf_tcp_raw_gen_syncookie_ipv6,
7514 .gpl_only = true, /* __cookie_v6_init_sequence() is GPL */
7516 .ret_type = RET_INTEGER,
7517 .arg1_type = ARG_PTR_TO_FIXED_SIZE_MEM,
7518 .arg1_size = sizeof(struct ipv6hdr),
7519 .arg2_type = ARG_PTR_TO_MEM,
7520 .arg3_type = ARG_CONST_SIZE,
7523 BPF_CALL_2(bpf_tcp_raw_check_syncookie_ipv4, struct iphdr *, iph,
7524 struct tcphdr *, th)
7526 u32 cookie = ntohl(th->ack_seq) - 1;
7528 if (__cookie_v4_check(iph, th, cookie) > 0)
7534 static const struct bpf_func_proto bpf_tcp_raw_check_syncookie_ipv4_proto = {
7535 .func = bpf_tcp_raw_check_syncookie_ipv4,
7536 .gpl_only = true, /* __cookie_v4_check is GPL */
7538 .ret_type = RET_INTEGER,
7539 .arg1_type = ARG_PTR_TO_FIXED_SIZE_MEM,
7540 .arg1_size = sizeof(struct iphdr),
7541 .arg2_type = ARG_PTR_TO_FIXED_SIZE_MEM,
7542 .arg2_size = sizeof(struct tcphdr),
7545 BPF_CALL_2(bpf_tcp_raw_check_syncookie_ipv6, struct ipv6hdr *, iph,
7546 struct tcphdr *, th)
7548 #if IS_BUILTIN(CONFIG_IPV6)
7549 u32 cookie = ntohl(th->ack_seq) - 1;
7551 if (__cookie_v6_check(iph, th, cookie) > 0)
7556 return -EPROTONOSUPPORT;
7560 static const struct bpf_func_proto bpf_tcp_raw_check_syncookie_ipv6_proto = {
7561 .func = bpf_tcp_raw_check_syncookie_ipv6,
7562 .gpl_only = true, /* __cookie_v6_check is GPL */
7564 .ret_type = RET_INTEGER,
7565 .arg1_type = ARG_PTR_TO_FIXED_SIZE_MEM,
7566 .arg1_size = sizeof(struct ipv6hdr),
7567 .arg2_type = ARG_PTR_TO_FIXED_SIZE_MEM,
7568 .arg2_size = sizeof(struct tcphdr),
7570 #endif /* CONFIG_SYN_COOKIES */
7572 #endif /* CONFIG_INET */
7574 bool bpf_helper_changes_pkt_data(void *func)
7576 if (func == bpf_skb_vlan_push ||
7577 func == bpf_skb_vlan_pop ||
7578 func == bpf_skb_store_bytes ||
7579 func == bpf_skb_change_proto ||
7580 func == bpf_skb_change_head ||
7581 func == sk_skb_change_head ||
7582 func == bpf_skb_change_tail ||
7583 func == sk_skb_change_tail ||
7584 func == bpf_skb_adjust_room ||
7585 func == sk_skb_adjust_room ||
7586 func == bpf_skb_pull_data ||
7587 func == sk_skb_pull_data ||
7588 func == bpf_clone_redirect ||
7589 func == bpf_l3_csum_replace ||
7590 func == bpf_l4_csum_replace ||
7591 func == bpf_xdp_adjust_head ||
7592 func == bpf_xdp_adjust_meta ||
7593 func == bpf_msg_pull_data ||
7594 func == bpf_msg_push_data ||
7595 func == bpf_msg_pop_data ||
7596 func == bpf_xdp_adjust_tail ||
7597 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
7598 func == bpf_lwt_seg6_store_bytes ||
7599 func == bpf_lwt_seg6_adjust_srh ||
7600 func == bpf_lwt_seg6_action ||
7603 func == bpf_sock_ops_store_hdr_opt ||
7605 func == bpf_lwt_in_push_encap ||
7606 func == bpf_lwt_xmit_push_encap)
7612 const struct bpf_func_proto bpf_event_output_data_proto __weak;
7613 const struct bpf_func_proto bpf_sk_storage_get_cg_sock_proto __weak;
7615 static const struct bpf_func_proto *
7616 sock_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
7618 const struct bpf_func_proto *func_proto;
7620 func_proto = cgroup_common_func_proto(func_id, prog);
7624 func_proto = cgroup_current_func_proto(func_id, prog);
7629 case BPF_FUNC_get_socket_cookie:
7630 return &bpf_get_socket_cookie_sock_proto;
7631 case BPF_FUNC_get_netns_cookie:
7632 return &bpf_get_netns_cookie_sock_proto;
7633 case BPF_FUNC_perf_event_output:
7634 return &bpf_event_output_data_proto;
7635 case BPF_FUNC_sk_storage_get:
7636 return &bpf_sk_storage_get_cg_sock_proto;
7637 case BPF_FUNC_ktime_get_coarse_ns:
7638 return &bpf_ktime_get_coarse_ns_proto;
7640 return bpf_base_func_proto(func_id);
7644 static const struct bpf_func_proto *
7645 sock_addr_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
7647 const struct bpf_func_proto *func_proto;
7649 func_proto = cgroup_common_func_proto(func_id, prog);
7653 func_proto = cgroup_current_func_proto(func_id, prog);
7659 switch (prog->expected_attach_type) {
7660 case BPF_CGROUP_INET4_CONNECT:
7661 case BPF_CGROUP_INET6_CONNECT:
7662 return &bpf_bind_proto;
7666 case BPF_FUNC_get_socket_cookie:
7667 return &bpf_get_socket_cookie_sock_addr_proto;
7668 case BPF_FUNC_get_netns_cookie:
7669 return &bpf_get_netns_cookie_sock_addr_proto;
7670 case BPF_FUNC_perf_event_output:
7671 return &bpf_event_output_data_proto;
7673 case BPF_FUNC_sk_lookup_tcp:
7674 return &bpf_sock_addr_sk_lookup_tcp_proto;
7675 case BPF_FUNC_sk_lookup_udp:
7676 return &bpf_sock_addr_sk_lookup_udp_proto;
7677 case BPF_FUNC_sk_release:
7678 return &bpf_sk_release_proto;
7679 case BPF_FUNC_skc_lookup_tcp:
7680 return &bpf_sock_addr_skc_lookup_tcp_proto;
7681 #endif /* CONFIG_INET */
7682 case BPF_FUNC_sk_storage_get:
7683 return &bpf_sk_storage_get_proto;
7684 case BPF_FUNC_sk_storage_delete:
7685 return &bpf_sk_storage_delete_proto;
7686 case BPF_FUNC_setsockopt:
7687 switch (prog->expected_attach_type) {
7688 case BPF_CGROUP_INET4_BIND:
7689 case BPF_CGROUP_INET6_BIND:
7690 case BPF_CGROUP_INET4_CONNECT:
7691 case BPF_CGROUP_INET6_CONNECT:
7692 case BPF_CGROUP_UDP4_RECVMSG:
7693 case BPF_CGROUP_UDP6_RECVMSG:
7694 case BPF_CGROUP_UDP4_SENDMSG:
7695 case BPF_CGROUP_UDP6_SENDMSG:
7696 case BPF_CGROUP_INET4_GETPEERNAME:
7697 case BPF_CGROUP_INET6_GETPEERNAME:
7698 case BPF_CGROUP_INET4_GETSOCKNAME:
7699 case BPF_CGROUP_INET6_GETSOCKNAME:
7700 return &bpf_sock_addr_setsockopt_proto;
7704 case BPF_FUNC_getsockopt:
7705 switch (prog->expected_attach_type) {
7706 case BPF_CGROUP_INET4_BIND:
7707 case BPF_CGROUP_INET6_BIND:
7708 case BPF_CGROUP_INET4_CONNECT:
7709 case BPF_CGROUP_INET6_CONNECT:
7710 case BPF_CGROUP_UDP4_RECVMSG:
7711 case BPF_CGROUP_UDP6_RECVMSG:
7712 case BPF_CGROUP_UDP4_SENDMSG:
7713 case BPF_CGROUP_UDP6_SENDMSG:
7714 case BPF_CGROUP_INET4_GETPEERNAME:
7715 case BPF_CGROUP_INET6_GETPEERNAME:
7716 case BPF_CGROUP_INET4_GETSOCKNAME:
7717 case BPF_CGROUP_INET6_GETSOCKNAME:
7718 return &bpf_sock_addr_getsockopt_proto;
7723 return bpf_sk_base_func_proto(func_id);
7727 static const struct bpf_func_proto *
7728 sk_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
7731 case BPF_FUNC_skb_load_bytes:
7732 return &bpf_skb_load_bytes_proto;
7733 case BPF_FUNC_skb_load_bytes_relative:
7734 return &bpf_skb_load_bytes_relative_proto;
7735 case BPF_FUNC_get_socket_cookie:
7736 return &bpf_get_socket_cookie_proto;
7737 case BPF_FUNC_get_socket_uid:
7738 return &bpf_get_socket_uid_proto;
7739 case BPF_FUNC_perf_event_output:
7740 return &bpf_skb_event_output_proto;
7742 return bpf_sk_base_func_proto(func_id);
7746 const struct bpf_func_proto bpf_sk_storage_get_proto __weak;
7747 const struct bpf_func_proto bpf_sk_storage_delete_proto __weak;
7749 static const struct bpf_func_proto *
7750 cg_skb_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
7752 const struct bpf_func_proto *func_proto;
7754 func_proto = cgroup_common_func_proto(func_id, prog);
7759 case BPF_FUNC_sk_fullsock:
7760 return &bpf_sk_fullsock_proto;
7761 case BPF_FUNC_sk_storage_get:
7762 return &bpf_sk_storage_get_proto;
7763 case BPF_FUNC_sk_storage_delete:
7764 return &bpf_sk_storage_delete_proto;
7765 case BPF_FUNC_perf_event_output:
7766 return &bpf_skb_event_output_proto;
7767 #ifdef CONFIG_SOCK_CGROUP_DATA
7768 case BPF_FUNC_skb_cgroup_id:
7769 return &bpf_skb_cgroup_id_proto;
7770 case BPF_FUNC_skb_ancestor_cgroup_id:
7771 return &bpf_skb_ancestor_cgroup_id_proto;
7772 case BPF_FUNC_sk_cgroup_id:
7773 return &bpf_sk_cgroup_id_proto;
7774 case BPF_FUNC_sk_ancestor_cgroup_id:
7775 return &bpf_sk_ancestor_cgroup_id_proto;
7778 case BPF_FUNC_sk_lookup_tcp:
7779 return &bpf_sk_lookup_tcp_proto;
7780 case BPF_FUNC_sk_lookup_udp:
7781 return &bpf_sk_lookup_udp_proto;
7782 case BPF_FUNC_sk_release:
7783 return &bpf_sk_release_proto;
7784 case BPF_FUNC_skc_lookup_tcp:
7785 return &bpf_skc_lookup_tcp_proto;
7786 case BPF_FUNC_tcp_sock:
7787 return &bpf_tcp_sock_proto;
7788 case BPF_FUNC_get_listener_sock:
7789 return &bpf_get_listener_sock_proto;
7790 case BPF_FUNC_skb_ecn_set_ce:
7791 return &bpf_skb_ecn_set_ce_proto;
7794 return sk_filter_func_proto(func_id, prog);
7798 static const struct bpf_func_proto *
7799 tc_cls_act_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
7802 case BPF_FUNC_skb_store_bytes:
7803 return &bpf_skb_store_bytes_proto;
7804 case BPF_FUNC_skb_load_bytes:
7805 return &bpf_skb_load_bytes_proto;
7806 case BPF_FUNC_skb_load_bytes_relative:
7807 return &bpf_skb_load_bytes_relative_proto;
7808 case BPF_FUNC_skb_pull_data:
7809 return &bpf_skb_pull_data_proto;
7810 case BPF_FUNC_csum_diff:
7811 return &bpf_csum_diff_proto;
7812 case BPF_FUNC_csum_update:
7813 return &bpf_csum_update_proto;
7814 case BPF_FUNC_csum_level:
7815 return &bpf_csum_level_proto;
7816 case BPF_FUNC_l3_csum_replace:
7817 return &bpf_l3_csum_replace_proto;
7818 case BPF_FUNC_l4_csum_replace:
7819 return &bpf_l4_csum_replace_proto;
7820 case BPF_FUNC_clone_redirect:
7821 return &bpf_clone_redirect_proto;
7822 case BPF_FUNC_get_cgroup_classid:
7823 return &bpf_get_cgroup_classid_proto;
7824 case BPF_FUNC_skb_vlan_push:
7825 return &bpf_skb_vlan_push_proto;
7826 case BPF_FUNC_skb_vlan_pop:
7827 return &bpf_skb_vlan_pop_proto;
7828 case BPF_FUNC_skb_change_proto:
7829 return &bpf_skb_change_proto_proto;
7830 case BPF_FUNC_skb_change_type:
7831 return &bpf_skb_change_type_proto;
7832 case BPF_FUNC_skb_adjust_room:
7833 return &bpf_skb_adjust_room_proto;
7834 case BPF_FUNC_skb_change_tail:
7835 return &bpf_skb_change_tail_proto;
7836 case BPF_FUNC_skb_change_head:
7837 return &bpf_skb_change_head_proto;
7838 case BPF_FUNC_skb_get_tunnel_key:
7839 return &bpf_skb_get_tunnel_key_proto;
7840 case BPF_FUNC_skb_set_tunnel_key:
7841 return bpf_get_skb_set_tunnel_proto(func_id);
7842 case BPF_FUNC_skb_get_tunnel_opt:
7843 return &bpf_skb_get_tunnel_opt_proto;
7844 case BPF_FUNC_skb_set_tunnel_opt:
7845 return bpf_get_skb_set_tunnel_proto(func_id);
7846 case BPF_FUNC_redirect:
7847 return &bpf_redirect_proto;
7848 case BPF_FUNC_redirect_neigh:
7849 return &bpf_redirect_neigh_proto;
7850 case BPF_FUNC_redirect_peer:
7851 return &bpf_redirect_peer_proto;
7852 case BPF_FUNC_get_route_realm:
7853 return &bpf_get_route_realm_proto;
7854 case BPF_FUNC_get_hash_recalc:
7855 return &bpf_get_hash_recalc_proto;
7856 case BPF_FUNC_set_hash_invalid:
7857 return &bpf_set_hash_invalid_proto;
7858 case BPF_FUNC_set_hash:
7859 return &bpf_set_hash_proto;
7860 case BPF_FUNC_perf_event_output:
7861 return &bpf_skb_event_output_proto;
7862 case BPF_FUNC_get_smp_processor_id:
7863 return &bpf_get_smp_processor_id_proto;
7864 case BPF_FUNC_skb_under_cgroup:
7865 return &bpf_skb_under_cgroup_proto;
7866 case BPF_FUNC_get_socket_cookie:
7867 return &bpf_get_socket_cookie_proto;
7868 case BPF_FUNC_get_socket_uid:
7869 return &bpf_get_socket_uid_proto;
7870 case BPF_FUNC_fib_lookup:
7871 return &bpf_skb_fib_lookup_proto;
7872 case BPF_FUNC_check_mtu:
7873 return &bpf_skb_check_mtu_proto;
7874 case BPF_FUNC_sk_fullsock:
7875 return &bpf_sk_fullsock_proto;
7876 case BPF_FUNC_sk_storage_get:
7877 return &bpf_sk_storage_get_proto;
7878 case BPF_FUNC_sk_storage_delete:
7879 return &bpf_sk_storage_delete_proto;
7881 case BPF_FUNC_skb_get_xfrm_state:
7882 return &bpf_skb_get_xfrm_state_proto;
7884 #ifdef CONFIG_CGROUP_NET_CLASSID
7885 case BPF_FUNC_skb_cgroup_classid:
7886 return &bpf_skb_cgroup_classid_proto;
7888 #ifdef CONFIG_SOCK_CGROUP_DATA
7889 case BPF_FUNC_skb_cgroup_id:
7890 return &bpf_skb_cgroup_id_proto;
7891 case BPF_FUNC_skb_ancestor_cgroup_id:
7892 return &bpf_skb_ancestor_cgroup_id_proto;
7895 case BPF_FUNC_sk_lookup_tcp:
7896 return &bpf_sk_lookup_tcp_proto;
7897 case BPF_FUNC_sk_lookup_udp:
7898 return &bpf_sk_lookup_udp_proto;
7899 case BPF_FUNC_sk_release:
7900 return &bpf_sk_release_proto;
7901 case BPF_FUNC_tcp_sock:
7902 return &bpf_tcp_sock_proto;
7903 case BPF_FUNC_get_listener_sock:
7904 return &bpf_get_listener_sock_proto;
7905 case BPF_FUNC_skc_lookup_tcp:
7906 return &bpf_skc_lookup_tcp_proto;
7907 case BPF_FUNC_tcp_check_syncookie:
7908 return &bpf_tcp_check_syncookie_proto;
7909 case BPF_FUNC_skb_ecn_set_ce:
7910 return &bpf_skb_ecn_set_ce_proto;
7911 case BPF_FUNC_tcp_gen_syncookie:
7912 return &bpf_tcp_gen_syncookie_proto;
7913 case BPF_FUNC_sk_assign:
7914 return &bpf_sk_assign_proto;
7915 case BPF_FUNC_skb_set_tstamp:
7916 return &bpf_skb_set_tstamp_proto;
7917 #ifdef CONFIG_SYN_COOKIES
7918 case BPF_FUNC_tcp_raw_gen_syncookie_ipv4:
7919 return &bpf_tcp_raw_gen_syncookie_ipv4_proto;
7920 case BPF_FUNC_tcp_raw_gen_syncookie_ipv6:
7921 return &bpf_tcp_raw_gen_syncookie_ipv6_proto;
7922 case BPF_FUNC_tcp_raw_check_syncookie_ipv4:
7923 return &bpf_tcp_raw_check_syncookie_ipv4_proto;
7924 case BPF_FUNC_tcp_raw_check_syncookie_ipv6:
7925 return &bpf_tcp_raw_check_syncookie_ipv6_proto;
7929 return bpf_sk_base_func_proto(func_id);
7933 static const struct bpf_func_proto *
7934 xdp_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
7937 case BPF_FUNC_perf_event_output:
7938 return &bpf_xdp_event_output_proto;
7939 case BPF_FUNC_get_smp_processor_id:
7940 return &bpf_get_smp_processor_id_proto;
7941 case BPF_FUNC_csum_diff:
7942 return &bpf_csum_diff_proto;
7943 case BPF_FUNC_xdp_adjust_head:
7944 return &bpf_xdp_adjust_head_proto;
7945 case BPF_FUNC_xdp_adjust_meta:
7946 return &bpf_xdp_adjust_meta_proto;
7947 case BPF_FUNC_redirect:
7948 return &bpf_xdp_redirect_proto;
7949 case BPF_FUNC_redirect_map:
7950 return &bpf_xdp_redirect_map_proto;
7951 case BPF_FUNC_xdp_adjust_tail:
7952 return &bpf_xdp_adjust_tail_proto;
7953 case BPF_FUNC_xdp_get_buff_len:
7954 return &bpf_xdp_get_buff_len_proto;
7955 case BPF_FUNC_xdp_load_bytes:
7956 return &bpf_xdp_load_bytes_proto;
7957 case BPF_FUNC_xdp_store_bytes:
7958 return &bpf_xdp_store_bytes_proto;
7959 case BPF_FUNC_fib_lookup:
7960 return &bpf_xdp_fib_lookup_proto;
7961 case BPF_FUNC_check_mtu:
7962 return &bpf_xdp_check_mtu_proto;
7964 case BPF_FUNC_sk_lookup_udp:
7965 return &bpf_xdp_sk_lookup_udp_proto;
7966 case BPF_FUNC_sk_lookup_tcp:
7967 return &bpf_xdp_sk_lookup_tcp_proto;
7968 case BPF_FUNC_sk_release:
7969 return &bpf_sk_release_proto;
7970 case BPF_FUNC_skc_lookup_tcp:
7971 return &bpf_xdp_skc_lookup_tcp_proto;
7972 case BPF_FUNC_tcp_check_syncookie:
7973 return &bpf_tcp_check_syncookie_proto;
7974 case BPF_FUNC_tcp_gen_syncookie:
7975 return &bpf_tcp_gen_syncookie_proto;
7976 #ifdef CONFIG_SYN_COOKIES
7977 case BPF_FUNC_tcp_raw_gen_syncookie_ipv4:
7978 return &bpf_tcp_raw_gen_syncookie_ipv4_proto;
7979 case BPF_FUNC_tcp_raw_gen_syncookie_ipv6:
7980 return &bpf_tcp_raw_gen_syncookie_ipv6_proto;
7981 case BPF_FUNC_tcp_raw_check_syncookie_ipv4:
7982 return &bpf_tcp_raw_check_syncookie_ipv4_proto;
7983 case BPF_FUNC_tcp_raw_check_syncookie_ipv6:
7984 return &bpf_tcp_raw_check_syncookie_ipv6_proto;
7988 return bpf_sk_base_func_proto(func_id);
7992 const struct bpf_func_proto bpf_sock_map_update_proto __weak;
7993 const struct bpf_func_proto bpf_sock_hash_update_proto __weak;
7995 static const struct bpf_func_proto *
7996 sock_ops_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
7998 const struct bpf_func_proto *func_proto;
8000 func_proto = cgroup_common_func_proto(func_id, prog);
8005 case BPF_FUNC_setsockopt:
8006 return &bpf_sock_ops_setsockopt_proto;
8007 case BPF_FUNC_getsockopt:
8008 return &bpf_sock_ops_getsockopt_proto;
8009 case BPF_FUNC_sock_ops_cb_flags_set:
8010 return &bpf_sock_ops_cb_flags_set_proto;
8011 case BPF_FUNC_sock_map_update:
8012 return &bpf_sock_map_update_proto;
8013 case BPF_FUNC_sock_hash_update:
8014 return &bpf_sock_hash_update_proto;
8015 case BPF_FUNC_get_socket_cookie:
8016 return &bpf_get_socket_cookie_sock_ops_proto;
8017 case BPF_FUNC_perf_event_output:
8018 return &bpf_event_output_data_proto;
8019 case BPF_FUNC_sk_storage_get:
8020 return &bpf_sk_storage_get_proto;
8021 case BPF_FUNC_sk_storage_delete:
8022 return &bpf_sk_storage_delete_proto;
8023 case BPF_FUNC_get_netns_cookie:
8024 return &bpf_get_netns_cookie_sock_ops_proto;
8026 case BPF_FUNC_load_hdr_opt:
8027 return &bpf_sock_ops_load_hdr_opt_proto;
8028 case BPF_FUNC_store_hdr_opt:
8029 return &bpf_sock_ops_store_hdr_opt_proto;
8030 case BPF_FUNC_reserve_hdr_opt:
8031 return &bpf_sock_ops_reserve_hdr_opt_proto;
8032 case BPF_FUNC_tcp_sock:
8033 return &bpf_tcp_sock_proto;
8034 #endif /* CONFIG_INET */
8036 return bpf_sk_base_func_proto(func_id);
8040 const struct bpf_func_proto bpf_msg_redirect_map_proto __weak;
8041 const struct bpf_func_proto bpf_msg_redirect_hash_proto __weak;
8043 static const struct bpf_func_proto *
8044 sk_msg_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
8047 case BPF_FUNC_msg_redirect_map:
8048 return &bpf_msg_redirect_map_proto;
8049 case BPF_FUNC_msg_redirect_hash:
8050 return &bpf_msg_redirect_hash_proto;
8051 case BPF_FUNC_msg_apply_bytes:
8052 return &bpf_msg_apply_bytes_proto;
8053 case BPF_FUNC_msg_cork_bytes:
8054 return &bpf_msg_cork_bytes_proto;
8055 case BPF_FUNC_msg_pull_data:
8056 return &bpf_msg_pull_data_proto;
8057 case BPF_FUNC_msg_push_data:
8058 return &bpf_msg_push_data_proto;
8059 case BPF_FUNC_msg_pop_data:
8060 return &bpf_msg_pop_data_proto;
8061 case BPF_FUNC_perf_event_output:
8062 return &bpf_event_output_data_proto;
8063 case BPF_FUNC_get_current_uid_gid:
8064 return &bpf_get_current_uid_gid_proto;
8065 case BPF_FUNC_get_current_pid_tgid:
8066 return &bpf_get_current_pid_tgid_proto;
8067 case BPF_FUNC_sk_storage_get:
8068 return &bpf_sk_storage_get_proto;
8069 case BPF_FUNC_sk_storage_delete:
8070 return &bpf_sk_storage_delete_proto;
8071 case BPF_FUNC_get_netns_cookie:
8072 return &bpf_get_netns_cookie_sk_msg_proto;
8073 #ifdef CONFIG_CGROUPS
8074 case BPF_FUNC_get_current_cgroup_id:
8075 return &bpf_get_current_cgroup_id_proto;
8076 case BPF_FUNC_get_current_ancestor_cgroup_id:
8077 return &bpf_get_current_ancestor_cgroup_id_proto;
8079 #ifdef CONFIG_CGROUP_NET_CLASSID
8080 case BPF_FUNC_get_cgroup_classid:
8081 return &bpf_get_cgroup_classid_curr_proto;
8084 return bpf_sk_base_func_proto(func_id);
8088 const struct bpf_func_proto bpf_sk_redirect_map_proto __weak;
8089 const struct bpf_func_proto bpf_sk_redirect_hash_proto __weak;
8091 static const struct bpf_func_proto *
8092 sk_skb_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
8095 case BPF_FUNC_skb_store_bytes:
8096 return &bpf_skb_store_bytes_proto;
8097 case BPF_FUNC_skb_load_bytes:
8098 return &bpf_skb_load_bytes_proto;
8099 case BPF_FUNC_skb_pull_data:
8100 return &sk_skb_pull_data_proto;
8101 case BPF_FUNC_skb_change_tail:
8102 return &sk_skb_change_tail_proto;
8103 case BPF_FUNC_skb_change_head:
8104 return &sk_skb_change_head_proto;
8105 case BPF_FUNC_skb_adjust_room:
8106 return &sk_skb_adjust_room_proto;
8107 case BPF_FUNC_get_socket_cookie:
8108 return &bpf_get_socket_cookie_proto;
8109 case BPF_FUNC_get_socket_uid:
8110 return &bpf_get_socket_uid_proto;
8111 case BPF_FUNC_sk_redirect_map:
8112 return &bpf_sk_redirect_map_proto;
8113 case BPF_FUNC_sk_redirect_hash:
8114 return &bpf_sk_redirect_hash_proto;
8115 case BPF_FUNC_perf_event_output:
8116 return &bpf_skb_event_output_proto;
8118 case BPF_FUNC_sk_lookup_tcp:
8119 return &bpf_sk_lookup_tcp_proto;
8120 case BPF_FUNC_sk_lookup_udp:
8121 return &bpf_sk_lookup_udp_proto;
8122 case BPF_FUNC_sk_release:
8123 return &bpf_sk_release_proto;
8124 case BPF_FUNC_skc_lookup_tcp:
8125 return &bpf_skc_lookup_tcp_proto;
8128 return bpf_sk_base_func_proto(func_id);
8132 static const struct bpf_func_proto *
8133 flow_dissector_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
8136 case BPF_FUNC_skb_load_bytes:
8137 return &bpf_flow_dissector_load_bytes_proto;
8139 return bpf_sk_base_func_proto(func_id);
8143 static const struct bpf_func_proto *
8144 lwt_out_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
8147 case BPF_FUNC_skb_load_bytes:
8148 return &bpf_skb_load_bytes_proto;
8149 case BPF_FUNC_skb_pull_data:
8150 return &bpf_skb_pull_data_proto;
8151 case BPF_FUNC_csum_diff:
8152 return &bpf_csum_diff_proto;
8153 case BPF_FUNC_get_cgroup_classid:
8154 return &bpf_get_cgroup_classid_proto;
8155 case BPF_FUNC_get_route_realm:
8156 return &bpf_get_route_realm_proto;
8157 case BPF_FUNC_get_hash_recalc:
8158 return &bpf_get_hash_recalc_proto;
8159 case BPF_FUNC_perf_event_output:
8160 return &bpf_skb_event_output_proto;
8161 case BPF_FUNC_get_smp_processor_id:
8162 return &bpf_get_smp_processor_id_proto;
8163 case BPF_FUNC_skb_under_cgroup:
8164 return &bpf_skb_under_cgroup_proto;
8166 return bpf_sk_base_func_proto(func_id);
8170 static const struct bpf_func_proto *
8171 lwt_in_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
8174 case BPF_FUNC_lwt_push_encap:
8175 return &bpf_lwt_in_push_encap_proto;
8177 return lwt_out_func_proto(func_id, prog);
8181 static const struct bpf_func_proto *
8182 lwt_xmit_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
8185 case BPF_FUNC_skb_get_tunnel_key:
8186 return &bpf_skb_get_tunnel_key_proto;
8187 case BPF_FUNC_skb_set_tunnel_key:
8188 return bpf_get_skb_set_tunnel_proto(func_id);
8189 case BPF_FUNC_skb_get_tunnel_opt:
8190 return &bpf_skb_get_tunnel_opt_proto;
8191 case BPF_FUNC_skb_set_tunnel_opt:
8192 return bpf_get_skb_set_tunnel_proto(func_id);
8193 case BPF_FUNC_redirect:
8194 return &bpf_redirect_proto;
8195 case BPF_FUNC_clone_redirect:
8196 return &bpf_clone_redirect_proto;
8197 case BPF_FUNC_skb_change_tail:
8198 return &bpf_skb_change_tail_proto;
8199 case BPF_FUNC_skb_change_head:
8200 return &bpf_skb_change_head_proto;
8201 case BPF_FUNC_skb_store_bytes:
8202 return &bpf_skb_store_bytes_proto;
8203 case BPF_FUNC_csum_update:
8204 return &bpf_csum_update_proto;
8205 case BPF_FUNC_csum_level:
8206 return &bpf_csum_level_proto;
8207 case BPF_FUNC_l3_csum_replace:
8208 return &bpf_l3_csum_replace_proto;
8209 case BPF_FUNC_l4_csum_replace:
8210 return &bpf_l4_csum_replace_proto;
8211 case BPF_FUNC_set_hash_invalid:
8212 return &bpf_set_hash_invalid_proto;
8213 case BPF_FUNC_lwt_push_encap:
8214 return &bpf_lwt_xmit_push_encap_proto;
8216 return lwt_out_func_proto(func_id, prog);
8220 static const struct bpf_func_proto *
8221 lwt_seg6local_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
8224 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
8225 case BPF_FUNC_lwt_seg6_store_bytes:
8226 return &bpf_lwt_seg6_store_bytes_proto;
8227 case BPF_FUNC_lwt_seg6_action:
8228 return &bpf_lwt_seg6_action_proto;
8229 case BPF_FUNC_lwt_seg6_adjust_srh:
8230 return &bpf_lwt_seg6_adjust_srh_proto;
8233 return lwt_out_func_proto(func_id, prog);
8237 static bool bpf_skb_is_valid_access(int off, int size, enum bpf_access_type type,
8238 const struct bpf_prog *prog,
8239 struct bpf_insn_access_aux *info)
8241 const int size_default = sizeof(__u32);
8243 if (off < 0 || off >= sizeof(struct __sk_buff))
8246 /* The verifier guarantees that size > 0. */
8247 if (off % size != 0)
8251 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
8252 if (off + size > offsetofend(struct __sk_buff, cb[4]))
8255 case bpf_ctx_range_till(struct __sk_buff, remote_ip6[0], remote_ip6[3]):
8256 case bpf_ctx_range_till(struct __sk_buff, local_ip6[0], local_ip6[3]):
8257 case bpf_ctx_range_till(struct __sk_buff, remote_ip4, remote_ip4):
8258 case bpf_ctx_range_till(struct __sk_buff, local_ip4, local_ip4):
8259 case bpf_ctx_range(struct __sk_buff, data):
8260 case bpf_ctx_range(struct __sk_buff, data_meta):
8261 case bpf_ctx_range(struct __sk_buff, data_end):
8262 if (size != size_default)
8265 case bpf_ctx_range_ptr(struct __sk_buff, flow_keys):
8267 case bpf_ctx_range(struct __sk_buff, hwtstamp):
8268 if (type == BPF_WRITE || size != sizeof(__u64))
8271 case bpf_ctx_range(struct __sk_buff, tstamp):
8272 if (size != sizeof(__u64))
8275 case offsetof(struct __sk_buff, sk):
8276 if (type == BPF_WRITE || size != sizeof(__u64))
8278 info->reg_type = PTR_TO_SOCK_COMMON_OR_NULL;
8280 case offsetof(struct __sk_buff, tstamp_type):
8282 case offsetofend(struct __sk_buff, tstamp_type) ... offsetof(struct __sk_buff, hwtstamp) - 1:
8283 /* Explicitly prohibit access to padding in __sk_buff. */
8286 /* Only narrow read access allowed for now. */
8287 if (type == BPF_WRITE) {
8288 if (size != size_default)
8291 bpf_ctx_record_field_size(info, size_default);
8292 if (!bpf_ctx_narrow_access_ok(off, size, size_default))
8300 static bool sk_filter_is_valid_access(int off, int size,
8301 enum bpf_access_type type,
8302 const struct bpf_prog *prog,
8303 struct bpf_insn_access_aux *info)
8306 case bpf_ctx_range(struct __sk_buff, tc_classid):
8307 case bpf_ctx_range(struct __sk_buff, data):
8308 case bpf_ctx_range(struct __sk_buff, data_meta):
8309 case bpf_ctx_range(struct __sk_buff, data_end):
8310 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
8311 case bpf_ctx_range(struct __sk_buff, tstamp):
8312 case bpf_ctx_range(struct __sk_buff, wire_len):
8313 case bpf_ctx_range(struct __sk_buff, hwtstamp):
8317 if (type == BPF_WRITE) {
8319 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
8326 return bpf_skb_is_valid_access(off, size, type, prog, info);
8329 static bool cg_skb_is_valid_access(int off, int size,
8330 enum bpf_access_type type,
8331 const struct bpf_prog *prog,
8332 struct bpf_insn_access_aux *info)
8335 case bpf_ctx_range(struct __sk_buff, tc_classid):
8336 case bpf_ctx_range(struct __sk_buff, data_meta):
8337 case bpf_ctx_range(struct __sk_buff, wire_len):
8339 case bpf_ctx_range(struct __sk_buff, data):
8340 case bpf_ctx_range(struct __sk_buff, data_end):
8346 if (type == BPF_WRITE) {
8348 case bpf_ctx_range(struct __sk_buff, mark):
8349 case bpf_ctx_range(struct __sk_buff, priority):
8350 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
8352 case bpf_ctx_range(struct __sk_buff, tstamp):
8362 case bpf_ctx_range(struct __sk_buff, data):
8363 info->reg_type = PTR_TO_PACKET;
8365 case bpf_ctx_range(struct __sk_buff, data_end):
8366 info->reg_type = PTR_TO_PACKET_END;
8370 return bpf_skb_is_valid_access(off, size, type, prog, info);
8373 static bool lwt_is_valid_access(int off, int size,
8374 enum bpf_access_type type,
8375 const struct bpf_prog *prog,
8376 struct bpf_insn_access_aux *info)
8379 case bpf_ctx_range(struct __sk_buff, tc_classid):
8380 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
8381 case bpf_ctx_range(struct __sk_buff, data_meta):
8382 case bpf_ctx_range(struct __sk_buff, tstamp):
8383 case bpf_ctx_range(struct __sk_buff, wire_len):
8384 case bpf_ctx_range(struct __sk_buff, hwtstamp):
8388 if (type == BPF_WRITE) {
8390 case bpf_ctx_range(struct __sk_buff, mark):
8391 case bpf_ctx_range(struct __sk_buff, priority):
8392 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
8400 case bpf_ctx_range(struct __sk_buff, data):
8401 info->reg_type = PTR_TO_PACKET;
8403 case bpf_ctx_range(struct __sk_buff, data_end):
8404 info->reg_type = PTR_TO_PACKET_END;
8408 return bpf_skb_is_valid_access(off, size, type, prog, info);
8411 /* Attach type specific accesses */
8412 static bool __sock_filter_check_attach_type(int off,
8413 enum bpf_access_type access_type,
8414 enum bpf_attach_type attach_type)
8417 case offsetof(struct bpf_sock, bound_dev_if):
8418 case offsetof(struct bpf_sock, mark):
8419 case offsetof(struct bpf_sock, priority):
8420 switch (attach_type) {
8421 case BPF_CGROUP_INET_SOCK_CREATE:
8422 case BPF_CGROUP_INET_SOCK_RELEASE:
8427 case bpf_ctx_range(struct bpf_sock, src_ip4):
8428 switch (attach_type) {
8429 case BPF_CGROUP_INET4_POST_BIND:
8434 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
8435 switch (attach_type) {
8436 case BPF_CGROUP_INET6_POST_BIND:
8441 case bpf_ctx_range(struct bpf_sock, src_port):
8442 switch (attach_type) {
8443 case BPF_CGROUP_INET4_POST_BIND:
8444 case BPF_CGROUP_INET6_POST_BIND:
8451 return access_type == BPF_READ;
8456 bool bpf_sock_common_is_valid_access(int off, int size,
8457 enum bpf_access_type type,
8458 struct bpf_insn_access_aux *info)
8461 case bpf_ctx_range_till(struct bpf_sock, type, priority):
8464 return bpf_sock_is_valid_access(off, size, type, info);
8468 bool bpf_sock_is_valid_access(int off, int size, enum bpf_access_type type,
8469 struct bpf_insn_access_aux *info)
8471 const int size_default = sizeof(__u32);
8474 if (off < 0 || off >= sizeof(struct bpf_sock))
8476 if (off % size != 0)
8480 case offsetof(struct bpf_sock, state):
8481 case offsetof(struct bpf_sock, family):
8482 case offsetof(struct bpf_sock, type):
8483 case offsetof(struct bpf_sock, protocol):
8484 case offsetof(struct bpf_sock, src_port):
8485 case offsetof(struct bpf_sock, rx_queue_mapping):
8486 case bpf_ctx_range(struct bpf_sock, src_ip4):
8487 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
8488 case bpf_ctx_range(struct bpf_sock, dst_ip4):
8489 case bpf_ctx_range_till(struct bpf_sock, dst_ip6[0], dst_ip6[3]):
8490 bpf_ctx_record_field_size(info, size_default);
8491 return bpf_ctx_narrow_access_ok(off, size, size_default);
8492 case bpf_ctx_range(struct bpf_sock, dst_port):
8493 field_size = size == size_default ?
8494 size_default : sizeof_field(struct bpf_sock, dst_port);
8495 bpf_ctx_record_field_size(info, field_size);
8496 return bpf_ctx_narrow_access_ok(off, size, field_size);
8497 case offsetofend(struct bpf_sock, dst_port) ...
8498 offsetof(struct bpf_sock, dst_ip4) - 1:
8502 return size == size_default;
8505 static bool sock_filter_is_valid_access(int off, int size,
8506 enum bpf_access_type type,
8507 const struct bpf_prog *prog,
8508 struct bpf_insn_access_aux *info)
8510 if (!bpf_sock_is_valid_access(off, size, type, info))
8512 return __sock_filter_check_attach_type(off, type,
8513 prog->expected_attach_type);
8516 static int bpf_noop_prologue(struct bpf_insn *insn_buf, bool direct_write,
8517 const struct bpf_prog *prog)
8519 /* Neither direct read nor direct write requires any preliminary
8525 static int bpf_unclone_prologue(struct bpf_insn *insn_buf, bool direct_write,
8526 const struct bpf_prog *prog, int drop_verdict)
8528 struct bpf_insn *insn = insn_buf;
8533 /* if (!skb->cloned)
8536 * (Fast-path, otherwise approximation that we might be
8537 * a clone, do the rest in helper.)
8539 *insn++ = BPF_LDX_MEM(BPF_B, BPF_REG_6, BPF_REG_1, CLONED_OFFSET);
8540 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_6, CLONED_MASK);
8541 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_6, 0, 7);
8543 /* ret = bpf_skb_pull_data(skb, 0); */
8544 *insn++ = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1);
8545 *insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_2, BPF_REG_2);
8546 *insn++ = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
8547 BPF_FUNC_skb_pull_data);
8550 * return TC_ACT_SHOT;
8552 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2);
8553 *insn++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_0, drop_verdict);
8554 *insn++ = BPF_EXIT_INSN();
8557 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_6);
8559 *insn++ = prog->insnsi[0];
8561 return insn - insn_buf;
8564 static int bpf_gen_ld_abs(const struct bpf_insn *orig,
8565 struct bpf_insn *insn_buf)
8567 bool indirect = BPF_MODE(orig->code) == BPF_IND;
8568 struct bpf_insn *insn = insn_buf;
8571 *insn++ = BPF_MOV64_IMM(BPF_REG_2, orig->imm);
8573 *insn++ = BPF_MOV64_REG(BPF_REG_2, orig->src_reg);
8575 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, orig->imm);
8577 /* We're guaranteed here that CTX is in R6. */
8578 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_CTX);
8580 switch (BPF_SIZE(orig->code)) {
8582 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_8_no_cache);
8585 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_16_no_cache);
8588 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_32_no_cache);
8592 *insn++ = BPF_JMP_IMM(BPF_JSGE, BPF_REG_0, 0, 2);
8593 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_0, BPF_REG_0);
8594 *insn++ = BPF_EXIT_INSN();
8596 return insn - insn_buf;
8599 static int tc_cls_act_prologue(struct bpf_insn *insn_buf, bool direct_write,
8600 const struct bpf_prog *prog)
8602 return bpf_unclone_prologue(insn_buf, direct_write, prog, TC_ACT_SHOT);
8605 static bool tc_cls_act_is_valid_access(int off, int size,
8606 enum bpf_access_type type,
8607 const struct bpf_prog *prog,
8608 struct bpf_insn_access_aux *info)
8610 if (type == BPF_WRITE) {
8612 case bpf_ctx_range(struct __sk_buff, mark):
8613 case bpf_ctx_range(struct __sk_buff, tc_index):
8614 case bpf_ctx_range(struct __sk_buff, priority):
8615 case bpf_ctx_range(struct __sk_buff, tc_classid):
8616 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
8617 case bpf_ctx_range(struct __sk_buff, tstamp):
8618 case bpf_ctx_range(struct __sk_buff, queue_mapping):
8626 case bpf_ctx_range(struct __sk_buff, data):
8627 info->reg_type = PTR_TO_PACKET;
8629 case bpf_ctx_range(struct __sk_buff, data_meta):
8630 info->reg_type = PTR_TO_PACKET_META;
8632 case bpf_ctx_range(struct __sk_buff, data_end):
8633 info->reg_type = PTR_TO_PACKET_END;
8635 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
8637 case offsetof(struct __sk_buff, tstamp_type):
8638 /* The convert_ctx_access() on reading and writing
8639 * __sk_buff->tstamp depends on whether the bpf prog
8640 * has used __sk_buff->tstamp_type or not.
8641 * Thus, we need to set prog->tstamp_type_access
8642 * earlier during is_valid_access() here.
8644 ((struct bpf_prog *)prog)->tstamp_type_access = 1;
8645 return size == sizeof(__u8);
8648 return bpf_skb_is_valid_access(off, size, type, prog, info);
8651 DEFINE_MUTEX(nf_conn_btf_access_lock);
8652 EXPORT_SYMBOL_GPL(nf_conn_btf_access_lock);
8654 int (*nfct_btf_struct_access)(struct bpf_verifier_log *log, const struct btf *btf,
8655 const struct btf_type *t, int off, int size,
8656 enum bpf_access_type atype, u32 *next_btf_id,
8657 enum bpf_type_flag *flag);
8658 EXPORT_SYMBOL_GPL(nfct_btf_struct_access);
8660 static int tc_cls_act_btf_struct_access(struct bpf_verifier_log *log,
8661 const struct btf *btf,
8662 const struct btf_type *t, int off,
8663 int size, enum bpf_access_type atype,
8665 enum bpf_type_flag *flag)
8669 if (atype == BPF_READ)
8670 return btf_struct_access(log, btf, t, off, size, atype, next_btf_id,
8673 mutex_lock(&nf_conn_btf_access_lock);
8674 if (nfct_btf_struct_access)
8675 ret = nfct_btf_struct_access(log, btf, t, off, size, atype, next_btf_id, flag);
8676 mutex_unlock(&nf_conn_btf_access_lock);
8681 static bool __is_valid_xdp_access(int off, int size)
8683 if (off < 0 || off >= sizeof(struct xdp_md))
8685 if (off % size != 0)
8687 if (size != sizeof(__u32))
8693 static bool xdp_is_valid_access(int off, int size,
8694 enum bpf_access_type type,
8695 const struct bpf_prog *prog,
8696 struct bpf_insn_access_aux *info)
8698 if (prog->expected_attach_type != BPF_XDP_DEVMAP) {
8700 case offsetof(struct xdp_md, egress_ifindex):
8705 if (type == BPF_WRITE) {
8706 if (bpf_prog_is_dev_bound(prog->aux)) {
8708 case offsetof(struct xdp_md, rx_queue_index):
8709 return __is_valid_xdp_access(off, size);
8716 case offsetof(struct xdp_md, data):
8717 info->reg_type = PTR_TO_PACKET;
8719 case offsetof(struct xdp_md, data_meta):
8720 info->reg_type = PTR_TO_PACKET_META;
8722 case offsetof(struct xdp_md, data_end):
8723 info->reg_type = PTR_TO_PACKET_END;
8727 return __is_valid_xdp_access(off, size);
8730 void bpf_warn_invalid_xdp_action(struct net_device *dev, struct bpf_prog *prog, u32 act)
8732 const u32 act_max = XDP_REDIRECT;
8734 pr_warn_once("%s XDP return value %u on prog %s (id %d) dev %s, expect packet loss!\n",
8735 act > act_max ? "Illegal" : "Driver unsupported",
8736 act, prog->aux->name, prog->aux->id, dev ? dev->name : "N/A");
8738 EXPORT_SYMBOL_GPL(bpf_warn_invalid_xdp_action);
8740 static int xdp_btf_struct_access(struct bpf_verifier_log *log,
8741 const struct btf *btf,
8742 const struct btf_type *t, int off,
8743 int size, enum bpf_access_type atype,
8745 enum bpf_type_flag *flag)
8749 if (atype == BPF_READ)
8750 return btf_struct_access(log, btf, t, off, size, atype, next_btf_id,
8753 mutex_lock(&nf_conn_btf_access_lock);
8754 if (nfct_btf_struct_access)
8755 ret = nfct_btf_struct_access(log, btf, t, off, size, atype, next_btf_id, flag);
8756 mutex_unlock(&nf_conn_btf_access_lock);
8761 static bool sock_addr_is_valid_access(int off, int size,
8762 enum bpf_access_type type,
8763 const struct bpf_prog *prog,
8764 struct bpf_insn_access_aux *info)
8766 const int size_default = sizeof(__u32);
8768 if (off < 0 || off >= sizeof(struct bpf_sock_addr))
8770 if (off % size != 0)
8773 /* Disallow access to IPv6 fields from IPv4 contex and vise
8777 case bpf_ctx_range(struct bpf_sock_addr, user_ip4):
8778 switch (prog->expected_attach_type) {
8779 case BPF_CGROUP_INET4_BIND:
8780 case BPF_CGROUP_INET4_CONNECT:
8781 case BPF_CGROUP_INET4_GETPEERNAME:
8782 case BPF_CGROUP_INET4_GETSOCKNAME:
8783 case BPF_CGROUP_UDP4_SENDMSG:
8784 case BPF_CGROUP_UDP4_RECVMSG:
8790 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
8791 switch (prog->expected_attach_type) {
8792 case BPF_CGROUP_INET6_BIND:
8793 case BPF_CGROUP_INET6_CONNECT:
8794 case BPF_CGROUP_INET6_GETPEERNAME:
8795 case BPF_CGROUP_INET6_GETSOCKNAME:
8796 case BPF_CGROUP_UDP6_SENDMSG:
8797 case BPF_CGROUP_UDP6_RECVMSG:
8803 case bpf_ctx_range(struct bpf_sock_addr, msg_src_ip4):
8804 switch (prog->expected_attach_type) {
8805 case BPF_CGROUP_UDP4_SENDMSG:
8811 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
8813 switch (prog->expected_attach_type) {
8814 case BPF_CGROUP_UDP6_SENDMSG:
8823 case bpf_ctx_range(struct bpf_sock_addr, user_ip4):
8824 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
8825 case bpf_ctx_range(struct bpf_sock_addr, msg_src_ip4):
8826 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
8828 case bpf_ctx_range(struct bpf_sock_addr, user_port):
8829 if (type == BPF_READ) {
8830 bpf_ctx_record_field_size(info, size_default);
8832 if (bpf_ctx_wide_access_ok(off, size,
8833 struct bpf_sock_addr,
8837 if (bpf_ctx_wide_access_ok(off, size,
8838 struct bpf_sock_addr,
8842 if (!bpf_ctx_narrow_access_ok(off, size, size_default))
8845 if (bpf_ctx_wide_access_ok(off, size,
8846 struct bpf_sock_addr,
8850 if (bpf_ctx_wide_access_ok(off, size,
8851 struct bpf_sock_addr,
8855 if (size != size_default)
8859 case offsetof(struct bpf_sock_addr, sk):
8860 if (type != BPF_READ)
8862 if (size != sizeof(__u64))
8864 info->reg_type = PTR_TO_SOCKET;
8867 if (type == BPF_READ) {
8868 if (size != size_default)
8878 static bool sock_ops_is_valid_access(int off, int size,
8879 enum bpf_access_type type,
8880 const struct bpf_prog *prog,
8881 struct bpf_insn_access_aux *info)
8883 const int size_default = sizeof(__u32);
8885 if (off < 0 || off >= sizeof(struct bpf_sock_ops))
8888 /* The verifier guarantees that size > 0. */
8889 if (off % size != 0)
8892 if (type == BPF_WRITE) {
8894 case offsetof(struct bpf_sock_ops, reply):
8895 case offsetof(struct bpf_sock_ops, sk_txhash):
8896 if (size != size_default)
8904 case bpf_ctx_range_till(struct bpf_sock_ops, bytes_received,
8906 if (size != sizeof(__u64))
8909 case offsetof(struct bpf_sock_ops, sk):
8910 if (size != sizeof(__u64))
8912 info->reg_type = PTR_TO_SOCKET_OR_NULL;
8914 case offsetof(struct bpf_sock_ops, skb_data):
8915 if (size != sizeof(__u64))
8917 info->reg_type = PTR_TO_PACKET;
8919 case offsetof(struct bpf_sock_ops, skb_data_end):
8920 if (size != sizeof(__u64))
8922 info->reg_type = PTR_TO_PACKET_END;
8924 case offsetof(struct bpf_sock_ops, skb_tcp_flags):
8925 bpf_ctx_record_field_size(info, size_default);
8926 return bpf_ctx_narrow_access_ok(off, size,
8928 case offsetof(struct bpf_sock_ops, skb_hwtstamp):
8929 if (size != sizeof(__u64))
8933 if (size != size_default)
8942 static int sk_skb_prologue(struct bpf_insn *insn_buf, bool direct_write,
8943 const struct bpf_prog *prog)
8945 return bpf_unclone_prologue(insn_buf, direct_write, prog, SK_DROP);
8948 static bool sk_skb_is_valid_access(int off, int size,
8949 enum bpf_access_type type,
8950 const struct bpf_prog *prog,
8951 struct bpf_insn_access_aux *info)
8954 case bpf_ctx_range(struct __sk_buff, tc_classid):
8955 case bpf_ctx_range(struct __sk_buff, data_meta):
8956 case bpf_ctx_range(struct __sk_buff, tstamp):
8957 case bpf_ctx_range(struct __sk_buff, wire_len):
8958 case bpf_ctx_range(struct __sk_buff, hwtstamp):
8962 if (type == BPF_WRITE) {
8964 case bpf_ctx_range(struct __sk_buff, tc_index):
8965 case bpf_ctx_range(struct __sk_buff, priority):
8973 case bpf_ctx_range(struct __sk_buff, mark):
8975 case bpf_ctx_range(struct __sk_buff, data):
8976 info->reg_type = PTR_TO_PACKET;
8978 case bpf_ctx_range(struct __sk_buff, data_end):
8979 info->reg_type = PTR_TO_PACKET_END;
8983 return bpf_skb_is_valid_access(off, size, type, prog, info);
8986 static bool sk_msg_is_valid_access(int off, int size,
8987 enum bpf_access_type type,
8988 const struct bpf_prog *prog,
8989 struct bpf_insn_access_aux *info)
8991 if (type == BPF_WRITE)
8994 if (off % size != 0)
8998 case offsetof(struct sk_msg_md, data):
8999 info->reg_type = PTR_TO_PACKET;
9000 if (size != sizeof(__u64))
9003 case offsetof(struct sk_msg_md, data_end):
9004 info->reg_type = PTR_TO_PACKET_END;
9005 if (size != sizeof(__u64))
9008 case offsetof(struct sk_msg_md, sk):
9009 if (size != sizeof(__u64))
9011 info->reg_type = PTR_TO_SOCKET;
9013 case bpf_ctx_range(struct sk_msg_md, family):
9014 case bpf_ctx_range(struct sk_msg_md, remote_ip4):
9015 case bpf_ctx_range(struct sk_msg_md, local_ip4):
9016 case bpf_ctx_range_till(struct sk_msg_md, remote_ip6[0], remote_ip6[3]):
9017 case bpf_ctx_range_till(struct sk_msg_md, local_ip6[0], local_ip6[3]):
9018 case bpf_ctx_range(struct sk_msg_md, remote_port):
9019 case bpf_ctx_range(struct sk_msg_md, local_port):
9020 case bpf_ctx_range(struct sk_msg_md, size):
9021 if (size != sizeof(__u32))
9030 static bool flow_dissector_is_valid_access(int off, int size,
9031 enum bpf_access_type type,
9032 const struct bpf_prog *prog,
9033 struct bpf_insn_access_aux *info)
9035 const int size_default = sizeof(__u32);
9037 if (off < 0 || off >= sizeof(struct __sk_buff))
9040 if (type == BPF_WRITE)
9044 case bpf_ctx_range(struct __sk_buff, data):
9045 if (size != size_default)
9047 info->reg_type = PTR_TO_PACKET;
9049 case bpf_ctx_range(struct __sk_buff, data_end):
9050 if (size != size_default)
9052 info->reg_type = PTR_TO_PACKET_END;
9054 case bpf_ctx_range_ptr(struct __sk_buff, flow_keys):
9055 if (size != sizeof(__u64))
9057 info->reg_type = PTR_TO_FLOW_KEYS;
9064 static u32 flow_dissector_convert_ctx_access(enum bpf_access_type type,
9065 const struct bpf_insn *si,
9066 struct bpf_insn *insn_buf,
9067 struct bpf_prog *prog,
9071 struct bpf_insn *insn = insn_buf;
9074 case offsetof(struct __sk_buff, data):
9075 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_flow_dissector, data),
9076 si->dst_reg, si->src_reg,
9077 offsetof(struct bpf_flow_dissector, data));
9080 case offsetof(struct __sk_buff, data_end):
9081 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_flow_dissector, data_end),
9082 si->dst_reg, si->src_reg,
9083 offsetof(struct bpf_flow_dissector, data_end));
9086 case offsetof(struct __sk_buff, flow_keys):
9087 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_flow_dissector, flow_keys),
9088 si->dst_reg, si->src_reg,
9089 offsetof(struct bpf_flow_dissector, flow_keys));
9093 return insn - insn_buf;
9096 static struct bpf_insn *bpf_convert_tstamp_type_read(const struct bpf_insn *si,
9097 struct bpf_insn *insn)
9099 __u8 value_reg = si->dst_reg;
9100 __u8 skb_reg = si->src_reg;
9101 /* AX is needed because src_reg and dst_reg could be the same */
9102 __u8 tmp_reg = BPF_REG_AX;
9104 *insn++ = BPF_LDX_MEM(BPF_B, tmp_reg, skb_reg,
9105 PKT_VLAN_PRESENT_OFFSET);
9106 *insn++ = BPF_JMP32_IMM(BPF_JSET, tmp_reg,
9107 SKB_MONO_DELIVERY_TIME_MASK, 2);
9108 *insn++ = BPF_MOV32_IMM(value_reg, BPF_SKB_TSTAMP_UNSPEC);
9109 *insn++ = BPF_JMP_A(1);
9110 *insn++ = BPF_MOV32_IMM(value_reg, BPF_SKB_TSTAMP_DELIVERY_MONO);
9115 static struct bpf_insn *bpf_convert_shinfo_access(__u8 dst_reg, __u8 skb_reg,
9116 struct bpf_insn *insn)
9118 /* si->dst_reg = skb_shinfo(SKB); */
9119 #ifdef NET_SKBUFF_DATA_USES_OFFSET
9120 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, end),
9121 BPF_REG_AX, skb_reg,
9122 offsetof(struct sk_buff, end));
9123 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, head),
9125 offsetof(struct sk_buff, head));
9126 *insn++ = BPF_ALU64_REG(BPF_ADD, dst_reg, BPF_REG_AX);
9128 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, end),
9130 offsetof(struct sk_buff, end));
9136 static struct bpf_insn *bpf_convert_tstamp_read(const struct bpf_prog *prog,
9137 const struct bpf_insn *si,
9138 struct bpf_insn *insn)
9140 __u8 value_reg = si->dst_reg;
9141 __u8 skb_reg = si->src_reg;
9143 #ifdef CONFIG_NET_CLS_ACT
9144 /* If the tstamp_type is read,
9145 * the bpf prog is aware the tstamp could have delivery time.
9146 * Thus, read skb->tstamp as is if tstamp_type_access is true.
9148 if (!prog->tstamp_type_access) {
9149 /* AX is needed because src_reg and dst_reg could be the same */
9150 __u8 tmp_reg = BPF_REG_AX;
9152 *insn++ = BPF_LDX_MEM(BPF_B, tmp_reg, skb_reg, PKT_VLAN_PRESENT_OFFSET);
9153 *insn++ = BPF_ALU32_IMM(BPF_AND, tmp_reg,
9154 TC_AT_INGRESS_MASK | SKB_MONO_DELIVERY_TIME_MASK);
9155 *insn++ = BPF_JMP32_IMM(BPF_JNE, tmp_reg,
9156 TC_AT_INGRESS_MASK | SKB_MONO_DELIVERY_TIME_MASK, 2);
9157 /* skb->tc_at_ingress && skb->mono_delivery_time,
9158 * read 0 as the (rcv) timestamp.
9160 *insn++ = BPF_MOV64_IMM(value_reg, 0);
9161 *insn++ = BPF_JMP_A(1);
9165 *insn++ = BPF_LDX_MEM(BPF_DW, value_reg, skb_reg,
9166 offsetof(struct sk_buff, tstamp));
9170 static struct bpf_insn *bpf_convert_tstamp_write(const struct bpf_prog *prog,
9171 const struct bpf_insn *si,
9172 struct bpf_insn *insn)
9174 __u8 value_reg = si->src_reg;
9175 __u8 skb_reg = si->dst_reg;
9177 #ifdef CONFIG_NET_CLS_ACT
9178 /* If the tstamp_type is read,
9179 * the bpf prog is aware the tstamp could have delivery time.
9180 * Thus, write skb->tstamp as is if tstamp_type_access is true.
9181 * Otherwise, writing at ingress will have to clear the
9182 * mono_delivery_time bit also.
9184 if (!prog->tstamp_type_access) {
9185 __u8 tmp_reg = BPF_REG_AX;
9187 *insn++ = BPF_LDX_MEM(BPF_B, tmp_reg, skb_reg, PKT_VLAN_PRESENT_OFFSET);
9188 /* Writing __sk_buff->tstamp as ingress, goto <clear> */
9189 *insn++ = BPF_JMP32_IMM(BPF_JSET, tmp_reg, TC_AT_INGRESS_MASK, 1);
9191 *insn++ = BPF_JMP_A(2);
9192 /* <clear>: mono_delivery_time */
9193 *insn++ = BPF_ALU32_IMM(BPF_AND, tmp_reg, ~SKB_MONO_DELIVERY_TIME_MASK);
9194 *insn++ = BPF_STX_MEM(BPF_B, skb_reg, tmp_reg, PKT_VLAN_PRESENT_OFFSET);
9198 /* <store>: skb->tstamp = tstamp */
9199 *insn++ = BPF_STX_MEM(BPF_DW, skb_reg, value_reg,
9200 offsetof(struct sk_buff, tstamp));
9204 static u32 bpf_convert_ctx_access(enum bpf_access_type type,
9205 const struct bpf_insn *si,
9206 struct bpf_insn *insn_buf,
9207 struct bpf_prog *prog, u32 *target_size)
9209 struct bpf_insn *insn = insn_buf;
9213 case offsetof(struct __sk_buff, len):
9214 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
9215 bpf_target_off(struct sk_buff, len, 4,
9219 case offsetof(struct __sk_buff, protocol):
9220 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
9221 bpf_target_off(struct sk_buff, protocol, 2,
9225 case offsetof(struct __sk_buff, vlan_proto):
9226 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
9227 bpf_target_off(struct sk_buff, vlan_proto, 2,
9231 case offsetof(struct __sk_buff, priority):
9232 if (type == BPF_WRITE)
9233 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
9234 bpf_target_off(struct sk_buff, priority, 4,
9237 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
9238 bpf_target_off(struct sk_buff, priority, 4,
9242 case offsetof(struct __sk_buff, ingress_ifindex):
9243 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
9244 bpf_target_off(struct sk_buff, skb_iif, 4,
9248 case offsetof(struct __sk_buff, ifindex):
9249 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
9250 si->dst_reg, si->src_reg,
9251 offsetof(struct sk_buff, dev));
9252 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
9253 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
9254 bpf_target_off(struct net_device, ifindex, 4,
9258 case offsetof(struct __sk_buff, hash):
9259 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
9260 bpf_target_off(struct sk_buff, hash, 4,
9264 case offsetof(struct __sk_buff, mark):
9265 if (type == BPF_WRITE)
9266 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
9267 bpf_target_off(struct sk_buff, mark, 4,
9270 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
9271 bpf_target_off(struct sk_buff, mark, 4,
9275 case offsetof(struct __sk_buff, pkt_type):
9277 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->src_reg,
9279 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, PKT_TYPE_MAX);
9280 #ifdef __BIG_ENDIAN_BITFIELD
9281 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, 5);
9285 case offsetof(struct __sk_buff, queue_mapping):
9286 if (type == BPF_WRITE) {
9287 *insn++ = BPF_JMP_IMM(BPF_JGE, si->src_reg, NO_QUEUE_MAPPING, 1);
9288 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg, si->src_reg,
9289 bpf_target_off(struct sk_buff,
9293 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
9294 bpf_target_off(struct sk_buff,
9300 case offsetof(struct __sk_buff, vlan_present):
9301 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
9302 bpf_target_off(struct sk_buff,
9303 vlan_all, 4, target_size));
9304 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
9305 *insn++ = BPF_ALU32_IMM(BPF_MOV, si->dst_reg, 1);
9308 case offsetof(struct __sk_buff, vlan_tci):
9309 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
9310 bpf_target_off(struct sk_buff, vlan_tci, 2,
9314 case offsetof(struct __sk_buff, cb[0]) ...
9315 offsetofend(struct __sk_buff, cb[4]) - 1:
9316 BUILD_BUG_ON(sizeof_field(struct qdisc_skb_cb, data) < 20);
9317 BUILD_BUG_ON((offsetof(struct sk_buff, cb) +
9318 offsetof(struct qdisc_skb_cb, data)) %
9321 prog->cb_access = 1;
9323 off -= offsetof(struct __sk_buff, cb[0]);
9324 off += offsetof(struct sk_buff, cb);
9325 off += offsetof(struct qdisc_skb_cb, data);
9326 if (type == BPF_WRITE)
9327 *insn++ = BPF_STX_MEM(BPF_SIZE(si->code), si->dst_reg,
9330 *insn++ = BPF_LDX_MEM(BPF_SIZE(si->code), si->dst_reg,
9334 case offsetof(struct __sk_buff, tc_classid):
9335 BUILD_BUG_ON(sizeof_field(struct qdisc_skb_cb, tc_classid) != 2);
9338 off -= offsetof(struct __sk_buff, tc_classid);
9339 off += offsetof(struct sk_buff, cb);
9340 off += offsetof(struct qdisc_skb_cb, tc_classid);
9342 if (type == BPF_WRITE)
9343 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg,
9346 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg,
9350 case offsetof(struct __sk_buff, data):
9351 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
9352 si->dst_reg, si->src_reg,
9353 offsetof(struct sk_buff, data));
9356 case offsetof(struct __sk_buff, data_meta):
9358 off -= offsetof(struct __sk_buff, data_meta);
9359 off += offsetof(struct sk_buff, cb);
9360 off += offsetof(struct bpf_skb_data_end, data_meta);
9361 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
9365 case offsetof(struct __sk_buff, data_end):
9367 off -= offsetof(struct __sk_buff, data_end);
9368 off += offsetof(struct sk_buff, cb);
9369 off += offsetof(struct bpf_skb_data_end, data_end);
9370 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
9374 case offsetof(struct __sk_buff, tc_index):
9375 #ifdef CONFIG_NET_SCHED
9376 if (type == BPF_WRITE)
9377 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg, si->src_reg,
9378 bpf_target_off(struct sk_buff, tc_index, 2,
9381 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
9382 bpf_target_off(struct sk_buff, tc_index, 2,
9386 if (type == BPF_WRITE)
9387 *insn++ = BPF_MOV64_REG(si->dst_reg, si->dst_reg);
9389 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
9393 case offsetof(struct __sk_buff, napi_id):
9394 #if defined(CONFIG_NET_RX_BUSY_POLL)
9395 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
9396 bpf_target_off(struct sk_buff, napi_id, 4,
9398 *insn++ = BPF_JMP_IMM(BPF_JGE, si->dst_reg, MIN_NAPI_ID, 1);
9399 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
9402 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
9405 case offsetof(struct __sk_buff, family):
9406 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_family) != 2);
9408 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
9409 si->dst_reg, si->src_reg,
9410 offsetof(struct sk_buff, sk));
9411 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
9412 bpf_target_off(struct sock_common,
9416 case offsetof(struct __sk_buff, remote_ip4):
9417 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_daddr) != 4);
9419 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
9420 si->dst_reg, si->src_reg,
9421 offsetof(struct sk_buff, sk));
9422 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
9423 bpf_target_off(struct sock_common,
9427 case offsetof(struct __sk_buff, local_ip4):
9428 BUILD_BUG_ON(sizeof_field(struct sock_common,
9429 skc_rcv_saddr) != 4);
9431 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
9432 si->dst_reg, si->src_reg,
9433 offsetof(struct sk_buff, sk));
9434 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
9435 bpf_target_off(struct sock_common,
9439 case offsetof(struct __sk_buff, remote_ip6[0]) ...
9440 offsetof(struct __sk_buff, remote_ip6[3]):
9441 #if IS_ENABLED(CONFIG_IPV6)
9442 BUILD_BUG_ON(sizeof_field(struct sock_common,
9443 skc_v6_daddr.s6_addr32[0]) != 4);
9446 off -= offsetof(struct __sk_buff, remote_ip6[0]);
9448 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
9449 si->dst_reg, si->src_reg,
9450 offsetof(struct sk_buff, sk));
9451 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
9452 offsetof(struct sock_common,
9453 skc_v6_daddr.s6_addr32[0]) +
9456 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
9459 case offsetof(struct __sk_buff, local_ip6[0]) ...
9460 offsetof(struct __sk_buff, local_ip6[3]):
9461 #if IS_ENABLED(CONFIG_IPV6)
9462 BUILD_BUG_ON(sizeof_field(struct sock_common,
9463 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
9466 off -= offsetof(struct __sk_buff, local_ip6[0]);
9468 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
9469 si->dst_reg, si->src_reg,
9470 offsetof(struct sk_buff, sk));
9471 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
9472 offsetof(struct sock_common,
9473 skc_v6_rcv_saddr.s6_addr32[0]) +
9476 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
9480 case offsetof(struct __sk_buff, remote_port):
9481 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_dport) != 2);
9483 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
9484 si->dst_reg, si->src_reg,
9485 offsetof(struct sk_buff, sk));
9486 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
9487 bpf_target_off(struct sock_common,
9490 #ifndef __BIG_ENDIAN_BITFIELD
9491 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
9495 case offsetof(struct __sk_buff, local_port):
9496 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_num) != 2);
9498 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
9499 si->dst_reg, si->src_reg,
9500 offsetof(struct sk_buff, sk));
9501 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
9502 bpf_target_off(struct sock_common,
9503 skc_num, 2, target_size));
9506 case offsetof(struct __sk_buff, tstamp):
9507 BUILD_BUG_ON(sizeof_field(struct sk_buff, tstamp) != 8);
9509 if (type == BPF_WRITE)
9510 insn = bpf_convert_tstamp_write(prog, si, insn);
9512 insn = bpf_convert_tstamp_read(prog, si, insn);
9515 case offsetof(struct __sk_buff, tstamp_type):
9516 insn = bpf_convert_tstamp_type_read(si, insn);
9519 case offsetof(struct __sk_buff, gso_segs):
9520 insn = bpf_convert_shinfo_access(si->dst_reg, si->src_reg, insn);
9521 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct skb_shared_info, gso_segs),
9522 si->dst_reg, si->dst_reg,
9523 bpf_target_off(struct skb_shared_info,
9527 case offsetof(struct __sk_buff, gso_size):
9528 insn = bpf_convert_shinfo_access(si->dst_reg, si->src_reg, insn);
9529 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct skb_shared_info, gso_size),
9530 si->dst_reg, si->dst_reg,
9531 bpf_target_off(struct skb_shared_info,
9535 case offsetof(struct __sk_buff, wire_len):
9536 BUILD_BUG_ON(sizeof_field(struct qdisc_skb_cb, pkt_len) != 4);
9539 off -= offsetof(struct __sk_buff, wire_len);
9540 off += offsetof(struct sk_buff, cb);
9541 off += offsetof(struct qdisc_skb_cb, pkt_len);
9543 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, off);
9546 case offsetof(struct __sk_buff, sk):
9547 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
9548 si->dst_reg, si->src_reg,
9549 offsetof(struct sk_buff, sk));
9551 case offsetof(struct __sk_buff, hwtstamp):
9552 BUILD_BUG_ON(sizeof_field(struct skb_shared_hwtstamps, hwtstamp) != 8);
9553 BUILD_BUG_ON(offsetof(struct skb_shared_hwtstamps, hwtstamp) != 0);
9555 insn = bpf_convert_shinfo_access(si->dst_reg, si->src_reg, insn);
9556 *insn++ = BPF_LDX_MEM(BPF_DW,
9557 si->dst_reg, si->dst_reg,
9558 bpf_target_off(struct skb_shared_info,
9564 return insn - insn_buf;
9567 u32 bpf_sock_convert_ctx_access(enum bpf_access_type type,
9568 const struct bpf_insn *si,
9569 struct bpf_insn *insn_buf,
9570 struct bpf_prog *prog, u32 *target_size)
9572 struct bpf_insn *insn = insn_buf;
9576 case offsetof(struct bpf_sock, bound_dev_if):
9577 BUILD_BUG_ON(sizeof_field(struct sock, sk_bound_dev_if) != 4);
9579 if (type == BPF_WRITE)
9580 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
9581 offsetof(struct sock, sk_bound_dev_if));
9583 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
9584 offsetof(struct sock, sk_bound_dev_if));
9587 case offsetof(struct bpf_sock, mark):
9588 BUILD_BUG_ON(sizeof_field(struct sock, sk_mark) != 4);
9590 if (type == BPF_WRITE)
9591 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
9592 offsetof(struct sock, sk_mark));
9594 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
9595 offsetof(struct sock, sk_mark));
9598 case offsetof(struct bpf_sock, priority):
9599 BUILD_BUG_ON(sizeof_field(struct sock, sk_priority) != 4);
9601 if (type == BPF_WRITE)
9602 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
9603 offsetof(struct sock, sk_priority));
9605 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
9606 offsetof(struct sock, sk_priority));
9609 case offsetof(struct bpf_sock, family):
9610 *insn++ = BPF_LDX_MEM(
9611 BPF_FIELD_SIZEOF(struct sock_common, skc_family),
9612 si->dst_reg, si->src_reg,
9613 bpf_target_off(struct sock_common,
9615 sizeof_field(struct sock_common,
9620 case offsetof(struct bpf_sock, type):
9621 *insn++ = BPF_LDX_MEM(
9622 BPF_FIELD_SIZEOF(struct sock, sk_type),
9623 si->dst_reg, si->src_reg,
9624 bpf_target_off(struct sock, sk_type,
9625 sizeof_field(struct sock, sk_type),
9629 case offsetof(struct bpf_sock, protocol):
9630 *insn++ = BPF_LDX_MEM(
9631 BPF_FIELD_SIZEOF(struct sock, sk_protocol),
9632 si->dst_reg, si->src_reg,
9633 bpf_target_off(struct sock, sk_protocol,
9634 sizeof_field(struct sock, sk_protocol),
9638 case offsetof(struct bpf_sock, src_ip4):
9639 *insn++ = BPF_LDX_MEM(
9640 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
9641 bpf_target_off(struct sock_common, skc_rcv_saddr,
9642 sizeof_field(struct sock_common,
9647 case offsetof(struct bpf_sock, dst_ip4):
9648 *insn++ = BPF_LDX_MEM(
9649 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
9650 bpf_target_off(struct sock_common, skc_daddr,
9651 sizeof_field(struct sock_common,
9656 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
9657 #if IS_ENABLED(CONFIG_IPV6)
9659 off -= offsetof(struct bpf_sock, src_ip6[0]);
9660 *insn++ = BPF_LDX_MEM(
9661 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
9664 skc_v6_rcv_saddr.s6_addr32[0],
9665 sizeof_field(struct sock_common,
9666 skc_v6_rcv_saddr.s6_addr32[0]),
9667 target_size) + off);
9670 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
9674 case bpf_ctx_range_till(struct bpf_sock, dst_ip6[0], dst_ip6[3]):
9675 #if IS_ENABLED(CONFIG_IPV6)
9677 off -= offsetof(struct bpf_sock, dst_ip6[0]);
9678 *insn++ = BPF_LDX_MEM(
9679 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
9680 bpf_target_off(struct sock_common,
9681 skc_v6_daddr.s6_addr32[0],
9682 sizeof_field(struct sock_common,
9683 skc_v6_daddr.s6_addr32[0]),
9684 target_size) + off);
9686 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
9691 case offsetof(struct bpf_sock, src_port):
9692 *insn++ = BPF_LDX_MEM(
9693 BPF_FIELD_SIZEOF(struct sock_common, skc_num),
9694 si->dst_reg, si->src_reg,
9695 bpf_target_off(struct sock_common, skc_num,
9696 sizeof_field(struct sock_common,
9701 case offsetof(struct bpf_sock, dst_port):
9702 *insn++ = BPF_LDX_MEM(
9703 BPF_FIELD_SIZEOF(struct sock_common, skc_dport),
9704 si->dst_reg, si->src_reg,
9705 bpf_target_off(struct sock_common, skc_dport,
9706 sizeof_field(struct sock_common,
9711 case offsetof(struct bpf_sock, state):
9712 *insn++ = BPF_LDX_MEM(
9713 BPF_FIELD_SIZEOF(struct sock_common, skc_state),
9714 si->dst_reg, si->src_reg,
9715 bpf_target_off(struct sock_common, skc_state,
9716 sizeof_field(struct sock_common,
9720 case offsetof(struct bpf_sock, rx_queue_mapping):
9721 #ifdef CONFIG_SOCK_RX_QUEUE_MAPPING
9722 *insn++ = BPF_LDX_MEM(
9723 BPF_FIELD_SIZEOF(struct sock, sk_rx_queue_mapping),
9724 si->dst_reg, si->src_reg,
9725 bpf_target_off(struct sock, sk_rx_queue_mapping,
9726 sizeof_field(struct sock,
9727 sk_rx_queue_mapping),
9729 *insn++ = BPF_JMP_IMM(BPF_JNE, si->dst_reg, NO_QUEUE_MAPPING,
9731 *insn++ = BPF_MOV64_IMM(si->dst_reg, -1);
9733 *insn++ = BPF_MOV64_IMM(si->dst_reg, -1);
9739 return insn - insn_buf;
9742 static u32 tc_cls_act_convert_ctx_access(enum bpf_access_type type,
9743 const struct bpf_insn *si,
9744 struct bpf_insn *insn_buf,
9745 struct bpf_prog *prog, u32 *target_size)
9747 struct bpf_insn *insn = insn_buf;
9750 case offsetof(struct __sk_buff, ifindex):
9751 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
9752 si->dst_reg, si->src_reg,
9753 offsetof(struct sk_buff, dev));
9754 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
9755 bpf_target_off(struct net_device, ifindex, 4,
9759 return bpf_convert_ctx_access(type, si, insn_buf, prog,
9763 return insn - insn_buf;
9766 static u32 xdp_convert_ctx_access(enum bpf_access_type type,
9767 const struct bpf_insn *si,
9768 struct bpf_insn *insn_buf,
9769 struct bpf_prog *prog, u32 *target_size)
9771 struct bpf_insn *insn = insn_buf;
9774 case offsetof(struct xdp_md, data):
9775 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data),
9776 si->dst_reg, si->src_reg,
9777 offsetof(struct xdp_buff, data));
9779 case offsetof(struct xdp_md, data_meta):
9780 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_meta),
9781 si->dst_reg, si->src_reg,
9782 offsetof(struct xdp_buff, data_meta));
9784 case offsetof(struct xdp_md, data_end):
9785 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_end),
9786 si->dst_reg, si->src_reg,
9787 offsetof(struct xdp_buff, data_end));
9789 case offsetof(struct xdp_md, ingress_ifindex):
9790 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, rxq),
9791 si->dst_reg, si->src_reg,
9792 offsetof(struct xdp_buff, rxq));
9793 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_rxq_info, dev),
9794 si->dst_reg, si->dst_reg,
9795 offsetof(struct xdp_rxq_info, dev));
9796 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
9797 offsetof(struct net_device, ifindex));
9799 case offsetof(struct xdp_md, rx_queue_index):
9800 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, rxq),
9801 si->dst_reg, si->src_reg,
9802 offsetof(struct xdp_buff, rxq));
9803 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
9804 offsetof(struct xdp_rxq_info,
9807 case offsetof(struct xdp_md, egress_ifindex):
9808 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, txq),
9809 si->dst_reg, si->src_reg,
9810 offsetof(struct xdp_buff, txq));
9811 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_txq_info, dev),
9812 si->dst_reg, si->dst_reg,
9813 offsetof(struct xdp_txq_info, dev));
9814 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
9815 offsetof(struct net_device, ifindex));
9819 return insn - insn_buf;
9822 /* SOCK_ADDR_LOAD_NESTED_FIELD() loads Nested Field S.F.NF where S is type of
9823 * context Structure, F is Field in context structure that contains a pointer
9824 * to Nested Structure of type NS that has the field NF.
9826 * SIZE encodes the load size (BPF_B, BPF_H, etc). It's up to caller to make
9827 * sure that SIZE is not greater than actual size of S.F.NF.
9829 * If offset OFF is provided, the load happens from that offset relative to
9832 #define SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, SIZE, OFF) \
9834 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), si->dst_reg, \
9835 si->src_reg, offsetof(S, F)); \
9836 *insn++ = BPF_LDX_MEM( \
9837 SIZE, si->dst_reg, si->dst_reg, \
9838 bpf_target_off(NS, NF, sizeof_field(NS, NF), \
9843 #define SOCK_ADDR_LOAD_NESTED_FIELD(S, NS, F, NF) \
9844 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, \
9845 BPF_FIELD_SIZEOF(NS, NF), 0)
9847 /* SOCK_ADDR_STORE_NESTED_FIELD_OFF() has semantic similar to
9848 * SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF() but for store operation.
9850 * In addition it uses Temporary Field TF (member of struct S) as the 3rd
9851 * "register" since two registers available in convert_ctx_access are not
9852 * enough: we can't override neither SRC, since it contains value to store, nor
9853 * DST since it contains pointer to context that may be used by later
9854 * instructions. But we need a temporary place to save pointer to nested
9855 * structure whose field we want to store to.
9857 #define SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, SIZE, OFF, TF) \
9859 int tmp_reg = BPF_REG_9; \
9860 if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg) \
9862 if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg) \
9864 *insn++ = BPF_STX_MEM(BPF_DW, si->dst_reg, tmp_reg, \
9866 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), tmp_reg, \
9867 si->dst_reg, offsetof(S, F)); \
9868 *insn++ = BPF_STX_MEM(SIZE, tmp_reg, si->src_reg, \
9869 bpf_target_off(NS, NF, sizeof_field(NS, NF), \
9872 *insn++ = BPF_LDX_MEM(BPF_DW, tmp_reg, si->dst_reg, \
9876 #define SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, SIZE, OFF, \
9879 if (type == BPF_WRITE) { \
9880 SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, SIZE, \
9883 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF( \
9884 S, NS, F, NF, SIZE, OFF); \
9888 #define SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD(S, NS, F, NF, TF) \
9889 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF( \
9890 S, NS, F, NF, BPF_FIELD_SIZEOF(NS, NF), 0, TF)
9892 static u32 sock_addr_convert_ctx_access(enum bpf_access_type type,
9893 const struct bpf_insn *si,
9894 struct bpf_insn *insn_buf,
9895 struct bpf_prog *prog, u32 *target_size)
9897 int off, port_size = sizeof_field(struct sockaddr_in6, sin6_port);
9898 struct bpf_insn *insn = insn_buf;
9901 case offsetof(struct bpf_sock_addr, user_family):
9902 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
9903 struct sockaddr, uaddr, sa_family);
9906 case offsetof(struct bpf_sock_addr, user_ip4):
9907 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
9908 struct bpf_sock_addr_kern, struct sockaddr_in, uaddr,
9909 sin_addr, BPF_SIZE(si->code), 0, tmp_reg);
9912 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
9914 off -= offsetof(struct bpf_sock_addr, user_ip6[0]);
9915 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
9916 struct bpf_sock_addr_kern, struct sockaddr_in6, uaddr,
9917 sin6_addr.s6_addr32[0], BPF_SIZE(si->code), off,
9921 case offsetof(struct bpf_sock_addr, user_port):
9922 /* To get port we need to know sa_family first and then treat
9923 * sockaddr as either sockaddr_in or sockaddr_in6.
9924 * Though we can simplify since port field has same offset and
9925 * size in both structures.
9926 * Here we check this invariant and use just one of the
9927 * structures if it's true.
9929 BUILD_BUG_ON(offsetof(struct sockaddr_in, sin_port) !=
9930 offsetof(struct sockaddr_in6, sin6_port));
9931 BUILD_BUG_ON(sizeof_field(struct sockaddr_in, sin_port) !=
9932 sizeof_field(struct sockaddr_in6, sin6_port));
9933 /* Account for sin6_port being smaller than user_port. */
9934 port_size = min(port_size, BPF_LDST_BYTES(si));
9935 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
9936 struct bpf_sock_addr_kern, struct sockaddr_in6, uaddr,
9937 sin6_port, bytes_to_bpf_size(port_size), 0, tmp_reg);
9940 case offsetof(struct bpf_sock_addr, family):
9941 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
9942 struct sock, sk, sk_family);
9945 case offsetof(struct bpf_sock_addr, type):
9946 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
9947 struct sock, sk, sk_type);
9950 case offsetof(struct bpf_sock_addr, protocol):
9951 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
9952 struct sock, sk, sk_protocol);
9955 case offsetof(struct bpf_sock_addr, msg_src_ip4):
9956 /* Treat t_ctx as struct in_addr for msg_src_ip4. */
9957 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
9958 struct bpf_sock_addr_kern, struct in_addr, t_ctx,
9959 s_addr, BPF_SIZE(si->code), 0, tmp_reg);
9962 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
9965 off -= offsetof(struct bpf_sock_addr, msg_src_ip6[0]);
9966 /* Treat t_ctx as struct in6_addr for msg_src_ip6. */
9967 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
9968 struct bpf_sock_addr_kern, struct in6_addr, t_ctx,
9969 s6_addr32[0], BPF_SIZE(si->code), off, tmp_reg);
9971 case offsetof(struct bpf_sock_addr, sk):
9972 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_addr_kern, sk),
9973 si->dst_reg, si->src_reg,
9974 offsetof(struct bpf_sock_addr_kern, sk));
9978 return insn - insn_buf;
9981 static u32 sock_ops_convert_ctx_access(enum bpf_access_type type,
9982 const struct bpf_insn *si,
9983 struct bpf_insn *insn_buf,
9984 struct bpf_prog *prog,
9987 struct bpf_insn *insn = insn_buf;
9990 /* Helper macro for adding read access to tcp_sock or sock fields. */
9991 #define SOCK_OPS_GET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ) \
9993 int fullsock_reg = si->dst_reg, reg = BPF_REG_9, jmp = 2; \
9994 BUILD_BUG_ON(sizeof_field(OBJ, OBJ_FIELD) > \
9995 sizeof_field(struct bpf_sock_ops, BPF_FIELD)); \
9996 if (si->dst_reg == reg || si->src_reg == reg) \
9998 if (si->dst_reg == reg || si->src_reg == reg) \
10000 if (si->dst_reg == si->src_reg) { \
10001 *insn++ = BPF_STX_MEM(BPF_DW, si->src_reg, reg, \
10002 offsetof(struct bpf_sock_ops_kern, \
10004 fullsock_reg = reg; \
10007 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
10008 struct bpf_sock_ops_kern, \
10010 fullsock_reg, si->src_reg, \
10011 offsetof(struct bpf_sock_ops_kern, \
10013 *insn++ = BPF_JMP_IMM(BPF_JEQ, fullsock_reg, 0, jmp); \
10014 if (si->dst_reg == si->src_reg) \
10015 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->src_reg, \
10016 offsetof(struct bpf_sock_ops_kern, \
10018 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
10019 struct bpf_sock_ops_kern, sk),\
10020 si->dst_reg, si->src_reg, \
10021 offsetof(struct bpf_sock_ops_kern, sk));\
10022 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(OBJ, \
10024 si->dst_reg, si->dst_reg, \
10025 offsetof(OBJ, OBJ_FIELD)); \
10026 if (si->dst_reg == si->src_reg) { \
10027 *insn++ = BPF_JMP_A(1); \
10028 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->src_reg, \
10029 offsetof(struct bpf_sock_ops_kern, \
10034 #define SOCK_OPS_GET_SK() \
10036 int fullsock_reg = si->dst_reg, reg = BPF_REG_9, jmp = 1; \
10037 if (si->dst_reg == reg || si->src_reg == reg) \
10039 if (si->dst_reg == reg || si->src_reg == reg) \
10041 if (si->dst_reg == si->src_reg) { \
10042 *insn++ = BPF_STX_MEM(BPF_DW, si->src_reg, reg, \
10043 offsetof(struct bpf_sock_ops_kern, \
10045 fullsock_reg = reg; \
10048 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
10049 struct bpf_sock_ops_kern, \
10051 fullsock_reg, si->src_reg, \
10052 offsetof(struct bpf_sock_ops_kern, \
10054 *insn++ = BPF_JMP_IMM(BPF_JEQ, fullsock_reg, 0, jmp); \
10055 if (si->dst_reg == si->src_reg) \
10056 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->src_reg, \
10057 offsetof(struct bpf_sock_ops_kern, \
10059 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
10060 struct bpf_sock_ops_kern, sk),\
10061 si->dst_reg, si->src_reg, \
10062 offsetof(struct bpf_sock_ops_kern, sk));\
10063 if (si->dst_reg == si->src_reg) { \
10064 *insn++ = BPF_JMP_A(1); \
10065 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->src_reg, \
10066 offsetof(struct bpf_sock_ops_kern, \
10071 #define SOCK_OPS_GET_TCP_SOCK_FIELD(FIELD) \
10072 SOCK_OPS_GET_FIELD(FIELD, FIELD, struct tcp_sock)
10074 /* Helper macro for adding write access to tcp_sock or sock fields.
10075 * The macro is called with two registers, dst_reg which contains a pointer
10076 * to ctx (context) and src_reg which contains the value that should be
10077 * stored. However, we need an additional register since we cannot overwrite
10078 * dst_reg because it may be used later in the program.
10079 * Instead we "borrow" one of the other register. We first save its value
10080 * into a new (temp) field in bpf_sock_ops_kern, use it, and then restore
10081 * it at the end of the macro.
10083 #define SOCK_OPS_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ) \
10085 int reg = BPF_REG_9; \
10086 BUILD_BUG_ON(sizeof_field(OBJ, OBJ_FIELD) > \
10087 sizeof_field(struct bpf_sock_ops, BPF_FIELD)); \
10088 if (si->dst_reg == reg || si->src_reg == reg) \
10090 if (si->dst_reg == reg || si->src_reg == reg) \
10092 *insn++ = BPF_STX_MEM(BPF_DW, si->dst_reg, reg, \
10093 offsetof(struct bpf_sock_ops_kern, \
10095 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
10096 struct bpf_sock_ops_kern, \
10098 reg, si->dst_reg, \
10099 offsetof(struct bpf_sock_ops_kern, \
10101 *insn++ = BPF_JMP_IMM(BPF_JEQ, reg, 0, 2); \
10102 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
10103 struct bpf_sock_ops_kern, sk),\
10104 reg, si->dst_reg, \
10105 offsetof(struct bpf_sock_ops_kern, sk));\
10106 *insn++ = BPF_STX_MEM(BPF_FIELD_SIZEOF(OBJ, OBJ_FIELD), \
10107 reg, si->src_reg, \
10108 offsetof(OBJ, OBJ_FIELD)); \
10109 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->dst_reg, \
10110 offsetof(struct bpf_sock_ops_kern, \
10114 #define SOCK_OPS_GET_OR_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ, TYPE) \
10116 if (TYPE == BPF_WRITE) \
10117 SOCK_OPS_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ); \
10119 SOCK_OPS_GET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ); \
10122 if (insn > insn_buf)
10123 return insn - insn_buf;
10126 case offsetof(struct bpf_sock_ops, op):
10127 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_ops_kern,
10129 si->dst_reg, si->src_reg,
10130 offsetof(struct bpf_sock_ops_kern, op));
10133 case offsetof(struct bpf_sock_ops, replylong[0]) ...
10134 offsetof(struct bpf_sock_ops, replylong[3]):
10135 BUILD_BUG_ON(sizeof_field(struct bpf_sock_ops, reply) !=
10136 sizeof_field(struct bpf_sock_ops_kern, reply));
10137 BUILD_BUG_ON(sizeof_field(struct bpf_sock_ops, replylong) !=
10138 sizeof_field(struct bpf_sock_ops_kern, replylong));
10140 off -= offsetof(struct bpf_sock_ops, replylong[0]);
10141 off += offsetof(struct bpf_sock_ops_kern, replylong[0]);
10142 if (type == BPF_WRITE)
10143 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
10146 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
10150 case offsetof(struct bpf_sock_ops, family):
10151 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_family) != 2);
10153 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10154 struct bpf_sock_ops_kern, sk),
10155 si->dst_reg, si->src_reg,
10156 offsetof(struct bpf_sock_ops_kern, sk));
10157 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
10158 offsetof(struct sock_common, skc_family));
10161 case offsetof(struct bpf_sock_ops, remote_ip4):
10162 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_daddr) != 4);
10164 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10165 struct bpf_sock_ops_kern, sk),
10166 si->dst_reg, si->src_reg,
10167 offsetof(struct bpf_sock_ops_kern, sk));
10168 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
10169 offsetof(struct sock_common, skc_daddr));
10172 case offsetof(struct bpf_sock_ops, local_ip4):
10173 BUILD_BUG_ON(sizeof_field(struct sock_common,
10174 skc_rcv_saddr) != 4);
10176 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10177 struct bpf_sock_ops_kern, sk),
10178 si->dst_reg, si->src_reg,
10179 offsetof(struct bpf_sock_ops_kern, sk));
10180 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
10181 offsetof(struct sock_common,
10185 case offsetof(struct bpf_sock_ops, remote_ip6[0]) ...
10186 offsetof(struct bpf_sock_ops, remote_ip6[3]):
10187 #if IS_ENABLED(CONFIG_IPV6)
10188 BUILD_BUG_ON(sizeof_field(struct sock_common,
10189 skc_v6_daddr.s6_addr32[0]) != 4);
10192 off -= offsetof(struct bpf_sock_ops, remote_ip6[0]);
10193 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10194 struct bpf_sock_ops_kern, sk),
10195 si->dst_reg, si->src_reg,
10196 offsetof(struct bpf_sock_ops_kern, sk));
10197 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
10198 offsetof(struct sock_common,
10199 skc_v6_daddr.s6_addr32[0]) +
10202 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
10206 case offsetof(struct bpf_sock_ops, local_ip6[0]) ...
10207 offsetof(struct bpf_sock_ops, local_ip6[3]):
10208 #if IS_ENABLED(CONFIG_IPV6)
10209 BUILD_BUG_ON(sizeof_field(struct sock_common,
10210 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
10213 off -= offsetof(struct bpf_sock_ops, local_ip6[0]);
10214 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10215 struct bpf_sock_ops_kern, sk),
10216 si->dst_reg, si->src_reg,
10217 offsetof(struct bpf_sock_ops_kern, sk));
10218 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
10219 offsetof(struct sock_common,
10220 skc_v6_rcv_saddr.s6_addr32[0]) +
10223 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
10227 case offsetof(struct bpf_sock_ops, remote_port):
10228 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_dport) != 2);
10230 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10231 struct bpf_sock_ops_kern, sk),
10232 si->dst_reg, si->src_reg,
10233 offsetof(struct bpf_sock_ops_kern, sk));
10234 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
10235 offsetof(struct sock_common, skc_dport));
10236 #ifndef __BIG_ENDIAN_BITFIELD
10237 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
10241 case offsetof(struct bpf_sock_ops, local_port):
10242 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_num) != 2);
10244 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10245 struct bpf_sock_ops_kern, sk),
10246 si->dst_reg, si->src_reg,
10247 offsetof(struct bpf_sock_ops_kern, sk));
10248 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
10249 offsetof(struct sock_common, skc_num));
10252 case offsetof(struct bpf_sock_ops, is_fullsock):
10253 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10254 struct bpf_sock_ops_kern,
10256 si->dst_reg, si->src_reg,
10257 offsetof(struct bpf_sock_ops_kern,
10261 case offsetof(struct bpf_sock_ops, state):
10262 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_state) != 1);
10264 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10265 struct bpf_sock_ops_kern, sk),
10266 si->dst_reg, si->src_reg,
10267 offsetof(struct bpf_sock_ops_kern, sk));
10268 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->dst_reg,
10269 offsetof(struct sock_common, skc_state));
10272 case offsetof(struct bpf_sock_ops, rtt_min):
10273 BUILD_BUG_ON(sizeof_field(struct tcp_sock, rtt_min) !=
10274 sizeof(struct minmax));
10275 BUILD_BUG_ON(sizeof(struct minmax) <
10276 sizeof(struct minmax_sample));
10278 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10279 struct bpf_sock_ops_kern, sk),
10280 si->dst_reg, si->src_reg,
10281 offsetof(struct bpf_sock_ops_kern, sk));
10282 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
10283 offsetof(struct tcp_sock, rtt_min) +
10284 sizeof_field(struct minmax_sample, t));
10287 case offsetof(struct bpf_sock_ops, bpf_sock_ops_cb_flags):
10288 SOCK_OPS_GET_FIELD(bpf_sock_ops_cb_flags, bpf_sock_ops_cb_flags,
10292 case offsetof(struct bpf_sock_ops, sk_txhash):
10293 SOCK_OPS_GET_OR_SET_FIELD(sk_txhash, sk_txhash,
10294 struct sock, type);
10296 case offsetof(struct bpf_sock_ops, snd_cwnd):
10297 SOCK_OPS_GET_TCP_SOCK_FIELD(snd_cwnd);
10299 case offsetof(struct bpf_sock_ops, srtt_us):
10300 SOCK_OPS_GET_TCP_SOCK_FIELD(srtt_us);
10302 case offsetof(struct bpf_sock_ops, snd_ssthresh):
10303 SOCK_OPS_GET_TCP_SOCK_FIELD(snd_ssthresh);
10305 case offsetof(struct bpf_sock_ops, rcv_nxt):
10306 SOCK_OPS_GET_TCP_SOCK_FIELD(rcv_nxt);
10308 case offsetof(struct bpf_sock_ops, snd_nxt):
10309 SOCK_OPS_GET_TCP_SOCK_FIELD(snd_nxt);
10311 case offsetof(struct bpf_sock_ops, snd_una):
10312 SOCK_OPS_GET_TCP_SOCK_FIELD(snd_una);
10314 case offsetof(struct bpf_sock_ops, mss_cache):
10315 SOCK_OPS_GET_TCP_SOCK_FIELD(mss_cache);
10317 case offsetof(struct bpf_sock_ops, ecn_flags):
10318 SOCK_OPS_GET_TCP_SOCK_FIELD(ecn_flags);
10320 case offsetof(struct bpf_sock_ops, rate_delivered):
10321 SOCK_OPS_GET_TCP_SOCK_FIELD(rate_delivered);
10323 case offsetof(struct bpf_sock_ops, rate_interval_us):
10324 SOCK_OPS_GET_TCP_SOCK_FIELD(rate_interval_us);
10326 case offsetof(struct bpf_sock_ops, packets_out):
10327 SOCK_OPS_GET_TCP_SOCK_FIELD(packets_out);
10329 case offsetof(struct bpf_sock_ops, retrans_out):
10330 SOCK_OPS_GET_TCP_SOCK_FIELD(retrans_out);
10332 case offsetof(struct bpf_sock_ops, total_retrans):
10333 SOCK_OPS_GET_TCP_SOCK_FIELD(total_retrans);
10335 case offsetof(struct bpf_sock_ops, segs_in):
10336 SOCK_OPS_GET_TCP_SOCK_FIELD(segs_in);
10338 case offsetof(struct bpf_sock_ops, data_segs_in):
10339 SOCK_OPS_GET_TCP_SOCK_FIELD(data_segs_in);
10341 case offsetof(struct bpf_sock_ops, segs_out):
10342 SOCK_OPS_GET_TCP_SOCK_FIELD(segs_out);
10344 case offsetof(struct bpf_sock_ops, data_segs_out):
10345 SOCK_OPS_GET_TCP_SOCK_FIELD(data_segs_out);
10347 case offsetof(struct bpf_sock_ops, lost_out):
10348 SOCK_OPS_GET_TCP_SOCK_FIELD(lost_out);
10350 case offsetof(struct bpf_sock_ops, sacked_out):
10351 SOCK_OPS_GET_TCP_SOCK_FIELD(sacked_out);
10353 case offsetof(struct bpf_sock_ops, bytes_received):
10354 SOCK_OPS_GET_TCP_SOCK_FIELD(bytes_received);
10356 case offsetof(struct bpf_sock_ops, bytes_acked):
10357 SOCK_OPS_GET_TCP_SOCK_FIELD(bytes_acked);
10359 case offsetof(struct bpf_sock_ops, sk):
10362 case offsetof(struct bpf_sock_ops, skb_data_end):
10363 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_ops_kern,
10365 si->dst_reg, si->src_reg,
10366 offsetof(struct bpf_sock_ops_kern,
10369 case offsetof(struct bpf_sock_ops, skb_data):
10370 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_ops_kern,
10372 si->dst_reg, si->src_reg,
10373 offsetof(struct bpf_sock_ops_kern,
10375 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
10376 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
10377 si->dst_reg, si->dst_reg,
10378 offsetof(struct sk_buff, data));
10380 case offsetof(struct bpf_sock_ops, skb_len):
10381 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_ops_kern,
10383 si->dst_reg, si->src_reg,
10384 offsetof(struct bpf_sock_ops_kern,
10386 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
10387 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, len),
10388 si->dst_reg, si->dst_reg,
10389 offsetof(struct sk_buff, len));
10391 case offsetof(struct bpf_sock_ops, skb_tcp_flags):
10392 off = offsetof(struct sk_buff, cb);
10393 off += offsetof(struct tcp_skb_cb, tcp_flags);
10394 *target_size = sizeof_field(struct tcp_skb_cb, tcp_flags);
10395 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_ops_kern,
10397 si->dst_reg, si->src_reg,
10398 offsetof(struct bpf_sock_ops_kern,
10400 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
10401 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct tcp_skb_cb,
10403 si->dst_reg, si->dst_reg, off);
10405 case offsetof(struct bpf_sock_ops, skb_hwtstamp): {
10406 struct bpf_insn *jmp_on_null_skb;
10408 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_ops_kern,
10410 si->dst_reg, si->src_reg,
10411 offsetof(struct bpf_sock_ops_kern,
10413 /* Reserve one insn to test skb == NULL */
10414 jmp_on_null_skb = insn++;
10415 insn = bpf_convert_shinfo_access(si->dst_reg, si->dst_reg, insn);
10416 *insn++ = BPF_LDX_MEM(BPF_DW, si->dst_reg, si->dst_reg,
10417 bpf_target_off(struct skb_shared_info,
10420 *jmp_on_null_skb = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0,
10421 insn - jmp_on_null_skb - 1);
10425 return insn - insn_buf;
10428 /* data_end = skb->data + skb_headlen() */
10429 static struct bpf_insn *bpf_convert_data_end_access(const struct bpf_insn *si,
10430 struct bpf_insn *insn)
10433 int temp_reg_off = offsetof(struct sk_buff, cb) +
10434 offsetof(struct sk_skb_cb, temp_reg);
10436 if (si->src_reg == si->dst_reg) {
10437 /* We need an extra register, choose and save a register. */
10439 if (si->src_reg == reg || si->dst_reg == reg)
10441 if (si->src_reg == reg || si->dst_reg == reg)
10443 *insn++ = BPF_STX_MEM(BPF_DW, si->src_reg, reg, temp_reg_off);
10448 /* reg = skb->data */
10449 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
10451 offsetof(struct sk_buff, data));
10452 /* AX = skb->len */
10453 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, len),
10454 BPF_REG_AX, si->src_reg,
10455 offsetof(struct sk_buff, len));
10456 /* reg = skb->data + skb->len */
10457 *insn++ = BPF_ALU64_REG(BPF_ADD, reg, BPF_REG_AX);
10458 /* AX = skb->data_len */
10459 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data_len),
10460 BPF_REG_AX, si->src_reg,
10461 offsetof(struct sk_buff, data_len));
10463 /* reg = skb->data + skb->len - skb->data_len */
10464 *insn++ = BPF_ALU64_REG(BPF_SUB, reg, BPF_REG_AX);
10466 if (si->src_reg == si->dst_reg) {
10467 /* Restore the saved register */
10468 *insn++ = BPF_MOV64_REG(BPF_REG_AX, si->src_reg);
10469 *insn++ = BPF_MOV64_REG(si->dst_reg, reg);
10470 *insn++ = BPF_LDX_MEM(BPF_DW, reg, BPF_REG_AX, temp_reg_off);
10476 static u32 sk_skb_convert_ctx_access(enum bpf_access_type type,
10477 const struct bpf_insn *si,
10478 struct bpf_insn *insn_buf,
10479 struct bpf_prog *prog, u32 *target_size)
10481 struct bpf_insn *insn = insn_buf;
10485 case offsetof(struct __sk_buff, data_end):
10486 insn = bpf_convert_data_end_access(si, insn);
10488 case offsetof(struct __sk_buff, cb[0]) ...
10489 offsetofend(struct __sk_buff, cb[4]) - 1:
10490 BUILD_BUG_ON(sizeof_field(struct sk_skb_cb, data) < 20);
10491 BUILD_BUG_ON((offsetof(struct sk_buff, cb) +
10492 offsetof(struct sk_skb_cb, data)) %
10495 prog->cb_access = 1;
10497 off -= offsetof(struct __sk_buff, cb[0]);
10498 off += offsetof(struct sk_buff, cb);
10499 off += offsetof(struct sk_skb_cb, data);
10500 if (type == BPF_WRITE)
10501 *insn++ = BPF_STX_MEM(BPF_SIZE(si->code), si->dst_reg,
10504 *insn++ = BPF_LDX_MEM(BPF_SIZE(si->code), si->dst_reg,
10510 return bpf_convert_ctx_access(type, si, insn_buf, prog,
10514 return insn - insn_buf;
10517 static u32 sk_msg_convert_ctx_access(enum bpf_access_type type,
10518 const struct bpf_insn *si,
10519 struct bpf_insn *insn_buf,
10520 struct bpf_prog *prog, u32 *target_size)
10522 struct bpf_insn *insn = insn_buf;
10523 #if IS_ENABLED(CONFIG_IPV6)
10527 /* convert ctx uses the fact sg element is first in struct */
10528 BUILD_BUG_ON(offsetof(struct sk_msg, sg) != 0);
10531 case offsetof(struct sk_msg_md, data):
10532 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg, data),
10533 si->dst_reg, si->src_reg,
10534 offsetof(struct sk_msg, data));
10536 case offsetof(struct sk_msg_md, data_end):
10537 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg, data_end),
10538 si->dst_reg, si->src_reg,
10539 offsetof(struct sk_msg, data_end));
10541 case offsetof(struct sk_msg_md, family):
10542 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_family) != 2);
10544 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10545 struct sk_msg, sk),
10546 si->dst_reg, si->src_reg,
10547 offsetof(struct sk_msg, sk));
10548 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
10549 offsetof(struct sock_common, skc_family));
10552 case offsetof(struct sk_msg_md, remote_ip4):
10553 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_daddr) != 4);
10555 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10556 struct sk_msg, sk),
10557 si->dst_reg, si->src_reg,
10558 offsetof(struct sk_msg, sk));
10559 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
10560 offsetof(struct sock_common, skc_daddr));
10563 case offsetof(struct sk_msg_md, local_ip4):
10564 BUILD_BUG_ON(sizeof_field(struct sock_common,
10565 skc_rcv_saddr) != 4);
10567 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10568 struct sk_msg, sk),
10569 si->dst_reg, si->src_reg,
10570 offsetof(struct sk_msg, sk));
10571 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
10572 offsetof(struct sock_common,
10576 case offsetof(struct sk_msg_md, remote_ip6[0]) ...
10577 offsetof(struct sk_msg_md, remote_ip6[3]):
10578 #if IS_ENABLED(CONFIG_IPV6)
10579 BUILD_BUG_ON(sizeof_field(struct sock_common,
10580 skc_v6_daddr.s6_addr32[0]) != 4);
10583 off -= offsetof(struct sk_msg_md, remote_ip6[0]);
10584 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10585 struct sk_msg, sk),
10586 si->dst_reg, si->src_reg,
10587 offsetof(struct sk_msg, sk));
10588 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
10589 offsetof(struct sock_common,
10590 skc_v6_daddr.s6_addr32[0]) +
10593 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
10597 case offsetof(struct sk_msg_md, local_ip6[0]) ...
10598 offsetof(struct sk_msg_md, local_ip6[3]):
10599 #if IS_ENABLED(CONFIG_IPV6)
10600 BUILD_BUG_ON(sizeof_field(struct sock_common,
10601 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
10604 off -= offsetof(struct sk_msg_md, local_ip6[0]);
10605 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10606 struct sk_msg, sk),
10607 si->dst_reg, si->src_reg,
10608 offsetof(struct sk_msg, sk));
10609 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
10610 offsetof(struct sock_common,
10611 skc_v6_rcv_saddr.s6_addr32[0]) +
10614 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
10618 case offsetof(struct sk_msg_md, remote_port):
10619 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_dport) != 2);
10621 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10622 struct sk_msg, sk),
10623 si->dst_reg, si->src_reg,
10624 offsetof(struct sk_msg, sk));
10625 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
10626 offsetof(struct sock_common, skc_dport));
10627 #ifndef __BIG_ENDIAN_BITFIELD
10628 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
10632 case offsetof(struct sk_msg_md, local_port):
10633 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_num) != 2);
10635 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
10636 struct sk_msg, sk),
10637 si->dst_reg, si->src_reg,
10638 offsetof(struct sk_msg, sk));
10639 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
10640 offsetof(struct sock_common, skc_num));
10643 case offsetof(struct sk_msg_md, size):
10644 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg_sg, size),
10645 si->dst_reg, si->src_reg,
10646 offsetof(struct sk_msg_sg, size));
10649 case offsetof(struct sk_msg_md, sk):
10650 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg, sk),
10651 si->dst_reg, si->src_reg,
10652 offsetof(struct sk_msg, sk));
10656 return insn - insn_buf;
10659 const struct bpf_verifier_ops sk_filter_verifier_ops = {
10660 .get_func_proto = sk_filter_func_proto,
10661 .is_valid_access = sk_filter_is_valid_access,
10662 .convert_ctx_access = bpf_convert_ctx_access,
10663 .gen_ld_abs = bpf_gen_ld_abs,
10666 const struct bpf_prog_ops sk_filter_prog_ops = {
10667 .test_run = bpf_prog_test_run_skb,
10670 const struct bpf_verifier_ops tc_cls_act_verifier_ops = {
10671 .get_func_proto = tc_cls_act_func_proto,
10672 .is_valid_access = tc_cls_act_is_valid_access,
10673 .convert_ctx_access = tc_cls_act_convert_ctx_access,
10674 .gen_prologue = tc_cls_act_prologue,
10675 .gen_ld_abs = bpf_gen_ld_abs,
10676 .btf_struct_access = tc_cls_act_btf_struct_access,
10679 const struct bpf_prog_ops tc_cls_act_prog_ops = {
10680 .test_run = bpf_prog_test_run_skb,
10683 const struct bpf_verifier_ops xdp_verifier_ops = {
10684 .get_func_proto = xdp_func_proto,
10685 .is_valid_access = xdp_is_valid_access,
10686 .convert_ctx_access = xdp_convert_ctx_access,
10687 .gen_prologue = bpf_noop_prologue,
10688 .btf_struct_access = xdp_btf_struct_access,
10691 const struct bpf_prog_ops xdp_prog_ops = {
10692 .test_run = bpf_prog_test_run_xdp,
10695 const struct bpf_verifier_ops cg_skb_verifier_ops = {
10696 .get_func_proto = cg_skb_func_proto,
10697 .is_valid_access = cg_skb_is_valid_access,
10698 .convert_ctx_access = bpf_convert_ctx_access,
10701 const struct bpf_prog_ops cg_skb_prog_ops = {
10702 .test_run = bpf_prog_test_run_skb,
10705 const struct bpf_verifier_ops lwt_in_verifier_ops = {
10706 .get_func_proto = lwt_in_func_proto,
10707 .is_valid_access = lwt_is_valid_access,
10708 .convert_ctx_access = bpf_convert_ctx_access,
10711 const struct bpf_prog_ops lwt_in_prog_ops = {
10712 .test_run = bpf_prog_test_run_skb,
10715 const struct bpf_verifier_ops lwt_out_verifier_ops = {
10716 .get_func_proto = lwt_out_func_proto,
10717 .is_valid_access = lwt_is_valid_access,
10718 .convert_ctx_access = bpf_convert_ctx_access,
10721 const struct bpf_prog_ops lwt_out_prog_ops = {
10722 .test_run = bpf_prog_test_run_skb,
10725 const struct bpf_verifier_ops lwt_xmit_verifier_ops = {
10726 .get_func_proto = lwt_xmit_func_proto,
10727 .is_valid_access = lwt_is_valid_access,
10728 .convert_ctx_access = bpf_convert_ctx_access,
10729 .gen_prologue = tc_cls_act_prologue,
10732 const struct bpf_prog_ops lwt_xmit_prog_ops = {
10733 .test_run = bpf_prog_test_run_skb,
10736 const struct bpf_verifier_ops lwt_seg6local_verifier_ops = {
10737 .get_func_proto = lwt_seg6local_func_proto,
10738 .is_valid_access = lwt_is_valid_access,
10739 .convert_ctx_access = bpf_convert_ctx_access,
10742 const struct bpf_prog_ops lwt_seg6local_prog_ops = {
10743 .test_run = bpf_prog_test_run_skb,
10746 const struct bpf_verifier_ops cg_sock_verifier_ops = {
10747 .get_func_proto = sock_filter_func_proto,
10748 .is_valid_access = sock_filter_is_valid_access,
10749 .convert_ctx_access = bpf_sock_convert_ctx_access,
10752 const struct bpf_prog_ops cg_sock_prog_ops = {
10755 const struct bpf_verifier_ops cg_sock_addr_verifier_ops = {
10756 .get_func_proto = sock_addr_func_proto,
10757 .is_valid_access = sock_addr_is_valid_access,
10758 .convert_ctx_access = sock_addr_convert_ctx_access,
10761 const struct bpf_prog_ops cg_sock_addr_prog_ops = {
10764 const struct bpf_verifier_ops sock_ops_verifier_ops = {
10765 .get_func_proto = sock_ops_func_proto,
10766 .is_valid_access = sock_ops_is_valid_access,
10767 .convert_ctx_access = sock_ops_convert_ctx_access,
10770 const struct bpf_prog_ops sock_ops_prog_ops = {
10773 const struct bpf_verifier_ops sk_skb_verifier_ops = {
10774 .get_func_proto = sk_skb_func_proto,
10775 .is_valid_access = sk_skb_is_valid_access,
10776 .convert_ctx_access = sk_skb_convert_ctx_access,
10777 .gen_prologue = sk_skb_prologue,
10780 const struct bpf_prog_ops sk_skb_prog_ops = {
10783 const struct bpf_verifier_ops sk_msg_verifier_ops = {
10784 .get_func_proto = sk_msg_func_proto,
10785 .is_valid_access = sk_msg_is_valid_access,
10786 .convert_ctx_access = sk_msg_convert_ctx_access,
10787 .gen_prologue = bpf_noop_prologue,
10790 const struct bpf_prog_ops sk_msg_prog_ops = {
10793 const struct bpf_verifier_ops flow_dissector_verifier_ops = {
10794 .get_func_proto = flow_dissector_func_proto,
10795 .is_valid_access = flow_dissector_is_valid_access,
10796 .convert_ctx_access = flow_dissector_convert_ctx_access,
10799 const struct bpf_prog_ops flow_dissector_prog_ops = {
10800 .test_run = bpf_prog_test_run_flow_dissector,
10803 int sk_detach_filter(struct sock *sk)
10806 struct sk_filter *filter;
10808 if (sock_flag(sk, SOCK_FILTER_LOCKED))
10811 filter = rcu_dereference_protected(sk->sk_filter,
10812 lockdep_sock_is_held(sk));
10814 RCU_INIT_POINTER(sk->sk_filter, NULL);
10815 sk_filter_uncharge(sk, filter);
10821 EXPORT_SYMBOL_GPL(sk_detach_filter);
10823 int sk_get_filter(struct sock *sk, sockptr_t optval, unsigned int len)
10825 struct sock_fprog_kern *fprog;
10826 struct sk_filter *filter;
10829 sockopt_lock_sock(sk);
10830 filter = rcu_dereference_protected(sk->sk_filter,
10831 lockdep_sock_is_held(sk));
10835 /* We're copying the filter that has been originally attached,
10836 * so no conversion/decode needed anymore. eBPF programs that
10837 * have no original program cannot be dumped through this.
10840 fprog = filter->prog->orig_prog;
10846 /* User space only enquires number of filter blocks. */
10850 if (len < fprog->len)
10854 if (copy_to_sockptr(optval, fprog->filter, bpf_classic_proglen(fprog)))
10857 /* Instead of bytes, the API requests to return the number
10858 * of filter blocks.
10862 sockopt_release_sock(sk);
10867 static void bpf_init_reuseport_kern(struct sk_reuseport_kern *reuse_kern,
10868 struct sock_reuseport *reuse,
10869 struct sock *sk, struct sk_buff *skb,
10870 struct sock *migrating_sk,
10873 reuse_kern->skb = skb;
10874 reuse_kern->sk = sk;
10875 reuse_kern->selected_sk = NULL;
10876 reuse_kern->migrating_sk = migrating_sk;
10877 reuse_kern->data_end = skb->data + skb_headlen(skb);
10878 reuse_kern->hash = hash;
10879 reuse_kern->reuseport_id = reuse->reuseport_id;
10880 reuse_kern->bind_inany = reuse->bind_inany;
10883 struct sock *bpf_run_sk_reuseport(struct sock_reuseport *reuse, struct sock *sk,
10884 struct bpf_prog *prog, struct sk_buff *skb,
10885 struct sock *migrating_sk,
10888 struct sk_reuseport_kern reuse_kern;
10889 enum sk_action action;
10891 bpf_init_reuseport_kern(&reuse_kern, reuse, sk, skb, migrating_sk, hash);
10892 action = bpf_prog_run(prog, &reuse_kern);
10894 if (action == SK_PASS)
10895 return reuse_kern.selected_sk;
10897 return ERR_PTR(-ECONNREFUSED);
10900 BPF_CALL_4(sk_select_reuseport, struct sk_reuseport_kern *, reuse_kern,
10901 struct bpf_map *, map, void *, key, u32, flags)
10903 bool is_sockarray = map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY;
10904 struct sock_reuseport *reuse;
10905 struct sock *selected_sk;
10907 selected_sk = map->ops->map_lookup_elem(map, key);
10911 reuse = rcu_dereference(selected_sk->sk_reuseport_cb);
10913 /* Lookup in sock_map can return TCP ESTABLISHED sockets. */
10914 if (sk_is_refcounted(selected_sk))
10915 sock_put(selected_sk);
10917 /* reuseport_array has only sk with non NULL sk_reuseport_cb.
10918 * The only (!reuse) case here is - the sk has already been
10919 * unhashed (e.g. by close()), so treat it as -ENOENT.
10921 * Other maps (e.g. sock_map) do not provide this guarantee and
10922 * the sk may never be in the reuseport group to begin with.
10924 return is_sockarray ? -ENOENT : -EINVAL;
10927 if (unlikely(reuse->reuseport_id != reuse_kern->reuseport_id)) {
10928 struct sock *sk = reuse_kern->sk;
10930 if (sk->sk_protocol != selected_sk->sk_protocol)
10931 return -EPROTOTYPE;
10932 else if (sk->sk_family != selected_sk->sk_family)
10933 return -EAFNOSUPPORT;
10935 /* Catch all. Likely bound to a different sockaddr. */
10939 reuse_kern->selected_sk = selected_sk;
10944 static const struct bpf_func_proto sk_select_reuseport_proto = {
10945 .func = sk_select_reuseport,
10947 .ret_type = RET_INTEGER,
10948 .arg1_type = ARG_PTR_TO_CTX,
10949 .arg2_type = ARG_CONST_MAP_PTR,
10950 .arg3_type = ARG_PTR_TO_MAP_KEY,
10951 .arg4_type = ARG_ANYTHING,
10954 BPF_CALL_4(sk_reuseport_load_bytes,
10955 const struct sk_reuseport_kern *, reuse_kern, u32, offset,
10956 void *, to, u32, len)
10958 return ____bpf_skb_load_bytes(reuse_kern->skb, offset, to, len);
10961 static const struct bpf_func_proto sk_reuseport_load_bytes_proto = {
10962 .func = sk_reuseport_load_bytes,
10964 .ret_type = RET_INTEGER,
10965 .arg1_type = ARG_PTR_TO_CTX,
10966 .arg2_type = ARG_ANYTHING,
10967 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
10968 .arg4_type = ARG_CONST_SIZE,
10971 BPF_CALL_5(sk_reuseport_load_bytes_relative,
10972 const struct sk_reuseport_kern *, reuse_kern, u32, offset,
10973 void *, to, u32, len, u32, start_header)
10975 return ____bpf_skb_load_bytes_relative(reuse_kern->skb, offset, to,
10976 len, start_header);
10979 static const struct bpf_func_proto sk_reuseport_load_bytes_relative_proto = {
10980 .func = sk_reuseport_load_bytes_relative,
10982 .ret_type = RET_INTEGER,
10983 .arg1_type = ARG_PTR_TO_CTX,
10984 .arg2_type = ARG_ANYTHING,
10985 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
10986 .arg4_type = ARG_CONST_SIZE,
10987 .arg5_type = ARG_ANYTHING,
10990 static const struct bpf_func_proto *
10991 sk_reuseport_func_proto(enum bpf_func_id func_id,
10992 const struct bpf_prog *prog)
10995 case BPF_FUNC_sk_select_reuseport:
10996 return &sk_select_reuseport_proto;
10997 case BPF_FUNC_skb_load_bytes:
10998 return &sk_reuseport_load_bytes_proto;
10999 case BPF_FUNC_skb_load_bytes_relative:
11000 return &sk_reuseport_load_bytes_relative_proto;
11001 case BPF_FUNC_get_socket_cookie:
11002 return &bpf_get_socket_ptr_cookie_proto;
11003 case BPF_FUNC_ktime_get_coarse_ns:
11004 return &bpf_ktime_get_coarse_ns_proto;
11006 return bpf_base_func_proto(func_id);
11011 sk_reuseport_is_valid_access(int off, int size,
11012 enum bpf_access_type type,
11013 const struct bpf_prog *prog,
11014 struct bpf_insn_access_aux *info)
11016 const u32 size_default = sizeof(__u32);
11018 if (off < 0 || off >= sizeof(struct sk_reuseport_md) ||
11019 off % size || type != BPF_READ)
11023 case offsetof(struct sk_reuseport_md, data):
11024 info->reg_type = PTR_TO_PACKET;
11025 return size == sizeof(__u64);
11027 case offsetof(struct sk_reuseport_md, data_end):
11028 info->reg_type = PTR_TO_PACKET_END;
11029 return size == sizeof(__u64);
11031 case offsetof(struct sk_reuseport_md, hash):
11032 return size == size_default;
11034 case offsetof(struct sk_reuseport_md, sk):
11035 info->reg_type = PTR_TO_SOCKET;
11036 return size == sizeof(__u64);
11038 case offsetof(struct sk_reuseport_md, migrating_sk):
11039 info->reg_type = PTR_TO_SOCK_COMMON_OR_NULL;
11040 return size == sizeof(__u64);
11042 /* Fields that allow narrowing */
11043 case bpf_ctx_range(struct sk_reuseport_md, eth_protocol):
11044 if (size < sizeof_field(struct sk_buff, protocol))
11047 case bpf_ctx_range(struct sk_reuseport_md, ip_protocol):
11048 case bpf_ctx_range(struct sk_reuseport_md, bind_inany):
11049 case bpf_ctx_range(struct sk_reuseport_md, len):
11050 bpf_ctx_record_field_size(info, size_default);
11051 return bpf_ctx_narrow_access_ok(off, size, size_default);
11058 #define SK_REUSEPORT_LOAD_FIELD(F) ({ \
11059 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_reuseport_kern, F), \
11060 si->dst_reg, si->src_reg, \
11061 bpf_target_off(struct sk_reuseport_kern, F, \
11062 sizeof_field(struct sk_reuseport_kern, F), \
11066 #define SK_REUSEPORT_LOAD_SKB_FIELD(SKB_FIELD) \
11067 SOCK_ADDR_LOAD_NESTED_FIELD(struct sk_reuseport_kern, \
11072 #define SK_REUSEPORT_LOAD_SK_FIELD(SK_FIELD) \
11073 SOCK_ADDR_LOAD_NESTED_FIELD(struct sk_reuseport_kern, \
11078 static u32 sk_reuseport_convert_ctx_access(enum bpf_access_type type,
11079 const struct bpf_insn *si,
11080 struct bpf_insn *insn_buf,
11081 struct bpf_prog *prog,
11084 struct bpf_insn *insn = insn_buf;
11087 case offsetof(struct sk_reuseport_md, data):
11088 SK_REUSEPORT_LOAD_SKB_FIELD(data);
11091 case offsetof(struct sk_reuseport_md, len):
11092 SK_REUSEPORT_LOAD_SKB_FIELD(len);
11095 case offsetof(struct sk_reuseport_md, eth_protocol):
11096 SK_REUSEPORT_LOAD_SKB_FIELD(protocol);
11099 case offsetof(struct sk_reuseport_md, ip_protocol):
11100 SK_REUSEPORT_LOAD_SK_FIELD(sk_protocol);
11103 case offsetof(struct sk_reuseport_md, data_end):
11104 SK_REUSEPORT_LOAD_FIELD(data_end);
11107 case offsetof(struct sk_reuseport_md, hash):
11108 SK_REUSEPORT_LOAD_FIELD(hash);
11111 case offsetof(struct sk_reuseport_md, bind_inany):
11112 SK_REUSEPORT_LOAD_FIELD(bind_inany);
11115 case offsetof(struct sk_reuseport_md, sk):
11116 SK_REUSEPORT_LOAD_FIELD(sk);
11119 case offsetof(struct sk_reuseport_md, migrating_sk):
11120 SK_REUSEPORT_LOAD_FIELD(migrating_sk);
11124 return insn - insn_buf;
11127 const struct bpf_verifier_ops sk_reuseport_verifier_ops = {
11128 .get_func_proto = sk_reuseport_func_proto,
11129 .is_valid_access = sk_reuseport_is_valid_access,
11130 .convert_ctx_access = sk_reuseport_convert_ctx_access,
11133 const struct bpf_prog_ops sk_reuseport_prog_ops = {
11136 DEFINE_STATIC_KEY_FALSE(bpf_sk_lookup_enabled);
11137 EXPORT_SYMBOL(bpf_sk_lookup_enabled);
11139 BPF_CALL_3(bpf_sk_lookup_assign, struct bpf_sk_lookup_kern *, ctx,
11140 struct sock *, sk, u64, flags)
11142 if (unlikely(flags & ~(BPF_SK_LOOKUP_F_REPLACE |
11143 BPF_SK_LOOKUP_F_NO_REUSEPORT)))
11145 if (unlikely(sk && sk_is_refcounted(sk)))
11146 return -ESOCKTNOSUPPORT; /* reject non-RCU freed sockets */
11147 if (unlikely(sk && sk_is_tcp(sk) && sk->sk_state != TCP_LISTEN))
11148 return -ESOCKTNOSUPPORT; /* only accept TCP socket in LISTEN */
11149 if (unlikely(sk && sk_is_udp(sk) && sk->sk_state != TCP_CLOSE))
11150 return -ESOCKTNOSUPPORT; /* only accept UDP socket in CLOSE */
11152 /* Check if socket is suitable for packet L3/L4 protocol */
11153 if (sk && sk->sk_protocol != ctx->protocol)
11154 return -EPROTOTYPE;
11155 if (sk && sk->sk_family != ctx->family &&
11156 (sk->sk_family == AF_INET || ipv6_only_sock(sk)))
11157 return -EAFNOSUPPORT;
11159 if (ctx->selected_sk && !(flags & BPF_SK_LOOKUP_F_REPLACE))
11162 /* Select socket as lookup result */
11163 ctx->selected_sk = sk;
11164 ctx->no_reuseport = flags & BPF_SK_LOOKUP_F_NO_REUSEPORT;
11168 static const struct bpf_func_proto bpf_sk_lookup_assign_proto = {
11169 .func = bpf_sk_lookup_assign,
11171 .ret_type = RET_INTEGER,
11172 .arg1_type = ARG_PTR_TO_CTX,
11173 .arg2_type = ARG_PTR_TO_SOCKET_OR_NULL,
11174 .arg3_type = ARG_ANYTHING,
11177 static const struct bpf_func_proto *
11178 sk_lookup_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
11181 case BPF_FUNC_perf_event_output:
11182 return &bpf_event_output_data_proto;
11183 case BPF_FUNC_sk_assign:
11184 return &bpf_sk_lookup_assign_proto;
11185 case BPF_FUNC_sk_release:
11186 return &bpf_sk_release_proto;
11188 return bpf_sk_base_func_proto(func_id);
11192 static bool sk_lookup_is_valid_access(int off, int size,
11193 enum bpf_access_type type,
11194 const struct bpf_prog *prog,
11195 struct bpf_insn_access_aux *info)
11197 if (off < 0 || off >= sizeof(struct bpf_sk_lookup))
11199 if (off % size != 0)
11201 if (type != BPF_READ)
11205 case offsetof(struct bpf_sk_lookup, sk):
11206 info->reg_type = PTR_TO_SOCKET_OR_NULL;
11207 return size == sizeof(__u64);
11209 case bpf_ctx_range(struct bpf_sk_lookup, family):
11210 case bpf_ctx_range(struct bpf_sk_lookup, protocol):
11211 case bpf_ctx_range(struct bpf_sk_lookup, remote_ip4):
11212 case bpf_ctx_range(struct bpf_sk_lookup, local_ip4):
11213 case bpf_ctx_range_till(struct bpf_sk_lookup, remote_ip6[0], remote_ip6[3]):
11214 case bpf_ctx_range_till(struct bpf_sk_lookup, local_ip6[0], local_ip6[3]):
11215 case bpf_ctx_range(struct bpf_sk_lookup, local_port):
11216 case bpf_ctx_range(struct bpf_sk_lookup, ingress_ifindex):
11217 bpf_ctx_record_field_size(info, sizeof(__u32));
11218 return bpf_ctx_narrow_access_ok(off, size, sizeof(__u32));
11220 case bpf_ctx_range(struct bpf_sk_lookup, remote_port):
11221 /* Allow 4-byte access to 2-byte field for backward compatibility */
11222 if (size == sizeof(__u32))
11224 bpf_ctx_record_field_size(info, sizeof(__be16));
11225 return bpf_ctx_narrow_access_ok(off, size, sizeof(__be16));
11227 case offsetofend(struct bpf_sk_lookup, remote_port) ...
11228 offsetof(struct bpf_sk_lookup, local_ip4) - 1:
11229 /* Allow access to zero padding for backward compatibility */
11230 bpf_ctx_record_field_size(info, sizeof(__u16));
11231 return bpf_ctx_narrow_access_ok(off, size, sizeof(__u16));
11238 static u32 sk_lookup_convert_ctx_access(enum bpf_access_type type,
11239 const struct bpf_insn *si,
11240 struct bpf_insn *insn_buf,
11241 struct bpf_prog *prog,
11244 struct bpf_insn *insn = insn_buf;
11247 case offsetof(struct bpf_sk_lookup, sk):
11248 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg, si->src_reg,
11249 offsetof(struct bpf_sk_lookup_kern, selected_sk));
11252 case offsetof(struct bpf_sk_lookup, family):
11253 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
11254 bpf_target_off(struct bpf_sk_lookup_kern,
11255 family, 2, target_size));
11258 case offsetof(struct bpf_sk_lookup, protocol):
11259 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
11260 bpf_target_off(struct bpf_sk_lookup_kern,
11261 protocol, 2, target_size));
11264 case offsetof(struct bpf_sk_lookup, remote_ip4):
11265 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
11266 bpf_target_off(struct bpf_sk_lookup_kern,
11267 v4.saddr, 4, target_size));
11270 case offsetof(struct bpf_sk_lookup, local_ip4):
11271 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
11272 bpf_target_off(struct bpf_sk_lookup_kern,
11273 v4.daddr, 4, target_size));
11276 case bpf_ctx_range_till(struct bpf_sk_lookup,
11277 remote_ip6[0], remote_ip6[3]): {
11278 #if IS_ENABLED(CONFIG_IPV6)
11281 off -= offsetof(struct bpf_sk_lookup, remote_ip6[0]);
11282 off += bpf_target_off(struct in6_addr, s6_addr32[0], 4, target_size);
11283 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg, si->src_reg,
11284 offsetof(struct bpf_sk_lookup_kern, v6.saddr));
11285 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
11286 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, off);
11288 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
11292 case bpf_ctx_range_till(struct bpf_sk_lookup,
11293 local_ip6[0], local_ip6[3]): {
11294 #if IS_ENABLED(CONFIG_IPV6)
11297 off -= offsetof(struct bpf_sk_lookup, local_ip6[0]);
11298 off += bpf_target_off(struct in6_addr, s6_addr32[0], 4, target_size);
11299 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg, si->src_reg,
11300 offsetof(struct bpf_sk_lookup_kern, v6.daddr));
11301 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
11302 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, off);
11304 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
11308 case offsetof(struct bpf_sk_lookup, remote_port):
11309 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
11310 bpf_target_off(struct bpf_sk_lookup_kern,
11311 sport, 2, target_size));
11314 case offsetofend(struct bpf_sk_lookup, remote_port):
11316 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
11319 case offsetof(struct bpf_sk_lookup, local_port):
11320 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
11321 bpf_target_off(struct bpf_sk_lookup_kern,
11322 dport, 2, target_size));
11325 case offsetof(struct bpf_sk_lookup, ingress_ifindex):
11326 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
11327 bpf_target_off(struct bpf_sk_lookup_kern,
11328 ingress_ifindex, 4, target_size));
11332 return insn - insn_buf;
11335 const struct bpf_prog_ops sk_lookup_prog_ops = {
11336 .test_run = bpf_prog_test_run_sk_lookup,
11339 const struct bpf_verifier_ops sk_lookup_verifier_ops = {
11340 .get_func_proto = sk_lookup_func_proto,
11341 .is_valid_access = sk_lookup_is_valid_access,
11342 .convert_ctx_access = sk_lookup_convert_ctx_access,
11345 #endif /* CONFIG_INET */
11347 DEFINE_BPF_DISPATCHER(xdp)
11349 void bpf_prog_change_xdp(struct bpf_prog *prev_prog, struct bpf_prog *prog)
11351 bpf_dispatcher_change_prog(BPF_DISPATCHER_PTR(xdp), prev_prog, prog);
11354 BTF_ID_LIST_GLOBAL(btf_sock_ids, MAX_BTF_SOCK_TYPE)
11355 #define BTF_SOCK_TYPE(name, type) BTF_ID(struct, type)
11357 #undef BTF_SOCK_TYPE
11359 BPF_CALL_1(bpf_skc_to_tcp6_sock, struct sock *, sk)
11361 /* tcp6_sock type is not generated in dwarf and hence btf,
11362 * trigger an explicit type generation here.
11364 BTF_TYPE_EMIT(struct tcp6_sock);
11365 if (sk && sk_fullsock(sk) && sk->sk_protocol == IPPROTO_TCP &&
11366 sk->sk_family == AF_INET6)
11367 return (unsigned long)sk;
11369 return (unsigned long)NULL;
11372 const struct bpf_func_proto bpf_skc_to_tcp6_sock_proto = {
11373 .func = bpf_skc_to_tcp6_sock,
11375 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
11376 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
11377 .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_TCP6],
11380 BPF_CALL_1(bpf_skc_to_tcp_sock, struct sock *, sk)
11382 if (sk && sk_fullsock(sk) && sk->sk_protocol == IPPROTO_TCP)
11383 return (unsigned long)sk;
11385 return (unsigned long)NULL;
11388 const struct bpf_func_proto bpf_skc_to_tcp_sock_proto = {
11389 .func = bpf_skc_to_tcp_sock,
11391 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
11392 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
11393 .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_TCP],
11396 BPF_CALL_1(bpf_skc_to_tcp_timewait_sock, struct sock *, sk)
11398 /* BTF types for tcp_timewait_sock and inet_timewait_sock are not
11399 * generated if CONFIG_INET=n. Trigger an explicit generation here.
11401 BTF_TYPE_EMIT(struct inet_timewait_sock);
11402 BTF_TYPE_EMIT(struct tcp_timewait_sock);
11405 if (sk && sk->sk_prot == &tcp_prot && sk->sk_state == TCP_TIME_WAIT)
11406 return (unsigned long)sk;
11409 #if IS_BUILTIN(CONFIG_IPV6)
11410 if (sk && sk->sk_prot == &tcpv6_prot && sk->sk_state == TCP_TIME_WAIT)
11411 return (unsigned long)sk;
11414 return (unsigned long)NULL;
11417 const struct bpf_func_proto bpf_skc_to_tcp_timewait_sock_proto = {
11418 .func = bpf_skc_to_tcp_timewait_sock,
11420 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
11421 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
11422 .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_TCP_TW],
11425 BPF_CALL_1(bpf_skc_to_tcp_request_sock, struct sock *, sk)
11428 if (sk && sk->sk_prot == &tcp_prot && sk->sk_state == TCP_NEW_SYN_RECV)
11429 return (unsigned long)sk;
11432 #if IS_BUILTIN(CONFIG_IPV6)
11433 if (sk && sk->sk_prot == &tcpv6_prot && sk->sk_state == TCP_NEW_SYN_RECV)
11434 return (unsigned long)sk;
11437 return (unsigned long)NULL;
11440 const struct bpf_func_proto bpf_skc_to_tcp_request_sock_proto = {
11441 .func = bpf_skc_to_tcp_request_sock,
11443 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
11444 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
11445 .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_TCP_REQ],
11448 BPF_CALL_1(bpf_skc_to_udp6_sock, struct sock *, sk)
11450 /* udp6_sock type is not generated in dwarf and hence btf,
11451 * trigger an explicit type generation here.
11453 BTF_TYPE_EMIT(struct udp6_sock);
11454 if (sk && sk_fullsock(sk) && sk->sk_protocol == IPPROTO_UDP &&
11455 sk->sk_type == SOCK_DGRAM && sk->sk_family == AF_INET6)
11456 return (unsigned long)sk;
11458 return (unsigned long)NULL;
11461 const struct bpf_func_proto bpf_skc_to_udp6_sock_proto = {
11462 .func = bpf_skc_to_udp6_sock,
11464 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
11465 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
11466 .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_UDP6],
11469 BPF_CALL_1(bpf_skc_to_unix_sock, struct sock *, sk)
11471 /* unix_sock type is not generated in dwarf and hence btf,
11472 * trigger an explicit type generation here.
11474 BTF_TYPE_EMIT(struct unix_sock);
11475 if (sk && sk_fullsock(sk) && sk->sk_family == AF_UNIX)
11476 return (unsigned long)sk;
11478 return (unsigned long)NULL;
11481 const struct bpf_func_proto bpf_skc_to_unix_sock_proto = {
11482 .func = bpf_skc_to_unix_sock,
11484 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
11485 .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
11486 .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_UNIX],
11489 BPF_CALL_1(bpf_skc_to_mptcp_sock, struct sock *, sk)
11491 BTF_TYPE_EMIT(struct mptcp_sock);
11492 return (unsigned long)bpf_mptcp_sock_from_subflow(sk);
11495 const struct bpf_func_proto bpf_skc_to_mptcp_sock_proto = {
11496 .func = bpf_skc_to_mptcp_sock,
11498 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
11499 .arg1_type = ARG_PTR_TO_SOCK_COMMON,
11500 .ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_MPTCP],
11503 BPF_CALL_1(bpf_sock_from_file, struct file *, file)
11505 return (unsigned long)sock_from_file(file);
11508 BTF_ID_LIST(bpf_sock_from_file_btf_ids)
11509 BTF_ID(struct, socket)
11510 BTF_ID(struct, file)
11512 const struct bpf_func_proto bpf_sock_from_file_proto = {
11513 .func = bpf_sock_from_file,
11515 .ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
11516 .ret_btf_id = &bpf_sock_from_file_btf_ids[0],
11517 .arg1_type = ARG_PTR_TO_BTF_ID,
11518 .arg1_btf_id = &bpf_sock_from_file_btf_ids[1],
11521 static const struct bpf_func_proto *
11522 bpf_sk_base_func_proto(enum bpf_func_id func_id)
11524 const struct bpf_func_proto *func;
11527 case BPF_FUNC_skc_to_tcp6_sock:
11528 func = &bpf_skc_to_tcp6_sock_proto;
11530 case BPF_FUNC_skc_to_tcp_sock:
11531 func = &bpf_skc_to_tcp_sock_proto;
11533 case BPF_FUNC_skc_to_tcp_timewait_sock:
11534 func = &bpf_skc_to_tcp_timewait_sock_proto;
11536 case BPF_FUNC_skc_to_tcp_request_sock:
11537 func = &bpf_skc_to_tcp_request_sock_proto;
11539 case BPF_FUNC_skc_to_udp6_sock:
11540 func = &bpf_skc_to_udp6_sock_proto;
11542 case BPF_FUNC_skc_to_unix_sock:
11543 func = &bpf_skc_to_unix_sock_proto;
11545 case BPF_FUNC_skc_to_mptcp_sock:
11546 func = &bpf_skc_to_mptcp_sock_proto;
11548 case BPF_FUNC_ktime_get_coarse_ns:
11549 return &bpf_ktime_get_coarse_ns_proto;
11551 return bpf_base_func_proto(func_id);
11554 if (!perfmon_capable())