2 * Linux Socket Filter - Kernel level socket filtering
4 * Based on the design of the Berkeley Packet Filter. The new
5 * internal format has been designed by PLUMgrid:
7 * Copyright (c) 2011 - 2014 PLUMgrid, http://plumgrid.com
11 * Jay Schulist <jschlst@samba.org>
12 * Alexei Starovoitov <ast@plumgrid.com>
13 * Daniel Borkmann <dborkman@redhat.com>
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version
18 * 2 of the License, or (at your option) any later version.
20 * Andi Kleen - Fix a few bad bugs and races.
21 * Kris Katterjohn - Added many additional checks in bpf_check_classic()
24 #include <linux/module.h>
25 #include <linux/types.h>
27 #include <linux/fcntl.h>
28 #include <linux/socket.h>
29 #include <linux/sock_diag.h>
31 #include <linux/inet.h>
32 #include <linux/netdevice.h>
33 #include <linux/if_packet.h>
34 #include <linux/if_arp.h>
35 #include <linux/gfp.h>
36 #include <net/inet_common.h>
38 #include <net/protocol.h>
39 #include <net/netlink.h>
40 #include <linux/skbuff.h>
41 #include <linux/skmsg.h>
43 #include <net/flow_dissector.h>
44 #include <linux/errno.h>
45 #include <linux/timer.h>
46 #include <linux/uaccess.h>
47 #include <asm/unaligned.h>
48 #include <asm/cmpxchg.h>
49 #include <linux/filter.h>
50 #include <linux/ratelimit.h>
51 #include <linux/seccomp.h>
52 #include <linux/if_vlan.h>
53 #include <linux/bpf.h>
54 #include <net/sch_generic.h>
55 #include <net/cls_cgroup.h>
56 #include <net/dst_metadata.h>
58 #include <net/sock_reuseport.h>
59 #include <net/busy_poll.h>
63 #include <linux/bpf_trace.h>
64 #include <net/xdp_sock.h>
65 #include <linux/inetdevice.h>
66 #include <net/inet_hashtables.h>
67 #include <net/inet6_hashtables.h>
68 #include <net/ip_fib.h>
72 #include <net/net_namespace.h>
73 #include <linux/seg6_local.h>
75 #include <net/seg6_local.h>
76 #include <net/lwtunnel.h>
77 #include <net/ipv6_stubs.h>
80 * sk_filter_trim_cap - run a packet through a socket filter
81 * @sk: sock associated with &sk_buff
82 * @skb: buffer to filter
83 * @cap: limit on how short the eBPF program may trim the packet
85 * Run the eBPF program and then cut skb->data to correct size returned by
86 * the program. If pkt_len is 0 we toss packet. If skb->len is smaller
87 * than pkt_len we keep whole skb->data. This is the socket level
88 * wrapper to BPF_PROG_RUN. It returns 0 if the packet should
89 * be accepted or -EPERM if the packet should be tossed.
92 int sk_filter_trim_cap(struct sock *sk, struct sk_buff *skb, unsigned int cap)
95 struct sk_filter *filter;
98 * If the skb was allocated from pfmemalloc reserves, only
99 * allow SOCK_MEMALLOC sockets to use it as this socket is
100 * helping free memory
102 if (skb_pfmemalloc(skb) && !sock_flag(sk, SOCK_MEMALLOC)) {
103 NET_INC_STATS(sock_net(sk), LINUX_MIB_PFMEMALLOCDROP);
106 err = BPF_CGROUP_RUN_PROG_INET_INGRESS(sk, skb);
110 err = security_sock_rcv_skb(sk, skb);
115 filter = rcu_dereference(sk->sk_filter);
117 struct sock *save_sk = skb->sk;
118 unsigned int pkt_len;
121 pkt_len = bpf_prog_run_save_cb(filter->prog, skb);
123 err = pkt_len ? pskb_trim(skb, max(cap, pkt_len)) : -EPERM;
129 EXPORT_SYMBOL(sk_filter_trim_cap);
131 BPF_CALL_1(bpf_skb_get_pay_offset, struct sk_buff *, skb)
133 return skb_get_poff(skb);
136 BPF_CALL_3(bpf_skb_get_nlattr, struct sk_buff *, skb, u32, a, u32, x)
140 if (skb_is_nonlinear(skb))
143 if (skb->len < sizeof(struct nlattr))
146 if (a > skb->len - sizeof(struct nlattr))
149 nla = nla_find((struct nlattr *) &skb->data[a], skb->len - a, x);
151 return (void *) nla - (void *) skb->data;
156 BPF_CALL_3(bpf_skb_get_nlattr_nest, struct sk_buff *, skb, u32, a, u32, x)
160 if (skb_is_nonlinear(skb))
163 if (skb->len < sizeof(struct nlattr))
166 if (a > skb->len - sizeof(struct nlattr))
169 nla = (struct nlattr *) &skb->data[a];
170 if (nla->nla_len > skb->len - a)
173 nla = nla_find_nested(nla, x);
175 return (void *) nla - (void *) skb->data;
180 BPF_CALL_4(bpf_skb_load_helper_8, const struct sk_buff *, skb, const void *,
181 data, int, headlen, int, offset)
184 const int len = sizeof(tmp);
187 if (headlen - offset >= len)
188 return *(u8 *)(data + offset);
189 if (!skb_copy_bits(skb, offset, &tmp, sizeof(tmp)))
192 ptr = bpf_internal_load_pointer_neg_helper(skb, offset, len);
200 BPF_CALL_2(bpf_skb_load_helper_8_no_cache, const struct sk_buff *, skb,
203 return ____bpf_skb_load_helper_8(skb, skb->data, skb->len - skb->data_len,
207 BPF_CALL_4(bpf_skb_load_helper_16, const struct sk_buff *, skb, const void *,
208 data, int, headlen, int, offset)
211 const int len = sizeof(tmp);
214 if (headlen - offset >= len)
215 return get_unaligned_be16(data + offset);
216 if (!skb_copy_bits(skb, offset, &tmp, sizeof(tmp)))
217 return be16_to_cpu(tmp);
219 ptr = bpf_internal_load_pointer_neg_helper(skb, offset, len);
221 return get_unaligned_be16(ptr);
227 BPF_CALL_2(bpf_skb_load_helper_16_no_cache, const struct sk_buff *, skb,
230 return ____bpf_skb_load_helper_16(skb, skb->data, skb->len - skb->data_len,
234 BPF_CALL_4(bpf_skb_load_helper_32, const struct sk_buff *, skb, const void *,
235 data, int, headlen, int, offset)
238 const int len = sizeof(tmp);
240 if (likely(offset >= 0)) {
241 if (headlen - offset >= len)
242 return get_unaligned_be32(data + offset);
243 if (!skb_copy_bits(skb, offset, &tmp, sizeof(tmp)))
244 return be32_to_cpu(tmp);
246 ptr = bpf_internal_load_pointer_neg_helper(skb, offset, len);
248 return get_unaligned_be32(ptr);
254 BPF_CALL_2(bpf_skb_load_helper_32_no_cache, const struct sk_buff *, skb,
257 return ____bpf_skb_load_helper_32(skb, skb->data, skb->len - skb->data_len,
261 BPF_CALL_0(bpf_get_raw_cpu_id)
263 return raw_smp_processor_id();
266 static const struct bpf_func_proto bpf_get_raw_smp_processor_id_proto = {
267 .func = bpf_get_raw_cpu_id,
269 .ret_type = RET_INTEGER,
272 static u32 convert_skb_access(int skb_field, int dst_reg, int src_reg,
273 struct bpf_insn *insn_buf)
275 struct bpf_insn *insn = insn_buf;
279 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
281 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
282 offsetof(struct sk_buff, mark));
286 *insn++ = BPF_LDX_MEM(BPF_B, dst_reg, src_reg, PKT_TYPE_OFFSET());
287 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, PKT_TYPE_MAX);
288 #ifdef __BIG_ENDIAN_BITFIELD
289 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 5);
294 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, queue_mapping) != 2);
296 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
297 offsetof(struct sk_buff, queue_mapping));
300 case SKF_AD_VLAN_TAG:
301 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_tci) != 2);
303 /* dst_reg = *(u16 *) (src_reg + offsetof(vlan_tci)) */
304 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
305 offsetof(struct sk_buff, vlan_tci));
307 case SKF_AD_VLAN_TAG_PRESENT:
308 *insn++ = BPF_LDX_MEM(BPF_B, dst_reg, src_reg, PKT_VLAN_PRESENT_OFFSET());
309 if (PKT_VLAN_PRESENT_BIT)
310 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, PKT_VLAN_PRESENT_BIT);
311 if (PKT_VLAN_PRESENT_BIT < 7)
312 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, 1);
316 return insn - insn_buf;
319 static bool convert_bpf_extensions(struct sock_filter *fp,
320 struct bpf_insn **insnp)
322 struct bpf_insn *insn = *insnp;
326 case SKF_AD_OFF + SKF_AD_PROTOCOL:
327 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2);
329 /* A = *(u16 *) (CTX + offsetof(protocol)) */
330 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
331 offsetof(struct sk_buff, protocol));
332 /* A = ntohs(A) [emitting a nop or swap16] */
333 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
336 case SKF_AD_OFF + SKF_AD_PKTTYPE:
337 cnt = convert_skb_access(SKF_AD_PKTTYPE, BPF_REG_A, BPF_REG_CTX, insn);
341 case SKF_AD_OFF + SKF_AD_IFINDEX:
342 case SKF_AD_OFF + SKF_AD_HATYPE:
343 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4);
344 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, type) != 2);
346 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
347 BPF_REG_TMP, BPF_REG_CTX,
348 offsetof(struct sk_buff, dev));
349 /* if (tmp != 0) goto pc + 1 */
350 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_TMP, 0, 1);
351 *insn++ = BPF_EXIT_INSN();
352 if (fp->k == SKF_AD_OFF + SKF_AD_IFINDEX)
353 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_TMP,
354 offsetof(struct net_device, ifindex));
356 *insn = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_TMP,
357 offsetof(struct net_device, type));
360 case SKF_AD_OFF + SKF_AD_MARK:
361 cnt = convert_skb_access(SKF_AD_MARK, BPF_REG_A, BPF_REG_CTX, insn);
365 case SKF_AD_OFF + SKF_AD_RXHASH:
366 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4);
368 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX,
369 offsetof(struct sk_buff, hash));
372 case SKF_AD_OFF + SKF_AD_QUEUE:
373 cnt = convert_skb_access(SKF_AD_QUEUE, BPF_REG_A, BPF_REG_CTX, insn);
377 case SKF_AD_OFF + SKF_AD_VLAN_TAG:
378 cnt = convert_skb_access(SKF_AD_VLAN_TAG,
379 BPF_REG_A, BPF_REG_CTX, insn);
383 case SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT:
384 cnt = convert_skb_access(SKF_AD_VLAN_TAG_PRESENT,
385 BPF_REG_A, BPF_REG_CTX, insn);
389 case SKF_AD_OFF + SKF_AD_VLAN_TPID:
390 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_proto) != 2);
392 /* A = *(u16 *) (CTX + offsetof(vlan_proto)) */
393 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
394 offsetof(struct sk_buff, vlan_proto));
395 /* A = ntohs(A) [emitting a nop or swap16] */
396 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
399 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
400 case SKF_AD_OFF + SKF_AD_NLATTR:
401 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
402 case SKF_AD_OFF + SKF_AD_CPU:
403 case SKF_AD_OFF + SKF_AD_RANDOM:
405 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX);
407 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_A);
409 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_X);
410 /* Emit call(arg1=CTX, arg2=A, arg3=X) */
412 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
413 *insn = BPF_EMIT_CALL(bpf_skb_get_pay_offset);
415 case SKF_AD_OFF + SKF_AD_NLATTR:
416 *insn = BPF_EMIT_CALL(bpf_skb_get_nlattr);
418 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
419 *insn = BPF_EMIT_CALL(bpf_skb_get_nlattr_nest);
421 case SKF_AD_OFF + SKF_AD_CPU:
422 *insn = BPF_EMIT_CALL(bpf_get_raw_cpu_id);
424 case SKF_AD_OFF + SKF_AD_RANDOM:
425 *insn = BPF_EMIT_CALL(bpf_user_rnd_u32);
426 bpf_user_rnd_init_once();
431 case SKF_AD_OFF + SKF_AD_ALU_XOR_X:
433 *insn = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_X);
437 /* This is just a dummy call to avoid letting the compiler
438 * evict __bpf_call_base() as an optimization. Placed here
439 * where no-one bothers.
441 BUG_ON(__bpf_call_base(0, 0, 0, 0, 0) != 0);
449 static bool convert_bpf_ld_abs(struct sock_filter *fp, struct bpf_insn **insnp)
451 const bool unaligned_ok = IS_BUILTIN(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS);
452 int size = bpf_size_to_bytes(BPF_SIZE(fp->code));
453 bool endian = BPF_SIZE(fp->code) == BPF_H ||
454 BPF_SIZE(fp->code) == BPF_W;
455 bool indirect = BPF_MODE(fp->code) == BPF_IND;
456 const int ip_align = NET_IP_ALIGN;
457 struct bpf_insn *insn = *insnp;
461 ((unaligned_ok && offset >= 0) ||
462 (!unaligned_ok && offset >= 0 &&
463 offset + ip_align >= 0 &&
464 offset + ip_align % size == 0))) {
465 bool ldx_off_ok = offset <= S16_MAX;
467 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_H);
469 *insn++ = BPF_ALU64_IMM(BPF_SUB, BPF_REG_TMP, offset);
470 *insn++ = BPF_JMP_IMM(BPF_JSLT, BPF_REG_TMP,
471 size, 2 + endian + (!ldx_off_ok * 2));
473 *insn++ = BPF_LDX_MEM(BPF_SIZE(fp->code), BPF_REG_A,
476 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_D);
477 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_TMP, offset);
478 *insn++ = BPF_LDX_MEM(BPF_SIZE(fp->code), BPF_REG_A,
482 *insn++ = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, size * 8);
483 *insn++ = BPF_JMP_A(8);
486 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX);
487 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_D);
488 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_H);
490 *insn++ = BPF_MOV64_IMM(BPF_REG_ARG4, offset);
492 *insn++ = BPF_MOV64_REG(BPF_REG_ARG4, BPF_REG_X);
494 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_ARG4, offset);
497 switch (BPF_SIZE(fp->code)) {
499 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_8);
502 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_16);
505 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_32);
511 *insn++ = BPF_JMP_IMM(BPF_JSGE, BPF_REG_A, 0, 2);
512 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
513 *insn = BPF_EXIT_INSN();
520 * bpf_convert_filter - convert filter program
521 * @prog: the user passed filter program
522 * @len: the length of the user passed filter program
523 * @new_prog: allocated 'struct bpf_prog' or NULL
524 * @new_len: pointer to store length of converted program
525 * @seen_ld_abs: bool whether we've seen ld_abs/ind
527 * Remap 'sock_filter' style classic BPF (cBPF) instruction set to 'bpf_insn'
528 * style extended BPF (eBPF).
529 * Conversion workflow:
531 * 1) First pass for calculating the new program length:
532 * bpf_convert_filter(old_prog, old_len, NULL, &new_len, &seen_ld_abs)
534 * 2) 2nd pass to remap in two passes: 1st pass finds new
535 * jump offsets, 2nd pass remapping:
536 * bpf_convert_filter(old_prog, old_len, new_prog, &new_len, &seen_ld_abs)
538 static int bpf_convert_filter(struct sock_filter *prog, int len,
539 struct bpf_prog *new_prog, int *new_len,
542 int new_flen = 0, pass = 0, target, i, stack_off;
543 struct bpf_insn *new_insn, *first_insn = NULL;
544 struct sock_filter *fp;
548 BUILD_BUG_ON(BPF_MEMWORDS * sizeof(u32) > MAX_BPF_STACK);
549 BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG);
551 if (len <= 0 || len > BPF_MAXINSNS)
555 first_insn = new_prog->insnsi;
556 addrs = kcalloc(len, sizeof(*addrs),
557 GFP_KERNEL | __GFP_NOWARN);
563 new_insn = first_insn;
566 /* Classic BPF related prologue emission. */
568 /* Classic BPF expects A and X to be reset first. These need
569 * to be guaranteed to be the first two instructions.
571 *new_insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
572 *new_insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_X, BPF_REG_X);
574 /* All programs must keep CTX in callee saved BPF_REG_CTX.
575 * In eBPF case it's done by the compiler, here we need to
576 * do this ourself. Initial CTX is present in BPF_REG_ARG1.
578 *new_insn++ = BPF_MOV64_REG(BPF_REG_CTX, BPF_REG_ARG1);
580 /* For packet access in classic BPF, cache skb->data
581 * in callee-saved BPF R8 and skb->len - skb->data_len
582 * (headlen) in BPF R9. Since classic BPF is read-only
583 * on CTX, we only need to cache it once.
585 *new_insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
586 BPF_REG_D, BPF_REG_CTX,
587 offsetof(struct sk_buff, data));
588 *new_insn++ = BPF_LDX_MEM(BPF_W, BPF_REG_H, BPF_REG_CTX,
589 offsetof(struct sk_buff, len));
590 *new_insn++ = BPF_LDX_MEM(BPF_W, BPF_REG_TMP, BPF_REG_CTX,
591 offsetof(struct sk_buff, data_len));
592 *new_insn++ = BPF_ALU32_REG(BPF_SUB, BPF_REG_H, BPF_REG_TMP);
598 for (i = 0; i < len; fp++, i++) {
599 struct bpf_insn tmp_insns[32] = { };
600 struct bpf_insn *insn = tmp_insns;
603 addrs[i] = new_insn - first_insn;
606 /* All arithmetic insns and skb loads map as-is. */
607 case BPF_ALU | BPF_ADD | BPF_X:
608 case BPF_ALU | BPF_ADD | BPF_K:
609 case BPF_ALU | BPF_SUB | BPF_X:
610 case BPF_ALU | BPF_SUB | BPF_K:
611 case BPF_ALU | BPF_AND | BPF_X:
612 case BPF_ALU | BPF_AND | BPF_K:
613 case BPF_ALU | BPF_OR | BPF_X:
614 case BPF_ALU | BPF_OR | BPF_K:
615 case BPF_ALU | BPF_LSH | BPF_X:
616 case BPF_ALU | BPF_LSH | BPF_K:
617 case BPF_ALU | BPF_RSH | BPF_X:
618 case BPF_ALU | BPF_RSH | BPF_K:
619 case BPF_ALU | BPF_XOR | BPF_X:
620 case BPF_ALU | BPF_XOR | BPF_K:
621 case BPF_ALU | BPF_MUL | BPF_X:
622 case BPF_ALU | BPF_MUL | BPF_K:
623 case BPF_ALU | BPF_DIV | BPF_X:
624 case BPF_ALU | BPF_DIV | BPF_K:
625 case BPF_ALU | BPF_MOD | BPF_X:
626 case BPF_ALU | BPF_MOD | BPF_K:
627 case BPF_ALU | BPF_NEG:
628 case BPF_LD | BPF_ABS | BPF_W:
629 case BPF_LD | BPF_ABS | BPF_H:
630 case BPF_LD | BPF_ABS | BPF_B:
631 case BPF_LD | BPF_IND | BPF_W:
632 case BPF_LD | BPF_IND | BPF_H:
633 case BPF_LD | BPF_IND | BPF_B:
634 /* Check for overloaded BPF extension and
635 * directly convert it if found, otherwise
636 * just move on with mapping.
638 if (BPF_CLASS(fp->code) == BPF_LD &&
639 BPF_MODE(fp->code) == BPF_ABS &&
640 convert_bpf_extensions(fp, &insn))
642 if (BPF_CLASS(fp->code) == BPF_LD &&
643 convert_bpf_ld_abs(fp, &insn)) {
648 if (fp->code == (BPF_ALU | BPF_DIV | BPF_X) ||
649 fp->code == (BPF_ALU | BPF_MOD | BPF_X)) {
650 *insn++ = BPF_MOV32_REG(BPF_REG_X, BPF_REG_X);
651 /* Error with exception code on div/mod by 0.
652 * For cBPF programs, this was always return 0.
654 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_X, 0, 2);
655 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
656 *insn++ = BPF_EXIT_INSN();
659 *insn = BPF_RAW_INSN(fp->code, BPF_REG_A, BPF_REG_X, 0, fp->k);
662 /* Jump transformation cannot use BPF block macros
663 * everywhere as offset calculation and target updates
664 * require a bit more work than the rest, i.e. jump
665 * opcodes map as-is, but offsets need adjustment.
668 #define BPF_EMIT_JMP \
670 const s32 off_min = S16_MIN, off_max = S16_MAX; \
673 if (target >= len || target < 0) \
675 off = addrs ? addrs[target] - addrs[i] - 1 : 0; \
676 /* Adjust pc relative offset for 2nd or 3rd insn. */ \
677 off -= insn - tmp_insns; \
678 /* Reject anything not fitting into insn->off. */ \
679 if (off < off_min || off > off_max) \
684 case BPF_JMP | BPF_JA:
685 target = i + fp->k + 1;
686 insn->code = fp->code;
690 case BPF_JMP | BPF_JEQ | BPF_K:
691 case BPF_JMP | BPF_JEQ | BPF_X:
692 case BPF_JMP | BPF_JSET | BPF_K:
693 case BPF_JMP | BPF_JSET | BPF_X:
694 case BPF_JMP | BPF_JGT | BPF_K:
695 case BPF_JMP | BPF_JGT | BPF_X:
696 case BPF_JMP | BPF_JGE | BPF_K:
697 case BPF_JMP | BPF_JGE | BPF_X:
698 if (BPF_SRC(fp->code) == BPF_K && (int) fp->k < 0) {
699 /* BPF immediates are signed, zero extend
700 * immediate into tmp register and use it
703 *insn++ = BPF_MOV32_IMM(BPF_REG_TMP, fp->k);
705 insn->dst_reg = BPF_REG_A;
706 insn->src_reg = BPF_REG_TMP;
709 insn->dst_reg = BPF_REG_A;
711 bpf_src = BPF_SRC(fp->code);
712 insn->src_reg = bpf_src == BPF_X ? BPF_REG_X : 0;
715 /* Common case where 'jump_false' is next insn. */
717 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
718 target = i + fp->jt + 1;
723 /* Convert some jumps when 'jump_true' is next insn. */
725 switch (BPF_OP(fp->code)) {
727 insn->code = BPF_JMP | BPF_JNE | bpf_src;
730 insn->code = BPF_JMP | BPF_JLE | bpf_src;
733 insn->code = BPF_JMP | BPF_JLT | bpf_src;
739 target = i + fp->jf + 1;
744 /* Other jumps are mapped into two insns: Jxx and JA. */
745 target = i + fp->jt + 1;
746 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
750 insn->code = BPF_JMP | BPF_JA;
751 target = i + fp->jf + 1;
755 /* ldxb 4 * ([14] & 0xf) is remaped into 6 insns. */
756 case BPF_LDX | BPF_MSH | BPF_B: {
757 struct sock_filter tmp = {
758 .code = BPF_LD | BPF_ABS | BPF_B,
765 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
766 /* A = BPF_R0 = *(u8 *) (skb->data + K) */
767 convert_bpf_ld_abs(&tmp, &insn);
770 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, 0xf);
772 *insn++ = BPF_ALU32_IMM(BPF_LSH, BPF_REG_A, 2);
774 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_X);
776 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
778 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_TMP);
781 /* RET_K is remaped into 2 insns. RET_A case doesn't need an
782 * extra mov as BPF_REG_0 is already mapped into BPF_REG_A.
784 case BPF_RET | BPF_A:
785 case BPF_RET | BPF_K:
786 if (BPF_RVAL(fp->code) == BPF_K)
787 *insn++ = BPF_MOV32_RAW(BPF_K, BPF_REG_0,
789 *insn = BPF_EXIT_INSN();
792 /* Store to stack. */
795 stack_off = fp->k * 4 + 4;
796 *insn = BPF_STX_MEM(BPF_W, BPF_REG_FP, BPF_CLASS(fp->code) ==
797 BPF_ST ? BPF_REG_A : BPF_REG_X,
799 /* check_load_and_stores() verifies that classic BPF can
800 * load from stack only after write, so tracking
801 * stack_depth for ST|STX insns is enough
803 if (new_prog && new_prog->aux->stack_depth < stack_off)
804 new_prog->aux->stack_depth = stack_off;
807 /* Load from stack. */
808 case BPF_LD | BPF_MEM:
809 case BPF_LDX | BPF_MEM:
810 stack_off = fp->k * 4 + 4;
811 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
812 BPF_REG_A : BPF_REG_X, BPF_REG_FP,
817 case BPF_LD | BPF_IMM:
818 case BPF_LDX | BPF_IMM:
819 *insn = BPF_MOV32_IMM(BPF_CLASS(fp->code) == BPF_LD ?
820 BPF_REG_A : BPF_REG_X, fp->k);
824 case BPF_MISC | BPF_TAX:
825 *insn = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
829 case BPF_MISC | BPF_TXA:
830 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_X);
833 /* A = skb->len or X = skb->len */
834 case BPF_LD | BPF_W | BPF_LEN:
835 case BPF_LDX | BPF_W | BPF_LEN:
836 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
837 BPF_REG_A : BPF_REG_X, BPF_REG_CTX,
838 offsetof(struct sk_buff, len));
841 /* Access seccomp_data fields. */
842 case BPF_LDX | BPF_ABS | BPF_W:
843 /* A = *(u32 *) (ctx + K) */
844 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX, fp->k);
847 /* Unknown instruction. */
854 memcpy(new_insn, tmp_insns,
855 sizeof(*insn) * (insn - tmp_insns));
856 new_insn += insn - tmp_insns;
860 /* Only calculating new length. */
861 *new_len = new_insn - first_insn;
863 *new_len += 4; /* Prologue bits. */
868 if (new_flen != new_insn - first_insn) {
869 new_flen = new_insn - first_insn;
876 BUG_ON(*new_len != new_flen);
885 * As we dont want to clear mem[] array for each packet going through
886 * __bpf_prog_run(), we check that filter loaded by user never try to read
887 * a cell if not previously written, and we check all branches to be sure
888 * a malicious user doesn't try to abuse us.
890 static int check_load_and_stores(const struct sock_filter *filter, int flen)
892 u16 *masks, memvalid = 0; /* One bit per cell, 16 cells */
895 BUILD_BUG_ON(BPF_MEMWORDS > 16);
897 masks = kmalloc_array(flen, sizeof(*masks), GFP_KERNEL);
901 memset(masks, 0xff, flen * sizeof(*masks));
903 for (pc = 0; pc < flen; pc++) {
904 memvalid &= masks[pc];
906 switch (filter[pc].code) {
909 memvalid |= (1 << filter[pc].k);
911 case BPF_LD | BPF_MEM:
912 case BPF_LDX | BPF_MEM:
913 if (!(memvalid & (1 << filter[pc].k))) {
918 case BPF_JMP | BPF_JA:
919 /* A jump must set masks on target */
920 masks[pc + 1 + filter[pc].k] &= memvalid;
923 case BPF_JMP | BPF_JEQ | BPF_K:
924 case BPF_JMP | BPF_JEQ | BPF_X:
925 case BPF_JMP | BPF_JGE | BPF_K:
926 case BPF_JMP | BPF_JGE | BPF_X:
927 case BPF_JMP | BPF_JGT | BPF_K:
928 case BPF_JMP | BPF_JGT | BPF_X:
929 case BPF_JMP | BPF_JSET | BPF_K:
930 case BPF_JMP | BPF_JSET | BPF_X:
931 /* A jump must set masks on targets */
932 masks[pc + 1 + filter[pc].jt] &= memvalid;
933 masks[pc + 1 + filter[pc].jf] &= memvalid;
943 static bool chk_code_allowed(u16 code_to_probe)
945 static const bool codes[] = {
946 /* 32 bit ALU operations */
947 [BPF_ALU | BPF_ADD | BPF_K] = true,
948 [BPF_ALU | BPF_ADD | BPF_X] = true,
949 [BPF_ALU | BPF_SUB | BPF_K] = true,
950 [BPF_ALU | BPF_SUB | BPF_X] = true,
951 [BPF_ALU | BPF_MUL | BPF_K] = true,
952 [BPF_ALU | BPF_MUL | BPF_X] = true,
953 [BPF_ALU | BPF_DIV | BPF_K] = true,
954 [BPF_ALU | BPF_DIV | BPF_X] = true,
955 [BPF_ALU | BPF_MOD | BPF_K] = true,
956 [BPF_ALU | BPF_MOD | BPF_X] = true,
957 [BPF_ALU | BPF_AND | BPF_K] = true,
958 [BPF_ALU | BPF_AND | BPF_X] = true,
959 [BPF_ALU | BPF_OR | BPF_K] = true,
960 [BPF_ALU | BPF_OR | BPF_X] = true,
961 [BPF_ALU | BPF_XOR | BPF_K] = true,
962 [BPF_ALU | BPF_XOR | BPF_X] = true,
963 [BPF_ALU | BPF_LSH | BPF_K] = true,
964 [BPF_ALU | BPF_LSH | BPF_X] = true,
965 [BPF_ALU | BPF_RSH | BPF_K] = true,
966 [BPF_ALU | BPF_RSH | BPF_X] = true,
967 [BPF_ALU | BPF_NEG] = true,
968 /* Load instructions */
969 [BPF_LD | BPF_W | BPF_ABS] = true,
970 [BPF_LD | BPF_H | BPF_ABS] = true,
971 [BPF_LD | BPF_B | BPF_ABS] = true,
972 [BPF_LD | BPF_W | BPF_LEN] = true,
973 [BPF_LD | BPF_W | BPF_IND] = true,
974 [BPF_LD | BPF_H | BPF_IND] = true,
975 [BPF_LD | BPF_B | BPF_IND] = true,
976 [BPF_LD | BPF_IMM] = true,
977 [BPF_LD | BPF_MEM] = true,
978 [BPF_LDX | BPF_W | BPF_LEN] = true,
979 [BPF_LDX | BPF_B | BPF_MSH] = true,
980 [BPF_LDX | BPF_IMM] = true,
981 [BPF_LDX | BPF_MEM] = true,
982 /* Store instructions */
985 /* Misc instructions */
986 [BPF_MISC | BPF_TAX] = true,
987 [BPF_MISC | BPF_TXA] = true,
988 /* Return instructions */
989 [BPF_RET | BPF_K] = true,
990 [BPF_RET | BPF_A] = true,
991 /* Jump instructions */
992 [BPF_JMP | BPF_JA] = true,
993 [BPF_JMP | BPF_JEQ | BPF_K] = true,
994 [BPF_JMP | BPF_JEQ | BPF_X] = true,
995 [BPF_JMP | BPF_JGE | BPF_K] = true,
996 [BPF_JMP | BPF_JGE | BPF_X] = true,
997 [BPF_JMP | BPF_JGT | BPF_K] = true,
998 [BPF_JMP | BPF_JGT | BPF_X] = true,
999 [BPF_JMP | BPF_JSET | BPF_K] = true,
1000 [BPF_JMP | BPF_JSET | BPF_X] = true,
1003 if (code_to_probe >= ARRAY_SIZE(codes))
1006 return codes[code_to_probe];
1009 static bool bpf_check_basics_ok(const struct sock_filter *filter,
1014 if (flen == 0 || flen > BPF_MAXINSNS)
1021 * bpf_check_classic - verify socket filter code
1022 * @filter: filter to verify
1023 * @flen: length of filter
1025 * Check the user's filter code. If we let some ugly
1026 * filter code slip through kaboom! The filter must contain
1027 * no references or jumps that are out of range, no illegal
1028 * instructions, and must end with a RET instruction.
1030 * All jumps are forward as they are not signed.
1032 * Returns 0 if the rule set is legal or -EINVAL if not.
1034 static int bpf_check_classic(const struct sock_filter *filter,
1040 /* Check the filter code now */
1041 for (pc = 0; pc < flen; pc++) {
1042 const struct sock_filter *ftest = &filter[pc];
1044 /* May we actually operate on this code? */
1045 if (!chk_code_allowed(ftest->code))
1048 /* Some instructions need special checks */
1049 switch (ftest->code) {
1050 case BPF_ALU | BPF_DIV | BPF_K:
1051 case BPF_ALU | BPF_MOD | BPF_K:
1052 /* Check for division by zero */
1056 case BPF_ALU | BPF_LSH | BPF_K:
1057 case BPF_ALU | BPF_RSH | BPF_K:
1061 case BPF_LD | BPF_MEM:
1062 case BPF_LDX | BPF_MEM:
1065 /* Check for invalid memory addresses */
1066 if (ftest->k >= BPF_MEMWORDS)
1069 case BPF_JMP | BPF_JA:
1070 /* Note, the large ftest->k might cause loops.
1071 * Compare this with conditional jumps below,
1072 * where offsets are limited. --ANK (981016)
1074 if (ftest->k >= (unsigned int)(flen - pc - 1))
1077 case BPF_JMP | BPF_JEQ | BPF_K:
1078 case BPF_JMP | BPF_JEQ | BPF_X:
1079 case BPF_JMP | BPF_JGE | BPF_K:
1080 case BPF_JMP | BPF_JGE | BPF_X:
1081 case BPF_JMP | BPF_JGT | BPF_K:
1082 case BPF_JMP | BPF_JGT | BPF_X:
1083 case BPF_JMP | BPF_JSET | BPF_K:
1084 case BPF_JMP | BPF_JSET | BPF_X:
1085 /* Both conditionals must be safe */
1086 if (pc + ftest->jt + 1 >= flen ||
1087 pc + ftest->jf + 1 >= flen)
1090 case BPF_LD | BPF_W | BPF_ABS:
1091 case BPF_LD | BPF_H | BPF_ABS:
1092 case BPF_LD | BPF_B | BPF_ABS:
1094 if (bpf_anc_helper(ftest) & BPF_ANC)
1096 /* Ancillary operation unknown or unsupported */
1097 if (anc_found == false && ftest->k >= SKF_AD_OFF)
1102 /* Last instruction must be a RET code */
1103 switch (filter[flen - 1].code) {
1104 case BPF_RET | BPF_K:
1105 case BPF_RET | BPF_A:
1106 return check_load_and_stores(filter, flen);
1112 static int bpf_prog_store_orig_filter(struct bpf_prog *fp,
1113 const struct sock_fprog *fprog)
1115 unsigned int fsize = bpf_classic_proglen(fprog);
1116 struct sock_fprog_kern *fkprog;
1118 fp->orig_prog = kmalloc(sizeof(*fkprog), GFP_KERNEL);
1122 fkprog = fp->orig_prog;
1123 fkprog->len = fprog->len;
1125 fkprog->filter = kmemdup(fp->insns, fsize,
1126 GFP_KERNEL | __GFP_NOWARN);
1127 if (!fkprog->filter) {
1128 kfree(fp->orig_prog);
1135 static void bpf_release_orig_filter(struct bpf_prog *fp)
1137 struct sock_fprog_kern *fprog = fp->orig_prog;
1140 kfree(fprog->filter);
1145 static void __bpf_prog_release(struct bpf_prog *prog)
1147 if (prog->type == BPF_PROG_TYPE_SOCKET_FILTER) {
1150 bpf_release_orig_filter(prog);
1151 bpf_prog_free(prog);
1155 static void __sk_filter_release(struct sk_filter *fp)
1157 __bpf_prog_release(fp->prog);
1162 * sk_filter_release_rcu - Release a socket filter by rcu_head
1163 * @rcu: rcu_head that contains the sk_filter to free
1165 static void sk_filter_release_rcu(struct rcu_head *rcu)
1167 struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu);
1169 __sk_filter_release(fp);
1173 * sk_filter_release - release a socket filter
1174 * @fp: filter to remove
1176 * Remove a filter from a socket and release its resources.
1178 static void sk_filter_release(struct sk_filter *fp)
1180 if (refcount_dec_and_test(&fp->refcnt))
1181 call_rcu(&fp->rcu, sk_filter_release_rcu);
1184 void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp)
1186 u32 filter_size = bpf_prog_size(fp->prog->len);
1188 atomic_sub(filter_size, &sk->sk_omem_alloc);
1189 sk_filter_release(fp);
1192 /* try to charge the socket memory if there is space available
1193 * return true on success
1195 static bool __sk_filter_charge(struct sock *sk, struct sk_filter *fp)
1197 u32 filter_size = bpf_prog_size(fp->prog->len);
1199 /* same check as in sock_kmalloc() */
1200 if (filter_size <= sysctl_optmem_max &&
1201 atomic_read(&sk->sk_omem_alloc) + filter_size < sysctl_optmem_max) {
1202 atomic_add(filter_size, &sk->sk_omem_alloc);
1208 bool sk_filter_charge(struct sock *sk, struct sk_filter *fp)
1210 if (!refcount_inc_not_zero(&fp->refcnt))
1213 if (!__sk_filter_charge(sk, fp)) {
1214 sk_filter_release(fp);
1220 static struct bpf_prog *bpf_migrate_filter(struct bpf_prog *fp)
1222 struct sock_filter *old_prog;
1223 struct bpf_prog *old_fp;
1224 int err, new_len, old_len = fp->len;
1225 bool seen_ld_abs = false;
1227 /* We are free to overwrite insns et al right here as it
1228 * won't be used at this point in time anymore internally
1229 * after the migration to the internal BPF instruction
1232 BUILD_BUG_ON(sizeof(struct sock_filter) !=
1233 sizeof(struct bpf_insn));
1235 /* Conversion cannot happen on overlapping memory areas,
1236 * so we need to keep the user BPF around until the 2nd
1237 * pass. At this time, the user BPF is stored in fp->insns.
1239 old_prog = kmemdup(fp->insns, old_len * sizeof(struct sock_filter),
1240 GFP_KERNEL | __GFP_NOWARN);
1246 /* 1st pass: calculate the new program length. */
1247 err = bpf_convert_filter(old_prog, old_len, NULL, &new_len,
1252 /* Expand fp for appending the new filter representation. */
1254 fp = bpf_prog_realloc(old_fp, bpf_prog_size(new_len), 0);
1256 /* The old_fp is still around in case we couldn't
1257 * allocate new memory, so uncharge on that one.
1266 /* 2nd pass: remap sock_filter insns into bpf_insn insns. */
1267 err = bpf_convert_filter(old_prog, old_len, fp, &new_len,
1270 /* 2nd bpf_convert_filter() can fail only if it fails
1271 * to allocate memory, remapping must succeed. Note,
1272 * that at this time old_fp has already been released
1277 fp = bpf_prog_select_runtime(fp, &err);
1287 __bpf_prog_release(fp);
1288 return ERR_PTR(err);
1291 static struct bpf_prog *bpf_prepare_filter(struct bpf_prog *fp,
1292 bpf_aux_classic_check_t trans)
1296 fp->bpf_func = NULL;
1299 err = bpf_check_classic(fp->insns, fp->len);
1301 __bpf_prog_release(fp);
1302 return ERR_PTR(err);
1305 /* There might be additional checks and transformations
1306 * needed on classic filters, f.e. in case of seccomp.
1309 err = trans(fp->insns, fp->len);
1311 __bpf_prog_release(fp);
1312 return ERR_PTR(err);
1316 /* Probe if we can JIT compile the filter and if so, do
1317 * the compilation of the filter.
1319 bpf_jit_compile(fp);
1321 /* JIT compiler couldn't process this filter, so do the
1322 * internal BPF translation for the optimized interpreter.
1325 fp = bpf_migrate_filter(fp);
1331 * bpf_prog_create - create an unattached filter
1332 * @pfp: the unattached filter that is created
1333 * @fprog: the filter program
1335 * Create a filter independent of any socket. We first run some
1336 * sanity checks on it to make sure it does not explode on us later.
1337 * If an error occurs or there is insufficient memory for the filter
1338 * a negative errno code is returned. On success the return is zero.
1340 int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog)
1342 unsigned int fsize = bpf_classic_proglen(fprog);
1343 struct bpf_prog *fp;
1345 /* Make sure new filter is there and in the right amounts. */
1346 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1349 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1353 memcpy(fp->insns, fprog->filter, fsize);
1355 fp->len = fprog->len;
1356 /* Since unattached filters are not copied back to user
1357 * space through sk_get_filter(), we do not need to hold
1358 * a copy here, and can spare us the work.
1360 fp->orig_prog = NULL;
1362 /* bpf_prepare_filter() already takes care of freeing
1363 * memory in case something goes wrong.
1365 fp = bpf_prepare_filter(fp, NULL);
1372 EXPORT_SYMBOL_GPL(bpf_prog_create);
1375 * bpf_prog_create_from_user - create an unattached filter from user buffer
1376 * @pfp: the unattached filter that is created
1377 * @fprog: the filter program
1378 * @trans: post-classic verifier transformation handler
1379 * @save_orig: save classic BPF program
1381 * This function effectively does the same as bpf_prog_create(), only
1382 * that it builds up its insns buffer from user space provided buffer.
1383 * It also allows for passing a bpf_aux_classic_check_t handler.
1385 int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog,
1386 bpf_aux_classic_check_t trans, bool save_orig)
1388 unsigned int fsize = bpf_classic_proglen(fprog);
1389 struct bpf_prog *fp;
1392 /* Make sure new filter is there and in the right amounts. */
1393 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1396 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1400 if (copy_from_user(fp->insns, fprog->filter, fsize)) {
1401 __bpf_prog_free(fp);
1405 fp->len = fprog->len;
1406 fp->orig_prog = NULL;
1409 err = bpf_prog_store_orig_filter(fp, fprog);
1411 __bpf_prog_free(fp);
1416 /* bpf_prepare_filter() already takes care of freeing
1417 * memory in case something goes wrong.
1419 fp = bpf_prepare_filter(fp, trans);
1426 EXPORT_SYMBOL_GPL(bpf_prog_create_from_user);
1428 void bpf_prog_destroy(struct bpf_prog *fp)
1430 __bpf_prog_release(fp);
1432 EXPORT_SYMBOL_GPL(bpf_prog_destroy);
1434 static int __sk_attach_prog(struct bpf_prog *prog, struct sock *sk)
1436 struct sk_filter *fp, *old_fp;
1438 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
1444 if (!__sk_filter_charge(sk, fp)) {
1448 refcount_set(&fp->refcnt, 1);
1450 old_fp = rcu_dereference_protected(sk->sk_filter,
1451 lockdep_sock_is_held(sk));
1452 rcu_assign_pointer(sk->sk_filter, fp);
1455 sk_filter_uncharge(sk, old_fp);
1461 struct bpf_prog *__get_filter(struct sock_fprog *fprog, struct sock *sk)
1463 unsigned int fsize = bpf_classic_proglen(fprog);
1464 struct bpf_prog *prog;
1467 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1468 return ERR_PTR(-EPERM);
1470 /* Make sure new filter is there and in the right amounts. */
1471 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1472 return ERR_PTR(-EINVAL);
1474 prog = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1476 return ERR_PTR(-ENOMEM);
1478 if (copy_from_user(prog->insns, fprog->filter, fsize)) {
1479 __bpf_prog_free(prog);
1480 return ERR_PTR(-EFAULT);
1483 prog->len = fprog->len;
1485 err = bpf_prog_store_orig_filter(prog, fprog);
1487 __bpf_prog_free(prog);
1488 return ERR_PTR(-ENOMEM);
1491 /* bpf_prepare_filter() already takes care of freeing
1492 * memory in case something goes wrong.
1494 return bpf_prepare_filter(prog, NULL);
1498 * sk_attach_filter - attach a socket filter
1499 * @fprog: the filter program
1500 * @sk: the socket to use
1502 * Attach the user's filter code. We first run some sanity checks on
1503 * it to make sure it does not explode on us later. If an error
1504 * occurs or there is insufficient memory for the filter a negative
1505 * errno code is returned. On success the return is zero.
1507 int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1509 struct bpf_prog *prog = __get_filter(fprog, sk);
1513 return PTR_ERR(prog);
1515 err = __sk_attach_prog(prog, sk);
1517 __bpf_prog_release(prog);
1523 EXPORT_SYMBOL_GPL(sk_attach_filter);
1525 int sk_reuseport_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1527 struct bpf_prog *prog = __get_filter(fprog, sk);
1531 return PTR_ERR(prog);
1533 if (bpf_prog_size(prog->len) > sysctl_optmem_max)
1536 err = reuseport_attach_prog(sk, prog);
1539 __bpf_prog_release(prog);
1544 static struct bpf_prog *__get_bpf(u32 ufd, struct sock *sk)
1546 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1547 return ERR_PTR(-EPERM);
1549 return bpf_prog_get_type(ufd, BPF_PROG_TYPE_SOCKET_FILTER);
1552 int sk_attach_bpf(u32 ufd, struct sock *sk)
1554 struct bpf_prog *prog = __get_bpf(ufd, sk);
1558 return PTR_ERR(prog);
1560 err = __sk_attach_prog(prog, sk);
1569 int sk_reuseport_attach_bpf(u32 ufd, struct sock *sk)
1571 struct bpf_prog *prog;
1574 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1577 prog = bpf_prog_get_type(ufd, BPF_PROG_TYPE_SOCKET_FILTER);
1578 if (IS_ERR(prog) && PTR_ERR(prog) == -EINVAL)
1579 prog = bpf_prog_get_type(ufd, BPF_PROG_TYPE_SK_REUSEPORT);
1581 return PTR_ERR(prog);
1583 if (prog->type == BPF_PROG_TYPE_SK_REUSEPORT) {
1584 /* Like other non BPF_PROG_TYPE_SOCKET_FILTER
1585 * bpf prog (e.g. sockmap). It depends on the
1586 * limitation imposed by bpf_prog_load().
1587 * Hence, sysctl_optmem_max is not checked.
1589 if ((sk->sk_type != SOCK_STREAM &&
1590 sk->sk_type != SOCK_DGRAM) ||
1591 (sk->sk_protocol != IPPROTO_UDP &&
1592 sk->sk_protocol != IPPROTO_TCP) ||
1593 (sk->sk_family != AF_INET &&
1594 sk->sk_family != AF_INET6)) {
1599 /* BPF_PROG_TYPE_SOCKET_FILTER */
1600 if (bpf_prog_size(prog->len) > sysctl_optmem_max) {
1606 err = reuseport_attach_prog(sk, prog);
1614 void sk_reuseport_prog_free(struct bpf_prog *prog)
1619 if (prog->type == BPF_PROG_TYPE_SK_REUSEPORT)
1622 bpf_prog_destroy(prog);
1625 struct bpf_scratchpad {
1627 __be32 diff[MAX_BPF_STACK / sizeof(__be32)];
1628 u8 buff[MAX_BPF_STACK];
1632 static DEFINE_PER_CPU(struct bpf_scratchpad, bpf_sp);
1634 static inline int __bpf_try_make_writable(struct sk_buff *skb,
1635 unsigned int write_len)
1637 return skb_ensure_writable(skb, write_len);
1640 static inline int bpf_try_make_writable(struct sk_buff *skb,
1641 unsigned int write_len)
1643 int err = __bpf_try_make_writable(skb, write_len);
1645 bpf_compute_data_pointers(skb);
1649 static int bpf_try_make_head_writable(struct sk_buff *skb)
1651 return bpf_try_make_writable(skb, skb_headlen(skb));
1654 static inline void bpf_push_mac_rcsum(struct sk_buff *skb)
1656 if (skb_at_tc_ingress(skb))
1657 skb_postpush_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1660 static inline void bpf_pull_mac_rcsum(struct sk_buff *skb)
1662 if (skb_at_tc_ingress(skb))
1663 skb_postpull_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1666 BPF_CALL_5(bpf_skb_store_bytes, struct sk_buff *, skb, u32, offset,
1667 const void *, from, u32, len, u64, flags)
1671 if (unlikely(flags & ~(BPF_F_RECOMPUTE_CSUM | BPF_F_INVALIDATE_HASH)))
1673 if (unlikely(offset > 0xffff))
1675 if (unlikely(bpf_try_make_writable(skb, offset + len)))
1678 ptr = skb->data + offset;
1679 if (flags & BPF_F_RECOMPUTE_CSUM)
1680 __skb_postpull_rcsum(skb, ptr, len, offset);
1682 memcpy(ptr, from, len);
1684 if (flags & BPF_F_RECOMPUTE_CSUM)
1685 __skb_postpush_rcsum(skb, ptr, len, offset);
1686 if (flags & BPF_F_INVALIDATE_HASH)
1687 skb_clear_hash(skb);
1692 static const struct bpf_func_proto bpf_skb_store_bytes_proto = {
1693 .func = bpf_skb_store_bytes,
1695 .ret_type = RET_INTEGER,
1696 .arg1_type = ARG_PTR_TO_CTX,
1697 .arg2_type = ARG_ANYTHING,
1698 .arg3_type = ARG_PTR_TO_MEM,
1699 .arg4_type = ARG_CONST_SIZE,
1700 .arg5_type = ARG_ANYTHING,
1703 BPF_CALL_4(bpf_skb_load_bytes, const struct sk_buff *, skb, u32, offset,
1704 void *, to, u32, len)
1708 if (unlikely(offset > 0xffff))
1711 ptr = skb_header_pointer(skb, offset, len, to);
1715 memcpy(to, ptr, len);
1723 static const struct bpf_func_proto bpf_skb_load_bytes_proto = {
1724 .func = bpf_skb_load_bytes,
1726 .ret_type = RET_INTEGER,
1727 .arg1_type = ARG_PTR_TO_CTX,
1728 .arg2_type = ARG_ANYTHING,
1729 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
1730 .arg4_type = ARG_CONST_SIZE,
1733 BPF_CALL_5(bpf_skb_load_bytes_relative, const struct sk_buff *, skb,
1734 u32, offset, void *, to, u32, len, u32, start_header)
1736 u8 *end = skb_tail_pointer(skb);
1737 u8 *net = skb_network_header(skb);
1738 u8 *mac = skb_mac_header(skb);
1741 if (unlikely(offset > 0xffff || len > (end - mac)))
1744 switch (start_header) {
1745 case BPF_HDR_START_MAC:
1748 case BPF_HDR_START_NET:
1755 if (likely(ptr >= mac && ptr + len <= end)) {
1756 memcpy(to, ptr, len);
1765 static const struct bpf_func_proto bpf_skb_load_bytes_relative_proto = {
1766 .func = bpf_skb_load_bytes_relative,
1768 .ret_type = RET_INTEGER,
1769 .arg1_type = ARG_PTR_TO_CTX,
1770 .arg2_type = ARG_ANYTHING,
1771 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
1772 .arg4_type = ARG_CONST_SIZE,
1773 .arg5_type = ARG_ANYTHING,
1776 BPF_CALL_2(bpf_skb_pull_data, struct sk_buff *, skb, u32, len)
1778 /* Idea is the following: should the needed direct read/write
1779 * test fail during runtime, we can pull in more data and redo
1780 * again, since implicitly, we invalidate previous checks here.
1782 * Or, since we know how much we need to make read/writeable,
1783 * this can be done once at the program beginning for direct
1784 * access case. By this we overcome limitations of only current
1785 * headroom being accessible.
1787 return bpf_try_make_writable(skb, len ? : skb_headlen(skb));
1790 static const struct bpf_func_proto bpf_skb_pull_data_proto = {
1791 .func = bpf_skb_pull_data,
1793 .ret_type = RET_INTEGER,
1794 .arg1_type = ARG_PTR_TO_CTX,
1795 .arg2_type = ARG_ANYTHING,
1798 BPF_CALL_1(bpf_sk_fullsock, struct sock *, sk)
1800 return sk_fullsock(sk) ? (unsigned long)sk : (unsigned long)NULL;
1803 static const struct bpf_func_proto bpf_sk_fullsock_proto = {
1804 .func = bpf_sk_fullsock,
1806 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
1807 .arg1_type = ARG_PTR_TO_SOCK_COMMON,
1810 static inline int sk_skb_try_make_writable(struct sk_buff *skb,
1811 unsigned int write_len)
1813 int err = __bpf_try_make_writable(skb, write_len);
1815 bpf_compute_data_end_sk_skb(skb);
1819 BPF_CALL_2(sk_skb_pull_data, struct sk_buff *, skb, u32, len)
1821 /* Idea is the following: should the needed direct read/write
1822 * test fail during runtime, we can pull in more data and redo
1823 * again, since implicitly, we invalidate previous checks here.
1825 * Or, since we know how much we need to make read/writeable,
1826 * this can be done once at the program beginning for direct
1827 * access case. By this we overcome limitations of only current
1828 * headroom being accessible.
1830 return sk_skb_try_make_writable(skb, len ? : skb_headlen(skb));
1833 static const struct bpf_func_proto sk_skb_pull_data_proto = {
1834 .func = sk_skb_pull_data,
1836 .ret_type = RET_INTEGER,
1837 .arg1_type = ARG_PTR_TO_CTX,
1838 .arg2_type = ARG_ANYTHING,
1841 BPF_CALL_5(bpf_l3_csum_replace, struct sk_buff *, skb, u32, offset,
1842 u64, from, u64, to, u64, flags)
1846 if (unlikely(flags & ~(BPF_F_HDR_FIELD_MASK)))
1848 if (unlikely(offset > 0xffff || offset & 1))
1850 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
1853 ptr = (__sum16 *)(skb->data + offset);
1854 switch (flags & BPF_F_HDR_FIELD_MASK) {
1856 if (unlikely(from != 0))
1859 csum_replace_by_diff(ptr, to);
1862 csum_replace2(ptr, from, to);
1865 csum_replace4(ptr, from, to);
1874 static const struct bpf_func_proto bpf_l3_csum_replace_proto = {
1875 .func = bpf_l3_csum_replace,
1877 .ret_type = RET_INTEGER,
1878 .arg1_type = ARG_PTR_TO_CTX,
1879 .arg2_type = ARG_ANYTHING,
1880 .arg3_type = ARG_ANYTHING,
1881 .arg4_type = ARG_ANYTHING,
1882 .arg5_type = ARG_ANYTHING,
1885 BPF_CALL_5(bpf_l4_csum_replace, struct sk_buff *, skb, u32, offset,
1886 u64, from, u64, to, u64, flags)
1888 bool is_pseudo = flags & BPF_F_PSEUDO_HDR;
1889 bool is_mmzero = flags & BPF_F_MARK_MANGLED_0;
1890 bool do_mforce = flags & BPF_F_MARK_ENFORCE;
1893 if (unlikely(flags & ~(BPF_F_MARK_MANGLED_0 | BPF_F_MARK_ENFORCE |
1894 BPF_F_PSEUDO_HDR | BPF_F_HDR_FIELD_MASK)))
1896 if (unlikely(offset > 0xffff || offset & 1))
1898 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
1901 ptr = (__sum16 *)(skb->data + offset);
1902 if (is_mmzero && !do_mforce && !*ptr)
1905 switch (flags & BPF_F_HDR_FIELD_MASK) {
1907 if (unlikely(from != 0))
1910 inet_proto_csum_replace_by_diff(ptr, skb, to, is_pseudo);
1913 inet_proto_csum_replace2(ptr, skb, from, to, is_pseudo);
1916 inet_proto_csum_replace4(ptr, skb, from, to, is_pseudo);
1922 if (is_mmzero && !*ptr)
1923 *ptr = CSUM_MANGLED_0;
1927 static const struct bpf_func_proto bpf_l4_csum_replace_proto = {
1928 .func = bpf_l4_csum_replace,
1930 .ret_type = RET_INTEGER,
1931 .arg1_type = ARG_PTR_TO_CTX,
1932 .arg2_type = ARG_ANYTHING,
1933 .arg3_type = ARG_ANYTHING,
1934 .arg4_type = ARG_ANYTHING,
1935 .arg5_type = ARG_ANYTHING,
1938 BPF_CALL_5(bpf_csum_diff, __be32 *, from, u32, from_size,
1939 __be32 *, to, u32, to_size, __wsum, seed)
1941 struct bpf_scratchpad *sp = this_cpu_ptr(&bpf_sp);
1942 u32 diff_size = from_size + to_size;
1945 /* This is quite flexible, some examples:
1947 * from_size == 0, to_size > 0, seed := csum --> pushing data
1948 * from_size > 0, to_size == 0, seed := csum --> pulling data
1949 * from_size > 0, to_size > 0, seed := 0 --> diffing data
1951 * Even for diffing, from_size and to_size don't need to be equal.
1953 if (unlikely(((from_size | to_size) & (sizeof(__be32) - 1)) ||
1954 diff_size > sizeof(sp->diff)))
1957 for (i = 0; i < from_size / sizeof(__be32); i++, j++)
1958 sp->diff[j] = ~from[i];
1959 for (i = 0; i < to_size / sizeof(__be32); i++, j++)
1960 sp->diff[j] = to[i];
1962 return csum_partial(sp->diff, diff_size, seed);
1965 static const struct bpf_func_proto bpf_csum_diff_proto = {
1966 .func = bpf_csum_diff,
1969 .ret_type = RET_INTEGER,
1970 .arg1_type = ARG_PTR_TO_MEM_OR_NULL,
1971 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
1972 .arg3_type = ARG_PTR_TO_MEM_OR_NULL,
1973 .arg4_type = ARG_CONST_SIZE_OR_ZERO,
1974 .arg5_type = ARG_ANYTHING,
1977 BPF_CALL_2(bpf_csum_update, struct sk_buff *, skb, __wsum, csum)
1979 /* The interface is to be used in combination with bpf_csum_diff()
1980 * for direct packet writes. csum rotation for alignment as well
1981 * as emulating csum_sub() can be done from the eBPF program.
1983 if (skb->ip_summed == CHECKSUM_COMPLETE)
1984 return (skb->csum = csum_add(skb->csum, csum));
1989 static const struct bpf_func_proto bpf_csum_update_proto = {
1990 .func = bpf_csum_update,
1992 .ret_type = RET_INTEGER,
1993 .arg1_type = ARG_PTR_TO_CTX,
1994 .arg2_type = ARG_ANYTHING,
1997 static inline int __bpf_rx_skb(struct net_device *dev, struct sk_buff *skb)
1999 return dev_forward_skb(dev, skb);
2002 static inline int __bpf_rx_skb_no_mac(struct net_device *dev,
2003 struct sk_buff *skb)
2005 int ret = ____dev_forward_skb(dev, skb);
2009 ret = netif_rx(skb);
2015 static inline int __bpf_tx_skb(struct net_device *dev, struct sk_buff *skb)
2019 if (dev_xmit_recursion()) {
2020 net_crit_ratelimited("bpf: recursion limit reached on datapath, buggy bpf program?\n");
2027 dev_xmit_recursion_inc();
2028 ret = dev_queue_xmit(skb);
2029 dev_xmit_recursion_dec();
2034 static int __bpf_redirect_no_mac(struct sk_buff *skb, struct net_device *dev,
2037 unsigned int mlen = skb_network_offset(skb);
2040 __skb_pull(skb, mlen);
2042 /* At ingress, the mac header has already been pulled once.
2043 * At egress, skb_pospull_rcsum has to be done in case that
2044 * the skb is originated from ingress (i.e. a forwarded skb)
2045 * to ensure that rcsum starts at net header.
2047 if (!skb_at_tc_ingress(skb))
2048 skb_postpull_rcsum(skb, skb_mac_header(skb), mlen);
2050 skb_pop_mac_header(skb);
2051 skb_reset_mac_len(skb);
2052 return flags & BPF_F_INGRESS ?
2053 __bpf_rx_skb_no_mac(dev, skb) : __bpf_tx_skb(dev, skb);
2056 static int __bpf_redirect_common(struct sk_buff *skb, struct net_device *dev,
2059 /* Verify that a link layer header is carried */
2060 if (unlikely(skb->mac_header >= skb->network_header)) {
2065 bpf_push_mac_rcsum(skb);
2066 return flags & BPF_F_INGRESS ?
2067 __bpf_rx_skb(dev, skb) : __bpf_tx_skb(dev, skb);
2070 static int __bpf_redirect(struct sk_buff *skb, struct net_device *dev,
2073 if (dev_is_mac_header_xmit(dev))
2074 return __bpf_redirect_common(skb, dev, flags);
2076 return __bpf_redirect_no_mac(skb, dev, flags);
2079 BPF_CALL_3(bpf_clone_redirect, struct sk_buff *, skb, u32, ifindex, u64, flags)
2081 struct net_device *dev;
2082 struct sk_buff *clone;
2085 if (unlikely(flags & ~(BPF_F_INGRESS)))
2088 dev = dev_get_by_index_rcu(dev_net(skb->dev), ifindex);
2092 clone = skb_clone(skb, GFP_ATOMIC);
2093 if (unlikely(!clone))
2096 /* For direct write, we need to keep the invariant that the skbs
2097 * we're dealing with need to be uncloned. Should uncloning fail
2098 * here, we need to free the just generated clone to unclone once
2101 ret = bpf_try_make_head_writable(skb);
2102 if (unlikely(ret)) {
2107 return __bpf_redirect(clone, dev, flags);
2110 static const struct bpf_func_proto bpf_clone_redirect_proto = {
2111 .func = bpf_clone_redirect,
2113 .ret_type = RET_INTEGER,
2114 .arg1_type = ARG_PTR_TO_CTX,
2115 .arg2_type = ARG_ANYTHING,
2116 .arg3_type = ARG_ANYTHING,
2119 DEFINE_PER_CPU(struct bpf_redirect_info, bpf_redirect_info);
2120 EXPORT_PER_CPU_SYMBOL_GPL(bpf_redirect_info);
2122 BPF_CALL_2(bpf_redirect, u32, ifindex, u64, flags)
2124 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
2126 if (unlikely(flags & ~(BPF_F_INGRESS)))
2129 ri->ifindex = ifindex;
2132 return TC_ACT_REDIRECT;
2135 int skb_do_redirect(struct sk_buff *skb)
2137 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
2138 struct net_device *dev;
2140 dev = dev_get_by_index_rcu(dev_net(skb->dev), ri->ifindex);
2142 if (unlikely(!dev)) {
2147 return __bpf_redirect(skb, dev, ri->flags);
2150 static const struct bpf_func_proto bpf_redirect_proto = {
2151 .func = bpf_redirect,
2153 .ret_type = RET_INTEGER,
2154 .arg1_type = ARG_ANYTHING,
2155 .arg2_type = ARG_ANYTHING,
2158 BPF_CALL_2(bpf_msg_apply_bytes, struct sk_msg *, msg, u32, bytes)
2160 msg->apply_bytes = bytes;
2164 static const struct bpf_func_proto bpf_msg_apply_bytes_proto = {
2165 .func = bpf_msg_apply_bytes,
2167 .ret_type = RET_INTEGER,
2168 .arg1_type = ARG_PTR_TO_CTX,
2169 .arg2_type = ARG_ANYTHING,
2172 BPF_CALL_2(bpf_msg_cork_bytes, struct sk_msg *, msg, u32, bytes)
2174 msg->cork_bytes = bytes;
2178 static const struct bpf_func_proto bpf_msg_cork_bytes_proto = {
2179 .func = bpf_msg_cork_bytes,
2181 .ret_type = RET_INTEGER,
2182 .arg1_type = ARG_PTR_TO_CTX,
2183 .arg2_type = ARG_ANYTHING,
2186 BPF_CALL_4(bpf_msg_pull_data, struct sk_msg *, msg, u32, start,
2187 u32, end, u64, flags)
2189 u32 len = 0, offset = 0, copy = 0, poffset = 0, bytes = end - start;
2190 u32 first_sge, last_sge, i, shift, bytes_sg_total;
2191 struct scatterlist *sge;
2192 u8 *raw, *to, *from;
2195 if (unlikely(flags || end <= start))
2198 /* First find the starting scatterlist element */
2201 len = sk_msg_elem(msg, i)->length;
2202 if (start < offset + len)
2205 sk_msg_iter_var_next(i);
2206 } while (i != msg->sg.end);
2208 if (unlikely(start >= offset + len))
2212 /* The start may point into the sg element so we need to also
2213 * account for the headroom.
2215 bytes_sg_total = start - offset + bytes;
2216 if (!msg->sg.copy[i] && bytes_sg_total <= len)
2219 /* At this point we need to linearize multiple scatterlist
2220 * elements or a single shared page. Either way we need to
2221 * copy into a linear buffer exclusively owned by BPF. Then
2222 * place the buffer in the scatterlist and fixup the original
2223 * entries by removing the entries now in the linear buffer
2224 * and shifting the remaining entries. For now we do not try
2225 * to copy partial entries to avoid complexity of running out
2226 * of sg_entry slots. The downside is reading a single byte
2227 * will copy the entire sg entry.
2230 copy += sk_msg_elem(msg, i)->length;
2231 sk_msg_iter_var_next(i);
2232 if (bytes_sg_total <= copy)
2234 } while (i != msg->sg.end);
2237 if (unlikely(bytes_sg_total > copy))
2240 page = alloc_pages(__GFP_NOWARN | GFP_ATOMIC | __GFP_COMP,
2242 if (unlikely(!page))
2245 raw = page_address(page);
2248 sge = sk_msg_elem(msg, i);
2249 from = sg_virt(sge);
2253 memcpy(to, from, len);
2256 put_page(sg_page(sge));
2258 sk_msg_iter_var_next(i);
2259 } while (i != last_sge);
2261 sg_set_page(&msg->sg.data[first_sge], page, copy, 0);
2263 /* To repair sg ring we need to shift entries. If we only
2264 * had a single entry though we can just replace it and
2265 * be done. Otherwise walk the ring and shift the entries.
2267 WARN_ON_ONCE(last_sge == first_sge);
2268 shift = last_sge > first_sge ?
2269 last_sge - first_sge - 1 :
2270 MAX_SKB_FRAGS - first_sge + last_sge - 1;
2275 sk_msg_iter_var_next(i);
2279 if (i + shift >= MAX_MSG_FRAGS)
2280 move_from = i + shift - MAX_MSG_FRAGS;
2282 move_from = i + shift;
2283 if (move_from == msg->sg.end)
2286 msg->sg.data[i] = msg->sg.data[move_from];
2287 msg->sg.data[move_from].length = 0;
2288 msg->sg.data[move_from].page_link = 0;
2289 msg->sg.data[move_from].offset = 0;
2290 sk_msg_iter_var_next(i);
2293 msg->sg.end = msg->sg.end - shift > msg->sg.end ?
2294 msg->sg.end - shift + MAX_MSG_FRAGS :
2295 msg->sg.end - shift;
2297 msg->data = sg_virt(&msg->sg.data[first_sge]) + start - offset;
2298 msg->data_end = msg->data + bytes;
2302 static const struct bpf_func_proto bpf_msg_pull_data_proto = {
2303 .func = bpf_msg_pull_data,
2305 .ret_type = RET_INTEGER,
2306 .arg1_type = ARG_PTR_TO_CTX,
2307 .arg2_type = ARG_ANYTHING,
2308 .arg3_type = ARG_ANYTHING,
2309 .arg4_type = ARG_ANYTHING,
2312 BPF_CALL_4(bpf_msg_push_data, struct sk_msg *, msg, u32, start,
2313 u32, len, u64, flags)
2315 struct scatterlist sge, nsge, nnsge, rsge = {0}, *psge;
2316 u32 new, i = 0, l, space, copy = 0, offset = 0;
2317 u8 *raw, *to, *from;
2320 if (unlikely(flags))
2323 /* First find the starting scatterlist element */
2326 l = sk_msg_elem(msg, i)->length;
2328 if (start < offset + l)
2331 sk_msg_iter_var_next(i);
2332 } while (i != msg->sg.end);
2334 if (start >= offset + l)
2337 space = MAX_MSG_FRAGS - sk_msg_elem_used(msg);
2339 /* If no space available will fallback to copy, we need at
2340 * least one scatterlist elem available to push data into
2341 * when start aligns to the beginning of an element or two
2342 * when it falls inside an element. We handle the start equals
2343 * offset case because its the common case for inserting a
2346 if (!space || (space == 1 && start != offset))
2347 copy = msg->sg.data[i].length;
2349 page = alloc_pages(__GFP_NOWARN | GFP_ATOMIC | __GFP_COMP,
2350 get_order(copy + len));
2351 if (unlikely(!page))
2357 raw = page_address(page);
2359 psge = sk_msg_elem(msg, i);
2360 front = start - offset;
2361 back = psge->length - front;
2362 from = sg_virt(psge);
2365 memcpy(raw, from, front);
2369 to = raw + front + len;
2371 memcpy(to, from, back);
2374 put_page(sg_page(psge));
2375 } else if (start - offset) {
2376 psge = sk_msg_elem(msg, i);
2377 rsge = sk_msg_elem_cpy(msg, i);
2379 psge->length = start - offset;
2380 rsge.length -= psge->length;
2381 rsge.offset += start;
2383 sk_msg_iter_var_next(i);
2384 sg_unmark_end(psge);
2385 sk_msg_iter_next(msg, end);
2388 /* Slot(s) to place newly allocated data */
2391 /* Shift one or two slots as needed */
2393 sge = sk_msg_elem_cpy(msg, i);
2395 sk_msg_iter_var_next(i);
2396 sg_unmark_end(&sge);
2397 sk_msg_iter_next(msg, end);
2399 nsge = sk_msg_elem_cpy(msg, i);
2401 sk_msg_iter_var_next(i);
2402 nnsge = sk_msg_elem_cpy(msg, i);
2405 while (i != msg->sg.end) {
2406 msg->sg.data[i] = sge;
2408 sk_msg_iter_var_next(i);
2411 nnsge = sk_msg_elem_cpy(msg, i);
2413 nsge = sk_msg_elem_cpy(msg, i);
2418 /* Place newly allocated data buffer */
2419 sk_mem_charge(msg->sk, len);
2420 msg->sg.size += len;
2421 msg->sg.copy[new] = false;
2422 sg_set_page(&msg->sg.data[new], page, len + copy, 0);
2424 get_page(sg_page(&rsge));
2425 sk_msg_iter_var_next(new);
2426 msg->sg.data[new] = rsge;
2429 sk_msg_compute_data_pointers(msg);
2433 static const struct bpf_func_proto bpf_msg_push_data_proto = {
2434 .func = bpf_msg_push_data,
2436 .ret_type = RET_INTEGER,
2437 .arg1_type = ARG_PTR_TO_CTX,
2438 .arg2_type = ARG_ANYTHING,
2439 .arg3_type = ARG_ANYTHING,
2440 .arg4_type = ARG_ANYTHING,
2443 static void sk_msg_shift_left(struct sk_msg *msg, int i)
2449 sk_msg_iter_var_next(i);
2450 msg->sg.data[prev] = msg->sg.data[i];
2451 } while (i != msg->sg.end);
2453 sk_msg_iter_prev(msg, end);
2456 static void sk_msg_shift_right(struct sk_msg *msg, int i)
2458 struct scatterlist tmp, sge;
2460 sk_msg_iter_next(msg, end);
2461 sge = sk_msg_elem_cpy(msg, i);
2462 sk_msg_iter_var_next(i);
2463 tmp = sk_msg_elem_cpy(msg, i);
2465 while (i != msg->sg.end) {
2466 msg->sg.data[i] = sge;
2467 sk_msg_iter_var_next(i);
2469 tmp = sk_msg_elem_cpy(msg, i);
2473 BPF_CALL_4(bpf_msg_pop_data, struct sk_msg *, msg, u32, start,
2474 u32, len, u64, flags)
2476 u32 i = 0, l, space, offset = 0;
2477 u64 last = start + len;
2480 if (unlikely(flags))
2483 /* First find the starting scatterlist element */
2486 l = sk_msg_elem(msg, i)->length;
2488 if (start < offset + l)
2491 sk_msg_iter_var_next(i);
2492 } while (i != msg->sg.end);
2494 /* Bounds checks: start and pop must be inside message */
2495 if (start >= offset + l || last >= msg->sg.size)
2498 space = MAX_MSG_FRAGS - sk_msg_elem_used(msg);
2501 /* --------------| offset
2502 * -| start |-------- len -------|
2504 * |----- a ----|-------- pop -------|----- b ----|
2505 * |______________________________________________| length
2508 * a: region at front of scatter element to save
2509 * b: region at back of scatter element to save when length > A + pop
2510 * pop: region to pop from element, same as input 'pop' here will be
2511 * decremented below per iteration.
2513 * Two top-level cases to handle when start != offset, first B is non
2514 * zero and second B is zero corresponding to when a pop includes more
2517 * Then if B is non-zero AND there is no space allocate space and
2518 * compact A, B regions into page. If there is space shift ring to
2519 * the rigth free'ing the next element in ring to place B, leaving
2520 * A untouched except to reduce length.
2522 if (start != offset) {
2523 struct scatterlist *nsge, *sge = sk_msg_elem(msg, i);
2525 int b = sge->length - pop - a;
2527 sk_msg_iter_var_next(i);
2529 if (pop < sge->length - a) {
2532 sk_msg_shift_right(msg, i);
2533 nsge = sk_msg_elem(msg, i);
2534 get_page(sg_page(sge));
2537 b, sge->offset + pop + a);
2539 struct page *page, *orig;
2542 page = alloc_pages(__GFP_NOWARN |
2543 __GFP_COMP | GFP_ATOMIC,
2545 if (unlikely(!page))
2549 orig = sg_page(sge);
2550 from = sg_virt(sge);
2551 to = page_address(page);
2552 memcpy(to, from, a);
2553 memcpy(to + a, from + a + pop, b);
2554 sg_set_page(sge, page, a + b, 0);
2558 } else if (pop >= sge->length - a) {
2560 pop -= (sge->length - a);
2564 /* From above the current layout _must_ be as follows,
2569 * |---- pop ---|---------------- b ------------|
2570 * |____________________________________________| length
2572 * Offset and start of the current msg elem are equal because in the
2573 * previous case we handled offset != start and either consumed the
2574 * entire element and advanced to the next element OR pop == 0.
2576 * Two cases to handle here are first pop is less than the length
2577 * leaving some remainder b above. Simply adjust the element's layout
2578 * in this case. Or pop >= length of the element so that b = 0. In this
2579 * case advance to next element decrementing pop.
2582 struct scatterlist *sge = sk_msg_elem(msg, i);
2584 if (pop < sge->length) {
2590 sk_msg_shift_left(msg, i);
2592 sk_msg_iter_var_next(i);
2595 sk_mem_uncharge(msg->sk, len - pop);
2596 msg->sg.size -= (len - pop);
2597 sk_msg_compute_data_pointers(msg);
2601 static const struct bpf_func_proto bpf_msg_pop_data_proto = {
2602 .func = bpf_msg_pop_data,
2604 .ret_type = RET_INTEGER,
2605 .arg1_type = ARG_PTR_TO_CTX,
2606 .arg2_type = ARG_ANYTHING,
2607 .arg3_type = ARG_ANYTHING,
2608 .arg4_type = ARG_ANYTHING,
2611 BPF_CALL_1(bpf_get_cgroup_classid, const struct sk_buff *, skb)
2613 return task_get_classid(skb);
2616 static const struct bpf_func_proto bpf_get_cgroup_classid_proto = {
2617 .func = bpf_get_cgroup_classid,
2619 .ret_type = RET_INTEGER,
2620 .arg1_type = ARG_PTR_TO_CTX,
2623 BPF_CALL_1(bpf_get_route_realm, const struct sk_buff *, skb)
2625 return dst_tclassid(skb);
2628 static const struct bpf_func_proto bpf_get_route_realm_proto = {
2629 .func = bpf_get_route_realm,
2631 .ret_type = RET_INTEGER,
2632 .arg1_type = ARG_PTR_TO_CTX,
2635 BPF_CALL_1(bpf_get_hash_recalc, struct sk_buff *, skb)
2637 /* If skb_clear_hash() was called due to mangling, we can
2638 * trigger SW recalculation here. Later access to hash
2639 * can then use the inline skb->hash via context directly
2640 * instead of calling this helper again.
2642 return skb_get_hash(skb);
2645 static const struct bpf_func_proto bpf_get_hash_recalc_proto = {
2646 .func = bpf_get_hash_recalc,
2648 .ret_type = RET_INTEGER,
2649 .arg1_type = ARG_PTR_TO_CTX,
2652 BPF_CALL_1(bpf_set_hash_invalid, struct sk_buff *, skb)
2654 /* After all direct packet write, this can be used once for
2655 * triggering a lazy recalc on next skb_get_hash() invocation.
2657 skb_clear_hash(skb);
2661 static const struct bpf_func_proto bpf_set_hash_invalid_proto = {
2662 .func = bpf_set_hash_invalid,
2664 .ret_type = RET_INTEGER,
2665 .arg1_type = ARG_PTR_TO_CTX,
2668 BPF_CALL_2(bpf_set_hash, struct sk_buff *, skb, u32, hash)
2670 /* Set user specified hash as L4(+), so that it gets returned
2671 * on skb_get_hash() call unless BPF prog later on triggers a
2674 __skb_set_sw_hash(skb, hash, true);
2678 static const struct bpf_func_proto bpf_set_hash_proto = {
2679 .func = bpf_set_hash,
2681 .ret_type = RET_INTEGER,
2682 .arg1_type = ARG_PTR_TO_CTX,
2683 .arg2_type = ARG_ANYTHING,
2686 BPF_CALL_3(bpf_skb_vlan_push, struct sk_buff *, skb, __be16, vlan_proto,
2691 if (unlikely(vlan_proto != htons(ETH_P_8021Q) &&
2692 vlan_proto != htons(ETH_P_8021AD)))
2693 vlan_proto = htons(ETH_P_8021Q);
2695 bpf_push_mac_rcsum(skb);
2696 ret = skb_vlan_push(skb, vlan_proto, vlan_tci);
2697 bpf_pull_mac_rcsum(skb);
2699 bpf_compute_data_pointers(skb);
2703 static const struct bpf_func_proto bpf_skb_vlan_push_proto = {
2704 .func = bpf_skb_vlan_push,
2706 .ret_type = RET_INTEGER,
2707 .arg1_type = ARG_PTR_TO_CTX,
2708 .arg2_type = ARG_ANYTHING,
2709 .arg3_type = ARG_ANYTHING,
2712 BPF_CALL_1(bpf_skb_vlan_pop, struct sk_buff *, skb)
2716 bpf_push_mac_rcsum(skb);
2717 ret = skb_vlan_pop(skb);
2718 bpf_pull_mac_rcsum(skb);
2720 bpf_compute_data_pointers(skb);
2724 static const struct bpf_func_proto bpf_skb_vlan_pop_proto = {
2725 .func = bpf_skb_vlan_pop,
2727 .ret_type = RET_INTEGER,
2728 .arg1_type = ARG_PTR_TO_CTX,
2731 static int bpf_skb_generic_push(struct sk_buff *skb, u32 off, u32 len)
2733 /* Caller already did skb_cow() with len as headroom,
2734 * so no need to do it here.
2737 memmove(skb->data, skb->data + len, off);
2738 memset(skb->data + off, 0, len);
2740 /* No skb_postpush_rcsum(skb, skb->data + off, len)
2741 * needed here as it does not change the skb->csum
2742 * result for checksum complete when summing over
2748 static int bpf_skb_generic_pop(struct sk_buff *skb, u32 off, u32 len)
2750 /* skb_ensure_writable() is not needed here, as we're
2751 * already working on an uncloned skb.
2753 if (unlikely(!pskb_may_pull(skb, off + len)))
2756 skb_postpull_rcsum(skb, skb->data + off, len);
2757 memmove(skb->data + len, skb->data, off);
2758 __skb_pull(skb, len);
2763 static int bpf_skb_net_hdr_push(struct sk_buff *skb, u32 off, u32 len)
2765 bool trans_same = skb->transport_header == skb->network_header;
2768 /* There's no need for __skb_push()/__skb_pull() pair to
2769 * get to the start of the mac header as we're guaranteed
2770 * to always start from here under eBPF.
2772 ret = bpf_skb_generic_push(skb, off, len);
2774 skb->mac_header -= len;
2775 skb->network_header -= len;
2777 skb->transport_header = skb->network_header;
2783 static int bpf_skb_net_hdr_pop(struct sk_buff *skb, u32 off, u32 len)
2785 bool trans_same = skb->transport_header == skb->network_header;
2788 /* Same here, __skb_push()/__skb_pull() pair not needed. */
2789 ret = bpf_skb_generic_pop(skb, off, len);
2791 skb->mac_header += len;
2792 skb->network_header += len;
2794 skb->transport_header = skb->network_header;
2800 static int bpf_skb_proto_4_to_6(struct sk_buff *skb)
2802 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
2803 u32 off = skb_mac_header_len(skb);
2806 if (skb_is_gso(skb) && !skb_is_gso_tcp(skb))
2809 ret = skb_cow(skb, len_diff);
2810 if (unlikely(ret < 0))
2813 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
2814 if (unlikely(ret < 0))
2817 if (skb_is_gso(skb)) {
2818 struct skb_shared_info *shinfo = skb_shinfo(skb);
2820 /* SKB_GSO_TCPV4 needs to be changed into
2823 if (shinfo->gso_type & SKB_GSO_TCPV4) {
2824 shinfo->gso_type &= ~SKB_GSO_TCPV4;
2825 shinfo->gso_type |= SKB_GSO_TCPV6;
2828 /* Due to IPv6 header, MSS needs to be downgraded. */
2829 skb_decrease_gso_size(shinfo, len_diff);
2830 /* Header must be checked, and gso_segs recomputed. */
2831 shinfo->gso_type |= SKB_GSO_DODGY;
2832 shinfo->gso_segs = 0;
2835 skb->protocol = htons(ETH_P_IPV6);
2836 skb_clear_hash(skb);
2841 static int bpf_skb_proto_6_to_4(struct sk_buff *skb)
2843 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
2844 u32 off = skb_mac_header_len(skb);
2847 if (skb_is_gso(skb) && !skb_is_gso_tcp(skb))
2850 ret = skb_unclone(skb, GFP_ATOMIC);
2851 if (unlikely(ret < 0))
2854 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
2855 if (unlikely(ret < 0))
2858 if (skb_is_gso(skb)) {
2859 struct skb_shared_info *shinfo = skb_shinfo(skb);
2861 /* SKB_GSO_TCPV6 needs to be changed into
2864 if (shinfo->gso_type & SKB_GSO_TCPV6) {
2865 shinfo->gso_type &= ~SKB_GSO_TCPV6;
2866 shinfo->gso_type |= SKB_GSO_TCPV4;
2869 /* Due to IPv4 header, MSS can be upgraded. */
2870 skb_increase_gso_size(shinfo, len_diff);
2871 /* Header must be checked, and gso_segs recomputed. */
2872 shinfo->gso_type |= SKB_GSO_DODGY;
2873 shinfo->gso_segs = 0;
2876 skb->protocol = htons(ETH_P_IP);
2877 skb_clear_hash(skb);
2882 static int bpf_skb_proto_xlat(struct sk_buff *skb, __be16 to_proto)
2884 __be16 from_proto = skb->protocol;
2886 if (from_proto == htons(ETH_P_IP) &&
2887 to_proto == htons(ETH_P_IPV6))
2888 return bpf_skb_proto_4_to_6(skb);
2890 if (from_proto == htons(ETH_P_IPV6) &&
2891 to_proto == htons(ETH_P_IP))
2892 return bpf_skb_proto_6_to_4(skb);
2897 BPF_CALL_3(bpf_skb_change_proto, struct sk_buff *, skb, __be16, proto,
2902 if (unlikely(flags))
2905 /* General idea is that this helper does the basic groundwork
2906 * needed for changing the protocol, and eBPF program fills the
2907 * rest through bpf_skb_store_bytes(), bpf_lX_csum_replace()
2908 * and other helpers, rather than passing a raw buffer here.
2910 * The rationale is to keep this minimal and without a need to
2911 * deal with raw packet data. F.e. even if we would pass buffers
2912 * here, the program still needs to call the bpf_lX_csum_replace()
2913 * helpers anyway. Plus, this way we keep also separation of
2914 * concerns, since f.e. bpf_skb_store_bytes() should only take
2917 * Currently, additional options and extension header space are
2918 * not supported, but flags register is reserved so we can adapt
2919 * that. For offloads, we mark packet as dodgy, so that headers
2920 * need to be verified first.
2922 ret = bpf_skb_proto_xlat(skb, proto);
2923 bpf_compute_data_pointers(skb);
2927 static const struct bpf_func_proto bpf_skb_change_proto_proto = {
2928 .func = bpf_skb_change_proto,
2930 .ret_type = RET_INTEGER,
2931 .arg1_type = ARG_PTR_TO_CTX,
2932 .arg2_type = ARG_ANYTHING,
2933 .arg3_type = ARG_ANYTHING,
2936 BPF_CALL_2(bpf_skb_change_type, struct sk_buff *, skb, u32, pkt_type)
2938 /* We only allow a restricted subset to be changed for now. */
2939 if (unlikely(!skb_pkt_type_ok(skb->pkt_type) ||
2940 !skb_pkt_type_ok(pkt_type)))
2943 skb->pkt_type = pkt_type;
2947 static const struct bpf_func_proto bpf_skb_change_type_proto = {
2948 .func = bpf_skb_change_type,
2950 .ret_type = RET_INTEGER,
2951 .arg1_type = ARG_PTR_TO_CTX,
2952 .arg2_type = ARG_ANYTHING,
2955 static u32 bpf_skb_net_base_len(const struct sk_buff *skb)
2957 switch (skb->protocol) {
2958 case htons(ETH_P_IP):
2959 return sizeof(struct iphdr);
2960 case htons(ETH_P_IPV6):
2961 return sizeof(struct ipv6hdr);
2967 #define BPF_F_ADJ_ROOM_ENCAP_L3_MASK (BPF_F_ADJ_ROOM_ENCAP_L3_IPV4 | \
2968 BPF_F_ADJ_ROOM_ENCAP_L3_IPV6)
2970 #define BPF_F_ADJ_ROOM_MASK (BPF_F_ADJ_ROOM_FIXED_GSO | \
2971 BPF_F_ADJ_ROOM_ENCAP_L3_MASK | \
2972 BPF_F_ADJ_ROOM_ENCAP_L4_GRE | \
2973 BPF_F_ADJ_ROOM_ENCAP_L4_UDP | \
2974 BPF_F_ADJ_ROOM_ENCAP_L2( \
2975 BPF_ADJ_ROOM_ENCAP_L2_MASK))
2977 static int bpf_skb_net_grow(struct sk_buff *skb, u32 off, u32 len_diff,
2980 u8 inner_mac_len = flags >> BPF_ADJ_ROOM_ENCAP_L2_SHIFT;
2981 bool encap = flags & BPF_F_ADJ_ROOM_ENCAP_L3_MASK;
2982 u16 mac_len = 0, inner_net = 0, inner_trans = 0;
2983 unsigned int gso_type = SKB_GSO_DODGY;
2986 if (skb_is_gso(skb) && !skb_is_gso_tcp(skb)) {
2987 /* udp gso_size delineates datagrams, only allow if fixed */
2988 if (!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) ||
2989 !(flags & BPF_F_ADJ_ROOM_FIXED_GSO))
2993 ret = skb_cow_head(skb, len_diff);
2994 if (unlikely(ret < 0))
2998 if (skb->protocol != htons(ETH_P_IP) &&
2999 skb->protocol != htons(ETH_P_IPV6))
3002 if (flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV4 &&
3003 flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV6)
3006 if (flags & BPF_F_ADJ_ROOM_ENCAP_L4_GRE &&
3007 flags & BPF_F_ADJ_ROOM_ENCAP_L4_UDP)
3010 if (skb->encapsulation)
3013 mac_len = skb->network_header - skb->mac_header;
3014 inner_net = skb->network_header;
3015 if (inner_mac_len > len_diff)
3017 inner_trans = skb->transport_header;
3020 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
3021 if (unlikely(ret < 0))
3025 skb->inner_mac_header = inner_net - inner_mac_len;
3026 skb->inner_network_header = inner_net;
3027 skb->inner_transport_header = inner_trans;
3028 skb_set_inner_protocol(skb, skb->protocol);
3030 skb->encapsulation = 1;
3031 skb_set_network_header(skb, mac_len);
3033 if (flags & BPF_F_ADJ_ROOM_ENCAP_L4_UDP)
3034 gso_type |= SKB_GSO_UDP_TUNNEL;
3035 else if (flags & BPF_F_ADJ_ROOM_ENCAP_L4_GRE)
3036 gso_type |= SKB_GSO_GRE;
3037 else if (flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV6)
3038 gso_type |= SKB_GSO_IPXIP6;
3039 else if (flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV4)
3040 gso_type |= SKB_GSO_IPXIP4;
3042 if (flags & BPF_F_ADJ_ROOM_ENCAP_L4_GRE ||
3043 flags & BPF_F_ADJ_ROOM_ENCAP_L4_UDP) {
3044 int nh_len = flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV6 ?
3045 sizeof(struct ipv6hdr) :
3046 sizeof(struct iphdr);
3048 skb_set_transport_header(skb, mac_len + nh_len);
3052 if (skb_is_gso(skb)) {
3053 struct skb_shared_info *shinfo = skb_shinfo(skb);
3055 /* Due to header grow, MSS needs to be downgraded. */
3056 if (!(flags & BPF_F_ADJ_ROOM_FIXED_GSO))
3057 skb_decrease_gso_size(shinfo, len_diff);
3059 /* Header must be checked, and gso_segs recomputed. */
3060 shinfo->gso_type |= gso_type;
3061 shinfo->gso_segs = 0;
3067 static int bpf_skb_net_shrink(struct sk_buff *skb, u32 off, u32 len_diff,
3072 if (skb_is_gso(skb) && !skb_is_gso_tcp(skb)) {
3073 /* udp gso_size delineates datagrams, only allow if fixed */
3074 if (!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) ||
3075 !(flags & BPF_F_ADJ_ROOM_FIXED_GSO))
3079 ret = skb_unclone(skb, GFP_ATOMIC);
3080 if (unlikely(ret < 0))
3083 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
3084 if (unlikely(ret < 0))
3087 if (skb_is_gso(skb)) {
3088 struct skb_shared_info *shinfo = skb_shinfo(skb);
3090 /* Due to header shrink, MSS can be upgraded. */
3091 if (!(flags & BPF_F_ADJ_ROOM_FIXED_GSO))
3092 skb_increase_gso_size(shinfo, len_diff);
3094 /* Header must be checked, and gso_segs recomputed. */
3095 shinfo->gso_type |= SKB_GSO_DODGY;
3096 shinfo->gso_segs = 0;
3102 static u32 __bpf_skb_max_len(const struct sk_buff *skb)
3104 return skb->dev ? skb->dev->mtu + skb->dev->hard_header_len :
3108 BPF_CALL_4(bpf_skb_adjust_room, struct sk_buff *, skb, s32, len_diff,
3109 u32, mode, u64, flags)
3111 u32 len_cur, len_diff_abs = abs(len_diff);
3112 u32 len_min = bpf_skb_net_base_len(skb);
3113 u32 len_max = __bpf_skb_max_len(skb);
3114 __be16 proto = skb->protocol;
3115 bool shrink = len_diff < 0;
3119 if (unlikely(flags & ~BPF_F_ADJ_ROOM_MASK))
3121 if (unlikely(len_diff_abs > 0xfffU))
3123 if (unlikely(proto != htons(ETH_P_IP) &&
3124 proto != htons(ETH_P_IPV6)))
3127 off = skb_mac_header_len(skb);
3129 case BPF_ADJ_ROOM_NET:
3130 off += bpf_skb_net_base_len(skb);
3132 case BPF_ADJ_ROOM_MAC:
3138 len_cur = skb->len - skb_network_offset(skb);
3139 if ((shrink && (len_diff_abs >= len_cur ||
3140 len_cur - len_diff_abs < len_min)) ||
3141 (!shrink && (skb->len + len_diff_abs > len_max &&
3145 ret = shrink ? bpf_skb_net_shrink(skb, off, len_diff_abs, flags) :
3146 bpf_skb_net_grow(skb, off, len_diff_abs, flags);
3148 bpf_compute_data_pointers(skb);
3152 static const struct bpf_func_proto bpf_skb_adjust_room_proto = {
3153 .func = bpf_skb_adjust_room,
3155 .ret_type = RET_INTEGER,
3156 .arg1_type = ARG_PTR_TO_CTX,
3157 .arg2_type = ARG_ANYTHING,
3158 .arg3_type = ARG_ANYTHING,
3159 .arg4_type = ARG_ANYTHING,
3162 static u32 __bpf_skb_min_len(const struct sk_buff *skb)
3164 u32 min_len = skb_network_offset(skb);
3166 if (skb_transport_header_was_set(skb))
3167 min_len = skb_transport_offset(skb);
3168 if (skb->ip_summed == CHECKSUM_PARTIAL)
3169 min_len = skb_checksum_start_offset(skb) +
3170 skb->csum_offset + sizeof(__sum16);
3174 static int bpf_skb_grow_rcsum(struct sk_buff *skb, unsigned int new_len)
3176 unsigned int old_len = skb->len;
3179 ret = __skb_grow_rcsum(skb, new_len);
3181 memset(skb->data + old_len, 0, new_len - old_len);
3185 static int bpf_skb_trim_rcsum(struct sk_buff *skb, unsigned int new_len)
3187 return __skb_trim_rcsum(skb, new_len);
3190 static inline int __bpf_skb_change_tail(struct sk_buff *skb, u32 new_len,
3193 u32 max_len = __bpf_skb_max_len(skb);
3194 u32 min_len = __bpf_skb_min_len(skb);
3197 if (unlikely(flags || new_len > max_len || new_len < min_len))
3199 if (skb->encapsulation)
3202 /* The basic idea of this helper is that it's performing the
3203 * needed work to either grow or trim an skb, and eBPF program
3204 * rewrites the rest via helpers like bpf_skb_store_bytes(),
3205 * bpf_lX_csum_replace() and others rather than passing a raw
3206 * buffer here. This one is a slow path helper and intended
3207 * for replies with control messages.
3209 * Like in bpf_skb_change_proto(), we want to keep this rather
3210 * minimal and without protocol specifics so that we are able
3211 * to separate concerns as in bpf_skb_store_bytes() should only
3212 * be the one responsible for writing buffers.
3214 * It's really expected to be a slow path operation here for
3215 * control message replies, so we're implicitly linearizing,
3216 * uncloning and drop offloads from the skb by this.
3218 ret = __bpf_try_make_writable(skb, skb->len);
3220 if (new_len > skb->len)
3221 ret = bpf_skb_grow_rcsum(skb, new_len);
3222 else if (new_len < skb->len)
3223 ret = bpf_skb_trim_rcsum(skb, new_len);
3224 if (!ret && skb_is_gso(skb))
3230 BPF_CALL_3(bpf_skb_change_tail, struct sk_buff *, skb, u32, new_len,
3233 int ret = __bpf_skb_change_tail(skb, new_len, flags);
3235 bpf_compute_data_pointers(skb);
3239 static const struct bpf_func_proto bpf_skb_change_tail_proto = {
3240 .func = bpf_skb_change_tail,
3242 .ret_type = RET_INTEGER,
3243 .arg1_type = ARG_PTR_TO_CTX,
3244 .arg2_type = ARG_ANYTHING,
3245 .arg3_type = ARG_ANYTHING,
3248 BPF_CALL_3(sk_skb_change_tail, struct sk_buff *, skb, u32, new_len,
3251 int ret = __bpf_skb_change_tail(skb, new_len, flags);
3253 bpf_compute_data_end_sk_skb(skb);
3257 static const struct bpf_func_proto sk_skb_change_tail_proto = {
3258 .func = sk_skb_change_tail,
3260 .ret_type = RET_INTEGER,
3261 .arg1_type = ARG_PTR_TO_CTX,
3262 .arg2_type = ARG_ANYTHING,
3263 .arg3_type = ARG_ANYTHING,
3266 static inline int __bpf_skb_change_head(struct sk_buff *skb, u32 head_room,
3269 u32 max_len = __bpf_skb_max_len(skb);
3270 u32 new_len = skb->len + head_room;
3273 if (unlikely(flags || (!skb_is_gso(skb) && new_len > max_len) ||
3274 new_len < skb->len))
3277 ret = skb_cow(skb, head_room);
3279 /* Idea for this helper is that we currently only
3280 * allow to expand on mac header. This means that
3281 * skb->protocol network header, etc, stay as is.
3282 * Compared to bpf_skb_change_tail(), we're more
3283 * flexible due to not needing to linearize or
3284 * reset GSO. Intention for this helper is to be
3285 * used by an L3 skb that needs to push mac header
3286 * for redirection into L2 device.
3288 __skb_push(skb, head_room);
3289 memset(skb->data, 0, head_room);
3290 skb_reset_mac_header(skb);
3296 BPF_CALL_3(bpf_skb_change_head, struct sk_buff *, skb, u32, head_room,
3299 int ret = __bpf_skb_change_head(skb, head_room, flags);
3301 bpf_compute_data_pointers(skb);
3305 static const struct bpf_func_proto bpf_skb_change_head_proto = {
3306 .func = bpf_skb_change_head,
3308 .ret_type = RET_INTEGER,
3309 .arg1_type = ARG_PTR_TO_CTX,
3310 .arg2_type = ARG_ANYTHING,
3311 .arg3_type = ARG_ANYTHING,
3314 BPF_CALL_3(sk_skb_change_head, struct sk_buff *, skb, u32, head_room,
3317 int ret = __bpf_skb_change_head(skb, head_room, flags);
3319 bpf_compute_data_end_sk_skb(skb);
3323 static const struct bpf_func_proto sk_skb_change_head_proto = {
3324 .func = sk_skb_change_head,
3326 .ret_type = RET_INTEGER,
3327 .arg1_type = ARG_PTR_TO_CTX,
3328 .arg2_type = ARG_ANYTHING,
3329 .arg3_type = ARG_ANYTHING,
3331 static unsigned long xdp_get_metalen(const struct xdp_buff *xdp)
3333 return xdp_data_meta_unsupported(xdp) ? 0 :
3334 xdp->data - xdp->data_meta;
3337 BPF_CALL_2(bpf_xdp_adjust_head, struct xdp_buff *, xdp, int, offset)
3339 void *xdp_frame_end = xdp->data_hard_start + sizeof(struct xdp_frame);
3340 unsigned long metalen = xdp_get_metalen(xdp);
3341 void *data_start = xdp_frame_end + metalen;
3342 void *data = xdp->data + offset;
3344 if (unlikely(data < data_start ||
3345 data > xdp->data_end - ETH_HLEN))
3349 memmove(xdp->data_meta + offset,
3350 xdp->data_meta, metalen);
3351 xdp->data_meta += offset;
3357 static const struct bpf_func_proto bpf_xdp_adjust_head_proto = {
3358 .func = bpf_xdp_adjust_head,
3360 .ret_type = RET_INTEGER,
3361 .arg1_type = ARG_PTR_TO_CTX,
3362 .arg2_type = ARG_ANYTHING,
3365 BPF_CALL_2(bpf_xdp_adjust_tail, struct xdp_buff *, xdp, int, offset)
3367 void *data_end = xdp->data_end + offset;
3369 /* only shrinking is allowed for now. */
3370 if (unlikely(offset >= 0))
3373 if (unlikely(data_end < xdp->data + ETH_HLEN))
3376 xdp->data_end = data_end;
3381 static const struct bpf_func_proto bpf_xdp_adjust_tail_proto = {
3382 .func = bpf_xdp_adjust_tail,
3384 .ret_type = RET_INTEGER,
3385 .arg1_type = ARG_PTR_TO_CTX,
3386 .arg2_type = ARG_ANYTHING,
3389 BPF_CALL_2(bpf_xdp_adjust_meta, struct xdp_buff *, xdp, int, offset)
3391 void *xdp_frame_end = xdp->data_hard_start + sizeof(struct xdp_frame);
3392 void *meta = xdp->data_meta + offset;
3393 unsigned long metalen = xdp->data - meta;
3395 if (xdp_data_meta_unsupported(xdp))
3397 if (unlikely(meta < xdp_frame_end ||
3400 if (unlikely((metalen & (sizeof(__u32) - 1)) ||
3404 xdp->data_meta = meta;
3409 static const struct bpf_func_proto bpf_xdp_adjust_meta_proto = {
3410 .func = bpf_xdp_adjust_meta,
3412 .ret_type = RET_INTEGER,
3413 .arg1_type = ARG_PTR_TO_CTX,
3414 .arg2_type = ARG_ANYTHING,
3417 static int __bpf_tx_xdp(struct net_device *dev,
3418 struct bpf_map *map,
3419 struct xdp_buff *xdp,
3422 struct xdp_frame *xdpf;
3425 if (!dev->netdev_ops->ndo_xdp_xmit) {
3429 err = xdp_ok_fwd_dev(dev, xdp->data_end - xdp->data);
3433 xdpf = convert_to_xdp_frame(xdp);
3434 if (unlikely(!xdpf))
3437 sent = dev->netdev_ops->ndo_xdp_xmit(dev, 1, &xdpf, XDP_XMIT_FLUSH);
3444 xdp_do_redirect_slow(struct net_device *dev, struct xdp_buff *xdp,
3445 struct bpf_prog *xdp_prog, struct bpf_redirect_info *ri)
3447 struct net_device *fwd;
3448 u32 index = ri->ifindex;
3451 fwd = dev_get_by_index_rcu(dev_net(dev), index);
3453 if (unlikely(!fwd)) {
3458 err = __bpf_tx_xdp(fwd, NULL, xdp, 0);
3462 _trace_xdp_redirect(dev, xdp_prog, index);
3465 _trace_xdp_redirect_err(dev, xdp_prog, index, err);
3469 static int __bpf_tx_xdp_map(struct net_device *dev_rx, void *fwd,
3470 struct bpf_map *map,
3471 struct xdp_buff *xdp,
3476 switch (map->map_type) {
3477 case BPF_MAP_TYPE_DEVMAP: {
3478 struct bpf_dtab_netdev *dst = fwd;
3480 err = dev_map_enqueue(dst, xdp, dev_rx);
3483 __dev_map_insert_ctx(map, index);
3486 case BPF_MAP_TYPE_CPUMAP: {
3487 struct bpf_cpu_map_entry *rcpu = fwd;
3489 err = cpu_map_enqueue(rcpu, xdp, dev_rx);
3492 __cpu_map_insert_ctx(map, index);
3495 case BPF_MAP_TYPE_XSKMAP: {
3496 struct xdp_sock *xs = fwd;
3498 err = __xsk_map_redirect(map, xdp, xs);
3507 void xdp_do_flush_map(void)
3509 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
3510 struct bpf_map *map = ri->map_to_flush;
3512 ri->map_to_flush = NULL;
3514 switch (map->map_type) {
3515 case BPF_MAP_TYPE_DEVMAP:
3516 __dev_map_flush(map);
3518 case BPF_MAP_TYPE_CPUMAP:
3519 __cpu_map_flush(map);
3521 case BPF_MAP_TYPE_XSKMAP:
3522 __xsk_map_flush(map);
3529 EXPORT_SYMBOL_GPL(xdp_do_flush_map);
3531 static inline void *__xdp_map_lookup_elem(struct bpf_map *map, u32 index)
3533 switch (map->map_type) {
3534 case BPF_MAP_TYPE_DEVMAP:
3535 return __dev_map_lookup_elem(map, index);
3536 case BPF_MAP_TYPE_CPUMAP:
3537 return __cpu_map_lookup_elem(map, index);
3538 case BPF_MAP_TYPE_XSKMAP:
3539 return __xsk_map_lookup_elem(map, index);
3545 void bpf_clear_redirect_map(struct bpf_map *map)
3547 struct bpf_redirect_info *ri;
3550 for_each_possible_cpu(cpu) {
3551 ri = per_cpu_ptr(&bpf_redirect_info, cpu);
3552 /* Avoid polluting remote cacheline due to writes if
3553 * not needed. Once we pass this test, we need the
3554 * cmpxchg() to make sure it hasn't been changed in
3555 * the meantime by remote CPU.
3557 if (unlikely(READ_ONCE(ri->map) == map))
3558 cmpxchg(&ri->map, map, NULL);
3562 static int xdp_do_redirect_map(struct net_device *dev, struct xdp_buff *xdp,
3563 struct bpf_prog *xdp_prog, struct bpf_map *map,
3564 struct bpf_redirect_info *ri)
3566 u32 index = ri->ifindex;
3571 WRITE_ONCE(ri->map, NULL);
3573 fwd = __xdp_map_lookup_elem(map, index);
3574 if (unlikely(!fwd)) {
3578 if (ri->map_to_flush && unlikely(ri->map_to_flush != map))
3581 err = __bpf_tx_xdp_map(dev, fwd, map, xdp, index);
3585 ri->map_to_flush = map;
3586 _trace_xdp_redirect_map(dev, xdp_prog, fwd, map, index);
3589 _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map, index, err);
3593 int xdp_do_redirect(struct net_device *dev, struct xdp_buff *xdp,
3594 struct bpf_prog *xdp_prog)
3596 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
3597 struct bpf_map *map = READ_ONCE(ri->map);
3600 return xdp_do_redirect_map(dev, xdp, xdp_prog, map, ri);
3602 return xdp_do_redirect_slow(dev, xdp, xdp_prog, ri);
3604 EXPORT_SYMBOL_GPL(xdp_do_redirect);
3606 static int xdp_do_generic_redirect_map(struct net_device *dev,
3607 struct sk_buff *skb,
3608 struct xdp_buff *xdp,
3609 struct bpf_prog *xdp_prog,
3610 struct bpf_map *map)
3612 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
3613 u32 index = ri->ifindex;
3618 WRITE_ONCE(ri->map, NULL);
3620 fwd = __xdp_map_lookup_elem(map, index);
3621 if (unlikely(!fwd)) {
3626 if (map->map_type == BPF_MAP_TYPE_DEVMAP) {
3627 struct bpf_dtab_netdev *dst = fwd;
3629 err = dev_map_generic_redirect(dst, skb, xdp_prog);
3632 } else if (map->map_type == BPF_MAP_TYPE_XSKMAP) {
3633 struct xdp_sock *xs = fwd;
3635 err = xsk_generic_rcv(xs, xdp);
3640 /* TODO: Handle BPF_MAP_TYPE_CPUMAP */
3645 _trace_xdp_redirect_map(dev, xdp_prog, fwd, map, index);
3648 _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map, index, err);
3652 int xdp_do_generic_redirect(struct net_device *dev, struct sk_buff *skb,
3653 struct xdp_buff *xdp, struct bpf_prog *xdp_prog)
3655 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
3656 struct bpf_map *map = READ_ONCE(ri->map);
3657 u32 index = ri->ifindex;
3658 struct net_device *fwd;
3662 return xdp_do_generic_redirect_map(dev, skb, xdp, xdp_prog,
3665 fwd = dev_get_by_index_rcu(dev_net(dev), index);
3666 if (unlikely(!fwd)) {
3671 err = xdp_ok_fwd_dev(fwd, skb->len);
3676 _trace_xdp_redirect(dev, xdp_prog, index);
3677 generic_xdp_tx(skb, xdp_prog);
3680 _trace_xdp_redirect_err(dev, xdp_prog, index, err);
3683 EXPORT_SYMBOL_GPL(xdp_do_generic_redirect);
3685 BPF_CALL_2(bpf_xdp_redirect, u32, ifindex, u64, flags)
3687 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
3689 if (unlikely(flags))
3692 ri->ifindex = ifindex;
3694 WRITE_ONCE(ri->map, NULL);
3696 return XDP_REDIRECT;
3699 static const struct bpf_func_proto bpf_xdp_redirect_proto = {
3700 .func = bpf_xdp_redirect,
3702 .ret_type = RET_INTEGER,
3703 .arg1_type = ARG_ANYTHING,
3704 .arg2_type = ARG_ANYTHING,
3707 BPF_CALL_3(bpf_xdp_redirect_map, struct bpf_map *, map, u32, ifindex,
3710 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
3712 if (unlikely(flags))
3715 ri->ifindex = ifindex;
3717 WRITE_ONCE(ri->map, map);
3719 return XDP_REDIRECT;
3722 static const struct bpf_func_proto bpf_xdp_redirect_map_proto = {
3723 .func = bpf_xdp_redirect_map,
3725 .ret_type = RET_INTEGER,
3726 .arg1_type = ARG_CONST_MAP_PTR,
3727 .arg2_type = ARG_ANYTHING,
3728 .arg3_type = ARG_ANYTHING,
3731 static unsigned long bpf_skb_copy(void *dst_buff, const void *skb,
3732 unsigned long off, unsigned long len)
3734 void *ptr = skb_header_pointer(skb, off, len, dst_buff);
3738 if (ptr != dst_buff)
3739 memcpy(dst_buff, ptr, len);
3744 BPF_CALL_5(bpf_skb_event_output, struct sk_buff *, skb, struct bpf_map *, map,
3745 u64, flags, void *, meta, u64, meta_size)
3747 u64 skb_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
3749 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
3751 if (unlikely(skb_size > skb->len))
3754 return bpf_event_output(map, flags, meta, meta_size, skb, skb_size,
3758 static const struct bpf_func_proto bpf_skb_event_output_proto = {
3759 .func = bpf_skb_event_output,
3761 .ret_type = RET_INTEGER,
3762 .arg1_type = ARG_PTR_TO_CTX,
3763 .arg2_type = ARG_CONST_MAP_PTR,
3764 .arg3_type = ARG_ANYTHING,
3765 .arg4_type = ARG_PTR_TO_MEM,
3766 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
3769 static unsigned short bpf_tunnel_key_af(u64 flags)
3771 return flags & BPF_F_TUNINFO_IPV6 ? AF_INET6 : AF_INET;
3774 BPF_CALL_4(bpf_skb_get_tunnel_key, struct sk_buff *, skb, struct bpf_tunnel_key *, to,
3775 u32, size, u64, flags)
3777 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
3778 u8 compat[sizeof(struct bpf_tunnel_key)];
3782 if (unlikely(!info || (flags & ~(BPF_F_TUNINFO_IPV6)))) {
3786 if (ip_tunnel_info_af(info) != bpf_tunnel_key_af(flags)) {
3790 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
3793 case offsetof(struct bpf_tunnel_key, tunnel_label):
3794 case offsetof(struct bpf_tunnel_key, tunnel_ext):
3796 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
3797 /* Fixup deprecated structure layouts here, so we have
3798 * a common path later on.
3800 if (ip_tunnel_info_af(info) != AF_INET)
3803 to = (struct bpf_tunnel_key *)compat;
3810 to->tunnel_id = be64_to_cpu(info->key.tun_id);
3811 to->tunnel_tos = info->key.tos;
3812 to->tunnel_ttl = info->key.ttl;
3815 if (flags & BPF_F_TUNINFO_IPV6) {
3816 memcpy(to->remote_ipv6, &info->key.u.ipv6.src,
3817 sizeof(to->remote_ipv6));
3818 to->tunnel_label = be32_to_cpu(info->key.label);
3820 to->remote_ipv4 = be32_to_cpu(info->key.u.ipv4.src);
3821 memset(&to->remote_ipv6[1], 0, sizeof(__u32) * 3);
3822 to->tunnel_label = 0;
3825 if (unlikely(size != sizeof(struct bpf_tunnel_key)))
3826 memcpy(to_orig, to, size);
3830 memset(to_orig, 0, size);
3834 static const struct bpf_func_proto bpf_skb_get_tunnel_key_proto = {
3835 .func = bpf_skb_get_tunnel_key,
3837 .ret_type = RET_INTEGER,
3838 .arg1_type = ARG_PTR_TO_CTX,
3839 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
3840 .arg3_type = ARG_CONST_SIZE,
3841 .arg4_type = ARG_ANYTHING,
3844 BPF_CALL_3(bpf_skb_get_tunnel_opt, struct sk_buff *, skb, u8 *, to, u32, size)
3846 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
3849 if (unlikely(!info ||
3850 !(info->key.tun_flags & TUNNEL_OPTIONS_PRESENT))) {
3854 if (unlikely(size < info->options_len)) {
3859 ip_tunnel_info_opts_get(to, info);
3860 if (size > info->options_len)
3861 memset(to + info->options_len, 0, size - info->options_len);
3863 return info->options_len;
3865 memset(to, 0, size);
3869 static const struct bpf_func_proto bpf_skb_get_tunnel_opt_proto = {
3870 .func = bpf_skb_get_tunnel_opt,
3872 .ret_type = RET_INTEGER,
3873 .arg1_type = ARG_PTR_TO_CTX,
3874 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
3875 .arg3_type = ARG_CONST_SIZE,
3878 static struct metadata_dst __percpu *md_dst;
3880 BPF_CALL_4(bpf_skb_set_tunnel_key, struct sk_buff *, skb,
3881 const struct bpf_tunnel_key *, from, u32, size, u64, flags)
3883 struct metadata_dst *md = this_cpu_ptr(md_dst);
3884 u8 compat[sizeof(struct bpf_tunnel_key)];
3885 struct ip_tunnel_info *info;
3887 if (unlikely(flags & ~(BPF_F_TUNINFO_IPV6 | BPF_F_ZERO_CSUM_TX |
3888 BPF_F_DONT_FRAGMENT | BPF_F_SEQ_NUMBER)))
3890 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
3892 case offsetof(struct bpf_tunnel_key, tunnel_label):
3893 case offsetof(struct bpf_tunnel_key, tunnel_ext):
3894 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
3895 /* Fixup deprecated structure layouts here, so we have
3896 * a common path later on.
3898 memcpy(compat, from, size);
3899 memset(compat + size, 0, sizeof(compat) - size);
3900 from = (const struct bpf_tunnel_key *) compat;
3906 if (unlikely((!(flags & BPF_F_TUNINFO_IPV6) && from->tunnel_label) ||
3911 dst_hold((struct dst_entry *) md);
3912 skb_dst_set(skb, (struct dst_entry *) md);
3914 info = &md->u.tun_info;
3915 memset(info, 0, sizeof(*info));
3916 info->mode = IP_TUNNEL_INFO_TX;
3918 info->key.tun_flags = TUNNEL_KEY | TUNNEL_CSUM | TUNNEL_NOCACHE;
3919 if (flags & BPF_F_DONT_FRAGMENT)
3920 info->key.tun_flags |= TUNNEL_DONT_FRAGMENT;
3921 if (flags & BPF_F_ZERO_CSUM_TX)
3922 info->key.tun_flags &= ~TUNNEL_CSUM;
3923 if (flags & BPF_F_SEQ_NUMBER)
3924 info->key.tun_flags |= TUNNEL_SEQ;
3926 info->key.tun_id = cpu_to_be64(from->tunnel_id);
3927 info->key.tos = from->tunnel_tos;
3928 info->key.ttl = from->tunnel_ttl;
3930 if (flags & BPF_F_TUNINFO_IPV6) {
3931 info->mode |= IP_TUNNEL_INFO_IPV6;
3932 memcpy(&info->key.u.ipv6.dst, from->remote_ipv6,
3933 sizeof(from->remote_ipv6));
3934 info->key.label = cpu_to_be32(from->tunnel_label) &
3935 IPV6_FLOWLABEL_MASK;
3937 info->key.u.ipv4.dst = cpu_to_be32(from->remote_ipv4);
3943 static const struct bpf_func_proto bpf_skb_set_tunnel_key_proto = {
3944 .func = bpf_skb_set_tunnel_key,
3946 .ret_type = RET_INTEGER,
3947 .arg1_type = ARG_PTR_TO_CTX,
3948 .arg2_type = ARG_PTR_TO_MEM,
3949 .arg3_type = ARG_CONST_SIZE,
3950 .arg4_type = ARG_ANYTHING,
3953 BPF_CALL_3(bpf_skb_set_tunnel_opt, struct sk_buff *, skb,
3954 const u8 *, from, u32, size)
3956 struct ip_tunnel_info *info = skb_tunnel_info(skb);
3957 const struct metadata_dst *md = this_cpu_ptr(md_dst);
3959 if (unlikely(info != &md->u.tun_info || (size & (sizeof(u32) - 1))))
3961 if (unlikely(size > IP_TUNNEL_OPTS_MAX))
3964 ip_tunnel_info_opts_set(info, from, size, TUNNEL_OPTIONS_PRESENT);
3969 static const struct bpf_func_proto bpf_skb_set_tunnel_opt_proto = {
3970 .func = bpf_skb_set_tunnel_opt,
3972 .ret_type = RET_INTEGER,
3973 .arg1_type = ARG_PTR_TO_CTX,
3974 .arg2_type = ARG_PTR_TO_MEM,
3975 .arg3_type = ARG_CONST_SIZE,
3978 static const struct bpf_func_proto *
3979 bpf_get_skb_set_tunnel_proto(enum bpf_func_id which)
3982 struct metadata_dst __percpu *tmp;
3984 tmp = metadata_dst_alloc_percpu(IP_TUNNEL_OPTS_MAX,
3989 if (cmpxchg(&md_dst, NULL, tmp))
3990 metadata_dst_free_percpu(tmp);
3994 case BPF_FUNC_skb_set_tunnel_key:
3995 return &bpf_skb_set_tunnel_key_proto;
3996 case BPF_FUNC_skb_set_tunnel_opt:
3997 return &bpf_skb_set_tunnel_opt_proto;
4003 BPF_CALL_3(bpf_skb_under_cgroup, struct sk_buff *, skb, struct bpf_map *, map,
4006 struct bpf_array *array = container_of(map, struct bpf_array, map);
4007 struct cgroup *cgrp;
4010 sk = skb_to_full_sk(skb);
4011 if (!sk || !sk_fullsock(sk))
4013 if (unlikely(idx >= array->map.max_entries))
4016 cgrp = READ_ONCE(array->ptrs[idx]);
4017 if (unlikely(!cgrp))
4020 return sk_under_cgroup_hierarchy(sk, cgrp);
4023 static const struct bpf_func_proto bpf_skb_under_cgroup_proto = {
4024 .func = bpf_skb_under_cgroup,
4026 .ret_type = RET_INTEGER,
4027 .arg1_type = ARG_PTR_TO_CTX,
4028 .arg2_type = ARG_CONST_MAP_PTR,
4029 .arg3_type = ARG_ANYTHING,
4032 #ifdef CONFIG_SOCK_CGROUP_DATA
4033 BPF_CALL_1(bpf_skb_cgroup_id, const struct sk_buff *, skb)
4035 struct sock *sk = skb_to_full_sk(skb);
4036 struct cgroup *cgrp;
4038 if (!sk || !sk_fullsock(sk))
4041 cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
4042 return cgrp->kn->id.id;
4045 static const struct bpf_func_proto bpf_skb_cgroup_id_proto = {
4046 .func = bpf_skb_cgroup_id,
4048 .ret_type = RET_INTEGER,
4049 .arg1_type = ARG_PTR_TO_CTX,
4052 BPF_CALL_2(bpf_skb_ancestor_cgroup_id, const struct sk_buff *, skb, int,
4055 struct sock *sk = skb_to_full_sk(skb);
4056 struct cgroup *ancestor;
4057 struct cgroup *cgrp;
4059 if (!sk || !sk_fullsock(sk))
4062 cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
4063 ancestor = cgroup_ancestor(cgrp, ancestor_level);
4067 return ancestor->kn->id.id;
4070 static const struct bpf_func_proto bpf_skb_ancestor_cgroup_id_proto = {
4071 .func = bpf_skb_ancestor_cgroup_id,
4073 .ret_type = RET_INTEGER,
4074 .arg1_type = ARG_PTR_TO_CTX,
4075 .arg2_type = ARG_ANYTHING,
4079 static unsigned long bpf_xdp_copy(void *dst_buff, const void *src_buff,
4080 unsigned long off, unsigned long len)
4082 memcpy(dst_buff, src_buff + off, len);
4086 BPF_CALL_5(bpf_xdp_event_output, struct xdp_buff *, xdp, struct bpf_map *, map,
4087 u64, flags, void *, meta, u64, meta_size)
4089 u64 xdp_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
4091 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
4093 if (unlikely(xdp_size > (unsigned long)(xdp->data_end - xdp->data)))
4096 return bpf_event_output(map, flags, meta, meta_size, xdp->data,
4097 xdp_size, bpf_xdp_copy);
4100 static const struct bpf_func_proto bpf_xdp_event_output_proto = {
4101 .func = bpf_xdp_event_output,
4103 .ret_type = RET_INTEGER,
4104 .arg1_type = ARG_PTR_TO_CTX,
4105 .arg2_type = ARG_CONST_MAP_PTR,
4106 .arg3_type = ARG_ANYTHING,
4107 .arg4_type = ARG_PTR_TO_MEM,
4108 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
4111 BPF_CALL_1(bpf_get_socket_cookie, struct sk_buff *, skb)
4113 return skb->sk ? sock_gen_cookie(skb->sk) : 0;
4116 static const struct bpf_func_proto bpf_get_socket_cookie_proto = {
4117 .func = bpf_get_socket_cookie,
4119 .ret_type = RET_INTEGER,
4120 .arg1_type = ARG_PTR_TO_CTX,
4123 BPF_CALL_1(bpf_get_socket_cookie_sock_addr, struct bpf_sock_addr_kern *, ctx)
4125 return sock_gen_cookie(ctx->sk);
4128 static const struct bpf_func_proto bpf_get_socket_cookie_sock_addr_proto = {
4129 .func = bpf_get_socket_cookie_sock_addr,
4131 .ret_type = RET_INTEGER,
4132 .arg1_type = ARG_PTR_TO_CTX,
4135 BPF_CALL_1(bpf_get_socket_cookie_sock_ops, struct bpf_sock_ops_kern *, ctx)
4137 return sock_gen_cookie(ctx->sk);
4140 static const struct bpf_func_proto bpf_get_socket_cookie_sock_ops_proto = {
4141 .func = bpf_get_socket_cookie_sock_ops,
4143 .ret_type = RET_INTEGER,
4144 .arg1_type = ARG_PTR_TO_CTX,
4147 BPF_CALL_1(bpf_get_socket_uid, struct sk_buff *, skb)
4149 struct sock *sk = sk_to_full_sk(skb->sk);
4152 if (!sk || !sk_fullsock(sk))
4154 kuid = sock_net_uid(sock_net(sk), sk);
4155 return from_kuid_munged(sock_net(sk)->user_ns, kuid);
4158 static const struct bpf_func_proto bpf_get_socket_uid_proto = {
4159 .func = bpf_get_socket_uid,
4161 .ret_type = RET_INTEGER,
4162 .arg1_type = ARG_PTR_TO_CTX,
4165 BPF_CALL_5(bpf_sockopt_event_output, struct bpf_sock_ops_kern *, bpf_sock,
4166 struct bpf_map *, map, u64, flags, void *, data, u64, size)
4168 if (unlikely(flags & ~(BPF_F_INDEX_MASK)))
4171 return bpf_event_output(map, flags, data, size, NULL, 0, NULL);
4174 static const struct bpf_func_proto bpf_sockopt_event_output_proto = {
4175 .func = bpf_sockopt_event_output,
4177 .ret_type = RET_INTEGER,
4178 .arg1_type = ARG_PTR_TO_CTX,
4179 .arg2_type = ARG_CONST_MAP_PTR,
4180 .arg3_type = ARG_ANYTHING,
4181 .arg4_type = ARG_PTR_TO_MEM,
4182 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
4185 BPF_CALL_5(bpf_setsockopt, struct bpf_sock_ops_kern *, bpf_sock,
4186 int, level, int, optname, char *, optval, int, optlen)
4188 struct sock *sk = bpf_sock->sk;
4192 if (!sk_fullsock(sk))
4195 if (level == SOL_SOCKET) {
4196 if (optlen != sizeof(int))
4198 val = *((int *)optval);
4200 /* Only some socketops are supported */
4203 val = min_t(u32, val, sysctl_rmem_max);
4204 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
4205 sk->sk_rcvbuf = max_t(int, val * 2, SOCK_MIN_RCVBUF);
4208 val = min_t(u32, val, sysctl_wmem_max);
4209 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
4210 sk->sk_sndbuf = max_t(int, val * 2, SOCK_MIN_SNDBUF);
4212 case SO_MAX_PACING_RATE: /* 32bit version */
4214 cmpxchg(&sk->sk_pacing_status,
4217 sk->sk_max_pacing_rate = (val == ~0U) ? ~0UL : val;
4218 sk->sk_pacing_rate = min(sk->sk_pacing_rate,
4219 sk->sk_max_pacing_rate);
4222 sk->sk_priority = val;
4227 sk->sk_rcvlowat = val ? : 1;
4230 if (sk->sk_mark != val) {
4239 } else if (level == SOL_IP) {
4240 if (optlen != sizeof(int) || sk->sk_family != AF_INET)
4243 val = *((int *)optval);
4244 /* Only some options are supported */
4247 if (val < -1 || val > 0xff) {
4250 struct inet_sock *inet = inet_sk(sk);
4260 #if IS_ENABLED(CONFIG_IPV6)
4261 } else if (level == SOL_IPV6) {
4262 if (optlen != sizeof(int) || sk->sk_family != AF_INET6)
4265 val = *((int *)optval);
4266 /* Only some options are supported */
4269 if (val < -1 || val > 0xff) {
4272 struct ipv6_pinfo *np = inet6_sk(sk);
4283 } else if (level == SOL_TCP &&
4284 sk->sk_prot->setsockopt == tcp_setsockopt) {
4285 if (optname == TCP_CONGESTION) {
4286 char name[TCP_CA_NAME_MAX];
4287 bool reinit = bpf_sock->op > BPF_SOCK_OPS_NEEDS_ECN;
4289 strncpy(name, optval, min_t(long, optlen,
4290 TCP_CA_NAME_MAX-1));
4291 name[TCP_CA_NAME_MAX-1] = 0;
4292 ret = tcp_set_congestion_control(sk, name, false,
4295 struct tcp_sock *tp = tcp_sk(sk);
4297 if (optlen != sizeof(int))
4300 val = *((int *)optval);
4301 /* Only some options are supported */
4304 if (val <= 0 || tp->data_segs_out > tp->syn_data)
4309 case TCP_BPF_SNDCWND_CLAMP:
4313 tp->snd_cwnd_clamp = val;
4314 tp->snd_ssthresh = val;
4318 if (val < 0 || val > 1)
4334 static const struct bpf_func_proto bpf_setsockopt_proto = {
4335 .func = bpf_setsockopt,
4337 .ret_type = RET_INTEGER,
4338 .arg1_type = ARG_PTR_TO_CTX,
4339 .arg2_type = ARG_ANYTHING,
4340 .arg3_type = ARG_ANYTHING,
4341 .arg4_type = ARG_PTR_TO_MEM,
4342 .arg5_type = ARG_CONST_SIZE,
4345 BPF_CALL_5(bpf_getsockopt, struct bpf_sock_ops_kern *, bpf_sock,
4346 int, level, int, optname, char *, optval, int, optlen)
4348 struct sock *sk = bpf_sock->sk;
4350 if (!sk_fullsock(sk))
4353 if (level == SOL_TCP && sk->sk_prot->getsockopt == tcp_getsockopt) {
4354 struct inet_connection_sock *icsk;
4355 struct tcp_sock *tp;
4358 case TCP_CONGESTION:
4359 icsk = inet_csk(sk);
4361 if (!icsk->icsk_ca_ops || optlen <= 1)
4363 strncpy(optval, icsk->icsk_ca_ops->name, optlen);
4364 optval[optlen - 1] = 0;
4369 if (optlen <= 0 || !tp->saved_syn ||
4370 optlen > tp->saved_syn[0])
4372 memcpy(optval, tp->saved_syn + 1, optlen);
4377 } else if (level == SOL_IP) {
4378 struct inet_sock *inet = inet_sk(sk);
4380 if (optlen != sizeof(int) || sk->sk_family != AF_INET)
4383 /* Only some options are supported */
4386 *((int *)optval) = (int)inet->tos;
4391 #if IS_ENABLED(CONFIG_IPV6)
4392 } else if (level == SOL_IPV6) {
4393 struct ipv6_pinfo *np = inet6_sk(sk);
4395 if (optlen != sizeof(int) || sk->sk_family != AF_INET6)
4398 /* Only some options are supported */
4401 *((int *)optval) = (int)np->tclass;
4413 memset(optval, 0, optlen);
4417 static const struct bpf_func_proto bpf_getsockopt_proto = {
4418 .func = bpf_getsockopt,
4420 .ret_type = RET_INTEGER,
4421 .arg1_type = ARG_PTR_TO_CTX,
4422 .arg2_type = ARG_ANYTHING,
4423 .arg3_type = ARG_ANYTHING,
4424 .arg4_type = ARG_PTR_TO_UNINIT_MEM,
4425 .arg5_type = ARG_CONST_SIZE,
4428 BPF_CALL_2(bpf_sock_ops_cb_flags_set, struct bpf_sock_ops_kern *, bpf_sock,
4431 struct sock *sk = bpf_sock->sk;
4432 int val = argval & BPF_SOCK_OPS_ALL_CB_FLAGS;
4434 if (!IS_ENABLED(CONFIG_INET) || !sk_fullsock(sk))
4438 tcp_sk(sk)->bpf_sock_ops_cb_flags = val;
4440 return argval & (~BPF_SOCK_OPS_ALL_CB_FLAGS);
4443 static const struct bpf_func_proto bpf_sock_ops_cb_flags_set_proto = {
4444 .func = bpf_sock_ops_cb_flags_set,
4446 .ret_type = RET_INTEGER,
4447 .arg1_type = ARG_PTR_TO_CTX,
4448 .arg2_type = ARG_ANYTHING,
4451 const struct ipv6_bpf_stub *ipv6_bpf_stub __read_mostly;
4452 EXPORT_SYMBOL_GPL(ipv6_bpf_stub);
4454 BPF_CALL_3(bpf_bind, struct bpf_sock_addr_kern *, ctx, struct sockaddr *, addr,
4458 struct sock *sk = ctx->sk;
4461 /* Binding to port can be expensive so it's prohibited in the helper.
4462 * Only binding to IP is supported.
4465 if (addr->sa_family == AF_INET) {
4466 if (addr_len < sizeof(struct sockaddr_in))
4468 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
4470 return __inet_bind(sk, addr, addr_len, true, false);
4471 #if IS_ENABLED(CONFIG_IPV6)
4472 } else if (addr->sa_family == AF_INET6) {
4473 if (addr_len < SIN6_LEN_RFC2133)
4475 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
4477 /* ipv6_bpf_stub cannot be NULL, since it's called from
4478 * bpf_cgroup_inet6_connect hook and ipv6 is already loaded
4480 return ipv6_bpf_stub->inet6_bind(sk, addr, addr_len, true, false);
4481 #endif /* CONFIG_IPV6 */
4483 #endif /* CONFIG_INET */
4485 return -EAFNOSUPPORT;
4488 static const struct bpf_func_proto bpf_bind_proto = {
4491 .ret_type = RET_INTEGER,
4492 .arg1_type = ARG_PTR_TO_CTX,
4493 .arg2_type = ARG_PTR_TO_MEM,
4494 .arg3_type = ARG_CONST_SIZE,
4498 BPF_CALL_5(bpf_skb_get_xfrm_state, struct sk_buff *, skb, u32, index,
4499 struct bpf_xfrm_state *, to, u32, size, u64, flags)
4501 const struct sec_path *sp = skb_sec_path(skb);
4502 const struct xfrm_state *x;
4504 if (!sp || unlikely(index >= sp->len || flags))
4507 x = sp->xvec[index];
4509 if (unlikely(size != sizeof(struct bpf_xfrm_state)))
4512 to->reqid = x->props.reqid;
4513 to->spi = x->id.spi;
4514 to->family = x->props.family;
4517 if (to->family == AF_INET6) {
4518 memcpy(to->remote_ipv6, x->props.saddr.a6,
4519 sizeof(to->remote_ipv6));
4521 to->remote_ipv4 = x->props.saddr.a4;
4522 memset(&to->remote_ipv6[1], 0, sizeof(__u32) * 3);
4527 memset(to, 0, size);
4531 static const struct bpf_func_proto bpf_skb_get_xfrm_state_proto = {
4532 .func = bpf_skb_get_xfrm_state,
4534 .ret_type = RET_INTEGER,
4535 .arg1_type = ARG_PTR_TO_CTX,
4536 .arg2_type = ARG_ANYTHING,
4537 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
4538 .arg4_type = ARG_CONST_SIZE,
4539 .arg5_type = ARG_ANYTHING,
4543 #if IS_ENABLED(CONFIG_INET) || IS_ENABLED(CONFIG_IPV6)
4544 static int bpf_fib_set_fwd_params(struct bpf_fib_lookup *params,
4545 const struct neighbour *neigh,
4546 const struct net_device *dev)
4548 memcpy(params->dmac, neigh->ha, ETH_ALEN);
4549 memcpy(params->smac, dev->dev_addr, ETH_ALEN);
4550 params->h_vlan_TCI = 0;
4551 params->h_vlan_proto = 0;
4552 params->ifindex = dev->ifindex;
4558 #if IS_ENABLED(CONFIG_INET)
4559 static int bpf_ipv4_fib_lookup(struct net *net, struct bpf_fib_lookup *params,
4560 u32 flags, bool check_mtu)
4562 struct fib_nh_common *nhc;
4563 struct in_device *in_dev;
4564 struct neighbour *neigh;
4565 struct net_device *dev;
4566 struct fib_result res;
4571 dev = dev_get_by_index_rcu(net, params->ifindex);
4575 /* verify forwarding is enabled on this interface */
4576 in_dev = __in_dev_get_rcu(dev);
4577 if (unlikely(!in_dev || !IN_DEV_FORWARD(in_dev)))
4578 return BPF_FIB_LKUP_RET_FWD_DISABLED;
4580 if (flags & BPF_FIB_LOOKUP_OUTPUT) {
4582 fl4.flowi4_oif = params->ifindex;
4584 fl4.flowi4_iif = params->ifindex;
4587 fl4.flowi4_tos = params->tos & IPTOS_RT_MASK;
4588 fl4.flowi4_scope = RT_SCOPE_UNIVERSE;
4589 fl4.flowi4_flags = 0;
4591 fl4.flowi4_proto = params->l4_protocol;
4592 fl4.daddr = params->ipv4_dst;
4593 fl4.saddr = params->ipv4_src;
4594 fl4.fl4_sport = params->sport;
4595 fl4.fl4_dport = params->dport;
4597 if (flags & BPF_FIB_LOOKUP_DIRECT) {
4598 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
4599 struct fib_table *tb;
4601 tb = fib_get_table(net, tbid);
4603 return BPF_FIB_LKUP_RET_NOT_FWDED;
4605 err = fib_table_lookup(tb, &fl4, &res, FIB_LOOKUP_NOREF);
4607 fl4.flowi4_mark = 0;
4608 fl4.flowi4_secid = 0;
4609 fl4.flowi4_tun_key.tun_id = 0;
4610 fl4.flowi4_uid = sock_net_uid(net, NULL);
4612 err = fib_lookup(net, &fl4, &res, FIB_LOOKUP_NOREF);
4616 /* map fib lookup errors to RTN_ type */
4618 return BPF_FIB_LKUP_RET_BLACKHOLE;
4619 if (err == -EHOSTUNREACH)
4620 return BPF_FIB_LKUP_RET_UNREACHABLE;
4622 return BPF_FIB_LKUP_RET_PROHIBIT;
4624 return BPF_FIB_LKUP_RET_NOT_FWDED;
4627 if (res.type != RTN_UNICAST)
4628 return BPF_FIB_LKUP_RET_NOT_FWDED;
4630 if (res.fi->fib_nhs > 1)
4631 fib_select_path(net, &res, &fl4, NULL);
4634 mtu = ip_mtu_from_fib_result(&res, params->ipv4_dst);
4635 if (params->tot_len > mtu)
4636 return BPF_FIB_LKUP_RET_FRAG_NEEDED;
4641 /* do not handle lwt encaps right now */
4642 if (nhc->nhc_lwtstate)
4643 return BPF_FIB_LKUP_RET_UNSUPP_LWT;
4647 params->rt_metric = res.fi->fib_priority;
4649 /* xdp and cls_bpf programs are run in RCU-bh so
4650 * rcu_read_lock_bh is not needed here
4652 if (likely(nhc->nhc_gw_family != AF_INET6)) {
4653 if (nhc->nhc_gw_family)
4654 params->ipv4_dst = nhc->nhc_gw.ipv4;
4656 neigh = __ipv4_neigh_lookup_noref(dev,
4657 (__force u32)params->ipv4_dst);
4659 struct in6_addr *dst = (struct in6_addr *)params->ipv6_dst;
4661 params->family = AF_INET6;
4662 *dst = nhc->nhc_gw.ipv6;
4663 neigh = __ipv6_neigh_lookup_noref_stub(dev, dst);
4667 return BPF_FIB_LKUP_RET_NO_NEIGH;
4669 return bpf_fib_set_fwd_params(params, neigh, dev);
4673 #if IS_ENABLED(CONFIG_IPV6)
4674 static int bpf_ipv6_fib_lookup(struct net *net, struct bpf_fib_lookup *params,
4675 u32 flags, bool check_mtu)
4677 struct in6_addr *src = (struct in6_addr *) params->ipv6_src;
4678 struct in6_addr *dst = (struct in6_addr *) params->ipv6_dst;
4679 struct neighbour *neigh;
4680 struct net_device *dev;
4681 struct inet6_dev *idev;
4682 struct fib6_info *f6i;
4688 /* link local addresses are never forwarded */
4689 if (rt6_need_strict(dst) || rt6_need_strict(src))
4690 return BPF_FIB_LKUP_RET_NOT_FWDED;
4692 dev = dev_get_by_index_rcu(net, params->ifindex);
4696 idev = __in6_dev_get_safely(dev);
4697 if (unlikely(!idev || !net->ipv6.devconf_all->forwarding))
4698 return BPF_FIB_LKUP_RET_FWD_DISABLED;
4700 if (flags & BPF_FIB_LOOKUP_OUTPUT) {
4702 oif = fl6.flowi6_oif = params->ifindex;
4704 oif = fl6.flowi6_iif = params->ifindex;
4706 strict = RT6_LOOKUP_F_HAS_SADDR;
4708 fl6.flowlabel = params->flowinfo;
4709 fl6.flowi6_scope = 0;
4710 fl6.flowi6_flags = 0;
4713 fl6.flowi6_proto = params->l4_protocol;
4716 fl6.fl6_sport = params->sport;
4717 fl6.fl6_dport = params->dport;
4719 if (flags & BPF_FIB_LOOKUP_DIRECT) {
4720 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
4721 struct fib6_table *tb;
4723 tb = ipv6_stub->fib6_get_table(net, tbid);
4725 return BPF_FIB_LKUP_RET_NOT_FWDED;
4727 f6i = ipv6_stub->fib6_table_lookup(net, tb, oif, &fl6, strict);
4729 fl6.flowi6_mark = 0;
4730 fl6.flowi6_secid = 0;
4731 fl6.flowi6_tun_key.tun_id = 0;
4732 fl6.flowi6_uid = sock_net_uid(net, NULL);
4734 f6i = ipv6_stub->fib6_lookup(net, oif, &fl6, strict);
4737 if (unlikely(IS_ERR_OR_NULL(f6i) || f6i == net->ipv6.fib6_null_entry))
4738 return BPF_FIB_LKUP_RET_NOT_FWDED;
4740 if (unlikely(f6i->fib6_flags & RTF_REJECT)) {
4741 switch (f6i->fib6_type) {
4743 return BPF_FIB_LKUP_RET_BLACKHOLE;
4744 case RTN_UNREACHABLE:
4745 return BPF_FIB_LKUP_RET_UNREACHABLE;
4747 return BPF_FIB_LKUP_RET_PROHIBIT;
4749 return BPF_FIB_LKUP_RET_NOT_FWDED;
4753 if (f6i->fib6_type != RTN_UNICAST)
4754 return BPF_FIB_LKUP_RET_NOT_FWDED;
4756 if (f6i->fib6_nsiblings && fl6.flowi6_oif == 0)
4757 f6i = ipv6_stub->fib6_multipath_select(net, f6i, &fl6,
4758 fl6.flowi6_oif, NULL,
4762 mtu = ipv6_stub->ip6_mtu_from_fib6(f6i, dst, src);
4763 if (params->tot_len > mtu)
4764 return BPF_FIB_LKUP_RET_FRAG_NEEDED;
4767 if (f6i->fib6_nh.fib_nh_lws)
4768 return BPF_FIB_LKUP_RET_UNSUPP_LWT;
4770 if (f6i->fib6_nh.fib_nh_gw_family)
4771 *dst = f6i->fib6_nh.fib_nh_gw6;
4773 dev = f6i->fib6_nh.fib_nh_dev;
4774 params->rt_metric = f6i->fib6_metric;
4776 /* xdp and cls_bpf programs are run in RCU-bh so rcu_read_lock_bh is
4779 neigh = __ipv6_neigh_lookup_noref_stub(dev, dst);
4781 return BPF_FIB_LKUP_RET_NO_NEIGH;
4783 return bpf_fib_set_fwd_params(params, neigh, dev);
4787 BPF_CALL_4(bpf_xdp_fib_lookup, struct xdp_buff *, ctx,
4788 struct bpf_fib_lookup *, params, int, plen, u32, flags)
4790 if (plen < sizeof(*params))
4793 if (flags & ~(BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_OUTPUT))
4796 switch (params->family) {
4797 #if IS_ENABLED(CONFIG_INET)
4799 return bpf_ipv4_fib_lookup(dev_net(ctx->rxq->dev), params,
4802 #if IS_ENABLED(CONFIG_IPV6)
4804 return bpf_ipv6_fib_lookup(dev_net(ctx->rxq->dev), params,
4808 return -EAFNOSUPPORT;
4811 static const struct bpf_func_proto bpf_xdp_fib_lookup_proto = {
4812 .func = bpf_xdp_fib_lookup,
4814 .ret_type = RET_INTEGER,
4815 .arg1_type = ARG_PTR_TO_CTX,
4816 .arg2_type = ARG_PTR_TO_MEM,
4817 .arg3_type = ARG_CONST_SIZE,
4818 .arg4_type = ARG_ANYTHING,
4821 BPF_CALL_4(bpf_skb_fib_lookup, struct sk_buff *, skb,
4822 struct bpf_fib_lookup *, params, int, plen, u32, flags)
4824 struct net *net = dev_net(skb->dev);
4825 int rc = -EAFNOSUPPORT;
4827 if (plen < sizeof(*params))
4830 if (flags & ~(BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_OUTPUT))
4833 switch (params->family) {
4834 #if IS_ENABLED(CONFIG_INET)
4836 rc = bpf_ipv4_fib_lookup(net, params, flags, false);
4839 #if IS_ENABLED(CONFIG_IPV6)
4841 rc = bpf_ipv6_fib_lookup(net, params, flags, false);
4847 struct net_device *dev;
4849 dev = dev_get_by_index_rcu(net, params->ifindex);
4850 if (!is_skb_forwardable(dev, skb))
4851 rc = BPF_FIB_LKUP_RET_FRAG_NEEDED;
4857 static const struct bpf_func_proto bpf_skb_fib_lookup_proto = {
4858 .func = bpf_skb_fib_lookup,
4860 .ret_type = RET_INTEGER,
4861 .arg1_type = ARG_PTR_TO_CTX,
4862 .arg2_type = ARG_PTR_TO_MEM,
4863 .arg3_type = ARG_CONST_SIZE,
4864 .arg4_type = ARG_ANYTHING,
4867 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
4868 static int bpf_push_seg6_encap(struct sk_buff *skb, u32 type, void *hdr, u32 len)
4871 struct ipv6_sr_hdr *srh = (struct ipv6_sr_hdr *)hdr;
4873 if (!seg6_validate_srh(srh, len))
4877 case BPF_LWT_ENCAP_SEG6_INLINE:
4878 if (skb->protocol != htons(ETH_P_IPV6))
4881 err = seg6_do_srh_inline(skb, srh);
4883 case BPF_LWT_ENCAP_SEG6:
4884 skb_reset_inner_headers(skb);
4885 skb->encapsulation = 1;
4886 err = seg6_do_srh_encap(skb, srh, IPPROTO_IPV6);
4892 bpf_compute_data_pointers(skb);
4896 ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
4897 skb_set_transport_header(skb, sizeof(struct ipv6hdr));
4899 return seg6_lookup_nexthop(skb, NULL, 0);
4901 #endif /* CONFIG_IPV6_SEG6_BPF */
4903 #if IS_ENABLED(CONFIG_LWTUNNEL_BPF)
4904 static int bpf_push_ip_encap(struct sk_buff *skb, void *hdr, u32 len,
4907 return bpf_lwt_push_ip_encap(skb, hdr, len, ingress);
4911 BPF_CALL_4(bpf_lwt_in_push_encap, struct sk_buff *, skb, u32, type, void *, hdr,
4915 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
4916 case BPF_LWT_ENCAP_SEG6:
4917 case BPF_LWT_ENCAP_SEG6_INLINE:
4918 return bpf_push_seg6_encap(skb, type, hdr, len);
4920 #if IS_ENABLED(CONFIG_LWTUNNEL_BPF)
4921 case BPF_LWT_ENCAP_IP:
4922 return bpf_push_ip_encap(skb, hdr, len, true /* ingress */);
4929 BPF_CALL_4(bpf_lwt_xmit_push_encap, struct sk_buff *, skb, u32, type,
4930 void *, hdr, u32, len)
4933 #if IS_ENABLED(CONFIG_LWTUNNEL_BPF)
4934 case BPF_LWT_ENCAP_IP:
4935 return bpf_push_ip_encap(skb, hdr, len, false /* egress */);
4942 static const struct bpf_func_proto bpf_lwt_in_push_encap_proto = {
4943 .func = bpf_lwt_in_push_encap,
4945 .ret_type = RET_INTEGER,
4946 .arg1_type = ARG_PTR_TO_CTX,
4947 .arg2_type = ARG_ANYTHING,
4948 .arg3_type = ARG_PTR_TO_MEM,
4949 .arg4_type = ARG_CONST_SIZE
4952 static const struct bpf_func_proto bpf_lwt_xmit_push_encap_proto = {
4953 .func = bpf_lwt_xmit_push_encap,
4955 .ret_type = RET_INTEGER,
4956 .arg1_type = ARG_PTR_TO_CTX,
4957 .arg2_type = ARG_ANYTHING,
4958 .arg3_type = ARG_PTR_TO_MEM,
4959 .arg4_type = ARG_CONST_SIZE
4962 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
4963 BPF_CALL_4(bpf_lwt_seg6_store_bytes, struct sk_buff *, skb, u32, offset,
4964 const void *, from, u32, len)
4966 struct seg6_bpf_srh_state *srh_state =
4967 this_cpu_ptr(&seg6_bpf_srh_states);
4968 struct ipv6_sr_hdr *srh = srh_state->srh;
4969 void *srh_tlvs, *srh_end, *ptr;
4975 srh_tlvs = (void *)((char *)srh + ((srh->first_segment + 1) << 4));
4976 srh_end = (void *)((char *)srh + sizeof(*srh) + srh_state->hdrlen);
4978 ptr = skb->data + offset;
4979 if (ptr >= srh_tlvs && ptr + len <= srh_end)
4980 srh_state->valid = false;
4981 else if (ptr < (void *)&srh->flags ||
4982 ptr + len > (void *)&srh->segments)
4985 if (unlikely(bpf_try_make_writable(skb, offset + len)))
4987 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0)
4989 srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
4991 memcpy(skb->data + offset, from, len);
4995 static const struct bpf_func_proto bpf_lwt_seg6_store_bytes_proto = {
4996 .func = bpf_lwt_seg6_store_bytes,
4998 .ret_type = RET_INTEGER,
4999 .arg1_type = ARG_PTR_TO_CTX,
5000 .arg2_type = ARG_ANYTHING,
5001 .arg3_type = ARG_PTR_TO_MEM,
5002 .arg4_type = ARG_CONST_SIZE
5005 static void bpf_update_srh_state(struct sk_buff *skb)
5007 struct seg6_bpf_srh_state *srh_state =
5008 this_cpu_ptr(&seg6_bpf_srh_states);
5011 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0) {
5012 srh_state->srh = NULL;
5014 srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
5015 srh_state->hdrlen = srh_state->srh->hdrlen << 3;
5016 srh_state->valid = true;
5020 BPF_CALL_4(bpf_lwt_seg6_action, struct sk_buff *, skb,
5021 u32, action, void *, param, u32, param_len)
5023 struct seg6_bpf_srh_state *srh_state =
5024 this_cpu_ptr(&seg6_bpf_srh_states);
5029 case SEG6_LOCAL_ACTION_END_X:
5030 if (!seg6_bpf_has_valid_srh(skb))
5032 if (param_len != sizeof(struct in6_addr))
5034 return seg6_lookup_nexthop(skb, (struct in6_addr *)param, 0);
5035 case SEG6_LOCAL_ACTION_END_T:
5036 if (!seg6_bpf_has_valid_srh(skb))
5038 if (param_len != sizeof(int))
5040 return seg6_lookup_nexthop(skb, NULL, *(int *)param);
5041 case SEG6_LOCAL_ACTION_END_DT6:
5042 if (!seg6_bpf_has_valid_srh(skb))
5044 if (param_len != sizeof(int))
5047 if (ipv6_find_hdr(skb, &hdroff, IPPROTO_IPV6, NULL, NULL) < 0)
5049 if (!pskb_pull(skb, hdroff))
5052 skb_postpull_rcsum(skb, skb_network_header(skb), hdroff);
5053 skb_reset_network_header(skb);
5054 skb_reset_transport_header(skb);
5055 skb->encapsulation = 0;
5057 bpf_compute_data_pointers(skb);
5058 bpf_update_srh_state(skb);
5059 return seg6_lookup_nexthop(skb, NULL, *(int *)param);
5060 case SEG6_LOCAL_ACTION_END_B6:
5061 if (srh_state->srh && !seg6_bpf_has_valid_srh(skb))
5063 err = bpf_push_seg6_encap(skb, BPF_LWT_ENCAP_SEG6_INLINE,
5066 bpf_update_srh_state(skb);
5069 case SEG6_LOCAL_ACTION_END_B6_ENCAP:
5070 if (srh_state->srh && !seg6_bpf_has_valid_srh(skb))
5072 err = bpf_push_seg6_encap(skb, BPF_LWT_ENCAP_SEG6,
5075 bpf_update_srh_state(skb);
5083 static const struct bpf_func_proto bpf_lwt_seg6_action_proto = {
5084 .func = bpf_lwt_seg6_action,
5086 .ret_type = RET_INTEGER,
5087 .arg1_type = ARG_PTR_TO_CTX,
5088 .arg2_type = ARG_ANYTHING,
5089 .arg3_type = ARG_PTR_TO_MEM,
5090 .arg4_type = ARG_CONST_SIZE
5093 BPF_CALL_3(bpf_lwt_seg6_adjust_srh, struct sk_buff *, skb, u32, offset,
5096 struct seg6_bpf_srh_state *srh_state =
5097 this_cpu_ptr(&seg6_bpf_srh_states);
5098 struct ipv6_sr_hdr *srh = srh_state->srh;
5099 void *srh_end, *srh_tlvs, *ptr;
5100 struct ipv6hdr *hdr;
5104 if (unlikely(srh == NULL))
5107 srh_tlvs = (void *)((unsigned char *)srh + sizeof(*srh) +
5108 ((srh->first_segment + 1) << 4));
5109 srh_end = (void *)((unsigned char *)srh + sizeof(*srh) +
5111 ptr = skb->data + offset;
5113 if (unlikely(ptr < srh_tlvs || ptr > srh_end))
5115 if (unlikely(len < 0 && (void *)((char *)ptr - len) > srh_end))
5119 ret = skb_cow_head(skb, len);
5120 if (unlikely(ret < 0))
5123 ret = bpf_skb_net_hdr_push(skb, offset, len);
5125 ret = bpf_skb_net_hdr_pop(skb, offset, -1 * len);
5128 bpf_compute_data_pointers(skb);
5129 if (unlikely(ret < 0))
5132 hdr = (struct ipv6hdr *)skb->data;
5133 hdr->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
5135 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0)
5137 srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
5138 srh_state->hdrlen += len;
5139 srh_state->valid = false;
5143 static const struct bpf_func_proto bpf_lwt_seg6_adjust_srh_proto = {
5144 .func = bpf_lwt_seg6_adjust_srh,
5146 .ret_type = RET_INTEGER,
5147 .arg1_type = ARG_PTR_TO_CTX,
5148 .arg2_type = ARG_ANYTHING,
5149 .arg3_type = ARG_ANYTHING,
5151 #endif /* CONFIG_IPV6_SEG6_BPF */
5153 #define CONVERT_COMMON_TCP_SOCK_FIELDS(md_type, CONVERT) \
5155 switch (si->off) { \
5156 case offsetof(md_type, snd_cwnd): \
5157 CONVERT(snd_cwnd); break; \
5158 case offsetof(md_type, srtt_us): \
5159 CONVERT(srtt_us); break; \
5160 case offsetof(md_type, snd_ssthresh): \
5161 CONVERT(snd_ssthresh); break; \
5162 case offsetof(md_type, rcv_nxt): \
5163 CONVERT(rcv_nxt); break; \
5164 case offsetof(md_type, snd_nxt): \
5165 CONVERT(snd_nxt); break; \
5166 case offsetof(md_type, snd_una): \
5167 CONVERT(snd_una); break; \
5168 case offsetof(md_type, mss_cache): \
5169 CONVERT(mss_cache); break; \
5170 case offsetof(md_type, ecn_flags): \
5171 CONVERT(ecn_flags); break; \
5172 case offsetof(md_type, rate_delivered): \
5173 CONVERT(rate_delivered); break; \
5174 case offsetof(md_type, rate_interval_us): \
5175 CONVERT(rate_interval_us); break; \
5176 case offsetof(md_type, packets_out): \
5177 CONVERT(packets_out); break; \
5178 case offsetof(md_type, retrans_out): \
5179 CONVERT(retrans_out); break; \
5180 case offsetof(md_type, total_retrans): \
5181 CONVERT(total_retrans); break; \
5182 case offsetof(md_type, segs_in): \
5183 CONVERT(segs_in); break; \
5184 case offsetof(md_type, data_segs_in): \
5185 CONVERT(data_segs_in); break; \
5186 case offsetof(md_type, segs_out): \
5187 CONVERT(segs_out); break; \
5188 case offsetof(md_type, data_segs_out): \
5189 CONVERT(data_segs_out); break; \
5190 case offsetof(md_type, lost_out): \
5191 CONVERT(lost_out); break; \
5192 case offsetof(md_type, sacked_out): \
5193 CONVERT(sacked_out); break; \
5194 case offsetof(md_type, bytes_received): \
5195 CONVERT(bytes_received); break; \
5196 case offsetof(md_type, bytes_acked): \
5197 CONVERT(bytes_acked); break; \
5202 static struct sock *sk_lookup(struct net *net, struct bpf_sock_tuple *tuple,
5203 int dif, int sdif, u8 family, u8 proto)
5205 bool refcounted = false;
5206 struct sock *sk = NULL;
5208 if (family == AF_INET) {
5209 __be32 src4 = tuple->ipv4.saddr;
5210 __be32 dst4 = tuple->ipv4.daddr;
5212 if (proto == IPPROTO_TCP)
5213 sk = __inet_lookup(net, &tcp_hashinfo, NULL, 0,
5214 src4, tuple->ipv4.sport,
5215 dst4, tuple->ipv4.dport,
5216 dif, sdif, &refcounted);
5218 sk = __udp4_lib_lookup(net, src4, tuple->ipv4.sport,
5219 dst4, tuple->ipv4.dport,
5220 dif, sdif, &udp_table, NULL);
5221 #if IS_ENABLED(CONFIG_IPV6)
5223 struct in6_addr *src6 = (struct in6_addr *)&tuple->ipv6.saddr;
5224 struct in6_addr *dst6 = (struct in6_addr *)&tuple->ipv6.daddr;
5226 if (proto == IPPROTO_TCP)
5227 sk = __inet6_lookup(net, &tcp_hashinfo, NULL, 0,
5228 src6, tuple->ipv6.sport,
5229 dst6, ntohs(tuple->ipv6.dport),
5230 dif, sdif, &refcounted);
5231 else if (likely(ipv6_bpf_stub))
5232 sk = ipv6_bpf_stub->udp6_lib_lookup(net,
5233 src6, tuple->ipv6.sport,
5234 dst6, tuple->ipv6.dport,
5240 if (unlikely(sk && !refcounted && !sock_flag(sk, SOCK_RCU_FREE))) {
5241 WARN_ONCE(1, "Found non-RCU, unreferenced socket!");
5247 /* bpf_skc_lookup performs the core lookup for different types of sockets,
5248 * taking a reference on the socket if it doesn't have the flag SOCK_RCU_FREE.
5249 * Returns the socket as an 'unsigned long' to simplify the casting in the
5250 * callers to satisfy BPF_CALL declarations.
5252 static struct sock *
5253 __bpf_skc_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
5254 struct net *caller_net, u32 ifindex, u8 proto, u64 netns_id,
5257 struct sock *sk = NULL;
5258 u8 family = AF_UNSPEC;
5262 family = len == sizeof(tuple->ipv4) ? AF_INET : AF_INET6;
5263 if (unlikely(family == AF_UNSPEC || flags ||
5264 !((s32)netns_id < 0 || netns_id <= S32_MAX)))
5267 if (family == AF_INET)
5268 sdif = inet_sdif(skb);
5270 sdif = inet6_sdif(skb);
5272 if ((s32)netns_id < 0) {
5274 sk = sk_lookup(net, tuple, ifindex, sdif, family, proto);
5276 net = get_net_ns_by_id(caller_net, netns_id);
5279 sk = sk_lookup(net, tuple, ifindex, sdif, family, proto);
5287 static struct sock *
5288 __bpf_sk_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
5289 struct net *caller_net, u32 ifindex, u8 proto, u64 netns_id,
5292 struct sock *sk = __bpf_skc_lookup(skb, tuple, len, caller_net,
5293 ifindex, proto, netns_id, flags);
5296 sk = sk_to_full_sk(sk);
5301 static struct sock *
5302 bpf_skc_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
5303 u8 proto, u64 netns_id, u64 flags)
5305 struct net *caller_net;
5309 caller_net = dev_net(skb->dev);
5310 ifindex = skb->dev->ifindex;
5312 caller_net = sock_net(skb->sk);
5316 return __bpf_skc_lookup(skb, tuple, len, caller_net, ifindex, proto,
5320 static struct sock *
5321 bpf_sk_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
5322 u8 proto, u64 netns_id, u64 flags)
5324 struct sock *sk = bpf_skc_lookup(skb, tuple, len, proto, netns_id,
5328 sk = sk_to_full_sk(sk);
5333 BPF_CALL_5(bpf_skc_lookup_tcp, struct sk_buff *, skb,
5334 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
5336 return (unsigned long)bpf_skc_lookup(skb, tuple, len, IPPROTO_TCP,
5340 static const struct bpf_func_proto bpf_skc_lookup_tcp_proto = {
5341 .func = bpf_skc_lookup_tcp,
5344 .ret_type = RET_PTR_TO_SOCK_COMMON_OR_NULL,
5345 .arg1_type = ARG_PTR_TO_CTX,
5346 .arg2_type = ARG_PTR_TO_MEM,
5347 .arg3_type = ARG_CONST_SIZE,
5348 .arg4_type = ARG_ANYTHING,
5349 .arg5_type = ARG_ANYTHING,
5352 BPF_CALL_5(bpf_sk_lookup_tcp, struct sk_buff *, skb,
5353 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
5355 return (unsigned long)bpf_sk_lookup(skb, tuple, len, IPPROTO_TCP,
5359 static const struct bpf_func_proto bpf_sk_lookup_tcp_proto = {
5360 .func = bpf_sk_lookup_tcp,
5363 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
5364 .arg1_type = ARG_PTR_TO_CTX,
5365 .arg2_type = ARG_PTR_TO_MEM,
5366 .arg3_type = ARG_CONST_SIZE,
5367 .arg4_type = ARG_ANYTHING,
5368 .arg5_type = ARG_ANYTHING,
5371 BPF_CALL_5(bpf_sk_lookup_udp, struct sk_buff *, skb,
5372 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
5374 return (unsigned long)bpf_sk_lookup(skb, tuple, len, IPPROTO_UDP,
5378 static const struct bpf_func_proto bpf_sk_lookup_udp_proto = {
5379 .func = bpf_sk_lookup_udp,
5382 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
5383 .arg1_type = ARG_PTR_TO_CTX,
5384 .arg2_type = ARG_PTR_TO_MEM,
5385 .arg3_type = ARG_CONST_SIZE,
5386 .arg4_type = ARG_ANYTHING,
5387 .arg5_type = ARG_ANYTHING,
5390 BPF_CALL_1(bpf_sk_release, struct sock *, sk)
5392 if (!sock_flag(sk, SOCK_RCU_FREE))
5397 static const struct bpf_func_proto bpf_sk_release_proto = {
5398 .func = bpf_sk_release,
5400 .ret_type = RET_INTEGER,
5401 .arg1_type = ARG_PTR_TO_SOCK_COMMON,
5404 BPF_CALL_5(bpf_xdp_sk_lookup_udp, struct xdp_buff *, ctx,
5405 struct bpf_sock_tuple *, tuple, u32, len, u32, netns_id, u64, flags)
5407 struct net *caller_net = dev_net(ctx->rxq->dev);
5408 int ifindex = ctx->rxq->dev->ifindex;
5410 return (unsigned long)__bpf_sk_lookup(NULL, tuple, len, caller_net,
5411 ifindex, IPPROTO_UDP, netns_id,
5415 static const struct bpf_func_proto bpf_xdp_sk_lookup_udp_proto = {
5416 .func = bpf_xdp_sk_lookup_udp,
5419 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
5420 .arg1_type = ARG_PTR_TO_CTX,
5421 .arg2_type = ARG_PTR_TO_MEM,
5422 .arg3_type = ARG_CONST_SIZE,
5423 .arg4_type = ARG_ANYTHING,
5424 .arg5_type = ARG_ANYTHING,
5427 BPF_CALL_5(bpf_xdp_skc_lookup_tcp, struct xdp_buff *, ctx,
5428 struct bpf_sock_tuple *, tuple, u32, len, u32, netns_id, u64, flags)
5430 struct net *caller_net = dev_net(ctx->rxq->dev);
5431 int ifindex = ctx->rxq->dev->ifindex;
5433 return (unsigned long)__bpf_skc_lookup(NULL, tuple, len, caller_net,
5434 ifindex, IPPROTO_TCP, netns_id,
5438 static const struct bpf_func_proto bpf_xdp_skc_lookup_tcp_proto = {
5439 .func = bpf_xdp_skc_lookup_tcp,
5442 .ret_type = RET_PTR_TO_SOCK_COMMON_OR_NULL,
5443 .arg1_type = ARG_PTR_TO_CTX,
5444 .arg2_type = ARG_PTR_TO_MEM,
5445 .arg3_type = ARG_CONST_SIZE,
5446 .arg4_type = ARG_ANYTHING,
5447 .arg5_type = ARG_ANYTHING,
5450 BPF_CALL_5(bpf_xdp_sk_lookup_tcp, struct xdp_buff *, ctx,
5451 struct bpf_sock_tuple *, tuple, u32, len, u32, netns_id, u64, flags)
5453 struct net *caller_net = dev_net(ctx->rxq->dev);
5454 int ifindex = ctx->rxq->dev->ifindex;
5456 return (unsigned long)__bpf_sk_lookup(NULL, tuple, len, caller_net,
5457 ifindex, IPPROTO_TCP, netns_id,
5461 static const struct bpf_func_proto bpf_xdp_sk_lookup_tcp_proto = {
5462 .func = bpf_xdp_sk_lookup_tcp,
5465 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
5466 .arg1_type = ARG_PTR_TO_CTX,
5467 .arg2_type = ARG_PTR_TO_MEM,
5468 .arg3_type = ARG_CONST_SIZE,
5469 .arg4_type = ARG_ANYTHING,
5470 .arg5_type = ARG_ANYTHING,
5473 BPF_CALL_5(bpf_sock_addr_skc_lookup_tcp, struct bpf_sock_addr_kern *, ctx,
5474 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
5476 return (unsigned long)__bpf_skc_lookup(NULL, tuple, len,
5477 sock_net(ctx->sk), 0,
5478 IPPROTO_TCP, netns_id, flags);
5481 static const struct bpf_func_proto bpf_sock_addr_skc_lookup_tcp_proto = {
5482 .func = bpf_sock_addr_skc_lookup_tcp,
5484 .ret_type = RET_PTR_TO_SOCK_COMMON_OR_NULL,
5485 .arg1_type = ARG_PTR_TO_CTX,
5486 .arg2_type = ARG_PTR_TO_MEM,
5487 .arg3_type = ARG_CONST_SIZE,
5488 .arg4_type = ARG_ANYTHING,
5489 .arg5_type = ARG_ANYTHING,
5492 BPF_CALL_5(bpf_sock_addr_sk_lookup_tcp, struct bpf_sock_addr_kern *, ctx,
5493 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
5495 return (unsigned long)__bpf_sk_lookup(NULL, tuple, len,
5496 sock_net(ctx->sk), 0, IPPROTO_TCP,
5500 static const struct bpf_func_proto bpf_sock_addr_sk_lookup_tcp_proto = {
5501 .func = bpf_sock_addr_sk_lookup_tcp,
5503 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
5504 .arg1_type = ARG_PTR_TO_CTX,
5505 .arg2_type = ARG_PTR_TO_MEM,
5506 .arg3_type = ARG_CONST_SIZE,
5507 .arg4_type = ARG_ANYTHING,
5508 .arg5_type = ARG_ANYTHING,
5511 BPF_CALL_5(bpf_sock_addr_sk_lookup_udp, struct bpf_sock_addr_kern *, ctx,
5512 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
5514 return (unsigned long)__bpf_sk_lookup(NULL, tuple, len,
5515 sock_net(ctx->sk), 0, IPPROTO_UDP,
5519 static const struct bpf_func_proto bpf_sock_addr_sk_lookup_udp_proto = {
5520 .func = bpf_sock_addr_sk_lookup_udp,
5522 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
5523 .arg1_type = ARG_PTR_TO_CTX,
5524 .arg2_type = ARG_PTR_TO_MEM,
5525 .arg3_type = ARG_CONST_SIZE,
5526 .arg4_type = ARG_ANYTHING,
5527 .arg5_type = ARG_ANYTHING,
5530 bool bpf_tcp_sock_is_valid_access(int off, int size, enum bpf_access_type type,
5531 struct bpf_insn_access_aux *info)
5533 if (off < 0 || off >= offsetofend(struct bpf_tcp_sock, bytes_acked))
5536 if (off % size != 0)
5540 case offsetof(struct bpf_tcp_sock, bytes_received):
5541 case offsetof(struct bpf_tcp_sock, bytes_acked):
5542 return size == sizeof(__u64);
5544 return size == sizeof(__u32);
5548 u32 bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type,
5549 const struct bpf_insn *si,
5550 struct bpf_insn *insn_buf,
5551 struct bpf_prog *prog, u32 *target_size)
5553 struct bpf_insn *insn = insn_buf;
5555 #define BPF_TCP_SOCK_GET_COMMON(FIELD) \
5557 BUILD_BUG_ON(FIELD_SIZEOF(struct tcp_sock, FIELD) > \
5558 FIELD_SIZEOF(struct bpf_tcp_sock, FIELD)); \
5559 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct tcp_sock, FIELD),\
5560 si->dst_reg, si->src_reg, \
5561 offsetof(struct tcp_sock, FIELD)); \
5564 CONVERT_COMMON_TCP_SOCK_FIELDS(struct bpf_tcp_sock,
5565 BPF_TCP_SOCK_GET_COMMON);
5567 if (insn > insn_buf)
5568 return insn - insn_buf;
5571 case offsetof(struct bpf_tcp_sock, rtt_min):
5572 BUILD_BUG_ON(FIELD_SIZEOF(struct tcp_sock, rtt_min) !=
5573 sizeof(struct minmax));
5574 BUILD_BUG_ON(sizeof(struct minmax) <
5575 sizeof(struct minmax_sample));
5577 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
5578 offsetof(struct tcp_sock, rtt_min) +
5579 offsetof(struct minmax_sample, v));
5583 return insn - insn_buf;
5586 BPF_CALL_1(bpf_tcp_sock, struct sock *, sk)
5588 if (sk_fullsock(sk) && sk->sk_protocol == IPPROTO_TCP)
5589 return (unsigned long)sk;
5591 return (unsigned long)NULL;
5594 static const struct bpf_func_proto bpf_tcp_sock_proto = {
5595 .func = bpf_tcp_sock,
5597 .ret_type = RET_PTR_TO_TCP_SOCK_OR_NULL,
5598 .arg1_type = ARG_PTR_TO_SOCK_COMMON,
5601 BPF_CALL_1(bpf_get_listener_sock, struct sock *, sk)
5603 sk = sk_to_full_sk(sk);
5605 if (sk->sk_state == TCP_LISTEN && sock_flag(sk, SOCK_RCU_FREE))
5606 return (unsigned long)sk;
5608 return (unsigned long)NULL;
5611 static const struct bpf_func_proto bpf_get_listener_sock_proto = {
5612 .func = bpf_get_listener_sock,
5614 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
5615 .arg1_type = ARG_PTR_TO_SOCK_COMMON,
5618 BPF_CALL_1(bpf_skb_ecn_set_ce, struct sk_buff *, skb)
5620 unsigned int iphdr_len;
5622 if (skb->protocol == cpu_to_be16(ETH_P_IP))
5623 iphdr_len = sizeof(struct iphdr);
5624 else if (skb->protocol == cpu_to_be16(ETH_P_IPV6))
5625 iphdr_len = sizeof(struct ipv6hdr);
5629 if (skb_headlen(skb) < iphdr_len)
5632 if (skb_cloned(skb) && !skb_clone_writable(skb, iphdr_len))
5635 return INET_ECN_set_ce(skb);
5638 static const struct bpf_func_proto bpf_skb_ecn_set_ce_proto = {
5639 .func = bpf_skb_ecn_set_ce,
5641 .ret_type = RET_INTEGER,
5642 .arg1_type = ARG_PTR_TO_CTX,
5645 BPF_CALL_5(bpf_tcp_check_syncookie, struct sock *, sk, void *, iph, u32, iph_len,
5646 struct tcphdr *, th, u32, th_len)
5648 #ifdef CONFIG_SYN_COOKIES
5652 if (unlikely(th_len < sizeof(*th)))
5655 /* sk_listener() allows TCP_NEW_SYN_RECV, which makes no sense here. */
5656 if (sk->sk_protocol != IPPROTO_TCP || sk->sk_state != TCP_LISTEN)
5659 if (!sock_net(sk)->ipv4.sysctl_tcp_syncookies)
5662 if (!th->ack || th->rst || th->syn)
5665 if (tcp_synq_no_recent_overflow(sk))
5668 cookie = ntohl(th->ack_seq) - 1;
5670 switch (sk->sk_family) {
5672 if (unlikely(iph_len < sizeof(struct iphdr)))
5675 ret = __cookie_v4_check((struct iphdr *)iph, th, cookie);
5678 #if IS_BUILTIN(CONFIG_IPV6)
5680 if (unlikely(iph_len < sizeof(struct ipv6hdr)))
5683 ret = __cookie_v6_check((struct ipv6hdr *)iph, th, cookie);
5685 #endif /* CONFIG_IPV6 */
5688 return -EPROTONOSUPPORT;
5700 static const struct bpf_func_proto bpf_tcp_check_syncookie_proto = {
5701 .func = bpf_tcp_check_syncookie,
5704 .ret_type = RET_INTEGER,
5705 .arg1_type = ARG_PTR_TO_SOCK_COMMON,
5706 .arg2_type = ARG_PTR_TO_MEM,
5707 .arg3_type = ARG_CONST_SIZE,
5708 .arg4_type = ARG_PTR_TO_MEM,
5709 .arg5_type = ARG_CONST_SIZE,
5712 #endif /* CONFIG_INET */
5714 bool bpf_helper_changes_pkt_data(void *func)
5716 if (func == bpf_skb_vlan_push ||
5717 func == bpf_skb_vlan_pop ||
5718 func == bpf_skb_store_bytes ||
5719 func == bpf_skb_change_proto ||
5720 func == bpf_skb_change_head ||
5721 func == sk_skb_change_head ||
5722 func == bpf_skb_change_tail ||
5723 func == sk_skb_change_tail ||
5724 func == bpf_skb_adjust_room ||
5725 func == bpf_skb_pull_data ||
5726 func == sk_skb_pull_data ||
5727 func == bpf_clone_redirect ||
5728 func == bpf_l3_csum_replace ||
5729 func == bpf_l4_csum_replace ||
5730 func == bpf_xdp_adjust_head ||
5731 func == bpf_xdp_adjust_meta ||
5732 func == bpf_msg_pull_data ||
5733 func == bpf_msg_push_data ||
5734 func == bpf_msg_pop_data ||
5735 func == bpf_xdp_adjust_tail ||
5736 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
5737 func == bpf_lwt_seg6_store_bytes ||
5738 func == bpf_lwt_seg6_adjust_srh ||
5739 func == bpf_lwt_seg6_action ||
5741 func == bpf_lwt_in_push_encap ||
5742 func == bpf_lwt_xmit_push_encap)
5748 static const struct bpf_func_proto *
5749 bpf_base_func_proto(enum bpf_func_id func_id)
5752 case BPF_FUNC_map_lookup_elem:
5753 return &bpf_map_lookup_elem_proto;
5754 case BPF_FUNC_map_update_elem:
5755 return &bpf_map_update_elem_proto;
5756 case BPF_FUNC_map_delete_elem:
5757 return &bpf_map_delete_elem_proto;
5758 case BPF_FUNC_map_push_elem:
5759 return &bpf_map_push_elem_proto;
5760 case BPF_FUNC_map_pop_elem:
5761 return &bpf_map_pop_elem_proto;
5762 case BPF_FUNC_map_peek_elem:
5763 return &bpf_map_peek_elem_proto;
5764 case BPF_FUNC_get_prandom_u32:
5765 return &bpf_get_prandom_u32_proto;
5766 case BPF_FUNC_get_smp_processor_id:
5767 return &bpf_get_raw_smp_processor_id_proto;
5768 case BPF_FUNC_get_numa_node_id:
5769 return &bpf_get_numa_node_id_proto;
5770 case BPF_FUNC_tail_call:
5771 return &bpf_tail_call_proto;
5772 case BPF_FUNC_ktime_get_ns:
5773 return &bpf_ktime_get_ns_proto;
5778 if (!capable(CAP_SYS_ADMIN))
5782 case BPF_FUNC_spin_lock:
5783 return &bpf_spin_lock_proto;
5784 case BPF_FUNC_spin_unlock:
5785 return &bpf_spin_unlock_proto;
5786 case BPF_FUNC_trace_printk:
5787 return bpf_get_trace_printk_proto();
5793 static const struct bpf_func_proto *
5794 sock_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5797 /* inet and inet6 sockets are created in a process
5798 * context so there is always a valid uid/gid
5800 case BPF_FUNC_get_current_uid_gid:
5801 return &bpf_get_current_uid_gid_proto;
5802 case BPF_FUNC_get_local_storage:
5803 return &bpf_get_local_storage_proto;
5805 return bpf_base_func_proto(func_id);
5809 static const struct bpf_func_proto *
5810 sock_addr_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5813 /* inet and inet6 sockets are created in a process
5814 * context so there is always a valid uid/gid
5816 case BPF_FUNC_get_current_uid_gid:
5817 return &bpf_get_current_uid_gid_proto;
5819 switch (prog->expected_attach_type) {
5820 case BPF_CGROUP_INET4_CONNECT:
5821 case BPF_CGROUP_INET6_CONNECT:
5822 return &bpf_bind_proto;
5826 case BPF_FUNC_get_socket_cookie:
5827 return &bpf_get_socket_cookie_sock_addr_proto;
5828 case BPF_FUNC_get_local_storage:
5829 return &bpf_get_local_storage_proto;
5831 case BPF_FUNC_sk_lookup_tcp:
5832 return &bpf_sock_addr_sk_lookup_tcp_proto;
5833 case BPF_FUNC_sk_lookup_udp:
5834 return &bpf_sock_addr_sk_lookup_udp_proto;
5835 case BPF_FUNC_sk_release:
5836 return &bpf_sk_release_proto;
5837 case BPF_FUNC_skc_lookup_tcp:
5838 return &bpf_sock_addr_skc_lookup_tcp_proto;
5839 #endif /* CONFIG_INET */
5841 return bpf_base_func_proto(func_id);
5845 static const struct bpf_func_proto *
5846 sk_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5849 case BPF_FUNC_skb_load_bytes:
5850 return &bpf_skb_load_bytes_proto;
5851 case BPF_FUNC_skb_load_bytes_relative:
5852 return &bpf_skb_load_bytes_relative_proto;
5853 case BPF_FUNC_get_socket_cookie:
5854 return &bpf_get_socket_cookie_proto;
5855 case BPF_FUNC_get_socket_uid:
5856 return &bpf_get_socket_uid_proto;
5858 return bpf_base_func_proto(func_id);
5862 static const struct bpf_func_proto *
5863 cg_skb_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5866 case BPF_FUNC_get_local_storage:
5867 return &bpf_get_local_storage_proto;
5868 case BPF_FUNC_sk_fullsock:
5869 return &bpf_sk_fullsock_proto;
5871 case BPF_FUNC_tcp_sock:
5872 return &bpf_tcp_sock_proto;
5873 case BPF_FUNC_get_listener_sock:
5874 return &bpf_get_listener_sock_proto;
5875 case BPF_FUNC_skb_ecn_set_ce:
5876 return &bpf_skb_ecn_set_ce_proto;
5879 return sk_filter_func_proto(func_id, prog);
5883 static const struct bpf_func_proto *
5884 tc_cls_act_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5887 case BPF_FUNC_skb_store_bytes:
5888 return &bpf_skb_store_bytes_proto;
5889 case BPF_FUNC_skb_load_bytes:
5890 return &bpf_skb_load_bytes_proto;
5891 case BPF_FUNC_skb_load_bytes_relative:
5892 return &bpf_skb_load_bytes_relative_proto;
5893 case BPF_FUNC_skb_pull_data:
5894 return &bpf_skb_pull_data_proto;
5895 case BPF_FUNC_csum_diff:
5896 return &bpf_csum_diff_proto;
5897 case BPF_FUNC_csum_update:
5898 return &bpf_csum_update_proto;
5899 case BPF_FUNC_l3_csum_replace:
5900 return &bpf_l3_csum_replace_proto;
5901 case BPF_FUNC_l4_csum_replace:
5902 return &bpf_l4_csum_replace_proto;
5903 case BPF_FUNC_clone_redirect:
5904 return &bpf_clone_redirect_proto;
5905 case BPF_FUNC_get_cgroup_classid:
5906 return &bpf_get_cgroup_classid_proto;
5907 case BPF_FUNC_skb_vlan_push:
5908 return &bpf_skb_vlan_push_proto;
5909 case BPF_FUNC_skb_vlan_pop:
5910 return &bpf_skb_vlan_pop_proto;
5911 case BPF_FUNC_skb_change_proto:
5912 return &bpf_skb_change_proto_proto;
5913 case BPF_FUNC_skb_change_type:
5914 return &bpf_skb_change_type_proto;
5915 case BPF_FUNC_skb_adjust_room:
5916 return &bpf_skb_adjust_room_proto;
5917 case BPF_FUNC_skb_change_tail:
5918 return &bpf_skb_change_tail_proto;
5919 case BPF_FUNC_skb_get_tunnel_key:
5920 return &bpf_skb_get_tunnel_key_proto;
5921 case BPF_FUNC_skb_set_tunnel_key:
5922 return bpf_get_skb_set_tunnel_proto(func_id);
5923 case BPF_FUNC_skb_get_tunnel_opt:
5924 return &bpf_skb_get_tunnel_opt_proto;
5925 case BPF_FUNC_skb_set_tunnel_opt:
5926 return bpf_get_skb_set_tunnel_proto(func_id);
5927 case BPF_FUNC_redirect:
5928 return &bpf_redirect_proto;
5929 case BPF_FUNC_get_route_realm:
5930 return &bpf_get_route_realm_proto;
5931 case BPF_FUNC_get_hash_recalc:
5932 return &bpf_get_hash_recalc_proto;
5933 case BPF_FUNC_set_hash_invalid:
5934 return &bpf_set_hash_invalid_proto;
5935 case BPF_FUNC_set_hash:
5936 return &bpf_set_hash_proto;
5937 case BPF_FUNC_perf_event_output:
5938 return &bpf_skb_event_output_proto;
5939 case BPF_FUNC_get_smp_processor_id:
5940 return &bpf_get_smp_processor_id_proto;
5941 case BPF_FUNC_skb_under_cgroup:
5942 return &bpf_skb_under_cgroup_proto;
5943 case BPF_FUNC_get_socket_cookie:
5944 return &bpf_get_socket_cookie_proto;
5945 case BPF_FUNC_get_socket_uid:
5946 return &bpf_get_socket_uid_proto;
5947 case BPF_FUNC_fib_lookup:
5948 return &bpf_skb_fib_lookup_proto;
5949 case BPF_FUNC_sk_fullsock:
5950 return &bpf_sk_fullsock_proto;
5952 case BPF_FUNC_skb_get_xfrm_state:
5953 return &bpf_skb_get_xfrm_state_proto;
5955 #ifdef CONFIG_SOCK_CGROUP_DATA
5956 case BPF_FUNC_skb_cgroup_id:
5957 return &bpf_skb_cgroup_id_proto;
5958 case BPF_FUNC_skb_ancestor_cgroup_id:
5959 return &bpf_skb_ancestor_cgroup_id_proto;
5962 case BPF_FUNC_sk_lookup_tcp:
5963 return &bpf_sk_lookup_tcp_proto;
5964 case BPF_FUNC_sk_lookup_udp:
5965 return &bpf_sk_lookup_udp_proto;
5966 case BPF_FUNC_sk_release:
5967 return &bpf_sk_release_proto;
5968 case BPF_FUNC_tcp_sock:
5969 return &bpf_tcp_sock_proto;
5970 case BPF_FUNC_get_listener_sock:
5971 return &bpf_get_listener_sock_proto;
5972 case BPF_FUNC_skc_lookup_tcp:
5973 return &bpf_skc_lookup_tcp_proto;
5974 case BPF_FUNC_tcp_check_syncookie:
5975 return &bpf_tcp_check_syncookie_proto;
5976 case BPF_FUNC_skb_ecn_set_ce:
5977 return &bpf_skb_ecn_set_ce_proto;
5980 return bpf_base_func_proto(func_id);
5984 static const struct bpf_func_proto *
5985 xdp_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5988 case BPF_FUNC_perf_event_output:
5989 return &bpf_xdp_event_output_proto;
5990 case BPF_FUNC_get_smp_processor_id:
5991 return &bpf_get_smp_processor_id_proto;
5992 case BPF_FUNC_csum_diff:
5993 return &bpf_csum_diff_proto;
5994 case BPF_FUNC_xdp_adjust_head:
5995 return &bpf_xdp_adjust_head_proto;
5996 case BPF_FUNC_xdp_adjust_meta:
5997 return &bpf_xdp_adjust_meta_proto;
5998 case BPF_FUNC_redirect:
5999 return &bpf_xdp_redirect_proto;
6000 case BPF_FUNC_redirect_map:
6001 return &bpf_xdp_redirect_map_proto;
6002 case BPF_FUNC_xdp_adjust_tail:
6003 return &bpf_xdp_adjust_tail_proto;
6004 case BPF_FUNC_fib_lookup:
6005 return &bpf_xdp_fib_lookup_proto;
6007 case BPF_FUNC_sk_lookup_udp:
6008 return &bpf_xdp_sk_lookup_udp_proto;
6009 case BPF_FUNC_sk_lookup_tcp:
6010 return &bpf_xdp_sk_lookup_tcp_proto;
6011 case BPF_FUNC_sk_release:
6012 return &bpf_sk_release_proto;
6013 case BPF_FUNC_skc_lookup_tcp:
6014 return &bpf_xdp_skc_lookup_tcp_proto;
6015 case BPF_FUNC_tcp_check_syncookie:
6016 return &bpf_tcp_check_syncookie_proto;
6019 return bpf_base_func_proto(func_id);
6023 const struct bpf_func_proto bpf_sock_map_update_proto __weak;
6024 const struct bpf_func_proto bpf_sock_hash_update_proto __weak;
6026 static const struct bpf_func_proto *
6027 sock_ops_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6030 case BPF_FUNC_setsockopt:
6031 return &bpf_setsockopt_proto;
6032 case BPF_FUNC_getsockopt:
6033 return &bpf_getsockopt_proto;
6034 case BPF_FUNC_sock_ops_cb_flags_set:
6035 return &bpf_sock_ops_cb_flags_set_proto;
6036 case BPF_FUNC_sock_map_update:
6037 return &bpf_sock_map_update_proto;
6038 case BPF_FUNC_sock_hash_update:
6039 return &bpf_sock_hash_update_proto;
6040 case BPF_FUNC_get_socket_cookie:
6041 return &bpf_get_socket_cookie_sock_ops_proto;
6042 case BPF_FUNC_get_local_storage:
6043 return &bpf_get_local_storage_proto;
6044 case BPF_FUNC_perf_event_output:
6045 return &bpf_sockopt_event_output_proto;
6047 return bpf_base_func_proto(func_id);
6051 const struct bpf_func_proto bpf_msg_redirect_map_proto __weak;
6052 const struct bpf_func_proto bpf_msg_redirect_hash_proto __weak;
6054 static const struct bpf_func_proto *
6055 sk_msg_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6058 case BPF_FUNC_msg_redirect_map:
6059 return &bpf_msg_redirect_map_proto;
6060 case BPF_FUNC_msg_redirect_hash:
6061 return &bpf_msg_redirect_hash_proto;
6062 case BPF_FUNC_msg_apply_bytes:
6063 return &bpf_msg_apply_bytes_proto;
6064 case BPF_FUNC_msg_cork_bytes:
6065 return &bpf_msg_cork_bytes_proto;
6066 case BPF_FUNC_msg_pull_data:
6067 return &bpf_msg_pull_data_proto;
6068 case BPF_FUNC_msg_push_data:
6069 return &bpf_msg_push_data_proto;
6070 case BPF_FUNC_msg_pop_data:
6071 return &bpf_msg_pop_data_proto;
6073 return bpf_base_func_proto(func_id);
6077 const struct bpf_func_proto bpf_sk_redirect_map_proto __weak;
6078 const struct bpf_func_proto bpf_sk_redirect_hash_proto __weak;
6080 static const struct bpf_func_proto *
6081 sk_skb_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6084 case BPF_FUNC_skb_store_bytes:
6085 return &bpf_skb_store_bytes_proto;
6086 case BPF_FUNC_skb_load_bytes:
6087 return &bpf_skb_load_bytes_proto;
6088 case BPF_FUNC_skb_pull_data:
6089 return &sk_skb_pull_data_proto;
6090 case BPF_FUNC_skb_change_tail:
6091 return &sk_skb_change_tail_proto;
6092 case BPF_FUNC_skb_change_head:
6093 return &sk_skb_change_head_proto;
6094 case BPF_FUNC_get_socket_cookie:
6095 return &bpf_get_socket_cookie_proto;
6096 case BPF_FUNC_get_socket_uid:
6097 return &bpf_get_socket_uid_proto;
6098 case BPF_FUNC_sk_redirect_map:
6099 return &bpf_sk_redirect_map_proto;
6100 case BPF_FUNC_sk_redirect_hash:
6101 return &bpf_sk_redirect_hash_proto;
6103 case BPF_FUNC_sk_lookup_tcp:
6104 return &bpf_sk_lookup_tcp_proto;
6105 case BPF_FUNC_sk_lookup_udp:
6106 return &bpf_sk_lookup_udp_proto;
6107 case BPF_FUNC_sk_release:
6108 return &bpf_sk_release_proto;
6109 case BPF_FUNC_skc_lookup_tcp:
6110 return &bpf_skc_lookup_tcp_proto;
6113 return bpf_base_func_proto(func_id);
6117 static const struct bpf_func_proto *
6118 flow_dissector_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6121 case BPF_FUNC_skb_load_bytes:
6122 return &bpf_skb_load_bytes_proto;
6124 return bpf_base_func_proto(func_id);
6128 static const struct bpf_func_proto *
6129 lwt_out_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6132 case BPF_FUNC_skb_load_bytes:
6133 return &bpf_skb_load_bytes_proto;
6134 case BPF_FUNC_skb_pull_data:
6135 return &bpf_skb_pull_data_proto;
6136 case BPF_FUNC_csum_diff:
6137 return &bpf_csum_diff_proto;
6138 case BPF_FUNC_get_cgroup_classid:
6139 return &bpf_get_cgroup_classid_proto;
6140 case BPF_FUNC_get_route_realm:
6141 return &bpf_get_route_realm_proto;
6142 case BPF_FUNC_get_hash_recalc:
6143 return &bpf_get_hash_recalc_proto;
6144 case BPF_FUNC_perf_event_output:
6145 return &bpf_skb_event_output_proto;
6146 case BPF_FUNC_get_smp_processor_id:
6147 return &bpf_get_smp_processor_id_proto;
6148 case BPF_FUNC_skb_under_cgroup:
6149 return &bpf_skb_under_cgroup_proto;
6151 return bpf_base_func_proto(func_id);
6155 static const struct bpf_func_proto *
6156 lwt_in_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6159 case BPF_FUNC_lwt_push_encap:
6160 return &bpf_lwt_in_push_encap_proto;
6162 return lwt_out_func_proto(func_id, prog);
6166 static const struct bpf_func_proto *
6167 lwt_xmit_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6170 case BPF_FUNC_skb_get_tunnel_key:
6171 return &bpf_skb_get_tunnel_key_proto;
6172 case BPF_FUNC_skb_set_tunnel_key:
6173 return bpf_get_skb_set_tunnel_proto(func_id);
6174 case BPF_FUNC_skb_get_tunnel_opt:
6175 return &bpf_skb_get_tunnel_opt_proto;
6176 case BPF_FUNC_skb_set_tunnel_opt:
6177 return bpf_get_skb_set_tunnel_proto(func_id);
6178 case BPF_FUNC_redirect:
6179 return &bpf_redirect_proto;
6180 case BPF_FUNC_clone_redirect:
6181 return &bpf_clone_redirect_proto;
6182 case BPF_FUNC_skb_change_tail:
6183 return &bpf_skb_change_tail_proto;
6184 case BPF_FUNC_skb_change_head:
6185 return &bpf_skb_change_head_proto;
6186 case BPF_FUNC_skb_store_bytes:
6187 return &bpf_skb_store_bytes_proto;
6188 case BPF_FUNC_csum_update:
6189 return &bpf_csum_update_proto;
6190 case BPF_FUNC_l3_csum_replace:
6191 return &bpf_l3_csum_replace_proto;
6192 case BPF_FUNC_l4_csum_replace:
6193 return &bpf_l4_csum_replace_proto;
6194 case BPF_FUNC_set_hash_invalid:
6195 return &bpf_set_hash_invalid_proto;
6196 case BPF_FUNC_lwt_push_encap:
6197 return &bpf_lwt_xmit_push_encap_proto;
6199 return lwt_out_func_proto(func_id, prog);
6203 static const struct bpf_func_proto *
6204 lwt_seg6local_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6207 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
6208 case BPF_FUNC_lwt_seg6_store_bytes:
6209 return &bpf_lwt_seg6_store_bytes_proto;
6210 case BPF_FUNC_lwt_seg6_action:
6211 return &bpf_lwt_seg6_action_proto;
6212 case BPF_FUNC_lwt_seg6_adjust_srh:
6213 return &bpf_lwt_seg6_adjust_srh_proto;
6216 return lwt_out_func_proto(func_id, prog);
6220 static bool bpf_skb_is_valid_access(int off, int size, enum bpf_access_type type,
6221 const struct bpf_prog *prog,
6222 struct bpf_insn_access_aux *info)
6224 const int size_default = sizeof(__u32);
6226 if (off < 0 || off >= sizeof(struct __sk_buff))
6229 /* The verifier guarantees that size > 0. */
6230 if (off % size != 0)
6234 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
6235 if (off + size > offsetofend(struct __sk_buff, cb[4]))
6238 case bpf_ctx_range_till(struct __sk_buff, remote_ip6[0], remote_ip6[3]):
6239 case bpf_ctx_range_till(struct __sk_buff, local_ip6[0], local_ip6[3]):
6240 case bpf_ctx_range_till(struct __sk_buff, remote_ip4, remote_ip4):
6241 case bpf_ctx_range_till(struct __sk_buff, local_ip4, local_ip4):
6242 case bpf_ctx_range(struct __sk_buff, data):
6243 case bpf_ctx_range(struct __sk_buff, data_meta):
6244 case bpf_ctx_range(struct __sk_buff, data_end):
6245 if (size != size_default)
6248 case bpf_ctx_range_ptr(struct __sk_buff, flow_keys):
6249 if (size != sizeof(__u64))
6252 case bpf_ctx_range(struct __sk_buff, tstamp):
6253 if (size != sizeof(__u64))
6256 case offsetof(struct __sk_buff, sk):
6257 if (type == BPF_WRITE || size != sizeof(__u64))
6259 info->reg_type = PTR_TO_SOCK_COMMON_OR_NULL;
6262 /* Only narrow read access allowed for now. */
6263 if (type == BPF_WRITE) {
6264 if (size != size_default)
6267 bpf_ctx_record_field_size(info, size_default);
6268 if (!bpf_ctx_narrow_access_ok(off, size, size_default))
6276 static bool sk_filter_is_valid_access(int off, int size,
6277 enum bpf_access_type type,
6278 const struct bpf_prog *prog,
6279 struct bpf_insn_access_aux *info)
6282 case bpf_ctx_range(struct __sk_buff, tc_classid):
6283 case bpf_ctx_range(struct __sk_buff, data):
6284 case bpf_ctx_range(struct __sk_buff, data_meta):
6285 case bpf_ctx_range(struct __sk_buff, data_end):
6286 case bpf_ctx_range_ptr(struct __sk_buff, flow_keys):
6287 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
6288 case bpf_ctx_range(struct __sk_buff, tstamp):
6289 case bpf_ctx_range(struct __sk_buff, wire_len):
6293 if (type == BPF_WRITE) {
6295 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
6302 return bpf_skb_is_valid_access(off, size, type, prog, info);
6305 static bool cg_skb_is_valid_access(int off, int size,
6306 enum bpf_access_type type,
6307 const struct bpf_prog *prog,
6308 struct bpf_insn_access_aux *info)
6311 case bpf_ctx_range(struct __sk_buff, tc_classid):
6312 case bpf_ctx_range(struct __sk_buff, data_meta):
6313 case bpf_ctx_range_ptr(struct __sk_buff, flow_keys):
6314 case bpf_ctx_range(struct __sk_buff, wire_len):
6316 case bpf_ctx_range(struct __sk_buff, data):
6317 case bpf_ctx_range(struct __sk_buff, data_end):
6318 if (!capable(CAP_SYS_ADMIN))
6323 if (type == BPF_WRITE) {
6325 case bpf_ctx_range(struct __sk_buff, mark):
6326 case bpf_ctx_range(struct __sk_buff, priority):
6327 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
6329 case bpf_ctx_range(struct __sk_buff, tstamp):
6330 if (!capable(CAP_SYS_ADMIN))
6339 case bpf_ctx_range(struct __sk_buff, data):
6340 info->reg_type = PTR_TO_PACKET;
6342 case bpf_ctx_range(struct __sk_buff, data_end):
6343 info->reg_type = PTR_TO_PACKET_END;
6347 return bpf_skb_is_valid_access(off, size, type, prog, info);
6350 static bool lwt_is_valid_access(int off, int size,
6351 enum bpf_access_type type,
6352 const struct bpf_prog *prog,
6353 struct bpf_insn_access_aux *info)
6356 case bpf_ctx_range(struct __sk_buff, tc_classid):
6357 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
6358 case bpf_ctx_range(struct __sk_buff, data_meta):
6359 case bpf_ctx_range_ptr(struct __sk_buff, flow_keys):
6360 case bpf_ctx_range(struct __sk_buff, tstamp):
6361 case bpf_ctx_range(struct __sk_buff, wire_len):
6365 if (type == BPF_WRITE) {
6367 case bpf_ctx_range(struct __sk_buff, mark):
6368 case bpf_ctx_range(struct __sk_buff, priority):
6369 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
6377 case bpf_ctx_range(struct __sk_buff, data):
6378 info->reg_type = PTR_TO_PACKET;
6380 case bpf_ctx_range(struct __sk_buff, data_end):
6381 info->reg_type = PTR_TO_PACKET_END;
6385 return bpf_skb_is_valid_access(off, size, type, prog, info);
6388 /* Attach type specific accesses */
6389 static bool __sock_filter_check_attach_type(int off,
6390 enum bpf_access_type access_type,
6391 enum bpf_attach_type attach_type)
6394 case offsetof(struct bpf_sock, bound_dev_if):
6395 case offsetof(struct bpf_sock, mark):
6396 case offsetof(struct bpf_sock, priority):
6397 switch (attach_type) {
6398 case BPF_CGROUP_INET_SOCK_CREATE:
6403 case bpf_ctx_range(struct bpf_sock, src_ip4):
6404 switch (attach_type) {
6405 case BPF_CGROUP_INET4_POST_BIND:
6410 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
6411 switch (attach_type) {
6412 case BPF_CGROUP_INET6_POST_BIND:
6417 case bpf_ctx_range(struct bpf_sock, src_port):
6418 switch (attach_type) {
6419 case BPF_CGROUP_INET4_POST_BIND:
6420 case BPF_CGROUP_INET6_POST_BIND:
6427 return access_type == BPF_READ;
6432 bool bpf_sock_common_is_valid_access(int off, int size,
6433 enum bpf_access_type type,
6434 struct bpf_insn_access_aux *info)
6437 case bpf_ctx_range_till(struct bpf_sock, type, priority):
6440 return bpf_sock_is_valid_access(off, size, type, info);
6444 bool bpf_sock_is_valid_access(int off, int size, enum bpf_access_type type,
6445 struct bpf_insn_access_aux *info)
6447 const int size_default = sizeof(__u32);
6449 if (off < 0 || off >= sizeof(struct bpf_sock))
6451 if (off % size != 0)
6455 case offsetof(struct bpf_sock, state):
6456 case offsetof(struct bpf_sock, family):
6457 case offsetof(struct bpf_sock, type):
6458 case offsetof(struct bpf_sock, protocol):
6459 case offsetof(struct bpf_sock, dst_port):
6460 case offsetof(struct bpf_sock, src_port):
6461 case bpf_ctx_range(struct bpf_sock, src_ip4):
6462 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
6463 case bpf_ctx_range(struct bpf_sock, dst_ip4):
6464 case bpf_ctx_range_till(struct bpf_sock, dst_ip6[0], dst_ip6[3]):
6465 bpf_ctx_record_field_size(info, size_default);
6466 return bpf_ctx_narrow_access_ok(off, size, size_default);
6469 return size == size_default;
6472 static bool sock_filter_is_valid_access(int off, int size,
6473 enum bpf_access_type type,
6474 const struct bpf_prog *prog,
6475 struct bpf_insn_access_aux *info)
6477 if (!bpf_sock_is_valid_access(off, size, type, info))
6479 return __sock_filter_check_attach_type(off, type,
6480 prog->expected_attach_type);
6483 static int bpf_noop_prologue(struct bpf_insn *insn_buf, bool direct_write,
6484 const struct bpf_prog *prog)
6486 /* Neither direct read nor direct write requires any preliminary
6492 static int bpf_unclone_prologue(struct bpf_insn *insn_buf, bool direct_write,
6493 const struct bpf_prog *prog, int drop_verdict)
6495 struct bpf_insn *insn = insn_buf;
6500 /* if (!skb->cloned)
6503 * (Fast-path, otherwise approximation that we might be
6504 * a clone, do the rest in helper.)
6506 *insn++ = BPF_LDX_MEM(BPF_B, BPF_REG_6, BPF_REG_1, CLONED_OFFSET());
6507 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_6, CLONED_MASK);
6508 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_6, 0, 7);
6510 /* ret = bpf_skb_pull_data(skb, 0); */
6511 *insn++ = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1);
6512 *insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_2, BPF_REG_2);
6513 *insn++ = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
6514 BPF_FUNC_skb_pull_data);
6517 * return TC_ACT_SHOT;
6519 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2);
6520 *insn++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_0, drop_verdict);
6521 *insn++ = BPF_EXIT_INSN();
6524 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_6);
6526 *insn++ = prog->insnsi[0];
6528 return insn - insn_buf;
6531 static int bpf_gen_ld_abs(const struct bpf_insn *orig,
6532 struct bpf_insn *insn_buf)
6534 bool indirect = BPF_MODE(orig->code) == BPF_IND;
6535 struct bpf_insn *insn = insn_buf;
6537 /* We're guaranteed here that CTX is in R6. */
6538 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_CTX);
6540 *insn++ = BPF_MOV64_IMM(BPF_REG_2, orig->imm);
6542 *insn++ = BPF_MOV64_REG(BPF_REG_2, orig->src_reg);
6544 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, orig->imm);
6547 switch (BPF_SIZE(orig->code)) {
6549 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_8_no_cache);
6552 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_16_no_cache);
6555 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_32_no_cache);
6559 *insn++ = BPF_JMP_IMM(BPF_JSGE, BPF_REG_0, 0, 2);
6560 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_0, BPF_REG_0);
6561 *insn++ = BPF_EXIT_INSN();
6563 return insn - insn_buf;
6566 static int tc_cls_act_prologue(struct bpf_insn *insn_buf, bool direct_write,
6567 const struct bpf_prog *prog)
6569 return bpf_unclone_prologue(insn_buf, direct_write, prog, TC_ACT_SHOT);
6572 static bool tc_cls_act_is_valid_access(int off, int size,
6573 enum bpf_access_type type,
6574 const struct bpf_prog *prog,
6575 struct bpf_insn_access_aux *info)
6577 if (type == BPF_WRITE) {
6579 case bpf_ctx_range(struct __sk_buff, mark):
6580 case bpf_ctx_range(struct __sk_buff, tc_index):
6581 case bpf_ctx_range(struct __sk_buff, priority):
6582 case bpf_ctx_range(struct __sk_buff, tc_classid):
6583 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
6584 case bpf_ctx_range(struct __sk_buff, tstamp):
6585 case bpf_ctx_range(struct __sk_buff, queue_mapping):
6593 case bpf_ctx_range(struct __sk_buff, data):
6594 info->reg_type = PTR_TO_PACKET;
6596 case bpf_ctx_range(struct __sk_buff, data_meta):
6597 info->reg_type = PTR_TO_PACKET_META;
6599 case bpf_ctx_range(struct __sk_buff, data_end):
6600 info->reg_type = PTR_TO_PACKET_END;
6602 case bpf_ctx_range_ptr(struct __sk_buff, flow_keys):
6603 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
6607 return bpf_skb_is_valid_access(off, size, type, prog, info);
6610 static bool __is_valid_xdp_access(int off, int size)
6612 if (off < 0 || off >= sizeof(struct xdp_md))
6614 if (off % size != 0)
6616 if (size != sizeof(__u32))
6622 static bool xdp_is_valid_access(int off, int size,
6623 enum bpf_access_type type,
6624 const struct bpf_prog *prog,
6625 struct bpf_insn_access_aux *info)
6627 if (type == BPF_WRITE) {
6628 if (bpf_prog_is_dev_bound(prog->aux)) {
6630 case offsetof(struct xdp_md, rx_queue_index):
6631 return __is_valid_xdp_access(off, size);
6638 case offsetof(struct xdp_md, data):
6639 info->reg_type = PTR_TO_PACKET;
6641 case offsetof(struct xdp_md, data_meta):
6642 info->reg_type = PTR_TO_PACKET_META;
6644 case offsetof(struct xdp_md, data_end):
6645 info->reg_type = PTR_TO_PACKET_END;
6649 return __is_valid_xdp_access(off, size);
6652 void bpf_warn_invalid_xdp_action(u32 act)
6654 const u32 act_max = XDP_REDIRECT;
6656 WARN_ONCE(1, "%s XDP return value %u, expect packet loss!\n",
6657 act > act_max ? "Illegal" : "Driver unsupported",
6660 EXPORT_SYMBOL_GPL(bpf_warn_invalid_xdp_action);
6662 static bool sock_addr_is_valid_access(int off, int size,
6663 enum bpf_access_type type,
6664 const struct bpf_prog *prog,
6665 struct bpf_insn_access_aux *info)
6667 const int size_default = sizeof(__u32);
6669 if (off < 0 || off >= sizeof(struct bpf_sock_addr))
6671 if (off % size != 0)
6674 /* Disallow access to IPv6 fields from IPv4 contex and vise
6678 case bpf_ctx_range(struct bpf_sock_addr, user_ip4):
6679 switch (prog->expected_attach_type) {
6680 case BPF_CGROUP_INET4_BIND:
6681 case BPF_CGROUP_INET4_CONNECT:
6682 case BPF_CGROUP_UDP4_SENDMSG:
6688 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
6689 switch (prog->expected_attach_type) {
6690 case BPF_CGROUP_INET6_BIND:
6691 case BPF_CGROUP_INET6_CONNECT:
6692 case BPF_CGROUP_UDP6_SENDMSG:
6698 case bpf_ctx_range(struct bpf_sock_addr, msg_src_ip4):
6699 switch (prog->expected_attach_type) {
6700 case BPF_CGROUP_UDP4_SENDMSG:
6706 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
6708 switch (prog->expected_attach_type) {
6709 case BPF_CGROUP_UDP6_SENDMSG:
6718 case bpf_ctx_range(struct bpf_sock_addr, user_ip4):
6719 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
6720 case bpf_ctx_range(struct bpf_sock_addr, msg_src_ip4):
6721 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
6723 /* Only narrow read access allowed for now. */
6724 if (type == BPF_READ) {
6725 bpf_ctx_record_field_size(info, size_default);
6726 if (!bpf_ctx_narrow_access_ok(off, size, size_default))
6729 if (size != size_default)
6733 case bpf_ctx_range(struct bpf_sock_addr, user_port):
6734 if (size != size_default)
6738 if (type == BPF_READ) {
6739 if (size != size_default)
6749 static bool sock_ops_is_valid_access(int off, int size,
6750 enum bpf_access_type type,
6751 const struct bpf_prog *prog,
6752 struct bpf_insn_access_aux *info)
6754 const int size_default = sizeof(__u32);
6756 if (off < 0 || off >= sizeof(struct bpf_sock_ops))
6759 /* The verifier guarantees that size > 0. */
6760 if (off % size != 0)
6763 if (type == BPF_WRITE) {
6765 case offsetof(struct bpf_sock_ops, reply):
6766 case offsetof(struct bpf_sock_ops, sk_txhash):
6767 if (size != size_default)
6775 case bpf_ctx_range_till(struct bpf_sock_ops, bytes_received,
6777 if (size != sizeof(__u64))
6781 if (size != size_default)
6790 static int sk_skb_prologue(struct bpf_insn *insn_buf, bool direct_write,
6791 const struct bpf_prog *prog)
6793 return bpf_unclone_prologue(insn_buf, direct_write, prog, SK_DROP);
6796 static bool sk_skb_is_valid_access(int off, int size,
6797 enum bpf_access_type type,
6798 const struct bpf_prog *prog,
6799 struct bpf_insn_access_aux *info)
6802 case bpf_ctx_range(struct __sk_buff, tc_classid):
6803 case bpf_ctx_range(struct __sk_buff, data_meta):
6804 case bpf_ctx_range_ptr(struct __sk_buff, flow_keys):
6805 case bpf_ctx_range(struct __sk_buff, tstamp):
6806 case bpf_ctx_range(struct __sk_buff, wire_len):
6810 if (type == BPF_WRITE) {
6812 case bpf_ctx_range(struct __sk_buff, tc_index):
6813 case bpf_ctx_range(struct __sk_buff, priority):
6821 case bpf_ctx_range(struct __sk_buff, mark):
6823 case bpf_ctx_range(struct __sk_buff, data):
6824 info->reg_type = PTR_TO_PACKET;
6826 case bpf_ctx_range(struct __sk_buff, data_end):
6827 info->reg_type = PTR_TO_PACKET_END;
6831 return bpf_skb_is_valid_access(off, size, type, prog, info);
6834 static bool sk_msg_is_valid_access(int off, int size,
6835 enum bpf_access_type type,
6836 const struct bpf_prog *prog,
6837 struct bpf_insn_access_aux *info)
6839 if (type == BPF_WRITE)
6842 if (off % size != 0)
6846 case offsetof(struct sk_msg_md, data):
6847 info->reg_type = PTR_TO_PACKET;
6848 if (size != sizeof(__u64))
6851 case offsetof(struct sk_msg_md, data_end):
6852 info->reg_type = PTR_TO_PACKET_END;
6853 if (size != sizeof(__u64))
6856 case bpf_ctx_range(struct sk_msg_md, family):
6857 case bpf_ctx_range(struct sk_msg_md, remote_ip4):
6858 case bpf_ctx_range(struct sk_msg_md, local_ip4):
6859 case bpf_ctx_range_till(struct sk_msg_md, remote_ip6[0], remote_ip6[3]):
6860 case bpf_ctx_range_till(struct sk_msg_md, local_ip6[0], local_ip6[3]):
6861 case bpf_ctx_range(struct sk_msg_md, remote_port):
6862 case bpf_ctx_range(struct sk_msg_md, local_port):
6863 case bpf_ctx_range(struct sk_msg_md, size):
6864 if (size != sizeof(__u32))
6873 static bool flow_dissector_is_valid_access(int off, int size,
6874 enum bpf_access_type type,
6875 const struct bpf_prog *prog,
6876 struct bpf_insn_access_aux *info)
6878 if (type == BPF_WRITE)
6882 case bpf_ctx_range(struct __sk_buff, data):
6883 info->reg_type = PTR_TO_PACKET;
6885 case bpf_ctx_range(struct __sk_buff, data_end):
6886 info->reg_type = PTR_TO_PACKET_END;
6888 case bpf_ctx_range_ptr(struct __sk_buff, flow_keys):
6889 info->reg_type = PTR_TO_FLOW_KEYS;
6895 return bpf_skb_is_valid_access(off, size, type, prog, info);
6898 static u32 bpf_convert_ctx_access(enum bpf_access_type type,
6899 const struct bpf_insn *si,
6900 struct bpf_insn *insn_buf,
6901 struct bpf_prog *prog, u32 *target_size)
6903 struct bpf_insn *insn = insn_buf;
6907 case offsetof(struct __sk_buff, len):
6908 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6909 bpf_target_off(struct sk_buff, len, 4,
6913 case offsetof(struct __sk_buff, protocol):
6914 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
6915 bpf_target_off(struct sk_buff, protocol, 2,
6919 case offsetof(struct __sk_buff, vlan_proto):
6920 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
6921 bpf_target_off(struct sk_buff, vlan_proto, 2,
6925 case offsetof(struct __sk_buff, priority):
6926 if (type == BPF_WRITE)
6927 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
6928 bpf_target_off(struct sk_buff, priority, 4,
6931 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6932 bpf_target_off(struct sk_buff, priority, 4,
6936 case offsetof(struct __sk_buff, ingress_ifindex):
6937 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6938 bpf_target_off(struct sk_buff, skb_iif, 4,
6942 case offsetof(struct __sk_buff, ifindex):
6943 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
6944 si->dst_reg, si->src_reg,
6945 offsetof(struct sk_buff, dev));
6946 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
6947 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6948 bpf_target_off(struct net_device, ifindex, 4,
6952 case offsetof(struct __sk_buff, hash):
6953 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6954 bpf_target_off(struct sk_buff, hash, 4,
6958 case offsetof(struct __sk_buff, mark):
6959 if (type == BPF_WRITE)
6960 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
6961 bpf_target_off(struct sk_buff, mark, 4,
6964 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6965 bpf_target_off(struct sk_buff, mark, 4,
6969 case offsetof(struct __sk_buff, pkt_type):
6971 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->src_reg,
6973 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, PKT_TYPE_MAX);
6974 #ifdef __BIG_ENDIAN_BITFIELD
6975 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, 5);
6979 case offsetof(struct __sk_buff, queue_mapping):
6980 if (type == BPF_WRITE) {
6981 *insn++ = BPF_JMP_IMM(BPF_JGE, si->src_reg, NO_QUEUE_MAPPING, 1);
6982 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg, si->src_reg,
6983 bpf_target_off(struct sk_buff,
6987 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
6988 bpf_target_off(struct sk_buff,
6994 case offsetof(struct __sk_buff, vlan_present):
6996 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->src_reg,
6997 PKT_VLAN_PRESENT_OFFSET());
6998 if (PKT_VLAN_PRESENT_BIT)
6999 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, PKT_VLAN_PRESENT_BIT);
7000 if (PKT_VLAN_PRESENT_BIT < 7)
7001 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, 1);
7004 case offsetof(struct __sk_buff, vlan_tci):
7005 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
7006 bpf_target_off(struct sk_buff, vlan_tci, 2,
7010 case offsetof(struct __sk_buff, cb[0]) ...
7011 offsetofend(struct __sk_buff, cb[4]) - 1:
7012 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, data) < 20);
7013 BUILD_BUG_ON((offsetof(struct sk_buff, cb) +
7014 offsetof(struct qdisc_skb_cb, data)) %
7017 prog->cb_access = 1;
7019 off -= offsetof(struct __sk_buff, cb[0]);
7020 off += offsetof(struct sk_buff, cb);
7021 off += offsetof(struct qdisc_skb_cb, data);
7022 if (type == BPF_WRITE)
7023 *insn++ = BPF_STX_MEM(BPF_SIZE(si->code), si->dst_reg,
7026 *insn++ = BPF_LDX_MEM(BPF_SIZE(si->code), si->dst_reg,
7030 case offsetof(struct __sk_buff, tc_classid):
7031 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, tc_classid) != 2);
7034 off -= offsetof(struct __sk_buff, tc_classid);
7035 off += offsetof(struct sk_buff, cb);
7036 off += offsetof(struct qdisc_skb_cb, tc_classid);
7038 if (type == BPF_WRITE)
7039 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg,
7042 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg,
7046 case offsetof(struct __sk_buff, data):
7047 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
7048 si->dst_reg, si->src_reg,
7049 offsetof(struct sk_buff, data));
7052 case offsetof(struct __sk_buff, data_meta):
7054 off -= offsetof(struct __sk_buff, data_meta);
7055 off += offsetof(struct sk_buff, cb);
7056 off += offsetof(struct bpf_skb_data_end, data_meta);
7057 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
7061 case offsetof(struct __sk_buff, data_end):
7063 off -= offsetof(struct __sk_buff, data_end);
7064 off += offsetof(struct sk_buff, cb);
7065 off += offsetof(struct bpf_skb_data_end, data_end);
7066 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
7070 case offsetof(struct __sk_buff, tc_index):
7071 #ifdef CONFIG_NET_SCHED
7072 if (type == BPF_WRITE)
7073 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg, si->src_reg,
7074 bpf_target_off(struct sk_buff, tc_index, 2,
7077 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
7078 bpf_target_off(struct sk_buff, tc_index, 2,
7082 if (type == BPF_WRITE)
7083 *insn++ = BPF_MOV64_REG(si->dst_reg, si->dst_reg);
7085 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
7089 case offsetof(struct __sk_buff, napi_id):
7090 #if defined(CONFIG_NET_RX_BUSY_POLL)
7091 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
7092 bpf_target_off(struct sk_buff, napi_id, 4,
7094 *insn++ = BPF_JMP_IMM(BPF_JGE, si->dst_reg, MIN_NAPI_ID, 1);
7095 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
7098 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
7101 case offsetof(struct __sk_buff, family):
7102 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_family) != 2);
7104 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
7105 si->dst_reg, si->src_reg,
7106 offsetof(struct sk_buff, sk));
7107 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
7108 bpf_target_off(struct sock_common,
7112 case offsetof(struct __sk_buff, remote_ip4):
7113 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_daddr) != 4);
7115 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
7116 si->dst_reg, si->src_reg,
7117 offsetof(struct sk_buff, sk));
7118 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7119 bpf_target_off(struct sock_common,
7123 case offsetof(struct __sk_buff, local_ip4):
7124 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
7125 skc_rcv_saddr) != 4);
7127 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
7128 si->dst_reg, si->src_reg,
7129 offsetof(struct sk_buff, sk));
7130 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7131 bpf_target_off(struct sock_common,
7135 case offsetof(struct __sk_buff, remote_ip6[0]) ...
7136 offsetof(struct __sk_buff, remote_ip6[3]):
7137 #if IS_ENABLED(CONFIG_IPV6)
7138 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
7139 skc_v6_daddr.s6_addr32[0]) != 4);
7142 off -= offsetof(struct __sk_buff, remote_ip6[0]);
7144 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
7145 si->dst_reg, si->src_reg,
7146 offsetof(struct sk_buff, sk));
7147 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7148 offsetof(struct sock_common,
7149 skc_v6_daddr.s6_addr32[0]) +
7152 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
7155 case offsetof(struct __sk_buff, local_ip6[0]) ...
7156 offsetof(struct __sk_buff, local_ip6[3]):
7157 #if IS_ENABLED(CONFIG_IPV6)
7158 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
7159 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
7162 off -= offsetof(struct __sk_buff, local_ip6[0]);
7164 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
7165 si->dst_reg, si->src_reg,
7166 offsetof(struct sk_buff, sk));
7167 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7168 offsetof(struct sock_common,
7169 skc_v6_rcv_saddr.s6_addr32[0]) +
7172 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
7176 case offsetof(struct __sk_buff, remote_port):
7177 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_dport) != 2);
7179 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
7180 si->dst_reg, si->src_reg,
7181 offsetof(struct sk_buff, sk));
7182 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
7183 bpf_target_off(struct sock_common,
7186 #ifndef __BIG_ENDIAN_BITFIELD
7187 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
7191 case offsetof(struct __sk_buff, local_port):
7192 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_num) != 2);
7194 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
7195 si->dst_reg, si->src_reg,
7196 offsetof(struct sk_buff, sk));
7197 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
7198 bpf_target_off(struct sock_common,
7199 skc_num, 2, target_size));
7202 case offsetof(struct __sk_buff, flow_keys):
7204 off -= offsetof(struct __sk_buff, flow_keys);
7205 off += offsetof(struct sk_buff, cb);
7206 off += offsetof(struct qdisc_skb_cb, flow_keys);
7207 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
7211 case offsetof(struct __sk_buff, tstamp):
7212 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, tstamp) != 8);
7214 if (type == BPF_WRITE)
7215 *insn++ = BPF_STX_MEM(BPF_DW,
7216 si->dst_reg, si->src_reg,
7217 bpf_target_off(struct sk_buff,
7221 *insn++ = BPF_LDX_MEM(BPF_DW,
7222 si->dst_reg, si->src_reg,
7223 bpf_target_off(struct sk_buff,
7228 case offsetof(struct __sk_buff, gso_segs):
7229 /* si->dst_reg = skb_shinfo(SKB); */
7230 #ifdef NET_SKBUFF_DATA_USES_OFFSET
7231 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, head),
7232 si->dst_reg, si->src_reg,
7233 offsetof(struct sk_buff, head));
7234 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, end),
7235 BPF_REG_AX, si->src_reg,
7236 offsetof(struct sk_buff, end));
7237 *insn++ = BPF_ALU64_REG(BPF_ADD, si->dst_reg, BPF_REG_AX);
7239 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, end),
7240 si->dst_reg, si->src_reg,
7241 offsetof(struct sk_buff, end));
7243 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct skb_shared_info, gso_segs),
7244 si->dst_reg, si->dst_reg,
7245 bpf_target_off(struct skb_shared_info,
7249 case offsetof(struct __sk_buff, wire_len):
7250 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, pkt_len) != 4);
7253 off -= offsetof(struct __sk_buff, wire_len);
7254 off += offsetof(struct sk_buff, cb);
7255 off += offsetof(struct qdisc_skb_cb, pkt_len);
7257 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, off);
7260 case offsetof(struct __sk_buff, sk):
7261 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
7262 si->dst_reg, si->src_reg,
7263 offsetof(struct sk_buff, sk));
7267 return insn - insn_buf;
7270 u32 bpf_sock_convert_ctx_access(enum bpf_access_type type,
7271 const struct bpf_insn *si,
7272 struct bpf_insn *insn_buf,
7273 struct bpf_prog *prog, u32 *target_size)
7275 struct bpf_insn *insn = insn_buf;
7279 case offsetof(struct bpf_sock, bound_dev_if):
7280 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_bound_dev_if) != 4);
7282 if (type == BPF_WRITE)
7283 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
7284 offsetof(struct sock, sk_bound_dev_if));
7286 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
7287 offsetof(struct sock, sk_bound_dev_if));
7290 case offsetof(struct bpf_sock, mark):
7291 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_mark) != 4);
7293 if (type == BPF_WRITE)
7294 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
7295 offsetof(struct sock, sk_mark));
7297 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
7298 offsetof(struct sock, sk_mark));
7301 case offsetof(struct bpf_sock, priority):
7302 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_priority) != 4);
7304 if (type == BPF_WRITE)
7305 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
7306 offsetof(struct sock, sk_priority));
7308 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
7309 offsetof(struct sock, sk_priority));
7312 case offsetof(struct bpf_sock, family):
7313 *insn++ = BPF_LDX_MEM(
7314 BPF_FIELD_SIZEOF(struct sock_common, skc_family),
7315 si->dst_reg, si->src_reg,
7316 bpf_target_off(struct sock_common,
7318 FIELD_SIZEOF(struct sock_common,
7323 case offsetof(struct bpf_sock, type):
7324 BUILD_BUG_ON(HWEIGHT32(SK_FL_TYPE_MASK) != BITS_PER_BYTE * 2);
7325 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
7326 offsetof(struct sock, __sk_flags_offset));
7327 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_TYPE_MASK);
7328 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, SK_FL_TYPE_SHIFT);
7332 case offsetof(struct bpf_sock, protocol):
7333 BUILD_BUG_ON(HWEIGHT32(SK_FL_PROTO_MASK) != BITS_PER_BYTE);
7334 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
7335 offsetof(struct sock, __sk_flags_offset));
7336 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_PROTO_MASK);
7337 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, SK_FL_PROTO_SHIFT);
7341 case offsetof(struct bpf_sock, src_ip4):
7342 *insn++ = BPF_LDX_MEM(
7343 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
7344 bpf_target_off(struct sock_common, skc_rcv_saddr,
7345 FIELD_SIZEOF(struct sock_common,
7350 case offsetof(struct bpf_sock, dst_ip4):
7351 *insn++ = BPF_LDX_MEM(
7352 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
7353 bpf_target_off(struct sock_common, skc_daddr,
7354 FIELD_SIZEOF(struct sock_common,
7359 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
7360 #if IS_ENABLED(CONFIG_IPV6)
7362 off -= offsetof(struct bpf_sock, src_ip6[0]);
7363 *insn++ = BPF_LDX_MEM(
7364 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
7367 skc_v6_rcv_saddr.s6_addr32[0],
7368 FIELD_SIZEOF(struct sock_common,
7369 skc_v6_rcv_saddr.s6_addr32[0]),
7370 target_size) + off);
7373 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
7377 case bpf_ctx_range_till(struct bpf_sock, dst_ip6[0], dst_ip6[3]):
7378 #if IS_ENABLED(CONFIG_IPV6)
7380 off -= offsetof(struct bpf_sock, dst_ip6[0]);
7381 *insn++ = BPF_LDX_MEM(
7382 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
7383 bpf_target_off(struct sock_common,
7384 skc_v6_daddr.s6_addr32[0],
7385 FIELD_SIZEOF(struct sock_common,
7386 skc_v6_daddr.s6_addr32[0]),
7387 target_size) + off);
7389 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
7394 case offsetof(struct bpf_sock, src_port):
7395 *insn++ = BPF_LDX_MEM(
7396 BPF_FIELD_SIZEOF(struct sock_common, skc_num),
7397 si->dst_reg, si->src_reg,
7398 bpf_target_off(struct sock_common, skc_num,
7399 FIELD_SIZEOF(struct sock_common,
7404 case offsetof(struct bpf_sock, dst_port):
7405 *insn++ = BPF_LDX_MEM(
7406 BPF_FIELD_SIZEOF(struct sock_common, skc_dport),
7407 si->dst_reg, si->src_reg,
7408 bpf_target_off(struct sock_common, skc_dport,
7409 FIELD_SIZEOF(struct sock_common,
7414 case offsetof(struct bpf_sock, state):
7415 *insn++ = BPF_LDX_MEM(
7416 BPF_FIELD_SIZEOF(struct sock_common, skc_state),
7417 si->dst_reg, si->src_reg,
7418 bpf_target_off(struct sock_common, skc_state,
7419 FIELD_SIZEOF(struct sock_common,
7425 return insn - insn_buf;
7428 static u32 tc_cls_act_convert_ctx_access(enum bpf_access_type type,
7429 const struct bpf_insn *si,
7430 struct bpf_insn *insn_buf,
7431 struct bpf_prog *prog, u32 *target_size)
7433 struct bpf_insn *insn = insn_buf;
7436 case offsetof(struct __sk_buff, ifindex):
7437 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
7438 si->dst_reg, si->src_reg,
7439 offsetof(struct sk_buff, dev));
7440 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7441 bpf_target_off(struct net_device, ifindex, 4,
7445 return bpf_convert_ctx_access(type, si, insn_buf, prog,
7449 return insn - insn_buf;
7452 static u32 xdp_convert_ctx_access(enum bpf_access_type type,
7453 const struct bpf_insn *si,
7454 struct bpf_insn *insn_buf,
7455 struct bpf_prog *prog, u32 *target_size)
7457 struct bpf_insn *insn = insn_buf;
7460 case offsetof(struct xdp_md, data):
7461 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data),
7462 si->dst_reg, si->src_reg,
7463 offsetof(struct xdp_buff, data));
7465 case offsetof(struct xdp_md, data_meta):
7466 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_meta),
7467 si->dst_reg, si->src_reg,
7468 offsetof(struct xdp_buff, data_meta));
7470 case offsetof(struct xdp_md, data_end):
7471 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_end),
7472 si->dst_reg, si->src_reg,
7473 offsetof(struct xdp_buff, data_end));
7475 case offsetof(struct xdp_md, ingress_ifindex):
7476 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, rxq),
7477 si->dst_reg, si->src_reg,
7478 offsetof(struct xdp_buff, rxq));
7479 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_rxq_info, dev),
7480 si->dst_reg, si->dst_reg,
7481 offsetof(struct xdp_rxq_info, dev));
7482 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7483 offsetof(struct net_device, ifindex));
7485 case offsetof(struct xdp_md, rx_queue_index):
7486 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, rxq),
7487 si->dst_reg, si->src_reg,
7488 offsetof(struct xdp_buff, rxq));
7489 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7490 offsetof(struct xdp_rxq_info,
7495 return insn - insn_buf;
7498 /* SOCK_ADDR_LOAD_NESTED_FIELD() loads Nested Field S.F.NF where S is type of
7499 * context Structure, F is Field in context structure that contains a pointer
7500 * to Nested Structure of type NS that has the field NF.
7502 * SIZE encodes the load size (BPF_B, BPF_H, etc). It's up to caller to make
7503 * sure that SIZE is not greater than actual size of S.F.NF.
7505 * If offset OFF is provided, the load happens from that offset relative to
7508 #define SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, SIZE, OFF) \
7510 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), si->dst_reg, \
7511 si->src_reg, offsetof(S, F)); \
7512 *insn++ = BPF_LDX_MEM( \
7513 SIZE, si->dst_reg, si->dst_reg, \
7514 bpf_target_off(NS, NF, FIELD_SIZEOF(NS, NF), \
7519 #define SOCK_ADDR_LOAD_NESTED_FIELD(S, NS, F, NF) \
7520 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, \
7521 BPF_FIELD_SIZEOF(NS, NF), 0)
7523 /* SOCK_ADDR_STORE_NESTED_FIELD_OFF() has semantic similar to
7524 * SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF() but for store operation.
7526 * It doesn't support SIZE argument though since narrow stores are not
7527 * supported for now.
7529 * In addition it uses Temporary Field TF (member of struct S) as the 3rd
7530 * "register" since two registers available in convert_ctx_access are not
7531 * enough: we can't override neither SRC, since it contains value to store, nor
7532 * DST since it contains pointer to context that may be used by later
7533 * instructions. But we need a temporary place to save pointer to nested
7534 * structure whose field we want to store to.
7536 #define SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, OFF, TF) \
7538 int tmp_reg = BPF_REG_9; \
7539 if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg) \
7541 if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg) \
7543 *insn++ = BPF_STX_MEM(BPF_DW, si->dst_reg, tmp_reg, \
7545 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), tmp_reg, \
7546 si->dst_reg, offsetof(S, F)); \
7547 *insn++ = BPF_STX_MEM( \
7548 BPF_FIELD_SIZEOF(NS, NF), tmp_reg, si->src_reg, \
7549 bpf_target_off(NS, NF, FIELD_SIZEOF(NS, NF), \
7552 *insn++ = BPF_LDX_MEM(BPF_DW, tmp_reg, si->dst_reg, \
7556 #define SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, SIZE, OFF, \
7559 if (type == BPF_WRITE) { \
7560 SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, OFF, \
7563 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF( \
7564 S, NS, F, NF, SIZE, OFF); \
7568 #define SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD(S, NS, F, NF, TF) \
7569 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF( \
7570 S, NS, F, NF, BPF_FIELD_SIZEOF(NS, NF), 0, TF)
7572 static u32 sock_addr_convert_ctx_access(enum bpf_access_type type,
7573 const struct bpf_insn *si,
7574 struct bpf_insn *insn_buf,
7575 struct bpf_prog *prog, u32 *target_size)
7577 struct bpf_insn *insn = insn_buf;
7581 case offsetof(struct bpf_sock_addr, user_family):
7582 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
7583 struct sockaddr, uaddr, sa_family);
7586 case offsetof(struct bpf_sock_addr, user_ip4):
7587 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
7588 struct bpf_sock_addr_kern, struct sockaddr_in, uaddr,
7589 sin_addr, BPF_SIZE(si->code), 0, tmp_reg);
7592 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
7594 off -= offsetof(struct bpf_sock_addr, user_ip6[0]);
7595 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
7596 struct bpf_sock_addr_kern, struct sockaddr_in6, uaddr,
7597 sin6_addr.s6_addr32[0], BPF_SIZE(si->code), off,
7601 case offsetof(struct bpf_sock_addr, user_port):
7602 /* To get port we need to know sa_family first and then treat
7603 * sockaddr as either sockaddr_in or sockaddr_in6.
7604 * Though we can simplify since port field has same offset and
7605 * size in both structures.
7606 * Here we check this invariant and use just one of the
7607 * structures if it's true.
7609 BUILD_BUG_ON(offsetof(struct sockaddr_in, sin_port) !=
7610 offsetof(struct sockaddr_in6, sin6_port));
7611 BUILD_BUG_ON(FIELD_SIZEOF(struct sockaddr_in, sin_port) !=
7612 FIELD_SIZEOF(struct sockaddr_in6, sin6_port));
7613 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD(struct bpf_sock_addr_kern,
7614 struct sockaddr_in6, uaddr,
7615 sin6_port, tmp_reg);
7618 case offsetof(struct bpf_sock_addr, family):
7619 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
7620 struct sock, sk, sk_family);
7623 case offsetof(struct bpf_sock_addr, type):
7624 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(
7625 struct bpf_sock_addr_kern, struct sock, sk,
7626 __sk_flags_offset, BPF_W, 0);
7627 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_TYPE_MASK);
7628 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, SK_FL_TYPE_SHIFT);
7631 case offsetof(struct bpf_sock_addr, protocol):
7632 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(
7633 struct bpf_sock_addr_kern, struct sock, sk,
7634 __sk_flags_offset, BPF_W, 0);
7635 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_PROTO_MASK);
7636 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg,
7640 case offsetof(struct bpf_sock_addr, msg_src_ip4):
7641 /* Treat t_ctx as struct in_addr for msg_src_ip4. */
7642 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
7643 struct bpf_sock_addr_kern, struct in_addr, t_ctx,
7644 s_addr, BPF_SIZE(si->code), 0, tmp_reg);
7647 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
7650 off -= offsetof(struct bpf_sock_addr, msg_src_ip6[0]);
7651 /* Treat t_ctx as struct in6_addr for msg_src_ip6. */
7652 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
7653 struct bpf_sock_addr_kern, struct in6_addr, t_ctx,
7654 s6_addr32[0], BPF_SIZE(si->code), off, tmp_reg);
7658 return insn - insn_buf;
7661 static u32 sock_ops_convert_ctx_access(enum bpf_access_type type,
7662 const struct bpf_insn *si,
7663 struct bpf_insn *insn_buf,
7664 struct bpf_prog *prog,
7667 struct bpf_insn *insn = insn_buf;
7670 /* Helper macro for adding read access to tcp_sock or sock fields. */
7671 #define SOCK_OPS_GET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ) \
7673 BUILD_BUG_ON(FIELD_SIZEOF(OBJ, OBJ_FIELD) > \
7674 FIELD_SIZEOF(struct bpf_sock_ops, BPF_FIELD)); \
7675 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
7676 struct bpf_sock_ops_kern, \
7678 si->dst_reg, si->src_reg, \
7679 offsetof(struct bpf_sock_ops_kern, \
7681 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 2); \
7682 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
7683 struct bpf_sock_ops_kern, sk),\
7684 si->dst_reg, si->src_reg, \
7685 offsetof(struct bpf_sock_ops_kern, sk));\
7686 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(OBJ, \
7688 si->dst_reg, si->dst_reg, \
7689 offsetof(OBJ, OBJ_FIELD)); \
7692 #define SOCK_OPS_GET_TCP_SOCK_FIELD(FIELD) \
7693 SOCK_OPS_GET_FIELD(FIELD, FIELD, struct tcp_sock)
7695 /* Helper macro for adding write access to tcp_sock or sock fields.
7696 * The macro is called with two registers, dst_reg which contains a pointer
7697 * to ctx (context) and src_reg which contains the value that should be
7698 * stored. However, we need an additional register since we cannot overwrite
7699 * dst_reg because it may be used later in the program.
7700 * Instead we "borrow" one of the other register. We first save its value
7701 * into a new (temp) field in bpf_sock_ops_kern, use it, and then restore
7702 * it at the end of the macro.
7704 #define SOCK_OPS_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ) \
7706 int reg = BPF_REG_9; \
7707 BUILD_BUG_ON(FIELD_SIZEOF(OBJ, OBJ_FIELD) > \
7708 FIELD_SIZEOF(struct bpf_sock_ops, BPF_FIELD)); \
7709 if (si->dst_reg == reg || si->src_reg == reg) \
7711 if (si->dst_reg == reg || si->src_reg == reg) \
7713 *insn++ = BPF_STX_MEM(BPF_DW, si->dst_reg, reg, \
7714 offsetof(struct bpf_sock_ops_kern, \
7716 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
7717 struct bpf_sock_ops_kern, \
7720 offsetof(struct bpf_sock_ops_kern, \
7722 *insn++ = BPF_JMP_IMM(BPF_JEQ, reg, 0, 2); \
7723 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
7724 struct bpf_sock_ops_kern, sk),\
7726 offsetof(struct bpf_sock_ops_kern, sk));\
7727 *insn++ = BPF_STX_MEM(BPF_FIELD_SIZEOF(OBJ, OBJ_FIELD), \
7729 offsetof(OBJ, OBJ_FIELD)); \
7730 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->dst_reg, \
7731 offsetof(struct bpf_sock_ops_kern, \
7735 #define SOCK_OPS_GET_OR_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ, TYPE) \
7737 if (TYPE == BPF_WRITE) \
7738 SOCK_OPS_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ); \
7740 SOCK_OPS_GET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ); \
7743 CONVERT_COMMON_TCP_SOCK_FIELDS(struct bpf_sock_ops,
7744 SOCK_OPS_GET_TCP_SOCK_FIELD);
7746 if (insn > insn_buf)
7747 return insn - insn_buf;
7750 case offsetof(struct bpf_sock_ops, op) ...
7751 offsetof(struct bpf_sock_ops, replylong[3]):
7752 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, op) !=
7753 FIELD_SIZEOF(struct bpf_sock_ops_kern, op));
7754 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, reply) !=
7755 FIELD_SIZEOF(struct bpf_sock_ops_kern, reply));
7756 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, replylong) !=
7757 FIELD_SIZEOF(struct bpf_sock_ops_kern, replylong));
7759 off -= offsetof(struct bpf_sock_ops, op);
7760 off += offsetof(struct bpf_sock_ops_kern, op);
7761 if (type == BPF_WRITE)
7762 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
7765 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
7769 case offsetof(struct bpf_sock_ops, family):
7770 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_family) != 2);
7772 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7773 struct bpf_sock_ops_kern, sk),
7774 si->dst_reg, si->src_reg,
7775 offsetof(struct bpf_sock_ops_kern, sk));
7776 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
7777 offsetof(struct sock_common, skc_family));
7780 case offsetof(struct bpf_sock_ops, remote_ip4):
7781 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_daddr) != 4);
7783 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7784 struct bpf_sock_ops_kern, sk),
7785 si->dst_reg, si->src_reg,
7786 offsetof(struct bpf_sock_ops_kern, sk));
7787 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7788 offsetof(struct sock_common, skc_daddr));
7791 case offsetof(struct bpf_sock_ops, local_ip4):
7792 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
7793 skc_rcv_saddr) != 4);
7795 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7796 struct bpf_sock_ops_kern, sk),
7797 si->dst_reg, si->src_reg,
7798 offsetof(struct bpf_sock_ops_kern, sk));
7799 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7800 offsetof(struct sock_common,
7804 case offsetof(struct bpf_sock_ops, remote_ip6[0]) ...
7805 offsetof(struct bpf_sock_ops, remote_ip6[3]):
7806 #if IS_ENABLED(CONFIG_IPV6)
7807 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
7808 skc_v6_daddr.s6_addr32[0]) != 4);
7811 off -= offsetof(struct bpf_sock_ops, remote_ip6[0]);
7812 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7813 struct bpf_sock_ops_kern, sk),
7814 si->dst_reg, si->src_reg,
7815 offsetof(struct bpf_sock_ops_kern, sk));
7816 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7817 offsetof(struct sock_common,
7818 skc_v6_daddr.s6_addr32[0]) +
7821 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
7825 case offsetof(struct bpf_sock_ops, local_ip6[0]) ...
7826 offsetof(struct bpf_sock_ops, local_ip6[3]):
7827 #if IS_ENABLED(CONFIG_IPV6)
7828 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
7829 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
7832 off -= offsetof(struct bpf_sock_ops, local_ip6[0]);
7833 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7834 struct bpf_sock_ops_kern, sk),
7835 si->dst_reg, si->src_reg,
7836 offsetof(struct bpf_sock_ops_kern, sk));
7837 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7838 offsetof(struct sock_common,
7839 skc_v6_rcv_saddr.s6_addr32[0]) +
7842 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
7846 case offsetof(struct bpf_sock_ops, remote_port):
7847 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_dport) != 2);
7849 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7850 struct bpf_sock_ops_kern, sk),
7851 si->dst_reg, si->src_reg,
7852 offsetof(struct bpf_sock_ops_kern, sk));
7853 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
7854 offsetof(struct sock_common, skc_dport));
7855 #ifndef __BIG_ENDIAN_BITFIELD
7856 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
7860 case offsetof(struct bpf_sock_ops, local_port):
7861 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_num) != 2);
7863 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7864 struct bpf_sock_ops_kern, sk),
7865 si->dst_reg, si->src_reg,
7866 offsetof(struct bpf_sock_ops_kern, sk));
7867 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
7868 offsetof(struct sock_common, skc_num));
7871 case offsetof(struct bpf_sock_ops, is_fullsock):
7872 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7873 struct bpf_sock_ops_kern,
7875 si->dst_reg, si->src_reg,
7876 offsetof(struct bpf_sock_ops_kern,
7880 case offsetof(struct bpf_sock_ops, state):
7881 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_state) != 1);
7883 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7884 struct bpf_sock_ops_kern, sk),
7885 si->dst_reg, si->src_reg,
7886 offsetof(struct bpf_sock_ops_kern, sk));
7887 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->dst_reg,
7888 offsetof(struct sock_common, skc_state));
7891 case offsetof(struct bpf_sock_ops, rtt_min):
7892 BUILD_BUG_ON(FIELD_SIZEOF(struct tcp_sock, rtt_min) !=
7893 sizeof(struct minmax));
7894 BUILD_BUG_ON(sizeof(struct minmax) <
7895 sizeof(struct minmax_sample));
7897 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7898 struct bpf_sock_ops_kern, sk),
7899 si->dst_reg, si->src_reg,
7900 offsetof(struct bpf_sock_ops_kern, sk));
7901 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7902 offsetof(struct tcp_sock, rtt_min) +
7903 FIELD_SIZEOF(struct minmax_sample, t));
7906 case offsetof(struct bpf_sock_ops, bpf_sock_ops_cb_flags):
7907 SOCK_OPS_GET_FIELD(bpf_sock_ops_cb_flags, bpf_sock_ops_cb_flags,
7911 case offsetof(struct bpf_sock_ops, sk_txhash):
7912 SOCK_OPS_GET_OR_SET_FIELD(sk_txhash, sk_txhash,
7916 return insn - insn_buf;
7919 static u32 sk_skb_convert_ctx_access(enum bpf_access_type type,
7920 const struct bpf_insn *si,
7921 struct bpf_insn *insn_buf,
7922 struct bpf_prog *prog, u32 *target_size)
7924 struct bpf_insn *insn = insn_buf;
7928 case offsetof(struct __sk_buff, data_end):
7930 off -= offsetof(struct __sk_buff, data_end);
7931 off += offsetof(struct sk_buff, cb);
7932 off += offsetof(struct tcp_skb_cb, bpf.data_end);
7933 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
7937 return bpf_convert_ctx_access(type, si, insn_buf, prog,
7941 return insn - insn_buf;
7944 static u32 sk_msg_convert_ctx_access(enum bpf_access_type type,
7945 const struct bpf_insn *si,
7946 struct bpf_insn *insn_buf,
7947 struct bpf_prog *prog, u32 *target_size)
7949 struct bpf_insn *insn = insn_buf;
7950 #if IS_ENABLED(CONFIG_IPV6)
7954 /* convert ctx uses the fact sg element is first in struct */
7955 BUILD_BUG_ON(offsetof(struct sk_msg, sg) != 0);
7958 case offsetof(struct sk_msg_md, data):
7959 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg, data),
7960 si->dst_reg, si->src_reg,
7961 offsetof(struct sk_msg, data));
7963 case offsetof(struct sk_msg_md, data_end):
7964 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg, data_end),
7965 si->dst_reg, si->src_reg,
7966 offsetof(struct sk_msg, data_end));
7968 case offsetof(struct sk_msg_md, family):
7969 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_family) != 2);
7971 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7973 si->dst_reg, si->src_reg,
7974 offsetof(struct sk_msg, sk));
7975 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
7976 offsetof(struct sock_common, skc_family));
7979 case offsetof(struct sk_msg_md, remote_ip4):
7980 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_daddr) != 4);
7982 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7984 si->dst_reg, si->src_reg,
7985 offsetof(struct sk_msg, sk));
7986 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7987 offsetof(struct sock_common, skc_daddr));
7990 case offsetof(struct sk_msg_md, local_ip4):
7991 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
7992 skc_rcv_saddr) != 4);
7994 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7996 si->dst_reg, si->src_reg,
7997 offsetof(struct sk_msg, sk));
7998 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7999 offsetof(struct sock_common,
8003 case offsetof(struct sk_msg_md, remote_ip6[0]) ...
8004 offsetof(struct sk_msg_md, remote_ip6[3]):
8005 #if IS_ENABLED(CONFIG_IPV6)
8006 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
8007 skc_v6_daddr.s6_addr32[0]) != 4);
8010 off -= offsetof(struct sk_msg_md, remote_ip6[0]);
8011 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8013 si->dst_reg, si->src_reg,
8014 offsetof(struct sk_msg, sk));
8015 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
8016 offsetof(struct sock_common,
8017 skc_v6_daddr.s6_addr32[0]) +
8020 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
8024 case offsetof(struct sk_msg_md, local_ip6[0]) ...
8025 offsetof(struct sk_msg_md, local_ip6[3]):
8026 #if IS_ENABLED(CONFIG_IPV6)
8027 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
8028 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
8031 off -= offsetof(struct sk_msg_md, local_ip6[0]);
8032 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8034 si->dst_reg, si->src_reg,
8035 offsetof(struct sk_msg, sk));
8036 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
8037 offsetof(struct sock_common,
8038 skc_v6_rcv_saddr.s6_addr32[0]) +
8041 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
8045 case offsetof(struct sk_msg_md, remote_port):
8046 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_dport) != 2);
8048 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8050 si->dst_reg, si->src_reg,
8051 offsetof(struct sk_msg, sk));
8052 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
8053 offsetof(struct sock_common, skc_dport));
8054 #ifndef __BIG_ENDIAN_BITFIELD
8055 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
8059 case offsetof(struct sk_msg_md, local_port):
8060 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_num) != 2);
8062 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8064 si->dst_reg, si->src_reg,
8065 offsetof(struct sk_msg, sk));
8066 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
8067 offsetof(struct sock_common, skc_num));
8070 case offsetof(struct sk_msg_md, size):
8071 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg_sg, size),
8072 si->dst_reg, si->src_reg,
8073 offsetof(struct sk_msg_sg, size));
8077 return insn - insn_buf;
8080 const struct bpf_verifier_ops sk_filter_verifier_ops = {
8081 .get_func_proto = sk_filter_func_proto,
8082 .is_valid_access = sk_filter_is_valid_access,
8083 .convert_ctx_access = bpf_convert_ctx_access,
8084 .gen_ld_abs = bpf_gen_ld_abs,
8087 const struct bpf_prog_ops sk_filter_prog_ops = {
8088 .test_run = bpf_prog_test_run_skb,
8091 const struct bpf_verifier_ops tc_cls_act_verifier_ops = {
8092 .get_func_proto = tc_cls_act_func_proto,
8093 .is_valid_access = tc_cls_act_is_valid_access,
8094 .convert_ctx_access = tc_cls_act_convert_ctx_access,
8095 .gen_prologue = tc_cls_act_prologue,
8096 .gen_ld_abs = bpf_gen_ld_abs,
8099 const struct bpf_prog_ops tc_cls_act_prog_ops = {
8100 .test_run = bpf_prog_test_run_skb,
8103 const struct bpf_verifier_ops xdp_verifier_ops = {
8104 .get_func_proto = xdp_func_proto,
8105 .is_valid_access = xdp_is_valid_access,
8106 .convert_ctx_access = xdp_convert_ctx_access,
8107 .gen_prologue = bpf_noop_prologue,
8110 const struct bpf_prog_ops xdp_prog_ops = {
8111 .test_run = bpf_prog_test_run_xdp,
8114 const struct bpf_verifier_ops cg_skb_verifier_ops = {
8115 .get_func_proto = cg_skb_func_proto,
8116 .is_valid_access = cg_skb_is_valid_access,
8117 .convert_ctx_access = bpf_convert_ctx_access,
8120 const struct bpf_prog_ops cg_skb_prog_ops = {
8121 .test_run = bpf_prog_test_run_skb,
8124 const struct bpf_verifier_ops lwt_in_verifier_ops = {
8125 .get_func_proto = lwt_in_func_proto,
8126 .is_valid_access = lwt_is_valid_access,
8127 .convert_ctx_access = bpf_convert_ctx_access,
8130 const struct bpf_prog_ops lwt_in_prog_ops = {
8131 .test_run = bpf_prog_test_run_skb,
8134 const struct bpf_verifier_ops lwt_out_verifier_ops = {
8135 .get_func_proto = lwt_out_func_proto,
8136 .is_valid_access = lwt_is_valid_access,
8137 .convert_ctx_access = bpf_convert_ctx_access,
8140 const struct bpf_prog_ops lwt_out_prog_ops = {
8141 .test_run = bpf_prog_test_run_skb,
8144 const struct bpf_verifier_ops lwt_xmit_verifier_ops = {
8145 .get_func_proto = lwt_xmit_func_proto,
8146 .is_valid_access = lwt_is_valid_access,
8147 .convert_ctx_access = bpf_convert_ctx_access,
8148 .gen_prologue = tc_cls_act_prologue,
8151 const struct bpf_prog_ops lwt_xmit_prog_ops = {
8152 .test_run = bpf_prog_test_run_skb,
8155 const struct bpf_verifier_ops lwt_seg6local_verifier_ops = {
8156 .get_func_proto = lwt_seg6local_func_proto,
8157 .is_valid_access = lwt_is_valid_access,
8158 .convert_ctx_access = bpf_convert_ctx_access,
8161 const struct bpf_prog_ops lwt_seg6local_prog_ops = {
8162 .test_run = bpf_prog_test_run_skb,
8165 const struct bpf_verifier_ops cg_sock_verifier_ops = {
8166 .get_func_proto = sock_filter_func_proto,
8167 .is_valid_access = sock_filter_is_valid_access,
8168 .convert_ctx_access = bpf_sock_convert_ctx_access,
8171 const struct bpf_prog_ops cg_sock_prog_ops = {
8174 const struct bpf_verifier_ops cg_sock_addr_verifier_ops = {
8175 .get_func_proto = sock_addr_func_proto,
8176 .is_valid_access = sock_addr_is_valid_access,
8177 .convert_ctx_access = sock_addr_convert_ctx_access,
8180 const struct bpf_prog_ops cg_sock_addr_prog_ops = {
8183 const struct bpf_verifier_ops sock_ops_verifier_ops = {
8184 .get_func_proto = sock_ops_func_proto,
8185 .is_valid_access = sock_ops_is_valid_access,
8186 .convert_ctx_access = sock_ops_convert_ctx_access,
8189 const struct bpf_prog_ops sock_ops_prog_ops = {
8192 const struct bpf_verifier_ops sk_skb_verifier_ops = {
8193 .get_func_proto = sk_skb_func_proto,
8194 .is_valid_access = sk_skb_is_valid_access,
8195 .convert_ctx_access = sk_skb_convert_ctx_access,
8196 .gen_prologue = sk_skb_prologue,
8199 const struct bpf_prog_ops sk_skb_prog_ops = {
8202 const struct bpf_verifier_ops sk_msg_verifier_ops = {
8203 .get_func_proto = sk_msg_func_proto,
8204 .is_valid_access = sk_msg_is_valid_access,
8205 .convert_ctx_access = sk_msg_convert_ctx_access,
8206 .gen_prologue = bpf_noop_prologue,
8209 const struct bpf_prog_ops sk_msg_prog_ops = {
8212 const struct bpf_verifier_ops flow_dissector_verifier_ops = {
8213 .get_func_proto = flow_dissector_func_proto,
8214 .is_valid_access = flow_dissector_is_valid_access,
8215 .convert_ctx_access = bpf_convert_ctx_access,
8218 const struct bpf_prog_ops flow_dissector_prog_ops = {
8219 .test_run = bpf_prog_test_run_flow_dissector,
8222 int sk_detach_filter(struct sock *sk)
8225 struct sk_filter *filter;
8227 if (sock_flag(sk, SOCK_FILTER_LOCKED))
8230 filter = rcu_dereference_protected(sk->sk_filter,
8231 lockdep_sock_is_held(sk));
8233 RCU_INIT_POINTER(sk->sk_filter, NULL);
8234 sk_filter_uncharge(sk, filter);
8240 EXPORT_SYMBOL_GPL(sk_detach_filter);
8242 int sk_get_filter(struct sock *sk, struct sock_filter __user *ubuf,
8245 struct sock_fprog_kern *fprog;
8246 struct sk_filter *filter;
8250 filter = rcu_dereference_protected(sk->sk_filter,
8251 lockdep_sock_is_held(sk));
8255 /* We're copying the filter that has been originally attached,
8256 * so no conversion/decode needed anymore. eBPF programs that
8257 * have no original program cannot be dumped through this.
8260 fprog = filter->prog->orig_prog;
8266 /* User space only enquires number of filter blocks. */
8270 if (len < fprog->len)
8274 if (copy_to_user(ubuf, fprog->filter, bpf_classic_proglen(fprog)))
8277 /* Instead of bytes, the API requests to return the number
8287 struct sk_reuseport_kern {
8288 struct sk_buff *skb;
8290 struct sock *selected_sk;
8297 static void bpf_init_reuseport_kern(struct sk_reuseport_kern *reuse_kern,
8298 struct sock_reuseport *reuse,
8299 struct sock *sk, struct sk_buff *skb,
8302 reuse_kern->skb = skb;
8303 reuse_kern->sk = sk;
8304 reuse_kern->selected_sk = NULL;
8305 reuse_kern->data_end = skb->data + skb_headlen(skb);
8306 reuse_kern->hash = hash;
8307 reuse_kern->reuseport_id = reuse->reuseport_id;
8308 reuse_kern->bind_inany = reuse->bind_inany;
8311 struct sock *bpf_run_sk_reuseport(struct sock_reuseport *reuse, struct sock *sk,
8312 struct bpf_prog *prog, struct sk_buff *skb,
8315 struct sk_reuseport_kern reuse_kern;
8316 enum sk_action action;
8318 bpf_init_reuseport_kern(&reuse_kern, reuse, sk, skb, hash);
8319 action = BPF_PROG_RUN(prog, &reuse_kern);
8321 if (action == SK_PASS)
8322 return reuse_kern.selected_sk;
8324 return ERR_PTR(-ECONNREFUSED);
8327 BPF_CALL_4(sk_select_reuseport, struct sk_reuseport_kern *, reuse_kern,
8328 struct bpf_map *, map, void *, key, u32, flags)
8330 struct sock_reuseport *reuse;
8331 struct sock *selected_sk;
8333 selected_sk = map->ops->map_lookup_elem(map, key);
8337 reuse = rcu_dereference(selected_sk->sk_reuseport_cb);
8339 /* selected_sk is unhashed (e.g. by close()) after the
8340 * above map_lookup_elem(). Treat selected_sk has already
8341 * been removed from the map.
8345 if (unlikely(reuse->reuseport_id != reuse_kern->reuseport_id)) {
8348 if (unlikely(!reuse_kern->reuseport_id))
8349 /* There is a small race between adding the
8350 * sk to the map and setting the
8351 * reuse_kern->reuseport_id.
8352 * Treat it as the sk has not been added to
8357 sk = reuse_kern->sk;
8358 if (sk->sk_protocol != selected_sk->sk_protocol)
8360 else if (sk->sk_family != selected_sk->sk_family)
8361 return -EAFNOSUPPORT;
8363 /* Catch all. Likely bound to a different sockaddr. */
8367 reuse_kern->selected_sk = selected_sk;
8372 static const struct bpf_func_proto sk_select_reuseport_proto = {
8373 .func = sk_select_reuseport,
8375 .ret_type = RET_INTEGER,
8376 .arg1_type = ARG_PTR_TO_CTX,
8377 .arg2_type = ARG_CONST_MAP_PTR,
8378 .arg3_type = ARG_PTR_TO_MAP_KEY,
8379 .arg4_type = ARG_ANYTHING,
8382 BPF_CALL_4(sk_reuseport_load_bytes,
8383 const struct sk_reuseport_kern *, reuse_kern, u32, offset,
8384 void *, to, u32, len)
8386 return ____bpf_skb_load_bytes(reuse_kern->skb, offset, to, len);
8389 static const struct bpf_func_proto sk_reuseport_load_bytes_proto = {
8390 .func = sk_reuseport_load_bytes,
8392 .ret_type = RET_INTEGER,
8393 .arg1_type = ARG_PTR_TO_CTX,
8394 .arg2_type = ARG_ANYTHING,
8395 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
8396 .arg4_type = ARG_CONST_SIZE,
8399 BPF_CALL_5(sk_reuseport_load_bytes_relative,
8400 const struct sk_reuseport_kern *, reuse_kern, u32, offset,
8401 void *, to, u32, len, u32, start_header)
8403 return ____bpf_skb_load_bytes_relative(reuse_kern->skb, offset, to,
8407 static const struct bpf_func_proto sk_reuseport_load_bytes_relative_proto = {
8408 .func = sk_reuseport_load_bytes_relative,
8410 .ret_type = RET_INTEGER,
8411 .arg1_type = ARG_PTR_TO_CTX,
8412 .arg2_type = ARG_ANYTHING,
8413 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
8414 .arg4_type = ARG_CONST_SIZE,
8415 .arg5_type = ARG_ANYTHING,
8418 static const struct bpf_func_proto *
8419 sk_reuseport_func_proto(enum bpf_func_id func_id,
8420 const struct bpf_prog *prog)
8423 case BPF_FUNC_sk_select_reuseport:
8424 return &sk_select_reuseport_proto;
8425 case BPF_FUNC_skb_load_bytes:
8426 return &sk_reuseport_load_bytes_proto;
8427 case BPF_FUNC_skb_load_bytes_relative:
8428 return &sk_reuseport_load_bytes_relative_proto;
8430 return bpf_base_func_proto(func_id);
8435 sk_reuseport_is_valid_access(int off, int size,
8436 enum bpf_access_type type,
8437 const struct bpf_prog *prog,
8438 struct bpf_insn_access_aux *info)
8440 const u32 size_default = sizeof(__u32);
8442 if (off < 0 || off >= sizeof(struct sk_reuseport_md) ||
8443 off % size || type != BPF_READ)
8447 case offsetof(struct sk_reuseport_md, data):
8448 info->reg_type = PTR_TO_PACKET;
8449 return size == sizeof(__u64);
8451 case offsetof(struct sk_reuseport_md, data_end):
8452 info->reg_type = PTR_TO_PACKET_END;
8453 return size == sizeof(__u64);
8455 case offsetof(struct sk_reuseport_md, hash):
8456 return size == size_default;
8458 /* Fields that allow narrowing */
8459 case offsetof(struct sk_reuseport_md, eth_protocol):
8460 if (size < FIELD_SIZEOF(struct sk_buff, protocol))
8463 case offsetof(struct sk_reuseport_md, ip_protocol):
8464 case offsetof(struct sk_reuseport_md, bind_inany):
8465 case offsetof(struct sk_reuseport_md, len):
8466 bpf_ctx_record_field_size(info, size_default);
8467 return bpf_ctx_narrow_access_ok(off, size, size_default);
8474 #define SK_REUSEPORT_LOAD_FIELD(F) ({ \
8475 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_reuseport_kern, F), \
8476 si->dst_reg, si->src_reg, \
8477 bpf_target_off(struct sk_reuseport_kern, F, \
8478 FIELD_SIZEOF(struct sk_reuseport_kern, F), \
8482 #define SK_REUSEPORT_LOAD_SKB_FIELD(SKB_FIELD) \
8483 SOCK_ADDR_LOAD_NESTED_FIELD(struct sk_reuseport_kern, \
8488 #define SK_REUSEPORT_LOAD_SK_FIELD_SIZE_OFF(SK_FIELD, BPF_SIZE, EXTRA_OFF) \
8489 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(struct sk_reuseport_kern, \
8492 SK_FIELD, BPF_SIZE, EXTRA_OFF)
8494 static u32 sk_reuseport_convert_ctx_access(enum bpf_access_type type,
8495 const struct bpf_insn *si,
8496 struct bpf_insn *insn_buf,
8497 struct bpf_prog *prog,
8500 struct bpf_insn *insn = insn_buf;
8503 case offsetof(struct sk_reuseport_md, data):
8504 SK_REUSEPORT_LOAD_SKB_FIELD(data);
8507 case offsetof(struct sk_reuseport_md, len):
8508 SK_REUSEPORT_LOAD_SKB_FIELD(len);
8511 case offsetof(struct sk_reuseport_md, eth_protocol):
8512 SK_REUSEPORT_LOAD_SKB_FIELD(protocol);
8515 case offsetof(struct sk_reuseport_md, ip_protocol):
8516 BUILD_BUG_ON(HWEIGHT32(SK_FL_PROTO_MASK) != BITS_PER_BYTE);
8517 SK_REUSEPORT_LOAD_SK_FIELD_SIZE_OFF(__sk_flags_offset,
8519 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_PROTO_MASK);
8520 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg,
8522 /* SK_FL_PROTO_MASK and SK_FL_PROTO_SHIFT are endian
8523 * aware. No further narrowing or masking is needed.
8528 case offsetof(struct sk_reuseport_md, data_end):
8529 SK_REUSEPORT_LOAD_FIELD(data_end);
8532 case offsetof(struct sk_reuseport_md, hash):
8533 SK_REUSEPORT_LOAD_FIELD(hash);
8536 case offsetof(struct sk_reuseport_md, bind_inany):
8537 SK_REUSEPORT_LOAD_FIELD(bind_inany);
8541 return insn - insn_buf;
8544 const struct bpf_verifier_ops sk_reuseport_verifier_ops = {
8545 .get_func_proto = sk_reuseport_func_proto,
8546 .is_valid_access = sk_reuseport_is_valid_access,
8547 .convert_ctx_access = sk_reuseport_convert_ctx_access,
8550 const struct bpf_prog_ops sk_reuseport_prog_ops = {
8552 #endif /* CONFIG_INET */