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 static u32 convert_skb_access(int skb_field, int dst_reg, int src_reg,
260 struct bpf_insn *insn_buf)
262 struct bpf_insn *insn = insn_buf;
266 BUILD_BUG_ON(sizeof_field(struct sk_buff, mark) != 4);
268 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
269 offsetof(struct sk_buff, mark));
273 *insn++ = BPF_LDX_MEM(BPF_B, dst_reg, src_reg, PKT_TYPE_OFFSET());
274 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, PKT_TYPE_MAX);
275 #ifdef __BIG_ENDIAN_BITFIELD
276 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 5);
281 BUILD_BUG_ON(sizeof_field(struct sk_buff, queue_mapping) != 2);
283 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
284 offsetof(struct sk_buff, queue_mapping));
287 case SKF_AD_VLAN_TAG:
288 BUILD_BUG_ON(sizeof_field(struct sk_buff, vlan_tci) != 2);
290 /* dst_reg = *(u16 *) (src_reg + offsetof(vlan_tci)) */
291 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
292 offsetof(struct sk_buff, vlan_tci));
294 case SKF_AD_VLAN_TAG_PRESENT:
295 *insn++ = BPF_LDX_MEM(BPF_B, dst_reg, src_reg, PKT_VLAN_PRESENT_OFFSET());
296 if (PKT_VLAN_PRESENT_BIT)
297 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, PKT_VLAN_PRESENT_BIT);
298 if (PKT_VLAN_PRESENT_BIT < 7)
299 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, 1);
303 return insn - insn_buf;
306 static bool convert_bpf_extensions(struct sock_filter *fp,
307 struct bpf_insn **insnp)
309 struct bpf_insn *insn = *insnp;
313 case SKF_AD_OFF + SKF_AD_PROTOCOL:
314 BUILD_BUG_ON(sizeof_field(struct sk_buff, protocol) != 2);
316 /* A = *(u16 *) (CTX + offsetof(protocol)) */
317 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
318 offsetof(struct sk_buff, protocol));
319 /* A = ntohs(A) [emitting a nop or swap16] */
320 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
323 case SKF_AD_OFF + SKF_AD_PKTTYPE:
324 cnt = convert_skb_access(SKF_AD_PKTTYPE, BPF_REG_A, BPF_REG_CTX, insn);
328 case SKF_AD_OFF + SKF_AD_IFINDEX:
329 case SKF_AD_OFF + SKF_AD_HATYPE:
330 BUILD_BUG_ON(sizeof_field(struct net_device, ifindex) != 4);
331 BUILD_BUG_ON(sizeof_field(struct net_device, type) != 2);
333 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
334 BPF_REG_TMP, BPF_REG_CTX,
335 offsetof(struct sk_buff, dev));
336 /* if (tmp != 0) goto pc + 1 */
337 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_TMP, 0, 1);
338 *insn++ = BPF_EXIT_INSN();
339 if (fp->k == SKF_AD_OFF + SKF_AD_IFINDEX)
340 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_TMP,
341 offsetof(struct net_device, ifindex));
343 *insn = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_TMP,
344 offsetof(struct net_device, type));
347 case SKF_AD_OFF + SKF_AD_MARK:
348 cnt = convert_skb_access(SKF_AD_MARK, BPF_REG_A, BPF_REG_CTX, insn);
352 case SKF_AD_OFF + SKF_AD_RXHASH:
353 BUILD_BUG_ON(sizeof_field(struct sk_buff, hash) != 4);
355 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX,
356 offsetof(struct sk_buff, hash));
359 case SKF_AD_OFF + SKF_AD_QUEUE:
360 cnt = convert_skb_access(SKF_AD_QUEUE, BPF_REG_A, BPF_REG_CTX, insn);
364 case SKF_AD_OFF + SKF_AD_VLAN_TAG:
365 cnt = convert_skb_access(SKF_AD_VLAN_TAG,
366 BPF_REG_A, BPF_REG_CTX, insn);
370 case SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT:
371 cnt = convert_skb_access(SKF_AD_VLAN_TAG_PRESENT,
372 BPF_REG_A, BPF_REG_CTX, insn);
376 case SKF_AD_OFF + SKF_AD_VLAN_TPID:
377 BUILD_BUG_ON(sizeof_field(struct sk_buff, vlan_proto) != 2);
379 /* A = *(u16 *) (CTX + offsetof(vlan_proto)) */
380 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
381 offsetof(struct sk_buff, vlan_proto));
382 /* A = ntohs(A) [emitting a nop or swap16] */
383 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
386 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
387 case SKF_AD_OFF + SKF_AD_NLATTR:
388 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
389 case SKF_AD_OFF + SKF_AD_CPU:
390 case SKF_AD_OFF + SKF_AD_RANDOM:
392 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX);
394 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_A);
396 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_X);
397 /* Emit call(arg1=CTX, arg2=A, arg3=X) */
399 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
400 *insn = BPF_EMIT_CALL(bpf_skb_get_pay_offset);
402 case SKF_AD_OFF + SKF_AD_NLATTR:
403 *insn = BPF_EMIT_CALL(bpf_skb_get_nlattr);
405 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
406 *insn = BPF_EMIT_CALL(bpf_skb_get_nlattr_nest);
408 case SKF_AD_OFF + SKF_AD_CPU:
409 *insn = BPF_EMIT_CALL(bpf_get_raw_cpu_id);
411 case SKF_AD_OFF + SKF_AD_RANDOM:
412 *insn = BPF_EMIT_CALL(bpf_user_rnd_u32);
413 bpf_user_rnd_init_once();
418 case SKF_AD_OFF + SKF_AD_ALU_XOR_X:
420 *insn = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_X);
424 /* This is just a dummy call to avoid letting the compiler
425 * evict __bpf_call_base() as an optimization. Placed here
426 * where no-one bothers.
428 BUG_ON(__bpf_call_base(0, 0, 0, 0, 0) != 0);
436 static bool convert_bpf_ld_abs(struct sock_filter *fp, struct bpf_insn **insnp)
438 const bool unaligned_ok = IS_BUILTIN(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS);
439 int size = bpf_size_to_bytes(BPF_SIZE(fp->code));
440 bool endian = BPF_SIZE(fp->code) == BPF_H ||
441 BPF_SIZE(fp->code) == BPF_W;
442 bool indirect = BPF_MODE(fp->code) == BPF_IND;
443 const int ip_align = NET_IP_ALIGN;
444 struct bpf_insn *insn = *insnp;
448 ((unaligned_ok && offset >= 0) ||
449 (!unaligned_ok && offset >= 0 &&
450 offset + ip_align >= 0 &&
451 offset + ip_align % size == 0))) {
452 bool ldx_off_ok = offset <= S16_MAX;
454 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_H);
456 *insn++ = BPF_ALU64_IMM(BPF_SUB, BPF_REG_TMP, offset);
457 *insn++ = BPF_JMP_IMM(BPF_JSLT, BPF_REG_TMP,
458 size, 2 + endian + (!ldx_off_ok * 2));
460 *insn++ = BPF_LDX_MEM(BPF_SIZE(fp->code), BPF_REG_A,
463 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_D);
464 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_TMP, offset);
465 *insn++ = BPF_LDX_MEM(BPF_SIZE(fp->code), BPF_REG_A,
469 *insn++ = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, size * 8);
470 *insn++ = BPF_JMP_A(8);
473 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX);
474 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_D);
475 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_H);
477 *insn++ = BPF_MOV64_IMM(BPF_REG_ARG4, offset);
479 *insn++ = BPF_MOV64_REG(BPF_REG_ARG4, BPF_REG_X);
481 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_ARG4, offset);
484 switch (BPF_SIZE(fp->code)) {
486 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_8);
489 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_16);
492 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_32);
498 *insn++ = BPF_JMP_IMM(BPF_JSGE, BPF_REG_A, 0, 2);
499 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
500 *insn = BPF_EXIT_INSN();
507 * bpf_convert_filter - convert filter program
508 * @prog: the user passed filter program
509 * @len: the length of the user passed filter program
510 * @new_prog: allocated 'struct bpf_prog' or NULL
511 * @new_len: pointer to store length of converted program
512 * @seen_ld_abs: bool whether we've seen ld_abs/ind
514 * Remap 'sock_filter' style classic BPF (cBPF) instruction set to 'bpf_insn'
515 * style extended BPF (eBPF).
516 * Conversion workflow:
518 * 1) First pass for calculating the new program length:
519 * bpf_convert_filter(old_prog, old_len, NULL, &new_len, &seen_ld_abs)
521 * 2) 2nd pass to remap in two passes: 1st pass finds new
522 * jump offsets, 2nd pass remapping:
523 * bpf_convert_filter(old_prog, old_len, new_prog, &new_len, &seen_ld_abs)
525 static int bpf_convert_filter(struct sock_filter *prog, int len,
526 struct bpf_prog *new_prog, int *new_len,
529 int new_flen = 0, pass = 0, target, i, stack_off;
530 struct bpf_insn *new_insn, *first_insn = NULL;
531 struct sock_filter *fp;
535 BUILD_BUG_ON(BPF_MEMWORDS * sizeof(u32) > MAX_BPF_STACK);
536 BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG);
538 if (len <= 0 || len > BPF_MAXINSNS)
542 first_insn = new_prog->insnsi;
543 addrs = kcalloc(len, sizeof(*addrs),
544 GFP_KERNEL | __GFP_NOWARN);
550 new_insn = first_insn;
553 /* Classic BPF related prologue emission. */
555 /* Classic BPF expects A and X to be reset first. These need
556 * to be guaranteed to be the first two instructions.
558 *new_insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
559 *new_insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_X, BPF_REG_X);
561 /* All programs must keep CTX in callee saved BPF_REG_CTX.
562 * In eBPF case it's done by the compiler, here we need to
563 * do this ourself. Initial CTX is present in BPF_REG_ARG1.
565 *new_insn++ = BPF_MOV64_REG(BPF_REG_CTX, BPF_REG_ARG1);
567 /* For packet access in classic BPF, cache skb->data
568 * in callee-saved BPF R8 and skb->len - skb->data_len
569 * (headlen) in BPF R9. Since classic BPF is read-only
570 * on CTX, we only need to cache it once.
572 *new_insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
573 BPF_REG_D, BPF_REG_CTX,
574 offsetof(struct sk_buff, data));
575 *new_insn++ = BPF_LDX_MEM(BPF_W, BPF_REG_H, BPF_REG_CTX,
576 offsetof(struct sk_buff, len));
577 *new_insn++ = BPF_LDX_MEM(BPF_W, BPF_REG_TMP, BPF_REG_CTX,
578 offsetof(struct sk_buff, data_len));
579 *new_insn++ = BPF_ALU32_REG(BPF_SUB, BPF_REG_H, BPF_REG_TMP);
585 for (i = 0; i < len; fp++, i++) {
586 struct bpf_insn tmp_insns[32] = { };
587 struct bpf_insn *insn = tmp_insns;
590 addrs[i] = new_insn - first_insn;
593 /* All arithmetic insns and skb loads map as-is. */
594 case BPF_ALU | BPF_ADD | BPF_X:
595 case BPF_ALU | BPF_ADD | BPF_K:
596 case BPF_ALU | BPF_SUB | BPF_X:
597 case BPF_ALU | BPF_SUB | BPF_K:
598 case BPF_ALU | BPF_AND | BPF_X:
599 case BPF_ALU | BPF_AND | BPF_K:
600 case BPF_ALU | BPF_OR | BPF_X:
601 case BPF_ALU | BPF_OR | BPF_K:
602 case BPF_ALU | BPF_LSH | BPF_X:
603 case BPF_ALU | BPF_LSH | BPF_K:
604 case BPF_ALU | BPF_RSH | BPF_X:
605 case BPF_ALU | BPF_RSH | BPF_K:
606 case BPF_ALU | BPF_XOR | BPF_X:
607 case BPF_ALU | BPF_XOR | BPF_K:
608 case BPF_ALU | BPF_MUL | BPF_X:
609 case BPF_ALU | BPF_MUL | BPF_K:
610 case BPF_ALU | BPF_DIV | BPF_X:
611 case BPF_ALU | BPF_DIV | BPF_K:
612 case BPF_ALU | BPF_MOD | BPF_X:
613 case BPF_ALU | BPF_MOD | BPF_K:
614 case BPF_ALU | BPF_NEG:
615 case BPF_LD | BPF_ABS | BPF_W:
616 case BPF_LD | BPF_ABS | BPF_H:
617 case BPF_LD | BPF_ABS | BPF_B:
618 case BPF_LD | BPF_IND | BPF_W:
619 case BPF_LD | BPF_IND | BPF_H:
620 case BPF_LD | BPF_IND | BPF_B:
621 /* Check for overloaded BPF extension and
622 * directly convert it if found, otherwise
623 * just move on with mapping.
625 if (BPF_CLASS(fp->code) == BPF_LD &&
626 BPF_MODE(fp->code) == BPF_ABS &&
627 convert_bpf_extensions(fp, &insn))
629 if (BPF_CLASS(fp->code) == BPF_LD &&
630 convert_bpf_ld_abs(fp, &insn)) {
635 if (fp->code == (BPF_ALU | BPF_DIV | BPF_X) ||
636 fp->code == (BPF_ALU | BPF_MOD | BPF_X)) {
637 *insn++ = BPF_MOV32_REG(BPF_REG_X, BPF_REG_X);
638 /* Error with exception code on div/mod by 0.
639 * For cBPF programs, this was always return 0.
641 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_X, 0, 2);
642 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
643 *insn++ = BPF_EXIT_INSN();
646 *insn = BPF_RAW_INSN(fp->code, BPF_REG_A, BPF_REG_X, 0, fp->k);
649 /* Jump transformation cannot use BPF block macros
650 * everywhere as offset calculation and target updates
651 * require a bit more work than the rest, i.e. jump
652 * opcodes map as-is, but offsets need adjustment.
655 #define BPF_EMIT_JMP \
657 const s32 off_min = S16_MIN, off_max = S16_MAX; \
660 if (target >= len || target < 0) \
662 off = addrs ? addrs[target] - addrs[i] - 1 : 0; \
663 /* Adjust pc relative offset for 2nd or 3rd insn. */ \
664 off -= insn - tmp_insns; \
665 /* Reject anything not fitting into insn->off. */ \
666 if (off < off_min || off > off_max) \
671 case BPF_JMP | BPF_JA:
672 target = i + fp->k + 1;
673 insn->code = fp->code;
677 case BPF_JMP | BPF_JEQ | BPF_K:
678 case BPF_JMP | BPF_JEQ | BPF_X:
679 case BPF_JMP | BPF_JSET | BPF_K:
680 case BPF_JMP | BPF_JSET | BPF_X:
681 case BPF_JMP | BPF_JGT | BPF_K:
682 case BPF_JMP | BPF_JGT | BPF_X:
683 case BPF_JMP | BPF_JGE | BPF_K:
684 case BPF_JMP | BPF_JGE | BPF_X:
685 if (BPF_SRC(fp->code) == BPF_K && (int) fp->k < 0) {
686 /* BPF immediates are signed, zero extend
687 * immediate into tmp register and use it
690 *insn++ = BPF_MOV32_IMM(BPF_REG_TMP, fp->k);
692 insn->dst_reg = BPF_REG_A;
693 insn->src_reg = BPF_REG_TMP;
696 insn->dst_reg = BPF_REG_A;
698 bpf_src = BPF_SRC(fp->code);
699 insn->src_reg = bpf_src == BPF_X ? BPF_REG_X : 0;
702 /* Common case where 'jump_false' is next insn. */
704 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
705 target = i + fp->jt + 1;
710 /* Convert some jumps when 'jump_true' is next insn. */
712 switch (BPF_OP(fp->code)) {
714 insn->code = BPF_JMP | BPF_JNE | bpf_src;
717 insn->code = BPF_JMP | BPF_JLE | bpf_src;
720 insn->code = BPF_JMP | BPF_JLT | bpf_src;
726 target = i + fp->jf + 1;
731 /* Other jumps are mapped into two insns: Jxx and JA. */
732 target = i + fp->jt + 1;
733 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
737 insn->code = BPF_JMP | BPF_JA;
738 target = i + fp->jf + 1;
742 /* ldxb 4 * ([14] & 0xf) is remaped into 6 insns. */
743 case BPF_LDX | BPF_MSH | BPF_B: {
744 struct sock_filter tmp = {
745 .code = BPF_LD | BPF_ABS | BPF_B,
752 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
753 /* A = BPF_R0 = *(u8 *) (skb->data + K) */
754 convert_bpf_ld_abs(&tmp, &insn);
757 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, 0xf);
759 *insn++ = BPF_ALU32_IMM(BPF_LSH, BPF_REG_A, 2);
761 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_X);
763 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
765 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_TMP);
768 /* RET_K is remaped into 2 insns. RET_A case doesn't need an
769 * extra mov as BPF_REG_0 is already mapped into BPF_REG_A.
771 case BPF_RET | BPF_A:
772 case BPF_RET | BPF_K:
773 if (BPF_RVAL(fp->code) == BPF_K)
774 *insn++ = BPF_MOV32_RAW(BPF_K, BPF_REG_0,
776 *insn = BPF_EXIT_INSN();
779 /* Store to stack. */
782 stack_off = fp->k * 4 + 4;
783 *insn = BPF_STX_MEM(BPF_W, BPF_REG_FP, BPF_CLASS(fp->code) ==
784 BPF_ST ? BPF_REG_A : BPF_REG_X,
786 /* check_load_and_stores() verifies that classic BPF can
787 * load from stack only after write, so tracking
788 * stack_depth for ST|STX insns is enough
790 if (new_prog && new_prog->aux->stack_depth < stack_off)
791 new_prog->aux->stack_depth = stack_off;
794 /* Load from stack. */
795 case BPF_LD | BPF_MEM:
796 case BPF_LDX | BPF_MEM:
797 stack_off = fp->k * 4 + 4;
798 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
799 BPF_REG_A : BPF_REG_X, BPF_REG_FP,
804 case BPF_LD | BPF_IMM:
805 case BPF_LDX | BPF_IMM:
806 *insn = BPF_MOV32_IMM(BPF_CLASS(fp->code) == BPF_LD ?
807 BPF_REG_A : BPF_REG_X, fp->k);
811 case BPF_MISC | BPF_TAX:
812 *insn = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
816 case BPF_MISC | BPF_TXA:
817 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_X);
820 /* A = skb->len or X = skb->len */
821 case BPF_LD | BPF_W | BPF_LEN:
822 case BPF_LDX | BPF_W | BPF_LEN:
823 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
824 BPF_REG_A : BPF_REG_X, BPF_REG_CTX,
825 offsetof(struct sk_buff, len));
828 /* Access seccomp_data fields. */
829 case BPF_LDX | BPF_ABS | BPF_W:
830 /* A = *(u32 *) (ctx + K) */
831 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX, fp->k);
834 /* Unknown instruction. */
841 memcpy(new_insn, tmp_insns,
842 sizeof(*insn) * (insn - tmp_insns));
843 new_insn += insn - tmp_insns;
847 /* Only calculating new length. */
848 *new_len = new_insn - first_insn;
850 *new_len += 4; /* Prologue bits. */
855 if (new_flen != new_insn - first_insn) {
856 new_flen = new_insn - first_insn;
863 BUG_ON(*new_len != new_flen);
872 * As we dont want to clear mem[] array for each packet going through
873 * __bpf_prog_run(), we check that filter loaded by user never try to read
874 * a cell if not previously written, and we check all branches to be sure
875 * a malicious user doesn't try to abuse us.
877 static int check_load_and_stores(const struct sock_filter *filter, int flen)
879 u16 *masks, memvalid = 0; /* One bit per cell, 16 cells */
882 BUILD_BUG_ON(BPF_MEMWORDS > 16);
884 masks = kmalloc_array(flen, sizeof(*masks), GFP_KERNEL);
888 memset(masks, 0xff, flen * sizeof(*masks));
890 for (pc = 0; pc < flen; pc++) {
891 memvalid &= masks[pc];
893 switch (filter[pc].code) {
896 memvalid |= (1 << filter[pc].k);
898 case BPF_LD | BPF_MEM:
899 case BPF_LDX | BPF_MEM:
900 if (!(memvalid & (1 << filter[pc].k))) {
905 case BPF_JMP | BPF_JA:
906 /* A jump must set masks on target */
907 masks[pc + 1 + filter[pc].k] &= memvalid;
910 case BPF_JMP | BPF_JEQ | BPF_K:
911 case BPF_JMP | BPF_JEQ | BPF_X:
912 case BPF_JMP | BPF_JGE | BPF_K:
913 case BPF_JMP | BPF_JGE | BPF_X:
914 case BPF_JMP | BPF_JGT | BPF_K:
915 case BPF_JMP | BPF_JGT | BPF_X:
916 case BPF_JMP | BPF_JSET | BPF_K:
917 case BPF_JMP | BPF_JSET | BPF_X:
918 /* A jump must set masks on targets */
919 masks[pc + 1 + filter[pc].jt] &= memvalid;
920 masks[pc + 1 + filter[pc].jf] &= memvalid;
930 static bool chk_code_allowed(u16 code_to_probe)
932 static const bool codes[] = {
933 /* 32 bit ALU operations */
934 [BPF_ALU | BPF_ADD | BPF_K] = true,
935 [BPF_ALU | BPF_ADD | BPF_X] = true,
936 [BPF_ALU | BPF_SUB | BPF_K] = true,
937 [BPF_ALU | BPF_SUB | BPF_X] = true,
938 [BPF_ALU | BPF_MUL | BPF_K] = true,
939 [BPF_ALU | BPF_MUL | BPF_X] = true,
940 [BPF_ALU | BPF_DIV | BPF_K] = true,
941 [BPF_ALU | BPF_DIV | BPF_X] = true,
942 [BPF_ALU | BPF_MOD | BPF_K] = true,
943 [BPF_ALU | BPF_MOD | BPF_X] = true,
944 [BPF_ALU | BPF_AND | BPF_K] = true,
945 [BPF_ALU | BPF_AND | BPF_X] = true,
946 [BPF_ALU | BPF_OR | BPF_K] = true,
947 [BPF_ALU | BPF_OR | BPF_X] = true,
948 [BPF_ALU | BPF_XOR | BPF_K] = true,
949 [BPF_ALU | BPF_XOR | BPF_X] = true,
950 [BPF_ALU | BPF_LSH | BPF_K] = true,
951 [BPF_ALU | BPF_LSH | BPF_X] = true,
952 [BPF_ALU | BPF_RSH | BPF_K] = true,
953 [BPF_ALU | BPF_RSH | BPF_X] = true,
954 [BPF_ALU | BPF_NEG] = true,
955 /* Load instructions */
956 [BPF_LD | BPF_W | BPF_ABS] = true,
957 [BPF_LD | BPF_H | BPF_ABS] = true,
958 [BPF_LD | BPF_B | BPF_ABS] = true,
959 [BPF_LD | BPF_W | BPF_LEN] = true,
960 [BPF_LD | BPF_W | BPF_IND] = true,
961 [BPF_LD | BPF_H | BPF_IND] = true,
962 [BPF_LD | BPF_B | BPF_IND] = true,
963 [BPF_LD | BPF_IMM] = true,
964 [BPF_LD | BPF_MEM] = true,
965 [BPF_LDX | BPF_W | BPF_LEN] = true,
966 [BPF_LDX | BPF_B | BPF_MSH] = true,
967 [BPF_LDX | BPF_IMM] = true,
968 [BPF_LDX | BPF_MEM] = true,
969 /* Store instructions */
972 /* Misc instructions */
973 [BPF_MISC | BPF_TAX] = true,
974 [BPF_MISC | BPF_TXA] = true,
975 /* Return instructions */
976 [BPF_RET | BPF_K] = true,
977 [BPF_RET | BPF_A] = true,
978 /* Jump instructions */
979 [BPF_JMP | BPF_JA] = true,
980 [BPF_JMP | BPF_JEQ | BPF_K] = true,
981 [BPF_JMP | BPF_JEQ | BPF_X] = true,
982 [BPF_JMP | BPF_JGE | BPF_K] = true,
983 [BPF_JMP | BPF_JGE | BPF_X] = true,
984 [BPF_JMP | BPF_JGT | BPF_K] = true,
985 [BPF_JMP | BPF_JGT | BPF_X] = true,
986 [BPF_JMP | BPF_JSET | BPF_K] = true,
987 [BPF_JMP | BPF_JSET | BPF_X] = true,
990 if (code_to_probe >= ARRAY_SIZE(codes))
993 return codes[code_to_probe];
996 static bool bpf_check_basics_ok(const struct sock_filter *filter,
1001 if (flen == 0 || flen > BPF_MAXINSNS)
1008 * bpf_check_classic - verify socket filter code
1009 * @filter: filter to verify
1010 * @flen: length of filter
1012 * Check the user's filter code. If we let some ugly
1013 * filter code slip through kaboom! The filter must contain
1014 * no references or jumps that are out of range, no illegal
1015 * instructions, and must end with a RET instruction.
1017 * All jumps are forward as they are not signed.
1019 * Returns 0 if the rule set is legal or -EINVAL if not.
1021 static int bpf_check_classic(const struct sock_filter *filter,
1027 /* Check the filter code now */
1028 for (pc = 0; pc < flen; pc++) {
1029 const struct sock_filter *ftest = &filter[pc];
1031 /* May we actually operate on this code? */
1032 if (!chk_code_allowed(ftest->code))
1035 /* Some instructions need special checks */
1036 switch (ftest->code) {
1037 case BPF_ALU | BPF_DIV | BPF_K:
1038 case BPF_ALU | BPF_MOD | BPF_K:
1039 /* Check for division by zero */
1043 case BPF_ALU | BPF_LSH | BPF_K:
1044 case BPF_ALU | BPF_RSH | BPF_K:
1048 case BPF_LD | BPF_MEM:
1049 case BPF_LDX | BPF_MEM:
1052 /* Check for invalid memory addresses */
1053 if (ftest->k >= BPF_MEMWORDS)
1056 case BPF_JMP | BPF_JA:
1057 /* Note, the large ftest->k might cause loops.
1058 * Compare this with conditional jumps below,
1059 * where offsets are limited. --ANK (981016)
1061 if (ftest->k >= (unsigned int)(flen - pc - 1))
1064 case BPF_JMP | BPF_JEQ | BPF_K:
1065 case BPF_JMP | BPF_JEQ | BPF_X:
1066 case BPF_JMP | BPF_JGE | BPF_K:
1067 case BPF_JMP | BPF_JGE | BPF_X:
1068 case BPF_JMP | BPF_JGT | BPF_K:
1069 case BPF_JMP | BPF_JGT | BPF_X:
1070 case BPF_JMP | BPF_JSET | BPF_K:
1071 case BPF_JMP | BPF_JSET | BPF_X:
1072 /* Both conditionals must be safe */
1073 if (pc + ftest->jt + 1 >= flen ||
1074 pc + ftest->jf + 1 >= flen)
1077 case BPF_LD | BPF_W | BPF_ABS:
1078 case BPF_LD | BPF_H | BPF_ABS:
1079 case BPF_LD | BPF_B | BPF_ABS:
1081 if (bpf_anc_helper(ftest) & BPF_ANC)
1083 /* Ancillary operation unknown or unsupported */
1084 if (anc_found == false && ftest->k >= SKF_AD_OFF)
1089 /* Last instruction must be a RET code */
1090 switch (filter[flen - 1].code) {
1091 case BPF_RET | BPF_K:
1092 case BPF_RET | BPF_A:
1093 return check_load_and_stores(filter, flen);
1099 static int bpf_prog_store_orig_filter(struct bpf_prog *fp,
1100 const struct sock_fprog *fprog)
1102 unsigned int fsize = bpf_classic_proglen(fprog);
1103 struct sock_fprog_kern *fkprog;
1105 fp->orig_prog = kmalloc(sizeof(*fkprog), GFP_KERNEL);
1109 fkprog = fp->orig_prog;
1110 fkprog->len = fprog->len;
1112 fkprog->filter = kmemdup(fp->insns, fsize,
1113 GFP_KERNEL | __GFP_NOWARN);
1114 if (!fkprog->filter) {
1115 kfree(fp->orig_prog);
1122 static void bpf_release_orig_filter(struct bpf_prog *fp)
1124 struct sock_fprog_kern *fprog = fp->orig_prog;
1127 kfree(fprog->filter);
1132 static void __bpf_prog_release(struct bpf_prog *prog)
1134 if (prog->type == BPF_PROG_TYPE_SOCKET_FILTER) {
1137 bpf_release_orig_filter(prog);
1138 bpf_prog_free(prog);
1142 static void __sk_filter_release(struct sk_filter *fp)
1144 __bpf_prog_release(fp->prog);
1149 * sk_filter_release_rcu - Release a socket filter by rcu_head
1150 * @rcu: rcu_head that contains the sk_filter to free
1152 static void sk_filter_release_rcu(struct rcu_head *rcu)
1154 struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu);
1156 __sk_filter_release(fp);
1160 * sk_filter_release - release a socket filter
1161 * @fp: filter to remove
1163 * Remove a filter from a socket and release its resources.
1165 static void sk_filter_release(struct sk_filter *fp)
1167 if (refcount_dec_and_test(&fp->refcnt))
1168 call_rcu(&fp->rcu, sk_filter_release_rcu);
1171 void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp)
1173 u32 filter_size = bpf_prog_size(fp->prog->len);
1175 atomic_sub(filter_size, &sk->sk_omem_alloc);
1176 sk_filter_release(fp);
1179 /* try to charge the socket memory if there is space available
1180 * return true on success
1182 static bool __sk_filter_charge(struct sock *sk, struct sk_filter *fp)
1184 u32 filter_size = bpf_prog_size(fp->prog->len);
1186 /* same check as in sock_kmalloc() */
1187 if (filter_size <= sysctl_optmem_max &&
1188 atomic_read(&sk->sk_omem_alloc) + filter_size < sysctl_optmem_max) {
1189 atomic_add(filter_size, &sk->sk_omem_alloc);
1195 bool sk_filter_charge(struct sock *sk, struct sk_filter *fp)
1197 if (!refcount_inc_not_zero(&fp->refcnt))
1200 if (!__sk_filter_charge(sk, fp)) {
1201 sk_filter_release(fp);
1207 static struct bpf_prog *bpf_migrate_filter(struct bpf_prog *fp)
1209 struct sock_filter *old_prog;
1210 struct bpf_prog *old_fp;
1211 int err, new_len, old_len = fp->len;
1212 bool seen_ld_abs = false;
1214 /* We are free to overwrite insns et al right here as it
1215 * won't be used at this point in time anymore internally
1216 * after the migration to the internal BPF instruction
1219 BUILD_BUG_ON(sizeof(struct sock_filter) !=
1220 sizeof(struct bpf_insn));
1222 /* Conversion cannot happen on overlapping memory areas,
1223 * so we need to keep the user BPF around until the 2nd
1224 * pass. At this time, the user BPF is stored in fp->insns.
1226 old_prog = kmemdup(fp->insns, old_len * sizeof(struct sock_filter),
1227 GFP_KERNEL | __GFP_NOWARN);
1233 /* 1st pass: calculate the new program length. */
1234 err = bpf_convert_filter(old_prog, old_len, NULL, &new_len,
1239 /* Expand fp for appending the new filter representation. */
1241 fp = bpf_prog_realloc(old_fp, bpf_prog_size(new_len), 0);
1243 /* The old_fp is still around in case we couldn't
1244 * allocate new memory, so uncharge on that one.
1253 /* 2nd pass: remap sock_filter insns into bpf_insn insns. */
1254 err = bpf_convert_filter(old_prog, old_len, fp, &new_len,
1257 /* 2nd bpf_convert_filter() can fail only if it fails
1258 * to allocate memory, remapping must succeed. Note,
1259 * that at this time old_fp has already been released
1264 fp = bpf_prog_select_runtime(fp, &err);
1274 __bpf_prog_release(fp);
1275 return ERR_PTR(err);
1278 static struct bpf_prog *bpf_prepare_filter(struct bpf_prog *fp,
1279 bpf_aux_classic_check_t trans)
1283 fp->bpf_func = NULL;
1286 err = bpf_check_classic(fp->insns, fp->len);
1288 __bpf_prog_release(fp);
1289 return ERR_PTR(err);
1292 /* There might be additional checks and transformations
1293 * needed on classic filters, f.e. in case of seccomp.
1296 err = trans(fp->insns, fp->len);
1298 __bpf_prog_release(fp);
1299 return ERR_PTR(err);
1303 /* Probe if we can JIT compile the filter and if so, do
1304 * the compilation of the filter.
1306 bpf_jit_compile(fp);
1308 /* JIT compiler couldn't process this filter, so do the
1309 * internal BPF translation for the optimized interpreter.
1312 fp = bpf_migrate_filter(fp);
1318 * bpf_prog_create - create an unattached filter
1319 * @pfp: the unattached filter that is created
1320 * @fprog: the filter program
1322 * Create a filter independent of any socket. We first run some
1323 * sanity checks on it to make sure it does not explode on us later.
1324 * If an error occurs or there is insufficient memory for the filter
1325 * a negative errno code is returned. On success the return is zero.
1327 int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog)
1329 unsigned int fsize = bpf_classic_proglen(fprog);
1330 struct bpf_prog *fp;
1332 /* Make sure new filter is there and in the right amounts. */
1333 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1336 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1340 memcpy(fp->insns, fprog->filter, fsize);
1342 fp->len = fprog->len;
1343 /* Since unattached filters are not copied back to user
1344 * space through sk_get_filter(), we do not need to hold
1345 * a copy here, and can spare us the work.
1347 fp->orig_prog = NULL;
1349 /* bpf_prepare_filter() already takes care of freeing
1350 * memory in case something goes wrong.
1352 fp = bpf_prepare_filter(fp, NULL);
1359 EXPORT_SYMBOL_GPL(bpf_prog_create);
1362 * bpf_prog_create_from_user - create an unattached filter from user buffer
1363 * @pfp: the unattached filter that is created
1364 * @fprog: the filter program
1365 * @trans: post-classic verifier transformation handler
1366 * @save_orig: save classic BPF program
1368 * This function effectively does the same as bpf_prog_create(), only
1369 * that it builds up its insns buffer from user space provided buffer.
1370 * It also allows for passing a bpf_aux_classic_check_t handler.
1372 int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog,
1373 bpf_aux_classic_check_t trans, bool save_orig)
1375 unsigned int fsize = bpf_classic_proglen(fprog);
1376 struct bpf_prog *fp;
1379 /* Make sure new filter is there and in the right amounts. */
1380 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1383 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1387 if (copy_from_user(fp->insns, fprog->filter, fsize)) {
1388 __bpf_prog_free(fp);
1392 fp->len = fprog->len;
1393 fp->orig_prog = NULL;
1396 err = bpf_prog_store_orig_filter(fp, fprog);
1398 __bpf_prog_free(fp);
1403 /* bpf_prepare_filter() already takes care of freeing
1404 * memory in case something goes wrong.
1406 fp = bpf_prepare_filter(fp, trans);
1413 EXPORT_SYMBOL_GPL(bpf_prog_create_from_user);
1415 void bpf_prog_destroy(struct bpf_prog *fp)
1417 __bpf_prog_release(fp);
1419 EXPORT_SYMBOL_GPL(bpf_prog_destroy);
1421 static int __sk_attach_prog(struct bpf_prog *prog, struct sock *sk)
1423 struct sk_filter *fp, *old_fp;
1425 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
1431 if (!__sk_filter_charge(sk, fp)) {
1435 refcount_set(&fp->refcnt, 1);
1437 old_fp = rcu_dereference_protected(sk->sk_filter,
1438 lockdep_sock_is_held(sk));
1439 rcu_assign_pointer(sk->sk_filter, fp);
1442 sk_filter_uncharge(sk, old_fp);
1448 struct bpf_prog *__get_filter(struct sock_fprog *fprog, struct sock *sk)
1450 unsigned int fsize = bpf_classic_proglen(fprog);
1451 struct bpf_prog *prog;
1454 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1455 return ERR_PTR(-EPERM);
1457 /* Make sure new filter is there and in the right amounts. */
1458 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1459 return ERR_PTR(-EINVAL);
1461 prog = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1463 return ERR_PTR(-ENOMEM);
1465 if (copy_from_user(prog->insns, fprog->filter, fsize)) {
1466 __bpf_prog_free(prog);
1467 return ERR_PTR(-EFAULT);
1470 prog->len = fprog->len;
1472 err = bpf_prog_store_orig_filter(prog, fprog);
1474 __bpf_prog_free(prog);
1475 return ERR_PTR(-ENOMEM);
1478 /* bpf_prepare_filter() already takes care of freeing
1479 * memory in case something goes wrong.
1481 return bpf_prepare_filter(prog, NULL);
1485 * sk_attach_filter - attach a socket filter
1486 * @fprog: the filter program
1487 * @sk: the socket to use
1489 * Attach the user's filter code. We first run some sanity checks on
1490 * it to make sure it does not explode on us later. If an error
1491 * occurs or there is insufficient memory for the filter a negative
1492 * errno code is returned. On success the return is zero.
1494 int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1496 struct bpf_prog *prog = __get_filter(fprog, sk);
1500 return PTR_ERR(prog);
1502 err = __sk_attach_prog(prog, sk);
1504 __bpf_prog_release(prog);
1510 EXPORT_SYMBOL_GPL(sk_attach_filter);
1512 int sk_reuseport_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1514 struct bpf_prog *prog = __get_filter(fprog, sk);
1518 return PTR_ERR(prog);
1520 if (bpf_prog_size(prog->len) > sysctl_optmem_max)
1523 err = reuseport_attach_prog(sk, prog);
1526 __bpf_prog_release(prog);
1531 static struct bpf_prog *__get_bpf(u32 ufd, struct sock *sk)
1533 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1534 return ERR_PTR(-EPERM);
1536 return bpf_prog_get_type(ufd, BPF_PROG_TYPE_SOCKET_FILTER);
1539 int sk_attach_bpf(u32 ufd, struct sock *sk)
1541 struct bpf_prog *prog = __get_bpf(ufd, sk);
1545 return PTR_ERR(prog);
1547 err = __sk_attach_prog(prog, sk);
1556 int sk_reuseport_attach_bpf(u32 ufd, struct sock *sk)
1558 struct bpf_prog *prog;
1561 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1564 prog = bpf_prog_get_type(ufd, BPF_PROG_TYPE_SOCKET_FILTER);
1565 if (PTR_ERR(prog) == -EINVAL)
1566 prog = bpf_prog_get_type(ufd, BPF_PROG_TYPE_SK_REUSEPORT);
1568 return PTR_ERR(prog);
1570 if (prog->type == BPF_PROG_TYPE_SK_REUSEPORT) {
1571 /* Like other non BPF_PROG_TYPE_SOCKET_FILTER
1572 * bpf prog (e.g. sockmap). It depends on the
1573 * limitation imposed by bpf_prog_load().
1574 * Hence, sysctl_optmem_max is not checked.
1576 if ((sk->sk_type != SOCK_STREAM &&
1577 sk->sk_type != SOCK_DGRAM) ||
1578 (sk->sk_protocol != IPPROTO_UDP &&
1579 sk->sk_protocol != IPPROTO_TCP) ||
1580 (sk->sk_family != AF_INET &&
1581 sk->sk_family != AF_INET6)) {
1586 /* BPF_PROG_TYPE_SOCKET_FILTER */
1587 if (bpf_prog_size(prog->len) > sysctl_optmem_max) {
1593 err = reuseport_attach_prog(sk, prog);
1601 void sk_reuseport_prog_free(struct bpf_prog *prog)
1606 if (prog->type == BPF_PROG_TYPE_SK_REUSEPORT)
1609 bpf_prog_destroy(prog);
1612 struct bpf_scratchpad {
1614 __be32 diff[MAX_BPF_STACK / sizeof(__be32)];
1615 u8 buff[MAX_BPF_STACK];
1619 static DEFINE_PER_CPU(struct bpf_scratchpad, bpf_sp);
1621 static inline int __bpf_try_make_writable(struct sk_buff *skb,
1622 unsigned int write_len)
1624 return skb_ensure_writable(skb, write_len);
1627 static inline int bpf_try_make_writable(struct sk_buff *skb,
1628 unsigned int write_len)
1630 int err = __bpf_try_make_writable(skb, write_len);
1632 bpf_compute_data_pointers(skb);
1636 static int bpf_try_make_head_writable(struct sk_buff *skb)
1638 return bpf_try_make_writable(skb, skb_headlen(skb));
1641 static inline void bpf_push_mac_rcsum(struct sk_buff *skb)
1643 if (skb_at_tc_ingress(skb))
1644 skb_postpush_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1647 static inline void bpf_pull_mac_rcsum(struct sk_buff *skb)
1649 if (skb_at_tc_ingress(skb))
1650 skb_postpull_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1653 BPF_CALL_5(bpf_skb_store_bytes, struct sk_buff *, skb, u32, offset,
1654 const void *, from, u32, len, u64, flags)
1658 if (unlikely(flags & ~(BPF_F_RECOMPUTE_CSUM | BPF_F_INVALIDATE_HASH)))
1660 if (unlikely(offset > 0xffff))
1662 if (unlikely(bpf_try_make_writable(skb, offset + len)))
1665 ptr = skb->data + offset;
1666 if (flags & BPF_F_RECOMPUTE_CSUM)
1667 __skb_postpull_rcsum(skb, ptr, len, offset);
1669 memcpy(ptr, from, len);
1671 if (flags & BPF_F_RECOMPUTE_CSUM)
1672 __skb_postpush_rcsum(skb, ptr, len, offset);
1673 if (flags & BPF_F_INVALIDATE_HASH)
1674 skb_clear_hash(skb);
1679 static const struct bpf_func_proto bpf_skb_store_bytes_proto = {
1680 .func = bpf_skb_store_bytes,
1682 .ret_type = RET_INTEGER,
1683 .arg1_type = ARG_PTR_TO_CTX,
1684 .arg2_type = ARG_ANYTHING,
1685 .arg3_type = ARG_PTR_TO_MEM,
1686 .arg4_type = ARG_CONST_SIZE,
1687 .arg5_type = ARG_ANYTHING,
1690 BPF_CALL_4(bpf_skb_load_bytes, const struct sk_buff *, skb, u32, offset,
1691 void *, to, u32, len)
1695 if (unlikely(offset > 0xffff))
1698 ptr = skb_header_pointer(skb, offset, len, to);
1702 memcpy(to, ptr, len);
1710 static const struct bpf_func_proto bpf_skb_load_bytes_proto = {
1711 .func = bpf_skb_load_bytes,
1713 .ret_type = RET_INTEGER,
1714 .arg1_type = ARG_PTR_TO_CTX,
1715 .arg2_type = ARG_ANYTHING,
1716 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
1717 .arg4_type = ARG_CONST_SIZE,
1720 BPF_CALL_4(bpf_flow_dissector_load_bytes,
1721 const struct bpf_flow_dissector *, ctx, u32, offset,
1722 void *, to, u32, len)
1726 if (unlikely(offset > 0xffff))
1729 if (unlikely(!ctx->skb))
1732 ptr = skb_header_pointer(ctx->skb, offset, len, to);
1736 memcpy(to, ptr, len);
1744 static const struct bpf_func_proto bpf_flow_dissector_load_bytes_proto = {
1745 .func = bpf_flow_dissector_load_bytes,
1747 .ret_type = RET_INTEGER,
1748 .arg1_type = ARG_PTR_TO_CTX,
1749 .arg2_type = ARG_ANYTHING,
1750 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
1751 .arg4_type = ARG_CONST_SIZE,
1754 BPF_CALL_5(bpf_skb_load_bytes_relative, const struct sk_buff *, skb,
1755 u32, offset, void *, to, u32, len, u32, start_header)
1757 u8 *end = skb_tail_pointer(skb);
1760 if (unlikely(offset > 0xffff))
1763 switch (start_header) {
1764 case BPF_HDR_START_MAC:
1765 if (unlikely(!skb_mac_header_was_set(skb)))
1767 start = skb_mac_header(skb);
1769 case BPF_HDR_START_NET:
1770 start = skb_network_header(skb);
1776 ptr = start + offset;
1778 if (likely(ptr + len <= end)) {
1779 memcpy(to, ptr, len);
1788 static const struct bpf_func_proto bpf_skb_load_bytes_relative_proto = {
1789 .func = bpf_skb_load_bytes_relative,
1791 .ret_type = RET_INTEGER,
1792 .arg1_type = ARG_PTR_TO_CTX,
1793 .arg2_type = ARG_ANYTHING,
1794 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
1795 .arg4_type = ARG_CONST_SIZE,
1796 .arg5_type = ARG_ANYTHING,
1799 BPF_CALL_2(bpf_skb_pull_data, struct sk_buff *, skb, u32, len)
1801 /* Idea is the following: should the needed direct read/write
1802 * test fail during runtime, we can pull in more data and redo
1803 * again, since implicitly, we invalidate previous checks here.
1805 * Or, since we know how much we need to make read/writeable,
1806 * this can be done once at the program beginning for direct
1807 * access case. By this we overcome limitations of only current
1808 * headroom being accessible.
1810 return bpf_try_make_writable(skb, len ? : skb_headlen(skb));
1813 static const struct bpf_func_proto bpf_skb_pull_data_proto = {
1814 .func = bpf_skb_pull_data,
1816 .ret_type = RET_INTEGER,
1817 .arg1_type = ARG_PTR_TO_CTX,
1818 .arg2_type = ARG_ANYTHING,
1821 BPF_CALL_1(bpf_sk_fullsock, struct sock *, sk)
1823 return sk_fullsock(sk) ? (unsigned long)sk : (unsigned long)NULL;
1826 static const struct bpf_func_proto bpf_sk_fullsock_proto = {
1827 .func = bpf_sk_fullsock,
1829 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
1830 .arg1_type = ARG_PTR_TO_SOCK_COMMON,
1833 static inline int sk_skb_try_make_writable(struct sk_buff *skb,
1834 unsigned int write_len)
1836 int err = __bpf_try_make_writable(skb, write_len);
1838 bpf_compute_data_end_sk_skb(skb);
1842 BPF_CALL_2(sk_skb_pull_data, struct sk_buff *, skb, u32, len)
1844 /* Idea is the following: should the needed direct read/write
1845 * test fail during runtime, we can pull in more data and redo
1846 * again, since implicitly, we invalidate previous checks here.
1848 * Or, since we know how much we need to make read/writeable,
1849 * this can be done once at the program beginning for direct
1850 * access case. By this we overcome limitations of only current
1851 * headroom being accessible.
1853 return sk_skb_try_make_writable(skb, len ? : skb_headlen(skb));
1856 static const struct bpf_func_proto sk_skb_pull_data_proto = {
1857 .func = sk_skb_pull_data,
1859 .ret_type = RET_INTEGER,
1860 .arg1_type = ARG_PTR_TO_CTX,
1861 .arg2_type = ARG_ANYTHING,
1864 BPF_CALL_5(bpf_l3_csum_replace, struct sk_buff *, skb, u32, offset,
1865 u64, from, u64, to, u64, flags)
1869 if (unlikely(flags & ~(BPF_F_HDR_FIELD_MASK)))
1871 if (unlikely(offset > 0xffff || offset & 1))
1873 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
1876 ptr = (__sum16 *)(skb->data + offset);
1877 switch (flags & BPF_F_HDR_FIELD_MASK) {
1879 if (unlikely(from != 0))
1882 csum_replace_by_diff(ptr, to);
1885 csum_replace2(ptr, from, to);
1888 csum_replace4(ptr, from, to);
1897 static const struct bpf_func_proto bpf_l3_csum_replace_proto = {
1898 .func = bpf_l3_csum_replace,
1900 .ret_type = RET_INTEGER,
1901 .arg1_type = ARG_PTR_TO_CTX,
1902 .arg2_type = ARG_ANYTHING,
1903 .arg3_type = ARG_ANYTHING,
1904 .arg4_type = ARG_ANYTHING,
1905 .arg5_type = ARG_ANYTHING,
1908 BPF_CALL_5(bpf_l4_csum_replace, struct sk_buff *, skb, u32, offset,
1909 u64, from, u64, to, u64, flags)
1911 bool is_pseudo = flags & BPF_F_PSEUDO_HDR;
1912 bool is_mmzero = flags & BPF_F_MARK_MANGLED_0;
1913 bool do_mforce = flags & BPF_F_MARK_ENFORCE;
1916 if (unlikely(flags & ~(BPF_F_MARK_MANGLED_0 | BPF_F_MARK_ENFORCE |
1917 BPF_F_PSEUDO_HDR | BPF_F_HDR_FIELD_MASK)))
1919 if (unlikely(offset > 0xffff || offset & 1))
1921 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
1924 ptr = (__sum16 *)(skb->data + offset);
1925 if (is_mmzero && !do_mforce && !*ptr)
1928 switch (flags & BPF_F_HDR_FIELD_MASK) {
1930 if (unlikely(from != 0))
1933 inet_proto_csum_replace_by_diff(ptr, skb, to, is_pseudo);
1936 inet_proto_csum_replace2(ptr, skb, from, to, is_pseudo);
1939 inet_proto_csum_replace4(ptr, skb, from, to, is_pseudo);
1945 if (is_mmzero && !*ptr)
1946 *ptr = CSUM_MANGLED_0;
1950 static const struct bpf_func_proto bpf_l4_csum_replace_proto = {
1951 .func = bpf_l4_csum_replace,
1953 .ret_type = RET_INTEGER,
1954 .arg1_type = ARG_PTR_TO_CTX,
1955 .arg2_type = ARG_ANYTHING,
1956 .arg3_type = ARG_ANYTHING,
1957 .arg4_type = ARG_ANYTHING,
1958 .arg5_type = ARG_ANYTHING,
1961 BPF_CALL_5(bpf_csum_diff, __be32 *, from, u32, from_size,
1962 __be32 *, to, u32, to_size, __wsum, seed)
1964 struct bpf_scratchpad *sp = this_cpu_ptr(&bpf_sp);
1965 u32 diff_size = from_size + to_size;
1968 /* This is quite flexible, some examples:
1970 * from_size == 0, to_size > 0, seed := csum --> pushing data
1971 * from_size > 0, to_size == 0, seed := csum --> pulling data
1972 * from_size > 0, to_size > 0, seed := 0 --> diffing data
1974 * Even for diffing, from_size and to_size don't need to be equal.
1976 if (unlikely(((from_size | to_size) & (sizeof(__be32) - 1)) ||
1977 diff_size > sizeof(sp->diff)))
1980 for (i = 0; i < from_size / sizeof(__be32); i++, j++)
1981 sp->diff[j] = ~from[i];
1982 for (i = 0; i < to_size / sizeof(__be32); i++, j++)
1983 sp->diff[j] = to[i];
1985 return csum_partial(sp->diff, diff_size, seed);
1988 static const struct bpf_func_proto bpf_csum_diff_proto = {
1989 .func = bpf_csum_diff,
1992 .ret_type = RET_INTEGER,
1993 .arg1_type = ARG_PTR_TO_MEM_OR_NULL,
1994 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
1995 .arg3_type = ARG_PTR_TO_MEM_OR_NULL,
1996 .arg4_type = ARG_CONST_SIZE_OR_ZERO,
1997 .arg5_type = ARG_ANYTHING,
2000 BPF_CALL_2(bpf_csum_update, struct sk_buff *, skb, __wsum, csum)
2002 /* The interface is to be used in combination with bpf_csum_diff()
2003 * for direct packet writes. csum rotation for alignment as well
2004 * as emulating csum_sub() can be done from the eBPF program.
2006 if (skb->ip_summed == CHECKSUM_COMPLETE)
2007 return (skb->csum = csum_add(skb->csum, csum));
2012 static const struct bpf_func_proto bpf_csum_update_proto = {
2013 .func = bpf_csum_update,
2015 .ret_type = RET_INTEGER,
2016 .arg1_type = ARG_PTR_TO_CTX,
2017 .arg2_type = ARG_ANYTHING,
2020 BPF_CALL_2(bpf_csum_level, struct sk_buff *, skb, u64, level)
2022 /* The interface is to be used in combination with bpf_skb_adjust_room()
2023 * for encap/decap of packet headers when BPF_F_ADJ_ROOM_NO_CSUM_RESET
2024 * is passed as flags, for example.
2027 case BPF_CSUM_LEVEL_INC:
2028 __skb_incr_checksum_unnecessary(skb);
2030 case BPF_CSUM_LEVEL_DEC:
2031 __skb_decr_checksum_unnecessary(skb);
2033 case BPF_CSUM_LEVEL_RESET:
2034 __skb_reset_checksum_unnecessary(skb);
2036 case BPF_CSUM_LEVEL_QUERY:
2037 return skb->ip_summed == CHECKSUM_UNNECESSARY ?
2038 skb->csum_level : -EACCES;
2046 static const struct bpf_func_proto bpf_csum_level_proto = {
2047 .func = bpf_csum_level,
2049 .ret_type = RET_INTEGER,
2050 .arg1_type = ARG_PTR_TO_CTX,
2051 .arg2_type = ARG_ANYTHING,
2054 static inline int __bpf_rx_skb(struct net_device *dev, struct sk_buff *skb)
2056 return dev_forward_skb(dev, skb);
2059 static inline int __bpf_rx_skb_no_mac(struct net_device *dev,
2060 struct sk_buff *skb)
2062 int ret = ____dev_forward_skb(dev, skb);
2066 ret = netif_rx(skb);
2072 static inline int __bpf_tx_skb(struct net_device *dev, struct sk_buff *skb)
2076 if (dev_xmit_recursion()) {
2077 net_crit_ratelimited("bpf: recursion limit reached on datapath, buggy bpf program?\n");
2085 dev_xmit_recursion_inc();
2086 ret = dev_queue_xmit(skb);
2087 dev_xmit_recursion_dec();
2092 static int __bpf_redirect_no_mac(struct sk_buff *skb, struct net_device *dev,
2095 unsigned int mlen = skb_network_offset(skb);
2098 __skb_pull(skb, mlen);
2100 /* At ingress, the mac header has already been pulled once.
2101 * At egress, skb_pospull_rcsum has to be done in case that
2102 * the skb is originated from ingress (i.e. a forwarded skb)
2103 * to ensure that rcsum starts at net header.
2105 if (!skb_at_tc_ingress(skb))
2106 skb_postpull_rcsum(skb, skb_mac_header(skb), mlen);
2108 skb_pop_mac_header(skb);
2109 skb_reset_mac_len(skb);
2110 return flags & BPF_F_INGRESS ?
2111 __bpf_rx_skb_no_mac(dev, skb) : __bpf_tx_skb(dev, skb);
2114 static int __bpf_redirect_common(struct sk_buff *skb, struct net_device *dev,
2117 /* Verify that a link layer header is carried */
2118 if (unlikely(skb->mac_header >= skb->network_header)) {
2123 bpf_push_mac_rcsum(skb);
2124 return flags & BPF_F_INGRESS ?
2125 __bpf_rx_skb(dev, skb) : __bpf_tx_skb(dev, skb);
2128 static int __bpf_redirect(struct sk_buff *skb, struct net_device *dev,
2131 if (dev_is_mac_header_xmit(dev))
2132 return __bpf_redirect_common(skb, dev, flags);
2134 return __bpf_redirect_no_mac(skb, dev, flags);
2137 BPF_CALL_3(bpf_clone_redirect, struct sk_buff *, skb, u32, ifindex, u64, flags)
2139 struct net_device *dev;
2140 struct sk_buff *clone;
2143 if (unlikely(flags & ~(BPF_F_INGRESS)))
2146 dev = dev_get_by_index_rcu(dev_net(skb->dev), ifindex);
2150 clone = skb_clone(skb, GFP_ATOMIC);
2151 if (unlikely(!clone))
2154 /* For direct write, we need to keep the invariant that the skbs
2155 * we're dealing with need to be uncloned. Should uncloning fail
2156 * here, we need to free the just generated clone to unclone once
2159 ret = bpf_try_make_head_writable(skb);
2160 if (unlikely(ret)) {
2165 return __bpf_redirect(clone, dev, flags);
2168 static const struct bpf_func_proto bpf_clone_redirect_proto = {
2169 .func = bpf_clone_redirect,
2171 .ret_type = RET_INTEGER,
2172 .arg1_type = ARG_PTR_TO_CTX,
2173 .arg2_type = ARG_ANYTHING,
2174 .arg3_type = ARG_ANYTHING,
2177 DEFINE_PER_CPU(struct bpf_redirect_info, bpf_redirect_info);
2178 EXPORT_PER_CPU_SYMBOL_GPL(bpf_redirect_info);
2180 BPF_CALL_2(bpf_redirect, u32, ifindex, u64, flags)
2182 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
2184 if (unlikely(flags & ~(BPF_F_INGRESS)))
2188 ri->tgt_index = ifindex;
2190 return TC_ACT_REDIRECT;
2193 int skb_do_redirect(struct sk_buff *skb)
2195 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
2196 struct net_device *dev;
2198 dev = dev_get_by_index_rcu(dev_net(skb->dev), ri->tgt_index);
2200 if (unlikely(!dev)) {
2205 return __bpf_redirect(skb, dev, ri->flags);
2208 static const struct bpf_func_proto bpf_redirect_proto = {
2209 .func = bpf_redirect,
2211 .ret_type = RET_INTEGER,
2212 .arg1_type = ARG_ANYTHING,
2213 .arg2_type = ARG_ANYTHING,
2216 BPF_CALL_2(bpf_msg_apply_bytes, struct sk_msg *, msg, u32, bytes)
2218 msg->apply_bytes = bytes;
2222 static const struct bpf_func_proto bpf_msg_apply_bytes_proto = {
2223 .func = bpf_msg_apply_bytes,
2225 .ret_type = RET_INTEGER,
2226 .arg1_type = ARG_PTR_TO_CTX,
2227 .arg2_type = ARG_ANYTHING,
2230 BPF_CALL_2(bpf_msg_cork_bytes, struct sk_msg *, msg, u32, bytes)
2232 msg->cork_bytes = bytes;
2236 static const struct bpf_func_proto bpf_msg_cork_bytes_proto = {
2237 .func = bpf_msg_cork_bytes,
2239 .ret_type = RET_INTEGER,
2240 .arg1_type = ARG_PTR_TO_CTX,
2241 .arg2_type = ARG_ANYTHING,
2244 BPF_CALL_4(bpf_msg_pull_data, struct sk_msg *, msg, u32, start,
2245 u32, end, u64, flags)
2247 u32 len = 0, offset = 0, copy = 0, poffset = 0, bytes = end - start;
2248 u32 first_sge, last_sge, i, shift, bytes_sg_total;
2249 struct scatterlist *sge;
2250 u8 *raw, *to, *from;
2253 if (unlikely(flags || end <= start))
2256 /* First find the starting scatterlist element */
2260 len = sk_msg_elem(msg, i)->length;
2261 if (start < offset + len)
2263 sk_msg_iter_var_next(i);
2264 } while (i != msg->sg.end);
2266 if (unlikely(start >= offset + len))
2270 /* The start may point into the sg element so we need to also
2271 * account for the headroom.
2273 bytes_sg_total = start - offset + bytes;
2274 if (!test_bit(i, &msg->sg.copy) && bytes_sg_total <= len)
2277 /* At this point we need to linearize multiple scatterlist
2278 * elements or a single shared page. Either way we need to
2279 * copy into a linear buffer exclusively owned by BPF. Then
2280 * place the buffer in the scatterlist and fixup the original
2281 * entries by removing the entries now in the linear buffer
2282 * and shifting the remaining entries. For now we do not try
2283 * to copy partial entries to avoid complexity of running out
2284 * of sg_entry slots. The downside is reading a single byte
2285 * will copy the entire sg entry.
2288 copy += sk_msg_elem(msg, i)->length;
2289 sk_msg_iter_var_next(i);
2290 if (bytes_sg_total <= copy)
2292 } while (i != msg->sg.end);
2295 if (unlikely(bytes_sg_total > copy))
2298 page = alloc_pages(__GFP_NOWARN | GFP_ATOMIC | __GFP_COMP,
2300 if (unlikely(!page))
2303 raw = page_address(page);
2306 sge = sk_msg_elem(msg, i);
2307 from = sg_virt(sge);
2311 memcpy(to, from, len);
2314 put_page(sg_page(sge));
2316 sk_msg_iter_var_next(i);
2317 } while (i != last_sge);
2319 sg_set_page(&msg->sg.data[first_sge], page, copy, 0);
2321 /* To repair sg ring we need to shift entries. If we only
2322 * had a single entry though we can just replace it and
2323 * be done. Otherwise walk the ring and shift the entries.
2325 WARN_ON_ONCE(last_sge == first_sge);
2326 shift = last_sge > first_sge ?
2327 last_sge - first_sge - 1 :
2328 NR_MSG_FRAG_IDS - first_sge + last_sge - 1;
2333 sk_msg_iter_var_next(i);
2337 if (i + shift >= NR_MSG_FRAG_IDS)
2338 move_from = i + shift - NR_MSG_FRAG_IDS;
2340 move_from = i + shift;
2341 if (move_from == msg->sg.end)
2344 msg->sg.data[i] = msg->sg.data[move_from];
2345 msg->sg.data[move_from].length = 0;
2346 msg->sg.data[move_from].page_link = 0;
2347 msg->sg.data[move_from].offset = 0;
2348 sk_msg_iter_var_next(i);
2351 msg->sg.end = msg->sg.end - shift > msg->sg.end ?
2352 msg->sg.end - shift + NR_MSG_FRAG_IDS :
2353 msg->sg.end - shift;
2355 msg->data = sg_virt(&msg->sg.data[first_sge]) + start - offset;
2356 msg->data_end = msg->data + bytes;
2360 static const struct bpf_func_proto bpf_msg_pull_data_proto = {
2361 .func = bpf_msg_pull_data,
2363 .ret_type = RET_INTEGER,
2364 .arg1_type = ARG_PTR_TO_CTX,
2365 .arg2_type = ARG_ANYTHING,
2366 .arg3_type = ARG_ANYTHING,
2367 .arg4_type = ARG_ANYTHING,
2370 BPF_CALL_4(bpf_msg_push_data, struct sk_msg *, msg, u32, start,
2371 u32, len, u64, flags)
2373 struct scatterlist sge, nsge, nnsge, rsge = {0}, *psge;
2374 u32 new, i = 0, l = 0, space, copy = 0, offset = 0;
2375 u8 *raw, *to, *from;
2378 if (unlikely(flags))
2381 /* First find the starting scatterlist element */
2385 l = sk_msg_elem(msg, i)->length;
2387 if (start < offset + l)
2389 sk_msg_iter_var_next(i);
2390 } while (i != msg->sg.end);
2392 if (start >= offset + l)
2395 space = MAX_MSG_FRAGS - sk_msg_elem_used(msg);
2397 /* If no space available will fallback to copy, we need at
2398 * least one scatterlist elem available to push data into
2399 * when start aligns to the beginning of an element or two
2400 * when it falls inside an element. We handle the start equals
2401 * offset case because its the common case for inserting a
2404 if (!space || (space == 1 && start != offset))
2405 copy = msg->sg.data[i].length;
2407 page = alloc_pages(__GFP_NOWARN | GFP_ATOMIC | __GFP_COMP,
2408 get_order(copy + len));
2409 if (unlikely(!page))
2415 raw = page_address(page);
2417 psge = sk_msg_elem(msg, i);
2418 front = start - offset;
2419 back = psge->length - front;
2420 from = sg_virt(psge);
2423 memcpy(raw, from, front);
2427 to = raw + front + len;
2429 memcpy(to, from, back);
2432 put_page(sg_page(psge));
2433 } else if (start - offset) {
2434 psge = sk_msg_elem(msg, i);
2435 rsge = sk_msg_elem_cpy(msg, i);
2437 psge->length = start - offset;
2438 rsge.length -= psge->length;
2439 rsge.offset += start;
2441 sk_msg_iter_var_next(i);
2442 sg_unmark_end(psge);
2443 sg_unmark_end(&rsge);
2444 sk_msg_iter_next(msg, end);
2447 /* Slot(s) to place newly allocated data */
2450 /* Shift one or two slots as needed */
2452 sge = sk_msg_elem_cpy(msg, i);
2454 sk_msg_iter_var_next(i);
2455 sg_unmark_end(&sge);
2456 sk_msg_iter_next(msg, end);
2458 nsge = sk_msg_elem_cpy(msg, i);
2460 sk_msg_iter_var_next(i);
2461 nnsge = sk_msg_elem_cpy(msg, i);
2464 while (i != msg->sg.end) {
2465 msg->sg.data[i] = sge;
2467 sk_msg_iter_var_next(i);
2470 nnsge = sk_msg_elem_cpy(msg, i);
2472 nsge = sk_msg_elem_cpy(msg, i);
2477 /* Place newly allocated data buffer */
2478 sk_mem_charge(msg->sk, len);
2479 msg->sg.size += len;
2480 __clear_bit(new, &msg->sg.copy);
2481 sg_set_page(&msg->sg.data[new], page, len + copy, 0);
2483 get_page(sg_page(&rsge));
2484 sk_msg_iter_var_next(new);
2485 msg->sg.data[new] = rsge;
2488 sk_msg_compute_data_pointers(msg);
2492 static const struct bpf_func_proto bpf_msg_push_data_proto = {
2493 .func = bpf_msg_push_data,
2495 .ret_type = RET_INTEGER,
2496 .arg1_type = ARG_PTR_TO_CTX,
2497 .arg2_type = ARG_ANYTHING,
2498 .arg3_type = ARG_ANYTHING,
2499 .arg4_type = ARG_ANYTHING,
2502 static void sk_msg_shift_left(struct sk_msg *msg, int i)
2508 sk_msg_iter_var_next(i);
2509 msg->sg.data[prev] = msg->sg.data[i];
2510 } while (i != msg->sg.end);
2512 sk_msg_iter_prev(msg, end);
2515 static void sk_msg_shift_right(struct sk_msg *msg, int i)
2517 struct scatterlist tmp, sge;
2519 sk_msg_iter_next(msg, end);
2520 sge = sk_msg_elem_cpy(msg, i);
2521 sk_msg_iter_var_next(i);
2522 tmp = sk_msg_elem_cpy(msg, i);
2524 while (i != msg->sg.end) {
2525 msg->sg.data[i] = sge;
2526 sk_msg_iter_var_next(i);
2528 tmp = sk_msg_elem_cpy(msg, i);
2532 BPF_CALL_4(bpf_msg_pop_data, struct sk_msg *, msg, u32, start,
2533 u32, len, u64, flags)
2535 u32 i = 0, l = 0, space, offset = 0;
2536 u64 last = start + len;
2539 if (unlikely(flags))
2542 /* First find the starting scatterlist element */
2546 l = sk_msg_elem(msg, i)->length;
2548 if (start < offset + l)
2550 sk_msg_iter_var_next(i);
2551 } while (i != msg->sg.end);
2553 /* Bounds checks: start and pop must be inside message */
2554 if (start >= offset + l || last >= msg->sg.size)
2557 space = MAX_MSG_FRAGS - sk_msg_elem_used(msg);
2560 /* --------------| offset
2561 * -| start |-------- len -------|
2563 * |----- a ----|-------- pop -------|----- b ----|
2564 * |______________________________________________| length
2567 * a: region at front of scatter element to save
2568 * b: region at back of scatter element to save when length > A + pop
2569 * pop: region to pop from element, same as input 'pop' here will be
2570 * decremented below per iteration.
2572 * Two top-level cases to handle when start != offset, first B is non
2573 * zero and second B is zero corresponding to when a pop includes more
2576 * Then if B is non-zero AND there is no space allocate space and
2577 * compact A, B regions into page. If there is space shift ring to
2578 * the rigth free'ing the next element in ring to place B, leaving
2579 * A untouched except to reduce length.
2581 if (start != offset) {
2582 struct scatterlist *nsge, *sge = sk_msg_elem(msg, i);
2584 int b = sge->length - pop - a;
2586 sk_msg_iter_var_next(i);
2588 if (pop < sge->length - a) {
2591 sk_msg_shift_right(msg, i);
2592 nsge = sk_msg_elem(msg, i);
2593 get_page(sg_page(sge));
2596 b, sge->offset + pop + a);
2598 struct page *page, *orig;
2601 page = alloc_pages(__GFP_NOWARN |
2602 __GFP_COMP | GFP_ATOMIC,
2604 if (unlikely(!page))
2608 orig = sg_page(sge);
2609 from = sg_virt(sge);
2610 to = page_address(page);
2611 memcpy(to, from, a);
2612 memcpy(to + a, from + a + pop, b);
2613 sg_set_page(sge, page, a + b, 0);
2617 } else if (pop >= sge->length - a) {
2618 pop -= (sge->length - a);
2623 /* From above the current layout _must_ be as follows,
2628 * |---- pop ---|---------------- b ------------|
2629 * |____________________________________________| length
2631 * Offset and start of the current msg elem are equal because in the
2632 * previous case we handled offset != start and either consumed the
2633 * entire element and advanced to the next element OR pop == 0.
2635 * Two cases to handle here are first pop is less than the length
2636 * leaving some remainder b above. Simply adjust the element's layout
2637 * in this case. Or pop >= length of the element so that b = 0. In this
2638 * case advance to next element decrementing pop.
2641 struct scatterlist *sge = sk_msg_elem(msg, i);
2643 if (pop < sge->length) {
2649 sk_msg_shift_left(msg, i);
2651 sk_msg_iter_var_next(i);
2654 sk_mem_uncharge(msg->sk, len - pop);
2655 msg->sg.size -= (len - pop);
2656 sk_msg_compute_data_pointers(msg);
2660 static const struct bpf_func_proto bpf_msg_pop_data_proto = {
2661 .func = bpf_msg_pop_data,
2663 .ret_type = RET_INTEGER,
2664 .arg1_type = ARG_PTR_TO_CTX,
2665 .arg2_type = ARG_ANYTHING,
2666 .arg3_type = ARG_ANYTHING,
2667 .arg4_type = ARG_ANYTHING,
2670 #ifdef CONFIG_CGROUP_NET_CLASSID
2671 BPF_CALL_0(bpf_get_cgroup_classid_curr)
2673 return __task_get_classid(current);
2676 static const struct bpf_func_proto bpf_get_cgroup_classid_curr_proto = {
2677 .func = bpf_get_cgroup_classid_curr,
2679 .ret_type = RET_INTEGER,
2683 BPF_CALL_1(bpf_get_cgroup_classid, const struct sk_buff *, skb)
2685 return task_get_classid(skb);
2688 static const struct bpf_func_proto bpf_get_cgroup_classid_proto = {
2689 .func = bpf_get_cgroup_classid,
2691 .ret_type = RET_INTEGER,
2692 .arg1_type = ARG_PTR_TO_CTX,
2695 BPF_CALL_1(bpf_get_route_realm, const struct sk_buff *, skb)
2697 return dst_tclassid(skb);
2700 static const struct bpf_func_proto bpf_get_route_realm_proto = {
2701 .func = bpf_get_route_realm,
2703 .ret_type = RET_INTEGER,
2704 .arg1_type = ARG_PTR_TO_CTX,
2707 BPF_CALL_1(bpf_get_hash_recalc, struct sk_buff *, skb)
2709 /* If skb_clear_hash() was called due to mangling, we can
2710 * trigger SW recalculation here. Later access to hash
2711 * can then use the inline skb->hash via context directly
2712 * instead of calling this helper again.
2714 return skb_get_hash(skb);
2717 static const struct bpf_func_proto bpf_get_hash_recalc_proto = {
2718 .func = bpf_get_hash_recalc,
2720 .ret_type = RET_INTEGER,
2721 .arg1_type = ARG_PTR_TO_CTX,
2724 BPF_CALL_1(bpf_set_hash_invalid, struct sk_buff *, skb)
2726 /* After all direct packet write, this can be used once for
2727 * triggering a lazy recalc on next skb_get_hash() invocation.
2729 skb_clear_hash(skb);
2733 static const struct bpf_func_proto bpf_set_hash_invalid_proto = {
2734 .func = bpf_set_hash_invalid,
2736 .ret_type = RET_INTEGER,
2737 .arg1_type = ARG_PTR_TO_CTX,
2740 BPF_CALL_2(bpf_set_hash, struct sk_buff *, skb, u32, hash)
2742 /* Set user specified hash as L4(+), so that it gets returned
2743 * on skb_get_hash() call unless BPF prog later on triggers a
2746 __skb_set_sw_hash(skb, hash, true);
2750 static const struct bpf_func_proto bpf_set_hash_proto = {
2751 .func = bpf_set_hash,
2753 .ret_type = RET_INTEGER,
2754 .arg1_type = ARG_PTR_TO_CTX,
2755 .arg2_type = ARG_ANYTHING,
2758 BPF_CALL_3(bpf_skb_vlan_push, struct sk_buff *, skb, __be16, vlan_proto,
2763 if (unlikely(vlan_proto != htons(ETH_P_8021Q) &&
2764 vlan_proto != htons(ETH_P_8021AD)))
2765 vlan_proto = htons(ETH_P_8021Q);
2767 bpf_push_mac_rcsum(skb);
2768 ret = skb_vlan_push(skb, vlan_proto, vlan_tci);
2769 bpf_pull_mac_rcsum(skb);
2771 bpf_compute_data_pointers(skb);
2775 static const struct bpf_func_proto bpf_skb_vlan_push_proto = {
2776 .func = bpf_skb_vlan_push,
2778 .ret_type = RET_INTEGER,
2779 .arg1_type = ARG_PTR_TO_CTX,
2780 .arg2_type = ARG_ANYTHING,
2781 .arg3_type = ARG_ANYTHING,
2784 BPF_CALL_1(bpf_skb_vlan_pop, struct sk_buff *, skb)
2788 bpf_push_mac_rcsum(skb);
2789 ret = skb_vlan_pop(skb);
2790 bpf_pull_mac_rcsum(skb);
2792 bpf_compute_data_pointers(skb);
2796 static const struct bpf_func_proto bpf_skb_vlan_pop_proto = {
2797 .func = bpf_skb_vlan_pop,
2799 .ret_type = RET_INTEGER,
2800 .arg1_type = ARG_PTR_TO_CTX,
2803 static int bpf_skb_generic_push(struct sk_buff *skb, u32 off, u32 len)
2805 /* Caller already did skb_cow() with len as headroom,
2806 * so no need to do it here.
2809 memmove(skb->data, skb->data + len, off);
2810 memset(skb->data + off, 0, len);
2812 /* No skb_postpush_rcsum(skb, skb->data + off, len)
2813 * needed here as it does not change the skb->csum
2814 * result for checksum complete when summing over
2820 static int bpf_skb_generic_pop(struct sk_buff *skb, u32 off, u32 len)
2822 /* skb_ensure_writable() is not needed here, as we're
2823 * already working on an uncloned skb.
2825 if (unlikely(!pskb_may_pull(skb, off + len)))
2828 skb_postpull_rcsum(skb, skb->data + off, len);
2829 memmove(skb->data + len, skb->data, off);
2830 __skb_pull(skb, len);
2835 static int bpf_skb_net_hdr_push(struct sk_buff *skb, u32 off, u32 len)
2837 bool trans_same = skb->transport_header == skb->network_header;
2840 /* There's no need for __skb_push()/__skb_pull() pair to
2841 * get to the start of the mac header as we're guaranteed
2842 * to always start from here under eBPF.
2844 ret = bpf_skb_generic_push(skb, off, len);
2846 skb->mac_header -= len;
2847 skb->network_header -= len;
2849 skb->transport_header = skb->network_header;
2855 static int bpf_skb_net_hdr_pop(struct sk_buff *skb, u32 off, u32 len)
2857 bool trans_same = skb->transport_header == skb->network_header;
2860 /* Same here, __skb_push()/__skb_pull() pair not needed. */
2861 ret = bpf_skb_generic_pop(skb, off, len);
2863 skb->mac_header += len;
2864 skb->network_header += len;
2866 skb->transport_header = skb->network_header;
2872 static int bpf_skb_proto_4_to_6(struct sk_buff *skb)
2874 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
2875 u32 off = skb_mac_header_len(skb);
2878 if (skb_is_gso(skb) && !skb_is_gso_tcp(skb))
2881 ret = skb_cow(skb, len_diff);
2882 if (unlikely(ret < 0))
2885 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
2886 if (unlikely(ret < 0))
2889 if (skb_is_gso(skb)) {
2890 struct skb_shared_info *shinfo = skb_shinfo(skb);
2892 /* SKB_GSO_TCPV4 needs to be changed into
2895 if (shinfo->gso_type & SKB_GSO_TCPV4) {
2896 shinfo->gso_type &= ~SKB_GSO_TCPV4;
2897 shinfo->gso_type |= SKB_GSO_TCPV6;
2900 /* Due to IPv6 header, MSS needs to be downgraded. */
2901 skb_decrease_gso_size(shinfo, len_diff);
2902 /* Header must be checked, and gso_segs recomputed. */
2903 shinfo->gso_type |= SKB_GSO_DODGY;
2904 shinfo->gso_segs = 0;
2907 skb->protocol = htons(ETH_P_IPV6);
2908 skb_clear_hash(skb);
2913 static int bpf_skb_proto_6_to_4(struct sk_buff *skb)
2915 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
2916 u32 off = skb_mac_header_len(skb);
2919 if (skb_is_gso(skb) && !skb_is_gso_tcp(skb))
2922 ret = skb_unclone(skb, GFP_ATOMIC);
2923 if (unlikely(ret < 0))
2926 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
2927 if (unlikely(ret < 0))
2930 if (skb_is_gso(skb)) {
2931 struct skb_shared_info *shinfo = skb_shinfo(skb);
2933 /* SKB_GSO_TCPV6 needs to be changed into
2936 if (shinfo->gso_type & SKB_GSO_TCPV6) {
2937 shinfo->gso_type &= ~SKB_GSO_TCPV6;
2938 shinfo->gso_type |= SKB_GSO_TCPV4;
2941 /* Due to IPv4 header, MSS can be upgraded. */
2942 skb_increase_gso_size(shinfo, len_diff);
2943 /* Header must be checked, and gso_segs recomputed. */
2944 shinfo->gso_type |= SKB_GSO_DODGY;
2945 shinfo->gso_segs = 0;
2948 skb->protocol = htons(ETH_P_IP);
2949 skb_clear_hash(skb);
2954 static int bpf_skb_proto_xlat(struct sk_buff *skb, __be16 to_proto)
2956 __be16 from_proto = skb->protocol;
2958 if (from_proto == htons(ETH_P_IP) &&
2959 to_proto == htons(ETH_P_IPV6))
2960 return bpf_skb_proto_4_to_6(skb);
2962 if (from_proto == htons(ETH_P_IPV6) &&
2963 to_proto == htons(ETH_P_IP))
2964 return bpf_skb_proto_6_to_4(skb);
2969 BPF_CALL_3(bpf_skb_change_proto, struct sk_buff *, skb, __be16, proto,
2974 if (unlikely(flags))
2977 /* General idea is that this helper does the basic groundwork
2978 * needed for changing the protocol, and eBPF program fills the
2979 * rest through bpf_skb_store_bytes(), bpf_lX_csum_replace()
2980 * and other helpers, rather than passing a raw buffer here.
2982 * The rationale is to keep this minimal and without a need to
2983 * deal with raw packet data. F.e. even if we would pass buffers
2984 * here, the program still needs to call the bpf_lX_csum_replace()
2985 * helpers anyway. Plus, this way we keep also separation of
2986 * concerns, since f.e. bpf_skb_store_bytes() should only take
2989 * Currently, additional options and extension header space are
2990 * not supported, but flags register is reserved so we can adapt
2991 * that. For offloads, we mark packet as dodgy, so that headers
2992 * need to be verified first.
2994 ret = bpf_skb_proto_xlat(skb, proto);
2995 bpf_compute_data_pointers(skb);
2999 static const struct bpf_func_proto bpf_skb_change_proto_proto = {
3000 .func = bpf_skb_change_proto,
3002 .ret_type = RET_INTEGER,
3003 .arg1_type = ARG_PTR_TO_CTX,
3004 .arg2_type = ARG_ANYTHING,
3005 .arg3_type = ARG_ANYTHING,
3008 BPF_CALL_2(bpf_skb_change_type, struct sk_buff *, skb, u32, pkt_type)
3010 /* We only allow a restricted subset to be changed for now. */
3011 if (unlikely(!skb_pkt_type_ok(skb->pkt_type) ||
3012 !skb_pkt_type_ok(pkt_type)))
3015 skb->pkt_type = pkt_type;
3019 static const struct bpf_func_proto bpf_skb_change_type_proto = {
3020 .func = bpf_skb_change_type,
3022 .ret_type = RET_INTEGER,
3023 .arg1_type = ARG_PTR_TO_CTX,
3024 .arg2_type = ARG_ANYTHING,
3027 static u32 bpf_skb_net_base_len(const struct sk_buff *skb)
3029 switch (skb->protocol) {
3030 case htons(ETH_P_IP):
3031 return sizeof(struct iphdr);
3032 case htons(ETH_P_IPV6):
3033 return sizeof(struct ipv6hdr);
3039 #define BPF_F_ADJ_ROOM_ENCAP_L3_MASK (BPF_F_ADJ_ROOM_ENCAP_L3_IPV4 | \
3040 BPF_F_ADJ_ROOM_ENCAP_L3_IPV6)
3042 #define BPF_F_ADJ_ROOM_MASK (BPF_F_ADJ_ROOM_FIXED_GSO | \
3043 BPF_F_ADJ_ROOM_ENCAP_L3_MASK | \
3044 BPF_F_ADJ_ROOM_ENCAP_L4_GRE | \
3045 BPF_F_ADJ_ROOM_ENCAP_L4_UDP | \
3046 BPF_F_ADJ_ROOM_ENCAP_L2( \
3047 BPF_ADJ_ROOM_ENCAP_L2_MASK))
3049 static int bpf_skb_net_grow(struct sk_buff *skb, u32 off, u32 len_diff,
3052 u8 inner_mac_len = flags >> BPF_ADJ_ROOM_ENCAP_L2_SHIFT;
3053 bool encap = flags & BPF_F_ADJ_ROOM_ENCAP_L3_MASK;
3054 u16 mac_len = 0, inner_net = 0, inner_trans = 0;
3055 unsigned int gso_type = SKB_GSO_DODGY;
3058 if (skb_is_gso(skb) && !skb_is_gso_tcp(skb)) {
3059 /* udp gso_size delineates datagrams, only allow if fixed */
3060 if (!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) ||
3061 !(flags & BPF_F_ADJ_ROOM_FIXED_GSO))
3065 ret = skb_cow_head(skb, len_diff);
3066 if (unlikely(ret < 0))
3070 if (skb->protocol != htons(ETH_P_IP) &&
3071 skb->protocol != htons(ETH_P_IPV6))
3074 if (flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV4 &&
3075 flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV6)
3078 if (flags & BPF_F_ADJ_ROOM_ENCAP_L4_GRE &&
3079 flags & BPF_F_ADJ_ROOM_ENCAP_L4_UDP)
3082 if (skb->encapsulation)
3085 mac_len = skb->network_header - skb->mac_header;
3086 inner_net = skb->network_header;
3087 if (inner_mac_len > len_diff)
3089 inner_trans = skb->transport_header;
3092 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
3093 if (unlikely(ret < 0))
3097 skb->inner_mac_header = inner_net - inner_mac_len;
3098 skb->inner_network_header = inner_net;
3099 skb->inner_transport_header = inner_trans;
3100 skb_set_inner_protocol(skb, skb->protocol);
3102 skb->encapsulation = 1;
3103 skb_set_network_header(skb, mac_len);
3105 if (flags & BPF_F_ADJ_ROOM_ENCAP_L4_UDP)
3106 gso_type |= SKB_GSO_UDP_TUNNEL;
3107 else if (flags & BPF_F_ADJ_ROOM_ENCAP_L4_GRE)
3108 gso_type |= SKB_GSO_GRE;
3109 else if (flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV6)
3110 gso_type |= SKB_GSO_IPXIP6;
3111 else if (flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV4)
3112 gso_type |= SKB_GSO_IPXIP4;
3114 if (flags & BPF_F_ADJ_ROOM_ENCAP_L4_GRE ||
3115 flags & BPF_F_ADJ_ROOM_ENCAP_L4_UDP) {
3116 int nh_len = flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV6 ?
3117 sizeof(struct ipv6hdr) :
3118 sizeof(struct iphdr);
3120 skb_set_transport_header(skb, mac_len + nh_len);
3123 /* Match skb->protocol to new outer l3 protocol */
3124 if (skb->protocol == htons(ETH_P_IP) &&
3125 flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV6)
3126 skb->protocol = htons(ETH_P_IPV6);
3127 else if (skb->protocol == htons(ETH_P_IPV6) &&
3128 flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV4)
3129 skb->protocol = htons(ETH_P_IP);
3132 if (skb_is_gso(skb)) {
3133 struct skb_shared_info *shinfo = skb_shinfo(skb);
3135 /* Due to header grow, MSS needs to be downgraded. */
3136 if (!(flags & BPF_F_ADJ_ROOM_FIXED_GSO))
3137 skb_decrease_gso_size(shinfo, len_diff);
3139 /* Header must be checked, and gso_segs recomputed. */
3140 shinfo->gso_type |= gso_type;
3141 shinfo->gso_segs = 0;
3147 static int bpf_skb_net_shrink(struct sk_buff *skb, u32 off, u32 len_diff,
3152 if (unlikely(flags & ~(BPF_F_ADJ_ROOM_FIXED_GSO |
3153 BPF_F_ADJ_ROOM_NO_CSUM_RESET)))
3156 if (skb_is_gso(skb) && !skb_is_gso_tcp(skb)) {
3157 /* udp gso_size delineates datagrams, only allow if fixed */
3158 if (!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) ||
3159 !(flags & BPF_F_ADJ_ROOM_FIXED_GSO))
3163 ret = skb_unclone(skb, GFP_ATOMIC);
3164 if (unlikely(ret < 0))
3167 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
3168 if (unlikely(ret < 0))
3171 if (skb_is_gso(skb)) {
3172 struct skb_shared_info *shinfo = skb_shinfo(skb);
3174 /* Due to header shrink, MSS can be upgraded. */
3175 if (!(flags & BPF_F_ADJ_ROOM_FIXED_GSO))
3176 skb_increase_gso_size(shinfo, len_diff);
3178 /* Header must be checked, and gso_segs recomputed. */
3179 shinfo->gso_type |= SKB_GSO_DODGY;
3180 shinfo->gso_segs = 0;
3186 static u32 __bpf_skb_max_len(const struct sk_buff *skb)
3188 return skb->dev ? skb->dev->mtu + skb->dev->hard_header_len :
3192 BPF_CALL_4(bpf_skb_adjust_room, struct sk_buff *, skb, s32, len_diff,
3193 u32, mode, u64, flags)
3195 u32 len_cur, len_diff_abs = abs(len_diff);
3196 u32 len_min = bpf_skb_net_base_len(skb);
3197 u32 len_max = __bpf_skb_max_len(skb);
3198 __be16 proto = skb->protocol;
3199 bool shrink = len_diff < 0;
3203 if (unlikely(flags & ~(BPF_F_ADJ_ROOM_MASK |
3204 BPF_F_ADJ_ROOM_NO_CSUM_RESET)))
3206 if (unlikely(len_diff_abs > 0xfffU))
3208 if (unlikely(proto != htons(ETH_P_IP) &&
3209 proto != htons(ETH_P_IPV6)))
3212 off = skb_mac_header_len(skb);
3214 case BPF_ADJ_ROOM_NET:
3215 off += bpf_skb_net_base_len(skb);
3217 case BPF_ADJ_ROOM_MAC:
3223 len_cur = skb->len - skb_network_offset(skb);
3224 if ((shrink && (len_diff_abs >= len_cur ||
3225 len_cur - len_diff_abs < len_min)) ||
3226 (!shrink && (skb->len + len_diff_abs > len_max &&
3230 ret = shrink ? bpf_skb_net_shrink(skb, off, len_diff_abs, flags) :
3231 bpf_skb_net_grow(skb, off, len_diff_abs, flags);
3232 if (!ret && !(flags & BPF_F_ADJ_ROOM_NO_CSUM_RESET))
3233 __skb_reset_checksum_unnecessary(skb);
3235 bpf_compute_data_pointers(skb);
3239 static const struct bpf_func_proto bpf_skb_adjust_room_proto = {
3240 .func = bpf_skb_adjust_room,
3242 .ret_type = RET_INTEGER,
3243 .arg1_type = ARG_PTR_TO_CTX,
3244 .arg2_type = ARG_ANYTHING,
3245 .arg3_type = ARG_ANYTHING,
3246 .arg4_type = ARG_ANYTHING,
3249 static u32 __bpf_skb_min_len(const struct sk_buff *skb)
3251 u32 min_len = skb_network_offset(skb);
3253 if (skb_transport_header_was_set(skb))
3254 min_len = skb_transport_offset(skb);
3255 if (skb->ip_summed == CHECKSUM_PARTIAL)
3256 min_len = skb_checksum_start_offset(skb) +
3257 skb->csum_offset + sizeof(__sum16);
3261 static int bpf_skb_grow_rcsum(struct sk_buff *skb, unsigned int new_len)
3263 unsigned int old_len = skb->len;
3266 ret = __skb_grow_rcsum(skb, new_len);
3268 memset(skb->data + old_len, 0, new_len - old_len);
3272 static int bpf_skb_trim_rcsum(struct sk_buff *skb, unsigned int new_len)
3274 return __skb_trim_rcsum(skb, new_len);
3277 static inline int __bpf_skb_change_tail(struct sk_buff *skb, u32 new_len,
3280 u32 max_len = __bpf_skb_max_len(skb);
3281 u32 min_len = __bpf_skb_min_len(skb);
3284 if (unlikely(flags || new_len > max_len || new_len < min_len))
3286 if (skb->encapsulation)
3289 /* The basic idea of this helper is that it's performing the
3290 * needed work to either grow or trim an skb, and eBPF program
3291 * rewrites the rest via helpers like bpf_skb_store_bytes(),
3292 * bpf_lX_csum_replace() and others rather than passing a raw
3293 * buffer here. This one is a slow path helper and intended
3294 * for replies with control messages.
3296 * Like in bpf_skb_change_proto(), we want to keep this rather
3297 * minimal and without protocol specifics so that we are able
3298 * to separate concerns as in bpf_skb_store_bytes() should only
3299 * be the one responsible for writing buffers.
3301 * It's really expected to be a slow path operation here for
3302 * control message replies, so we're implicitly linearizing,
3303 * uncloning and drop offloads from the skb by this.
3305 ret = __bpf_try_make_writable(skb, skb->len);
3307 if (new_len > skb->len)
3308 ret = bpf_skb_grow_rcsum(skb, new_len);
3309 else if (new_len < skb->len)
3310 ret = bpf_skb_trim_rcsum(skb, new_len);
3311 if (!ret && skb_is_gso(skb))
3317 BPF_CALL_3(bpf_skb_change_tail, struct sk_buff *, skb, u32, new_len,
3320 int ret = __bpf_skb_change_tail(skb, new_len, flags);
3322 bpf_compute_data_pointers(skb);
3326 static const struct bpf_func_proto bpf_skb_change_tail_proto = {
3327 .func = bpf_skb_change_tail,
3329 .ret_type = RET_INTEGER,
3330 .arg1_type = ARG_PTR_TO_CTX,
3331 .arg2_type = ARG_ANYTHING,
3332 .arg3_type = ARG_ANYTHING,
3335 BPF_CALL_3(sk_skb_change_tail, struct sk_buff *, skb, u32, new_len,
3338 int ret = __bpf_skb_change_tail(skb, new_len, flags);
3340 bpf_compute_data_end_sk_skb(skb);
3344 static const struct bpf_func_proto sk_skb_change_tail_proto = {
3345 .func = sk_skb_change_tail,
3347 .ret_type = RET_INTEGER,
3348 .arg1_type = ARG_PTR_TO_CTX,
3349 .arg2_type = ARG_ANYTHING,
3350 .arg3_type = ARG_ANYTHING,
3353 static inline int __bpf_skb_change_head(struct sk_buff *skb, u32 head_room,
3356 u32 max_len = __bpf_skb_max_len(skb);
3357 u32 new_len = skb->len + head_room;
3360 if (unlikely(flags || (!skb_is_gso(skb) && new_len > max_len) ||
3361 new_len < skb->len))
3364 ret = skb_cow(skb, head_room);
3366 /* Idea for this helper is that we currently only
3367 * allow to expand on mac header. This means that
3368 * skb->protocol network header, etc, stay as is.
3369 * Compared to bpf_skb_change_tail(), we're more
3370 * flexible due to not needing to linearize or
3371 * reset GSO. Intention for this helper is to be
3372 * used by an L3 skb that needs to push mac header
3373 * for redirection into L2 device.
3375 __skb_push(skb, head_room);
3376 memset(skb->data, 0, head_room);
3377 skb_reset_mac_header(skb);
3383 BPF_CALL_3(bpf_skb_change_head, struct sk_buff *, skb, u32, head_room,
3386 int ret = __bpf_skb_change_head(skb, head_room, flags);
3388 bpf_compute_data_pointers(skb);
3392 static const struct bpf_func_proto bpf_skb_change_head_proto = {
3393 .func = bpf_skb_change_head,
3395 .ret_type = RET_INTEGER,
3396 .arg1_type = ARG_PTR_TO_CTX,
3397 .arg2_type = ARG_ANYTHING,
3398 .arg3_type = ARG_ANYTHING,
3401 BPF_CALL_3(sk_skb_change_head, struct sk_buff *, skb, u32, head_room,
3404 int ret = __bpf_skb_change_head(skb, head_room, flags);
3406 bpf_compute_data_end_sk_skb(skb);
3410 static const struct bpf_func_proto sk_skb_change_head_proto = {
3411 .func = sk_skb_change_head,
3413 .ret_type = RET_INTEGER,
3414 .arg1_type = ARG_PTR_TO_CTX,
3415 .arg2_type = ARG_ANYTHING,
3416 .arg3_type = ARG_ANYTHING,
3418 static unsigned long xdp_get_metalen(const struct xdp_buff *xdp)
3420 return xdp_data_meta_unsupported(xdp) ? 0 :
3421 xdp->data - xdp->data_meta;
3424 BPF_CALL_2(bpf_xdp_adjust_head, struct xdp_buff *, xdp, int, offset)
3426 void *xdp_frame_end = xdp->data_hard_start + sizeof(struct xdp_frame);
3427 unsigned long metalen = xdp_get_metalen(xdp);
3428 void *data_start = xdp_frame_end + metalen;
3429 void *data = xdp->data + offset;
3431 if (unlikely(data < data_start ||
3432 data > xdp->data_end - ETH_HLEN))
3436 memmove(xdp->data_meta + offset,
3437 xdp->data_meta, metalen);
3438 xdp->data_meta += offset;
3444 static const struct bpf_func_proto bpf_xdp_adjust_head_proto = {
3445 .func = bpf_xdp_adjust_head,
3447 .ret_type = RET_INTEGER,
3448 .arg1_type = ARG_PTR_TO_CTX,
3449 .arg2_type = ARG_ANYTHING,
3452 BPF_CALL_2(bpf_xdp_adjust_tail, struct xdp_buff *, xdp, int, offset)
3454 void *data_hard_end = xdp_data_hard_end(xdp); /* use xdp->frame_sz */
3455 void *data_end = xdp->data_end + offset;
3457 /* Notice that xdp_data_hard_end have reserved some tailroom */
3458 if (unlikely(data_end > data_hard_end))
3461 /* ALL drivers MUST init xdp->frame_sz, chicken check below */
3462 if (unlikely(xdp->frame_sz > PAGE_SIZE)) {
3463 WARN_ONCE(1, "Too BIG xdp->frame_sz = %d\n", xdp->frame_sz);
3467 if (unlikely(data_end < xdp->data + ETH_HLEN))
3470 /* Clear memory area on grow, can contain uninit kernel memory */
3472 memset(xdp->data_end, 0, offset);
3474 xdp->data_end = data_end;
3479 static const struct bpf_func_proto bpf_xdp_adjust_tail_proto = {
3480 .func = bpf_xdp_adjust_tail,
3482 .ret_type = RET_INTEGER,
3483 .arg1_type = ARG_PTR_TO_CTX,
3484 .arg2_type = ARG_ANYTHING,
3487 BPF_CALL_2(bpf_xdp_adjust_meta, struct xdp_buff *, xdp, int, offset)
3489 void *xdp_frame_end = xdp->data_hard_start + sizeof(struct xdp_frame);
3490 void *meta = xdp->data_meta + offset;
3491 unsigned long metalen = xdp->data - meta;
3493 if (xdp_data_meta_unsupported(xdp))
3495 if (unlikely(meta < xdp_frame_end ||
3498 if (unlikely((metalen & (sizeof(__u32) - 1)) ||
3502 xdp->data_meta = meta;
3507 static const struct bpf_func_proto bpf_xdp_adjust_meta_proto = {
3508 .func = bpf_xdp_adjust_meta,
3510 .ret_type = RET_INTEGER,
3511 .arg1_type = ARG_PTR_TO_CTX,
3512 .arg2_type = ARG_ANYTHING,
3515 static int __bpf_tx_xdp_map(struct net_device *dev_rx, void *fwd,
3516 struct bpf_map *map, struct xdp_buff *xdp)
3518 switch (map->map_type) {
3519 case BPF_MAP_TYPE_DEVMAP:
3520 case BPF_MAP_TYPE_DEVMAP_HASH:
3521 return dev_map_enqueue(fwd, xdp, dev_rx);
3522 case BPF_MAP_TYPE_CPUMAP:
3523 return cpu_map_enqueue(fwd, xdp, dev_rx);
3524 case BPF_MAP_TYPE_XSKMAP:
3525 return __xsk_map_redirect(fwd, xdp);
3532 void xdp_do_flush(void)
3538 EXPORT_SYMBOL_GPL(xdp_do_flush);
3540 static inline void *__xdp_map_lookup_elem(struct bpf_map *map, u32 index)
3542 switch (map->map_type) {
3543 case BPF_MAP_TYPE_DEVMAP:
3544 return __dev_map_lookup_elem(map, index);
3545 case BPF_MAP_TYPE_DEVMAP_HASH:
3546 return __dev_map_hash_lookup_elem(map, index);
3547 case BPF_MAP_TYPE_CPUMAP:
3548 return __cpu_map_lookup_elem(map, index);
3549 case BPF_MAP_TYPE_XSKMAP:
3550 return __xsk_map_lookup_elem(map, index);
3556 void bpf_clear_redirect_map(struct bpf_map *map)
3558 struct bpf_redirect_info *ri;
3561 for_each_possible_cpu(cpu) {
3562 ri = per_cpu_ptr(&bpf_redirect_info, cpu);
3563 /* Avoid polluting remote cacheline due to writes if
3564 * not needed. Once we pass this test, we need the
3565 * cmpxchg() to make sure it hasn't been changed in
3566 * the meantime by remote CPU.
3568 if (unlikely(READ_ONCE(ri->map) == map))
3569 cmpxchg(&ri->map, map, NULL);
3573 int xdp_do_redirect(struct net_device *dev, struct xdp_buff *xdp,
3574 struct bpf_prog *xdp_prog)
3576 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
3577 struct bpf_map *map = READ_ONCE(ri->map);
3578 u32 index = ri->tgt_index;
3579 void *fwd = ri->tgt_value;
3583 ri->tgt_value = NULL;
3584 WRITE_ONCE(ri->map, NULL);
3586 if (unlikely(!map)) {
3587 fwd = dev_get_by_index_rcu(dev_net(dev), index);
3588 if (unlikely(!fwd)) {
3593 err = dev_xdp_enqueue(fwd, xdp, dev);
3595 err = __bpf_tx_xdp_map(dev, fwd, map, xdp);
3601 _trace_xdp_redirect_map(dev, xdp_prog, fwd, map, index);
3604 _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map, index, err);
3607 EXPORT_SYMBOL_GPL(xdp_do_redirect);
3609 static int xdp_do_generic_redirect_map(struct net_device *dev,
3610 struct sk_buff *skb,
3611 struct xdp_buff *xdp,
3612 struct bpf_prog *xdp_prog,
3613 struct bpf_map *map)
3615 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
3616 u32 index = ri->tgt_index;
3617 void *fwd = ri->tgt_value;
3621 ri->tgt_value = NULL;
3622 WRITE_ONCE(ri->map, NULL);
3624 if (map->map_type == BPF_MAP_TYPE_DEVMAP ||
3625 map->map_type == BPF_MAP_TYPE_DEVMAP_HASH) {
3626 struct bpf_dtab_netdev *dst = fwd;
3628 err = dev_map_generic_redirect(dst, skb, xdp_prog);
3631 } else if (map->map_type == BPF_MAP_TYPE_XSKMAP) {
3632 struct xdp_sock *xs = fwd;
3634 err = xsk_generic_rcv(xs, xdp);
3639 /* TODO: Handle BPF_MAP_TYPE_CPUMAP */
3644 _trace_xdp_redirect_map(dev, xdp_prog, fwd, map, index);
3647 _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map, index, err);
3651 int xdp_do_generic_redirect(struct net_device *dev, struct sk_buff *skb,
3652 struct xdp_buff *xdp, struct bpf_prog *xdp_prog)
3654 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
3655 struct bpf_map *map = READ_ONCE(ri->map);
3656 u32 index = ri->tgt_index;
3657 struct net_device *fwd;
3661 return xdp_do_generic_redirect_map(dev, skb, xdp, xdp_prog,
3664 fwd = dev_get_by_index_rcu(dev_net(dev), index);
3665 if (unlikely(!fwd)) {
3670 err = xdp_ok_fwd_dev(fwd, skb->len);
3675 _trace_xdp_redirect(dev, xdp_prog, index);
3676 generic_xdp_tx(skb, xdp_prog);
3679 _trace_xdp_redirect_err(dev, xdp_prog, index, err);
3683 BPF_CALL_2(bpf_xdp_redirect, u32, ifindex, u64, flags)
3685 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
3687 if (unlikely(flags))
3691 ri->tgt_index = ifindex;
3692 ri->tgt_value = NULL;
3693 WRITE_ONCE(ri->map, NULL);
3695 return XDP_REDIRECT;
3698 static const struct bpf_func_proto bpf_xdp_redirect_proto = {
3699 .func = bpf_xdp_redirect,
3701 .ret_type = RET_INTEGER,
3702 .arg1_type = ARG_ANYTHING,
3703 .arg2_type = ARG_ANYTHING,
3706 BPF_CALL_3(bpf_xdp_redirect_map, struct bpf_map *, map, u32, ifindex,
3709 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
3711 /* Lower bits of the flags are used as return code on lookup failure */
3712 if (unlikely(flags > XDP_TX))
3715 ri->tgt_value = __xdp_map_lookup_elem(map, ifindex);
3716 if (unlikely(!ri->tgt_value)) {
3717 /* If the lookup fails we want to clear out the state in the
3718 * redirect_info struct completely, so that if an eBPF program
3719 * performs multiple lookups, the last one always takes
3722 WRITE_ONCE(ri->map, NULL);
3727 ri->tgt_index = ifindex;
3728 WRITE_ONCE(ri->map, map);
3730 return XDP_REDIRECT;
3733 static const struct bpf_func_proto bpf_xdp_redirect_map_proto = {
3734 .func = bpf_xdp_redirect_map,
3736 .ret_type = RET_INTEGER,
3737 .arg1_type = ARG_CONST_MAP_PTR,
3738 .arg2_type = ARG_ANYTHING,
3739 .arg3_type = ARG_ANYTHING,
3742 static unsigned long bpf_skb_copy(void *dst_buff, const void *skb,
3743 unsigned long off, unsigned long len)
3745 void *ptr = skb_header_pointer(skb, off, len, dst_buff);
3749 if (ptr != dst_buff)
3750 memcpy(dst_buff, ptr, len);
3755 BPF_CALL_5(bpf_skb_event_output, struct sk_buff *, skb, struct bpf_map *, map,
3756 u64, flags, void *, meta, u64, meta_size)
3758 u64 skb_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
3760 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
3762 if (unlikely(!skb || skb_size > skb->len))
3765 return bpf_event_output(map, flags, meta, meta_size, skb, skb_size,
3769 static const struct bpf_func_proto bpf_skb_event_output_proto = {
3770 .func = bpf_skb_event_output,
3772 .ret_type = RET_INTEGER,
3773 .arg1_type = ARG_PTR_TO_CTX,
3774 .arg2_type = ARG_CONST_MAP_PTR,
3775 .arg3_type = ARG_ANYTHING,
3776 .arg4_type = ARG_PTR_TO_MEM,
3777 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
3780 static int bpf_skb_output_btf_ids[5];
3781 const struct bpf_func_proto bpf_skb_output_proto = {
3782 .func = bpf_skb_event_output,
3784 .ret_type = RET_INTEGER,
3785 .arg1_type = ARG_PTR_TO_BTF_ID,
3786 .arg2_type = ARG_CONST_MAP_PTR,
3787 .arg3_type = ARG_ANYTHING,
3788 .arg4_type = ARG_PTR_TO_MEM,
3789 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
3790 .btf_id = bpf_skb_output_btf_ids,
3793 static unsigned short bpf_tunnel_key_af(u64 flags)
3795 return flags & BPF_F_TUNINFO_IPV6 ? AF_INET6 : AF_INET;
3798 BPF_CALL_4(bpf_skb_get_tunnel_key, struct sk_buff *, skb, struct bpf_tunnel_key *, to,
3799 u32, size, u64, flags)
3801 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
3802 u8 compat[sizeof(struct bpf_tunnel_key)];
3806 if (unlikely(!info || (flags & ~(BPF_F_TUNINFO_IPV6)))) {
3810 if (ip_tunnel_info_af(info) != bpf_tunnel_key_af(flags)) {
3814 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
3817 case offsetof(struct bpf_tunnel_key, tunnel_label):
3818 case offsetof(struct bpf_tunnel_key, tunnel_ext):
3820 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
3821 /* Fixup deprecated structure layouts here, so we have
3822 * a common path later on.
3824 if (ip_tunnel_info_af(info) != AF_INET)
3827 to = (struct bpf_tunnel_key *)compat;
3834 to->tunnel_id = be64_to_cpu(info->key.tun_id);
3835 to->tunnel_tos = info->key.tos;
3836 to->tunnel_ttl = info->key.ttl;
3839 if (flags & BPF_F_TUNINFO_IPV6) {
3840 memcpy(to->remote_ipv6, &info->key.u.ipv6.src,
3841 sizeof(to->remote_ipv6));
3842 to->tunnel_label = be32_to_cpu(info->key.label);
3844 to->remote_ipv4 = be32_to_cpu(info->key.u.ipv4.src);
3845 memset(&to->remote_ipv6[1], 0, sizeof(__u32) * 3);
3846 to->tunnel_label = 0;
3849 if (unlikely(size != sizeof(struct bpf_tunnel_key)))
3850 memcpy(to_orig, to, size);
3854 memset(to_orig, 0, size);
3858 static const struct bpf_func_proto bpf_skb_get_tunnel_key_proto = {
3859 .func = bpf_skb_get_tunnel_key,
3861 .ret_type = RET_INTEGER,
3862 .arg1_type = ARG_PTR_TO_CTX,
3863 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
3864 .arg3_type = ARG_CONST_SIZE,
3865 .arg4_type = ARG_ANYTHING,
3868 BPF_CALL_3(bpf_skb_get_tunnel_opt, struct sk_buff *, skb, u8 *, to, u32, size)
3870 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
3873 if (unlikely(!info ||
3874 !(info->key.tun_flags & TUNNEL_OPTIONS_PRESENT))) {
3878 if (unlikely(size < info->options_len)) {
3883 ip_tunnel_info_opts_get(to, info);
3884 if (size > info->options_len)
3885 memset(to + info->options_len, 0, size - info->options_len);
3887 return info->options_len;
3889 memset(to, 0, size);
3893 static const struct bpf_func_proto bpf_skb_get_tunnel_opt_proto = {
3894 .func = bpf_skb_get_tunnel_opt,
3896 .ret_type = RET_INTEGER,
3897 .arg1_type = ARG_PTR_TO_CTX,
3898 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
3899 .arg3_type = ARG_CONST_SIZE,
3902 static struct metadata_dst __percpu *md_dst;
3904 BPF_CALL_4(bpf_skb_set_tunnel_key, struct sk_buff *, skb,
3905 const struct bpf_tunnel_key *, from, u32, size, u64, flags)
3907 struct metadata_dst *md = this_cpu_ptr(md_dst);
3908 u8 compat[sizeof(struct bpf_tunnel_key)];
3909 struct ip_tunnel_info *info;
3911 if (unlikely(flags & ~(BPF_F_TUNINFO_IPV6 | BPF_F_ZERO_CSUM_TX |
3912 BPF_F_DONT_FRAGMENT | BPF_F_SEQ_NUMBER)))
3914 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
3916 case offsetof(struct bpf_tunnel_key, tunnel_label):
3917 case offsetof(struct bpf_tunnel_key, tunnel_ext):
3918 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
3919 /* Fixup deprecated structure layouts here, so we have
3920 * a common path later on.
3922 memcpy(compat, from, size);
3923 memset(compat + size, 0, sizeof(compat) - size);
3924 from = (const struct bpf_tunnel_key *) compat;
3930 if (unlikely((!(flags & BPF_F_TUNINFO_IPV6) && from->tunnel_label) ||
3935 dst_hold((struct dst_entry *) md);
3936 skb_dst_set(skb, (struct dst_entry *) md);
3938 info = &md->u.tun_info;
3939 memset(info, 0, sizeof(*info));
3940 info->mode = IP_TUNNEL_INFO_TX;
3942 info->key.tun_flags = TUNNEL_KEY | TUNNEL_CSUM | TUNNEL_NOCACHE;
3943 if (flags & BPF_F_DONT_FRAGMENT)
3944 info->key.tun_flags |= TUNNEL_DONT_FRAGMENT;
3945 if (flags & BPF_F_ZERO_CSUM_TX)
3946 info->key.tun_flags &= ~TUNNEL_CSUM;
3947 if (flags & BPF_F_SEQ_NUMBER)
3948 info->key.tun_flags |= TUNNEL_SEQ;
3950 info->key.tun_id = cpu_to_be64(from->tunnel_id);
3951 info->key.tos = from->tunnel_tos;
3952 info->key.ttl = from->tunnel_ttl;
3954 if (flags & BPF_F_TUNINFO_IPV6) {
3955 info->mode |= IP_TUNNEL_INFO_IPV6;
3956 memcpy(&info->key.u.ipv6.dst, from->remote_ipv6,
3957 sizeof(from->remote_ipv6));
3958 info->key.label = cpu_to_be32(from->tunnel_label) &
3959 IPV6_FLOWLABEL_MASK;
3961 info->key.u.ipv4.dst = cpu_to_be32(from->remote_ipv4);
3967 static const struct bpf_func_proto bpf_skb_set_tunnel_key_proto = {
3968 .func = bpf_skb_set_tunnel_key,
3970 .ret_type = RET_INTEGER,
3971 .arg1_type = ARG_PTR_TO_CTX,
3972 .arg2_type = ARG_PTR_TO_MEM,
3973 .arg3_type = ARG_CONST_SIZE,
3974 .arg4_type = ARG_ANYTHING,
3977 BPF_CALL_3(bpf_skb_set_tunnel_opt, struct sk_buff *, skb,
3978 const u8 *, from, u32, size)
3980 struct ip_tunnel_info *info = skb_tunnel_info(skb);
3981 const struct metadata_dst *md = this_cpu_ptr(md_dst);
3983 if (unlikely(info != &md->u.tun_info || (size & (sizeof(u32) - 1))))
3985 if (unlikely(size > IP_TUNNEL_OPTS_MAX))
3988 ip_tunnel_info_opts_set(info, from, size, TUNNEL_OPTIONS_PRESENT);
3993 static const struct bpf_func_proto bpf_skb_set_tunnel_opt_proto = {
3994 .func = bpf_skb_set_tunnel_opt,
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,
4002 static const struct bpf_func_proto *
4003 bpf_get_skb_set_tunnel_proto(enum bpf_func_id which)
4006 struct metadata_dst __percpu *tmp;
4008 tmp = metadata_dst_alloc_percpu(IP_TUNNEL_OPTS_MAX,
4013 if (cmpxchg(&md_dst, NULL, tmp))
4014 metadata_dst_free_percpu(tmp);
4018 case BPF_FUNC_skb_set_tunnel_key:
4019 return &bpf_skb_set_tunnel_key_proto;
4020 case BPF_FUNC_skb_set_tunnel_opt:
4021 return &bpf_skb_set_tunnel_opt_proto;
4027 BPF_CALL_3(bpf_skb_under_cgroup, struct sk_buff *, skb, struct bpf_map *, map,
4030 struct bpf_array *array = container_of(map, struct bpf_array, map);
4031 struct cgroup *cgrp;
4034 sk = skb_to_full_sk(skb);
4035 if (!sk || !sk_fullsock(sk))
4037 if (unlikely(idx >= array->map.max_entries))
4040 cgrp = READ_ONCE(array->ptrs[idx]);
4041 if (unlikely(!cgrp))
4044 return sk_under_cgroup_hierarchy(sk, cgrp);
4047 static const struct bpf_func_proto bpf_skb_under_cgroup_proto = {
4048 .func = bpf_skb_under_cgroup,
4050 .ret_type = RET_INTEGER,
4051 .arg1_type = ARG_PTR_TO_CTX,
4052 .arg2_type = ARG_CONST_MAP_PTR,
4053 .arg3_type = ARG_ANYTHING,
4056 #ifdef CONFIG_SOCK_CGROUP_DATA
4057 static inline u64 __bpf_sk_cgroup_id(struct sock *sk)
4059 struct cgroup *cgrp;
4061 cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
4062 return cgroup_id(cgrp);
4065 BPF_CALL_1(bpf_skb_cgroup_id, const struct sk_buff *, skb)
4067 struct sock *sk = skb_to_full_sk(skb);
4069 if (!sk || !sk_fullsock(sk))
4072 return __bpf_sk_cgroup_id(sk);
4075 static const struct bpf_func_proto bpf_skb_cgroup_id_proto = {
4076 .func = bpf_skb_cgroup_id,
4078 .ret_type = RET_INTEGER,
4079 .arg1_type = ARG_PTR_TO_CTX,
4082 static inline u64 __bpf_sk_ancestor_cgroup_id(struct sock *sk,
4085 struct cgroup *ancestor;
4086 struct cgroup *cgrp;
4088 cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
4089 ancestor = cgroup_ancestor(cgrp, ancestor_level);
4093 return cgroup_id(ancestor);
4096 BPF_CALL_2(bpf_skb_ancestor_cgroup_id, const struct sk_buff *, skb, int,
4099 struct sock *sk = skb_to_full_sk(skb);
4101 if (!sk || !sk_fullsock(sk))
4104 return __bpf_sk_ancestor_cgroup_id(sk, ancestor_level);
4107 static const struct bpf_func_proto bpf_skb_ancestor_cgroup_id_proto = {
4108 .func = bpf_skb_ancestor_cgroup_id,
4110 .ret_type = RET_INTEGER,
4111 .arg1_type = ARG_PTR_TO_CTX,
4112 .arg2_type = ARG_ANYTHING,
4115 BPF_CALL_1(bpf_sk_cgroup_id, struct sock *, sk)
4117 return __bpf_sk_cgroup_id(sk);
4120 static const struct bpf_func_proto bpf_sk_cgroup_id_proto = {
4121 .func = bpf_sk_cgroup_id,
4123 .ret_type = RET_INTEGER,
4124 .arg1_type = ARG_PTR_TO_SOCKET,
4127 BPF_CALL_2(bpf_sk_ancestor_cgroup_id, struct sock *, sk, int, ancestor_level)
4129 return __bpf_sk_ancestor_cgroup_id(sk, ancestor_level);
4132 static const struct bpf_func_proto bpf_sk_ancestor_cgroup_id_proto = {
4133 .func = bpf_sk_ancestor_cgroup_id,
4135 .ret_type = RET_INTEGER,
4136 .arg1_type = ARG_PTR_TO_SOCKET,
4137 .arg2_type = ARG_ANYTHING,
4141 static unsigned long bpf_xdp_copy(void *dst_buff, const void *src_buff,
4142 unsigned long off, unsigned long len)
4144 memcpy(dst_buff, src_buff + off, len);
4148 BPF_CALL_5(bpf_xdp_event_output, struct xdp_buff *, xdp, struct bpf_map *, map,
4149 u64, flags, void *, meta, u64, meta_size)
4151 u64 xdp_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
4153 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
4155 if (unlikely(!xdp ||
4156 xdp_size > (unsigned long)(xdp->data_end - xdp->data)))
4159 return bpf_event_output(map, flags, meta, meta_size, xdp->data,
4160 xdp_size, bpf_xdp_copy);
4163 static const struct bpf_func_proto bpf_xdp_event_output_proto = {
4164 .func = bpf_xdp_event_output,
4166 .ret_type = RET_INTEGER,
4167 .arg1_type = ARG_PTR_TO_CTX,
4168 .arg2_type = ARG_CONST_MAP_PTR,
4169 .arg3_type = ARG_ANYTHING,
4170 .arg4_type = ARG_PTR_TO_MEM,
4171 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
4174 static int bpf_xdp_output_btf_ids[5];
4175 const struct bpf_func_proto bpf_xdp_output_proto = {
4176 .func = bpf_xdp_event_output,
4178 .ret_type = RET_INTEGER,
4179 .arg1_type = ARG_PTR_TO_BTF_ID,
4180 .arg2_type = ARG_CONST_MAP_PTR,
4181 .arg3_type = ARG_ANYTHING,
4182 .arg4_type = ARG_PTR_TO_MEM,
4183 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
4184 .btf_id = bpf_xdp_output_btf_ids,
4187 BPF_CALL_1(bpf_get_socket_cookie, struct sk_buff *, skb)
4189 return skb->sk ? sock_gen_cookie(skb->sk) : 0;
4192 static const struct bpf_func_proto bpf_get_socket_cookie_proto = {
4193 .func = bpf_get_socket_cookie,
4195 .ret_type = RET_INTEGER,
4196 .arg1_type = ARG_PTR_TO_CTX,
4199 BPF_CALL_1(bpf_get_socket_cookie_sock_addr, struct bpf_sock_addr_kern *, ctx)
4201 return sock_gen_cookie(ctx->sk);
4204 static const struct bpf_func_proto bpf_get_socket_cookie_sock_addr_proto = {
4205 .func = bpf_get_socket_cookie_sock_addr,
4207 .ret_type = RET_INTEGER,
4208 .arg1_type = ARG_PTR_TO_CTX,
4211 BPF_CALL_1(bpf_get_socket_cookie_sock, struct sock *, ctx)
4213 return sock_gen_cookie(ctx);
4216 static const struct bpf_func_proto bpf_get_socket_cookie_sock_proto = {
4217 .func = bpf_get_socket_cookie_sock,
4219 .ret_type = RET_INTEGER,
4220 .arg1_type = ARG_PTR_TO_CTX,
4223 BPF_CALL_1(bpf_get_socket_cookie_sock_ops, struct bpf_sock_ops_kern *, ctx)
4225 return sock_gen_cookie(ctx->sk);
4228 static const struct bpf_func_proto bpf_get_socket_cookie_sock_ops_proto = {
4229 .func = bpf_get_socket_cookie_sock_ops,
4231 .ret_type = RET_INTEGER,
4232 .arg1_type = ARG_PTR_TO_CTX,
4235 static u64 __bpf_get_netns_cookie(struct sock *sk)
4237 #ifdef CONFIG_NET_NS
4238 return net_gen_cookie(sk ? sk->sk_net.net : &init_net);
4244 BPF_CALL_1(bpf_get_netns_cookie_sock, struct sock *, ctx)
4246 return __bpf_get_netns_cookie(ctx);
4249 static const struct bpf_func_proto bpf_get_netns_cookie_sock_proto = {
4250 .func = bpf_get_netns_cookie_sock,
4252 .ret_type = RET_INTEGER,
4253 .arg1_type = ARG_PTR_TO_CTX_OR_NULL,
4256 BPF_CALL_1(bpf_get_netns_cookie_sock_addr, struct bpf_sock_addr_kern *, ctx)
4258 return __bpf_get_netns_cookie(ctx ? ctx->sk : NULL);
4261 static const struct bpf_func_proto bpf_get_netns_cookie_sock_addr_proto = {
4262 .func = bpf_get_netns_cookie_sock_addr,
4264 .ret_type = RET_INTEGER,
4265 .arg1_type = ARG_PTR_TO_CTX_OR_NULL,
4268 BPF_CALL_1(bpf_get_socket_uid, struct sk_buff *, skb)
4270 struct sock *sk = sk_to_full_sk(skb->sk);
4273 if (!sk || !sk_fullsock(sk))
4275 kuid = sock_net_uid(sock_net(sk), sk);
4276 return from_kuid_munged(sock_net(sk)->user_ns, kuid);
4279 static const struct bpf_func_proto bpf_get_socket_uid_proto = {
4280 .func = bpf_get_socket_uid,
4282 .ret_type = RET_INTEGER,
4283 .arg1_type = ARG_PTR_TO_CTX,
4286 #define SOCKOPT_CC_REINIT (1 << 0)
4288 static int _bpf_setsockopt(struct sock *sk, int level, int optname,
4289 char *optval, int optlen, u32 flags)
4291 char devname[IFNAMSIZ];
4297 if (!sk_fullsock(sk))
4300 sock_owned_by_me(sk);
4302 if (level == SOL_SOCKET) {
4303 if (optlen != sizeof(int) && optname != SO_BINDTODEVICE)
4305 val = *((int *)optval);
4307 /* Only some socketops are supported */
4310 val = min_t(u32, val, sysctl_rmem_max);
4311 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
4312 WRITE_ONCE(sk->sk_rcvbuf,
4313 max_t(int, val * 2, SOCK_MIN_RCVBUF));
4316 val = min_t(u32, val, sysctl_wmem_max);
4317 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
4318 WRITE_ONCE(sk->sk_sndbuf,
4319 max_t(int, val * 2, SOCK_MIN_SNDBUF));
4321 case SO_MAX_PACING_RATE: /* 32bit version */
4323 cmpxchg(&sk->sk_pacing_status,
4326 sk->sk_max_pacing_rate = (val == ~0U) ? ~0UL : val;
4327 sk->sk_pacing_rate = min(sk->sk_pacing_rate,
4328 sk->sk_max_pacing_rate);
4331 sk->sk_priority = val;
4336 WRITE_ONCE(sk->sk_rcvlowat, val ? : 1);
4339 if (sk->sk_mark != val) {
4344 case SO_BINDTODEVICE:
4345 optlen = min_t(long, optlen, IFNAMSIZ - 1);
4346 strncpy(devname, optval, optlen);
4347 devname[optlen] = 0;
4350 if (devname[0] != '\0') {
4351 struct net_device *dev;
4356 dev = dev_get_by_name(net, devname);
4359 ifindex = dev->ifindex;
4362 ret = sock_bindtoindex(sk, ifindex, false);
4368 } else if (level == SOL_IP) {
4369 if (optlen != sizeof(int) || sk->sk_family != AF_INET)
4372 val = *((int *)optval);
4373 /* Only some options are supported */
4376 if (val < -1 || val > 0xff) {
4379 struct inet_sock *inet = inet_sk(sk);
4389 #if IS_ENABLED(CONFIG_IPV6)
4390 } else if (level == SOL_IPV6) {
4391 if (optlen != sizeof(int) || sk->sk_family != AF_INET6)
4394 val = *((int *)optval);
4395 /* Only some options are supported */
4398 if (val < -1 || val > 0xff) {
4401 struct ipv6_pinfo *np = inet6_sk(sk);
4412 } else if (level == SOL_TCP &&
4413 sk->sk_prot->setsockopt == tcp_setsockopt) {
4414 if (optname == TCP_CONGESTION) {
4415 char name[TCP_CA_NAME_MAX];
4416 bool reinit = flags & SOCKOPT_CC_REINIT;
4418 strncpy(name, optval, min_t(long, optlen,
4419 TCP_CA_NAME_MAX-1));
4420 name[TCP_CA_NAME_MAX-1] = 0;
4421 ret = tcp_set_congestion_control(sk, name, false,
4424 struct tcp_sock *tp = tcp_sk(sk);
4426 if (optlen != sizeof(int))
4429 val = *((int *)optval);
4430 /* Only some options are supported */
4433 if (val <= 0 || tp->data_segs_out > tp->syn_data)
4438 case TCP_BPF_SNDCWND_CLAMP:
4442 tp->snd_cwnd_clamp = val;
4443 tp->snd_ssthresh = val;
4447 if (val < 0 || val > 1)
4463 static int _bpf_getsockopt(struct sock *sk, int level, int optname,
4464 char *optval, int optlen)
4466 if (!sk_fullsock(sk))
4469 sock_owned_by_me(sk);
4472 if (level == SOL_TCP && sk->sk_prot->getsockopt == tcp_getsockopt) {
4473 struct inet_connection_sock *icsk;
4474 struct tcp_sock *tp;
4477 case TCP_CONGESTION:
4478 icsk = inet_csk(sk);
4480 if (!icsk->icsk_ca_ops || optlen <= 1)
4482 strncpy(optval, icsk->icsk_ca_ops->name, optlen);
4483 optval[optlen - 1] = 0;
4488 if (optlen <= 0 || !tp->saved_syn ||
4489 optlen > tp->saved_syn[0])
4491 memcpy(optval, tp->saved_syn + 1, optlen);
4496 } else if (level == SOL_IP) {
4497 struct inet_sock *inet = inet_sk(sk);
4499 if (optlen != sizeof(int) || sk->sk_family != AF_INET)
4502 /* Only some options are supported */
4505 *((int *)optval) = (int)inet->tos;
4510 #if IS_ENABLED(CONFIG_IPV6)
4511 } else if (level == SOL_IPV6) {
4512 struct ipv6_pinfo *np = inet6_sk(sk);
4514 if (optlen != sizeof(int) || sk->sk_family != AF_INET6)
4517 /* Only some options are supported */
4520 *((int *)optval) = (int)np->tclass;
4532 memset(optval, 0, optlen);
4536 BPF_CALL_5(bpf_sock_addr_setsockopt, struct bpf_sock_addr_kern *, ctx,
4537 int, level, int, optname, char *, optval, int, optlen)
4540 return _bpf_setsockopt(ctx->sk, level, optname, optval, optlen,
4544 static const struct bpf_func_proto bpf_sock_addr_setsockopt_proto = {
4545 .func = bpf_sock_addr_setsockopt,
4547 .ret_type = RET_INTEGER,
4548 .arg1_type = ARG_PTR_TO_CTX,
4549 .arg2_type = ARG_ANYTHING,
4550 .arg3_type = ARG_ANYTHING,
4551 .arg4_type = ARG_PTR_TO_MEM,
4552 .arg5_type = ARG_CONST_SIZE,
4555 BPF_CALL_5(bpf_sock_addr_getsockopt, struct bpf_sock_addr_kern *, ctx,
4556 int, level, int, optname, char *, optval, int, optlen)
4558 return _bpf_getsockopt(ctx->sk, level, optname, optval, optlen);
4561 static const struct bpf_func_proto bpf_sock_addr_getsockopt_proto = {
4562 .func = bpf_sock_addr_getsockopt,
4564 .ret_type = RET_INTEGER,
4565 .arg1_type = ARG_PTR_TO_CTX,
4566 .arg2_type = ARG_ANYTHING,
4567 .arg3_type = ARG_ANYTHING,
4568 .arg4_type = ARG_PTR_TO_UNINIT_MEM,
4569 .arg5_type = ARG_CONST_SIZE,
4572 BPF_CALL_5(bpf_sock_ops_setsockopt, struct bpf_sock_ops_kern *, bpf_sock,
4573 int, level, int, optname, char *, optval, int, optlen)
4576 if (bpf_sock->op > BPF_SOCK_OPS_NEEDS_ECN)
4577 flags |= SOCKOPT_CC_REINIT;
4578 return _bpf_setsockopt(bpf_sock->sk, level, optname, optval, optlen,
4582 static const struct bpf_func_proto bpf_sock_ops_setsockopt_proto = {
4583 .func = bpf_sock_ops_setsockopt,
4585 .ret_type = RET_INTEGER,
4586 .arg1_type = ARG_PTR_TO_CTX,
4587 .arg2_type = ARG_ANYTHING,
4588 .arg3_type = ARG_ANYTHING,
4589 .arg4_type = ARG_PTR_TO_MEM,
4590 .arg5_type = ARG_CONST_SIZE,
4593 BPF_CALL_5(bpf_sock_ops_getsockopt, struct bpf_sock_ops_kern *, bpf_sock,
4594 int, level, int, optname, char *, optval, int, optlen)
4596 return _bpf_getsockopt(bpf_sock->sk, level, optname, optval, optlen);
4599 static const struct bpf_func_proto bpf_sock_ops_getsockopt_proto = {
4600 .func = bpf_sock_ops_getsockopt,
4602 .ret_type = RET_INTEGER,
4603 .arg1_type = ARG_PTR_TO_CTX,
4604 .arg2_type = ARG_ANYTHING,
4605 .arg3_type = ARG_ANYTHING,
4606 .arg4_type = ARG_PTR_TO_UNINIT_MEM,
4607 .arg5_type = ARG_CONST_SIZE,
4610 BPF_CALL_2(bpf_sock_ops_cb_flags_set, struct bpf_sock_ops_kern *, bpf_sock,
4613 struct sock *sk = bpf_sock->sk;
4614 int val = argval & BPF_SOCK_OPS_ALL_CB_FLAGS;
4616 if (!IS_ENABLED(CONFIG_INET) || !sk_fullsock(sk))
4619 tcp_sk(sk)->bpf_sock_ops_cb_flags = val;
4621 return argval & (~BPF_SOCK_OPS_ALL_CB_FLAGS);
4624 static const struct bpf_func_proto bpf_sock_ops_cb_flags_set_proto = {
4625 .func = bpf_sock_ops_cb_flags_set,
4627 .ret_type = RET_INTEGER,
4628 .arg1_type = ARG_PTR_TO_CTX,
4629 .arg2_type = ARG_ANYTHING,
4632 const struct ipv6_bpf_stub *ipv6_bpf_stub __read_mostly;
4633 EXPORT_SYMBOL_GPL(ipv6_bpf_stub);
4635 BPF_CALL_3(bpf_bind, struct bpf_sock_addr_kern *, ctx, struct sockaddr *, addr,
4639 struct sock *sk = ctx->sk;
4640 u32 flags = BIND_FROM_BPF;
4644 if (addr_len < offsetofend(struct sockaddr, sa_family))
4646 if (addr->sa_family == AF_INET) {
4647 if (addr_len < sizeof(struct sockaddr_in))
4649 if (((struct sockaddr_in *)addr)->sin_port == htons(0))
4650 flags |= BIND_FORCE_ADDRESS_NO_PORT;
4651 return __inet_bind(sk, addr, addr_len, flags);
4652 #if IS_ENABLED(CONFIG_IPV6)
4653 } else if (addr->sa_family == AF_INET6) {
4654 if (addr_len < SIN6_LEN_RFC2133)
4656 if (((struct sockaddr_in6 *)addr)->sin6_port == htons(0))
4657 flags |= BIND_FORCE_ADDRESS_NO_PORT;
4658 /* ipv6_bpf_stub cannot be NULL, since it's called from
4659 * bpf_cgroup_inet6_connect hook and ipv6 is already loaded
4661 return ipv6_bpf_stub->inet6_bind(sk, addr, addr_len, flags);
4662 #endif /* CONFIG_IPV6 */
4664 #endif /* CONFIG_INET */
4666 return -EAFNOSUPPORT;
4669 static const struct bpf_func_proto bpf_bind_proto = {
4672 .ret_type = RET_INTEGER,
4673 .arg1_type = ARG_PTR_TO_CTX,
4674 .arg2_type = ARG_PTR_TO_MEM,
4675 .arg3_type = ARG_CONST_SIZE,
4679 BPF_CALL_5(bpf_skb_get_xfrm_state, struct sk_buff *, skb, u32, index,
4680 struct bpf_xfrm_state *, to, u32, size, u64, flags)
4682 const struct sec_path *sp = skb_sec_path(skb);
4683 const struct xfrm_state *x;
4685 if (!sp || unlikely(index >= sp->len || flags))
4688 x = sp->xvec[index];
4690 if (unlikely(size != sizeof(struct bpf_xfrm_state)))
4693 to->reqid = x->props.reqid;
4694 to->spi = x->id.spi;
4695 to->family = x->props.family;
4698 if (to->family == AF_INET6) {
4699 memcpy(to->remote_ipv6, x->props.saddr.a6,
4700 sizeof(to->remote_ipv6));
4702 to->remote_ipv4 = x->props.saddr.a4;
4703 memset(&to->remote_ipv6[1], 0, sizeof(__u32) * 3);
4708 memset(to, 0, size);
4712 static const struct bpf_func_proto bpf_skb_get_xfrm_state_proto = {
4713 .func = bpf_skb_get_xfrm_state,
4715 .ret_type = RET_INTEGER,
4716 .arg1_type = ARG_PTR_TO_CTX,
4717 .arg2_type = ARG_ANYTHING,
4718 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
4719 .arg4_type = ARG_CONST_SIZE,
4720 .arg5_type = ARG_ANYTHING,
4724 #if IS_ENABLED(CONFIG_INET) || IS_ENABLED(CONFIG_IPV6)
4725 static int bpf_fib_set_fwd_params(struct bpf_fib_lookup *params,
4726 const struct neighbour *neigh,
4727 const struct net_device *dev)
4729 memcpy(params->dmac, neigh->ha, ETH_ALEN);
4730 memcpy(params->smac, dev->dev_addr, ETH_ALEN);
4731 params->h_vlan_TCI = 0;
4732 params->h_vlan_proto = 0;
4733 params->ifindex = dev->ifindex;
4739 #if IS_ENABLED(CONFIG_INET)
4740 static int bpf_ipv4_fib_lookup(struct net *net, struct bpf_fib_lookup *params,
4741 u32 flags, bool check_mtu)
4743 struct fib_nh_common *nhc;
4744 struct in_device *in_dev;
4745 struct neighbour *neigh;
4746 struct net_device *dev;
4747 struct fib_result res;
4752 dev = dev_get_by_index_rcu(net, params->ifindex);
4756 /* verify forwarding is enabled on this interface */
4757 in_dev = __in_dev_get_rcu(dev);
4758 if (unlikely(!in_dev || !IN_DEV_FORWARD(in_dev)))
4759 return BPF_FIB_LKUP_RET_FWD_DISABLED;
4761 if (flags & BPF_FIB_LOOKUP_OUTPUT) {
4763 fl4.flowi4_oif = params->ifindex;
4765 fl4.flowi4_iif = params->ifindex;
4768 fl4.flowi4_tos = params->tos & IPTOS_RT_MASK;
4769 fl4.flowi4_scope = RT_SCOPE_UNIVERSE;
4770 fl4.flowi4_flags = 0;
4772 fl4.flowi4_proto = params->l4_protocol;
4773 fl4.daddr = params->ipv4_dst;
4774 fl4.saddr = params->ipv4_src;
4775 fl4.fl4_sport = params->sport;
4776 fl4.fl4_dport = params->dport;
4778 if (flags & BPF_FIB_LOOKUP_DIRECT) {
4779 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
4780 struct fib_table *tb;
4782 tb = fib_get_table(net, tbid);
4784 return BPF_FIB_LKUP_RET_NOT_FWDED;
4786 err = fib_table_lookup(tb, &fl4, &res, FIB_LOOKUP_NOREF);
4788 fl4.flowi4_mark = 0;
4789 fl4.flowi4_secid = 0;
4790 fl4.flowi4_tun_key.tun_id = 0;
4791 fl4.flowi4_uid = sock_net_uid(net, NULL);
4793 err = fib_lookup(net, &fl4, &res, FIB_LOOKUP_NOREF);
4797 /* map fib lookup errors to RTN_ type */
4799 return BPF_FIB_LKUP_RET_BLACKHOLE;
4800 if (err == -EHOSTUNREACH)
4801 return BPF_FIB_LKUP_RET_UNREACHABLE;
4803 return BPF_FIB_LKUP_RET_PROHIBIT;
4805 return BPF_FIB_LKUP_RET_NOT_FWDED;
4808 if (res.type != RTN_UNICAST)
4809 return BPF_FIB_LKUP_RET_NOT_FWDED;
4811 if (fib_info_num_path(res.fi) > 1)
4812 fib_select_path(net, &res, &fl4, NULL);
4815 mtu = ip_mtu_from_fib_result(&res, params->ipv4_dst);
4816 if (params->tot_len > mtu)
4817 return BPF_FIB_LKUP_RET_FRAG_NEEDED;
4822 /* do not handle lwt encaps right now */
4823 if (nhc->nhc_lwtstate)
4824 return BPF_FIB_LKUP_RET_UNSUPP_LWT;
4828 params->rt_metric = res.fi->fib_priority;
4830 /* xdp and cls_bpf programs are run in RCU-bh so
4831 * rcu_read_lock_bh is not needed here
4833 if (likely(nhc->nhc_gw_family != AF_INET6)) {
4834 if (nhc->nhc_gw_family)
4835 params->ipv4_dst = nhc->nhc_gw.ipv4;
4837 neigh = __ipv4_neigh_lookup_noref(dev,
4838 (__force u32)params->ipv4_dst);
4840 struct in6_addr *dst = (struct in6_addr *)params->ipv6_dst;
4842 params->family = AF_INET6;
4843 *dst = nhc->nhc_gw.ipv6;
4844 neigh = __ipv6_neigh_lookup_noref_stub(dev, dst);
4848 return BPF_FIB_LKUP_RET_NO_NEIGH;
4850 return bpf_fib_set_fwd_params(params, neigh, dev);
4854 #if IS_ENABLED(CONFIG_IPV6)
4855 static int bpf_ipv6_fib_lookup(struct net *net, struct bpf_fib_lookup *params,
4856 u32 flags, bool check_mtu)
4858 struct in6_addr *src = (struct in6_addr *) params->ipv6_src;
4859 struct in6_addr *dst = (struct in6_addr *) params->ipv6_dst;
4860 struct fib6_result res = {};
4861 struct neighbour *neigh;
4862 struct net_device *dev;
4863 struct inet6_dev *idev;
4869 /* link local addresses are never forwarded */
4870 if (rt6_need_strict(dst) || rt6_need_strict(src))
4871 return BPF_FIB_LKUP_RET_NOT_FWDED;
4873 dev = dev_get_by_index_rcu(net, params->ifindex);
4877 idev = __in6_dev_get_safely(dev);
4878 if (unlikely(!idev || !idev->cnf.forwarding))
4879 return BPF_FIB_LKUP_RET_FWD_DISABLED;
4881 if (flags & BPF_FIB_LOOKUP_OUTPUT) {
4883 oif = fl6.flowi6_oif = params->ifindex;
4885 oif = fl6.flowi6_iif = params->ifindex;
4887 strict = RT6_LOOKUP_F_HAS_SADDR;
4889 fl6.flowlabel = params->flowinfo;
4890 fl6.flowi6_scope = 0;
4891 fl6.flowi6_flags = 0;
4894 fl6.flowi6_proto = params->l4_protocol;
4897 fl6.fl6_sport = params->sport;
4898 fl6.fl6_dport = params->dport;
4900 if (flags & BPF_FIB_LOOKUP_DIRECT) {
4901 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
4902 struct fib6_table *tb;
4904 tb = ipv6_stub->fib6_get_table(net, tbid);
4906 return BPF_FIB_LKUP_RET_NOT_FWDED;
4908 err = ipv6_stub->fib6_table_lookup(net, tb, oif, &fl6, &res,
4911 fl6.flowi6_mark = 0;
4912 fl6.flowi6_secid = 0;
4913 fl6.flowi6_tun_key.tun_id = 0;
4914 fl6.flowi6_uid = sock_net_uid(net, NULL);
4916 err = ipv6_stub->fib6_lookup(net, oif, &fl6, &res, strict);
4919 if (unlikely(err || IS_ERR_OR_NULL(res.f6i) ||
4920 res.f6i == net->ipv6.fib6_null_entry))
4921 return BPF_FIB_LKUP_RET_NOT_FWDED;
4923 switch (res.fib6_type) {
4924 /* only unicast is forwarded */
4928 return BPF_FIB_LKUP_RET_BLACKHOLE;
4929 case RTN_UNREACHABLE:
4930 return BPF_FIB_LKUP_RET_UNREACHABLE;
4932 return BPF_FIB_LKUP_RET_PROHIBIT;
4934 return BPF_FIB_LKUP_RET_NOT_FWDED;
4937 ipv6_stub->fib6_select_path(net, &res, &fl6, fl6.flowi6_oif,
4938 fl6.flowi6_oif != 0, NULL, strict);
4941 mtu = ipv6_stub->ip6_mtu_from_fib6(&res, dst, src);
4942 if (params->tot_len > mtu)
4943 return BPF_FIB_LKUP_RET_FRAG_NEEDED;
4946 if (res.nh->fib_nh_lws)
4947 return BPF_FIB_LKUP_RET_UNSUPP_LWT;
4949 if (res.nh->fib_nh_gw_family)
4950 *dst = res.nh->fib_nh_gw6;
4952 dev = res.nh->fib_nh_dev;
4953 params->rt_metric = res.f6i->fib6_metric;
4955 /* xdp and cls_bpf programs are run in RCU-bh so rcu_read_lock_bh is
4958 neigh = __ipv6_neigh_lookup_noref_stub(dev, dst);
4960 return BPF_FIB_LKUP_RET_NO_NEIGH;
4962 return bpf_fib_set_fwd_params(params, neigh, dev);
4966 BPF_CALL_4(bpf_xdp_fib_lookup, struct xdp_buff *, ctx,
4967 struct bpf_fib_lookup *, params, int, plen, u32, flags)
4969 if (plen < sizeof(*params))
4972 if (flags & ~(BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_OUTPUT))
4975 switch (params->family) {
4976 #if IS_ENABLED(CONFIG_INET)
4978 return bpf_ipv4_fib_lookup(dev_net(ctx->rxq->dev), params,
4981 #if IS_ENABLED(CONFIG_IPV6)
4983 return bpf_ipv6_fib_lookup(dev_net(ctx->rxq->dev), params,
4987 return -EAFNOSUPPORT;
4990 static const struct bpf_func_proto bpf_xdp_fib_lookup_proto = {
4991 .func = bpf_xdp_fib_lookup,
4993 .ret_type = RET_INTEGER,
4994 .arg1_type = ARG_PTR_TO_CTX,
4995 .arg2_type = ARG_PTR_TO_MEM,
4996 .arg3_type = ARG_CONST_SIZE,
4997 .arg4_type = ARG_ANYTHING,
5000 BPF_CALL_4(bpf_skb_fib_lookup, struct sk_buff *, skb,
5001 struct bpf_fib_lookup *, params, int, plen, u32, flags)
5003 struct net *net = dev_net(skb->dev);
5004 int rc = -EAFNOSUPPORT;
5006 if (plen < sizeof(*params))
5009 if (flags & ~(BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_OUTPUT))
5012 switch (params->family) {
5013 #if IS_ENABLED(CONFIG_INET)
5015 rc = bpf_ipv4_fib_lookup(net, params, flags, false);
5018 #if IS_ENABLED(CONFIG_IPV6)
5020 rc = bpf_ipv6_fib_lookup(net, params, flags, false);
5026 struct net_device *dev;
5028 dev = dev_get_by_index_rcu(net, params->ifindex);
5029 if (!is_skb_forwardable(dev, skb))
5030 rc = BPF_FIB_LKUP_RET_FRAG_NEEDED;
5036 static const struct bpf_func_proto bpf_skb_fib_lookup_proto = {
5037 .func = bpf_skb_fib_lookup,
5039 .ret_type = RET_INTEGER,
5040 .arg1_type = ARG_PTR_TO_CTX,
5041 .arg2_type = ARG_PTR_TO_MEM,
5042 .arg3_type = ARG_CONST_SIZE,
5043 .arg4_type = ARG_ANYTHING,
5046 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
5047 static int bpf_push_seg6_encap(struct sk_buff *skb, u32 type, void *hdr, u32 len)
5050 struct ipv6_sr_hdr *srh = (struct ipv6_sr_hdr *)hdr;
5052 if (!seg6_validate_srh(srh, len))
5056 case BPF_LWT_ENCAP_SEG6_INLINE:
5057 if (skb->protocol != htons(ETH_P_IPV6))
5060 err = seg6_do_srh_inline(skb, srh);
5062 case BPF_LWT_ENCAP_SEG6:
5063 skb_reset_inner_headers(skb);
5064 skb->encapsulation = 1;
5065 err = seg6_do_srh_encap(skb, srh, IPPROTO_IPV6);
5071 bpf_compute_data_pointers(skb);
5075 ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
5076 skb_set_transport_header(skb, sizeof(struct ipv6hdr));
5078 return seg6_lookup_nexthop(skb, NULL, 0);
5080 #endif /* CONFIG_IPV6_SEG6_BPF */
5082 #if IS_ENABLED(CONFIG_LWTUNNEL_BPF)
5083 static int bpf_push_ip_encap(struct sk_buff *skb, void *hdr, u32 len,
5086 return bpf_lwt_push_ip_encap(skb, hdr, len, ingress);
5090 BPF_CALL_4(bpf_lwt_in_push_encap, struct sk_buff *, skb, u32, type, void *, hdr,
5094 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
5095 case BPF_LWT_ENCAP_SEG6:
5096 case BPF_LWT_ENCAP_SEG6_INLINE:
5097 return bpf_push_seg6_encap(skb, type, hdr, len);
5099 #if IS_ENABLED(CONFIG_LWTUNNEL_BPF)
5100 case BPF_LWT_ENCAP_IP:
5101 return bpf_push_ip_encap(skb, hdr, len, true /* ingress */);
5108 BPF_CALL_4(bpf_lwt_xmit_push_encap, struct sk_buff *, skb, u32, type,
5109 void *, hdr, u32, len)
5112 #if IS_ENABLED(CONFIG_LWTUNNEL_BPF)
5113 case BPF_LWT_ENCAP_IP:
5114 return bpf_push_ip_encap(skb, hdr, len, false /* egress */);
5121 static const struct bpf_func_proto bpf_lwt_in_push_encap_proto = {
5122 .func = bpf_lwt_in_push_encap,
5124 .ret_type = RET_INTEGER,
5125 .arg1_type = ARG_PTR_TO_CTX,
5126 .arg2_type = ARG_ANYTHING,
5127 .arg3_type = ARG_PTR_TO_MEM,
5128 .arg4_type = ARG_CONST_SIZE
5131 static const struct bpf_func_proto bpf_lwt_xmit_push_encap_proto = {
5132 .func = bpf_lwt_xmit_push_encap,
5134 .ret_type = RET_INTEGER,
5135 .arg1_type = ARG_PTR_TO_CTX,
5136 .arg2_type = ARG_ANYTHING,
5137 .arg3_type = ARG_PTR_TO_MEM,
5138 .arg4_type = ARG_CONST_SIZE
5141 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
5142 BPF_CALL_4(bpf_lwt_seg6_store_bytes, struct sk_buff *, skb, u32, offset,
5143 const void *, from, u32, len)
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_tlvs, *srh_end, *ptr;
5154 srh_tlvs = (void *)((char *)srh + ((srh->first_segment + 1) << 4));
5155 srh_end = (void *)((char *)srh + sizeof(*srh) + srh_state->hdrlen);
5157 ptr = skb->data + offset;
5158 if (ptr >= srh_tlvs && ptr + len <= srh_end)
5159 srh_state->valid = false;
5160 else if (ptr < (void *)&srh->flags ||
5161 ptr + len > (void *)&srh->segments)
5164 if (unlikely(bpf_try_make_writable(skb, offset + len)))
5166 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0)
5168 srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
5170 memcpy(skb->data + offset, from, len);
5174 static const struct bpf_func_proto bpf_lwt_seg6_store_bytes_proto = {
5175 .func = bpf_lwt_seg6_store_bytes,
5177 .ret_type = RET_INTEGER,
5178 .arg1_type = ARG_PTR_TO_CTX,
5179 .arg2_type = ARG_ANYTHING,
5180 .arg3_type = ARG_PTR_TO_MEM,
5181 .arg4_type = ARG_CONST_SIZE
5184 static void bpf_update_srh_state(struct sk_buff *skb)
5186 struct seg6_bpf_srh_state *srh_state =
5187 this_cpu_ptr(&seg6_bpf_srh_states);
5190 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0) {
5191 srh_state->srh = NULL;
5193 srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
5194 srh_state->hdrlen = srh_state->srh->hdrlen << 3;
5195 srh_state->valid = true;
5199 BPF_CALL_4(bpf_lwt_seg6_action, struct sk_buff *, skb,
5200 u32, action, void *, param, u32, param_len)
5202 struct seg6_bpf_srh_state *srh_state =
5203 this_cpu_ptr(&seg6_bpf_srh_states);
5208 case SEG6_LOCAL_ACTION_END_X:
5209 if (!seg6_bpf_has_valid_srh(skb))
5211 if (param_len != sizeof(struct in6_addr))
5213 return seg6_lookup_nexthop(skb, (struct in6_addr *)param, 0);
5214 case SEG6_LOCAL_ACTION_END_T:
5215 if (!seg6_bpf_has_valid_srh(skb))
5217 if (param_len != sizeof(int))
5219 return seg6_lookup_nexthop(skb, NULL, *(int *)param);
5220 case SEG6_LOCAL_ACTION_END_DT6:
5221 if (!seg6_bpf_has_valid_srh(skb))
5223 if (param_len != sizeof(int))
5226 if (ipv6_find_hdr(skb, &hdroff, IPPROTO_IPV6, NULL, NULL) < 0)
5228 if (!pskb_pull(skb, hdroff))
5231 skb_postpull_rcsum(skb, skb_network_header(skb), hdroff);
5232 skb_reset_network_header(skb);
5233 skb_reset_transport_header(skb);
5234 skb->encapsulation = 0;
5236 bpf_compute_data_pointers(skb);
5237 bpf_update_srh_state(skb);
5238 return seg6_lookup_nexthop(skb, NULL, *(int *)param);
5239 case SEG6_LOCAL_ACTION_END_B6:
5240 if (srh_state->srh && !seg6_bpf_has_valid_srh(skb))
5242 err = bpf_push_seg6_encap(skb, BPF_LWT_ENCAP_SEG6_INLINE,
5245 bpf_update_srh_state(skb);
5248 case SEG6_LOCAL_ACTION_END_B6_ENCAP:
5249 if (srh_state->srh && !seg6_bpf_has_valid_srh(skb))
5251 err = bpf_push_seg6_encap(skb, BPF_LWT_ENCAP_SEG6,
5254 bpf_update_srh_state(skb);
5262 static const struct bpf_func_proto bpf_lwt_seg6_action_proto = {
5263 .func = bpf_lwt_seg6_action,
5265 .ret_type = RET_INTEGER,
5266 .arg1_type = ARG_PTR_TO_CTX,
5267 .arg2_type = ARG_ANYTHING,
5268 .arg3_type = ARG_PTR_TO_MEM,
5269 .arg4_type = ARG_CONST_SIZE
5272 BPF_CALL_3(bpf_lwt_seg6_adjust_srh, struct sk_buff *, skb, u32, offset,
5275 struct seg6_bpf_srh_state *srh_state =
5276 this_cpu_ptr(&seg6_bpf_srh_states);
5277 struct ipv6_sr_hdr *srh = srh_state->srh;
5278 void *srh_end, *srh_tlvs, *ptr;
5279 struct ipv6hdr *hdr;
5283 if (unlikely(srh == NULL))
5286 srh_tlvs = (void *)((unsigned char *)srh + sizeof(*srh) +
5287 ((srh->first_segment + 1) << 4));
5288 srh_end = (void *)((unsigned char *)srh + sizeof(*srh) +
5290 ptr = skb->data + offset;
5292 if (unlikely(ptr < srh_tlvs || ptr > srh_end))
5294 if (unlikely(len < 0 && (void *)((char *)ptr - len) > srh_end))
5298 ret = skb_cow_head(skb, len);
5299 if (unlikely(ret < 0))
5302 ret = bpf_skb_net_hdr_push(skb, offset, len);
5304 ret = bpf_skb_net_hdr_pop(skb, offset, -1 * len);
5307 bpf_compute_data_pointers(skb);
5308 if (unlikely(ret < 0))
5311 hdr = (struct ipv6hdr *)skb->data;
5312 hdr->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
5314 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0)
5316 srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
5317 srh_state->hdrlen += len;
5318 srh_state->valid = false;
5322 static const struct bpf_func_proto bpf_lwt_seg6_adjust_srh_proto = {
5323 .func = bpf_lwt_seg6_adjust_srh,
5325 .ret_type = RET_INTEGER,
5326 .arg1_type = ARG_PTR_TO_CTX,
5327 .arg2_type = ARG_ANYTHING,
5328 .arg3_type = ARG_ANYTHING,
5330 #endif /* CONFIG_IPV6_SEG6_BPF */
5333 static struct sock *sk_lookup(struct net *net, struct bpf_sock_tuple *tuple,
5334 int dif, int sdif, u8 family, u8 proto)
5336 bool refcounted = false;
5337 struct sock *sk = NULL;
5339 if (family == AF_INET) {
5340 __be32 src4 = tuple->ipv4.saddr;
5341 __be32 dst4 = tuple->ipv4.daddr;
5343 if (proto == IPPROTO_TCP)
5344 sk = __inet_lookup(net, &tcp_hashinfo, NULL, 0,
5345 src4, tuple->ipv4.sport,
5346 dst4, tuple->ipv4.dport,
5347 dif, sdif, &refcounted);
5349 sk = __udp4_lib_lookup(net, src4, tuple->ipv4.sport,
5350 dst4, tuple->ipv4.dport,
5351 dif, sdif, &udp_table, NULL);
5352 #if IS_ENABLED(CONFIG_IPV6)
5354 struct in6_addr *src6 = (struct in6_addr *)&tuple->ipv6.saddr;
5355 struct in6_addr *dst6 = (struct in6_addr *)&tuple->ipv6.daddr;
5357 if (proto == IPPROTO_TCP)
5358 sk = __inet6_lookup(net, &tcp_hashinfo, NULL, 0,
5359 src6, tuple->ipv6.sport,
5360 dst6, ntohs(tuple->ipv6.dport),
5361 dif, sdif, &refcounted);
5362 else if (likely(ipv6_bpf_stub))
5363 sk = ipv6_bpf_stub->udp6_lib_lookup(net,
5364 src6, tuple->ipv6.sport,
5365 dst6, tuple->ipv6.dport,
5371 if (unlikely(sk && !refcounted && !sock_flag(sk, SOCK_RCU_FREE))) {
5372 WARN_ONCE(1, "Found non-RCU, unreferenced socket!");
5378 /* bpf_skc_lookup performs the core lookup for different types of sockets,
5379 * taking a reference on the socket if it doesn't have the flag SOCK_RCU_FREE.
5380 * Returns the socket as an 'unsigned long' to simplify the casting in the
5381 * callers to satisfy BPF_CALL declarations.
5383 static struct sock *
5384 __bpf_skc_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
5385 struct net *caller_net, u32 ifindex, u8 proto, u64 netns_id,
5388 struct sock *sk = NULL;
5389 u8 family = AF_UNSPEC;
5393 if (len == sizeof(tuple->ipv4))
5395 else if (len == sizeof(tuple->ipv6))
5400 if (unlikely(family == AF_UNSPEC || flags ||
5401 !((s32)netns_id < 0 || netns_id <= S32_MAX)))
5404 if (family == AF_INET)
5405 sdif = inet_sdif(skb);
5407 sdif = inet6_sdif(skb);
5409 if ((s32)netns_id < 0) {
5411 sk = sk_lookup(net, tuple, ifindex, sdif, family, proto);
5413 net = get_net_ns_by_id(caller_net, netns_id);
5416 sk = sk_lookup(net, tuple, ifindex, sdif, family, proto);
5424 static struct sock *
5425 __bpf_sk_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
5426 struct net *caller_net, u32 ifindex, u8 proto, u64 netns_id,
5429 struct sock *sk = __bpf_skc_lookup(skb, tuple, len, caller_net,
5430 ifindex, proto, netns_id, flags);
5433 sk = sk_to_full_sk(sk);
5434 if (!sk_fullsock(sk)) {
5443 static struct sock *
5444 bpf_skc_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
5445 u8 proto, u64 netns_id, u64 flags)
5447 struct net *caller_net;
5451 caller_net = dev_net(skb->dev);
5452 ifindex = skb->dev->ifindex;
5454 caller_net = sock_net(skb->sk);
5458 return __bpf_skc_lookup(skb, tuple, len, caller_net, ifindex, proto,
5462 static struct sock *
5463 bpf_sk_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
5464 u8 proto, u64 netns_id, u64 flags)
5466 struct sock *sk = bpf_skc_lookup(skb, tuple, len, proto, netns_id,
5470 sk = sk_to_full_sk(sk);
5471 if (!sk_fullsock(sk)) {
5480 BPF_CALL_5(bpf_skc_lookup_tcp, struct sk_buff *, skb,
5481 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
5483 return (unsigned long)bpf_skc_lookup(skb, tuple, len, IPPROTO_TCP,
5487 static const struct bpf_func_proto bpf_skc_lookup_tcp_proto = {
5488 .func = bpf_skc_lookup_tcp,
5491 .ret_type = RET_PTR_TO_SOCK_COMMON_OR_NULL,
5492 .arg1_type = ARG_PTR_TO_CTX,
5493 .arg2_type = ARG_PTR_TO_MEM,
5494 .arg3_type = ARG_CONST_SIZE,
5495 .arg4_type = ARG_ANYTHING,
5496 .arg5_type = ARG_ANYTHING,
5499 BPF_CALL_5(bpf_sk_lookup_tcp, struct sk_buff *, skb,
5500 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
5502 return (unsigned long)bpf_sk_lookup(skb, tuple, len, IPPROTO_TCP,
5506 static const struct bpf_func_proto bpf_sk_lookup_tcp_proto = {
5507 .func = bpf_sk_lookup_tcp,
5510 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
5511 .arg1_type = ARG_PTR_TO_CTX,
5512 .arg2_type = ARG_PTR_TO_MEM,
5513 .arg3_type = ARG_CONST_SIZE,
5514 .arg4_type = ARG_ANYTHING,
5515 .arg5_type = ARG_ANYTHING,
5518 BPF_CALL_5(bpf_sk_lookup_udp, struct sk_buff *, skb,
5519 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
5521 return (unsigned long)bpf_sk_lookup(skb, tuple, len, IPPROTO_UDP,
5525 static const struct bpf_func_proto bpf_sk_lookup_udp_proto = {
5526 .func = bpf_sk_lookup_udp,
5529 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
5530 .arg1_type = ARG_PTR_TO_CTX,
5531 .arg2_type = ARG_PTR_TO_MEM,
5532 .arg3_type = ARG_CONST_SIZE,
5533 .arg4_type = ARG_ANYTHING,
5534 .arg5_type = ARG_ANYTHING,
5537 BPF_CALL_1(bpf_sk_release, struct sock *, sk)
5539 if (sk_is_refcounted(sk))
5544 static const struct bpf_func_proto bpf_sk_release_proto = {
5545 .func = bpf_sk_release,
5547 .ret_type = RET_INTEGER,
5548 .arg1_type = ARG_PTR_TO_SOCK_COMMON,
5551 BPF_CALL_5(bpf_xdp_sk_lookup_udp, struct xdp_buff *, ctx,
5552 struct bpf_sock_tuple *, tuple, u32, len, u32, netns_id, u64, flags)
5554 struct net *caller_net = dev_net(ctx->rxq->dev);
5555 int ifindex = ctx->rxq->dev->ifindex;
5557 return (unsigned long)__bpf_sk_lookup(NULL, tuple, len, caller_net,
5558 ifindex, IPPROTO_UDP, netns_id,
5562 static const struct bpf_func_proto bpf_xdp_sk_lookup_udp_proto = {
5563 .func = bpf_xdp_sk_lookup_udp,
5566 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
5567 .arg1_type = ARG_PTR_TO_CTX,
5568 .arg2_type = ARG_PTR_TO_MEM,
5569 .arg3_type = ARG_CONST_SIZE,
5570 .arg4_type = ARG_ANYTHING,
5571 .arg5_type = ARG_ANYTHING,
5574 BPF_CALL_5(bpf_xdp_skc_lookup_tcp, struct xdp_buff *, ctx,
5575 struct bpf_sock_tuple *, tuple, u32, len, u32, netns_id, u64, flags)
5577 struct net *caller_net = dev_net(ctx->rxq->dev);
5578 int ifindex = ctx->rxq->dev->ifindex;
5580 return (unsigned long)__bpf_skc_lookup(NULL, tuple, len, caller_net,
5581 ifindex, IPPROTO_TCP, netns_id,
5585 static const struct bpf_func_proto bpf_xdp_skc_lookup_tcp_proto = {
5586 .func = bpf_xdp_skc_lookup_tcp,
5589 .ret_type = RET_PTR_TO_SOCK_COMMON_OR_NULL,
5590 .arg1_type = ARG_PTR_TO_CTX,
5591 .arg2_type = ARG_PTR_TO_MEM,
5592 .arg3_type = ARG_CONST_SIZE,
5593 .arg4_type = ARG_ANYTHING,
5594 .arg5_type = ARG_ANYTHING,
5597 BPF_CALL_5(bpf_xdp_sk_lookup_tcp, struct xdp_buff *, ctx,
5598 struct bpf_sock_tuple *, tuple, u32, len, u32, netns_id, u64, flags)
5600 struct net *caller_net = dev_net(ctx->rxq->dev);
5601 int ifindex = ctx->rxq->dev->ifindex;
5603 return (unsigned long)__bpf_sk_lookup(NULL, tuple, len, caller_net,
5604 ifindex, IPPROTO_TCP, netns_id,
5608 static const struct bpf_func_proto bpf_xdp_sk_lookup_tcp_proto = {
5609 .func = bpf_xdp_sk_lookup_tcp,
5612 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
5613 .arg1_type = ARG_PTR_TO_CTX,
5614 .arg2_type = ARG_PTR_TO_MEM,
5615 .arg3_type = ARG_CONST_SIZE,
5616 .arg4_type = ARG_ANYTHING,
5617 .arg5_type = ARG_ANYTHING,
5620 BPF_CALL_5(bpf_sock_addr_skc_lookup_tcp, struct bpf_sock_addr_kern *, ctx,
5621 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
5623 return (unsigned long)__bpf_skc_lookup(NULL, tuple, len,
5624 sock_net(ctx->sk), 0,
5625 IPPROTO_TCP, netns_id, flags);
5628 static const struct bpf_func_proto bpf_sock_addr_skc_lookup_tcp_proto = {
5629 .func = bpf_sock_addr_skc_lookup_tcp,
5631 .ret_type = RET_PTR_TO_SOCK_COMMON_OR_NULL,
5632 .arg1_type = ARG_PTR_TO_CTX,
5633 .arg2_type = ARG_PTR_TO_MEM,
5634 .arg3_type = ARG_CONST_SIZE,
5635 .arg4_type = ARG_ANYTHING,
5636 .arg5_type = ARG_ANYTHING,
5639 BPF_CALL_5(bpf_sock_addr_sk_lookup_tcp, struct bpf_sock_addr_kern *, ctx,
5640 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
5642 return (unsigned long)__bpf_sk_lookup(NULL, tuple, len,
5643 sock_net(ctx->sk), 0, IPPROTO_TCP,
5647 static const struct bpf_func_proto bpf_sock_addr_sk_lookup_tcp_proto = {
5648 .func = bpf_sock_addr_sk_lookup_tcp,
5650 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
5651 .arg1_type = ARG_PTR_TO_CTX,
5652 .arg2_type = ARG_PTR_TO_MEM,
5653 .arg3_type = ARG_CONST_SIZE,
5654 .arg4_type = ARG_ANYTHING,
5655 .arg5_type = ARG_ANYTHING,
5658 BPF_CALL_5(bpf_sock_addr_sk_lookup_udp, struct bpf_sock_addr_kern *, ctx,
5659 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
5661 return (unsigned long)__bpf_sk_lookup(NULL, tuple, len,
5662 sock_net(ctx->sk), 0, IPPROTO_UDP,
5666 static const struct bpf_func_proto bpf_sock_addr_sk_lookup_udp_proto = {
5667 .func = bpf_sock_addr_sk_lookup_udp,
5669 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
5670 .arg1_type = ARG_PTR_TO_CTX,
5671 .arg2_type = ARG_PTR_TO_MEM,
5672 .arg3_type = ARG_CONST_SIZE,
5673 .arg4_type = ARG_ANYTHING,
5674 .arg5_type = ARG_ANYTHING,
5677 bool bpf_tcp_sock_is_valid_access(int off, int size, enum bpf_access_type type,
5678 struct bpf_insn_access_aux *info)
5680 if (off < 0 || off >= offsetofend(struct bpf_tcp_sock,
5684 if (off % size != 0)
5688 case offsetof(struct bpf_tcp_sock, bytes_received):
5689 case offsetof(struct bpf_tcp_sock, bytes_acked):
5690 return size == sizeof(__u64);
5692 return size == sizeof(__u32);
5696 u32 bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type,
5697 const struct bpf_insn *si,
5698 struct bpf_insn *insn_buf,
5699 struct bpf_prog *prog, u32 *target_size)
5701 struct bpf_insn *insn = insn_buf;
5703 #define BPF_TCP_SOCK_GET_COMMON(FIELD) \
5705 BUILD_BUG_ON(sizeof_field(struct tcp_sock, FIELD) > \
5706 sizeof_field(struct bpf_tcp_sock, FIELD)); \
5707 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct tcp_sock, FIELD),\
5708 si->dst_reg, si->src_reg, \
5709 offsetof(struct tcp_sock, FIELD)); \
5712 #define BPF_INET_SOCK_GET_COMMON(FIELD) \
5714 BUILD_BUG_ON(sizeof_field(struct inet_connection_sock, \
5716 sizeof_field(struct bpf_tcp_sock, FIELD)); \
5717 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
5718 struct inet_connection_sock, \
5720 si->dst_reg, si->src_reg, \
5722 struct inet_connection_sock, \
5726 if (insn > insn_buf)
5727 return insn - insn_buf;
5730 case offsetof(struct bpf_tcp_sock, rtt_min):
5731 BUILD_BUG_ON(sizeof_field(struct tcp_sock, rtt_min) !=
5732 sizeof(struct minmax));
5733 BUILD_BUG_ON(sizeof(struct minmax) <
5734 sizeof(struct minmax_sample));
5736 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
5737 offsetof(struct tcp_sock, rtt_min) +
5738 offsetof(struct minmax_sample, v));
5740 case offsetof(struct bpf_tcp_sock, snd_cwnd):
5741 BPF_TCP_SOCK_GET_COMMON(snd_cwnd);
5743 case offsetof(struct bpf_tcp_sock, srtt_us):
5744 BPF_TCP_SOCK_GET_COMMON(srtt_us);
5746 case offsetof(struct bpf_tcp_sock, snd_ssthresh):
5747 BPF_TCP_SOCK_GET_COMMON(snd_ssthresh);
5749 case offsetof(struct bpf_tcp_sock, rcv_nxt):
5750 BPF_TCP_SOCK_GET_COMMON(rcv_nxt);
5752 case offsetof(struct bpf_tcp_sock, snd_nxt):
5753 BPF_TCP_SOCK_GET_COMMON(snd_nxt);
5755 case offsetof(struct bpf_tcp_sock, snd_una):
5756 BPF_TCP_SOCK_GET_COMMON(snd_una);
5758 case offsetof(struct bpf_tcp_sock, mss_cache):
5759 BPF_TCP_SOCK_GET_COMMON(mss_cache);
5761 case offsetof(struct bpf_tcp_sock, ecn_flags):
5762 BPF_TCP_SOCK_GET_COMMON(ecn_flags);
5764 case offsetof(struct bpf_tcp_sock, rate_delivered):
5765 BPF_TCP_SOCK_GET_COMMON(rate_delivered);
5767 case offsetof(struct bpf_tcp_sock, rate_interval_us):
5768 BPF_TCP_SOCK_GET_COMMON(rate_interval_us);
5770 case offsetof(struct bpf_tcp_sock, packets_out):
5771 BPF_TCP_SOCK_GET_COMMON(packets_out);
5773 case offsetof(struct bpf_tcp_sock, retrans_out):
5774 BPF_TCP_SOCK_GET_COMMON(retrans_out);
5776 case offsetof(struct bpf_tcp_sock, total_retrans):
5777 BPF_TCP_SOCK_GET_COMMON(total_retrans);
5779 case offsetof(struct bpf_tcp_sock, segs_in):
5780 BPF_TCP_SOCK_GET_COMMON(segs_in);
5782 case offsetof(struct bpf_tcp_sock, data_segs_in):
5783 BPF_TCP_SOCK_GET_COMMON(data_segs_in);
5785 case offsetof(struct bpf_tcp_sock, segs_out):
5786 BPF_TCP_SOCK_GET_COMMON(segs_out);
5788 case offsetof(struct bpf_tcp_sock, data_segs_out):
5789 BPF_TCP_SOCK_GET_COMMON(data_segs_out);
5791 case offsetof(struct bpf_tcp_sock, lost_out):
5792 BPF_TCP_SOCK_GET_COMMON(lost_out);
5794 case offsetof(struct bpf_tcp_sock, sacked_out):
5795 BPF_TCP_SOCK_GET_COMMON(sacked_out);
5797 case offsetof(struct bpf_tcp_sock, bytes_received):
5798 BPF_TCP_SOCK_GET_COMMON(bytes_received);
5800 case offsetof(struct bpf_tcp_sock, bytes_acked):
5801 BPF_TCP_SOCK_GET_COMMON(bytes_acked);
5803 case offsetof(struct bpf_tcp_sock, dsack_dups):
5804 BPF_TCP_SOCK_GET_COMMON(dsack_dups);
5806 case offsetof(struct bpf_tcp_sock, delivered):
5807 BPF_TCP_SOCK_GET_COMMON(delivered);
5809 case offsetof(struct bpf_tcp_sock, delivered_ce):
5810 BPF_TCP_SOCK_GET_COMMON(delivered_ce);
5812 case offsetof(struct bpf_tcp_sock, icsk_retransmits):
5813 BPF_INET_SOCK_GET_COMMON(icsk_retransmits);
5817 return insn - insn_buf;
5820 BPF_CALL_1(bpf_tcp_sock, struct sock *, sk)
5822 if (sk_fullsock(sk) && sk->sk_protocol == IPPROTO_TCP)
5823 return (unsigned long)sk;
5825 return (unsigned long)NULL;
5828 const struct bpf_func_proto bpf_tcp_sock_proto = {
5829 .func = bpf_tcp_sock,
5831 .ret_type = RET_PTR_TO_TCP_SOCK_OR_NULL,
5832 .arg1_type = ARG_PTR_TO_SOCK_COMMON,
5835 BPF_CALL_1(bpf_get_listener_sock, struct sock *, sk)
5837 sk = sk_to_full_sk(sk);
5839 if (sk->sk_state == TCP_LISTEN && sock_flag(sk, SOCK_RCU_FREE))
5840 return (unsigned long)sk;
5842 return (unsigned long)NULL;
5845 static const struct bpf_func_proto bpf_get_listener_sock_proto = {
5846 .func = bpf_get_listener_sock,
5848 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
5849 .arg1_type = ARG_PTR_TO_SOCK_COMMON,
5852 BPF_CALL_1(bpf_skb_ecn_set_ce, struct sk_buff *, skb)
5854 unsigned int iphdr_len;
5856 if (skb->protocol == cpu_to_be16(ETH_P_IP))
5857 iphdr_len = sizeof(struct iphdr);
5858 else if (skb->protocol == cpu_to_be16(ETH_P_IPV6))
5859 iphdr_len = sizeof(struct ipv6hdr);
5863 if (skb_headlen(skb) < iphdr_len)
5866 if (skb_cloned(skb) && !skb_clone_writable(skb, iphdr_len))
5869 return INET_ECN_set_ce(skb);
5872 bool bpf_xdp_sock_is_valid_access(int off, int size, enum bpf_access_type type,
5873 struct bpf_insn_access_aux *info)
5875 if (off < 0 || off >= offsetofend(struct bpf_xdp_sock, queue_id))
5878 if (off % size != 0)
5883 return size == sizeof(__u32);
5887 u32 bpf_xdp_sock_convert_ctx_access(enum bpf_access_type type,
5888 const struct bpf_insn *si,
5889 struct bpf_insn *insn_buf,
5890 struct bpf_prog *prog, u32 *target_size)
5892 struct bpf_insn *insn = insn_buf;
5894 #define BPF_XDP_SOCK_GET(FIELD) \
5896 BUILD_BUG_ON(sizeof_field(struct xdp_sock, FIELD) > \
5897 sizeof_field(struct bpf_xdp_sock, FIELD)); \
5898 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_sock, FIELD),\
5899 si->dst_reg, si->src_reg, \
5900 offsetof(struct xdp_sock, FIELD)); \
5904 case offsetof(struct bpf_xdp_sock, queue_id):
5905 BPF_XDP_SOCK_GET(queue_id);
5909 return insn - insn_buf;
5912 static const struct bpf_func_proto bpf_skb_ecn_set_ce_proto = {
5913 .func = bpf_skb_ecn_set_ce,
5915 .ret_type = RET_INTEGER,
5916 .arg1_type = ARG_PTR_TO_CTX,
5919 BPF_CALL_5(bpf_tcp_check_syncookie, struct sock *, sk, void *, iph, u32, iph_len,
5920 struct tcphdr *, th, u32, th_len)
5922 #ifdef CONFIG_SYN_COOKIES
5926 if (unlikely(th_len < sizeof(*th)))
5929 /* sk_listener() allows TCP_NEW_SYN_RECV, which makes no sense here. */
5930 if (sk->sk_protocol != IPPROTO_TCP || sk->sk_state != TCP_LISTEN)
5933 if (!sock_net(sk)->ipv4.sysctl_tcp_syncookies)
5936 if (!th->ack || th->rst || th->syn)
5939 if (tcp_synq_no_recent_overflow(sk))
5942 cookie = ntohl(th->ack_seq) - 1;
5944 switch (sk->sk_family) {
5946 if (unlikely(iph_len < sizeof(struct iphdr)))
5949 ret = __cookie_v4_check((struct iphdr *)iph, th, cookie);
5952 #if IS_BUILTIN(CONFIG_IPV6)
5954 if (unlikely(iph_len < sizeof(struct ipv6hdr)))
5957 ret = __cookie_v6_check((struct ipv6hdr *)iph, th, cookie);
5959 #endif /* CONFIG_IPV6 */
5962 return -EPROTONOSUPPORT;
5974 static const struct bpf_func_proto bpf_tcp_check_syncookie_proto = {
5975 .func = bpf_tcp_check_syncookie,
5978 .ret_type = RET_INTEGER,
5979 .arg1_type = ARG_PTR_TO_SOCK_COMMON,
5980 .arg2_type = ARG_PTR_TO_MEM,
5981 .arg3_type = ARG_CONST_SIZE,
5982 .arg4_type = ARG_PTR_TO_MEM,
5983 .arg5_type = ARG_CONST_SIZE,
5986 BPF_CALL_5(bpf_tcp_gen_syncookie, struct sock *, sk, void *, iph, u32, iph_len,
5987 struct tcphdr *, th, u32, th_len)
5989 #ifdef CONFIG_SYN_COOKIES
5993 if (unlikely(th_len < sizeof(*th) || th_len != th->doff * 4))
5996 if (sk->sk_protocol != IPPROTO_TCP || sk->sk_state != TCP_LISTEN)
5999 if (!sock_net(sk)->ipv4.sysctl_tcp_syncookies)
6002 if (!th->syn || th->ack || th->fin || th->rst)
6005 if (unlikely(iph_len < sizeof(struct iphdr)))
6008 /* Both struct iphdr and struct ipv6hdr have the version field at the
6009 * same offset so we can cast to the shorter header (struct iphdr).
6011 switch (((struct iphdr *)iph)->version) {
6013 if (sk->sk_family == AF_INET6 && sk->sk_ipv6only)
6016 mss = tcp_v4_get_syncookie(sk, iph, th, &cookie);
6019 #if IS_BUILTIN(CONFIG_IPV6)
6021 if (unlikely(iph_len < sizeof(struct ipv6hdr)))
6024 if (sk->sk_family != AF_INET6)
6027 mss = tcp_v6_get_syncookie(sk, iph, th, &cookie);
6029 #endif /* CONFIG_IPV6 */
6032 return -EPROTONOSUPPORT;
6037 return cookie | ((u64)mss << 32);
6040 #endif /* CONFIG_SYN_COOKIES */
6043 static const struct bpf_func_proto bpf_tcp_gen_syncookie_proto = {
6044 .func = bpf_tcp_gen_syncookie,
6045 .gpl_only = true, /* __cookie_v*_init_sequence() is GPL */
6047 .ret_type = RET_INTEGER,
6048 .arg1_type = ARG_PTR_TO_SOCK_COMMON,
6049 .arg2_type = ARG_PTR_TO_MEM,
6050 .arg3_type = ARG_CONST_SIZE,
6051 .arg4_type = ARG_PTR_TO_MEM,
6052 .arg5_type = ARG_CONST_SIZE,
6055 BPF_CALL_3(bpf_sk_assign, struct sk_buff *, skb, struct sock *, sk, u64, flags)
6059 if (!skb_at_tc_ingress(skb))
6061 if (unlikely(dev_net(skb->dev) != sock_net(sk)))
6062 return -ENETUNREACH;
6063 if (unlikely(sk_fullsock(sk) && sk->sk_reuseport))
6064 return -ESOCKTNOSUPPORT;
6065 if (sk_is_refcounted(sk) &&
6066 unlikely(!refcount_inc_not_zero(&sk->sk_refcnt)))
6071 skb->destructor = sock_pfree;
6076 static const struct bpf_func_proto bpf_sk_assign_proto = {
6077 .func = bpf_sk_assign,
6079 .ret_type = RET_INTEGER,
6080 .arg1_type = ARG_PTR_TO_CTX,
6081 .arg2_type = ARG_PTR_TO_SOCK_COMMON,
6082 .arg3_type = ARG_ANYTHING,
6085 #endif /* CONFIG_INET */
6087 bool bpf_helper_changes_pkt_data(void *func)
6089 if (func == bpf_skb_vlan_push ||
6090 func == bpf_skb_vlan_pop ||
6091 func == bpf_skb_store_bytes ||
6092 func == bpf_skb_change_proto ||
6093 func == bpf_skb_change_head ||
6094 func == sk_skb_change_head ||
6095 func == bpf_skb_change_tail ||
6096 func == sk_skb_change_tail ||
6097 func == bpf_skb_adjust_room ||
6098 func == bpf_skb_pull_data ||
6099 func == sk_skb_pull_data ||
6100 func == bpf_clone_redirect ||
6101 func == bpf_l3_csum_replace ||
6102 func == bpf_l4_csum_replace ||
6103 func == bpf_xdp_adjust_head ||
6104 func == bpf_xdp_adjust_meta ||
6105 func == bpf_msg_pull_data ||
6106 func == bpf_msg_push_data ||
6107 func == bpf_msg_pop_data ||
6108 func == bpf_xdp_adjust_tail ||
6109 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
6110 func == bpf_lwt_seg6_store_bytes ||
6111 func == bpf_lwt_seg6_adjust_srh ||
6112 func == bpf_lwt_seg6_action ||
6114 func == bpf_lwt_in_push_encap ||
6115 func == bpf_lwt_xmit_push_encap)
6121 const struct bpf_func_proto bpf_event_output_data_proto __weak;
6123 static const struct bpf_func_proto *
6124 sock_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6127 /* inet and inet6 sockets are created in a process
6128 * context so there is always a valid uid/gid
6130 case BPF_FUNC_get_current_uid_gid:
6131 return &bpf_get_current_uid_gid_proto;
6132 case BPF_FUNC_get_local_storage:
6133 return &bpf_get_local_storage_proto;
6134 case BPF_FUNC_get_socket_cookie:
6135 return &bpf_get_socket_cookie_sock_proto;
6136 case BPF_FUNC_get_netns_cookie:
6137 return &bpf_get_netns_cookie_sock_proto;
6138 case BPF_FUNC_perf_event_output:
6139 return &bpf_event_output_data_proto;
6140 case BPF_FUNC_get_current_pid_tgid:
6141 return &bpf_get_current_pid_tgid_proto;
6142 case BPF_FUNC_get_current_comm:
6143 return &bpf_get_current_comm_proto;
6144 #ifdef CONFIG_CGROUPS
6145 case BPF_FUNC_get_current_cgroup_id:
6146 return &bpf_get_current_cgroup_id_proto;
6147 case BPF_FUNC_get_current_ancestor_cgroup_id:
6148 return &bpf_get_current_ancestor_cgroup_id_proto;
6150 #ifdef CONFIG_CGROUP_NET_CLASSID
6151 case BPF_FUNC_get_cgroup_classid:
6152 return &bpf_get_cgroup_classid_curr_proto;
6155 return bpf_base_func_proto(func_id);
6159 static const struct bpf_func_proto *
6160 sock_addr_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6163 /* inet and inet6 sockets are created in a process
6164 * context so there is always a valid uid/gid
6166 case BPF_FUNC_get_current_uid_gid:
6167 return &bpf_get_current_uid_gid_proto;
6169 switch (prog->expected_attach_type) {
6170 case BPF_CGROUP_INET4_CONNECT:
6171 case BPF_CGROUP_INET6_CONNECT:
6172 return &bpf_bind_proto;
6176 case BPF_FUNC_get_socket_cookie:
6177 return &bpf_get_socket_cookie_sock_addr_proto;
6178 case BPF_FUNC_get_netns_cookie:
6179 return &bpf_get_netns_cookie_sock_addr_proto;
6180 case BPF_FUNC_get_local_storage:
6181 return &bpf_get_local_storage_proto;
6182 case BPF_FUNC_perf_event_output:
6183 return &bpf_event_output_data_proto;
6184 case BPF_FUNC_get_current_pid_tgid:
6185 return &bpf_get_current_pid_tgid_proto;
6186 case BPF_FUNC_get_current_comm:
6187 return &bpf_get_current_comm_proto;
6188 #ifdef CONFIG_CGROUPS
6189 case BPF_FUNC_get_current_cgroup_id:
6190 return &bpf_get_current_cgroup_id_proto;
6191 case BPF_FUNC_get_current_ancestor_cgroup_id:
6192 return &bpf_get_current_ancestor_cgroup_id_proto;
6194 #ifdef CONFIG_CGROUP_NET_CLASSID
6195 case BPF_FUNC_get_cgroup_classid:
6196 return &bpf_get_cgroup_classid_curr_proto;
6199 case BPF_FUNC_sk_lookup_tcp:
6200 return &bpf_sock_addr_sk_lookup_tcp_proto;
6201 case BPF_FUNC_sk_lookup_udp:
6202 return &bpf_sock_addr_sk_lookup_udp_proto;
6203 case BPF_FUNC_sk_release:
6204 return &bpf_sk_release_proto;
6205 case BPF_FUNC_skc_lookup_tcp:
6206 return &bpf_sock_addr_skc_lookup_tcp_proto;
6207 #endif /* CONFIG_INET */
6208 case BPF_FUNC_sk_storage_get:
6209 return &bpf_sk_storage_get_proto;
6210 case BPF_FUNC_sk_storage_delete:
6211 return &bpf_sk_storage_delete_proto;
6212 case BPF_FUNC_setsockopt:
6213 switch (prog->expected_attach_type) {
6214 case BPF_CGROUP_INET4_CONNECT:
6215 case BPF_CGROUP_INET6_CONNECT:
6216 return &bpf_sock_addr_setsockopt_proto;
6220 case BPF_FUNC_getsockopt:
6221 switch (prog->expected_attach_type) {
6222 case BPF_CGROUP_INET4_CONNECT:
6223 case BPF_CGROUP_INET6_CONNECT:
6224 return &bpf_sock_addr_getsockopt_proto;
6229 return bpf_base_func_proto(func_id);
6233 static const struct bpf_func_proto *
6234 sk_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6237 case BPF_FUNC_skb_load_bytes:
6238 return &bpf_skb_load_bytes_proto;
6239 case BPF_FUNC_skb_load_bytes_relative:
6240 return &bpf_skb_load_bytes_relative_proto;
6241 case BPF_FUNC_get_socket_cookie:
6242 return &bpf_get_socket_cookie_proto;
6243 case BPF_FUNC_get_socket_uid:
6244 return &bpf_get_socket_uid_proto;
6245 case BPF_FUNC_perf_event_output:
6246 return &bpf_skb_event_output_proto;
6248 return bpf_base_func_proto(func_id);
6252 const struct bpf_func_proto bpf_sk_storage_get_proto __weak;
6253 const struct bpf_func_proto bpf_sk_storage_delete_proto __weak;
6255 static const struct bpf_func_proto *
6256 cg_skb_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6259 case BPF_FUNC_get_local_storage:
6260 return &bpf_get_local_storage_proto;
6261 case BPF_FUNC_sk_fullsock:
6262 return &bpf_sk_fullsock_proto;
6263 case BPF_FUNC_sk_storage_get:
6264 return &bpf_sk_storage_get_proto;
6265 case BPF_FUNC_sk_storage_delete:
6266 return &bpf_sk_storage_delete_proto;
6267 case BPF_FUNC_perf_event_output:
6268 return &bpf_skb_event_output_proto;
6269 #ifdef CONFIG_SOCK_CGROUP_DATA
6270 case BPF_FUNC_skb_cgroup_id:
6271 return &bpf_skb_cgroup_id_proto;
6272 case BPF_FUNC_skb_ancestor_cgroup_id:
6273 return &bpf_skb_ancestor_cgroup_id_proto;
6274 case BPF_FUNC_sk_cgroup_id:
6275 return &bpf_sk_cgroup_id_proto;
6276 case BPF_FUNC_sk_ancestor_cgroup_id:
6277 return &bpf_sk_ancestor_cgroup_id_proto;
6280 case BPF_FUNC_sk_lookup_tcp:
6281 return &bpf_sk_lookup_tcp_proto;
6282 case BPF_FUNC_sk_lookup_udp:
6283 return &bpf_sk_lookup_udp_proto;
6284 case BPF_FUNC_sk_release:
6285 return &bpf_sk_release_proto;
6286 case BPF_FUNC_skc_lookup_tcp:
6287 return &bpf_skc_lookup_tcp_proto;
6288 case BPF_FUNC_tcp_sock:
6289 return &bpf_tcp_sock_proto;
6290 case BPF_FUNC_get_listener_sock:
6291 return &bpf_get_listener_sock_proto;
6292 case BPF_FUNC_skb_ecn_set_ce:
6293 return &bpf_skb_ecn_set_ce_proto;
6296 return sk_filter_func_proto(func_id, prog);
6300 static const struct bpf_func_proto *
6301 tc_cls_act_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6304 case BPF_FUNC_skb_store_bytes:
6305 return &bpf_skb_store_bytes_proto;
6306 case BPF_FUNC_skb_load_bytes:
6307 return &bpf_skb_load_bytes_proto;
6308 case BPF_FUNC_skb_load_bytes_relative:
6309 return &bpf_skb_load_bytes_relative_proto;
6310 case BPF_FUNC_skb_pull_data:
6311 return &bpf_skb_pull_data_proto;
6312 case BPF_FUNC_csum_diff:
6313 return &bpf_csum_diff_proto;
6314 case BPF_FUNC_csum_update:
6315 return &bpf_csum_update_proto;
6316 case BPF_FUNC_csum_level:
6317 return &bpf_csum_level_proto;
6318 case BPF_FUNC_l3_csum_replace:
6319 return &bpf_l3_csum_replace_proto;
6320 case BPF_FUNC_l4_csum_replace:
6321 return &bpf_l4_csum_replace_proto;
6322 case BPF_FUNC_clone_redirect:
6323 return &bpf_clone_redirect_proto;
6324 case BPF_FUNC_get_cgroup_classid:
6325 return &bpf_get_cgroup_classid_proto;
6326 case BPF_FUNC_skb_vlan_push:
6327 return &bpf_skb_vlan_push_proto;
6328 case BPF_FUNC_skb_vlan_pop:
6329 return &bpf_skb_vlan_pop_proto;
6330 case BPF_FUNC_skb_change_proto:
6331 return &bpf_skb_change_proto_proto;
6332 case BPF_FUNC_skb_change_type:
6333 return &bpf_skb_change_type_proto;
6334 case BPF_FUNC_skb_adjust_room:
6335 return &bpf_skb_adjust_room_proto;
6336 case BPF_FUNC_skb_change_tail:
6337 return &bpf_skb_change_tail_proto;
6338 case BPF_FUNC_skb_change_head:
6339 return &bpf_skb_change_head_proto;
6340 case BPF_FUNC_skb_get_tunnel_key:
6341 return &bpf_skb_get_tunnel_key_proto;
6342 case BPF_FUNC_skb_set_tunnel_key:
6343 return bpf_get_skb_set_tunnel_proto(func_id);
6344 case BPF_FUNC_skb_get_tunnel_opt:
6345 return &bpf_skb_get_tunnel_opt_proto;
6346 case BPF_FUNC_skb_set_tunnel_opt:
6347 return bpf_get_skb_set_tunnel_proto(func_id);
6348 case BPF_FUNC_redirect:
6349 return &bpf_redirect_proto;
6350 case BPF_FUNC_get_route_realm:
6351 return &bpf_get_route_realm_proto;
6352 case BPF_FUNC_get_hash_recalc:
6353 return &bpf_get_hash_recalc_proto;
6354 case BPF_FUNC_set_hash_invalid:
6355 return &bpf_set_hash_invalid_proto;
6356 case BPF_FUNC_set_hash:
6357 return &bpf_set_hash_proto;
6358 case BPF_FUNC_perf_event_output:
6359 return &bpf_skb_event_output_proto;
6360 case BPF_FUNC_get_smp_processor_id:
6361 return &bpf_get_smp_processor_id_proto;
6362 case BPF_FUNC_skb_under_cgroup:
6363 return &bpf_skb_under_cgroup_proto;
6364 case BPF_FUNC_get_socket_cookie:
6365 return &bpf_get_socket_cookie_proto;
6366 case BPF_FUNC_get_socket_uid:
6367 return &bpf_get_socket_uid_proto;
6368 case BPF_FUNC_fib_lookup:
6369 return &bpf_skb_fib_lookup_proto;
6370 case BPF_FUNC_sk_fullsock:
6371 return &bpf_sk_fullsock_proto;
6372 case BPF_FUNC_sk_storage_get:
6373 return &bpf_sk_storage_get_proto;
6374 case BPF_FUNC_sk_storage_delete:
6375 return &bpf_sk_storage_delete_proto;
6377 case BPF_FUNC_skb_get_xfrm_state:
6378 return &bpf_skb_get_xfrm_state_proto;
6380 #ifdef CONFIG_SOCK_CGROUP_DATA
6381 case BPF_FUNC_skb_cgroup_id:
6382 return &bpf_skb_cgroup_id_proto;
6383 case BPF_FUNC_skb_ancestor_cgroup_id:
6384 return &bpf_skb_ancestor_cgroup_id_proto;
6387 case BPF_FUNC_sk_lookup_tcp:
6388 return &bpf_sk_lookup_tcp_proto;
6389 case BPF_FUNC_sk_lookup_udp:
6390 return &bpf_sk_lookup_udp_proto;
6391 case BPF_FUNC_sk_release:
6392 return &bpf_sk_release_proto;
6393 case BPF_FUNC_tcp_sock:
6394 return &bpf_tcp_sock_proto;
6395 case BPF_FUNC_get_listener_sock:
6396 return &bpf_get_listener_sock_proto;
6397 case BPF_FUNC_skc_lookup_tcp:
6398 return &bpf_skc_lookup_tcp_proto;
6399 case BPF_FUNC_tcp_check_syncookie:
6400 return &bpf_tcp_check_syncookie_proto;
6401 case BPF_FUNC_skb_ecn_set_ce:
6402 return &bpf_skb_ecn_set_ce_proto;
6403 case BPF_FUNC_tcp_gen_syncookie:
6404 return &bpf_tcp_gen_syncookie_proto;
6405 case BPF_FUNC_sk_assign:
6406 return &bpf_sk_assign_proto;
6409 return bpf_base_func_proto(func_id);
6413 static const struct bpf_func_proto *
6414 xdp_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6417 case BPF_FUNC_perf_event_output:
6418 return &bpf_xdp_event_output_proto;
6419 case BPF_FUNC_get_smp_processor_id:
6420 return &bpf_get_smp_processor_id_proto;
6421 case BPF_FUNC_csum_diff:
6422 return &bpf_csum_diff_proto;
6423 case BPF_FUNC_xdp_adjust_head:
6424 return &bpf_xdp_adjust_head_proto;
6425 case BPF_FUNC_xdp_adjust_meta:
6426 return &bpf_xdp_adjust_meta_proto;
6427 case BPF_FUNC_redirect:
6428 return &bpf_xdp_redirect_proto;
6429 case BPF_FUNC_redirect_map:
6430 return &bpf_xdp_redirect_map_proto;
6431 case BPF_FUNC_xdp_adjust_tail:
6432 return &bpf_xdp_adjust_tail_proto;
6433 case BPF_FUNC_fib_lookup:
6434 return &bpf_xdp_fib_lookup_proto;
6436 case BPF_FUNC_sk_lookup_udp:
6437 return &bpf_xdp_sk_lookup_udp_proto;
6438 case BPF_FUNC_sk_lookup_tcp:
6439 return &bpf_xdp_sk_lookup_tcp_proto;
6440 case BPF_FUNC_sk_release:
6441 return &bpf_sk_release_proto;
6442 case BPF_FUNC_skc_lookup_tcp:
6443 return &bpf_xdp_skc_lookup_tcp_proto;
6444 case BPF_FUNC_tcp_check_syncookie:
6445 return &bpf_tcp_check_syncookie_proto;
6446 case BPF_FUNC_tcp_gen_syncookie:
6447 return &bpf_tcp_gen_syncookie_proto;
6450 return bpf_base_func_proto(func_id);
6454 const struct bpf_func_proto bpf_sock_map_update_proto __weak;
6455 const struct bpf_func_proto bpf_sock_hash_update_proto __weak;
6457 static const struct bpf_func_proto *
6458 sock_ops_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6461 case BPF_FUNC_setsockopt:
6462 return &bpf_sock_ops_setsockopt_proto;
6463 case BPF_FUNC_getsockopt:
6464 return &bpf_sock_ops_getsockopt_proto;
6465 case BPF_FUNC_sock_ops_cb_flags_set:
6466 return &bpf_sock_ops_cb_flags_set_proto;
6467 case BPF_FUNC_sock_map_update:
6468 return &bpf_sock_map_update_proto;
6469 case BPF_FUNC_sock_hash_update:
6470 return &bpf_sock_hash_update_proto;
6471 case BPF_FUNC_get_socket_cookie:
6472 return &bpf_get_socket_cookie_sock_ops_proto;
6473 case BPF_FUNC_get_local_storage:
6474 return &bpf_get_local_storage_proto;
6475 case BPF_FUNC_perf_event_output:
6476 return &bpf_event_output_data_proto;
6477 case BPF_FUNC_sk_storage_get:
6478 return &bpf_sk_storage_get_proto;
6479 case BPF_FUNC_sk_storage_delete:
6480 return &bpf_sk_storage_delete_proto;
6482 case BPF_FUNC_tcp_sock:
6483 return &bpf_tcp_sock_proto;
6484 #endif /* CONFIG_INET */
6486 return bpf_base_func_proto(func_id);
6490 const struct bpf_func_proto bpf_msg_redirect_map_proto __weak;
6491 const struct bpf_func_proto bpf_msg_redirect_hash_proto __weak;
6493 static const struct bpf_func_proto *
6494 sk_msg_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6497 case BPF_FUNC_msg_redirect_map:
6498 return &bpf_msg_redirect_map_proto;
6499 case BPF_FUNC_msg_redirect_hash:
6500 return &bpf_msg_redirect_hash_proto;
6501 case BPF_FUNC_msg_apply_bytes:
6502 return &bpf_msg_apply_bytes_proto;
6503 case BPF_FUNC_msg_cork_bytes:
6504 return &bpf_msg_cork_bytes_proto;
6505 case BPF_FUNC_msg_pull_data:
6506 return &bpf_msg_pull_data_proto;
6507 case BPF_FUNC_msg_push_data:
6508 return &bpf_msg_push_data_proto;
6509 case BPF_FUNC_msg_pop_data:
6510 return &bpf_msg_pop_data_proto;
6511 case BPF_FUNC_perf_event_output:
6512 return &bpf_event_output_data_proto;
6513 case BPF_FUNC_get_current_uid_gid:
6514 return &bpf_get_current_uid_gid_proto;
6515 case BPF_FUNC_get_current_pid_tgid:
6516 return &bpf_get_current_pid_tgid_proto;
6517 case BPF_FUNC_sk_storage_get:
6518 return &bpf_sk_storage_get_proto;
6519 case BPF_FUNC_sk_storage_delete:
6520 return &bpf_sk_storage_delete_proto;
6521 #ifdef CONFIG_CGROUPS
6522 case BPF_FUNC_get_current_cgroup_id:
6523 return &bpf_get_current_cgroup_id_proto;
6524 case BPF_FUNC_get_current_ancestor_cgroup_id:
6525 return &bpf_get_current_ancestor_cgroup_id_proto;
6527 #ifdef CONFIG_CGROUP_NET_CLASSID
6528 case BPF_FUNC_get_cgroup_classid:
6529 return &bpf_get_cgroup_classid_curr_proto;
6532 return bpf_base_func_proto(func_id);
6536 const struct bpf_func_proto bpf_sk_redirect_map_proto __weak;
6537 const struct bpf_func_proto bpf_sk_redirect_hash_proto __weak;
6539 static const struct bpf_func_proto *
6540 sk_skb_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6543 case BPF_FUNC_skb_store_bytes:
6544 return &bpf_skb_store_bytes_proto;
6545 case BPF_FUNC_skb_load_bytes:
6546 return &bpf_skb_load_bytes_proto;
6547 case BPF_FUNC_skb_pull_data:
6548 return &sk_skb_pull_data_proto;
6549 case BPF_FUNC_skb_change_tail:
6550 return &sk_skb_change_tail_proto;
6551 case BPF_FUNC_skb_change_head:
6552 return &sk_skb_change_head_proto;
6553 case BPF_FUNC_get_socket_cookie:
6554 return &bpf_get_socket_cookie_proto;
6555 case BPF_FUNC_get_socket_uid:
6556 return &bpf_get_socket_uid_proto;
6557 case BPF_FUNC_sk_redirect_map:
6558 return &bpf_sk_redirect_map_proto;
6559 case BPF_FUNC_sk_redirect_hash:
6560 return &bpf_sk_redirect_hash_proto;
6561 case BPF_FUNC_perf_event_output:
6562 return &bpf_skb_event_output_proto;
6564 case BPF_FUNC_sk_lookup_tcp:
6565 return &bpf_sk_lookup_tcp_proto;
6566 case BPF_FUNC_sk_lookup_udp:
6567 return &bpf_sk_lookup_udp_proto;
6568 case BPF_FUNC_sk_release:
6569 return &bpf_sk_release_proto;
6570 case BPF_FUNC_skc_lookup_tcp:
6571 return &bpf_skc_lookup_tcp_proto;
6574 return bpf_base_func_proto(func_id);
6578 static const struct bpf_func_proto *
6579 flow_dissector_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6582 case BPF_FUNC_skb_load_bytes:
6583 return &bpf_flow_dissector_load_bytes_proto;
6585 return bpf_base_func_proto(func_id);
6589 static const struct bpf_func_proto *
6590 lwt_out_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6593 case BPF_FUNC_skb_load_bytes:
6594 return &bpf_skb_load_bytes_proto;
6595 case BPF_FUNC_skb_pull_data:
6596 return &bpf_skb_pull_data_proto;
6597 case BPF_FUNC_csum_diff:
6598 return &bpf_csum_diff_proto;
6599 case BPF_FUNC_get_cgroup_classid:
6600 return &bpf_get_cgroup_classid_proto;
6601 case BPF_FUNC_get_route_realm:
6602 return &bpf_get_route_realm_proto;
6603 case BPF_FUNC_get_hash_recalc:
6604 return &bpf_get_hash_recalc_proto;
6605 case BPF_FUNC_perf_event_output:
6606 return &bpf_skb_event_output_proto;
6607 case BPF_FUNC_get_smp_processor_id:
6608 return &bpf_get_smp_processor_id_proto;
6609 case BPF_FUNC_skb_under_cgroup:
6610 return &bpf_skb_under_cgroup_proto;
6612 return bpf_base_func_proto(func_id);
6616 static const struct bpf_func_proto *
6617 lwt_in_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6620 case BPF_FUNC_lwt_push_encap:
6621 return &bpf_lwt_in_push_encap_proto;
6623 return lwt_out_func_proto(func_id, prog);
6627 static const struct bpf_func_proto *
6628 lwt_xmit_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6631 case BPF_FUNC_skb_get_tunnel_key:
6632 return &bpf_skb_get_tunnel_key_proto;
6633 case BPF_FUNC_skb_set_tunnel_key:
6634 return bpf_get_skb_set_tunnel_proto(func_id);
6635 case BPF_FUNC_skb_get_tunnel_opt:
6636 return &bpf_skb_get_tunnel_opt_proto;
6637 case BPF_FUNC_skb_set_tunnel_opt:
6638 return bpf_get_skb_set_tunnel_proto(func_id);
6639 case BPF_FUNC_redirect:
6640 return &bpf_redirect_proto;
6641 case BPF_FUNC_clone_redirect:
6642 return &bpf_clone_redirect_proto;
6643 case BPF_FUNC_skb_change_tail:
6644 return &bpf_skb_change_tail_proto;
6645 case BPF_FUNC_skb_change_head:
6646 return &bpf_skb_change_head_proto;
6647 case BPF_FUNC_skb_store_bytes:
6648 return &bpf_skb_store_bytes_proto;
6649 case BPF_FUNC_csum_update:
6650 return &bpf_csum_update_proto;
6651 case BPF_FUNC_csum_level:
6652 return &bpf_csum_level_proto;
6653 case BPF_FUNC_l3_csum_replace:
6654 return &bpf_l3_csum_replace_proto;
6655 case BPF_FUNC_l4_csum_replace:
6656 return &bpf_l4_csum_replace_proto;
6657 case BPF_FUNC_set_hash_invalid:
6658 return &bpf_set_hash_invalid_proto;
6659 case BPF_FUNC_lwt_push_encap:
6660 return &bpf_lwt_xmit_push_encap_proto;
6662 return lwt_out_func_proto(func_id, prog);
6666 static const struct bpf_func_proto *
6667 lwt_seg6local_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6670 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
6671 case BPF_FUNC_lwt_seg6_store_bytes:
6672 return &bpf_lwt_seg6_store_bytes_proto;
6673 case BPF_FUNC_lwt_seg6_action:
6674 return &bpf_lwt_seg6_action_proto;
6675 case BPF_FUNC_lwt_seg6_adjust_srh:
6676 return &bpf_lwt_seg6_adjust_srh_proto;
6679 return lwt_out_func_proto(func_id, prog);
6683 static bool bpf_skb_is_valid_access(int off, int size, enum bpf_access_type type,
6684 const struct bpf_prog *prog,
6685 struct bpf_insn_access_aux *info)
6687 const int size_default = sizeof(__u32);
6689 if (off < 0 || off >= sizeof(struct __sk_buff))
6692 /* The verifier guarantees that size > 0. */
6693 if (off % size != 0)
6697 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
6698 if (off + size > offsetofend(struct __sk_buff, cb[4]))
6701 case bpf_ctx_range_till(struct __sk_buff, remote_ip6[0], remote_ip6[3]):
6702 case bpf_ctx_range_till(struct __sk_buff, local_ip6[0], local_ip6[3]):
6703 case bpf_ctx_range_till(struct __sk_buff, remote_ip4, remote_ip4):
6704 case bpf_ctx_range_till(struct __sk_buff, local_ip4, local_ip4):
6705 case bpf_ctx_range(struct __sk_buff, data):
6706 case bpf_ctx_range(struct __sk_buff, data_meta):
6707 case bpf_ctx_range(struct __sk_buff, data_end):
6708 if (size != size_default)
6711 case bpf_ctx_range_ptr(struct __sk_buff, flow_keys):
6713 case bpf_ctx_range(struct __sk_buff, tstamp):
6714 if (size != sizeof(__u64))
6717 case offsetof(struct __sk_buff, sk):
6718 if (type == BPF_WRITE || size != sizeof(__u64))
6720 info->reg_type = PTR_TO_SOCK_COMMON_OR_NULL;
6723 /* Only narrow read access allowed for now. */
6724 if (type == BPF_WRITE) {
6725 if (size != size_default)
6728 bpf_ctx_record_field_size(info, size_default);
6729 if (!bpf_ctx_narrow_access_ok(off, size, size_default))
6737 static bool sk_filter_is_valid_access(int off, int size,
6738 enum bpf_access_type type,
6739 const struct bpf_prog *prog,
6740 struct bpf_insn_access_aux *info)
6743 case bpf_ctx_range(struct __sk_buff, tc_classid):
6744 case bpf_ctx_range(struct __sk_buff, data):
6745 case bpf_ctx_range(struct __sk_buff, data_meta):
6746 case bpf_ctx_range(struct __sk_buff, data_end):
6747 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
6748 case bpf_ctx_range(struct __sk_buff, tstamp):
6749 case bpf_ctx_range(struct __sk_buff, wire_len):
6753 if (type == BPF_WRITE) {
6755 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
6762 return bpf_skb_is_valid_access(off, size, type, prog, info);
6765 static bool cg_skb_is_valid_access(int off, int size,
6766 enum bpf_access_type type,
6767 const struct bpf_prog *prog,
6768 struct bpf_insn_access_aux *info)
6771 case bpf_ctx_range(struct __sk_buff, tc_classid):
6772 case bpf_ctx_range(struct __sk_buff, data_meta):
6773 case bpf_ctx_range(struct __sk_buff, wire_len):
6775 case bpf_ctx_range(struct __sk_buff, data):
6776 case bpf_ctx_range(struct __sk_buff, data_end):
6782 if (type == BPF_WRITE) {
6784 case bpf_ctx_range(struct __sk_buff, mark):
6785 case bpf_ctx_range(struct __sk_buff, priority):
6786 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
6788 case bpf_ctx_range(struct __sk_buff, tstamp):
6798 case bpf_ctx_range(struct __sk_buff, data):
6799 info->reg_type = PTR_TO_PACKET;
6801 case bpf_ctx_range(struct __sk_buff, data_end):
6802 info->reg_type = PTR_TO_PACKET_END;
6806 return bpf_skb_is_valid_access(off, size, type, prog, info);
6809 static bool lwt_is_valid_access(int off, int size,
6810 enum bpf_access_type type,
6811 const struct bpf_prog *prog,
6812 struct bpf_insn_access_aux *info)
6815 case bpf_ctx_range(struct __sk_buff, tc_classid):
6816 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
6817 case bpf_ctx_range(struct __sk_buff, data_meta):
6818 case bpf_ctx_range(struct __sk_buff, tstamp):
6819 case bpf_ctx_range(struct __sk_buff, wire_len):
6823 if (type == BPF_WRITE) {
6825 case bpf_ctx_range(struct __sk_buff, mark):
6826 case bpf_ctx_range(struct __sk_buff, priority):
6827 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
6835 case bpf_ctx_range(struct __sk_buff, data):
6836 info->reg_type = PTR_TO_PACKET;
6838 case bpf_ctx_range(struct __sk_buff, data_end):
6839 info->reg_type = PTR_TO_PACKET_END;
6843 return bpf_skb_is_valid_access(off, size, type, prog, info);
6846 /* Attach type specific accesses */
6847 static bool __sock_filter_check_attach_type(int off,
6848 enum bpf_access_type access_type,
6849 enum bpf_attach_type attach_type)
6852 case offsetof(struct bpf_sock, bound_dev_if):
6853 case offsetof(struct bpf_sock, mark):
6854 case offsetof(struct bpf_sock, priority):
6855 switch (attach_type) {
6856 case BPF_CGROUP_INET_SOCK_CREATE:
6861 case bpf_ctx_range(struct bpf_sock, src_ip4):
6862 switch (attach_type) {
6863 case BPF_CGROUP_INET4_POST_BIND:
6868 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
6869 switch (attach_type) {
6870 case BPF_CGROUP_INET6_POST_BIND:
6875 case bpf_ctx_range(struct bpf_sock, src_port):
6876 switch (attach_type) {
6877 case BPF_CGROUP_INET4_POST_BIND:
6878 case BPF_CGROUP_INET6_POST_BIND:
6885 return access_type == BPF_READ;
6890 bool bpf_sock_common_is_valid_access(int off, int size,
6891 enum bpf_access_type type,
6892 struct bpf_insn_access_aux *info)
6895 case bpf_ctx_range_till(struct bpf_sock, type, priority):
6898 return bpf_sock_is_valid_access(off, size, type, info);
6902 bool bpf_sock_is_valid_access(int off, int size, enum bpf_access_type type,
6903 struct bpf_insn_access_aux *info)
6905 const int size_default = sizeof(__u32);
6907 if (off < 0 || off >= sizeof(struct bpf_sock))
6909 if (off % size != 0)
6913 case offsetof(struct bpf_sock, state):
6914 case offsetof(struct bpf_sock, family):
6915 case offsetof(struct bpf_sock, type):
6916 case offsetof(struct bpf_sock, protocol):
6917 case offsetof(struct bpf_sock, dst_port):
6918 case offsetof(struct bpf_sock, src_port):
6919 case offsetof(struct bpf_sock, rx_queue_mapping):
6920 case bpf_ctx_range(struct bpf_sock, src_ip4):
6921 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
6922 case bpf_ctx_range(struct bpf_sock, dst_ip4):
6923 case bpf_ctx_range_till(struct bpf_sock, dst_ip6[0], dst_ip6[3]):
6924 bpf_ctx_record_field_size(info, size_default);
6925 return bpf_ctx_narrow_access_ok(off, size, size_default);
6928 return size == size_default;
6931 static bool sock_filter_is_valid_access(int off, int size,
6932 enum bpf_access_type type,
6933 const struct bpf_prog *prog,
6934 struct bpf_insn_access_aux *info)
6936 if (!bpf_sock_is_valid_access(off, size, type, info))
6938 return __sock_filter_check_attach_type(off, type,
6939 prog->expected_attach_type);
6942 static int bpf_noop_prologue(struct bpf_insn *insn_buf, bool direct_write,
6943 const struct bpf_prog *prog)
6945 /* Neither direct read nor direct write requires any preliminary
6951 static int bpf_unclone_prologue(struct bpf_insn *insn_buf, bool direct_write,
6952 const struct bpf_prog *prog, int drop_verdict)
6954 struct bpf_insn *insn = insn_buf;
6959 /* if (!skb->cloned)
6962 * (Fast-path, otherwise approximation that we might be
6963 * a clone, do the rest in helper.)
6965 *insn++ = BPF_LDX_MEM(BPF_B, BPF_REG_6, BPF_REG_1, CLONED_OFFSET());
6966 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_6, CLONED_MASK);
6967 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_6, 0, 7);
6969 /* ret = bpf_skb_pull_data(skb, 0); */
6970 *insn++ = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1);
6971 *insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_2, BPF_REG_2);
6972 *insn++ = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
6973 BPF_FUNC_skb_pull_data);
6976 * return TC_ACT_SHOT;
6978 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2);
6979 *insn++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_0, drop_verdict);
6980 *insn++ = BPF_EXIT_INSN();
6983 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_6);
6985 *insn++ = prog->insnsi[0];
6987 return insn - insn_buf;
6990 static int bpf_gen_ld_abs(const struct bpf_insn *orig,
6991 struct bpf_insn *insn_buf)
6993 bool indirect = BPF_MODE(orig->code) == BPF_IND;
6994 struct bpf_insn *insn = insn_buf;
6996 /* We're guaranteed here that CTX is in R6. */
6997 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_CTX);
6999 *insn++ = BPF_MOV64_IMM(BPF_REG_2, orig->imm);
7001 *insn++ = BPF_MOV64_REG(BPF_REG_2, orig->src_reg);
7003 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, orig->imm);
7006 switch (BPF_SIZE(orig->code)) {
7008 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_8_no_cache);
7011 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_16_no_cache);
7014 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_32_no_cache);
7018 *insn++ = BPF_JMP_IMM(BPF_JSGE, BPF_REG_0, 0, 2);
7019 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_0, BPF_REG_0);
7020 *insn++ = BPF_EXIT_INSN();
7022 return insn - insn_buf;
7025 static int tc_cls_act_prologue(struct bpf_insn *insn_buf, bool direct_write,
7026 const struct bpf_prog *prog)
7028 return bpf_unclone_prologue(insn_buf, direct_write, prog, TC_ACT_SHOT);
7031 static bool tc_cls_act_is_valid_access(int off, int size,
7032 enum bpf_access_type type,
7033 const struct bpf_prog *prog,
7034 struct bpf_insn_access_aux *info)
7036 if (type == BPF_WRITE) {
7038 case bpf_ctx_range(struct __sk_buff, mark):
7039 case bpf_ctx_range(struct __sk_buff, tc_index):
7040 case bpf_ctx_range(struct __sk_buff, priority):
7041 case bpf_ctx_range(struct __sk_buff, tc_classid):
7042 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
7043 case bpf_ctx_range(struct __sk_buff, tstamp):
7044 case bpf_ctx_range(struct __sk_buff, queue_mapping):
7052 case bpf_ctx_range(struct __sk_buff, data):
7053 info->reg_type = PTR_TO_PACKET;
7055 case bpf_ctx_range(struct __sk_buff, data_meta):
7056 info->reg_type = PTR_TO_PACKET_META;
7058 case bpf_ctx_range(struct __sk_buff, data_end):
7059 info->reg_type = PTR_TO_PACKET_END;
7061 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
7065 return bpf_skb_is_valid_access(off, size, type, prog, info);
7068 static bool __is_valid_xdp_access(int off, int size)
7070 if (off < 0 || off >= sizeof(struct xdp_md))
7072 if (off % size != 0)
7074 if (size != sizeof(__u32))
7080 static bool xdp_is_valid_access(int off, int size,
7081 enum bpf_access_type type,
7082 const struct bpf_prog *prog,
7083 struct bpf_insn_access_aux *info)
7085 if (prog->expected_attach_type != BPF_XDP_DEVMAP) {
7087 case offsetof(struct xdp_md, egress_ifindex):
7092 if (type == BPF_WRITE) {
7093 if (bpf_prog_is_dev_bound(prog->aux)) {
7095 case offsetof(struct xdp_md, rx_queue_index):
7096 return __is_valid_xdp_access(off, size);
7103 case offsetof(struct xdp_md, data):
7104 info->reg_type = PTR_TO_PACKET;
7106 case offsetof(struct xdp_md, data_meta):
7107 info->reg_type = PTR_TO_PACKET_META;
7109 case offsetof(struct xdp_md, data_end):
7110 info->reg_type = PTR_TO_PACKET_END;
7114 return __is_valid_xdp_access(off, size);
7117 void bpf_warn_invalid_xdp_action(u32 act)
7119 const u32 act_max = XDP_REDIRECT;
7121 WARN_ONCE(1, "%s XDP return value %u, expect packet loss!\n",
7122 act > act_max ? "Illegal" : "Driver unsupported",
7125 EXPORT_SYMBOL_GPL(bpf_warn_invalid_xdp_action);
7127 static bool sock_addr_is_valid_access(int off, int size,
7128 enum bpf_access_type type,
7129 const struct bpf_prog *prog,
7130 struct bpf_insn_access_aux *info)
7132 const int size_default = sizeof(__u32);
7134 if (off < 0 || off >= sizeof(struct bpf_sock_addr))
7136 if (off % size != 0)
7139 /* Disallow access to IPv6 fields from IPv4 contex and vise
7143 case bpf_ctx_range(struct bpf_sock_addr, user_ip4):
7144 switch (prog->expected_attach_type) {
7145 case BPF_CGROUP_INET4_BIND:
7146 case BPF_CGROUP_INET4_CONNECT:
7147 case BPF_CGROUP_INET4_GETPEERNAME:
7148 case BPF_CGROUP_INET4_GETSOCKNAME:
7149 case BPF_CGROUP_UDP4_SENDMSG:
7150 case BPF_CGROUP_UDP4_RECVMSG:
7156 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
7157 switch (prog->expected_attach_type) {
7158 case BPF_CGROUP_INET6_BIND:
7159 case BPF_CGROUP_INET6_CONNECT:
7160 case BPF_CGROUP_INET6_GETPEERNAME:
7161 case BPF_CGROUP_INET6_GETSOCKNAME:
7162 case BPF_CGROUP_UDP6_SENDMSG:
7163 case BPF_CGROUP_UDP6_RECVMSG:
7169 case bpf_ctx_range(struct bpf_sock_addr, msg_src_ip4):
7170 switch (prog->expected_attach_type) {
7171 case BPF_CGROUP_UDP4_SENDMSG:
7177 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
7179 switch (prog->expected_attach_type) {
7180 case BPF_CGROUP_UDP6_SENDMSG:
7189 case bpf_ctx_range(struct bpf_sock_addr, user_ip4):
7190 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
7191 case bpf_ctx_range(struct bpf_sock_addr, msg_src_ip4):
7192 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
7194 case bpf_ctx_range(struct bpf_sock_addr, user_port):
7195 if (type == BPF_READ) {
7196 bpf_ctx_record_field_size(info, size_default);
7198 if (bpf_ctx_wide_access_ok(off, size,
7199 struct bpf_sock_addr,
7203 if (bpf_ctx_wide_access_ok(off, size,
7204 struct bpf_sock_addr,
7208 if (!bpf_ctx_narrow_access_ok(off, size, size_default))
7211 if (bpf_ctx_wide_access_ok(off, size,
7212 struct bpf_sock_addr,
7216 if (bpf_ctx_wide_access_ok(off, size,
7217 struct bpf_sock_addr,
7221 if (size != size_default)
7225 case offsetof(struct bpf_sock_addr, sk):
7226 if (type != BPF_READ)
7228 if (size != sizeof(__u64))
7230 info->reg_type = PTR_TO_SOCKET;
7233 if (type == BPF_READ) {
7234 if (size != size_default)
7244 static bool sock_ops_is_valid_access(int off, int size,
7245 enum bpf_access_type type,
7246 const struct bpf_prog *prog,
7247 struct bpf_insn_access_aux *info)
7249 const int size_default = sizeof(__u32);
7251 if (off < 0 || off >= sizeof(struct bpf_sock_ops))
7254 /* The verifier guarantees that size > 0. */
7255 if (off % size != 0)
7258 if (type == BPF_WRITE) {
7260 case offsetof(struct bpf_sock_ops, reply):
7261 case offsetof(struct bpf_sock_ops, sk_txhash):
7262 if (size != size_default)
7270 case bpf_ctx_range_till(struct bpf_sock_ops, bytes_received,
7272 if (size != sizeof(__u64))
7275 case offsetof(struct bpf_sock_ops, sk):
7276 if (size != sizeof(__u64))
7278 info->reg_type = PTR_TO_SOCKET_OR_NULL;
7281 if (size != size_default)
7290 static int sk_skb_prologue(struct bpf_insn *insn_buf, bool direct_write,
7291 const struct bpf_prog *prog)
7293 return bpf_unclone_prologue(insn_buf, direct_write, prog, SK_DROP);
7296 static bool sk_skb_is_valid_access(int off, int size,
7297 enum bpf_access_type type,
7298 const struct bpf_prog *prog,
7299 struct bpf_insn_access_aux *info)
7302 case bpf_ctx_range(struct __sk_buff, tc_classid):
7303 case bpf_ctx_range(struct __sk_buff, data_meta):
7304 case bpf_ctx_range(struct __sk_buff, tstamp):
7305 case bpf_ctx_range(struct __sk_buff, wire_len):
7309 if (type == BPF_WRITE) {
7311 case bpf_ctx_range(struct __sk_buff, tc_index):
7312 case bpf_ctx_range(struct __sk_buff, priority):
7320 case bpf_ctx_range(struct __sk_buff, mark):
7322 case bpf_ctx_range(struct __sk_buff, data):
7323 info->reg_type = PTR_TO_PACKET;
7325 case bpf_ctx_range(struct __sk_buff, data_end):
7326 info->reg_type = PTR_TO_PACKET_END;
7330 return bpf_skb_is_valid_access(off, size, type, prog, info);
7333 static bool sk_msg_is_valid_access(int off, int size,
7334 enum bpf_access_type type,
7335 const struct bpf_prog *prog,
7336 struct bpf_insn_access_aux *info)
7338 if (type == BPF_WRITE)
7341 if (off % size != 0)
7345 case offsetof(struct sk_msg_md, data):
7346 info->reg_type = PTR_TO_PACKET;
7347 if (size != sizeof(__u64))
7350 case offsetof(struct sk_msg_md, data_end):
7351 info->reg_type = PTR_TO_PACKET_END;
7352 if (size != sizeof(__u64))
7355 case offsetof(struct sk_msg_md, sk):
7356 if (size != sizeof(__u64))
7358 info->reg_type = PTR_TO_SOCKET;
7360 case bpf_ctx_range(struct sk_msg_md, family):
7361 case bpf_ctx_range(struct sk_msg_md, remote_ip4):
7362 case bpf_ctx_range(struct sk_msg_md, local_ip4):
7363 case bpf_ctx_range_till(struct sk_msg_md, remote_ip6[0], remote_ip6[3]):
7364 case bpf_ctx_range_till(struct sk_msg_md, local_ip6[0], local_ip6[3]):
7365 case bpf_ctx_range(struct sk_msg_md, remote_port):
7366 case bpf_ctx_range(struct sk_msg_md, local_port):
7367 case bpf_ctx_range(struct sk_msg_md, size):
7368 if (size != sizeof(__u32))
7377 static bool flow_dissector_is_valid_access(int off, int size,
7378 enum bpf_access_type type,
7379 const struct bpf_prog *prog,
7380 struct bpf_insn_access_aux *info)
7382 const int size_default = sizeof(__u32);
7384 if (off < 0 || off >= sizeof(struct __sk_buff))
7387 if (type == BPF_WRITE)
7391 case bpf_ctx_range(struct __sk_buff, data):
7392 if (size != size_default)
7394 info->reg_type = PTR_TO_PACKET;
7396 case bpf_ctx_range(struct __sk_buff, data_end):
7397 if (size != size_default)
7399 info->reg_type = PTR_TO_PACKET_END;
7401 case bpf_ctx_range_ptr(struct __sk_buff, flow_keys):
7402 if (size != sizeof(__u64))
7404 info->reg_type = PTR_TO_FLOW_KEYS;
7411 static u32 flow_dissector_convert_ctx_access(enum bpf_access_type type,
7412 const struct bpf_insn *si,
7413 struct bpf_insn *insn_buf,
7414 struct bpf_prog *prog,
7418 struct bpf_insn *insn = insn_buf;
7421 case offsetof(struct __sk_buff, data):
7422 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_flow_dissector, data),
7423 si->dst_reg, si->src_reg,
7424 offsetof(struct bpf_flow_dissector, data));
7427 case offsetof(struct __sk_buff, data_end):
7428 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_flow_dissector, data_end),
7429 si->dst_reg, si->src_reg,
7430 offsetof(struct bpf_flow_dissector, data_end));
7433 case offsetof(struct __sk_buff, flow_keys):
7434 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_flow_dissector, flow_keys),
7435 si->dst_reg, si->src_reg,
7436 offsetof(struct bpf_flow_dissector, flow_keys));
7440 return insn - insn_buf;
7443 static struct bpf_insn *bpf_convert_shinfo_access(const struct bpf_insn *si,
7444 struct bpf_insn *insn)
7446 /* si->dst_reg = skb_shinfo(SKB); */
7447 #ifdef NET_SKBUFF_DATA_USES_OFFSET
7448 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, end),
7449 BPF_REG_AX, si->src_reg,
7450 offsetof(struct sk_buff, end));
7451 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, head),
7452 si->dst_reg, si->src_reg,
7453 offsetof(struct sk_buff, head));
7454 *insn++ = BPF_ALU64_REG(BPF_ADD, si->dst_reg, BPF_REG_AX);
7456 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, end),
7457 si->dst_reg, si->src_reg,
7458 offsetof(struct sk_buff, end));
7464 static u32 bpf_convert_ctx_access(enum bpf_access_type type,
7465 const struct bpf_insn *si,
7466 struct bpf_insn *insn_buf,
7467 struct bpf_prog *prog, u32 *target_size)
7469 struct bpf_insn *insn = insn_buf;
7473 case offsetof(struct __sk_buff, len):
7474 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
7475 bpf_target_off(struct sk_buff, len, 4,
7479 case offsetof(struct __sk_buff, protocol):
7480 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
7481 bpf_target_off(struct sk_buff, protocol, 2,
7485 case offsetof(struct __sk_buff, vlan_proto):
7486 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
7487 bpf_target_off(struct sk_buff, vlan_proto, 2,
7491 case offsetof(struct __sk_buff, priority):
7492 if (type == BPF_WRITE)
7493 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
7494 bpf_target_off(struct sk_buff, priority, 4,
7497 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
7498 bpf_target_off(struct sk_buff, priority, 4,
7502 case offsetof(struct __sk_buff, ingress_ifindex):
7503 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
7504 bpf_target_off(struct sk_buff, skb_iif, 4,
7508 case offsetof(struct __sk_buff, ifindex):
7509 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
7510 si->dst_reg, si->src_reg,
7511 offsetof(struct sk_buff, dev));
7512 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
7513 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7514 bpf_target_off(struct net_device, ifindex, 4,
7518 case offsetof(struct __sk_buff, hash):
7519 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
7520 bpf_target_off(struct sk_buff, hash, 4,
7524 case offsetof(struct __sk_buff, mark):
7525 if (type == BPF_WRITE)
7526 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
7527 bpf_target_off(struct sk_buff, mark, 4,
7530 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
7531 bpf_target_off(struct sk_buff, mark, 4,
7535 case offsetof(struct __sk_buff, pkt_type):
7537 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->src_reg,
7539 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, PKT_TYPE_MAX);
7540 #ifdef __BIG_ENDIAN_BITFIELD
7541 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, 5);
7545 case offsetof(struct __sk_buff, queue_mapping):
7546 if (type == BPF_WRITE) {
7547 *insn++ = BPF_JMP_IMM(BPF_JGE, si->src_reg, NO_QUEUE_MAPPING, 1);
7548 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg, si->src_reg,
7549 bpf_target_off(struct sk_buff,
7553 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
7554 bpf_target_off(struct sk_buff,
7560 case offsetof(struct __sk_buff, vlan_present):
7562 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->src_reg,
7563 PKT_VLAN_PRESENT_OFFSET());
7564 if (PKT_VLAN_PRESENT_BIT)
7565 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, PKT_VLAN_PRESENT_BIT);
7566 if (PKT_VLAN_PRESENT_BIT < 7)
7567 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, 1);
7570 case offsetof(struct __sk_buff, vlan_tci):
7571 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
7572 bpf_target_off(struct sk_buff, vlan_tci, 2,
7576 case offsetof(struct __sk_buff, cb[0]) ...
7577 offsetofend(struct __sk_buff, cb[4]) - 1:
7578 BUILD_BUG_ON(sizeof_field(struct qdisc_skb_cb, data) < 20);
7579 BUILD_BUG_ON((offsetof(struct sk_buff, cb) +
7580 offsetof(struct qdisc_skb_cb, data)) %
7583 prog->cb_access = 1;
7585 off -= offsetof(struct __sk_buff, cb[0]);
7586 off += offsetof(struct sk_buff, cb);
7587 off += offsetof(struct qdisc_skb_cb, data);
7588 if (type == BPF_WRITE)
7589 *insn++ = BPF_STX_MEM(BPF_SIZE(si->code), si->dst_reg,
7592 *insn++ = BPF_LDX_MEM(BPF_SIZE(si->code), si->dst_reg,
7596 case offsetof(struct __sk_buff, tc_classid):
7597 BUILD_BUG_ON(sizeof_field(struct qdisc_skb_cb, tc_classid) != 2);
7600 off -= offsetof(struct __sk_buff, tc_classid);
7601 off += offsetof(struct sk_buff, cb);
7602 off += offsetof(struct qdisc_skb_cb, tc_classid);
7604 if (type == BPF_WRITE)
7605 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg,
7608 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg,
7612 case offsetof(struct __sk_buff, data):
7613 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
7614 si->dst_reg, si->src_reg,
7615 offsetof(struct sk_buff, data));
7618 case offsetof(struct __sk_buff, data_meta):
7620 off -= offsetof(struct __sk_buff, data_meta);
7621 off += offsetof(struct sk_buff, cb);
7622 off += offsetof(struct bpf_skb_data_end, data_meta);
7623 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
7627 case offsetof(struct __sk_buff, data_end):
7629 off -= offsetof(struct __sk_buff, data_end);
7630 off += offsetof(struct sk_buff, cb);
7631 off += offsetof(struct bpf_skb_data_end, data_end);
7632 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
7636 case offsetof(struct __sk_buff, tc_index):
7637 #ifdef CONFIG_NET_SCHED
7638 if (type == BPF_WRITE)
7639 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg, si->src_reg,
7640 bpf_target_off(struct sk_buff, tc_index, 2,
7643 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
7644 bpf_target_off(struct sk_buff, tc_index, 2,
7648 if (type == BPF_WRITE)
7649 *insn++ = BPF_MOV64_REG(si->dst_reg, si->dst_reg);
7651 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
7655 case offsetof(struct __sk_buff, napi_id):
7656 #if defined(CONFIG_NET_RX_BUSY_POLL)
7657 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
7658 bpf_target_off(struct sk_buff, napi_id, 4,
7660 *insn++ = BPF_JMP_IMM(BPF_JGE, si->dst_reg, MIN_NAPI_ID, 1);
7661 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
7664 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
7667 case offsetof(struct __sk_buff, family):
7668 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_family) != 2);
7670 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
7671 si->dst_reg, si->src_reg,
7672 offsetof(struct sk_buff, sk));
7673 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
7674 bpf_target_off(struct sock_common,
7678 case offsetof(struct __sk_buff, remote_ip4):
7679 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_daddr) != 4);
7681 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
7682 si->dst_reg, si->src_reg,
7683 offsetof(struct sk_buff, sk));
7684 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7685 bpf_target_off(struct sock_common,
7689 case offsetof(struct __sk_buff, local_ip4):
7690 BUILD_BUG_ON(sizeof_field(struct sock_common,
7691 skc_rcv_saddr) != 4);
7693 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
7694 si->dst_reg, si->src_reg,
7695 offsetof(struct sk_buff, sk));
7696 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7697 bpf_target_off(struct sock_common,
7701 case offsetof(struct __sk_buff, remote_ip6[0]) ...
7702 offsetof(struct __sk_buff, remote_ip6[3]):
7703 #if IS_ENABLED(CONFIG_IPV6)
7704 BUILD_BUG_ON(sizeof_field(struct sock_common,
7705 skc_v6_daddr.s6_addr32[0]) != 4);
7708 off -= offsetof(struct __sk_buff, remote_ip6[0]);
7710 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
7711 si->dst_reg, si->src_reg,
7712 offsetof(struct sk_buff, sk));
7713 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7714 offsetof(struct sock_common,
7715 skc_v6_daddr.s6_addr32[0]) +
7718 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
7721 case offsetof(struct __sk_buff, local_ip6[0]) ...
7722 offsetof(struct __sk_buff, local_ip6[3]):
7723 #if IS_ENABLED(CONFIG_IPV6)
7724 BUILD_BUG_ON(sizeof_field(struct sock_common,
7725 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
7728 off -= offsetof(struct __sk_buff, local_ip6[0]);
7730 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
7731 si->dst_reg, si->src_reg,
7732 offsetof(struct sk_buff, sk));
7733 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7734 offsetof(struct sock_common,
7735 skc_v6_rcv_saddr.s6_addr32[0]) +
7738 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
7742 case offsetof(struct __sk_buff, remote_port):
7743 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_dport) != 2);
7745 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
7746 si->dst_reg, si->src_reg,
7747 offsetof(struct sk_buff, sk));
7748 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
7749 bpf_target_off(struct sock_common,
7752 #ifndef __BIG_ENDIAN_BITFIELD
7753 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
7757 case offsetof(struct __sk_buff, local_port):
7758 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_num) != 2);
7760 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
7761 si->dst_reg, si->src_reg,
7762 offsetof(struct sk_buff, sk));
7763 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
7764 bpf_target_off(struct sock_common,
7765 skc_num, 2, target_size));
7768 case offsetof(struct __sk_buff, tstamp):
7769 BUILD_BUG_ON(sizeof_field(struct sk_buff, tstamp) != 8);
7771 if (type == BPF_WRITE)
7772 *insn++ = BPF_STX_MEM(BPF_DW,
7773 si->dst_reg, si->src_reg,
7774 bpf_target_off(struct sk_buff,
7778 *insn++ = BPF_LDX_MEM(BPF_DW,
7779 si->dst_reg, si->src_reg,
7780 bpf_target_off(struct sk_buff,
7785 case offsetof(struct __sk_buff, gso_segs):
7786 insn = bpf_convert_shinfo_access(si, insn);
7787 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct skb_shared_info, gso_segs),
7788 si->dst_reg, si->dst_reg,
7789 bpf_target_off(struct skb_shared_info,
7793 case offsetof(struct __sk_buff, gso_size):
7794 insn = bpf_convert_shinfo_access(si, insn);
7795 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct skb_shared_info, gso_size),
7796 si->dst_reg, si->dst_reg,
7797 bpf_target_off(struct skb_shared_info,
7801 case offsetof(struct __sk_buff, wire_len):
7802 BUILD_BUG_ON(sizeof_field(struct qdisc_skb_cb, pkt_len) != 4);
7805 off -= offsetof(struct __sk_buff, wire_len);
7806 off += offsetof(struct sk_buff, cb);
7807 off += offsetof(struct qdisc_skb_cb, pkt_len);
7809 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, off);
7812 case offsetof(struct __sk_buff, sk):
7813 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
7814 si->dst_reg, si->src_reg,
7815 offsetof(struct sk_buff, sk));
7819 return insn - insn_buf;
7822 u32 bpf_sock_convert_ctx_access(enum bpf_access_type type,
7823 const struct bpf_insn *si,
7824 struct bpf_insn *insn_buf,
7825 struct bpf_prog *prog, u32 *target_size)
7827 struct bpf_insn *insn = insn_buf;
7831 case offsetof(struct bpf_sock, bound_dev_if):
7832 BUILD_BUG_ON(sizeof_field(struct sock, sk_bound_dev_if) != 4);
7834 if (type == BPF_WRITE)
7835 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
7836 offsetof(struct sock, sk_bound_dev_if));
7838 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
7839 offsetof(struct sock, sk_bound_dev_if));
7842 case offsetof(struct bpf_sock, mark):
7843 BUILD_BUG_ON(sizeof_field(struct sock, sk_mark) != 4);
7845 if (type == BPF_WRITE)
7846 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
7847 offsetof(struct sock, sk_mark));
7849 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
7850 offsetof(struct sock, sk_mark));
7853 case offsetof(struct bpf_sock, priority):
7854 BUILD_BUG_ON(sizeof_field(struct sock, sk_priority) != 4);
7856 if (type == BPF_WRITE)
7857 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
7858 offsetof(struct sock, sk_priority));
7860 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
7861 offsetof(struct sock, sk_priority));
7864 case offsetof(struct bpf_sock, family):
7865 *insn++ = BPF_LDX_MEM(
7866 BPF_FIELD_SIZEOF(struct sock_common, skc_family),
7867 si->dst_reg, si->src_reg,
7868 bpf_target_off(struct sock_common,
7870 sizeof_field(struct sock_common,
7875 case offsetof(struct bpf_sock, type):
7876 *insn++ = BPF_LDX_MEM(
7877 BPF_FIELD_SIZEOF(struct sock, sk_type),
7878 si->dst_reg, si->src_reg,
7879 bpf_target_off(struct sock, sk_type,
7880 sizeof_field(struct sock, sk_type),
7884 case offsetof(struct bpf_sock, protocol):
7885 *insn++ = BPF_LDX_MEM(
7886 BPF_FIELD_SIZEOF(struct sock, sk_protocol),
7887 si->dst_reg, si->src_reg,
7888 bpf_target_off(struct sock, sk_protocol,
7889 sizeof_field(struct sock, sk_protocol),
7893 case offsetof(struct bpf_sock, src_ip4):
7894 *insn++ = BPF_LDX_MEM(
7895 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
7896 bpf_target_off(struct sock_common, skc_rcv_saddr,
7897 sizeof_field(struct sock_common,
7902 case offsetof(struct bpf_sock, dst_ip4):
7903 *insn++ = BPF_LDX_MEM(
7904 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
7905 bpf_target_off(struct sock_common, skc_daddr,
7906 sizeof_field(struct sock_common,
7911 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
7912 #if IS_ENABLED(CONFIG_IPV6)
7914 off -= offsetof(struct bpf_sock, src_ip6[0]);
7915 *insn++ = BPF_LDX_MEM(
7916 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
7919 skc_v6_rcv_saddr.s6_addr32[0],
7920 sizeof_field(struct sock_common,
7921 skc_v6_rcv_saddr.s6_addr32[0]),
7922 target_size) + off);
7925 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
7929 case bpf_ctx_range_till(struct bpf_sock, dst_ip6[0], dst_ip6[3]):
7930 #if IS_ENABLED(CONFIG_IPV6)
7932 off -= offsetof(struct bpf_sock, dst_ip6[0]);
7933 *insn++ = BPF_LDX_MEM(
7934 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
7935 bpf_target_off(struct sock_common,
7936 skc_v6_daddr.s6_addr32[0],
7937 sizeof_field(struct sock_common,
7938 skc_v6_daddr.s6_addr32[0]),
7939 target_size) + off);
7941 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
7946 case offsetof(struct bpf_sock, src_port):
7947 *insn++ = BPF_LDX_MEM(
7948 BPF_FIELD_SIZEOF(struct sock_common, skc_num),
7949 si->dst_reg, si->src_reg,
7950 bpf_target_off(struct sock_common, skc_num,
7951 sizeof_field(struct sock_common,
7956 case offsetof(struct bpf_sock, dst_port):
7957 *insn++ = BPF_LDX_MEM(
7958 BPF_FIELD_SIZEOF(struct sock_common, skc_dport),
7959 si->dst_reg, si->src_reg,
7960 bpf_target_off(struct sock_common, skc_dport,
7961 sizeof_field(struct sock_common,
7966 case offsetof(struct bpf_sock, state):
7967 *insn++ = BPF_LDX_MEM(
7968 BPF_FIELD_SIZEOF(struct sock_common, skc_state),
7969 si->dst_reg, si->src_reg,
7970 bpf_target_off(struct sock_common, skc_state,
7971 sizeof_field(struct sock_common,
7975 case offsetof(struct bpf_sock, rx_queue_mapping):
7977 *insn++ = BPF_LDX_MEM(
7978 BPF_FIELD_SIZEOF(struct sock, sk_rx_queue_mapping),
7979 si->dst_reg, si->src_reg,
7980 bpf_target_off(struct sock, sk_rx_queue_mapping,
7981 sizeof_field(struct sock,
7982 sk_rx_queue_mapping),
7984 *insn++ = BPF_JMP_IMM(BPF_JNE, si->dst_reg, NO_QUEUE_MAPPING,
7986 *insn++ = BPF_MOV64_IMM(si->dst_reg, -1);
7988 *insn++ = BPF_MOV64_IMM(si->dst_reg, -1);
7994 return insn - insn_buf;
7997 static u32 tc_cls_act_convert_ctx_access(enum bpf_access_type type,
7998 const struct bpf_insn *si,
7999 struct bpf_insn *insn_buf,
8000 struct bpf_prog *prog, u32 *target_size)
8002 struct bpf_insn *insn = insn_buf;
8005 case offsetof(struct __sk_buff, ifindex):
8006 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
8007 si->dst_reg, si->src_reg,
8008 offsetof(struct sk_buff, dev));
8009 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
8010 bpf_target_off(struct net_device, ifindex, 4,
8014 return bpf_convert_ctx_access(type, si, insn_buf, prog,
8018 return insn - insn_buf;
8021 static u32 xdp_convert_ctx_access(enum bpf_access_type type,
8022 const struct bpf_insn *si,
8023 struct bpf_insn *insn_buf,
8024 struct bpf_prog *prog, u32 *target_size)
8026 struct bpf_insn *insn = insn_buf;
8029 case offsetof(struct xdp_md, data):
8030 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data),
8031 si->dst_reg, si->src_reg,
8032 offsetof(struct xdp_buff, data));
8034 case offsetof(struct xdp_md, data_meta):
8035 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_meta),
8036 si->dst_reg, si->src_reg,
8037 offsetof(struct xdp_buff, data_meta));
8039 case offsetof(struct xdp_md, data_end):
8040 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_end),
8041 si->dst_reg, si->src_reg,
8042 offsetof(struct xdp_buff, data_end));
8044 case offsetof(struct xdp_md, ingress_ifindex):
8045 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, rxq),
8046 si->dst_reg, si->src_reg,
8047 offsetof(struct xdp_buff, rxq));
8048 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_rxq_info, dev),
8049 si->dst_reg, si->dst_reg,
8050 offsetof(struct xdp_rxq_info, dev));
8051 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
8052 offsetof(struct net_device, ifindex));
8054 case offsetof(struct xdp_md, rx_queue_index):
8055 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, rxq),
8056 si->dst_reg, si->src_reg,
8057 offsetof(struct xdp_buff, rxq));
8058 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
8059 offsetof(struct xdp_rxq_info,
8062 case offsetof(struct xdp_md, egress_ifindex):
8063 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, txq),
8064 si->dst_reg, si->src_reg,
8065 offsetof(struct xdp_buff, txq));
8066 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_txq_info, dev),
8067 si->dst_reg, si->dst_reg,
8068 offsetof(struct xdp_txq_info, dev));
8069 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
8070 offsetof(struct net_device, ifindex));
8074 return insn - insn_buf;
8077 /* SOCK_ADDR_LOAD_NESTED_FIELD() loads Nested Field S.F.NF where S is type of
8078 * context Structure, F is Field in context structure that contains a pointer
8079 * to Nested Structure of type NS that has the field NF.
8081 * SIZE encodes the load size (BPF_B, BPF_H, etc). It's up to caller to make
8082 * sure that SIZE is not greater than actual size of S.F.NF.
8084 * If offset OFF is provided, the load happens from that offset relative to
8087 #define SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, SIZE, OFF) \
8089 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), si->dst_reg, \
8090 si->src_reg, offsetof(S, F)); \
8091 *insn++ = BPF_LDX_MEM( \
8092 SIZE, si->dst_reg, si->dst_reg, \
8093 bpf_target_off(NS, NF, sizeof_field(NS, NF), \
8098 #define SOCK_ADDR_LOAD_NESTED_FIELD(S, NS, F, NF) \
8099 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, \
8100 BPF_FIELD_SIZEOF(NS, NF), 0)
8102 /* SOCK_ADDR_STORE_NESTED_FIELD_OFF() has semantic similar to
8103 * SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF() but for store operation.
8105 * In addition it uses Temporary Field TF (member of struct S) as the 3rd
8106 * "register" since two registers available in convert_ctx_access are not
8107 * enough: we can't override neither SRC, since it contains value to store, nor
8108 * DST since it contains pointer to context that may be used by later
8109 * instructions. But we need a temporary place to save pointer to nested
8110 * structure whose field we want to store to.
8112 #define SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, SIZE, OFF, TF) \
8114 int tmp_reg = BPF_REG_9; \
8115 if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg) \
8117 if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg) \
8119 *insn++ = BPF_STX_MEM(BPF_DW, si->dst_reg, tmp_reg, \
8121 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), tmp_reg, \
8122 si->dst_reg, offsetof(S, F)); \
8123 *insn++ = BPF_STX_MEM(SIZE, tmp_reg, si->src_reg, \
8124 bpf_target_off(NS, NF, sizeof_field(NS, NF), \
8127 *insn++ = BPF_LDX_MEM(BPF_DW, tmp_reg, si->dst_reg, \
8131 #define SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, SIZE, OFF, \
8134 if (type == BPF_WRITE) { \
8135 SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, SIZE, \
8138 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF( \
8139 S, NS, F, NF, SIZE, OFF); \
8143 #define SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD(S, NS, F, NF, TF) \
8144 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF( \
8145 S, NS, F, NF, BPF_FIELD_SIZEOF(NS, NF), 0, TF)
8147 static u32 sock_addr_convert_ctx_access(enum bpf_access_type type,
8148 const struct bpf_insn *si,
8149 struct bpf_insn *insn_buf,
8150 struct bpf_prog *prog, u32 *target_size)
8152 int off, port_size = sizeof_field(struct sockaddr_in6, sin6_port);
8153 struct bpf_insn *insn = insn_buf;
8156 case offsetof(struct bpf_sock_addr, user_family):
8157 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
8158 struct sockaddr, uaddr, sa_family);
8161 case offsetof(struct bpf_sock_addr, user_ip4):
8162 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
8163 struct bpf_sock_addr_kern, struct sockaddr_in, uaddr,
8164 sin_addr, BPF_SIZE(si->code), 0, tmp_reg);
8167 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
8169 off -= offsetof(struct bpf_sock_addr, user_ip6[0]);
8170 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
8171 struct bpf_sock_addr_kern, struct sockaddr_in6, uaddr,
8172 sin6_addr.s6_addr32[0], BPF_SIZE(si->code), off,
8176 case offsetof(struct bpf_sock_addr, user_port):
8177 /* To get port we need to know sa_family first and then treat
8178 * sockaddr as either sockaddr_in or sockaddr_in6.
8179 * Though we can simplify since port field has same offset and
8180 * size in both structures.
8181 * Here we check this invariant and use just one of the
8182 * structures if it's true.
8184 BUILD_BUG_ON(offsetof(struct sockaddr_in, sin_port) !=
8185 offsetof(struct sockaddr_in6, sin6_port));
8186 BUILD_BUG_ON(sizeof_field(struct sockaddr_in, sin_port) !=
8187 sizeof_field(struct sockaddr_in6, sin6_port));
8188 /* Account for sin6_port being smaller than user_port. */
8189 port_size = min(port_size, BPF_LDST_BYTES(si));
8190 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
8191 struct bpf_sock_addr_kern, struct sockaddr_in6, uaddr,
8192 sin6_port, bytes_to_bpf_size(port_size), 0, tmp_reg);
8195 case offsetof(struct bpf_sock_addr, family):
8196 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
8197 struct sock, sk, sk_family);
8200 case offsetof(struct bpf_sock_addr, type):
8201 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
8202 struct sock, sk, sk_type);
8205 case offsetof(struct bpf_sock_addr, protocol):
8206 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
8207 struct sock, sk, sk_protocol);
8210 case offsetof(struct bpf_sock_addr, msg_src_ip4):
8211 /* Treat t_ctx as struct in_addr for msg_src_ip4. */
8212 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
8213 struct bpf_sock_addr_kern, struct in_addr, t_ctx,
8214 s_addr, BPF_SIZE(si->code), 0, tmp_reg);
8217 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
8220 off -= offsetof(struct bpf_sock_addr, msg_src_ip6[0]);
8221 /* Treat t_ctx as struct in6_addr for msg_src_ip6. */
8222 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
8223 struct bpf_sock_addr_kern, struct in6_addr, t_ctx,
8224 s6_addr32[0], BPF_SIZE(si->code), off, tmp_reg);
8226 case offsetof(struct bpf_sock_addr, sk):
8227 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct bpf_sock_addr_kern, sk),
8228 si->dst_reg, si->src_reg,
8229 offsetof(struct bpf_sock_addr_kern, sk));
8233 return insn - insn_buf;
8236 static u32 sock_ops_convert_ctx_access(enum bpf_access_type type,
8237 const struct bpf_insn *si,
8238 struct bpf_insn *insn_buf,
8239 struct bpf_prog *prog,
8242 struct bpf_insn *insn = insn_buf;
8245 /* Helper macro for adding read access to tcp_sock or sock fields. */
8246 #define SOCK_OPS_GET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ) \
8248 BUILD_BUG_ON(sizeof_field(OBJ, OBJ_FIELD) > \
8249 sizeof_field(struct bpf_sock_ops, BPF_FIELD)); \
8250 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
8251 struct bpf_sock_ops_kern, \
8253 si->dst_reg, si->src_reg, \
8254 offsetof(struct bpf_sock_ops_kern, \
8256 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 2); \
8257 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
8258 struct bpf_sock_ops_kern, sk),\
8259 si->dst_reg, si->src_reg, \
8260 offsetof(struct bpf_sock_ops_kern, sk));\
8261 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(OBJ, \
8263 si->dst_reg, si->dst_reg, \
8264 offsetof(OBJ, OBJ_FIELD)); \
8267 #define SOCK_OPS_GET_TCP_SOCK_FIELD(FIELD) \
8268 SOCK_OPS_GET_FIELD(FIELD, FIELD, struct tcp_sock)
8270 /* Helper macro for adding write access to tcp_sock or sock fields.
8271 * The macro is called with two registers, dst_reg which contains a pointer
8272 * to ctx (context) and src_reg which contains the value that should be
8273 * stored. However, we need an additional register since we cannot overwrite
8274 * dst_reg because it may be used later in the program.
8275 * Instead we "borrow" one of the other register. We first save its value
8276 * into a new (temp) field in bpf_sock_ops_kern, use it, and then restore
8277 * it at the end of the macro.
8279 #define SOCK_OPS_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ) \
8281 int reg = BPF_REG_9; \
8282 BUILD_BUG_ON(sizeof_field(OBJ, OBJ_FIELD) > \
8283 sizeof_field(struct bpf_sock_ops, BPF_FIELD)); \
8284 if (si->dst_reg == reg || si->src_reg == reg) \
8286 if (si->dst_reg == reg || si->src_reg == reg) \
8288 *insn++ = BPF_STX_MEM(BPF_DW, si->dst_reg, reg, \
8289 offsetof(struct bpf_sock_ops_kern, \
8291 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
8292 struct bpf_sock_ops_kern, \
8295 offsetof(struct bpf_sock_ops_kern, \
8297 *insn++ = BPF_JMP_IMM(BPF_JEQ, reg, 0, 2); \
8298 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
8299 struct bpf_sock_ops_kern, sk),\
8301 offsetof(struct bpf_sock_ops_kern, sk));\
8302 *insn++ = BPF_STX_MEM(BPF_FIELD_SIZEOF(OBJ, OBJ_FIELD), \
8304 offsetof(OBJ, OBJ_FIELD)); \
8305 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->dst_reg, \
8306 offsetof(struct bpf_sock_ops_kern, \
8310 #define SOCK_OPS_GET_OR_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ, TYPE) \
8312 if (TYPE == BPF_WRITE) \
8313 SOCK_OPS_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ); \
8315 SOCK_OPS_GET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ); \
8318 if (insn > insn_buf)
8319 return insn - insn_buf;
8322 case offsetof(struct bpf_sock_ops, op) ...
8323 offsetof(struct bpf_sock_ops, replylong[3]):
8324 BUILD_BUG_ON(sizeof_field(struct bpf_sock_ops, op) !=
8325 sizeof_field(struct bpf_sock_ops_kern, op));
8326 BUILD_BUG_ON(sizeof_field(struct bpf_sock_ops, reply) !=
8327 sizeof_field(struct bpf_sock_ops_kern, reply));
8328 BUILD_BUG_ON(sizeof_field(struct bpf_sock_ops, replylong) !=
8329 sizeof_field(struct bpf_sock_ops_kern, replylong));
8331 off -= offsetof(struct bpf_sock_ops, op);
8332 off += offsetof(struct bpf_sock_ops_kern, op);
8333 if (type == BPF_WRITE)
8334 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
8337 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
8341 case offsetof(struct bpf_sock_ops, family):
8342 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_family) != 2);
8344 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8345 struct bpf_sock_ops_kern, sk),
8346 si->dst_reg, si->src_reg,
8347 offsetof(struct bpf_sock_ops_kern, sk));
8348 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
8349 offsetof(struct sock_common, skc_family));
8352 case offsetof(struct bpf_sock_ops, remote_ip4):
8353 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_daddr) != 4);
8355 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8356 struct bpf_sock_ops_kern, sk),
8357 si->dst_reg, si->src_reg,
8358 offsetof(struct bpf_sock_ops_kern, sk));
8359 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
8360 offsetof(struct sock_common, skc_daddr));
8363 case offsetof(struct bpf_sock_ops, local_ip4):
8364 BUILD_BUG_ON(sizeof_field(struct sock_common,
8365 skc_rcv_saddr) != 4);
8367 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8368 struct bpf_sock_ops_kern, sk),
8369 si->dst_reg, si->src_reg,
8370 offsetof(struct bpf_sock_ops_kern, sk));
8371 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
8372 offsetof(struct sock_common,
8376 case offsetof(struct bpf_sock_ops, remote_ip6[0]) ...
8377 offsetof(struct bpf_sock_ops, remote_ip6[3]):
8378 #if IS_ENABLED(CONFIG_IPV6)
8379 BUILD_BUG_ON(sizeof_field(struct sock_common,
8380 skc_v6_daddr.s6_addr32[0]) != 4);
8383 off -= offsetof(struct bpf_sock_ops, remote_ip6[0]);
8384 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8385 struct bpf_sock_ops_kern, sk),
8386 si->dst_reg, si->src_reg,
8387 offsetof(struct bpf_sock_ops_kern, sk));
8388 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
8389 offsetof(struct sock_common,
8390 skc_v6_daddr.s6_addr32[0]) +
8393 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
8397 case offsetof(struct bpf_sock_ops, local_ip6[0]) ...
8398 offsetof(struct bpf_sock_ops, local_ip6[3]):
8399 #if IS_ENABLED(CONFIG_IPV6)
8400 BUILD_BUG_ON(sizeof_field(struct sock_common,
8401 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
8404 off -= offsetof(struct bpf_sock_ops, local_ip6[0]);
8405 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8406 struct bpf_sock_ops_kern, sk),
8407 si->dst_reg, si->src_reg,
8408 offsetof(struct bpf_sock_ops_kern, sk));
8409 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
8410 offsetof(struct sock_common,
8411 skc_v6_rcv_saddr.s6_addr32[0]) +
8414 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
8418 case offsetof(struct bpf_sock_ops, remote_port):
8419 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_dport) != 2);
8421 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8422 struct bpf_sock_ops_kern, sk),
8423 si->dst_reg, si->src_reg,
8424 offsetof(struct bpf_sock_ops_kern, sk));
8425 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
8426 offsetof(struct sock_common, skc_dport));
8427 #ifndef __BIG_ENDIAN_BITFIELD
8428 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
8432 case offsetof(struct bpf_sock_ops, local_port):
8433 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_num) != 2);
8435 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8436 struct bpf_sock_ops_kern, sk),
8437 si->dst_reg, si->src_reg,
8438 offsetof(struct bpf_sock_ops_kern, sk));
8439 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
8440 offsetof(struct sock_common, skc_num));
8443 case offsetof(struct bpf_sock_ops, is_fullsock):
8444 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8445 struct bpf_sock_ops_kern,
8447 si->dst_reg, si->src_reg,
8448 offsetof(struct bpf_sock_ops_kern,
8452 case offsetof(struct bpf_sock_ops, state):
8453 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_state) != 1);
8455 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8456 struct bpf_sock_ops_kern, sk),
8457 si->dst_reg, si->src_reg,
8458 offsetof(struct bpf_sock_ops_kern, sk));
8459 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->dst_reg,
8460 offsetof(struct sock_common, skc_state));
8463 case offsetof(struct bpf_sock_ops, rtt_min):
8464 BUILD_BUG_ON(sizeof_field(struct tcp_sock, rtt_min) !=
8465 sizeof(struct minmax));
8466 BUILD_BUG_ON(sizeof(struct minmax) <
8467 sizeof(struct minmax_sample));
8469 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8470 struct bpf_sock_ops_kern, sk),
8471 si->dst_reg, si->src_reg,
8472 offsetof(struct bpf_sock_ops_kern, sk));
8473 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
8474 offsetof(struct tcp_sock, rtt_min) +
8475 sizeof_field(struct minmax_sample, t));
8478 case offsetof(struct bpf_sock_ops, bpf_sock_ops_cb_flags):
8479 SOCK_OPS_GET_FIELD(bpf_sock_ops_cb_flags, bpf_sock_ops_cb_flags,
8483 case offsetof(struct bpf_sock_ops, sk_txhash):
8484 SOCK_OPS_GET_OR_SET_FIELD(sk_txhash, sk_txhash,
8487 case offsetof(struct bpf_sock_ops, snd_cwnd):
8488 SOCK_OPS_GET_TCP_SOCK_FIELD(snd_cwnd);
8490 case offsetof(struct bpf_sock_ops, srtt_us):
8491 SOCK_OPS_GET_TCP_SOCK_FIELD(srtt_us);
8493 case offsetof(struct bpf_sock_ops, snd_ssthresh):
8494 SOCK_OPS_GET_TCP_SOCK_FIELD(snd_ssthresh);
8496 case offsetof(struct bpf_sock_ops, rcv_nxt):
8497 SOCK_OPS_GET_TCP_SOCK_FIELD(rcv_nxt);
8499 case offsetof(struct bpf_sock_ops, snd_nxt):
8500 SOCK_OPS_GET_TCP_SOCK_FIELD(snd_nxt);
8502 case offsetof(struct bpf_sock_ops, snd_una):
8503 SOCK_OPS_GET_TCP_SOCK_FIELD(snd_una);
8505 case offsetof(struct bpf_sock_ops, mss_cache):
8506 SOCK_OPS_GET_TCP_SOCK_FIELD(mss_cache);
8508 case offsetof(struct bpf_sock_ops, ecn_flags):
8509 SOCK_OPS_GET_TCP_SOCK_FIELD(ecn_flags);
8511 case offsetof(struct bpf_sock_ops, rate_delivered):
8512 SOCK_OPS_GET_TCP_SOCK_FIELD(rate_delivered);
8514 case offsetof(struct bpf_sock_ops, rate_interval_us):
8515 SOCK_OPS_GET_TCP_SOCK_FIELD(rate_interval_us);
8517 case offsetof(struct bpf_sock_ops, packets_out):
8518 SOCK_OPS_GET_TCP_SOCK_FIELD(packets_out);
8520 case offsetof(struct bpf_sock_ops, retrans_out):
8521 SOCK_OPS_GET_TCP_SOCK_FIELD(retrans_out);
8523 case offsetof(struct bpf_sock_ops, total_retrans):
8524 SOCK_OPS_GET_TCP_SOCK_FIELD(total_retrans);
8526 case offsetof(struct bpf_sock_ops, segs_in):
8527 SOCK_OPS_GET_TCP_SOCK_FIELD(segs_in);
8529 case offsetof(struct bpf_sock_ops, data_segs_in):
8530 SOCK_OPS_GET_TCP_SOCK_FIELD(data_segs_in);
8532 case offsetof(struct bpf_sock_ops, segs_out):
8533 SOCK_OPS_GET_TCP_SOCK_FIELD(segs_out);
8535 case offsetof(struct bpf_sock_ops, data_segs_out):
8536 SOCK_OPS_GET_TCP_SOCK_FIELD(data_segs_out);
8538 case offsetof(struct bpf_sock_ops, lost_out):
8539 SOCK_OPS_GET_TCP_SOCK_FIELD(lost_out);
8541 case offsetof(struct bpf_sock_ops, sacked_out):
8542 SOCK_OPS_GET_TCP_SOCK_FIELD(sacked_out);
8544 case offsetof(struct bpf_sock_ops, bytes_received):
8545 SOCK_OPS_GET_TCP_SOCK_FIELD(bytes_received);
8547 case offsetof(struct bpf_sock_ops, bytes_acked):
8548 SOCK_OPS_GET_TCP_SOCK_FIELD(bytes_acked);
8550 case offsetof(struct bpf_sock_ops, sk):
8551 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8552 struct bpf_sock_ops_kern,
8554 si->dst_reg, si->src_reg,
8555 offsetof(struct bpf_sock_ops_kern,
8557 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
8558 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8559 struct bpf_sock_ops_kern, sk),
8560 si->dst_reg, si->src_reg,
8561 offsetof(struct bpf_sock_ops_kern, sk));
8564 return insn - insn_buf;
8567 static u32 sk_skb_convert_ctx_access(enum bpf_access_type type,
8568 const struct bpf_insn *si,
8569 struct bpf_insn *insn_buf,
8570 struct bpf_prog *prog, u32 *target_size)
8572 struct bpf_insn *insn = insn_buf;
8576 case offsetof(struct __sk_buff, data_end):
8578 off -= offsetof(struct __sk_buff, data_end);
8579 off += offsetof(struct sk_buff, cb);
8580 off += offsetof(struct tcp_skb_cb, bpf.data_end);
8581 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
8585 return bpf_convert_ctx_access(type, si, insn_buf, prog,
8589 return insn - insn_buf;
8592 static u32 sk_msg_convert_ctx_access(enum bpf_access_type type,
8593 const struct bpf_insn *si,
8594 struct bpf_insn *insn_buf,
8595 struct bpf_prog *prog, u32 *target_size)
8597 struct bpf_insn *insn = insn_buf;
8598 #if IS_ENABLED(CONFIG_IPV6)
8602 /* convert ctx uses the fact sg element is first in struct */
8603 BUILD_BUG_ON(offsetof(struct sk_msg, sg) != 0);
8606 case offsetof(struct sk_msg_md, data):
8607 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg, data),
8608 si->dst_reg, si->src_reg,
8609 offsetof(struct sk_msg, data));
8611 case offsetof(struct sk_msg_md, data_end):
8612 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg, data_end),
8613 si->dst_reg, si->src_reg,
8614 offsetof(struct sk_msg, data_end));
8616 case offsetof(struct sk_msg_md, family):
8617 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_family) != 2);
8619 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8621 si->dst_reg, si->src_reg,
8622 offsetof(struct sk_msg, sk));
8623 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
8624 offsetof(struct sock_common, skc_family));
8627 case offsetof(struct sk_msg_md, remote_ip4):
8628 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_daddr) != 4);
8630 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8632 si->dst_reg, si->src_reg,
8633 offsetof(struct sk_msg, sk));
8634 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
8635 offsetof(struct sock_common, skc_daddr));
8638 case offsetof(struct sk_msg_md, local_ip4):
8639 BUILD_BUG_ON(sizeof_field(struct sock_common,
8640 skc_rcv_saddr) != 4);
8642 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8644 si->dst_reg, si->src_reg,
8645 offsetof(struct sk_msg, sk));
8646 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
8647 offsetof(struct sock_common,
8651 case offsetof(struct sk_msg_md, remote_ip6[0]) ...
8652 offsetof(struct sk_msg_md, remote_ip6[3]):
8653 #if IS_ENABLED(CONFIG_IPV6)
8654 BUILD_BUG_ON(sizeof_field(struct sock_common,
8655 skc_v6_daddr.s6_addr32[0]) != 4);
8658 off -= offsetof(struct sk_msg_md, remote_ip6[0]);
8659 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8661 si->dst_reg, si->src_reg,
8662 offsetof(struct sk_msg, sk));
8663 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
8664 offsetof(struct sock_common,
8665 skc_v6_daddr.s6_addr32[0]) +
8668 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
8672 case offsetof(struct sk_msg_md, local_ip6[0]) ...
8673 offsetof(struct sk_msg_md, local_ip6[3]):
8674 #if IS_ENABLED(CONFIG_IPV6)
8675 BUILD_BUG_ON(sizeof_field(struct sock_common,
8676 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
8679 off -= offsetof(struct sk_msg_md, local_ip6[0]);
8680 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8682 si->dst_reg, si->src_reg,
8683 offsetof(struct sk_msg, sk));
8684 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
8685 offsetof(struct sock_common,
8686 skc_v6_rcv_saddr.s6_addr32[0]) +
8689 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
8693 case offsetof(struct sk_msg_md, remote_port):
8694 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_dport) != 2);
8696 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8698 si->dst_reg, si->src_reg,
8699 offsetof(struct sk_msg, sk));
8700 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
8701 offsetof(struct sock_common, skc_dport));
8702 #ifndef __BIG_ENDIAN_BITFIELD
8703 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
8707 case offsetof(struct sk_msg_md, local_port):
8708 BUILD_BUG_ON(sizeof_field(struct sock_common, skc_num) != 2);
8710 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8712 si->dst_reg, si->src_reg,
8713 offsetof(struct sk_msg, sk));
8714 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
8715 offsetof(struct sock_common, skc_num));
8718 case offsetof(struct sk_msg_md, size):
8719 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg_sg, size),
8720 si->dst_reg, si->src_reg,
8721 offsetof(struct sk_msg_sg, size));
8724 case offsetof(struct sk_msg_md, sk):
8725 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg, sk),
8726 si->dst_reg, si->src_reg,
8727 offsetof(struct sk_msg, sk));
8731 return insn - insn_buf;
8734 const struct bpf_verifier_ops sk_filter_verifier_ops = {
8735 .get_func_proto = sk_filter_func_proto,
8736 .is_valid_access = sk_filter_is_valid_access,
8737 .convert_ctx_access = bpf_convert_ctx_access,
8738 .gen_ld_abs = bpf_gen_ld_abs,
8741 const struct bpf_prog_ops sk_filter_prog_ops = {
8742 .test_run = bpf_prog_test_run_skb,
8745 const struct bpf_verifier_ops tc_cls_act_verifier_ops = {
8746 .get_func_proto = tc_cls_act_func_proto,
8747 .is_valid_access = tc_cls_act_is_valid_access,
8748 .convert_ctx_access = tc_cls_act_convert_ctx_access,
8749 .gen_prologue = tc_cls_act_prologue,
8750 .gen_ld_abs = bpf_gen_ld_abs,
8753 const struct bpf_prog_ops tc_cls_act_prog_ops = {
8754 .test_run = bpf_prog_test_run_skb,
8757 const struct bpf_verifier_ops xdp_verifier_ops = {
8758 .get_func_proto = xdp_func_proto,
8759 .is_valid_access = xdp_is_valid_access,
8760 .convert_ctx_access = xdp_convert_ctx_access,
8761 .gen_prologue = bpf_noop_prologue,
8764 const struct bpf_prog_ops xdp_prog_ops = {
8765 .test_run = bpf_prog_test_run_xdp,
8768 const struct bpf_verifier_ops cg_skb_verifier_ops = {
8769 .get_func_proto = cg_skb_func_proto,
8770 .is_valid_access = cg_skb_is_valid_access,
8771 .convert_ctx_access = bpf_convert_ctx_access,
8774 const struct bpf_prog_ops cg_skb_prog_ops = {
8775 .test_run = bpf_prog_test_run_skb,
8778 const struct bpf_verifier_ops lwt_in_verifier_ops = {
8779 .get_func_proto = lwt_in_func_proto,
8780 .is_valid_access = lwt_is_valid_access,
8781 .convert_ctx_access = bpf_convert_ctx_access,
8784 const struct bpf_prog_ops lwt_in_prog_ops = {
8785 .test_run = bpf_prog_test_run_skb,
8788 const struct bpf_verifier_ops lwt_out_verifier_ops = {
8789 .get_func_proto = lwt_out_func_proto,
8790 .is_valid_access = lwt_is_valid_access,
8791 .convert_ctx_access = bpf_convert_ctx_access,
8794 const struct bpf_prog_ops lwt_out_prog_ops = {
8795 .test_run = bpf_prog_test_run_skb,
8798 const struct bpf_verifier_ops lwt_xmit_verifier_ops = {
8799 .get_func_proto = lwt_xmit_func_proto,
8800 .is_valid_access = lwt_is_valid_access,
8801 .convert_ctx_access = bpf_convert_ctx_access,
8802 .gen_prologue = tc_cls_act_prologue,
8805 const struct bpf_prog_ops lwt_xmit_prog_ops = {
8806 .test_run = bpf_prog_test_run_skb,
8809 const struct bpf_verifier_ops lwt_seg6local_verifier_ops = {
8810 .get_func_proto = lwt_seg6local_func_proto,
8811 .is_valid_access = lwt_is_valid_access,
8812 .convert_ctx_access = bpf_convert_ctx_access,
8815 const struct bpf_prog_ops lwt_seg6local_prog_ops = {
8816 .test_run = bpf_prog_test_run_skb,
8819 const struct bpf_verifier_ops cg_sock_verifier_ops = {
8820 .get_func_proto = sock_filter_func_proto,
8821 .is_valid_access = sock_filter_is_valid_access,
8822 .convert_ctx_access = bpf_sock_convert_ctx_access,
8825 const struct bpf_prog_ops cg_sock_prog_ops = {
8828 const struct bpf_verifier_ops cg_sock_addr_verifier_ops = {
8829 .get_func_proto = sock_addr_func_proto,
8830 .is_valid_access = sock_addr_is_valid_access,
8831 .convert_ctx_access = sock_addr_convert_ctx_access,
8834 const struct bpf_prog_ops cg_sock_addr_prog_ops = {
8837 const struct bpf_verifier_ops sock_ops_verifier_ops = {
8838 .get_func_proto = sock_ops_func_proto,
8839 .is_valid_access = sock_ops_is_valid_access,
8840 .convert_ctx_access = sock_ops_convert_ctx_access,
8843 const struct bpf_prog_ops sock_ops_prog_ops = {
8846 const struct bpf_verifier_ops sk_skb_verifier_ops = {
8847 .get_func_proto = sk_skb_func_proto,
8848 .is_valid_access = sk_skb_is_valid_access,
8849 .convert_ctx_access = sk_skb_convert_ctx_access,
8850 .gen_prologue = sk_skb_prologue,
8853 const struct bpf_prog_ops sk_skb_prog_ops = {
8856 const struct bpf_verifier_ops sk_msg_verifier_ops = {
8857 .get_func_proto = sk_msg_func_proto,
8858 .is_valid_access = sk_msg_is_valid_access,
8859 .convert_ctx_access = sk_msg_convert_ctx_access,
8860 .gen_prologue = bpf_noop_prologue,
8863 const struct bpf_prog_ops sk_msg_prog_ops = {
8866 const struct bpf_verifier_ops flow_dissector_verifier_ops = {
8867 .get_func_proto = flow_dissector_func_proto,
8868 .is_valid_access = flow_dissector_is_valid_access,
8869 .convert_ctx_access = flow_dissector_convert_ctx_access,
8872 const struct bpf_prog_ops flow_dissector_prog_ops = {
8873 .test_run = bpf_prog_test_run_flow_dissector,
8876 int sk_detach_filter(struct sock *sk)
8879 struct sk_filter *filter;
8881 if (sock_flag(sk, SOCK_FILTER_LOCKED))
8884 filter = rcu_dereference_protected(sk->sk_filter,
8885 lockdep_sock_is_held(sk));
8887 RCU_INIT_POINTER(sk->sk_filter, NULL);
8888 sk_filter_uncharge(sk, filter);
8894 EXPORT_SYMBOL_GPL(sk_detach_filter);
8896 int sk_get_filter(struct sock *sk, struct sock_filter __user *ubuf,
8899 struct sock_fprog_kern *fprog;
8900 struct sk_filter *filter;
8904 filter = rcu_dereference_protected(sk->sk_filter,
8905 lockdep_sock_is_held(sk));
8909 /* We're copying the filter that has been originally attached,
8910 * so no conversion/decode needed anymore. eBPF programs that
8911 * have no original program cannot be dumped through this.
8914 fprog = filter->prog->orig_prog;
8920 /* User space only enquires number of filter blocks. */
8924 if (len < fprog->len)
8928 if (copy_to_user(ubuf, fprog->filter, bpf_classic_proglen(fprog)))
8931 /* Instead of bytes, the API requests to return the number
8941 static void bpf_init_reuseport_kern(struct sk_reuseport_kern *reuse_kern,
8942 struct sock_reuseport *reuse,
8943 struct sock *sk, struct sk_buff *skb,
8946 reuse_kern->skb = skb;
8947 reuse_kern->sk = sk;
8948 reuse_kern->selected_sk = NULL;
8949 reuse_kern->data_end = skb->data + skb_headlen(skb);
8950 reuse_kern->hash = hash;
8951 reuse_kern->reuseport_id = reuse->reuseport_id;
8952 reuse_kern->bind_inany = reuse->bind_inany;
8955 struct sock *bpf_run_sk_reuseport(struct sock_reuseport *reuse, struct sock *sk,
8956 struct bpf_prog *prog, struct sk_buff *skb,
8959 struct sk_reuseport_kern reuse_kern;
8960 enum sk_action action;
8962 bpf_init_reuseport_kern(&reuse_kern, reuse, sk, skb, hash);
8963 action = BPF_PROG_RUN(prog, &reuse_kern);
8965 if (action == SK_PASS)
8966 return reuse_kern.selected_sk;
8968 return ERR_PTR(-ECONNREFUSED);
8971 BPF_CALL_4(sk_select_reuseport, struct sk_reuseport_kern *, reuse_kern,
8972 struct bpf_map *, map, void *, key, u32, flags)
8974 bool is_sockarray = map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY;
8975 struct sock_reuseport *reuse;
8976 struct sock *selected_sk;
8978 selected_sk = map->ops->map_lookup_elem(map, key);
8982 reuse = rcu_dereference(selected_sk->sk_reuseport_cb);
8984 /* Lookup in sock_map can return TCP ESTABLISHED sockets. */
8985 if (sk_is_refcounted(selected_sk))
8986 sock_put(selected_sk);
8988 /* reuseport_array has only sk with non NULL sk_reuseport_cb.
8989 * The only (!reuse) case here is - the sk has already been
8990 * unhashed (e.g. by close()), so treat it as -ENOENT.
8992 * Other maps (e.g. sock_map) do not provide this guarantee and
8993 * the sk may never be in the reuseport group to begin with.
8995 return is_sockarray ? -ENOENT : -EINVAL;
8998 if (unlikely(reuse->reuseport_id != reuse_kern->reuseport_id)) {
8999 struct sock *sk = reuse_kern->sk;
9001 if (sk->sk_protocol != selected_sk->sk_protocol)
9003 else if (sk->sk_family != selected_sk->sk_family)
9004 return -EAFNOSUPPORT;
9006 /* Catch all. Likely bound to a different sockaddr. */
9010 reuse_kern->selected_sk = selected_sk;
9015 static const struct bpf_func_proto sk_select_reuseport_proto = {
9016 .func = sk_select_reuseport,
9018 .ret_type = RET_INTEGER,
9019 .arg1_type = ARG_PTR_TO_CTX,
9020 .arg2_type = ARG_CONST_MAP_PTR,
9021 .arg3_type = ARG_PTR_TO_MAP_KEY,
9022 .arg4_type = ARG_ANYTHING,
9025 BPF_CALL_4(sk_reuseport_load_bytes,
9026 const struct sk_reuseport_kern *, reuse_kern, u32, offset,
9027 void *, to, u32, len)
9029 return ____bpf_skb_load_bytes(reuse_kern->skb, offset, to, len);
9032 static const struct bpf_func_proto sk_reuseport_load_bytes_proto = {
9033 .func = sk_reuseport_load_bytes,
9035 .ret_type = RET_INTEGER,
9036 .arg1_type = ARG_PTR_TO_CTX,
9037 .arg2_type = ARG_ANYTHING,
9038 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
9039 .arg4_type = ARG_CONST_SIZE,
9042 BPF_CALL_5(sk_reuseport_load_bytes_relative,
9043 const struct sk_reuseport_kern *, reuse_kern, u32, offset,
9044 void *, to, u32, len, u32, start_header)
9046 return ____bpf_skb_load_bytes_relative(reuse_kern->skb, offset, to,
9050 static const struct bpf_func_proto sk_reuseport_load_bytes_relative_proto = {
9051 .func = sk_reuseport_load_bytes_relative,
9053 .ret_type = RET_INTEGER,
9054 .arg1_type = ARG_PTR_TO_CTX,
9055 .arg2_type = ARG_ANYTHING,
9056 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
9057 .arg4_type = ARG_CONST_SIZE,
9058 .arg5_type = ARG_ANYTHING,
9061 static const struct bpf_func_proto *
9062 sk_reuseport_func_proto(enum bpf_func_id func_id,
9063 const struct bpf_prog *prog)
9066 case BPF_FUNC_sk_select_reuseport:
9067 return &sk_select_reuseport_proto;
9068 case BPF_FUNC_skb_load_bytes:
9069 return &sk_reuseport_load_bytes_proto;
9070 case BPF_FUNC_skb_load_bytes_relative:
9071 return &sk_reuseport_load_bytes_relative_proto;
9073 return bpf_base_func_proto(func_id);
9078 sk_reuseport_is_valid_access(int off, int size,
9079 enum bpf_access_type type,
9080 const struct bpf_prog *prog,
9081 struct bpf_insn_access_aux *info)
9083 const u32 size_default = sizeof(__u32);
9085 if (off < 0 || off >= sizeof(struct sk_reuseport_md) ||
9086 off % size || type != BPF_READ)
9090 case offsetof(struct sk_reuseport_md, data):
9091 info->reg_type = PTR_TO_PACKET;
9092 return size == sizeof(__u64);
9094 case offsetof(struct sk_reuseport_md, data_end):
9095 info->reg_type = PTR_TO_PACKET_END;
9096 return size == sizeof(__u64);
9098 case offsetof(struct sk_reuseport_md, hash):
9099 return size == size_default;
9101 /* Fields that allow narrowing */
9102 case bpf_ctx_range(struct sk_reuseport_md, eth_protocol):
9103 if (size < sizeof_field(struct sk_buff, protocol))
9106 case bpf_ctx_range(struct sk_reuseport_md, ip_protocol):
9107 case bpf_ctx_range(struct sk_reuseport_md, bind_inany):
9108 case bpf_ctx_range(struct sk_reuseport_md, len):
9109 bpf_ctx_record_field_size(info, size_default);
9110 return bpf_ctx_narrow_access_ok(off, size, size_default);
9117 #define SK_REUSEPORT_LOAD_FIELD(F) ({ \
9118 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_reuseport_kern, F), \
9119 si->dst_reg, si->src_reg, \
9120 bpf_target_off(struct sk_reuseport_kern, F, \
9121 sizeof_field(struct sk_reuseport_kern, F), \
9125 #define SK_REUSEPORT_LOAD_SKB_FIELD(SKB_FIELD) \
9126 SOCK_ADDR_LOAD_NESTED_FIELD(struct sk_reuseport_kern, \
9131 #define SK_REUSEPORT_LOAD_SK_FIELD(SK_FIELD) \
9132 SOCK_ADDR_LOAD_NESTED_FIELD(struct sk_reuseport_kern, \
9137 static u32 sk_reuseport_convert_ctx_access(enum bpf_access_type type,
9138 const struct bpf_insn *si,
9139 struct bpf_insn *insn_buf,
9140 struct bpf_prog *prog,
9143 struct bpf_insn *insn = insn_buf;
9146 case offsetof(struct sk_reuseport_md, data):
9147 SK_REUSEPORT_LOAD_SKB_FIELD(data);
9150 case offsetof(struct sk_reuseport_md, len):
9151 SK_REUSEPORT_LOAD_SKB_FIELD(len);
9154 case offsetof(struct sk_reuseport_md, eth_protocol):
9155 SK_REUSEPORT_LOAD_SKB_FIELD(protocol);
9158 case offsetof(struct sk_reuseport_md, ip_protocol):
9159 SK_REUSEPORT_LOAD_SK_FIELD(sk_protocol);
9162 case offsetof(struct sk_reuseport_md, data_end):
9163 SK_REUSEPORT_LOAD_FIELD(data_end);
9166 case offsetof(struct sk_reuseport_md, hash):
9167 SK_REUSEPORT_LOAD_FIELD(hash);
9170 case offsetof(struct sk_reuseport_md, bind_inany):
9171 SK_REUSEPORT_LOAD_FIELD(bind_inany);
9175 return insn - insn_buf;
9178 const struct bpf_verifier_ops sk_reuseport_verifier_ops = {
9179 .get_func_proto = sk_reuseport_func_proto,
9180 .is_valid_access = sk_reuseport_is_valid_access,
9181 .convert_ctx_access = sk_reuseport_convert_ctx_access,
9184 const struct bpf_prog_ops sk_reuseport_prog_ops = {
9186 #endif /* CONFIG_INET */
9188 DEFINE_BPF_DISPATCHER(xdp)
9190 void bpf_prog_change_xdp(struct bpf_prog *prev_prog, struct bpf_prog *prog)
9192 bpf_dispatcher_change_prog(BPF_DISPATCHER_PTR(xdp), prev_prog, prog);