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>
78 * sk_filter_trim_cap - run a packet through a socket filter
79 * @sk: sock associated with &sk_buff
80 * @skb: buffer to filter
81 * @cap: limit on how short the eBPF program may trim the packet
83 * Run the eBPF program and then cut skb->data to correct size returned by
84 * the program. If pkt_len is 0 we toss packet. If skb->len is smaller
85 * than pkt_len we keep whole skb->data. This is the socket level
86 * wrapper to BPF_PROG_RUN. It returns 0 if the packet should
87 * be accepted or -EPERM if the packet should be tossed.
90 int sk_filter_trim_cap(struct sock *sk, struct sk_buff *skb, unsigned int cap)
93 struct sk_filter *filter;
96 * If the skb was allocated from pfmemalloc reserves, only
97 * allow SOCK_MEMALLOC sockets to use it as this socket is
100 if (skb_pfmemalloc(skb) && !sock_flag(sk, SOCK_MEMALLOC)) {
101 NET_INC_STATS(sock_net(sk), LINUX_MIB_PFMEMALLOCDROP);
104 err = BPF_CGROUP_RUN_PROG_INET_INGRESS(sk, skb);
108 err = security_sock_rcv_skb(sk, skb);
113 filter = rcu_dereference(sk->sk_filter);
115 struct sock *save_sk = skb->sk;
116 unsigned int pkt_len;
119 pkt_len = bpf_prog_run_save_cb(filter->prog, skb);
121 err = pkt_len ? pskb_trim(skb, max(cap, pkt_len)) : -EPERM;
127 EXPORT_SYMBOL(sk_filter_trim_cap);
129 BPF_CALL_1(bpf_skb_get_pay_offset, struct sk_buff *, skb)
131 return skb_get_poff(skb);
134 BPF_CALL_3(bpf_skb_get_nlattr, struct sk_buff *, skb, u32, a, u32, x)
138 if (skb_is_nonlinear(skb))
141 if (skb->len < sizeof(struct nlattr))
144 if (a > skb->len - sizeof(struct nlattr))
147 nla = nla_find((struct nlattr *) &skb->data[a], skb->len - a, x);
149 return (void *) nla - (void *) skb->data;
154 BPF_CALL_3(bpf_skb_get_nlattr_nest, struct sk_buff *, skb, u32, a, u32, x)
158 if (skb_is_nonlinear(skb))
161 if (skb->len < sizeof(struct nlattr))
164 if (a > skb->len - sizeof(struct nlattr))
167 nla = (struct nlattr *) &skb->data[a];
168 if (nla->nla_len > skb->len - a)
171 nla = nla_find_nested(nla, x);
173 return (void *) nla - (void *) skb->data;
178 BPF_CALL_4(bpf_skb_load_helper_8, const struct sk_buff *, skb, const void *,
179 data, int, headlen, int, offset)
182 const int len = sizeof(tmp);
185 if (headlen - offset >= len)
186 return *(u8 *)(data + offset);
187 if (!skb_copy_bits(skb, offset, &tmp, sizeof(tmp)))
190 ptr = bpf_internal_load_pointer_neg_helper(skb, offset, len);
198 BPF_CALL_2(bpf_skb_load_helper_8_no_cache, const struct sk_buff *, skb,
201 return ____bpf_skb_load_helper_8(skb, skb->data, skb->len - skb->data_len,
205 BPF_CALL_4(bpf_skb_load_helper_16, const struct sk_buff *, skb, const void *,
206 data, int, headlen, int, offset)
209 const int len = sizeof(tmp);
212 if (headlen - offset >= len)
213 return get_unaligned_be16(data + offset);
214 if (!skb_copy_bits(skb, offset, &tmp, sizeof(tmp)))
215 return be16_to_cpu(tmp);
217 ptr = bpf_internal_load_pointer_neg_helper(skb, offset, len);
219 return get_unaligned_be16(ptr);
225 BPF_CALL_2(bpf_skb_load_helper_16_no_cache, const struct sk_buff *, skb,
228 return ____bpf_skb_load_helper_16(skb, skb->data, skb->len - skb->data_len,
232 BPF_CALL_4(bpf_skb_load_helper_32, const struct sk_buff *, skb, const void *,
233 data, int, headlen, int, offset)
236 const int len = sizeof(tmp);
238 if (likely(offset >= 0)) {
239 if (headlen - offset >= len)
240 return get_unaligned_be32(data + offset);
241 if (!skb_copy_bits(skb, offset, &tmp, sizeof(tmp)))
242 return be32_to_cpu(tmp);
244 ptr = bpf_internal_load_pointer_neg_helper(skb, offset, len);
246 return get_unaligned_be32(ptr);
252 BPF_CALL_2(bpf_skb_load_helper_32_no_cache, const struct sk_buff *, skb,
255 return ____bpf_skb_load_helper_32(skb, skb->data, skb->len - skb->data_len,
259 BPF_CALL_0(bpf_get_raw_cpu_id)
261 return raw_smp_processor_id();
264 static const struct bpf_func_proto bpf_get_raw_smp_processor_id_proto = {
265 .func = bpf_get_raw_cpu_id,
267 .ret_type = RET_INTEGER,
270 static u32 convert_skb_access(int skb_field, int dst_reg, int src_reg,
271 struct bpf_insn *insn_buf)
273 struct bpf_insn *insn = insn_buf;
277 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
279 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
280 offsetof(struct sk_buff, mark));
284 *insn++ = BPF_LDX_MEM(BPF_B, dst_reg, src_reg, PKT_TYPE_OFFSET());
285 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, PKT_TYPE_MAX);
286 #ifdef __BIG_ENDIAN_BITFIELD
287 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 5);
292 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, queue_mapping) != 2);
294 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
295 offsetof(struct sk_buff, queue_mapping));
298 case SKF_AD_VLAN_TAG:
299 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_tci) != 2);
301 /* dst_reg = *(u16 *) (src_reg + offsetof(vlan_tci)) */
302 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
303 offsetof(struct sk_buff, vlan_tci));
305 case SKF_AD_VLAN_TAG_PRESENT:
306 *insn++ = BPF_LDX_MEM(BPF_B, dst_reg, src_reg, PKT_VLAN_PRESENT_OFFSET());
307 if (PKT_VLAN_PRESENT_BIT)
308 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, PKT_VLAN_PRESENT_BIT);
309 if (PKT_VLAN_PRESENT_BIT < 7)
310 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, 1);
314 return insn - insn_buf;
317 static bool convert_bpf_extensions(struct sock_filter *fp,
318 struct bpf_insn **insnp)
320 struct bpf_insn *insn = *insnp;
324 case SKF_AD_OFF + SKF_AD_PROTOCOL:
325 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2);
327 /* A = *(u16 *) (CTX + offsetof(protocol)) */
328 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
329 offsetof(struct sk_buff, protocol));
330 /* A = ntohs(A) [emitting a nop or swap16] */
331 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
334 case SKF_AD_OFF + SKF_AD_PKTTYPE:
335 cnt = convert_skb_access(SKF_AD_PKTTYPE, BPF_REG_A, BPF_REG_CTX, insn);
339 case SKF_AD_OFF + SKF_AD_IFINDEX:
340 case SKF_AD_OFF + SKF_AD_HATYPE:
341 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4);
342 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, type) != 2);
344 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
345 BPF_REG_TMP, BPF_REG_CTX,
346 offsetof(struct sk_buff, dev));
347 /* if (tmp != 0) goto pc + 1 */
348 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_TMP, 0, 1);
349 *insn++ = BPF_EXIT_INSN();
350 if (fp->k == SKF_AD_OFF + SKF_AD_IFINDEX)
351 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_TMP,
352 offsetof(struct net_device, ifindex));
354 *insn = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_TMP,
355 offsetof(struct net_device, type));
358 case SKF_AD_OFF + SKF_AD_MARK:
359 cnt = convert_skb_access(SKF_AD_MARK, BPF_REG_A, BPF_REG_CTX, insn);
363 case SKF_AD_OFF + SKF_AD_RXHASH:
364 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4);
366 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX,
367 offsetof(struct sk_buff, hash));
370 case SKF_AD_OFF + SKF_AD_QUEUE:
371 cnt = convert_skb_access(SKF_AD_QUEUE, BPF_REG_A, BPF_REG_CTX, insn);
375 case SKF_AD_OFF + SKF_AD_VLAN_TAG:
376 cnt = convert_skb_access(SKF_AD_VLAN_TAG,
377 BPF_REG_A, BPF_REG_CTX, insn);
381 case SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT:
382 cnt = convert_skb_access(SKF_AD_VLAN_TAG_PRESENT,
383 BPF_REG_A, BPF_REG_CTX, insn);
387 case SKF_AD_OFF + SKF_AD_VLAN_TPID:
388 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_proto) != 2);
390 /* A = *(u16 *) (CTX + offsetof(vlan_proto)) */
391 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
392 offsetof(struct sk_buff, vlan_proto));
393 /* A = ntohs(A) [emitting a nop or swap16] */
394 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
397 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
398 case SKF_AD_OFF + SKF_AD_NLATTR:
399 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
400 case SKF_AD_OFF + SKF_AD_CPU:
401 case SKF_AD_OFF + SKF_AD_RANDOM:
403 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX);
405 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_A);
407 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_X);
408 /* Emit call(arg1=CTX, arg2=A, arg3=X) */
410 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
411 *insn = BPF_EMIT_CALL(bpf_skb_get_pay_offset);
413 case SKF_AD_OFF + SKF_AD_NLATTR:
414 *insn = BPF_EMIT_CALL(bpf_skb_get_nlattr);
416 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
417 *insn = BPF_EMIT_CALL(bpf_skb_get_nlattr_nest);
419 case SKF_AD_OFF + SKF_AD_CPU:
420 *insn = BPF_EMIT_CALL(bpf_get_raw_cpu_id);
422 case SKF_AD_OFF + SKF_AD_RANDOM:
423 *insn = BPF_EMIT_CALL(bpf_user_rnd_u32);
424 bpf_user_rnd_init_once();
429 case SKF_AD_OFF + SKF_AD_ALU_XOR_X:
431 *insn = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_X);
435 /* This is just a dummy call to avoid letting the compiler
436 * evict __bpf_call_base() as an optimization. Placed here
437 * where no-one bothers.
439 BUG_ON(__bpf_call_base(0, 0, 0, 0, 0) != 0);
447 static bool convert_bpf_ld_abs(struct sock_filter *fp, struct bpf_insn **insnp)
449 const bool unaligned_ok = IS_BUILTIN(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS);
450 int size = bpf_size_to_bytes(BPF_SIZE(fp->code));
451 bool endian = BPF_SIZE(fp->code) == BPF_H ||
452 BPF_SIZE(fp->code) == BPF_W;
453 bool indirect = BPF_MODE(fp->code) == BPF_IND;
454 const int ip_align = NET_IP_ALIGN;
455 struct bpf_insn *insn = *insnp;
459 ((unaligned_ok && offset >= 0) ||
460 (!unaligned_ok && offset >= 0 &&
461 offset + ip_align >= 0 &&
462 offset + ip_align % size == 0))) {
463 bool ldx_off_ok = offset <= S16_MAX;
465 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_H);
467 *insn++ = BPF_ALU64_IMM(BPF_SUB, BPF_REG_TMP, offset);
468 *insn++ = BPF_JMP_IMM(BPF_JSLT, BPF_REG_TMP,
469 size, 2 + endian + (!ldx_off_ok * 2));
471 *insn++ = BPF_LDX_MEM(BPF_SIZE(fp->code), BPF_REG_A,
474 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_D);
475 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_TMP, offset);
476 *insn++ = BPF_LDX_MEM(BPF_SIZE(fp->code), BPF_REG_A,
480 *insn++ = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, size * 8);
481 *insn++ = BPF_JMP_A(8);
484 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX);
485 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_D);
486 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_H);
488 *insn++ = BPF_MOV64_IMM(BPF_REG_ARG4, offset);
490 *insn++ = BPF_MOV64_REG(BPF_REG_ARG4, BPF_REG_X);
492 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_ARG4, offset);
495 switch (BPF_SIZE(fp->code)) {
497 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_8);
500 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_16);
503 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_32);
509 *insn++ = BPF_JMP_IMM(BPF_JSGE, BPF_REG_A, 0, 2);
510 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
511 *insn = BPF_EXIT_INSN();
518 * bpf_convert_filter - convert filter program
519 * @prog: the user passed filter program
520 * @len: the length of the user passed filter program
521 * @new_prog: allocated 'struct bpf_prog' or NULL
522 * @new_len: pointer to store length of converted program
523 * @seen_ld_abs: bool whether we've seen ld_abs/ind
525 * Remap 'sock_filter' style classic BPF (cBPF) instruction set to 'bpf_insn'
526 * style extended BPF (eBPF).
527 * Conversion workflow:
529 * 1) First pass for calculating the new program length:
530 * bpf_convert_filter(old_prog, old_len, NULL, &new_len, &seen_ld_abs)
532 * 2) 2nd pass to remap in two passes: 1st pass finds new
533 * jump offsets, 2nd pass remapping:
534 * bpf_convert_filter(old_prog, old_len, new_prog, &new_len, &seen_ld_abs)
536 static int bpf_convert_filter(struct sock_filter *prog, int len,
537 struct bpf_prog *new_prog, int *new_len,
540 int new_flen = 0, pass = 0, target, i, stack_off;
541 struct bpf_insn *new_insn, *first_insn = NULL;
542 struct sock_filter *fp;
546 BUILD_BUG_ON(BPF_MEMWORDS * sizeof(u32) > MAX_BPF_STACK);
547 BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG);
549 if (len <= 0 || len > BPF_MAXINSNS)
553 first_insn = new_prog->insnsi;
554 addrs = kcalloc(len, sizeof(*addrs),
555 GFP_KERNEL | __GFP_NOWARN);
561 new_insn = first_insn;
564 /* Classic BPF related prologue emission. */
566 /* Classic BPF expects A and X to be reset first. These need
567 * to be guaranteed to be the first two instructions.
569 *new_insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
570 *new_insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_X, BPF_REG_X);
572 /* All programs must keep CTX in callee saved BPF_REG_CTX.
573 * In eBPF case it's done by the compiler, here we need to
574 * do this ourself. Initial CTX is present in BPF_REG_ARG1.
576 *new_insn++ = BPF_MOV64_REG(BPF_REG_CTX, BPF_REG_ARG1);
578 /* For packet access in classic BPF, cache skb->data
579 * in callee-saved BPF R8 and skb->len - skb->data_len
580 * (headlen) in BPF R9. Since classic BPF is read-only
581 * on CTX, we only need to cache it once.
583 *new_insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
584 BPF_REG_D, BPF_REG_CTX,
585 offsetof(struct sk_buff, data));
586 *new_insn++ = BPF_LDX_MEM(BPF_W, BPF_REG_H, BPF_REG_CTX,
587 offsetof(struct sk_buff, len));
588 *new_insn++ = BPF_LDX_MEM(BPF_W, BPF_REG_TMP, BPF_REG_CTX,
589 offsetof(struct sk_buff, data_len));
590 *new_insn++ = BPF_ALU32_REG(BPF_SUB, BPF_REG_H, BPF_REG_TMP);
596 for (i = 0; i < len; fp++, i++) {
597 struct bpf_insn tmp_insns[32] = { };
598 struct bpf_insn *insn = tmp_insns;
601 addrs[i] = new_insn - first_insn;
604 /* All arithmetic insns and skb loads map as-is. */
605 case BPF_ALU | BPF_ADD | BPF_X:
606 case BPF_ALU | BPF_ADD | BPF_K:
607 case BPF_ALU | BPF_SUB | BPF_X:
608 case BPF_ALU | BPF_SUB | BPF_K:
609 case BPF_ALU | BPF_AND | BPF_X:
610 case BPF_ALU | BPF_AND | BPF_K:
611 case BPF_ALU | BPF_OR | BPF_X:
612 case BPF_ALU | BPF_OR | BPF_K:
613 case BPF_ALU | BPF_LSH | BPF_X:
614 case BPF_ALU | BPF_LSH | BPF_K:
615 case BPF_ALU | BPF_RSH | BPF_X:
616 case BPF_ALU | BPF_RSH | BPF_K:
617 case BPF_ALU | BPF_XOR | BPF_X:
618 case BPF_ALU | BPF_XOR | BPF_K:
619 case BPF_ALU | BPF_MUL | BPF_X:
620 case BPF_ALU | BPF_MUL | BPF_K:
621 case BPF_ALU | BPF_DIV | BPF_X:
622 case BPF_ALU | BPF_DIV | BPF_K:
623 case BPF_ALU | BPF_MOD | BPF_X:
624 case BPF_ALU | BPF_MOD | BPF_K:
625 case BPF_ALU | BPF_NEG:
626 case BPF_LD | BPF_ABS | BPF_W:
627 case BPF_LD | BPF_ABS | BPF_H:
628 case BPF_LD | BPF_ABS | BPF_B:
629 case BPF_LD | BPF_IND | BPF_W:
630 case BPF_LD | BPF_IND | BPF_H:
631 case BPF_LD | BPF_IND | BPF_B:
632 /* Check for overloaded BPF extension and
633 * directly convert it if found, otherwise
634 * just move on with mapping.
636 if (BPF_CLASS(fp->code) == BPF_LD &&
637 BPF_MODE(fp->code) == BPF_ABS &&
638 convert_bpf_extensions(fp, &insn))
640 if (BPF_CLASS(fp->code) == BPF_LD &&
641 convert_bpf_ld_abs(fp, &insn)) {
646 if (fp->code == (BPF_ALU | BPF_DIV | BPF_X) ||
647 fp->code == (BPF_ALU | BPF_MOD | BPF_X)) {
648 *insn++ = BPF_MOV32_REG(BPF_REG_X, BPF_REG_X);
649 /* Error with exception code on div/mod by 0.
650 * For cBPF programs, this was always return 0.
652 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_X, 0, 2);
653 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
654 *insn++ = BPF_EXIT_INSN();
657 *insn = BPF_RAW_INSN(fp->code, BPF_REG_A, BPF_REG_X, 0, fp->k);
660 /* Jump transformation cannot use BPF block macros
661 * everywhere as offset calculation and target updates
662 * require a bit more work than the rest, i.e. jump
663 * opcodes map as-is, but offsets need adjustment.
666 #define BPF_EMIT_JMP \
668 const s32 off_min = S16_MIN, off_max = S16_MAX; \
671 if (target >= len || target < 0) \
673 off = addrs ? addrs[target] - addrs[i] - 1 : 0; \
674 /* Adjust pc relative offset for 2nd or 3rd insn. */ \
675 off -= insn - tmp_insns; \
676 /* Reject anything not fitting into insn->off. */ \
677 if (off < off_min || off > off_max) \
682 case BPF_JMP | BPF_JA:
683 target = i + fp->k + 1;
684 insn->code = fp->code;
688 case BPF_JMP | BPF_JEQ | BPF_K:
689 case BPF_JMP | BPF_JEQ | BPF_X:
690 case BPF_JMP | BPF_JSET | BPF_K:
691 case BPF_JMP | BPF_JSET | BPF_X:
692 case BPF_JMP | BPF_JGT | BPF_K:
693 case BPF_JMP | BPF_JGT | BPF_X:
694 case BPF_JMP | BPF_JGE | BPF_K:
695 case BPF_JMP | BPF_JGE | BPF_X:
696 if (BPF_SRC(fp->code) == BPF_K && (int) fp->k < 0) {
697 /* BPF immediates are signed, zero extend
698 * immediate into tmp register and use it
701 *insn++ = BPF_MOV32_IMM(BPF_REG_TMP, fp->k);
703 insn->dst_reg = BPF_REG_A;
704 insn->src_reg = BPF_REG_TMP;
707 insn->dst_reg = BPF_REG_A;
709 bpf_src = BPF_SRC(fp->code);
710 insn->src_reg = bpf_src == BPF_X ? BPF_REG_X : 0;
713 /* Common case where 'jump_false' is next insn. */
715 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
716 target = i + fp->jt + 1;
721 /* Convert some jumps when 'jump_true' is next insn. */
723 switch (BPF_OP(fp->code)) {
725 insn->code = BPF_JMP | BPF_JNE | bpf_src;
728 insn->code = BPF_JMP | BPF_JLE | bpf_src;
731 insn->code = BPF_JMP | BPF_JLT | bpf_src;
737 target = i + fp->jf + 1;
742 /* Other jumps are mapped into two insns: Jxx and JA. */
743 target = i + fp->jt + 1;
744 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
748 insn->code = BPF_JMP | BPF_JA;
749 target = i + fp->jf + 1;
753 /* ldxb 4 * ([14] & 0xf) is remaped into 6 insns. */
754 case BPF_LDX | BPF_MSH | BPF_B: {
755 struct sock_filter tmp = {
756 .code = BPF_LD | BPF_ABS | BPF_B,
763 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
764 /* A = BPF_R0 = *(u8 *) (skb->data + K) */
765 convert_bpf_ld_abs(&tmp, &insn);
768 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, 0xf);
770 *insn++ = BPF_ALU32_IMM(BPF_LSH, BPF_REG_A, 2);
772 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_X);
774 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
776 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_TMP);
779 /* RET_K is remaped into 2 insns. RET_A case doesn't need an
780 * extra mov as BPF_REG_0 is already mapped into BPF_REG_A.
782 case BPF_RET | BPF_A:
783 case BPF_RET | BPF_K:
784 if (BPF_RVAL(fp->code) == BPF_K)
785 *insn++ = BPF_MOV32_RAW(BPF_K, BPF_REG_0,
787 *insn = BPF_EXIT_INSN();
790 /* Store to stack. */
793 stack_off = fp->k * 4 + 4;
794 *insn = BPF_STX_MEM(BPF_W, BPF_REG_FP, BPF_CLASS(fp->code) ==
795 BPF_ST ? BPF_REG_A : BPF_REG_X,
797 /* check_load_and_stores() verifies that classic BPF can
798 * load from stack only after write, so tracking
799 * stack_depth for ST|STX insns is enough
801 if (new_prog && new_prog->aux->stack_depth < stack_off)
802 new_prog->aux->stack_depth = stack_off;
805 /* Load from stack. */
806 case BPF_LD | BPF_MEM:
807 case BPF_LDX | BPF_MEM:
808 stack_off = fp->k * 4 + 4;
809 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
810 BPF_REG_A : BPF_REG_X, BPF_REG_FP,
815 case BPF_LD | BPF_IMM:
816 case BPF_LDX | BPF_IMM:
817 *insn = BPF_MOV32_IMM(BPF_CLASS(fp->code) == BPF_LD ?
818 BPF_REG_A : BPF_REG_X, fp->k);
822 case BPF_MISC | BPF_TAX:
823 *insn = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
827 case BPF_MISC | BPF_TXA:
828 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_X);
831 /* A = skb->len or X = skb->len */
832 case BPF_LD | BPF_W | BPF_LEN:
833 case BPF_LDX | BPF_W | BPF_LEN:
834 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
835 BPF_REG_A : BPF_REG_X, BPF_REG_CTX,
836 offsetof(struct sk_buff, len));
839 /* Access seccomp_data fields. */
840 case BPF_LDX | BPF_ABS | BPF_W:
841 /* A = *(u32 *) (ctx + K) */
842 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX, fp->k);
845 /* Unknown instruction. */
852 memcpy(new_insn, tmp_insns,
853 sizeof(*insn) * (insn - tmp_insns));
854 new_insn += insn - tmp_insns;
858 /* Only calculating new length. */
859 *new_len = new_insn - first_insn;
861 *new_len += 4; /* Prologue bits. */
866 if (new_flen != new_insn - first_insn) {
867 new_flen = new_insn - first_insn;
874 BUG_ON(*new_len != new_flen);
883 * As we dont want to clear mem[] array for each packet going through
884 * __bpf_prog_run(), we check that filter loaded by user never try to read
885 * a cell if not previously written, and we check all branches to be sure
886 * a malicious user doesn't try to abuse us.
888 static int check_load_and_stores(const struct sock_filter *filter, int flen)
890 u16 *masks, memvalid = 0; /* One bit per cell, 16 cells */
893 BUILD_BUG_ON(BPF_MEMWORDS > 16);
895 masks = kmalloc_array(flen, sizeof(*masks), GFP_KERNEL);
899 memset(masks, 0xff, flen * sizeof(*masks));
901 for (pc = 0; pc < flen; pc++) {
902 memvalid &= masks[pc];
904 switch (filter[pc].code) {
907 memvalid |= (1 << filter[pc].k);
909 case BPF_LD | BPF_MEM:
910 case BPF_LDX | BPF_MEM:
911 if (!(memvalid & (1 << filter[pc].k))) {
916 case BPF_JMP | BPF_JA:
917 /* A jump must set masks on target */
918 masks[pc + 1 + filter[pc].k] &= memvalid;
921 case BPF_JMP | BPF_JEQ | BPF_K:
922 case BPF_JMP | BPF_JEQ | BPF_X:
923 case BPF_JMP | BPF_JGE | BPF_K:
924 case BPF_JMP | BPF_JGE | BPF_X:
925 case BPF_JMP | BPF_JGT | BPF_K:
926 case BPF_JMP | BPF_JGT | BPF_X:
927 case BPF_JMP | BPF_JSET | BPF_K:
928 case BPF_JMP | BPF_JSET | BPF_X:
929 /* A jump must set masks on targets */
930 masks[pc + 1 + filter[pc].jt] &= memvalid;
931 masks[pc + 1 + filter[pc].jf] &= memvalid;
941 static bool chk_code_allowed(u16 code_to_probe)
943 static const bool codes[] = {
944 /* 32 bit ALU operations */
945 [BPF_ALU | BPF_ADD | BPF_K] = true,
946 [BPF_ALU | BPF_ADD | BPF_X] = true,
947 [BPF_ALU | BPF_SUB | BPF_K] = true,
948 [BPF_ALU | BPF_SUB | BPF_X] = true,
949 [BPF_ALU | BPF_MUL | BPF_K] = true,
950 [BPF_ALU | BPF_MUL | BPF_X] = true,
951 [BPF_ALU | BPF_DIV | BPF_K] = true,
952 [BPF_ALU | BPF_DIV | BPF_X] = true,
953 [BPF_ALU | BPF_MOD | BPF_K] = true,
954 [BPF_ALU | BPF_MOD | BPF_X] = true,
955 [BPF_ALU | BPF_AND | BPF_K] = true,
956 [BPF_ALU | BPF_AND | BPF_X] = true,
957 [BPF_ALU | BPF_OR | BPF_K] = true,
958 [BPF_ALU | BPF_OR | BPF_X] = true,
959 [BPF_ALU | BPF_XOR | BPF_K] = true,
960 [BPF_ALU | BPF_XOR | BPF_X] = true,
961 [BPF_ALU | BPF_LSH | BPF_K] = true,
962 [BPF_ALU | BPF_LSH | BPF_X] = true,
963 [BPF_ALU | BPF_RSH | BPF_K] = true,
964 [BPF_ALU | BPF_RSH | BPF_X] = true,
965 [BPF_ALU | BPF_NEG] = true,
966 /* Load instructions */
967 [BPF_LD | BPF_W | BPF_ABS] = true,
968 [BPF_LD | BPF_H | BPF_ABS] = true,
969 [BPF_LD | BPF_B | BPF_ABS] = true,
970 [BPF_LD | BPF_W | BPF_LEN] = true,
971 [BPF_LD | BPF_W | BPF_IND] = true,
972 [BPF_LD | BPF_H | BPF_IND] = true,
973 [BPF_LD | BPF_B | BPF_IND] = true,
974 [BPF_LD | BPF_IMM] = true,
975 [BPF_LD | BPF_MEM] = true,
976 [BPF_LDX | BPF_W | BPF_LEN] = true,
977 [BPF_LDX | BPF_B | BPF_MSH] = true,
978 [BPF_LDX | BPF_IMM] = true,
979 [BPF_LDX | BPF_MEM] = true,
980 /* Store instructions */
983 /* Misc instructions */
984 [BPF_MISC | BPF_TAX] = true,
985 [BPF_MISC | BPF_TXA] = true,
986 /* Return instructions */
987 [BPF_RET | BPF_K] = true,
988 [BPF_RET | BPF_A] = true,
989 /* Jump instructions */
990 [BPF_JMP | BPF_JA] = true,
991 [BPF_JMP | BPF_JEQ | BPF_K] = true,
992 [BPF_JMP | BPF_JEQ | BPF_X] = true,
993 [BPF_JMP | BPF_JGE | BPF_K] = true,
994 [BPF_JMP | BPF_JGE | BPF_X] = true,
995 [BPF_JMP | BPF_JGT | BPF_K] = true,
996 [BPF_JMP | BPF_JGT | BPF_X] = true,
997 [BPF_JMP | BPF_JSET | BPF_K] = true,
998 [BPF_JMP | BPF_JSET | BPF_X] = true,
1001 if (code_to_probe >= ARRAY_SIZE(codes))
1004 return codes[code_to_probe];
1007 static bool bpf_check_basics_ok(const struct sock_filter *filter,
1012 if (flen == 0 || flen > BPF_MAXINSNS)
1019 * bpf_check_classic - verify socket filter code
1020 * @filter: filter to verify
1021 * @flen: length of filter
1023 * Check the user's filter code. If we let some ugly
1024 * filter code slip through kaboom! The filter must contain
1025 * no references or jumps that are out of range, no illegal
1026 * instructions, and must end with a RET instruction.
1028 * All jumps are forward as they are not signed.
1030 * Returns 0 if the rule set is legal or -EINVAL if not.
1032 static int bpf_check_classic(const struct sock_filter *filter,
1038 /* Check the filter code now */
1039 for (pc = 0; pc < flen; pc++) {
1040 const struct sock_filter *ftest = &filter[pc];
1042 /* May we actually operate on this code? */
1043 if (!chk_code_allowed(ftest->code))
1046 /* Some instructions need special checks */
1047 switch (ftest->code) {
1048 case BPF_ALU | BPF_DIV | BPF_K:
1049 case BPF_ALU | BPF_MOD | BPF_K:
1050 /* Check for division by zero */
1054 case BPF_ALU | BPF_LSH | BPF_K:
1055 case BPF_ALU | BPF_RSH | BPF_K:
1059 case BPF_LD | BPF_MEM:
1060 case BPF_LDX | BPF_MEM:
1063 /* Check for invalid memory addresses */
1064 if (ftest->k >= BPF_MEMWORDS)
1067 case BPF_JMP | BPF_JA:
1068 /* Note, the large ftest->k might cause loops.
1069 * Compare this with conditional jumps below,
1070 * where offsets are limited. --ANK (981016)
1072 if (ftest->k >= (unsigned int)(flen - pc - 1))
1075 case BPF_JMP | BPF_JEQ | BPF_K:
1076 case BPF_JMP | BPF_JEQ | BPF_X:
1077 case BPF_JMP | BPF_JGE | BPF_K:
1078 case BPF_JMP | BPF_JGE | BPF_X:
1079 case BPF_JMP | BPF_JGT | BPF_K:
1080 case BPF_JMP | BPF_JGT | BPF_X:
1081 case BPF_JMP | BPF_JSET | BPF_K:
1082 case BPF_JMP | BPF_JSET | BPF_X:
1083 /* Both conditionals must be safe */
1084 if (pc + ftest->jt + 1 >= flen ||
1085 pc + ftest->jf + 1 >= flen)
1088 case BPF_LD | BPF_W | BPF_ABS:
1089 case BPF_LD | BPF_H | BPF_ABS:
1090 case BPF_LD | BPF_B | BPF_ABS:
1092 if (bpf_anc_helper(ftest) & BPF_ANC)
1094 /* Ancillary operation unknown or unsupported */
1095 if (anc_found == false && ftest->k >= SKF_AD_OFF)
1100 /* Last instruction must be a RET code */
1101 switch (filter[flen - 1].code) {
1102 case BPF_RET | BPF_K:
1103 case BPF_RET | BPF_A:
1104 return check_load_and_stores(filter, flen);
1110 static int bpf_prog_store_orig_filter(struct bpf_prog *fp,
1111 const struct sock_fprog *fprog)
1113 unsigned int fsize = bpf_classic_proglen(fprog);
1114 struct sock_fprog_kern *fkprog;
1116 fp->orig_prog = kmalloc(sizeof(*fkprog), GFP_KERNEL);
1120 fkprog = fp->orig_prog;
1121 fkprog->len = fprog->len;
1123 fkprog->filter = kmemdup(fp->insns, fsize,
1124 GFP_KERNEL | __GFP_NOWARN);
1125 if (!fkprog->filter) {
1126 kfree(fp->orig_prog);
1133 static void bpf_release_orig_filter(struct bpf_prog *fp)
1135 struct sock_fprog_kern *fprog = fp->orig_prog;
1138 kfree(fprog->filter);
1143 static void __bpf_prog_release(struct bpf_prog *prog)
1145 if (prog->type == BPF_PROG_TYPE_SOCKET_FILTER) {
1148 bpf_release_orig_filter(prog);
1149 bpf_prog_free(prog);
1153 static void __sk_filter_release(struct sk_filter *fp)
1155 __bpf_prog_release(fp->prog);
1160 * sk_filter_release_rcu - Release a socket filter by rcu_head
1161 * @rcu: rcu_head that contains the sk_filter to free
1163 static void sk_filter_release_rcu(struct rcu_head *rcu)
1165 struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu);
1167 __sk_filter_release(fp);
1171 * sk_filter_release - release a socket filter
1172 * @fp: filter to remove
1174 * Remove a filter from a socket and release its resources.
1176 static void sk_filter_release(struct sk_filter *fp)
1178 if (refcount_dec_and_test(&fp->refcnt))
1179 call_rcu(&fp->rcu, sk_filter_release_rcu);
1182 void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp)
1184 u32 filter_size = bpf_prog_size(fp->prog->len);
1186 atomic_sub(filter_size, &sk->sk_omem_alloc);
1187 sk_filter_release(fp);
1190 /* try to charge the socket memory if there is space available
1191 * return true on success
1193 static bool __sk_filter_charge(struct sock *sk, struct sk_filter *fp)
1195 u32 filter_size = bpf_prog_size(fp->prog->len);
1197 /* same check as in sock_kmalloc() */
1198 if (filter_size <= sysctl_optmem_max &&
1199 atomic_read(&sk->sk_omem_alloc) + filter_size < sysctl_optmem_max) {
1200 atomic_add(filter_size, &sk->sk_omem_alloc);
1206 bool sk_filter_charge(struct sock *sk, struct sk_filter *fp)
1208 if (!refcount_inc_not_zero(&fp->refcnt))
1211 if (!__sk_filter_charge(sk, fp)) {
1212 sk_filter_release(fp);
1218 static struct bpf_prog *bpf_migrate_filter(struct bpf_prog *fp)
1220 struct sock_filter *old_prog;
1221 struct bpf_prog *old_fp;
1222 int err, new_len, old_len = fp->len;
1223 bool seen_ld_abs = false;
1225 /* We are free to overwrite insns et al right here as it
1226 * won't be used at this point in time anymore internally
1227 * after the migration to the internal BPF instruction
1230 BUILD_BUG_ON(sizeof(struct sock_filter) !=
1231 sizeof(struct bpf_insn));
1233 /* Conversion cannot happen on overlapping memory areas,
1234 * so we need to keep the user BPF around until the 2nd
1235 * pass. At this time, the user BPF is stored in fp->insns.
1237 old_prog = kmemdup(fp->insns, old_len * sizeof(struct sock_filter),
1238 GFP_KERNEL | __GFP_NOWARN);
1244 /* 1st pass: calculate the new program length. */
1245 err = bpf_convert_filter(old_prog, old_len, NULL, &new_len,
1250 /* Expand fp for appending the new filter representation. */
1252 fp = bpf_prog_realloc(old_fp, bpf_prog_size(new_len), 0);
1254 /* The old_fp is still around in case we couldn't
1255 * allocate new memory, so uncharge on that one.
1264 /* 2nd pass: remap sock_filter insns into bpf_insn insns. */
1265 err = bpf_convert_filter(old_prog, old_len, fp, &new_len,
1268 /* 2nd bpf_convert_filter() can fail only if it fails
1269 * to allocate memory, remapping must succeed. Note,
1270 * that at this time old_fp has already been released
1275 fp = bpf_prog_select_runtime(fp, &err);
1285 __bpf_prog_release(fp);
1286 return ERR_PTR(err);
1289 static struct bpf_prog *bpf_prepare_filter(struct bpf_prog *fp,
1290 bpf_aux_classic_check_t trans)
1294 fp->bpf_func = NULL;
1297 err = bpf_check_classic(fp->insns, fp->len);
1299 __bpf_prog_release(fp);
1300 return ERR_PTR(err);
1303 /* There might be additional checks and transformations
1304 * needed on classic filters, f.e. in case of seccomp.
1307 err = trans(fp->insns, fp->len);
1309 __bpf_prog_release(fp);
1310 return ERR_PTR(err);
1314 /* Probe if we can JIT compile the filter and if so, do
1315 * the compilation of the filter.
1317 bpf_jit_compile(fp);
1319 /* JIT compiler couldn't process this filter, so do the
1320 * internal BPF translation for the optimized interpreter.
1323 fp = bpf_migrate_filter(fp);
1329 * bpf_prog_create - create an unattached filter
1330 * @pfp: the unattached filter that is created
1331 * @fprog: the filter program
1333 * Create a filter independent of any socket. We first run some
1334 * sanity checks on it to make sure it does not explode on us later.
1335 * If an error occurs or there is insufficient memory for the filter
1336 * a negative errno code is returned. On success the return is zero.
1338 int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog)
1340 unsigned int fsize = bpf_classic_proglen(fprog);
1341 struct bpf_prog *fp;
1343 /* Make sure new filter is there and in the right amounts. */
1344 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1347 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1351 memcpy(fp->insns, fprog->filter, fsize);
1353 fp->len = fprog->len;
1354 /* Since unattached filters are not copied back to user
1355 * space through sk_get_filter(), we do not need to hold
1356 * a copy here, and can spare us the work.
1358 fp->orig_prog = NULL;
1360 /* bpf_prepare_filter() already takes care of freeing
1361 * memory in case something goes wrong.
1363 fp = bpf_prepare_filter(fp, NULL);
1370 EXPORT_SYMBOL_GPL(bpf_prog_create);
1373 * bpf_prog_create_from_user - create an unattached filter from user buffer
1374 * @pfp: the unattached filter that is created
1375 * @fprog: the filter program
1376 * @trans: post-classic verifier transformation handler
1377 * @save_orig: save classic BPF program
1379 * This function effectively does the same as bpf_prog_create(), only
1380 * that it builds up its insns buffer from user space provided buffer.
1381 * It also allows for passing a bpf_aux_classic_check_t handler.
1383 int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog,
1384 bpf_aux_classic_check_t trans, bool save_orig)
1386 unsigned int fsize = bpf_classic_proglen(fprog);
1387 struct bpf_prog *fp;
1390 /* Make sure new filter is there and in the right amounts. */
1391 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1394 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1398 if (copy_from_user(fp->insns, fprog->filter, fsize)) {
1399 __bpf_prog_free(fp);
1403 fp->len = fprog->len;
1404 fp->orig_prog = NULL;
1407 err = bpf_prog_store_orig_filter(fp, fprog);
1409 __bpf_prog_free(fp);
1414 /* bpf_prepare_filter() already takes care of freeing
1415 * memory in case something goes wrong.
1417 fp = bpf_prepare_filter(fp, trans);
1424 EXPORT_SYMBOL_GPL(bpf_prog_create_from_user);
1426 void bpf_prog_destroy(struct bpf_prog *fp)
1428 __bpf_prog_release(fp);
1430 EXPORT_SYMBOL_GPL(bpf_prog_destroy);
1432 static int __sk_attach_prog(struct bpf_prog *prog, struct sock *sk)
1434 struct sk_filter *fp, *old_fp;
1436 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
1442 if (!__sk_filter_charge(sk, fp)) {
1446 refcount_set(&fp->refcnt, 1);
1448 old_fp = rcu_dereference_protected(sk->sk_filter,
1449 lockdep_sock_is_held(sk));
1450 rcu_assign_pointer(sk->sk_filter, fp);
1453 sk_filter_uncharge(sk, old_fp);
1459 struct bpf_prog *__get_filter(struct sock_fprog *fprog, struct sock *sk)
1461 unsigned int fsize = bpf_classic_proglen(fprog);
1462 struct bpf_prog *prog;
1465 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1466 return ERR_PTR(-EPERM);
1468 /* Make sure new filter is there and in the right amounts. */
1469 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1470 return ERR_PTR(-EINVAL);
1472 prog = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1474 return ERR_PTR(-ENOMEM);
1476 if (copy_from_user(prog->insns, fprog->filter, fsize)) {
1477 __bpf_prog_free(prog);
1478 return ERR_PTR(-EFAULT);
1481 prog->len = fprog->len;
1483 err = bpf_prog_store_orig_filter(prog, fprog);
1485 __bpf_prog_free(prog);
1486 return ERR_PTR(-ENOMEM);
1489 /* bpf_prepare_filter() already takes care of freeing
1490 * memory in case something goes wrong.
1492 return bpf_prepare_filter(prog, NULL);
1496 * sk_attach_filter - attach a socket filter
1497 * @fprog: the filter program
1498 * @sk: the socket to use
1500 * Attach the user's filter code. We first run some sanity checks on
1501 * it to make sure it does not explode on us later. If an error
1502 * occurs or there is insufficient memory for the filter a negative
1503 * errno code is returned. On success the return is zero.
1505 int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1507 struct bpf_prog *prog = __get_filter(fprog, sk);
1511 return PTR_ERR(prog);
1513 err = __sk_attach_prog(prog, sk);
1515 __bpf_prog_release(prog);
1521 EXPORT_SYMBOL_GPL(sk_attach_filter);
1523 int sk_reuseport_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1525 struct bpf_prog *prog = __get_filter(fprog, sk);
1529 return PTR_ERR(prog);
1531 if (bpf_prog_size(prog->len) > sysctl_optmem_max)
1534 err = reuseport_attach_prog(sk, prog);
1537 __bpf_prog_release(prog);
1542 static struct bpf_prog *__get_bpf(u32 ufd, struct sock *sk)
1544 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1545 return ERR_PTR(-EPERM);
1547 return bpf_prog_get_type(ufd, BPF_PROG_TYPE_SOCKET_FILTER);
1550 int sk_attach_bpf(u32 ufd, struct sock *sk)
1552 struct bpf_prog *prog = __get_bpf(ufd, sk);
1556 return PTR_ERR(prog);
1558 err = __sk_attach_prog(prog, sk);
1567 int sk_reuseport_attach_bpf(u32 ufd, struct sock *sk)
1569 struct bpf_prog *prog;
1572 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1575 prog = bpf_prog_get_type(ufd, BPF_PROG_TYPE_SOCKET_FILTER);
1576 if (IS_ERR(prog) && PTR_ERR(prog) == -EINVAL)
1577 prog = bpf_prog_get_type(ufd, BPF_PROG_TYPE_SK_REUSEPORT);
1579 return PTR_ERR(prog);
1581 if (prog->type == BPF_PROG_TYPE_SK_REUSEPORT) {
1582 /* Like other non BPF_PROG_TYPE_SOCKET_FILTER
1583 * bpf prog (e.g. sockmap). It depends on the
1584 * limitation imposed by bpf_prog_load().
1585 * Hence, sysctl_optmem_max is not checked.
1587 if ((sk->sk_type != SOCK_STREAM &&
1588 sk->sk_type != SOCK_DGRAM) ||
1589 (sk->sk_protocol != IPPROTO_UDP &&
1590 sk->sk_protocol != IPPROTO_TCP) ||
1591 (sk->sk_family != AF_INET &&
1592 sk->sk_family != AF_INET6)) {
1597 /* BPF_PROG_TYPE_SOCKET_FILTER */
1598 if (bpf_prog_size(prog->len) > sysctl_optmem_max) {
1604 err = reuseport_attach_prog(sk, prog);
1612 void sk_reuseport_prog_free(struct bpf_prog *prog)
1617 if (prog->type == BPF_PROG_TYPE_SK_REUSEPORT)
1620 bpf_prog_destroy(prog);
1623 struct bpf_scratchpad {
1625 __be32 diff[MAX_BPF_STACK / sizeof(__be32)];
1626 u8 buff[MAX_BPF_STACK];
1630 static DEFINE_PER_CPU(struct bpf_scratchpad, bpf_sp);
1632 static inline int __bpf_try_make_writable(struct sk_buff *skb,
1633 unsigned int write_len)
1635 return skb_ensure_writable(skb, write_len);
1638 static inline int bpf_try_make_writable(struct sk_buff *skb,
1639 unsigned int write_len)
1641 int err = __bpf_try_make_writable(skb, write_len);
1643 bpf_compute_data_pointers(skb);
1647 static int bpf_try_make_head_writable(struct sk_buff *skb)
1649 return bpf_try_make_writable(skb, skb_headlen(skb));
1652 static inline void bpf_push_mac_rcsum(struct sk_buff *skb)
1654 if (skb_at_tc_ingress(skb))
1655 skb_postpush_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1658 static inline void bpf_pull_mac_rcsum(struct sk_buff *skb)
1660 if (skb_at_tc_ingress(skb))
1661 skb_postpull_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1664 BPF_CALL_5(bpf_skb_store_bytes, struct sk_buff *, skb, u32, offset,
1665 const void *, from, u32, len, u64, flags)
1669 if (unlikely(flags & ~(BPF_F_RECOMPUTE_CSUM | BPF_F_INVALIDATE_HASH)))
1671 if (unlikely(offset > 0xffff))
1673 if (unlikely(bpf_try_make_writable(skb, offset + len)))
1676 ptr = skb->data + offset;
1677 if (flags & BPF_F_RECOMPUTE_CSUM)
1678 __skb_postpull_rcsum(skb, ptr, len, offset);
1680 memcpy(ptr, from, len);
1682 if (flags & BPF_F_RECOMPUTE_CSUM)
1683 __skb_postpush_rcsum(skb, ptr, len, offset);
1684 if (flags & BPF_F_INVALIDATE_HASH)
1685 skb_clear_hash(skb);
1690 static const struct bpf_func_proto bpf_skb_store_bytes_proto = {
1691 .func = bpf_skb_store_bytes,
1693 .ret_type = RET_INTEGER,
1694 .arg1_type = ARG_PTR_TO_CTX,
1695 .arg2_type = ARG_ANYTHING,
1696 .arg3_type = ARG_PTR_TO_MEM,
1697 .arg4_type = ARG_CONST_SIZE,
1698 .arg5_type = ARG_ANYTHING,
1701 BPF_CALL_4(bpf_skb_load_bytes, const struct sk_buff *, skb, u32, offset,
1702 void *, to, u32, len)
1706 if (unlikely(offset > 0xffff))
1709 ptr = skb_header_pointer(skb, offset, len, to);
1713 memcpy(to, ptr, len);
1721 static const struct bpf_func_proto bpf_skb_load_bytes_proto = {
1722 .func = bpf_skb_load_bytes,
1724 .ret_type = RET_INTEGER,
1725 .arg1_type = ARG_PTR_TO_CTX,
1726 .arg2_type = ARG_ANYTHING,
1727 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
1728 .arg4_type = ARG_CONST_SIZE,
1731 BPF_CALL_5(bpf_skb_load_bytes_relative, const struct sk_buff *, skb,
1732 u32, offset, void *, to, u32, len, u32, start_header)
1734 u8 *end = skb_tail_pointer(skb);
1735 u8 *net = skb_network_header(skb);
1736 u8 *mac = skb_mac_header(skb);
1739 if (unlikely(offset > 0xffff || len > (end - mac)))
1742 switch (start_header) {
1743 case BPF_HDR_START_MAC:
1746 case BPF_HDR_START_NET:
1753 if (likely(ptr >= mac && ptr + len <= end)) {
1754 memcpy(to, ptr, len);
1763 static const struct bpf_func_proto bpf_skb_load_bytes_relative_proto = {
1764 .func = bpf_skb_load_bytes_relative,
1766 .ret_type = RET_INTEGER,
1767 .arg1_type = ARG_PTR_TO_CTX,
1768 .arg2_type = ARG_ANYTHING,
1769 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
1770 .arg4_type = ARG_CONST_SIZE,
1771 .arg5_type = ARG_ANYTHING,
1774 BPF_CALL_2(bpf_skb_pull_data, struct sk_buff *, skb, u32, len)
1776 /* Idea is the following: should the needed direct read/write
1777 * test fail during runtime, we can pull in more data and redo
1778 * again, since implicitly, we invalidate previous checks here.
1780 * Or, since we know how much we need to make read/writeable,
1781 * this can be done once at the program beginning for direct
1782 * access case. By this we overcome limitations of only current
1783 * headroom being accessible.
1785 return bpf_try_make_writable(skb, len ? : skb_headlen(skb));
1788 static const struct bpf_func_proto bpf_skb_pull_data_proto = {
1789 .func = bpf_skb_pull_data,
1791 .ret_type = RET_INTEGER,
1792 .arg1_type = ARG_PTR_TO_CTX,
1793 .arg2_type = ARG_ANYTHING,
1796 static inline int sk_skb_try_make_writable(struct sk_buff *skb,
1797 unsigned int write_len)
1799 int err = __bpf_try_make_writable(skb, write_len);
1801 bpf_compute_data_end_sk_skb(skb);
1805 BPF_CALL_2(sk_skb_pull_data, struct sk_buff *, skb, u32, len)
1807 /* Idea is the following: should the needed direct read/write
1808 * test fail during runtime, we can pull in more data and redo
1809 * again, since implicitly, we invalidate previous checks here.
1811 * Or, since we know how much we need to make read/writeable,
1812 * this can be done once at the program beginning for direct
1813 * access case. By this we overcome limitations of only current
1814 * headroom being accessible.
1816 return sk_skb_try_make_writable(skb, len ? : skb_headlen(skb));
1819 static const struct bpf_func_proto sk_skb_pull_data_proto = {
1820 .func = sk_skb_pull_data,
1822 .ret_type = RET_INTEGER,
1823 .arg1_type = ARG_PTR_TO_CTX,
1824 .arg2_type = ARG_ANYTHING,
1827 BPF_CALL_5(bpf_l3_csum_replace, struct sk_buff *, skb, u32, offset,
1828 u64, from, u64, to, u64, flags)
1832 if (unlikely(flags & ~(BPF_F_HDR_FIELD_MASK)))
1834 if (unlikely(offset > 0xffff || offset & 1))
1836 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
1839 ptr = (__sum16 *)(skb->data + offset);
1840 switch (flags & BPF_F_HDR_FIELD_MASK) {
1842 if (unlikely(from != 0))
1845 csum_replace_by_diff(ptr, to);
1848 csum_replace2(ptr, from, to);
1851 csum_replace4(ptr, from, to);
1860 static const struct bpf_func_proto bpf_l3_csum_replace_proto = {
1861 .func = bpf_l3_csum_replace,
1863 .ret_type = RET_INTEGER,
1864 .arg1_type = ARG_PTR_TO_CTX,
1865 .arg2_type = ARG_ANYTHING,
1866 .arg3_type = ARG_ANYTHING,
1867 .arg4_type = ARG_ANYTHING,
1868 .arg5_type = ARG_ANYTHING,
1871 BPF_CALL_5(bpf_l4_csum_replace, struct sk_buff *, skb, u32, offset,
1872 u64, from, u64, to, u64, flags)
1874 bool is_pseudo = flags & BPF_F_PSEUDO_HDR;
1875 bool is_mmzero = flags & BPF_F_MARK_MANGLED_0;
1876 bool do_mforce = flags & BPF_F_MARK_ENFORCE;
1879 if (unlikely(flags & ~(BPF_F_MARK_MANGLED_0 | BPF_F_MARK_ENFORCE |
1880 BPF_F_PSEUDO_HDR | BPF_F_HDR_FIELD_MASK)))
1882 if (unlikely(offset > 0xffff || offset & 1))
1884 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
1887 ptr = (__sum16 *)(skb->data + offset);
1888 if (is_mmzero && !do_mforce && !*ptr)
1891 switch (flags & BPF_F_HDR_FIELD_MASK) {
1893 if (unlikely(from != 0))
1896 inet_proto_csum_replace_by_diff(ptr, skb, to, is_pseudo);
1899 inet_proto_csum_replace2(ptr, skb, from, to, is_pseudo);
1902 inet_proto_csum_replace4(ptr, skb, from, to, is_pseudo);
1908 if (is_mmzero && !*ptr)
1909 *ptr = CSUM_MANGLED_0;
1913 static const struct bpf_func_proto bpf_l4_csum_replace_proto = {
1914 .func = bpf_l4_csum_replace,
1916 .ret_type = RET_INTEGER,
1917 .arg1_type = ARG_PTR_TO_CTX,
1918 .arg2_type = ARG_ANYTHING,
1919 .arg3_type = ARG_ANYTHING,
1920 .arg4_type = ARG_ANYTHING,
1921 .arg5_type = ARG_ANYTHING,
1924 BPF_CALL_5(bpf_csum_diff, __be32 *, from, u32, from_size,
1925 __be32 *, to, u32, to_size, __wsum, seed)
1927 struct bpf_scratchpad *sp = this_cpu_ptr(&bpf_sp);
1928 u32 diff_size = from_size + to_size;
1931 /* This is quite flexible, some examples:
1933 * from_size == 0, to_size > 0, seed := csum --> pushing data
1934 * from_size > 0, to_size == 0, seed := csum --> pulling data
1935 * from_size > 0, to_size > 0, seed := 0 --> diffing data
1937 * Even for diffing, from_size and to_size don't need to be equal.
1939 if (unlikely(((from_size | to_size) & (sizeof(__be32) - 1)) ||
1940 diff_size > sizeof(sp->diff)))
1943 for (i = 0; i < from_size / sizeof(__be32); i++, j++)
1944 sp->diff[j] = ~from[i];
1945 for (i = 0; i < to_size / sizeof(__be32); i++, j++)
1946 sp->diff[j] = to[i];
1948 return csum_partial(sp->diff, diff_size, seed);
1951 static const struct bpf_func_proto bpf_csum_diff_proto = {
1952 .func = bpf_csum_diff,
1955 .ret_type = RET_INTEGER,
1956 .arg1_type = ARG_PTR_TO_MEM_OR_NULL,
1957 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
1958 .arg3_type = ARG_PTR_TO_MEM_OR_NULL,
1959 .arg4_type = ARG_CONST_SIZE_OR_ZERO,
1960 .arg5_type = ARG_ANYTHING,
1963 BPF_CALL_2(bpf_csum_update, struct sk_buff *, skb, __wsum, csum)
1965 /* The interface is to be used in combination with bpf_csum_diff()
1966 * for direct packet writes. csum rotation for alignment as well
1967 * as emulating csum_sub() can be done from the eBPF program.
1969 if (skb->ip_summed == CHECKSUM_COMPLETE)
1970 return (skb->csum = csum_add(skb->csum, csum));
1975 static const struct bpf_func_proto bpf_csum_update_proto = {
1976 .func = bpf_csum_update,
1978 .ret_type = RET_INTEGER,
1979 .arg1_type = ARG_PTR_TO_CTX,
1980 .arg2_type = ARG_ANYTHING,
1983 static inline int __bpf_rx_skb(struct net_device *dev, struct sk_buff *skb)
1985 return dev_forward_skb(dev, skb);
1988 static inline int __bpf_rx_skb_no_mac(struct net_device *dev,
1989 struct sk_buff *skb)
1991 int ret = ____dev_forward_skb(dev, skb);
1995 ret = netif_rx(skb);
2001 static inline int __bpf_tx_skb(struct net_device *dev, struct sk_buff *skb)
2005 if (unlikely(__this_cpu_read(xmit_recursion) > XMIT_RECURSION_LIMIT)) {
2006 net_crit_ratelimited("bpf: recursion limit reached on datapath, buggy bpf program?\n");
2013 __this_cpu_inc(xmit_recursion);
2014 ret = dev_queue_xmit(skb);
2015 __this_cpu_dec(xmit_recursion);
2020 static int __bpf_redirect_no_mac(struct sk_buff *skb, struct net_device *dev,
2023 /* skb->mac_len is not set on normal egress */
2024 unsigned int mlen = skb->network_header - skb->mac_header;
2026 __skb_pull(skb, mlen);
2028 /* At ingress, the mac header has already been pulled once.
2029 * At egress, skb_pospull_rcsum has to be done in case that
2030 * the skb is originated from ingress (i.e. a forwarded skb)
2031 * to ensure that rcsum starts at net header.
2033 if (!skb_at_tc_ingress(skb))
2034 skb_postpull_rcsum(skb, skb_mac_header(skb), mlen);
2035 skb_pop_mac_header(skb);
2036 skb_reset_mac_len(skb);
2037 return flags & BPF_F_INGRESS ?
2038 __bpf_rx_skb_no_mac(dev, skb) : __bpf_tx_skb(dev, skb);
2041 static int __bpf_redirect_common(struct sk_buff *skb, struct net_device *dev,
2044 /* Verify that a link layer header is carried */
2045 if (unlikely(skb->mac_header >= skb->network_header)) {
2050 bpf_push_mac_rcsum(skb);
2051 return flags & BPF_F_INGRESS ?
2052 __bpf_rx_skb(dev, skb) : __bpf_tx_skb(dev, skb);
2055 static int __bpf_redirect(struct sk_buff *skb, struct net_device *dev,
2058 if (dev_is_mac_header_xmit(dev))
2059 return __bpf_redirect_common(skb, dev, flags);
2061 return __bpf_redirect_no_mac(skb, dev, flags);
2064 BPF_CALL_3(bpf_clone_redirect, struct sk_buff *, skb, u32, ifindex, u64, flags)
2066 struct net_device *dev;
2067 struct sk_buff *clone;
2070 if (unlikely(flags & ~(BPF_F_INGRESS)))
2073 dev = dev_get_by_index_rcu(dev_net(skb->dev), ifindex);
2077 clone = skb_clone(skb, GFP_ATOMIC);
2078 if (unlikely(!clone))
2081 /* For direct write, we need to keep the invariant that the skbs
2082 * we're dealing with need to be uncloned. Should uncloning fail
2083 * here, we need to free the just generated clone to unclone once
2086 ret = bpf_try_make_head_writable(skb);
2087 if (unlikely(ret)) {
2092 return __bpf_redirect(clone, dev, flags);
2095 static const struct bpf_func_proto bpf_clone_redirect_proto = {
2096 .func = bpf_clone_redirect,
2098 .ret_type = RET_INTEGER,
2099 .arg1_type = ARG_PTR_TO_CTX,
2100 .arg2_type = ARG_ANYTHING,
2101 .arg3_type = ARG_ANYTHING,
2104 DEFINE_PER_CPU(struct bpf_redirect_info, bpf_redirect_info);
2105 EXPORT_PER_CPU_SYMBOL_GPL(bpf_redirect_info);
2107 BPF_CALL_2(bpf_redirect, u32, ifindex, u64, flags)
2109 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
2111 if (unlikely(flags & ~(BPF_F_INGRESS)))
2114 ri->ifindex = ifindex;
2117 return TC_ACT_REDIRECT;
2120 int skb_do_redirect(struct sk_buff *skb)
2122 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
2123 struct net_device *dev;
2125 dev = dev_get_by_index_rcu(dev_net(skb->dev), ri->ifindex);
2127 if (unlikely(!dev)) {
2132 return __bpf_redirect(skb, dev, ri->flags);
2135 static const struct bpf_func_proto bpf_redirect_proto = {
2136 .func = bpf_redirect,
2138 .ret_type = RET_INTEGER,
2139 .arg1_type = ARG_ANYTHING,
2140 .arg2_type = ARG_ANYTHING,
2143 BPF_CALL_2(bpf_msg_apply_bytes, struct sk_msg *, msg, u32, bytes)
2145 msg->apply_bytes = bytes;
2149 static const struct bpf_func_proto bpf_msg_apply_bytes_proto = {
2150 .func = bpf_msg_apply_bytes,
2152 .ret_type = RET_INTEGER,
2153 .arg1_type = ARG_PTR_TO_CTX,
2154 .arg2_type = ARG_ANYTHING,
2157 BPF_CALL_2(bpf_msg_cork_bytes, struct sk_msg *, msg, u32, bytes)
2159 msg->cork_bytes = bytes;
2163 static const struct bpf_func_proto bpf_msg_cork_bytes_proto = {
2164 .func = bpf_msg_cork_bytes,
2166 .ret_type = RET_INTEGER,
2167 .arg1_type = ARG_PTR_TO_CTX,
2168 .arg2_type = ARG_ANYTHING,
2171 BPF_CALL_4(bpf_msg_pull_data, struct sk_msg *, msg, u32, start,
2172 u32, end, u64, flags)
2174 u32 len = 0, offset = 0, copy = 0, poffset = 0, bytes = end - start;
2175 u32 first_sge, last_sge, i, shift, bytes_sg_total;
2176 struct scatterlist *sge;
2177 u8 *raw, *to, *from;
2180 if (unlikely(flags || end <= start))
2183 /* First find the starting scatterlist element */
2186 len = sk_msg_elem(msg, i)->length;
2187 if (start < offset + len)
2190 sk_msg_iter_var_next(i);
2191 } while (i != msg->sg.end);
2193 if (unlikely(start >= offset + len))
2197 /* The start may point into the sg element so we need to also
2198 * account for the headroom.
2200 bytes_sg_total = start - offset + bytes;
2201 if (!msg->sg.copy[i] && bytes_sg_total <= len)
2204 /* At this point we need to linearize multiple scatterlist
2205 * elements or a single shared page. Either way we need to
2206 * copy into a linear buffer exclusively owned by BPF. Then
2207 * place the buffer in the scatterlist and fixup the original
2208 * entries by removing the entries now in the linear buffer
2209 * and shifting the remaining entries. For now we do not try
2210 * to copy partial entries to avoid complexity of running out
2211 * of sg_entry slots. The downside is reading a single byte
2212 * will copy the entire sg entry.
2215 copy += sk_msg_elem(msg, i)->length;
2216 sk_msg_iter_var_next(i);
2217 if (bytes_sg_total <= copy)
2219 } while (i != msg->sg.end);
2222 if (unlikely(bytes_sg_total > copy))
2225 page = alloc_pages(__GFP_NOWARN | GFP_ATOMIC | __GFP_COMP,
2227 if (unlikely(!page))
2230 raw = page_address(page);
2233 sge = sk_msg_elem(msg, i);
2234 from = sg_virt(sge);
2238 memcpy(to, from, len);
2241 put_page(sg_page(sge));
2243 sk_msg_iter_var_next(i);
2244 } while (i != last_sge);
2246 sg_set_page(&msg->sg.data[first_sge], page, copy, 0);
2248 /* To repair sg ring we need to shift entries. If we only
2249 * had a single entry though we can just replace it and
2250 * be done. Otherwise walk the ring and shift the entries.
2252 WARN_ON_ONCE(last_sge == first_sge);
2253 shift = last_sge > first_sge ?
2254 last_sge - first_sge - 1 :
2255 MAX_SKB_FRAGS - first_sge + last_sge - 1;
2260 sk_msg_iter_var_next(i);
2264 if (i + shift >= MAX_MSG_FRAGS)
2265 move_from = i + shift - MAX_MSG_FRAGS;
2267 move_from = i + shift;
2268 if (move_from == msg->sg.end)
2271 msg->sg.data[i] = msg->sg.data[move_from];
2272 msg->sg.data[move_from].length = 0;
2273 msg->sg.data[move_from].page_link = 0;
2274 msg->sg.data[move_from].offset = 0;
2275 sk_msg_iter_var_next(i);
2278 msg->sg.end = msg->sg.end - shift > msg->sg.end ?
2279 msg->sg.end - shift + MAX_MSG_FRAGS :
2280 msg->sg.end - shift;
2282 msg->data = sg_virt(&msg->sg.data[first_sge]) + start - offset;
2283 msg->data_end = msg->data + bytes;
2287 static const struct bpf_func_proto bpf_msg_pull_data_proto = {
2288 .func = bpf_msg_pull_data,
2290 .ret_type = RET_INTEGER,
2291 .arg1_type = ARG_PTR_TO_CTX,
2292 .arg2_type = ARG_ANYTHING,
2293 .arg3_type = ARG_ANYTHING,
2294 .arg4_type = ARG_ANYTHING,
2297 BPF_CALL_4(bpf_msg_push_data, struct sk_msg *, msg, u32, start,
2298 u32, len, u64, flags)
2300 struct scatterlist sge, nsge, nnsge, rsge = {0}, *psge;
2301 u32 new, i = 0, l, space, copy = 0, offset = 0;
2302 u8 *raw, *to, *from;
2305 if (unlikely(flags))
2308 /* First find the starting scatterlist element */
2311 l = sk_msg_elem(msg, i)->length;
2313 if (start < offset + l)
2316 sk_msg_iter_var_next(i);
2317 } while (i != msg->sg.end);
2319 if (start >= offset + l)
2322 space = MAX_MSG_FRAGS - sk_msg_elem_used(msg);
2324 /* If no space available will fallback to copy, we need at
2325 * least one scatterlist elem available to push data into
2326 * when start aligns to the beginning of an element or two
2327 * when it falls inside an element. We handle the start equals
2328 * offset case because its the common case for inserting a
2331 if (!space || (space == 1 && start != offset))
2332 copy = msg->sg.data[i].length;
2334 page = alloc_pages(__GFP_NOWARN | GFP_ATOMIC | __GFP_COMP,
2335 get_order(copy + len));
2336 if (unlikely(!page))
2342 raw = page_address(page);
2344 psge = sk_msg_elem(msg, i);
2345 front = start - offset;
2346 back = psge->length - front;
2347 from = sg_virt(psge);
2350 memcpy(raw, from, front);
2354 to = raw + front + len;
2356 memcpy(to, from, back);
2359 put_page(sg_page(psge));
2360 } else if (start - offset) {
2361 psge = sk_msg_elem(msg, i);
2362 rsge = sk_msg_elem_cpy(msg, i);
2364 psge->length = start - offset;
2365 rsge.length -= psge->length;
2366 rsge.offset += start;
2368 sk_msg_iter_var_next(i);
2369 sg_unmark_end(psge);
2370 sk_msg_iter_next(msg, end);
2373 /* Slot(s) to place newly allocated data */
2376 /* Shift one or two slots as needed */
2378 sge = sk_msg_elem_cpy(msg, i);
2380 sk_msg_iter_var_next(i);
2381 sg_unmark_end(&sge);
2382 sk_msg_iter_next(msg, end);
2384 nsge = sk_msg_elem_cpy(msg, i);
2386 sk_msg_iter_var_next(i);
2387 nnsge = sk_msg_elem_cpy(msg, i);
2390 while (i != msg->sg.end) {
2391 msg->sg.data[i] = sge;
2393 sk_msg_iter_var_next(i);
2396 nnsge = sk_msg_elem_cpy(msg, i);
2398 nsge = sk_msg_elem_cpy(msg, i);
2403 /* Place newly allocated data buffer */
2404 sk_mem_charge(msg->sk, len);
2405 msg->sg.size += len;
2406 msg->sg.copy[new] = false;
2407 sg_set_page(&msg->sg.data[new], page, len + copy, 0);
2409 get_page(sg_page(&rsge));
2410 sk_msg_iter_var_next(new);
2411 msg->sg.data[new] = rsge;
2414 sk_msg_compute_data_pointers(msg);
2418 static const struct bpf_func_proto bpf_msg_push_data_proto = {
2419 .func = bpf_msg_push_data,
2421 .ret_type = RET_INTEGER,
2422 .arg1_type = ARG_PTR_TO_CTX,
2423 .arg2_type = ARG_ANYTHING,
2424 .arg3_type = ARG_ANYTHING,
2425 .arg4_type = ARG_ANYTHING,
2428 static void sk_msg_shift_left(struct sk_msg *msg, int i)
2434 sk_msg_iter_var_next(i);
2435 msg->sg.data[prev] = msg->sg.data[i];
2436 } while (i != msg->sg.end);
2438 sk_msg_iter_prev(msg, end);
2441 static void sk_msg_shift_right(struct sk_msg *msg, int i)
2443 struct scatterlist tmp, sge;
2445 sk_msg_iter_next(msg, end);
2446 sge = sk_msg_elem_cpy(msg, i);
2447 sk_msg_iter_var_next(i);
2448 tmp = sk_msg_elem_cpy(msg, i);
2450 while (i != msg->sg.end) {
2451 msg->sg.data[i] = sge;
2452 sk_msg_iter_var_next(i);
2454 tmp = sk_msg_elem_cpy(msg, i);
2458 BPF_CALL_4(bpf_msg_pop_data, struct sk_msg *, msg, u32, start,
2459 u32, len, u64, flags)
2461 u32 i = 0, l, space, offset = 0;
2462 u64 last = start + len;
2465 if (unlikely(flags))
2468 /* First find the starting scatterlist element */
2471 l = sk_msg_elem(msg, i)->length;
2473 if (start < offset + l)
2476 sk_msg_iter_var_next(i);
2477 } while (i != msg->sg.end);
2479 /* Bounds checks: start and pop must be inside message */
2480 if (start >= offset + l || last >= msg->sg.size)
2483 space = MAX_MSG_FRAGS - sk_msg_elem_used(msg);
2486 /* --------------| offset
2487 * -| start |-------- len -------|
2489 * |----- a ----|-------- pop -------|----- b ----|
2490 * |______________________________________________| length
2493 * a: region at front of scatter element to save
2494 * b: region at back of scatter element to save when length > A + pop
2495 * pop: region to pop from element, same as input 'pop' here will be
2496 * decremented below per iteration.
2498 * Two top-level cases to handle when start != offset, first B is non
2499 * zero and second B is zero corresponding to when a pop includes more
2502 * Then if B is non-zero AND there is no space allocate space and
2503 * compact A, B regions into page. If there is space shift ring to
2504 * the rigth free'ing the next element in ring to place B, leaving
2505 * A untouched except to reduce length.
2507 if (start != offset) {
2508 struct scatterlist *nsge, *sge = sk_msg_elem(msg, i);
2510 int b = sge->length - pop - a;
2512 sk_msg_iter_var_next(i);
2514 if (pop < sge->length - a) {
2517 sk_msg_shift_right(msg, i);
2518 nsge = sk_msg_elem(msg, i);
2519 get_page(sg_page(sge));
2522 b, sge->offset + pop + a);
2524 struct page *page, *orig;
2527 page = alloc_pages(__GFP_NOWARN |
2528 __GFP_COMP | GFP_ATOMIC,
2530 if (unlikely(!page))
2534 orig = sg_page(sge);
2535 from = sg_virt(sge);
2536 to = page_address(page);
2537 memcpy(to, from, a);
2538 memcpy(to + a, from + a + pop, b);
2539 sg_set_page(sge, page, a + b, 0);
2543 } else if (pop >= sge->length - a) {
2545 pop -= (sge->length - a);
2549 /* From above the current layout _must_ be as follows,
2554 * |---- pop ---|---------------- b ------------|
2555 * |____________________________________________| length
2557 * Offset and start of the current msg elem are equal because in the
2558 * previous case we handled offset != start and either consumed the
2559 * entire element and advanced to the next element OR pop == 0.
2561 * Two cases to handle here are first pop is less than the length
2562 * leaving some remainder b above. Simply adjust the element's layout
2563 * in this case. Or pop >= length of the element so that b = 0. In this
2564 * case advance to next element decrementing pop.
2567 struct scatterlist *sge = sk_msg_elem(msg, i);
2569 if (pop < sge->length) {
2575 sk_msg_shift_left(msg, i);
2577 sk_msg_iter_var_next(i);
2580 sk_mem_uncharge(msg->sk, len - pop);
2581 msg->sg.size -= (len - pop);
2582 sk_msg_compute_data_pointers(msg);
2586 static const struct bpf_func_proto bpf_msg_pop_data_proto = {
2587 .func = bpf_msg_pop_data,
2589 .ret_type = RET_INTEGER,
2590 .arg1_type = ARG_PTR_TO_CTX,
2591 .arg2_type = ARG_ANYTHING,
2592 .arg3_type = ARG_ANYTHING,
2593 .arg4_type = ARG_ANYTHING,
2596 BPF_CALL_1(bpf_get_cgroup_classid, const struct sk_buff *, skb)
2598 return task_get_classid(skb);
2601 static const struct bpf_func_proto bpf_get_cgroup_classid_proto = {
2602 .func = bpf_get_cgroup_classid,
2604 .ret_type = RET_INTEGER,
2605 .arg1_type = ARG_PTR_TO_CTX,
2608 BPF_CALL_1(bpf_get_route_realm, const struct sk_buff *, skb)
2610 return dst_tclassid(skb);
2613 static const struct bpf_func_proto bpf_get_route_realm_proto = {
2614 .func = bpf_get_route_realm,
2616 .ret_type = RET_INTEGER,
2617 .arg1_type = ARG_PTR_TO_CTX,
2620 BPF_CALL_1(bpf_get_hash_recalc, struct sk_buff *, skb)
2622 /* If skb_clear_hash() was called due to mangling, we can
2623 * trigger SW recalculation here. Later access to hash
2624 * can then use the inline skb->hash via context directly
2625 * instead of calling this helper again.
2627 return skb_get_hash(skb);
2630 static const struct bpf_func_proto bpf_get_hash_recalc_proto = {
2631 .func = bpf_get_hash_recalc,
2633 .ret_type = RET_INTEGER,
2634 .arg1_type = ARG_PTR_TO_CTX,
2637 BPF_CALL_1(bpf_set_hash_invalid, struct sk_buff *, skb)
2639 /* After all direct packet write, this can be used once for
2640 * triggering a lazy recalc on next skb_get_hash() invocation.
2642 skb_clear_hash(skb);
2646 static const struct bpf_func_proto bpf_set_hash_invalid_proto = {
2647 .func = bpf_set_hash_invalid,
2649 .ret_type = RET_INTEGER,
2650 .arg1_type = ARG_PTR_TO_CTX,
2653 BPF_CALL_2(bpf_set_hash, struct sk_buff *, skb, u32, hash)
2655 /* Set user specified hash as L4(+), so that it gets returned
2656 * on skb_get_hash() call unless BPF prog later on triggers a
2659 __skb_set_sw_hash(skb, hash, true);
2663 static const struct bpf_func_proto bpf_set_hash_proto = {
2664 .func = bpf_set_hash,
2666 .ret_type = RET_INTEGER,
2667 .arg1_type = ARG_PTR_TO_CTX,
2668 .arg2_type = ARG_ANYTHING,
2671 BPF_CALL_3(bpf_skb_vlan_push, struct sk_buff *, skb, __be16, vlan_proto,
2676 if (unlikely(vlan_proto != htons(ETH_P_8021Q) &&
2677 vlan_proto != htons(ETH_P_8021AD)))
2678 vlan_proto = htons(ETH_P_8021Q);
2680 bpf_push_mac_rcsum(skb);
2681 ret = skb_vlan_push(skb, vlan_proto, vlan_tci);
2682 bpf_pull_mac_rcsum(skb);
2684 bpf_compute_data_pointers(skb);
2688 static const struct bpf_func_proto bpf_skb_vlan_push_proto = {
2689 .func = bpf_skb_vlan_push,
2691 .ret_type = RET_INTEGER,
2692 .arg1_type = ARG_PTR_TO_CTX,
2693 .arg2_type = ARG_ANYTHING,
2694 .arg3_type = ARG_ANYTHING,
2697 BPF_CALL_1(bpf_skb_vlan_pop, struct sk_buff *, skb)
2701 bpf_push_mac_rcsum(skb);
2702 ret = skb_vlan_pop(skb);
2703 bpf_pull_mac_rcsum(skb);
2705 bpf_compute_data_pointers(skb);
2709 static const struct bpf_func_proto bpf_skb_vlan_pop_proto = {
2710 .func = bpf_skb_vlan_pop,
2712 .ret_type = RET_INTEGER,
2713 .arg1_type = ARG_PTR_TO_CTX,
2716 static int bpf_skb_generic_push(struct sk_buff *skb, u32 off, u32 len)
2718 /* Caller already did skb_cow() with len as headroom,
2719 * so no need to do it here.
2722 memmove(skb->data, skb->data + len, off);
2723 memset(skb->data + off, 0, len);
2725 /* No skb_postpush_rcsum(skb, skb->data + off, len)
2726 * needed here as it does not change the skb->csum
2727 * result for checksum complete when summing over
2733 static int bpf_skb_generic_pop(struct sk_buff *skb, u32 off, u32 len)
2735 /* skb_ensure_writable() is not needed here, as we're
2736 * already working on an uncloned skb.
2738 if (unlikely(!pskb_may_pull(skb, off + len)))
2741 skb_postpull_rcsum(skb, skb->data + off, len);
2742 memmove(skb->data + len, skb->data, off);
2743 __skb_pull(skb, len);
2748 static int bpf_skb_net_hdr_push(struct sk_buff *skb, u32 off, u32 len)
2750 bool trans_same = skb->transport_header == skb->network_header;
2753 /* There's no need for __skb_push()/__skb_pull() pair to
2754 * get to the start of the mac header as we're guaranteed
2755 * to always start from here under eBPF.
2757 ret = bpf_skb_generic_push(skb, off, len);
2759 skb->mac_header -= len;
2760 skb->network_header -= len;
2762 skb->transport_header = skb->network_header;
2768 static int bpf_skb_net_hdr_pop(struct sk_buff *skb, u32 off, u32 len)
2770 bool trans_same = skb->transport_header == skb->network_header;
2773 /* Same here, __skb_push()/__skb_pull() pair not needed. */
2774 ret = bpf_skb_generic_pop(skb, off, len);
2776 skb->mac_header += len;
2777 skb->network_header += len;
2779 skb->transport_header = skb->network_header;
2785 static int bpf_skb_proto_4_to_6(struct sk_buff *skb)
2787 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
2788 u32 off = skb_mac_header_len(skb);
2791 /* SCTP uses GSO_BY_FRAGS, thus cannot adjust it. */
2792 if (skb_is_gso(skb) && unlikely(skb_is_gso_sctp(skb)))
2795 ret = skb_cow(skb, len_diff);
2796 if (unlikely(ret < 0))
2799 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
2800 if (unlikely(ret < 0))
2803 if (skb_is_gso(skb)) {
2804 struct skb_shared_info *shinfo = skb_shinfo(skb);
2806 /* SKB_GSO_TCPV4 needs to be changed into
2809 if (shinfo->gso_type & SKB_GSO_TCPV4) {
2810 shinfo->gso_type &= ~SKB_GSO_TCPV4;
2811 shinfo->gso_type |= SKB_GSO_TCPV6;
2814 /* Due to IPv6 header, MSS needs to be downgraded. */
2815 skb_decrease_gso_size(shinfo, len_diff);
2816 /* Header must be checked, and gso_segs recomputed. */
2817 shinfo->gso_type |= SKB_GSO_DODGY;
2818 shinfo->gso_segs = 0;
2821 skb->protocol = htons(ETH_P_IPV6);
2822 skb_clear_hash(skb);
2827 static int bpf_skb_proto_6_to_4(struct sk_buff *skb)
2829 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
2830 u32 off = skb_mac_header_len(skb);
2833 /* SCTP uses GSO_BY_FRAGS, thus cannot adjust it. */
2834 if (skb_is_gso(skb) && unlikely(skb_is_gso_sctp(skb)))
2837 ret = skb_unclone(skb, GFP_ATOMIC);
2838 if (unlikely(ret < 0))
2841 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
2842 if (unlikely(ret < 0))
2845 if (skb_is_gso(skb)) {
2846 struct skb_shared_info *shinfo = skb_shinfo(skb);
2848 /* SKB_GSO_TCPV6 needs to be changed into
2851 if (shinfo->gso_type & SKB_GSO_TCPV6) {
2852 shinfo->gso_type &= ~SKB_GSO_TCPV6;
2853 shinfo->gso_type |= SKB_GSO_TCPV4;
2856 /* Due to IPv4 header, MSS can be upgraded. */
2857 skb_increase_gso_size(shinfo, len_diff);
2858 /* Header must be checked, and gso_segs recomputed. */
2859 shinfo->gso_type |= SKB_GSO_DODGY;
2860 shinfo->gso_segs = 0;
2863 skb->protocol = htons(ETH_P_IP);
2864 skb_clear_hash(skb);
2869 static int bpf_skb_proto_xlat(struct sk_buff *skb, __be16 to_proto)
2871 __be16 from_proto = skb->protocol;
2873 if (from_proto == htons(ETH_P_IP) &&
2874 to_proto == htons(ETH_P_IPV6))
2875 return bpf_skb_proto_4_to_6(skb);
2877 if (from_proto == htons(ETH_P_IPV6) &&
2878 to_proto == htons(ETH_P_IP))
2879 return bpf_skb_proto_6_to_4(skb);
2884 BPF_CALL_3(bpf_skb_change_proto, struct sk_buff *, skb, __be16, proto,
2889 if (unlikely(flags))
2892 /* General idea is that this helper does the basic groundwork
2893 * needed for changing the protocol, and eBPF program fills the
2894 * rest through bpf_skb_store_bytes(), bpf_lX_csum_replace()
2895 * and other helpers, rather than passing a raw buffer here.
2897 * The rationale is to keep this minimal and without a need to
2898 * deal with raw packet data. F.e. even if we would pass buffers
2899 * here, the program still needs to call the bpf_lX_csum_replace()
2900 * helpers anyway. Plus, this way we keep also separation of
2901 * concerns, since f.e. bpf_skb_store_bytes() should only take
2904 * Currently, additional options and extension header space are
2905 * not supported, but flags register is reserved so we can adapt
2906 * that. For offloads, we mark packet as dodgy, so that headers
2907 * need to be verified first.
2909 ret = bpf_skb_proto_xlat(skb, proto);
2910 bpf_compute_data_pointers(skb);
2914 static const struct bpf_func_proto bpf_skb_change_proto_proto = {
2915 .func = bpf_skb_change_proto,
2917 .ret_type = RET_INTEGER,
2918 .arg1_type = ARG_PTR_TO_CTX,
2919 .arg2_type = ARG_ANYTHING,
2920 .arg3_type = ARG_ANYTHING,
2923 BPF_CALL_2(bpf_skb_change_type, struct sk_buff *, skb, u32, pkt_type)
2925 /* We only allow a restricted subset to be changed for now. */
2926 if (unlikely(!skb_pkt_type_ok(skb->pkt_type) ||
2927 !skb_pkt_type_ok(pkt_type)))
2930 skb->pkt_type = pkt_type;
2934 static const struct bpf_func_proto bpf_skb_change_type_proto = {
2935 .func = bpf_skb_change_type,
2937 .ret_type = RET_INTEGER,
2938 .arg1_type = ARG_PTR_TO_CTX,
2939 .arg2_type = ARG_ANYTHING,
2942 static u32 bpf_skb_net_base_len(const struct sk_buff *skb)
2944 switch (skb->protocol) {
2945 case htons(ETH_P_IP):
2946 return sizeof(struct iphdr);
2947 case htons(ETH_P_IPV6):
2948 return sizeof(struct ipv6hdr);
2954 static int bpf_skb_net_grow(struct sk_buff *skb, u32 len_diff)
2956 u32 off = skb_mac_header_len(skb) + bpf_skb_net_base_len(skb);
2959 /* SCTP uses GSO_BY_FRAGS, thus cannot adjust it. */
2960 if (skb_is_gso(skb) && unlikely(skb_is_gso_sctp(skb)))
2963 ret = skb_cow(skb, len_diff);
2964 if (unlikely(ret < 0))
2967 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
2968 if (unlikely(ret < 0))
2971 if (skb_is_gso(skb)) {
2972 struct skb_shared_info *shinfo = skb_shinfo(skb);
2974 /* Due to header grow, MSS needs to be downgraded. */
2975 skb_decrease_gso_size(shinfo, len_diff);
2976 /* Header must be checked, and gso_segs recomputed. */
2977 shinfo->gso_type |= SKB_GSO_DODGY;
2978 shinfo->gso_segs = 0;
2984 static int bpf_skb_net_shrink(struct sk_buff *skb, u32 len_diff)
2986 u32 off = skb_mac_header_len(skb) + bpf_skb_net_base_len(skb);
2989 /* SCTP uses GSO_BY_FRAGS, thus cannot adjust it. */
2990 if (skb_is_gso(skb) && unlikely(skb_is_gso_sctp(skb)))
2993 ret = skb_unclone(skb, GFP_ATOMIC);
2994 if (unlikely(ret < 0))
2997 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
2998 if (unlikely(ret < 0))
3001 if (skb_is_gso(skb)) {
3002 struct skb_shared_info *shinfo = skb_shinfo(skb);
3004 /* Due to header shrink, MSS can be upgraded. */
3005 skb_increase_gso_size(shinfo, len_diff);
3006 /* Header must be checked, and gso_segs recomputed. */
3007 shinfo->gso_type |= SKB_GSO_DODGY;
3008 shinfo->gso_segs = 0;
3014 static u32 __bpf_skb_max_len(const struct sk_buff *skb)
3016 return skb->dev ? skb->dev->mtu + skb->dev->hard_header_len :
3020 static int bpf_skb_adjust_net(struct sk_buff *skb, s32 len_diff)
3022 bool trans_same = skb->transport_header == skb->network_header;
3023 u32 len_cur, len_diff_abs = abs(len_diff);
3024 u32 len_min = bpf_skb_net_base_len(skb);
3025 u32 len_max = __bpf_skb_max_len(skb);
3026 __be16 proto = skb->protocol;
3027 bool shrink = len_diff < 0;
3030 if (unlikely(len_diff_abs > 0xfffU))
3032 if (unlikely(proto != htons(ETH_P_IP) &&
3033 proto != htons(ETH_P_IPV6)))
3036 len_cur = skb->len - skb_network_offset(skb);
3037 if (skb_transport_header_was_set(skb) && !trans_same)
3038 len_cur = skb_network_header_len(skb);
3039 if ((shrink && (len_diff_abs >= len_cur ||
3040 len_cur - len_diff_abs < len_min)) ||
3041 (!shrink && (skb->len + len_diff_abs > len_max &&
3045 ret = shrink ? bpf_skb_net_shrink(skb, len_diff_abs) :
3046 bpf_skb_net_grow(skb, len_diff_abs);
3048 bpf_compute_data_pointers(skb);
3052 BPF_CALL_4(bpf_skb_adjust_room, struct sk_buff *, skb, s32, len_diff,
3053 u32, mode, u64, flags)
3055 if (unlikely(flags))
3057 if (likely(mode == BPF_ADJ_ROOM_NET))
3058 return bpf_skb_adjust_net(skb, len_diff);
3063 static const struct bpf_func_proto bpf_skb_adjust_room_proto = {
3064 .func = bpf_skb_adjust_room,
3066 .ret_type = RET_INTEGER,
3067 .arg1_type = ARG_PTR_TO_CTX,
3068 .arg2_type = ARG_ANYTHING,
3069 .arg3_type = ARG_ANYTHING,
3070 .arg4_type = ARG_ANYTHING,
3073 static u32 __bpf_skb_min_len(const struct sk_buff *skb)
3075 u32 min_len = skb_network_offset(skb);
3077 if (skb_transport_header_was_set(skb))
3078 min_len = skb_transport_offset(skb);
3079 if (skb->ip_summed == CHECKSUM_PARTIAL)
3080 min_len = skb_checksum_start_offset(skb) +
3081 skb->csum_offset + sizeof(__sum16);
3085 static int bpf_skb_grow_rcsum(struct sk_buff *skb, unsigned int new_len)
3087 unsigned int old_len = skb->len;
3090 ret = __skb_grow_rcsum(skb, new_len);
3092 memset(skb->data + old_len, 0, new_len - old_len);
3096 static int bpf_skb_trim_rcsum(struct sk_buff *skb, unsigned int new_len)
3098 return __skb_trim_rcsum(skb, new_len);
3101 static inline int __bpf_skb_change_tail(struct sk_buff *skb, u32 new_len,
3104 u32 max_len = __bpf_skb_max_len(skb);
3105 u32 min_len = __bpf_skb_min_len(skb);
3108 if (unlikely(flags || new_len > max_len || new_len < min_len))
3110 if (skb->encapsulation)
3113 /* The basic idea of this helper is that it's performing the
3114 * needed work to either grow or trim an skb, and eBPF program
3115 * rewrites the rest via helpers like bpf_skb_store_bytes(),
3116 * bpf_lX_csum_replace() and others rather than passing a raw
3117 * buffer here. This one is a slow path helper and intended
3118 * for replies with control messages.
3120 * Like in bpf_skb_change_proto(), we want to keep this rather
3121 * minimal and without protocol specifics so that we are able
3122 * to separate concerns as in bpf_skb_store_bytes() should only
3123 * be the one responsible for writing buffers.
3125 * It's really expected to be a slow path operation here for
3126 * control message replies, so we're implicitly linearizing,
3127 * uncloning and drop offloads from the skb by this.
3129 ret = __bpf_try_make_writable(skb, skb->len);
3131 if (new_len > skb->len)
3132 ret = bpf_skb_grow_rcsum(skb, new_len);
3133 else if (new_len < skb->len)
3134 ret = bpf_skb_trim_rcsum(skb, new_len);
3135 if (!ret && skb_is_gso(skb))
3141 BPF_CALL_3(bpf_skb_change_tail, struct sk_buff *, skb, u32, new_len,
3144 int ret = __bpf_skb_change_tail(skb, new_len, flags);
3146 bpf_compute_data_pointers(skb);
3150 static const struct bpf_func_proto bpf_skb_change_tail_proto = {
3151 .func = bpf_skb_change_tail,
3153 .ret_type = RET_INTEGER,
3154 .arg1_type = ARG_PTR_TO_CTX,
3155 .arg2_type = ARG_ANYTHING,
3156 .arg3_type = ARG_ANYTHING,
3159 BPF_CALL_3(sk_skb_change_tail, struct sk_buff *, skb, u32, new_len,
3162 int ret = __bpf_skb_change_tail(skb, new_len, flags);
3164 bpf_compute_data_end_sk_skb(skb);
3168 static const struct bpf_func_proto sk_skb_change_tail_proto = {
3169 .func = sk_skb_change_tail,
3171 .ret_type = RET_INTEGER,
3172 .arg1_type = ARG_PTR_TO_CTX,
3173 .arg2_type = ARG_ANYTHING,
3174 .arg3_type = ARG_ANYTHING,
3177 static inline int __bpf_skb_change_head(struct sk_buff *skb, u32 head_room,
3180 u32 max_len = __bpf_skb_max_len(skb);
3181 u32 new_len = skb->len + head_room;
3184 if (unlikely(flags || (!skb_is_gso(skb) && new_len > max_len) ||
3185 new_len < skb->len))
3188 ret = skb_cow(skb, head_room);
3190 /* Idea for this helper is that we currently only
3191 * allow to expand on mac header. This means that
3192 * skb->protocol network header, etc, stay as is.
3193 * Compared to bpf_skb_change_tail(), we're more
3194 * flexible due to not needing to linearize or
3195 * reset GSO. Intention for this helper is to be
3196 * used by an L3 skb that needs to push mac header
3197 * for redirection into L2 device.
3199 __skb_push(skb, head_room);
3200 memset(skb->data, 0, head_room);
3201 skb_reset_mac_header(skb);
3207 BPF_CALL_3(bpf_skb_change_head, struct sk_buff *, skb, u32, head_room,
3210 int ret = __bpf_skb_change_head(skb, head_room, flags);
3212 bpf_compute_data_pointers(skb);
3216 static const struct bpf_func_proto bpf_skb_change_head_proto = {
3217 .func = bpf_skb_change_head,
3219 .ret_type = RET_INTEGER,
3220 .arg1_type = ARG_PTR_TO_CTX,
3221 .arg2_type = ARG_ANYTHING,
3222 .arg3_type = ARG_ANYTHING,
3225 BPF_CALL_3(sk_skb_change_head, struct sk_buff *, skb, u32, head_room,
3228 int ret = __bpf_skb_change_head(skb, head_room, flags);
3230 bpf_compute_data_end_sk_skb(skb);
3234 static const struct bpf_func_proto sk_skb_change_head_proto = {
3235 .func = sk_skb_change_head,
3237 .ret_type = RET_INTEGER,
3238 .arg1_type = ARG_PTR_TO_CTX,
3239 .arg2_type = ARG_ANYTHING,
3240 .arg3_type = ARG_ANYTHING,
3242 static unsigned long xdp_get_metalen(const struct xdp_buff *xdp)
3244 return xdp_data_meta_unsupported(xdp) ? 0 :
3245 xdp->data - xdp->data_meta;
3248 BPF_CALL_2(bpf_xdp_adjust_head, struct xdp_buff *, xdp, int, offset)
3250 void *xdp_frame_end = xdp->data_hard_start + sizeof(struct xdp_frame);
3251 unsigned long metalen = xdp_get_metalen(xdp);
3252 void *data_start = xdp_frame_end + metalen;
3253 void *data = xdp->data + offset;
3255 if (unlikely(data < data_start ||
3256 data > xdp->data_end - ETH_HLEN))
3260 memmove(xdp->data_meta + offset,
3261 xdp->data_meta, metalen);
3262 xdp->data_meta += offset;
3268 static const struct bpf_func_proto bpf_xdp_adjust_head_proto = {
3269 .func = bpf_xdp_adjust_head,
3271 .ret_type = RET_INTEGER,
3272 .arg1_type = ARG_PTR_TO_CTX,
3273 .arg2_type = ARG_ANYTHING,
3276 BPF_CALL_2(bpf_xdp_adjust_tail, struct xdp_buff *, xdp, int, offset)
3278 void *data_end = xdp->data_end + offset;
3280 /* only shrinking is allowed for now. */
3281 if (unlikely(offset >= 0))
3284 if (unlikely(data_end < xdp->data + ETH_HLEN))
3287 xdp->data_end = data_end;
3292 static const struct bpf_func_proto bpf_xdp_adjust_tail_proto = {
3293 .func = bpf_xdp_adjust_tail,
3295 .ret_type = RET_INTEGER,
3296 .arg1_type = ARG_PTR_TO_CTX,
3297 .arg2_type = ARG_ANYTHING,
3300 BPF_CALL_2(bpf_xdp_adjust_meta, struct xdp_buff *, xdp, int, offset)
3302 void *xdp_frame_end = xdp->data_hard_start + sizeof(struct xdp_frame);
3303 void *meta = xdp->data_meta + offset;
3304 unsigned long metalen = xdp->data - meta;
3306 if (xdp_data_meta_unsupported(xdp))
3308 if (unlikely(meta < xdp_frame_end ||
3311 if (unlikely((metalen & (sizeof(__u32) - 1)) ||
3315 xdp->data_meta = meta;
3320 static const struct bpf_func_proto bpf_xdp_adjust_meta_proto = {
3321 .func = bpf_xdp_adjust_meta,
3323 .ret_type = RET_INTEGER,
3324 .arg1_type = ARG_PTR_TO_CTX,
3325 .arg2_type = ARG_ANYTHING,
3328 static int __bpf_tx_xdp(struct net_device *dev,
3329 struct bpf_map *map,
3330 struct xdp_buff *xdp,
3333 struct xdp_frame *xdpf;
3336 if (!dev->netdev_ops->ndo_xdp_xmit) {
3340 err = xdp_ok_fwd_dev(dev, xdp->data_end - xdp->data);
3344 xdpf = convert_to_xdp_frame(xdp);
3345 if (unlikely(!xdpf))
3348 sent = dev->netdev_ops->ndo_xdp_xmit(dev, 1, &xdpf, XDP_XMIT_FLUSH);
3355 xdp_do_redirect_slow(struct net_device *dev, struct xdp_buff *xdp,
3356 struct bpf_prog *xdp_prog, struct bpf_redirect_info *ri)
3358 struct net_device *fwd;
3359 u32 index = ri->ifindex;
3362 fwd = dev_get_by_index_rcu(dev_net(dev), index);
3364 if (unlikely(!fwd)) {
3369 err = __bpf_tx_xdp(fwd, NULL, xdp, 0);
3373 _trace_xdp_redirect(dev, xdp_prog, index);
3376 _trace_xdp_redirect_err(dev, xdp_prog, index, err);
3380 static int __bpf_tx_xdp_map(struct net_device *dev_rx, void *fwd,
3381 struct bpf_map *map,
3382 struct xdp_buff *xdp,
3387 switch (map->map_type) {
3388 case BPF_MAP_TYPE_DEVMAP: {
3389 struct bpf_dtab_netdev *dst = fwd;
3391 err = dev_map_enqueue(dst, xdp, dev_rx);
3394 __dev_map_insert_ctx(map, index);
3397 case BPF_MAP_TYPE_CPUMAP: {
3398 struct bpf_cpu_map_entry *rcpu = fwd;
3400 err = cpu_map_enqueue(rcpu, xdp, dev_rx);
3403 __cpu_map_insert_ctx(map, index);
3406 case BPF_MAP_TYPE_XSKMAP: {
3407 struct xdp_sock *xs = fwd;
3409 err = __xsk_map_redirect(map, xdp, xs);
3418 void xdp_do_flush_map(void)
3420 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
3421 struct bpf_map *map = ri->map_to_flush;
3423 ri->map_to_flush = NULL;
3425 switch (map->map_type) {
3426 case BPF_MAP_TYPE_DEVMAP:
3427 __dev_map_flush(map);
3429 case BPF_MAP_TYPE_CPUMAP:
3430 __cpu_map_flush(map);
3432 case BPF_MAP_TYPE_XSKMAP:
3433 __xsk_map_flush(map);
3440 EXPORT_SYMBOL_GPL(xdp_do_flush_map);
3442 static inline void *__xdp_map_lookup_elem(struct bpf_map *map, u32 index)
3444 switch (map->map_type) {
3445 case BPF_MAP_TYPE_DEVMAP:
3446 return __dev_map_lookup_elem(map, index);
3447 case BPF_MAP_TYPE_CPUMAP:
3448 return __cpu_map_lookup_elem(map, index);
3449 case BPF_MAP_TYPE_XSKMAP:
3450 return __xsk_map_lookup_elem(map, index);
3456 void bpf_clear_redirect_map(struct bpf_map *map)
3458 struct bpf_redirect_info *ri;
3461 for_each_possible_cpu(cpu) {
3462 ri = per_cpu_ptr(&bpf_redirect_info, cpu);
3463 /* Avoid polluting remote cacheline due to writes if
3464 * not needed. Once we pass this test, we need the
3465 * cmpxchg() to make sure it hasn't been changed in
3466 * the meantime by remote CPU.
3468 if (unlikely(READ_ONCE(ri->map) == map))
3469 cmpxchg(&ri->map, map, NULL);
3473 static int xdp_do_redirect_map(struct net_device *dev, struct xdp_buff *xdp,
3474 struct bpf_prog *xdp_prog, struct bpf_map *map,
3475 struct bpf_redirect_info *ri)
3477 u32 index = ri->ifindex;
3482 WRITE_ONCE(ri->map, NULL);
3484 fwd = __xdp_map_lookup_elem(map, index);
3485 if (unlikely(!fwd)) {
3489 if (ri->map_to_flush && unlikely(ri->map_to_flush != map))
3492 err = __bpf_tx_xdp_map(dev, fwd, map, xdp, index);
3496 ri->map_to_flush = map;
3497 _trace_xdp_redirect_map(dev, xdp_prog, fwd, map, index);
3500 _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map, index, err);
3504 int xdp_do_redirect(struct net_device *dev, struct xdp_buff *xdp,
3505 struct bpf_prog *xdp_prog)
3507 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
3508 struct bpf_map *map = READ_ONCE(ri->map);
3511 return xdp_do_redirect_map(dev, xdp, xdp_prog, map, ri);
3513 return xdp_do_redirect_slow(dev, xdp, xdp_prog, ri);
3515 EXPORT_SYMBOL_GPL(xdp_do_redirect);
3517 static int xdp_do_generic_redirect_map(struct net_device *dev,
3518 struct sk_buff *skb,
3519 struct xdp_buff *xdp,
3520 struct bpf_prog *xdp_prog,
3521 struct bpf_map *map)
3523 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
3524 u32 index = ri->ifindex;
3529 WRITE_ONCE(ri->map, NULL);
3531 fwd = __xdp_map_lookup_elem(map, index);
3532 if (unlikely(!fwd)) {
3537 if (map->map_type == BPF_MAP_TYPE_DEVMAP) {
3538 struct bpf_dtab_netdev *dst = fwd;
3540 err = dev_map_generic_redirect(dst, skb, xdp_prog);
3543 } else if (map->map_type == BPF_MAP_TYPE_XSKMAP) {
3544 struct xdp_sock *xs = fwd;
3546 err = xsk_generic_rcv(xs, xdp);
3551 /* TODO: Handle BPF_MAP_TYPE_CPUMAP */
3556 _trace_xdp_redirect_map(dev, xdp_prog, fwd, map, index);
3559 _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map, index, err);
3563 int xdp_do_generic_redirect(struct net_device *dev, struct sk_buff *skb,
3564 struct xdp_buff *xdp, struct bpf_prog *xdp_prog)
3566 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
3567 struct bpf_map *map = READ_ONCE(ri->map);
3568 u32 index = ri->ifindex;
3569 struct net_device *fwd;
3573 return xdp_do_generic_redirect_map(dev, skb, xdp, xdp_prog,
3576 fwd = dev_get_by_index_rcu(dev_net(dev), index);
3577 if (unlikely(!fwd)) {
3582 err = xdp_ok_fwd_dev(fwd, skb->len);
3587 _trace_xdp_redirect(dev, xdp_prog, index);
3588 generic_xdp_tx(skb, xdp_prog);
3591 _trace_xdp_redirect_err(dev, xdp_prog, index, err);
3594 EXPORT_SYMBOL_GPL(xdp_do_generic_redirect);
3596 BPF_CALL_2(bpf_xdp_redirect, u32, ifindex, u64, flags)
3598 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
3600 if (unlikely(flags))
3603 ri->ifindex = ifindex;
3605 WRITE_ONCE(ri->map, NULL);
3607 return XDP_REDIRECT;
3610 static const struct bpf_func_proto bpf_xdp_redirect_proto = {
3611 .func = bpf_xdp_redirect,
3613 .ret_type = RET_INTEGER,
3614 .arg1_type = ARG_ANYTHING,
3615 .arg2_type = ARG_ANYTHING,
3618 BPF_CALL_3(bpf_xdp_redirect_map, struct bpf_map *, map, u32, ifindex,
3621 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
3623 if (unlikely(flags))
3626 ri->ifindex = ifindex;
3628 WRITE_ONCE(ri->map, map);
3630 return XDP_REDIRECT;
3633 static const struct bpf_func_proto bpf_xdp_redirect_map_proto = {
3634 .func = bpf_xdp_redirect_map,
3636 .ret_type = RET_INTEGER,
3637 .arg1_type = ARG_CONST_MAP_PTR,
3638 .arg2_type = ARG_ANYTHING,
3639 .arg3_type = ARG_ANYTHING,
3642 static unsigned long bpf_skb_copy(void *dst_buff, const void *skb,
3643 unsigned long off, unsigned long len)
3645 void *ptr = skb_header_pointer(skb, off, len, dst_buff);
3649 if (ptr != dst_buff)
3650 memcpy(dst_buff, ptr, len);
3655 BPF_CALL_5(bpf_skb_event_output, struct sk_buff *, skb, struct bpf_map *, map,
3656 u64, flags, void *, meta, u64, meta_size)
3658 u64 skb_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
3660 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
3662 if (unlikely(skb_size > skb->len))
3665 return bpf_event_output(map, flags, meta, meta_size, skb, skb_size,
3669 static const struct bpf_func_proto bpf_skb_event_output_proto = {
3670 .func = bpf_skb_event_output,
3672 .ret_type = RET_INTEGER,
3673 .arg1_type = ARG_PTR_TO_CTX,
3674 .arg2_type = ARG_CONST_MAP_PTR,
3675 .arg3_type = ARG_ANYTHING,
3676 .arg4_type = ARG_PTR_TO_MEM,
3677 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
3680 static unsigned short bpf_tunnel_key_af(u64 flags)
3682 return flags & BPF_F_TUNINFO_IPV6 ? AF_INET6 : AF_INET;
3685 BPF_CALL_4(bpf_skb_get_tunnel_key, struct sk_buff *, skb, struct bpf_tunnel_key *, to,
3686 u32, size, u64, flags)
3688 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
3689 u8 compat[sizeof(struct bpf_tunnel_key)];
3693 if (unlikely(!info || (flags & ~(BPF_F_TUNINFO_IPV6)))) {
3697 if (ip_tunnel_info_af(info) != bpf_tunnel_key_af(flags)) {
3701 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
3704 case offsetof(struct bpf_tunnel_key, tunnel_label):
3705 case offsetof(struct bpf_tunnel_key, tunnel_ext):
3707 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
3708 /* Fixup deprecated structure layouts here, so we have
3709 * a common path later on.
3711 if (ip_tunnel_info_af(info) != AF_INET)
3714 to = (struct bpf_tunnel_key *)compat;
3721 to->tunnel_id = be64_to_cpu(info->key.tun_id);
3722 to->tunnel_tos = info->key.tos;
3723 to->tunnel_ttl = info->key.ttl;
3726 if (flags & BPF_F_TUNINFO_IPV6) {
3727 memcpy(to->remote_ipv6, &info->key.u.ipv6.src,
3728 sizeof(to->remote_ipv6));
3729 to->tunnel_label = be32_to_cpu(info->key.label);
3731 to->remote_ipv4 = be32_to_cpu(info->key.u.ipv4.src);
3732 memset(&to->remote_ipv6[1], 0, sizeof(__u32) * 3);
3733 to->tunnel_label = 0;
3736 if (unlikely(size != sizeof(struct bpf_tunnel_key)))
3737 memcpy(to_orig, to, size);
3741 memset(to_orig, 0, size);
3745 static const struct bpf_func_proto bpf_skb_get_tunnel_key_proto = {
3746 .func = bpf_skb_get_tunnel_key,
3748 .ret_type = RET_INTEGER,
3749 .arg1_type = ARG_PTR_TO_CTX,
3750 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
3751 .arg3_type = ARG_CONST_SIZE,
3752 .arg4_type = ARG_ANYTHING,
3755 BPF_CALL_3(bpf_skb_get_tunnel_opt, struct sk_buff *, skb, u8 *, to, u32, size)
3757 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
3760 if (unlikely(!info ||
3761 !(info->key.tun_flags & TUNNEL_OPTIONS_PRESENT))) {
3765 if (unlikely(size < info->options_len)) {
3770 ip_tunnel_info_opts_get(to, info);
3771 if (size > info->options_len)
3772 memset(to + info->options_len, 0, size - info->options_len);
3774 return info->options_len;
3776 memset(to, 0, size);
3780 static const struct bpf_func_proto bpf_skb_get_tunnel_opt_proto = {
3781 .func = bpf_skb_get_tunnel_opt,
3783 .ret_type = RET_INTEGER,
3784 .arg1_type = ARG_PTR_TO_CTX,
3785 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
3786 .arg3_type = ARG_CONST_SIZE,
3789 static struct metadata_dst __percpu *md_dst;
3791 BPF_CALL_4(bpf_skb_set_tunnel_key, struct sk_buff *, skb,
3792 const struct bpf_tunnel_key *, from, u32, size, u64, flags)
3794 struct metadata_dst *md = this_cpu_ptr(md_dst);
3795 u8 compat[sizeof(struct bpf_tunnel_key)];
3796 struct ip_tunnel_info *info;
3798 if (unlikely(flags & ~(BPF_F_TUNINFO_IPV6 | BPF_F_ZERO_CSUM_TX |
3799 BPF_F_DONT_FRAGMENT | BPF_F_SEQ_NUMBER)))
3801 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
3803 case offsetof(struct bpf_tunnel_key, tunnel_label):
3804 case offsetof(struct bpf_tunnel_key, tunnel_ext):
3805 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
3806 /* Fixup deprecated structure layouts here, so we have
3807 * a common path later on.
3809 memcpy(compat, from, size);
3810 memset(compat + size, 0, sizeof(compat) - size);
3811 from = (const struct bpf_tunnel_key *) compat;
3817 if (unlikely((!(flags & BPF_F_TUNINFO_IPV6) && from->tunnel_label) ||
3822 dst_hold((struct dst_entry *) md);
3823 skb_dst_set(skb, (struct dst_entry *) md);
3825 info = &md->u.tun_info;
3826 memset(info, 0, sizeof(*info));
3827 info->mode = IP_TUNNEL_INFO_TX;
3829 info->key.tun_flags = TUNNEL_KEY | TUNNEL_CSUM | TUNNEL_NOCACHE;
3830 if (flags & BPF_F_DONT_FRAGMENT)
3831 info->key.tun_flags |= TUNNEL_DONT_FRAGMENT;
3832 if (flags & BPF_F_ZERO_CSUM_TX)
3833 info->key.tun_flags &= ~TUNNEL_CSUM;
3834 if (flags & BPF_F_SEQ_NUMBER)
3835 info->key.tun_flags |= TUNNEL_SEQ;
3837 info->key.tun_id = cpu_to_be64(from->tunnel_id);
3838 info->key.tos = from->tunnel_tos;
3839 info->key.ttl = from->tunnel_ttl;
3841 if (flags & BPF_F_TUNINFO_IPV6) {
3842 info->mode |= IP_TUNNEL_INFO_IPV6;
3843 memcpy(&info->key.u.ipv6.dst, from->remote_ipv6,
3844 sizeof(from->remote_ipv6));
3845 info->key.label = cpu_to_be32(from->tunnel_label) &
3846 IPV6_FLOWLABEL_MASK;
3848 info->key.u.ipv4.dst = cpu_to_be32(from->remote_ipv4);
3854 static const struct bpf_func_proto bpf_skb_set_tunnel_key_proto = {
3855 .func = bpf_skb_set_tunnel_key,
3857 .ret_type = RET_INTEGER,
3858 .arg1_type = ARG_PTR_TO_CTX,
3859 .arg2_type = ARG_PTR_TO_MEM,
3860 .arg3_type = ARG_CONST_SIZE,
3861 .arg4_type = ARG_ANYTHING,
3864 BPF_CALL_3(bpf_skb_set_tunnel_opt, struct sk_buff *, skb,
3865 const u8 *, from, u32, size)
3867 struct ip_tunnel_info *info = skb_tunnel_info(skb);
3868 const struct metadata_dst *md = this_cpu_ptr(md_dst);
3870 if (unlikely(info != &md->u.tun_info || (size & (sizeof(u32) - 1))))
3872 if (unlikely(size > IP_TUNNEL_OPTS_MAX))
3875 ip_tunnel_info_opts_set(info, from, size, TUNNEL_OPTIONS_PRESENT);
3880 static const struct bpf_func_proto bpf_skb_set_tunnel_opt_proto = {
3881 .func = bpf_skb_set_tunnel_opt,
3883 .ret_type = RET_INTEGER,
3884 .arg1_type = ARG_PTR_TO_CTX,
3885 .arg2_type = ARG_PTR_TO_MEM,
3886 .arg3_type = ARG_CONST_SIZE,
3889 static const struct bpf_func_proto *
3890 bpf_get_skb_set_tunnel_proto(enum bpf_func_id which)
3893 struct metadata_dst __percpu *tmp;
3895 tmp = metadata_dst_alloc_percpu(IP_TUNNEL_OPTS_MAX,
3900 if (cmpxchg(&md_dst, NULL, tmp))
3901 metadata_dst_free_percpu(tmp);
3905 case BPF_FUNC_skb_set_tunnel_key:
3906 return &bpf_skb_set_tunnel_key_proto;
3907 case BPF_FUNC_skb_set_tunnel_opt:
3908 return &bpf_skb_set_tunnel_opt_proto;
3914 BPF_CALL_3(bpf_skb_under_cgroup, struct sk_buff *, skb, struct bpf_map *, map,
3917 struct bpf_array *array = container_of(map, struct bpf_array, map);
3918 struct cgroup *cgrp;
3921 sk = skb_to_full_sk(skb);
3922 if (!sk || !sk_fullsock(sk))
3924 if (unlikely(idx >= array->map.max_entries))
3927 cgrp = READ_ONCE(array->ptrs[idx]);
3928 if (unlikely(!cgrp))
3931 return sk_under_cgroup_hierarchy(sk, cgrp);
3934 static const struct bpf_func_proto bpf_skb_under_cgroup_proto = {
3935 .func = bpf_skb_under_cgroup,
3937 .ret_type = RET_INTEGER,
3938 .arg1_type = ARG_PTR_TO_CTX,
3939 .arg2_type = ARG_CONST_MAP_PTR,
3940 .arg3_type = ARG_ANYTHING,
3943 #ifdef CONFIG_SOCK_CGROUP_DATA
3944 BPF_CALL_1(bpf_skb_cgroup_id, const struct sk_buff *, skb)
3946 struct sock *sk = skb_to_full_sk(skb);
3947 struct cgroup *cgrp;
3949 if (!sk || !sk_fullsock(sk))
3952 cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
3953 return cgrp->kn->id.id;
3956 static const struct bpf_func_proto bpf_skb_cgroup_id_proto = {
3957 .func = bpf_skb_cgroup_id,
3959 .ret_type = RET_INTEGER,
3960 .arg1_type = ARG_PTR_TO_CTX,
3963 BPF_CALL_2(bpf_skb_ancestor_cgroup_id, const struct sk_buff *, skb, int,
3966 struct sock *sk = skb_to_full_sk(skb);
3967 struct cgroup *ancestor;
3968 struct cgroup *cgrp;
3970 if (!sk || !sk_fullsock(sk))
3973 cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
3974 ancestor = cgroup_ancestor(cgrp, ancestor_level);
3978 return ancestor->kn->id.id;
3981 static const struct bpf_func_proto bpf_skb_ancestor_cgroup_id_proto = {
3982 .func = bpf_skb_ancestor_cgroup_id,
3984 .ret_type = RET_INTEGER,
3985 .arg1_type = ARG_PTR_TO_CTX,
3986 .arg2_type = ARG_ANYTHING,
3990 static unsigned long bpf_xdp_copy(void *dst_buff, const void *src_buff,
3991 unsigned long off, unsigned long len)
3993 memcpy(dst_buff, src_buff + off, len);
3997 BPF_CALL_5(bpf_xdp_event_output, struct xdp_buff *, xdp, struct bpf_map *, map,
3998 u64, flags, void *, meta, u64, meta_size)
4000 u64 xdp_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
4002 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
4004 if (unlikely(xdp_size > (unsigned long)(xdp->data_end - xdp->data)))
4007 return bpf_event_output(map, flags, meta, meta_size, xdp->data,
4008 xdp_size, bpf_xdp_copy);
4011 static const struct bpf_func_proto bpf_xdp_event_output_proto = {
4012 .func = bpf_xdp_event_output,
4014 .ret_type = RET_INTEGER,
4015 .arg1_type = ARG_PTR_TO_CTX,
4016 .arg2_type = ARG_CONST_MAP_PTR,
4017 .arg3_type = ARG_ANYTHING,
4018 .arg4_type = ARG_PTR_TO_MEM,
4019 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
4022 BPF_CALL_1(bpf_get_socket_cookie, struct sk_buff *, skb)
4024 return skb->sk ? sock_gen_cookie(skb->sk) : 0;
4027 static const struct bpf_func_proto bpf_get_socket_cookie_proto = {
4028 .func = bpf_get_socket_cookie,
4030 .ret_type = RET_INTEGER,
4031 .arg1_type = ARG_PTR_TO_CTX,
4034 BPF_CALL_1(bpf_get_socket_cookie_sock_addr, struct bpf_sock_addr_kern *, ctx)
4036 return sock_gen_cookie(ctx->sk);
4039 static const struct bpf_func_proto bpf_get_socket_cookie_sock_addr_proto = {
4040 .func = bpf_get_socket_cookie_sock_addr,
4042 .ret_type = RET_INTEGER,
4043 .arg1_type = ARG_PTR_TO_CTX,
4046 BPF_CALL_1(bpf_get_socket_cookie_sock_ops, struct bpf_sock_ops_kern *, ctx)
4048 return sock_gen_cookie(ctx->sk);
4051 static const struct bpf_func_proto bpf_get_socket_cookie_sock_ops_proto = {
4052 .func = bpf_get_socket_cookie_sock_ops,
4054 .ret_type = RET_INTEGER,
4055 .arg1_type = ARG_PTR_TO_CTX,
4058 BPF_CALL_1(bpf_get_socket_uid, struct sk_buff *, skb)
4060 struct sock *sk = sk_to_full_sk(skb->sk);
4063 if (!sk || !sk_fullsock(sk))
4065 kuid = sock_net_uid(sock_net(sk), sk);
4066 return from_kuid_munged(sock_net(sk)->user_ns, kuid);
4069 static const struct bpf_func_proto bpf_get_socket_uid_proto = {
4070 .func = bpf_get_socket_uid,
4072 .ret_type = RET_INTEGER,
4073 .arg1_type = ARG_PTR_TO_CTX,
4076 BPF_CALL_5(bpf_sockopt_event_output, struct bpf_sock_ops_kern *, bpf_sock,
4077 struct bpf_map *, map, u64, flags, void *, data, u64, size)
4079 if (unlikely(flags & ~(BPF_F_INDEX_MASK)))
4082 return bpf_event_output(map, flags, data, size, NULL, 0, NULL);
4085 static const struct bpf_func_proto bpf_sockopt_event_output_proto = {
4086 .func = bpf_sockopt_event_output,
4088 .ret_type = RET_INTEGER,
4089 .arg1_type = ARG_PTR_TO_CTX,
4090 .arg2_type = ARG_CONST_MAP_PTR,
4091 .arg3_type = ARG_ANYTHING,
4092 .arg4_type = ARG_PTR_TO_MEM,
4093 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
4096 BPF_CALL_5(bpf_setsockopt, struct bpf_sock_ops_kern *, bpf_sock,
4097 int, level, int, optname, char *, optval, int, optlen)
4099 struct sock *sk = bpf_sock->sk;
4103 if (!sk_fullsock(sk))
4106 if (level == SOL_SOCKET) {
4107 if (optlen != sizeof(int))
4109 val = *((int *)optval);
4111 /* Only some socketops are supported */
4114 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
4115 sk->sk_rcvbuf = max_t(int, val * 2, SOCK_MIN_RCVBUF);
4118 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
4119 sk->sk_sndbuf = max_t(int, val * 2, SOCK_MIN_SNDBUF);
4121 case SO_MAX_PACING_RATE: /* 32bit version */
4122 sk->sk_max_pacing_rate = (val == ~0U) ? ~0UL : val;
4123 sk->sk_pacing_rate = min(sk->sk_pacing_rate,
4124 sk->sk_max_pacing_rate);
4127 sk->sk_priority = val;
4132 sk->sk_rcvlowat = val ? : 1;
4135 if (sk->sk_mark != val) {
4144 } else if (level == SOL_IP) {
4145 if (optlen != sizeof(int) || sk->sk_family != AF_INET)
4148 val = *((int *)optval);
4149 /* Only some options are supported */
4152 if (val < -1 || val > 0xff) {
4155 struct inet_sock *inet = inet_sk(sk);
4165 #if IS_ENABLED(CONFIG_IPV6)
4166 } else if (level == SOL_IPV6) {
4167 if (optlen != sizeof(int) || sk->sk_family != AF_INET6)
4170 val = *((int *)optval);
4171 /* Only some options are supported */
4174 if (val < -1 || val > 0xff) {
4177 struct ipv6_pinfo *np = inet6_sk(sk);
4188 } else if (level == SOL_TCP &&
4189 sk->sk_prot->setsockopt == tcp_setsockopt) {
4190 if (optname == TCP_CONGESTION) {
4191 char name[TCP_CA_NAME_MAX];
4192 bool reinit = bpf_sock->op > BPF_SOCK_OPS_NEEDS_ECN;
4194 strncpy(name, optval, min_t(long, optlen,
4195 TCP_CA_NAME_MAX-1));
4196 name[TCP_CA_NAME_MAX-1] = 0;
4197 ret = tcp_set_congestion_control(sk, name, false,
4200 struct tcp_sock *tp = tcp_sk(sk);
4202 if (optlen != sizeof(int))
4205 val = *((int *)optval);
4206 /* Only some options are supported */
4209 if (val <= 0 || tp->data_segs_out > tp->syn_data)
4214 case TCP_BPF_SNDCWND_CLAMP:
4218 tp->snd_cwnd_clamp = val;
4219 tp->snd_ssthresh = val;
4223 if (val < 0 || val > 1)
4239 static const struct bpf_func_proto bpf_setsockopt_proto = {
4240 .func = bpf_setsockopt,
4242 .ret_type = RET_INTEGER,
4243 .arg1_type = ARG_PTR_TO_CTX,
4244 .arg2_type = ARG_ANYTHING,
4245 .arg3_type = ARG_ANYTHING,
4246 .arg4_type = ARG_PTR_TO_MEM,
4247 .arg5_type = ARG_CONST_SIZE,
4250 BPF_CALL_5(bpf_getsockopt, struct bpf_sock_ops_kern *, bpf_sock,
4251 int, level, int, optname, char *, optval, int, optlen)
4253 struct sock *sk = bpf_sock->sk;
4255 if (!sk_fullsock(sk))
4258 if (level == SOL_TCP && sk->sk_prot->getsockopt == tcp_getsockopt) {
4259 struct inet_connection_sock *icsk;
4260 struct tcp_sock *tp;
4263 case TCP_CONGESTION:
4264 icsk = inet_csk(sk);
4266 if (!icsk->icsk_ca_ops || optlen <= 1)
4268 strncpy(optval, icsk->icsk_ca_ops->name, optlen);
4269 optval[optlen - 1] = 0;
4274 if (optlen <= 0 || !tp->saved_syn ||
4275 optlen > tp->saved_syn[0])
4277 memcpy(optval, tp->saved_syn + 1, optlen);
4282 } else if (level == SOL_IP) {
4283 struct inet_sock *inet = inet_sk(sk);
4285 if (optlen != sizeof(int) || sk->sk_family != AF_INET)
4288 /* Only some options are supported */
4291 *((int *)optval) = (int)inet->tos;
4296 #if IS_ENABLED(CONFIG_IPV6)
4297 } else if (level == SOL_IPV6) {
4298 struct ipv6_pinfo *np = inet6_sk(sk);
4300 if (optlen != sizeof(int) || sk->sk_family != AF_INET6)
4303 /* Only some options are supported */
4306 *((int *)optval) = (int)np->tclass;
4318 memset(optval, 0, optlen);
4322 static const struct bpf_func_proto bpf_getsockopt_proto = {
4323 .func = bpf_getsockopt,
4325 .ret_type = RET_INTEGER,
4326 .arg1_type = ARG_PTR_TO_CTX,
4327 .arg2_type = ARG_ANYTHING,
4328 .arg3_type = ARG_ANYTHING,
4329 .arg4_type = ARG_PTR_TO_UNINIT_MEM,
4330 .arg5_type = ARG_CONST_SIZE,
4333 BPF_CALL_2(bpf_sock_ops_cb_flags_set, struct bpf_sock_ops_kern *, bpf_sock,
4336 struct sock *sk = bpf_sock->sk;
4337 int val = argval & BPF_SOCK_OPS_ALL_CB_FLAGS;
4339 if (!IS_ENABLED(CONFIG_INET) || !sk_fullsock(sk))
4343 tcp_sk(sk)->bpf_sock_ops_cb_flags = val;
4345 return argval & (~BPF_SOCK_OPS_ALL_CB_FLAGS);
4348 static const struct bpf_func_proto bpf_sock_ops_cb_flags_set_proto = {
4349 .func = bpf_sock_ops_cb_flags_set,
4351 .ret_type = RET_INTEGER,
4352 .arg1_type = ARG_PTR_TO_CTX,
4353 .arg2_type = ARG_ANYTHING,
4356 const struct ipv6_bpf_stub *ipv6_bpf_stub __read_mostly;
4357 EXPORT_SYMBOL_GPL(ipv6_bpf_stub);
4359 BPF_CALL_3(bpf_bind, struct bpf_sock_addr_kern *, ctx, struct sockaddr *, addr,
4363 struct sock *sk = ctx->sk;
4366 /* Binding to port can be expensive so it's prohibited in the helper.
4367 * Only binding to IP is supported.
4370 if (addr->sa_family == AF_INET) {
4371 if (addr_len < sizeof(struct sockaddr_in))
4373 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
4375 return __inet_bind(sk, addr, addr_len, true, false);
4376 #if IS_ENABLED(CONFIG_IPV6)
4377 } else if (addr->sa_family == AF_INET6) {
4378 if (addr_len < SIN6_LEN_RFC2133)
4380 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
4382 /* ipv6_bpf_stub cannot be NULL, since it's called from
4383 * bpf_cgroup_inet6_connect hook and ipv6 is already loaded
4385 return ipv6_bpf_stub->inet6_bind(sk, addr, addr_len, true, false);
4386 #endif /* CONFIG_IPV6 */
4388 #endif /* CONFIG_INET */
4390 return -EAFNOSUPPORT;
4393 static const struct bpf_func_proto bpf_bind_proto = {
4396 .ret_type = RET_INTEGER,
4397 .arg1_type = ARG_PTR_TO_CTX,
4398 .arg2_type = ARG_PTR_TO_MEM,
4399 .arg3_type = ARG_CONST_SIZE,
4403 BPF_CALL_5(bpf_skb_get_xfrm_state, struct sk_buff *, skb, u32, index,
4404 struct bpf_xfrm_state *, to, u32, size, u64, flags)
4406 const struct sec_path *sp = skb_sec_path(skb);
4407 const struct xfrm_state *x;
4409 if (!sp || unlikely(index >= sp->len || flags))
4412 x = sp->xvec[index];
4414 if (unlikely(size != sizeof(struct bpf_xfrm_state)))
4417 to->reqid = x->props.reqid;
4418 to->spi = x->id.spi;
4419 to->family = x->props.family;
4422 if (to->family == AF_INET6) {
4423 memcpy(to->remote_ipv6, x->props.saddr.a6,
4424 sizeof(to->remote_ipv6));
4426 to->remote_ipv4 = x->props.saddr.a4;
4427 memset(&to->remote_ipv6[1], 0, sizeof(__u32) * 3);
4432 memset(to, 0, size);
4436 static const struct bpf_func_proto bpf_skb_get_xfrm_state_proto = {
4437 .func = bpf_skb_get_xfrm_state,
4439 .ret_type = RET_INTEGER,
4440 .arg1_type = ARG_PTR_TO_CTX,
4441 .arg2_type = ARG_ANYTHING,
4442 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
4443 .arg4_type = ARG_CONST_SIZE,
4444 .arg5_type = ARG_ANYTHING,
4448 #if IS_ENABLED(CONFIG_INET) || IS_ENABLED(CONFIG_IPV6)
4449 static int bpf_fib_set_fwd_params(struct bpf_fib_lookup *params,
4450 const struct neighbour *neigh,
4451 const struct net_device *dev)
4453 memcpy(params->dmac, neigh->ha, ETH_ALEN);
4454 memcpy(params->smac, dev->dev_addr, ETH_ALEN);
4455 params->h_vlan_TCI = 0;
4456 params->h_vlan_proto = 0;
4457 params->ifindex = dev->ifindex;
4463 #if IS_ENABLED(CONFIG_INET)
4464 static int bpf_ipv4_fib_lookup(struct net *net, struct bpf_fib_lookup *params,
4465 u32 flags, bool check_mtu)
4467 struct in_device *in_dev;
4468 struct neighbour *neigh;
4469 struct net_device *dev;
4470 struct fib_result res;
4476 dev = dev_get_by_index_rcu(net, params->ifindex);
4480 /* verify forwarding is enabled on this interface */
4481 in_dev = __in_dev_get_rcu(dev);
4482 if (unlikely(!in_dev || !IN_DEV_FORWARD(in_dev)))
4483 return BPF_FIB_LKUP_RET_FWD_DISABLED;
4485 if (flags & BPF_FIB_LOOKUP_OUTPUT) {
4487 fl4.flowi4_oif = params->ifindex;
4489 fl4.flowi4_iif = params->ifindex;
4492 fl4.flowi4_tos = params->tos & IPTOS_RT_MASK;
4493 fl4.flowi4_scope = RT_SCOPE_UNIVERSE;
4494 fl4.flowi4_flags = 0;
4496 fl4.flowi4_proto = params->l4_protocol;
4497 fl4.daddr = params->ipv4_dst;
4498 fl4.saddr = params->ipv4_src;
4499 fl4.fl4_sport = params->sport;
4500 fl4.fl4_dport = params->dport;
4502 if (flags & BPF_FIB_LOOKUP_DIRECT) {
4503 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
4504 struct fib_table *tb;
4506 tb = fib_get_table(net, tbid);
4508 return BPF_FIB_LKUP_RET_NOT_FWDED;
4510 err = fib_table_lookup(tb, &fl4, &res, FIB_LOOKUP_NOREF);
4512 fl4.flowi4_mark = 0;
4513 fl4.flowi4_secid = 0;
4514 fl4.flowi4_tun_key.tun_id = 0;
4515 fl4.flowi4_uid = sock_net_uid(net, NULL);
4517 err = fib_lookup(net, &fl4, &res, FIB_LOOKUP_NOREF);
4521 /* map fib lookup errors to RTN_ type */
4523 return BPF_FIB_LKUP_RET_BLACKHOLE;
4524 if (err == -EHOSTUNREACH)
4525 return BPF_FIB_LKUP_RET_UNREACHABLE;
4527 return BPF_FIB_LKUP_RET_PROHIBIT;
4529 return BPF_FIB_LKUP_RET_NOT_FWDED;
4532 if (res.type != RTN_UNICAST)
4533 return BPF_FIB_LKUP_RET_NOT_FWDED;
4535 if (res.fi->fib_nhs > 1)
4536 fib_select_path(net, &res, &fl4, NULL);
4539 mtu = ip_mtu_from_fib_result(&res, params->ipv4_dst);
4540 if (params->tot_len > mtu)
4541 return BPF_FIB_LKUP_RET_FRAG_NEEDED;
4544 nh = &res.fi->fib_nh[res.nh_sel];
4546 /* do not handle lwt encaps right now */
4547 if (nh->nh_lwtstate)
4548 return BPF_FIB_LKUP_RET_UNSUPP_LWT;
4552 params->ipv4_dst = nh->nh_gw;
4554 params->rt_metric = res.fi->fib_priority;
4556 /* xdp and cls_bpf programs are run in RCU-bh so
4557 * rcu_read_lock_bh is not needed here
4559 neigh = __ipv4_neigh_lookup_noref(dev, (__force u32)params->ipv4_dst);
4561 return BPF_FIB_LKUP_RET_NO_NEIGH;
4563 return bpf_fib_set_fwd_params(params, neigh, dev);
4567 #if IS_ENABLED(CONFIG_IPV6)
4568 static int bpf_ipv6_fib_lookup(struct net *net, struct bpf_fib_lookup *params,
4569 u32 flags, bool check_mtu)
4571 struct in6_addr *src = (struct in6_addr *) params->ipv6_src;
4572 struct in6_addr *dst = (struct in6_addr *) params->ipv6_dst;
4573 struct neighbour *neigh;
4574 struct net_device *dev;
4575 struct inet6_dev *idev;
4576 struct fib6_info *f6i;
4582 /* link local addresses are never forwarded */
4583 if (rt6_need_strict(dst) || rt6_need_strict(src))
4584 return BPF_FIB_LKUP_RET_NOT_FWDED;
4586 dev = dev_get_by_index_rcu(net, params->ifindex);
4590 idev = __in6_dev_get_safely(dev);
4591 if (unlikely(!idev || !net->ipv6.devconf_all->forwarding))
4592 return BPF_FIB_LKUP_RET_FWD_DISABLED;
4594 if (flags & BPF_FIB_LOOKUP_OUTPUT) {
4596 oif = fl6.flowi6_oif = params->ifindex;
4598 oif = fl6.flowi6_iif = params->ifindex;
4600 strict = RT6_LOOKUP_F_HAS_SADDR;
4602 fl6.flowlabel = params->flowinfo;
4603 fl6.flowi6_scope = 0;
4604 fl6.flowi6_flags = 0;
4607 fl6.flowi6_proto = params->l4_protocol;
4610 fl6.fl6_sport = params->sport;
4611 fl6.fl6_dport = params->dport;
4613 if (flags & BPF_FIB_LOOKUP_DIRECT) {
4614 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
4615 struct fib6_table *tb;
4617 tb = ipv6_stub->fib6_get_table(net, tbid);
4619 return BPF_FIB_LKUP_RET_NOT_FWDED;
4621 f6i = ipv6_stub->fib6_table_lookup(net, tb, oif, &fl6, strict);
4623 fl6.flowi6_mark = 0;
4624 fl6.flowi6_secid = 0;
4625 fl6.flowi6_tun_key.tun_id = 0;
4626 fl6.flowi6_uid = sock_net_uid(net, NULL);
4628 f6i = ipv6_stub->fib6_lookup(net, oif, &fl6, strict);
4631 if (unlikely(IS_ERR_OR_NULL(f6i) || f6i == net->ipv6.fib6_null_entry))
4632 return BPF_FIB_LKUP_RET_NOT_FWDED;
4634 if (unlikely(f6i->fib6_flags & RTF_REJECT)) {
4635 switch (f6i->fib6_type) {
4637 return BPF_FIB_LKUP_RET_BLACKHOLE;
4638 case RTN_UNREACHABLE:
4639 return BPF_FIB_LKUP_RET_UNREACHABLE;
4641 return BPF_FIB_LKUP_RET_PROHIBIT;
4643 return BPF_FIB_LKUP_RET_NOT_FWDED;
4647 if (f6i->fib6_type != RTN_UNICAST)
4648 return BPF_FIB_LKUP_RET_NOT_FWDED;
4650 if (f6i->fib6_nsiblings && fl6.flowi6_oif == 0)
4651 f6i = ipv6_stub->fib6_multipath_select(net, f6i, &fl6,
4652 fl6.flowi6_oif, NULL,
4656 mtu = ipv6_stub->ip6_mtu_from_fib6(f6i, dst, src);
4657 if (params->tot_len > mtu)
4658 return BPF_FIB_LKUP_RET_FRAG_NEEDED;
4661 if (f6i->fib6_nh.nh_lwtstate)
4662 return BPF_FIB_LKUP_RET_UNSUPP_LWT;
4664 if (f6i->fib6_flags & RTF_GATEWAY)
4665 *dst = f6i->fib6_nh.nh_gw;
4667 dev = f6i->fib6_nh.nh_dev;
4668 params->rt_metric = f6i->fib6_metric;
4670 /* xdp and cls_bpf programs are run in RCU-bh so rcu_read_lock_bh is
4671 * not needed here. Can not use __ipv6_neigh_lookup_noref here
4672 * because we need to get nd_tbl via the stub
4674 neigh = ___neigh_lookup_noref(ipv6_stub->nd_tbl, neigh_key_eq128,
4675 ndisc_hashfn, dst, dev);
4677 return BPF_FIB_LKUP_RET_NO_NEIGH;
4679 return bpf_fib_set_fwd_params(params, neigh, dev);
4683 BPF_CALL_4(bpf_xdp_fib_lookup, struct xdp_buff *, ctx,
4684 struct bpf_fib_lookup *, params, int, plen, u32, flags)
4686 if (plen < sizeof(*params))
4689 if (flags & ~(BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_OUTPUT))
4692 switch (params->family) {
4693 #if IS_ENABLED(CONFIG_INET)
4695 return bpf_ipv4_fib_lookup(dev_net(ctx->rxq->dev), params,
4698 #if IS_ENABLED(CONFIG_IPV6)
4700 return bpf_ipv6_fib_lookup(dev_net(ctx->rxq->dev), params,
4704 return -EAFNOSUPPORT;
4707 static const struct bpf_func_proto bpf_xdp_fib_lookup_proto = {
4708 .func = bpf_xdp_fib_lookup,
4710 .ret_type = RET_INTEGER,
4711 .arg1_type = ARG_PTR_TO_CTX,
4712 .arg2_type = ARG_PTR_TO_MEM,
4713 .arg3_type = ARG_CONST_SIZE,
4714 .arg4_type = ARG_ANYTHING,
4717 BPF_CALL_4(bpf_skb_fib_lookup, struct sk_buff *, skb,
4718 struct bpf_fib_lookup *, params, int, plen, u32, flags)
4720 struct net *net = dev_net(skb->dev);
4721 int rc = -EAFNOSUPPORT;
4723 if (plen < sizeof(*params))
4726 if (flags & ~(BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_OUTPUT))
4729 switch (params->family) {
4730 #if IS_ENABLED(CONFIG_INET)
4732 rc = bpf_ipv4_fib_lookup(net, params, flags, false);
4735 #if IS_ENABLED(CONFIG_IPV6)
4737 rc = bpf_ipv6_fib_lookup(net, params, flags, false);
4743 struct net_device *dev;
4745 dev = dev_get_by_index_rcu(net, params->ifindex);
4746 if (!is_skb_forwardable(dev, skb))
4747 rc = BPF_FIB_LKUP_RET_FRAG_NEEDED;
4753 static const struct bpf_func_proto bpf_skb_fib_lookup_proto = {
4754 .func = bpf_skb_fib_lookup,
4756 .ret_type = RET_INTEGER,
4757 .arg1_type = ARG_PTR_TO_CTX,
4758 .arg2_type = ARG_PTR_TO_MEM,
4759 .arg3_type = ARG_CONST_SIZE,
4760 .arg4_type = ARG_ANYTHING,
4763 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
4764 static int bpf_push_seg6_encap(struct sk_buff *skb, u32 type, void *hdr, u32 len)
4767 struct ipv6_sr_hdr *srh = (struct ipv6_sr_hdr *)hdr;
4769 if (!seg6_validate_srh(srh, len))
4773 case BPF_LWT_ENCAP_SEG6_INLINE:
4774 if (skb->protocol != htons(ETH_P_IPV6))
4777 err = seg6_do_srh_inline(skb, srh);
4779 case BPF_LWT_ENCAP_SEG6:
4780 skb_reset_inner_headers(skb);
4781 skb->encapsulation = 1;
4782 err = seg6_do_srh_encap(skb, srh, IPPROTO_IPV6);
4788 bpf_compute_data_pointers(skb);
4792 ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
4793 skb_set_transport_header(skb, sizeof(struct ipv6hdr));
4795 return seg6_lookup_nexthop(skb, NULL, 0);
4797 #endif /* CONFIG_IPV6_SEG6_BPF */
4799 BPF_CALL_4(bpf_lwt_push_encap, struct sk_buff *, skb, u32, type, void *, hdr,
4803 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
4804 case BPF_LWT_ENCAP_SEG6:
4805 case BPF_LWT_ENCAP_SEG6_INLINE:
4806 return bpf_push_seg6_encap(skb, type, hdr, len);
4813 static const struct bpf_func_proto bpf_lwt_push_encap_proto = {
4814 .func = bpf_lwt_push_encap,
4816 .ret_type = RET_INTEGER,
4817 .arg1_type = ARG_PTR_TO_CTX,
4818 .arg2_type = ARG_ANYTHING,
4819 .arg3_type = ARG_PTR_TO_MEM,
4820 .arg4_type = ARG_CONST_SIZE
4823 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
4824 BPF_CALL_4(bpf_lwt_seg6_store_bytes, struct sk_buff *, skb, u32, offset,
4825 const void *, from, u32, len)
4827 struct seg6_bpf_srh_state *srh_state =
4828 this_cpu_ptr(&seg6_bpf_srh_states);
4829 struct ipv6_sr_hdr *srh = srh_state->srh;
4830 void *srh_tlvs, *srh_end, *ptr;
4836 srh_tlvs = (void *)((char *)srh + ((srh->first_segment + 1) << 4));
4837 srh_end = (void *)((char *)srh + sizeof(*srh) + srh_state->hdrlen);
4839 ptr = skb->data + offset;
4840 if (ptr >= srh_tlvs && ptr + len <= srh_end)
4841 srh_state->valid = false;
4842 else if (ptr < (void *)&srh->flags ||
4843 ptr + len > (void *)&srh->segments)
4846 if (unlikely(bpf_try_make_writable(skb, offset + len)))
4848 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0)
4850 srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
4852 memcpy(skb->data + offset, from, len);
4856 static const struct bpf_func_proto bpf_lwt_seg6_store_bytes_proto = {
4857 .func = bpf_lwt_seg6_store_bytes,
4859 .ret_type = RET_INTEGER,
4860 .arg1_type = ARG_PTR_TO_CTX,
4861 .arg2_type = ARG_ANYTHING,
4862 .arg3_type = ARG_PTR_TO_MEM,
4863 .arg4_type = ARG_CONST_SIZE
4866 static void bpf_update_srh_state(struct sk_buff *skb)
4868 struct seg6_bpf_srh_state *srh_state =
4869 this_cpu_ptr(&seg6_bpf_srh_states);
4872 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0) {
4873 srh_state->srh = NULL;
4875 srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
4876 srh_state->hdrlen = srh_state->srh->hdrlen << 3;
4877 srh_state->valid = true;
4881 BPF_CALL_4(bpf_lwt_seg6_action, struct sk_buff *, skb,
4882 u32, action, void *, param, u32, param_len)
4884 struct seg6_bpf_srh_state *srh_state =
4885 this_cpu_ptr(&seg6_bpf_srh_states);
4890 case SEG6_LOCAL_ACTION_END_X:
4891 if (!seg6_bpf_has_valid_srh(skb))
4893 if (param_len != sizeof(struct in6_addr))
4895 return seg6_lookup_nexthop(skb, (struct in6_addr *)param, 0);
4896 case SEG6_LOCAL_ACTION_END_T:
4897 if (!seg6_bpf_has_valid_srh(skb))
4899 if (param_len != sizeof(int))
4901 return seg6_lookup_nexthop(skb, NULL, *(int *)param);
4902 case SEG6_LOCAL_ACTION_END_DT6:
4903 if (!seg6_bpf_has_valid_srh(skb))
4905 if (param_len != sizeof(int))
4908 if (ipv6_find_hdr(skb, &hdroff, IPPROTO_IPV6, NULL, NULL) < 0)
4910 if (!pskb_pull(skb, hdroff))
4913 skb_postpull_rcsum(skb, skb_network_header(skb), hdroff);
4914 skb_reset_network_header(skb);
4915 skb_reset_transport_header(skb);
4916 skb->encapsulation = 0;
4918 bpf_compute_data_pointers(skb);
4919 bpf_update_srh_state(skb);
4920 return seg6_lookup_nexthop(skb, NULL, *(int *)param);
4921 case SEG6_LOCAL_ACTION_END_B6:
4922 if (srh_state->srh && !seg6_bpf_has_valid_srh(skb))
4924 err = bpf_push_seg6_encap(skb, BPF_LWT_ENCAP_SEG6_INLINE,
4927 bpf_update_srh_state(skb);
4930 case SEG6_LOCAL_ACTION_END_B6_ENCAP:
4931 if (srh_state->srh && !seg6_bpf_has_valid_srh(skb))
4933 err = bpf_push_seg6_encap(skb, BPF_LWT_ENCAP_SEG6,
4936 bpf_update_srh_state(skb);
4944 static const struct bpf_func_proto bpf_lwt_seg6_action_proto = {
4945 .func = bpf_lwt_seg6_action,
4947 .ret_type = RET_INTEGER,
4948 .arg1_type = ARG_PTR_TO_CTX,
4949 .arg2_type = ARG_ANYTHING,
4950 .arg3_type = ARG_PTR_TO_MEM,
4951 .arg4_type = ARG_CONST_SIZE
4954 BPF_CALL_3(bpf_lwt_seg6_adjust_srh, struct sk_buff *, skb, u32, offset,
4957 struct seg6_bpf_srh_state *srh_state =
4958 this_cpu_ptr(&seg6_bpf_srh_states);
4959 struct ipv6_sr_hdr *srh = srh_state->srh;
4960 void *srh_end, *srh_tlvs, *ptr;
4961 struct ipv6hdr *hdr;
4965 if (unlikely(srh == NULL))
4968 srh_tlvs = (void *)((unsigned char *)srh + sizeof(*srh) +
4969 ((srh->first_segment + 1) << 4));
4970 srh_end = (void *)((unsigned char *)srh + sizeof(*srh) +
4972 ptr = skb->data + offset;
4974 if (unlikely(ptr < srh_tlvs || ptr > srh_end))
4976 if (unlikely(len < 0 && (void *)((char *)ptr - len) > srh_end))
4980 ret = skb_cow_head(skb, len);
4981 if (unlikely(ret < 0))
4984 ret = bpf_skb_net_hdr_push(skb, offset, len);
4986 ret = bpf_skb_net_hdr_pop(skb, offset, -1 * len);
4989 bpf_compute_data_pointers(skb);
4990 if (unlikely(ret < 0))
4993 hdr = (struct ipv6hdr *)skb->data;
4994 hdr->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
4996 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0)
4998 srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
4999 srh_state->hdrlen += len;
5000 srh_state->valid = false;
5004 static const struct bpf_func_proto bpf_lwt_seg6_adjust_srh_proto = {
5005 .func = bpf_lwt_seg6_adjust_srh,
5007 .ret_type = RET_INTEGER,
5008 .arg1_type = ARG_PTR_TO_CTX,
5009 .arg2_type = ARG_ANYTHING,
5010 .arg3_type = ARG_ANYTHING,
5012 #endif /* CONFIG_IPV6_SEG6_BPF */
5015 static struct sock *sk_lookup(struct net *net, struct bpf_sock_tuple *tuple,
5016 int dif, int sdif, u8 family, u8 proto)
5018 bool refcounted = false;
5019 struct sock *sk = NULL;
5021 if (family == AF_INET) {
5022 __be32 src4 = tuple->ipv4.saddr;
5023 __be32 dst4 = tuple->ipv4.daddr;
5025 if (proto == IPPROTO_TCP)
5026 sk = __inet_lookup(net, &tcp_hashinfo, NULL, 0,
5027 src4, tuple->ipv4.sport,
5028 dst4, tuple->ipv4.dport,
5029 dif, sdif, &refcounted);
5031 sk = __udp4_lib_lookup(net, src4, tuple->ipv4.sport,
5032 dst4, tuple->ipv4.dport,
5033 dif, sdif, &udp_table, NULL);
5034 #if IS_ENABLED(CONFIG_IPV6)
5036 struct in6_addr *src6 = (struct in6_addr *)&tuple->ipv6.saddr;
5037 struct in6_addr *dst6 = (struct in6_addr *)&tuple->ipv6.daddr;
5039 if (proto == IPPROTO_TCP)
5040 sk = __inet6_lookup(net, &tcp_hashinfo, NULL, 0,
5041 src6, tuple->ipv6.sport,
5042 dst6, ntohs(tuple->ipv6.dport),
5043 dif, sdif, &refcounted);
5044 else if (likely(ipv6_bpf_stub))
5045 sk = ipv6_bpf_stub->udp6_lib_lookup(net,
5046 src6, tuple->ipv6.sport,
5047 dst6, tuple->ipv6.dport,
5053 if (unlikely(sk && !refcounted && !sock_flag(sk, SOCK_RCU_FREE))) {
5054 WARN_ONCE(1, "Found non-RCU, unreferenced socket!");
5060 /* bpf_sk_lookup performs the core lookup for different types of sockets,
5061 * taking a reference on the socket if it doesn't have the flag SOCK_RCU_FREE.
5062 * Returns the socket as an 'unsigned long' to simplify the casting in the
5063 * callers to satisfy BPF_CALL declarations.
5065 static unsigned long
5066 __bpf_sk_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
5067 struct net *caller_net, u32 ifindex, u8 proto, u64 netns_id,
5070 struct sock *sk = NULL;
5071 u8 family = AF_UNSPEC;
5075 family = len == sizeof(tuple->ipv4) ? AF_INET : AF_INET6;
5076 if (unlikely(family == AF_UNSPEC || flags ||
5077 !((s32)netns_id < 0 || netns_id <= S32_MAX)))
5080 if (family == AF_INET)
5081 sdif = inet_sdif(skb);
5083 sdif = inet6_sdif(skb);
5085 if ((s32)netns_id < 0) {
5087 sk = sk_lookup(net, tuple, ifindex, sdif, family, proto);
5089 net = get_net_ns_by_id(caller_net, netns_id);
5092 sk = sk_lookup(net, tuple, ifindex, sdif, family, proto);
5097 sk = sk_to_full_sk(sk);
5099 return (unsigned long) sk;
5102 static unsigned long
5103 bpf_sk_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
5104 u8 proto, u64 netns_id, u64 flags)
5106 struct net *caller_net;
5110 caller_net = dev_net(skb->dev);
5111 ifindex = skb->dev->ifindex;
5113 caller_net = sock_net(skb->sk);
5117 return __bpf_sk_lookup(skb, tuple, len, caller_net, ifindex,
5118 proto, netns_id, flags);
5121 BPF_CALL_5(bpf_sk_lookup_tcp, struct sk_buff *, skb,
5122 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
5124 return bpf_sk_lookup(skb, tuple, len, IPPROTO_TCP, netns_id, flags);
5127 static const struct bpf_func_proto bpf_sk_lookup_tcp_proto = {
5128 .func = bpf_sk_lookup_tcp,
5131 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
5132 .arg1_type = ARG_PTR_TO_CTX,
5133 .arg2_type = ARG_PTR_TO_MEM,
5134 .arg3_type = ARG_CONST_SIZE,
5135 .arg4_type = ARG_ANYTHING,
5136 .arg5_type = ARG_ANYTHING,
5139 BPF_CALL_5(bpf_sk_lookup_udp, struct sk_buff *, skb,
5140 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
5142 return bpf_sk_lookup(skb, tuple, len, IPPROTO_UDP, netns_id, flags);
5145 static const struct bpf_func_proto bpf_sk_lookup_udp_proto = {
5146 .func = bpf_sk_lookup_udp,
5149 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
5150 .arg1_type = ARG_PTR_TO_CTX,
5151 .arg2_type = ARG_PTR_TO_MEM,
5152 .arg3_type = ARG_CONST_SIZE,
5153 .arg4_type = ARG_ANYTHING,
5154 .arg5_type = ARG_ANYTHING,
5157 BPF_CALL_1(bpf_sk_release, struct sock *, sk)
5159 if (!sock_flag(sk, SOCK_RCU_FREE))
5164 static const struct bpf_func_proto bpf_sk_release_proto = {
5165 .func = bpf_sk_release,
5167 .ret_type = RET_INTEGER,
5168 .arg1_type = ARG_PTR_TO_SOCKET,
5171 BPF_CALL_5(bpf_xdp_sk_lookup_udp, struct xdp_buff *, ctx,
5172 struct bpf_sock_tuple *, tuple, u32, len, u32, netns_id, u64, flags)
5174 struct net *caller_net = dev_net(ctx->rxq->dev);
5175 int ifindex = ctx->rxq->dev->ifindex;
5177 return __bpf_sk_lookup(NULL, tuple, len, caller_net, ifindex,
5178 IPPROTO_UDP, netns_id, flags);
5181 static const struct bpf_func_proto bpf_xdp_sk_lookup_udp_proto = {
5182 .func = bpf_xdp_sk_lookup_udp,
5185 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
5186 .arg1_type = ARG_PTR_TO_CTX,
5187 .arg2_type = ARG_PTR_TO_MEM,
5188 .arg3_type = ARG_CONST_SIZE,
5189 .arg4_type = ARG_ANYTHING,
5190 .arg5_type = ARG_ANYTHING,
5193 BPF_CALL_5(bpf_xdp_sk_lookup_tcp, struct xdp_buff *, ctx,
5194 struct bpf_sock_tuple *, tuple, u32, len, u32, netns_id, u64, flags)
5196 struct net *caller_net = dev_net(ctx->rxq->dev);
5197 int ifindex = ctx->rxq->dev->ifindex;
5199 return __bpf_sk_lookup(NULL, tuple, len, caller_net, ifindex,
5200 IPPROTO_TCP, netns_id, flags);
5203 static const struct bpf_func_proto bpf_xdp_sk_lookup_tcp_proto = {
5204 .func = bpf_xdp_sk_lookup_tcp,
5207 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
5208 .arg1_type = ARG_PTR_TO_CTX,
5209 .arg2_type = ARG_PTR_TO_MEM,
5210 .arg3_type = ARG_CONST_SIZE,
5211 .arg4_type = ARG_ANYTHING,
5212 .arg5_type = ARG_ANYTHING,
5215 BPF_CALL_5(bpf_sock_addr_sk_lookup_tcp, struct bpf_sock_addr_kern *, ctx,
5216 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
5218 return __bpf_sk_lookup(NULL, tuple, len, sock_net(ctx->sk), 0,
5219 IPPROTO_TCP, netns_id, flags);
5222 static const struct bpf_func_proto bpf_sock_addr_sk_lookup_tcp_proto = {
5223 .func = bpf_sock_addr_sk_lookup_tcp,
5225 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
5226 .arg1_type = ARG_PTR_TO_CTX,
5227 .arg2_type = ARG_PTR_TO_MEM,
5228 .arg3_type = ARG_CONST_SIZE,
5229 .arg4_type = ARG_ANYTHING,
5230 .arg5_type = ARG_ANYTHING,
5233 BPF_CALL_5(bpf_sock_addr_sk_lookup_udp, struct bpf_sock_addr_kern *, ctx,
5234 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
5236 return __bpf_sk_lookup(NULL, tuple, len, sock_net(ctx->sk), 0,
5237 IPPROTO_UDP, netns_id, flags);
5240 static const struct bpf_func_proto bpf_sock_addr_sk_lookup_udp_proto = {
5241 .func = bpf_sock_addr_sk_lookup_udp,
5243 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
5244 .arg1_type = ARG_PTR_TO_CTX,
5245 .arg2_type = ARG_PTR_TO_MEM,
5246 .arg3_type = ARG_CONST_SIZE,
5247 .arg4_type = ARG_ANYTHING,
5248 .arg5_type = ARG_ANYTHING,
5251 #endif /* CONFIG_INET */
5253 bool bpf_helper_changes_pkt_data(void *func)
5255 if (func == bpf_skb_vlan_push ||
5256 func == bpf_skb_vlan_pop ||
5257 func == bpf_skb_store_bytes ||
5258 func == bpf_skb_change_proto ||
5259 func == bpf_skb_change_head ||
5260 func == sk_skb_change_head ||
5261 func == bpf_skb_change_tail ||
5262 func == sk_skb_change_tail ||
5263 func == bpf_skb_adjust_room ||
5264 func == bpf_skb_pull_data ||
5265 func == sk_skb_pull_data ||
5266 func == bpf_clone_redirect ||
5267 func == bpf_l3_csum_replace ||
5268 func == bpf_l4_csum_replace ||
5269 func == bpf_xdp_adjust_head ||
5270 func == bpf_xdp_adjust_meta ||
5271 func == bpf_msg_pull_data ||
5272 func == bpf_msg_push_data ||
5273 func == bpf_msg_pop_data ||
5274 func == bpf_xdp_adjust_tail ||
5275 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
5276 func == bpf_lwt_seg6_store_bytes ||
5277 func == bpf_lwt_seg6_adjust_srh ||
5278 func == bpf_lwt_seg6_action ||
5280 func == bpf_lwt_push_encap)
5286 static const struct bpf_func_proto *
5287 bpf_base_func_proto(enum bpf_func_id func_id)
5290 case BPF_FUNC_map_lookup_elem:
5291 return &bpf_map_lookup_elem_proto;
5292 case BPF_FUNC_map_update_elem:
5293 return &bpf_map_update_elem_proto;
5294 case BPF_FUNC_map_delete_elem:
5295 return &bpf_map_delete_elem_proto;
5296 case BPF_FUNC_map_push_elem:
5297 return &bpf_map_push_elem_proto;
5298 case BPF_FUNC_map_pop_elem:
5299 return &bpf_map_pop_elem_proto;
5300 case BPF_FUNC_map_peek_elem:
5301 return &bpf_map_peek_elem_proto;
5302 case BPF_FUNC_get_prandom_u32:
5303 return &bpf_get_prandom_u32_proto;
5304 case BPF_FUNC_get_smp_processor_id:
5305 return &bpf_get_raw_smp_processor_id_proto;
5306 case BPF_FUNC_get_numa_node_id:
5307 return &bpf_get_numa_node_id_proto;
5308 case BPF_FUNC_tail_call:
5309 return &bpf_tail_call_proto;
5310 case BPF_FUNC_ktime_get_ns:
5311 return &bpf_ktime_get_ns_proto;
5312 case BPF_FUNC_trace_printk:
5313 if (capable(CAP_SYS_ADMIN))
5314 return bpf_get_trace_printk_proto();
5315 /* else, fall through */
5321 static const struct bpf_func_proto *
5322 sock_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5325 /* inet and inet6 sockets are created in a process
5326 * context so there is always a valid uid/gid
5328 case BPF_FUNC_get_current_uid_gid:
5329 return &bpf_get_current_uid_gid_proto;
5330 case BPF_FUNC_get_local_storage:
5331 return &bpf_get_local_storage_proto;
5333 return bpf_base_func_proto(func_id);
5337 static const struct bpf_func_proto *
5338 sock_addr_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5341 /* inet and inet6 sockets are created in a process
5342 * context so there is always a valid uid/gid
5344 case BPF_FUNC_get_current_uid_gid:
5345 return &bpf_get_current_uid_gid_proto;
5347 switch (prog->expected_attach_type) {
5348 case BPF_CGROUP_INET4_CONNECT:
5349 case BPF_CGROUP_INET6_CONNECT:
5350 return &bpf_bind_proto;
5354 case BPF_FUNC_get_socket_cookie:
5355 return &bpf_get_socket_cookie_sock_addr_proto;
5356 case BPF_FUNC_get_local_storage:
5357 return &bpf_get_local_storage_proto;
5359 case BPF_FUNC_sk_lookup_tcp:
5360 return &bpf_sock_addr_sk_lookup_tcp_proto;
5361 case BPF_FUNC_sk_lookup_udp:
5362 return &bpf_sock_addr_sk_lookup_udp_proto;
5363 case BPF_FUNC_sk_release:
5364 return &bpf_sk_release_proto;
5365 #endif /* CONFIG_INET */
5367 return bpf_base_func_proto(func_id);
5371 static const struct bpf_func_proto *
5372 sk_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5375 case BPF_FUNC_skb_load_bytes:
5376 return &bpf_skb_load_bytes_proto;
5377 case BPF_FUNC_skb_load_bytes_relative:
5378 return &bpf_skb_load_bytes_relative_proto;
5379 case BPF_FUNC_get_socket_cookie:
5380 return &bpf_get_socket_cookie_proto;
5381 case BPF_FUNC_get_socket_uid:
5382 return &bpf_get_socket_uid_proto;
5384 return bpf_base_func_proto(func_id);
5388 static const struct bpf_func_proto *
5389 cg_skb_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5392 case BPF_FUNC_get_local_storage:
5393 return &bpf_get_local_storage_proto;
5395 return sk_filter_func_proto(func_id, prog);
5399 static const struct bpf_func_proto *
5400 tc_cls_act_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5403 case BPF_FUNC_skb_store_bytes:
5404 return &bpf_skb_store_bytes_proto;
5405 case BPF_FUNC_skb_load_bytes:
5406 return &bpf_skb_load_bytes_proto;
5407 case BPF_FUNC_skb_load_bytes_relative:
5408 return &bpf_skb_load_bytes_relative_proto;
5409 case BPF_FUNC_skb_pull_data:
5410 return &bpf_skb_pull_data_proto;
5411 case BPF_FUNC_csum_diff:
5412 return &bpf_csum_diff_proto;
5413 case BPF_FUNC_csum_update:
5414 return &bpf_csum_update_proto;
5415 case BPF_FUNC_l3_csum_replace:
5416 return &bpf_l3_csum_replace_proto;
5417 case BPF_FUNC_l4_csum_replace:
5418 return &bpf_l4_csum_replace_proto;
5419 case BPF_FUNC_clone_redirect:
5420 return &bpf_clone_redirect_proto;
5421 case BPF_FUNC_get_cgroup_classid:
5422 return &bpf_get_cgroup_classid_proto;
5423 case BPF_FUNC_skb_vlan_push:
5424 return &bpf_skb_vlan_push_proto;
5425 case BPF_FUNC_skb_vlan_pop:
5426 return &bpf_skb_vlan_pop_proto;
5427 case BPF_FUNC_skb_change_proto:
5428 return &bpf_skb_change_proto_proto;
5429 case BPF_FUNC_skb_change_type:
5430 return &bpf_skb_change_type_proto;
5431 case BPF_FUNC_skb_adjust_room:
5432 return &bpf_skb_adjust_room_proto;
5433 case BPF_FUNC_skb_change_tail:
5434 return &bpf_skb_change_tail_proto;
5435 case BPF_FUNC_skb_get_tunnel_key:
5436 return &bpf_skb_get_tunnel_key_proto;
5437 case BPF_FUNC_skb_set_tunnel_key:
5438 return bpf_get_skb_set_tunnel_proto(func_id);
5439 case BPF_FUNC_skb_get_tunnel_opt:
5440 return &bpf_skb_get_tunnel_opt_proto;
5441 case BPF_FUNC_skb_set_tunnel_opt:
5442 return bpf_get_skb_set_tunnel_proto(func_id);
5443 case BPF_FUNC_redirect:
5444 return &bpf_redirect_proto;
5445 case BPF_FUNC_get_route_realm:
5446 return &bpf_get_route_realm_proto;
5447 case BPF_FUNC_get_hash_recalc:
5448 return &bpf_get_hash_recalc_proto;
5449 case BPF_FUNC_set_hash_invalid:
5450 return &bpf_set_hash_invalid_proto;
5451 case BPF_FUNC_set_hash:
5452 return &bpf_set_hash_proto;
5453 case BPF_FUNC_perf_event_output:
5454 return &bpf_skb_event_output_proto;
5455 case BPF_FUNC_get_smp_processor_id:
5456 return &bpf_get_smp_processor_id_proto;
5457 case BPF_FUNC_skb_under_cgroup:
5458 return &bpf_skb_under_cgroup_proto;
5459 case BPF_FUNC_get_socket_cookie:
5460 return &bpf_get_socket_cookie_proto;
5461 case BPF_FUNC_get_socket_uid:
5462 return &bpf_get_socket_uid_proto;
5463 case BPF_FUNC_fib_lookup:
5464 return &bpf_skb_fib_lookup_proto;
5466 case BPF_FUNC_skb_get_xfrm_state:
5467 return &bpf_skb_get_xfrm_state_proto;
5469 #ifdef CONFIG_SOCK_CGROUP_DATA
5470 case BPF_FUNC_skb_cgroup_id:
5471 return &bpf_skb_cgroup_id_proto;
5472 case BPF_FUNC_skb_ancestor_cgroup_id:
5473 return &bpf_skb_ancestor_cgroup_id_proto;
5476 case BPF_FUNC_sk_lookup_tcp:
5477 return &bpf_sk_lookup_tcp_proto;
5478 case BPF_FUNC_sk_lookup_udp:
5479 return &bpf_sk_lookup_udp_proto;
5480 case BPF_FUNC_sk_release:
5481 return &bpf_sk_release_proto;
5484 return bpf_base_func_proto(func_id);
5488 static const struct bpf_func_proto *
5489 xdp_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5492 case BPF_FUNC_perf_event_output:
5493 return &bpf_xdp_event_output_proto;
5494 case BPF_FUNC_get_smp_processor_id:
5495 return &bpf_get_smp_processor_id_proto;
5496 case BPF_FUNC_csum_diff:
5497 return &bpf_csum_diff_proto;
5498 case BPF_FUNC_xdp_adjust_head:
5499 return &bpf_xdp_adjust_head_proto;
5500 case BPF_FUNC_xdp_adjust_meta:
5501 return &bpf_xdp_adjust_meta_proto;
5502 case BPF_FUNC_redirect:
5503 return &bpf_xdp_redirect_proto;
5504 case BPF_FUNC_redirect_map:
5505 return &bpf_xdp_redirect_map_proto;
5506 case BPF_FUNC_xdp_adjust_tail:
5507 return &bpf_xdp_adjust_tail_proto;
5508 case BPF_FUNC_fib_lookup:
5509 return &bpf_xdp_fib_lookup_proto;
5511 case BPF_FUNC_sk_lookup_udp:
5512 return &bpf_xdp_sk_lookup_udp_proto;
5513 case BPF_FUNC_sk_lookup_tcp:
5514 return &bpf_xdp_sk_lookup_tcp_proto;
5515 case BPF_FUNC_sk_release:
5516 return &bpf_sk_release_proto;
5519 return bpf_base_func_proto(func_id);
5523 const struct bpf_func_proto bpf_sock_map_update_proto __weak;
5524 const struct bpf_func_proto bpf_sock_hash_update_proto __weak;
5526 static const struct bpf_func_proto *
5527 sock_ops_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5530 case BPF_FUNC_setsockopt:
5531 return &bpf_setsockopt_proto;
5532 case BPF_FUNC_getsockopt:
5533 return &bpf_getsockopt_proto;
5534 case BPF_FUNC_sock_ops_cb_flags_set:
5535 return &bpf_sock_ops_cb_flags_set_proto;
5536 case BPF_FUNC_sock_map_update:
5537 return &bpf_sock_map_update_proto;
5538 case BPF_FUNC_sock_hash_update:
5539 return &bpf_sock_hash_update_proto;
5540 case BPF_FUNC_get_socket_cookie:
5541 return &bpf_get_socket_cookie_sock_ops_proto;
5542 case BPF_FUNC_get_local_storage:
5543 return &bpf_get_local_storage_proto;
5544 case BPF_FUNC_perf_event_output:
5545 return &bpf_sockopt_event_output_proto;
5547 return bpf_base_func_proto(func_id);
5551 const struct bpf_func_proto bpf_msg_redirect_map_proto __weak;
5552 const struct bpf_func_proto bpf_msg_redirect_hash_proto __weak;
5554 static const struct bpf_func_proto *
5555 sk_msg_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5558 case BPF_FUNC_msg_redirect_map:
5559 return &bpf_msg_redirect_map_proto;
5560 case BPF_FUNC_msg_redirect_hash:
5561 return &bpf_msg_redirect_hash_proto;
5562 case BPF_FUNC_msg_apply_bytes:
5563 return &bpf_msg_apply_bytes_proto;
5564 case BPF_FUNC_msg_cork_bytes:
5565 return &bpf_msg_cork_bytes_proto;
5566 case BPF_FUNC_msg_pull_data:
5567 return &bpf_msg_pull_data_proto;
5568 case BPF_FUNC_msg_push_data:
5569 return &bpf_msg_push_data_proto;
5570 case BPF_FUNC_msg_pop_data:
5571 return &bpf_msg_pop_data_proto;
5573 return bpf_base_func_proto(func_id);
5577 const struct bpf_func_proto bpf_sk_redirect_map_proto __weak;
5578 const struct bpf_func_proto bpf_sk_redirect_hash_proto __weak;
5580 static const struct bpf_func_proto *
5581 sk_skb_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5584 case BPF_FUNC_skb_store_bytes:
5585 return &bpf_skb_store_bytes_proto;
5586 case BPF_FUNC_skb_load_bytes:
5587 return &bpf_skb_load_bytes_proto;
5588 case BPF_FUNC_skb_pull_data:
5589 return &sk_skb_pull_data_proto;
5590 case BPF_FUNC_skb_change_tail:
5591 return &sk_skb_change_tail_proto;
5592 case BPF_FUNC_skb_change_head:
5593 return &sk_skb_change_head_proto;
5594 case BPF_FUNC_get_socket_cookie:
5595 return &bpf_get_socket_cookie_proto;
5596 case BPF_FUNC_get_socket_uid:
5597 return &bpf_get_socket_uid_proto;
5598 case BPF_FUNC_sk_redirect_map:
5599 return &bpf_sk_redirect_map_proto;
5600 case BPF_FUNC_sk_redirect_hash:
5601 return &bpf_sk_redirect_hash_proto;
5603 case BPF_FUNC_sk_lookup_tcp:
5604 return &bpf_sk_lookup_tcp_proto;
5605 case BPF_FUNC_sk_lookup_udp:
5606 return &bpf_sk_lookup_udp_proto;
5607 case BPF_FUNC_sk_release:
5608 return &bpf_sk_release_proto;
5611 return bpf_base_func_proto(func_id);
5615 static const struct bpf_func_proto *
5616 flow_dissector_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5619 case BPF_FUNC_skb_load_bytes:
5620 return &bpf_skb_load_bytes_proto;
5622 return bpf_base_func_proto(func_id);
5626 static const struct bpf_func_proto *
5627 lwt_out_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5630 case BPF_FUNC_skb_load_bytes:
5631 return &bpf_skb_load_bytes_proto;
5632 case BPF_FUNC_skb_pull_data:
5633 return &bpf_skb_pull_data_proto;
5634 case BPF_FUNC_csum_diff:
5635 return &bpf_csum_diff_proto;
5636 case BPF_FUNC_get_cgroup_classid:
5637 return &bpf_get_cgroup_classid_proto;
5638 case BPF_FUNC_get_route_realm:
5639 return &bpf_get_route_realm_proto;
5640 case BPF_FUNC_get_hash_recalc:
5641 return &bpf_get_hash_recalc_proto;
5642 case BPF_FUNC_perf_event_output:
5643 return &bpf_skb_event_output_proto;
5644 case BPF_FUNC_get_smp_processor_id:
5645 return &bpf_get_smp_processor_id_proto;
5646 case BPF_FUNC_skb_under_cgroup:
5647 return &bpf_skb_under_cgroup_proto;
5649 return bpf_base_func_proto(func_id);
5653 static const struct bpf_func_proto *
5654 lwt_in_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5657 case BPF_FUNC_lwt_push_encap:
5658 return &bpf_lwt_push_encap_proto;
5660 return lwt_out_func_proto(func_id, prog);
5664 static const struct bpf_func_proto *
5665 lwt_xmit_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5668 case BPF_FUNC_skb_get_tunnel_key:
5669 return &bpf_skb_get_tunnel_key_proto;
5670 case BPF_FUNC_skb_set_tunnel_key:
5671 return bpf_get_skb_set_tunnel_proto(func_id);
5672 case BPF_FUNC_skb_get_tunnel_opt:
5673 return &bpf_skb_get_tunnel_opt_proto;
5674 case BPF_FUNC_skb_set_tunnel_opt:
5675 return bpf_get_skb_set_tunnel_proto(func_id);
5676 case BPF_FUNC_redirect:
5677 return &bpf_redirect_proto;
5678 case BPF_FUNC_clone_redirect:
5679 return &bpf_clone_redirect_proto;
5680 case BPF_FUNC_skb_change_tail:
5681 return &bpf_skb_change_tail_proto;
5682 case BPF_FUNC_skb_change_head:
5683 return &bpf_skb_change_head_proto;
5684 case BPF_FUNC_skb_store_bytes:
5685 return &bpf_skb_store_bytes_proto;
5686 case BPF_FUNC_csum_update:
5687 return &bpf_csum_update_proto;
5688 case BPF_FUNC_l3_csum_replace:
5689 return &bpf_l3_csum_replace_proto;
5690 case BPF_FUNC_l4_csum_replace:
5691 return &bpf_l4_csum_replace_proto;
5692 case BPF_FUNC_set_hash_invalid:
5693 return &bpf_set_hash_invalid_proto;
5695 return lwt_out_func_proto(func_id, prog);
5699 static const struct bpf_func_proto *
5700 lwt_seg6local_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5703 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
5704 case BPF_FUNC_lwt_seg6_store_bytes:
5705 return &bpf_lwt_seg6_store_bytes_proto;
5706 case BPF_FUNC_lwt_seg6_action:
5707 return &bpf_lwt_seg6_action_proto;
5708 case BPF_FUNC_lwt_seg6_adjust_srh:
5709 return &bpf_lwt_seg6_adjust_srh_proto;
5712 return lwt_out_func_proto(func_id, prog);
5716 static bool bpf_skb_is_valid_access(int off, int size, enum bpf_access_type type,
5717 const struct bpf_prog *prog,
5718 struct bpf_insn_access_aux *info)
5720 const int size_default = sizeof(__u32);
5722 if (off < 0 || off >= sizeof(struct __sk_buff))
5725 /* The verifier guarantees that size > 0. */
5726 if (off % size != 0)
5730 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
5731 if (off + size > offsetofend(struct __sk_buff, cb[4]))
5734 case bpf_ctx_range_till(struct __sk_buff, remote_ip6[0], remote_ip6[3]):
5735 case bpf_ctx_range_till(struct __sk_buff, local_ip6[0], local_ip6[3]):
5736 case bpf_ctx_range_till(struct __sk_buff, remote_ip4, remote_ip4):
5737 case bpf_ctx_range_till(struct __sk_buff, local_ip4, local_ip4):
5738 case bpf_ctx_range(struct __sk_buff, data):
5739 case bpf_ctx_range(struct __sk_buff, data_meta):
5740 case bpf_ctx_range(struct __sk_buff, data_end):
5741 if (size != size_default)
5744 case bpf_ctx_range_ptr(struct __sk_buff, flow_keys):
5745 if (size != sizeof(__u64))
5748 case bpf_ctx_range(struct __sk_buff, tstamp):
5749 if (size != sizeof(__u64))
5753 /* Only narrow read access allowed for now. */
5754 if (type == BPF_WRITE) {
5755 if (size != size_default)
5758 bpf_ctx_record_field_size(info, size_default);
5759 if (!bpf_ctx_narrow_access_ok(off, size, size_default))
5767 static bool sk_filter_is_valid_access(int off, int size,
5768 enum bpf_access_type type,
5769 const struct bpf_prog *prog,
5770 struct bpf_insn_access_aux *info)
5773 case bpf_ctx_range(struct __sk_buff, tc_classid):
5774 case bpf_ctx_range(struct __sk_buff, data):
5775 case bpf_ctx_range(struct __sk_buff, data_meta):
5776 case bpf_ctx_range(struct __sk_buff, data_end):
5777 case bpf_ctx_range_ptr(struct __sk_buff, flow_keys):
5778 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
5779 case bpf_ctx_range(struct __sk_buff, tstamp):
5780 case bpf_ctx_range(struct __sk_buff, wire_len):
5784 if (type == BPF_WRITE) {
5786 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
5793 return bpf_skb_is_valid_access(off, size, type, prog, info);
5796 static bool cg_skb_is_valid_access(int off, int size,
5797 enum bpf_access_type type,
5798 const struct bpf_prog *prog,
5799 struct bpf_insn_access_aux *info)
5802 case bpf_ctx_range(struct __sk_buff, tc_classid):
5803 case bpf_ctx_range(struct __sk_buff, data_meta):
5804 case bpf_ctx_range_ptr(struct __sk_buff, flow_keys):
5805 case bpf_ctx_range(struct __sk_buff, wire_len):
5807 case bpf_ctx_range(struct __sk_buff, data):
5808 case bpf_ctx_range(struct __sk_buff, data_end):
5809 if (!capable(CAP_SYS_ADMIN))
5814 if (type == BPF_WRITE) {
5816 case bpf_ctx_range(struct __sk_buff, mark):
5817 case bpf_ctx_range(struct __sk_buff, priority):
5818 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
5820 case bpf_ctx_range(struct __sk_buff, tstamp):
5821 if (!capable(CAP_SYS_ADMIN))
5830 case bpf_ctx_range(struct __sk_buff, data):
5831 info->reg_type = PTR_TO_PACKET;
5833 case bpf_ctx_range(struct __sk_buff, data_end):
5834 info->reg_type = PTR_TO_PACKET_END;
5838 return bpf_skb_is_valid_access(off, size, type, prog, info);
5841 static bool lwt_is_valid_access(int off, int size,
5842 enum bpf_access_type type,
5843 const struct bpf_prog *prog,
5844 struct bpf_insn_access_aux *info)
5847 case bpf_ctx_range(struct __sk_buff, tc_classid):
5848 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
5849 case bpf_ctx_range(struct __sk_buff, data_meta):
5850 case bpf_ctx_range_ptr(struct __sk_buff, flow_keys):
5851 case bpf_ctx_range(struct __sk_buff, tstamp):
5852 case bpf_ctx_range(struct __sk_buff, wire_len):
5856 if (type == BPF_WRITE) {
5858 case bpf_ctx_range(struct __sk_buff, mark):
5859 case bpf_ctx_range(struct __sk_buff, priority):
5860 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
5868 case bpf_ctx_range(struct __sk_buff, data):
5869 info->reg_type = PTR_TO_PACKET;
5871 case bpf_ctx_range(struct __sk_buff, data_end):
5872 info->reg_type = PTR_TO_PACKET_END;
5876 return bpf_skb_is_valid_access(off, size, type, prog, info);
5879 /* Attach type specific accesses */
5880 static bool __sock_filter_check_attach_type(int off,
5881 enum bpf_access_type access_type,
5882 enum bpf_attach_type attach_type)
5885 case offsetof(struct bpf_sock, bound_dev_if):
5886 case offsetof(struct bpf_sock, mark):
5887 case offsetof(struct bpf_sock, priority):
5888 switch (attach_type) {
5889 case BPF_CGROUP_INET_SOCK_CREATE:
5894 case bpf_ctx_range(struct bpf_sock, src_ip4):
5895 switch (attach_type) {
5896 case BPF_CGROUP_INET4_POST_BIND:
5901 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
5902 switch (attach_type) {
5903 case BPF_CGROUP_INET6_POST_BIND:
5908 case bpf_ctx_range(struct bpf_sock, src_port):
5909 switch (attach_type) {
5910 case BPF_CGROUP_INET4_POST_BIND:
5911 case BPF_CGROUP_INET6_POST_BIND:
5918 return access_type == BPF_READ;
5923 static bool __sock_filter_check_size(int off, int size,
5924 struct bpf_insn_access_aux *info)
5926 const int size_default = sizeof(__u32);
5929 case bpf_ctx_range(struct bpf_sock, src_ip4):
5930 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
5931 bpf_ctx_record_field_size(info, size_default);
5932 return bpf_ctx_narrow_access_ok(off, size, size_default);
5935 return size == size_default;
5938 bool bpf_sock_is_valid_access(int off, int size, enum bpf_access_type type,
5939 struct bpf_insn_access_aux *info)
5941 if (off < 0 || off >= sizeof(struct bpf_sock))
5943 if (off % size != 0)
5945 if (!__sock_filter_check_size(off, size, info))
5950 static bool sock_filter_is_valid_access(int off, int size,
5951 enum bpf_access_type type,
5952 const struct bpf_prog *prog,
5953 struct bpf_insn_access_aux *info)
5955 if (!bpf_sock_is_valid_access(off, size, type, info))
5957 return __sock_filter_check_attach_type(off, type,
5958 prog->expected_attach_type);
5961 static int bpf_noop_prologue(struct bpf_insn *insn_buf, bool direct_write,
5962 const struct bpf_prog *prog)
5964 /* Neither direct read nor direct write requires any preliminary
5970 static int bpf_unclone_prologue(struct bpf_insn *insn_buf, bool direct_write,
5971 const struct bpf_prog *prog, int drop_verdict)
5973 struct bpf_insn *insn = insn_buf;
5978 /* if (!skb->cloned)
5981 * (Fast-path, otherwise approximation that we might be
5982 * a clone, do the rest in helper.)
5984 *insn++ = BPF_LDX_MEM(BPF_B, BPF_REG_6, BPF_REG_1, CLONED_OFFSET());
5985 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_6, CLONED_MASK);
5986 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_6, 0, 7);
5988 /* ret = bpf_skb_pull_data(skb, 0); */
5989 *insn++ = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1);
5990 *insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_2, BPF_REG_2);
5991 *insn++ = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
5992 BPF_FUNC_skb_pull_data);
5995 * return TC_ACT_SHOT;
5997 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2);
5998 *insn++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_0, drop_verdict);
5999 *insn++ = BPF_EXIT_INSN();
6002 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_6);
6004 *insn++ = prog->insnsi[0];
6006 return insn - insn_buf;
6009 static int bpf_gen_ld_abs(const struct bpf_insn *orig,
6010 struct bpf_insn *insn_buf)
6012 bool indirect = BPF_MODE(orig->code) == BPF_IND;
6013 struct bpf_insn *insn = insn_buf;
6015 /* We're guaranteed here that CTX is in R6. */
6016 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_CTX);
6018 *insn++ = BPF_MOV64_IMM(BPF_REG_2, orig->imm);
6020 *insn++ = BPF_MOV64_REG(BPF_REG_2, orig->src_reg);
6022 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, orig->imm);
6025 switch (BPF_SIZE(orig->code)) {
6027 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_8_no_cache);
6030 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_16_no_cache);
6033 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_32_no_cache);
6037 *insn++ = BPF_JMP_IMM(BPF_JSGE, BPF_REG_0, 0, 2);
6038 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_0, BPF_REG_0);
6039 *insn++ = BPF_EXIT_INSN();
6041 return insn - insn_buf;
6044 static int tc_cls_act_prologue(struct bpf_insn *insn_buf, bool direct_write,
6045 const struct bpf_prog *prog)
6047 return bpf_unclone_prologue(insn_buf, direct_write, prog, TC_ACT_SHOT);
6050 static bool tc_cls_act_is_valid_access(int off, int size,
6051 enum bpf_access_type type,
6052 const struct bpf_prog *prog,
6053 struct bpf_insn_access_aux *info)
6055 if (type == BPF_WRITE) {
6057 case bpf_ctx_range(struct __sk_buff, mark):
6058 case bpf_ctx_range(struct __sk_buff, tc_index):
6059 case bpf_ctx_range(struct __sk_buff, priority):
6060 case bpf_ctx_range(struct __sk_buff, tc_classid):
6061 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
6062 case bpf_ctx_range(struct __sk_buff, tstamp):
6070 case bpf_ctx_range(struct __sk_buff, data):
6071 info->reg_type = PTR_TO_PACKET;
6073 case bpf_ctx_range(struct __sk_buff, data_meta):
6074 info->reg_type = PTR_TO_PACKET_META;
6076 case bpf_ctx_range(struct __sk_buff, data_end):
6077 info->reg_type = PTR_TO_PACKET_END;
6079 case bpf_ctx_range_ptr(struct __sk_buff, flow_keys):
6080 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
6084 return bpf_skb_is_valid_access(off, size, type, prog, info);
6087 static bool __is_valid_xdp_access(int off, int size)
6089 if (off < 0 || off >= sizeof(struct xdp_md))
6091 if (off % size != 0)
6093 if (size != sizeof(__u32))
6099 static bool xdp_is_valid_access(int off, int size,
6100 enum bpf_access_type type,
6101 const struct bpf_prog *prog,
6102 struct bpf_insn_access_aux *info)
6104 if (type == BPF_WRITE) {
6105 if (bpf_prog_is_dev_bound(prog->aux)) {
6107 case offsetof(struct xdp_md, rx_queue_index):
6108 return __is_valid_xdp_access(off, size);
6115 case offsetof(struct xdp_md, data):
6116 info->reg_type = PTR_TO_PACKET;
6118 case offsetof(struct xdp_md, data_meta):
6119 info->reg_type = PTR_TO_PACKET_META;
6121 case offsetof(struct xdp_md, data_end):
6122 info->reg_type = PTR_TO_PACKET_END;
6126 return __is_valid_xdp_access(off, size);
6129 void bpf_warn_invalid_xdp_action(u32 act)
6131 const u32 act_max = XDP_REDIRECT;
6133 WARN_ONCE(1, "%s XDP return value %u, expect packet loss!\n",
6134 act > act_max ? "Illegal" : "Driver unsupported",
6137 EXPORT_SYMBOL_GPL(bpf_warn_invalid_xdp_action);
6139 static bool sock_addr_is_valid_access(int off, int size,
6140 enum bpf_access_type type,
6141 const struct bpf_prog *prog,
6142 struct bpf_insn_access_aux *info)
6144 const int size_default = sizeof(__u32);
6146 if (off < 0 || off >= sizeof(struct bpf_sock_addr))
6148 if (off % size != 0)
6151 /* Disallow access to IPv6 fields from IPv4 contex and vise
6155 case bpf_ctx_range(struct bpf_sock_addr, user_ip4):
6156 switch (prog->expected_attach_type) {
6157 case BPF_CGROUP_INET4_BIND:
6158 case BPF_CGROUP_INET4_CONNECT:
6159 case BPF_CGROUP_UDP4_SENDMSG:
6165 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
6166 switch (prog->expected_attach_type) {
6167 case BPF_CGROUP_INET6_BIND:
6168 case BPF_CGROUP_INET6_CONNECT:
6169 case BPF_CGROUP_UDP6_SENDMSG:
6175 case bpf_ctx_range(struct bpf_sock_addr, msg_src_ip4):
6176 switch (prog->expected_attach_type) {
6177 case BPF_CGROUP_UDP4_SENDMSG:
6183 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
6185 switch (prog->expected_attach_type) {
6186 case BPF_CGROUP_UDP6_SENDMSG:
6195 case bpf_ctx_range(struct bpf_sock_addr, user_ip4):
6196 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
6197 case bpf_ctx_range(struct bpf_sock_addr, msg_src_ip4):
6198 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
6200 /* Only narrow read access allowed for now. */
6201 if (type == BPF_READ) {
6202 bpf_ctx_record_field_size(info, size_default);
6203 if (!bpf_ctx_narrow_access_ok(off, size, size_default))
6206 if (size != size_default)
6210 case bpf_ctx_range(struct bpf_sock_addr, user_port):
6211 if (size != size_default)
6215 if (type == BPF_READ) {
6216 if (size != size_default)
6226 static bool sock_ops_is_valid_access(int off, int size,
6227 enum bpf_access_type type,
6228 const struct bpf_prog *prog,
6229 struct bpf_insn_access_aux *info)
6231 const int size_default = sizeof(__u32);
6233 if (off < 0 || off >= sizeof(struct bpf_sock_ops))
6236 /* The verifier guarantees that size > 0. */
6237 if (off % size != 0)
6240 if (type == BPF_WRITE) {
6242 case offsetof(struct bpf_sock_ops, reply):
6243 case offsetof(struct bpf_sock_ops, sk_txhash):
6244 if (size != size_default)
6252 case bpf_ctx_range_till(struct bpf_sock_ops, bytes_received,
6254 if (size != sizeof(__u64))
6258 if (size != size_default)
6267 static int sk_skb_prologue(struct bpf_insn *insn_buf, bool direct_write,
6268 const struct bpf_prog *prog)
6270 return bpf_unclone_prologue(insn_buf, direct_write, prog, SK_DROP);
6273 static bool sk_skb_is_valid_access(int off, int size,
6274 enum bpf_access_type type,
6275 const struct bpf_prog *prog,
6276 struct bpf_insn_access_aux *info)
6279 case bpf_ctx_range(struct __sk_buff, tc_classid):
6280 case bpf_ctx_range(struct __sk_buff, data_meta):
6281 case bpf_ctx_range_ptr(struct __sk_buff, flow_keys):
6282 case bpf_ctx_range(struct __sk_buff, tstamp):
6283 case bpf_ctx_range(struct __sk_buff, wire_len):
6287 if (type == BPF_WRITE) {
6289 case bpf_ctx_range(struct __sk_buff, tc_index):
6290 case bpf_ctx_range(struct __sk_buff, priority):
6298 case bpf_ctx_range(struct __sk_buff, mark):
6300 case bpf_ctx_range(struct __sk_buff, data):
6301 info->reg_type = PTR_TO_PACKET;
6303 case bpf_ctx_range(struct __sk_buff, data_end):
6304 info->reg_type = PTR_TO_PACKET_END;
6308 return bpf_skb_is_valid_access(off, size, type, prog, info);
6311 static bool sk_msg_is_valid_access(int off, int size,
6312 enum bpf_access_type type,
6313 const struct bpf_prog *prog,
6314 struct bpf_insn_access_aux *info)
6316 if (type == BPF_WRITE)
6319 if (off % size != 0)
6323 case offsetof(struct sk_msg_md, data):
6324 info->reg_type = PTR_TO_PACKET;
6325 if (size != sizeof(__u64))
6328 case offsetof(struct sk_msg_md, data_end):
6329 info->reg_type = PTR_TO_PACKET_END;
6330 if (size != sizeof(__u64))
6333 case bpf_ctx_range(struct sk_msg_md, family):
6334 case bpf_ctx_range(struct sk_msg_md, remote_ip4):
6335 case bpf_ctx_range(struct sk_msg_md, local_ip4):
6336 case bpf_ctx_range_till(struct sk_msg_md, remote_ip6[0], remote_ip6[3]):
6337 case bpf_ctx_range_till(struct sk_msg_md, local_ip6[0], local_ip6[3]):
6338 case bpf_ctx_range(struct sk_msg_md, remote_port):
6339 case bpf_ctx_range(struct sk_msg_md, local_port):
6340 case bpf_ctx_range(struct sk_msg_md, size):
6341 if (size != sizeof(__u32))
6350 static bool flow_dissector_is_valid_access(int off, int size,
6351 enum bpf_access_type type,
6352 const struct bpf_prog *prog,
6353 struct bpf_insn_access_aux *info)
6355 if (type == BPF_WRITE) {
6357 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
6365 case bpf_ctx_range(struct __sk_buff, data):
6366 info->reg_type = PTR_TO_PACKET;
6368 case bpf_ctx_range(struct __sk_buff, data_end):
6369 info->reg_type = PTR_TO_PACKET_END;
6371 case bpf_ctx_range_ptr(struct __sk_buff, flow_keys):
6372 info->reg_type = PTR_TO_FLOW_KEYS;
6374 case bpf_ctx_range(struct __sk_buff, tc_classid):
6375 case bpf_ctx_range(struct __sk_buff, data_meta):
6376 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
6377 case bpf_ctx_range(struct __sk_buff, tstamp):
6378 case bpf_ctx_range(struct __sk_buff, wire_len):
6382 return bpf_skb_is_valid_access(off, size, type, prog, info);
6385 static u32 bpf_convert_ctx_access(enum bpf_access_type type,
6386 const struct bpf_insn *si,
6387 struct bpf_insn *insn_buf,
6388 struct bpf_prog *prog, u32 *target_size)
6390 struct bpf_insn *insn = insn_buf;
6394 case offsetof(struct __sk_buff, len):
6395 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6396 bpf_target_off(struct sk_buff, len, 4,
6400 case offsetof(struct __sk_buff, protocol):
6401 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
6402 bpf_target_off(struct sk_buff, protocol, 2,
6406 case offsetof(struct __sk_buff, vlan_proto):
6407 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
6408 bpf_target_off(struct sk_buff, vlan_proto, 2,
6412 case offsetof(struct __sk_buff, priority):
6413 if (type == BPF_WRITE)
6414 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
6415 bpf_target_off(struct sk_buff, priority, 4,
6418 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6419 bpf_target_off(struct sk_buff, priority, 4,
6423 case offsetof(struct __sk_buff, ingress_ifindex):
6424 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6425 bpf_target_off(struct sk_buff, skb_iif, 4,
6429 case offsetof(struct __sk_buff, ifindex):
6430 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
6431 si->dst_reg, si->src_reg,
6432 offsetof(struct sk_buff, dev));
6433 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
6434 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6435 bpf_target_off(struct net_device, ifindex, 4,
6439 case offsetof(struct __sk_buff, hash):
6440 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6441 bpf_target_off(struct sk_buff, hash, 4,
6445 case offsetof(struct __sk_buff, mark):
6446 if (type == BPF_WRITE)
6447 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
6448 bpf_target_off(struct sk_buff, mark, 4,
6451 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6452 bpf_target_off(struct sk_buff, mark, 4,
6456 case offsetof(struct __sk_buff, pkt_type):
6458 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->src_reg,
6460 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, PKT_TYPE_MAX);
6461 #ifdef __BIG_ENDIAN_BITFIELD
6462 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, 5);
6466 case offsetof(struct __sk_buff, queue_mapping):
6467 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
6468 bpf_target_off(struct sk_buff, queue_mapping, 2,
6472 case offsetof(struct __sk_buff, vlan_present):
6474 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->src_reg,
6475 PKT_VLAN_PRESENT_OFFSET());
6476 if (PKT_VLAN_PRESENT_BIT)
6477 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, PKT_VLAN_PRESENT_BIT);
6478 if (PKT_VLAN_PRESENT_BIT < 7)
6479 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, 1);
6482 case offsetof(struct __sk_buff, vlan_tci):
6483 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
6484 bpf_target_off(struct sk_buff, vlan_tci, 2,
6488 case offsetof(struct __sk_buff, cb[0]) ...
6489 offsetofend(struct __sk_buff, cb[4]) - 1:
6490 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, data) < 20);
6491 BUILD_BUG_ON((offsetof(struct sk_buff, cb) +
6492 offsetof(struct qdisc_skb_cb, data)) %
6495 prog->cb_access = 1;
6497 off -= offsetof(struct __sk_buff, cb[0]);
6498 off += offsetof(struct sk_buff, cb);
6499 off += offsetof(struct qdisc_skb_cb, data);
6500 if (type == BPF_WRITE)
6501 *insn++ = BPF_STX_MEM(BPF_SIZE(si->code), si->dst_reg,
6504 *insn++ = BPF_LDX_MEM(BPF_SIZE(si->code), si->dst_reg,
6508 case offsetof(struct __sk_buff, tc_classid):
6509 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, tc_classid) != 2);
6512 off -= offsetof(struct __sk_buff, tc_classid);
6513 off += offsetof(struct sk_buff, cb);
6514 off += offsetof(struct qdisc_skb_cb, tc_classid);
6516 if (type == BPF_WRITE)
6517 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg,
6520 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg,
6524 case offsetof(struct __sk_buff, data):
6525 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
6526 si->dst_reg, si->src_reg,
6527 offsetof(struct sk_buff, data));
6530 case offsetof(struct __sk_buff, data_meta):
6532 off -= offsetof(struct __sk_buff, data_meta);
6533 off += offsetof(struct sk_buff, cb);
6534 off += offsetof(struct bpf_skb_data_end, data_meta);
6535 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
6539 case offsetof(struct __sk_buff, data_end):
6541 off -= offsetof(struct __sk_buff, data_end);
6542 off += offsetof(struct sk_buff, cb);
6543 off += offsetof(struct bpf_skb_data_end, data_end);
6544 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
6548 case offsetof(struct __sk_buff, tc_index):
6549 #ifdef CONFIG_NET_SCHED
6550 if (type == BPF_WRITE)
6551 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg, si->src_reg,
6552 bpf_target_off(struct sk_buff, tc_index, 2,
6555 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
6556 bpf_target_off(struct sk_buff, tc_index, 2,
6560 if (type == BPF_WRITE)
6561 *insn++ = BPF_MOV64_REG(si->dst_reg, si->dst_reg);
6563 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
6567 case offsetof(struct __sk_buff, napi_id):
6568 #if defined(CONFIG_NET_RX_BUSY_POLL)
6569 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6570 bpf_target_off(struct sk_buff, napi_id, 4,
6572 *insn++ = BPF_JMP_IMM(BPF_JGE, si->dst_reg, MIN_NAPI_ID, 1);
6573 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
6576 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
6579 case offsetof(struct __sk_buff, family):
6580 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_family) != 2);
6582 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
6583 si->dst_reg, si->src_reg,
6584 offsetof(struct sk_buff, sk));
6585 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
6586 bpf_target_off(struct sock_common,
6590 case offsetof(struct __sk_buff, remote_ip4):
6591 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_daddr) != 4);
6593 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
6594 si->dst_reg, si->src_reg,
6595 offsetof(struct sk_buff, sk));
6596 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6597 bpf_target_off(struct sock_common,
6601 case offsetof(struct __sk_buff, local_ip4):
6602 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
6603 skc_rcv_saddr) != 4);
6605 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
6606 si->dst_reg, si->src_reg,
6607 offsetof(struct sk_buff, sk));
6608 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6609 bpf_target_off(struct sock_common,
6613 case offsetof(struct __sk_buff, remote_ip6[0]) ...
6614 offsetof(struct __sk_buff, remote_ip6[3]):
6615 #if IS_ENABLED(CONFIG_IPV6)
6616 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
6617 skc_v6_daddr.s6_addr32[0]) != 4);
6620 off -= offsetof(struct __sk_buff, remote_ip6[0]);
6622 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
6623 si->dst_reg, si->src_reg,
6624 offsetof(struct sk_buff, sk));
6625 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6626 offsetof(struct sock_common,
6627 skc_v6_daddr.s6_addr32[0]) +
6630 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
6633 case offsetof(struct __sk_buff, local_ip6[0]) ...
6634 offsetof(struct __sk_buff, local_ip6[3]):
6635 #if IS_ENABLED(CONFIG_IPV6)
6636 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
6637 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
6640 off -= offsetof(struct __sk_buff, local_ip6[0]);
6642 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
6643 si->dst_reg, si->src_reg,
6644 offsetof(struct sk_buff, sk));
6645 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6646 offsetof(struct sock_common,
6647 skc_v6_rcv_saddr.s6_addr32[0]) +
6650 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
6654 case offsetof(struct __sk_buff, remote_port):
6655 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_dport) != 2);
6657 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
6658 si->dst_reg, si->src_reg,
6659 offsetof(struct sk_buff, sk));
6660 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
6661 bpf_target_off(struct sock_common,
6664 #ifndef __BIG_ENDIAN_BITFIELD
6665 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
6669 case offsetof(struct __sk_buff, local_port):
6670 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_num) != 2);
6672 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
6673 si->dst_reg, si->src_reg,
6674 offsetof(struct sk_buff, sk));
6675 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
6676 bpf_target_off(struct sock_common,
6677 skc_num, 2, target_size));
6680 case offsetof(struct __sk_buff, flow_keys):
6682 off -= offsetof(struct __sk_buff, flow_keys);
6683 off += offsetof(struct sk_buff, cb);
6684 off += offsetof(struct qdisc_skb_cb, flow_keys);
6685 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
6689 case offsetof(struct __sk_buff, tstamp):
6690 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, tstamp) != 8);
6692 if (type == BPF_WRITE)
6693 *insn++ = BPF_STX_MEM(BPF_DW,
6694 si->dst_reg, si->src_reg,
6695 bpf_target_off(struct sk_buff,
6699 *insn++ = BPF_LDX_MEM(BPF_DW,
6700 si->dst_reg, si->src_reg,
6701 bpf_target_off(struct sk_buff,
6706 case offsetof(struct __sk_buff, wire_len):
6707 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, pkt_len) != 4);
6710 off -= offsetof(struct __sk_buff, wire_len);
6711 off += offsetof(struct sk_buff, cb);
6712 off += offsetof(struct qdisc_skb_cb, pkt_len);
6714 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg, off);
6717 return insn - insn_buf;
6720 u32 bpf_sock_convert_ctx_access(enum bpf_access_type type,
6721 const struct bpf_insn *si,
6722 struct bpf_insn *insn_buf,
6723 struct bpf_prog *prog, u32 *target_size)
6725 struct bpf_insn *insn = insn_buf;
6729 case offsetof(struct bpf_sock, bound_dev_if):
6730 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_bound_dev_if) != 4);
6732 if (type == BPF_WRITE)
6733 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
6734 offsetof(struct sock, sk_bound_dev_if));
6736 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6737 offsetof(struct sock, sk_bound_dev_if));
6740 case offsetof(struct bpf_sock, mark):
6741 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_mark) != 4);
6743 if (type == BPF_WRITE)
6744 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
6745 offsetof(struct sock, sk_mark));
6747 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6748 offsetof(struct sock, sk_mark));
6751 case offsetof(struct bpf_sock, priority):
6752 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_priority) != 4);
6754 if (type == BPF_WRITE)
6755 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
6756 offsetof(struct sock, sk_priority));
6758 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6759 offsetof(struct sock, sk_priority));
6762 case offsetof(struct bpf_sock, family):
6763 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_family) != 2);
6765 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
6766 offsetof(struct sock, sk_family));
6769 case offsetof(struct bpf_sock, type):
6770 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6771 offsetof(struct sock, __sk_flags_offset));
6772 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_TYPE_MASK);
6773 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, SK_FL_TYPE_SHIFT);
6776 case offsetof(struct bpf_sock, protocol):
6777 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6778 offsetof(struct sock, __sk_flags_offset));
6779 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_PROTO_MASK);
6780 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, SK_FL_PROTO_SHIFT);
6783 case offsetof(struct bpf_sock, src_ip4):
6784 *insn++ = BPF_LDX_MEM(
6785 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
6786 bpf_target_off(struct sock_common, skc_rcv_saddr,
6787 FIELD_SIZEOF(struct sock_common,
6792 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
6793 #if IS_ENABLED(CONFIG_IPV6)
6795 off -= offsetof(struct bpf_sock, src_ip6[0]);
6796 *insn++ = BPF_LDX_MEM(
6797 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
6800 skc_v6_rcv_saddr.s6_addr32[0],
6801 FIELD_SIZEOF(struct sock_common,
6802 skc_v6_rcv_saddr.s6_addr32[0]),
6803 target_size) + off);
6806 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
6810 case offsetof(struct bpf_sock, src_port):
6811 *insn++ = BPF_LDX_MEM(
6812 BPF_FIELD_SIZEOF(struct sock_common, skc_num),
6813 si->dst_reg, si->src_reg,
6814 bpf_target_off(struct sock_common, skc_num,
6815 FIELD_SIZEOF(struct sock_common,
6821 return insn - insn_buf;
6824 static u32 tc_cls_act_convert_ctx_access(enum bpf_access_type type,
6825 const struct bpf_insn *si,
6826 struct bpf_insn *insn_buf,
6827 struct bpf_prog *prog, u32 *target_size)
6829 struct bpf_insn *insn = insn_buf;
6832 case offsetof(struct __sk_buff, ifindex):
6833 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
6834 si->dst_reg, si->src_reg,
6835 offsetof(struct sk_buff, dev));
6836 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6837 bpf_target_off(struct net_device, ifindex, 4,
6841 return bpf_convert_ctx_access(type, si, insn_buf, prog,
6845 return insn - insn_buf;
6848 static u32 xdp_convert_ctx_access(enum bpf_access_type type,
6849 const struct bpf_insn *si,
6850 struct bpf_insn *insn_buf,
6851 struct bpf_prog *prog, u32 *target_size)
6853 struct bpf_insn *insn = insn_buf;
6856 case offsetof(struct xdp_md, data):
6857 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data),
6858 si->dst_reg, si->src_reg,
6859 offsetof(struct xdp_buff, data));
6861 case offsetof(struct xdp_md, data_meta):
6862 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_meta),
6863 si->dst_reg, si->src_reg,
6864 offsetof(struct xdp_buff, data_meta));
6866 case offsetof(struct xdp_md, data_end):
6867 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_end),
6868 si->dst_reg, si->src_reg,
6869 offsetof(struct xdp_buff, data_end));
6871 case offsetof(struct xdp_md, ingress_ifindex):
6872 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, rxq),
6873 si->dst_reg, si->src_reg,
6874 offsetof(struct xdp_buff, rxq));
6875 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_rxq_info, dev),
6876 si->dst_reg, si->dst_reg,
6877 offsetof(struct xdp_rxq_info, dev));
6878 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6879 offsetof(struct net_device, ifindex));
6881 case offsetof(struct xdp_md, rx_queue_index):
6882 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, rxq),
6883 si->dst_reg, si->src_reg,
6884 offsetof(struct xdp_buff, rxq));
6885 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6886 offsetof(struct xdp_rxq_info,
6891 return insn - insn_buf;
6894 /* SOCK_ADDR_LOAD_NESTED_FIELD() loads Nested Field S.F.NF where S is type of
6895 * context Structure, F is Field in context structure that contains a pointer
6896 * to Nested Structure of type NS that has the field NF.
6898 * SIZE encodes the load size (BPF_B, BPF_H, etc). It's up to caller to make
6899 * sure that SIZE is not greater than actual size of S.F.NF.
6901 * If offset OFF is provided, the load happens from that offset relative to
6904 #define SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, SIZE, OFF) \
6906 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), si->dst_reg, \
6907 si->src_reg, offsetof(S, F)); \
6908 *insn++ = BPF_LDX_MEM( \
6909 SIZE, si->dst_reg, si->dst_reg, \
6910 bpf_target_off(NS, NF, FIELD_SIZEOF(NS, NF), \
6915 #define SOCK_ADDR_LOAD_NESTED_FIELD(S, NS, F, NF) \
6916 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, \
6917 BPF_FIELD_SIZEOF(NS, NF), 0)
6919 /* SOCK_ADDR_STORE_NESTED_FIELD_OFF() has semantic similar to
6920 * SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF() but for store operation.
6922 * It doesn't support SIZE argument though since narrow stores are not
6923 * supported for now.
6925 * In addition it uses Temporary Field TF (member of struct S) as the 3rd
6926 * "register" since two registers available in convert_ctx_access are not
6927 * enough: we can't override neither SRC, since it contains value to store, nor
6928 * DST since it contains pointer to context that may be used by later
6929 * instructions. But we need a temporary place to save pointer to nested
6930 * structure whose field we want to store to.
6932 #define SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, OFF, TF) \
6934 int tmp_reg = BPF_REG_9; \
6935 if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg) \
6937 if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg) \
6939 *insn++ = BPF_STX_MEM(BPF_DW, si->dst_reg, tmp_reg, \
6941 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), tmp_reg, \
6942 si->dst_reg, offsetof(S, F)); \
6943 *insn++ = BPF_STX_MEM( \
6944 BPF_FIELD_SIZEOF(NS, NF), tmp_reg, si->src_reg, \
6945 bpf_target_off(NS, NF, FIELD_SIZEOF(NS, NF), \
6948 *insn++ = BPF_LDX_MEM(BPF_DW, tmp_reg, si->dst_reg, \
6952 #define SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, SIZE, OFF, \
6955 if (type == BPF_WRITE) { \
6956 SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, OFF, \
6959 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF( \
6960 S, NS, F, NF, SIZE, OFF); \
6964 #define SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD(S, NS, F, NF, TF) \
6965 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF( \
6966 S, NS, F, NF, BPF_FIELD_SIZEOF(NS, NF), 0, TF)
6968 static u32 sock_addr_convert_ctx_access(enum bpf_access_type type,
6969 const struct bpf_insn *si,
6970 struct bpf_insn *insn_buf,
6971 struct bpf_prog *prog, u32 *target_size)
6973 struct bpf_insn *insn = insn_buf;
6977 case offsetof(struct bpf_sock_addr, user_family):
6978 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
6979 struct sockaddr, uaddr, sa_family);
6982 case offsetof(struct bpf_sock_addr, user_ip4):
6983 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
6984 struct bpf_sock_addr_kern, struct sockaddr_in, uaddr,
6985 sin_addr, BPF_SIZE(si->code), 0, tmp_reg);
6988 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
6990 off -= offsetof(struct bpf_sock_addr, user_ip6[0]);
6991 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
6992 struct bpf_sock_addr_kern, struct sockaddr_in6, uaddr,
6993 sin6_addr.s6_addr32[0], BPF_SIZE(si->code), off,
6997 case offsetof(struct bpf_sock_addr, user_port):
6998 /* To get port we need to know sa_family first and then treat
6999 * sockaddr as either sockaddr_in or sockaddr_in6.
7000 * Though we can simplify since port field has same offset and
7001 * size in both structures.
7002 * Here we check this invariant and use just one of the
7003 * structures if it's true.
7005 BUILD_BUG_ON(offsetof(struct sockaddr_in, sin_port) !=
7006 offsetof(struct sockaddr_in6, sin6_port));
7007 BUILD_BUG_ON(FIELD_SIZEOF(struct sockaddr_in, sin_port) !=
7008 FIELD_SIZEOF(struct sockaddr_in6, sin6_port));
7009 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD(struct bpf_sock_addr_kern,
7010 struct sockaddr_in6, uaddr,
7011 sin6_port, tmp_reg);
7014 case offsetof(struct bpf_sock_addr, family):
7015 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
7016 struct sock, sk, sk_family);
7019 case offsetof(struct bpf_sock_addr, type):
7020 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(
7021 struct bpf_sock_addr_kern, struct sock, sk,
7022 __sk_flags_offset, BPF_W, 0);
7023 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_TYPE_MASK);
7024 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, SK_FL_TYPE_SHIFT);
7027 case offsetof(struct bpf_sock_addr, protocol):
7028 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(
7029 struct bpf_sock_addr_kern, struct sock, sk,
7030 __sk_flags_offset, BPF_W, 0);
7031 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_PROTO_MASK);
7032 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg,
7036 case offsetof(struct bpf_sock_addr, msg_src_ip4):
7037 /* Treat t_ctx as struct in_addr for msg_src_ip4. */
7038 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
7039 struct bpf_sock_addr_kern, struct in_addr, t_ctx,
7040 s_addr, BPF_SIZE(si->code), 0, tmp_reg);
7043 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
7046 off -= offsetof(struct bpf_sock_addr, msg_src_ip6[0]);
7047 /* Treat t_ctx as struct in6_addr for msg_src_ip6. */
7048 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
7049 struct bpf_sock_addr_kern, struct in6_addr, t_ctx,
7050 s6_addr32[0], BPF_SIZE(si->code), off, tmp_reg);
7054 return insn - insn_buf;
7057 static u32 sock_ops_convert_ctx_access(enum bpf_access_type type,
7058 const struct bpf_insn *si,
7059 struct bpf_insn *insn_buf,
7060 struct bpf_prog *prog,
7063 struct bpf_insn *insn = insn_buf;
7067 case offsetof(struct bpf_sock_ops, op) ...
7068 offsetof(struct bpf_sock_ops, replylong[3]):
7069 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, op) !=
7070 FIELD_SIZEOF(struct bpf_sock_ops_kern, op));
7071 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, reply) !=
7072 FIELD_SIZEOF(struct bpf_sock_ops_kern, reply));
7073 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, replylong) !=
7074 FIELD_SIZEOF(struct bpf_sock_ops_kern, replylong));
7076 off -= offsetof(struct bpf_sock_ops, op);
7077 off += offsetof(struct bpf_sock_ops_kern, op);
7078 if (type == BPF_WRITE)
7079 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
7082 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
7086 case offsetof(struct bpf_sock_ops, family):
7087 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_family) != 2);
7089 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7090 struct bpf_sock_ops_kern, sk),
7091 si->dst_reg, si->src_reg,
7092 offsetof(struct bpf_sock_ops_kern, sk));
7093 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
7094 offsetof(struct sock_common, skc_family));
7097 case offsetof(struct bpf_sock_ops, remote_ip4):
7098 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_daddr) != 4);
7100 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7101 struct bpf_sock_ops_kern, sk),
7102 si->dst_reg, si->src_reg,
7103 offsetof(struct bpf_sock_ops_kern, sk));
7104 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7105 offsetof(struct sock_common, skc_daddr));
7108 case offsetof(struct bpf_sock_ops, local_ip4):
7109 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
7110 skc_rcv_saddr) != 4);
7112 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7113 struct bpf_sock_ops_kern, sk),
7114 si->dst_reg, si->src_reg,
7115 offsetof(struct bpf_sock_ops_kern, sk));
7116 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7117 offsetof(struct sock_common,
7121 case offsetof(struct bpf_sock_ops, remote_ip6[0]) ...
7122 offsetof(struct bpf_sock_ops, remote_ip6[3]):
7123 #if IS_ENABLED(CONFIG_IPV6)
7124 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
7125 skc_v6_daddr.s6_addr32[0]) != 4);
7128 off -= offsetof(struct bpf_sock_ops, remote_ip6[0]);
7129 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7130 struct bpf_sock_ops_kern, sk),
7131 si->dst_reg, si->src_reg,
7132 offsetof(struct bpf_sock_ops_kern, sk));
7133 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7134 offsetof(struct sock_common,
7135 skc_v6_daddr.s6_addr32[0]) +
7138 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
7142 case offsetof(struct bpf_sock_ops, local_ip6[0]) ...
7143 offsetof(struct bpf_sock_ops, local_ip6[3]):
7144 #if IS_ENABLED(CONFIG_IPV6)
7145 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
7146 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
7149 off -= offsetof(struct bpf_sock_ops, local_ip6[0]);
7150 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7151 struct bpf_sock_ops_kern, sk),
7152 si->dst_reg, si->src_reg,
7153 offsetof(struct bpf_sock_ops_kern, sk));
7154 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7155 offsetof(struct sock_common,
7156 skc_v6_rcv_saddr.s6_addr32[0]) +
7159 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
7163 case offsetof(struct bpf_sock_ops, remote_port):
7164 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_dport) != 2);
7166 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7167 struct bpf_sock_ops_kern, sk),
7168 si->dst_reg, si->src_reg,
7169 offsetof(struct bpf_sock_ops_kern, sk));
7170 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
7171 offsetof(struct sock_common, skc_dport));
7172 #ifndef __BIG_ENDIAN_BITFIELD
7173 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
7177 case offsetof(struct bpf_sock_ops, local_port):
7178 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_num) != 2);
7180 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7181 struct bpf_sock_ops_kern, sk),
7182 si->dst_reg, si->src_reg,
7183 offsetof(struct bpf_sock_ops_kern, sk));
7184 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
7185 offsetof(struct sock_common, skc_num));
7188 case offsetof(struct bpf_sock_ops, is_fullsock):
7189 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7190 struct bpf_sock_ops_kern,
7192 si->dst_reg, si->src_reg,
7193 offsetof(struct bpf_sock_ops_kern,
7197 case offsetof(struct bpf_sock_ops, state):
7198 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_state) != 1);
7200 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7201 struct bpf_sock_ops_kern, sk),
7202 si->dst_reg, si->src_reg,
7203 offsetof(struct bpf_sock_ops_kern, sk));
7204 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->dst_reg,
7205 offsetof(struct sock_common, skc_state));
7208 case offsetof(struct bpf_sock_ops, rtt_min):
7209 BUILD_BUG_ON(FIELD_SIZEOF(struct tcp_sock, rtt_min) !=
7210 sizeof(struct minmax));
7211 BUILD_BUG_ON(sizeof(struct minmax) <
7212 sizeof(struct minmax_sample));
7214 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7215 struct bpf_sock_ops_kern, sk),
7216 si->dst_reg, si->src_reg,
7217 offsetof(struct bpf_sock_ops_kern, sk));
7218 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7219 offsetof(struct tcp_sock, rtt_min) +
7220 FIELD_SIZEOF(struct minmax_sample, t));
7223 /* Helper macro for adding read access to tcp_sock or sock fields. */
7224 #define SOCK_OPS_GET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ) \
7226 BUILD_BUG_ON(FIELD_SIZEOF(OBJ, OBJ_FIELD) > \
7227 FIELD_SIZEOF(struct bpf_sock_ops, BPF_FIELD)); \
7228 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
7229 struct bpf_sock_ops_kern, \
7231 si->dst_reg, si->src_reg, \
7232 offsetof(struct bpf_sock_ops_kern, \
7234 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 2); \
7235 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
7236 struct bpf_sock_ops_kern, sk),\
7237 si->dst_reg, si->src_reg, \
7238 offsetof(struct bpf_sock_ops_kern, sk));\
7239 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(OBJ, \
7241 si->dst_reg, si->dst_reg, \
7242 offsetof(OBJ, OBJ_FIELD)); \
7245 /* Helper macro for adding write access to tcp_sock or sock fields.
7246 * The macro is called with two registers, dst_reg which contains a pointer
7247 * to ctx (context) and src_reg which contains the value that should be
7248 * stored. However, we need an additional register since we cannot overwrite
7249 * dst_reg because it may be used later in the program.
7250 * Instead we "borrow" one of the other register. We first save its value
7251 * into a new (temp) field in bpf_sock_ops_kern, use it, and then restore
7252 * it at the end of the macro.
7254 #define SOCK_OPS_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ) \
7256 int reg = BPF_REG_9; \
7257 BUILD_BUG_ON(FIELD_SIZEOF(OBJ, OBJ_FIELD) > \
7258 FIELD_SIZEOF(struct bpf_sock_ops, BPF_FIELD)); \
7259 if (si->dst_reg == reg || si->src_reg == reg) \
7261 if (si->dst_reg == reg || si->src_reg == reg) \
7263 *insn++ = BPF_STX_MEM(BPF_DW, si->dst_reg, reg, \
7264 offsetof(struct bpf_sock_ops_kern, \
7266 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
7267 struct bpf_sock_ops_kern, \
7270 offsetof(struct bpf_sock_ops_kern, \
7272 *insn++ = BPF_JMP_IMM(BPF_JEQ, reg, 0, 2); \
7273 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
7274 struct bpf_sock_ops_kern, sk),\
7276 offsetof(struct bpf_sock_ops_kern, sk));\
7277 *insn++ = BPF_STX_MEM(BPF_FIELD_SIZEOF(OBJ, OBJ_FIELD), \
7279 offsetof(OBJ, OBJ_FIELD)); \
7280 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->dst_reg, \
7281 offsetof(struct bpf_sock_ops_kern, \
7285 #define SOCK_OPS_GET_OR_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ, TYPE) \
7287 if (TYPE == BPF_WRITE) \
7288 SOCK_OPS_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ); \
7290 SOCK_OPS_GET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ); \
7293 case offsetof(struct bpf_sock_ops, snd_cwnd):
7294 SOCK_OPS_GET_FIELD(snd_cwnd, snd_cwnd, struct tcp_sock);
7297 case offsetof(struct bpf_sock_ops, srtt_us):
7298 SOCK_OPS_GET_FIELD(srtt_us, srtt_us, struct tcp_sock);
7301 case offsetof(struct bpf_sock_ops, bpf_sock_ops_cb_flags):
7302 SOCK_OPS_GET_FIELD(bpf_sock_ops_cb_flags, bpf_sock_ops_cb_flags,
7306 case offsetof(struct bpf_sock_ops, snd_ssthresh):
7307 SOCK_OPS_GET_FIELD(snd_ssthresh, snd_ssthresh, struct tcp_sock);
7310 case offsetof(struct bpf_sock_ops, rcv_nxt):
7311 SOCK_OPS_GET_FIELD(rcv_nxt, rcv_nxt, struct tcp_sock);
7314 case offsetof(struct bpf_sock_ops, snd_nxt):
7315 SOCK_OPS_GET_FIELD(snd_nxt, snd_nxt, struct tcp_sock);
7318 case offsetof(struct bpf_sock_ops, snd_una):
7319 SOCK_OPS_GET_FIELD(snd_una, snd_una, struct tcp_sock);
7322 case offsetof(struct bpf_sock_ops, mss_cache):
7323 SOCK_OPS_GET_FIELD(mss_cache, mss_cache, struct tcp_sock);
7326 case offsetof(struct bpf_sock_ops, ecn_flags):
7327 SOCK_OPS_GET_FIELD(ecn_flags, ecn_flags, struct tcp_sock);
7330 case offsetof(struct bpf_sock_ops, rate_delivered):
7331 SOCK_OPS_GET_FIELD(rate_delivered, rate_delivered,
7335 case offsetof(struct bpf_sock_ops, rate_interval_us):
7336 SOCK_OPS_GET_FIELD(rate_interval_us, rate_interval_us,
7340 case offsetof(struct bpf_sock_ops, packets_out):
7341 SOCK_OPS_GET_FIELD(packets_out, packets_out, struct tcp_sock);
7344 case offsetof(struct bpf_sock_ops, retrans_out):
7345 SOCK_OPS_GET_FIELD(retrans_out, retrans_out, struct tcp_sock);
7348 case offsetof(struct bpf_sock_ops, total_retrans):
7349 SOCK_OPS_GET_FIELD(total_retrans, total_retrans,
7353 case offsetof(struct bpf_sock_ops, segs_in):
7354 SOCK_OPS_GET_FIELD(segs_in, segs_in, struct tcp_sock);
7357 case offsetof(struct bpf_sock_ops, data_segs_in):
7358 SOCK_OPS_GET_FIELD(data_segs_in, data_segs_in, struct tcp_sock);
7361 case offsetof(struct bpf_sock_ops, segs_out):
7362 SOCK_OPS_GET_FIELD(segs_out, segs_out, struct tcp_sock);
7365 case offsetof(struct bpf_sock_ops, data_segs_out):
7366 SOCK_OPS_GET_FIELD(data_segs_out, data_segs_out,
7370 case offsetof(struct bpf_sock_ops, lost_out):
7371 SOCK_OPS_GET_FIELD(lost_out, lost_out, struct tcp_sock);
7374 case offsetof(struct bpf_sock_ops, sacked_out):
7375 SOCK_OPS_GET_FIELD(sacked_out, sacked_out, struct tcp_sock);
7378 case offsetof(struct bpf_sock_ops, sk_txhash):
7379 SOCK_OPS_GET_OR_SET_FIELD(sk_txhash, sk_txhash,
7383 case offsetof(struct bpf_sock_ops, bytes_received):
7384 SOCK_OPS_GET_FIELD(bytes_received, bytes_received,
7388 case offsetof(struct bpf_sock_ops, bytes_acked):
7389 SOCK_OPS_GET_FIELD(bytes_acked, bytes_acked, struct tcp_sock);
7393 return insn - insn_buf;
7396 static u32 sk_skb_convert_ctx_access(enum bpf_access_type type,
7397 const struct bpf_insn *si,
7398 struct bpf_insn *insn_buf,
7399 struct bpf_prog *prog, u32 *target_size)
7401 struct bpf_insn *insn = insn_buf;
7405 case offsetof(struct __sk_buff, data_end):
7407 off -= offsetof(struct __sk_buff, data_end);
7408 off += offsetof(struct sk_buff, cb);
7409 off += offsetof(struct tcp_skb_cb, bpf.data_end);
7410 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
7414 return bpf_convert_ctx_access(type, si, insn_buf, prog,
7418 return insn - insn_buf;
7421 static u32 sk_msg_convert_ctx_access(enum bpf_access_type type,
7422 const struct bpf_insn *si,
7423 struct bpf_insn *insn_buf,
7424 struct bpf_prog *prog, u32 *target_size)
7426 struct bpf_insn *insn = insn_buf;
7427 #if IS_ENABLED(CONFIG_IPV6)
7431 /* convert ctx uses the fact sg element is first in struct */
7432 BUILD_BUG_ON(offsetof(struct sk_msg, sg) != 0);
7435 case offsetof(struct sk_msg_md, data):
7436 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg, data),
7437 si->dst_reg, si->src_reg,
7438 offsetof(struct sk_msg, data));
7440 case offsetof(struct sk_msg_md, data_end):
7441 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg, data_end),
7442 si->dst_reg, si->src_reg,
7443 offsetof(struct sk_msg, data_end));
7445 case offsetof(struct sk_msg_md, family):
7446 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_family) != 2);
7448 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7450 si->dst_reg, si->src_reg,
7451 offsetof(struct sk_msg, sk));
7452 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
7453 offsetof(struct sock_common, skc_family));
7456 case offsetof(struct sk_msg_md, remote_ip4):
7457 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_daddr) != 4);
7459 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7461 si->dst_reg, si->src_reg,
7462 offsetof(struct sk_msg, sk));
7463 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7464 offsetof(struct sock_common, skc_daddr));
7467 case offsetof(struct sk_msg_md, local_ip4):
7468 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
7469 skc_rcv_saddr) != 4);
7471 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7473 si->dst_reg, si->src_reg,
7474 offsetof(struct sk_msg, sk));
7475 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7476 offsetof(struct sock_common,
7480 case offsetof(struct sk_msg_md, remote_ip6[0]) ...
7481 offsetof(struct sk_msg_md, remote_ip6[3]):
7482 #if IS_ENABLED(CONFIG_IPV6)
7483 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
7484 skc_v6_daddr.s6_addr32[0]) != 4);
7487 off -= offsetof(struct sk_msg_md, remote_ip6[0]);
7488 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7490 si->dst_reg, si->src_reg,
7491 offsetof(struct sk_msg, sk));
7492 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7493 offsetof(struct sock_common,
7494 skc_v6_daddr.s6_addr32[0]) +
7497 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
7501 case offsetof(struct sk_msg_md, local_ip6[0]) ...
7502 offsetof(struct sk_msg_md, local_ip6[3]):
7503 #if IS_ENABLED(CONFIG_IPV6)
7504 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
7505 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
7508 off -= offsetof(struct sk_msg_md, local_ip6[0]);
7509 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7511 si->dst_reg, si->src_reg,
7512 offsetof(struct sk_msg, sk));
7513 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7514 offsetof(struct sock_common,
7515 skc_v6_rcv_saddr.s6_addr32[0]) +
7518 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
7522 case offsetof(struct sk_msg_md, remote_port):
7523 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_dport) != 2);
7525 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7527 si->dst_reg, si->src_reg,
7528 offsetof(struct sk_msg, sk));
7529 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
7530 offsetof(struct sock_common, skc_dport));
7531 #ifndef __BIG_ENDIAN_BITFIELD
7532 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
7536 case offsetof(struct sk_msg_md, local_port):
7537 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_num) != 2);
7539 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7541 si->dst_reg, si->src_reg,
7542 offsetof(struct sk_msg, sk));
7543 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
7544 offsetof(struct sock_common, skc_num));
7547 case offsetof(struct sk_msg_md, size):
7548 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg_sg, size),
7549 si->dst_reg, si->src_reg,
7550 offsetof(struct sk_msg_sg, size));
7554 return insn - insn_buf;
7557 const struct bpf_verifier_ops sk_filter_verifier_ops = {
7558 .get_func_proto = sk_filter_func_proto,
7559 .is_valid_access = sk_filter_is_valid_access,
7560 .convert_ctx_access = bpf_convert_ctx_access,
7561 .gen_ld_abs = bpf_gen_ld_abs,
7564 const struct bpf_prog_ops sk_filter_prog_ops = {
7565 .test_run = bpf_prog_test_run_skb,
7568 const struct bpf_verifier_ops tc_cls_act_verifier_ops = {
7569 .get_func_proto = tc_cls_act_func_proto,
7570 .is_valid_access = tc_cls_act_is_valid_access,
7571 .convert_ctx_access = tc_cls_act_convert_ctx_access,
7572 .gen_prologue = tc_cls_act_prologue,
7573 .gen_ld_abs = bpf_gen_ld_abs,
7576 const struct bpf_prog_ops tc_cls_act_prog_ops = {
7577 .test_run = bpf_prog_test_run_skb,
7580 const struct bpf_verifier_ops xdp_verifier_ops = {
7581 .get_func_proto = xdp_func_proto,
7582 .is_valid_access = xdp_is_valid_access,
7583 .convert_ctx_access = xdp_convert_ctx_access,
7584 .gen_prologue = bpf_noop_prologue,
7587 const struct bpf_prog_ops xdp_prog_ops = {
7588 .test_run = bpf_prog_test_run_xdp,
7591 const struct bpf_verifier_ops cg_skb_verifier_ops = {
7592 .get_func_proto = cg_skb_func_proto,
7593 .is_valid_access = cg_skb_is_valid_access,
7594 .convert_ctx_access = bpf_convert_ctx_access,
7597 const struct bpf_prog_ops cg_skb_prog_ops = {
7598 .test_run = bpf_prog_test_run_skb,
7601 const struct bpf_verifier_ops lwt_in_verifier_ops = {
7602 .get_func_proto = lwt_in_func_proto,
7603 .is_valid_access = lwt_is_valid_access,
7604 .convert_ctx_access = bpf_convert_ctx_access,
7607 const struct bpf_prog_ops lwt_in_prog_ops = {
7608 .test_run = bpf_prog_test_run_skb,
7611 const struct bpf_verifier_ops lwt_out_verifier_ops = {
7612 .get_func_proto = lwt_out_func_proto,
7613 .is_valid_access = lwt_is_valid_access,
7614 .convert_ctx_access = bpf_convert_ctx_access,
7617 const struct bpf_prog_ops lwt_out_prog_ops = {
7618 .test_run = bpf_prog_test_run_skb,
7621 const struct bpf_verifier_ops lwt_xmit_verifier_ops = {
7622 .get_func_proto = lwt_xmit_func_proto,
7623 .is_valid_access = lwt_is_valid_access,
7624 .convert_ctx_access = bpf_convert_ctx_access,
7625 .gen_prologue = tc_cls_act_prologue,
7628 const struct bpf_prog_ops lwt_xmit_prog_ops = {
7629 .test_run = bpf_prog_test_run_skb,
7632 const struct bpf_verifier_ops lwt_seg6local_verifier_ops = {
7633 .get_func_proto = lwt_seg6local_func_proto,
7634 .is_valid_access = lwt_is_valid_access,
7635 .convert_ctx_access = bpf_convert_ctx_access,
7638 const struct bpf_prog_ops lwt_seg6local_prog_ops = {
7639 .test_run = bpf_prog_test_run_skb,
7642 const struct bpf_verifier_ops cg_sock_verifier_ops = {
7643 .get_func_proto = sock_filter_func_proto,
7644 .is_valid_access = sock_filter_is_valid_access,
7645 .convert_ctx_access = bpf_sock_convert_ctx_access,
7648 const struct bpf_prog_ops cg_sock_prog_ops = {
7651 const struct bpf_verifier_ops cg_sock_addr_verifier_ops = {
7652 .get_func_proto = sock_addr_func_proto,
7653 .is_valid_access = sock_addr_is_valid_access,
7654 .convert_ctx_access = sock_addr_convert_ctx_access,
7657 const struct bpf_prog_ops cg_sock_addr_prog_ops = {
7660 const struct bpf_verifier_ops sock_ops_verifier_ops = {
7661 .get_func_proto = sock_ops_func_proto,
7662 .is_valid_access = sock_ops_is_valid_access,
7663 .convert_ctx_access = sock_ops_convert_ctx_access,
7666 const struct bpf_prog_ops sock_ops_prog_ops = {
7669 const struct bpf_verifier_ops sk_skb_verifier_ops = {
7670 .get_func_proto = sk_skb_func_proto,
7671 .is_valid_access = sk_skb_is_valid_access,
7672 .convert_ctx_access = sk_skb_convert_ctx_access,
7673 .gen_prologue = sk_skb_prologue,
7676 const struct bpf_prog_ops sk_skb_prog_ops = {
7679 const struct bpf_verifier_ops sk_msg_verifier_ops = {
7680 .get_func_proto = sk_msg_func_proto,
7681 .is_valid_access = sk_msg_is_valid_access,
7682 .convert_ctx_access = sk_msg_convert_ctx_access,
7683 .gen_prologue = bpf_noop_prologue,
7686 const struct bpf_prog_ops sk_msg_prog_ops = {
7689 const struct bpf_verifier_ops flow_dissector_verifier_ops = {
7690 .get_func_proto = flow_dissector_func_proto,
7691 .is_valid_access = flow_dissector_is_valid_access,
7692 .convert_ctx_access = bpf_convert_ctx_access,
7695 const struct bpf_prog_ops flow_dissector_prog_ops = {
7698 int sk_detach_filter(struct sock *sk)
7701 struct sk_filter *filter;
7703 if (sock_flag(sk, SOCK_FILTER_LOCKED))
7706 filter = rcu_dereference_protected(sk->sk_filter,
7707 lockdep_sock_is_held(sk));
7709 RCU_INIT_POINTER(sk->sk_filter, NULL);
7710 sk_filter_uncharge(sk, filter);
7716 EXPORT_SYMBOL_GPL(sk_detach_filter);
7718 int sk_get_filter(struct sock *sk, struct sock_filter __user *ubuf,
7721 struct sock_fprog_kern *fprog;
7722 struct sk_filter *filter;
7726 filter = rcu_dereference_protected(sk->sk_filter,
7727 lockdep_sock_is_held(sk));
7731 /* We're copying the filter that has been originally attached,
7732 * so no conversion/decode needed anymore. eBPF programs that
7733 * have no original program cannot be dumped through this.
7736 fprog = filter->prog->orig_prog;
7742 /* User space only enquires number of filter blocks. */
7746 if (len < fprog->len)
7750 if (copy_to_user(ubuf, fprog->filter, bpf_classic_proglen(fprog)))
7753 /* Instead of bytes, the API requests to return the number
7763 struct sk_reuseport_kern {
7764 struct sk_buff *skb;
7766 struct sock *selected_sk;
7773 static void bpf_init_reuseport_kern(struct sk_reuseport_kern *reuse_kern,
7774 struct sock_reuseport *reuse,
7775 struct sock *sk, struct sk_buff *skb,
7778 reuse_kern->skb = skb;
7779 reuse_kern->sk = sk;
7780 reuse_kern->selected_sk = NULL;
7781 reuse_kern->data_end = skb->data + skb_headlen(skb);
7782 reuse_kern->hash = hash;
7783 reuse_kern->reuseport_id = reuse->reuseport_id;
7784 reuse_kern->bind_inany = reuse->bind_inany;
7787 struct sock *bpf_run_sk_reuseport(struct sock_reuseport *reuse, struct sock *sk,
7788 struct bpf_prog *prog, struct sk_buff *skb,
7791 struct sk_reuseport_kern reuse_kern;
7792 enum sk_action action;
7794 bpf_init_reuseport_kern(&reuse_kern, reuse, sk, skb, hash);
7795 action = BPF_PROG_RUN(prog, &reuse_kern);
7797 if (action == SK_PASS)
7798 return reuse_kern.selected_sk;
7800 return ERR_PTR(-ECONNREFUSED);
7803 BPF_CALL_4(sk_select_reuseport, struct sk_reuseport_kern *, reuse_kern,
7804 struct bpf_map *, map, void *, key, u32, flags)
7806 struct sock_reuseport *reuse;
7807 struct sock *selected_sk;
7809 selected_sk = map->ops->map_lookup_elem(map, key);
7813 reuse = rcu_dereference(selected_sk->sk_reuseport_cb);
7815 /* selected_sk is unhashed (e.g. by close()) after the
7816 * above map_lookup_elem(). Treat selected_sk has already
7817 * been removed from the map.
7821 if (unlikely(reuse->reuseport_id != reuse_kern->reuseport_id)) {
7824 if (unlikely(!reuse_kern->reuseport_id))
7825 /* There is a small race between adding the
7826 * sk to the map and setting the
7827 * reuse_kern->reuseport_id.
7828 * Treat it as the sk has not been added to
7833 sk = reuse_kern->sk;
7834 if (sk->sk_protocol != selected_sk->sk_protocol)
7836 else if (sk->sk_family != selected_sk->sk_family)
7837 return -EAFNOSUPPORT;
7839 /* Catch all. Likely bound to a different sockaddr. */
7843 reuse_kern->selected_sk = selected_sk;
7848 static const struct bpf_func_proto sk_select_reuseport_proto = {
7849 .func = sk_select_reuseport,
7851 .ret_type = RET_INTEGER,
7852 .arg1_type = ARG_PTR_TO_CTX,
7853 .arg2_type = ARG_CONST_MAP_PTR,
7854 .arg3_type = ARG_PTR_TO_MAP_KEY,
7855 .arg4_type = ARG_ANYTHING,
7858 BPF_CALL_4(sk_reuseport_load_bytes,
7859 const struct sk_reuseport_kern *, reuse_kern, u32, offset,
7860 void *, to, u32, len)
7862 return ____bpf_skb_load_bytes(reuse_kern->skb, offset, to, len);
7865 static const struct bpf_func_proto sk_reuseport_load_bytes_proto = {
7866 .func = sk_reuseport_load_bytes,
7868 .ret_type = RET_INTEGER,
7869 .arg1_type = ARG_PTR_TO_CTX,
7870 .arg2_type = ARG_ANYTHING,
7871 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
7872 .arg4_type = ARG_CONST_SIZE,
7875 BPF_CALL_5(sk_reuseport_load_bytes_relative,
7876 const struct sk_reuseport_kern *, reuse_kern, u32, offset,
7877 void *, to, u32, len, u32, start_header)
7879 return ____bpf_skb_load_bytes_relative(reuse_kern->skb, offset, to,
7883 static const struct bpf_func_proto sk_reuseport_load_bytes_relative_proto = {
7884 .func = sk_reuseport_load_bytes_relative,
7886 .ret_type = RET_INTEGER,
7887 .arg1_type = ARG_PTR_TO_CTX,
7888 .arg2_type = ARG_ANYTHING,
7889 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
7890 .arg4_type = ARG_CONST_SIZE,
7891 .arg5_type = ARG_ANYTHING,
7894 static const struct bpf_func_proto *
7895 sk_reuseport_func_proto(enum bpf_func_id func_id,
7896 const struct bpf_prog *prog)
7899 case BPF_FUNC_sk_select_reuseport:
7900 return &sk_select_reuseport_proto;
7901 case BPF_FUNC_skb_load_bytes:
7902 return &sk_reuseport_load_bytes_proto;
7903 case BPF_FUNC_skb_load_bytes_relative:
7904 return &sk_reuseport_load_bytes_relative_proto;
7906 return bpf_base_func_proto(func_id);
7911 sk_reuseport_is_valid_access(int off, int size,
7912 enum bpf_access_type type,
7913 const struct bpf_prog *prog,
7914 struct bpf_insn_access_aux *info)
7916 const u32 size_default = sizeof(__u32);
7918 if (off < 0 || off >= sizeof(struct sk_reuseport_md) ||
7919 off % size || type != BPF_READ)
7923 case offsetof(struct sk_reuseport_md, data):
7924 info->reg_type = PTR_TO_PACKET;
7925 return size == sizeof(__u64);
7927 case offsetof(struct sk_reuseport_md, data_end):
7928 info->reg_type = PTR_TO_PACKET_END;
7929 return size == sizeof(__u64);
7931 case offsetof(struct sk_reuseport_md, hash):
7932 return size == size_default;
7934 /* Fields that allow narrowing */
7935 case offsetof(struct sk_reuseport_md, eth_protocol):
7936 if (size < FIELD_SIZEOF(struct sk_buff, protocol))
7939 case offsetof(struct sk_reuseport_md, ip_protocol):
7940 case offsetof(struct sk_reuseport_md, bind_inany):
7941 case offsetof(struct sk_reuseport_md, len):
7942 bpf_ctx_record_field_size(info, size_default);
7943 return bpf_ctx_narrow_access_ok(off, size, size_default);
7950 #define SK_REUSEPORT_LOAD_FIELD(F) ({ \
7951 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_reuseport_kern, F), \
7952 si->dst_reg, si->src_reg, \
7953 bpf_target_off(struct sk_reuseport_kern, F, \
7954 FIELD_SIZEOF(struct sk_reuseport_kern, F), \
7958 #define SK_REUSEPORT_LOAD_SKB_FIELD(SKB_FIELD) \
7959 SOCK_ADDR_LOAD_NESTED_FIELD(struct sk_reuseport_kern, \
7964 #define SK_REUSEPORT_LOAD_SK_FIELD_SIZE_OFF(SK_FIELD, BPF_SIZE, EXTRA_OFF) \
7965 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(struct sk_reuseport_kern, \
7968 SK_FIELD, BPF_SIZE, EXTRA_OFF)
7970 static u32 sk_reuseport_convert_ctx_access(enum bpf_access_type type,
7971 const struct bpf_insn *si,
7972 struct bpf_insn *insn_buf,
7973 struct bpf_prog *prog,
7976 struct bpf_insn *insn = insn_buf;
7979 case offsetof(struct sk_reuseport_md, data):
7980 SK_REUSEPORT_LOAD_SKB_FIELD(data);
7983 case offsetof(struct sk_reuseport_md, len):
7984 SK_REUSEPORT_LOAD_SKB_FIELD(len);
7987 case offsetof(struct sk_reuseport_md, eth_protocol):
7988 SK_REUSEPORT_LOAD_SKB_FIELD(protocol);
7991 case offsetof(struct sk_reuseport_md, ip_protocol):
7992 BUILD_BUG_ON(HWEIGHT32(SK_FL_PROTO_MASK) != BITS_PER_BYTE);
7993 SK_REUSEPORT_LOAD_SK_FIELD_SIZE_OFF(__sk_flags_offset,
7995 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_PROTO_MASK);
7996 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg,
7998 /* SK_FL_PROTO_MASK and SK_FL_PROTO_SHIFT are endian
7999 * aware. No further narrowing or masking is needed.
8004 case offsetof(struct sk_reuseport_md, data_end):
8005 SK_REUSEPORT_LOAD_FIELD(data_end);
8008 case offsetof(struct sk_reuseport_md, hash):
8009 SK_REUSEPORT_LOAD_FIELD(hash);
8012 case offsetof(struct sk_reuseport_md, bind_inany):
8013 SK_REUSEPORT_LOAD_FIELD(bind_inany);
8017 return insn - insn_buf;
8020 const struct bpf_verifier_ops sk_reuseport_verifier_ops = {
8021 .get_func_proto = sk_reuseport_func_proto,
8022 .is_valid_access = sk_reuseport_is_valid_access,
8023 .convert_ctx_access = sk_reuseport_convert_ctx_access,
8026 const struct bpf_prog_ops sk_reuseport_prog_ops = {
8028 #endif /* CONFIG_INET */