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>
42 #include <net/flow_dissector.h>
43 #include <linux/errno.h>
44 #include <linux/timer.h>
45 #include <linux/uaccess.h>
46 #include <asm/unaligned.h>
47 #include <asm/cmpxchg.h>
48 #include <linux/filter.h>
49 #include <linux/ratelimit.h>
50 #include <linux/seccomp.h>
51 #include <linux/if_vlan.h>
52 #include <linux/bpf.h>
53 #include <net/sch_generic.h>
54 #include <net/cls_cgroup.h>
55 #include <net/dst_metadata.h>
57 #include <net/sock_reuseport.h>
58 #include <net/busy_poll.h>
61 #include <linux/bpf_trace.h>
62 #include <net/xdp_sock.h>
63 #include <linux/inetdevice.h>
64 #include <net/ip_fib.h>
68 #include <linux/seg6_local.h>
70 #include <net/seg6_local.h>
73 * sk_filter_trim_cap - run a packet through a socket filter
74 * @sk: sock associated with &sk_buff
75 * @skb: buffer to filter
76 * @cap: limit on how short the eBPF program may trim the packet
78 * Run the eBPF program and then cut skb->data to correct size returned by
79 * the program. If pkt_len is 0 we toss packet. If skb->len is smaller
80 * than pkt_len we keep whole skb->data. This is the socket level
81 * wrapper to BPF_PROG_RUN. It returns 0 if the packet should
82 * be accepted or -EPERM if the packet should be tossed.
85 int sk_filter_trim_cap(struct sock *sk, struct sk_buff *skb, unsigned int cap)
88 struct sk_filter *filter;
91 * If the skb was allocated from pfmemalloc reserves, only
92 * allow SOCK_MEMALLOC sockets to use it as this socket is
95 if (skb_pfmemalloc(skb) && !sock_flag(sk, SOCK_MEMALLOC)) {
96 NET_INC_STATS(sock_net(sk), LINUX_MIB_PFMEMALLOCDROP);
99 err = BPF_CGROUP_RUN_PROG_INET_INGRESS(sk, skb);
103 err = security_sock_rcv_skb(sk, skb);
108 filter = rcu_dereference(sk->sk_filter);
110 struct sock *save_sk = skb->sk;
111 unsigned int pkt_len;
114 pkt_len = bpf_prog_run_save_cb(filter->prog, skb);
116 err = pkt_len ? pskb_trim(skb, max(cap, pkt_len)) : -EPERM;
122 EXPORT_SYMBOL(sk_filter_trim_cap);
124 BPF_CALL_1(bpf_skb_get_pay_offset, struct sk_buff *, skb)
126 return skb_get_poff(skb);
129 BPF_CALL_3(bpf_skb_get_nlattr, struct sk_buff *, skb, u32, a, u32, x)
133 if (skb_is_nonlinear(skb))
136 if (skb->len < sizeof(struct nlattr))
139 if (a > skb->len - sizeof(struct nlattr))
142 nla = nla_find((struct nlattr *) &skb->data[a], skb->len - a, x);
144 return (void *) nla - (void *) skb->data;
149 BPF_CALL_3(bpf_skb_get_nlattr_nest, struct sk_buff *, skb, u32, a, u32, x)
153 if (skb_is_nonlinear(skb))
156 if (skb->len < sizeof(struct nlattr))
159 if (a > skb->len - sizeof(struct nlattr))
162 nla = (struct nlattr *) &skb->data[a];
163 if (nla->nla_len > skb->len - a)
166 nla = nla_find_nested(nla, x);
168 return (void *) nla - (void *) skb->data;
173 BPF_CALL_4(bpf_skb_load_helper_8, const struct sk_buff *, skb, const void *,
174 data, int, headlen, int, offset)
177 const int len = sizeof(tmp);
180 if (headlen - offset >= len)
181 return *(u8 *)(data + offset);
182 if (!skb_copy_bits(skb, offset, &tmp, sizeof(tmp)))
185 ptr = bpf_internal_load_pointer_neg_helper(skb, offset, len);
193 BPF_CALL_2(bpf_skb_load_helper_8_no_cache, const struct sk_buff *, skb,
196 return ____bpf_skb_load_helper_8(skb, skb->data, skb->len - skb->data_len,
200 BPF_CALL_4(bpf_skb_load_helper_16, const struct sk_buff *, skb, const void *,
201 data, int, headlen, int, offset)
204 const int len = sizeof(tmp);
207 if (headlen - offset >= len)
208 return get_unaligned_be16(data + offset);
209 if (!skb_copy_bits(skb, offset, &tmp, sizeof(tmp)))
210 return be16_to_cpu(tmp);
212 ptr = bpf_internal_load_pointer_neg_helper(skb, offset, len);
214 return get_unaligned_be16(ptr);
220 BPF_CALL_2(bpf_skb_load_helper_16_no_cache, const struct sk_buff *, skb,
223 return ____bpf_skb_load_helper_16(skb, skb->data, skb->len - skb->data_len,
227 BPF_CALL_4(bpf_skb_load_helper_32, const struct sk_buff *, skb, const void *,
228 data, int, headlen, int, offset)
231 const int len = sizeof(tmp);
233 if (likely(offset >= 0)) {
234 if (headlen - offset >= len)
235 return get_unaligned_be32(data + offset);
236 if (!skb_copy_bits(skb, offset, &tmp, sizeof(tmp)))
237 return be32_to_cpu(tmp);
239 ptr = bpf_internal_load_pointer_neg_helper(skb, offset, len);
241 return get_unaligned_be32(ptr);
247 BPF_CALL_2(bpf_skb_load_helper_32_no_cache, const struct sk_buff *, skb,
250 return ____bpf_skb_load_helper_32(skb, skb->data, skb->len - skb->data_len,
254 BPF_CALL_0(bpf_get_raw_cpu_id)
256 return raw_smp_processor_id();
259 static const struct bpf_func_proto bpf_get_raw_smp_processor_id_proto = {
260 .func = bpf_get_raw_cpu_id,
262 .ret_type = RET_INTEGER,
265 static u32 convert_skb_access(int skb_field, int dst_reg, int src_reg,
266 struct bpf_insn *insn_buf)
268 struct bpf_insn *insn = insn_buf;
272 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
274 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
275 offsetof(struct sk_buff, mark));
279 *insn++ = BPF_LDX_MEM(BPF_B, dst_reg, src_reg, PKT_TYPE_OFFSET());
280 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, PKT_TYPE_MAX);
281 #ifdef __BIG_ENDIAN_BITFIELD
282 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 5);
287 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, queue_mapping) != 2);
289 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
290 offsetof(struct sk_buff, queue_mapping));
293 case SKF_AD_VLAN_TAG:
294 case SKF_AD_VLAN_TAG_PRESENT:
295 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_tci) != 2);
296 BUILD_BUG_ON(VLAN_TAG_PRESENT != 0x1000);
298 /* dst_reg = *(u16 *) (src_reg + offsetof(vlan_tci)) */
299 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
300 offsetof(struct sk_buff, vlan_tci));
301 if (skb_field == SKF_AD_VLAN_TAG) {
302 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg,
306 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 12);
308 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, 1);
313 return insn - insn_buf;
316 static bool convert_bpf_extensions(struct sock_filter *fp,
317 struct bpf_insn **insnp)
319 struct bpf_insn *insn = *insnp;
323 case SKF_AD_OFF + SKF_AD_PROTOCOL:
324 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2);
326 /* A = *(u16 *) (CTX + offsetof(protocol)) */
327 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
328 offsetof(struct sk_buff, protocol));
329 /* A = ntohs(A) [emitting a nop or swap16] */
330 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
333 case SKF_AD_OFF + SKF_AD_PKTTYPE:
334 cnt = convert_skb_access(SKF_AD_PKTTYPE, BPF_REG_A, BPF_REG_CTX, insn);
338 case SKF_AD_OFF + SKF_AD_IFINDEX:
339 case SKF_AD_OFF + SKF_AD_HATYPE:
340 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, ifindex) != 4);
341 BUILD_BUG_ON(FIELD_SIZEOF(struct net_device, type) != 2);
343 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
344 BPF_REG_TMP, BPF_REG_CTX,
345 offsetof(struct sk_buff, dev));
346 /* if (tmp != 0) goto pc + 1 */
347 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_TMP, 0, 1);
348 *insn++ = BPF_EXIT_INSN();
349 if (fp->k == SKF_AD_OFF + SKF_AD_IFINDEX)
350 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_TMP,
351 offsetof(struct net_device, ifindex));
353 *insn = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_TMP,
354 offsetof(struct net_device, type));
357 case SKF_AD_OFF + SKF_AD_MARK:
358 cnt = convert_skb_access(SKF_AD_MARK, BPF_REG_A, BPF_REG_CTX, insn);
362 case SKF_AD_OFF + SKF_AD_RXHASH:
363 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4);
365 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX,
366 offsetof(struct sk_buff, hash));
369 case SKF_AD_OFF + SKF_AD_QUEUE:
370 cnt = convert_skb_access(SKF_AD_QUEUE, BPF_REG_A, BPF_REG_CTX, insn);
374 case SKF_AD_OFF + SKF_AD_VLAN_TAG:
375 cnt = convert_skb_access(SKF_AD_VLAN_TAG,
376 BPF_REG_A, BPF_REG_CTX, insn);
380 case SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT:
381 cnt = convert_skb_access(SKF_AD_VLAN_TAG_PRESENT,
382 BPF_REG_A, BPF_REG_CTX, insn);
386 case SKF_AD_OFF + SKF_AD_VLAN_TPID:
387 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_proto) != 2);
389 /* A = *(u16 *) (CTX + offsetof(vlan_proto)) */
390 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
391 offsetof(struct sk_buff, vlan_proto));
392 /* A = ntohs(A) [emitting a nop or swap16] */
393 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
396 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
397 case SKF_AD_OFF + SKF_AD_NLATTR:
398 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
399 case SKF_AD_OFF + SKF_AD_CPU:
400 case SKF_AD_OFF + SKF_AD_RANDOM:
402 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX);
404 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_A);
406 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_X);
407 /* Emit call(arg1=CTX, arg2=A, arg3=X) */
409 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
410 *insn = BPF_EMIT_CALL(bpf_skb_get_pay_offset);
412 case SKF_AD_OFF + SKF_AD_NLATTR:
413 *insn = BPF_EMIT_CALL(bpf_skb_get_nlattr);
415 case SKF_AD_OFF + SKF_AD_NLATTR_NEST:
416 *insn = BPF_EMIT_CALL(bpf_skb_get_nlattr_nest);
418 case SKF_AD_OFF + SKF_AD_CPU:
419 *insn = BPF_EMIT_CALL(bpf_get_raw_cpu_id);
421 case SKF_AD_OFF + SKF_AD_RANDOM:
422 *insn = BPF_EMIT_CALL(bpf_user_rnd_u32);
423 bpf_user_rnd_init_once();
428 case SKF_AD_OFF + SKF_AD_ALU_XOR_X:
430 *insn = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_X);
434 /* This is just a dummy call to avoid letting the compiler
435 * evict __bpf_call_base() as an optimization. Placed here
436 * where no-one bothers.
438 BUG_ON(__bpf_call_base(0, 0, 0, 0, 0) != 0);
446 static bool convert_bpf_ld_abs(struct sock_filter *fp, struct bpf_insn **insnp)
448 const bool unaligned_ok = IS_BUILTIN(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS);
449 int size = bpf_size_to_bytes(BPF_SIZE(fp->code));
450 bool endian = BPF_SIZE(fp->code) == BPF_H ||
451 BPF_SIZE(fp->code) == BPF_W;
452 bool indirect = BPF_MODE(fp->code) == BPF_IND;
453 const int ip_align = NET_IP_ALIGN;
454 struct bpf_insn *insn = *insnp;
458 ((unaligned_ok && offset >= 0) ||
459 (!unaligned_ok && offset >= 0 &&
460 offset + ip_align >= 0 &&
461 offset + ip_align % size == 0))) {
462 bool ldx_off_ok = offset <= S16_MAX;
464 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_H);
465 *insn++ = BPF_ALU64_IMM(BPF_SUB, BPF_REG_TMP, offset);
466 *insn++ = BPF_JMP_IMM(BPF_JSLT, BPF_REG_TMP,
467 size, 2 + endian + (!ldx_off_ok * 2));
469 *insn++ = BPF_LDX_MEM(BPF_SIZE(fp->code), BPF_REG_A,
472 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_D);
473 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_TMP, offset);
474 *insn++ = BPF_LDX_MEM(BPF_SIZE(fp->code), BPF_REG_A,
478 *insn++ = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, size * 8);
479 *insn++ = BPF_JMP_A(8);
482 *insn++ = BPF_MOV64_REG(BPF_REG_ARG1, BPF_REG_CTX);
483 *insn++ = BPF_MOV64_REG(BPF_REG_ARG2, BPF_REG_D);
484 *insn++ = BPF_MOV64_REG(BPF_REG_ARG3, BPF_REG_H);
486 *insn++ = BPF_MOV64_IMM(BPF_REG_ARG4, offset);
488 *insn++ = BPF_MOV64_REG(BPF_REG_ARG4, BPF_REG_X);
490 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_ARG4, offset);
493 switch (BPF_SIZE(fp->code)) {
495 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_8);
498 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_16);
501 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_32);
507 *insn++ = BPF_JMP_IMM(BPF_JSGE, BPF_REG_A, 0, 2);
508 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
509 *insn = BPF_EXIT_INSN();
516 * bpf_convert_filter - convert filter program
517 * @prog: the user passed filter program
518 * @len: the length of the user passed filter program
519 * @new_prog: allocated 'struct bpf_prog' or NULL
520 * @new_len: pointer to store length of converted program
521 * @seen_ld_abs: bool whether we've seen ld_abs/ind
523 * Remap 'sock_filter' style classic BPF (cBPF) instruction set to 'bpf_insn'
524 * style extended BPF (eBPF).
525 * Conversion workflow:
527 * 1) First pass for calculating the new program length:
528 * bpf_convert_filter(old_prog, old_len, NULL, &new_len, &seen_ld_abs)
530 * 2) 2nd pass to remap in two passes: 1st pass finds new
531 * jump offsets, 2nd pass remapping:
532 * bpf_convert_filter(old_prog, old_len, new_prog, &new_len, &seen_ld_abs)
534 static int bpf_convert_filter(struct sock_filter *prog, int len,
535 struct bpf_prog *new_prog, int *new_len,
538 int new_flen = 0, pass = 0, target, i, stack_off;
539 struct bpf_insn *new_insn, *first_insn = NULL;
540 struct sock_filter *fp;
544 BUILD_BUG_ON(BPF_MEMWORDS * sizeof(u32) > MAX_BPF_STACK);
545 BUILD_BUG_ON(BPF_REG_FP + 1 != MAX_BPF_REG);
547 if (len <= 0 || len > BPF_MAXINSNS)
551 first_insn = new_prog->insnsi;
552 addrs = kcalloc(len, sizeof(*addrs),
553 GFP_KERNEL | __GFP_NOWARN);
559 new_insn = first_insn;
562 /* Classic BPF related prologue emission. */
564 /* Classic BPF expects A and X to be reset first. These need
565 * to be guaranteed to be the first two instructions.
567 *new_insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
568 *new_insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_X, BPF_REG_X);
570 /* All programs must keep CTX in callee saved BPF_REG_CTX.
571 * In eBPF case it's done by the compiler, here we need to
572 * do this ourself. Initial CTX is present in BPF_REG_ARG1.
574 *new_insn++ = BPF_MOV64_REG(BPF_REG_CTX, BPF_REG_ARG1);
576 /* For packet access in classic BPF, cache skb->data
577 * in callee-saved BPF R8 and skb->len - skb->data_len
578 * (headlen) in BPF R9. Since classic BPF is read-only
579 * on CTX, we only need to cache it once.
581 *new_insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
582 BPF_REG_D, BPF_REG_CTX,
583 offsetof(struct sk_buff, data));
584 *new_insn++ = BPF_LDX_MEM(BPF_W, BPF_REG_H, BPF_REG_CTX,
585 offsetof(struct sk_buff, len));
586 *new_insn++ = BPF_LDX_MEM(BPF_W, BPF_REG_TMP, BPF_REG_CTX,
587 offsetof(struct sk_buff, data_len));
588 *new_insn++ = BPF_ALU32_REG(BPF_SUB, BPF_REG_H, BPF_REG_TMP);
594 for (i = 0; i < len; fp++, i++) {
595 struct bpf_insn tmp_insns[32] = { };
596 struct bpf_insn *insn = tmp_insns;
599 addrs[i] = new_insn - first_insn;
602 /* All arithmetic insns and skb loads map as-is. */
603 case BPF_ALU | BPF_ADD | BPF_X:
604 case BPF_ALU | BPF_ADD | BPF_K:
605 case BPF_ALU | BPF_SUB | BPF_X:
606 case BPF_ALU | BPF_SUB | BPF_K:
607 case BPF_ALU | BPF_AND | BPF_X:
608 case BPF_ALU | BPF_AND | BPF_K:
609 case BPF_ALU | BPF_OR | BPF_X:
610 case BPF_ALU | BPF_OR | BPF_K:
611 case BPF_ALU | BPF_LSH | BPF_X:
612 case BPF_ALU | BPF_LSH | BPF_K:
613 case BPF_ALU | BPF_RSH | BPF_X:
614 case BPF_ALU | BPF_RSH | BPF_K:
615 case BPF_ALU | BPF_XOR | BPF_X:
616 case BPF_ALU | BPF_XOR | BPF_K:
617 case BPF_ALU | BPF_MUL | BPF_X:
618 case BPF_ALU | BPF_MUL | BPF_K:
619 case BPF_ALU | BPF_DIV | BPF_X:
620 case BPF_ALU | BPF_DIV | BPF_K:
621 case BPF_ALU | BPF_MOD | BPF_X:
622 case BPF_ALU | BPF_MOD | BPF_K:
623 case BPF_ALU | BPF_NEG:
624 case BPF_LD | BPF_ABS | BPF_W:
625 case BPF_LD | BPF_ABS | BPF_H:
626 case BPF_LD | BPF_ABS | BPF_B:
627 case BPF_LD | BPF_IND | BPF_W:
628 case BPF_LD | BPF_IND | BPF_H:
629 case BPF_LD | BPF_IND | BPF_B:
630 /* Check for overloaded BPF extension and
631 * directly convert it if found, otherwise
632 * just move on with mapping.
634 if (BPF_CLASS(fp->code) == BPF_LD &&
635 BPF_MODE(fp->code) == BPF_ABS &&
636 convert_bpf_extensions(fp, &insn))
638 if (BPF_CLASS(fp->code) == BPF_LD &&
639 convert_bpf_ld_abs(fp, &insn)) {
644 if (fp->code == (BPF_ALU | BPF_DIV | BPF_X) ||
645 fp->code == (BPF_ALU | BPF_MOD | BPF_X)) {
646 *insn++ = BPF_MOV32_REG(BPF_REG_X, BPF_REG_X);
647 /* Error with exception code on div/mod by 0.
648 * For cBPF programs, this was always return 0.
650 *insn++ = BPF_JMP_IMM(BPF_JNE, BPF_REG_X, 0, 2);
651 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_A, BPF_REG_A);
652 *insn++ = BPF_EXIT_INSN();
655 *insn = BPF_RAW_INSN(fp->code, BPF_REG_A, BPF_REG_X, 0, fp->k);
658 /* Jump transformation cannot use BPF block macros
659 * everywhere as offset calculation and target updates
660 * require a bit more work than the rest, i.e. jump
661 * opcodes map as-is, but offsets need adjustment.
664 #define BPF_EMIT_JMP \
666 const s32 off_min = S16_MIN, off_max = S16_MAX; \
669 if (target >= len || target < 0) \
671 off = addrs ? addrs[target] - addrs[i] - 1 : 0; \
672 /* Adjust pc relative offset for 2nd or 3rd insn. */ \
673 off -= insn - tmp_insns; \
674 /* Reject anything not fitting into insn->off. */ \
675 if (off < off_min || off > off_max) \
680 case BPF_JMP | BPF_JA:
681 target = i + fp->k + 1;
682 insn->code = fp->code;
686 case BPF_JMP | BPF_JEQ | BPF_K:
687 case BPF_JMP | BPF_JEQ | BPF_X:
688 case BPF_JMP | BPF_JSET | BPF_K:
689 case BPF_JMP | BPF_JSET | BPF_X:
690 case BPF_JMP | BPF_JGT | BPF_K:
691 case BPF_JMP | BPF_JGT | BPF_X:
692 case BPF_JMP | BPF_JGE | BPF_K:
693 case BPF_JMP | BPF_JGE | BPF_X:
694 if (BPF_SRC(fp->code) == BPF_K && (int) fp->k < 0) {
695 /* BPF immediates are signed, zero extend
696 * immediate into tmp register and use it
699 *insn++ = BPF_MOV32_IMM(BPF_REG_TMP, fp->k);
701 insn->dst_reg = BPF_REG_A;
702 insn->src_reg = BPF_REG_TMP;
705 insn->dst_reg = BPF_REG_A;
707 bpf_src = BPF_SRC(fp->code);
708 insn->src_reg = bpf_src == BPF_X ? BPF_REG_X : 0;
711 /* Common case where 'jump_false' is next insn. */
713 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
714 target = i + fp->jt + 1;
719 /* Convert some jumps when 'jump_true' is next insn. */
721 switch (BPF_OP(fp->code)) {
723 insn->code = BPF_JMP | BPF_JNE | bpf_src;
726 insn->code = BPF_JMP | BPF_JLE | bpf_src;
729 insn->code = BPF_JMP | BPF_JLT | bpf_src;
735 target = i + fp->jf + 1;
740 /* Other jumps are mapped into two insns: Jxx and JA. */
741 target = i + fp->jt + 1;
742 insn->code = BPF_JMP | BPF_OP(fp->code) | bpf_src;
746 insn->code = BPF_JMP | BPF_JA;
747 target = i + fp->jf + 1;
751 /* ldxb 4 * ([14] & 0xf) is remaped into 6 insns. */
752 case BPF_LDX | BPF_MSH | BPF_B: {
753 struct sock_filter tmp = {
754 .code = BPF_LD | BPF_ABS | BPF_B,
761 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
762 /* A = BPF_R0 = *(u8 *) (skb->data + K) */
763 convert_bpf_ld_abs(&tmp, &insn);
766 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, 0xf);
768 *insn++ = BPF_ALU32_IMM(BPF_LSH, BPF_REG_A, 2);
770 *insn++ = BPF_MOV64_REG(BPF_REG_TMP, BPF_REG_X);
772 *insn++ = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
774 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_TMP);
777 /* RET_K is remaped into 2 insns. RET_A case doesn't need an
778 * extra mov as BPF_REG_0 is already mapped into BPF_REG_A.
780 case BPF_RET | BPF_A:
781 case BPF_RET | BPF_K:
782 if (BPF_RVAL(fp->code) == BPF_K)
783 *insn++ = BPF_MOV32_RAW(BPF_K, BPF_REG_0,
785 *insn = BPF_EXIT_INSN();
788 /* Store to stack. */
791 stack_off = fp->k * 4 + 4;
792 *insn = BPF_STX_MEM(BPF_W, BPF_REG_FP, BPF_CLASS(fp->code) ==
793 BPF_ST ? BPF_REG_A : BPF_REG_X,
795 /* check_load_and_stores() verifies that classic BPF can
796 * load from stack only after write, so tracking
797 * stack_depth for ST|STX insns is enough
799 if (new_prog && new_prog->aux->stack_depth < stack_off)
800 new_prog->aux->stack_depth = stack_off;
803 /* Load from stack. */
804 case BPF_LD | BPF_MEM:
805 case BPF_LDX | BPF_MEM:
806 stack_off = fp->k * 4 + 4;
807 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
808 BPF_REG_A : BPF_REG_X, BPF_REG_FP,
813 case BPF_LD | BPF_IMM:
814 case BPF_LDX | BPF_IMM:
815 *insn = BPF_MOV32_IMM(BPF_CLASS(fp->code) == BPF_LD ?
816 BPF_REG_A : BPF_REG_X, fp->k);
820 case BPF_MISC | BPF_TAX:
821 *insn = BPF_MOV64_REG(BPF_REG_X, BPF_REG_A);
825 case BPF_MISC | BPF_TXA:
826 *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_X);
829 /* A = skb->len or X = skb->len */
830 case BPF_LD | BPF_W | BPF_LEN:
831 case BPF_LDX | BPF_W | BPF_LEN:
832 *insn = BPF_LDX_MEM(BPF_W, BPF_CLASS(fp->code) == BPF_LD ?
833 BPF_REG_A : BPF_REG_X, BPF_REG_CTX,
834 offsetof(struct sk_buff, len));
837 /* Access seccomp_data fields. */
838 case BPF_LDX | BPF_ABS | BPF_W:
839 /* A = *(u32 *) (ctx + K) */
840 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX, fp->k);
843 /* Unknown instruction. */
850 memcpy(new_insn, tmp_insns,
851 sizeof(*insn) * (insn - tmp_insns));
852 new_insn += insn - tmp_insns;
856 /* Only calculating new length. */
857 *new_len = new_insn - first_insn;
859 *new_len += 4; /* Prologue bits. */
864 if (new_flen != new_insn - first_insn) {
865 new_flen = new_insn - first_insn;
872 BUG_ON(*new_len != new_flen);
881 * As we dont want to clear mem[] array for each packet going through
882 * __bpf_prog_run(), we check that filter loaded by user never try to read
883 * a cell if not previously written, and we check all branches to be sure
884 * a malicious user doesn't try to abuse us.
886 static int check_load_and_stores(const struct sock_filter *filter, int flen)
888 u16 *masks, memvalid = 0; /* One bit per cell, 16 cells */
891 BUILD_BUG_ON(BPF_MEMWORDS > 16);
893 masks = kmalloc_array(flen, sizeof(*masks), GFP_KERNEL);
897 memset(masks, 0xff, flen * sizeof(*masks));
899 for (pc = 0; pc < flen; pc++) {
900 memvalid &= masks[pc];
902 switch (filter[pc].code) {
905 memvalid |= (1 << filter[pc].k);
907 case BPF_LD | BPF_MEM:
908 case BPF_LDX | BPF_MEM:
909 if (!(memvalid & (1 << filter[pc].k))) {
914 case BPF_JMP | BPF_JA:
915 /* A jump must set masks on target */
916 masks[pc + 1 + filter[pc].k] &= memvalid;
919 case BPF_JMP | BPF_JEQ | BPF_K:
920 case BPF_JMP | BPF_JEQ | BPF_X:
921 case BPF_JMP | BPF_JGE | BPF_K:
922 case BPF_JMP | BPF_JGE | BPF_X:
923 case BPF_JMP | BPF_JGT | BPF_K:
924 case BPF_JMP | BPF_JGT | BPF_X:
925 case BPF_JMP | BPF_JSET | BPF_K:
926 case BPF_JMP | BPF_JSET | BPF_X:
927 /* A jump must set masks on targets */
928 masks[pc + 1 + filter[pc].jt] &= memvalid;
929 masks[pc + 1 + filter[pc].jf] &= memvalid;
939 static bool chk_code_allowed(u16 code_to_probe)
941 static const bool codes[] = {
942 /* 32 bit ALU operations */
943 [BPF_ALU | BPF_ADD | BPF_K] = true,
944 [BPF_ALU | BPF_ADD | BPF_X] = true,
945 [BPF_ALU | BPF_SUB | BPF_K] = true,
946 [BPF_ALU | BPF_SUB | BPF_X] = true,
947 [BPF_ALU | BPF_MUL | BPF_K] = true,
948 [BPF_ALU | BPF_MUL | BPF_X] = true,
949 [BPF_ALU | BPF_DIV | BPF_K] = true,
950 [BPF_ALU | BPF_DIV | BPF_X] = true,
951 [BPF_ALU | BPF_MOD | BPF_K] = true,
952 [BPF_ALU | BPF_MOD | BPF_X] = true,
953 [BPF_ALU | BPF_AND | BPF_K] = true,
954 [BPF_ALU | BPF_AND | BPF_X] = true,
955 [BPF_ALU | BPF_OR | BPF_K] = true,
956 [BPF_ALU | BPF_OR | BPF_X] = true,
957 [BPF_ALU | BPF_XOR | BPF_K] = true,
958 [BPF_ALU | BPF_XOR | BPF_X] = true,
959 [BPF_ALU | BPF_LSH | BPF_K] = true,
960 [BPF_ALU | BPF_LSH | BPF_X] = true,
961 [BPF_ALU | BPF_RSH | BPF_K] = true,
962 [BPF_ALU | BPF_RSH | BPF_X] = true,
963 [BPF_ALU | BPF_NEG] = true,
964 /* Load instructions */
965 [BPF_LD | BPF_W | BPF_ABS] = true,
966 [BPF_LD | BPF_H | BPF_ABS] = true,
967 [BPF_LD | BPF_B | BPF_ABS] = true,
968 [BPF_LD | BPF_W | BPF_LEN] = true,
969 [BPF_LD | BPF_W | BPF_IND] = true,
970 [BPF_LD | BPF_H | BPF_IND] = true,
971 [BPF_LD | BPF_B | BPF_IND] = true,
972 [BPF_LD | BPF_IMM] = true,
973 [BPF_LD | BPF_MEM] = true,
974 [BPF_LDX | BPF_W | BPF_LEN] = true,
975 [BPF_LDX | BPF_B | BPF_MSH] = true,
976 [BPF_LDX | BPF_IMM] = true,
977 [BPF_LDX | BPF_MEM] = true,
978 /* Store instructions */
981 /* Misc instructions */
982 [BPF_MISC | BPF_TAX] = true,
983 [BPF_MISC | BPF_TXA] = true,
984 /* Return instructions */
985 [BPF_RET | BPF_K] = true,
986 [BPF_RET | BPF_A] = true,
987 /* Jump instructions */
988 [BPF_JMP | BPF_JA] = true,
989 [BPF_JMP | BPF_JEQ | BPF_K] = true,
990 [BPF_JMP | BPF_JEQ | BPF_X] = true,
991 [BPF_JMP | BPF_JGE | BPF_K] = true,
992 [BPF_JMP | BPF_JGE | BPF_X] = true,
993 [BPF_JMP | BPF_JGT | BPF_K] = true,
994 [BPF_JMP | BPF_JGT | BPF_X] = true,
995 [BPF_JMP | BPF_JSET | BPF_K] = true,
996 [BPF_JMP | BPF_JSET | BPF_X] = true,
999 if (code_to_probe >= ARRAY_SIZE(codes))
1002 return codes[code_to_probe];
1005 static bool bpf_check_basics_ok(const struct sock_filter *filter,
1010 if (flen == 0 || flen > BPF_MAXINSNS)
1017 * bpf_check_classic - verify socket filter code
1018 * @filter: filter to verify
1019 * @flen: length of filter
1021 * Check the user's filter code. If we let some ugly
1022 * filter code slip through kaboom! The filter must contain
1023 * no references or jumps that are out of range, no illegal
1024 * instructions, and must end with a RET instruction.
1026 * All jumps are forward as they are not signed.
1028 * Returns 0 if the rule set is legal or -EINVAL if not.
1030 static int bpf_check_classic(const struct sock_filter *filter,
1036 /* Check the filter code now */
1037 for (pc = 0; pc < flen; pc++) {
1038 const struct sock_filter *ftest = &filter[pc];
1040 /* May we actually operate on this code? */
1041 if (!chk_code_allowed(ftest->code))
1044 /* Some instructions need special checks */
1045 switch (ftest->code) {
1046 case BPF_ALU | BPF_DIV | BPF_K:
1047 case BPF_ALU | BPF_MOD | BPF_K:
1048 /* Check for division by zero */
1052 case BPF_ALU | BPF_LSH | BPF_K:
1053 case BPF_ALU | BPF_RSH | BPF_K:
1057 case BPF_LD | BPF_MEM:
1058 case BPF_LDX | BPF_MEM:
1061 /* Check for invalid memory addresses */
1062 if (ftest->k >= BPF_MEMWORDS)
1065 case BPF_JMP | BPF_JA:
1066 /* Note, the large ftest->k might cause loops.
1067 * Compare this with conditional jumps below,
1068 * where offsets are limited. --ANK (981016)
1070 if (ftest->k >= (unsigned int)(flen - pc - 1))
1073 case BPF_JMP | BPF_JEQ | BPF_K:
1074 case BPF_JMP | BPF_JEQ | BPF_X:
1075 case BPF_JMP | BPF_JGE | BPF_K:
1076 case BPF_JMP | BPF_JGE | BPF_X:
1077 case BPF_JMP | BPF_JGT | BPF_K:
1078 case BPF_JMP | BPF_JGT | BPF_X:
1079 case BPF_JMP | BPF_JSET | BPF_K:
1080 case BPF_JMP | BPF_JSET | BPF_X:
1081 /* Both conditionals must be safe */
1082 if (pc + ftest->jt + 1 >= flen ||
1083 pc + ftest->jf + 1 >= flen)
1086 case BPF_LD | BPF_W | BPF_ABS:
1087 case BPF_LD | BPF_H | BPF_ABS:
1088 case BPF_LD | BPF_B | BPF_ABS:
1090 if (bpf_anc_helper(ftest) & BPF_ANC)
1092 /* Ancillary operation unknown or unsupported */
1093 if (anc_found == false && ftest->k >= SKF_AD_OFF)
1098 /* Last instruction must be a RET code */
1099 switch (filter[flen - 1].code) {
1100 case BPF_RET | BPF_K:
1101 case BPF_RET | BPF_A:
1102 return check_load_and_stores(filter, flen);
1108 static int bpf_prog_store_orig_filter(struct bpf_prog *fp,
1109 const struct sock_fprog *fprog)
1111 unsigned int fsize = bpf_classic_proglen(fprog);
1112 struct sock_fprog_kern *fkprog;
1114 fp->orig_prog = kmalloc(sizeof(*fkprog), GFP_KERNEL);
1118 fkprog = fp->orig_prog;
1119 fkprog->len = fprog->len;
1121 fkprog->filter = kmemdup(fp->insns, fsize,
1122 GFP_KERNEL | __GFP_NOWARN);
1123 if (!fkprog->filter) {
1124 kfree(fp->orig_prog);
1131 static void bpf_release_orig_filter(struct bpf_prog *fp)
1133 struct sock_fprog_kern *fprog = fp->orig_prog;
1136 kfree(fprog->filter);
1141 static void __bpf_prog_release(struct bpf_prog *prog)
1143 if (prog->type == BPF_PROG_TYPE_SOCKET_FILTER) {
1146 bpf_release_orig_filter(prog);
1147 bpf_prog_free(prog);
1151 static void __sk_filter_release(struct sk_filter *fp)
1153 __bpf_prog_release(fp->prog);
1158 * sk_filter_release_rcu - Release a socket filter by rcu_head
1159 * @rcu: rcu_head that contains the sk_filter to free
1161 static void sk_filter_release_rcu(struct rcu_head *rcu)
1163 struct sk_filter *fp = container_of(rcu, struct sk_filter, rcu);
1165 __sk_filter_release(fp);
1169 * sk_filter_release - release a socket filter
1170 * @fp: filter to remove
1172 * Remove a filter from a socket and release its resources.
1174 static void sk_filter_release(struct sk_filter *fp)
1176 if (refcount_dec_and_test(&fp->refcnt))
1177 call_rcu(&fp->rcu, sk_filter_release_rcu);
1180 void sk_filter_uncharge(struct sock *sk, struct sk_filter *fp)
1182 u32 filter_size = bpf_prog_size(fp->prog->len);
1184 atomic_sub(filter_size, &sk->sk_omem_alloc);
1185 sk_filter_release(fp);
1188 /* try to charge the socket memory if there is space available
1189 * return true on success
1191 static bool __sk_filter_charge(struct sock *sk, struct sk_filter *fp)
1193 u32 filter_size = bpf_prog_size(fp->prog->len);
1195 /* same check as in sock_kmalloc() */
1196 if (filter_size <= sysctl_optmem_max &&
1197 atomic_read(&sk->sk_omem_alloc) + filter_size < sysctl_optmem_max) {
1198 atomic_add(filter_size, &sk->sk_omem_alloc);
1204 bool sk_filter_charge(struct sock *sk, struct sk_filter *fp)
1206 if (!refcount_inc_not_zero(&fp->refcnt))
1209 if (!__sk_filter_charge(sk, fp)) {
1210 sk_filter_release(fp);
1216 static struct bpf_prog *bpf_migrate_filter(struct bpf_prog *fp)
1218 struct sock_filter *old_prog;
1219 struct bpf_prog *old_fp;
1220 int err, new_len, old_len = fp->len;
1221 bool seen_ld_abs = false;
1223 /* We are free to overwrite insns et al right here as it
1224 * won't be used at this point in time anymore internally
1225 * after the migration to the internal BPF instruction
1228 BUILD_BUG_ON(sizeof(struct sock_filter) !=
1229 sizeof(struct bpf_insn));
1231 /* Conversion cannot happen on overlapping memory areas,
1232 * so we need to keep the user BPF around until the 2nd
1233 * pass. At this time, the user BPF is stored in fp->insns.
1235 old_prog = kmemdup(fp->insns, old_len * sizeof(struct sock_filter),
1236 GFP_KERNEL | __GFP_NOWARN);
1242 /* 1st pass: calculate the new program length. */
1243 err = bpf_convert_filter(old_prog, old_len, NULL, &new_len,
1248 /* Expand fp for appending the new filter representation. */
1250 fp = bpf_prog_realloc(old_fp, bpf_prog_size(new_len), 0);
1252 /* The old_fp is still around in case we couldn't
1253 * allocate new memory, so uncharge on that one.
1262 /* 2nd pass: remap sock_filter insns into bpf_insn insns. */
1263 err = bpf_convert_filter(old_prog, old_len, fp, &new_len,
1266 /* 2nd bpf_convert_filter() can fail only if it fails
1267 * to allocate memory, remapping must succeed. Note,
1268 * that at this time old_fp has already been released
1273 fp = bpf_prog_select_runtime(fp, &err);
1283 __bpf_prog_release(fp);
1284 return ERR_PTR(err);
1287 static struct bpf_prog *bpf_prepare_filter(struct bpf_prog *fp,
1288 bpf_aux_classic_check_t trans)
1292 fp->bpf_func = NULL;
1295 err = bpf_check_classic(fp->insns, fp->len);
1297 __bpf_prog_release(fp);
1298 return ERR_PTR(err);
1301 /* There might be additional checks and transformations
1302 * needed on classic filters, f.e. in case of seccomp.
1305 err = trans(fp->insns, fp->len);
1307 __bpf_prog_release(fp);
1308 return ERR_PTR(err);
1312 /* Probe if we can JIT compile the filter and if so, do
1313 * the compilation of the filter.
1315 bpf_jit_compile(fp);
1317 /* JIT compiler couldn't process this filter, so do the
1318 * internal BPF translation for the optimized interpreter.
1321 fp = bpf_migrate_filter(fp);
1327 * bpf_prog_create - create an unattached filter
1328 * @pfp: the unattached filter that is created
1329 * @fprog: the filter program
1331 * Create a filter independent of any socket. We first run some
1332 * sanity checks on it to make sure it does not explode on us later.
1333 * If an error occurs or there is insufficient memory for the filter
1334 * a negative errno code is returned. On success the return is zero.
1336 int bpf_prog_create(struct bpf_prog **pfp, struct sock_fprog_kern *fprog)
1338 unsigned int fsize = bpf_classic_proglen(fprog);
1339 struct bpf_prog *fp;
1341 /* Make sure new filter is there and in the right amounts. */
1342 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1345 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1349 memcpy(fp->insns, fprog->filter, fsize);
1351 fp->len = fprog->len;
1352 /* Since unattached filters are not copied back to user
1353 * space through sk_get_filter(), we do not need to hold
1354 * a copy here, and can spare us the work.
1356 fp->orig_prog = NULL;
1358 /* bpf_prepare_filter() already takes care of freeing
1359 * memory in case something goes wrong.
1361 fp = bpf_prepare_filter(fp, NULL);
1368 EXPORT_SYMBOL_GPL(bpf_prog_create);
1371 * bpf_prog_create_from_user - create an unattached filter from user buffer
1372 * @pfp: the unattached filter that is created
1373 * @fprog: the filter program
1374 * @trans: post-classic verifier transformation handler
1375 * @save_orig: save classic BPF program
1377 * This function effectively does the same as bpf_prog_create(), only
1378 * that it builds up its insns buffer from user space provided buffer.
1379 * It also allows for passing a bpf_aux_classic_check_t handler.
1381 int bpf_prog_create_from_user(struct bpf_prog **pfp, struct sock_fprog *fprog,
1382 bpf_aux_classic_check_t trans, bool save_orig)
1384 unsigned int fsize = bpf_classic_proglen(fprog);
1385 struct bpf_prog *fp;
1388 /* Make sure new filter is there and in the right amounts. */
1389 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1392 fp = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1396 if (copy_from_user(fp->insns, fprog->filter, fsize)) {
1397 __bpf_prog_free(fp);
1401 fp->len = fprog->len;
1402 fp->orig_prog = NULL;
1405 err = bpf_prog_store_orig_filter(fp, fprog);
1407 __bpf_prog_free(fp);
1412 /* bpf_prepare_filter() already takes care of freeing
1413 * memory in case something goes wrong.
1415 fp = bpf_prepare_filter(fp, trans);
1422 EXPORT_SYMBOL_GPL(bpf_prog_create_from_user);
1424 void bpf_prog_destroy(struct bpf_prog *fp)
1426 __bpf_prog_release(fp);
1428 EXPORT_SYMBOL_GPL(bpf_prog_destroy);
1430 static int __sk_attach_prog(struct bpf_prog *prog, struct sock *sk)
1432 struct sk_filter *fp, *old_fp;
1434 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
1440 if (!__sk_filter_charge(sk, fp)) {
1444 refcount_set(&fp->refcnt, 1);
1446 old_fp = rcu_dereference_protected(sk->sk_filter,
1447 lockdep_sock_is_held(sk));
1448 rcu_assign_pointer(sk->sk_filter, fp);
1451 sk_filter_uncharge(sk, old_fp);
1456 static int __reuseport_attach_prog(struct bpf_prog *prog, struct sock *sk)
1458 struct bpf_prog *old_prog;
1461 if (bpf_prog_size(prog->len) > sysctl_optmem_max)
1464 if (sk_unhashed(sk) && sk->sk_reuseport) {
1465 err = reuseport_alloc(sk);
1468 } else if (!rcu_access_pointer(sk->sk_reuseport_cb)) {
1469 /* The socket wasn't bound with SO_REUSEPORT */
1473 old_prog = reuseport_attach_prog(sk, prog);
1475 bpf_prog_destroy(old_prog);
1481 struct bpf_prog *__get_filter(struct sock_fprog *fprog, struct sock *sk)
1483 unsigned int fsize = bpf_classic_proglen(fprog);
1484 struct bpf_prog *prog;
1487 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1488 return ERR_PTR(-EPERM);
1490 /* Make sure new filter is there and in the right amounts. */
1491 if (!bpf_check_basics_ok(fprog->filter, fprog->len))
1492 return ERR_PTR(-EINVAL);
1494 prog = bpf_prog_alloc(bpf_prog_size(fprog->len), 0);
1496 return ERR_PTR(-ENOMEM);
1498 if (copy_from_user(prog->insns, fprog->filter, fsize)) {
1499 __bpf_prog_free(prog);
1500 return ERR_PTR(-EFAULT);
1503 prog->len = fprog->len;
1505 err = bpf_prog_store_orig_filter(prog, fprog);
1507 __bpf_prog_free(prog);
1508 return ERR_PTR(-ENOMEM);
1511 /* bpf_prepare_filter() already takes care of freeing
1512 * memory in case something goes wrong.
1514 return bpf_prepare_filter(prog, NULL);
1518 * sk_attach_filter - attach a socket filter
1519 * @fprog: the filter program
1520 * @sk: the socket to use
1522 * Attach the user's filter code. We first run some sanity checks on
1523 * it to make sure it does not explode on us later. If an error
1524 * occurs or there is insufficient memory for the filter a negative
1525 * errno code is returned. On success the return is zero.
1527 int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1529 struct bpf_prog *prog = __get_filter(fprog, sk);
1533 return PTR_ERR(prog);
1535 err = __sk_attach_prog(prog, sk);
1537 __bpf_prog_release(prog);
1543 EXPORT_SYMBOL_GPL(sk_attach_filter);
1545 int sk_reuseport_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1547 struct bpf_prog *prog = __get_filter(fprog, sk);
1551 return PTR_ERR(prog);
1553 err = __reuseport_attach_prog(prog, sk);
1555 __bpf_prog_release(prog);
1562 static struct bpf_prog *__get_bpf(u32 ufd, struct sock *sk)
1564 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1565 return ERR_PTR(-EPERM);
1567 return bpf_prog_get_type(ufd, BPF_PROG_TYPE_SOCKET_FILTER);
1570 int sk_attach_bpf(u32 ufd, struct sock *sk)
1572 struct bpf_prog *prog = __get_bpf(ufd, sk);
1576 return PTR_ERR(prog);
1578 err = __sk_attach_prog(prog, sk);
1587 int sk_reuseport_attach_bpf(u32 ufd, struct sock *sk)
1589 struct bpf_prog *prog = __get_bpf(ufd, sk);
1593 return PTR_ERR(prog);
1595 err = __reuseport_attach_prog(prog, sk);
1604 struct bpf_scratchpad {
1606 __be32 diff[MAX_BPF_STACK / sizeof(__be32)];
1607 u8 buff[MAX_BPF_STACK];
1611 static DEFINE_PER_CPU(struct bpf_scratchpad, bpf_sp);
1613 static inline int __bpf_try_make_writable(struct sk_buff *skb,
1614 unsigned int write_len)
1616 return skb_ensure_writable(skb, write_len);
1619 static inline int bpf_try_make_writable(struct sk_buff *skb,
1620 unsigned int write_len)
1622 int err = __bpf_try_make_writable(skb, write_len);
1624 bpf_compute_data_pointers(skb);
1628 static int bpf_try_make_head_writable(struct sk_buff *skb)
1630 return bpf_try_make_writable(skb, skb_headlen(skb));
1633 static inline void bpf_push_mac_rcsum(struct sk_buff *skb)
1635 if (skb_at_tc_ingress(skb))
1636 skb_postpush_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1639 static inline void bpf_pull_mac_rcsum(struct sk_buff *skb)
1641 if (skb_at_tc_ingress(skb))
1642 skb_postpull_rcsum(skb, skb_mac_header(skb), skb->mac_len);
1645 BPF_CALL_5(bpf_skb_store_bytes, struct sk_buff *, skb, u32, offset,
1646 const void *, from, u32, len, u64, flags)
1650 if (unlikely(flags & ~(BPF_F_RECOMPUTE_CSUM | BPF_F_INVALIDATE_HASH)))
1652 if (unlikely(offset > 0xffff))
1654 if (unlikely(bpf_try_make_writable(skb, offset + len)))
1657 ptr = skb->data + offset;
1658 if (flags & BPF_F_RECOMPUTE_CSUM)
1659 __skb_postpull_rcsum(skb, ptr, len, offset);
1661 memcpy(ptr, from, len);
1663 if (flags & BPF_F_RECOMPUTE_CSUM)
1664 __skb_postpush_rcsum(skb, ptr, len, offset);
1665 if (flags & BPF_F_INVALIDATE_HASH)
1666 skb_clear_hash(skb);
1671 static const struct bpf_func_proto bpf_skb_store_bytes_proto = {
1672 .func = bpf_skb_store_bytes,
1674 .ret_type = RET_INTEGER,
1675 .arg1_type = ARG_PTR_TO_CTX,
1676 .arg2_type = ARG_ANYTHING,
1677 .arg3_type = ARG_PTR_TO_MEM,
1678 .arg4_type = ARG_CONST_SIZE,
1679 .arg5_type = ARG_ANYTHING,
1682 BPF_CALL_4(bpf_skb_load_bytes, const struct sk_buff *, skb, u32, offset,
1683 void *, to, u32, len)
1687 if (unlikely(offset > 0xffff))
1690 ptr = skb_header_pointer(skb, offset, len, to);
1694 memcpy(to, ptr, len);
1702 static const struct bpf_func_proto bpf_skb_load_bytes_proto = {
1703 .func = bpf_skb_load_bytes,
1705 .ret_type = RET_INTEGER,
1706 .arg1_type = ARG_PTR_TO_CTX,
1707 .arg2_type = ARG_ANYTHING,
1708 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
1709 .arg4_type = ARG_CONST_SIZE,
1712 BPF_CALL_5(bpf_skb_load_bytes_relative, const struct sk_buff *, skb,
1713 u32, offset, void *, to, u32, len, u32, start_header)
1717 if (unlikely(offset > 0xffff || len > skb_headlen(skb)))
1720 switch (start_header) {
1721 case BPF_HDR_START_MAC:
1722 ptr = skb_mac_header(skb) + offset;
1724 case BPF_HDR_START_NET:
1725 ptr = skb_network_header(skb) + offset;
1731 if (likely(ptr >= skb_mac_header(skb) &&
1732 ptr + len <= skb_tail_pointer(skb))) {
1733 memcpy(to, ptr, len);
1742 static const struct bpf_func_proto bpf_skb_load_bytes_relative_proto = {
1743 .func = bpf_skb_load_bytes_relative,
1745 .ret_type = RET_INTEGER,
1746 .arg1_type = ARG_PTR_TO_CTX,
1747 .arg2_type = ARG_ANYTHING,
1748 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
1749 .arg4_type = ARG_CONST_SIZE,
1750 .arg5_type = ARG_ANYTHING,
1753 BPF_CALL_2(bpf_skb_pull_data, struct sk_buff *, skb, u32, len)
1755 /* Idea is the following: should the needed direct read/write
1756 * test fail during runtime, we can pull in more data and redo
1757 * again, since implicitly, we invalidate previous checks here.
1759 * Or, since we know how much we need to make read/writeable,
1760 * this can be done once at the program beginning for direct
1761 * access case. By this we overcome limitations of only current
1762 * headroom being accessible.
1764 return bpf_try_make_writable(skb, len ? : skb_headlen(skb));
1767 static const struct bpf_func_proto bpf_skb_pull_data_proto = {
1768 .func = bpf_skb_pull_data,
1770 .ret_type = RET_INTEGER,
1771 .arg1_type = ARG_PTR_TO_CTX,
1772 .arg2_type = ARG_ANYTHING,
1775 static inline int sk_skb_try_make_writable(struct sk_buff *skb,
1776 unsigned int write_len)
1778 int err = __bpf_try_make_writable(skb, write_len);
1780 bpf_compute_data_end_sk_skb(skb);
1784 BPF_CALL_2(sk_skb_pull_data, struct sk_buff *, skb, u32, len)
1786 /* Idea is the following: should the needed direct read/write
1787 * test fail during runtime, we can pull in more data and redo
1788 * again, since implicitly, we invalidate previous checks here.
1790 * Or, since we know how much we need to make read/writeable,
1791 * this can be done once at the program beginning for direct
1792 * access case. By this we overcome limitations of only current
1793 * headroom being accessible.
1795 return sk_skb_try_make_writable(skb, len ? : skb_headlen(skb));
1798 static const struct bpf_func_proto sk_skb_pull_data_proto = {
1799 .func = sk_skb_pull_data,
1801 .ret_type = RET_INTEGER,
1802 .arg1_type = ARG_PTR_TO_CTX,
1803 .arg2_type = ARG_ANYTHING,
1806 BPF_CALL_5(bpf_l3_csum_replace, struct sk_buff *, skb, u32, offset,
1807 u64, from, u64, to, u64, flags)
1811 if (unlikely(flags & ~(BPF_F_HDR_FIELD_MASK)))
1813 if (unlikely(offset > 0xffff || offset & 1))
1815 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
1818 ptr = (__sum16 *)(skb->data + offset);
1819 switch (flags & BPF_F_HDR_FIELD_MASK) {
1821 if (unlikely(from != 0))
1824 csum_replace_by_diff(ptr, to);
1827 csum_replace2(ptr, from, to);
1830 csum_replace4(ptr, from, to);
1839 static const struct bpf_func_proto bpf_l3_csum_replace_proto = {
1840 .func = bpf_l3_csum_replace,
1842 .ret_type = RET_INTEGER,
1843 .arg1_type = ARG_PTR_TO_CTX,
1844 .arg2_type = ARG_ANYTHING,
1845 .arg3_type = ARG_ANYTHING,
1846 .arg4_type = ARG_ANYTHING,
1847 .arg5_type = ARG_ANYTHING,
1850 BPF_CALL_5(bpf_l4_csum_replace, struct sk_buff *, skb, u32, offset,
1851 u64, from, u64, to, u64, flags)
1853 bool is_pseudo = flags & BPF_F_PSEUDO_HDR;
1854 bool is_mmzero = flags & BPF_F_MARK_MANGLED_0;
1855 bool do_mforce = flags & BPF_F_MARK_ENFORCE;
1858 if (unlikely(flags & ~(BPF_F_MARK_MANGLED_0 | BPF_F_MARK_ENFORCE |
1859 BPF_F_PSEUDO_HDR | BPF_F_HDR_FIELD_MASK)))
1861 if (unlikely(offset > 0xffff || offset & 1))
1863 if (unlikely(bpf_try_make_writable(skb, offset + sizeof(*ptr))))
1866 ptr = (__sum16 *)(skb->data + offset);
1867 if (is_mmzero && !do_mforce && !*ptr)
1870 switch (flags & BPF_F_HDR_FIELD_MASK) {
1872 if (unlikely(from != 0))
1875 inet_proto_csum_replace_by_diff(ptr, skb, to, is_pseudo);
1878 inet_proto_csum_replace2(ptr, skb, from, to, is_pseudo);
1881 inet_proto_csum_replace4(ptr, skb, from, to, is_pseudo);
1887 if (is_mmzero && !*ptr)
1888 *ptr = CSUM_MANGLED_0;
1892 static const struct bpf_func_proto bpf_l4_csum_replace_proto = {
1893 .func = bpf_l4_csum_replace,
1895 .ret_type = RET_INTEGER,
1896 .arg1_type = ARG_PTR_TO_CTX,
1897 .arg2_type = ARG_ANYTHING,
1898 .arg3_type = ARG_ANYTHING,
1899 .arg4_type = ARG_ANYTHING,
1900 .arg5_type = ARG_ANYTHING,
1903 BPF_CALL_5(bpf_csum_diff, __be32 *, from, u32, from_size,
1904 __be32 *, to, u32, to_size, __wsum, seed)
1906 struct bpf_scratchpad *sp = this_cpu_ptr(&bpf_sp);
1907 u32 diff_size = from_size + to_size;
1910 /* This is quite flexible, some examples:
1912 * from_size == 0, to_size > 0, seed := csum --> pushing data
1913 * from_size > 0, to_size == 0, seed := csum --> pulling data
1914 * from_size > 0, to_size > 0, seed := 0 --> diffing data
1916 * Even for diffing, from_size and to_size don't need to be equal.
1918 if (unlikely(((from_size | to_size) & (sizeof(__be32) - 1)) ||
1919 diff_size > sizeof(sp->diff)))
1922 for (i = 0; i < from_size / sizeof(__be32); i++, j++)
1923 sp->diff[j] = ~from[i];
1924 for (i = 0; i < to_size / sizeof(__be32); i++, j++)
1925 sp->diff[j] = to[i];
1927 return csum_partial(sp->diff, diff_size, seed);
1930 static const struct bpf_func_proto bpf_csum_diff_proto = {
1931 .func = bpf_csum_diff,
1934 .ret_type = RET_INTEGER,
1935 .arg1_type = ARG_PTR_TO_MEM_OR_NULL,
1936 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
1937 .arg3_type = ARG_PTR_TO_MEM_OR_NULL,
1938 .arg4_type = ARG_CONST_SIZE_OR_ZERO,
1939 .arg5_type = ARG_ANYTHING,
1942 BPF_CALL_2(bpf_csum_update, struct sk_buff *, skb, __wsum, csum)
1944 /* The interface is to be used in combination with bpf_csum_diff()
1945 * for direct packet writes. csum rotation for alignment as well
1946 * as emulating csum_sub() can be done from the eBPF program.
1948 if (skb->ip_summed == CHECKSUM_COMPLETE)
1949 return (skb->csum = csum_add(skb->csum, csum));
1954 static const struct bpf_func_proto bpf_csum_update_proto = {
1955 .func = bpf_csum_update,
1957 .ret_type = RET_INTEGER,
1958 .arg1_type = ARG_PTR_TO_CTX,
1959 .arg2_type = ARG_ANYTHING,
1962 static inline int __bpf_rx_skb(struct net_device *dev, struct sk_buff *skb)
1964 return dev_forward_skb(dev, skb);
1967 static inline int __bpf_rx_skb_no_mac(struct net_device *dev,
1968 struct sk_buff *skb)
1970 int ret = ____dev_forward_skb(dev, skb);
1974 ret = netif_rx(skb);
1980 static inline int __bpf_tx_skb(struct net_device *dev, struct sk_buff *skb)
1984 if (unlikely(__this_cpu_read(xmit_recursion) > XMIT_RECURSION_LIMIT)) {
1985 net_crit_ratelimited("bpf: recursion limit reached on datapath, buggy bpf program?\n");
1992 __this_cpu_inc(xmit_recursion);
1993 ret = dev_queue_xmit(skb);
1994 __this_cpu_dec(xmit_recursion);
1999 static int __bpf_redirect_no_mac(struct sk_buff *skb, struct net_device *dev,
2002 /* skb->mac_len is not set on normal egress */
2003 unsigned int mlen = skb->network_header - skb->mac_header;
2005 __skb_pull(skb, mlen);
2007 /* At ingress, the mac header has already been pulled once.
2008 * At egress, skb_pospull_rcsum has to be done in case that
2009 * the skb is originated from ingress (i.e. a forwarded skb)
2010 * to ensure that rcsum starts at net header.
2012 if (!skb_at_tc_ingress(skb))
2013 skb_postpull_rcsum(skb, skb_mac_header(skb), mlen);
2014 skb_pop_mac_header(skb);
2015 skb_reset_mac_len(skb);
2016 return flags & BPF_F_INGRESS ?
2017 __bpf_rx_skb_no_mac(dev, skb) : __bpf_tx_skb(dev, skb);
2020 static int __bpf_redirect_common(struct sk_buff *skb, struct net_device *dev,
2023 /* Verify that a link layer header is carried */
2024 if (unlikely(skb->mac_header >= skb->network_header)) {
2029 bpf_push_mac_rcsum(skb);
2030 return flags & BPF_F_INGRESS ?
2031 __bpf_rx_skb(dev, skb) : __bpf_tx_skb(dev, skb);
2034 static int __bpf_redirect(struct sk_buff *skb, struct net_device *dev,
2037 if (dev_is_mac_header_xmit(dev))
2038 return __bpf_redirect_common(skb, dev, flags);
2040 return __bpf_redirect_no_mac(skb, dev, flags);
2043 BPF_CALL_3(bpf_clone_redirect, struct sk_buff *, skb, u32, ifindex, u64, flags)
2045 struct net_device *dev;
2046 struct sk_buff *clone;
2049 if (unlikely(flags & ~(BPF_F_INGRESS)))
2052 dev = dev_get_by_index_rcu(dev_net(skb->dev), ifindex);
2056 clone = skb_clone(skb, GFP_ATOMIC);
2057 if (unlikely(!clone))
2060 /* For direct write, we need to keep the invariant that the skbs
2061 * we're dealing with need to be uncloned. Should uncloning fail
2062 * here, we need to free the just generated clone to unclone once
2065 ret = bpf_try_make_head_writable(skb);
2066 if (unlikely(ret)) {
2071 return __bpf_redirect(clone, dev, flags);
2074 static const struct bpf_func_proto bpf_clone_redirect_proto = {
2075 .func = bpf_clone_redirect,
2077 .ret_type = RET_INTEGER,
2078 .arg1_type = ARG_PTR_TO_CTX,
2079 .arg2_type = ARG_ANYTHING,
2080 .arg3_type = ARG_ANYTHING,
2083 struct redirect_info {
2086 struct bpf_map *map;
2087 struct bpf_map *map_to_flush;
2088 unsigned long map_owner;
2091 static DEFINE_PER_CPU(struct redirect_info, redirect_info);
2093 BPF_CALL_2(bpf_redirect, u32, ifindex, u64, flags)
2095 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
2097 if (unlikely(flags & ~(BPF_F_INGRESS)))
2100 ri->ifindex = ifindex;
2103 return TC_ACT_REDIRECT;
2106 int skb_do_redirect(struct sk_buff *skb)
2108 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
2109 struct net_device *dev;
2111 dev = dev_get_by_index_rcu(dev_net(skb->dev), ri->ifindex);
2113 if (unlikely(!dev)) {
2118 return __bpf_redirect(skb, dev, ri->flags);
2121 static const struct bpf_func_proto bpf_redirect_proto = {
2122 .func = bpf_redirect,
2124 .ret_type = RET_INTEGER,
2125 .arg1_type = ARG_ANYTHING,
2126 .arg2_type = ARG_ANYTHING,
2129 BPF_CALL_4(bpf_sk_redirect_hash, struct sk_buff *, skb,
2130 struct bpf_map *, map, void *, key, u64, flags)
2132 struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
2134 /* If user passes invalid input drop the packet. */
2135 if (unlikely(flags & ~(BPF_F_INGRESS)))
2138 tcb->bpf.flags = flags;
2139 tcb->bpf.sk_redir = __sock_hash_lookup_elem(map, key);
2140 if (!tcb->bpf.sk_redir)
2146 static const struct bpf_func_proto bpf_sk_redirect_hash_proto = {
2147 .func = bpf_sk_redirect_hash,
2149 .ret_type = RET_INTEGER,
2150 .arg1_type = ARG_PTR_TO_CTX,
2151 .arg2_type = ARG_CONST_MAP_PTR,
2152 .arg3_type = ARG_PTR_TO_MAP_KEY,
2153 .arg4_type = ARG_ANYTHING,
2156 BPF_CALL_4(bpf_sk_redirect_map, struct sk_buff *, skb,
2157 struct bpf_map *, map, u32, key, u64, flags)
2159 struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
2161 /* If user passes invalid input drop the packet. */
2162 if (unlikely(flags & ~(BPF_F_INGRESS)))
2165 tcb->bpf.flags = flags;
2166 tcb->bpf.sk_redir = __sock_map_lookup_elem(map, key);
2167 if (!tcb->bpf.sk_redir)
2173 struct sock *do_sk_redirect_map(struct sk_buff *skb)
2175 struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
2177 return tcb->bpf.sk_redir;
2180 static const struct bpf_func_proto bpf_sk_redirect_map_proto = {
2181 .func = bpf_sk_redirect_map,
2183 .ret_type = RET_INTEGER,
2184 .arg1_type = ARG_PTR_TO_CTX,
2185 .arg2_type = ARG_CONST_MAP_PTR,
2186 .arg3_type = ARG_ANYTHING,
2187 .arg4_type = ARG_ANYTHING,
2190 BPF_CALL_4(bpf_msg_redirect_hash, struct sk_msg_buff *, msg,
2191 struct bpf_map *, map, void *, key, u64, flags)
2193 /* If user passes invalid input drop the packet. */
2194 if (unlikely(flags & ~(BPF_F_INGRESS)))
2198 msg->sk_redir = __sock_hash_lookup_elem(map, key);
2205 static const struct bpf_func_proto bpf_msg_redirect_hash_proto = {
2206 .func = bpf_msg_redirect_hash,
2208 .ret_type = RET_INTEGER,
2209 .arg1_type = ARG_PTR_TO_CTX,
2210 .arg2_type = ARG_CONST_MAP_PTR,
2211 .arg3_type = ARG_PTR_TO_MAP_KEY,
2212 .arg4_type = ARG_ANYTHING,
2215 BPF_CALL_4(bpf_msg_redirect_map, struct sk_msg_buff *, msg,
2216 struct bpf_map *, map, u32, key, u64, flags)
2218 /* If user passes invalid input drop the packet. */
2219 if (unlikely(flags & ~(BPF_F_INGRESS)))
2223 msg->sk_redir = __sock_map_lookup_elem(map, key);
2230 struct sock *do_msg_redirect_map(struct sk_msg_buff *msg)
2232 return msg->sk_redir;
2235 static const struct bpf_func_proto bpf_msg_redirect_map_proto = {
2236 .func = bpf_msg_redirect_map,
2238 .ret_type = RET_INTEGER,
2239 .arg1_type = ARG_PTR_TO_CTX,
2240 .arg2_type = ARG_CONST_MAP_PTR,
2241 .arg3_type = ARG_ANYTHING,
2242 .arg4_type = ARG_ANYTHING,
2245 BPF_CALL_2(bpf_msg_apply_bytes, struct sk_msg_buff *, msg, u32, bytes)
2247 msg->apply_bytes = bytes;
2251 static const struct bpf_func_proto bpf_msg_apply_bytes_proto = {
2252 .func = bpf_msg_apply_bytes,
2254 .ret_type = RET_INTEGER,
2255 .arg1_type = ARG_PTR_TO_CTX,
2256 .arg2_type = ARG_ANYTHING,
2259 BPF_CALL_2(bpf_msg_cork_bytes, struct sk_msg_buff *, msg, u32, bytes)
2261 msg->cork_bytes = bytes;
2265 static const struct bpf_func_proto bpf_msg_cork_bytes_proto = {
2266 .func = bpf_msg_cork_bytes,
2268 .ret_type = RET_INTEGER,
2269 .arg1_type = ARG_PTR_TO_CTX,
2270 .arg2_type = ARG_ANYTHING,
2273 BPF_CALL_4(bpf_msg_pull_data,
2274 struct sk_msg_buff *, msg, u32, start, u32, end, u64, flags)
2276 unsigned int len = 0, offset = 0, copy = 0;
2277 struct scatterlist *sg = msg->sg_data;
2278 int first_sg, last_sg, i, shift;
2279 unsigned char *p, *to, *from;
2280 int bytes = end - start;
2283 if (unlikely(flags || end <= start))
2286 /* First find the starting scatterlist element */
2291 if (start < offset + len)
2294 if (i == MAX_SKB_FRAGS)
2296 } while (i != msg->sg_end);
2298 if (unlikely(start >= offset + len))
2301 if (!msg->sg_copy[i] && bytes <= len)
2306 /* At this point we need to linearize multiple scatterlist
2307 * elements or a single shared page. Either way we need to
2308 * copy into a linear buffer exclusively owned by BPF. Then
2309 * place the buffer in the scatterlist and fixup the original
2310 * entries by removing the entries now in the linear buffer
2311 * and shifting the remaining entries. For now we do not try
2312 * to copy partial entries to avoid complexity of running out
2313 * of sg_entry slots. The downside is reading a single byte
2314 * will copy the entire sg entry.
2317 copy += sg[i].length;
2319 if (i == MAX_SKB_FRAGS)
2323 } while (i != msg->sg_end);
2326 if (unlikely(copy < end - start))
2329 page = alloc_pages(__GFP_NOWARN | GFP_ATOMIC, get_order(copy));
2330 if (unlikely(!page))
2332 p = page_address(page);
2337 from = sg_virt(&sg[i]);
2341 memcpy(to, from, len);
2344 put_page(sg_page(&sg[i]));
2347 if (i == MAX_SKB_FRAGS)
2349 } while (i != last_sg);
2351 sg[first_sg].length = copy;
2352 sg_set_page(&sg[first_sg], page, copy, 0);
2354 /* To repair sg ring we need to shift entries. If we only
2355 * had a single entry though we can just replace it and
2356 * be done. Otherwise walk the ring and shift the entries.
2358 shift = last_sg - first_sg - 1;
2366 if (i + shift >= MAX_SKB_FRAGS)
2367 move_from = i + shift - MAX_SKB_FRAGS;
2369 move_from = i + shift;
2371 if (move_from == msg->sg_end)
2374 sg[i] = sg[move_from];
2375 sg[move_from].length = 0;
2376 sg[move_from].page_link = 0;
2377 sg[move_from].offset = 0;
2380 if (i == MAX_SKB_FRAGS)
2383 msg->sg_end -= shift;
2384 if (msg->sg_end < 0)
2385 msg->sg_end += MAX_SKB_FRAGS;
2387 msg->data = sg_virt(&sg[i]) + start - offset;
2388 msg->data_end = msg->data + bytes;
2393 static const struct bpf_func_proto bpf_msg_pull_data_proto = {
2394 .func = bpf_msg_pull_data,
2396 .ret_type = RET_INTEGER,
2397 .arg1_type = ARG_PTR_TO_CTX,
2398 .arg2_type = ARG_ANYTHING,
2399 .arg3_type = ARG_ANYTHING,
2400 .arg4_type = ARG_ANYTHING,
2403 BPF_CALL_1(bpf_get_cgroup_classid, const struct sk_buff *, skb)
2405 return task_get_classid(skb);
2408 static const struct bpf_func_proto bpf_get_cgroup_classid_proto = {
2409 .func = bpf_get_cgroup_classid,
2411 .ret_type = RET_INTEGER,
2412 .arg1_type = ARG_PTR_TO_CTX,
2415 BPF_CALL_1(bpf_get_route_realm, const struct sk_buff *, skb)
2417 return dst_tclassid(skb);
2420 static const struct bpf_func_proto bpf_get_route_realm_proto = {
2421 .func = bpf_get_route_realm,
2423 .ret_type = RET_INTEGER,
2424 .arg1_type = ARG_PTR_TO_CTX,
2427 BPF_CALL_1(bpf_get_hash_recalc, struct sk_buff *, skb)
2429 /* If skb_clear_hash() was called due to mangling, we can
2430 * trigger SW recalculation here. Later access to hash
2431 * can then use the inline skb->hash via context directly
2432 * instead of calling this helper again.
2434 return skb_get_hash(skb);
2437 static const struct bpf_func_proto bpf_get_hash_recalc_proto = {
2438 .func = bpf_get_hash_recalc,
2440 .ret_type = RET_INTEGER,
2441 .arg1_type = ARG_PTR_TO_CTX,
2444 BPF_CALL_1(bpf_set_hash_invalid, struct sk_buff *, skb)
2446 /* After all direct packet write, this can be used once for
2447 * triggering a lazy recalc on next skb_get_hash() invocation.
2449 skb_clear_hash(skb);
2453 static const struct bpf_func_proto bpf_set_hash_invalid_proto = {
2454 .func = bpf_set_hash_invalid,
2456 .ret_type = RET_INTEGER,
2457 .arg1_type = ARG_PTR_TO_CTX,
2460 BPF_CALL_2(bpf_set_hash, struct sk_buff *, skb, u32, hash)
2462 /* Set user specified hash as L4(+), so that it gets returned
2463 * on skb_get_hash() call unless BPF prog later on triggers a
2466 __skb_set_sw_hash(skb, hash, true);
2470 static const struct bpf_func_proto bpf_set_hash_proto = {
2471 .func = bpf_set_hash,
2473 .ret_type = RET_INTEGER,
2474 .arg1_type = ARG_PTR_TO_CTX,
2475 .arg2_type = ARG_ANYTHING,
2478 BPF_CALL_3(bpf_skb_vlan_push, struct sk_buff *, skb, __be16, vlan_proto,
2483 if (unlikely(vlan_proto != htons(ETH_P_8021Q) &&
2484 vlan_proto != htons(ETH_P_8021AD)))
2485 vlan_proto = htons(ETH_P_8021Q);
2487 bpf_push_mac_rcsum(skb);
2488 ret = skb_vlan_push(skb, vlan_proto, vlan_tci);
2489 bpf_pull_mac_rcsum(skb);
2491 bpf_compute_data_pointers(skb);
2495 static const struct bpf_func_proto bpf_skb_vlan_push_proto = {
2496 .func = bpf_skb_vlan_push,
2498 .ret_type = RET_INTEGER,
2499 .arg1_type = ARG_PTR_TO_CTX,
2500 .arg2_type = ARG_ANYTHING,
2501 .arg3_type = ARG_ANYTHING,
2504 BPF_CALL_1(bpf_skb_vlan_pop, struct sk_buff *, skb)
2508 bpf_push_mac_rcsum(skb);
2509 ret = skb_vlan_pop(skb);
2510 bpf_pull_mac_rcsum(skb);
2512 bpf_compute_data_pointers(skb);
2516 static const struct bpf_func_proto bpf_skb_vlan_pop_proto = {
2517 .func = bpf_skb_vlan_pop,
2519 .ret_type = RET_INTEGER,
2520 .arg1_type = ARG_PTR_TO_CTX,
2523 static int bpf_skb_generic_push(struct sk_buff *skb, u32 off, u32 len)
2525 /* Caller already did skb_cow() with len as headroom,
2526 * so no need to do it here.
2529 memmove(skb->data, skb->data + len, off);
2530 memset(skb->data + off, 0, len);
2532 /* No skb_postpush_rcsum(skb, skb->data + off, len)
2533 * needed here as it does not change the skb->csum
2534 * result for checksum complete when summing over
2540 static int bpf_skb_generic_pop(struct sk_buff *skb, u32 off, u32 len)
2542 /* skb_ensure_writable() is not needed here, as we're
2543 * already working on an uncloned skb.
2545 if (unlikely(!pskb_may_pull(skb, off + len)))
2548 skb_postpull_rcsum(skb, skb->data + off, len);
2549 memmove(skb->data + len, skb->data, off);
2550 __skb_pull(skb, len);
2555 static int bpf_skb_net_hdr_push(struct sk_buff *skb, u32 off, u32 len)
2557 bool trans_same = skb->transport_header == skb->network_header;
2560 /* There's no need for __skb_push()/__skb_pull() pair to
2561 * get to the start of the mac header as we're guaranteed
2562 * to always start from here under eBPF.
2564 ret = bpf_skb_generic_push(skb, off, len);
2566 skb->mac_header -= len;
2567 skb->network_header -= len;
2569 skb->transport_header = skb->network_header;
2575 static int bpf_skb_net_hdr_pop(struct sk_buff *skb, u32 off, u32 len)
2577 bool trans_same = skb->transport_header == skb->network_header;
2580 /* Same here, __skb_push()/__skb_pull() pair not needed. */
2581 ret = bpf_skb_generic_pop(skb, off, len);
2583 skb->mac_header += len;
2584 skb->network_header += len;
2586 skb->transport_header = skb->network_header;
2592 static int bpf_skb_proto_4_to_6(struct sk_buff *skb)
2594 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
2595 u32 off = skb_mac_header_len(skb);
2598 /* SCTP uses GSO_BY_FRAGS, thus cannot adjust it. */
2599 if (skb_is_gso(skb) && unlikely(skb_is_gso_sctp(skb)))
2602 ret = skb_cow(skb, len_diff);
2603 if (unlikely(ret < 0))
2606 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
2607 if (unlikely(ret < 0))
2610 if (skb_is_gso(skb)) {
2611 struct skb_shared_info *shinfo = skb_shinfo(skb);
2613 /* SKB_GSO_TCPV4 needs to be changed into
2616 if (shinfo->gso_type & SKB_GSO_TCPV4) {
2617 shinfo->gso_type &= ~SKB_GSO_TCPV4;
2618 shinfo->gso_type |= SKB_GSO_TCPV6;
2621 /* Due to IPv6 header, MSS needs to be downgraded. */
2622 skb_decrease_gso_size(shinfo, len_diff);
2623 /* Header must be checked, and gso_segs recomputed. */
2624 shinfo->gso_type |= SKB_GSO_DODGY;
2625 shinfo->gso_segs = 0;
2628 skb->protocol = htons(ETH_P_IPV6);
2629 skb_clear_hash(skb);
2634 static int bpf_skb_proto_6_to_4(struct sk_buff *skb)
2636 const u32 len_diff = sizeof(struct ipv6hdr) - sizeof(struct iphdr);
2637 u32 off = skb_mac_header_len(skb);
2640 /* SCTP uses GSO_BY_FRAGS, thus cannot adjust it. */
2641 if (skb_is_gso(skb) && unlikely(skb_is_gso_sctp(skb)))
2644 ret = skb_unclone(skb, GFP_ATOMIC);
2645 if (unlikely(ret < 0))
2648 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
2649 if (unlikely(ret < 0))
2652 if (skb_is_gso(skb)) {
2653 struct skb_shared_info *shinfo = skb_shinfo(skb);
2655 /* SKB_GSO_TCPV6 needs to be changed into
2658 if (shinfo->gso_type & SKB_GSO_TCPV6) {
2659 shinfo->gso_type &= ~SKB_GSO_TCPV6;
2660 shinfo->gso_type |= SKB_GSO_TCPV4;
2663 /* Due to IPv4 header, MSS can be upgraded. */
2664 skb_increase_gso_size(shinfo, len_diff);
2665 /* Header must be checked, and gso_segs recomputed. */
2666 shinfo->gso_type |= SKB_GSO_DODGY;
2667 shinfo->gso_segs = 0;
2670 skb->protocol = htons(ETH_P_IP);
2671 skb_clear_hash(skb);
2676 static int bpf_skb_proto_xlat(struct sk_buff *skb, __be16 to_proto)
2678 __be16 from_proto = skb->protocol;
2680 if (from_proto == htons(ETH_P_IP) &&
2681 to_proto == htons(ETH_P_IPV6))
2682 return bpf_skb_proto_4_to_6(skb);
2684 if (from_proto == htons(ETH_P_IPV6) &&
2685 to_proto == htons(ETH_P_IP))
2686 return bpf_skb_proto_6_to_4(skb);
2691 BPF_CALL_3(bpf_skb_change_proto, struct sk_buff *, skb, __be16, proto,
2696 if (unlikely(flags))
2699 /* General idea is that this helper does the basic groundwork
2700 * needed for changing the protocol, and eBPF program fills the
2701 * rest through bpf_skb_store_bytes(), bpf_lX_csum_replace()
2702 * and other helpers, rather than passing a raw buffer here.
2704 * The rationale is to keep this minimal and without a need to
2705 * deal with raw packet data. F.e. even if we would pass buffers
2706 * here, the program still needs to call the bpf_lX_csum_replace()
2707 * helpers anyway. Plus, this way we keep also separation of
2708 * concerns, since f.e. bpf_skb_store_bytes() should only take
2711 * Currently, additional options and extension header space are
2712 * not supported, but flags register is reserved so we can adapt
2713 * that. For offloads, we mark packet as dodgy, so that headers
2714 * need to be verified first.
2716 ret = bpf_skb_proto_xlat(skb, proto);
2717 bpf_compute_data_pointers(skb);
2721 static const struct bpf_func_proto bpf_skb_change_proto_proto = {
2722 .func = bpf_skb_change_proto,
2724 .ret_type = RET_INTEGER,
2725 .arg1_type = ARG_PTR_TO_CTX,
2726 .arg2_type = ARG_ANYTHING,
2727 .arg3_type = ARG_ANYTHING,
2730 BPF_CALL_2(bpf_skb_change_type, struct sk_buff *, skb, u32, pkt_type)
2732 /* We only allow a restricted subset to be changed for now. */
2733 if (unlikely(!skb_pkt_type_ok(skb->pkt_type) ||
2734 !skb_pkt_type_ok(pkt_type)))
2737 skb->pkt_type = pkt_type;
2741 static const struct bpf_func_proto bpf_skb_change_type_proto = {
2742 .func = bpf_skb_change_type,
2744 .ret_type = RET_INTEGER,
2745 .arg1_type = ARG_PTR_TO_CTX,
2746 .arg2_type = ARG_ANYTHING,
2749 static u32 bpf_skb_net_base_len(const struct sk_buff *skb)
2751 switch (skb->protocol) {
2752 case htons(ETH_P_IP):
2753 return sizeof(struct iphdr);
2754 case htons(ETH_P_IPV6):
2755 return sizeof(struct ipv6hdr);
2761 static int bpf_skb_net_grow(struct sk_buff *skb, u32 len_diff)
2763 u32 off = skb_mac_header_len(skb) + bpf_skb_net_base_len(skb);
2766 /* SCTP uses GSO_BY_FRAGS, thus cannot adjust it. */
2767 if (skb_is_gso(skb) && unlikely(skb_is_gso_sctp(skb)))
2770 ret = skb_cow(skb, len_diff);
2771 if (unlikely(ret < 0))
2774 ret = bpf_skb_net_hdr_push(skb, off, len_diff);
2775 if (unlikely(ret < 0))
2778 if (skb_is_gso(skb)) {
2779 struct skb_shared_info *shinfo = skb_shinfo(skb);
2781 /* Due to header grow, MSS needs to be downgraded. */
2782 skb_decrease_gso_size(shinfo, len_diff);
2783 /* Header must be checked, and gso_segs recomputed. */
2784 shinfo->gso_type |= SKB_GSO_DODGY;
2785 shinfo->gso_segs = 0;
2791 static int bpf_skb_net_shrink(struct sk_buff *skb, u32 len_diff)
2793 u32 off = skb_mac_header_len(skb) + bpf_skb_net_base_len(skb);
2796 /* SCTP uses GSO_BY_FRAGS, thus cannot adjust it. */
2797 if (skb_is_gso(skb) && unlikely(skb_is_gso_sctp(skb)))
2800 ret = skb_unclone(skb, GFP_ATOMIC);
2801 if (unlikely(ret < 0))
2804 ret = bpf_skb_net_hdr_pop(skb, off, len_diff);
2805 if (unlikely(ret < 0))
2808 if (skb_is_gso(skb)) {
2809 struct skb_shared_info *shinfo = skb_shinfo(skb);
2811 /* Due to header shrink, MSS can be upgraded. */
2812 skb_increase_gso_size(shinfo, len_diff);
2813 /* Header must be checked, and gso_segs recomputed. */
2814 shinfo->gso_type |= SKB_GSO_DODGY;
2815 shinfo->gso_segs = 0;
2821 static u32 __bpf_skb_max_len(const struct sk_buff *skb)
2823 return skb->dev ? skb->dev->mtu + skb->dev->hard_header_len :
2827 static int bpf_skb_adjust_net(struct sk_buff *skb, s32 len_diff)
2829 bool trans_same = skb->transport_header == skb->network_header;
2830 u32 len_cur, len_diff_abs = abs(len_diff);
2831 u32 len_min = bpf_skb_net_base_len(skb);
2832 u32 len_max = __bpf_skb_max_len(skb);
2833 __be16 proto = skb->protocol;
2834 bool shrink = len_diff < 0;
2837 if (unlikely(len_diff_abs > 0xfffU))
2839 if (unlikely(proto != htons(ETH_P_IP) &&
2840 proto != htons(ETH_P_IPV6)))
2843 len_cur = skb->len - skb_network_offset(skb);
2844 if (skb_transport_header_was_set(skb) && !trans_same)
2845 len_cur = skb_network_header_len(skb);
2846 if ((shrink && (len_diff_abs >= len_cur ||
2847 len_cur - len_diff_abs < len_min)) ||
2848 (!shrink && (skb->len + len_diff_abs > len_max &&
2852 ret = shrink ? bpf_skb_net_shrink(skb, len_diff_abs) :
2853 bpf_skb_net_grow(skb, len_diff_abs);
2855 bpf_compute_data_pointers(skb);
2859 BPF_CALL_4(bpf_skb_adjust_room, struct sk_buff *, skb, s32, len_diff,
2860 u32, mode, u64, flags)
2862 if (unlikely(flags))
2864 if (likely(mode == BPF_ADJ_ROOM_NET))
2865 return bpf_skb_adjust_net(skb, len_diff);
2870 static const struct bpf_func_proto bpf_skb_adjust_room_proto = {
2871 .func = bpf_skb_adjust_room,
2873 .ret_type = RET_INTEGER,
2874 .arg1_type = ARG_PTR_TO_CTX,
2875 .arg2_type = ARG_ANYTHING,
2876 .arg3_type = ARG_ANYTHING,
2877 .arg4_type = ARG_ANYTHING,
2880 static u32 __bpf_skb_min_len(const struct sk_buff *skb)
2882 u32 min_len = skb_network_offset(skb);
2884 if (skb_transport_header_was_set(skb))
2885 min_len = skb_transport_offset(skb);
2886 if (skb->ip_summed == CHECKSUM_PARTIAL)
2887 min_len = skb_checksum_start_offset(skb) +
2888 skb->csum_offset + sizeof(__sum16);
2892 static int bpf_skb_grow_rcsum(struct sk_buff *skb, unsigned int new_len)
2894 unsigned int old_len = skb->len;
2897 ret = __skb_grow_rcsum(skb, new_len);
2899 memset(skb->data + old_len, 0, new_len - old_len);
2903 static int bpf_skb_trim_rcsum(struct sk_buff *skb, unsigned int new_len)
2905 return __skb_trim_rcsum(skb, new_len);
2908 static inline int __bpf_skb_change_tail(struct sk_buff *skb, u32 new_len,
2911 u32 max_len = __bpf_skb_max_len(skb);
2912 u32 min_len = __bpf_skb_min_len(skb);
2915 if (unlikely(flags || new_len > max_len || new_len < min_len))
2917 if (skb->encapsulation)
2920 /* The basic idea of this helper is that it's performing the
2921 * needed work to either grow or trim an skb, and eBPF program
2922 * rewrites the rest via helpers like bpf_skb_store_bytes(),
2923 * bpf_lX_csum_replace() and others rather than passing a raw
2924 * buffer here. This one is a slow path helper and intended
2925 * for replies with control messages.
2927 * Like in bpf_skb_change_proto(), we want to keep this rather
2928 * minimal and without protocol specifics so that we are able
2929 * to separate concerns as in bpf_skb_store_bytes() should only
2930 * be the one responsible for writing buffers.
2932 * It's really expected to be a slow path operation here for
2933 * control message replies, so we're implicitly linearizing,
2934 * uncloning and drop offloads from the skb by this.
2936 ret = __bpf_try_make_writable(skb, skb->len);
2938 if (new_len > skb->len)
2939 ret = bpf_skb_grow_rcsum(skb, new_len);
2940 else if (new_len < skb->len)
2941 ret = bpf_skb_trim_rcsum(skb, new_len);
2942 if (!ret && skb_is_gso(skb))
2948 BPF_CALL_3(bpf_skb_change_tail, struct sk_buff *, skb, u32, new_len,
2951 int ret = __bpf_skb_change_tail(skb, new_len, flags);
2953 bpf_compute_data_pointers(skb);
2957 static const struct bpf_func_proto bpf_skb_change_tail_proto = {
2958 .func = bpf_skb_change_tail,
2960 .ret_type = RET_INTEGER,
2961 .arg1_type = ARG_PTR_TO_CTX,
2962 .arg2_type = ARG_ANYTHING,
2963 .arg3_type = ARG_ANYTHING,
2966 BPF_CALL_3(sk_skb_change_tail, struct sk_buff *, skb, u32, new_len,
2969 int ret = __bpf_skb_change_tail(skb, new_len, flags);
2971 bpf_compute_data_end_sk_skb(skb);
2975 static const struct bpf_func_proto sk_skb_change_tail_proto = {
2976 .func = sk_skb_change_tail,
2978 .ret_type = RET_INTEGER,
2979 .arg1_type = ARG_PTR_TO_CTX,
2980 .arg2_type = ARG_ANYTHING,
2981 .arg3_type = ARG_ANYTHING,
2984 static inline int __bpf_skb_change_head(struct sk_buff *skb, u32 head_room,
2987 u32 max_len = __bpf_skb_max_len(skb);
2988 u32 new_len = skb->len + head_room;
2991 if (unlikely(flags || (!skb_is_gso(skb) && new_len > max_len) ||
2992 new_len < skb->len))
2995 ret = skb_cow(skb, head_room);
2997 /* Idea for this helper is that we currently only
2998 * allow to expand on mac header. This means that
2999 * skb->protocol network header, etc, stay as is.
3000 * Compared to bpf_skb_change_tail(), we're more
3001 * flexible due to not needing to linearize or
3002 * reset GSO. Intention for this helper is to be
3003 * used by an L3 skb that needs to push mac header
3004 * for redirection into L2 device.
3006 __skb_push(skb, head_room);
3007 memset(skb->data, 0, head_room);
3008 skb_reset_mac_header(skb);
3014 BPF_CALL_3(bpf_skb_change_head, struct sk_buff *, skb, u32, head_room,
3017 int ret = __bpf_skb_change_head(skb, head_room, flags);
3019 bpf_compute_data_pointers(skb);
3023 static const struct bpf_func_proto bpf_skb_change_head_proto = {
3024 .func = bpf_skb_change_head,
3026 .ret_type = RET_INTEGER,
3027 .arg1_type = ARG_PTR_TO_CTX,
3028 .arg2_type = ARG_ANYTHING,
3029 .arg3_type = ARG_ANYTHING,
3032 BPF_CALL_3(sk_skb_change_head, struct sk_buff *, skb, u32, head_room,
3035 int ret = __bpf_skb_change_head(skb, head_room, flags);
3037 bpf_compute_data_end_sk_skb(skb);
3041 static const struct bpf_func_proto sk_skb_change_head_proto = {
3042 .func = sk_skb_change_head,
3044 .ret_type = RET_INTEGER,
3045 .arg1_type = ARG_PTR_TO_CTX,
3046 .arg2_type = ARG_ANYTHING,
3047 .arg3_type = ARG_ANYTHING,
3049 static unsigned long xdp_get_metalen(const struct xdp_buff *xdp)
3051 return xdp_data_meta_unsupported(xdp) ? 0 :
3052 xdp->data - xdp->data_meta;
3055 BPF_CALL_2(bpf_xdp_adjust_head, struct xdp_buff *, xdp, int, offset)
3057 void *xdp_frame_end = xdp->data_hard_start + sizeof(struct xdp_frame);
3058 unsigned long metalen = xdp_get_metalen(xdp);
3059 void *data_start = xdp_frame_end + metalen;
3060 void *data = xdp->data + offset;
3062 if (unlikely(data < data_start ||
3063 data > xdp->data_end - ETH_HLEN))
3067 memmove(xdp->data_meta + offset,
3068 xdp->data_meta, metalen);
3069 xdp->data_meta += offset;
3075 static const struct bpf_func_proto bpf_xdp_adjust_head_proto = {
3076 .func = bpf_xdp_adjust_head,
3078 .ret_type = RET_INTEGER,
3079 .arg1_type = ARG_PTR_TO_CTX,
3080 .arg2_type = ARG_ANYTHING,
3083 BPF_CALL_2(bpf_xdp_adjust_tail, struct xdp_buff *, xdp, int, offset)
3085 void *data_end = xdp->data_end + offset;
3087 /* only shrinking is allowed for now. */
3088 if (unlikely(offset >= 0))
3091 if (unlikely(data_end < xdp->data + ETH_HLEN))
3094 xdp->data_end = data_end;
3099 static const struct bpf_func_proto bpf_xdp_adjust_tail_proto = {
3100 .func = bpf_xdp_adjust_tail,
3102 .ret_type = RET_INTEGER,
3103 .arg1_type = ARG_PTR_TO_CTX,
3104 .arg2_type = ARG_ANYTHING,
3107 BPF_CALL_2(bpf_xdp_adjust_meta, struct xdp_buff *, xdp, int, offset)
3109 void *xdp_frame_end = xdp->data_hard_start + sizeof(struct xdp_frame);
3110 void *meta = xdp->data_meta + offset;
3111 unsigned long metalen = xdp->data - meta;
3113 if (xdp_data_meta_unsupported(xdp))
3115 if (unlikely(meta < xdp_frame_end ||
3118 if (unlikely((metalen & (sizeof(__u32) - 1)) ||
3122 xdp->data_meta = meta;
3127 static const struct bpf_func_proto bpf_xdp_adjust_meta_proto = {
3128 .func = bpf_xdp_adjust_meta,
3130 .ret_type = RET_INTEGER,
3131 .arg1_type = ARG_PTR_TO_CTX,
3132 .arg2_type = ARG_ANYTHING,
3135 static int __bpf_tx_xdp(struct net_device *dev,
3136 struct bpf_map *map,
3137 struct xdp_buff *xdp,
3140 struct xdp_frame *xdpf;
3143 if (!dev->netdev_ops->ndo_xdp_xmit) {
3147 err = xdp_ok_fwd_dev(dev, xdp->data_end - xdp->data);
3151 xdpf = convert_to_xdp_frame(xdp);
3152 if (unlikely(!xdpf))
3155 sent = dev->netdev_ops->ndo_xdp_xmit(dev, 1, &xdpf, XDP_XMIT_FLUSH);
3161 static int __bpf_tx_xdp_map(struct net_device *dev_rx, void *fwd,
3162 struct bpf_map *map,
3163 struct xdp_buff *xdp,
3168 switch (map->map_type) {
3169 case BPF_MAP_TYPE_DEVMAP: {
3170 struct bpf_dtab_netdev *dst = fwd;
3172 err = dev_map_enqueue(dst, xdp, dev_rx);
3175 __dev_map_insert_ctx(map, index);
3178 case BPF_MAP_TYPE_CPUMAP: {
3179 struct bpf_cpu_map_entry *rcpu = fwd;
3181 err = cpu_map_enqueue(rcpu, xdp, dev_rx);
3184 __cpu_map_insert_ctx(map, index);
3187 case BPF_MAP_TYPE_XSKMAP: {
3188 struct xdp_sock *xs = fwd;
3190 err = __xsk_map_redirect(map, xdp, xs);
3199 void xdp_do_flush_map(void)
3201 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
3202 struct bpf_map *map = ri->map_to_flush;
3204 ri->map_to_flush = NULL;
3206 switch (map->map_type) {
3207 case BPF_MAP_TYPE_DEVMAP:
3208 __dev_map_flush(map);
3210 case BPF_MAP_TYPE_CPUMAP:
3211 __cpu_map_flush(map);
3213 case BPF_MAP_TYPE_XSKMAP:
3214 __xsk_map_flush(map);
3221 EXPORT_SYMBOL_GPL(xdp_do_flush_map);
3223 static void *__xdp_map_lookup_elem(struct bpf_map *map, u32 index)
3225 switch (map->map_type) {
3226 case BPF_MAP_TYPE_DEVMAP:
3227 return __dev_map_lookup_elem(map, index);
3228 case BPF_MAP_TYPE_CPUMAP:
3229 return __cpu_map_lookup_elem(map, index);
3230 case BPF_MAP_TYPE_XSKMAP:
3231 return __xsk_map_lookup_elem(map, index);
3237 static inline bool xdp_map_invalid(const struct bpf_prog *xdp_prog,
3240 return (unsigned long)xdp_prog->aux != aux;
3243 static int xdp_do_redirect_map(struct net_device *dev, struct xdp_buff *xdp,
3244 struct bpf_prog *xdp_prog)
3246 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
3247 unsigned long map_owner = ri->map_owner;
3248 struct bpf_map *map = ri->map;
3249 u32 index = ri->ifindex;
3257 if (unlikely(xdp_map_invalid(xdp_prog, map_owner))) {
3263 fwd = __xdp_map_lookup_elem(map, index);
3268 if (ri->map_to_flush && ri->map_to_flush != map)
3271 err = __bpf_tx_xdp_map(dev, fwd, map, xdp, index);
3275 ri->map_to_flush = map;
3276 _trace_xdp_redirect_map(dev, xdp_prog, fwd, map, index);
3279 _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map, index, err);
3283 int xdp_do_redirect(struct net_device *dev, struct xdp_buff *xdp,
3284 struct bpf_prog *xdp_prog)
3286 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
3287 struct net_device *fwd;
3288 u32 index = ri->ifindex;
3292 return xdp_do_redirect_map(dev, xdp, xdp_prog);
3294 fwd = dev_get_by_index_rcu(dev_net(dev), index);
3296 if (unlikely(!fwd)) {
3301 err = __bpf_tx_xdp(fwd, NULL, xdp, 0);
3305 _trace_xdp_redirect(dev, xdp_prog, index);
3308 _trace_xdp_redirect_err(dev, xdp_prog, index, err);
3311 EXPORT_SYMBOL_GPL(xdp_do_redirect);
3313 static int xdp_do_generic_redirect_map(struct net_device *dev,
3314 struct sk_buff *skb,
3315 struct xdp_buff *xdp,
3316 struct bpf_prog *xdp_prog)
3318 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
3319 unsigned long map_owner = ri->map_owner;
3320 struct bpf_map *map = ri->map;
3321 u32 index = ri->ifindex;
3329 if (unlikely(xdp_map_invalid(xdp_prog, map_owner))) {
3334 fwd = __xdp_map_lookup_elem(map, index);
3335 if (unlikely(!fwd)) {
3340 if (map->map_type == BPF_MAP_TYPE_DEVMAP) {
3341 struct bpf_dtab_netdev *dst = fwd;
3343 err = dev_map_generic_redirect(dst, skb, xdp_prog);
3346 } else if (map->map_type == BPF_MAP_TYPE_XSKMAP) {
3347 struct xdp_sock *xs = fwd;
3349 err = xsk_generic_rcv(xs, xdp);
3354 /* TODO: Handle BPF_MAP_TYPE_CPUMAP */
3359 _trace_xdp_redirect_map(dev, xdp_prog, fwd, map, index);
3362 _trace_xdp_redirect_map_err(dev, xdp_prog, fwd, map, index, err);
3366 int xdp_do_generic_redirect(struct net_device *dev, struct sk_buff *skb,
3367 struct xdp_buff *xdp, struct bpf_prog *xdp_prog)
3369 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
3370 u32 index = ri->ifindex;
3371 struct net_device *fwd;
3375 return xdp_do_generic_redirect_map(dev, skb, xdp, xdp_prog);
3378 fwd = dev_get_by_index_rcu(dev_net(dev), index);
3379 if (unlikely(!fwd)) {
3384 err = xdp_ok_fwd_dev(fwd, skb->len);
3389 _trace_xdp_redirect(dev, xdp_prog, index);
3390 generic_xdp_tx(skb, xdp_prog);
3393 _trace_xdp_redirect_err(dev, xdp_prog, index, err);
3396 EXPORT_SYMBOL_GPL(xdp_do_generic_redirect);
3398 BPF_CALL_2(bpf_xdp_redirect, u32, ifindex, u64, flags)
3400 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
3402 if (unlikely(flags))
3405 ri->ifindex = ifindex;
3410 return XDP_REDIRECT;
3413 static const struct bpf_func_proto bpf_xdp_redirect_proto = {
3414 .func = bpf_xdp_redirect,
3416 .ret_type = RET_INTEGER,
3417 .arg1_type = ARG_ANYTHING,
3418 .arg2_type = ARG_ANYTHING,
3421 BPF_CALL_4(bpf_xdp_redirect_map, struct bpf_map *, map, u32, ifindex, u64, flags,
3422 unsigned long, map_owner)
3424 struct redirect_info *ri = this_cpu_ptr(&redirect_info);
3426 if (unlikely(flags))
3429 ri->ifindex = ifindex;
3432 ri->map_owner = map_owner;
3434 return XDP_REDIRECT;
3437 /* Note, arg4 is hidden from users and populated by the verifier
3438 * with the right pointer.
3440 static const struct bpf_func_proto bpf_xdp_redirect_map_proto = {
3441 .func = bpf_xdp_redirect_map,
3443 .ret_type = RET_INTEGER,
3444 .arg1_type = ARG_CONST_MAP_PTR,
3445 .arg2_type = ARG_ANYTHING,
3446 .arg3_type = ARG_ANYTHING,
3449 static unsigned long bpf_skb_copy(void *dst_buff, const void *skb,
3450 unsigned long off, unsigned long len)
3452 void *ptr = skb_header_pointer(skb, off, len, dst_buff);
3456 if (ptr != dst_buff)
3457 memcpy(dst_buff, ptr, len);
3462 BPF_CALL_5(bpf_skb_event_output, struct sk_buff *, skb, struct bpf_map *, map,
3463 u64, flags, void *, meta, u64, meta_size)
3465 u64 skb_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
3467 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
3469 if (unlikely(skb_size > skb->len))
3472 return bpf_event_output(map, flags, meta, meta_size, skb, skb_size,
3476 static const struct bpf_func_proto bpf_skb_event_output_proto = {
3477 .func = bpf_skb_event_output,
3479 .ret_type = RET_INTEGER,
3480 .arg1_type = ARG_PTR_TO_CTX,
3481 .arg2_type = ARG_CONST_MAP_PTR,
3482 .arg3_type = ARG_ANYTHING,
3483 .arg4_type = ARG_PTR_TO_MEM,
3484 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
3487 static unsigned short bpf_tunnel_key_af(u64 flags)
3489 return flags & BPF_F_TUNINFO_IPV6 ? AF_INET6 : AF_INET;
3492 BPF_CALL_4(bpf_skb_get_tunnel_key, struct sk_buff *, skb, struct bpf_tunnel_key *, to,
3493 u32, size, u64, flags)
3495 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
3496 u8 compat[sizeof(struct bpf_tunnel_key)];
3500 if (unlikely(!info || (flags & ~(BPF_F_TUNINFO_IPV6)))) {
3504 if (ip_tunnel_info_af(info) != bpf_tunnel_key_af(flags)) {
3508 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
3511 case offsetof(struct bpf_tunnel_key, tunnel_label):
3512 case offsetof(struct bpf_tunnel_key, tunnel_ext):
3514 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
3515 /* Fixup deprecated structure layouts here, so we have
3516 * a common path later on.
3518 if (ip_tunnel_info_af(info) != AF_INET)
3521 to = (struct bpf_tunnel_key *)compat;
3528 to->tunnel_id = be64_to_cpu(info->key.tun_id);
3529 to->tunnel_tos = info->key.tos;
3530 to->tunnel_ttl = info->key.ttl;
3533 if (flags & BPF_F_TUNINFO_IPV6) {
3534 memcpy(to->remote_ipv6, &info->key.u.ipv6.src,
3535 sizeof(to->remote_ipv6));
3536 to->tunnel_label = be32_to_cpu(info->key.label);
3538 to->remote_ipv4 = be32_to_cpu(info->key.u.ipv4.src);
3539 memset(&to->remote_ipv6[1], 0, sizeof(__u32) * 3);
3540 to->tunnel_label = 0;
3543 if (unlikely(size != sizeof(struct bpf_tunnel_key)))
3544 memcpy(to_orig, to, size);
3548 memset(to_orig, 0, size);
3552 static const struct bpf_func_proto bpf_skb_get_tunnel_key_proto = {
3553 .func = bpf_skb_get_tunnel_key,
3555 .ret_type = RET_INTEGER,
3556 .arg1_type = ARG_PTR_TO_CTX,
3557 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
3558 .arg3_type = ARG_CONST_SIZE,
3559 .arg4_type = ARG_ANYTHING,
3562 BPF_CALL_3(bpf_skb_get_tunnel_opt, struct sk_buff *, skb, u8 *, to, u32, size)
3564 const struct ip_tunnel_info *info = skb_tunnel_info(skb);
3567 if (unlikely(!info ||
3568 !(info->key.tun_flags & TUNNEL_OPTIONS_PRESENT))) {
3572 if (unlikely(size < info->options_len)) {
3577 ip_tunnel_info_opts_get(to, info);
3578 if (size > info->options_len)
3579 memset(to + info->options_len, 0, size - info->options_len);
3581 return info->options_len;
3583 memset(to, 0, size);
3587 static const struct bpf_func_proto bpf_skb_get_tunnel_opt_proto = {
3588 .func = bpf_skb_get_tunnel_opt,
3590 .ret_type = RET_INTEGER,
3591 .arg1_type = ARG_PTR_TO_CTX,
3592 .arg2_type = ARG_PTR_TO_UNINIT_MEM,
3593 .arg3_type = ARG_CONST_SIZE,
3596 static struct metadata_dst __percpu *md_dst;
3598 BPF_CALL_4(bpf_skb_set_tunnel_key, struct sk_buff *, skb,
3599 const struct bpf_tunnel_key *, from, u32, size, u64, flags)
3601 struct metadata_dst *md = this_cpu_ptr(md_dst);
3602 u8 compat[sizeof(struct bpf_tunnel_key)];
3603 struct ip_tunnel_info *info;
3605 if (unlikely(flags & ~(BPF_F_TUNINFO_IPV6 | BPF_F_ZERO_CSUM_TX |
3606 BPF_F_DONT_FRAGMENT | BPF_F_SEQ_NUMBER)))
3608 if (unlikely(size != sizeof(struct bpf_tunnel_key))) {
3610 case offsetof(struct bpf_tunnel_key, tunnel_label):
3611 case offsetof(struct bpf_tunnel_key, tunnel_ext):
3612 case offsetof(struct bpf_tunnel_key, remote_ipv6[1]):
3613 /* Fixup deprecated structure layouts here, so we have
3614 * a common path later on.
3616 memcpy(compat, from, size);
3617 memset(compat + size, 0, sizeof(compat) - size);
3618 from = (const struct bpf_tunnel_key *) compat;
3624 if (unlikely((!(flags & BPF_F_TUNINFO_IPV6) && from->tunnel_label) ||
3629 dst_hold((struct dst_entry *) md);
3630 skb_dst_set(skb, (struct dst_entry *) md);
3632 info = &md->u.tun_info;
3633 memset(info, 0, sizeof(*info));
3634 info->mode = IP_TUNNEL_INFO_TX;
3636 info->key.tun_flags = TUNNEL_KEY | TUNNEL_CSUM | TUNNEL_NOCACHE;
3637 if (flags & BPF_F_DONT_FRAGMENT)
3638 info->key.tun_flags |= TUNNEL_DONT_FRAGMENT;
3639 if (flags & BPF_F_ZERO_CSUM_TX)
3640 info->key.tun_flags &= ~TUNNEL_CSUM;
3641 if (flags & BPF_F_SEQ_NUMBER)
3642 info->key.tun_flags |= TUNNEL_SEQ;
3644 info->key.tun_id = cpu_to_be64(from->tunnel_id);
3645 info->key.tos = from->tunnel_tos;
3646 info->key.ttl = from->tunnel_ttl;
3648 if (flags & BPF_F_TUNINFO_IPV6) {
3649 info->mode |= IP_TUNNEL_INFO_IPV6;
3650 memcpy(&info->key.u.ipv6.dst, from->remote_ipv6,
3651 sizeof(from->remote_ipv6));
3652 info->key.label = cpu_to_be32(from->tunnel_label) &
3653 IPV6_FLOWLABEL_MASK;
3655 info->key.u.ipv4.dst = cpu_to_be32(from->remote_ipv4);
3661 static const struct bpf_func_proto bpf_skb_set_tunnel_key_proto = {
3662 .func = bpf_skb_set_tunnel_key,
3664 .ret_type = RET_INTEGER,
3665 .arg1_type = ARG_PTR_TO_CTX,
3666 .arg2_type = ARG_PTR_TO_MEM,
3667 .arg3_type = ARG_CONST_SIZE,
3668 .arg4_type = ARG_ANYTHING,
3671 BPF_CALL_3(bpf_skb_set_tunnel_opt, struct sk_buff *, skb,
3672 const u8 *, from, u32, size)
3674 struct ip_tunnel_info *info = skb_tunnel_info(skb);
3675 const struct metadata_dst *md = this_cpu_ptr(md_dst);
3677 if (unlikely(info != &md->u.tun_info || (size & (sizeof(u32) - 1))))
3679 if (unlikely(size > IP_TUNNEL_OPTS_MAX))
3682 ip_tunnel_info_opts_set(info, from, size);
3687 static const struct bpf_func_proto bpf_skb_set_tunnel_opt_proto = {
3688 .func = bpf_skb_set_tunnel_opt,
3690 .ret_type = RET_INTEGER,
3691 .arg1_type = ARG_PTR_TO_CTX,
3692 .arg2_type = ARG_PTR_TO_MEM,
3693 .arg3_type = ARG_CONST_SIZE,
3696 static const struct bpf_func_proto *
3697 bpf_get_skb_set_tunnel_proto(enum bpf_func_id which)
3700 struct metadata_dst __percpu *tmp;
3702 tmp = metadata_dst_alloc_percpu(IP_TUNNEL_OPTS_MAX,
3707 if (cmpxchg(&md_dst, NULL, tmp))
3708 metadata_dst_free_percpu(tmp);
3712 case BPF_FUNC_skb_set_tunnel_key:
3713 return &bpf_skb_set_tunnel_key_proto;
3714 case BPF_FUNC_skb_set_tunnel_opt:
3715 return &bpf_skb_set_tunnel_opt_proto;
3721 BPF_CALL_3(bpf_skb_under_cgroup, struct sk_buff *, skb, struct bpf_map *, map,
3724 struct bpf_array *array = container_of(map, struct bpf_array, map);
3725 struct cgroup *cgrp;
3728 sk = skb_to_full_sk(skb);
3729 if (!sk || !sk_fullsock(sk))
3731 if (unlikely(idx >= array->map.max_entries))
3734 cgrp = READ_ONCE(array->ptrs[idx]);
3735 if (unlikely(!cgrp))
3738 return sk_under_cgroup_hierarchy(sk, cgrp);
3741 static const struct bpf_func_proto bpf_skb_under_cgroup_proto = {
3742 .func = bpf_skb_under_cgroup,
3744 .ret_type = RET_INTEGER,
3745 .arg1_type = ARG_PTR_TO_CTX,
3746 .arg2_type = ARG_CONST_MAP_PTR,
3747 .arg3_type = ARG_ANYTHING,
3750 #ifdef CONFIG_SOCK_CGROUP_DATA
3751 BPF_CALL_1(bpf_skb_cgroup_id, const struct sk_buff *, skb)
3753 struct sock *sk = skb_to_full_sk(skb);
3754 struct cgroup *cgrp;
3756 if (!sk || !sk_fullsock(sk))
3759 cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
3760 return cgrp->kn->id.id;
3763 static const struct bpf_func_proto bpf_skb_cgroup_id_proto = {
3764 .func = bpf_skb_cgroup_id,
3766 .ret_type = RET_INTEGER,
3767 .arg1_type = ARG_PTR_TO_CTX,
3771 static unsigned long bpf_xdp_copy(void *dst_buff, const void *src_buff,
3772 unsigned long off, unsigned long len)
3774 memcpy(dst_buff, src_buff + off, len);
3778 BPF_CALL_5(bpf_xdp_event_output, struct xdp_buff *, xdp, struct bpf_map *, map,
3779 u64, flags, void *, meta, u64, meta_size)
3781 u64 xdp_size = (flags & BPF_F_CTXLEN_MASK) >> 32;
3783 if (unlikely(flags & ~(BPF_F_CTXLEN_MASK | BPF_F_INDEX_MASK)))
3785 if (unlikely(xdp_size > (unsigned long)(xdp->data_end - xdp->data)))
3788 return bpf_event_output(map, flags, meta, meta_size, xdp->data,
3789 xdp_size, bpf_xdp_copy);
3792 static const struct bpf_func_proto bpf_xdp_event_output_proto = {
3793 .func = bpf_xdp_event_output,
3795 .ret_type = RET_INTEGER,
3796 .arg1_type = ARG_PTR_TO_CTX,
3797 .arg2_type = ARG_CONST_MAP_PTR,
3798 .arg3_type = ARG_ANYTHING,
3799 .arg4_type = ARG_PTR_TO_MEM,
3800 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
3803 BPF_CALL_1(bpf_get_socket_cookie, struct sk_buff *, skb)
3805 return skb->sk ? sock_gen_cookie(skb->sk) : 0;
3808 static const struct bpf_func_proto bpf_get_socket_cookie_proto = {
3809 .func = bpf_get_socket_cookie,
3811 .ret_type = RET_INTEGER,
3812 .arg1_type = ARG_PTR_TO_CTX,
3815 BPF_CALL_1(bpf_get_socket_uid, struct sk_buff *, skb)
3817 struct sock *sk = sk_to_full_sk(skb->sk);
3820 if (!sk || !sk_fullsock(sk))
3822 kuid = sock_net_uid(sock_net(sk), sk);
3823 return from_kuid_munged(sock_net(sk)->user_ns, kuid);
3826 static const struct bpf_func_proto bpf_get_socket_uid_proto = {
3827 .func = bpf_get_socket_uid,
3829 .ret_type = RET_INTEGER,
3830 .arg1_type = ARG_PTR_TO_CTX,
3833 BPF_CALL_5(bpf_setsockopt, struct bpf_sock_ops_kern *, bpf_sock,
3834 int, level, int, optname, char *, optval, int, optlen)
3836 struct sock *sk = bpf_sock->sk;
3840 if (!sk_fullsock(sk))
3843 if (level == SOL_SOCKET) {
3844 if (optlen != sizeof(int))
3846 val = *((int *)optval);
3848 /* Only some socketops are supported */
3851 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
3852 sk->sk_rcvbuf = max_t(int, val * 2, SOCK_MIN_RCVBUF);
3855 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
3856 sk->sk_sndbuf = max_t(int, val * 2, SOCK_MIN_SNDBUF);
3858 case SO_MAX_PACING_RATE:
3859 sk->sk_max_pacing_rate = val;
3860 sk->sk_pacing_rate = min(sk->sk_pacing_rate,
3861 sk->sk_max_pacing_rate);
3864 sk->sk_priority = val;
3869 sk->sk_rcvlowat = val ? : 1;
3878 } else if (level == SOL_IP) {
3879 if (optlen != sizeof(int) || sk->sk_family != AF_INET)
3882 val = *((int *)optval);
3883 /* Only some options are supported */
3886 if (val < -1 || val > 0xff) {
3889 struct inet_sock *inet = inet_sk(sk);
3899 #if IS_ENABLED(CONFIG_IPV6)
3900 } else if (level == SOL_IPV6) {
3901 if (optlen != sizeof(int) || sk->sk_family != AF_INET6)
3904 val = *((int *)optval);
3905 /* Only some options are supported */
3908 if (val < -1 || val > 0xff) {
3911 struct ipv6_pinfo *np = inet6_sk(sk);
3922 } else if (level == SOL_TCP &&
3923 sk->sk_prot->setsockopt == tcp_setsockopt) {
3924 if (optname == TCP_CONGESTION) {
3925 char name[TCP_CA_NAME_MAX];
3926 bool reinit = bpf_sock->op > BPF_SOCK_OPS_NEEDS_ECN;
3928 strncpy(name, optval, min_t(long, optlen,
3929 TCP_CA_NAME_MAX-1));
3930 name[TCP_CA_NAME_MAX-1] = 0;
3931 ret = tcp_set_congestion_control(sk, name, false,
3934 struct tcp_sock *tp = tcp_sk(sk);
3936 if (optlen != sizeof(int))
3939 val = *((int *)optval);
3940 /* Only some options are supported */
3943 if (val <= 0 || tp->data_segs_out > 0)
3948 case TCP_BPF_SNDCWND_CLAMP:
3952 tp->snd_cwnd_clamp = val;
3953 tp->snd_ssthresh = val;
3967 static const struct bpf_func_proto bpf_setsockopt_proto = {
3968 .func = bpf_setsockopt,
3970 .ret_type = RET_INTEGER,
3971 .arg1_type = ARG_PTR_TO_CTX,
3972 .arg2_type = ARG_ANYTHING,
3973 .arg3_type = ARG_ANYTHING,
3974 .arg4_type = ARG_PTR_TO_MEM,
3975 .arg5_type = ARG_CONST_SIZE,
3978 BPF_CALL_5(bpf_getsockopt, struct bpf_sock_ops_kern *, bpf_sock,
3979 int, level, int, optname, char *, optval, int, optlen)
3981 struct sock *sk = bpf_sock->sk;
3983 if (!sk_fullsock(sk))
3987 if (level == SOL_TCP && sk->sk_prot->getsockopt == tcp_getsockopt) {
3988 if (optname == TCP_CONGESTION) {
3989 struct inet_connection_sock *icsk = inet_csk(sk);
3991 if (!icsk->icsk_ca_ops || optlen <= 1)
3993 strncpy(optval, icsk->icsk_ca_ops->name, optlen);
3994 optval[optlen - 1] = 0;
3998 } else if (level == SOL_IP) {
3999 struct inet_sock *inet = inet_sk(sk);
4001 if (optlen != sizeof(int) || sk->sk_family != AF_INET)
4004 /* Only some options are supported */
4007 *((int *)optval) = (int)inet->tos;
4012 #if IS_ENABLED(CONFIG_IPV6)
4013 } else if (level == SOL_IPV6) {
4014 struct ipv6_pinfo *np = inet6_sk(sk);
4016 if (optlen != sizeof(int) || sk->sk_family != AF_INET6)
4019 /* Only some options are supported */
4022 *((int *)optval) = (int)np->tclass;
4034 memset(optval, 0, optlen);
4038 static const struct bpf_func_proto bpf_getsockopt_proto = {
4039 .func = bpf_getsockopt,
4041 .ret_type = RET_INTEGER,
4042 .arg1_type = ARG_PTR_TO_CTX,
4043 .arg2_type = ARG_ANYTHING,
4044 .arg3_type = ARG_ANYTHING,
4045 .arg4_type = ARG_PTR_TO_UNINIT_MEM,
4046 .arg5_type = ARG_CONST_SIZE,
4049 BPF_CALL_2(bpf_sock_ops_cb_flags_set, struct bpf_sock_ops_kern *, bpf_sock,
4052 struct sock *sk = bpf_sock->sk;
4053 int val = argval & BPF_SOCK_OPS_ALL_CB_FLAGS;
4055 if (!IS_ENABLED(CONFIG_INET) || !sk_fullsock(sk))
4059 tcp_sk(sk)->bpf_sock_ops_cb_flags = val;
4061 return argval & (~BPF_SOCK_OPS_ALL_CB_FLAGS);
4064 static const struct bpf_func_proto bpf_sock_ops_cb_flags_set_proto = {
4065 .func = bpf_sock_ops_cb_flags_set,
4067 .ret_type = RET_INTEGER,
4068 .arg1_type = ARG_PTR_TO_CTX,
4069 .arg2_type = ARG_ANYTHING,
4072 const struct ipv6_bpf_stub *ipv6_bpf_stub __read_mostly;
4073 EXPORT_SYMBOL_GPL(ipv6_bpf_stub);
4075 BPF_CALL_3(bpf_bind, struct bpf_sock_addr_kern *, ctx, struct sockaddr *, addr,
4079 struct sock *sk = ctx->sk;
4082 /* Binding to port can be expensive so it's prohibited in the helper.
4083 * Only binding to IP is supported.
4086 if (addr->sa_family == AF_INET) {
4087 if (addr_len < sizeof(struct sockaddr_in))
4089 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
4091 return __inet_bind(sk, addr, addr_len, true, false);
4092 #if IS_ENABLED(CONFIG_IPV6)
4093 } else if (addr->sa_family == AF_INET6) {
4094 if (addr_len < SIN6_LEN_RFC2133)
4096 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
4098 /* ipv6_bpf_stub cannot be NULL, since it's called from
4099 * bpf_cgroup_inet6_connect hook and ipv6 is already loaded
4101 return ipv6_bpf_stub->inet6_bind(sk, addr, addr_len, true, false);
4102 #endif /* CONFIG_IPV6 */
4104 #endif /* CONFIG_INET */
4106 return -EAFNOSUPPORT;
4109 static const struct bpf_func_proto bpf_bind_proto = {
4112 .ret_type = RET_INTEGER,
4113 .arg1_type = ARG_PTR_TO_CTX,
4114 .arg2_type = ARG_PTR_TO_MEM,
4115 .arg3_type = ARG_CONST_SIZE,
4119 BPF_CALL_5(bpf_skb_get_xfrm_state, struct sk_buff *, skb, u32, index,
4120 struct bpf_xfrm_state *, to, u32, size, u64, flags)
4122 const struct sec_path *sp = skb_sec_path(skb);
4123 const struct xfrm_state *x;
4125 if (!sp || unlikely(index >= sp->len || flags))
4128 x = sp->xvec[index];
4130 if (unlikely(size != sizeof(struct bpf_xfrm_state)))
4133 to->reqid = x->props.reqid;
4134 to->spi = x->id.spi;
4135 to->family = x->props.family;
4138 if (to->family == AF_INET6) {
4139 memcpy(to->remote_ipv6, x->props.saddr.a6,
4140 sizeof(to->remote_ipv6));
4142 to->remote_ipv4 = x->props.saddr.a4;
4143 memset(&to->remote_ipv6[1], 0, sizeof(__u32) * 3);
4148 memset(to, 0, size);
4152 static const struct bpf_func_proto bpf_skb_get_xfrm_state_proto = {
4153 .func = bpf_skb_get_xfrm_state,
4155 .ret_type = RET_INTEGER,
4156 .arg1_type = ARG_PTR_TO_CTX,
4157 .arg2_type = ARG_ANYTHING,
4158 .arg3_type = ARG_PTR_TO_UNINIT_MEM,
4159 .arg4_type = ARG_CONST_SIZE,
4160 .arg5_type = ARG_ANYTHING,
4164 #if IS_ENABLED(CONFIG_INET) || IS_ENABLED(CONFIG_IPV6)
4165 static int bpf_fib_set_fwd_params(struct bpf_fib_lookup *params,
4166 const struct neighbour *neigh,
4167 const struct net_device *dev)
4169 memcpy(params->dmac, neigh->ha, ETH_ALEN);
4170 memcpy(params->smac, dev->dev_addr, ETH_ALEN);
4171 params->h_vlan_TCI = 0;
4172 params->h_vlan_proto = 0;
4173 params->ifindex = dev->ifindex;
4179 #if IS_ENABLED(CONFIG_INET)
4180 static int bpf_ipv4_fib_lookup(struct net *net, struct bpf_fib_lookup *params,
4181 u32 flags, bool check_mtu)
4183 struct in_device *in_dev;
4184 struct neighbour *neigh;
4185 struct net_device *dev;
4186 struct fib_result res;
4192 dev = dev_get_by_index_rcu(net, params->ifindex);
4196 /* verify forwarding is enabled on this interface */
4197 in_dev = __in_dev_get_rcu(dev);
4198 if (unlikely(!in_dev || !IN_DEV_FORWARD(in_dev)))
4199 return BPF_FIB_LKUP_RET_FWD_DISABLED;
4201 if (flags & BPF_FIB_LOOKUP_OUTPUT) {
4203 fl4.flowi4_oif = params->ifindex;
4205 fl4.flowi4_iif = params->ifindex;
4208 fl4.flowi4_tos = params->tos & IPTOS_RT_MASK;
4209 fl4.flowi4_scope = RT_SCOPE_UNIVERSE;
4210 fl4.flowi4_flags = 0;
4212 fl4.flowi4_proto = params->l4_protocol;
4213 fl4.daddr = params->ipv4_dst;
4214 fl4.saddr = params->ipv4_src;
4215 fl4.fl4_sport = params->sport;
4216 fl4.fl4_dport = params->dport;
4218 if (flags & BPF_FIB_LOOKUP_DIRECT) {
4219 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
4220 struct fib_table *tb;
4222 tb = fib_get_table(net, tbid);
4224 return BPF_FIB_LKUP_RET_NOT_FWDED;
4226 err = fib_table_lookup(tb, &fl4, &res, FIB_LOOKUP_NOREF);
4228 fl4.flowi4_mark = 0;
4229 fl4.flowi4_secid = 0;
4230 fl4.flowi4_tun_key.tun_id = 0;
4231 fl4.flowi4_uid = sock_net_uid(net, NULL);
4233 err = fib_lookup(net, &fl4, &res, FIB_LOOKUP_NOREF);
4237 /* map fib lookup errors to RTN_ type */
4239 return BPF_FIB_LKUP_RET_BLACKHOLE;
4240 if (err == -EHOSTUNREACH)
4241 return BPF_FIB_LKUP_RET_UNREACHABLE;
4243 return BPF_FIB_LKUP_RET_PROHIBIT;
4245 return BPF_FIB_LKUP_RET_NOT_FWDED;
4248 if (res.type != RTN_UNICAST)
4249 return BPF_FIB_LKUP_RET_NOT_FWDED;
4251 if (res.fi->fib_nhs > 1)
4252 fib_select_path(net, &res, &fl4, NULL);
4255 mtu = ip_mtu_from_fib_result(&res, params->ipv4_dst);
4256 if (params->tot_len > mtu)
4257 return BPF_FIB_LKUP_RET_FRAG_NEEDED;
4260 nh = &res.fi->fib_nh[res.nh_sel];
4262 /* do not handle lwt encaps right now */
4263 if (nh->nh_lwtstate)
4264 return BPF_FIB_LKUP_RET_UNSUPP_LWT;
4268 params->ipv4_dst = nh->nh_gw;
4270 params->rt_metric = res.fi->fib_priority;
4272 /* xdp and cls_bpf programs are run in RCU-bh so
4273 * rcu_read_lock_bh is not needed here
4275 neigh = __ipv4_neigh_lookup_noref(dev, (__force u32)params->ipv4_dst);
4277 return BPF_FIB_LKUP_RET_NO_NEIGH;
4279 return bpf_fib_set_fwd_params(params, neigh, dev);
4283 #if IS_ENABLED(CONFIG_IPV6)
4284 static int bpf_ipv6_fib_lookup(struct net *net, struct bpf_fib_lookup *params,
4285 u32 flags, bool check_mtu)
4287 struct in6_addr *src = (struct in6_addr *) params->ipv6_src;
4288 struct in6_addr *dst = (struct in6_addr *) params->ipv6_dst;
4289 struct neighbour *neigh;
4290 struct net_device *dev;
4291 struct inet6_dev *idev;
4292 struct fib6_info *f6i;
4298 /* link local addresses are never forwarded */
4299 if (rt6_need_strict(dst) || rt6_need_strict(src))
4300 return BPF_FIB_LKUP_RET_NOT_FWDED;
4302 dev = dev_get_by_index_rcu(net, params->ifindex);
4306 idev = __in6_dev_get_safely(dev);
4307 if (unlikely(!idev || !net->ipv6.devconf_all->forwarding))
4308 return BPF_FIB_LKUP_RET_FWD_DISABLED;
4310 if (flags & BPF_FIB_LOOKUP_OUTPUT) {
4312 oif = fl6.flowi6_oif = params->ifindex;
4314 oif = fl6.flowi6_iif = params->ifindex;
4316 strict = RT6_LOOKUP_F_HAS_SADDR;
4318 fl6.flowlabel = params->flowinfo;
4319 fl6.flowi6_scope = 0;
4320 fl6.flowi6_flags = 0;
4323 fl6.flowi6_proto = params->l4_protocol;
4326 fl6.fl6_sport = params->sport;
4327 fl6.fl6_dport = params->dport;
4329 if (flags & BPF_FIB_LOOKUP_DIRECT) {
4330 u32 tbid = l3mdev_fib_table_rcu(dev) ? : RT_TABLE_MAIN;
4331 struct fib6_table *tb;
4333 tb = ipv6_stub->fib6_get_table(net, tbid);
4335 return BPF_FIB_LKUP_RET_NOT_FWDED;
4337 f6i = ipv6_stub->fib6_table_lookup(net, tb, oif, &fl6, strict);
4339 fl6.flowi6_mark = 0;
4340 fl6.flowi6_secid = 0;
4341 fl6.flowi6_tun_key.tun_id = 0;
4342 fl6.flowi6_uid = sock_net_uid(net, NULL);
4344 f6i = ipv6_stub->fib6_lookup(net, oif, &fl6, strict);
4347 if (unlikely(IS_ERR_OR_NULL(f6i) || f6i == net->ipv6.fib6_null_entry))
4348 return BPF_FIB_LKUP_RET_NOT_FWDED;
4350 if (unlikely(f6i->fib6_flags & RTF_REJECT)) {
4351 switch (f6i->fib6_type) {
4353 return BPF_FIB_LKUP_RET_BLACKHOLE;
4354 case RTN_UNREACHABLE:
4355 return BPF_FIB_LKUP_RET_UNREACHABLE;
4357 return BPF_FIB_LKUP_RET_PROHIBIT;
4359 return BPF_FIB_LKUP_RET_NOT_FWDED;
4363 if (f6i->fib6_type != RTN_UNICAST)
4364 return BPF_FIB_LKUP_RET_NOT_FWDED;
4366 if (f6i->fib6_nsiblings && fl6.flowi6_oif == 0)
4367 f6i = ipv6_stub->fib6_multipath_select(net, f6i, &fl6,
4368 fl6.flowi6_oif, NULL,
4372 mtu = ipv6_stub->ip6_mtu_from_fib6(f6i, dst, src);
4373 if (params->tot_len > mtu)
4374 return BPF_FIB_LKUP_RET_FRAG_NEEDED;
4377 if (f6i->fib6_nh.nh_lwtstate)
4378 return BPF_FIB_LKUP_RET_UNSUPP_LWT;
4380 if (f6i->fib6_flags & RTF_GATEWAY)
4381 *dst = f6i->fib6_nh.nh_gw;
4383 dev = f6i->fib6_nh.nh_dev;
4384 params->rt_metric = f6i->fib6_metric;
4386 /* xdp and cls_bpf programs are run in RCU-bh so rcu_read_lock_bh is
4387 * not needed here. Can not use __ipv6_neigh_lookup_noref here
4388 * because we need to get nd_tbl via the stub
4390 neigh = ___neigh_lookup_noref(ipv6_stub->nd_tbl, neigh_key_eq128,
4391 ndisc_hashfn, dst, dev);
4393 return BPF_FIB_LKUP_RET_NO_NEIGH;
4395 return bpf_fib_set_fwd_params(params, neigh, dev);
4399 BPF_CALL_4(bpf_xdp_fib_lookup, struct xdp_buff *, ctx,
4400 struct bpf_fib_lookup *, params, int, plen, u32, flags)
4402 if (plen < sizeof(*params))
4405 if (flags & ~(BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_OUTPUT))
4408 switch (params->family) {
4409 #if IS_ENABLED(CONFIG_INET)
4411 return bpf_ipv4_fib_lookup(dev_net(ctx->rxq->dev), params,
4414 #if IS_ENABLED(CONFIG_IPV6)
4416 return bpf_ipv6_fib_lookup(dev_net(ctx->rxq->dev), params,
4420 return -EAFNOSUPPORT;
4423 static const struct bpf_func_proto bpf_xdp_fib_lookup_proto = {
4424 .func = bpf_xdp_fib_lookup,
4426 .ret_type = RET_INTEGER,
4427 .arg1_type = ARG_PTR_TO_CTX,
4428 .arg2_type = ARG_PTR_TO_MEM,
4429 .arg3_type = ARG_CONST_SIZE,
4430 .arg4_type = ARG_ANYTHING,
4433 BPF_CALL_4(bpf_skb_fib_lookup, struct sk_buff *, skb,
4434 struct bpf_fib_lookup *, params, int, plen, u32, flags)
4436 struct net *net = dev_net(skb->dev);
4437 int rc = -EAFNOSUPPORT;
4439 if (plen < sizeof(*params))
4442 if (flags & ~(BPF_FIB_LOOKUP_DIRECT | BPF_FIB_LOOKUP_OUTPUT))
4445 switch (params->family) {
4446 #if IS_ENABLED(CONFIG_INET)
4448 rc = bpf_ipv4_fib_lookup(net, params, flags, false);
4451 #if IS_ENABLED(CONFIG_IPV6)
4453 rc = bpf_ipv6_fib_lookup(net, params, flags, false);
4459 struct net_device *dev;
4461 dev = dev_get_by_index_rcu(net, params->ifindex);
4462 if (!is_skb_forwardable(dev, skb))
4463 rc = BPF_FIB_LKUP_RET_FRAG_NEEDED;
4469 static const struct bpf_func_proto bpf_skb_fib_lookup_proto = {
4470 .func = bpf_skb_fib_lookup,
4472 .ret_type = RET_INTEGER,
4473 .arg1_type = ARG_PTR_TO_CTX,
4474 .arg2_type = ARG_PTR_TO_MEM,
4475 .arg3_type = ARG_CONST_SIZE,
4476 .arg4_type = ARG_ANYTHING,
4479 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
4480 static int bpf_push_seg6_encap(struct sk_buff *skb, u32 type, void *hdr, u32 len)
4483 struct ipv6_sr_hdr *srh = (struct ipv6_sr_hdr *)hdr;
4485 if (!seg6_validate_srh(srh, len))
4489 case BPF_LWT_ENCAP_SEG6_INLINE:
4490 if (skb->protocol != htons(ETH_P_IPV6))
4493 err = seg6_do_srh_inline(skb, srh);
4495 case BPF_LWT_ENCAP_SEG6:
4496 skb_reset_inner_headers(skb);
4497 skb->encapsulation = 1;
4498 err = seg6_do_srh_encap(skb, srh, IPPROTO_IPV6);
4504 bpf_compute_data_pointers(skb);
4508 ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
4509 skb_set_transport_header(skb, sizeof(struct ipv6hdr));
4511 return seg6_lookup_nexthop(skb, NULL, 0);
4513 #endif /* CONFIG_IPV6_SEG6_BPF */
4515 BPF_CALL_4(bpf_lwt_push_encap, struct sk_buff *, skb, u32, type, void *, hdr,
4519 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
4520 case BPF_LWT_ENCAP_SEG6:
4521 case BPF_LWT_ENCAP_SEG6_INLINE:
4522 return bpf_push_seg6_encap(skb, type, hdr, len);
4529 static const struct bpf_func_proto bpf_lwt_push_encap_proto = {
4530 .func = bpf_lwt_push_encap,
4532 .ret_type = RET_INTEGER,
4533 .arg1_type = ARG_PTR_TO_CTX,
4534 .arg2_type = ARG_ANYTHING,
4535 .arg3_type = ARG_PTR_TO_MEM,
4536 .arg4_type = ARG_CONST_SIZE
4539 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
4540 BPF_CALL_4(bpf_lwt_seg6_store_bytes, struct sk_buff *, skb, u32, offset,
4541 const void *, from, u32, len)
4543 struct seg6_bpf_srh_state *srh_state =
4544 this_cpu_ptr(&seg6_bpf_srh_states);
4545 void *srh_tlvs, *srh_end, *ptr;
4546 struct ipv6_sr_hdr *srh;
4549 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0)
4552 srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
4553 srh_tlvs = (void *)((char *)srh + ((srh->first_segment + 1) << 4));
4554 srh_end = (void *)((char *)srh + sizeof(*srh) + srh_state->hdrlen);
4556 ptr = skb->data + offset;
4557 if (ptr >= srh_tlvs && ptr + len <= srh_end)
4558 srh_state->valid = 0;
4559 else if (ptr < (void *)&srh->flags ||
4560 ptr + len > (void *)&srh->segments)
4563 if (unlikely(bpf_try_make_writable(skb, offset + len)))
4566 memcpy(skb->data + offset, from, len);
4570 static const struct bpf_func_proto bpf_lwt_seg6_store_bytes_proto = {
4571 .func = bpf_lwt_seg6_store_bytes,
4573 .ret_type = RET_INTEGER,
4574 .arg1_type = ARG_PTR_TO_CTX,
4575 .arg2_type = ARG_ANYTHING,
4576 .arg3_type = ARG_PTR_TO_MEM,
4577 .arg4_type = ARG_CONST_SIZE
4580 BPF_CALL_4(bpf_lwt_seg6_action, struct sk_buff *, skb,
4581 u32, action, void *, param, u32, param_len)
4583 struct seg6_bpf_srh_state *srh_state =
4584 this_cpu_ptr(&seg6_bpf_srh_states);
4585 struct ipv6_sr_hdr *srh;
4589 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0)
4591 srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
4593 if (!srh_state->valid) {
4594 if (unlikely((srh_state->hdrlen & 7) != 0))
4597 srh->hdrlen = (u8)(srh_state->hdrlen >> 3);
4598 if (unlikely(!seg6_validate_srh(srh, (srh->hdrlen + 1) << 3)))
4601 srh_state->valid = 1;
4605 case SEG6_LOCAL_ACTION_END_X:
4606 if (param_len != sizeof(struct in6_addr))
4608 return seg6_lookup_nexthop(skb, (struct in6_addr *)param, 0);
4609 case SEG6_LOCAL_ACTION_END_T:
4610 if (param_len != sizeof(int))
4612 return seg6_lookup_nexthop(skb, NULL, *(int *)param);
4613 case SEG6_LOCAL_ACTION_END_B6:
4614 err = bpf_push_seg6_encap(skb, BPF_LWT_ENCAP_SEG6_INLINE,
4618 ((struct ipv6_sr_hdr *)param)->hdrlen << 3;
4620 case SEG6_LOCAL_ACTION_END_B6_ENCAP:
4621 err = bpf_push_seg6_encap(skb, BPF_LWT_ENCAP_SEG6,
4625 ((struct ipv6_sr_hdr *)param)->hdrlen << 3;
4632 static const struct bpf_func_proto bpf_lwt_seg6_action_proto = {
4633 .func = bpf_lwt_seg6_action,
4635 .ret_type = RET_INTEGER,
4636 .arg1_type = ARG_PTR_TO_CTX,
4637 .arg2_type = ARG_ANYTHING,
4638 .arg3_type = ARG_PTR_TO_MEM,
4639 .arg4_type = ARG_CONST_SIZE
4642 BPF_CALL_3(bpf_lwt_seg6_adjust_srh, struct sk_buff *, skb, u32, offset,
4645 struct seg6_bpf_srh_state *srh_state =
4646 this_cpu_ptr(&seg6_bpf_srh_states);
4647 void *srh_end, *srh_tlvs, *ptr;
4648 struct ipv6_sr_hdr *srh;
4649 struct ipv6hdr *hdr;
4653 if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, NULL) < 0)
4655 srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
4657 srh_tlvs = (void *)((unsigned char *)srh + sizeof(*srh) +
4658 ((srh->first_segment + 1) << 4));
4659 srh_end = (void *)((unsigned char *)srh + sizeof(*srh) +
4661 ptr = skb->data + offset;
4663 if (unlikely(ptr < srh_tlvs || ptr > srh_end))
4665 if (unlikely(len < 0 && (void *)((char *)ptr - len) > srh_end))
4669 ret = skb_cow_head(skb, len);
4670 if (unlikely(ret < 0))
4673 ret = bpf_skb_net_hdr_push(skb, offset, len);
4675 ret = bpf_skb_net_hdr_pop(skb, offset, -1 * len);
4678 bpf_compute_data_pointers(skb);
4679 if (unlikely(ret < 0))
4682 hdr = (struct ipv6hdr *)skb->data;
4683 hdr->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
4685 srh_state->hdrlen += len;
4686 srh_state->valid = 0;
4690 static const struct bpf_func_proto bpf_lwt_seg6_adjust_srh_proto = {
4691 .func = bpf_lwt_seg6_adjust_srh,
4693 .ret_type = RET_INTEGER,
4694 .arg1_type = ARG_PTR_TO_CTX,
4695 .arg2_type = ARG_ANYTHING,
4696 .arg3_type = ARG_ANYTHING,
4698 #endif /* CONFIG_IPV6_SEG6_BPF */
4700 bool bpf_helper_changes_pkt_data(void *func)
4702 if (func == bpf_skb_vlan_push ||
4703 func == bpf_skb_vlan_pop ||
4704 func == bpf_skb_store_bytes ||
4705 func == bpf_skb_change_proto ||
4706 func == bpf_skb_change_head ||
4707 func == sk_skb_change_head ||
4708 func == bpf_skb_change_tail ||
4709 func == sk_skb_change_tail ||
4710 func == bpf_skb_adjust_room ||
4711 func == bpf_skb_pull_data ||
4712 func == sk_skb_pull_data ||
4713 func == bpf_clone_redirect ||
4714 func == bpf_l3_csum_replace ||
4715 func == bpf_l4_csum_replace ||
4716 func == bpf_xdp_adjust_head ||
4717 func == bpf_xdp_adjust_meta ||
4718 func == bpf_msg_pull_data ||
4719 func == bpf_xdp_adjust_tail ||
4720 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
4721 func == bpf_lwt_seg6_store_bytes ||
4722 func == bpf_lwt_seg6_adjust_srh ||
4723 func == bpf_lwt_seg6_action ||
4725 func == bpf_lwt_push_encap)
4731 static const struct bpf_func_proto *
4732 bpf_base_func_proto(enum bpf_func_id func_id)
4735 case BPF_FUNC_map_lookup_elem:
4736 return &bpf_map_lookup_elem_proto;
4737 case BPF_FUNC_map_update_elem:
4738 return &bpf_map_update_elem_proto;
4739 case BPF_FUNC_map_delete_elem:
4740 return &bpf_map_delete_elem_proto;
4741 case BPF_FUNC_get_prandom_u32:
4742 return &bpf_get_prandom_u32_proto;
4743 case BPF_FUNC_get_smp_processor_id:
4744 return &bpf_get_raw_smp_processor_id_proto;
4745 case BPF_FUNC_get_numa_node_id:
4746 return &bpf_get_numa_node_id_proto;
4747 case BPF_FUNC_tail_call:
4748 return &bpf_tail_call_proto;
4749 case BPF_FUNC_ktime_get_ns:
4750 return &bpf_ktime_get_ns_proto;
4751 case BPF_FUNC_trace_printk:
4752 if (capable(CAP_SYS_ADMIN))
4753 return bpf_get_trace_printk_proto();
4759 static const struct bpf_func_proto *
4760 sock_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4763 /* inet and inet6 sockets are created in a process
4764 * context so there is always a valid uid/gid
4766 case BPF_FUNC_get_current_uid_gid:
4767 return &bpf_get_current_uid_gid_proto;
4769 return bpf_base_func_proto(func_id);
4773 static const struct bpf_func_proto *
4774 sock_addr_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4777 /* inet and inet6 sockets are created in a process
4778 * context so there is always a valid uid/gid
4780 case BPF_FUNC_get_current_uid_gid:
4781 return &bpf_get_current_uid_gid_proto;
4783 switch (prog->expected_attach_type) {
4784 case BPF_CGROUP_INET4_CONNECT:
4785 case BPF_CGROUP_INET6_CONNECT:
4786 return &bpf_bind_proto;
4791 return bpf_base_func_proto(func_id);
4795 static const struct bpf_func_proto *
4796 sk_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4799 case BPF_FUNC_skb_load_bytes:
4800 return &bpf_skb_load_bytes_proto;
4801 case BPF_FUNC_skb_load_bytes_relative:
4802 return &bpf_skb_load_bytes_relative_proto;
4803 case BPF_FUNC_get_socket_cookie:
4804 return &bpf_get_socket_cookie_proto;
4805 case BPF_FUNC_get_socket_uid:
4806 return &bpf_get_socket_uid_proto;
4808 return bpf_base_func_proto(func_id);
4812 static const struct bpf_func_proto *
4813 tc_cls_act_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4816 case BPF_FUNC_skb_store_bytes:
4817 return &bpf_skb_store_bytes_proto;
4818 case BPF_FUNC_skb_load_bytes:
4819 return &bpf_skb_load_bytes_proto;
4820 case BPF_FUNC_skb_load_bytes_relative:
4821 return &bpf_skb_load_bytes_relative_proto;
4822 case BPF_FUNC_skb_pull_data:
4823 return &bpf_skb_pull_data_proto;
4824 case BPF_FUNC_csum_diff:
4825 return &bpf_csum_diff_proto;
4826 case BPF_FUNC_csum_update:
4827 return &bpf_csum_update_proto;
4828 case BPF_FUNC_l3_csum_replace:
4829 return &bpf_l3_csum_replace_proto;
4830 case BPF_FUNC_l4_csum_replace:
4831 return &bpf_l4_csum_replace_proto;
4832 case BPF_FUNC_clone_redirect:
4833 return &bpf_clone_redirect_proto;
4834 case BPF_FUNC_get_cgroup_classid:
4835 return &bpf_get_cgroup_classid_proto;
4836 case BPF_FUNC_skb_vlan_push:
4837 return &bpf_skb_vlan_push_proto;
4838 case BPF_FUNC_skb_vlan_pop:
4839 return &bpf_skb_vlan_pop_proto;
4840 case BPF_FUNC_skb_change_proto:
4841 return &bpf_skb_change_proto_proto;
4842 case BPF_FUNC_skb_change_type:
4843 return &bpf_skb_change_type_proto;
4844 case BPF_FUNC_skb_adjust_room:
4845 return &bpf_skb_adjust_room_proto;
4846 case BPF_FUNC_skb_change_tail:
4847 return &bpf_skb_change_tail_proto;
4848 case BPF_FUNC_skb_get_tunnel_key:
4849 return &bpf_skb_get_tunnel_key_proto;
4850 case BPF_FUNC_skb_set_tunnel_key:
4851 return bpf_get_skb_set_tunnel_proto(func_id);
4852 case BPF_FUNC_skb_get_tunnel_opt:
4853 return &bpf_skb_get_tunnel_opt_proto;
4854 case BPF_FUNC_skb_set_tunnel_opt:
4855 return bpf_get_skb_set_tunnel_proto(func_id);
4856 case BPF_FUNC_redirect:
4857 return &bpf_redirect_proto;
4858 case BPF_FUNC_get_route_realm:
4859 return &bpf_get_route_realm_proto;
4860 case BPF_FUNC_get_hash_recalc:
4861 return &bpf_get_hash_recalc_proto;
4862 case BPF_FUNC_set_hash_invalid:
4863 return &bpf_set_hash_invalid_proto;
4864 case BPF_FUNC_set_hash:
4865 return &bpf_set_hash_proto;
4866 case BPF_FUNC_perf_event_output:
4867 return &bpf_skb_event_output_proto;
4868 case BPF_FUNC_get_smp_processor_id:
4869 return &bpf_get_smp_processor_id_proto;
4870 case BPF_FUNC_skb_under_cgroup:
4871 return &bpf_skb_under_cgroup_proto;
4872 case BPF_FUNC_get_socket_cookie:
4873 return &bpf_get_socket_cookie_proto;
4874 case BPF_FUNC_get_socket_uid:
4875 return &bpf_get_socket_uid_proto;
4876 case BPF_FUNC_fib_lookup:
4877 return &bpf_skb_fib_lookup_proto;
4879 case BPF_FUNC_skb_get_xfrm_state:
4880 return &bpf_skb_get_xfrm_state_proto;
4882 #ifdef CONFIG_SOCK_CGROUP_DATA
4883 case BPF_FUNC_skb_cgroup_id:
4884 return &bpf_skb_cgroup_id_proto;
4887 return bpf_base_func_proto(func_id);
4891 static const struct bpf_func_proto *
4892 xdp_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4895 case BPF_FUNC_perf_event_output:
4896 return &bpf_xdp_event_output_proto;
4897 case BPF_FUNC_get_smp_processor_id:
4898 return &bpf_get_smp_processor_id_proto;
4899 case BPF_FUNC_csum_diff:
4900 return &bpf_csum_diff_proto;
4901 case BPF_FUNC_xdp_adjust_head:
4902 return &bpf_xdp_adjust_head_proto;
4903 case BPF_FUNC_xdp_adjust_meta:
4904 return &bpf_xdp_adjust_meta_proto;
4905 case BPF_FUNC_redirect:
4906 return &bpf_xdp_redirect_proto;
4907 case BPF_FUNC_redirect_map:
4908 return &bpf_xdp_redirect_map_proto;
4909 case BPF_FUNC_xdp_adjust_tail:
4910 return &bpf_xdp_adjust_tail_proto;
4911 case BPF_FUNC_fib_lookup:
4912 return &bpf_xdp_fib_lookup_proto;
4914 return bpf_base_func_proto(func_id);
4918 static const struct bpf_func_proto *
4919 sock_ops_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4922 case BPF_FUNC_setsockopt:
4923 return &bpf_setsockopt_proto;
4924 case BPF_FUNC_getsockopt:
4925 return &bpf_getsockopt_proto;
4926 case BPF_FUNC_sock_ops_cb_flags_set:
4927 return &bpf_sock_ops_cb_flags_set_proto;
4928 case BPF_FUNC_sock_map_update:
4929 return &bpf_sock_map_update_proto;
4930 case BPF_FUNC_sock_hash_update:
4931 return &bpf_sock_hash_update_proto;
4933 return bpf_base_func_proto(func_id);
4937 static const struct bpf_func_proto *
4938 sk_msg_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4941 case BPF_FUNC_msg_redirect_map:
4942 return &bpf_msg_redirect_map_proto;
4943 case BPF_FUNC_msg_redirect_hash:
4944 return &bpf_msg_redirect_hash_proto;
4945 case BPF_FUNC_msg_apply_bytes:
4946 return &bpf_msg_apply_bytes_proto;
4947 case BPF_FUNC_msg_cork_bytes:
4948 return &bpf_msg_cork_bytes_proto;
4949 case BPF_FUNC_msg_pull_data:
4950 return &bpf_msg_pull_data_proto;
4952 return bpf_base_func_proto(func_id);
4956 static const struct bpf_func_proto *
4957 sk_skb_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4960 case BPF_FUNC_skb_store_bytes:
4961 return &bpf_skb_store_bytes_proto;
4962 case BPF_FUNC_skb_load_bytes:
4963 return &bpf_skb_load_bytes_proto;
4964 case BPF_FUNC_skb_pull_data:
4965 return &sk_skb_pull_data_proto;
4966 case BPF_FUNC_skb_change_tail:
4967 return &sk_skb_change_tail_proto;
4968 case BPF_FUNC_skb_change_head:
4969 return &sk_skb_change_head_proto;
4970 case BPF_FUNC_get_socket_cookie:
4971 return &bpf_get_socket_cookie_proto;
4972 case BPF_FUNC_get_socket_uid:
4973 return &bpf_get_socket_uid_proto;
4974 case BPF_FUNC_sk_redirect_map:
4975 return &bpf_sk_redirect_map_proto;
4976 case BPF_FUNC_sk_redirect_hash:
4977 return &bpf_sk_redirect_hash_proto;
4979 return bpf_base_func_proto(func_id);
4983 static const struct bpf_func_proto *
4984 lwt_out_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4987 case BPF_FUNC_skb_load_bytes:
4988 return &bpf_skb_load_bytes_proto;
4989 case BPF_FUNC_skb_pull_data:
4990 return &bpf_skb_pull_data_proto;
4991 case BPF_FUNC_csum_diff:
4992 return &bpf_csum_diff_proto;
4993 case BPF_FUNC_get_cgroup_classid:
4994 return &bpf_get_cgroup_classid_proto;
4995 case BPF_FUNC_get_route_realm:
4996 return &bpf_get_route_realm_proto;
4997 case BPF_FUNC_get_hash_recalc:
4998 return &bpf_get_hash_recalc_proto;
4999 case BPF_FUNC_perf_event_output:
5000 return &bpf_skb_event_output_proto;
5001 case BPF_FUNC_get_smp_processor_id:
5002 return &bpf_get_smp_processor_id_proto;
5003 case BPF_FUNC_skb_under_cgroup:
5004 return &bpf_skb_under_cgroup_proto;
5006 return bpf_base_func_proto(func_id);
5010 static const struct bpf_func_proto *
5011 lwt_in_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5014 case BPF_FUNC_lwt_push_encap:
5015 return &bpf_lwt_push_encap_proto;
5017 return lwt_out_func_proto(func_id, prog);
5021 static const struct bpf_func_proto *
5022 lwt_xmit_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5025 case BPF_FUNC_skb_get_tunnel_key:
5026 return &bpf_skb_get_tunnel_key_proto;
5027 case BPF_FUNC_skb_set_tunnel_key:
5028 return bpf_get_skb_set_tunnel_proto(func_id);
5029 case BPF_FUNC_skb_get_tunnel_opt:
5030 return &bpf_skb_get_tunnel_opt_proto;
5031 case BPF_FUNC_skb_set_tunnel_opt:
5032 return bpf_get_skb_set_tunnel_proto(func_id);
5033 case BPF_FUNC_redirect:
5034 return &bpf_redirect_proto;
5035 case BPF_FUNC_clone_redirect:
5036 return &bpf_clone_redirect_proto;
5037 case BPF_FUNC_skb_change_tail:
5038 return &bpf_skb_change_tail_proto;
5039 case BPF_FUNC_skb_change_head:
5040 return &bpf_skb_change_head_proto;
5041 case BPF_FUNC_skb_store_bytes:
5042 return &bpf_skb_store_bytes_proto;
5043 case BPF_FUNC_csum_update:
5044 return &bpf_csum_update_proto;
5045 case BPF_FUNC_l3_csum_replace:
5046 return &bpf_l3_csum_replace_proto;
5047 case BPF_FUNC_l4_csum_replace:
5048 return &bpf_l4_csum_replace_proto;
5049 case BPF_FUNC_set_hash_invalid:
5050 return &bpf_set_hash_invalid_proto;
5052 return lwt_out_func_proto(func_id, prog);
5056 static const struct bpf_func_proto *
5057 lwt_seg6local_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5060 #if IS_ENABLED(CONFIG_IPV6_SEG6_BPF)
5061 case BPF_FUNC_lwt_seg6_store_bytes:
5062 return &bpf_lwt_seg6_store_bytes_proto;
5063 case BPF_FUNC_lwt_seg6_action:
5064 return &bpf_lwt_seg6_action_proto;
5065 case BPF_FUNC_lwt_seg6_adjust_srh:
5066 return &bpf_lwt_seg6_adjust_srh_proto;
5069 return lwt_out_func_proto(func_id, prog);
5073 static bool bpf_skb_is_valid_access(int off, int size, enum bpf_access_type type,
5074 const struct bpf_prog *prog,
5075 struct bpf_insn_access_aux *info)
5077 const int size_default = sizeof(__u32);
5079 if (off < 0 || off >= sizeof(struct __sk_buff))
5082 /* The verifier guarantees that size > 0. */
5083 if (off % size != 0)
5087 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
5088 if (off + size > offsetofend(struct __sk_buff, cb[4]))
5091 case bpf_ctx_range_till(struct __sk_buff, remote_ip6[0], remote_ip6[3]):
5092 case bpf_ctx_range_till(struct __sk_buff, local_ip6[0], local_ip6[3]):
5093 case bpf_ctx_range_till(struct __sk_buff, remote_ip4, remote_ip4):
5094 case bpf_ctx_range_till(struct __sk_buff, local_ip4, local_ip4):
5095 case bpf_ctx_range(struct __sk_buff, data):
5096 case bpf_ctx_range(struct __sk_buff, data_meta):
5097 case bpf_ctx_range(struct __sk_buff, data_end):
5098 if (size != size_default)
5102 /* Only narrow read access allowed for now. */
5103 if (type == BPF_WRITE) {
5104 if (size != size_default)
5107 bpf_ctx_record_field_size(info, size_default);
5108 if (!bpf_ctx_narrow_access_ok(off, size, size_default))
5116 static bool sk_filter_is_valid_access(int off, int size,
5117 enum bpf_access_type type,
5118 const struct bpf_prog *prog,
5119 struct bpf_insn_access_aux *info)
5122 case bpf_ctx_range(struct __sk_buff, tc_classid):
5123 case bpf_ctx_range(struct __sk_buff, data):
5124 case bpf_ctx_range(struct __sk_buff, data_meta):
5125 case bpf_ctx_range(struct __sk_buff, data_end):
5126 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
5130 if (type == BPF_WRITE) {
5132 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
5139 return bpf_skb_is_valid_access(off, size, type, prog, info);
5142 static bool lwt_is_valid_access(int off, int size,
5143 enum bpf_access_type type,
5144 const struct bpf_prog *prog,
5145 struct bpf_insn_access_aux *info)
5148 case bpf_ctx_range(struct __sk_buff, tc_classid):
5149 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
5150 case bpf_ctx_range(struct __sk_buff, data_meta):
5154 if (type == BPF_WRITE) {
5156 case bpf_ctx_range(struct __sk_buff, mark):
5157 case bpf_ctx_range(struct __sk_buff, priority):
5158 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
5166 case bpf_ctx_range(struct __sk_buff, data):
5167 info->reg_type = PTR_TO_PACKET;
5169 case bpf_ctx_range(struct __sk_buff, data_end):
5170 info->reg_type = PTR_TO_PACKET_END;
5174 return bpf_skb_is_valid_access(off, size, type, prog, info);
5177 /* Attach type specific accesses */
5178 static bool __sock_filter_check_attach_type(int off,
5179 enum bpf_access_type access_type,
5180 enum bpf_attach_type attach_type)
5183 case offsetof(struct bpf_sock, bound_dev_if):
5184 case offsetof(struct bpf_sock, mark):
5185 case offsetof(struct bpf_sock, priority):
5186 switch (attach_type) {
5187 case BPF_CGROUP_INET_SOCK_CREATE:
5192 case bpf_ctx_range(struct bpf_sock, src_ip4):
5193 switch (attach_type) {
5194 case BPF_CGROUP_INET4_POST_BIND:
5199 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
5200 switch (attach_type) {
5201 case BPF_CGROUP_INET6_POST_BIND:
5206 case bpf_ctx_range(struct bpf_sock, src_port):
5207 switch (attach_type) {
5208 case BPF_CGROUP_INET4_POST_BIND:
5209 case BPF_CGROUP_INET6_POST_BIND:
5216 return access_type == BPF_READ;
5221 static bool __sock_filter_check_size(int off, int size,
5222 struct bpf_insn_access_aux *info)
5224 const int size_default = sizeof(__u32);
5227 case bpf_ctx_range(struct bpf_sock, src_ip4):
5228 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
5229 bpf_ctx_record_field_size(info, size_default);
5230 return bpf_ctx_narrow_access_ok(off, size, size_default);
5233 return size == size_default;
5236 static bool sock_filter_is_valid_access(int off, int size,
5237 enum bpf_access_type type,
5238 const struct bpf_prog *prog,
5239 struct bpf_insn_access_aux *info)
5241 if (off < 0 || off >= sizeof(struct bpf_sock))
5243 if (off % size != 0)
5245 if (!__sock_filter_check_attach_type(off, type,
5246 prog->expected_attach_type))
5248 if (!__sock_filter_check_size(off, size, info))
5253 static int bpf_unclone_prologue(struct bpf_insn *insn_buf, bool direct_write,
5254 const struct bpf_prog *prog, int drop_verdict)
5256 struct bpf_insn *insn = insn_buf;
5261 /* if (!skb->cloned)
5264 * (Fast-path, otherwise approximation that we might be
5265 * a clone, do the rest in helper.)
5267 *insn++ = BPF_LDX_MEM(BPF_B, BPF_REG_6, BPF_REG_1, CLONED_OFFSET());
5268 *insn++ = BPF_ALU32_IMM(BPF_AND, BPF_REG_6, CLONED_MASK);
5269 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_6, 0, 7);
5271 /* ret = bpf_skb_pull_data(skb, 0); */
5272 *insn++ = BPF_MOV64_REG(BPF_REG_6, BPF_REG_1);
5273 *insn++ = BPF_ALU64_REG(BPF_XOR, BPF_REG_2, BPF_REG_2);
5274 *insn++ = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0,
5275 BPF_FUNC_skb_pull_data);
5278 * return TC_ACT_SHOT;
5280 *insn++ = BPF_JMP_IMM(BPF_JEQ, BPF_REG_0, 0, 2);
5281 *insn++ = BPF_ALU32_IMM(BPF_MOV, BPF_REG_0, drop_verdict);
5282 *insn++ = BPF_EXIT_INSN();
5285 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_6);
5287 *insn++ = prog->insnsi[0];
5289 return insn - insn_buf;
5292 static int bpf_gen_ld_abs(const struct bpf_insn *orig,
5293 struct bpf_insn *insn_buf)
5295 bool indirect = BPF_MODE(orig->code) == BPF_IND;
5296 struct bpf_insn *insn = insn_buf;
5298 /* We're guaranteed here that CTX is in R6. */
5299 *insn++ = BPF_MOV64_REG(BPF_REG_1, BPF_REG_CTX);
5301 *insn++ = BPF_MOV64_IMM(BPF_REG_2, orig->imm);
5303 *insn++ = BPF_MOV64_REG(BPF_REG_2, orig->src_reg);
5305 *insn++ = BPF_ALU64_IMM(BPF_ADD, BPF_REG_2, orig->imm);
5308 switch (BPF_SIZE(orig->code)) {
5310 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_8_no_cache);
5313 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_16_no_cache);
5316 *insn++ = BPF_EMIT_CALL(bpf_skb_load_helper_32_no_cache);
5320 *insn++ = BPF_JMP_IMM(BPF_JSGE, BPF_REG_0, 0, 2);
5321 *insn++ = BPF_ALU32_REG(BPF_XOR, BPF_REG_0, BPF_REG_0);
5322 *insn++ = BPF_EXIT_INSN();
5324 return insn - insn_buf;
5327 static int tc_cls_act_prologue(struct bpf_insn *insn_buf, bool direct_write,
5328 const struct bpf_prog *prog)
5330 return bpf_unclone_prologue(insn_buf, direct_write, prog, TC_ACT_SHOT);
5333 static bool tc_cls_act_is_valid_access(int off, int size,
5334 enum bpf_access_type type,
5335 const struct bpf_prog *prog,
5336 struct bpf_insn_access_aux *info)
5338 if (type == BPF_WRITE) {
5340 case bpf_ctx_range(struct __sk_buff, mark):
5341 case bpf_ctx_range(struct __sk_buff, tc_index):
5342 case bpf_ctx_range(struct __sk_buff, priority):
5343 case bpf_ctx_range(struct __sk_buff, tc_classid):
5344 case bpf_ctx_range_till(struct __sk_buff, cb[0], cb[4]):
5352 case bpf_ctx_range(struct __sk_buff, data):
5353 info->reg_type = PTR_TO_PACKET;
5355 case bpf_ctx_range(struct __sk_buff, data_meta):
5356 info->reg_type = PTR_TO_PACKET_META;
5358 case bpf_ctx_range(struct __sk_buff, data_end):
5359 info->reg_type = PTR_TO_PACKET_END;
5361 case bpf_ctx_range_till(struct __sk_buff, family, local_port):
5365 return bpf_skb_is_valid_access(off, size, type, prog, info);
5368 static bool __is_valid_xdp_access(int off, int size)
5370 if (off < 0 || off >= sizeof(struct xdp_md))
5372 if (off % size != 0)
5374 if (size != sizeof(__u32))
5380 static bool xdp_is_valid_access(int off, int size,
5381 enum bpf_access_type type,
5382 const struct bpf_prog *prog,
5383 struct bpf_insn_access_aux *info)
5385 if (type == BPF_WRITE) {
5386 if (bpf_prog_is_dev_bound(prog->aux)) {
5388 case offsetof(struct xdp_md, rx_queue_index):
5389 return __is_valid_xdp_access(off, size);
5396 case offsetof(struct xdp_md, data):
5397 info->reg_type = PTR_TO_PACKET;
5399 case offsetof(struct xdp_md, data_meta):
5400 info->reg_type = PTR_TO_PACKET_META;
5402 case offsetof(struct xdp_md, data_end):
5403 info->reg_type = PTR_TO_PACKET_END;
5407 return __is_valid_xdp_access(off, size);
5410 void bpf_warn_invalid_xdp_action(u32 act)
5412 const u32 act_max = XDP_REDIRECT;
5414 WARN_ONCE(1, "%s XDP return value %u, expect packet loss!\n",
5415 act > act_max ? "Illegal" : "Driver unsupported",
5418 EXPORT_SYMBOL_GPL(bpf_warn_invalid_xdp_action);
5420 static bool sock_addr_is_valid_access(int off, int size,
5421 enum bpf_access_type type,
5422 const struct bpf_prog *prog,
5423 struct bpf_insn_access_aux *info)
5425 const int size_default = sizeof(__u32);
5427 if (off < 0 || off >= sizeof(struct bpf_sock_addr))
5429 if (off % size != 0)
5432 /* Disallow access to IPv6 fields from IPv4 contex and vise
5436 case bpf_ctx_range(struct bpf_sock_addr, user_ip4):
5437 switch (prog->expected_attach_type) {
5438 case BPF_CGROUP_INET4_BIND:
5439 case BPF_CGROUP_INET4_CONNECT:
5440 case BPF_CGROUP_UDP4_SENDMSG:
5446 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
5447 switch (prog->expected_attach_type) {
5448 case BPF_CGROUP_INET6_BIND:
5449 case BPF_CGROUP_INET6_CONNECT:
5450 case BPF_CGROUP_UDP6_SENDMSG:
5456 case bpf_ctx_range(struct bpf_sock_addr, msg_src_ip4):
5457 switch (prog->expected_attach_type) {
5458 case BPF_CGROUP_UDP4_SENDMSG:
5464 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
5466 switch (prog->expected_attach_type) {
5467 case BPF_CGROUP_UDP6_SENDMSG:
5476 case bpf_ctx_range(struct bpf_sock_addr, user_ip4):
5477 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
5478 case bpf_ctx_range(struct bpf_sock_addr, msg_src_ip4):
5479 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
5481 /* Only narrow read access allowed for now. */
5482 if (type == BPF_READ) {
5483 bpf_ctx_record_field_size(info, size_default);
5484 if (!bpf_ctx_narrow_access_ok(off, size, size_default))
5487 if (size != size_default)
5491 case bpf_ctx_range(struct bpf_sock_addr, user_port):
5492 if (size != size_default)
5496 if (type == BPF_READ) {
5497 if (size != size_default)
5507 static bool sock_ops_is_valid_access(int off, int size,
5508 enum bpf_access_type type,
5509 const struct bpf_prog *prog,
5510 struct bpf_insn_access_aux *info)
5512 const int size_default = sizeof(__u32);
5514 if (off < 0 || off >= sizeof(struct bpf_sock_ops))
5517 /* The verifier guarantees that size > 0. */
5518 if (off % size != 0)
5521 if (type == BPF_WRITE) {
5523 case offsetof(struct bpf_sock_ops, reply):
5524 case offsetof(struct bpf_sock_ops, sk_txhash):
5525 if (size != size_default)
5533 case bpf_ctx_range_till(struct bpf_sock_ops, bytes_received,
5535 if (size != sizeof(__u64))
5539 if (size != size_default)
5548 static int sk_skb_prologue(struct bpf_insn *insn_buf, bool direct_write,
5549 const struct bpf_prog *prog)
5551 return bpf_unclone_prologue(insn_buf, direct_write, prog, SK_DROP);
5554 static bool sk_skb_is_valid_access(int off, int size,
5555 enum bpf_access_type type,
5556 const struct bpf_prog *prog,
5557 struct bpf_insn_access_aux *info)
5560 case bpf_ctx_range(struct __sk_buff, tc_classid):
5561 case bpf_ctx_range(struct __sk_buff, data_meta):
5565 if (type == BPF_WRITE) {
5567 case bpf_ctx_range(struct __sk_buff, tc_index):
5568 case bpf_ctx_range(struct __sk_buff, priority):
5576 case bpf_ctx_range(struct __sk_buff, mark):
5578 case bpf_ctx_range(struct __sk_buff, data):
5579 info->reg_type = PTR_TO_PACKET;
5581 case bpf_ctx_range(struct __sk_buff, data_end):
5582 info->reg_type = PTR_TO_PACKET_END;
5586 return bpf_skb_is_valid_access(off, size, type, prog, info);
5589 static bool sk_msg_is_valid_access(int off, int size,
5590 enum bpf_access_type type,
5591 const struct bpf_prog *prog,
5592 struct bpf_insn_access_aux *info)
5594 if (type == BPF_WRITE)
5598 case offsetof(struct sk_msg_md, data):
5599 info->reg_type = PTR_TO_PACKET;
5600 if (size != sizeof(__u64))
5603 case offsetof(struct sk_msg_md, data_end):
5604 info->reg_type = PTR_TO_PACKET_END;
5605 if (size != sizeof(__u64))
5609 if (size != sizeof(__u32))
5613 if (off < 0 || off >= sizeof(struct sk_msg_md))
5615 if (off % size != 0)
5621 static u32 bpf_convert_ctx_access(enum bpf_access_type type,
5622 const struct bpf_insn *si,
5623 struct bpf_insn *insn_buf,
5624 struct bpf_prog *prog, u32 *target_size)
5626 struct bpf_insn *insn = insn_buf;
5630 case offsetof(struct __sk_buff, len):
5631 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
5632 bpf_target_off(struct sk_buff, len, 4,
5636 case offsetof(struct __sk_buff, protocol):
5637 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
5638 bpf_target_off(struct sk_buff, protocol, 2,
5642 case offsetof(struct __sk_buff, vlan_proto):
5643 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
5644 bpf_target_off(struct sk_buff, vlan_proto, 2,
5648 case offsetof(struct __sk_buff, priority):
5649 if (type == BPF_WRITE)
5650 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
5651 bpf_target_off(struct sk_buff, priority, 4,
5654 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
5655 bpf_target_off(struct sk_buff, priority, 4,
5659 case offsetof(struct __sk_buff, ingress_ifindex):
5660 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
5661 bpf_target_off(struct sk_buff, skb_iif, 4,
5665 case offsetof(struct __sk_buff, ifindex):
5666 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
5667 si->dst_reg, si->src_reg,
5668 offsetof(struct sk_buff, dev));
5669 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
5670 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
5671 bpf_target_off(struct net_device, ifindex, 4,
5675 case offsetof(struct __sk_buff, hash):
5676 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
5677 bpf_target_off(struct sk_buff, hash, 4,
5681 case offsetof(struct __sk_buff, mark):
5682 if (type == BPF_WRITE)
5683 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
5684 bpf_target_off(struct sk_buff, mark, 4,
5687 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
5688 bpf_target_off(struct sk_buff, mark, 4,
5692 case offsetof(struct __sk_buff, pkt_type):
5694 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->src_reg,
5696 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, PKT_TYPE_MAX);
5697 #ifdef __BIG_ENDIAN_BITFIELD
5698 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, 5);
5702 case offsetof(struct __sk_buff, queue_mapping):
5703 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
5704 bpf_target_off(struct sk_buff, queue_mapping, 2,
5708 case offsetof(struct __sk_buff, vlan_present):
5709 case offsetof(struct __sk_buff, vlan_tci):
5710 BUILD_BUG_ON(VLAN_TAG_PRESENT != 0x1000);
5712 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
5713 bpf_target_off(struct sk_buff, vlan_tci, 2,
5715 if (si->off == offsetof(struct __sk_buff, vlan_tci)) {
5716 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg,
5719 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, 12);
5720 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, 1);
5724 case offsetof(struct __sk_buff, cb[0]) ...
5725 offsetofend(struct __sk_buff, cb[4]) - 1:
5726 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, data) < 20);
5727 BUILD_BUG_ON((offsetof(struct sk_buff, cb) +
5728 offsetof(struct qdisc_skb_cb, data)) %
5731 prog->cb_access = 1;
5733 off -= offsetof(struct __sk_buff, cb[0]);
5734 off += offsetof(struct sk_buff, cb);
5735 off += offsetof(struct qdisc_skb_cb, data);
5736 if (type == BPF_WRITE)
5737 *insn++ = BPF_STX_MEM(BPF_SIZE(si->code), si->dst_reg,
5740 *insn++ = BPF_LDX_MEM(BPF_SIZE(si->code), si->dst_reg,
5744 case offsetof(struct __sk_buff, tc_classid):
5745 BUILD_BUG_ON(FIELD_SIZEOF(struct qdisc_skb_cb, tc_classid) != 2);
5748 off -= offsetof(struct __sk_buff, tc_classid);
5749 off += offsetof(struct sk_buff, cb);
5750 off += offsetof(struct qdisc_skb_cb, tc_classid);
5752 if (type == BPF_WRITE)
5753 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg,
5756 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg,
5760 case offsetof(struct __sk_buff, data):
5761 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, data),
5762 si->dst_reg, si->src_reg,
5763 offsetof(struct sk_buff, data));
5766 case offsetof(struct __sk_buff, data_meta):
5768 off -= offsetof(struct __sk_buff, data_meta);
5769 off += offsetof(struct sk_buff, cb);
5770 off += offsetof(struct bpf_skb_data_end, data_meta);
5771 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
5775 case offsetof(struct __sk_buff, data_end):
5777 off -= offsetof(struct __sk_buff, data_end);
5778 off += offsetof(struct sk_buff, cb);
5779 off += offsetof(struct bpf_skb_data_end, data_end);
5780 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
5784 case offsetof(struct __sk_buff, tc_index):
5785 #ifdef CONFIG_NET_SCHED
5786 if (type == BPF_WRITE)
5787 *insn++ = BPF_STX_MEM(BPF_H, si->dst_reg, si->src_reg,
5788 bpf_target_off(struct sk_buff, tc_index, 2,
5791 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
5792 bpf_target_off(struct sk_buff, tc_index, 2,
5796 if (type == BPF_WRITE)
5797 *insn++ = BPF_MOV64_REG(si->dst_reg, si->dst_reg);
5799 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
5803 case offsetof(struct __sk_buff, napi_id):
5804 #if defined(CONFIG_NET_RX_BUSY_POLL)
5805 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
5806 bpf_target_off(struct sk_buff, napi_id, 4,
5808 *insn++ = BPF_JMP_IMM(BPF_JGE, si->dst_reg, MIN_NAPI_ID, 1);
5809 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
5812 *insn++ = BPF_MOV64_IMM(si->dst_reg, 0);
5815 case offsetof(struct __sk_buff, family):
5816 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_family) != 2);
5818 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
5819 si->dst_reg, si->src_reg,
5820 offsetof(struct sk_buff, sk));
5821 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
5822 bpf_target_off(struct sock_common,
5826 case offsetof(struct __sk_buff, remote_ip4):
5827 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_daddr) != 4);
5829 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
5830 si->dst_reg, si->src_reg,
5831 offsetof(struct sk_buff, sk));
5832 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
5833 bpf_target_off(struct sock_common,
5837 case offsetof(struct __sk_buff, local_ip4):
5838 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
5839 skc_rcv_saddr) != 4);
5841 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
5842 si->dst_reg, si->src_reg,
5843 offsetof(struct sk_buff, sk));
5844 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
5845 bpf_target_off(struct sock_common,
5849 case offsetof(struct __sk_buff, remote_ip6[0]) ...
5850 offsetof(struct __sk_buff, remote_ip6[3]):
5851 #if IS_ENABLED(CONFIG_IPV6)
5852 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
5853 skc_v6_daddr.s6_addr32[0]) != 4);
5856 off -= offsetof(struct __sk_buff, remote_ip6[0]);
5858 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
5859 si->dst_reg, si->src_reg,
5860 offsetof(struct sk_buff, sk));
5861 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
5862 offsetof(struct sock_common,
5863 skc_v6_daddr.s6_addr32[0]) +
5866 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
5869 case offsetof(struct __sk_buff, local_ip6[0]) ...
5870 offsetof(struct __sk_buff, local_ip6[3]):
5871 #if IS_ENABLED(CONFIG_IPV6)
5872 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
5873 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
5876 off -= offsetof(struct __sk_buff, local_ip6[0]);
5878 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
5879 si->dst_reg, si->src_reg,
5880 offsetof(struct sk_buff, sk));
5881 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
5882 offsetof(struct sock_common,
5883 skc_v6_rcv_saddr.s6_addr32[0]) +
5886 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
5890 case offsetof(struct __sk_buff, remote_port):
5891 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_dport) != 2);
5893 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
5894 si->dst_reg, si->src_reg,
5895 offsetof(struct sk_buff, sk));
5896 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
5897 bpf_target_off(struct sock_common,
5900 #ifndef __BIG_ENDIAN_BITFIELD
5901 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
5905 case offsetof(struct __sk_buff, local_port):
5906 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_num) != 2);
5908 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, sk),
5909 si->dst_reg, si->src_reg,
5910 offsetof(struct sk_buff, sk));
5911 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
5912 bpf_target_off(struct sock_common,
5913 skc_num, 2, target_size));
5917 return insn - insn_buf;
5920 static u32 sock_filter_convert_ctx_access(enum bpf_access_type type,
5921 const struct bpf_insn *si,
5922 struct bpf_insn *insn_buf,
5923 struct bpf_prog *prog, u32 *target_size)
5925 struct bpf_insn *insn = insn_buf;
5929 case offsetof(struct bpf_sock, bound_dev_if):
5930 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_bound_dev_if) != 4);
5932 if (type == BPF_WRITE)
5933 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
5934 offsetof(struct sock, sk_bound_dev_if));
5936 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
5937 offsetof(struct sock, sk_bound_dev_if));
5940 case offsetof(struct bpf_sock, mark):
5941 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_mark) != 4);
5943 if (type == BPF_WRITE)
5944 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
5945 offsetof(struct sock, sk_mark));
5947 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
5948 offsetof(struct sock, sk_mark));
5951 case offsetof(struct bpf_sock, priority):
5952 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_priority) != 4);
5954 if (type == BPF_WRITE)
5955 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
5956 offsetof(struct sock, sk_priority));
5958 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
5959 offsetof(struct sock, sk_priority));
5962 case offsetof(struct bpf_sock, family):
5963 BUILD_BUG_ON(FIELD_SIZEOF(struct sock, sk_family) != 2);
5965 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->src_reg,
5966 offsetof(struct sock, sk_family));
5969 case offsetof(struct bpf_sock, type):
5970 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
5971 offsetof(struct sock, __sk_flags_offset));
5972 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_TYPE_MASK);
5973 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, SK_FL_TYPE_SHIFT);
5976 case offsetof(struct bpf_sock, protocol):
5977 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
5978 offsetof(struct sock, __sk_flags_offset));
5979 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_PROTO_MASK);
5980 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, SK_FL_PROTO_SHIFT);
5983 case offsetof(struct bpf_sock, src_ip4):
5984 *insn++ = BPF_LDX_MEM(
5985 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
5986 bpf_target_off(struct sock_common, skc_rcv_saddr,
5987 FIELD_SIZEOF(struct sock_common,
5992 case bpf_ctx_range_till(struct bpf_sock, src_ip6[0], src_ip6[3]):
5993 #if IS_ENABLED(CONFIG_IPV6)
5995 off -= offsetof(struct bpf_sock, src_ip6[0]);
5996 *insn++ = BPF_LDX_MEM(
5997 BPF_SIZE(si->code), si->dst_reg, si->src_reg,
6000 skc_v6_rcv_saddr.s6_addr32[0],
6001 FIELD_SIZEOF(struct sock_common,
6002 skc_v6_rcv_saddr.s6_addr32[0]),
6003 target_size) + off);
6006 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
6010 case offsetof(struct bpf_sock, src_port):
6011 *insn++ = BPF_LDX_MEM(
6012 BPF_FIELD_SIZEOF(struct sock_common, skc_num),
6013 si->dst_reg, si->src_reg,
6014 bpf_target_off(struct sock_common, skc_num,
6015 FIELD_SIZEOF(struct sock_common,
6021 return insn - insn_buf;
6024 static u32 tc_cls_act_convert_ctx_access(enum bpf_access_type type,
6025 const struct bpf_insn *si,
6026 struct bpf_insn *insn_buf,
6027 struct bpf_prog *prog, u32 *target_size)
6029 struct bpf_insn *insn = insn_buf;
6032 case offsetof(struct __sk_buff, ifindex):
6033 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_buff, dev),
6034 si->dst_reg, si->src_reg,
6035 offsetof(struct sk_buff, dev));
6036 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6037 bpf_target_off(struct net_device, ifindex, 4,
6041 return bpf_convert_ctx_access(type, si, insn_buf, prog,
6045 return insn - insn_buf;
6048 static u32 xdp_convert_ctx_access(enum bpf_access_type type,
6049 const struct bpf_insn *si,
6050 struct bpf_insn *insn_buf,
6051 struct bpf_prog *prog, u32 *target_size)
6053 struct bpf_insn *insn = insn_buf;
6056 case offsetof(struct xdp_md, data):
6057 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data),
6058 si->dst_reg, si->src_reg,
6059 offsetof(struct xdp_buff, data));
6061 case offsetof(struct xdp_md, data_meta):
6062 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_meta),
6063 si->dst_reg, si->src_reg,
6064 offsetof(struct xdp_buff, data_meta));
6066 case offsetof(struct xdp_md, data_end):
6067 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, data_end),
6068 si->dst_reg, si->src_reg,
6069 offsetof(struct xdp_buff, data_end));
6071 case offsetof(struct xdp_md, ingress_ifindex):
6072 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, rxq),
6073 si->dst_reg, si->src_reg,
6074 offsetof(struct xdp_buff, rxq));
6075 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_rxq_info, dev),
6076 si->dst_reg, si->dst_reg,
6077 offsetof(struct xdp_rxq_info, dev));
6078 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6079 offsetof(struct net_device, ifindex));
6081 case offsetof(struct xdp_md, rx_queue_index):
6082 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, rxq),
6083 si->dst_reg, si->src_reg,
6084 offsetof(struct xdp_buff, rxq));
6085 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6086 offsetof(struct xdp_rxq_info,
6091 return insn - insn_buf;
6094 /* SOCK_ADDR_LOAD_NESTED_FIELD() loads Nested Field S.F.NF where S is type of
6095 * context Structure, F is Field in context structure that contains a pointer
6096 * to Nested Structure of type NS that has the field NF.
6098 * SIZE encodes the load size (BPF_B, BPF_H, etc). It's up to caller to make
6099 * sure that SIZE is not greater than actual size of S.F.NF.
6101 * If offset OFF is provided, the load happens from that offset relative to
6104 #define SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, SIZE, OFF) \
6106 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), si->dst_reg, \
6107 si->src_reg, offsetof(S, F)); \
6108 *insn++ = BPF_LDX_MEM( \
6109 SIZE, si->dst_reg, si->dst_reg, \
6110 bpf_target_off(NS, NF, FIELD_SIZEOF(NS, NF), \
6115 #define SOCK_ADDR_LOAD_NESTED_FIELD(S, NS, F, NF) \
6116 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, \
6117 BPF_FIELD_SIZEOF(NS, NF), 0)
6119 /* SOCK_ADDR_STORE_NESTED_FIELD_OFF() has semantic similar to
6120 * SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF() but for store operation.
6122 * It doesn't support SIZE argument though since narrow stores are not
6123 * supported for now.
6125 * In addition it uses Temporary Field TF (member of struct S) as the 3rd
6126 * "register" since two registers available in convert_ctx_access are not
6127 * enough: we can't override neither SRC, since it contains value to store, nor
6128 * DST since it contains pointer to context that may be used by later
6129 * instructions. But we need a temporary place to save pointer to nested
6130 * structure whose field we want to store to.
6132 #define SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, OFF, TF) \
6134 int tmp_reg = BPF_REG_9; \
6135 if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg) \
6137 if (si->src_reg == tmp_reg || si->dst_reg == tmp_reg) \
6139 *insn++ = BPF_STX_MEM(BPF_DW, si->dst_reg, tmp_reg, \
6141 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(S, F), tmp_reg, \
6142 si->dst_reg, offsetof(S, F)); \
6143 *insn++ = BPF_STX_MEM( \
6144 BPF_FIELD_SIZEOF(NS, NF), tmp_reg, si->src_reg, \
6145 bpf_target_off(NS, NF, FIELD_SIZEOF(NS, NF), \
6148 *insn++ = BPF_LDX_MEM(BPF_DW, tmp_reg, si->dst_reg, \
6152 #define SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(S, NS, F, NF, SIZE, OFF, \
6155 if (type == BPF_WRITE) { \
6156 SOCK_ADDR_STORE_NESTED_FIELD_OFF(S, NS, F, NF, OFF, \
6159 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF( \
6160 S, NS, F, NF, SIZE, OFF); \
6164 #define SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD(S, NS, F, NF, TF) \
6165 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF( \
6166 S, NS, F, NF, BPF_FIELD_SIZEOF(NS, NF), 0, TF)
6168 static u32 sock_addr_convert_ctx_access(enum bpf_access_type type,
6169 const struct bpf_insn *si,
6170 struct bpf_insn *insn_buf,
6171 struct bpf_prog *prog, u32 *target_size)
6173 struct bpf_insn *insn = insn_buf;
6177 case offsetof(struct bpf_sock_addr, user_family):
6178 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
6179 struct sockaddr, uaddr, sa_family);
6182 case offsetof(struct bpf_sock_addr, user_ip4):
6183 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
6184 struct bpf_sock_addr_kern, struct sockaddr_in, uaddr,
6185 sin_addr, BPF_SIZE(si->code), 0, tmp_reg);
6188 case bpf_ctx_range_till(struct bpf_sock_addr, user_ip6[0], user_ip6[3]):
6190 off -= offsetof(struct bpf_sock_addr, user_ip6[0]);
6191 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
6192 struct bpf_sock_addr_kern, struct sockaddr_in6, uaddr,
6193 sin6_addr.s6_addr32[0], BPF_SIZE(si->code), off,
6197 case offsetof(struct bpf_sock_addr, user_port):
6198 /* To get port we need to know sa_family first and then treat
6199 * sockaddr as either sockaddr_in or sockaddr_in6.
6200 * Though we can simplify since port field has same offset and
6201 * size in both structures.
6202 * Here we check this invariant and use just one of the
6203 * structures if it's true.
6205 BUILD_BUG_ON(offsetof(struct sockaddr_in, sin_port) !=
6206 offsetof(struct sockaddr_in6, sin6_port));
6207 BUILD_BUG_ON(FIELD_SIZEOF(struct sockaddr_in, sin_port) !=
6208 FIELD_SIZEOF(struct sockaddr_in6, sin6_port));
6209 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD(struct bpf_sock_addr_kern,
6210 struct sockaddr_in6, uaddr,
6211 sin6_port, tmp_reg);
6214 case offsetof(struct bpf_sock_addr, family):
6215 SOCK_ADDR_LOAD_NESTED_FIELD(struct bpf_sock_addr_kern,
6216 struct sock, sk, sk_family);
6219 case offsetof(struct bpf_sock_addr, type):
6220 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(
6221 struct bpf_sock_addr_kern, struct sock, sk,
6222 __sk_flags_offset, BPF_W, 0);
6223 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_TYPE_MASK);
6224 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg, SK_FL_TYPE_SHIFT);
6227 case offsetof(struct bpf_sock_addr, protocol):
6228 SOCK_ADDR_LOAD_NESTED_FIELD_SIZE_OFF(
6229 struct bpf_sock_addr_kern, struct sock, sk,
6230 __sk_flags_offset, BPF_W, 0);
6231 *insn++ = BPF_ALU32_IMM(BPF_AND, si->dst_reg, SK_FL_PROTO_MASK);
6232 *insn++ = BPF_ALU32_IMM(BPF_RSH, si->dst_reg,
6236 case offsetof(struct bpf_sock_addr, msg_src_ip4):
6237 /* Treat t_ctx as struct in_addr for msg_src_ip4. */
6238 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
6239 struct bpf_sock_addr_kern, struct in_addr, t_ctx,
6240 s_addr, BPF_SIZE(si->code), 0, tmp_reg);
6243 case bpf_ctx_range_till(struct bpf_sock_addr, msg_src_ip6[0],
6246 off -= offsetof(struct bpf_sock_addr, msg_src_ip6[0]);
6247 /* Treat t_ctx as struct in6_addr for msg_src_ip6. */
6248 SOCK_ADDR_LOAD_OR_STORE_NESTED_FIELD_SIZE_OFF(
6249 struct bpf_sock_addr_kern, struct in6_addr, t_ctx,
6250 s6_addr32[0], BPF_SIZE(si->code), off, tmp_reg);
6254 return insn - insn_buf;
6257 static u32 sock_ops_convert_ctx_access(enum bpf_access_type type,
6258 const struct bpf_insn *si,
6259 struct bpf_insn *insn_buf,
6260 struct bpf_prog *prog,
6263 struct bpf_insn *insn = insn_buf;
6267 case offsetof(struct bpf_sock_ops, op) ...
6268 offsetof(struct bpf_sock_ops, replylong[3]):
6269 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, op) !=
6270 FIELD_SIZEOF(struct bpf_sock_ops_kern, op));
6271 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, reply) !=
6272 FIELD_SIZEOF(struct bpf_sock_ops_kern, reply));
6273 BUILD_BUG_ON(FIELD_SIZEOF(struct bpf_sock_ops, replylong) !=
6274 FIELD_SIZEOF(struct bpf_sock_ops_kern, replylong));
6276 off -= offsetof(struct bpf_sock_ops, op);
6277 off += offsetof(struct bpf_sock_ops_kern, op);
6278 if (type == BPF_WRITE)
6279 *insn++ = BPF_STX_MEM(BPF_W, si->dst_reg, si->src_reg,
6282 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->src_reg,
6286 case offsetof(struct bpf_sock_ops, family):
6287 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_family) != 2);
6289 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6290 struct bpf_sock_ops_kern, sk),
6291 si->dst_reg, si->src_reg,
6292 offsetof(struct bpf_sock_ops_kern, sk));
6293 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
6294 offsetof(struct sock_common, skc_family));
6297 case offsetof(struct bpf_sock_ops, remote_ip4):
6298 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_daddr) != 4);
6300 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6301 struct bpf_sock_ops_kern, sk),
6302 si->dst_reg, si->src_reg,
6303 offsetof(struct bpf_sock_ops_kern, sk));
6304 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6305 offsetof(struct sock_common, skc_daddr));
6308 case offsetof(struct bpf_sock_ops, local_ip4):
6309 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
6310 skc_rcv_saddr) != 4);
6312 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6313 struct bpf_sock_ops_kern, sk),
6314 si->dst_reg, si->src_reg,
6315 offsetof(struct bpf_sock_ops_kern, sk));
6316 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6317 offsetof(struct sock_common,
6321 case offsetof(struct bpf_sock_ops, remote_ip6[0]) ...
6322 offsetof(struct bpf_sock_ops, remote_ip6[3]):
6323 #if IS_ENABLED(CONFIG_IPV6)
6324 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
6325 skc_v6_daddr.s6_addr32[0]) != 4);
6328 off -= offsetof(struct bpf_sock_ops, remote_ip6[0]);
6329 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6330 struct bpf_sock_ops_kern, sk),
6331 si->dst_reg, si->src_reg,
6332 offsetof(struct bpf_sock_ops_kern, sk));
6333 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6334 offsetof(struct sock_common,
6335 skc_v6_daddr.s6_addr32[0]) +
6338 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
6342 case offsetof(struct bpf_sock_ops, local_ip6[0]) ...
6343 offsetof(struct bpf_sock_ops, local_ip6[3]):
6344 #if IS_ENABLED(CONFIG_IPV6)
6345 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
6346 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
6349 off -= offsetof(struct bpf_sock_ops, local_ip6[0]);
6350 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6351 struct bpf_sock_ops_kern, sk),
6352 si->dst_reg, si->src_reg,
6353 offsetof(struct bpf_sock_ops_kern, sk));
6354 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6355 offsetof(struct sock_common,
6356 skc_v6_rcv_saddr.s6_addr32[0]) +
6359 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
6363 case offsetof(struct bpf_sock_ops, remote_port):
6364 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_dport) != 2);
6366 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6367 struct bpf_sock_ops_kern, sk),
6368 si->dst_reg, si->src_reg,
6369 offsetof(struct bpf_sock_ops_kern, sk));
6370 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
6371 offsetof(struct sock_common, skc_dport));
6372 #ifndef __BIG_ENDIAN_BITFIELD
6373 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
6377 case offsetof(struct bpf_sock_ops, local_port):
6378 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_num) != 2);
6380 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6381 struct bpf_sock_ops_kern, sk),
6382 si->dst_reg, si->src_reg,
6383 offsetof(struct bpf_sock_ops_kern, sk));
6384 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
6385 offsetof(struct sock_common, skc_num));
6388 case offsetof(struct bpf_sock_ops, is_fullsock):
6389 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6390 struct bpf_sock_ops_kern,
6392 si->dst_reg, si->src_reg,
6393 offsetof(struct bpf_sock_ops_kern,
6397 case offsetof(struct bpf_sock_ops, state):
6398 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_state) != 1);
6400 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6401 struct bpf_sock_ops_kern, sk),
6402 si->dst_reg, si->src_reg,
6403 offsetof(struct bpf_sock_ops_kern, sk));
6404 *insn++ = BPF_LDX_MEM(BPF_B, si->dst_reg, si->dst_reg,
6405 offsetof(struct sock_common, skc_state));
6408 case offsetof(struct bpf_sock_ops, rtt_min):
6409 BUILD_BUG_ON(FIELD_SIZEOF(struct tcp_sock, rtt_min) !=
6410 sizeof(struct minmax));
6411 BUILD_BUG_ON(sizeof(struct minmax) <
6412 sizeof(struct minmax_sample));
6414 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6415 struct bpf_sock_ops_kern, sk),
6416 si->dst_reg, si->src_reg,
6417 offsetof(struct bpf_sock_ops_kern, sk));
6418 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6419 offsetof(struct tcp_sock, rtt_min) +
6420 FIELD_SIZEOF(struct minmax_sample, t));
6423 /* Helper macro for adding read access to tcp_sock or sock fields. */
6424 #define SOCK_OPS_GET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ) \
6426 BUILD_BUG_ON(FIELD_SIZEOF(OBJ, OBJ_FIELD) > \
6427 FIELD_SIZEOF(struct bpf_sock_ops, BPF_FIELD)); \
6428 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
6429 struct bpf_sock_ops_kern, \
6431 si->dst_reg, si->src_reg, \
6432 offsetof(struct bpf_sock_ops_kern, \
6434 *insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 2); \
6435 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
6436 struct bpf_sock_ops_kern, sk),\
6437 si->dst_reg, si->src_reg, \
6438 offsetof(struct bpf_sock_ops_kern, sk));\
6439 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(OBJ, \
6441 si->dst_reg, si->dst_reg, \
6442 offsetof(OBJ, OBJ_FIELD)); \
6445 /* Helper macro for adding write access to tcp_sock or sock fields.
6446 * The macro is called with two registers, dst_reg which contains a pointer
6447 * to ctx (context) and src_reg which contains the value that should be
6448 * stored. However, we need an additional register since we cannot overwrite
6449 * dst_reg because it may be used later in the program.
6450 * Instead we "borrow" one of the other register. We first save its value
6451 * into a new (temp) field in bpf_sock_ops_kern, use it, and then restore
6452 * it at the end of the macro.
6454 #define SOCK_OPS_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ) \
6456 int reg = BPF_REG_9; \
6457 BUILD_BUG_ON(FIELD_SIZEOF(OBJ, OBJ_FIELD) > \
6458 FIELD_SIZEOF(struct bpf_sock_ops, BPF_FIELD)); \
6459 if (si->dst_reg == reg || si->src_reg == reg) \
6461 if (si->dst_reg == reg || si->src_reg == reg) \
6463 *insn++ = BPF_STX_MEM(BPF_DW, si->dst_reg, reg, \
6464 offsetof(struct bpf_sock_ops_kern, \
6466 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
6467 struct bpf_sock_ops_kern, \
6470 offsetof(struct bpf_sock_ops_kern, \
6472 *insn++ = BPF_JMP_IMM(BPF_JEQ, reg, 0, 2); \
6473 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( \
6474 struct bpf_sock_ops_kern, sk),\
6476 offsetof(struct bpf_sock_ops_kern, sk));\
6477 *insn++ = BPF_STX_MEM(BPF_FIELD_SIZEOF(OBJ, OBJ_FIELD), \
6479 offsetof(OBJ, OBJ_FIELD)); \
6480 *insn++ = BPF_LDX_MEM(BPF_DW, reg, si->dst_reg, \
6481 offsetof(struct bpf_sock_ops_kern, \
6485 #define SOCK_OPS_GET_OR_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ, TYPE) \
6487 if (TYPE == BPF_WRITE) \
6488 SOCK_OPS_SET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ); \
6490 SOCK_OPS_GET_FIELD(BPF_FIELD, OBJ_FIELD, OBJ); \
6493 case offsetof(struct bpf_sock_ops, snd_cwnd):
6494 SOCK_OPS_GET_FIELD(snd_cwnd, snd_cwnd, struct tcp_sock);
6497 case offsetof(struct bpf_sock_ops, srtt_us):
6498 SOCK_OPS_GET_FIELD(srtt_us, srtt_us, struct tcp_sock);
6501 case offsetof(struct bpf_sock_ops, bpf_sock_ops_cb_flags):
6502 SOCK_OPS_GET_FIELD(bpf_sock_ops_cb_flags, bpf_sock_ops_cb_flags,
6506 case offsetof(struct bpf_sock_ops, snd_ssthresh):
6507 SOCK_OPS_GET_FIELD(snd_ssthresh, snd_ssthresh, struct tcp_sock);
6510 case offsetof(struct bpf_sock_ops, rcv_nxt):
6511 SOCK_OPS_GET_FIELD(rcv_nxt, rcv_nxt, struct tcp_sock);
6514 case offsetof(struct bpf_sock_ops, snd_nxt):
6515 SOCK_OPS_GET_FIELD(snd_nxt, snd_nxt, struct tcp_sock);
6518 case offsetof(struct bpf_sock_ops, snd_una):
6519 SOCK_OPS_GET_FIELD(snd_una, snd_una, struct tcp_sock);
6522 case offsetof(struct bpf_sock_ops, mss_cache):
6523 SOCK_OPS_GET_FIELD(mss_cache, mss_cache, struct tcp_sock);
6526 case offsetof(struct bpf_sock_ops, ecn_flags):
6527 SOCK_OPS_GET_FIELD(ecn_flags, ecn_flags, struct tcp_sock);
6530 case offsetof(struct bpf_sock_ops, rate_delivered):
6531 SOCK_OPS_GET_FIELD(rate_delivered, rate_delivered,
6535 case offsetof(struct bpf_sock_ops, rate_interval_us):
6536 SOCK_OPS_GET_FIELD(rate_interval_us, rate_interval_us,
6540 case offsetof(struct bpf_sock_ops, packets_out):
6541 SOCK_OPS_GET_FIELD(packets_out, packets_out, struct tcp_sock);
6544 case offsetof(struct bpf_sock_ops, retrans_out):
6545 SOCK_OPS_GET_FIELD(retrans_out, retrans_out, struct tcp_sock);
6548 case offsetof(struct bpf_sock_ops, total_retrans):
6549 SOCK_OPS_GET_FIELD(total_retrans, total_retrans,
6553 case offsetof(struct bpf_sock_ops, segs_in):
6554 SOCK_OPS_GET_FIELD(segs_in, segs_in, struct tcp_sock);
6557 case offsetof(struct bpf_sock_ops, data_segs_in):
6558 SOCK_OPS_GET_FIELD(data_segs_in, data_segs_in, struct tcp_sock);
6561 case offsetof(struct bpf_sock_ops, segs_out):
6562 SOCK_OPS_GET_FIELD(segs_out, segs_out, struct tcp_sock);
6565 case offsetof(struct bpf_sock_ops, data_segs_out):
6566 SOCK_OPS_GET_FIELD(data_segs_out, data_segs_out,
6570 case offsetof(struct bpf_sock_ops, lost_out):
6571 SOCK_OPS_GET_FIELD(lost_out, lost_out, struct tcp_sock);
6574 case offsetof(struct bpf_sock_ops, sacked_out):
6575 SOCK_OPS_GET_FIELD(sacked_out, sacked_out, struct tcp_sock);
6578 case offsetof(struct bpf_sock_ops, sk_txhash):
6579 SOCK_OPS_GET_OR_SET_FIELD(sk_txhash, sk_txhash,
6583 case offsetof(struct bpf_sock_ops, bytes_received):
6584 SOCK_OPS_GET_FIELD(bytes_received, bytes_received,
6588 case offsetof(struct bpf_sock_ops, bytes_acked):
6589 SOCK_OPS_GET_FIELD(bytes_acked, bytes_acked, struct tcp_sock);
6593 return insn - insn_buf;
6596 static u32 sk_skb_convert_ctx_access(enum bpf_access_type type,
6597 const struct bpf_insn *si,
6598 struct bpf_insn *insn_buf,
6599 struct bpf_prog *prog, u32 *target_size)
6601 struct bpf_insn *insn = insn_buf;
6605 case offsetof(struct __sk_buff, data_end):
6607 off -= offsetof(struct __sk_buff, data_end);
6608 off += offsetof(struct sk_buff, cb);
6609 off += offsetof(struct tcp_skb_cb, bpf.data_end);
6610 *insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
6614 return bpf_convert_ctx_access(type, si, insn_buf, prog,
6618 return insn - insn_buf;
6621 static u32 sk_msg_convert_ctx_access(enum bpf_access_type type,
6622 const struct bpf_insn *si,
6623 struct bpf_insn *insn_buf,
6624 struct bpf_prog *prog, u32 *target_size)
6626 struct bpf_insn *insn = insn_buf;
6627 #if IS_ENABLED(CONFIG_IPV6)
6632 case offsetof(struct sk_msg_md, data):
6633 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg_buff, data),
6634 si->dst_reg, si->src_reg,
6635 offsetof(struct sk_msg_buff, data));
6637 case offsetof(struct sk_msg_md, data_end):
6638 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg_buff, data_end),
6639 si->dst_reg, si->src_reg,
6640 offsetof(struct sk_msg_buff, data_end));
6642 case offsetof(struct sk_msg_md, family):
6643 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_family) != 2);
6645 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6646 struct sk_msg_buff, sk),
6647 si->dst_reg, si->src_reg,
6648 offsetof(struct sk_msg_buff, sk));
6649 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
6650 offsetof(struct sock_common, skc_family));
6653 case offsetof(struct sk_msg_md, remote_ip4):
6654 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_daddr) != 4);
6656 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6657 struct sk_msg_buff, sk),
6658 si->dst_reg, si->src_reg,
6659 offsetof(struct sk_msg_buff, sk));
6660 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6661 offsetof(struct sock_common, skc_daddr));
6664 case offsetof(struct sk_msg_md, local_ip4):
6665 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
6666 skc_rcv_saddr) != 4);
6668 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6669 struct sk_msg_buff, sk),
6670 si->dst_reg, si->src_reg,
6671 offsetof(struct sk_msg_buff, sk));
6672 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6673 offsetof(struct sock_common,
6677 case offsetof(struct sk_msg_md, remote_ip6[0]) ...
6678 offsetof(struct sk_msg_md, remote_ip6[3]):
6679 #if IS_ENABLED(CONFIG_IPV6)
6680 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
6681 skc_v6_daddr.s6_addr32[0]) != 4);
6684 off -= offsetof(struct sk_msg_md, remote_ip6[0]);
6685 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6686 struct sk_msg_buff, sk),
6687 si->dst_reg, si->src_reg,
6688 offsetof(struct sk_msg_buff, sk));
6689 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6690 offsetof(struct sock_common,
6691 skc_v6_daddr.s6_addr32[0]) +
6694 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
6698 case offsetof(struct sk_msg_md, local_ip6[0]) ...
6699 offsetof(struct sk_msg_md, local_ip6[3]):
6700 #if IS_ENABLED(CONFIG_IPV6)
6701 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common,
6702 skc_v6_rcv_saddr.s6_addr32[0]) != 4);
6705 off -= offsetof(struct sk_msg_md, local_ip6[0]);
6706 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6707 struct sk_msg_buff, sk),
6708 si->dst_reg, si->src_reg,
6709 offsetof(struct sk_msg_buff, sk));
6710 *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
6711 offsetof(struct sock_common,
6712 skc_v6_rcv_saddr.s6_addr32[0]) +
6715 *insn++ = BPF_MOV32_IMM(si->dst_reg, 0);
6719 case offsetof(struct sk_msg_md, remote_port):
6720 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_dport) != 2);
6722 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6723 struct sk_msg_buff, sk),
6724 si->dst_reg, si->src_reg,
6725 offsetof(struct sk_msg_buff, sk));
6726 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
6727 offsetof(struct sock_common, skc_dport));
6728 #ifndef __BIG_ENDIAN_BITFIELD
6729 *insn++ = BPF_ALU32_IMM(BPF_LSH, si->dst_reg, 16);
6733 case offsetof(struct sk_msg_md, local_port):
6734 BUILD_BUG_ON(FIELD_SIZEOF(struct sock_common, skc_num) != 2);
6736 *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
6737 struct sk_msg_buff, sk),
6738 si->dst_reg, si->src_reg,
6739 offsetof(struct sk_msg_buff, sk));
6740 *insn++ = BPF_LDX_MEM(BPF_H, si->dst_reg, si->dst_reg,
6741 offsetof(struct sock_common, skc_num));
6745 return insn - insn_buf;
6748 const struct bpf_verifier_ops sk_filter_verifier_ops = {
6749 .get_func_proto = sk_filter_func_proto,
6750 .is_valid_access = sk_filter_is_valid_access,
6751 .convert_ctx_access = bpf_convert_ctx_access,
6752 .gen_ld_abs = bpf_gen_ld_abs,
6755 const struct bpf_prog_ops sk_filter_prog_ops = {
6756 .test_run = bpf_prog_test_run_skb,
6759 const struct bpf_verifier_ops tc_cls_act_verifier_ops = {
6760 .get_func_proto = tc_cls_act_func_proto,
6761 .is_valid_access = tc_cls_act_is_valid_access,
6762 .convert_ctx_access = tc_cls_act_convert_ctx_access,
6763 .gen_prologue = tc_cls_act_prologue,
6764 .gen_ld_abs = bpf_gen_ld_abs,
6767 const struct bpf_prog_ops tc_cls_act_prog_ops = {
6768 .test_run = bpf_prog_test_run_skb,
6771 const struct bpf_verifier_ops xdp_verifier_ops = {
6772 .get_func_proto = xdp_func_proto,
6773 .is_valid_access = xdp_is_valid_access,
6774 .convert_ctx_access = xdp_convert_ctx_access,
6777 const struct bpf_prog_ops xdp_prog_ops = {
6778 .test_run = bpf_prog_test_run_xdp,
6781 const struct bpf_verifier_ops cg_skb_verifier_ops = {
6782 .get_func_proto = sk_filter_func_proto,
6783 .is_valid_access = sk_filter_is_valid_access,
6784 .convert_ctx_access = bpf_convert_ctx_access,
6787 const struct bpf_prog_ops cg_skb_prog_ops = {
6788 .test_run = bpf_prog_test_run_skb,
6791 const struct bpf_verifier_ops lwt_in_verifier_ops = {
6792 .get_func_proto = lwt_in_func_proto,
6793 .is_valid_access = lwt_is_valid_access,
6794 .convert_ctx_access = bpf_convert_ctx_access,
6797 const struct bpf_prog_ops lwt_in_prog_ops = {
6798 .test_run = bpf_prog_test_run_skb,
6801 const struct bpf_verifier_ops lwt_out_verifier_ops = {
6802 .get_func_proto = lwt_out_func_proto,
6803 .is_valid_access = lwt_is_valid_access,
6804 .convert_ctx_access = bpf_convert_ctx_access,
6807 const struct bpf_prog_ops lwt_out_prog_ops = {
6808 .test_run = bpf_prog_test_run_skb,
6811 const struct bpf_verifier_ops lwt_xmit_verifier_ops = {
6812 .get_func_proto = lwt_xmit_func_proto,
6813 .is_valid_access = lwt_is_valid_access,
6814 .convert_ctx_access = bpf_convert_ctx_access,
6815 .gen_prologue = tc_cls_act_prologue,
6818 const struct bpf_prog_ops lwt_xmit_prog_ops = {
6819 .test_run = bpf_prog_test_run_skb,
6822 const struct bpf_verifier_ops lwt_seg6local_verifier_ops = {
6823 .get_func_proto = lwt_seg6local_func_proto,
6824 .is_valid_access = lwt_is_valid_access,
6825 .convert_ctx_access = bpf_convert_ctx_access,
6828 const struct bpf_prog_ops lwt_seg6local_prog_ops = {
6829 .test_run = bpf_prog_test_run_skb,
6832 const struct bpf_verifier_ops cg_sock_verifier_ops = {
6833 .get_func_proto = sock_filter_func_proto,
6834 .is_valid_access = sock_filter_is_valid_access,
6835 .convert_ctx_access = sock_filter_convert_ctx_access,
6838 const struct bpf_prog_ops cg_sock_prog_ops = {
6841 const struct bpf_verifier_ops cg_sock_addr_verifier_ops = {
6842 .get_func_proto = sock_addr_func_proto,
6843 .is_valid_access = sock_addr_is_valid_access,
6844 .convert_ctx_access = sock_addr_convert_ctx_access,
6847 const struct bpf_prog_ops cg_sock_addr_prog_ops = {
6850 const struct bpf_verifier_ops sock_ops_verifier_ops = {
6851 .get_func_proto = sock_ops_func_proto,
6852 .is_valid_access = sock_ops_is_valid_access,
6853 .convert_ctx_access = sock_ops_convert_ctx_access,
6856 const struct bpf_prog_ops sock_ops_prog_ops = {
6859 const struct bpf_verifier_ops sk_skb_verifier_ops = {
6860 .get_func_proto = sk_skb_func_proto,
6861 .is_valid_access = sk_skb_is_valid_access,
6862 .convert_ctx_access = sk_skb_convert_ctx_access,
6863 .gen_prologue = sk_skb_prologue,
6866 const struct bpf_prog_ops sk_skb_prog_ops = {
6869 const struct bpf_verifier_ops sk_msg_verifier_ops = {
6870 .get_func_proto = sk_msg_func_proto,
6871 .is_valid_access = sk_msg_is_valid_access,
6872 .convert_ctx_access = sk_msg_convert_ctx_access,
6875 const struct bpf_prog_ops sk_msg_prog_ops = {
6878 int sk_detach_filter(struct sock *sk)
6881 struct sk_filter *filter;
6883 if (sock_flag(sk, SOCK_FILTER_LOCKED))
6886 filter = rcu_dereference_protected(sk->sk_filter,
6887 lockdep_sock_is_held(sk));
6889 RCU_INIT_POINTER(sk->sk_filter, NULL);
6890 sk_filter_uncharge(sk, filter);
6896 EXPORT_SYMBOL_GPL(sk_detach_filter);
6898 int sk_get_filter(struct sock *sk, struct sock_filter __user *ubuf,
6901 struct sock_fprog_kern *fprog;
6902 struct sk_filter *filter;
6906 filter = rcu_dereference_protected(sk->sk_filter,
6907 lockdep_sock_is_held(sk));
6911 /* We're copying the filter that has been originally attached,
6912 * so no conversion/decode needed anymore. eBPF programs that
6913 * have no original program cannot be dumped through this.
6916 fprog = filter->prog->orig_prog;
6922 /* User space only enquires number of filter blocks. */
6926 if (len < fprog->len)
6930 if (copy_to_user(ubuf, fprog->filter, bpf_classic_proglen(fprog)))
6933 /* Instead of bytes, the API requests to return the number