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 <linux/nospec.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 case SKF_AD_VLAN_TAG_PRESENT:
301 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_tci) != 2);
302 BUILD_BUG_ON(VLAN_TAG_PRESENT != 0x1000);
304 /* dst_reg = *(u16 *) (src_reg + offsetof(vlan_tci)) */
305 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
306 offsetof(struct sk_buff, vlan_tci));
307 if (skb_field == SKF_AD_VLAN_TAG) {
308 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg,
312 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 12);
314 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, 1);
319 return insn - insn_buf;
322 static bool convert_bpf_extensions(struct sock_filter *fp,
323 struct bpf_insn **insnp)
325 struct bpf_insn *insn = *insnp;
329 case SKF_AD_OFF + SKF_AD_PROTOCOL:
330 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2);
332 /* A = *(u16 *) (CTX + offsetof(protocol)) */
333 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
334 offsetof(struct sk_buff, protocol));
335 /* A = ntohs(A) [emitting a nop or swap16] */
336 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
339 case SKF_AD_OFF + SKF_AD_PKTTYPE:
340 cnt = convert_skb_access(SKF_AD_PKTTYPE, BPF_REG_A, BPF_REG_CTX, insn);
344 case SKF_AD_OFF + SKF_AD_IFINDEX:
345 case SKF_AD_OFF + SKF_AD_HATYPE:
346 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4);
347 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, type) != 2);
349 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
350 BPF_REG_TMP, BPF_REG_CTX,
351 offsetof(struct sk_buff, dev));
352 /* if (tmp != 0) goto pc + 1 */
353 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_TMP, 0, 1);
354 *insn++ = BPF_EXIT_INSN();
355 if (fp->k == SKF_AD_OFF + SKF_AD_IFINDEX)
356 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_TMP,
357 offsetof(struct net_device, ifindex));
359 *insn = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_TMP,
360 offsetof(struct net_device, type));
363 case SKF_AD_OFF + SKF_AD_MARK:
364 cnt = convert_skb_access(SKF_AD_MARK, BPF_REG_A, BPF_REG_CTX, insn);
368 case SKF_AD_OFF + SKF_AD_RXHASH:
369 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4);
371 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX,
372 offsetof(struct sk_buff, hash));
375 case SKF_AD_OFF + SKF_AD_QUEUE:
376 cnt = convert_skb_access(SKF_AD_QUEUE, BPF_REG_A, BPF_REG_CTX, insn);
380 case SKF_AD_OFF + SKF_AD_VLAN_TAG:
381 cnt = convert_skb_access(SKF_AD_VLAN_TAG,
382 BPF_REG_A, BPF_REG_CTX, insn);
386 case SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT:
387 cnt = convert_skb_access(SKF_AD_VLAN_TAG_PRESENT,
388 BPF_REG_A, BPF_REG_CTX, insn);
392 case SKF_AD_OFF + SKF_AD_VLAN_TPID:
393 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_proto) != 2);
395 /* A = *(u16 *) (CTX + offsetof(vlan_proto)) */
396 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
397 offsetof(struct sk_buff, vlan_proto));
398 /* A = ntohs(A) [emitting a nop or swap16] */
399 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
402 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
403 case SKF_AD_OFF + SKF_AD_NLATTR:
404 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
405 case SKF_AD_OFF + SKF_AD_CPU:
406 case SKF_AD_OFF + SKF_AD_RANDOM:
408 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX);
410 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_A);
412 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_X);
413 /* Emit call(arg1=CTX, arg2=A, arg3=X) */
415 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
416 *insn = BPF_EMIT_CALL(bpf_skb_get_pay_offset);
418 case SKF_AD_OFF + SKF_AD_NLATTR:
419 *insn = BPF_EMIT_CALL(bpf_skb_get_nlattr);
421 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
422 *insn = BPF_EMIT_CALL(bpf_skb_get_nlattr_nest);
424 case SKF_AD_OFF + SKF_AD_CPU:
425 *insn = BPF_EMIT_CALL(bpf_get_raw_cpu_id);
427 case SKF_AD_OFF + SKF_AD_RANDOM:
428 *insn = BPF_EMIT_CALL(bpf_user_rnd_u32);
429 bpf_user_rnd_init_once();
434 case SKF_AD_OFF + SKF_AD_ALU_XOR_X:
436 *insn = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_X);
440 /* This is just a dummy call to avoid letting the compiler
441 * evict __bpf_call_base() as an optimization. Placed here
442 * where no-one bothers.
444 BUG_ON(__bpf_call_base(0, 0, 0, 0, 0) != 0);
452 static bool convert_bpf_ld_abs(struct sock_filter *fp, struct bpf_insn **insnp)
454 const bool unaligned_ok = IS_BUILTIN(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS);
455 int size = bpf_size_to_bytes(BPF_SIZE(fp->code));
456 bool endian = BPF_SIZE(fp->code) == BPF_H ||
457 BPF_SIZE(fp->code) == BPF_W;
458 bool indirect = BPF_MODE(fp->code) == BPF_IND;
459 const int ip_align = NET_IP_ALIGN;
460 struct bpf_insn *insn = *insnp;
464 ((unaligned_ok && offset >= 0) ||
465 (!unaligned_ok && offset >= 0 &&
466 offset + ip_align >= 0 &&
467 offset + ip_align % size == 0))) {
468 bool ldx_off_ok = offset <= S16_MAX;
470 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_H);
471 *insn++ = BPF_ALU64_IMM(BPF_SUB, BPF_REG_TMP, offset);
472 *insn++ = BPF_JMP_IMM(BPF_JSLT, BPF_REG_TMP,
473 size, 2 + endian + (!ldx_off_ok * 2));
475 *insn++ = BPF_LDX_MEM(BPF_SIZE(fp->code), BPF_REG_A,
478 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_D);
479 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_TMP, offset);
480 *insn++ = BPF_LDX_MEM(BPF_SIZE(fp->code), BPF_REG_A,
484 *insn++ = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, size * 8);
485 *insn++ = BPF_JMP_A(8);
488 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX);
489 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_D);
490 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_H);
492 *insn++ = BPF_MOV64_IMM(BPF_REG_ARG4, offset);
494 *insn++ = BPF_MOV64_REG(BPF_REG_ARG4, BPF_REG_X);
496 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_ARG4, offset);
499 switch (BPF_SIZE(fp->code)) {
501 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_8);
504 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_16);
507 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_32);
513 *insn++ = BPF_JMP_IMM(BPF_JSGE, BPF_REG_A, 0, 2);
514 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
515 *insn = BPF_EXIT_INSN();
522 * bpf_convert_filter - convert filter program
523 * @prog: the user passed filter program
524 * @len: the length of the user passed filter program
525 * @new_prog: allocated 'struct bpf_prog' or NULL
526 * @new_len: pointer to store length of converted program
527 * @seen_ld_abs: bool whether we've seen ld_abs/ind
529 * Remap 'sock_filter' style classic BPF (cBPF) instruction set to 'bpf_insn'
530 * style extended BPF (eBPF).
531 * Conversion workflow:
533 * 1) First pass for calculating the new program length:
534 * bpf_convert_filter(old_prog, old_len, NULL, &new_len, &seen_ld_abs)
536 * 2) 2nd pass to remap in two passes: 1st pass finds new
537 * jump offsets, 2nd pass remapping:
538 * bpf_convert_filter(old_prog, old_len, new_prog, &new_len, &seen_ld_abs)
540 static int bpf_convert_filter(struct sock_filter *prog, int len,
541 struct bpf_prog *new_prog, int *new_len,
544 int new_flen = 0, pass = 0, target, i, stack_off;
545 struct bpf_insn *new_insn, *first_insn = NULL;
546 struct sock_filter *fp;
550 BUILD_BUG_ON(BPF_MEMWORDS * sizeof(u32) > MAX_BPF_STACK);
551 BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG);
553 if (len <= 0 || len > BPF_MAXINSNS)
557 first_insn = new_prog->insnsi;
558 addrs = kcalloc(len, sizeof(*addrs),
559 GFP_KERNEL | __GFP_NOWARN);
565 new_insn = first_insn;
568 /* Classic BPF related prologue emission. */
570 /* Classic BPF expects A and X to be reset first. These need
571 * to be guaranteed to be the first two instructions.
573 *new_insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
574 *new_insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_X, BPF_REG_X);
576 /* All programs must keep CTX in callee saved BPF_REG_CTX.
577 * In eBPF case it's done by the compiler, here we need to
578 * do this ourself. Initial CTX is present in BPF_REG_ARG1.
580 *new_insn++ = BPF_MOV64_REG(BPF_REG_CTX, BPF_REG_ARG1);
582 /* For packet access in classic BPF, cache skb->data
583 * in callee-saved BPF R8 and skb->len - skb->data_len
584 * (headlen) in BPF R9. Since classic BPF is read-only
585 * on CTX, we only need to cache it once.
587 *new_insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
588 BPF_REG_D, BPF_REG_CTX,
589 offsetof(struct sk_buff, data));
590 *new_insn++ = BPF_LDX_MEM(BPF_W, BPF_REG_H, BPF_REG_CTX,
591 offsetof(struct sk_buff, len));
592 *new_insn++ = BPF_LDX_MEM(BPF_W, BPF_REG_TMP, BPF_REG_CTX,
593 offsetof(struct sk_buff, data_len));
594 *new_insn++ = BPF_ALU32_REG(BPF_SUB, BPF_REG_H, BPF_REG_TMP);
600 for (i = 0; i < len; fp++, i++) {
601 struct bpf_insn tmp_insns[32] = { };
602 struct bpf_insn *insn = tmp_insns;
605 addrs[i] = new_insn - first_insn;
608 /* All arithmetic insns and skb loads map as-is. */
609 case BPF_ALU | BPF_ADD | BPF_X:
610 case BPF_ALU | BPF_ADD | BPF_K:
611 case BPF_ALU | BPF_SUB | BPF_X:
612 case BPF_ALU | BPF_SUB | BPF_K:
613 case BPF_ALU | BPF_AND | BPF_X:
614 case BPF_ALU | BPF_AND | BPF_K:
615 case BPF_ALU | BPF_OR | BPF_X:
616 case BPF_ALU | BPF_OR | BPF_K:
617 case BPF_ALU | BPF_LSH | BPF_X:
618 case BPF_ALU | BPF_LSH | BPF_K:
619 case BPF_ALU | BPF_RSH | BPF_X:
620 case BPF_ALU | BPF_RSH | BPF_K:
621 case BPF_ALU | BPF_XOR | BPF_X:
622 case BPF_ALU | BPF_XOR | BPF_K:
623 case BPF_ALU | BPF_MUL | BPF_X:
624 case BPF_ALU | BPF_MUL | BPF_K:
625 case BPF_ALU | BPF_DIV | BPF_X:
626 case BPF_ALU | BPF_DIV | BPF_K:
627 case BPF_ALU | BPF_MOD | BPF_X:
628 case BPF_ALU | BPF_MOD | BPF_K:
629 case BPF_ALU | BPF_NEG:
630 case BPF_LD | BPF_ABS | BPF_W:
631 case BPF_LD | BPF_ABS | BPF_H:
632 case BPF_LD | BPF_ABS | BPF_B:
633 case BPF_LD | BPF_IND | BPF_W:
634 case BPF_LD | BPF_IND | BPF_H:
635 case BPF_LD | BPF_IND | BPF_B:
636 /* Check for overloaded BPF extension and
637 * directly convert it if found, otherwise
638 * just move on with mapping.
640 if (BPF_CLASS(fp->code) == BPF_LD &&
641 BPF_MODE(fp->code) == BPF_ABS &&
642 convert_bpf_extensions(fp, &insn))
644 if (BPF_CLASS(fp->code) == BPF_LD &&
645 convert_bpf_ld_abs(fp, &insn)) {
650 if (fp->code == (BPF_ALU | BPF_DIV | BPF_X) ||
651 fp->code == (BPF_ALU | BPF_MOD | BPF_X)) {
652 *insn++ = BPF_MOV32_REG(BPF_REG_X, BPF_REG_X);
653 /* Error with exception code on div/mod by 0.
654 * For cBPF programs, this was always return 0.
656 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_X, 0, 2);
657 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
658 *insn++ = BPF_EXIT_INSN();
661 *insn = BPF_RAW_INSN(fp->code, BPF_REG_A, BPF_REG_X, 0, fp->k);
664 /* Jump transformation cannot use BPF block macros
665 * everywhere as offset calculation and target updates
666 * require a bit more work than the rest, i.e. jump
667 * opcodes map as-is, but offsets need adjustment.
670 #define BPF_EMIT_JMP \
672 const s32 off_min = S16_MIN, off_max = S16_MAX; \
675 if (target >= len || target < 0) \
677 off = addrs ? addrs[target] - addrs[i] - 1 : 0; \
678 /* Adjust pc relative offset for 2nd or 3rd insn. */ \
679 off -= insn - tmp_insns; \
680 /* Reject anything not fitting into insn->off. */ \
681 if (off < off_min || off > off_max) \
686 case BPF_JMP | BPF_JA:
687 target = i + fp->k + 1;
688 insn->code = fp->code;
692 case BPF_JMP | BPF_JEQ | BPF_K:
693 case BPF_JMP | BPF_JEQ | BPF_X:
694 case BPF_JMP | BPF_JSET | BPF_K:
695 case BPF_JMP | BPF_JSET | BPF_X:
696 case BPF_JMP | BPF_JGT | BPF_K:
697 case BPF_JMP | BPF_JGT | BPF_X:
698 case BPF_JMP | BPF_JGE | BPF_K:
699 case BPF_JMP | BPF_JGE | BPF_X:
700 if (BPF_SRC(fp->code) == BPF_K && (int) fp->k < 0) {
701 /* BPF immediates are signed, zero extend
702 * immediate into tmp register and use it
705 *insn++ = BPF_MOV32_IMM(BPF_REG_TMP, fp->k);
707 insn->dst_reg = BPF_REG_A;
708 insn->src_reg = BPF_REG_TMP;
711 insn->dst_reg = BPF_REG_A;
713 bpf_src = BPF_SRC(fp->code);
714 insn->src_reg = bpf_src == BPF_X ? BPF_REG_X : 0;
717 /* Common case where 'jump_false' is next insn. */
719 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
720 target = i + fp->jt + 1;
725 /* Convert some jumps when 'jump_true' is next insn. */
727 switch (BPF_OP(fp->code)) {
729 insn->code = BPF_JMP | BPF_JNE | bpf_src;
732 insn->code = BPF_JMP | BPF_JLE | bpf_src;
735 insn->code = BPF_JMP | BPF_JLT | bpf_src;
741 target = i + fp->jf + 1;
746 /* Other jumps are mapped into two insns: Jxx and JA. */
747 target = i + fp->jt + 1;
748 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
752 insn->code = BPF_JMP | BPF_JA;
753 target = i + fp->jf + 1;
757 /* ldxb 4 * ([14] & 0xf) is remaped into 6 insns. */
758 case BPF_LDX | BPF_MSH | BPF_B: {
759 struct sock_filter tmp = {
760 .code = BPF_LD | BPF_ABS | BPF_B,
767 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
768 /* A = BPF_R0 = *(u8 *) (skb->data + K) */
769 convert_bpf_ld_abs(&tmp, &insn);
772 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, 0xf);
774 *insn++ = BPF_ALU32_IMM(BPF_LSH, BPF_REG_A, 2);
776 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_X);
778 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
780 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_TMP);
783 /* RET_K is remaped into 2 insns. RET_A case doesn't need an
784 * extra mov as BPF_REG_0 is already mapped into BPF_REG_A.
786 case BPF_RET | BPF_A:
787 case BPF_RET | BPF_K:
788 if (BPF_RVAL(fp->code) == BPF_K)
789 *insn++ = BPF_MOV32_RAW(BPF_K, BPF_REG_0,
791 *insn = BPF_EXIT_INSN();
794 /* Store to stack. */
797 stack_off = fp->k * 4 + 4;
798 *insn = BPF_STX_MEM(BPF_W, BPF_REG_FP, BPF_CLASS(fp->code) ==
799 BPF_ST ? BPF_REG_A : BPF_REG_X,
801 /* check_load_and_stores() verifies that classic BPF can
802 * load from stack only after write, so tracking
803 * stack_depth for ST|STX insns is enough
805 if (new_prog && new_prog->aux->stack_depth < stack_off)
806 new_prog->aux->stack_depth = stack_off;
809 /* Load from stack. */
810 case BPF_LD | BPF_MEM:
811 case BPF_LDX | BPF_MEM:
812 stack_off = fp->k * 4 + 4;
813 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
814 BPF_REG_A : BPF_REG_X, BPF_REG_FP,
819 case BPF_LD | BPF_IMM:
820 case BPF_LDX | BPF_IMM:
821 *insn = BPF_MOV32_IMM(BPF_CLASS(fp->code) == BPF_LD ?
822 BPF_REG_A : BPF_REG_X, fp->k);
826 case BPF_MISC | BPF_TAX:
827 *insn = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
831 case BPF_MISC | BPF_TXA:
832 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_X);
835 /* A = skb->len or X = skb->len */
836 case BPF_LD | BPF_W | BPF_LEN:
837 case BPF_LDX | BPF_W | BPF_LEN:
838 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
839 BPF_REG_A : BPF_REG_X, BPF_REG_CTX,
840 offsetof(struct sk_buff, len));
843 /* Access seccomp_data fields. */
844 case BPF_LDX | BPF_ABS | BPF_W:
845 /* A = *(u32 *) (ctx + K) */
846 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX, fp->k);
849 /* Unknown instruction. */
856 memcpy(new_insn, tmp_insns,
857 sizeof(*insn) * (insn - tmp_insns));
858 new_insn += insn - tmp_insns;
862 /* Only calculating new length. */
863 *new_len = new_insn - first_insn;
865 *new_len += 4; /* Prologue bits. */
870 if (new_flen != new_insn - first_insn) {
871 new_flen = new_insn - first_insn;
878 BUG_ON(*new_len != new_flen);
887 * As we dont want to clear mem[] array for each packet going through
888 * __bpf_prog_run(), we check that filter loaded by user never try to read
889 * a cell if not previously written, and we check all branches to be sure
890 * a malicious user doesn't try to abuse us.
892 static int check_load_and_stores(const struct sock_filter *filter, int flen)
894 u16 *masks, memvalid = 0; /* One bit per cell, 16 cells */
897 BUILD_BUG_ON(BPF_MEMWORDS > 16);
899 masks = kmalloc_array(flen, sizeof(*masks), GFP_KERNEL);
903 memset(masks, 0xff, flen * sizeof(*masks));
905 for (pc = 0; pc < flen; pc++) {
906 memvalid &= masks[pc];
908 switch (filter[pc].code) {
911 memvalid |= (1 << filter[pc].k);
913 case BPF_LD | BPF_MEM:
914 case BPF_LDX | BPF_MEM:
915 if (!(memvalid & (1 << filter[pc].k))) {
920 case BPF_JMP | BPF_JA:
921 /* A jump must set masks on target */
922 masks[pc + 1 + filter[pc].k] &= memvalid;
925 case BPF_JMP | BPF_JEQ | BPF_K:
926 case BPF_JMP | BPF_JEQ | BPF_X:
927 case BPF_JMP | BPF_JGE | BPF_K:
928 case BPF_JMP | BPF_JGE | BPF_X:
929 case BPF_JMP | BPF_JGT | BPF_K:
930 case BPF_JMP | BPF_JGT | BPF_X:
931 case BPF_JMP | BPF_JSET | BPF_K:
932 case BPF_JMP | BPF_JSET | BPF_X:
933 /* A jump must set masks on targets */
934 masks[pc + 1 + filter[pc].jt] &= memvalid;
935 masks[pc + 1 + filter[pc].jf] &= memvalid;
945 static bool chk_code_allowed(u16 code_to_probe)
947 static const bool codes[] = {
948 /* 32 bit ALU operations */
949 [BPF_ALU | BPF_ADD | BPF_K] = true,
950 [BPF_ALU | BPF_ADD | BPF_X] = true,
951 [BPF_ALU | BPF_SUB | BPF_K] = true,
952 [BPF_ALU | BPF_SUB | BPF_X] = true,
953 [BPF_ALU | BPF_MUL | BPF_K] = true,
954 [BPF_ALU | BPF_MUL | BPF_X] = true,
955 [BPF_ALU | BPF_DIV | BPF_K] = true,
956 [BPF_ALU | BPF_DIV | BPF_X] = true,
957 [BPF_ALU | BPF_MOD | BPF_K] = true,
958 [BPF_ALU | BPF_MOD | BPF_X] = true,
959 [BPF_ALU | BPF_AND | BPF_K] = true,
960 [BPF_ALU | BPF_AND | BPF_X] = true,
961 [BPF_ALU | BPF_OR | BPF_K] = true,
962 [BPF_ALU | BPF_OR | BPF_X] = true,
963 [BPF_ALU | BPF_XOR | BPF_K] = true,
964 [BPF_ALU | BPF_XOR | BPF_X] = true,
965 [BPF_ALU | BPF_LSH | BPF_K] = true,
966 [BPF_ALU | BPF_LSH | BPF_X] = true,
967 [BPF_ALU | BPF_RSH | BPF_K] = true,
968 [BPF_ALU | BPF_RSH | BPF_X] = true,
969 [BPF_ALU | BPF_NEG] = true,
970 /* Load instructions */
971 [BPF_LD | BPF_W | BPF_ABS] = true,
972 [BPF_LD | BPF_H | BPF_ABS] = true,
973 [BPF_LD | BPF_B | BPF_ABS] = true,
974 [BPF_LD | BPF_W | BPF_LEN] = true,
975 [BPF_LD | BPF_W | BPF_IND] = true,
976 [BPF_LD | BPF_H | BPF_IND] = true,
977 [BPF_LD | BPF_B | BPF_IND] = true,
978 [BPF_LD | BPF_IMM] = true,
979 [BPF_LD | BPF_MEM] = true,
980 [BPF_LDX | BPF_W | BPF_LEN] = true,
981 [BPF_LDX | BPF_B | BPF_MSH] = true,
982 [BPF_LDX | BPF_IMM] = true,
983 [BPF_LDX | BPF_MEM] = true,
984 /* Store instructions */
987 /* Misc instructions */
988 [BPF_MISC | BPF_TAX] = true,
989 [BPF_MISC | BPF_TXA] = true,
990 /* Return instructions */
991 [BPF_RET | BPF_K] = true,
992 [BPF_RET | BPF_A] = true,
993 /* Jump instructions */
994 [BPF_JMP | BPF_JA] = true,
995 [BPF_JMP | BPF_JEQ | BPF_K] = true,
996 [BPF_JMP | BPF_JEQ | BPF_X] = true,
997 [BPF_JMP | BPF_JGE | BPF_K] = true,
998 [BPF_JMP | BPF_JGE | BPF_X] = true,
999 [BPF_JMP | BPF_JGT | BPF_K] = true,
1000 [BPF_JMP | BPF_JGT | BPF_X] = true,
1001 [BPF_JMP | BPF_JSET | BPF_K] = true,
1002 [BPF_JMP | BPF_JSET | BPF_X] = true,
1005 if (code_to_probe >= ARRAY_SIZE(codes))
1008 return codes[code_to_probe];
1011 static bool bpf_check_basics_ok(const struct sock_filter *filter,
1016 if (flen == 0 || flen > BPF_MAXINSNS)
1023 * bpf_check_classic - verify socket filter code
1024 * @filter: filter to verify
1025 * @flen: length of filter
1027 * Check the user's filter code. If we let some ugly
1028 * filter code slip through kaboom! The filter must contain
1029 * no references or jumps that are out of range, no illegal
1030 * instructions, and must end with a RET instruction.
1032 * All jumps are forward as they are not signed.
1034 * Returns 0 if the rule set is legal or -EINVAL if not.
1036 static int bpf_check_classic(const struct sock_filter *filter,
1042 flen = array_index_nospec(flen, BPF_MAXINSNS + 1);
1043 /* Check the filter code now */
1044 for (pc = 0; pc < flen; pc++) {
1045 const struct sock_filter *ftest = &filter[pc];
1047 /* May we actually operate on this code? */
1048 if (!chk_code_allowed(ftest->code))
1051 /* Some instructions need special checks */
1052 switch (ftest->code) {
1053 case BPF_ALU | BPF_DIV | BPF_K:
1054 case BPF_ALU | BPF_MOD | BPF_K:
1055 /* Check for division by zero */
1059 case BPF_ALU | BPF_LSH | BPF_K:
1060 case BPF_ALU | BPF_RSH | BPF_K:
1064 case BPF_LD | BPF_MEM:
1065 case BPF_LDX | BPF_MEM:
1068 /* Check for invalid memory addresses */
1069 if (ftest->k >= BPF_MEMWORDS)
1072 case BPF_JMP | BPF_JA:
1073 /* Note, the large ftest->k might cause loops.
1074 * Compare this with conditional jumps below,
1075 * where offsets are limited. --ANK (981016)
1077 if (ftest->k >= (unsigned int)(flen - pc - 1))
1080 case BPF_JMP | BPF_JEQ | BPF_K:
1081 case BPF_JMP | BPF_JEQ | BPF_X:
1082 case BPF_JMP | BPF_JGE | BPF_K:
1083 case BPF_JMP | BPF_JGE | BPF_X:
1084 case BPF_JMP | BPF_JGT | BPF_K:
1085 case BPF_JMP | BPF_JGT | BPF_X:
1086 case BPF_JMP | BPF_JSET | BPF_K:
1087 case BPF_JMP | BPF_JSET | BPF_X:
1088 /* Both conditionals must be safe */
1089 if (pc + ftest->jt + 1 >= flen ||
1090 pc + ftest->jf + 1 >= flen)
1093 case BPF_LD | BPF_W | BPF_ABS:
1094 case BPF_LD | BPF_H | BPF_ABS:
1095 case BPF_LD | BPF_B | BPF_ABS:
1097 if (bpf_anc_helper(ftest) & BPF_ANC)
1099 /* Ancillary operation unknown or unsupported */
1100 if (anc_found == false && ftest->k >= SKF_AD_OFF)
1105 /* Last instruction must be a RET code */
1106 switch (filter[flen - 1].code) {
1107 case BPF_RET | BPF_K:
1108 case BPF_RET | BPF_A:
1109 return check_load_and_stores(filter, flen);
1115 static int bpf_prog_store_orig_filter(struct bpf_prog *fp,
1116 const struct sock_fprog *fprog)
1118 unsigned int fsize = bpf_classic_proglen(fprog);
1119 struct sock_fprog_kern *fkprog;
1121 fp->orig_prog = kmalloc(sizeof(*fkprog), GFP_KERNEL);
1125 fkprog = fp->orig_prog;
1126 fkprog->len = fprog->len;
1128 fkprog->filter = kmemdup(fp->insns, fsize,
1129 GFP_KERNEL | __GFP_NOWARN);
1130 if (!fkprog->filter) {
1131 kfree(fp->orig_prog);
1138 static void bpf_release_orig_filter(struct bpf_prog *fp)
1140 struct sock_fprog_kern *fprog = fp->orig_prog;
1143 kfree(fprog->filter);
1148 static void __bpf_prog_release(struct bpf_prog *prog)
1150 if (prog->type == BPF_PROG_TYPE_SOCKET_FILTER) {
1153 bpf_release_orig_filter(prog);
1154 bpf_prog_free(prog);
1158 static void __sk_filter_release(struct sk_filter *fp)
1160 __bpf_prog_release(fp->prog);
1165 * sk_filter_release_rcu - Release a socket filter by rcu_head
1166 * @rcu: rcu_head that contains the sk_filter to free
1168 static void sk_filter_release_rcu(struct rcu_head *rcu)
1170 struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu);
1172 __sk_filter_release(fp);
1176 * sk_filter_release - release a socket filter
1177 * @fp: filter to remove
1179 * Remove a filter from a socket and release its resources.
1181 static void sk_filter_release(struct sk_filter *fp)
1183 if (refcount_dec_and_test(&fp->refcnt))
1184 call_rcu(&fp->rcu, sk_filter_release_rcu);
1187 void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp)
1189 u32 filter_size = bpf_prog_size(fp->prog->len);
1191 atomic_sub(filter_size, &sk->sk_omem_alloc);
1192 sk_filter_release(fp);
1195 /* try to charge the socket memory if there is space available
1196 * return true on success
1198 static bool __sk_filter_charge(struct sock *sk, struct sk_filter *fp)
1200 u32 filter_size = bpf_prog_size(fp->prog->len);
1202 /* same check as in sock_kmalloc() */
1203 if (filter_size <= sysctl_optmem_max &&
1204 atomic_read(&sk->sk_omem_alloc) + filter_size < sysctl_optmem_max) {
1205 atomic_add(filter_size, &sk->sk_omem_alloc);
1211 bool sk_filter_charge(struct sock *sk, struct sk_filter *fp)
1213 if (!refcount_inc_not_zero(&fp->refcnt))
1216 if (!__sk_filter_charge(sk, fp)) {
1217 sk_filter_release(fp);
1223 static struct bpf_prog *bpf_migrate_filter(struct bpf_prog *fp)
1225 struct sock_filter *old_prog;
1226 struct bpf_prog *old_fp;
1227 int err, new_len, old_len = fp->len;
1228 bool seen_ld_abs = false;
1230 /* We are free to overwrite insns et al right here as it
1231 * won't be used at this point in time anymore internally
1232 * after the migration to the internal BPF instruction
1235 BUILD_BUG_ON(sizeof(struct sock_filter) !=
1236 sizeof(struct bpf_insn));
1238 /* Conversion cannot happen on overlapping memory areas,
1239 * so we need to keep the user BPF around until the 2nd
1240 * pass. At this time, the user BPF is stored in fp->insns.
1242 old_prog = kmemdup(fp->insns, old_len * sizeof(struct sock_filter),
1243 GFP_KERNEL | __GFP_NOWARN);
1249 /* 1st pass: calculate the new program length. */
1250 err = bpf_convert_filter(old_prog, old_len, NULL, &new_len,
1255 /* Expand fp for appending the new filter representation. */
1257 fp = bpf_prog_realloc(old_fp, bpf_prog_size(new_len), 0);
1259 /* The old_fp is still around in case we couldn't
1260 * allocate new memory, so uncharge on that one.
1269 /* 2nd pass: remap sock_filter insns into bpf_insn insns. */
1270 err = bpf_convert_filter(old_prog, old_len, fp, &new_len,
1273 /* 2nd bpf_convert_filter() can fail only if it fails
1274 * to allocate memory, remapping must succeed. Note,
1275 * that at this time old_fp has already been released
1280 fp = bpf_prog_select_runtime(fp, &err);
1290 __bpf_prog_release(fp);
1291 return ERR_PTR(err);
1294 static struct bpf_prog *bpf_prepare_filter(struct bpf_prog *fp,
1295 bpf_aux_classic_check_t trans)
1299 fp->bpf_func = NULL;
1302 err = bpf_check_classic(fp->insns, fp->len);
1304 __bpf_prog_release(fp);
1305 return ERR_PTR(err);
1308 /* There might be additional checks and transformations
1309 * needed on classic filters, f.e. in case of seccomp.
1312 err = trans(fp->insns, fp->len);
1314 __bpf_prog_release(fp);
1315 return ERR_PTR(err);
1319 /* Probe if we can JIT compile the filter and if so, do
1320 * the compilation of the filter.
1322 bpf_jit_compile(fp);
1324 /* JIT compiler couldn't process this filter, so do the
1325 * internal BPF translation for the optimized interpreter.
1328 fp = bpf_migrate_filter(fp);
1334 * bpf_prog_create - create an unattached filter
1335 * @pfp: the unattached filter that is created
1336 * @fprog: the filter program
1338 * Create a filter independent of any socket. We first run some
1339 * sanity checks on it to make sure it does not explode on us later.
1340 * If an error occurs or there is insufficient memory for the filter
1341 * a negative errno code is returned. On success the return is zero.
1343 int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog)
1345 unsigned int fsize = bpf_classic_proglen(fprog);
1346 struct bpf_prog *fp;
1348 /* Make sure new filter is there and in the right amounts. */
1349 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1352 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1356 memcpy(fp->insns, fprog->filter, fsize);
1358 fp->len = fprog->len;
1359 /* Since unattached filters are not copied back to user
1360 * space through sk_get_filter(), we do not need to hold
1361 * a copy here, and can spare us the work.
1363 fp->orig_prog = NULL;
1365 /* bpf_prepare_filter() already takes care of freeing
1366 * memory in case something goes wrong.
1368 fp = bpf_prepare_filter(fp, NULL);
1375 EXPORT_SYMBOL_GPL(bpf_prog_create);
1378 * bpf_prog_create_from_user - create an unattached filter from user buffer
1379 * @pfp: the unattached filter that is created
1380 * @fprog: the filter program
1381 * @trans: post-classic verifier transformation handler
1382 * @save_orig: save classic BPF program
1384 * This function effectively does the same as bpf_prog_create(), only
1385 * that it builds up its insns buffer from user space provided buffer.
1386 * It also allows for passing a bpf_aux_classic_check_t handler.
1388 int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog,
1389 bpf_aux_classic_check_t trans, bool save_orig)
1391 unsigned int fsize = bpf_classic_proglen(fprog);
1392 struct bpf_prog *fp;
1395 /* Make sure new filter is there and in the right amounts. */
1396 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1399 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1403 if (copy_from_user(fp->insns, fprog->filter, fsize)) {
1404 __bpf_prog_free(fp);
1408 fp->len = fprog->len;
1409 fp->orig_prog = NULL;
1412 err = bpf_prog_store_orig_filter(fp, fprog);
1414 __bpf_prog_free(fp);
1419 /* bpf_prepare_filter() already takes care of freeing
1420 * memory in case something goes wrong.
1422 fp = bpf_prepare_filter(fp, trans);
1429 EXPORT_SYMBOL_GPL(bpf_prog_create_from_user);
1431 void bpf_prog_destroy(struct bpf_prog *fp)
1433 __bpf_prog_release(fp);
1435 EXPORT_SYMBOL_GPL(bpf_prog_destroy);
1437 static int __sk_attach_prog(struct bpf_prog *prog, struct sock *sk)
1439 struct sk_filter *fp, *old_fp;
1441 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
1447 if (!__sk_filter_charge(sk, fp)) {
1451 refcount_set(&fp->refcnt, 1);
1453 old_fp = rcu_dereference_protected(sk->sk_filter,
1454 lockdep_sock_is_held(sk));
1455 rcu_assign_pointer(sk->sk_filter, fp);
1458 sk_filter_uncharge(sk, old_fp);
1464 struct bpf_prog *__get_filter(struct sock_fprog *fprog, struct sock *sk)
1466 unsigned int fsize = bpf_classic_proglen(fprog);
1467 struct bpf_prog *prog;
1470 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1471 return ERR_PTR(-EPERM);
1473 /* Make sure new filter is there and in the right amounts. */
1474 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1475 return ERR_PTR(-EINVAL);
1477 prog = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1479 return ERR_PTR(-ENOMEM);
1481 if (copy_from_user(prog->insns, fprog->filter, fsize)) {
1482 __bpf_prog_free(prog);
1483 return ERR_PTR(-EFAULT);
1486 prog->len = fprog->len;
1488 err = bpf_prog_store_orig_filter(prog, fprog);
1490 __bpf_prog_free(prog);
1491 return ERR_PTR(-ENOMEM);
1494 /* bpf_prepare_filter() already takes care of freeing
1495 * memory in case something goes wrong.
1497 return bpf_prepare_filter(prog, NULL);
1501 * sk_attach_filter - attach a socket filter
1502 * @fprog: the filter program
1503 * @sk: the socket to use
1505 * Attach the user's filter code. We first run some sanity checks on
1506 * it to make sure it does not explode on us later. If an error
1507 * occurs or there is insufficient memory for the filter a negative
1508 * errno code is returned. On success the return is zero.
1510 int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1512 struct bpf_prog *prog = __get_filter(fprog, sk);
1516 return PTR_ERR(prog);
1518 err = __sk_attach_prog(prog, sk);
1520 __bpf_prog_release(prog);
1526 EXPORT_SYMBOL_GPL(sk_attach_filter);
1528 int sk_reuseport_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1530 struct bpf_prog *prog = __get_filter(fprog, sk);
1534 return PTR_ERR(prog);
1536 if (bpf_prog_size(prog->len) > sysctl_optmem_max)
1539 err = reuseport_attach_prog(sk, prog);
1542 __bpf_prog_release(prog);
1547 static struct bpf_prog *__get_bpf(u32 ufd, struct sock *sk)
1549 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1550 return ERR_PTR(-EPERM);
1552 return bpf_prog_get_type(ufd, BPF_PROG_TYPE_SOCKET_FILTER);
1555 int sk_attach_bpf(u32 ufd, struct sock *sk)
1557 struct bpf_prog *prog = __get_bpf(ufd, sk);
1561 return PTR_ERR(prog);
1563 err = __sk_attach_prog(prog, sk);
1572 int sk_reuseport_attach_bpf(u32 ufd, struct sock *sk)
1574 struct bpf_prog *prog;
1577 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1580 prog = bpf_prog_get_type(ufd, BPF_PROG_TYPE_SOCKET_FILTER);
1581 if (IS_ERR(prog) && PTR_ERR(prog) == -EINVAL)
1582 prog = bpf_prog_get_type(ufd, BPF_PROG_TYPE_SK_REUSEPORT);
1584 return PTR_ERR(prog);
1586 if (prog->type == BPF_PROG_TYPE_SK_REUSEPORT) {
1587 /* Like other non BPF_PROG_TYPE_SOCKET_FILTER
1588 * bpf prog (e.g. sockmap). It depends on the
1589 * limitation imposed by bpf_prog_load().
1590 * Hence, sysctl_optmem_max is not checked.
1592 if ((sk->sk_type != SOCK_STREAM &&
1593 sk->sk_type != SOCK_DGRAM) ||
1594 (sk->sk_protocol != IPPROTO_UDP &&
1595 sk->sk_protocol != IPPROTO_TCP) ||
1596 (sk->sk_family != AF_INET &&
1597 sk->sk_family != AF_INET6)) {
1602 /* BPF_PROG_TYPE_SOCKET_FILTER */
1603 if (bpf_prog_size(prog->len) > sysctl_optmem_max) {
1609 err = reuseport_attach_prog(sk, prog);
1617 void sk_reuseport_prog_free(struct bpf_prog *prog)
1622 if (prog->type == BPF_PROG_TYPE_SK_REUSEPORT)
1625 bpf_prog_destroy(prog);
1628 struct bpf_scratchpad {
1630 __be32 diff[MAX_BPF_STACK / sizeof(__be32)];
1631 u8 buff[MAX_BPF_STACK];
1635 static DEFINE_PER_CPU(struct bpf_scratchpad, bpf_sp);
1637 static inline int __bpf_try_make_writable(struct sk_buff *skb,
1638 unsigned int write_len)
1640 return skb_ensure_writable(skb, write_len);
1643 static inline int bpf_try_make_writable(struct sk_buff *skb,
1644 unsigned int write_len)
1646 int err = __bpf_try_make_writable(skb, write_len);
1648 bpf_compute_data_pointers(skb);
1652 static int bpf_try_make_head_writable(struct sk_buff *skb)
1654 return bpf_try_make_writable(skb, skb_headlen(skb));
1657 static inline void bpf_push_mac_rcsum(struct sk_buff *skb)
1659 if (skb_at_tc_ingress(skb))
1660 skb_postpush_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1663 static inline void bpf_pull_mac_rcsum(struct sk_buff *skb)
1665 if (skb_at_tc_ingress(skb))
1666 skb_postpull_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1669 BPF_CALL_5(bpf_skb_store_bytes, struct sk_buff *, skb, u32, offset,
1670 const void *, from, u32, len, u64, flags)
1674 if (unlikely(flags & ~(BPF_F_RECOMPUTE_CSUM | BPF_F_INVALIDATE_HASH)))
1676 if (unlikely(offset > 0xffff))
1678 if (unlikely(bpf_try_make_writable(skb, offset + len)))
1681 ptr = skb->data + offset;
1682 if (flags & BPF_F_RECOMPUTE_CSUM)
1683 __skb_postpull_rcsum(skb, ptr, len, offset);
1685 memcpy(ptr, from, len);
1687 if (flags & BPF_F_RECOMPUTE_CSUM)
1688 __skb_postpush_rcsum(skb, ptr, len, offset);
1689 if (flags & BPF_F_INVALIDATE_HASH)
1690 skb_clear_hash(skb);
1695 static const struct bpf_func_proto bpf_skb_store_bytes_proto = {
1696 .func = bpf_skb_store_bytes,
1698 .ret_type = RET_INTEGER,
1699 .arg1_type = ARG_PTR_TO_CTX,
1700 .arg2_type = ARG_ANYTHING,
1701 .arg3_type = ARG_PTR_TO_MEM,
1702 .arg4_type = ARG_CONST_SIZE,
1703 .arg5_type = ARG_ANYTHING,
1706 BPF_CALL_4(bpf_skb_load_bytes, const struct sk_buff *, skb, u32, offset,
1707 void *, to, u32, len)
1711 if (unlikely(offset > 0xffff))
1714 ptr = skb_header_pointer(skb, offset, len, to);
1718 memcpy(to, ptr, len);
1726 static const struct bpf_func_proto bpf_skb_load_bytes_proto = {
1727 .func = bpf_skb_load_bytes,
1729 .ret_type = RET_INTEGER,
1730 .arg1_type = ARG_PTR_TO_CTX,
1731 .arg2_type = ARG_ANYTHING,
1732 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
1733 .arg4_type = ARG_CONST_SIZE,
1736 BPF_CALL_5(bpf_skb_load_bytes_relative, const struct sk_buff *, skb,
1737 u32, offset, void *, to, u32, len, u32, start_header)
1739 u8 *end = skb_tail_pointer(skb);
1740 u8 *net = skb_network_header(skb);
1741 u8 *mac = skb_mac_header(skb);
1744 if (unlikely(offset > 0xffff || len > (end - mac)))
1747 switch (start_header) {
1748 case BPF_HDR_START_MAC:
1751 case BPF_HDR_START_NET:
1758 if (likely(ptr >= mac && ptr + len <= end)) {
1759 memcpy(to, ptr, len);
1768 static const struct bpf_func_proto bpf_skb_load_bytes_relative_proto = {
1769 .func = bpf_skb_load_bytes_relative,
1771 .ret_type = RET_INTEGER,
1772 .arg1_type = ARG_PTR_TO_CTX,
1773 .arg2_type = ARG_ANYTHING,
1774 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
1775 .arg4_type = ARG_CONST_SIZE,
1776 .arg5_type = ARG_ANYTHING,
1779 BPF_CALL_2(bpf_skb_pull_data, struct sk_buff *, skb, u32, len)
1781 /* Idea is the following: should the needed direct read/write
1782 * test fail during runtime, we can pull in more data and redo
1783 * again, since implicitly, we invalidate previous checks here.
1785 * Or, since we know how much we need to make read/writeable,
1786 * this can be done once at the program beginning for direct
1787 * access case. By this we overcome limitations of only current
1788 * headroom being accessible.
1790 return bpf_try_make_writable(skb, len ? : skb_headlen(skb));
1793 static const struct bpf_func_proto bpf_skb_pull_data_proto = {
1794 .func = bpf_skb_pull_data,
1796 .ret_type = RET_INTEGER,
1797 .arg1_type = ARG_PTR_TO_CTX,
1798 .arg2_type = ARG_ANYTHING,
1801 static inline int sk_skb_try_make_writable(struct sk_buff *skb,
1802 unsigned int write_len)
1804 int err = __bpf_try_make_writable(skb, write_len);
1806 bpf_compute_data_end_sk_skb(skb);
1810 BPF_CALL_2(sk_skb_pull_data, struct sk_buff *, skb, u32, len)
1812 /* Idea is the following: should the needed direct read/write
1813 * test fail during runtime, we can pull in more data and redo
1814 * again, since implicitly, we invalidate previous checks here.
1816 * Or, since we know how much we need to make read/writeable,
1817 * this can be done once at the program beginning for direct
1818 * access case. By this we overcome limitations of only current
1819 * headroom being accessible.
1821 return sk_skb_try_make_writable(skb, len ? : skb_headlen(skb));
1824 static const struct bpf_func_proto sk_skb_pull_data_proto = {
1825 .func = sk_skb_pull_data,
1827 .ret_type = RET_INTEGER,
1828 .arg1_type = ARG_PTR_TO_CTX,
1829 .arg2_type = ARG_ANYTHING,
1832 BPF_CALL_5(bpf_l3_csum_replace, struct sk_buff *, skb, u32, offset,
1833 u64, from, u64, to, u64, flags)
1837 if (unlikely(flags & ~(BPF_F_HDR_FIELD_MASK)))
1839 if (unlikely(offset > 0xffff || offset & 1))
1841 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
1844 ptr = (__sum16 *)(skb->data + offset);
1845 switch (flags & BPF_F_HDR_FIELD_MASK) {
1847 if (unlikely(from != 0))
1850 csum_replace_by_diff(ptr, to);
1853 csum_replace2(ptr, from, to);
1856 csum_replace4(ptr, from, to);
1865 static const struct bpf_func_proto bpf_l3_csum_replace_proto = {
1866 .func = bpf_l3_csum_replace,
1868 .ret_type = RET_INTEGER,
1869 .arg1_type = ARG_PTR_TO_CTX,
1870 .arg2_type = ARG_ANYTHING,
1871 .arg3_type = ARG_ANYTHING,
1872 .arg4_type = ARG_ANYTHING,
1873 .arg5_type = ARG_ANYTHING,
1876 BPF_CALL_5(bpf_l4_csum_replace, struct sk_buff *, skb, u32, offset,
1877 u64, from, u64, to, u64, flags)
1879 bool is_pseudo = flags & BPF_F_PSEUDO_HDR;
1880 bool is_mmzero = flags & BPF_F_MARK_MANGLED_0;
1881 bool do_mforce = flags & BPF_F_MARK_ENFORCE;
1884 if (unlikely(flags & ~(BPF_F_MARK_MANGLED_0 | BPF_F_MARK_ENFORCE |
1885 BPF_F_PSEUDO_HDR | BPF_F_HDR_FIELD_MASK)))
1887 if (unlikely(offset > 0xffff || offset & 1))
1889 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
1892 ptr = (__sum16 *)(skb->data + offset);
1893 if (is_mmzero && !do_mforce && !*ptr)
1896 switch (flags & BPF_F_HDR_FIELD_MASK) {
1898 if (unlikely(from != 0))
1901 inet_proto_csum_replace_by_diff(ptr, skb, to, is_pseudo);
1904 inet_proto_csum_replace2(ptr, skb, from, to, is_pseudo);
1907 inet_proto_csum_replace4(ptr, skb, from, to, is_pseudo);
1913 if (is_mmzero && !*ptr)
1914 *ptr = CSUM_MANGLED_0;
1918 static const struct bpf_func_proto bpf_l4_csum_replace_proto = {
1919 .func = bpf_l4_csum_replace,
1921 .ret_type = RET_INTEGER,
1922 .arg1_type = ARG_PTR_TO_CTX,
1923 .arg2_type = ARG_ANYTHING,
1924 .arg3_type = ARG_ANYTHING,
1925 .arg4_type = ARG_ANYTHING,
1926 .arg5_type = ARG_ANYTHING,
1929 BPF_CALL_5(bpf_csum_diff, __be32 *, from, u32, from_size,
1930 __be32 *, to, u32, to_size, __wsum, seed)
1932 struct bpf_scratchpad *sp = this_cpu_ptr(&bpf_sp);
1933 u32 diff_size = from_size + to_size;
1936 /* This is quite flexible, some examples:
1938 * from_size == 0, to_size > 0, seed := csum --> pushing data
1939 * from_size > 0, to_size == 0, seed := csum --> pulling data
1940 * from_size > 0, to_size > 0, seed := 0 --> diffing data
1942 * Even for diffing, from_size and to_size don't need to be equal.
1944 if (unlikely(((from_size | to_size) & (sizeof(__be32) - 1)) ||
1945 diff_size > sizeof(sp->diff)))
1948 for (i = 0; i < from_size / sizeof(__be32); i++, j++)
1949 sp->diff[j] = ~from[i];
1950 for (i = 0; i < to_size / sizeof(__be32); i++, j++)
1951 sp->diff[j] = to[i];
1953 return csum_partial(sp->diff, diff_size, seed);
1956 static const struct bpf_func_proto bpf_csum_diff_proto = {
1957 .func = bpf_csum_diff,
1960 .ret_type = RET_INTEGER,
1961 .arg1_type = ARG_PTR_TO_MEM_OR_NULL,
1962 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
1963 .arg3_type = ARG_PTR_TO_MEM_OR_NULL,
1964 .arg4_type = ARG_CONST_SIZE_OR_ZERO,
1965 .arg5_type = ARG_ANYTHING,
1968 BPF_CALL_2(bpf_csum_update, struct sk_buff *, skb, __wsum, csum)
1970 /* The interface is to be used in combination with bpf_csum_diff()
1971 * for direct packet writes. csum rotation for alignment as well
1972 * as emulating csum_sub() can be done from the eBPF program.
1974 if (skb->ip_summed == CHECKSUM_COMPLETE)
1975 return (skb->csum = csum_add(skb->csum, csum));
1980 static const struct bpf_func_proto bpf_csum_update_proto = {
1981 .func = bpf_csum_update,
1983 .ret_type = RET_INTEGER,
1984 .arg1_type = ARG_PTR_TO_CTX,
1985 .arg2_type = ARG_ANYTHING,
1988 static inline int __bpf_rx_skb(struct net_device *dev, struct sk_buff *skb)
1990 return dev_forward_skb(dev, skb);
1993 static inline int __bpf_rx_skb_no_mac(struct net_device *dev,
1994 struct sk_buff *skb)
1996 int ret = ____dev_forward_skb(dev, skb);
2000 ret = netif_rx(skb);
2006 static inline int __bpf_tx_skb(struct net_device *dev, struct sk_buff *skb)
2010 if (unlikely(__this_cpu_read(xmit_recursion) > XMIT_RECURSION_LIMIT)) {
2011 net_crit_ratelimited("bpf: recursion limit reached on datapath, buggy bpf program?\n");
2018 __this_cpu_inc(xmit_recursion);
2019 ret = dev_queue_xmit(skb);
2020 __this_cpu_dec(xmit_recursion);
2025 static int __bpf_redirect_no_mac(struct sk_buff *skb, struct net_device *dev,
2028 /* skb->mac_len is not set on normal egress */
2029 unsigned int mlen = skb->network_header - skb->mac_header;
2031 __skb_pull(skb, mlen);
2033 /* At ingress, the mac header has already been pulled once.
2034 * At egress, skb_pospull_rcsum has to be done in case that
2035 * the skb is originated from ingress (i.e. a forwarded skb)
2036 * to ensure that rcsum starts at net header.
2038 if (!skb_at_tc_ingress(skb))
2039 skb_postpull_rcsum(skb, skb_mac_header(skb), mlen);
2040 skb_pop_mac_header(skb);
2041 skb_reset_mac_len(skb);
2042 return flags & BPF_F_INGRESS ?
2043 __bpf_rx_skb_no_mac(dev, skb) : __bpf_tx_skb(dev, skb);
2046 static int __bpf_redirect_common(struct sk_buff *skb, struct net_device *dev,
2049 /* Verify that a link layer header is carried */
2050 if (unlikely(skb->mac_header >= skb->network_header)) {
2055 bpf_push_mac_rcsum(skb);
2056 return flags & BPF_F_INGRESS ?
2057 __bpf_rx_skb(dev, skb) : __bpf_tx_skb(dev, skb);
2060 static int __bpf_redirect(struct sk_buff *skb, struct net_device *dev,
2063 if (dev_is_mac_header_xmit(dev))
2064 return __bpf_redirect_common(skb, dev, flags);
2066 return __bpf_redirect_no_mac(skb, dev, flags);
2069 BPF_CALL_3(bpf_clone_redirect, struct sk_buff *, skb, u32, ifindex, u64, flags)
2071 struct net_device *dev;
2072 struct sk_buff *clone;
2075 if (unlikely(flags & ~(BPF_F_INGRESS)))
2078 dev = dev_get_by_index_rcu(dev_net(skb->dev), ifindex);
2082 clone = skb_clone(skb, GFP_ATOMIC);
2083 if (unlikely(!clone))
2086 /* For direct write, we need to keep the invariant that the skbs
2087 * we're dealing with need to be uncloned. Should uncloning fail
2088 * here, we need to free the just generated clone to unclone once
2091 ret = bpf_try_make_head_writable(skb);
2092 if (unlikely(ret)) {
2097 return __bpf_redirect(clone, dev, flags);
2100 static const struct bpf_func_proto bpf_clone_redirect_proto = {
2101 .func = bpf_clone_redirect,
2103 .ret_type = RET_INTEGER,
2104 .arg1_type = ARG_PTR_TO_CTX,
2105 .arg2_type = ARG_ANYTHING,
2106 .arg3_type = ARG_ANYTHING,
2109 DEFINE_PER_CPU(struct bpf_redirect_info, bpf_redirect_info);
2110 EXPORT_PER_CPU_SYMBOL_GPL(bpf_redirect_info);
2112 BPF_CALL_2(bpf_redirect, u32, ifindex, u64, flags)
2114 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
2116 if (unlikely(flags & ~(BPF_F_INGRESS)))
2119 ri->ifindex = ifindex;
2122 return TC_ACT_REDIRECT;
2125 int skb_do_redirect(struct sk_buff *skb)
2127 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
2128 struct net_device *dev;
2130 dev = dev_get_by_index_rcu(dev_net(skb->dev), ri->ifindex);
2132 if (unlikely(!dev)) {
2137 return __bpf_redirect(skb, dev, ri->flags);
2140 static const struct bpf_func_proto bpf_redirect_proto = {
2141 .func = bpf_redirect,
2143 .ret_type = RET_INTEGER,
2144 .arg1_type = ARG_ANYTHING,
2145 .arg2_type = ARG_ANYTHING,
2148 BPF_CALL_2(bpf_msg_apply_bytes, struct sk_msg *, msg, u32, bytes)
2150 msg->apply_bytes = bytes;
2154 static const struct bpf_func_proto bpf_msg_apply_bytes_proto = {
2155 .func = bpf_msg_apply_bytes,
2157 .ret_type = RET_INTEGER,
2158 .arg1_type = ARG_PTR_TO_CTX,
2159 .arg2_type = ARG_ANYTHING,
2162 BPF_CALL_2(bpf_msg_cork_bytes, struct sk_msg *, msg, u32, bytes)
2164 msg->cork_bytes = bytes;
2168 static const struct bpf_func_proto bpf_msg_cork_bytes_proto = {
2169 .func = bpf_msg_cork_bytes,
2171 .ret_type = RET_INTEGER,
2172 .arg1_type = ARG_PTR_TO_CTX,
2173 .arg2_type = ARG_ANYTHING,
2176 BPF_CALL_4(bpf_msg_pull_data, struct sk_msg *, msg, u32, start,
2177 u32, end, u64, flags)
2179 u32 len = 0, offset = 0, copy = 0, poffset = 0, bytes = end - start;
2180 u32 first_sge, last_sge, i, shift, bytes_sg_total;
2181 struct scatterlist *sge;
2182 u8 *raw, *to, *from;
2185 if (unlikely(flags || end <= start))
2188 /* First find the starting scatterlist element */
2191 len = sk_msg_elem(msg, i)->length;
2192 if (start < offset + len)
2195 sk_msg_iter_var_next(i);
2196 } while (i != msg->sg.end);
2198 if (unlikely(start >= offset + len))
2202 /* The start may point into the sg element so we need to also
2203 * account for the headroom.
2205 bytes_sg_total = start - offset + bytes;
2206 if (!msg->sg.copy[i] && bytes_sg_total <= len)
2209 /* At this point we need to linearize multiple scatterlist
2210 * elements or a single shared page. Either way we need to
2211 * copy into a linear buffer exclusively owned by BPF. Then
2212 * place the buffer in the scatterlist and fixup the original
2213 * entries by removing the entries now in the linear buffer
2214 * and shifting the remaining entries. For now we do not try
2215 * to copy partial entries to avoid complexity of running out
2216 * of sg_entry slots. The downside is reading a single byte
2217 * will copy the entire sg entry.
2220 copy += sk_msg_elem(msg, i)->length;
2221 sk_msg_iter_var_next(i);
2222 if (bytes_sg_total <= copy)
2224 } while (i != msg->sg.end);
2227 if (unlikely(bytes_sg_total > copy))
2230 page = alloc_pages(__GFP_NOWARN | GFP_ATOMIC | __GFP_COMP,
2232 if (unlikely(!page))
2235 raw = page_address(page);
2238 sge = sk_msg_elem(msg, i);
2239 from = sg_virt(sge);
2243 memcpy(to, from, len);
2246 put_page(sg_page(sge));
2248 sk_msg_iter_var_next(i);
2249 } while (i != last_sge);
2251 sg_set_page(&msg->sg.data[first_sge], page, copy, 0);
2253 /* To repair sg ring we need to shift entries. If we only
2254 * had a single entry though we can just replace it and
2255 * be done. Otherwise walk the ring and shift the entries.
2257 WARN_ON_ONCE(last_sge == first_sge);
2258 shift = last_sge > first_sge ?
2259 last_sge - first_sge - 1 :
2260 MAX_SKB_FRAGS - first_sge + last_sge - 1;
2265 sk_msg_iter_var_next(i);
2269 if (i + shift >= MAX_MSG_FRAGS)
2270 move_from = i + shift - MAX_MSG_FRAGS;
2272 move_from = i + shift;
2273 if (move_from == msg->sg.end)
2276 msg->sg.data[i] = msg->sg.data[move_from];
2277 msg->sg.data[move_from].length = 0;
2278 msg->sg.data[move_from].page_link = 0;
2279 msg->sg.data[move_from].offset = 0;
2280 sk_msg_iter_var_next(i);
2283 msg->sg.end = msg->sg.end - shift > msg->sg.end ?
2284 msg->sg.end - shift + MAX_MSG_FRAGS :
2285 msg->sg.end - shift;
2287 msg->data = sg_virt(&msg->sg.data[first_sge]) + start - offset;
2288 msg->data_end = msg->data + bytes;
2292 static const struct bpf_func_proto bpf_msg_pull_data_proto = {
2293 .func = bpf_msg_pull_data,
2295 .ret_type = RET_INTEGER,
2296 .arg1_type = ARG_PTR_TO_CTX,
2297 .arg2_type = ARG_ANYTHING,
2298 .arg3_type = ARG_ANYTHING,
2299 .arg4_type = ARG_ANYTHING,
2302 BPF_CALL_4(bpf_msg_push_data, struct sk_msg *, msg, u32, start,
2303 u32, len, u64, flags)
2305 struct scatterlist sge, nsge, nnsge, rsge = {0}, *psge;
2306 u32 new, i = 0, l, space, copy = 0, offset = 0;
2307 u8 *raw, *to, *from;
2310 if (unlikely(flags))
2313 /* First find the starting scatterlist element */
2316 l = sk_msg_elem(msg, i)->length;
2318 if (start < offset + l)
2321 sk_msg_iter_var_next(i);
2322 } while (i != msg->sg.end);
2324 if (start >= offset + l)
2327 space = MAX_MSG_FRAGS - sk_msg_elem_used(msg);
2329 /* If no space available will fallback to copy, we need at
2330 * least one scatterlist elem available to push data into
2331 * when start aligns to the beginning of an element or two
2332 * when it falls inside an element. We handle the start equals
2333 * offset case because its the common case for inserting a
2336 if (!space || (space == 1 && start != offset))
2337 copy = msg->sg.data[i].length;
2339 page = alloc_pages(__GFP_NOWARN | GFP_ATOMIC | __GFP_COMP,
2340 get_order(copy + len));
2341 if (unlikely(!page))
2347 raw = page_address(page);
2349 psge = sk_msg_elem(msg, i);
2350 front = start - offset;
2351 back = psge->length - front;
2352 from = sg_virt(psge);
2355 memcpy(raw, from, front);
2359 to = raw + front + len;
2361 memcpy(to, from, back);
2364 put_page(sg_page(psge));
2365 } else if (start - offset) {
2366 psge = sk_msg_elem(msg, i);
2367 rsge = sk_msg_elem_cpy(msg, i);
2369 psge->length = start - offset;
2370 rsge.length -= psge->length;
2371 rsge.offset += start;
2373 sk_msg_iter_var_next(i);
2374 sg_unmark_end(psge);
2375 sk_msg_iter_next(msg, end);
2378 /* Slot(s) to place newly allocated data */
2381 /* Shift one or two slots as needed */
2383 sge = sk_msg_elem_cpy(msg, i);
2385 sk_msg_iter_var_next(i);
2386 sg_unmark_end(&sge);
2387 sk_msg_iter_next(msg, end);
2389 nsge = sk_msg_elem_cpy(msg, i);
2391 sk_msg_iter_var_next(i);
2392 nnsge = sk_msg_elem_cpy(msg, i);
2395 while (i != msg->sg.end) {
2396 msg->sg.data[i] = sge;
2398 sk_msg_iter_var_next(i);
2401 nnsge = sk_msg_elem_cpy(msg, i);
2403 nsge = sk_msg_elem_cpy(msg, i);
2408 /* Place newly allocated data buffer */
2409 sk_mem_charge(msg->sk, len);
2410 msg->sg.size += len;
2411 msg->sg.copy[new] = false;
2412 sg_set_page(&msg->sg.data[new], page, len + copy, 0);
2414 get_page(sg_page(&rsge));
2415 sk_msg_iter_var_next(new);
2416 msg->sg.data[new] = rsge;
2419 sk_msg_compute_data_pointers(msg);
2423 static const struct bpf_func_proto bpf_msg_push_data_proto = {
2424 .func = bpf_msg_push_data,
2426 .ret_type = RET_INTEGER,
2427 .arg1_type = ARG_PTR_TO_CTX,
2428 .arg2_type = ARG_ANYTHING,
2429 .arg3_type = ARG_ANYTHING,
2430 .arg4_type = ARG_ANYTHING,
2433 BPF_CALL_1(bpf_get_cgroup_classid, const struct sk_buff *, skb)
2435 return task_get_classid(skb);
2438 static const struct bpf_func_proto bpf_get_cgroup_classid_proto = {
2439 .func = bpf_get_cgroup_classid,
2441 .ret_type = RET_INTEGER,
2442 .arg1_type = ARG_PTR_TO_CTX,
2445 BPF_CALL_1(bpf_get_route_realm, const struct sk_buff *, skb)
2447 return dst_tclassid(skb);
2450 static const struct bpf_func_proto bpf_get_route_realm_proto = {
2451 .func = bpf_get_route_realm,
2453 .ret_type = RET_INTEGER,
2454 .arg1_type = ARG_PTR_TO_CTX,
2457 BPF_CALL_1(bpf_get_hash_recalc, struct sk_buff *, skb)
2459 /* If skb_clear_hash() was called due to mangling, we can
2460 * trigger SW recalculation here. Later access to hash
2461 * can then use the inline skb->hash via context directly
2462 * instead of calling this helper again.
2464 return skb_get_hash(skb);
2467 static const struct bpf_func_proto bpf_get_hash_recalc_proto = {
2468 .func = bpf_get_hash_recalc,
2470 .ret_type = RET_INTEGER,
2471 .arg1_type = ARG_PTR_TO_CTX,
2474 BPF_CALL_1(bpf_set_hash_invalid, struct sk_buff *, skb)
2476 /* After all direct packet write, this can be used once for
2477 * triggering a lazy recalc on next skb_get_hash() invocation.
2479 skb_clear_hash(skb);
2483 static const struct bpf_func_proto bpf_set_hash_invalid_proto = {
2484 .func = bpf_set_hash_invalid,
2486 .ret_type = RET_INTEGER,
2487 .arg1_type = ARG_PTR_TO_CTX,
2490 BPF_CALL_2(bpf_set_hash, struct sk_buff *, skb, u32, hash)
2492 /* Set user specified hash as L4(+), so that it gets returned
2493 * on skb_get_hash() call unless BPF prog later on triggers a
2496 __skb_set_sw_hash(skb, hash, true);
2500 static const struct bpf_func_proto bpf_set_hash_proto = {
2501 .func = bpf_set_hash,
2503 .ret_type = RET_INTEGER,
2504 .arg1_type = ARG_PTR_TO_CTX,
2505 .arg2_type = ARG_ANYTHING,
2508 BPF_CALL_3(bpf_skb_vlan_push, struct sk_buff *, skb, __be16, vlan_proto,
2513 if (unlikely(vlan_proto != htons(ETH_P_8021Q) &&
2514 vlan_proto != htons(ETH_P_8021AD)))
2515 vlan_proto = htons(ETH_P_8021Q);
2517 bpf_push_mac_rcsum(skb);
2518 ret = skb_vlan_push(skb, vlan_proto, vlan_tci);
2519 bpf_pull_mac_rcsum(skb);
2521 bpf_compute_data_pointers(skb);
2525 static const struct bpf_func_proto bpf_skb_vlan_push_proto = {
2526 .func = bpf_skb_vlan_push,
2528 .ret_type = RET_INTEGER,
2529 .arg1_type = ARG_PTR_TO_CTX,
2530 .arg2_type = ARG_ANYTHING,
2531 .arg3_type = ARG_ANYTHING,
2534 BPF_CALL_1(bpf_skb_vlan_pop, struct sk_buff *, skb)
2538 bpf_push_mac_rcsum(skb);
2539 ret = skb_vlan_pop(skb);
2540 bpf_pull_mac_rcsum(skb);
2542 bpf_compute_data_pointers(skb);
2546 static const struct bpf_func_proto bpf_skb_vlan_pop_proto = {
2547 .func = bpf_skb_vlan_pop,
2549 .ret_type = RET_INTEGER,
2550 .arg1_type = ARG_PTR_TO_CTX,
2553 static int bpf_skb_generic_push(struct sk_buff *skb, u32 off, u32 len)
2555 /* Caller already did skb_cow() with len as headroom,
2556 * so no need to do it here.
2559 memmove(skb->data, skb->data + len, off);
2560 memset(skb->data + off, 0, len);
2562 /* No skb_postpush_rcsum(skb, skb->data + off, len)
2563 * needed here as it does not change the skb->csum
2564 * result for checksum complete when summing over
2570 static int bpf_skb_generic_pop(struct sk_buff *skb, u32 off, u32 len)
2572 /* skb_ensure_writable() is not needed here, as we're
2573 * already working on an uncloned skb.
2575 if (unlikely(!pskb_may_pull(skb, off + len)))
2578 skb_postpull_rcsum(skb, skb->data + off, len);
2579 memmove(skb->data + len, skb->data, off);
2580 __skb_pull(skb, len);
2585 static int bpf_skb_net_hdr_push(struct sk_buff *skb, u32 off, u32 len)
2587 bool trans_same = skb->transport_header == skb->network_header;
2590 /* There's no need for __skb_push()/__skb_pull() pair to
2591 * get to the start of the mac header as we're guaranteed
2592 * to always start from here under eBPF.
2594 ret = bpf_skb_generic_push(skb, off, len);
2596 skb->mac_header -= len;
2597 skb->network_header -= len;
2599 skb->transport_header = skb->network_header;
2605 static int bpf_skb_net_hdr_pop(struct sk_buff *skb, u32 off, u32 len)
2607 bool trans_same = skb->transport_header == skb->network_header;
2610 /* Same here, __skb_push()/__skb_pull() pair not needed. */
2611 ret = bpf_skb_generic_pop(skb, off, len);
2613 skb->mac_header += len;
2614 skb->network_header += len;
2616 skb->transport_header = skb->network_header;
2622 static int bpf_skb_proto_4_to_6(struct sk_buff *skb)
2624 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
2625 u32 off = skb_mac_header_len(skb);
2628 /* SCTP uses GSO_BY_FRAGS, thus cannot adjust it. */
2629 if (skb_is_gso(skb) && unlikely(skb_is_gso_sctp(skb)))
2632 ret = skb_cow(skb, len_diff);
2633 if (unlikely(ret < 0))
2636 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
2637 if (unlikely(ret < 0))
2640 if (skb_is_gso(skb)) {
2641 struct skb_shared_info *shinfo = skb_shinfo(skb);
2643 /* SKB_GSO_TCPV4 needs to be changed into
2646 if (shinfo->gso_type & SKB_GSO_TCPV4) {
2647 shinfo->gso_type &= ~SKB_GSO_TCPV4;
2648 shinfo->gso_type |= SKB_GSO_TCPV6;
2651 /* Due to IPv6 header, MSS needs to be downgraded. */
2652 skb_decrease_gso_size(shinfo, len_diff);
2653 /* Header must be checked, and gso_segs recomputed. */
2654 shinfo->gso_type |= SKB_GSO_DODGY;
2655 shinfo->gso_segs = 0;
2658 skb->protocol = htons(ETH_P_IPV6);
2659 skb_clear_hash(skb);
2664 static int bpf_skb_proto_6_to_4(struct sk_buff *skb)
2666 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
2667 u32 off = skb_mac_header_len(skb);
2670 /* SCTP uses GSO_BY_FRAGS, thus cannot adjust it. */
2671 if (skb_is_gso(skb) && unlikely(skb_is_gso_sctp(skb)))
2674 ret = skb_unclone(skb, GFP_ATOMIC);
2675 if (unlikely(ret < 0))
2678 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
2679 if (unlikely(ret < 0))
2682 if (skb_is_gso(skb)) {
2683 struct skb_shared_info *shinfo = skb_shinfo(skb);
2685 /* SKB_GSO_TCPV6 needs to be changed into
2688 if (shinfo->gso_type & SKB_GSO_TCPV6) {
2689 shinfo->gso_type &= ~SKB_GSO_TCPV6;
2690 shinfo->gso_type |= SKB_GSO_TCPV4;
2693 /* Due to IPv4 header, MSS can be upgraded. */
2694 skb_increase_gso_size(shinfo, len_diff);
2695 /* Header must be checked, and gso_segs recomputed. */
2696 shinfo->gso_type |= SKB_GSO_DODGY;
2697 shinfo->gso_segs = 0;
2700 skb->protocol = htons(ETH_P_IP);
2701 skb_clear_hash(skb);
2706 static int bpf_skb_proto_xlat(struct sk_buff *skb, __be16 to_proto)
2708 __be16 from_proto = skb->protocol;
2710 if (from_proto == htons(ETH_P_IP) &&
2711 to_proto == htons(ETH_P_IPV6))
2712 return bpf_skb_proto_4_to_6(skb);
2714 if (from_proto == htons(ETH_P_IPV6) &&
2715 to_proto == htons(ETH_P_IP))
2716 return bpf_skb_proto_6_to_4(skb);
2721 BPF_CALL_3(bpf_skb_change_proto, struct sk_buff *, skb, __be16, proto,
2726 if (unlikely(flags))
2729 /* General idea is that this helper does the basic groundwork
2730 * needed for changing the protocol, and eBPF program fills the
2731 * rest through bpf_skb_store_bytes(), bpf_lX_csum_replace()
2732 * and other helpers, rather than passing a raw buffer here.
2734 * The rationale is to keep this minimal and without a need to
2735 * deal with raw packet data. F.e. even if we would pass buffers
2736 * here, the program still needs to call the bpf_lX_csum_replace()
2737 * helpers anyway. Plus, this way we keep also separation of
2738 * concerns, since f.e. bpf_skb_store_bytes() should only take
2741 * Currently, additional options and extension header space are
2742 * not supported, but flags register is reserved so we can adapt
2743 * that. For offloads, we mark packet as dodgy, so that headers
2744 * need to be verified first.
2746 ret = bpf_skb_proto_xlat(skb, proto);
2747 bpf_compute_data_pointers(skb);
2751 static const struct bpf_func_proto bpf_skb_change_proto_proto = {
2752 .func = bpf_skb_change_proto,
2754 .ret_type = RET_INTEGER,
2755 .arg1_type = ARG_PTR_TO_CTX,
2756 .arg2_type = ARG_ANYTHING,
2757 .arg3_type = ARG_ANYTHING,
2760 BPF_CALL_2(bpf_skb_change_type, struct sk_buff *, skb, u32, pkt_type)
2762 /* We only allow a restricted subset to be changed for now. */
2763 if (unlikely(!skb_pkt_type_ok(skb->pkt_type) ||
2764 !skb_pkt_type_ok(pkt_type)))
2767 skb->pkt_type = pkt_type;
2771 static const struct bpf_func_proto bpf_skb_change_type_proto = {
2772 .func = bpf_skb_change_type,
2774 .ret_type = RET_INTEGER,
2775 .arg1_type = ARG_PTR_TO_CTX,
2776 .arg2_type = ARG_ANYTHING,
2779 static u32 bpf_skb_net_base_len(const struct sk_buff *skb)
2781 switch (skb->protocol) {
2782 case htons(ETH_P_IP):
2783 return sizeof(struct iphdr);
2784 case htons(ETH_P_IPV6):
2785 return sizeof(struct ipv6hdr);
2791 static int bpf_skb_net_grow(struct sk_buff *skb, u32 len_diff)
2793 u32 off = skb_mac_header_len(skb) + bpf_skb_net_base_len(skb);
2796 /* SCTP uses GSO_BY_FRAGS, thus cannot adjust it. */
2797 if (skb_is_gso(skb) && unlikely(skb_is_gso_sctp(skb)))
2800 ret = skb_cow(skb, len_diff);
2801 if (unlikely(ret < 0))
2804 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
2805 if (unlikely(ret < 0))
2808 if (skb_is_gso(skb)) {
2809 struct skb_shared_info *shinfo = skb_shinfo(skb);
2811 /* Due to header grow, MSS needs to be downgraded. */
2812 skb_decrease_gso_size(shinfo, len_diff);
2813 /* Header must be checked, and gso_segs recomputed. */
2814 shinfo->gso_type |= SKB_GSO_DODGY;
2815 shinfo->gso_segs = 0;
2821 static int bpf_skb_net_shrink(struct sk_buff *skb, u32 len_diff)
2823 u32 off = skb_mac_header_len(skb) + bpf_skb_net_base_len(skb);
2826 /* SCTP uses GSO_BY_FRAGS, thus cannot adjust it. */
2827 if (skb_is_gso(skb) && unlikely(skb_is_gso_sctp(skb)))
2830 ret = skb_unclone(skb, GFP_ATOMIC);
2831 if (unlikely(ret < 0))
2834 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
2835 if (unlikely(ret < 0))
2838 if (skb_is_gso(skb)) {
2839 struct skb_shared_info *shinfo = skb_shinfo(skb);
2841 /* Due to header shrink, MSS can be upgraded. */
2842 skb_increase_gso_size(shinfo, len_diff);
2843 /* Header must be checked, and gso_segs recomputed. */
2844 shinfo->gso_type |= SKB_GSO_DODGY;
2845 shinfo->gso_segs = 0;
2851 static u32 __bpf_skb_max_len(const struct sk_buff *skb)
2853 return skb->dev ? skb->dev->mtu + skb->dev->hard_header_len :
2857 static int bpf_skb_adjust_net(struct sk_buff *skb, s32 len_diff)
2859 bool trans_same = skb->transport_header == skb->network_header;
2860 u32 len_cur, len_diff_abs = abs(len_diff);
2861 u32 len_min = bpf_skb_net_base_len(skb);
2862 u32 len_max = __bpf_skb_max_len(skb);
2863 __be16 proto = skb->protocol;
2864 bool shrink = len_diff < 0;
2867 if (unlikely(len_diff_abs > 0xfffU))
2869 if (unlikely(proto != htons(ETH_P_IP) &&
2870 proto != htons(ETH_P_IPV6)))
2873 len_cur = skb->len - skb_network_offset(skb);
2874 if (skb_transport_header_was_set(skb) && !trans_same)
2875 len_cur = skb_network_header_len(skb);
2876 if ((shrink && (len_diff_abs >= len_cur ||
2877 len_cur - len_diff_abs < len_min)) ||
2878 (!shrink && (skb->len + len_diff_abs > len_max &&
2882 ret = shrink ? bpf_skb_net_shrink(skb, len_diff_abs) :
2883 bpf_skb_net_grow(skb, len_diff_abs);
2885 bpf_compute_data_pointers(skb);
2889 BPF_CALL_4(bpf_skb_adjust_room, struct sk_buff *, skb, s32, len_diff,
2890 u32, mode, u64, flags)
2892 if (unlikely(flags))
2894 if (likely(mode == BPF_ADJ_ROOM_NET))
2895 return bpf_skb_adjust_net(skb, len_diff);
2900 static const struct bpf_func_proto bpf_skb_adjust_room_proto = {
2901 .func = bpf_skb_adjust_room,
2903 .ret_type = RET_INTEGER,
2904 .arg1_type = ARG_PTR_TO_CTX,
2905 .arg2_type = ARG_ANYTHING,
2906 .arg3_type = ARG_ANYTHING,
2907 .arg4_type = ARG_ANYTHING,
2910 static u32 __bpf_skb_min_len(const struct sk_buff *skb)
2912 u32 min_len = skb_network_offset(skb);
2914 if (skb_transport_header_was_set(skb))
2915 min_len = skb_transport_offset(skb);
2916 if (skb->ip_summed == CHECKSUM_PARTIAL)
2917 min_len = skb_checksum_start_offset(skb) +
2918 skb->csum_offset + sizeof(__sum16);
2922 static int bpf_skb_grow_rcsum(struct sk_buff *skb, unsigned int new_len)
2924 unsigned int old_len = skb->len;
2927 ret = __skb_grow_rcsum(skb, new_len);
2929 memset(skb->data + old_len, 0, new_len - old_len);
2933 static int bpf_skb_trim_rcsum(struct sk_buff *skb, unsigned int new_len)
2935 return __skb_trim_rcsum(skb, new_len);
2938 static inline int __bpf_skb_change_tail(struct sk_buff *skb, u32 new_len,
2941 u32 max_len = __bpf_skb_max_len(skb);
2942 u32 min_len = __bpf_skb_min_len(skb);
2945 if (unlikely(flags || new_len > max_len || new_len < min_len))
2947 if (skb->encapsulation)
2950 /* The basic idea of this helper is that it's performing the
2951 * needed work to either grow or trim an skb, and eBPF program
2952 * rewrites the rest via helpers like bpf_skb_store_bytes(),
2953 * bpf_lX_csum_replace() and others rather than passing a raw
2954 * buffer here. This one is a slow path helper and intended
2955 * for replies with control messages.
2957 * Like in bpf_skb_change_proto(), we want to keep this rather
2958 * minimal and without protocol specifics so that we are able
2959 * to separate concerns as in bpf_skb_store_bytes() should only
2960 * be the one responsible for writing buffers.
2962 * It's really expected to be a slow path operation here for
2963 * control message replies, so we're implicitly linearizing,
2964 * uncloning and drop offloads from the skb by this.
2966 ret = __bpf_try_make_writable(skb, skb->len);
2968 if (new_len > skb->len)
2969 ret = bpf_skb_grow_rcsum(skb, new_len);
2970 else if (new_len < skb->len)
2971 ret = bpf_skb_trim_rcsum(skb, new_len);
2972 if (!ret && skb_is_gso(skb))
2978 BPF_CALL_3(bpf_skb_change_tail, struct sk_buff *, skb, u32, new_len,
2981 int ret = __bpf_skb_change_tail(skb, new_len, flags);
2983 bpf_compute_data_pointers(skb);
2987 static const struct bpf_func_proto bpf_skb_change_tail_proto = {
2988 .func = bpf_skb_change_tail,
2990 .ret_type = RET_INTEGER,
2991 .arg1_type = ARG_PTR_TO_CTX,
2992 .arg2_type = ARG_ANYTHING,
2993 .arg3_type = ARG_ANYTHING,
2996 BPF_CALL_3(sk_skb_change_tail, struct sk_buff *, skb, u32, new_len,
2999 int ret = __bpf_skb_change_tail(skb, new_len, flags);
3001 bpf_compute_data_end_sk_skb(skb);
3005 static const struct bpf_func_proto sk_skb_change_tail_proto = {
3006 .func = sk_skb_change_tail,
3008 .ret_type = RET_INTEGER,
3009 .arg1_type = ARG_PTR_TO_CTX,
3010 .arg2_type = ARG_ANYTHING,
3011 .arg3_type = ARG_ANYTHING,
3014 static inline int __bpf_skb_change_head(struct sk_buff *skb, u32 head_room,
3017 u32 max_len = __bpf_skb_max_len(skb);
3018 u32 new_len = skb->len + head_room;
3021 if (unlikely(flags || (!skb_is_gso(skb) && new_len > max_len) ||
3022 new_len < skb->len))
3025 ret = skb_cow(skb, head_room);
3027 /* Idea for this helper is that we currently only
3028 * allow to expand on mac header. This means that
3029 * skb->protocol network header, etc, stay as is.
3030 * Compared to bpf_skb_change_tail(), we're more
3031 * flexible due to not needing to linearize or
3032 * reset GSO. Intention for this helper is to be
3033 * used by an L3 skb that needs to push mac header
3034 * for redirection into L2 device.
3036 __skb_push(skb, head_room);
3037 memset(skb->data, 0, head_room);
3038 skb_reset_mac_header(skb);
3044 BPF_CALL_3(bpf_skb_change_head, struct sk_buff *, skb, u32, head_room,
3047 int ret = __bpf_skb_change_head(skb, head_room, flags);
3049 bpf_compute_data_pointers(skb);
3053 static const struct bpf_func_proto bpf_skb_change_head_proto = {
3054 .func = bpf_skb_change_head,
3056 .ret_type = RET_INTEGER,
3057 .arg1_type = ARG_PTR_TO_CTX,
3058 .arg2_type = ARG_ANYTHING,
3059 .arg3_type = ARG_ANYTHING,
3062 BPF_CALL_3(sk_skb_change_head, struct sk_buff *, skb, u32, head_room,
3065 int ret = __bpf_skb_change_head(skb, head_room, flags);
3067 bpf_compute_data_end_sk_skb(skb);
3071 static const struct bpf_func_proto sk_skb_change_head_proto = {
3072 .func = sk_skb_change_head,
3074 .ret_type = RET_INTEGER,
3075 .arg1_type = ARG_PTR_TO_CTX,
3076 .arg2_type = ARG_ANYTHING,
3077 .arg3_type = ARG_ANYTHING,
3079 static unsigned long xdp_get_metalen(const struct xdp_buff *xdp)
3081 return xdp_data_meta_unsupported(xdp) ? 0 :
3082 xdp->data - xdp->data_meta;
3085 BPF_CALL_2(bpf_xdp_adjust_head, struct xdp_buff *, xdp, int, offset)
3087 void *xdp_frame_end = xdp->data_hard_start + sizeof(struct xdp_frame);
3088 unsigned long metalen = xdp_get_metalen(xdp);
3089 void *data_start = xdp_frame_end + metalen;
3090 void *data = xdp->data + offset;
3092 if (unlikely(data < data_start ||
3093 data > xdp->data_end - ETH_HLEN))
3097 memmove(xdp->data_meta + offset,
3098 xdp->data_meta, metalen);
3099 xdp->data_meta += offset;
3105 static const struct bpf_func_proto bpf_xdp_adjust_head_proto = {
3106 .func = bpf_xdp_adjust_head,
3108 .ret_type = RET_INTEGER,
3109 .arg1_type = ARG_PTR_TO_CTX,
3110 .arg2_type = ARG_ANYTHING,
3113 BPF_CALL_2(bpf_xdp_adjust_tail, struct xdp_buff *, xdp, int, offset)
3115 void *data_end = xdp->data_end + offset;
3117 /* only shrinking is allowed for now. */
3118 if (unlikely(offset >= 0))
3121 if (unlikely(data_end < xdp->data + ETH_HLEN))
3124 xdp->data_end = data_end;
3129 static const struct bpf_func_proto bpf_xdp_adjust_tail_proto = {
3130 .func = bpf_xdp_adjust_tail,
3132 .ret_type = RET_INTEGER,
3133 .arg1_type = ARG_PTR_TO_CTX,
3134 .arg2_type = ARG_ANYTHING,
3137 BPF_CALL_2(bpf_xdp_adjust_meta, struct xdp_buff *, xdp, int, offset)
3139 void *xdp_frame_end = xdp->data_hard_start + sizeof(struct xdp_frame);
3140 void *meta = xdp->data_meta + offset;
3141 unsigned long metalen = xdp->data - meta;
3143 if (xdp_data_meta_unsupported(xdp))
3145 if (unlikely(meta < xdp_frame_end ||
3148 if (unlikely((metalen & (sizeof(__u32) - 1)) ||
3152 xdp->data_meta = meta;
3157 static const struct bpf_func_proto bpf_xdp_adjust_meta_proto = {
3158 .func = bpf_xdp_adjust_meta,
3160 .ret_type = RET_INTEGER,
3161 .arg1_type = ARG_PTR_TO_CTX,
3162 .arg2_type = ARG_ANYTHING,
3165 static int __bpf_tx_xdp(struct net_device *dev,
3166 struct bpf_map *map,
3167 struct xdp_buff *xdp,
3170 struct xdp_frame *xdpf;
3173 if (!dev->netdev_ops->ndo_xdp_xmit) {
3177 err = xdp_ok_fwd_dev(dev, xdp->data_end - xdp->data);
3181 xdpf = convert_to_xdp_frame(xdp);
3182 if (unlikely(!xdpf))
3185 sent = dev->netdev_ops->ndo_xdp_xmit(dev, 1, &xdpf, XDP_XMIT_FLUSH);
3192 xdp_do_redirect_slow(struct net_device *dev, struct xdp_buff *xdp,
3193 struct bpf_prog *xdp_prog, struct bpf_redirect_info *ri)
3195 struct net_device *fwd;
3196 u32 index = ri->ifindex;
3199 fwd = dev_get_by_index_rcu(dev_net(dev), index);
3201 if (unlikely(!fwd)) {
3206 err = __bpf_tx_xdp(fwd, NULL, xdp, 0);
3210 _trace_xdp_redirect(dev, xdp_prog, index);
3213 _trace_xdp_redirect_err(dev, xdp_prog, index, err);
3217 static int __bpf_tx_xdp_map(struct net_device *dev_rx, void *fwd,
3218 struct bpf_map *map,
3219 struct xdp_buff *xdp,
3224 switch (map->map_type) {
3225 case BPF_MAP_TYPE_DEVMAP: {
3226 struct bpf_dtab_netdev *dst = fwd;
3228 err = dev_map_enqueue(dst, xdp, dev_rx);
3231 __dev_map_insert_ctx(map, index);
3234 case BPF_MAP_TYPE_CPUMAP: {
3235 struct bpf_cpu_map_entry *rcpu = fwd;
3237 err = cpu_map_enqueue(rcpu, xdp, dev_rx);
3240 __cpu_map_insert_ctx(map, index);
3243 case BPF_MAP_TYPE_XSKMAP: {
3244 struct xdp_sock *xs = fwd;
3246 err = __xsk_map_redirect(map, xdp, xs);
3255 void xdp_do_flush_map(void)
3257 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
3258 struct bpf_map *map = ri->map_to_flush;
3260 ri->map_to_flush = NULL;
3262 switch (map->map_type) {
3263 case BPF_MAP_TYPE_DEVMAP:
3264 __dev_map_flush(map);
3266 case BPF_MAP_TYPE_CPUMAP:
3267 __cpu_map_flush(map);
3269 case BPF_MAP_TYPE_XSKMAP:
3270 __xsk_map_flush(map);
3277 EXPORT_SYMBOL_GPL(xdp_do_flush_map);
3279 static inline void *__xdp_map_lookup_elem(struct bpf_map *map, u32 index)
3281 switch (map->map_type) {
3282 case BPF_MAP_TYPE_DEVMAP:
3283 return __dev_map_lookup_elem(map, index);
3284 case BPF_MAP_TYPE_CPUMAP:
3285 return __cpu_map_lookup_elem(map, index);
3286 case BPF_MAP_TYPE_XSKMAP:
3287 return __xsk_map_lookup_elem(map, index);
3293 void bpf_clear_redirect_map(struct bpf_map *map)
3295 struct bpf_redirect_info *ri;
3298 for_each_possible_cpu(cpu) {
3299 ri = per_cpu_ptr(&bpf_redirect_info, cpu);
3300 /* Avoid polluting remote cacheline due to writes if
3301 * not needed. Once we pass this test, we need the
3302 * cmpxchg() to make sure it hasn't been changed in
3303 * the meantime by remote CPU.
3305 if (unlikely(READ_ONCE(ri->map) == map))
3306 cmpxchg(&ri->map, map, NULL);
3310 static int xdp_do_redirect_map(struct net_device *dev, struct xdp_buff *xdp,
3311 struct bpf_prog *xdp_prog, struct bpf_map *map,
3312 struct bpf_redirect_info *ri)
3314 u32 index = ri->ifindex;
3319 WRITE_ONCE(ri->map, NULL);
3321 fwd = __xdp_map_lookup_elem(map, index);
3322 if (unlikely(!fwd)) {
3326 if (ri->map_to_flush && unlikely(ri->map_to_flush != map))
3329 err = __bpf_tx_xdp_map(dev, fwd, map, xdp, index);
3333 ri->map_to_flush = map;
3334 _trace_xdp_redirect_map(dev, xdp_prog, fwd, map, index);
3337 _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map, index, err);
3341 int xdp_do_redirect(struct net_device *dev, struct xdp_buff *xdp,
3342 struct bpf_prog *xdp_prog)
3344 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
3345 struct bpf_map *map = READ_ONCE(ri->map);
3348 return xdp_do_redirect_map(dev, xdp, xdp_prog, map, ri);
3350 return xdp_do_redirect_slow(dev, xdp, xdp_prog, ri);
3352 EXPORT_SYMBOL_GPL(xdp_do_redirect);
3354 static int xdp_do_generic_redirect_map(struct net_device *dev,
3355 struct sk_buff *skb,
3356 struct xdp_buff *xdp,
3357 struct bpf_prog *xdp_prog,
3358 struct bpf_map *map)
3360 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
3361 u32 index = ri->ifindex;
3366 WRITE_ONCE(ri->map, NULL);
3368 fwd = __xdp_map_lookup_elem(map, index);
3369 if (unlikely(!fwd)) {
3374 if (map->map_type == BPF_MAP_TYPE_DEVMAP) {
3375 struct bpf_dtab_netdev *dst = fwd;
3377 err = dev_map_generic_redirect(dst, skb, xdp_prog);
3380 } else if (map->map_type == BPF_MAP_TYPE_XSKMAP) {
3381 struct xdp_sock *xs = fwd;
3383 err = xsk_generic_rcv(xs, xdp);
3388 /* TODO: Handle BPF_MAP_TYPE_CPUMAP */
3393 _trace_xdp_redirect_map(dev, xdp_prog, fwd, map, index);
3396 _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map, index, err);
3400 int xdp_do_generic_redirect(struct net_device *dev, struct sk_buff *skb,
3401 struct xdp_buff *xdp, struct bpf_prog *xdp_prog)
3403 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
3404 struct bpf_map *map = READ_ONCE(ri->map);
3405 u32 index = ri->ifindex;
3406 struct net_device *fwd;
3410 return xdp_do_generic_redirect_map(dev, skb, xdp, xdp_prog,
3413 fwd = dev_get_by_index_rcu(dev_net(dev), index);
3414 if (unlikely(!fwd)) {
3419 err = xdp_ok_fwd_dev(fwd, skb->len);
3424 _trace_xdp_redirect(dev, xdp_prog, index);
3425 generic_xdp_tx(skb, xdp_prog);
3428 _trace_xdp_redirect_err(dev, xdp_prog, index, err);
3431 EXPORT_SYMBOL_GPL(xdp_do_generic_redirect);
3433 BPF_CALL_2(bpf_xdp_redirect, u32, ifindex, u64, flags)
3435 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
3437 if (unlikely(flags))
3440 ri->ifindex = ifindex;
3442 WRITE_ONCE(ri->map, NULL);
3444 return XDP_REDIRECT;
3447 static const struct bpf_func_proto bpf_xdp_redirect_proto = {
3448 .func = bpf_xdp_redirect,
3450 .ret_type = RET_INTEGER,
3451 .arg1_type = ARG_ANYTHING,
3452 .arg2_type = ARG_ANYTHING,
3455 BPF_CALL_3(bpf_xdp_redirect_map, struct bpf_map *, map, u32, ifindex,
3458 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
3460 if (unlikely(flags))
3463 ri->ifindex = ifindex;
3465 WRITE_ONCE(ri->map, map);
3467 return XDP_REDIRECT;
3470 static const struct bpf_func_proto bpf_xdp_redirect_map_proto = {
3471 .func = bpf_xdp_redirect_map,
3473 .ret_type = RET_INTEGER,
3474 .arg1_type = ARG_CONST_MAP_PTR,
3475 .arg2_type = ARG_ANYTHING,
3476 .arg3_type = ARG_ANYTHING,
3479 static unsigned long bpf_skb_copy(void *dst_buff, const void *skb,
3480 unsigned long off, unsigned long len)
3482 void *ptr = skb_header_pointer(skb, off, len, dst_buff);
3486 if (ptr != dst_buff)
3487 memcpy(dst_buff, ptr, len);
3492 BPF_CALL_5(bpf_skb_event_output, struct sk_buff *, skb, struct bpf_map *, map,
3493 u64, flags, void *, meta, u64, meta_size)
3495 u64 skb_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
3497 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
3499 if (unlikely(skb_size > skb->len))
3502 return bpf_event_output(map, flags, meta, meta_size, skb, skb_size,
3506 static const struct bpf_func_proto bpf_skb_event_output_proto = {
3507 .func = bpf_skb_event_output,
3509 .ret_type = RET_INTEGER,
3510 .arg1_type = ARG_PTR_TO_CTX,
3511 .arg2_type = ARG_CONST_MAP_PTR,
3512 .arg3_type = ARG_ANYTHING,
3513 .arg4_type = ARG_PTR_TO_MEM,
3514 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
3517 static unsigned short bpf_tunnel_key_af(u64 flags)
3519 return flags & BPF_F_TUNINFO_IPV6 ? AF_INET6 : AF_INET;
3522 BPF_CALL_4(bpf_skb_get_tunnel_key, struct sk_buff *, skb, struct bpf_tunnel_key *, to,
3523 u32, size, u64, flags)
3525 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
3526 u8 compat[sizeof(struct bpf_tunnel_key)];
3530 if (unlikely(!info || (flags & ~(BPF_F_TUNINFO_IPV6)))) {
3534 if (ip_tunnel_info_af(info) != bpf_tunnel_key_af(flags)) {
3538 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
3541 case offsetof(struct bpf_tunnel_key, tunnel_label):
3542 case offsetof(struct bpf_tunnel_key, tunnel_ext):
3544 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
3545 /* Fixup deprecated structure layouts here, so we have
3546 * a common path later on.
3548 if (ip_tunnel_info_af(info) != AF_INET)
3551 to = (struct bpf_tunnel_key *)compat;
3558 to->tunnel_id = be64_to_cpu(info->key.tun_id);
3559 to->tunnel_tos = info->key.tos;
3560 to->tunnel_ttl = info->key.ttl;
3563 if (flags & BPF_F_TUNINFO_IPV6) {
3564 memcpy(to->remote_ipv6, &info->key.u.ipv6.src,
3565 sizeof(to->remote_ipv6));
3566 to->tunnel_label = be32_to_cpu(info->key.label);
3568 to->remote_ipv4 = be32_to_cpu(info->key.u.ipv4.src);
3569 memset(&to->remote_ipv6[1], 0, sizeof(__u32) * 3);
3570 to->tunnel_label = 0;
3573 if (unlikely(size != sizeof(struct bpf_tunnel_key)))
3574 memcpy(to_orig, to, size);
3578 memset(to_orig, 0, size);
3582 static const struct bpf_func_proto bpf_skb_get_tunnel_key_proto = {
3583 .func = bpf_skb_get_tunnel_key,
3585 .ret_type = RET_INTEGER,
3586 .arg1_type = ARG_PTR_TO_CTX,
3587 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
3588 .arg3_type = ARG_CONST_SIZE,
3589 .arg4_type = ARG_ANYTHING,
3592 BPF_CALL_3(bpf_skb_get_tunnel_opt, struct sk_buff *, skb, u8 *, to, u32, size)
3594 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
3597 if (unlikely(!info ||
3598 !(info->key.tun_flags & TUNNEL_OPTIONS_PRESENT))) {
3602 if (unlikely(size < info->options_len)) {
3607 ip_tunnel_info_opts_get(to, info);
3608 if (size > info->options_len)
3609 memset(to + info->options_len, 0, size - info->options_len);
3611 return info->options_len;
3613 memset(to, 0, size);
3617 static const struct bpf_func_proto bpf_skb_get_tunnel_opt_proto = {
3618 .func = bpf_skb_get_tunnel_opt,
3620 .ret_type = RET_INTEGER,
3621 .arg1_type = ARG_PTR_TO_CTX,
3622 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
3623 .arg3_type = ARG_CONST_SIZE,
3626 static struct metadata_dst __percpu *md_dst;
3628 BPF_CALL_4(bpf_skb_set_tunnel_key, struct sk_buff *, skb,
3629 const struct bpf_tunnel_key *, from, u32, size, u64, flags)
3631 struct metadata_dst *md = this_cpu_ptr(md_dst);
3632 u8 compat[sizeof(struct bpf_tunnel_key)];
3633 struct ip_tunnel_info *info;
3635 if (unlikely(flags & ~(BPF_F_TUNINFO_IPV6 | BPF_F_ZERO_CSUM_TX |
3636 BPF_F_DONT_FRAGMENT | BPF_F_SEQ_NUMBER)))
3638 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
3640 case offsetof(struct bpf_tunnel_key, tunnel_label):
3641 case offsetof(struct bpf_tunnel_key, tunnel_ext):
3642 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
3643 /* Fixup deprecated structure layouts here, so we have
3644 * a common path later on.
3646 memcpy(compat, from, size);
3647 memset(compat + size, 0, sizeof(compat) - size);
3648 from = (const struct bpf_tunnel_key *) compat;
3654 if (unlikely((!(flags & BPF_F_TUNINFO_IPV6) && from->tunnel_label) ||
3659 dst_hold((struct dst_entry *) md);
3660 skb_dst_set(skb, (struct dst_entry *) md);
3662 info = &md->u.tun_info;
3663 memset(info, 0, sizeof(*info));
3664 info->mode = IP_TUNNEL_INFO_TX;
3666 info->key.tun_flags = TUNNEL_KEY | TUNNEL_CSUM | TUNNEL_NOCACHE;
3667 if (flags & BPF_F_DONT_FRAGMENT)
3668 info->key.tun_flags |= TUNNEL_DONT_FRAGMENT;
3669 if (flags & BPF_F_ZERO_CSUM_TX)
3670 info->key.tun_flags &= ~TUNNEL_CSUM;
3671 if (flags & BPF_F_SEQ_NUMBER)
3672 info->key.tun_flags |= TUNNEL_SEQ;
3674 info->key.tun_id = cpu_to_be64(from->tunnel_id);
3675 info->key.tos = from->tunnel_tos;
3676 info->key.ttl = from->tunnel_ttl;
3678 if (flags & BPF_F_TUNINFO_IPV6) {
3679 info->mode |= IP_TUNNEL_INFO_IPV6;
3680 memcpy(&info->key.u.ipv6.dst, from->remote_ipv6,
3681 sizeof(from->remote_ipv6));
3682 info->key.label = cpu_to_be32(from->tunnel_label) &
3683 IPV6_FLOWLABEL_MASK;
3685 info->key.u.ipv4.dst = cpu_to_be32(from->remote_ipv4);
3691 static const struct bpf_func_proto bpf_skb_set_tunnel_key_proto = {
3692 .func = bpf_skb_set_tunnel_key,
3694 .ret_type = RET_INTEGER,
3695 .arg1_type = ARG_PTR_TO_CTX,
3696 .arg2_type = ARG_PTR_TO_MEM,
3697 .arg3_type = ARG_CONST_SIZE,
3698 .arg4_type = ARG_ANYTHING,
3701 BPF_CALL_3(bpf_skb_set_tunnel_opt, struct sk_buff *, skb,
3702 const u8 *, from, u32, size)
3704 struct ip_tunnel_info *info = skb_tunnel_info(skb);
3705 const struct metadata_dst *md = this_cpu_ptr(md_dst);
3707 if (unlikely(info != &md->u.tun_info || (size & (sizeof(u32) - 1))))
3709 if (unlikely(size > IP_TUNNEL_OPTS_MAX))
3712 ip_tunnel_info_opts_set(info, from, size, TUNNEL_OPTIONS_PRESENT);
3717 static const struct bpf_func_proto bpf_skb_set_tunnel_opt_proto = {
3718 .func = bpf_skb_set_tunnel_opt,
3720 .ret_type = RET_INTEGER,
3721 .arg1_type = ARG_PTR_TO_CTX,
3722 .arg2_type = ARG_PTR_TO_MEM,
3723 .arg3_type = ARG_CONST_SIZE,
3726 static const struct bpf_func_proto *
3727 bpf_get_skb_set_tunnel_proto(enum bpf_func_id which)
3730 struct metadata_dst __percpu *tmp;
3732 tmp = metadata_dst_alloc_percpu(IP_TUNNEL_OPTS_MAX,
3737 if (cmpxchg(&md_dst, NULL, tmp))
3738 metadata_dst_free_percpu(tmp);
3742 case BPF_FUNC_skb_set_tunnel_key:
3743 return &bpf_skb_set_tunnel_key_proto;
3744 case BPF_FUNC_skb_set_tunnel_opt:
3745 return &bpf_skb_set_tunnel_opt_proto;
3751 BPF_CALL_3(bpf_skb_under_cgroup, struct sk_buff *, skb, struct bpf_map *, map,
3754 struct bpf_array *array = container_of(map, struct bpf_array, map);
3755 struct cgroup *cgrp;
3758 sk = skb_to_full_sk(skb);
3759 if (!sk || !sk_fullsock(sk))
3761 if (unlikely(idx >= array->map.max_entries))
3764 cgrp = READ_ONCE(array->ptrs[idx]);
3765 if (unlikely(!cgrp))
3768 return sk_under_cgroup_hierarchy(sk, cgrp);
3771 static const struct bpf_func_proto bpf_skb_under_cgroup_proto = {
3772 .func = bpf_skb_under_cgroup,
3774 .ret_type = RET_INTEGER,
3775 .arg1_type = ARG_PTR_TO_CTX,
3776 .arg2_type = ARG_CONST_MAP_PTR,
3777 .arg3_type = ARG_ANYTHING,
3780 #ifdef CONFIG_SOCK_CGROUP_DATA
3781 BPF_CALL_1(bpf_skb_cgroup_id, const struct sk_buff *, skb)
3783 struct sock *sk = skb_to_full_sk(skb);
3784 struct cgroup *cgrp;
3786 if (!sk || !sk_fullsock(sk))
3789 cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
3790 return cgrp->kn->id.id;
3793 static const struct bpf_func_proto bpf_skb_cgroup_id_proto = {
3794 .func = bpf_skb_cgroup_id,
3796 .ret_type = RET_INTEGER,
3797 .arg1_type = ARG_PTR_TO_CTX,
3800 BPF_CALL_2(bpf_skb_ancestor_cgroup_id, const struct sk_buff *, skb, int,
3803 struct sock *sk = skb_to_full_sk(skb);
3804 struct cgroup *ancestor;
3805 struct cgroup *cgrp;
3807 if (!sk || !sk_fullsock(sk))
3810 cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
3811 ancestor = cgroup_ancestor(cgrp, ancestor_level);
3815 return ancestor->kn->id.id;
3818 static const struct bpf_func_proto bpf_skb_ancestor_cgroup_id_proto = {
3819 .func = bpf_skb_ancestor_cgroup_id,
3821 .ret_type = RET_INTEGER,
3822 .arg1_type = ARG_PTR_TO_CTX,
3823 .arg2_type = ARG_ANYTHING,
3827 static unsigned long bpf_xdp_copy(void *dst_buff, const void *src_buff,
3828 unsigned long off, unsigned long len)
3830 memcpy(dst_buff, src_buff + off, len);
3834 BPF_CALL_5(bpf_xdp_event_output, struct xdp_buff *, xdp, struct bpf_map *, map,
3835 u64, flags, void *, meta, u64, meta_size)
3837 u64 xdp_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
3839 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
3841 if (unlikely(xdp_size > (unsigned long)(xdp->data_end - xdp->data)))
3844 return bpf_event_output(map, flags, meta, meta_size, xdp->data,
3845 xdp_size, bpf_xdp_copy);
3848 static const struct bpf_func_proto bpf_xdp_event_output_proto = {
3849 .func = bpf_xdp_event_output,
3851 .ret_type = RET_INTEGER,
3852 .arg1_type = ARG_PTR_TO_CTX,
3853 .arg2_type = ARG_CONST_MAP_PTR,
3854 .arg3_type = ARG_ANYTHING,
3855 .arg4_type = ARG_PTR_TO_MEM,
3856 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
3859 BPF_CALL_1(bpf_get_socket_cookie, struct sk_buff *, skb)
3861 return skb->sk ? sock_gen_cookie(skb->sk) : 0;
3864 static const struct bpf_func_proto bpf_get_socket_cookie_proto = {
3865 .func = bpf_get_socket_cookie,
3867 .ret_type = RET_INTEGER,
3868 .arg1_type = ARG_PTR_TO_CTX,
3871 BPF_CALL_1(bpf_get_socket_cookie_sock_addr, struct bpf_sock_addr_kern *, ctx)
3873 return sock_gen_cookie(ctx->sk);
3876 static const struct bpf_func_proto bpf_get_socket_cookie_sock_addr_proto = {
3877 .func = bpf_get_socket_cookie_sock_addr,
3879 .ret_type = RET_INTEGER,
3880 .arg1_type = ARG_PTR_TO_CTX,
3883 BPF_CALL_1(bpf_get_socket_cookie_sock_ops, struct bpf_sock_ops_kern *, ctx)
3885 return sock_gen_cookie(ctx->sk);
3888 static const struct bpf_func_proto bpf_get_socket_cookie_sock_ops_proto = {
3889 .func = bpf_get_socket_cookie_sock_ops,
3891 .ret_type = RET_INTEGER,
3892 .arg1_type = ARG_PTR_TO_CTX,
3895 BPF_CALL_1(bpf_get_socket_uid, struct sk_buff *, skb)
3897 struct sock *sk = sk_to_full_sk(skb->sk);
3900 if (!sk || !sk_fullsock(sk))
3902 kuid = sock_net_uid(sock_net(sk), sk);
3903 return from_kuid_munged(sock_net(sk)->user_ns, kuid);
3906 static const struct bpf_func_proto bpf_get_socket_uid_proto = {
3907 .func = bpf_get_socket_uid,
3909 .ret_type = RET_INTEGER,
3910 .arg1_type = ARG_PTR_TO_CTX,
3913 BPF_CALL_5(bpf_setsockopt, struct bpf_sock_ops_kern *, bpf_sock,
3914 int, level, int, optname, char *, optval, int, optlen)
3916 struct sock *sk = bpf_sock->sk;
3920 if (!sk_fullsock(sk))
3923 if (level == SOL_SOCKET) {
3924 if (optlen != sizeof(int))
3926 val = *((int *)optval);
3928 /* Only some socketops are supported */
3931 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
3932 sk->sk_rcvbuf = max_t(int, val * 2, SOCK_MIN_RCVBUF);
3935 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
3936 sk->sk_sndbuf = max_t(int, val * 2, SOCK_MIN_SNDBUF);
3938 case SO_MAX_PACING_RATE: /* 32bit version */
3939 sk->sk_max_pacing_rate = (val == ~0U) ? ~0UL : val;
3940 sk->sk_pacing_rate = min(sk->sk_pacing_rate,
3941 sk->sk_max_pacing_rate);
3944 sk->sk_priority = val;
3949 sk->sk_rcvlowat = val ? : 1;
3958 } else if (level == SOL_IP) {
3959 if (optlen != sizeof(int) || sk->sk_family != AF_INET)
3962 val = *((int *)optval);
3963 /* Only some options are supported */
3966 if (val < -1 || val > 0xff) {
3969 struct inet_sock *inet = inet_sk(sk);
3979 #if IS_ENABLED(CONFIG_IPV6)
3980 } else if (level == SOL_IPV6) {
3981 if (optlen != sizeof(int) || sk->sk_family != AF_INET6)
3984 val = *((int *)optval);
3985 /* Only some options are supported */
3988 if (val < -1 || val > 0xff) {
3991 struct ipv6_pinfo *np = inet6_sk(sk);
4002 } else if (level == SOL_TCP &&
4003 sk->sk_prot->setsockopt == tcp_setsockopt) {
4004 if (optname == TCP_CONGESTION) {
4005 char name[TCP_CA_NAME_MAX];
4006 bool reinit = bpf_sock->op > BPF_SOCK_OPS_NEEDS_ECN;
4008 strncpy(name, optval, min_t(long, optlen,
4009 TCP_CA_NAME_MAX-1));
4010 name[TCP_CA_NAME_MAX-1] = 0;
4011 ret = tcp_set_congestion_control(sk, name, false,
4014 struct tcp_sock *tp = tcp_sk(sk);
4016 if (optlen != sizeof(int))
4019 val = *((int *)optval);
4020 /* Only some options are supported */
4023 if (val <= 0 || tp->data_segs_out > 0)
4028 case TCP_BPF_SNDCWND_CLAMP:
4032 tp->snd_cwnd_clamp = val;
4033 tp->snd_ssthresh = val;
4037 if (val < 0 || val > 1)
4053 static const struct bpf_func_proto bpf_setsockopt_proto = {
4054 .func = bpf_setsockopt,
4056 .ret_type = RET_INTEGER,
4057 .arg1_type = ARG_PTR_TO_CTX,
4058 .arg2_type = ARG_ANYTHING,
4059 .arg3_type = ARG_ANYTHING,
4060 .arg4_type = ARG_PTR_TO_MEM,
4061 .arg5_type = ARG_CONST_SIZE,
4064 BPF_CALL_5(bpf_getsockopt, struct bpf_sock_ops_kern *, bpf_sock,
4065 int, level, int, optname, char *, optval, int, optlen)
4067 struct sock *sk = bpf_sock->sk;
4069 if (!sk_fullsock(sk))
4072 if (level == SOL_TCP && sk->sk_prot->getsockopt == tcp_getsockopt) {
4073 struct inet_connection_sock *icsk;
4074 struct tcp_sock *tp;
4077 case TCP_CONGESTION:
4078 icsk = inet_csk(sk);
4080 if (!icsk->icsk_ca_ops || optlen <= 1)
4082 strncpy(optval, icsk->icsk_ca_ops->name, optlen);
4083 optval[optlen - 1] = 0;
4088 if (optlen <= 0 || !tp->saved_syn ||
4089 optlen > tp->saved_syn[0])
4091 memcpy(optval, tp->saved_syn + 1, optlen);
4096 } else if (level == SOL_IP) {
4097 struct inet_sock *inet = inet_sk(sk);
4099 if (optlen != sizeof(int) || sk->sk_family != AF_INET)
4102 /* Only some options are supported */
4105 *((int *)optval) = (int)inet->tos;
4110 #if IS_ENABLED(CONFIG_IPV6)
4111 } else if (level == SOL_IPV6) {
4112 struct ipv6_pinfo *np = inet6_sk(sk);
4114 if (optlen != sizeof(int) || sk->sk_family != AF_INET6)
4117 /* Only some options are supported */
4120 *((int *)optval) = (int)np->tclass;
4132 memset(optval, 0, optlen);
4136 static const struct bpf_func_proto bpf_getsockopt_proto = {
4137 .func = bpf_getsockopt,
4139 .ret_type = RET_INTEGER,
4140 .arg1_type = ARG_PTR_TO_CTX,
4141 .arg2_type = ARG_ANYTHING,
4142 .arg3_type = ARG_ANYTHING,
4143 .arg4_type = ARG_PTR_TO_UNINIT_MEM,
4144 .arg5_type = ARG_CONST_SIZE,
4147 BPF_CALL_2(bpf_sock_ops_cb_flags_set, struct bpf_sock_ops_kern *, bpf_sock,
4150 struct sock *sk = bpf_sock->sk;
4151 int val = argval & BPF_SOCK_OPS_ALL_CB_FLAGS;
4153 if (!IS_ENABLED(CONFIG_INET) || !sk_fullsock(sk))
4157 tcp_sk(sk)->bpf_sock_ops_cb_flags = val;
4159 return argval & (~BPF_SOCK_OPS_ALL_CB_FLAGS);
4162 static const struct bpf_func_proto bpf_sock_ops_cb_flags_set_proto = {
4163 .func = bpf_sock_ops_cb_flags_set,
4165 .ret_type = RET_INTEGER,
4166 .arg1_type = ARG_PTR_TO_CTX,
4167 .arg2_type = ARG_ANYTHING,
4170 const struct ipv6_bpf_stub *ipv6_bpf_stub __read_mostly;
4171 EXPORT_SYMBOL_GPL(ipv6_bpf_stub);
4173 BPF_CALL_3(bpf_bind, struct bpf_sock_addr_kern *, ctx, struct sockaddr *, addr,
4177 struct sock *sk = ctx->sk;
4180 /* Binding to port can be expensive so it's prohibited in the helper.
4181 * Only binding to IP is supported.
4184 if (addr->sa_family == AF_INET) {
4185 if (addr_len < sizeof(struct sockaddr_in))
4187 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
4189 return __inet_bind(sk, addr, addr_len, true, false);
4190 #if IS_ENABLED(CONFIG_IPV6)
4191 } else if (addr->sa_family == AF_INET6) {
4192 if (addr_len < SIN6_LEN_RFC2133)
4194 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
4196 /* ipv6_bpf_stub cannot be NULL, since it's called from
4197 * bpf_cgroup_inet6_connect hook and ipv6 is already loaded
4199 return ipv6_bpf_stub->inet6_bind(sk, addr, addr_len, true, false);
4200 #endif /* CONFIG_IPV6 */
4202 #endif /* CONFIG_INET */
4204 return -EAFNOSUPPORT;
4207 static const struct bpf_func_proto bpf_bind_proto = {
4210 .ret_type = RET_INTEGER,
4211 .arg1_type = ARG_PTR_TO_CTX,
4212 .arg2_type = ARG_PTR_TO_MEM,
4213 .arg3_type = ARG_CONST_SIZE,
4217 BPF_CALL_5(bpf_skb_get_xfrm_state, struct sk_buff *, skb, u32, index,
4218 struct bpf_xfrm_state *, to, u32, size, u64, flags)
4220 const struct sec_path *sp = skb_sec_path(skb);
4221 const struct xfrm_state *x;
4223 if (!sp || unlikely(index >= sp->len || flags))
4226 x = sp->xvec[index];
4228 if (unlikely(size != sizeof(struct bpf_xfrm_state)))
4231 to->reqid = x->props.reqid;
4232 to->spi = x->id.spi;
4233 to->family = x->props.family;
4236 if (to->family == AF_INET6) {
4237 memcpy(to->remote_ipv6, x->props.saddr.a6,
4238 sizeof(to->remote_ipv6));
4240 to->remote_ipv4 = x->props.saddr.a4;
4241 memset(&to->remote_ipv6[1], 0, sizeof(__u32) * 3);
4246 memset(to, 0, size);
4250 static const struct bpf_func_proto bpf_skb_get_xfrm_state_proto = {
4251 .func = bpf_skb_get_xfrm_state,
4253 .ret_type = RET_INTEGER,
4254 .arg1_type = ARG_PTR_TO_CTX,
4255 .arg2_type = ARG_ANYTHING,
4256 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
4257 .arg4_type = ARG_CONST_SIZE,
4258 .arg5_type = ARG_ANYTHING,
4262 #if IS_ENABLED(CONFIG_INET) || IS_ENABLED(CONFIG_IPV6)
4263 static int bpf_fib_set_fwd_params(struct bpf_fib_lookup *params,
4264 const struct neighbour *neigh,
4265 const struct net_device *dev)
4267 memcpy(params->dmac, neigh->ha, ETH_ALEN);
4268 memcpy(params->smac, dev->dev_addr, ETH_ALEN);
4269 params->h_vlan_TCI = 0;
4270 params->h_vlan_proto = 0;
4271 params->ifindex = dev->ifindex;
4277 #if IS_ENABLED(CONFIG_INET)
4278 static int bpf_ipv4_fib_lookup(struct net *net, struct bpf_fib_lookup *params,
4279 u32 flags, bool check_mtu)
4281 struct in_device *in_dev;
4282 struct neighbour *neigh;
4283 struct net_device *dev;
4284 struct fib_result res;
4290 dev = dev_get_by_index_rcu(net, params->ifindex);
4294 /* verify forwarding is enabled on this interface */
4295 in_dev = __in_dev_get_rcu(dev);
4296 if (unlikely(!in_dev || !IN_DEV_FORWARD(in_dev)))
4297 return BPF_FIB_LKUP_RET_FWD_DISABLED;
4299 if (flags & BPF_FIB_LOOKUP_OUTPUT) {
4301 fl4.flowi4_oif = params->ifindex;
4303 fl4.flowi4_iif = params->ifindex;
4306 fl4.flowi4_tos = params->tos & IPTOS_RT_MASK;
4307 fl4.flowi4_scope = RT_SCOPE_UNIVERSE;
4308 fl4.flowi4_flags = 0;
4310 fl4.flowi4_proto = params->l4_protocol;
4311 fl4.daddr = params->ipv4_dst;
4312 fl4.saddr = params->ipv4_src;
4313 fl4.fl4_sport = params->sport;
4314 fl4.fl4_dport = params->dport;
4316 if (flags & BPF_FIB_LOOKUP_DIRECT) {
4317 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
4318 struct fib_table *tb;
4320 tb = fib_get_table(net, tbid);
4322 return BPF_FIB_LKUP_RET_NOT_FWDED;
4324 err = fib_table_lookup(tb, &fl4, &res, FIB_LOOKUP_NOREF);
4326 fl4.flowi4_mark = 0;
4327 fl4.flowi4_secid = 0;
4328 fl4.flowi4_tun_key.tun_id = 0;
4329 fl4.flowi4_uid = sock_net_uid(net, NULL);
4331 err = fib_lookup(net, &fl4, &res, FIB_LOOKUP_NOREF);
4335 /* map fib lookup errors to RTN_ type */
4337 return BPF_FIB_LKUP_RET_BLACKHOLE;
4338 if (err == -EHOSTUNREACH)
4339 return BPF_FIB_LKUP_RET_UNREACHABLE;
4341 return BPF_FIB_LKUP_RET_PROHIBIT;
4343 return BPF_FIB_LKUP_RET_NOT_FWDED;
4346 if (res.type != RTN_UNICAST)
4347 return BPF_FIB_LKUP_RET_NOT_FWDED;
4349 if (res.fi->fib_nhs > 1)
4350 fib_select_path(net, &res, &fl4, NULL);
4353 mtu = ip_mtu_from_fib_result(&res, params->ipv4_dst);
4354 if (params->tot_len > mtu)
4355 return BPF_FIB_LKUP_RET_FRAG_NEEDED;
4358 nh = &res.fi->fib_nh[res.nh_sel];
4360 /* do not handle lwt encaps right now */
4361 if (nh->nh_lwtstate)
4362 return BPF_FIB_LKUP_RET_UNSUPP_LWT;
4366 params->ipv4_dst = nh->nh_gw;
4368 params->rt_metric = res.fi->fib_priority;
4370 /* xdp and cls_bpf programs are run in RCU-bh so
4371 * rcu_read_lock_bh is not needed here
4373 neigh = __ipv4_neigh_lookup_noref(dev, (__force u32)params->ipv4_dst);
4375 return BPF_FIB_LKUP_RET_NO_NEIGH;
4377 return bpf_fib_set_fwd_params(params, neigh, dev);
4381 #if IS_ENABLED(CONFIG_IPV6)
4382 static int bpf_ipv6_fib_lookup(struct net *net, struct bpf_fib_lookup *params,
4383 u32 flags, bool check_mtu)
4385 struct in6_addr *src = (struct in6_addr *) params->ipv6_src;
4386 struct in6_addr *dst = (struct in6_addr *) params->ipv6_dst;
4387 struct neighbour *neigh;
4388 struct net_device *dev;
4389 struct inet6_dev *idev;
4390 struct fib6_info *f6i;
4396 /* link local addresses are never forwarded */
4397 if (rt6_need_strict(dst) || rt6_need_strict(src))
4398 return BPF_FIB_LKUP_RET_NOT_FWDED;
4400 dev = dev_get_by_index_rcu(net, params->ifindex);
4404 idev = __in6_dev_get_safely(dev);
4405 if (unlikely(!idev || !net->ipv6.devconf_all->forwarding))
4406 return BPF_FIB_LKUP_RET_FWD_DISABLED;
4408 if (flags & BPF_FIB_LOOKUP_OUTPUT) {
4410 oif = fl6.flowi6_oif = params->ifindex;
4412 oif = fl6.flowi6_iif = params->ifindex;
4414 strict = RT6_LOOKUP_F_HAS_SADDR;
4416 fl6.flowlabel = params->flowinfo;
4417 fl6.flowi6_scope = 0;
4418 fl6.flowi6_flags = 0;
4421 fl6.flowi6_proto = params->l4_protocol;
4424 fl6.fl6_sport = params->sport;
4425 fl6.fl6_dport = params->dport;
4427 if (flags & BPF_FIB_LOOKUP_DIRECT) {
4428 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
4429 struct fib6_table *tb;
4431 tb = ipv6_stub->fib6_get_table(net, tbid);
4433 return BPF_FIB_LKUP_RET_NOT_FWDED;
4435 f6i = ipv6_stub->fib6_table_lookup(net, tb, oif, &fl6, strict);
4437 fl6.flowi6_mark = 0;
4438 fl6.flowi6_secid = 0;
4439 fl6.flowi6_tun_key.tun_id = 0;
4440 fl6.flowi6_uid = sock_net_uid(net, NULL);
4442 f6i = ipv6_stub->fib6_lookup(net, oif, &fl6, strict);
4445 if (unlikely(IS_ERR_OR_NULL(f6i) || f6i == net->ipv6.fib6_null_entry))
4446 return BPF_FIB_LKUP_RET_NOT_FWDED;
4448 if (unlikely(f6i->fib6_flags & RTF_REJECT)) {
4449 switch (f6i->fib6_type) {
4451 return BPF_FIB_LKUP_RET_BLACKHOLE;
4452 case RTN_UNREACHABLE:
4453 return BPF_FIB_LKUP_RET_UNREACHABLE;
4455 return BPF_FIB_LKUP_RET_PROHIBIT;
4457 return BPF_FIB_LKUP_RET_NOT_FWDED;
4461 if (f6i->fib6_type != RTN_UNICAST)
4462 return BPF_FIB_LKUP_RET_NOT_FWDED;
4464 if (f6i->fib6_nsiblings && fl6.flowi6_oif == 0)
4465 f6i = ipv6_stub->fib6_multipath_select(net, f6i, &fl6,
4466 fl6.flowi6_oif, NULL,
4470 mtu = ipv6_stub->ip6_mtu_from_fib6(f6i, dst, src);
4471 if (params->tot_len > mtu)
4472 return BPF_FIB_LKUP_RET_FRAG_NEEDED;
4475 if (f6i->fib6_nh.nh_lwtstate)
4476 return BPF_FIB_LKUP_RET_UNSUPP_LWT;
4478 if (f6i->fib6_flags & RTF_GATEWAY)
4479 *dst = f6i->fib6_nh.nh_gw;
4481 dev = f6i->fib6_nh.nh_dev;
4482 params->rt_metric = f6i->fib6_metric;
4484 /* xdp and cls_bpf programs are run in RCU-bh so rcu_read_lock_bh is
4485 * not needed here. Can not use __ipv6_neigh_lookup_noref here
4486 * because we need to get nd_tbl via the stub
4488 neigh = ___neigh_lookup_noref(ipv6_stub->nd_tbl, neigh_key_eq128,
4489 ndisc_hashfn, dst, dev);
4491 return BPF_FIB_LKUP_RET_NO_NEIGH;
4493 return bpf_fib_set_fwd_params(params, neigh, dev);
4497 BPF_CALL_4(bpf_xdp_fib_lookup, struct xdp_buff *, ctx,
4498 struct bpf_fib_lookup *, params, int, plen, u32, flags)
4500 if (plen < sizeof(*params))
4503 if (flags & ~(BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_OUTPUT))
4506 switch (params->family) {
4507 #if IS_ENABLED(CONFIG_INET)
4509 return bpf_ipv4_fib_lookup(dev_net(ctx->rxq->dev), params,
4512 #if IS_ENABLED(CONFIG_IPV6)
4514 return bpf_ipv6_fib_lookup(dev_net(ctx->rxq->dev), params,
4518 return -EAFNOSUPPORT;
4521 static const struct bpf_func_proto bpf_xdp_fib_lookup_proto = {
4522 .func = bpf_xdp_fib_lookup,
4524 .ret_type = RET_INTEGER,
4525 .arg1_type = ARG_PTR_TO_CTX,
4526 .arg2_type = ARG_PTR_TO_MEM,
4527 .arg3_type = ARG_CONST_SIZE,
4528 .arg4_type = ARG_ANYTHING,
4531 BPF_CALL_4(bpf_skb_fib_lookup, struct sk_buff *, skb,
4532 struct bpf_fib_lookup *, params, int, plen, u32, flags)
4534 struct net *net = dev_net(skb->dev);
4535 int rc = -EAFNOSUPPORT;
4537 if (plen < sizeof(*params))
4540 if (flags & ~(BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_OUTPUT))
4543 switch (params->family) {
4544 #if IS_ENABLED(CONFIG_INET)
4546 rc = bpf_ipv4_fib_lookup(net, params, flags, false);
4549 #if IS_ENABLED(CONFIG_IPV6)
4551 rc = bpf_ipv6_fib_lookup(net, params, flags, false);
4557 struct net_device *dev;
4559 dev = dev_get_by_index_rcu(net, params->ifindex);
4560 if (!is_skb_forwardable(dev, skb))
4561 rc = BPF_FIB_LKUP_RET_FRAG_NEEDED;
4567 static const struct bpf_func_proto bpf_skb_fib_lookup_proto = {
4568 .func = bpf_skb_fib_lookup,
4570 .ret_type = RET_INTEGER,
4571 .arg1_type = ARG_PTR_TO_CTX,
4572 .arg2_type = ARG_PTR_TO_MEM,
4573 .arg3_type = ARG_CONST_SIZE,
4574 .arg4_type = ARG_ANYTHING,
4577 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
4578 static int bpf_push_seg6_encap(struct sk_buff *skb, u32 type, void *hdr, u32 len)
4581 struct ipv6_sr_hdr *srh = (struct ipv6_sr_hdr *)hdr;
4583 if (!seg6_validate_srh(srh, len))
4587 case BPF_LWT_ENCAP_SEG6_INLINE:
4588 if (skb->protocol != htons(ETH_P_IPV6))
4591 err = seg6_do_srh_inline(skb, srh);
4593 case BPF_LWT_ENCAP_SEG6:
4594 skb_reset_inner_headers(skb);
4595 skb->encapsulation = 1;
4596 err = seg6_do_srh_encap(skb, srh, IPPROTO_IPV6);
4602 bpf_compute_data_pointers(skb);
4606 ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
4607 skb_set_transport_header(skb, sizeof(struct ipv6hdr));
4609 return seg6_lookup_nexthop(skb, NULL, 0);
4611 #endif /* CONFIG_IPV6_SEG6_BPF */
4613 BPF_CALL_4(bpf_lwt_push_encap, struct sk_buff *, skb, u32, type, void *, hdr,
4617 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
4618 case BPF_LWT_ENCAP_SEG6:
4619 case BPF_LWT_ENCAP_SEG6_INLINE:
4620 return bpf_push_seg6_encap(skb, type, hdr, len);
4627 static const struct bpf_func_proto bpf_lwt_push_encap_proto = {
4628 .func = bpf_lwt_push_encap,
4630 .ret_type = RET_INTEGER,
4631 .arg1_type = ARG_PTR_TO_CTX,
4632 .arg2_type = ARG_ANYTHING,
4633 .arg3_type = ARG_PTR_TO_MEM,
4634 .arg4_type = ARG_CONST_SIZE
4637 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
4638 BPF_CALL_4(bpf_lwt_seg6_store_bytes, struct sk_buff *, skb, u32, offset,
4639 const void *, from, u32, len)
4641 struct seg6_bpf_srh_state *srh_state =
4642 this_cpu_ptr(&seg6_bpf_srh_states);
4643 struct ipv6_sr_hdr *srh = srh_state->srh;
4644 void *srh_tlvs, *srh_end, *ptr;
4650 srh_tlvs = (void *)((char *)srh + ((srh->first_segment + 1) << 4));
4651 srh_end = (void *)((char *)srh + sizeof(*srh) + srh_state->hdrlen);
4653 ptr = skb->data + offset;
4654 if (ptr >= srh_tlvs && ptr + len <= srh_end)
4655 srh_state->valid = false;
4656 else if (ptr < (void *)&srh->flags ||
4657 ptr + len > (void *)&srh->segments)
4660 if (unlikely(bpf_try_make_writable(skb, offset + len)))
4662 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0)
4664 srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
4666 memcpy(skb->data + offset, from, len);
4670 static const struct bpf_func_proto bpf_lwt_seg6_store_bytes_proto = {
4671 .func = bpf_lwt_seg6_store_bytes,
4673 .ret_type = RET_INTEGER,
4674 .arg1_type = ARG_PTR_TO_CTX,
4675 .arg2_type = ARG_ANYTHING,
4676 .arg3_type = ARG_PTR_TO_MEM,
4677 .arg4_type = ARG_CONST_SIZE
4680 static void bpf_update_srh_state(struct sk_buff *skb)
4682 struct seg6_bpf_srh_state *srh_state =
4683 this_cpu_ptr(&seg6_bpf_srh_states);
4686 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0) {
4687 srh_state->srh = NULL;
4689 srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
4690 srh_state->hdrlen = srh_state->srh->hdrlen << 3;
4691 srh_state->valid = true;
4695 BPF_CALL_4(bpf_lwt_seg6_action, struct sk_buff *, skb,
4696 u32, action, void *, param, u32, param_len)
4698 struct seg6_bpf_srh_state *srh_state =
4699 this_cpu_ptr(&seg6_bpf_srh_states);
4704 case SEG6_LOCAL_ACTION_END_X:
4705 if (!seg6_bpf_has_valid_srh(skb))
4707 if (param_len != sizeof(struct in6_addr))
4709 return seg6_lookup_nexthop(skb, (struct in6_addr *)param, 0);
4710 case SEG6_LOCAL_ACTION_END_T:
4711 if (!seg6_bpf_has_valid_srh(skb))
4713 if (param_len != sizeof(int))
4715 return seg6_lookup_nexthop(skb, NULL, *(int *)param);
4716 case SEG6_LOCAL_ACTION_END_DT6:
4717 if (!seg6_bpf_has_valid_srh(skb))
4719 if (param_len != sizeof(int))
4722 if (ipv6_find_hdr(skb, &hdroff, IPPROTO_IPV6, NULL, NULL) < 0)
4724 if (!pskb_pull(skb, hdroff))
4727 skb_postpull_rcsum(skb, skb_network_header(skb), hdroff);
4728 skb_reset_network_header(skb);
4729 skb_reset_transport_header(skb);
4730 skb->encapsulation = 0;
4732 bpf_compute_data_pointers(skb);
4733 bpf_update_srh_state(skb);
4734 return seg6_lookup_nexthop(skb, NULL, *(int *)param);
4735 case SEG6_LOCAL_ACTION_END_B6:
4736 if (srh_state->srh && !seg6_bpf_has_valid_srh(skb))
4738 err = bpf_push_seg6_encap(skb, BPF_LWT_ENCAP_SEG6_INLINE,
4741 bpf_update_srh_state(skb);
4744 case SEG6_LOCAL_ACTION_END_B6_ENCAP:
4745 if (srh_state->srh && !seg6_bpf_has_valid_srh(skb))
4747 err = bpf_push_seg6_encap(skb, BPF_LWT_ENCAP_SEG6,
4750 bpf_update_srh_state(skb);
4758 static const struct bpf_func_proto bpf_lwt_seg6_action_proto = {
4759 .func = bpf_lwt_seg6_action,
4761 .ret_type = RET_INTEGER,
4762 .arg1_type = ARG_PTR_TO_CTX,
4763 .arg2_type = ARG_ANYTHING,
4764 .arg3_type = ARG_PTR_TO_MEM,
4765 .arg4_type = ARG_CONST_SIZE
4768 BPF_CALL_3(bpf_lwt_seg6_adjust_srh, struct sk_buff *, skb, u32, offset,
4771 struct seg6_bpf_srh_state *srh_state =
4772 this_cpu_ptr(&seg6_bpf_srh_states);
4773 struct ipv6_sr_hdr *srh = srh_state->srh;
4774 void *srh_end, *srh_tlvs, *ptr;
4775 struct ipv6hdr *hdr;
4779 if (unlikely(srh == NULL))
4782 srh_tlvs = (void *)((unsigned char *)srh + sizeof(*srh) +
4783 ((srh->first_segment + 1) << 4));
4784 srh_end = (void *)((unsigned char *)srh + sizeof(*srh) +
4786 ptr = skb->data + offset;
4788 if (unlikely(ptr < srh_tlvs || ptr > srh_end))
4790 if (unlikely(len < 0 && (void *)((char *)ptr - len) > srh_end))
4794 ret = skb_cow_head(skb, len);
4795 if (unlikely(ret < 0))
4798 ret = bpf_skb_net_hdr_push(skb, offset, len);
4800 ret = bpf_skb_net_hdr_pop(skb, offset, -1 * len);
4803 bpf_compute_data_pointers(skb);
4804 if (unlikely(ret < 0))
4807 hdr = (struct ipv6hdr *)skb->data;
4808 hdr->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
4810 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0)
4812 srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
4813 srh_state->hdrlen += len;
4814 srh_state->valid = false;
4818 static const struct bpf_func_proto bpf_lwt_seg6_adjust_srh_proto = {
4819 .func = bpf_lwt_seg6_adjust_srh,
4821 .ret_type = RET_INTEGER,
4822 .arg1_type = ARG_PTR_TO_CTX,
4823 .arg2_type = ARG_ANYTHING,
4824 .arg3_type = ARG_ANYTHING,
4826 #endif /* CONFIG_IPV6_SEG6_BPF */
4829 static struct sock *sk_lookup(struct net *net, struct bpf_sock_tuple *tuple,
4830 struct sk_buff *skb, u8 family, u8 proto)
4832 bool refcounted = false;
4833 struct sock *sk = NULL;
4837 dif = skb->dev->ifindex;
4839 if (family == AF_INET) {
4840 __be32 src4 = tuple->ipv4.saddr;
4841 __be32 dst4 = tuple->ipv4.daddr;
4842 int sdif = inet_sdif(skb);
4844 if (proto == IPPROTO_TCP)
4845 sk = __inet_lookup(net, &tcp_hashinfo, skb, 0,
4846 src4, tuple->ipv4.sport,
4847 dst4, tuple->ipv4.dport,
4848 dif, sdif, &refcounted);
4850 sk = __udp4_lib_lookup(net, src4, tuple->ipv4.sport,
4851 dst4, tuple->ipv4.dport,
4852 dif, sdif, &udp_table, skb);
4853 #if IS_ENABLED(CONFIG_IPV6)
4855 struct in6_addr *src6 = (struct in6_addr *)&tuple->ipv6.saddr;
4856 struct in6_addr *dst6 = (struct in6_addr *)&tuple->ipv6.daddr;
4857 int sdif = inet6_sdif(skb);
4859 if (proto == IPPROTO_TCP)
4860 sk = __inet6_lookup(net, &tcp_hashinfo, skb, 0,
4861 src6, tuple->ipv6.sport,
4862 dst6, ntohs(tuple->ipv6.dport),
4863 dif, sdif, &refcounted);
4864 else if (likely(ipv6_bpf_stub))
4865 sk = ipv6_bpf_stub->udp6_lib_lookup(net,
4866 src6, tuple->ipv6.sport,
4867 dst6, tuple->ipv6.dport,
4873 if (unlikely(sk && !refcounted && !sock_flag(sk, SOCK_RCU_FREE))) {
4874 WARN_ONCE(1, "Found non-RCU, unreferenced socket!");
4880 /* bpf_sk_lookup performs the core lookup for different types of sockets,
4881 * taking a reference on the socket if it doesn't have the flag SOCK_RCU_FREE.
4882 * Returns the socket as an 'unsigned long' to simplify the casting in the
4883 * callers to satisfy BPF_CALL declarations.
4885 static unsigned long
4886 bpf_sk_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
4887 u8 proto, u64 netns_id, u64 flags)
4889 struct net *caller_net;
4890 struct sock *sk = NULL;
4891 u8 family = AF_UNSPEC;
4894 family = len == sizeof(tuple->ipv4) ? AF_INET : AF_INET6;
4895 if (unlikely(family == AF_UNSPEC || flags ||
4896 !((s32)netns_id < 0 || netns_id <= S32_MAX)))
4900 caller_net = dev_net(skb->dev);
4902 caller_net = sock_net(skb->sk);
4903 if ((s32)netns_id < 0) {
4905 sk = sk_lookup(net, tuple, skb, family, proto);
4907 net = get_net_ns_by_id(caller_net, netns_id);
4910 sk = sk_lookup(net, tuple, skb, family, proto);
4915 sk = sk_to_full_sk(sk);
4917 return (unsigned long) sk;
4920 BPF_CALL_5(bpf_sk_lookup_tcp, struct sk_buff *, skb,
4921 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
4923 return bpf_sk_lookup(skb, tuple, len, IPPROTO_TCP, netns_id, flags);
4926 static const struct bpf_func_proto bpf_sk_lookup_tcp_proto = {
4927 .func = bpf_sk_lookup_tcp,
4930 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
4931 .arg1_type = ARG_PTR_TO_CTX,
4932 .arg2_type = ARG_PTR_TO_MEM,
4933 .arg3_type = ARG_CONST_SIZE,
4934 .arg4_type = ARG_ANYTHING,
4935 .arg5_type = ARG_ANYTHING,
4938 BPF_CALL_5(bpf_sk_lookup_udp, struct sk_buff *, skb,
4939 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
4941 return bpf_sk_lookup(skb, tuple, len, IPPROTO_UDP, netns_id, flags);
4944 static const struct bpf_func_proto bpf_sk_lookup_udp_proto = {
4945 .func = bpf_sk_lookup_udp,
4948 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
4949 .arg1_type = ARG_PTR_TO_CTX,
4950 .arg2_type = ARG_PTR_TO_MEM,
4951 .arg3_type = ARG_CONST_SIZE,
4952 .arg4_type = ARG_ANYTHING,
4953 .arg5_type = ARG_ANYTHING,
4956 BPF_CALL_1(bpf_sk_release, struct sock *, sk)
4958 if (!sock_flag(sk, SOCK_RCU_FREE))
4963 static const struct bpf_func_proto bpf_sk_release_proto = {
4964 .func = bpf_sk_release,
4966 .ret_type = RET_INTEGER,
4967 .arg1_type = ARG_PTR_TO_SOCKET,
4969 #endif /* CONFIG_INET */
4971 bool bpf_helper_changes_pkt_data(void *func)
4973 if (func == bpf_skb_vlan_push ||
4974 func == bpf_skb_vlan_pop ||
4975 func == bpf_skb_store_bytes ||
4976 func == bpf_skb_change_proto ||
4977 func == bpf_skb_change_head ||
4978 func == sk_skb_change_head ||
4979 func == bpf_skb_change_tail ||
4980 func == sk_skb_change_tail ||
4981 func == bpf_skb_adjust_room ||
4982 func == bpf_skb_pull_data ||
4983 func == sk_skb_pull_data ||
4984 func == bpf_clone_redirect ||
4985 func == bpf_l3_csum_replace ||
4986 func == bpf_l4_csum_replace ||
4987 func == bpf_xdp_adjust_head ||
4988 func == bpf_xdp_adjust_meta ||
4989 func == bpf_msg_pull_data ||
4990 func == bpf_msg_push_data ||
4991 func == bpf_xdp_adjust_tail ||
4992 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
4993 func == bpf_lwt_seg6_store_bytes ||
4994 func == bpf_lwt_seg6_adjust_srh ||
4995 func == bpf_lwt_seg6_action ||
4997 func == bpf_lwt_push_encap)
5003 static const struct bpf_func_proto *
5004 bpf_base_func_proto(enum bpf_func_id func_id)
5007 case BPF_FUNC_map_lookup_elem:
5008 return &bpf_map_lookup_elem_proto;
5009 case BPF_FUNC_map_update_elem:
5010 return &bpf_map_update_elem_proto;
5011 case BPF_FUNC_map_delete_elem:
5012 return &bpf_map_delete_elem_proto;
5013 case BPF_FUNC_map_push_elem:
5014 return &bpf_map_push_elem_proto;
5015 case BPF_FUNC_map_pop_elem:
5016 return &bpf_map_pop_elem_proto;
5017 case BPF_FUNC_map_peek_elem:
5018 return &bpf_map_peek_elem_proto;
5019 case BPF_FUNC_get_prandom_u32:
5020 return &bpf_get_prandom_u32_proto;
5021 case BPF_FUNC_get_smp_processor_id:
5022 return &bpf_get_raw_smp_processor_id_proto;
5023 case BPF_FUNC_get_numa_node_id:
5024 return &bpf_get_numa_node_id_proto;
5025 case BPF_FUNC_tail_call:
5026 return &bpf_tail_call_proto;
5027 case BPF_FUNC_ktime_get_ns:
5028 return &bpf_ktime_get_ns_proto;
5029 case BPF_FUNC_trace_printk:
5030 if (capable(CAP_SYS_ADMIN))
5031 return bpf_get_trace_printk_proto();
5032 /* else: fall through */
5038 static const struct bpf_func_proto *
5039 sock_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5042 /* inet and inet6 sockets are created in a process
5043 * context so there is always a valid uid/gid
5045 case BPF_FUNC_get_current_uid_gid:
5046 return &bpf_get_current_uid_gid_proto;
5047 case BPF_FUNC_get_local_storage:
5048 return &bpf_get_local_storage_proto;
5050 return bpf_base_func_proto(func_id);
5054 static const struct bpf_func_proto *
5055 sock_addr_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5058 /* inet and inet6 sockets are created in a process
5059 * context so there is always a valid uid/gid
5061 case BPF_FUNC_get_current_uid_gid:
5062 return &bpf_get_current_uid_gid_proto;
5064 switch (prog->expected_attach_type) {
5065 case BPF_CGROUP_INET4_CONNECT:
5066 case BPF_CGROUP_INET6_CONNECT:
5067 return &bpf_bind_proto;
5071 case BPF_FUNC_get_socket_cookie:
5072 return &bpf_get_socket_cookie_sock_addr_proto;
5073 case BPF_FUNC_get_local_storage:
5074 return &bpf_get_local_storage_proto;
5076 return bpf_base_func_proto(func_id);
5080 static const struct bpf_func_proto *
5081 sk_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5084 case BPF_FUNC_skb_load_bytes:
5085 return &bpf_skb_load_bytes_proto;
5086 case BPF_FUNC_skb_load_bytes_relative:
5087 return &bpf_skb_load_bytes_relative_proto;
5088 case BPF_FUNC_get_socket_cookie:
5089 return &bpf_get_socket_cookie_proto;
5090 case BPF_FUNC_get_socket_uid:
5091 return &bpf_get_socket_uid_proto;
5093 return bpf_base_func_proto(func_id);
5097 static const struct bpf_func_proto *
5098 cg_skb_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5101 case BPF_FUNC_get_local_storage:
5102 return &bpf_get_local_storage_proto;
5104 return sk_filter_func_proto(func_id, prog);
5108 static const struct bpf_func_proto *
5109 tc_cls_act_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5112 case BPF_FUNC_skb_store_bytes:
5113 return &bpf_skb_store_bytes_proto;
5114 case BPF_FUNC_skb_load_bytes:
5115 return &bpf_skb_load_bytes_proto;
5116 case BPF_FUNC_skb_load_bytes_relative:
5117 return &bpf_skb_load_bytes_relative_proto;
5118 case BPF_FUNC_skb_pull_data:
5119 return &bpf_skb_pull_data_proto;
5120 case BPF_FUNC_csum_diff:
5121 return &bpf_csum_diff_proto;
5122 case BPF_FUNC_csum_update:
5123 return &bpf_csum_update_proto;
5124 case BPF_FUNC_l3_csum_replace:
5125 return &bpf_l3_csum_replace_proto;
5126 case BPF_FUNC_l4_csum_replace:
5127 return &bpf_l4_csum_replace_proto;
5128 case BPF_FUNC_clone_redirect:
5129 return &bpf_clone_redirect_proto;
5130 case BPF_FUNC_get_cgroup_classid:
5131 return &bpf_get_cgroup_classid_proto;
5132 case BPF_FUNC_skb_vlan_push:
5133 return &bpf_skb_vlan_push_proto;
5134 case BPF_FUNC_skb_vlan_pop:
5135 return &bpf_skb_vlan_pop_proto;
5136 case BPF_FUNC_skb_change_proto:
5137 return &bpf_skb_change_proto_proto;
5138 case BPF_FUNC_skb_change_type:
5139 return &bpf_skb_change_type_proto;
5140 case BPF_FUNC_skb_adjust_room:
5141 return &bpf_skb_adjust_room_proto;
5142 case BPF_FUNC_skb_change_tail:
5143 return &bpf_skb_change_tail_proto;
5144 case BPF_FUNC_skb_get_tunnel_key:
5145 return &bpf_skb_get_tunnel_key_proto;
5146 case BPF_FUNC_skb_set_tunnel_key:
5147 return bpf_get_skb_set_tunnel_proto(func_id);
5148 case BPF_FUNC_skb_get_tunnel_opt:
5149 return &bpf_skb_get_tunnel_opt_proto;
5150 case BPF_FUNC_skb_set_tunnel_opt:
5151 return bpf_get_skb_set_tunnel_proto(func_id);
5152 case BPF_FUNC_redirect:
5153 return &bpf_redirect_proto;
5154 case BPF_FUNC_get_route_realm:
5155 return &bpf_get_route_realm_proto;
5156 case BPF_FUNC_get_hash_recalc:
5157 return &bpf_get_hash_recalc_proto;
5158 case BPF_FUNC_set_hash_invalid:
5159 return &bpf_set_hash_invalid_proto;
5160 case BPF_FUNC_set_hash:
5161 return &bpf_set_hash_proto;
5162 case BPF_FUNC_perf_event_output:
5163 return &bpf_skb_event_output_proto;
5164 case BPF_FUNC_get_smp_processor_id:
5165 return &bpf_get_smp_processor_id_proto;
5166 case BPF_FUNC_skb_under_cgroup:
5167 return &bpf_skb_under_cgroup_proto;
5168 case BPF_FUNC_get_socket_cookie:
5169 return &bpf_get_socket_cookie_proto;
5170 case BPF_FUNC_get_socket_uid:
5171 return &bpf_get_socket_uid_proto;
5172 case BPF_FUNC_fib_lookup:
5173 return &bpf_skb_fib_lookup_proto;
5175 case BPF_FUNC_skb_get_xfrm_state:
5176 return &bpf_skb_get_xfrm_state_proto;
5178 #ifdef CONFIG_SOCK_CGROUP_DATA
5179 case BPF_FUNC_skb_cgroup_id:
5180 return &bpf_skb_cgroup_id_proto;
5181 case BPF_FUNC_skb_ancestor_cgroup_id:
5182 return &bpf_skb_ancestor_cgroup_id_proto;
5185 case BPF_FUNC_sk_lookup_tcp:
5186 return &bpf_sk_lookup_tcp_proto;
5187 case BPF_FUNC_sk_lookup_udp:
5188 return &bpf_sk_lookup_udp_proto;
5189 case BPF_FUNC_sk_release:
5190 return &bpf_sk_release_proto;
5193 return bpf_base_func_proto(func_id);
5197 static const struct bpf_func_proto *
5198 xdp_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5201 case BPF_FUNC_perf_event_output:
5202 return &bpf_xdp_event_output_proto;
5203 case BPF_FUNC_get_smp_processor_id:
5204 return &bpf_get_smp_processor_id_proto;
5205 case BPF_FUNC_csum_diff:
5206 return &bpf_csum_diff_proto;
5207 case BPF_FUNC_xdp_adjust_head:
5208 return &bpf_xdp_adjust_head_proto;
5209 case BPF_FUNC_xdp_adjust_meta:
5210 return &bpf_xdp_adjust_meta_proto;
5211 case BPF_FUNC_redirect:
5212 return &bpf_xdp_redirect_proto;
5213 case BPF_FUNC_redirect_map:
5214 return &bpf_xdp_redirect_map_proto;
5215 case BPF_FUNC_xdp_adjust_tail:
5216 return &bpf_xdp_adjust_tail_proto;
5217 case BPF_FUNC_fib_lookup:
5218 return &bpf_xdp_fib_lookup_proto;
5220 return bpf_base_func_proto(func_id);
5224 const struct bpf_func_proto bpf_sock_map_update_proto __weak;
5225 const struct bpf_func_proto bpf_sock_hash_update_proto __weak;
5227 static const struct bpf_func_proto *
5228 sock_ops_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5231 case BPF_FUNC_setsockopt:
5232 return &bpf_setsockopt_proto;
5233 case BPF_FUNC_getsockopt:
5234 return &bpf_getsockopt_proto;
5235 case BPF_FUNC_sock_ops_cb_flags_set:
5236 return &bpf_sock_ops_cb_flags_set_proto;
5237 case BPF_FUNC_sock_map_update:
5238 return &bpf_sock_map_update_proto;
5239 case BPF_FUNC_sock_hash_update:
5240 return &bpf_sock_hash_update_proto;
5241 case BPF_FUNC_get_socket_cookie:
5242 return &bpf_get_socket_cookie_sock_ops_proto;
5243 case BPF_FUNC_get_local_storage:
5244 return &bpf_get_local_storage_proto;
5246 return bpf_base_func_proto(func_id);
5250 const struct bpf_func_proto bpf_msg_redirect_map_proto __weak;
5251 const struct bpf_func_proto bpf_msg_redirect_hash_proto __weak;
5253 static const struct bpf_func_proto *
5254 sk_msg_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5257 case BPF_FUNC_msg_redirect_map:
5258 return &bpf_msg_redirect_map_proto;
5259 case BPF_FUNC_msg_redirect_hash:
5260 return &bpf_msg_redirect_hash_proto;
5261 case BPF_FUNC_msg_apply_bytes:
5262 return &bpf_msg_apply_bytes_proto;
5263 case BPF_FUNC_msg_cork_bytes:
5264 return &bpf_msg_cork_bytes_proto;
5265 case BPF_FUNC_msg_pull_data:
5266 return &bpf_msg_pull_data_proto;
5267 case BPF_FUNC_msg_push_data:
5268 return &bpf_msg_push_data_proto;
5270 return bpf_base_func_proto(func_id);
5274 const struct bpf_func_proto bpf_sk_redirect_map_proto __weak;
5275 const struct bpf_func_proto bpf_sk_redirect_hash_proto __weak;
5277 static const struct bpf_func_proto *
5278 sk_skb_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5281 case BPF_FUNC_skb_store_bytes:
5282 return &bpf_skb_store_bytes_proto;
5283 case BPF_FUNC_skb_load_bytes:
5284 return &bpf_skb_load_bytes_proto;
5285 case BPF_FUNC_skb_pull_data:
5286 return &sk_skb_pull_data_proto;
5287 case BPF_FUNC_skb_change_tail:
5288 return &sk_skb_change_tail_proto;
5289 case BPF_FUNC_skb_change_head:
5290 return &sk_skb_change_head_proto;
5291 case BPF_FUNC_get_socket_cookie:
5292 return &bpf_get_socket_cookie_proto;
5293 case BPF_FUNC_get_socket_uid:
5294 return &bpf_get_socket_uid_proto;
5295 case BPF_FUNC_sk_redirect_map:
5296 return &bpf_sk_redirect_map_proto;
5297 case BPF_FUNC_sk_redirect_hash:
5298 return &bpf_sk_redirect_hash_proto;
5300 case BPF_FUNC_sk_lookup_tcp:
5301 return &bpf_sk_lookup_tcp_proto;
5302 case BPF_FUNC_sk_lookup_udp:
5303 return &bpf_sk_lookup_udp_proto;
5304 case BPF_FUNC_sk_release:
5305 return &bpf_sk_release_proto;
5308 return bpf_base_func_proto(func_id);
5312 static const struct bpf_func_proto *
5313 flow_dissector_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5316 case BPF_FUNC_skb_load_bytes:
5317 return &bpf_skb_load_bytes_proto;
5319 return bpf_base_func_proto(func_id);
5323 static const struct bpf_func_proto *
5324 lwt_out_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5327 case BPF_FUNC_skb_load_bytes:
5328 return &bpf_skb_load_bytes_proto;
5329 case BPF_FUNC_skb_pull_data:
5330 return &bpf_skb_pull_data_proto;
5331 case BPF_FUNC_csum_diff:
5332 return &bpf_csum_diff_proto;
5333 case BPF_FUNC_get_cgroup_classid:
5334 return &bpf_get_cgroup_classid_proto;
5335 case BPF_FUNC_get_route_realm:
5336 return &bpf_get_route_realm_proto;
5337 case BPF_FUNC_get_hash_recalc:
5338 return &bpf_get_hash_recalc_proto;
5339 case BPF_FUNC_perf_event_output:
5340 return &bpf_skb_event_output_proto;
5341 case BPF_FUNC_get_smp_processor_id:
5342 return &bpf_get_smp_processor_id_proto;
5343 case BPF_FUNC_skb_under_cgroup:
5344 return &bpf_skb_under_cgroup_proto;
5346 return bpf_base_func_proto(func_id);
5350 static const struct bpf_func_proto *
5351 lwt_in_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5354 case BPF_FUNC_lwt_push_encap:
5355 return &bpf_lwt_push_encap_proto;
5357 return lwt_out_func_proto(func_id, prog);
5361 static const struct bpf_func_proto *
5362 lwt_xmit_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5365 case BPF_FUNC_skb_get_tunnel_key:
5366 return &bpf_skb_get_tunnel_key_proto;
5367 case BPF_FUNC_skb_set_tunnel_key:
5368 return bpf_get_skb_set_tunnel_proto(func_id);
5369 case BPF_FUNC_skb_get_tunnel_opt:
5370 return &bpf_skb_get_tunnel_opt_proto;
5371 case BPF_FUNC_skb_set_tunnel_opt:
5372 return bpf_get_skb_set_tunnel_proto(func_id);
5373 case BPF_FUNC_redirect:
5374 return &bpf_redirect_proto;
5375 case BPF_FUNC_clone_redirect:
5376 return &bpf_clone_redirect_proto;
5377 case BPF_FUNC_skb_change_tail:
5378 return &bpf_skb_change_tail_proto;
5379 case BPF_FUNC_skb_change_head:
5380 return &bpf_skb_change_head_proto;
5381 case BPF_FUNC_skb_store_bytes:
5382 return &bpf_skb_store_bytes_proto;
5383 case BPF_FUNC_csum_update:
5384 return &bpf_csum_update_proto;
5385 case BPF_FUNC_l3_csum_replace:
5386 return &bpf_l3_csum_replace_proto;
5387 case BPF_FUNC_l4_csum_replace:
5388 return &bpf_l4_csum_replace_proto;
5389 case BPF_FUNC_set_hash_invalid:
5390 return &bpf_set_hash_invalid_proto;
5392 return lwt_out_func_proto(func_id, prog);
5396 static const struct bpf_func_proto *
5397 lwt_seg6local_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5400 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
5401 case BPF_FUNC_lwt_seg6_store_bytes:
5402 return &bpf_lwt_seg6_store_bytes_proto;
5403 case BPF_FUNC_lwt_seg6_action:
5404 return &bpf_lwt_seg6_action_proto;
5405 case BPF_FUNC_lwt_seg6_adjust_srh:
5406 return &bpf_lwt_seg6_adjust_srh_proto;
5409 return lwt_out_func_proto(func_id, prog);
5413 static bool bpf_skb_is_valid_access(int off, int size, enum bpf_access_type type,
5414 const struct bpf_prog *prog,
5415 struct bpf_insn_access_aux *info)
5417 const int size_default = sizeof(__u32);
5419 if (off < 0 || off >= sizeof(struct __sk_buff))
5422 /* The verifier guarantees that size > 0. */
5423 if (off % size != 0)
5427 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
5428 if (off + size > offsetofend(struct __sk_buff, cb[4]))
5431 case bpf_ctx_range_till(struct __sk_buff, remote_ip6[0], remote_ip6[3]):
5432 case bpf_ctx_range_till(struct __sk_buff, local_ip6[0], local_ip6[3]):
5433 case bpf_ctx_range_till(struct __sk_buff, remote_ip4, remote_ip4):
5434 case bpf_ctx_range_till(struct __sk_buff, local_ip4, local_ip4):
5435 case bpf_ctx_range(struct __sk_buff, data):
5436 case bpf_ctx_range(struct __sk_buff, data_meta):
5437 case bpf_ctx_range(struct __sk_buff, data_end):
5438 if (size != size_default)
5441 case bpf_ctx_range_ptr(struct __sk_buff, flow_keys):
5442 if (size != sizeof(__u64))
5446 /* Only narrow read access allowed for now. */
5447 if (type == BPF_WRITE) {
5448 if (size != size_default)
5451 bpf_ctx_record_field_size(info, size_default);
5452 if (!bpf_ctx_narrow_access_ok(off, size, size_default))
5460 static bool sk_filter_is_valid_access(int off, int size,
5461 enum bpf_access_type type,
5462 const struct bpf_prog *prog,
5463 struct bpf_insn_access_aux *info)
5466 case bpf_ctx_range(struct __sk_buff, tc_classid):
5467 case bpf_ctx_range(struct __sk_buff, data):
5468 case bpf_ctx_range(struct __sk_buff, data_meta):
5469 case bpf_ctx_range(struct __sk_buff, data_end):
5470 case bpf_ctx_range_ptr(struct __sk_buff, flow_keys):
5471 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
5475 if (type == BPF_WRITE) {
5477 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
5484 return bpf_skb_is_valid_access(off, size, type, prog, info);
5487 static bool cg_skb_is_valid_access(int off, int size,
5488 enum bpf_access_type type,
5489 const struct bpf_prog *prog,
5490 struct bpf_insn_access_aux *info)
5493 case bpf_ctx_range(struct __sk_buff, tc_classid):
5494 case bpf_ctx_range(struct __sk_buff, data_meta):
5495 case bpf_ctx_range_ptr(struct __sk_buff, flow_keys):
5497 case bpf_ctx_range(struct __sk_buff, data):
5498 case bpf_ctx_range(struct __sk_buff, data_end):
5499 if (!capable(CAP_SYS_ADMIN))
5504 if (type == BPF_WRITE) {
5506 case bpf_ctx_range(struct __sk_buff, mark):
5507 case bpf_ctx_range(struct __sk_buff, priority):
5508 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
5516 case bpf_ctx_range(struct __sk_buff, data):
5517 info->reg_type = PTR_TO_PACKET;
5519 case bpf_ctx_range(struct __sk_buff, data_end):
5520 info->reg_type = PTR_TO_PACKET_END;
5524 return bpf_skb_is_valid_access(off, size, type, prog, info);
5527 static bool lwt_is_valid_access(int off, int size,
5528 enum bpf_access_type type,
5529 const struct bpf_prog *prog,
5530 struct bpf_insn_access_aux *info)
5533 case bpf_ctx_range(struct __sk_buff, tc_classid):
5534 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
5535 case bpf_ctx_range(struct __sk_buff, data_meta):
5536 case bpf_ctx_range_ptr(struct __sk_buff, flow_keys):
5540 if (type == BPF_WRITE) {
5542 case bpf_ctx_range(struct __sk_buff, mark):
5543 case bpf_ctx_range(struct __sk_buff, priority):
5544 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
5552 case bpf_ctx_range(struct __sk_buff, data):
5553 info->reg_type = PTR_TO_PACKET;
5555 case bpf_ctx_range(struct __sk_buff, data_end):
5556 info->reg_type = PTR_TO_PACKET_END;
5560 return bpf_skb_is_valid_access(off, size, type, prog, info);
5563 /* Attach type specific accesses */
5564 static bool __sock_filter_check_attach_type(int off,
5565 enum bpf_access_type access_type,
5566 enum bpf_attach_type attach_type)
5569 case offsetof(struct bpf_sock, bound_dev_if):
5570 case offsetof(struct bpf_sock, mark):
5571 case offsetof(struct bpf_sock, priority):
5572 switch (attach_type) {
5573 case BPF_CGROUP_INET_SOCK_CREATE:
5578 case bpf_ctx_range(struct bpf_sock, src_ip4):
5579 switch (attach_type) {
5580 case BPF_CGROUP_INET4_POST_BIND:
5585 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
5586 switch (attach_type) {
5587 case BPF_CGROUP_INET6_POST_BIND:
5592 case bpf_ctx_range(struct bpf_sock, src_port):
5593 switch (attach_type) {
5594 case BPF_CGROUP_INET4_POST_BIND:
5595 case BPF_CGROUP_INET6_POST_BIND:
5602 return access_type == BPF_READ;
5607 static bool __sock_filter_check_size(int off, int size,
5608 struct bpf_insn_access_aux *info)
5610 const int size_default = sizeof(__u32);
5613 case bpf_ctx_range(struct bpf_sock, src_ip4):
5614 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
5615 bpf_ctx_record_field_size(info, size_default);
5616 return bpf_ctx_narrow_access_ok(off, size, size_default);
5619 return size == size_default;
5622 bool bpf_sock_is_valid_access(int off, int size, enum bpf_access_type type,
5623 struct bpf_insn_access_aux *info)
5625 if (off < 0 || off >= sizeof(struct bpf_sock))
5627 if (off % size != 0)
5629 if (!__sock_filter_check_size(off, size, info))
5634 static bool sock_filter_is_valid_access(int off, int size,
5635 enum bpf_access_type type,
5636 const struct bpf_prog *prog,
5637 struct bpf_insn_access_aux *info)
5639 if (!bpf_sock_is_valid_access(off, size, type, info))
5641 return __sock_filter_check_attach_type(off, type,
5642 prog->expected_attach_type);
5645 static int bpf_noop_prologue(struct bpf_insn *insn_buf, bool direct_write,
5646 const struct bpf_prog *prog)
5648 /* Neither direct read nor direct write requires any preliminary
5654 static int bpf_unclone_prologue(struct bpf_insn *insn_buf, bool direct_write,
5655 const struct bpf_prog *prog, int drop_verdict)
5657 struct bpf_insn *insn = insn_buf;
5662 /* if (!skb->cloned)
5665 * (Fast-path, otherwise approximation that we might be
5666 * a clone, do the rest in helper.)
5668 *insn++ = BPF_LDX_MEM(BPF_B, BPF_REG_6, BPF_REG_1, CLONED_OFFSET());
5669 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_6, CLONED_MASK);
5670 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_6, 0, 7);
5672 /* ret = bpf_skb_pull_data(skb, 0); */
5673 *insn++ = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1);
5674 *insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_2, BPF_REG_2);
5675 *insn++ = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
5676 BPF_FUNC_skb_pull_data);
5679 * return TC_ACT_SHOT;
5681 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2);
5682 *insn++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_0, drop_verdict);
5683 *insn++ = BPF_EXIT_INSN();
5686 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_6);
5688 *insn++ = prog->insnsi[0];
5690 return insn - insn_buf;
5693 static int bpf_gen_ld_abs(const struct bpf_insn *orig,
5694 struct bpf_insn *insn_buf)
5696 bool indirect = BPF_MODE(orig->code) == BPF_IND;
5697 struct bpf_insn *insn = insn_buf;
5699 /* We're guaranteed here that CTX is in R6. */
5700 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_CTX);
5702 *insn++ = BPF_MOV64_IMM(BPF_REG_2, orig->imm);
5704 *insn++ = BPF_MOV64_REG(BPF_REG_2, orig->src_reg);
5706 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, orig->imm);
5709 switch (BPF_SIZE(orig->code)) {
5711 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_8_no_cache);
5714 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_16_no_cache);
5717 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_32_no_cache);
5721 *insn++ = BPF_JMP_IMM(BPF_JSGE, BPF_REG_0, 0, 2);
5722 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_0, BPF_REG_0);
5723 *insn++ = BPF_EXIT_INSN();
5725 return insn - insn_buf;
5728 static int tc_cls_act_prologue(struct bpf_insn *insn_buf, bool direct_write,
5729 const struct bpf_prog *prog)
5731 return bpf_unclone_prologue(insn_buf, direct_write, prog, TC_ACT_SHOT);
5734 static bool tc_cls_act_is_valid_access(int off, int size,
5735 enum bpf_access_type type,
5736 const struct bpf_prog *prog,
5737 struct bpf_insn_access_aux *info)
5739 if (type == BPF_WRITE) {
5741 case bpf_ctx_range(struct __sk_buff, mark):
5742 case bpf_ctx_range(struct __sk_buff, tc_index):
5743 case bpf_ctx_range(struct __sk_buff, priority):
5744 case bpf_ctx_range(struct __sk_buff, tc_classid):
5745 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
5753 case bpf_ctx_range(struct __sk_buff, data):
5754 info->reg_type = PTR_TO_PACKET;
5756 case bpf_ctx_range(struct __sk_buff, data_meta):
5757 info->reg_type = PTR_TO_PACKET_META;
5759 case bpf_ctx_range(struct __sk_buff, data_end):
5760 info->reg_type = PTR_TO_PACKET_END;
5762 case bpf_ctx_range_ptr(struct __sk_buff, flow_keys):
5763 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
5767 return bpf_skb_is_valid_access(off, size, type, prog, info);
5770 static bool __is_valid_xdp_access(int off, int size)
5772 if (off < 0 || off >= sizeof(struct xdp_md))
5774 if (off % size != 0)
5776 if (size != sizeof(__u32))
5782 static bool xdp_is_valid_access(int off, int size,
5783 enum bpf_access_type type,
5784 const struct bpf_prog *prog,
5785 struct bpf_insn_access_aux *info)
5787 if (type == BPF_WRITE) {
5788 if (bpf_prog_is_dev_bound(prog->aux)) {
5790 case offsetof(struct xdp_md, rx_queue_index):
5791 return __is_valid_xdp_access(off, size);
5798 case offsetof(struct xdp_md, data):
5799 info->reg_type = PTR_TO_PACKET;
5801 case offsetof(struct xdp_md, data_meta):
5802 info->reg_type = PTR_TO_PACKET_META;
5804 case offsetof(struct xdp_md, data_end):
5805 info->reg_type = PTR_TO_PACKET_END;
5809 return __is_valid_xdp_access(off, size);
5812 void bpf_warn_invalid_xdp_action(u32 act)
5814 const u32 act_max = XDP_REDIRECT;
5816 WARN_ONCE(1, "%s XDP return value %u, expect packet loss!\n",
5817 act > act_max ? "Illegal" : "Driver unsupported",
5820 EXPORT_SYMBOL_GPL(bpf_warn_invalid_xdp_action);
5822 static bool sock_addr_is_valid_access(int off, int size,
5823 enum bpf_access_type type,
5824 const struct bpf_prog *prog,
5825 struct bpf_insn_access_aux *info)
5827 const int size_default = sizeof(__u32);
5829 if (off < 0 || off >= sizeof(struct bpf_sock_addr))
5831 if (off % size != 0)
5834 /* Disallow access to IPv6 fields from IPv4 contex and vise
5838 case bpf_ctx_range(struct bpf_sock_addr, user_ip4):
5839 switch (prog->expected_attach_type) {
5840 case BPF_CGROUP_INET4_BIND:
5841 case BPF_CGROUP_INET4_CONNECT:
5842 case BPF_CGROUP_UDP4_SENDMSG:
5848 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
5849 switch (prog->expected_attach_type) {
5850 case BPF_CGROUP_INET6_BIND:
5851 case BPF_CGROUP_INET6_CONNECT:
5852 case BPF_CGROUP_UDP6_SENDMSG:
5858 case bpf_ctx_range(struct bpf_sock_addr, msg_src_ip4):
5859 switch (prog->expected_attach_type) {
5860 case BPF_CGROUP_UDP4_SENDMSG:
5866 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
5868 switch (prog->expected_attach_type) {
5869 case BPF_CGROUP_UDP6_SENDMSG:
5878 case bpf_ctx_range(struct bpf_sock_addr, user_ip4):
5879 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
5880 case bpf_ctx_range(struct bpf_sock_addr, msg_src_ip4):
5881 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
5883 /* Only narrow read access allowed for now. */
5884 if (type == BPF_READ) {
5885 bpf_ctx_record_field_size(info, size_default);
5886 if (!bpf_ctx_narrow_access_ok(off, size, size_default))
5889 if (size != size_default)
5893 case bpf_ctx_range(struct bpf_sock_addr, user_port):
5894 if (size != size_default)
5898 if (type == BPF_READ) {
5899 if (size != size_default)
5909 static bool sock_ops_is_valid_access(int off, int size,
5910 enum bpf_access_type type,
5911 const struct bpf_prog *prog,
5912 struct bpf_insn_access_aux *info)
5914 const int size_default = sizeof(__u32);
5916 if (off < 0 || off >= sizeof(struct bpf_sock_ops))
5919 /* The verifier guarantees that size > 0. */
5920 if (off % size != 0)
5923 if (type == BPF_WRITE) {
5925 case offsetof(struct bpf_sock_ops, reply):
5926 case offsetof(struct bpf_sock_ops, sk_txhash):
5927 if (size != size_default)
5935 case bpf_ctx_range_till(struct bpf_sock_ops, bytes_received,
5937 if (size != sizeof(__u64))
5941 if (size != size_default)
5950 static int sk_skb_prologue(struct bpf_insn *insn_buf, bool direct_write,
5951 const struct bpf_prog *prog)
5953 return bpf_unclone_prologue(insn_buf, direct_write, prog, SK_DROP);
5956 static bool sk_skb_is_valid_access(int off, int size,
5957 enum bpf_access_type type,
5958 const struct bpf_prog *prog,
5959 struct bpf_insn_access_aux *info)
5962 case bpf_ctx_range(struct __sk_buff, tc_classid):
5963 case bpf_ctx_range(struct __sk_buff, data_meta):
5964 case bpf_ctx_range_ptr(struct __sk_buff, flow_keys):
5968 if (type == BPF_WRITE) {
5970 case bpf_ctx_range(struct __sk_buff, tc_index):
5971 case bpf_ctx_range(struct __sk_buff, priority):
5979 case bpf_ctx_range(struct __sk_buff, mark):
5981 case bpf_ctx_range(struct __sk_buff, data):
5982 info->reg_type = PTR_TO_PACKET;
5984 case bpf_ctx_range(struct __sk_buff, data_end):
5985 info->reg_type = PTR_TO_PACKET_END;
5989 return bpf_skb_is_valid_access(off, size, type, prog, info);
5992 static bool sk_msg_is_valid_access(int off, int size,
5993 enum bpf_access_type type,
5994 const struct bpf_prog *prog,
5995 struct bpf_insn_access_aux *info)
5997 if (type == BPF_WRITE)
6001 case offsetof(struct sk_msg_md, data):
6002 info->reg_type = PTR_TO_PACKET;
6003 if (size != sizeof(__u64))
6006 case offsetof(struct sk_msg_md, data_end):
6007 info->reg_type = PTR_TO_PACKET_END;
6008 if (size != sizeof(__u64))
6012 if (size != sizeof(__u32))
6016 if (off < 0 || off >= sizeof(struct sk_msg_md))
6018 if (off % size != 0)
6024 static bool flow_dissector_is_valid_access(int off, int size,
6025 enum bpf_access_type type,
6026 const struct bpf_prog *prog,
6027 struct bpf_insn_access_aux *info)
6029 if (type == BPF_WRITE) {
6031 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
6039 case bpf_ctx_range(struct __sk_buff, data):
6040 info->reg_type = PTR_TO_PACKET;
6042 case bpf_ctx_range(struct __sk_buff, data_end):
6043 info->reg_type = PTR_TO_PACKET_END;
6045 case bpf_ctx_range_ptr(struct __sk_buff, flow_keys):
6046 info->reg_type = PTR_TO_FLOW_KEYS;
6048 case bpf_ctx_range(struct __sk_buff, tc_classid):
6049 case bpf_ctx_range(struct __sk_buff, data_meta):
6050 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
6054 return bpf_skb_is_valid_access(off, size, type, prog, info);
6057 static u32 bpf_convert_ctx_access(enum bpf_access_type type,
6058 const struct bpf_insn *si,
6059 struct bpf_insn *insn_buf,
6060 struct bpf_prog *prog, u32 *target_size)
6062 struct bpf_insn *insn = insn_buf;
6066 case offsetof(struct __sk_buff, len):
6067 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6068 bpf_target_off(struct sk_buff, len, 4,
6072 case offsetof(struct __sk_buff, protocol):
6073 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
6074 bpf_target_off(struct sk_buff, protocol, 2,
6078 case offsetof(struct __sk_buff, vlan_proto):
6079 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
6080 bpf_target_off(struct sk_buff, vlan_proto, 2,
6084 case offsetof(struct __sk_buff, priority):
6085 if (type == BPF_WRITE)
6086 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
6087 bpf_target_off(struct sk_buff, priority, 4,
6090 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6091 bpf_target_off(struct sk_buff, priority, 4,
6095 case offsetof(struct __sk_buff, ingress_ifindex):
6096 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6097 bpf_target_off(struct sk_buff, skb_iif, 4,
6101 case offsetof(struct __sk_buff, ifindex):
6102 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
6103 si->dst_reg, si->src_reg,
6104 offsetof(struct sk_buff, dev));
6105 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
6106 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6107 bpf_target_off(struct net_device, ifindex, 4,
6111 case offsetof(struct __sk_buff, hash):
6112 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6113 bpf_target_off(struct sk_buff, hash, 4,
6117 case offsetof(struct __sk_buff, mark):
6118 if (type == BPF_WRITE)
6119 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
6120 bpf_target_off(struct sk_buff, mark, 4,
6123 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6124 bpf_target_off(struct sk_buff, mark, 4,
6128 case offsetof(struct __sk_buff, pkt_type):
6130 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->src_reg,
6132 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, PKT_TYPE_MAX);
6133 #ifdef __BIG_ENDIAN_BITFIELD
6134 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, 5);
6138 case offsetof(struct __sk_buff, queue_mapping):
6139 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
6140 bpf_target_off(struct sk_buff, queue_mapping, 2,
6144 case offsetof(struct __sk_buff, vlan_present):
6145 case offsetof(struct __sk_buff, vlan_tci):
6146 BUILD_BUG_ON(VLAN_TAG_PRESENT != 0x1000);
6148 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
6149 bpf_target_off(struct sk_buff, vlan_tci, 2,
6151 if (si->off == offsetof(struct __sk_buff, vlan_tci)) {
6152 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg,
6155 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, 12);
6156 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, 1);
6160 case offsetof(struct __sk_buff, cb[0]) ...
6161 offsetofend(struct __sk_buff, cb[4]) - 1:
6162 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, data) < 20);
6163 BUILD_BUG_ON((offsetof(struct sk_buff, cb) +
6164 offsetof(struct qdisc_skb_cb, data)) %
6167 prog->cb_access = 1;
6169 off -= offsetof(struct __sk_buff, cb[0]);
6170 off += offsetof(struct sk_buff, cb);
6171 off += offsetof(struct qdisc_skb_cb, data);
6172 if (type == BPF_WRITE)
6173 *insn++ = BPF_STX_MEM(BPF_SIZE(si->code), si->dst_reg,
6176 *insn++ = BPF_LDX_MEM(BPF_SIZE(si->code), si->dst_reg,
6180 case offsetof(struct __sk_buff, tc_classid):
6181 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, tc_classid) != 2);
6184 off -= offsetof(struct __sk_buff, tc_classid);
6185 off += offsetof(struct sk_buff, cb);
6186 off += offsetof(struct qdisc_skb_cb, tc_classid);
6188 if (type == BPF_WRITE)
6189 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg,
6192 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg,
6196 case offsetof(struct __sk_buff, data):
6197 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
6198 si->dst_reg, si->src_reg,
6199 offsetof(struct sk_buff, data));
6202 case offsetof(struct __sk_buff, data_meta):
6204 off -= offsetof(struct __sk_buff, data_meta);
6205 off += offsetof(struct sk_buff, cb);
6206 off += offsetof(struct bpf_skb_data_end, data_meta);
6207 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
6211 case offsetof(struct __sk_buff, data_end):
6213 off -= offsetof(struct __sk_buff, data_end);
6214 off += offsetof(struct sk_buff, cb);
6215 off += offsetof(struct bpf_skb_data_end, data_end);
6216 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
6220 case offsetof(struct __sk_buff, tc_index):
6221 #ifdef CONFIG_NET_SCHED
6222 if (type == BPF_WRITE)
6223 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg, si->src_reg,
6224 bpf_target_off(struct sk_buff, tc_index, 2,
6227 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
6228 bpf_target_off(struct sk_buff, tc_index, 2,
6232 if (type == BPF_WRITE)
6233 *insn++ = BPF_MOV64_REG(si->dst_reg, si->dst_reg);
6235 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
6239 case offsetof(struct __sk_buff, napi_id):
6240 #if defined(CONFIG_NET_RX_BUSY_POLL)
6241 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6242 bpf_target_off(struct sk_buff, napi_id, 4,
6244 *insn++ = BPF_JMP_IMM(BPF_JGE, si->dst_reg, MIN_NAPI_ID, 1);
6245 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
6248 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
6251 case offsetof(struct __sk_buff, family):
6252 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_family) != 2);
6254 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
6255 si->dst_reg, si->src_reg,
6256 offsetof(struct sk_buff, sk));
6257 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
6258 bpf_target_off(struct sock_common,
6262 case offsetof(struct __sk_buff, remote_ip4):
6263 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_daddr) != 4);
6265 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
6266 si->dst_reg, si->src_reg,
6267 offsetof(struct sk_buff, sk));
6268 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6269 bpf_target_off(struct sock_common,
6273 case offsetof(struct __sk_buff, local_ip4):
6274 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
6275 skc_rcv_saddr) != 4);
6277 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
6278 si->dst_reg, si->src_reg,
6279 offsetof(struct sk_buff, sk));
6280 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6281 bpf_target_off(struct sock_common,
6285 case offsetof(struct __sk_buff, remote_ip6[0]) ...
6286 offsetof(struct __sk_buff, remote_ip6[3]):
6287 #if IS_ENABLED(CONFIG_IPV6)
6288 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
6289 skc_v6_daddr.s6_addr32[0]) != 4);
6292 off -= offsetof(struct __sk_buff, remote_ip6[0]);
6294 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
6295 si->dst_reg, si->src_reg,
6296 offsetof(struct sk_buff, sk));
6297 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6298 offsetof(struct sock_common,
6299 skc_v6_daddr.s6_addr32[0]) +
6302 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
6305 case offsetof(struct __sk_buff, local_ip6[0]) ...
6306 offsetof(struct __sk_buff, local_ip6[3]):
6307 #if IS_ENABLED(CONFIG_IPV6)
6308 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
6309 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
6312 off -= offsetof(struct __sk_buff, local_ip6[0]);
6314 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
6315 si->dst_reg, si->src_reg,
6316 offsetof(struct sk_buff, sk));
6317 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6318 offsetof(struct sock_common,
6319 skc_v6_rcv_saddr.s6_addr32[0]) +
6322 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
6326 case offsetof(struct __sk_buff, remote_port):
6327 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_dport) != 2);
6329 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
6330 si->dst_reg, si->src_reg,
6331 offsetof(struct sk_buff, sk));
6332 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
6333 bpf_target_off(struct sock_common,
6336 #ifndef __BIG_ENDIAN_BITFIELD
6337 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
6341 case offsetof(struct __sk_buff, local_port):
6342 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_num) != 2);
6344 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
6345 si->dst_reg, si->src_reg,
6346 offsetof(struct sk_buff, sk));
6347 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
6348 bpf_target_off(struct sock_common,
6349 skc_num, 2, target_size));
6352 case offsetof(struct __sk_buff, flow_keys):
6354 off -= offsetof(struct __sk_buff, flow_keys);
6355 off += offsetof(struct sk_buff, cb);
6356 off += offsetof(struct qdisc_skb_cb, flow_keys);
6357 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
6362 return insn - insn_buf;
6365 u32 bpf_sock_convert_ctx_access(enum bpf_access_type type,
6366 const struct bpf_insn *si,
6367 struct bpf_insn *insn_buf,
6368 struct bpf_prog *prog, u32 *target_size)
6370 struct bpf_insn *insn = insn_buf;
6374 case offsetof(struct bpf_sock, bound_dev_if):
6375 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_bound_dev_if) != 4);
6377 if (type == BPF_WRITE)
6378 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
6379 offsetof(struct sock, sk_bound_dev_if));
6381 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6382 offsetof(struct sock, sk_bound_dev_if));
6385 case offsetof(struct bpf_sock, mark):
6386 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_mark) != 4);
6388 if (type == BPF_WRITE)
6389 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
6390 offsetof(struct sock, sk_mark));
6392 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6393 offsetof(struct sock, sk_mark));
6396 case offsetof(struct bpf_sock, priority):
6397 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_priority) != 4);
6399 if (type == BPF_WRITE)
6400 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
6401 offsetof(struct sock, sk_priority));
6403 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6404 offsetof(struct sock, sk_priority));
6407 case offsetof(struct bpf_sock, family):
6408 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_family) != 2);
6410 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
6411 offsetof(struct sock, sk_family));
6414 case offsetof(struct bpf_sock, type):
6415 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6416 offsetof(struct sock, __sk_flags_offset));
6417 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_TYPE_MASK);
6418 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, SK_FL_TYPE_SHIFT);
6421 case offsetof(struct bpf_sock, protocol):
6422 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6423 offsetof(struct sock, __sk_flags_offset));
6424 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_PROTO_MASK);
6425 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, SK_FL_PROTO_SHIFT);
6428 case offsetof(struct bpf_sock, src_ip4):
6429 *insn++ = BPF_LDX_MEM(
6430 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
6431 bpf_target_off(struct sock_common, skc_rcv_saddr,
6432 FIELD_SIZEOF(struct sock_common,
6437 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
6438 #if IS_ENABLED(CONFIG_IPV6)
6440 off -= offsetof(struct bpf_sock, src_ip6[0]);
6441 *insn++ = BPF_LDX_MEM(
6442 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
6445 skc_v6_rcv_saddr.s6_addr32[0],
6446 FIELD_SIZEOF(struct sock_common,
6447 skc_v6_rcv_saddr.s6_addr32[0]),
6448 target_size) + off);
6451 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
6455 case offsetof(struct bpf_sock, src_port):
6456 *insn++ = BPF_LDX_MEM(
6457 BPF_FIELD_SIZEOF(struct sock_common, skc_num),
6458 si->dst_reg, si->src_reg,
6459 bpf_target_off(struct sock_common, skc_num,
6460 FIELD_SIZEOF(struct sock_common,
6466 return insn - insn_buf;
6469 static u32 tc_cls_act_convert_ctx_access(enum bpf_access_type type,
6470 const struct bpf_insn *si,
6471 struct bpf_insn *insn_buf,
6472 struct bpf_prog *prog, u32 *target_size)
6474 struct bpf_insn *insn = insn_buf;
6477 case offsetof(struct __sk_buff, ifindex):
6478 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
6479 si->dst_reg, si->src_reg,
6480 offsetof(struct sk_buff, dev));
6481 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6482 bpf_target_off(struct net_device, ifindex, 4,
6486 return bpf_convert_ctx_access(type, si, insn_buf, prog,
6490 return insn - insn_buf;
6493 static u32 xdp_convert_ctx_access(enum bpf_access_type type,
6494 const struct bpf_insn *si,
6495 struct bpf_insn *insn_buf,
6496 struct bpf_prog *prog, u32 *target_size)
6498 struct bpf_insn *insn = insn_buf;
6501 case offsetof(struct xdp_md, data):
6502 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data),
6503 si->dst_reg, si->src_reg,
6504 offsetof(struct xdp_buff, data));
6506 case offsetof(struct xdp_md, data_meta):
6507 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_meta),
6508 si->dst_reg, si->src_reg,
6509 offsetof(struct xdp_buff, data_meta));
6511 case offsetof(struct xdp_md, data_end):
6512 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_end),
6513 si->dst_reg, si->src_reg,
6514 offsetof(struct xdp_buff, data_end));
6516 case offsetof(struct xdp_md, ingress_ifindex):
6517 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, rxq),
6518 si->dst_reg, si->src_reg,
6519 offsetof(struct xdp_buff, rxq));
6520 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_rxq_info, dev),
6521 si->dst_reg, si->dst_reg,
6522 offsetof(struct xdp_rxq_info, dev));
6523 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6524 offsetof(struct net_device, ifindex));
6526 case offsetof(struct xdp_md, rx_queue_index):
6527 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, rxq),
6528 si->dst_reg, si->src_reg,
6529 offsetof(struct xdp_buff, rxq));
6530 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6531 offsetof(struct xdp_rxq_info,
6536 return insn - insn_buf;
6539 /* SOCK_ADDR_LOAD_NESTED_FIELD() loads Nested Field S.F.NF where S is type of
6540 * context Structure, F is Field in context structure that contains a pointer
6541 * to Nested Structure of type NS that has the field NF.
6543 * SIZE encodes the load size (BPF_B, BPF_H, etc). It's up to caller to make
6544 * sure that SIZE is not greater than actual size of S.F.NF.
6546 * If offset OFF is provided, the load happens from that offset relative to
6549 #define SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, SIZE, OFF) \
6551 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), si->dst_reg, \
6552 si->src_reg, offsetof(S, F)); \
6553 *insn++ = BPF_LDX_MEM( \
6554 SIZE, si->dst_reg, si->dst_reg, \
6555 bpf_target_off(NS, NF, FIELD_SIZEOF(NS, NF), \
6560 #define SOCK_ADDR_LOAD_NESTED_FIELD(S, NS, F, NF) \
6561 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, \
6562 BPF_FIELD_SIZEOF(NS, NF), 0)
6564 /* SOCK_ADDR_STORE_NESTED_FIELD_OFF() has semantic similar to
6565 * SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF() but for store operation.
6567 * It doesn't support SIZE argument though since narrow stores are not
6568 * supported for now.
6570 * In addition it uses Temporary Field TF (member of struct S) as the 3rd
6571 * "register" since two registers available in convert_ctx_access are not
6572 * enough: we can't override neither SRC, since it contains value to store, nor
6573 * DST since it contains pointer to context that may be used by later
6574 * instructions. But we need a temporary place to save pointer to nested
6575 * structure whose field we want to store to.
6577 #define SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, OFF, TF) \
6579 int tmp_reg = BPF_REG_9; \
6580 if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg) \
6582 if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg) \
6584 *insn++ = BPF_STX_MEM(BPF_DW, si->dst_reg, tmp_reg, \
6586 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), tmp_reg, \
6587 si->dst_reg, offsetof(S, F)); \
6588 *insn++ = BPF_STX_MEM( \
6589 BPF_FIELD_SIZEOF(NS, NF), tmp_reg, si->src_reg, \
6590 bpf_target_off(NS, NF, FIELD_SIZEOF(NS, NF), \
6593 *insn++ = BPF_LDX_MEM(BPF_DW, tmp_reg, si->dst_reg, \
6597 #define SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, SIZE, OFF, \
6600 if (type == BPF_WRITE) { \
6601 SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, OFF, \
6604 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF( \
6605 S, NS, F, NF, SIZE, OFF); \
6609 #define SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD(S, NS, F, NF, TF) \
6610 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF( \
6611 S, NS, F, NF, BPF_FIELD_SIZEOF(NS, NF), 0, TF)
6613 static u32 sock_addr_convert_ctx_access(enum bpf_access_type type,
6614 const struct bpf_insn *si,
6615 struct bpf_insn *insn_buf,
6616 struct bpf_prog *prog, u32 *target_size)
6618 struct bpf_insn *insn = insn_buf;
6622 case offsetof(struct bpf_sock_addr, user_family):
6623 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
6624 struct sockaddr, uaddr, sa_family);
6627 case offsetof(struct bpf_sock_addr, user_ip4):
6628 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
6629 struct bpf_sock_addr_kern, struct sockaddr_in, uaddr,
6630 sin_addr, BPF_SIZE(si->code), 0, tmp_reg);
6633 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
6635 off -= offsetof(struct bpf_sock_addr, user_ip6[0]);
6636 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
6637 struct bpf_sock_addr_kern, struct sockaddr_in6, uaddr,
6638 sin6_addr.s6_addr32[0], BPF_SIZE(si->code), off,
6642 case offsetof(struct bpf_sock_addr, user_port):
6643 /* To get port we need to know sa_family first and then treat
6644 * sockaddr as either sockaddr_in or sockaddr_in6.
6645 * Though we can simplify since port field has same offset and
6646 * size in both structures.
6647 * Here we check this invariant and use just one of the
6648 * structures if it's true.
6650 BUILD_BUG_ON(offsetof(struct sockaddr_in, sin_port) !=
6651 offsetof(struct sockaddr_in6, sin6_port));
6652 BUILD_BUG_ON(FIELD_SIZEOF(struct sockaddr_in, sin_port) !=
6653 FIELD_SIZEOF(struct sockaddr_in6, sin6_port));
6654 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD(struct bpf_sock_addr_kern,
6655 struct sockaddr_in6, uaddr,
6656 sin6_port, tmp_reg);
6659 case offsetof(struct bpf_sock_addr, family):
6660 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
6661 struct sock, sk, sk_family);
6664 case offsetof(struct bpf_sock_addr, type):
6665 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(
6666 struct bpf_sock_addr_kern, struct sock, sk,
6667 __sk_flags_offset, BPF_W, 0);
6668 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_TYPE_MASK);
6669 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, SK_FL_TYPE_SHIFT);
6672 case offsetof(struct bpf_sock_addr, protocol):
6673 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(
6674 struct bpf_sock_addr_kern, struct sock, sk,
6675 __sk_flags_offset, BPF_W, 0);
6676 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_PROTO_MASK);
6677 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg,
6681 case offsetof(struct bpf_sock_addr, msg_src_ip4):
6682 /* Treat t_ctx as struct in_addr for msg_src_ip4. */
6683 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
6684 struct bpf_sock_addr_kern, struct in_addr, t_ctx,
6685 s_addr, BPF_SIZE(si->code), 0, tmp_reg);
6688 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
6691 off -= offsetof(struct bpf_sock_addr, msg_src_ip6[0]);
6692 /* Treat t_ctx as struct in6_addr for msg_src_ip6. */
6693 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
6694 struct bpf_sock_addr_kern, struct in6_addr, t_ctx,
6695 s6_addr32[0], BPF_SIZE(si->code), off, tmp_reg);
6699 return insn - insn_buf;
6702 static u32 sock_ops_convert_ctx_access(enum bpf_access_type type,
6703 const struct bpf_insn *si,
6704 struct bpf_insn *insn_buf,
6705 struct bpf_prog *prog,
6708 struct bpf_insn *insn = insn_buf;
6712 case offsetof(struct bpf_sock_ops, op) ...
6713 offsetof(struct bpf_sock_ops, replylong[3]):
6714 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, op) !=
6715 FIELD_SIZEOF(struct bpf_sock_ops_kern, op));
6716 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, reply) !=
6717 FIELD_SIZEOF(struct bpf_sock_ops_kern, reply));
6718 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, replylong) !=
6719 FIELD_SIZEOF(struct bpf_sock_ops_kern, replylong));
6721 off -= offsetof(struct bpf_sock_ops, op);
6722 off += offsetof(struct bpf_sock_ops_kern, op);
6723 if (type == BPF_WRITE)
6724 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
6727 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6731 case offsetof(struct bpf_sock_ops, family):
6732 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_family) != 2);
6734 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6735 struct bpf_sock_ops_kern, sk),
6736 si->dst_reg, si->src_reg,
6737 offsetof(struct bpf_sock_ops_kern, sk));
6738 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
6739 offsetof(struct sock_common, skc_family));
6742 case offsetof(struct bpf_sock_ops, remote_ip4):
6743 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_daddr) != 4);
6745 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6746 struct bpf_sock_ops_kern, sk),
6747 si->dst_reg, si->src_reg,
6748 offsetof(struct bpf_sock_ops_kern, sk));
6749 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6750 offsetof(struct sock_common, skc_daddr));
6753 case offsetof(struct bpf_sock_ops, local_ip4):
6754 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
6755 skc_rcv_saddr) != 4);
6757 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6758 struct bpf_sock_ops_kern, sk),
6759 si->dst_reg, si->src_reg,
6760 offsetof(struct bpf_sock_ops_kern, sk));
6761 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6762 offsetof(struct sock_common,
6766 case offsetof(struct bpf_sock_ops, remote_ip6[0]) ...
6767 offsetof(struct bpf_sock_ops, remote_ip6[3]):
6768 #if IS_ENABLED(CONFIG_IPV6)
6769 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
6770 skc_v6_daddr.s6_addr32[0]) != 4);
6773 off -= offsetof(struct bpf_sock_ops, remote_ip6[0]);
6774 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6775 struct bpf_sock_ops_kern, sk),
6776 si->dst_reg, si->src_reg,
6777 offsetof(struct bpf_sock_ops_kern, sk));
6778 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6779 offsetof(struct sock_common,
6780 skc_v6_daddr.s6_addr32[0]) +
6783 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
6787 case offsetof(struct bpf_sock_ops, local_ip6[0]) ...
6788 offsetof(struct bpf_sock_ops, local_ip6[3]):
6789 #if IS_ENABLED(CONFIG_IPV6)
6790 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
6791 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
6794 off -= offsetof(struct bpf_sock_ops, local_ip6[0]);
6795 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6796 struct bpf_sock_ops_kern, sk),
6797 si->dst_reg, si->src_reg,
6798 offsetof(struct bpf_sock_ops_kern, sk));
6799 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6800 offsetof(struct sock_common,
6801 skc_v6_rcv_saddr.s6_addr32[0]) +
6804 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
6808 case offsetof(struct bpf_sock_ops, remote_port):
6809 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_dport) != 2);
6811 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6812 struct bpf_sock_ops_kern, sk),
6813 si->dst_reg, si->src_reg,
6814 offsetof(struct bpf_sock_ops_kern, sk));
6815 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
6816 offsetof(struct sock_common, skc_dport));
6817 #ifndef __BIG_ENDIAN_BITFIELD
6818 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
6822 case offsetof(struct bpf_sock_ops, local_port):
6823 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_num) != 2);
6825 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6826 struct bpf_sock_ops_kern, sk),
6827 si->dst_reg, si->src_reg,
6828 offsetof(struct bpf_sock_ops_kern, sk));
6829 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
6830 offsetof(struct sock_common, skc_num));
6833 case offsetof(struct bpf_sock_ops, is_fullsock):
6834 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6835 struct bpf_sock_ops_kern,
6837 si->dst_reg, si->src_reg,
6838 offsetof(struct bpf_sock_ops_kern,
6842 case offsetof(struct bpf_sock_ops, state):
6843 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_state) != 1);
6845 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6846 struct bpf_sock_ops_kern, sk),
6847 si->dst_reg, si->src_reg,
6848 offsetof(struct bpf_sock_ops_kern, sk));
6849 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->dst_reg,
6850 offsetof(struct sock_common, skc_state));
6853 case offsetof(struct bpf_sock_ops, rtt_min):
6854 BUILD_BUG_ON(FIELD_SIZEOF(struct tcp_sock, rtt_min) !=
6855 sizeof(struct minmax));
6856 BUILD_BUG_ON(sizeof(struct minmax) <
6857 sizeof(struct minmax_sample));
6859 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6860 struct bpf_sock_ops_kern, sk),
6861 si->dst_reg, si->src_reg,
6862 offsetof(struct bpf_sock_ops_kern, sk));
6863 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6864 offsetof(struct tcp_sock, rtt_min) +
6865 FIELD_SIZEOF(struct minmax_sample, t));
6868 /* Helper macro for adding read access to tcp_sock or sock fields. */
6869 #define SOCK_OPS_GET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ) \
6871 BUILD_BUG_ON(FIELD_SIZEOF(OBJ, OBJ_FIELD) > \
6872 FIELD_SIZEOF(struct bpf_sock_ops, BPF_FIELD)); \
6873 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
6874 struct bpf_sock_ops_kern, \
6876 si->dst_reg, si->src_reg, \
6877 offsetof(struct bpf_sock_ops_kern, \
6879 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 2); \
6880 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
6881 struct bpf_sock_ops_kern, sk),\
6882 si->dst_reg, si->src_reg, \
6883 offsetof(struct bpf_sock_ops_kern, sk));\
6884 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(OBJ, \
6886 si->dst_reg, si->dst_reg, \
6887 offsetof(OBJ, OBJ_FIELD)); \
6890 /* Helper macro for adding write access to tcp_sock or sock fields.
6891 * The macro is called with two registers, dst_reg which contains a pointer
6892 * to ctx (context) and src_reg which contains the value that should be
6893 * stored. However, we need an additional register since we cannot overwrite
6894 * dst_reg because it may be used later in the program.
6895 * Instead we "borrow" one of the other register. We first save its value
6896 * into a new (temp) field in bpf_sock_ops_kern, use it, and then restore
6897 * it at the end of the macro.
6899 #define SOCK_OPS_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ) \
6901 int reg = BPF_REG_9; \
6902 BUILD_BUG_ON(FIELD_SIZEOF(OBJ, OBJ_FIELD) > \
6903 FIELD_SIZEOF(struct bpf_sock_ops, BPF_FIELD)); \
6904 if (si->dst_reg == reg || si->src_reg == reg) \
6906 if (si->dst_reg == reg || si->src_reg == reg) \
6908 *insn++ = BPF_STX_MEM(BPF_DW, si->dst_reg, reg, \
6909 offsetof(struct bpf_sock_ops_kern, \
6911 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
6912 struct bpf_sock_ops_kern, \
6915 offsetof(struct bpf_sock_ops_kern, \
6917 *insn++ = BPF_JMP_IMM(BPF_JEQ, reg, 0, 2); \
6918 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
6919 struct bpf_sock_ops_kern, sk),\
6921 offsetof(struct bpf_sock_ops_kern, sk));\
6922 *insn++ = BPF_STX_MEM(BPF_FIELD_SIZEOF(OBJ, OBJ_FIELD), \
6924 offsetof(OBJ, OBJ_FIELD)); \
6925 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->dst_reg, \
6926 offsetof(struct bpf_sock_ops_kern, \
6930 #define SOCK_OPS_GET_OR_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ, TYPE) \
6932 if (TYPE == BPF_WRITE) \
6933 SOCK_OPS_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ); \
6935 SOCK_OPS_GET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ); \
6938 case offsetof(struct bpf_sock_ops, snd_cwnd):
6939 SOCK_OPS_GET_FIELD(snd_cwnd, snd_cwnd, struct tcp_sock);
6942 case offsetof(struct bpf_sock_ops, srtt_us):
6943 SOCK_OPS_GET_FIELD(srtt_us, srtt_us, struct tcp_sock);
6946 case offsetof(struct bpf_sock_ops, bpf_sock_ops_cb_flags):
6947 SOCK_OPS_GET_FIELD(bpf_sock_ops_cb_flags, bpf_sock_ops_cb_flags,
6951 case offsetof(struct bpf_sock_ops, snd_ssthresh):
6952 SOCK_OPS_GET_FIELD(snd_ssthresh, snd_ssthresh, struct tcp_sock);
6955 case offsetof(struct bpf_sock_ops, rcv_nxt):
6956 SOCK_OPS_GET_FIELD(rcv_nxt, rcv_nxt, struct tcp_sock);
6959 case offsetof(struct bpf_sock_ops, snd_nxt):
6960 SOCK_OPS_GET_FIELD(snd_nxt, snd_nxt, struct tcp_sock);
6963 case offsetof(struct bpf_sock_ops, snd_una):
6964 SOCK_OPS_GET_FIELD(snd_una, snd_una, struct tcp_sock);
6967 case offsetof(struct bpf_sock_ops, mss_cache):
6968 SOCK_OPS_GET_FIELD(mss_cache, mss_cache, struct tcp_sock);
6971 case offsetof(struct bpf_sock_ops, ecn_flags):
6972 SOCK_OPS_GET_FIELD(ecn_flags, ecn_flags, struct tcp_sock);
6975 case offsetof(struct bpf_sock_ops, rate_delivered):
6976 SOCK_OPS_GET_FIELD(rate_delivered, rate_delivered,
6980 case offsetof(struct bpf_sock_ops, rate_interval_us):
6981 SOCK_OPS_GET_FIELD(rate_interval_us, rate_interval_us,
6985 case offsetof(struct bpf_sock_ops, packets_out):
6986 SOCK_OPS_GET_FIELD(packets_out, packets_out, struct tcp_sock);
6989 case offsetof(struct bpf_sock_ops, retrans_out):
6990 SOCK_OPS_GET_FIELD(retrans_out, retrans_out, struct tcp_sock);
6993 case offsetof(struct bpf_sock_ops, total_retrans):
6994 SOCK_OPS_GET_FIELD(total_retrans, total_retrans,
6998 case offsetof(struct bpf_sock_ops, segs_in):
6999 SOCK_OPS_GET_FIELD(segs_in, segs_in, struct tcp_sock);
7002 case offsetof(struct bpf_sock_ops, data_segs_in):
7003 SOCK_OPS_GET_FIELD(data_segs_in, data_segs_in, struct tcp_sock);
7006 case offsetof(struct bpf_sock_ops, segs_out):
7007 SOCK_OPS_GET_FIELD(segs_out, segs_out, struct tcp_sock);
7010 case offsetof(struct bpf_sock_ops, data_segs_out):
7011 SOCK_OPS_GET_FIELD(data_segs_out, data_segs_out,
7015 case offsetof(struct bpf_sock_ops, lost_out):
7016 SOCK_OPS_GET_FIELD(lost_out, lost_out, struct tcp_sock);
7019 case offsetof(struct bpf_sock_ops, sacked_out):
7020 SOCK_OPS_GET_FIELD(sacked_out, sacked_out, struct tcp_sock);
7023 case offsetof(struct bpf_sock_ops, sk_txhash):
7024 SOCK_OPS_GET_OR_SET_FIELD(sk_txhash, sk_txhash,
7028 case offsetof(struct bpf_sock_ops, bytes_received):
7029 SOCK_OPS_GET_FIELD(bytes_received, bytes_received,
7033 case offsetof(struct bpf_sock_ops, bytes_acked):
7034 SOCK_OPS_GET_FIELD(bytes_acked, bytes_acked, struct tcp_sock);
7038 return insn - insn_buf;
7041 static u32 sk_skb_convert_ctx_access(enum bpf_access_type type,
7042 const struct bpf_insn *si,
7043 struct bpf_insn *insn_buf,
7044 struct bpf_prog *prog, u32 *target_size)
7046 struct bpf_insn *insn = insn_buf;
7050 case offsetof(struct __sk_buff, data_end):
7052 off -= offsetof(struct __sk_buff, data_end);
7053 off += offsetof(struct sk_buff, cb);
7054 off += offsetof(struct tcp_skb_cb, bpf.data_end);
7055 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
7059 return bpf_convert_ctx_access(type, si, insn_buf, prog,
7063 return insn - insn_buf;
7066 static u32 sk_msg_convert_ctx_access(enum bpf_access_type type,
7067 const struct bpf_insn *si,
7068 struct bpf_insn *insn_buf,
7069 struct bpf_prog *prog, u32 *target_size)
7071 struct bpf_insn *insn = insn_buf;
7072 #if IS_ENABLED(CONFIG_IPV6)
7077 case offsetof(struct sk_msg_md, data):
7078 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg, data),
7079 si->dst_reg, si->src_reg,
7080 offsetof(struct sk_msg, data));
7082 case offsetof(struct sk_msg_md, data_end):
7083 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg, data_end),
7084 si->dst_reg, si->src_reg,
7085 offsetof(struct sk_msg, data_end));
7087 case offsetof(struct sk_msg_md, family):
7088 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_family) != 2);
7090 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7092 si->dst_reg, si->src_reg,
7093 offsetof(struct sk_msg, sk));
7094 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
7095 offsetof(struct sock_common, skc_family));
7098 case offsetof(struct sk_msg_md, remote_ip4):
7099 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_daddr) != 4);
7101 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7103 si->dst_reg, si->src_reg,
7104 offsetof(struct sk_msg, sk));
7105 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7106 offsetof(struct sock_common, skc_daddr));
7109 case offsetof(struct sk_msg_md, local_ip4):
7110 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
7111 skc_rcv_saddr) != 4);
7113 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7115 si->dst_reg, si->src_reg,
7116 offsetof(struct sk_msg, sk));
7117 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7118 offsetof(struct sock_common,
7122 case offsetof(struct sk_msg_md, remote_ip6[0]) ...
7123 offsetof(struct sk_msg_md, remote_ip6[3]):
7124 #if IS_ENABLED(CONFIG_IPV6)
7125 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
7126 skc_v6_daddr.s6_addr32[0]) != 4);
7129 off -= offsetof(struct sk_msg_md, remote_ip6[0]);
7130 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7132 si->dst_reg, si->src_reg,
7133 offsetof(struct sk_msg, sk));
7134 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7135 offsetof(struct sock_common,
7136 skc_v6_daddr.s6_addr32[0]) +
7139 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
7143 case offsetof(struct sk_msg_md, local_ip6[0]) ...
7144 offsetof(struct sk_msg_md, local_ip6[3]):
7145 #if IS_ENABLED(CONFIG_IPV6)
7146 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
7147 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
7150 off -= offsetof(struct sk_msg_md, local_ip6[0]);
7151 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7153 si->dst_reg, si->src_reg,
7154 offsetof(struct sk_msg, sk));
7155 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7156 offsetof(struct sock_common,
7157 skc_v6_rcv_saddr.s6_addr32[0]) +
7160 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
7164 case offsetof(struct sk_msg_md, remote_port):
7165 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_dport) != 2);
7167 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7169 si->dst_reg, si->src_reg,
7170 offsetof(struct sk_msg, sk));
7171 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
7172 offsetof(struct sock_common, skc_dport));
7173 #ifndef __BIG_ENDIAN_BITFIELD
7174 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
7178 case offsetof(struct sk_msg_md, local_port):
7179 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_num) != 2);
7181 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7183 si->dst_reg, si->src_reg,
7184 offsetof(struct sk_msg, sk));
7185 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
7186 offsetof(struct sock_common, skc_num));
7190 return insn - insn_buf;
7193 const struct bpf_verifier_ops sk_filter_verifier_ops = {
7194 .get_func_proto = sk_filter_func_proto,
7195 .is_valid_access = sk_filter_is_valid_access,
7196 .convert_ctx_access = bpf_convert_ctx_access,
7197 .gen_ld_abs = bpf_gen_ld_abs,
7200 const struct bpf_prog_ops sk_filter_prog_ops = {
7201 .test_run = bpf_prog_test_run_skb,
7204 const struct bpf_verifier_ops tc_cls_act_verifier_ops = {
7205 .get_func_proto = tc_cls_act_func_proto,
7206 .is_valid_access = tc_cls_act_is_valid_access,
7207 .convert_ctx_access = tc_cls_act_convert_ctx_access,
7208 .gen_prologue = tc_cls_act_prologue,
7209 .gen_ld_abs = bpf_gen_ld_abs,
7212 const struct bpf_prog_ops tc_cls_act_prog_ops = {
7213 .test_run = bpf_prog_test_run_skb,
7216 const struct bpf_verifier_ops xdp_verifier_ops = {
7217 .get_func_proto = xdp_func_proto,
7218 .is_valid_access = xdp_is_valid_access,
7219 .convert_ctx_access = xdp_convert_ctx_access,
7220 .gen_prologue = bpf_noop_prologue,
7223 const struct bpf_prog_ops xdp_prog_ops = {
7224 .test_run = bpf_prog_test_run_xdp,
7227 const struct bpf_verifier_ops cg_skb_verifier_ops = {
7228 .get_func_proto = cg_skb_func_proto,
7229 .is_valid_access = cg_skb_is_valid_access,
7230 .convert_ctx_access = bpf_convert_ctx_access,
7233 const struct bpf_prog_ops cg_skb_prog_ops = {
7234 .test_run = bpf_prog_test_run_skb,
7237 const struct bpf_verifier_ops lwt_in_verifier_ops = {
7238 .get_func_proto = lwt_in_func_proto,
7239 .is_valid_access = lwt_is_valid_access,
7240 .convert_ctx_access = bpf_convert_ctx_access,
7243 const struct bpf_prog_ops lwt_in_prog_ops = {
7244 .test_run = bpf_prog_test_run_skb,
7247 const struct bpf_verifier_ops lwt_out_verifier_ops = {
7248 .get_func_proto = lwt_out_func_proto,
7249 .is_valid_access = lwt_is_valid_access,
7250 .convert_ctx_access = bpf_convert_ctx_access,
7253 const struct bpf_prog_ops lwt_out_prog_ops = {
7254 .test_run = bpf_prog_test_run_skb,
7257 const struct bpf_verifier_ops lwt_xmit_verifier_ops = {
7258 .get_func_proto = lwt_xmit_func_proto,
7259 .is_valid_access = lwt_is_valid_access,
7260 .convert_ctx_access = bpf_convert_ctx_access,
7261 .gen_prologue = tc_cls_act_prologue,
7264 const struct bpf_prog_ops lwt_xmit_prog_ops = {
7265 .test_run = bpf_prog_test_run_skb,
7268 const struct bpf_verifier_ops lwt_seg6local_verifier_ops = {
7269 .get_func_proto = lwt_seg6local_func_proto,
7270 .is_valid_access = lwt_is_valid_access,
7271 .convert_ctx_access = bpf_convert_ctx_access,
7274 const struct bpf_prog_ops lwt_seg6local_prog_ops = {
7275 .test_run = bpf_prog_test_run_skb,
7278 const struct bpf_verifier_ops cg_sock_verifier_ops = {
7279 .get_func_proto = sock_filter_func_proto,
7280 .is_valid_access = sock_filter_is_valid_access,
7281 .convert_ctx_access = bpf_sock_convert_ctx_access,
7284 const struct bpf_prog_ops cg_sock_prog_ops = {
7287 const struct bpf_verifier_ops cg_sock_addr_verifier_ops = {
7288 .get_func_proto = sock_addr_func_proto,
7289 .is_valid_access = sock_addr_is_valid_access,
7290 .convert_ctx_access = sock_addr_convert_ctx_access,
7293 const struct bpf_prog_ops cg_sock_addr_prog_ops = {
7296 const struct bpf_verifier_ops sock_ops_verifier_ops = {
7297 .get_func_proto = sock_ops_func_proto,
7298 .is_valid_access = sock_ops_is_valid_access,
7299 .convert_ctx_access = sock_ops_convert_ctx_access,
7302 const struct bpf_prog_ops sock_ops_prog_ops = {
7305 const struct bpf_verifier_ops sk_skb_verifier_ops = {
7306 .get_func_proto = sk_skb_func_proto,
7307 .is_valid_access = sk_skb_is_valid_access,
7308 .convert_ctx_access = sk_skb_convert_ctx_access,
7309 .gen_prologue = sk_skb_prologue,
7312 const struct bpf_prog_ops sk_skb_prog_ops = {
7315 const struct bpf_verifier_ops sk_msg_verifier_ops = {
7316 .get_func_proto = sk_msg_func_proto,
7317 .is_valid_access = sk_msg_is_valid_access,
7318 .convert_ctx_access = sk_msg_convert_ctx_access,
7319 .gen_prologue = bpf_noop_prologue,
7322 const struct bpf_prog_ops sk_msg_prog_ops = {
7325 const struct bpf_verifier_ops flow_dissector_verifier_ops = {
7326 .get_func_proto = flow_dissector_func_proto,
7327 .is_valid_access = flow_dissector_is_valid_access,
7328 .convert_ctx_access = bpf_convert_ctx_access,
7331 const struct bpf_prog_ops flow_dissector_prog_ops = {
7334 int sk_detach_filter(struct sock *sk)
7337 struct sk_filter *filter;
7339 if (sock_flag(sk, SOCK_FILTER_LOCKED))
7342 filter = rcu_dereference_protected(sk->sk_filter,
7343 lockdep_sock_is_held(sk));
7345 RCU_INIT_POINTER(sk->sk_filter, NULL);
7346 sk_filter_uncharge(sk, filter);
7352 EXPORT_SYMBOL_GPL(sk_detach_filter);
7354 int sk_get_filter(struct sock *sk, struct sock_filter __user *ubuf,
7357 struct sock_fprog_kern *fprog;
7358 struct sk_filter *filter;
7362 filter = rcu_dereference_protected(sk->sk_filter,
7363 lockdep_sock_is_held(sk));
7367 /* We're copying the filter that has been originally attached,
7368 * so no conversion/decode needed anymore. eBPF programs that
7369 * have no original program cannot be dumped through this.
7372 fprog = filter->prog->orig_prog;
7378 /* User space only enquires number of filter blocks. */
7382 if (len < fprog->len)
7386 if (copy_to_user(ubuf, fprog->filter, bpf_classic_proglen(fprog)))
7389 /* Instead of bytes, the API requests to return the number
7399 struct sk_reuseport_kern {
7400 struct sk_buff *skb;
7402 struct sock *selected_sk;
7409 static void bpf_init_reuseport_kern(struct sk_reuseport_kern *reuse_kern,
7410 struct sock_reuseport *reuse,
7411 struct sock *sk, struct sk_buff *skb,
7414 reuse_kern->skb = skb;
7415 reuse_kern->sk = sk;
7416 reuse_kern->selected_sk = NULL;
7417 reuse_kern->data_end = skb->data + skb_headlen(skb);
7418 reuse_kern->hash = hash;
7419 reuse_kern->reuseport_id = reuse->reuseport_id;
7420 reuse_kern->bind_inany = reuse->bind_inany;
7423 struct sock *bpf_run_sk_reuseport(struct sock_reuseport *reuse, struct sock *sk,
7424 struct bpf_prog *prog, struct sk_buff *skb,
7427 struct sk_reuseport_kern reuse_kern;
7428 enum sk_action action;
7430 bpf_init_reuseport_kern(&reuse_kern, reuse, sk, skb, hash);
7431 action = BPF_PROG_RUN(prog, &reuse_kern);
7433 if (action == SK_PASS)
7434 return reuse_kern.selected_sk;
7436 return ERR_PTR(-ECONNREFUSED);
7439 BPF_CALL_4(sk_select_reuseport, struct sk_reuseport_kern *, reuse_kern,
7440 struct bpf_map *, map, void *, key, u32, flags)
7442 struct sock_reuseport *reuse;
7443 struct sock *selected_sk;
7445 selected_sk = map->ops->map_lookup_elem(map, key);
7449 reuse = rcu_dereference(selected_sk->sk_reuseport_cb);
7451 /* selected_sk is unhashed (e.g. by close()) after the
7452 * above map_lookup_elem(). Treat selected_sk has already
7453 * been removed from the map.
7457 if (unlikely(reuse->reuseport_id != reuse_kern->reuseport_id)) {
7460 if (unlikely(!reuse_kern->reuseport_id))
7461 /* There is a small race between adding the
7462 * sk to the map and setting the
7463 * reuse_kern->reuseport_id.
7464 * Treat it as the sk has not been added to
7469 sk = reuse_kern->sk;
7470 if (sk->sk_protocol != selected_sk->sk_protocol)
7472 else if (sk->sk_family != selected_sk->sk_family)
7473 return -EAFNOSUPPORT;
7475 /* Catch all. Likely bound to a different sockaddr. */
7479 reuse_kern->selected_sk = selected_sk;
7484 static const struct bpf_func_proto sk_select_reuseport_proto = {
7485 .func = sk_select_reuseport,
7487 .ret_type = RET_INTEGER,
7488 .arg1_type = ARG_PTR_TO_CTX,
7489 .arg2_type = ARG_CONST_MAP_PTR,
7490 .arg3_type = ARG_PTR_TO_MAP_KEY,
7491 .arg4_type = ARG_ANYTHING,
7494 BPF_CALL_4(sk_reuseport_load_bytes,
7495 const struct sk_reuseport_kern *, reuse_kern, u32, offset,
7496 void *, to, u32, len)
7498 return ____bpf_skb_load_bytes(reuse_kern->skb, offset, to, len);
7501 static const struct bpf_func_proto sk_reuseport_load_bytes_proto = {
7502 .func = sk_reuseport_load_bytes,
7504 .ret_type = RET_INTEGER,
7505 .arg1_type = ARG_PTR_TO_CTX,
7506 .arg2_type = ARG_ANYTHING,
7507 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
7508 .arg4_type = ARG_CONST_SIZE,
7511 BPF_CALL_5(sk_reuseport_load_bytes_relative,
7512 const struct sk_reuseport_kern *, reuse_kern, u32, offset,
7513 void *, to, u32, len, u32, start_header)
7515 return ____bpf_skb_load_bytes_relative(reuse_kern->skb, offset, to,
7519 static const struct bpf_func_proto sk_reuseport_load_bytes_relative_proto = {
7520 .func = sk_reuseport_load_bytes_relative,
7522 .ret_type = RET_INTEGER,
7523 .arg1_type = ARG_PTR_TO_CTX,
7524 .arg2_type = ARG_ANYTHING,
7525 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
7526 .arg4_type = ARG_CONST_SIZE,
7527 .arg5_type = ARG_ANYTHING,
7530 static const struct bpf_func_proto *
7531 sk_reuseport_func_proto(enum bpf_func_id func_id,
7532 const struct bpf_prog *prog)
7535 case BPF_FUNC_sk_select_reuseport:
7536 return &sk_select_reuseport_proto;
7537 case BPF_FUNC_skb_load_bytes:
7538 return &sk_reuseport_load_bytes_proto;
7539 case BPF_FUNC_skb_load_bytes_relative:
7540 return &sk_reuseport_load_bytes_relative_proto;
7542 return bpf_base_func_proto(func_id);
7547 sk_reuseport_is_valid_access(int off, int size,
7548 enum bpf_access_type type,
7549 const struct bpf_prog *prog,
7550 struct bpf_insn_access_aux *info)
7552 const u32 size_default = sizeof(__u32);
7554 if (off < 0 || off >= sizeof(struct sk_reuseport_md) ||
7555 off % size || type != BPF_READ)
7559 case offsetof(struct sk_reuseport_md, data):
7560 info->reg_type = PTR_TO_PACKET;
7561 return size == sizeof(__u64);
7563 case offsetof(struct sk_reuseport_md, data_end):
7564 info->reg_type = PTR_TO_PACKET_END;
7565 return size == sizeof(__u64);
7567 case offsetof(struct sk_reuseport_md, hash):
7568 return size == size_default;
7570 /* Fields that allow narrowing */
7571 case offsetof(struct sk_reuseport_md, eth_protocol):
7572 if (size < FIELD_SIZEOF(struct sk_buff, protocol))
7575 case offsetof(struct sk_reuseport_md, ip_protocol):
7576 case offsetof(struct sk_reuseport_md, bind_inany):
7577 case offsetof(struct sk_reuseport_md, len):
7578 bpf_ctx_record_field_size(info, size_default);
7579 return bpf_ctx_narrow_access_ok(off, size, size_default);
7586 #define SK_REUSEPORT_LOAD_FIELD(F) ({ \
7587 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_reuseport_kern, F), \
7588 si->dst_reg, si->src_reg, \
7589 bpf_target_off(struct sk_reuseport_kern, F, \
7590 FIELD_SIZEOF(struct sk_reuseport_kern, F), \
7594 #define SK_REUSEPORT_LOAD_SKB_FIELD(SKB_FIELD) \
7595 SOCK_ADDR_LOAD_NESTED_FIELD(struct sk_reuseport_kern, \
7600 #define SK_REUSEPORT_LOAD_SK_FIELD_SIZE_OFF(SK_FIELD, BPF_SIZE, EXTRA_OFF) \
7601 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(struct sk_reuseport_kern, \
7604 SK_FIELD, BPF_SIZE, EXTRA_OFF)
7606 static u32 sk_reuseport_convert_ctx_access(enum bpf_access_type type,
7607 const struct bpf_insn *si,
7608 struct bpf_insn *insn_buf,
7609 struct bpf_prog *prog,
7612 struct bpf_insn *insn = insn_buf;
7615 case offsetof(struct sk_reuseport_md, data):
7616 SK_REUSEPORT_LOAD_SKB_FIELD(data);
7619 case offsetof(struct sk_reuseport_md, len):
7620 SK_REUSEPORT_LOAD_SKB_FIELD(len);
7623 case offsetof(struct sk_reuseport_md, eth_protocol):
7624 SK_REUSEPORT_LOAD_SKB_FIELD(protocol);
7627 case offsetof(struct sk_reuseport_md, ip_protocol):
7628 BUILD_BUG_ON(HWEIGHT32(SK_FL_PROTO_MASK) != BITS_PER_BYTE);
7629 SK_REUSEPORT_LOAD_SK_FIELD_SIZE_OFF(__sk_flags_offset,
7631 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_PROTO_MASK);
7632 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg,
7634 /* SK_FL_PROTO_MASK and SK_FL_PROTO_SHIFT are endian
7635 * aware. No further narrowing or masking is needed.
7640 case offsetof(struct sk_reuseport_md, data_end):
7641 SK_REUSEPORT_LOAD_FIELD(data_end);
7644 case offsetof(struct sk_reuseport_md, hash):
7645 SK_REUSEPORT_LOAD_FIELD(hash);
7648 case offsetof(struct sk_reuseport_md, bind_inany):
7649 SK_REUSEPORT_LOAD_FIELD(bind_inany);
7653 return insn - insn_buf;
7656 const struct bpf_verifier_ops sk_reuseport_verifier_ops = {
7657 .get_func_proto = sk_reuseport_func_proto,
7658 .is_valid_access = sk_reuseport_is_valid_access,
7659 .convert_ctx_access = sk_reuseport_convert_ctx_access,
7662 const struct bpf_prog_ops sk_reuseport_prog_ops = {
7664 #endif /* CONFIG_INET */