1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Linux Socket Filter - Kernel level socket filtering
5 * Based on the design of the Berkeley Packet Filter. The new
6 * internal format has been designed by PLUMgrid:
8 * Copyright (c) 2011 - 2014 PLUMgrid, http://plumgrid.com
12 * Jay Schulist <jschlst@samba.org>
13 * Alexei Starovoitov <ast@plumgrid.com>
14 * Daniel Borkmann <dborkman@redhat.com>
16 * Andi Kleen - Fix a few bad bugs and races.
17 * Kris Katterjohn - Added many additional checks in bpf_check_classic()
20 #include <linux/module.h>
21 #include <linux/types.h>
23 #include <linux/fcntl.h>
24 #include <linux/socket.h>
25 #include <linux/sock_diag.h>
27 #include <linux/inet.h>
28 #include <linux/netdevice.h>
29 #include <linux/if_packet.h>
30 #include <linux/if_arp.h>
31 #include <linux/gfp.h>
32 #include <net/inet_common.h>
34 #include <net/protocol.h>
35 #include <net/netlink.h>
36 #include <linux/skbuff.h>
37 #include <linux/skmsg.h>
39 #include <net/flow_dissector.h>
40 #include <linux/errno.h>
41 #include <linux/timer.h>
42 #include <linux/uaccess.h>
43 #include <asm/unaligned.h>
44 #include <asm/cmpxchg.h>
45 #include <linux/filter.h>
46 #include <linux/ratelimit.h>
47 #include <linux/seccomp.h>
48 #include <linux/if_vlan.h>
49 #include <linux/bpf.h>
50 #include <net/sch_generic.h>
51 #include <net/cls_cgroup.h>
52 #include <net/dst_metadata.h>
54 #include <net/sock_reuseport.h>
55 #include <net/busy_poll.h>
59 #include <linux/bpf_trace.h>
60 #include <net/xdp_sock.h>
61 #include <linux/inetdevice.h>
62 #include <net/inet_hashtables.h>
63 #include <net/inet6_hashtables.h>
64 #include <net/ip_fib.h>
65 #include <net/nexthop.h>
69 #include <net/net_namespace.h>
70 #include <linux/seg6_local.h>
72 #include <net/seg6_local.h>
73 #include <net/lwtunnel.h>
74 #include <net/ipv6_stubs.h>
75 #include <net/bpf_sk_storage.h>
78 * sk_filter_trim_cap - run a packet through a socket filter
79 * @sk: sock associated with &sk_buff
80 * @skb: buffer to filter
81 * @cap: limit on how short the eBPF program may trim the packet
83 * Run the eBPF program and then cut skb->data to correct size returned by
84 * the program. If pkt_len is 0 we toss packet. If skb->len is smaller
85 * than pkt_len we keep whole skb->data. This is the socket level
86 * wrapper to BPF_PROG_RUN. It returns 0 if the packet should
87 * be accepted or -EPERM if the packet should be tossed.
90 int sk_filter_trim_cap(struct sock *sk, struct sk_buff *skb, unsigned int cap)
93 struct sk_filter *filter;
96 * If the skb was allocated from pfmemalloc reserves, only
97 * allow SOCK_MEMALLOC sockets to use it as this socket is
100 if (skb_pfmemalloc(skb) && !sock_flag(sk, SOCK_MEMALLOC)) {
101 NET_INC_STATS(sock_net(sk), LINUX_MIB_PFMEMALLOCDROP);
104 err = BPF_CGROUP_RUN_PROG_INET_INGRESS(sk, skb);
108 err = security_sock_rcv_skb(sk, skb);
113 filter = rcu_dereference(sk->sk_filter);
115 struct sock *save_sk = skb->sk;
116 unsigned int pkt_len;
119 pkt_len = bpf_prog_run_save_cb(filter->prog, skb);
121 err = pkt_len ? pskb_trim(skb, max(cap, pkt_len)) : -EPERM;
127 EXPORT_SYMBOL(sk_filter_trim_cap);
129 BPF_CALL_1(bpf_skb_get_pay_offset, struct sk_buff *, skb)
131 return skb_get_poff(skb);
134 BPF_CALL_3(bpf_skb_get_nlattr, struct sk_buff *, skb, u32, a, u32, x)
138 if (skb_is_nonlinear(skb))
141 if (skb->len < sizeof(struct nlattr))
144 if (a > skb->len - sizeof(struct nlattr))
147 nla = nla_find((struct nlattr *) &skb->data[a], skb->len - a, x);
149 return (void *) nla - (void *) skb->data;
154 BPF_CALL_3(bpf_skb_get_nlattr_nest, struct sk_buff *, skb, u32, a, u32, x)
158 if (skb_is_nonlinear(skb))
161 if (skb->len < sizeof(struct nlattr))
164 if (a > skb->len - sizeof(struct nlattr))
167 nla = (struct nlattr *) &skb->data[a];
168 if (nla->nla_len > skb->len - a)
171 nla = nla_find_nested(nla, x);
173 return (void *) nla - (void *) skb->data;
178 BPF_CALL_4(bpf_skb_load_helper_8, const struct sk_buff *, skb, const void *,
179 data, int, headlen, int, offset)
182 const int len = sizeof(tmp);
185 if (headlen - offset >= len)
186 return *(u8 *)(data + offset);
187 if (!skb_copy_bits(skb, offset, &tmp, sizeof(tmp)))
190 ptr = bpf_internal_load_pointer_neg_helper(skb, offset, len);
198 BPF_CALL_2(bpf_skb_load_helper_8_no_cache, const struct sk_buff *, skb,
201 return ____bpf_skb_load_helper_8(skb, skb->data, skb->len - skb->data_len,
205 BPF_CALL_4(bpf_skb_load_helper_16, const struct sk_buff *, skb, const void *,
206 data, int, headlen, int, offset)
209 const int len = sizeof(tmp);
212 if (headlen - offset >= len)
213 return get_unaligned_be16(data + offset);
214 if (!skb_copy_bits(skb, offset, &tmp, sizeof(tmp)))
215 return be16_to_cpu(tmp);
217 ptr = bpf_internal_load_pointer_neg_helper(skb, offset, len);
219 return get_unaligned_be16(ptr);
225 BPF_CALL_2(bpf_skb_load_helper_16_no_cache, const struct sk_buff *, skb,
228 return ____bpf_skb_load_helper_16(skb, skb->data, skb->len - skb->data_len,
232 BPF_CALL_4(bpf_skb_load_helper_32, const struct sk_buff *, skb, const void *,
233 data, int, headlen, int, offset)
236 const int len = sizeof(tmp);
238 if (likely(offset >= 0)) {
239 if (headlen - offset >= len)
240 return get_unaligned_be32(data + offset);
241 if (!skb_copy_bits(skb, offset, &tmp, sizeof(tmp)))
242 return be32_to_cpu(tmp);
244 ptr = bpf_internal_load_pointer_neg_helper(skb, offset, len);
246 return get_unaligned_be32(ptr);
252 BPF_CALL_2(bpf_skb_load_helper_32_no_cache, const struct sk_buff *, skb,
255 return ____bpf_skb_load_helper_32(skb, skb->data, skb->len - skb->data_len,
259 BPF_CALL_0(bpf_get_raw_cpu_id)
261 return raw_smp_processor_id();
264 static const struct bpf_func_proto bpf_get_raw_smp_processor_id_proto = {
265 .func = bpf_get_raw_cpu_id,
267 .ret_type = RET_INTEGER,
270 static u32 convert_skb_access(int skb_field, int dst_reg, int src_reg,
271 struct bpf_insn *insn_buf)
273 struct bpf_insn *insn = insn_buf;
277 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
279 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
280 offsetof(struct sk_buff, mark));
284 *insn++ = BPF_LDX_MEM(BPF_B, dst_reg, src_reg, PKT_TYPE_OFFSET());
285 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, PKT_TYPE_MAX);
286 #ifdef __BIG_ENDIAN_BITFIELD
287 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 5);
292 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, queue_mapping) != 2);
294 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
295 offsetof(struct sk_buff, queue_mapping));
298 case SKF_AD_VLAN_TAG:
299 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_tci) != 2);
301 /* dst_reg = *(u16 *) (src_reg + offsetof(vlan_tci)) */
302 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
303 offsetof(struct sk_buff, vlan_tci));
305 case SKF_AD_VLAN_TAG_PRESENT:
306 *insn++ = BPF_LDX_MEM(BPF_B, dst_reg, src_reg, PKT_VLAN_PRESENT_OFFSET());
307 if (PKT_VLAN_PRESENT_BIT)
308 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, PKT_VLAN_PRESENT_BIT);
309 if (PKT_VLAN_PRESENT_BIT < 7)
310 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, 1);
314 return insn - insn_buf;
317 static bool convert_bpf_extensions(struct sock_filter *fp,
318 struct bpf_insn **insnp)
320 struct bpf_insn *insn = *insnp;
324 case SKF_AD_OFF + SKF_AD_PROTOCOL:
325 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2);
327 /* A = *(u16 *) (CTX + offsetof(protocol)) */
328 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
329 offsetof(struct sk_buff, protocol));
330 /* A = ntohs(A) [emitting a nop or swap16] */
331 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
334 case SKF_AD_OFF + SKF_AD_PKTTYPE:
335 cnt = convert_skb_access(SKF_AD_PKTTYPE, BPF_REG_A, BPF_REG_CTX, insn);
339 case SKF_AD_OFF + SKF_AD_IFINDEX:
340 case SKF_AD_OFF + SKF_AD_HATYPE:
341 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4);
342 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, type) != 2);
344 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
345 BPF_REG_TMP, BPF_REG_CTX,
346 offsetof(struct sk_buff, dev));
347 /* if (tmp != 0) goto pc + 1 */
348 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_TMP, 0, 1);
349 *insn++ = BPF_EXIT_INSN();
350 if (fp->k == SKF_AD_OFF + SKF_AD_IFINDEX)
351 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_TMP,
352 offsetof(struct net_device, ifindex));
354 *insn = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_TMP,
355 offsetof(struct net_device, type));
358 case SKF_AD_OFF + SKF_AD_MARK:
359 cnt = convert_skb_access(SKF_AD_MARK, BPF_REG_A, BPF_REG_CTX, insn);
363 case SKF_AD_OFF + SKF_AD_RXHASH:
364 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4);
366 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX,
367 offsetof(struct sk_buff, hash));
370 case SKF_AD_OFF + SKF_AD_QUEUE:
371 cnt = convert_skb_access(SKF_AD_QUEUE, BPF_REG_A, BPF_REG_CTX, insn);
375 case SKF_AD_OFF + SKF_AD_VLAN_TAG:
376 cnt = convert_skb_access(SKF_AD_VLAN_TAG,
377 BPF_REG_A, BPF_REG_CTX, insn);
381 case SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT:
382 cnt = convert_skb_access(SKF_AD_VLAN_TAG_PRESENT,
383 BPF_REG_A, BPF_REG_CTX, insn);
387 case SKF_AD_OFF + SKF_AD_VLAN_TPID:
388 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_proto) != 2);
390 /* A = *(u16 *) (CTX + offsetof(vlan_proto)) */
391 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
392 offsetof(struct sk_buff, vlan_proto));
393 /* A = ntohs(A) [emitting a nop or swap16] */
394 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
397 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
398 case SKF_AD_OFF + SKF_AD_NLATTR:
399 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
400 case SKF_AD_OFF + SKF_AD_CPU:
401 case SKF_AD_OFF + SKF_AD_RANDOM:
403 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX);
405 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_A);
407 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_X);
408 /* Emit call(arg1=CTX, arg2=A, arg3=X) */
410 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
411 *insn = BPF_EMIT_CALL(bpf_skb_get_pay_offset);
413 case SKF_AD_OFF + SKF_AD_NLATTR:
414 *insn = BPF_EMIT_CALL(bpf_skb_get_nlattr);
416 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
417 *insn = BPF_EMIT_CALL(bpf_skb_get_nlattr_nest);
419 case SKF_AD_OFF + SKF_AD_CPU:
420 *insn = BPF_EMIT_CALL(bpf_get_raw_cpu_id);
422 case SKF_AD_OFF + SKF_AD_RANDOM:
423 *insn = BPF_EMIT_CALL(bpf_user_rnd_u32);
424 bpf_user_rnd_init_once();
429 case SKF_AD_OFF + SKF_AD_ALU_XOR_X:
431 *insn = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_X);
435 /* This is just a dummy call to avoid letting the compiler
436 * evict __bpf_call_base() as an optimization. Placed here
437 * where no-one bothers.
439 BUG_ON(__bpf_call_base(0, 0, 0, 0, 0) != 0);
447 static bool convert_bpf_ld_abs(struct sock_filter *fp, struct bpf_insn **insnp)
449 const bool unaligned_ok = IS_BUILTIN(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS);
450 int size = bpf_size_to_bytes(BPF_SIZE(fp->code));
451 bool endian = BPF_SIZE(fp->code) == BPF_H ||
452 BPF_SIZE(fp->code) == BPF_W;
453 bool indirect = BPF_MODE(fp->code) == BPF_IND;
454 const int ip_align = NET_IP_ALIGN;
455 struct bpf_insn *insn = *insnp;
459 ((unaligned_ok && offset >= 0) ||
460 (!unaligned_ok && offset >= 0 &&
461 offset + ip_align >= 0 &&
462 offset + ip_align % size == 0))) {
463 bool ldx_off_ok = offset <= S16_MAX;
465 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_H);
467 *insn++ = BPF_ALU64_IMM(BPF_SUB, BPF_REG_TMP, offset);
468 *insn++ = BPF_JMP_IMM(BPF_JSLT, BPF_REG_TMP,
469 size, 2 + endian + (!ldx_off_ok * 2));
471 *insn++ = BPF_LDX_MEM(BPF_SIZE(fp->code), BPF_REG_A,
474 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_D);
475 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_TMP, offset);
476 *insn++ = BPF_LDX_MEM(BPF_SIZE(fp->code), BPF_REG_A,
480 *insn++ = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, size * 8);
481 *insn++ = BPF_JMP_A(8);
484 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX);
485 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_D);
486 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_H);
488 *insn++ = BPF_MOV64_IMM(BPF_REG_ARG4, offset);
490 *insn++ = BPF_MOV64_REG(BPF_REG_ARG4, BPF_REG_X);
492 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_ARG4, offset);
495 switch (BPF_SIZE(fp->code)) {
497 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_8);
500 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_16);
503 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_32);
509 *insn++ = BPF_JMP_IMM(BPF_JSGE, BPF_REG_A, 0, 2);
510 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
511 *insn = BPF_EXIT_INSN();
518 * bpf_convert_filter - convert filter program
519 * @prog: the user passed filter program
520 * @len: the length of the user passed filter program
521 * @new_prog: allocated 'struct bpf_prog' or NULL
522 * @new_len: pointer to store length of converted program
523 * @seen_ld_abs: bool whether we've seen ld_abs/ind
525 * Remap 'sock_filter' style classic BPF (cBPF) instruction set to 'bpf_insn'
526 * style extended BPF (eBPF).
527 * Conversion workflow:
529 * 1) First pass for calculating the new program length:
530 * bpf_convert_filter(old_prog, old_len, NULL, &new_len, &seen_ld_abs)
532 * 2) 2nd pass to remap in two passes: 1st pass finds new
533 * jump offsets, 2nd pass remapping:
534 * bpf_convert_filter(old_prog, old_len, new_prog, &new_len, &seen_ld_abs)
536 static int bpf_convert_filter(struct sock_filter *prog, int len,
537 struct bpf_prog *new_prog, int *new_len,
540 int new_flen = 0, pass = 0, target, i, stack_off;
541 struct bpf_insn *new_insn, *first_insn = NULL;
542 struct sock_filter *fp;
546 BUILD_BUG_ON(BPF_MEMWORDS * sizeof(u32) > MAX_BPF_STACK);
547 BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG);
549 if (len <= 0 || len > BPF_MAXINSNS)
553 first_insn = new_prog->insnsi;
554 addrs = kcalloc(len, sizeof(*addrs),
555 GFP_KERNEL | __GFP_NOWARN);
561 new_insn = first_insn;
564 /* Classic BPF related prologue emission. */
566 /* Classic BPF expects A and X to be reset first. These need
567 * to be guaranteed to be the first two instructions.
569 *new_insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
570 *new_insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_X, BPF_REG_X);
572 /* All programs must keep CTX in callee saved BPF_REG_CTX.
573 * In eBPF case it's done by the compiler, here we need to
574 * do this ourself. Initial CTX is present in BPF_REG_ARG1.
576 *new_insn++ = BPF_MOV64_REG(BPF_REG_CTX, BPF_REG_ARG1);
578 /* For packet access in classic BPF, cache skb->data
579 * in callee-saved BPF R8 and skb->len - skb->data_len
580 * (headlen) in BPF R9. Since classic BPF is read-only
581 * on CTX, we only need to cache it once.
583 *new_insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
584 BPF_REG_D, BPF_REG_CTX,
585 offsetof(struct sk_buff, data));
586 *new_insn++ = BPF_LDX_MEM(BPF_W, BPF_REG_H, BPF_REG_CTX,
587 offsetof(struct sk_buff, len));
588 *new_insn++ = BPF_LDX_MEM(BPF_W, BPF_REG_TMP, BPF_REG_CTX,
589 offsetof(struct sk_buff, data_len));
590 *new_insn++ = BPF_ALU32_REG(BPF_SUB, BPF_REG_H, BPF_REG_TMP);
596 for (i = 0; i < len; fp++, i++) {
597 struct bpf_insn tmp_insns[32] = { };
598 struct bpf_insn *insn = tmp_insns;
601 addrs[i] = new_insn - first_insn;
604 /* All arithmetic insns and skb loads map as-is. */
605 case BPF_ALU | BPF_ADD | BPF_X:
606 case BPF_ALU | BPF_ADD | BPF_K:
607 case BPF_ALU | BPF_SUB | BPF_X:
608 case BPF_ALU | BPF_SUB | BPF_K:
609 case BPF_ALU | BPF_AND | BPF_X:
610 case BPF_ALU | BPF_AND | BPF_K:
611 case BPF_ALU | BPF_OR | BPF_X:
612 case BPF_ALU | BPF_OR | BPF_K:
613 case BPF_ALU | BPF_LSH | BPF_X:
614 case BPF_ALU | BPF_LSH | BPF_K:
615 case BPF_ALU | BPF_RSH | BPF_X:
616 case BPF_ALU | BPF_RSH | BPF_K:
617 case BPF_ALU | BPF_XOR | BPF_X:
618 case BPF_ALU | BPF_XOR | BPF_K:
619 case BPF_ALU | BPF_MUL | BPF_X:
620 case BPF_ALU | BPF_MUL | BPF_K:
621 case BPF_ALU | BPF_DIV | BPF_X:
622 case BPF_ALU | BPF_DIV | BPF_K:
623 case BPF_ALU | BPF_MOD | BPF_X:
624 case BPF_ALU | BPF_MOD | BPF_K:
625 case BPF_ALU | BPF_NEG:
626 case BPF_LD | BPF_ABS | BPF_W:
627 case BPF_LD | BPF_ABS | BPF_H:
628 case BPF_LD | BPF_ABS | BPF_B:
629 case BPF_LD | BPF_IND | BPF_W:
630 case BPF_LD | BPF_IND | BPF_H:
631 case BPF_LD | BPF_IND | BPF_B:
632 /* Check for overloaded BPF extension and
633 * directly convert it if found, otherwise
634 * just move on with mapping.
636 if (BPF_CLASS(fp->code) == BPF_LD &&
637 BPF_MODE(fp->code) == BPF_ABS &&
638 convert_bpf_extensions(fp, &insn))
640 if (BPF_CLASS(fp->code) == BPF_LD &&
641 convert_bpf_ld_abs(fp, &insn)) {
646 if (fp->code == (BPF_ALU | BPF_DIV | BPF_X) ||
647 fp->code == (BPF_ALU | BPF_MOD | BPF_X)) {
648 *insn++ = BPF_MOV32_REG(BPF_REG_X, BPF_REG_X);
649 /* Error with exception code on div/mod by 0.
650 * For cBPF programs, this was always return 0.
652 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_X, 0, 2);
653 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
654 *insn++ = BPF_EXIT_INSN();
657 *insn = BPF_RAW_INSN(fp->code, BPF_REG_A, BPF_REG_X, 0, fp->k);
660 /* Jump transformation cannot use BPF block macros
661 * everywhere as offset calculation and target updates
662 * require a bit more work than the rest, i.e. jump
663 * opcodes map as-is, but offsets need adjustment.
666 #define BPF_EMIT_JMP \
668 const s32 off_min = S16_MIN, off_max = S16_MAX; \
671 if (target >= len || target < 0) \
673 off = addrs ? addrs[target] - addrs[i] - 1 : 0; \
674 /* Adjust pc relative offset for 2nd or 3rd insn. */ \
675 off -= insn - tmp_insns; \
676 /* Reject anything not fitting into insn->off. */ \
677 if (off < off_min || off > off_max) \
682 case BPF_JMP | BPF_JA:
683 target = i + fp->k + 1;
684 insn->code = fp->code;
688 case BPF_JMP | BPF_JEQ | BPF_K:
689 case BPF_JMP | BPF_JEQ | BPF_X:
690 case BPF_JMP | BPF_JSET | BPF_K:
691 case BPF_JMP | BPF_JSET | BPF_X:
692 case BPF_JMP | BPF_JGT | BPF_K:
693 case BPF_JMP | BPF_JGT | BPF_X:
694 case BPF_JMP | BPF_JGE | BPF_K:
695 case BPF_JMP | BPF_JGE | BPF_X:
696 if (BPF_SRC(fp->code) == BPF_K && (int) fp->k < 0) {
697 /* BPF immediates are signed, zero extend
698 * immediate into tmp register and use it
701 *insn++ = BPF_MOV32_IMM(BPF_REG_TMP, fp->k);
703 insn->dst_reg = BPF_REG_A;
704 insn->src_reg = BPF_REG_TMP;
707 insn->dst_reg = BPF_REG_A;
709 bpf_src = BPF_SRC(fp->code);
710 insn->src_reg = bpf_src == BPF_X ? BPF_REG_X : 0;
713 /* Common case where 'jump_false' is next insn. */
715 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
716 target = i + fp->jt + 1;
721 /* Convert some jumps when 'jump_true' is next insn. */
723 switch (BPF_OP(fp->code)) {
725 insn->code = BPF_JMP | BPF_JNE | bpf_src;
728 insn->code = BPF_JMP | BPF_JLE | bpf_src;
731 insn->code = BPF_JMP | BPF_JLT | bpf_src;
737 target = i + fp->jf + 1;
742 /* Other jumps are mapped into two insns: Jxx and JA. */
743 target = i + fp->jt + 1;
744 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
748 insn->code = BPF_JMP | BPF_JA;
749 target = i + fp->jf + 1;
753 /* ldxb 4 * ([14] & 0xf) is remaped into 6 insns. */
754 case BPF_LDX | BPF_MSH | BPF_B: {
755 struct sock_filter tmp = {
756 .code = BPF_LD | BPF_ABS | BPF_B,
763 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
764 /* A = BPF_R0 = *(u8 *) (skb->data + K) */
765 convert_bpf_ld_abs(&tmp, &insn);
768 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, 0xf);
770 *insn++ = BPF_ALU32_IMM(BPF_LSH, BPF_REG_A, 2);
772 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_X);
774 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
776 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_TMP);
779 /* RET_K is remaped into 2 insns. RET_A case doesn't need an
780 * extra mov as BPF_REG_0 is already mapped into BPF_REG_A.
782 case BPF_RET | BPF_A:
783 case BPF_RET | BPF_K:
784 if (BPF_RVAL(fp->code) == BPF_K)
785 *insn++ = BPF_MOV32_RAW(BPF_K, BPF_REG_0,
787 *insn = BPF_EXIT_INSN();
790 /* Store to stack. */
793 stack_off = fp->k * 4 + 4;
794 *insn = BPF_STX_MEM(BPF_W, BPF_REG_FP, BPF_CLASS(fp->code) ==
795 BPF_ST ? BPF_REG_A : BPF_REG_X,
797 /* check_load_and_stores() verifies that classic BPF can
798 * load from stack only after write, so tracking
799 * stack_depth for ST|STX insns is enough
801 if (new_prog && new_prog->aux->stack_depth < stack_off)
802 new_prog->aux->stack_depth = stack_off;
805 /* Load from stack. */
806 case BPF_LD | BPF_MEM:
807 case BPF_LDX | BPF_MEM:
808 stack_off = fp->k * 4 + 4;
809 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
810 BPF_REG_A : BPF_REG_X, BPF_REG_FP,
815 case BPF_LD | BPF_IMM:
816 case BPF_LDX | BPF_IMM:
817 *insn = BPF_MOV32_IMM(BPF_CLASS(fp->code) == BPF_LD ?
818 BPF_REG_A : BPF_REG_X, fp->k);
822 case BPF_MISC | BPF_TAX:
823 *insn = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
827 case BPF_MISC | BPF_TXA:
828 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_X);
831 /* A = skb->len or X = skb->len */
832 case BPF_LD | BPF_W | BPF_LEN:
833 case BPF_LDX | BPF_W | BPF_LEN:
834 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
835 BPF_REG_A : BPF_REG_X, BPF_REG_CTX,
836 offsetof(struct sk_buff, len));
839 /* Access seccomp_data fields. */
840 case BPF_LDX | BPF_ABS | BPF_W:
841 /* A = *(u32 *) (ctx + K) */
842 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX, fp->k);
845 /* Unknown instruction. */
852 memcpy(new_insn, tmp_insns,
853 sizeof(*insn) * (insn - tmp_insns));
854 new_insn += insn - tmp_insns;
858 /* Only calculating new length. */
859 *new_len = new_insn - first_insn;
861 *new_len += 4; /* Prologue bits. */
866 if (new_flen != new_insn - first_insn) {
867 new_flen = new_insn - first_insn;
874 BUG_ON(*new_len != new_flen);
883 * As we dont want to clear mem[] array for each packet going through
884 * __bpf_prog_run(), we check that filter loaded by user never try to read
885 * a cell if not previously written, and we check all branches to be sure
886 * a malicious user doesn't try to abuse us.
888 static int check_load_and_stores(const struct sock_filter *filter, int flen)
890 u16 *masks, memvalid = 0; /* One bit per cell, 16 cells */
893 BUILD_BUG_ON(BPF_MEMWORDS > 16);
895 masks = kmalloc_array(flen, sizeof(*masks), GFP_KERNEL);
899 memset(masks, 0xff, flen * sizeof(*masks));
901 for (pc = 0; pc < flen; pc++) {
902 memvalid &= masks[pc];
904 switch (filter[pc].code) {
907 memvalid |= (1 << filter[pc].k);
909 case BPF_LD | BPF_MEM:
910 case BPF_LDX | BPF_MEM:
911 if (!(memvalid & (1 << filter[pc].k))) {
916 case BPF_JMP | BPF_JA:
917 /* A jump must set masks on target */
918 masks[pc + 1 + filter[pc].k] &= memvalid;
921 case BPF_JMP | BPF_JEQ | BPF_K:
922 case BPF_JMP | BPF_JEQ | BPF_X:
923 case BPF_JMP | BPF_JGE | BPF_K:
924 case BPF_JMP | BPF_JGE | BPF_X:
925 case BPF_JMP | BPF_JGT | BPF_K:
926 case BPF_JMP | BPF_JGT | BPF_X:
927 case BPF_JMP | BPF_JSET | BPF_K:
928 case BPF_JMP | BPF_JSET | BPF_X:
929 /* A jump must set masks on targets */
930 masks[pc + 1 + filter[pc].jt] &= memvalid;
931 masks[pc + 1 + filter[pc].jf] &= memvalid;
941 static bool chk_code_allowed(u16 code_to_probe)
943 static const bool codes[] = {
944 /* 32 bit ALU operations */
945 [BPF_ALU | BPF_ADD | BPF_K] = true,
946 [BPF_ALU | BPF_ADD | BPF_X] = true,
947 [BPF_ALU | BPF_SUB | BPF_K] = true,
948 [BPF_ALU | BPF_SUB | BPF_X] = true,
949 [BPF_ALU | BPF_MUL | BPF_K] = true,
950 [BPF_ALU | BPF_MUL | BPF_X] = true,
951 [BPF_ALU | BPF_DIV | BPF_K] = true,
952 [BPF_ALU | BPF_DIV | BPF_X] = true,
953 [BPF_ALU | BPF_MOD | BPF_K] = true,
954 [BPF_ALU | BPF_MOD | BPF_X] = true,
955 [BPF_ALU | BPF_AND | BPF_K] = true,
956 [BPF_ALU | BPF_AND | BPF_X] = true,
957 [BPF_ALU | BPF_OR | BPF_K] = true,
958 [BPF_ALU | BPF_OR | BPF_X] = true,
959 [BPF_ALU | BPF_XOR | BPF_K] = true,
960 [BPF_ALU | BPF_XOR | BPF_X] = true,
961 [BPF_ALU | BPF_LSH | BPF_K] = true,
962 [BPF_ALU | BPF_LSH | BPF_X] = true,
963 [BPF_ALU | BPF_RSH | BPF_K] = true,
964 [BPF_ALU | BPF_RSH | BPF_X] = true,
965 [BPF_ALU | BPF_NEG] = true,
966 /* Load instructions */
967 [BPF_LD | BPF_W | BPF_ABS] = true,
968 [BPF_LD | BPF_H | BPF_ABS] = true,
969 [BPF_LD | BPF_B | BPF_ABS] = true,
970 [BPF_LD | BPF_W | BPF_LEN] = true,
971 [BPF_LD | BPF_W | BPF_IND] = true,
972 [BPF_LD | BPF_H | BPF_IND] = true,
973 [BPF_LD | BPF_B | BPF_IND] = true,
974 [BPF_LD | BPF_IMM] = true,
975 [BPF_LD | BPF_MEM] = true,
976 [BPF_LDX | BPF_W | BPF_LEN] = true,
977 [BPF_LDX | BPF_B | BPF_MSH] = true,
978 [BPF_LDX | BPF_IMM] = true,
979 [BPF_LDX | BPF_MEM] = true,
980 /* Store instructions */
983 /* Misc instructions */
984 [BPF_MISC | BPF_TAX] = true,
985 [BPF_MISC | BPF_TXA] = true,
986 /* Return instructions */
987 [BPF_RET | BPF_K] = true,
988 [BPF_RET | BPF_A] = true,
989 /* Jump instructions */
990 [BPF_JMP | BPF_JA] = true,
991 [BPF_JMP | BPF_JEQ | BPF_K] = true,
992 [BPF_JMP | BPF_JEQ | BPF_X] = true,
993 [BPF_JMP | BPF_JGE | BPF_K] = true,
994 [BPF_JMP | BPF_JGE | BPF_X] = true,
995 [BPF_JMP | BPF_JGT | BPF_K] = true,
996 [BPF_JMP | BPF_JGT | BPF_X] = true,
997 [BPF_JMP | BPF_JSET | BPF_K] = true,
998 [BPF_JMP | BPF_JSET | BPF_X] = true,
1001 if (code_to_probe >= ARRAY_SIZE(codes))
1004 return codes[code_to_probe];
1007 static bool bpf_check_basics_ok(const struct sock_filter *filter,
1012 if (flen == 0 || flen > BPF_MAXINSNS)
1019 * bpf_check_classic - verify socket filter code
1020 * @filter: filter to verify
1021 * @flen: length of filter
1023 * Check the user's filter code. If we let some ugly
1024 * filter code slip through kaboom! The filter must contain
1025 * no references or jumps that are out of range, no illegal
1026 * instructions, and must end with a RET instruction.
1028 * All jumps are forward as they are not signed.
1030 * Returns 0 if the rule set is legal or -EINVAL if not.
1032 static int bpf_check_classic(const struct sock_filter *filter,
1038 /* Check the filter code now */
1039 for (pc = 0; pc < flen; pc++) {
1040 const struct sock_filter *ftest = &filter[pc];
1042 /* May we actually operate on this code? */
1043 if (!chk_code_allowed(ftest->code))
1046 /* Some instructions need special checks */
1047 switch (ftest->code) {
1048 case BPF_ALU | BPF_DIV | BPF_K:
1049 case BPF_ALU | BPF_MOD | BPF_K:
1050 /* Check for division by zero */
1054 case BPF_ALU | BPF_LSH | BPF_K:
1055 case BPF_ALU | BPF_RSH | BPF_K:
1059 case BPF_LD | BPF_MEM:
1060 case BPF_LDX | BPF_MEM:
1063 /* Check for invalid memory addresses */
1064 if (ftest->k >= BPF_MEMWORDS)
1067 case BPF_JMP | BPF_JA:
1068 /* Note, the large ftest->k might cause loops.
1069 * Compare this with conditional jumps below,
1070 * where offsets are limited. --ANK (981016)
1072 if (ftest->k >= (unsigned int)(flen - pc - 1))
1075 case BPF_JMP | BPF_JEQ | BPF_K:
1076 case BPF_JMP | BPF_JEQ | BPF_X:
1077 case BPF_JMP | BPF_JGE | BPF_K:
1078 case BPF_JMP | BPF_JGE | BPF_X:
1079 case BPF_JMP | BPF_JGT | BPF_K:
1080 case BPF_JMP | BPF_JGT | BPF_X:
1081 case BPF_JMP | BPF_JSET | BPF_K:
1082 case BPF_JMP | BPF_JSET | BPF_X:
1083 /* Both conditionals must be safe */
1084 if (pc + ftest->jt + 1 >= flen ||
1085 pc + ftest->jf + 1 >= flen)
1088 case BPF_LD | BPF_W | BPF_ABS:
1089 case BPF_LD | BPF_H | BPF_ABS:
1090 case BPF_LD | BPF_B | BPF_ABS:
1092 if (bpf_anc_helper(ftest) & BPF_ANC)
1094 /* Ancillary operation unknown or unsupported */
1095 if (anc_found == false && ftest->k >= SKF_AD_OFF)
1100 /* Last instruction must be a RET code */
1101 switch (filter[flen - 1].code) {
1102 case BPF_RET | BPF_K:
1103 case BPF_RET | BPF_A:
1104 return check_load_and_stores(filter, flen);
1110 static int bpf_prog_store_orig_filter(struct bpf_prog *fp,
1111 const struct sock_fprog *fprog)
1113 unsigned int fsize = bpf_classic_proglen(fprog);
1114 struct sock_fprog_kern *fkprog;
1116 fp->orig_prog = kmalloc(sizeof(*fkprog), GFP_KERNEL);
1120 fkprog = fp->orig_prog;
1121 fkprog->len = fprog->len;
1123 fkprog->filter = kmemdup(fp->insns, fsize,
1124 GFP_KERNEL | __GFP_NOWARN);
1125 if (!fkprog->filter) {
1126 kfree(fp->orig_prog);
1133 static void bpf_release_orig_filter(struct bpf_prog *fp)
1135 struct sock_fprog_kern *fprog = fp->orig_prog;
1138 kfree(fprog->filter);
1143 static void __bpf_prog_release(struct bpf_prog *prog)
1145 if (prog->type == BPF_PROG_TYPE_SOCKET_FILTER) {
1148 bpf_release_orig_filter(prog);
1149 bpf_prog_free(prog);
1153 static void __sk_filter_release(struct sk_filter *fp)
1155 __bpf_prog_release(fp->prog);
1160 * sk_filter_release_rcu - Release a socket filter by rcu_head
1161 * @rcu: rcu_head that contains the sk_filter to free
1163 static void sk_filter_release_rcu(struct rcu_head *rcu)
1165 struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu);
1167 __sk_filter_release(fp);
1171 * sk_filter_release - release a socket filter
1172 * @fp: filter to remove
1174 * Remove a filter from a socket and release its resources.
1176 static void sk_filter_release(struct sk_filter *fp)
1178 if (refcount_dec_and_test(&fp->refcnt))
1179 call_rcu(&fp->rcu, sk_filter_release_rcu);
1182 void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp)
1184 u32 filter_size = bpf_prog_size(fp->prog->len);
1186 atomic_sub(filter_size, &sk->sk_omem_alloc);
1187 sk_filter_release(fp);
1190 /* try to charge the socket memory if there is space available
1191 * return true on success
1193 static bool __sk_filter_charge(struct sock *sk, struct sk_filter *fp)
1195 u32 filter_size = bpf_prog_size(fp->prog->len);
1197 /* same check as in sock_kmalloc() */
1198 if (filter_size <= sysctl_optmem_max &&
1199 atomic_read(&sk->sk_omem_alloc) + filter_size < sysctl_optmem_max) {
1200 atomic_add(filter_size, &sk->sk_omem_alloc);
1206 bool sk_filter_charge(struct sock *sk, struct sk_filter *fp)
1208 if (!refcount_inc_not_zero(&fp->refcnt))
1211 if (!__sk_filter_charge(sk, fp)) {
1212 sk_filter_release(fp);
1218 static struct bpf_prog *bpf_migrate_filter(struct bpf_prog *fp)
1220 struct sock_filter *old_prog;
1221 struct bpf_prog *old_fp;
1222 int err, new_len, old_len = fp->len;
1223 bool seen_ld_abs = false;
1225 /* We are free to overwrite insns et al right here as it
1226 * won't be used at this point in time anymore internally
1227 * after the migration to the internal BPF instruction
1230 BUILD_BUG_ON(sizeof(struct sock_filter) !=
1231 sizeof(struct bpf_insn));
1233 /* Conversion cannot happen on overlapping memory areas,
1234 * so we need to keep the user BPF around until the 2nd
1235 * pass. At this time, the user BPF is stored in fp->insns.
1237 old_prog = kmemdup(fp->insns, old_len * sizeof(struct sock_filter),
1238 GFP_KERNEL | __GFP_NOWARN);
1244 /* 1st pass: calculate the new program length. */
1245 err = bpf_convert_filter(old_prog, old_len, NULL, &new_len,
1250 /* Expand fp for appending the new filter representation. */
1252 fp = bpf_prog_realloc(old_fp, bpf_prog_size(new_len), 0);
1254 /* The old_fp is still around in case we couldn't
1255 * allocate new memory, so uncharge on that one.
1264 /* 2nd pass: remap sock_filter insns into bpf_insn insns. */
1265 err = bpf_convert_filter(old_prog, old_len, fp, &new_len,
1268 /* 2nd bpf_convert_filter() can fail only if it fails
1269 * to allocate memory, remapping must succeed. Note,
1270 * that at this time old_fp has already been released
1275 fp = bpf_prog_select_runtime(fp, &err);
1285 __bpf_prog_release(fp);
1286 return ERR_PTR(err);
1289 static struct bpf_prog *bpf_prepare_filter(struct bpf_prog *fp,
1290 bpf_aux_classic_check_t trans)
1294 fp->bpf_func = NULL;
1297 err = bpf_check_classic(fp->insns, fp->len);
1299 __bpf_prog_release(fp);
1300 return ERR_PTR(err);
1303 /* There might be additional checks and transformations
1304 * needed on classic filters, f.e. in case of seccomp.
1307 err = trans(fp->insns, fp->len);
1309 __bpf_prog_release(fp);
1310 return ERR_PTR(err);
1314 /* Probe if we can JIT compile the filter and if so, do
1315 * the compilation of the filter.
1317 bpf_jit_compile(fp);
1319 /* JIT compiler couldn't process this filter, so do the
1320 * internal BPF translation for the optimized interpreter.
1323 fp = bpf_migrate_filter(fp);
1329 * bpf_prog_create - create an unattached filter
1330 * @pfp: the unattached filter that is created
1331 * @fprog: the filter program
1333 * Create a filter independent of any socket. We first run some
1334 * sanity checks on it to make sure it does not explode on us later.
1335 * If an error occurs or there is insufficient memory for the filter
1336 * a negative errno code is returned. On success the return is zero.
1338 int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog)
1340 unsigned int fsize = bpf_classic_proglen(fprog);
1341 struct bpf_prog *fp;
1343 /* Make sure new filter is there and in the right amounts. */
1344 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1347 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1351 memcpy(fp->insns, fprog->filter, fsize);
1353 fp->len = fprog->len;
1354 /* Since unattached filters are not copied back to user
1355 * space through sk_get_filter(), we do not need to hold
1356 * a copy here, and can spare us the work.
1358 fp->orig_prog = NULL;
1360 /* bpf_prepare_filter() already takes care of freeing
1361 * memory in case something goes wrong.
1363 fp = bpf_prepare_filter(fp, NULL);
1370 EXPORT_SYMBOL_GPL(bpf_prog_create);
1373 * bpf_prog_create_from_user - create an unattached filter from user buffer
1374 * @pfp: the unattached filter that is created
1375 * @fprog: the filter program
1376 * @trans: post-classic verifier transformation handler
1377 * @save_orig: save classic BPF program
1379 * This function effectively does the same as bpf_prog_create(), only
1380 * that it builds up its insns buffer from user space provided buffer.
1381 * It also allows for passing a bpf_aux_classic_check_t handler.
1383 int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog,
1384 bpf_aux_classic_check_t trans, bool save_orig)
1386 unsigned int fsize = bpf_classic_proglen(fprog);
1387 struct bpf_prog *fp;
1390 /* Make sure new filter is there and in the right amounts. */
1391 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1394 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1398 if (copy_from_user(fp->insns, fprog->filter, fsize)) {
1399 __bpf_prog_free(fp);
1403 fp->len = fprog->len;
1404 fp->orig_prog = NULL;
1407 err = bpf_prog_store_orig_filter(fp, fprog);
1409 __bpf_prog_free(fp);
1414 /* bpf_prepare_filter() already takes care of freeing
1415 * memory in case something goes wrong.
1417 fp = bpf_prepare_filter(fp, trans);
1424 EXPORT_SYMBOL_GPL(bpf_prog_create_from_user);
1426 void bpf_prog_destroy(struct bpf_prog *fp)
1428 __bpf_prog_release(fp);
1430 EXPORT_SYMBOL_GPL(bpf_prog_destroy);
1432 static int __sk_attach_prog(struct bpf_prog *prog, struct sock *sk)
1434 struct sk_filter *fp, *old_fp;
1436 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
1442 if (!__sk_filter_charge(sk, fp)) {
1446 refcount_set(&fp->refcnt, 1);
1448 old_fp = rcu_dereference_protected(sk->sk_filter,
1449 lockdep_sock_is_held(sk));
1450 rcu_assign_pointer(sk->sk_filter, fp);
1453 sk_filter_uncharge(sk, old_fp);
1459 struct bpf_prog *__get_filter(struct sock_fprog *fprog, struct sock *sk)
1461 unsigned int fsize = bpf_classic_proglen(fprog);
1462 struct bpf_prog *prog;
1465 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1466 return ERR_PTR(-EPERM);
1468 /* Make sure new filter is there and in the right amounts. */
1469 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1470 return ERR_PTR(-EINVAL);
1472 prog = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1474 return ERR_PTR(-ENOMEM);
1476 if (copy_from_user(prog->insns, fprog->filter, fsize)) {
1477 __bpf_prog_free(prog);
1478 return ERR_PTR(-EFAULT);
1481 prog->len = fprog->len;
1483 err = bpf_prog_store_orig_filter(prog, fprog);
1485 __bpf_prog_free(prog);
1486 return ERR_PTR(-ENOMEM);
1489 /* bpf_prepare_filter() already takes care of freeing
1490 * memory in case something goes wrong.
1492 return bpf_prepare_filter(prog, NULL);
1496 * sk_attach_filter - attach a socket filter
1497 * @fprog: the filter program
1498 * @sk: the socket to use
1500 * Attach the user's filter code. We first run some sanity checks on
1501 * it to make sure it does not explode on us later. If an error
1502 * occurs or there is insufficient memory for the filter a negative
1503 * errno code is returned. On success the return is zero.
1505 int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1507 struct bpf_prog *prog = __get_filter(fprog, sk);
1511 return PTR_ERR(prog);
1513 err = __sk_attach_prog(prog, sk);
1515 __bpf_prog_release(prog);
1521 EXPORT_SYMBOL_GPL(sk_attach_filter);
1523 int sk_reuseport_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1525 struct bpf_prog *prog = __get_filter(fprog, sk);
1529 return PTR_ERR(prog);
1531 if (bpf_prog_size(prog->len) > sysctl_optmem_max)
1534 err = reuseport_attach_prog(sk, prog);
1537 __bpf_prog_release(prog);
1542 static struct bpf_prog *__get_bpf(u32 ufd, struct sock *sk)
1544 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1545 return ERR_PTR(-EPERM);
1547 return bpf_prog_get_type(ufd, BPF_PROG_TYPE_SOCKET_FILTER);
1550 int sk_attach_bpf(u32 ufd, struct sock *sk)
1552 struct bpf_prog *prog = __get_bpf(ufd, sk);
1556 return PTR_ERR(prog);
1558 err = __sk_attach_prog(prog, sk);
1567 int sk_reuseport_attach_bpf(u32 ufd, struct sock *sk)
1569 struct bpf_prog *prog;
1572 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1575 prog = bpf_prog_get_type(ufd, BPF_PROG_TYPE_SOCKET_FILTER);
1576 if (IS_ERR(prog) && PTR_ERR(prog) == -EINVAL)
1577 prog = bpf_prog_get_type(ufd, BPF_PROG_TYPE_SK_REUSEPORT);
1579 return PTR_ERR(prog);
1581 if (prog->type == BPF_PROG_TYPE_SK_REUSEPORT) {
1582 /* Like other non BPF_PROG_TYPE_SOCKET_FILTER
1583 * bpf prog (e.g. sockmap). It depends on the
1584 * limitation imposed by bpf_prog_load().
1585 * Hence, sysctl_optmem_max is not checked.
1587 if ((sk->sk_type != SOCK_STREAM &&
1588 sk->sk_type != SOCK_DGRAM) ||
1589 (sk->sk_protocol != IPPROTO_UDP &&
1590 sk->sk_protocol != IPPROTO_TCP) ||
1591 (sk->sk_family != AF_INET &&
1592 sk->sk_family != AF_INET6)) {
1597 /* BPF_PROG_TYPE_SOCKET_FILTER */
1598 if (bpf_prog_size(prog->len) > sysctl_optmem_max) {
1604 err = reuseport_attach_prog(sk, prog);
1612 void sk_reuseport_prog_free(struct bpf_prog *prog)
1617 if (prog->type == BPF_PROG_TYPE_SK_REUSEPORT)
1620 bpf_prog_destroy(prog);
1623 struct bpf_scratchpad {
1625 __be32 diff[MAX_BPF_STACK / sizeof(__be32)];
1626 u8 buff[MAX_BPF_STACK];
1630 static DEFINE_PER_CPU(struct bpf_scratchpad, bpf_sp);
1632 static inline int __bpf_try_make_writable(struct sk_buff *skb,
1633 unsigned int write_len)
1635 return skb_ensure_writable(skb, write_len);
1638 static inline int bpf_try_make_writable(struct sk_buff *skb,
1639 unsigned int write_len)
1641 int err = __bpf_try_make_writable(skb, write_len);
1643 bpf_compute_data_pointers(skb);
1647 static int bpf_try_make_head_writable(struct sk_buff *skb)
1649 return bpf_try_make_writable(skb, skb_headlen(skb));
1652 static inline void bpf_push_mac_rcsum(struct sk_buff *skb)
1654 if (skb_at_tc_ingress(skb))
1655 skb_postpush_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1658 static inline void bpf_pull_mac_rcsum(struct sk_buff *skb)
1660 if (skb_at_tc_ingress(skb))
1661 skb_postpull_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1664 BPF_CALL_5(bpf_skb_store_bytes, struct sk_buff *, skb, u32, offset,
1665 const void *, from, u32, len, u64, flags)
1669 if (unlikely(flags & ~(BPF_F_RECOMPUTE_CSUM | BPF_F_INVALIDATE_HASH)))
1671 if (unlikely(offset > 0xffff))
1673 if (unlikely(bpf_try_make_writable(skb, offset + len)))
1676 ptr = skb->data + offset;
1677 if (flags & BPF_F_RECOMPUTE_CSUM)
1678 __skb_postpull_rcsum(skb, ptr, len, offset);
1680 memcpy(ptr, from, len);
1682 if (flags & BPF_F_RECOMPUTE_CSUM)
1683 __skb_postpush_rcsum(skb, ptr, len, offset);
1684 if (flags & BPF_F_INVALIDATE_HASH)
1685 skb_clear_hash(skb);
1690 static const struct bpf_func_proto bpf_skb_store_bytes_proto = {
1691 .func = bpf_skb_store_bytes,
1693 .ret_type = RET_INTEGER,
1694 .arg1_type = ARG_PTR_TO_CTX,
1695 .arg2_type = ARG_ANYTHING,
1696 .arg3_type = ARG_PTR_TO_MEM,
1697 .arg4_type = ARG_CONST_SIZE,
1698 .arg5_type = ARG_ANYTHING,
1701 BPF_CALL_4(bpf_skb_load_bytes, const struct sk_buff *, skb, u32, offset,
1702 void *, to, u32, len)
1706 if (unlikely(offset > 0xffff))
1709 ptr = skb_header_pointer(skb, offset, len, to);
1713 memcpy(to, ptr, len);
1721 static const struct bpf_func_proto bpf_skb_load_bytes_proto = {
1722 .func = bpf_skb_load_bytes,
1724 .ret_type = RET_INTEGER,
1725 .arg1_type = ARG_PTR_TO_CTX,
1726 .arg2_type = ARG_ANYTHING,
1727 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
1728 .arg4_type = ARG_CONST_SIZE,
1731 BPF_CALL_4(bpf_flow_dissector_load_bytes,
1732 const struct bpf_flow_dissector *, ctx, u32, offset,
1733 void *, to, u32, len)
1737 if (unlikely(offset > 0xffff))
1740 if (unlikely(!ctx->skb))
1743 ptr = skb_header_pointer(ctx->skb, offset, len, to);
1747 memcpy(to, ptr, len);
1755 static const struct bpf_func_proto bpf_flow_dissector_load_bytes_proto = {
1756 .func = bpf_flow_dissector_load_bytes,
1758 .ret_type = RET_INTEGER,
1759 .arg1_type = ARG_PTR_TO_CTX,
1760 .arg2_type = ARG_ANYTHING,
1761 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
1762 .arg4_type = ARG_CONST_SIZE,
1765 BPF_CALL_5(bpf_skb_load_bytes_relative, const struct sk_buff *, skb,
1766 u32, offset, void *, to, u32, len, u32, start_header)
1768 u8 *end = skb_tail_pointer(skb);
1769 u8 *net = skb_network_header(skb);
1770 u8 *mac = skb_mac_header(skb);
1773 if (unlikely(offset > 0xffff || len > (end - mac)))
1776 switch (start_header) {
1777 case BPF_HDR_START_MAC:
1780 case BPF_HDR_START_NET:
1787 if (likely(ptr >= mac && ptr + len <= end)) {
1788 memcpy(to, ptr, len);
1797 static const struct bpf_func_proto bpf_skb_load_bytes_relative_proto = {
1798 .func = bpf_skb_load_bytes_relative,
1800 .ret_type = RET_INTEGER,
1801 .arg1_type = ARG_PTR_TO_CTX,
1802 .arg2_type = ARG_ANYTHING,
1803 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
1804 .arg4_type = ARG_CONST_SIZE,
1805 .arg5_type = ARG_ANYTHING,
1808 BPF_CALL_2(bpf_skb_pull_data, struct sk_buff *, skb, u32, len)
1810 /* Idea is the following: should the needed direct read/write
1811 * test fail during runtime, we can pull in more data and redo
1812 * again, since implicitly, we invalidate previous checks here.
1814 * Or, since we know how much we need to make read/writeable,
1815 * this can be done once at the program beginning for direct
1816 * access case. By this we overcome limitations of only current
1817 * headroom being accessible.
1819 return bpf_try_make_writable(skb, len ? : skb_headlen(skb));
1822 static const struct bpf_func_proto bpf_skb_pull_data_proto = {
1823 .func = bpf_skb_pull_data,
1825 .ret_type = RET_INTEGER,
1826 .arg1_type = ARG_PTR_TO_CTX,
1827 .arg2_type = ARG_ANYTHING,
1830 BPF_CALL_1(bpf_sk_fullsock, struct sock *, sk)
1832 return sk_fullsock(sk) ? (unsigned long)sk : (unsigned long)NULL;
1835 static const struct bpf_func_proto bpf_sk_fullsock_proto = {
1836 .func = bpf_sk_fullsock,
1838 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
1839 .arg1_type = ARG_PTR_TO_SOCK_COMMON,
1842 static inline int sk_skb_try_make_writable(struct sk_buff *skb,
1843 unsigned int write_len)
1845 int err = __bpf_try_make_writable(skb, write_len);
1847 bpf_compute_data_end_sk_skb(skb);
1851 BPF_CALL_2(sk_skb_pull_data, struct sk_buff *, skb, u32, len)
1853 /* Idea is the following: should the needed direct read/write
1854 * test fail during runtime, we can pull in more data and redo
1855 * again, since implicitly, we invalidate previous checks here.
1857 * Or, since we know how much we need to make read/writeable,
1858 * this can be done once at the program beginning for direct
1859 * access case. By this we overcome limitations of only current
1860 * headroom being accessible.
1862 return sk_skb_try_make_writable(skb, len ? : skb_headlen(skb));
1865 static const struct bpf_func_proto sk_skb_pull_data_proto = {
1866 .func = sk_skb_pull_data,
1868 .ret_type = RET_INTEGER,
1869 .arg1_type = ARG_PTR_TO_CTX,
1870 .arg2_type = ARG_ANYTHING,
1873 BPF_CALL_5(bpf_l3_csum_replace, struct sk_buff *, skb, u32, offset,
1874 u64, from, u64, to, u64, flags)
1878 if (unlikely(flags & ~(BPF_F_HDR_FIELD_MASK)))
1880 if (unlikely(offset > 0xffff || offset & 1))
1882 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
1885 ptr = (__sum16 *)(skb->data + offset);
1886 switch (flags & BPF_F_HDR_FIELD_MASK) {
1888 if (unlikely(from != 0))
1891 csum_replace_by_diff(ptr, to);
1894 csum_replace2(ptr, from, to);
1897 csum_replace4(ptr, from, to);
1906 static const struct bpf_func_proto bpf_l3_csum_replace_proto = {
1907 .func = bpf_l3_csum_replace,
1909 .ret_type = RET_INTEGER,
1910 .arg1_type = ARG_PTR_TO_CTX,
1911 .arg2_type = ARG_ANYTHING,
1912 .arg3_type = ARG_ANYTHING,
1913 .arg4_type = ARG_ANYTHING,
1914 .arg5_type = ARG_ANYTHING,
1917 BPF_CALL_5(bpf_l4_csum_replace, struct sk_buff *, skb, u32, offset,
1918 u64, from, u64, to, u64, flags)
1920 bool is_pseudo = flags & BPF_F_PSEUDO_HDR;
1921 bool is_mmzero = flags & BPF_F_MARK_MANGLED_0;
1922 bool do_mforce = flags & BPF_F_MARK_ENFORCE;
1925 if (unlikely(flags & ~(BPF_F_MARK_MANGLED_0 | BPF_F_MARK_ENFORCE |
1926 BPF_F_PSEUDO_HDR | BPF_F_HDR_FIELD_MASK)))
1928 if (unlikely(offset > 0xffff || offset & 1))
1930 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
1933 ptr = (__sum16 *)(skb->data + offset);
1934 if (is_mmzero && !do_mforce && !*ptr)
1937 switch (flags & BPF_F_HDR_FIELD_MASK) {
1939 if (unlikely(from != 0))
1942 inet_proto_csum_replace_by_diff(ptr, skb, to, is_pseudo);
1945 inet_proto_csum_replace2(ptr, skb, from, to, is_pseudo);
1948 inet_proto_csum_replace4(ptr, skb, from, to, is_pseudo);
1954 if (is_mmzero && !*ptr)
1955 *ptr = CSUM_MANGLED_0;
1959 static const struct bpf_func_proto bpf_l4_csum_replace_proto = {
1960 .func = bpf_l4_csum_replace,
1962 .ret_type = RET_INTEGER,
1963 .arg1_type = ARG_PTR_TO_CTX,
1964 .arg2_type = ARG_ANYTHING,
1965 .arg3_type = ARG_ANYTHING,
1966 .arg4_type = ARG_ANYTHING,
1967 .arg5_type = ARG_ANYTHING,
1970 BPF_CALL_5(bpf_csum_diff, __be32 *, from, u32, from_size,
1971 __be32 *, to, u32, to_size, __wsum, seed)
1973 struct bpf_scratchpad *sp = this_cpu_ptr(&bpf_sp);
1974 u32 diff_size = from_size + to_size;
1977 /* This is quite flexible, some examples:
1979 * from_size == 0, to_size > 0, seed := csum --> pushing data
1980 * from_size > 0, to_size == 0, seed := csum --> pulling data
1981 * from_size > 0, to_size > 0, seed := 0 --> diffing data
1983 * Even for diffing, from_size and to_size don't need to be equal.
1985 if (unlikely(((from_size | to_size) & (sizeof(__be32) - 1)) ||
1986 diff_size > sizeof(sp->diff)))
1989 for (i = 0; i < from_size / sizeof(__be32); i++, j++)
1990 sp->diff[j] = ~from[i];
1991 for (i = 0; i < to_size / sizeof(__be32); i++, j++)
1992 sp->diff[j] = to[i];
1994 return csum_partial(sp->diff, diff_size, seed);
1997 static const struct bpf_func_proto bpf_csum_diff_proto = {
1998 .func = bpf_csum_diff,
2001 .ret_type = RET_INTEGER,
2002 .arg1_type = ARG_PTR_TO_MEM_OR_NULL,
2003 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
2004 .arg3_type = ARG_PTR_TO_MEM_OR_NULL,
2005 .arg4_type = ARG_CONST_SIZE_OR_ZERO,
2006 .arg5_type = ARG_ANYTHING,
2009 BPF_CALL_2(bpf_csum_update, struct sk_buff *, skb, __wsum, csum)
2011 /* The interface is to be used in combination with bpf_csum_diff()
2012 * for direct packet writes. csum rotation for alignment as well
2013 * as emulating csum_sub() can be done from the eBPF program.
2015 if (skb->ip_summed == CHECKSUM_COMPLETE)
2016 return (skb->csum = csum_add(skb->csum, csum));
2021 static const struct bpf_func_proto bpf_csum_update_proto = {
2022 .func = bpf_csum_update,
2024 .ret_type = RET_INTEGER,
2025 .arg1_type = ARG_PTR_TO_CTX,
2026 .arg2_type = ARG_ANYTHING,
2029 static inline int __bpf_rx_skb(struct net_device *dev, struct sk_buff *skb)
2031 return dev_forward_skb(dev, skb);
2034 static inline int __bpf_rx_skb_no_mac(struct net_device *dev,
2035 struct sk_buff *skb)
2037 int ret = ____dev_forward_skb(dev, skb);
2041 ret = netif_rx(skb);
2047 static inline int __bpf_tx_skb(struct net_device *dev, struct sk_buff *skb)
2051 if (dev_xmit_recursion()) {
2052 net_crit_ratelimited("bpf: recursion limit reached on datapath, buggy bpf program?\n");
2059 dev_xmit_recursion_inc();
2060 ret = dev_queue_xmit(skb);
2061 dev_xmit_recursion_dec();
2066 static int __bpf_redirect_no_mac(struct sk_buff *skb, struct net_device *dev,
2069 unsigned int mlen = skb_network_offset(skb);
2072 __skb_pull(skb, mlen);
2074 /* At ingress, the mac header has already been pulled once.
2075 * At egress, skb_pospull_rcsum has to be done in case that
2076 * the skb is originated from ingress (i.e. a forwarded skb)
2077 * to ensure that rcsum starts at net header.
2079 if (!skb_at_tc_ingress(skb))
2080 skb_postpull_rcsum(skb, skb_mac_header(skb), mlen);
2082 skb_pop_mac_header(skb);
2083 skb_reset_mac_len(skb);
2084 return flags & BPF_F_INGRESS ?
2085 __bpf_rx_skb_no_mac(dev, skb) : __bpf_tx_skb(dev, skb);
2088 static int __bpf_redirect_common(struct sk_buff *skb, struct net_device *dev,
2091 /* Verify that a link layer header is carried */
2092 if (unlikely(skb->mac_header >= skb->network_header)) {
2097 bpf_push_mac_rcsum(skb);
2098 return flags & BPF_F_INGRESS ?
2099 __bpf_rx_skb(dev, skb) : __bpf_tx_skb(dev, skb);
2102 static int __bpf_redirect(struct sk_buff *skb, struct net_device *dev,
2105 if (dev_is_mac_header_xmit(dev))
2106 return __bpf_redirect_common(skb, dev, flags);
2108 return __bpf_redirect_no_mac(skb, dev, flags);
2111 BPF_CALL_3(bpf_clone_redirect, struct sk_buff *, skb, u32, ifindex, u64, flags)
2113 struct net_device *dev;
2114 struct sk_buff *clone;
2117 if (unlikely(flags & ~(BPF_F_INGRESS)))
2120 dev = dev_get_by_index_rcu(dev_net(skb->dev), ifindex);
2124 clone = skb_clone(skb, GFP_ATOMIC);
2125 if (unlikely(!clone))
2128 /* For direct write, we need to keep the invariant that the skbs
2129 * we're dealing with need to be uncloned. Should uncloning fail
2130 * here, we need to free the just generated clone to unclone once
2133 ret = bpf_try_make_head_writable(skb);
2134 if (unlikely(ret)) {
2139 return __bpf_redirect(clone, dev, flags);
2142 static const struct bpf_func_proto bpf_clone_redirect_proto = {
2143 .func = bpf_clone_redirect,
2145 .ret_type = RET_INTEGER,
2146 .arg1_type = ARG_PTR_TO_CTX,
2147 .arg2_type = ARG_ANYTHING,
2148 .arg3_type = ARG_ANYTHING,
2151 DEFINE_PER_CPU(struct bpf_redirect_info, bpf_redirect_info);
2152 EXPORT_PER_CPU_SYMBOL_GPL(bpf_redirect_info);
2154 BPF_CALL_2(bpf_redirect, u32, ifindex, u64, flags)
2156 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
2158 if (unlikely(flags & ~(BPF_F_INGRESS)))
2162 ri->tgt_index = ifindex;
2164 return TC_ACT_REDIRECT;
2167 int skb_do_redirect(struct sk_buff *skb)
2169 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
2170 struct net_device *dev;
2172 dev = dev_get_by_index_rcu(dev_net(skb->dev), ri->tgt_index);
2174 if (unlikely(!dev)) {
2179 return __bpf_redirect(skb, dev, ri->flags);
2182 static const struct bpf_func_proto bpf_redirect_proto = {
2183 .func = bpf_redirect,
2185 .ret_type = RET_INTEGER,
2186 .arg1_type = ARG_ANYTHING,
2187 .arg2_type = ARG_ANYTHING,
2190 BPF_CALL_2(bpf_msg_apply_bytes, struct sk_msg *, msg, u32, bytes)
2192 msg->apply_bytes = bytes;
2196 static const struct bpf_func_proto bpf_msg_apply_bytes_proto = {
2197 .func = bpf_msg_apply_bytes,
2199 .ret_type = RET_INTEGER,
2200 .arg1_type = ARG_PTR_TO_CTX,
2201 .arg2_type = ARG_ANYTHING,
2204 BPF_CALL_2(bpf_msg_cork_bytes, struct sk_msg *, msg, u32, bytes)
2206 msg->cork_bytes = bytes;
2210 static const struct bpf_func_proto bpf_msg_cork_bytes_proto = {
2211 .func = bpf_msg_cork_bytes,
2213 .ret_type = RET_INTEGER,
2214 .arg1_type = ARG_PTR_TO_CTX,
2215 .arg2_type = ARG_ANYTHING,
2218 BPF_CALL_4(bpf_msg_pull_data, struct sk_msg *, msg, u32, start,
2219 u32, end, u64, flags)
2221 u32 len = 0, offset = 0, copy = 0, poffset = 0, bytes = end - start;
2222 u32 first_sge, last_sge, i, shift, bytes_sg_total;
2223 struct scatterlist *sge;
2224 u8 *raw, *to, *from;
2227 if (unlikely(flags || end <= start))
2230 /* First find the starting scatterlist element */
2233 len = sk_msg_elem(msg, i)->length;
2234 if (start < offset + len)
2237 sk_msg_iter_var_next(i);
2238 } while (i != msg->sg.end);
2240 if (unlikely(start >= offset + len))
2244 /* The start may point into the sg element so we need to also
2245 * account for the headroom.
2247 bytes_sg_total = start - offset + bytes;
2248 if (!msg->sg.copy[i] && bytes_sg_total <= len)
2251 /* At this point we need to linearize multiple scatterlist
2252 * elements or a single shared page. Either way we need to
2253 * copy into a linear buffer exclusively owned by BPF. Then
2254 * place the buffer in the scatterlist and fixup the original
2255 * entries by removing the entries now in the linear buffer
2256 * and shifting the remaining entries. For now we do not try
2257 * to copy partial entries to avoid complexity of running out
2258 * of sg_entry slots. The downside is reading a single byte
2259 * will copy the entire sg entry.
2262 copy += sk_msg_elem(msg, i)->length;
2263 sk_msg_iter_var_next(i);
2264 if (bytes_sg_total <= copy)
2266 } while (i != msg->sg.end);
2269 if (unlikely(bytes_sg_total > copy))
2272 page = alloc_pages(__GFP_NOWARN | GFP_ATOMIC | __GFP_COMP,
2274 if (unlikely(!page))
2277 raw = page_address(page);
2280 sge = sk_msg_elem(msg, i);
2281 from = sg_virt(sge);
2285 memcpy(to, from, len);
2288 put_page(sg_page(sge));
2290 sk_msg_iter_var_next(i);
2291 } while (i != last_sge);
2293 sg_set_page(&msg->sg.data[first_sge], page, copy, 0);
2295 /* To repair sg ring we need to shift entries. If we only
2296 * had a single entry though we can just replace it and
2297 * be done. Otherwise walk the ring and shift the entries.
2299 WARN_ON_ONCE(last_sge == first_sge);
2300 shift = last_sge > first_sge ?
2301 last_sge - first_sge - 1 :
2302 MAX_SKB_FRAGS - first_sge + last_sge - 1;
2307 sk_msg_iter_var_next(i);
2311 if (i + shift >= MAX_MSG_FRAGS)
2312 move_from = i + shift - MAX_MSG_FRAGS;
2314 move_from = i + shift;
2315 if (move_from == msg->sg.end)
2318 msg->sg.data[i] = msg->sg.data[move_from];
2319 msg->sg.data[move_from].length = 0;
2320 msg->sg.data[move_from].page_link = 0;
2321 msg->sg.data[move_from].offset = 0;
2322 sk_msg_iter_var_next(i);
2325 msg->sg.end = msg->sg.end - shift > msg->sg.end ?
2326 msg->sg.end - shift + MAX_MSG_FRAGS :
2327 msg->sg.end - shift;
2329 msg->data = sg_virt(&msg->sg.data[first_sge]) + start - offset;
2330 msg->data_end = msg->data + bytes;
2334 static const struct bpf_func_proto bpf_msg_pull_data_proto = {
2335 .func = bpf_msg_pull_data,
2337 .ret_type = RET_INTEGER,
2338 .arg1_type = ARG_PTR_TO_CTX,
2339 .arg2_type = ARG_ANYTHING,
2340 .arg3_type = ARG_ANYTHING,
2341 .arg4_type = ARG_ANYTHING,
2344 BPF_CALL_4(bpf_msg_push_data, struct sk_msg *, msg, u32, start,
2345 u32, len, u64, flags)
2347 struct scatterlist sge, nsge, nnsge, rsge = {0}, *psge;
2348 u32 new, i = 0, l, space, copy = 0, offset = 0;
2349 u8 *raw, *to, *from;
2352 if (unlikely(flags))
2355 /* First find the starting scatterlist element */
2358 l = sk_msg_elem(msg, i)->length;
2360 if (start < offset + l)
2363 sk_msg_iter_var_next(i);
2364 } while (i != msg->sg.end);
2366 if (start >= offset + l)
2369 space = MAX_MSG_FRAGS - sk_msg_elem_used(msg);
2371 /* If no space available will fallback to copy, we need at
2372 * least one scatterlist elem available to push data into
2373 * when start aligns to the beginning of an element or two
2374 * when it falls inside an element. We handle the start equals
2375 * offset case because its the common case for inserting a
2378 if (!space || (space == 1 && start != offset))
2379 copy = msg->sg.data[i].length;
2381 page = alloc_pages(__GFP_NOWARN | GFP_ATOMIC | __GFP_COMP,
2382 get_order(copy + len));
2383 if (unlikely(!page))
2389 raw = page_address(page);
2391 psge = sk_msg_elem(msg, i);
2392 front = start - offset;
2393 back = psge->length - front;
2394 from = sg_virt(psge);
2397 memcpy(raw, from, front);
2401 to = raw + front + len;
2403 memcpy(to, from, back);
2406 put_page(sg_page(psge));
2407 } else if (start - offset) {
2408 psge = sk_msg_elem(msg, i);
2409 rsge = sk_msg_elem_cpy(msg, i);
2411 psge->length = start - offset;
2412 rsge.length -= psge->length;
2413 rsge.offset += start;
2415 sk_msg_iter_var_next(i);
2416 sg_unmark_end(psge);
2417 sk_msg_iter_next(msg, end);
2420 /* Slot(s) to place newly allocated data */
2423 /* Shift one or two slots as needed */
2425 sge = sk_msg_elem_cpy(msg, i);
2427 sk_msg_iter_var_next(i);
2428 sg_unmark_end(&sge);
2429 sk_msg_iter_next(msg, end);
2431 nsge = sk_msg_elem_cpy(msg, i);
2433 sk_msg_iter_var_next(i);
2434 nnsge = sk_msg_elem_cpy(msg, i);
2437 while (i != msg->sg.end) {
2438 msg->sg.data[i] = sge;
2440 sk_msg_iter_var_next(i);
2443 nnsge = sk_msg_elem_cpy(msg, i);
2445 nsge = sk_msg_elem_cpy(msg, i);
2450 /* Place newly allocated data buffer */
2451 sk_mem_charge(msg->sk, len);
2452 msg->sg.size += len;
2453 msg->sg.copy[new] = false;
2454 sg_set_page(&msg->sg.data[new], page, len + copy, 0);
2456 get_page(sg_page(&rsge));
2457 sk_msg_iter_var_next(new);
2458 msg->sg.data[new] = rsge;
2461 sk_msg_compute_data_pointers(msg);
2465 static const struct bpf_func_proto bpf_msg_push_data_proto = {
2466 .func = bpf_msg_push_data,
2468 .ret_type = RET_INTEGER,
2469 .arg1_type = ARG_PTR_TO_CTX,
2470 .arg2_type = ARG_ANYTHING,
2471 .arg3_type = ARG_ANYTHING,
2472 .arg4_type = ARG_ANYTHING,
2475 static void sk_msg_shift_left(struct sk_msg *msg, int i)
2481 sk_msg_iter_var_next(i);
2482 msg->sg.data[prev] = msg->sg.data[i];
2483 } while (i != msg->sg.end);
2485 sk_msg_iter_prev(msg, end);
2488 static void sk_msg_shift_right(struct sk_msg *msg, int i)
2490 struct scatterlist tmp, sge;
2492 sk_msg_iter_next(msg, end);
2493 sge = sk_msg_elem_cpy(msg, i);
2494 sk_msg_iter_var_next(i);
2495 tmp = sk_msg_elem_cpy(msg, i);
2497 while (i != msg->sg.end) {
2498 msg->sg.data[i] = sge;
2499 sk_msg_iter_var_next(i);
2501 tmp = sk_msg_elem_cpy(msg, i);
2505 BPF_CALL_4(bpf_msg_pop_data, struct sk_msg *, msg, u32, start,
2506 u32, len, u64, flags)
2508 u32 i = 0, l, space, offset = 0;
2509 u64 last = start + len;
2512 if (unlikely(flags))
2515 /* First find the starting scatterlist element */
2518 l = sk_msg_elem(msg, i)->length;
2520 if (start < offset + l)
2523 sk_msg_iter_var_next(i);
2524 } while (i != msg->sg.end);
2526 /* Bounds checks: start and pop must be inside message */
2527 if (start >= offset + l || last >= msg->sg.size)
2530 space = MAX_MSG_FRAGS - sk_msg_elem_used(msg);
2533 /* --------------| offset
2534 * -| start |-------- len -------|
2536 * |----- a ----|-------- pop -------|----- b ----|
2537 * |______________________________________________| length
2540 * a: region at front of scatter element to save
2541 * b: region at back of scatter element to save when length > A + pop
2542 * pop: region to pop from element, same as input 'pop' here will be
2543 * decremented below per iteration.
2545 * Two top-level cases to handle when start != offset, first B is non
2546 * zero and second B is zero corresponding to when a pop includes more
2549 * Then if B is non-zero AND there is no space allocate space and
2550 * compact A, B regions into page. If there is space shift ring to
2551 * the rigth free'ing the next element in ring to place B, leaving
2552 * A untouched except to reduce length.
2554 if (start != offset) {
2555 struct scatterlist *nsge, *sge = sk_msg_elem(msg, i);
2557 int b = sge->length - pop - a;
2559 sk_msg_iter_var_next(i);
2561 if (pop < sge->length - a) {
2564 sk_msg_shift_right(msg, i);
2565 nsge = sk_msg_elem(msg, i);
2566 get_page(sg_page(sge));
2569 b, sge->offset + pop + a);
2571 struct page *page, *orig;
2574 page = alloc_pages(__GFP_NOWARN |
2575 __GFP_COMP | GFP_ATOMIC,
2577 if (unlikely(!page))
2581 orig = sg_page(sge);
2582 from = sg_virt(sge);
2583 to = page_address(page);
2584 memcpy(to, from, a);
2585 memcpy(to + a, from + a + pop, b);
2586 sg_set_page(sge, page, a + b, 0);
2590 } else if (pop >= sge->length - a) {
2592 pop -= (sge->length - a);
2596 /* From above the current layout _must_ be as follows,
2601 * |---- pop ---|---------------- b ------------|
2602 * |____________________________________________| length
2604 * Offset and start of the current msg elem are equal because in the
2605 * previous case we handled offset != start and either consumed the
2606 * entire element and advanced to the next element OR pop == 0.
2608 * Two cases to handle here are first pop is less than the length
2609 * leaving some remainder b above. Simply adjust the element's layout
2610 * in this case. Or pop >= length of the element so that b = 0. In this
2611 * case advance to next element decrementing pop.
2614 struct scatterlist *sge = sk_msg_elem(msg, i);
2616 if (pop < sge->length) {
2622 sk_msg_shift_left(msg, i);
2624 sk_msg_iter_var_next(i);
2627 sk_mem_uncharge(msg->sk, len - pop);
2628 msg->sg.size -= (len - pop);
2629 sk_msg_compute_data_pointers(msg);
2633 static const struct bpf_func_proto bpf_msg_pop_data_proto = {
2634 .func = bpf_msg_pop_data,
2636 .ret_type = RET_INTEGER,
2637 .arg1_type = ARG_PTR_TO_CTX,
2638 .arg2_type = ARG_ANYTHING,
2639 .arg3_type = ARG_ANYTHING,
2640 .arg4_type = ARG_ANYTHING,
2643 BPF_CALL_1(bpf_get_cgroup_classid, const struct sk_buff *, skb)
2645 return task_get_classid(skb);
2648 static const struct bpf_func_proto bpf_get_cgroup_classid_proto = {
2649 .func = bpf_get_cgroup_classid,
2651 .ret_type = RET_INTEGER,
2652 .arg1_type = ARG_PTR_TO_CTX,
2655 BPF_CALL_1(bpf_get_route_realm, const struct sk_buff *, skb)
2657 return dst_tclassid(skb);
2660 static const struct bpf_func_proto bpf_get_route_realm_proto = {
2661 .func = bpf_get_route_realm,
2663 .ret_type = RET_INTEGER,
2664 .arg1_type = ARG_PTR_TO_CTX,
2667 BPF_CALL_1(bpf_get_hash_recalc, struct sk_buff *, skb)
2669 /* If skb_clear_hash() was called due to mangling, we can
2670 * trigger SW recalculation here. Later access to hash
2671 * can then use the inline skb->hash via context directly
2672 * instead of calling this helper again.
2674 return skb_get_hash(skb);
2677 static const struct bpf_func_proto bpf_get_hash_recalc_proto = {
2678 .func = bpf_get_hash_recalc,
2680 .ret_type = RET_INTEGER,
2681 .arg1_type = ARG_PTR_TO_CTX,
2684 BPF_CALL_1(bpf_set_hash_invalid, struct sk_buff *, skb)
2686 /* After all direct packet write, this can be used once for
2687 * triggering a lazy recalc on next skb_get_hash() invocation.
2689 skb_clear_hash(skb);
2693 static const struct bpf_func_proto bpf_set_hash_invalid_proto = {
2694 .func = bpf_set_hash_invalid,
2696 .ret_type = RET_INTEGER,
2697 .arg1_type = ARG_PTR_TO_CTX,
2700 BPF_CALL_2(bpf_set_hash, struct sk_buff *, skb, u32, hash)
2702 /* Set user specified hash as L4(+), so that it gets returned
2703 * on skb_get_hash() call unless BPF prog later on triggers a
2706 __skb_set_sw_hash(skb, hash, true);
2710 static const struct bpf_func_proto bpf_set_hash_proto = {
2711 .func = bpf_set_hash,
2713 .ret_type = RET_INTEGER,
2714 .arg1_type = ARG_PTR_TO_CTX,
2715 .arg2_type = ARG_ANYTHING,
2718 BPF_CALL_3(bpf_skb_vlan_push, struct sk_buff *, skb, __be16, vlan_proto,
2723 if (unlikely(vlan_proto != htons(ETH_P_8021Q) &&
2724 vlan_proto != htons(ETH_P_8021AD)))
2725 vlan_proto = htons(ETH_P_8021Q);
2727 bpf_push_mac_rcsum(skb);
2728 ret = skb_vlan_push(skb, vlan_proto, vlan_tci);
2729 bpf_pull_mac_rcsum(skb);
2731 bpf_compute_data_pointers(skb);
2735 static const struct bpf_func_proto bpf_skb_vlan_push_proto = {
2736 .func = bpf_skb_vlan_push,
2738 .ret_type = RET_INTEGER,
2739 .arg1_type = ARG_PTR_TO_CTX,
2740 .arg2_type = ARG_ANYTHING,
2741 .arg3_type = ARG_ANYTHING,
2744 BPF_CALL_1(bpf_skb_vlan_pop, struct sk_buff *, skb)
2748 bpf_push_mac_rcsum(skb);
2749 ret = skb_vlan_pop(skb);
2750 bpf_pull_mac_rcsum(skb);
2752 bpf_compute_data_pointers(skb);
2756 static const struct bpf_func_proto bpf_skb_vlan_pop_proto = {
2757 .func = bpf_skb_vlan_pop,
2759 .ret_type = RET_INTEGER,
2760 .arg1_type = ARG_PTR_TO_CTX,
2763 static int bpf_skb_generic_push(struct sk_buff *skb, u32 off, u32 len)
2765 /* Caller already did skb_cow() with len as headroom,
2766 * so no need to do it here.
2769 memmove(skb->data, skb->data + len, off);
2770 memset(skb->data + off, 0, len);
2772 /* No skb_postpush_rcsum(skb, skb->data + off, len)
2773 * needed here as it does not change the skb->csum
2774 * result for checksum complete when summing over
2780 static int bpf_skb_generic_pop(struct sk_buff *skb, u32 off, u32 len)
2782 /* skb_ensure_writable() is not needed here, as we're
2783 * already working on an uncloned skb.
2785 if (unlikely(!pskb_may_pull(skb, off + len)))
2788 skb_postpull_rcsum(skb, skb->data + off, len);
2789 memmove(skb->data + len, skb->data, off);
2790 __skb_pull(skb, len);
2795 static int bpf_skb_net_hdr_push(struct sk_buff *skb, u32 off, u32 len)
2797 bool trans_same = skb->transport_header == skb->network_header;
2800 /* There's no need for __skb_push()/__skb_pull() pair to
2801 * get to the start of the mac header as we're guaranteed
2802 * to always start from here under eBPF.
2804 ret = bpf_skb_generic_push(skb, off, len);
2806 skb->mac_header -= len;
2807 skb->network_header -= len;
2809 skb->transport_header = skb->network_header;
2815 static int bpf_skb_net_hdr_pop(struct sk_buff *skb, u32 off, u32 len)
2817 bool trans_same = skb->transport_header == skb->network_header;
2820 /* Same here, __skb_push()/__skb_pull() pair not needed. */
2821 ret = bpf_skb_generic_pop(skb, off, len);
2823 skb->mac_header += len;
2824 skb->network_header += len;
2826 skb->transport_header = skb->network_header;
2832 static int bpf_skb_proto_4_to_6(struct sk_buff *skb)
2834 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
2835 u32 off = skb_mac_header_len(skb);
2838 if (skb_is_gso(skb) && !skb_is_gso_tcp(skb))
2841 ret = skb_cow(skb, len_diff);
2842 if (unlikely(ret < 0))
2845 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
2846 if (unlikely(ret < 0))
2849 if (skb_is_gso(skb)) {
2850 struct skb_shared_info *shinfo = skb_shinfo(skb);
2852 /* SKB_GSO_TCPV4 needs to be changed into
2855 if (shinfo->gso_type & SKB_GSO_TCPV4) {
2856 shinfo->gso_type &= ~SKB_GSO_TCPV4;
2857 shinfo->gso_type |= SKB_GSO_TCPV6;
2860 /* Due to IPv6 header, MSS needs to be downgraded. */
2861 skb_decrease_gso_size(shinfo, len_diff);
2862 /* Header must be checked, and gso_segs recomputed. */
2863 shinfo->gso_type |= SKB_GSO_DODGY;
2864 shinfo->gso_segs = 0;
2867 skb->protocol = htons(ETH_P_IPV6);
2868 skb_clear_hash(skb);
2873 static int bpf_skb_proto_6_to_4(struct sk_buff *skb)
2875 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
2876 u32 off = skb_mac_header_len(skb);
2879 if (skb_is_gso(skb) && !skb_is_gso_tcp(skb))
2882 ret = skb_unclone(skb, GFP_ATOMIC);
2883 if (unlikely(ret < 0))
2886 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
2887 if (unlikely(ret < 0))
2890 if (skb_is_gso(skb)) {
2891 struct skb_shared_info *shinfo = skb_shinfo(skb);
2893 /* SKB_GSO_TCPV6 needs to be changed into
2896 if (shinfo->gso_type & SKB_GSO_TCPV6) {
2897 shinfo->gso_type &= ~SKB_GSO_TCPV6;
2898 shinfo->gso_type |= SKB_GSO_TCPV4;
2901 /* Due to IPv4 header, MSS can be upgraded. */
2902 skb_increase_gso_size(shinfo, len_diff);
2903 /* Header must be checked, and gso_segs recomputed. */
2904 shinfo->gso_type |= SKB_GSO_DODGY;
2905 shinfo->gso_segs = 0;
2908 skb->protocol = htons(ETH_P_IP);
2909 skb_clear_hash(skb);
2914 static int bpf_skb_proto_xlat(struct sk_buff *skb, __be16 to_proto)
2916 __be16 from_proto = skb->protocol;
2918 if (from_proto == htons(ETH_P_IP) &&
2919 to_proto == htons(ETH_P_IPV6))
2920 return bpf_skb_proto_4_to_6(skb);
2922 if (from_proto == htons(ETH_P_IPV6) &&
2923 to_proto == htons(ETH_P_IP))
2924 return bpf_skb_proto_6_to_4(skb);
2929 BPF_CALL_3(bpf_skb_change_proto, struct sk_buff *, skb, __be16, proto,
2934 if (unlikely(flags))
2937 /* General idea is that this helper does the basic groundwork
2938 * needed for changing the protocol, and eBPF program fills the
2939 * rest through bpf_skb_store_bytes(), bpf_lX_csum_replace()
2940 * and other helpers, rather than passing a raw buffer here.
2942 * The rationale is to keep this minimal and without a need to
2943 * deal with raw packet data. F.e. even if we would pass buffers
2944 * here, the program still needs to call the bpf_lX_csum_replace()
2945 * helpers anyway. Plus, this way we keep also separation of
2946 * concerns, since f.e. bpf_skb_store_bytes() should only take
2949 * Currently, additional options and extension header space are
2950 * not supported, but flags register is reserved so we can adapt
2951 * that. For offloads, we mark packet as dodgy, so that headers
2952 * need to be verified first.
2954 ret = bpf_skb_proto_xlat(skb, proto);
2955 bpf_compute_data_pointers(skb);
2959 static const struct bpf_func_proto bpf_skb_change_proto_proto = {
2960 .func = bpf_skb_change_proto,
2962 .ret_type = RET_INTEGER,
2963 .arg1_type = ARG_PTR_TO_CTX,
2964 .arg2_type = ARG_ANYTHING,
2965 .arg3_type = ARG_ANYTHING,
2968 BPF_CALL_2(bpf_skb_change_type, struct sk_buff *, skb, u32, pkt_type)
2970 /* We only allow a restricted subset to be changed for now. */
2971 if (unlikely(!skb_pkt_type_ok(skb->pkt_type) ||
2972 !skb_pkt_type_ok(pkt_type)))
2975 skb->pkt_type = pkt_type;
2979 static const struct bpf_func_proto bpf_skb_change_type_proto = {
2980 .func = bpf_skb_change_type,
2982 .ret_type = RET_INTEGER,
2983 .arg1_type = ARG_PTR_TO_CTX,
2984 .arg2_type = ARG_ANYTHING,
2987 static u32 bpf_skb_net_base_len(const struct sk_buff *skb)
2989 switch (skb->protocol) {
2990 case htons(ETH_P_IP):
2991 return sizeof(struct iphdr);
2992 case htons(ETH_P_IPV6):
2993 return sizeof(struct ipv6hdr);
2999 #define BPF_F_ADJ_ROOM_ENCAP_L3_MASK (BPF_F_ADJ_ROOM_ENCAP_L3_IPV4 | \
3000 BPF_F_ADJ_ROOM_ENCAP_L3_IPV6)
3002 #define BPF_F_ADJ_ROOM_MASK (BPF_F_ADJ_ROOM_FIXED_GSO | \
3003 BPF_F_ADJ_ROOM_ENCAP_L3_MASK | \
3004 BPF_F_ADJ_ROOM_ENCAP_L4_GRE | \
3005 BPF_F_ADJ_ROOM_ENCAP_L4_UDP | \
3006 BPF_F_ADJ_ROOM_ENCAP_L2( \
3007 BPF_ADJ_ROOM_ENCAP_L2_MASK))
3009 static int bpf_skb_net_grow(struct sk_buff *skb, u32 off, u32 len_diff,
3012 u8 inner_mac_len = flags >> BPF_ADJ_ROOM_ENCAP_L2_SHIFT;
3013 bool encap = flags & BPF_F_ADJ_ROOM_ENCAP_L3_MASK;
3014 u16 mac_len = 0, inner_net = 0, inner_trans = 0;
3015 unsigned int gso_type = SKB_GSO_DODGY;
3018 if (skb_is_gso(skb) && !skb_is_gso_tcp(skb)) {
3019 /* udp gso_size delineates datagrams, only allow if fixed */
3020 if (!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) ||
3021 !(flags & BPF_F_ADJ_ROOM_FIXED_GSO))
3025 ret = skb_cow_head(skb, len_diff);
3026 if (unlikely(ret < 0))
3030 if (skb->protocol != htons(ETH_P_IP) &&
3031 skb->protocol != htons(ETH_P_IPV6))
3034 if (flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV4 &&
3035 flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV6)
3038 if (flags & BPF_F_ADJ_ROOM_ENCAP_L4_GRE &&
3039 flags & BPF_F_ADJ_ROOM_ENCAP_L4_UDP)
3042 if (skb->encapsulation)
3045 mac_len = skb->network_header - skb->mac_header;
3046 inner_net = skb->network_header;
3047 if (inner_mac_len > len_diff)
3049 inner_trans = skb->transport_header;
3052 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
3053 if (unlikely(ret < 0))
3057 skb->inner_mac_header = inner_net - inner_mac_len;
3058 skb->inner_network_header = inner_net;
3059 skb->inner_transport_header = inner_trans;
3060 skb_set_inner_protocol(skb, skb->protocol);
3062 skb->encapsulation = 1;
3063 skb_set_network_header(skb, mac_len);
3065 if (flags & BPF_F_ADJ_ROOM_ENCAP_L4_UDP)
3066 gso_type |= SKB_GSO_UDP_TUNNEL;
3067 else if (flags & BPF_F_ADJ_ROOM_ENCAP_L4_GRE)
3068 gso_type |= SKB_GSO_GRE;
3069 else if (flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV6)
3070 gso_type |= SKB_GSO_IPXIP6;
3071 else if (flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV4)
3072 gso_type |= SKB_GSO_IPXIP4;
3074 if (flags & BPF_F_ADJ_ROOM_ENCAP_L4_GRE ||
3075 flags & BPF_F_ADJ_ROOM_ENCAP_L4_UDP) {
3076 int nh_len = flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV6 ?
3077 sizeof(struct ipv6hdr) :
3078 sizeof(struct iphdr);
3080 skb_set_transport_header(skb, mac_len + nh_len);
3083 /* Match skb->protocol to new outer l3 protocol */
3084 if (skb->protocol == htons(ETH_P_IP) &&
3085 flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV6)
3086 skb->protocol = htons(ETH_P_IPV6);
3087 else if (skb->protocol == htons(ETH_P_IPV6) &&
3088 flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV4)
3089 skb->protocol = htons(ETH_P_IP);
3092 if (skb_is_gso(skb)) {
3093 struct skb_shared_info *shinfo = skb_shinfo(skb);
3095 /* Due to header grow, MSS needs to be downgraded. */
3096 if (!(flags & BPF_F_ADJ_ROOM_FIXED_GSO))
3097 skb_decrease_gso_size(shinfo, len_diff);
3099 /* Header must be checked, and gso_segs recomputed. */
3100 shinfo->gso_type |= gso_type;
3101 shinfo->gso_segs = 0;
3107 static int bpf_skb_net_shrink(struct sk_buff *skb, u32 off, u32 len_diff,
3112 if (flags & ~BPF_F_ADJ_ROOM_FIXED_GSO)
3115 if (skb_is_gso(skb) && !skb_is_gso_tcp(skb)) {
3116 /* udp gso_size delineates datagrams, only allow if fixed */
3117 if (!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) ||
3118 !(flags & BPF_F_ADJ_ROOM_FIXED_GSO))
3122 ret = skb_unclone(skb, GFP_ATOMIC);
3123 if (unlikely(ret < 0))
3126 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
3127 if (unlikely(ret < 0))
3130 if (skb_is_gso(skb)) {
3131 struct skb_shared_info *shinfo = skb_shinfo(skb);
3133 /* Due to header shrink, MSS can be upgraded. */
3134 if (!(flags & BPF_F_ADJ_ROOM_FIXED_GSO))
3135 skb_increase_gso_size(shinfo, len_diff);
3137 /* Header must be checked, and gso_segs recomputed. */
3138 shinfo->gso_type |= SKB_GSO_DODGY;
3139 shinfo->gso_segs = 0;
3145 static u32 __bpf_skb_max_len(const struct sk_buff *skb)
3147 return skb->dev ? skb->dev->mtu + skb->dev->hard_header_len :
3151 BPF_CALL_4(bpf_skb_adjust_room, struct sk_buff *, skb, s32, len_diff,
3152 u32, mode, u64, flags)
3154 u32 len_cur, len_diff_abs = abs(len_diff);
3155 u32 len_min = bpf_skb_net_base_len(skb);
3156 u32 len_max = __bpf_skb_max_len(skb);
3157 __be16 proto = skb->protocol;
3158 bool shrink = len_diff < 0;
3162 if (unlikely(flags & ~BPF_F_ADJ_ROOM_MASK))
3164 if (unlikely(len_diff_abs > 0xfffU))
3166 if (unlikely(proto != htons(ETH_P_IP) &&
3167 proto != htons(ETH_P_IPV6)))
3170 off = skb_mac_header_len(skb);
3172 case BPF_ADJ_ROOM_NET:
3173 off += bpf_skb_net_base_len(skb);
3175 case BPF_ADJ_ROOM_MAC:
3181 len_cur = skb->len - skb_network_offset(skb);
3182 if ((shrink && (len_diff_abs >= len_cur ||
3183 len_cur - len_diff_abs < len_min)) ||
3184 (!shrink && (skb->len + len_diff_abs > len_max &&
3188 ret = shrink ? bpf_skb_net_shrink(skb, off, len_diff_abs, flags) :
3189 bpf_skb_net_grow(skb, off, len_diff_abs, flags);
3191 bpf_compute_data_pointers(skb);
3195 static const struct bpf_func_proto bpf_skb_adjust_room_proto = {
3196 .func = bpf_skb_adjust_room,
3198 .ret_type = RET_INTEGER,
3199 .arg1_type = ARG_PTR_TO_CTX,
3200 .arg2_type = ARG_ANYTHING,
3201 .arg3_type = ARG_ANYTHING,
3202 .arg4_type = ARG_ANYTHING,
3205 static u32 __bpf_skb_min_len(const struct sk_buff *skb)
3207 u32 min_len = skb_network_offset(skb);
3209 if (skb_transport_header_was_set(skb))
3210 min_len = skb_transport_offset(skb);
3211 if (skb->ip_summed == CHECKSUM_PARTIAL)
3212 min_len = skb_checksum_start_offset(skb) +
3213 skb->csum_offset + sizeof(__sum16);
3217 static int bpf_skb_grow_rcsum(struct sk_buff *skb, unsigned int new_len)
3219 unsigned int old_len = skb->len;
3222 ret = __skb_grow_rcsum(skb, new_len);
3224 memset(skb->data + old_len, 0, new_len - old_len);
3228 static int bpf_skb_trim_rcsum(struct sk_buff *skb, unsigned int new_len)
3230 return __skb_trim_rcsum(skb, new_len);
3233 static inline int __bpf_skb_change_tail(struct sk_buff *skb, u32 new_len,
3236 u32 max_len = __bpf_skb_max_len(skb);
3237 u32 min_len = __bpf_skb_min_len(skb);
3240 if (unlikely(flags || new_len > max_len || new_len < min_len))
3242 if (skb->encapsulation)
3245 /* The basic idea of this helper is that it's performing the
3246 * needed work to either grow or trim an skb, and eBPF program
3247 * rewrites the rest via helpers like bpf_skb_store_bytes(),
3248 * bpf_lX_csum_replace() and others rather than passing a raw
3249 * buffer here. This one is a slow path helper and intended
3250 * for replies with control messages.
3252 * Like in bpf_skb_change_proto(), we want to keep this rather
3253 * minimal and without protocol specifics so that we are able
3254 * to separate concerns as in bpf_skb_store_bytes() should only
3255 * be the one responsible for writing buffers.
3257 * It's really expected to be a slow path operation here for
3258 * control message replies, so we're implicitly linearizing,
3259 * uncloning and drop offloads from the skb by this.
3261 ret = __bpf_try_make_writable(skb, skb->len);
3263 if (new_len > skb->len)
3264 ret = bpf_skb_grow_rcsum(skb, new_len);
3265 else if (new_len < skb->len)
3266 ret = bpf_skb_trim_rcsum(skb, new_len);
3267 if (!ret && skb_is_gso(skb))
3273 BPF_CALL_3(bpf_skb_change_tail, struct sk_buff *, skb, u32, new_len,
3276 int ret = __bpf_skb_change_tail(skb, new_len, flags);
3278 bpf_compute_data_pointers(skb);
3282 static const struct bpf_func_proto bpf_skb_change_tail_proto = {
3283 .func = bpf_skb_change_tail,
3285 .ret_type = RET_INTEGER,
3286 .arg1_type = ARG_PTR_TO_CTX,
3287 .arg2_type = ARG_ANYTHING,
3288 .arg3_type = ARG_ANYTHING,
3291 BPF_CALL_3(sk_skb_change_tail, struct sk_buff *, skb, u32, new_len,
3294 int ret = __bpf_skb_change_tail(skb, new_len, flags);
3296 bpf_compute_data_end_sk_skb(skb);
3300 static const struct bpf_func_proto sk_skb_change_tail_proto = {
3301 .func = sk_skb_change_tail,
3303 .ret_type = RET_INTEGER,
3304 .arg1_type = ARG_PTR_TO_CTX,
3305 .arg2_type = ARG_ANYTHING,
3306 .arg3_type = ARG_ANYTHING,
3309 static inline int __bpf_skb_change_head(struct sk_buff *skb, u32 head_room,
3312 u32 max_len = __bpf_skb_max_len(skb);
3313 u32 new_len = skb->len + head_room;
3316 if (unlikely(flags || (!skb_is_gso(skb) && new_len > max_len) ||
3317 new_len < skb->len))
3320 ret = skb_cow(skb, head_room);
3322 /* Idea for this helper is that we currently only
3323 * allow to expand on mac header. This means that
3324 * skb->protocol network header, etc, stay as is.
3325 * Compared to bpf_skb_change_tail(), we're more
3326 * flexible due to not needing to linearize or
3327 * reset GSO. Intention for this helper is to be
3328 * used by an L3 skb that needs to push mac header
3329 * for redirection into L2 device.
3331 __skb_push(skb, head_room);
3332 memset(skb->data, 0, head_room);
3333 skb_reset_mac_header(skb);
3339 BPF_CALL_3(bpf_skb_change_head, struct sk_buff *, skb, u32, head_room,
3342 int ret = __bpf_skb_change_head(skb, head_room, flags);
3344 bpf_compute_data_pointers(skb);
3348 static const struct bpf_func_proto bpf_skb_change_head_proto = {
3349 .func = bpf_skb_change_head,
3351 .ret_type = RET_INTEGER,
3352 .arg1_type = ARG_PTR_TO_CTX,
3353 .arg2_type = ARG_ANYTHING,
3354 .arg3_type = ARG_ANYTHING,
3357 BPF_CALL_3(sk_skb_change_head, struct sk_buff *, skb, u32, head_room,
3360 int ret = __bpf_skb_change_head(skb, head_room, flags);
3362 bpf_compute_data_end_sk_skb(skb);
3366 static const struct bpf_func_proto sk_skb_change_head_proto = {
3367 .func = sk_skb_change_head,
3369 .ret_type = RET_INTEGER,
3370 .arg1_type = ARG_PTR_TO_CTX,
3371 .arg2_type = ARG_ANYTHING,
3372 .arg3_type = ARG_ANYTHING,
3374 static unsigned long xdp_get_metalen(const struct xdp_buff *xdp)
3376 return xdp_data_meta_unsupported(xdp) ? 0 :
3377 xdp->data - xdp->data_meta;
3380 BPF_CALL_2(bpf_xdp_adjust_head, struct xdp_buff *, xdp, int, offset)
3382 void *xdp_frame_end = xdp->data_hard_start + sizeof(struct xdp_frame);
3383 unsigned long metalen = xdp_get_metalen(xdp);
3384 void *data_start = xdp_frame_end + metalen;
3385 void *data = xdp->data + offset;
3387 if (unlikely(data < data_start ||
3388 data > xdp->data_end - ETH_HLEN))
3392 memmove(xdp->data_meta + offset,
3393 xdp->data_meta, metalen);
3394 xdp->data_meta += offset;
3400 static const struct bpf_func_proto bpf_xdp_adjust_head_proto = {
3401 .func = bpf_xdp_adjust_head,
3403 .ret_type = RET_INTEGER,
3404 .arg1_type = ARG_PTR_TO_CTX,
3405 .arg2_type = ARG_ANYTHING,
3408 BPF_CALL_2(bpf_xdp_adjust_tail, struct xdp_buff *, xdp, int, offset)
3410 void *data_end = xdp->data_end + offset;
3412 /* only shrinking is allowed for now. */
3413 if (unlikely(offset >= 0))
3416 if (unlikely(data_end < xdp->data + ETH_HLEN))
3419 xdp->data_end = data_end;
3424 static const struct bpf_func_proto bpf_xdp_adjust_tail_proto = {
3425 .func = bpf_xdp_adjust_tail,
3427 .ret_type = RET_INTEGER,
3428 .arg1_type = ARG_PTR_TO_CTX,
3429 .arg2_type = ARG_ANYTHING,
3432 BPF_CALL_2(bpf_xdp_adjust_meta, struct xdp_buff *, xdp, int, offset)
3434 void *xdp_frame_end = xdp->data_hard_start + sizeof(struct xdp_frame);
3435 void *meta = xdp->data_meta + offset;
3436 unsigned long metalen = xdp->data - meta;
3438 if (xdp_data_meta_unsupported(xdp))
3440 if (unlikely(meta < xdp_frame_end ||
3443 if (unlikely((metalen & (sizeof(__u32) - 1)) ||
3447 xdp->data_meta = meta;
3452 static const struct bpf_func_proto bpf_xdp_adjust_meta_proto = {
3453 .func = bpf_xdp_adjust_meta,
3455 .ret_type = RET_INTEGER,
3456 .arg1_type = ARG_PTR_TO_CTX,
3457 .arg2_type = ARG_ANYTHING,
3460 static int __bpf_tx_xdp(struct net_device *dev,
3461 struct bpf_map *map,
3462 struct xdp_buff *xdp,
3465 struct xdp_frame *xdpf;
3468 if (!dev->netdev_ops->ndo_xdp_xmit) {
3472 err = xdp_ok_fwd_dev(dev, xdp->data_end - xdp->data);
3476 xdpf = convert_to_xdp_frame(xdp);
3477 if (unlikely(!xdpf))
3480 sent = dev->netdev_ops->ndo_xdp_xmit(dev, 1, &xdpf, XDP_XMIT_FLUSH);
3487 xdp_do_redirect_slow(struct net_device *dev, struct xdp_buff *xdp,
3488 struct bpf_prog *xdp_prog, struct bpf_redirect_info *ri)
3490 struct net_device *fwd;
3491 u32 index = ri->tgt_index;
3494 fwd = dev_get_by_index_rcu(dev_net(dev), index);
3496 if (unlikely(!fwd)) {
3501 err = __bpf_tx_xdp(fwd, NULL, xdp, 0);
3505 _trace_xdp_redirect(dev, xdp_prog, index);
3508 _trace_xdp_redirect_err(dev, xdp_prog, index, err);
3512 static int __bpf_tx_xdp_map(struct net_device *dev_rx, void *fwd,
3513 struct bpf_map *map,
3514 struct xdp_buff *xdp,
3519 switch (map->map_type) {
3520 case BPF_MAP_TYPE_DEVMAP:
3521 case BPF_MAP_TYPE_DEVMAP_HASH: {
3522 struct bpf_dtab_netdev *dst = fwd;
3524 err = dev_map_enqueue(dst, xdp, dev_rx);
3529 case BPF_MAP_TYPE_CPUMAP: {
3530 struct bpf_cpu_map_entry *rcpu = fwd;
3532 err = cpu_map_enqueue(rcpu, xdp, dev_rx);
3537 case BPF_MAP_TYPE_XSKMAP: {
3538 struct xdp_sock *xs = fwd;
3540 err = __xsk_map_redirect(map, xdp, xs);
3549 void xdp_do_flush_map(void)
3551 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
3552 struct bpf_map *map = ri->map_to_flush;
3554 ri->map_to_flush = NULL;
3556 switch (map->map_type) {
3557 case BPF_MAP_TYPE_DEVMAP:
3558 case BPF_MAP_TYPE_DEVMAP_HASH:
3559 __dev_map_flush(map);
3561 case BPF_MAP_TYPE_CPUMAP:
3562 __cpu_map_flush(map);
3564 case BPF_MAP_TYPE_XSKMAP:
3565 __xsk_map_flush(map);
3572 EXPORT_SYMBOL_GPL(xdp_do_flush_map);
3574 static inline void *__xdp_map_lookup_elem(struct bpf_map *map, u32 index)
3576 switch (map->map_type) {
3577 case BPF_MAP_TYPE_DEVMAP:
3578 return __dev_map_lookup_elem(map, index);
3579 case BPF_MAP_TYPE_DEVMAP_HASH:
3580 return __dev_map_hash_lookup_elem(map, index);
3581 case BPF_MAP_TYPE_CPUMAP:
3582 return __cpu_map_lookup_elem(map, index);
3583 case BPF_MAP_TYPE_XSKMAP:
3584 return __xsk_map_lookup_elem(map, index);
3590 void bpf_clear_redirect_map(struct bpf_map *map)
3592 struct bpf_redirect_info *ri;
3595 for_each_possible_cpu(cpu) {
3596 ri = per_cpu_ptr(&bpf_redirect_info, cpu);
3597 /* Avoid polluting remote cacheline due to writes if
3598 * not needed. Once we pass this test, we need the
3599 * cmpxchg() to make sure it hasn't been changed in
3600 * the meantime by remote CPU.
3602 if (unlikely(READ_ONCE(ri->map) == map))
3603 cmpxchg(&ri->map, map, NULL);
3607 static int xdp_do_redirect_map(struct net_device *dev, struct xdp_buff *xdp,
3608 struct bpf_prog *xdp_prog, struct bpf_map *map,
3609 struct bpf_redirect_info *ri)
3611 u32 index = ri->tgt_index;
3612 void *fwd = ri->tgt_value;
3616 ri->tgt_value = NULL;
3617 WRITE_ONCE(ri->map, NULL);
3619 if (ri->map_to_flush && unlikely(ri->map_to_flush != map))
3622 err = __bpf_tx_xdp_map(dev, fwd, map, xdp, index);
3626 ri->map_to_flush = map;
3627 _trace_xdp_redirect_map(dev, xdp_prog, fwd, map, index);
3630 _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map, index, err);
3634 int xdp_do_redirect(struct net_device *dev, struct xdp_buff *xdp,
3635 struct bpf_prog *xdp_prog)
3637 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
3638 struct bpf_map *map = READ_ONCE(ri->map);
3641 return xdp_do_redirect_map(dev, xdp, xdp_prog, map, ri);
3643 return xdp_do_redirect_slow(dev, xdp, xdp_prog, ri);
3645 EXPORT_SYMBOL_GPL(xdp_do_redirect);
3647 static int xdp_do_generic_redirect_map(struct net_device *dev,
3648 struct sk_buff *skb,
3649 struct xdp_buff *xdp,
3650 struct bpf_prog *xdp_prog,
3651 struct bpf_map *map)
3653 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
3654 u32 index = ri->tgt_index;
3655 void *fwd = ri->tgt_value;
3659 ri->tgt_value = NULL;
3660 WRITE_ONCE(ri->map, NULL);
3662 if (map->map_type == BPF_MAP_TYPE_DEVMAP ||
3663 map->map_type == BPF_MAP_TYPE_DEVMAP_HASH) {
3664 struct bpf_dtab_netdev *dst = fwd;
3666 err = dev_map_generic_redirect(dst, skb, xdp_prog);
3669 } else if (map->map_type == BPF_MAP_TYPE_XSKMAP) {
3670 struct xdp_sock *xs = fwd;
3672 err = xsk_generic_rcv(xs, xdp);
3677 /* TODO: Handle BPF_MAP_TYPE_CPUMAP */
3682 _trace_xdp_redirect_map(dev, xdp_prog, fwd, map, index);
3685 _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map, index, err);
3689 int xdp_do_generic_redirect(struct net_device *dev, struct sk_buff *skb,
3690 struct xdp_buff *xdp, struct bpf_prog *xdp_prog)
3692 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
3693 struct bpf_map *map = READ_ONCE(ri->map);
3694 u32 index = ri->tgt_index;
3695 struct net_device *fwd;
3699 return xdp_do_generic_redirect_map(dev, skb, xdp, xdp_prog,
3702 fwd = dev_get_by_index_rcu(dev_net(dev), index);
3703 if (unlikely(!fwd)) {
3708 err = xdp_ok_fwd_dev(fwd, skb->len);
3713 _trace_xdp_redirect(dev, xdp_prog, index);
3714 generic_xdp_tx(skb, xdp_prog);
3717 _trace_xdp_redirect_err(dev, xdp_prog, index, err);
3720 EXPORT_SYMBOL_GPL(xdp_do_generic_redirect);
3722 BPF_CALL_2(bpf_xdp_redirect, u32, ifindex, u64, flags)
3724 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
3726 if (unlikely(flags))
3730 ri->tgt_index = ifindex;
3731 ri->tgt_value = NULL;
3732 WRITE_ONCE(ri->map, NULL);
3734 return XDP_REDIRECT;
3737 static const struct bpf_func_proto bpf_xdp_redirect_proto = {
3738 .func = bpf_xdp_redirect,
3740 .ret_type = RET_INTEGER,
3741 .arg1_type = ARG_ANYTHING,
3742 .arg2_type = ARG_ANYTHING,
3745 BPF_CALL_3(bpf_xdp_redirect_map, struct bpf_map *, map, u32, ifindex,
3748 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
3750 /* Lower bits of the flags are used as return code on lookup failure */
3751 if (unlikely(flags > XDP_TX))
3754 ri->tgt_value = __xdp_map_lookup_elem(map, ifindex);
3755 if (unlikely(!ri->tgt_value)) {
3756 /* If the lookup fails we want to clear out the state in the
3757 * redirect_info struct completely, so that if an eBPF program
3758 * performs multiple lookups, the last one always takes
3761 WRITE_ONCE(ri->map, NULL);
3766 ri->tgt_index = ifindex;
3767 WRITE_ONCE(ri->map, map);
3769 return XDP_REDIRECT;
3772 static const struct bpf_func_proto bpf_xdp_redirect_map_proto = {
3773 .func = bpf_xdp_redirect_map,
3775 .ret_type = RET_INTEGER,
3776 .arg1_type = ARG_CONST_MAP_PTR,
3777 .arg2_type = ARG_ANYTHING,
3778 .arg3_type = ARG_ANYTHING,
3781 static unsigned long bpf_skb_copy(void *dst_buff, const void *skb,
3782 unsigned long off, unsigned long len)
3784 void *ptr = skb_header_pointer(skb, off, len, dst_buff);
3788 if (ptr != dst_buff)
3789 memcpy(dst_buff, ptr, len);
3794 BPF_CALL_5(bpf_skb_event_output, struct sk_buff *, skb, struct bpf_map *, map,
3795 u64, flags, void *, meta, u64, meta_size)
3797 u64 skb_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
3799 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
3801 if (unlikely(skb_size > skb->len))
3804 return bpf_event_output(map, flags, meta, meta_size, skb, skb_size,
3808 static const struct bpf_func_proto bpf_skb_event_output_proto = {
3809 .func = bpf_skb_event_output,
3811 .ret_type = RET_INTEGER,
3812 .arg1_type = ARG_PTR_TO_CTX,
3813 .arg2_type = ARG_CONST_MAP_PTR,
3814 .arg3_type = ARG_ANYTHING,
3815 .arg4_type = ARG_PTR_TO_MEM,
3816 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
3819 static unsigned short bpf_tunnel_key_af(u64 flags)
3821 return flags & BPF_F_TUNINFO_IPV6 ? AF_INET6 : AF_INET;
3824 BPF_CALL_4(bpf_skb_get_tunnel_key, struct sk_buff *, skb, struct bpf_tunnel_key *, to,
3825 u32, size, u64, flags)
3827 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
3828 u8 compat[sizeof(struct bpf_tunnel_key)];
3832 if (unlikely(!info || (flags & ~(BPF_F_TUNINFO_IPV6)))) {
3836 if (ip_tunnel_info_af(info) != bpf_tunnel_key_af(flags)) {
3840 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
3843 case offsetof(struct bpf_tunnel_key, tunnel_label):
3844 case offsetof(struct bpf_tunnel_key, tunnel_ext):
3846 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
3847 /* Fixup deprecated structure layouts here, so we have
3848 * a common path later on.
3850 if (ip_tunnel_info_af(info) != AF_INET)
3853 to = (struct bpf_tunnel_key *)compat;
3860 to->tunnel_id = be64_to_cpu(info->key.tun_id);
3861 to->tunnel_tos = info->key.tos;
3862 to->tunnel_ttl = info->key.ttl;
3865 if (flags & BPF_F_TUNINFO_IPV6) {
3866 memcpy(to->remote_ipv6, &info->key.u.ipv6.src,
3867 sizeof(to->remote_ipv6));
3868 to->tunnel_label = be32_to_cpu(info->key.label);
3870 to->remote_ipv4 = be32_to_cpu(info->key.u.ipv4.src);
3871 memset(&to->remote_ipv6[1], 0, sizeof(__u32) * 3);
3872 to->tunnel_label = 0;
3875 if (unlikely(size != sizeof(struct bpf_tunnel_key)))
3876 memcpy(to_orig, to, size);
3880 memset(to_orig, 0, size);
3884 static const struct bpf_func_proto bpf_skb_get_tunnel_key_proto = {
3885 .func = bpf_skb_get_tunnel_key,
3887 .ret_type = RET_INTEGER,
3888 .arg1_type = ARG_PTR_TO_CTX,
3889 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
3890 .arg3_type = ARG_CONST_SIZE,
3891 .arg4_type = ARG_ANYTHING,
3894 BPF_CALL_3(bpf_skb_get_tunnel_opt, struct sk_buff *, skb, u8 *, to, u32, size)
3896 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
3899 if (unlikely(!info ||
3900 !(info->key.tun_flags & TUNNEL_OPTIONS_PRESENT))) {
3904 if (unlikely(size < info->options_len)) {
3909 ip_tunnel_info_opts_get(to, info);
3910 if (size > info->options_len)
3911 memset(to + info->options_len, 0, size - info->options_len);
3913 return info->options_len;
3915 memset(to, 0, size);
3919 static const struct bpf_func_proto bpf_skb_get_tunnel_opt_proto = {
3920 .func = bpf_skb_get_tunnel_opt,
3922 .ret_type = RET_INTEGER,
3923 .arg1_type = ARG_PTR_TO_CTX,
3924 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
3925 .arg3_type = ARG_CONST_SIZE,
3928 static struct metadata_dst __percpu *md_dst;
3930 BPF_CALL_4(bpf_skb_set_tunnel_key, struct sk_buff *, skb,
3931 const struct bpf_tunnel_key *, from, u32, size, u64, flags)
3933 struct metadata_dst *md = this_cpu_ptr(md_dst);
3934 u8 compat[sizeof(struct bpf_tunnel_key)];
3935 struct ip_tunnel_info *info;
3937 if (unlikely(flags & ~(BPF_F_TUNINFO_IPV6 | BPF_F_ZERO_CSUM_TX |
3938 BPF_F_DONT_FRAGMENT | BPF_F_SEQ_NUMBER)))
3940 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
3942 case offsetof(struct bpf_tunnel_key, tunnel_label):
3943 case offsetof(struct bpf_tunnel_key, tunnel_ext):
3944 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
3945 /* Fixup deprecated structure layouts here, so we have
3946 * a common path later on.
3948 memcpy(compat, from, size);
3949 memset(compat + size, 0, sizeof(compat) - size);
3950 from = (const struct bpf_tunnel_key *) compat;
3956 if (unlikely((!(flags & BPF_F_TUNINFO_IPV6) && from->tunnel_label) ||
3961 dst_hold((struct dst_entry *) md);
3962 skb_dst_set(skb, (struct dst_entry *) md);
3964 info = &md->u.tun_info;
3965 memset(info, 0, sizeof(*info));
3966 info->mode = IP_TUNNEL_INFO_TX;
3968 info->key.tun_flags = TUNNEL_KEY | TUNNEL_CSUM | TUNNEL_NOCACHE;
3969 if (flags & BPF_F_DONT_FRAGMENT)
3970 info->key.tun_flags |= TUNNEL_DONT_FRAGMENT;
3971 if (flags & BPF_F_ZERO_CSUM_TX)
3972 info->key.tun_flags &= ~TUNNEL_CSUM;
3973 if (flags & BPF_F_SEQ_NUMBER)
3974 info->key.tun_flags |= TUNNEL_SEQ;
3976 info->key.tun_id = cpu_to_be64(from->tunnel_id);
3977 info->key.tos = from->tunnel_tos;
3978 info->key.ttl = from->tunnel_ttl;
3980 if (flags & BPF_F_TUNINFO_IPV6) {
3981 info->mode |= IP_TUNNEL_INFO_IPV6;
3982 memcpy(&info->key.u.ipv6.dst, from->remote_ipv6,
3983 sizeof(from->remote_ipv6));
3984 info->key.label = cpu_to_be32(from->tunnel_label) &
3985 IPV6_FLOWLABEL_MASK;
3987 info->key.u.ipv4.dst = cpu_to_be32(from->remote_ipv4);
3993 static const struct bpf_func_proto bpf_skb_set_tunnel_key_proto = {
3994 .func = bpf_skb_set_tunnel_key,
3996 .ret_type = RET_INTEGER,
3997 .arg1_type = ARG_PTR_TO_CTX,
3998 .arg2_type = ARG_PTR_TO_MEM,
3999 .arg3_type = ARG_CONST_SIZE,
4000 .arg4_type = ARG_ANYTHING,
4003 BPF_CALL_3(bpf_skb_set_tunnel_opt, struct sk_buff *, skb,
4004 const u8 *, from, u32, size)
4006 struct ip_tunnel_info *info = skb_tunnel_info(skb);
4007 const struct metadata_dst *md = this_cpu_ptr(md_dst);
4009 if (unlikely(info != &md->u.tun_info || (size & (sizeof(u32) - 1))))
4011 if (unlikely(size > IP_TUNNEL_OPTS_MAX))
4014 ip_tunnel_info_opts_set(info, from, size, TUNNEL_OPTIONS_PRESENT);
4019 static const struct bpf_func_proto bpf_skb_set_tunnel_opt_proto = {
4020 .func = bpf_skb_set_tunnel_opt,
4022 .ret_type = RET_INTEGER,
4023 .arg1_type = ARG_PTR_TO_CTX,
4024 .arg2_type = ARG_PTR_TO_MEM,
4025 .arg3_type = ARG_CONST_SIZE,
4028 static const struct bpf_func_proto *
4029 bpf_get_skb_set_tunnel_proto(enum bpf_func_id which)
4032 struct metadata_dst __percpu *tmp;
4034 tmp = metadata_dst_alloc_percpu(IP_TUNNEL_OPTS_MAX,
4039 if (cmpxchg(&md_dst, NULL, tmp))
4040 metadata_dst_free_percpu(tmp);
4044 case BPF_FUNC_skb_set_tunnel_key:
4045 return &bpf_skb_set_tunnel_key_proto;
4046 case BPF_FUNC_skb_set_tunnel_opt:
4047 return &bpf_skb_set_tunnel_opt_proto;
4053 BPF_CALL_3(bpf_skb_under_cgroup, struct sk_buff *, skb, struct bpf_map *, map,
4056 struct bpf_array *array = container_of(map, struct bpf_array, map);
4057 struct cgroup *cgrp;
4060 sk = skb_to_full_sk(skb);
4061 if (!sk || !sk_fullsock(sk))
4063 if (unlikely(idx >= array->map.max_entries))
4066 cgrp = READ_ONCE(array->ptrs[idx]);
4067 if (unlikely(!cgrp))
4070 return sk_under_cgroup_hierarchy(sk, cgrp);
4073 static const struct bpf_func_proto bpf_skb_under_cgroup_proto = {
4074 .func = bpf_skb_under_cgroup,
4076 .ret_type = RET_INTEGER,
4077 .arg1_type = ARG_PTR_TO_CTX,
4078 .arg2_type = ARG_CONST_MAP_PTR,
4079 .arg3_type = ARG_ANYTHING,
4082 #ifdef CONFIG_SOCK_CGROUP_DATA
4083 BPF_CALL_1(bpf_skb_cgroup_id, const struct sk_buff *, skb)
4085 struct sock *sk = skb_to_full_sk(skb);
4086 struct cgroup *cgrp;
4088 if (!sk || !sk_fullsock(sk))
4091 cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
4092 return cgrp->kn->id.id;
4095 static const struct bpf_func_proto bpf_skb_cgroup_id_proto = {
4096 .func = bpf_skb_cgroup_id,
4098 .ret_type = RET_INTEGER,
4099 .arg1_type = ARG_PTR_TO_CTX,
4102 BPF_CALL_2(bpf_skb_ancestor_cgroup_id, const struct sk_buff *, skb, int,
4105 struct sock *sk = skb_to_full_sk(skb);
4106 struct cgroup *ancestor;
4107 struct cgroup *cgrp;
4109 if (!sk || !sk_fullsock(sk))
4112 cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
4113 ancestor = cgroup_ancestor(cgrp, ancestor_level);
4117 return ancestor->kn->id.id;
4120 static const struct bpf_func_proto bpf_skb_ancestor_cgroup_id_proto = {
4121 .func = bpf_skb_ancestor_cgroup_id,
4123 .ret_type = RET_INTEGER,
4124 .arg1_type = ARG_PTR_TO_CTX,
4125 .arg2_type = ARG_ANYTHING,
4129 static unsigned long bpf_xdp_copy(void *dst_buff, const void *src_buff,
4130 unsigned long off, unsigned long len)
4132 memcpy(dst_buff, src_buff + off, len);
4136 BPF_CALL_5(bpf_xdp_event_output, struct xdp_buff *, xdp, struct bpf_map *, map,
4137 u64, flags, void *, meta, u64, meta_size)
4139 u64 xdp_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
4141 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
4143 if (unlikely(xdp_size > (unsigned long)(xdp->data_end - xdp->data)))
4146 return bpf_event_output(map, flags, meta, meta_size, xdp->data,
4147 xdp_size, bpf_xdp_copy);
4150 static const struct bpf_func_proto bpf_xdp_event_output_proto = {
4151 .func = bpf_xdp_event_output,
4153 .ret_type = RET_INTEGER,
4154 .arg1_type = ARG_PTR_TO_CTX,
4155 .arg2_type = ARG_CONST_MAP_PTR,
4156 .arg3_type = ARG_ANYTHING,
4157 .arg4_type = ARG_PTR_TO_MEM,
4158 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
4161 BPF_CALL_1(bpf_get_socket_cookie, struct sk_buff *, skb)
4163 return skb->sk ? sock_gen_cookie(skb->sk) : 0;
4166 static const struct bpf_func_proto bpf_get_socket_cookie_proto = {
4167 .func = bpf_get_socket_cookie,
4169 .ret_type = RET_INTEGER,
4170 .arg1_type = ARG_PTR_TO_CTX,
4173 BPF_CALL_1(bpf_get_socket_cookie_sock_addr, struct bpf_sock_addr_kern *, ctx)
4175 return sock_gen_cookie(ctx->sk);
4178 static const struct bpf_func_proto bpf_get_socket_cookie_sock_addr_proto = {
4179 .func = bpf_get_socket_cookie_sock_addr,
4181 .ret_type = RET_INTEGER,
4182 .arg1_type = ARG_PTR_TO_CTX,
4185 BPF_CALL_1(bpf_get_socket_cookie_sock_ops, struct bpf_sock_ops_kern *, ctx)
4187 return sock_gen_cookie(ctx->sk);
4190 static const struct bpf_func_proto bpf_get_socket_cookie_sock_ops_proto = {
4191 .func = bpf_get_socket_cookie_sock_ops,
4193 .ret_type = RET_INTEGER,
4194 .arg1_type = ARG_PTR_TO_CTX,
4197 BPF_CALL_1(bpf_get_socket_uid, struct sk_buff *, skb)
4199 struct sock *sk = sk_to_full_sk(skb->sk);
4202 if (!sk || !sk_fullsock(sk))
4204 kuid = sock_net_uid(sock_net(sk), sk);
4205 return from_kuid_munged(sock_net(sk)->user_ns, kuid);
4208 static const struct bpf_func_proto bpf_get_socket_uid_proto = {
4209 .func = bpf_get_socket_uid,
4211 .ret_type = RET_INTEGER,
4212 .arg1_type = ARG_PTR_TO_CTX,
4215 BPF_CALL_5(bpf_sockopt_event_output, struct bpf_sock_ops_kern *, bpf_sock,
4216 struct bpf_map *, map, u64, flags, void *, data, u64, size)
4218 if (unlikely(flags & ~(BPF_F_INDEX_MASK)))
4221 return bpf_event_output(map, flags, data, size, NULL, 0, NULL);
4224 static const struct bpf_func_proto bpf_sockopt_event_output_proto = {
4225 .func = bpf_sockopt_event_output,
4227 .ret_type = RET_INTEGER,
4228 .arg1_type = ARG_PTR_TO_CTX,
4229 .arg2_type = ARG_CONST_MAP_PTR,
4230 .arg3_type = ARG_ANYTHING,
4231 .arg4_type = ARG_PTR_TO_MEM,
4232 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
4235 BPF_CALL_5(bpf_setsockopt, struct bpf_sock_ops_kern *, bpf_sock,
4236 int, level, int, optname, char *, optval, int, optlen)
4238 struct sock *sk = bpf_sock->sk;
4242 if (!sk_fullsock(sk))
4245 if (level == SOL_SOCKET) {
4246 if (optlen != sizeof(int))
4248 val = *((int *)optval);
4250 /* Only some socketops are supported */
4253 val = min_t(u32, val, sysctl_rmem_max);
4254 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
4255 sk->sk_rcvbuf = max_t(int, val * 2, SOCK_MIN_RCVBUF);
4258 val = min_t(u32, val, sysctl_wmem_max);
4259 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
4260 sk->sk_sndbuf = max_t(int, val * 2, SOCK_MIN_SNDBUF);
4262 case SO_MAX_PACING_RATE: /* 32bit version */
4264 cmpxchg(&sk->sk_pacing_status,
4267 sk->sk_max_pacing_rate = (val == ~0U) ? ~0UL : val;
4268 sk->sk_pacing_rate = min(sk->sk_pacing_rate,
4269 sk->sk_max_pacing_rate);
4272 sk->sk_priority = val;
4277 sk->sk_rcvlowat = val ? : 1;
4280 if (sk->sk_mark != val) {
4289 } else if (level == SOL_IP) {
4290 if (optlen != sizeof(int) || sk->sk_family != AF_INET)
4293 val = *((int *)optval);
4294 /* Only some options are supported */
4297 if (val < -1 || val > 0xff) {
4300 struct inet_sock *inet = inet_sk(sk);
4310 #if IS_ENABLED(CONFIG_IPV6)
4311 } else if (level == SOL_IPV6) {
4312 if (optlen != sizeof(int) || sk->sk_family != AF_INET6)
4315 val = *((int *)optval);
4316 /* Only some options are supported */
4319 if (val < -1 || val > 0xff) {
4322 struct ipv6_pinfo *np = inet6_sk(sk);
4333 } else if (level == SOL_TCP &&
4334 sk->sk_prot->setsockopt == tcp_setsockopt) {
4335 if (optname == TCP_CONGESTION) {
4336 char name[TCP_CA_NAME_MAX];
4337 bool reinit = bpf_sock->op > BPF_SOCK_OPS_NEEDS_ECN;
4339 strncpy(name, optval, min_t(long, optlen,
4340 TCP_CA_NAME_MAX-1));
4341 name[TCP_CA_NAME_MAX-1] = 0;
4342 ret = tcp_set_congestion_control(sk, name, false,
4345 struct tcp_sock *tp = tcp_sk(sk);
4347 if (optlen != sizeof(int))
4350 val = *((int *)optval);
4351 /* Only some options are supported */
4354 if (val <= 0 || tp->data_segs_out > tp->syn_data)
4359 case TCP_BPF_SNDCWND_CLAMP:
4363 tp->snd_cwnd_clamp = val;
4364 tp->snd_ssthresh = val;
4368 if (val < 0 || val > 1)
4384 static const struct bpf_func_proto bpf_setsockopt_proto = {
4385 .func = bpf_setsockopt,
4387 .ret_type = RET_INTEGER,
4388 .arg1_type = ARG_PTR_TO_CTX,
4389 .arg2_type = ARG_ANYTHING,
4390 .arg3_type = ARG_ANYTHING,
4391 .arg4_type = ARG_PTR_TO_MEM,
4392 .arg5_type = ARG_CONST_SIZE,
4395 BPF_CALL_5(bpf_getsockopt, struct bpf_sock_ops_kern *, bpf_sock,
4396 int, level, int, optname, char *, optval, int, optlen)
4398 struct sock *sk = bpf_sock->sk;
4400 if (!sk_fullsock(sk))
4403 if (level == SOL_TCP && sk->sk_prot->getsockopt == tcp_getsockopt) {
4404 struct inet_connection_sock *icsk;
4405 struct tcp_sock *tp;
4408 case TCP_CONGESTION:
4409 icsk = inet_csk(sk);
4411 if (!icsk->icsk_ca_ops || optlen <= 1)
4413 strncpy(optval, icsk->icsk_ca_ops->name, optlen);
4414 optval[optlen - 1] = 0;
4419 if (optlen <= 0 || !tp->saved_syn ||
4420 optlen > tp->saved_syn[0])
4422 memcpy(optval, tp->saved_syn + 1, optlen);
4427 } else if (level == SOL_IP) {
4428 struct inet_sock *inet = inet_sk(sk);
4430 if (optlen != sizeof(int) || sk->sk_family != AF_INET)
4433 /* Only some options are supported */
4436 *((int *)optval) = (int)inet->tos;
4441 #if IS_ENABLED(CONFIG_IPV6)
4442 } else if (level == SOL_IPV6) {
4443 struct ipv6_pinfo *np = inet6_sk(sk);
4445 if (optlen != sizeof(int) || sk->sk_family != AF_INET6)
4448 /* Only some options are supported */
4451 *((int *)optval) = (int)np->tclass;
4463 memset(optval, 0, optlen);
4467 static const struct bpf_func_proto bpf_getsockopt_proto = {
4468 .func = bpf_getsockopt,
4470 .ret_type = RET_INTEGER,
4471 .arg1_type = ARG_PTR_TO_CTX,
4472 .arg2_type = ARG_ANYTHING,
4473 .arg3_type = ARG_ANYTHING,
4474 .arg4_type = ARG_PTR_TO_UNINIT_MEM,
4475 .arg5_type = ARG_CONST_SIZE,
4478 BPF_CALL_2(bpf_sock_ops_cb_flags_set, struct bpf_sock_ops_kern *, bpf_sock,
4481 struct sock *sk = bpf_sock->sk;
4482 int val = argval & BPF_SOCK_OPS_ALL_CB_FLAGS;
4484 if (!IS_ENABLED(CONFIG_INET) || !sk_fullsock(sk))
4487 tcp_sk(sk)->bpf_sock_ops_cb_flags = val;
4489 return argval & (~BPF_SOCK_OPS_ALL_CB_FLAGS);
4492 static const struct bpf_func_proto bpf_sock_ops_cb_flags_set_proto = {
4493 .func = bpf_sock_ops_cb_flags_set,
4495 .ret_type = RET_INTEGER,
4496 .arg1_type = ARG_PTR_TO_CTX,
4497 .arg2_type = ARG_ANYTHING,
4500 const struct ipv6_bpf_stub *ipv6_bpf_stub __read_mostly;
4501 EXPORT_SYMBOL_GPL(ipv6_bpf_stub);
4503 BPF_CALL_3(bpf_bind, struct bpf_sock_addr_kern *, ctx, struct sockaddr *, addr,
4507 struct sock *sk = ctx->sk;
4510 /* Binding to port can be expensive so it's prohibited in the helper.
4511 * Only binding to IP is supported.
4514 if (addr_len < offsetofend(struct sockaddr, sa_family))
4516 if (addr->sa_family == AF_INET) {
4517 if (addr_len < sizeof(struct sockaddr_in))
4519 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
4521 return __inet_bind(sk, addr, addr_len, true, false);
4522 #if IS_ENABLED(CONFIG_IPV6)
4523 } else if (addr->sa_family == AF_INET6) {
4524 if (addr_len < SIN6_LEN_RFC2133)
4526 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
4528 /* ipv6_bpf_stub cannot be NULL, since it's called from
4529 * bpf_cgroup_inet6_connect hook and ipv6 is already loaded
4531 return ipv6_bpf_stub->inet6_bind(sk, addr, addr_len, true, false);
4532 #endif /* CONFIG_IPV6 */
4534 #endif /* CONFIG_INET */
4536 return -EAFNOSUPPORT;
4539 static const struct bpf_func_proto bpf_bind_proto = {
4542 .ret_type = RET_INTEGER,
4543 .arg1_type = ARG_PTR_TO_CTX,
4544 .arg2_type = ARG_PTR_TO_MEM,
4545 .arg3_type = ARG_CONST_SIZE,
4549 BPF_CALL_5(bpf_skb_get_xfrm_state, struct sk_buff *, skb, u32, index,
4550 struct bpf_xfrm_state *, to, u32, size, u64, flags)
4552 const struct sec_path *sp = skb_sec_path(skb);
4553 const struct xfrm_state *x;
4555 if (!sp || unlikely(index >= sp->len || flags))
4558 x = sp->xvec[index];
4560 if (unlikely(size != sizeof(struct bpf_xfrm_state)))
4563 to->reqid = x->props.reqid;
4564 to->spi = x->id.spi;
4565 to->family = x->props.family;
4568 if (to->family == AF_INET6) {
4569 memcpy(to->remote_ipv6, x->props.saddr.a6,
4570 sizeof(to->remote_ipv6));
4572 to->remote_ipv4 = x->props.saddr.a4;
4573 memset(&to->remote_ipv6[1], 0, sizeof(__u32) * 3);
4578 memset(to, 0, size);
4582 static const struct bpf_func_proto bpf_skb_get_xfrm_state_proto = {
4583 .func = bpf_skb_get_xfrm_state,
4585 .ret_type = RET_INTEGER,
4586 .arg1_type = ARG_PTR_TO_CTX,
4587 .arg2_type = ARG_ANYTHING,
4588 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
4589 .arg4_type = ARG_CONST_SIZE,
4590 .arg5_type = ARG_ANYTHING,
4594 #if IS_ENABLED(CONFIG_INET) || IS_ENABLED(CONFIG_IPV6)
4595 static int bpf_fib_set_fwd_params(struct bpf_fib_lookup *params,
4596 const struct neighbour *neigh,
4597 const struct net_device *dev)
4599 memcpy(params->dmac, neigh->ha, ETH_ALEN);
4600 memcpy(params->smac, dev->dev_addr, ETH_ALEN);
4601 params->h_vlan_TCI = 0;
4602 params->h_vlan_proto = 0;
4603 params->ifindex = dev->ifindex;
4609 #if IS_ENABLED(CONFIG_INET)
4610 static int bpf_ipv4_fib_lookup(struct net *net, struct bpf_fib_lookup *params,
4611 u32 flags, bool check_mtu)
4613 struct fib_nh_common *nhc;
4614 struct in_device *in_dev;
4615 struct neighbour *neigh;
4616 struct net_device *dev;
4617 struct fib_result res;
4622 dev = dev_get_by_index_rcu(net, params->ifindex);
4626 /* verify forwarding is enabled on this interface */
4627 in_dev = __in_dev_get_rcu(dev);
4628 if (unlikely(!in_dev || !IN_DEV_FORWARD(in_dev)))
4629 return BPF_FIB_LKUP_RET_FWD_DISABLED;
4631 if (flags & BPF_FIB_LOOKUP_OUTPUT) {
4633 fl4.flowi4_oif = params->ifindex;
4635 fl4.flowi4_iif = params->ifindex;
4638 fl4.flowi4_tos = params->tos & IPTOS_RT_MASK;
4639 fl4.flowi4_scope = RT_SCOPE_UNIVERSE;
4640 fl4.flowi4_flags = 0;
4642 fl4.flowi4_proto = params->l4_protocol;
4643 fl4.daddr = params->ipv4_dst;
4644 fl4.saddr = params->ipv4_src;
4645 fl4.fl4_sport = params->sport;
4646 fl4.fl4_dport = params->dport;
4648 if (flags & BPF_FIB_LOOKUP_DIRECT) {
4649 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
4650 struct fib_table *tb;
4652 tb = fib_get_table(net, tbid);
4654 return BPF_FIB_LKUP_RET_NOT_FWDED;
4656 err = fib_table_lookup(tb, &fl4, &res, FIB_LOOKUP_NOREF);
4658 fl4.flowi4_mark = 0;
4659 fl4.flowi4_secid = 0;
4660 fl4.flowi4_tun_key.tun_id = 0;
4661 fl4.flowi4_uid = sock_net_uid(net, NULL);
4663 err = fib_lookup(net, &fl4, &res, FIB_LOOKUP_NOREF);
4667 /* map fib lookup errors to RTN_ type */
4669 return BPF_FIB_LKUP_RET_BLACKHOLE;
4670 if (err == -EHOSTUNREACH)
4671 return BPF_FIB_LKUP_RET_UNREACHABLE;
4673 return BPF_FIB_LKUP_RET_PROHIBIT;
4675 return BPF_FIB_LKUP_RET_NOT_FWDED;
4678 if (res.type != RTN_UNICAST)
4679 return BPF_FIB_LKUP_RET_NOT_FWDED;
4681 if (fib_info_num_path(res.fi) > 1)
4682 fib_select_path(net, &res, &fl4, NULL);
4685 mtu = ip_mtu_from_fib_result(&res, params->ipv4_dst);
4686 if (params->tot_len > mtu)
4687 return BPF_FIB_LKUP_RET_FRAG_NEEDED;
4692 /* do not handle lwt encaps right now */
4693 if (nhc->nhc_lwtstate)
4694 return BPF_FIB_LKUP_RET_UNSUPP_LWT;
4698 params->rt_metric = res.fi->fib_priority;
4700 /* xdp and cls_bpf programs are run in RCU-bh so
4701 * rcu_read_lock_bh is not needed here
4703 if (likely(nhc->nhc_gw_family != AF_INET6)) {
4704 if (nhc->nhc_gw_family)
4705 params->ipv4_dst = nhc->nhc_gw.ipv4;
4707 neigh = __ipv4_neigh_lookup_noref(dev,
4708 (__force u32)params->ipv4_dst);
4710 struct in6_addr *dst = (struct in6_addr *)params->ipv6_dst;
4712 params->family = AF_INET6;
4713 *dst = nhc->nhc_gw.ipv6;
4714 neigh = __ipv6_neigh_lookup_noref_stub(dev, dst);
4718 return BPF_FIB_LKUP_RET_NO_NEIGH;
4720 return bpf_fib_set_fwd_params(params, neigh, dev);
4724 #if IS_ENABLED(CONFIG_IPV6)
4725 static int bpf_ipv6_fib_lookup(struct net *net, struct bpf_fib_lookup *params,
4726 u32 flags, bool check_mtu)
4728 struct in6_addr *src = (struct in6_addr *) params->ipv6_src;
4729 struct in6_addr *dst = (struct in6_addr *) params->ipv6_dst;
4730 struct fib6_result res = {};
4731 struct neighbour *neigh;
4732 struct net_device *dev;
4733 struct inet6_dev *idev;
4739 /* link local addresses are never forwarded */
4740 if (rt6_need_strict(dst) || rt6_need_strict(src))
4741 return BPF_FIB_LKUP_RET_NOT_FWDED;
4743 dev = dev_get_by_index_rcu(net, params->ifindex);
4747 idev = __in6_dev_get_safely(dev);
4748 if (unlikely(!idev || !idev->cnf.forwarding))
4749 return BPF_FIB_LKUP_RET_FWD_DISABLED;
4751 if (flags & BPF_FIB_LOOKUP_OUTPUT) {
4753 oif = fl6.flowi6_oif = params->ifindex;
4755 oif = fl6.flowi6_iif = params->ifindex;
4757 strict = RT6_LOOKUP_F_HAS_SADDR;
4759 fl6.flowlabel = params->flowinfo;
4760 fl6.flowi6_scope = 0;
4761 fl6.flowi6_flags = 0;
4764 fl6.flowi6_proto = params->l4_protocol;
4767 fl6.fl6_sport = params->sport;
4768 fl6.fl6_dport = params->dport;
4770 if (flags & BPF_FIB_LOOKUP_DIRECT) {
4771 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
4772 struct fib6_table *tb;
4774 tb = ipv6_stub->fib6_get_table(net, tbid);
4776 return BPF_FIB_LKUP_RET_NOT_FWDED;
4778 err = ipv6_stub->fib6_table_lookup(net, tb, oif, &fl6, &res,
4781 fl6.flowi6_mark = 0;
4782 fl6.flowi6_secid = 0;
4783 fl6.flowi6_tun_key.tun_id = 0;
4784 fl6.flowi6_uid = sock_net_uid(net, NULL);
4786 err = ipv6_stub->fib6_lookup(net, oif, &fl6, &res, strict);
4789 if (unlikely(err || IS_ERR_OR_NULL(res.f6i) ||
4790 res.f6i == net->ipv6.fib6_null_entry))
4791 return BPF_FIB_LKUP_RET_NOT_FWDED;
4793 switch (res.fib6_type) {
4794 /* only unicast is forwarded */
4798 return BPF_FIB_LKUP_RET_BLACKHOLE;
4799 case RTN_UNREACHABLE:
4800 return BPF_FIB_LKUP_RET_UNREACHABLE;
4802 return BPF_FIB_LKUP_RET_PROHIBIT;
4804 return BPF_FIB_LKUP_RET_NOT_FWDED;
4807 ipv6_stub->fib6_select_path(net, &res, &fl6, fl6.flowi6_oif,
4808 fl6.flowi6_oif != 0, NULL, strict);
4811 mtu = ipv6_stub->ip6_mtu_from_fib6(&res, dst, src);
4812 if (params->tot_len > mtu)
4813 return BPF_FIB_LKUP_RET_FRAG_NEEDED;
4816 if (res.nh->fib_nh_lws)
4817 return BPF_FIB_LKUP_RET_UNSUPP_LWT;
4819 if (res.nh->fib_nh_gw_family)
4820 *dst = res.nh->fib_nh_gw6;
4822 dev = res.nh->fib_nh_dev;
4823 params->rt_metric = res.f6i->fib6_metric;
4825 /* xdp and cls_bpf programs are run in RCU-bh so rcu_read_lock_bh is
4828 neigh = __ipv6_neigh_lookup_noref_stub(dev, dst);
4830 return BPF_FIB_LKUP_RET_NO_NEIGH;
4832 return bpf_fib_set_fwd_params(params, neigh, dev);
4836 BPF_CALL_4(bpf_xdp_fib_lookup, struct xdp_buff *, ctx,
4837 struct bpf_fib_lookup *, params, int, plen, u32, flags)
4839 if (plen < sizeof(*params))
4842 if (flags & ~(BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_OUTPUT))
4845 switch (params->family) {
4846 #if IS_ENABLED(CONFIG_INET)
4848 return bpf_ipv4_fib_lookup(dev_net(ctx->rxq->dev), params,
4851 #if IS_ENABLED(CONFIG_IPV6)
4853 return bpf_ipv6_fib_lookup(dev_net(ctx->rxq->dev), params,
4857 return -EAFNOSUPPORT;
4860 static const struct bpf_func_proto bpf_xdp_fib_lookup_proto = {
4861 .func = bpf_xdp_fib_lookup,
4863 .ret_type = RET_INTEGER,
4864 .arg1_type = ARG_PTR_TO_CTX,
4865 .arg2_type = ARG_PTR_TO_MEM,
4866 .arg3_type = ARG_CONST_SIZE,
4867 .arg4_type = ARG_ANYTHING,
4870 BPF_CALL_4(bpf_skb_fib_lookup, struct sk_buff *, skb,
4871 struct bpf_fib_lookup *, params, int, plen, u32, flags)
4873 struct net *net = dev_net(skb->dev);
4874 int rc = -EAFNOSUPPORT;
4876 if (plen < sizeof(*params))
4879 if (flags & ~(BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_OUTPUT))
4882 switch (params->family) {
4883 #if IS_ENABLED(CONFIG_INET)
4885 rc = bpf_ipv4_fib_lookup(net, params, flags, false);
4888 #if IS_ENABLED(CONFIG_IPV6)
4890 rc = bpf_ipv6_fib_lookup(net, params, flags, false);
4896 struct net_device *dev;
4898 dev = dev_get_by_index_rcu(net, params->ifindex);
4899 if (!is_skb_forwardable(dev, skb))
4900 rc = BPF_FIB_LKUP_RET_FRAG_NEEDED;
4906 static const struct bpf_func_proto bpf_skb_fib_lookup_proto = {
4907 .func = bpf_skb_fib_lookup,
4909 .ret_type = RET_INTEGER,
4910 .arg1_type = ARG_PTR_TO_CTX,
4911 .arg2_type = ARG_PTR_TO_MEM,
4912 .arg3_type = ARG_CONST_SIZE,
4913 .arg4_type = ARG_ANYTHING,
4916 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
4917 static int bpf_push_seg6_encap(struct sk_buff *skb, u32 type, void *hdr, u32 len)
4920 struct ipv6_sr_hdr *srh = (struct ipv6_sr_hdr *)hdr;
4922 if (!seg6_validate_srh(srh, len))
4926 case BPF_LWT_ENCAP_SEG6_INLINE:
4927 if (skb->protocol != htons(ETH_P_IPV6))
4930 err = seg6_do_srh_inline(skb, srh);
4932 case BPF_LWT_ENCAP_SEG6:
4933 skb_reset_inner_headers(skb);
4934 skb->encapsulation = 1;
4935 err = seg6_do_srh_encap(skb, srh, IPPROTO_IPV6);
4941 bpf_compute_data_pointers(skb);
4945 ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
4946 skb_set_transport_header(skb, sizeof(struct ipv6hdr));
4948 return seg6_lookup_nexthop(skb, NULL, 0);
4950 #endif /* CONFIG_IPV6_SEG6_BPF */
4952 #if IS_ENABLED(CONFIG_LWTUNNEL_BPF)
4953 static int bpf_push_ip_encap(struct sk_buff *skb, void *hdr, u32 len,
4956 return bpf_lwt_push_ip_encap(skb, hdr, len, ingress);
4960 BPF_CALL_4(bpf_lwt_in_push_encap, struct sk_buff *, skb, u32, type, void *, hdr,
4964 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
4965 case BPF_LWT_ENCAP_SEG6:
4966 case BPF_LWT_ENCAP_SEG6_INLINE:
4967 return bpf_push_seg6_encap(skb, type, hdr, len);
4969 #if IS_ENABLED(CONFIG_LWTUNNEL_BPF)
4970 case BPF_LWT_ENCAP_IP:
4971 return bpf_push_ip_encap(skb, hdr, len, true /* ingress */);
4978 BPF_CALL_4(bpf_lwt_xmit_push_encap, struct sk_buff *, skb, u32, type,
4979 void *, hdr, u32, len)
4982 #if IS_ENABLED(CONFIG_LWTUNNEL_BPF)
4983 case BPF_LWT_ENCAP_IP:
4984 return bpf_push_ip_encap(skb, hdr, len, false /* egress */);
4991 static const struct bpf_func_proto bpf_lwt_in_push_encap_proto = {
4992 .func = bpf_lwt_in_push_encap,
4994 .ret_type = RET_INTEGER,
4995 .arg1_type = ARG_PTR_TO_CTX,
4996 .arg2_type = ARG_ANYTHING,
4997 .arg3_type = ARG_PTR_TO_MEM,
4998 .arg4_type = ARG_CONST_SIZE
5001 static const struct bpf_func_proto bpf_lwt_xmit_push_encap_proto = {
5002 .func = bpf_lwt_xmit_push_encap,
5004 .ret_type = RET_INTEGER,
5005 .arg1_type = ARG_PTR_TO_CTX,
5006 .arg2_type = ARG_ANYTHING,
5007 .arg3_type = ARG_PTR_TO_MEM,
5008 .arg4_type = ARG_CONST_SIZE
5011 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
5012 BPF_CALL_4(bpf_lwt_seg6_store_bytes, struct sk_buff *, skb, u32, offset,
5013 const void *, from, u32, len)
5015 struct seg6_bpf_srh_state *srh_state =
5016 this_cpu_ptr(&seg6_bpf_srh_states);
5017 struct ipv6_sr_hdr *srh = srh_state->srh;
5018 void *srh_tlvs, *srh_end, *ptr;
5024 srh_tlvs = (void *)((char *)srh + ((srh->first_segment + 1) << 4));
5025 srh_end = (void *)((char *)srh + sizeof(*srh) + srh_state->hdrlen);
5027 ptr = skb->data + offset;
5028 if (ptr >= srh_tlvs && ptr + len <= srh_end)
5029 srh_state->valid = false;
5030 else if (ptr < (void *)&srh->flags ||
5031 ptr + len > (void *)&srh->segments)
5034 if (unlikely(bpf_try_make_writable(skb, offset + len)))
5036 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0)
5038 srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
5040 memcpy(skb->data + offset, from, len);
5044 static const struct bpf_func_proto bpf_lwt_seg6_store_bytes_proto = {
5045 .func = bpf_lwt_seg6_store_bytes,
5047 .ret_type = RET_INTEGER,
5048 .arg1_type = ARG_PTR_TO_CTX,
5049 .arg2_type = ARG_ANYTHING,
5050 .arg3_type = ARG_PTR_TO_MEM,
5051 .arg4_type = ARG_CONST_SIZE
5054 static void bpf_update_srh_state(struct sk_buff *skb)
5056 struct seg6_bpf_srh_state *srh_state =
5057 this_cpu_ptr(&seg6_bpf_srh_states);
5060 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0) {
5061 srh_state->srh = NULL;
5063 srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
5064 srh_state->hdrlen = srh_state->srh->hdrlen << 3;
5065 srh_state->valid = true;
5069 BPF_CALL_4(bpf_lwt_seg6_action, struct sk_buff *, skb,
5070 u32, action, void *, param, u32, param_len)
5072 struct seg6_bpf_srh_state *srh_state =
5073 this_cpu_ptr(&seg6_bpf_srh_states);
5078 case SEG6_LOCAL_ACTION_END_X:
5079 if (!seg6_bpf_has_valid_srh(skb))
5081 if (param_len != sizeof(struct in6_addr))
5083 return seg6_lookup_nexthop(skb, (struct in6_addr *)param, 0);
5084 case SEG6_LOCAL_ACTION_END_T:
5085 if (!seg6_bpf_has_valid_srh(skb))
5087 if (param_len != sizeof(int))
5089 return seg6_lookup_nexthop(skb, NULL, *(int *)param);
5090 case SEG6_LOCAL_ACTION_END_DT6:
5091 if (!seg6_bpf_has_valid_srh(skb))
5093 if (param_len != sizeof(int))
5096 if (ipv6_find_hdr(skb, &hdroff, IPPROTO_IPV6, NULL, NULL) < 0)
5098 if (!pskb_pull(skb, hdroff))
5101 skb_postpull_rcsum(skb, skb_network_header(skb), hdroff);
5102 skb_reset_network_header(skb);
5103 skb_reset_transport_header(skb);
5104 skb->encapsulation = 0;
5106 bpf_compute_data_pointers(skb);
5107 bpf_update_srh_state(skb);
5108 return seg6_lookup_nexthop(skb, NULL, *(int *)param);
5109 case SEG6_LOCAL_ACTION_END_B6:
5110 if (srh_state->srh && !seg6_bpf_has_valid_srh(skb))
5112 err = bpf_push_seg6_encap(skb, BPF_LWT_ENCAP_SEG6_INLINE,
5115 bpf_update_srh_state(skb);
5118 case SEG6_LOCAL_ACTION_END_B6_ENCAP:
5119 if (srh_state->srh && !seg6_bpf_has_valid_srh(skb))
5121 err = bpf_push_seg6_encap(skb, BPF_LWT_ENCAP_SEG6,
5124 bpf_update_srh_state(skb);
5132 static const struct bpf_func_proto bpf_lwt_seg6_action_proto = {
5133 .func = bpf_lwt_seg6_action,
5135 .ret_type = RET_INTEGER,
5136 .arg1_type = ARG_PTR_TO_CTX,
5137 .arg2_type = ARG_ANYTHING,
5138 .arg3_type = ARG_PTR_TO_MEM,
5139 .arg4_type = ARG_CONST_SIZE
5142 BPF_CALL_3(bpf_lwt_seg6_adjust_srh, struct sk_buff *, skb, u32, offset,
5145 struct seg6_bpf_srh_state *srh_state =
5146 this_cpu_ptr(&seg6_bpf_srh_states);
5147 struct ipv6_sr_hdr *srh = srh_state->srh;
5148 void *srh_end, *srh_tlvs, *ptr;
5149 struct ipv6hdr *hdr;
5153 if (unlikely(srh == NULL))
5156 srh_tlvs = (void *)((unsigned char *)srh + sizeof(*srh) +
5157 ((srh->first_segment + 1) << 4));
5158 srh_end = (void *)((unsigned char *)srh + sizeof(*srh) +
5160 ptr = skb->data + offset;
5162 if (unlikely(ptr < srh_tlvs || ptr > srh_end))
5164 if (unlikely(len < 0 && (void *)((char *)ptr - len) > srh_end))
5168 ret = skb_cow_head(skb, len);
5169 if (unlikely(ret < 0))
5172 ret = bpf_skb_net_hdr_push(skb, offset, len);
5174 ret = bpf_skb_net_hdr_pop(skb, offset, -1 * len);
5177 bpf_compute_data_pointers(skb);
5178 if (unlikely(ret < 0))
5181 hdr = (struct ipv6hdr *)skb->data;
5182 hdr->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
5184 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0)
5186 srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
5187 srh_state->hdrlen += len;
5188 srh_state->valid = false;
5192 static const struct bpf_func_proto bpf_lwt_seg6_adjust_srh_proto = {
5193 .func = bpf_lwt_seg6_adjust_srh,
5195 .ret_type = RET_INTEGER,
5196 .arg1_type = ARG_PTR_TO_CTX,
5197 .arg2_type = ARG_ANYTHING,
5198 .arg3_type = ARG_ANYTHING,
5200 #endif /* CONFIG_IPV6_SEG6_BPF */
5203 static struct sock *sk_lookup(struct net *net, struct bpf_sock_tuple *tuple,
5204 int dif, int sdif, u8 family, u8 proto)
5206 bool refcounted = false;
5207 struct sock *sk = NULL;
5209 if (family == AF_INET) {
5210 __be32 src4 = tuple->ipv4.saddr;
5211 __be32 dst4 = tuple->ipv4.daddr;
5213 if (proto == IPPROTO_TCP)
5214 sk = __inet_lookup(net, &tcp_hashinfo, NULL, 0,
5215 src4, tuple->ipv4.sport,
5216 dst4, tuple->ipv4.dport,
5217 dif, sdif, &refcounted);
5219 sk = __udp4_lib_lookup(net, src4, tuple->ipv4.sport,
5220 dst4, tuple->ipv4.dport,
5221 dif, sdif, &udp_table, NULL);
5222 #if IS_ENABLED(CONFIG_IPV6)
5224 struct in6_addr *src6 = (struct in6_addr *)&tuple->ipv6.saddr;
5225 struct in6_addr *dst6 = (struct in6_addr *)&tuple->ipv6.daddr;
5227 if (proto == IPPROTO_TCP)
5228 sk = __inet6_lookup(net, &tcp_hashinfo, NULL, 0,
5229 src6, tuple->ipv6.sport,
5230 dst6, ntohs(tuple->ipv6.dport),
5231 dif, sdif, &refcounted);
5232 else if (likely(ipv6_bpf_stub))
5233 sk = ipv6_bpf_stub->udp6_lib_lookup(net,
5234 src6, tuple->ipv6.sport,
5235 dst6, tuple->ipv6.dport,
5241 if (unlikely(sk && !refcounted && !sock_flag(sk, SOCK_RCU_FREE))) {
5242 WARN_ONCE(1, "Found non-RCU, unreferenced socket!");
5248 /* bpf_skc_lookup performs the core lookup for different types of sockets,
5249 * taking a reference on the socket if it doesn't have the flag SOCK_RCU_FREE.
5250 * Returns the socket as an 'unsigned long' to simplify the casting in the
5251 * callers to satisfy BPF_CALL declarations.
5253 static struct sock *
5254 __bpf_skc_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
5255 struct net *caller_net, u32 ifindex, u8 proto, u64 netns_id,
5258 struct sock *sk = NULL;
5259 u8 family = AF_UNSPEC;
5263 if (len == sizeof(tuple->ipv4))
5265 else if (len == sizeof(tuple->ipv6))
5270 if (unlikely(family == AF_UNSPEC || flags ||
5271 !((s32)netns_id < 0 || netns_id <= S32_MAX)))
5274 if (family == AF_INET)
5275 sdif = inet_sdif(skb);
5277 sdif = inet6_sdif(skb);
5279 if ((s32)netns_id < 0) {
5281 sk = sk_lookup(net, tuple, ifindex, sdif, family, proto);
5283 net = get_net_ns_by_id(caller_net, netns_id);
5286 sk = sk_lookup(net, tuple, ifindex, sdif, family, proto);
5294 static struct sock *
5295 __bpf_sk_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
5296 struct net *caller_net, u32 ifindex, u8 proto, u64 netns_id,
5299 struct sock *sk = __bpf_skc_lookup(skb, tuple, len, caller_net,
5300 ifindex, proto, netns_id, flags);
5303 sk = sk_to_full_sk(sk);
5304 if (!sk_fullsock(sk)) {
5305 if (!sock_flag(sk, SOCK_RCU_FREE))
5314 static struct sock *
5315 bpf_skc_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
5316 u8 proto, u64 netns_id, u64 flags)
5318 struct net *caller_net;
5322 caller_net = dev_net(skb->dev);
5323 ifindex = skb->dev->ifindex;
5325 caller_net = sock_net(skb->sk);
5329 return __bpf_skc_lookup(skb, tuple, len, caller_net, ifindex, proto,
5333 static struct sock *
5334 bpf_sk_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
5335 u8 proto, u64 netns_id, u64 flags)
5337 struct sock *sk = bpf_skc_lookup(skb, tuple, len, proto, netns_id,
5341 sk = sk_to_full_sk(sk);
5342 if (!sk_fullsock(sk)) {
5343 if (!sock_flag(sk, SOCK_RCU_FREE))
5352 BPF_CALL_5(bpf_skc_lookup_tcp, struct sk_buff *, skb,
5353 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
5355 return (unsigned long)bpf_skc_lookup(skb, tuple, len, IPPROTO_TCP,
5359 static const struct bpf_func_proto bpf_skc_lookup_tcp_proto = {
5360 .func = bpf_skc_lookup_tcp,
5363 .ret_type = RET_PTR_TO_SOCK_COMMON_OR_NULL,
5364 .arg1_type = ARG_PTR_TO_CTX,
5365 .arg2_type = ARG_PTR_TO_MEM,
5366 .arg3_type = ARG_CONST_SIZE,
5367 .arg4_type = ARG_ANYTHING,
5368 .arg5_type = ARG_ANYTHING,
5371 BPF_CALL_5(bpf_sk_lookup_tcp, struct sk_buff *, skb,
5372 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
5374 return (unsigned long)bpf_sk_lookup(skb, tuple, len, IPPROTO_TCP,
5378 static const struct bpf_func_proto bpf_sk_lookup_tcp_proto = {
5379 .func = bpf_sk_lookup_tcp,
5382 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
5383 .arg1_type = ARG_PTR_TO_CTX,
5384 .arg2_type = ARG_PTR_TO_MEM,
5385 .arg3_type = ARG_CONST_SIZE,
5386 .arg4_type = ARG_ANYTHING,
5387 .arg5_type = ARG_ANYTHING,
5390 BPF_CALL_5(bpf_sk_lookup_udp, struct sk_buff *, skb,
5391 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
5393 return (unsigned long)bpf_sk_lookup(skb, tuple, len, IPPROTO_UDP,
5397 static const struct bpf_func_proto bpf_sk_lookup_udp_proto = {
5398 .func = bpf_sk_lookup_udp,
5401 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
5402 .arg1_type = ARG_PTR_TO_CTX,
5403 .arg2_type = ARG_PTR_TO_MEM,
5404 .arg3_type = ARG_CONST_SIZE,
5405 .arg4_type = ARG_ANYTHING,
5406 .arg5_type = ARG_ANYTHING,
5409 BPF_CALL_1(bpf_sk_release, struct sock *, sk)
5411 if (!sock_flag(sk, SOCK_RCU_FREE))
5416 static const struct bpf_func_proto bpf_sk_release_proto = {
5417 .func = bpf_sk_release,
5419 .ret_type = RET_INTEGER,
5420 .arg1_type = ARG_PTR_TO_SOCK_COMMON,
5423 BPF_CALL_5(bpf_xdp_sk_lookup_udp, struct xdp_buff *, ctx,
5424 struct bpf_sock_tuple *, tuple, u32, len, u32, netns_id, u64, flags)
5426 struct net *caller_net = dev_net(ctx->rxq->dev);
5427 int ifindex = ctx->rxq->dev->ifindex;
5429 return (unsigned long)__bpf_sk_lookup(NULL, tuple, len, caller_net,
5430 ifindex, IPPROTO_UDP, netns_id,
5434 static const struct bpf_func_proto bpf_xdp_sk_lookup_udp_proto = {
5435 .func = bpf_xdp_sk_lookup_udp,
5438 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
5439 .arg1_type = ARG_PTR_TO_CTX,
5440 .arg2_type = ARG_PTR_TO_MEM,
5441 .arg3_type = ARG_CONST_SIZE,
5442 .arg4_type = ARG_ANYTHING,
5443 .arg5_type = ARG_ANYTHING,
5446 BPF_CALL_5(bpf_xdp_skc_lookup_tcp, struct xdp_buff *, ctx,
5447 struct bpf_sock_tuple *, tuple, u32, len, u32, netns_id, u64, flags)
5449 struct net *caller_net = dev_net(ctx->rxq->dev);
5450 int ifindex = ctx->rxq->dev->ifindex;
5452 return (unsigned long)__bpf_skc_lookup(NULL, tuple, len, caller_net,
5453 ifindex, IPPROTO_TCP, netns_id,
5457 static const struct bpf_func_proto bpf_xdp_skc_lookup_tcp_proto = {
5458 .func = bpf_xdp_skc_lookup_tcp,
5461 .ret_type = RET_PTR_TO_SOCK_COMMON_OR_NULL,
5462 .arg1_type = ARG_PTR_TO_CTX,
5463 .arg2_type = ARG_PTR_TO_MEM,
5464 .arg3_type = ARG_CONST_SIZE,
5465 .arg4_type = ARG_ANYTHING,
5466 .arg5_type = ARG_ANYTHING,
5469 BPF_CALL_5(bpf_xdp_sk_lookup_tcp, struct xdp_buff *, ctx,
5470 struct bpf_sock_tuple *, tuple, u32, len, u32, netns_id, u64, flags)
5472 struct net *caller_net = dev_net(ctx->rxq->dev);
5473 int ifindex = ctx->rxq->dev->ifindex;
5475 return (unsigned long)__bpf_sk_lookup(NULL, tuple, len, caller_net,
5476 ifindex, IPPROTO_TCP, netns_id,
5480 static const struct bpf_func_proto bpf_xdp_sk_lookup_tcp_proto = {
5481 .func = bpf_xdp_sk_lookup_tcp,
5484 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
5485 .arg1_type = ARG_PTR_TO_CTX,
5486 .arg2_type = ARG_PTR_TO_MEM,
5487 .arg3_type = ARG_CONST_SIZE,
5488 .arg4_type = ARG_ANYTHING,
5489 .arg5_type = ARG_ANYTHING,
5492 BPF_CALL_5(bpf_sock_addr_skc_lookup_tcp, struct bpf_sock_addr_kern *, ctx,
5493 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
5495 return (unsigned long)__bpf_skc_lookup(NULL, tuple, len,
5496 sock_net(ctx->sk), 0,
5497 IPPROTO_TCP, netns_id, flags);
5500 static const struct bpf_func_proto bpf_sock_addr_skc_lookup_tcp_proto = {
5501 .func = bpf_sock_addr_skc_lookup_tcp,
5503 .ret_type = RET_PTR_TO_SOCK_COMMON_OR_NULL,
5504 .arg1_type = ARG_PTR_TO_CTX,
5505 .arg2_type = ARG_PTR_TO_MEM,
5506 .arg3_type = ARG_CONST_SIZE,
5507 .arg4_type = ARG_ANYTHING,
5508 .arg5_type = ARG_ANYTHING,
5511 BPF_CALL_5(bpf_sock_addr_sk_lookup_tcp, struct bpf_sock_addr_kern *, ctx,
5512 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
5514 return (unsigned long)__bpf_sk_lookup(NULL, tuple, len,
5515 sock_net(ctx->sk), 0, IPPROTO_TCP,
5519 static const struct bpf_func_proto bpf_sock_addr_sk_lookup_tcp_proto = {
5520 .func = bpf_sock_addr_sk_lookup_tcp,
5522 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
5523 .arg1_type = ARG_PTR_TO_CTX,
5524 .arg2_type = ARG_PTR_TO_MEM,
5525 .arg3_type = ARG_CONST_SIZE,
5526 .arg4_type = ARG_ANYTHING,
5527 .arg5_type = ARG_ANYTHING,
5530 BPF_CALL_5(bpf_sock_addr_sk_lookup_udp, struct bpf_sock_addr_kern *, ctx,
5531 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
5533 return (unsigned long)__bpf_sk_lookup(NULL, tuple, len,
5534 sock_net(ctx->sk), 0, IPPROTO_UDP,
5538 static const struct bpf_func_proto bpf_sock_addr_sk_lookup_udp_proto = {
5539 .func = bpf_sock_addr_sk_lookup_udp,
5541 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
5542 .arg1_type = ARG_PTR_TO_CTX,
5543 .arg2_type = ARG_PTR_TO_MEM,
5544 .arg3_type = ARG_CONST_SIZE,
5545 .arg4_type = ARG_ANYTHING,
5546 .arg5_type = ARG_ANYTHING,
5549 bool bpf_tcp_sock_is_valid_access(int off, int size, enum bpf_access_type type,
5550 struct bpf_insn_access_aux *info)
5552 if (off < 0 || off >= offsetofend(struct bpf_tcp_sock,
5556 if (off % size != 0)
5560 case offsetof(struct bpf_tcp_sock, bytes_received):
5561 case offsetof(struct bpf_tcp_sock, bytes_acked):
5562 return size == sizeof(__u64);
5564 return size == sizeof(__u32);
5568 u32 bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type,
5569 const struct bpf_insn *si,
5570 struct bpf_insn *insn_buf,
5571 struct bpf_prog *prog, u32 *target_size)
5573 struct bpf_insn *insn = insn_buf;
5575 #define BPF_TCP_SOCK_GET_COMMON(FIELD) \
5577 BUILD_BUG_ON(FIELD_SIZEOF(struct tcp_sock, FIELD) > \
5578 FIELD_SIZEOF(struct bpf_tcp_sock, FIELD)); \
5579 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct tcp_sock, FIELD),\
5580 si->dst_reg, si->src_reg, \
5581 offsetof(struct tcp_sock, FIELD)); \
5584 #define BPF_INET_SOCK_GET_COMMON(FIELD) \
5586 BUILD_BUG_ON(FIELD_SIZEOF(struct inet_connection_sock, \
5588 FIELD_SIZEOF(struct bpf_tcp_sock, FIELD)); \
5589 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
5590 struct inet_connection_sock, \
5592 si->dst_reg, si->src_reg, \
5594 struct inet_connection_sock, \
5598 if (insn > insn_buf)
5599 return insn - insn_buf;
5602 case offsetof(struct bpf_tcp_sock, rtt_min):
5603 BUILD_BUG_ON(FIELD_SIZEOF(struct tcp_sock, rtt_min) !=
5604 sizeof(struct minmax));
5605 BUILD_BUG_ON(sizeof(struct minmax) <
5606 sizeof(struct minmax_sample));
5608 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
5609 offsetof(struct tcp_sock, rtt_min) +
5610 offsetof(struct minmax_sample, v));
5612 case offsetof(struct bpf_tcp_sock, snd_cwnd):
5613 BPF_TCP_SOCK_GET_COMMON(snd_cwnd);
5615 case offsetof(struct bpf_tcp_sock, srtt_us):
5616 BPF_TCP_SOCK_GET_COMMON(srtt_us);
5618 case offsetof(struct bpf_tcp_sock, snd_ssthresh):
5619 BPF_TCP_SOCK_GET_COMMON(snd_ssthresh);
5621 case offsetof(struct bpf_tcp_sock, rcv_nxt):
5622 BPF_TCP_SOCK_GET_COMMON(rcv_nxt);
5624 case offsetof(struct bpf_tcp_sock, snd_nxt):
5625 BPF_TCP_SOCK_GET_COMMON(snd_nxt);
5627 case offsetof(struct bpf_tcp_sock, snd_una):
5628 BPF_TCP_SOCK_GET_COMMON(snd_una);
5630 case offsetof(struct bpf_tcp_sock, mss_cache):
5631 BPF_TCP_SOCK_GET_COMMON(mss_cache);
5633 case offsetof(struct bpf_tcp_sock, ecn_flags):
5634 BPF_TCP_SOCK_GET_COMMON(ecn_flags);
5636 case offsetof(struct bpf_tcp_sock, rate_delivered):
5637 BPF_TCP_SOCK_GET_COMMON(rate_delivered);
5639 case offsetof(struct bpf_tcp_sock, rate_interval_us):
5640 BPF_TCP_SOCK_GET_COMMON(rate_interval_us);
5642 case offsetof(struct bpf_tcp_sock, packets_out):
5643 BPF_TCP_SOCK_GET_COMMON(packets_out);
5645 case offsetof(struct bpf_tcp_sock, retrans_out):
5646 BPF_TCP_SOCK_GET_COMMON(retrans_out);
5648 case offsetof(struct bpf_tcp_sock, total_retrans):
5649 BPF_TCP_SOCK_GET_COMMON(total_retrans);
5651 case offsetof(struct bpf_tcp_sock, segs_in):
5652 BPF_TCP_SOCK_GET_COMMON(segs_in);
5654 case offsetof(struct bpf_tcp_sock, data_segs_in):
5655 BPF_TCP_SOCK_GET_COMMON(data_segs_in);
5657 case offsetof(struct bpf_tcp_sock, segs_out):
5658 BPF_TCP_SOCK_GET_COMMON(segs_out);
5660 case offsetof(struct bpf_tcp_sock, data_segs_out):
5661 BPF_TCP_SOCK_GET_COMMON(data_segs_out);
5663 case offsetof(struct bpf_tcp_sock, lost_out):
5664 BPF_TCP_SOCK_GET_COMMON(lost_out);
5666 case offsetof(struct bpf_tcp_sock, sacked_out):
5667 BPF_TCP_SOCK_GET_COMMON(sacked_out);
5669 case offsetof(struct bpf_tcp_sock, bytes_received):
5670 BPF_TCP_SOCK_GET_COMMON(bytes_received);
5672 case offsetof(struct bpf_tcp_sock, bytes_acked):
5673 BPF_TCP_SOCK_GET_COMMON(bytes_acked);
5675 case offsetof(struct bpf_tcp_sock, dsack_dups):
5676 BPF_TCP_SOCK_GET_COMMON(dsack_dups);
5678 case offsetof(struct bpf_tcp_sock, delivered):
5679 BPF_TCP_SOCK_GET_COMMON(delivered);
5681 case offsetof(struct bpf_tcp_sock, delivered_ce):
5682 BPF_TCP_SOCK_GET_COMMON(delivered_ce);
5684 case offsetof(struct bpf_tcp_sock, icsk_retransmits):
5685 BPF_INET_SOCK_GET_COMMON(icsk_retransmits);
5689 return insn - insn_buf;
5692 BPF_CALL_1(bpf_tcp_sock, struct sock *, sk)
5694 if (sk_fullsock(sk) && sk->sk_protocol == IPPROTO_TCP)
5695 return (unsigned long)sk;
5697 return (unsigned long)NULL;
5700 const struct bpf_func_proto bpf_tcp_sock_proto = {
5701 .func = bpf_tcp_sock,
5703 .ret_type = RET_PTR_TO_TCP_SOCK_OR_NULL,
5704 .arg1_type = ARG_PTR_TO_SOCK_COMMON,
5707 BPF_CALL_1(bpf_get_listener_sock, struct sock *, sk)
5709 sk = sk_to_full_sk(sk);
5711 if (sk->sk_state == TCP_LISTEN && sock_flag(sk, SOCK_RCU_FREE))
5712 return (unsigned long)sk;
5714 return (unsigned long)NULL;
5717 static const struct bpf_func_proto bpf_get_listener_sock_proto = {
5718 .func = bpf_get_listener_sock,
5720 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
5721 .arg1_type = ARG_PTR_TO_SOCK_COMMON,
5724 BPF_CALL_1(bpf_skb_ecn_set_ce, struct sk_buff *, skb)
5726 unsigned int iphdr_len;
5728 if (skb->protocol == cpu_to_be16(ETH_P_IP))
5729 iphdr_len = sizeof(struct iphdr);
5730 else if (skb->protocol == cpu_to_be16(ETH_P_IPV6))
5731 iphdr_len = sizeof(struct ipv6hdr);
5735 if (skb_headlen(skb) < iphdr_len)
5738 if (skb_cloned(skb) && !skb_clone_writable(skb, iphdr_len))
5741 return INET_ECN_set_ce(skb);
5744 bool bpf_xdp_sock_is_valid_access(int off, int size, enum bpf_access_type type,
5745 struct bpf_insn_access_aux *info)
5747 if (off < 0 || off >= offsetofend(struct bpf_xdp_sock, queue_id))
5750 if (off % size != 0)
5755 return size == sizeof(__u32);
5759 u32 bpf_xdp_sock_convert_ctx_access(enum bpf_access_type type,
5760 const struct bpf_insn *si,
5761 struct bpf_insn *insn_buf,
5762 struct bpf_prog *prog, u32 *target_size)
5764 struct bpf_insn *insn = insn_buf;
5766 #define BPF_XDP_SOCK_GET(FIELD) \
5768 BUILD_BUG_ON(FIELD_SIZEOF(struct xdp_sock, FIELD) > \
5769 FIELD_SIZEOF(struct bpf_xdp_sock, FIELD)); \
5770 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_sock, FIELD),\
5771 si->dst_reg, si->src_reg, \
5772 offsetof(struct xdp_sock, FIELD)); \
5776 case offsetof(struct bpf_xdp_sock, queue_id):
5777 BPF_XDP_SOCK_GET(queue_id);
5781 return insn - insn_buf;
5784 static const struct bpf_func_proto bpf_skb_ecn_set_ce_proto = {
5785 .func = bpf_skb_ecn_set_ce,
5787 .ret_type = RET_INTEGER,
5788 .arg1_type = ARG_PTR_TO_CTX,
5791 BPF_CALL_5(bpf_tcp_check_syncookie, struct sock *, sk, void *, iph, u32, iph_len,
5792 struct tcphdr *, th, u32, th_len)
5794 #ifdef CONFIG_SYN_COOKIES
5798 if (unlikely(th_len < sizeof(*th)))
5801 /* sk_listener() allows TCP_NEW_SYN_RECV, which makes no sense here. */
5802 if (sk->sk_protocol != IPPROTO_TCP || sk->sk_state != TCP_LISTEN)
5805 if (!sock_net(sk)->ipv4.sysctl_tcp_syncookies)
5808 if (!th->ack || th->rst || th->syn)
5811 if (tcp_synq_no_recent_overflow(sk))
5814 cookie = ntohl(th->ack_seq) - 1;
5816 switch (sk->sk_family) {
5818 if (unlikely(iph_len < sizeof(struct iphdr)))
5821 ret = __cookie_v4_check((struct iphdr *)iph, th, cookie);
5824 #if IS_BUILTIN(CONFIG_IPV6)
5826 if (unlikely(iph_len < sizeof(struct ipv6hdr)))
5829 ret = __cookie_v6_check((struct ipv6hdr *)iph, th, cookie);
5831 #endif /* CONFIG_IPV6 */
5834 return -EPROTONOSUPPORT;
5846 static const struct bpf_func_proto bpf_tcp_check_syncookie_proto = {
5847 .func = bpf_tcp_check_syncookie,
5850 .ret_type = RET_INTEGER,
5851 .arg1_type = ARG_PTR_TO_SOCK_COMMON,
5852 .arg2_type = ARG_PTR_TO_MEM,
5853 .arg3_type = ARG_CONST_SIZE,
5854 .arg4_type = ARG_PTR_TO_MEM,
5855 .arg5_type = ARG_CONST_SIZE,
5858 BPF_CALL_5(bpf_tcp_gen_syncookie, struct sock *, sk, void *, iph, u32, iph_len,
5859 struct tcphdr *, th, u32, th_len)
5861 #ifdef CONFIG_SYN_COOKIES
5865 if (unlikely(th_len < sizeof(*th) || th_len != th->doff * 4))
5868 if (sk->sk_protocol != IPPROTO_TCP || sk->sk_state != TCP_LISTEN)
5871 if (!sock_net(sk)->ipv4.sysctl_tcp_syncookies)
5874 if (!th->syn || th->ack || th->fin || th->rst)
5877 if (unlikely(iph_len < sizeof(struct iphdr)))
5880 /* Both struct iphdr and struct ipv6hdr have the version field at the
5881 * same offset so we can cast to the shorter header (struct iphdr).
5883 switch (((struct iphdr *)iph)->version) {
5885 if (sk->sk_family == AF_INET6 && sk->sk_ipv6only)
5888 mss = tcp_v4_get_syncookie(sk, iph, th, &cookie);
5891 #if IS_BUILTIN(CONFIG_IPV6)
5893 if (unlikely(iph_len < sizeof(struct ipv6hdr)))
5896 if (sk->sk_family != AF_INET6)
5899 mss = tcp_v6_get_syncookie(sk, iph, th, &cookie);
5901 #endif /* CONFIG_IPV6 */
5904 return -EPROTONOSUPPORT;
5909 return cookie | ((u64)mss << 32);
5912 #endif /* CONFIG_SYN_COOKIES */
5915 static const struct bpf_func_proto bpf_tcp_gen_syncookie_proto = {
5916 .func = bpf_tcp_gen_syncookie,
5917 .gpl_only = true, /* __cookie_v*_init_sequence() is GPL */
5919 .ret_type = RET_INTEGER,
5920 .arg1_type = ARG_PTR_TO_SOCK_COMMON,
5921 .arg2_type = ARG_PTR_TO_MEM,
5922 .arg3_type = ARG_CONST_SIZE,
5923 .arg4_type = ARG_PTR_TO_MEM,
5924 .arg5_type = ARG_CONST_SIZE,
5927 #endif /* CONFIG_INET */
5929 bool bpf_helper_changes_pkt_data(void *func)
5931 if (func == bpf_skb_vlan_push ||
5932 func == bpf_skb_vlan_pop ||
5933 func == bpf_skb_store_bytes ||
5934 func == bpf_skb_change_proto ||
5935 func == bpf_skb_change_head ||
5936 func == sk_skb_change_head ||
5937 func == bpf_skb_change_tail ||
5938 func == sk_skb_change_tail ||
5939 func == bpf_skb_adjust_room ||
5940 func == bpf_skb_pull_data ||
5941 func == sk_skb_pull_data ||
5942 func == bpf_clone_redirect ||
5943 func == bpf_l3_csum_replace ||
5944 func == bpf_l4_csum_replace ||
5945 func == bpf_xdp_adjust_head ||
5946 func == bpf_xdp_adjust_meta ||
5947 func == bpf_msg_pull_data ||
5948 func == bpf_msg_push_data ||
5949 func == bpf_msg_pop_data ||
5950 func == bpf_xdp_adjust_tail ||
5951 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
5952 func == bpf_lwt_seg6_store_bytes ||
5953 func == bpf_lwt_seg6_adjust_srh ||
5954 func == bpf_lwt_seg6_action ||
5956 func == bpf_lwt_in_push_encap ||
5957 func == bpf_lwt_xmit_push_encap)
5963 static const struct bpf_func_proto *
5964 bpf_base_func_proto(enum bpf_func_id func_id)
5967 case BPF_FUNC_map_lookup_elem:
5968 return &bpf_map_lookup_elem_proto;
5969 case BPF_FUNC_map_update_elem:
5970 return &bpf_map_update_elem_proto;
5971 case BPF_FUNC_map_delete_elem:
5972 return &bpf_map_delete_elem_proto;
5973 case BPF_FUNC_map_push_elem:
5974 return &bpf_map_push_elem_proto;
5975 case BPF_FUNC_map_pop_elem:
5976 return &bpf_map_pop_elem_proto;
5977 case BPF_FUNC_map_peek_elem:
5978 return &bpf_map_peek_elem_proto;
5979 case BPF_FUNC_get_prandom_u32:
5980 return &bpf_get_prandom_u32_proto;
5981 case BPF_FUNC_get_smp_processor_id:
5982 return &bpf_get_raw_smp_processor_id_proto;
5983 case BPF_FUNC_get_numa_node_id:
5984 return &bpf_get_numa_node_id_proto;
5985 case BPF_FUNC_tail_call:
5986 return &bpf_tail_call_proto;
5987 case BPF_FUNC_ktime_get_ns:
5988 return &bpf_ktime_get_ns_proto;
5993 if (!capable(CAP_SYS_ADMIN))
5997 case BPF_FUNC_spin_lock:
5998 return &bpf_spin_lock_proto;
5999 case BPF_FUNC_spin_unlock:
6000 return &bpf_spin_unlock_proto;
6001 case BPF_FUNC_trace_printk:
6002 return bpf_get_trace_printk_proto();
6008 static const struct bpf_func_proto *
6009 sock_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6012 /* inet and inet6 sockets are created in a process
6013 * context so there is always a valid uid/gid
6015 case BPF_FUNC_get_current_uid_gid:
6016 return &bpf_get_current_uid_gid_proto;
6017 case BPF_FUNC_get_local_storage:
6018 return &bpf_get_local_storage_proto;
6020 return bpf_base_func_proto(func_id);
6024 static const struct bpf_func_proto *
6025 sock_addr_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6028 /* inet and inet6 sockets are created in a process
6029 * context so there is always a valid uid/gid
6031 case BPF_FUNC_get_current_uid_gid:
6032 return &bpf_get_current_uid_gid_proto;
6034 switch (prog->expected_attach_type) {
6035 case BPF_CGROUP_INET4_CONNECT:
6036 case BPF_CGROUP_INET6_CONNECT:
6037 return &bpf_bind_proto;
6041 case BPF_FUNC_get_socket_cookie:
6042 return &bpf_get_socket_cookie_sock_addr_proto;
6043 case BPF_FUNC_get_local_storage:
6044 return &bpf_get_local_storage_proto;
6046 case BPF_FUNC_sk_lookup_tcp:
6047 return &bpf_sock_addr_sk_lookup_tcp_proto;
6048 case BPF_FUNC_sk_lookup_udp:
6049 return &bpf_sock_addr_sk_lookup_udp_proto;
6050 case BPF_FUNC_sk_release:
6051 return &bpf_sk_release_proto;
6052 case BPF_FUNC_skc_lookup_tcp:
6053 return &bpf_sock_addr_skc_lookup_tcp_proto;
6054 #endif /* CONFIG_INET */
6055 case BPF_FUNC_sk_storage_get:
6056 return &bpf_sk_storage_get_proto;
6057 case BPF_FUNC_sk_storage_delete:
6058 return &bpf_sk_storage_delete_proto;
6060 return bpf_base_func_proto(func_id);
6064 static const struct bpf_func_proto *
6065 sk_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6068 case BPF_FUNC_skb_load_bytes:
6069 return &bpf_skb_load_bytes_proto;
6070 case BPF_FUNC_skb_load_bytes_relative:
6071 return &bpf_skb_load_bytes_relative_proto;
6072 case BPF_FUNC_get_socket_cookie:
6073 return &bpf_get_socket_cookie_proto;
6074 case BPF_FUNC_get_socket_uid:
6075 return &bpf_get_socket_uid_proto;
6076 case BPF_FUNC_perf_event_output:
6077 return &bpf_skb_event_output_proto;
6079 return bpf_base_func_proto(func_id);
6083 const struct bpf_func_proto bpf_sk_storage_get_proto __weak;
6084 const struct bpf_func_proto bpf_sk_storage_delete_proto __weak;
6086 static const struct bpf_func_proto *
6087 cg_skb_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6090 case BPF_FUNC_get_local_storage:
6091 return &bpf_get_local_storage_proto;
6092 case BPF_FUNC_sk_fullsock:
6093 return &bpf_sk_fullsock_proto;
6094 case BPF_FUNC_sk_storage_get:
6095 return &bpf_sk_storage_get_proto;
6096 case BPF_FUNC_sk_storage_delete:
6097 return &bpf_sk_storage_delete_proto;
6098 case BPF_FUNC_perf_event_output:
6099 return &bpf_skb_event_output_proto;
6100 #ifdef CONFIG_SOCK_CGROUP_DATA
6101 case BPF_FUNC_skb_cgroup_id:
6102 return &bpf_skb_cgroup_id_proto;
6105 case BPF_FUNC_tcp_sock:
6106 return &bpf_tcp_sock_proto;
6107 case BPF_FUNC_get_listener_sock:
6108 return &bpf_get_listener_sock_proto;
6109 case BPF_FUNC_skb_ecn_set_ce:
6110 return &bpf_skb_ecn_set_ce_proto;
6113 return sk_filter_func_proto(func_id, prog);
6117 static const struct bpf_func_proto *
6118 tc_cls_act_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6121 case BPF_FUNC_skb_store_bytes:
6122 return &bpf_skb_store_bytes_proto;
6123 case BPF_FUNC_skb_load_bytes:
6124 return &bpf_skb_load_bytes_proto;
6125 case BPF_FUNC_skb_load_bytes_relative:
6126 return &bpf_skb_load_bytes_relative_proto;
6127 case BPF_FUNC_skb_pull_data:
6128 return &bpf_skb_pull_data_proto;
6129 case BPF_FUNC_csum_diff:
6130 return &bpf_csum_diff_proto;
6131 case BPF_FUNC_csum_update:
6132 return &bpf_csum_update_proto;
6133 case BPF_FUNC_l3_csum_replace:
6134 return &bpf_l3_csum_replace_proto;
6135 case BPF_FUNC_l4_csum_replace:
6136 return &bpf_l4_csum_replace_proto;
6137 case BPF_FUNC_clone_redirect:
6138 return &bpf_clone_redirect_proto;
6139 case BPF_FUNC_get_cgroup_classid:
6140 return &bpf_get_cgroup_classid_proto;
6141 case BPF_FUNC_skb_vlan_push:
6142 return &bpf_skb_vlan_push_proto;
6143 case BPF_FUNC_skb_vlan_pop:
6144 return &bpf_skb_vlan_pop_proto;
6145 case BPF_FUNC_skb_change_proto:
6146 return &bpf_skb_change_proto_proto;
6147 case BPF_FUNC_skb_change_type:
6148 return &bpf_skb_change_type_proto;
6149 case BPF_FUNC_skb_adjust_room:
6150 return &bpf_skb_adjust_room_proto;
6151 case BPF_FUNC_skb_change_tail:
6152 return &bpf_skb_change_tail_proto;
6153 case BPF_FUNC_skb_get_tunnel_key:
6154 return &bpf_skb_get_tunnel_key_proto;
6155 case BPF_FUNC_skb_set_tunnel_key:
6156 return bpf_get_skb_set_tunnel_proto(func_id);
6157 case BPF_FUNC_skb_get_tunnel_opt:
6158 return &bpf_skb_get_tunnel_opt_proto;
6159 case BPF_FUNC_skb_set_tunnel_opt:
6160 return bpf_get_skb_set_tunnel_proto(func_id);
6161 case BPF_FUNC_redirect:
6162 return &bpf_redirect_proto;
6163 case BPF_FUNC_get_route_realm:
6164 return &bpf_get_route_realm_proto;
6165 case BPF_FUNC_get_hash_recalc:
6166 return &bpf_get_hash_recalc_proto;
6167 case BPF_FUNC_set_hash_invalid:
6168 return &bpf_set_hash_invalid_proto;
6169 case BPF_FUNC_set_hash:
6170 return &bpf_set_hash_proto;
6171 case BPF_FUNC_perf_event_output:
6172 return &bpf_skb_event_output_proto;
6173 case BPF_FUNC_get_smp_processor_id:
6174 return &bpf_get_smp_processor_id_proto;
6175 case BPF_FUNC_skb_under_cgroup:
6176 return &bpf_skb_under_cgroup_proto;
6177 case BPF_FUNC_get_socket_cookie:
6178 return &bpf_get_socket_cookie_proto;
6179 case BPF_FUNC_get_socket_uid:
6180 return &bpf_get_socket_uid_proto;
6181 case BPF_FUNC_fib_lookup:
6182 return &bpf_skb_fib_lookup_proto;
6183 case BPF_FUNC_sk_fullsock:
6184 return &bpf_sk_fullsock_proto;
6185 case BPF_FUNC_sk_storage_get:
6186 return &bpf_sk_storage_get_proto;
6187 case BPF_FUNC_sk_storage_delete:
6188 return &bpf_sk_storage_delete_proto;
6190 case BPF_FUNC_skb_get_xfrm_state:
6191 return &bpf_skb_get_xfrm_state_proto;
6193 #ifdef CONFIG_SOCK_CGROUP_DATA
6194 case BPF_FUNC_skb_cgroup_id:
6195 return &bpf_skb_cgroup_id_proto;
6196 case BPF_FUNC_skb_ancestor_cgroup_id:
6197 return &bpf_skb_ancestor_cgroup_id_proto;
6200 case BPF_FUNC_sk_lookup_tcp:
6201 return &bpf_sk_lookup_tcp_proto;
6202 case BPF_FUNC_sk_lookup_udp:
6203 return &bpf_sk_lookup_udp_proto;
6204 case BPF_FUNC_sk_release:
6205 return &bpf_sk_release_proto;
6206 case BPF_FUNC_tcp_sock:
6207 return &bpf_tcp_sock_proto;
6208 case BPF_FUNC_get_listener_sock:
6209 return &bpf_get_listener_sock_proto;
6210 case BPF_FUNC_skc_lookup_tcp:
6211 return &bpf_skc_lookup_tcp_proto;
6212 case BPF_FUNC_tcp_check_syncookie:
6213 return &bpf_tcp_check_syncookie_proto;
6214 case BPF_FUNC_skb_ecn_set_ce:
6215 return &bpf_skb_ecn_set_ce_proto;
6216 case BPF_FUNC_tcp_gen_syncookie:
6217 return &bpf_tcp_gen_syncookie_proto;
6220 return bpf_base_func_proto(func_id);
6224 static const struct bpf_func_proto *
6225 xdp_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6228 case BPF_FUNC_perf_event_output:
6229 return &bpf_xdp_event_output_proto;
6230 case BPF_FUNC_get_smp_processor_id:
6231 return &bpf_get_smp_processor_id_proto;
6232 case BPF_FUNC_csum_diff:
6233 return &bpf_csum_diff_proto;
6234 case BPF_FUNC_xdp_adjust_head:
6235 return &bpf_xdp_adjust_head_proto;
6236 case BPF_FUNC_xdp_adjust_meta:
6237 return &bpf_xdp_adjust_meta_proto;
6238 case BPF_FUNC_redirect:
6239 return &bpf_xdp_redirect_proto;
6240 case BPF_FUNC_redirect_map:
6241 return &bpf_xdp_redirect_map_proto;
6242 case BPF_FUNC_xdp_adjust_tail:
6243 return &bpf_xdp_adjust_tail_proto;
6244 case BPF_FUNC_fib_lookup:
6245 return &bpf_xdp_fib_lookup_proto;
6247 case BPF_FUNC_sk_lookup_udp:
6248 return &bpf_xdp_sk_lookup_udp_proto;
6249 case BPF_FUNC_sk_lookup_tcp:
6250 return &bpf_xdp_sk_lookup_tcp_proto;
6251 case BPF_FUNC_sk_release:
6252 return &bpf_sk_release_proto;
6253 case BPF_FUNC_skc_lookup_tcp:
6254 return &bpf_xdp_skc_lookup_tcp_proto;
6255 case BPF_FUNC_tcp_check_syncookie:
6256 return &bpf_tcp_check_syncookie_proto;
6257 case BPF_FUNC_tcp_gen_syncookie:
6258 return &bpf_tcp_gen_syncookie_proto;
6261 return bpf_base_func_proto(func_id);
6265 const struct bpf_func_proto bpf_sock_map_update_proto __weak;
6266 const struct bpf_func_proto bpf_sock_hash_update_proto __weak;
6268 static const struct bpf_func_proto *
6269 sock_ops_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6272 case BPF_FUNC_setsockopt:
6273 return &bpf_setsockopt_proto;
6274 case BPF_FUNC_getsockopt:
6275 return &bpf_getsockopt_proto;
6276 case BPF_FUNC_sock_ops_cb_flags_set:
6277 return &bpf_sock_ops_cb_flags_set_proto;
6278 case BPF_FUNC_sock_map_update:
6279 return &bpf_sock_map_update_proto;
6280 case BPF_FUNC_sock_hash_update:
6281 return &bpf_sock_hash_update_proto;
6282 case BPF_FUNC_get_socket_cookie:
6283 return &bpf_get_socket_cookie_sock_ops_proto;
6284 case BPF_FUNC_get_local_storage:
6285 return &bpf_get_local_storage_proto;
6286 case BPF_FUNC_perf_event_output:
6287 return &bpf_sockopt_event_output_proto;
6288 case BPF_FUNC_sk_storage_get:
6289 return &bpf_sk_storage_get_proto;
6290 case BPF_FUNC_sk_storage_delete:
6291 return &bpf_sk_storage_delete_proto;
6293 case BPF_FUNC_tcp_sock:
6294 return &bpf_tcp_sock_proto;
6295 #endif /* CONFIG_INET */
6297 return bpf_base_func_proto(func_id);
6301 const struct bpf_func_proto bpf_msg_redirect_map_proto __weak;
6302 const struct bpf_func_proto bpf_msg_redirect_hash_proto __weak;
6304 static const struct bpf_func_proto *
6305 sk_msg_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6308 case BPF_FUNC_msg_redirect_map:
6309 return &bpf_msg_redirect_map_proto;
6310 case BPF_FUNC_msg_redirect_hash:
6311 return &bpf_msg_redirect_hash_proto;
6312 case BPF_FUNC_msg_apply_bytes:
6313 return &bpf_msg_apply_bytes_proto;
6314 case BPF_FUNC_msg_cork_bytes:
6315 return &bpf_msg_cork_bytes_proto;
6316 case BPF_FUNC_msg_pull_data:
6317 return &bpf_msg_pull_data_proto;
6318 case BPF_FUNC_msg_push_data:
6319 return &bpf_msg_push_data_proto;
6320 case BPF_FUNC_msg_pop_data:
6321 return &bpf_msg_pop_data_proto;
6323 return bpf_base_func_proto(func_id);
6327 const struct bpf_func_proto bpf_sk_redirect_map_proto __weak;
6328 const struct bpf_func_proto bpf_sk_redirect_hash_proto __weak;
6330 static const struct bpf_func_proto *
6331 sk_skb_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6334 case BPF_FUNC_skb_store_bytes:
6335 return &bpf_skb_store_bytes_proto;
6336 case BPF_FUNC_skb_load_bytes:
6337 return &bpf_skb_load_bytes_proto;
6338 case BPF_FUNC_skb_pull_data:
6339 return &sk_skb_pull_data_proto;
6340 case BPF_FUNC_skb_change_tail:
6341 return &sk_skb_change_tail_proto;
6342 case BPF_FUNC_skb_change_head:
6343 return &sk_skb_change_head_proto;
6344 case BPF_FUNC_get_socket_cookie:
6345 return &bpf_get_socket_cookie_proto;
6346 case BPF_FUNC_get_socket_uid:
6347 return &bpf_get_socket_uid_proto;
6348 case BPF_FUNC_sk_redirect_map:
6349 return &bpf_sk_redirect_map_proto;
6350 case BPF_FUNC_sk_redirect_hash:
6351 return &bpf_sk_redirect_hash_proto;
6352 case BPF_FUNC_perf_event_output:
6353 return &bpf_skb_event_output_proto;
6355 case BPF_FUNC_sk_lookup_tcp:
6356 return &bpf_sk_lookup_tcp_proto;
6357 case BPF_FUNC_sk_lookup_udp:
6358 return &bpf_sk_lookup_udp_proto;
6359 case BPF_FUNC_sk_release:
6360 return &bpf_sk_release_proto;
6361 case BPF_FUNC_skc_lookup_tcp:
6362 return &bpf_skc_lookup_tcp_proto;
6365 return bpf_base_func_proto(func_id);
6369 static const struct bpf_func_proto *
6370 flow_dissector_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6373 case BPF_FUNC_skb_load_bytes:
6374 return &bpf_flow_dissector_load_bytes_proto;
6376 return bpf_base_func_proto(func_id);
6380 static const struct bpf_func_proto *
6381 lwt_out_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6384 case BPF_FUNC_skb_load_bytes:
6385 return &bpf_skb_load_bytes_proto;
6386 case BPF_FUNC_skb_pull_data:
6387 return &bpf_skb_pull_data_proto;
6388 case BPF_FUNC_csum_diff:
6389 return &bpf_csum_diff_proto;
6390 case BPF_FUNC_get_cgroup_classid:
6391 return &bpf_get_cgroup_classid_proto;
6392 case BPF_FUNC_get_route_realm:
6393 return &bpf_get_route_realm_proto;
6394 case BPF_FUNC_get_hash_recalc:
6395 return &bpf_get_hash_recalc_proto;
6396 case BPF_FUNC_perf_event_output:
6397 return &bpf_skb_event_output_proto;
6398 case BPF_FUNC_get_smp_processor_id:
6399 return &bpf_get_smp_processor_id_proto;
6400 case BPF_FUNC_skb_under_cgroup:
6401 return &bpf_skb_under_cgroup_proto;
6403 return bpf_base_func_proto(func_id);
6407 static const struct bpf_func_proto *
6408 lwt_in_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6411 case BPF_FUNC_lwt_push_encap:
6412 return &bpf_lwt_in_push_encap_proto;
6414 return lwt_out_func_proto(func_id, prog);
6418 static const struct bpf_func_proto *
6419 lwt_xmit_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6422 case BPF_FUNC_skb_get_tunnel_key:
6423 return &bpf_skb_get_tunnel_key_proto;
6424 case BPF_FUNC_skb_set_tunnel_key:
6425 return bpf_get_skb_set_tunnel_proto(func_id);
6426 case BPF_FUNC_skb_get_tunnel_opt:
6427 return &bpf_skb_get_tunnel_opt_proto;
6428 case BPF_FUNC_skb_set_tunnel_opt:
6429 return bpf_get_skb_set_tunnel_proto(func_id);
6430 case BPF_FUNC_redirect:
6431 return &bpf_redirect_proto;
6432 case BPF_FUNC_clone_redirect:
6433 return &bpf_clone_redirect_proto;
6434 case BPF_FUNC_skb_change_tail:
6435 return &bpf_skb_change_tail_proto;
6436 case BPF_FUNC_skb_change_head:
6437 return &bpf_skb_change_head_proto;
6438 case BPF_FUNC_skb_store_bytes:
6439 return &bpf_skb_store_bytes_proto;
6440 case BPF_FUNC_csum_update:
6441 return &bpf_csum_update_proto;
6442 case BPF_FUNC_l3_csum_replace:
6443 return &bpf_l3_csum_replace_proto;
6444 case BPF_FUNC_l4_csum_replace:
6445 return &bpf_l4_csum_replace_proto;
6446 case BPF_FUNC_set_hash_invalid:
6447 return &bpf_set_hash_invalid_proto;
6448 case BPF_FUNC_lwt_push_encap:
6449 return &bpf_lwt_xmit_push_encap_proto;
6451 return lwt_out_func_proto(func_id, prog);
6455 static const struct bpf_func_proto *
6456 lwt_seg6local_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6459 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
6460 case BPF_FUNC_lwt_seg6_store_bytes:
6461 return &bpf_lwt_seg6_store_bytes_proto;
6462 case BPF_FUNC_lwt_seg6_action:
6463 return &bpf_lwt_seg6_action_proto;
6464 case BPF_FUNC_lwt_seg6_adjust_srh:
6465 return &bpf_lwt_seg6_adjust_srh_proto;
6468 return lwt_out_func_proto(func_id, prog);
6472 static bool bpf_skb_is_valid_access(int off, int size, enum bpf_access_type type,
6473 const struct bpf_prog *prog,
6474 struct bpf_insn_access_aux *info)
6476 const int size_default = sizeof(__u32);
6478 if (off < 0 || off >= sizeof(struct __sk_buff))
6481 /* The verifier guarantees that size > 0. */
6482 if (off % size != 0)
6486 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
6487 if (off + size > offsetofend(struct __sk_buff, cb[4]))
6490 case bpf_ctx_range_till(struct __sk_buff, remote_ip6[0], remote_ip6[3]):
6491 case bpf_ctx_range_till(struct __sk_buff, local_ip6[0], local_ip6[3]):
6492 case bpf_ctx_range_till(struct __sk_buff, remote_ip4, remote_ip4):
6493 case bpf_ctx_range_till(struct __sk_buff, local_ip4, local_ip4):
6494 case bpf_ctx_range(struct __sk_buff, data):
6495 case bpf_ctx_range(struct __sk_buff, data_meta):
6496 case bpf_ctx_range(struct __sk_buff, data_end):
6497 if (size != size_default)
6500 case bpf_ctx_range_ptr(struct __sk_buff, flow_keys):
6502 case bpf_ctx_range(struct __sk_buff, tstamp):
6503 if (size != sizeof(__u64))
6506 case offsetof(struct __sk_buff, sk):
6507 if (type == BPF_WRITE || size != sizeof(__u64))
6509 info->reg_type = PTR_TO_SOCK_COMMON_OR_NULL;
6512 /* Only narrow read access allowed for now. */
6513 if (type == BPF_WRITE) {
6514 if (size != size_default)
6517 bpf_ctx_record_field_size(info, size_default);
6518 if (!bpf_ctx_narrow_access_ok(off, size, size_default))
6526 static bool sk_filter_is_valid_access(int off, int size,
6527 enum bpf_access_type type,
6528 const struct bpf_prog *prog,
6529 struct bpf_insn_access_aux *info)
6532 case bpf_ctx_range(struct __sk_buff, tc_classid):
6533 case bpf_ctx_range(struct __sk_buff, data):
6534 case bpf_ctx_range(struct __sk_buff, data_meta):
6535 case bpf_ctx_range(struct __sk_buff, data_end):
6536 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
6537 case bpf_ctx_range(struct __sk_buff, tstamp):
6538 case bpf_ctx_range(struct __sk_buff, wire_len):
6542 if (type == BPF_WRITE) {
6544 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
6551 return bpf_skb_is_valid_access(off, size, type, prog, info);
6554 static bool cg_skb_is_valid_access(int off, int size,
6555 enum bpf_access_type type,
6556 const struct bpf_prog *prog,
6557 struct bpf_insn_access_aux *info)
6560 case bpf_ctx_range(struct __sk_buff, tc_classid):
6561 case bpf_ctx_range(struct __sk_buff, data_meta):
6562 case bpf_ctx_range(struct __sk_buff, wire_len):
6564 case bpf_ctx_range(struct __sk_buff, data):
6565 case bpf_ctx_range(struct __sk_buff, data_end):
6566 if (!capable(CAP_SYS_ADMIN))
6571 if (type == BPF_WRITE) {
6573 case bpf_ctx_range(struct __sk_buff, mark):
6574 case bpf_ctx_range(struct __sk_buff, priority):
6575 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
6577 case bpf_ctx_range(struct __sk_buff, tstamp):
6578 if (!capable(CAP_SYS_ADMIN))
6587 case bpf_ctx_range(struct __sk_buff, data):
6588 info->reg_type = PTR_TO_PACKET;
6590 case bpf_ctx_range(struct __sk_buff, data_end):
6591 info->reg_type = PTR_TO_PACKET_END;
6595 return bpf_skb_is_valid_access(off, size, type, prog, info);
6598 static bool lwt_is_valid_access(int off, int size,
6599 enum bpf_access_type type,
6600 const struct bpf_prog *prog,
6601 struct bpf_insn_access_aux *info)
6604 case bpf_ctx_range(struct __sk_buff, tc_classid):
6605 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
6606 case bpf_ctx_range(struct __sk_buff, data_meta):
6607 case bpf_ctx_range(struct __sk_buff, tstamp):
6608 case bpf_ctx_range(struct __sk_buff, wire_len):
6612 if (type == BPF_WRITE) {
6614 case bpf_ctx_range(struct __sk_buff, mark):
6615 case bpf_ctx_range(struct __sk_buff, priority):
6616 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
6624 case bpf_ctx_range(struct __sk_buff, data):
6625 info->reg_type = PTR_TO_PACKET;
6627 case bpf_ctx_range(struct __sk_buff, data_end):
6628 info->reg_type = PTR_TO_PACKET_END;
6632 return bpf_skb_is_valid_access(off, size, type, prog, info);
6635 /* Attach type specific accesses */
6636 static bool __sock_filter_check_attach_type(int off,
6637 enum bpf_access_type access_type,
6638 enum bpf_attach_type attach_type)
6641 case offsetof(struct bpf_sock, bound_dev_if):
6642 case offsetof(struct bpf_sock, mark):
6643 case offsetof(struct bpf_sock, priority):
6644 switch (attach_type) {
6645 case BPF_CGROUP_INET_SOCK_CREATE:
6650 case bpf_ctx_range(struct bpf_sock, src_ip4):
6651 switch (attach_type) {
6652 case BPF_CGROUP_INET4_POST_BIND:
6657 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
6658 switch (attach_type) {
6659 case BPF_CGROUP_INET6_POST_BIND:
6664 case bpf_ctx_range(struct bpf_sock, src_port):
6665 switch (attach_type) {
6666 case BPF_CGROUP_INET4_POST_BIND:
6667 case BPF_CGROUP_INET6_POST_BIND:
6674 return access_type == BPF_READ;
6679 bool bpf_sock_common_is_valid_access(int off, int size,
6680 enum bpf_access_type type,
6681 struct bpf_insn_access_aux *info)
6684 case bpf_ctx_range_till(struct bpf_sock, type, priority):
6687 return bpf_sock_is_valid_access(off, size, type, info);
6691 bool bpf_sock_is_valid_access(int off, int size, enum bpf_access_type type,
6692 struct bpf_insn_access_aux *info)
6694 const int size_default = sizeof(__u32);
6696 if (off < 0 || off >= sizeof(struct bpf_sock))
6698 if (off % size != 0)
6702 case offsetof(struct bpf_sock, state):
6703 case offsetof(struct bpf_sock, family):
6704 case offsetof(struct bpf_sock, type):
6705 case offsetof(struct bpf_sock, protocol):
6706 case offsetof(struct bpf_sock, dst_port):
6707 case offsetof(struct bpf_sock, src_port):
6708 case bpf_ctx_range(struct bpf_sock, src_ip4):
6709 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
6710 case bpf_ctx_range(struct bpf_sock, dst_ip4):
6711 case bpf_ctx_range_till(struct bpf_sock, dst_ip6[0], dst_ip6[3]):
6712 bpf_ctx_record_field_size(info, size_default);
6713 return bpf_ctx_narrow_access_ok(off, size, size_default);
6716 return size == size_default;
6719 static bool sock_filter_is_valid_access(int off, int size,
6720 enum bpf_access_type type,
6721 const struct bpf_prog *prog,
6722 struct bpf_insn_access_aux *info)
6724 if (!bpf_sock_is_valid_access(off, size, type, info))
6726 return __sock_filter_check_attach_type(off, type,
6727 prog->expected_attach_type);
6730 static int bpf_noop_prologue(struct bpf_insn *insn_buf, bool direct_write,
6731 const struct bpf_prog *prog)
6733 /* Neither direct read nor direct write requires any preliminary
6739 static int bpf_unclone_prologue(struct bpf_insn *insn_buf, bool direct_write,
6740 const struct bpf_prog *prog, int drop_verdict)
6742 struct bpf_insn *insn = insn_buf;
6747 /* if (!skb->cloned)
6750 * (Fast-path, otherwise approximation that we might be
6751 * a clone, do the rest in helper.)
6753 *insn++ = BPF_LDX_MEM(BPF_B, BPF_REG_6, BPF_REG_1, CLONED_OFFSET());
6754 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_6, CLONED_MASK);
6755 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_6, 0, 7);
6757 /* ret = bpf_skb_pull_data(skb, 0); */
6758 *insn++ = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1);
6759 *insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_2, BPF_REG_2);
6760 *insn++ = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
6761 BPF_FUNC_skb_pull_data);
6764 * return TC_ACT_SHOT;
6766 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2);
6767 *insn++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_0, drop_verdict);
6768 *insn++ = BPF_EXIT_INSN();
6771 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_6);
6773 *insn++ = prog->insnsi[0];
6775 return insn - insn_buf;
6778 static int bpf_gen_ld_abs(const struct bpf_insn *orig,
6779 struct bpf_insn *insn_buf)
6781 bool indirect = BPF_MODE(orig->code) == BPF_IND;
6782 struct bpf_insn *insn = insn_buf;
6784 /* We're guaranteed here that CTX is in R6. */
6785 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_CTX);
6787 *insn++ = BPF_MOV64_IMM(BPF_REG_2, orig->imm);
6789 *insn++ = BPF_MOV64_REG(BPF_REG_2, orig->src_reg);
6791 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, orig->imm);
6794 switch (BPF_SIZE(orig->code)) {
6796 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_8_no_cache);
6799 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_16_no_cache);
6802 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_32_no_cache);
6806 *insn++ = BPF_JMP_IMM(BPF_JSGE, BPF_REG_0, 0, 2);
6807 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_0, BPF_REG_0);
6808 *insn++ = BPF_EXIT_INSN();
6810 return insn - insn_buf;
6813 static int tc_cls_act_prologue(struct bpf_insn *insn_buf, bool direct_write,
6814 const struct bpf_prog *prog)
6816 return bpf_unclone_prologue(insn_buf, direct_write, prog, TC_ACT_SHOT);
6819 static bool tc_cls_act_is_valid_access(int off, int size,
6820 enum bpf_access_type type,
6821 const struct bpf_prog *prog,
6822 struct bpf_insn_access_aux *info)
6824 if (type == BPF_WRITE) {
6826 case bpf_ctx_range(struct __sk_buff, mark):
6827 case bpf_ctx_range(struct __sk_buff, tc_index):
6828 case bpf_ctx_range(struct __sk_buff, priority):
6829 case bpf_ctx_range(struct __sk_buff, tc_classid):
6830 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
6831 case bpf_ctx_range(struct __sk_buff, tstamp):
6832 case bpf_ctx_range(struct __sk_buff, queue_mapping):
6840 case bpf_ctx_range(struct __sk_buff, data):
6841 info->reg_type = PTR_TO_PACKET;
6843 case bpf_ctx_range(struct __sk_buff, data_meta):
6844 info->reg_type = PTR_TO_PACKET_META;
6846 case bpf_ctx_range(struct __sk_buff, data_end):
6847 info->reg_type = PTR_TO_PACKET_END;
6849 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
6853 return bpf_skb_is_valid_access(off, size, type, prog, info);
6856 static bool __is_valid_xdp_access(int off, int size)
6858 if (off < 0 || off >= sizeof(struct xdp_md))
6860 if (off % size != 0)
6862 if (size != sizeof(__u32))
6868 static bool xdp_is_valid_access(int off, int size,
6869 enum bpf_access_type type,
6870 const struct bpf_prog *prog,
6871 struct bpf_insn_access_aux *info)
6873 if (type == BPF_WRITE) {
6874 if (bpf_prog_is_dev_bound(prog->aux)) {
6876 case offsetof(struct xdp_md, rx_queue_index):
6877 return __is_valid_xdp_access(off, size);
6884 case offsetof(struct xdp_md, data):
6885 info->reg_type = PTR_TO_PACKET;
6887 case offsetof(struct xdp_md, data_meta):
6888 info->reg_type = PTR_TO_PACKET_META;
6890 case offsetof(struct xdp_md, data_end):
6891 info->reg_type = PTR_TO_PACKET_END;
6895 return __is_valid_xdp_access(off, size);
6898 void bpf_warn_invalid_xdp_action(u32 act)
6900 const u32 act_max = XDP_REDIRECT;
6902 WARN_ONCE(1, "%s XDP return value %u, expect packet loss!\n",
6903 act > act_max ? "Illegal" : "Driver unsupported",
6906 EXPORT_SYMBOL_GPL(bpf_warn_invalid_xdp_action);
6908 static bool sock_addr_is_valid_access(int off, int size,
6909 enum bpf_access_type type,
6910 const struct bpf_prog *prog,
6911 struct bpf_insn_access_aux *info)
6913 const int size_default = sizeof(__u32);
6915 if (off < 0 || off >= sizeof(struct bpf_sock_addr))
6917 if (off % size != 0)
6920 /* Disallow access to IPv6 fields from IPv4 contex and vise
6924 case bpf_ctx_range(struct bpf_sock_addr, user_ip4):
6925 switch (prog->expected_attach_type) {
6926 case BPF_CGROUP_INET4_BIND:
6927 case BPF_CGROUP_INET4_CONNECT:
6928 case BPF_CGROUP_UDP4_SENDMSG:
6929 case BPF_CGROUP_UDP4_RECVMSG:
6935 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
6936 switch (prog->expected_attach_type) {
6937 case BPF_CGROUP_INET6_BIND:
6938 case BPF_CGROUP_INET6_CONNECT:
6939 case BPF_CGROUP_UDP6_SENDMSG:
6940 case BPF_CGROUP_UDP6_RECVMSG:
6946 case bpf_ctx_range(struct bpf_sock_addr, msg_src_ip4):
6947 switch (prog->expected_attach_type) {
6948 case BPF_CGROUP_UDP4_SENDMSG:
6954 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
6956 switch (prog->expected_attach_type) {
6957 case BPF_CGROUP_UDP6_SENDMSG:
6966 case bpf_ctx_range(struct bpf_sock_addr, user_ip4):
6967 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
6968 case bpf_ctx_range(struct bpf_sock_addr, msg_src_ip4):
6969 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
6971 if (type == BPF_READ) {
6972 bpf_ctx_record_field_size(info, size_default);
6974 if (bpf_ctx_wide_access_ok(off, size,
6975 struct bpf_sock_addr,
6979 if (bpf_ctx_wide_access_ok(off, size,
6980 struct bpf_sock_addr,
6984 if (!bpf_ctx_narrow_access_ok(off, size, size_default))
6987 if (bpf_ctx_wide_access_ok(off, size,
6988 struct bpf_sock_addr,
6992 if (bpf_ctx_wide_access_ok(off, size,
6993 struct bpf_sock_addr,
6997 if (size != size_default)
7001 case bpf_ctx_range(struct bpf_sock_addr, user_port):
7002 if (size != size_default)
7005 case offsetof(struct bpf_sock_addr, sk):
7006 if (type != BPF_READ)
7008 if (size != sizeof(__u64))
7010 info->reg_type = PTR_TO_SOCKET;
7013 if (type == BPF_READ) {
7014 if (size != size_default)
7024 static bool sock_ops_is_valid_access(int off, int size,
7025 enum bpf_access_type type,
7026 const struct bpf_prog *prog,
7027 struct bpf_insn_access_aux *info)
7029 const int size_default = sizeof(__u32);
7031 if (off < 0 || off >= sizeof(struct bpf_sock_ops))
7034 /* The verifier guarantees that size > 0. */
7035 if (off % size != 0)
7038 if (type == BPF_WRITE) {
7040 case offsetof(struct bpf_sock_ops, reply):
7041 case offsetof(struct bpf_sock_ops, sk_txhash):
7042 if (size != size_default)
7050 case bpf_ctx_range_till(struct bpf_sock_ops, bytes_received,
7052 if (size != sizeof(__u64))
7055 case offsetof(struct bpf_sock_ops, sk):
7056 if (size != sizeof(__u64))
7058 info->reg_type = PTR_TO_SOCKET_OR_NULL;
7061 if (size != size_default)
7070 static int sk_skb_prologue(struct bpf_insn *insn_buf, bool direct_write,
7071 const struct bpf_prog *prog)
7073 return bpf_unclone_prologue(insn_buf, direct_write, prog, SK_DROP);
7076 static bool sk_skb_is_valid_access(int off, int size,
7077 enum bpf_access_type type,
7078 const struct bpf_prog *prog,
7079 struct bpf_insn_access_aux *info)
7082 case bpf_ctx_range(struct __sk_buff, tc_classid):
7083 case bpf_ctx_range(struct __sk_buff, data_meta):
7084 case bpf_ctx_range(struct __sk_buff, tstamp):
7085 case bpf_ctx_range(struct __sk_buff, wire_len):
7089 if (type == BPF_WRITE) {
7091 case bpf_ctx_range(struct __sk_buff, tc_index):
7092 case bpf_ctx_range(struct __sk_buff, priority):
7100 case bpf_ctx_range(struct __sk_buff, mark):
7102 case bpf_ctx_range(struct __sk_buff, data):
7103 info->reg_type = PTR_TO_PACKET;
7105 case bpf_ctx_range(struct __sk_buff, data_end):
7106 info->reg_type = PTR_TO_PACKET_END;
7110 return bpf_skb_is_valid_access(off, size, type, prog, info);
7113 static bool sk_msg_is_valid_access(int off, int size,
7114 enum bpf_access_type type,
7115 const struct bpf_prog *prog,
7116 struct bpf_insn_access_aux *info)
7118 if (type == BPF_WRITE)
7121 if (off % size != 0)
7125 case offsetof(struct sk_msg_md, data):
7126 info->reg_type = PTR_TO_PACKET;
7127 if (size != sizeof(__u64))
7130 case offsetof(struct sk_msg_md, data_end):
7131 info->reg_type = PTR_TO_PACKET_END;
7132 if (size != sizeof(__u64))
7135 case bpf_ctx_range(struct sk_msg_md, family):
7136 case bpf_ctx_range(struct sk_msg_md, remote_ip4):
7137 case bpf_ctx_range(struct sk_msg_md, local_ip4):
7138 case bpf_ctx_range_till(struct sk_msg_md, remote_ip6[0], remote_ip6[3]):
7139 case bpf_ctx_range_till(struct sk_msg_md, local_ip6[0], local_ip6[3]):
7140 case bpf_ctx_range(struct sk_msg_md, remote_port):
7141 case bpf_ctx_range(struct sk_msg_md, local_port):
7142 case bpf_ctx_range(struct sk_msg_md, size):
7143 if (size != sizeof(__u32))
7152 static bool flow_dissector_is_valid_access(int off, int size,
7153 enum bpf_access_type type,
7154 const struct bpf_prog *prog,
7155 struct bpf_insn_access_aux *info)
7157 const int size_default = sizeof(__u32);
7159 if (off < 0 || off >= sizeof(struct __sk_buff))
7162 if (type == BPF_WRITE)
7166 case bpf_ctx_range(struct __sk_buff, data):
7167 if (size != size_default)
7169 info->reg_type = PTR_TO_PACKET;
7171 case bpf_ctx_range(struct __sk_buff, data_end):
7172 if (size != size_default)
7174 info->reg_type = PTR_TO_PACKET_END;
7176 case bpf_ctx_range_ptr(struct __sk_buff, flow_keys):
7177 if (size != sizeof(__u64))
7179 info->reg_type = PTR_TO_FLOW_KEYS;
7186 static u32 flow_dissector_convert_ctx_access(enum bpf_access_type type,
7187 const struct bpf_insn *si,
7188 struct bpf_insn *insn_buf,
7189 struct bpf_prog *prog,
7193 struct bpf_insn *insn = insn_buf;
7196 case offsetof(struct __sk_buff, data):
7197 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_flow_dissector, data),
7198 si->dst_reg, si->src_reg,
7199 offsetof(struct bpf_flow_dissector, data));
7202 case offsetof(struct __sk_buff, data_end):
7203 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_flow_dissector, data_end),
7204 si->dst_reg, si->src_reg,
7205 offsetof(struct bpf_flow_dissector, data_end));
7208 case offsetof(struct __sk_buff, flow_keys):
7209 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_flow_dissector, flow_keys),
7210 si->dst_reg, si->src_reg,
7211 offsetof(struct bpf_flow_dissector, flow_keys));
7215 return insn - insn_buf;
7218 static u32 bpf_convert_ctx_access(enum bpf_access_type type,
7219 const struct bpf_insn *si,
7220 struct bpf_insn *insn_buf,
7221 struct bpf_prog *prog, u32 *target_size)
7223 struct bpf_insn *insn = insn_buf;
7227 case offsetof(struct __sk_buff, len):
7228 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
7229 bpf_target_off(struct sk_buff, len, 4,
7233 case offsetof(struct __sk_buff, protocol):
7234 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
7235 bpf_target_off(struct sk_buff, protocol, 2,
7239 case offsetof(struct __sk_buff, vlan_proto):
7240 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
7241 bpf_target_off(struct sk_buff, vlan_proto, 2,
7245 case offsetof(struct __sk_buff, priority):
7246 if (type == BPF_WRITE)
7247 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
7248 bpf_target_off(struct sk_buff, priority, 4,
7251 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
7252 bpf_target_off(struct sk_buff, priority, 4,
7256 case offsetof(struct __sk_buff, ingress_ifindex):
7257 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
7258 bpf_target_off(struct sk_buff, skb_iif, 4,
7262 case offsetof(struct __sk_buff, ifindex):
7263 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
7264 si->dst_reg, si->src_reg,
7265 offsetof(struct sk_buff, dev));
7266 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
7267 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7268 bpf_target_off(struct net_device, ifindex, 4,
7272 case offsetof(struct __sk_buff, hash):
7273 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
7274 bpf_target_off(struct sk_buff, hash, 4,
7278 case offsetof(struct __sk_buff, mark):
7279 if (type == BPF_WRITE)
7280 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
7281 bpf_target_off(struct sk_buff, mark, 4,
7284 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
7285 bpf_target_off(struct sk_buff, mark, 4,
7289 case offsetof(struct __sk_buff, pkt_type):
7291 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->src_reg,
7293 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, PKT_TYPE_MAX);
7294 #ifdef __BIG_ENDIAN_BITFIELD
7295 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, 5);
7299 case offsetof(struct __sk_buff, queue_mapping):
7300 if (type == BPF_WRITE) {
7301 *insn++ = BPF_JMP_IMM(BPF_JGE, si->src_reg, NO_QUEUE_MAPPING, 1);
7302 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg, si->src_reg,
7303 bpf_target_off(struct sk_buff,
7307 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
7308 bpf_target_off(struct sk_buff,
7314 case offsetof(struct __sk_buff, vlan_present):
7316 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->src_reg,
7317 PKT_VLAN_PRESENT_OFFSET());
7318 if (PKT_VLAN_PRESENT_BIT)
7319 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, PKT_VLAN_PRESENT_BIT);
7320 if (PKT_VLAN_PRESENT_BIT < 7)
7321 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, 1);
7324 case offsetof(struct __sk_buff, vlan_tci):
7325 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
7326 bpf_target_off(struct sk_buff, vlan_tci, 2,
7330 case offsetof(struct __sk_buff, cb[0]) ...
7331 offsetofend(struct __sk_buff, cb[4]) - 1:
7332 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, data) < 20);
7333 BUILD_BUG_ON((offsetof(struct sk_buff, cb) +
7334 offsetof(struct qdisc_skb_cb, data)) %
7337 prog->cb_access = 1;
7339 off -= offsetof(struct __sk_buff, cb[0]);
7340 off += offsetof(struct sk_buff, cb);
7341 off += offsetof(struct qdisc_skb_cb, data);
7342 if (type == BPF_WRITE)
7343 *insn++ = BPF_STX_MEM(BPF_SIZE(si->code), si->dst_reg,
7346 *insn++ = BPF_LDX_MEM(BPF_SIZE(si->code), si->dst_reg,
7350 case offsetof(struct __sk_buff, tc_classid):
7351 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, tc_classid) != 2);
7354 off -= offsetof(struct __sk_buff, tc_classid);
7355 off += offsetof(struct sk_buff, cb);
7356 off += offsetof(struct qdisc_skb_cb, tc_classid);
7358 if (type == BPF_WRITE)
7359 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg,
7362 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg,
7366 case offsetof(struct __sk_buff, data):
7367 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
7368 si->dst_reg, si->src_reg,
7369 offsetof(struct sk_buff, data));
7372 case offsetof(struct __sk_buff, data_meta):
7374 off -= offsetof(struct __sk_buff, data_meta);
7375 off += offsetof(struct sk_buff, cb);
7376 off += offsetof(struct bpf_skb_data_end, data_meta);
7377 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
7381 case offsetof(struct __sk_buff, data_end):
7383 off -= offsetof(struct __sk_buff, data_end);
7384 off += offsetof(struct sk_buff, cb);
7385 off += offsetof(struct bpf_skb_data_end, data_end);
7386 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
7390 case offsetof(struct __sk_buff, tc_index):
7391 #ifdef CONFIG_NET_SCHED
7392 if (type == BPF_WRITE)
7393 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg, si->src_reg,
7394 bpf_target_off(struct sk_buff, tc_index, 2,
7397 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
7398 bpf_target_off(struct sk_buff, tc_index, 2,
7402 if (type == BPF_WRITE)
7403 *insn++ = BPF_MOV64_REG(si->dst_reg, si->dst_reg);
7405 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
7409 case offsetof(struct __sk_buff, napi_id):
7410 #if defined(CONFIG_NET_RX_BUSY_POLL)
7411 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
7412 bpf_target_off(struct sk_buff, napi_id, 4,
7414 *insn++ = BPF_JMP_IMM(BPF_JGE, si->dst_reg, MIN_NAPI_ID, 1);
7415 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
7418 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
7421 case offsetof(struct __sk_buff, family):
7422 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_family) != 2);
7424 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
7425 si->dst_reg, si->src_reg,
7426 offsetof(struct sk_buff, sk));
7427 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
7428 bpf_target_off(struct sock_common,
7432 case offsetof(struct __sk_buff, remote_ip4):
7433 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_daddr) != 4);
7435 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
7436 si->dst_reg, si->src_reg,
7437 offsetof(struct sk_buff, sk));
7438 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7439 bpf_target_off(struct sock_common,
7443 case offsetof(struct __sk_buff, local_ip4):
7444 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
7445 skc_rcv_saddr) != 4);
7447 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
7448 si->dst_reg, si->src_reg,
7449 offsetof(struct sk_buff, sk));
7450 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7451 bpf_target_off(struct sock_common,
7455 case offsetof(struct __sk_buff, remote_ip6[0]) ...
7456 offsetof(struct __sk_buff, remote_ip6[3]):
7457 #if IS_ENABLED(CONFIG_IPV6)
7458 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
7459 skc_v6_daddr.s6_addr32[0]) != 4);
7462 off -= offsetof(struct __sk_buff, remote_ip6[0]);
7464 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
7465 si->dst_reg, si->src_reg,
7466 offsetof(struct sk_buff, sk));
7467 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7468 offsetof(struct sock_common,
7469 skc_v6_daddr.s6_addr32[0]) +
7472 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
7475 case offsetof(struct __sk_buff, local_ip6[0]) ...
7476 offsetof(struct __sk_buff, local_ip6[3]):
7477 #if IS_ENABLED(CONFIG_IPV6)
7478 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
7479 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
7482 off -= offsetof(struct __sk_buff, local_ip6[0]);
7484 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
7485 si->dst_reg, si->src_reg,
7486 offsetof(struct sk_buff, sk));
7487 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7488 offsetof(struct sock_common,
7489 skc_v6_rcv_saddr.s6_addr32[0]) +
7492 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
7496 case offsetof(struct __sk_buff, remote_port):
7497 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_dport) != 2);
7499 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
7500 si->dst_reg, si->src_reg,
7501 offsetof(struct sk_buff, sk));
7502 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
7503 bpf_target_off(struct sock_common,
7506 #ifndef __BIG_ENDIAN_BITFIELD
7507 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
7511 case offsetof(struct __sk_buff, local_port):
7512 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_num) != 2);
7514 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
7515 si->dst_reg, si->src_reg,
7516 offsetof(struct sk_buff, sk));
7517 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
7518 bpf_target_off(struct sock_common,
7519 skc_num, 2, target_size));
7522 case offsetof(struct __sk_buff, tstamp):
7523 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, tstamp) != 8);
7525 if (type == BPF_WRITE)
7526 *insn++ = BPF_STX_MEM(BPF_DW,
7527 si->dst_reg, si->src_reg,
7528 bpf_target_off(struct sk_buff,
7532 *insn++ = BPF_LDX_MEM(BPF_DW,
7533 si->dst_reg, si->src_reg,
7534 bpf_target_off(struct sk_buff,
7539 case offsetof(struct __sk_buff, gso_segs):
7540 /* si->dst_reg = skb_shinfo(SKB); */
7541 #ifdef NET_SKBUFF_DATA_USES_OFFSET
7542 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, end),
7543 BPF_REG_AX, si->src_reg,
7544 offsetof(struct sk_buff, end));
7545 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, head),
7546 si->dst_reg, si->src_reg,
7547 offsetof(struct sk_buff, head));
7548 *insn++ = BPF_ALU64_REG(BPF_ADD, si->dst_reg, BPF_REG_AX);
7550 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, end),
7551 si->dst_reg, si->src_reg,
7552 offsetof(struct sk_buff, end));
7554 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct skb_shared_info, gso_segs),
7555 si->dst_reg, si->dst_reg,
7556 bpf_target_off(struct skb_shared_info,
7560 case offsetof(struct __sk_buff, wire_len):
7561 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, pkt_len) != 4);
7564 off -= offsetof(struct __sk_buff, wire_len);
7565 off += offsetof(struct sk_buff, cb);
7566 off += offsetof(struct qdisc_skb_cb, pkt_len);
7568 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, off);
7571 case offsetof(struct __sk_buff, sk):
7572 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
7573 si->dst_reg, si->src_reg,
7574 offsetof(struct sk_buff, sk));
7578 return insn - insn_buf;
7581 u32 bpf_sock_convert_ctx_access(enum bpf_access_type type,
7582 const struct bpf_insn *si,
7583 struct bpf_insn *insn_buf,
7584 struct bpf_prog *prog, u32 *target_size)
7586 struct bpf_insn *insn = insn_buf;
7590 case offsetof(struct bpf_sock, bound_dev_if):
7591 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_bound_dev_if) != 4);
7593 if (type == BPF_WRITE)
7594 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
7595 offsetof(struct sock, sk_bound_dev_if));
7597 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
7598 offsetof(struct sock, sk_bound_dev_if));
7601 case offsetof(struct bpf_sock, mark):
7602 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_mark) != 4);
7604 if (type == BPF_WRITE)
7605 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
7606 offsetof(struct sock, sk_mark));
7608 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
7609 offsetof(struct sock, sk_mark));
7612 case offsetof(struct bpf_sock, priority):
7613 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_priority) != 4);
7615 if (type == BPF_WRITE)
7616 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
7617 offsetof(struct sock, sk_priority));
7619 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
7620 offsetof(struct sock, sk_priority));
7623 case offsetof(struct bpf_sock, family):
7624 *insn++ = BPF_LDX_MEM(
7625 BPF_FIELD_SIZEOF(struct sock_common, skc_family),
7626 si->dst_reg, si->src_reg,
7627 bpf_target_off(struct sock_common,
7629 FIELD_SIZEOF(struct sock_common,
7634 case offsetof(struct bpf_sock, type):
7635 BUILD_BUG_ON(HWEIGHT32(SK_FL_TYPE_MASK) != BITS_PER_BYTE * 2);
7636 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
7637 offsetof(struct sock, __sk_flags_offset));
7638 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_TYPE_MASK);
7639 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, SK_FL_TYPE_SHIFT);
7643 case offsetof(struct bpf_sock, protocol):
7644 BUILD_BUG_ON(HWEIGHT32(SK_FL_PROTO_MASK) != BITS_PER_BYTE);
7645 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
7646 offsetof(struct sock, __sk_flags_offset));
7647 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_PROTO_MASK);
7648 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, SK_FL_PROTO_SHIFT);
7652 case offsetof(struct bpf_sock, src_ip4):
7653 *insn++ = BPF_LDX_MEM(
7654 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
7655 bpf_target_off(struct sock_common, skc_rcv_saddr,
7656 FIELD_SIZEOF(struct sock_common,
7661 case offsetof(struct bpf_sock, dst_ip4):
7662 *insn++ = BPF_LDX_MEM(
7663 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
7664 bpf_target_off(struct sock_common, skc_daddr,
7665 FIELD_SIZEOF(struct sock_common,
7670 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
7671 #if IS_ENABLED(CONFIG_IPV6)
7673 off -= offsetof(struct bpf_sock, src_ip6[0]);
7674 *insn++ = BPF_LDX_MEM(
7675 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
7678 skc_v6_rcv_saddr.s6_addr32[0],
7679 FIELD_SIZEOF(struct sock_common,
7680 skc_v6_rcv_saddr.s6_addr32[0]),
7681 target_size) + off);
7684 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
7688 case bpf_ctx_range_till(struct bpf_sock, dst_ip6[0], dst_ip6[3]):
7689 #if IS_ENABLED(CONFIG_IPV6)
7691 off -= offsetof(struct bpf_sock, dst_ip6[0]);
7692 *insn++ = BPF_LDX_MEM(
7693 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
7694 bpf_target_off(struct sock_common,
7695 skc_v6_daddr.s6_addr32[0],
7696 FIELD_SIZEOF(struct sock_common,
7697 skc_v6_daddr.s6_addr32[0]),
7698 target_size) + off);
7700 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
7705 case offsetof(struct bpf_sock, src_port):
7706 *insn++ = BPF_LDX_MEM(
7707 BPF_FIELD_SIZEOF(struct sock_common, skc_num),
7708 si->dst_reg, si->src_reg,
7709 bpf_target_off(struct sock_common, skc_num,
7710 FIELD_SIZEOF(struct sock_common,
7715 case offsetof(struct bpf_sock, dst_port):
7716 *insn++ = BPF_LDX_MEM(
7717 BPF_FIELD_SIZEOF(struct sock_common, skc_dport),
7718 si->dst_reg, si->src_reg,
7719 bpf_target_off(struct sock_common, skc_dport,
7720 FIELD_SIZEOF(struct sock_common,
7725 case offsetof(struct bpf_sock, state):
7726 *insn++ = BPF_LDX_MEM(
7727 BPF_FIELD_SIZEOF(struct sock_common, skc_state),
7728 si->dst_reg, si->src_reg,
7729 bpf_target_off(struct sock_common, skc_state,
7730 FIELD_SIZEOF(struct sock_common,
7736 return insn - insn_buf;
7739 static u32 tc_cls_act_convert_ctx_access(enum bpf_access_type type,
7740 const struct bpf_insn *si,
7741 struct bpf_insn *insn_buf,
7742 struct bpf_prog *prog, u32 *target_size)
7744 struct bpf_insn *insn = insn_buf;
7747 case offsetof(struct __sk_buff, ifindex):
7748 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
7749 si->dst_reg, si->src_reg,
7750 offsetof(struct sk_buff, dev));
7751 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7752 bpf_target_off(struct net_device, ifindex, 4,
7756 return bpf_convert_ctx_access(type, si, insn_buf, prog,
7760 return insn - insn_buf;
7763 static u32 xdp_convert_ctx_access(enum bpf_access_type type,
7764 const struct bpf_insn *si,
7765 struct bpf_insn *insn_buf,
7766 struct bpf_prog *prog, u32 *target_size)
7768 struct bpf_insn *insn = insn_buf;
7771 case offsetof(struct xdp_md, data):
7772 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data),
7773 si->dst_reg, si->src_reg,
7774 offsetof(struct xdp_buff, data));
7776 case offsetof(struct xdp_md, data_meta):
7777 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_meta),
7778 si->dst_reg, si->src_reg,
7779 offsetof(struct xdp_buff, data_meta));
7781 case offsetof(struct xdp_md, data_end):
7782 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_end),
7783 si->dst_reg, si->src_reg,
7784 offsetof(struct xdp_buff, data_end));
7786 case offsetof(struct xdp_md, ingress_ifindex):
7787 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, rxq),
7788 si->dst_reg, si->src_reg,
7789 offsetof(struct xdp_buff, rxq));
7790 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_rxq_info, dev),
7791 si->dst_reg, si->dst_reg,
7792 offsetof(struct xdp_rxq_info, dev));
7793 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7794 offsetof(struct net_device, ifindex));
7796 case offsetof(struct xdp_md, rx_queue_index):
7797 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, rxq),
7798 si->dst_reg, si->src_reg,
7799 offsetof(struct xdp_buff, rxq));
7800 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7801 offsetof(struct xdp_rxq_info,
7806 return insn - insn_buf;
7809 /* SOCK_ADDR_LOAD_NESTED_FIELD() loads Nested Field S.F.NF where S is type of
7810 * context Structure, F is Field in context structure that contains a pointer
7811 * to Nested Structure of type NS that has the field NF.
7813 * SIZE encodes the load size (BPF_B, BPF_H, etc). It's up to caller to make
7814 * sure that SIZE is not greater than actual size of S.F.NF.
7816 * If offset OFF is provided, the load happens from that offset relative to
7819 #define SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, SIZE, OFF) \
7821 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), si->dst_reg, \
7822 si->src_reg, offsetof(S, F)); \
7823 *insn++ = BPF_LDX_MEM( \
7824 SIZE, si->dst_reg, si->dst_reg, \
7825 bpf_target_off(NS, NF, FIELD_SIZEOF(NS, NF), \
7830 #define SOCK_ADDR_LOAD_NESTED_FIELD(S, NS, F, NF) \
7831 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, \
7832 BPF_FIELD_SIZEOF(NS, NF), 0)
7834 /* SOCK_ADDR_STORE_NESTED_FIELD_OFF() has semantic similar to
7835 * SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF() but for store operation.
7837 * In addition it uses Temporary Field TF (member of struct S) as the 3rd
7838 * "register" since two registers available in convert_ctx_access are not
7839 * enough: we can't override neither SRC, since it contains value to store, nor
7840 * DST since it contains pointer to context that may be used by later
7841 * instructions. But we need a temporary place to save pointer to nested
7842 * structure whose field we want to store to.
7844 #define SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, SIZE, OFF, TF) \
7846 int tmp_reg = BPF_REG_9; \
7847 if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg) \
7849 if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg) \
7851 *insn++ = BPF_STX_MEM(BPF_DW, si->dst_reg, tmp_reg, \
7853 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), tmp_reg, \
7854 si->dst_reg, offsetof(S, F)); \
7855 *insn++ = BPF_STX_MEM(SIZE, tmp_reg, si->src_reg, \
7856 bpf_target_off(NS, NF, FIELD_SIZEOF(NS, NF), \
7859 *insn++ = BPF_LDX_MEM(BPF_DW, tmp_reg, si->dst_reg, \
7863 #define SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, SIZE, OFF, \
7866 if (type == BPF_WRITE) { \
7867 SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, SIZE, \
7870 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF( \
7871 S, NS, F, NF, SIZE, OFF); \
7875 #define SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD(S, NS, F, NF, TF) \
7876 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF( \
7877 S, NS, F, NF, BPF_FIELD_SIZEOF(NS, NF), 0, TF)
7879 static u32 sock_addr_convert_ctx_access(enum bpf_access_type type,
7880 const struct bpf_insn *si,
7881 struct bpf_insn *insn_buf,
7882 struct bpf_prog *prog, u32 *target_size)
7884 struct bpf_insn *insn = insn_buf;
7888 case offsetof(struct bpf_sock_addr, user_family):
7889 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
7890 struct sockaddr, uaddr, sa_family);
7893 case offsetof(struct bpf_sock_addr, user_ip4):
7894 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
7895 struct bpf_sock_addr_kern, struct sockaddr_in, uaddr,
7896 sin_addr, BPF_SIZE(si->code), 0, tmp_reg);
7899 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
7901 off -= offsetof(struct bpf_sock_addr, user_ip6[0]);
7902 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
7903 struct bpf_sock_addr_kern, struct sockaddr_in6, uaddr,
7904 sin6_addr.s6_addr32[0], BPF_SIZE(si->code), off,
7908 case offsetof(struct bpf_sock_addr, user_port):
7909 /* To get port we need to know sa_family first and then treat
7910 * sockaddr as either sockaddr_in or sockaddr_in6.
7911 * Though we can simplify since port field has same offset and
7912 * size in both structures.
7913 * Here we check this invariant and use just one of the
7914 * structures if it's true.
7916 BUILD_BUG_ON(offsetof(struct sockaddr_in, sin_port) !=
7917 offsetof(struct sockaddr_in6, sin6_port));
7918 BUILD_BUG_ON(FIELD_SIZEOF(struct sockaddr_in, sin_port) !=
7919 FIELD_SIZEOF(struct sockaddr_in6, sin6_port));
7920 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD(struct bpf_sock_addr_kern,
7921 struct sockaddr_in6, uaddr,
7922 sin6_port, tmp_reg);
7925 case offsetof(struct bpf_sock_addr, family):
7926 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
7927 struct sock, sk, sk_family);
7930 case offsetof(struct bpf_sock_addr, type):
7931 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(
7932 struct bpf_sock_addr_kern, struct sock, sk,
7933 __sk_flags_offset, BPF_W, 0);
7934 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_TYPE_MASK);
7935 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, SK_FL_TYPE_SHIFT);
7938 case offsetof(struct bpf_sock_addr, protocol):
7939 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(
7940 struct bpf_sock_addr_kern, struct sock, sk,
7941 __sk_flags_offset, BPF_W, 0);
7942 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_PROTO_MASK);
7943 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg,
7947 case offsetof(struct bpf_sock_addr, msg_src_ip4):
7948 /* Treat t_ctx as struct in_addr for msg_src_ip4. */
7949 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
7950 struct bpf_sock_addr_kern, struct in_addr, t_ctx,
7951 s_addr, BPF_SIZE(si->code), 0, tmp_reg);
7954 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
7957 off -= offsetof(struct bpf_sock_addr, msg_src_ip6[0]);
7958 /* Treat t_ctx as struct in6_addr for msg_src_ip6. */
7959 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
7960 struct bpf_sock_addr_kern, struct in6_addr, t_ctx,
7961 s6_addr32[0], BPF_SIZE(si->code), off, tmp_reg);
7963 case offsetof(struct bpf_sock_addr, sk):
7964 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_addr_kern, sk),
7965 si->dst_reg, si->src_reg,
7966 offsetof(struct bpf_sock_addr_kern, sk));
7970 return insn - insn_buf;
7973 static u32 sock_ops_convert_ctx_access(enum bpf_access_type type,
7974 const struct bpf_insn *si,
7975 struct bpf_insn *insn_buf,
7976 struct bpf_prog *prog,
7979 struct bpf_insn *insn = insn_buf;
7982 /* Helper macro for adding read access to tcp_sock or sock fields. */
7983 #define SOCK_OPS_GET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ) \
7985 BUILD_BUG_ON(FIELD_SIZEOF(OBJ, OBJ_FIELD) > \
7986 FIELD_SIZEOF(struct bpf_sock_ops, BPF_FIELD)); \
7987 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
7988 struct bpf_sock_ops_kern, \
7990 si->dst_reg, si->src_reg, \
7991 offsetof(struct bpf_sock_ops_kern, \
7993 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 2); \
7994 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
7995 struct bpf_sock_ops_kern, sk),\
7996 si->dst_reg, si->src_reg, \
7997 offsetof(struct bpf_sock_ops_kern, sk));\
7998 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(OBJ, \
8000 si->dst_reg, si->dst_reg, \
8001 offsetof(OBJ, OBJ_FIELD)); \
8004 #define SOCK_OPS_GET_TCP_SOCK_FIELD(FIELD) \
8005 SOCK_OPS_GET_FIELD(FIELD, FIELD, struct tcp_sock)
8007 /* Helper macro for adding write access to tcp_sock or sock fields.
8008 * The macro is called with two registers, dst_reg which contains a pointer
8009 * to ctx (context) and src_reg which contains the value that should be
8010 * stored. However, we need an additional register since we cannot overwrite
8011 * dst_reg because it may be used later in the program.
8012 * Instead we "borrow" one of the other register. We first save its value
8013 * into a new (temp) field in bpf_sock_ops_kern, use it, and then restore
8014 * it at the end of the macro.
8016 #define SOCK_OPS_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ) \
8018 int reg = BPF_REG_9; \
8019 BUILD_BUG_ON(FIELD_SIZEOF(OBJ, OBJ_FIELD) > \
8020 FIELD_SIZEOF(struct bpf_sock_ops, BPF_FIELD)); \
8021 if (si->dst_reg == reg || si->src_reg == reg) \
8023 if (si->dst_reg == reg || si->src_reg == reg) \
8025 *insn++ = BPF_STX_MEM(BPF_DW, si->dst_reg, reg, \
8026 offsetof(struct bpf_sock_ops_kern, \
8028 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
8029 struct bpf_sock_ops_kern, \
8032 offsetof(struct bpf_sock_ops_kern, \
8034 *insn++ = BPF_JMP_IMM(BPF_JEQ, reg, 0, 2); \
8035 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
8036 struct bpf_sock_ops_kern, sk),\
8038 offsetof(struct bpf_sock_ops_kern, sk));\
8039 *insn++ = BPF_STX_MEM(BPF_FIELD_SIZEOF(OBJ, OBJ_FIELD), \
8041 offsetof(OBJ, OBJ_FIELD)); \
8042 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->dst_reg, \
8043 offsetof(struct bpf_sock_ops_kern, \
8047 #define SOCK_OPS_GET_OR_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ, TYPE) \
8049 if (TYPE == BPF_WRITE) \
8050 SOCK_OPS_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ); \
8052 SOCK_OPS_GET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ); \
8055 if (insn > insn_buf)
8056 return insn - insn_buf;
8059 case offsetof(struct bpf_sock_ops, op) ...
8060 offsetof(struct bpf_sock_ops, replylong[3]):
8061 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, op) !=
8062 FIELD_SIZEOF(struct bpf_sock_ops_kern, op));
8063 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, reply) !=
8064 FIELD_SIZEOF(struct bpf_sock_ops_kern, reply));
8065 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, replylong) !=
8066 FIELD_SIZEOF(struct bpf_sock_ops_kern, replylong));
8068 off -= offsetof(struct bpf_sock_ops, op);
8069 off += offsetof(struct bpf_sock_ops_kern, op);
8070 if (type == BPF_WRITE)
8071 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
8074 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
8078 case offsetof(struct bpf_sock_ops, family):
8079 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_family) != 2);
8081 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8082 struct bpf_sock_ops_kern, sk),
8083 si->dst_reg, si->src_reg,
8084 offsetof(struct bpf_sock_ops_kern, sk));
8085 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
8086 offsetof(struct sock_common, skc_family));
8089 case offsetof(struct bpf_sock_ops, remote_ip4):
8090 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_daddr) != 4);
8092 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8093 struct bpf_sock_ops_kern, sk),
8094 si->dst_reg, si->src_reg,
8095 offsetof(struct bpf_sock_ops_kern, sk));
8096 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
8097 offsetof(struct sock_common, skc_daddr));
8100 case offsetof(struct bpf_sock_ops, local_ip4):
8101 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
8102 skc_rcv_saddr) != 4);
8104 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8105 struct bpf_sock_ops_kern, sk),
8106 si->dst_reg, si->src_reg,
8107 offsetof(struct bpf_sock_ops_kern, sk));
8108 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
8109 offsetof(struct sock_common,
8113 case offsetof(struct bpf_sock_ops, remote_ip6[0]) ...
8114 offsetof(struct bpf_sock_ops, remote_ip6[3]):
8115 #if IS_ENABLED(CONFIG_IPV6)
8116 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
8117 skc_v6_daddr.s6_addr32[0]) != 4);
8120 off -= offsetof(struct bpf_sock_ops, remote_ip6[0]);
8121 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8122 struct bpf_sock_ops_kern, sk),
8123 si->dst_reg, si->src_reg,
8124 offsetof(struct bpf_sock_ops_kern, sk));
8125 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
8126 offsetof(struct sock_common,
8127 skc_v6_daddr.s6_addr32[0]) +
8130 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
8134 case offsetof(struct bpf_sock_ops, local_ip6[0]) ...
8135 offsetof(struct bpf_sock_ops, local_ip6[3]):
8136 #if IS_ENABLED(CONFIG_IPV6)
8137 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
8138 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
8141 off -= offsetof(struct bpf_sock_ops, local_ip6[0]);
8142 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8143 struct bpf_sock_ops_kern, sk),
8144 si->dst_reg, si->src_reg,
8145 offsetof(struct bpf_sock_ops_kern, sk));
8146 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
8147 offsetof(struct sock_common,
8148 skc_v6_rcv_saddr.s6_addr32[0]) +
8151 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
8155 case offsetof(struct bpf_sock_ops, remote_port):
8156 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_dport) != 2);
8158 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8159 struct bpf_sock_ops_kern, sk),
8160 si->dst_reg, si->src_reg,
8161 offsetof(struct bpf_sock_ops_kern, sk));
8162 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
8163 offsetof(struct sock_common, skc_dport));
8164 #ifndef __BIG_ENDIAN_BITFIELD
8165 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
8169 case offsetof(struct bpf_sock_ops, local_port):
8170 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_num) != 2);
8172 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8173 struct bpf_sock_ops_kern, sk),
8174 si->dst_reg, si->src_reg,
8175 offsetof(struct bpf_sock_ops_kern, sk));
8176 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
8177 offsetof(struct sock_common, skc_num));
8180 case offsetof(struct bpf_sock_ops, is_fullsock):
8181 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8182 struct bpf_sock_ops_kern,
8184 si->dst_reg, si->src_reg,
8185 offsetof(struct bpf_sock_ops_kern,
8189 case offsetof(struct bpf_sock_ops, state):
8190 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_state) != 1);
8192 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8193 struct bpf_sock_ops_kern, sk),
8194 si->dst_reg, si->src_reg,
8195 offsetof(struct bpf_sock_ops_kern, sk));
8196 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->dst_reg,
8197 offsetof(struct sock_common, skc_state));
8200 case offsetof(struct bpf_sock_ops, rtt_min):
8201 BUILD_BUG_ON(FIELD_SIZEOF(struct tcp_sock, rtt_min) !=
8202 sizeof(struct minmax));
8203 BUILD_BUG_ON(sizeof(struct minmax) <
8204 sizeof(struct minmax_sample));
8206 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8207 struct bpf_sock_ops_kern, sk),
8208 si->dst_reg, si->src_reg,
8209 offsetof(struct bpf_sock_ops_kern, sk));
8210 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
8211 offsetof(struct tcp_sock, rtt_min) +
8212 FIELD_SIZEOF(struct minmax_sample, t));
8215 case offsetof(struct bpf_sock_ops, bpf_sock_ops_cb_flags):
8216 SOCK_OPS_GET_FIELD(bpf_sock_ops_cb_flags, bpf_sock_ops_cb_flags,
8220 case offsetof(struct bpf_sock_ops, sk_txhash):
8221 SOCK_OPS_GET_OR_SET_FIELD(sk_txhash, sk_txhash,
8224 case offsetof(struct bpf_sock_ops, snd_cwnd):
8225 SOCK_OPS_GET_TCP_SOCK_FIELD(snd_cwnd);
8227 case offsetof(struct bpf_sock_ops, srtt_us):
8228 SOCK_OPS_GET_TCP_SOCK_FIELD(srtt_us);
8230 case offsetof(struct bpf_sock_ops, snd_ssthresh):
8231 SOCK_OPS_GET_TCP_SOCK_FIELD(snd_ssthresh);
8233 case offsetof(struct bpf_sock_ops, rcv_nxt):
8234 SOCK_OPS_GET_TCP_SOCK_FIELD(rcv_nxt);
8236 case offsetof(struct bpf_sock_ops, snd_nxt):
8237 SOCK_OPS_GET_TCP_SOCK_FIELD(snd_nxt);
8239 case offsetof(struct bpf_sock_ops, snd_una):
8240 SOCK_OPS_GET_TCP_SOCK_FIELD(snd_una);
8242 case offsetof(struct bpf_sock_ops, mss_cache):
8243 SOCK_OPS_GET_TCP_SOCK_FIELD(mss_cache);
8245 case offsetof(struct bpf_sock_ops, ecn_flags):
8246 SOCK_OPS_GET_TCP_SOCK_FIELD(ecn_flags);
8248 case offsetof(struct bpf_sock_ops, rate_delivered):
8249 SOCK_OPS_GET_TCP_SOCK_FIELD(rate_delivered);
8251 case offsetof(struct bpf_sock_ops, rate_interval_us):
8252 SOCK_OPS_GET_TCP_SOCK_FIELD(rate_interval_us);
8254 case offsetof(struct bpf_sock_ops, packets_out):
8255 SOCK_OPS_GET_TCP_SOCK_FIELD(packets_out);
8257 case offsetof(struct bpf_sock_ops, retrans_out):
8258 SOCK_OPS_GET_TCP_SOCK_FIELD(retrans_out);
8260 case offsetof(struct bpf_sock_ops, total_retrans):
8261 SOCK_OPS_GET_TCP_SOCK_FIELD(total_retrans);
8263 case offsetof(struct bpf_sock_ops, segs_in):
8264 SOCK_OPS_GET_TCP_SOCK_FIELD(segs_in);
8266 case offsetof(struct bpf_sock_ops, data_segs_in):
8267 SOCK_OPS_GET_TCP_SOCK_FIELD(data_segs_in);
8269 case offsetof(struct bpf_sock_ops, segs_out):
8270 SOCK_OPS_GET_TCP_SOCK_FIELD(segs_out);
8272 case offsetof(struct bpf_sock_ops, data_segs_out):
8273 SOCK_OPS_GET_TCP_SOCK_FIELD(data_segs_out);
8275 case offsetof(struct bpf_sock_ops, lost_out):
8276 SOCK_OPS_GET_TCP_SOCK_FIELD(lost_out);
8278 case offsetof(struct bpf_sock_ops, sacked_out):
8279 SOCK_OPS_GET_TCP_SOCK_FIELD(sacked_out);
8281 case offsetof(struct bpf_sock_ops, bytes_received):
8282 SOCK_OPS_GET_TCP_SOCK_FIELD(bytes_received);
8284 case offsetof(struct bpf_sock_ops, bytes_acked):
8285 SOCK_OPS_GET_TCP_SOCK_FIELD(bytes_acked);
8287 case offsetof(struct bpf_sock_ops, sk):
8288 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8289 struct bpf_sock_ops_kern,
8291 si->dst_reg, si->src_reg,
8292 offsetof(struct bpf_sock_ops_kern,
8294 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
8295 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8296 struct bpf_sock_ops_kern, sk),
8297 si->dst_reg, si->src_reg,
8298 offsetof(struct bpf_sock_ops_kern, sk));
8301 return insn - insn_buf;
8304 static u32 sk_skb_convert_ctx_access(enum bpf_access_type type,
8305 const struct bpf_insn *si,
8306 struct bpf_insn *insn_buf,
8307 struct bpf_prog *prog, u32 *target_size)
8309 struct bpf_insn *insn = insn_buf;
8313 case offsetof(struct __sk_buff, data_end):
8315 off -= offsetof(struct __sk_buff, data_end);
8316 off += offsetof(struct sk_buff, cb);
8317 off += offsetof(struct tcp_skb_cb, bpf.data_end);
8318 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
8322 return bpf_convert_ctx_access(type, si, insn_buf, prog,
8326 return insn - insn_buf;
8329 static u32 sk_msg_convert_ctx_access(enum bpf_access_type type,
8330 const struct bpf_insn *si,
8331 struct bpf_insn *insn_buf,
8332 struct bpf_prog *prog, u32 *target_size)
8334 struct bpf_insn *insn = insn_buf;
8335 #if IS_ENABLED(CONFIG_IPV6)
8339 /* convert ctx uses the fact sg element is first in struct */
8340 BUILD_BUG_ON(offsetof(struct sk_msg, sg) != 0);
8343 case offsetof(struct sk_msg_md, data):
8344 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg, data),
8345 si->dst_reg, si->src_reg,
8346 offsetof(struct sk_msg, data));
8348 case offsetof(struct sk_msg_md, data_end):
8349 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg, data_end),
8350 si->dst_reg, si->src_reg,
8351 offsetof(struct sk_msg, data_end));
8353 case offsetof(struct sk_msg_md, family):
8354 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_family) != 2);
8356 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8358 si->dst_reg, si->src_reg,
8359 offsetof(struct sk_msg, sk));
8360 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
8361 offsetof(struct sock_common, skc_family));
8364 case offsetof(struct sk_msg_md, remote_ip4):
8365 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_daddr) != 4);
8367 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8369 si->dst_reg, si->src_reg,
8370 offsetof(struct sk_msg, sk));
8371 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
8372 offsetof(struct sock_common, skc_daddr));
8375 case offsetof(struct sk_msg_md, local_ip4):
8376 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
8377 skc_rcv_saddr) != 4);
8379 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8381 si->dst_reg, si->src_reg,
8382 offsetof(struct sk_msg, sk));
8383 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
8384 offsetof(struct sock_common,
8388 case offsetof(struct sk_msg_md, remote_ip6[0]) ...
8389 offsetof(struct sk_msg_md, remote_ip6[3]):
8390 #if IS_ENABLED(CONFIG_IPV6)
8391 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
8392 skc_v6_daddr.s6_addr32[0]) != 4);
8395 off -= offsetof(struct sk_msg_md, remote_ip6[0]);
8396 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8398 si->dst_reg, si->src_reg,
8399 offsetof(struct sk_msg, sk));
8400 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
8401 offsetof(struct sock_common,
8402 skc_v6_daddr.s6_addr32[0]) +
8405 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
8409 case offsetof(struct sk_msg_md, local_ip6[0]) ...
8410 offsetof(struct sk_msg_md, local_ip6[3]):
8411 #if IS_ENABLED(CONFIG_IPV6)
8412 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
8413 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
8416 off -= offsetof(struct sk_msg_md, local_ip6[0]);
8417 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8419 si->dst_reg, si->src_reg,
8420 offsetof(struct sk_msg, sk));
8421 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
8422 offsetof(struct sock_common,
8423 skc_v6_rcv_saddr.s6_addr32[0]) +
8426 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
8430 case offsetof(struct sk_msg_md, remote_port):
8431 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_dport) != 2);
8433 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8435 si->dst_reg, si->src_reg,
8436 offsetof(struct sk_msg, sk));
8437 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
8438 offsetof(struct sock_common, skc_dport));
8439 #ifndef __BIG_ENDIAN_BITFIELD
8440 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
8444 case offsetof(struct sk_msg_md, local_port):
8445 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_num) != 2);
8447 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8449 si->dst_reg, si->src_reg,
8450 offsetof(struct sk_msg, sk));
8451 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
8452 offsetof(struct sock_common, skc_num));
8455 case offsetof(struct sk_msg_md, size):
8456 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg_sg, size),
8457 si->dst_reg, si->src_reg,
8458 offsetof(struct sk_msg_sg, size));
8462 return insn - insn_buf;
8465 const struct bpf_verifier_ops sk_filter_verifier_ops = {
8466 .get_func_proto = sk_filter_func_proto,
8467 .is_valid_access = sk_filter_is_valid_access,
8468 .convert_ctx_access = bpf_convert_ctx_access,
8469 .gen_ld_abs = bpf_gen_ld_abs,
8472 const struct bpf_prog_ops sk_filter_prog_ops = {
8473 .test_run = bpf_prog_test_run_skb,
8476 const struct bpf_verifier_ops tc_cls_act_verifier_ops = {
8477 .get_func_proto = tc_cls_act_func_proto,
8478 .is_valid_access = tc_cls_act_is_valid_access,
8479 .convert_ctx_access = tc_cls_act_convert_ctx_access,
8480 .gen_prologue = tc_cls_act_prologue,
8481 .gen_ld_abs = bpf_gen_ld_abs,
8484 const struct bpf_prog_ops tc_cls_act_prog_ops = {
8485 .test_run = bpf_prog_test_run_skb,
8488 const struct bpf_verifier_ops xdp_verifier_ops = {
8489 .get_func_proto = xdp_func_proto,
8490 .is_valid_access = xdp_is_valid_access,
8491 .convert_ctx_access = xdp_convert_ctx_access,
8492 .gen_prologue = bpf_noop_prologue,
8495 const struct bpf_prog_ops xdp_prog_ops = {
8496 .test_run = bpf_prog_test_run_xdp,
8499 const struct bpf_verifier_ops cg_skb_verifier_ops = {
8500 .get_func_proto = cg_skb_func_proto,
8501 .is_valid_access = cg_skb_is_valid_access,
8502 .convert_ctx_access = bpf_convert_ctx_access,
8505 const struct bpf_prog_ops cg_skb_prog_ops = {
8506 .test_run = bpf_prog_test_run_skb,
8509 const struct bpf_verifier_ops lwt_in_verifier_ops = {
8510 .get_func_proto = lwt_in_func_proto,
8511 .is_valid_access = lwt_is_valid_access,
8512 .convert_ctx_access = bpf_convert_ctx_access,
8515 const struct bpf_prog_ops lwt_in_prog_ops = {
8516 .test_run = bpf_prog_test_run_skb,
8519 const struct bpf_verifier_ops lwt_out_verifier_ops = {
8520 .get_func_proto = lwt_out_func_proto,
8521 .is_valid_access = lwt_is_valid_access,
8522 .convert_ctx_access = bpf_convert_ctx_access,
8525 const struct bpf_prog_ops lwt_out_prog_ops = {
8526 .test_run = bpf_prog_test_run_skb,
8529 const struct bpf_verifier_ops lwt_xmit_verifier_ops = {
8530 .get_func_proto = lwt_xmit_func_proto,
8531 .is_valid_access = lwt_is_valid_access,
8532 .convert_ctx_access = bpf_convert_ctx_access,
8533 .gen_prologue = tc_cls_act_prologue,
8536 const struct bpf_prog_ops lwt_xmit_prog_ops = {
8537 .test_run = bpf_prog_test_run_skb,
8540 const struct bpf_verifier_ops lwt_seg6local_verifier_ops = {
8541 .get_func_proto = lwt_seg6local_func_proto,
8542 .is_valid_access = lwt_is_valid_access,
8543 .convert_ctx_access = bpf_convert_ctx_access,
8546 const struct bpf_prog_ops lwt_seg6local_prog_ops = {
8547 .test_run = bpf_prog_test_run_skb,
8550 const struct bpf_verifier_ops cg_sock_verifier_ops = {
8551 .get_func_proto = sock_filter_func_proto,
8552 .is_valid_access = sock_filter_is_valid_access,
8553 .convert_ctx_access = bpf_sock_convert_ctx_access,
8556 const struct bpf_prog_ops cg_sock_prog_ops = {
8559 const struct bpf_verifier_ops cg_sock_addr_verifier_ops = {
8560 .get_func_proto = sock_addr_func_proto,
8561 .is_valid_access = sock_addr_is_valid_access,
8562 .convert_ctx_access = sock_addr_convert_ctx_access,
8565 const struct bpf_prog_ops cg_sock_addr_prog_ops = {
8568 const struct bpf_verifier_ops sock_ops_verifier_ops = {
8569 .get_func_proto = sock_ops_func_proto,
8570 .is_valid_access = sock_ops_is_valid_access,
8571 .convert_ctx_access = sock_ops_convert_ctx_access,
8574 const struct bpf_prog_ops sock_ops_prog_ops = {
8577 const struct bpf_verifier_ops sk_skb_verifier_ops = {
8578 .get_func_proto = sk_skb_func_proto,
8579 .is_valid_access = sk_skb_is_valid_access,
8580 .convert_ctx_access = sk_skb_convert_ctx_access,
8581 .gen_prologue = sk_skb_prologue,
8584 const struct bpf_prog_ops sk_skb_prog_ops = {
8587 const struct bpf_verifier_ops sk_msg_verifier_ops = {
8588 .get_func_proto = sk_msg_func_proto,
8589 .is_valid_access = sk_msg_is_valid_access,
8590 .convert_ctx_access = sk_msg_convert_ctx_access,
8591 .gen_prologue = bpf_noop_prologue,
8594 const struct bpf_prog_ops sk_msg_prog_ops = {
8597 const struct bpf_verifier_ops flow_dissector_verifier_ops = {
8598 .get_func_proto = flow_dissector_func_proto,
8599 .is_valid_access = flow_dissector_is_valid_access,
8600 .convert_ctx_access = flow_dissector_convert_ctx_access,
8603 const struct bpf_prog_ops flow_dissector_prog_ops = {
8604 .test_run = bpf_prog_test_run_flow_dissector,
8607 int sk_detach_filter(struct sock *sk)
8610 struct sk_filter *filter;
8612 if (sock_flag(sk, SOCK_FILTER_LOCKED))
8615 filter = rcu_dereference_protected(sk->sk_filter,
8616 lockdep_sock_is_held(sk));
8618 RCU_INIT_POINTER(sk->sk_filter, NULL);
8619 sk_filter_uncharge(sk, filter);
8625 EXPORT_SYMBOL_GPL(sk_detach_filter);
8627 int sk_get_filter(struct sock *sk, struct sock_filter __user *ubuf,
8630 struct sock_fprog_kern *fprog;
8631 struct sk_filter *filter;
8635 filter = rcu_dereference_protected(sk->sk_filter,
8636 lockdep_sock_is_held(sk));
8640 /* We're copying the filter that has been originally attached,
8641 * so no conversion/decode needed anymore. eBPF programs that
8642 * have no original program cannot be dumped through this.
8645 fprog = filter->prog->orig_prog;
8651 /* User space only enquires number of filter blocks. */
8655 if (len < fprog->len)
8659 if (copy_to_user(ubuf, fprog->filter, bpf_classic_proglen(fprog)))
8662 /* Instead of bytes, the API requests to return the number
8672 struct sk_reuseport_kern {
8673 struct sk_buff *skb;
8675 struct sock *selected_sk;
8682 static void bpf_init_reuseport_kern(struct sk_reuseport_kern *reuse_kern,
8683 struct sock_reuseport *reuse,
8684 struct sock *sk, struct sk_buff *skb,
8687 reuse_kern->skb = skb;
8688 reuse_kern->sk = sk;
8689 reuse_kern->selected_sk = NULL;
8690 reuse_kern->data_end = skb->data + skb_headlen(skb);
8691 reuse_kern->hash = hash;
8692 reuse_kern->reuseport_id = reuse->reuseport_id;
8693 reuse_kern->bind_inany = reuse->bind_inany;
8696 struct sock *bpf_run_sk_reuseport(struct sock_reuseport *reuse, struct sock *sk,
8697 struct bpf_prog *prog, struct sk_buff *skb,
8700 struct sk_reuseport_kern reuse_kern;
8701 enum sk_action action;
8703 bpf_init_reuseport_kern(&reuse_kern, reuse, sk, skb, hash);
8704 action = BPF_PROG_RUN(prog, &reuse_kern);
8706 if (action == SK_PASS)
8707 return reuse_kern.selected_sk;
8709 return ERR_PTR(-ECONNREFUSED);
8712 BPF_CALL_4(sk_select_reuseport, struct sk_reuseport_kern *, reuse_kern,
8713 struct bpf_map *, map, void *, key, u32, flags)
8715 struct sock_reuseport *reuse;
8716 struct sock *selected_sk;
8718 selected_sk = map->ops->map_lookup_elem(map, key);
8722 reuse = rcu_dereference(selected_sk->sk_reuseport_cb);
8724 /* selected_sk is unhashed (e.g. by close()) after the
8725 * above map_lookup_elem(). Treat selected_sk has already
8726 * been removed from the map.
8730 if (unlikely(reuse->reuseport_id != reuse_kern->reuseport_id)) {
8733 if (unlikely(!reuse_kern->reuseport_id))
8734 /* There is a small race between adding the
8735 * sk to the map and setting the
8736 * reuse_kern->reuseport_id.
8737 * Treat it as the sk has not been added to
8742 sk = reuse_kern->sk;
8743 if (sk->sk_protocol != selected_sk->sk_protocol)
8745 else if (sk->sk_family != selected_sk->sk_family)
8746 return -EAFNOSUPPORT;
8748 /* Catch all. Likely bound to a different sockaddr. */
8752 reuse_kern->selected_sk = selected_sk;
8757 static const struct bpf_func_proto sk_select_reuseport_proto = {
8758 .func = sk_select_reuseport,
8760 .ret_type = RET_INTEGER,
8761 .arg1_type = ARG_PTR_TO_CTX,
8762 .arg2_type = ARG_CONST_MAP_PTR,
8763 .arg3_type = ARG_PTR_TO_MAP_KEY,
8764 .arg4_type = ARG_ANYTHING,
8767 BPF_CALL_4(sk_reuseport_load_bytes,
8768 const struct sk_reuseport_kern *, reuse_kern, u32, offset,
8769 void *, to, u32, len)
8771 return ____bpf_skb_load_bytes(reuse_kern->skb, offset, to, len);
8774 static const struct bpf_func_proto sk_reuseport_load_bytes_proto = {
8775 .func = sk_reuseport_load_bytes,
8777 .ret_type = RET_INTEGER,
8778 .arg1_type = ARG_PTR_TO_CTX,
8779 .arg2_type = ARG_ANYTHING,
8780 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
8781 .arg4_type = ARG_CONST_SIZE,
8784 BPF_CALL_5(sk_reuseport_load_bytes_relative,
8785 const struct sk_reuseport_kern *, reuse_kern, u32, offset,
8786 void *, to, u32, len, u32, start_header)
8788 return ____bpf_skb_load_bytes_relative(reuse_kern->skb, offset, to,
8792 static const struct bpf_func_proto sk_reuseport_load_bytes_relative_proto = {
8793 .func = sk_reuseport_load_bytes_relative,
8795 .ret_type = RET_INTEGER,
8796 .arg1_type = ARG_PTR_TO_CTX,
8797 .arg2_type = ARG_ANYTHING,
8798 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
8799 .arg4_type = ARG_CONST_SIZE,
8800 .arg5_type = ARG_ANYTHING,
8803 static const struct bpf_func_proto *
8804 sk_reuseport_func_proto(enum bpf_func_id func_id,
8805 const struct bpf_prog *prog)
8808 case BPF_FUNC_sk_select_reuseport:
8809 return &sk_select_reuseport_proto;
8810 case BPF_FUNC_skb_load_bytes:
8811 return &sk_reuseport_load_bytes_proto;
8812 case BPF_FUNC_skb_load_bytes_relative:
8813 return &sk_reuseport_load_bytes_relative_proto;
8815 return bpf_base_func_proto(func_id);
8820 sk_reuseport_is_valid_access(int off, int size,
8821 enum bpf_access_type type,
8822 const struct bpf_prog *prog,
8823 struct bpf_insn_access_aux *info)
8825 const u32 size_default = sizeof(__u32);
8827 if (off < 0 || off >= sizeof(struct sk_reuseport_md) ||
8828 off % size || type != BPF_READ)
8832 case offsetof(struct sk_reuseport_md, data):
8833 info->reg_type = PTR_TO_PACKET;
8834 return size == sizeof(__u64);
8836 case offsetof(struct sk_reuseport_md, data_end):
8837 info->reg_type = PTR_TO_PACKET_END;
8838 return size == sizeof(__u64);
8840 case offsetof(struct sk_reuseport_md, hash):
8841 return size == size_default;
8843 /* Fields that allow narrowing */
8844 case bpf_ctx_range(struct sk_reuseport_md, eth_protocol):
8845 if (size < FIELD_SIZEOF(struct sk_buff, protocol))
8848 case bpf_ctx_range(struct sk_reuseport_md, ip_protocol):
8849 case bpf_ctx_range(struct sk_reuseport_md, bind_inany):
8850 case bpf_ctx_range(struct sk_reuseport_md, len):
8851 bpf_ctx_record_field_size(info, size_default);
8852 return bpf_ctx_narrow_access_ok(off, size, size_default);
8859 #define SK_REUSEPORT_LOAD_FIELD(F) ({ \
8860 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_reuseport_kern, F), \
8861 si->dst_reg, si->src_reg, \
8862 bpf_target_off(struct sk_reuseport_kern, F, \
8863 FIELD_SIZEOF(struct sk_reuseport_kern, F), \
8867 #define SK_REUSEPORT_LOAD_SKB_FIELD(SKB_FIELD) \
8868 SOCK_ADDR_LOAD_NESTED_FIELD(struct sk_reuseport_kern, \
8873 #define SK_REUSEPORT_LOAD_SK_FIELD_SIZE_OFF(SK_FIELD, BPF_SIZE, EXTRA_OFF) \
8874 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(struct sk_reuseport_kern, \
8877 SK_FIELD, BPF_SIZE, EXTRA_OFF)
8879 static u32 sk_reuseport_convert_ctx_access(enum bpf_access_type type,
8880 const struct bpf_insn *si,
8881 struct bpf_insn *insn_buf,
8882 struct bpf_prog *prog,
8885 struct bpf_insn *insn = insn_buf;
8888 case offsetof(struct sk_reuseport_md, data):
8889 SK_REUSEPORT_LOAD_SKB_FIELD(data);
8892 case offsetof(struct sk_reuseport_md, len):
8893 SK_REUSEPORT_LOAD_SKB_FIELD(len);
8896 case offsetof(struct sk_reuseport_md, eth_protocol):
8897 SK_REUSEPORT_LOAD_SKB_FIELD(protocol);
8900 case offsetof(struct sk_reuseport_md, ip_protocol):
8901 BUILD_BUG_ON(HWEIGHT32(SK_FL_PROTO_MASK) != BITS_PER_BYTE);
8902 SK_REUSEPORT_LOAD_SK_FIELD_SIZE_OFF(__sk_flags_offset,
8904 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_PROTO_MASK);
8905 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg,
8907 /* SK_FL_PROTO_MASK and SK_FL_PROTO_SHIFT are endian
8908 * aware. No further narrowing or masking is needed.
8913 case offsetof(struct sk_reuseport_md, data_end):
8914 SK_REUSEPORT_LOAD_FIELD(data_end);
8917 case offsetof(struct sk_reuseport_md, hash):
8918 SK_REUSEPORT_LOAD_FIELD(hash);
8921 case offsetof(struct sk_reuseport_md, bind_inany):
8922 SK_REUSEPORT_LOAD_FIELD(bind_inany);
8926 return insn - insn_buf;
8929 const struct bpf_verifier_ops sk_reuseport_verifier_ops = {
8930 .get_func_proto = sk_reuseport_func_proto,
8931 .is_valid_access = sk_reuseport_is_valid_access,
8932 .convert_ctx_access = sk_reuseport_convert_ctx_access,
8935 const struct bpf_prog_ops sk_reuseport_prog_ops = {
8937 #endif /* CONFIG_INET */