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>
79 * sk_filter_trim_cap - run a packet through a socket filter
80 * @sk: sock associated with &sk_buff
81 * @skb: buffer to filter
82 * @cap: limit on how short the eBPF program may trim the packet
84 * Run the eBPF program and then cut skb->data to correct size returned by
85 * the program. If pkt_len is 0 we toss packet. If skb->len is smaller
86 * than pkt_len we keep whole skb->data. This is the socket level
87 * wrapper to BPF_PROG_RUN. It returns 0 if the packet should
88 * be accepted or -EPERM if the packet should be tossed.
91 int sk_filter_trim_cap(struct sock *sk, struct sk_buff *skb, unsigned int cap)
94 struct sk_filter *filter;
97 * If the skb was allocated from pfmemalloc reserves, only
98 * allow SOCK_MEMALLOC sockets to use it as this socket is
101 if (skb_pfmemalloc(skb) && !sock_flag(sk, SOCK_MEMALLOC)) {
102 NET_INC_STATS(sock_net(sk), LINUX_MIB_PFMEMALLOCDROP);
105 err = BPF_CGROUP_RUN_PROG_INET_INGRESS(sk, skb);
109 err = security_sock_rcv_skb(sk, skb);
114 filter = rcu_dereference(sk->sk_filter);
116 struct sock *save_sk = skb->sk;
117 unsigned int pkt_len;
120 pkt_len = bpf_prog_run_save_cb(filter->prog, skb);
122 err = pkt_len ? pskb_trim(skb, max(cap, pkt_len)) : -EPERM;
128 EXPORT_SYMBOL(sk_filter_trim_cap);
130 BPF_CALL_1(bpf_skb_get_pay_offset, struct sk_buff *, skb)
132 return skb_get_poff(skb);
135 BPF_CALL_3(bpf_skb_get_nlattr, struct sk_buff *, skb, u32, a, u32, x)
139 if (skb_is_nonlinear(skb))
142 if (skb->len < sizeof(struct nlattr))
145 if (a > skb->len - sizeof(struct nlattr))
148 nla = nla_find((struct nlattr *) &skb->data[a], skb->len - a, x);
150 return (void *) nla - (void *) skb->data;
155 BPF_CALL_3(bpf_skb_get_nlattr_nest, struct sk_buff *, skb, u32, a, u32, x)
159 if (skb_is_nonlinear(skb))
162 if (skb->len < sizeof(struct nlattr))
165 if (a > skb->len - sizeof(struct nlattr))
168 nla = (struct nlattr *) &skb->data[a];
169 if (nla->nla_len > skb->len - a)
172 nla = nla_find_nested(nla, x);
174 return (void *) nla - (void *) skb->data;
179 BPF_CALL_4(bpf_skb_load_helper_8, const struct sk_buff *, skb, const void *,
180 data, int, headlen, int, offset)
183 const int len = sizeof(tmp);
186 if (headlen - offset >= len)
187 return *(u8 *)(data + offset);
188 if (!skb_copy_bits(skb, offset, &tmp, sizeof(tmp)))
191 ptr = bpf_internal_load_pointer_neg_helper(skb, offset, len);
199 BPF_CALL_2(bpf_skb_load_helper_8_no_cache, const struct sk_buff *, skb,
202 return ____bpf_skb_load_helper_8(skb, skb->data, skb->len - skb->data_len,
206 BPF_CALL_4(bpf_skb_load_helper_16, const struct sk_buff *, skb, const void *,
207 data, int, headlen, int, offset)
210 const int len = sizeof(tmp);
213 if (headlen - offset >= len)
214 return get_unaligned_be16(data + offset);
215 if (!skb_copy_bits(skb, offset, &tmp, sizeof(tmp)))
216 return be16_to_cpu(tmp);
218 ptr = bpf_internal_load_pointer_neg_helper(skb, offset, len);
220 return get_unaligned_be16(ptr);
226 BPF_CALL_2(bpf_skb_load_helper_16_no_cache, const struct sk_buff *, skb,
229 return ____bpf_skb_load_helper_16(skb, skb->data, skb->len - skb->data_len,
233 BPF_CALL_4(bpf_skb_load_helper_32, const struct sk_buff *, skb, const void *,
234 data, int, headlen, int, offset)
237 const int len = sizeof(tmp);
239 if (likely(offset >= 0)) {
240 if (headlen - offset >= len)
241 return get_unaligned_be32(data + offset);
242 if (!skb_copy_bits(skb, offset, &tmp, sizeof(tmp)))
243 return be32_to_cpu(tmp);
245 ptr = bpf_internal_load_pointer_neg_helper(skb, offset, len);
247 return get_unaligned_be32(ptr);
253 BPF_CALL_2(bpf_skb_load_helper_32_no_cache, const struct sk_buff *, skb,
256 return ____bpf_skb_load_helper_32(skb, skb->data, skb->len - skb->data_len,
260 BPF_CALL_0(bpf_get_raw_cpu_id)
262 return raw_smp_processor_id();
265 static const struct bpf_func_proto bpf_get_raw_smp_processor_id_proto = {
266 .func = bpf_get_raw_cpu_id,
268 .ret_type = RET_INTEGER,
271 static u32 convert_skb_access(int skb_field, int dst_reg, int src_reg,
272 struct bpf_insn *insn_buf)
274 struct bpf_insn *insn = insn_buf;
278 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
280 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
281 offsetof(struct sk_buff, mark));
285 *insn++ = BPF_LDX_MEM(BPF_B, dst_reg, src_reg, PKT_TYPE_OFFSET());
286 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, PKT_TYPE_MAX);
287 #ifdef __BIG_ENDIAN_BITFIELD
288 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 5);
293 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, queue_mapping) != 2);
295 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
296 offsetof(struct sk_buff, queue_mapping));
299 case SKF_AD_VLAN_TAG:
300 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_tci) != 2);
302 /* dst_reg = *(u16 *) (src_reg + offsetof(vlan_tci)) */
303 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
304 offsetof(struct sk_buff, vlan_tci));
306 case SKF_AD_VLAN_TAG_PRESENT:
307 *insn++ = BPF_LDX_MEM(BPF_B, dst_reg, src_reg, PKT_VLAN_PRESENT_OFFSET());
308 if (PKT_VLAN_PRESENT_BIT)
309 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, PKT_VLAN_PRESENT_BIT);
310 if (PKT_VLAN_PRESENT_BIT < 7)
311 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, 1);
315 return insn - insn_buf;
318 static bool convert_bpf_extensions(struct sock_filter *fp,
319 struct bpf_insn **insnp)
321 struct bpf_insn *insn = *insnp;
325 case SKF_AD_OFF + SKF_AD_PROTOCOL:
326 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2);
328 /* A = *(u16 *) (CTX + offsetof(protocol)) */
329 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
330 offsetof(struct sk_buff, protocol));
331 /* A = ntohs(A) [emitting a nop or swap16] */
332 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
335 case SKF_AD_OFF + SKF_AD_PKTTYPE:
336 cnt = convert_skb_access(SKF_AD_PKTTYPE, BPF_REG_A, BPF_REG_CTX, insn);
340 case SKF_AD_OFF + SKF_AD_IFINDEX:
341 case SKF_AD_OFF + SKF_AD_HATYPE:
342 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4);
343 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, type) != 2);
345 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
346 BPF_REG_TMP, BPF_REG_CTX,
347 offsetof(struct sk_buff, dev));
348 /* if (tmp != 0) goto pc + 1 */
349 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_TMP, 0, 1);
350 *insn++ = BPF_EXIT_INSN();
351 if (fp->k == SKF_AD_OFF + SKF_AD_IFINDEX)
352 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_TMP,
353 offsetof(struct net_device, ifindex));
355 *insn = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_TMP,
356 offsetof(struct net_device, type));
359 case SKF_AD_OFF + SKF_AD_MARK:
360 cnt = convert_skb_access(SKF_AD_MARK, BPF_REG_A, BPF_REG_CTX, insn);
364 case SKF_AD_OFF + SKF_AD_RXHASH:
365 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4);
367 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX,
368 offsetof(struct sk_buff, hash));
371 case SKF_AD_OFF + SKF_AD_QUEUE:
372 cnt = convert_skb_access(SKF_AD_QUEUE, BPF_REG_A, BPF_REG_CTX, insn);
376 case SKF_AD_OFF + SKF_AD_VLAN_TAG:
377 cnt = convert_skb_access(SKF_AD_VLAN_TAG,
378 BPF_REG_A, BPF_REG_CTX, insn);
382 case SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT:
383 cnt = convert_skb_access(SKF_AD_VLAN_TAG_PRESENT,
384 BPF_REG_A, BPF_REG_CTX, insn);
388 case SKF_AD_OFF + SKF_AD_VLAN_TPID:
389 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_proto) != 2);
391 /* A = *(u16 *) (CTX + offsetof(vlan_proto)) */
392 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
393 offsetof(struct sk_buff, vlan_proto));
394 /* A = ntohs(A) [emitting a nop or swap16] */
395 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
398 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
399 case SKF_AD_OFF + SKF_AD_NLATTR:
400 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
401 case SKF_AD_OFF + SKF_AD_CPU:
402 case SKF_AD_OFF + SKF_AD_RANDOM:
404 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX);
406 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_A);
408 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_X);
409 /* Emit call(arg1=CTX, arg2=A, arg3=X) */
411 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
412 *insn = BPF_EMIT_CALL(bpf_skb_get_pay_offset);
414 case SKF_AD_OFF + SKF_AD_NLATTR:
415 *insn = BPF_EMIT_CALL(bpf_skb_get_nlattr);
417 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
418 *insn = BPF_EMIT_CALL(bpf_skb_get_nlattr_nest);
420 case SKF_AD_OFF + SKF_AD_CPU:
421 *insn = BPF_EMIT_CALL(bpf_get_raw_cpu_id);
423 case SKF_AD_OFF + SKF_AD_RANDOM:
424 *insn = BPF_EMIT_CALL(bpf_user_rnd_u32);
425 bpf_user_rnd_init_once();
430 case SKF_AD_OFF + SKF_AD_ALU_XOR_X:
432 *insn = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_X);
436 /* This is just a dummy call to avoid letting the compiler
437 * evict __bpf_call_base() as an optimization. Placed here
438 * where no-one bothers.
440 BUG_ON(__bpf_call_base(0, 0, 0, 0, 0) != 0);
448 static bool convert_bpf_ld_abs(struct sock_filter *fp, struct bpf_insn **insnp)
450 const bool unaligned_ok = IS_BUILTIN(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS);
451 int size = bpf_size_to_bytes(BPF_SIZE(fp->code));
452 bool endian = BPF_SIZE(fp->code) == BPF_H ||
453 BPF_SIZE(fp->code) == BPF_W;
454 bool indirect = BPF_MODE(fp->code) == BPF_IND;
455 const int ip_align = NET_IP_ALIGN;
456 struct bpf_insn *insn = *insnp;
460 ((unaligned_ok && offset >= 0) ||
461 (!unaligned_ok && offset >= 0 &&
462 offset + ip_align >= 0 &&
463 offset + ip_align % size == 0))) {
464 bool ldx_off_ok = offset <= S16_MAX;
466 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_H);
468 *insn++ = BPF_ALU64_IMM(BPF_SUB, BPF_REG_TMP, offset);
469 *insn++ = BPF_JMP_IMM(BPF_JSLT, BPF_REG_TMP,
470 size, 2 + endian + (!ldx_off_ok * 2));
472 *insn++ = BPF_LDX_MEM(BPF_SIZE(fp->code), BPF_REG_A,
475 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_D);
476 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_TMP, offset);
477 *insn++ = BPF_LDX_MEM(BPF_SIZE(fp->code), BPF_REG_A,
481 *insn++ = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, size * 8);
482 *insn++ = BPF_JMP_A(8);
485 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX);
486 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_D);
487 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_H);
489 *insn++ = BPF_MOV64_IMM(BPF_REG_ARG4, offset);
491 *insn++ = BPF_MOV64_REG(BPF_REG_ARG4, BPF_REG_X);
493 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_ARG4, offset);
496 switch (BPF_SIZE(fp->code)) {
498 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_8);
501 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_16);
504 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_32);
510 *insn++ = BPF_JMP_IMM(BPF_JSGE, BPF_REG_A, 0, 2);
511 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
512 *insn = BPF_EXIT_INSN();
519 * bpf_convert_filter - convert filter program
520 * @prog: the user passed filter program
521 * @len: the length of the user passed filter program
522 * @new_prog: allocated 'struct bpf_prog' or NULL
523 * @new_len: pointer to store length of converted program
524 * @seen_ld_abs: bool whether we've seen ld_abs/ind
526 * Remap 'sock_filter' style classic BPF (cBPF) instruction set to 'bpf_insn'
527 * style extended BPF (eBPF).
528 * Conversion workflow:
530 * 1) First pass for calculating the new program length:
531 * bpf_convert_filter(old_prog, old_len, NULL, &new_len, &seen_ld_abs)
533 * 2) 2nd pass to remap in two passes: 1st pass finds new
534 * jump offsets, 2nd pass remapping:
535 * bpf_convert_filter(old_prog, old_len, new_prog, &new_len, &seen_ld_abs)
537 static int bpf_convert_filter(struct sock_filter *prog, int len,
538 struct bpf_prog *new_prog, int *new_len,
541 int new_flen = 0, pass = 0, target, i, stack_off;
542 struct bpf_insn *new_insn, *first_insn = NULL;
543 struct sock_filter *fp;
547 BUILD_BUG_ON(BPF_MEMWORDS * sizeof(u32) > MAX_BPF_STACK);
548 BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG);
550 if (len <= 0 || len > BPF_MAXINSNS)
554 first_insn = new_prog->insnsi;
555 addrs = kcalloc(len, sizeof(*addrs),
556 GFP_KERNEL | __GFP_NOWARN);
562 new_insn = first_insn;
565 /* Classic BPF related prologue emission. */
567 /* Classic BPF expects A and X to be reset first. These need
568 * to be guaranteed to be the first two instructions.
570 *new_insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
571 *new_insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_X, BPF_REG_X);
573 /* All programs must keep CTX in callee saved BPF_REG_CTX.
574 * In eBPF case it's done by the compiler, here we need to
575 * do this ourself. Initial CTX is present in BPF_REG_ARG1.
577 *new_insn++ = BPF_MOV64_REG(BPF_REG_CTX, BPF_REG_ARG1);
579 /* For packet access in classic BPF, cache skb->data
580 * in callee-saved BPF R8 and skb->len - skb->data_len
581 * (headlen) in BPF R9. Since classic BPF is read-only
582 * on CTX, we only need to cache it once.
584 *new_insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
585 BPF_REG_D, BPF_REG_CTX,
586 offsetof(struct sk_buff, data));
587 *new_insn++ = BPF_LDX_MEM(BPF_W, BPF_REG_H, BPF_REG_CTX,
588 offsetof(struct sk_buff, len));
589 *new_insn++ = BPF_LDX_MEM(BPF_W, BPF_REG_TMP, BPF_REG_CTX,
590 offsetof(struct sk_buff, data_len));
591 *new_insn++ = BPF_ALU32_REG(BPF_SUB, BPF_REG_H, BPF_REG_TMP);
597 for (i = 0; i < len; fp++, i++) {
598 struct bpf_insn tmp_insns[32] = { };
599 struct bpf_insn *insn = tmp_insns;
602 addrs[i] = new_insn - first_insn;
605 /* All arithmetic insns and skb loads map as-is. */
606 case BPF_ALU | BPF_ADD | BPF_X:
607 case BPF_ALU | BPF_ADD | BPF_K:
608 case BPF_ALU | BPF_SUB | BPF_X:
609 case BPF_ALU | BPF_SUB | BPF_K:
610 case BPF_ALU | BPF_AND | BPF_X:
611 case BPF_ALU | BPF_AND | BPF_K:
612 case BPF_ALU | BPF_OR | BPF_X:
613 case BPF_ALU | BPF_OR | BPF_K:
614 case BPF_ALU | BPF_LSH | BPF_X:
615 case BPF_ALU | BPF_LSH | BPF_K:
616 case BPF_ALU | BPF_RSH | BPF_X:
617 case BPF_ALU | BPF_RSH | BPF_K:
618 case BPF_ALU | BPF_XOR | BPF_X:
619 case BPF_ALU | BPF_XOR | BPF_K:
620 case BPF_ALU | BPF_MUL | BPF_X:
621 case BPF_ALU | BPF_MUL | BPF_K:
622 case BPF_ALU | BPF_DIV | BPF_X:
623 case BPF_ALU | BPF_DIV | BPF_K:
624 case BPF_ALU | BPF_MOD | BPF_X:
625 case BPF_ALU | BPF_MOD | BPF_K:
626 case BPF_ALU | BPF_NEG:
627 case BPF_LD | BPF_ABS | BPF_W:
628 case BPF_LD | BPF_ABS | BPF_H:
629 case BPF_LD | BPF_ABS | BPF_B:
630 case BPF_LD | BPF_IND | BPF_W:
631 case BPF_LD | BPF_IND | BPF_H:
632 case BPF_LD | BPF_IND | BPF_B:
633 /* Check for overloaded BPF extension and
634 * directly convert it if found, otherwise
635 * just move on with mapping.
637 if (BPF_CLASS(fp->code) == BPF_LD &&
638 BPF_MODE(fp->code) == BPF_ABS &&
639 convert_bpf_extensions(fp, &insn))
641 if (BPF_CLASS(fp->code) == BPF_LD &&
642 convert_bpf_ld_abs(fp, &insn)) {
647 if (fp->code == (BPF_ALU | BPF_DIV | BPF_X) ||
648 fp->code == (BPF_ALU | BPF_MOD | BPF_X)) {
649 *insn++ = BPF_MOV32_REG(BPF_REG_X, BPF_REG_X);
650 /* Error with exception code on div/mod by 0.
651 * For cBPF programs, this was always return 0.
653 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_X, 0, 2);
654 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
655 *insn++ = BPF_EXIT_INSN();
658 *insn = BPF_RAW_INSN(fp->code, BPF_REG_A, BPF_REG_X, 0, fp->k);
661 /* Jump transformation cannot use BPF block macros
662 * everywhere as offset calculation and target updates
663 * require a bit more work than the rest, i.e. jump
664 * opcodes map as-is, but offsets need adjustment.
667 #define BPF_EMIT_JMP \
669 const s32 off_min = S16_MIN, off_max = S16_MAX; \
672 if (target >= len || target < 0) \
674 off = addrs ? addrs[target] - addrs[i] - 1 : 0; \
675 /* Adjust pc relative offset for 2nd or 3rd insn. */ \
676 off -= insn - tmp_insns; \
677 /* Reject anything not fitting into insn->off. */ \
678 if (off < off_min || off > off_max) \
683 case BPF_JMP | BPF_JA:
684 target = i + fp->k + 1;
685 insn->code = fp->code;
689 case BPF_JMP | BPF_JEQ | BPF_K:
690 case BPF_JMP | BPF_JEQ | BPF_X:
691 case BPF_JMP | BPF_JSET | BPF_K:
692 case BPF_JMP | BPF_JSET | BPF_X:
693 case BPF_JMP | BPF_JGT | BPF_K:
694 case BPF_JMP | BPF_JGT | BPF_X:
695 case BPF_JMP | BPF_JGE | BPF_K:
696 case BPF_JMP | BPF_JGE | BPF_X:
697 if (BPF_SRC(fp->code) == BPF_K && (int) fp->k < 0) {
698 /* BPF immediates are signed, zero extend
699 * immediate into tmp register and use it
702 *insn++ = BPF_MOV32_IMM(BPF_REG_TMP, fp->k);
704 insn->dst_reg = BPF_REG_A;
705 insn->src_reg = BPF_REG_TMP;
708 insn->dst_reg = BPF_REG_A;
710 bpf_src = BPF_SRC(fp->code);
711 insn->src_reg = bpf_src == BPF_X ? BPF_REG_X : 0;
714 /* Common case where 'jump_false' is next insn. */
716 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
717 target = i + fp->jt + 1;
722 /* Convert some jumps when 'jump_true' is next insn. */
724 switch (BPF_OP(fp->code)) {
726 insn->code = BPF_JMP | BPF_JNE | bpf_src;
729 insn->code = BPF_JMP | BPF_JLE | bpf_src;
732 insn->code = BPF_JMP | BPF_JLT | bpf_src;
738 target = i + fp->jf + 1;
743 /* Other jumps are mapped into two insns: Jxx and JA. */
744 target = i + fp->jt + 1;
745 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
749 insn->code = BPF_JMP | BPF_JA;
750 target = i + fp->jf + 1;
754 /* ldxb 4 * ([14] & 0xf) is remaped into 6 insns. */
755 case BPF_LDX | BPF_MSH | BPF_B: {
756 struct sock_filter tmp = {
757 .code = BPF_LD | BPF_ABS | BPF_B,
764 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
765 /* A = BPF_R0 = *(u8 *) (skb->data + K) */
766 convert_bpf_ld_abs(&tmp, &insn);
769 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, 0xf);
771 *insn++ = BPF_ALU32_IMM(BPF_LSH, BPF_REG_A, 2);
773 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_X);
775 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
777 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_TMP);
780 /* RET_K is remaped into 2 insns. RET_A case doesn't need an
781 * extra mov as BPF_REG_0 is already mapped into BPF_REG_A.
783 case BPF_RET | BPF_A:
784 case BPF_RET | BPF_K:
785 if (BPF_RVAL(fp->code) == BPF_K)
786 *insn++ = BPF_MOV32_RAW(BPF_K, BPF_REG_0,
788 *insn = BPF_EXIT_INSN();
791 /* Store to stack. */
794 stack_off = fp->k * 4 + 4;
795 *insn = BPF_STX_MEM(BPF_W, BPF_REG_FP, BPF_CLASS(fp->code) ==
796 BPF_ST ? BPF_REG_A : BPF_REG_X,
798 /* check_load_and_stores() verifies that classic BPF can
799 * load from stack only after write, so tracking
800 * stack_depth for ST|STX insns is enough
802 if (new_prog && new_prog->aux->stack_depth < stack_off)
803 new_prog->aux->stack_depth = stack_off;
806 /* Load from stack. */
807 case BPF_LD | BPF_MEM:
808 case BPF_LDX | BPF_MEM:
809 stack_off = fp->k * 4 + 4;
810 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
811 BPF_REG_A : BPF_REG_X, BPF_REG_FP,
816 case BPF_LD | BPF_IMM:
817 case BPF_LDX | BPF_IMM:
818 *insn = BPF_MOV32_IMM(BPF_CLASS(fp->code) == BPF_LD ?
819 BPF_REG_A : BPF_REG_X, fp->k);
823 case BPF_MISC | BPF_TAX:
824 *insn = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
828 case BPF_MISC | BPF_TXA:
829 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_X);
832 /* A = skb->len or X = skb->len */
833 case BPF_LD | BPF_W | BPF_LEN:
834 case BPF_LDX | BPF_W | BPF_LEN:
835 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
836 BPF_REG_A : BPF_REG_X, BPF_REG_CTX,
837 offsetof(struct sk_buff, len));
840 /* Access seccomp_data fields. */
841 case BPF_LDX | BPF_ABS | BPF_W:
842 /* A = *(u32 *) (ctx + K) */
843 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX, fp->k);
846 /* Unknown instruction. */
853 memcpy(new_insn, tmp_insns,
854 sizeof(*insn) * (insn - tmp_insns));
855 new_insn += insn - tmp_insns;
859 /* Only calculating new length. */
860 *new_len = new_insn - first_insn;
862 *new_len += 4; /* Prologue bits. */
867 if (new_flen != new_insn - first_insn) {
868 new_flen = new_insn - first_insn;
875 BUG_ON(*new_len != new_flen);
884 * As we dont want to clear mem[] array for each packet going through
885 * __bpf_prog_run(), we check that filter loaded by user never try to read
886 * a cell if not previously written, and we check all branches to be sure
887 * a malicious user doesn't try to abuse us.
889 static int check_load_and_stores(const struct sock_filter *filter, int flen)
891 u16 *masks, memvalid = 0; /* One bit per cell, 16 cells */
894 BUILD_BUG_ON(BPF_MEMWORDS > 16);
896 masks = kmalloc_array(flen, sizeof(*masks), GFP_KERNEL);
900 memset(masks, 0xff, flen * sizeof(*masks));
902 for (pc = 0; pc < flen; pc++) {
903 memvalid &= masks[pc];
905 switch (filter[pc].code) {
908 memvalid |= (1 << filter[pc].k);
910 case BPF_LD | BPF_MEM:
911 case BPF_LDX | BPF_MEM:
912 if (!(memvalid & (1 << filter[pc].k))) {
917 case BPF_JMP | BPF_JA:
918 /* A jump must set masks on target */
919 masks[pc + 1 + filter[pc].k] &= memvalid;
922 case BPF_JMP | BPF_JEQ | BPF_K:
923 case BPF_JMP | BPF_JEQ | BPF_X:
924 case BPF_JMP | BPF_JGE | BPF_K:
925 case BPF_JMP | BPF_JGE | BPF_X:
926 case BPF_JMP | BPF_JGT | BPF_K:
927 case BPF_JMP | BPF_JGT | BPF_X:
928 case BPF_JMP | BPF_JSET | BPF_K:
929 case BPF_JMP | BPF_JSET | BPF_X:
930 /* A jump must set masks on targets */
931 masks[pc + 1 + filter[pc].jt] &= memvalid;
932 masks[pc + 1 + filter[pc].jf] &= memvalid;
942 static bool chk_code_allowed(u16 code_to_probe)
944 static const bool codes[] = {
945 /* 32 bit ALU operations */
946 [BPF_ALU | BPF_ADD | BPF_K] = true,
947 [BPF_ALU | BPF_ADD | BPF_X] = true,
948 [BPF_ALU | BPF_SUB | BPF_K] = true,
949 [BPF_ALU | BPF_SUB | BPF_X] = true,
950 [BPF_ALU | BPF_MUL | BPF_K] = true,
951 [BPF_ALU | BPF_MUL | BPF_X] = true,
952 [BPF_ALU | BPF_DIV | BPF_K] = true,
953 [BPF_ALU | BPF_DIV | BPF_X] = true,
954 [BPF_ALU | BPF_MOD | BPF_K] = true,
955 [BPF_ALU | BPF_MOD | BPF_X] = true,
956 [BPF_ALU | BPF_AND | BPF_K] = true,
957 [BPF_ALU | BPF_AND | BPF_X] = true,
958 [BPF_ALU | BPF_OR | BPF_K] = true,
959 [BPF_ALU | BPF_OR | BPF_X] = true,
960 [BPF_ALU | BPF_XOR | BPF_K] = true,
961 [BPF_ALU | BPF_XOR | BPF_X] = true,
962 [BPF_ALU | BPF_LSH | BPF_K] = true,
963 [BPF_ALU | BPF_LSH | BPF_X] = true,
964 [BPF_ALU | BPF_RSH | BPF_K] = true,
965 [BPF_ALU | BPF_RSH | BPF_X] = true,
966 [BPF_ALU | BPF_NEG] = true,
967 /* Load instructions */
968 [BPF_LD | BPF_W | BPF_ABS] = true,
969 [BPF_LD | BPF_H | BPF_ABS] = true,
970 [BPF_LD | BPF_B | BPF_ABS] = true,
971 [BPF_LD | BPF_W | BPF_LEN] = true,
972 [BPF_LD | BPF_W | BPF_IND] = true,
973 [BPF_LD | BPF_H | BPF_IND] = true,
974 [BPF_LD | BPF_B | BPF_IND] = true,
975 [BPF_LD | BPF_IMM] = true,
976 [BPF_LD | BPF_MEM] = true,
977 [BPF_LDX | BPF_W | BPF_LEN] = true,
978 [BPF_LDX | BPF_B | BPF_MSH] = true,
979 [BPF_LDX | BPF_IMM] = true,
980 [BPF_LDX | BPF_MEM] = true,
981 /* Store instructions */
984 /* Misc instructions */
985 [BPF_MISC | BPF_TAX] = true,
986 [BPF_MISC | BPF_TXA] = true,
987 /* Return instructions */
988 [BPF_RET | BPF_K] = true,
989 [BPF_RET | BPF_A] = true,
990 /* Jump instructions */
991 [BPF_JMP | BPF_JA] = true,
992 [BPF_JMP | BPF_JEQ | BPF_K] = true,
993 [BPF_JMP | BPF_JEQ | BPF_X] = true,
994 [BPF_JMP | BPF_JGE | BPF_K] = true,
995 [BPF_JMP | BPF_JGE | BPF_X] = true,
996 [BPF_JMP | BPF_JGT | BPF_K] = true,
997 [BPF_JMP | BPF_JGT | BPF_X] = true,
998 [BPF_JMP | BPF_JSET | BPF_K] = true,
999 [BPF_JMP | BPF_JSET | BPF_X] = true,
1002 if (code_to_probe >= ARRAY_SIZE(codes))
1005 return codes[code_to_probe];
1008 static bool bpf_check_basics_ok(const struct sock_filter *filter,
1013 if (flen == 0 || flen > BPF_MAXINSNS)
1020 * bpf_check_classic - verify socket filter code
1021 * @filter: filter to verify
1022 * @flen: length of filter
1024 * Check the user's filter code. If we let some ugly
1025 * filter code slip through kaboom! The filter must contain
1026 * no references or jumps that are out of range, no illegal
1027 * instructions, and must end with a RET instruction.
1029 * All jumps are forward as they are not signed.
1031 * Returns 0 if the rule set is legal or -EINVAL if not.
1033 static int bpf_check_classic(const struct sock_filter *filter,
1039 /* Check the filter code now */
1040 for (pc = 0; pc < flen; pc++) {
1041 const struct sock_filter *ftest = &filter[pc];
1043 /* May we actually operate on this code? */
1044 if (!chk_code_allowed(ftest->code))
1047 /* Some instructions need special checks */
1048 switch (ftest->code) {
1049 case BPF_ALU | BPF_DIV | BPF_K:
1050 case BPF_ALU | BPF_MOD | BPF_K:
1051 /* Check for division by zero */
1055 case BPF_ALU | BPF_LSH | BPF_K:
1056 case BPF_ALU | BPF_RSH | BPF_K:
1060 case BPF_LD | BPF_MEM:
1061 case BPF_LDX | BPF_MEM:
1064 /* Check for invalid memory addresses */
1065 if (ftest->k >= BPF_MEMWORDS)
1068 case BPF_JMP | BPF_JA:
1069 /* Note, the large ftest->k might cause loops.
1070 * Compare this with conditional jumps below,
1071 * where offsets are limited. --ANK (981016)
1073 if (ftest->k >= (unsigned int)(flen - pc - 1))
1076 case BPF_JMP | BPF_JEQ | BPF_K:
1077 case BPF_JMP | BPF_JEQ | BPF_X:
1078 case BPF_JMP | BPF_JGE | BPF_K:
1079 case BPF_JMP | BPF_JGE | BPF_X:
1080 case BPF_JMP | BPF_JGT | BPF_K:
1081 case BPF_JMP | BPF_JGT | BPF_X:
1082 case BPF_JMP | BPF_JSET | BPF_K:
1083 case BPF_JMP | BPF_JSET | BPF_X:
1084 /* Both conditionals must be safe */
1085 if (pc + ftest->jt + 1 >= flen ||
1086 pc + ftest->jf + 1 >= flen)
1089 case BPF_LD | BPF_W | BPF_ABS:
1090 case BPF_LD | BPF_H | BPF_ABS:
1091 case BPF_LD | BPF_B | BPF_ABS:
1093 if (bpf_anc_helper(ftest) & BPF_ANC)
1095 /* Ancillary operation unknown or unsupported */
1096 if (anc_found == false && ftest->k >= SKF_AD_OFF)
1101 /* Last instruction must be a RET code */
1102 switch (filter[flen - 1].code) {
1103 case BPF_RET | BPF_K:
1104 case BPF_RET | BPF_A:
1105 return check_load_and_stores(filter, flen);
1111 static int bpf_prog_store_orig_filter(struct bpf_prog *fp,
1112 const struct sock_fprog *fprog)
1114 unsigned int fsize = bpf_classic_proglen(fprog);
1115 struct sock_fprog_kern *fkprog;
1117 fp->orig_prog = kmalloc(sizeof(*fkprog), GFP_KERNEL);
1121 fkprog = fp->orig_prog;
1122 fkprog->len = fprog->len;
1124 fkprog->filter = kmemdup(fp->insns, fsize,
1125 GFP_KERNEL | __GFP_NOWARN);
1126 if (!fkprog->filter) {
1127 kfree(fp->orig_prog);
1134 static void bpf_release_orig_filter(struct bpf_prog *fp)
1136 struct sock_fprog_kern *fprog = fp->orig_prog;
1139 kfree(fprog->filter);
1144 static void __bpf_prog_release(struct bpf_prog *prog)
1146 if (prog->type == BPF_PROG_TYPE_SOCKET_FILTER) {
1149 bpf_release_orig_filter(prog);
1150 bpf_prog_free(prog);
1154 static void __sk_filter_release(struct sk_filter *fp)
1156 __bpf_prog_release(fp->prog);
1161 * sk_filter_release_rcu - Release a socket filter by rcu_head
1162 * @rcu: rcu_head that contains the sk_filter to free
1164 static void sk_filter_release_rcu(struct rcu_head *rcu)
1166 struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu);
1168 __sk_filter_release(fp);
1172 * sk_filter_release - release a socket filter
1173 * @fp: filter to remove
1175 * Remove a filter from a socket and release its resources.
1177 static void sk_filter_release(struct sk_filter *fp)
1179 if (refcount_dec_and_test(&fp->refcnt))
1180 call_rcu(&fp->rcu, sk_filter_release_rcu);
1183 void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp)
1185 u32 filter_size = bpf_prog_size(fp->prog->len);
1187 atomic_sub(filter_size, &sk->sk_omem_alloc);
1188 sk_filter_release(fp);
1191 /* try to charge the socket memory if there is space available
1192 * return true on success
1194 static bool __sk_filter_charge(struct sock *sk, struct sk_filter *fp)
1196 u32 filter_size = bpf_prog_size(fp->prog->len);
1198 /* same check as in sock_kmalloc() */
1199 if (filter_size <= sysctl_optmem_max &&
1200 atomic_read(&sk->sk_omem_alloc) + filter_size < sysctl_optmem_max) {
1201 atomic_add(filter_size, &sk->sk_omem_alloc);
1207 bool sk_filter_charge(struct sock *sk, struct sk_filter *fp)
1209 if (!refcount_inc_not_zero(&fp->refcnt))
1212 if (!__sk_filter_charge(sk, fp)) {
1213 sk_filter_release(fp);
1219 static struct bpf_prog *bpf_migrate_filter(struct bpf_prog *fp)
1221 struct sock_filter *old_prog;
1222 struct bpf_prog *old_fp;
1223 int err, new_len, old_len = fp->len;
1224 bool seen_ld_abs = false;
1226 /* We are free to overwrite insns et al right here as it
1227 * won't be used at this point in time anymore internally
1228 * after the migration to the internal BPF instruction
1231 BUILD_BUG_ON(sizeof(struct sock_filter) !=
1232 sizeof(struct bpf_insn));
1234 /* Conversion cannot happen on overlapping memory areas,
1235 * so we need to keep the user BPF around until the 2nd
1236 * pass. At this time, the user BPF is stored in fp->insns.
1238 old_prog = kmemdup(fp->insns, old_len * sizeof(struct sock_filter),
1239 GFP_KERNEL | __GFP_NOWARN);
1245 /* 1st pass: calculate the new program length. */
1246 err = bpf_convert_filter(old_prog, old_len, NULL, &new_len,
1251 /* Expand fp for appending the new filter representation. */
1253 fp = bpf_prog_realloc(old_fp, bpf_prog_size(new_len), 0);
1255 /* The old_fp is still around in case we couldn't
1256 * allocate new memory, so uncharge on that one.
1265 /* 2nd pass: remap sock_filter insns into bpf_insn insns. */
1266 err = bpf_convert_filter(old_prog, old_len, fp, &new_len,
1269 /* 2nd bpf_convert_filter() can fail only if it fails
1270 * to allocate memory, remapping must succeed. Note,
1271 * that at this time old_fp has already been released
1276 fp = bpf_prog_select_runtime(fp, &err);
1286 __bpf_prog_release(fp);
1287 return ERR_PTR(err);
1290 static struct bpf_prog *bpf_prepare_filter(struct bpf_prog *fp,
1291 bpf_aux_classic_check_t trans)
1295 fp->bpf_func = NULL;
1298 err = bpf_check_classic(fp->insns, fp->len);
1300 __bpf_prog_release(fp);
1301 return ERR_PTR(err);
1304 /* There might be additional checks and transformations
1305 * needed on classic filters, f.e. in case of seccomp.
1308 err = trans(fp->insns, fp->len);
1310 __bpf_prog_release(fp);
1311 return ERR_PTR(err);
1315 /* Probe if we can JIT compile the filter and if so, do
1316 * the compilation of the filter.
1318 bpf_jit_compile(fp);
1320 /* JIT compiler couldn't process this filter, so do the
1321 * internal BPF translation for the optimized interpreter.
1324 fp = bpf_migrate_filter(fp);
1330 * bpf_prog_create - create an unattached filter
1331 * @pfp: the unattached filter that is created
1332 * @fprog: the filter program
1334 * Create a filter independent of any socket. We first run some
1335 * sanity checks on it to make sure it does not explode on us later.
1336 * If an error occurs or there is insufficient memory for the filter
1337 * a negative errno code is returned. On success the return is zero.
1339 int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog)
1341 unsigned int fsize = bpf_classic_proglen(fprog);
1342 struct bpf_prog *fp;
1344 /* Make sure new filter is there and in the right amounts. */
1345 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1348 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1352 memcpy(fp->insns, fprog->filter, fsize);
1354 fp->len = fprog->len;
1355 /* Since unattached filters are not copied back to user
1356 * space through sk_get_filter(), we do not need to hold
1357 * a copy here, and can spare us the work.
1359 fp->orig_prog = NULL;
1361 /* bpf_prepare_filter() already takes care of freeing
1362 * memory in case something goes wrong.
1364 fp = bpf_prepare_filter(fp, NULL);
1371 EXPORT_SYMBOL_GPL(bpf_prog_create);
1374 * bpf_prog_create_from_user - create an unattached filter from user buffer
1375 * @pfp: the unattached filter that is created
1376 * @fprog: the filter program
1377 * @trans: post-classic verifier transformation handler
1378 * @save_orig: save classic BPF program
1380 * This function effectively does the same as bpf_prog_create(), only
1381 * that it builds up its insns buffer from user space provided buffer.
1382 * It also allows for passing a bpf_aux_classic_check_t handler.
1384 int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog,
1385 bpf_aux_classic_check_t trans, bool save_orig)
1387 unsigned int fsize = bpf_classic_proglen(fprog);
1388 struct bpf_prog *fp;
1391 /* Make sure new filter is there and in the right amounts. */
1392 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1395 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1399 if (copy_from_user(fp->insns, fprog->filter, fsize)) {
1400 __bpf_prog_free(fp);
1404 fp->len = fprog->len;
1405 fp->orig_prog = NULL;
1408 err = bpf_prog_store_orig_filter(fp, fprog);
1410 __bpf_prog_free(fp);
1415 /* bpf_prepare_filter() already takes care of freeing
1416 * memory in case something goes wrong.
1418 fp = bpf_prepare_filter(fp, trans);
1425 EXPORT_SYMBOL_GPL(bpf_prog_create_from_user);
1427 void bpf_prog_destroy(struct bpf_prog *fp)
1429 __bpf_prog_release(fp);
1431 EXPORT_SYMBOL_GPL(bpf_prog_destroy);
1433 static int __sk_attach_prog(struct bpf_prog *prog, struct sock *sk)
1435 struct sk_filter *fp, *old_fp;
1437 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
1443 if (!__sk_filter_charge(sk, fp)) {
1447 refcount_set(&fp->refcnt, 1);
1449 old_fp = rcu_dereference_protected(sk->sk_filter,
1450 lockdep_sock_is_held(sk));
1451 rcu_assign_pointer(sk->sk_filter, fp);
1454 sk_filter_uncharge(sk, old_fp);
1460 struct bpf_prog *__get_filter(struct sock_fprog *fprog, struct sock *sk)
1462 unsigned int fsize = bpf_classic_proglen(fprog);
1463 struct bpf_prog *prog;
1466 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1467 return ERR_PTR(-EPERM);
1469 /* Make sure new filter is there and in the right amounts. */
1470 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1471 return ERR_PTR(-EINVAL);
1473 prog = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1475 return ERR_PTR(-ENOMEM);
1477 if (copy_from_user(prog->insns, fprog->filter, fsize)) {
1478 __bpf_prog_free(prog);
1479 return ERR_PTR(-EFAULT);
1482 prog->len = fprog->len;
1484 err = bpf_prog_store_orig_filter(prog, fprog);
1486 __bpf_prog_free(prog);
1487 return ERR_PTR(-ENOMEM);
1490 /* bpf_prepare_filter() already takes care of freeing
1491 * memory in case something goes wrong.
1493 return bpf_prepare_filter(prog, NULL);
1497 * sk_attach_filter - attach a socket filter
1498 * @fprog: the filter program
1499 * @sk: the socket to use
1501 * Attach the user's filter code. We first run some sanity checks on
1502 * it to make sure it does not explode on us later. If an error
1503 * occurs or there is insufficient memory for the filter a negative
1504 * errno code is returned. On success the return is zero.
1506 int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1508 struct bpf_prog *prog = __get_filter(fprog, sk);
1512 return PTR_ERR(prog);
1514 err = __sk_attach_prog(prog, sk);
1516 __bpf_prog_release(prog);
1522 EXPORT_SYMBOL_GPL(sk_attach_filter);
1524 int sk_reuseport_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1526 struct bpf_prog *prog = __get_filter(fprog, sk);
1530 return PTR_ERR(prog);
1532 if (bpf_prog_size(prog->len) > sysctl_optmem_max)
1535 err = reuseport_attach_prog(sk, prog);
1538 __bpf_prog_release(prog);
1543 static struct bpf_prog *__get_bpf(u32 ufd, struct sock *sk)
1545 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1546 return ERR_PTR(-EPERM);
1548 return bpf_prog_get_type(ufd, BPF_PROG_TYPE_SOCKET_FILTER);
1551 int sk_attach_bpf(u32 ufd, struct sock *sk)
1553 struct bpf_prog *prog = __get_bpf(ufd, sk);
1557 return PTR_ERR(prog);
1559 err = __sk_attach_prog(prog, sk);
1568 int sk_reuseport_attach_bpf(u32 ufd, struct sock *sk)
1570 struct bpf_prog *prog;
1573 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1576 prog = bpf_prog_get_type(ufd, BPF_PROG_TYPE_SOCKET_FILTER);
1577 if (IS_ERR(prog) && PTR_ERR(prog) == -EINVAL)
1578 prog = bpf_prog_get_type(ufd, BPF_PROG_TYPE_SK_REUSEPORT);
1580 return PTR_ERR(prog);
1582 if (prog->type == BPF_PROG_TYPE_SK_REUSEPORT) {
1583 /* Like other non BPF_PROG_TYPE_SOCKET_FILTER
1584 * bpf prog (e.g. sockmap). It depends on the
1585 * limitation imposed by bpf_prog_load().
1586 * Hence, sysctl_optmem_max is not checked.
1588 if ((sk->sk_type != SOCK_STREAM &&
1589 sk->sk_type != SOCK_DGRAM) ||
1590 (sk->sk_protocol != IPPROTO_UDP &&
1591 sk->sk_protocol != IPPROTO_TCP) ||
1592 (sk->sk_family != AF_INET &&
1593 sk->sk_family != AF_INET6)) {
1598 /* BPF_PROG_TYPE_SOCKET_FILTER */
1599 if (bpf_prog_size(prog->len) > sysctl_optmem_max) {
1605 err = reuseport_attach_prog(sk, prog);
1613 void sk_reuseport_prog_free(struct bpf_prog *prog)
1618 if (prog->type == BPF_PROG_TYPE_SK_REUSEPORT)
1621 bpf_prog_destroy(prog);
1624 struct bpf_scratchpad {
1626 __be32 diff[MAX_BPF_STACK / sizeof(__be32)];
1627 u8 buff[MAX_BPF_STACK];
1631 static DEFINE_PER_CPU(struct bpf_scratchpad, bpf_sp);
1633 static inline int __bpf_try_make_writable(struct sk_buff *skb,
1634 unsigned int write_len)
1636 return skb_ensure_writable(skb, write_len);
1639 static inline int bpf_try_make_writable(struct sk_buff *skb,
1640 unsigned int write_len)
1642 int err = __bpf_try_make_writable(skb, write_len);
1644 bpf_compute_data_pointers(skb);
1648 static int bpf_try_make_head_writable(struct sk_buff *skb)
1650 return bpf_try_make_writable(skb, skb_headlen(skb));
1653 static inline void bpf_push_mac_rcsum(struct sk_buff *skb)
1655 if (skb_at_tc_ingress(skb))
1656 skb_postpush_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1659 static inline void bpf_pull_mac_rcsum(struct sk_buff *skb)
1661 if (skb_at_tc_ingress(skb))
1662 skb_postpull_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1665 BPF_CALL_5(bpf_skb_store_bytes, struct sk_buff *, skb, u32, offset,
1666 const void *, from, u32, len, u64, flags)
1670 if (unlikely(flags & ~(BPF_F_RECOMPUTE_CSUM | BPF_F_INVALIDATE_HASH)))
1672 if (unlikely(offset > 0xffff))
1674 if (unlikely(bpf_try_make_writable(skb, offset + len)))
1677 ptr = skb->data + offset;
1678 if (flags & BPF_F_RECOMPUTE_CSUM)
1679 __skb_postpull_rcsum(skb, ptr, len, offset);
1681 memcpy(ptr, from, len);
1683 if (flags & BPF_F_RECOMPUTE_CSUM)
1684 __skb_postpush_rcsum(skb, ptr, len, offset);
1685 if (flags & BPF_F_INVALIDATE_HASH)
1686 skb_clear_hash(skb);
1691 static const struct bpf_func_proto bpf_skb_store_bytes_proto = {
1692 .func = bpf_skb_store_bytes,
1694 .ret_type = RET_INTEGER,
1695 .arg1_type = ARG_PTR_TO_CTX,
1696 .arg2_type = ARG_ANYTHING,
1697 .arg3_type = ARG_PTR_TO_MEM,
1698 .arg4_type = ARG_CONST_SIZE,
1699 .arg5_type = ARG_ANYTHING,
1702 BPF_CALL_4(bpf_skb_load_bytes, const struct sk_buff *, skb, u32, offset,
1703 void *, to, u32, len)
1707 if (unlikely(offset > 0xffff))
1710 ptr = skb_header_pointer(skb, offset, len, to);
1714 memcpy(to, ptr, len);
1722 static const struct bpf_func_proto bpf_skb_load_bytes_proto = {
1723 .func = bpf_skb_load_bytes,
1725 .ret_type = RET_INTEGER,
1726 .arg1_type = ARG_PTR_TO_CTX,
1727 .arg2_type = ARG_ANYTHING,
1728 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
1729 .arg4_type = ARG_CONST_SIZE,
1732 BPF_CALL_5(bpf_skb_load_bytes_relative, const struct sk_buff *, skb,
1733 u32, offset, void *, to, u32, len, u32, start_header)
1735 u8 *end = skb_tail_pointer(skb);
1736 u8 *net = skb_network_header(skb);
1737 u8 *mac = skb_mac_header(skb);
1740 if (unlikely(offset > 0xffff || len > (end - mac)))
1743 switch (start_header) {
1744 case BPF_HDR_START_MAC:
1747 case BPF_HDR_START_NET:
1754 if (likely(ptr >= mac && ptr + len <= end)) {
1755 memcpy(to, ptr, len);
1764 static const struct bpf_func_proto bpf_skb_load_bytes_relative_proto = {
1765 .func = bpf_skb_load_bytes_relative,
1767 .ret_type = RET_INTEGER,
1768 .arg1_type = ARG_PTR_TO_CTX,
1769 .arg2_type = ARG_ANYTHING,
1770 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
1771 .arg4_type = ARG_CONST_SIZE,
1772 .arg5_type = ARG_ANYTHING,
1775 BPF_CALL_2(bpf_skb_pull_data, struct sk_buff *, skb, u32, len)
1777 /* Idea is the following: should the needed direct read/write
1778 * test fail during runtime, we can pull in more data and redo
1779 * again, since implicitly, we invalidate previous checks here.
1781 * Or, since we know how much we need to make read/writeable,
1782 * this can be done once at the program beginning for direct
1783 * access case. By this we overcome limitations of only current
1784 * headroom being accessible.
1786 return bpf_try_make_writable(skb, len ? : skb_headlen(skb));
1789 static const struct bpf_func_proto bpf_skb_pull_data_proto = {
1790 .func = bpf_skb_pull_data,
1792 .ret_type = RET_INTEGER,
1793 .arg1_type = ARG_PTR_TO_CTX,
1794 .arg2_type = ARG_ANYTHING,
1797 BPF_CALL_1(bpf_sk_fullsock, struct sock *, sk)
1799 return sk_fullsock(sk) ? (unsigned long)sk : (unsigned long)NULL;
1802 static const struct bpf_func_proto bpf_sk_fullsock_proto = {
1803 .func = bpf_sk_fullsock,
1805 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
1806 .arg1_type = ARG_PTR_TO_SOCK_COMMON,
1809 static inline int sk_skb_try_make_writable(struct sk_buff *skb,
1810 unsigned int write_len)
1812 int err = __bpf_try_make_writable(skb, write_len);
1814 bpf_compute_data_end_sk_skb(skb);
1818 BPF_CALL_2(sk_skb_pull_data, struct sk_buff *, skb, u32, len)
1820 /* Idea is the following: should the needed direct read/write
1821 * test fail during runtime, we can pull in more data and redo
1822 * again, since implicitly, we invalidate previous checks here.
1824 * Or, since we know how much we need to make read/writeable,
1825 * this can be done once at the program beginning for direct
1826 * access case. By this we overcome limitations of only current
1827 * headroom being accessible.
1829 return sk_skb_try_make_writable(skb, len ? : skb_headlen(skb));
1832 static const struct bpf_func_proto sk_skb_pull_data_proto = {
1833 .func = sk_skb_pull_data,
1835 .ret_type = RET_INTEGER,
1836 .arg1_type = ARG_PTR_TO_CTX,
1837 .arg2_type = ARG_ANYTHING,
1840 BPF_CALL_5(bpf_l3_csum_replace, struct sk_buff *, skb, u32, offset,
1841 u64, from, u64, to, u64, flags)
1845 if (unlikely(flags & ~(BPF_F_HDR_FIELD_MASK)))
1847 if (unlikely(offset > 0xffff || offset & 1))
1849 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
1852 ptr = (__sum16 *)(skb->data + offset);
1853 switch (flags & BPF_F_HDR_FIELD_MASK) {
1855 if (unlikely(from != 0))
1858 csum_replace_by_diff(ptr, to);
1861 csum_replace2(ptr, from, to);
1864 csum_replace4(ptr, from, to);
1873 static const struct bpf_func_proto bpf_l3_csum_replace_proto = {
1874 .func = bpf_l3_csum_replace,
1876 .ret_type = RET_INTEGER,
1877 .arg1_type = ARG_PTR_TO_CTX,
1878 .arg2_type = ARG_ANYTHING,
1879 .arg3_type = ARG_ANYTHING,
1880 .arg4_type = ARG_ANYTHING,
1881 .arg5_type = ARG_ANYTHING,
1884 BPF_CALL_5(bpf_l4_csum_replace, struct sk_buff *, skb, u32, offset,
1885 u64, from, u64, to, u64, flags)
1887 bool is_pseudo = flags & BPF_F_PSEUDO_HDR;
1888 bool is_mmzero = flags & BPF_F_MARK_MANGLED_0;
1889 bool do_mforce = flags & BPF_F_MARK_ENFORCE;
1892 if (unlikely(flags & ~(BPF_F_MARK_MANGLED_0 | BPF_F_MARK_ENFORCE |
1893 BPF_F_PSEUDO_HDR | BPF_F_HDR_FIELD_MASK)))
1895 if (unlikely(offset > 0xffff || offset & 1))
1897 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
1900 ptr = (__sum16 *)(skb->data + offset);
1901 if (is_mmzero && !do_mforce && !*ptr)
1904 switch (flags & BPF_F_HDR_FIELD_MASK) {
1906 if (unlikely(from != 0))
1909 inet_proto_csum_replace_by_diff(ptr, skb, to, is_pseudo);
1912 inet_proto_csum_replace2(ptr, skb, from, to, is_pseudo);
1915 inet_proto_csum_replace4(ptr, skb, from, to, is_pseudo);
1921 if (is_mmzero && !*ptr)
1922 *ptr = CSUM_MANGLED_0;
1926 static const struct bpf_func_proto bpf_l4_csum_replace_proto = {
1927 .func = bpf_l4_csum_replace,
1929 .ret_type = RET_INTEGER,
1930 .arg1_type = ARG_PTR_TO_CTX,
1931 .arg2_type = ARG_ANYTHING,
1932 .arg3_type = ARG_ANYTHING,
1933 .arg4_type = ARG_ANYTHING,
1934 .arg5_type = ARG_ANYTHING,
1937 BPF_CALL_5(bpf_csum_diff, __be32 *, from, u32, from_size,
1938 __be32 *, to, u32, to_size, __wsum, seed)
1940 struct bpf_scratchpad *sp = this_cpu_ptr(&bpf_sp);
1941 u32 diff_size = from_size + to_size;
1944 /* This is quite flexible, some examples:
1946 * from_size == 0, to_size > 0, seed := csum --> pushing data
1947 * from_size > 0, to_size == 0, seed := csum --> pulling data
1948 * from_size > 0, to_size > 0, seed := 0 --> diffing data
1950 * Even for diffing, from_size and to_size don't need to be equal.
1952 if (unlikely(((from_size | to_size) & (sizeof(__be32) - 1)) ||
1953 diff_size > sizeof(sp->diff)))
1956 for (i = 0; i < from_size / sizeof(__be32); i++, j++)
1957 sp->diff[j] = ~from[i];
1958 for (i = 0; i < to_size / sizeof(__be32); i++, j++)
1959 sp->diff[j] = to[i];
1961 return csum_partial(sp->diff, diff_size, seed);
1964 static const struct bpf_func_proto bpf_csum_diff_proto = {
1965 .func = bpf_csum_diff,
1968 .ret_type = RET_INTEGER,
1969 .arg1_type = ARG_PTR_TO_MEM_OR_NULL,
1970 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
1971 .arg3_type = ARG_PTR_TO_MEM_OR_NULL,
1972 .arg4_type = ARG_CONST_SIZE_OR_ZERO,
1973 .arg5_type = ARG_ANYTHING,
1976 BPF_CALL_2(bpf_csum_update, struct sk_buff *, skb, __wsum, csum)
1978 /* The interface is to be used in combination with bpf_csum_diff()
1979 * for direct packet writes. csum rotation for alignment as well
1980 * as emulating csum_sub() can be done from the eBPF program.
1982 if (skb->ip_summed == CHECKSUM_COMPLETE)
1983 return (skb->csum = csum_add(skb->csum, csum));
1988 static const struct bpf_func_proto bpf_csum_update_proto = {
1989 .func = bpf_csum_update,
1991 .ret_type = RET_INTEGER,
1992 .arg1_type = ARG_PTR_TO_CTX,
1993 .arg2_type = ARG_ANYTHING,
1996 static inline int __bpf_rx_skb(struct net_device *dev, struct sk_buff *skb)
1998 return dev_forward_skb(dev, skb);
2001 static inline int __bpf_rx_skb_no_mac(struct net_device *dev,
2002 struct sk_buff *skb)
2004 int ret = ____dev_forward_skb(dev, skb);
2008 ret = netif_rx(skb);
2014 static inline int __bpf_tx_skb(struct net_device *dev, struct sk_buff *skb)
2018 if (unlikely(__this_cpu_read(xmit_recursion) > XMIT_RECURSION_LIMIT)) {
2019 net_crit_ratelimited("bpf: recursion limit reached on datapath, buggy bpf program?\n");
2026 __this_cpu_inc(xmit_recursion);
2027 ret = dev_queue_xmit(skb);
2028 __this_cpu_dec(xmit_recursion);
2033 static int __bpf_redirect_no_mac(struct sk_buff *skb, struct net_device *dev,
2036 unsigned int mlen = skb_network_offset(skb);
2039 __skb_pull(skb, mlen);
2041 /* At ingress, the mac header has already been pulled once.
2042 * At egress, skb_pospull_rcsum has to be done in case that
2043 * the skb is originated from ingress (i.e. a forwarded skb)
2044 * to ensure that rcsum starts at net header.
2046 if (!skb_at_tc_ingress(skb))
2047 skb_postpull_rcsum(skb, skb_mac_header(skb), mlen);
2049 skb_pop_mac_header(skb);
2050 skb_reset_mac_len(skb);
2051 return flags & BPF_F_INGRESS ?
2052 __bpf_rx_skb_no_mac(dev, skb) : __bpf_tx_skb(dev, skb);
2055 static int __bpf_redirect_common(struct sk_buff *skb, struct net_device *dev,
2058 /* Verify that a link layer header is carried */
2059 if (unlikely(skb->mac_header >= skb->network_header)) {
2064 bpf_push_mac_rcsum(skb);
2065 return flags & BPF_F_INGRESS ?
2066 __bpf_rx_skb(dev, skb) : __bpf_tx_skb(dev, skb);
2069 static int __bpf_redirect(struct sk_buff *skb, struct net_device *dev,
2072 if (dev_is_mac_header_xmit(dev))
2073 return __bpf_redirect_common(skb, dev, flags);
2075 return __bpf_redirect_no_mac(skb, dev, flags);
2078 BPF_CALL_3(bpf_clone_redirect, struct sk_buff *, skb, u32, ifindex, u64, flags)
2080 struct net_device *dev;
2081 struct sk_buff *clone;
2084 if (unlikely(flags & ~(BPF_F_INGRESS)))
2087 dev = dev_get_by_index_rcu(dev_net(skb->dev), ifindex);
2091 clone = skb_clone(skb, GFP_ATOMIC);
2092 if (unlikely(!clone))
2095 /* For direct write, we need to keep the invariant that the skbs
2096 * we're dealing with need to be uncloned. Should uncloning fail
2097 * here, we need to free the just generated clone to unclone once
2100 ret = bpf_try_make_head_writable(skb);
2101 if (unlikely(ret)) {
2106 return __bpf_redirect(clone, dev, flags);
2109 static const struct bpf_func_proto bpf_clone_redirect_proto = {
2110 .func = bpf_clone_redirect,
2112 .ret_type = RET_INTEGER,
2113 .arg1_type = ARG_PTR_TO_CTX,
2114 .arg2_type = ARG_ANYTHING,
2115 .arg3_type = ARG_ANYTHING,
2118 DEFINE_PER_CPU(struct bpf_redirect_info, bpf_redirect_info);
2119 EXPORT_PER_CPU_SYMBOL_GPL(bpf_redirect_info);
2121 BPF_CALL_2(bpf_redirect, u32, ifindex, u64, flags)
2123 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
2125 if (unlikely(flags & ~(BPF_F_INGRESS)))
2128 ri->ifindex = ifindex;
2131 return TC_ACT_REDIRECT;
2134 int skb_do_redirect(struct sk_buff *skb)
2136 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
2137 struct net_device *dev;
2139 dev = dev_get_by_index_rcu(dev_net(skb->dev), ri->ifindex);
2141 if (unlikely(!dev)) {
2146 return __bpf_redirect(skb, dev, ri->flags);
2149 static const struct bpf_func_proto bpf_redirect_proto = {
2150 .func = bpf_redirect,
2152 .ret_type = RET_INTEGER,
2153 .arg1_type = ARG_ANYTHING,
2154 .arg2_type = ARG_ANYTHING,
2157 BPF_CALL_2(bpf_msg_apply_bytes, struct sk_msg *, msg, u32, bytes)
2159 msg->apply_bytes = bytes;
2163 static const struct bpf_func_proto bpf_msg_apply_bytes_proto = {
2164 .func = bpf_msg_apply_bytes,
2166 .ret_type = RET_INTEGER,
2167 .arg1_type = ARG_PTR_TO_CTX,
2168 .arg2_type = ARG_ANYTHING,
2171 BPF_CALL_2(bpf_msg_cork_bytes, struct sk_msg *, msg, u32, bytes)
2173 msg->cork_bytes = bytes;
2177 static const struct bpf_func_proto bpf_msg_cork_bytes_proto = {
2178 .func = bpf_msg_cork_bytes,
2180 .ret_type = RET_INTEGER,
2181 .arg1_type = ARG_PTR_TO_CTX,
2182 .arg2_type = ARG_ANYTHING,
2185 BPF_CALL_4(bpf_msg_pull_data, struct sk_msg *, msg, u32, start,
2186 u32, end, u64, flags)
2188 u32 len = 0, offset = 0, copy = 0, poffset = 0, bytes = end - start;
2189 u32 first_sge, last_sge, i, shift, bytes_sg_total;
2190 struct scatterlist *sge;
2191 u8 *raw, *to, *from;
2194 if (unlikely(flags || end <= start))
2197 /* First find the starting scatterlist element */
2200 len = sk_msg_elem(msg, i)->length;
2201 if (start < offset + len)
2204 sk_msg_iter_var_next(i);
2205 } while (i != msg->sg.end);
2207 if (unlikely(start >= offset + len))
2211 /* The start may point into the sg element so we need to also
2212 * account for the headroom.
2214 bytes_sg_total = start - offset + bytes;
2215 if (!msg->sg.copy[i] && bytes_sg_total <= len)
2218 /* At this point we need to linearize multiple scatterlist
2219 * elements or a single shared page. Either way we need to
2220 * copy into a linear buffer exclusively owned by BPF. Then
2221 * place the buffer in the scatterlist and fixup the original
2222 * entries by removing the entries now in the linear buffer
2223 * and shifting the remaining entries. For now we do not try
2224 * to copy partial entries to avoid complexity of running out
2225 * of sg_entry slots. The downside is reading a single byte
2226 * will copy the entire sg entry.
2229 copy += sk_msg_elem(msg, i)->length;
2230 sk_msg_iter_var_next(i);
2231 if (bytes_sg_total <= copy)
2233 } while (i != msg->sg.end);
2236 if (unlikely(bytes_sg_total > copy))
2239 page = alloc_pages(__GFP_NOWARN | GFP_ATOMIC | __GFP_COMP,
2241 if (unlikely(!page))
2244 raw = page_address(page);
2247 sge = sk_msg_elem(msg, i);
2248 from = sg_virt(sge);
2252 memcpy(to, from, len);
2255 put_page(sg_page(sge));
2257 sk_msg_iter_var_next(i);
2258 } while (i != last_sge);
2260 sg_set_page(&msg->sg.data[first_sge], page, copy, 0);
2262 /* To repair sg ring we need to shift entries. If we only
2263 * had a single entry though we can just replace it and
2264 * be done. Otherwise walk the ring and shift the entries.
2266 WARN_ON_ONCE(last_sge == first_sge);
2267 shift = last_sge > first_sge ?
2268 last_sge - first_sge - 1 :
2269 MAX_SKB_FRAGS - first_sge + last_sge - 1;
2274 sk_msg_iter_var_next(i);
2278 if (i + shift >= MAX_MSG_FRAGS)
2279 move_from = i + shift - MAX_MSG_FRAGS;
2281 move_from = i + shift;
2282 if (move_from == msg->sg.end)
2285 msg->sg.data[i] = msg->sg.data[move_from];
2286 msg->sg.data[move_from].length = 0;
2287 msg->sg.data[move_from].page_link = 0;
2288 msg->sg.data[move_from].offset = 0;
2289 sk_msg_iter_var_next(i);
2292 msg->sg.end = msg->sg.end - shift > msg->sg.end ?
2293 msg->sg.end - shift + MAX_MSG_FRAGS :
2294 msg->sg.end - shift;
2296 msg->data = sg_virt(&msg->sg.data[first_sge]) + start - offset;
2297 msg->data_end = msg->data + bytes;
2301 static const struct bpf_func_proto bpf_msg_pull_data_proto = {
2302 .func = bpf_msg_pull_data,
2304 .ret_type = RET_INTEGER,
2305 .arg1_type = ARG_PTR_TO_CTX,
2306 .arg2_type = ARG_ANYTHING,
2307 .arg3_type = ARG_ANYTHING,
2308 .arg4_type = ARG_ANYTHING,
2311 BPF_CALL_4(bpf_msg_push_data, struct sk_msg *, msg, u32, start,
2312 u32, len, u64, flags)
2314 struct scatterlist sge, nsge, nnsge, rsge = {0}, *psge;
2315 u32 new, i = 0, l, space, copy = 0, offset = 0;
2316 u8 *raw, *to, *from;
2319 if (unlikely(flags))
2322 /* First find the starting scatterlist element */
2325 l = sk_msg_elem(msg, i)->length;
2327 if (start < offset + l)
2330 sk_msg_iter_var_next(i);
2331 } while (i != msg->sg.end);
2333 if (start >= offset + l)
2336 space = MAX_MSG_FRAGS - sk_msg_elem_used(msg);
2338 /* If no space available will fallback to copy, we need at
2339 * least one scatterlist elem available to push data into
2340 * when start aligns to the beginning of an element or two
2341 * when it falls inside an element. We handle the start equals
2342 * offset case because its the common case for inserting a
2345 if (!space || (space == 1 && start != offset))
2346 copy = msg->sg.data[i].length;
2348 page = alloc_pages(__GFP_NOWARN | GFP_ATOMIC | __GFP_COMP,
2349 get_order(copy + len));
2350 if (unlikely(!page))
2356 raw = page_address(page);
2358 psge = sk_msg_elem(msg, i);
2359 front = start - offset;
2360 back = psge->length - front;
2361 from = sg_virt(psge);
2364 memcpy(raw, from, front);
2368 to = raw + front + len;
2370 memcpy(to, from, back);
2373 put_page(sg_page(psge));
2374 } else if (start - offset) {
2375 psge = sk_msg_elem(msg, i);
2376 rsge = sk_msg_elem_cpy(msg, i);
2378 psge->length = start - offset;
2379 rsge.length -= psge->length;
2380 rsge.offset += start;
2382 sk_msg_iter_var_next(i);
2383 sg_unmark_end(psge);
2384 sk_msg_iter_next(msg, end);
2387 /* Slot(s) to place newly allocated data */
2390 /* Shift one or two slots as needed */
2392 sge = sk_msg_elem_cpy(msg, i);
2394 sk_msg_iter_var_next(i);
2395 sg_unmark_end(&sge);
2396 sk_msg_iter_next(msg, end);
2398 nsge = sk_msg_elem_cpy(msg, i);
2400 sk_msg_iter_var_next(i);
2401 nnsge = sk_msg_elem_cpy(msg, i);
2404 while (i != msg->sg.end) {
2405 msg->sg.data[i] = sge;
2407 sk_msg_iter_var_next(i);
2410 nnsge = sk_msg_elem_cpy(msg, i);
2412 nsge = sk_msg_elem_cpy(msg, i);
2417 /* Place newly allocated data buffer */
2418 sk_mem_charge(msg->sk, len);
2419 msg->sg.size += len;
2420 msg->sg.copy[new] = false;
2421 sg_set_page(&msg->sg.data[new], page, len + copy, 0);
2423 get_page(sg_page(&rsge));
2424 sk_msg_iter_var_next(new);
2425 msg->sg.data[new] = rsge;
2428 sk_msg_compute_data_pointers(msg);
2432 static const struct bpf_func_proto bpf_msg_push_data_proto = {
2433 .func = bpf_msg_push_data,
2435 .ret_type = RET_INTEGER,
2436 .arg1_type = ARG_PTR_TO_CTX,
2437 .arg2_type = ARG_ANYTHING,
2438 .arg3_type = ARG_ANYTHING,
2439 .arg4_type = ARG_ANYTHING,
2442 static void sk_msg_shift_left(struct sk_msg *msg, int i)
2448 sk_msg_iter_var_next(i);
2449 msg->sg.data[prev] = msg->sg.data[i];
2450 } while (i != msg->sg.end);
2452 sk_msg_iter_prev(msg, end);
2455 static void sk_msg_shift_right(struct sk_msg *msg, int i)
2457 struct scatterlist tmp, sge;
2459 sk_msg_iter_next(msg, end);
2460 sge = sk_msg_elem_cpy(msg, i);
2461 sk_msg_iter_var_next(i);
2462 tmp = sk_msg_elem_cpy(msg, i);
2464 while (i != msg->sg.end) {
2465 msg->sg.data[i] = sge;
2466 sk_msg_iter_var_next(i);
2468 tmp = sk_msg_elem_cpy(msg, i);
2472 BPF_CALL_4(bpf_msg_pop_data, struct sk_msg *, msg, u32, start,
2473 u32, len, u64, flags)
2475 u32 i = 0, l, space, offset = 0;
2476 u64 last = start + len;
2479 if (unlikely(flags))
2482 /* First find the starting scatterlist element */
2485 l = sk_msg_elem(msg, i)->length;
2487 if (start < offset + l)
2490 sk_msg_iter_var_next(i);
2491 } while (i != msg->sg.end);
2493 /* Bounds checks: start and pop must be inside message */
2494 if (start >= offset + l || last >= msg->sg.size)
2497 space = MAX_MSG_FRAGS - sk_msg_elem_used(msg);
2500 /* --------------| offset
2501 * -| start |-------- len -------|
2503 * |----- a ----|-------- pop -------|----- b ----|
2504 * |______________________________________________| length
2507 * a: region at front of scatter element to save
2508 * b: region at back of scatter element to save when length > A + pop
2509 * pop: region to pop from element, same as input 'pop' here will be
2510 * decremented below per iteration.
2512 * Two top-level cases to handle when start != offset, first B is non
2513 * zero and second B is zero corresponding to when a pop includes more
2516 * Then if B is non-zero AND there is no space allocate space and
2517 * compact A, B regions into page. If there is space shift ring to
2518 * the rigth free'ing the next element in ring to place B, leaving
2519 * A untouched except to reduce length.
2521 if (start != offset) {
2522 struct scatterlist *nsge, *sge = sk_msg_elem(msg, i);
2524 int b = sge->length - pop - a;
2526 sk_msg_iter_var_next(i);
2528 if (pop < sge->length - a) {
2531 sk_msg_shift_right(msg, i);
2532 nsge = sk_msg_elem(msg, i);
2533 get_page(sg_page(sge));
2536 b, sge->offset + pop + a);
2538 struct page *page, *orig;
2541 page = alloc_pages(__GFP_NOWARN |
2542 __GFP_COMP | GFP_ATOMIC,
2544 if (unlikely(!page))
2548 orig = sg_page(sge);
2549 from = sg_virt(sge);
2550 to = page_address(page);
2551 memcpy(to, from, a);
2552 memcpy(to + a, from + a + pop, b);
2553 sg_set_page(sge, page, a + b, 0);
2557 } else if (pop >= sge->length - a) {
2559 pop -= (sge->length - a);
2563 /* From above the current layout _must_ be as follows,
2568 * |---- pop ---|---------------- b ------------|
2569 * |____________________________________________| length
2571 * Offset and start of the current msg elem are equal because in the
2572 * previous case we handled offset != start and either consumed the
2573 * entire element and advanced to the next element OR pop == 0.
2575 * Two cases to handle here are first pop is less than the length
2576 * leaving some remainder b above. Simply adjust the element's layout
2577 * in this case. Or pop >= length of the element so that b = 0. In this
2578 * case advance to next element decrementing pop.
2581 struct scatterlist *sge = sk_msg_elem(msg, i);
2583 if (pop < sge->length) {
2589 sk_msg_shift_left(msg, i);
2591 sk_msg_iter_var_next(i);
2594 sk_mem_uncharge(msg->sk, len - pop);
2595 msg->sg.size -= (len - pop);
2596 sk_msg_compute_data_pointers(msg);
2600 static const struct bpf_func_proto bpf_msg_pop_data_proto = {
2601 .func = bpf_msg_pop_data,
2603 .ret_type = RET_INTEGER,
2604 .arg1_type = ARG_PTR_TO_CTX,
2605 .arg2_type = ARG_ANYTHING,
2606 .arg3_type = ARG_ANYTHING,
2607 .arg4_type = ARG_ANYTHING,
2610 BPF_CALL_1(bpf_get_cgroup_classid, const struct sk_buff *, skb)
2612 return task_get_classid(skb);
2615 static const struct bpf_func_proto bpf_get_cgroup_classid_proto = {
2616 .func = bpf_get_cgroup_classid,
2618 .ret_type = RET_INTEGER,
2619 .arg1_type = ARG_PTR_TO_CTX,
2622 BPF_CALL_1(bpf_get_route_realm, const struct sk_buff *, skb)
2624 return dst_tclassid(skb);
2627 static const struct bpf_func_proto bpf_get_route_realm_proto = {
2628 .func = bpf_get_route_realm,
2630 .ret_type = RET_INTEGER,
2631 .arg1_type = ARG_PTR_TO_CTX,
2634 BPF_CALL_1(bpf_get_hash_recalc, struct sk_buff *, skb)
2636 /* If skb_clear_hash() was called due to mangling, we can
2637 * trigger SW recalculation here. Later access to hash
2638 * can then use the inline skb->hash via context directly
2639 * instead of calling this helper again.
2641 return skb_get_hash(skb);
2644 static const struct bpf_func_proto bpf_get_hash_recalc_proto = {
2645 .func = bpf_get_hash_recalc,
2647 .ret_type = RET_INTEGER,
2648 .arg1_type = ARG_PTR_TO_CTX,
2651 BPF_CALL_1(bpf_set_hash_invalid, struct sk_buff *, skb)
2653 /* After all direct packet write, this can be used once for
2654 * triggering a lazy recalc on next skb_get_hash() invocation.
2656 skb_clear_hash(skb);
2660 static const struct bpf_func_proto bpf_set_hash_invalid_proto = {
2661 .func = bpf_set_hash_invalid,
2663 .ret_type = RET_INTEGER,
2664 .arg1_type = ARG_PTR_TO_CTX,
2667 BPF_CALL_2(bpf_set_hash, struct sk_buff *, skb, u32, hash)
2669 /* Set user specified hash as L4(+), so that it gets returned
2670 * on skb_get_hash() call unless BPF prog later on triggers a
2673 __skb_set_sw_hash(skb, hash, true);
2677 static const struct bpf_func_proto bpf_set_hash_proto = {
2678 .func = bpf_set_hash,
2680 .ret_type = RET_INTEGER,
2681 .arg1_type = ARG_PTR_TO_CTX,
2682 .arg2_type = ARG_ANYTHING,
2685 BPF_CALL_3(bpf_skb_vlan_push, struct sk_buff *, skb, __be16, vlan_proto,
2690 if (unlikely(vlan_proto != htons(ETH_P_8021Q) &&
2691 vlan_proto != htons(ETH_P_8021AD)))
2692 vlan_proto = htons(ETH_P_8021Q);
2694 bpf_push_mac_rcsum(skb);
2695 ret = skb_vlan_push(skb, vlan_proto, vlan_tci);
2696 bpf_pull_mac_rcsum(skb);
2698 bpf_compute_data_pointers(skb);
2702 static const struct bpf_func_proto bpf_skb_vlan_push_proto = {
2703 .func = bpf_skb_vlan_push,
2705 .ret_type = RET_INTEGER,
2706 .arg1_type = ARG_PTR_TO_CTX,
2707 .arg2_type = ARG_ANYTHING,
2708 .arg3_type = ARG_ANYTHING,
2711 BPF_CALL_1(bpf_skb_vlan_pop, struct sk_buff *, skb)
2715 bpf_push_mac_rcsum(skb);
2716 ret = skb_vlan_pop(skb);
2717 bpf_pull_mac_rcsum(skb);
2719 bpf_compute_data_pointers(skb);
2723 static const struct bpf_func_proto bpf_skb_vlan_pop_proto = {
2724 .func = bpf_skb_vlan_pop,
2726 .ret_type = RET_INTEGER,
2727 .arg1_type = ARG_PTR_TO_CTX,
2730 static int bpf_skb_generic_push(struct sk_buff *skb, u32 off, u32 len)
2732 /* Caller already did skb_cow() with len as headroom,
2733 * so no need to do it here.
2736 memmove(skb->data, skb->data + len, off);
2737 memset(skb->data + off, 0, len);
2739 /* No skb_postpush_rcsum(skb, skb->data + off, len)
2740 * needed here as it does not change the skb->csum
2741 * result for checksum complete when summing over
2747 static int bpf_skb_generic_pop(struct sk_buff *skb, u32 off, u32 len)
2749 /* skb_ensure_writable() is not needed here, as we're
2750 * already working on an uncloned skb.
2752 if (unlikely(!pskb_may_pull(skb, off + len)))
2755 skb_postpull_rcsum(skb, skb->data + off, len);
2756 memmove(skb->data + len, skb->data, off);
2757 __skb_pull(skb, len);
2762 static int bpf_skb_net_hdr_push(struct sk_buff *skb, u32 off, u32 len)
2764 bool trans_same = skb->transport_header == skb->network_header;
2767 /* There's no need for __skb_push()/__skb_pull() pair to
2768 * get to the start of the mac header as we're guaranteed
2769 * to always start from here under eBPF.
2771 ret = bpf_skb_generic_push(skb, off, len);
2773 skb->mac_header -= len;
2774 skb->network_header -= len;
2776 skb->transport_header = skb->network_header;
2782 static int bpf_skb_net_hdr_pop(struct sk_buff *skb, u32 off, u32 len)
2784 bool trans_same = skb->transport_header == skb->network_header;
2787 /* Same here, __skb_push()/__skb_pull() pair not needed. */
2788 ret = bpf_skb_generic_pop(skb, off, len);
2790 skb->mac_header += len;
2791 skb->network_header += len;
2793 skb->transport_header = skb->network_header;
2799 static int bpf_skb_proto_4_to_6(struct sk_buff *skb)
2801 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
2802 u32 off = skb_mac_header_len(skb);
2805 if (skb_is_gso(skb) && !skb_is_gso_tcp(skb))
2808 ret = skb_cow(skb, len_diff);
2809 if (unlikely(ret < 0))
2812 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
2813 if (unlikely(ret < 0))
2816 if (skb_is_gso(skb)) {
2817 struct skb_shared_info *shinfo = skb_shinfo(skb);
2819 /* SKB_GSO_TCPV4 needs to be changed into
2822 if (shinfo->gso_type & SKB_GSO_TCPV4) {
2823 shinfo->gso_type &= ~SKB_GSO_TCPV4;
2824 shinfo->gso_type |= SKB_GSO_TCPV6;
2827 /* Due to IPv6 header, MSS needs to be downgraded. */
2828 skb_decrease_gso_size(shinfo, len_diff);
2829 /* Header must be checked, and gso_segs recomputed. */
2830 shinfo->gso_type |= SKB_GSO_DODGY;
2831 shinfo->gso_segs = 0;
2834 skb->protocol = htons(ETH_P_IPV6);
2835 skb_clear_hash(skb);
2840 static int bpf_skb_proto_6_to_4(struct sk_buff *skb)
2842 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
2843 u32 off = skb_mac_header_len(skb);
2846 if (skb_is_gso(skb) && !skb_is_gso_tcp(skb))
2849 ret = skb_unclone(skb, GFP_ATOMIC);
2850 if (unlikely(ret < 0))
2853 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
2854 if (unlikely(ret < 0))
2857 if (skb_is_gso(skb)) {
2858 struct skb_shared_info *shinfo = skb_shinfo(skb);
2860 /* SKB_GSO_TCPV6 needs to be changed into
2863 if (shinfo->gso_type & SKB_GSO_TCPV6) {
2864 shinfo->gso_type &= ~SKB_GSO_TCPV6;
2865 shinfo->gso_type |= SKB_GSO_TCPV4;
2868 /* Due to IPv4 header, MSS can be upgraded. */
2869 skb_increase_gso_size(shinfo, len_diff);
2870 /* Header must be checked, and gso_segs recomputed. */
2871 shinfo->gso_type |= SKB_GSO_DODGY;
2872 shinfo->gso_segs = 0;
2875 skb->protocol = htons(ETH_P_IP);
2876 skb_clear_hash(skb);
2881 static int bpf_skb_proto_xlat(struct sk_buff *skb, __be16 to_proto)
2883 __be16 from_proto = skb->protocol;
2885 if (from_proto == htons(ETH_P_IP) &&
2886 to_proto == htons(ETH_P_IPV6))
2887 return bpf_skb_proto_4_to_6(skb);
2889 if (from_proto == htons(ETH_P_IPV6) &&
2890 to_proto == htons(ETH_P_IP))
2891 return bpf_skb_proto_6_to_4(skb);
2896 BPF_CALL_3(bpf_skb_change_proto, struct sk_buff *, skb, __be16, proto,
2901 if (unlikely(flags))
2904 /* General idea is that this helper does the basic groundwork
2905 * needed for changing the protocol, and eBPF program fills the
2906 * rest through bpf_skb_store_bytes(), bpf_lX_csum_replace()
2907 * and other helpers, rather than passing a raw buffer here.
2909 * The rationale is to keep this minimal and without a need to
2910 * deal with raw packet data. F.e. even if we would pass buffers
2911 * here, the program still needs to call the bpf_lX_csum_replace()
2912 * helpers anyway. Plus, this way we keep also separation of
2913 * concerns, since f.e. bpf_skb_store_bytes() should only take
2916 * Currently, additional options and extension header space are
2917 * not supported, but flags register is reserved so we can adapt
2918 * that. For offloads, we mark packet as dodgy, so that headers
2919 * need to be verified first.
2921 ret = bpf_skb_proto_xlat(skb, proto);
2922 bpf_compute_data_pointers(skb);
2926 static const struct bpf_func_proto bpf_skb_change_proto_proto = {
2927 .func = bpf_skb_change_proto,
2929 .ret_type = RET_INTEGER,
2930 .arg1_type = ARG_PTR_TO_CTX,
2931 .arg2_type = ARG_ANYTHING,
2932 .arg3_type = ARG_ANYTHING,
2935 BPF_CALL_2(bpf_skb_change_type, struct sk_buff *, skb, u32, pkt_type)
2937 /* We only allow a restricted subset to be changed for now. */
2938 if (unlikely(!skb_pkt_type_ok(skb->pkt_type) ||
2939 !skb_pkt_type_ok(pkt_type)))
2942 skb->pkt_type = pkt_type;
2946 static const struct bpf_func_proto bpf_skb_change_type_proto = {
2947 .func = bpf_skb_change_type,
2949 .ret_type = RET_INTEGER,
2950 .arg1_type = ARG_PTR_TO_CTX,
2951 .arg2_type = ARG_ANYTHING,
2954 static u32 bpf_skb_net_base_len(const struct sk_buff *skb)
2956 switch (skb->protocol) {
2957 case htons(ETH_P_IP):
2958 return sizeof(struct iphdr);
2959 case htons(ETH_P_IPV6):
2960 return sizeof(struct ipv6hdr);
2966 #define BPF_F_ADJ_ROOM_ENCAP_L3_MASK (BPF_F_ADJ_ROOM_ENCAP_L3_IPV4 | \
2967 BPF_F_ADJ_ROOM_ENCAP_L3_IPV6)
2969 #define BPF_F_ADJ_ROOM_MASK (BPF_F_ADJ_ROOM_FIXED_GSO | \
2970 BPF_F_ADJ_ROOM_ENCAP_L3_MASK | \
2971 BPF_F_ADJ_ROOM_ENCAP_L4_GRE | \
2972 BPF_F_ADJ_ROOM_ENCAP_L4_UDP)
2974 static int bpf_skb_net_grow(struct sk_buff *skb, u32 off, u32 len_diff,
2977 bool encap = flags & BPF_F_ADJ_ROOM_ENCAP_L3_MASK;
2978 unsigned int gso_type = SKB_GSO_DODGY;
2979 u16 mac_len, inner_net, inner_trans;
2982 if (skb_is_gso(skb) && !skb_is_gso_tcp(skb)) {
2983 /* udp gso_size delineates datagrams, only allow if fixed */
2984 if (!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) ||
2985 !(flags & BPF_F_ADJ_ROOM_FIXED_GSO))
2989 ret = skb_cow_head(skb, len_diff);
2990 if (unlikely(ret < 0))
2994 if (skb->protocol != htons(ETH_P_IP) &&
2995 skb->protocol != htons(ETH_P_IPV6))
2998 if (flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV4 &&
2999 flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV6)
3002 if (flags & BPF_F_ADJ_ROOM_ENCAP_L4_GRE &&
3003 flags & BPF_F_ADJ_ROOM_ENCAP_L4_UDP)
3006 if (skb->encapsulation)
3009 mac_len = skb->network_header - skb->mac_header;
3010 inner_net = skb->network_header;
3011 inner_trans = skb->transport_header;
3014 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
3015 if (unlikely(ret < 0))
3019 /* inner mac == inner_net on l3 encap */
3020 skb->inner_mac_header = inner_net;
3021 skb->inner_network_header = inner_net;
3022 skb->inner_transport_header = inner_trans;
3023 skb_set_inner_protocol(skb, skb->protocol);
3025 skb->encapsulation = 1;
3026 skb_set_network_header(skb, mac_len);
3028 if (flags & BPF_F_ADJ_ROOM_ENCAP_L4_UDP)
3029 gso_type |= SKB_GSO_UDP_TUNNEL;
3030 else if (flags & BPF_F_ADJ_ROOM_ENCAP_L4_GRE)
3031 gso_type |= SKB_GSO_GRE;
3032 else if (flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV6)
3033 gso_type |= SKB_GSO_IPXIP6;
3035 gso_type |= SKB_GSO_IPXIP4;
3037 if (flags & BPF_F_ADJ_ROOM_ENCAP_L4_GRE ||
3038 flags & BPF_F_ADJ_ROOM_ENCAP_L4_UDP) {
3039 int nh_len = flags & BPF_F_ADJ_ROOM_ENCAP_L3_IPV6 ?
3040 sizeof(struct ipv6hdr) :
3041 sizeof(struct iphdr);
3043 skb_set_transport_header(skb, mac_len + nh_len);
3047 if (skb_is_gso(skb)) {
3048 struct skb_shared_info *shinfo = skb_shinfo(skb);
3050 /* Due to header grow, MSS needs to be downgraded. */
3051 if (!(flags & BPF_F_ADJ_ROOM_FIXED_GSO))
3052 skb_decrease_gso_size(shinfo, len_diff);
3054 /* Header must be checked, and gso_segs recomputed. */
3055 shinfo->gso_type |= gso_type;
3056 shinfo->gso_segs = 0;
3062 static int bpf_skb_net_shrink(struct sk_buff *skb, u32 off, u32 len_diff,
3067 if (skb_is_gso(skb) && !skb_is_gso_tcp(skb)) {
3068 /* udp gso_size delineates datagrams, only allow if fixed */
3069 if (!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) ||
3070 !(flags & BPF_F_ADJ_ROOM_FIXED_GSO))
3074 ret = skb_unclone(skb, GFP_ATOMIC);
3075 if (unlikely(ret < 0))
3078 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
3079 if (unlikely(ret < 0))
3082 if (skb_is_gso(skb)) {
3083 struct skb_shared_info *shinfo = skb_shinfo(skb);
3085 /* Due to header shrink, MSS can be upgraded. */
3086 if (!(flags & BPF_F_ADJ_ROOM_FIXED_GSO))
3087 skb_increase_gso_size(shinfo, len_diff);
3089 /* Header must be checked, and gso_segs recomputed. */
3090 shinfo->gso_type |= SKB_GSO_DODGY;
3091 shinfo->gso_segs = 0;
3097 static u32 __bpf_skb_max_len(const struct sk_buff *skb)
3099 return skb->dev ? skb->dev->mtu + skb->dev->hard_header_len :
3103 BPF_CALL_4(bpf_skb_adjust_room, struct sk_buff *, skb, s32, len_diff,
3104 u32, mode, u64, flags)
3106 u32 len_cur, len_diff_abs = abs(len_diff);
3107 u32 len_min = bpf_skb_net_base_len(skb);
3108 u32 len_max = __bpf_skb_max_len(skb);
3109 __be16 proto = skb->protocol;
3110 bool shrink = len_diff < 0;
3114 if (unlikely(flags & ~BPF_F_ADJ_ROOM_MASK))
3116 if (unlikely(len_diff_abs > 0xfffU))
3118 if (unlikely(proto != htons(ETH_P_IP) &&
3119 proto != htons(ETH_P_IPV6)))
3122 off = skb_mac_header_len(skb);
3124 case BPF_ADJ_ROOM_NET:
3125 off += bpf_skb_net_base_len(skb);
3127 case BPF_ADJ_ROOM_MAC:
3133 len_cur = skb->len - skb_network_offset(skb);
3134 if ((shrink && (len_diff_abs >= len_cur ||
3135 len_cur - len_diff_abs < len_min)) ||
3136 (!shrink && (skb->len + len_diff_abs > len_max &&
3140 ret = shrink ? bpf_skb_net_shrink(skb, off, len_diff_abs, flags) :
3141 bpf_skb_net_grow(skb, off, len_diff_abs, flags);
3143 bpf_compute_data_pointers(skb);
3147 static const struct bpf_func_proto bpf_skb_adjust_room_proto = {
3148 .func = bpf_skb_adjust_room,
3150 .ret_type = RET_INTEGER,
3151 .arg1_type = ARG_PTR_TO_CTX,
3152 .arg2_type = ARG_ANYTHING,
3153 .arg3_type = ARG_ANYTHING,
3154 .arg4_type = ARG_ANYTHING,
3157 static u32 __bpf_skb_min_len(const struct sk_buff *skb)
3159 u32 min_len = skb_network_offset(skb);
3161 if (skb_transport_header_was_set(skb))
3162 min_len = skb_transport_offset(skb);
3163 if (skb->ip_summed == CHECKSUM_PARTIAL)
3164 min_len = skb_checksum_start_offset(skb) +
3165 skb->csum_offset + sizeof(__sum16);
3169 static int bpf_skb_grow_rcsum(struct sk_buff *skb, unsigned int new_len)
3171 unsigned int old_len = skb->len;
3174 ret = __skb_grow_rcsum(skb, new_len);
3176 memset(skb->data + old_len, 0, new_len - old_len);
3180 static int bpf_skb_trim_rcsum(struct sk_buff *skb, unsigned int new_len)
3182 return __skb_trim_rcsum(skb, new_len);
3185 static inline int __bpf_skb_change_tail(struct sk_buff *skb, u32 new_len,
3188 u32 max_len = __bpf_skb_max_len(skb);
3189 u32 min_len = __bpf_skb_min_len(skb);
3192 if (unlikely(flags || new_len > max_len || new_len < min_len))
3194 if (skb->encapsulation)
3197 /* The basic idea of this helper is that it's performing the
3198 * needed work to either grow or trim an skb, and eBPF program
3199 * rewrites the rest via helpers like bpf_skb_store_bytes(),
3200 * bpf_lX_csum_replace() and others rather than passing a raw
3201 * buffer here. This one is a slow path helper and intended
3202 * for replies with control messages.
3204 * Like in bpf_skb_change_proto(), we want to keep this rather
3205 * minimal and without protocol specifics so that we are able
3206 * to separate concerns as in bpf_skb_store_bytes() should only
3207 * be the one responsible for writing buffers.
3209 * It's really expected to be a slow path operation here for
3210 * control message replies, so we're implicitly linearizing,
3211 * uncloning and drop offloads from the skb by this.
3213 ret = __bpf_try_make_writable(skb, skb->len);
3215 if (new_len > skb->len)
3216 ret = bpf_skb_grow_rcsum(skb, new_len);
3217 else if (new_len < skb->len)
3218 ret = bpf_skb_trim_rcsum(skb, new_len);
3219 if (!ret && skb_is_gso(skb))
3225 BPF_CALL_3(bpf_skb_change_tail, struct sk_buff *, skb, u32, new_len,
3228 int ret = __bpf_skb_change_tail(skb, new_len, flags);
3230 bpf_compute_data_pointers(skb);
3234 static const struct bpf_func_proto bpf_skb_change_tail_proto = {
3235 .func = bpf_skb_change_tail,
3237 .ret_type = RET_INTEGER,
3238 .arg1_type = ARG_PTR_TO_CTX,
3239 .arg2_type = ARG_ANYTHING,
3240 .arg3_type = ARG_ANYTHING,
3243 BPF_CALL_3(sk_skb_change_tail, struct sk_buff *, skb, u32, new_len,
3246 int ret = __bpf_skb_change_tail(skb, new_len, flags);
3248 bpf_compute_data_end_sk_skb(skb);
3252 static const struct bpf_func_proto sk_skb_change_tail_proto = {
3253 .func = sk_skb_change_tail,
3255 .ret_type = RET_INTEGER,
3256 .arg1_type = ARG_PTR_TO_CTX,
3257 .arg2_type = ARG_ANYTHING,
3258 .arg3_type = ARG_ANYTHING,
3261 static inline int __bpf_skb_change_head(struct sk_buff *skb, u32 head_room,
3264 u32 max_len = __bpf_skb_max_len(skb);
3265 u32 new_len = skb->len + head_room;
3268 if (unlikely(flags || (!skb_is_gso(skb) && new_len > max_len) ||
3269 new_len < skb->len))
3272 ret = skb_cow(skb, head_room);
3274 /* Idea for this helper is that we currently only
3275 * allow to expand on mac header. This means that
3276 * skb->protocol network header, etc, stay as is.
3277 * Compared to bpf_skb_change_tail(), we're more
3278 * flexible due to not needing to linearize or
3279 * reset GSO. Intention for this helper is to be
3280 * used by an L3 skb that needs to push mac header
3281 * for redirection into L2 device.
3283 __skb_push(skb, head_room);
3284 memset(skb->data, 0, head_room);
3285 skb_reset_mac_header(skb);
3291 BPF_CALL_3(bpf_skb_change_head, struct sk_buff *, skb, u32, head_room,
3294 int ret = __bpf_skb_change_head(skb, head_room, flags);
3296 bpf_compute_data_pointers(skb);
3300 static const struct bpf_func_proto bpf_skb_change_head_proto = {
3301 .func = bpf_skb_change_head,
3303 .ret_type = RET_INTEGER,
3304 .arg1_type = ARG_PTR_TO_CTX,
3305 .arg2_type = ARG_ANYTHING,
3306 .arg3_type = ARG_ANYTHING,
3309 BPF_CALL_3(sk_skb_change_head, struct sk_buff *, skb, u32, head_room,
3312 int ret = __bpf_skb_change_head(skb, head_room, flags);
3314 bpf_compute_data_end_sk_skb(skb);
3318 static const struct bpf_func_proto sk_skb_change_head_proto = {
3319 .func = sk_skb_change_head,
3321 .ret_type = RET_INTEGER,
3322 .arg1_type = ARG_PTR_TO_CTX,
3323 .arg2_type = ARG_ANYTHING,
3324 .arg3_type = ARG_ANYTHING,
3326 static unsigned long xdp_get_metalen(const struct xdp_buff *xdp)
3328 return xdp_data_meta_unsupported(xdp) ? 0 :
3329 xdp->data - xdp->data_meta;
3332 BPF_CALL_2(bpf_xdp_adjust_head, struct xdp_buff *, xdp, int, offset)
3334 void *xdp_frame_end = xdp->data_hard_start + sizeof(struct xdp_frame);
3335 unsigned long metalen = xdp_get_metalen(xdp);
3336 void *data_start = xdp_frame_end + metalen;
3337 void *data = xdp->data + offset;
3339 if (unlikely(data < data_start ||
3340 data > xdp->data_end - ETH_HLEN))
3344 memmove(xdp->data_meta + offset,
3345 xdp->data_meta, metalen);
3346 xdp->data_meta += offset;
3352 static const struct bpf_func_proto bpf_xdp_adjust_head_proto = {
3353 .func = bpf_xdp_adjust_head,
3355 .ret_type = RET_INTEGER,
3356 .arg1_type = ARG_PTR_TO_CTX,
3357 .arg2_type = ARG_ANYTHING,
3360 BPF_CALL_2(bpf_xdp_adjust_tail, struct xdp_buff *, xdp, int, offset)
3362 void *data_end = xdp->data_end + offset;
3364 /* only shrinking is allowed for now. */
3365 if (unlikely(offset >= 0))
3368 if (unlikely(data_end < xdp->data + ETH_HLEN))
3371 xdp->data_end = data_end;
3376 static const struct bpf_func_proto bpf_xdp_adjust_tail_proto = {
3377 .func = bpf_xdp_adjust_tail,
3379 .ret_type = RET_INTEGER,
3380 .arg1_type = ARG_PTR_TO_CTX,
3381 .arg2_type = ARG_ANYTHING,
3384 BPF_CALL_2(bpf_xdp_adjust_meta, struct xdp_buff *, xdp, int, offset)
3386 void *xdp_frame_end = xdp->data_hard_start + sizeof(struct xdp_frame);
3387 void *meta = xdp->data_meta + offset;
3388 unsigned long metalen = xdp->data - meta;
3390 if (xdp_data_meta_unsupported(xdp))
3392 if (unlikely(meta < xdp_frame_end ||
3395 if (unlikely((metalen & (sizeof(__u32) - 1)) ||
3399 xdp->data_meta = meta;
3404 static const struct bpf_func_proto bpf_xdp_adjust_meta_proto = {
3405 .func = bpf_xdp_adjust_meta,
3407 .ret_type = RET_INTEGER,
3408 .arg1_type = ARG_PTR_TO_CTX,
3409 .arg2_type = ARG_ANYTHING,
3412 static int __bpf_tx_xdp(struct net_device *dev,
3413 struct bpf_map *map,
3414 struct xdp_buff *xdp,
3417 struct xdp_frame *xdpf;
3420 if (!dev->netdev_ops->ndo_xdp_xmit) {
3424 err = xdp_ok_fwd_dev(dev, xdp->data_end - xdp->data);
3428 xdpf = convert_to_xdp_frame(xdp);
3429 if (unlikely(!xdpf))
3432 sent = dev->netdev_ops->ndo_xdp_xmit(dev, 1, &xdpf, XDP_XMIT_FLUSH);
3439 xdp_do_redirect_slow(struct net_device *dev, struct xdp_buff *xdp,
3440 struct bpf_prog *xdp_prog, struct bpf_redirect_info *ri)
3442 struct net_device *fwd;
3443 u32 index = ri->ifindex;
3446 fwd = dev_get_by_index_rcu(dev_net(dev), index);
3448 if (unlikely(!fwd)) {
3453 err = __bpf_tx_xdp(fwd, NULL, xdp, 0);
3457 _trace_xdp_redirect(dev, xdp_prog, index);
3460 _trace_xdp_redirect_err(dev, xdp_prog, index, err);
3464 static int __bpf_tx_xdp_map(struct net_device *dev_rx, void *fwd,
3465 struct bpf_map *map,
3466 struct xdp_buff *xdp,
3471 switch (map->map_type) {
3472 case BPF_MAP_TYPE_DEVMAP: {
3473 struct bpf_dtab_netdev *dst = fwd;
3475 err = dev_map_enqueue(dst, xdp, dev_rx);
3478 __dev_map_insert_ctx(map, index);
3481 case BPF_MAP_TYPE_CPUMAP: {
3482 struct bpf_cpu_map_entry *rcpu = fwd;
3484 err = cpu_map_enqueue(rcpu, xdp, dev_rx);
3487 __cpu_map_insert_ctx(map, index);
3490 case BPF_MAP_TYPE_XSKMAP: {
3491 struct xdp_sock *xs = fwd;
3493 err = __xsk_map_redirect(map, xdp, xs);
3502 void xdp_do_flush_map(void)
3504 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
3505 struct bpf_map *map = ri->map_to_flush;
3507 ri->map_to_flush = NULL;
3509 switch (map->map_type) {
3510 case BPF_MAP_TYPE_DEVMAP:
3511 __dev_map_flush(map);
3513 case BPF_MAP_TYPE_CPUMAP:
3514 __cpu_map_flush(map);
3516 case BPF_MAP_TYPE_XSKMAP:
3517 __xsk_map_flush(map);
3524 EXPORT_SYMBOL_GPL(xdp_do_flush_map);
3526 static inline void *__xdp_map_lookup_elem(struct bpf_map *map, u32 index)
3528 switch (map->map_type) {
3529 case BPF_MAP_TYPE_DEVMAP:
3530 return __dev_map_lookup_elem(map, index);
3531 case BPF_MAP_TYPE_CPUMAP:
3532 return __cpu_map_lookup_elem(map, index);
3533 case BPF_MAP_TYPE_XSKMAP:
3534 return __xsk_map_lookup_elem(map, index);
3540 void bpf_clear_redirect_map(struct bpf_map *map)
3542 struct bpf_redirect_info *ri;
3545 for_each_possible_cpu(cpu) {
3546 ri = per_cpu_ptr(&bpf_redirect_info, cpu);
3547 /* Avoid polluting remote cacheline due to writes if
3548 * not needed. Once we pass this test, we need the
3549 * cmpxchg() to make sure it hasn't been changed in
3550 * the meantime by remote CPU.
3552 if (unlikely(READ_ONCE(ri->map) == map))
3553 cmpxchg(&ri->map, map, NULL);
3557 static int xdp_do_redirect_map(struct net_device *dev, struct xdp_buff *xdp,
3558 struct bpf_prog *xdp_prog, struct bpf_map *map,
3559 struct bpf_redirect_info *ri)
3561 u32 index = ri->ifindex;
3566 WRITE_ONCE(ri->map, NULL);
3568 fwd = __xdp_map_lookup_elem(map, index);
3569 if (unlikely(!fwd)) {
3573 if (ri->map_to_flush && unlikely(ri->map_to_flush != map))
3576 err = __bpf_tx_xdp_map(dev, fwd, map, xdp, index);
3580 ri->map_to_flush = map;
3581 _trace_xdp_redirect_map(dev, xdp_prog, fwd, map, index);
3584 _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map, index, err);
3588 int xdp_do_redirect(struct net_device *dev, struct xdp_buff *xdp,
3589 struct bpf_prog *xdp_prog)
3591 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
3592 struct bpf_map *map = READ_ONCE(ri->map);
3595 return xdp_do_redirect_map(dev, xdp, xdp_prog, map, ri);
3597 return xdp_do_redirect_slow(dev, xdp, xdp_prog, ri);
3599 EXPORT_SYMBOL_GPL(xdp_do_redirect);
3601 static int xdp_do_generic_redirect_map(struct net_device *dev,
3602 struct sk_buff *skb,
3603 struct xdp_buff *xdp,
3604 struct bpf_prog *xdp_prog,
3605 struct bpf_map *map)
3607 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
3608 u32 index = ri->ifindex;
3613 WRITE_ONCE(ri->map, NULL);
3615 fwd = __xdp_map_lookup_elem(map, index);
3616 if (unlikely(!fwd)) {
3621 if (map->map_type == BPF_MAP_TYPE_DEVMAP) {
3622 struct bpf_dtab_netdev *dst = fwd;
3624 err = dev_map_generic_redirect(dst, skb, xdp_prog);
3627 } else if (map->map_type == BPF_MAP_TYPE_XSKMAP) {
3628 struct xdp_sock *xs = fwd;
3630 err = xsk_generic_rcv(xs, xdp);
3635 /* TODO: Handle BPF_MAP_TYPE_CPUMAP */
3640 _trace_xdp_redirect_map(dev, xdp_prog, fwd, map, index);
3643 _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map, index, err);
3647 int xdp_do_generic_redirect(struct net_device *dev, struct sk_buff *skb,
3648 struct xdp_buff *xdp, struct bpf_prog *xdp_prog)
3650 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
3651 struct bpf_map *map = READ_ONCE(ri->map);
3652 u32 index = ri->ifindex;
3653 struct net_device *fwd;
3657 return xdp_do_generic_redirect_map(dev, skb, xdp, xdp_prog,
3660 fwd = dev_get_by_index_rcu(dev_net(dev), index);
3661 if (unlikely(!fwd)) {
3666 err = xdp_ok_fwd_dev(fwd, skb->len);
3671 _trace_xdp_redirect(dev, xdp_prog, index);
3672 generic_xdp_tx(skb, xdp_prog);
3675 _trace_xdp_redirect_err(dev, xdp_prog, index, err);
3678 EXPORT_SYMBOL_GPL(xdp_do_generic_redirect);
3680 BPF_CALL_2(bpf_xdp_redirect, u32, ifindex, u64, flags)
3682 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
3684 if (unlikely(flags))
3687 ri->ifindex = ifindex;
3689 WRITE_ONCE(ri->map, NULL);
3691 return XDP_REDIRECT;
3694 static const struct bpf_func_proto bpf_xdp_redirect_proto = {
3695 .func = bpf_xdp_redirect,
3697 .ret_type = RET_INTEGER,
3698 .arg1_type = ARG_ANYTHING,
3699 .arg2_type = ARG_ANYTHING,
3702 BPF_CALL_3(bpf_xdp_redirect_map, struct bpf_map *, map, u32, ifindex,
3705 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
3707 if (unlikely(flags))
3710 ri->ifindex = ifindex;
3712 WRITE_ONCE(ri->map, map);
3714 return XDP_REDIRECT;
3717 static const struct bpf_func_proto bpf_xdp_redirect_map_proto = {
3718 .func = bpf_xdp_redirect_map,
3720 .ret_type = RET_INTEGER,
3721 .arg1_type = ARG_CONST_MAP_PTR,
3722 .arg2_type = ARG_ANYTHING,
3723 .arg3_type = ARG_ANYTHING,
3726 static unsigned long bpf_skb_copy(void *dst_buff, const void *skb,
3727 unsigned long off, unsigned long len)
3729 void *ptr = skb_header_pointer(skb, off, len, dst_buff);
3733 if (ptr != dst_buff)
3734 memcpy(dst_buff, ptr, len);
3739 BPF_CALL_5(bpf_skb_event_output, struct sk_buff *, skb, struct bpf_map *, map,
3740 u64, flags, void *, meta, u64, meta_size)
3742 u64 skb_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
3744 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
3746 if (unlikely(skb_size > skb->len))
3749 return bpf_event_output(map, flags, meta, meta_size, skb, skb_size,
3753 static const struct bpf_func_proto bpf_skb_event_output_proto = {
3754 .func = bpf_skb_event_output,
3756 .ret_type = RET_INTEGER,
3757 .arg1_type = ARG_PTR_TO_CTX,
3758 .arg2_type = ARG_CONST_MAP_PTR,
3759 .arg3_type = ARG_ANYTHING,
3760 .arg4_type = ARG_PTR_TO_MEM,
3761 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
3764 static unsigned short bpf_tunnel_key_af(u64 flags)
3766 return flags & BPF_F_TUNINFO_IPV6 ? AF_INET6 : AF_INET;
3769 BPF_CALL_4(bpf_skb_get_tunnel_key, struct sk_buff *, skb, struct bpf_tunnel_key *, to,
3770 u32, size, u64, flags)
3772 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
3773 u8 compat[sizeof(struct bpf_tunnel_key)];
3777 if (unlikely(!info || (flags & ~(BPF_F_TUNINFO_IPV6)))) {
3781 if (ip_tunnel_info_af(info) != bpf_tunnel_key_af(flags)) {
3785 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
3788 case offsetof(struct bpf_tunnel_key, tunnel_label):
3789 case offsetof(struct bpf_tunnel_key, tunnel_ext):
3791 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
3792 /* Fixup deprecated structure layouts here, so we have
3793 * a common path later on.
3795 if (ip_tunnel_info_af(info) != AF_INET)
3798 to = (struct bpf_tunnel_key *)compat;
3805 to->tunnel_id = be64_to_cpu(info->key.tun_id);
3806 to->tunnel_tos = info->key.tos;
3807 to->tunnel_ttl = info->key.ttl;
3810 if (flags & BPF_F_TUNINFO_IPV6) {
3811 memcpy(to->remote_ipv6, &info->key.u.ipv6.src,
3812 sizeof(to->remote_ipv6));
3813 to->tunnel_label = be32_to_cpu(info->key.label);
3815 to->remote_ipv4 = be32_to_cpu(info->key.u.ipv4.src);
3816 memset(&to->remote_ipv6[1], 0, sizeof(__u32) * 3);
3817 to->tunnel_label = 0;
3820 if (unlikely(size != sizeof(struct bpf_tunnel_key)))
3821 memcpy(to_orig, to, size);
3825 memset(to_orig, 0, size);
3829 static const struct bpf_func_proto bpf_skb_get_tunnel_key_proto = {
3830 .func = bpf_skb_get_tunnel_key,
3832 .ret_type = RET_INTEGER,
3833 .arg1_type = ARG_PTR_TO_CTX,
3834 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
3835 .arg3_type = ARG_CONST_SIZE,
3836 .arg4_type = ARG_ANYTHING,
3839 BPF_CALL_3(bpf_skb_get_tunnel_opt, struct sk_buff *, skb, u8 *, to, u32, size)
3841 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
3844 if (unlikely(!info ||
3845 !(info->key.tun_flags & TUNNEL_OPTIONS_PRESENT))) {
3849 if (unlikely(size < info->options_len)) {
3854 ip_tunnel_info_opts_get(to, info);
3855 if (size > info->options_len)
3856 memset(to + info->options_len, 0, size - info->options_len);
3858 return info->options_len;
3860 memset(to, 0, size);
3864 static const struct bpf_func_proto bpf_skb_get_tunnel_opt_proto = {
3865 .func = bpf_skb_get_tunnel_opt,
3867 .ret_type = RET_INTEGER,
3868 .arg1_type = ARG_PTR_TO_CTX,
3869 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
3870 .arg3_type = ARG_CONST_SIZE,
3873 static struct metadata_dst __percpu *md_dst;
3875 BPF_CALL_4(bpf_skb_set_tunnel_key, struct sk_buff *, skb,
3876 const struct bpf_tunnel_key *, from, u32, size, u64, flags)
3878 struct metadata_dst *md = this_cpu_ptr(md_dst);
3879 u8 compat[sizeof(struct bpf_tunnel_key)];
3880 struct ip_tunnel_info *info;
3882 if (unlikely(flags & ~(BPF_F_TUNINFO_IPV6 | BPF_F_ZERO_CSUM_TX |
3883 BPF_F_DONT_FRAGMENT | BPF_F_SEQ_NUMBER)))
3885 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
3887 case offsetof(struct bpf_tunnel_key, tunnel_label):
3888 case offsetof(struct bpf_tunnel_key, tunnel_ext):
3889 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
3890 /* Fixup deprecated structure layouts here, so we have
3891 * a common path later on.
3893 memcpy(compat, from, size);
3894 memset(compat + size, 0, sizeof(compat) - size);
3895 from = (const struct bpf_tunnel_key *) compat;
3901 if (unlikely((!(flags & BPF_F_TUNINFO_IPV6) && from->tunnel_label) ||
3906 dst_hold((struct dst_entry *) md);
3907 skb_dst_set(skb, (struct dst_entry *) md);
3909 info = &md->u.tun_info;
3910 memset(info, 0, sizeof(*info));
3911 info->mode = IP_TUNNEL_INFO_TX;
3913 info->key.tun_flags = TUNNEL_KEY | TUNNEL_CSUM | TUNNEL_NOCACHE;
3914 if (flags & BPF_F_DONT_FRAGMENT)
3915 info->key.tun_flags |= TUNNEL_DONT_FRAGMENT;
3916 if (flags & BPF_F_ZERO_CSUM_TX)
3917 info->key.tun_flags &= ~TUNNEL_CSUM;
3918 if (flags & BPF_F_SEQ_NUMBER)
3919 info->key.tun_flags |= TUNNEL_SEQ;
3921 info->key.tun_id = cpu_to_be64(from->tunnel_id);
3922 info->key.tos = from->tunnel_tos;
3923 info->key.ttl = from->tunnel_ttl;
3925 if (flags & BPF_F_TUNINFO_IPV6) {
3926 info->mode |= IP_TUNNEL_INFO_IPV6;
3927 memcpy(&info->key.u.ipv6.dst, from->remote_ipv6,
3928 sizeof(from->remote_ipv6));
3929 info->key.label = cpu_to_be32(from->tunnel_label) &
3930 IPV6_FLOWLABEL_MASK;
3932 info->key.u.ipv4.dst = cpu_to_be32(from->remote_ipv4);
3938 static const struct bpf_func_proto bpf_skb_set_tunnel_key_proto = {
3939 .func = bpf_skb_set_tunnel_key,
3941 .ret_type = RET_INTEGER,
3942 .arg1_type = ARG_PTR_TO_CTX,
3943 .arg2_type = ARG_PTR_TO_MEM,
3944 .arg3_type = ARG_CONST_SIZE,
3945 .arg4_type = ARG_ANYTHING,
3948 BPF_CALL_3(bpf_skb_set_tunnel_opt, struct sk_buff *, skb,
3949 const u8 *, from, u32, size)
3951 struct ip_tunnel_info *info = skb_tunnel_info(skb);
3952 const struct metadata_dst *md = this_cpu_ptr(md_dst);
3954 if (unlikely(info != &md->u.tun_info || (size & (sizeof(u32) - 1))))
3956 if (unlikely(size > IP_TUNNEL_OPTS_MAX))
3959 ip_tunnel_info_opts_set(info, from, size, TUNNEL_OPTIONS_PRESENT);
3964 static const struct bpf_func_proto bpf_skb_set_tunnel_opt_proto = {
3965 .func = bpf_skb_set_tunnel_opt,
3967 .ret_type = RET_INTEGER,
3968 .arg1_type = ARG_PTR_TO_CTX,
3969 .arg2_type = ARG_PTR_TO_MEM,
3970 .arg3_type = ARG_CONST_SIZE,
3973 static const struct bpf_func_proto *
3974 bpf_get_skb_set_tunnel_proto(enum bpf_func_id which)
3977 struct metadata_dst __percpu *tmp;
3979 tmp = metadata_dst_alloc_percpu(IP_TUNNEL_OPTS_MAX,
3984 if (cmpxchg(&md_dst, NULL, tmp))
3985 metadata_dst_free_percpu(tmp);
3989 case BPF_FUNC_skb_set_tunnel_key:
3990 return &bpf_skb_set_tunnel_key_proto;
3991 case BPF_FUNC_skb_set_tunnel_opt:
3992 return &bpf_skb_set_tunnel_opt_proto;
3998 BPF_CALL_3(bpf_skb_under_cgroup, struct sk_buff *, skb, struct bpf_map *, map,
4001 struct bpf_array *array = container_of(map, struct bpf_array, map);
4002 struct cgroup *cgrp;
4005 sk = skb_to_full_sk(skb);
4006 if (!sk || !sk_fullsock(sk))
4008 if (unlikely(idx >= array->map.max_entries))
4011 cgrp = READ_ONCE(array->ptrs[idx]);
4012 if (unlikely(!cgrp))
4015 return sk_under_cgroup_hierarchy(sk, cgrp);
4018 static const struct bpf_func_proto bpf_skb_under_cgroup_proto = {
4019 .func = bpf_skb_under_cgroup,
4021 .ret_type = RET_INTEGER,
4022 .arg1_type = ARG_PTR_TO_CTX,
4023 .arg2_type = ARG_CONST_MAP_PTR,
4024 .arg3_type = ARG_ANYTHING,
4027 #ifdef CONFIG_SOCK_CGROUP_DATA
4028 BPF_CALL_1(bpf_skb_cgroup_id, const struct sk_buff *, skb)
4030 struct sock *sk = skb_to_full_sk(skb);
4031 struct cgroup *cgrp;
4033 if (!sk || !sk_fullsock(sk))
4036 cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
4037 return cgrp->kn->id.id;
4040 static const struct bpf_func_proto bpf_skb_cgroup_id_proto = {
4041 .func = bpf_skb_cgroup_id,
4043 .ret_type = RET_INTEGER,
4044 .arg1_type = ARG_PTR_TO_CTX,
4047 BPF_CALL_2(bpf_skb_ancestor_cgroup_id, const struct sk_buff *, skb, int,
4050 struct sock *sk = skb_to_full_sk(skb);
4051 struct cgroup *ancestor;
4052 struct cgroup *cgrp;
4054 if (!sk || !sk_fullsock(sk))
4057 cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
4058 ancestor = cgroup_ancestor(cgrp, ancestor_level);
4062 return ancestor->kn->id.id;
4065 static const struct bpf_func_proto bpf_skb_ancestor_cgroup_id_proto = {
4066 .func = bpf_skb_ancestor_cgroup_id,
4068 .ret_type = RET_INTEGER,
4069 .arg1_type = ARG_PTR_TO_CTX,
4070 .arg2_type = ARG_ANYTHING,
4074 static unsigned long bpf_xdp_copy(void *dst_buff, const void *src_buff,
4075 unsigned long off, unsigned long len)
4077 memcpy(dst_buff, src_buff + off, len);
4081 BPF_CALL_5(bpf_xdp_event_output, struct xdp_buff *, xdp, struct bpf_map *, map,
4082 u64, flags, void *, meta, u64, meta_size)
4084 u64 xdp_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
4086 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
4088 if (unlikely(xdp_size > (unsigned long)(xdp->data_end - xdp->data)))
4091 return bpf_event_output(map, flags, meta, meta_size, xdp->data,
4092 xdp_size, bpf_xdp_copy);
4095 static const struct bpf_func_proto bpf_xdp_event_output_proto = {
4096 .func = bpf_xdp_event_output,
4098 .ret_type = RET_INTEGER,
4099 .arg1_type = ARG_PTR_TO_CTX,
4100 .arg2_type = ARG_CONST_MAP_PTR,
4101 .arg3_type = ARG_ANYTHING,
4102 .arg4_type = ARG_PTR_TO_MEM,
4103 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
4106 BPF_CALL_1(bpf_get_socket_cookie, struct sk_buff *, skb)
4108 return skb->sk ? sock_gen_cookie(skb->sk) : 0;
4111 static const struct bpf_func_proto bpf_get_socket_cookie_proto = {
4112 .func = bpf_get_socket_cookie,
4114 .ret_type = RET_INTEGER,
4115 .arg1_type = ARG_PTR_TO_CTX,
4118 BPF_CALL_1(bpf_get_socket_cookie_sock_addr, struct bpf_sock_addr_kern *, ctx)
4120 return sock_gen_cookie(ctx->sk);
4123 static const struct bpf_func_proto bpf_get_socket_cookie_sock_addr_proto = {
4124 .func = bpf_get_socket_cookie_sock_addr,
4126 .ret_type = RET_INTEGER,
4127 .arg1_type = ARG_PTR_TO_CTX,
4130 BPF_CALL_1(bpf_get_socket_cookie_sock_ops, struct bpf_sock_ops_kern *, ctx)
4132 return sock_gen_cookie(ctx->sk);
4135 static const struct bpf_func_proto bpf_get_socket_cookie_sock_ops_proto = {
4136 .func = bpf_get_socket_cookie_sock_ops,
4138 .ret_type = RET_INTEGER,
4139 .arg1_type = ARG_PTR_TO_CTX,
4142 BPF_CALL_1(bpf_get_socket_uid, struct sk_buff *, skb)
4144 struct sock *sk = sk_to_full_sk(skb->sk);
4147 if (!sk || !sk_fullsock(sk))
4149 kuid = sock_net_uid(sock_net(sk), sk);
4150 return from_kuid_munged(sock_net(sk)->user_ns, kuid);
4153 static const struct bpf_func_proto bpf_get_socket_uid_proto = {
4154 .func = bpf_get_socket_uid,
4156 .ret_type = RET_INTEGER,
4157 .arg1_type = ARG_PTR_TO_CTX,
4160 BPF_CALL_5(bpf_sockopt_event_output, struct bpf_sock_ops_kern *, bpf_sock,
4161 struct bpf_map *, map, u64, flags, void *, data, u64, size)
4163 if (unlikely(flags & ~(BPF_F_INDEX_MASK)))
4166 return bpf_event_output(map, flags, data, size, NULL, 0, NULL);
4169 static const struct bpf_func_proto bpf_sockopt_event_output_proto = {
4170 .func = bpf_sockopt_event_output,
4172 .ret_type = RET_INTEGER,
4173 .arg1_type = ARG_PTR_TO_CTX,
4174 .arg2_type = ARG_CONST_MAP_PTR,
4175 .arg3_type = ARG_ANYTHING,
4176 .arg4_type = ARG_PTR_TO_MEM,
4177 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
4180 BPF_CALL_5(bpf_setsockopt, struct bpf_sock_ops_kern *, bpf_sock,
4181 int, level, int, optname, char *, optval, int, optlen)
4183 struct sock *sk = bpf_sock->sk;
4187 if (!sk_fullsock(sk))
4190 if (level == SOL_SOCKET) {
4191 if (optlen != sizeof(int))
4193 val = *((int *)optval);
4195 /* Only some socketops are supported */
4198 val = min_t(u32, val, sysctl_rmem_max);
4199 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
4200 sk->sk_rcvbuf = max_t(int, val * 2, SOCK_MIN_RCVBUF);
4203 val = min_t(u32, val, sysctl_wmem_max);
4204 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
4205 sk->sk_sndbuf = max_t(int, val * 2, SOCK_MIN_SNDBUF);
4207 case SO_MAX_PACING_RATE: /* 32bit version */
4209 cmpxchg(&sk->sk_pacing_status,
4212 sk->sk_max_pacing_rate = (val == ~0U) ? ~0UL : val;
4213 sk->sk_pacing_rate = min(sk->sk_pacing_rate,
4214 sk->sk_max_pacing_rate);
4217 sk->sk_priority = val;
4222 sk->sk_rcvlowat = val ? : 1;
4225 if (sk->sk_mark != val) {
4234 } else if (level == SOL_IP) {
4235 if (optlen != sizeof(int) || sk->sk_family != AF_INET)
4238 val = *((int *)optval);
4239 /* Only some options are supported */
4242 if (val < -1 || val > 0xff) {
4245 struct inet_sock *inet = inet_sk(sk);
4255 #if IS_ENABLED(CONFIG_IPV6)
4256 } else if (level == SOL_IPV6) {
4257 if (optlen != sizeof(int) || sk->sk_family != AF_INET6)
4260 val = *((int *)optval);
4261 /* Only some options are supported */
4264 if (val < -1 || val > 0xff) {
4267 struct ipv6_pinfo *np = inet6_sk(sk);
4278 } else if (level == SOL_TCP &&
4279 sk->sk_prot->setsockopt == tcp_setsockopt) {
4280 if (optname == TCP_CONGESTION) {
4281 char name[TCP_CA_NAME_MAX];
4282 bool reinit = bpf_sock->op > BPF_SOCK_OPS_NEEDS_ECN;
4284 strncpy(name, optval, min_t(long, optlen,
4285 TCP_CA_NAME_MAX-1));
4286 name[TCP_CA_NAME_MAX-1] = 0;
4287 ret = tcp_set_congestion_control(sk, name, false,
4290 struct tcp_sock *tp = tcp_sk(sk);
4292 if (optlen != sizeof(int))
4295 val = *((int *)optval);
4296 /* Only some options are supported */
4299 if (val <= 0 || tp->data_segs_out > tp->syn_data)
4304 case TCP_BPF_SNDCWND_CLAMP:
4308 tp->snd_cwnd_clamp = val;
4309 tp->snd_ssthresh = val;
4313 if (val < 0 || val > 1)
4329 static const struct bpf_func_proto bpf_setsockopt_proto = {
4330 .func = bpf_setsockopt,
4332 .ret_type = RET_INTEGER,
4333 .arg1_type = ARG_PTR_TO_CTX,
4334 .arg2_type = ARG_ANYTHING,
4335 .arg3_type = ARG_ANYTHING,
4336 .arg4_type = ARG_PTR_TO_MEM,
4337 .arg5_type = ARG_CONST_SIZE,
4340 BPF_CALL_5(bpf_getsockopt, struct bpf_sock_ops_kern *, bpf_sock,
4341 int, level, int, optname, char *, optval, int, optlen)
4343 struct sock *sk = bpf_sock->sk;
4345 if (!sk_fullsock(sk))
4348 if (level == SOL_TCP && sk->sk_prot->getsockopt == tcp_getsockopt) {
4349 struct inet_connection_sock *icsk;
4350 struct tcp_sock *tp;
4353 case TCP_CONGESTION:
4354 icsk = inet_csk(sk);
4356 if (!icsk->icsk_ca_ops || optlen <= 1)
4358 strncpy(optval, icsk->icsk_ca_ops->name, optlen);
4359 optval[optlen - 1] = 0;
4364 if (optlen <= 0 || !tp->saved_syn ||
4365 optlen > tp->saved_syn[0])
4367 memcpy(optval, tp->saved_syn + 1, optlen);
4372 } else if (level == SOL_IP) {
4373 struct inet_sock *inet = inet_sk(sk);
4375 if (optlen != sizeof(int) || sk->sk_family != AF_INET)
4378 /* Only some options are supported */
4381 *((int *)optval) = (int)inet->tos;
4386 #if IS_ENABLED(CONFIG_IPV6)
4387 } else if (level == SOL_IPV6) {
4388 struct ipv6_pinfo *np = inet6_sk(sk);
4390 if (optlen != sizeof(int) || sk->sk_family != AF_INET6)
4393 /* Only some options are supported */
4396 *((int *)optval) = (int)np->tclass;
4408 memset(optval, 0, optlen);
4412 static const struct bpf_func_proto bpf_getsockopt_proto = {
4413 .func = bpf_getsockopt,
4415 .ret_type = RET_INTEGER,
4416 .arg1_type = ARG_PTR_TO_CTX,
4417 .arg2_type = ARG_ANYTHING,
4418 .arg3_type = ARG_ANYTHING,
4419 .arg4_type = ARG_PTR_TO_UNINIT_MEM,
4420 .arg5_type = ARG_CONST_SIZE,
4423 BPF_CALL_2(bpf_sock_ops_cb_flags_set, struct bpf_sock_ops_kern *, bpf_sock,
4426 struct sock *sk = bpf_sock->sk;
4427 int val = argval & BPF_SOCK_OPS_ALL_CB_FLAGS;
4429 if (!IS_ENABLED(CONFIG_INET) || !sk_fullsock(sk))
4433 tcp_sk(sk)->bpf_sock_ops_cb_flags = val;
4435 return argval & (~BPF_SOCK_OPS_ALL_CB_FLAGS);
4438 static const struct bpf_func_proto bpf_sock_ops_cb_flags_set_proto = {
4439 .func = bpf_sock_ops_cb_flags_set,
4441 .ret_type = RET_INTEGER,
4442 .arg1_type = ARG_PTR_TO_CTX,
4443 .arg2_type = ARG_ANYTHING,
4446 const struct ipv6_bpf_stub *ipv6_bpf_stub __read_mostly;
4447 EXPORT_SYMBOL_GPL(ipv6_bpf_stub);
4449 BPF_CALL_3(bpf_bind, struct bpf_sock_addr_kern *, ctx, struct sockaddr *, addr,
4453 struct sock *sk = ctx->sk;
4456 /* Binding to port can be expensive so it's prohibited in the helper.
4457 * Only binding to IP is supported.
4460 if (addr->sa_family == AF_INET) {
4461 if (addr_len < sizeof(struct sockaddr_in))
4463 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
4465 return __inet_bind(sk, addr, addr_len, true, false);
4466 #if IS_ENABLED(CONFIG_IPV6)
4467 } else if (addr->sa_family == AF_INET6) {
4468 if (addr_len < SIN6_LEN_RFC2133)
4470 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
4472 /* ipv6_bpf_stub cannot be NULL, since it's called from
4473 * bpf_cgroup_inet6_connect hook and ipv6 is already loaded
4475 return ipv6_bpf_stub->inet6_bind(sk, addr, addr_len, true, false);
4476 #endif /* CONFIG_IPV6 */
4478 #endif /* CONFIG_INET */
4480 return -EAFNOSUPPORT;
4483 static const struct bpf_func_proto bpf_bind_proto = {
4486 .ret_type = RET_INTEGER,
4487 .arg1_type = ARG_PTR_TO_CTX,
4488 .arg2_type = ARG_PTR_TO_MEM,
4489 .arg3_type = ARG_CONST_SIZE,
4493 BPF_CALL_5(bpf_skb_get_xfrm_state, struct sk_buff *, skb, u32, index,
4494 struct bpf_xfrm_state *, to, u32, size, u64, flags)
4496 const struct sec_path *sp = skb_sec_path(skb);
4497 const struct xfrm_state *x;
4499 if (!sp || unlikely(index >= sp->len || flags))
4502 x = sp->xvec[index];
4504 if (unlikely(size != sizeof(struct bpf_xfrm_state)))
4507 to->reqid = x->props.reqid;
4508 to->spi = x->id.spi;
4509 to->family = x->props.family;
4512 if (to->family == AF_INET6) {
4513 memcpy(to->remote_ipv6, x->props.saddr.a6,
4514 sizeof(to->remote_ipv6));
4516 to->remote_ipv4 = x->props.saddr.a4;
4517 memset(&to->remote_ipv6[1], 0, sizeof(__u32) * 3);
4522 memset(to, 0, size);
4526 static const struct bpf_func_proto bpf_skb_get_xfrm_state_proto = {
4527 .func = bpf_skb_get_xfrm_state,
4529 .ret_type = RET_INTEGER,
4530 .arg1_type = ARG_PTR_TO_CTX,
4531 .arg2_type = ARG_ANYTHING,
4532 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
4533 .arg4_type = ARG_CONST_SIZE,
4534 .arg5_type = ARG_ANYTHING,
4538 #if IS_ENABLED(CONFIG_INET) || IS_ENABLED(CONFIG_IPV6)
4539 static int bpf_fib_set_fwd_params(struct bpf_fib_lookup *params,
4540 const struct neighbour *neigh,
4541 const struct net_device *dev)
4543 memcpy(params->dmac, neigh->ha, ETH_ALEN);
4544 memcpy(params->smac, dev->dev_addr, ETH_ALEN);
4545 params->h_vlan_TCI = 0;
4546 params->h_vlan_proto = 0;
4547 params->ifindex = dev->ifindex;
4553 #if IS_ENABLED(CONFIG_INET)
4554 static int bpf_ipv4_fib_lookup(struct net *net, struct bpf_fib_lookup *params,
4555 u32 flags, bool check_mtu)
4557 struct in_device *in_dev;
4558 struct neighbour *neigh;
4559 struct net_device *dev;
4560 struct fib_result res;
4566 dev = dev_get_by_index_rcu(net, params->ifindex);
4570 /* verify forwarding is enabled on this interface */
4571 in_dev = __in_dev_get_rcu(dev);
4572 if (unlikely(!in_dev || !IN_DEV_FORWARD(in_dev)))
4573 return BPF_FIB_LKUP_RET_FWD_DISABLED;
4575 if (flags & BPF_FIB_LOOKUP_OUTPUT) {
4577 fl4.flowi4_oif = params->ifindex;
4579 fl4.flowi4_iif = params->ifindex;
4582 fl4.flowi4_tos = params->tos & IPTOS_RT_MASK;
4583 fl4.flowi4_scope = RT_SCOPE_UNIVERSE;
4584 fl4.flowi4_flags = 0;
4586 fl4.flowi4_proto = params->l4_protocol;
4587 fl4.daddr = params->ipv4_dst;
4588 fl4.saddr = params->ipv4_src;
4589 fl4.fl4_sport = params->sport;
4590 fl4.fl4_dport = params->dport;
4592 if (flags & BPF_FIB_LOOKUP_DIRECT) {
4593 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
4594 struct fib_table *tb;
4596 tb = fib_get_table(net, tbid);
4598 return BPF_FIB_LKUP_RET_NOT_FWDED;
4600 err = fib_table_lookup(tb, &fl4, &res, FIB_LOOKUP_NOREF);
4602 fl4.flowi4_mark = 0;
4603 fl4.flowi4_secid = 0;
4604 fl4.flowi4_tun_key.tun_id = 0;
4605 fl4.flowi4_uid = sock_net_uid(net, NULL);
4607 err = fib_lookup(net, &fl4, &res, FIB_LOOKUP_NOREF);
4611 /* map fib lookup errors to RTN_ type */
4613 return BPF_FIB_LKUP_RET_BLACKHOLE;
4614 if (err == -EHOSTUNREACH)
4615 return BPF_FIB_LKUP_RET_UNREACHABLE;
4617 return BPF_FIB_LKUP_RET_PROHIBIT;
4619 return BPF_FIB_LKUP_RET_NOT_FWDED;
4622 if (res.type != RTN_UNICAST)
4623 return BPF_FIB_LKUP_RET_NOT_FWDED;
4625 if (res.fi->fib_nhs > 1)
4626 fib_select_path(net, &res, &fl4, NULL);
4629 mtu = ip_mtu_from_fib_result(&res, params->ipv4_dst);
4630 if (params->tot_len > mtu)
4631 return BPF_FIB_LKUP_RET_FRAG_NEEDED;
4634 nh = &res.fi->fib_nh[res.nh_sel];
4636 /* do not handle lwt encaps right now */
4637 if (nh->nh_lwtstate)
4638 return BPF_FIB_LKUP_RET_UNSUPP_LWT;
4642 params->ipv4_dst = nh->nh_gw;
4644 params->rt_metric = res.fi->fib_priority;
4646 /* xdp and cls_bpf programs are run in RCU-bh so
4647 * rcu_read_lock_bh is not needed here
4649 neigh = __ipv4_neigh_lookup_noref(dev, (__force u32)params->ipv4_dst);
4651 return BPF_FIB_LKUP_RET_NO_NEIGH;
4653 return bpf_fib_set_fwd_params(params, neigh, dev);
4657 #if IS_ENABLED(CONFIG_IPV6)
4658 static int bpf_ipv6_fib_lookup(struct net *net, struct bpf_fib_lookup *params,
4659 u32 flags, bool check_mtu)
4661 struct in6_addr *src = (struct in6_addr *) params->ipv6_src;
4662 struct in6_addr *dst = (struct in6_addr *) params->ipv6_dst;
4663 struct neighbour *neigh;
4664 struct net_device *dev;
4665 struct inet6_dev *idev;
4666 struct fib6_info *f6i;
4672 /* link local addresses are never forwarded */
4673 if (rt6_need_strict(dst) || rt6_need_strict(src))
4674 return BPF_FIB_LKUP_RET_NOT_FWDED;
4676 dev = dev_get_by_index_rcu(net, params->ifindex);
4680 idev = __in6_dev_get_safely(dev);
4681 if (unlikely(!idev || !net->ipv6.devconf_all->forwarding))
4682 return BPF_FIB_LKUP_RET_FWD_DISABLED;
4684 if (flags & BPF_FIB_LOOKUP_OUTPUT) {
4686 oif = fl6.flowi6_oif = params->ifindex;
4688 oif = fl6.flowi6_iif = params->ifindex;
4690 strict = RT6_LOOKUP_F_HAS_SADDR;
4692 fl6.flowlabel = params->flowinfo;
4693 fl6.flowi6_scope = 0;
4694 fl6.flowi6_flags = 0;
4697 fl6.flowi6_proto = params->l4_protocol;
4700 fl6.fl6_sport = params->sport;
4701 fl6.fl6_dport = params->dport;
4703 if (flags & BPF_FIB_LOOKUP_DIRECT) {
4704 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
4705 struct fib6_table *tb;
4707 tb = ipv6_stub->fib6_get_table(net, tbid);
4709 return BPF_FIB_LKUP_RET_NOT_FWDED;
4711 f6i = ipv6_stub->fib6_table_lookup(net, tb, oif, &fl6, strict);
4713 fl6.flowi6_mark = 0;
4714 fl6.flowi6_secid = 0;
4715 fl6.flowi6_tun_key.tun_id = 0;
4716 fl6.flowi6_uid = sock_net_uid(net, NULL);
4718 f6i = ipv6_stub->fib6_lookup(net, oif, &fl6, strict);
4721 if (unlikely(IS_ERR_OR_NULL(f6i) || f6i == net->ipv6.fib6_null_entry))
4722 return BPF_FIB_LKUP_RET_NOT_FWDED;
4724 if (unlikely(f6i->fib6_flags & RTF_REJECT)) {
4725 switch (f6i->fib6_type) {
4727 return BPF_FIB_LKUP_RET_BLACKHOLE;
4728 case RTN_UNREACHABLE:
4729 return BPF_FIB_LKUP_RET_UNREACHABLE;
4731 return BPF_FIB_LKUP_RET_PROHIBIT;
4733 return BPF_FIB_LKUP_RET_NOT_FWDED;
4737 if (f6i->fib6_type != RTN_UNICAST)
4738 return BPF_FIB_LKUP_RET_NOT_FWDED;
4740 if (f6i->fib6_nsiblings && fl6.flowi6_oif == 0)
4741 f6i = ipv6_stub->fib6_multipath_select(net, f6i, &fl6,
4742 fl6.flowi6_oif, NULL,
4746 mtu = ipv6_stub->ip6_mtu_from_fib6(f6i, dst, src);
4747 if (params->tot_len > mtu)
4748 return BPF_FIB_LKUP_RET_FRAG_NEEDED;
4751 if (f6i->fib6_nh.nh_lwtstate)
4752 return BPF_FIB_LKUP_RET_UNSUPP_LWT;
4754 if (f6i->fib6_flags & RTF_GATEWAY)
4755 *dst = f6i->fib6_nh.nh_gw;
4757 dev = f6i->fib6_nh.nh_dev;
4758 params->rt_metric = f6i->fib6_metric;
4760 /* xdp and cls_bpf programs are run in RCU-bh so rcu_read_lock_bh is
4761 * not needed here. Can not use __ipv6_neigh_lookup_noref here
4762 * because we need to get nd_tbl via the stub
4764 neigh = ___neigh_lookup_noref(ipv6_stub->nd_tbl, neigh_key_eq128,
4765 ndisc_hashfn, dst, dev);
4767 return BPF_FIB_LKUP_RET_NO_NEIGH;
4769 return bpf_fib_set_fwd_params(params, neigh, dev);
4773 BPF_CALL_4(bpf_xdp_fib_lookup, struct xdp_buff *, ctx,
4774 struct bpf_fib_lookup *, params, int, plen, u32, flags)
4776 if (plen < sizeof(*params))
4779 if (flags & ~(BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_OUTPUT))
4782 switch (params->family) {
4783 #if IS_ENABLED(CONFIG_INET)
4785 return bpf_ipv4_fib_lookup(dev_net(ctx->rxq->dev), params,
4788 #if IS_ENABLED(CONFIG_IPV6)
4790 return bpf_ipv6_fib_lookup(dev_net(ctx->rxq->dev), params,
4794 return -EAFNOSUPPORT;
4797 static const struct bpf_func_proto bpf_xdp_fib_lookup_proto = {
4798 .func = bpf_xdp_fib_lookup,
4800 .ret_type = RET_INTEGER,
4801 .arg1_type = ARG_PTR_TO_CTX,
4802 .arg2_type = ARG_PTR_TO_MEM,
4803 .arg3_type = ARG_CONST_SIZE,
4804 .arg4_type = ARG_ANYTHING,
4807 BPF_CALL_4(bpf_skb_fib_lookup, struct sk_buff *, skb,
4808 struct bpf_fib_lookup *, params, int, plen, u32, flags)
4810 struct net *net = dev_net(skb->dev);
4811 int rc = -EAFNOSUPPORT;
4813 if (plen < sizeof(*params))
4816 if (flags & ~(BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_OUTPUT))
4819 switch (params->family) {
4820 #if IS_ENABLED(CONFIG_INET)
4822 rc = bpf_ipv4_fib_lookup(net, params, flags, false);
4825 #if IS_ENABLED(CONFIG_IPV6)
4827 rc = bpf_ipv6_fib_lookup(net, params, flags, false);
4833 struct net_device *dev;
4835 dev = dev_get_by_index_rcu(net, params->ifindex);
4836 if (!is_skb_forwardable(dev, skb))
4837 rc = BPF_FIB_LKUP_RET_FRAG_NEEDED;
4843 static const struct bpf_func_proto bpf_skb_fib_lookup_proto = {
4844 .func = bpf_skb_fib_lookup,
4846 .ret_type = RET_INTEGER,
4847 .arg1_type = ARG_PTR_TO_CTX,
4848 .arg2_type = ARG_PTR_TO_MEM,
4849 .arg3_type = ARG_CONST_SIZE,
4850 .arg4_type = ARG_ANYTHING,
4853 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
4854 static int bpf_push_seg6_encap(struct sk_buff *skb, u32 type, void *hdr, u32 len)
4857 struct ipv6_sr_hdr *srh = (struct ipv6_sr_hdr *)hdr;
4859 if (!seg6_validate_srh(srh, len))
4863 case BPF_LWT_ENCAP_SEG6_INLINE:
4864 if (skb->protocol != htons(ETH_P_IPV6))
4867 err = seg6_do_srh_inline(skb, srh);
4869 case BPF_LWT_ENCAP_SEG6:
4870 skb_reset_inner_headers(skb);
4871 skb->encapsulation = 1;
4872 err = seg6_do_srh_encap(skb, srh, IPPROTO_IPV6);
4878 bpf_compute_data_pointers(skb);
4882 ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
4883 skb_set_transport_header(skb, sizeof(struct ipv6hdr));
4885 return seg6_lookup_nexthop(skb, NULL, 0);
4887 #endif /* CONFIG_IPV6_SEG6_BPF */
4889 #if IS_ENABLED(CONFIG_LWTUNNEL_BPF)
4890 static int bpf_push_ip_encap(struct sk_buff *skb, void *hdr, u32 len,
4893 return bpf_lwt_push_ip_encap(skb, hdr, len, ingress);
4897 BPF_CALL_4(bpf_lwt_in_push_encap, struct sk_buff *, skb, u32, type, void *, hdr,
4901 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
4902 case BPF_LWT_ENCAP_SEG6:
4903 case BPF_LWT_ENCAP_SEG6_INLINE:
4904 return bpf_push_seg6_encap(skb, type, hdr, len);
4906 #if IS_ENABLED(CONFIG_LWTUNNEL_BPF)
4907 case BPF_LWT_ENCAP_IP:
4908 return bpf_push_ip_encap(skb, hdr, len, true /* ingress */);
4915 BPF_CALL_4(bpf_lwt_xmit_push_encap, struct sk_buff *, skb, u32, type,
4916 void *, hdr, u32, len)
4919 #if IS_ENABLED(CONFIG_LWTUNNEL_BPF)
4920 case BPF_LWT_ENCAP_IP:
4921 return bpf_push_ip_encap(skb, hdr, len, false /* egress */);
4928 static const struct bpf_func_proto bpf_lwt_in_push_encap_proto = {
4929 .func = bpf_lwt_in_push_encap,
4931 .ret_type = RET_INTEGER,
4932 .arg1_type = ARG_PTR_TO_CTX,
4933 .arg2_type = ARG_ANYTHING,
4934 .arg3_type = ARG_PTR_TO_MEM,
4935 .arg4_type = ARG_CONST_SIZE
4938 static const struct bpf_func_proto bpf_lwt_xmit_push_encap_proto = {
4939 .func = bpf_lwt_xmit_push_encap,
4941 .ret_type = RET_INTEGER,
4942 .arg1_type = ARG_PTR_TO_CTX,
4943 .arg2_type = ARG_ANYTHING,
4944 .arg3_type = ARG_PTR_TO_MEM,
4945 .arg4_type = ARG_CONST_SIZE
4948 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
4949 BPF_CALL_4(bpf_lwt_seg6_store_bytes, struct sk_buff *, skb, u32, offset,
4950 const void *, from, u32, len)
4952 struct seg6_bpf_srh_state *srh_state =
4953 this_cpu_ptr(&seg6_bpf_srh_states);
4954 struct ipv6_sr_hdr *srh = srh_state->srh;
4955 void *srh_tlvs, *srh_end, *ptr;
4961 srh_tlvs = (void *)((char *)srh + ((srh->first_segment + 1) << 4));
4962 srh_end = (void *)((char *)srh + sizeof(*srh) + srh_state->hdrlen);
4964 ptr = skb->data + offset;
4965 if (ptr >= srh_tlvs && ptr + len <= srh_end)
4966 srh_state->valid = false;
4967 else if (ptr < (void *)&srh->flags ||
4968 ptr + len > (void *)&srh->segments)
4971 if (unlikely(bpf_try_make_writable(skb, offset + len)))
4973 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0)
4975 srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
4977 memcpy(skb->data + offset, from, len);
4981 static const struct bpf_func_proto bpf_lwt_seg6_store_bytes_proto = {
4982 .func = bpf_lwt_seg6_store_bytes,
4984 .ret_type = RET_INTEGER,
4985 .arg1_type = ARG_PTR_TO_CTX,
4986 .arg2_type = ARG_ANYTHING,
4987 .arg3_type = ARG_PTR_TO_MEM,
4988 .arg4_type = ARG_CONST_SIZE
4991 static void bpf_update_srh_state(struct sk_buff *skb)
4993 struct seg6_bpf_srh_state *srh_state =
4994 this_cpu_ptr(&seg6_bpf_srh_states);
4997 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0) {
4998 srh_state->srh = NULL;
5000 srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
5001 srh_state->hdrlen = srh_state->srh->hdrlen << 3;
5002 srh_state->valid = true;
5006 BPF_CALL_4(bpf_lwt_seg6_action, struct sk_buff *, skb,
5007 u32, action, void *, param, u32, param_len)
5009 struct seg6_bpf_srh_state *srh_state =
5010 this_cpu_ptr(&seg6_bpf_srh_states);
5015 case SEG6_LOCAL_ACTION_END_X:
5016 if (!seg6_bpf_has_valid_srh(skb))
5018 if (param_len != sizeof(struct in6_addr))
5020 return seg6_lookup_nexthop(skb, (struct in6_addr *)param, 0);
5021 case SEG6_LOCAL_ACTION_END_T:
5022 if (!seg6_bpf_has_valid_srh(skb))
5024 if (param_len != sizeof(int))
5026 return seg6_lookup_nexthop(skb, NULL, *(int *)param);
5027 case SEG6_LOCAL_ACTION_END_DT6:
5028 if (!seg6_bpf_has_valid_srh(skb))
5030 if (param_len != sizeof(int))
5033 if (ipv6_find_hdr(skb, &hdroff, IPPROTO_IPV6, NULL, NULL) < 0)
5035 if (!pskb_pull(skb, hdroff))
5038 skb_postpull_rcsum(skb, skb_network_header(skb), hdroff);
5039 skb_reset_network_header(skb);
5040 skb_reset_transport_header(skb);
5041 skb->encapsulation = 0;
5043 bpf_compute_data_pointers(skb);
5044 bpf_update_srh_state(skb);
5045 return seg6_lookup_nexthop(skb, NULL, *(int *)param);
5046 case SEG6_LOCAL_ACTION_END_B6:
5047 if (srh_state->srh && !seg6_bpf_has_valid_srh(skb))
5049 err = bpf_push_seg6_encap(skb, BPF_LWT_ENCAP_SEG6_INLINE,
5052 bpf_update_srh_state(skb);
5055 case SEG6_LOCAL_ACTION_END_B6_ENCAP:
5056 if (srh_state->srh && !seg6_bpf_has_valid_srh(skb))
5058 err = bpf_push_seg6_encap(skb, BPF_LWT_ENCAP_SEG6,
5061 bpf_update_srh_state(skb);
5069 static const struct bpf_func_proto bpf_lwt_seg6_action_proto = {
5070 .func = bpf_lwt_seg6_action,
5072 .ret_type = RET_INTEGER,
5073 .arg1_type = ARG_PTR_TO_CTX,
5074 .arg2_type = ARG_ANYTHING,
5075 .arg3_type = ARG_PTR_TO_MEM,
5076 .arg4_type = ARG_CONST_SIZE
5079 BPF_CALL_3(bpf_lwt_seg6_adjust_srh, struct sk_buff *, skb, u32, offset,
5082 struct seg6_bpf_srh_state *srh_state =
5083 this_cpu_ptr(&seg6_bpf_srh_states);
5084 struct ipv6_sr_hdr *srh = srh_state->srh;
5085 void *srh_end, *srh_tlvs, *ptr;
5086 struct ipv6hdr *hdr;
5090 if (unlikely(srh == NULL))
5093 srh_tlvs = (void *)((unsigned char *)srh + sizeof(*srh) +
5094 ((srh->first_segment + 1) << 4));
5095 srh_end = (void *)((unsigned char *)srh + sizeof(*srh) +
5097 ptr = skb->data + offset;
5099 if (unlikely(ptr < srh_tlvs || ptr > srh_end))
5101 if (unlikely(len < 0 && (void *)((char *)ptr - len) > srh_end))
5105 ret = skb_cow_head(skb, len);
5106 if (unlikely(ret < 0))
5109 ret = bpf_skb_net_hdr_push(skb, offset, len);
5111 ret = bpf_skb_net_hdr_pop(skb, offset, -1 * len);
5114 bpf_compute_data_pointers(skb);
5115 if (unlikely(ret < 0))
5118 hdr = (struct ipv6hdr *)skb->data;
5119 hdr->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
5121 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0)
5123 srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
5124 srh_state->hdrlen += len;
5125 srh_state->valid = false;
5129 static const struct bpf_func_proto bpf_lwt_seg6_adjust_srh_proto = {
5130 .func = bpf_lwt_seg6_adjust_srh,
5132 .ret_type = RET_INTEGER,
5133 .arg1_type = ARG_PTR_TO_CTX,
5134 .arg2_type = ARG_ANYTHING,
5135 .arg3_type = ARG_ANYTHING,
5137 #endif /* CONFIG_IPV6_SEG6_BPF */
5139 #define CONVERT_COMMON_TCP_SOCK_FIELDS(md_type, CONVERT) \
5141 switch (si->off) { \
5142 case offsetof(md_type, snd_cwnd): \
5143 CONVERT(snd_cwnd); break; \
5144 case offsetof(md_type, srtt_us): \
5145 CONVERT(srtt_us); break; \
5146 case offsetof(md_type, snd_ssthresh): \
5147 CONVERT(snd_ssthresh); break; \
5148 case offsetof(md_type, rcv_nxt): \
5149 CONVERT(rcv_nxt); break; \
5150 case offsetof(md_type, snd_nxt): \
5151 CONVERT(snd_nxt); break; \
5152 case offsetof(md_type, snd_una): \
5153 CONVERT(snd_una); break; \
5154 case offsetof(md_type, mss_cache): \
5155 CONVERT(mss_cache); break; \
5156 case offsetof(md_type, ecn_flags): \
5157 CONVERT(ecn_flags); break; \
5158 case offsetof(md_type, rate_delivered): \
5159 CONVERT(rate_delivered); break; \
5160 case offsetof(md_type, rate_interval_us): \
5161 CONVERT(rate_interval_us); break; \
5162 case offsetof(md_type, packets_out): \
5163 CONVERT(packets_out); break; \
5164 case offsetof(md_type, retrans_out): \
5165 CONVERT(retrans_out); break; \
5166 case offsetof(md_type, total_retrans): \
5167 CONVERT(total_retrans); break; \
5168 case offsetof(md_type, segs_in): \
5169 CONVERT(segs_in); break; \
5170 case offsetof(md_type, data_segs_in): \
5171 CONVERT(data_segs_in); break; \
5172 case offsetof(md_type, segs_out): \
5173 CONVERT(segs_out); break; \
5174 case offsetof(md_type, data_segs_out): \
5175 CONVERT(data_segs_out); break; \
5176 case offsetof(md_type, lost_out): \
5177 CONVERT(lost_out); break; \
5178 case offsetof(md_type, sacked_out): \
5179 CONVERT(sacked_out); break; \
5180 case offsetof(md_type, bytes_received): \
5181 CONVERT(bytes_received); break; \
5182 case offsetof(md_type, bytes_acked): \
5183 CONVERT(bytes_acked); break; \
5188 static struct sock *sk_lookup(struct net *net, struct bpf_sock_tuple *tuple,
5189 int dif, int sdif, u8 family, u8 proto)
5191 bool refcounted = false;
5192 struct sock *sk = NULL;
5194 if (family == AF_INET) {
5195 __be32 src4 = tuple->ipv4.saddr;
5196 __be32 dst4 = tuple->ipv4.daddr;
5198 if (proto == IPPROTO_TCP)
5199 sk = __inet_lookup(net, &tcp_hashinfo, NULL, 0,
5200 src4, tuple->ipv4.sport,
5201 dst4, tuple->ipv4.dport,
5202 dif, sdif, &refcounted);
5204 sk = __udp4_lib_lookup(net, src4, tuple->ipv4.sport,
5205 dst4, tuple->ipv4.dport,
5206 dif, sdif, &udp_table, NULL);
5207 #if IS_ENABLED(CONFIG_IPV6)
5209 struct in6_addr *src6 = (struct in6_addr *)&tuple->ipv6.saddr;
5210 struct in6_addr *dst6 = (struct in6_addr *)&tuple->ipv6.daddr;
5212 if (proto == IPPROTO_TCP)
5213 sk = __inet6_lookup(net, &tcp_hashinfo, NULL, 0,
5214 src6, tuple->ipv6.sport,
5215 dst6, ntohs(tuple->ipv6.dport),
5216 dif, sdif, &refcounted);
5217 else if (likely(ipv6_bpf_stub))
5218 sk = ipv6_bpf_stub->udp6_lib_lookup(net,
5219 src6, tuple->ipv6.sport,
5220 dst6, tuple->ipv6.dport,
5226 if (unlikely(sk && !refcounted && !sock_flag(sk, SOCK_RCU_FREE))) {
5227 WARN_ONCE(1, "Found non-RCU, unreferenced socket!");
5233 /* bpf_skc_lookup performs the core lookup for different types of sockets,
5234 * taking a reference on the socket if it doesn't have the flag SOCK_RCU_FREE.
5235 * Returns the socket as an 'unsigned long' to simplify the casting in the
5236 * callers to satisfy BPF_CALL declarations.
5238 static struct sock *
5239 __bpf_skc_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
5240 struct net *caller_net, u32 ifindex, u8 proto, u64 netns_id,
5243 struct sock *sk = NULL;
5244 u8 family = AF_UNSPEC;
5248 family = len == sizeof(tuple->ipv4) ? AF_INET : AF_INET6;
5249 if (unlikely(family == AF_UNSPEC || flags ||
5250 !((s32)netns_id < 0 || netns_id <= S32_MAX)))
5253 if (family == AF_INET)
5254 sdif = inet_sdif(skb);
5256 sdif = inet6_sdif(skb);
5258 if ((s32)netns_id < 0) {
5260 sk = sk_lookup(net, tuple, ifindex, sdif, family, proto);
5262 net = get_net_ns_by_id(caller_net, netns_id);
5265 sk = sk_lookup(net, tuple, ifindex, sdif, family, proto);
5273 static struct sock *
5274 __bpf_sk_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
5275 struct net *caller_net, u32 ifindex, u8 proto, u64 netns_id,
5278 struct sock *sk = __bpf_skc_lookup(skb, tuple, len, caller_net,
5279 ifindex, proto, netns_id, flags);
5282 sk = sk_to_full_sk(sk);
5287 static struct sock *
5288 bpf_skc_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
5289 u8 proto, u64 netns_id, u64 flags)
5291 struct net *caller_net;
5295 caller_net = dev_net(skb->dev);
5296 ifindex = skb->dev->ifindex;
5298 caller_net = sock_net(skb->sk);
5302 return __bpf_skc_lookup(skb, tuple, len, caller_net, ifindex, proto,
5306 static struct sock *
5307 bpf_sk_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
5308 u8 proto, u64 netns_id, u64 flags)
5310 struct sock *sk = bpf_skc_lookup(skb, tuple, len, proto, netns_id,
5314 sk = sk_to_full_sk(sk);
5319 BPF_CALL_5(bpf_skc_lookup_tcp, struct sk_buff *, skb,
5320 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
5322 return (unsigned long)bpf_skc_lookup(skb, tuple, len, IPPROTO_TCP,
5326 static const struct bpf_func_proto bpf_skc_lookup_tcp_proto = {
5327 .func = bpf_skc_lookup_tcp,
5330 .ret_type = RET_PTR_TO_SOCK_COMMON_OR_NULL,
5331 .arg1_type = ARG_PTR_TO_CTX,
5332 .arg2_type = ARG_PTR_TO_MEM,
5333 .arg3_type = ARG_CONST_SIZE,
5334 .arg4_type = ARG_ANYTHING,
5335 .arg5_type = ARG_ANYTHING,
5338 BPF_CALL_5(bpf_sk_lookup_tcp, struct sk_buff *, skb,
5339 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
5341 return (unsigned long)bpf_sk_lookup(skb, tuple, len, IPPROTO_TCP,
5345 static const struct bpf_func_proto bpf_sk_lookup_tcp_proto = {
5346 .func = bpf_sk_lookup_tcp,
5349 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
5350 .arg1_type = ARG_PTR_TO_CTX,
5351 .arg2_type = ARG_PTR_TO_MEM,
5352 .arg3_type = ARG_CONST_SIZE,
5353 .arg4_type = ARG_ANYTHING,
5354 .arg5_type = ARG_ANYTHING,
5357 BPF_CALL_5(bpf_sk_lookup_udp, struct sk_buff *, skb,
5358 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
5360 return (unsigned long)bpf_sk_lookup(skb, tuple, len, IPPROTO_UDP,
5364 static const struct bpf_func_proto bpf_sk_lookup_udp_proto = {
5365 .func = bpf_sk_lookup_udp,
5368 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
5369 .arg1_type = ARG_PTR_TO_CTX,
5370 .arg2_type = ARG_PTR_TO_MEM,
5371 .arg3_type = ARG_CONST_SIZE,
5372 .arg4_type = ARG_ANYTHING,
5373 .arg5_type = ARG_ANYTHING,
5376 BPF_CALL_1(bpf_sk_release, struct sock *, sk)
5378 if (!sock_flag(sk, SOCK_RCU_FREE))
5383 static const struct bpf_func_proto bpf_sk_release_proto = {
5384 .func = bpf_sk_release,
5386 .ret_type = RET_INTEGER,
5387 .arg1_type = ARG_PTR_TO_SOCK_COMMON,
5390 BPF_CALL_5(bpf_xdp_sk_lookup_udp, struct xdp_buff *, ctx,
5391 struct bpf_sock_tuple *, tuple, u32, len, u32, netns_id, u64, flags)
5393 struct net *caller_net = dev_net(ctx->rxq->dev);
5394 int ifindex = ctx->rxq->dev->ifindex;
5396 return (unsigned long)__bpf_sk_lookup(NULL, tuple, len, caller_net,
5397 ifindex, IPPROTO_UDP, netns_id,
5401 static const struct bpf_func_proto bpf_xdp_sk_lookup_udp_proto = {
5402 .func = bpf_xdp_sk_lookup_udp,
5405 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
5406 .arg1_type = ARG_PTR_TO_CTX,
5407 .arg2_type = ARG_PTR_TO_MEM,
5408 .arg3_type = ARG_CONST_SIZE,
5409 .arg4_type = ARG_ANYTHING,
5410 .arg5_type = ARG_ANYTHING,
5413 BPF_CALL_5(bpf_xdp_skc_lookup_tcp, struct xdp_buff *, ctx,
5414 struct bpf_sock_tuple *, tuple, u32, len, u32, netns_id, u64, flags)
5416 struct net *caller_net = dev_net(ctx->rxq->dev);
5417 int ifindex = ctx->rxq->dev->ifindex;
5419 return (unsigned long)__bpf_skc_lookup(NULL, tuple, len, caller_net,
5420 ifindex, IPPROTO_TCP, netns_id,
5424 static const struct bpf_func_proto bpf_xdp_skc_lookup_tcp_proto = {
5425 .func = bpf_xdp_skc_lookup_tcp,
5428 .ret_type = RET_PTR_TO_SOCK_COMMON_OR_NULL,
5429 .arg1_type = ARG_PTR_TO_CTX,
5430 .arg2_type = ARG_PTR_TO_MEM,
5431 .arg3_type = ARG_CONST_SIZE,
5432 .arg4_type = ARG_ANYTHING,
5433 .arg5_type = ARG_ANYTHING,
5436 BPF_CALL_5(bpf_xdp_sk_lookup_tcp, struct xdp_buff *, ctx,
5437 struct bpf_sock_tuple *, tuple, u32, len, u32, netns_id, u64, flags)
5439 struct net *caller_net = dev_net(ctx->rxq->dev);
5440 int ifindex = ctx->rxq->dev->ifindex;
5442 return (unsigned long)__bpf_sk_lookup(NULL, tuple, len, caller_net,
5443 ifindex, IPPROTO_TCP, netns_id,
5447 static const struct bpf_func_proto bpf_xdp_sk_lookup_tcp_proto = {
5448 .func = bpf_xdp_sk_lookup_tcp,
5451 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
5452 .arg1_type = ARG_PTR_TO_CTX,
5453 .arg2_type = ARG_PTR_TO_MEM,
5454 .arg3_type = ARG_CONST_SIZE,
5455 .arg4_type = ARG_ANYTHING,
5456 .arg5_type = ARG_ANYTHING,
5459 BPF_CALL_5(bpf_sock_addr_skc_lookup_tcp, struct bpf_sock_addr_kern *, ctx,
5460 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
5462 return (unsigned long)__bpf_skc_lookup(NULL, tuple, len,
5463 sock_net(ctx->sk), 0,
5464 IPPROTO_TCP, netns_id, flags);
5467 static const struct bpf_func_proto bpf_sock_addr_skc_lookup_tcp_proto = {
5468 .func = bpf_sock_addr_skc_lookup_tcp,
5470 .ret_type = RET_PTR_TO_SOCK_COMMON_OR_NULL,
5471 .arg1_type = ARG_PTR_TO_CTX,
5472 .arg2_type = ARG_PTR_TO_MEM,
5473 .arg3_type = ARG_CONST_SIZE,
5474 .arg4_type = ARG_ANYTHING,
5475 .arg5_type = ARG_ANYTHING,
5478 BPF_CALL_5(bpf_sock_addr_sk_lookup_tcp, struct bpf_sock_addr_kern *, ctx,
5479 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
5481 return (unsigned long)__bpf_sk_lookup(NULL, tuple, len,
5482 sock_net(ctx->sk), 0, IPPROTO_TCP,
5486 static const struct bpf_func_proto bpf_sock_addr_sk_lookup_tcp_proto = {
5487 .func = bpf_sock_addr_sk_lookup_tcp,
5489 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
5490 .arg1_type = ARG_PTR_TO_CTX,
5491 .arg2_type = ARG_PTR_TO_MEM,
5492 .arg3_type = ARG_CONST_SIZE,
5493 .arg4_type = ARG_ANYTHING,
5494 .arg5_type = ARG_ANYTHING,
5497 BPF_CALL_5(bpf_sock_addr_sk_lookup_udp, struct bpf_sock_addr_kern *, ctx,
5498 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
5500 return (unsigned long)__bpf_sk_lookup(NULL, tuple, len,
5501 sock_net(ctx->sk), 0, IPPROTO_UDP,
5505 static const struct bpf_func_proto bpf_sock_addr_sk_lookup_udp_proto = {
5506 .func = bpf_sock_addr_sk_lookup_udp,
5508 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
5509 .arg1_type = ARG_PTR_TO_CTX,
5510 .arg2_type = ARG_PTR_TO_MEM,
5511 .arg3_type = ARG_CONST_SIZE,
5512 .arg4_type = ARG_ANYTHING,
5513 .arg5_type = ARG_ANYTHING,
5516 bool bpf_tcp_sock_is_valid_access(int off, int size, enum bpf_access_type type,
5517 struct bpf_insn_access_aux *info)
5519 if (off < 0 || off >= offsetofend(struct bpf_tcp_sock, bytes_acked))
5522 if (off % size != 0)
5526 case offsetof(struct bpf_tcp_sock, bytes_received):
5527 case offsetof(struct bpf_tcp_sock, bytes_acked):
5528 return size == sizeof(__u64);
5530 return size == sizeof(__u32);
5534 u32 bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type,
5535 const struct bpf_insn *si,
5536 struct bpf_insn *insn_buf,
5537 struct bpf_prog *prog, u32 *target_size)
5539 struct bpf_insn *insn = insn_buf;
5541 #define BPF_TCP_SOCK_GET_COMMON(FIELD) \
5543 BUILD_BUG_ON(FIELD_SIZEOF(struct tcp_sock, FIELD) > \
5544 FIELD_SIZEOF(struct bpf_tcp_sock, FIELD)); \
5545 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct tcp_sock, FIELD),\
5546 si->dst_reg, si->src_reg, \
5547 offsetof(struct tcp_sock, FIELD)); \
5550 CONVERT_COMMON_TCP_SOCK_FIELDS(struct bpf_tcp_sock,
5551 BPF_TCP_SOCK_GET_COMMON);
5553 if (insn > insn_buf)
5554 return insn - insn_buf;
5557 case offsetof(struct bpf_tcp_sock, rtt_min):
5558 BUILD_BUG_ON(FIELD_SIZEOF(struct tcp_sock, rtt_min) !=
5559 sizeof(struct minmax));
5560 BUILD_BUG_ON(sizeof(struct minmax) <
5561 sizeof(struct minmax_sample));
5563 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
5564 offsetof(struct tcp_sock, rtt_min) +
5565 offsetof(struct minmax_sample, v));
5569 return insn - insn_buf;
5572 BPF_CALL_1(bpf_tcp_sock, struct sock *, sk)
5574 if (sk_fullsock(sk) && sk->sk_protocol == IPPROTO_TCP)
5575 return (unsigned long)sk;
5577 return (unsigned long)NULL;
5580 static const struct bpf_func_proto bpf_tcp_sock_proto = {
5581 .func = bpf_tcp_sock,
5583 .ret_type = RET_PTR_TO_TCP_SOCK_OR_NULL,
5584 .arg1_type = ARG_PTR_TO_SOCK_COMMON,
5587 BPF_CALL_1(bpf_get_listener_sock, struct sock *, sk)
5589 sk = sk_to_full_sk(sk);
5591 if (sk->sk_state == TCP_LISTEN && sock_flag(sk, SOCK_RCU_FREE))
5592 return (unsigned long)sk;
5594 return (unsigned long)NULL;
5597 static const struct bpf_func_proto bpf_get_listener_sock_proto = {
5598 .func = bpf_get_listener_sock,
5600 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
5601 .arg1_type = ARG_PTR_TO_SOCK_COMMON,
5604 BPF_CALL_1(bpf_skb_ecn_set_ce, struct sk_buff *, skb)
5606 unsigned int iphdr_len;
5608 if (skb->protocol == cpu_to_be16(ETH_P_IP))
5609 iphdr_len = sizeof(struct iphdr);
5610 else if (skb->protocol == cpu_to_be16(ETH_P_IPV6))
5611 iphdr_len = sizeof(struct ipv6hdr);
5615 if (skb_headlen(skb) < iphdr_len)
5618 if (skb_cloned(skb) && !skb_clone_writable(skb, iphdr_len))
5621 return INET_ECN_set_ce(skb);
5624 static const struct bpf_func_proto bpf_skb_ecn_set_ce_proto = {
5625 .func = bpf_skb_ecn_set_ce,
5627 .ret_type = RET_INTEGER,
5628 .arg1_type = ARG_PTR_TO_CTX,
5631 BPF_CALL_5(bpf_tcp_check_syncookie, struct sock *, sk, void *, iph, u32, iph_len,
5632 struct tcphdr *, th, u32, th_len)
5634 #ifdef CONFIG_SYN_COOKIES
5638 if (unlikely(th_len < sizeof(*th)))
5641 /* sk_listener() allows TCP_NEW_SYN_RECV, which makes no sense here. */
5642 if (sk->sk_protocol != IPPROTO_TCP || sk->sk_state != TCP_LISTEN)
5645 if (!sock_net(sk)->ipv4.sysctl_tcp_syncookies)
5648 if (!th->ack || th->rst || th->syn)
5651 if (tcp_synq_no_recent_overflow(sk))
5654 cookie = ntohl(th->ack_seq) - 1;
5656 switch (sk->sk_family) {
5658 if (unlikely(iph_len < sizeof(struct iphdr)))
5661 ret = __cookie_v4_check((struct iphdr *)iph, th, cookie);
5664 #if IS_BUILTIN(CONFIG_IPV6)
5666 if (unlikely(iph_len < sizeof(struct ipv6hdr)))
5669 ret = __cookie_v6_check((struct ipv6hdr *)iph, th, cookie);
5671 #endif /* CONFIG_IPV6 */
5674 return -EPROTONOSUPPORT;
5686 static const struct bpf_func_proto bpf_tcp_check_syncookie_proto = {
5687 .func = bpf_tcp_check_syncookie,
5690 .ret_type = RET_INTEGER,
5691 .arg1_type = ARG_PTR_TO_SOCK_COMMON,
5692 .arg2_type = ARG_PTR_TO_MEM,
5693 .arg3_type = ARG_CONST_SIZE,
5694 .arg4_type = ARG_PTR_TO_MEM,
5695 .arg5_type = ARG_CONST_SIZE,
5698 #endif /* CONFIG_INET */
5700 bool bpf_helper_changes_pkt_data(void *func)
5702 if (func == bpf_skb_vlan_push ||
5703 func == bpf_skb_vlan_pop ||
5704 func == bpf_skb_store_bytes ||
5705 func == bpf_skb_change_proto ||
5706 func == bpf_skb_change_head ||
5707 func == sk_skb_change_head ||
5708 func == bpf_skb_change_tail ||
5709 func == sk_skb_change_tail ||
5710 func == bpf_skb_adjust_room ||
5711 func == bpf_skb_pull_data ||
5712 func == sk_skb_pull_data ||
5713 func == bpf_clone_redirect ||
5714 func == bpf_l3_csum_replace ||
5715 func == bpf_l4_csum_replace ||
5716 func == bpf_xdp_adjust_head ||
5717 func == bpf_xdp_adjust_meta ||
5718 func == bpf_msg_pull_data ||
5719 func == bpf_msg_push_data ||
5720 func == bpf_msg_pop_data ||
5721 func == bpf_xdp_adjust_tail ||
5722 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
5723 func == bpf_lwt_seg6_store_bytes ||
5724 func == bpf_lwt_seg6_adjust_srh ||
5725 func == bpf_lwt_seg6_action ||
5727 func == bpf_lwt_in_push_encap ||
5728 func == bpf_lwt_xmit_push_encap)
5734 static const struct bpf_func_proto *
5735 bpf_base_func_proto(enum bpf_func_id func_id)
5738 case BPF_FUNC_map_lookup_elem:
5739 return &bpf_map_lookup_elem_proto;
5740 case BPF_FUNC_map_update_elem:
5741 return &bpf_map_update_elem_proto;
5742 case BPF_FUNC_map_delete_elem:
5743 return &bpf_map_delete_elem_proto;
5744 case BPF_FUNC_map_push_elem:
5745 return &bpf_map_push_elem_proto;
5746 case BPF_FUNC_map_pop_elem:
5747 return &bpf_map_pop_elem_proto;
5748 case BPF_FUNC_map_peek_elem:
5749 return &bpf_map_peek_elem_proto;
5750 case BPF_FUNC_get_prandom_u32:
5751 return &bpf_get_prandom_u32_proto;
5752 case BPF_FUNC_get_smp_processor_id:
5753 return &bpf_get_raw_smp_processor_id_proto;
5754 case BPF_FUNC_get_numa_node_id:
5755 return &bpf_get_numa_node_id_proto;
5756 case BPF_FUNC_tail_call:
5757 return &bpf_tail_call_proto;
5758 case BPF_FUNC_ktime_get_ns:
5759 return &bpf_ktime_get_ns_proto;
5764 if (!capable(CAP_SYS_ADMIN))
5768 case BPF_FUNC_spin_lock:
5769 return &bpf_spin_lock_proto;
5770 case BPF_FUNC_spin_unlock:
5771 return &bpf_spin_unlock_proto;
5772 case BPF_FUNC_trace_printk:
5773 return bpf_get_trace_printk_proto();
5779 static const struct bpf_func_proto *
5780 sock_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5783 /* inet and inet6 sockets are created in a process
5784 * context so there is always a valid uid/gid
5786 case BPF_FUNC_get_current_uid_gid:
5787 return &bpf_get_current_uid_gid_proto;
5788 case BPF_FUNC_get_local_storage:
5789 return &bpf_get_local_storage_proto;
5791 return bpf_base_func_proto(func_id);
5795 static const struct bpf_func_proto *
5796 sock_addr_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;
5805 switch (prog->expected_attach_type) {
5806 case BPF_CGROUP_INET4_CONNECT:
5807 case BPF_CGROUP_INET6_CONNECT:
5808 return &bpf_bind_proto;
5812 case BPF_FUNC_get_socket_cookie:
5813 return &bpf_get_socket_cookie_sock_addr_proto;
5814 case BPF_FUNC_get_local_storage:
5815 return &bpf_get_local_storage_proto;
5817 case BPF_FUNC_sk_lookup_tcp:
5818 return &bpf_sock_addr_sk_lookup_tcp_proto;
5819 case BPF_FUNC_sk_lookup_udp:
5820 return &bpf_sock_addr_sk_lookup_udp_proto;
5821 case BPF_FUNC_sk_release:
5822 return &bpf_sk_release_proto;
5823 case BPF_FUNC_skc_lookup_tcp:
5824 return &bpf_sock_addr_skc_lookup_tcp_proto;
5825 #endif /* CONFIG_INET */
5827 return bpf_base_func_proto(func_id);
5831 static const struct bpf_func_proto *
5832 sk_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5835 case BPF_FUNC_skb_load_bytes:
5836 return &bpf_skb_load_bytes_proto;
5837 case BPF_FUNC_skb_load_bytes_relative:
5838 return &bpf_skb_load_bytes_relative_proto;
5839 case BPF_FUNC_get_socket_cookie:
5840 return &bpf_get_socket_cookie_proto;
5841 case BPF_FUNC_get_socket_uid:
5842 return &bpf_get_socket_uid_proto;
5844 return bpf_base_func_proto(func_id);
5848 static const struct bpf_func_proto *
5849 cg_skb_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5852 case BPF_FUNC_get_local_storage:
5853 return &bpf_get_local_storage_proto;
5854 case BPF_FUNC_sk_fullsock:
5855 return &bpf_sk_fullsock_proto;
5857 case BPF_FUNC_tcp_sock:
5858 return &bpf_tcp_sock_proto;
5859 case BPF_FUNC_get_listener_sock:
5860 return &bpf_get_listener_sock_proto;
5861 case BPF_FUNC_skb_ecn_set_ce:
5862 return &bpf_skb_ecn_set_ce_proto;
5865 return sk_filter_func_proto(func_id, prog);
5869 static const struct bpf_func_proto *
5870 tc_cls_act_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5873 case BPF_FUNC_skb_store_bytes:
5874 return &bpf_skb_store_bytes_proto;
5875 case BPF_FUNC_skb_load_bytes:
5876 return &bpf_skb_load_bytes_proto;
5877 case BPF_FUNC_skb_load_bytes_relative:
5878 return &bpf_skb_load_bytes_relative_proto;
5879 case BPF_FUNC_skb_pull_data:
5880 return &bpf_skb_pull_data_proto;
5881 case BPF_FUNC_csum_diff:
5882 return &bpf_csum_diff_proto;
5883 case BPF_FUNC_csum_update:
5884 return &bpf_csum_update_proto;
5885 case BPF_FUNC_l3_csum_replace:
5886 return &bpf_l3_csum_replace_proto;
5887 case BPF_FUNC_l4_csum_replace:
5888 return &bpf_l4_csum_replace_proto;
5889 case BPF_FUNC_clone_redirect:
5890 return &bpf_clone_redirect_proto;
5891 case BPF_FUNC_get_cgroup_classid:
5892 return &bpf_get_cgroup_classid_proto;
5893 case BPF_FUNC_skb_vlan_push:
5894 return &bpf_skb_vlan_push_proto;
5895 case BPF_FUNC_skb_vlan_pop:
5896 return &bpf_skb_vlan_pop_proto;
5897 case BPF_FUNC_skb_change_proto:
5898 return &bpf_skb_change_proto_proto;
5899 case BPF_FUNC_skb_change_type:
5900 return &bpf_skb_change_type_proto;
5901 case BPF_FUNC_skb_adjust_room:
5902 return &bpf_skb_adjust_room_proto;
5903 case BPF_FUNC_skb_change_tail:
5904 return &bpf_skb_change_tail_proto;
5905 case BPF_FUNC_skb_get_tunnel_key:
5906 return &bpf_skb_get_tunnel_key_proto;
5907 case BPF_FUNC_skb_set_tunnel_key:
5908 return bpf_get_skb_set_tunnel_proto(func_id);
5909 case BPF_FUNC_skb_get_tunnel_opt:
5910 return &bpf_skb_get_tunnel_opt_proto;
5911 case BPF_FUNC_skb_set_tunnel_opt:
5912 return bpf_get_skb_set_tunnel_proto(func_id);
5913 case BPF_FUNC_redirect:
5914 return &bpf_redirect_proto;
5915 case BPF_FUNC_get_route_realm:
5916 return &bpf_get_route_realm_proto;
5917 case BPF_FUNC_get_hash_recalc:
5918 return &bpf_get_hash_recalc_proto;
5919 case BPF_FUNC_set_hash_invalid:
5920 return &bpf_set_hash_invalid_proto;
5921 case BPF_FUNC_set_hash:
5922 return &bpf_set_hash_proto;
5923 case BPF_FUNC_perf_event_output:
5924 return &bpf_skb_event_output_proto;
5925 case BPF_FUNC_get_smp_processor_id:
5926 return &bpf_get_smp_processor_id_proto;
5927 case BPF_FUNC_skb_under_cgroup:
5928 return &bpf_skb_under_cgroup_proto;
5929 case BPF_FUNC_get_socket_cookie:
5930 return &bpf_get_socket_cookie_proto;
5931 case BPF_FUNC_get_socket_uid:
5932 return &bpf_get_socket_uid_proto;
5933 case BPF_FUNC_fib_lookup:
5934 return &bpf_skb_fib_lookup_proto;
5935 case BPF_FUNC_sk_fullsock:
5936 return &bpf_sk_fullsock_proto;
5938 case BPF_FUNC_skb_get_xfrm_state:
5939 return &bpf_skb_get_xfrm_state_proto;
5941 #ifdef CONFIG_SOCK_CGROUP_DATA
5942 case BPF_FUNC_skb_cgroup_id:
5943 return &bpf_skb_cgroup_id_proto;
5944 case BPF_FUNC_skb_ancestor_cgroup_id:
5945 return &bpf_skb_ancestor_cgroup_id_proto;
5948 case BPF_FUNC_sk_lookup_tcp:
5949 return &bpf_sk_lookup_tcp_proto;
5950 case BPF_FUNC_sk_lookup_udp:
5951 return &bpf_sk_lookup_udp_proto;
5952 case BPF_FUNC_sk_release:
5953 return &bpf_sk_release_proto;
5954 case BPF_FUNC_tcp_sock:
5955 return &bpf_tcp_sock_proto;
5956 case BPF_FUNC_get_listener_sock:
5957 return &bpf_get_listener_sock_proto;
5958 case BPF_FUNC_skc_lookup_tcp:
5959 return &bpf_skc_lookup_tcp_proto;
5960 case BPF_FUNC_tcp_check_syncookie:
5961 return &bpf_tcp_check_syncookie_proto;
5964 return bpf_base_func_proto(func_id);
5968 static const struct bpf_func_proto *
5969 xdp_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5972 case BPF_FUNC_perf_event_output:
5973 return &bpf_xdp_event_output_proto;
5974 case BPF_FUNC_get_smp_processor_id:
5975 return &bpf_get_smp_processor_id_proto;
5976 case BPF_FUNC_csum_diff:
5977 return &bpf_csum_diff_proto;
5978 case BPF_FUNC_xdp_adjust_head:
5979 return &bpf_xdp_adjust_head_proto;
5980 case BPF_FUNC_xdp_adjust_meta:
5981 return &bpf_xdp_adjust_meta_proto;
5982 case BPF_FUNC_redirect:
5983 return &bpf_xdp_redirect_proto;
5984 case BPF_FUNC_redirect_map:
5985 return &bpf_xdp_redirect_map_proto;
5986 case BPF_FUNC_xdp_adjust_tail:
5987 return &bpf_xdp_adjust_tail_proto;
5988 case BPF_FUNC_fib_lookup:
5989 return &bpf_xdp_fib_lookup_proto;
5991 case BPF_FUNC_sk_lookup_udp:
5992 return &bpf_xdp_sk_lookup_udp_proto;
5993 case BPF_FUNC_sk_lookup_tcp:
5994 return &bpf_xdp_sk_lookup_tcp_proto;
5995 case BPF_FUNC_sk_release:
5996 return &bpf_sk_release_proto;
5997 case BPF_FUNC_skc_lookup_tcp:
5998 return &bpf_xdp_skc_lookup_tcp_proto;
5999 case BPF_FUNC_tcp_check_syncookie:
6000 return &bpf_tcp_check_syncookie_proto;
6003 return bpf_base_func_proto(func_id);
6007 const struct bpf_func_proto bpf_sock_map_update_proto __weak;
6008 const struct bpf_func_proto bpf_sock_hash_update_proto __weak;
6010 static const struct bpf_func_proto *
6011 sock_ops_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6014 case BPF_FUNC_setsockopt:
6015 return &bpf_setsockopt_proto;
6016 case BPF_FUNC_getsockopt:
6017 return &bpf_getsockopt_proto;
6018 case BPF_FUNC_sock_ops_cb_flags_set:
6019 return &bpf_sock_ops_cb_flags_set_proto;
6020 case BPF_FUNC_sock_map_update:
6021 return &bpf_sock_map_update_proto;
6022 case BPF_FUNC_sock_hash_update:
6023 return &bpf_sock_hash_update_proto;
6024 case BPF_FUNC_get_socket_cookie:
6025 return &bpf_get_socket_cookie_sock_ops_proto;
6026 case BPF_FUNC_get_local_storage:
6027 return &bpf_get_local_storage_proto;
6028 case BPF_FUNC_perf_event_output:
6029 return &bpf_sockopt_event_output_proto;
6031 return bpf_base_func_proto(func_id);
6035 const struct bpf_func_proto bpf_msg_redirect_map_proto __weak;
6036 const struct bpf_func_proto bpf_msg_redirect_hash_proto __weak;
6038 static const struct bpf_func_proto *
6039 sk_msg_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6042 case BPF_FUNC_msg_redirect_map:
6043 return &bpf_msg_redirect_map_proto;
6044 case BPF_FUNC_msg_redirect_hash:
6045 return &bpf_msg_redirect_hash_proto;
6046 case BPF_FUNC_msg_apply_bytes:
6047 return &bpf_msg_apply_bytes_proto;
6048 case BPF_FUNC_msg_cork_bytes:
6049 return &bpf_msg_cork_bytes_proto;
6050 case BPF_FUNC_msg_pull_data:
6051 return &bpf_msg_pull_data_proto;
6052 case BPF_FUNC_msg_push_data:
6053 return &bpf_msg_push_data_proto;
6054 case BPF_FUNC_msg_pop_data:
6055 return &bpf_msg_pop_data_proto;
6057 return bpf_base_func_proto(func_id);
6061 const struct bpf_func_proto bpf_sk_redirect_map_proto __weak;
6062 const struct bpf_func_proto bpf_sk_redirect_hash_proto __weak;
6064 static const struct bpf_func_proto *
6065 sk_skb_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6068 case BPF_FUNC_skb_store_bytes:
6069 return &bpf_skb_store_bytes_proto;
6070 case BPF_FUNC_skb_load_bytes:
6071 return &bpf_skb_load_bytes_proto;
6072 case BPF_FUNC_skb_pull_data:
6073 return &sk_skb_pull_data_proto;
6074 case BPF_FUNC_skb_change_tail:
6075 return &sk_skb_change_tail_proto;
6076 case BPF_FUNC_skb_change_head:
6077 return &sk_skb_change_head_proto;
6078 case BPF_FUNC_get_socket_cookie:
6079 return &bpf_get_socket_cookie_proto;
6080 case BPF_FUNC_get_socket_uid:
6081 return &bpf_get_socket_uid_proto;
6082 case BPF_FUNC_sk_redirect_map:
6083 return &bpf_sk_redirect_map_proto;
6084 case BPF_FUNC_sk_redirect_hash:
6085 return &bpf_sk_redirect_hash_proto;
6087 case BPF_FUNC_sk_lookup_tcp:
6088 return &bpf_sk_lookup_tcp_proto;
6089 case BPF_FUNC_sk_lookup_udp:
6090 return &bpf_sk_lookup_udp_proto;
6091 case BPF_FUNC_sk_release:
6092 return &bpf_sk_release_proto;
6093 case BPF_FUNC_skc_lookup_tcp:
6094 return &bpf_skc_lookup_tcp_proto;
6097 return bpf_base_func_proto(func_id);
6101 static const struct bpf_func_proto *
6102 flow_dissector_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6105 case BPF_FUNC_skb_load_bytes:
6106 return &bpf_skb_load_bytes_proto;
6108 return bpf_base_func_proto(func_id);
6112 static const struct bpf_func_proto *
6113 lwt_out_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6116 case BPF_FUNC_skb_load_bytes:
6117 return &bpf_skb_load_bytes_proto;
6118 case BPF_FUNC_skb_pull_data:
6119 return &bpf_skb_pull_data_proto;
6120 case BPF_FUNC_csum_diff:
6121 return &bpf_csum_diff_proto;
6122 case BPF_FUNC_get_cgroup_classid:
6123 return &bpf_get_cgroup_classid_proto;
6124 case BPF_FUNC_get_route_realm:
6125 return &bpf_get_route_realm_proto;
6126 case BPF_FUNC_get_hash_recalc:
6127 return &bpf_get_hash_recalc_proto;
6128 case BPF_FUNC_perf_event_output:
6129 return &bpf_skb_event_output_proto;
6130 case BPF_FUNC_get_smp_processor_id:
6131 return &bpf_get_smp_processor_id_proto;
6132 case BPF_FUNC_skb_under_cgroup:
6133 return &bpf_skb_under_cgroup_proto;
6135 return bpf_base_func_proto(func_id);
6139 static const struct bpf_func_proto *
6140 lwt_in_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6143 case BPF_FUNC_lwt_push_encap:
6144 return &bpf_lwt_in_push_encap_proto;
6146 return lwt_out_func_proto(func_id, prog);
6150 static const struct bpf_func_proto *
6151 lwt_xmit_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6154 case BPF_FUNC_skb_get_tunnel_key:
6155 return &bpf_skb_get_tunnel_key_proto;
6156 case BPF_FUNC_skb_set_tunnel_key:
6157 return bpf_get_skb_set_tunnel_proto(func_id);
6158 case BPF_FUNC_skb_get_tunnel_opt:
6159 return &bpf_skb_get_tunnel_opt_proto;
6160 case BPF_FUNC_skb_set_tunnel_opt:
6161 return bpf_get_skb_set_tunnel_proto(func_id);
6162 case BPF_FUNC_redirect:
6163 return &bpf_redirect_proto;
6164 case BPF_FUNC_clone_redirect:
6165 return &bpf_clone_redirect_proto;
6166 case BPF_FUNC_skb_change_tail:
6167 return &bpf_skb_change_tail_proto;
6168 case BPF_FUNC_skb_change_head:
6169 return &bpf_skb_change_head_proto;
6170 case BPF_FUNC_skb_store_bytes:
6171 return &bpf_skb_store_bytes_proto;
6172 case BPF_FUNC_csum_update:
6173 return &bpf_csum_update_proto;
6174 case BPF_FUNC_l3_csum_replace:
6175 return &bpf_l3_csum_replace_proto;
6176 case BPF_FUNC_l4_csum_replace:
6177 return &bpf_l4_csum_replace_proto;
6178 case BPF_FUNC_set_hash_invalid:
6179 return &bpf_set_hash_invalid_proto;
6180 case BPF_FUNC_lwt_push_encap:
6181 return &bpf_lwt_xmit_push_encap_proto;
6183 return lwt_out_func_proto(func_id, prog);
6187 static const struct bpf_func_proto *
6188 lwt_seg6local_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6191 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
6192 case BPF_FUNC_lwt_seg6_store_bytes:
6193 return &bpf_lwt_seg6_store_bytes_proto;
6194 case BPF_FUNC_lwt_seg6_action:
6195 return &bpf_lwt_seg6_action_proto;
6196 case BPF_FUNC_lwt_seg6_adjust_srh:
6197 return &bpf_lwt_seg6_adjust_srh_proto;
6200 return lwt_out_func_proto(func_id, prog);
6204 static bool bpf_skb_is_valid_access(int off, int size, enum bpf_access_type type,
6205 const struct bpf_prog *prog,
6206 struct bpf_insn_access_aux *info)
6208 const int size_default = sizeof(__u32);
6210 if (off < 0 || off >= sizeof(struct __sk_buff))
6213 /* The verifier guarantees that size > 0. */
6214 if (off % size != 0)
6218 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
6219 if (off + size > offsetofend(struct __sk_buff, cb[4]))
6222 case bpf_ctx_range_till(struct __sk_buff, remote_ip6[0], remote_ip6[3]):
6223 case bpf_ctx_range_till(struct __sk_buff, local_ip6[0], local_ip6[3]):
6224 case bpf_ctx_range_till(struct __sk_buff, remote_ip4, remote_ip4):
6225 case bpf_ctx_range_till(struct __sk_buff, local_ip4, local_ip4):
6226 case bpf_ctx_range(struct __sk_buff, data):
6227 case bpf_ctx_range(struct __sk_buff, data_meta):
6228 case bpf_ctx_range(struct __sk_buff, data_end):
6229 if (size != size_default)
6232 case bpf_ctx_range_ptr(struct __sk_buff, flow_keys):
6233 if (size != sizeof(__u64))
6236 case bpf_ctx_range(struct __sk_buff, tstamp):
6237 if (size != sizeof(__u64))
6240 case offsetof(struct __sk_buff, sk):
6241 if (type == BPF_WRITE || size != sizeof(__u64))
6243 info->reg_type = PTR_TO_SOCK_COMMON_OR_NULL;
6246 /* Only narrow read access allowed for now. */
6247 if (type == BPF_WRITE) {
6248 if (size != size_default)
6251 bpf_ctx_record_field_size(info, size_default);
6252 if (!bpf_ctx_narrow_access_ok(off, size, size_default))
6260 static bool sk_filter_is_valid_access(int off, int size,
6261 enum bpf_access_type type,
6262 const struct bpf_prog *prog,
6263 struct bpf_insn_access_aux *info)
6266 case bpf_ctx_range(struct __sk_buff, tc_classid):
6267 case bpf_ctx_range(struct __sk_buff, data):
6268 case bpf_ctx_range(struct __sk_buff, data_meta):
6269 case bpf_ctx_range(struct __sk_buff, data_end):
6270 case bpf_ctx_range_ptr(struct __sk_buff, flow_keys):
6271 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
6272 case bpf_ctx_range(struct __sk_buff, tstamp):
6273 case bpf_ctx_range(struct __sk_buff, wire_len):
6277 if (type == BPF_WRITE) {
6279 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
6286 return bpf_skb_is_valid_access(off, size, type, prog, info);
6289 static bool cg_skb_is_valid_access(int off, int size,
6290 enum bpf_access_type type,
6291 const struct bpf_prog *prog,
6292 struct bpf_insn_access_aux *info)
6295 case bpf_ctx_range(struct __sk_buff, tc_classid):
6296 case bpf_ctx_range(struct __sk_buff, data_meta):
6297 case bpf_ctx_range_ptr(struct __sk_buff, flow_keys):
6298 case bpf_ctx_range(struct __sk_buff, wire_len):
6300 case bpf_ctx_range(struct __sk_buff, data):
6301 case bpf_ctx_range(struct __sk_buff, data_end):
6302 if (!capable(CAP_SYS_ADMIN))
6307 if (type == BPF_WRITE) {
6309 case bpf_ctx_range(struct __sk_buff, mark):
6310 case bpf_ctx_range(struct __sk_buff, priority):
6311 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
6313 case bpf_ctx_range(struct __sk_buff, tstamp):
6314 if (!capable(CAP_SYS_ADMIN))
6323 case bpf_ctx_range(struct __sk_buff, data):
6324 info->reg_type = PTR_TO_PACKET;
6326 case bpf_ctx_range(struct __sk_buff, data_end):
6327 info->reg_type = PTR_TO_PACKET_END;
6331 return bpf_skb_is_valid_access(off, size, type, prog, info);
6334 static bool lwt_is_valid_access(int off, int size,
6335 enum bpf_access_type type,
6336 const struct bpf_prog *prog,
6337 struct bpf_insn_access_aux *info)
6340 case bpf_ctx_range(struct __sk_buff, tc_classid):
6341 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
6342 case bpf_ctx_range(struct __sk_buff, data_meta):
6343 case bpf_ctx_range_ptr(struct __sk_buff, flow_keys):
6344 case bpf_ctx_range(struct __sk_buff, tstamp):
6345 case bpf_ctx_range(struct __sk_buff, wire_len):
6349 if (type == BPF_WRITE) {
6351 case bpf_ctx_range(struct __sk_buff, mark):
6352 case bpf_ctx_range(struct __sk_buff, priority):
6353 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
6361 case bpf_ctx_range(struct __sk_buff, data):
6362 info->reg_type = PTR_TO_PACKET;
6364 case bpf_ctx_range(struct __sk_buff, data_end):
6365 info->reg_type = PTR_TO_PACKET_END;
6369 return bpf_skb_is_valid_access(off, size, type, prog, info);
6372 /* Attach type specific accesses */
6373 static bool __sock_filter_check_attach_type(int off,
6374 enum bpf_access_type access_type,
6375 enum bpf_attach_type attach_type)
6378 case offsetof(struct bpf_sock, bound_dev_if):
6379 case offsetof(struct bpf_sock, mark):
6380 case offsetof(struct bpf_sock, priority):
6381 switch (attach_type) {
6382 case BPF_CGROUP_INET_SOCK_CREATE:
6387 case bpf_ctx_range(struct bpf_sock, src_ip4):
6388 switch (attach_type) {
6389 case BPF_CGROUP_INET4_POST_BIND:
6394 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
6395 switch (attach_type) {
6396 case BPF_CGROUP_INET6_POST_BIND:
6401 case bpf_ctx_range(struct bpf_sock, src_port):
6402 switch (attach_type) {
6403 case BPF_CGROUP_INET4_POST_BIND:
6404 case BPF_CGROUP_INET6_POST_BIND:
6411 return access_type == BPF_READ;
6416 bool bpf_sock_common_is_valid_access(int off, int size,
6417 enum bpf_access_type type,
6418 struct bpf_insn_access_aux *info)
6421 case bpf_ctx_range_till(struct bpf_sock, type, priority):
6424 return bpf_sock_is_valid_access(off, size, type, info);
6428 bool bpf_sock_is_valid_access(int off, int size, enum bpf_access_type type,
6429 struct bpf_insn_access_aux *info)
6431 const int size_default = sizeof(__u32);
6433 if (off < 0 || off >= sizeof(struct bpf_sock))
6435 if (off % size != 0)
6439 case offsetof(struct bpf_sock, state):
6440 case offsetof(struct bpf_sock, family):
6441 case offsetof(struct bpf_sock, type):
6442 case offsetof(struct bpf_sock, protocol):
6443 case offsetof(struct bpf_sock, dst_port):
6444 case offsetof(struct bpf_sock, src_port):
6445 case bpf_ctx_range(struct bpf_sock, src_ip4):
6446 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
6447 case bpf_ctx_range(struct bpf_sock, dst_ip4):
6448 case bpf_ctx_range_till(struct bpf_sock, dst_ip6[0], dst_ip6[3]):
6449 bpf_ctx_record_field_size(info, size_default);
6450 return bpf_ctx_narrow_access_ok(off, size, size_default);
6453 return size == size_default;
6456 static bool sock_filter_is_valid_access(int off, int size,
6457 enum bpf_access_type type,
6458 const struct bpf_prog *prog,
6459 struct bpf_insn_access_aux *info)
6461 if (!bpf_sock_is_valid_access(off, size, type, info))
6463 return __sock_filter_check_attach_type(off, type,
6464 prog->expected_attach_type);
6467 static int bpf_noop_prologue(struct bpf_insn *insn_buf, bool direct_write,
6468 const struct bpf_prog *prog)
6470 /* Neither direct read nor direct write requires any preliminary
6476 static int bpf_unclone_prologue(struct bpf_insn *insn_buf, bool direct_write,
6477 const struct bpf_prog *prog, int drop_verdict)
6479 struct bpf_insn *insn = insn_buf;
6484 /* if (!skb->cloned)
6487 * (Fast-path, otherwise approximation that we might be
6488 * a clone, do the rest in helper.)
6490 *insn++ = BPF_LDX_MEM(BPF_B, BPF_REG_6, BPF_REG_1, CLONED_OFFSET());
6491 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_6, CLONED_MASK);
6492 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_6, 0, 7);
6494 /* ret = bpf_skb_pull_data(skb, 0); */
6495 *insn++ = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1);
6496 *insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_2, BPF_REG_2);
6497 *insn++ = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
6498 BPF_FUNC_skb_pull_data);
6501 * return TC_ACT_SHOT;
6503 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2);
6504 *insn++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_0, drop_verdict);
6505 *insn++ = BPF_EXIT_INSN();
6508 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_6);
6510 *insn++ = prog->insnsi[0];
6512 return insn - insn_buf;
6515 static int bpf_gen_ld_abs(const struct bpf_insn *orig,
6516 struct bpf_insn *insn_buf)
6518 bool indirect = BPF_MODE(orig->code) == BPF_IND;
6519 struct bpf_insn *insn = insn_buf;
6521 /* We're guaranteed here that CTX is in R6. */
6522 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_CTX);
6524 *insn++ = BPF_MOV64_IMM(BPF_REG_2, orig->imm);
6526 *insn++ = BPF_MOV64_REG(BPF_REG_2, orig->src_reg);
6528 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, orig->imm);
6531 switch (BPF_SIZE(orig->code)) {
6533 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_8_no_cache);
6536 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_16_no_cache);
6539 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_32_no_cache);
6543 *insn++ = BPF_JMP_IMM(BPF_JSGE, BPF_REG_0, 0, 2);
6544 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_0, BPF_REG_0);
6545 *insn++ = BPF_EXIT_INSN();
6547 return insn - insn_buf;
6550 static int tc_cls_act_prologue(struct bpf_insn *insn_buf, bool direct_write,
6551 const struct bpf_prog *prog)
6553 return bpf_unclone_prologue(insn_buf, direct_write, prog, TC_ACT_SHOT);
6556 static bool tc_cls_act_is_valid_access(int off, int size,
6557 enum bpf_access_type type,
6558 const struct bpf_prog *prog,
6559 struct bpf_insn_access_aux *info)
6561 if (type == BPF_WRITE) {
6563 case bpf_ctx_range(struct __sk_buff, mark):
6564 case bpf_ctx_range(struct __sk_buff, tc_index):
6565 case bpf_ctx_range(struct __sk_buff, priority):
6566 case bpf_ctx_range(struct __sk_buff, tc_classid):
6567 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
6568 case bpf_ctx_range(struct __sk_buff, tstamp):
6569 case bpf_ctx_range(struct __sk_buff, queue_mapping):
6577 case bpf_ctx_range(struct __sk_buff, data):
6578 info->reg_type = PTR_TO_PACKET;
6580 case bpf_ctx_range(struct __sk_buff, data_meta):
6581 info->reg_type = PTR_TO_PACKET_META;
6583 case bpf_ctx_range(struct __sk_buff, data_end):
6584 info->reg_type = PTR_TO_PACKET_END;
6586 case bpf_ctx_range_ptr(struct __sk_buff, flow_keys):
6587 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
6591 return bpf_skb_is_valid_access(off, size, type, prog, info);
6594 static bool __is_valid_xdp_access(int off, int size)
6596 if (off < 0 || off >= sizeof(struct xdp_md))
6598 if (off % size != 0)
6600 if (size != sizeof(__u32))
6606 static bool xdp_is_valid_access(int off, int size,
6607 enum bpf_access_type type,
6608 const struct bpf_prog *prog,
6609 struct bpf_insn_access_aux *info)
6611 if (type == BPF_WRITE) {
6612 if (bpf_prog_is_dev_bound(prog->aux)) {
6614 case offsetof(struct xdp_md, rx_queue_index):
6615 return __is_valid_xdp_access(off, size);
6622 case offsetof(struct xdp_md, data):
6623 info->reg_type = PTR_TO_PACKET;
6625 case offsetof(struct xdp_md, data_meta):
6626 info->reg_type = PTR_TO_PACKET_META;
6628 case offsetof(struct xdp_md, data_end):
6629 info->reg_type = PTR_TO_PACKET_END;
6633 return __is_valid_xdp_access(off, size);
6636 void bpf_warn_invalid_xdp_action(u32 act)
6638 const u32 act_max = XDP_REDIRECT;
6640 WARN_ONCE(1, "%s XDP return value %u, expect packet loss!\n",
6641 act > act_max ? "Illegal" : "Driver unsupported",
6644 EXPORT_SYMBOL_GPL(bpf_warn_invalid_xdp_action);
6646 static bool sock_addr_is_valid_access(int off, int size,
6647 enum bpf_access_type type,
6648 const struct bpf_prog *prog,
6649 struct bpf_insn_access_aux *info)
6651 const int size_default = sizeof(__u32);
6653 if (off < 0 || off >= sizeof(struct bpf_sock_addr))
6655 if (off % size != 0)
6658 /* Disallow access to IPv6 fields from IPv4 contex and vise
6662 case bpf_ctx_range(struct bpf_sock_addr, user_ip4):
6663 switch (prog->expected_attach_type) {
6664 case BPF_CGROUP_INET4_BIND:
6665 case BPF_CGROUP_INET4_CONNECT:
6666 case BPF_CGROUP_UDP4_SENDMSG:
6672 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
6673 switch (prog->expected_attach_type) {
6674 case BPF_CGROUP_INET6_BIND:
6675 case BPF_CGROUP_INET6_CONNECT:
6676 case BPF_CGROUP_UDP6_SENDMSG:
6682 case bpf_ctx_range(struct bpf_sock_addr, msg_src_ip4):
6683 switch (prog->expected_attach_type) {
6684 case BPF_CGROUP_UDP4_SENDMSG:
6690 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
6692 switch (prog->expected_attach_type) {
6693 case BPF_CGROUP_UDP6_SENDMSG:
6702 case bpf_ctx_range(struct bpf_sock_addr, user_ip4):
6703 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
6704 case bpf_ctx_range(struct bpf_sock_addr, msg_src_ip4):
6705 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
6707 /* Only narrow read access allowed for now. */
6708 if (type == BPF_READ) {
6709 bpf_ctx_record_field_size(info, size_default);
6710 if (!bpf_ctx_narrow_access_ok(off, size, size_default))
6713 if (size != size_default)
6717 case bpf_ctx_range(struct bpf_sock_addr, user_port):
6718 if (size != size_default)
6722 if (type == BPF_READ) {
6723 if (size != size_default)
6733 static bool sock_ops_is_valid_access(int off, int size,
6734 enum bpf_access_type type,
6735 const struct bpf_prog *prog,
6736 struct bpf_insn_access_aux *info)
6738 const int size_default = sizeof(__u32);
6740 if (off < 0 || off >= sizeof(struct bpf_sock_ops))
6743 /* The verifier guarantees that size > 0. */
6744 if (off % size != 0)
6747 if (type == BPF_WRITE) {
6749 case offsetof(struct bpf_sock_ops, reply):
6750 case offsetof(struct bpf_sock_ops, sk_txhash):
6751 if (size != size_default)
6759 case bpf_ctx_range_till(struct bpf_sock_ops, bytes_received,
6761 if (size != sizeof(__u64))
6765 if (size != size_default)
6774 static int sk_skb_prologue(struct bpf_insn *insn_buf, bool direct_write,
6775 const struct bpf_prog *prog)
6777 return bpf_unclone_prologue(insn_buf, direct_write, prog, SK_DROP);
6780 static bool sk_skb_is_valid_access(int off, int size,
6781 enum bpf_access_type type,
6782 const struct bpf_prog *prog,
6783 struct bpf_insn_access_aux *info)
6786 case bpf_ctx_range(struct __sk_buff, tc_classid):
6787 case bpf_ctx_range(struct __sk_buff, data_meta):
6788 case bpf_ctx_range_ptr(struct __sk_buff, flow_keys):
6789 case bpf_ctx_range(struct __sk_buff, tstamp):
6790 case bpf_ctx_range(struct __sk_buff, wire_len):
6794 if (type == BPF_WRITE) {
6796 case bpf_ctx_range(struct __sk_buff, tc_index):
6797 case bpf_ctx_range(struct __sk_buff, priority):
6805 case bpf_ctx_range(struct __sk_buff, mark):
6807 case bpf_ctx_range(struct __sk_buff, data):
6808 info->reg_type = PTR_TO_PACKET;
6810 case bpf_ctx_range(struct __sk_buff, data_end):
6811 info->reg_type = PTR_TO_PACKET_END;
6815 return bpf_skb_is_valid_access(off, size, type, prog, info);
6818 static bool sk_msg_is_valid_access(int off, int size,
6819 enum bpf_access_type type,
6820 const struct bpf_prog *prog,
6821 struct bpf_insn_access_aux *info)
6823 if (type == BPF_WRITE)
6826 if (off % size != 0)
6830 case offsetof(struct sk_msg_md, data):
6831 info->reg_type = PTR_TO_PACKET;
6832 if (size != sizeof(__u64))
6835 case offsetof(struct sk_msg_md, data_end):
6836 info->reg_type = PTR_TO_PACKET_END;
6837 if (size != sizeof(__u64))
6840 case bpf_ctx_range(struct sk_msg_md, family):
6841 case bpf_ctx_range(struct sk_msg_md, remote_ip4):
6842 case bpf_ctx_range(struct sk_msg_md, local_ip4):
6843 case bpf_ctx_range_till(struct sk_msg_md, remote_ip6[0], remote_ip6[3]):
6844 case bpf_ctx_range_till(struct sk_msg_md, local_ip6[0], local_ip6[3]):
6845 case bpf_ctx_range(struct sk_msg_md, remote_port):
6846 case bpf_ctx_range(struct sk_msg_md, local_port):
6847 case bpf_ctx_range(struct sk_msg_md, size):
6848 if (size != sizeof(__u32))
6857 static bool flow_dissector_is_valid_access(int off, int size,
6858 enum bpf_access_type type,
6859 const struct bpf_prog *prog,
6860 struct bpf_insn_access_aux *info)
6862 if (type == BPF_WRITE) {
6864 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
6872 case bpf_ctx_range(struct __sk_buff, data):
6873 info->reg_type = PTR_TO_PACKET;
6875 case bpf_ctx_range(struct __sk_buff, data_end):
6876 info->reg_type = PTR_TO_PACKET_END;
6878 case bpf_ctx_range_ptr(struct __sk_buff, flow_keys):
6879 info->reg_type = PTR_TO_FLOW_KEYS;
6881 case bpf_ctx_range(struct __sk_buff, tc_classid):
6882 case bpf_ctx_range(struct __sk_buff, data_meta):
6883 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
6884 case bpf_ctx_range(struct __sk_buff, tstamp):
6885 case bpf_ctx_range(struct __sk_buff, wire_len):
6889 return bpf_skb_is_valid_access(off, size, type, prog, info);
6892 static u32 bpf_convert_ctx_access(enum bpf_access_type type,
6893 const struct bpf_insn *si,
6894 struct bpf_insn *insn_buf,
6895 struct bpf_prog *prog, u32 *target_size)
6897 struct bpf_insn *insn = insn_buf;
6901 case offsetof(struct __sk_buff, len):
6902 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6903 bpf_target_off(struct sk_buff, len, 4,
6907 case offsetof(struct __sk_buff, protocol):
6908 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
6909 bpf_target_off(struct sk_buff, protocol, 2,
6913 case offsetof(struct __sk_buff, vlan_proto):
6914 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
6915 bpf_target_off(struct sk_buff, vlan_proto, 2,
6919 case offsetof(struct __sk_buff, priority):
6920 if (type == BPF_WRITE)
6921 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
6922 bpf_target_off(struct sk_buff, priority, 4,
6925 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6926 bpf_target_off(struct sk_buff, priority, 4,
6930 case offsetof(struct __sk_buff, ingress_ifindex):
6931 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6932 bpf_target_off(struct sk_buff, skb_iif, 4,
6936 case offsetof(struct __sk_buff, ifindex):
6937 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
6938 si->dst_reg, si->src_reg,
6939 offsetof(struct sk_buff, dev));
6940 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
6941 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6942 bpf_target_off(struct net_device, ifindex, 4,
6946 case offsetof(struct __sk_buff, hash):
6947 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6948 bpf_target_off(struct sk_buff, hash, 4,
6952 case offsetof(struct __sk_buff, mark):
6953 if (type == BPF_WRITE)
6954 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
6955 bpf_target_off(struct sk_buff, mark, 4,
6958 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6959 bpf_target_off(struct sk_buff, mark, 4,
6963 case offsetof(struct __sk_buff, pkt_type):
6965 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->src_reg,
6967 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, PKT_TYPE_MAX);
6968 #ifdef __BIG_ENDIAN_BITFIELD
6969 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, 5);
6973 case offsetof(struct __sk_buff, queue_mapping):
6974 if (type == BPF_WRITE) {
6975 *insn++ = BPF_JMP_IMM(BPF_JGE, si->src_reg, NO_QUEUE_MAPPING, 1);
6976 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg, si->src_reg,
6977 bpf_target_off(struct sk_buff,
6981 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
6982 bpf_target_off(struct sk_buff,
6988 case offsetof(struct __sk_buff, vlan_present):
6990 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->src_reg,
6991 PKT_VLAN_PRESENT_OFFSET());
6992 if (PKT_VLAN_PRESENT_BIT)
6993 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, PKT_VLAN_PRESENT_BIT);
6994 if (PKT_VLAN_PRESENT_BIT < 7)
6995 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, 1);
6998 case offsetof(struct __sk_buff, vlan_tci):
6999 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
7000 bpf_target_off(struct sk_buff, vlan_tci, 2,
7004 case offsetof(struct __sk_buff, cb[0]) ...
7005 offsetofend(struct __sk_buff, cb[4]) - 1:
7006 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, data) < 20);
7007 BUILD_BUG_ON((offsetof(struct sk_buff, cb) +
7008 offsetof(struct qdisc_skb_cb, data)) %
7011 prog->cb_access = 1;
7013 off -= offsetof(struct __sk_buff, cb[0]);
7014 off += offsetof(struct sk_buff, cb);
7015 off += offsetof(struct qdisc_skb_cb, data);
7016 if (type == BPF_WRITE)
7017 *insn++ = BPF_STX_MEM(BPF_SIZE(si->code), si->dst_reg,
7020 *insn++ = BPF_LDX_MEM(BPF_SIZE(si->code), si->dst_reg,
7024 case offsetof(struct __sk_buff, tc_classid):
7025 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, tc_classid) != 2);
7028 off -= offsetof(struct __sk_buff, tc_classid);
7029 off += offsetof(struct sk_buff, cb);
7030 off += offsetof(struct qdisc_skb_cb, tc_classid);
7032 if (type == BPF_WRITE)
7033 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg,
7036 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg,
7040 case offsetof(struct __sk_buff, data):
7041 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
7042 si->dst_reg, si->src_reg,
7043 offsetof(struct sk_buff, data));
7046 case offsetof(struct __sk_buff, data_meta):
7048 off -= offsetof(struct __sk_buff, data_meta);
7049 off += offsetof(struct sk_buff, cb);
7050 off += offsetof(struct bpf_skb_data_end, data_meta);
7051 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
7055 case offsetof(struct __sk_buff, data_end):
7057 off -= offsetof(struct __sk_buff, data_end);
7058 off += offsetof(struct sk_buff, cb);
7059 off += offsetof(struct bpf_skb_data_end, data_end);
7060 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
7064 case offsetof(struct __sk_buff, tc_index):
7065 #ifdef CONFIG_NET_SCHED
7066 if (type == BPF_WRITE)
7067 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg, si->src_reg,
7068 bpf_target_off(struct sk_buff, tc_index, 2,
7071 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
7072 bpf_target_off(struct sk_buff, tc_index, 2,
7076 if (type == BPF_WRITE)
7077 *insn++ = BPF_MOV64_REG(si->dst_reg, si->dst_reg);
7079 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
7083 case offsetof(struct __sk_buff, napi_id):
7084 #if defined(CONFIG_NET_RX_BUSY_POLL)
7085 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
7086 bpf_target_off(struct sk_buff, napi_id, 4,
7088 *insn++ = BPF_JMP_IMM(BPF_JGE, si->dst_reg, MIN_NAPI_ID, 1);
7089 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
7092 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
7095 case offsetof(struct __sk_buff, family):
7096 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_family) != 2);
7098 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
7099 si->dst_reg, si->src_reg,
7100 offsetof(struct sk_buff, sk));
7101 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
7102 bpf_target_off(struct sock_common,
7106 case offsetof(struct __sk_buff, remote_ip4):
7107 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_daddr) != 4);
7109 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
7110 si->dst_reg, si->src_reg,
7111 offsetof(struct sk_buff, sk));
7112 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7113 bpf_target_off(struct sock_common,
7117 case offsetof(struct __sk_buff, local_ip4):
7118 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
7119 skc_rcv_saddr) != 4);
7121 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
7122 si->dst_reg, si->src_reg,
7123 offsetof(struct sk_buff, sk));
7124 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7125 bpf_target_off(struct sock_common,
7129 case offsetof(struct __sk_buff, remote_ip6[0]) ...
7130 offsetof(struct __sk_buff, remote_ip6[3]):
7131 #if IS_ENABLED(CONFIG_IPV6)
7132 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
7133 skc_v6_daddr.s6_addr32[0]) != 4);
7136 off -= offsetof(struct __sk_buff, remote_ip6[0]);
7138 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
7139 si->dst_reg, si->src_reg,
7140 offsetof(struct sk_buff, sk));
7141 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7142 offsetof(struct sock_common,
7143 skc_v6_daddr.s6_addr32[0]) +
7146 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
7149 case offsetof(struct __sk_buff, local_ip6[0]) ...
7150 offsetof(struct __sk_buff, local_ip6[3]):
7151 #if IS_ENABLED(CONFIG_IPV6)
7152 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
7153 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
7156 off -= offsetof(struct __sk_buff, local_ip6[0]);
7158 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
7159 si->dst_reg, si->src_reg,
7160 offsetof(struct sk_buff, sk));
7161 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7162 offsetof(struct sock_common,
7163 skc_v6_rcv_saddr.s6_addr32[0]) +
7166 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
7170 case offsetof(struct __sk_buff, remote_port):
7171 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_dport) != 2);
7173 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
7174 si->dst_reg, si->src_reg,
7175 offsetof(struct sk_buff, sk));
7176 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
7177 bpf_target_off(struct sock_common,
7180 #ifndef __BIG_ENDIAN_BITFIELD
7181 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
7185 case offsetof(struct __sk_buff, local_port):
7186 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_num) != 2);
7188 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
7189 si->dst_reg, si->src_reg,
7190 offsetof(struct sk_buff, sk));
7191 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
7192 bpf_target_off(struct sock_common,
7193 skc_num, 2, target_size));
7196 case offsetof(struct __sk_buff, flow_keys):
7198 off -= offsetof(struct __sk_buff, flow_keys);
7199 off += offsetof(struct sk_buff, cb);
7200 off += offsetof(struct qdisc_skb_cb, flow_keys);
7201 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
7205 case offsetof(struct __sk_buff, tstamp):
7206 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, tstamp) != 8);
7208 if (type == BPF_WRITE)
7209 *insn++ = BPF_STX_MEM(BPF_DW,
7210 si->dst_reg, si->src_reg,
7211 bpf_target_off(struct sk_buff,
7215 *insn++ = BPF_LDX_MEM(BPF_DW,
7216 si->dst_reg, si->src_reg,
7217 bpf_target_off(struct sk_buff,
7222 case offsetof(struct __sk_buff, gso_segs):
7223 /* si->dst_reg = skb_shinfo(SKB); */
7224 #ifdef NET_SKBUFF_DATA_USES_OFFSET
7225 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, head),
7226 si->dst_reg, si->src_reg,
7227 offsetof(struct sk_buff, head));
7228 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, end),
7229 BPF_REG_AX, si->src_reg,
7230 offsetof(struct sk_buff, end));
7231 *insn++ = BPF_ALU64_REG(BPF_ADD, si->dst_reg, BPF_REG_AX);
7233 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, end),
7234 si->dst_reg, si->src_reg,
7235 offsetof(struct sk_buff, end));
7237 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct skb_shared_info, gso_segs),
7238 si->dst_reg, si->dst_reg,
7239 bpf_target_off(struct skb_shared_info,
7243 case offsetof(struct __sk_buff, wire_len):
7244 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, pkt_len) != 4);
7247 off -= offsetof(struct __sk_buff, wire_len);
7248 off += offsetof(struct sk_buff, cb);
7249 off += offsetof(struct qdisc_skb_cb, pkt_len);
7251 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, off);
7254 case offsetof(struct __sk_buff, sk):
7255 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
7256 si->dst_reg, si->src_reg,
7257 offsetof(struct sk_buff, sk));
7261 return insn - insn_buf;
7264 u32 bpf_sock_convert_ctx_access(enum bpf_access_type type,
7265 const struct bpf_insn *si,
7266 struct bpf_insn *insn_buf,
7267 struct bpf_prog *prog, u32 *target_size)
7269 struct bpf_insn *insn = insn_buf;
7273 case offsetof(struct bpf_sock, bound_dev_if):
7274 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_bound_dev_if) != 4);
7276 if (type == BPF_WRITE)
7277 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
7278 offsetof(struct sock, sk_bound_dev_if));
7280 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
7281 offsetof(struct sock, sk_bound_dev_if));
7284 case offsetof(struct bpf_sock, mark):
7285 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_mark) != 4);
7287 if (type == BPF_WRITE)
7288 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
7289 offsetof(struct sock, sk_mark));
7291 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
7292 offsetof(struct sock, sk_mark));
7295 case offsetof(struct bpf_sock, priority):
7296 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_priority) != 4);
7298 if (type == BPF_WRITE)
7299 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
7300 offsetof(struct sock, sk_priority));
7302 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
7303 offsetof(struct sock, sk_priority));
7306 case offsetof(struct bpf_sock, family):
7307 *insn++ = BPF_LDX_MEM(
7308 BPF_FIELD_SIZEOF(struct sock_common, skc_family),
7309 si->dst_reg, si->src_reg,
7310 bpf_target_off(struct sock_common,
7312 FIELD_SIZEOF(struct sock_common,
7317 case offsetof(struct bpf_sock, type):
7318 BUILD_BUG_ON(HWEIGHT32(SK_FL_TYPE_MASK) != BITS_PER_BYTE * 2);
7319 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
7320 offsetof(struct sock, __sk_flags_offset));
7321 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_TYPE_MASK);
7322 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, SK_FL_TYPE_SHIFT);
7326 case offsetof(struct bpf_sock, protocol):
7327 BUILD_BUG_ON(HWEIGHT32(SK_FL_PROTO_MASK) != BITS_PER_BYTE);
7328 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
7329 offsetof(struct sock, __sk_flags_offset));
7330 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_PROTO_MASK);
7331 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, SK_FL_PROTO_SHIFT);
7335 case offsetof(struct bpf_sock, src_ip4):
7336 *insn++ = BPF_LDX_MEM(
7337 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
7338 bpf_target_off(struct sock_common, skc_rcv_saddr,
7339 FIELD_SIZEOF(struct sock_common,
7344 case offsetof(struct bpf_sock, dst_ip4):
7345 *insn++ = BPF_LDX_MEM(
7346 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
7347 bpf_target_off(struct sock_common, skc_daddr,
7348 FIELD_SIZEOF(struct sock_common,
7353 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
7354 #if IS_ENABLED(CONFIG_IPV6)
7356 off -= offsetof(struct bpf_sock, src_ip6[0]);
7357 *insn++ = BPF_LDX_MEM(
7358 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
7361 skc_v6_rcv_saddr.s6_addr32[0],
7362 FIELD_SIZEOF(struct sock_common,
7363 skc_v6_rcv_saddr.s6_addr32[0]),
7364 target_size) + off);
7367 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
7371 case bpf_ctx_range_till(struct bpf_sock, dst_ip6[0], dst_ip6[3]):
7372 #if IS_ENABLED(CONFIG_IPV6)
7374 off -= offsetof(struct bpf_sock, dst_ip6[0]);
7375 *insn++ = BPF_LDX_MEM(
7376 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
7377 bpf_target_off(struct sock_common,
7378 skc_v6_daddr.s6_addr32[0],
7379 FIELD_SIZEOF(struct sock_common,
7380 skc_v6_daddr.s6_addr32[0]),
7381 target_size) + off);
7383 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
7388 case offsetof(struct bpf_sock, src_port):
7389 *insn++ = BPF_LDX_MEM(
7390 BPF_FIELD_SIZEOF(struct sock_common, skc_num),
7391 si->dst_reg, si->src_reg,
7392 bpf_target_off(struct sock_common, skc_num,
7393 FIELD_SIZEOF(struct sock_common,
7398 case offsetof(struct bpf_sock, dst_port):
7399 *insn++ = BPF_LDX_MEM(
7400 BPF_FIELD_SIZEOF(struct sock_common, skc_dport),
7401 si->dst_reg, si->src_reg,
7402 bpf_target_off(struct sock_common, skc_dport,
7403 FIELD_SIZEOF(struct sock_common,
7408 case offsetof(struct bpf_sock, state):
7409 *insn++ = BPF_LDX_MEM(
7410 BPF_FIELD_SIZEOF(struct sock_common, skc_state),
7411 si->dst_reg, si->src_reg,
7412 bpf_target_off(struct sock_common, skc_state,
7413 FIELD_SIZEOF(struct sock_common,
7419 return insn - insn_buf;
7422 static u32 tc_cls_act_convert_ctx_access(enum bpf_access_type type,
7423 const struct bpf_insn *si,
7424 struct bpf_insn *insn_buf,
7425 struct bpf_prog *prog, u32 *target_size)
7427 struct bpf_insn *insn = insn_buf;
7430 case offsetof(struct __sk_buff, ifindex):
7431 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
7432 si->dst_reg, si->src_reg,
7433 offsetof(struct sk_buff, dev));
7434 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7435 bpf_target_off(struct net_device, ifindex, 4,
7439 return bpf_convert_ctx_access(type, si, insn_buf, prog,
7443 return insn - insn_buf;
7446 static u32 xdp_convert_ctx_access(enum bpf_access_type type,
7447 const struct bpf_insn *si,
7448 struct bpf_insn *insn_buf,
7449 struct bpf_prog *prog, u32 *target_size)
7451 struct bpf_insn *insn = insn_buf;
7454 case offsetof(struct xdp_md, data):
7455 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data),
7456 si->dst_reg, si->src_reg,
7457 offsetof(struct xdp_buff, data));
7459 case offsetof(struct xdp_md, data_meta):
7460 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_meta),
7461 si->dst_reg, si->src_reg,
7462 offsetof(struct xdp_buff, data_meta));
7464 case offsetof(struct xdp_md, data_end):
7465 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_end),
7466 si->dst_reg, si->src_reg,
7467 offsetof(struct xdp_buff, data_end));
7469 case offsetof(struct xdp_md, ingress_ifindex):
7470 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, rxq),
7471 si->dst_reg, si->src_reg,
7472 offsetof(struct xdp_buff, rxq));
7473 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_rxq_info, dev),
7474 si->dst_reg, si->dst_reg,
7475 offsetof(struct xdp_rxq_info, dev));
7476 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7477 offsetof(struct net_device, ifindex));
7479 case offsetof(struct xdp_md, rx_queue_index):
7480 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, rxq),
7481 si->dst_reg, si->src_reg,
7482 offsetof(struct xdp_buff, rxq));
7483 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7484 offsetof(struct xdp_rxq_info,
7489 return insn - insn_buf;
7492 /* SOCK_ADDR_LOAD_NESTED_FIELD() loads Nested Field S.F.NF where S is type of
7493 * context Structure, F is Field in context structure that contains a pointer
7494 * to Nested Structure of type NS that has the field NF.
7496 * SIZE encodes the load size (BPF_B, BPF_H, etc). It's up to caller to make
7497 * sure that SIZE is not greater than actual size of S.F.NF.
7499 * If offset OFF is provided, the load happens from that offset relative to
7502 #define SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, SIZE, OFF) \
7504 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), si->dst_reg, \
7505 si->src_reg, offsetof(S, F)); \
7506 *insn++ = BPF_LDX_MEM( \
7507 SIZE, si->dst_reg, si->dst_reg, \
7508 bpf_target_off(NS, NF, FIELD_SIZEOF(NS, NF), \
7513 #define SOCK_ADDR_LOAD_NESTED_FIELD(S, NS, F, NF) \
7514 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, \
7515 BPF_FIELD_SIZEOF(NS, NF), 0)
7517 /* SOCK_ADDR_STORE_NESTED_FIELD_OFF() has semantic similar to
7518 * SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF() but for store operation.
7520 * It doesn't support SIZE argument though since narrow stores are not
7521 * supported for now.
7523 * In addition it uses Temporary Field TF (member of struct S) as the 3rd
7524 * "register" since two registers available in convert_ctx_access are not
7525 * enough: we can't override neither SRC, since it contains value to store, nor
7526 * DST since it contains pointer to context that may be used by later
7527 * instructions. But we need a temporary place to save pointer to nested
7528 * structure whose field we want to store to.
7530 #define SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, OFF, TF) \
7532 int tmp_reg = BPF_REG_9; \
7533 if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg) \
7535 if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg) \
7537 *insn++ = BPF_STX_MEM(BPF_DW, si->dst_reg, tmp_reg, \
7539 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), tmp_reg, \
7540 si->dst_reg, offsetof(S, F)); \
7541 *insn++ = BPF_STX_MEM( \
7542 BPF_FIELD_SIZEOF(NS, NF), tmp_reg, si->src_reg, \
7543 bpf_target_off(NS, NF, FIELD_SIZEOF(NS, NF), \
7546 *insn++ = BPF_LDX_MEM(BPF_DW, tmp_reg, si->dst_reg, \
7550 #define SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, SIZE, OFF, \
7553 if (type == BPF_WRITE) { \
7554 SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, OFF, \
7557 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF( \
7558 S, NS, F, NF, SIZE, OFF); \
7562 #define SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD(S, NS, F, NF, TF) \
7563 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF( \
7564 S, NS, F, NF, BPF_FIELD_SIZEOF(NS, NF), 0, TF)
7566 static u32 sock_addr_convert_ctx_access(enum bpf_access_type type,
7567 const struct bpf_insn *si,
7568 struct bpf_insn *insn_buf,
7569 struct bpf_prog *prog, u32 *target_size)
7571 struct bpf_insn *insn = insn_buf;
7575 case offsetof(struct bpf_sock_addr, user_family):
7576 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
7577 struct sockaddr, uaddr, sa_family);
7580 case offsetof(struct bpf_sock_addr, user_ip4):
7581 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
7582 struct bpf_sock_addr_kern, struct sockaddr_in, uaddr,
7583 sin_addr, BPF_SIZE(si->code), 0, tmp_reg);
7586 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
7588 off -= offsetof(struct bpf_sock_addr, user_ip6[0]);
7589 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
7590 struct bpf_sock_addr_kern, struct sockaddr_in6, uaddr,
7591 sin6_addr.s6_addr32[0], BPF_SIZE(si->code), off,
7595 case offsetof(struct bpf_sock_addr, user_port):
7596 /* To get port we need to know sa_family first and then treat
7597 * sockaddr as either sockaddr_in or sockaddr_in6.
7598 * Though we can simplify since port field has same offset and
7599 * size in both structures.
7600 * Here we check this invariant and use just one of the
7601 * structures if it's true.
7603 BUILD_BUG_ON(offsetof(struct sockaddr_in, sin_port) !=
7604 offsetof(struct sockaddr_in6, sin6_port));
7605 BUILD_BUG_ON(FIELD_SIZEOF(struct sockaddr_in, sin_port) !=
7606 FIELD_SIZEOF(struct sockaddr_in6, sin6_port));
7607 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD(struct bpf_sock_addr_kern,
7608 struct sockaddr_in6, uaddr,
7609 sin6_port, tmp_reg);
7612 case offsetof(struct bpf_sock_addr, family):
7613 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
7614 struct sock, sk, sk_family);
7617 case offsetof(struct bpf_sock_addr, type):
7618 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(
7619 struct bpf_sock_addr_kern, struct sock, sk,
7620 __sk_flags_offset, BPF_W, 0);
7621 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_TYPE_MASK);
7622 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, SK_FL_TYPE_SHIFT);
7625 case offsetof(struct bpf_sock_addr, protocol):
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_PROTO_MASK);
7630 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg,
7634 case offsetof(struct bpf_sock_addr, msg_src_ip4):
7635 /* Treat t_ctx as struct in_addr for msg_src_ip4. */
7636 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
7637 struct bpf_sock_addr_kern, struct in_addr, t_ctx,
7638 s_addr, BPF_SIZE(si->code), 0, tmp_reg);
7641 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
7644 off -= offsetof(struct bpf_sock_addr, msg_src_ip6[0]);
7645 /* Treat t_ctx as struct in6_addr for msg_src_ip6. */
7646 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
7647 struct bpf_sock_addr_kern, struct in6_addr, t_ctx,
7648 s6_addr32[0], BPF_SIZE(si->code), off, tmp_reg);
7652 return insn - insn_buf;
7655 static u32 sock_ops_convert_ctx_access(enum bpf_access_type type,
7656 const struct bpf_insn *si,
7657 struct bpf_insn *insn_buf,
7658 struct bpf_prog *prog,
7661 struct bpf_insn *insn = insn_buf;
7664 /* Helper macro for adding read access to tcp_sock or sock fields. */
7665 #define SOCK_OPS_GET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ) \
7667 BUILD_BUG_ON(FIELD_SIZEOF(OBJ, OBJ_FIELD) > \
7668 FIELD_SIZEOF(struct bpf_sock_ops, BPF_FIELD)); \
7669 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
7670 struct bpf_sock_ops_kern, \
7672 si->dst_reg, si->src_reg, \
7673 offsetof(struct bpf_sock_ops_kern, \
7675 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 2); \
7676 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
7677 struct bpf_sock_ops_kern, sk),\
7678 si->dst_reg, si->src_reg, \
7679 offsetof(struct bpf_sock_ops_kern, sk));\
7680 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(OBJ, \
7682 si->dst_reg, si->dst_reg, \
7683 offsetof(OBJ, OBJ_FIELD)); \
7686 #define SOCK_OPS_GET_TCP_SOCK_FIELD(FIELD) \
7687 SOCK_OPS_GET_FIELD(FIELD, FIELD, struct tcp_sock)
7689 /* Helper macro for adding write access to tcp_sock or sock fields.
7690 * The macro is called with two registers, dst_reg which contains a pointer
7691 * to ctx (context) and src_reg which contains the value that should be
7692 * stored. However, we need an additional register since we cannot overwrite
7693 * dst_reg because it may be used later in the program.
7694 * Instead we "borrow" one of the other register. We first save its value
7695 * into a new (temp) field in bpf_sock_ops_kern, use it, and then restore
7696 * it at the end of the macro.
7698 #define SOCK_OPS_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ) \
7700 int reg = BPF_REG_9; \
7701 BUILD_BUG_ON(FIELD_SIZEOF(OBJ, OBJ_FIELD) > \
7702 FIELD_SIZEOF(struct bpf_sock_ops, BPF_FIELD)); \
7703 if (si->dst_reg == reg || si->src_reg == reg) \
7705 if (si->dst_reg == reg || si->src_reg == reg) \
7707 *insn++ = BPF_STX_MEM(BPF_DW, si->dst_reg, reg, \
7708 offsetof(struct bpf_sock_ops_kern, \
7710 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
7711 struct bpf_sock_ops_kern, \
7714 offsetof(struct bpf_sock_ops_kern, \
7716 *insn++ = BPF_JMP_IMM(BPF_JEQ, reg, 0, 2); \
7717 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
7718 struct bpf_sock_ops_kern, sk),\
7720 offsetof(struct bpf_sock_ops_kern, sk));\
7721 *insn++ = BPF_STX_MEM(BPF_FIELD_SIZEOF(OBJ, OBJ_FIELD), \
7723 offsetof(OBJ, OBJ_FIELD)); \
7724 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->dst_reg, \
7725 offsetof(struct bpf_sock_ops_kern, \
7729 #define SOCK_OPS_GET_OR_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ, TYPE) \
7731 if (TYPE == BPF_WRITE) \
7732 SOCK_OPS_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ); \
7734 SOCK_OPS_GET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ); \
7737 CONVERT_COMMON_TCP_SOCK_FIELDS(struct bpf_sock_ops,
7738 SOCK_OPS_GET_TCP_SOCK_FIELD);
7740 if (insn > insn_buf)
7741 return insn - insn_buf;
7744 case offsetof(struct bpf_sock_ops, op) ...
7745 offsetof(struct bpf_sock_ops, replylong[3]):
7746 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, op) !=
7747 FIELD_SIZEOF(struct bpf_sock_ops_kern, op));
7748 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, reply) !=
7749 FIELD_SIZEOF(struct bpf_sock_ops_kern, reply));
7750 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, replylong) !=
7751 FIELD_SIZEOF(struct bpf_sock_ops_kern, replylong));
7753 off -= offsetof(struct bpf_sock_ops, op);
7754 off += offsetof(struct bpf_sock_ops_kern, op);
7755 if (type == BPF_WRITE)
7756 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
7759 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
7763 case offsetof(struct bpf_sock_ops, family):
7764 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_family) != 2);
7766 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7767 struct bpf_sock_ops_kern, sk),
7768 si->dst_reg, si->src_reg,
7769 offsetof(struct bpf_sock_ops_kern, sk));
7770 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
7771 offsetof(struct sock_common, skc_family));
7774 case offsetof(struct bpf_sock_ops, remote_ip4):
7775 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_daddr) != 4);
7777 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7778 struct bpf_sock_ops_kern, sk),
7779 si->dst_reg, si->src_reg,
7780 offsetof(struct bpf_sock_ops_kern, sk));
7781 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7782 offsetof(struct sock_common, skc_daddr));
7785 case offsetof(struct bpf_sock_ops, local_ip4):
7786 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
7787 skc_rcv_saddr) != 4);
7789 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7790 struct bpf_sock_ops_kern, sk),
7791 si->dst_reg, si->src_reg,
7792 offsetof(struct bpf_sock_ops_kern, sk));
7793 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7794 offsetof(struct sock_common,
7798 case offsetof(struct bpf_sock_ops, remote_ip6[0]) ...
7799 offsetof(struct bpf_sock_ops, remote_ip6[3]):
7800 #if IS_ENABLED(CONFIG_IPV6)
7801 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
7802 skc_v6_daddr.s6_addr32[0]) != 4);
7805 off -= offsetof(struct bpf_sock_ops, remote_ip6[0]);
7806 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7807 struct bpf_sock_ops_kern, sk),
7808 si->dst_reg, si->src_reg,
7809 offsetof(struct bpf_sock_ops_kern, sk));
7810 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7811 offsetof(struct sock_common,
7812 skc_v6_daddr.s6_addr32[0]) +
7815 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
7819 case offsetof(struct bpf_sock_ops, local_ip6[0]) ...
7820 offsetof(struct bpf_sock_ops, local_ip6[3]):
7821 #if IS_ENABLED(CONFIG_IPV6)
7822 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
7823 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
7826 off -= offsetof(struct bpf_sock_ops, local_ip6[0]);
7827 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7828 struct bpf_sock_ops_kern, sk),
7829 si->dst_reg, si->src_reg,
7830 offsetof(struct bpf_sock_ops_kern, sk));
7831 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7832 offsetof(struct sock_common,
7833 skc_v6_rcv_saddr.s6_addr32[0]) +
7836 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
7840 case offsetof(struct bpf_sock_ops, remote_port):
7841 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_dport) != 2);
7843 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7844 struct bpf_sock_ops_kern, sk),
7845 si->dst_reg, si->src_reg,
7846 offsetof(struct bpf_sock_ops_kern, sk));
7847 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
7848 offsetof(struct sock_common, skc_dport));
7849 #ifndef __BIG_ENDIAN_BITFIELD
7850 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
7854 case offsetof(struct bpf_sock_ops, local_port):
7855 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_num) != 2);
7857 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7858 struct bpf_sock_ops_kern, sk),
7859 si->dst_reg, si->src_reg,
7860 offsetof(struct bpf_sock_ops_kern, sk));
7861 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
7862 offsetof(struct sock_common, skc_num));
7865 case offsetof(struct bpf_sock_ops, is_fullsock):
7866 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7867 struct bpf_sock_ops_kern,
7869 si->dst_reg, si->src_reg,
7870 offsetof(struct bpf_sock_ops_kern,
7874 case offsetof(struct bpf_sock_ops, state):
7875 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_state) != 1);
7877 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7878 struct bpf_sock_ops_kern, sk),
7879 si->dst_reg, si->src_reg,
7880 offsetof(struct bpf_sock_ops_kern, sk));
7881 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->dst_reg,
7882 offsetof(struct sock_common, skc_state));
7885 case offsetof(struct bpf_sock_ops, rtt_min):
7886 BUILD_BUG_ON(FIELD_SIZEOF(struct tcp_sock, rtt_min) !=
7887 sizeof(struct minmax));
7888 BUILD_BUG_ON(sizeof(struct minmax) <
7889 sizeof(struct minmax_sample));
7891 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7892 struct bpf_sock_ops_kern, sk),
7893 si->dst_reg, si->src_reg,
7894 offsetof(struct bpf_sock_ops_kern, sk));
7895 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7896 offsetof(struct tcp_sock, rtt_min) +
7897 FIELD_SIZEOF(struct minmax_sample, t));
7900 case offsetof(struct bpf_sock_ops, bpf_sock_ops_cb_flags):
7901 SOCK_OPS_GET_FIELD(bpf_sock_ops_cb_flags, bpf_sock_ops_cb_flags,
7905 case offsetof(struct bpf_sock_ops, sk_txhash):
7906 SOCK_OPS_GET_OR_SET_FIELD(sk_txhash, sk_txhash,
7910 return insn - insn_buf;
7913 static u32 sk_skb_convert_ctx_access(enum bpf_access_type type,
7914 const struct bpf_insn *si,
7915 struct bpf_insn *insn_buf,
7916 struct bpf_prog *prog, u32 *target_size)
7918 struct bpf_insn *insn = insn_buf;
7922 case offsetof(struct __sk_buff, data_end):
7924 off -= offsetof(struct __sk_buff, data_end);
7925 off += offsetof(struct sk_buff, cb);
7926 off += offsetof(struct tcp_skb_cb, bpf.data_end);
7927 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
7931 return bpf_convert_ctx_access(type, si, insn_buf, prog,
7935 return insn - insn_buf;
7938 static u32 sk_msg_convert_ctx_access(enum bpf_access_type type,
7939 const struct bpf_insn *si,
7940 struct bpf_insn *insn_buf,
7941 struct bpf_prog *prog, u32 *target_size)
7943 struct bpf_insn *insn = insn_buf;
7944 #if IS_ENABLED(CONFIG_IPV6)
7948 /* convert ctx uses the fact sg element is first in struct */
7949 BUILD_BUG_ON(offsetof(struct sk_msg, sg) != 0);
7952 case offsetof(struct sk_msg_md, data):
7953 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg, data),
7954 si->dst_reg, si->src_reg,
7955 offsetof(struct sk_msg, data));
7957 case offsetof(struct sk_msg_md, data_end):
7958 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg, data_end),
7959 si->dst_reg, si->src_reg,
7960 offsetof(struct sk_msg, data_end));
7962 case offsetof(struct sk_msg_md, family):
7963 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_family) != 2);
7965 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7967 si->dst_reg, si->src_reg,
7968 offsetof(struct sk_msg, sk));
7969 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
7970 offsetof(struct sock_common, skc_family));
7973 case offsetof(struct sk_msg_md, remote_ip4):
7974 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_daddr) != 4);
7976 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7978 si->dst_reg, si->src_reg,
7979 offsetof(struct sk_msg, sk));
7980 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7981 offsetof(struct sock_common, skc_daddr));
7984 case offsetof(struct sk_msg_md, local_ip4):
7985 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
7986 skc_rcv_saddr) != 4);
7988 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7990 si->dst_reg, si->src_reg,
7991 offsetof(struct sk_msg, sk));
7992 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7993 offsetof(struct sock_common,
7997 case offsetof(struct sk_msg_md, remote_ip6[0]) ...
7998 offsetof(struct sk_msg_md, remote_ip6[3]):
7999 #if IS_ENABLED(CONFIG_IPV6)
8000 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
8001 skc_v6_daddr.s6_addr32[0]) != 4);
8004 off -= offsetof(struct sk_msg_md, remote_ip6[0]);
8005 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8007 si->dst_reg, si->src_reg,
8008 offsetof(struct sk_msg, sk));
8009 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
8010 offsetof(struct sock_common,
8011 skc_v6_daddr.s6_addr32[0]) +
8014 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
8018 case offsetof(struct sk_msg_md, local_ip6[0]) ...
8019 offsetof(struct sk_msg_md, local_ip6[3]):
8020 #if IS_ENABLED(CONFIG_IPV6)
8021 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
8022 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
8025 off -= offsetof(struct sk_msg_md, local_ip6[0]);
8026 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8028 si->dst_reg, si->src_reg,
8029 offsetof(struct sk_msg, sk));
8030 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
8031 offsetof(struct sock_common,
8032 skc_v6_rcv_saddr.s6_addr32[0]) +
8035 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
8039 case offsetof(struct sk_msg_md, remote_port):
8040 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_dport) != 2);
8042 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8044 si->dst_reg, si->src_reg,
8045 offsetof(struct sk_msg, sk));
8046 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
8047 offsetof(struct sock_common, skc_dport));
8048 #ifndef __BIG_ENDIAN_BITFIELD
8049 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
8053 case offsetof(struct sk_msg_md, local_port):
8054 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_num) != 2);
8056 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8058 si->dst_reg, si->src_reg,
8059 offsetof(struct sk_msg, sk));
8060 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
8061 offsetof(struct sock_common, skc_num));
8064 case offsetof(struct sk_msg_md, size):
8065 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg_sg, size),
8066 si->dst_reg, si->src_reg,
8067 offsetof(struct sk_msg_sg, size));
8071 return insn - insn_buf;
8074 const struct bpf_verifier_ops sk_filter_verifier_ops = {
8075 .get_func_proto = sk_filter_func_proto,
8076 .is_valid_access = sk_filter_is_valid_access,
8077 .convert_ctx_access = bpf_convert_ctx_access,
8078 .gen_ld_abs = bpf_gen_ld_abs,
8081 const struct bpf_prog_ops sk_filter_prog_ops = {
8082 .test_run = bpf_prog_test_run_skb,
8085 const struct bpf_verifier_ops tc_cls_act_verifier_ops = {
8086 .get_func_proto = tc_cls_act_func_proto,
8087 .is_valid_access = tc_cls_act_is_valid_access,
8088 .convert_ctx_access = tc_cls_act_convert_ctx_access,
8089 .gen_prologue = tc_cls_act_prologue,
8090 .gen_ld_abs = bpf_gen_ld_abs,
8093 const struct bpf_prog_ops tc_cls_act_prog_ops = {
8094 .test_run = bpf_prog_test_run_skb,
8097 const struct bpf_verifier_ops xdp_verifier_ops = {
8098 .get_func_proto = xdp_func_proto,
8099 .is_valid_access = xdp_is_valid_access,
8100 .convert_ctx_access = xdp_convert_ctx_access,
8101 .gen_prologue = bpf_noop_prologue,
8104 const struct bpf_prog_ops xdp_prog_ops = {
8105 .test_run = bpf_prog_test_run_xdp,
8108 const struct bpf_verifier_ops cg_skb_verifier_ops = {
8109 .get_func_proto = cg_skb_func_proto,
8110 .is_valid_access = cg_skb_is_valid_access,
8111 .convert_ctx_access = bpf_convert_ctx_access,
8114 const struct bpf_prog_ops cg_skb_prog_ops = {
8115 .test_run = bpf_prog_test_run_skb,
8118 const struct bpf_verifier_ops lwt_in_verifier_ops = {
8119 .get_func_proto = lwt_in_func_proto,
8120 .is_valid_access = lwt_is_valid_access,
8121 .convert_ctx_access = bpf_convert_ctx_access,
8124 const struct bpf_prog_ops lwt_in_prog_ops = {
8125 .test_run = bpf_prog_test_run_skb,
8128 const struct bpf_verifier_ops lwt_out_verifier_ops = {
8129 .get_func_proto = lwt_out_func_proto,
8130 .is_valid_access = lwt_is_valid_access,
8131 .convert_ctx_access = bpf_convert_ctx_access,
8134 const struct bpf_prog_ops lwt_out_prog_ops = {
8135 .test_run = bpf_prog_test_run_skb,
8138 const struct bpf_verifier_ops lwt_xmit_verifier_ops = {
8139 .get_func_proto = lwt_xmit_func_proto,
8140 .is_valid_access = lwt_is_valid_access,
8141 .convert_ctx_access = bpf_convert_ctx_access,
8142 .gen_prologue = tc_cls_act_prologue,
8145 const struct bpf_prog_ops lwt_xmit_prog_ops = {
8146 .test_run = bpf_prog_test_run_skb,
8149 const struct bpf_verifier_ops lwt_seg6local_verifier_ops = {
8150 .get_func_proto = lwt_seg6local_func_proto,
8151 .is_valid_access = lwt_is_valid_access,
8152 .convert_ctx_access = bpf_convert_ctx_access,
8155 const struct bpf_prog_ops lwt_seg6local_prog_ops = {
8156 .test_run = bpf_prog_test_run_skb,
8159 const struct bpf_verifier_ops cg_sock_verifier_ops = {
8160 .get_func_proto = sock_filter_func_proto,
8161 .is_valid_access = sock_filter_is_valid_access,
8162 .convert_ctx_access = bpf_sock_convert_ctx_access,
8165 const struct bpf_prog_ops cg_sock_prog_ops = {
8168 const struct bpf_verifier_ops cg_sock_addr_verifier_ops = {
8169 .get_func_proto = sock_addr_func_proto,
8170 .is_valid_access = sock_addr_is_valid_access,
8171 .convert_ctx_access = sock_addr_convert_ctx_access,
8174 const struct bpf_prog_ops cg_sock_addr_prog_ops = {
8177 const struct bpf_verifier_ops sock_ops_verifier_ops = {
8178 .get_func_proto = sock_ops_func_proto,
8179 .is_valid_access = sock_ops_is_valid_access,
8180 .convert_ctx_access = sock_ops_convert_ctx_access,
8183 const struct bpf_prog_ops sock_ops_prog_ops = {
8186 const struct bpf_verifier_ops sk_skb_verifier_ops = {
8187 .get_func_proto = sk_skb_func_proto,
8188 .is_valid_access = sk_skb_is_valid_access,
8189 .convert_ctx_access = sk_skb_convert_ctx_access,
8190 .gen_prologue = sk_skb_prologue,
8193 const struct bpf_prog_ops sk_skb_prog_ops = {
8196 const struct bpf_verifier_ops sk_msg_verifier_ops = {
8197 .get_func_proto = sk_msg_func_proto,
8198 .is_valid_access = sk_msg_is_valid_access,
8199 .convert_ctx_access = sk_msg_convert_ctx_access,
8200 .gen_prologue = bpf_noop_prologue,
8203 const struct bpf_prog_ops sk_msg_prog_ops = {
8206 const struct bpf_verifier_ops flow_dissector_verifier_ops = {
8207 .get_func_proto = flow_dissector_func_proto,
8208 .is_valid_access = flow_dissector_is_valid_access,
8209 .convert_ctx_access = bpf_convert_ctx_access,
8212 const struct bpf_prog_ops flow_dissector_prog_ops = {
8213 .test_run = bpf_prog_test_run_flow_dissector,
8216 int sk_detach_filter(struct sock *sk)
8219 struct sk_filter *filter;
8221 if (sock_flag(sk, SOCK_FILTER_LOCKED))
8224 filter = rcu_dereference_protected(sk->sk_filter,
8225 lockdep_sock_is_held(sk));
8227 RCU_INIT_POINTER(sk->sk_filter, NULL);
8228 sk_filter_uncharge(sk, filter);
8234 EXPORT_SYMBOL_GPL(sk_detach_filter);
8236 int sk_get_filter(struct sock *sk, struct sock_filter __user *ubuf,
8239 struct sock_fprog_kern *fprog;
8240 struct sk_filter *filter;
8244 filter = rcu_dereference_protected(sk->sk_filter,
8245 lockdep_sock_is_held(sk));
8249 /* We're copying the filter that has been originally attached,
8250 * so no conversion/decode needed anymore. eBPF programs that
8251 * have no original program cannot be dumped through this.
8254 fprog = filter->prog->orig_prog;
8260 /* User space only enquires number of filter blocks. */
8264 if (len < fprog->len)
8268 if (copy_to_user(ubuf, fprog->filter, bpf_classic_proglen(fprog)))
8271 /* Instead of bytes, the API requests to return the number
8281 struct sk_reuseport_kern {
8282 struct sk_buff *skb;
8284 struct sock *selected_sk;
8291 static void bpf_init_reuseport_kern(struct sk_reuseport_kern *reuse_kern,
8292 struct sock_reuseport *reuse,
8293 struct sock *sk, struct sk_buff *skb,
8296 reuse_kern->skb = skb;
8297 reuse_kern->sk = sk;
8298 reuse_kern->selected_sk = NULL;
8299 reuse_kern->data_end = skb->data + skb_headlen(skb);
8300 reuse_kern->hash = hash;
8301 reuse_kern->reuseport_id = reuse->reuseport_id;
8302 reuse_kern->bind_inany = reuse->bind_inany;
8305 struct sock *bpf_run_sk_reuseport(struct sock_reuseport *reuse, struct sock *sk,
8306 struct bpf_prog *prog, struct sk_buff *skb,
8309 struct sk_reuseport_kern reuse_kern;
8310 enum sk_action action;
8312 bpf_init_reuseport_kern(&reuse_kern, reuse, sk, skb, hash);
8313 action = BPF_PROG_RUN(prog, &reuse_kern);
8315 if (action == SK_PASS)
8316 return reuse_kern.selected_sk;
8318 return ERR_PTR(-ECONNREFUSED);
8321 BPF_CALL_4(sk_select_reuseport, struct sk_reuseport_kern *, reuse_kern,
8322 struct bpf_map *, map, void *, key, u32, flags)
8324 struct sock_reuseport *reuse;
8325 struct sock *selected_sk;
8327 selected_sk = map->ops->map_lookup_elem(map, key);
8331 reuse = rcu_dereference(selected_sk->sk_reuseport_cb);
8333 /* selected_sk is unhashed (e.g. by close()) after the
8334 * above map_lookup_elem(). Treat selected_sk has already
8335 * been removed from the map.
8339 if (unlikely(reuse->reuseport_id != reuse_kern->reuseport_id)) {
8342 if (unlikely(!reuse_kern->reuseport_id))
8343 /* There is a small race between adding the
8344 * sk to the map and setting the
8345 * reuse_kern->reuseport_id.
8346 * Treat it as the sk has not been added to
8351 sk = reuse_kern->sk;
8352 if (sk->sk_protocol != selected_sk->sk_protocol)
8354 else if (sk->sk_family != selected_sk->sk_family)
8355 return -EAFNOSUPPORT;
8357 /* Catch all. Likely bound to a different sockaddr. */
8361 reuse_kern->selected_sk = selected_sk;
8366 static const struct bpf_func_proto sk_select_reuseport_proto = {
8367 .func = sk_select_reuseport,
8369 .ret_type = RET_INTEGER,
8370 .arg1_type = ARG_PTR_TO_CTX,
8371 .arg2_type = ARG_CONST_MAP_PTR,
8372 .arg3_type = ARG_PTR_TO_MAP_KEY,
8373 .arg4_type = ARG_ANYTHING,
8376 BPF_CALL_4(sk_reuseport_load_bytes,
8377 const struct sk_reuseport_kern *, reuse_kern, u32, offset,
8378 void *, to, u32, len)
8380 return ____bpf_skb_load_bytes(reuse_kern->skb, offset, to, len);
8383 static const struct bpf_func_proto sk_reuseport_load_bytes_proto = {
8384 .func = sk_reuseport_load_bytes,
8386 .ret_type = RET_INTEGER,
8387 .arg1_type = ARG_PTR_TO_CTX,
8388 .arg2_type = ARG_ANYTHING,
8389 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
8390 .arg4_type = ARG_CONST_SIZE,
8393 BPF_CALL_5(sk_reuseport_load_bytes_relative,
8394 const struct sk_reuseport_kern *, reuse_kern, u32, offset,
8395 void *, to, u32, len, u32, start_header)
8397 return ____bpf_skb_load_bytes_relative(reuse_kern->skb, offset, to,
8401 static const struct bpf_func_proto sk_reuseport_load_bytes_relative_proto = {
8402 .func = sk_reuseport_load_bytes_relative,
8404 .ret_type = RET_INTEGER,
8405 .arg1_type = ARG_PTR_TO_CTX,
8406 .arg2_type = ARG_ANYTHING,
8407 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
8408 .arg4_type = ARG_CONST_SIZE,
8409 .arg5_type = ARG_ANYTHING,
8412 static const struct bpf_func_proto *
8413 sk_reuseport_func_proto(enum bpf_func_id func_id,
8414 const struct bpf_prog *prog)
8417 case BPF_FUNC_sk_select_reuseport:
8418 return &sk_select_reuseport_proto;
8419 case BPF_FUNC_skb_load_bytes:
8420 return &sk_reuseport_load_bytes_proto;
8421 case BPF_FUNC_skb_load_bytes_relative:
8422 return &sk_reuseport_load_bytes_relative_proto;
8424 return bpf_base_func_proto(func_id);
8429 sk_reuseport_is_valid_access(int off, int size,
8430 enum bpf_access_type type,
8431 const struct bpf_prog *prog,
8432 struct bpf_insn_access_aux *info)
8434 const u32 size_default = sizeof(__u32);
8436 if (off < 0 || off >= sizeof(struct sk_reuseport_md) ||
8437 off % size || type != BPF_READ)
8441 case offsetof(struct sk_reuseport_md, data):
8442 info->reg_type = PTR_TO_PACKET;
8443 return size == sizeof(__u64);
8445 case offsetof(struct sk_reuseport_md, data_end):
8446 info->reg_type = PTR_TO_PACKET_END;
8447 return size == sizeof(__u64);
8449 case offsetof(struct sk_reuseport_md, hash):
8450 return size == size_default;
8452 /* Fields that allow narrowing */
8453 case offsetof(struct sk_reuseport_md, eth_protocol):
8454 if (size < FIELD_SIZEOF(struct sk_buff, protocol))
8457 case offsetof(struct sk_reuseport_md, ip_protocol):
8458 case offsetof(struct sk_reuseport_md, bind_inany):
8459 case offsetof(struct sk_reuseport_md, len):
8460 bpf_ctx_record_field_size(info, size_default);
8461 return bpf_ctx_narrow_access_ok(off, size, size_default);
8468 #define SK_REUSEPORT_LOAD_FIELD(F) ({ \
8469 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_reuseport_kern, F), \
8470 si->dst_reg, si->src_reg, \
8471 bpf_target_off(struct sk_reuseport_kern, F, \
8472 FIELD_SIZEOF(struct sk_reuseport_kern, F), \
8476 #define SK_REUSEPORT_LOAD_SKB_FIELD(SKB_FIELD) \
8477 SOCK_ADDR_LOAD_NESTED_FIELD(struct sk_reuseport_kern, \
8482 #define SK_REUSEPORT_LOAD_SK_FIELD_SIZE_OFF(SK_FIELD, BPF_SIZE, EXTRA_OFF) \
8483 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(struct sk_reuseport_kern, \
8486 SK_FIELD, BPF_SIZE, EXTRA_OFF)
8488 static u32 sk_reuseport_convert_ctx_access(enum bpf_access_type type,
8489 const struct bpf_insn *si,
8490 struct bpf_insn *insn_buf,
8491 struct bpf_prog *prog,
8494 struct bpf_insn *insn = insn_buf;
8497 case offsetof(struct sk_reuseport_md, data):
8498 SK_REUSEPORT_LOAD_SKB_FIELD(data);
8501 case offsetof(struct sk_reuseport_md, len):
8502 SK_REUSEPORT_LOAD_SKB_FIELD(len);
8505 case offsetof(struct sk_reuseport_md, eth_protocol):
8506 SK_REUSEPORT_LOAD_SKB_FIELD(protocol);
8509 case offsetof(struct sk_reuseport_md, ip_protocol):
8510 BUILD_BUG_ON(HWEIGHT32(SK_FL_PROTO_MASK) != BITS_PER_BYTE);
8511 SK_REUSEPORT_LOAD_SK_FIELD_SIZE_OFF(__sk_flags_offset,
8513 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_PROTO_MASK);
8514 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg,
8516 /* SK_FL_PROTO_MASK and SK_FL_PROTO_SHIFT are endian
8517 * aware. No further narrowing or masking is needed.
8522 case offsetof(struct sk_reuseport_md, data_end):
8523 SK_REUSEPORT_LOAD_FIELD(data_end);
8526 case offsetof(struct sk_reuseport_md, hash):
8527 SK_REUSEPORT_LOAD_FIELD(hash);
8530 case offsetof(struct sk_reuseport_md, bind_inany):
8531 SK_REUSEPORT_LOAD_FIELD(bind_inany);
8535 return insn - insn_buf;
8538 const struct bpf_verifier_ops sk_reuseport_verifier_ops = {
8539 .get_func_proto = sk_reuseport_func_proto,
8540 .is_valid_access = sk_reuseport_is_valid_access,
8541 .convert_ctx_access = sk_reuseport_convert_ctx_access,
8544 const struct bpf_prog_ops sk_reuseport_prog_ops = {
8546 #endif /* CONFIG_INET */