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_len < offsetofend(struct sockaddr, sa_family))
4467 if (addr->sa_family == AF_INET) {
4468 if (addr_len < sizeof(struct sockaddr_in))
4470 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
4472 return __inet_bind(sk, addr, addr_len, true, false);
4473 #if IS_ENABLED(CONFIG_IPV6)
4474 } else if (addr->sa_family == AF_INET6) {
4475 if (addr_len < SIN6_LEN_RFC2133)
4477 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
4479 /* ipv6_bpf_stub cannot be NULL, since it's called from
4480 * bpf_cgroup_inet6_connect hook and ipv6 is already loaded
4482 return ipv6_bpf_stub->inet6_bind(sk, addr, addr_len, true, false);
4483 #endif /* CONFIG_IPV6 */
4485 #endif /* CONFIG_INET */
4487 return -EAFNOSUPPORT;
4490 static const struct bpf_func_proto bpf_bind_proto = {
4493 .ret_type = RET_INTEGER,
4494 .arg1_type = ARG_PTR_TO_CTX,
4495 .arg2_type = ARG_PTR_TO_MEM,
4496 .arg3_type = ARG_CONST_SIZE,
4500 BPF_CALL_5(bpf_skb_get_xfrm_state, struct sk_buff *, skb, u32, index,
4501 struct bpf_xfrm_state *, to, u32, size, u64, flags)
4503 const struct sec_path *sp = skb_sec_path(skb);
4504 const struct xfrm_state *x;
4506 if (!sp || unlikely(index >= sp->len || flags))
4509 x = sp->xvec[index];
4511 if (unlikely(size != sizeof(struct bpf_xfrm_state)))
4514 to->reqid = x->props.reqid;
4515 to->spi = x->id.spi;
4516 to->family = x->props.family;
4519 if (to->family == AF_INET6) {
4520 memcpy(to->remote_ipv6, x->props.saddr.a6,
4521 sizeof(to->remote_ipv6));
4523 to->remote_ipv4 = x->props.saddr.a4;
4524 memset(&to->remote_ipv6[1], 0, sizeof(__u32) * 3);
4529 memset(to, 0, size);
4533 static const struct bpf_func_proto bpf_skb_get_xfrm_state_proto = {
4534 .func = bpf_skb_get_xfrm_state,
4536 .ret_type = RET_INTEGER,
4537 .arg1_type = ARG_PTR_TO_CTX,
4538 .arg2_type = ARG_ANYTHING,
4539 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
4540 .arg4_type = ARG_CONST_SIZE,
4541 .arg5_type = ARG_ANYTHING,
4545 #if IS_ENABLED(CONFIG_INET) || IS_ENABLED(CONFIG_IPV6)
4546 static int bpf_fib_set_fwd_params(struct bpf_fib_lookup *params,
4547 const struct neighbour *neigh,
4548 const struct net_device *dev)
4550 memcpy(params->dmac, neigh->ha, ETH_ALEN);
4551 memcpy(params->smac, dev->dev_addr, ETH_ALEN);
4552 params->h_vlan_TCI = 0;
4553 params->h_vlan_proto = 0;
4554 params->ifindex = dev->ifindex;
4560 #if IS_ENABLED(CONFIG_INET)
4561 static int bpf_ipv4_fib_lookup(struct net *net, struct bpf_fib_lookup *params,
4562 u32 flags, bool check_mtu)
4564 struct fib_nh_common *nhc;
4565 struct in_device *in_dev;
4566 struct neighbour *neigh;
4567 struct net_device *dev;
4568 struct fib_result res;
4573 dev = dev_get_by_index_rcu(net, params->ifindex);
4577 /* verify forwarding is enabled on this interface */
4578 in_dev = __in_dev_get_rcu(dev);
4579 if (unlikely(!in_dev || !IN_DEV_FORWARD(in_dev)))
4580 return BPF_FIB_LKUP_RET_FWD_DISABLED;
4582 if (flags & BPF_FIB_LOOKUP_OUTPUT) {
4584 fl4.flowi4_oif = params->ifindex;
4586 fl4.flowi4_iif = params->ifindex;
4589 fl4.flowi4_tos = params->tos & IPTOS_RT_MASK;
4590 fl4.flowi4_scope = RT_SCOPE_UNIVERSE;
4591 fl4.flowi4_flags = 0;
4593 fl4.flowi4_proto = params->l4_protocol;
4594 fl4.daddr = params->ipv4_dst;
4595 fl4.saddr = params->ipv4_src;
4596 fl4.fl4_sport = params->sport;
4597 fl4.fl4_dport = params->dport;
4599 if (flags & BPF_FIB_LOOKUP_DIRECT) {
4600 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
4601 struct fib_table *tb;
4603 tb = fib_get_table(net, tbid);
4605 return BPF_FIB_LKUP_RET_NOT_FWDED;
4607 err = fib_table_lookup(tb, &fl4, &res, FIB_LOOKUP_NOREF);
4609 fl4.flowi4_mark = 0;
4610 fl4.flowi4_secid = 0;
4611 fl4.flowi4_tun_key.tun_id = 0;
4612 fl4.flowi4_uid = sock_net_uid(net, NULL);
4614 err = fib_lookup(net, &fl4, &res, FIB_LOOKUP_NOREF);
4618 /* map fib lookup errors to RTN_ type */
4620 return BPF_FIB_LKUP_RET_BLACKHOLE;
4621 if (err == -EHOSTUNREACH)
4622 return BPF_FIB_LKUP_RET_UNREACHABLE;
4624 return BPF_FIB_LKUP_RET_PROHIBIT;
4626 return BPF_FIB_LKUP_RET_NOT_FWDED;
4629 if (res.type != RTN_UNICAST)
4630 return BPF_FIB_LKUP_RET_NOT_FWDED;
4632 if (res.fi->fib_nhs > 1)
4633 fib_select_path(net, &res, &fl4, NULL);
4636 mtu = ip_mtu_from_fib_result(&res, params->ipv4_dst);
4637 if (params->tot_len > mtu)
4638 return BPF_FIB_LKUP_RET_FRAG_NEEDED;
4643 /* do not handle lwt encaps right now */
4644 if (nhc->nhc_lwtstate)
4645 return BPF_FIB_LKUP_RET_UNSUPP_LWT;
4649 params->rt_metric = res.fi->fib_priority;
4651 /* xdp and cls_bpf programs are run in RCU-bh so
4652 * rcu_read_lock_bh is not needed here
4654 if (likely(nhc->nhc_gw_family != AF_INET6)) {
4655 if (nhc->nhc_gw_family)
4656 params->ipv4_dst = nhc->nhc_gw.ipv4;
4658 neigh = __ipv4_neigh_lookup_noref(dev,
4659 (__force u32)params->ipv4_dst);
4661 struct in6_addr *dst = (struct in6_addr *)params->ipv6_dst;
4663 params->family = AF_INET6;
4664 *dst = nhc->nhc_gw.ipv6;
4665 neigh = __ipv6_neigh_lookup_noref_stub(dev, dst);
4669 return BPF_FIB_LKUP_RET_NO_NEIGH;
4671 return bpf_fib_set_fwd_params(params, neigh, dev);
4675 #if IS_ENABLED(CONFIG_IPV6)
4676 static int bpf_ipv6_fib_lookup(struct net *net, struct bpf_fib_lookup *params,
4677 u32 flags, bool check_mtu)
4679 struct in6_addr *src = (struct in6_addr *) params->ipv6_src;
4680 struct in6_addr *dst = (struct in6_addr *) params->ipv6_dst;
4681 struct neighbour *neigh;
4682 struct net_device *dev;
4683 struct inet6_dev *idev;
4684 struct fib6_info *f6i;
4690 /* link local addresses are never forwarded */
4691 if (rt6_need_strict(dst) || rt6_need_strict(src))
4692 return BPF_FIB_LKUP_RET_NOT_FWDED;
4694 dev = dev_get_by_index_rcu(net, params->ifindex);
4698 idev = __in6_dev_get_safely(dev);
4699 if (unlikely(!idev || !net->ipv6.devconf_all->forwarding))
4700 return BPF_FIB_LKUP_RET_FWD_DISABLED;
4702 if (flags & BPF_FIB_LOOKUP_OUTPUT) {
4704 oif = fl6.flowi6_oif = params->ifindex;
4706 oif = fl6.flowi6_iif = params->ifindex;
4708 strict = RT6_LOOKUP_F_HAS_SADDR;
4710 fl6.flowlabel = params->flowinfo;
4711 fl6.flowi6_scope = 0;
4712 fl6.flowi6_flags = 0;
4715 fl6.flowi6_proto = params->l4_protocol;
4718 fl6.fl6_sport = params->sport;
4719 fl6.fl6_dport = params->dport;
4721 if (flags & BPF_FIB_LOOKUP_DIRECT) {
4722 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
4723 struct fib6_table *tb;
4725 tb = ipv6_stub->fib6_get_table(net, tbid);
4727 return BPF_FIB_LKUP_RET_NOT_FWDED;
4729 f6i = ipv6_stub->fib6_table_lookup(net, tb, oif, &fl6, strict);
4731 fl6.flowi6_mark = 0;
4732 fl6.flowi6_secid = 0;
4733 fl6.flowi6_tun_key.tun_id = 0;
4734 fl6.flowi6_uid = sock_net_uid(net, NULL);
4736 f6i = ipv6_stub->fib6_lookup(net, oif, &fl6, strict);
4739 if (unlikely(IS_ERR_OR_NULL(f6i) || f6i == net->ipv6.fib6_null_entry))
4740 return BPF_FIB_LKUP_RET_NOT_FWDED;
4742 if (unlikely(f6i->fib6_flags & RTF_REJECT)) {
4743 switch (f6i->fib6_type) {
4745 return BPF_FIB_LKUP_RET_BLACKHOLE;
4746 case RTN_UNREACHABLE:
4747 return BPF_FIB_LKUP_RET_UNREACHABLE;
4749 return BPF_FIB_LKUP_RET_PROHIBIT;
4751 return BPF_FIB_LKUP_RET_NOT_FWDED;
4755 if (f6i->fib6_type != RTN_UNICAST)
4756 return BPF_FIB_LKUP_RET_NOT_FWDED;
4758 if (f6i->fib6_nsiblings && fl6.flowi6_oif == 0)
4759 f6i = ipv6_stub->fib6_multipath_select(net, f6i, &fl6,
4760 fl6.flowi6_oif, NULL,
4764 mtu = ipv6_stub->ip6_mtu_from_fib6(f6i, dst, src);
4765 if (params->tot_len > mtu)
4766 return BPF_FIB_LKUP_RET_FRAG_NEEDED;
4769 if (f6i->fib6_nh.fib_nh_lws)
4770 return BPF_FIB_LKUP_RET_UNSUPP_LWT;
4772 if (f6i->fib6_nh.fib_nh_gw_family)
4773 *dst = f6i->fib6_nh.fib_nh_gw6;
4775 dev = f6i->fib6_nh.fib_nh_dev;
4776 params->rt_metric = f6i->fib6_metric;
4778 /* xdp and cls_bpf programs are run in RCU-bh so rcu_read_lock_bh is
4781 neigh = __ipv6_neigh_lookup_noref_stub(dev, dst);
4783 return BPF_FIB_LKUP_RET_NO_NEIGH;
4785 return bpf_fib_set_fwd_params(params, neigh, dev);
4789 BPF_CALL_4(bpf_xdp_fib_lookup, struct xdp_buff *, ctx,
4790 struct bpf_fib_lookup *, params, int, plen, u32, flags)
4792 if (plen < sizeof(*params))
4795 if (flags & ~(BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_OUTPUT))
4798 switch (params->family) {
4799 #if IS_ENABLED(CONFIG_INET)
4801 return bpf_ipv4_fib_lookup(dev_net(ctx->rxq->dev), params,
4804 #if IS_ENABLED(CONFIG_IPV6)
4806 return bpf_ipv6_fib_lookup(dev_net(ctx->rxq->dev), params,
4810 return -EAFNOSUPPORT;
4813 static const struct bpf_func_proto bpf_xdp_fib_lookup_proto = {
4814 .func = bpf_xdp_fib_lookup,
4816 .ret_type = RET_INTEGER,
4817 .arg1_type = ARG_PTR_TO_CTX,
4818 .arg2_type = ARG_PTR_TO_MEM,
4819 .arg3_type = ARG_CONST_SIZE,
4820 .arg4_type = ARG_ANYTHING,
4823 BPF_CALL_4(bpf_skb_fib_lookup, struct sk_buff *, skb,
4824 struct bpf_fib_lookup *, params, int, plen, u32, flags)
4826 struct net *net = dev_net(skb->dev);
4827 int rc = -EAFNOSUPPORT;
4829 if (plen < sizeof(*params))
4832 if (flags & ~(BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_OUTPUT))
4835 switch (params->family) {
4836 #if IS_ENABLED(CONFIG_INET)
4838 rc = bpf_ipv4_fib_lookup(net, params, flags, false);
4841 #if IS_ENABLED(CONFIG_IPV6)
4843 rc = bpf_ipv6_fib_lookup(net, params, flags, false);
4849 struct net_device *dev;
4851 dev = dev_get_by_index_rcu(net, params->ifindex);
4852 if (!is_skb_forwardable(dev, skb))
4853 rc = BPF_FIB_LKUP_RET_FRAG_NEEDED;
4859 static const struct bpf_func_proto bpf_skb_fib_lookup_proto = {
4860 .func = bpf_skb_fib_lookup,
4862 .ret_type = RET_INTEGER,
4863 .arg1_type = ARG_PTR_TO_CTX,
4864 .arg2_type = ARG_PTR_TO_MEM,
4865 .arg3_type = ARG_CONST_SIZE,
4866 .arg4_type = ARG_ANYTHING,
4869 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
4870 static int bpf_push_seg6_encap(struct sk_buff *skb, u32 type, void *hdr, u32 len)
4873 struct ipv6_sr_hdr *srh = (struct ipv6_sr_hdr *)hdr;
4875 if (!seg6_validate_srh(srh, len))
4879 case BPF_LWT_ENCAP_SEG6_INLINE:
4880 if (skb->protocol != htons(ETH_P_IPV6))
4883 err = seg6_do_srh_inline(skb, srh);
4885 case BPF_LWT_ENCAP_SEG6:
4886 skb_reset_inner_headers(skb);
4887 skb->encapsulation = 1;
4888 err = seg6_do_srh_encap(skb, srh, IPPROTO_IPV6);
4894 bpf_compute_data_pointers(skb);
4898 ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
4899 skb_set_transport_header(skb, sizeof(struct ipv6hdr));
4901 return seg6_lookup_nexthop(skb, NULL, 0);
4903 #endif /* CONFIG_IPV6_SEG6_BPF */
4905 #if IS_ENABLED(CONFIG_LWTUNNEL_BPF)
4906 static int bpf_push_ip_encap(struct sk_buff *skb, void *hdr, u32 len,
4909 return bpf_lwt_push_ip_encap(skb, hdr, len, ingress);
4913 BPF_CALL_4(bpf_lwt_in_push_encap, struct sk_buff *, skb, u32, type, void *, hdr,
4917 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
4918 case BPF_LWT_ENCAP_SEG6:
4919 case BPF_LWT_ENCAP_SEG6_INLINE:
4920 return bpf_push_seg6_encap(skb, type, hdr, len);
4922 #if IS_ENABLED(CONFIG_LWTUNNEL_BPF)
4923 case BPF_LWT_ENCAP_IP:
4924 return bpf_push_ip_encap(skb, hdr, len, true /* ingress */);
4931 BPF_CALL_4(bpf_lwt_xmit_push_encap, struct sk_buff *, skb, u32, type,
4932 void *, hdr, u32, len)
4935 #if IS_ENABLED(CONFIG_LWTUNNEL_BPF)
4936 case BPF_LWT_ENCAP_IP:
4937 return bpf_push_ip_encap(skb, hdr, len, false /* egress */);
4944 static const struct bpf_func_proto bpf_lwt_in_push_encap_proto = {
4945 .func = bpf_lwt_in_push_encap,
4947 .ret_type = RET_INTEGER,
4948 .arg1_type = ARG_PTR_TO_CTX,
4949 .arg2_type = ARG_ANYTHING,
4950 .arg3_type = ARG_PTR_TO_MEM,
4951 .arg4_type = ARG_CONST_SIZE
4954 static const struct bpf_func_proto bpf_lwt_xmit_push_encap_proto = {
4955 .func = bpf_lwt_xmit_push_encap,
4957 .ret_type = RET_INTEGER,
4958 .arg1_type = ARG_PTR_TO_CTX,
4959 .arg2_type = ARG_ANYTHING,
4960 .arg3_type = ARG_PTR_TO_MEM,
4961 .arg4_type = ARG_CONST_SIZE
4964 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
4965 BPF_CALL_4(bpf_lwt_seg6_store_bytes, struct sk_buff *, skb, u32, offset,
4966 const void *, from, u32, len)
4968 struct seg6_bpf_srh_state *srh_state =
4969 this_cpu_ptr(&seg6_bpf_srh_states);
4970 struct ipv6_sr_hdr *srh = srh_state->srh;
4971 void *srh_tlvs, *srh_end, *ptr;
4977 srh_tlvs = (void *)((char *)srh + ((srh->first_segment + 1) << 4));
4978 srh_end = (void *)((char *)srh + sizeof(*srh) + srh_state->hdrlen);
4980 ptr = skb->data + offset;
4981 if (ptr >= srh_tlvs && ptr + len <= srh_end)
4982 srh_state->valid = false;
4983 else if (ptr < (void *)&srh->flags ||
4984 ptr + len > (void *)&srh->segments)
4987 if (unlikely(bpf_try_make_writable(skb, offset + len)))
4989 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0)
4991 srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
4993 memcpy(skb->data + offset, from, len);
4997 static const struct bpf_func_proto bpf_lwt_seg6_store_bytes_proto = {
4998 .func = bpf_lwt_seg6_store_bytes,
5000 .ret_type = RET_INTEGER,
5001 .arg1_type = ARG_PTR_TO_CTX,
5002 .arg2_type = ARG_ANYTHING,
5003 .arg3_type = ARG_PTR_TO_MEM,
5004 .arg4_type = ARG_CONST_SIZE
5007 static void bpf_update_srh_state(struct sk_buff *skb)
5009 struct seg6_bpf_srh_state *srh_state =
5010 this_cpu_ptr(&seg6_bpf_srh_states);
5013 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0) {
5014 srh_state->srh = NULL;
5016 srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
5017 srh_state->hdrlen = srh_state->srh->hdrlen << 3;
5018 srh_state->valid = true;
5022 BPF_CALL_4(bpf_lwt_seg6_action, struct sk_buff *, skb,
5023 u32, action, void *, param, u32, param_len)
5025 struct seg6_bpf_srh_state *srh_state =
5026 this_cpu_ptr(&seg6_bpf_srh_states);
5031 case SEG6_LOCAL_ACTION_END_X:
5032 if (!seg6_bpf_has_valid_srh(skb))
5034 if (param_len != sizeof(struct in6_addr))
5036 return seg6_lookup_nexthop(skb, (struct in6_addr *)param, 0);
5037 case SEG6_LOCAL_ACTION_END_T:
5038 if (!seg6_bpf_has_valid_srh(skb))
5040 if (param_len != sizeof(int))
5042 return seg6_lookup_nexthop(skb, NULL, *(int *)param);
5043 case SEG6_LOCAL_ACTION_END_DT6:
5044 if (!seg6_bpf_has_valid_srh(skb))
5046 if (param_len != sizeof(int))
5049 if (ipv6_find_hdr(skb, &hdroff, IPPROTO_IPV6, NULL, NULL) < 0)
5051 if (!pskb_pull(skb, hdroff))
5054 skb_postpull_rcsum(skb, skb_network_header(skb), hdroff);
5055 skb_reset_network_header(skb);
5056 skb_reset_transport_header(skb);
5057 skb->encapsulation = 0;
5059 bpf_compute_data_pointers(skb);
5060 bpf_update_srh_state(skb);
5061 return seg6_lookup_nexthop(skb, NULL, *(int *)param);
5062 case SEG6_LOCAL_ACTION_END_B6:
5063 if (srh_state->srh && !seg6_bpf_has_valid_srh(skb))
5065 err = bpf_push_seg6_encap(skb, BPF_LWT_ENCAP_SEG6_INLINE,
5068 bpf_update_srh_state(skb);
5071 case SEG6_LOCAL_ACTION_END_B6_ENCAP:
5072 if (srh_state->srh && !seg6_bpf_has_valid_srh(skb))
5074 err = bpf_push_seg6_encap(skb, BPF_LWT_ENCAP_SEG6,
5077 bpf_update_srh_state(skb);
5085 static const struct bpf_func_proto bpf_lwt_seg6_action_proto = {
5086 .func = bpf_lwt_seg6_action,
5088 .ret_type = RET_INTEGER,
5089 .arg1_type = ARG_PTR_TO_CTX,
5090 .arg2_type = ARG_ANYTHING,
5091 .arg3_type = ARG_PTR_TO_MEM,
5092 .arg4_type = ARG_CONST_SIZE
5095 BPF_CALL_3(bpf_lwt_seg6_adjust_srh, struct sk_buff *, skb, u32, offset,
5098 struct seg6_bpf_srh_state *srh_state =
5099 this_cpu_ptr(&seg6_bpf_srh_states);
5100 struct ipv6_sr_hdr *srh = srh_state->srh;
5101 void *srh_end, *srh_tlvs, *ptr;
5102 struct ipv6hdr *hdr;
5106 if (unlikely(srh == NULL))
5109 srh_tlvs = (void *)((unsigned char *)srh + sizeof(*srh) +
5110 ((srh->first_segment + 1) << 4));
5111 srh_end = (void *)((unsigned char *)srh + sizeof(*srh) +
5113 ptr = skb->data + offset;
5115 if (unlikely(ptr < srh_tlvs || ptr > srh_end))
5117 if (unlikely(len < 0 && (void *)((char *)ptr - len) > srh_end))
5121 ret = skb_cow_head(skb, len);
5122 if (unlikely(ret < 0))
5125 ret = bpf_skb_net_hdr_push(skb, offset, len);
5127 ret = bpf_skb_net_hdr_pop(skb, offset, -1 * len);
5130 bpf_compute_data_pointers(skb);
5131 if (unlikely(ret < 0))
5134 hdr = (struct ipv6hdr *)skb->data;
5135 hdr->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
5137 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0)
5139 srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
5140 srh_state->hdrlen += len;
5141 srh_state->valid = false;
5145 static const struct bpf_func_proto bpf_lwt_seg6_adjust_srh_proto = {
5146 .func = bpf_lwt_seg6_adjust_srh,
5148 .ret_type = RET_INTEGER,
5149 .arg1_type = ARG_PTR_TO_CTX,
5150 .arg2_type = ARG_ANYTHING,
5151 .arg3_type = ARG_ANYTHING,
5153 #endif /* CONFIG_IPV6_SEG6_BPF */
5155 #define CONVERT_COMMON_TCP_SOCK_FIELDS(md_type, CONVERT) \
5157 switch (si->off) { \
5158 case offsetof(md_type, snd_cwnd): \
5159 CONVERT(snd_cwnd); break; \
5160 case offsetof(md_type, srtt_us): \
5161 CONVERT(srtt_us); break; \
5162 case offsetof(md_type, snd_ssthresh): \
5163 CONVERT(snd_ssthresh); break; \
5164 case offsetof(md_type, rcv_nxt): \
5165 CONVERT(rcv_nxt); break; \
5166 case offsetof(md_type, snd_nxt): \
5167 CONVERT(snd_nxt); break; \
5168 case offsetof(md_type, snd_una): \
5169 CONVERT(snd_una); break; \
5170 case offsetof(md_type, mss_cache): \
5171 CONVERT(mss_cache); break; \
5172 case offsetof(md_type, ecn_flags): \
5173 CONVERT(ecn_flags); break; \
5174 case offsetof(md_type, rate_delivered): \
5175 CONVERT(rate_delivered); break; \
5176 case offsetof(md_type, rate_interval_us): \
5177 CONVERT(rate_interval_us); break; \
5178 case offsetof(md_type, packets_out): \
5179 CONVERT(packets_out); break; \
5180 case offsetof(md_type, retrans_out): \
5181 CONVERT(retrans_out); break; \
5182 case offsetof(md_type, total_retrans): \
5183 CONVERT(total_retrans); break; \
5184 case offsetof(md_type, segs_in): \
5185 CONVERT(segs_in); break; \
5186 case offsetof(md_type, data_segs_in): \
5187 CONVERT(data_segs_in); break; \
5188 case offsetof(md_type, segs_out): \
5189 CONVERT(segs_out); break; \
5190 case offsetof(md_type, data_segs_out): \
5191 CONVERT(data_segs_out); break; \
5192 case offsetof(md_type, lost_out): \
5193 CONVERT(lost_out); break; \
5194 case offsetof(md_type, sacked_out): \
5195 CONVERT(sacked_out); break; \
5196 case offsetof(md_type, bytes_received): \
5197 CONVERT(bytes_received); break; \
5198 case offsetof(md_type, bytes_acked): \
5199 CONVERT(bytes_acked); break; \
5204 static struct sock *sk_lookup(struct net *net, struct bpf_sock_tuple *tuple,
5205 int dif, int sdif, u8 family, u8 proto)
5207 bool refcounted = false;
5208 struct sock *sk = NULL;
5210 if (family == AF_INET) {
5211 __be32 src4 = tuple->ipv4.saddr;
5212 __be32 dst4 = tuple->ipv4.daddr;
5214 if (proto == IPPROTO_TCP)
5215 sk = __inet_lookup(net, &tcp_hashinfo, NULL, 0,
5216 src4, tuple->ipv4.sport,
5217 dst4, tuple->ipv4.dport,
5218 dif, sdif, &refcounted);
5220 sk = __udp4_lib_lookup(net, src4, tuple->ipv4.sport,
5221 dst4, tuple->ipv4.dport,
5222 dif, sdif, &udp_table, NULL);
5223 #if IS_ENABLED(CONFIG_IPV6)
5225 struct in6_addr *src6 = (struct in6_addr *)&tuple->ipv6.saddr;
5226 struct in6_addr *dst6 = (struct in6_addr *)&tuple->ipv6.daddr;
5228 if (proto == IPPROTO_TCP)
5229 sk = __inet6_lookup(net, &tcp_hashinfo, NULL, 0,
5230 src6, tuple->ipv6.sport,
5231 dst6, ntohs(tuple->ipv6.dport),
5232 dif, sdif, &refcounted);
5233 else if (likely(ipv6_bpf_stub))
5234 sk = ipv6_bpf_stub->udp6_lib_lookup(net,
5235 src6, tuple->ipv6.sport,
5236 dst6, tuple->ipv6.dport,
5242 if (unlikely(sk && !refcounted && !sock_flag(sk, SOCK_RCU_FREE))) {
5243 WARN_ONCE(1, "Found non-RCU, unreferenced socket!");
5249 /* bpf_skc_lookup performs the core lookup for different types of sockets,
5250 * taking a reference on the socket if it doesn't have the flag SOCK_RCU_FREE.
5251 * Returns the socket as an 'unsigned long' to simplify the casting in the
5252 * callers to satisfy BPF_CALL declarations.
5254 static struct sock *
5255 __bpf_skc_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
5256 struct net *caller_net, u32 ifindex, u8 proto, u64 netns_id,
5259 struct sock *sk = NULL;
5260 u8 family = AF_UNSPEC;
5264 family = len == sizeof(tuple->ipv4) ? AF_INET : AF_INET6;
5265 if (unlikely(family == AF_UNSPEC || flags ||
5266 !((s32)netns_id < 0 || netns_id <= S32_MAX)))
5269 if (family == AF_INET)
5270 sdif = inet_sdif(skb);
5272 sdif = inet6_sdif(skb);
5274 if ((s32)netns_id < 0) {
5276 sk = sk_lookup(net, tuple, ifindex, sdif, family, proto);
5278 net = get_net_ns_by_id(caller_net, netns_id);
5281 sk = sk_lookup(net, tuple, ifindex, sdif, family, proto);
5289 static struct sock *
5290 __bpf_sk_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
5291 struct net *caller_net, u32 ifindex, u8 proto, u64 netns_id,
5294 struct sock *sk = __bpf_skc_lookup(skb, tuple, len, caller_net,
5295 ifindex, proto, netns_id, flags);
5298 sk = sk_to_full_sk(sk);
5303 static struct sock *
5304 bpf_skc_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
5305 u8 proto, u64 netns_id, u64 flags)
5307 struct net *caller_net;
5311 caller_net = dev_net(skb->dev);
5312 ifindex = skb->dev->ifindex;
5314 caller_net = sock_net(skb->sk);
5318 return __bpf_skc_lookup(skb, tuple, len, caller_net, ifindex, proto,
5322 static struct sock *
5323 bpf_sk_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
5324 u8 proto, u64 netns_id, u64 flags)
5326 struct sock *sk = bpf_skc_lookup(skb, tuple, len, proto, netns_id,
5330 sk = sk_to_full_sk(sk);
5335 BPF_CALL_5(bpf_skc_lookup_tcp, struct sk_buff *, skb,
5336 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
5338 return (unsigned long)bpf_skc_lookup(skb, tuple, len, IPPROTO_TCP,
5342 static const struct bpf_func_proto bpf_skc_lookup_tcp_proto = {
5343 .func = bpf_skc_lookup_tcp,
5346 .ret_type = RET_PTR_TO_SOCK_COMMON_OR_NULL,
5347 .arg1_type = ARG_PTR_TO_CTX,
5348 .arg2_type = ARG_PTR_TO_MEM,
5349 .arg3_type = ARG_CONST_SIZE,
5350 .arg4_type = ARG_ANYTHING,
5351 .arg5_type = ARG_ANYTHING,
5354 BPF_CALL_5(bpf_sk_lookup_tcp, struct sk_buff *, skb,
5355 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
5357 return (unsigned long)bpf_sk_lookup(skb, tuple, len, IPPROTO_TCP,
5361 static const struct bpf_func_proto bpf_sk_lookup_tcp_proto = {
5362 .func = bpf_sk_lookup_tcp,
5365 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
5366 .arg1_type = ARG_PTR_TO_CTX,
5367 .arg2_type = ARG_PTR_TO_MEM,
5368 .arg3_type = ARG_CONST_SIZE,
5369 .arg4_type = ARG_ANYTHING,
5370 .arg5_type = ARG_ANYTHING,
5373 BPF_CALL_5(bpf_sk_lookup_udp, struct sk_buff *, skb,
5374 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
5376 return (unsigned long)bpf_sk_lookup(skb, tuple, len, IPPROTO_UDP,
5380 static const struct bpf_func_proto bpf_sk_lookup_udp_proto = {
5381 .func = bpf_sk_lookup_udp,
5384 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
5385 .arg1_type = ARG_PTR_TO_CTX,
5386 .arg2_type = ARG_PTR_TO_MEM,
5387 .arg3_type = ARG_CONST_SIZE,
5388 .arg4_type = ARG_ANYTHING,
5389 .arg5_type = ARG_ANYTHING,
5392 BPF_CALL_1(bpf_sk_release, struct sock *, sk)
5394 if (!sock_flag(sk, SOCK_RCU_FREE))
5399 static const struct bpf_func_proto bpf_sk_release_proto = {
5400 .func = bpf_sk_release,
5402 .ret_type = RET_INTEGER,
5403 .arg1_type = ARG_PTR_TO_SOCK_COMMON,
5406 BPF_CALL_5(bpf_xdp_sk_lookup_udp, struct xdp_buff *, ctx,
5407 struct bpf_sock_tuple *, tuple, u32, len, u32, netns_id, u64, flags)
5409 struct net *caller_net = dev_net(ctx->rxq->dev);
5410 int ifindex = ctx->rxq->dev->ifindex;
5412 return (unsigned long)__bpf_sk_lookup(NULL, tuple, len, caller_net,
5413 ifindex, IPPROTO_UDP, netns_id,
5417 static const struct bpf_func_proto bpf_xdp_sk_lookup_udp_proto = {
5418 .func = bpf_xdp_sk_lookup_udp,
5421 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
5422 .arg1_type = ARG_PTR_TO_CTX,
5423 .arg2_type = ARG_PTR_TO_MEM,
5424 .arg3_type = ARG_CONST_SIZE,
5425 .arg4_type = ARG_ANYTHING,
5426 .arg5_type = ARG_ANYTHING,
5429 BPF_CALL_5(bpf_xdp_skc_lookup_tcp, struct xdp_buff *, ctx,
5430 struct bpf_sock_tuple *, tuple, u32, len, u32, netns_id, u64, flags)
5432 struct net *caller_net = dev_net(ctx->rxq->dev);
5433 int ifindex = ctx->rxq->dev->ifindex;
5435 return (unsigned long)__bpf_skc_lookup(NULL, tuple, len, caller_net,
5436 ifindex, IPPROTO_TCP, netns_id,
5440 static const struct bpf_func_proto bpf_xdp_skc_lookup_tcp_proto = {
5441 .func = bpf_xdp_skc_lookup_tcp,
5444 .ret_type = RET_PTR_TO_SOCK_COMMON_OR_NULL,
5445 .arg1_type = ARG_PTR_TO_CTX,
5446 .arg2_type = ARG_PTR_TO_MEM,
5447 .arg3_type = ARG_CONST_SIZE,
5448 .arg4_type = ARG_ANYTHING,
5449 .arg5_type = ARG_ANYTHING,
5452 BPF_CALL_5(bpf_xdp_sk_lookup_tcp, struct xdp_buff *, ctx,
5453 struct bpf_sock_tuple *, tuple, u32, len, u32, netns_id, u64, flags)
5455 struct net *caller_net = dev_net(ctx->rxq->dev);
5456 int ifindex = ctx->rxq->dev->ifindex;
5458 return (unsigned long)__bpf_sk_lookup(NULL, tuple, len, caller_net,
5459 ifindex, IPPROTO_TCP, netns_id,
5463 static const struct bpf_func_proto bpf_xdp_sk_lookup_tcp_proto = {
5464 .func = bpf_xdp_sk_lookup_tcp,
5467 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
5468 .arg1_type = ARG_PTR_TO_CTX,
5469 .arg2_type = ARG_PTR_TO_MEM,
5470 .arg3_type = ARG_CONST_SIZE,
5471 .arg4_type = ARG_ANYTHING,
5472 .arg5_type = ARG_ANYTHING,
5475 BPF_CALL_5(bpf_sock_addr_skc_lookup_tcp, struct bpf_sock_addr_kern *, ctx,
5476 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
5478 return (unsigned long)__bpf_skc_lookup(NULL, tuple, len,
5479 sock_net(ctx->sk), 0,
5480 IPPROTO_TCP, netns_id, flags);
5483 static const struct bpf_func_proto bpf_sock_addr_skc_lookup_tcp_proto = {
5484 .func = bpf_sock_addr_skc_lookup_tcp,
5486 .ret_type = RET_PTR_TO_SOCK_COMMON_OR_NULL,
5487 .arg1_type = ARG_PTR_TO_CTX,
5488 .arg2_type = ARG_PTR_TO_MEM,
5489 .arg3_type = ARG_CONST_SIZE,
5490 .arg4_type = ARG_ANYTHING,
5491 .arg5_type = ARG_ANYTHING,
5494 BPF_CALL_5(bpf_sock_addr_sk_lookup_tcp, struct bpf_sock_addr_kern *, ctx,
5495 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
5497 return (unsigned long)__bpf_sk_lookup(NULL, tuple, len,
5498 sock_net(ctx->sk), 0, IPPROTO_TCP,
5502 static const struct bpf_func_proto bpf_sock_addr_sk_lookup_tcp_proto = {
5503 .func = bpf_sock_addr_sk_lookup_tcp,
5505 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
5506 .arg1_type = ARG_PTR_TO_CTX,
5507 .arg2_type = ARG_PTR_TO_MEM,
5508 .arg3_type = ARG_CONST_SIZE,
5509 .arg4_type = ARG_ANYTHING,
5510 .arg5_type = ARG_ANYTHING,
5513 BPF_CALL_5(bpf_sock_addr_sk_lookup_udp, struct bpf_sock_addr_kern *, ctx,
5514 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
5516 return (unsigned long)__bpf_sk_lookup(NULL, tuple, len,
5517 sock_net(ctx->sk), 0, IPPROTO_UDP,
5521 static const struct bpf_func_proto bpf_sock_addr_sk_lookup_udp_proto = {
5522 .func = bpf_sock_addr_sk_lookup_udp,
5524 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
5525 .arg1_type = ARG_PTR_TO_CTX,
5526 .arg2_type = ARG_PTR_TO_MEM,
5527 .arg3_type = ARG_CONST_SIZE,
5528 .arg4_type = ARG_ANYTHING,
5529 .arg5_type = ARG_ANYTHING,
5532 bool bpf_tcp_sock_is_valid_access(int off, int size, enum bpf_access_type type,
5533 struct bpf_insn_access_aux *info)
5535 if (off < 0 || off >= offsetofend(struct bpf_tcp_sock, bytes_acked))
5538 if (off % size != 0)
5542 case offsetof(struct bpf_tcp_sock, bytes_received):
5543 case offsetof(struct bpf_tcp_sock, bytes_acked):
5544 return size == sizeof(__u64);
5546 return size == sizeof(__u32);
5550 u32 bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type,
5551 const struct bpf_insn *si,
5552 struct bpf_insn *insn_buf,
5553 struct bpf_prog *prog, u32 *target_size)
5555 struct bpf_insn *insn = insn_buf;
5557 #define BPF_TCP_SOCK_GET_COMMON(FIELD) \
5559 BUILD_BUG_ON(FIELD_SIZEOF(struct tcp_sock, FIELD) > \
5560 FIELD_SIZEOF(struct bpf_tcp_sock, FIELD)); \
5561 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct tcp_sock, FIELD),\
5562 si->dst_reg, si->src_reg, \
5563 offsetof(struct tcp_sock, FIELD)); \
5566 CONVERT_COMMON_TCP_SOCK_FIELDS(struct bpf_tcp_sock,
5567 BPF_TCP_SOCK_GET_COMMON);
5569 if (insn > insn_buf)
5570 return insn - insn_buf;
5573 case offsetof(struct bpf_tcp_sock, rtt_min):
5574 BUILD_BUG_ON(FIELD_SIZEOF(struct tcp_sock, rtt_min) !=
5575 sizeof(struct minmax));
5576 BUILD_BUG_ON(sizeof(struct minmax) <
5577 sizeof(struct minmax_sample));
5579 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
5580 offsetof(struct tcp_sock, rtt_min) +
5581 offsetof(struct minmax_sample, v));
5585 return insn - insn_buf;
5588 BPF_CALL_1(bpf_tcp_sock, struct sock *, sk)
5590 if (sk_fullsock(sk) && sk->sk_protocol == IPPROTO_TCP)
5591 return (unsigned long)sk;
5593 return (unsigned long)NULL;
5596 static const struct bpf_func_proto bpf_tcp_sock_proto = {
5597 .func = bpf_tcp_sock,
5599 .ret_type = RET_PTR_TO_TCP_SOCK_OR_NULL,
5600 .arg1_type = ARG_PTR_TO_SOCK_COMMON,
5603 BPF_CALL_1(bpf_get_listener_sock, struct sock *, sk)
5605 sk = sk_to_full_sk(sk);
5607 if (sk->sk_state == TCP_LISTEN && sock_flag(sk, SOCK_RCU_FREE))
5608 return (unsigned long)sk;
5610 return (unsigned long)NULL;
5613 static const struct bpf_func_proto bpf_get_listener_sock_proto = {
5614 .func = bpf_get_listener_sock,
5616 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
5617 .arg1_type = ARG_PTR_TO_SOCK_COMMON,
5620 BPF_CALL_1(bpf_skb_ecn_set_ce, struct sk_buff *, skb)
5622 unsigned int iphdr_len;
5624 if (skb->protocol == cpu_to_be16(ETH_P_IP))
5625 iphdr_len = sizeof(struct iphdr);
5626 else if (skb->protocol == cpu_to_be16(ETH_P_IPV6))
5627 iphdr_len = sizeof(struct ipv6hdr);
5631 if (skb_headlen(skb) < iphdr_len)
5634 if (skb_cloned(skb) && !skb_clone_writable(skb, iphdr_len))
5637 return INET_ECN_set_ce(skb);
5640 static const struct bpf_func_proto bpf_skb_ecn_set_ce_proto = {
5641 .func = bpf_skb_ecn_set_ce,
5643 .ret_type = RET_INTEGER,
5644 .arg1_type = ARG_PTR_TO_CTX,
5647 BPF_CALL_5(bpf_tcp_check_syncookie, struct sock *, sk, void *, iph, u32, iph_len,
5648 struct tcphdr *, th, u32, th_len)
5650 #ifdef CONFIG_SYN_COOKIES
5654 if (unlikely(th_len < sizeof(*th)))
5657 /* sk_listener() allows TCP_NEW_SYN_RECV, which makes no sense here. */
5658 if (sk->sk_protocol != IPPROTO_TCP || sk->sk_state != TCP_LISTEN)
5661 if (!sock_net(sk)->ipv4.sysctl_tcp_syncookies)
5664 if (!th->ack || th->rst || th->syn)
5667 if (tcp_synq_no_recent_overflow(sk))
5670 cookie = ntohl(th->ack_seq) - 1;
5672 switch (sk->sk_family) {
5674 if (unlikely(iph_len < sizeof(struct iphdr)))
5677 ret = __cookie_v4_check((struct iphdr *)iph, th, cookie);
5680 #if IS_BUILTIN(CONFIG_IPV6)
5682 if (unlikely(iph_len < sizeof(struct ipv6hdr)))
5685 ret = __cookie_v6_check((struct ipv6hdr *)iph, th, cookie);
5687 #endif /* CONFIG_IPV6 */
5690 return -EPROTONOSUPPORT;
5702 static const struct bpf_func_proto bpf_tcp_check_syncookie_proto = {
5703 .func = bpf_tcp_check_syncookie,
5706 .ret_type = RET_INTEGER,
5707 .arg1_type = ARG_PTR_TO_SOCK_COMMON,
5708 .arg2_type = ARG_PTR_TO_MEM,
5709 .arg3_type = ARG_CONST_SIZE,
5710 .arg4_type = ARG_PTR_TO_MEM,
5711 .arg5_type = ARG_CONST_SIZE,
5714 #endif /* CONFIG_INET */
5716 bool bpf_helper_changes_pkt_data(void *func)
5718 if (func == bpf_skb_vlan_push ||
5719 func == bpf_skb_vlan_pop ||
5720 func == bpf_skb_store_bytes ||
5721 func == bpf_skb_change_proto ||
5722 func == bpf_skb_change_head ||
5723 func == sk_skb_change_head ||
5724 func == bpf_skb_change_tail ||
5725 func == sk_skb_change_tail ||
5726 func == bpf_skb_adjust_room ||
5727 func == bpf_skb_pull_data ||
5728 func == sk_skb_pull_data ||
5729 func == bpf_clone_redirect ||
5730 func == bpf_l3_csum_replace ||
5731 func == bpf_l4_csum_replace ||
5732 func == bpf_xdp_adjust_head ||
5733 func == bpf_xdp_adjust_meta ||
5734 func == bpf_msg_pull_data ||
5735 func == bpf_msg_push_data ||
5736 func == bpf_msg_pop_data ||
5737 func == bpf_xdp_adjust_tail ||
5738 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
5739 func == bpf_lwt_seg6_store_bytes ||
5740 func == bpf_lwt_seg6_adjust_srh ||
5741 func == bpf_lwt_seg6_action ||
5743 func == bpf_lwt_in_push_encap ||
5744 func == bpf_lwt_xmit_push_encap)
5750 static const struct bpf_func_proto *
5751 bpf_base_func_proto(enum bpf_func_id func_id)
5754 case BPF_FUNC_map_lookup_elem:
5755 return &bpf_map_lookup_elem_proto;
5756 case BPF_FUNC_map_update_elem:
5757 return &bpf_map_update_elem_proto;
5758 case BPF_FUNC_map_delete_elem:
5759 return &bpf_map_delete_elem_proto;
5760 case BPF_FUNC_map_push_elem:
5761 return &bpf_map_push_elem_proto;
5762 case BPF_FUNC_map_pop_elem:
5763 return &bpf_map_pop_elem_proto;
5764 case BPF_FUNC_map_peek_elem:
5765 return &bpf_map_peek_elem_proto;
5766 case BPF_FUNC_get_prandom_u32:
5767 return &bpf_get_prandom_u32_proto;
5768 case BPF_FUNC_get_smp_processor_id:
5769 return &bpf_get_raw_smp_processor_id_proto;
5770 case BPF_FUNC_get_numa_node_id:
5771 return &bpf_get_numa_node_id_proto;
5772 case BPF_FUNC_tail_call:
5773 return &bpf_tail_call_proto;
5774 case BPF_FUNC_ktime_get_ns:
5775 return &bpf_ktime_get_ns_proto;
5780 if (!capable(CAP_SYS_ADMIN))
5784 case BPF_FUNC_spin_lock:
5785 return &bpf_spin_lock_proto;
5786 case BPF_FUNC_spin_unlock:
5787 return &bpf_spin_unlock_proto;
5788 case BPF_FUNC_trace_printk:
5789 return bpf_get_trace_printk_proto();
5795 static const struct bpf_func_proto *
5796 sock_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5799 /* inet and inet6 sockets are created in a process
5800 * context so there is always a valid uid/gid
5802 case BPF_FUNC_get_current_uid_gid:
5803 return &bpf_get_current_uid_gid_proto;
5804 case BPF_FUNC_get_local_storage:
5805 return &bpf_get_local_storage_proto;
5807 return bpf_base_func_proto(func_id);
5811 static const struct bpf_func_proto *
5812 sock_addr_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5815 /* inet and inet6 sockets are created in a process
5816 * context so there is always a valid uid/gid
5818 case BPF_FUNC_get_current_uid_gid:
5819 return &bpf_get_current_uid_gid_proto;
5821 switch (prog->expected_attach_type) {
5822 case BPF_CGROUP_INET4_CONNECT:
5823 case BPF_CGROUP_INET6_CONNECT:
5824 return &bpf_bind_proto;
5828 case BPF_FUNC_get_socket_cookie:
5829 return &bpf_get_socket_cookie_sock_addr_proto;
5830 case BPF_FUNC_get_local_storage:
5831 return &bpf_get_local_storage_proto;
5833 case BPF_FUNC_sk_lookup_tcp:
5834 return &bpf_sock_addr_sk_lookup_tcp_proto;
5835 case BPF_FUNC_sk_lookup_udp:
5836 return &bpf_sock_addr_sk_lookup_udp_proto;
5837 case BPF_FUNC_sk_release:
5838 return &bpf_sk_release_proto;
5839 case BPF_FUNC_skc_lookup_tcp:
5840 return &bpf_sock_addr_skc_lookup_tcp_proto;
5841 #endif /* CONFIG_INET */
5843 return bpf_base_func_proto(func_id);
5847 static const struct bpf_func_proto *
5848 sk_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5851 case BPF_FUNC_skb_load_bytes:
5852 return &bpf_skb_load_bytes_proto;
5853 case BPF_FUNC_skb_load_bytes_relative:
5854 return &bpf_skb_load_bytes_relative_proto;
5855 case BPF_FUNC_get_socket_cookie:
5856 return &bpf_get_socket_cookie_proto;
5857 case BPF_FUNC_get_socket_uid:
5858 return &bpf_get_socket_uid_proto;
5860 return bpf_base_func_proto(func_id);
5864 static const struct bpf_func_proto *
5865 cg_skb_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5868 case BPF_FUNC_get_local_storage:
5869 return &bpf_get_local_storage_proto;
5870 case BPF_FUNC_sk_fullsock:
5871 return &bpf_sk_fullsock_proto;
5873 case BPF_FUNC_tcp_sock:
5874 return &bpf_tcp_sock_proto;
5875 case BPF_FUNC_get_listener_sock:
5876 return &bpf_get_listener_sock_proto;
5877 case BPF_FUNC_skb_ecn_set_ce:
5878 return &bpf_skb_ecn_set_ce_proto;
5881 return sk_filter_func_proto(func_id, prog);
5885 static const struct bpf_func_proto *
5886 tc_cls_act_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5889 case BPF_FUNC_skb_store_bytes:
5890 return &bpf_skb_store_bytes_proto;
5891 case BPF_FUNC_skb_load_bytes:
5892 return &bpf_skb_load_bytes_proto;
5893 case BPF_FUNC_skb_load_bytes_relative:
5894 return &bpf_skb_load_bytes_relative_proto;
5895 case BPF_FUNC_skb_pull_data:
5896 return &bpf_skb_pull_data_proto;
5897 case BPF_FUNC_csum_diff:
5898 return &bpf_csum_diff_proto;
5899 case BPF_FUNC_csum_update:
5900 return &bpf_csum_update_proto;
5901 case BPF_FUNC_l3_csum_replace:
5902 return &bpf_l3_csum_replace_proto;
5903 case BPF_FUNC_l4_csum_replace:
5904 return &bpf_l4_csum_replace_proto;
5905 case BPF_FUNC_clone_redirect:
5906 return &bpf_clone_redirect_proto;
5907 case BPF_FUNC_get_cgroup_classid:
5908 return &bpf_get_cgroup_classid_proto;
5909 case BPF_FUNC_skb_vlan_push:
5910 return &bpf_skb_vlan_push_proto;
5911 case BPF_FUNC_skb_vlan_pop:
5912 return &bpf_skb_vlan_pop_proto;
5913 case BPF_FUNC_skb_change_proto:
5914 return &bpf_skb_change_proto_proto;
5915 case BPF_FUNC_skb_change_type:
5916 return &bpf_skb_change_type_proto;
5917 case BPF_FUNC_skb_adjust_room:
5918 return &bpf_skb_adjust_room_proto;
5919 case BPF_FUNC_skb_change_tail:
5920 return &bpf_skb_change_tail_proto;
5921 case BPF_FUNC_skb_get_tunnel_key:
5922 return &bpf_skb_get_tunnel_key_proto;
5923 case BPF_FUNC_skb_set_tunnel_key:
5924 return bpf_get_skb_set_tunnel_proto(func_id);
5925 case BPF_FUNC_skb_get_tunnel_opt:
5926 return &bpf_skb_get_tunnel_opt_proto;
5927 case BPF_FUNC_skb_set_tunnel_opt:
5928 return bpf_get_skb_set_tunnel_proto(func_id);
5929 case BPF_FUNC_redirect:
5930 return &bpf_redirect_proto;
5931 case BPF_FUNC_get_route_realm:
5932 return &bpf_get_route_realm_proto;
5933 case BPF_FUNC_get_hash_recalc:
5934 return &bpf_get_hash_recalc_proto;
5935 case BPF_FUNC_set_hash_invalid:
5936 return &bpf_set_hash_invalid_proto;
5937 case BPF_FUNC_set_hash:
5938 return &bpf_set_hash_proto;
5939 case BPF_FUNC_perf_event_output:
5940 return &bpf_skb_event_output_proto;
5941 case BPF_FUNC_get_smp_processor_id:
5942 return &bpf_get_smp_processor_id_proto;
5943 case BPF_FUNC_skb_under_cgroup:
5944 return &bpf_skb_under_cgroup_proto;
5945 case BPF_FUNC_get_socket_cookie:
5946 return &bpf_get_socket_cookie_proto;
5947 case BPF_FUNC_get_socket_uid:
5948 return &bpf_get_socket_uid_proto;
5949 case BPF_FUNC_fib_lookup:
5950 return &bpf_skb_fib_lookup_proto;
5951 case BPF_FUNC_sk_fullsock:
5952 return &bpf_sk_fullsock_proto;
5954 case BPF_FUNC_skb_get_xfrm_state:
5955 return &bpf_skb_get_xfrm_state_proto;
5957 #ifdef CONFIG_SOCK_CGROUP_DATA
5958 case BPF_FUNC_skb_cgroup_id:
5959 return &bpf_skb_cgroup_id_proto;
5960 case BPF_FUNC_skb_ancestor_cgroup_id:
5961 return &bpf_skb_ancestor_cgroup_id_proto;
5964 case BPF_FUNC_sk_lookup_tcp:
5965 return &bpf_sk_lookup_tcp_proto;
5966 case BPF_FUNC_sk_lookup_udp:
5967 return &bpf_sk_lookup_udp_proto;
5968 case BPF_FUNC_sk_release:
5969 return &bpf_sk_release_proto;
5970 case BPF_FUNC_tcp_sock:
5971 return &bpf_tcp_sock_proto;
5972 case BPF_FUNC_get_listener_sock:
5973 return &bpf_get_listener_sock_proto;
5974 case BPF_FUNC_skc_lookup_tcp:
5975 return &bpf_skc_lookup_tcp_proto;
5976 case BPF_FUNC_tcp_check_syncookie:
5977 return &bpf_tcp_check_syncookie_proto;
5978 case BPF_FUNC_skb_ecn_set_ce:
5979 return &bpf_skb_ecn_set_ce_proto;
5982 return bpf_base_func_proto(func_id);
5986 static const struct bpf_func_proto *
5987 xdp_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5990 case BPF_FUNC_perf_event_output:
5991 return &bpf_xdp_event_output_proto;
5992 case BPF_FUNC_get_smp_processor_id:
5993 return &bpf_get_smp_processor_id_proto;
5994 case BPF_FUNC_csum_diff:
5995 return &bpf_csum_diff_proto;
5996 case BPF_FUNC_xdp_adjust_head:
5997 return &bpf_xdp_adjust_head_proto;
5998 case BPF_FUNC_xdp_adjust_meta:
5999 return &bpf_xdp_adjust_meta_proto;
6000 case BPF_FUNC_redirect:
6001 return &bpf_xdp_redirect_proto;
6002 case BPF_FUNC_redirect_map:
6003 return &bpf_xdp_redirect_map_proto;
6004 case BPF_FUNC_xdp_adjust_tail:
6005 return &bpf_xdp_adjust_tail_proto;
6006 case BPF_FUNC_fib_lookup:
6007 return &bpf_xdp_fib_lookup_proto;
6009 case BPF_FUNC_sk_lookup_udp:
6010 return &bpf_xdp_sk_lookup_udp_proto;
6011 case BPF_FUNC_sk_lookup_tcp:
6012 return &bpf_xdp_sk_lookup_tcp_proto;
6013 case BPF_FUNC_sk_release:
6014 return &bpf_sk_release_proto;
6015 case BPF_FUNC_skc_lookup_tcp:
6016 return &bpf_xdp_skc_lookup_tcp_proto;
6017 case BPF_FUNC_tcp_check_syncookie:
6018 return &bpf_tcp_check_syncookie_proto;
6021 return bpf_base_func_proto(func_id);
6025 const struct bpf_func_proto bpf_sock_map_update_proto __weak;
6026 const struct bpf_func_proto bpf_sock_hash_update_proto __weak;
6028 static const struct bpf_func_proto *
6029 sock_ops_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6032 case BPF_FUNC_setsockopt:
6033 return &bpf_setsockopt_proto;
6034 case BPF_FUNC_getsockopt:
6035 return &bpf_getsockopt_proto;
6036 case BPF_FUNC_sock_ops_cb_flags_set:
6037 return &bpf_sock_ops_cb_flags_set_proto;
6038 case BPF_FUNC_sock_map_update:
6039 return &bpf_sock_map_update_proto;
6040 case BPF_FUNC_sock_hash_update:
6041 return &bpf_sock_hash_update_proto;
6042 case BPF_FUNC_get_socket_cookie:
6043 return &bpf_get_socket_cookie_sock_ops_proto;
6044 case BPF_FUNC_get_local_storage:
6045 return &bpf_get_local_storage_proto;
6046 case BPF_FUNC_perf_event_output:
6047 return &bpf_sockopt_event_output_proto;
6049 return bpf_base_func_proto(func_id);
6053 const struct bpf_func_proto bpf_msg_redirect_map_proto __weak;
6054 const struct bpf_func_proto bpf_msg_redirect_hash_proto __weak;
6056 static const struct bpf_func_proto *
6057 sk_msg_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6060 case BPF_FUNC_msg_redirect_map:
6061 return &bpf_msg_redirect_map_proto;
6062 case BPF_FUNC_msg_redirect_hash:
6063 return &bpf_msg_redirect_hash_proto;
6064 case BPF_FUNC_msg_apply_bytes:
6065 return &bpf_msg_apply_bytes_proto;
6066 case BPF_FUNC_msg_cork_bytes:
6067 return &bpf_msg_cork_bytes_proto;
6068 case BPF_FUNC_msg_pull_data:
6069 return &bpf_msg_pull_data_proto;
6070 case BPF_FUNC_msg_push_data:
6071 return &bpf_msg_push_data_proto;
6072 case BPF_FUNC_msg_pop_data:
6073 return &bpf_msg_pop_data_proto;
6075 return bpf_base_func_proto(func_id);
6079 const struct bpf_func_proto bpf_sk_redirect_map_proto __weak;
6080 const struct bpf_func_proto bpf_sk_redirect_hash_proto __weak;
6082 static const struct bpf_func_proto *
6083 sk_skb_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6086 case BPF_FUNC_skb_store_bytes:
6087 return &bpf_skb_store_bytes_proto;
6088 case BPF_FUNC_skb_load_bytes:
6089 return &bpf_skb_load_bytes_proto;
6090 case BPF_FUNC_skb_pull_data:
6091 return &sk_skb_pull_data_proto;
6092 case BPF_FUNC_skb_change_tail:
6093 return &sk_skb_change_tail_proto;
6094 case BPF_FUNC_skb_change_head:
6095 return &sk_skb_change_head_proto;
6096 case BPF_FUNC_get_socket_cookie:
6097 return &bpf_get_socket_cookie_proto;
6098 case BPF_FUNC_get_socket_uid:
6099 return &bpf_get_socket_uid_proto;
6100 case BPF_FUNC_sk_redirect_map:
6101 return &bpf_sk_redirect_map_proto;
6102 case BPF_FUNC_sk_redirect_hash:
6103 return &bpf_sk_redirect_hash_proto;
6105 case BPF_FUNC_sk_lookup_tcp:
6106 return &bpf_sk_lookup_tcp_proto;
6107 case BPF_FUNC_sk_lookup_udp:
6108 return &bpf_sk_lookup_udp_proto;
6109 case BPF_FUNC_sk_release:
6110 return &bpf_sk_release_proto;
6111 case BPF_FUNC_skc_lookup_tcp:
6112 return &bpf_skc_lookup_tcp_proto;
6115 return bpf_base_func_proto(func_id);
6119 static const struct bpf_func_proto *
6120 flow_dissector_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6123 case BPF_FUNC_skb_load_bytes:
6124 return &bpf_skb_load_bytes_proto;
6126 return bpf_base_func_proto(func_id);
6130 static const struct bpf_func_proto *
6131 lwt_out_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6134 case BPF_FUNC_skb_load_bytes:
6135 return &bpf_skb_load_bytes_proto;
6136 case BPF_FUNC_skb_pull_data:
6137 return &bpf_skb_pull_data_proto;
6138 case BPF_FUNC_csum_diff:
6139 return &bpf_csum_diff_proto;
6140 case BPF_FUNC_get_cgroup_classid:
6141 return &bpf_get_cgroup_classid_proto;
6142 case BPF_FUNC_get_route_realm:
6143 return &bpf_get_route_realm_proto;
6144 case BPF_FUNC_get_hash_recalc:
6145 return &bpf_get_hash_recalc_proto;
6146 case BPF_FUNC_perf_event_output:
6147 return &bpf_skb_event_output_proto;
6148 case BPF_FUNC_get_smp_processor_id:
6149 return &bpf_get_smp_processor_id_proto;
6150 case BPF_FUNC_skb_under_cgroup:
6151 return &bpf_skb_under_cgroup_proto;
6153 return bpf_base_func_proto(func_id);
6157 static const struct bpf_func_proto *
6158 lwt_in_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6161 case BPF_FUNC_lwt_push_encap:
6162 return &bpf_lwt_in_push_encap_proto;
6164 return lwt_out_func_proto(func_id, prog);
6168 static const struct bpf_func_proto *
6169 lwt_xmit_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6172 case BPF_FUNC_skb_get_tunnel_key:
6173 return &bpf_skb_get_tunnel_key_proto;
6174 case BPF_FUNC_skb_set_tunnel_key:
6175 return bpf_get_skb_set_tunnel_proto(func_id);
6176 case BPF_FUNC_skb_get_tunnel_opt:
6177 return &bpf_skb_get_tunnel_opt_proto;
6178 case BPF_FUNC_skb_set_tunnel_opt:
6179 return bpf_get_skb_set_tunnel_proto(func_id);
6180 case BPF_FUNC_redirect:
6181 return &bpf_redirect_proto;
6182 case BPF_FUNC_clone_redirect:
6183 return &bpf_clone_redirect_proto;
6184 case BPF_FUNC_skb_change_tail:
6185 return &bpf_skb_change_tail_proto;
6186 case BPF_FUNC_skb_change_head:
6187 return &bpf_skb_change_head_proto;
6188 case BPF_FUNC_skb_store_bytes:
6189 return &bpf_skb_store_bytes_proto;
6190 case BPF_FUNC_csum_update:
6191 return &bpf_csum_update_proto;
6192 case BPF_FUNC_l3_csum_replace:
6193 return &bpf_l3_csum_replace_proto;
6194 case BPF_FUNC_l4_csum_replace:
6195 return &bpf_l4_csum_replace_proto;
6196 case BPF_FUNC_set_hash_invalid:
6197 return &bpf_set_hash_invalid_proto;
6198 case BPF_FUNC_lwt_push_encap:
6199 return &bpf_lwt_xmit_push_encap_proto;
6201 return lwt_out_func_proto(func_id, prog);
6205 static const struct bpf_func_proto *
6206 lwt_seg6local_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6209 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
6210 case BPF_FUNC_lwt_seg6_store_bytes:
6211 return &bpf_lwt_seg6_store_bytes_proto;
6212 case BPF_FUNC_lwt_seg6_action:
6213 return &bpf_lwt_seg6_action_proto;
6214 case BPF_FUNC_lwt_seg6_adjust_srh:
6215 return &bpf_lwt_seg6_adjust_srh_proto;
6218 return lwt_out_func_proto(func_id, prog);
6222 static bool bpf_skb_is_valid_access(int off, int size, enum bpf_access_type type,
6223 const struct bpf_prog *prog,
6224 struct bpf_insn_access_aux *info)
6226 const int size_default = sizeof(__u32);
6228 if (off < 0 || off >= sizeof(struct __sk_buff))
6231 /* The verifier guarantees that size > 0. */
6232 if (off % size != 0)
6236 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
6237 if (off + size > offsetofend(struct __sk_buff, cb[4]))
6240 case bpf_ctx_range_till(struct __sk_buff, remote_ip6[0], remote_ip6[3]):
6241 case bpf_ctx_range_till(struct __sk_buff, local_ip6[0], local_ip6[3]):
6242 case bpf_ctx_range_till(struct __sk_buff, remote_ip4, remote_ip4):
6243 case bpf_ctx_range_till(struct __sk_buff, local_ip4, local_ip4):
6244 case bpf_ctx_range(struct __sk_buff, data):
6245 case bpf_ctx_range(struct __sk_buff, data_meta):
6246 case bpf_ctx_range(struct __sk_buff, data_end):
6247 if (size != size_default)
6250 case bpf_ctx_range_ptr(struct __sk_buff, flow_keys):
6251 if (size != sizeof(__u64))
6254 case bpf_ctx_range(struct __sk_buff, tstamp):
6255 if (size != sizeof(__u64))
6258 case offsetof(struct __sk_buff, sk):
6259 if (type == BPF_WRITE || size != sizeof(__u64))
6261 info->reg_type = PTR_TO_SOCK_COMMON_OR_NULL;
6264 /* Only narrow read access allowed for now. */
6265 if (type == BPF_WRITE) {
6266 if (size != size_default)
6269 bpf_ctx_record_field_size(info, size_default);
6270 if (!bpf_ctx_narrow_access_ok(off, size, size_default))
6278 static bool sk_filter_is_valid_access(int off, int size,
6279 enum bpf_access_type type,
6280 const struct bpf_prog *prog,
6281 struct bpf_insn_access_aux *info)
6284 case bpf_ctx_range(struct __sk_buff, tc_classid):
6285 case bpf_ctx_range(struct __sk_buff, data):
6286 case bpf_ctx_range(struct __sk_buff, data_meta):
6287 case bpf_ctx_range(struct __sk_buff, data_end):
6288 case bpf_ctx_range_ptr(struct __sk_buff, flow_keys):
6289 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
6290 case bpf_ctx_range(struct __sk_buff, tstamp):
6291 case bpf_ctx_range(struct __sk_buff, wire_len):
6295 if (type == BPF_WRITE) {
6297 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
6304 return bpf_skb_is_valid_access(off, size, type, prog, info);
6307 static bool cg_skb_is_valid_access(int off, int size,
6308 enum bpf_access_type type,
6309 const struct bpf_prog *prog,
6310 struct bpf_insn_access_aux *info)
6313 case bpf_ctx_range(struct __sk_buff, tc_classid):
6314 case bpf_ctx_range(struct __sk_buff, data_meta):
6315 case bpf_ctx_range_ptr(struct __sk_buff, flow_keys):
6316 case bpf_ctx_range(struct __sk_buff, wire_len):
6318 case bpf_ctx_range(struct __sk_buff, data):
6319 case bpf_ctx_range(struct __sk_buff, data_end):
6320 if (!capable(CAP_SYS_ADMIN))
6325 if (type == BPF_WRITE) {
6327 case bpf_ctx_range(struct __sk_buff, mark):
6328 case bpf_ctx_range(struct __sk_buff, priority):
6329 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
6331 case bpf_ctx_range(struct __sk_buff, tstamp):
6332 if (!capable(CAP_SYS_ADMIN))
6341 case bpf_ctx_range(struct __sk_buff, data):
6342 info->reg_type = PTR_TO_PACKET;
6344 case bpf_ctx_range(struct __sk_buff, data_end):
6345 info->reg_type = PTR_TO_PACKET_END;
6349 return bpf_skb_is_valid_access(off, size, type, prog, info);
6352 static bool lwt_is_valid_access(int off, int size,
6353 enum bpf_access_type type,
6354 const struct bpf_prog *prog,
6355 struct bpf_insn_access_aux *info)
6358 case bpf_ctx_range(struct __sk_buff, tc_classid):
6359 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
6360 case bpf_ctx_range(struct __sk_buff, data_meta):
6361 case bpf_ctx_range_ptr(struct __sk_buff, flow_keys):
6362 case bpf_ctx_range(struct __sk_buff, tstamp):
6363 case bpf_ctx_range(struct __sk_buff, wire_len):
6367 if (type == BPF_WRITE) {
6369 case bpf_ctx_range(struct __sk_buff, mark):
6370 case bpf_ctx_range(struct __sk_buff, priority):
6371 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
6379 case bpf_ctx_range(struct __sk_buff, data):
6380 info->reg_type = PTR_TO_PACKET;
6382 case bpf_ctx_range(struct __sk_buff, data_end):
6383 info->reg_type = PTR_TO_PACKET_END;
6387 return bpf_skb_is_valid_access(off, size, type, prog, info);
6390 /* Attach type specific accesses */
6391 static bool __sock_filter_check_attach_type(int off,
6392 enum bpf_access_type access_type,
6393 enum bpf_attach_type attach_type)
6396 case offsetof(struct bpf_sock, bound_dev_if):
6397 case offsetof(struct bpf_sock, mark):
6398 case offsetof(struct bpf_sock, priority):
6399 switch (attach_type) {
6400 case BPF_CGROUP_INET_SOCK_CREATE:
6405 case bpf_ctx_range(struct bpf_sock, src_ip4):
6406 switch (attach_type) {
6407 case BPF_CGROUP_INET4_POST_BIND:
6412 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
6413 switch (attach_type) {
6414 case BPF_CGROUP_INET6_POST_BIND:
6419 case bpf_ctx_range(struct bpf_sock, src_port):
6420 switch (attach_type) {
6421 case BPF_CGROUP_INET4_POST_BIND:
6422 case BPF_CGROUP_INET6_POST_BIND:
6429 return access_type == BPF_READ;
6434 bool bpf_sock_common_is_valid_access(int off, int size,
6435 enum bpf_access_type type,
6436 struct bpf_insn_access_aux *info)
6439 case bpf_ctx_range_till(struct bpf_sock, type, priority):
6442 return bpf_sock_is_valid_access(off, size, type, info);
6446 bool bpf_sock_is_valid_access(int off, int size, enum bpf_access_type type,
6447 struct bpf_insn_access_aux *info)
6449 const int size_default = sizeof(__u32);
6451 if (off < 0 || off >= sizeof(struct bpf_sock))
6453 if (off % size != 0)
6457 case offsetof(struct bpf_sock, state):
6458 case offsetof(struct bpf_sock, family):
6459 case offsetof(struct bpf_sock, type):
6460 case offsetof(struct bpf_sock, protocol):
6461 case offsetof(struct bpf_sock, dst_port):
6462 case offsetof(struct bpf_sock, src_port):
6463 case bpf_ctx_range(struct bpf_sock, src_ip4):
6464 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
6465 case bpf_ctx_range(struct bpf_sock, dst_ip4):
6466 case bpf_ctx_range_till(struct bpf_sock, dst_ip6[0], dst_ip6[3]):
6467 bpf_ctx_record_field_size(info, size_default);
6468 return bpf_ctx_narrow_access_ok(off, size, size_default);
6471 return size == size_default;
6474 static bool sock_filter_is_valid_access(int off, int size,
6475 enum bpf_access_type type,
6476 const struct bpf_prog *prog,
6477 struct bpf_insn_access_aux *info)
6479 if (!bpf_sock_is_valid_access(off, size, type, info))
6481 return __sock_filter_check_attach_type(off, type,
6482 prog->expected_attach_type);
6485 static int bpf_noop_prologue(struct bpf_insn *insn_buf, bool direct_write,
6486 const struct bpf_prog *prog)
6488 /* Neither direct read nor direct write requires any preliminary
6494 static int bpf_unclone_prologue(struct bpf_insn *insn_buf, bool direct_write,
6495 const struct bpf_prog *prog, int drop_verdict)
6497 struct bpf_insn *insn = insn_buf;
6502 /* if (!skb->cloned)
6505 * (Fast-path, otherwise approximation that we might be
6506 * a clone, do the rest in helper.)
6508 *insn++ = BPF_LDX_MEM(BPF_B, BPF_REG_6, BPF_REG_1, CLONED_OFFSET());
6509 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_6, CLONED_MASK);
6510 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_6, 0, 7);
6512 /* ret = bpf_skb_pull_data(skb, 0); */
6513 *insn++ = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1);
6514 *insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_2, BPF_REG_2);
6515 *insn++ = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
6516 BPF_FUNC_skb_pull_data);
6519 * return TC_ACT_SHOT;
6521 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2);
6522 *insn++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_0, drop_verdict);
6523 *insn++ = BPF_EXIT_INSN();
6526 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_6);
6528 *insn++ = prog->insnsi[0];
6530 return insn - insn_buf;
6533 static int bpf_gen_ld_abs(const struct bpf_insn *orig,
6534 struct bpf_insn *insn_buf)
6536 bool indirect = BPF_MODE(orig->code) == BPF_IND;
6537 struct bpf_insn *insn = insn_buf;
6539 /* We're guaranteed here that CTX is in R6. */
6540 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_CTX);
6542 *insn++ = BPF_MOV64_IMM(BPF_REG_2, orig->imm);
6544 *insn++ = BPF_MOV64_REG(BPF_REG_2, orig->src_reg);
6546 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, orig->imm);
6549 switch (BPF_SIZE(orig->code)) {
6551 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_8_no_cache);
6554 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_16_no_cache);
6557 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_32_no_cache);
6561 *insn++ = BPF_JMP_IMM(BPF_JSGE, BPF_REG_0, 0, 2);
6562 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_0, BPF_REG_0);
6563 *insn++ = BPF_EXIT_INSN();
6565 return insn - insn_buf;
6568 static int tc_cls_act_prologue(struct bpf_insn *insn_buf, bool direct_write,
6569 const struct bpf_prog *prog)
6571 return bpf_unclone_prologue(insn_buf, direct_write, prog, TC_ACT_SHOT);
6574 static bool tc_cls_act_is_valid_access(int off, int size,
6575 enum bpf_access_type type,
6576 const struct bpf_prog *prog,
6577 struct bpf_insn_access_aux *info)
6579 if (type == BPF_WRITE) {
6581 case bpf_ctx_range(struct __sk_buff, mark):
6582 case bpf_ctx_range(struct __sk_buff, tc_index):
6583 case bpf_ctx_range(struct __sk_buff, priority):
6584 case bpf_ctx_range(struct __sk_buff, tc_classid):
6585 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
6586 case bpf_ctx_range(struct __sk_buff, tstamp):
6587 case bpf_ctx_range(struct __sk_buff, queue_mapping):
6595 case bpf_ctx_range(struct __sk_buff, data):
6596 info->reg_type = PTR_TO_PACKET;
6598 case bpf_ctx_range(struct __sk_buff, data_meta):
6599 info->reg_type = PTR_TO_PACKET_META;
6601 case bpf_ctx_range(struct __sk_buff, data_end):
6602 info->reg_type = PTR_TO_PACKET_END;
6604 case bpf_ctx_range_ptr(struct __sk_buff, flow_keys):
6605 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
6609 return bpf_skb_is_valid_access(off, size, type, prog, info);
6612 static bool __is_valid_xdp_access(int off, int size)
6614 if (off < 0 || off >= sizeof(struct xdp_md))
6616 if (off % size != 0)
6618 if (size != sizeof(__u32))
6624 static bool xdp_is_valid_access(int off, int size,
6625 enum bpf_access_type type,
6626 const struct bpf_prog *prog,
6627 struct bpf_insn_access_aux *info)
6629 if (type == BPF_WRITE) {
6630 if (bpf_prog_is_dev_bound(prog->aux)) {
6632 case offsetof(struct xdp_md, rx_queue_index):
6633 return __is_valid_xdp_access(off, size);
6640 case offsetof(struct xdp_md, data):
6641 info->reg_type = PTR_TO_PACKET;
6643 case offsetof(struct xdp_md, data_meta):
6644 info->reg_type = PTR_TO_PACKET_META;
6646 case offsetof(struct xdp_md, data_end):
6647 info->reg_type = PTR_TO_PACKET_END;
6651 return __is_valid_xdp_access(off, size);
6654 void bpf_warn_invalid_xdp_action(u32 act)
6656 const u32 act_max = XDP_REDIRECT;
6658 WARN_ONCE(1, "%s XDP return value %u, expect packet loss!\n",
6659 act > act_max ? "Illegal" : "Driver unsupported",
6662 EXPORT_SYMBOL_GPL(bpf_warn_invalid_xdp_action);
6664 static bool sock_addr_is_valid_access(int off, int size,
6665 enum bpf_access_type type,
6666 const struct bpf_prog *prog,
6667 struct bpf_insn_access_aux *info)
6669 const int size_default = sizeof(__u32);
6671 if (off < 0 || off >= sizeof(struct bpf_sock_addr))
6673 if (off % size != 0)
6676 /* Disallow access to IPv6 fields from IPv4 contex and vise
6680 case bpf_ctx_range(struct bpf_sock_addr, user_ip4):
6681 switch (prog->expected_attach_type) {
6682 case BPF_CGROUP_INET4_BIND:
6683 case BPF_CGROUP_INET4_CONNECT:
6684 case BPF_CGROUP_UDP4_SENDMSG:
6690 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
6691 switch (prog->expected_attach_type) {
6692 case BPF_CGROUP_INET6_BIND:
6693 case BPF_CGROUP_INET6_CONNECT:
6694 case BPF_CGROUP_UDP6_SENDMSG:
6700 case bpf_ctx_range(struct bpf_sock_addr, msg_src_ip4):
6701 switch (prog->expected_attach_type) {
6702 case BPF_CGROUP_UDP4_SENDMSG:
6708 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
6710 switch (prog->expected_attach_type) {
6711 case BPF_CGROUP_UDP6_SENDMSG:
6720 case bpf_ctx_range(struct bpf_sock_addr, user_ip4):
6721 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
6722 case bpf_ctx_range(struct bpf_sock_addr, msg_src_ip4):
6723 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
6725 /* Only narrow read access allowed for now. */
6726 if (type == BPF_READ) {
6727 bpf_ctx_record_field_size(info, size_default);
6728 if (!bpf_ctx_narrow_access_ok(off, size, size_default))
6731 if (size != size_default)
6735 case bpf_ctx_range(struct bpf_sock_addr, user_port):
6736 if (size != size_default)
6740 if (type == BPF_READ) {
6741 if (size != size_default)
6751 static bool sock_ops_is_valid_access(int off, int size,
6752 enum bpf_access_type type,
6753 const struct bpf_prog *prog,
6754 struct bpf_insn_access_aux *info)
6756 const int size_default = sizeof(__u32);
6758 if (off < 0 || off >= sizeof(struct bpf_sock_ops))
6761 /* The verifier guarantees that size > 0. */
6762 if (off % size != 0)
6765 if (type == BPF_WRITE) {
6767 case offsetof(struct bpf_sock_ops, reply):
6768 case offsetof(struct bpf_sock_ops, sk_txhash):
6769 if (size != size_default)
6777 case bpf_ctx_range_till(struct bpf_sock_ops, bytes_received,
6779 if (size != sizeof(__u64))
6783 if (size != size_default)
6792 static int sk_skb_prologue(struct bpf_insn *insn_buf, bool direct_write,
6793 const struct bpf_prog *prog)
6795 return bpf_unclone_prologue(insn_buf, direct_write, prog, SK_DROP);
6798 static bool sk_skb_is_valid_access(int off, int size,
6799 enum bpf_access_type type,
6800 const struct bpf_prog *prog,
6801 struct bpf_insn_access_aux *info)
6804 case bpf_ctx_range(struct __sk_buff, tc_classid):
6805 case bpf_ctx_range(struct __sk_buff, data_meta):
6806 case bpf_ctx_range_ptr(struct __sk_buff, flow_keys):
6807 case bpf_ctx_range(struct __sk_buff, tstamp):
6808 case bpf_ctx_range(struct __sk_buff, wire_len):
6812 if (type == BPF_WRITE) {
6814 case bpf_ctx_range(struct __sk_buff, tc_index):
6815 case bpf_ctx_range(struct __sk_buff, priority):
6823 case bpf_ctx_range(struct __sk_buff, mark):
6825 case bpf_ctx_range(struct __sk_buff, data):
6826 info->reg_type = PTR_TO_PACKET;
6828 case bpf_ctx_range(struct __sk_buff, data_end):
6829 info->reg_type = PTR_TO_PACKET_END;
6833 return bpf_skb_is_valid_access(off, size, type, prog, info);
6836 static bool sk_msg_is_valid_access(int off, int size,
6837 enum bpf_access_type type,
6838 const struct bpf_prog *prog,
6839 struct bpf_insn_access_aux *info)
6841 if (type == BPF_WRITE)
6844 if (off % size != 0)
6848 case offsetof(struct sk_msg_md, data):
6849 info->reg_type = PTR_TO_PACKET;
6850 if (size != sizeof(__u64))
6853 case offsetof(struct sk_msg_md, data_end):
6854 info->reg_type = PTR_TO_PACKET_END;
6855 if (size != sizeof(__u64))
6858 case bpf_ctx_range(struct sk_msg_md, family):
6859 case bpf_ctx_range(struct sk_msg_md, remote_ip4):
6860 case bpf_ctx_range(struct sk_msg_md, local_ip4):
6861 case bpf_ctx_range_till(struct sk_msg_md, remote_ip6[0], remote_ip6[3]):
6862 case bpf_ctx_range_till(struct sk_msg_md, local_ip6[0], local_ip6[3]):
6863 case bpf_ctx_range(struct sk_msg_md, remote_port):
6864 case bpf_ctx_range(struct sk_msg_md, local_port):
6865 case bpf_ctx_range(struct sk_msg_md, size):
6866 if (size != sizeof(__u32))
6875 static bool flow_dissector_is_valid_access(int off, int size,
6876 enum bpf_access_type type,
6877 const struct bpf_prog *prog,
6878 struct bpf_insn_access_aux *info)
6880 if (type == BPF_WRITE)
6884 case bpf_ctx_range(struct __sk_buff, data):
6885 info->reg_type = PTR_TO_PACKET;
6887 case bpf_ctx_range(struct __sk_buff, data_end):
6888 info->reg_type = PTR_TO_PACKET_END;
6890 case bpf_ctx_range_ptr(struct __sk_buff, flow_keys):
6891 info->reg_type = PTR_TO_FLOW_KEYS;
6897 return bpf_skb_is_valid_access(off, size, type, prog, info);
6900 static u32 bpf_convert_ctx_access(enum bpf_access_type type,
6901 const struct bpf_insn *si,
6902 struct bpf_insn *insn_buf,
6903 struct bpf_prog *prog, u32 *target_size)
6905 struct bpf_insn *insn = insn_buf;
6909 case offsetof(struct __sk_buff, len):
6910 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6911 bpf_target_off(struct sk_buff, len, 4,
6915 case offsetof(struct __sk_buff, protocol):
6916 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
6917 bpf_target_off(struct sk_buff, protocol, 2,
6921 case offsetof(struct __sk_buff, vlan_proto):
6922 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
6923 bpf_target_off(struct sk_buff, vlan_proto, 2,
6927 case offsetof(struct __sk_buff, priority):
6928 if (type == BPF_WRITE)
6929 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
6930 bpf_target_off(struct sk_buff, priority, 4,
6933 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6934 bpf_target_off(struct sk_buff, priority, 4,
6938 case offsetof(struct __sk_buff, ingress_ifindex):
6939 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6940 bpf_target_off(struct sk_buff, skb_iif, 4,
6944 case offsetof(struct __sk_buff, ifindex):
6945 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
6946 si->dst_reg, si->src_reg,
6947 offsetof(struct sk_buff, dev));
6948 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
6949 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6950 bpf_target_off(struct net_device, ifindex, 4,
6954 case offsetof(struct __sk_buff, hash):
6955 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6956 bpf_target_off(struct sk_buff, hash, 4,
6960 case offsetof(struct __sk_buff, mark):
6961 if (type == BPF_WRITE)
6962 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
6963 bpf_target_off(struct sk_buff, mark, 4,
6966 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6967 bpf_target_off(struct sk_buff, mark, 4,
6971 case offsetof(struct __sk_buff, pkt_type):
6973 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->src_reg,
6975 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, PKT_TYPE_MAX);
6976 #ifdef __BIG_ENDIAN_BITFIELD
6977 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, 5);
6981 case offsetof(struct __sk_buff, queue_mapping):
6982 if (type == BPF_WRITE) {
6983 *insn++ = BPF_JMP_IMM(BPF_JGE, si->src_reg, NO_QUEUE_MAPPING, 1);
6984 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg, si->src_reg,
6985 bpf_target_off(struct sk_buff,
6989 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
6990 bpf_target_off(struct sk_buff,
6996 case offsetof(struct __sk_buff, vlan_present):
6998 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->src_reg,
6999 PKT_VLAN_PRESENT_OFFSET());
7000 if (PKT_VLAN_PRESENT_BIT)
7001 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, PKT_VLAN_PRESENT_BIT);
7002 if (PKT_VLAN_PRESENT_BIT < 7)
7003 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, 1);
7006 case offsetof(struct __sk_buff, vlan_tci):
7007 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
7008 bpf_target_off(struct sk_buff, vlan_tci, 2,
7012 case offsetof(struct __sk_buff, cb[0]) ...
7013 offsetofend(struct __sk_buff, cb[4]) - 1:
7014 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, data) < 20);
7015 BUILD_BUG_ON((offsetof(struct sk_buff, cb) +
7016 offsetof(struct qdisc_skb_cb, data)) %
7019 prog->cb_access = 1;
7021 off -= offsetof(struct __sk_buff, cb[0]);
7022 off += offsetof(struct sk_buff, cb);
7023 off += offsetof(struct qdisc_skb_cb, data);
7024 if (type == BPF_WRITE)
7025 *insn++ = BPF_STX_MEM(BPF_SIZE(si->code), si->dst_reg,
7028 *insn++ = BPF_LDX_MEM(BPF_SIZE(si->code), si->dst_reg,
7032 case offsetof(struct __sk_buff, tc_classid):
7033 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, tc_classid) != 2);
7036 off -= offsetof(struct __sk_buff, tc_classid);
7037 off += offsetof(struct sk_buff, cb);
7038 off += offsetof(struct qdisc_skb_cb, tc_classid);
7040 if (type == BPF_WRITE)
7041 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg,
7044 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg,
7048 case offsetof(struct __sk_buff, data):
7049 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
7050 si->dst_reg, si->src_reg,
7051 offsetof(struct sk_buff, data));
7054 case offsetof(struct __sk_buff, data_meta):
7056 off -= offsetof(struct __sk_buff, data_meta);
7057 off += offsetof(struct sk_buff, cb);
7058 off += offsetof(struct bpf_skb_data_end, data_meta);
7059 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
7063 case offsetof(struct __sk_buff, data_end):
7065 off -= offsetof(struct __sk_buff, data_end);
7066 off += offsetof(struct sk_buff, cb);
7067 off += offsetof(struct bpf_skb_data_end, data_end);
7068 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
7072 case offsetof(struct __sk_buff, tc_index):
7073 #ifdef CONFIG_NET_SCHED
7074 if (type == BPF_WRITE)
7075 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg, si->src_reg,
7076 bpf_target_off(struct sk_buff, tc_index, 2,
7079 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
7080 bpf_target_off(struct sk_buff, tc_index, 2,
7084 if (type == BPF_WRITE)
7085 *insn++ = BPF_MOV64_REG(si->dst_reg, si->dst_reg);
7087 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
7091 case offsetof(struct __sk_buff, napi_id):
7092 #if defined(CONFIG_NET_RX_BUSY_POLL)
7093 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
7094 bpf_target_off(struct sk_buff, napi_id, 4,
7096 *insn++ = BPF_JMP_IMM(BPF_JGE, si->dst_reg, MIN_NAPI_ID, 1);
7097 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
7100 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
7103 case offsetof(struct __sk_buff, family):
7104 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_family) != 2);
7106 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
7107 si->dst_reg, si->src_reg,
7108 offsetof(struct sk_buff, sk));
7109 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
7110 bpf_target_off(struct sock_common,
7114 case offsetof(struct __sk_buff, remote_ip4):
7115 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_daddr) != 4);
7117 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
7118 si->dst_reg, si->src_reg,
7119 offsetof(struct sk_buff, sk));
7120 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7121 bpf_target_off(struct sock_common,
7125 case offsetof(struct __sk_buff, local_ip4):
7126 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
7127 skc_rcv_saddr) != 4);
7129 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
7130 si->dst_reg, si->src_reg,
7131 offsetof(struct sk_buff, sk));
7132 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7133 bpf_target_off(struct sock_common,
7137 case offsetof(struct __sk_buff, remote_ip6[0]) ...
7138 offsetof(struct __sk_buff, remote_ip6[3]):
7139 #if IS_ENABLED(CONFIG_IPV6)
7140 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
7141 skc_v6_daddr.s6_addr32[0]) != 4);
7144 off -= offsetof(struct __sk_buff, remote_ip6[0]);
7146 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
7147 si->dst_reg, si->src_reg,
7148 offsetof(struct sk_buff, sk));
7149 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7150 offsetof(struct sock_common,
7151 skc_v6_daddr.s6_addr32[0]) +
7154 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
7157 case offsetof(struct __sk_buff, local_ip6[0]) ...
7158 offsetof(struct __sk_buff, local_ip6[3]):
7159 #if IS_ENABLED(CONFIG_IPV6)
7160 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
7161 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
7164 off -= offsetof(struct __sk_buff, local_ip6[0]);
7166 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
7167 si->dst_reg, si->src_reg,
7168 offsetof(struct sk_buff, sk));
7169 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7170 offsetof(struct sock_common,
7171 skc_v6_rcv_saddr.s6_addr32[0]) +
7174 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
7178 case offsetof(struct __sk_buff, remote_port):
7179 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_dport) != 2);
7181 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
7182 si->dst_reg, si->src_reg,
7183 offsetof(struct sk_buff, sk));
7184 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
7185 bpf_target_off(struct sock_common,
7188 #ifndef __BIG_ENDIAN_BITFIELD
7189 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
7193 case offsetof(struct __sk_buff, local_port):
7194 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_num) != 2);
7196 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
7197 si->dst_reg, si->src_reg,
7198 offsetof(struct sk_buff, sk));
7199 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
7200 bpf_target_off(struct sock_common,
7201 skc_num, 2, target_size));
7204 case offsetof(struct __sk_buff, flow_keys):
7206 off -= offsetof(struct __sk_buff, flow_keys);
7207 off += offsetof(struct sk_buff, cb);
7208 off += offsetof(struct qdisc_skb_cb, flow_keys);
7209 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
7213 case offsetof(struct __sk_buff, tstamp):
7214 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, tstamp) != 8);
7216 if (type == BPF_WRITE)
7217 *insn++ = BPF_STX_MEM(BPF_DW,
7218 si->dst_reg, si->src_reg,
7219 bpf_target_off(struct sk_buff,
7223 *insn++ = BPF_LDX_MEM(BPF_DW,
7224 si->dst_reg, si->src_reg,
7225 bpf_target_off(struct sk_buff,
7230 case offsetof(struct __sk_buff, gso_segs):
7231 /* si->dst_reg = skb_shinfo(SKB); */
7232 #ifdef NET_SKBUFF_DATA_USES_OFFSET
7233 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, head),
7234 si->dst_reg, si->src_reg,
7235 offsetof(struct sk_buff, head));
7236 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, end),
7237 BPF_REG_AX, si->src_reg,
7238 offsetof(struct sk_buff, end));
7239 *insn++ = BPF_ALU64_REG(BPF_ADD, si->dst_reg, BPF_REG_AX);
7241 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, end),
7242 si->dst_reg, si->src_reg,
7243 offsetof(struct sk_buff, end));
7245 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct skb_shared_info, gso_segs),
7246 si->dst_reg, si->dst_reg,
7247 bpf_target_off(struct skb_shared_info,
7251 case offsetof(struct __sk_buff, wire_len):
7252 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, pkt_len) != 4);
7255 off -= offsetof(struct __sk_buff, wire_len);
7256 off += offsetof(struct sk_buff, cb);
7257 off += offsetof(struct qdisc_skb_cb, pkt_len);
7259 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, off);
7262 case offsetof(struct __sk_buff, sk):
7263 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
7264 si->dst_reg, si->src_reg,
7265 offsetof(struct sk_buff, sk));
7269 return insn - insn_buf;
7272 u32 bpf_sock_convert_ctx_access(enum bpf_access_type type,
7273 const struct bpf_insn *si,
7274 struct bpf_insn *insn_buf,
7275 struct bpf_prog *prog, u32 *target_size)
7277 struct bpf_insn *insn = insn_buf;
7281 case offsetof(struct bpf_sock, bound_dev_if):
7282 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_bound_dev_if) != 4);
7284 if (type == BPF_WRITE)
7285 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
7286 offsetof(struct sock, sk_bound_dev_if));
7288 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
7289 offsetof(struct sock, sk_bound_dev_if));
7292 case offsetof(struct bpf_sock, mark):
7293 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_mark) != 4);
7295 if (type == BPF_WRITE)
7296 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
7297 offsetof(struct sock, sk_mark));
7299 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
7300 offsetof(struct sock, sk_mark));
7303 case offsetof(struct bpf_sock, priority):
7304 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_priority) != 4);
7306 if (type == BPF_WRITE)
7307 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
7308 offsetof(struct sock, sk_priority));
7310 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
7311 offsetof(struct sock, sk_priority));
7314 case offsetof(struct bpf_sock, family):
7315 *insn++ = BPF_LDX_MEM(
7316 BPF_FIELD_SIZEOF(struct sock_common, skc_family),
7317 si->dst_reg, si->src_reg,
7318 bpf_target_off(struct sock_common,
7320 FIELD_SIZEOF(struct sock_common,
7325 case offsetof(struct bpf_sock, type):
7326 BUILD_BUG_ON(HWEIGHT32(SK_FL_TYPE_MASK) != BITS_PER_BYTE * 2);
7327 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
7328 offsetof(struct sock, __sk_flags_offset));
7329 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_TYPE_MASK);
7330 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, SK_FL_TYPE_SHIFT);
7334 case offsetof(struct bpf_sock, protocol):
7335 BUILD_BUG_ON(HWEIGHT32(SK_FL_PROTO_MASK) != BITS_PER_BYTE);
7336 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
7337 offsetof(struct sock, __sk_flags_offset));
7338 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_PROTO_MASK);
7339 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, SK_FL_PROTO_SHIFT);
7343 case offsetof(struct bpf_sock, src_ip4):
7344 *insn++ = BPF_LDX_MEM(
7345 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
7346 bpf_target_off(struct sock_common, skc_rcv_saddr,
7347 FIELD_SIZEOF(struct sock_common,
7352 case offsetof(struct bpf_sock, dst_ip4):
7353 *insn++ = BPF_LDX_MEM(
7354 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
7355 bpf_target_off(struct sock_common, skc_daddr,
7356 FIELD_SIZEOF(struct sock_common,
7361 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
7362 #if IS_ENABLED(CONFIG_IPV6)
7364 off -= offsetof(struct bpf_sock, src_ip6[0]);
7365 *insn++ = BPF_LDX_MEM(
7366 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
7369 skc_v6_rcv_saddr.s6_addr32[0],
7370 FIELD_SIZEOF(struct sock_common,
7371 skc_v6_rcv_saddr.s6_addr32[0]),
7372 target_size) + off);
7375 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
7379 case bpf_ctx_range_till(struct bpf_sock, dst_ip6[0], dst_ip6[3]):
7380 #if IS_ENABLED(CONFIG_IPV6)
7382 off -= offsetof(struct bpf_sock, dst_ip6[0]);
7383 *insn++ = BPF_LDX_MEM(
7384 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
7385 bpf_target_off(struct sock_common,
7386 skc_v6_daddr.s6_addr32[0],
7387 FIELD_SIZEOF(struct sock_common,
7388 skc_v6_daddr.s6_addr32[0]),
7389 target_size) + off);
7391 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
7396 case offsetof(struct bpf_sock, src_port):
7397 *insn++ = BPF_LDX_MEM(
7398 BPF_FIELD_SIZEOF(struct sock_common, skc_num),
7399 si->dst_reg, si->src_reg,
7400 bpf_target_off(struct sock_common, skc_num,
7401 FIELD_SIZEOF(struct sock_common,
7406 case offsetof(struct bpf_sock, dst_port):
7407 *insn++ = BPF_LDX_MEM(
7408 BPF_FIELD_SIZEOF(struct sock_common, skc_dport),
7409 si->dst_reg, si->src_reg,
7410 bpf_target_off(struct sock_common, skc_dport,
7411 FIELD_SIZEOF(struct sock_common,
7416 case offsetof(struct bpf_sock, state):
7417 *insn++ = BPF_LDX_MEM(
7418 BPF_FIELD_SIZEOF(struct sock_common, skc_state),
7419 si->dst_reg, si->src_reg,
7420 bpf_target_off(struct sock_common, skc_state,
7421 FIELD_SIZEOF(struct sock_common,
7427 return insn - insn_buf;
7430 static u32 tc_cls_act_convert_ctx_access(enum bpf_access_type type,
7431 const struct bpf_insn *si,
7432 struct bpf_insn *insn_buf,
7433 struct bpf_prog *prog, u32 *target_size)
7435 struct bpf_insn *insn = insn_buf;
7438 case offsetof(struct __sk_buff, ifindex):
7439 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
7440 si->dst_reg, si->src_reg,
7441 offsetof(struct sk_buff, dev));
7442 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7443 bpf_target_off(struct net_device, ifindex, 4,
7447 return bpf_convert_ctx_access(type, si, insn_buf, prog,
7451 return insn - insn_buf;
7454 static u32 xdp_convert_ctx_access(enum bpf_access_type type,
7455 const struct bpf_insn *si,
7456 struct bpf_insn *insn_buf,
7457 struct bpf_prog *prog, u32 *target_size)
7459 struct bpf_insn *insn = insn_buf;
7462 case offsetof(struct xdp_md, data):
7463 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data),
7464 si->dst_reg, si->src_reg,
7465 offsetof(struct xdp_buff, data));
7467 case offsetof(struct xdp_md, data_meta):
7468 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_meta),
7469 si->dst_reg, si->src_reg,
7470 offsetof(struct xdp_buff, data_meta));
7472 case offsetof(struct xdp_md, data_end):
7473 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_end),
7474 si->dst_reg, si->src_reg,
7475 offsetof(struct xdp_buff, data_end));
7477 case offsetof(struct xdp_md, ingress_ifindex):
7478 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, rxq),
7479 si->dst_reg, si->src_reg,
7480 offsetof(struct xdp_buff, rxq));
7481 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_rxq_info, dev),
7482 si->dst_reg, si->dst_reg,
7483 offsetof(struct xdp_rxq_info, dev));
7484 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7485 offsetof(struct net_device, ifindex));
7487 case offsetof(struct xdp_md, rx_queue_index):
7488 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, rxq),
7489 si->dst_reg, si->src_reg,
7490 offsetof(struct xdp_buff, rxq));
7491 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7492 offsetof(struct xdp_rxq_info,
7497 return insn - insn_buf;
7500 /* SOCK_ADDR_LOAD_NESTED_FIELD() loads Nested Field S.F.NF where S is type of
7501 * context Structure, F is Field in context structure that contains a pointer
7502 * to Nested Structure of type NS that has the field NF.
7504 * SIZE encodes the load size (BPF_B, BPF_H, etc). It's up to caller to make
7505 * sure that SIZE is not greater than actual size of S.F.NF.
7507 * If offset OFF is provided, the load happens from that offset relative to
7510 #define SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, SIZE, OFF) \
7512 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), si->dst_reg, \
7513 si->src_reg, offsetof(S, F)); \
7514 *insn++ = BPF_LDX_MEM( \
7515 SIZE, si->dst_reg, si->dst_reg, \
7516 bpf_target_off(NS, NF, FIELD_SIZEOF(NS, NF), \
7521 #define SOCK_ADDR_LOAD_NESTED_FIELD(S, NS, F, NF) \
7522 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, \
7523 BPF_FIELD_SIZEOF(NS, NF), 0)
7525 /* SOCK_ADDR_STORE_NESTED_FIELD_OFF() has semantic similar to
7526 * SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF() but for store operation.
7528 * It doesn't support SIZE argument though since narrow stores are not
7529 * supported for now.
7531 * In addition it uses Temporary Field TF (member of struct S) as the 3rd
7532 * "register" since two registers available in convert_ctx_access are not
7533 * enough: we can't override neither SRC, since it contains value to store, nor
7534 * DST since it contains pointer to context that may be used by later
7535 * instructions. But we need a temporary place to save pointer to nested
7536 * structure whose field we want to store to.
7538 #define SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, OFF, TF) \
7540 int tmp_reg = BPF_REG_9; \
7541 if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg) \
7543 if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg) \
7545 *insn++ = BPF_STX_MEM(BPF_DW, si->dst_reg, tmp_reg, \
7547 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), tmp_reg, \
7548 si->dst_reg, offsetof(S, F)); \
7549 *insn++ = BPF_STX_MEM( \
7550 BPF_FIELD_SIZEOF(NS, NF), tmp_reg, si->src_reg, \
7551 bpf_target_off(NS, NF, FIELD_SIZEOF(NS, NF), \
7554 *insn++ = BPF_LDX_MEM(BPF_DW, tmp_reg, si->dst_reg, \
7558 #define SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, SIZE, OFF, \
7561 if (type == BPF_WRITE) { \
7562 SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, OFF, \
7565 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF( \
7566 S, NS, F, NF, SIZE, OFF); \
7570 #define SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD(S, NS, F, NF, TF) \
7571 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF( \
7572 S, NS, F, NF, BPF_FIELD_SIZEOF(NS, NF), 0, TF)
7574 static u32 sock_addr_convert_ctx_access(enum bpf_access_type type,
7575 const struct bpf_insn *si,
7576 struct bpf_insn *insn_buf,
7577 struct bpf_prog *prog, u32 *target_size)
7579 struct bpf_insn *insn = insn_buf;
7583 case offsetof(struct bpf_sock_addr, user_family):
7584 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
7585 struct sockaddr, uaddr, sa_family);
7588 case offsetof(struct bpf_sock_addr, user_ip4):
7589 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
7590 struct bpf_sock_addr_kern, struct sockaddr_in, uaddr,
7591 sin_addr, BPF_SIZE(si->code), 0, tmp_reg);
7594 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
7596 off -= offsetof(struct bpf_sock_addr, user_ip6[0]);
7597 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
7598 struct bpf_sock_addr_kern, struct sockaddr_in6, uaddr,
7599 sin6_addr.s6_addr32[0], BPF_SIZE(si->code), off,
7603 case offsetof(struct bpf_sock_addr, user_port):
7604 /* To get port we need to know sa_family first and then treat
7605 * sockaddr as either sockaddr_in or sockaddr_in6.
7606 * Though we can simplify since port field has same offset and
7607 * size in both structures.
7608 * Here we check this invariant and use just one of the
7609 * structures if it's true.
7611 BUILD_BUG_ON(offsetof(struct sockaddr_in, sin_port) !=
7612 offsetof(struct sockaddr_in6, sin6_port));
7613 BUILD_BUG_ON(FIELD_SIZEOF(struct sockaddr_in, sin_port) !=
7614 FIELD_SIZEOF(struct sockaddr_in6, sin6_port));
7615 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD(struct bpf_sock_addr_kern,
7616 struct sockaddr_in6, uaddr,
7617 sin6_port, tmp_reg);
7620 case offsetof(struct bpf_sock_addr, family):
7621 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
7622 struct sock, sk, sk_family);
7625 case offsetof(struct bpf_sock_addr, type):
7626 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(
7627 struct bpf_sock_addr_kern, struct sock, sk,
7628 __sk_flags_offset, BPF_W, 0);
7629 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_TYPE_MASK);
7630 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, SK_FL_TYPE_SHIFT);
7633 case offsetof(struct bpf_sock_addr, protocol):
7634 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(
7635 struct bpf_sock_addr_kern, struct sock, sk,
7636 __sk_flags_offset, BPF_W, 0);
7637 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_PROTO_MASK);
7638 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg,
7642 case offsetof(struct bpf_sock_addr, msg_src_ip4):
7643 /* Treat t_ctx as struct in_addr for msg_src_ip4. */
7644 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
7645 struct bpf_sock_addr_kern, struct in_addr, t_ctx,
7646 s_addr, BPF_SIZE(si->code), 0, tmp_reg);
7649 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
7652 off -= offsetof(struct bpf_sock_addr, msg_src_ip6[0]);
7653 /* Treat t_ctx as struct in6_addr for msg_src_ip6. */
7654 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
7655 struct bpf_sock_addr_kern, struct in6_addr, t_ctx,
7656 s6_addr32[0], BPF_SIZE(si->code), off, tmp_reg);
7660 return insn - insn_buf;
7663 static u32 sock_ops_convert_ctx_access(enum bpf_access_type type,
7664 const struct bpf_insn *si,
7665 struct bpf_insn *insn_buf,
7666 struct bpf_prog *prog,
7669 struct bpf_insn *insn = insn_buf;
7672 /* Helper macro for adding read access to tcp_sock or sock fields. */
7673 #define SOCK_OPS_GET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ) \
7675 BUILD_BUG_ON(FIELD_SIZEOF(OBJ, OBJ_FIELD) > \
7676 FIELD_SIZEOF(struct bpf_sock_ops, BPF_FIELD)); \
7677 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
7678 struct bpf_sock_ops_kern, \
7680 si->dst_reg, si->src_reg, \
7681 offsetof(struct bpf_sock_ops_kern, \
7683 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 2); \
7684 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
7685 struct bpf_sock_ops_kern, sk),\
7686 si->dst_reg, si->src_reg, \
7687 offsetof(struct bpf_sock_ops_kern, sk));\
7688 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(OBJ, \
7690 si->dst_reg, si->dst_reg, \
7691 offsetof(OBJ, OBJ_FIELD)); \
7694 #define SOCK_OPS_GET_TCP_SOCK_FIELD(FIELD) \
7695 SOCK_OPS_GET_FIELD(FIELD, FIELD, struct tcp_sock)
7697 /* Helper macro for adding write access to tcp_sock or sock fields.
7698 * The macro is called with two registers, dst_reg which contains a pointer
7699 * to ctx (context) and src_reg which contains the value that should be
7700 * stored. However, we need an additional register since we cannot overwrite
7701 * dst_reg because it may be used later in the program.
7702 * Instead we "borrow" one of the other register. We first save its value
7703 * into a new (temp) field in bpf_sock_ops_kern, use it, and then restore
7704 * it at the end of the macro.
7706 #define SOCK_OPS_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ) \
7708 int reg = BPF_REG_9; \
7709 BUILD_BUG_ON(FIELD_SIZEOF(OBJ, OBJ_FIELD) > \
7710 FIELD_SIZEOF(struct bpf_sock_ops, BPF_FIELD)); \
7711 if (si->dst_reg == reg || si->src_reg == reg) \
7713 if (si->dst_reg == reg || si->src_reg == reg) \
7715 *insn++ = BPF_STX_MEM(BPF_DW, si->dst_reg, reg, \
7716 offsetof(struct bpf_sock_ops_kern, \
7718 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
7719 struct bpf_sock_ops_kern, \
7722 offsetof(struct bpf_sock_ops_kern, \
7724 *insn++ = BPF_JMP_IMM(BPF_JEQ, reg, 0, 2); \
7725 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
7726 struct bpf_sock_ops_kern, sk),\
7728 offsetof(struct bpf_sock_ops_kern, sk));\
7729 *insn++ = BPF_STX_MEM(BPF_FIELD_SIZEOF(OBJ, OBJ_FIELD), \
7731 offsetof(OBJ, OBJ_FIELD)); \
7732 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->dst_reg, \
7733 offsetof(struct bpf_sock_ops_kern, \
7737 #define SOCK_OPS_GET_OR_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ, TYPE) \
7739 if (TYPE == BPF_WRITE) \
7740 SOCK_OPS_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ); \
7742 SOCK_OPS_GET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ); \
7745 CONVERT_COMMON_TCP_SOCK_FIELDS(struct bpf_sock_ops,
7746 SOCK_OPS_GET_TCP_SOCK_FIELD);
7748 if (insn > insn_buf)
7749 return insn - insn_buf;
7752 case offsetof(struct bpf_sock_ops, op) ...
7753 offsetof(struct bpf_sock_ops, replylong[3]):
7754 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, op) !=
7755 FIELD_SIZEOF(struct bpf_sock_ops_kern, op));
7756 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, reply) !=
7757 FIELD_SIZEOF(struct bpf_sock_ops_kern, reply));
7758 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, replylong) !=
7759 FIELD_SIZEOF(struct bpf_sock_ops_kern, replylong));
7761 off -= offsetof(struct bpf_sock_ops, op);
7762 off += offsetof(struct bpf_sock_ops_kern, op);
7763 if (type == BPF_WRITE)
7764 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
7767 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
7771 case offsetof(struct bpf_sock_ops, family):
7772 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_family) != 2);
7774 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7775 struct bpf_sock_ops_kern, sk),
7776 si->dst_reg, si->src_reg,
7777 offsetof(struct bpf_sock_ops_kern, sk));
7778 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
7779 offsetof(struct sock_common, skc_family));
7782 case offsetof(struct bpf_sock_ops, remote_ip4):
7783 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_daddr) != 4);
7785 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7786 struct bpf_sock_ops_kern, sk),
7787 si->dst_reg, si->src_reg,
7788 offsetof(struct bpf_sock_ops_kern, sk));
7789 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7790 offsetof(struct sock_common, skc_daddr));
7793 case offsetof(struct bpf_sock_ops, local_ip4):
7794 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
7795 skc_rcv_saddr) != 4);
7797 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7798 struct bpf_sock_ops_kern, sk),
7799 si->dst_reg, si->src_reg,
7800 offsetof(struct bpf_sock_ops_kern, sk));
7801 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7802 offsetof(struct sock_common,
7806 case offsetof(struct bpf_sock_ops, remote_ip6[0]) ...
7807 offsetof(struct bpf_sock_ops, remote_ip6[3]):
7808 #if IS_ENABLED(CONFIG_IPV6)
7809 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
7810 skc_v6_daddr.s6_addr32[0]) != 4);
7813 off -= offsetof(struct bpf_sock_ops, remote_ip6[0]);
7814 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7815 struct bpf_sock_ops_kern, sk),
7816 si->dst_reg, si->src_reg,
7817 offsetof(struct bpf_sock_ops_kern, sk));
7818 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7819 offsetof(struct sock_common,
7820 skc_v6_daddr.s6_addr32[0]) +
7823 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
7827 case offsetof(struct bpf_sock_ops, local_ip6[0]) ...
7828 offsetof(struct bpf_sock_ops, local_ip6[3]):
7829 #if IS_ENABLED(CONFIG_IPV6)
7830 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
7831 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
7834 off -= offsetof(struct bpf_sock_ops, local_ip6[0]);
7835 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7836 struct bpf_sock_ops_kern, sk),
7837 si->dst_reg, si->src_reg,
7838 offsetof(struct bpf_sock_ops_kern, sk));
7839 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7840 offsetof(struct sock_common,
7841 skc_v6_rcv_saddr.s6_addr32[0]) +
7844 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
7848 case offsetof(struct bpf_sock_ops, remote_port):
7849 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_dport) != 2);
7851 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7852 struct bpf_sock_ops_kern, sk),
7853 si->dst_reg, si->src_reg,
7854 offsetof(struct bpf_sock_ops_kern, sk));
7855 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
7856 offsetof(struct sock_common, skc_dport));
7857 #ifndef __BIG_ENDIAN_BITFIELD
7858 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
7862 case offsetof(struct bpf_sock_ops, local_port):
7863 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_num) != 2);
7865 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7866 struct bpf_sock_ops_kern, sk),
7867 si->dst_reg, si->src_reg,
7868 offsetof(struct bpf_sock_ops_kern, sk));
7869 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
7870 offsetof(struct sock_common, skc_num));
7873 case offsetof(struct bpf_sock_ops, is_fullsock):
7874 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7875 struct bpf_sock_ops_kern,
7877 si->dst_reg, si->src_reg,
7878 offsetof(struct bpf_sock_ops_kern,
7882 case offsetof(struct bpf_sock_ops, state):
7883 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_state) != 1);
7885 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7886 struct bpf_sock_ops_kern, sk),
7887 si->dst_reg, si->src_reg,
7888 offsetof(struct bpf_sock_ops_kern, sk));
7889 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->dst_reg,
7890 offsetof(struct sock_common, skc_state));
7893 case offsetof(struct bpf_sock_ops, rtt_min):
7894 BUILD_BUG_ON(FIELD_SIZEOF(struct tcp_sock, rtt_min) !=
7895 sizeof(struct minmax));
7896 BUILD_BUG_ON(sizeof(struct minmax) <
7897 sizeof(struct minmax_sample));
7899 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7900 struct bpf_sock_ops_kern, sk),
7901 si->dst_reg, si->src_reg,
7902 offsetof(struct bpf_sock_ops_kern, sk));
7903 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7904 offsetof(struct tcp_sock, rtt_min) +
7905 FIELD_SIZEOF(struct minmax_sample, t));
7908 case offsetof(struct bpf_sock_ops, bpf_sock_ops_cb_flags):
7909 SOCK_OPS_GET_FIELD(bpf_sock_ops_cb_flags, bpf_sock_ops_cb_flags,
7913 case offsetof(struct bpf_sock_ops, sk_txhash):
7914 SOCK_OPS_GET_OR_SET_FIELD(sk_txhash, sk_txhash,
7918 return insn - insn_buf;
7921 static u32 sk_skb_convert_ctx_access(enum bpf_access_type type,
7922 const struct bpf_insn *si,
7923 struct bpf_insn *insn_buf,
7924 struct bpf_prog *prog, u32 *target_size)
7926 struct bpf_insn *insn = insn_buf;
7930 case offsetof(struct __sk_buff, data_end):
7932 off -= offsetof(struct __sk_buff, data_end);
7933 off += offsetof(struct sk_buff, cb);
7934 off += offsetof(struct tcp_skb_cb, bpf.data_end);
7935 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
7939 return bpf_convert_ctx_access(type, si, insn_buf, prog,
7943 return insn - insn_buf;
7946 static u32 sk_msg_convert_ctx_access(enum bpf_access_type type,
7947 const struct bpf_insn *si,
7948 struct bpf_insn *insn_buf,
7949 struct bpf_prog *prog, u32 *target_size)
7951 struct bpf_insn *insn = insn_buf;
7952 #if IS_ENABLED(CONFIG_IPV6)
7956 /* convert ctx uses the fact sg element is first in struct */
7957 BUILD_BUG_ON(offsetof(struct sk_msg, sg) != 0);
7960 case offsetof(struct sk_msg_md, data):
7961 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg, data),
7962 si->dst_reg, si->src_reg,
7963 offsetof(struct sk_msg, data));
7965 case offsetof(struct sk_msg_md, data_end):
7966 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg, data_end),
7967 si->dst_reg, si->src_reg,
7968 offsetof(struct sk_msg, data_end));
7970 case offsetof(struct sk_msg_md, family):
7971 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_family) != 2);
7973 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7975 si->dst_reg, si->src_reg,
7976 offsetof(struct sk_msg, sk));
7977 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
7978 offsetof(struct sock_common, skc_family));
7981 case offsetof(struct sk_msg_md, remote_ip4):
7982 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_daddr) != 4);
7984 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7986 si->dst_reg, si->src_reg,
7987 offsetof(struct sk_msg, sk));
7988 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7989 offsetof(struct sock_common, skc_daddr));
7992 case offsetof(struct sk_msg_md, local_ip4):
7993 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
7994 skc_rcv_saddr) != 4);
7996 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7998 si->dst_reg, si->src_reg,
7999 offsetof(struct sk_msg, sk));
8000 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
8001 offsetof(struct sock_common,
8005 case offsetof(struct sk_msg_md, remote_ip6[0]) ...
8006 offsetof(struct sk_msg_md, remote_ip6[3]):
8007 #if IS_ENABLED(CONFIG_IPV6)
8008 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
8009 skc_v6_daddr.s6_addr32[0]) != 4);
8012 off -= offsetof(struct sk_msg_md, remote_ip6[0]);
8013 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8015 si->dst_reg, si->src_reg,
8016 offsetof(struct sk_msg, sk));
8017 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
8018 offsetof(struct sock_common,
8019 skc_v6_daddr.s6_addr32[0]) +
8022 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
8026 case offsetof(struct sk_msg_md, local_ip6[0]) ...
8027 offsetof(struct sk_msg_md, local_ip6[3]):
8028 #if IS_ENABLED(CONFIG_IPV6)
8029 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
8030 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
8033 off -= offsetof(struct sk_msg_md, local_ip6[0]);
8034 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8036 si->dst_reg, si->src_reg,
8037 offsetof(struct sk_msg, sk));
8038 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
8039 offsetof(struct sock_common,
8040 skc_v6_rcv_saddr.s6_addr32[0]) +
8043 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
8047 case offsetof(struct sk_msg_md, remote_port):
8048 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_dport) != 2);
8050 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8052 si->dst_reg, si->src_reg,
8053 offsetof(struct sk_msg, sk));
8054 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
8055 offsetof(struct sock_common, skc_dport));
8056 #ifndef __BIG_ENDIAN_BITFIELD
8057 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
8061 case offsetof(struct sk_msg_md, local_port):
8062 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_num) != 2);
8064 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8066 si->dst_reg, si->src_reg,
8067 offsetof(struct sk_msg, sk));
8068 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
8069 offsetof(struct sock_common, skc_num));
8072 case offsetof(struct sk_msg_md, size):
8073 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg_sg, size),
8074 si->dst_reg, si->src_reg,
8075 offsetof(struct sk_msg_sg, size));
8079 return insn - insn_buf;
8082 const struct bpf_verifier_ops sk_filter_verifier_ops = {
8083 .get_func_proto = sk_filter_func_proto,
8084 .is_valid_access = sk_filter_is_valid_access,
8085 .convert_ctx_access = bpf_convert_ctx_access,
8086 .gen_ld_abs = bpf_gen_ld_abs,
8089 const struct bpf_prog_ops sk_filter_prog_ops = {
8090 .test_run = bpf_prog_test_run_skb,
8093 const struct bpf_verifier_ops tc_cls_act_verifier_ops = {
8094 .get_func_proto = tc_cls_act_func_proto,
8095 .is_valid_access = tc_cls_act_is_valid_access,
8096 .convert_ctx_access = tc_cls_act_convert_ctx_access,
8097 .gen_prologue = tc_cls_act_prologue,
8098 .gen_ld_abs = bpf_gen_ld_abs,
8101 const struct bpf_prog_ops tc_cls_act_prog_ops = {
8102 .test_run = bpf_prog_test_run_skb,
8105 const struct bpf_verifier_ops xdp_verifier_ops = {
8106 .get_func_proto = xdp_func_proto,
8107 .is_valid_access = xdp_is_valid_access,
8108 .convert_ctx_access = xdp_convert_ctx_access,
8109 .gen_prologue = bpf_noop_prologue,
8112 const struct bpf_prog_ops xdp_prog_ops = {
8113 .test_run = bpf_prog_test_run_xdp,
8116 const struct bpf_verifier_ops cg_skb_verifier_ops = {
8117 .get_func_proto = cg_skb_func_proto,
8118 .is_valid_access = cg_skb_is_valid_access,
8119 .convert_ctx_access = bpf_convert_ctx_access,
8122 const struct bpf_prog_ops cg_skb_prog_ops = {
8123 .test_run = bpf_prog_test_run_skb,
8126 const struct bpf_verifier_ops lwt_in_verifier_ops = {
8127 .get_func_proto = lwt_in_func_proto,
8128 .is_valid_access = lwt_is_valid_access,
8129 .convert_ctx_access = bpf_convert_ctx_access,
8132 const struct bpf_prog_ops lwt_in_prog_ops = {
8133 .test_run = bpf_prog_test_run_skb,
8136 const struct bpf_verifier_ops lwt_out_verifier_ops = {
8137 .get_func_proto = lwt_out_func_proto,
8138 .is_valid_access = lwt_is_valid_access,
8139 .convert_ctx_access = bpf_convert_ctx_access,
8142 const struct bpf_prog_ops lwt_out_prog_ops = {
8143 .test_run = bpf_prog_test_run_skb,
8146 const struct bpf_verifier_ops lwt_xmit_verifier_ops = {
8147 .get_func_proto = lwt_xmit_func_proto,
8148 .is_valid_access = lwt_is_valid_access,
8149 .convert_ctx_access = bpf_convert_ctx_access,
8150 .gen_prologue = tc_cls_act_prologue,
8153 const struct bpf_prog_ops lwt_xmit_prog_ops = {
8154 .test_run = bpf_prog_test_run_skb,
8157 const struct bpf_verifier_ops lwt_seg6local_verifier_ops = {
8158 .get_func_proto = lwt_seg6local_func_proto,
8159 .is_valid_access = lwt_is_valid_access,
8160 .convert_ctx_access = bpf_convert_ctx_access,
8163 const struct bpf_prog_ops lwt_seg6local_prog_ops = {
8164 .test_run = bpf_prog_test_run_skb,
8167 const struct bpf_verifier_ops cg_sock_verifier_ops = {
8168 .get_func_proto = sock_filter_func_proto,
8169 .is_valid_access = sock_filter_is_valid_access,
8170 .convert_ctx_access = bpf_sock_convert_ctx_access,
8173 const struct bpf_prog_ops cg_sock_prog_ops = {
8176 const struct bpf_verifier_ops cg_sock_addr_verifier_ops = {
8177 .get_func_proto = sock_addr_func_proto,
8178 .is_valid_access = sock_addr_is_valid_access,
8179 .convert_ctx_access = sock_addr_convert_ctx_access,
8182 const struct bpf_prog_ops cg_sock_addr_prog_ops = {
8185 const struct bpf_verifier_ops sock_ops_verifier_ops = {
8186 .get_func_proto = sock_ops_func_proto,
8187 .is_valid_access = sock_ops_is_valid_access,
8188 .convert_ctx_access = sock_ops_convert_ctx_access,
8191 const struct bpf_prog_ops sock_ops_prog_ops = {
8194 const struct bpf_verifier_ops sk_skb_verifier_ops = {
8195 .get_func_proto = sk_skb_func_proto,
8196 .is_valid_access = sk_skb_is_valid_access,
8197 .convert_ctx_access = sk_skb_convert_ctx_access,
8198 .gen_prologue = sk_skb_prologue,
8201 const struct bpf_prog_ops sk_skb_prog_ops = {
8204 const struct bpf_verifier_ops sk_msg_verifier_ops = {
8205 .get_func_proto = sk_msg_func_proto,
8206 .is_valid_access = sk_msg_is_valid_access,
8207 .convert_ctx_access = sk_msg_convert_ctx_access,
8208 .gen_prologue = bpf_noop_prologue,
8211 const struct bpf_prog_ops sk_msg_prog_ops = {
8214 const struct bpf_verifier_ops flow_dissector_verifier_ops = {
8215 .get_func_proto = flow_dissector_func_proto,
8216 .is_valid_access = flow_dissector_is_valid_access,
8217 .convert_ctx_access = bpf_convert_ctx_access,
8220 const struct bpf_prog_ops flow_dissector_prog_ops = {
8221 .test_run = bpf_prog_test_run_flow_dissector,
8224 int sk_detach_filter(struct sock *sk)
8227 struct sk_filter *filter;
8229 if (sock_flag(sk, SOCK_FILTER_LOCKED))
8232 filter = rcu_dereference_protected(sk->sk_filter,
8233 lockdep_sock_is_held(sk));
8235 RCU_INIT_POINTER(sk->sk_filter, NULL);
8236 sk_filter_uncharge(sk, filter);
8242 EXPORT_SYMBOL_GPL(sk_detach_filter);
8244 int sk_get_filter(struct sock *sk, struct sock_filter __user *ubuf,
8247 struct sock_fprog_kern *fprog;
8248 struct sk_filter *filter;
8252 filter = rcu_dereference_protected(sk->sk_filter,
8253 lockdep_sock_is_held(sk));
8257 /* We're copying the filter that has been originally attached,
8258 * so no conversion/decode needed anymore. eBPF programs that
8259 * have no original program cannot be dumped through this.
8262 fprog = filter->prog->orig_prog;
8268 /* User space only enquires number of filter blocks. */
8272 if (len < fprog->len)
8276 if (copy_to_user(ubuf, fprog->filter, bpf_classic_proglen(fprog)))
8279 /* Instead of bytes, the API requests to return the number
8289 struct sk_reuseport_kern {
8290 struct sk_buff *skb;
8292 struct sock *selected_sk;
8299 static void bpf_init_reuseport_kern(struct sk_reuseport_kern *reuse_kern,
8300 struct sock_reuseport *reuse,
8301 struct sock *sk, struct sk_buff *skb,
8304 reuse_kern->skb = skb;
8305 reuse_kern->sk = sk;
8306 reuse_kern->selected_sk = NULL;
8307 reuse_kern->data_end = skb->data + skb_headlen(skb);
8308 reuse_kern->hash = hash;
8309 reuse_kern->reuseport_id = reuse->reuseport_id;
8310 reuse_kern->bind_inany = reuse->bind_inany;
8313 struct sock *bpf_run_sk_reuseport(struct sock_reuseport *reuse, struct sock *sk,
8314 struct bpf_prog *prog, struct sk_buff *skb,
8317 struct sk_reuseport_kern reuse_kern;
8318 enum sk_action action;
8320 bpf_init_reuseport_kern(&reuse_kern, reuse, sk, skb, hash);
8321 action = BPF_PROG_RUN(prog, &reuse_kern);
8323 if (action == SK_PASS)
8324 return reuse_kern.selected_sk;
8326 return ERR_PTR(-ECONNREFUSED);
8329 BPF_CALL_4(sk_select_reuseport, struct sk_reuseport_kern *, reuse_kern,
8330 struct bpf_map *, map, void *, key, u32, flags)
8332 struct sock_reuseport *reuse;
8333 struct sock *selected_sk;
8335 selected_sk = map->ops->map_lookup_elem(map, key);
8339 reuse = rcu_dereference(selected_sk->sk_reuseport_cb);
8341 /* selected_sk is unhashed (e.g. by close()) after the
8342 * above map_lookup_elem(). Treat selected_sk has already
8343 * been removed from the map.
8347 if (unlikely(reuse->reuseport_id != reuse_kern->reuseport_id)) {
8350 if (unlikely(!reuse_kern->reuseport_id))
8351 /* There is a small race between adding the
8352 * sk to the map and setting the
8353 * reuse_kern->reuseport_id.
8354 * Treat it as the sk has not been added to
8359 sk = reuse_kern->sk;
8360 if (sk->sk_protocol != selected_sk->sk_protocol)
8362 else if (sk->sk_family != selected_sk->sk_family)
8363 return -EAFNOSUPPORT;
8365 /* Catch all. Likely bound to a different sockaddr. */
8369 reuse_kern->selected_sk = selected_sk;
8374 static const struct bpf_func_proto sk_select_reuseport_proto = {
8375 .func = sk_select_reuseport,
8377 .ret_type = RET_INTEGER,
8378 .arg1_type = ARG_PTR_TO_CTX,
8379 .arg2_type = ARG_CONST_MAP_PTR,
8380 .arg3_type = ARG_PTR_TO_MAP_KEY,
8381 .arg4_type = ARG_ANYTHING,
8384 BPF_CALL_4(sk_reuseport_load_bytes,
8385 const struct sk_reuseport_kern *, reuse_kern, u32, offset,
8386 void *, to, u32, len)
8388 return ____bpf_skb_load_bytes(reuse_kern->skb, offset, to, len);
8391 static const struct bpf_func_proto sk_reuseport_load_bytes_proto = {
8392 .func = sk_reuseport_load_bytes,
8394 .ret_type = RET_INTEGER,
8395 .arg1_type = ARG_PTR_TO_CTX,
8396 .arg2_type = ARG_ANYTHING,
8397 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
8398 .arg4_type = ARG_CONST_SIZE,
8401 BPF_CALL_5(sk_reuseport_load_bytes_relative,
8402 const struct sk_reuseport_kern *, reuse_kern, u32, offset,
8403 void *, to, u32, len, u32, start_header)
8405 return ____bpf_skb_load_bytes_relative(reuse_kern->skb, offset, to,
8409 static const struct bpf_func_proto sk_reuseport_load_bytes_relative_proto = {
8410 .func = sk_reuseport_load_bytes_relative,
8412 .ret_type = RET_INTEGER,
8413 .arg1_type = ARG_PTR_TO_CTX,
8414 .arg2_type = ARG_ANYTHING,
8415 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
8416 .arg4_type = ARG_CONST_SIZE,
8417 .arg5_type = ARG_ANYTHING,
8420 static const struct bpf_func_proto *
8421 sk_reuseport_func_proto(enum bpf_func_id func_id,
8422 const struct bpf_prog *prog)
8425 case BPF_FUNC_sk_select_reuseport:
8426 return &sk_select_reuseport_proto;
8427 case BPF_FUNC_skb_load_bytes:
8428 return &sk_reuseport_load_bytes_proto;
8429 case BPF_FUNC_skb_load_bytes_relative:
8430 return &sk_reuseport_load_bytes_relative_proto;
8432 return bpf_base_func_proto(func_id);
8437 sk_reuseport_is_valid_access(int off, int size,
8438 enum bpf_access_type type,
8439 const struct bpf_prog *prog,
8440 struct bpf_insn_access_aux *info)
8442 const u32 size_default = sizeof(__u32);
8444 if (off < 0 || off >= sizeof(struct sk_reuseport_md) ||
8445 off % size || type != BPF_READ)
8449 case offsetof(struct sk_reuseport_md, data):
8450 info->reg_type = PTR_TO_PACKET;
8451 return size == sizeof(__u64);
8453 case offsetof(struct sk_reuseport_md, data_end):
8454 info->reg_type = PTR_TO_PACKET_END;
8455 return size == sizeof(__u64);
8457 case offsetof(struct sk_reuseport_md, hash):
8458 return size == size_default;
8460 /* Fields that allow narrowing */
8461 case offsetof(struct sk_reuseport_md, eth_protocol):
8462 if (size < FIELD_SIZEOF(struct sk_buff, protocol))
8465 case offsetof(struct sk_reuseport_md, ip_protocol):
8466 case offsetof(struct sk_reuseport_md, bind_inany):
8467 case offsetof(struct sk_reuseport_md, len):
8468 bpf_ctx_record_field_size(info, size_default);
8469 return bpf_ctx_narrow_access_ok(off, size, size_default);
8476 #define SK_REUSEPORT_LOAD_FIELD(F) ({ \
8477 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_reuseport_kern, F), \
8478 si->dst_reg, si->src_reg, \
8479 bpf_target_off(struct sk_reuseport_kern, F, \
8480 FIELD_SIZEOF(struct sk_reuseport_kern, F), \
8484 #define SK_REUSEPORT_LOAD_SKB_FIELD(SKB_FIELD) \
8485 SOCK_ADDR_LOAD_NESTED_FIELD(struct sk_reuseport_kern, \
8490 #define SK_REUSEPORT_LOAD_SK_FIELD_SIZE_OFF(SK_FIELD, BPF_SIZE, EXTRA_OFF) \
8491 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(struct sk_reuseport_kern, \
8494 SK_FIELD, BPF_SIZE, EXTRA_OFF)
8496 static u32 sk_reuseport_convert_ctx_access(enum bpf_access_type type,
8497 const struct bpf_insn *si,
8498 struct bpf_insn *insn_buf,
8499 struct bpf_prog *prog,
8502 struct bpf_insn *insn = insn_buf;
8505 case offsetof(struct sk_reuseport_md, data):
8506 SK_REUSEPORT_LOAD_SKB_FIELD(data);
8509 case offsetof(struct sk_reuseport_md, len):
8510 SK_REUSEPORT_LOAD_SKB_FIELD(len);
8513 case offsetof(struct sk_reuseport_md, eth_protocol):
8514 SK_REUSEPORT_LOAD_SKB_FIELD(protocol);
8517 case offsetof(struct sk_reuseport_md, ip_protocol):
8518 BUILD_BUG_ON(HWEIGHT32(SK_FL_PROTO_MASK) != BITS_PER_BYTE);
8519 SK_REUSEPORT_LOAD_SK_FIELD_SIZE_OFF(__sk_flags_offset,
8521 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_PROTO_MASK);
8522 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg,
8524 /* SK_FL_PROTO_MASK and SK_FL_PROTO_SHIFT are endian
8525 * aware. No further narrowing or masking is needed.
8530 case offsetof(struct sk_reuseport_md, data_end):
8531 SK_REUSEPORT_LOAD_FIELD(data_end);
8534 case offsetof(struct sk_reuseport_md, hash):
8535 SK_REUSEPORT_LOAD_FIELD(hash);
8538 case offsetof(struct sk_reuseport_md, bind_inany):
8539 SK_REUSEPORT_LOAD_FIELD(bind_inany);
8543 return insn - insn_buf;
8546 const struct bpf_verifier_ops sk_reuseport_verifier_ops = {
8547 .get_func_proto = sk_reuseport_func_proto,
8548 .is_valid_access = sk_reuseport_is_valid_access,
8549 .convert_ctx_access = sk_reuseport_convert_ctx_access,
8552 const struct bpf_prog_ops sk_reuseport_prog_ops = {
8554 #endif /* CONFIG_INET */