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 case SKF_AD_VLAN_TAG_PRESENT:
300 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_tci) != 2);
301 BUILD_BUG_ON(VLAN_TAG_PRESENT != 0x1000);
303 /* dst_reg = *(u16 *) (src_reg + offsetof(vlan_tci)) */
304 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
305 offsetof(struct sk_buff, vlan_tci));
306 if (skb_field == SKF_AD_VLAN_TAG) {
307 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg,
311 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 12);
313 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, 1);
318 return insn - insn_buf;
321 static bool convert_bpf_extensions(struct sock_filter *fp,
322 struct bpf_insn **insnp)
324 struct bpf_insn *insn = *insnp;
328 case SKF_AD_OFF + SKF_AD_PROTOCOL:
329 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2);
331 /* A = *(u16 *) (CTX + offsetof(protocol)) */
332 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
333 offsetof(struct sk_buff, protocol));
334 /* A = ntohs(A) [emitting a nop or swap16] */
335 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
338 case SKF_AD_OFF + SKF_AD_PKTTYPE:
339 cnt = convert_skb_access(SKF_AD_PKTTYPE, BPF_REG_A, BPF_REG_CTX, insn);
343 case SKF_AD_OFF + SKF_AD_IFINDEX:
344 case SKF_AD_OFF + SKF_AD_HATYPE:
345 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4);
346 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, type) != 2);
348 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
349 BPF_REG_TMP, BPF_REG_CTX,
350 offsetof(struct sk_buff, dev));
351 /* if (tmp != 0) goto pc + 1 */
352 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_TMP, 0, 1);
353 *insn++ = BPF_EXIT_INSN();
354 if (fp->k == SKF_AD_OFF + SKF_AD_IFINDEX)
355 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_TMP,
356 offsetof(struct net_device, ifindex));
358 *insn = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_TMP,
359 offsetof(struct net_device, type));
362 case SKF_AD_OFF + SKF_AD_MARK:
363 cnt = convert_skb_access(SKF_AD_MARK, BPF_REG_A, BPF_REG_CTX, insn);
367 case SKF_AD_OFF + SKF_AD_RXHASH:
368 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4);
370 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX,
371 offsetof(struct sk_buff, hash));
374 case SKF_AD_OFF + SKF_AD_QUEUE:
375 cnt = convert_skb_access(SKF_AD_QUEUE, BPF_REG_A, BPF_REG_CTX, insn);
379 case SKF_AD_OFF + SKF_AD_VLAN_TAG:
380 cnt = convert_skb_access(SKF_AD_VLAN_TAG,
381 BPF_REG_A, BPF_REG_CTX, insn);
385 case SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT:
386 cnt = convert_skb_access(SKF_AD_VLAN_TAG_PRESENT,
387 BPF_REG_A, BPF_REG_CTX, insn);
391 case SKF_AD_OFF + SKF_AD_VLAN_TPID:
392 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_proto) != 2);
394 /* A = *(u16 *) (CTX + offsetof(vlan_proto)) */
395 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
396 offsetof(struct sk_buff, vlan_proto));
397 /* A = ntohs(A) [emitting a nop or swap16] */
398 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
401 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
402 case SKF_AD_OFF + SKF_AD_NLATTR:
403 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
404 case SKF_AD_OFF + SKF_AD_CPU:
405 case SKF_AD_OFF + SKF_AD_RANDOM:
407 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX);
409 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_A);
411 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_X);
412 /* Emit call(arg1=CTX, arg2=A, arg3=X) */
414 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
415 *insn = BPF_EMIT_CALL(bpf_skb_get_pay_offset);
417 case SKF_AD_OFF + SKF_AD_NLATTR:
418 *insn = BPF_EMIT_CALL(bpf_skb_get_nlattr);
420 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
421 *insn = BPF_EMIT_CALL(bpf_skb_get_nlattr_nest);
423 case SKF_AD_OFF + SKF_AD_CPU:
424 *insn = BPF_EMIT_CALL(bpf_get_raw_cpu_id);
426 case SKF_AD_OFF + SKF_AD_RANDOM:
427 *insn = BPF_EMIT_CALL(bpf_user_rnd_u32);
428 bpf_user_rnd_init_once();
433 case SKF_AD_OFF + SKF_AD_ALU_XOR_X:
435 *insn = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_X);
439 /* This is just a dummy call to avoid letting the compiler
440 * evict __bpf_call_base() as an optimization. Placed here
441 * where no-one bothers.
443 BUG_ON(__bpf_call_base(0, 0, 0, 0, 0) != 0);
451 static bool convert_bpf_ld_abs(struct sock_filter *fp, struct bpf_insn **insnp)
453 const bool unaligned_ok = IS_BUILTIN(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS);
454 int size = bpf_size_to_bytes(BPF_SIZE(fp->code));
455 bool endian = BPF_SIZE(fp->code) == BPF_H ||
456 BPF_SIZE(fp->code) == BPF_W;
457 bool indirect = BPF_MODE(fp->code) == BPF_IND;
458 const int ip_align = NET_IP_ALIGN;
459 struct bpf_insn *insn = *insnp;
463 ((unaligned_ok && offset >= 0) ||
464 (!unaligned_ok && offset >= 0 &&
465 offset + ip_align >= 0 &&
466 offset + ip_align % size == 0))) {
467 bool ldx_off_ok = offset <= S16_MAX;
469 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_H);
470 *insn++ = BPF_ALU64_IMM(BPF_SUB, BPF_REG_TMP, offset);
471 *insn++ = BPF_JMP_IMM(BPF_JSLT, BPF_REG_TMP,
472 size, 2 + endian + (!ldx_off_ok * 2));
474 *insn++ = BPF_LDX_MEM(BPF_SIZE(fp->code), BPF_REG_A,
477 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_D);
478 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_TMP, offset);
479 *insn++ = BPF_LDX_MEM(BPF_SIZE(fp->code), BPF_REG_A,
483 *insn++ = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, size * 8);
484 *insn++ = BPF_JMP_A(8);
487 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX);
488 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_D);
489 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_H);
491 *insn++ = BPF_MOV64_IMM(BPF_REG_ARG4, offset);
493 *insn++ = BPF_MOV64_REG(BPF_REG_ARG4, BPF_REG_X);
495 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_ARG4, offset);
498 switch (BPF_SIZE(fp->code)) {
500 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_8);
503 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_16);
506 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_32);
512 *insn++ = BPF_JMP_IMM(BPF_JSGE, BPF_REG_A, 0, 2);
513 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
514 *insn = BPF_EXIT_INSN();
521 * bpf_convert_filter - convert filter program
522 * @prog: the user passed filter program
523 * @len: the length of the user passed filter program
524 * @new_prog: allocated 'struct bpf_prog' or NULL
525 * @new_len: pointer to store length of converted program
526 * @seen_ld_abs: bool whether we've seen ld_abs/ind
528 * Remap 'sock_filter' style classic BPF (cBPF) instruction set to 'bpf_insn'
529 * style extended BPF (eBPF).
530 * Conversion workflow:
532 * 1) First pass for calculating the new program length:
533 * bpf_convert_filter(old_prog, old_len, NULL, &new_len, &seen_ld_abs)
535 * 2) 2nd pass to remap in two passes: 1st pass finds new
536 * jump offsets, 2nd pass remapping:
537 * bpf_convert_filter(old_prog, old_len, new_prog, &new_len, &seen_ld_abs)
539 static int bpf_convert_filter(struct sock_filter *prog, int len,
540 struct bpf_prog *new_prog, int *new_len,
543 int new_flen = 0, pass = 0, target, i, stack_off;
544 struct bpf_insn *new_insn, *first_insn = NULL;
545 struct sock_filter *fp;
549 BUILD_BUG_ON(BPF_MEMWORDS * sizeof(u32) > MAX_BPF_STACK);
550 BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG);
552 if (len <= 0 || len > BPF_MAXINSNS)
556 first_insn = new_prog->insnsi;
557 addrs = kcalloc(len, sizeof(*addrs),
558 GFP_KERNEL | __GFP_NOWARN);
564 new_insn = first_insn;
567 /* Classic BPF related prologue emission. */
569 /* Classic BPF expects A and X to be reset first. These need
570 * to be guaranteed to be the first two instructions.
572 *new_insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
573 *new_insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_X, BPF_REG_X);
575 /* All programs must keep CTX in callee saved BPF_REG_CTX.
576 * In eBPF case it's done by the compiler, here we need to
577 * do this ourself. Initial CTX is present in BPF_REG_ARG1.
579 *new_insn++ = BPF_MOV64_REG(BPF_REG_CTX, BPF_REG_ARG1);
581 /* For packet access in classic BPF, cache skb->data
582 * in callee-saved BPF R8 and skb->len - skb->data_len
583 * (headlen) in BPF R9. Since classic BPF is read-only
584 * on CTX, we only need to cache it once.
586 *new_insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
587 BPF_REG_D, BPF_REG_CTX,
588 offsetof(struct sk_buff, data));
589 *new_insn++ = BPF_LDX_MEM(BPF_W, BPF_REG_H, BPF_REG_CTX,
590 offsetof(struct sk_buff, len));
591 *new_insn++ = BPF_LDX_MEM(BPF_W, BPF_REG_TMP, BPF_REG_CTX,
592 offsetof(struct sk_buff, data_len));
593 *new_insn++ = BPF_ALU32_REG(BPF_SUB, BPF_REG_H, BPF_REG_TMP);
599 for (i = 0; i < len; fp++, i++) {
600 struct bpf_insn tmp_insns[32] = { };
601 struct bpf_insn *insn = tmp_insns;
604 addrs[i] = new_insn - first_insn;
607 /* All arithmetic insns and skb loads map as-is. */
608 case BPF_ALU | BPF_ADD | BPF_X:
609 case BPF_ALU | BPF_ADD | BPF_K:
610 case BPF_ALU | BPF_SUB | BPF_X:
611 case BPF_ALU | BPF_SUB | BPF_K:
612 case BPF_ALU | BPF_AND | BPF_X:
613 case BPF_ALU | BPF_AND | BPF_K:
614 case BPF_ALU | BPF_OR | BPF_X:
615 case BPF_ALU | BPF_OR | BPF_K:
616 case BPF_ALU | BPF_LSH | BPF_X:
617 case BPF_ALU | BPF_LSH | BPF_K:
618 case BPF_ALU | BPF_RSH | BPF_X:
619 case BPF_ALU | BPF_RSH | BPF_K:
620 case BPF_ALU | BPF_XOR | BPF_X:
621 case BPF_ALU | BPF_XOR | BPF_K:
622 case BPF_ALU | BPF_MUL | BPF_X:
623 case BPF_ALU | BPF_MUL | BPF_K:
624 case BPF_ALU | BPF_DIV | BPF_X:
625 case BPF_ALU | BPF_DIV | BPF_K:
626 case BPF_ALU | BPF_MOD | BPF_X:
627 case BPF_ALU | BPF_MOD | BPF_K:
628 case BPF_ALU | BPF_NEG:
629 case BPF_LD | BPF_ABS | BPF_W:
630 case BPF_LD | BPF_ABS | BPF_H:
631 case BPF_LD | BPF_ABS | BPF_B:
632 case BPF_LD | BPF_IND | BPF_W:
633 case BPF_LD | BPF_IND | BPF_H:
634 case BPF_LD | BPF_IND | BPF_B:
635 /* Check for overloaded BPF extension and
636 * directly convert it if found, otherwise
637 * just move on with mapping.
639 if (BPF_CLASS(fp->code) == BPF_LD &&
640 BPF_MODE(fp->code) == BPF_ABS &&
641 convert_bpf_extensions(fp, &insn))
643 if (BPF_CLASS(fp->code) == BPF_LD &&
644 convert_bpf_ld_abs(fp, &insn)) {
649 if (fp->code == (BPF_ALU | BPF_DIV | BPF_X) ||
650 fp->code == (BPF_ALU | BPF_MOD | BPF_X)) {
651 *insn++ = BPF_MOV32_REG(BPF_REG_X, BPF_REG_X);
652 /* Error with exception code on div/mod by 0.
653 * For cBPF programs, this was always return 0.
655 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_X, 0, 2);
656 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
657 *insn++ = BPF_EXIT_INSN();
660 *insn = BPF_RAW_INSN(fp->code, BPF_REG_A, BPF_REG_X, 0, fp->k);
663 /* Jump transformation cannot use BPF block macros
664 * everywhere as offset calculation and target updates
665 * require a bit more work than the rest, i.e. jump
666 * opcodes map as-is, but offsets need adjustment.
669 #define BPF_EMIT_JMP \
671 const s32 off_min = S16_MIN, off_max = S16_MAX; \
674 if (target >= len || target < 0) \
676 off = addrs ? addrs[target] - addrs[i] - 1 : 0; \
677 /* Adjust pc relative offset for 2nd or 3rd insn. */ \
678 off -= insn - tmp_insns; \
679 /* Reject anything not fitting into insn->off. */ \
680 if (off < off_min || off > off_max) \
685 case BPF_JMP | BPF_JA:
686 target = i + fp->k + 1;
687 insn->code = fp->code;
691 case BPF_JMP | BPF_JEQ | BPF_K:
692 case BPF_JMP | BPF_JEQ | BPF_X:
693 case BPF_JMP | BPF_JSET | BPF_K:
694 case BPF_JMP | BPF_JSET | BPF_X:
695 case BPF_JMP | BPF_JGT | BPF_K:
696 case BPF_JMP | BPF_JGT | BPF_X:
697 case BPF_JMP | BPF_JGE | BPF_K:
698 case BPF_JMP | BPF_JGE | BPF_X:
699 if (BPF_SRC(fp->code) == BPF_K && (int) fp->k < 0) {
700 /* BPF immediates are signed, zero extend
701 * immediate into tmp register and use it
704 *insn++ = BPF_MOV32_IMM(BPF_REG_TMP, fp->k);
706 insn->dst_reg = BPF_REG_A;
707 insn->src_reg = BPF_REG_TMP;
710 insn->dst_reg = BPF_REG_A;
712 bpf_src = BPF_SRC(fp->code);
713 insn->src_reg = bpf_src == BPF_X ? BPF_REG_X : 0;
716 /* Common case where 'jump_false' is next insn. */
718 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
719 target = i + fp->jt + 1;
724 /* Convert some jumps when 'jump_true' is next insn. */
726 switch (BPF_OP(fp->code)) {
728 insn->code = BPF_JMP | BPF_JNE | bpf_src;
731 insn->code = BPF_JMP | BPF_JLE | bpf_src;
734 insn->code = BPF_JMP | BPF_JLT | bpf_src;
740 target = i + fp->jf + 1;
745 /* Other jumps are mapped into two insns: Jxx and JA. */
746 target = i + fp->jt + 1;
747 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
751 insn->code = BPF_JMP | BPF_JA;
752 target = i + fp->jf + 1;
756 /* ldxb 4 * ([14] & 0xf) is remaped into 6 insns. */
757 case BPF_LDX | BPF_MSH | BPF_B: {
758 struct sock_filter tmp = {
759 .code = BPF_LD | BPF_ABS | BPF_B,
766 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
767 /* A = BPF_R0 = *(u8 *) (skb->data + K) */
768 convert_bpf_ld_abs(&tmp, &insn);
771 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, 0xf);
773 *insn++ = BPF_ALU32_IMM(BPF_LSH, BPF_REG_A, 2);
775 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_X);
777 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
779 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_TMP);
782 /* RET_K is remaped into 2 insns. RET_A case doesn't need an
783 * extra mov as BPF_REG_0 is already mapped into BPF_REG_A.
785 case BPF_RET | BPF_A:
786 case BPF_RET | BPF_K:
787 if (BPF_RVAL(fp->code) == BPF_K)
788 *insn++ = BPF_MOV32_RAW(BPF_K, BPF_REG_0,
790 *insn = BPF_EXIT_INSN();
793 /* Store to stack. */
796 stack_off = fp->k * 4 + 4;
797 *insn = BPF_STX_MEM(BPF_W, BPF_REG_FP, BPF_CLASS(fp->code) ==
798 BPF_ST ? BPF_REG_A : BPF_REG_X,
800 /* check_load_and_stores() verifies that classic BPF can
801 * load from stack only after write, so tracking
802 * stack_depth for ST|STX insns is enough
804 if (new_prog && new_prog->aux->stack_depth < stack_off)
805 new_prog->aux->stack_depth = stack_off;
808 /* Load from stack. */
809 case BPF_LD | BPF_MEM:
810 case BPF_LDX | BPF_MEM:
811 stack_off = fp->k * 4 + 4;
812 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
813 BPF_REG_A : BPF_REG_X, BPF_REG_FP,
818 case BPF_LD | BPF_IMM:
819 case BPF_LDX | BPF_IMM:
820 *insn = BPF_MOV32_IMM(BPF_CLASS(fp->code) == BPF_LD ?
821 BPF_REG_A : BPF_REG_X, fp->k);
825 case BPF_MISC | BPF_TAX:
826 *insn = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
830 case BPF_MISC | BPF_TXA:
831 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_X);
834 /* A = skb->len or X = skb->len */
835 case BPF_LD | BPF_W | BPF_LEN:
836 case BPF_LDX | BPF_W | BPF_LEN:
837 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
838 BPF_REG_A : BPF_REG_X, BPF_REG_CTX,
839 offsetof(struct sk_buff, len));
842 /* Access seccomp_data fields. */
843 case BPF_LDX | BPF_ABS | BPF_W:
844 /* A = *(u32 *) (ctx + K) */
845 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX, fp->k);
848 /* Unknown instruction. */
855 memcpy(new_insn, tmp_insns,
856 sizeof(*insn) * (insn - tmp_insns));
857 new_insn += insn - tmp_insns;
861 /* Only calculating new length. */
862 *new_len = new_insn - first_insn;
864 *new_len += 4; /* Prologue bits. */
869 if (new_flen != new_insn - first_insn) {
870 new_flen = new_insn - first_insn;
877 BUG_ON(*new_len != new_flen);
886 * As we dont want to clear mem[] array for each packet going through
887 * __bpf_prog_run(), we check that filter loaded by user never try to read
888 * a cell if not previously written, and we check all branches to be sure
889 * a malicious user doesn't try to abuse us.
891 static int check_load_and_stores(const struct sock_filter *filter, int flen)
893 u16 *masks, memvalid = 0; /* One bit per cell, 16 cells */
896 BUILD_BUG_ON(BPF_MEMWORDS > 16);
898 masks = kmalloc_array(flen, sizeof(*masks), GFP_KERNEL);
902 memset(masks, 0xff, flen * sizeof(*masks));
904 for (pc = 0; pc < flen; pc++) {
905 memvalid &= masks[pc];
907 switch (filter[pc].code) {
910 memvalid |= (1 << filter[pc].k);
912 case BPF_LD | BPF_MEM:
913 case BPF_LDX | BPF_MEM:
914 if (!(memvalid & (1 << filter[pc].k))) {
919 case BPF_JMP | BPF_JA:
920 /* A jump must set masks on target */
921 masks[pc + 1 + filter[pc].k] &= memvalid;
924 case BPF_JMP | BPF_JEQ | BPF_K:
925 case BPF_JMP | BPF_JEQ | BPF_X:
926 case BPF_JMP | BPF_JGE | BPF_K:
927 case BPF_JMP | BPF_JGE | BPF_X:
928 case BPF_JMP | BPF_JGT | BPF_K:
929 case BPF_JMP | BPF_JGT | BPF_X:
930 case BPF_JMP | BPF_JSET | BPF_K:
931 case BPF_JMP | BPF_JSET | BPF_X:
932 /* A jump must set masks on targets */
933 masks[pc + 1 + filter[pc].jt] &= memvalid;
934 masks[pc + 1 + filter[pc].jf] &= memvalid;
944 static bool chk_code_allowed(u16 code_to_probe)
946 static const bool codes[] = {
947 /* 32 bit ALU operations */
948 [BPF_ALU | BPF_ADD | BPF_K] = true,
949 [BPF_ALU | BPF_ADD | BPF_X] = true,
950 [BPF_ALU | BPF_SUB | BPF_K] = true,
951 [BPF_ALU | BPF_SUB | BPF_X] = true,
952 [BPF_ALU | BPF_MUL | BPF_K] = true,
953 [BPF_ALU | BPF_MUL | BPF_X] = true,
954 [BPF_ALU | BPF_DIV | BPF_K] = true,
955 [BPF_ALU | BPF_DIV | BPF_X] = true,
956 [BPF_ALU | BPF_MOD | BPF_K] = true,
957 [BPF_ALU | BPF_MOD | BPF_X] = true,
958 [BPF_ALU | BPF_AND | BPF_K] = true,
959 [BPF_ALU | BPF_AND | BPF_X] = true,
960 [BPF_ALU | BPF_OR | BPF_K] = true,
961 [BPF_ALU | BPF_OR | BPF_X] = true,
962 [BPF_ALU | BPF_XOR | BPF_K] = true,
963 [BPF_ALU | BPF_XOR | BPF_X] = true,
964 [BPF_ALU | BPF_LSH | BPF_K] = true,
965 [BPF_ALU | BPF_LSH | BPF_X] = true,
966 [BPF_ALU | BPF_RSH | BPF_K] = true,
967 [BPF_ALU | BPF_RSH | BPF_X] = true,
968 [BPF_ALU | BPF_NEG] = true,
969 /* Load instructions */
970 [BPF_LD | BPF_W | BPF_ABS] = true,
971 [BPF_LD | BPF_H | BPF_ABS] = true,
972 [BPF_LD | BPF_B | BPF_ABS] = true,
973 [BPF_LD | BPF_W | BPF_LEN] = true,
974 [BPF_LD | BPF_W | BPF_IND] = true,
975 [BPF_LD | BPF_H | BPF_IND] = true,
976 [BPF_LD | BPF_B | BPF_IND] = true,
977 [BPF_LD | BPF_IMM] = true,
978 [BPF_LD | BPF_MEM] = true,
979 [BPF_LDX | BPF_W | BPF_LEN] = true,
980 [BPF_LDX | BPF_B | BPF_MSH] = true,
981 [BPF_LDX | BPF_IMM] = true,
982 [BPF_LDX | BPF_MEM] = true,
983 /* Store instructions */
986 /* Misc instructions */
987 [BPF_MISC | BPF_TAX] = true,
988 [BPF_MISC | BPF_TXA] = true,
989 /* Return instructions */
990 [BPF_RET | BPF_K] = true,
991 [BPF_RET | BPF_A] = true,
992 /* Jump instructions */
993 [BPF_JMP | BPF_JA] = true,
994 [BPF_JMP | BPF_JEQ | BPF_K] = true,
995 [BPF_JMP | BPF_JEQ | BPF_X] = true,
996 [BPF_JMP | BPF_JGE | BPF_K] = true,
997 [BPF_JMP | BPF_JGE | BPF_X] = true,
998 [BPF_JMP | BPF_JGT | BPF_K] = true,
999 [BPF_JMP | BPF_JGT | BPF_X] = true,
1000 [BPF_JMP | BPF_JSET | BPF_K] = true,
1001 [BPF_JMP | BPF_JSET | BPF_X] = true,
1004 if (code_to_probe >= ARRAY_SIZE(codes))
1007 return codes[code_to_probe];
1010 static bool bpf_check_basics_ok(const struct sock_filter *filter,
1015 if (flen == 0 || flen > BPF_MAXINSNS)
1022 * bpf_check_classic - verify socket filter code
1023 * @filter: filter to verify
1024 * @flen: length of filter
1026 * Check the user's filter code. If we let some ugly
1027 * filter code slip through kaboom! The filter must contain
1028 * no references or jumps that are out of range, no illegal
1029 * instructions, and must end with a RET instruction.
1031 * All jumps are forward as they are not signed.
1033 * Returns 0 if the rule set is legal or -EINVAL if not.
1035 static int bpf_check_classic(const struct sock_filter *filter,
1041 /* Check the filter code now */
1042 for (pc = 0; pc < flen; pc++) {
1043 const struct sock_filter *ftest = &filter[pc];
1045 /* May we actually operate on this code? */
1046 if (!chk_code_allowed(ftest->code))
1049 /* Some instructions need special checks */
1050 switch (ftest->code) {
1051 case BPF_ALU | BPF_DIV | BPF_K:
1052 case BPF_ALU | BPF_MOD | BPF_K:
1053 /* Check for division by zero */
1057 case BPF_ALU | BPF_LSH | BPF_K:
1058 case BPF_ALU | BPF_RSH | BPF_K:
1062 case BPF_LD | BPF_MEM:
1063 case BPF_LDX | BPF_MEM:
1066 /* Check for invalid memory addresses */
1067 if (ftest->k >= BPF_MEMWORDS)
1070 case BPF_JMP | BPF_JA:
1071 /* Note, the large ftest->k might cause loops.
1072 * Compare this with conditional jumps below,
1073 * where offsets are limited. --ANK (981016)
1075 if (ftest->k >= (unsigned int)(flen - pc - 1))
1078 case BPF_JMP | BPF_JEQ | BPF_K:
1079 case BPF_JMP | BPF_JEQ | BPF_X:
1080 case BPF_JMP | BPF_JGE | BPF_K:
1081 case BPF_JMP | BPF_JGE | BPF_X:
1082 case BPF_JMP | BPF_JGT | BPF_K:
1083 case BPF_JMP | BPF_JGT | BPF_X:
1084 case BPF_JMP | BPF_JSET | BPF_K:
1085 case BPF_JMP | BPF_JSET | BPF_X:
1086 /* Both conditionals must be safe */
1087 if (pc + ftest->jt + 1 >= flen ||
1088 pc + ftest->jf + 1 >= flen)
1091 case BPF_LD | BPF_W | BPF_ABS:
1092 case BPF_LD | BPF_H | BPF_ABS:
1093 case BPF_LD | BPF_B | BPF_ABS:
1095 if (bpf_anc_helper(ftest) & BPF_ANC)
1097 /* Ancillary operation unknown or unsupported */
1098 if (anc_found == false && ftest->k >= SKF_AD_OFF)
1103 /* Last instruction must be a RET code */
1104 switch (filter[flen - 1].code) {
1105 case BPF_RET | BPF_K:
1106 case BPF_RET | BPF_A:
1107 return check_load_and_stores(filter, flen);
1113 static int bpf_prog_store_orig_filter(struct bpf_prog *fp,
1114 const struct sock_fprog *fprog)
1116 unsigned int fsize = bpf_classic_proglen(fprog);
1117 struct sock_fprog_kern *fkprog;
1119 fp->orig_prog = kmalloc(sizeof(*fkprog), GFP_KERNEL);
1123 fkprog = fp->orig_prog;
1124 fkprog->len = fprog->len;
1126 fkprog->filter = kmemdup(fp->insns, fsize,
1127 GFP_KERNEL | __GFP_NOWARN);
1128 if (!fkprog->filter) {
1129 kfree(fp->orig_prog);
1136 static void bpf_release_orig_filter(struct bpf_prog *fp)
1138 struct sock_fprog_kern *fprog = fp->orig_prog;
1141 kfree(fprog->filter);
1146 static void __bpf_prog_release(struct bpf_prog *prog)
1148 if (prog->type == BPF_PROG_TYPE_SOCKET_FILTER) {
1151 bpf_release_orig_filter(prog);
1152 bpf_prog_free(prog);
1156 static void __sk_filter_release(struct sk_filter *fp)
1158 __bpf_prog_release(fp->prog);
1163 * sk_filter_release_rcu - Release a socket filter by rcu_head
1164 * @rcu: rcu_head that contains the sk_filter to free
1166 static void sk_filter_release_rcu(struct rcu_head *rcu)
1168 struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu);
1170 __sk_filter_release(fp);
1174 * sk_filter_release - release a socket filter
1175 * @fp: filter to remove
1177 * Remove a filter from a socket and release its resources.
1179 static void sk_filter_release(struct sk_filter *fp)
1181 if (refcount_dec_and_test(&fp->refcnt))
1182 call_rcu(&fp->rcu, sk_filter_release_rcu);
1185 void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp)
1187 u32 filter_size = bpf_prog_size(fp->prog->len);
1189 atomic_sub(filter_size, &sk->sk_omem_alloc);
1190 sk_filter_release(fp);
1193 /* try to charge the socket memory if there is space available
1194 * return true on success
1196 static bool __sk_filter_charge(struct sock *sk, struct sk_filter *fp)
1198 u32 filter_size = bpf_prog_size(fp->prog->len);
1200 /* same check as in sock_kmalloc() */
1201 if (filter_size <= sysctl_optmem_max &&
1202 atomic_read(&sk->sk_omem_alloc) + filter_size < sysctl_optmem_max) {
1203 atomic_add(filter_size, &sk->sk_omem_alloc);
1209 bool sk_filter_charge(struct sock *sk, struct sk_filter *fp)
1211 if (!refcount_inc_not_zero(&fp->refcnt))
1214 if (!__sk_filter_charge(sk, fp)) {
1215 sk_filter_release(fp);
1221 static struct bpf_prog *bpf_migrate_filter(struct bpf_prog *fp)
1223 struct sock_filter *old_prog;
1224 struct bpf_prog *old_fp;
1225 int err, new_len, old_len = fp->len;
1226 bool seen_ld_abs = false;
1228 /* We are free to overwrite insns et al right here as it
1229 * won't be used at this point in time anymore internally
1230 * after the migration to the internal BPF instruction
1233 BUILD_BUG_ON(sizeof(struct sock_filter) !=
1234 sizeof(struct bpf_insn));
1236 /* Conversion cannot happen on overlapping memory areas,
1237 * so we need to keep the user BPF around until the 2nd
1238 * pass. At this time, the user BPF is stored in fp->insns.
1240 old_prog = kmemdup(fp->insns, old_len * sizeof(struct sock_filter),
1241 GFP_KERNEL | __GFP_NOWARN);
1247 /* 1st pass: calculate the new program length. */
1248 err = bpf_convert_filter(old_prog, old_len, NULL, &new_len,
1253 /* Expand fp for appending the new filter representation. */
1255 fp = bpf_prog_realloc(old_fp, bpf_prog_size(new_len), 0);
1257 /* The old_fp is still around in case we couldn't
1258 * allocate new memory, so uncharge on that one.
1267 /* 2nd pass: remap sock_filter insns into bpf_insn insns. */
1268 err = bpf_convert_filter(old_prog, old_len, fp, &new_len,
1271 /* 2nd bpf_convert_filter() can fail only if it fails
1272 * to allocate memory, remapping must succeed. Note,
1273 * that at this time old_fp has already been released
1278 fp = bpf_prog_select_runtime(fp, &err);
1288 __bpf_prog_release(fp);
1289 return ERR_PTR(err);
1292 static struct bpf_prog *bpf_prepare_filter(struct bpf_prog *fp,
1293 bpf_aux_classic_check_t trans)
1297 fp->bpf_func = NULL;
1300 err = bpf_check_classic(fp->insns, fp->len);
1302 __bpf_prog_release(fp);
1303 return ERR_PTR(err);
1306 /* There might be additional checks and transformations
1307 * needed on classic filters, f.e. in case of seccomp.
1310 err = trans(fp->insns, fp->len);
1312 __bpf_prog_release(fp);
1313 return ERR_PTR(err);
1317 /* Probe if we can JIT compile the filter and if so, do
1318 * the compilation of the filter.
1320 bpf_jit_compile(fp);
1322 /* JIT compiler couldn't process this filter, so do the
1323 * internal BPF translation for the optimized interpreter.
1326 fp = bpf_migrate_filter(fp);
1332 * bpf_prog_create - create an unattached filter
1333 * @pfp: the unattached filter that is created
1334 * @fprog: the filter program
1336 * Create a filter independent of any socket. We first run some
1337 * sanity checks on it to make sure it does not explode on us later.
1338 * If an error occurs or there is insufficient memory for the filter
1339 * a negative errno code is returned. On success the return is zero.
1341 int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog)
1343 unsigned int fsize = bpf_classic_proglen(fprog);
1344 struct bpf_prog *fp;
1346 /* Make sure new filter is there and in the right amounts. */
1347 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1350 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1354 memcpy(fp->insns, fprog->filter, fsize);
1356 fp->len = fprog->len;
1357 /* Since unattached filters are not copied back to user
1358 * space through sk_get_filter(), we do not need to hold
1359 * a copy here, and can spare us the work.
1361 fp->orig_prog = NULL;
1363 /* bpf_prepare_filter() already takes care of freeing
1364 * memory in case something goes wrong.
1366 fp = bpf_prepare_filter(fp, NULL);
1373 EXPORT_SYMBOL_GPL(bpf_prog_create);
1376 * bpf_prog_create_from_user - create an unattached filter from user buffer
1377 * @pfp: the unattached filter that is created
1378 * @fprog: the filter program
1379 * @trans: post-classic verifier transformation handler
1380 * @save_orig: save classic BPF program
1382 * This function effectively does the same as bpf_prog_create(), only
1383 * that it builds up its insns buffer from user space provided buffer.
1384 * It also allows for passing a bpf_aux_classic_check_t handler.
1386 int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog,
1387 bpf_aux_classic_check_t trans, bool save_orig)
1389 unsigned int fsize = bpf_classic_proglen(fprog);
1390 struct bpf_prog *fp;
1393 /* Make sure new filter is there and in the right amounts. */
1394 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1397 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1401 if (copy_from_user(fp->insns, fprog->filter, fsize)) {
1402 __bpf_prog_free(fp);
1406 fp->len = fprog->len;
1407 fp->orig_prog = NULL;
1410 err = bpf_prog_store_orig_filter(fp, fprog);
1412 __bpf_prog_free(fp);
1417 /* bpf_prepare_filter() already takes care of freeing
1418 * memory in case something goes wrong.
1420 fp = bpf_prepare_filter(fp, trans);
1427 EXPORT_SYMBOL_GPL(bpf_prog_create_from_user);
1429 void bpf_prog_destroy(struct bpf_prog *fp)
1431 __bpf_prog_release(fp);
1433 EXPORT_SYMBOL_GPL(bpf_prog_destroy);
1435 static int __sk_attach_prog(struct bpf_prog *prog, struct sock *sk)
1437 struct sk_filter *fp, *old_fp;
1439 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
1445 if (!__sk_filter_charge(sk, fp)) {
1449 refcount_set(&fp->refcnt, 1);
1451 old_fp = rcu_dereference_protected(sk->sk_filter,
1452 lockdep_sock_is_held(sk));
1453 rcu_assign_pointer(sk->sk_filter, fp);
1456 sk_filter_uncharge(sk, old_fp);
1462 struct bpf_prog *__get_filter(struct sock_fprog *fprog, struct sock *sk)
1464 unsigned int fsize = bpf_classic_proglen(fprog);
1465 struct bpf_prog *prog;
1468 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1469 return ERR_PTR(-EPERM);
1471 /* Make sure new filter is there and in the right amounts. */
1472 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1473 return ERR_PTR(-EINVAL);
1475 prog = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1477 return ERR_PTR(-ENOMEM);
1479 if (copy_from_user(prog->insns, fprog->filter, fsize)) {
1480 __bpf_prog_free(prog);
1481 return ERR_PTR(-EFAULT);
1484 prog->len = fprog->len;
1486 err = bpf_prog_store_orig_filter(prog, fprog);
1488 __bpf_prog_free(prog);
1489 return ERR_PTR(-ENOMEM);
1492 /* bpf_prepare_filter() already takes care of freeing
1493 * memory in case something goes wrong.
1495 return bpf_prepare_filter(prog, NULL);
1499 * sk_attach_filter - attach a socket filter
1500 * @fprog: the filter program
1501 * @sk: the socket to use
1503 * Attach the user's filter code. We first run some sanity checks on
1504 * it to make sure it does not explode on us later. If an error
1505 * occurs or there is insufficient memory for the filter a negative
1506 * errno code is returned. On success the return is zero.
1508 int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1510 struct bpf_prog *prog = __get_filter(fprog, sk);
1514 return PTR_ERR(prog);
1516 err = __sk_attach_prog(prog, sk);
1518 __bpf_prog_release(prog);
1524 EXPORT_SYMBOL_GPL(sk_attach_filter);
1526 int sk_reuseport_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1528 struct bpf_prog *prog = __get_filter(fprog, sk);
1532 return PTR_ERR(prog);
1534 if (bpf_prog_size(prog->len) > sysctl_optmem_max)
1537 err = reuseport_attach_prog(sk, prog);
1540 __bpf_prog_release(prog);
1545 static struct bpf_prog *__get_bpf(u32 ufd, struct sock *sk)
1547 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1548 return ERR_PTR(-EPERM);
1550 return bpf_prog_get_type(ufd, BPF_PROG_TYPE_SOCKET_FILTER);
1553 int sk_attach_bpf(u32 ufd, struct sock *sk)
1555 struct bpf_prog *prog = __get_bpf(ufd, sk);
1559 return PTR_ERR(prog);
1561 err = __sk_attach_prog(prog, sk);
1570 int sk_reuseport_attach_bpf(u32 ufd, struct sock *sk)
1572 struct bpf_prog *prog;
1575 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1578 prog = bpf_prog_get_type(ufd, BPF_PROG_TYPE_SOCKET_FILTER);
1579 if (IS_ERR(prog) && PTR_ERR(prog) == -EINVAL)
1580 prog = bpf_prog_get_type(ufd, BPF_PROG_TYPE_SK_REUSEPORT);
1582 return PTR_ERR(prog);
1584 if (prog->type == BPF_PROG_TYPE_SK_REUSEPORT) {
1585 /* Like other non BPF_PROG_TYPE_SOCKET_FILTER
1586 * bpf prog (e.g. sockmap). It depends on the
1587 * limitation imposed by bpf_prog_load().
1588 * Hence, sysctl_optmem_max is not checked.
1590 if ((sk->sk_type != SOCK_STREAM &&
1591 sk->sk_type != SOCK_DGRAM) ||
1592 (sk->sk_protocol != IPPROTO_UDP &&
1593 sk->sk_protocol != IPPROTO_TCP) ||
1594 (sk->sk_family != AF_INET &&
1595 sk->sk_family != AF_INET6)) {
1600 /* BPF_PROG_TYPE_SOCKET_FILTER */
1601 if (bpf_prog_size(prog->len) > sysctl_optmem_max) {
1607 err = reuseport_attach_prog(sk, prog);
1615 void sk_reuseport_prog_free(struct bpf_prog *prog)
1620 if (prog->type == BPF_PROG_TYPE_SK_REUSEPORT)
1623 bpf_prog_destroy(prog);
1626 struct bpf_scratchpad {
1628 __be32 diff[MAX_BPF_STACK / sizeof(__be32)];
1629 u8 buff[MAX_BPF_STACK];
1633 static DEFINE_PER_CPU(struct bpf_scratchpad, bpf_sp);
1635 static inline int __bpf_try_make_writable(struct sk_buff *skb,
1636 unsigned int write_len)
1638 return skb_ensure_writable(skb, write_len);
1641 static inline int bpf_try_make_writable(struct sk_buff *skb,
1642 unsigned int write_len)
1644 int err = __bpf_try_make_writable(skb, write_len);
1646 bpf_compute_data_pointers(skb);
1650 static int bpf_try_make_head_writable(struct sk_buff *skb)
1652 return bpf_try_make_writable(skb, skb_headlen(skb));
1655 static inline void bpf_push_mac_rcsum(struct sk_buff *skb)
1657 if (skb_at_tc_ingress(skb))
1658 skb_postpush_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1661 static inline void bpf_pull_mac_rcsum(struct sk_buff *skb)
1663 if (skb_at_tc_ingress(skb))
1664 skb_postpull_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1667 BPF_CALL_5(bpf_skb_store_bytes, struct sk_buff *, skb, u32, offset,
1668 const void *, from, u32, len, u64, flags)
1672 if (unlikely(flags & ~(BPF_F_RECOMPUTE_CSUM | BPF_F_INVALIDATE_HASH)))
1674 if (unlikely(offset > 0xffff))
1676 if (unlikely(bpf_try_make_writable(skb, offset + len)))
1679 ptr = skb->data + offset;
1680 if (flags & BPF_F_RECOMPUTE_CSUM)
1681 __skb_postpull_rcsum(skb, ptr, len, offset);
1683 memcpy(ptr, from, len);
1685 if (flags & BPF_F_RECOMPUTE_CSUM)
1686 __skb_postpush_rcsum(skb, ptr, len, offset);
1687 if (flags & BPF_F_INVALIDATE_HASH)
1688 skb_clear_hash(skb);
1693 static const struct bpf_func_proto bpf_skb_store_bytes_proto = {
1694 .func = bpf_skb_store_bytes,
1696 .ret_type = RET_INTEGER,
1697 .arg1_type = ARG_PTR_TO_CTX,
1698 .arg2_type = ARG_ANYTHING,
1699 .arg3_type = ARG_PTR_TO_MEM,
1700 .arg4_type = ARG_CONST_SIZE,
1701 .arg5_type = ARG_ANYTHING,
1704 BPF_CALL_4(bpf_skb_load_bytes, const struct sk_buff *, skb, u32, offset,
1705 void *, to, u32, len)
1709 if (unlikely(offset > 0xffff))
1712 ptr = skb_header_pointer(skb, offset, len, to);
1716 memcpy(to, ptr, len);
1724 static const struct bpf_func_proto bpf_skb_load_bytes_proto = {
1725 .func = bpf_skb_load_bytes,
1727 .ret_type = RET_INTEGER,
1728 .arg1_type = ARG_PTR_TO_CTX,
1729 .arg2_type = ARG_ANYTHING,
1730 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
1731 .arg4_type = ARG_CONST_SIZE,
1734 BPF_CALL_5(bpf_skb_load_bytes_relative, const struct sk_buff *, skb,
1735 u32, offset, void *, to, u32, len, u32, start_header)
1737 u8 *end = skb_tail_pointer(skb);
1738 u8 *net = skb_network_header(skb);
1739 u8 *mac = skb_mac_header(skb);
1742 if (unlikely(offset > 0xffff || len > (end - mac)))
1745 switch (start_header) {
1746 case BPF_HDR_START_MAC:
1749 case BPF_HDR_START_NET:
1756 if (likely(ptr >= mac && ptr + len <= end)) {
1757 memcpy(to, ptr, len);
1766 static const struct bpf_func_proto bpf_skb_load_bytes_relative_proto = {
1767 .func = bpf_skb_load_bytes_relative,
1769 .ret_type = RET_INTEGER,
1770 .arg1_type = ARG_PTR_TO_CTX,
1771 .arg2_type = ARG_ANYTHING,
1772 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
1773 .arg4_type = ARG_CONST_SIZE,
1774 .arg5_type = ARG_ANYTHING,
1777 BPF_CALL_2(bpf_skb_pull_data, struct sk_buff *, skb, u32, len)
1779 /* Idea is the following: should the needed direct read/write
1780 * test fail during runtime, we can pull in more data and redo
1781 * again, since implicitly, we invalidate previous checks here.
1783 * Or, since we know how much we need to make read/writeable,
1784 * this can be done once at the program beginning for direct
1785 * access case. By this we overcome limitations of only current
1786 * headroom being accessible.
1788 return bpf_try_make_writable(skb, len ? : skb_headlen(skb));
1791 static const struct bpf_func_proto bpf_skb_pull_data_proto = {
1792 .func = bpf_skb_pull_data,
1794 .ret_type = RET_INTEGER,
1795 .arg1_type = ARG_PTR_TO_CTX,
1796 .arg2_type = ARG_ANYTHING,
1799 static inline int sk_skb_try_make_writable(struct sk_buff *skb,
1800 unsigned int write_len)
1802 int err = __bpf_try_make_writable(skb, write_len);
1804 bpf_compute_data_end_sk_skb(skb);
1808 BPF_CALL_2(sk_skb_pull_data, struct sk_buff *, skb, u32, len)
1810 /* Idea is the following: should the needed direct read/write
1811 * test fail during runtime, we can pull in more data and redo
1812 * again, since implicitly, we invalidate previous checks here.
1814 * Or, since we know how much we need to make read/writeable,
1815 * this can be done once at the program beginning for direct
1816 * access case. By this we overcome limitations of only current
1817 * headroom being accessible.
1819 return sk_skb_try_make_writable(skb, len ? : skb_headlen(skb));
1822 static const struct bpf_func_proto sk_skb_pull_data_proto = {
1823 .func = sk_skb_pull_data,
1825 .ret_type = RET_INTEGER,
1826 .arg1_type = ARG_PTR_TO_CTX,
1827 .arg2_type = ARG_ANYTHING,
1830 BPF_CALL_5(bpf_l3_csum_replace, struct sk_buff *, skb, u32, offset,
1831 u64, from, u64, to, u64, flags)
1835 if (unlikely(flags & ~(BPF_F_HDR_FIELD_MASK)))
1837 if (unlikely(offset > 0xffff || offset & 1))
1839 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
1842 ptr = (__sum16 *)(skb->data + offset);
1843 switch (flags & BPF_F_HDR_FIELD_MASK) {
1845 if (unlikely(from != 0))
1848 csum_replace_by_diff(ptr, to);
1851 csum_replace2(ptr, from, to);
1854 csum_replace4(ptr, from, to);
1863 static const struct bpf_func_proto bpf_l3_csum_replace_proto = {
1864 .func = bpf_l3_csum_replace,
1866 .ret_type = RET_INTEGER,
1867 .arg1_type = ARG_PTR_TO_CTX,
1868 .arg2_type = ARG_ANYTHING,
1869 .arg3_type = ARG_ANYTHING,
1870 .arg4_type = ARG_ANYTHING,
1871 .arg5_type = ARG_ANYTHING,
1874 BPF_CALL_5(bpf_l4_csum_replace, struct sk_buff *, skb, u32, offset,
1875 u64, from, u64, to, u64, flags)
1877 bool is_pseudo = flags & BPF_F_PSEUDO_HDR;
1878 bool is_mmzero = flags & BPF_F_MARK_MANGLED_0;
1879 bool do_mforce = flags & BPF_F_MARK_ENFORCE;
1882 if (unlikely(flags & ~(BPF_F_MARK_MANGLED_0 | BPF_F_MARK_ENFORCE |
1883 BPF_F_PSEUDO_HDR | BPF_F_HDR_FIELD_MASK)))
1885 if (unlikely(offset > 0xffff || offset & 1))
1887 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
1890 ptr = (__sum16 *)(skb->data + offset);
1891 if (is_mmzero && !do_mforce && !*ptr)
1894 switch (flags & BPF_F_HDR_FIELD_MASK) {
1896 if (unlikely(from != 0))
1899 inet_proto_csum_replace_by_diff(ptr, skb, to, is_pseudo);
1902 inet_proto_csum_replace2(ptr, skb, from, to, is_pseudo);
1905 inet_proto_csum_replace4(ptr, skb, from, to, is_pseudo);
1911 if (is_mmzero && !*ptr)
1912 *ptr = CSUM_MANGLED_0;
1916 static const struct bpf_func_proto bpf_l4_csum_replace_proto = {
1917 .func = bpf_l4_csum_replace,
1919 .ret_type = RET_INTEGER,
1920 .arg1_type = ARG_PTR_TO_CTX,
1921 .arg2_type = ARG_ANYTHING,
1922 .arg3_type = ARG_ANYTHING,
1923 .arg4_type = ARG_ANYTHING,
1924 .arg5_type = ARG_ANYTHING,
1927 BPF_CALL_5(bpf_csum_diff, __be32 *, from, u32, from_size,
1928 __be32 *, to, u32, to_size, __wsum, seed)
1930 struct bpf_scratchpad *sp = this_cpu_ptr(&bpf_sp);
1931 u32 diff_size = from_size + to_size;
1934 /* This is quite flexible, some examples:
1936 * from_size == 0, to_size > 0, seed := csum --> pushing data
1937 * from_size > 0, to_size == 0, seed := csum --> pulling data
1938 * from_size > 0, to_size > 0, seed := 0 --> diffing data
1940 * Even for diffing, from_size and to_size don't need to be equal.
1942 if (unlikely(((from_size | to_size) & (sizeof(__be32) - 1)) ||
1943 diff_size > sizeof(sp->diff)))
1946 for (i = 0; i < from_size / sizeof(__be32); i++, j++)
1947 sp->diff[j] = ~from[i];
1948 for (i = 0; i < to_size / sizeof(__be32); i++, j++)
1949 sp->diff[j] = to[i];
1951 return csum_partial(sp->diff, diff_size, seed);
1954 static const struct bpf_func_proto bpf_csum_diff_proto = {
1955 .func = bpf_csum_diff,
1958 .ret_type = RET_INTEGER,
1959 .arg1_type = ARG_PTR_TO_MEM_OR_NULL,
1960 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
1961 .arg3_type = ARG_PTR_TO_MEM_OR_NULL,
1962 .arg4_type = ARG_CONST_SIZE_OR_ZERO,
1963 .arg5_type = ARG_ANYTHING,
1966 BPF_CALL_2(bpf_csum_update, struct sk_buff *, skb, __wsum, csum)
1968 /* The interface is to be used in combination with bpf_csum_diff()
1969 * for direct packet writes. csum rotation for alignment as well
1970 * as emulating csum_sub() can be done from the eBPF program.
1972 if (skb->ip_summed == CHECKSUM_COMPLETE)
1973 return (skb->csum = csum_add(skb->csum, csum));
1978 static const struct bpf_func_proto bpf_csum_update_proto = {
1979 .func = bpf_csum_update,
1981 .ret_type = RET_INTEGER,
1982 .arg1_type = ARG_PTR_TO_CTX,
1983 .arg2_type = ARG_ANYTHING,
1986 static inline int __bpf_rx_skb(struct net_device *dev, struct sk_buff *skb)
1988 return dev_forward_skb(dev, skb);
1991 static inline int __bpf_rx_skb_no_mac(struct net_device *dev,
1992 struct sk_buff *skb)
1994 int ret = ____dev_forward_skb(dev, skb);
1998 ret = netif_rx(skb);
2004 static inline int __bpf_tx_skb(struct net_device *dev, struct sk_buff *skb)
2008 if (unlikely(__this_cpu_read(xmit_recursion) > XMIT_RECURSION_LIMIT)) {
2009 net_crit_ratelimited("bpf: recursion limit reached on datapath, buggy bpf program?\n");
2016 __this_cpu_inc(xmit_recursion);
2017 ret = dev_queue_xmit(skb);
2018 __this_cpu_dec(xmit_recursion);
2023 static int __bpf_redirect_no_mac(struct sk_buff *skb, struct net_device *dev,
2026 /* skb->mac_len is not set on normal egress */
2027 unsigned int mlen = skb->network_header - skb->mac_header;
2029 __skb_pull(skb, mlen);
2031 /* At ingress, the mac header has already been pulled once.
2032 * At egress, skb_pospull_rcsum has to be done in case that
2033 * the skb is originated from ingress (i.e. a forwarded skb)
2034 * to ensure that rcsum starts at net header.
2036 if (!skb_at_tc_ingress(skb))
2037 skb_postpull_rcsum(skb, skb_mac_header(skb), mlen);
2038 skb_pop_mac_header(skb);
2039 skb_reset_mac_len(skb);
2040 return flags & BPF_F_INGRESS ?
2041 __bpf_rx_skb_no_mac(dev, skb) : __bpf_tx_skb(dev, skb);
2044 static int __bpf_redirect_common(struct sk_buff *skb, struct net_device *dev,
2047 /* Verify that a link layer header is carried */
2048 if (unlikely(skb->mac_header >= skb->network_header)) {
2053 bpf_push_mac_rcsum(skb);
2054 return flags & BPF_F_INGRESS ?
2055 __bpf_rx_skb(dev, skb) : __bpf_tx_skb(dev, skb);
2058 static int __bpf_redirect(struct sk_buff *skb, struct net_device *dev,
2061 if (dev_is_mac_header_xmit(dev))
2062 return __bpf_redirect_common(skb, dev, flags);
2064 return __bpf_redirect_no_mac(skb, dev, flags);
2067 BPF_CALL_3(bpf_clone_redirect, struct sk_buff *, skb, u32, ifindex, u64, flags)
2069 struct net_device *dev;
2070 struct sk_buff *clone;
2073 if (unlikely(flags & ~(BPF_F_INGRESS)))
2076 dev = dev_get_by_index_rcu(dev_net(skb->dev), ifindex);
2080 clone = skb_clone(skb, GFP_ATOMIC);
2081 if (unlikely(!clone))
2084 /* For direct write, we need to keep the invariant that the skbs
2085 * we're dealing with need to be uncloned. Should uncloning fail
2086 * here, we need to free the just generated clone to unclone once
2089 ret = bpf_try_make_head_writable(skb);
2090 if (unlikely(ret)) {
2095 return __bpf_redirect(clone, dev, flags);
2098 static const struct bpf_func_proto bpf_clone_redirect_proto = {
2099 .func = bpf_clone_redirect,
2101 .ret_type = RET_INTEGER,
2102 .arg1_type = ARG_PTR_TO_CTX,
2103 .arg2_type = ARG_ANYTHING,
2104 .arg3_type = ARG_ANYTHING,
2107 DEFINE_PER_CPU(struct bpf_redirect_info, bpf_redirect_info);
2108 EXPORT_PER_CPU_SYMBOL_GPL(bpf_redirect_info);
2110 BPF_CALL_2(bpf_redirect, u32, ifindex, u64, flags)
2112 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
2114 if (unlikely(flags & ~(BPF_F_INGRESS)))
2117 ri->ifindex = ifindex;
2120 return TC_ACT_REDIRECT;
2123 int skb_do_redirect(struct sk_buff *skb)
2125 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
2126 struct net_device *dev;
2128 dev = dev_get_by_index_rcu(dev_net(skb->dev), ri->ifindex);
2130 if (unlikely(!dev)) {
2135 return __bpf_redirect(skb, dev, ri->flags);
2138 static const struct bpf_func_proto bpf_redirect_proto = {
2139 .func = bpf_redirect,
2141 .ret_type = RET_INTEGER,
2142 .arg1_type = ARG_ANYTHING,
2143 .arg2_type = ARG_ANYTHING,
2146 BPF_CALL_2(bpf_msg_apply_bytes, struct sk_msg *, msg, u32, bytes)
2148 msg->apply_bytes = bytes;
2152 static const struct bpf_func_proto bpf_msg_apply_bytes_proto = {
2153 .func = bpf_msg_apply_bytes,
2155 .ret_type = RET_INTEGER,
2156 .arg1_type = ARG_PTR_TO_CTX,
2157 .arg2_type = ARG_ANYTHING,
2160 BPF_CALL_2(bpf_msg_cork_bytes, struct sk_msg *, msg, u32, bytes)
2162 msg->cork_bytes = bytes;
2166 static const struct bpf_func_proto bpf_msg_cork_bytes_proto = {
2167 .func = bpf_msg_cork_bytes,
2169 .ret_type = RET_INTEGER,
2170 .arg1_type = ARG_PTR_TO_CTX,
2171 .arg2_type = ARG_ANYTHING,
2174 BPF_CALL_4(bpf_msg_pull_data, struct sk_msg *, msg, u32, start,
2175 u32, end, u64, flags)
2177 u32 len = 0, offset = 0, copy = 0, poffset = 0, bytes = end - start;
2178 u32 first_sge, last_sge, i, shift, bytes_sg_total;
2179 struct scatterlist *sge;
2180 u8 *raw, *to, *from;
2183 if (unlikely(flags || end <= start))
2186 /* First find the starting scatterlist element */
2189 len = sk_msg_elem(msg, i)->length;
2190 if (start < offset + len)
2193 sk_msg_iter_var_next(i);
2194 } while (i != msg->sg.end);
2196 if (unlikely(start >= offset + len))
2200 /* The start may point into the sg element so we need to also
2201 * account for the headroom.
2203 bytes_sg_total = start - offset + bytes;
2204 if (!msg->sg.copy[i] && bytes_sg_total <= len)
2207 /* At this point we need to linearize multiple scatterlist
2208 * elements or a single shared page. Either way we need to
2209 * copy into a linear buffer exclusively owned by BPF. Then
2210 * place the buffer in the scatterlist and fixup the original
2211 * entries by removing the entries now in the linear buffer
2212 * and shifting the remaining entries. For now we do not try
2213 * to copy partial entries to avoid complexity of running out
2214 * of sg_entry slots. The downside is reading a single byte
2215 * will copy the entire sg entry.
2218 copy += sk_msg_elem(msg, i)->length;
2219 sk_msg_iter_var_next(i);
2220 if (bytes_sg_total <= copy)
2222 } while (i != msg->sg.end);
2225 if (unlikely(bytes_sg_total > copy))
2228 page = alloc_pages(__GFP_NOWARN | GFP_ATOMIC | __GFP_COMP,
2230 if (unlikely(!page))
2233 raw = page_address(page);
2236 sge = sk_msg_elem(msg, i);
2237 from = sg_virt(sge);
2241 memcpy(to, from, len);
2244 put_page(sg_page(sge));
2246 sk_msg_iter_var_next(i);
2247 } while (i != last_sge);
2249 sg_set_page(&msg->sg.data[first_sge], page, copy, 0);
2251 /* To repair sg ring we need to shift entries. If we only
2252 * had a single entry though we can just replace it and
2253 * be done. Otherwise walk the ring and shift the entries.
2255 WARN_ON_ONCE(last_sge == first_sge);
2256 shift = last_sge > first_sge ?
2257 last_sge - first_sge - 1 :
2258 MAX_SKB_FRAGS - first_sge + last_sge - 1;
2263 sk_msg_iter_var_next(i);
2267 if (i + shift >= MAX_MSG_FRAGS)
2268 move_from = i + shift - MAX_MSG_FRAGS;
2270 move_from = i + shift;
2271 if (move_from == msg->sg.end)
2274 msg->sg.data[i] = msg->sg.data[move_from];
2275 msg->sg.data[move_from].length = 0;
2276 msg->sg.data[move_from].page_link = 0;
2277 msg->sg.data[move_from].offset = 0;
2278 sk_msg_iter_var_next(i);
2281 msg->sg.end = msg->sg.end - shift > msg->sg.end ?
2282 msg->sg.end - shift + MAX_MSG_FRAGS :
2283 msg->sg.end - shift;
2285 msg->data = sg_virt(&msg->sg.data[first_sge]) + start - offset;
2286 msg->data_end = msg->data + bytes;
2290 static const struct bpf_func_proto bpf_msg_pull_data_proto = {
2291 .func = bpf_msg_pull_data,
2293 .ret_type = RET_INTEGER,
2294 .arg1_type = ARG_PTR_TO_CTX,
2295 .arg2_type = ARG_ANYTHING,
2296 .arg3_type = ARG_ANYTHING,
2297 .arg4_type = ARG_ANYTHING,
2300 BPF_CALL_4(bpf_msg_push_data, struct sk_msg *, msg, u32, start,
2301 u32, len, u64, flags)
2303 struct scatterlist sge, nsge, nnsge, rsge = {0}, *psge;
2304 u32 new, i = 0, l, space, copy = 0, offset = 0;
2305 u8 *raw, *to, *from;
2308 if (unlikely(flags))
2311 /* First find the starting scatterlist element */
2314 l = sk_msg_elem(msg, i)->length;
2316 if (start < offset + l)
2319 sk_msg_iter_var_next(i);
2320 } while (i != msg->sg.end);
2322 if (start >= offset + l)
2325 space = MAX_MSG_FRAGS - sk_msg_elem_used(msg);
2327 /* If no space available will fallback to copy, we need at
2328 * least one scatterlist elem available to push data into
2329 * when start aligns to the beginning of an element or two
2330 * when it falls inside an element. We handle the start equals
2331 * offset case because its the common case for inserting a
2334 if (!space || (space == 1 && start != offset))
2335 copy = msg->sg.data[i].length;
2337 page = alloc_pages(__GFP_NOWARN | GFP_ATOMIC | __GFP_COMP,
2338 get_order(copy + len));
2339 if (unlikely(!page))
2345 raw = page_address(page);
2347 psge = sk_msg_elem(msg, i);
2348 front = start - offset;
2349 back = psge->length - front;
2350 from = sg_virt(psge);
2353 memcpy(raw, from, front);
2357 to = raw + front + len;
2359 memcpy(to, from, back);
2362 put_page(sg_page(psge));
2363 } else if (start - offset) {
2364 psge = sk_msg_elem(msg, i);
2365 rsge = sk_msg_elem_cpy(msg, i);
2367 psge->length = start - offset;
2368 rsge.length -= psge->length;
2369 rsge.offset += start;
2371 sk_msg_iter_var_next(i);
2372 sg_unmark_end(psge);
2373 sk_msg_iter_next(msg, end);
2376 /* Slot(s) to place newly allocated data */
2379 /* Shift one or two slots as needed */
2381 sge = sk_msg_elem_cpy(msg, i);
2383 sk_msg_iter_var_next(i);
2384 sg_unmark_end(&sge);
2385 sk_msg_iter_next(msg, end);
2387 nsge = sk_msg_elem_cpy(msg, i);
2389 sk_msg_iter_var_next(i);
2390 nnsge = sk_msg_elem_cpy(msg, i);
2393 while (i != msg->sg.end) {
2394 msg->sg.data[i] = sge;
2396 sk_msg_iter_var_next(i);
2399 nnsge = sk_msg_elem_cpy(msg, i);
2401 nsge = sk_msg_elem_cpy(msg, i);
2406 /* Place newly allocated data buffer */
2407 sk_mem_charge(msg->sk, len);
2408 msg->sg.size += len;
2409 msg->sg.copy[new] = false;
2410 sg_set_page(&msg->sg.data[new], page, len + copy, 0);
2412 get_page(sg_page(&rsge));
2413 sk_msg_iter_var_next(new);
2414 msg->sg.data[new] = rsge;
2417 sk_msg_compute_data_pointers(msg);
2421 static const struct bpf_func_proto bpf_msg_push_data_proto = {
2422 .func = bpf_msg_push_data,
2424 .ret_type = RET_INTEGER,
2425 .arg1_type = ARG_PTR_TO_CTX,
2426 .arg2_type = ARG_ANYTHING,
2427 .arg3_type = ARG_ANYTHING,
2428 .arg4_type = ARG_ANYTHING,
2431 BPF_CALL_1(bpf_get_cgroup_classid, const struct sk_buff *, skb)
2433 return task_get_classid(skb);
2436 static const struct bpf_func_proto bpf_get_cgroup_classid_proto = {
2437 .func = bpf_get_cgroup_classid,
2439 .ret_type = RET_INTEGER,
2440 .arg1_type = ARG_PTR_TO_CTX,
2443 BPF_CALL_1(bpf_get_route_realm, const struct sk_buff *, skb)
2445 return dst_tclassid(skb);
2448 static const struct bpf_func_proto bpf_get_route_realm_proto = {
2449 .func = bpf_get_route_realm,
2451 .ret_type = RET_INTEGER,
2452 .arg1_type = ARG_PTR_TO_CTX,
2455 BPF_CALL_1(bpf_get_hash_recalc, struct sk_buff *, skb)
2457 /* If skb_clear_hash() was called due to mangling, we can
2458 * trigger SW recalculation here. Later access to hash
2459 * can then use the inline skb->hash via context directly
2460 * instead of calling this helper again.
2462 return skb_get_hash(skb);
2465 static const struct bpf_func_proto bpf_get_hash_recalc_proto = {
2466 .func = bpf_get_hash_recalc,
2468 .ret_type = RET_INTEGER,
2469 .arg1_type = ARG_PTR_TO_CTX,
2472 BPF_CALL_1(bpf_set_hash_invalid, struct sk_buff *, skb)
2474 /* After all direct packet write, this can be used once for
2475 * triggering a lazy recalc on next skb_get_hash() invocation.
2477 skb_clear_hash(skb);
2481 static const struct bpf_func_proto bpf_set_hash_invalid_proto = {
2482 .func = bpf_set_hash_invalid,
2484 .ret_type = RET_INTEGER,
2485 .arg1_type = ARG_PTR_TO_CTX,
2488 BPF_CALL_2(bpf_set_hash, struct sk_buff *, skb, u32, hash)
2490 /* Set user specified hash as L4(+), so that it gets returned
2491 * on skb_get_hash() call unless BPF prog later on triggers a
2494 __skb_set_sw_hash(skb, hash, true);
2498 static const struct bpf_func_proto bpf_set_hash_proto = {
2499 .func = bpf_set_hash,
2501 .ret_type = RET_INTEGER,
2502 .arg1_type = ARG_PTR_TO_CTX,
2503 .arg2_type = ARG_ANYTHING,
2506 BPF_CALL_3(bpf_skb_vlan_push, struct sk_buff *, skb, __be16, vlan_proto,
2511 if (unlikely(vlan_proto != htons(ETH_P_8021Q) &&
2512 vlan_proto != htons(ETH_P_8021AD)))
2513 vlan_proto = htons(ETH_P_8021Q);
2515 bpf_push_mac_rcsum(skb);
2516 ret = skb_vlan_push(skb, vlan_proto, vlan_tci);
2517 bpf_pull_mac_rcsum(skb);
2519 bpf_compute_data_pointers(skb);
2523 static const struct bpf_func_proto bpf_skb_vlan_push_proto = {
2524 .func = bpf_skb_vlan_push,
2526 .ret_type = RET_INTEGER,
2527 .arg1_type = ARG_PTR_TO_CTX,
2528 .arg2_type = ARG_ANYTHING,
2529 .arg3_type = ARG_ANYTHING,
2532 BPF_CALL_1(bpf_skb_vlan_pop, struct sk_buff *, skb)
2536 bpf_push_mac_rcsum(skb);
2537 ret = skb_vlan_pop(skb);
2538 bpf_pull_mac_rcsum(skb);
2540 bpf_compute_data_pointers(skb);
2544 static const struct bpf_func_proto bpf_skb_vlan_pop_proto = {
2545 .func = bpf_skb_vlan_pop,
2547 .ret_type = RET_INTEGER,
2548 .arg1_type = ARG_PTR_TO_CTX,
2551 static int bpf_skb_generic_push(struct sk_buff *skb, u32 off, u32 len)
2553 /* Caller already did skb_cow() with len as headroom,
2554 * so no need to do it here.
2557 memmove(skb->data, skb->data + len, off);
2558 memset(skb->data + off, 0, len);
2560 /* No skb_postpush_rcsum(skb, skb->data + off, len)
2561 * needed here as it does not change the skb->csum
2562 * result for checksum complete when summing over
2568 static int bpf_skb_generic_pop(struct sk_buff *skb, u32 off, u32 len)
2570 /* skb_ensure_writable() is not needed here, as we're
2571 * already working on an uncloned skb.
2573 if (unlikely(!pskb_may_pull(skb, off + len)))
2576 skb_postpull_rcsum(skb, skb->data + off, len);
2577 memmove(skb->data + len, skb->data, off);
2578 __skb_pull(skb, len);
2583 static int bpf_skb_net_hdr_push(struct sk_buff *skb, u32 off, u32 len)
2585 bool trans_same = skb->transport_header == skb->network_header;
2588 /* There's no need for __skb_push()/__skb_pull() pair to
2589 * get to the start of the mac header as we're guaranteed
2590 * to always start from here under eBPF.
2592 ret = bpf_skb_generic_push(skb, off, len);
2594 skb->mac_header -= len;
2595 skb->network_header -= len;
2597 skb->transport_header = skb->network_header;
2603 static int bpf_skb_net_hdr_pop(struct sk_buff *skb, u32 off, u32 len)
2605 bool trans_same = skb->transport_header == skb->network_header;
2608 /* Same here, __skb_push()/__skb_pull() pair not needed. */
2609 ret = bpf_skb_generic_pop(skb, off, len);
2611 skb->mac_header += len;
2612 skb->network_header += len;
2614 skb->transport_header = skb->network_header;
2620 static int bpf_skb_proto_4_to_6(struct sk_buff *skb)
2622 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
2623 u32 off = skb_mac_header_len(skb);
2626 /* SCTP uses GSO_BY_FRAGS, thus cannot adjust it. */
2627 if (skb_is_gso(skb) && unlikely(skb_is_gso_sctp(skb)))
2630 ret = skb_cow(skb, len_diff);
2631 if (unlikely(ret < 0))
2634 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
2635 if (unlikely(ret < 0))
2638 if (skb_is_gso(skb)) {
2639 struct skb_shared_info *shinfo = skb_shinfo(skb);
2641 /* SKB_GSO_TCPV4 needs to be changed into
2644 if (shinfo->gso_type & SKB_GSO_TCPV4) {
2645 shinfo->gso_type &= ~SKB_GSO_TCPV4;
2646 shinfo->gso_type |= SKB_GSO_TCPV6;
2649 /* Due to IPv6 header, MSS needs to be downgraded. */
2650 skb_decrease_gso_size(shinfo, len_diff);
2651 /* Header must be checked, and gso_segs recomputed. */
2652 shinfo->gso_type |= SKB_GSO_DODGY;
2653 shinfo->gso_segs = 0;
2656 skb->protocol = htons(ETH_P_IPV6);
2657 skb_clear_hash(skb);
2662 static int bpf_skb_proto_6_to_4(struct sk_buff *skb)
2664 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
2665 u32 off = skb_mac_header_len(skb);
2668 /* SCTP uses GSO_BY_FRAGS, thus cannot adjust it. */
2669 if (skb_is_gso(skb) && unlikely(skb_is_gso_sctp(skb)))
2672 ret = skb_unclone(skb, GFP_ATOMIC);
2673 if (unlikely(ret < 0))
2676 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
2677 if (unlikely(ret < 0))
2680 if (skb_is_gso(skb)) {
2681 struct skb_shared_info *shinfo = skb_shinfo(skb);
2683 /* SKB_GSO_TCPV6 needs to be changed into
2686 if (shinfo->gso_type & SKB_GSO_TCPV6) {
2687 shinfo->gso_type &= ~SKB_GSO_TCPV6;
2688 shinfo->gso_type |= SKB_GSO_TCPV4;
2691 /* Due to IPv4 header, MSS can be upgraded. */
2692 skb_increase_gso_size(shinfo, len_diff);
2693 /* Header must be checked, and gso_segs recomputed. */
2694 shinfo->gso_type |= SKB_GSO_DODGY;
2695 shinfo->gso_segs = 0;
2698 skb->protocol = htons(ETH_P_IP);
2699 skb_clear_hash(skb);
2704 static int bpf_skb_proto_xlat(struct sk_buff *skb, __be16 to_proto)
2706 __be16 from_proto = skb->protocol;
2708 if (from_proto == htons(ETH_P_IP) &&
2709 to_proto == htons(ETH_P_IPV6))
2710 return bpf_skb_proto_4_to_6(skb);
2712 if (from_proto == htons(ETH_P_IPV6) &&
2713 to_proto == htons(ETH_P_IP))
2714 return bpf_skb_proto_6_to_4(skb);
2719 BPF_CALL_3(bpf_skb_change_proto, struct sk_buff *, skb, __be16, proto,
2724 if (unlikely(flags))
2727 /* General idea is that this helper does the basic groundwork
2728 * needed for changing the protocol, and eBPF program fills the
2729 * rest through bpf_skb_store_bytes(), bpf_lX_csum_replace()
2730 * and other helpers, rather than passing a raw buffer here.
2732 * The rationale is to keep this minimal and without a need to
2733 * deal with raw packet data. F.e. even if we would pass buffers
2734 * here, the program still needs to call the bpf_lX_csum_replace()
2735 * helpers anyway. Plus, this way we keep also separation of
2736 * concerns, since f.e. bpf_skb_store_bytes() should only take
2739 * Currently, additional options and extension header space are
2740 * not supported, but flags register is reserved so we can adapt
2741 * that. For offloads, we mark packet as dodgy, so that headers
2742 * need to be verified first.
2744 ret = bpf_skb_proto_xlat(skb, proto);
2745 bpf_compute_data_pointers(skb);
2749 static const struct bpf_func_proto bpf_skb_change_proto_proto = {
2750 .func = bpf_skb_change_proto,
2752 .ret_type = RET_INTEGER,
2753 .arg1_type = ARG_PTR_TO_CTX,
2754 .arg2_type = ARG_ANYTHING,
2755 .arg3_type = ARG_ANYTHING,
2758 BPF_CALL_2(bpf_skb_change_type, struct sk_buff *, skb, u32, pkt_type)
2760 /* We only allow a restricted subset to be changed for now. */
2761 if (unlikely(!skb_pkt_type_ok(skb->pkt_type) ||
2762 !skb_pkt_type_ok(pkt_type)))
2765 skb->pkt_type = pkt_type;
2769 static const struct bpf_func_proto bpf_skb_change_type_proto = {
2770 .func = bpf_skb_change_type,
2772 .ret_type = RET_INTEGER,
2773 .arg1_type = ARG_PTR_TO_CTX,
2774 .arg2_type = ARG_ANYTHING,
2777 static u32 bpf_skb_net_base_len(const struct sk_buff *skb)
2779 switch (skb->protocol) {
2780 case htons(ETH_P_IP):
2781 return sizeof(struct iphdr);
2782 case htons(ETH_P_IPV6):
2783 return sizeof(struct ipv6hdr);
2789 static int bpf_skb_net_grow(struct sk_buff *skb, u32 len_diff)
2791 u32 off = skb_mac_header_len(skb) + bpf_skb_net_base_len(skb);
2794 /* SCTP uses GSO_BY_FRAGS, thus cannot adjust it. */
2795 if (skb_is_gso(skb) && unlikely(skb_is_gso_sctp(skb)))
2798 ret = skb_cow(skb, len_diff);
2799 if (unlikely(ret < 0))
2802 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
2803 if (unlikely(ret < 0))
2806 if (skb_is_gso(skb)) {
2807 struct skb_shared_info *shinfo = skb_shinfo(skb);
2809 /* Due to header grow, MSS needs to be downgraded. */
2810 skb_decrease_gso_size(shinfo, len_diff);
2811 /* Header must be checked, and gso_segs recomputed. */
2812 shinfo->gso_type |= SKB_GSO_DODGY;
2813 shinfo->gso_segs = 0;
2819 static int bpf_skb_net_shrink(struct sk_buff *skb, u32 len_diff)
2821 u32 off = skb_mac_header_len(skb) + bpf_skb_net_base_len(skb);
2824 /* SCTP uses GSO_BY_FRAGS, thus cannot adjust it. */
2825 if (skb_is_gso(skb) && unlikely(skb_is_gso_sctp(skb)))
2828 ret = skb_unclone(skb, GFP_ATOMIC);
2829 if (unlikely(ret < 0))
2832 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
2833 if (unlikely(ret < 0))
2836 if (skb_is_gso(skb)) {
2837 struct skb_shared_info *shinfo = skb_shinfo(skb);
2839 /* Due to header shrink, MSS can be upgraded. */
2840 skb_increase_gso_size(shinfo, len_diff);
2841 /* Header must be checked, and gso_segs recomputed. */
2842 shinfo->gso_type |= SKB_GSO_DODGY;
2843 shinfo->gso_segs = 0;
2849 static u32 __bpf_skb_max_len(const struct sk_buff *skb)
2851 return skb->dev ? skb->dev->mtu + skb->dev->hard_header_len :
2855 static int bpf_skb_adjust_net(struct sk_buff *skb, s32 len_diff)
2857 bool trans_same = skb->transport_header == skb->network_header;
2858 u32 len_cur, len_diff_abs = abs(len_diff);
2859 u32 len_min = bpf_skb_net_base_len(skb);
2860 u32 len_max = __bpf_skb_max_len(skb);
2861 __be16 proto = skb->protocol;
2862 bool shrink = len_diff < 0;
2865 if (unlikely(len_diff_abs > 0xfffU))
2867 if (unlikely(proto != htons(ETH_P_IP) &&
2868 proto != htons(ETH_P_IPV6)))
2871 len_cur = skb->len - skb_network_offset(skb);
2872 if (skb_transport_header_was_set(skb) && !trans_same)
2873 len_cur = skb_network_header_len(skb);
2874 if ((shrink && (len_diff_abs >= len_cur ||
2875 len_cur - len_diff_abs < len_min)) ||
2876 (!shrink && (skb->len + len_diff_abs > len_max &&
2880 ret = shrink ? bpf_skb_net_shrink(skb, len_diff_abs) :
2881 bpf_skb_net_grow(skb, len_diff_abs);
2883 bpf_compute_data_pointers(skb);
2887 BPF_CALL_4(bpf_skb_adjust_room, struct sk_buff *, skb, s32, len_diff,
2888 u32, mode, u64, flags)
2890 if (unlikely(flags))
2892 if (likely(mode == BPF_ADJ_ROOM_NET))
2893 return bpf_skb_adjust_net(skb, len_diff);
2898 static const struct bpf_func_proto bpf_skb_adjust_room_proto = {
2899 .func = bpf_skb_adjust_room,
2901 .ret_type = RET_INTEGER,
2902 .arg1_type = ARG_PTR_TO_CTX,
2903 .arg2_type = ARG_ANYTHING,
2904 .arg3_type = ARG_ANYTHING,
2905 .arg4_type = ARG_ANYTHING,
2908 static u32 __bpf_skb_min_len(const struct sk_buff *skb)
2910 u32 min_len = skb_network_offset(skb);
2912 if (skb_transport_header_was_set(skb))
2913 min_len = skb_transport_offset(skb);
2914 if (skb->ip_summed == CHECKSUM_PARTIAL)
2915 min_len = skb_checksum_start_offset(skb) +
2916 skb->csum_offset + sizeof(__sum16);
2920 static int bpf_skb_grow_rcsum(struct sk_buff *skb, unsigned int new_len)
2922 unsigned int old_len = skb->len;
2925 ret = __skb_grow_rcsum(skb, new_len);
2927 memset(skb->data + old_len, 0, new_len - old_len);
2931 static int bpf_skb_trim_rcsum(struct sk_buff *skb, unsigned int new_len)
2933 return __skb_trim_rcsum(skb, new_len);
2936 static inline int __bpf_skb_change_tail(struct sk_buff *skb, u32 new_len,
2939 u32 max_len = __bpf_skb_max_len(skb);
2940 u32 min_len = __bpf_skb_min_len(skb);
2943 if (unlikely(flags || new_len > max_len || new_len < min_len))
2945 if (skb->encapsulation)
2948 /* The basic idea of this helper is that it's performing the
2949 * needed work to either grow or trim an skb, and eBPF program
2950 * rewrites the rest via helpers like bpf_skb_store_bytes(),
2951 * bpf_lX_csum_replace() and others rather than passing a raw
2952 * buffer here. This one is a slow path helper and intended
2953 * for replies with control messages.
2955 * Like in bpf_skb_change_proto(), we want to keep this rather
2956 * minimal and without protocol specifics so that we are able
2957 * to separate concerns as in bpf_skb_store_bytes() should only
2958 * be the one responsible for writing buffers.
2960 * It's really expected to be a slow path operation here for
2961 * control message replies, so we're implicitly linearizing,
2962 * uncloning and drop offloads from the skb by this.
2964 ret = __bpf_try_make_writable(skb, skb->len);
2966 if (new_len > skb->len)
2967 ret = bpf_skb_grow_rcsum(skb, new_len);
2968 else if (new_len < skb->len)
2969 ret = bpf_skb_trim_rcsum(skb, new_len);
2970 if (!ret && skb_is_gso(skb))
2976 BPF_CALL_3(bpf_skb_change_tail, struct sk_buff *, skb, u32, new_len,
2979 int ret = __bpf_skb_change_tail(skb, new_len, flags);
2981 bpf_compute_data_pointers(skb);
2985 static const struct bpf_func_proto bpf_skb_change_tail_proto = {
2986 .func = bpf_skb_change_tail,
2988 .ret_type = RET_INTEGER,
2989 .arg1_type = ARG_PTR_TO_CTX,
2990 .arg2_type = ARG_ANYTHING,
2991 .arg3_type = ARG_ANYTHING,
2994 BPF_CALL_3(sk_skb_change_tail, struct sk_buff *, skb, u32, new_len,
2997 int ret = __bpf_skb_change_tail(skb, new_len, flags);
2999 bpf_compute_data_end_sk_skb(skb);
3003 static const struct bpf_func_proto sk_skb_change_tail_proto = {
3004 .func = sk_skb_change_tail,
3006 .ret_type = RET_INTEGER,
3007 .arg1_type = ARG_PTR_TO_CTX,
3008 .arg2_type = ARG_ANYTHING,
3009 .arg3_type = ARG_ANYTHING,
3012 static inline int __bpf_skb_change_head(struct sk_buff *skb, u32 head_room,
3015 u32 max_len = __bpf_skb_max_len(skb);
3016 u32 new_len = skb->len + head_room;
3019 if (unlikely(flags || (!skb_is_gso(skb) && new_len > max_len) ||
3020 new_len < skb->len))
3023 ret = skb_cow(skb, head_room);
3025 /* Idea for this helper is that we currently only
3026 * allow to expand on mac header. This means that
3027 * skb->protocol network header, etc, stay as is.
3028 * Compared to bpf_skb_change_tail(), we're more
3029 * flexible due to not needing to linearize or
3030 * reset GSO. Intention for this helper is to be
3031 * used by an L3 skb that needs to push mac header
3032 * for redirection into L2 device.
3034 __skb_push(skb, head_room);
3035 memset(skb->data, 0, head_room);
3036 skb_reset_mac_header(skb);
3042 BPF_CALL_3(bpf_skb_change_head, struct sk_buff *, skb, u32, head_room,
3045 int ret = __bpf_skb_change_head(skb, head_room, flags);
3047 bpf_compute_data_pointers(skb);
3051 static const struct bpf_func_proto bpf_skb_change_head_proto = {
3052 .func = bpf_skb_change_head,
3054 .ret_type = RET_INTEGER,
3055 .arg1_type = ARG_PTR_TO_CTX,
3056 .arg2_type = ARG_ANYTHING,
3057 .arg3_type = ARG_ANYTHING,
3060 BPF_CALL_3(sk_skb_change_head, struct sk_buff *, skb, u32, head_room,
3063 int ret = __bpf_skb_change_head(skb, head_room, flags);
3065 bpf_compute_data_end_sk_skb(skb);
3069 static const struct bpf_func_proto sk_skb_change_head_proto = {
3070 .func = sk_skb_change_head,
3072 .ret_type = RET_INTEGER,
3073 .arg1_type = ARG_PTR_TO_CTX,
3074 .arg2_type = ARG_ANYTHING,
3075 .arg3_type = ARG_ANYTHING,
3077 static unsigned long xdp_get_metalen(const struct xdp_buff *xdp)
3079 return xdp_data_meta_unsupported(xdp) ? 0 :
3080 xdp->data - xdp->data_meta;
3083 BPF_CALL_2(bpf_xdp_adjust_head, struct xdp_buff *, xdp, int, offset)
3085 void *xdp_frame_end = xdp->data_hard_start + sizeof(struct xdp_frame);
3086 unsigned long metalen = xdp_get_metalen(xdp);
3087 void *data_start = xdp_frame_end + metalen;
3088 void *data = xdp->data + offset;
3090 if (unlikely(data < data_start ||
3091 data > xdp->data_end - ETH_HLEN))
3095 memmove(xdp->data_meta + offset,
3096 xdp->data_meta, metalen);
3097 xdp->data_meta += offset;
3103 static const struct bpf_func_proto bpf_xdp_adjust_head_proto = {
3104 .func = bpf_xdp_adjust_head,
3106 .ret_type = RET_INTEGER,
3107 .arg1_type = ARG_PTR_TO_CTX,
3108 .arg2_type = ARG_ANYTHING,
3111 BPF_CALL_2(bpf_xdp_adjust_tail, struct xdp_buff *, xdp, int, offset)
3113 void *data_end = xdp->data_end + offset;
3115 /* only shrinking is allowed for now. */
3116 if (unlikely(offset >= 0))
3119 if (unlikely(data_end < xdp->data + ETH_HLEN))
3122 xdp->data_end = data_end;
3127 static const struct bpf_func_proto bpf_xdp_adjust_tail_proto = {
3128 .func = bpf_xdp_adjust_tail,
3130 .ret_type = RET_INTEGER,
3131 .arg1_type = ARG_PTR_TO_CTX,
3132 .arg2_type = ARG_ANYTHING,
3135 BPF_CALL_2(bpf_xdp_adjust_meta, struct xdp_buff *, xdp, int, offset)
3137 void *xdp_frame_end = xdp->data_hard_start + sizeof(struct xdp_frame);
3138 void *meta = xdp->data_meta + offset;
3139 unsigned long metalen = xdp->data - meta;
3141 if (xdp_data_meta_unsupported(xdp))
3143 if (unlikely(meta < xdp_frame_end ||
3146 if (unlikely((metalen & (sizeof(__u32) - 1)) ||
3150 xdp->data_meta = meta;
3155 static const struct bpf_func_proto bpf_xdp_adjust_meta_proto = {
3156 .func = bpf_xdp_adjust_meta,
3158 .ret_type = RET_INTEGER,
3159 .arg1_type = ARG_PTR_TO_CTX,
3160 .arg2_type = ARG_ANYTHING,
3163 static int __bpf_tx_xdp(struct net_device *dev,
3164 struct bpf_map *map,
3165 struct xdp_buff *xdp,
3168 struct xdp_frame *xdpf;
3171 if (!dev->netdev_ops->ndo_xdp_xmit) {
3175 err = xdp_ok_fwd_dev(dev, xdp->data_end - xdp->data);
3179 xdpf = convert_to_xdp_frame(xdp);
3180 if (unlikely(!xdpf))
3183 sent = dev->netdev_ops->ndo_xdp_xmit(dev, 1, &xdpf, XDP_XMIT_FLUSH);
3190 xdp_do_redirect_slow(struct net_device *dev, struct xdp_buff *xdp,
3191 struct bpf_prog *xdp_prog, struct bpf_redirect_info *ri)
3193 struct net_device *fwd;
3194 u32 index = ri->ifindex;
3197 fwd = dev_get_by_index_rcu(dev_net(dev), index);
3199 if (unlikely(!fwd)) {
3204 err = __bpf_tx_xdp(fwd, NULL, xdp, 0);
3208 _trace_xdp_redirect(dev, xdp_prog, index);
3211 _trace_xdp_redirect_err(dev, xdp_prog, index, err);
3215 static int __bpf_tx_xdp_map(struct net_device *dev_rx, void *fwd,
3216 struct bpf_map *map,
3217 struct xdp_buff *xdp,
3222 switch (map->map_type) {
3223 case BPF_MAP_TYPE_DEVMAP: {
3224 struct bpf_dtab_netdev *dst = fwd;
3226 err = dev_map_enqueue(dst, xdp, dev_rx);
3229 __dev_map_insert_ctx(map, index);
3232 case BPF_MAP_TYPE_CPUMAP: {
3233 struct bpf_cpu_map_entry *rcpu = fwd;
3235 err = cpu_map_enqueue(rcpu, xdp, dev_rx);
3238 __cpu_map_insert_ctx(map, index);
3241 case BPF_MAP_TYPE_XSKMAP: {
3242 struct xdp_sock *xs = fwd;
3244 err = __xsk_map_redirect(map, xdp, xs);
3253 void xdp_do_flush_map(void)
3255 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
3256 struct bpf_map *map = ri->map_to_flush;
3258 ri->map_to_flush = NULL;
3260 switch (map->map_type) {
3261 case BPF_MAP_TYPE_DEVMAP:
3262 __dev_map_flush(map);
3264 case BPF_MAP_TYPE_CPUMAP:
3265 __cpu_map_flush(map);
3267 case BPF_MAP_TYPE_XSKMAP:
3268 __xsk_map_flush(map);
3275 EXPORT_SYMBOL_GPL(xdp_do_flush_map);
3277 static inline void *__xdp_map_lookup_elem(struct bpf_map *map, u32 index)
3279 switch (map->map_type) {
3280 case BPF_MAP_TYPE_DEVMAP:
3281 return __dev_map_lookup_elem(map, index);
3282 case BPF_MAP_TYPE_CPUMAP:
3283 return __cpu_map_lookup_elem(map, index);
3284 case BPF_MAP_TYPE_XSKMAP:
3285 return __xsk_map_lookup_elem(map, index);
3291 void bpf_clear_redirect_map(struct bpf_map *map)
3293 struct bpf_redirect_info *ri;
3296 for_each_possible_cpu(cpu) {
3297 ri = per_cpu_ptr(&bpf_redirect_info, cpu);
3298 /* Avoid polluting remote cacheline due to writes if
3299 * not needed. Once we pass this test, we need the
3300 * cmpxchg() to make sure it hasn't been changed in
3301 * the meantime by remote CPU.
3303 if (unlikely(READ_ONCE(ri->map) == map))
3304 cmpxchg(&ri->map, map, NULL);
3308 static int xdp_do_redirect_map(struct net_device *dev, struct xdp_buff *xdp,
3309 struct bpf_prog *xdp_prog, struct bpf_map *map,
3310 struct bpf_redirect_info *ri)
3312 u32 index = ri->ifindex;
3317 WRITE_ONCE(ri->map, NULL);
3319 fwd = __xdp_map_lookup_elem(map, index);
3320 if (unlikely(!fwd)) {
3324 if (ri->map_to_flush && unlikely(ri->map_to_flush != map))
3327 err = __bpf_tx_xdp_map(dev, fwd, map, xdp, index);
3331 ri->map_to_flush = map;
3332 _trace_xdp_redirect_map(dev, xdp_prog, fwd, map, index);
3335 _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map, index, err);
3339 int xdp_do_redirect(struct net_device *dev, struct xdp_buff *xdp,
3340 struct bpf_prog *xdp_prog)
3342 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
3343 struct bpf_map *map = READ_ONCE(ri->map);
3346 return xdp_do_redirect_map(dev, xdp, xdp_prog, map, ri);
3348 return xdp_do_redirect_slow(dev, xdp, xdp_prog, ri);
3350 EXPORT_SYMBOL_GPL(xdp_do_redirect);
3352 static int xdp_do_generic_redirect_map(struct net_device *dev,
3353 struct sk_buff *skb,
3354 struct xdp_buff *xdp,
3355 struct bpf_prog *xdp_prog,
3356 struct bpf_map *map)
3358 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
3359 u32 index = ri->ifindex;
3364 WRITE_ONCE(ri->map, NULL);
3366 fwd = __xdp_map_lookup_elem(map, index);
3367 if (unlikely(!fwd)) {
3372 if (map->map_type == BPF_MAP_TYPE_DEVMAP) {
3373 struct bpf_dtab_netdev *dst = fwd;
3375 err = dev_map_generic_redirect(dst, skb, xdp_prog);
3378 } else if (map->map_type == BPF_MAP_TYPE_XSKMAP) {
3379 struct xdp_sock *xs = fwd;
3381 err = xsk_generic_rcv(xs, xdp);
3386 /* TODO: Handle BPF_MAP_TYPE_CPUMAP */
3391 _trace_xdp_redirect_map(dev, xdp_prog, fwd, map, index);
3394 _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map, index, err);
3398 int xdp_do_generic_redirect(struct net_device *dev, struct sk_buff *skb,
3399 struct xdp_buff *xdp, struct bpf_prog *xdp_prog)
3401 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
3402 struct bpf_map *map = READ_ONCE(ri->map);
3403 u32 index = ri->ifindex;
3404 struct net_device *fwd;
3408 return xdp_do_generic_redirect_map(dev, skb, xdp, xdp_prog,
3411 fwd = dev_get_by_index_rcu(dev_net(dev), index);
3412 if (unlikely(!fwd)) {
3417 err = xdp_ok_fwd_dev(fwd, skb->len);
3422 _trace_xdp_redirect(dev, xdp_prog, index);
3423 generic_xdp_tx(skb, xdp_prog);
3426 _trace_xdp_redirect_err(dev, xdp_prog, index, err);
3429 EXPORT_SYMBOL_GPL(xdp_do_generic_redirect);
3431 BPF_CALL_2(bpf_xdp_redirect, u32, ifindex, u64, flags)
3433 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
3435 if (unlikely(flags))
3438 ri->ifindex = ifindex;
3440 WRITE_ONCE(ri->map, NULL);
3442 return XDP_REDIRECT;
3445 static const struct bpf_func_proto bpf_xdp_redirect_proto = {
3446 .func = bpf_xdp_redirect,
3448 .ret_type = RET_INTEGER,
3449 .arg1_type = ARG_ANYTHING,
3450 .arg2_type = ARG_ANYTHING,
3453 BPF_CALL_3(bpf_xdp_redirect_map, struct bpf_map *, map, u32, ifindex,
3456 struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info);
3458 if (unlikely(flags))
3461 ri->ifindex = ifindex;
3463 WRITE_ONCE(ri->map, map);
3465 return XDP_REDIRECT;
3468 static const struct bpf_func_proto bpf_xdp_redirect_map_proto = {
3469 .func = bpf_xdp_redirect_map,
3471 .ret_type = RET_INTEGER,
3472 .arg1_type = ARG_CONST_MAP_PTR,
3473 .arg2_type = ARG_ANYTHING,
3474 .arg3_type = ARG_ANYTHING,
3477 static unsigned long bpf_skb_copy(void *dst_buff, const void *skb,
3478 unsigned long off, unsigned long len)
3480 void *ptr = skb_header_pointer(skb, off, len, dst_buff);
3484 if (ptr != dst_buff)
3485 memcpy(dst_buff, ptr, len);
3490 BPF_CALL_5(bpf_skb_event_output, struct sk_buff *, skb, struct bpf_map *, map,
3491 u64, flags, void *, meta, u64, meta_size)
3493 u64 skb_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
3495 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
3497 if (unlikely(skb_size > skb->len))
3500 return bpf_event_output(map, flags, meta, meta_size, skb, skb_size,
3504 static const struct bpf_func_proto bpf_skb_event_output_proto = {
3505 .func = bpf_skb_event_output,
3507 .ret_type = RET_INTEGER,
3508 .arg1_type = ARG_PTR_TO_CTX,
3509 .arg2_type = ARG_CONST_MAP_PTR,
3510 .arg3_type = ARG_ANYTHING,
3511 .arg4_type = ARG_PTR_TO_MEM,
3512 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
3515 static unsigned short bpf_tunnel_key_af(u64 flags)
3517 return flags & BPF_F_TUNINFO_IPV6 ? AF_INET6 : AF_INET;
3520 BPF_CALL_4(bpf_skb_get_tunnel_key, struct sk_buff *, skb, struct bpf_tunnel_key *, to,
3521 u32, size, u64, flags)
3523 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
3524 u8 compat[sizeof(struct bpf_tunnel_key)];
3528 if (unlikely(!info || (flags & ~(BPF_F_TUNINFO_IPV6)))) {
3532 if (ip_tunnel_info_af(info) != bpf_tunnel_key_af(flags)) {
3536 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
3539 case offsetof(struct bpf_tunnel_key, tunnel_label):
3540 case offsetof(struct bpf_tunnel_key, tunnel_ext):
3542 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
3543 /* Fixup deprecated structure layouts here, so we have
3544 * a common path later on.
3546 if (ip_tunnel_info_af(info) != AF_INET)
3549 to = (struct bpf_tunnel_key *)compat;
3556 to->tunnel_id = be64_to_cpu(info->key.tun_id);
3557 to->tunnel_tos = info->key.tos;
3558 to->tunnel_ttl = info->key.ttl;
3561 if (flags & BPF_F_TUNINFO_IPV6) {
3562 memcpy(to->remote_ipv6, &info->key.u.ipv6.src,
3563 sizeof(to->remote_ipv6));
3564 to->tunnel_label = be32_to_cpu(info->key.label);
3566 to->remote_ipv4 = be32_to_cpu(info->key.u.ipv4.src);
3567 memset(&to->remote_ipv6[1], 0, sizeof(__u32) * 3);
3568 to->tunnel_label = 0;
3571 if (unlikely(size != sizeof(struct bpf_tunnel_key)))
3572 memcpy(to_orig, to, size);
3576 memset(to_orig, 0, size);
3580 static const struct bpf_func_proto bpf_skb_get_tunnel_key_proto = {
3581 .func = bpf_skb_get_tunnel_key,
3583 .ret_type = RET_INTEGER,
3584 .arg1_type = ARG_PTR_TO_CTX,
3585 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
3586 .arg3_type = ARG_CONST_SIZE,
3587 .arg4_type = ARG_ANYTHING,
3590 BPF_CALL_3(bpf_skb_get_tunnel_opt, struct sk_buff *, skb, u8 *, to, u32, size)
3592 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
3595 if (unlikely(!info ||
3596 !(info->key.tun_flags & TUNNEL_OPTIONS_PRESENT))) {
3600 if (unlikely(size < info->options_len)) {
3605 ip_tunnel_info_opts_get(to, info);
3606 if (size > info->options_len)
3607 memset(to + info->options_len, 0, size - info->options_len);
3609 return info->options_len;
3611 memset(to, 0, size);
3615 static const struct bpf_func_proto bpf_skb_get_tunnel_opt_proto = {
3616 .func = bpf_skb_get_tunnel_opt,
3618 .ret_type = RET_INTEGER,
3619 .arg1_type = ARG_PTR_TO_CTX,
3620 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
3621 .arg3_type = ARG_CONST_SIZE,
3624 static struct metadata_dst __percpu *md_dst;
3626 BPF_CALL_4(bpf_skb_set_tunnel_key, struct sk_buff *, skb,
3627 const struct bpf_tunnel_key *, from, u32, size, u64, flags)
3629 struct metadata_dst *md = this_cpu_ptr(md_dst);
3630 u8 compat[sizeof(struct bpf_tunnel_key)];
3631 struct ip_tunnel_info *info;
3633 if (unlikely(flags & ~(BPF_F_TUNINFO_IPV6 | BPF_F_ZERO_CSUM_TX |
3634 BPF_F_DONT_FRAGMENT | BPF_F_SEQ_NUMBER)))
3636 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
3638 case offsetof(struct bpf_tunnel_key, tunnel_label):
3639 case offsetof(struct bpf_tunnel_key, tunnel_ext):
3640 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
3641 /* Fixup deprecated structure layouts here, so we have
3642 * a common path later on.
3644 memcpy(compat, from, size);
3645 memset(compat + size, 0, sizeof(compat) - size);
3646 from = (const struct bpf_tunnel_key *) compat;
3652 if (unlikely((!(flags & BPF_F_TUNINFO_IPV6) && from->tunnel_label) ||
3657 dst_hold((struct dst_entry *) md);
3658 skb_dst_set(skb, (struct dst_entry *) md);
3660 info = &md->u.tun_info;
3661 memset(info, 0, sizeof(*info));
3662 info->mode = IP_TUNNEL_INFO_TX;
3664 info->key.tun_flags = TUNNEL_KEY | TUNNEL_CSUM | TUNNEL_NOCACHE;
3665 if (flags & BPF_F_DONT_FRAGMENT)
3666 info->key.tun_flags |= TUNNEL_DONT_FRAGMENT;
3667 if (flags & BPF_F_ZERO_CSUM_TX)
3668 info->key.tun_flags &= ~TUNNEL_CSUM;
3669 if (flags & BPF_F_SEQ_NUMBER)
3670 info->key.tun_flags |= TUNNEL_SEQ;
3672 info->key.tun_id = cpu_to_be64(from->tunnel_id);
3673 info->key.tos = from->tunnel_tos;
3674 info->key.ttl = from->tunnel_ttl;
3676 if (flags & BPF_F_TUNINFO_IPV6) {
3677 info->mode |= IP_TUNNEL_INFO_IPV6;
3678 memcpy(&info->key.u.ipv6.dst, from->remote_ipv6,
3679 sizeof(from->remote_ipv6));
3680 info->key.label = cpu_to_be32(from->tunnel_label) &
3681 IPV6_FLOWLABEL_MASK;
3683 info->key.u.ipv4.dst = cpu_to_be32(from->remote_ipv4);
3689 static const struct bpf_func_proto bpf_skb_set_tunnel_key_proto = {
3690 .func = bpf_skb_set_tunnel_key,
3692 .ret_type = RET_INTEGER,
3693 .arg1_type = ARG_PTR_TO_CTX,
3694 .arg2_type = ARG_PTR_TO_MEM,
3695 .arg3_type = ARG_CONST_SIZE,
3696 .arg4_type = ARG_ANYTHING,
3699 BPF_CALL_3(bpf_skb_set_tunnel_opt, struct sk_buff *, skb,
3700 const u8 *, from, u32, size)
3702 struct ip_tunnel_info *info = skb_tunnel_info(skb);
3703 const struct metadata_dst *md = this_cpu_ptr(md_dst);
3705 if (unlikely(info != &md->u.tun_info || (size & (sizeof(u32) - 1))))
3707 if (unlikely(size > IP_TUNNEL_OPTS_MAX))
3710 ip_tunnel_info_opts_set(info, from, size, TUNNEL_OPTIONS_PRESENT);
3715 static const struct bpf_func_proto bpf_skb_set_tunnel_opt_proto = {
3716 .func = bpf_skb_set_tunnel_opt,
3718 .ret_type = RET_INTEGER,
3719 .arg1_type = ARG_PTR_TO_CTX,
3720 .arg2_type = ARG_PTR_TO_MEM,
3721 .arg3_type = ARG_CONST_SIZE,
3724 static const struct bpf_func_proto *
3725 bpf_get_skb_set_tunnel_proto(enum bpf_func_id which)
3728 struct metadata_dst __percpu *tmp;
3730 tmp = metadata_dst_alloc_percpu(IP_TUNNEL_OPTS_MAX,
3735 if (cmpxchg(&md_dst, NULL, tmp))
3736 metadata_dst_free_percpu(tmp);
3740 case BPF_FUNC_skb_set_tunnel_key:
3741 return &bpf_skb_set_tunnel_key_proto;
3742 case BPF_FUNC_skb_set_tunnel_opt:
3743 return &bpf_skb_set_tunnel_opt_proto;
3749 BPF_CALL_3(bpf_skb_under_cgroup, struct sk_buff *, skb, struct bpf_map *, map,
3752 struct bpf_array *array = container_of(map, struct bpf_array, map);
3753 struct cgroup *cgrp;
3756 sk = skb_to_full_sk(skb);
3757 if (!sk || !sk_fullsock(sk))
3759 if (unlikely(idx >= array->map.max_entries))
3762 cgrp = READ_ONCE(array->ptrs[idx]);
3763 if (unlikely(!cgrp))
3766 return sk_under_cgroup_hierarchy(sk, cgrp);
3769 static const struct bpf_func_proto bpf_skb_under_cgroup_proto = {
3770 .func = bpf_skb_under_cgroup,
3772 .ret_type = RET_INTEGER,
3773 .arg1_type = ARG_PTR_TO_CTX,
3774 .arg2_type = ARG_CONST_MAP_PTR,
3775 .arg3_type = ARG_ANYTHING,
3778 #ifdef CONFIG_SOCK_CGROUP_DATA
3779 BPF_CALL_1(bpf_skb_cgroup_id, const struct sk_buff *, skb)
3781 struct sock *sk = skb_to_full_sk(skb);
3782 struct cgroup *cgrp;
3784 if (!sk || !sk_fullsock(sk))
3787 cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
3788 return cgrp->kn->id.id;
3791 static const struct bpf_func_proto bpf_skb_cgroup_id_proto = {
3792 .func = bpf_skb_cgroup_id,
3794 .ret_type = RET_INTEGER,
3795 .arg1_type = ARG_PTR_TO_CTX,
3798 BPF_CALL_2(bpf_skb_ancestor_cgroup_id, const struct sk_buff *, skb, int,
3801 struct sock *sk = skb_to_full_sk(skb);
3802 struct cgroup *ancestor;
3803 struct cgroup *cgrp;
3805 if (!sk || !sk_fullsock(sk))
3808 cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
3809 ancestor = cgroup_ancestor(cgrp, ancestor_level);
3813 return ancestor->kn->id.id;
3816 static const struct bpf_func_proto bpf_skb_ancestor_cgroup_id_proto = {
3817 .func = bpf_skb_ancestor_cgroup_id,
3819 .ret_type = RET_INTEGER,
3820 .arg1_type = ARG_PTR_TO_CTX,
3821 .arg2_type = ARG_ANYTHING,
3825 static unsigned long bpf_xdp_copy(void *dst_buff, const void *src_buff,
3826 unsigned long off, unsigned long len)
3828 memcpy(dst_buff, src_buff + off, len);
3832 BPF_CALL_5(bpf_xdp_event_output, struct xdp_buff *, xdp, struct bpf_map *, map,
3833 u64, flags, void *, meta, u64, meta_size)
3835 u64 xdp_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
3837 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
3839 if (unlikely(xdp_size > (unsigned long)(xdp->data_end - xdp->data)))
3842 return bpf_event_output(map, flags, meta, meta_size, xdp->data,
3843 xdp_size, bpf_xdp_copy);
3846 static const struct bpf_func_proto bpf_xdp_event_output_proto = {
3847 .func = bpf_xdp_event_output,
3849 .ret_type = RET_INTEGER,
3850 .arg1_type = ARG_PTR_TO_CTX,
3851 .arg2_type = ARG_CONST_MAP_PTR,
3852 .arg3_type = ARG_ANYTHING,
3853 .arg4_type = ARG_PTR_TO_MEM,
3854 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
3857 BPF_CALL_1(bpf_get_socket_cookie, struct sk_buff *, skb)
3859 return skb->sk ? sock_gen_cookie(skb->sk) : 0;
3862 static const struct bpf_func_proto bpf_get_socket_cookie_proto = {
3863 .func = bpf_get_socket_cookie,
3865 .ret_type = RET_INTEGER,
3866 .arg1_type = ARG_PTR_TO_CTX,
3869 BPF_CALL_1(bpf_get_socket_cookie_sock_addr, struct bpf_sock_addr_kern *, ctx)
3871 return sock_gen_cookie(ctx->sk);
3874 static const struct bpf_func_proto bpf_get_socket_cookie_sock_addr_proto = {
3875 .func = bpf_get_socket_cookie_sock_addr,
3877 .ret_type = RET_INTEGER,
3878 .arg1_type = ARG_PTR_TO_CTX,
3881 BPF_CALL_1(bpf_get_socket_cookie_sock_ops, struct bpf_sock_ops_kern *, ctx)
3883 return sock_gen_cookie(ctx->sk);
3886 static const struct bpf_func_proto bpf_get_socket_cookie_sock_ops_proto = {
3887 .func = bpf_get_socket_cookie_sock_ops,
3889 .ret_type = RET_INTEGER,
3890 .arg1_type = ARG_PTR_TO_CTX,
3893 BPF_CALL_1(bpf_get_socket_uid, struct sk_buff *, skb)
3895 struct sock *sk = sk_to_full_sk(skb->sk);
3898 if (!sk || !sk_fullsock(sk))
3900 kuid = sock_net_uid(sock_net(sk), sk);
3901 return from_kuid_munged(sock_net(sk)->user_ns, kuid);
3904 static const struct bpf_func_proto bpf_get_socket_uid_proto = {
3905 .func = bpf_get_socket_uid,
3907 .ret_type = RET_INTEGER,
3908 .arg1_type = ARG_PTR_TO_CTX,
3911 BPF_CALL_5(bpf_setsockopt, struct bpf_sock_ops_kern *, bpf_sock,
3912 int, level, int, optname, char *, optval, int, optlen)
3914 struct sock *sk = bpf_sock->sk;
3918 if (!sk_fullsock(sk))
3921 if (level == SOL_SOCKET) {
3922 if (optlen != sizeof(int))
3924 val = *((int *)optval);
3926 /* Only some socketops are supported */
3929 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
3930 sk->sk_rcvbuf = max_t(int, val * 2, SOCK_MIN_RCVBUF);
3933 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
3934 sk->sk_sndbuf = max_t(int, val * 2, SOCK_MIN_SNDBUF);
3936 case SO_MAX_PACING_RATE: /* 32bit version */
3937 sk->sk_max_pacing_rate = (val == ~0U) ? ~0UL : val;
3938 sk->sk_pacing_rate = min(sk->sk_pacing_rate,
3939 sk->sk_max_pacing_rate);
3942 sk->sk_priority = val;
3947 sk->sk_rcvlowat = val ? : 1;
3956 } else if (level == SOL_IP) {
3957 if (optlen != sizeof(int) || sk->sk_family != AF_INET)
3960 val = *((int *)optval);
3961 /* Only some options are supported */
3964 if (val < -1 || val > 0xff) {
3967 struct inet_sock *inet = inet_sk(sk);
3977 #if IS_ENABLED(CONFIG_IPV6)
3978 } else if (level == SOL_IPV6) {
3979 if (optlen != sizeof(int) || sk->sk_family != AF_INET6)
3982 val = *((int *)optval);
3983 /* Only some options are supported */
3986 if (val < -1 || val > 0xff) {
3989 struct ipv6_pinfo *np = inet6_sk(sk);
4000 } else if (level == SOL_TCP &&
4001 sk->sk_prot->setsockopt == tcp_setsockopt) {
4002 if (optname == TCP_CONGESTION) {
4003 char name[TCP_CA_NAME_MAX];
4004 bool reinit = bpf_sock->op > BPF_SOCK_OPS_NEEDS_ECN;
4006 strncpy(name, optval, min_t(long, optlen,
4007 TCP_CA_NAME_MAX-1));
4008 name[TCP_CA_NAME_MAX-1] = 0;
4009 ret = tcp_set_congestion_control(sk, name, false,
4012 struct tcp_sock *tp = tcp_sk(sk);
4014 if (optlen != sizeof(int))
4017 val = *((int *)optval);
4018 /* Only some options are supported */
4021 if (val <= 0 || tp->data_segs_out > 0)
4026 case TCP_BPF_SNDCWND_CLAMP:
4030 tp->snd_cwnd_clamp = val;
4031 tp->snd_ssthresh = val;
4035 if (val < 0 || val > 1)
4051 static const struct bpf_func_proto bpf_setsockopt_proto = {
4052 .func = bpf_setsockopt,
4054 .ret_type = RET_INTEGER,
4055 .arg1_type = ARG_PTR_TO_CTX,
4056 .arg2_type = ARG_ANYTHING,
4057 .arg3_type = ARG_ANYTHING,
4058 .arg4_type = ARG_PTR_TO_MEM,
4059 .arg5_type = ARG_CONST_SIZE,
4062 BPF_CALL_5(bpf_getsockopt, struct bpf_sock_ops_kern *, bpf_sock,
4063 int, level, int, optname, char *, optval, int, optlen)
4065 struct sock *sk = bpf_sock->sk;
4067 if (!sk_fullsock(sk))
4070 if (level == SOL_TCP && sk->sk_prot->getsockopt == tcp_getsockopt) {
4071 struct inet_connection_sock *icsk;
4072 struct tcp_sock *tp;
4075 case TCP_CONGESTION:
4076 icsk = inet_csk(sk);
4078 if (!icsk->icsk_ca_ops || optlen <= 1)
4080 strncpy(optval, icsk->icsk_ca_ops->name, optlen);
4081 optval[optlen - 1] = 0;
4086 if (optlen <= 0 || !tp->saved_syn ||
4087 optlen > tp->saved_syn[0])
4089 memcpy(optval, tp->saved_syn + 1, optlen);
4094 } else if (level == SOL_IP) {
4095 struct inet_sock *inet = inet_sk(sk);
4097 if (optlen != sizeof(int) || sk->sk_family != AF_INET)
4100 /* Only some options are supported */
4103 *((int *)optval) = (int)inet->tos;
4108 #if IS_ENABLED(CONFIG_IPV6)
4109 } else if (level == SOL_IPV6) {
4110 struct ipv6_pinfo *np = inet6_sk(sk);
4112 if (optlen != sizeof(int) || sk->sk_family != AF_INET6)
4115 /* Only some options are supported */
4118 *((int *)optval) = (int)np->tclass;
4130 memset(optval, 0, optlen);
4134 static const struct bpf_func_proto bpf_getsockopt_proto = {
4135 .func = bpf_getsockopt,
4137 .ret_type = RET_INTEGER,
4138 .arg1_type = ARG_PTR_TO_CTX,
4139 .arg2_type = ARG_ANYTHING,
4140 .arg3_type = ARG_ANYTHING,
4141 .arg4_type = ARG_PTR_TO_UNINIT_MEM,
4142 .arg5_type = ARG_CONST_SIZE,
4145 BPF_CALL_2(bpf_sock_ops_cb_flags_set, struct bpf_sock_ops_kern *, bpf_sock,
4148 struct sock *sk = bpf_sock->sk;
4149 int val = argval & BPF_SOCK_OPS_ALL_CB_FLAGS;
4151 if (!IS_ENABLED(CONFIG_INET) || !sk_fullsock(sk))
4155 tcp_sk(sk)->bpf_sock_ops_cb_flags = val;
4157 return argval & (~BPF_SOCK_OPS_ALL_CB_FLAGS);
4160 static const struct bpf_func_proto bpf_sock_ops_cb_flags_set_proto = {
4161 .func = bpf_sock_ops_cb_flags_set,
4163 .ret_type = RET_INTEGER,
4164 .arg1_type = ARG_PTR_TO_CTX,
4165 .arg2_type = ARG_ANYTHING,
4168 const struct ipv6_bpf_stub *ipv6_bpf_stub __read_mostly;
4169 EXPORT_SYMBOL_GPL(ipv6_bpf_stub);
4171 BPF_CALL_3(bpf_bind, struct bpf_sock_addr_kern *, ctx, struct sockaddr *, addr,
4175 struct sock *sk = ctx->sk;
4178 /* Binding to port can be expensive so it's prohibited in the helper.
4179 * Only binding to IP is supported.
4182 if (addr->sa_family == AF_INET) {
4183 if (addr_len < sizeof(struct sockaddr_in))
4185 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
4187 return __inet_bind(sk, addr, addr_len, true, false);
4188 #if IS_ENABLED(CONFIG_IPV6)
4189 } else if (addr->sa_family == AF_INET6) {
4190 if (addr_len < SIN6_LEN_RFC2133)
4192 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
4194 /* ipv6_bpf_stub cannot be NULL, since it's called from
4195 * bpf_cgroup_inet6_connect hook and ipv6 is already loaded
4197 return ipv6_bpf_stub->inet6_bind(sk, addr, addr_len, true, false);
4198 #endif /* CONFIG_IPV6 */
4200 #endif /* CONFIG_INET */
4202 return -EAFNOSUPPORT;
4205 static const struct bpf_func_proto bpf_bind_proto = {
4208 .ret_type = RET_INTEGER,
4209 .arg1_type = ARG_PTR_TO_CTX,
4210 .arg2_type = ARG_PTR_TO_MEM,
4211 .arg3_type = ARG_CONST_SIZE,
4215 BPF_CALL_5(bpf_skb_get_xfrm_state, struct sk_buff *, skb, u32, index,
4216 struct bpf_xfrm_state *, to, u32, size, u64, flags)
4218 const struct sec_path *sp = skb_sec_path(skb);
4219 const struct xfrm_state *x;
4221 if (!sp || unlikely(index >= sp->len || flags))
4224 x = sp->xvec[index];
4226 if (unlikely(size != sizeof(struct bpf_xfrm_state)))
4229 to->reqid = x->props.reqid;
4230 to->spi = x->id.spi;
4231 to->family = x->props.family;
4234 if (to->family == AF_INET6) {
4235 memcpy(to->remote_ipv6, x->props.saddr.a6,
4236 sizeof(to->remote_ipv6));
4238 to->remote_ipv4 = x->props.saddr.a4;
4239 memset(&to->remote_ipv6[1], 0, sizeof(__u32) * 3);
4244 memset(to, 0, size);
4248 static const struct bpf_func_proto bpf_skb_get_xfrm_state_proto = {
4249 .func = bpf_skb_get_xfrm_state,
4251 .ret_type = RET_INTEGER,
4252 .arg1_type = ARG_PTR_TO_CTX,
4253 .arg2_type = ARG_ANYTHING,
4254 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
4255 .arg4_type = ARG_CONST_SIZE,
4256 .arg5_type = ARG_ANYTHING,
4260 #if IS_ENABLED(CONFIG_INET) || IS_ENABLED(CONFIG_IPV6)
4261 static int bpf_fib_set_fwd_params(struct bpf_fib_lookup *params,
4262 const struct neighbour *neigh,
4263 const struct net_device *dev)
4265 memcpy(params->dmac, neigh->ha, ETH_ALEN);
4266 memcpy(params->smac, dev->dev_addr, ETH_ALEN);
4267 params->h_vlan_TCI = 0;
4268 params->h_vlan_proto = 0;
4269 params->ifindex = dev->ifindex;
4275 #if IS_ENABLED(CONFIG_INET)
4276 static int bpf_ipv4_fib_lookup(struct net *net, struct bpf_fib_lookup *params,
4277 u32 flags, bool check_mtu)
4279 struct in_device *in_dev;
4280 struct neighbour *neigh;
4281 struct net_device *dev;
4282 struct fib_result res;
4288 dev = dev_get_by_index_rcu(net, params->ifindex);
4292 /* verify forwarding is enabled on this interface */
4293 in_dev = __in_dev_get_rcu(dev);
4294 if (unlikely(!in_dev || !IN_DEV_FORWARD(in_dev)))
4295 return BPF_FIB_LKUP_RET_FWD_DISABLED;
4297 if (flags & BPF_FIB_LOOKUP_OUTPUT) {
4299 fl4.flowi4_oif = params->ifindex;
4301 fl4.flowi4_iif = params->ifindex;
4304 fl4.flowi4_tos = params->tos & IPTOS_RT_MASK;
4305 fl4.flowi4_scope = RT_SCOPE_UNIVERSE;
4306 fl4.flowi4_flags = 0;
4308 fl4.flowi4_proto = params->l4_protocol;
4309 fl4.daddr = params->ipv4_dst;
4310 fl4.saddr = params->ipv4_src;
4311 fl4.fl4_sport = params->sport;
4312 fl4.fl4_dport = params->dport;
4314 if (flags & BPF_FIB_LOOKUP_DIRECT) {
4315 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
4316 struct fib_table *tb;
4318 tb = fib_get_table(net, tbid);
4320 return BPF_FIB_LKUP_RET_NOT_FWDED;
4322 err = fib_table_lookup(tb, &fl4, &res, FIB_LOOKUP_NOREF);
4324 fl4.flowi4_mark = 0;
4325 fl4.flowi4_secid = 0;
4326 fl4.flowi4_tun_key.tun_id = 0;
4327 fl4.flowi4_uid = sock_net_uid(net, NULL);
4329 err = fib_lookup(net, &fl4, &res, FIB_LOOKUP_NOREF);
4333 /* map fib lookup errors to RTN_ type */
4335 return BPF_FIB_LKUP_RET_BLACKHOLE;
4336 if (err == -EHOSTUNREACH)
4337 return BPF_FIB_LKUP_RET_UNREACHABLE;
4339 return BPF_FIB_LKUP_RET_PROHIBIT;
4341 return BPF_FIB_LKUP_RET_NOT_FWDED;
4344 if (res.type != RTN_UNICAST)
4345 return BPF_FIB_LKUP_RET_NOT_FWDED;
4347 if (res.fi->fib_nhs > 1)
4348 fib_select_path(net, &res, &fl4, NULL);
4351 mtu = ip_mtu_from_fib_result(&res, params->ipv4_dst);
4352 if (params->tot_len > mtu)
4353 return BPF_FIB_LKUP_RET_FRAG_NEEDED;
4356 nh = &res.fi->fib_nh[res.nh_sel];
4358 /* do not handle lwt encaps right now */
4359 if (nh->nh_lwtstate)
4360 return BPF_FIB_LKUP_RET_UNSUPP_LWT;
4364 params->ipv4_dst = nh->nh_gw;
4366 params->rt_metric = res.fi->fib_priority;
4368 /* xdp and cls_bpf programs are run in RCU-bh so
4369 * rcu_read_lock_bh is not needed here
4371 neigh = __ipv4_neigh_lookup_noref(dev, (__force u32)params->ipv4_dst);
4373 return BPF_FIB_LKUP_RET_NO_NEIGH;
4375 return bpf_fib_set_fwd_params(params, neigh, dev);
4379 #if IS_ENABLED(CONFIG_IPV6)
4380 static int bpf_ipv6_fib_lookup(struct net *net, struct bpf_fib_lookup *params,
4381 u32 flags, bool check_mtu)
4383 struct in6_addr *src = (struct in6_addr *) params->ipv6_src;
4384 struct in6_addr *dst = (struct in6_addr *) params->ipv6_dst;
4385 struct neighbour *neigh;
4386 struct net_device *dev;
4387 struct inet6_dev *idev;
4388 struct fib6_info *f6i;
4394 /* link local addresses are never forwarded */
4395 if (rt6_need_strict(dst) || rt6_need_strict(src))
4396 return BPF_FIB_LKUP_RET_NOT_FWDED;
4398 dev = dev_get_by_index_rcu(net, params->ifindex);
4402 idev = __in6_dev_get_safely(dev);
4403 if (unlikely(!idev || !net->ipv6.devconf_all->forwarding))
4404 return BPF_FIB_LKUP_RET_FWD_DISABLED;
4406 if (flags & BPF_FIB_LOOKUP_OUTPUT) {
4408 oif = fl6.flowi6_oif = params->ifindex;
4410 oif = fl6.flowi6_iif = params->ifindex;
4412 strict = RT6_LOOKUP_F_HAS_SADDR;
4414 fl6.flowlabel = params->flowinfo;
4415 fl6.flowi6_scope = 0;
4416 fl6.flowi6_flags = 0;
4419 fl6.flowi6_proto = params->l4_protocol;
4422 fl6.fl6_sport = params->sport;
4423 fl6.fl6_dport = params->dport;
4425 if (flags & BPF_FIB_LOOKUP_DIRECT) {
4426 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
4427 struct fib6_table *tb;
4429 tb = ipv6_stub->fib6_get_table(net, tbid);
4431 return BPF_FIB_LKUP_RET_NOT_FWDED;
4433 f6i = ipv6_stub->fib6_table_lookup(net, tb, oif, &fl6, strict);
4435 fl6.flowi6_mark = 0;
4436 fl6.flowi6_secid = 0;
4437 fl6.flowi6_tun_key.tun_id = 0;
4438 fl6.flowi6_uid = sock_net_uid(net, NULL);
4440 f6i = ipv6_stub->fib6_lookup(net, oif, &fl6, strict);
4443 if (unlikely(IS_ERR_OR_NULL(f6i) || f6i == net->ipv6.fib6_null_entry))
4444 return BPF_FIB_LKUP_RET_NOT_FWDED;
4446 if (unlikely(f6i->fib6_flags & RTF_REJECT)) {
4447 switch (f6i->fib6_type) {
4449 return BPF_FIB_LKUP_RET_BLACKHOLE;
4450 case RTN_UNREACHABLE:
4451 return BPF_FIB_LKUP_RET_UNREACHABLE;
4453 return BPF_FIB_LKUP_RET_PROHIBIT;
4455 return BPF_FIB_LKUP_RET_NOT_FWDED;
4459 if (f6i->fib6_type != RTN_UNICAST)
4460 return BPF_FIB_LKUP_RET_NOT_FWDED;
4462 if (f6i->fib6_nsiblings && fl6.flowi6_oif == 0)
4463 f6i = ipv6_stub->fib6_multipath_select(net, f6i, &fl6,
4464 fl6.flowi6_oif, NULL,
4468 mtu = ipv6_stub->ip6_mtu_from_fib6(f6i, dst, src);
4469 if (params->tot_len > mtu)
4470 return BPF_FIB_LKUP_RET_FRAG_NEEDED;
4473 if (f6i->fib6_nh.nh_lwtstate)
4474 return BPF_FIB_LKUP_RET_UNSUPP_LWT;
4476 if (f6i->fib6_flags & RTF_GATEWAY)
4477 *dst = f6i->fib6_nh.nh_gw;
4479 dev = f6i->fib6_nh.nh_dev;
4480 params->rt_metric = f6i->fib6_metric;
4482 /* xdp and cls_bpf programs are run in RCU-bh so rcu_read_lock_bh is
4483 * not needed here. Can not use __ipv6_neigh_lookup_noref here
4484 * because we need to get nd_tbl via the stub
4486 neigh = ___neigh_lookup_noref(ipv6_stub->nd_tbl, neigh_key_eq128,
4487 ndisc_hashfn, dst, dev);
4489 return BPF_FIB_LKUP_RET_NO_NEIGH;
4491 return bpf_fib_set_fwd_params(params, neigh, dev);
4495 BPF_CALL_4(bpf_xdp_fib_lookup, struct xdp_buff *, ctx,
4496 struct bpf_fib_lookup *, params, int, plen, u32, flags)
4498 if (plen < sizeof(*params))
4501 if (flags & ~(BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_OUTPUT))
4504 switch (params->family) {
4505 #if IS_ENABLED(CONFIG_INET)
4507 return bpf_ipv4_fib_lookup(dev_net(ctx->rxq->dev), params,
4510 #if IS_ENABLED(CONFIG_IPV6)
4512 return bpf_ipv6_fib_lookup(dev_net(ctx->rxq->dev), params,
4516 return -EAFNOSUPPORT;
4519 static const struct bpf_func_proto bpf_xdp_fib_lookup_proto = {
4520 .func = bpf_xdp_fib_lookup,
4522 .ret_type = RET_INTEGER,
4523 .arg1_type = ARG_PTR_TO_CTX,
4524 .arg2_type = ARG_PTR_TO_MEM,
4525 .arg3_type = ARG_CONST_SIZE,
4526 .arg4_type = ARG_ANYTHING,
4529 BPF_CALL_4(bpf_skb_fib_lookup, struct sk_buff *, skb,
4530 struct bpf_fib_lookup *, params, int, plen, u32, flags)
4532 struct net *net = dev_net(skb->dev);
4533 int rc = -EAFNOSUPPORT;
4535 if (plen < sizeof(*params))
4538 if (flags & ~(BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_OUTPUT))
4541 switch (params->family) {
4542 #if IS_ENABLED(CONFIG_INET)
4544 rc = bpf_ipv4_fib_lookup(net, params, flags, false);
4547 #if IS_ENABLED(CONFIG_IPV6)
4549 rc = bpf_ipv6_fib_lookup(net, params, flags, false);
4555 struct net_device *dev;
4557 dev = dev_get_by_index_rcu(net, params->ifindex);
4558 if (!is_skb_forwardable(dev, skb))
4559 rc = BPF_FIB_LKUP_RET_FRAG_NEEDED;
4565 static const struct bpf_func_proto bpf_skb_fib_lookup_proto = {
4566 .func = bpf_skb_fib_lookup,
4568 .ret_type = RET_INTEGER,
4569 .arg1_type = ARG_PTR_TO_CTX,
4570 .arg2_type = ARG_PTR_TO_MEM,
4571 .arg3_type = ARG_CONST_SIZE,
4572 .arg4_type = ARG_ANYTHING,
4575 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
4576 static int bpf_push_seg6_encap(struct sk_buff *skb, u32 type, void *hdr, u32 len)
4579 struct ipv6_sr_hdr *srh = (struct ipv6_sr_hdr *)hdr;
4581 if (!seg6_validate_srh(srh, len))
4585 case BPF_LWT_ENCAP_SEG6_INLINE:
4586 if (skb->protocol != htons(ETH_P_IPV6))
4589 err = seg6_do_srh_inline(skb, srh);
4591 case BPF_LWT_ENCAP_SEG6:
4592 skb_reset_inner_headers(skb);
4593 skb->encapsulation = 1;
4594 err = seg6_do_srh_encap(skb, srh, IPPROTO_IPV6);
4600 bpf_compute_data_pointers(skb);
4604 ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
4605 skb_set_transport_header(skb, sizeof(struct ipv6hdr));
4607 return seg6_lookup_nexthop(skb, NULL, 0);
4609 #endif /* CONFIG_IPV6_SEG6_BPF */
4611 BPF_CALL_4(bpf_lwt_push_encap, struct sk_buff *, skb, u32, type, void *, hdr,
4615 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
4616 case BPF_LWT_ENCAP_SEG6:
4617 case BPF_LWT_ENCAP_SEG6_INLINE:
4618 return bpf_push_seg6_encap(skb, type, hdr, len);
4625 static const struct bpf_func_proto bpf_lwt_push_encap_proto = {
4626 .func = bpf_lwt_push_encap,
4628 .ret_type = RET_INTEGER,
4629 .arg1_type = ARG_PTR_TO_CTX,
4630 .arg2_type = ARG_ANYTHING,
4631 .arg3_type = ARG_PTR_TO_MEM,
4632 .arg4_type = ARG_CONST_SIZE
4635 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
4636 BPF_CALL_4(bpf_lwt_seg6_store_bytes, struct sk_buff *, skb, u32, offset,
4637 const void *, from, u32, len)
4639 struct seg6_bpf_srh_state *srh_state =
4640 this_cpu_ptr(&seg6_bpf_srh_states);
4641 struct ipv6_sr_hdr *srh = srh_state->srh;
4642 void *srh_tlvs, *srh_end, *ptr;
4648 srh_tlvs = (void *)((char *)srh + ((srh->first_segment + 1) << 4));
4649 srh_end = (void *)((char *)srh + sizeof(*srh) + srh_state->hdrlen);
4651 ptr = skb->data + offset;
4652 if (ptr >= srh_tlvs && ptr + len <= srh_end)
4653 srh_state->valid = false;
4654 else if (ptr < (void *)&srh->flags ||
4655 ptr + len > (void *)&srh->segments)
4658 if (unlikely(bpf_try_make_writable(skb, offset + len)))
4660 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0)
4662 srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
4664 memcpy(skb->data + offset, from, len);
4668 static const struct bpf_func_proto bpf_lwt_seg6_store_bytes_proto = {
4669 .func = bpf_lwt_seg6_store_bytes,
4671 .ret_type = RET_INTEGER,
4672 .arg1_type = ARG_PTR_TO_CTX,
4673 .arg2_type = ARG_ANYTHING,
4674 .arg3_type = ARG_PTR_TO_MEM,
4675 .arg4_type = ARG_CONST_SIZE
4678 static void bpf_update_srh_state(struct sk_buff *skb)
4680 struct seg6_bpf_srh_state *srh_state =
4681 this_cpu_ptr(&seg6_bpf_srh_states);
4684 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0) {
4685 srh_state->srh = NULL;
4687 srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
4688 srh_state->hdrlen = srh_state->srh->hdrlen << 3;
4689 srh_state->valid = true;
4693 BPF_CALL_4(bpf_lwt_seg6_action, struct sk_buff *, skb,
4694 u32, action, void *, param, u32, param_len)
4696 struct seg6_bpf_srh_state *srh_state =
4697 this_cpu_ptr(&seg6_bpf_srh_states);
4702 case SEG6_LOCAL_ACTION_END_X:
4703 if (!seg6_bpf_has_valid_srh(skb))
4705 if (param_len != sizeof(struct in6_addr))
4707 return seg6_lookup_nexthop(skb, (struct in6_addr *)param, 0);
4708 case SEG6_LOCAL_ACTION_END_T:
4709 if (!seg6_bpf_has_valid_srh(skb))
4711 if (param_len != sizeof(int))
4713 return seg6_lookup_nexthop(skb, NULL, *(int *)param);
4714 case SEG6_LOCAL_ACTION_END_DT6:
4715 if (!seg6_bpf_has_valid_srh(skb))
4717 if (param_len != sizeof(int))
4720 if (ipv6_find_hdr(skb, &hdroff, IPPROTO_IPV6, NULL, NULL) < 0)
4722 if (!pskb_pull(skb, hdroff))
4725 skb_postpull_rcsum(skb, skb_network_header(skb), hdroff);
4726 skb_reset_network_header(skb);
4727 skb_reset_transport_header(skb);
4728 skb->encapsulation = 0;
4730 bpf_compute_data_pointers(skb);
4731 bpf_update_srh_state(skb);
4732 return seg6_lookup_nexthop(skb, NULL, *(int *)param);
4733 case SEG6_LOCAL_ACTION_END_B6:
4734 if (srh_state->srh && !seg6_bpf_has_valid_srh(skb))
4736 err = bpf_push_seg6_encap(skb, BPF_LWT_ENCAP_SEG6_INLINE,
4739 bpf_update_srh_state(skb);
4742 case SEG6_LOCAL_ACTION_END_B6_ENCAP:
4743 if (srh_state->srh && !seg6_bpf_has_valid_srh(skb))
4745 err = bpf_push_seg6_encap(skb, BPF_LWT_ENCAP_SEG6,
4748 bpf_update_srh_state(skb);
4756 static const struct bpf_func_proto bpf_lwt_seg6_action_proto = {
4757 .func = bpf_lwt_seg6_action,
4759 .ret_type = RET_INTEGER,
4760 .arg1_type = ARG_PTR_TO_CTX,
4761 .arg2_type = ARG_ANYTHING,
4762 .arg3_type = ARG_PTR_TO_MEM,
4763 .arg4_type = ARG_CONST_SIZE
4766 BPF_CALL_3(bpf_lwt_seg6_adjust_srh, struct sk_buff *, skb, u32, offset,
4769 struct seg6_bpf_srh_state *srh_state =
4770 this_cpu_ptr(&seg6_bpf_srh_states);
4771 struct ipv6_sr_hdr *srh = srh_state->srh;
4772 void *srh_end, *srh_tlvs, *ptr;
4773 struct ipv6hdr *hdr;
4777 if (unlikely(srh == NULL))
4780 srh_tlvs = (void *)((unsigned char *)srh + sizeof(*srh) +
4781 ((srh->first_segment + 1) << 4));
4782 srh_end = (void *)((unsigned char *)srh + sizeof(*srh) +
4784 ptr = skb->data + offset;
4786 if (unlikely(ptr < srh_tlvs || ptr > srh_end))
4788 if (unlikely(len < 0 && (void *)((char *)ptr - len) > srh_end))
4792 ret = skb_cow_head(skb, len);
4793 if (unlikely(ret < 0))
4796 ret = bpf_skb_net_hdr_push(skb, offset, len);
4798 ret = bpf_skb_net_hdr_pop(skb, offset, -1 * len);
4801 bpf_compute_data_pointers(skb);
4802 if (unlikely(ret < 0))
4805 hdr = (struct ipv6hdr *)skb->data;
4806 hdr->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
4808 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0)
4810 srh_state->srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
4811 srh_state->hdrlen += len;
4812 srh_state->valid = false;
4816 static const struct bpf_func_proto bpf_lwt_seg6_adjust_srh_proto = {
4817 .func = bpf_lwt_seg6_adjust_srh,
4819 .ret_type = RET_INTEGER,
4820 .arg1_type = ARG_PTR_TO_CTX,
4821 .arg2_type = ARG_ANYTHING,
4822 .arg3_type = ARG_ANYTHING,
4824 #endif /* CONFIG_IPV6_SEG6_BPF */
4827 static struct sock *sk_lookup(struct net *net, struct bpf_sock_tuple *tuple,
4828 struct sk_buff *skb, u8 family, u8 proto)
4830 bool refcounted = false;
4831 struct sock *sk = NULL;
4835 dif = skb->dev->ifindex;
4837 if (family == AF_INET) {
4838 __be32 src4 = tuple->ipv4.saddr;
4839 __be32 dst4 = tuple->ipv4.daddr;
4840 int sdif = inet_sdif(skb);
4842 if (proto == IPPROTO_TCP)
4843 sk = __inet_lookup(net, &tcp_hashinfo, skb, 0,
4844 src4, tuple->ipv4.sport,
4845 dst4, tuple->ipv4.dport,
4846 dif, sdif, &refcounted);
4848 sk = __udp4_lib_lookup(net, src4, tuple->ipv4.sport,
4849 dst4, tuple->ipv4.dport,
4850 dif, sdif, &udp_table, skb);
4851 #if IS_ENABLED(CONFIG_IPV6)
4853 struct in6_addr *src6 = (struct in6_addr *)&tuple->ipv6.saddr;
4854 struct in6_addr *dst6 = (struct in6_addr *)&tuple->ipv6.daddr;
4855 int sdif = inet6_sdif(skb);
4857 if (proto == IPPROTO_TCP)
4858 sk = __inet6_lookup(net, &tcp_hashinfo, skb, 0,
4859 src6, tuple->ipv6.sport,
4860 dst6, ntohs(tuple->ipv6.dport),
4861 dif, sdif, &refcounted);
4862 else if (likely(ipv6_bpf_stub))
4863 sk = ipv6_bpf_stub->udp6_lib_lookup(net,
4864 src6, tuple->ipv6.sport,
4865 dst6, tuple->ipv6.dport,
4871 if (unlikely(sk && !refcounted && !sock_flag(sk, SOCK_RCU_FREE))) {
4872 WARN_ONCE(1, "Found non-RCU, unreferenced socket!");
4878 /* bpf_sk_lookup performs the core lookup for different types of sockets,
4879 * taking a reference on the socket if it doesn't have the flag SOCK_RCU_FREE.
4880 * Returns the socket as an 'unsigned long' to simplify the casting in the
4881 * callers to satisfy BPF_CALL declarations.
4883 static unsigned long
4884 bpf_sk_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len,
4885 u8 proto, u64 netns_id, u64 flags)
4887 struct net *caller_net;
4888 struct sock *sk = NULL;
4889 u8 family = AF_UNSPEC;
4892 family = len == sizeof(tuple->ipv4) ? AF_INET : AF_INET6;
4893 if (unlikely(family == AF_UNSPEC || flags ||
4894 !((s32)netns_id < 0 || netns_id <= S32_MAX)))
4898 caller_net = dev_net(skb->dev);
4900 caller_net = sock_net(skb->sk);
4901 if ((s32)netns_id < 0) {
4903 sk = sk_lookup(net, tuple, skb, family, proto);
4905 net = get_net_ns_by_id(caller_net, netns_id);
4908 sk = sk_lookup(net, tuple, skb, family, proto);
4913 sk = sk_to_full_sk(sk);
4915 return (unsigned long) sk;
4918 BPF_CALL_5(bpf_sk_lookup_tcp, struct sk_buff *, skb,
4919 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
4921 return bpf_sk_lookup(skb, tuple, len, IPPROTO_TCP, netns_id, flags);
4924 static const struct bpf_func_proto bpf_sk_lookup_tcp_proto = {
4925 .func = bpf_sk_lookup_tcp,
4928 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
4929 .arg1_type = ARG_PTR_TO_CTX,
4930 .arg2_type = ARG_PTR_TO_MEM,
4931 .arg3_type = ARG_CONST_SIZE,
4932 .arg4_type = ARG_ANYTHING,
4933 .arg5_type = ARG_ANYTHING,
4936 BPF_CALL_5(bpf_sk_lookup_udp, struct sk_buff *, skb,
4937 struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags)
4939 return bpf_sk_lookup(skb, tuple, len, IPPROTO_UDP, netns_id, flags);
4942 static const struct bpf_func_proto bpf_sk_lookup_udp_proto = {
4943 .func = bpf_sk_lookup_udp,
4946 .ret_type = RET_PTR_TO_SOCKET_OR_NULL,
4947 .arg1_type = ARG_PTR_TO_CTX,
4948 .arg2_type = ARG_PTR_TO_MEM,
4949 .arg3_type = ARG_CONST_SIZE,
4950 .arg4_type = ARG_ANYTHING,
4951 .arg5_type = ARG_ANYTHING,
4954 BPF_CALL_1(bpf_sk_release, struct sock *, sk)
4956 if (!sock_flag(sk, SOCK_RCU_FREE))
4961 static const struct bpf_func_proto bpf_sk_release_proto = {
4962 .func = bpf_sk_release,
4964 .ret_type = RET_INTEGER,
4965 .arg1_type = ARG_PTR_TO_SOCKET,
4967 #endif /* CONFIG_INET */
4969 bool bpf_helper_changes_pkt_data(void *func)
4971 if (func == bpf_skb_vlan_push ||
4972 func == bpf_skb_vlan_pop ||
4973 func == bpf_skb_store_bytes ||
4974 func == bpf_skb_change_proto ||
4975 func == bpf_skb_change_head ||
4976 func == sk_skb_change_head ||
4977 func == bpf_skb_change_tail ||
4978 func == sk_skb_change_tail ||
4979 func == bpf_skb_adjust_room ||
4980 func == bpf_skb_pull_data ||
4981 func == sk_skb_pull_data ||
4982 func == bpf_clone_redirect ||
4983 func == bpf_l3_csum_replace ||
4984 func == bpf_l4_csum_replace ||
4985 func == bpf_xdp_adjust_head ||
4986 func == bpf_xdp_adjust_meta ||
4987 func == bpf_msg_pull_data ||
4988 func == bpf_msg_push_data ||
4989 func == bpf_xdp_adjust_tail ||
4990 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
4991 func == bpf_lwt_seg6_store_bytes ||
4992 func == bpf_lwt_seg6_adjust_srh ||
4993 func == bpf_lwt_seg6_action ||
4995 func == bpf_lwt_push_encap)
5001 static const struct bpf_func_proto *
5002 bpf_base_func_proto(enum bpf_func_id func_id)
5005 case BPF_FUNC_map_lookup_elem:
5006 return &bpf_map_lookup_elem_proto;
5007 case BPF_FUNC_map_update_elem:
5008 return &bpf_map_update_elem_proto;
5009 case BPF_FUNC_map_delete_elem:
5010 return &bpf_map_delete_elem_proto;
5011 case BPF_FUNC_map_push_elem:
5012 return &bpf_map_push_elem_proto;
5013 case BPF_FUNC_map_pop_elem:
5014 return &bpf_map_pop_elem_proto;
5015 case BPF_FUNC_map_peek_elem:
5016 return &bpf_map_peek_elem_proto;
5017 case BPF_FUNC_get_prandom_u32:
5018 return &bpf_get_prandom_u32_proto;
5019 case BPF_FUNC_get_smp_processor_id:
5020 return &bpf_get_raw_smp_processor_id_proto;
5021 case BPF_FUNC_get_numa_node_id:
5022 return &bpf_get_numa_node_id_proto;
5023 case BPF_FUNC_tail_call:
5024 return &bpf_tail_call_proto;
5025 case BPF_FUNC_ktime_get_ns:
5026 return &bpf_ktime_get_ns_proto;
5027 case BPF_FUNC_trace_printk:
5028 if (capable(CAP_SYS_ADMIN))
5029 return bpf_get_trace_printk_proto();
5030 /* else: fall through */
5036 static const struct bpf_func_proto *
5037 sock_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5040 /* inet and inet6 sockets are created in a process
5041 * context so there is always a valid uid/gid
5043 case BPF_FUNC_get_current_uid_gid:
5044 return &bpf_get_current_uid_gid_proto;
5045 case BPF_FUNC_get_local_storage:
5046 return &bpf_get_local_storage_proto;
5048 return bpf_base_func_proto(func_id);
5052 static const struct bpf_func_proto *
5053 sock_addr_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5056 /* inet and inet6 sockets are created in a process
5057 * context so there is always a valid uid/gid
5059 case BPF_FUNC_get_current_uid_gid:
5060 return &bpf_get_current_uid_gid_proto;
5062 switch (prog->expected_attach_type) {
5063 case BPF_CGROUP_INET4_CONNECT:
5064 case BPF_CGROUP_INET6_CONNECT:
5065 return &bpf_bind_proto;
5069 case BPF_FUNC_get_socket_cookie:
5070 return &bpf_get_socket_cookie_sock_addr_proto;
5071 case BPF_FUNC_get_local_storage:
5072 return &bpf_get_local_storage_proto;
5074 return bpf_base_func_proto(func_id);
5078 static const struct bpf_func_proto *
5079 sk_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5082 case BPF_FUNC_skb_load_bytes:
5083 return &bpf_skb_load_bytes_proto;
5084 case BPF_FUNC_skb_load_bytes_relative:
5085 return &bpf_skb_load_bytes_relative_proto;
5086 case BPF_FUNC_get_socket_cookie:
5087 return &bpf_get_socket_cookie_proto;
5088 case BPF_FUNC_get_socket_uid:
5089 return &bpf_get_socket_uid_proto;
5091 return bpf_base_func_proto(func_id);
5095 static const struct bpf_func_proto *
5096 cg_skb_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5099 case BPF_FUNC_get_local_storage:
5100 return &bpf_get_local_storage_proto;
5102 return sk_filter_func_proto(func_id, prog);
5106 static const struct bpf_func_proto *
5107 tc_cls_act_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5110 case BPF_FUNC_skb_store_bytes:
5111 return &bpf_skb_store_bytes_proto;
5112 case BPF_FUNC_skb_load_bytes:
5113 return &bpf_skb_load_bytes_proto;
5114 case BPF_FUNC_skb_load_bytes_relative:
5115 return &bpf_skb_load_bytes_relative_proto;
5116 case BPF_FUNC_skb_pull_data:
5117 return &bpf_skb_pull_data_proto;
5118 case BPF_FUNC_csum_diff:
5119 return &bpf_csum_diff_proto;
5120 case BPF_FUNC_csum_update:
5121 return &bpf_csum_update_proto;
5122 case BPF_FUNC_l3_csum_replace:
5123 return &bpf_l3_csum_replace_proto;
5124 case BPF_FUNC_l4_csum_replace:
5125 return &bpf_l4_csum_replace_proto;
5126 case BPF_FUNC_clone_redirect:
5127 return &bpf_clone_redirect_proto;
5128 case BPF_FUNC_get_cgroup_classid:
5129 return &bpf_get_cgroup_classid_proto;
5130 case BPF_FUNC_skb_vlan_push:
5131 return &bpf_skb_vlan_push_proto;
5132 case BPF_FUNC_skb_vlan_pop:
5133 return &bpf_skb_vlan_pop_proto;
5134 case BPF_FUNC_skb_change_proto:
5135 return &bpf_skb_change_proto_proto;
5136 case BPF_FUNC_skb_change_type:
5137 return &bpf_skb_change_type_proto;
5138 case BPF_FUNC_skb_adjust_room:
5139 return &bpf_skb_adjust_room_proto;
5140 case BPF_FUNC_skb_change_tail:
5141 return &bpf_skb_change_tail_proto;
5142 case BPF_FUNC_skb_get_tunnel_key:
5143 return &bpf_skb_get_tunnel_key_proto;
5144 case BPF_FUNC_skb_set_tunnel_key:
5145 return bpf_get_skb_set_tunnel_proto(func_id);
5146 case BPF_FUNC_skb_get_tunnel_opt:
5147 return &bpf_skb_get_tunnel_opt_proto;
5148 case BPF_FUNC_skb_set_tunnel_opt:
5149 return bpf_get_skb_set_tunnel_proto(func_id);
5150 case BPF_FUNC_redirect:
5151 return &bpf_redirect_proto;
5152 case BPF_FUNC_get_route_realm:
5153 return &bpf_get_route_realm_proto;
5154 case BPF_FUNC_get_hash_recalc:
5155 return &bpf_get_hash_recalc_proto;
5156 case BPF_FUNC_set_hash_invalid:
5157 return &bpf_set_hash_invalid_proto;
5158 case BPF_FUNC_set_hash:
5159 return &bpf_set_hash_proto;
5160 case BPF_FUNC_perf_event_output:
5161 return &bpf_skb_event_output_proto;
5162 case BPF_FUNC_get_smp_processor_id:
5163 return &bpf_get_smp_processor_id_proto;
5164 case BPF_FUNC_skb_under_cgroup:
5165 return &bpf_skb_under_cgroup_proto;
5166 case BPF_FUNC_get_socket_cookie:
5167 return &bpf_get_socket_cookie_proto;
5168 case BPF_FUNC_get_socket_uid:
5169 return &bpf_get_socket_uid_proto;
5170 case BPF_FUNC_fib_lookup:
5171 return &bpf_skb_fib_lookup_proto;
5173 case BPF_FUNC_skb_get_xfrm_state:
5174 return &bpf_skb_get_xfrm_state_proto;
5176 #ifdef CONFIG_SOCK_CGROUP_DATA
5177 case BPF_FUNC_skb_cgroup_id:
5178 return &bpf_skb_cgroup_id_proto;
5179 case BPF_FUNC_skb_ancestor_cgroup_id:
5180 return &bpf_skb_ancestor_cgroup_id_proto;
5183 case BPF_FUNC_sk_lookup_tcp:
5184 return &bpf_sk_lookup_tcp_proto;
5185 case BPF_FUNC_sk_lookup_udp:
5186 return &bpf_sk_lookup_udp_proto;
5187 case BPF_FUNC_sk_release:
5188 return &bpf_sk_release_proto;
5191 return bpf_base_func_proto(func_id);
5195 static const struct bpf_func_proto *
5196 xdp_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5199 case BPF_FUNC_perf_event_output:
5200 return &bpf_xdp_event_output_proto;
5201 case BPF_FUNC_get_smp_processor_id:
5202 return &bpf_get_smp_processor_id_proto;
5203 case BPF_FUNC_csum_diff:
5204 return &bpf_csum_diff_proto;
5205 case BPF_FUNC_xdp_adjust_head:
5206 return &bpf_xdp_adjust_head_proto;
5207 case BPF_FUNC_xdp_adjust_meta:
5208 return &bpf_xdp_adjust_meta_proto;
5209 case BPF_FUNC_redirect:
5210 return &bpf_xdp_redirect_proto;
5211 case BPF_FUNC_redirect_map:
5212 return &bpf_xdp_redirect_map_proto;
5213 case BPF_FUNC_xdp_adjust_tail:
5214 return &bpf_xdp_adjust_tail_proto;
5215 case BPF_FUNC_fib_lookup:
5216 return &bpf_xdp_fib_lookup_proto;
5218 return bpf_base_func_proto(func_id);
5222 const struct bpf_func_proto bpf_sock_map_update_proto __weak;
5223 const struct bpf_func_proto bpf_sock_hash_update_proto __weak;
5225 static const struct bpf_func_proto *
5226 sock_ops_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5229 case BPF_FUNC_setsockopt:
5230 return &bpf_setsockopt_proto;
5231 case BPF_FUNC_getsockopt:
5232 return &bpf_getsockopt_proto;
5233 case BPF_FUNC_sock_ops_cb_flags_set:
5234 return &bpf_sock_ops_cb_flags_set_proto;
5235 case BPF_FUNC_sock_map_update:
5236 return &bpf_sock_map_update_proto;
5237 case BPF_FUNC_sock_hash_update:
5238 return &bpf_sock_hash_update_proto;
5239 case BPF_FUNC_get_socket_cookie:
5240 return &bpf_get_socket_cookie_sock_ops_proto;
5241 case BPF_FUNC_get_local_storage:
5242 return &bpf_get_local_storage_proto;
5244 return bpf_base_func_proto(func_id);
5248 const struct bpf_func_proto bpf_msg_redirect_map_proto __weak;
5249 const struct bpf_func_proto bpf_msg_redirect_hash_proto __weak;
5251 static const struct bpf_func_proto *
5252 sk_msg_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5255 case BPF_FUNC_msg_redirect_map:
5256 return &bpf_msg_redirect_map_proto;
5257 case BPF_FUNC_msg_redirect_hash:
5258 return &bpf_msg_redirect_hash_proto;
5259 case BPF_FUNC_msg_apply_bytes:
5260 return &bpf_msg_apply_bytes_proto;
5261 case BPF_FUNC_msg_cork_bytes:
5262 return &bpf_msg_cork_bytes_proto;
5263 case BPF_FUNC_msg_pull_data:
5264 return &bpf_msg_pull_data_proto;
5265 case BPF_FUNC_msg_push_data:
5266 return &bpf_msg_push_data_proto;
5268 return bpf_base_func_proto(func_id);
5272 const struct bpf_func_proto bpf_sk_redirect_map_proto __weak;
5273 const struct bpf_func_proto bpf_sk_redirect_hash_proto __weak;
5275 static const struct bpf_func_proto *
5276 sk_skb_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5279 case BPF_FUNC_skb_store_bytes:
5280 return &bpf_skb_store_bytes_proto;
5281 case BPF_FUNC_skb_load_bytes:
5282 return &bpf_skb_load_bytes_proto;
5283 case BPF_FUNC_skb_pull_data:
5284 return &sk_skb_pull_data_proto;
5285 case BPF_FUNC_skb_change_tail:
5286 return &sk_skb_change_tail_proto;
5287 case BPF_FUNC_skb_change_head:
5288 return &sk_skb_change_head_proto;
5289 case BPF_FUNC_get_socket_cookie:
5290 return &bpf_get_socket_cookie_proto;
5291 case BPF_FUNC_get_socket_uid:
5292 return &bpf_get_socket_uid_proto;
5293 case BPF_FUNC_sk_redirect_map:
5294 return &bpf_sk_redirect_map_proto;
5295 case BPF_FUNC_sk_redirect_hash:
5296 return &bpf_sk_redirect_hash_proto;
5298 case BPF_FUNC_sk_lookup_tcp:
5299 return &bpf_sk_lookup_tcp_proto;
5300 case BPF_FUNC_sk_lookup_udp:
5301 return &bpf_sk_lookup_udp_proto;
5302 case BPF_FUNC_sk_release:
5303 return &bpf_sk_release_proto;
5306 return bpf_base_func_proto(func_id);
5310 static const struct bpf_func_proto *
5311 flow_dissector_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5314 case BPF_FUNC_skb_load_bytes:
5315 return &bpf_skb_load_bytes_proto;
5317 return bpf_base_func_proto(func_id);
5321 static const struct bpf_func_proto *
5322 lwt_out_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5325 case BPF_FUNC_skb_load_bytes:
5326 return &bpf_skb_load_bytes_proto;
5327 case BPF_FUNC_skb_pull_data:
5328 return &bpf_skb_pull_data_proto;
5329 case BPF_FUNC_csum_diff:
5330 return &bpf_csum_diff_proto;
5331 case BPF_FUNC_get_cgroup_classid:
5332 return &bpf_get_cgroup_classid_proto;
5333 case BPF_FUNC_get_route_realm:
5334 return &bpf_get_route_realm_proto;
5335 case BPF_FUNC_get_hash_recalc:
5336 return &bpf_get_hash_recalc_proto;
5337 case BPF_FUNC_perf_event_output:
5338 return &bpf_skb_event_output_proto;
5339 case BPF_FUNC_get_smp_processor_id:
5340 return &bpf_get_smp_processor_id_proto;
5341 case BPF_FUNC_skb_under_cgroup:
5342 return &bpf_skb_under_cgroup_proto;
5344 return bpf_base_func_proto(func_id);
5348 static const struct bpf_func_proto *
5349 lwt_in_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5352 case BPF_FUNC_lwt_push_encap:
5353 return &bpf_lwt_push_encap_proto;
5355 return lwt_out_func_proto(func_id, prog);
5359 static const struct bpf_func_proto *
5360 lwt_xmit_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5363 case BPF_FUNC_skb_get_tunnel_key:
5364 return &bpf_skb_get_tunnel_key_proto;
5365 case BPF_FUNC_skb_set_tunnel_key:
5366 return bpf_get_skb_set_tunnel_proto(func_id);
5367 case BPF_FUNC_skb_get_tunnel_opt:
5368 return &bpf_skb_get_tunnel_opt_proto;
5369 case BPF_FUNC_skb_set_tunnel_opt:
5370 return bpf_get_skb_set_tunnel_proto(func_id);
5371 case BPF_FUNC_redirect:
5372 return &bpf_redirect_proto;
5373 case BPF_FUNC_clone_redirect:
5374 return &bpf_clone_redirect_proto;
5375 case BPF_FUNC_skb_change_tail:
5376 return &bpf_skb_change_tail_proto;
5377 case BPF_FUNC_skb_change_head:
5378 return &bpf_skb_change_head_proto;
5379 case BPF_FUNC_skb_store_bytes:
5380 return &bpf_skb_store_bytes_proto;
5381 case BPF_FUNC_csum_update:
5382 return &bpf_csum_update_proto;
5383 case BPF_FUNC_l3_csum_replace:
5384 return &bpf_l3_csum_replace_proto;
5385 case BPF_FUNC_l4_csum_replace:
5386 return &bpf_l4_csum_replace_proto;
5387 case BPF_FUNC_set_hash_invalid:
5388 return &bpf_set_hash_invalid_proto;
5390 return lwt_out_func_proto(func_id, prog);
5394 static const struct bpf_func_proto *
5395 lwt_seg6local_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5398 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
5399 case BPF_FUNC_lwt_seg6_store_bytes:
5400 return &bpf_lwt_seg6_store_bytes_proto;
5401 case BPF_FUNC_lwt_seg6_action:
5402 return &bpf_lwt_seg6_action_proto;
5403 case BPF_FUNC_lwt_seg6_adjust_srh:
5404 return &bpf_lwt_seg6_adjust_srh_proto;
5407 return lwt_out_func_proto(func_id, prog);
5411 static bool bpf_skb_is_valid_access(int off, int size, enum bpf_access_type type,
5412 const struct bpf_prog *prog,
5413 struct bpf_insn_access_aux *info)
5415 const int size_default = sizeof(__u32);
5417 if (off < 0 || off >= sizeof(struct __sk_buff))
5420 /* The verifier guarantees that size > 0. */
5421 if (off % size != 0)
5425 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
5426 if (off + size > offsetofend(struct __sk_buff, cb[4]))
5429 case bpf_ctx_range_till(struct __sk_buff, remote_ip6[0], remote_ip6[3]):
5430 case bpf_ctx_range_till(struct __sk_buff, local_ip6[0], local_ip6[3]):
5431 case bpf_ctx_range_till(struct __sk_buff, remote_ip4, remote_ip4):
5432 case bpf_ctx_range_till(struct __sk_buff, local_ip4, local_ip4):
5433 case bpf_ctx_range(struct __sk_buff, data):
5434 case bpf_ctx_range(struct __sk_buff, data_meta):
5435 case bpf_ctx_range(struct __sk_buff, data_end):
5436 if (size != size_default)
5439 case bpf_ctx_range_ptr(struct __sk_buff, flow_keys):
5440 if (size != sizeof(__u64))
5444 /* Only narrow read access allowed for now. */
5445 if (type == BPF_WRITE) {
5446 if (size != size_default)
5449 bpf_ctx_record_field_size(info, size_default);
5450 if (!bpf_ctx_narrow_access_ok(off, size, size_default))
5458 static bool sk_filter_is_valid_access(int off, int size,
5459 enum bpf_access_type type,
5460 const struct bpf_prog *prog,
5461 struct bpf_insn_access_aux *info)
5464 case bpf_ctx_range(struct __sk_buff, tc_classid):
5465 case bpf_ctx_range(struct __sk_buff, data):
5466 case bpf_ctx_range(struct __sk_buff, data_meta):
5467 case bpf_ctx_range(struct __sk_buff, data_end):
5468 case bpf_ctx_range_ptr(struct __sk_buff, flow_keys):
5469 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
5473 if (type == BPF_WRITE) {
5475 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
5482 return bpf_skb_is_valid_access(off, size, type, prog, info);
5485 static bool cg_skb_is_valid_access(int off, int size,
5486 enum bpf_access_type type,
5487 const struct bpf_prog *prog,
5488 struct bpf_insn_access_aux *info)
5491 case bpf_ctx_range(struct __sk_buff, tc_classid):
5492 case bpf_ctx_range(struct __sk_buff, data_meta):
5493 case bpf_ctx_range_ptr(struct __sk_buff, flow_keys):
5495 case bpf_ctx_range(struct __sk_buff, data):
5496 case bpf_ctx_range(struct __sk_buff, data_end):
5497 if (!capable(CAP_SYS_ADMIN))
5502 if (type == BPF_WRITE) {
5504 case bpf_ctx_range(struct __sk_buff, mark):
5505 case bpf_ctx_range(struct __sk_buff, priority):
5506 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
5514 case bpf_ctx_range(struct __sk_buff, data):
5515 info->reg_type = PTR_TO_PACKET;
5517 case bpf_ctx_range(struct __sk_buff, data_end):
5518 info->reg_type = PTR_TO_PACKET_END;
5522 return bpf_skb_is_valid_access(off, size, type, prog, info);
5525 static bool lwt_is_valid_access(int off, int size,
5526 enum bpf_access_type type,
5527 const struct bpf_prog *prog,
5528 struct bpf_insn_access_aux *info)
5531 case bpf_ctx_range(struct __sk_buff, tc_classid):
5532 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
5533 case bpf_ctx_range(struct __sk_buff, data_meta):
5534 case bpf_ctx_range_ptr(struct __sk_buff, flow_keys):
5538 if (type == BPF_WRITE) {
5540 case bpf_ctx_range(struct __sk_buff, mark):
5541 case bpf_ctx_range(struct __sk_buff, priority):
5542 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
5550 case bpf_ctx_range(struct __sk_buff, data):
5551 info->reg_type = PTR_TO_PACKET;
5553 case bpf_ctx_range(struct __sk_buff, data_end):
5554 info->reg_type = PTR_TO_PACKET_END;
5558 return bpf_skb_is_valid_access(off, size, type, prog, info);
5561 /* Attach type specific accesses */
5562 static bool __sock_filter_check_attach_type(int off,
5563 enum bpf_access_type access_type,
5564 enum bpf_attach_type attach_type)
5567 case offsetof(struct bpf_sock, bound_dev_if):
5568 case offsetof(struct bpf_sock, mark):
5569 case offsetof(struct bpf_sock, priority):
5570 switch (attach_type) {
5571 case BPF_CGROUP_INET_SOCK_CREATE:
5576 case bpf_ctx_range(struct bpf_sock, src_ip4):
5577 switch (attach_type) {
5578 case BPF_CGROUP_INET4_POST_BIND:
5583 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
5584 switch (attach_type) {
5585 case BPF_CGROUP_INET6_POST_BIND:
5590 case bpf_ctx_range(struct bpf_sock, src_port):
5591 switch (attach_type) {
5592 case BPF_CGROUP_INET4_POST_BIND:
5593 case BPF_CGROUP_INET6_POST_BIND:
5600 return access_type == BPF_READ;
5605 static bool __sock_filter_check_size(int off, int size,
5606 struct bpf_insn_access_aux *info)
5608 const int size_default = sizeof(__u32);
5611 case bpf_ctx_range(struct bpf_sock, src_ip4):
5612 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
5613 bpf_ctx_record_field_size(info, size_default);
5614 return bpf_ctx_narrow_access_ok(off, size, size_default);
5617 return size == size_default;
5620 bool bpf_sock_is_valid_access(int off, int size, enum bpf_access_type type,
5621 struct bpf_insn_access_aux *info)
5623 if (off < 0 || off >= sizeof(struct bpf_sock))
5625 if (off % size != 0)
5627 if (!__sock_filter_check_size(off, size, info))
5632 static bool sock_filter_is_valid_access(int off, int size,
5633 enum bpf_access_type type,
5634 const struct bpf_prog *prog,
5635 struct bpf_insn_access_aux *info)
5637 if (!bpf_sock_is_valid_access(off, size, type, info))
5639 return __sock_filter_check_attach_type(off, type,
5640 prog->expected_attach_type);
5643 static int bpf_noop_prologue(struct bpf_insn *insn_buf, bool direct_write,
5644 const struct bpf_prog *prog)
5646 /* Neither direct read nor direct write requires any preliminary
5652 static int bpf_unclone_prologue(struct bpf_insn *insn_buf, bool direct_write,
5653 const struct bpf_prog *prog, int drop_verdict)
5655 struct bpf_insn *insn = insn_buf;
5660 /* if (!skb->cloned)
5663 * (Fast-path, otherwise approximation that we might be
5664 * a clone, do the rest in helper.)
5666 *insn++ = BPF_LDX_MEM(BPF_B, BPF_REG_6, BPF_REG_1, CLONED_OFFSET());
5667 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_6, CLONED_MASK);
5668 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_6, 0, 7);
5670 /* ret = bpf_skb_pull_data(skb, 0); */
5671 *insn++ = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1);
5672 *insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_2, BPF_REG_2);
5673 *insn++ = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
5674 BPF_FUNC_skb_pull_data);
5677 * return TC_ACT_SHOT;
5679 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2);
5680 *insn++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_0, drop_verdict);
5681 *insn++ = BPF_EXIT_INSN();
5684 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_6);
5686 *insn++ = prog->insnsi[0];
5688 return insn - insn_buf;
5691 static int bpf_gen_ld_abs(const struct bpf_insn *orig,
5692 struct bpf_insn *insn_buf)
5694 bool indirect = BPF_MODE(orig->code) == BPF_IND;
5695 struct bpf_insn *insn = insn_buf;
5697 /* We're guaranteed here that CTX is in R6. */
5698 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_CTX);
5700 *insn++ = BPF_MOV64_IMM(BPF_REG_2, orig->imm);
5702 *insn++ = BPF_MOV64_REG(BPF_REG_2, orig->src_reg);
5704 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, orig->imm);
5707 switch (BPF_SIZE(orig->code)) {
5709 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_8_no_cache);
5712 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_16_no_cache);
5715 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_32_no_cache);
5719 *insn++ = BPF_JMP_IMM(BPF_JSGE, BPF_REG_0, 0, 2);
5720 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_0, BPF_REG_0);
5721 *insn++ = BPF_EXIT_INSN();
5723 return insn - insn_buf;
5726 static int tc_cls_act_prologue(struct bpf_insn *insn_buf, bool direct_write,
5727 const struct bpf_prog *prog)
5729 return bpf_unclone_prologue(insn_buf, direct_write, prog, TC_ACT_SHOT);
5732 static bool tc_cls_act_is_valid_access(int off, int size,
5733 enum bpf_access_type type,
5734 const struct bpf_prog *prog,
5735 struct bpf_insn_access_aux *info)
5737 if (type == BPF_WRITE) {
5739 case bpf_ctx_range(struct __sk_buff, mark):
5740 case bpf_ctx_range(struct __sk_buff, tc_index):
5741 case bpf_ctx_range(struct __sk_buff, priority):
5742 case bpf_ctx_range(struct __sk_buff, tc_classid):
5743 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
5751 case bpf_ctx_range(struct __sk_buff, data):
5752 info->reg_type = PTR_TO_PACKET;
5754 case bpf_ctx_range(struct __sk_buff, data_meta):
5755 info->reg_type = PTR_TO_PACKET_META;
5757 case bpf_ctx_range(struct __sk_buff, data_end):
5758 info->reg_type = PTR_TO_PACKET_END;
5760 case bpf_ctx_range_ptr(struct __sk_buff, flow_keys):
5761 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
5765 return bpf_skb_is_valid_access(off, size, type, prog, info);
5768 static bool __is_valid_xdp_access(int off, int size)
5770 if (off < 0 || off >= sizeof(struct xdp_md))
5772 if (off % size != 0)
5774 if (size != sizeof(__u32))
5780 static bool xdp_is_valid_access(int off, int size,
5781 enum bpf_access_type type,
5782 const struct bpf_prog *prog,
5783 struct bpf_insn_access_aux *info)
5785 if (type == BPF_WRITE) {
5786 if (bpf_prog_is_dev_bound(prog->aux)) {
5788 case offsetof(struct xdp_md, rx_queue_index):
5789 return __is_valid_xdp_access(off, size);
5796 case offsetof(struct xdp_md, data):
5797 info->reg_type = PTR_TO_PACKET;
5799 case offsetof(struct xdp_md, data_meta):
5800 info->reg_type = PTR_TO_PACKET_META;
5802 case offsetof(struct xdp_md, data_end):
5803 info->reg_type = PTR_TO_PACKET_END;
5807 return __is_valid_xdp_access(off, size);
5810 void bpf_warn_invalid_xdp_action(u32 act)
5812 const u32 act_max = XDP_REDIRECT;
5814 WARN_ONCE(1, "%s XDP return value %u, expect packet loss!\n",
5815 act > act_max ? "Illegal" : "Driver unsupported",
5818 EXPORT_SYMBOL_GPL(bpf_warn_invalid_xdp_action);
5820 static bool sock_addr_is_valid_access(int off, int size,
5821 enum bpf_access_type type,
5822 const struct bpf_prog *prog,
5823 struct bpf_insn_access_aux *info)
5825 const int size_default = sizeof(__u32);
5827 if (off < 0 || off >= sizeof(struct bpf_sock_addr))
5829 if (off % size != 0)
5832 /* Disallow access to IPv6 fields from IPv4 contex and vise
5836 case bpf_ctx_range(struct bpf_sock_addr, user_ip4):
5837 switch (prog->expected_attach_type) {
5838 case BPF_CGROUP_INET4_BIND:
5839 case BPF_CGROUP_INET4_CONNECT:
5840 case BPF_CGROUP_UDP4_SENDMSG:
5846 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
5847 switch (prog->expected_attach_type) {
5848 case BPF_CGROUP_INET6_BIND:
5849 case BPF_CGROUP_INET6_CONNECT:
5850 case BPF_CGROUP_UDP6_SENDMSG:
5856 case bpf_ctx_range(struct bpf_sock_addr, msg_src_ip4):
5857 switch (prog->expected_attach_type) {
5858 case BPF_CGROUP_UDP4_SENDMSG:
5864 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
5866 switch (prog->expected_attach_type) {
5867 case BPF_CGROUP_UDP6_SENDMSG:
5876 case bpf_ctx_range(struct bpf_sock_addr, user_ip4):
5877 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
5878 case bpf_ctx_range(struct bpf_sock_addr, msg_src_ip4):
5879 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
5881 /* Only narrow read access allowed for now. */
5882 if (type == BPF_READ) {
5883 bpf_ctx_record_field_size(info, size_default);
5884 if (!bpf_ctx_narrow_access_ok(off, size, size_default))
5887 if (size != size_default)
5891 case bpf_ctx_range(struct bpf_sock_addr, user_port):
5892 if (size != size_default)
5896 if (type == BPF_READ) {
5897 if (size != size_default)
5907 static bool sock_ops_is_valid_access(int off, int size,
5908 enum bpf_access_type type,
5909 const struct bpf_prog *prog,
5910 struct bpf_insn_access_aux *info)
5912 const int size_default = sizeof(__u32);
5914 if (off < 0 || off >= sizeof(struct bpf_sock_ops))
5917 /* The verifier guarantees that size > 0. */
5918 if (off % size != 0)
5921 if (type == BPF_WRITE) {
5923 case offsetof(struct bpf_sock_ops, reply):
5924 case offsetof(struct bpf_sock_ops, sk_txhash):
5925 if (size != size_default)
5933 case bpf_ctx_range_till(struct bpf_sock_ops, bytes_received,
5935 if (size != sizeof(__u64))
5939 if (size != size_default)
5948 static int sk_skb_prologue(struct bpf_insn *insn_buf, bool direct_write,
5949 const struct bpf_prog *prog)
5951 return bpf_unclone_prologue(insn_buf, direct_write, prog, SK_DROP);
5954 static bool sk_skb_is_valid_access(int off, int size,
5955 enum bpf_access_type type,
5956 const struct bpf_prog *prog,
5957 struct bpf_insn_access_aux *info)
5960 case bpf_ctx_range(struct __sk_buff, tc_classid):
5961 case bpf_ctx_range(struct __sk_buff, data_meta):
5962 case bpf_ctx_range_ptr(struct __sk_buff, flow_keys):
5966 if (type == BPF_WRITE) {
5968 case bpf_ctx_range(struct __sk_buff, tc_index):
5969 case bpf_ctx_range(struct __sk_buff, priority):
5977 case bpf_ctx_range(struct __sk_buff, mark):
5979 case bpf_ctx_range(struct __sk_buff, data):
5980 info->reg_type = PTR_TO_PACKET;
5982 case bpf_ctx_range(struct __sk_buff, data_end):
5983 info->reg_type = PTR_TO_PACKET_END;
5987 return bpf_skb_is_valid_access(off, size, type, prog, info);
5990 static bool sk_msg_is_valid_access(int off, int size,
5991 enum bpf_access_type type,
5992 const struct bpf_prog *prog,
5993 struct bpf_insn_access_aux *info)
5995 if (type == BPF_WRITE)
5999 case offsetof(struct sk_msg_md, data):
6000 info->reg_type = PTR_TO_PACKET;
6001 if (size != sizeof(__u64))
6004 case offsetof(struct sk_msg_md, data_end):
6005 info->reg_type = PTR_TO_PACKET_END;
6006 if (size != sizeof(__u64))
6010 if (size != sizeof(__u32))
6014 if (off < 0 || off >= sizeof(struct sk_msg_md))
6016 if (off % size != 0)
6022 static bool flow_dissector_is_valid_access(int off, int size,
6023 enum bpf_access_type type,
6024 const struct bpf_prog *prog,
6025 struct bpf_insn_access_aux *info)
6027 if (type == BPF_WRITE) {
6029 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
6037 case bpf_ctx_range(struct __sk_buff, data):
6038 info->reg_type = PTR_TO_PACKET;
6040 case bpf_ctx_range(struct __sk_buff, data_end):
6041 info->reg_type = PTR_TO_PACKET_END;
6043 case bpf_ctx_range_ptr(struct __sk_buff, flow_keys):
6044 info->reg_type = PTR_TO_FLOW_KEYS;
6046 case bpf_ctx_range(struct __sk_buff, tc_classid):
6047 case bpf_ctx_range(struct __sk_buff, data_meta):
6048 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
6052 return bpf_skb_is_valid_access(off, size, type, prog, info);
6055 static u32 bpf_convert_ctx_access(enum bpf_access_type type,
6056 const struct bpf_insn *si,
6057 struct bpf_insn *insn_buf,
6058 struct bpf_prog *prog, u32 *target_size)
6060 struct bpf_insn *insn = insn_buf;
6064 case offsetof(struct __sk_buff, len):
6065 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6066 bpf_target_off(struct sk_buff, len, 4,
6070 case offsetof(struct __sk_buff, protocol):
6071 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
6072 bpf_target_off(struct sk_buff, protocol, 2,
6076 case offsetof(struct __sk_buff, vlan_proto):
6077 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
6078 bpf_target_off(struct sk_buff, vlan_proto, 2,
6082 case offsetof(struct __sk_buff, priority):
6083 if (type == BPF_WRITE)
6084 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
6085 bpf_target_off(struct sk_buff, priority, 4,
6088 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6089 bpf_target_off(struct sk_buff, priority, 4,
6093 case offsetof(struct __sk_buff, ingress_ifindex):
6094 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6095 bpf_target_off(struct sk_buff, skb_iif, 4,
6099 case offsetof(struct __sk_buff, ifindex):
6100 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
6101 si->dst_reg, si->src_reg,
6102 offsetof(struct sk_buff, dev));
6103 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
6104 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6105 bpf_target_off(struct net_device, ifindex, 4,
6109 case offsetof(struct __sk_buff, hash):
6110 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6111 bpf_target_off(struct sk_buff, hash, 4,
6115 case offsetof(struct __sk_buff, mark):
6116 if (type == BPF_WRITE)
6117 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
6118 bpf_target_off(struct sk_buff, mark, 4,
6121 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6122 bpf_target_off(struct sk_buff, mark, 4,
6126 case offsetof(struct __sk_buff, pkt_type):
6128 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->src_reg,
6130 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, PKT_TYPE_MAX);
6131 #ifdef __BIG_ENDIAN_BITFIELD
6132 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, 5);
6136 case offsetof(struct __sk_buff, queue_mapping):
6137 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
6138 bpf_target_off(struct sk_buff, queue_mapping, 2,
6142 case offsetof(struct __sk_buff, vlan_present):
6143 case offsetof(struct __sk_buff, vlan_tci):
6144 BUILD_BUG_ON(VLAN_TAG_PRESENT != 0x1000);
6146 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
6147 bpf_target_off(struct sk_buff, vlan_tci, 2,
6149 if (si->off == offsetof(struct __sk_buff, vlan_tci)) {
6150 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg,
6153 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, 12);
6154 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, 1);
6158 case offsetof(struct __sk_buff, cb[0]) ...
6159 offsetofend(struct __sk_buff, cb[4]) - 1:
6160 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, data) < 20);
6161 BUILD_BUG_ON((offsetof(struct sk_buff, cb) +
6162 offsetof(struct qdisc_skb_cb, data)) %
6165 prog->cb_access = 1;
6167 off -= offsetof(struct __sk_buff, cb[0]);
6168 off += offsetof(struct sk_buff, cb);
6169 off += offsetof(struct qdisc_skb_cb, data);
6170 if (type == BPF_WRITE)
6171 *insn++ = BPF_STX_MEM(BPF_SIZE(si->code), si->dst_reg,
6174 *insn++ = BPF_LDX_MEM(BPF_SIZE(si->code), si->dst_reg,
6178 case offsetof(struct __sk_buff, tc_classid):
6179 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, tc_classid) != 2);
6182 off -= offsetof(struct __sk_buff, tc_classid);
6183 off += offsetof(struct sk_buff, cb);
6184 off += offsetof(struct qdisc_skb_cb, tc_classid);
6186 if (type == BPF_WRITE)
6187 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg,
6190 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg,
6194 case offsetof(struct __sk_buff, data):
6195 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
6196 si->dst_reg, si->src_reg,
6197 offsetof(struct sk_buff, data));
6200 case offsetof(struct __sk_buff, data_meta):
6202 off -= offsetof(struct __sk_buff, data_meta);
6203 off += offsetof(struct sk_buff, cb);
6204 off += offsetof(struct bpf_skb_data_end, data_meta);
6205 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
6209 case offsetof(struct __sk_buff, data_end):
6211 off -= offsetof(struct __sk_buff, data_end);
6212 off += offsetof(struct sk_buff, cb);
6213 off += offsetof(struct bpf_skb_data_end, data_end);
6214 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
6218 case offsetof(struct __sk_buff, tc_index):
6219 #ifdef CONFIG_NET_SCHED
6220 if (type == BPF_WRITE)
6221 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg, si->src_reg,
6222 bpf_target_off(struct sk_buff, tc_index, 2,
6225 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
6226 bpf_target_off(struct sk_buff, tc_index, 2,
6230 if (type == BPF_WRITE)
6231 *insn++ = BPF_MOV64_REG(si->dst_reg, si->dst_reg);
6233 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
6237 case offsetof(struct __sk_buff, napi_id):
6238 #if defined(CONFIG_NET_RX_BUSY_POLL)
6239 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6240 bpf_target_off(struct sk_buff, napi_id, 4,
6242 *insn++ = BPF_JMP_IMM(BPF_JGE, si->dst_reg, MIN_NAPI_ID, 1);
6243 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
6246 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
6249 case offsetof(struct __sk_buff, family):
6250 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_family) != 2);
6252 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
6253 si->dst_reg, si->src_reg,
6254 offsetof(struct sk_buff, sk));
6255 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
6256 bpf_target_off(struct sock_common,
6260 case offsetof(struct __sk_buff, remote_ip4):
6261 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_daddr) != 4);
6263 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
6264 si->dst_reg, si->src_reg,
6265 offsetof(struct sk_buff, sk));
6266 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6267 bpf_target_off(struct sock_common,
6271 case offsetof(struct __sk_buff, local_ip4):
6272 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
6273 skc_rcv_saddr) != 4);
6275 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
6276 si->dst_reg, si->src_reg,
6277 offsetof(struct sk_buff, sk));
6278 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6279 bpf_target_off(struct sock_common,
6283 case offsetof(struct __sk_buff, remote_ip6[0]) ...
6284 offsetof(struct __sk_buff, remote_ip6[3]):
6285 #if IS_ENABLED(CONFIG_IPV6)
6286 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
6287 skc_v6_daddr.s6_addr32[0]) != 4);
6290 off -= offsetof(struct __sk_buff, remote_ip6[0]);
6292 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
6293 si->dst_reg, si->src_reg,
6294 offsetof(struct sk_buff, sk));
6295 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6296 offsetof(struct sock_common,
6297 skc_v6_daddr.s6_addr32[0]) +
6300 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
6303 case offsetof(struct __sk_buff, local_ip6[0]) ...
6304 offsetof(struct __sk_buff, local_ip6[3]):
6305 #if IS_ENABLED(CONFIG_IPV6)
6306 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
6307 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
6310 off -= offsetof(struct __sk_buff, local_ip6[0]);
6312 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
6313 si->dst_reg, si->src_reg,
6314 offsetof(struct sk_buff, sk));
6315 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6316 offsetof(struct sock_common,
6317 skc_v6_rcv_saddr.s6_addr32[0]) +
6320 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
6324 case offsetof(struct __sk_buff, remote_port):
6325 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_dport) != 2);
6327 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
6328 si->dst_reg, si->src_reg,
6329 offsetof(struct sk_buff, sk));
6330 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
6331 bpf_target_off(struct sock_common,
6334 #ifndef __BIG_ENDIAN_BITFIELD
6335 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
6339 case offsetof(struct __sk_buff, local_port):
6340 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_num) != 2);
6342 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
6343 si->dst_reg, si->src_reg,
6344 offsetof(struct sk_buff, sk));
6345 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
6346 bpf_target_off(struct sock_common,
6347 skc_num, 2, target_size));
6350 case offsetof(struct __sk_buff, flow_keys):
6352 off -= offsetof(struct __sk_buff, flow_keys);
6353 off += offsetof(struct sk_buff, cb);
6354 off += offsetof(struct qdisc_skb_cb, flow_keys);
6355 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
6360 return insn - insn_buf;
6363 u32 bpf_sock_convert_ctx_access(enum bpf_access_type type,
6364 const struct bpf_insn *si,
6365 struct bpf_insn *insn_buf,
6366 struct bpf_prog *prog, u32 *target_size)
6368 struct bpf_insn *insn = insn_buf;
6372 case offsetof(struct bpf_sock, bound_dev_if):
6373 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_bound_dev_if) != 4);
6375 if (type == BPF_WRITE)
6376 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
6377 offsetof(struct sock, sk_bound_dev_if));
6379 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6380 offsetof(struct sock, sk_bound_dev_if));
6383 case offsetof(struct bpf_sock, mark):
6384 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_mark) != 4);
6386 if (type == BPF_WRITE)
6387 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
6388 offsetof(struct sock, sk_mark));
6390 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6391 offsetof(struct sock, sk_mark));
6394 case offsetof(struct bpf_sock, priority):
6395 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_priority) != 4);
6397 if (type == BPF_WRITE)
6398 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
6399 offsetof(struct sock, sk_priority));
6401 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6402 offsetof(struct sock, sk_priority));
6405 case offsetof(struct bpf_sock, family):
6406 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_family) != 2);
6408 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
6409 offsetof(struct sock, sk_family));
6412 case offsetof(struct bpf_sock, type):
6413 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6414 offsetof(struct sock, __sk_flags_offset));
6415 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_TYPE_MASK);
6416 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, SK_FL_TYPE_SHIFT);
6419 case offsetof(struct bpf_sock, protocol):
6420 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6421 offsetof(struct sock, __sk_flags_offset));
6422 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_PROTO_MASK);
6423 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, SK_FL_PROTO_SHIFT);
6426 case offsetof(struct bpf_sock, src_ip4):
6427 *insn++ = BPF_LDX_MEM(
6428 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
6429 bpf_target_off(struct sock_common, skc_rcv_saddr,
6430 FIELD_SIZEOF(struct sock_common,
6435 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
6436 #if IS_ENABLED(CONFIG_IPV6)
6438 off -= offsetof(struct bpf_sock, src_ip6[0]);
6439 *insn++ = BPF_LDX_MEM(
6440 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
6443 skc_v6_rcv_saddr.s6_addr32[0],
6444 FIELD_SIZEOF(struct sock_common,
6445 skc_v6_rcv_saddr.s6_addr32[0]),
6446 target_size) + off);
6449 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
6453 case offsetof(struct bpf_sock, src_port):
6454 *insn++ = BPF_LDX_MEM(
6455 BPF_FIELD_SIZEOF(struct sock_common, skc_num),
6456 si->dst_reg, si->src_reg,
6457 bpf_target_off(struct sock_common, skc_num,
6458 FIELD_SIZEOF(struct sock_common,
6464 return insn - insn_buf;
6467 static u32 tc_cls_act_convert_ctx_access(enum bpf_access_type type,
6468 const struct bpf_insn *si,
6469 struct bpf_insn *insn_buf,
6470 struct bpf_prog *prog, u32 *target_size)
6472 struct bpf_insn *insn = insn_buf;
6475 case offsetof(struct __sk_buff, ifindex):
6476 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
6477 si->dst_reg, si->src_reg,
6478 offsetof(struct sk_buff, dev));
6479 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6480 bpf_target_off(struct net_device, ifindex, 4,
6484 return bpf_convert_ctx_access(type, si, insn_buf, prog,
6488 return insn - insn_buf;
6491 static u32 xdp_convert_ctx_access(enum bpf_access_type type,
6492 const struct bpf_insn *si,
6493 struct bpf_insn *insn_buf,
6494 struct bpf_prog *prog, u32 *target_size)
6496 struct bpf_insn *insn = insn_buf;
6499 case offsetof(struct xdp_md, data):
6500 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data),
6501 si->dst_reg, si->src_reg,
6502 offsetof(struct xdp_buff, data));
6504 case offsetof(struct xdp_md, data_meta):
6505 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_meta),
6506 si->dst_reg, si->src_reg,
6507 offsetof(struct xdp_buff, data_meta));
6509 case offsetof(struct xdp_md, data_end):
6510 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_end),
6511 si->dst_reg, si->src_reg,
6512 offsetof(struct xdp_buff, data_end));
6514 case offsetof(struct xdp_md, ingress_ifindex):
6515 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, rxq),
6516 si->dst_reg, si->src_reg,
6517 offsetof(struct xdp_buff, rxq));
6518 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_rxq_info, dev),
6519 si->dst_reg, si->dst_reg,
6520 offsetof(struct xdp_rxq_info, dev));
6521 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6522 offsetof(struct net_device, ifindex));
6524 case offsetof(struct xdp_md, rx_queue_index):
6525 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, rxq),
6526 si->dst_reg, si->src_reg,
6527 offsetof(struct xdp_buff, rxq));
6528 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6529 offsetof(struct xdp_rxq_info,
6534 return insn - insn_buf;
6537 /* SOCK_ADDR_LOAD_NESTED_FIELD() loads Nested Field S.F.NF where S is type of
6538 * context Structure, F is Field in context structure that contains a pointer
6539 * to Nested Structure of type NS that has the field NF.
6541 * SIZE encodes the load size (BPF_B, BPF_H, etc). It's up to caller to make
6542 * sure that SIZE is not greater than actual size of S.F.NF.
6544 * If offset OFF is provided, the load happens from that offset relative to
6547 #define SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, SIZE, OFF) \
6549 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), si->dst_reg, \
6550 si->src_reg, offsetof(S, F)); \
6551 *insn++ = BPF_LDX_MEM( \
6552 SIZE, si->dst_reg, si->dst_reg, \
6553 bpf_target_off(NS, NF, FIELD_SIZEOF(NS, NF), \
6558 #define SOCK_ADDR_LOAD_NESTED_FIELD(S, NS, F, NF) \
6559 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, \
6560 BPF_FIELD_SIZEOF(NS, NF), 0)
6562 /* SOCK_ADDR_STORE_NESTED_FIELD_OFF() has semantic similar to
6563 * SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF() but for store operation.
6565 * It doesn't support SIZE argument though since narrow stores are not
6566 * supported for now.
6568 * In addition it uses Temporary Field TF (member of struct S) as the 3rd
6569 * "register" since two registers available in convert_ctx_access are not
6570 * enough: we can't override neither SRC, since it contains value to store, nor
6571 * DST since it contains pointer to context that may be used by later
6572 * instructions. But we need a temporary place to save pointer to nested
6573 * structure whose field we want to store to.
6575 #define SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, OFF, TF) \
6577 int tmp_reg = BPF_REG_9; \
6578 if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg) \
6580 if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg) \
6582 *insn++ = BPF_STX_MEM(BPF_DW, si->dst_reg, tmp_reg, \
6584 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), tmp_reg, \
6585 si->dst_reg, offsetof(S, F)); \
6586 *insn++ = BPF_STX_MEM( \
6587 BPF_FIELD_SIZEOF(NS, NF), tmp_reg, si->src_reg, \
6588 bpf_target_off(NS, NF, FIELD_SIZEOF(NS, NF), \
6591 *insn++ = BPF_LDX_MEM(BPF_DW, tmp_reg, si->dst_reg, \
6595 #define SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, SIZE, OFF, \
6598 if (type == BPF_WRITE) { \
6599 SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, OFF, \
6602 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF( \
6603 S, NS, F, NF, SIZE, OFF); \
6607 #define SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD(S, NS, F, NF, TF) \
6608 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF( \
6609 S, NS, F, NF, BPF_FIELD_SIZEOF(NS, NF), 0, TF)
6611 static u32 sock_addr_convert_ctx_access(enum bpf_access_type type,
6612 const struct bpf_insn *si,
6613 struct bpf_insn *insn_buf,
6614 struct bpf_prog *prog, u32 *target_size)
6616 struct bpf_insn *insn = insn_buf;
6620 case offsetof(struct bpf_sock_addr, user_family):
6621 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
6622 struct sockaddr, uaddr, sa_family);
6625 case offsetof(struct bpf_sock_addr, user_ip4):
6626 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
6627 struct bpf_sock_addr_kern, struct sockaddr_in, uaddr,
6628 sin_addr, BPF_SIZE(si->code), 0, tmp_reg);
6631 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
6633 off -= offsetof(struct bpf_sock_addr, user_ip6[0]);
6634 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
6635 struct bpf_sock_addr_kern, struct sockaddr_in6, uaddr,
6636 sin6_addr.s6_addr32[0], BPF_SIZE(si->code), off,
6640 case offsetof(struct bpf_sock_addr, user_port):
6641 /* To get port we need to know sa_family first and then treat
6642 * sockaddr as either sockaddr_in or sockaddr_in6.
6643 * Though we can simplify since port field has same offset and
6644 * size in both structures.
6645 * Here we check this invariant and use just one of the
6646 * structures if it's true.
6648 BUILD_BUG_ON(offsetof(struct sockaddr_in, sin_port) !=
6649 offsetof(struct sockaddr_in6, sin6_port));
6650 BUILD_BUG_ON(FIELD_SIZEOF(struct sockaddr_in, sin_port) !=
6651 FIELD_SIZEOF(struct sockaddr_in6, sin6_port));
6652 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD(struct bpf_sock_addr_kern,
6653 struct sockaddr_in6, uaddr,
6654 sin6_port, tmp_reg);
6657 case offsetof(struct bpf_sock_addr, family):
6658 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
6659 struct sock, sk, sk_family);
6662 case offsetof(struct bpf_sock_addr, type):
6663 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(
6664 struct bpf_sock_addr_kern, struct sock, sk,
6665 __sk_flags_offset, BPF_W, 0);
6666 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_TYPE_MASK);
6667 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, SK_FL_TYPE_SHIFT);
6670 case offsetof(struct bpf_sock_addr, protocol):
6671 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(
6672 struct bpf_sock_addr_kern, struct sock, sk,
6673 __sk_flags_offset, BPF_W, 0);
6674 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_PROTO_MASK);
6675 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg,
6679 case offsetof(struct bpf_sock_addr, msg_src_ip4):
6680 /* Treat t_ctx as struct in_addr for msg_src_ip4. */
6681 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
6682 struct bpf_sock_addr_kern, struct in_addr, t_ctx,
6683 s_addr, BPF_SIZE(si->code), 0, tmp_reg);
6686 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
6689 off -= offsetof(struct bpf_sock_addr, msg_src_ip6[0]);
6690 /* Treat t_ctx as struct in6_addr for msg_src_ip6. */
6691 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
6692 struct bpf_sock_addr_kern, struct in6_addr, t_ctx,
6693 s6_addr32[0], BPF_SIZE(si->code), off, tmp_reg);
6697 return insn - insn_buf;
6700 static u32 sock_ops_convert_ctx_access(enum bpf_access_type type,
6701 const struct bpf_insn *si,
6702 struct bpf_insn *insn_buf,
6703 struct bpf_prog *prog,
6706 struct bpf_insn *insn = insn_buf;
6710 case offsetof(struct bpf_sock_ops, op) ...
6711 offsetof(struct bpf_sock_ops, replylong[3]):
6712 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, op) !=
6713 FIELD_SIZEOF(struct bpf_sock_ops_kern, op));
6714 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, reply) !=
6715 FIELD_SIZEOF(struct bpf_sock_ops_kern, reply));
6716 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, replylong) !=
6717 FIELD_SIZEOF(struct bpf_sock_ops_kern, replylong));
6719 off -= offsetof(struct bpf_sock_ops, op);
6720 off += offsetof(struct bpf_sock_ops_kern, op);
6721 if (type == BPF_WRITE)
6722 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
6725 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6729 case offsetof(struct bpf_sock_ops, family):
6730 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_family) != 2);
6732 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6733 struct bpf_sock_ops_kern, sk),
6734 si->dst_reg, si->src_reg,
6735 offsetof(struct bpf_sock_ops_kern, sk));
6736 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
6737 offsetof(struct sock_common, skc_family));
6740 case offsetof(struct bpf_sock_ops, remote_ip4):
6741 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_daddr) != 4);
6743 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6744 struct bpf_sock_ops_kern, sk),
6745 si->dst_reg, si->src_reg,
6746 offsetof(struct bpf_sock_ops_kern, sk));
6747 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6748 offsetof(struct sock_common, skc_daddr));
6751 case offsetof(struct bpf_sock_ops, local_ip4):
6752 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
6753 skc_rcv_saddr) != 4);
6755 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6756 struct bpf_sock_ops_kern, sk),
6757 si->dst_reg, si->src_reg,
6758 offsetof(struct bpf_sock_ops_kern, sk));
6759 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6760 offsetof(struct sock_common,
6764 case offsetof(struct bpf_sock_ops, remote_ip6[0]) ...
6765 offsetof(struct bpf_sock_ops, remote_ip6[3]):
6766 #if IS_ENABLED(CONFIG_IPV6)
6767 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
6768 skc_v6_daddr.s6_addr32[0]) != 4);
6771 off -= offsetof(struct bpf_sock_ops, remote_ip6[0]);
6772 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6773 struct bpf_sock_ops_kern, sk),
6774 si->dst_reg, si->src_reg,
6775 offsetof(struct bpf_sock_ops_kern, sk));
6776 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6777 offsetof(struct sock_common,
6778 skc_v6_daddr.s6_addr32[0]) +
6781 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
6785 case offsetof(struct bpf_sock_ops, local_ip6[0]) ...
6786 offsetof(struct bpf_sock_ops, local_ip6[3]):
6787 #if IS_ENABLED(CONFIG_IPV6)
6788 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
6789 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
6792 off -= offsetof(struct bpf_sock_ops, local_ip6[0]);
6793 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6794 struct bpf_sock_ops_kern, sk),
6795 si->dst_reg, si->src_reg,
6796 offsetof(struct bpf_sock_ops_kern, sk));
6797 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6798 offsetof(struct sock_common,
6799 skc_v6_rcv_saddr.s6_addr32[0]) +
6802 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
6806 case offsetof(struct bpf_sock_ops, remote_port):
6807 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_dport) != 2);
6809 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6810 struct bpf_sock_ops_kern, sk),
6811 si->dst_reg, si->src_reg,
6812 offsetof(struct bpf_sock_ops_kern, sk));
6813 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
6814 offsetof(struct sock_common, skc_dport));
6815 #ifndef __BIG_ENDIAN_BITFIELD
6816 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
6820 case offsetof(struct bpf_sock_ops, local_port):
6821 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_num) != 2);
6823 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6824 struct bpf_sock_ops_kern, sk),
6825 si->dst_reg, si->src_reg,
6826 offsetof(struct bpf_sock_ops_kern, sk));
6827 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
6828 offsetof(struct sock_common, skc_num));
6831 case offsetof(struct bpf_sock_ops, is_fullsock):
6832 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6833 struct bpf_sock_ops_kern,
6835 si->dst_reg, si->src_reg,
6836 offsetof(struct bpf_sock_ops_kern,
6840 case offsetof(struct bpf_sock_ops, state):
6841 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_state) != 1);
6843 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6844 struct bpf_sock_ops_kern, sk),
6845 si->dst_reg, si->src_reg,
6846 offsetof(struct bpf_sock_ops_kern, sk));
6847 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->dst_reg,
6848 offsetof(struct sock_common, skc_state));
6851 case offsetof(struct bpf_sock_ops, rtt_min):
6852 BUILD_BUG_ON(FIELD_SIZEOF(struct tcp_sock, rtt_min) !=
6853 sizeof(struct minmax));
6854 BUILD_BUG_ON(sizeof(struct minmax) <
6855 sizeof(struct minmax_sample));
6857 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6858 struct bpf_sock_ops_kern, sk),
6859 si->dst_reg, si->src_reg,
6860 offsetof(struct bpf_sock_ops_kern, sk));
6861 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6862 offsetof(struct tcp_sock, rtt_min) +
6863 FIELD_SIZEOF(struct minmax_sample, t));
6866 /* Helper macro for adding read access to tcp_sock or sock fields. */
6867 #define SOCK_OPS_GET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ) \
6869 BUILD_BUG_ON(FIELD_SIZEOF(OBJ, OBJ_FIELD) > \
6870 FIELD_SIZEOF(struct bpf_sock_ops, BPF_FIELD)); \
6871 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
6872 struct bpf_sock_ops_kern, \
6874 si->dst_reg, si->src_reg, \
6875 offsetof(struct bpf_sock_ops_kern, \
6877 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 2); \
6878 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
6879 struct bpf_sock_ops_kern, sk),\
6880 si->dst_reg, si->src_reg, \
6881 offsetof(struct bpf_sock_ops_kern, sk));\
6882 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(OBJ, \
6884 si->dst_reg, si->dst_reg, \
6885 offsetof(OBJ, OBJ_FIELD)); \
6888 /* Helper macro for adding write access to tcp_sock or sock fields.
6889 * The macro is called with two registers, dst_reg which contains a pointer
6890 * to ctx (context) and src_reg which contains the value that should be
6891 * stored. However, we need an additional register since we cannot overwrite
6892 * dst_reg because it may be used later in the program.
6893 * Instead we "borrow" one of the other register. We first save its value
6894 * into a new (temp) field in bpf_sock_ops_kern, use it, and then restore
6895 * it at the end of the macro.
6897 #define SOCK_OPS_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ) \
6899 int reg = BPF_REG_9; \
6900 BUILD_BUG_ON(FIELD_SIZEOF(OBJ, OBJ_FIELD) > \
6901 FIELD_SIZEOF(struct bpf_sock_ops, BPF_FIELD)); \
6902 if (si->dst_reg == reg || si->src_reg == reg) \
6904 if (si->dst_reg == reg || si->src_reg == reg) \
6906 *insn++ = BPF_STX_MEM(BPF_DW, si->dst_reg, reg, \
6907 offsetof(struct bpf_sock_ops_kern, \
6909 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
6910 struct bpf_sock_ops_kern, \
6913 offsetof(struct bpf_sock_ops_kern, \
6915 *insn++ = BPF_JMP_IMM(BPF_JEQ, reg, 0, 2); \
6916 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
6917 struct bpf_sock_ops_kern, sk),\
6919 offsetof(struct bpf_sock_ops_kern, sk));\
6920 *insn++ = BPF_STX_MEM(BPF_FIELD_SIZEOF(OBJ, OBJ_FIELD), \
6922 offsetof(OBJ, OBJ_FIELD)); \
6923 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->dst_reg, \
6924 offsetof(struct bpf_sock_ops_kern, \
6928 #define SOCK_OPS_GET_OR_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ, TYPE) \
6930 if (TYPE == BPF_WRITE) \
6931 SOCK_OPS_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ); \
6933 SOCK_OPS_GET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ); \
6936 case offsetof(struct bpf_sock_ops, snd_cwnd):
6937 SOCK_OPS_GET_FIELD(snd_cwnd, snd_cwnd, struct tcp_sock);
6940 case offsetof(struct bpf_sock_ops, srtt_us):
6941 SOCK_OPS_GET_FIELD(srtt_us, srtt_us, struct tcp_sock);
6944 case offsetof(struct bpf_sock_ops, bpf_sock_ops_cb_flags):
6945 SOCK_OPS_GET_FIELD(bpf_sock_ops_cb_flags, bpf_sock_ops_cb_flags,
6949 case offsetof(struct bpf_sock_ops, snd_ssthresh):
6950 SOCK_OPS_GET_FIELD(snd_ssthresh, snd_ssthresh, struct tcp_sock);
6953 case offsetof(struct bpf_sock_ops, rcv_nxt):
6954 SOCK_OPS_GET_FIELD(rcv_nxt, rcv_nxt, struct tcp_sock);
6957 case offsetof(struct bpf_sock_ops, snd_nxt):
6958 SOCK_OPS_GET_FIELD(snd_nxt, snd_nxt, struct tcp_sock);
6961 case offsetof(struct bpf_sock_ops, snd_una):
6962 SOCK_OPS_GET_FIELD(snd_una, snd_una, struct tcp_sock);
6965 case offsetof(struct bpf_sock_ops, mss_cache):
6966 SOCK_OPS_GET_FIELD(mss_cache, mss_cache, struct tcp_sock);
6969 case offsetof(struct bpf_sock_ops, ecn_flags):
6970 SOCK_OPS_GET_FIELD(ecn_flags, ecn_flags, struct tcp_sock);
6973 case offsetof(struct bpf_sock_ops, rate_delivered):
6974 SOCK_OPS_GET_FIELD(rate_delivered, rate_delivered,
6978 case offsetof(struct bpf_sock_ops, rate_interval_us):
6979 SOCK_OPS_GET_FIELD(rate_interval_us, rate_interval_us,
6983 case offsetof(struct bpf_sock_ops, packets_out):
6984 SOCK_OPS_GET_FIELD(packets_out, packets_out, struct tcp_sock);
6987 case offsetof(struct bpf_sock_ops, retrans_out):
6988 SOCK_OPS_GET_FIELD(retrans_out, retrans_out, struct tcp_sock);
6991 case offsetof(struct bpf_sock_ops, total_retrans):
6992 SOCK_OPS_GET_FIELD(total_retrans, total_retrans,
6996 case offsetof(struct bpf_sock_ops, segs_in):
6997 SOCK_OPS_GET_FIELD(segs_in, segs_in, struct tcp_sock);
7000 case offsetof(struct bpf_sock_ops, data_segs_in):
7001 SOCK_OPS_GET_FIELD(data_segs_in, data_segs_in, struct tcp_sock);
7004 case offsetof(struct bpf_sock_ops, segs_out):
7005 SOCK_OPS_GET_FIELD(segs_out, segs_out, struct tcp_sock);
7008 case offsetof(struct bpf_sock_ops, data_segs_out):
7009 SOCK_OPS_GET_FIELD(data_segs_out, data_segs_out,
7013 case offsetof(struct bpf_sock_ops, lost_out):
7014 SOCK_OPS_GET_FIELD(lost_out, lost_out, struct tcp_sock);
7017 case offsetof(struct bpf_sock_ops, sacked_out):
7018 SOCK_OPS_GET_FIELD(sacked_out, sacked_out, struct tcp_sock);
7021 case offsetof(struct bpf_sock_ops, sk_txhash):
7022 SOCK_OPS_GET_OR_SET_FIELD(sk_txhash, sk_txhash,
7026 case offsetof(struct bpf_sock_ops, bytes_received):
7027 SOCK_OPS_GET_FIELD(bytes_received, bytes_received,
7031 case offsetof(struct bpf_sock_ops, bytes_acked):
7032 SOCK_OPS_GET_FIELD(bytes_acked, bytes_acked, struct tcp_sock);
7036 return insn - insn_buf;
7039 static u32 sk_skb_convert_ctx_access(enum bpf_access_type type,
7040 const struct bpf_insn *si,
7041 struct bpf_insn *insn_buf,
7042 struct bpf_prog *prog, u32 *target_size)
7044 struct bpf_insn *insn = insn_buf;
7048 case offsetof(struct __sk_buff, data_end):
7050 off -= offsetof(struct __sk_buff, data_end);
7051 off += offsetof(struct sk_buff, cb);
7052 off += offsetof(struct tcp_skb_cb, bpf.data_end);
7053 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
7057 return bpf_convert_ctx_access(type, si, insn_buf, prog,
7061 return insn - insn_buf;
7064 static u32 sk_msg_convert_ctx_access(enum bpf_access_type type,
7065 const struct bpf_insn *si,
7066 struct bpf_insn *insn_buf,
7067 struct bpf_prog *prog, u32 *target_size)
7069 struct bpf_insn *insn = insn_buf;
7070 #if IS_ENABLED(CONFIG_IPV6)
7075 case offsetof(struct sk_msg_md, data):
7076 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg, data),
7077 si->dst_reg, si->src_reg,
7078 offsetof(struct sk_msg, data));
7080 case offsetof(struct sk_msg_md, data_end):
7081 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg, data_end),
7082 si->dst_reg, si->src_reg,
7083 offsetof(struct sk_msg, data_end));
7085 case offsetof(struct sk_msg_md, family):
7086 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_family) != 2);
7088 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7090 si->dst_reg, si->src_reg,
7091 offsetof(struct sk_msg, sk));
7092 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
7093 offsetof(struct sock_common, skc_family));
7096 case offsetof(struct sk_msg_md, remote_ip4):
7097 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_daddr) != 4);
7099 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7101 si->dst_reg, si->src_reg,
7102 offsetof(struct sk_msg, sk));
7103 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7104 offsetof(struct sock_common, skc_daddr));
7107 case offsetof(struct sk_msg_md, local_ip4):
7108 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
7109 skc_rcv_saddr) != 4);
7111 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7113 si->dst_reg, si->src_reg,
7114 offsetof(struct sk_msg, sk));
7115 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7116 offsetof(struct sock_common,
7120 case offsetof(struct sk_msg_md, remote_ip6[0]) ...
7121 offsetof(struct sk_msg_md, remote_ip6[3]):
7122 #if IS_ENABLED(CONFIG_IPV6)
7123 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
7124 skc_v6_daddr.s6_addr32[0]) != 4);
7127 off -= offsetof(struct sk_msg_md, remote_ip6[0]);
7128 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7130 si->dst_reg, si->src_reg,
7131 offsetof(struct sk_msg, sk));
7132 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7133 offsetof(struct sock_common,
7134 skc_v6_daddr.s6_addr32[0]) +
7137 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
7141 case offsetof(struct sk_msg_md, local_ip6[0]) ...
7142 offsetof(struct sk_msg_md, local_ip6[3]):
7143 #if IS_ENABLED(CONFIG_IPV6)
7144 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
7145 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
7148 off -= offsetof(struct sk_msg_md, local_ip6[0]);
7149 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7151 si->dst_reg, si->src_reg,
7152 offsetof(struct sk_msg, sk));
7153 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
7154 offsetof(struct sock_common,
7155 skc_v6_rcv_saddr.s6_addr32[0]) +
7158 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
7162 case offsetof(struct sk_msg_md, remote_port):
7163 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_dport) != 2);
7165 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7167 si->dst_reg, si->src_reg,
7168 offsetof(struct sk_msg, sk));
7169 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
7170 offsetof(struct sock_common, skc_dport));
7171 #ifndef __BIG_ENDIAN_BITFIELD
7172 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
7176 case offsetof(struct sk_msg_md, local_port):
7177 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_num) != 2);
7179 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
7181 si->dst_reg, si->src_reg,
7182 offsetof(struct sk_msg, sk));
7183 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
7184 offsetof(struct sock_common, skc_num));
7188 return insn - insn_buf;
7191 const struct bpf_verifier_ops sk_filter_verifier_ops = {
7192 .get_func_proto = sk_filter_func_proto,
7193 .is_valid_access = sk_filter_is_valid_access,
7194 .convert_ctx_access = bpf_convert_ctx_access,
7195 .gen_ld_abs = bpf_gen_ld_abs,
7198 const struct bpf_prog_ops sk_filter_prog_ops = {
7199 .test_run = bpf_prog_test_run_skb,
7202 const struct bpf_verifier_ops tc_cls_act_verifier_ops = {
7203 .get_func_proto = tc_cls_act_func_proto,
7204 .is_valid_access = tc_cls_act_is_valid_access,
7205 .convert_ctx_access = tc_cls_act_convert_ctx_access,
7206 .gen_prologue = tc_cls_act_prologue,
7207 .gen_ld_abs = bpf_gen_ld_abs,
7210 const struct bpf_prog_ops tc_cls_act_prog_ops = {
7211 .test_run = bpf_prog_test_run_skb,
7214 const struct bpf_verifier_ops xdp_verifier_ops = {
7215 .get_func_proto = xdp_func_proto,
7216 .is_valid_access = xdp_is_valid_access,
7217 .convert_ctx_access = xdp_convert_ctx_access,
7218 .gen_prologue = bpf_noop_prologue,
7221 const struct bpf_prog_ops xdp_prog_ops = {
7222 .test_run = bpf_prog_test_run_xdp,
7225 const struct bpf_verifier_ops cg_skb_verifier_ops = {
7226 .get_func_proto = cg_skb_func_proto,
7227 .is_valid_access = cg_skb_is_valid_access,
7228 .convert_ctx_access = bpf_convert_ctx_access,
7231 const struct bpf_prog_ops cg_skb_prog_ops = {
7232 .test_run = bpf_prog_test_run_skb,
7235 const struct bpf_verifier_ops lwt_in_verifier_ops = {
7236 .get_func_proto = lwt_in_func_proto,
7237 .is_valid_access = lwt_is_valid_access,
7238 .convert_ctx_access = bpf_convert_ctx_access,
7241 const struct bpf_prog_ops lwt_in_prog_ops = {
7242 .test_run = bpf_prog_test_run_skb,
7245 const struct bpf_verifier_ops lwt_out_verifier_ops = {
7246 .get_func_proto = lwt_out_func_proto,
7247 .is_valid_access = lwt_is_valid_access,
7248 .convert_ctx_access = bpf_convert_ctx_access,
7251 const struct bpf_prog_ops lwt_out_prog_ops = {
7252 .test_run = bpf_prog_test_run_skb,
7255 const struct bpf_verifier_ops lwt_xmit_verifier_ops = {
7256 .get_func_proto = lwt_xmit_func_proto,
7257 .is_valid_access = lwt_is_valid_access,
7258 .convert_ctx_access = bpf_convert_ctx_access,
7259 .gen_prologue = tc_cls_act_prologue,
7262 const struct bpf_prog_ops lwt_xmit_prog_ops = {
7263 .test_run = bpf_prog_test_run_skb,
7266 const struct bpf_verifier_ops lwt_seg6local_verifier_ops = {
7267 .get_func_proto = lwt_seg6local_func_proto,
7268 .is_valid_access = lwt_is_valid_access,
7269 .convert_ctx_access = bpf_convert_ctx_access,
7272 const struct bpf_prog_ops lwt_seg6local_prog_ops = {
7273 .test_run = bpf_prog_test_run_skb,
7276 const struct bpf_verifier_ops cg_sock_verifier_ops = {
7277 .get_func_proto = sock_filter_func_proto,
7278 .is_valid_access = sock_filter_is_valid_access,
7279 .convert_ctx_access = bpf_sock_convert_ctx_access,
7282 const struct bpf_prog_ops cg_sock_prog_ops = {
7285 const struct bpf_verifier_ops cg_sock_addr_verifier_ops = {
7286 .get_func_proto = sock_addr_func_proto,
7287 .is_valid_access = sock_addr_is_valid_access,
7288 .convert_ctx_access = sock_addr_convert_ctx_access,
7291 const struct bpf_prog_ops cg_sock_addr_prog_ops = {
7294 const struct bpf_verifier_ops sock_ops_verifier_ops = {
7295 .get_func_proto = sock_ops_func_proto,
7296 .is_valid_access = sock_ops_is_valid_access,
7297 .convert_ctx_access = sock_ops_convert_ctx_access,
7300 const struct bpf_prog_ops sock_ops_prog_ops = {
7303 const struct bpf_verifier_ops sk_skb_verifier_ops = {
7304 .get_func_proto = sk_skb_func_proto,
7305 .is_valid_access = sk_skb_is_valid_access,
7306 .convert_ctx_access = sk_skb_convert_ctx_access,
7307 .gen_prologue = sk_skb_prologue,
7310 const struct bpf_prog_ops sk_skb_prog_ops = {
7313 const struct bpf_verifier_ops sk_msg_verifier_ops = {
7314 .get_func_proto = sk_msg_func_proto,
7315 .is_valid_access = sk_msg_is_valid_access,
7316 .convert_ctx_access = sk_msg_convert_ctx_access,
7317 .gen_prologue = bpf_noop_prologue,
7320 const struct bpf_prog_ops sk_msg_prog_ops = {
7323 const struct bpf_verifier_ops flow_dissector_verifier_ops = {
7324 .get_func_proto = flow_dissector_func_proto,
7325 .is_valid_access = flow_dissector_is_valid_access,
7326 .convert_ctx_access = bpf_convert_ctx_access,
7329 const struct bpf_prog_ops flow_dissector_prog_ops = {
7332 int sk_detach_filter(struct sock *sk)
7335 struct sk_filter *filter;
7337 if (sock_flag(sk, SOCK_FILTER_LOCKED))
7340 filter = rcu_dereference_protected(sk->sk_filter,
7341 lockdep_sock_is_held(sk));
7343 RCU_INIT_POINTER(sk->sk_filter, NULL);
7344 sk_filter_uncharge(sk, filter);
7350 EXPORT_SYMBOL_GPL(sk_detach_filter);
7352 int sk_get_filter(struct sock *sk, struct sock_filter __user *ubuf,
7355 struct sock_fprog_kern *fprog;
7356 struct sk_filter *filter;
7360 filter = rcu_dereference_protected(sk->sk_filter,
7361 lockdep_sock_is_held(sk));
7365 /* We're copying the filter that has been originally attached,
7366 * so no conversion/decode needed anymore. eBPF programs that
7367 * have no original program cannot be dumped through this.
7370 fprog = filter->prog->orig_prog;
7376 /* User space only enquires number of filter blocks. */
7380 if (len < fprog->len)
7384 if (copy_to_user(ubuf, fprog->filter, bpf_classic_proglen(fprog)))
7387 /* Instead of bytes, the API requests to return the number
7397 struct sk_reuseport_kern {
7398 struct sk_buff *skb;
7400 struct sock *selected_sk;
7407 static void bpf_init_reuseport_kern(struct sk_reuseport_kern *reuse_kern,
7408 struct sock_reuseport *reuse,
7409 struct sock *sk, struct sk_buff *skb,
7412 reuse_kern->skb = skb;
7413 reuse_kern->sk = sk;
7414 reuse_kern->selected_sk = NULL;
7415 reuse_kern->data_end = skb->data + skb_headlen(skb);
7416 reuse_kern->hash = hash;
7417 reuse_kern->reuseport_id = reuse->reuseport_id;
7418 reuse_kern->bind_inany = reuse->bind_inany;
7421 struct sock *bpf_run_sk_reuseport(struct sock_reuseport *reuse, struct sock *sk,
7422 struct bpf_prog *prog, struct sk_buff *skb,
7425 struct sk_reuseport_kern reuse_kern;
7426 enum sk_action action;
7428 bpf_init_reuseport_kern(&reuse_kern, reuse, sk, skb, hash);
7429 action = BPF_PROG_RUN(prog, &reuse_kern);
7431 if (action == SK_PASS)
7432 return reuse_kern.selected_sk;
7434 return ERR_PTR(-ECONNREFUSED);
7437 BPF_CALL_4(sk_select_reuseport, struct sk_reuseport_kern *, reuse_kern,
7438 struct bpf_map *, map, void *, key, u32, flags)
7440 struct sock_reuseport *reuse;
7441 struct sock *selected_sk;
7443 selected_sk = map->ops->map_lookup_elem(map, key);
7447 reuse = rcu_dereference(selected_sk->sk_reuseport_cb);
7449 /* selected_sk is unhashed (e.g. by close()) after the
7450 * above map_lookup_elem(). Treat selected_sk has already
7451 * been removed from the map.
7455 if (unlikely(reuse->reuseport_id != reuse_kern->reuseport_id)) {
7458 if (unlikely(!reuse_kern->reuseport_id))
7459 /* There is a small race between adding the
7460 * sk to the map and setting the
7461 * reuse_kern->reuseport_id.
7462 * Treat it as the sk has not been added to
7467 sk = reuse_kern->sk;
7468 if (sk->sk_protocol != selected_sk->sk_protocol)
7470 else if (sk->sk_family != selected_sk->sk_family)
7471 return -EAFNOSUPPORT;
7473 /* Catch all. Likely bound to a different sockaddr. */
7477 reuse_kern->selected_sk = selected_sk;
7482 static const struct bpf_func_proto sk_select_reuseport_proto = {
7483 .func = sk_select_reuseport,
7485 .ret_type = RET_INTEGER,
7486 .arg1_type = ARG_PTR_TO_CTX,
7487 .arg2_type = ARG_CONST_MAP_PTR,
7488 .arg3_type = ARG_PTR_TO_MAP_KEY,
7489 .arg4_type = ARG_ANYTHING,
7492 BPF_CALL_4(sk_reuseport_load_bytes,
7493 const struct sk_reuseport_kern *, reuse_kern, u32, offset,
7494 void *, to, u32, len)
7496 return ____bpf_skb_load_bytes(reuse_kern->skb, offset, to, len);
7499 static const struct bpf_func_proto sk_reuseport_load_bytes_proto = {
7500 .func = sk_reuseport_load_bytes,
7502 .ret_type = RET_INTEGER,
7503 .arg1_type = ARG_PTR_TO_CTX,
7504 .arg2_type = ARG_ANYTHING,
7505 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
7506 .arg4_type = ARG_CONST_SIZE,
7509 BPF_CALL_5(sk_reuseport_load_bytes_relative,
7510 const struct sk_reuseport_kern *, reuse_kern, u32, offset,
7511 void *, to, u32, len, u32, start_header)
7513 return ____bpf_skb_load_bytes_relative(reuse_kern->skb, offset, to,
7517 static const struct bpf_func_proto sk_reuseport_load_bytes_relative_proto = {
7518 .func = sk_reuseport_load_bytes_relative,
7520 .ret_type = RET_INTEGER,
7521 .arg1_type = ARG_PTR_TO_CTX,
7522 .arg2_type = ARG_ANYTHING,
7523 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
7524 .arg4_type = ARG_CONST_SIZE,
7525 .arg5_type = ARG_ANYTHING,
7528 static const struct bpf_func_proto *
7529 sk_reuseport_func_proto(enum bpf_func_id func_id,
7530 const struct bpf_prog *prog)
7533 case BPF_FUNC_sk_select_reuseport:
7534 return &sk_select_reuseport_proto;
7535 case BPF_FUNC_skb_load_bytes:
7536 return &sk_reuseport_load_bytes_proto;
7537 case BPF_FUNC_skb_load_bytes_relative:
7538 return &sk_reuseport_load_bytes_relative_proto;
7540 return bpf_base_func_proto(func_id);
7545 sk_reuseport_is_valid_access(int off, int size,
7546 enum bpf_access_type type,
7547 const struct bpf_prog *prog,
7548 struct bpf_insn_access_aux *info)
7550 const u32 size_default = sizeof(__u32);
7552 if (off < 0 || off >= sizeof(struct sk_reuseport_md) ||
7553 off % size || type != BPF_READ)
7557 case offsetof(struct sk_reuseport_md, data):
7558 info->reg_type = PTR_TO_PACKET;
7559 return size == sizeof(__u64);
7561 case offsetof(struct sk_reuseport_md, data_end):
7562 info->reg_type = PTR_TO_PACKET_END;
7563 return size == sizeof(__u64);
7565 case offsetof(struct sk_reuseport_md, hash):
7566 return size == size_default;
7568 /* Fields that allow narrowing */
7569 case offsetof(struct sk_reuseport_md, eth_protocol):
7570 if (size < FIELD_SIZEOF(struct sk_buff, protocol))
7573 case offsetof(struct sk_reuseport_md, ip_protocol):
7574 case offsetof(struct sk_reuseport_md, bind_inany):
7575 case offsetof(struct sk_reuseport_md, len):
7576 bpf_ctx_record_field_size(info, size_default);
7577 return bpf_ctx_narrow_access_ok(off, size, size_default);
7584 #define SK_REUSEPORT_LOAD_FIELD(F) ({ \
7585 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_reuseport_kern, F), \
7586 si->dst_reg, si->src_reg, \
7587 bpf_target_off(struct sk_reuseport_kern, F, \
7588 FIELD_SIZEOF(struct sk_reuseport_kern, F), \
7592 #define SK_REUSEPORT_LOAD_SKB_FIELD(SKB_FIELD) \
7593 SOCK_ADDR_LOAD_NESTED_FIELD(struct sk_reuseport_kern, \
7598 #define SK_REUSEPORT_LOAD_SK_FIELD_SIZE_OFF(SK_FIELD, BPF_SIZE, EXTRA_OFF) \
7599 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(struct sk_reuseport_kern, \
7602 SK_FIELD, BPF_SIZE, EXTRA_OFF)
7604 static u32 sk_reuseport_convert_ctx_access(enum bpf_access_type type,
7605 const struct bpf_insn *si,
7606 struct bpf_insn *insn_buf,
7607 struct bpf_prog *prog,
7610 struct bpf_insn *insn = insn_buf;
7613 case offsetof(struct sk_reuseport_md, data):
7614 SK_REUSEPORT_LOAD_SKB_FIELD(data);
7617 case offsetof(struct sk_reuseport_md, len):
7618 SK_REUSEPORT_LOAD_SKB_FIELD(len);
7621 case offsetof(struct sk_reuseport_md, eth_protocol):
7622 SK_REUSEPORT_LOAD_SKB_FIELD(protocol);
7625 case offsetof(struct sk_reuseport_md, ip_protocol):
7626 BUILD_BUG_ON(HWEIGHT32(SK_FL_PROTO_MASK) != BITS_PER_BYTE);
7627 SK_REUSEPORT_LOAD_SK_FIELD_SIZE_OFF(__sk_flags_offset,
7629 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_PROTO_MASK);
7630 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg,
7632 /* SK_FL_PROTO_MASK and SK_FL_PROTO_SHIFT are endian
7633 * aware. No further narrowing or masking is needed.
7638 case offsetof(struct sk_reuseport_md, data_end):
7639 SK_REUSEPORT_LOAD_FIELD(data_end);
7642 case offsetof(struct sk_reuseport_md, hash):
7643 SK_REUSEPORT_LOAD_FIELD(hash);
7646 case offsetof(struct sk_reuseport_md, bind_inany):
7647 SK_REUSEPORT_LOAD_FIELD(bind_inany);
7651 return insn - insn_buf;
7654 const struct bpf_verifier_ops sk_reuseport_verifier_ops = {
7655 .get_func_proto = sk_reuseport_func_proto,
7656 .is_valid_access = sk_reuseport_is_valid_access,
7657 .convert_ctx_access = sk_reuseport_convert_ctx_access,
7660 const struct bpf_prog_ops sk_reuseport_prog_ops = {
7662 #endif /* CONFIG_INET */